spirv/nir: add offset at vtn_variable
[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 case SpvDecorationOffset:
1350 vtn_var->offset = dec->literals[0];
1351 break;
1352 default:
1353 break;
1354 }
1355
1356 if (val->value_type == vtn_value_type_pointer) {
1357 assert(val->pointer->var == void_var);
1358 assert(val->pointer->chain == NULL);
1359 assert(member == -1);
1360 } else {
1361 assert(val->value_type == vtn_value_type_type);
1362 }
1363
1364 /* Location is odd. If applied to a split structure, we have to walk the
1365 * whole thing and accumulate the location. It's easier to handle as a
1366 * special case.
1367 */
1368 if (dec->decoration == SpvDecorationLocation) {
1369 unsigned location = dec->literals[0];
1370 bool is_vertex_input;
1371 if (b->shader->info.stage == MESA_SHADER_FRAGMENT &&
1372 vtn_var->mode == vtn_variable_mode_output) {
1373 is_vertex_input = false;
1374 location += FRAG_RESULT_DATA0;
1375 } else if (b->shader->info.stage == MESA_SHADER_VERTEX &&
1376 vtn_var->mode == vtn_variable_mode_input) {
1377 is_vertex_input = true;
1378 location += VERT_ATTRIB_GENERIC0;
1379 } else if (vtn_var->mode == vtn_variable_mode_input ||
1380 vtn_var->mode == vtn_variable_mode_output) {
1381 is_vertex_input = false;
1382 location += vtn_var->patch ? VARYING_SLOT_PATCH0 : VARYING_SLOT_VAR0;
1383 } else if (vtn_var->mode != vtn_variable_mode_uniform) {
1384 vtn_warn("Location must be on input, output, uniform, sampler or "
1385 "image variable");
1386 return;
1387 }
1388
1389 if (vtn_var->var->num_members == 0) {
1390 /* This handles the member and lone variable cases */
1391 vtn_var->var->data.location = location;
1392 } else {
1393 /* This handles the structure member case */
1394 assert(vtn_var->var->members);
1395 for (unsigned i = 0; i < vtn_var->var->num_members; i++) {
1396 vtn_var->var->members[i].location = location;
1397 const struct glsl_type *member_type =
1398 glsl_get_struct_field(vtn_var->var->interface_type, i);
1399 location += glsl_count_attribute_slots(member_type,
1400 is_vertex_input);
1401 }
1402 }
1403 return;
1404 } else {
1405 if (vtn_var->var) {
1406 if (vtn_var->var->num_members == 0) {
1407 assert(member == -1);
1408 apply_var_decoration(b, &vtn_var->var->data, dec);
1409 } else if (member >= 0) {
1410 /* Member decorations must come from a type */
1411 assert(val->value_type == vtn_value_type_type);
1412 apply_var_decoration(b, &vtn_var->var->members[member], dec);
1413 } else {
1414 unsigned length =
1415 glsl_get_length(glsl_without_array(vtn_var->type->type));
1416 for (unsigned i = 0; i < length; i++)
1417 apply_var_decoration(b, &vtn_var->var->members[i], dec);
1418 }
1419 } else {
1420 /* A few variables, those with external storage, have no actual
1421 * nir_variables associated with them. Fortunately, all decorations
1422 * we care about for those variables are on the type only.
1423 */
1424 vtn_assert(vtn_var->mode == vtn_variable_mode_ubo ||
1425 vtn_var->mode == vtn_variable_mode_ssbo ||
1426 vtn_var->mode == vtn_variable_mode_push_constant ||
1427 (vtn_var->mode == vtn_variable_mode_workgroup &&
1428 b->options->lower_workgroup_access_to_offsets));
1429 }
1430 }
1431 }
1432
1433 static enum vtn_variable_mode
1434 vtn_storage_class_to_mode(struct vtn_builder *b,
1435 SpvStorageClass class,
1436 struct vtn_type *interface_type,
1437 nir_variable_mode *nir_mode_out)
1438 {
1439 enum vtn_variable_mode mode;
1440 nir_variable_mode nir_mode;
1441 switch (class) {
1442 case SpvStorageClassUniform:
1443 if (interface_type->block) {
1444 mode = vtn_variable_mode_ubo;
1445 nir_mode = 0;
1446 } else if (interface_type->buffer_block) {
1447 mode = vtn_variable_mode_ssbo;
1448 nir_mode = 0;
1449 } else {
1450 /* Default-block uniforms, coming from gl_spirv */
1451 mode = vtn_variable_mode_uniform;
1452 nir_mode = nir_var_uniform;
1453 }
1454 break;
1455 case SpvStorageClassStorageBuffer:
1456 mode = vtn_variable_mode_ssbo;
1457 nir_mode = 0;
1458 break;
1459 case SpvStorageClassUniformConstant:
1460 mode = vtn_variable_mode_uniform;
1461 nir_mode = nir_var_uniform;
1462 break;
1463 case SpvStorageClassPushConstant:
1464 mode = vtn_variable_mode_push_constant;
1465 nir_mode = nir_var_uniform;
1466 break;
1467 case SpvStorageClassInput:
1468 mode = vtn_variable_mode_input;
1469 nir_mode = nir_var_shader_in;
1470 break;
1471 case SpvStorageClassOutput:
1472 mode = vtn_variable_mode_output;
1473 nir_mode = nir_var_shader_out;
1474 break;
1475 case SpvStorageClassPrivate:
1476 mode = vtn_variable_mode_global;
1477 nir_mode = nir_var_global;
1478 break;
1479 case SpvStorageClassFunction:
1480 mode = vtn_variable_mode_local;
1481 nir_mode = nir_var_local;
1482 break;
1483 case SpvStorageClassWorkgroup:
1484 mode = vtn_variable_mode_workgroup;
1485 nir_mode = nir_var_shared;
1486 break;
1487 case SpvStorageClassAtomicCounter:
1488 mode = vtn_variable_mode_uniform;
1489 nir_mode = nir_var_uniform;
1490 break;
1491 case SpvStorageClassCrossWorkgroup:
1492 case SpvStorageClassGeneric:
1493 default:
1494 vtn_fail("Unhandled variable storage class");
1495 }
1496
1497 if (nir_mode_out)
1498 *nir_mode_out = nir_mode;
1499
1500 return mode;
1501 }
1502
1503 nir_ssa_def *
1504 vtn_pointer_to_ssa(struct vtn_builder *b, struct vtn_pointer *ptr)
1505 {
1506 if (vtn_pointer_uses_ssa_offset(b, ptr)) {
1507 /* This pointer needs to have a pointer type with actual storage */
1508 vtn_assert(ptr->ptr_type);
1509 vtn_assert(ptr->ptr_type->type);
1510
1511 if (!ptr->offset) {
1512 /* If we don't have an offset then we must be a pointer to the variable
1513 * itself.
1514 */
1515 vtn_assert(!ptr->offset && !ptr->block_index);
1516
1517 struct vtn_access_chain chain = {
1518 .length = 0,
1519 };
1520 ptr = vtn_ssa_offset_pointer_dereference(b, ptr, &chain);
1521 }
1522
1523 vtn_assert(ptr->offset);
1524 if (ptr->block_index) {
1525 vtn_assert(ptr->mode == vtn_variable_mode_ubo ||
1526 ptr->mode == vtn_variable_mode_ssbo);
1527 return nir_vec2(&b->nb, ptr->block_index, ptr->offset);
1528 } else {
1529 vtn_assert(ptr->mode == vtn_variable_mode_workgroup);
1530 return ptr->offset;
1531 }
1532 } else {
1533 return &vtn_pointer_to_deref(b, ptr)->dest.ssa;
1534 }
1535 }
1536
1537 struct vtn_pointer *
1538 vtn_pointer_from_ssa(struct vtn_builder *b, nir_ssa_def *ssa,
1539 struct vtn_type *ptr_type)
1540 {
1541 vtn_assert(ssa->num_components <= 2 && ssa->bit_size == 32);
1542 vtn_assert(ptr_type->base_type == vtn_base_type_pointer);
1543
1544 struct vtn_type *interface_type = ptr_type->deref;
1545 while (interface_type->base_type == vtn_base_type_array)
1546 interface_type = interface_type->array_element;
1547
1548 struct vtn_pointer *ptr = rzalloc(b, struct vtn_pointer);
1549 nir_variable_mode nir_mode;
1550 ptr->mode = vtn_storage_class_to_mode(b, ptr_type->storage_class,
1551 interface_type, &nir_mode);
1552 ptr->type = ptr_type->deref;
1553 ptr->ptr_type = ptr_type;
1554
1555 if (ptr->mode == vtn_variable_mode_ubo ||
1556 ptr->mode == vtn_variable_mode_ssbo) {
1557 /* This pointer type needs to have actual storage */
1558 vtn_assert(ptr_type->type);
1559 vtn_assert(ssa->num_components == 2);
1560 ptr->block_index = nir_channel(&b->nb, ssa, 0);
1561 ptr->offset = nir_channel(&b->nb, ssa, 1);
1562 } else if (ptr->mode == vtn_variable_mode_workgroup ||
1563 ptr->mode == vtn_variable_mode_push_constant) {
1564 /* This pointer type needs to have actual storage */
1565 vtn_assert(ptr_type->type);
1566 vtn_assert(ssa->num_components == 1);
1567 ptr->block_index = NULL;
1568 ptr->offset = ssa;
1569 } else {
1570 ptr->deref = nir_build_deref_cast(&b->nb, ssa, nir_mode,
1571 ptr_type->deref->type);
1572 }
1573
1574 return ptr;
1575 }
1576
1577 static bool
1578 is_per_vertex_inout(const struct vtn_variable *var, gl_shader_stage stage)
1579 {
1580 if (var->patch || !glsl_type_is_array(var->type->type))
1581 return false;
1582
1583 if (var->mode == vtn_variable_mode_input) {
1584 return stage == MESA_SHADER_TESS_CTRL ||
1585 stage == MESA_SHADER_TESS_EVAL ||
1586 stage == MESA_SHADER_GEOMETRY;
1587 }
1588
1589 if (var->mode == vtn_variable_mode_output)
1590 return stage == MESA_SHADER_TESS_CTRL;
1591
1592 return false;
1593 }
1594
1595 static void
1596 vtn_create_variable(struct vtn_builder *b, struct vtn_value *val,
1597 struct vtn_type *ptr_type, SpvStorageClass storage_class,
1598 nir_constant *initializer)
1599 {
1600 vtn_assert(ptr_type->base_type == vtn_base_type_pointer);
1601 struct vtn_type *type = ptr_type->deref;
1602
1603 struct vtn_type *without_array = type;
1604 while(glsl_type_is_array(without_array->type))
1605 without_array = without_array->array_element;
1606
1607 enum vtn_variable_mode mode;
1608 nir_variable_mode nir_mode;
1609 mode = vtn_storage_class_to_mode(b, storage_class, without_array, &nir_mode);
1610
1611 switch (mode) {
1612 case vtn_variable_mode_ubo:
1613 b->shader->info.num_ubos++;
1614 break;
1615 case vtn_variable_mode_ssbo:
1616 b->shader->info.num_ssbos++;
1617 break;
1618 case vtn_variable_mode_uniform:
1619 if (glsl_type_is_image(without_array->type))
1620 b->shader->info.num_images++;
1621 else if (glsl_type_is_sampler(without_array->type))
1622 b->shader->info.num_textures++;
1623 break;
1624 case vtn_variable_mode_push_constant:
1625 b->shader->num_uniforms = vtn_type_block_size(b, type);
1626 break;
1627 default:
1628 /* No tallying is needed */
1629 break;
1630 }
1631
1632 struct vtn_variable *var = rzalloc(b, struct vtn_variable);
1633 var->type = type;
1634 var->mode = mode;
1635
1636 vtn_assert(val->value_type == vtn_value_type_pointer);
1637 val->pointer = vtn_pointer_for_variable(b, var, ptr_type);
1638
1639 switch (var->mode) {
1640 case vtn_variable_mode_local:
1641 case vtn_variable_mode_global:
1642 case vtn_variable_mode_uniform:
1643 /* For these, we create the variable normally */
1644 var->var = rzalloc(b->shader, nir_variable);
1645 var->var->name = ralloc_strdup(var->var, val->name);
1646 var->var->type = var->type->type;
1647 var->var->data.mode = nir_mode;
1648 var->var->data.location = -1;
1649 var->var->interface_type = NULL;
1650 break;
1651
1652 case vtn_variable_mode_workgroup:
1653 if (b->options->lower_workgroup_access_to_offsets) {
1654 var->shared_location = -1;
1655 } else {
1656 /* Create the variable normally */
1657 var->var = rzalloc(b->shader, nir_variable);
1658 var->var->name = ralloc_strdup(var->var, val->name);
1659 var->var->type = var->type->type;
1660 var->var->data.mode = nir_var_shared;
1661 }
1662 break;
1663
1664 case vtn_variable_mode_input:
1665 case vtn_variable_mode_output: {
1666 /* In order to know whether or not we're a per-vertex inout, we need
1667 * the patch qualifier. This means walking the variable decorations
1668 * early before we actually create any variables. Not a big deal.
1669 *
1670 * GLSLang really likes to place decorations in the most interior
1671 * thing it possibly can. In particular, if you have a struct, it
1672 * will place the patch decorations on the struct members. This
1673 * should be handled by the variable splitting below just fine.
1674 *
1675 * If you have an array-of-struct, things get even more weird as it
1676 * will place the patch decorations on the struct even though it's
1677 * inside an array and some of the members being patch and others not
1678 * makes no sense whatsoever. Since the only sensible thing is for
1679 * it to be all or nothing, we'll call it patch if any of the members
1680 * are declared patch.
1681 */
1682 var->patch = false;
1683 vtn_foreach_decoration(b, val, var_is_patch_cb, &var->patch);
1684 if (glsl_type_is_array(var->type->type) &&
1685 glsl_type_is_struct(without_array->type)) {
1686 vtn_foreach_decoration(b, vtn_value(b, without_array->id,
1687 vtn_value_type_type),
1688 var_is_patch_cb, &var->patch);
1689 }
1690
1691 /* For inputs and outputs, we immediately split structures. This
1692 * is for a couple of reasons. For one, builtins may all come in
1693 * a struct and we really want those split out into separate
1694 * variables. For another, interpolation qualifiers can be
1695 * applied to members of the top-level struct ane we need to be
1696 * able to preserve that information.
1697 */
1698
1699 struct vtn_type *interface_type = var->type;
1700 if (is_per_vertex_inout(var, b->shader->info.stage)) {
1701 /* In Geometry shaders (and some tessellation), inputs come
1702 * in per-vertex arrays. However, some builtins come in
1703 * non-per-vertex, hence the need for the is_array check. In
1704 * any case, there are no non-builtin arrays allowed so this
1705 * check should be sufficient.
1706 */
1707 interface_type = var->type->array_element;
1708 }
1709
1710 var->var = rzalloc(b->shader, nir_variable);
1711 var->var->name = ralloc_strdup(var->var, val->name);
1712 var->var->type = var->type->type;
1713 var->var->interface_type = interface_type->type;
1714 var->var->data.mode = nir_mode;
1715 var->var->data.patch = var->patch;
1716
1717 if (glsl_type_is_struct(interface_type->type)) {
1718 /* It's a struct. Set it up as per-member. */
1719 var->var->num_members = glsl_get_length(interface_type->type);
1720 var->var->members = rzalloc_array(var->var, struct nir_variable_data,
1721 var->var->num_members);
1722
1723 for (unsigned i = 0; i < var->var->num_members; i++) {
1724 var->var->members[i].mode = nir_mode;
1725 var->var->members[i].patch = var->patch;
1726 }
1727 }
1728
1729 /* For inputs and outputs, we need to grab locations and builtin
1730 * information from the interface type.
1731 */
1732 vtn_foreach_decoration(b, vtn_value(b, interface_type->id,
1733 vtn_value_type_type),
1734 var_decoration_cb, var);
1735 break;
1736 }
1737
1738 case vtn_variable_mode_ubo:
1739 case vtn_variable_mode_ssbo:
1740 case vtn_variable_mode_push_constant:
1741 /* These don't need actual variables. */
1742 break;
1743 }
1744
1745 if (initializer) {
1746 var->var->constant_initializer =
1747 nir_constant_clone(initializer, var->var);
1748 }
1749
1750 vtn_foreach_decoration(b, val, var_decoration_cb, var);
1751
1752 if (var->mode == vtn_variable_mode_uniform) {
1753 /* XXX: We still need the binding information in the nir_variable
1754 * for these. We should fix that.
1755 */
1756 var->var->data.binding = var->binding;
1757 var->var->data.explicit_binding = var->explicit_binding;
1758 var->var->data.descriptor_set = var->descriptor_set;
1759 var->var->data.index = var->input_attachment_index;
1760
1761 if (glsl_type_is_image(without_array->type))
1762 var->var->data.image.format = without_array->image_format;
1763 }
1764
1765 if (var->mode == vtn_variable_mode_local) {
1766 vtn_assert(var->var != NULL && var->var->members == NULL);
1767 nir_function_impl_add_variable(b->nb.impl, var->var);
1768 } else if (var->var) {
1769 nir_shader_add_variable(b->shader, var->var);
1770 } else {
1771 vtn_assert(vtn_pointer_is_external_block(b, val->pointer));
1772 }
1773 }
1774
1775 static void
1776 vtn_assert_types_equal(struct vtn_builder *b, SpvOp opcode,
1777 struct vtn_type *dst_type,
1778 struct vtn_type *src_type)
1779 {
1780 if (dst_type->id == src_type->id)
1781 return;
1782
1783 if (vtn_types_compatible(b, dst_type, src_type)) {
1784 /* Early versions of GLSLang would re-emit types unnecessarily and you
1785 * would end up with OpLoad, OpStore, or OpCopyMemory opcodes which have
1786 * mismatched source and destination types.
1787 *
1788 * https://github.com/KhronosGroup/glslang/issues/304
1789 * https://github.com/KhronosGroup/glslang/issues/307
1790 * https://bugs.freedesktop.org/show_bug.cgi?id=104338
1791 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
1792 */
1793 vtn_warn("Source and destination types of %s do not have the same "
1794 "ID (but are compatible): %u vs %u",
1795 spirv_op_to_string(opcode), dst_type->id, src_type->id);
1796 return;
1797 }
1798
1799 vtn_fail("Source and destination types of %s do not match: %s vs. %s",
1800 spirv_op_to_string(opcode),
1801 glsl_get_type_name(dst_type->type),
1802 glsl_get_type_name(src_type->type));
1803 }
1804
1805 void
1806 vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
1807 const uint32_t *w, unsigned count)
1808 {
1809 switch (opcode) {
1810 case SpvOpUndef: {
1811 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_undef);
1812 val->type = vtn_value(b, w[1], vtn_value_type_type)->type;
1813 break;
1814 }
1815
1816 case SpvOpVariable: {
1817 struct vtn_type *ptr_type = vtn_value(b, w[1], vtn_value_type_type)->type;
1818
1819 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_pointer);
1820
1821 SpvStorageClass storage_class = w[3];
1822 nir_constant *initializer = NULL;
1823 if (count > 4)
1824 initializer = vtn_value(b, w[4], vtn_value_type_constant)->constant;
1825
1826 vtn_create_variable(b, val, ptr_type, storage_class, initializer);
1827 break;
1828 }
1829
1830 case SpvOpAccessChain:
1831 case SpvOpPtrAccessChain:
1832 case SpvOpInBoundsAccessChain: {
1833 struct vtn_access_chain *chain = vtn_access_chain_create(b, count - 4);
1834 chain->ptr_as_array = (opcode == SpvOpPtrAccessChain);
1835
1836 unsigned idx = 0;
1837 for (int i = 4; i < count; i++) {
1838 struct vtn_value *link_val = vtn_untyped_value(b, w[i]);
1839 if (link_val->value_type == vtn_value_type_constant) {
1840 chain->link[idx].mode = vtn_access_mode_literal;
1841 chain->link[idx].id = link_val->constant->values[0].u32[0];
1842 } else {
1843 chain->link[idx].mode = vtn_access_mode_id;
1844 chain->link[idx].id = w[i];
1845
1846 }
1847 idx++;
1848 }
1849
1850 struct vtn_type *ptr_type = vtn_value(b, w[1], vtn_value_type_type)->type;
1851 struct vtn_value *base_val = vtn_untyped_value(b, w[3]);
1852 if (base_val->value_type == vtn_value_type_sampled_image) {
1853 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1854 * to combine an array of images with a single sampler to get an
1855 * array of sampled images that all share the same sampler.
1856 * Fortunately, this means that we can more-or-less ignore the
1857 * sampler when crawling the access chain, but it does leave us
1858 * with this rather awkward little special-case.
1859 */
1860 struct vtn_value *val =
1861 vtn_push_value(b, w[2], vtn_value_type_sampled_image);
1862 val->sampled_image = ralloc(b, struct vtn_sampled_image);
1863 val->sampled_image->type = base_val->sampled_image->type;
1864 val->sampled_image->image =
1865 vtn_pointer_dereference(b, base_val->sampled_image->image, chain);
1866 val->sampled_image->sampler = base_val->sampled_image->sampler;
1867 } else {
1868 vtn_assert(base_val->value_type == vtn_value_type_pointer);
1869 struct vtn_value *val =
1870 vtn_push_value(b, w[2], vtn_value_type_pointer);
1871 val->pointer = vtn_pointer_dereference(b, base_val->pointer, chain);
1872 val->pointer->ptr_type = ptr_type;
1873 }
1874 break;
1875 }
1876
1877 case SpvOpCopyMemory: {
1878 struct vtn_value *dest = vtn_value(b, w[1], vtn_value_type_pointer);
1879 struct vtn_value *src = vtn_value(b, w[2], vtn_value_type_pointer);
1880
1881 vtn_assert_types_equal(b, opcode, dest->type->deref, src->type->deref);
1882
1883 vtn_variable_copy(b, dest->pointer, src->pointer);
1884 break;
1885 }
1886
1887 case SpvOpLoad: {
1888 struct vtn_type *res_type =
1889 vtn_value(b, w[1], vtn_value_type_type)->type;
1890 struct vtn_value *src_val = vtn_value(b, w[3], vtn_value_type_pointer);
1891 struct vtn_pointer *src = src_val->pointer;
1892
1893 vtn_assert_types_equal(b, opcode, res_type, src_val->type->deref);
1894
1895 if (glsl_type_is_image(res_type->type) ||
1896 glsl_type_is_sampler(res_type->type)) {
1897 vtn_push_value(b, w[2], vtn_value_type_pointer)->pointer = src;
1898 return;
1899 }
1900
1901 vtn_push_ssa(b, w[2], res_type, vtn_variable_load(b, src));
1902 break;
1903 }
1904
1905 case SpvOpStore: {
1906 struct vtn_value *dest_val = vtn_value(b, w[1], vtn_value_type_pointer);
1907 struct vtn_pointer *dest = dest_val->pointer;
1908 struct vtn_value *src_val = vtn_untyped_value(b, w[2]);
1909
1910 /* OpStore requires us to actually have a storage type */
1911 vtn_fail_if(dest->type->type == NULL,
1912 "Invalid destination type for OpStore");
1913
1914 if (glsl_get_base_type(dest->type->type) == GLSL_TYPE_BOOL &&
1915 glsl_get_base_type(src_val->type->type) == GLSL_TYPE_UINT) {
1916 /* Early versions of GLSLang would use uint types for UBOs/SSBOs but
1917 * would then store them to a local variable as bool. Work around
1918 * the issue by doing an implicit conversion.
1919 *
1920 * https://github.com/KhronosGroup/glslang/issues/170
1921 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
1922 */
1923 vtn_warn("OpStore of value of type OpTypeInt to a pointer to type "
1924 "OpTypeBool. Doing an implicit conversion to work around "
1925 "the problem.");
1926 struct vtn_ssa_value *bool_ssa =
1927 vtn_create_ssa_value(b, dest->type->type);
1928 bool_ssa->def = nir_i2b(&b->nb, vtn_ssa_value(b, w[2])->def);
1929 vtn_variable_store(b, bool_ssa, dest);
1930 break;
1931 }
1932
1933 vtn_assert_types_equal(b, opcode, dest_val->type->deref, src_val->type);
1934
1935 if (glsl_type_is_sampler(dest->type->type)) {
1936 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
1937 "propagation to workaround the problem.");
1938 vtn_assert(dest->var->copy_prop_sampler == NULL);
1939 dest->var->copy_prop_sampler =
1940 vtn_value(b, w[2], vtn_value_type_pointer)->pointer;
1941 break;
1942 }
1943
1944 struct vtn_ssa_value *src = vtn_ssa_value(b, w[2]);
1945 vtn_variable_store(b, src, dest);
1946 break;
1947 }
1948
1949 case SpvOpArrayLength: {
1950 struct vtn_pointer *ptr =
1951 vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
1952
1953 const uint32_t offset = ptr->var->type->offsets[w[4]];
1954 const uint32_t stride = ptr->var->type->members[w[4]]->stride;
1955
1956 if (!ptr->block_index) {
1957 struct vtn_access_chain chain = {
1958 .length = 0,
1959 };
1960 ptr = vtn_ssa_offset_pointer_dereference(b, ptr, &chain);
1961 vtn_assert(ptr->block_index);
1962 }
1963
1964 nir_intrinsic_instr *instr =
1965 nir_intrinsic_instr_create(b->nb.shader,
1966 nir_intrinsic_get_buffer_size);
1967 instr->src[0] = nir_src_for_ssa(ptr->block_index);
1968 nir_ssa_dest_init(&instr->instr, &instr->dest, 1, 32, NULL);
1969 nir_builder_instr_insert(&b->nb, &instr->instr);
1970 nir_ssa_def *buf_size = &instr->dest.ssa;
1971
1972 /* array_length = max(buffer_size - offset, 0) / stride */
1973 nir_ssa_def *array_length =
1974 nir_idiv(&b->nb,
1975 nir_imax(&b->nb,
1976 nir_isub(&b->nb,
1977 buf_size,
1978 nir_imm_int(&b->nb, offset)),
1979 nir_imm_int(&b->nb, 0u)),
1980 nir_imm_int(&b->nb, stride));
1981
1982 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1983 val->ssa = vtn_create_ssa_value(b, glsl_uint_type());
1984 val->ssa->def = array_length;
1985 break;
1986 }
1987
1988 case SpvOpCopyMemorySized:
1989 default:
1990 vtn_fail("Unhandled opcode");
1991 }
1992 }