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