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