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