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