spirv: Add support for SPV_KHR_multiview
[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_extend(struct vtn_builder *b, struct vtn_access_chain *old,
33 unsigned new_ids)
34 {
35 struct vtn_access_chain *chain;
36
37 unsigned new_len = old->length + new_ids;
38 /* TODO: don't use rzalloc */
39 chain = rzalloc_size(b, sizeof(*chain) + new_len * sizeof(chain->link[0]));
40
41 chain->var = old->var;
42 chain->length = new_len;
43
44 for (unsigned i = 0; i < old->length; i++)
45 chain->link[i] = old->link[i];
46
47 return chain;
48 }
49
50 static nir_ssa_def *
51 vtn_access_link_as_ssa(struct vtn_builder *b, struct vtn_access_link link,
52 unsigned stride)
53 {
54 assert(stride > 0);
55 if (link.mode == vtn_access_mode_literal) {
56 return nir_imm_int(&b->nb, link.id * stride);
57 } else if (stride == 1) {
58 return vtn_ssa_value(b, link.id)->def;
59 } else {
60 return nir_imul(&b->nb, vtn_ssa_value(b, link.id)->def,
61 nir_imm_int(&b->nb, stride));
62 }
63 }
64
65 static struct vtn_type *
66 vtn_access_chain_tail_type(struct vtn_builder *b,
67 struct vtn_access_chain *chain)
68 {
69 struct vtn_type *type = chain->var->type;
70 for (unsigned i = 0; i < chain->length; i++) {
71 if (glsl_type_is_struct(type->type)) {
72 assert(chain->link[i].mode == vtn_access_mode_literal);
73 type = type->members[chain->link[i].id];
74 } else {
75 type = type->array_element;
76 }
77 }
78 return type;
79 }
80
81 /* Crawls a chain of array derefs and rewrites the types so that the
82 * lengths stay the same but the terminal type is the one given by
83 * tail_type. This is useful for split structures.
84 */
85 static void
86 rewrite_deref_types(nir_deref *deref, const struct glsl_type *type)
87 {
88 deref->type = type;
89 if (deref->child) {
90 assert(deref->child->deref_type == nir_deref_type_array);
91 assert(glsl_type_is_array(deref->type));
92 rewrite_deref_types(deref->child, glsl_get_array_element(type));
93 }
94 }
95
96 nir_deref_var *
97 vtn_access_chain_to_deref(struct vtn_builder *b, struct vtn_access_chain *chain)
98 {
99 nir_deref_var *deref_var;
100 if (chain->var->var) {
101 deref_var = nir_deref_var_create(b, chain->var->var);
102 } else {
103 assert(chain->var->members);
104 /* Create the deref_var manually. It will get filled out later. */
105 deref_var = rzalloc(b, nir_deref_var);
106 deref_var->deref.deref_type = nir_deref_type_var;
107 }
108
109 struct vtn_type *deref_type = chain->var->type;
110 nir_deref *tail = &deref_var->deref;
111 nir_variable **members = chain->var->members;
112
113 for (unsigned i = 0; i < chain->length; i++) {
114 enum glsl_base_type base_type = glsl_get_base_type(deref_type->type);
115 switch (base_type) {
116 case GLSL_TYPE_UINT:
117 case GLSL_TYPE_INT:
118 case GLSL_TYPE_UINT64:
119 case GLSL_TYPE_INT64:
120 case GLSL_TYPE_FLOAT:
121 case GLSL_TYPE_DOUBLE:
122 case GLSL_TYPE_BOOL:
123 case GLSL_TYPE_ARRAY: {
124 deref_type = deref_type->array_element;
125
126 nir_deref_array *deref_arr = nir_deref_array_create(b);
127 deref_arr->deref.type = deref_type->type;
128
129 if (chain->link[i].mode == vtn_access_mode_literal) {
130 deref_arr->deref_array_type = nir_deref_array_type_direct;
131 deref_arr->base_offset = chain->link[i].id;
132 } else {
133 assert(chain->link[i].mode == vtn_access_mode_id);
134 deref_arr->deref_array_type = nir_deref_array_type_indirect;
135 deref_arr->base_offset = 0;
136 deref_arr->indirect =
137 nir_src_for_ssa(vtn_ssa_value(b, chain->link[i].id)->def);
138 }
139 tail->child = &deref_arr->deref;
140 tail = tail->child;
141 break;
142 }
143
144 case GLSL_TYPE_STRUCT: {
145 assert(chain->link[i].mode == vtn_access_mode_literal);
146 unsigned idx = chain->link[i].id;
147 deref_type = deref_type->members[idx];
148 if (members) {
149 /* This is a pre-split structure. */
150 deref_var->var = members[idx];
151 rewrite_deref_types(&deref_var->deref, members[idx]->type);
152 assert(tail->type == deref_type->type);
153 members = NULL;
154 } else {
155 nir_deref_struct *deref_struct = nir_deref_struct_create(b, idx);
156 deref_struct->deref.type = deref_type->type;
157 tail->child = &deref_struct->deref;
158 tail = tail->child;
159 }
160 break;
161 }
162 default:
163 unreachable("Invalid type for deref");
164 }
165 }
166
167 assert(members == NULL);
168 return deref_var;
169 }
170
171 static void
172 _vtn_local_load_store(struct vtn_builder *b, bool load, nir_deref_var *deref,
173 nir_deref *tail, struct vtn_ssa_value *inout)
174 {
175 /* The deref tail may contain a deref to select a component of a vector (in
176 * other words, it might not be an actual tail) so we have to save it away
177 * here since we overwrite it later.
178 */
179 nir_deref *old_child = tail->child;
180
181 if (glsl_type_is_vector_or_scalar(tail->type)) {
182 /* Terminate the deref chain in case there is one more link to pick
183 * off a component of the vector.
184 */
185 tail->child = NULL;
186
187 nir_intrinsic_op op = load ? nir_intrinsic_load_var :
188 nir_intrinsic_store_var;
189
190 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->shader, op);
191 intrin->variables[0] = nir_deref_var_clone(deref, intrin);
192 intrin->num_components = glsl_get_vector_elements(tail->type);
193
194 if (load) {
195 nir_ssa_dest_init(&intrin->instr, &intrin->dest,
196 intrin->num_components,
197 glsl_get_bit_size(tail->type),
198 NULL);
199 inout->def = &intrin->dest.ssa;
200 } else {
201 nir_intrinsic_set_write_mask(intrin, (1 << intrin->num_components) - 1);
202 intrin->src[0] = nir_src_for_ssa(inout->def);
203 }
204
205 nir_builder_instr_insert(&b->nb, &intrin->instr);
206 } else if (glsl_get_base_type(tail->type) == GLSL_TYPE_ARRAY ||
207 glsl_type_is_matrix(tail->type)) {
208 unsigned elems = glsl_get_length(tail->type);
209 nir_deref_array *deref_arr = nir_deref_array_create(b);
210 deref_arr->deref_array_type = nir_deref_array_type_direct;
211 deref_arr->deref.type = glsl_get_array_element(tail->type);
212 tail->child = &deref_arr->deref;
213 for (unsigned i = 0; i < elems; i++) {
214 deref_arr->base_offset = i;
215 _vtn_local_load_store(b, load, deref, tail->child, inout->elems[i]);
216 }
217 } else {
218 assert(glsl_get_base_type(tail->type) == GLSL_TYPE_STRUCT);
219 unsigned elems = glsl_get_length(tail->type);
220 nir_deref_struct *deref_struct = nir_deref_struct_create(b, 0);
221 tail->child = &deref_struct->deref;
222 for (unsigned i = 0; i < elems; i++) {
223 deref_struct->index = i;
224 deref_struct->deref.type = glsl_get_struct_field(tail->type, i);
225 _vtn_local_load_store(b, load, deref, tail->child, inout->elems[i]);
226 }
227 }
228
229 tail->child = old_child;
230 }
231
232 nir_deref_var *
233 vtn_nir_deref(struct vtn_builder *b, uint32_t id)
234 {
235 struct vtn_access_chain *chain =
236 vtn_value(b, id, vtn_value_type_access_chain)->access_chain;
237
238 return vtn_access_chain_to_deref(b, chain);
239 }
240
241 /*
242 * Gets the NIR-level deref tail, which may have as a child an array deref
243 * selecting which component due to OpAccessChain supporting per-component
244 * indexing in SPIR-V.
245 */
246 static nir_deref *
247 get_deref_tail(nir_deref_var *deref)
248 {
249 nir_deref *cur = &deref->deref;
250 while (!glsl_type_is_vector_or_scalar(cur->type) && cur->child)
251 cur = cur->child;
252
253 return cur;
254 }
255
256 struct vtn_ssa_value *
257 vtn_local_load(struct vtn_builder *b, nir_deref_var *src)
258 {
259 nir_deref *src_tail = get_deref_tail(src);
260 struct vtn_ssa_value *val = vtn_create_ssa_value(b, src_tail->type);
261 _vtn_local_load_store(b, true, src, src_tail, val);
262
263 if (src_tail->child) {
264 nir_deref_array *vec_deref = nir_deref_as_array(src_tail->child);
265 assert(vec_deref->deref.child == NULL);
266 val->type = vec_deref->deref.type;
267 if (vec_deref->deref_array_type == nir_deref_array_type_direct)
268 val->def = vtn_vector_extract(b, val->def, vec_deref->base_offset);
269 else
270 val->def = vtn_vector_extract_dynamic(b, val->def,
271 vec_deref->indirect.ssa);
272 }
273
274 return val;
275 }
276
277 void
278 vtn_local_store(struct vtn_builder *b, struct vtn_ssa_value *src,
279 nir_deref_var *dest)
280 {
281 nir_deref *dest_tail = get_deref_tail(dest);
282
283 if (dest_tail->child) {
284 struct vtn_ssa_value *val = vtn_create_ssa_value(b, dest_tail->type);
285 _vtn_local_load_store(b, true, dest, dest_tail, val);
286 nir_deref_array *deref = nir_deref_as_array(dest_tail->child);
287 assert(deref->deref.child == NULL);
288 if (deref->deref_array_type == nir_deref_array_type_direct)
289 val->def = vtn_vector_insert(b, val->def, src->def,
290 deref->base_offset);
291 else
292 val->def = vtn_vector_insert_dynamic(b, val->def, src->def,
293 deref->indirect.ssa);
294 _vtn_local_load_store(b, false, dest, dest_tail, val);
295 } else {
296 _vtn_local_load_store(b, false, dest, dest_tail, src);
297 }
298 }
299
300 static nir_ssa_def *
301 get_vulkan_resource_index(struct vtn_builder *b, struct vtn_access_chain *chain,
302 struct vtn_type **type, unsigned *chain_idx)
303 {
304 /* Push constants have no explicit binding */
305 if (chain->var->mode == vtn_variable_mode_push_constant) {
306 *chain_idx = 0;
307 *type = chain->var->type;
308 return NULL;
309 }
310
311 nir_ssa_def *array_index;
312 if (glsl_type_is_array(chain->var->type->type)) {
313 assert(chain->length > 0);
314 array_index = vtn_access_link_as_ssa(b, chain->link[0], 1);
315 *chain_idx = 1;
316 *type = chain->var->type->array_element;
317 } else {
318 array_index = nir_imm_int(&b->nb, 0);
319 *chain_idx = 0;
320 *type = chain->var->type;
321 }
322
323 nir_intrinsic_instr *instr =
324 nir_intrinsic_instr_create(b->nb.shader,
325 nir_intrinsic_vulkan_resource_index);
326 instr->src[0] = nir_src_for_ssa(array_index);
327 nir_intrinsic_set_desc_set(instr, chain->var->descriptor_set);
328 nir_intrinsic_set_binding(instr, chain->var->binding);
329
330 nir_ssa_dest_init(&instr->instr, &instr->dest, 1, 32, NULL);
331 nir_builder_instr_insert(&b->nb, &instr->instr);
332
333 return &instr->dest.ssa;
334 }
335
336 nir_ssa_def *
337 vtn_access_chain_to_offset(struct vtn_builder *b,
338 struct vtn_access_chain *chain,
339 nir_ssa_def **index_out, struct vtn_type **type_out,
340 unsigned *end_idx_out, bool stop_at_matrix)
341 {
342 unsigned idx = 0;
343 struct vtn_type *type;
344 *index_out = get_vulkan_resource_index(b, chain, &type, &idx);
345
346 nir_ssa_def *offset = nir_imm_int(&b->nb, 0);
347 for (; idx < chain->length; idx++) {
348 enum glsl_base_type base_type = glsl_get_base_type(type->type);
349 switch (base_type) {
350 case GLSL_TYPE_UINT:
351 case GLSL_TYPE_INT:
352 case GLSL_TYPE_UINT64:
353 case GLSL_TYPE_INT64:
354 case GLSL_TYPE_FLOAT:
355 case GLSL_TYPE_DOUBLE:
356 case GLSL_TYPE_BOOL:
357 /* Some users may not want matrix or vector derefs */
358 if (stop_at_matrix)
359 goto end;
360 /* Fall through */
361
362 case GLSL_TYPE_ARRAY:
363 offset = nir_iadd(&b->nb, offset,
364 vtn_access_link_as_ssa(b, chain->link[idx],
365 type->stride));
366
367 type = type->array_element;
368 break;
369
370 case GLSL_TYPE_STRUCT: {
371 assert(chain->link[idx].mode == vtn_access_mode_literal);
372 unsigned member = chain->link[idx].id;
373 offset = nir_iadd(&b->nb, offset,
374 nir_imm_int(&b->nb, type->offsets[member]));
375 type = type->members[member];
376 break;
377 }
378
379 default:
380 unreachable("Invalid type for deref");
381 }
382 }
383
384 end:
385 *type_out = type;
386 if (end_idx_out)
387 *end_idx_out = idx;
388
389 return offset;
390 }
391
392 /* Tries to compute the size of an interface block based on the strides and
393 * offsets that are provided to us in the SPIR-V source.
394 */
395 static unsigned
396 vtn_type_block_size(struct vtn_type *type)
397 {
398 enum glsl_base_type base_type = glsl_get_base_type(type->type);
399 switch (base_type) {
400 case GLSL_TYPE_UINT:
401 case GLSL_TYPE_INT:
402 case GLSL_TYPE_UINT64:
403 case GLSL_TYPE_INT64:
404 case GLSL_TYPE_FLOAT:
405 case GLSL_TYPE_BOOL:
406 case GLSL_TYPE_DOUBLE: {
407 unsigned cols = type->row_major ? glsl_get_vector_elements(type->type) :
408 glsl_get_matrix_columns(type->type);
409 if (cols > 1) {
410 assert(type->stride > 0);
411 return type->stride * cols;
412 } else if (base_type == GLSL_TYPE_DOUBLE ||
413 base_type == GLSL_TYPE_UINT64 ||
414 base_type == GLSL_TYPE_INT64) {
415 return glsl_get_vector_elements(type->type) * 8;
416 } else {
417 return glsl_get_vector_elements(type->type) * 4;
418 }
419 }
420
421 case GLSL_TYPE_STRUCT:
422 case GLSL_TYPE_INTERFACE: {
423 unsigned size = 0;
424 unsigned num_fields = glsl_get_length(type->type);
425 for (unsigned f = 0; f < num_fields; f++) {
426 unsigned field_end = type->offsets[f] +
427 vtn_type_block_size(type->members[f]);
428 size = MAX2(size, field_end);
429 }
430 return size;
431 }
432
433 case GLSL_TYPE_ARRAY:
434 assert(type->stride > 0);
435 assert(glsl_get_length(type->type) > 0);
436 return type->stride * glsl_get_length(type->type);
437
438 default:
439 assert(!"Invalid block type");
440 return 0;
441 }
442 }
443
444 static void
445 vtn_access_chain_get_offset_size(struct vtn_access_chain *chain,
446 unsigned *access_offset,
447 unsigned *access_size)
448 {
449 /* Only valid for push constants accesses now. */
450 assert(chain->var->mode == vtn_variable_mode_push_constant);
451
452 struct vtn_type *type = chain->var->type;
453
454 *access_offset = 0;
455
456 for (unsigned i = 0; i < chain->length; i++) {
457 if (chain->link[i].mode != vtn_access_mode_literal)
458 break;
459
460 if (glsl_type_is_struct(type->type)) {
461 *access_offset += type->offsets[chain->link[i].id];
462 type = type->members[chain->link[i].id];
463 } else {
464 *access_offset += type->stride * chain->link[i].id;
465 type = type->array_element;
466 }
467 }
468
469 *access_size = vtn_type_block_size(type);
470 }
471
472 static void
473 _vtn_load_store_tail(struct vtn_builder *b, nir_intrinsic_op op, bool load,
474 nir_ssa_def *index, nir_ssa_def *offset,
475 unsigned access_offset, unsigned access_size,
476 struct vtn_ssa_value **inout, const struct glsl_type *type)
477 {
478 nir_intrinsic_instr *instr = nir_intrinsic_instr_create(b->nb.shader, op);
479 instr->num_components = glsl_get_vector_elements(type);
480
481 int src = 0;
482 if (!load) {
483 nir_intrinsic_set_write_mask(instr, (1 << instr->num_components) - 1);
484 instr->src[src++] = nir_src_for_ssa((*inout)->def);
485 }
486
487 if (op == nir_intrinsic_load_push_constant) {
488 assert(access_offset % 4 == 0);
489
490 nir_intrinsic_set_base(instr, access_offset);
491 nir_intrinsic_set_range(instr, access_size);
492 }
493
494 if (index)
495 instr->src[src++] = nir_src_for_ssa(index);
496
497 if (op == nir_intrinsic_load_push_constant) {
498 /* We need to subtract the offset from where the intrinsic will load the
499 * data. */
500 instr->src[src++] =
501 nir_src_for_ssa(nir_isub(&b->nb, offset,
502 nir_imm_int(&b->nb, access_offset)));
503 } else {
504 instr->src[src++] = nir_src_for_ssa(offset);
505 }
506
507 if (load) {
508 nir_ssa_dest_init(&instr->instr, &instr->dest,
509 instr->num_components,
510 glsl_get_bit_size(type), NULL);
511 (*inout)->def = &instr->dest.ssa;
512 }
513
514 nir_builder_instr_insert(&b->nb, &instr->instr);
515
516 if (load && glsl_get_base_type(type) == GLSL_TYPE_BOOL)
517 (*inout)->def = nir_ine(&b->nb, (*inout)->def, nir_imm_int(&b->nb, 0));
518 }
519
520 static void
521 _vtn_block_load_store(struct vtn_builder *b, nir_intrinsic_op op, bool load,
522 nir_ssa_def *index, nir_ssa_def *offset,
523 unsigned access_offset, unsigned access_size,
524 struct vtn_access_chain *chain, unsigned chain_idx,
525 struct vtn_type *type, struct vtn_ssa_value **inout)
526 {
527 if (chain && chain_idx >= chain->length)
528 chain = NULL;
529
530 if (load && chain == NULL && *inout == NULL)
531 *inout = vtn_create_ssa_value(b, type->type);
532
533 enum glsl_base_type base_type = glsl_get_base_type(type->type);
534 switch (base_type) {
535 case GLSL_TYPE_UINT:
536 case GLSL_TYPE_INT:
537 case GLSL_TYPE_UINT64:
538 case GLSL_TYPE_INT64:
539 case GLSL_TYPE_FLOAT:
540 case GLSL_TYPE_DOUBLE:
541 case GLSL_TYPE_BOOL:
542 /* This is where things get interesting. At this point, we've hit
543 * a vector, a scalar, or a matrix.
544 */
545 if (glsl_type_is_matrix(type->type)) {
546 if (chain == NULL) {
547 /* Loading the whole matrix */
548 struct vtn_ssa_value *transpose;
549 unsigned num_ops, vec_width;
550 if (type->row_major) {
551 num_ops = glsl_get_vector_elements(type->type);
552 vec_width = glsl_get_matrix_columns(type->type);
553 if (load) {
554 const struct glsl_type *transpose_type =
555 glsl_matrix_type(base_type, vec_width, num_ops);
556 *inout = vtn_create_ssa_value(b, transpose_type);
557 } else {
558 transpose = vtn_ssa_transpose(b, *inout);
559 inout = &transpose;
560 }
561 } else {
562 num_ops = glsl_get_matrix_columns(type->type);
563 vec_width = glsl_get_vector_elements(type->type);
564 }
565
566 for (unsigned i = 0; i < num_ops; i++) {
567 nir_ssa_def *elem_offset =
568 nir_iadd(&b->nb, offset,
569 nir_imm_int(&b->nb, i * type->stride));
570 _vtn_load_store_tail(b, op, load, index, elem_offset,
571 access_offset, access_size,
572 &(*inout)->elems[i],
573 glsl_vector_type(base_type, vec_width));
574 }
575
576 if (load && type->row_major)
577 *inout = vtn_ssa_transpose(b, *inout);
578 } else if (type->row_major) {
579 /* Row-major but with an access chiain. */
580 nir_ssa_def *col_offset =
581 vtn_access_link_as_ssa(b, chain->link[chain_idx],
582 type->array_element->stride);
583 offset = nir_iadd(&b->nb, offset, col_offset);
584
585 if (chain_idx + 1 < chain->length) {
586 /* Picking off a single element */
587 nir_ssa_def *row_offset =
588 vtn_access_link_as_ssa(b, chain->link[chain_idx + 1],
589 type->stride);
590 offset = nir_iadd(&b->nb, offset, row_offset);
591 if (load)
592 *inout = vtn_create_ssa_value(b, glsl_scalar_type(base_type));
593 _vtn_load_store_tail(b, op, load, index, offset,
594 access_offset, access_size,
595 inout, glsl_scalar_type(base_type));
596 } else {
597 /* Grabbing a column; picking one element off each row */
598 unsigned num_comps = glsl_get_vector_elements(type->type);
599 const struct glsl_type *column_type =
600 glsl_get_column_type(type->type);
601
602 nir_ssa_def *comps[4];
603 for (unsigned i = 0; i < num_comps; i++) {
604 nir_ssa_def *elem_offset =
605 nir_iadd(&b->nb, offset,
606 nir_imm_int(&b->nb, i * type->stride));
607
608 struct vtn_ssa_value *comp, temp_val;
609 if (!load) {
610 temp_val.def = nir_channel(&b->nb, (*inout)->def, i);
611 temp_val.type = glsl_scalar_type(base_type);
612 }
613 comp = &temp_val;
614 _vtn_load_store_tail(b, op, load, index, elem_offset,
615 access_offset, access_size,
616 &comp, glsl_scalar_type(base_type));
617 comps[i] = comp->def;
618 }
619
620 if (load) {
621 if (*inout == NULL)
622 *inout = vtn_create_ssa_value(b, column_type);
623
624 (*inout)->def = nir_vec(&b->nb, comps, num_comps);
625 }
626 }
627 } else {
628 /* Column-major with a deref. Fall through to array case. */
629 nir_ssa_def *col_offset =
630 vtn_access_link_as_ssa(b, chain->link[chain_idx], type->stride);
631 offset = nir_iadd(&b->nb, offset, col_offset);
632
633 _vtn_block_load_store(b, op, load, index, offset,
634 access_offset, access_size,
635 chain, chain_idx + 1,
636 type->array_element, inout);
637 }
638 } else if (chain == NULL) {
639 /* Single whole vector */
640 assert(glsl_type_is_vector_or_scalar(type->type));
641 _vtn_load_store_tail(b, op, load, index, offset,
642 access_offset, access_size,
643 inout, type->type);
644 } else {
645 /* Single component of a vector. Fall through to array case. */
646 nir_ssa_def *elem_offset =
647 vtn_access_link_as_ssa(b, chain->link[chain_idx], type->stride);
648 offset = nir_iadd(&b->nb, offset, elem_offset);
649
650 _vtn_block_load_store(b, op, load, index, offset,
651 access_offset, access_size,
652 NULL, 0,
653 type->array_element, inout);
654 }
655 return;
656
657 case GLSL_TYPE_ARRAY: {
658 unsigned elems = glsl_get_length(type->type);
659 for (unsigned i = 0; i < elems; i++) {
660 nir_ssa_def *elem_off =
661 nir_iadd(&b->nb, offset, nir_imm_int(&b->nb, i * type->stride));
662 _vtn_block_load_store(b, op, load, index, elem_off,
663 access_offset, access_size,
664 NULL, 0,
665 type->array_element, &(*inout)->elems[i]);
666 }
667 return;
668 }
669
670 case GLSL_TYPE_STRUCT: {
671 unsigned elems = glsl_get_length(type->type);
672 for (unsigned i = 0; i < elems; i++) {
673 nir_ssa_def *elem_off =
674 nir_iadd(&b->nb, offset, nir_imm_int(&b->nb, type->offsets[i]));
675 _vtn_block_load_store(b, op, load, index, elem_off,
676 access_offset, access_size,
677 NULL, 0,
678 type->members[i], &(*inout)->elems[i]);
679 }
680 return;
681 }
682
683 default:
684 unreachable("Invalid block member type");
685 }
686 }
687
688 static struct vtn_ssa_value *
689 vtn_block_load(struct vtn_builder *b, struct vtn_access_chain *src)
690 {
691 nir_intrinsic_op op;
692 unsigned access_offset = 0, access_size = 0;
693 switch (src->var->mode) {
694 case vtn_variable_mode_ubo:
695 op = nir_intrinsic_load_ubo;
696 break;
697 case vtn_variable_mode_ssbo:
698 op = nir_intrinsic_load_ssbo;
699 break;
700 case vtn_variable_mode_push_constant:
701 op = nir_intrinsic_load_push_constant;
702 vtn_access_chain_get_offset_size(src, &access_offset, &access_size);
703 break;
704 default:
705 assert(!"Invalid block variable mode");
706 }
707
708 nir_ssa_def *offset, *index = NULL;
709 struct vtn_type *type;
710 unsigned chain_idx;
711 offset = vtn_access_chain_to_offset(b, src, &index, &type, &chain_idx, true);
712
713 struct vtn_ssa_value *value = NULL;
714 _vtn_block_load_store(b, op, true, index, offset,
715 access_offset, access_size,
716 src, chain_idx, type, &value);
717 return value;
718 }
719
720 static void
721 vtn_block_store(struct vtn_builder *b, struct vtn_ssa_value *src,
722 struct vtn_access_chain *dst)
723 {
724 nir_ssa_def *offset, *index = NULL;
725 struct vtn_type *type;
726 unsigned chain_idx;
727 offset = vtn_access_chain_to_offset(b, dst, &index, &type, &chain_idx, true);
728
729 _vtn_block_load_store(b, nir_intrinsic_store_ssbo, false, index, offset,
730 0, 0, dst, chain_idx, type, &src);
731 }
732
733 static bool
734 vtn_variable_is_external_block(struct vtn_variable *var)
735 {
736 return var->mode == vtn_variable_mode_ssbo ||
737 var->mode == vtn_variable_mode_ubo ||
738 var->mode == vtn_variable_mode_push_constant;
739 }
740
741 static void
742 _vtn_variable_load_store(struct vtn_builder *b, bool load,
743 struct vtn_access_chain *chain,
744 struct vtn_type *tail_type,
745 struct vtn_ssa_value **inout)
746 {
747 enum glsl_base_type base_type = glsl_get_base_type(tail_type->type);
748 switch (base_type) {
749 case GLSL_TYPE_UINT:
750 case GLSL_TYPE_INT:
751 case GLSL_TYPE_UINT64:
752 case GLSL_TYPE_INT64:
753 case GLSL_TYPE_FLOAT:
754 case GLSL_TYPE_BOOL:
755 case GLSL_TYPE_DOUBLE:
756 /* At this point, we have a scalar, vector, or matrix so we know that
757 * there cannot be any structure splitting still in the way. By
758 * stopping at the matrix level rather than the vector level, we
759 * ensure that matrices get loaded in the optimal way even if they
760 * are storred row-major in a UBO.
761 */
762 if (load) {
763 *inout = vtn_local_load(b, vtn_access_chain_to_deref(b, chain));
764 } else {
765 vtn_local_store(b, *inout, vtn_access_chain_to_deref(b, chain));
766 }
767 return;
768
769 case GLSL_TYPE_ARRAY:
770 case GLSL_TYPE_STRUCT: {
771 struct vtn_access_chain *new_chain =
772 vtn_access_chain_extend(b, chain, 1);
773 new_chain->link[chain->length].mode = vtn_access_mode_literal;
774 unsigned elems = glsl_get_length(tail_type->type);
775 if (load) {
776 assert(*inout == NULL);
777 *inout = rzalloc(b, struct vtn_ssa_value);
778 (*inout)->type = tail_type->type;
779 (*inout)->elems = rzalloc_array(b, struct vtn_ssa_value *, elems);
780 }
781 for (unsigned i = 0; i < elems; i++) {
782 new_chain->link[chain->length].id = i;
783 struct vtn_type *elem_type = base_type == GLSL_TYPE_ARRAY ?
784 tail_type->array_element : tail_type->members[i];
785 _vtn_variable_load_store(b, load, new_chain, elem_type,
786 &(*inout)->elems[i]);
787 }
788 return;
789 }
790
791 default:
792 unreachable("Invalid access chain type");
793 }
794 }
795
796 struct vtn_ssa_value *
797 vtn_variable_load(struct vtn_builder *b, struct vtn_access_chain *src)
798 {
799 if (vtn_variable_is_external_block(src->var)) {
800 return vtn_block_load(b, src);
801 } else {
802 struct vtn_type *tail_type = vtn_access_chain_tail_type(b, src);
803 struct vtn_ssa_value *val = NULL;
804 _vtn_variable_load_store(b, true, src, tail_type, &val);
805 return val;
806 }
807 }
808
809 void
810 vtn_variable_store(struct vtn_builder *b, struct vtn_ssa_value *src,
811 struct vtn_access_chain *dest)
812 {
813 if (vtn_variable_is_external_block(dest->var)) {
814 assert(dest->var->mode == vtn_variable_mode_ssbo);
815 vtn_block_store(b, src, dest);
816 } else {
817 struct vtn_type *tail_type = vtn_access_chain_tail_type(b, dest);
818 _vtn_variable_load_store(b, false, dest, tail_type, &src);
819 }
820 }
821
822 static void
823 _vtn_variable_copy(struct vtn_builder *b, struct vtn_access_chain *dest,
824 struct vtn_access_chain *src, struct vtn_type *tail_type)
825 {
826 enum glsl_base_type base_type = glsl_get_base_type(tail_type->type);
827 switch (base_type) {
828 case GLSL_TYPE_UINT:
829 case GLSL_TYPE_INT:
830 case GLSL_TYPE_UINT64:
831 case GLSL_TYPE_INT64:
832 case GLSL_TYPE_FLOAT:
833 case GLSL_TYPE_DOUBLE:
834 case GLSL_TYPE_BOOL:
835 /* At this point, we have a scalar, vector, or matrix so we know that
836 * there cannot be any structure splitting still in the way. By
837 * stopping at the matrix level rather than the vector level, we
838 * ensure that matrices get loaded in the optimal way even if they
839 * are storred row-major in a UBO.
840 */
841 vtn_variable_store(b, vtn_variable_load(b, src), dest);
842 return;
843
844 case GLSL_TYPE_ARRAY:
845 case GLSL_TYPE_STRUCT: {
846 struct vtn_access_chain *new_src, *new_dest;
847 new_src = vtn_access_chain_extend(b, src, 1);
848 new_dest = vtn_access_chain_extend(b, dest, 1);
849 new_src->link[src->length].mode = vtn_access_mode_literal;
850 new_dest->link[dest->length].mode = vtn_access_mode_literal;
851 unsigned elems = glsl_get_length(tail_type->type);
852 for (unsigned i = 0; i < elems; i++) {
853 new_src->link[src->length].id = i;
854 new_dest->link[dest->length].id = i;
855 struct vtn_type *elem_type = base_type == GLSL_TYPE_ARRAY ?
856 tail_type->array_element : tail_type->members[i];
857 _vtn_variable_copy(b, new_dest, new_src, elem_type);
858 }
859 return;
860 }
861
862 default:
863 unreachable("Invalid access chain type");
864 }
865 }
866
867 static void
868 vtn_variable_copy(struct vtn_builder *b, struct vtn_access_chain *dest,
869 struct vtn_access_chain *src)
870 {
871 struct vtn_type *tail_type = vtn_access_chain_tail_type(b, src);
872 assert(vtn_access_chain_tail_type(b, dest)->type == tail_type->type);
873
874 /* TODO: At some point, we should add a special-case for when we can
875 * just emit a copy_var intrinsic.
876 */
877 _vtn_variable_copy(b, dest, src, tail_type);
878 }
879
880 static void
881 set_mode_system_value(nir_variable_mode *mode)
882 {
883 assert(*mode == nir_var_system_value || *mode == nir_var_shader_in);
884 *mode = nir_var_system_value;
885 }
886
887 static void
888 vtn_get_builtin_location(struct vtn_builder *b,
889 SpvBuiltIn builtin, int *location,
890 nir_variable_mode *mode)
891 {
892 switch (builtin) {
893 case SpvBuiltInPosition:
894 *location = VARYING_SLOT_POS;
895 break;
896 case SpvBuiltInPointSize:
897 *location = VARYING_SLOT_PSIZ;
898 break;
899 case SpvBuiltInClipDistance:
900 *location = VARYING_SLOT_CLIP_DIST0; /* XXX CLIP_DIST1? */
901 break;
902 case SpvBuiltInCullDistance:
903 *location = VARYING_SLOT_CULL_DIST0;
904 break;
905 case SpvBuiltInVertexIndex:
906 *location = SYSTEM_VALUE_VERTEX_ID;
907 set_mode_system_value(mode);
908 break;
909 case SpvBuiltInVertexId:
910 /* Vulkan defines VertexID to be zero-based and reserves the new
911 * builtin keyword VertexIndex to indicate the non-zero-based value.
912 */
913 *location = SYSTEM_VALUE_VERTEX_ID_ZERO_BASE;
914 set_mode_system_value(mode);
915 break;
916 case SpvBuiltInInstanceIndex:
917 *location = SYSTEM_VALUE_INSTANCE_INDEX;
918 set_mode_system_value(mode);
919 break;
920 case SpvBuiltInInstanceId:
921 *location = SYSTEM_VALUE_INSTANCE_ID;
922 set_mode_system_value(mode);
923 break;
924 case SpvBuiltInPrimitiveId:
925 if (b->shader->stage == MESA_SHADER_FRAGMENT) {
926 assert(*mode == nir_var_shader_in);
927 *location = VARYING_SLOT_PRIMITIVE_ID;
928 } else if (*mode == nir_var_shader_out) {
929 *location = VARYING_SLOT_PRIMITIVE_ID;
930 } else {
931 *location = SYSTEM_VALUE_PRIMITIVE_ID;
932 set_mode_system_value(mode);
933 }
934 break;
935 case SpvBuiltInInvocationId:
936 *location = SYSTEM_VALUE_INVOCATION_ID;
937 set_mode_system_value(mode);
938 break;
939 case SpvBuiltInLayer:
940 *location = VARYING_SLOT_LAYER;
941 if (b->shader->stage == MESA_SHADER_FRAGMENT)
942 *mode = nir_var_shader_in;
943 else if (b->shader->stage == MESA_SHADER_GEOMETRY)
944 *mode = nir_var_shader_out;
945 else
946 unreachable("invalid stage for SpvBuiltInLayer");
947 break;
948 case SpvBuiltInViewportIndex:
949 *location = VARYING_SLOT_VIEWPORT;
950 if (b->shader->stage == MESA_SHADER_GEOMETRY)
951 *mode = nir_var_shader_out;
952 else if (b->shader->stage == MESA_SHADER_FRAGMENT)
953 *mode = nir_var_shader_in;
954 else
955 unreachable("invalid stage for SpvBuiltInViewportIndex");
956 break;
957 case SpvBuiltInTessLevelOuter:
958 *location = VARYING_SLOT_TESS_LEVEL_OUTER;
959 break;
960 case SpvBuiltInTessLevelInner:
961 *location = VARYING_SLOT_TESS_LEVEL_INNER;
962 break;
963 case SpvBuiltInTessCoord:
964 *location = SYSTEM_VALUE_TESS_COORD;
965 set_mode_system_value(mode);
966 break;
967 case SpvBuiltInPatchVertices:
968 *location = SYSTEM_VALUE_VERTICES_IN;
969 set_mode_system_value(mode);
970 break;
971 case SpvBuiltInFragCoord:
972 *location = VARYING_SLOT_POS;
973 assert(*mode == nir_var_shader_in);
974 break;
975 case SpvBuiltInPointCoord:
976 *location = VARYING_SLOT_PNTC;
977 assert(*mode == nir_var_shader_in);
978 break;
979 case SpvBuiltInFrontFacing:
980 *location = SYSTEM_VALUE_FRONT_FACE;
981 set_mode_system_value(mode);
982 break;
983 case SpvBuiltInSampleId:
984 *location = SYSTEM_VALUE_SAMPLE_ID;
985 set_mode_system_value(mode);
986 break;
987 case SpvBuiltInSamplePosition:
988 *location = SYSTEM_VALUE_SAMPLE_POS;
989 set_mode_system_value(mode);
990 break;
991 case SpvBuiltInSampleMask:
992 if (*mode == nir_var_shader_out) {
993 *location = FRAG_RESULT_SAMPLE_MASK;
994 } else {
995 *location = SYSTEM_VALUE_SAMPLE_MASK_IN;
996 set_mode_system_value(mode);
997 }
998 break;
999 case SpvBuiltInFragDepth:
1000 *location = FRAG_RESULT_DEPTH;
1001 assert(*mode == nir_var_shader_out);
1002 break;
1003 case SpvBuiltInNumWorkgroups:
1004 *location = SYSTEM_VALUE_NUM_WORK_GROUPS;
1005 set_mode_system_value(mode);
1006 break;
1007 case SpvBuiltInWorkgroupSize:
1008 /* This should already be handled */
1009 unreachable("unsupported builtin");
1010 break;
1011 case SpvBuiltInWorkgroupId:
1012 *location = SYSTEM_VALUE_WORK_GROUP_ID;
1013 set_mode_system_value(mode);
1014 break;
1015 case SpvBuiltInLocalInvocationId:
1016 *location = SYSTEM_VALUE_LOCAL_INVOCATION_ID;
1017 set_mode_system_value(mode);
1018 break;
1019 case SpvBuiltInLocalInvocationIndex:
1020 *location = SYSTEM_VALUE_LOCAL_INVOCATION_INDEX;
1021 set_mode_system_value(mode);
1022 break;
1023 case SpvBuiltInGlobalInvocationId:
1024 *location = SYSTEM_VALUE_GLOBAL_INVOCATION_ID;
1025 set_mode_system_value(mode);
1026 break;
1027 case SpvBuiltInBaseVertex:
1028 *location = SYSTEM_VALUE_BASE_VERTEX;
1029 set_mode_system_value(mode);
1030 break;
1031 case SpvBuiltInBaseInstance:
1032 *location = SYSTEM_VALUE_BASE_INSTANCE;
1033 set_mode_system_value(mode);
1034 break;
1035 case SpvBuiltInDrawIndex:
1036 *location = SYSTEM_VALUE_DRAW_ID;
1037 set_mode_system_value(mode);
1038 break;
1039 case SpvBuiltInViewIndex:
1040 *location = SYSTEM_VALUE_VIEW_INDEX;
1041 set_mode_system_value(mode);
1042 break;
1043 case SpvBuiltInHelperInvocation:
1044 default:
1045 unreachable("unsupported builtin");
1046 }
1047 }
1048
1049 static void
1050 apply_var_decoration(struct vtn_builder *b, nir_variable *nir_var,
1051 const struct vtn_decoration *dec)
1052 {
1053 switch (dec->decoration) {
1054 case SpvDecorationRelaxedPrecision:
1055 break; /* FIXME: Do nothing with this for now. */
1056 case SpvDecorationNoPerspective:
1057 nir_var->data.interpolation = INTERP_MODE_NOPERSPECTIVE;
1058 break;
1059 case SpvDecorationFlat:
1060 nir_var->data.interpolation = INTERP_MODE_FLAT;
1061 break;
1062 case SpvDecorationCentroid:
1063 nir_var->data.centroid = true;
1064 break;
1065 case SpvDecorationSample:
1066 nir_var->data.sample = true;
1067 break;
1068 case SpvDecorationInvariant:
1069 nir_var->data.invariant = true;
1070 break;
1071 case SpvDecorationConstant:
1072 assert(nir_var->constant_initializer != NULL);
1073 nir_var->data.read_only = true;
1074 break;
1075 case SpvDecorationNonReadable:
1076 nir_var->data.image.write_only = true;
1077 break;
1078 case SpvDecorationNonWritable:
1079 nir_var->data.read_only = true;
1080 nir_var->data.image.read_only = true;
1081 break;
1082 case SpvDecorationComponent:
1083 nir_var->data.location_frac = dec->literals[0];
1084 break;
1085 case SpvDecorationIndex:
1086 nir_var->data.index = dec->literals[0];
1087 break;
1088 case SpvDecorationBuiltIn: {
1089 SpvBuiltIn builtin = dec->literals[0];
1090
1091 if (builtin == SpvBuiltInWorkgroupSize) {
1092 /* This shouldn't be a builtin. It's actually a constant. */
1093 nir_var->data.mode = nir_var_global;
1094 nir_var->data.read_only = true;
1095
1096 nir_constant *c = rzalloc(nir_var, nir_constant);
1097 c->values[0].u32[0] = b->shader->info->cs.local_size[0];
1098 c->values[0].u32[1] = b->shader->info->cs.local_size[1];
1099 c->values[0].u32[2] = b->shader->info->cs.local_size[2];
1100 nir_var->constant_initializer = c;
1101 break;
1102 }
1103
1104 nir_variable_mode mode = nir_var->data.mode;
1105 vtn_get_builtin_location(b, builtin, &nir_var->data.location, &mode);
1106 nir_var->data.mode = mode;
1107
1108 switch (builtin) {
1109 case SpvBuiltInTessLevelOuter:
1110 case SpvBuiltInTessLevelInner:
1111 nir_var->data.compact = true;
1112 break;
1113 case SpvBuiltInSamplePosition:
1114 nir_var->data.origin_upper_left = b->origin_upper_left;
1115 /* fallthrough */
1116 case SpvBuiltInFragCoord:
1117 nir_var->data.pixel_center_integer = b->pixel_center_integer;
1118 break;
1119 default:
1120 break;
1121 }
1122 }
1123
1124 case SpvDecorationSpecId:
1125 case SpvDecorationRowMajor:
1126 case SpvDecorationColMajor:
1127 case SpvDecorationMatrixStride:
1128 case SpvDecorationRestrict:
1129 case SpvDecorationAliased:
1130 case SpvDecorationVolatile:
1131 case SpvDecorationCoherent:
1132 case SpvDecorationUniform:
1133 case SpvDecorationStream:
1134 case SpvDecorationOffset:
1135 case SpvDecorationLinkageAttributes:
1136 break; /* Do nothing with these here */
1137
1138 case SpvDecorationPatch:
1139 nir_var->data.patch = true;
1140 break;
1141
1142 case SpvDecorationLocation:
1143 unreachable("Handled above");
1144
1145 case SpvDecorationBlock:
1146 case SpvDecorationBufferBlock:
1147 case SpvDecorationArrayStride:
1148 case SpvDecorationGLSLShared:
1149 case SpvDecorationGLSLPacked:
1150 break; /* These can apply to a type but we don't care about them */
1151
1152 case SpvDecorationBinding:
1153 case SpvDecorationDescriptorSet:
1154 case SpvDecorationNoContraction:
1155 case SpvDecorationInputAttachmentIndex:
1156 vtn_warn("Decoration not allowed for variable or structure member: %s",
1157 spirv_decoration_to_string(dec->decoration));
1158 break;
1159
1160 case SpvDecorationXfbBuffer:
1161 case SpvDecorationXfbStride:
1162 vtn_warn("Vulkan does not have transform feedback: %s",
1163 spirv_decoration_to_string(dec->decoration));
1164 break;
1165
1166 case SpvDecorationCPacked:
1167 case SpvDecorationSaturatedConversion:
1168 case SpvDecorationFuncParamAttr:
1169 case SpvDecorationFPRoundingMode:
1170 case SpvDecorationFPFastMathMode:
1171 case SpvDecorationAlignment:
1172 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1173 spirv_decoration_to_string(dec->decoration));
1174 break;
1175
1176 default:
1177 unreachable("Unhandled decoration");
1178 }
1179 }
1180
1181 static void
1182 var_is_patch_cb(struct vtn_builder *b, struct vtn_value *val, int member,
1183 const struct vtn_decoration *dec, void *out_is_patch)
1184 {
1185 if (dec->decoration == SpvDecorationPatch) {
1186 *((bool *) out_is_patch) = true;
1187 }
1188 }
1189
1190 static void
1191 var_decoration_cb(struct vtn_builder *b, struct vtn_value *val, int member,
1192 const struct vtn_decoration *dec, void *void_var)
1193 {
1194 struct vtn_variable *vtn_var = void_var;
1195
1196 /* Handle decorations that apply to a vtn_variable as a whole */
1197 switch (dec->decoration) {
1198 case SpvDecorationBinding:
1199 vtn_var->binding = dec->literals[0];
1200 return;
1201 case SpvDecorationDescriptorSet:
1202 vtn_var->descriptor_set = dec->literals[0];
1203 return;
1204 case SpvDecorationInputAttachmentIndex:
1205 vtn_var->input_attachment_index = dec->literals[0];
1206 return;
1207 case SpvDecorationPatch:
1208 vtn_var->patch = true;
1209 break;
1210 default:
1211 break;
1212 }
1213
1214 if (val->value_type == vtn_value_type_access_chain) {
1215 assert(val->access_chain->length == 0);
1216 assert(val->access_chain->var == void_var);
1217 assert(member == -1);
1218 } else {
1219 assert(val->value_type == vtn_value_type_type);
1220 }
1221
1222 /* Location is odd. If applied to a split structure, we have to walk the
1223 * whole thing and accumulate the location. It's easier to handle as a
1224 * special case.
1225 */
1226 if (dec->decoration == SpvDecorationLocation) {
1227 unsigned location = dec->literals[0];
1228 bool is_vertex_input;
1229 if (b->shader->stage == MESA_SHADER_FRAGMENT &&
1230 vtn_var->mode == vtn_variable_mode_output) {
1231 is_vertex_input = false;
1232 location += FRAG_RESULT_DATA0;
1233 } else if (b->shader->stage == MESA_SHADER_VERTEX &&
1234 vtn_var->mode == vtn_variable_mode_input) {
1235 is_vertex_input = true;
1236 location += VERT_ATTRIB_GENERIC0;
1237 } else if (vtn_var->mode == vtn_variable_mode_input ||
1238 vtn_var->mode == vtn_variable_mode_output) {
1239 is_vertex_input = false;
1240 location += vtn_var->patch ? VARYING_SLOT_PATCH0 : VARYING_SLOT_VAR0;
1241 } else {
1242 vtn_warn("Location must be on input or output variable");
1243 return;
1244 }
1245
1246 if (vtn_var->var) {
1247 /* This handles the member and lone variable cases */
1248 vtn_var->var->data.location = location;
1249 } else {
1250 /* This handles the structure member case */
1251 assert(vtn_var->members);
1252 unsigned length =
1253 glsl_get_length(glsl_without_array(vtn_var->type->type));
1254 for (unsigned i = 0; i < length; i++) {
1255 vtn_var->members[i]->data.location = location;
1256 location +=
1257 glsl_count_attribute_slots(vtn_var->members[i]->interface_type,
1258 is_vertex_input);
1259 }
1260 }
1261 return;
1262 } else {
1263 if (vtn_var->var) {
1264 assert(member <= 0);
1265 apply_var_decoration(b, vtn_var->var, dec);
1266 } else if (vtn_var->members) {
1267 if (member >= 0) {
1268 assert(vtn_var->members);
1269 apply_var_decoration(b, vtn_var->members[member], dec);
1270 } else {
1271 unsigned length =
1272 glsl_get_length(glsl_without_array(vtn_var->type->type));
1273 for (unsigned i = 0; i < length; i++)
1274 apply_var_decoration(b, vtn_var->members[i], dec);
1275 }
1276 } else {
1277 /* A few variables, those with external storage, have no actual
1278 * nir_variables associated with them. Fortunately, all decorations
1279 * we care about for those variables are on the type only.
1280 */
1281 assert(vtn_var->mode == vtn_variable_mode_ubo ||
1282 vtn_var->mode == vtn_variable_mode_ssbo ||
1283 vtn_var->mode == vtn_variable_mode_push_constant);
1284 }
1285 }
1286 }
1287
1288 static bool
1289 is_per_vertex_inout(const struct vtn_variable *var, gl_shader_stage stage)
1290 {
1291 if (var->patch || !glsl_type_is_array(var->type->type))
1292 return false;
1293
1294 if (var->mode == vtn_variable_mode_input) {
1295 return stage == MESA_SHADER_TESS_CTRL ||
1296 stage == MESA_SHADER_TESS_EVAL ||
1297 stage == MESA_SHADER_GEOMETRY;
1298 }
1299
1300 if (var->mode == vtn_variable_mode_output)
1301 return stage == MESA_SHADER_TESS_CTRL;
1302
1303 return false;
1304 }
1305
1306 void
1307 vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
1308 const uint32_t *w, unsigned count)
1309 {
1310 switch (opcode) {
1311 case SpvOpUndef: {
1312 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_undef);
1313 val->type = vtn_value(b, w[1], vtn_value_type_type)->type;
1314 break;
1315 }
1316
1317 case SpvOpVariable: {
1318 struct vtn_variable *var = rzalloc(b, struct vtn_variable);
1319 var->type = vtn_value(b, w[1], vtn_value_type_type)->type;
1320
1321 var->chain.var = var;
1322 var->chain.length = 0;
1323
1324 struct vtn_value *val =
1325 vtn_push_value(b, w[2], vtn_value_type_access_chain);
1326 val->access_chain = &var->chain;
1327
1328 struct vtn_type *without_array = var->type;
1329 while(glsl_type_is_array(without_array->type))
1330 without_array = without_array->array_element;
1331
1332 nir_variable_mode nir_mode;
1333 switch ((SpvStorageClass)w[3]) {
1334 case SpvStorageClassUniform:
1335 case SpvStorageClassUniformConstant:
1336 if (without_array->block) {
1337 var->mode = vtn_variable_mode_ubo;
1338 b->shader->info->num_ubos++;
1339 } else if (without_array->buffer_block) {
1340 var->mode = vtn_variable_mode_ssbo;
1341 b->shader->info->num_ssbos++;
1342 } else if (glsl_type_is_image(without_array->type)) {
1343 var->mode = vtn_variable_mode_image;
1344 nir_mode = nir_var_uniform;
1345 b->shader->info->num_images++;
1346 } else if (glsl_type_is_sampler(without_array->type)) {
1347 var->mode = vtn_variable_mode_sampler;
1348 nir_mode = nir_var_uniform;
1349 b->shader->info->num_textures++;
1350 } else {
1351 assert(!"Invalid uniform variable type");
1352 }
1353 break;
1354 case SpvStorageClassPushConstant:
1355 var->mode = vtn_variable_mode_push_constant;
1356 assert(b->shader->num_uniforms == 0);
1357 b->shader->num_uniforms = vtn_type_block_size(var->type);
1358 break;
1359 case SpvStorageClassInput:
1360 var->mode = vtn_variable_mode_input;
1361 nir_mode = nir_var_shader_in;
1362 break;
1363 case SpvStorageClassOutput:
1364 var->mode = vtn_variable_mode_output;
1365 nir_mode = nir_var_shader_out;
1366 break;
1367 case SpvStorageClassPrivate:
1368 var->mode = vtn_variable_mode_global;
1369 nir_mode = nir_var_global;
1370 break;
1371 case SpvStorageClassFunction:
1372 var->mode = vtn_variable_mode_local;
1373 nir_mode = nir_var_local;
1374 break;
1375 case SpvStorageClassWorkgroup:
1376 var->mode = vtn_variable_mode_workgroup;
1377 nir_mode = nir_var_shared;
1378 break;
1379 case SpvStorageClassCrossWorkgroup:
1380 case SpvStorageClassGeneric:
1381 case SpvStorageClassAtomicCounter:
1382 default:
1383 unreachable("Unhandled variable storage class");
1384 }
1385
1386 switch (var->mode) {
1387 case vtn_variable_mode_local:
1388 case vtn_variable_mode_global:
1389 case vtn_variable_mode_image:
1390 case vtn_variable_mode_sampler:
1391 case vtn_variable_mode_workgroup:
1392 /* For these, we create the variable normally */
1393 var->var = rzalloc(b->shader, nir_variable);
1394 var->var->name = ralloc_strdup(var->var, val->name);
1395 var->var->type = var->type->type;
1396 var->var->data.mode = nir_mode;
1397
1398 switch (var->mode) {
1399 case vtn_variable_mode_image:
1400 case vtn_variable_mode_sampler:
1401 var->var->interface_type = without_array->type;
1402 break;
1403 default:
1404 var->var->interface_type = NULL;
1405 break;
1406 }
1407 break;
1408
1409 case vtn_variable_mode_input:
1410 case vtn_variable_mode_output: {
1411 /* In order to know whether or not we're a per-vertex inout, we need
1412 * the patch qualifier. This means walking the variable decorations
1413 * early before we actually create any variables. Not a big deal.
1414 *
1415 * GLSLang really likes to place decorations in the most interior
1416 * thing it possibly can. In particular, if you have a struct, it
1417 * will place the patch decorations on the struct members. This
1418 * should be handled by the variable splitting below just fine.
1419 *
1420 * If you have an array-of-struct, things get even more weird as it
1421 * will place the patch decorations on the struct even though it's
1422 * inside an array and some of the members being patch and others not
1423 * makes no sense whatsoever. Since the only sensible thing is for
1424 * it to be all or nothing, we'll call it patch if any of the members
1425 * are declared patch.
1426 */
1427 var->patch = false;
1428 vtn_foreach_decoration(b, val, var_is_patch_cb, &var->patch);
1429 if (glsl_type_is_array(var->type->type) &&
1430 glsl_type_is_struct(without_array->type)) {
1431 vtn_foreach_decoration(b, without_array->val,
1432 var_is_patch_cb, &var->patch);
1433 }
1434
1435 /* For inputs and outputs, we immediately split structures. This
1436 * is for a couple of reasons. For one, builtins may all come in
1437 * a struct and we really want those split out into separate
1438 * variables. For another, interpolation qualifiers can be
1439 * applied to members of the top-level struct ane we need to be
1440 * able to preserve that information.
1441 */
1442
1443 int array_length = -1;
1444 struct vtn_type *interface_type = var->type;
1445 if (is_per_vertex_inout(var, b->shader->stage)) {
1446 /* In Geometry shaders (and some tessellation), inputs come
1447 * in per-vertex arrays. However, some builtins come in
1448 * non-per-vertex, hence the need for the is_array check. In
1449 * any case, there are no non-builtin arrays allowed so this
1450 * check should be sufficient.
1451 */
1452 interface_type = var->type->array_element;
1453 array_length = glsl_get_length(var->type->type);
1454 }
1455
1456 if (glsl_type_is_struct(interface_type->type)) {
1457 /* It's a struct. Split it. */
1458 unsigned num_members = glsl_get_length(interface_type->type);
1459 var->members = ralloc_array(b, nir_variable *, num_members);
1460
1461 for (unsigned i = 0; i < num_members; i++) {
1462 const struct glsl_type *mtype = interface_type->members[i]->type;
1463 if (array_length >= 0)
1464 mtype = glsl_array_type(mtype, array_length);
1465
1466 var->members[i] = rzalloc(b->shader, nir_variable);
1467 var->members[i]->name =
1468 ralloc_asprintf(var->members[i], "%s.%d", val->name, i);
1469 var->members[i]->type = mtype;
1470 var->members[i]->interface_type =
1471 interface_type->members[i]->type;
1472 var->members[i]->data.mode = nir_mode;
1473 var->members[i]->data.patch = var->patch;
1474 }
1475 } else {
1476 var->var = rzalloc(b->shader, nir_variable);
1477 var->var->name = ralloc_strdup(var->var, val->name);
1478 var->var->type = var->type->type;
1479 var->var->interface_type = interface_type->type;
1480 var->var->data.mode = nir_mode;
1481 var->var->data.patch = var->patch;
1482 }
1483
1484 /* For inputs and outputs, we need to grab locations and builtin
1485 * information from the interface type.
1486 */
1487 vtn_foreach_decoration(b, interface_type->val, var_decoration_cb, var);
1488 break;
1489
1490 case vtn_variable_mode_param:
1491 unreachable("Not created through OpVariable");
1492 }
1493
1494 case vtn_variable_mode_ubo:
1495 case vtn_variable_mode_ssbo:
1496 case vtn_variable_mode_push_constant:
1497 /* These don't need actual variables. */
1498 break;
1499 }
1500
1501 if (count > 4) {
1502 assert(count == 5);
1503 nir_constant *constant =
1504 vtn_value(b, w[4], vtn_value_type_constant)->constant;
1505 var->var->constant_initializer =
1506 nir_constant_clone(constant, var->var);
1507 }
1508
1509 vtn_foreach_decoration(b, val, var_decoration_cb, var);
1510
1511 if (var->mode == vtn_variable_mode_image ||
1512 var->mode == vtn_variable_mode_sampler) {
1513 /* XXX: We still need the binding information in the nir_variable
1514 * for these. We should fix that.
1515 */
1516 var->var->data.binding = var->binding;
1517 var->var->data.descriptor_set = var->descriptor_set;
1518 var->var->data.index = var->input_attachment_index;
1519
1520 if (var->mode == vtn_variable_mode_image)
1521 var->var->data.image.format = without_array->image_format;
1522 }
1523
1524 if (var->mode == vtn_variable_mode_local) {
1525 assert(var->members == NULL && var->var != NULL);
1526 nir_function_impl_add_variable(b->impl, var->var);
1527 } else if (var->var) {
1528 nir_shader_add_variable(b->shader, var->var);
1529 } else if (var->members) {
1530 unsigned count = glsl_get_length(without_array->type);
1531 for (unsigned i = 0; i < count; i++) {
1532 assert(var->members[i]->data.mode != nir_var_local);
1533 nir_shader_add_variable(b->shader, var->members[i]);
1534 }
1535 } else {
1536 assert(var->mode == vtn_variable_mode_ubo ||
1537 var->mode == vtn_variable_mode_ssbo ||
1538 var->mode == vtn_variable_mode_push_constant);
1539 }
1540 break;
1541 }
1542
1543 case SpvOpAccessChain:
1544 case SpvOpInBoundsAccessChain: {
1545 struct vtn_access_chain *base, *chain;
1546 struct vtn_value *base_val = vtn_untyped_value(b, w[3]);
1547 if (base_val->value_type == vtn_value_type_sampled_image) {
1548 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1549 * to combine an array of images with a single sampler to get an
1550 * array of sampled images that all share the same sampler.
1551 * Fortunately, this means that we can more-or-less ignore the
1552 * sampler when crawling the access chain, but it does leave us
1553 * with this rather awkward little special-case.
1554 */
1555 base = base_val->sampled_image->image;
1556 } else {
1557 assert(base_val->value_type == vtn_value_type_access_chain);
1558 base = base_val->access_chain;
1559 }
1560
1561 chain = vtn_access_chain_extend(b, base, count - 4);
1562
1563 unsigned idx = base->length;
1564 for (int i = 4; i < count; i++) {
1565 struct vtn_value *link_val = vtn_untyped_value(b, w[i]);
1566 if (link_val->value_type == vtn_value_type_constant) {
1567 chain->link[idx].mode = vtn_access_mode_literal;
1568 chain->link[idx].id = link_val->constant->values[0].u32[0];
1569 } else {
1570 chain->link[idx].mode = vtn_access_mode_id;
1571 chain->link[idx].id = w[i];
1572 }
1573 idx++;
1574 }
1575
1576 if (base_val->value_type == vtn_value_type_sampled_image) {
1577 struct vtn_value *val =
1578 vtn_push_value(b, w[2], vtn_value_type_sampled_image);
1579 val->sampled_image = ralloc(b, struct vtn_sampled_image);
1580 val->sampled_image->image = chain;
1581 val->sampled_image->sampler = base_val->sampled_image->sampler;
1582 } else {
1583 struct vtn_value *val =
1584 vtn_push_value(b, w[2], vtn_value_type_access_chain);
1585 val->access_chain = chain;
1586 }
1587 break;
1588 }
1589
1590 case SpvOpCopyMemory: {
1591 struct vtn_value *dest = vtn_value(b, w[1], vtn_value_type_access_chain);
1592 struct vtn_value *src = vtn_value(b, w[2], vtn_value_type_access_chain);
1593
1594 vtn_variable_copy(b, dest->access_chain, src->access_chain);
1595 break;
1596 }
1597
1598 case SpvOpLoad: {
1599 struct vtn_access_chain *src =
1600 vtn_value(b, w[3], vtn_value_type_access_chain)->access_chain;
1601
1602 if (src->var->mode == vtn_variable_mode_image ||
1603 src->var->mode == vtn_variable_mode_sampler) {
1604 vtn_push_value(b, w[2], vtn_value_type_access_chain)->access_chain = src;
1605 return;
1606 }
1607
1608 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1609 val->ssa = vtn_variable_load(b, src);
1610 break;
1611 }
1612
1613 case SpvOpStore: {
1614 struct vtn_access_chain *dest =
1615 vtn_value(b, w[1], vtn_value_type_access_chain)->access_chain;
1616 struct vtn_ssa_value *src = vtn_ssa_value(b, w[2]);
1617 vtn_variable_store(b, src, dest);
1618 break;
1619 }
1620
1621 case SpvOpArrayLength: {
1622 struct vtn_access_chain *chain =
1623 vtn_value(b, w[3], vtn_value_type_access_chain)->access_chain;
1624
1625 const uint32_t offset = chain->var->type->offsets[w[4]];
1626 const uint32_t stride = chain->var->type->members[w[4]]->stride;
1627
1628 unsigned chain_idx;
1629 struct vtn_type *type;
1630 nir_ssa_def *index =
1631 get_vulkan_resource_index(b, chain, &type, &chain_idx);
1632
1633 nir_intrinsic_instr *instr =
1634 nir_intrinsic_instr_create(b->nb.shader,
1635 nir_intrinsic_get_buffer_size);
1636 instr->src[0] = nir_src_for_ssa(index);
1637 nir_ssa_dest_init(&instr->instr, &instr->dest, 1, 32, NULL);
1638 nir_builder_instr_insert(&b->nb, &instr->instr);
1639 nir_ssa_def *buf_size = &instr->dest.ssa;
1640
1641 /* array_length = max(buffer_size - offset, 0) / stride */
1642 nir_ssa_def *array_length =
1643 nir_idiv(&b->nb,
1644 nir_imax(&b->nb,
1645 nir_isub(&b->nb,
1646 buf_size,
1647 nir_imm_int(&b->nb, offset)),
1648 nir_imm_int(&b->nb, 0u)),
1649 nir_imm_int(&b->nb, stride));
1650
1651 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1652 val->ssa = vtn_create_ssa_value(b, glsl_uint_type());
1653 val->ssa->def = array_length;
1654 break;
1655 }
1656
1657 case SpvOpCopyMemorySized:
1658 default:
1659 unreachable("Unhandled opcode");
1660 }
1661 }