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