1/*
2 * © Copyright 2018 Alyssa Rosenzweig
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 */
24
25#ifndef __BUILDER_H__
26#define __BUILDER_H__
27
28#define _LARGEFILE64_SOURCE 1
29#define CACHE_LINE_SIZE 1024 /* TODO */
30#include <sys/mman.h>
31#include <assert.h>
32#include "pan_resource.h"
33#include "pan_job.h"
34
35#include "pipe/p_compiler.h"
36#include "pipe/p_config.h"
37#include "pipe/p_context.h"
38#include "pipe/p_defines.h"
39#include "pipe/p_format.h"
40#include "pipe/p_screen.h"
41#include "pipe/p_state.h"
42#include "util/u_blitter.h"
43#include "util/hash_table.h"
44
45#include "midgard/midgard_compile.h"
46
47/* Forward declare to avoid extra header dep */
48struct prim_convert_context;
49
50#define MAX_DRAW_CALLS 4096
51#define MAX_VARYINGS   4096
52
53//#define PAN_DIRTY_CLEAR	     (1 << 0)
54#define PAN_DIRTY_RASTERIZER (1 << 2)
55#define PAN_DIRTY_FS	     (1 << 3)
56#define PAN_DIRTY_FRAG_CORE  (PAN_DIRTY_FS) /* Dirty writes are tied */
57#define PAN_DIRTY_VS	     (1 << 4)
58#define PAN_DIRTY_VERTEX     (1 << 5)
59#define PAN_DIRTY_VERT_BUF   (1 << 6)
60//#define PAN_DIRTY_VIEWPORT   (1 << 7)
61#define PAN_DIRTY_SAMPLERS   (1 << 8)
62#define PAN_DIRTY_TEXTURES   (1 << 9)
63
64#define SET_BIT(lval, bit, cond) \
65	if (cond) \
66		lval |= (bit); \
67	else \
68		lval &= ~(bit);
69
70struct panfrost_constant_buffer {
71        bool dirty;
72        size_t size;
73        void *buffer;
74};
75
76struct panfrost_query {
77        /* Passthrough from Gallium */
78        unsigned type;
79        unsigned index;
80
81        /* Memory for the GPU to writeback the value of the query */
82        struct panfrost_transfer transfer;
83};
84
85struct panfrost_fence {
86        struct pipe_reference reference;
87        int fd;
88};
89
90#define PANFROST_MAX_TRANSIENT_ENTRIES 64
91
92struct panfrost_transient_pool {
93        /* Memory blocks in the pool */
94        struct panfrost_memory_entry *entries[PANFROST_MAX_TRANSIENT_ENTRIES];
95
96        /* Number of entries we own */
97        unsigned entry_count;
98
99        /* Current entry that we are writing to, zero-indexed, strictly less than entry_count */
100        unsigned entry_index;
101
102        /* Number of bytes into the current entry we are */
103        off_t entry_offset;
104
105        /* Entry size (all entries must be homogenous) */
106        size_t entry_size;
107};
108
109struct panfrost_context {
110        /* Gallium context */
111        struct pipe_context base;
112
113        /* Bound job and map of panfrost_job_key to jobs */
114        struct panfrost_job *job;
115        struct hash_table *jobs;
116
117        /* panfrost_resource -> panfrost_job */
118        struct hash_table *write_jobs;
119
120        /* Bit mask for supported PIPE_DRAW for this hardware */
121        unsigned draw_modes;
122
123        struct pipe_framebuffer_state pipe_framebuffer;
124
125        /* The number of concurrent FBOs allowed depends on the number of pools
126         * used; pools are ringed for parallelism opportunities */
127
128        struct panfrost_transient_pool transient_pools[2];
129        int cmdstream_i;
130
131        struct panfrost_memory cmdstream_persistent;
132        struct panfrost_memory shaders;
133        struct panfrost_memory scratchpad;
134        struct panfrost_memory tiler_heap;
135        struct panfrost_memory varying_mem;
136        struct panfrost_memory misc_0;
137        struct panfrost_memory misc_1;
138        struct panfrost_memory depth_stencil_buffer;
139
140        struct panfrost_query *occlusion_query;
141
142        /* Each draw has corresponding vertex and tiler payloads */
143        struct midgard_payload_vertex_tiler payload_vertex;
144        struct midgard_payload_vertex_tiler payload_tiler;
145
146        /* The fragment shader binary itself is pointed here (for the tripipe) but
147         * also everything else in the shader core, including blending, the
148         * stencil/depth tests, etc. Refer to the presentations. */
149
150        struct mali_shader_meta fragment_shader_core;
151
152        /* A frame is composed of a starting set value job, a number of vertex
153         * and tiler jobs, linked to the fragment job at the end. See the
154         * presentations for more information how this works */
155
156        unsigned draw_count;
157
158        mali_ptr set_value_job;
159        mali_ptr vertex_jobs[MAX_DRAW_CALLS];
160        mali_ptr tiler_jobs[MAX_DRAW_CALLS];
161
162        struct mali_job_descriptor_header *u_set_value_job;
163        struct mali_job_descriptor_header *u_vertex_jobs[MAX_DRAW_CALLS];
164        struct mali_job_descriptor_header *u_tiler_jobs[MAX_DRAW_CALLS];
165
166        unsigned vertex_job_count;
167        unsigned tiler_job_count;
168
169        /* Per-draw Dirty flags are setup like any other driver */
170        int dirty;
171
172        unsigned vertex_count;
173
174        union mali_attr attributes[PIPE_MAX_ATTRIBS];
175
176        unsigned varying_height;
177
178        struct mali_single_framebuffer vt_framebuffer_sfbd;
179        struct bifrost_framebuffer vt_framebuffer_mfbd;
180
181        /* TODO: Multiple uniform buffers (index =/= 0), finer updates? */
182
183        struct panfrost_constant_buffer constant_buffer[PIPE_SHADER_TYPES];
184
185        /* CSOs */
186        struct panfrost_rasterizer *rasterizer;
187
188        struct panfrost_shader_variants *vs;
189        struct panfrost_shader_variants *fs;
190
191        struct panfrost_vertex_state *vertex;
192
193        struct pipe_vertex_buffer vertex_buffers[PIPE_MAX_ATTRIBS];
194        uint32_t vb_mask;
195
196        struct panfrost_sampler_state *samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
197        unsigned sampler_count[PIPE_SHADER_TYPES];
198
199        struct panfrost_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_SAMPLER_VIEWS];
200        unsigned sampler_view_count[PIPE_SHADER_TYPES];
201
202        struct primconvert_context *primconvert;
203        struct blitter_context *blitter;
204
205        struct panfrost_blend_state *blend;
206
207        struct pipe_viewport_state pipe_viewport;
208        struct pipe_scissor_state scissor;
209        struct pipe_blend_color blend_color;
210        struct pipe_depth_stencil_alpha_state *depth_stencil;
211        struct pipe_stencil_ref stencil_ref;
212
213        /* True for t6XX, false for t8xx. */
214        bool is_t6xx;
215
216        /* If set, we'll require the use of single render-target framebuffer
217         * descriptors (SFBD), for older hardware -- specifically, <T760 hardware, If
218         * false, we'll use the MFBD no matter what. New hardware -does- retain support
219         * for SFBD, and in theory we could flip between them on a per-RT basis, but
220         * there's no real advantage to doing so */
221        bool require_sfbd;
222
223	uint32_t out_sync;
224};
225
226/* Corresponds to the CSO */
227
228struct panfrost_rasterizer {
229        struct pipe_rasterizer_state base;
230
231        /* Bitmask of front face, etc */
232        unsigned tiler_gl_enables;
233};
234
235struct panfrost_blend_state {
236        struct pipe_blend_state base;
237
238        /* Whether a blend shader is in use */
239        bool has_blend_shader;
240
241        /* Compiled fixed function command */
242        struct mali_blend_equation equation;
243
244        /* Compiled blend shader */
245        mali_ptr blend_shader;
246        int blend_work_count;
247};
248
249/* Variants bundle together to form the backing CSO, bundling multiple
250 * shaders with varying emulated features baked in (alpha test
251 * parameters, etc) */
252#define MAX_SHADER_VARIANTS 8
253
254/* A shader state corresponds to the actual, current variant of the shader */
255struct panfrost_shader_state {
256        struct pipe_shader_state *base;
257
258        /* Compiled, mapped descriptor, ready for the hardware */
259        bool compiled;
260        struct mali_shader_meta *tripipe;
261        mali_ptr tripipe_gpu;
262
263        /* Non-descript information */
264        int uniform_count;
265        bool can_discard;
266        bool writes_point_size;
267        bool reads_point_coord;
268
269        unsigned general_varying_stride;
270        struct mali_attr_meta varyings[PIPE_MAX_ATTRIBS];
271
272        unsigned sysval_count;
273        unsigned sysval[MAX_SYSVAL_COUNT];
274
275        /* Information on this particular shader variant */
276        struct pipe_alpha_state alpha_state;
277};
278
279/* A collection of varyings (the CSO) */
280struct panfrost_shader_variants {
281        struct pipe_shader_state base;
282
283        struct panfrost_shader_state variants[MAX_SHADER_VARIANTS];
284        unsigned variant_count;
285
286        /* The current active variant */
287        unsigned active_variant;
288};
289
290struct panfrost_vertex_state {
291        unsigned num_elements;
292
293        struct pipe_vertex_element pipe[PIPE_MAX_ATTRIBS];
294        struct mali_attr_meta hw[PIPE_MAX_ATTRIBS];
295};
296
297struct panfrost_sampler_state {
298        struct pipe_sampler_state base;
299        struct mali_sampler_descriptor hw;
300};
301
302/* Misnomer: Sampler view corresponds to textures, not samplers */
303
304struct panfrost_sampler_view {
305        struct pipe_sampler_view base;
306        struct mali_texture_descriptor hw;
307};
308
309static inline struct panfrost_context *
310pan_context(struct pipe_context *pcontext)
311{
312        return (struct panfrost_context *) pcontext;
313}
314
315static inline struct panfrost_screen *
316pan_screen(struct pipe_screen *p)
317{
318   return (struct panfrost_screen *)p;
319}
320
321struct pipe_context *
322panfrost_create_context(struct pipe_screen *screen, void *priv, unsigned flags);
323
324void
325panfrost_emit_for_draw(struct panfrost_context *ctx, bool with_vertex_data);
326
327struct panfrost_transfer
328panfrost_vertex_tiler_job(struct panfrost_context *ctx, bool is_tiler, bool is_elided_tiler);
329
330unsigned
331panfrost_get_default_swizzle(unsigned components);
332
333void
334panfrost_flush(
335        struct pipe_context *pipe,
336        struct pipe_fence_handle **fence,
337        unsigned flags);
338
339bool
340panfrost_is_scanout(struct panfrost_context *ctx);
341
342mali_ptr
343panfrost_sfbd_fragment(struct panfrost_context *ctx, bool flip_y);
344
345mali_ptr
346panfrost_mfbd_fragment(struct panfrost_context *ctx, bool flip_y);
347
348struct bifrost_framebuffer
349panfrost_emit_mfbd(struct panfrost_context *ctx);
350
351struct mali_single_framebuffer
352panfrost_emit_sfbd(struct panfrost_context *ctx);
353
354mali_ptr
355panfrost_fragment_job(struct panfrost_context *ctx);
356
357void
358panfrost_shader_compile(struct panfrost_context *ctx, struct mali_shader_meta *meta, const char *src, int type, struct panfrost_shader_state *state);
359
360#endif
361