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