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