Debian Codegraph @

src/panfrost/compiler/pan_compiler.h

raw
1 /*
2 * Copyright (C) 2025 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #ifndef __PAN_COMPILER_H__
7 #define __PAN_COMPILER_H__
8
9 #include <stdbool.h>
10 #include <stdio.h>
11
12 #include "compiler/nir/nir_defines.h"
13 #include "compiler/shader_enums.h"
14 #include "util/u_dynarray.h"
15 #include "util/format/u_formats.h"
16 #include "util/shader_stats.h"
17
18 struct pan_shader_info;
19
20 bool pan_will_dump_shaders(unsigned arch);
21 bool pan_want_debug_info(unsigned arch);
22
23 const nir_shader_compiler_options *
24 pan_get_nir_shader_compiler_options(unsigned arch, bool merge_wg);
25
26 /* Inputs to the backend compiler */
27 struct pan_compile_inputs {
28 uint64_t gpu_id;
29 uint32_t gpu_variant;
30 bool is_blend, is_blit;
31 bool no_idvs;
32 uint32_t view_mask;
33
34 nir_variable_mode robust_modes;
35 /* Whether or not descriptor accesses should add additional robustness
36 * checks. */
37 bool robust_descriptors;
38
39 /* Mask of UBOs that may be moved to push constants */
40 uint32_t pushable_ubos;
41
42 /* Varying layout in memory, if known */
43 const struct pan_varying_layout *varying_layout;
44
45 /* Optimizations as nir_opt_varyings can erase all flat types to float, when
46 * this field is false, varying types are inferred from their usage.
47 */
48 bool trust_varying_flat_highp_types;
49
50 /* Settings to move constants into the FAU. */
51 struct {
52 uint32_t *values;
53 /* In multiples of 32bit. */
54 uint32_t max_amount;
55 /* In multiples of 32bit. */
56 uint32_t offset;
57 } fau_consts;
58 };
59
60 /* Every panfrost compilation pipeline should adhere to:
61 * 1. Driver-specific early lowering + pan_optimize_nir() (optional)
62 * 2. pan_preprocess_nir()
63 * 3. Descriptor lowering
64 * 4. pan_postprocess_nir()
65 * 5. Inline sysvals
66 * 5. pan_shader_compile()
67 * ONLY SYSVAL LOWERING is allowed between postprocess and shader_compile.
68 * Driver-specific lowerings should be either BEFORE preprocess or BETWEEN
69 * preprocess and postprocess. Any code except sysval inlining put after
70 * postprocess WILL BE NAKed.
71 */
72 void pan_preprocess_nir(nir_shader *nir, uint64_t gpu_id);
73 void pan_optimize_nir(nir_shader *nir, uint64_t gpu_id);
74 void pan_postprocess_nir(nir_shader *nir, uint64_t gpu_id);
75
76 void pan_shader_compile(nir_shader *nir, struct pan_compile_inputs *inputs,
77 struct util_dynarray *binary,
78 struct pan_shader_info *info);
79
80 #define PAN_PRINTF_BUFFER_SIZE 16384
81
82 /* Any address with the top bit set is treated OOB by the hardware when
83 * accessed from a shader and any reads will return zero and writes will be
84 * discarded. Using these is sometimes preferable to control-flow in the
85 * shader.
86 */
87 #define PAN_SHADER_OOB_ADDRESS (((uint64_t)1) << 63)
88
89 /* Indices for named (non-XFB) varyings that are present. These are packed
90 * tightly so they correspond to a bitfield present (P) indexed by (1 <<
91 * PAN_VARY_*). This has the nice property that you can lookup the buffer index
92 * of a given special field given a shift S by:
93 *
94 * idx = popcount(P & ((1 << S) - 1))
95 *
96 * That is... look at all of the varyings that come earlier and count them, the
97 * count is the new index since plus one. Likewise, the total number of special
98 * buffers required is simply popcount(P)
99 */
100
101 enum pan_special_varying {
102 PAN_VARY_GENERAL = 0,
103 PAN_VARY_POSITION = 1,
104 PAN_VARY_PSIZ = 2,
105 PAN_VARY_PNTCOORD = 3,
106 PAN_VARY_FACE = 4,
107 PAN_VARY_FRAGCOORD = 5,
108
109 /* Keep last */
110 PAN_VARY_MAX,
111 };
112
113 /* Maximum number of attribute descriptors required for varyings. These include
114 * up to MAX_VARYING source level varyings plus a descriptor each non-GENERAL
115 * special varying */
116 #define PAN_MAX_VARYINGS (MAX_VARYING + PAN_VARY_MAX - 1)
117
118 /* Special attribute slots for vertex builtins. Sort of arbitrary but let's be
119 * consistent with the blob so we can compare traces easier. */
120
121 enum { PAN_VERTEX_ID = 16, PAN_INSTANCE_ID = 17, PAN_MAX_ATTRIBUTE };
122
123 /* Architecturally, Bifrost/Valhall can address 128 FAU slots of 64-bits each.
124 * In practice, the maximum number of FAU slots is limited by implementation.
125 * All known Bifrost and Valhall devices limit to 64 FAU slots. Therefore the
126 * maximum number of 32-bit words is 128, since there are 2 words per FAU slot.
127 *
128 * Midgard can push at most 92 words, so this bound suffices. The Midgard
129 * compiler pushes less than this, as Midgard uses register-mapped uniforms
130 * instead of FAU, preventing large numbers of uniforms to be pushed for
131 * nontrivial programs.
132 */
133 #define PAN_MAX_PUSH 128
134
135 #define PAN_MAX_MULTIVIEW_VIEW_COUNT 8
136
137 /* Architectural invariants (Midgard and Bifrost): UBO must be <= 2^16 bytes so
138 * an offset to a word must be < 2^16. There are less than 2^8 UBOs */
139
140 struct pan_ubo_word {
141 uint16_t ubo;
142 uint16_t offset;
143 };
144
145 struct pan_ubo_push {
146 unsigned count;
147 struct pan_ubo_word words[PAN_MAX_PUSH];
148 };
149
150 /* Helper for searching the above. Note this is O(N) to the number of pushed
151 * constants, do not run in the draw call hot path */
152
153 unsigned pan_lookup_pushed_ubo(struct pan_ubo_push *push, unsigned ubo,
154 unsigned offs);
155
156 enum pan_varying_section {
157 PAN_VARYING_SECTION_POSITION,
158 PAN_VARYING_SECTION_ATTRIBS,
159 /* Varyings computed on-the-fly */
160 PAN_VARYING_SECTION_SPECIAL,
161 PAN_VARYING_SECTION_GENERIC,
162 };
163
164 /* Varyings which go in PAN_VARYING_SECTION_ATTRIBS */
165 #define PAN_ATTRIB_VARYING_BITS \
166 (VARYING_BIT_PSIZ | VARYING_BIT_LAYER | VARYING_BIT_VIEWPORT | \
167 VARYING_BIT_PRIMITIVE_ID)
168
169 /* Varyings which go in PAN_VARYING_SECTION_SPECIAL (Midgard only) */
170 #define PAN_SPECIAL_VARYING_BITS \
171 (VARYING_BIT_PNTC | VARYING_BIT_POS | VARYING_BIT_FACE)
172
173 /* Varyings which DO NOT go in PAN_VARYING_SECTION_GENERIC */
174 #define PAN_HARDWARE_VARYING_BITS \
175 (VARYING_BIT_POS | PAN_ATTRIB_VARYING_BITS | PAN_SPECIAL_VARYING_BITS)
176
177 struct pan_varying_slot {
178 /* GLSL/SPIR-V location of the varying slot */
179 gl_varying_slot location : 7;
180
181 /* Format of the varying slot in memory
182 * (really nir_alu_type, but the compiler screams at you if you don't lie) */
183 unsigned alu_type : 8;
184 unsigned ncomps : 3;
185
186 enum pan_varying_section section : 2;
187
188 /* Offset of the varying slot in the specified section of the varying
189 * buffer. For special VS outputs (see PAN_ATTRIB_VARYING_BITS), this is
190 * relative to the start of the position header. For all other varyings,
191 * this is relative to the start of the varying space. The offset will be
192 * -1 if unknown (before the memory layout is built).
193 */
194 int offset : 12;
195 };
196 static_assert(sizeof(struct pan_varying_slot) == 4,
197 "This struct has no holes");
198
199 static inline bool
200 pan_varying_slot_is_empty(const struct pan_varying_slot *slot)
201 {
202 return slot->alu_type == nir_type_invalid;
203 }
204
205 enum ENUM_PACKED pan_varying_knowledge {
206 PAN_VARYING_FORMAT_KNOWN = BITFIELD_BIT(0),
207 PAN_VARYING_LAYOUT_KNOWN = BITFIELD_BIT(1),
208 };
209
210 /* Contains information about varyings, both their format and the physical
211 * memory layout. The format is not necessarily what is actually stored in
212 * memory, but what format is in the register before the store_output, or what
213 * the shader expects after a load_input. The layout is optional and specifies
214 * the exact offset in memory of each varying, its section and the size of the
215 * generic buffer. The layout is only built for the Vertex Shader and passed
216 * on to the Fragment Shader if they are linked together, since the struct is
217 * valid even without format or layout information, the "known" field tracks
218 * what information the structure has, before accessing any format information
219 * you should check with `pan_varying_layout_require_format` that it is built
220 * and before accessing any layout information you should check with
221 * pan_varying_layout_require_layout if it is present.
222 *
223 * The format and layout are not split into two different structures to avoid
224 * duplicating indexing information.
225 *
226 * The slots are valid only up to `count`, but can also contain holes if they
227 * have been dead-code-eliminated after `nir_assign_io_var_locations`. Please
228 * use `pan_varying_slot_is_empty` to check if slots are empty. Empty slots are
229 * ignored by finding functions.
230 */
231 PRAGMA_DIAGNOSTIC_PUSH
232 PRAGMA_DIAGNOSTIC_ERROR(-Wpadded)
233 struct pan_varying_layout {
234 uint8_t count;
235 enum pan_varying_knowledge known;
236 /* Size of the generic section, in bytes */
237 uint16_t generic_size_B;
238
239 struct pan_varying_slot slots[PAN_MAX_VARYINGS];
240 };
241 PRAGMA_DIAGNOSTIC_POP
242
243 static inline const struct pan_varying_slot *
244 pan_varying_layout_find_slot(const struct pan_varying_layout *layout,
245 gl_varying_slot location)
246 {
247 for (unsigned i = 0; i < layout->count; i++) {
248 if (layout->slots[i].location != location)
249 continue;
250 const struct pan_varying_slot *slot = &layout->slots[i];
251 if (pan_varying_slot_is_empty(slot))
252 break;
253 return slot;
254 }
255
256 return NULL;
257 }
258
259 static inline const struct pan_varying_slot *
260 pan_varying_layout_slot_at(const struct pan_varying_layout *layout,
261 unsigned index)
262 {
263 if (index >= layout->count)
264 return NULL;
265
266 const struct pan_varying_slot *slot = &layout->slots[index];
267 if (pan_varying_slot_is_empty(slot))
268 return NULL;
269
270 return slot;
271 }
272
273 static inline void
274 pan_varying_layout_require_format(const struct pan_varying_layout *layout)
275 {
276 assert(layout);
277 if (!(layout->known & PAN_VARYING_FORMAT_KNOWN))
278 assert(!"Format is required");
279 }
280
281 static inline void
282 pan_varying_layout_require_layout(const struct pan_varying_layout *layout)
283 {
284 assert(layout);
285 if (!(layout->known & PAN_VARYING_LAYOUT_KNOWN))
286 assert(!"Layout is required");
287 }
288
289 enum pipe_format
290 pan_varying_format(nir_alu_type type, unsigned ncomps);
291
292 void pan_build_varying_layout_compact(struct pan_varying_layout *layout,
293 nir_shader *nir, uint64_t gpu_id);
294
295 void pan_varying_collect_formats(struct pan_varying_layout *registry,
296 nir_shader *nir, uint64_t gpu_id,
297 bool trust_varying_flat_highp_types,
298 bool lower_mediump);
299
300 struct pan_shader_varying {
301 gl_varying_slot location;
302 enum pipe_format format;
303 };
304
305 struct bifrost_shader_blend_info {
306 nir_alu_type type;
307 uint32_t return_offset;
308 };
309
310 /*
311 * Unpacked form of a v7 message preload descriptor, produced by the compiler's
312 * message preload optimization. By splitting out this struct, the compiler does
313 * not need to know about data structure packing, avoiding a dependency on
314 * GenXML.
315 */
316 struct bifrost_message_preload {
317 /* Whether to preload this message */
318 bool enabled;
319
320 /* Varying to load from */
321 unsigned varying_index;
322
323 /* Register type, FP32 otherwise */
324 bool fp16;
325
326 /* Number of components, ignored if texturing */
327 unsigned num_components;
328
329 /* If texture is set, performs a texture instruction according to
330 * texture_index, skip, and zero_lod. If texture is unset, only the
331 * varying load is performed.
332 */
333 bool texture, skip, zero_lod;
334 unsigned texture_index;
335 };
336
337 struct bifrost_shader_info {
338 struct bifrost_shader_blend_info blend[8];
339 nir_alu_type blend_src1_type;
340 bool wait_6, wait_7;
341 struct bifrost_message_preload messages[2];
342
343 /* Whether any flat varyings are loaded. This may disable optimizations
344 * that change the provoking vertex, since that would load incorrect
345 * values for flat varyings.
346 */
347 bool uses_flat_shading;
348
349 /* Whether there are any LD_VAR[_IMM] instructions that require varying
350 * descriptors. LD_VAR_BUF[_IMM] do not count as they only need buffer
351 * descriptors to work correctly. For more details check out
352 * docs/drivers/panfrost/varyings.rst
353 */
354 bool uses_ld_var;
355 };
356
357 struct midgard_shader_info {
358 unsigned first_tag;
359 union {
360 struct {
361 bool reads_raw_vertex_id;
362 } vs;
363 };
364 };
365
366 struct pan_shader_info {
367 mesa_shader_stage stage;
368 unsigned work_reg_count;
369 unsigned tls_size;
370 unsigned wls_size;
371
372 struct pan_stats stats, stats_idvs_varying;
373
374 /* Bit mask of preloaded registers */
375 uint64_t preload;
376
377 uint32_t fau_consts_count;
378 uint32_t fau_consts[128];
379
380 union {
381 struct {
382 bool reads_frag_coord;
383 bool reads_point_coord;
384 bool reads_primitive_id;
385 bool reads_face;
386 bool can_discard;
387 bool writes_depth;
388 bool writes_stencil;
389 bool writes_coverage;
390 bool sidefx;
391 bool sample_shading;
392 bool early_fragment_tests;
393 bool can_early_z, can_fpk;
394 bool untyped_color_outputs;
395 struct {
396 bool ld_tile;
397 bool wait_or_tile_access_before_atest_zsemit;
398 bool rasterizer_coverage_read;
399 bool centroid_interpolation;
400 bool varying_before_atest_zsemit;
401 } hsr;
402 uint32_t outputs_read;
403 } fs;
404
405 struct {
406 bool writes_point_size;
407
408 /* True if this shader needs the extended FIFO format for
409 * more than just point size.
410 */
411 bool needs_extended_fifo;
412
413 /* If the primary shader writes point size, the Valhall
414 * driver may need a variant that does not write point
415 * size. Offset to such a shader in the program binary.
416 *
417 * Zero if no such variant is required.
418 *
419 * Only used with IDVS on Valhall.
420 */
421 unsigned no_psiz_offset;
422
423 /* Set if Index-Driven Vertex Shading is in use */
424 bool idvs;
425
426 /* If IDVS is used, whether a varying shader is used */
427 bool secondary_enable;
428
429 /* If a varying shader is used, the varying shader's
430 * offset in the program binary
431 */
432 unsigned secondary_offset;
433
434 /* If IDVS is in use, number of work registers used by
435 * the varying shader
436 */
437 unsigned secondary_work_reg_count;
438
439 /* If IDVS is in use, bit mask of preloaded registers
440 * used by the varying shader
441 */
442 uint64_t secondary_preload;
443 } vs;
444
445 struct {
446 /* Is it legal to merge workgroups? This is true if the
447 * shader uses neither barriers nor shared memory. This
448 * requires caution: if the API allows specifying shared
449 * memory at launch time (instead of compile time), that
450 * memory will not be accounted for by the compiler.
451 *
452 * Used by the Valhall hardware.
453 */
454 bool allow_merging_workgroups;
455 } cs;
456 };
457
458 /* Does the shader contains a barrier? or (for fragment shaders) does it
459 * require helper invocations, which demand the same ordering guarantees
460 * of the hardware? These notions are unified in the hardware, so we
461 * unify them here as well.
462 */
463 bool contains_barrier;
464 bool separable;
465 bool writes_global;
466 uint64_t outputs_written;
467
468 /* Floating point controls that the driver should try to honour */
469 bool ftz_fp16, ftz_fp32;
470
471 /* True if the shader contains a shader_clock instruction. */
472 bool has_shader_clk_instr;
473
474 unsigned sampler_count;
475 unsigned texture_count;
476 unsigned ubo_count;
477 unsigned attributes_read_count;
478 unsigned attribute_count;
479 unsigned attributes_read;
480 uint64_t images_used;
481
482 struct {
483 /* Bitfield of noperspective varyings, starting at VARYING_SLOT_VAR0 */
484 uint32_t noperspective;
485
486 struct pan_varying_layout formats;
487 } varyings;
488
489 /* UBOs to push to Register Mapped Uniforms (Midgard) or Fast Access
490 * Uniforms (Bifrost) */
491 struct pan_ubo_push push;
492
493 uint32_t ubo_mask;
494
495 union {
496 struct bifrost_shader_info bifrost;
497 struct midgard_shader_info midgard;
498 };
499 };
500
501 void pan_shader_update_info(struct pan_shader_info *info, nir_shader *s,
502 const struct pan_compile_inputs *inputs);
503
504 uint16_t pan_to_bytemask(unsigned bytes, unsigned mask);
505
506 /* NIR passes to do some backend-specific lowering */
507
508 /*
509 * Helper returning the subgroup size. Generally, this is equal to the number of
510 * threads in a warp. For Midgard (including warping models), this returns 1, as
511 * subgroups are not supported.
512 */
513 static inline unsigned
514 pan_subgroup_size(unsigned arch)
515 {
516 if (arch >= 9)
517 return 16;
518 else if (arch >= 7)
519 return 8;
520 else if (arch >= 6)
521 return 4;
522 else
523 return 1;
524 }
525
526 /*
527 * Helper returning the maximum offset in bytes (exclusive) that a LD_VAR_BUF*
528 * instruction can use.
529 */
530 static inline unsigned
531 pan_ld_var_buf_off_size(unsigned arch)
532 {
533 if (arch >= 11)
534 return 2048;
535 else if (arch >= 9)
536 return 256;
537 else
538 return 0;
539 }
540
541 /*
542 * Helper extracting the table from a given handle of Valhall descriptor model.
543 */
544 static inline unsigned
545 pan_res_handle_get_table(unsigned handle)
546 {
547 unsigned table = handle >> 24;
548
549 assert(table < 64);
550 return table;
551 }
552
553 /*
554 * Helper returning the index from a given handle of Valhall descriptor model.
555 */
556 static inline unsigned
557 pan_res_handle_get_index(unsigned handle)
558 {
559 return handle & BITFIELD_MASK(24);
560 }
561
562 /*
563 * Helper creating an handle for Valhall descriptor model.
564 */
565 static inline unsigned
566 pan_res_handle(unsigned table, unsigned index)
567 {
568 assert(table < 64);
569 assert(index < (1u << 24));
570
571 return (table << 24) | index;
572 }
573
574 void pan_disassemble(FILE *fp, const void *code, size_t size, uint64_t gpu_id,
575 bool verbose);
576
577 #endif /* __PAN_COMPILER_H__ */