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