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