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