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