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