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