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