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