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