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 #include <sys/mman.h>
30 #include <assert.h>
31 #include "pan_resource.h"
32 #include "pan_job.h"
33 #include "pan_blend_cso.h"
34 #include "pan_encoder.h"
35 #include "pan_texture.h"
36 
37 #include "pipe/p_compiler.h"
38 #include "pipe/p_config.h"
39 #include "pipe/p_context.h"
40 #include "pipe/p_defines.h"
41 #include "pipe/p_format.h"
42 #include "pipe/p_screen.h"
43 #include "pipe/p_state.h"
44 #include "util/u_blitter.h"
45 #include "util/hash_table.h"
46 #include "util/simple_mtx.h"
47 
48 #include "midgard/midgard_compile.h"
49 #include "compiler/shader_enums.h"
50 
51 /* Forward declare to avoid extra header dep */
52 struct prim_convert_context;
53 
54 #define SET_BIT(lval, bit, cond) \
55 	if (cond) \
56 		lval |= (bit); \
57 	else \
58 		lval &= ~(bit);
59 
60 /* Dirty tracking flags. 3D is for general 3D state. Shader flags are
61  * per-stage. Renderer refers to Renderer State Descriptors. Vertex refers to
62  * vertex attributes/elements. */
63 
64 enum pan_dirty_3d {
65         PAN_DIRTY_VIEWPORT       = BITFIELD_BIT(0),
66         PAN_DIRTY_SCISSOR        = BITFIELD_BIT(1),
67         PAN_DIRTY_VERTEX         = BITFIELD_BIT(2),
68         PAN_DIRTY_PARAMS         = BITFIELD_BIT(3),
69         PAN_DIRTY_DRAWID         = BITFIELD_BIT(4),
70         PAN_DIRTY_TLS_SIZE       = BITFIELD_BIT(5),
71 };
72 
73 enum pan_dirty_shader {
74         PAN_DIRTY_STAGE_RENDERER = BITFIELD_BIT(0),
75         PAN_DIRTY_STAGE_TEXTURE  = BITFIELD_BIT(1),
76         PAN_DIRTY_STAGE_SAMPLER  = BITFIELD_BIT(2),
77         PAN_DIRTY_STAGE_IMAGE    = BITFIELD_BIT(3),
78         PAN_DIRTY_STAGE_CONST    = BITFIELD_BIT(4),
79         PAN_DIRTY_STAGE_SSBO     = BITFIELD_BIT(5),
80 };
81 
82 struct panfrost_constant_buffer {
83         struct pipe_constant_buffer cb[PIPE_MAX_CONSTANT_BUFFERS];
84         uint32_t enabled_mask;
85 };
86 
87 struct panfrost_query {
88         /* Passthrough from Gallium */
89         unsigned type;
90         unsigned index;
91 
92         /* For computed queries. 64-bit to prevent overflow */
93         struct {
94                 uint64_t start;
95                 uint64_t end;
96         };
97 
98         /* Memory for the GPU to writeback the value of the query */
99         struct pipe_resource *rsrc;
100 
101         /* Whether an occlusion query is for a MSAA framebuffer */
102         bool msaa;
103 };
104 
105 struct pipe_fence_handle {
106         struct pipe_reference reference;
107         uint32_t syncobj;
108         bool signaled;
109 };
110 
111 struct panfrost_streamout_target {
112         struct pipe_stream_output_target base;
113         uint32_t offset;
114 };
115 
116 struct panfrost_streamout {
117         struct pipe_stream_output_target *targets[PIPE_MAX_SO_BUFFERS];
118         unsigned num_targets;
119 };
120 
121 struct panfrost_context {
122         /* Gallium context */
123         struct pipe_context base;
124 
125         /* Dirty global state */
126         enum pan_dirty_3d dirty;
127 
128         /* Per shader stage dirty state */
129         enum pan_dirty_shader dirty_shader[PIPE_SHADER_TYPES];
130 
131         /* Unowned pools, so manage yourself. */
132         struct panfrost_pool descs, shaders;
133 
134         /* Sync obj used to keep track of in-flight jobs. */
135         uint32_t syncobj;
136 
137         /* Set of 32 batches. When the set is full, the LRU entry (the batch
138          * with the smallest seqnum) is flushed to free a slot.
139          */
140         struct {
141                 uint64_t seqnum;
142                 struct panfrost_batch slots[PAN_MAX_BATCHES];
143 
144                 /** Set of active batches for faster traversal */
145                 BITSET_DECLARE(active, PAN_MAX_BATCHES);
146         } batches;
147 
148         /* Map from resources to panfrost_batches */
149         struct hash_table *writers;
150 
151         /* Bound job batch */
152         struct panfrost_batch *batch;
153 
154         /* Within a launch_grid call.. */
155         const struct pipe_grid_info *compute_grid;
156 
157         struct pipe_framebuffer_state pipe_framebuffer;
158         struct panfrost_streamout streamout;
159 
160         bool active_queries;
161         uint64_t prims_generated;
162         uint64_t tf_prims_generated;
163         struct panfrost_query *occlusion_query;
164 
165         bool indirect_draw;
166         unsigned drawid;
167         unsigned vertex_count;
168         unsigned instance_count;
169         unsigned offset_start;
170         unsigned base_vertex;
171         unsigned base_instance;
172         mali_ptr first_vertex_sysval_ptr;
173         mali_ptr base_vertex_sysval_ptr;
174         mali_ptr base_instance_sysval_ptr;
175         enum pipe_prim_type active_prim;
176 
177         /* If instancing is enabled, vertex count padded for instance; if
178          * it is disabled, just equal to plain vertex count */
179         unsigned padded_count;
180 
181         struct panfrost_constant_buffer constant_buffer[PIPE_SHADER_TYPES];
182         struct panfrost_rasterizer *rasterizer;
183         struct panfrost_shader_variants *shader[PIPE_SHADER_TYPES];
184         struct panfrost_vertex_state *vertex;
185 
186         struct pipe_vertex_buffer vertex_buffers[PIPE_MAX_ATTRIBS];
187         uint32_t vb_mask;
188 
189         struct pipe_shader_buffer ssbo[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_BUFFERS];
190         uint32_t ssbo_mask[PIPE_SHADER_TYPES];
191 
192         struct pipe_image_view images[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_IMAGES];
193         uint32_t image_mask[PIPE_SHADER_TYPES];
194 
195         struct panfrost_sampler_state *samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
196         unsigned sampler_count[PIPE_SHADER_TYPES];
197 
198         struct panfrost_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_SAMPLER_VIEWS];
199         unsigned sampler_view_count[PIPE_SHADER_TYPES];
200 
201         struct blitter_context *blitter;
202 
203         struct panfrost_blend_state *blend;
204 
205         struct pipe_viewport_state pipe_viewport;
206         struct pipe_scissor_state scissor;
207         struct pipe_blend_color blend_color;
208         struct panfrost_zsa_state *depth_stencil;
209         struct pipe_stencil_ref stencil_ref;
210         uint16_t sample_mask;
211         unsigned min_samples;
212 
213         struct panfrost_query *cond_query;
214         bool cond_cond;
215         enum pipe_render_cond_flag cond_mode;
216 
217         bool is_noop;
218 
219         /* Mask of active render targets */
220         uint8_t fb_rt_mask;
221 };
222 
223 /* Corresponds to the CSO */
224 
225 struct panfrost_rasterizer;
226 
227 /* Linked varyings */
228 struct pan_linkage {
229         /* If the upload is owned by the CSO instead
230          * of the pool, the referenced BO. Else,
231          * NULL. */
232         struct panfrost_bo *bo;
233 
234         /* Uploaded attribute descriptors */
235         mali_ptr producer, consumer;
236 
237         /* Varyings buffers required */
238         uint32_t present;
239 
240         /* Per-vertex stride for general varying buffer */
241         uint32_t stride;
242 };
243 
244 #define RSD_WORDS 16
245 
246 /* Variants bundle together to form the backing CSO, bundling multiple
247  * shaders with varying emulated features baked in */
248 
249 /* A shader state corresponds to the actual, current variant of the shader */
250 struct panfrost_shader_state {
251         /* Compiled, mapped descriptor, ready for the hardware */
252         bool compiled;
253 
254         /* Respectively, shader binary and Renderer State Descriptor */
255         struct panfrost_pool_ref bin, state;
256 
257         /* For fragment shaders, a prepared (but not uploaded RSD) */
258         uint32_t partial_rsd[RSD_WORDS];
259 
260         struct pan_shader_info info;
261 
262         /* Linked varyings, for non-separable programs */
263         struct pan_linkage linkage;
264 
265         struct pipe_stream_output_info stream_output;
266         uint64_t so_mask;
267 
268         /* Variants */
269         enum pipe_format rt_formats[8];
270         unsigned nr_cbufs;
271 
272         /* Mask of state that dirties the sysvals */
273         unsigned dirty_3d, dirty_shader;
274 };
275 
276 /* A collection of varyings (the CSO) */
277 struct panfrost_shader_variants {
278         /* A panfrost_shader_variants can represent a shader for
279          * either graphics or compute */
280 
281         bool is_compute;
282 
283         union {
284                 struct pipe_shader_state base;
285                 struct pipe_compute_state cbase;
286         };
287 
288         /** Lock for the variants array */
289         simple_mtx_t lock;
290 
291         struct panfrost_shader_state *variants;
292         unsigned variant_space;
293 
294         unsigned variant_count;
295 
296         /* The current active variant */
297         unsigned active_variant;
298 };
299 
300 struct pan_vertex_buffer {
301         unsigned vbi;
302         unsigned divisor;
303 };
304 
305 struct panfrost_vertex_state {
306         unsigned num_elements;
307 
308         /* buffers corresponds to attribute buffer, element_buffers corresponds
309          * to an index in buffers for each vertex element */
310         struct pan_vertex_buffer buffers[PIPE_MAX_ATTRIBS];
311         unsigned element_buffer[PIPE_MAX_ATTRIBS];
312         unsigned nr_bufs;
313 
314         struct pipe_vertex_element pipe[PIPE_MAX_ATTRIBS];
315         unsigned formats[PIPE_MAX_ATTRIBS];
316 };
317 
318 struct panfrost_zsa_state;
319 struct panfrost_sampler_state;
320 struct panfrost_sampler_view;
321 
322 static inline struct panfrost_context *
pan_context(struct pipe_context * pcontext)323 pan_context(struct pipe_context *pcontext)
324 {
325         return (struct panfrost_context *) pcontext;
326 }
327 
328 static inline struct panfrost_streamout_target *
pan_so_target(struct pipe_stream_output_target * target)329 pan_so_target(struct pipe_stream_output_target *target)
330 {
331         return (struct panfrost_streamout_target *)target;
332 }
333 
334 static inline struct panfrost_shader_state *
panfrost_get_shader_state(struct panfrost_context * ctx,enum pipe_shader_type st)335 panfrost_get_shader_state(struct panfrost_context *ctx,
336                           enum pipe_shader_type st)
337 {
338         struct panfrost_shader_variants *all = ctx->shader[st];
339 
340         if (!all)
341                 return NULL;
342 
343         return &all->variants[all->active_variant];
344 }
345 
346 struct pipe_context *
347 panfrost_create_context(struct pipe_screen *screen, void *priv, unsigned flags);
348 
349 bool
350 panfrost_writes_point_size(struct panfrost_context *ctx);
351 
352 struct panfrost_ptr
353 panfrost_vertex_tiler_job(struct panfrost_context *ctx, bool is_tiler);
354 
355 void
356 panfrost_flush(
357         struct pipe_context *pipe,
358         struct pipe_fence_handle **fence,
359         unsigned flags);
360 
361 bool
362 panfrost_render_condition_check(struct panfrost_context *ctx);
363 
364 void
365 panfrost_shader_compile(struct pipe_screen *pscreen,
366                         struct panfrost_pool *shader_pool,
367                         struct panfrost_pool *desc_pool,
368                         enum pipe_shader_ir ir_type,
369                         const void *ir,
370                         gl_shader_stage stage,
371                         struct panfrost_shader_state *state);
372 
373 void
374 panfrost_analyze_sysvals(struct panfrost_shader_state *ss);
375 
376 mali_ptr
377 panfrost_get_index_buffer_bounded(struct panfrost_batch *batch,
378                                   const struct pipe_draw_info *info,
379                                   const struct pipe_draw_start_count_bias *draw,
380                                   unsigned *min_index, unsigned *max_index);
381 
382 /* Instancing */
383 
384 mali_ptr
385 panfrost_vertex_buffer_address(struct panfrost_context *ctx, unsigned i);
386 
387 /* Compute */
388 
389 void
390 panfrost_compute_context_init(struct pipe_context *pctx);
391 
392 static inline void
panfrost_dirty_state_all(struct panfrost_context * ctx)393 panfrost_dirty_state_all(struct panfrost_context *ctx)
394 {
395         ctx->dirty = ~0;
396 
397         for (unsigned i = 0; i < PIPE_SHADER_TYPES; ++i)
398                 ctx->dirty_shader[i] = ~0;
399 }
400 
401 static inline void
panfrost_clean_state_3d(struct panfrost_context * ctx)402 panfrost_clean_state_3d(struct panfrost_context *ctx)
403 {
404         ctx->dirty = 0;
405 
406         for (unsigned i = 0; i < PIPE_SHADER_TYPES; ++i) {
407                 if (i != PIPE_SHADER_COMPUTE)
408                         ctx->dirty_shader[i] = 0;
409         }
410 }
411 
412 #endif
413