9dc2c755ca570bfd5a2164c26caaec3692cdb245
[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 val->def = nir_vector_extract(&b->nb, val->def, src->arr.index.ssa);
739 }
740
741 return val;
742 }
743
744 void
745 vtn_local_store(struct vtn_builder *b, struct vtn_ssa_value *src,
746 nir_deref_instr *dest, enum gl_access_qualifier access)
747 {
748 nir_deref_instr *dest_tail = get_deref_tail(dest);
749
750 if (dest_tail != dest) {
751 struct vtn_ssa_value *val = vtn_create_ssa_value(b, dest_tail->type);
752 _vtn_local_load_store(b, true, dest_tail, val, access);
753
754 val->def = nir_vector_insert(&b->nb, val->def, src->def,
755 dest->arr.index.ssa);
756 _vtn_local_load_store(b, false, dest_tail, val, access);
757 } else {
758 _vtn_local_load_store(b, false, dest_tail, src, access);
759 }
760 }
761
762 nir_ssa_def *
763 vtn_pointer_to_offset(struct vtn_builder *b, struct vtn_pointer *ptr,
764 nir_ssa_def **index_out)
765 {
766 assert(vtn_pointer_uses_ssa_offset(b, ptr));
767 if (!ptr->offset) {
768 struct vtn_access_chain chain = {
769 .length = 0,
770 };
771 ptr = vtn_ssa_offset_pointer_dereference(b, ptr, &chain);
772 }
773 *index_out = ptr->block_index;
774 return ptr->offset;
775 }
776
777 /* Tries to compute the size of an interface block based on the strides and
778 * offsets that are provided to us in the SPIR-V source.
779 */
780 static unsigned
781 vtn_type_block_size(struct vtn_builder *b, struct vtn_type *type)
782 {
783 enum glsl_base_type base_type = glsl_get_base_type(type->type);
784 switch (base_type) {
785 case GLSL_TYPE_UINT:
786 case GLSL_TYPE_INT:
787 case GLSL_TYPE_UINT16:
788 case GLSL_TYPE_INT16:
789 case GLSL_TYPE_UINT8:
790 case GLSL_TYPE_INT8:
791 case GLSL_TYPE_UINT64:
792 case GLSL_TYPE_INT64:
793 case GLSL_TYPE_FLOAT:
794 case GLSL_TYPE_FLOAT16:
795 case GLSL_TYPE_BOOL:
796 case GLSL_TYPE_DOUBLE: {
797 unsigned cols = type->row_major ? glsl_get_vector_elements(type->type) :
798 glsl_get_matrix_columns(type->type);
799 if (cols > 1) {
800 vtn_assert(type->stride > 0);
801 return type->stride * cols;
802 } else {
803 unsigned type_size = glsl_get_bit_size(type->type) / 8;
804 return glsl_get_vector_elements(type->type) * type_size;
805 }
806 }
807
808 case GLSL_TYPE_STRUCT:
809 case GLSL_TYPE_INTERFACE: {
810 unsigned size = 0;
811 unsigned num_fields = glsl_get_length(type->type);
812 for (unsigned f = 0; f < num_fields; f++) {
813 unsigned field_end = type->offsets[f] +
814 vtn_type_block_size(b, type->members[f]);
815 size = MAX2(size, field_end);
816 }
817 return size;
818 }
819
820 case GLSL_TYPE_ARRAY:
821 vtn_assert(type->stride > 0);
822 vtn_assert(glsl_get_length(type->type) > 0);
823 return type->stride * glsl_get_length(type->type);
824
825 default:
826 vtn_fail("Invalid block type");
827 return 0;
828 }
829 }
830
831 static void
832 _vtn_load_store_tail(struct vtn_builder *b, nir_intrinsic_op op, bool load,
833 nir_ssa_def *index, nir_ssa_def *offset,
834 unsigned access_offset, unsigned access_size,
835 struct vtn_ssa_value **inout, const struct glsl_type *type,
836 enum gl_access_qualifier access)
837 {
838 nir_intrinsic_instr *instr = nir_intrinsic_instr_create(b->nb.shader, op);
839 instr->num_components = glsl_get_vector_elements(type);
840
841 /* Booleans usually shouldn't show up in external memory in SPIR-V.
842 * However, they do for certain older GLSLang versions and can for shared
843 * memory when we lower access chains internally.
844 */
845 const unsigned data_bit_size = glsl_type_is_boolean(type) ? 32 :
846 glsl_get_bit_size(type);
847
848 int src = 0;
849 if (!load) {
850 nir_intrinsic_set_write_mask(instr, (1 << instr->num_components) - 1);
851 instr->src[src++] = nir_src_for_ssa((*inout)->def);
852 }
853
854 if (op == nir_intrinsic_load_push_constant) {
855 nir_intrinsic_set_base(instr, access_offset);
856 nir_intrinsic_set_range(instr, access_size);
857 }
858
859 if (op == nir_intrinsic_load_ubo ||
860 op == nir_intrinsic_load_ssbo ||
861 op == nir_intrinsic_store_ssbo) {
862 nir_intrinsic_set_access(instr, access);
863 }
864
865 /* With extensions like relaxed_block_layout, we really can't guarantee
866 * much more than scalar alignment.
867 */
868 if (op != nir_intrinsic_load_push_constant)
869 nir_intrinsic_set_align(instr, data_bit_size / 8, 0);
870
871 if (index)
872 instr->src[src++] = nir_src_for_ssa(index);
873
874 if (op == nir_intrinsic_load_push_constant) {
875 /* We need to subtract the offset from where the intrinsic will load the
876 * data. */
877 instr->src[src++] =
878 nir_src_for_ssa(nir_isub(&b->nb, offset,
879 nir_imm_int(&b->nb, access_offset)));
880 } else {
881 instr->src[src++] = nir_src_for_ssa(offset);
882 }
883
884 if (load) {
885 nir_ssa_dest_init(&instr->instr, &instr->dest,
886 instr->num_components, data_bit_size, NULL);
887 (*inout)->def = &instr->dest.ssa;
888 }
889
890 nir_builder_instr_insert(&b->nb, &instr->instr);
891
892 if (load && glsl_get_base_type(type) == GLSL_TYPE_BOOL)
893 (*inout)->def = nir_ine(&b->nb, (*inout)->def, nir_imm_int(&b->nb, 0));
894 }
895
896 static void
897 _vtn_block_load_store(struct vtn_builder *b, nir_intrinsic_op op, bool load,
898 nir_ssa_def *index, nir_ssa_def *offset,
899 unsigned access_offset, unsigned access_size,
900 struct vtn_type *type, enum gl_access_qualifier access,
901 struct vtn_ssa_value **inout)
902 {
903 if (load && *inout == NULL)
904 *inout = vtn_create_ssa_value(b, type->type);
905
906 enum glsl_base_type base_type = glsl_get_base_type(type->type);
907 switch (base_type) {
908 case GLSL_TYPE_UINT:
909 case GLSL_TYPE_INT:
910 case GLSL_TYPE_UINT16:
911 case GLSL_TYPE_INT16:
912 case GLSL_TYPE_UINT8:
913 case GLSL_TYPE_INT8:
914 case GLSL_TYPE_UINT64:
915 case GLSL_TYPE_INT64:
916 case GLSL_TYPE_FLOAT:
917 case GLSL_TYPE_FLOAT16:
918 case GLSL_TYPE_DOUBLE:
919 case GLSL_TYPE_BOOL:
920 /* This is where things get interesting. At this point, we've hit
921 * a vector, a scalar, or a matrix.
922 */
923 if (glsl_type_is_matrix(type->type)) {
924 /* Loading the whole matrix */
925 struct vtn_ssa_value *transpose;
926 unsigned num_ops, vec_width, col_stride;
927 if (type->row_major) {
928 num_ops = glsl_get_vector_elements(type->type);
929 vec_width = glsl_get_matrix_columns(type->type);
930 col_stride = type->array_element->stride;
931 if (load) {
932 const struct glsl_type *transpose_type =
933 glsl_matrix_type(base_type, vec_width, num_ops);
934 *inout = vtn_create_ssa_value(b, transpose_type);
935 } else {
936 transpose = vtn_ssa_transpose(b, *inout);
937 inout = &transpose;
938 }
939 } else {
940 num_ops = glsl_get_matrix_columns(type->type);
941 vec_width = glsl_get_vector_elements(type->type);
942 col_stride = type->stride;
943 }
944
945 for (unsigned i = 0; i < num_ops; i++) {
946 nir_ssa_def *elem_offset =
947 nir_iadd_imm(&b->nb, offset, i * col_stride);
948 _vtn_load_store_tail(b, op, load, index, elem_offset,
949 access_offset, access_size,
950 &(*inout)->elems[i],
951 glsl_vector_type(base_type, vec_width),
952 type->access | access);
953 }
954
955 if (load && type->row_major)
956 *inout = vtn_ssa_transpose(b, *inout);
957 } else {
958 unsigned elems = glsl_get_vector_elements(type->type);
959 unsigned type_size = glsl_get_bit_size(type->type) / 8;
960 if (elems == 1 || type->stride == type_size) {
961 /* This is a tightly-packed normal scalar or vector load */
962 vtn_assert(glsl_type_is_vector_or_scalar(type->type));
963 _vtn_load_store_tail(b, op, load, index, offset,
964 access_offset, access_size,
965 inout, type->type,
966 type->access | access);
967 } else {
968 /* This is a strided load. We have to load N things separately.
969 * This is the single column of a row-major matrix case.
970 */
971 vtn_assert(type->stride > type_size);
972 vtn_assert(type->stride % type_size == 0);
973
974 nir_ssa_def *per_comp[4];
975 for (unsigned i = 0; i < elems; i++) {
976 nir_ssa_def *elem_offset =
977 nir_iadd_imm(&b->nb, offset, i * type->stride);
978 struct vtn_ssa_value *comp, temp_val;
979 if (!load) {
980 temp_val.def = nir_channel(&b->nb, (*inout)->def, i);
981 temp_val.type = glsl_scalar_type(base_type);
982 }
983 comp = &temp_val;
984 _vtn_load_store_tail(b, op, load, index, elem_offset,
985 access_offset, access_size,
986 &comp, glsl_scalar_type(base_type),
987 type->access | access);
988 per_comp[i] = comp->def;
989 }
990
991 if (load) {
992 if (*inout == NULL)
993 *inout = vtn_create_ssa_value(b, type->type);
994 (*inout)->def = nir_vec(&b->nb, per_comp, elems);
995 }
996 }
997 }
998 return;
999
1000 case GLSL_TYPE_ARRAY: {
1001 unsigned elems = glsl_get_length(type->type);
1002 for (unsigned i = 0; i < elems; i++) {
1003 nir_ssa_def *elem_off =
1004 nir_iadd_imm(&b->nb, offset, i * type->stride);
1005 _vtn_block_load_store(b, op, load, index, elem_off,
1006 access_offset, access_size,
1007 type->array_element,
1008 type->array_element->access | access,
1009 &(*inout)->elems[i]);
1010 }
1011 return;
1012 }
1013
1014 case GLSL_TYPE_INTERFACE:
1015 case GLSL_TYPE_STRUCT: {
1016 unsigned elems = glsl_get_length(type->type);
1017 for (unsigned i = 0; i < elems; i++) {
1018 nir_ssa_def *elem_off =
1019 nir_iadd_imm(&b->nb, offset, type->offsets[i]);
1020 _vtn_block_load_store(b, op, load, index, elem_off,
1021 access_offset, access_size,
1022 type->members[i],
1023 type->members[i]->access | access,
1024 &(*inout)->elems[i]);
1025 }
1026 return;
1027 }
1028
1029 default:
1030 vtn_fail("Invalid block member type");
1031 }
1032 }
1033
1034 static struct vtn_ssa_value *
1035 vtn_block_load(struct vtn_builder *b, struct vtn_pointer *src)
1036 {
1037 nir_intrinsic_op op;
1038 unsigned access_offset = 0, access_size = 0;
1039 switch (src->mode) {
1040 case vtn_variable_mode_ubo:
1041 op = nir_intrinsic_load_ubo;
1042 break;
1043 case vtn_variable_mode_ssbo:
1044 op = nir_intrinsic_load_ssbo;
1045 break;
1046 case vtn_variable_mode_push_constant:
1047 op = nir_intrinsic_load_push_constant;
1048 access_size = b->shader->num_uniforms;
1049 break;
1050 case vtn_variable_mode_workgroup:
1051 op = nir_intrinsic_load_shared;
1052 break;
1053 default:
1054 vtn_fail("Invalid block variable mode");
1055 }
1056
1057 nir_ssa_def *offset, *index = NULL;
1058 offset = vtn_pointer_to_offset(b, src, &index);
1059
1060 struct vtn_ssa_value *value = NULL;
1061 _vtn_block_load_store(b, op, true, index, offset,
1062 access_offset, access_size,
1063 src->type, src->access, &value);
1064 return value;
1065 }
1066
1067 static void
1068 vtn_block_store(struct vtn_builder *b, struct vtn_ssa_value *src,
1069 struct vtn_pointer *dst)
1070 {
1071 nir_intrinsic_op op;
1072 switch (dst->mode) {
1073 case vtn_variable_mode_ssbo:
1074 op = nir_intrinsic_store_ssbo;
1075 break;
1076 case vtn_variable_mode_workgroup:
1077 op = nir_intrinsic_store_shared;
1078 break;
1079 default:
1080 vtn_fail("Invalid block variable mode");
1081 }
1082
1083 nir_ssa_def *offset, *index = NULL;
1084 offset = vtn_pointer_to_offset(b, dst, &index);
1085
1086 _vtn_block_load_store(b, op, false, index, offset,
1087 0, 0, dst->type, dst->access, &src);
1088 }
1089
1090 static void
1091 _vtn_variable_load_store(struct vtn_builder *b, bool load,
1092 struct vtn_pointer *ptr,
1093 enum gl_access_qualifier access,
1094 struct vtn_ssa_value **inout)
1095 {
1096 enum glsl_base_type base_type = glsl_get_base_type(ptr->type->type);
1097 switch (base_type) {
1098 case GLSL_TYPE_UINT:
1099 case GLSL_TYPE_INT:
1100 case GLSL_TYPE_UINT16:
1101 case GLSL_TYPE_INT16:
1102 case GLSL_TYPE_UINT8:
1103 case GLSL_TYPE_INT8:
1104 case GLSL_TYPE_UINT64:
1105 case GLSL_TYPE_INT64:
1106 case GLSL_TYPE_FLOAT:
1107 case GLSL_TYPE_FLOAT16:
1108 case GLSL_TYPE_BOOL:
1109 case GLSL_TYPE_DOUBLE:
1110 if (glsl_type_is_vector_or_scalar(ptr->type->type)) {
1111 /* We hit a vector or scalar; go ahead and emit the load[s] */
1112 nir_deref_instr *deref = vtn_pointer_to_deref(b, ptr);
1113 if (vtn_pointer_is_external_block(b, ptr)) {
1114 /* If it's external, we call nir_load/store_deref directly. The
1115 * vtn_local_load/store helpers are too clever and do magic to
1116 * avoid array derefs of vectors. That magic is both less
1117 * efficient than the direct load/store and, in the case of
1118 * stores, is broken because it creates a race condition if two
1119 * threads are writing to different components of the same vector
1120 * due to the load+insert+store it uses to emulate the array
1121 * deref.
1122 */
1123 if (load) {
1124 *inout = vtn_create_ssa_value(b, ptr->type->type);
1125 (*inout)->def = nir_load_deref_with_access(&b->nb, deref,
1126 ptr->type->access | access);
1127 } else {
1128 nir_store_deref_with_access(&b->nb, deref, (*inout)->def, ~0,
1129 ptr->type->access | access);
1130 }
1131 } else {
1132 if (load) {
1133 *inout = vtn_local_load(b, deref, ptr->type->access | access);
1134 } else {
1135 vtn_local_store(b, *inout, deref, ptr->type->access | access);
1136 }
1137 }
1138 return;
1139 }
1140 /* Fall through */
1141
1142 case GLSL_TYPE_INTERFACE:
1143 case GLSL_TYPE_ARRAY:
1144 case GLSL_TYPE_STRUCT: {
1145 unsigned elems = glsl_get_length(ptr->type->type);
1146 if (load) {
1147 vtn_assert(*inout == NULL);
1148 *inout = rzalloc(b, struct vtn_ssa_value);
1149 (*inout)->type = ptr->type->type;
1150 (*inout)->elems = rzalloc_array(b, struct vtn_ssa_value *, elems);
1151 }
1152
1153 struct vtn_access_chain chain = {
1154 .length = 1,
1155 .link = {
1156 { .mode = vtn_access_mode_literal, },
1157 }
1158 };
1159 for (unsigned i = 0; i < elems; i++) {
1160 chain.link[0].id = i;
1161 struct vtn_pointer *elem = vtn_pointer_dereference(b, ptr, &chain);
1162 _vtn_variable_load_store(b, load, elem, ptr->type->access | access,
1163 &(*inout)->elems[i]);
1164 }
1165 return;
1166 }
1167
1168 default:
1169 vtn_fail("Invalid access chain type");
1170 }
1171 }
1172
1173 struct vtn_ssa_value *
1174 vtn_variable_load(struct vtn_builder *b, struct vtn_pointer *src)
1175 {
1176 if (vtn_pointer_uses_ssa_offset(b, src)) {
1177 return vtn_block_load(b, src);
1178 } else {
1179 struct vtn_ssa_value *val = NULL;
1180 _vtn_variable_load_store(b, true, src, src->access, &val);
1181 return val;
1182 }
1183 }
1184
1185 void
1186 vtn_variable_store(struct vtn_builder *b, struct vtn_ssa_value *src,
1187 struct vtn_pointer *dest)
1188 {
1189 if (vtn_pointer_uses_ssa_offset(b, dest)) {
1190 vtn_assert(dest->mode == vtn_variable_mode_ssbo ||
1191 dest->mode == vtn_variable_mode_workgroup);
1192 vtn_block_store(b, src, dest);
1193 } else {
1194 _vtn_variable_load_store(b, false, dest, dest->access, &src);
1195 }
1196 }
1197
1198 static void
1199 _vtn_variable_copy(struct vtn_builder *b, struct vtn_pointer *dest,
1200 struct vtn_pointer *src)
1201 {
1202 vtn_assert(src->type->type == dest->type->type);
1203 enum glsl_base_type base_type = glsl_get_base_type(src->type->type);
1204 switch (base_type) {
1205 case GLSL_TYPE_UINT:
1206 case GLSL_TYPE_INT:
1207 case GLSL_TYPE_UINT16:
1208 case GLSL_TYPE_INT16:
1209 case GLSL_TYPE_UINT8:
1210 case GLSL_TYPE_INT8:
1211 case GLSL_TYPE_UINT64:
1212 case GLSL_TYPE_INT64:
1213 case GLSL_TYPE_FLOAT:
1214 case GLSL_TYPE_FLOAT16:
1215 case GLSL_TYPE_DOUBLE:
1216 case GLSL_TYPE_BOOL:
1217 /* At this point, we have a scalar, vector, or matrix so we know that
1218 * there cannot be any structure splitting still in the way. By
1219 * stopping at the matrix level rather than the vector level, we
1220 * ensure that matrices get loaded in the optimal way even if they
1221 * are storred row-major in a UBO.
1222 */
1223 vtn_variable_store(b, vtn_variable_load(b, src), dest);
1224 return;
1225
1226 case GLSL_TYPE_INTERFACE:
1227 case GLSL_TYPE_ARRAY:
1228 case GLSL_TYPE_STRUCT: {
1229 struct vtn_access_chain chain = {
1230 .length = 1,
1231 .link = {
1232 { .mode = vtn_access_mode_literal, },
1233 }
1234 };
1235 unsigned elems = glsl_get_length(src->type->type);
1236 for (unsigned i = 0; i < elems; i++) {
1237 chain.link[0].id = i;
1238 struct vtn_pointer *src_elem =
1239 vtn_pointer_dereference(b, src, &chain);
1240 struct vtn_pointer *dest_elem =
1241 vtn_pointer_dereference(b, dest, &chain);
1242
1243 _vtn_variable_copy(b, dest_elem, src_elem);
1244 }
1245 return;
1246 }
1247
1248 default:
1249 vtn_fail("Invalid access chain type");
1250 }
1251 }
1252
1253 static void
1254 vtn_variable_copy(struct vtn_builder *b, struct vtn_pointer *dest,
1255 struct vtn_pointer *src)
1256 {
1257 /* TODO: At some point, we should add a special-case for when we can
1258 * just emit a copy_var intrinsic.
1259 */
1260 _vtn_variable_copy(b, dest, src);
1261 }
1262
1263 static void
1264 set_mode_system_value(struct vtn_builder *b, nir_variable_mode *mode)
1265 {
1266 vtn_assert(*mode == nir_var_system_value || *mode == nir_var_shader_in);
1267 *mode = nir_var_system_value;
1268 }
1269
1270 static void
1271 vtn_get_builtin_location(struct vtn_builder *b,
1272 SpvBuiltIn builtin, int *location,
1273 nir_variable_mode *mode)
1274 {
1275 switch (builtin) {
1276 case SpvBuiltInPosition:
1277 *location = VARYING_SLOT_POS;
1278 break;
1279 case SpvBuiltInPointSize:
1280 *location = VARYING_SLOT_PSIZ;
1281 break;
1282 case SpvBuiltInClipDistance:
1283 *location = VARYING_SLOT_CLIP_DIST0; /* XXX CLIP_DIST1? */
1284 break;
1285 case SpvBuiltInCullDistance:
1286 *location = VARYING_SLOT_CULL_DIST0;
1287 break;
1288 case SpvBuiltInVertexId:
1289 case SpvBuiltInVertexIndex:
1290 /* The Vulkan spec defines VertexIndex to be non-zero-based and doesn't
1291 * allow VertexId. The ARB_gl_spirv spec defines VertexId to be the
1292 * same as gl_VertexID, which is non-zero-based, and removes
1293 * VertexIndex. Since they're both defined to be non-zero-based, we use
1294 * SYSTEM_VALUE_VERTEX_ID for both.
1295 */
1296 *location = SYSTEM_VALUE_VERTEX_ID;
1297 set_mode_system_value(b, mode);
1298 break;
1299 case SpvBuiltInInstanceIndex:
1300 *location = SYSTEM_VALUE_INSTANCE_INDEX;
1301 set_mode_system_value(b, mode);
1302 break;
1303 case SpvBuiltInInstanceId:
1304 *location = SYSTEM_VALUE_INSTANCE_ID;
1305 set_mode_system_value(b, mode);
1306 break;
1307 case SpvBuiltInPrimitiveId:
1308 if (b->shader->info.stage == MESA_SHADER_FRAGMENT) {
1309 vtn_assert(*mode == nir_var_shader_in);
1310 *location = VARYING_SLOT_PRIMITIVE_ID;
1311 } else if (*mode == nir_var_shader_out) {
1312 *location = VARYING_SLOT_PRIMITIVE_ID;
1313 } else {
1314 *location = SYSTEM_VALUE_PRIMITIVE_ID;
1315 set_mode_system_value(b, mode);
1316 }
1317 break;
1318 case SpvBuiltInInvocationId:
1319 *location = SYSTEM_VALUE_INVOCATION_ID;
1320 set_mode_system_value(b, mode);
1321 break;
1322 case SpvBuiltInLayer:
1323 *location = VARYING_SLOT_LAYER;
1324 if (b->shader->info.stage == MESA_SHADER_FRAGMENT)
1325 *mode = nir_var_shader_in;
1326 else if (b->shader->info.stage == MESA_SHADER_GEOMETRY)
1327 *mode = nir_var_shader_out;
1328 else if (b->options && b->options->caps.shader_viewport_index_layer &&
1329 (b->shader->info.stage == MESA_SHADER_VERTEX ||
1330 b->shader->info.stage == MESA_SHADER_TESS_EVAL))
1331 *mode = nir_var_shader_out;
1332 else
1333 vtn_fail("invalid stage for SpvBuiltInLayer");
1334 break;
1335 case SpvBuiltInViewportIndex:
1336 *location = VARYING_SLOT_VIEWPORT;
1337 if (b->shader->info.stage == MESA_SHADER_GEOMETRY)
1338 *mode = nir_var_shader_out;
1339 else if (b->options && b->options->caps.shader_viewport_index_layer &&
1340 (b->shader->info.stage == MESA_SHADER_VERTEX ||
1341 b->shader->info.stage == MESA_SHADER_TESS_EVAL))
1342 *mode = nir_var_shader_out;
1343 else if (b->shader->info.stage == MESA_SHADER_FRAGMENT)
1344 *mode = nir_var_shader_in;
1345 else
1346 vtn_fail("invalid stage for SpvBuiltInViewportIndex");
1347 break;
1348 case SpvBuiltInTessLevelOuter:
1349 *location = VARYING_SLOT_TESS_LEVEL_OUTER;
1350 break;
1351 case SpvBuiltInTessLevelInner:
1352 *location = VARYING_SLOT_TESS_LEVEL_INNER;
1353 break;
1354 case SpvBuiltInTessCoord:
1355 *location = SYSTEM_VALUE_TESS_COORD;
1356 set_mode_system_value(b, mode);
1357 break;
1358 case SpvBuiltInPatchVertices:
1359 *location = SYSTEM_VALUE_VERTICES_IN;
1360 set_mode_system_value(b, mode);
1361 break;
1362 case SpvBuiltInFragCoord:
1363 vtn_assert(*mode == nir_var_shader_in);
1364 if (b->options && b->options->frag_coord_is_sysval) {
1365 *mode = nir_var_system_value;
1366 *location = SYSTEM_VALUE_FRAG_COORD;
1367 } else {
1368 *location = VARYING_SLOT_POS;
1369 }
1370 break;
1371 case SpvBuiltInPointCoord:
1372 *location = VARYING_SLOT_PNTC;
1373 vtn_assert(*mode == nir_var_shader_in);
1374 break;
1375 case SpvBuiltInFrontFacing:
1376 *location = SYSTEM_VALUE_FRONT_FACE;
1377 set_mode_system_value(b, mode);
1378 break;
1379 case SpvBuiltInSampleId:
1380 *location = SYSTEM_VALUE_SAMPLE_ID;
1381 set_mode_system_value(b, mode);
1382 break;
1383 case SpvBuiltInSamplePosition:
1384 *location = SYSTEM_VALUE_SAMPLE_POS;
1385 set_mode_system_value(b, mode);
1386 break;
1387 case SpvBuiltInSampleMask:
1388 if (*mode == nir_var_shader_out) {
1389 *location = FRAG_RESULT_SAMPLE_MASK;
1390 } else {
1391 *location = SYSTEM_VALUE_SAMPLE_MASK_IN;
1392 set_mode_system_value(b, mode);
1393 }
1394 break;
1395 case SpvBuiltInFragDepth:
1396 *location = FRAG_RESULT_DEPTH;
1397 vtn_assert(*mode == nir_var_shader_out);
1398 break;
1399 case SpvBuiltInHelperInvocation:
1400 *location = SYSTEM_VALUE_HELPER_INVOCATION;
1401 set_mode_system_value(b, mode);
1402 break;
1403 case SpvBuiltInNumWorkgroups:
1404 *location = SYSTEM_VALUE_NUM_WORK_GROUPS;
1405 set_mode_system_value(b, mode);
1406 break;
1407 case SpvBuiltInWorkgroupSize:
1408 *location = SYSTEM_VALUE_LOCAL_GROUP_SIZE;
1409 set_mode_system_value(b, mode);
1410 break;
1411 case SpvBuiltInWorkgroupId:
1412 *location = SYSTEM_VALUE_WORK_GROUP_ID;
1413 set_mode_system_value(b, mode);
1414 break;
1415 case SpvBuiltInLocalInvocationId:
1416 *location = SYSTEM_VALUE_LOCAL_INVOCATION_ID;
1417 set_mode_system_value(b, mode);
1418 break;
1419 case SpvBuiltInLocalInvocationIndex:
1420 *location = SYSTEM_VALUE_LOCAL_INVOCATION_INDEX;
1421 set_mode_system_value(b, mode);
1422 break;
1423 case SpvBuiltInGlobalInvocationId:
1424 *location = SYSTEM_VALUE_GLOBAL_INVOCATION_ID;
1425 set_mode_system_value(b, mode);
1426 break;
1427 case SpvBuiltInGlobalLinearId:
1428 *location = SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX;
1429 set_mode_system_value(b, mode);
1430 break;
1431 case SpvBuiltInBaseVertex:
1432 /* OpenGL gl_BaseVertex (SYSTEM_VALUE_BASE_VERTEX) is not the same
1433 * semantic as Vulkan BaseVertex (SYSTEM_VALUE_FIRST_VERTEX).
1434 */
1435 if (b->options->environment == NIR_SPIRV_OPENGL)
1436 *location = SYSTEM_VALUE_BASE_VERTEX;
1437 else
1438 *location = SYSTEM_VALUE_FIRST_VERTEX;
1439 set_mode_system_value(b, mode);
1440 break;
1441 case SpvBuiltInBaseInstance:
1442 *location = SYSTEM_VALUE_BASE_INSTANCE;
1443 set_mode_system_value(b, mode);
1444 break;
1445 case SpvBuiltInDrawIndex:
1446 *location = SYSTEM_VALUE_DRAW_ID;
1447 set_mode_system_value(b, mode);
1448 break;
1449 case SpvBuiltInSubgroupSize:
1450 *location = SYSTEM_VALUE_SUBGROUP_SIZE;
1451 set_mode_system_value(b, mode);
1452 break;
1453 case SpvBuiltInSubgroupId:
1454 *location = SYSTEM_VALUE_SUBGROUP_ID;
1455 set_mode_system_value(b, mode);
1456 break;
1457 case SpvBuiltInSubgroupLocalInvocationId:
1458 *location = SYSTEM_VALUE_SUBGROUP_INVOCATION;
1459 set_mode_system_value(b, mode);
1460 break;
1461 case SpvBuiltInNumSubgroups:
1462 *location = SYSTEM_VALUE_NUM_SUBGROUPS;
1463 set_mode_system_value(b, mode);
1464 break;
1465 case SpvBuiltInDeviceIndex:
1466 *location = SYSTEM_VALUE_DEVICE_INDEX;
1467 set_mode_system_value(b, mode);
1468 break;
1469 case SpvBuiltInViewIndex:
1470 *location = SYSTEM_VALUE_VIEW_INDEX;
1471 set_mode_system_value(b, mode);
1472 break;
1473 case SpvBuiltInSubgroupEqMask:
1474 *location = SYSTEM_VALUE_SUBGROUP_EQ_MASK,
1475 set_mode_system_value(b, mode);
1476 break;
1477 case SpvBuiltInSubgroupGeMask:
1478 *location = SYSTEM_VALUE_SUBGROUP_GE_MASK,
1479 set_mode_system_value(b, mode);
1480 break;
1481 case SpvBuiltInSubgroupGtMask:
1482 *location = SYSTEM_VALUE_SUBGROUP_GT_MASK,
1483 set_mode_system_value(b, mode);
1484 break;
1485 case SpvBuiltInSubgroupLeMask:
1486 *location = SYSTEM_VALUE_SUBGROUP_LE_MASK,
1487 set_mode_system_value(b, mode);
1488 break;
1489 case SpvBuiltInSubgroupLtMask:
1490 *location = SYSTEM_VALUE_SUBGROUP_LT_MASK,
1491 set_mode_system_value(b, mode);
1492 break;
1493 case SpvBuiltInFragStencilRefEXT:
1494 *location = FRAG_RESULT_STENCIL;
1495 vtn_assert(*mode == nir_var_shader_out);
1496 break;
1497 case SpvBuiltInWorkDim:
1498 *location = SYSTEM_VALUE_WORK_DIM;
1499 set_mode_system_value(b, mode);
1500 break;
1501 case SpvBuiltInGlobalSize:
1502 *location = SYSTEM_VALUE_GLOBAL_GROUP_SIZE;
1503 set_mode_system_value(b, mode);
1504 break;
1505 case SpvBuiltInBaryCoordNoPerspAMD:
1506 *location = SYSTEM_VALUE_BARYCENTRIC_LINEAR_PIXEL;
1507 set_mode_system_value(b, mode);
1508 break;
1509 case SpvBuiltInBaryCoordNoPerspCentroidAMD:
1510 *location = SYSTEM_VALUE_BARYCENTRIC_LINEAR_CENTROID;
1511 set_mode_system_value(b, mode);
1512 break;
1513 case SpvBuiltInBaryCoordNoPerspSampleAMD:
1514 *location = SYSTEM_VALUE_BARYCENTRIC_LINEAR_SAMPLE;
1515 set_mode_system_value(b, mode);
1516 break;
1517 case SpvBuiltInBaryCoordSmoothAMD:
1518 *location = SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL;
1519 set_mode_system_value(b, mode);
1520 break;
1521 case SpvBuiltInBaryCoordSmoothCentroidAMD:
1522 *location = SYSTEM_VALUE_BARYCENTRIC_PERSP_CENTROID;
1523 set_mode_system_value(b, mode);
1524 break;
1525 case SpvBuiltInBaryCoordSmoothSampleAMD:
1526 *location = SYSTEM_VALUE_BARYCENTRIC_PERSP_SAMPLE;
1527 set_mode_system_value(b, mode);
1528 break;
1529 case SpvBuiltInBaryCoordPullModelAMD:
1530 *location = SYSTEM_VALUE_BARYCENTRIC_PULL_MODEL;
1531 set_mode_system_value(b, mode);
1532 break;
1533 default:
1534 vtn_fail("Unsupported builtin: %s (%u)",
1535 spirv_builtin_to_string(builtin), builtin);
1536 }
1537 }
1538
1539 static void
1540 apply_var_decoration(struct vtn_builder *b,
1541 struct nir_variable_data *var_data,
1542 const struct vtn_decoration *dec)
1543 {
1544 switch (dec->decoration) {
1545 case SpvDecorationRelaxedPrecision:
1546 break; /* FIXME: Do nothing with this for now. */
1547 case SpvDecorationNoPerspective:
1548 var_data->interpolation = INTERP_MODE_NOPERSPECTIVE;
1549 break;
1550 case SpvDecorationFlat:
1551 var_data->interpolation = INTERP_MODE_FLAT;
1552 break;
1553 case SpvDecorationExplicitInterpAMD:
1554 var_data->interpolation = INTERP_MODE_EXPLICIT;
1555 break;
1556 case SpvDecorationCentroid:
1557 var_data->centroid = true;
1558 break;
1559 case SpvDecorationSample:
1560 var_data->sample = true;
1561 break;
1562 case SpvDecorationInvariant:
1563 var_data->invariant = true;
1564 break;
1565 case SpvDecorationConstant:
1566 var_data->read_only = true;
1567 break;
1568 case SpvDecorationNonReadable:
1569 var_data->access |= ACCESS_NON_READABLE;
1570 break;
1571 case SpvDecorationNonWritable:
1572 var_data->read_only = true;
1573 var_data->access |= ACCESS_NON_WRITEABLE;
1574 break;
1575 case SpvDecorationRestrict:
1576 var_data->access |= ACCESS_RESTRICT;
1577 break;
1578 case SpvDecorationVolatile:
1579 var_data->access |= ACCESS_VOLATILE;
1580 break;
1581 case SpvDecorationCoherent:
1582 var_data->access |= ACCESS_COHERENT;
1583 break;
1584 case SpvDecorationComponent:
1585 var_data->location_frac = dec->operands[0];
1586 break;
1587 case SpvDecorationIndex:
1588 var_data->index = dec->operands[0];
1589 break;
1590 case SpvDecorationBuiltIn: {
1591 SpvBuiltIn builtin = dec->operands[0];
1592
1593 nir_variable_mode mode = var_data->mode;
1594 vtn_get_builtin_location(b, builtin, &var_data->location, &mode);
1595 var_data->mode = mode;
1596
1597 switch (builtin) {
1598 case SpvBuiltInTessLevelOuter:
1599 case SpvBuiltInTessLevelInner:
1600 case SpvBuiltInClipDistance:
1601 case SpvBuiltInCullDistance:
1602 var_data->compact = true;
1603 break;
1604 default:
1605 break;
1606 }
1607 }
1608
1609 case SpvDecorationSpecId:
1610 case SpvDecorationRowMajor:
1611 case SpvDecorationColMajor:
1612 case SpvDecorationMatrixStride:
1613 case SpvDecorationAliased:
1614 case SpvDecorationUniform:
1615 case SpvDecorationUniformId:
1616 case SpvDecorationLinkageAttributes:
1617 break; /* Do nothing with these here */
1618
1619 case SpvDecorationPatch:
1620 var_data->patch = true;
1621 break;
1622
1623 case SpvDecorationLocation:
1624 vtn_fail("Handled above");
1625
1626 case SpvDecorationBlock:
1627 case SpvDecorationBufferBlock:
1628 case SpvDecorationArrayStride:
1629 case SpvDecorationGLSLShared:
1630 case SpvDecorationGLSLPacked:
1631 break; /* These can apply to a type but we don't care about them */
1632
1633 case SpvDecorationBinding:
1634 case SpvDecorationDescriptorSet:
1635 case SpvDecorationNoContraction:
1636 case SpvDecorationInputAttachmentIndex:
1637 vtn_warn("Decoration not allowed for variable or structure member: %s",
1638 spirv_decoration_to_string(dec->decoration));
1639 break;
1640
1641 case SpvDecorationXfbBuffer:
1642 var_data->explicit_xfb_buffer = true;
1643 var_data->xfb.buffer = dec->operands[0];
1644 var_data->always_active_io = true;
1645 break;
1646 case SpvDecorationXfbStride:
1647 var_data->explicit_xfb_stride = true;
1648 var_data->xfb.stride = dec->operands[0];
1649 break;
1650 case SpvDecorationOffset:
1651 var_data->explicit_offset = true;
1652 var_data->offset = dec->operands[0];
1653 break;
1654
1655 case SpvDecorationStream:
1656 var_data->stream = dec->operands[0];
1657 break;
1658
1659 case SpvDecorationCPacked:
1660 case SpvDecorationSaturatedConversion:
1661 case SpvDecorationFuncParamAttr:
1662 case SpvDecorationFPRoundingMode:
1663 case SpvDecorationFPFastMathMode:
1664 case SpvDecorationAlignment:
1665 if (b->shader->info.stage != MESA_SHADER_KERNEL) {
1666 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1667 spirv_decoration_to_string(dec->decoration));
1668 }
1669 break;
1670
1671 case SpvDecorationUserSemantic:
1672 /* User semantic decorations can safely be ignored by the driver. */
1673 break;
1674
1675 case SpvDecorationRestrictPointerEXT:
1676 case SpvDecorationAliasedPointerEXT:
1677 /* TODO: We should actually plumb alias information through NIR. */
1678 break;
1679
1680 default:
1681 vtn_fail_with_decoration("Unhandled decoration", dec->decoration);
1682 }
1683 }
1684
1685 static void
1686 var_is_patch_cb(struct vtn_builder *b, struct vtn_value *val, int member,
1687 const struct vtn_decoration *dec, void *out_is_patch)
1688 {
1689 if (dec->decoration == SpvDecorationPatch) {
1690 *((bool *) out_is_patch) = true;
1691 }
1692 }
1693
1694 static void
1695 var_decoration_cb(struct vtn_builder *b, struct vtn_value *val, int member,
1696 const struct vtn_decoration *dec, void *void_var)
1697 {
1698 struct vtn_variable *vtn_var = void_var;
1699
1700 /* Handle decorations that apply to a vtn_variable as a whole */
1701 switch (dec->decoration) {
1702 case SpvDecorationBinding:
1703 vtn_var->binding = dec->operands[0];
1704 vtn_var->explicit_binding = true;
1705 return;
1706 case SpvDecorationDescriptorSet:
1707 vtn_var->descriptor_set = dec->operands[0];
1708 return;
1709 case SpvDecorationInputAttachmentIndex:
1710 vtn_var->input_attachment_index = dec->operands[0];
1711 return;
1712 case SpvDecorationPatch:
1713 vtn_var->patch = true;
1714 break;
1715 case SpvDecorationOffset:
1716 vtn_var->offset = dec->operands[0];
1717 break;
1718 case SpvDecorationNonWritable:
1719 vtn_var->access |= ACCESS_NON_WRITEABLE;
1720 break;
1721 case SpvDecorationNonReadable:
1722 vtn_var->access |= ACCESS_NON_READABLE;
1723 break;
1724 case SpvDecorationVolatile:
1725 vtn_var->access |= ACCESS_VOLATILE;
1726 break;
1727 case SpvDecorationCoherent:
1728 vtn_var->access |= ACCESS_COHERENT;
1729 break;
1730 case SpvDecorationCounterBuffer:
1731 /* Counter buffer decorations can safely be ignored by the driver. */
1732 return;
1733 default:
1734 break;
1735 }
1736
1737 if (val->value_type == vtn_value_type_pointer) {
1738 assert(val->pointer->var == void_var);
1739 assert(member == -1);
1740 } else {
1741 assert(val->value_type == vtn_value_type_type);
1742 }
1743
1744 /* Location is odd. If applied to a split structure, we have to walk the
1745 * whole thing and accumulate the location. It's easier to handle as a
1746 * special case.
1747 */
1748 if (dec->decoration == SpvDecorationLocation) {
1749 unsigned location = dec->operands[0];
1750 if (b->shader->info.stage == MESA_SHADER_FRAGMENT &&
1751 vtn_var->mode == vtn_variable_mode_output) {
1752 location += FRAG_RESULT_DATA0;
1753 } else if (b->shader->info.stage == MESA_SHADER_VERTEX &&
1754 vtn_var->mode == vtn_variable_mode_input) {
1755 location += VERT_ATTRIB_GENERIC0;
1756 } else if (vtn_var->mode == vtn_variable_mode_input ||
1757 vtn_var->mode == vtn_variable_mode_output) {
1758 location += vtn_var->patch ? VARYING_SLOT_PATCH0 : VARYING_SLOT_VAR0;
1759 } else if (vtn_var->mode != vtn_variable_mode_uniform) {
1760 vtn_warn("Location must be on input, output, uniform, sampler or "
1761 "image variable");
1762 return;
1763 }
1764
1765 if (vtn_var->var->num_members == 0) {
1766 /* This handles the member and lone variable cases */
1767 vtn_var->var->data.location = location;
1768 } else {
1769 /* This handles the structure member case */
1770 assert(vtn_var->var->members);
1771
1772 if (member == -1)
1773 vtn_var->base_location = location;
1774 else
1775 vtn_var->var->members[member].location = location;
1776 }
1777
1778 return;
1779 } else {
1780 if (vtn_var->var) {
1781 if (vtn_var->var->num_members == 0) {
1782 /* We call this function on types as well as variables and not all
1783 * struct types get split so we can end up having stray member
1784 * decorations; just ignore them.
1785 */
1786 if (member == -1)
1787 apply_var_decoration(b, &vtn_var->var->data, dec);
1788 } else if (member >= 0) {
1789 /* Member decorations must come from a type */
1790 assert(val->value_type == vtn_value_type_type);
1791 apply_var_decoration(b, &vtn_var->var->members[member], dec);
1792 } else {
1793 unsigned length =
1794 glsl_get_length(glsl_without_array(vtn_var->type->type));
1795 for (unsigned i = 0; i < length; i++)
1796 apply_var_decoration(b, &vtn_var->var->members[i], dec);
1797 }
1798 } else {
1799 /* A few variables, those with external storage, have no actual
1800 * nir_variables associated with them. Fortunately, all decorations
1801 * we care about for those variables are on the type only.
1802 */
1803 vtn_assert(vtn_var->mode == vtn_variable_mode_ubo ||
1804 vtn_var->mode == vtn_variable_mode_ssbo ||
1805 vtn_var->mode == vtn_variable_mode_push_constant);
1806 }
1807 }
1808 }
1809
1810 enum vtn_variable_mode
1811 vtn_storage_class_to_mode(struct vtn_builder *b,
1812 SpvStorageClass class,
1813 struct vtn_type *interface_type,
1814 nir_variable_mode *nir_mode_out)
1815 {
1816 enum vtn_variable_mode mode;
1817 nir_variable_mode nir_mode;
1818 switch (class) {
1819 case SpvStorageClassUniform:
1820 /* Assume it's an UBO if we lack the interface_type. */
1821 if (!interface_type || interface_type->block) {
1822 mode = vtn_variable_mode_ubo;
1823 nir_mode = nir_var_mem_ubo;
1824 } else if (interface_type->buffer_block) {
1825 mode = vtn_variable_mode_ssbo;
1826 nir_mode = nir_var_mem_ssbo;
1827 } else {
1828 /* Default-block uniforms, coming from gl_spirv */
1829 mode = vtn_variable_mode_uniform;
1830 nir_mode = nir_var_uniform;
1831 }
1832 break;
1833 case SpvStorageClassStorageBuffer:
1834 mode = vtn_variable_mode_ssbo;
1835 nir_mode = nir_var_mem_ssbo;
1836 break;
1837 case SpvStorageClassPhysicalStorageBuffer:
1838 mode = vtn_variable_mode_phys_ssbo;
1839 nir_mode = nir_var_mem_global;
1840 break;
1841 case SpvStorageClassUniformConstant:
1842 if (b->shader->info.stage == MESA_SHADER_KERNEL) {
1843 if (b->options->constant_as_global) {
1844 mode = vtn_variable_mode_cross_workgroup;
1845 nir_mode = nir_var_mem_global;
1846 } else {
1847 mode = vtn_variable_mode_ubo;
1848 nir_mode = nir_var_mem_ubo;
1849 }
1850 } else {
1851 mode = vtn_variable_mode_uniform;
1852 nir_mode = nir_var_uniform;
1853 }
1854 break;
1855 case SpvStorageClassPushConstant:
1856 mode = vtn_variable_mode_push_constant;
1857 nir_mode = nir_var_uniform;
1858 break;
1859 case SpvStorageClassInput:
1860 mode = vtn_variable_mode_input;
1861 nir_mode = nir_var_shader_in;
1862 break;
1863 case SpvStorageClassOutput:
1864 mode = vtn_variable_mode_output;
1865 nir_mode = nir_var_shader_out;
1866 break;
1867 case SpvStorageClassPrivate:
1868 mode = vtn_variable_mode_private;
1869 nir_mode = nir_var_shader_temp;
1870 break;
1871 case SpvStorageClassFunction:
1872 mode = vtn_variable_mode_function;
1873 nir_mode = nir_var_function_temp;
1874 break;
1875 case SpvStorageClassWorkgroup:
1876 mode = vtn_variable_mode_workgroup;
1877 nir_mode = nir_var_mem_shared;
1878 break;
1879 case SpvStorageClassAtomicCounter:
1880 mode = vtn_variable_mode_uniform;
1881 nir_mode = nir_var_uniform;
1882 break;
1883 case SpvStorageClassCrossWorkgroup:
1884 mode = vtn_variable_mode_cross_workgroup;
1885 nir_mode = nir_var_mem_global;
1886 break;
1887 case SpvStorageClassImage:
1888 mode = vtn_variable_mode_image;
1889 nir_mode = nir_var_mem_ubo;
1890 break;
1891 case SpvStorageClassGeneric:
1892 default:
1893 vtn_fail("Unhandled variable storage class: %s (%u)",
1894 spirv_storageclass_to_string(class), class);
1895 }
1896
1897 if (nir_mode_out)
1898 *nir_mode_out = nir_mode;
1899
1900 return mode;
1901 }
1902
1903 nir_address_format
1904 vtn_mode_to_address_format(struct vtn_builder *b, enum vtn_variable_mode mode)
1905 {
1906 switch (mode) {
1907 case vtn_variable_mode_ubo:
1908 return b->options->ubo_addr_format;
1909
1910 case vtn_variable_mode_ssbo:
1911 return b->options->ssbo_addr_format;
1912
1913 case vtn_variable_mode_phys_ssbo:
1914 return b->options->phys_ssbo_addr_format;
1915
1916 case vtn_variable_mode_push_constant:
1917 return b->options->push_const_addr_format;
1918
1919 case vtn_variable_mode_workgroup:
1920 return b->options->shared_addr_format;
1921
1922 case vtn_variable_mode_cross_workgroup:
1923 return b->options->global_addr_format;
1924
1925 case vtn_variable_mode_function:
1926 if (b->physical_ptrs)
1927 return b->options->temp_addr_format;
1928 /* Fall through. */
1929
1930 case vtn_variable_mode_private:
1931 case vtn_variable_mode_uniform:
1932 case vtn_variable_mode_input:
1933 case vtn_variable_mode_output:
1934 case vtn_variable_mode_image:
1935 return nir_address_format_logical;
1936 }
1937
1938 unreachable("Invalid variable mode");
1939 }
1940
1941 nir_ssa_def *
1942 vtn_pointer_to_ssa(struct vtn_builder *b, struct vtn_pointer *ptr)
1943 {
1944 if (vtn_pointer_uses_ssa_offset(b, ptr)) {
1945 /* This pointer needs to have a pointer type with actual storage */
1946 vtn_assert(ptr->ptr_type);
1947 vtn_assert(ptr->ptr_type->type);
1948
1949 if (!ptr->offset) {
1950 /* If we don't have an offset then we must be a pointer to the variable
1951 * itself.
1952 */
1953 vtn_assert(!ptr->offset && !ptr->block_index);
1954
1955 struct vtn_access_chain chain = {
1956 .length = 0,
1957 };
1958 ptr = vtn_ssa_offset_pointer_dereference(b, ptr, &chain);
1959 }
1960
1961 vtn_assert(ptr->offset);
1962 if (ptr->block_index) {
1963 vtn_assert(ptr->mode == vtn_variable_mode_ubo ||
1964 ptr->mode == vtn_variable_mode_ssbo);
1965 return nir_vec2(&b->nb, ptr->block_index, ptr->offset);
1966 } else {
1967 vtn_assert(ptr->mode == vtn_variable_mode_workgroup);
1968 return ptr->offset;
1969 }
1970 } else {
1971 if (vtn_pointer_is_external_block(b, ptr) &&
1972 vtn_type_contains_block(b, ptr->type) &&
1973 ptr->mode != vtn_variable_mode_phys_ssbo) {
1974 /* In this case, we're looking for a block index and not an actual
1975 * deref.
1976 *
1977 * For PhysicalStorageBuffer pointers, we don't have a block index
1978 * at all because we get the pointer directly from the client. This
1979 * assumes that there will never be a SSBO binding variable using the
1980 * PhysicalStorageBuffer storage class. This assumption appears
1981 * to be correct according to the Vulkan spec because the table,
1982 * "Shader Resource and Storage Class Correspondence," the only the
1983 * Uniform storage class with BufferBlock or the StorageBuffer
1984 * storage class with Block can be used.
1985 */
1986 if (!ptr->block_index) {
1987 /* If we don't have a block_index then we must be a pointer to the
1988 * variable itself.
1989 */
1990 vtn_assert(!ptr->deref);
1991
1992 struct vtn_access_chain chain = {
1993 .length = 0,
1994 };
1995 ptr = vtn_nir_deref_pointer_dereference(b, ptr, &chain);
1996 }
1997
1998 return ptr->block_index;
1999 } else {
2000 return &vtn_pointer_to_deref(b, ptr)->dest.ssa;
2001 }
2002 }
2003 }
2004
2005 struct vtn_pointer *
2006 vtn_pointer_from_ssa(struct vtn_builder *b, nir_ssa_def *ssa,
2007 struct vtn_type *ptr_type)
2008 {
2009 vtn_assert(ptr_type->base_type == vtn_base_type_pointer);
2010
2011 struct vtn_pointer *ptr = rzalloc(b, struct vtn_pointer);
2012 struct vtn_type *without_array =
2013 vtn_type_without_array(ptr_type->deref);
2014
2015 nir_variable_mode nir_mode;
2016 ptr->mode = vtn_storage_class_to_mode(b, ptr_type->storage_class,
2017 without_array, &nir_mode);
2018 ptr->type = ptr_type->deref;
2019 ptr->ptr_type = ptr_type;
2020
2021 if (b->wa_glslang_179) {
2022 /* To work around https://github.com/KhronosGroup/glslang/issues/179 we
2023 * need to whack the mode because it creates a function parameter with
2024 * the Function storage class even though it's a pointer to a sampler.
2025 * If we don't do this, then NIR won't get rid of the deref_cast for us.
2026 */
2027 if (ptr->mode == vtn_variable_mode_function &&
2028 (ptr->type->base_type == vtn_base_type_sampler ||
2029 ptr->type->base_type == vtn_base_type_sampled_image)) {
2030 ptr->mode = vtn_variable_mode_uniform;
2031 nir_mode = nir_var_uniform;
2032 }
2033 }
2034
2035 if (vtn_pointer_uses_ssa_offset(b, ptr)) {
2036 /* This pointer type needs to have actual storage */
2037 vtn_assert(ptr_type->type);
2038 if (ptr->mode == vtn_variable_mode_ubo ||
2039 ptr->mode == vtn_variable_mode_ssbo) {
2040 vtn_assert(ssa->num_components == 2);
2041 ptr->block_index = nir_channel(&b->nb, ssa, 0);
2042 ptr->offset = nir_channel(&b->nb, ssa, 1);
2043 } else {
2044 vtn_assert(ssa->num_components == 1);
2045 ptr->block_index = NULL;
2046 ptr->offset = ssa;
2047 }
2048 } else {
2049 const struct glsl_type *deref_type = ptr_type->deref->type;
2050 if (!vtn_pointer_is_external_block(b, ptr)) {
2051 ptr->deref = nir_build_deref_cast(&b->nb, ssa, nir_mode,
2052 deref_type, ptr_type->stride);
2053 } else if (vtn_type_contains_block(b, ptr->type) &&
2054 ptr->mode != vtn_variable_mode_phys_ssbo) {
2055 /* This is a pointer to somewhere in an array of blocks, not a
2056 * pointer to somewhere inside the block. Set the block index
2057 * instead of making a cast.
2058 */
2059 ptr->block_index = ssa;
2060 } else {
2061 /* This is a pointer to something internal or a pointer inside a
2062 * block. It's just a regular cast.
2063 *
2064 * For PhysicalStorageBuffer pointers, we don't have a block index
2065 * at all because we get the pointer directly from the client. This
2066 * assumes that there will never be a SSBO binding variable using the
2067 * PhysicalStorageBuffer storage class. This assumption appears
2068 * to be correct according to the Vulkan spec because the table,
2069 * "Shader Resource and Storage Class Correspondence," the only the
2070 * Uniform storage class with BufferBlock or the StorageBuffer
2071 * storage class with Block can be used.
2072 */
2073 ptr->deref = nir_build_deref_cast(&b->nb, ssa, nir_mode,
2074 ptr_type->deref->type,
2075 ptr_type->stride);
2076 ptr->deref->dest.ssa.num_components =
2077 glsl_get_vector_elements(ptr_type->type);
2078 ptr->deref->dest.ssa.bit_size = glsl_get_bit_size(ptr_type->type);
2079 }
2080 }
2081
2082 return ptr;
2083 }
2084
2085 static bool
2086 is_per_vertex_inout(const struct vtn_variable *var, gl_shader_stage stage)
2087 {
2088 if (var->patch || !glsl_type_is_array(var->type->type))
2089 return false;
2090
2091 if (var->mode == vtn_variable_mode_input) {
2092 return stage == MESA_SHADER_TESS_CTRL ||
2093 stage == MESA_SHADER_TESS_EVAL ||
2094 stage == MESA_SHADER_GEOMETRY;
2095 }
2096
2097 if (var->mode == vtn_variable_mode_output)
2098 return stage == MESA_SHADER_TESS_CTRL;
2099
2100 return false;
2101 }
2102
2103 static void
2104 assign_missing_member_locations(struct vtn_variable *var)
2105 {
2106 unsigned length =
2107 glsl_get_length(glsl_without_array(var->type->type));
2108 int location = var->base_location;
2109
2110 for (unsigned i = 0; i < length; i++) {
2111 /* From the Vulkan spec:
2112 *
2113 * “If the structure type is a Block but without a Location, then each
2114 * of its members must have a Location decoration.”
2115 *
2116 */
2117 if (var->type->block) {
2118 assert(var->base_location != -1 ||
2119 var->var->members[i].location != -1);
2120 }
2121
2122 /* From the Vulkan spec:
2123 *
2124 * “Any member with its own Location decoration is assigned that
2125 * location. Each remaining member is assigned the location after the
2126 * immediately preceding member in declaration order.”
2127 */
2128 if (var->var->members[i].location != -1)
2129 location = var->var->members[i].location;
2130 else
2131 var->var->members[i].location = location;
2132
2133 /* Below we use type instead of interface_type, because interface_type
2134 * is only available when it is a Block. This code also supports
2135 * input/outputs that are just structs
2136 */
2137 const struct glsl_type *member_type =
2138 glsl_get_struct_field(glsl_without_array(var->type->type), i);
2139
2140 location +=
2141 glsl_count_attribute_slots(member_type,
2142 false /* is_gl_vertex_input */);
2143 }
2144 }
2145
2146
2147 static void
2148 vtn_create_variable(struct vtn_builder *b, struct vtn_value *val,
2149 struct vtn_type *ptr_type, SpvStorageClass storage_class,
2150 nir_constant *const_initializer, nir_variable *var_initializer)
2151 {
2152 vtn_assert(ptr_type->base_type == vtn_base_type_pointer);
2153 struct vtn_type *type = ptr_type->deref;
2154
2155 struct vtn_type *without_array = vtn_type_without_array(ptr_type->deref);
2156
2157 enum vtn_variable_mode mode;
2158 nir_variable_mode nir_mode;
2159 mode = vtn_storage_class_to_mode(b, storage_class, without_array, &nir_mode);
2160
2161 switch (mode) {
2162 case vtn_variable_mode_ubo:
2163 /* There's no other way to get vtn_variable_mode_ubo */
2164 vtn_assert(without_array->block);
2165 b->shader->info.num_ubos++;
2166 break;
2167 case vtn_variable_mode_ssbo:
2168 if (storage_class == SpvStorageClassStorageBuffer &&
2169 !without_array->block) {
2170 if (b->variable_pointers) {
2171 vtn_fail("Variables in the StorageBuffer storage class must "
2172 "have a struct type with the Block decoration");
2173 } else {
2174 /* If variable pointers are not present, it's still malformed
2175 * SPIR-V but we can parse it and do the right thing anyway.
2176 * Since some of the 8-bit storage tests have bugs in this are,
2177 * just make it a warning for now.
2178 */
2179 vtn_warn("Variables in the StorageBuffer storage class must "
2180 "have a struct type with the Block decoration");
2181 }
2182 }
2183 b->shader->info.num_ssbos++;
2184 break;
2185 case vtn_variable_mode_uniform:
2186 if (glsl_type_is_image(without_array->type))
2187 b->shader->info.num_images++;
2188 else if (glsl_type_is_sampler(without_array->type))
2189 b->shader->info.num_textures++;
2190 break;
2191 case vtn_variable_mode_push_constant:
2192 b->shader->num_uniforms = vtn_type_block_size(b, type);
2193 break;
2194
2195 case vtn_variable_mode_image:
2196 vtn_fail("Cannot create a variable with the Image storage class");
2197 break;
2198
2199 case vtn_variable_mode_phys_ssbo:
2200 vtn_fail("Cannot create a variable with the "
2201 "PhysicalStorageBuffer storage class");
2202 break;
2203
2204 default:
2205 /* No tallying is needed */
2206 break;
2207 }
2208
2209 struct vtn_variable *var = rzalloc(b, struct vtn_variable);
2210 var->type = type;
2211 var->mode = mode;
2212 var->base_location = -1;
2213
2214 vtn_assert(val->value_type == vtn_value_type_pointer);
2215 val->pointer = vtn_pointer_for_variable(b, var, ptr_type);
2216
2217 switch (var->mode) {
2218 case vtn_variable_mode_function:
2219 case vtn_variable_mode_private:
2220 case vtn_variable_mode_uniform:
2221 /* For these, we create the variable normally */
2222 var->var = rzalloc(b->shader, nir_variable);
2223 var->var->name = ralloc_strdup(var->var, val->name);
2224
2225 if (storage_class == SpvStorageClassAtomicCounter) {
2226 /* Need to tweak the nir type here as at vtn_handle_type we don't
2227 * have the access to storage_class, that is the one that points us
2228 * that is an atomic uint.
2229 */
2230 var->var->type = repair_atomic_type(var->type->type);
2231 } else {
2232 /* Private variables don't have any explicit layout but some layouts
2233 * may have leaked through due to type deduplication in the SPIR-V.
2234 */
2235 var->var->type = var->type->type;
2236 }
2237 var->var->data.mode = nir_mode;
2238 var->var->data.location = -1;
2239 var->var->interface_type = NULL;
2240 break;
2241
2242 case vtn_variable_mode_ubo:
2243 case vtn_variable_mode_ssbo:
2244 var->var = rzalloc(b->shader, nir_variable);
2245 var->var->name = ralloc_strdup(var->var, val->name);
2246
2247 var->var->type = var->type->type;
2248 var->var->interface_type = var->type->type;
2249
2250 var->var->data.mode = nir_mode;
2251 var->var->data.location = -1;
2252
2253 break;
2254
2255 case vtn_variable_mode_workgroup:
2256 /* Create the variable normally */
2257 var->var = rzalloc(b->shader, nir_variable);
2258 var->var->name = ralloc_strdup(var->var, val->name);
2259 /* Workgroup variables don't have any explicit layout but some
2260 * layouts may have leaked through due to type deduplication in the
2261 * SPIR-V.
2262 */
2263 var->var->type = var->type->type;
2264 var->var->data.mode = nir_var_mem_shared;
2265 break;
2266
2267 case vtn_variable_mode_input:
2268 case vtn_variable_mode_output: {
2269 /* In order to know whether or not we're a per-vertex inout, we need
2270 * the patch qualifier. This means walking the variable decorations
2271 * early before we actually create any variables. Not a big deal.
2272 *
2273 * GLSLang really likes to place decorations in the most interior
2274 * thing it possibly can. In particular, if you have a struct, it
2275 * will place the patch decorations on the struct members. This
2276 * should be handled by the variable splitting below just fine.
2277 *
2278 * If you have an array-of-struct, things get even more weird as it
2279 * will place the patch decorations on the struct even though it's
2280 * inside an array and some of the members being patch and others not
2281 * makes no sense whatsoever. Since the only sensible thing is for
2282 * it to be all or nothing, we'll call it patch if any of the members
2283 * are declared patch.
2284 */
2285 var->patch = false;
2286 vtn_foreach_decoration(b, val, var_is_patch_cb, &var->patch);
2287 if (glsl_type_is_array(var->type->type) &&
2288 glsl_type_is_struct_or_ifc(without_array->type)) {
2289 vtn_foreach_decoration(b, vtn_value(b, without_array->id,
2290 vtn_value_type_type),
2291 var_is_patch_cb, &var->patch);
2292 }
2293
2294 /* For inputs and outputs, we immediately split structures. This
2295 * is for a couple of reasons. For one, builtins may all come in
2296 * a struct and we really want those split out into separate
2297 * variables. For another, interpolation qualifiers can be
2298 * applied to members of the top-level struct ane we need to be
2299 * able to preserve that information.
2300 */
2301
2302 struct vtn_type *per_vertex_type = var->type;
2303 if (is_per_vertex_inout(var, b->shader->info.stage)) {
2304 /* In Geometry shaders (and some tessellation), inputs come
2305 * in per-vertex arrays. However, some builtins come in
2306 * non-per-vertex, hence the need for the is_array check. In
2307 * any case, there are no non-builtin arrays allowed so this
2308 * check should be sufficient.
2309 */
2310 per_vertex_type = var->type->array_element;
2311 }
2312
2313 var->var = rzalloc(b->shader, nir_variable);
2314 var->var->name = ralloc_strdup(var->var, val->name);
2315 /* In Vulkan, shader I/O variables don't have any explicit layout but
2316 * some layouts may have leaked through due to type deduplication in
2317 * the SPIR-V. We do, however, keep the layouts in the variable's
2318 * interface_type because we need offsets for XFB arrays of blocks.
2319 */
2320 var->var->type = var->type->type;
2321 var->var->data.mode = nir_mode;
2322 var->var->data.patch = var->patch;
2323
2324 /* Figure out the interface block type. */
2325 struct vtn_type *iface_type = per_vertex_type;
2326 if (var->mode == vtn_variable_mode_output &&
2327 (b->shader->info.stage == MESA_SHADER_VERTEX ||
2328 b->shader->info.stage == MESA_SHADER_TESS_EVAL ||
2329 b->shader->info.stage == MESA_SHADER_GEOMETRY)) {
2330 /* For vertex data outputs, we can end up with arrays of blocks for
2331 * transform feedback where each array element corresponds to a
2332 * different XFB output buffer.
2333 */
2334 while (iface_type->base_type == vtn_base_type_array)
2335 iface_type = iface_type->array_element;
2336 }
2337 if (iface_type->base_type == vtn_base_type_struct && iface_type->block)
2338 var->var->interface_type = iface_type->type;
2339
2340 if (per_vertex_type->base_type == vtn_base_type_struct &&
2341 per_vertex_type->block) {
2342 /* It's a struct. Set it up as per-member. */
2343 var->var->num_members = glsl_get_length(per_vertex_type->type);
2344 var->var->members = rzalloc_array(var->var, struct nir_variable_data,
2345 var->var->num_members);
2346
2347 for (unsigned i = 0; i < var->var->num_members; i++) {
2348 var->var->members[i].mode = nir_mode;
2349 var->var->members[i].patch = var->patch;
2350 var->var->members[i].location = -1;
2351 }
2352 }
2353
2354 /* For inputs and outputs, we need to grab locations and builtin
2355 * information from the per-vertex type.
2356 */
2357 vtn_foreach_decoration(b, vtn_value(b, per_vertex_type->id,
2358 vtn_value_type_type),
2359 var_decoration_cb, var);
2360 break;
2361 }
2362
2363 case vtn_variable_mode_push_constant:
2364 case vtn_variable_mode_cross_workgroup:
2365 /* These don't need actual variables. */
2366 break;
2367
2368 case vtn_variable_mode_image:
2369 case vtn_variable_mode_phys_ssbo:
2370 unreachable("Should have been caught before");
2371 }
2372
2373 /* We can only have one type of initializer */
2374 assert(!(const_initializer && var_initializer));
2375 if (const_initializer) {
2376 var->var->constant_initializer =
2377 nir_constant_clone(const_initializer, var->var);
2378 }
2379 if (var_initializer)
2380 var->var->pointer_initializer = var_initializer;
2381
2382 vtn_foreach_decoration(b, val, var_decoration_cb, var);
2383 vtn_foreach_decoration(b, val, ptr_decoration_cb, val->pointer);
2384
2385 if ((var->mode == vtn_variable_mode_input ||
2386 var->mode == vtn_variable_mode_output) &&
2387 var->var->members) {
2388 assign_missing_member_locations(var);
2389 }
2390
2391 if (var->mode == vtn_variable_mode_uniform ||
2392 var->mode == vtn_variable_mode_ubo ||
2393 var->mode == vtn_variable_mode_ssbo) {
2394 /* XXX: We still need the binding information in the nir_variable
2395 * for these. We should fix that.
2396 */
2397 var->var->data.binding = var->binding;
2398 var->var->data.explicit_binding = var->explicit_binding;
2399 var->var->data.descriptor_set = var->descriptor_set;
2400 var->var->data.index = var->input_attachment_index;
2401 var->var->data.offset = var->offset;
2402
2403 if (glsl_type_is_image(without_array->type))
2404 var->var->data.image.format = without_array->image_format;
2405 }
2406
2407 if (var->mode == vtn_variable_mode_function) {
2408 vtn_assert(var->var != NULL && var->var->members == NULL);
2409 nir_function_impl_add_variable(b->nb.impl, var->var);
2410 } else if (var->var) {
2411 nir_shader_add_variable(b->shader, var->var);
2412 } else {
2413 vtn_assert(vtn_pointer_is_external_block(b, val->pointer));
2414 }
2415 }
2416
2417 static void
2418 vtn_assert_types_equal(struct vtn_builder *b, SpvOp opcode,
2419 struct vtn_type *dst_type,
2420 struct vtn_type *src_type)
2421 {
2422 if (dst_type->id == src_type->id)
2423 return;
2424
2425 if (vtn_types_compatible(b, dst_type, src_type)) {
2426 /* Early versions of GLSLang would re-emit types unnecessarily and you
2427 * would end up with OpLoad, OpStore, or OpCopyMemory opcodes which have
2428 * mismatched source and destination types.
2429 *
2430 * https://github.com/KhronosGroup/glslang/issues/304
2431 * https://github.com/KhronosGroup/glslang/issues/307
2432 * https://bugs.freedesktop.org/show_bug.cgi?id=104338
2433 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2434 */
2435 vtn_warn("Source and destination types of %s do not have the same "
2436 "ID (but are compatible): %u vs %u",
2437 spirv_op_to_string(opcode), dst_type->id, src_type->id);
2438 return;
2439 }
2440
2441 vtn_fail("Source and destination types of %s do not match: %s vs. %s",
2442 spirv_op_to_string(opcode),
2443 glsl_get_type_name(dst_type->type),
2444 glsl_get_type_name(src_type->type));
2445 }
2446
2447 static nir_ssa_def *
2448 nir_shrink_zero_pad_vec(nir_builder *b, nir_ssa_def *val,
2449 unsigned num_components)
2450 {
2451 if (val->num_components == num_components)
2452 return val;
2453
2454 nir_ssa_def *comps[NIR_MAX_VEC_COMPONENTS];
2455 for (unsigned i = 0; i < num_components; i++) {
2456 if (i < val->num_components)
2457 comps[i] = nir_channel(b, val, i);
2458 else
2459 comps[i] = nir_imm_intN_t(b, 0, val->bit_size);
2460 }
2461 return nir_vec(b, comps, num_components);
2462 }
2463
2464 static nir_ssa_def *
2465 nir_sloppy_bitcast(nir_builder *b, nir_ssa_def *val,
2466 const struct glsl_type *type)
2467 {
2468 const unsigned num_components = glsl_get_vector_elements(type);
2469 const unsigned bit_size = glsl_get_bit_size(type);
2470
2471 /* First, zero-pad to ensure that the value is big enough that when we
2472 * bit-cast it, we don't loose anything.
2473 */
2474 if (val->bit_size < bit_size) {
2475 const unsigned src_num_components_needed =
2476 vtn_align_u32(val->num_components, bit_size / val->bit_size);
2477 val = nir_shrink_zero_pad_vec(b, val, src_num_components_needed);
2478 }
2479
2480 val = nir_bitcast_vector(b, val, bit_size);
2481
2482 return nir_shrink_zero_pad_vec(b, val, num_components);
2483 }
2484
2485 void
2486 vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
2487 const uint32_t *w, unsigned count)
2488 {
2489 switch (opcode) {
2490 case SpvOpUndef: {
2491 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_undef);
2492 val->type = vtn_value(b, w[1], vtn_value_type_type)->type;
2493 break;
2494 }
2495
2496 case SpvOpVariable: {
2497 struct vtn_type *ptr_type = vtn_value(b, w[1], vtn_value_type_type)->type;
2498
2499 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_pointer);
2500
2501 SpvStorageClass storage_class = w[3];
2502 nir_constant *const_initializer = NULL;
2503 nir_variable *var_initializer = NULL;
2504 if (count > 4) {
2505 struct vtn_value *init = vtn_untyped_value(b, w[4]);
2506 switch (init->value_type) {
2507 case vtn_value_type_constant:
2508 const_initializer = init->constant;
2509 break;
2510 case vtn_value_type_pointer:
2511 var_initializer = init->pointer->var->var;
2512 break;
2513 default:
2514 vtn_fail("SPIR-V variable initializer %u must be constant or pointer",
2515 w[4]);
2516 }
2517 }
2518
2519 vtn_create_variable(b, val, ptr_type, storage_class, const_initializer, var_initializer);
2520
2521 break;
2522 }
2523
2524 case SpvOpAccessChain:
2525 case SpvOpPtrAccessChain:
2526 case SpvOpInBoundsAccessChain:
2527 case SpvOpInBoundsPtrAccessChain: {
2528 struct vtn_access_chain *chain = vtn_access_chain_create(b, count - 4);
2529 enum gl_access_qualifier access = 0;
2530 chain->ptr_as_array = (opcode == SpvOpPtrAccessChain || opcode == SpvOpInBoundsPtrAccessChain);
2531
2532 unsigned idx = 0;
2533 for (int i = 4; i < count; i++) {
2534 struct vtn_value *link_val = vtn_untyped_value(b, w[i]);
2535 if (link_val->value_type == vtn_value_type_constant) {
2536 chain->link[idx].mode = vtn_access_mode_literal;
2537 chain->link[idx].id = vtn_constant_int(b, w[i]);
2538 } else {
2539 chain->link[idx].mode = vtn_access_mode_id;
2540 chain->link[idx].id = w[i];
2541 }
2542 access |= vtn_value_access(link_val);
2543 idx++;
2544 }
2545
2546 struct vtn_type *ptr_type = vtn_value(b, w[1], vtn_value_type_type)->type;
2547 struct vtn_value *base_val = vtn_untyped_value(b, w[3]);
2548 if (base_val->value_type == vtn_value_type_sampled_image) {
2549 /* This is rather insane. SPIR-V allows you to use OpSampledImage
2550 * to combine an array of images with a single sampler to get an
2551 * array of sampled images that all share the same sampler.
2552 * Fortunately, this means that we can more-or-less ignore the
2553 * sampler when crawling the access chain, but it does leave us
2554 * with this rather awkward little special-case.
2555 */
2556 struct vtn_value *val =
2557 vtn_push_value(b, w[2], vtn_value_type_sampled_image);
2558 val->sampled_image = ralloc(b, struct vtn_sampled_image);
2559 val->sampled_image->image =
2560 vtn_pointer_dereference(b, base_val->sampled_image->image, chain);
2561 val->sampled_image->sampler = base_val->sampled_image->sampler;
2562 val->sampled_image->image =
2563 vtn_decorate_pointer(b, val, val->sampled_image->image);
2564 val->sampled_image->sampler =
2565 vtn_decorate_pointer(b, val, val->sampled_image->sampler);
2566 } else {
2567 vtn_assert(base_val->value_type == vtn_value_type_pointer);
2568 struct vtn_pointer *ptr =
2569 vtn_pointer_dereference(b, base_val->pointer, chain);
2570 ptr->ptr_type = ptr_type;
2571 ptr->access |= access;
2572 vtn_push_value_pointer(b, w[2], ptr);
2573 }
2574 break;
2575 }
2576
2577 case SpvOpCopyMemory: {
2578 struct vtn_value *dest = vtn_value(b, w[1], vtn_value_type_pointer);
2579 struct vtn_value *src = vtn_value(b, w[2], vtn_value_type_pointer);
2580
2581 vtn_assert_types_equal(b, opcode, dest->type->deref, src->type->deref);
2582
2583 vtn_variable_copy(b, dest->pointer, src->pointer);
2584 break;
2585 }
2586
2587 case SpvOpLoad: {
2588 struct vtn_type *res_type =
2589 vtn_value(b, w[1], vtn_value_type_type)->type;
2590 struct vtn_value *src_val = vtn_value(b, w[3], vtn_value_type_pointer);
2591 struct vtn_pointer *src = src_val->pointer;
2592
2593 vtn_assert_types_equal(b, opcode, res_type, src_val->type->deref);
2594
2595 if (res_type->base_type == vtn_base_type_image ||
2596 res_type->base_type == vtn_base_type_sampler) {
2597 vtn_push_value_pointer(b, w[2], src);
2598 return;
2599 } else if (res_type->base_type == vtn_base_type_sampled_image) {
2600 struct vtn_value *val =
2601 vtn_push_value(b, w[2], vtn_value_type_sampled_image);
2602 val->sampled_image = ralloc(b, struct vtn_sampled_image);
2603 val->sampled_image->image = val->sampled_image->sampler =
2604 vtn_decorate_pointer(b, val, src);
2605 return;
2606 }
2607
2608 if (count > 4) {
2609 unsigned idx = 5;
2610 SpvMemoryAccessMask access = w[4];
2611 if (access & SpvMemoryAccessAlignedMask)
2612 idx++;
2613
2614 if (access & SpvMemoryAccessMakePointerVisibleMask) {
2615 SpvMemorySemanticsMask semantics =
2616 SpvMemorySemanticsMakeVisibleMask |
2617 vtn_storage_class_to_memory_semantics(src->ptr_type->storage_class);
2618
2619 SpvScope scope = vtn_constant_uint(b, w[idx]);
2620 vtn_emit_memory_barrier(b, scope, semantics);
2621 }
2622 }
2623
2624 vtn_push_ssa(b, w[2], res_type, vtn_variable_load(b, src));
2625 break;
2626 }
2627
2628 case SpvOpStore: {
2629 struct vtn_value *dest_val = vtn_value(b, w[1], vtn_value_type_pointer);
2630 struct vtn_pointer *dest = dest_val->pointer;
2631 struct vtn_value *src_val = vtn_untyped_value(b, w[2]);
2632
2633 /* OpStore requires us to actually have a storage type */
2634 vtn_fail_if(dest->type->type == NULL,
2635 "Invalid destination type for OpStore");
2636
2637 if (glsl_get_base_type(dest->type->type) == GLSL_TYPE_BOOL &&
2638 glsl_get_base_type(src_val->type->type) == GLSL_TYPE_UINT) {
2639 /* Early versions of GLSLang would use uint types for UBOs/SSBOs but
2640 * would then store them to a local variable as bool. Work around
2641 * the issue by doing an implicit conversion.
2642 *
2643 * https://github.com/KhronosGroup/glslang/issues/170
2644 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2645 */
2646 vtn_warn("OpStore of value of type OpTypeInt to a pointer to type "
2647 "OpTypeBool. Doing an implicit conversion to work around "
2648 "the problem.");
2649 struct vtn_ssa_value *bool_ssa =
2650 vtn_create_ssa_value(b, dest->type->type);
2651 bool_ssa->def = nir_i2b(&b->nb, vtn_ssa_value(b, w[2])->def);
2652 vtn_variable_store(b, bool_ssa, dest);
2653 break;
2654 }
2655
2656 vtn_assert_types_equal(b, opcode, dest_val->type->deref, src_val->type);
2657
2658 if (glsl_type_is_sampler(dest->type->type)) {
2659 if (b->wa_glslang_179) {
2660 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
2661 "propagation to workaround the problem.");
2662 vtn_assert(dest->var->copy_prop_sampler == NULL);
2663 struct vtn_value *v = vtn_untyped_value(b, w[2]);
2664 if (v->value_type == vtn_value_type_sampled_image) {
2665 dest->var->copy_prop_sampler = v->sampled_image->sampler;
2666 } else {
2667 vtn_assert(v->value_type == vtn_value_type_pointer);
2668 dest->var->copy_prop_sampler = v->pointer;
2669 }
2670 } else {
2671 vtn_fail("Vulkan does not allow OpStore of a sampler or image.");
2672 }
2673 break;
2674 }
2675
2676 struct vtn_ssa_value *src = vtn_ssa_value(b, w[2]);
2677 vtn_variable_store(b, src, dest);
2678
2679 if (count > 3) {
2680 unsigned idx = 4;
2681 SpvMemoryAccessMask access = w[3];
2682
2683 if (access & SpvMemoryAccessAlignedMask)
2684 idx++;
2685
2686 if (access & SpvMemoryAccessMakePointerAvailableMask) {
2687 SpvMemorySemanticsMask semantics =
2688 SpvMemorySemanticsMakeAvailableMask |
2689 vtn_storage_class_to_memory_semantics(dest->ptr_type->storage_class);
2690 SpvScope scope = vtn_constant_uint(b, w[idx]);
2691 vtn_emit_memory_barrier(b, scope, semantics);
2692 }
2693 }
2694 break;
2695 }
2696
2697 case SpvOpArrayLength: {
2698 struct vtn_pointer *ptr =
2699 vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
2700 const uint32_t field = w[4];
2701
2702 vtn_fail_if(ptr->type->base_type != vtn_base_type_struct,
2703 "OpArrayLength must take a pointer to a structure type");
2704 vtn_fail_if(field != ptr->type->length - 1 ||
2705 ptr->type->members[field]->base_type != vtn_base_type_array,
2706 "OpArrayLength must reference the last memeber of the "
2707 "structure and that must be an array");
2708
2709 const uint32_t offset = ptr->type->offsets[field];
2710 const uint32_t stride = ptr->type->members[field]->stride;
2711
2712 if (!ptr->block_index) {
2713 struct vtn_access_chain chain = {
2714 .length = 0,
2715 };
2716 ptr = vtn_pointer_dereference(b, ptr, &chain);
2717 vtn_assert(ptr->block_index);
2718 }
2719
2720 nir_intrinsic_instr *instr =
2721 nir_intrinsic_instr_create(b->nb.shader,
2722 nir_intrinsic_get_buffer_size);
2723 instr->src[0] = nir_src_for_ssa(ptr->block_index);
2724 nir_ssa_dest_init(&instr->instr, &instr->dest, 1, 32, NULL);
2725 nir_builder_instr_insert(&b->nb, &instr->instr);
2726 nir_ssa_def *buf_size = &instr->dest.ssa;
2727
2728 /* array_length = max(buffer_size - offset, 0) / stride */
2729 nir_ssa_def *array_length =
2730 nir_idiv(&b->nb,
2731 nir_imax(&b->nb,
2732 nir_isub(&b->nb,
2733 buf_size,
2734 nir_imm_int(&b->nb, offset)),
2735 nir_imm_int(&b->nb, 0u)),
2736 nir_imm_int(&b->nb, stride));
2737
2738 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2739 val->ssa = vtn_create_ssa_value(b, glsl_uint_type());
2740 val->ssa->def = array_length;
2741 break;
2742 }
2743
2744 case SpvOpConvertPtrToU: {
2745 struct vtn_value *u_val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2746
2747 vtn_fail_if(u_val->type->base_type != vtn_base_type_vector &&
2748 u_val->type->base_type != vtn_base_type_scalar,
2749 "OpConvertPtrToU can only be used to cast to a vector or "
2750 "scalar type");
2751
2752 /* The pointer will be converted to an SSA value automatically */
2753 struct vtn_ssa_value *ptr_ssa = vtn_ssa_value(b, w[3]);
2754
2755 u_val->ssa = vtn_create_ssa_value(b, u_val->type->type);
2756 u_val->ssa->def = nir_sloppy_bitcast(&b->nb, ptr_ssa->def, u_val->type->type);
2757 u_val->ssa->access |= ptr_ssa->access;
2758 break;
2759 }
2760
2761 case SpvOpConvertUToPtr: {
2762 struct vtn_value *ptr_val =
2763 vtn_push_value(b, w[2], vtn_value_type_pointer);
2764 struct vtn_value *u_val = vtn_value(b, w[3], vtn_value_type_ssa);
2765
2766 vtn_fail_if(ptr_val->type->type == NULL,
2767 "OpConvertUToPtr can only be used on physical pointers");
2768
2769 vtn_fail_if(u_val->type->base_type != vtn_base_type_vector &&
2770 u_val->type->base_type != vtn_base_type_scalar,
2771 "OpConvertUToPtr can only be used to cast from a vector or "
2772 "scalar type");
2773
2774 nir_ssa_def *ptr_ssa = nir_sloppy_bitcast(&b->nb, u_val->ssa->def,
2775 ptr_val->type->type);
2776 ptr_val->pointer = vtn_pointer_from_ssa(b, ptr_ssa, ptr_val->type);
2777 vtn_foreach_decoration(b, ptr_val, ptr_decoration_cb, ptr_val->pointer);
2778 ptr_val->pointer->access |= u_val->ssa->access;
2779 break;
2780 }
2781
2782 case SpvOpCopyMemorySized:
2783 default:
2784 vtn_fail_with_opcode("Unhandled opcode", opcode);
2785 }
2786 }