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