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