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