spirv: handle UniformConstant for OpenCL kernels
[mesa.git] / src / compiler / spirv / vtn_variables.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 * Authors:
24 * Jason Ekstrand (jason@jlekstrand.net)
25 *
26 */
27
28 #include "vtn_private.h"
29 #include "spirv_info.h"
30 #include "nir_deref.h"
31 #include <vulkan/vulkan_core.h>
32
33 static void
34 ptr_decoration_cb(struct vtn_builder *b, struct vtn_value *val, int member,
35 const struct vtn_decoration *dec, void *void_ptr)
36 {
37 struct vtn_pointer *ptr = void_ptr;
38
39 switch (dec->decoration) {
40 case SpvDecorationNonUniformEXT:
41 ptr->access |= ACCESS_NON_UNIFORM;
42 break;
43
44 default:
45 break;
46 }
47 }
48
49 static struct vtn_pointer*
50 vtn_decorate_pointer(struct vtn_builder *b, struct vtn_value *val,
51 struct vtn_pointer *ptr)
52 {
53 struct vtn_pointer dummy = { .access = 0 };
54 vtn_foreach_decoration(b, val, ptr_decoration_cb, &dummy);
55
56 /* If we're adding access flags, make a copy of the pointer. We could
57 * probably just OR them in without doing so but this prevents us from
58 * leaking them any further than actually specified in the SPIR-V.
59 */
60 if (dummy.access & ~ptr->access) {
61 struct vtn_pointer *copy = ralloc(b, struct vtn_pointer);
62 *copy = *ptr;
63 copy->access |= dummy.access;
64 return copy;
65 }
66
67 return ptr;
68 }
69
70 struct vtn_value *
71 vtn_push_value_pointer(struct vtn_builder *b, uint32_t value_id,
72 struct vtn_pointer *ptr)
73 {
74 struct vtn_value *val = vtn_push_value(b, value_id, vtn_value_type_pointer);
75 val->pointer = vtn_decorate_pointer(b, val, ptr);
76 return val;
77 }
78
79 static void
80 ssa_decoration_cb(struct vtn_builder *b, struct vtn_value *val, int member,
81 const struct vtn_decoration *dec, void *void_ssa)
82 {
83 struct vtn_ssa_value *ssa = void_ssa;
84
85 switch (dec->decoration) {
86 case SpvDecorationNonUniformEXT:
87 ssa->access |= ACCESS_NON_UNIFORM;
88 break;
89
90 default:
91 break;
92 }
93 }
94
95 struct vtn_value *
96 vtn_push_ssa(struct vtn_builder *b, uint32_t value_id,
97 struct vtn_type *type, struct vtn_ssa_value *ssa)
98 {
99 struct vtn_value *val;
100 if (type->base_type == vtn_base_type_pointer) {
101 val = vtn_push_value_pointer(b, value_id, vtn_pointer_from_ssa(b, ssa->def, type));
102 } else {
103 val = vtn_push_value(b, value_id, vtn_value_type_ssa);
104 val->ssa = ssa;
105 vtn_foreach_decoration(b, val, ssa_decoration_cb, val->ssa);
106 }
107 return val;
108 }
109
110 static struct vtn_access_chain *
111 vtn_access_chain_create(struct vtn_builder *b, unsigned length)
112 {
113 struct vtn_access_chain *chain;
114
115 /* Subtract 1 from the length since there's already one built in */
116 size_t size = sizeof(*chain) +
117 (MAX2(length, 1) - 1) * sizeof(chain->link[0]);
118 chain = rzalloc_size(b, size);
119 chain->length = length;
120
121 return chain;
122 }
123
124 bool
125 vtn_mode_uses_ssa_offset(struct vtn_builder *b,
126 enum vtn_variable_mode mode)
127 {
128 return ((mode == vtn_variable_mode_ubo ||
129 mode == vtn_variable_mode_ssbo) &&
130 b->options->lower_ubo_ssbo_access_to_offsets) ||
131 mode == vtn_variable_mode_push_constant;
132 }
133
134 static bool
135 vtn_pointer_is_external_block(struct vtn_builder *b,
136 struct vtn_pointer *ptr)
137 {
138 return ptr->mode == vtn_variable_mode_ssbo ||
139 ptr->mode == vtn_variable_mode_ubo ||
140 ptr->mode == vtn_variable_mode_phys_ssbo ||
141 ptr->mode == vtn_variable_mode_push_constant;
142 }
143
144 static nir_ssa_def *
145 vtn_access_link_as_ssa(struct vtn_builder *b, struct vtn_access_link link,
146 unsigned stride, unsigned bit_size)
147 {
148 vtn_assert(stride > 0);
149 if (link.mode == vtn_access_mode_literal) {
150 return nir_imm_intN_t(&b->nb, link.id * stride, bit_size);
151 } else {
152 nir_ssa_def *ssa = vtn_ssa_value(b, link.id)->def;
153 if (ssa->bit_size != bit_size)
154 ssa = nir_i2i(&b->nb, ssa, bit_size);
155 return nir_imul_imm(&b->nb, ssa, stride);
156 }
157 }
158
159 static VkDescriptorType
160 vk_desc_type_for_mode(struct vtn_builder *b, enum vtn_variable_mode mode)
161 {
162 switch (mode) {
163 case vtn_variable_mode_ubo:
164 return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
165 case vtn_variable_mode_ssbo:
166 return VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
167 default:
168 vtn_fail("Invalid mode for vulkan_resource_index");
169 }
170 }
171
172 static nir_ssa_def *
173 vtn_variable_resource_index(struct vtn_builder *b, struct vtn_variable *var,
174 nir_ssa_def *desc_array_index)
175 {
176 vtn_assert(b->options->environment == NIR_SPIRV_VULKAN);
177
178 if (!desc_array_index) {
179 vtn_assert(glsl_type_is_struct_or_ifc(var->type->type));
180 desc_array_index = nir_imm_int(&b->nb, 0);
181 }
182
183 nir_intrinsic_instr *instr =
184 nir_intrinsic_instr_create(b->nb.shader,
185 nir_intrinsic_vulkan_resource_index);
186 instr->src[0] = nir_src_for_ssa(desc_array_index);
187 nir_intrinsic_set_desc_set(instr, var->descriptor_set);
188 nir_intrinsic_set_binding(instr, var->binding);
189 nir_intrinsic_set_desc_type(instr, vk_desc_type_for_mode(b, var->mode));
190
191 vtn_fail_if(var->mode != vtn_variable_mode_ubo &&
192 var->mode != vtn_variable_mode_ssbo,
193 "Invalid mode for vulkan_resource_index");
194
195 nir_address_format addr_format = vtn_mode_to_address_format(b, var->mode);
196 const struct glsl_type *index_type =
197 b->options->lower_ubo_ssbo_access_to_offsets ?
198 glsl_uint_type() : nir_address_format_to_glsl_type(addr_format);
199
200 instr->num_components = glsl_get_vector_elements(index_type);
201 nir_ssa_dest_init(&instr->instr, &instr->dest, instr->num_components,
202 glsl_get_bit_size(index_type), NULL);
203 nir_builder_instr_insert(&b->nb, &instr->instr);
204
205 return &instr->dest.ssa;
206 }
207
208 static nir_ssa_def *
209 vtn_resource_reindex(struct vtn_builder *b, enum vtn_variable_mode mode,
210 nir_ssa_def *base_index, nir_ssa_def *offset_index)
211 {
212 vtn_assert(b->options->environment == NIR_SPIRV_VULKAN);
213
214 nir_intrinsic_instr *instr =
215 nir_intrinsic_instr_create(b->nb.shader,
216 nir_intrinsic_vulkan_resource_reindex);
217 instr->src[0] = nir_src_for_ssa(base_index);
218 instr->src[1] = nir_src_for_ssa(offset_index);
219 nir_intrinsic_set_desc_type(instr, vk_desc_type_for_mode(b, mode));
220
221 vtn_fail_if(mode != vtn_variable_mode_ubo && mode != vtn_variable_mode_ssbo,
222 "Invalid mode for vulkan_resource_reindex");
223
224 nir_address_format addr_format = vtn_mode_to_address_format(b, mode);
225 const struct glsl_type *index_type =
226 b->options->lower_ubo_ssbo_access_to_offsets ?
227 glsl_uint_type() : nir_address_format_to_glsl_type(addr_format);
228
229 instr->num_components = glsl_get_vector_elements(index_type);
230 nir_ssa_dest_init(&instr->instr, &instr->dest, instr->num_components,
231 glsl_get_bit_size(index_type), NULL);
232 nir_builder_instr_insert(&b->nb, &instr->instr);
233
234 return &instr->dest.ssa;
235 }
236
237 static nir_ssa_def *
238 vtn_descriptor_load(struct vtn_builder *b, enum vtn_variable_mode mode,
239 nir_ssa_def *desc_index)
240 {
241 vtn_assert(b->options->environment == NIR_SPIRV_VULKAN);
242
243 nir_intrinsic_instr *desc_load =
244 nir_intrinsic_instr_create(b->nb.shader,
245 nir_intrinsic_load_vulkan_descriptor);
246 desc_load->src[0] = nir_src_for_ssa(desc_index);
247 nir_intrinsic_set_desc_type(desc_load, vk_desc_type_for_mode(b, mode));
248
249 vtn_fail_if(mode != vtn_variable_mode_ubo && mode != vtn_variable_mode_ssbo,
250 "Invalid mode for load_vulkan_descriptor");
251
252 nir_address_format addr_format = vtn_mode_to_address_format(b, mode);
253 const struct glsl_type *ptr_type =
254 nir_address_format_to_glsl_type(addr_format);
255
256 desc_load->num_components = glsl_get_vector_elements(ptr_type);
257 nir_ssa_dest_init(&desc_load->instr, &desc_load->dest,
258 desc_load->num_components,
259 glsl_get_bit_size(ptr_type), NULL);
260 nir_builder_instr_insert(&b->nb, &desc_load->instr);
261
262 return &desc_load->dest.ssa;
263 }
264
265 /* Dereference the given base pointer by the access chain */
266 static struct vtn_pointer *
267 vtn_nir_deref_pointer_dereference(struct vtn_builder *b,
268 struct vtn_pointer *base,
269 struct vtn_access_chain *deref_chain)
270 {
271 struct vtn_type *type = base->type;
272 enum gl_access_qualifier access = base->access | deref_chain->access;
273 unsigned idx = 0;
274
275 nir_deref_instr *tail;
276 if (base->deref) {
277 tail = base->deref;
278 } else if (b->options->environment == NIR_SPIRV_VULKAN &&
279 vtn_pointer_is_external_block(b, base)) {
280 nir_ssa_def *block_index = base->block_index;
281
282 /* We dereferencing an external block pointer. Correctness of this
283 * operation relies on one particular line in the SPIR-V spec, section
284 * entitled "Validation Rules for Shader Capabilities":
285 *
286 * "Block and BufferBlock decorations cannot decorate a structure
287 * type that is nested at any level inside another structure type
288 * decorated with Block or BufferBlock."
289 *
290 * This means that we can detect the point where we cross over from
291 * descriptor indexing to buffer indexing by looking for the block
292 * decorated struct type. Anything before the block decorated struct
293 * type is a descriptor indexing operation and anything after the block
294 * decorated struct is a buffer offset operation.
295 */
296
297 /* Figure out the descriptor array index if any
298 *
299 * Some of the Vulkan CTS tests with hand-rolled SPIR-V have been known
300 * to forget the Block or BufferBlock decoration from time to time.
301 * It's more robust if we check for both !block_index and for the type
302 * to contain a block. This way there's a decent chance that arrays of
303 * UBOs/SSBOs will work correctly even if variable pointers are
304 * completley toast.
305 */
306 nir_ssa_def *desc_arr_idx = NULL;
307 if (!block_index || vtn_type_contains_block(b, type)) {
308 /* If our type contains a block, then we're still outside the block
309 * and we need to process enough levels of dereferences to get inside
310 * of it.
311 */
312 if (deref_chain->ptr_as_array) {
313 unsigned aoa_size = glsl_get_aoa_size(type->type);
314 desc_arr_idx = vtn_access_link_as_ssa(b, deref_chain->link[idx],
315 MAX2(aoa_size, 1), 32);
316 idx++;
317 }
318
319 for (; idx < deref_chain->length; idx++) {
320 if (type->base_type != vtn_base_type_array) {
321 vtn_assert(type->base_type == vtn_base_type_struct);
322 break;
323 }
324
325 unsigned aoa_size = glsl_get_aoa_size(type->array_element->type);
326 nir_ssa_def *arr_offset =
327 vtn_access_link_as_ssa(b, deref_chain->link[idx],
328 MAX2(aoa_size, 1), 32);
329 if (desc_arr_idx)
330 desc_arr_idx = nir_iadd(&b->nb, desc_arr_idx, arr_offset);
331 else
332 desc_arr_idx = arr_offset;
333
334 type = type->array_element;
335 access |= type->access;
336 }
337 }
338
339 if (!block_index) {
340 vtn_assert(base->var && base->type);
341 block_index = vtn_variable_resource_index(b, base->var, desc_arr_idx);
342 } else if (desc_arr_idx) {
343 block_index = vtn_resource_reindex(b, base->mode,
344 block_index, desc_arr_idx);
345 }
346
347 if (idx == deref_chain->length) {
348 /* The entire deref was consumed in finding the block index. Return
349 * a pointer which just has a block index and a later access chain
350 * will dereference deeper.
351 */
352 struct vtn_pointer *ptr = rzalloc(b, struct vtn_pointer);
353 ptr->mode = base->mode;
354 ptr->type = type;
355 ptr->block_index = block_index;
356 ptr->access = access;
357 return ptr;
358 }
359
360 /* If we got here, there's more access chain to handle and we have the
361 * final block index. Insert a descriptor load and cast to a deref to
362 * start the deref chain.
363 */
364 nir_ssa_def *desc = vtn_descriptor_load(b, base->mode, block_index);
365
366 assert(base->mode == vtn_variable_mode_ssbo ||
367 base->mode == vtn_variable_mode_ubo);
368 nir_variable_mode nir_mode =
369 base->mode == vtn_variable_mode_ssbo ? nir_var_mem_ssbo : nir_var_mem_ubo;
370
371 tail = nir_build_deref_cast(&b->nb, desc, nir_mode, type->type,
372 base->ptr_type->stride);
373 } else {
374 assert(base->var && base->var->var);
375 tail = nir_build_deref_var(&b->nb, base->var->var);
376 if (base->ptr_type && base->ptr_type->type) {
377 tail->dest.ssa.num_components =
378 glsl_get_vector_elements(base->ptr_type->type);
379 tail->dest.ssa.bit_size = glsl_get_bit_size(base->ptr_type->type);
380 }
381 }
382
383 if (idx == 0 && deref_chain->ptr_as_array) {
384 /* We start with a deref cast to get the stride. Hopefully, we'll be
385 * able to delete that cast eventually.
386 */
387 tail = nir_build_deref_cast(&b->nb, &tail->dest.ssa, tail->mode,
388 tail->type, base->ptr_type->stride);
389
390 nir_ssa_def *index = vtn_access_link_as_ssa(b, deref_chain->link[0], 1,
391 tail->dest.ssa.bit_size);
392 tail = nir_build_deref_ptr_as_array(&b->nb, tail, index);
393 idx++;
394 }
395
396 for (; idx < deref_chain->length; idx++) {
397 if (glsl_type_is_struct_or_ifc(type->type)) {
398 vtn_assert(deref_chain->link[idx].mode == vtn_access_mode_literal);
399 unsigned field = deref_chain->link[idx].id;
400 tail = nir_build_deref_struct(&b->nb, tail, field);
401 type = type->members[field];
402 } else {
403 nir_ssa_def *arr_index =
404 vtn_access_link_as_ssa(b, deref_chain->link[idx], 1,
405 tail->dest.ssa.bit_size);
406 tail = nir_build_deref_array(&b->nb, tail, arr_index);
407 type = type->array_element;
408 }
409
410 access |= type->access;
411 }
412
413 struct vtn_pointer *ptr = rzalloc(b, struct vtn_pointer);
414 ptr->mode = base->mode;
415 ptr->type = type;
416 ptr->var = base->var;
417 ptr->deref = tail;
418 ptr->access = access;
419
420 return ptr;
421 }
422
423 static struct vtn_pointer *
424 vtn_ssa_offset_pointer_dereference(struct vtn_builder *b,
425 struct vtn_pointer *base,
426 struct vtn_access_chain *deref_chain)
427 {
428 nir_ssa_def *block_index = base->block_index;
429 nir_ssa_def *offset = base->offset;
430 struct vtn_type *type = base->type;
431 enum gl_access_qualifier access = base->access;
432
433 unsigned idx = 0;
434 if (base->mode == vtn_variable_mode_ubo ||
435 base->mode == vtn_variable_mode_ssbo) {
436 if (!block_index) {
437 vtn_assert(base->var && base->type);
438 nir_ssa_def *desc_arr_idx;
439 if (glsl_type_is_array(type->type)) {
440 if (deref_chain->length >= 1) {
441 desc_arr_idx =
442 vtn_access_link_as_ssa(b, deref_chain->link[0], 1, 32);
443 idx++;
444 /* This consumes a level of type */
445 type = type->array_element;
446 access |= type->access;
447 } else {
448 /* This is annoying. We've been asked for a pointer to the
449 * array of UBOs/SSBOs and not a specifc buffer. Return a
450 * pointer with a descriptor index of 0 and we'll have to do
451 * a reindex later to adjust it to the right thing.
452 */
453 desc_arr_idx = nir_imm_int(&b->nb, 0);
454 }
455 } else if (deref_chain->ptr_as_array) {
456 /* You can't have a zero-length OpPtrAccessChain */
457 vtn_assert(deref_chain->length >= 1);
458 desc_arr_idx = vtn_access_link_as_ssa(b, deref_chain->link[0], 1, 32);
459 } else {
460 /* We have a regular non-array SSBO. */
461 desc_arr_idx = NULL;
462 }
463 block_index = vtn_variable_resource_index(b, base->var, desc_arr_idx);
464 } else if (deref_chain->ptr_as_array &&
465 type->base_type == vtn_base_type_struct && type->block) {
466 /* We are doing an OpPtrAccessChain on a pointer to a struct that is
467 * decorated block. This is an interesting corner in the SPIR-V
468 * spec. One interpretation would be that they client is clearly
469 * trying to treat that block as if it's an implicit array of blocks
470 * repeated in the buffer. However, the SPIR-V spec for the
471 * OpPtrAccessChain says:
472 *
473 * "Base is treated as the address of the first element of an
474 * array, and the Element element’s address is computed to be the
475 * base for the Indexes, as per OpAccessChain."
476 *
477 * Taken literally, that would mean that your struct type is supposed
478 * to be treated as an array of such a struct and, since it's
479 * decorated block, that means an array of blocks which corresponds
480 * to an array descriptor. Therefore, we need to do a reindex
481 * operation to add the index from the first link in the access chain
482 * to the index we recieved.
483 *
484 * The downside to this interpretation (there always is one) is that
485 * this might be somewhat surprising behavior to apps if they expect
486 * the implicit array behavior described above.
487 */
488 vtn_assert(deref_chain->length >= 1);
489 nir_ssa_def *offset_index =
490 vtn_access_link_as_ssa(b, deref_chain->link[0], 1, 32);
491 idx++;
492
493 block_index = vtn_resource_reindex(b, base->mode,
494 block_index, offset_index);
495 }
496 }
497
498 if (!offset) {
499 if (base->mode == vtn_variable_mode_workgroup) {
500 /* SLM doesn't need nor have a block index */
501 vtn_assert(!block_index);
502
503 /* We need the variable for the base offset */
504 vtn_assert(base->var);
505
506 /* We need ptr_type for size and alignment */
507 vtn_assert(base->ptr_type);
508
509 /* Assign location on first use so that we don't end up bloating SLM
510 * address space for variables which are never statically used.
511 */
512 if (base->var->shared_location < 0) {
513 vtn_assert(base->ptr_type->length > 0 && base->ptr_type->align > 0);
514 b->shader->num_shared = vtn_align_u32(b->shader->num_shared,
515 base->ptr_type->align);
516 base->var->shared_location = b->shader->num_shared;
517 b->shader->num_shared += base->ptr_type->length;
518 }
519
520 offset = nir_imm_int(&b->nb, base->var->shared_location);
521 } else if (base->mode == vtn_variable_mode_push_constant) {
522 /* Push constants neither need nor have a block index */
523 vtn_assert(!block_index);
524
525 /* Start off with at the start of the push constant block. */
526 offset = nir_imm_int(&b->nb, 0);
527 } else {
528 /* The code above should have ensured a block_index when needed. */
529 vtn_assert(block_index);
530
531 /* Start off with at the start of the buffer. */
532 offset = nir_imm_int(&b->nb, 0);
533 }
534 }
535
536 if (deref_chain->ptr_as_array && idx == 0) {
537 /* We need ptr_type for the stride */
538 vtn_assert(base->ptr_type);
539
540 /* We need at least one element in the chain */
541 vtn_assert(deref_chain->length >= 1);
542
543 nir_ssa_def *elem_offset =
544 vtn_access_link_as_ssa(b, deref_chain->link[idx],
545 base->ptr_type->stride, offset->bit_size);
546 offset = nir_iadd(&b->nb, offset, elem_offset);
547 idx++;
548 }
549
550 for (; idx < deref_chain->length; idx++) {
551 switch (glsl_get_base_type(type->type)) {
552 case GLSL_TYPE_UINT:
553 case GLSL_TYPE_INT:
554 case GLSL_TYPE_UINT16:
555 case GLSL_TYPE_INT16:
556 case GLSL_TYPE_UINT8:
557 case GLSL_TYPE_INT8:
558 case GLSL_TYPE_UINT64:
559 case GLSL_TYPE_INT64:
560 case GLSL_TYPE_FLOAT:
561 case GLSL_TYPE_FLOAT16:
562 case GLSL_TYPE_DOUBLE:
563 case GLSL_TYPE_BOOL:
564 case GLSL_TYPE_ARRAY: {
565 nir_ssa_def *elem_offset =
566 vtn_access_link_as_ssa(b, deref_chain->link[idx],
567 type->stride, offset->bit_size);
568 offset = nir_iadd(&b->nb, offset, elem_offset);
569 type = type->array_element;
570 access |= type->access;
571 break;
572 }
573
574 case GLSL_TYPE_INTERFACE:
575 case GLSL_TYPE_STRUCT: {
576 vtn_assert(deref_chain->link[idx].mode == vtn_access_mode_literal);
577 unsigned member = deref_chain->link[idx].id;
578 offset = nir_iadd_imm(&b->nb, offset, type->offsets[member]);
579 type = type->members[member];
580 access |= type->access;
581 break;
582 }
583
584 default:
585 vtn_fail("Invalid type for deref");
586 }
587 }
588
589 struct vtn_pointer *ptr = rzalloc(b, struct vtn_pointer);
590 ptr->mode = base->mode;
591 ptr->type = type;
592 ptr->block_index = block_index;
593 ptr->offset = offset;
594 ptr->access = access;
595
596 return ptr;
597 }
598
599 /* Dereference the given base pointer by the access chain */
600 static struct vtn_pointer *
601 vtn_pointer_dereference(struct vtn_builder *b,
602 struct vtn_pointer *base,
603 struct vtn_access_chain *deref_chain)
604 {
605 if (vtn_pointer_uses_ssa_offset(b, base)) {
606 return vtn_ssa_offset_pointer_dereference(b, base, deref_chain);
607 } else {
608 return vtn_nir_deref_pointer_dereference(b, base, deref_chain);
609 }
610 }
611
612 struct vtn_pointer *
613 vtn_pointer_for_variable(struct vtn_builder *b,
614 struct vtn_variable *var, struct vtn_type *ptr_type)
615 {
616 struct vtn_pointer *pointer = rzalloc(b, struct vtn_pointer);
617
618 pointer->mode = var->mode;
619 pointer->type = var->type;
620 vtn_assert(ptr_type->base_type == vtn_base_type_pointer);
621 vtn_assert(ptr_type->deref->type == var->type->type);
622 pointer->ptr_type = ptr_type;
623 pointer->var = var;
624 pointer->access = var->access | var->type->access;
625
626 return pointer;
627 }
628
629 /* Returns an atomic_uint type based on the original uint type. The returned
630 * type will be equivalent to the original one but will have an atomic_uint
631 * type as leaf instead of an uint.
632 *
633 * Manages uint scalars, arrays, and arrays of arrays of any nested depth.
634 */
635 static const struct glsl_type *
636 repair_atomic_type(const struct glsl_type *type)
637 {
638 assert(glsl_get_base_type(glsl_without_array(type)) == GLSL_TYPE_UINT);
639 assert(glsl_type_is_scalar(glsl_without_array(type)));
640
641 if (glsl_type_is_array(type)) {
642 const struct glsl_type *atomic =
643 repair_atomic_type(glsl_get_array_element(type));
644
645 return glsl_array_type(atomic, glsl_get_length(type),
646 glsl_get_explicit_stride(type));
647 } else {
648 return glsl_atomic_uint_type();
649 }
650 }
651
652 nir_deref_instr *
653 vtn_pointer_to_deref(struct vtn_builder *b, struct vtn_pointer *ptr)
654 {
655 if (b->wa_glslang_179) {
656 /* Do on-the-fly copy propagation for samplers. */
657 if (ptr->var && ptr->var->copy_prop_sampler)
658 return vtn_pointer_to_deref(b, ptr->var->copy_prop_sampler);
659 }
660
661 vtn_assert(!vtn_pointer_uses_ssa_offset(b, ptr));
662 if (!ptr->deref) {
663 struct vtn_access_chain chain = {
664 .length = 0,
665 };
666 ptr = vtn_nir_deref_pointer_dereference(b, ptr, &chain);
667 }
668
669 return ptr->deref;
670 }
671
672 static void
673 _vtn_local_load_store(struct vtn_builder *b, bool load, nir_deref_instr *deref,
674 struct vtn_ssa_value *inout,
675 enum gl_access_qualifier access)
676 {
677 if (glsl_type_is_vector_or_scalar(deref->type)) {
678 if (load) {
679 inout->def = nir_load_deref_with_access(&b->nb, deref, access);
680 } else {
681 nir_store_deref_with_access(&b->nb, deref, inout->def, ~0, access);
682 }
683 } else if (glsl_type_is_array(deref->type) ||
684 glsl_type_is_matrix(deref->type)) {
685 unsigned elems = glsl_get_length(deref->type);
686 for (unsigned i = 0; i < elems; i++) {
687 nir_deref_instr *child =
688 nir_build_deref_array_imm(&b->nb, deref, i);
689 _vtn_local_load_store(b, load, child, inout->elems[i], access);
690 }
691 } else {
692 vtn_assert(glsl_type_is_struct_or_ifc(deref->type));
693 unsigned elems = glsl_get_length(deref->type);
694 for (unsigned i = 0; i < elems; i++) {
695 nir_deref_instr *child = nir_build_deref_struct(&b->nb, deref, i);
696 _vtn_local_load_store(b, load, child, inout->elems[i], access);
697 }
698 }
699 }
700
701 nir_deref_instr *
702 vtn_nir_deref(struct vtn_builder *b, uint32_t id)
703 {
704 struct vtn_pointer *ptr = vtn_value(b, id, vtn_value_type_pointer)->pointer;
705 return vtn_pointer_to_deref(b, ptr);
706 }
707
708 /*
709 * Gets the NIR-level deref tail, which may have as a child an array deref
710 * selecting which component due to OpAccessChain supporting per-component
711 * indexing in SPIR-V.
712 */
713 static nir_deref_instr *
714 get_deref_tail(nir_deref_instr *deref)
715 {
716 if (deref->deref_type != nir_deref_type_array)
717 return deref;
718
719 nir_deref_instr *parent =
720 nir_instr_as_deref(deref->parent.ssa->parent_instr);
721
722 if (glsl_type_is_vector(parent->type))
723 return parent;
724 else
725 return deref;
726 }
727
728 struct vtn_ssa_value *
729 vtn_local_load(struct vtn_builder *b, nir_deref_instr *src,
730 enum gl_access_qualifier access)
731 {
732 nir_deref_instr *src_tail = get_deref_tail(src);
733 struct vtn_ssa_value *val = vtn_create_ssa_value(b, src_tail->type);
734 _vtn_local_load_store(b, true, src_tail, val, access);
735
736 if (src_tail != src) {
737 val->type = src->type;
738 if (nir_src_is_const(src->arr.index))
739 val->def = vtn_vector_extract(b, val->def,
740 nir_src_as_uint(src->arr.index));
741 else
742 val->def = vtn_vector_extract_dynamic(b, val->def, src->arr.index.ssa);
743 }
744
745 return val;
746 }
747
748 void
749 vtn_local_store(struct vtn_builder *b, struct vtn_ssa_value *src,
750 nir_deref_instr *dest, enum gl_access_qualifier access)
751 {
752 nir_deref_instr *dest_tail = get_deref_tail(dest);
753
754 if (dest_tail != dest) {
755 struct vtn_ssa_value *val = vtn_create_ssa_value(b, dest_tail->type);
756 _vtn_local_load_store(b, true, dest_tail, val, access);
757
758 if (nir_src_is_const(dest->arr.index))
759 val->def = vtn_vector_insert(b, val->def, src->def,
760 nir_src_as_uint(dest->arr.index));
761 else
762 val->def = vtn_vector_insert_dynamic(b, val->def, src->def,
763 dest->arr.index.ssa);
764 _vtn_local_load_store(b, false, dest_tail, val, access);
765 } else {
766 _vtn_local_load_store(b, false, dest_tail, src, access);
767 }
768 }
769
770 nir_ssa_def *
771 vtn_pointer_to_offset(struct vtn_builder *b, struct vtn_pointer *ptr,
772 nir_ssa_def **index_out)
773 {
774 assert(vtn_pointer_uses_ssa_offset(b, ptr));
775 if (!ptr->offset) {
776 struct vtn_access_chain chain = {
777 .length = 0,
778 };
779 ptr = vtn_ssa_offset_pointer_dereference(b, ptr, &chain);
780 }
781 *index_out = ptr->block_index;
782 return ptr->offset;
783 }
784
785 /* Tries to compute the size of an interface block based on the strides and
786 * offsets that are provided to us in the SPIR-V source.
787 */
788 static unsigned
789 vtn_type_block_size(struct vtn_builder *b, struct vtn_type *type)
790 {
791 enum glsl_base_type base_type = glsl_get_base_type(type->type);
792 switch (base_type) {
793 case GLSL_TYPE_UINT:
794 case GLSL_TYPE_INT:
795 case GLSL_TYPE_UINT16:
796 case GLSL_TYPE_INT16:
797 case GLSL_TYPE_UINT8:
798 case GLSL_TYPE_INT8:
799 case GLSL_TYPE_UINT64:
800 case GLSL_TYPE_INT64:
801 case GLSL_TYPE_FLOAT:
802 case GLSL_TYPE_FLOAT16:
803 case GLSL_TYPE_BOOL:
804 case GLSL_TYPE_DOUBLE: {
805 unsigned cols = type->row_major ? glsl_get_vector_elements(type->type) :
806 glsl_get_matrix_columns(type->type);
807 if (cols > 1) {
808 vtn_assert(type->stride > 0);
809 return type->stride * cols;
810 } else {
811 unsigned type_size = glsl_get_bit_size(type->type) / 8;
812 return glsl_get_vector_elements(type->type) * type_size;
813 }
814 }
815
816 case GLSL_TYPE_STRUCT:
817 case GLSL_TYPE_INTERFACE: {
818 unsigned size = 0;
819 unsigned num_fields = glsl_get_length(type->type);
820 for (unsigned f = 0; f < num_fields; f++) {
821 unsigned field_end = type->offsets[f] +
822 vtn_type_block_size(b, type->members[f]);
823 size = MAX2(size, field_end);
824 }
825 return size;
826 }
827
828 case GLSL_TYPE_ARRAY:
829 vtn_assert(type->stride > 0);
830 vtn_assert(glsl_get_length(type->type) > 0);
831 return type->stride * glsl_get_length(type->type);
832
833 default:
834 vtn_fail("Invalid block type");
835 return 0;
836 }
837 }
838
839 static void
840 _vtn_load_store_tail(struct vtn_builder *b, nir_intrinsic_op op, bool load,
841 nir_ssa_def *index, nir_ssa_def *offset,
842 unsigned access_offset, unsigned access_size,
843 struct vtn_ssa_value **inout, const struct glsl_type *type,
844 enum gl_access_qualifier access)
845 {
846 nir_intrinsic_instr *instr = nir_intrinsic_instr_create(b->nb.shader, op);
847 instr->num_components = glsl_get_vector_elements(type);
848
849 /* Booleans usually shouldn't show up in external memory in SPIR-V.
850 * However, they do for certain older GLSLang versions and can for shared
851 * memory when we lower access chains internally.
852 */
853 const unsigned data_bit_size = glsl_type_is_boolean(type) ? 32 :
854 glsl_get_bit_size(type);
855
856 int src = 0;
857 if (!load) {
858 nir_intrinsic_set_write_mask(instr, (1 << instr->num_components) - 1);
859 instr->src[src++] = nir_src_for_ssa((*inout)->def);
860 }
861
862 if (op == nir_intrinsic_load_push_constant) {
863 nir_intrinsic_set_base(instr, access_offset);
864 nir_intrinsic_set_range(instr, access_size);
865 }
866
867 if (op == nir_intrinsic_load_ubo ||
868 op == nir_intrinsic_load_ssbo ||
869 op == nir_intrinsic_store_ssbo) {
870 nir_intrinsic_set_access(instr, access);
871 }
872
873 /* With extensions like relaxed_block_layout, we really can't guarantee
874 * much more than scalar alignment.
875 */
876 if (op != nir_intrinsic_load_push_constant)
877 nir_intrinsic_set_align(instr, data_bit_size / 8, 0);
878
879 if (index)
880 instr->src[src++] = nir_src_for_ssa(index);
881
882 if (op == nir_intrinsic_load_push_constant) {
883 /* We need to subtract the offset from where the intrinsic will load the
884 * data. */
885 instr->src[src++] =
886 nir_src_for_ssa(nir_isub(&b->nb, offset,
887 nir_imm_int(&b->nb, access_offset)));
888 } else {
889 instr->src[src++] = nir_src_for_ssa(offset);
890 }
891
892 if (load) {
893 nir_ssa_dest_init(&instr->instr, &instr->dest,
894 instr->num_components, data_bit_size, NULL);
895 (*inout)->def = &instr->dest.ssa;
896 }
897
898 nir_builder_instr_insert(&b->nb, &instr->instr);
899
900 if (load && glsl_get_base_type(type) == GLSL_TYPE_BOOL)
901 (*inout)->def = nir_ine(&b->nb, (*inout)->def, nir_imm_int(&b->nb, 0));
902 }
903
904 static void
905 _vtn_block_load_store(struct vtn_builder *b, nir_intrinsic_op op, bool load,
906 nir_ssa_def *index, nir_ssa_def *offset,
907 unsigned access_offset, unsigned access_size,
908 struct vtn_type *type, enum gl_access_qualifier access,
909 struct vtn_ssa_value **inout)
910 {
911 if (load && *inout == NULL)
912 *inout = vtn_create_ssa_value(b, type->type);
913
914 enum glsl_base_type base_type = glsl_get_base_type(type->type);
915 switch (base_type) {
916 case GLSL_TYPE_UINT:
917 case GLSL_TYPE_INT:
918 case GLSL_TYPE_UINT16:
919 case GLSL_TYPE_INT16:
920 case GLSL_TYPE_UINT8:
921 case GLSL_TYPE_INT8:
922 case GLSL_TYPE_UINT64:
923 case GLSL_TYPE_INT64:
924 case GLSL_TYPE_FLOAT:
925 case GLSL_TYPE_FLOAT16:
926 case GLSL_TYPE_DOUBLE:
927 case GLSL_TYPE_BOOL:
928 /* This is where things get interesting. At this point, we've hit
929 * a vector, a scalar, or a matrix.
930 */
931 if (glsl_type_is_matrix(type->type)) {
932 /* Loading the whole matrix */
933 struct vtn_ssa_value *transpose;
934 unsigned num_ops, vec_width, col_stride;
935 if (type->row_major) {
936 num_ops = glsl_get_vector_elements(type->type);
937 vec_width = glsl_get_matrix_columns(type->type);
938 col_stride = type->array_element->stride;
939 if (load) {
940 const struct glsl_type *transpose_type =
941 glsl_matrix_type(base_type, vec_width, num_ops);
942 *inout = vtn_create_ssa_value(b, transpose_type);
943 } else {
944 transpose = vtn_ssa_transpose(b, *inout);
945 inout = &transpose;
946 }
947 } else {
948 num_ops = glsl_get_matrix_columns(type->type);
949 vec_width = glsl_get_vector_elements(type->type);
950 col_stride = type->stride;
951 }
952
953 for (unsigned i = 0; i < num_ops; i++) {
954 nir_ssa_def *elem_offset =
955 nir_iadd_imm(&b->nb, offset, i * col_stride);
956 _vtn_load_store_tail(b, op, load, index, elem_offset,
957 access_offset, access_size,
958 &(*inout)->elems[i],
959 glsl_vector_type(base_type, vec_width),
960 type->access | access);
961 }
962
963 if (load && type->row_major)
964 *inout = vtn_ssa_transpose(b, *inout);
965 } else {
966 unsigned elems = glsl_get_vector_elements(type->type);
967 unsigned type_size = glsl_get_bit_size(type->type) / 8;
968 if (elems == 1 || type->stride == type_size) {
969 /* This is a tightly-packed normal scalar or vector load */
970 vtn_assert(glsl_type_is_vector_or_scalar(type->type));
971 _vtn_load_store_tail(b, op, load, index, offset,
972 access_offset, access_size,
973 inout, type->type,
974 type->access | access);
975 } else {
976 /* This is a strided load. We have to load N things separately.
977 * This is the single column of a row-major matrix case.
978 */
979 vtn_assert(type->stride > type_size);
980 vtn_assert(type->stride % type_size == 0);
981
982 nir_ssa_def *per_comp[4];
983 for (unsigned i = 0; i < elems; i++) {
984 nir_ssa_def *elem_offset =
985 nir_iadd_imm(&b->nb, offset, i * type->stride);
986 struct vtn_ssa_value *comp, temp_val;
987 if (!load) {
988 temp_val.def = nir_channel(&b->nb, (*inout)->def, i);
989 temp_val.type = glsl_scalar_type(base_type);
990 }
991 comp = &temp_val;
992 _vtn_load_store_tail(b, op, load, index, elem_offset,
993 access_offset, access_size,
994 &comp, glsl_scalar_type(base_type),
995 type->access | access);
996 per_comp[i] = comp->def;
997 }
998
999 if (load) {
1000 if (*inout == NULL)
1001 *inout = vtn_create_ssa_value(b, type->type);
1002 (*inout)->def = nir_vec(&b->nb, per_comp, elems);
1003 }
1004 }
1005 }
1006 return;
1007
1008 case GLSL_TYPE_ARRAY: {
1009 unsigned elems = glsl_get_length(type->type);
1010 for (unsigned i = 0; i < elems; i++) {
1011 nir_ssa_def *elem_off =
1012 nir_iadd_imm(&b->nb, offset, i * type->stride);
1013 _vtn_block_load_store(b, op, load, index, elem_off,
1014 access_offset, access_size,
1015 type->array_element,
1016 type->array_element->access | access,
1017 &(*inout)->elems[i]);
1018 }
1019 return;
1020 }
1021
1022 case GLSL_TYPE_INTERFACE:
1023 case GLSL_TYPE_STRUCT: {
1024 unsigned elems = glsl_get_length(type->type);
1025 for (unsigned i = 0; i < elems; i++) {
1026 nir_ssa_def *elem_off =
1027 nir_iadd_imm(&b->nb, offset, type->offsets[i]);
1028 _vtn_block_load_store(b, op, load, index, elem_off,
1029 access_offset, access_size,
1030 type->members[i],
1031 type->members[i]->access | access,
1032 &(*inout)->elems[i]);
1033 }
1034 return;
1035 }
1036
1037 default:
1038 vtn_fail("Invalid block member type");
1039 }
1040 }
1041
1042 static struct vtn_ssa_value *
1043 vtn_block_load(struct vtn_builder *b, struct vtn_pointer *src)
1044 {
1045 nir_intrinsic_op op;
1046 unsigned access_offset = 0, access_size = 0;
1047 switch (src->mode) {
1048 case vtn_variable_mode_ubo:
1049 op = nir_intrinsic_load_ubo;
1050 break;
1051 case vtn_variable_mode_ssbo:
1052 op = nir_intrinsic_load_ssbo;
1053 break;
1054 case vtn_variable_mode_push_constant:
1055 op = nir_intrinsic_load_push_constant;
1056 access_size = b->shader->num_uniforms;
1057 break;
1058 case vtn_variable_mode_workgroup:
1059 op = nir_intrinsic_load_shared;
1060 break;
1061 default:
1062 vtn_fail("Invalid block variable mode");
1063 }
1064
1065 nir_ssa_def *offset, *index = NULL;
1066 offset = vtn_pointer_to_offset(b, src, &index);
1067
1068 struct vtn_ssa_value *value = NULL;
1069 _vtn_block_load_store(b, op, true, index, offset,
1070 access_offset, access_size,
1071 src->type, src->access, &value);
1072 return value;
1073 }
1074
1075 static void
1076 vtn_block_store(struct vtn_builder *b, struct vtn_ssa_value *src,
1077 struct vtn_pointer *dst)
1078 {
1079 nir_intrinsic_op op;
1080 switch (dst->mode) {
1081 case vtn_variable_mode_ssbo:
1082 op = nir_intrinsic_store_ssbo;
1083 break;
1084 case vtn_variable_mode_workgroup:
1085 op = nir_intrinsic_store_shared;
1086 break;
1087 default:
1088 vtn_fail("Invalid block variable mode");
1089 }
1090
1091 nir_ssa_def *offset, *index = NULL;
1092 offset = vtn_pointer_to_offset(b, dst, &index);
1093
1094 _vtn_block_load_store(b, op, false, index, offset,
1095 0, 0, dst->type, dst->access, &src);
1096 }
1097
1098 static void
1099 _vtn_variable_load_store(struct vtn_builder *b, bool load,
1100 struct vtn_pointer *ptr,
1101 enum gl_access_qualifier access,
1102 struct vtn_ssa_value **inout)
1103 {
1104 enum glsl_base_type base_type = glsl_get_base_type(ptr->type->type);
1105 switch (base_type) {
1106 case GLSL_TYPE_UINT:
1107 case GLSL_TYPE_INT:
1108 case GLSL_TYPE_UINT16:
1109 case GLSL_TYPE_INT16:
1110 case GLSL_TYPE_UINT8:
1111 case GLSL_TYPE_INT8:
1112 case GLSL_TYPE_UINT64:
1113 case GLSL_TYPE_INT64:
1114 case GLSL_TYPE_FLOAT:
1115 case GLSL_TYPE_FLOAT16:
1116 case GLSL_TYPE_BOOL:
1117 case GLSL_TYPE_DOUBLE:
1118 if (glsl_type_is_vector_or_scalar(ptr->type->type)) {
1119 /* We hit a vector or scalar; go ahead and emit the load[s] */
1120 nir_deref_instr *deref = vtn_pointer_to_deref(b, ptr);
1121 if (vtn_pointer_is_external_block(b, ptr)) {
1122 /* If it's external, we call nir_load/store_deref directly. The
1123 * vtn_local_load/store helpers are too clever and do magic to
1124 * avoid array derefs of vectors. That magic is both less
1125 * efficient than the direct load/store and, in the case of
1126 * stores, is broken because it creates a race condition if two
1127 * threads are writing to different components of the same vector
1128 * due to the load+insert+store it uses to emulate the array
1129 * deref.
1130 */
1131 if (load) {
1132 *inout = vtn_create_ssa_value(b, ptr->type->type);
1133 (*inout)->def = nir_load_deref_with_access(&b->nb, deref,
1134 ptr->type->access | access);
1135 } else {
1136 nir_store_deref_with_access(&b->nb, deref, (*inout)->def, ~0,
1137 ptr->type->access | access);
1138 }
1139 } else {
1140 if (load) {
1141 *inout = vtn_local_load(b, deref, ptr->type->access | access);
1142 } else {
1143 vtn_local_store(b, *inout, deref, ptr->type->access | access);
1144 }
1145 }
1146 return;
1147 }
1148 /* Fall through */
1149
1150 case GLSL_TYPE_INTERFACE:
1151 case GLSL_TYPE_ARRAY:
1152 case GLSL_TYPE_STRUCT: {
1153 unsigned elems = glsl_get_length(ptr->type->type);
1154 if (load) {
1155 vtn_assert(*inout == NULL);
1156 *inout = rzalloc(b, struct vtn_ssa_value);
1157 (*inout)->type = ptr->type->type;
1158 (*inout)->elems = rzalloc_array(b, struct vtn_ssa_value *, elems);
1159 }
1160
1161 struct vtn_access_chain chain = {
1162 .length = 1,
1163 .link = {
1164 { .mode = vtn_access_mode_literal, },
1165 }
1166 };
1167 for (unsigned i = 0; i < elems; i++) {
1168 chain.link[0].id = i;
1169 struct vtn_pointer *elem = vtn_pointer_dereference(b, ptr, &chain);
1170 _vtn_variable_load_store(b, load, elem, ptr->type->access | access,
1171 &(*inout)->elems[i]);
1172 }
1173 return;
1174 }
1175
1176 default:
1177 vtn_fail("Invalid access chain type");
1178 }
1179 }
1180
1181 struct vtn_ssa_value *
1182 vtn_variable_load(struct vtn_builder *b, struct vtn_pointer *src)
1183 {
1184 if (vtn_pointer_uses_ssa_offset(b, src)) {
1185 return vtn_block_load(b, src);
1186 } else {
1187 struct vtn_ssa_value *val = NULL;
1188 _vtn_variable_load_store(b, true, src, src->access, &val);
1189 return val;
1190 }
1191 }
1192
1193 void
1194 vtn_variable_store(struct vtn_builder *b, struct vtn_ssa_value *src,
1195 struct vtn_pointer *dest)
1196 {
1197 if (vtn_pointer_uses_ssa_offset(b, dest)) {
1198 vtn_assert(dest->mode == vtn_variable_mode_ssbo ||
1199 dest->mode == vtn_variable_mode_workgroup);
1200 vtn_block_store(b, src, dest);
1201 } else {
1202 _vtn_variable_load_store(b, false, dest, dest->access, &src);
1203 }
1204 }
1205
1206 static void
1207 _vtn_variable_copy(struct vtn_builder *b, struct vtn_pointer *dest,
1208 struct vtn_pointer *src)
1209 {
1210 vtn_assert(src->type->type == dest->type->type);
1211 enum glsl_base_type base_type = glsl_get_base_type(src->type->type);
1212 switch (base_type) {
1213 case GLSL_TYPE_UINT:
1214 case GLSL_TYPE_INT:
1215 case GLSL_TYPE_UINT16:
1216 case GLSL_TYPE_INT16:
1217 case GLSL_TYPE_UINT8:
1218 case GLSL_TYPE_INT8:
1219 case GLSL_TYPE_UINT64:
1220 case GLSL_TYPE_INT64:
1221 case GLSL_TYPE_FLOAT:
1222 case GLSL_TYPE_FLOAT16:
1223 case GLSL_TYPE_DOUBLE:
1224 case GLSL_TYPE_BOOL:
1225 /* At this point, we have a scalar, vector, or matrix so we know that
1226 * there cannot be any structure splitting still in the way. By
1227 * stopping at the matrix level rather than the vector level, we
1228 * ensure that matrices get loaded in the optimal way even if they
1229 * are storred row-major in a UBO.
1230 */
1231 vtn_variable_store(b, vtn_variable_load(b, src), dest);
1232 return;
1233
1234 case GLSL_TYPE_INTERFACE:
1235 case GLSL_TYPE_ARRAY:
1236 case GLSL_TYPE_STRUCT: {
1237 struct vtn_access_chain chain = {
1238 .length = 1,
1239 .link = {
1240 { .mode = vtn_access_mode_literal, },
1241 }
1242 };
1243 unsigned elems = glsl_get_length(src->type->type);
1244 for (unsigned i = 0; i < elems; i++) {
1245 chain.link[0].id = i;
1246 struct vtn_pointer *src_elem =
1247 vtn_pointer_dereference(b, src, &chain);
1248 struct vtn_pointer *dest_elem =
1249 vtn_pointer_dereference(b, dest, &chain);
1250
1251 _vtn_variable_copy(b, dest_elem, src_elem);
1252 }
1253 return;
1254 }
1255
1256 default:
1257 vtn_fail("Invalid access chain type");
1258 }
1259 }
1260
1261 static void
1262 vtn_variable_copy(struct vtn_builder *b, struct vtn_pointer *dest,
1263 struct vtn_pointer *src)
1264 {
1265 /* TODO: At some point, we should add a special-case for when we can
1266 * just emit a copy_var intrinsic.
1267 */
1268 _vtn_variable_copy(b, dest, src);
1269 }
1270
1271 static void
1272 set_mode_system_value(struct vtn_builder *b, nir_variable_mode *mode)
1273 {
1274 vtn_assert(*mode == nir_var_system_value || *mode == nir_var_shader_in);
1275 *mode = nir_var_system_value;
1276 }
1277
1278 static void
1279 vtn_get_builtin_location(struct vtn_builder *b,
1280 SpvBuiltIn builtin, int *location,
1281 nir_variable_mode *mode)
1282 {
1283 switch (builtin) {
1284 case SpvBuiltInPosition:
1285 *location = VARYING_SLOT_POS;
1286 break;
1287 case SpvBuiltInPointSize:
1288 *location = VARYING_SLOT_PSIZ;
1289 break;
1290 case SpvBuiltInClipDistance:
1291 *location = VARYING_SLOT_CLIP_DIST0; /* XXX CLIP_DIST1? */
1292 break;
1293 case SpvBuiltInCullDistance:
1294 *location = VARYING_SLOT_CULL_DIST0;
1295 break;
1296 case SpvBuiltInVertexId:
1297 case SpvBuiltInVertexIndex:
1298 /* The Vulkan spec defines VertexIndex to be non-zero-based and doesn't
1299 * allow VertexId. The ARB_gl_spirv spec defines VertexId to be the
1300 * same as gl_VertexID, which is non-zero-based, and removes
1301 * VertexIndex. Since they're both defined to be non-zero-based, we use
1302 * SYSTEM_VALUE_VERTEX_ID for both.
1303 */
1304 *location = SYSTEM_VALUE_VERTEX_ID;
1305 set_mode_system_value(b, mode);
1306 break;
1307 case SpvBuiltInInstanceIndex:
1308 *location = SYSTEM_VALUE_INSTANCE_INDEX;
1309 set_mode_system_value(b, mode);
1310 break;
1311 case SpvBuiltInInstanceId:
1312 *location = SYSTEM_VALUE_INSTANCE_ID;
1313 set_mode_system_value(b, mode);
1314 break;
1315 case SpvBuiltInPrimitiveId:
1316 if (b->shader->info.stage == MESA_SHADER_FRAGMENT) {
1317 vtn_assert(*mode == nir_var_shader_in);
1318 *location = VARYING_SLOT_PRIMITIVE_ID;
1319 } else if (*mode == nir_var_shader_out) {
1320 *location = VARYING_SLOT_PRIMITIVE_ID;
1321 } else {
1322 *location = SYSTEM_VALUE_PRIMITIVE_ID;
1323 set_mode_system_value(b, mode);
1324 }
1325 break;
1326 case SpvBuiltInInvocationId:
1327 *location = SYSTEM_VALUE_INVOCATION_ID;
1328 set_mode_system_value(b, mode);
1329 break;
1330 case SpvBuiltInLayer:
1331 *location = VARYING_SLOT_LAYER;
1332 if (b->shader->info.stage == MESA_SHADER_FRAGMENT)
1333 *mode = nir_var_shader_in;
1334 else if (b->shader->info.stage == MESA_SHADER_GEOMETRY)
1335 *mode = nir_var_shader_out;
1336 else if (b->options && b->options->caps.shader_viewport_index_layer &&
1337 (b->shader->info.stage == MESA_SHADER_VERTEX ||
1338 b->shader->info.stage == MESA_SHADER_TESS_EVAL))
1339 *mode = nir_var_shader_out;
1340 else
1341 vtn_fail("invalid stage for SpvBuiltInLayer");
1342 break;
1343 case SpvBuiltInViewportIndex:
1344 *location = VARYING_SLOT_VIEWPORT;
1345 if (b->shader->info.stage == MESA_SHADER_GEOMETRY)
1346 *mode = nir_var_shader_out;
1347 else if (b->options && b->options->caps.shader_viewport_index_layer &&
1348 (b->shader->info.stage == MESA_SHADER_VERTEX ||
1349 b->shader->info.stage == MESA_SHADER_TESS_EVAL))
1350 *mode = nir_var_shader_out;
1351 else if (b->shader->info.stage == MESA_SHADER_FRAGMENT)
1352 *mode = nir_var_shader_in;
1353 else
1354 vtn_fail("invalid stage for SpvBuiltInViewportIndex");
1355 break;
1356 case SpvBuiltInTessLevelOuter:
1357 *location = VARYING_SLOT_TESS_LEVEL_OUTER;
1358 break;
1359 case SpvBuiltInTessLevelInner:
1360 *location = VARYING_SLOT_TESS_LEVEL_INNER;
1361 break;
1362 case SpvBuiltInTessCoord:
1363 *location = SYSTEM_VALUE_TESS_COORD;
1364 set_mode_system_value(b, mode);
1365 break;
1366 case SpvBuiltInPatchVertices:
1367 *location = SYSTEM_VALUE_VERTICES_IN;
1368 set_mode_system_value(b, mode);
1369 break;
1370 case SpvBuiltInFragCoord:
1371 vtn_assert(*mode == nir_var_shader_in);
1372 if (b->options && b->options->frag_coord_is_sysval) {
1373 *mode = nir_var_system_value;
1374 *location = SYSTEM_VALUE_FRAG_COORD;
1375 } else {
1376 *location = VARYING_SLOT_POS;
1377 }
1378 break;
1379 case SpvBuiltInPointCoord:
1380 *location = VARYING_SLOT_PNTC;
1381 vtn_assert(*mode == nir_var_shader_in);
1382 break;
1383 case SpvBuiltInFrontFacing:
1384 *location = SYSTEM_VALUE_FRONT_FACE;
1385 set_mode_system_value(b, mode);
1386 break;
1387 case SpvBuiltInSampleId:
1388 *location = SYSTEM_VALUE_SAMPLE_ID;
1389 set_mode_system_value(b, mode);
1390 break;
1391 case SpvBuiltInSamplePosition:
1392 *location = SYSTEM_VALUE_SAMPLE_POS;
1393 set_mode_system_value(b, mode);
1394 break;
1395 case SpvBuiltInSampleMask:
1396 if (*mode == nir_var_shader_out) {
1397 *location = FRAG_RESULT_SAMPLE_MASK;
1398 } else {
1399 *location = SYSTEM_VALUE_SAMPLE_MASK_IN;
1400 set_mode_system_value(b, mode);
1401 }
1402 break;
1403 case SpvBuiltInFragDepth:
1404 *location = FRAG_RESULT_DEPTH;
1405 vtn_assert(*mode == nir_var_shader_out);
1406 break;
1407 case SpvBuiltInHelperInvocation:
1408 *location = SYSTEM_VALUE_HELPER_INVOCATION;
1409 set_mode_system_value(b, mode);
1410 break;
1411 case SpvBuiltInNumWorkgroups:
1412 *location = SYSTEM_VALUE_NUM_WORK_GROUPS;
1413 set_mode_system_value(b, mode);
1414 break;
1415 case SpvBuiltInWorkgroupSize:
1416 *location = SYSTEM_VALUE_LOCAL_GROUP_SIZE;
1417 set_mode_system_value(b, mode);
1418 break;
1419 case SpvBuiltInWorkgroupId:
1420 *location = SYSTEM_VALUE_WORK_GROUP_ID;
1421 set_mode_system_value(b, mode);
1422 break;
1423 case SpvBuiltInLocalInvocationId:
1424 *location = SYSTEM_VALUE_LOCAL_INVOCATION_ID;
1425 set_mode_system_value(b, mode);
1426 break;
1427 case SpvBuiltInLocalInvocationIndex:
1428 *location = SYSTEM_VALUE_LOCAL_INVOCATION_INDEX;
1429 set_mode_system_value(b, mode);
1430 break;
1431 case SpvBuiltInGlobalInvocationId:
1432 *location = SYSTEM_VALUE_GLOBAL_INVOCATION_ID;
1433 set_mode_system_value(b, mode);
1434 break;
1435 case SpvBuiltInGlobalLinearId:
1436 *location = SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX;
1437 set_mode_system_value(b, mode);
1438 break;
1439 case SpvBuiltInBaseVertex:
1440 /* OpenGL gl_BaseVertex (SYSTEM_VALUE_BASE_VERTEX) is not the same
1441 * semantic as Vulkan BaseVertex (SYSTEM_VALUE_FIRST_VERTEX).
1442 */
1443 if (b->options->environment == NIR_SPIRV_OPENGL)
1444 *location = SYSTEM_VALUE_BASE_VERTEX;
1445 else
1446 *location = SYSTEM_VALUE_FIRST_VERTEX;
1447 set_mode_system_value(b, mode);
1448 break;
1449 case SpvBuiltInBaseInstance:
1450 *location = SYSTEM_VALUE_BASE_INSTANCE;
1451 set_mode_system_value(b, mode);
1452 break;
1453 case SpvBuiltInDrawIndex:
1454 *location = SYSTEM_VALUE_DRAW_ID;
1455 set_mode_system_value(b, mode);
1456 break;
1457 case SpvBuiltInSubgroupSize:
1458 *location = SYSTEM_VALUE_SUBGROUP_SIZE;
1459 set_mode_system_value(b, mode);
1460 break;
1461 case SpvBuiltInSubgroupId:
1462 *location = SYSTEM_VALUE_SUBGROUP_ID;
1463 set_mode_system_value(b, mode);
1464 break;
1465 case SpvBuiltInSubgroupLocalInvocationId:
1466 *location = SYSTEM_VALUE_SUBGROUP_INVOCATION;
1467 set_mode_system_value(b, mode);
1468 break;
1469 case SpvBuiltInNumSubgroups:
1470 *location = SYSTEM_VALUE_NUM_SUBGROUPS;
1471 set_mode_system_value(b, mode);
1472 break;
1473 case SpvBuiltInDeviceIndex:
1474 *location = SYSTEM_VALUE_DEVICE_INDEX;
1475 set_mode_system_value(b, mode);
1476 break;
1477 case SpvBuiltInViewIndex:
1478 *location = SYSTEM_VALUE_VIEW_INDEX;
1479 set_mode_system_value(b, mode);
1480 break;
1481 case SpvBuiltInSubgroupEqMask:
1482 *location = SYSTEM_VALUE_SUBGROUP_EQ_MASK,
1483 set_mode_system_value(b, mode);
1484 break;
1485 case SpvBuiltInSubgroupGeMask:
1486 *location = SYSTEM_VALUE_SUBGROUP_GE_MASK,
1487 set_mode_system_value(b, mode);
1488 break;
1489 case SpvBuiltInSubgroupGtMask:
1490 *location = SYSTEM_VALUE_SUBGROUP_GT_MASK,
1491 set_mode_system_value(b, mode);
1492 break;
1493 case SpvBuiltInSubgroupLeMask:
1494 *location = SYSTEM_VALUE_SUBGROUP_LE_MASK,
1495 set_mode_system_value(b, mode);
1496 break;
1497 case SpvBuiltInSubgroupLtMask:
1498 *location = SYSTEM_VALUE_SUBGROUP_LT_MASK,
1499 set_mode_system_value(b, mode);
1500 break;
1501 case SpvBuiltInFragStencilRefEXT:
1502 *location = FRAG_RESULT_STENCIL;
1503 vtn_assert(*mode == nir_var_shader_out);
1504 break;
1505 case SpvBuiltInWorkDim:
1506 *location = SYSTEM_VALUE_WORK_DIM;
1507 set_mode_system_value(b, mode);
1508 break;
1509 case SpvBuiltInGlobalSize:
1510 *location = SYSTEM_VALUE_GLOBAL_GROUP_SIZE;
1511 set_mode_system_value(b, mode);
1512 break;
1513 default:
1514 vtn_fail("Unsupported builtin: %s (%u)",
1515 spirv_builtin_to_string(builtin), builtin);
1516 }
1517 }
1518
1519 static void
1520 apply_var_decoration(struct vtn_builder *b,
1521 struct nir_variable_data *var_data,
1522 const struct vtn_decoration *dec)
1523 {
1524 switch (dec->decoration) {
1525 case SpvDecorationRelaxedPrecision:
1526 break; /* FIXME: Do nothing with this for now. */
1527 case SpvDecorationNoPerspective:
1528 var_data->interpolation = INTERP_MODE_NOPERSPECTIVE;
1529 break;
1530 case SpvDecorationFlat:
1531 var_data->interpolation = INTERP_MODE_FLAT;
1532 break;
1533 case SpvDecorationCentroid:
1534 var_data->centroid = true;
1535 break;
1536 case SpvDecorationSample:
1537 var_data->sample = true;
1538 break;
1539 case SpvDecorationInvariant:
1540 var_data->invariant = true;
1541 break;
1542 case SpvDecorationConstant:
1543 var_data->read_only = true;
1544 break;
1545 case SpvDecorationNonReadable:
1546 var_data->access |= ACCESS_NON_READABLE;
1547 break;
1548 case SpvDecorationNonWritable:
1549 var_data->read_only = true;
1550 var_data->access |= ACCESS_NON_WRITEABLE;
1551 break;
1552 case SpvDecorationRestrict:
1553 var_data->access |= ACCESS_RESTRICT;
1554 break;
1555 case SpvDecorationVolatile:
1556 var_data->access |= ACCESS_VOLATILE;
1557 break;
1558 case SpvDecorationCoherent:
1559 var_data->access |= ACCESS_COHERENT;
1560 break;
1561 case SpvDecorationComponent:
1562 var_data->location_frac = dec->operands[0];
1563 break;
1564 case SpvDecorationIndex:
1565 var_data->index = dec->operands[0];
1566 break;
1567 case SpvDecorationBuiltIn: {
1568 SpvBuiltIn builtin = dec->operands[0];
1569
1570 nir_variable_mode mode = var_data->mode;
1571 vtn_get_builtin_location(b, builtin, &var_data->location, &mode);
1572 var_data->mode = mode;
1573
1574 switch (builtin) {
1575 case SpvBuiltInTessLevelOuter:
1576 case SpvBuiltInTessLevelInner:
1577 case SpvBuiltInClipDistance:
1578 case SpvBuiltInCullDistance:
1579 var_data->compact = true;
1580 break;
1581 default:
1582 break;
1583 }
1584 }
1585
1586 case SpvDecorationSpecId:
1587 case SpvDecorationRowMajor:
1588 case SpvDecorationColMajor:
1589 case SpvDecorationMatrixStride:
1590 case SpvDecorationAliased:
1591 case SpvDecorationUniform:
1592 case SpvDecorationUniformId:
1593 case SpvDecorationLinkageAttributes:
1594 break; /* Do nothing with these here */
1595
1596 case SpvDecorationPatch:
1597 var_data->patch = true;
1598 break;
1599
1600 case SpvDecorationLocation:
1601 vtn_fail("Handled above");
1602
1603 case SpvDecorationBlock:
1604 case SpvDecorationBufferBlock:
1605 case SpvDecorationArrayStride:
1606 case SpvDecorationGLSLShared:
1607 case SpvDecorationGLSLPacked:
1608 break; /* These can apply to a type but we don't care about them */
1609
1610 case SpvDecorationBinding:
1611 case SpvDecorationDescriptorSet:
1612 case SpvDecorationNoContraction:
1613 case SpvDecorationInputAttachmentIndex:
1614 vtn_warn("Decoration not allowed for variable or structure member: %s",
1615 spirv_decoration_to_string(dec->decoration));
1616 break;
1617
1618 case SpvDecorationXfbBuffer:
1619 var_data->explicit_xfb_buffer = true;
1620 var_data->xfb.buffer = dec->operands[0];
1621 var_data->always_active_io = true;
1622 break;
1623 case SpvDecorationXfbStride:
1624 var_data->explicit_xfb_stride = true;
1625 var_data->xfb.stride = dec->operands[0];
1626 break;
1627 case SpvDecorationOffset:
1628 var_data->explicit_offset = true;
1629 var_data->offset = dec->operands[0];
1630 break;
1631
1632 case SpvDecorationStream:
1633 var_data->stream = dec->operands[0];
1634 break;
1635
1636 case SpvDecorationCPacked:
1637 case SpvDecorationSaturatedConversion:
1638 case SpvDecorationFuncParamAttr:
1639 case SpvDecorationFPRoundingMode:
1640 case SpvDecorationFPFastMathMode:
1641 case SpvDecorationAlignment:
1642 if (b->shader->info.stage != MESA_SHADER_KERNEL) {
1643 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1644 spirv_decoration_to_string(dec->decoration));
1645 }
1646 break;
1647
1648 case SpvDecorationUserSemantic:
1649 /* User semantic decorations can safely be ignored by the driver. */
1650 break;
1651
1652 case SpvDecorationRestrictPointerEXT:
1653 case SpvDecorationAliasedPointerEXT:
1654 /* TODO: We should actually plumb alias information through NIR. */
1655 break;
1656
1657 default:
1658 vtn_fail_with_decoration("Unhandled decoration", dec->decoration);
1659 }
1660 }
1661
1662 static void
1663 var_is_patch_cb(struct vtn_builder *b, struct vtn_value *val, int member,
1664 const struct vtn_decoration *dec, void *out_is_patch)
1665 {
1666 if (dec->decoration == SpvDecorationPatch) {
1667 *((bool *) out_is_patch) = true;
1668 }
1669 }
1670
1671 static void
1672 var_decoration_cb(struct vtn_builder *b, struct vtn_value *val, int member,
1673 const struct vtn_decoration *dec, void *void_var)
1674 {
1675 struct vtn_variable *vtn_var = void_var;
1676
1677 /* Handle decorations that apply to a vtn_variable as a whole */
1678 switch (dec->decoration) {
1679 case SpvDecorationBinding:
1680 vtn_var->binding = dec->operands[0];
1681 vtn_var->explicit_binding = true;
1682 return;
1683 case SpvDecorationDescriptorSet:
1684 vtn_var->descriptor_set = dec->operands[0];
1685 return;
1686 case SpvDecorationInputAttachmentIndex:
1687 vtn_var->input_attachment_index = dec->operands[0];
1688 return;
1689 case SpvDecorationPatch:
1690 vtn_var->patch = true;
1691 break;
1692 case SpvDecorationOffset:
1693 vtn_var->offset = dec->operands[0];
1694 break;
1695 case SpvDecorationNonWritable:
1696 vtn_var->access |= ACCESS_NON_WRITEABLE;
1697 break;
1698 case SpvDecorationNonReadable:
1699 vtn_var->access |= ACCESS_NON_READABLE;
1700 break;
1701 case SpvDecorationVolatile:
1702 vtn_var->access |= ACCESS_VOLATILE;
1703 break;
1704 case SpvDecorationCoherent:
1705 vtn_var->access |= ACCESS_COHERENT;
1706 break;
1707 case SpvDecorationCounterBuffer:
1708 /* Counter buffer decorations can safely be ignored by the driver. */
1709 return;
1710 default:
1711 break;
1712 }
1713
1714 if (val->value_type == vtn_value_type_pointer) {
1715 assert(val->pointer->var == void_var);
1716 assert(member == -1);
1717 } else {
1718 assert(val->value_type == vtn_value_type_type);
1719 }
1720
1721 /* Location is odd. If applied to a split structure, we have to walk the
1722 * whole thing and accumulate the location. It's easier to handle as a
1723 * special case.
1724 */
1725 if (dec->decoration == SpvDecorationLocation) {
1726 unsigned location = dec->operands[0];
1727 if (b->shader->info.stage == MESA_SHADER_FRAGMENT &&
1728 vtn_var->mode == vtn_variable_mode_output) {
1729 location += FRAG_RESULT_DATA0;
1730 } else if (b->shader->info.stage == MESA_SHADER_VERTEX &&
1731 vtn_var->mode == vtn_variable_mode_input) {
1732 location += VERT_ATTRIB_GENERIC0;
1733 } else if (vtn_var->mode == vtn_variable_mode_input ||
1734 vtn_var->mode == vtn_variable_mode_output) {
1735 location += vtn_var->patch ? VARYING_SLOT_PATCH0 : VARYING_SLOT_VAR0;
1736 } else if (vtn_var->mode != vtn_variable_mode_uniform) {
1737 vtn_warn("Location must be on input, output, uniform, sampler or "
1738 "image variable");
1739 return;
1740 }
1741
1742 if (vtn_var->var->num_members == 0) {
1743 /* This handles the member and lone variable cases */
1744 vtn_var->var->data.location = location;
1745 } else {
1746 /* This handles the structure member case */
1747 assert(vtn_var->var->members);
1748
1749 if (member == -1)
1750 vtn_var->base_location = location;
1751 else
1752 vtn_var->var->members[member].location = location;
1753 }
1754
1755 return;
1756 } else {
1757 if (vtn_var->var) {
1758 if (vtn_var->var->num_members == 0) {
1759 /* We call this function on types as well as variables and not all
1760 * struct types get split so we can end up having stray member
1761 * decorations; just ignore them.
1762 */
1763 if (member == -1)
1764 apply_var_decoration(b, &vtn_var->var->data, dec);
1765 } else if (member >= 0) {
1766 /* Member decorations must come from a type */
1767 assert(val->value_type == vtn_value_type_type);
1768 apply_var_decoration(b, &vtn_var->var->members[member], dec);
1769 } else {
1770 unsigned length =
1771 glsl_get_length(glsl_without_array(vtn_var->type->type));
1772 for (unsigned i = 0; i < length; i++)
1773 apply_var_decoration(b, &vtn_var->var->members[i], dec);
1774 }
1775 } else {
1776 /* A few variables, those with external storage, have no actual
1777 * nir_variables associated with them. Fortunately, all decorations
1778 * we care about for those variables are on the type only.
1779 */
1780 vtn_assert(vtn_var->mode == vtn_variable_mode_ubo ||
1781 vtn_var->mode == vtn_variable_mode_ssbo ||
1782 vtn_var->mode == vtn_variable_mode_push_constant);
1783 }
1784 }
1785 }
1786
1787 enum vtn_variable_mode
1788 vtn_storage_class_to_mode(struct vtn_builder *b,
1789 SpvStorageClass class,
1790 struct vtn_type *interface_type,
1791 nir_variable_mode *nir_mode_out)
1792 {
1793 enum vtn_variable_mode mode;
1794 nir_variable_mode nir_mode;
1795 switch (class) {
1796 case SpvStorageClassUniform:
1797 /* Assume it's an UBO if we lack the interface_type. */
1798 if (!interface_type || interface_type->block) {
1799 mode = vtn_variable_mode_ubo;
1800 nir_mode = nir_var_mem_ubo;
1801 } else if (interface_type->buffer_block) {
1802 mode = vtn_variable_mode_ssbo;
1803 nir_mode = nir_var_mem_ssbo;
1804 } else {
1805 /* Default-block uniforms, coming from gl_spirv */
1806 mode = vtn_variable_mode_uniform;
1807 nir_mode = nir_var_uniform;
1808 }
1809 break;
1810 case SpvStorageClassStorageBuffer:
1811 mode = vtn_variable_mode_ssbo;
1812 nir_mode = nir_var_mem_ssbo;
1813 break;
1814 case SpvStorageClassPhysicalStorageBufferEXT:
1815 mode = vtn_variable_mode_phys_ssbo;
1816 nir_mode = nir_var_mem_global;
1817 break;
1818 case SpvStorageClassUniformConstant:
1819 if (b->shader->info.stage == MESA_SHADER_KERNEL) {
1820 if (b->options->constant_as_global) {
1821 mode = vtn_variable_mode_cross_workgroup;
1822 nir_mode = nir_var_mem_global;
1823 } else {
1824 mode = vtn_variable_mode_ubo;
1825 nir_mode = nir_var_mem_ubo;
1826 }
1827 } else {
1828 mode = vtn_variable_mode_uniform;
1829 nir_mode = nir_var_uniform;
1830 }
1831 break;
1832 case SpvStorageClassPushConstant:
1833 mode = vtn_variable_mode_push_constant;
1834 nir_mode = nir_var_uniform;
1835 break;
1836 case SpvStorageClassInput:
1837 mode = vtn_variable_mode_input;
1838 nir_mode = nir_var_shader_in;
1839 break;
1840 case SpvStorageClassOutput:
1841 mode = vtn_variable_mode_output;
1842 nir_mode = nir_var_shader_out;
1843 break;
1844 case SpvStorageClassPrivate:
1845 mode = vtn_variable_mode_private;
1846 nir_mode = nir_var_shader_temp;
1847 break;
1848 case SpvStorageClassFunction:
1849 mode = vtn_variable_mode_function;
1850 nir_mode = nir_var_function_temp;
1851 break;
1852 case SpvStorageClassWorkgroup:
1853 mode = vtn_variable_mode_workgroup;
1854 nir_mode = nir_var_mem_shared;
1855 break;
1856 case SpvStorageClassAtomicCounter:
1857 mode = vtn_variable_mode_uniform;
1858 nir_mode = nir_var_uniform;
1859 break;
1860 case SpvStorageClassCrossWorkgroup:
1861 mode = vtn_variable_mode_cross_workgroup;
1862 nir_mode = nir_var_mem_global;
1863 break;
1864 case SpvStorageClassImage:
1865 mode = vtn_variable_mode_image;
1866 nir_mode = nir_var_mem_ubo;
1867 break;
1868 case SpvStorageClassGeneric:
1869 default:
1870 vtn_fail("Unhandled variable storage class: %s (%u)",
1871 spirv_storageclass_to_string(class), class);
1872 }
1873
1874 if (nir_mode_out)
1875 *nir_mode_out = nir_mode;
1876
1877 return mode;
1878 }
1879
1880 nir_address_format
1881 vtn_mode_to_address_format(struct vtn_builder *b, enum vtn_variable_mode mode)
1882 {
1883 switch (mode) {
1884 case vtn_variable_mode_ubo:
1885 return b->options->ubo_addr_format;
1886
1887 case vtn_variable_mode_ssbo:
1888 return b->options->ssbo_addr_format;
1889
1890 case vtn_variable_mode_phys_ssbo:
1891 return b->options->phys_ssbo_addr_format;
1892
1893 case vtn_variable_mode_push_constant:
1894 return b->options->push_const_addr_format;
1895
1896 case vtn_variable_mode_workgroup:
1897 return b->options->shared_addr_format;
1898
1899 case vtn_variable_mode_cross_workgroup:
1900 return b->options->global_addr_format;
1901
1902 case vtn_variable_mode_function:
1903 if (b->physical_ptrs)
1904 return b->options->temp_addr_format;
1905 /* Fall through. */
1906
1907 case vtn_variable_mode_private:
1908 case vtn_variable_mode_uniform:
1909 case vtn_variable_mode_input:
1910 case vtn_variable_mode_output:
1911 case vtn_variable_mode_image:
1912 return nir_address_format_logical;
1913 }
1914
1915 unreachable("Invalid variable mode");
1916 }
1917
1918 nir_ssa_def *
1919 vtn_pointer_to_ssa(struct vtn_builder *b, struct vtn_pointer *ptr)
1920 {
1921 if (vtn_pointer_uses_ssa_offset(b, ptr)) {
1922 /* This pointer needs to have a pointer type with actual storage */
1923 vtn_assert(ptr->ptr_type);
1924 vtn_assert(ptr->ptr_type->type);
1925
1926 if (!ptr->offset) {
1927 /* If we don't have an offset then we must be a pointer to the variable
1928 * itself.
1929 */
1930 vtn_assert(!ptr->offset && !ptr->block_index);
1931
1932 struct vtn_access_chain chain = {
1933 .length = 0,
1934 };
1935 ptr = vtn_ssa_offset_pointer_dereference(b, ptr, &chain);
1936 }
1937
1938 vtn_assert(ptr->offset);
1939 if (ptr->block_index) {
1940 vtn_assert(ptr->mode == vtn_variable_mode_ubo ||
1941 ptr->mode == vtn_variable_mode_ssbo);
1942 return nir_vec2(&b->nb, ptr->block_index, ptr->offset);
1943 } else {
1944 vtn_assert(ptr->mode == vtn_variable_mode_workgroup);
1945 return ptr->offset;
1946 }
1947 } else {
1948 if (vtn_pointer_is_external_block(b, ptr) &&
1949 vtn_type_contains_block(b, ptr->type) &&
1950 ptr->mode != vtn_variable_mode_phys_ssbo) {
1951 /* In this case, we're looking for a block index and not an actual
1952 * deref.
1953 *
1954 * For PhysicalStorageBufferEXT pointers, we don't have a block index
1955 * at all because we get the pointer directly from the client. This
1956 * assumes that there will never be a SSBO binding variable using the
1957 * PhysicalStorageBufferEXT storage class. This assumption appears
1958 * to be correct according to the Vulkan spec because the table,
1959 * "Shader Resource and Storage Class Correspondence," the only the
1960 * Uniform storage class with BufferBlock or the StorageBuffer
1961 * storage class with Block can be used.
1962 */
1963 if (!ptr->block_index) {
1964 /* If we don't have a block_index then we must be a pointer to the
1965 * variable itself.
1966 */
1967 vtn_assert(!ptr->deref);
1968
1969 struct vtn_access_chain chain = {
1970 .length = 0,
1971 };
1972 ptr = vtn_nir_deref_pointer_dereference(b, ptr, &chain);
1973 }
1974
1975 return ptr->block_index;
1976 } else {
1977 return &vtn_pointer_to_deref(b, ptr)->dest.ssa;
1978 }
1979 }
1980 }
1981
1982 struct vtn_pointer *
1983 vtn_pointer_from_ssa(struct vtn_builder *b, nir_ssa_def *ssa,
1984 struct vtn_type *ptr_type)
1985 {
1986 vtn_assert(ptr_type->base_type == vtn_base_type_pointer);
1987
1988 struct vtn_pointer *ptr = rzalloc(b, struct vtn_pointer);
1989 struct vtn_type *without_array =
1990 vtn_type_without_array(ptr_type->deref);
1991
1992 nir_variable_mode nir_mode;
1993 ptr->mode = vtn_storage_class_to_mode(b, ptr_type->storage_class,
1994 without_array, &nir_mode);
1995 ptr->type = ptr_type->deref;
1996 ptr->ptr_type = ptr_type;
1997
1998 if (b->wa_glslang_179) {
1999 /* To work around https://github.com/KhronosGroup/glslang/issues/179 we
2000 * need to whack the mode because it creates a function parameter with
2001 * the Function storage class even though it's a pointer to a sampler.
2002 * If we don't do this, then NIR won't get rid of the deref_cast for us.
2003 */
2004 if (ptr->mode == vtn_variable_mode_function &&
2005 (ptr->type->base_type == vtn_base_type_sampler ||
2006 ptr->type->base_type == vtn_base_type_sampled_image)) {
2007 ptr->mode = vtn_variable_mode_uniform;
2008 nir_mode = nir_var_uniform;
2009 }
2010 }
2011
2012 if (vtn_pointer_uses_ssa_offset(b, ptr)) {
2013 /* This pointer type needs to have actual storage */
2014 vtn_assert(ptr_type->type);
2015 if (ptr->mode == vtn_variable_mode_ubo ||
2016 ptr->mode == vtn_variable_mode_ssbo) {
2017 vtn_assert(ssa->num_components == 2);
2018 ptr->block_index = nir_channel(&b->nb, ssa, 0);
2019 ptr->offset = nir_channel(&b->nb, ssa, 1);
2020 } else {
2021 vtn_assert(ssa->num_components == 1);
2022 ptr->block_index = NULL;
2023 ptr->offset = ssa;
2024 }
2025 } else {
2026 const struct glsl_type *deref_type = ptr_type->deref->type;
2027 if (!vtn_pointer_is_external_block(b, ptr)) {
2028 ptr->deref = nir_build_deref_cast(&b->nb, ssa, nir_mode,
2029 deref_type, ptr_type->stride);
2030 } else if (vtn_type_contains_block(b, ptr->type) &&
2031 ptr->mode != vtn_variable_mode_phys_ssbo) {
2032 /* This is a pointer to somewhere in an array of blocks, not a
2033 * pointer to somewhere inside the block. Set the block index
2034 * instead of making a cast.
2035 */
2036 ptr->block_index = ssa;
2037 } else {
2038 /* This is a pointer to something internal or a pointer inside a
2039 * block. It's just a regular cast.
2040 *
2041 * For PhysicalStorageBufferEXT pointers, we don't have a block index
2042 * at all because we get the pointer directly from the client. This
2043 * assumes that there will never be a SSBO binding variable using the
2044 * PhysicalStorageBufferEXT storage class. This assumption appears
2045 * to be correct according to the Vulkan spec because the table,
2046 * "Shader Resource and Storage Class Correspondence," the only the
2047 * Uniform storage class with BufferBlock or the StorageBuffer
2048 * storage class with Block can be used.
2049 */
2050 ptr->deref = nir_build_deref_cast(&b->nb, ssa, nir_mode,
2051 ptr_type->deref->type,
2052 ptr_type->stride);
2053 ptr->deref->dest.ssa.num_components =
2054 glsl_get_vector_elements(ptr_type->type);
2055 ptr->deref->dest.ssa.bit_size = glsl_get_bit_size(ptr_type->type);
2056 }
2057 }
2058
2059 return ptr;
2060 }
2061
2062 static bool
2063 is_per_vertex_inout(const struct vtn_variable *var, gl_shader_stage stage)
2064 {
2065 if (var->patch || !glsl_type_is_array(var->type->type))
2066 return false;
2067
2068 if (var->mode == vtn_variable_mode_input) {
2069 return stage == MESA_SHADER_TESS_CTRL ||
2070 stage == MESA_SHADER_TESS_EVAL ||
2071 stage == MESA_SHADER_GEOMETRY;
2072 }
2073
2074 if (var->mode == vtn_variable_mode_output)
2075 return stage == MESA_SHADER_TESS_CTRL;
2076
2077 return false;
2078 }
2079
2080 static void
2081 assign_missing_member_locations(struct vtn_variable *var)
2082 {
2083 unsigned length =
2084 glsl_get_length(glsl_without_array(var->type->type));
2085 int location = var->base_location;
2086
2087 for (unsigned i = 0; i < length; i++) {
2088 /* From the Vulkan spec:
2089 *
2090 * “If the structure type is a Block but without a Location, then each
2091 * of its members must have a Location decoration.”
2092 *
2093 */
2094 if (var->type->block) {
2095 assert(var->base_location != -1 ||
2096 var->var->members[i].location != -1);
2097 }
2098
2099 /* From the Vulkan spec:
2100 *
2101 * “Any member with its own Location decoration is assigned that
2102 * location. Each remaining member is assigned the location after the
2103 * immediately preceding member in declaration order.”
2104 */
2105 if (var->var->members[i].location != -1)
2106 location = var->var->members[i].location;
2107 else
2108 var->var->members[i].location = location;
2109
2110 /* Below we use type instead of interface_type, because interface_type
2111 * is only available when it is a Block. This code also supports
2112 * input/outputs that are just structs
2113 */
2114 const struct glsl_type *member_type =
2115 glsl_get_struct_field(glsl_without_array(var->type->type), i);
2116
2117 location +=
2118 glsl_count_attribute_slots(member_type,
2119 false /* is_gl_vertex_input */);
2120 }
2121 }
2122
2123
2124 static void
2125 vtn_create_variable(struct vtn_builder *b, struct vtn_value *val,
2126 struct vtn_type *ptr_type, SpvStorageClass storage_class,
2127 nir_constant *initializer)
2128 {
2129 vtn_assert(ptr_type->base_type == vtn_base_type_pointer);
2130 struct vtn_type *type = ptr_type->deref;
2131
2132 struct vtn_type *without_array = vtn_type_without_array(ptr_type->deref);
2133
2134 enum vtn_variable_mode mode;
2135 nir_variable_mode nir_mode;
2136 mode = vtn_storage_class_to_mode(b, storage_class, without_array, &nir_mode);
2137
2138 switch (mode) {
2139 case vtn_variable_mode_ubo:
2140 /* There's no other way to get vtn_variable_mode_ubo */
2141 vtn_assert(without_array->block);
2142 b->shader->info.num_ubos++;
2143 break;
2144 case vtn_variable_mode_ssbo:
2145 if (storage_class == SpvStorageClassStorageBuffer &&
2146 !without_array->block) {
2147 if (b->variable_pointers) {
2148 vtn_fail("Variables in the StorageBuffer storage class must "
2149 "have a struct type with the Block decoration");
2150 } else {
2151 /* If variable pointers are not present, it's still malformed
2152 * SPIR-V but we can parse it and do the right thing anyway.
2153 * Since some of the 8-bit storage tests have bugs in this are,
2154 * just make it a warning for now.
2155 */
2156 vtn_warn("Variables in the StorageBuffer storage class must "
2157 "have a struct type with the Block decoration");
2158 }
2159 }
2160 b->shader->info.num_ssbos++;
2161 break;
2162 case vtn_variable_mode_uniform:
2163 if (glsl_type_is_image(without_array->type))
2164 b->shader->info.num_images++;
2165 else if (glsl_type_is_sampler(without_array->type))
2166 b->shader->info.num_textures++;
2167 break;
2168 case vtn_variable_mode_push_constant:
2169 b->shader->num_uniforms = vtn_type_block_size(b, type);
2170 break;
2171
2172 case vtn_variable_mode_image:
2173 vtn_fail("Cannot create a variable with the Image storage class");
2174 break;
2175
2176 case vtn_variable_mode_phys_ssbo:
2177 vtn_fail("Cannot create a variable with the "
2178 "PhysicalStorageBufferEXT storage class");
2179 break;
2180
2181 default:
2182 /* No tallying is needed */
2183 break;
2184 }
2185
2186 struct vtn_variable *var = rzalloc(b, struct vtn_variable);
2187 var->type = type;
2188 var->mode = mode;
2189 var->base_location = -1;
2190
2191 vtn_assert(val->value_type == vtn_value_type_pointer);
2192 val->pointer = vtn_pointer_for_variable(b, var, ptr_type);
2193
2194 switch (var->mode) {
2195 case vtn_variable_mode_function:
2196 case vtn_variable_mode_private:
2197 case vtn_variable_mode_uniform:
2198 /* For these, we create the variable normally */
2199 var->var = rzalloc(b->shader, nir_variable);
2200 var->var->name = ralloc_strdup(var->var, val->name);
2201
2202 if (storage_class == SpvStorageClassAtomicCounter) {
2203 /* Need to tweak the nir type here as at vtn_handle_type we don't
2204 * have the access to storage_class, that is the one that points us
2205 * that is an atomic uint.
2206 */
2207 var->var->type = repair_atomic_type(var->type->type);
2208 } else {
2209 /* Private variables don't have any explicit layout but some layouts
2210 * may have leaked through due to type deduplication in the SPIR-V.
2211 */
2212 var->var->type = var->type->type;
2213 }
2214 var->var->data.mode = nir_mode;
2215 var->var->data.location = -1;
2216 var->var->interface_type = NULL;
2217 break;
2218
2219 case vtn_variable_mode_ubo:
2220 case vtn_variable_mode_ssbo:
2221 var->var = rzalloc(b->shader, nir_variable);
2222 var->var->name = ralloc_strdup(var->var, val->name);
2223
2224 var->var->type = var->type->type;
2225 var->var->interface_type = var->type->type;
2226
2227 var->var->data.mode = nir_mode;
2228 var->var->data.location = -1;
2229
2230 break;
2231
2232 case vtn_variable_mode_workgroup:
2233 /* Create the variable normally */
2234 var->var = rzalloc(b->shader, nir_variable);
2235 var->var->name = ralloc_strdup(var->var, val->name);
2236 /* Workgroup variables don't have any explicit layout but some
2237 * layouts may have leaked through due to type deduplication in the
2238 * SPIR-V.
2239 */
2240 var->var->type = var->type->type;
2241 var->var->data.mode = nir_var_mem_shared;
2242 break;
2243
2244 case vtn_variable_mode_input:
2245 case vtn_variable_mode_output: {
2246 /* In order to know whether or not we're a per-vertex inout, we need
2247 * the patch qualifier. This means walking the variable decorations
2248 * early before we actually create any variables. Not a big deal.
2249 *
2250 * GLSLang really likes to place decorations in the most interior
2251 * thing it possibly can. In particular, if you have a struct, it
2252 * will place the patch decorations on the struct members. This
2253 * should be handled by the variable splitting below just fine.
2254 *
2255 * If you have an array-of-struct, things get even more weird as it
2256 * will place the patch decorations on the struct even though it's
2257 * inside an array and some of the members being patch and others not
2258 * makes no sense whatsoever. Since the only sensible thing is for
2259 * it to be all or nothing, we'll call it patch if any of the members
2260 * are declared patch.
2261 */
2262 var->patch = false;
2263 vtn_foreach_decoration(b, val, var_is_patch_cb, &var->patch);
2264 if (glsl_type_is_array(var->type->type) &&
2265 glsl_type_is_struct_or_ifc(without_array->type)) {
2266 vtn_foreach_decoration(b, vtn_value(b, without_array->id,
2267 vtn_value_type_type),
2268 var_is_patch_cb, &var->patch);
2269 }
2270
2271 /* For inputs and outputs, we immediately split structures. This
2272 * is for a couple of reasons. For one, builtins may all come in
2273 * a struct and we really want those split out into separate
2274 * variables. For another, interpolation qualifiers can be
2275 * applied to members of the top-level struct ane we need to be
2276 * able to preserve that information.
2277 */
2278
2279 struct vtn_type *per_vertex_type = var->type;
2280 if (is_per_vertex_inout(var, b->shader->info.stage)) {
2281 /* In Geometry shaders (and some tessellation), inputs come
2282 * in per-vertex arrays. However, some builtins come in
2283 * non-per-vertex, hence the need for the is_array check. In
2284 * any case, there are no non-builtin arrays allowed so this
2285 * check should be sufficient.
2286 */
2287 per_vertex_type = var->type->array_element;
2288 }
2289
2290 var->var = rzalloc(b->shader, nir_variable);
2291 var->var->name = ralloc_strdup(var->var, val->name);
2292 /* In Vulkan, shader I/O variables don't have any explicit layout but
2293 * some layouts may have leaked through due to type deduplication in
2294 * the SPIR-V. We do, however, keep the layouts in the variable's
2295 * interface_type because we need offsets for XFB arrays of blocks.
2296 */
2297 var->var->type = var->type->type;
2298 var->var->data.mode = nir_mode;
2299 var->var->data.patch = var->patch;
2300
2301 /* Figure out the interface block type. */
2302 struct vtn_type *iface_type = per_vertex_type;
2303 if (var->mode == vtn_variable_mode_output &&
2304 (b->shader->info.stage == MESA_SHADER_VERTEX ||
2305 b->shader->info.stage == MESA_SHADER_TESS_EVAL ||
2306 b->shader->info.stage == MESA_SHADER_GEOMETRY)) {
2307 /* For vertex data outputs, we can end up with arrays of blocks for
2308 * transform feedback where each array element corresponds to a
2309 * different XFB output buffer.
2310 */
2311 while (iface_type->base_type == vtn_base_type_array)
2312 iface_type = iface_type->array_element;
2313 }
2314 if (iface_type->base_type == vtn_base_type_struct && iface_type->block)
2315 var->var->interface_type = iface_type->type;
2316
2317 if (per_vertex_type->base_type == vtn_base_type_struct &&
2318 per_vertex_type->block) {
2319 /* It's a struct. Set it up as per-member. */
2320 var->var->num_members = glsl_get_length(per_vertex_type->type);
2321 var->var->members = rzalloc_array(var->var, struct nir_variable_data,
2322 var->var->num_members);
2323
2324 for (unsigned i = 0; i < var->var->num_members; i++) {
2325 var->var->members[i].mode = nir_mode;
2326 var->var->members[i].patch = var->patch;
2327 var->var->members[i].location = -1;
2328 }
2329 }
2330
2331 /* For inputs and outputs, we need to grab locations and builtin
2332 * information from the per-vertex type.
2333 */
2334 vtn_foreach_decoration(b, vtn_value(b, per_vertex_type->id,
2335 vtn_value_type_type),
2336 var_decoration_cb, var);
2337 break;
2338 }
2339
2340 case vtn_variable_mode_push_constant:
2341 case vtn_variable_mode_cross_workgroup:
2342 /* These don't need actual variables. */
2343 break;
2344
2345 case vtn_variable_mode_image:
2346 case vtn_variable_mode_phys_ssbo:
2347 unreachable("Should have been caught before");
2348 }
2349
2350 if (initializer) {
2351 var->var->constant_initializer =
2352 nir_constant_clone(initializer, var->var);
2353 }
2354
2355 vtn_foreach_decoration(b, val, var_decoration_cb, var);
2356 vtn_foreach_decoration(b, val, ptr_decoration_cb, val->pointer);
2357
2358 if ((var->mode == vtn_variable_mode_input ||
2359 var->mode == vtn_variable_mode_output) &&
2360 var->var->members) {
2361 assign_missing_member_locations(var);
2362 }
2363
2364 if (var->mode == vtn_variable_mode_uniform ||
2365 var->mode == vtn_variable_mode_ubo ||
2366 var->mode == vtn_variable_mode_ssbo) {
2367 /* XXX: We still need the binding information in the nir_variable
2368 * for these. We should fix that.
2369 */
2370 var->var->data.binding = var->binding;
2371 var->var->data.explicit_binding = var->explicit_binding;
2372 var->var->data.descriptor_set = var->descriptor_set;
2373 var->var->data.index = var->input_attachment_index;
2374 var->var->data.offset = var->offset;
2375
2376 if (glsl_type_is_image(without_array->type))
2377 var->var->data.image.format = without_array->image_format;
2378 }
2379
2380 if (var->mode == vtn_variable_mode_function) {
2381 vtn_assert(var->var != NULL && var->var->members == NULL);
2382 nir_function_impl_add_variable(b->nb.impl, var->var);
2383 } else if (var->var) {
2384 nir_shader_add_variable(b->shader, var->var);
2385 } else {
2386 vtn_assert(vtn_pointer_is_external_block(b, val->pointer));
2387 }
2388 }
2389
2390 static void
2391 vtn_assert_types_equal(struct vtn_builder *b, SpvOp opcode,
2392 struct vtn_type *dst_type,
2393 struct vtn_type *src_type)
2394 {
2395 if (dst_type->id == src_type->id)
2396 return;
2397
2398 if (vtn_types_compatible(b, dst_type, src_type)) {
2399 /* Early versions of GLSLang would re-emit types unnecessarily and you
2400 * would end up with OpLoad, OpStore, or OpCopyMemory opcodes which have
2401 * mismatched source and destination types.
2402 *
2403 * https://github.com/KhronosGroup/glslang/issues/304
2404 * https://github.com/KhronosGroup/glslang/issues/307
2405 * https://bugs.freedesktop.org/show_bug.cgi?id=104338
2406 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2407 */
2408 vtn_warn("Source and destination types of %s do not have the same "
2409 "ID (but are compatible): %u vs %u",
2410 spirv_op_to_string(opcode), dst_type->id, src_type->id);
2411 return;
2412 }
2413
2414 vtn_fail("Source and destination types of %s do not match: %s vs. %s",
2415 spirv_op_to_string(opcode),
2416 glsl_get_type_name(dst_type->type),
2417 glsl_get_type_name(src_type->type));
2418 }
2419
2420 static nir_ssa_def *
2421 nir_shrink_zero_pad_vec(nir_builder *b, nir_ssa_def *val,
2422 unsigned num_components)
2423 {
2424 if (val->num_components == num_components)
2425 return val;
2426
2427 nir_ssa_def *comps[NIR_MAX_VEC_COMPONENTS];
2428 for (unsigned i = 0; i < num_components; i++) {
2429 if (i < val->num_components)
2430 comps[i] = nir_channel(b, val, i);
2431 else
2432 comps[i] = nir_imm_intN_t(b, 0, val->bit_size);
2433 }
2434 return nir_vec(b, comps, num_components);
2435 }
2436
2437 static nir_ssa_def *
2438 nir_sloppy_bitcast(nir_builder *b, nir_ssa_def *val,
2439 const struct glsl_type *type)
2440 {
2441 const unsigned num_components = glsl_get_vector_elements(type);
2442 const unsigned bit_size = glsl_get_bit_size(type);
2443
2444 /* First, zero-pad to ensure that the value is big enough that when we
2445 * bit-cast it, we don't loose anything.
2446 */
2447 if (val->bit_size < bit_size) {
2448 const unsigned src_num_components_needed =
2449 vtn_align_u32(val->num_components, bit_size / val->bit_size);
2450 val = nir_shrink_zero_pad_vec(b, val, src_num_components_needed);
2451 }
2452
2453 val = nir_bitcast_vector(b, val, bit_size);
2454
2455 return nir_shrink_zero_pad_vec(b, val, num_components);
2456 }
2457
2458 void
2459 vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
2460 const uint32_t *w, unsigned count)
2461 {
2462 switch (opcode) {
2463 case SpvOpUndef: {
2464 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_undef);
2465 val->type = vtn_value(b, w[1], vtn_value_type_type)->type;
2466 break;
2467 }
2468
2469 case SpvOpVariable: {
2470 struct vtn_type *ptr_type = vtn_value(b, w[1], vtn_value_type_type)->type;
2471
2472 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_pointer);
2473
2474 SpvStorageClass storage_class = w[3];
2475 nir_constant *initializer = NULL;
2476 if (count > 4)
2477 initializer = vtn_value(b, w[4], vtn_value_type_constant)->constant;
2478
2479 vtn_create_variable(b, val, ptr_type, storage_class, initializer);
2480 break;
2481 }
2482
2483 case SpvOpAccessChain:
2484 case SpvOpPtrAccessChain:
2485 case SpvOpInBoundsAccessChain:
2486 case SpvOpInBoundsPtrAccessChain: {
2487 struct vtn_access_chain *chain = vtn_access_chain_create(b, count - 4);
2488 enum gl_access_qualifier access = 0;
2489 chain->ptr_as_array = (opcode == SpvOpPtrAccessChain || opcode == SpvOpInBoundsPtrAccessChain);
2490
2491 unsigned idx = 0;
2492 for (int i = 4; i < count; i++) {
2493 struct vtn_value *link_val = vtn_untyped_value(b, w[i]);
2494 if (link_val->value_type == vtn_value_type_constant) {
2495 chain->link[idx].mode = vtn_access_mode_literal;
2496 chain->link[idx].id = vtn_constant_int(b, w[i]);
2497 } else {
2498 chain->link[idx].mode = vtn_access_mode_id;
2499 chain->link[idx].id = w[i];
2500 }
2501 access |= vtn_value_access(link_val);
2502 idx++;
2503 }
2504
2505 struct vtn_type *ptr_type = vtn_value(b, w[1], vtn_value_type_type)->type;
2506 struct vtn_value *base_val = vtn_untyped_value(b, w[3]);
2507 if (base_val->value_type == vtn_value_type_sampled_image) {
2508 /* This is rather insane. SPIR-V allows you to use OpSampledImage
2509 * to combine an array of images with a single sampler to get an
2510 * array of sampled images that all share the same sampler.
2511 * Fortunately, this means that we can more-or-less ignore the
2512 * sampler when crawling the access chain, but it does leave us
2513 * with this rather awkward little special-case.
2514 */
2515 struct vtn_value *val =
2516 vtn_push_value(b, w[2], vtn_value_type_sampled_image);
2517 val->sampled_image = ralloc(b, struct vtn_sampled_image);
2518 val->sampled_image->image =
2519 vtn_pointer_dereference(b, base_val->sampled_image->image, chain);
2520 val->sampled_image->sampler = base_val->sampled_image->sampler;
2521 val->sampled_image->image =
2522 vtn_decorate_pointer(b, val, val->sampled_image->image);
2523 val->sampled_image->sampler =
2524 vtn_decorate_pointer(b, val, val->sampled_image->sampler);
2525 } else {
2526 vtn_assert(base_val->value_type == vtn_value_type_pointer);
2527 struct vtn_pointer *ptr =
2528 vtn_pointer_dereference(b, base_val->pointer, chain);
2529 ptr->ptr_type = ptr_type;
2530 ptr->access |= access;
2531 vtn_push_value_pointer(b, w[2], ptr);
2532 }
2533 break;
2534 }
2535
2536 case SpvOpCopyMemory: {
2537 struct vtn_value *dest = vtn_value(b, w[1], vtn_value_type_pointer);
2538 struct vtn_value *src = vtn_value(b, w[2], vtn_value_type_pointer);
2539
2540 vtn_assert_types_equal(b, opcode, dest->type->deref, src->type->deref);
2541
2542 vtn_variable_copy(b, dest->pointer, src->pointer);
2543 break;
2544 }
2545
2546 case SpvOpLoad: {
2547 struct vtn_type *res_type =
2548 vtn_value(b, w[1], vtn_value_type_type)->type;
2549 struct vtn_value *src_val = vtn_value(b, w[3], vtn_value_type_pointer);
2550 struct vtn_pointer *src = src_val->pointer;
2551
2552 vtn_assert_types_equal(b, opcode, res_type, src_val->type->deref);
2553
2554 if (res_type->base_type == vtn_base_type_image ||
2555 res_type->base_type == vtn_base_type_sampler) {
2556 vtn_push_value_pointer(b, w[2], src);
2557 return;
2558 } else if (res_type->base_type == vtn_base_type_sampled_image) {
2559 struct vtn_value *val =
2560 vtn_push_value(b, w[2], vtn_value_type_sampled_image);
2561 val->sampled_image = ralloc(b, struct vtn_sampled_image);
2562 val->sampled_image->image = val->sampled_image->sampler =
2563 vtn_decorate_pointer(b, val, src);
2564 return;
2565 }
2566
2567 if (count > 4) {
2568 unsigned idx = 5;
2569 SpvMemoryAccessMask access = w[4];
2570 if (access & SpvMemoryAccessAlignedMask)
2571 idx++;
2572
2573 if (access & SpvMemoryAccessMakePointerVisibleMask) {
2574 SpvMemorySemanticsMask semantics =
2575 SpvMemorySemanticsMakeVisibleMask |
2576 vtn_storage_class_to_memory_semantics(src->ptr_type->storage_class);
2577
2578 SpvScope scope = vtn_constant_uint(b, w[idx]);
2579 vtn_emit_memory_barrier(b, scope, semantics);
2580 }
2581 }
2582
2583 vtn_push_ssa(b, w[2], res_type, vtn_variable_load(b, src));
2584 break;
2585 }
2586
2587 case SpvOpStore: {
2588 struct vtn_value *dest_val = vtn_value(b, w[1], vtn_value_type_pointer);
2589 struct vtn_pointer *dest = dest_val->pointer;
2590 struct vtn_value *src_val = vtn_untyped_value(b, w[2]);
2591
2592 /* OpStore requires us to actually have a storage type */
2593 vtn_fail_if(dest->type->type == NULL,
2594 "Invalid destination type for OpStore");
2595
2596 if (glsl_get_base_type(dest->type->type) == GLSL_TYPE_BOOL &&
2597 glsl_get_base_type(src_val->type->type) == GLSL_TYPE_UINT) {
2598 /* Early versions of GLSLang would use uint types for UBOs/SSBOs but
2599 * would then store them to a local variable as bool. Work around
2600 * the issue by doing an implicit conversion.
2601 *
2602 * https://github.com/KhronosGroup/glslang/issues/170
2603 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2604 */
2605 vtn_warn("OpStore of value of type OpTypeInt to a pointer to type "
2606 "OpTypeBool. Doing an implicit conversion to work around "
2607 "the problem.");
2608 struct vtn_ssa_value *bool_ssa =
2609 vtn_create_ssa_value(b, dest->type->type);
2610 bool_ssa->def = nir_i2b(&b->nb, vtn_ssa_value(b, w[2])->def);
2611 vtn_variable_store(b, bool_ssa, dest);
2612 break;
2613 }
2614
2615 vtn_assert_types_equal(b, opcode, dest_val->type->deref, src_val->type);
2616
2617 if (glsl_type_is_sampler(dest->type->type)) {
2618 if (b->wa_glslang_179) {
2619 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
2620 "propagation to workaround the problem.");
2621 vtn_assert(dest->var->copy_prop_sampler == NULL);
2622 struct vtn_value *v = vtn_untyped_value(b, w[2]);
2623 if (v->value_type == vtn_value_type_sampled_image) {
2624 dest->var->copy_prop_sampler = v->sampled_image->sampler;
2625 } else {
2626 vtn_assert(v->value_type == vtn_value_type_pointer);
2627 dest->var->copy_prop_sampler = v->pointer;
2628 }
2629 } else {
2630 vtn_fail("Vulkan does not allow OpStore of a sampler or image.");
2631 }
2632 break;
2633 }
2634
2635 struct vtn_ssa_value *src = vtn_ssa_value(b, w[2]);
2636 vtn_variable_store(b, src, dest);
2637
2638 if (count > 3) {
2639 unsigned idx = 4;
2640 SpvMemoryAccessMask access = w[3];
2641
2642 if (access & SpvMemoryAccessAlignedMask)
2643 idx++;
2644
2645 if (access & SpvMemoryAccessMakePointerAvailableMask) {
2646 SpvMemorySemanticsMask semantics =
2647 SpvMemorySemanticsMakeAvailableMask |
2648 vtn_storage_class_to_memory_semantics(dest->ptr_type->storage_class);
2649 SpvScope scope = vtn_constant_uint(b, w[idx]);
2650 vtn_emit_memory_barrier(b, scope, semantics);
2651 }
2652 }
2653 break;
2654 }
2655
2656 case SpvOpArrayLength: {
2657 struct vtn_pointer *ptr =
2658 vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
2659 const uint32_t field = w[4];
2660
2661 vtn_fail_if(ptr->type->base_type != vtn_base_type_struct,
2662 "OpArrayLength must take a pointer to a structure type");
2663 vtn_fail_if(field != ptr->type->length - 1 ||
2664 ptr->type->members[field]->base_type != vtn_base_type_array,
2665 "OpArrayLength must reference the last memeber of the "
2666 "structure and that must be an array");
2667
2668 const uint32_t offset = ptr->type->offsets[field];
2669 const uint32_t stride = ptr->type->members[field]->stride;
2670
2671 if (!ptr->block_index) {
2672 struct vtn_access_chain chain = {
2673 .length = 0,
2674 };
2675 ptr = vtn_pointer_dereference(b, ptr, &chain);
2676 vtn_assert(ptr->block_index);
2677 }
2678
2679 nir_intrinsic_instr *instr =
2680 nir_intrinsic_instr_create(b->nb.shader,
2681 nir_intrinsic_get_buffer_size);
2682 instr->src[0] = nir_src_for_ssa(ptr->block_index);
2683 nir_ssa_dest_init(&instr->instr, &instr->dest, 1, 32, NULL);
2684 nir_builder_instr_insert(&b->nb, &instr->instr);
2685 nir_ssa_def *buf_size = &instr->dest.ssa;
2686
2687 /* array_length = max(buffer_size - offset, 0) / stride */
2688 nir_ssa_def *array_length =
2689 nir_idiv(&b->nb,
2690 nir_imax(&b->nb,
2691 nir_isub(&b->nb,
2692 buf_size,
2693 nir_imm_int(&b->nb, offset)),
2694 nir_imm_int(&b->nb, 0u)),
2695 nir_imm_int(&b->nb, stride));
2696
2697 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2698 val->ssa = vtn_create_ssa_value(b, glsl_uint_type());
2699 val->ssa->def = array_length;
2700 break;
2701 }
2702
2703 case SpvOpConvertPtrToU: {
2704 struct vtn_value *u_val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2705
2706 vtn_fail_if(u_val->type->base_type != vtn_base_type_vector &&
2707 u_val->type->base_type != vtn_base_type_scalar,
2708 "OpConvertPtrToU can only be used to cast to a vector or "
2709 "scalar type");
2710
2711 /* The pointer will be converted to an SSA value automatically */
2712 struct vtn_ssa_value *ptr_ssa = vtn_ssa_value(b, w[3]);
2713
2714 u_val->ssa = vtn_create_ssa_value(b, u_val->type->type);
2715 u_val->ssa->def = nir_sloppy_bitcast(&b->nb, ptr_ssa->def, u_val->type->type);
2716 u_val->ssa->access |= ptr_ssa->access;
2717 break;
2718 }
2719
2720 case SpvOpConvertUToPtr: {
2721 struct vtn_value *ptr_val =
2722 vtn_push_value(b, w[2], vtn_value_type_pointer);
2723 struct vtn_value *u_val = vtn_value(b, w[3], vtn_value_type_ssa);
2724
2725 vtn_fail_if(ptr_val->type->type == NULL,
2726 "OpConvertUToPtr can only be used on physical pointers");
2727
2728 vtn_fail_if(u_val->type->base_type != vtn_base_type_vector &&
2729 u_val->type->base_type != vtn_base_type_scalar,
2730 "OpConvertUToPtr can only be used to cast from a vector or "
2731 "scalar type");
2732
2733 nir_ssa_def *ptr_ssa = nir_sloppy_bitcast(&b->nb, u_val->ssa->def,
2734 ptr_val->type->type);
2735 ptr_val->pointer = vtn_pointer_from_ssa(b, ptr_ssa, ptr_val->type);
2736 vtn_foreach_decoration(b, ptr_val, ptr_decoration_cb, ptr_val->pointer);
2737 ptr_val->pointer->access |= u_val->ssa->access;
2738 break;
2739 }
2740
2741 case SpvOpCopyMemorySized:
2742 default:
2743 vtn_fail_with_opcode("Unhandled opcode", opcode);
2744 }
2745 }