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