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