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