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