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