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