spirv: Use nir_var_mem_constant for UniformConstant data in CL
[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 if (b->options->constant_as_global) {
1807 mode = vtn_variable_mode_cross_workgroup;
1808 nir_mode = nir_var_mem_global;
1809 } else {
1810 mode = vtn_variable_mode_constant;
1811 nir_mode = nir_var_mem_constant;
1812 }
1813 } else {
1814 mode = vtn_variable_mode_uniform;
1815 nir_mode = nir_var_uniform;
1816 }
1817 break;
1818 case SpvStorageClassPushConstant:
1819 mode = vtn_variable_mode_push_constant;
1820 nir_mode = nir_var_uniform;
1821 break;
1822 case SpvStorageClassInput:
1823 mode = vtn_variable_mode_input;
1824 nir_mode = nir_var_shader_in;
1825 break;
1826 case SpvStorageClassOutput:
1827 mode = vtn_variable_mode_output;
1828 nir_mode = nir_var_shader_out;
1829 break;
1830 case SpvStorageClassPrivate:
1831 mode = vtn_variable_mode_private;
1832 nir_mode = nir_var_shader_temp;
1833 break;
1834 case SpvStorageClassFunction:
1835 mode = vtn_variable_mode_function;
1836 nir_mode = nir_var_function_temp;
1837 break;
1838 case SpvStorageClassWorkgroup:
1839 mode = vtn_variable_mode_workgroup;
1840 nir_mode = nir_var_mem_shared;
1841 break;
1842 case SpvStorageClassAtomicCounter:
1843 mode = vtn_variable_mode_atomic_counter;
1844 nir_mode = nir_var_uniform;
1845 break;
1846 case SpvStorageClassCrossWorkgroup:
1847 mode = vtn_variable_mode_cross_workgroup;
1848 nir_mode = nir_var_mem_global;
1849 break;
1850 case SpvStorageClassImage:
1851 mode = vtn_variable_mode_image;
1852 nir_mode = nir_var_mem_ubo;
1853 break;
1854 case SpvStorageClassGeneric:
1855 default:
1856 vtn_fail("Unhandled variable storage class: %s (%u)",
1857 spirv_storageclass_to_string(class), class);
1858 }
1859
1860 if (nir_mode_out)
1861 *nir_mode_out = nir_mode;
1862
1863 return mode;
1864 }
1865
1866 nir_address_format
1867 vtn_mode_to_address_format(struct vtn_builder *b, enum vtn_variable_mode mode)
1868 {
1869 switch (mode) {
1870 case vtn_variable_mode_ubo:
1871 return b->options->ubo_addr_format;
1872
1873 case vtn_variable_mode_ssbo:
1874 return b->options->ssbo_addr_format;
1875
1876 case vtn_variable_mode_phys_ssbo:
1877 return b->options->phys_ssbo_addr_format;
1878
1879 case vtn_variable_mode_push_constant:
1880 return b->options->push_const_addr_format;
1881
1882 case vtn_variable_mode_workgroup:
1883 return b->options->shared_addr_format;
1884
1885 case vtn_variable_mode_cross_workgroup:
1886 return b->options->global_addr_format;
1887
1888 case vtn_variable_mode_constant:
1889 return b->options->constant_addr_format;
1890
1891 case vtn_variable_mode_function:
1892 if (b->physical_ptrs)
1893 return b->options->temp_addr_format;
1894 /* Fall through. */
1895
1896 case vtn_variable_mode_private:
1897 case vtn_variable_mode_uniform:
1898 case vtn_variable_mode_atomic_counter:
1899 case vtn_variable_mode_input:
1900 case vtn_variable_mode_output:
1901 case vtn_variable_mode_image:
1902 return nir_address_format_logical;
1903 }
1904
1905 unreachable("Invalid variable mode");
1906 }
1907
1908 nir_ssa_def *
1909 vtn_pointer_to_ssa(struct vtn_builder *b, struct vtn_pointer *ptr)
1910 {
1911 if (vtn_pointer_uses_ssa_offset(b, ptr)) {
1912 /* This pointer needs to have a pointer type with actual storage */
1913 vtn_assert(ptr->ptr_type);
1914 vtn_assert(ptr->ptr_type->type);
1915
1916 if (!ptr->offset) {
1917 /* If we don't have an offset then we must be a pointer to the variable
1918 * itself.
1919 */
1920 vtn_assert(!ptr->offset && !ptr->block_index);
1921
1922 struct vtn_access_chain chain = {
1923 .length = 0,
1924 };
1925 ptr = vtn_ssa_offset_pointer_dereference(b, ptr, &chain);
1926 }
1927
1928 vtn_assert(ptr->offset);
1929 if (ptr->block_index) {
1930 vtn_assert(ptr->mode == vtn_variable_mode_ubo ||
1931 ptr->mode == vtn_variable_mode_ssbo);
1932 return nir_vec2(&b->nb, ptr->block_index, ptr->offset);
1933 } else {
1934 vtn_assert(ptr->mode == vtn_variable_mode_workgroup);
1935 return ptr->offset;
1936 }
1937 } else {
1938 if (vtn_pointer_is_external_block(b, ptr) &&
1939 vtn_type_contains_block(b, ptr->type) &&
1940 ptr->mode != vtn_variable_mode_phys_ssbo) {
1941 /* In this case, we're looking for a block index and not an actual
1942 * deref.
1943 *
1944 * For PhysicalStorageBuffer pointers, we don't have a block index
1945 * at all because we get the pointer directly from the client. This
1946 * assumes that there will never be a SSBO binding variable using the
1947 * PhysicalStorageBuffer storage class. This assumption appears
1948 * to be correct according to the Vulkan spec because the table,
1949 * "Shader Resource and Storage Class Correspondence," the only the
1950 * Uniform storage class with BufferBlock or the StorageBuffer
1951 * storage class with Block can be used.
1952 */
1953 if (!ptr->block_index) {
1954 /* If we don't have a block_index then we must be a pointer to the
1955 * variable itself.
1956 */
1957 vtn_assert(!ptr->deref);
1958
1959 struct vtn_access_chain chain = {
1960 .length = 0,
1961 };
1962 ptr = vtn_nir_deref_pointer_dereference(b, ptr, &chain);
1963 }
1964
1965 return ptr->block_index;
1966 } else {
1967 return &vtn_pointer_to_deref(b, ptr)->dest.ssa;
1968 }
1969 }
1970 }
1971
1972 struct vtn_pointer *
1973 vtn_pointer_from_ssa(struct vtn_builder *b, nir_ssa_def *ssa,
1974 struct vtn_type *ptr_type)
1975 {
1976 vtn_assert(ptr_type->base_type == vtn_base_type_pointer);
1977
1978 struct vtn_pointer *ptr = rzalloc(b, struct vtn_pointer);
1979 struct vtn_type *without_array =
1980 vtn_type_without_array(ptr_type->deref);
1981
1982 nir_variable_mode nir_mode;
1983 ptr->mode = vtn_storage_class_to_mode(b, ptr_type->storage_class,
1984 without_array, &nir_mode);
1985 ptr->type = ptr_type->deref;
1986 ptr->ptr_type = ptr_type;
1987
1988 if (vtn_pointer_uses_ssa_offset(b, ptr)) {
1989 /* This pointer type needs to have actual storage */
1990 vtn_assert(ptr_type->type);
1991 if (ptr->mode == vtn_variable_mode_ubo ||
1992 ptr->mode == vtn_variable_mode_ssbo) {
1993 vtn_assert(ssa->num_components == 2);
1994 ptr->block_index = nir_channel(&b->nb, ssa, 0);
1995 ptr->offset = nir_channel(&b->nb, ssa, 1);
1996 } else {
1997 vtn_assert(ssa->num_components == 1);
1998 ptr->block_index = NULL;
1999 ptr->offset = ssa;
2000 }
2001 } else {
2002 const struct glsl_type *deref_type =
2003 vtn_type_get_nir_type(b, ptr_type->deref, ptr->mode);
2004 if (!vtn_pointer_is_external_block(b, ptr)) {
2005 ptr->deref = nir_build_deref_cast(&b->nb, ssa, nir_mode,
2006 deref_type, ptr_type->stride);
2007 } else if (vtn_type_contains_block(b, ptr->type) &&
2008 ptr->mode != vtn_variable_mode_phys_ssbo) {
2009 /* This is a pointer to somewhere in an array of blocks, not a
2010 * pointer to somewhere inside the block. Set the block index
2011 * instead of making a cast.
2012 */
2013 ptr->block_index = ssa;
2014 } else {
2015 /* This is a pointer to something internal or a pointer inside a
2016 * block. It's just a regular cast.
2017 *
2018 * For PhysicalStorageBuffer pointers, we don't have a block index
2019 * at all because we get the pointer directly from the client. This
2020 * assumes that there will never be a SSBO binding variable using the
2021 * PhysicalStorageBuffer storage class. This assumption appears
2022 * to be correct according to the Vulkan spec because the table,
2023 * "Shader Resource and Storage Class Correspondence," the only the
2024 * Uniform storage class with BufferBlock or the StorageBuffer
2025 * storage class with Block can be used.
2026 */
2027 ptr->deref = nir_build_deref_cast(&b->nb, ssa, nir_mode,
2028 deref_type, ptr_type->stride);
2029 ptr->deref->dest.ssa.num_components =
2030 glsl_get_vector_elements(ptr_type->type);
2031 ptr->deref->dest.ssa.bit_size = glsl_get_bit_size(ptr_type->type);
2032 }
2033 }
2034
2035 return ptr;
2036 }
2037
2038 static bool
2039 is_per_vertex_inout(const struct vtn_variable *var, gl_shader_stage stage)
2040 {
2041 if (var->patch || !glsl_type_is_array(var->type->type))
2042 return false;
2043
2044 if (var->mode == vtn_variable_mode_input) {
2045 return stage == MESA_SHADER_TESS_CTRL ||
2046 stage == MESA_SHADER_TESS_EVAL ||
2047 stage == MESA_SHADER_GEOMETRY;
2048 }
2049
2050 if (var->mode == vtn_variable_mode_output)
2051 return stage == MESA_SHADER_TESS_CTRL;
2052
2053 return false;
2054 }
2055
2056 static void
2057 assign_missing_member_locations(struct vtn_variable *var)
2058 {
2059 unsigned length =
2060 glsl_get_length(glsl_without_array(var->type->type));
2061 int location = var->base_location;
2062
2063 for (unsigned i = 0; i < length; i++) {
2064 /* From the Vulkan spec:
2065 *
2066 * “If the structure type is a Block but without a Location, then each
2067 * of its members must have a Location decoration.”
2068 *
2069 */
2070 if (var->type->block) {
2071 assert(var->base_location != -1 ||
2072 var->var->members[i].location != -1);
2073 }
2074
2075 /* From the Vulkan spec:
2076 *
2077 * “Any member with its own Location decoration is assigned that
2078 * location. Each remaining member is assigned the location after the
2079 * immediately preceding member in declaration order.”
2080 */
2081 if (var->var->members[i].location != -1)
2082 location = var->var->members[i].location;
2083 else
2084 var->var->members[i].location = location;
2085
2086 /* Below we use type instead of interface_type, because interface_type
2087 * is only available when it is a Block. This code also supports
2088 * input/outputs that are just structs
2089 */
2090 const struct glsl_type *member_type =
2091 glsl_get_struct_field(glsl_without_array(var->type->type), i);
2092
2093 location +=
2094 glsl_count_attribute_slots(member_type,
2095 false /* is_gl_vertex_input */);
2096 }
2097 }
2098
2099
2100 static void
2101 vtn_create_variable(struct vtn_builder *b, struct vtn_value *val,
2102 struct vtn_type *ptr_type, SpvStorageClass storage_class,
2103 nir_constant *const_initializer, nir_variable *var_initializer)
2104 {
2105 vtn_assert(ptr_type->base_type == vtn_base_type_pointer);
2106 struct vtn_type *type = ptr_type->deref;
2107
2108 struct vtn_type *without_array = vtn_type_without_array(ptr_type->deref);
2109
2110 enum vtn_variable_mode mode;
2111 nir_variable_mode nir_mode;
2112 mode = vtn_storage_class_to_mode(b, storage_class, without_array, &nir_mode);
2113
2114 switch (mode) {
2115 case vtn_variable_mode_ubo:
2116 /* There's no other way to get vtn_variable_mode_ubo */
2117 vtn_assert(without_array->block);
2118 b->shader->info.num_ubos++;
2119 break;
2120 case vtn_variable_mode_ssbo:
2121 if (storage_class == SpvStorageClassStorageBuffer &&
2122 !without_array->block) {
2123 if (b->variable_pointers) {
2124 vtn_fail("Variables in the StorageBuffer storage class must "
2125 "have a struct type with the Block decoration");
2126 } else {
2127 /* If variable pointers are not present, it's still malformed
2128 * SPIR-V but we can parse it and do the right thing anyway.
2129 * Since some of the 8-bit storage tests have bugs in this are,
2130 * just make it a warning for now.
2131 */
2132 vtn_warn("Variables in the StorageBuffer storage class must "
2133 "have a struct type with the Block decoration");
2134 }
2135 }
2136 b->shader->info.num_ssbos++;
2137 break;
2138 case vtn_variable_mode_uniform:
2139 if (without_array->base_type == vtn_base_type_image) {
2140 if (glsl_type_is_image(without_array->glsl_image))
2141 b->shader->info.num_images++;
2142 else if (glsl_type_is_sampler(without_array->glsl_image))
2143 b->shader->info.num_textures++;
2144 }
2145 break;
2146 case vtn_variable_mode_push_constant:
2147 b->shader->num_uniforms = vtn_type_block_size(b, type);
2148 break;
2149
2150 case vtn_variable_mode_image:
2151 vtn_fail("Cannot create a variable with the Image storage class");
2152 break;
2153
2154 case vtn_variable_mode_phys_ssbo:
2155 vtn_fail("Cannot create a variable with the "
2156 "PhysicalStorageBuffer storage class");
2157 break;
2158
2159 default:
2160 /* No tallying is needed */
2161 break;
2162 }
2163
2164 struct vtn_variable *var = rzalloc(b, struct vtn_variable);
2165 var->type = type;
2166 var->mode = mode;
2167 var->base_location = -1;
2168
2169 val->pointer = rzalloc(b, struct vtn_pointer);
2170 val->pointer->mode = var->mode;
2171 val->pointer->type = var->type;
2172 val->pointer->ptr_type = ptr_type;
2173 val->pointer->var = var;
2174 val->pointer->access = var->type->access;
2175
2176 switch (var->mode) {
2177 case vtn_variable_mode_function:
2178 case vtn_variable_mode_private:
2179 case vtn_variable_mode_uniform:
2180 case vtn_variable_mode_atomic_counter:
2181 case vtn_variable_mode_constant:
2182 /* For these, we create the variable normally */
2183 var->var = rzalloc(b->shader, nir_variable);
2184 var->var->name = ralloc_strdup(var->var, val->name);
2185 var->var->type = vtn_type_get_nir_type(b, var->type, var->mode);
2186 var->var->data.mode = nir_mode;
2187 var->var->data.location = -1;
2188 var->var->interface_type = NULL;
2189 break;
2190
2191 case vtn_variable_mode_ubo:
2192 case vtn_variable_mode_ssbo:
2193 var->var = rzalloc(b->shader, nir_variable);
2194 var->var->name = ralloc_strdup(var->var, val->name);
2195
2196 var->var->type = vtn_type_get_nir_type(b, var->type, var->mode);
2197 var->var->interface_type = var->var->type;
2198
2199 var->var->data.mode = nir_mode;
2200 var->var->data.location = -1;
2201
2202 break;
2203
2204 case vtn_variable_mode_workgroup:
2205 /* Create the variable normally */
2206 var->var = rzalloc(b->shader, nir_variable);
2207 var->var->name = ralloc_strdup(var->var, val->name);
2208 var->var->type = vtn_type_get_nir_type(b, var->type, var->mode);
2209 var->var->data.mode = nir_var_mem_shared;
2210 break;
2211
2212 case vtn_variable_mode_input:
2213 case vtn_variable_mode_output: {
2214 /* In order to know whether or not we're a per-vertex inout, we need
2215 * the patch qualifier. This means walking the variable decorations
2216 * early before we actually create any variables. Not a big deal.
2217 *
2218 * GLSLang really likes to place decorations in the most interior
2219 * thing it possibly can. In particular, if you have a struct, it
2220 * will place the patch decorations on the struct members. This
2221 * should be handled by the variable splitting below just fine.
2222 *
2223 * If you have an array-of-struct, things get even more weird as it
2224 * will place the patch decorations on the struct even though it's
2225 * inside an array and some of the members being patch and others not
2226 * makes no sense whatsoever. Since the only sensible thing is for
2227 * it to be all or nothing, we'll call it patch if any of the members
2228 * are declared patch.
2229 */
2230 var->patch = false;
2231 vtn_foreach_decoration(b, val, var_is_patch_cb, &var->patch);
2232 if (glsl_type_is_array(var->type->type) &&
2233 glsl_type_is_struct_or_ifc(without_array->type)) {
2234 vtn_foreach_decoration(b, vtn_value(b, without_array->id,
2235 vtn_value_type_type),
2236 var_is_patch_cb, &var->patch);
2237 }
2238
2239 /* For inputs and outputs, we immediately split structures. This
2240 * is for a couple of reasons. For one, builtins may all come in
2241 * a struct and we really want those split out into separate
2242 * variables. For another, interpolation qualifiers can be
2243 * applied to members of the top-level struct ane we need to be
2244 * able to preserve that information.
2245 */
2246
2247 struct vtn_type *per_vertex_type = var->type;
2248 if (is_per_vertex_inout(var, b->shader->info.stage)) {
2249 /* In Geometry shaders (and some tessellation), inputs come
2250 * in per-vertex arrays. However, some builtins come in
2251 * non-per-vertex, hence the need for the is_array check. In
2252 * any case, there are no non-builtin arrays allowed so this
2253 * check should be sufficient.
2254 */
2255 per_vertex_type = var->type->array_element;
2256 }
2257
2258 var->var = rzalloc(b->shader, nir_variable);
2259 var->var->name = ralloc_strdup(var->var, val->name);
2260 var->var->type = vtn_type_get_nir_type(b, var->type, var->mode);
2261 var->var->data.mode = nir_mode;
2262 var->var->data.patch = var->patch;
2263
2264 /* Figure out the interface block type. */
2265 struct vtn_type *iface_type = per_vertex_type;
2266 if (var->mode == vtn_variable_mode_output &&
2267 (b->shader->info.stage == MESA_SHADER_VERTEX ||
2268 b->shader->info.stage == MESA_SHADER_TESS_EVAL ||
2269 b->shader->info.stage == MESA_SHADER_GEOMETRY)) {
2270 /* For vertex data outputs, we can end up with arrays of blocks for
2271 * transform feedback where each array element corresponds to a
2272 * different XFB output buffer.
2273 */
2274 while (iface_type->base_type == vtn_base_type_array)
2275 iface_type = iface_type->array_element;
2276 }
2277 if (iface_type->base_type == vtn_base_type_struct && iface_type->block)
2278 var->var->interface_type = vtn_type_get_nir_type(b, iface_type,
2279 var->mode);
2280
2281 if (per_vertex_type->base_type == vtn_base_type_struct &&
2282 per_vertex_type->block) {
2283 /* It's a struct. Set it up as per-member. */
2284 var->var->num_members = glsl_get_length(per_vertex_type->type);
2285 var->var->members = rzalloc_array(var->var, struct nir_variable_data,
2286 var->var->num_members);
2287
2288 for (unsigned i = 0; i < var->var->num_members; i++) {
2289 var->var->members[i].mode = nir_mode;
2290 var->var->members[i].patch = var->patch;
2291 var->var->members[i].location = -1;
2292 }
2293 }
2294
2295 /* For inputs and outputs, we need to grab locations and builtin
2296 * information from the per-vertex type.
2297 */
2298 vtn_foreach_decoration(b, vtn_value(b, per_vertex_type->id,
2299 vtn_value_type_type),
2300 var_decoration_cb, var);
2301 break;
2302 }
2303
2304 case vtn_variable_mode_push_constant:
2305 case vtn_variable_mode_cross_workgroup:
2306 /* These don't need actual variables. */
2307 break;
2308
2309 case vtn_variable_mode_image:
2310 case vtn_variable_mode_phys_ssbo:
2311 unreachable("Should have been caught before");
2312 }
2313
2314 /* We can only have one type of initializer */
2315 assert(!(const_initializer && var_initializer));
2316 if (const_initializer) {
2317 var->var->constant_initializer =
2318 nir_constant_clone(const_initializer, var->var);
2319 }
2320 if (var_initializer)
2321 var->var->pointer_initializer = var_initializer;
2322
2323 if (var->mode == vtn_variable_mode_uniform ||
2324 var->mode == vtn_variable_mode_ssbo) {
2325 /* SSBOs and images are assumed to not alias in the Simple, GLSL and Vulkan memory models */
2326 var->var->data.access |= b->mem_model != SpvMemoryModelOpenCL ? ACCESS_RESTRICT : 0;
2327 }
2328
2329 vtn_foreach_decoration(b, val, var_decoration_cb, var);
2330 vtn_foreach_decoration(b, val, ptr_decoration_cb, val->pointer);
2331
2332 /* Propagate access flags from the OpVariable decorations. */
2333 val->pointer->access |= var->access;
2334
2335 if ((var->mode == vtn_variable_mode_input ||
2336 var->mode == vtn_variable_mode_output) &&
2337 var->var->members) {
2338 assign_missing_member_locations(var);
2339 }
2340
2341 if (var->mode == vtn_variable_mode_uniform ||
2342 var->mode == vtn_variable_mode_ubo ||
2343 var->mode == vtn_variable_mode_ssbo ||
2344 var->mode == vtn_variable_mode_atomic_counter) {
2345 /* XXX: We still need the binding information in the nir_variable
2346 * for these. We should fix that.
2347 */
2348 var->var->data.binding = var->binding;
2349 var->var->data.explicit_binding = var->explicit_binding;
2350 var->var->data.descriptor_set = var->descriptor_set;
2351 var->var->data.index = var->input_attachment_index;
2352 var->var->data.offset = var->offset;
2353
2354 if (glsl_type_is_image(glsl_without_array(var->var->type)))
2355 var->var->data.image.format = without_array->image_format;
2356 }
2357
2358 if (var->mode == vtn_variable_mode_function) {
2359 vtn_assert(var->var != NULL && var->var->members == NULL);
2360 nir_function_impl_add_variable(b->nb.impl, var->var);
2361 } else if (var->var) {
2362 nir_shader_add_variable(b->shader, var->var);
2363 } else {
2364 vtn_assert(vtn_pointer_is_external_block(b, val->pointer));
2365 }
2366 }
2367
2368 static void
2369 vtn_assert_types_equal(struct vtn_builder *b, SpvOp opcode,
2370 struct vtn_type *dst_type,
2371 struct vtn_type *src_type)
2372 {
2373 if (dst_type->id == src_type->id)
2374 return;
2375
2376 if (vtn_types_compatible(b, dst_type, src_type)) {
2377 /* Early versions of GLSLang would re-emit types unnecessarily and you
2378 * would end up with OpLoad, OpStore, or OpCopyMemory opcodes which have
2379 * mismatched source and destination types.
2380 *
2381 * https://github.com/KhronosGroup/glslang/issues/304
2382 * https://github.com/KhronosGroup/glslang/issues/307
2383 * https://bugs.freedesktop.org/show_bug.cgi?id=104338
2384 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2385 */
2386 vtn_warn("Source and destination types of %s do not have the same "
2387 "ID (but are compatible): %u vs %u",
2388 spirv_op_to_string(opcode), dst_type->id, src_type->id);
2389 return;
2390 }
2391
2392 vtn_fail("Source and destination types of %s do not match: %s vs. %s",
2393 spirv_op_to_string(opcode),
2394 glsl_get_type_name(dst_type->type),
2395 glsl_get_type_name(src_type->type));
2396 }
2397
2398 static nir_ssa_def *
2399 nir_shrink_zero_pad_vec(nir_builder *b, nir_ssa_def *val,
2400 unsigned num_components)
2401 {
2402 if (val->num_components == num_components)
2403 return val;
2404
2405 nir_ssa_def *comps[NIR_MAX_VEC_COMPONENTS];
2406 for (unsigned i = 0; i < num_components; i++) {
2407 if (i < val->num_components)
2408 comps[i] = nir_channel(b, val, i);
2409 else
2410 comps[i] = nir_imm_intN_t(b, 0, val->bit_size);
2411 }
2412 return nir_vec(b, comps, num_components);
2413 }
2414
2415 static nir_ssa_def *
2416 nir_sloppy_bitcast(nir_builder *b, nir_ssa_def *val,
2417 const struct glsl_type *type)
2418 {
2419 const unsigned num_components = glsl_get_vector_elements(type);
2420 const unsigned bit_size = glsl_get_bit_size(type);
2421
2422 /* First, zero-pad to ensure that the value is big enough that when we
2423 * bit-cast it, we don't loose anything.
2424 */
2425 if (val->bit_size < bit_size) {
2426 const unsigned src_num_components_needed =
2427 vtn_align_u32(val->num_components, bit_size / val->bit_size);
2428 val = nir_shrink_zero_pad_vec(b, val, src_num_components_needed);
2429 }
2430
2431 val = nir_bitcast_vector(b, val, bit_size);
2432
2433 return nir_shrink_zero_pad_vec(b, val, num_components);
2434 }
2435
2436 static bool
2437 vtn_get_mem_operands(struct vtn_builder *b, const uint32_t *w, unsigned count,
2438 unsigned *idx, SpvMemoryAccessMask *access, unsigned *alignment,
2439 SpvScope *dest_scope, SpvScope *src_scope)
2440 {
2441 *access = 0;
2442 *alignment = 0;
2443 if (*idx >= count)
2444 return false;
2445
2446 *access = w[(*idx)++];
2447 if (*access & SpvMemoryAccessAlignedMask) {
2448 vtn_assert(*idx < count);
2449 *alignment = w[(*idx)++];
2450 }
2451
2452 if (*access & SpvMemoryAccessMakePointerAvailableMask) {
2453 vtn_assert(*idx < count);
2454 vtn_assert(dest_scope);
2455 *dest_scope = vtn_constant_uint(b, w[(*idx)++]);
2456 }
2457
2458 if (*access & SpvMemoryAccessMakePointerVisibleMask) {
2459 vtn_assert(*idx < count);
2460 vtn_assert(src_scope);
2461 *src_scope = vtn_constant_uint(b, w[(*idx)++]);
2462 }
2463
2464 return true;
2465 }
2466
2467 static enum gl_access_qualifier
2468 spv_access_to_gl_access(SpvMemoryAccessMask access)
2469 {
2470 if (access & SpvMemoryAccessVolatileMask)
2471 return ACCESS_VOLATILE;
2472
2473 return 0;
2474 }
2475
2476
2477 SpvMemorySemanticsMask
2478 vtn_mode_to_memory_semantics(enum vtn_variable_mode mode)
2479 {
2480 switch (mode) {
2481 case vtn_variable_mode_ssbo:
2482 case vtn_variable_mode_phys_ssbo:
2483 return SpvMemorySemanticsUniformMemoryMask;
2484 case vtn_variable_mode_workgroup:
2485 return SpvMemorySemanticsWorkgroupMemoryMask;
2486 case vtn_variable_mode_cross_workgroup:
2487 return SpvMemorySemanticsCrossWorkgroupMemoryMask;
2488 case vtn_variable_mode_atomic_counter:
2489 return SpvMemorySemanticsAtomicCounterMemoryMask;
2490 case vtn_variable_mode_image:
2491 return SpvMemorySemanticsImageMemoryMask;
2492 case vtn_variable_mode_output:
2493 return SpvMemorySemanticsOutputMemoryMask;
2494 default:
2495 return SpvMemorySemanticsMaskNone;
2496 }
2497 }
2498
2499 static void
2500 vtn_emit_make_visible_barrier(struct vtn_builder *b, SpvMemoryAccessMask access,
2501 SpvScope scope, enum vtn_variable_mode mode)
2502 {
2503 if (!(access & SpvMemoryAccessMakePointerVisibleMask))
2504 return;
2505
2506 vtn_emit_memory_barrier(b, scope, SpvMemorySemanticsMakeVisibleMask |
2507 SpvMemorySemanticsAcquireMask |
2508 vtn_mode_to_memory_semantics(mode));
2509 }
2510
2511 static void
2512 vtn_emit_make_available_barrier(struct vtn_builder *b, SpvMemoryAccessMask access,
2513 SpvScope scope, enum vtn_variable_mode mode)
2514 {
2515 if (!(access & SpvMemoryAccessMakePointerAvailableMask))
2516 return;
2517
2518 vtn_emit_memory_barrier(b, scope, SpvMemorySemanticsMakeAvailableMask |
2519 SpvMemorySemanticsReleaseMask |
2520 vtn_mode_to_memory_semantics(mode));
2521 }
2522
2523 void
2524 vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
2525 const uint32_t *w, unsigned count)
2526 {
2527 switch (opcode) {
2528 case SpvOpUndef: {
2529 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_undef);
2530 val->type = vtn_get_type(b, w[1]);
2531 break;
2532 }
2533
2534 case SpvOpVariable: {
2535 struct vtn_type *ptr_type = vtn_get_type(b, w[1]);
2536
2537 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_pointer);
2538
2539 SpvStorageClass storage_class = w[3];
2540 nir_constant *const_initializer = NULL;
2541 nir_variable *var_initializer = NULL;
2542 if (count > 4) {
2543 struct vtn_value *init = vtn_untyped_value(b, w[4]);
2544 switch (init->value_type) {
2545 case vtn_value_type_constant:
2546 const_initializer = init->constant;
2547 break;
2548 case vtn_value_type_pointer:
2549 var_initializer = init->pointer->var->var;
2550 break;
2551 default:
2552 vtn_fail("SPIR-V variable initializer %u must be constant or pointer",
2553 w[4]);
2554 }
2555 }
2556
2557 vtn_create_variable(b, val, ptr_type, storage_class, const_initializer, var_initializer);
2558
2559 break;
2560 }
2561
2562 case SpvOpConstantSampler: {
2563 /* Synthesize a pointer-to-sampler type, create a variable of that type,
2564 * and give the variable a constant initializer with the sampler params */
2565 struct vtn_type *sampler_type = vtn_value(b, w[1], vtn_value_type_type)->type;
2566 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_pointer);
2567
2568 struct vtn_type *ptr_type = rzalloc(b, struct vtn_type);
2569 ptr_type = rzalloc(b, struct vtn_type);
2570 ptr_type->base_type = vtn_base_type_pointer;
2571 ptr_type->deref = sampler_type;
2572 ptr_type->storage_class = SpvStorageClassUniform;
2573
2574 ptr_type->type = nir_address_format_to_glsl_type(
2575 vtn_mode_to_address_format(b, vtn_variable_mode_function));
2576
2577 vtn_create_variable(b, val, ptr_type, ptr_type->storage_class, NULL, NULL);
2578
2579 nir_variable *nir_var = val->pointer->var->var;
2580 nir_var->data.sampler.is_inline_sampler = true;
2581 nir_var->data.sampler.addressing_mode = w[3];
2582 nir_var->data.sampler.normalized_coordinates = w[4];
2583 nir_var->data.sampler.filter_mode = w[5];
2584
2585 break;
2586 }
2587
2588 case SpvOpAccessChain:
2589 case SpvOpPtrAccessChain:
2590 case SpvOpInBoundsAccessChain:
2591 case SpvOpInBoundsPtrAccessChain: {
2592 struct vtn_access_chain *chain = vtn_access_chain_create(b, count - 4);
2593 enum gl_access_qualifier access = 0;
2594 chain->ptr_as_array = (opcode == SpvOpPtrAccessChain || opcode == SpvOpInBoundsPtrAccessChain);
2595
2596 unsigned idx = 0;
2597 for (int i = 4; i < count; i++) {
2598 struct vtn_value *link_val = vtn_untyped_value(b, w[i]);
2599 if (link_val->value_type == vtn_value_type_constant) {
2600 chain->link[idx].mode = vtn_access_mode_literal;
2601 chain->link[idx].id = vtn_constant_int(b, w[i]);
2602 } else {
2603 chain->link[idx].mode = vtn_access_mode_id;
2604 chain->link[idx].id = w[i];
2605 }
2606 idx++;
2607 }
2608
2609 struct vtn_type *ptr_type = vtn_get_type(b, w[1]);
2610 struct vtn_pointer *base =
2611 vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
2612 struct vtn_pointer *ptr = vtn_pointer_dereference(b, base, chain);
2613 ptr->ptr_type = ptr_type;
2614 ptr->access |= access;
2615 vtn_push_pointer(b, w[2], ptr);
2616 break;
2617 }
2618
2619 case SpvOpCopyMemory: {
2620 struct vtn_value *dest = vtn_value(b, w[1], vtn_value_type_pointer);
2621 struct vtn_value *src = vtn_value(b, w[2], vtn_value_type_pointer);
2622
2623 vtn_assert_types_equal(b, opcode, dest->type->deref, src->type->deref);
2624
2625 unsigned idx = 3, dest_alignment, src_alignment;
2626 SpvMemoryAccessMask dest_access, src_access;
2627 SpvScope dest_scope, src_scope;
2628 vtn_get_mem_operands(b, w, count, &idx, &dest_access, &dest_alignment,
2629 &dest_scope, &src_scope);
2630 if (!vtn_get_mem_operands(b, w, count, &idx, &src_access, &src_alignment,
2631 NULL, &src_scope)) {
2632 src_alignment = dest_alignment;
2633 src_access = dest_access;
2634 }
2635
2636 vtn_emit_make_visible_barrier(b, src_access, src_scope, src->pointer->mode);
2637
2638 vtn_variable_copy(b, dest->pointer, src->pointer,
2639 spv_access_to_gl_access(dest_access),
2640 spv_access_to_gl_access(src_access));
2641
2642 vtn_emit_make_available_barrier(b, dest_access, dest_scope, dest->pointer->mode);
2643 break;
2644 }
2645
2646 case SpvOpLoad: {
2647 struct vtn_type *res_type = vtn_get_type(b, w[1]);
2648 struct vtn_value *src_val = vtn_value(b, w[3], vtn_value_type_pointer);
2649 struct vtn_pointer *src = src_val->pointer;
2650
2651 vtn_assert_types_equal(b, opcode, res_type, src_val->type->deref);
2652
2653 unsigned idx = 4, alignment;
2654 SpvMemoryAccessMask access;
2655 SpvScope scope;
2656 vtn_get_mem_operands(b, w, count, &idx, &access, &alignment, NULL, &scope);
2657
2658 vtn_emit_make_visible_barrier(b, access, scope, src->mode);
2659
2660 vtn_push_ssa_value(b, w[2], vtn_variable_load(b, src, spv_access_to_gl_access(access)));
2661 break;
2662 }
2663
2664 case SpvOpStore: {
2665 struct vtn_value *dest_val = vtn_value(b, w[1], vtn_value_type_pointer);
2666 struct vtn_pointer *dest = dest_val->pointer;
2667 struct vtn_value *src_val = vtn_untyped_value(b, w[2]);
2668
2669 /* OpStore requires us to actually have a storage type */
2670 vtn_fail_if(dest->type->type == NULL,
2671 "Invalid destination type for OpStore");
2672
2673 if (glsl_get_base_type(dest->type->type) == GLSL_TYPE_BOOL &&
2674 glsl_get_base_type(src_val->type->type) == GLSL_TYPE_UINT) {
2675 /* Early versions of GLSLang would use uint types for UBOs/SSBOs but
2676 * would then store them to a local variable as bool. Work around
2677 * the issue by doing an implicit conversion.
2678 *
2679 * https://github.com/KhronosGroup/glslang/issues/170
2680 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2681 */
2682 vtn_warn("OpStore of value of type OpTypeInt to a pointer to type "
2683 "OpTypeBool. Doing an implicit conversion to work around "
2684 "the problem.");
2685 struct vtn_ssa_value *bool_ssa =
2686 vtn_create_ssa_value(b, dest->type->type);
2687 bool_ssa->def = nir_i2b(&b->nb, vtn_ssa_value(b, w[2])->def);
2688 vtn_variable_store(b, bool_ssa, dest, 0);
2689 break;
2690 }
2691
2692 vtn_assert_types_equal(b, opcode, dest_val->type->deref, src_val->type);
2693
2694 unsigned idx = 3, alignment;
2695 SpvMemoryAccessMask access;
2696 SpvScope scope;
2697 vtn_get_mem_operands(b, w, count, &idx, &access, &alignment, &scope, NULL);
2698
2699 struct vtn_ssa_value *src = vtn_ssa_value(b, w[2]);
2700 vtn_variable_store(b, src, dest, spv_access_to_gl_access(access));
2701
2702 vtn_emit_make_available_barrier(b, access, scope, dest->mode);
2703 break;
2704 }
2705
2706 case SpvOpArrayLength: {
2707 struct vtn_pointer *ptr =
2708 vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
2709 const uint32_t field = w[4];
2710
2711 vtn_fail_if(ptr->type->base_type != vtn_base_type_struct,
2712 "OpArrayLength must take a pointer to a structure type");
2713 vtn_fail_if(field != ptr->type->length - 1 ||
2714 ptr->type->members[field]->base_type != vtn_base_type_array,
2715 "OpArrayLength must reference the last memeber of the "
2716 "structure and that must be an array");
2717
2718 const uint32_t offset = ptr->type->offsets[field];
2719 const uint32_t stride = ptr->type->members[field]->stride;
2720
2721 if (!ptr->block_index) {
2722 struct vtn_access_chain chain = {
2723 .length = 0,
2724 };
2725 ptr = vtn_pointer_dereference(b, ptr, &chain);
2726 vtn_assert(ptr->block_index);
2727 }
2728
2729 nir_intrinsic_instr *instr =
2730 nir_intrinsic_instr_create(b->nb.shader,
2731 nir_intrinsic_get_buffer_size);
2732 instr->src[0] = nir_src_for_ssa(ptr->block_index);
2733 nir_ssa_dest_init(&instr->instr, &instr->dest, 1, 32, NULL);
2734 nir_builder_instr_insert(&b->nb, &instr->instr);
2735 nir_ssa_def *buf_size = &instr->dest.ssa;
2736
2737 /* array_length = max(buffer_size - offset, 0) / stride */
2738 nir_ssa_def *array_length =
2739 nir_idiv(&b->nb,
2740 nir_imax(&b->nb,
2741 nir_isub(&b->nb,
2742 buf_size,
2743 nir_imm_int(&b->nb, offset)),
2744 nir_imm_int(&b->nb, 0u)),
2745 nir_imm_int(&b->nb, stride));
2746
2747 vtn_push_nir_ssa(b, w[2], array_length);
2748 break;
2749 }
2750
2751 case SpvOpConvertPtrToU: {
2752 struct vtn_type *u_type = vtn_get_type(b, w[1]);
2753 struct vtn_type *ptr_type = vtn_get_value_type(b, w[3]);
2754
2755 vtn_fail_if(ptr_type->base_type != vtn_base_type_pointer ||
2756 ptr_type->type == NULL,
2757 "OpConvertPtrToU can only be used on physical pointers");
2758
2759 vtn_fail_if(u_type->base_type != vtn_base_type_vector &&
2760 u_type->base_type != vtn_base_type_scalar,
2761 "OpConvertPtrToU can only be used to cast to a vector or "
2762 "scalar type");
2763
2764 /* The pointer will be converted to an SSA value automatically */
2765 nir_ssa_def *ptr = vtn_get_nir_ssa(b, w[3]);
2766 nir_ssa_def *u = nir_sloppy_bitcast(&b->nb, ptr, u_type->type);
2767 vtn_push_nir_ssa(b, w[2], u);
2768 break;
2769 }
2770
2771 case SpvOpConvertUToPtr: {
2772 struct vtn_type *ptr_type = vtn_get_type(b, w[1]);
2773 struct vtn_type *u_type = vtn_get_value_type(b, w[3]);
2774
2775 vtn_fail_if(ptr_type->base_type != vtn_base_type_pointer ||
2776 ptr_type->type == NULL,
2777 "OpConvertUToPtr can only be used on physical pointers");
2778
2779 vtn_fail_if(u_type->base_type != vtn_base_type_vector &&
2780 u_type->base_type != vtn_base_type_scalar,
2781 "OpConvertUToPtr can only be used to cast from a vector or "
2782 "scalar type");
2783
2784 nir_ssa_def *u = vtn_get_nir_ssa(b, w[3]);
2785 nir_ssa_def *ptr = nir_sloppy_bitcast(&b->nb, u, ptr_type->type);
2786 vtn_push_pointer(b, w[2], vtn_pointer_from_ssa(b, ptr, ptr_type));
2787 break;
2788 }
2789
2790 case SpvOpCopyMemorySized:
2791 default:
2792 vtn_fail_with_opcode("Unhandled opcode", opcode);
2793 }
2794 }