anv: Use bindless handles for images
[mesa.git] / src / intel / vulkan / anv_nir_apply_pipeline_layout.c
1 /*
2 * Copyright © 2015 Intel Corporation
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "anv_nir.h"
25 #include "program/prog_parameter.h"
26 #include "nir/nir_builder.h"
27 #include "compiler/brw_nir.h"
28 #include "util/set.h"
29
30 /* Sampler tables don't actually have a maximum size but we pick one just so
31 * that we don't end up emitting too much state on-the-fly.
32 */
33 #define MAX_SAMPLER_TABLE_SIZE 128
34 #define BINDLESS_OFFSET 255
35
36 struct apply_pipeline_layout_state {
37 const struct anv_physical_device *pdevice;
38
39 nir_shader *shader;
40 nir_builder builder;
41
42 struct anv_pipeline_layout *layout;
43 bool add_bounds_checks;
44
45 /* Place to flag lowered instructions so we don't lower them twice */
46 struct set *lowered_instrs;
47
48 int dynamic_offset_uniform_start;
49
50 bool uses_constants;
51 uint8_t constants_offset;
52 struct {
53 bool desc_buffer_used;
54 uint8_t desc_offset;
55
56 uint8_t *use_count;
57 uint8_t *surface_offsets;
58 uint8_t *sampler_offsets;
59 } set[MAX_SETS];
60 };
61
62 static void
63 add_binding(struct apply_pipeline_layout_state *state,
64 uint32_t set, uint32_t binding)
65 {
66 const struct anv_descriptor_set_binding_layout *bind_layout =
67 &state->layout->set[set].layout->binding[binding];
68
69 if (state->set[set].use_count[binding] < UINT8_MAX)
70 state->set[set].use_count[binding]++;
71
72 /* Only flag the descriptor buffer as used if there's actually data for
73 * this binding. This lets us be lazy and call this function constantly
74 * without worrying about unnecessarily enabling the buffer.
75 */
76 if (anv_descriptor_size(bind_layout))
77 state->set[set].desc_buffer_used = true;
78 }
79
80 static void
81 add_deref_src_binding(struct apply_pipeline_layout_state *state, nir_src src)
82 {
83 nir_deref_instr *deref = nir_src_as_deref(src);
84 nir_variable *var = nir_deref_instr_get_variable(deref);
85 add_binding(state, var->data.descriptor_set, var->data.binding);
86 }
87
88 static void
89 add_tex_src_binding(struct apply_pipeline_layout_state *state,
90 nir_tex_instr *tex, nir_tex_src_type deref_src_type)
91 {
92 int deref_src_idx = nir_tex_instr_src_index(tex, deref_src_type);
93 if (deref_src_idx < 0)
94 return;
95
96 add_deref_src_binding(state, tex->src[deref_src_idx].src);
97 }
98
99 static void
100 get_used_bindings_block(nir_block *block,
101 struct apply_pipeline_layout_state *state)
102 {
103 nir_foreach_instr_safe(instr, block) {
104 switch (instr->type) {
105 case nir_instr_type_intrinsic: {
106 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
107 switch (intrin->intrinsic) {
108 case nir_intrinsic_vulkan_resource_index:
109 add_binding(state, nir_intrinsic_desc_set(intrin),
110 nir_intrinsic_binding(intrin));
111 break;
112
113 case nir_intrinsic_image_deref_load:
114 case nir_intrinsic_image_deref_store:
115 case nir_intrinsic_image_deref_atomic_add:
116 case nir_intrinsic_image_deref_atomic_min:
117 case nir_intrinsic_image_deref_atomic_max:
118 case nir_intrinsic_image_deref_atomic_and:
119 case nir_intrinsic_image_deref_atomic_or:
120 case nir_intrinsic_image_deref_atomic_xor:
121 case nir_intrinsic_image_deref_atomic_exchange:
122 case nir_intrinsic_image_deref_atomic_comp_swap:
123 case nir_intrinsic_image_deref_size:
124 case nir_intrinsic_image_deref_samples:
125 case nir_intrinsic_image_deref_load_param_intel:
126 case nir_intrinsic_image_deref_load_raw_intel:
127 case nir_intrinsic_image_deref_store_raw_intel:
128 add_deref_src_binding(state, intrin->src[0]);
129 break;
130
131 case nir_intrinsic_load_constant:
132 state->uses_constants = true;
133 break;
134
135 default:
136 break;
137 }
138 break;
139 }
140 case nir_instr_type_tex: {
141 nir_tex_instr *tex = nir_instr_as_tex(instr);
142 add_tex_src_binding(state, tex, nir_tex_src_texture_deref);
143 add_tex_src_binding(state, tex, nir_tex_src_sampler_deref);
144 break;
145 }
146 default:
147 continue;
148 }
149 }
150 }
151
152 static bool
153 find_descriptor_for_index_src(nir_src src,
154 struct apply_pipeline_layout_state *state)
155 {
156 nir_intrinsic_instr *intrin = nir_src_as_intrinsic(src);
157
158 while (intrin && intrin->intrinsic == nir_intrinsic_vulkan_resource_reindex)
159 intrin = nir_src_as_intrinsic(intrin->src[0]);
160
161 if (!intrin || intrin->intrinsic != nir_intrinsic_vulkan_resource_index)
162 return false;
163
164 uint32_t set = nir_intrinsic_desc_set(intrin);
165 uint32_t binding = nir_intrinsic_binding(intrin);
166 uint32_t surface_index = state->set[set].surface_offsets[binding];
167
168 /* Only lower to a BTI message if we have a valid binding table index. */
169 return surface_index < MAX_BINDING_TABLE_SIZE;
170 }
171
172 static bool
173 nir_deref_find_descriptor(nir_deref_instr *deref,
174 struct apply_pipeline_layout_state *state)
175 {
176 while (1) {
177 /* Nothing we will use this on has a variable */
178 assert(deref->deref_type != nir_deref_type_var);
179
180 nir_deref_instr *parent = nir_src_as_deref(deref->parent);
181 if (!parent)
182 break;
183
184 deref = parent;
185 }
186 assert(deref->deref_type == nir_deref_type_cast);
187
188 nir_intrinsic_instr *intrin = nir_src_as_intrinsic(deref->parent);
189 if (!intrin || intrin->intrinsic != nir_intrinsic_load_vulkan_descriptor)
190 return false;
191
192 return find_descriptor_for_index_src(intrin->src[0], state);
193 }
194
195 static nir_ssa_def *
196 build_index_for_res_reindex(nir_intrinsic_instr *intrin,
197 struct apply_pipeline_layout_state *state)
198 {
199 nir_builder *b = &state->builder;
200
201 if (intrin->intrinsic == nir_intrinsic_vulkan_resource_reindex) {
202 nir_ssa_def *bti =
203 build_index_for_res_reindex(nir_src_as_intrinsic(intrin->src[0]), state);
204
205 b->cursor = nir_before_instr(&intrin->instr);
206 return nir_iadd(b, bti, nir_ssa_for_src(b, intrin->src[1], 1));
207 }
208
209 assert(intrin->intrinsic == nir_intrinsic_vulkan_resource_index);
210
211 uint32_t set = nir_intrinsic_desc_set(intrin);
212 uint32_t binding = nir_intrinsic_binding(intrin);
213
214 const struct anv_descriptor_set_binding_layout *bind_layout =
215 &state->layout->set[set].layout->binding[binding];
216
217 uint32_t surface_index = state->set[set].surface_offsets[binding];
218 uint32_t array_size = bind_layout->array_size;
219
220 b->cursor = nir_before_instr(&intrin->instr);
221
222 nir_ssa_def *array_index = nir_ssa_for_src(b, intrin->src[0], 1);
223 if (nir_src_is_const(intrin->src[0]) || state->add_bounds_checks)
224 array_index = nir_umin(b, array_index, nir_imm_int(b, array_size - 1));
225
226 return nir_iadd_imm(b, array_index, surface_index);
227 }
228
229 static nir_ssa_def *
230 build_index_offset_for_deref(nir_deref_instr *deref,
231 struct apply_pipeline_layout_state *state)
232 {
233 nir_builder *b = &state->builder;
234
235 nir_deref_instr *parent = nir_deref_instr_parent(deref);
236 if (parent) {
237 nir_ssa_def *addr = build_index_offset_for_deref(parent, state);
238
239 b->cursor = nir_before_instr(&deref->instr);
240 return nir_explicit_io_address_from_deref(b, deref, addr,
241 nir_address_format_32bit_index_offset);
242 }
243
244 nir_intrinsic_instr *load_desc = nir_src_as_intrinsic(deref->parent);
245 assert(load_desc->intrinsic == nir_intrinsic_load_vulkan_descriptor);
246
247 nir_ssa_def *index =
248 build_index_for_res_reindex(nir_src_as_intrinsic(load_desc->src[0]), state);
249
250 /* Return a 0 offset which will get picked up by the recursion */
251 b->cursor = nir_before_instr(&deref->instr);
252 return nir_vec2(b, index, nir_imm_int(b, 0));
253 }
254
255 static bool
256 try_lower_direct_buffer_intrinsic(nir_intrinsic_instr *intrin, bool is_atomic,
257 struct apply_pipeline_layout_state *state)
258 {
259 nir_builder *b = &state->builder;
260
261 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
262 if (deref->mode != nir_var_mem_ssbo)
263 return false;
264
265 /* 64-bit atomics only support A64 messages so we can't lower them to the
266 * index+offset model.
267 */
268 if (is_atomic && nir_dest_bit_size(intrin->dest) == 64)
269 return false;
270
271 if (!nir_deref_find_descriptor(deref, state))
272 return false;
273
274 nir_ssa_def *addr = build_index_offset_for_deref(deref, state);
275
276 b->cursor = nir_before_instr(&intrin->instr);
277 nir_lower_explicit_io_instr(b, intrin, addr,
278 nir_address_format_32bit_index_offset);
279 return true;
280 }
281
282 static void
283 lower_direct_buffer_access(nir_function_impl *impl,
284 struct apply_pipeline_layout_state *state)
285 {
286 nir_foreach_block(block, impl) {
287 nir_foreach_instr_safe(instr, block) {
288 if (instr->type != nir_instr_type_intrinsic)
289 continue;
290
291 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
292 switch (intrin->intrinsic) {
293 case nir_intrinsic_load_deref:
294 case nir_intrinsic_store_deref:
295 try_lower_direct_buffer_intrinsic(intrin, false, state);
296 break;
297 case nir_intrinsic_deref_atomic_add:
298 case nir_intrinsic_deref_atomic_imin:
299 case nir_intrinsic_deref_atomic_umin:
300 case nir_intrinsic_deref_atomic_imax:
301 case nir_intrinsic_deref_atomic_umax:
302 case nir_intrinsic_deref_atomic_and:
303 case nir_intrinsic_deref_atomic_or:
304 case nir_intrinsic_deref_atomic_xor:
305 case nir_intrinsic_deref_atomic_exchange:
306 case nir_intrinsic_deref_atomic_comp_swap:
307 case nir_intrinsic_deref_atomic_fmin:
308 case nir_intrinsic_deref_atomic_fmax:
309 case nir_intrinsic_deref_atomic_fcomp_swap:
310 try_lower_direct_buffer_intrinsic(intrin, true, state);
311 break;
312
313 case nir_intrinsic_get_buffer_size: {
314 /* The get_buffer_size intrinsic always just takes a
315 * index/reindex intrinsic.
316 */
317 if (!find_descriptor_for_index_src(intrin->src[0], state))
318 break;
319
320 nir_ssa_def *index =
321 build_index_for_res_reindex(nir_src_as_intrinsic(intrin->src[0]),
322 state);
323 nir_instr_rewrite_src(&intrin->instr, &intrin->src[0],
324 nir_src_for_ssa(index));
325 _mesa_set_add(state->lowered_instrs, intrin);
326 }
327
328 default:
329 break;
330 }
331 }
332 }
333 }
334
335 static void
336 lower_res_index_intrinsic(nir_intrinsic_instr *intrin,
337 struct apply_pipeline_layout_state *state)
338 {
339 nir_builder *b = &state->builder;
340
341 b->cursor = nir_before_instr(&intrin->instr);
342
343 uint32_t set = nir_intrinsic_desc_set(intrin);
344 uint32_t binding = nir_intrinsic_binding(intrin);
345 const VkDescriptorType desc_type = nir_intrinsic_desc_type(intrin);
346
347 const struct anv_descriptor_set_binding_layout *bind_layout =
348 &state->layout->set[set].layout->binding[binding];
349
350 uint32_t surface_index = state->set[set].surface_offsets[binding];
351 uint32_t array_size = bind_layout->array_size;
352
353 nir_ssa_def *array_index = nir_ssa_for_src(b, intrin->src[0], 1);
354 if (nir_src_is_const(intrin->src[0]) || state->add_bounds_checks)
355 array_index = nir_umin(b, array_index, nir_imm_int(b, array_size - 1));
356
357 nir_ssa_def *index;
358 if (state->pdevice->has_a64_buffer_access &&
359 (desc_type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER ||
360 desc_type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
361 /* We store the descriptor offset as 16.8.8 where the top 16 bits are
362 * the offset into the descriptor set, the next 8 are the binding table
363 * index of the descriptor buffer, and the bottom 8 bits are the offset
364 * (in bytes) into the dynamic offset table.
365 */
366 assert(bind_layout->dynamic_offset_index < MAX_DYNAMIC_BUFFERS);
367 uint32_t dynamic_offset_index = 0xff; /* No dynamic offset */
368 if (bind_layout->dynamic_offset_index >= 0) {
369 dynamic_offset_index =
370 state->layout->set[set].dynamic_offset_start +
371 bind_layout->dynamic_offset_index;
372 }
373
374 const uint32_t desc_offset =
375 bind_layout->descriptor_offset << 16 |
376 (uint32_t)state->set[set].desc_offset << 8 |
377 dynamic_offset_index;
378
379 if (state->add_bounds_checks) {
380 /* We're using nir_address_format_64bit_bounded_global */
381 assert(intrin->dest.ssa.num_components == 4);
382 assert(intrin->dest.ssa.bit_size == 32);
383 index = nir_vec4(b, nir_imm_int(b, desc_offset),
384 nir_ssa_for_src(b, intrin->src[0], 1),
385 nir_imm_int(b, array_size - 1),
386 nir_ssa_undef(b, 1, 32));
387 } else {
388 /* We're using nir_address_format_64bit_global */
389 assert(intrin->dest.ssa.num_components == 1);
390 assert(intrin->dest.ssa.bit_size == 64);
391 index = nir_pack_64_2x32_split(b, nir_imm_int(b, desc_offset),
392 nir_ssa_for_src(b, intrin->src[0], 1));
393 }
394 } else if (bind_layout->data & ANV_DESCRIPTOR_INLINE_UNIFORM) {
395 /* This is an inline uniform block. Just reference the descriptor set
396 * and use the descriptor offset as the base. Inline uniforms always
397 * use nir_address_format_32bit_index_offset
398 */
399 assert(intrin->dest.ssa.num_components == 2);
400 assert(intrin->dest.ssa.bit_size == 32);
401 index = nir_imm_ivec2(b, state->set[set].desc_offset,
402 bind_layout->descriptor_offset);
403 } else {
404 /* We're using nir_address_format_32bit_index_offset */
405 assert(intrin->dest.ssa.num_components == 2);
406 assert(intrin->dest.ssa.bit_size == 32);
407 index = nir_vec2(b, nir_iadd_imm(b, array_index, surface_index),
408 nir_imm_int(b, 0));
409 }
410
411 assert(intrin->dest.is_ssa);
412 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, nir_src_for_ssa(index));
413 nir_instr_remove(&intrin->instr);
414 }
415
416 static void
417 lower_res_reindex_intrinsic(nir_intrinsic_instr *intrin,
418 struct apply_pipeline_layout_state *state)
419 {
420 nir_builder *b = &state->builder;
421
422 b->cursor = nir_before_instr(&intrin->instr);
423
424 const VkDescriptorType desc_type = nir_intrinsic_desc_type(intrin);
425
426 /* For us, the resource indices are just indices into the binding table and
427 * array elements are sequential. A resource_reindex just turns into an
428 * add of the two indices.
429 */
430 assert(intrin->src[0].is_ssa && intrin->src[1].is_ssa);
431 nir_ssa_def *old_index = intrin->src[0].ssa;
432 nir_ssa_def *offset = intrin->src[1].ssa;
433
434 nir_ssa_def *new_index;
435 if (state->pdevice->has_a64_buffer_access &&
436 (desc_type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER ||
437 desc_type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
438 if (state->add_bounds_checks) {
439 /* We're using nir_address_format_64bit_bounded_global */
440 assert(intrin->dest.ssa.num_components == 4);
441 assert(intrin->dest.ssa.bit_size == 32);
442 new_index = nir_vec4(b, nir_channel(b, old_index, 0),
443 nir_iadd(b, nir_channel(b, old_index, 1),
444 offset),
445 nir_channel(b, old_index, 2),
446 nir_ssa_undef(b, 1, 32));
447 } else {
448 /* We're using nir_address_format_64bit_global */
449 assert(intrin->dest.ssa.num_components == 1);
450 assert(intrin->dest.ssa.bit_size == 64);
451 nir_ssa_def *base = nir_unpack_64_2x32_split_x(b, old_index);
452 nir_ssa_def *arr_idx = nir_unpack_64_2x32_split_y(b, old_index);
453 new_index = nir_pack_64_2x32_split(b, base, nir_iadd(b, arr_idx, offset));
454 }
455 } else {
456 /* We're using nir_address_format_32bit_index_offset */
457 assert(intrin->dest.ssa.num_components == 2);
458 assert(intrin->dest.ssa.bit_size == 32);
459 new_index = nir_vec2(b, nir_iadd(b, nir_channel(b, old_index, 0), offset),
460 nir_channel(b, old_index, 1));
461 }
462
463 assert(intrin->dest.is_ssa);
464 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, nir_src_for_ssa(new_index));
465 nir_instr_remove(&intrin->instr);
466 }
467
468 static nir_ssa_def *
469 build_ssbo_descriptor_load(const VkDescriptorType desc_type,
470 nir_ssa_def *index,
471 struct apply_pipeline_layout_state *state)
472 {
473 nir_builder *b = &state->builder;
474
475 nir_ssa_def *desc_offset, *array_index;
476 if (state->add_bounds_checks) {
477 /* We're using nir_address_format_64bit_bounded_global */
478 desc_offset = nir_channel(b, index, 0);
479 array_index = nir_umin(b, nir_channel(b, index, 1),
480 nir_channel(b, index, 2));
481 } else {
482 desc_offset = nir_unpack_64_2x32_split_x(b, index);
483 array_index = nir_unpack_64_2x32_split_y(b, index);
484 }
485
486 /* The desc_offset is actually 16.8.8 */
487 nir_ssa_def *desc_buffer_index =
488 nir_extract_u8(b, desc_offset, nir_imm_int(b, 1));
489 nir_ssa_def *desc_offset_base =
490 nir_extract_u16(b, desc_offset, nir_imm_int(b, 1));
491
492 /* Compute the actual descriptor offset */
493 const unsigned descriptor_size =
494 anv_descriptor_type_size(state->pdevice, desc_type);
495 desc_offset = nir_iadd(b, desc_offset_base,
496 nir_imul_imm(b, array_index, descriptor_size));
497
498 nir_intrinsic_instr *desc_load =
499 nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_ubo);
500 desc_load->src[0] = nir_src_for_ssa(desc_buffer_index);
501 desc_load->src[1] = nir_src_for_ssa(desc_offset);
502 desc_load->num_components = 4;
503 nir_ssa_dest_init(&desc_load->instr, &desc_load->dest, 4, 32, NULL);
504 nir_builder_instr_insert(b, &desc_load->instr);
505
506 return &desc_load->dest.ssa;
507 }
508
509 static void
510 lower_load_vulkan_descriptor(nir_intrinsic_instr *intrin,
511 struct apply_pipeline_layout_state *state)
512 {
513 nir_builder *b = &state->builder;
514
515 b->cursor = nir_before_instr(&intrin->instr);
516
517 const VkDescriptorType desc_type = nir_intrinsic_desc_type(intrin);
518
519 assert(intrin->src[0].is_ssa);
520 nir_ssa_def *index = intrin->src[0].ssa;
521
522 nir_ssa_def *desc;
523 if (state->pdevice->has_a64_buffer_access &&
524 (desc_type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER ||
525 desc_type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
526 desc = build_ssbo_descriptor_load(desc_type, index, state);
527
528 /* We want nir_address_format_64bit_global */
529 if (!state->add_bounds_checks)
530 desc = nir_pack_64_2x32(b, nir_channels(b, desc, 0x3));
531
532 if (state->dynamic_offset_uniform_start >= 0) {
533 /* This shader has dynamic offsets and we have no way of knowing
534 * (save from the dynamic offset base index) if this buffer has a
535 * dynamic offset.
536 */
537 nir_ssa_def *desc_offset, *array_index;
538 if (state->add_bounds_checks) {
539 /* We're using nir_address_format_64bit_bounded_global */
540 desc_offset = nir_channel(b, index, 0);
541 array_index = nir_umin(b, nir_channel(b, index, 1),
542 nir_channel(b, index, 2));
543 } else {
544 desc_offset = nir_unpack_64_2x32_split_x(b, index);
545 array_index = nir_unpack_64_2x32_split_y(b, index);
546 }
547
548 nir_ssa_def *dyn_offset_base =
549 nir_extract_u8(b, desc_offset, nir_imm_int(b, 0));
550 nir_ssa_def *dyn_offset_idx =
551 nir_iadd(b, dyn_offset_base, array_index);
552 if (state->add_bounds_checks) {
553 dyn_offset_idx = nir_umin(b, dyn_offset_idx,
554 nir_imm_int(b, MAX_DYNAMIC_BUFFERS));
555 }
556
557 nir_intrinsic_instr *dyn_load =
558 nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_uniform);
559 nir_intrinsic_set_base(dyn_load, state->dynamic_offset_uniform_start);
560 nir_intrinsic_set_range(dyn_load, MAX_DYNAMIC_BUFFERS * 4);
561 dyn_load->src[0] = nir_src_for_ssa(nir_imul_imm(b, dyn_offset_idx, 4));
562 dyn_load->num_components = 1;
563 nir_ssa_dest_init(&dyn_load->instr, &dyn_load->dest, 1, 32, NULL);
564 nir_builder_instr_insert(b, &dyn_load->instr);
565
566 nir_ssa_def *dynamic_offset =
567 nir_bcsel(b, nir_ieq(b, dyn_offset_base, nir_imm_int(b, 0xff)),
568 nir_imm_int(b, 0), &dyn_load->dest.ssa);
569
570 if (state->add_bounds_checks) {
571 /* The dynamic offset gets added to the base pointer so that we
572 * have a sliding window range.
573 *
574 * We're using nir_address_format_64bit_bounded_global.
575 */
576 nir_ssa_def *base_ptr =
577 nir_pack_64_2x32(b, nir_channels(b, desc, 0x3));
578 base_ptr = nir_iadd(b, base_ptr, nir_u2u64(b, dynamic_offset));
579 desc = nir_vec4(b, nir_unpack_64_2x32_split_x(b, base_ptr),
580 nir_unpack_64_2x32_split_y(b, base_ptr),
581 nir_channel(b, desc, 2),
582 nir_channel(b, desc, 3));
583 } else {
584 /* We're using nir_address_format_64bit_global */
585 desc = nir_iadd(b, desc, nir_u2u64(b, dynamic_offset));
586 }
587 }
588 } else {
589 /* We follow the nir_address_format_32bit_index_offset model */
590 desc = index;
591 }
592
593 assert(intrin->dest.is_ssa);
594 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, nir_src_for_ssa(desc));
595 nir_instr_remove(&intrin->instr);
596 }
597
598 static void
599 lower_get_buffer_size(nir_intrinsic_instr *intrin,
600 struct apply_pipeline_layout_state *state)
601 {
602 if (_mesa_set_search(state->lowered_instrs, intrin))
603 return;
604
605 nir_builder *b = &state->builder;
606
607 b->cursor = nir_before_instr(&intrin->instr);
608
609 const VkDescriptorType desc_type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
610
611 assert(intrin->src[0].is_ssa);
612 nir_ssa_def *index = intrin->src[0].ssa;
613
614 if (state->pdevice->has_a64_buffer_access) {
615 nir_ssa_def *desc = build_ssbo_descriptor_load(desc_type, index, state);
616 nir_ssa_def *size = nir_channel(b, desc, 2);
617 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, nir_src_for_ssa(size));
618 nir_instr_remove(&intrin->instr);
619 } else {
620 /* We're following the nir_address_format_32bit_index_offset model so
621 * the binding table index is the first component of the address. The
622 * back-end wants a scalar binding table index source.
623 */
624 nir_instr_rewrite_src(&intrin->instr, &intrin->src[0],
625 nir_src_for_ssa(nir_channel(b, index, 0)));
626 }
627 }
628
629 static nir_ssa_def *
630 build_descriptor_load(nir_deref_instr *deref, unsigned offset,
631 unsigned num_components, unsigned bit_size,
632 struct apply_pipeline_layout_state *state)
633 {
634 nir_variable *var = nir_deref_instr_get_variable(deref);
635
636 unsigned set = var->data.descriptor_set;
637 unsigned binding = var->data.binding;
638 unsigned array_size =
639 state->layout->set[set].layout->binding[binding].array_size;
640
641 const struct anv_descriptor_set_binding_layout *bind_layout =
642 &state->layout->set[set].layout->binding[binding];
643
644 nir_builder *b = &state->builder;
645
646 nir_ssa_def *desc_buffer_index =
647 nir_imm_int(b, state->set[set].desc_offset);
648
649 nir_ssa_def *desc_offset =
650 nir_imm_int(b, bind_layout->descriptor_offset + offset);
651 if (deref->deref_type != nir_deref_type_var) {
652 assert(deref->deref_type == nir_deref_type_array);
653
654 const unsigned descriptor_size = anv_descriptor_size(bind_layout);
655 nir_ssa_def *arr_index = nir_ssa_for_src(b, deref->arr.index, 1);
656 if (state->add_bounds_checks)
657 arr_index = nir_umin(b, arr_index, nir_imm_int(b, array_size - 1));
658
659 desc_offset = nir_iadd(b, desc_offset,
660 nir_imul_imm(b, arr_index, descriptor_size));
661 }
662
663 nir_intrinsic_instr *desc_load =
664 nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_ubo);
665 desc_load->src[0] = nir_src_for_ssa(desc_buffer_index);
666 desc_load->src[1] = nir_src_for_ssa(desc_offset);
667 desc_load->num_components = num_components;
668 nir_ssa_dest_init(&desc_load->instr, &desc_load->dest,
669 num_components, bit_size, NULL);
670 nir_builder_instr_insert(b, &desc_load->instr);
671
672 return &desc_load->dest.ssa;
673 }
674
675 static void
676 lower_image_intrinsic(nir_intrinsic_instr *intrin,
677 struct apply_pipeline_layout_state *state)
678 {
679 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
680 nir_variable *var = nir_deref_instr_get_variable(deref);
681
682 nir_builder *b = &state->builder;
683 b->cursor = nir_before_instr(&intrin->instr);
684
685 const bool use_bindless = state->pdevice->has_bindless_images;
686
687 if (intrin->intrinsic == nir_intrinsic_image_deref_load_param_intel) {
688 b->cursor = nir_instr_remove(&intrin->instr);
689
690 assert(!use_bindless); /* Otherwise our offsets would be wrong */
691 const unsigned param = nir_intrinsic_base(intrin);
692
693 nir_ssa_def *desc =
694 build_descriptor_load(deref, param * 16,
695 intrin->dest.ssa.num_components,
696 intrin->dest.ssa.bit_size, state);
697
698 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, nir_src_for_ssa(desc));
699 } else if (use_bindless) {
700 const bool write_only =
701 (var->data.image.access & ACCESS_NON_READABLE) != 0;
702 nir_ssa_def *desc =
703 build_descriptor_load(deref, 0, 2, 32, state);
704 nir_ssa_def *handle = nir_channel(b, desc, write_only ? 1 : 0);
705 nir_rewrite_image_intrinsic(intrin, handle, true);
706 } else {
707 unsigned set = var->data.descriptor_set;
708 unsigned binding = var->data.binding;
709 unsigned binding_offset = state->set[set].surface_offsets[binding];
710 unsigned array_size =
711 state->layout->set[set].layout->binding[binding].array_size;
712
713 nir_ssa_def *index = NULL;
714 if (deref->deref_type != nir_deref_type_var) {
715 assert(deref->deref_type == nir_deref_type_array);
716 index = nir_ssa_for_src(b, deref->arr.index, 1);
717 if (state->add_bounds_checks)
718 index = nir_umin(b, index, nir_imm_int(b, array_size - 1));
719 } else {
720 index = nir_imm_int(b, 0);
721 }
722
723 index = nir_iadd_imm(b, index, binding_offset);
724 nir_rewrite_image_intrinsic(intrin, index, false);
725 }
726 }
727
728 static void
729 lower_load_constant(nir_intrinsic_instr *intrin,
730 struct apply_pipeline_layout_state *state)
731 {
732 nir_builder *b = &state->builder;
733
734 b->cursor = nir_before_instr(&intrin->instr);
735
736 nir_ssa_def *index = nir_imm_int(b, state->constants_offset);
737 nir_ssa_def *offset = nir_iadd(b, nir_ssa_for_src(b, intrin->src[0], 1),
738 nir_imm_int(b, nir_intrinsic_base(intrin)));
739
740 nir_intrinsic_instr *load_ubo =
741 nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_ubo);
742 load_ubo->num_components = intrin->num_components;
743 load_ubo->src[0] = nir_src_for_ssa(index);
744 load_ubo->src[1] = nir_src_for_ssa(offset);
745 nir_ssa_dest_init(&load_ubo->instr, &load_ubo->dest,
746 intrin->dest.ssa.num_components,
747 intrin->dest.ssa.bit_size, NULL);
748 nir_builder_instr_insert(b, &load_ubo->instr);
749
750 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
751 nir_src_for_ssa(&load_ubo->dest.ssa));
752 nir_instr_remove(&intrin->instr);
753 }
754
755 static void
756 lower_tex_deref(nir_tex_instr *tex, nir_tex_src_type deref_src_type,
757 unsigned *base_index, unsigned plane,
758 struct apply_pipeline_layout_state *state)
759 {
760 int deref_src_idx = nir_tex_instr_src_index(tex, deref_src_type);
761 if (deref_src_idx < 0)
762 return;
763
764 nir_deref_instr *deref = nir_src_as_deref(tex->src[deref_src_idx].src);
765 nir_variable *var = nir_deref_instr_get_variable(deref);
766
767 unsigned set = var->data.descriptor_set;
768 unsigned binding = var->data.binding;
769 unsigned array_size =
770 state->layout->set[set].layout->binding[binding].array_size;
771
772 unsigned binding_offset;
773 if (deref_src_type == nir_tex_src_texture_deref) {
774 binding_offset = state->set[set].surface_offsets[binding];
775 } else {
776 assert(deref_src_type == nir_tex_src_sampler_deref);
777 binding_offset = state->set[set].sampler_offsets[binding];
778 }
779
780 nir_builder *b = &state->builder;
781
782 nir_tex_src_type offset_src_type;
783 nir_ssa_def *index = NULL;
784 if (binding_offset > MAX_BINDING_TABLE_SIZE) {
785 const unsigned plane_offset =
786 plane * sizeof(struct anv_sampled_image_descriptor);
787
788 nir_ssa_def *desc =
789 build_descriptor_load(deref, plane_offset, 2, 32, state);
790
791 if (deref_src_type == nir_tex_src_texture_deref) {
792 offset_src_type = nir_tex_src_texture_handle;
793 index = nir_channel(b, desc, 0);
794 } else {
795 assert(deref_src_type == nir_tex_src_sampler_deref);
796 offset_src_type = nir_tex_src_sampler_handle;
797 index = nir_channel(b, desc, 1);
798 }
799 } else {
800 if (deref_src_type == nir_tex_src_texture_deref) {
801 offset_src_type = nir_tex_src_texture_offset;
802 } else {
803 assert(deref_src_type == nir_tex_src_sampler_deref);
804 offset_src_type = nir_tex_src_sampler_offset;
805 }
806
807 *base_index = binding_offset + plane;
808
809 if (deref->deref_type != nir_deref_type_var) {
810 assert(deref->deref_type == nir_deref_type_array);
811
812 if (nir_src_is_const(deref->arr.index)) {
813 unsigned arr_index = nir_src_as_uint(deref->arr.index);
814 *base_index += MIN2(arr_index, array_size - 1);
815 } else {
816 /* From VK_KHR_sampler_ycbcr_conversion:
817 *
818 * If sampler Y’CBCR conversion is enabled, the combined image
819 * sampler must be indexed only by constant integral expressions
820 * when aggregated into arrays in shader code, irrespective of
821 * the shaderSampledImageArrayDynamicIndexing feature.
822 */
823 assert(nir_tex_instr_src_index(tex, nir_tex_src_plane) == -1);
824
825 index = nir_ssa_for_src(b, deref->arr.index, 1);
826
827 if (state->add_bounds_checks)
828 index = nir_umin(b, index, nir_imm_int(b, array_size - 1));
829 }
830 }
831 }
832
833 if (index) {
834 nir_instr_rewrite_src(&tex->instr, &tex->src[deref_src_idx].src,
835 nir_src_for_ssa(index));
836 tex->src[deref_src_idx].src_type = offset_src_type;
837 } else {
838 nir_tex_instr_remove_src(tex, deref_src_idx);
839 }
840 }
841
842 static uint32_t
843 tex_instr_get_and_remove_plane_src(nir_tex_instr *tex)
844 {
845 int plane_src_idx = nir_tex_instr_src_index(tex, nir_tex_src_plane);
846 if (plane_src_idx < 0)
847 return 0;
848
849 unsigned plane = nir_src_as_uint(tex->src[plane_src_idx].src);
850
851 nir_tex_instr_remove_src(tex, plane_src_idx);
852
853 return plane;
854 }
855
856 static void
857 lower_tex(nir_tex_instr *tex, struct apply_pipeline_layout_state *state)
858 {
859 state->builder.cursor = nir_before_instr(&tex->instr);
860
861 unsigned plane = tex_instr_get_and_remove_plane_src(tex);
862
863 lower_tex_deref(tex, nir_tex_src_texture_deref,
864 &tex->texture_index, plane, state);
865
866 lower_tex_deref(tex, nir_tex_src_sampler_deref,
867 &tex->sampler_index, plane, state);
868
869 /* The backend only ever uses this to mark used surfaces. We don't care
870 * about that little optimization so it just needs to be non-zero.
871 */
872 tex->texture_array_size = 1;
873 }
874
875 static void
876 apply_pipeline_layout_block(nir_block *block,
877 struct apply_pipeline_layout_state *state)
878 {
879 nir_foreach_instr_safe(instr, block) {
880 switch (instr->type) {
881 case nir_instr_type_intrinsic: {
882 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
883 switch (intrin->intrinsic) {
884 case nir_intrinsic_vulkan_resource_index:
885 lower_res_index_intrinsic(intrin, state);
886 break;
887 case nir_intrinsic_vulkan_resource_reindex:
888 lower_res_reindex_intrinsic(intrin, state);
889 break;
890 case nir_intrinsic_load_vulkan_descriptor:
891 lower_load_vulkan_descriptor(intrin, state);
892 break;
893 case nir_intrinsic_get_buffer_size:
894 lower_get_buffer_size(intrin, state);
895 break;
896 case nir_intrinsic_image_deref_load:
897 case nir_intrinsic_image_deref_store:
898 case nir_intrinsic_image_deref_atomic_add:
899 case nir_intrinsic_image_deref_atomic_min:
900 case nir_intrinsic_image_deref_atomic_max:
901 case nir_intrinsic_image_deref_atomic_and:
902 case nir_intrinsic_image_deref_atomic_or:
903 case nir_intrinsic_image_deref_atomic_xor:
904 case nir_intrinsic_image_deref_atomic_exchange:
905 case nir_intrinsic_image_deref_atomic_comp_swap:
906 case nir_intrinsic_image_deref_size:
907 case nir_intrinsic_image_deref_samples:
908 case nir_intrinsic_image_deref_load_param_intel:
909 case nir_intrinsic_image_deref_load_raw_intel:
910 case nir_intrinsic_image_deref_store_raw_intel:
911 lower_image_intrinsic(intrin, state);
912 break;
913 case nir_intrinsic_load_constant:
914 lower_load_constant(intrin, state);
915 break;
916 default:
917 break;
918 }
919 break;
920 }
921 case nir_instr_type_tex:
922 lower_tex(nir_instr_as_tex(instr), state);
923 break;
924 default:
925 continue;
926 }
927 }
928 }
929
930 struct binding_info {
931 uint32_t binding;
932 uint8_t set;
933 uint16_t score;
934 };
935
936 static int
937 compare_binding_infos(const void *_a, const void *_b)
938 {
939 const struct binding_info *a = _a, *b = _b;
940 if (a->score != b->score)
941 return b->score - a->score;
942
943 if (a->set != b->set)
944 return a->set - b->set;
945
946 return a->binding - b->binding;
947 }
948
949 void
950 anv_nir_apply_pipeline_layout(const struct anv_physical_device *pdevice,
951 bool robust_buffer_access,
952 struct anv_pipeline_layout *layout,
953 nir_shader *shader,
954 struct brw_stage_prog_data *prog_data,
955 struct anv_pipeline_bind_map *map)
956 {
957 void *mem_ctx = ralloc_context(NULL);
958
959 struct apply_pipeline_layout_state state = {
960 .pdevice = pdevice,
961 .shader = shader,
962 .layout = layout,
963 .add_bounds_checks = robust_buffer_access,
964 .lowered_instrs = _mesa_pointer_set_create(mem_ctx),
965 .dynamic_offset_uniform_start = -1,
966 };
967
968 for (unsigned s = 0; s < layout->num_sets; s++) {
969 const unsigned count = layout->set[s].layout->binding_count;
970 state.set[s].use_count = rzalloc_array(mem_ctx, uint8_t, count);
971 state.set[s].surface_offsets = rzalloc_array(mem_ctx, uint8_t, count);
972 state.set[s].sampler_offsets = rzalloc_array(mem_ctx, uint8_t, count);
973 }
974
975 nir_foreach_function(function, shader) {
976 if (!function->impl)
977 continue;
978
979 nir_foreach_block(block, function->impl)
980 get_used_bindings_block(block, &state);
981 }
982
983 for (unsigned s = 0; s < layout->num_sets; s++) {
984 if (state.set[s].desc_buffer_used) {
985 map->surface_to_descriptor[map->surface_count] =
986 (struct anv_pipeline_binding) {
987 .set = ANV_DESCRIPTOR_SET_DESCRIPTORS,
988 .binding = s,
989 };
990 state.set[s].desc_offset = map->surface_count;
991 map->surface_count++;
992 }
993 }
994
995 if (state.uses_constants) {
996 state.constants_offset = map->surface_count;
997 map->surface_to_descriptor[map->surface_count].set =
998 ANV_DESCRIPTOR_SET_SHADER_CONSTANTS;
999 map->surface_count++;
1000 }
1001
1002 unsigned used_binding_count = 0;
1003 for (uint32_t set = 0; set < layout->num_sets; set++) {
1004 struct anv_descriptor_set_layout *set_layout = layout->set[set].layout;
1005 for (unsigned b = 0; b < set_layout->binding_count; b++) {
1006 if (state.set[set].use_count[b] == 0)
1007 continue;
1008
1009 used_binding_count++;
1010 }
1011 }
1012
1013 struct binding_info *infos =
1014 rzalloc_array(mem_ctx, struct binding_info, used_binding_count);
1015 used_binding_count = 0;
1016 for (uint32_t set = 0; set < layout->num_sets; set++) {
1017 struct anv_descriptor_set_layout *set_layout = layout->set[set].layout;
1018 for (unsigned b = 0; b < set_layout->binding_count; b++) {
1019 if (state.set[set].use_count[b] == 0)
1020 continue;
1021
1022 struct anv_descriptor_set_binding_layout *binding =
1023 &layout->set[set].layout->binding[b];
1024
1025 /* Do a fixed-point calculation to generate a score based on the
1026 * number of uses and the binding array size. We shift by 7 instead
1027 * of 8 because we're going to use the top bit below to make
1028 * everything which does not support bindless super higher priority
1029 * than things which do.
1030 */
1031 uint16_t score = ((uint16_t)state.set[set].use_count[b] << 7) /
1032 binding->array_size;
1033
1034 /* If the descriptor type doesn't support bindless then put it at the
1035 * beginning so we guarantee it gets a slot.
1036 */
1037 if (!anv_descriptor_supports_bindless(pdevice, binding, true) ||
1038 !anv_descriptor_supports_bindless(pdevice, binding, false))
1039 score |= 1 << 15;
1040
1041 infos[used_binding_count++] = (struct binding_info) {
1042 .set = set,
1043 .binding = b,
1044 .score = score,
1045 };
1046 }
1047 }
1048
1049 /* Order the binding infos based on score with highest scores first. If
1050 * scores are equal we then order by set and binding.
1051 */
1052 qsort(infos, used_binding_count, sizeof(struct binding_info),
1053 compare_binding_infos);
1054
1055 bool have_dynamic_buffers = false;
1056
1057 for (unsigned i = 0; i < used_binding_count; i++) {
1058 unsigned set = infos[i].set, b = infos[i].binding;
1059 struct anv_descriptor_set_binding_layout *binding =
1060 &layout->set[set].layout->binding[b];
1061
1062 if (binding->dynamic_offset_index >= 0)
1063 have_dynamic_buffers = true;
1064
1065 const uint32_t array_size = binding->array_size;
1066
1067 if (binding->data & ANV_DESCRIPTOR_SURFACE_STATE) {
1068 if (map->surface_count + array_size > MAX_BINDING_TABLE_SIZE ||
1069 anv_descriptor_requires_bindless(pdevice, binding, false)) {
1070 /* If this descriptor doesn't fit in the binding table or if it
1071 * requires bindless for some reason, flag it as bindless.
1072 */
1073 assert(anv_descriptor_supports_bindless(pdevice, binding, false));
1074 state.set[set].surface_offsets[b] = BINDLESS_OFFSET;
1075 } else {
1076 state.set[set].surface_offsets[b] = map->surface_count;
1077 struct anv_sampler **samplers = binding->immutable_samplers;
1078 for (unsigned i = 0; i < binding->array_size; i++) {
1079 uint8_t planes = samplers ? samplers[i]->n_planes : 1;
1080 for (uint8_t p = 0; p < planes; p++) {
1081 map->surface_to_descriptor[map->surface_count++] =
1082 (struct anv_pipeline_binding) {
1083 .set = set,
1084 .binding = b,
1085 .index = i,
1086 .plane = p,
1087 };
1088 }
1089 }
1090 }
1091 assert(map->surface_count <= MAX_BINDING_TABLE_SIZE);
1092 }
1093
1094 if (binding->data & ANV_DESCRIPTOR_SAMPLER_STATE) {
1095 if (map->sampler_count + array_size > MAX_SAMPLER_TABLE_SIZE ||
1096 anv_descriptor_requires_bindless(pdevice, binding, true)) {
1097 /* If this descriptor doesn't fit in the binding table or if it
1098 * requires bindless for some reason, flag it as bindless.
1099 *
1100 * We also make large sampler arrays bindless because we can avoid
1101 * using indirect sends thanks to bindless samplers being packed
1102 * less tightly than the sampler table.
1103 */
1104 assert(anv_descriptor_supports_bindless(pdevice, binding, true));
1105 state.set[set].sampler_offsets[b] = BINDLESS_OFFSET;
1106 } else {
1107 state.set[set].sampler_offsets[b] = map->sampler_count;
1108 struct anv_sampler **samplers = binding->immutable_samplers;
1109 for (unsigned i = 0; i < binding->array_size; i++) {
1110 uint8_t planes = samplers ? samplers[i]->n_planes : 1;
1111 for (uint8_t p = 0; p < planes; p++) {
1112 map->sampler_to_descriptor[map->sampler_count++] =
1113 (struct anv_pipeline_binding) {
1114 .set = set,
1115 .binding = b,
1116 .index = i,
1117 .plane = p,
1118 };
1119 }
1120 }
1121 }
1122 }
1123 }
1124
1125 if (have_dynamic_buffers) {
1126 state.dynamic_offset_uniform_start = shader->num_uniforms;
1127 uint32_t *param = brw_stage_prog_data_add_params(prog_data,
1128 MAX_DYNAMIC_BUFFERS);
1129 for (unsigned i = 0; i < MAX_DYNAMIC_BUFFERS; i++)
1130 param[i] = ANV_PARAM_DYN_OFFSET(i);
1131 shader->num_uniforms += MAX_DYNAMIC_BUFFERS * 4;
1132 assert(shader->num_uniforms == prog_data->nr_params * 4);
1133 }
1134
1135 nir_foreach_variable(var, &shader->uniforms) {
1136 const struct glsl_type *glsl_type = glsl_without_array(var->type);
1137
1138 if (!glsl_type_is_image(glsl_type))
1139 continue;
1140
1141 enum glsl_sampler_dim dim = glsl_get_sampler_dim(glsl_type);
1142
1143 const uint32_t set = var->data.descriptor_set;
1144 const uint32_t binding = var->data.binding;
1145 const uint32_t array_size =
1146 layout->set[set].layout->binding[binding].array_size;
1147
1148 if (state.set[set].use_count[binding] == 0)
1149 continue;
1150
1151 if (state.set[set].surface_offsets[binding] >= MAX_BINDING_TABLE_SIZE)
1152 continue;
1153
1154 struct anv_pipeline_binding *pipe_binding =
1155 &map->surface_to_descriptor[state.set[set].surface_offsets[binding]];
1156 for (unsigned i = 0; i < array_size; i++) {
1157 assert(pipe_binding[i].set == set);
1158 assert(pipe_binding[i].binding == binding);
1159 assert(pipe_binding[i].index == i);
1160
1161 if (dim == GLSL_SAMPLER_DIM_SUBPASS ||
1162 dim == GLSL_SAMPLER_DIM_SUBPASS_MS)
1163 pipe_binding[i].input_attachment_index = var->data.index + i;
1164
1165 pipe_binding[i].write_only =
1166 (var->data.image.access & ACCESS_NON_READABLE) != 0;
1167 }
1168 }
1169
1170 nir_foreach_function(function, shader) {
1171 if (!function->impl)
1172 continue;
1173
1174 /* Before we do the normal lowering, we look for any SSBO operations
1175 * that we can lower to the BTI model and lower them up-front. The BTI
1176 * model can perform better than the A64 model for a couple reasons:
1177 *
1178 * 1. 48-bit address calculations are potentially expensive and using
1179 * the BTI model lets us simply compute 32-bit offsets and the
1180 * hardware adds the 64-bit surface base address.
1181 *
1182 * 2. The BTI messages, because they use surface states, do bounds
1183 * checking for us. With the A64 model, we have to do our own
1184 * bounds checking and this means wider pointers and extra
1185 * calculations and branching in the shader.
1186 *
1187 * The solution to both of these is to convert things to the BTI model
1188 * opportunistically. The reason why we need to do this as a pre-pass
1189 * is for two reasons:
1190 *
1191 * 1. The BTI model requires nir_address_format_32bit_index_offset
1192 * pointers which are not the same type as the pointers needed for
1193 * the A64 model. Because all our derefs are set up for the A64
1194 * model (in case we have variable pointers), we have to crawl all
1195 * the way back to the vulkan_resource_index intrinsic and build a
1196 * completely fresh index+offset calculation.
1197 *
1198 * 2. Because the variable-pointers-capable lowering that we do as part
1199 * of apply_pipeline_layout_block is destructive (It really has to
1200 * be to handle variable pointers properly), we've lost the deref
1201 * information by the time we get to the load/store/atomic
1202 * intrinsics in that pass.
1203 */
1204 lower_direct_buffer_access(function->impl, &state);
1205
1206 nir_builder_init(&state.builder, function->impl);
1207 nir_foreach_block(block, function->impl)
1208 apply_pipeline_layout_block(block, &state);
1209 nir_metadata_preserve(function->impl, nir_metadata_block_index |
1210 nir_metadata_dominance);
1211 }
1212
1213 ralloc_free(mem_ctx);
1214 }