Debian Codegraph @

src/panfrost/vulkan/panvk_shader.h

raw
1 /*
2 * Copyright © 2021 Collabora Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #ifndef PANVK_SHADER_H
7 #define PANVK_SHADER_H
8
9 #ifndef PAN_ARCH
10 #error "PAN_ARCH must be defined"
11 #endif
12
13 #include "compiler/pan_compiler.h"
14
15 #include "pan_desc.h"
16 #include "pan_earlyzs.h"
17
18 #include "panvk_cmd_push_constant.h"
19 #include "panvk_descriptor_set.h"
20 #include "panvk_macros.h"
21 #include "panvk_mempool.h"
22
23 #include "vk_pipeline_layout.h"
24
25 #include "vk_shader.h"
26
27 extern const struct vk_device_shader_ops panvk_per_arch(device_shader_ops);
28
29 #define MAX_RTS 8
30 #define MAX_VS_ATTRIBS 16
31
32 #if PAN_ARCH < 9
33
34 /* We could theoretically use the MAX_PER_SET values here (except for UBOs
35 * where we're really limited to 256 on the shader side), but on Bifrost we
36 * have to copy some tables around, which comes at an extra memory/processing
37 * cost, so let's pick something smaller. */
38 #define MAX_PER_STAGE_SAMPLED_IMAGES 256
39 #define MAX_PER_STAGE_SAMPLERS 128
40 #define MAX_PER_STAGE_UNIFORM_BUFFERS MAX_PER_SET_UNIFORM_BUFFERS
41 #define MAX_PER_STAGE_STORAGE_BUFFERS 64
42 #define MAX_PER_STAGE_STORAGE_IMAGES 32
43 #define MAX_PER_STAGE_INPUT_ATTACHMENTS MAX_PER_SET_INPUT_ATTACHMENTS
44
45 #else
46
47 #define MAX_PER_STAGE_SAMPLED_IMAGES MAX_PER_SET_SAMPLED_IMAGES
48 #define MAX_PER_STAGE_SAMPLERS MAX_PER_SET_SAMPLERS
49 #define MAX_PER_STAGE_UNIFORM_BUFFERS MAX_PER_SET_UNIFORM_BUFFERS
50 #define MAX_PER_STAGE_STORAGE_BUFFERS MAX_PER_SET_STORAGE_BUFFERS
51 #define MAX_PER_STAGE_STORAGE_IMAGES MAX_PER_SET_STORAGE_IMAGES
52 #define MAX_PER_STAGE_INPUT_ATTACHMENTS MAX_PER_SET_INPUT_ATTACHMENTS
53
54 #endif
55
56 #define MAX_PER_STAGE_RESOURCES ( \
57 MAX_PER_STAGE_SAMPLED_IMAGES + MAX_PER_STAGE_SAMPLERS + \
58 MAX_PER_STAGE_UNIFORM_BUFFERS + MAX_PER_STAGE_STORAGE_BUFFERS + \
59 MAX_PER_STAGE_STORAGE_IMAGES + MAX_PER_STAGE_INPUT_ATTACHMENTS)
60
61 struct nir_shader;
62 struct pan_blend_state;
63 struct panvk_device;
64
65 enum panvk_varying_buf_id {
66 PANVK_VARY_BUF_GENERAL,
67 PANVK_VARY_BUF_POSITION,
68 PANVK_VARY_BUF_PSIZ,
69
70 /* Keep last */
71 PANVK_VARY_BUF_MAX,
72 };
73
74 #if PAN_ARCH < 9
75 enum panvk_desc_table_id {
76 PANVK_DESC_TABLE_USER = 0,
77 PANVK_DESC_TABLE_CS_DYN_SSBOS = MAX_SETS,
78 PANVK_DESC_TABLE_COMPUTE_COUNT = PANVK_DESC_TABLE_CS_DYN_SSBOS + 1,
79 PANVK_DESC_TABLE_VS_DYN_SSBOS = MAX_SETS,
80 PANVK_DESC_TABLE_FS_DYN_SSBOS = MAX_SETS + 1,
81 PANVK_DESC_TABLE_GFX_COUNT = PANVK_DESC_TABLE_FS_DYN_SSBOS + 1,
82 };
83 #endif
84
85 #define PANVK_COLOR_ATTACHMENT(x) (x)
86 #define PANVK_ZS_ATTACHMENT 255
87
88 struct panvk_input_attachment_info {
89 uint32_t target;
90 uint32_t conversion;
91 };
92
93 /* One attachment per color, one for depth, one for stencil, and the last one
94 * for the attachment without an InputAttachmentIndex attribute. */
95 #define INPUT_ATTACHMENT_MAP_SIZE 11
96
97 #define FAU_WORD_SIZE sizeof(uint64_t)
98
99 #define aligned_u64 __attribute__((aligned(sizeof(uint64_t)))) uint64_t
100
101 /* System values which are common to both graphics and compute. These are
102 * always at the same offset in both graphics and compute allowing us to
103 * compile the shader without knowing which queue it will be dispatched on.
104 */
105 struct panvk_common_sysvals_inner {
106 /* Address of sysval/push constant buffer used for indirect loads */
107 aligned_u64 push_uniforms;
108
109 /* Address of the printf buffer */
110 aligned_u64 printf_buffer_address;
111 } __attribute__((aligned(FAU_WORD_SIZE)));
112
113 struct panvk_common_sysvals {
114 uint32_t _pad[4];
115 struct panvk_common_sysvals_inner common;
116 } __attribute__((aligned(FAU_WORD_SIZE)));
117
118 static_assert((offsetof(struct panvk_common_sysvals, common) %
119 FAU_WORD_SIZE) == 0,
120 "struct panvk_graphics_sysvals_inner must be 8-byte aligned");
121 static_assert((sizeof(struct panvk_common_sysvals_inner) %
122 FAU_WORD_SIZE) == 0,
123 "struct panvk_graphics_sysvals_inner must be 8-byte aligned");
124
125 #define SYSVALS_COMMON_START \
126 (offsetof(struct panvk_common_sysvals, common) / FAU_WORD_SIZE)
127
128 #define SYSVALS_COMMON_COUNT \
129 (sizeof(struct panvk_common_sysvals_inner) / FAU_WORD_SIZE)
130
131 #define SYSVALS_COMMON_END (SYSVALS_COMMON_START + SYSVALS_COMMON_COUNT)
132
133 struct panvk_graphics_sysvals {
134 /* Blend constants MUST come first because their position cannot depend on
135 * the FAU packing of the fragment shader.
136 */
137 struct {
138 float constants[4];
139 } blend;
140
141 /* This must be at the same offset for both compute and graphics */
142 struct panvk_common_sysvals_inner common;
143
144 struct {
145 struct {
146 float x, y, z;
147 } scale, offset;
148 } viewport;
149
150 struct {
151 #if PAN_ARCH < 9
152 int32_t raw_vertex_offset;
153 #endif
154 int32_t first_vertex;
155 int32_t base_instance;
156 uint32_t noperspective_varyings;
157 } vs;
158
159 struct {
160 aligned_u64 blend_descs[MAX_RTS];
161 } fs;
162
163 struct panvk_input_attachment_info iam[INPUT_ATTACHMENT_MAP_SIZE];
164
165 #if PAN_ARCH < 9
166 /* gl_Layer on Bifrost is a bit of hack. We have to issue one draw per
167 * layer, and filter primitives at the VS level.
168 */
169 int32_t layer_id;
170
171 struct {
172 aligned_u64 sets[PANVK_DESC_TABLE_GFX_COUNT];
173 } desc;
174 #endif
175 } __attribute__((aligned(FAU_WORD_SIZE)));
176
177 static_assert(offsetof(struct panvk_graphics_sysvals, blend) == 0,
178 "panvk_graphics_sysvals::blend must be at the start");
179 static_assert(offsetof(struct panvk_graphics_sysvals, common) ==
180 offsetof(struct panvk_common_sysvals, common),
181 "Common sysvals must be at the same offset everywhere");
182 static_assert((sizeof(struct panvk_graphics_sysvals) % FAU_WORD_SIZE) == 0,
183 "struct panvk_graphics_sysvals must be 8-byte aligned");
184 #if PAN_ARCH < 9
185 static_assert((offsetof(struct panvk_graphics_sysvals, desc) % FAU_WORD_SIZE) ==
186 0,
187 "panvk_graphics_sysvals::desc must be 8-byte aligned");
188 #endif
189
190 struct panvk_compute_sysvals {
191 struct {
192 uint32_t x, y, z;
193 } base;
194
195 uint32_t _pad;
196
197 /* This must be at the same offset for both compute and graphics */
198 struct panvk_common_sysvals_inner common;
199
200 struct {
201 uint32_t x, y, z;
202 } num_work_groups;
203 struct {
204 uint32_t x, y, z;
205 } local_group_size;
206
207 #if PAN_ARCH < 9
208 struct {
209 aligned_u64 sets[PANVK_DESC_TABLE_COMPUTE_COUNT];
210 } desc;
211 #endif
212 } __attribute__((aligned(FAU_WORD_SIZE)));
213
214 static_assert(offsetof(struct panvk_compute_sysvals, common) ==
215 offsetof(struct panvk_common_sysvals, common),
216 "Common sysvals must be at the same offset everywhere");
217 static_assert((sizeof(struct panvk_compute_sysvals) % FAU_WORD_SIZE) == 0,
218 "struct panvk_compute_sysvals must be 8-byte aligned");
219 #if PAN_ARCH < 9
220 static_assert((offsetof(struct panvk_compute_sysvals, desc) % FAU_WORD_SIZE) ==
221 0,
222 "panvk_compute_sysvals::desc must be 8-byte aligned");
223 #endif
224
225 /* This is not the final offset in the push constant buffer (AKA FAU), but
226 * just a magic offset we use before packing push constants so we can easily
227 * identify the type of push constant (driver sysvals vs user push constants).
228 */
229 #define SYSVALS_PUSH_CONST_BASE MAX_PUSH_CONSTANTS_SIZE
230
231 #define common_sysval_size(__name) \
232 sizeof(((struct panvk_common_sysvals *)NULL)->common.__name)
233
234 #define graphics_sysval_size(__name) \
235 sizeof(((struct panvk_graphics_sysvals *)NULL)->__name)
236
237 #define compute_sysval_size(__name) \
238 sizeof(((struct panvk_compute_sysvals *)NULL)->__name)
239
240 #define sysval_size(__ptype, __name) __ptype##_sysval_size(__name)
241
242 #define common_sysval_offset(__name) \
243 offsetof(struct panvk_common_sysvals, common.__name)
244
245 #define graphics_sysval_offset(__name) \
246 offsetof(struct panvk_graphics_sysvals, __name)
247
248 #define compute_sysval_offset(__name) \
249 offsetof(struct panvk_compute_sysvals, __name)
250
251 #define sysval_offset(__ptype, __name) __ptype##_sysval_offset(__name)
252
253 #define sysval_entry_size(__ptype, __name) \
254 sizeof(((struct panvk_##__ptype##_sysvals *)NULL)->__name[0])
255
256 #define sysval_entry_offset(__ptype, __name, __idx) \
257 (sysval_offset(__ptype, __name) + \
258 (sysval_entry_size(__ptype, __name) * __idx))
259
260 #define sysval_fau_start(__ptype, __name) \
261 (sysval_offset(__ptype, __name) / FAU_WORD_SIZE)
262
263 #define sysval_fau_end(__ptype, __name) \
264 ((sysval_offset(__ptype, __name) + sysval_size(__ptype, __name) - 1) / \
265 FAU_WORD_SIZE)
266
267 #define sysval_fau_entry_start(__ptype, __name, __idx) \
268 (sysval_entry_offset(__ptype, __name, __idx) / FAU_WORD_SIZE)
269
270 #define sysval_fau_entry_end(__ptype, __name, __idx) \
271 ((sysval_entry_offset(__ptype, __name, __idx + 1) - 1) / FAU_WORD_SIZE)
272
273 #define shader_remapped_fau_offset(__shader, __kind, __offset) \
274 ((FAU_WORD_SIZE * BITSET_PREFIX_SUM((__shader)->fau.used_##__kind, \
275 (__offset) / FAU_WORD_SIZE)) + \
276 ((__offset) % FAU_WORD_SIZE))
277
278 #define shader_remapped_sysval_offset(__shader, __offset) \
279 shader_remapped_fau_offset(__shader, sysvals, __offset)
280
281 #define shader_remapped_push_const_offset(__shader, __offset) \
282 (((__shader)->fau.sysval_count * FAU_WORD_SIZE) + \
283 shader_remapped_fau_offset(__shader, push_consts, __offset))
284
285 #define shader_use_sysval(__shader, __ptype, __name) \
286 BITSET_SET_RANGE((__shader)->fau.used_sysvals, \
287 sysval_fau_start(__ptype, __name), \
288 sysval_fau_end(__ptype, __name))
289
290 #define shader_uses_sysval(__shader, __ptype, __name) \
291 BITSET_TEST_RANGE((__shader)->fau.used_sysvals, \
292 sysval_fau_start(__ptype, __name), \
293 sysval_fau_end(__ptype, __name))
294
295 #define shader_uses_sysval_entry(__shader, __ptype, __name, __idx) \
296 BITSET_TEST_RANGE((__shader)->fau.used_sysvals, \
297 sysval_fau_entry_start(__ptype, __name, __idx), \
298 sysval_fau_entry_end(__ptype, __name, __idx))
299
300 #define shader_use_sysval_range(__shader, __base, __range) \
301 BITSET_SET_RANGE((__shader)->fau.used_sysvals, (__base) / FAU_WORD_SIZE, \
302 ((__base) + (__range) - 1) / FAU_WORD_SIZE)
303
304 #define shader_use_push_const_range(__shader, __base, __range) \
305 BITSET_SET_RANGE((__shader)->fau.used_push_consts, \
306 (__base) / FAU_WORD_SIZE, \
307 ((__base) + (__range) - 1) / FAU_WORD_SIZE)
308
309 #define load_sysval(__b, __ptype, __bitsz, __name) \
310 nir_load_push_constant( \
311 __b, sysval_size(__ptype, __name) / ((__bitsz) / 8), __bitsz, \
312 nir_imm_int(__b, sysval_offset(__ptype, __name)), \
313 .base = SYSVALS_PUSH_CONST_BASE)
314
315 #define load_sysval_entry(__b, __ptype, __bitsz, __name, __dyn_idx) \
316 nir_load_push_constant( \
317 __b, sysval_entry_size(__ptype, __name) / ((__bitsz) / 8), __bitsz, \
318 nir_imul_imm(__b, __dyn_idx, sysval_entry_size(__ptype, __name)), \
319 .base = SYSVALS_PUSH_CONST_BASE + sysval_offset(__ptype, __name), \
320 .range = sysval_size(__ptype, __name))
321
322 #if PAN_ARCH < 9
323 enum panvk_bifrost_desc_table_type {
324 PANVK_BIFROST_DESC_TABLE_INVALID = -1,
325
326 /* UBO is encoded on 8 bytes */
327 PANVK_BIFROST_DESC_TABLE_UBO = 0,
328
329 /* Images are using a <3DAttributeBuffer,Attribute> pair, each
330 * of them being stored in a separate table. */
331 PANVK_BIFROST_DESC_TABLE_IMG,
332
333 /* Texture and sampler are encoded on 32 bytes */
334 PANVK_BIFROST_DESC_TABLE_TEXTURE,
335 PANVK_BIFROST_DESC_TABLE_SAMPLER,
336
337 PANVK_BIFROST_DESC_TABLE_COUNT,
338 };
339 #endif
340
341 #define COPY_DESC_HANDLE(table, idx) ((table << 28) | (idx))
342 #define COPY_DESC_HANDLE_EXTRACT_INDEX(handle) ((handle) & BITFIELD_MASK(28))
343 #define COPY_DESC_HANDLE_EXTRACT_TABLE(handle) ((handle) >> 28)
344
345 #define MAX_COMPUTE_SYSVAL_FAUS \
346 (sizeof(struct panvk_compute_sysvals) / FAU_WORD_SIZE)
347 #define MAX_GFX_SYSVAL_FAUS \
348 (sizeof(struct panvk_graphics_sysvals) / FAU_WORD_SIZE)
349 #define MAX_SYSVAL_FAUS MAX2(MAX_COMPUTE_SYSVAL_FAUS, MAX_GFX_SYSVAL_FAUS)
350 #define MAX_PUSH_CONST_FAUS (MAX_PUSH_CONSTANTS_SIZE / FAU_WORD_SIZE)
351
352 struct panvk_shader_fau_info {
353 BITSET_DECLARE(used_sysvals, MAX_SYSVAL_FAUS);
354 BITSET_DECLARE(used_push_consts, MAX_PUSH_CONST_FAUS);
355 uint32_t sysval_count;
356 uint32_t total_count;
357 };
358
359 struct panvk_shader_desc_info {
360 uint32_t used_set_mask;
361
362 #if PAN_ARCH < 9
363 struct {
364 uint32_t map[MAX_DYNAMIC_UNIFORM_BUFFERS];
365 uint32_t count;
366 } dyn_ubos;
367 struct {
368 uint32_t map[MAX_DYNAMIC_STORAGE_BUFFERS];
369 uint32_t count;
370 } dyn_ssbos;
371 struct {
372 struct panvk_priv_mem map;
373 uint32_t count[PANVK_BIFROST_DESC_TABLE_COUNT];
374 } others;
375 #else
376 struct {
377 uint32_t map[MAX_DYNAMIC_BUFFERS];
378 uint32_t count;
379 } dyn_bufs;
380 uint32_t fs_varying_attr_desc_count;
381 #endif
382 };
383
384 struct panvk_shader_variant {
385 struct pan_shader_info info;
386
387 union {
388 struct {
389 struct pan_compute_dim local_size;
390 } cs;
391
392 struct {
393 struct pan_earlyzs_lut earlyzs_lut;
394 uint32_t input_attachment_read;
395 } fs;
396 };
397
398 struct panvk_shader_desc_info desc_info;
399
400 struct panvk_shader_fau_info fau;
401
402 const void *bin_ptr;
403 uint32_t bin_size;
404 bool own_bin;
405
406 struct panvk_priv_mem code_mem;
407
408 #if PAN_ARCH < 9
409 struct panvk_priv_mem rsd;
410 #else
411 union {
412 struct panvk_priv_mem spd;
413 struct {
414 #if PAN_ARCH < 12
415 struct panvk_priv_mem pos_points;
416 struct panvk_priv_mem pos_triangles;
417 struct panvk_priv_mem var;
418 #else
419 struct panvk_priv_mem all_points;
420 struct panvk_priv_mem all_triangles;
421 #endif
422 } spds;
423 };
424 #endif
425
426 const char *nir_str;
427 const char *asm_str;
428 };
429
430 enum panvk_vs_variant {
431 /* Hardware vertex shader, when next stage is fragment */
432 PANVK_VS_VARIANT_HW,
433
434 PANVK_VS_VARIANTS,
435 };
436
437 struct panvk_shader {
438 struct vk_shader vk;
439
440 struct panvk_shader_variant variants[];
441 };
442
443 static inline unsigned
444 panvk_shader_num_variants(mesa_shader_stage stage)
445 {
446 if (stage == MESA_SHADER_VERTEX)
447 return PANVK_VS_VARIANTS;
448
449 return 1;
450 }
451
452 static const char *panvk_vs_shader_variant_name[] = {
453 [PANVK_VS_VARIANT_HW] = NULL,
454 };
455
456 static const char *
457 panvk_shader_variant_name(const struct panvk_shader *shader,
458 struct panvk_shader_variant *variant)
459 {
460 unsigned i = variant - shader->variants;
461 assert(i < panvk_shader_num_variants(shader->vk.stage));
462
463 if (shader->vk.stage == MESA_SHADER_VERTEX) {
464 assert(i < ARRAY_SIZE(panvk_vs_shader_variant_name));
465 return panvk_vs_shader_variant_name[i];
466 }
467
468 assert(panvk_shader_num_variants(shader->vk.stage) == 1);
469
470 return NULL;
471 }
472
473 static const struct panvk_shader_variant *
474 panvk_shader_only_variant(const struct panvk_shader *shader)
475 {
476 if (!shader)
477 return NULL;
478
479 assert(panvk_shader_num_variants(shader->vk.stage) == 1);
480 return &shader->variants[0];
481 }
482
483 static const struct panvk_shader_variant *
484 panvk_shader_hw_variant(const struct panvk_shader *shader)
485 {
486 if (!shader)
487 return NULL;
488
489 return &shader->variants[0];
490 }
491
492 static inline uint64_t
493 panvk_shader_variant_get_dev_addr(const struct panvk_shader_variant *shader)
494 {
495 return shader != NULL ? panvk_priv_mem_dev_addr(shader->code_mem) : 0;
496 }
497
498 #define panvk_shader_foreach_variant(__shader, __var) \
499 for (struct panvk_shader_variant *__var = (__shader)->variants; \
500 __var < (__shader)->variants + \
501 panvk_shader_num_variants((__shader)->vk.stage); \
502 ++__var)
503
504 #if PAN_ARCH < 9
505 struct panvk_shader_link {
506 struct {
507 struct panvk_priv_mem attribs;
508 } vs, fs;
509 unsigned buf_strides[PANVK_VARY_BUF_MAX];
510 };
511
512 VkResult panvk_per_arch(link_shaders)(struct panvk_pool *desc_pool,
513 const struct panvk_shader_variant *vs,
514 const struct panvk_shader_variant *fs,
515 struct panvk_shader_link *link);
516
517 static inline void
518 panvk_shader_link_cleanup(struct panvk_shader_link *link)
519 {
520 panvk_pool_free_mem(&link->vs.attribs);
521 panvk_pool_free_mem(&link->fs.attribs);
522 }
523 #endif
524
525 bool panvk_per_arch(nir_lower_input_attachment_loads)(
526 nir_shader *nir,
527 const struct vk_graphics_pipeline_state *state,
528 uint32_t *input_attachment_read_out);
529
530 void panvk_per_arch(nir_lower_descriptors)(
531 nir_shader *nir, struct panvk_device *dev,
532 const struct vk_pipeline_robustness_state *rs, uint32_t set_layout_count,
533 struct vk_descriptor_set_layout *const *set_layouts,
534 const struct vk_graphics_pipeline_state *state,
535 struct panvk_shader_desc_info *desc_info);
536
537 /* This a stripped-down version of panvk_shader for internal shaders that
538 * are managed by vk_meta (blend and preload shaders). Those don't need the
539 * complexity inherent to user provided shaders as they're not exposed. */
540 struct panvk_internal_shader {
541 struct vk_shader vk;
542 struct pan_shader_info info;
543 struct panvk_priv_mem code_mem;
544
545 #if PAN_ARCH < 9
546 struct panvk_priv_mem rsd;
547 #else
548 struct panvk_priv_mem spd;
549 #endif
550 };
551
552 VK_DEFINE_NONDISP_HANDLE_CASTS(panvk_internal_shader, vk.base, VkShaderEXT,
553 VK_OBJECT_TYPE_SHADER_EXT)
554
555 void panvk_per_arch(compiler_lock)(void);
556 void panvk_per_arch(compiler_unlock)(void);
557
558 VkResult panvk_per_arch(create_internal_shader)(
559 struct panvk_device *dev, nir_shader *nir,
560 struct pan_compile_inputs *compiler_inputs,
561 struct panvk_internal_shader **shader_out);
562
563 VkResult panvk_per_arch(create_shader_from_binary)(
564 struct panvk_device *dev, const struct pan_shader_info *info,
565 struct pan_compute_dim local_size, const void *bin_ptr, size_t bin_size,
566 struct panvk_shader **shader_out);
567
568 #endif