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 <assert.h>
30 #include <sys/mman.h>
31 #include "pan_afbc_cso.h"
32 #include "pan_blend_cso.h"
33 #include "pan_earlyzs.h"
34 #include "pan_encoder.h"
35 #include "pan_job.h"
36 #include "pan_resource.h"
37 #include "pan_texture.h"
38
39 #include "pipe/p_context.h"
40 #include "pipe/p_defines.h"
41 #include "pipe/p_screen.h"
42 #include "pipe/p_state.h"
43 #include "util/compiler.h"
44 #include "util/detect.h"
45 #include "util/format/u_formats.h"
46 #include "util/hash_table.h"
47 #include "util/simple_mtx.h"
48 #include "util/u_blitter.h"
49
50 #include "compiler/shader_enums.h"
51 #include "midgard/midgard_compile.h"
52
53 #define SET_BIT(lval, bit, cond) \
54 if (cond) \
55 lval |= (bit); \
56 else \
57 lval &= ~(bit);
58
59 /* Dirty tracking flags. 3D is for general 3D state. Shader flags are
60 * per-stage. Renderer refers to Renderer State Descriptors. Vertex refers to
61 * vertex attributes/elements. */
62
63 enum pan_dirty_3d {
64 PAN_DIRTY_VIEWPORT = BITFIELD_BIT(0),
65 PAN_DIRTY_SCISSOR = BITFIELD_BIT(1),
66 PAN_DIRTY_VERTEX = BITFIELD_BIT(2),
67 PAN_DIRTY_PARAMS = BITFIELD_BIT(3),
68 PAN_DIRTY_DRAWID = BITFIELD_BIT(4),
69 PAN_DIRTY_TLS_SIZE = BITFIELD_BIT(5),
70 PAN_DIRTY_ZS = BITFIELD_BIT(6),
71 PAN_DIRTY_BLEND = BITFIELD_BIT(7),
72 PAN_DIRTY_MSAA = BITFIELD_BIT(8),
73 PAN_DIRTY_OQ = BITFIELD_BIT(9),
74 PAN_DIRTY_RASTERIZER = BITFIELD_BIT(10),
75 PAN_DIRTY_POINTS = BITFIELD_BIT(11),
76 PAN_DIRTY_SO = BITFIELD_BIT(12),
77 };
78
79 enum pan_dirty_shader {
80 PAN_DIRTY_STAGE_SHADER = BITFIELD_BIT(0),
81 PAN_DIRTY_STAGE_TEXTURE = BITFIELD_BIT(1),
82 PAN_DIRTY_STAGE_SAMPLER = BITFIELD_BIT(2),
83 PAN_DIRTY_STAGE_IMAGE = BITFIELD_BIT(3),
84 PAN_DIRTY_STAGE_CONST = BITFIELD_BIT(4),
85 PAN_DIRTY_STAGE_SSBO = BITFIELD_BIT(5),
86 };
87
88 struct panfrost_constant_buffer {
89 struct pipe_constant_buffer cb[PIPE_MAX_CONSTANT_BUFFERS];
90 uint32_t enabled_mask;
91 };
92
93 struct panfrost_query {
94 /* Passthrough from Gallium */
95 unsigned type;
96 unsigned index;
97
98 /* For computed queries. 64-bit to prevent overflow */
99 struct {
100 uint64_t start;
101 uint64_t end;
102 };
103
104 /* Memory for the GPU to writeback the value of the query */
105 struct pipe_resource *rsrc;
106
107 /* Whether an occlusion query is for a MSAA framebuffer */
108 bool msaa;
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 uint64_t draw_calls;
164 struct panfrost_query *occlusion_query;
165
166 unsigned drawid;
167 unsigned vertex_count;
168 unsigned instance_count;
169 unsigned offset_start;
170 unsigned base_vertex;
171 unsigned base_instance;
172 enum mesa_prim active_prim;
173
174 /* If instancing is enabled, vertex count padded for instance; if
175 * it is disabled, just equal to plain vertex count */
176 unsigned padded_count;
177
178 struct panfrost_constant_buffer constant_buffer[PIPE_SHADER_TYPES];
179 struct panfrost_rasterizer *rasterizer;
180 struct panfrost_vertex_state *vertex;
181
182 struct panfrost_uncompiled_shader *uncompiled[PIPE_SHADER_TYPES];
183 struct panfrost_compiled_shader *prog[PIPE_SHADER_TYPES];
184
185 struct pipe_vertex_buffer vertex_buffers[PIPE_MAX_ATTRIBS];
186 uint32_t vb_mask;
187
188 struct pipe_shader_buffer ssbo[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_BUFFERS];
189 uint32_t ssbo_mask[PIPE_SHADER_TYPES];
190
191 struct pipe_image_view images[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_IMAGES];
192 uint32_t image_mask[PIPE_SHADER_TYPES];
193
194 struct panfrost_sampler_state *samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
195 unsigned sampler_count[PIPE_SHADER_TYPES];
196 uint32_t valid_samplers[PIPE_SHADER_TYPES];
197
198 struct panfrost_sampler_view
199 *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_SAMPLER_VIEWS];
200 unsigned sampler_view_count[PIPE_SHADER_TYPES];
201
202 struct blitter_context *blitter;
203
204 struct pan_afbc_shaders afbc_shaders;
205
206 struct panfrost_blend_state *blend;
207
208 /* On Valhall, does the current blend state use a blend shader for any
209 * output? We need this information in a hot path to decide if
210 * per-sample shading should be enabled.
211 */
212 bool valhall_has_blend_shader;
213
214 struct pipe_viewport_state pipe_viewport;
215 struct pipe_scissor_state scissor;
216 struct pipe_blend_color blend_color;
217 struct panfrost_zsa_state *depth_stencil;
218 struct pipe_stencil_ref stencil_ref;
219 uint16_t sample_mask;
220 unsigned min_samples;
221
222 struct panfrost_query *cond_query;
223 bool cond_cond;
224 enum pipe_render_cond_flag cond_mode;
225
226 bool is_noop;
227
228 /* Mask of active render targets */
229 uint8_t fb_rt_mask;
230
231 int in_sync_fd;
232 uint32_t in_sync_obj;
233 };
234
235 /* Corresponds to the CSO */
236
237 struct panfrost_rasterizer;
238
239 /* Linked varyings */
240 struct pan_linkage {
241 /* If the upload is owned by the CSO instead
242 * of the pool, the referenced BO. Else,
243 * NULL. */
244 struct panfrost_bo *bo;
245
246 /* Uploaded attribute descriptors */
247 mali_ptr producer, consumer;
248
249 /* Varyings buffers required */
250 uint32_t present;
251
252 /* Per-vertex stride for general varying buffer */
253 uint32_t stride;
254 };
255
256 /* System value infrastructure */
257 #define MAX_SYSVAL_COUNT 32
258
259 /* Allow 2D of sysval IDs, while allowing nonparametric sysvals to equal
260 * their class for equal comparison */
261
262 #define PAN_SYSVAL(type, no) (((no) << 16) | PAN_SYSVAL_##type)
263 #define PAN_SYSVAL_TYPE(sysval) ((sysval)&0xffff)
264 #define PAN_SYSVAL_ID(sysval) ((sysval) >> 16)
265
266 /* Define some common types. We start at one for easy indexing of hash
267 * tables internal to the compiler */
268
269 enum {
270 PAN_SYSVAL_VIEWPORT_SCALE = 1,
271 PAN_SYSVAL_VIEWPORT_OFFSET = 2,
272 PAN_SYSVAL_TEXTURE_SIZE = 3,
273 PAN_SYSVAL_SSBO = 4,
274 PAN_SYSVAL_NUM_WORK_GROUPS = 5,
275 PAN_SYSVAL_SAMPLER = 7,
276 PAN_SYSVAL_LOCAL_GROUP_SIZE = 8,
277 PAN_SYSVAL_WORK_DIM = 9,
278 PAN_SYSVAL_IMAGE_SIZE = 10,
279 PAN_SYSVAL_SAMPLE_POSITIONS = 11,
280 PAN_SYSVAL_MULTISAMPLED = 12,
281 PAN_SYSVAL_RT_CONVERSION = 13,
282 PAN_SYSVAL_VERTEX_INSTANCE_OFFSETS = 14,
283 PAN_SYSVAL_DRAWID = 15,
284 PAN_SYSVAL_BLEND_CONSTANTS = 16,
285 PAN_SYSVAL_XFB = 17,
286 PAN_SYSVAL_NUM_VERTICES = 18,
287 };
288
289 #define PAN_TXS_SYSVAL_ID(texidx, dim, is_array) \
290 ((texidx) | ((dim) << 7) | ((is_array) ? (1 << 9) : 0))
291
292 #define PAN_SYSVAL_ID_TO_TXS_TEX_IDX(id) ((id)&0x7f)
293 #define PAN_SYSVAL_ID_TO_TXS_DIM(id) (((id) >> 7) & 0x3)
294 #define PAN_SYSVAL_ID_TO_TXS_IS_ARRAY(id) !!((id) & (1 << 9))
295
296 struct panfrost_sysvals {
297 /* The mapping of sysvals to uniforms, the count, and the off-by-one inverse */
298 unsigned sysvals[MAX_SYSVAL_COUNT];
299 unsigned sysval_count;
300 };
301
302 #define RSD_WORDS 16
303
304 /* Variants bundle together to form the backing CSO, bundling multiple
305 * shaders with varying emulated features baked in
306 */
307 struct panfrost_fs_key {
308 /* Number of colour buffers if gl_FragColor is written */
309 unsigned nr_cbufs_for_fragcolor;
310
311 /* On Valhall, fixed_varying_mask of the linked vertex shader */
312 uint32_t fixed_varying_mask;
313
314 /* Midgard shaders that read the tilebuffer must be keyed for
315 * non-blendable formats
316 */
317 enum pipe_format rt_formats[8];
318
319 /* From rasterize state, to lower point sprites */
320 uint16_t sprite_coord_enable;
321
322 /* User clip plane lowering */
323 uint8_t clip_plane_enable;
324
325 bool line_smooth;
326 };
327
328 struct panfrost_shader_key {
329 union {
330 /* Vertex shaders do not use shader keys. However, we have a
331 * special "transform feedback" vertex program derived from a
332 * vertex shader. If vs_is_xfb is set on a vertex shader, this
333 * is a transform feedback shader, else it is a regular
334 * (unkeyed) vertex shader.
335 */
336 bool vs_is_xfb;
337
338 /* Fragment shaders use regular shader keys */
339 struct panfrost_fs_key fs;
340 };
341 };
342
343 struct panfrost_compiled_shader {
344 /* Respectively, shader binary and Renderer State Descriptor */
345 struct panfrost_pool_ref bin, state;
346
347 /* For fragment shaders, a prepared (but not uploaded RSD) */
348 uint32_t partial_rsd[RSD_WORDS];
349
350 struct pan_shader_info info;
351 struct panfrost_sysvals sysvals;
352
353 struct pan_earlyzs_lut earlyzs;
354
355 /* Linked varyings, for non-separable programs */
356 struct pan_linkage linkage;
357
358 struct pipe_stream_output_info stream_output;
359
360 struct panfrost_shader_key key;
361
362 /* Mask of state that dirties the sysvals */
363 unsigned dirty_3d, dirty_shader;
364 };
365
366 /* Shader CSO */
367 struct panfrost_uncompiled_shader {
368 /* NIR for the shader. For graphics, this will be non-NULL even for
369 * TGSI. For compute, this will be NULL after the shader is compiled,
370 * as we don't need any compute variants.
371 */
372 const nir_shader *nir;
373
374 /* A SHA1 of the serialized NIR for the disk cache. */
375 unsigned char nir_sha1[20];
376
377 /* Stream output information */
378 struct pipe_stream_output_info stream_output;
379
380 /** Lock for the variants array */
381 simple_mtx_t lock;
382
383 /* Array of panfrost_compiled_shader */
384 struct util_dynarray variants;
385
386 /* Compiled transform feedback program, if one is required */
387 struct panfrost_compiled_shader *xfb;
388
389 /* On vertex shaders, bit mask of special desktop-only varyings to link
390 * with the fragment shader. Used on Valhall to implement separable
391 * shaders for desktop GL.
392 */
393 uint32_t fixed_varying_mask;
394
395 /* If gl_FragColor was lowered, we need to optimize the stores later */
396 bool fragcolor_lowered;
397 };
398
399 /* The binary artefacts of compiling a shader. This differs from
400 * panfrost_compiled_shader, which adds extra metadata beyond compiling but
401 * throws away information not needed after the initial compile.
402 *
403 * This structure is serialized for the shader disk cache.
404 */
405 struct panfrost_shader_binary {
406 /* Collected information about the compiled shader */
407 struct pan_shader_info info;
408 struct panfrost_sysvals sysvals;
409
410 /* The binary itself */
411 struct util_dynarray binary;
412 };
413
414 void
415 panfrost_disk_cache_store(struct disk_cache *cache,
416 const struct panfrost_uncompiled_shader *uncompiled,
417 const struct panfrost_shader_key *key,
418 const struct panfrost_shader_binary *binary);
419
420 bool panfrost_disk_cache_retrieve(
421 struct disk_cache *cache,
422 const struct panfrost_uncompiled_shader *uncompiled,
423 const struct panfrost_shader_key *key,
424 struct panfrost_shader_binary *binary);
425
426 void panfrost_disk_cache_init(struct panfrost_screen *screen);
427
428 bool panfrost_nir_remove_fragcolor_stores(nir_shader *s, unsigned nr_cbufs);
429
430 bool panfrost_nir_lower_sysvals(nir_shader *s,
431 struct panfrost_sysvals *sysvals);
432
433 /** (Vertex buffer index, divisor) tuple that will become an Attribute Buffer
434 * Descriptor at draw-time on Midgard
435 */
436 struct pan_vertex_buffer {
437 unsigned vbi;
438 unsigned divisor;
439 };
440
441 unsigned pan_assign_vertex_buffer(struct pan_vertex_buffer *buffers,
442 unsigned *nr_bufs, unsigned vbi,
443 unsigned divisor);
444
445 struct panfrost_zsa_state;
446 struct panfrost_sampler_state;
447 struct panfrost_sampler_view;
448 struct panfrost_vertex_state;
449
450 static inline struct panfrost_context *
pan_context(struct pipe_context * pcontext)451 pan_context(struct pipe_context *pcontext)
452 {
453 return (struct panfrost_context *)pcontext;
454 }
455
456 static inline struct panfrost_streamout_target *
pan_so_target(struct pipe_stream_output_target * target)457 pan_so_target(struct pipe_stream_output_target *target)
458 {
459 return (struct panfrost_streamout_target *)target;
460 }
461
462 struct pipe_context *panfrost_create_context(struct pipe_screen *screen,
463 void *priv, unsigned flags);
464
465 bool panfrost_writes_point_size(struct panfrost_context *ctx);
466
467 struct panfrost_ptr panfrost_vertex_tiler_job(struct panfrost_context *ctx,
468 bool is_tiler);
469
470 void panfrost_flush(struct pipe_context *pipe, struct pipe_fence_handle **fence,
471 unsigned flags);
472
473 bool panfrost_render_condition_check(struct panfrost_context *ctx);
474
475 void panfrost_update_shader_variant(struct panfrost_context *ctx,
476 enum pipe_shader_type type);
477
478 void panfrost_analyze_sysvals(struct panfrost_compiled_shader *ss);
479
480 mali_ptr
481 panfrost_get_index_buffer(struct panfrost_batch *batch,
482 const struct pipe_draw_info *info,
483 const struct pipe_draw_start_count_bias *draw);
484
485 mali_ptr
486 panfrost_get_index_buffer_bounded(struct panfrost_batch *batch,
487 const struct pipe_draw_info *info,
488 const struct pipe_draw_start_count_bias *draw,
489 unsigned *min_index, unsigned *max_index);
490
491 /* Instancing */
492
493 mali_ptr panfrost_vertex_buffer_address(struct panfrost_context *ctx,
494 unsigned i);
495
496 void panfrost_shader_context_init(struct pipe_context *pctx);
497
498 static inline void
panfrost_dirty_state_all(struct panfrost_context * ctx)499 panfrost_dirty_state_all(struct panfrost_context *ctx)
500 {
501 ctx->dirty = ~0;
502
503 for (unsigned i = 0; i < PIPE_SHADER_TYPES; ++i)
504 ctx->dirty_shader[i] = ~0;
505 }
506
507 static inline void
panfrost_clean_state_3d(struct panfrost_context * ctx)508 panfrost_clean_state_3d(struct panfrost_context *ctx)
509 {
510 ctx->dirty = 0;
511
512 for (unsigned i = 0; i < PIPE_SHADER_TYPES; ++i) {
513 if (i != PIPE_SHADER_COMPUTE)
514 ctx->dirty_shader[i] = 0;
515 }
516 }
517
518 void panfrost_set_batch_masks_blend(struct panfrost_batch *batch);
519
520 void panfrost_set_batch_masks_zs(struct panfrost_batch *batch);
521
522 void panfrost_track_image_access(struct panfrost_batch *batch,
523 enum pipe_shader_type stage,
524 struct pipe_image_view *image);
525
526 #endif
527