Debian Codegraph @

src/panfrost/vulkan/panvk_vX_nir_lower_descriptors.c

raw
1 /*
2 * Copyright © 2021 Collabora Ltd.
3 *
4 * Derived from tu_shader.c which is:
5 * Copyright © 2019 Google LLC
6 *
7 * Also derived from anv_pipeline.c which is
8 * Copyright © 2015 Intel Corporation
9 *
10 * SPDX-License-Identifier: MIT
11 */
12
13 #include "panvk_device.h"
14 #include "panvk_shader.h"
15
16 #include "vk_graphics_state.h"
17 #include "vk_pipeline.h"
18 #include "vk_pipeline_layout.h"
19
20 #include "util/bitset.h"
21 #include "nir.h"
22 #include "nir_builder.h"
23
24 #if PAN_ARCH >= 9
25 #define VALHALL_RESOURCE_TABLE_IDX 62
26 #endif
27
28 struct lower_desc_map {
29 /* The index of the map serves as the table offset, the value of the
30 * entry is a COPY_DESC_HANDLE() encoding the source set, and the
31 * index of the descriptor in the set. */
32 uint32_t *map;
33
34 /* Number of entries in the map array. */
35 uint32_t count;
36 };
37
38 struct lower_desc_info {
39 uint32_t used_set_mask;
40 #if PAN_ARCH < 9
41 struct lower_desc_map dyn_ubos;
42 struct lower_desc_map dyn_ssbos;
43 struct lower_desc_map others[PANVK_BIFROST_DESC_TABLE_COUNT];
44 #else
45 uint32_t dummy_sampler_handle;
46 uint32_t dyn_bufs_start;
47 struct lower_desc_map dyn_bufs;
48 uint32_t num_varying_attr_descs;
49 #endif
50 };
51
52 struct lower_desc_ctx {
53 const struct panvk_descriptor_set_layout *set_layouts[MAX_SETS];
54 struct lower_desc_info desc_info;
55 struct hash_table_u64 *ht;
56 bool add_bounds_checks;
57 bool null_descriptor_support;
58 nir_address_format ubo_addr_format;
59 nir_address_format ssbo_addr_format;
60 };
61
62 static nir_address_format
63 addr_format_for_desc_type(nir_descriptor_type desc_type,
64 const struct lower_desc_ctx *ctx)
65 {
66 switch (desc_type) {
67 case nir_descriptor_type_uniform_buffer:
68 return ctx->ubo_addr_format;
69
70 case nir_descriptor_type_storage_buffer:
71 return ctx->ssbo_addr_format;
72
73 default:
74 UNREACHABLE("Unsupported descriptor type");
75 }
76 }
77
78 static const struct panvk_descriptor_set_layout *
79 get_set_layout(uint32_t set, const struct lower_desc_ctx *ctx)
80 {
81 return ctx->set_layouts[set];
82 }
83
84 static const struct panvk_descriptor_set_binding_layout *
85 get_binding_layout(uint32_t set, uint32_t binding,
86 const struct lower_desc_ctx *ctx)
87 {
88 return &get_set_layout(set, ctx)->bindings[binding];
89 }
90
91 struct desc_id {
92 union {
93 struct {
94 uint32_t binding;
95 uint32_t set : 4;
96 uint32_t sampler_subdesc : 1;
97 uint32_t pad : 27;
98 };
99 uint64_t ht_key;
100 };
101 };
102
103 #if PAN_ARCH < 9
104 static enum panvk_bifrost_desc_table_type
105 desc_type_to_table_type(
106 const struct panvk_descriptor_set_binding_layout *binding_layout,
107 bool sampler_subdesc)
108 {
109 switch (binding_layout->type) {
110 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
111 return sampler_subdesc ? PANVK_BIFROST_DESC_TABLE_SAMPLER
112 : PANVK_BIFROST_DESC_TABLE_TEXTURE;
113 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
114 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
115 return PANVK_BIFROST_DESC_TABLE_TEXTURE;
116 case VK_DESCRIPTOR_TYPE_SAMPLER:
117 return PANVK_BIFROST_DESC_TABLE_SAMPLER;
118 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
119 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
120 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
121 return PANVK_BIFROST_DESC_TABLE_IMG;
122 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
123 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK:
124 return PANVK_BIFROST_DESC_TABLE_UBO;
125 default:
126 return PANVK_BIFROST_DESC_TABLE_INVALID;
127 }
128 }
129 #endif
130
131 static uint32_t
132 shader_desc_idx(uint32_t set, uint32_t binding,
133 struct panvk_subdesc_info subdesc,
134 const struct lower_desc_ctx *ctx)
135 {
136 const struct panvk_descriptor_set_layout *set_layout =
137 get_set_layout(set, ctx);
138 const struct panvk_descriptor_set_binding_layout *bind_layout =
139 &set_layout->bindings[binding];
140 uint32_t subdesc_idx = get_subdesc_idx(bind_layout, subdesc);
141
142 /* On Valhall, all non-dynamic descriptors are accessed directly through
143 * their set. The vertex attribute table always comes first, so we always
144 * offset user sets by one if we're dealing with a vertex shader. */
145 if (PAN_ARCH >= 9 && !vk_descriptor_type_is_dynamic(bind_layout->type))
146 return pan_res_handle(set + 1, bind_layout->desc_idx + subdesc_idx);
147
148 /* On Bifrost, the SSBO descriptors are read directly from the set. */
149 if (PAN_ARCH < 9 && bind_layout->type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)
150 return bind_layout->desc_idx;
151
152 struct desc_id src = {
153 .set = set,
154 .sampler_subdesc = subdesc.type == VK_DESCRIPTOR_TYPE_SAMPLER,
155 .binding = binding,
156 };
157 uint32_t *entry =
158 _mesa_hash_table_u64_search(ctx->ht, src.ht_key);
159
160 assert(entry);
161
162 const struct lower_desc_map *map;
163
164 #if PAN_ARCH < 9
165 if (bind_layout->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
166 map = &ctx->desc_info.dyn_ubos;
167 } else if (bind_layout->type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
168 map = &ctx->desc_info.dyn_ssbos;
169 } else {
170 uint32_t table = desc_type_to_table_type(bind_layout, src.sampler_subdesc);
171
172 assert(table < PANVK_BIFROST_DESC_TABLE_COUNT);
173 map = &ctx->desc_info.others[table];
174 }
175 #else
176 map = &ctx->desc_info.dyn_bufs;
177 #endif
178
179 assert(entry >= map->map && entry < map->map + map->count);
180
181 uint32_t idx = entry - map->map;
182
183 #if PAN_ARCH < 9
184 /* Adjust the destination index for all dynamic UBOs, which are laid out
185 * just after the regular UBOs in the UBO table. */
186 if (bind_layout->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
187 idx += ctx->desc_info.others[PANVK_BIFROST_DESC_TABLE_UBO].count;
188 } else if (subdesc.type == VK_DESCRIPTOR_TYPE_SAMPLER) {
189 /* the Cb/Cr planes share the same sampler, so in a 3 plane arrangement
190 * the number of planes can exceed the number of samplers */
191 idx += MIN2(subdesc.plane, bind_layout->samplers_per_desc - 1);
192 } else if (subdesc.type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) {
193 assert(subdesc.plane < bind_layout->textures_per_desc);
194 idx += subdesc.plane;
195 }
196 #else
197 /* Dynamic buffers are pushed directly in the resource tables, after all
198 * sets. */
199 idx = pan_res_handle(0, ctx->desc_info.dyn_bufs_start + idx);
200 #endif
201
202 return idx;
203 }
204
205 static nir_address_format
206 addr_format_for_type(VkDescriptorType type, const struct lower_desc_ctx *ctx)
207 {
208 /* Mutable must imply that both formats are the same. */
209 assert(type != VK_DESCRIPTOR_TYPE_MUTABLE_EXT || ctx->ubo_addr_format == ctx->ssbo_addr_format);
210
211 switch (type) {
212 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
213 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
214 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK:
215 case VK_DESCRIPTOR_TYPE_MUTABLE_EXT:
216 return ctx->ubo_addr_format;
217
218 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
219 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
220 return ctx->ssbo_addr_format;
221
222 default:
223 UNREACHABLE("Unsupported descriptor type");
224 return ~0;
225 }
226 }
227
228 #if PAN_ARCH < 9
229 static uint32_t
230 shader_ssbo_table(nir_builder *b, unsigned set, unsigned binding,
231 const struct lower_desc_ctx *ctx)
232 {
233 const struct panvk_descriptor_set_layout *set_layout =
234 get_set_layout(set, ctx);
235 const struct panvk_descriptor_set_binding_layout *bind_layout =
236 &set_layout->bindings[binding];
237
238 assert(bind_layout->type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER ||
239 bind_layout->type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
240 bool is_dyn = bind_layout->type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC;
241
242 if (!is_dyn)
243 return PANVK_DESC_TABLE_USER + set;
244
245 switch (b->shader->info.stage) {
246 case MESA_SHADER_COMPUTE:
247 return PANVK_DESC_TABLE_CS_DYN_SSBOS;
248 case MESA_SHADER_VERTEX:
249 return PANVK_DESC_TABLE_VS_DYN_SSBOS;
250 case MESA_SHADER_FRAGMENT:
251 return PANVK_DESC_TABLE_FS_DYN_SSBOS;
252 default:
253 assert(!"Invalid stage");
254 return ~0;
255 }
256 }
257 #endif
258
259 /** Build a Vulkan resource index
260 *
261 * A "resource index" is the term used by our SPIR-V parser and the relevant
262 * NIR intrinsics for a reference into a descriptor set. It acts much like a
263 * deref in NIR except that it accesses opaque descriptors instead of memory.
264 *
265 * Coming out of SPIR-V, both the resource indices (in the form of
266 * vulkan_resource_[re]index intrinsics) and the memory derefs (in the form
267 * of nir_deref_instr) use the same vector component/bit size. The meaning
268 * of those values for memory derefs (nir_deref_instr) is given by the
269 * nir_address_format associated with the descriptor type. For resource
270 * indices, it's an entirely internal to panvk encoding which describes, in
271 * some sense, the address of the descriptor. Thanks to the NIR/SPIR-V rules,
272 * it must be packed into the same size SSA values as a memory address. For
273 * this reason, the actual encoding may depend both on the address format for
274 * memory derefs and the descriptor address format.
275 *
276 * The load_vulkan_descriptor intrinsic exists to provide a transition point
277 * between these two forms of derefs: descriptor and memory.
278 */
279 static nir_def *
280 build_res_index(nir_builder *b, uint32_t set, uint32_t binding,
281 nir_def *array_index, nir_address_format addr_format,
282 const struct lower_desc_ctx *ctx)
283 {
284 const struct panvk_descriptor_set_layout *set_layout =
285 get_set_layout(set, ctx);
286 const struct panvk_descriptor_set_binding_layout *bind_layout =
287 &set_layout->bindings[binding];
288 uint32_t array_size = bind_layout->desc_count;
289 nir_address_format addr_fmt = addr_format_for_type(bind_layout->type, ctx);
290 uint32_t desc_idx = shader_desc_idx(set, binding, NO_SUBDESC, ctx);
291
292 switch (addr_fmt) {
293 #if PAN_ARCH < 9
294 case nir_address_format_32bit_index_offset: {
295 const uint32_t packed_desc_idx_array_size =
296 (array_size - 1) << 16 | desc_idx;
297
298 return nir_vec2(b, nir_imm_int(b, packed_desc_idx_array_size),
299 array_index);
300 }
301
302 case nir_address_format_64bit_bounded_global:
303 case nir_address_format_64bit_global_32bit_offset: {
304 unsigned desc_table = shader_ssbo_table(b, set, binding, ctx);
305
306 return nir_vec4(b, nir_imm_int(b, desc_table),
307 nir_imm_int(b, desc_idx), array_index,
308 nir_imm_int(b, array_size - 1));
309 }
310 #else
311 case nir_address_format_vec2_index_32bit_offset:
312 return nir_vec3(b, nir_imm_int(b, desc_idx), array_index,
313 nir_imm_int(b, array_size - 1));
314 #endif
315
316 default:
317 UNREACHABLE("Unsupported descriptor type");
318 }
319 }
320
321 /** Adjust a Vulkan resource index
322 *
323 * This is the equivalent of nir_deref_type_ptr_as_array for resource indices.
324 * For array descriptors, it allows us to adjust the array index. Thanks to
325 * variable pointers, we cannot always fold this re-index operation into the
326 * vulkan_resource_index intrinsic and we have to do it based on nothing but
327 * the address format.
328 */
329 static nir_def *
330 build_res_reindex(nir_builder *b, nir_def *orig, nir_def *delta,
331 nir_address_format addr_format)
332 {
333 switch (addr_format) {
334 #if PAN_ARCH < 9
335 case nir_address_format_32bit_index_offset:
336 return nir_vec2(b, nir_channel(b, orig, 0),
337 nir_iadd(b, nir_channel(b, orig, 1), delta));
338
339 case nir_address_format_64bit_bounded_global:
340 case nir_address_format_64bit_global_32bit_offset:
341 return nir_vec4(b, nir_channel(b, orig, 0), nir_channel(b, orig, 1),
342 nir_iadd(b, nir_channel(b, orig, 2), delta),
343 nir_imm_int(b, 3));
344 #else
345 case nir_address_format_vec2_index_32bit_offset:
346 return nir_vec3(b, nir_channel(b, orig, 0),
347 nir_iadd(b, nir_channel(b, orig, 1), delta),
348 nir_channel(b, orig, 2));
349 #endif
350
351 default:
352 UNREACHABLE("Unhandled address format");
353 }
354 }
355
356 /** Convert a Vulkan resource index into a buffer address
357 *
358 * In some cases, this does a memory load from the descriptor set and, in
359 * others, it simply converts from one form to another.
360 *
361 * See build_res_index for details about each resource index format.
362 */
363 static nir_def *
364 build_buffer_addr_for_res_index(nir_builder *b, nir_def *res_index,
365 nir_address_format addr_format,
366 const struct lower_desc_ctx *ctx)
367 {
368 switch (addr_format) {
369 #if PAN_ARCH < 9
370 case nir_address_format_32bit_index_offset: {
371 nir_def *packed = nir_channel(b, res_index, 0);
372 nir_def *array_index = nir_channel(b, res_index, 1);
373 nir_def *first_desc_index = nir_extract_u16(b, packed, nir_imm_int(b, 0));
374 nir_def *array_max = nir_extract_u16(b, packed, nir_imm_int(b, 1));
375
376 if (ctx->add_bounds_checks)
377 array_index = nir_umin(b, array_index, array_max);
378
379 return nir_vec2(b, nir_iadd(b, first_desc_index, array_index),
380 nir_imm_int(b, 0));
381 }
382
383 case nir_address_format_64bit_bounded_global:
384 case nir_address_format_64bit_global_32bit_offset: {
385 nir_def *desc_table_index = nir_channel(b, res_index, 0);
386 nir_def *first_desc_index = nir_channel(b, res_index, 1);
387 nir_def *array_index = nir_channel(b, res_index, 2);
388 nir_def *array_max = nir_channel(b, res_index, 3);
389
390 if (ctx->add_bounds_checks)
391 array_index = nir_umin(b, array_index, array_max);
392
393 nir_def *desc_offset = nir_imul_imm(
394 b, nir_iadd(b, array_index, first_desc_index), PANVK_DESCRIPTOR_SIZE);
395
396 nir_def *base_addr =
397 b->shader->info.stage == MESA_SHADER_COMPUTE
398 ? load_sysval_entry(b, compute, 64, desc.sets, desc_table_index)
399 : load_sysval_entry(b, graphics, 64, desc.sets, desc_table_index);
400
401 nir_def *desc_addr = nir_iadd(b, base_addr, nir_u2u64(b, desc_offset));
402 nir_def *desc = nir_load_global(b, 4, 32, desc_addr,
403 .align_mul = PANVK_DESCRIPTOR_SIZE);
404
405 /* The offset in the descriptor is guaranteed to be zero when it's
406 * written into the descriptor set. This lets us avoid some unnecessary
407 * adds.
408 */
409 return nir_vec4(b, nir_channel(b, desc, 0), nir_channel(b, desc, 1),
410 nir_channel(b, desc, 2), nir_imm_int(b, 0));
411 }
412 #else
413 case nir_address_format_vec2_index_32bit_offset: {
414 nir_def *first_desc_index = nir_channel(b, res_index, 0);
415 nir_def *array_index = nir_channel(b, res_index, 1);
416 nir_def *array_max = nir_channel(b, res_index, 2);
417
418 if (ctx->add_bounds_checks)
419 array_index = nir_umin(b, array_index, array_max);
420
421 return nir_vec3(b, first_desc_index, array_index, nir_imm_int(b, 0));
422 }
423 #endif
424
425 default:
426 UNREACHABLE("Unhandled address format");
427 }
428 }
429
430 static bool
431 lower_res_intrinsic(nir_builder *b, nir_intrinsic_instr *intrin,
432 const struct lower_desc_ctx *ctx)
433 {
434 b->cursor = nir_before_instr(&intrin->instr);
435
436 const nir_descriptor_type desc_type = nir_intrinsic_desc_type(intrin);
437 nir_address_format addr_format = addr_format_for_desc_type(desc_type, ctx);
438
439 nir_def *res;
440 switch (intrin->intrinsic) {
441 case nir_intrinsic_vulkan_resource_index:
442 res = build_res_index(b, nir_intrinsic_desc_set(intrin),
443 nir_intrinsic_binding(intrin), intrin->src[0].ssa,
444 addr_format, ctx);
445 break;
446
447 case nir_intrinsic_vulkan_resource_reindex:
448 res = build_res_reindex(b, intrin->src[0].ssa, intrin->src[1].ssa,
449 addr_format);
450 break;
451
452 case nir_intrinsic_load_vulkan_descriptor:
453 res = build_buffer_addr_for_res_index(b, intrin->src[0].ssa, addr_format,
454 ctx);
455 break;
456
457 default:
458 UNREACHABLE("Unhandled resource intrinsic");
459 }
460
461 assert(intrin->def.bit_size == res->bit_size);
462 assert(intrin->def.num_components == res->num_components);
463 nir_def_replace(&intrin->def, res);
464
465 return true;
466 }
467
468 static void
469 get_resource_deref_binding(nir_deref_instr *deref, uint32_t *set,
470 uint32_t *binding, uint32_t *index_imm,
471 nir_def **index_ssa, uint32_t *max_idx)
472 {
473 *index_imm = 0;
474 *max_idx = 0;
475 *index_ssa = NULL;
476
477 if (deref->deref_type == nir_deref_type_array) {
478 if (nir_src_is_const(deref->arr.index)) {
479 *index_imm = nir_src_as_uint(deref->arr.index);
480 *max_idx = *index_imm;
481 } else {
482 *index_ssa = deref->arr.index.ssa;
483
484 /* Zero means variable array. The minus one should give us UINT32_MAX,
485 * which matches what we want. */
486 *max_idx = ((uint32_t)glsl_array_size(nir_deref_instr_parent(deref)->type)) - 1;
487 }
488
489 deref = nir_deref_instr_parent(deref);
490 }
491
492 assert(deref->deref_type == nir_deref_type_var);
493 nir_variable *var = deref->var;
494
495 *set = var->data.descriptor_set;
496 *binding = var->data.binding;
497 }
498
499 static nir_def *
500 load_resource_deref_desc(nir_builder *b, nir_deref_instr *deref,
501 VkDescriptorType subdesc_type, unsigned desc_offset,
502 unsigned num_components, unsigned bit_size,
503 const struct lower_desc_ctx *ctx)
504 {
505 uint32_t set, binding, index_imm, max_idx;
506 nir_def *index_ssa;
507 get_resource_deref_binding(deref, &set, &binding, &index_imm, &index_ssa,
508 &max_idx);
509
510 const struct panvk_descriptor_set_layout *set_layout =
511 get_set_layout(set, ctx);
512 const struct panvk_descriptor_set_binding_layout *bind_layout =
513 &set_layout->bindings[binding];
514 struct panvk_subdesc_info subdesc =
515 subdesc_type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
516 ? get_tex_subdesc_info(bind_layout->type, 0)
517 : subdesc_type == VK_DESCRIPTOR_TYPE_SAMPLER
518 ? get_sampler_subdesc_info(bind_layout->type, 0)
519 : NO_SUBDESC;
520
521 unsigned subdesc_idx = get_subdesc_idx(bind_layout, subdesc);
522
523 assert(index_ssa == NULL || index_imm == 0);
524 if (index_ssa == NULL)
525 index_ssa = nir_imm_int(b, index_imm);
526
527 unsigned desc_stride = panvk_get_desc_stride(bind_layout);
528 nir_def *set_offset =
529 nir_imul_imm(b,
530 nir_iadd_imm(b, nir_imul_imm(b, index_ssa, desc_stride),
531 bind_layout->desc_idx + subdesc_idx),
532 PANVK_DESCRIPTOR_SIZE);
533
534 set_offset = nir_iadd_imm(b, set_offset, desc_offset);
535
536 #if PAN_ARCH < 9
537 nir_def *set_base_addr =
538 b->shader->info.stage == MESA_SHADER_COMPUTE
539 ? load_sysval_entry(b, compute, 64, desc.sets, nir_imm_int(b, set))
540 : load_sysval_entry(b, graphics, 64, desc.sets, nir_imm_int(b, set));
541
542 unsigned desc_align = 1 << (ffs(PANVK_DESCRIPTOR_SIZE + desc_offset) - 1);
543
544 return nir_load_global(b, num_components, bit_size,
545 nir_iadd(b, set_base_addr, nir_u2u64(b, set_offset)),
546 .align_mul = desc_align);
547 #else
548 /* note that user sets start from index 1 */
549 return nir_load_ubo(
550 b, num_components, bit_size,
551 nir_imm_int(b, pan_res_handle(VALHALL_RESOURCE_TABLE_IDX, set + 1)),
552 set_offset, .range = ~0u, .align_mul = PANVK_DESCRIPTOR_SIZE,
553 .align_offset = desc_offset);
554 #endif
555 }
556
557 static nir_def *
558 is_nulldesc(nir_builder *b, nir_deref_instr *deref,
559 enum VkDescriptorType desc_type, const struct lower_desc_ctx *ctx)
560 {
561 nir_def *desc_header =
562 load_resource_deref_desc(b, deref, desc_type, 0, 1, 16, ctx);
563 /* If the first 16 bits are all zero (specifically the descriptor type),
564 * this is a nulldescriptor, in which case we need to avoid the "add 1"
565 * when loading the size from the descriptor. */
566 return nir_ieq_imm(b, desc_header, 0);
567 }
568
569 static nir_def *
570 load_tex_size(nir_builder *b, nir_deref_instr *deref, enum glsl_sampler_dim dim,
571 bool is_array, const struct lower_desc_ctx *ctx)
572 {
573 nir_def *loaded_size;
574 if (dim == GLSL_SAMPLER_DIM_BUF) {
575 #if PAN_ARCH >= 9
576 nir_def *size = load_resource_deref_desc(
577 b, deref, VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 4, 1, 32, ctx);
578 nir_def *stride = load_resource_deref_desc(
579 b, deref, VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 16, 1, 32, ctx);
580 loaded_size = nir_idiv(b, size, stride);
581 #else
582 nir_def *stride_size = load_resource_deref_desc(
583 b, deref, VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 8, 2, 32, ctx);
584 loaded_size = nir_idiv(b, nir_channel(b, stride_size, 1),
585 nir_channel(b, stride_size, 0));
586 #endif
587 } else {
588 nir_def *tex_w_h = load_resource_deref_desc(
589 b, deref, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 4, 2, 16, ctx);
590 nir_def *tex_depth_or_layer_count = load_resource_deref_desc(
591 b, deref, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
592 dim == GLSL_SAMPLER_DIM_3D ? 28 : 24, 1, 16, ctx);
593
594 nir_def *tex_sz =
595 is_array && dim == GLSL_SAMPLER_DIM_1D
596 ? nir_vec2(b, nir_channel(b, tex_w_h, 0), tex_depth_or_layer_count)
597 : nir_vec3(b, nir_channel(b, tex_w_h, 0),
598 nir_channel(b, tex_w_h, 1), tex_depth_or_layer_count);
599
600 tex_sz = nir_pad_vector_imm_int(b, tex_sz, 0, 4);
601
602 /* The sizes are provided as 16-bit values with 1 subtracted so
603 * convert to 32-bit and add 1.
604 */
605 loaded_size = nir_iadd_imm(b, nir_u2u32(b, tex_sz), 1);
606 }
607
608 if (PAN_ARCH >= 9 && ctx->null_descriptor_support) {
609 nir_def *nulldesc =
610 is_nulldesc(b, deref, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, ctx);
611 return nir_bcsel(b, nulldesc, nir_u2u32(b, nir_imm_int(b, 0)),
612 loaded_size);
613 }
614
615 return loaded_size;
616 }
617
618 static nir_def *
619 load_img_size(nir_builder *b, nir_deref_instr *deref, enum glsl_sampler_dim dim,
620 bool is_array, const struct lower_desc_ctx *ctx)
621 {
622 if (PAN_ARCH >= 9)
623 return load_tex_size(b, deref, dim, is_array, ctx);
624
625 if (dim == GLSL_SAMPLER_DIM_BUF) {
626 nir_def *stride_size = load_resource_deref_desc(
627 b, deref, VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 8, 2, 32, ctx);
628 return nir_idiv(b, nir_channel(b, stride_size, 1),
629 nir_channel(b, stride_size, 0));
630 } else {
631 nir_def *tex_sz = load_resource_deref_desc(
632 b, deref, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 18, 3, 16, ctx);
633
634 if (is_array && dim == GLSL_SAMPLER_DIM_CUBE)
635 tex_sz =
636 nir_vector_insert_imm(b, tex_sz,
637 nir_udiv_imm(b, nir_channel(b, tex_sz, 2),
638 6),
639 2);
640
641 if (is_array && dim == GLSL_SAMPLER_DIM_1D)
642 tex_sz =
643 nir_vec2(b, nir_channel(b, tex_sz, 0), nir_channel(b, tex_sz, 2));
644
645 tex_sz = nir_pad_vector_imm_int(b, tex_sz, 0, 4);
646
647 /* The sizes are provided as 16-bit values with 1 subtracted so
648 * convert to 32-bit and add 1.
649 */
650 tex_sz = nir_iadd_imm(b, nir_u2u32(b, tex_sz), 1);
651
652 if (is_array && dim == GLSL_SAMPLER_DIM_MS) {
653 /* log2(sample_count) is placed into bits 7..9 of the pixel
654 * stride (see prepare_attr_buf_descs) which is stored at
655 * offset 8 in the attribute descriptor buffer
656 */
657 nir_def *sample_count = load_resource_deref_desc(
658 b, deref, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 8, 1, 32, ctx);
659 sample_count = nir_iand_imm(b, nir_ushr_imm(b, sample_count, 7),
660 BITFIELD_MASK(3));
661 /* the Z value was lowered by the number of samples so that
662 * we could make the multisampled array look like a 3D texture
663 * Undo that here so we get the correct size
664 */
665 nir_def *z_val = nir_channel(b, tex_sz, 2);
666 z_val = nir_ushr(b, z_val, sample_count);
667 tex_sz = nir_vector_insert_imm(b, tex_sz, z_val, 2);
668 }
669 return tex_sz;
670 }
671 }
672
673 static nir_def *
674 load_tex_levels(nir_builder *b, nir_deref_instr *deref,
675 enum glsl_sampler_dim dim, const struct lower_desc_ctx *ctx)
676 {
677 assert(dim != GLSL_SAMPLER_DIM_BUF);
678
679 /* LOD count is stored in word2[16:21] and has a minus(1) modifier. */
680 nir_def *tex_word2 = load_resource_deref_desc(
681 b, deref, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 8, 1, 32, ctx);
682 nir_def *lod_count = nir_iand_imm(b, nir_ushr_imm(b, tex_word2, 16), 0x1f);
683 nir_def *loaded_levels = nir_iadd_imm(b, lod_count, 1);
684
685 if (PAN_ARCH >= 9 && ctx->null_descriptor_support) {
686 nir_def *nulldesc =
687 is_nulldesc(b, deref, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, ctx);
688 return nir_bcsel(b, nulldesc, nir_imm_int(b, 0), loaded_levels);
689 }
690
691 return loaded_levels;
692 }
693
694 static nir_def *
695 load_tex_samples(nir_builder *b, nir_deref_instr *deref,
696 enum glsl_sampler_dim dim, const struct lower_desc_ctx *ctx)
697 {
698 assert(dim != GLSL_SAMPLER_DIM_BUF);
699
700 /* Sample count is stored in word3[13:25], and has a log2 modifier. */
701 nir_def *tex_word3 = load_resource_deref_desc(
702 b, deref, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 12, 1, 32, ctx);
703 nir_def *sample_count = nir_iand_imm(b, nir_ushr_imm(b, tex_word3, 13), 0x7);
704 nir_def *loaded_samples = nir_ishl(b, nir_imm_int(b, 1), sample_count);
705
706 if (PAN_ARCH >= 9 && ctx->null_descriptor_support) {
707 nir_def *nulldesc =
708 is_nulldesc(b, deref, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, ctx);
709 return nir_bcsel(b, nulldesc, nir_imm_int(b, 0), loaded_samples);
710 }
711
712 return loaded_samples;
713 }
714
715 static nir_def *
716 load_img_samples(nir_builder *b, nir_deref_instr *deref,
717 enum glsl_sampler_dim dim, const struct lower_desc_ctx *ctx)
718 {
719 if (PAN_ARCH >= 9)
720 return load_tex_samples(b, deref, dim, ctx);
721
722 assert(dim != GLSL_SAMPLER_DIM_BUF);
723
724 /* log2(sample_count) is placed into bits 7..9 of the pixel
725 * stride (see prepare_attr_buf_descs) which is stored at
726 * offset 8 in the attribute descriptor buffer
727 */
728 nir_def *one = nir_imm_int(b, 1);
729 nir_def *sample_count = load_resource_deref_desc(
730 b, deref, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 8, 1, 32, ctx);
731
732 sample_count = nir_iand_imm(b, nir_ushr_imm(b, sample_count, 7),
733 BITFIELD_MASK(3));
734 return nir_ishl(b, one, sample_count);
735 }
736
737 static uint32_t
738 get_desc_array_stride(const struct panvk_descriptor_set_binding_layout *layout,
739 VkDescriptorType subtype)
740 {
741 if (PAN_ARCH >= 9)
742 return panvk_get_desc_stride(layout);
743
744 if (layout->type != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
745 return 1;
746
747 /* On Bifrost, descriptors are copied from the sets to the final
748 * descriptor tables which are per-type. For combined image-sampler,
749 * the stride is {textures/samplers}_per_desc in this context;
750 * otherwise the stride is one. */
751 switch(subtype) {
752 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
753 return layout->textures_per_desc;
754 case VK_DESCRIPTOR_TYPE_SAMPLER:
755 return layout->samplers_per_desc;
756 default:
757 UNREACHABLE("Invalid subtype");
758 }
759 }
760
761 static bool
762 lower_tex(nir_builder *b, nir_tex_instr *tex, const struct lower_desc_ctx *ctx)
763 {
764 bool progress = false;
765
766 b->cursor = nir_before_instr(&tex->instr);
767
768 if (tex->op == nir_texop_txs || tex->op == nir_texop_query_levels ||
769 tex->op == nir_texop_texture_samples) {
770 int tex_src_idx = nir_tex_instr_src_index(tex, nir_tex_src_texture_deref);
771 assert(tex_src_idx >= 0);
772 nir_deref_instr *deref = nir_src_as_deref(tex->src[tex_src_idx].src);
773
774 const enum glsl_sampler_dim dim = tex->sampler_dim;
775
776 nir_def *res;
777 switch (tex->op) {
778 case nir_texop_txs:
779 res = nir_channels(b, load_tex_size(b, deref, dim, tex->is_array, ctx),
780 nir_component_mask(tex->def.num_components));
781 break;
782 case nir_texop_query_levels:
783 assert(tex->def.num_components == 1);
784 res = load_tex_levels(b, deref, dim, ctx);
785 break;
786 case nir_texop_texture_samples:
787 assert(tex->def.num_components == 1);
788 res = load_tex_samples(b, deref, dim, ctx);
789 break;
790 default:
791 UNREACHABLE("Unsupported texture query op");
792 }
793
794 nir_def_replace(&tex->def, res);
795 return true;
796 }
797
798 uint32_t plane = 0;
799 int sampler_src_idx =
800 nir_tex_instr_src_index(tex, nir_tex_src_sampler_deref);
801 if (sampler_src_idx >= 0) {
802 nir_def *plane_ssa = nir_steal_tex_src(tex, nir_tex_src_plane);
803 plane =
804 plane_ssa ? nir_src_as_uint(nir_src_for_ssa(plane_ssa)) : 0;
805
806 nir_deref_instr *deref = nir_src_as_deref(tex->src[sampler_src_idx].src);
807 nir_tex_instr_remove_src(tex, sampler_src_idx);
808
809 uint32_t set, binding, index_imm, max_idx;
810 nir_def *index_ssa;
811 get_resource_deref_binding(deref, &set, &binding, &index_imm, &index_ssa, &max_idx);
812
813 const struct panvk_descriptor_set_layout *set_layout =
814 get_set_layout(set, ctx);
815 const struct panvk_descriptor_set_binding_layout *bind_layout =
816 &set_layout->bindings[binding];
817 struct panvk_subdesc_info subdesc =
818 get_sampler_subdesc_info(bind_layout->type, plane);
819 uint32_t desc_stride = get_desc_array_stride(bind_layout, subdesc.type);
820 uint32_t sampler_index = shader_desc_idx(set, binding, subdesc, ctx) + index_imm * desc_stride;
821
822 if (index_ssa != NULL) {
823 nir_def *offset = nir_imul_imm(b, index_ssa, desc_stride);
824 offset = nir_iadd_imm(b, offset, sampler_index);
825 nir_tex_instr_add_src(tex, nir_tex_src_sampler_offset, offset);
826 } else {
827 tex->sampler_index = sampler_index;
828 }
829 progress = true;
830 } else {
831 #if PAN_ARCH >= 9
832 tex->sampler_index = ctx->desc_info.dummy_sampler_handle;
833 #endif
834 }
835
836 int tex_src_idx = nir_tex_instr_src_index(tex, nir_tex_src_texture_deref);
837 if (tex_src_idx >= 0) {
838 nir_deref_instr *deref = nir_src_as_deref(tex->src[tex_src_idx].src);
839 nir_tex_instr_remove_src(tex, tex_src_idx);
840
841 uint32_t set, binding, index_imm, max_idx;
842 nir_def *index_ssa;
843 get_resource_deref_binding(deref, &set, &binding, &index_imm, &index_ssa,
844 &max_idx);
845
846 const struct panvk_descriptor_set_layout *set_layout =
847 get_set_layout(set, ctx);
848 const struct panvk_descriptor_set_binding_layout *bind_layout =
849 &set_layout->bindings[binding];
850 struct panvk_subdesc_info subdesc =
851 get_tex_subdesc_info(bind_layout->type, plane);
852 uint32_t desc_stride = get_desc_array_stride(bind_layout, subdesc.type);
853 uint32_t texture_index = shader_desc_idx(set, binding, subdesc, ctx) + index_imm * desc_stride;
854
855 if (index_ssa != NULL) {
856 nir_def *offset = nir_imul_imm(b, index_ssa, desc_stride);
857 offset = nir_iadd_imm(b, offset, texture_index);
858 nir_tex_instr_add_src(tex, nir_tex_src_texture_offset, offset);
859 } else {
860 tex->texture_index = texture_index;
861 }
862 progress = true;
863 }
864
865 return progress;
866 }
867
868 static nir_def *
869 get_img_index(nir_builder *b, nir_deref_instr *deref,
870 const struct lower_desc_ctx *ctx)
871 {
872 uint32_t set, binding, index_imm, max_idx;
873 nir_def *index_ssa;
874 get_resource_deref_binding(deref, &set, &binding, &index_imm, &index_ssa,
875 &max_idx);
876
877 ASSERTED const struct panvk_descriptor_set_binding_layout *bind_layout =
878 get_binding_layout(set, binding, ctx);
879 assert(bind_layout->type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE ||
880 bind_layout->type == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER ||
881 bind_layout->type == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER ||
882 bind_layout->type == VK_DESCRIPTOR_TYPE_MUTABLE_EXT);
883
884 unsigned img_offset = shader_desc_idx(set, binding, NO_SUBDESC, ctx);
885
886 if (index_ssa == NULL) {
887 return nir_imm_int(b, img_offset + index_imm);
888 } else {
889 assert(index_imm == 0);
890 return nir_iadd_imm(b, index_ssa, img_offset);
891 }
892 }
893
894 static bool
895 lower_img_intrinsic(nir_builder *b, nir_intrinsic_instr *intr,
896 struct lower_desc_ctx *ctx)
897 {
898 b->cursor = nir_before_instr(&intr->instr);
899 nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
900
901 if (intr->intrinsic == nir_intrinsic_image_deref_size ||
902 intr->intrinsic == nir_intrinsic_image_deref_samples) {
903 const enum glsl_sampler_dim dim = nir_intrinsic_image_dim(intr);
904 bool is_array = nir_intrinsic_image_array(intr);
905
906 nir_def *res;
907 switch (intr->intrinsic) {
908 case nir_intrinsic_image_deref_size:
909 res = nir_channels(b, load_img_size(b, deref, dim, is_array, ctx),
910 nir_component_mask(intr->def.num_components));
911 break;
912 case nir_intrinsic_image_deref_samples:
913 res = load_img_samples(b, deref, dim, ctx);
914 break;
915 default:
916 UNREACHABLE("Unsupported image query op");
917 }
918
919 nir_def_replace(&intr->def, res);
920 } else {
921 nir_rewrite_image_intrinsic(intr, get_img_index(b, deref, ctx),
922 nir_image_intrinsic_type_default);
923 }
924
925 return true;
926 }
927
928 static bool
929 lower_intrinsic(nir_builder *b, nir_intrinsic_instr *intr,
930 struct lower_desc_ctx *ctx)
931 {
932 switch (intr->intrinsic) {
933 case nir_intrinsic_vulkan_resource_index:
934 case nir_intrinsic_vulkan_resource_reindex:
935 case nir_intrinsic_load_vulkan_descriptor:
936 return lower_res_intrinsic(b, intr, ctx);
937 case nir_intrinsic_image_deref_store:
938 case nir_intrinsic_image_deref_load:
939 case nir_intrinsic_image_deref_atomic:
940 case nir_intrinsic_image_deref_atomic_swap:
941 case nir_intrinsic_image_deref_size:
942 case nir_intrinsic_image_deref_samples:
943 return lower_img_intrinsic(b, intr, ctx);
944 default:
945 return false;
946 }
947 }
948
949 static bool
950 lower_descriptors_instr(nir_builder *b, nir_instr *instr, void *data)
951 {
952 struct lower_desc_ctx *ctx = data;
953
954 switch (instr->type) {
955 case nir_instr_type_tex:
956 return lower_tex(b, nir_instr_as_tex(instr), ctx);
957 case nir_instr_type_intrinsic:
958 return lower_intrinsic(b, nir_instr_as_intrinsic(instr), ctx);
959 default:
960 return false;
961 }
962 }
963
964 static void
965 record_binding(struct lower_desc_ctx *ctx, unsigned set, unsigned binding,
966 struct panvk_subdesc_info subdesc, uint32_t max_idx)
967 {
968 const struct panvk_descriptor_set_layout *set_layout = ctx->set_layouts[set];
969 const struct panvk_descriptor_set_binding_layout *binding_layout =
970 &set_layout->bindings[binding];
971 ASSERTED uint32_t subdesc_idx = get_subdesc_idx(binding_layout, subdesc);
972 ASSERTED uint32_t desc_stride = panvk_get_desc_stride(binding_layout);
973 ASSERTED uint32_t max_desc_stride = MAX2(
974 binding_layout->samplers_per_desc + binding_layout->textures_per_desc, 1);
975
976 assert(desc_stride >= 1 && desc_stride <= max_desc_stride);
977 ctx->desc_info.used_set_mask |= BITFIELD_BIT(set);
978
979 /* On valhall, we only record dynamic bindings, others are accessed directly
980 * from the set. */
981 if (PAN_ARCH >= 9 && !vk_descriptor_type_is_dynamic(binding_layout->type))
982 return;
983
984 /* SSBOs are accessed directly from the sets, no need to record accesses
985 * to such resources. */
986 if (PAN_ARCH < 9 &&
987 binding_layout->type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)
988 return;
989
990 assert(subdesc_idx < desc_stride);
991
992 struct desc_id src = {
993 .set = set,
994 .sampler_subdesc = subdesc.type == VK_DESCRIPTOR_TYPE_SAMPLER,
995 .binding = binding,
996 };
997 uint32_t *entry = _mesa_hash_table_u64_search(ctx->ht, src.ht_key);
998 uint32_t old_desc_count = (uintptr_t)entry;
999 uint32_t new_desc_count =
1000 max_idx == UINT32_MAX ? binding_layout->desc_count : max_idx + 1;
1001
1002 assert(new_desc_count <= binding_layout->desc_count);
1003
1004 if (old_desc_count >= new_desc_count)
1005 return;
1006
1007 _mesa_hash_table_u64_insert(ctx->ht, src.ht_key,
1008 (void *)(uintptr_t)new_desc_count);
1009
1010 uint32_t desc_count_diff = new_desc_count - old_desc_count;
1011
1012 #if PAN_ARCH < 9
1013 if (binding_layout->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
1014 ctx->desc_info.dyn_ubos.count += desc_count_diff;
1015 } else if (binding_layout->type ==
1016 VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
1017 ctx->desc_info.dyn_ssbos.count += desc_count_diff;
1018 } else {
1019 uint32_t table =
1020 desc_type_to_table_type(binding_layout, src.sampler_subdesc);
1021
1022 assert(table < PANVK_BIFROST_DESC_TABLE_COUNT);
1023 ctx->desc_info.others[table].count +=
1024 desc_count_diff * get_desc_array_stride(binding_layout, subdesc.type);
1025 }
1026 #else
1027 ctx->desc_info.dyn_bufs.count += desc_count_diff;
1028 #endif
1029 }
1030
1031 static uint32_t *
1032 fill_copy_descs_for_binding(struct lower_desc_ctx *ctx, unsigned set,
1033 unsigned binding, bool sampler_subdesc,
1034 uint32_t desc_count)
1035 {
1036 assert(desc_count);
1037
1038 const struct panvk_descriptor_set_layout *set_layout = ctx->set_layouts[set];
1039 const struct panvk_descriptor_set_binding_layout *binding_layout =
1040 &set_layout->bindings[binding];
1041 uint32_t desc_stride = panvk_get_desc_stride(binding_layout);
1042 uint32_t subdesc_offset = sampler_subdesc ? binding_layout->textures_per_desc : 0;
1043 uint32_t *first_entry = NULL;
1044
1045 assert(desc_count <= binding_layout->desc_count);
1046
1047 for (uint32_t i = 0; i < desc_count; i++) {
1048 uint32_t src_idx =
1049 binding_layout->desc_idx + (i * desc_stride) + subdesc_offset;
1050 struct lower_desc_map *map;
1051
1052 #if PAN_ARCH < 9
1053 if (binding_layout->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
1054 map = &ctx->desc_info.dyn_ubos;
1055 } else if (binding_layout->type ==
1056 VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
1057 map = &ctx->desc_info.dyn_ssbos;
1058 } else {
1059 uint32_t dst_table =
1060 desc_type_to_table_type(binding_layout, sampler_subdesc);
1061
1062 assert(dst_table < PANVK_BIFROST_DESC_TABLE_COUNT);
1063 map = &ctx->desc_info.others[dst_table];
1064 }
1065 #else
1066 map = &ctx->desc_info.dyn_bufs;
1067 #endif
1068
1069 if (!first_entry)
1070 first_entry = &map->map[map->count];
1071
1072 uint32_t desc_array_stride = get_desc_array_stride(
1073 binding_layout, sampler_subdesc ? VK_DESCRIPTOR_TYPE_SAMPLER
1074 : VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
1075
1076 for (uint32_t j = 0; j < desc_array_stride; j++)
1077 map->map[map->count++] = COPY_DESC_HANDLE(set, src_idx + j);
1078 }
1079
1080 return first_entry;
1081 }
1082
1083 static void
1084 create_copy_table(nir_shader *nir, struct lower_desc_ctx *ctx)
1085 {
1086 struct lower_desc_info *desc_info = &ctx->desc_info;
1087 uint32_t copy_count;
1088
1089 #if PAN_ARCH < 9
1090 copy_count = desc_info->dyn_ubos.count + desc_info->dyn_ssbos.count;
1091 for (uint32_t i = 0; i < PANVK_BIFROST_DESC_TABLE_COUNT; i++)
1092 copy_count += desc_info->others[i].count;
1093 #else
1094 uint32_t dummy_sampler_idx;
1095 switch (nir->info.stage) {
1096 case MESA_SHADER_VERTEX:
1097 /* Dummy sampler comes after the vertex attributes. */
1098 dummy_sampler_idx = 16;
1099 break;
1100 case MESA_SHADER_FRAGMENT:
1101 /* Dummy sampler comes after the varyings. */
1102 dummy_sampler_idx = desc_info->num_varying_attr_descs;
1103 break;
1104 case MESA_SHADER_COMPUTE:
1105 dummy_sampler_idx = 0;
1106 break;
1107 default:
1108 UNREACHABLE("unexpected stage");
1109 }
1110 desc_info->dummy_sampler_handle = pan_res_handle(0, dummy_sampler_idx);
1111
1112 copy_count = desc_info->dyn_bufs.count;
1113 #endif
1114
1115 if (copy_count == 0)
1116 return;
1117
1118 #if PAN_ARCH < 9
1119 uint32_t *copy_table = rzalloc_array(ctx->ht, uint32_t, copy_count);
1120
1121 assert(copy_table);
1122 desc_info->dyn_ubos.map = copy_table;
1123 copy_table += desc_info->dyn_ubos.count;
1124 desc_info->dyn_ubos.count = 0;
1125 desc_info->dyn_ssbos.map = copy_table;
1126 copy_table += desc_info->dyn_ssbos.count;
1127 desc_info->dyn_ssbos.count = 0;
1128
1129 for (uint32_t i = 0; i < PANVK_BIFROST_DESC_TABLE_COUNT; i++) {
1130 desc_info->others[i].map = copy_table;
1131 copy_table += desc_info->others[i].count;
1132 desc_info->others[i].count = 0;
1133 }
1134 #else
1135 /* Dynamic buffers come after the dummy sampler. */
1136 desc_info->dyn_bufs_start = dummy_sampler_idx + 1;
1137
1138 desc_info->dyn_bufs.map = rzalloc_array(ctx->ht, uint32_t, copy_count);
1139 desc_info->dyn_bufs.count = 0;
1140 assert(desc_info->dyn_bufs.map);
1141 #endif
1142
1143 hash_table_u64_foreach(ctx->ht, he) {
1144 /* We use the upper binding bit to encode the subdesc index. */
1145 uint32_t desc_count = (uintptr_t)he.data;
1146 struct desc_id src = {
1147 .ht_key = he.key,
1148 };
1149
1150 /* Until now, we were just using the hash table to track descriptors
1151 * count, but after that point, it's a <set,binding> -> <table_index>
1152 * map. */
1153 void *new_data = fill_copy_descs_for_binding(
1154 ctx, src.set, src.binding, src.sampler_subdesc, desc_count);
1155 _mesa_hash_table_u64_replace(ctx->ht, &he, new_data);
1156 }
1157 }
1158
1159 /* TODO: Texture instructions support bindless through DTSEL_IMM(63),
1160 * which would save us copies of the texture/sampler descriptors. */
1161 static bool
1162 collect_tex_desc_access(nir_builder *b, nir_tex_instr *tex,
1163 struct lower_desc_ctx *ctx)
1164 {
1165 bool recorded = false;
1166 uint32_t plane = 0;
1167 int sampler_src_idx =
1168 nir_tex_instr_src_index(tex, nir_tex_src_sampler_deref);
1169 if (sampler_src_idx >= 0) {
1170 int plane_src_idx = nir_tex_instr_src_index(tex, nir_tex_src_plane);
1171 if (plane_src_idx >= 0)
1172 plane = nir_src_as_uint(tex->src[plane_src_idx].src);
1173
1174 nir_deref_instr *deref = nir_src_as_deref(tex->src[sampler_src_idx].src);
1175
1176 uint32_t set, binding, index_imm, max_idx;
1177 nir_def *index_ssa;
1178 get_resource_deref_binding(deref, &set, &binding, &index_imm, &index_ssa,
1179 &max_idx);
1180 const struct panvk_descriptor_set_layout *set_layout =
1181 ctx->set_layouts[set];
1182 const struct panvk_descriptor_set_binding_layout *binding_layout =
1183 &set_layout->bindings[binding];
1184 struct panvk_subdesc_info subdesc =
1185 get_sampler_subdesc_info(binding_layout->type, plane);
1186
1187 record_binding(ctx, set, binding, subdesc, max_idx);
1188 recorded = true;
1189 }
1190
1191 int tex_src_idx = nir_tex_instr_src_index(tex, nir_tex_src_texture_deref);
1192 if (tex_src_idx >= 0) {
1193 nir_deref_instr *deref = nir_src_as_deref(tex->src[tex_src_idx].src);
1194
1195 uint32_t set, binding, index_imm, max_idx;
1196 nir_def *index_ssa;
1197 get_resource_deref_binding(deref, &set, &binding, &index_imm, &index_ssa,
1198 &max_idx);
1199 const struct panvk_descriptor_set_layout *set_layout =
1200 ctx->set_layouts[set];
1201 const struct panvk_descriptor_set_binding_layout *binding_layout =
1202 &set_layout->bindings[binding];
1203 struct panvk_subdesc_info subdesc =
1204 get_tex_subdesc_info(binding_layout->type, plane);
1205
1206 record_binding(ctx, set, binding, subdesc, max_idx);
1207 recorded = true;
1208 }
1209
1210 return recorded;
1211 }
1212
1213 static bool
1214 collect_intr_desc_access(nir_builder *b, nir_intrinsic_instr *intrin,
1215 struct lower_desc_ctx *ctx)
1216 {
1217 switch (intrin->intrinsic) {
1218 case nir_intrinsic_vulkan_resource_index: {
1219 unsigned set, binding;
1220
1221 set = nir_intrinsic_desc_set(intrin);
1222 binding = nir_intrinsic_binding(intrin);
1223
1224 /* TODO: walk the reindex chain from load_vulkan_descriptor() to try and
1225 * guess the max index. */
1226 record_binding(ctx, set, binding, NO_SUBDESC, UINT32_MAX);
1227 return true;
1228 }
1229
1230 case nir_intrinsic_image_deref_store:
1231 case nir_intrinsic_image_deref_load:
1232 case nir_intrinsic_image_deref_atomic:
1233 case nir_intrinsic_image_deref_atomic_swap:
1234 case nir_intrinsic_image_deref_size:
1235 case nir_intrinsic_image_deref_samples: {
1236 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
1237 unsigned set, binding, index_imm, max_idx;
1238 nir_def *index_ssa;
1239
1240 get_resource_deref_binding(deref, &set, &binding, &index_imm, &index_ssa,
1241 &max_idx);
1242 record_binding(ctx, set, binding, NO_SUBDESC, max_idx);
1243 return true;
1244 }
1245 default:
1246 return false;
1247 }
1248 }
1249
1250 static bool
1251 collect_instr_desc_access(nir_builder *b, nir_instr *instr, void *data)
1252 {
1253 struct lower_desc_ctx *ctx = data;
1254
1255 switch (instr->type) {
1256 case nir_instr_type_tex:
1257 return collect_tex_desc_access(b, nir_instr_as_tex(instr), ctx);
1258 case nir_instr_type_intrinsic:
1259 return collect_intr_desc_access(b, nir_instr_as_intrinsic(instr), ctx);
1260 default:
1261 return false;
1262 }
1263 }
1264
1265 static void
1266 upload_shader_desc_info(struct panvk_device *dev,
1267 struct panvk_shader_desc_info *desc_info,
1268 const struct lower_desc_info *lower_info)
1269 {
1270 #if PAN_ARCH < 9
1271 unsigned copy_count = 0;
1272 for (unsigned i = 0; i < ARRAY_SIZE(desc_info->others.count); i++) {
1273 desc_info->others.count[i] = lower_info->others[i].count;
1274 copy_count += lower_info->others[i].count;
1275 }
1276
1277 if (copy_count > 0) {
1278 desc_info->others.map = panvk_pool_upload_aligned(
1279 &dev->mempools.rw, lower_info->others[0].map,
1280 copy_count * sizeof(uint32_t), sizeof(uint32_t));
1281 }
1282
1283 assert(lower_info->dyn_ubos.count <=
1284 ARRAY_SIZE(desc_info->dyn_ubos.map));
1285 desc_info->dyn_ubos.count = lower_info->dyn_ubos.count;
1286 memcpy(desc_info->dyn_ubos.map, lower_info->dyn_ubos.map,
1287 lower_info->dyn_ubos.count * sizeof(*desc_info->dyn_ubos.map));
1288 assert(lower_info->dyn_ssbos.count <=
1289 ARRAY_SIZE(desc_info->dyn_ssbos.map));
1290 desc_info->dyn_ssbos.count = lower_info->dyn_ssbos.count;
1291 memcpy(
1292 desc_info->dyn_ssbos.map, lower_info->dyn_ssbos.map,
1293 lower_info->dyn_ssbos.count * sizeof(*desc_info->dyn_ssbos.map));
1294 #else
1295 assert(lower_info->dyn_bufs.count <=
1296 ARRAY_SIZE(desc_info->dyn_bufs.map));
1297 desc_info->dyn_bufs.count = lower_info->dyn_bufs.count;
1298 memcpy(desc_info->dyn_bufs.map, lower_info->dyn_bufs.map,
1299 lower_info->dyn_bufs.count * sizeof(*desc_info->dyn_bufs.map));
1300 #endif
1301
1302 desc_info->used_set_mask = lower_info->used_set_mask;
1303 }
1304
1305 void
1306 panvk_per_arch(nir_lower_descriptors)(
1307 nir_shader *nir, struct panvk_device *dev,
1308 const struct vk_pipeline_robustness_state *rs, uint32_t set_layout_count,
1309 struct vk_descriptor_set_layout *const *set_layouts,
1310 const struct vk_graphics_pipeline_state *state,
1311 struct panvk_shader_desc_info *desc_info)
1312 {
1313 struct lower_desc_ctx ctx = {
1314 .add_bounds_checks =
1315 rs->storage_buffers !=
1316 VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT ||
1317 rs->uniform_buffers !=
1318 VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT ||
1319 rs->images != VK_PIPELINE_ROBUSTNESS_IMAGE_BEHAVIOR_DISABLED_EXT,
1320 .null_descriptor_support = dev->vk.enabled_features.nullDescriptor,
1321 };
1322 bool progress = false;
1323
1324 #if PAN_ARCH < 9
1325 ctx.ubo_addr_format = nir_address_format_32bit_index_offset;
1326 ctx.ssbo_addr_format =
1327 rs->storage_buffers != VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT
1328 ? nir_address_format_64bit_bounded_global
1329 : nir_address_format_64bit_global_32bit_offset;
1330 #else
1331 ctx.ubo_addr_format = nir_address_format_vec2_index_32bit_offset;
1332 ctx.ssbo_addr_format = nir_address_format_vec2_index_32bit_offset;
1333 #endif
1334
1335 ctx.ht = _mesa_hash_table_u64_create(NULL);
1336 assert(ctx.ht);
1337
1338 for (uint32_t i = 0; i < set_layout_count; i++)
1339 ctx.set_layouts[i] = to_panvk_descriptor_set_layout(set_layouts[i]);
1340
1341 NIR_PASS(progress, nir, nir_shader_instructions_pass,
1342 collect_instr_desc_access, nir_metadata_all, &ctx);
1343 if (!progress)
1344 goto out;
1345
1346 #if PAN_ARCH >= 9
1347 ctx.desc_info.num_varying_attr_descs = 0;
1348 /* We require Attribute Descriptors if we cannot use LD_VAR_BUF[_IMM] for
1349 * varyings. */
1350 if (nir->info.stage == MESA_SHADER_FRAGMENT)
1351 ctx.desc_info.num_varying_attr_descs =
1352 desc_info->fs_varying_attr_desc_count;
1353 #endif
1354 create_copy_table(nir, &ctx);
1355 upload_shader_desc_info(dev, desc_info, &ctx.desc_info);
1356
1357 NIR_PASS(progress, nir, nir_shader_instructions_pass,
1358 lower_descriptors_instr, nir_metadata_control_flow, &ctx);
1359
1360 out:
1361 _mesa_hash_table_u64_destroy(ctx.ht);
1362 }