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