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