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