2 * Copyright © 2015 Intel Corporation
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:
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
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
24 * Jason Ekstrand (jason@jlekstrand.net)
28 #include "vtn_private.h"
30 static struct vtn_access_chain
*
31 vtn_access_chain_extend(struct vtn_builder
*b
, struct vtn_access_chain
*old
,
34 struct vtn_access_chain
*chain
;
36 unsigned new_len
= old
->length
+ new_ids
;
37 chain
= ralloc_size(b
, sizeof(*chain
) + new_len
* sizeof(chain
->link
[0]));
39 chain
->var
= old
->var
;
40 chain
->length
= new_len
;
42 for (unsigned i
= 0; i
< old
->length
; i
++)
43 chain
->link
[i
] = old
->link
[i
];
49 vtn_access_link_as_ssa(struct vtn_builder
*b
, struct vtn_access_link link
,
53 if (link
.mode
== vtn_access_mode_literal
) {
54 return nir_imm_int(&b
->nb
, link
.id
* stride
);
55 } else if (stride
== 1) {
56 return vtn_ssa_value(b
, link
.id
)->def
;
58 return nir_imul(&b
->nb
, vtn_ssa_value(b
, link
.id
)->def
,
59 nir_imm_int(&b
->nb
, stride
));
63 static struct vtn_type
*
64 vtn_access_chain_tail_type(struct vtn_builder
*b
,
65 struct vtn_access_chain
*chain
)
67 struct vtn_type
*type
= chain
->var
->type
;
68 for (unsigned i
= 0; i
< chain
->length
; i
++) {
69 if (glsl_type_is_struct(type
->type
)) {
70 assert(chain
->link
[i
].mode
== vtn_access_mode_literal
);
71 type
= type
->members
[chain
->link
[i
].id
];
73 type
= type
->array_element
;
79 /* Crawls a chain of array derefs and rewrites the types so that the
80 * lengths stay the same but the terminal type is the one given by
81 * tail_type. This is useful for split structures.
84 rewrite_deref_types(nir_deref
*deref
, const struct glsl_type
*type
)
88 assert(deref
->child
->deref_type
== nir_deref_type_array
);
89 assert(glsl_type_is_array(deref
->type
));
90 rewrite_deref_types(deref
->child
, glsl_get_array_element(type
));
95 vtn_access_chain_to_deref(struct vtn_builder
*b
, struct vtn_access_chain
*chain
)
97 nir_deref_var
*deref_var
;
98 if (chain
->var
->var
) {
99 deref_var
= nir_deref_var_create(b
, chain
->var
->var
);
101 assert(chain
->var
->members
);
102 /* Create the deref_var manually. It will get filled out later. */
103 deref_var
= rzalloc(b
, nir_deref_var
);
104 deref_var
->deref
.deref_type
= nir_deref_type_var
;
107 struct vtn_type
*deref_type
= chain
->var
->type
;
108 nir_deref
*tail
= &deref_var
->deref
;
109 nir_variable
**members
= chain
->var
->members
;
111 for (unsigned i
= 0; i
< chain
->length
; i
++) {
112 enum glsl_base_type base_type
= glsl_get_base_type(deref_type
->type
);
116 case GLSL_TYPE_FLOAT
:
117 case GLSL_TYPE_DOUBLE
:
119 case GLSL_TYPE_ARRAY
: {
120 deref_type
= deref_type
->array_element
;
122 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
123 deref_arr
->deref
.type
= deref_type
->type
;
125 if (chain
->link
[i
].mode
== vtn_access_mode_literal
) {
126 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
127 deref_arr
->base_offset
= chain
->link
[i
].id
;
129 assert(chain
->link
[i
].mode
== vtn_access_mode_id
);
130 deref_arr
->deref_array_type
= nir_deref_array_type_indirect
;
131 deref_arr
->base_offset
= 0;
132 deref_arr
->indirect
=
133 nir_src_for_ssa(vtn_ssa_value(b
, chain
->link
[i
].id
)->def
);
135 tail
->child
= &deref_arr
->deref
;
140 case GLSL_TYPE_STRUCT
: {
141 assert(chain
->link
[i
].mode
== vtn_access_mode_literal
);
142 unsigned idx
= chain
->link
[i
].id
;
143 deref_type
= deref_type
->members
[idx
];
145 /* This is a pre-split structure. */
146 deref_var
->var
= members
[idx
];
147 rewrite_deref_types(&deref_var
->deref
, members
[idx
]->type
);
148 assert(tail
->type
== deref_type
->type
);
151 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, idx
);
152 deref_struct
->deref
.type
= deref_type
->type
;
153 tail
->child
= &deref_struct
->deref
;
159 unreachable("Invalid type for deref");
163 assert(members
== NULL
);
168 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_var
*deref
,
169 nir_deref
*tail
, struct vtn_ssa_value
*inout
)
171 /* The deref tail may contain a deref to select a component of a vector (in
172 * other words, it might not be an actual tail) so we have to save it away
173 * here since we overwrite it later.
175 nir_deref
*old_child
= tail
->child
;
177 if (glsl_type_is_vector_or_scalar(tail
->type
)) {
178 /* Terminate the deref chain in case there is one more link to pick
179 * off a component of the vector.
183 nir_intrinsic_op op
= load
? nir_intrinsic_load_var
:
184 nir_intrinsic_store_var
;
186 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
187 intrin
->variables
[0] =
188 nir_deref_as_var(nir_copy_deref(intrin
, &deref
->deref
));
189 intrin
->num_components
= glsl_get_vector_elements(tail
->type
);
192 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
193 intrin
->num_components
,
194 glsl_get_bit_size(tail
->type
),
196 inout
->def
= &intrin
->dest
.ssa
;
198 nir_intrinsic_set_write_mask(intrin
, (1 << intrin
->num_components
) - 1);
199 intrin
->src
[0] = nir_src_for_ssa(inout
->def
);
202 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
203 } else if (glsl_get_base_type(tail
->type
) == GLSL_TYPE_ARRAY
||
204 glsl_type_is_matrix(tail
->type
)) {
205 unsigned elems
= glsl_get_length(tail
->type
);
206 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
207 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
208 deref_arr
->deref
.type
= glsl_get_array_element(tail
->type
);
209 tail
->child
= &deref_arr
->deref
;
210 for (unsigned i
= 0; i
< elems
; i
++) {
211 deref_arr
->base_offset
= i
;
212 _vtn_local_load_store(b
, load
, deref
, tail
->child
, inout
->elems
[i
]);
215 assert(glsl_get_base_type(tail
->type
) == GLSL_TYPE_STRUCT
);
216 unsigned elems
= glsl_get_length(tail
->type
);
217 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, 0);
218 tail
->child
= &deref_struct
->deref
;
219 for (unsigned i
= 0; i
< elems
; i
++) {
220 deref_struct
->index
= i
;
221 deref_struct
->deref
.type
= glsl_get_struct_field(tail
->type
, i
);
222 _vtn_local_load_store(b
, load
, deref
, tail
->child
, inout
->elems
[i
]);
226 tail
->child
= old_child
;
230 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
232 struct vtn_access_chain
*chain
=
233 vtn_value(b
, id
, vtn_value_type_access_chain
)->access_chain
;
235 return vtn_access_chain_to_deref(b
, chain
);
239 * Gets the NIR-level deref tail, which may have as a child an array deref
240 * selecting which component due to OpAccessChain supporting per-component
241 * indexing in SPIR-V.
244 get_deref_tail(nir_deref_var
*deref
)
246 nir_deref
*cur
= &deref
->deref
;
247 while (!glsl_type_is_vector_or_scalar(cur
->type
) && cur
->child
)
253 struct vtn_ssa_value
*
254 vtn_local_load(struct vtn_builder
*b
, nir_deref_var
*src
)
256 nir_deref
*src_tail
= get_deref_tail(src
);
257 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
258 _vtn_local_load_store(b
, true, src
, src_tail
, val
);
260 if (src_tail
->child
) {
261 nir_deref_array
*vec_deref
= nir_deref_as_array(src_tail
->child
);
262 assert(vec_deref
->deref
.child
== NULL
);
263 val
->type
= vec_deref
->deref
.type
;
264 if (vec_deref
->deref_array_type
== nir_deref_array_type_direct
)
265 val
->def
= vtn_vector_extract(b
, val
->def
, vec_deref
->base_offset
);
267 val
->def
= vtn_vector_extract_dynamic(b
, val
->def
,
268 vec_deref
->indirect
.ssa
);
275 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
278 nir_deref
*dest_tail
= get_deref_tail(dest
);
280 if (dest_tail
->child
) {
281 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
282 _vtn_local_load_store(b
, true, dest
, dest_tail
, val
);
283 nir_deref_array
*deref
= nir_deref_as_array(dest_tail
->child
);
284 assert(deref
->deref
.child
== NULL
);
285 if (deref
->deref_array_type
== nir_deref_array_type_direct
)
286 val
->def
= vtn_vector_insert(b
, val
->def
, src
->def
,
289 val
->def
= vtn_vector_insert_dynamic(b
, val
->def
, src
->def
,
290 deref
->indirect
.ssa
);
291 _vtn_local_load_store(b
, false, dest
, dest_tail
, val
);
293 _vtn_local_load_store(b
, false, dest
, dest_tail
, src
);
298 get_vulkan_resource_index(struct vtn_builder
*b
, struct vtn_access_chain
*chain
,
299 struct vtn_type
**type
, unsigned *chain_idx
)
301 /* Push constants have no explicit binding */
302 if (chain
->var
->mode
== vtn_variable_mode_push_constant
) {
304 *type
= chain
->var
->type
;
308 nir_ssa_def
*array_index
;
309 if (glsl_type_is_array(chain
->var
->type
->type
)) {
310 assert(chain
->length
> 0);
311 array_index
= vtn_access_link_as_ssa(b
, chain
->link
[0], 1);
313 *type
= chain
->var
->type
->array_element
;
315 array_index
= nir_imm_int(&b
->nb
, 0);
317 *type
= chain
->var
->type
;
320 nir_intrinsic_instr
*instr
=
321 nir_intrinsic_instr_create(b
->nb
.shader
,
322 nir_intrinsic_vulkan_resource_index
);
323 instr
->src
[0] = nir_src_for_ssa(array_index
);
324 nir_intrinsic_set_desc_set(instr
, chain
->var
->descriptor_set
);
325 nir_intrinsic_set_binding(instr
, chain
->var
->binding
);
327 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
328 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
330 return &instr
->dest
.ssa
;
334 vtn_access_chain_to_offset(struct vtn_builder
*b
,
335 struct vtn_access_chain
*chain
,
336 nir_ssa_def
**index_out
, struct vtn_type
**type_out
,
337 unsigned *end_idx_out
, bool stop_at_matrix
)
340 struct vtn_type
*type
;
341 *index_out
= get_vulkan_resource_index(b
, chain
, &type
, &idx
);
343 nir_ssa_def
*offset
= nir_imm_int(&b
->nb
, 0);
344 for (; idx
< chain
->length
; idx
++) {
345 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
349 case GLSL_TYPE_FLOAT
:
350 case GLSL_TYPE_DOUBLE
:
352 /* Some users may not want matrix or vector derefs */
357 case GLSL_TYPE_ARRAY
:
358 offset
= nir_iadd(&b
->nb
, offset
,
359 vtn_access_link_as_ssa(b
, chain
->link
[idx
],
362 type
= type
->array_element
;
365 case GLSL_TYPE_STRUCT
: {
366 assert(chain
->link
[idx
].mode
== vtn_access_mode_literal
);
367 unsigned member
= chain
->link
[idx
].id
;
368 offset
= nir_iadd(&b
->nb
, offset
,
369 nir_imm_int(&b
->nb
, type
->offsets
[member
]));
370 type
= type
->members
[member
];
375 unreachable("Invalid type for deref");
388 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
389 nir_ssa_def
*index
, nir_ssa_def
*offset
,
390 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
)
392 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
393 instr
->num_components
= glsl_get_vector_elements(type
);
397 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
398 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
401 /* We set the base and size for push constant load to the entire push
402 * constant block for now.
404 if (op
== nir_intrinsic_load_push_constant
) {
405 nir_intrinsic_set_base(instr
, 0);
406 nir_intrinsic_set_range(instr
, 128);
410 instr
->src
[src
++] = nir_src_for_ssa(index
);
412 instr
->src
[src
++] = nir_src_for_ssa(offset
);
415 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
416 instr
->num_components
,
417 glsl_get_bit_size(type
), NULL
);
418 (*inout
)->def
= &instr
->dest
.ssa
;
421 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
423 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
424 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
428 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
429 nir_ssa_def
*index
, nir_ssa_def
*offset
,
430 struct vtn_access_chain
*chain
, unsigned chain_idx
,
431 struct vtn_type
*type
, struct vtn_ssa_value
**inout
)
433 if (chain
&& chain_idx
>= chain
->length
)
436 if (load
&& chain
== NULL
&& *inout
== NULL
)
437 *inout
= vtn_create_ssa_value(b
, type
->type
);
439 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
443 case GLSL_TYPE_FLOAT
:
445 /* This is where things get interesting. At this point, we've hit
446 * a vector, a scalar, or a matrix.
448 if (glsl_type_is_matrix(type
->type
)) {
450 /* Loading the whole matrix */
451 struct vtn_ssa_value
*transpose
;
452 unsigned num_ops
, vec_width
;
453 if (type
->row_major
) {
454 num_ops
= glsl_get_vector_elements(type
->type
);
455 vec_width
= glsl_get_matrix_columns(type
->type
);
457 const struct glsl_type
*transpose_type
=
458 glsl_matrix_type(base_type
, vec_width
, num_ops
);
459 *inout
= vtn_create_ssa_value(b
, transpose_type
);
461 transpose
= vtn_ssa_transpose(b
, *inout
);
465 num_ops
= glsl_get_matrix_columns(type
->type
);
466 vec_width
= glsl_get_vector_elements(type
->type
);
469 for (unsigned i
= 0; i
< num_ops
; i
++) {
470 nir_ssa_def
*elem_offset
=
471 nir_iadd(&b
->nb
, offset
,
472 nir_imm_int(&b
->nb
, i
* type
->stride
));
473 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
475 glsl_vector_type(base_type
, vec_width
));
478 if (load
&& type
->row_major
)
479 *inout
= vtn_ssa_transpose(b
, *inout
);
480 } else if (type
->row_major
) {
481 /* Row-major but with an access chiain. */
482 nir_ssa_def
*col_offset
=
483 vtn_access_link_as_ssa(b
, chain
->link
[chain_idx
],
484 type
->array_element
->stride
);
485 offset
= nir_iadd(&b
->nb
, offset
, col_offset
);
487 if (chain_idx
+ 1 < chain
->length
) {
488 /* Picking off a single element */
489 nir_ssa_def
*row_offset
=
490 vtn_access_link_as_ssa(b
, chain
->link
[chain_idx
+ 1],
492 offset
= nir_iadd(&b
->nb
, offset
, row_offset
);
494 *inout
= vtn_create_ssa_value(b
, glsl_scalar_type(base_type
));
495 _vtn_load_store_tail(b
, op
, load
, index
, offset
, inout
,
496 glsl_scalar_type(base_type
));
498 /* Grabbing a column; picking one element off each row */
499 unsigned num_comps
= glsl_get_vector_elements(type
->type
);
500 const struct glsl_type
*column_type
=
501 glsl_get_column_type(type
->type
);
503 nir_ssa_def
*comps
[4];
504 for (unsigned i
= 0; i
< num_comps
; i
++) {
505 nir_ssa_def
*elem_offset
=
506 nir_iadd(&b
->nb
, offset
,
507 nir_imm_int(&b
->nb
, i
* type
->stride
));
509 struct vtn_ssa_value
*comp
, temp_val
;
511 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
512 temp_val
.type
= glsl_scalar_type(base_type
);
515 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
516 &comp
, glsl_scalar_type(base_type
));
517 comps
[i
] = comp
->def
;
522 *inout
= vtn_create_ssa_value(b
, column_type
);
524 (*inout
)->def
= nir_vec(&b
->nb
, comps
, num_comps
);
528 /* Column-major with a deref. Fall through to array case. */
529 nir_ssa_def
*col_offset
=
530 vtn_access_link_as_ssa(b
, chain
->link
[chain_idx
], type
->stride
);
531 offset
= nir_iadd(&b
->nb
, offset
, col_offset
);
533 _vtn_block_load_store(b
, op
, load
, index
, offset
,
534 chain
, chain_idx
+ 1,
535 type
->array_element
, inout
);
537 } else if (chain
== NULL
) {
538 /* Single whole vector */
539 assert(glsl_type_is_vector_or_scalar(type
->type
));
540 _vtn_load_store_tail(b
, op
, load
, index
, offset
, inout
, type
->type
);
542 /* Single component of a vector. Fall through to array case. */
543 nir_ssa_def
*elem_offset
=
544 vtn_access_link_as_ssa(b
, chain
->link
[chain_idx
], type
->stride
);
545 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
547 _vtn_block_load_store(b
, op
, load
, index
, offset
, NULL
, 0,
548 type
->array_element
, inout
);
552 case GLSL_TYPE_ARRAY
: {
553 unsigned elems
= glsl_get_length(type
->type
);
554 for (unsigned i
= 0; i
< elems
; i
++) {
555 nir_ssa_def
*elem_off
=
556 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* type
->stride
));
557 _vtn_block_load_store(b
, op
, load
, index
, elem_off
, NULL
, 0,
558 type
->array_element
, &(*inout
)->elems
[i
]);
563 case GLSL_TYPE_STRUCT
: {
564 unsigned elems
= glsl_get_length(type
->type
);
565 for (unsigned i
= 0; i
< elems
; i
++) {
566 nir_ssa_def
*elem_off
=
567 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, type
->offsets
[i
]));
568 _vtn_block_load_store(b
, op
, load
, index
, elem_off
, NULL
, 0,
569 type
->members
[i
], &(*inout
)->elems
[i
]);
575 unreachable("Invalid block member type");
579 static struct vtn_ssa_value
*
580 vtn_block_load(struct vtn_builder
*b
, struct vtn_access_chain
*src
)
583 switch (src
->var
->mode
) {
584 case vtn_variable_mode_ubo
:
585 op
= nir_intrinsic_load_ubo
;
587 case vtn_variable_mode_ssbo
:
588 op
= nir_intrinsic_load_ssbo
;
590 case vtn_variable_mode_push_constant
:
591 op
= nir_intrinsic_load_push_constant
;
594 assert(!"Invalid block variable mode");
597 nir_ssa_def
*offset
, *index
= NULL
;
598 struct vtn_type
*type
;
600 offset
= vtn_access_chain_to_offset(b
, src
, &index
, &type
, &chain_idx
, true);
602 struct vtn_ssa_value
*value
= NULL
;
603 _vtn_block_load_store(b
, op
, true, index
, offset
,
604 src
, chain_idx
, type
, &value
);
609 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
610 struct vtn_access_chain
*dst
)
612 nir_ssa_def
*offset
, *index
= NULL
;
613 struct vtn_type
*type
;
615 offset
= vtn_access_chain_to_offset(b
, dst
, &index
, &type
, &chain_idx
, true);
617 _vtn_block_load_store(b
, nir_intrinsic_store_ssbo
, false, index
, offset
,
618 dst
, chain_idx
, type
, &src
);
622 vtn_variable_is_external_block(struct vtn_variable
*var
)
624 return var
->mode
== vtn_variable_mode_ssbo
||
625 var
->mode
== vtn_variable_mode_ubo
||
626 var
->mode
== vtn_variable_mode_push_constant
;
630 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
631 struct vtn_access_chain
*chain
,
632 struct vtn_type
*tail_type
,
633 struct vtn_ssa_value
**inout
)
635 enum glsl_base_type base_type
= glsl_get_base_type(tail_type
->type
);
639 case GLSL_TYPE_FLOAT
:
641 /* At this point, we have a scalar, vector, or matrix so we know that
642 * there cannot be any structure splitting still in the way. By
643 * stopping at the matrix level rather than the vector level, we
644 * ensure that matrices get loaded in the optimal way even if they
645 * are storred row-major in a UBO.
648 *inout
= vtn_local_load(b
, vtn_access_chain_to_deref(b
, chain
));
650 vtn_local_store(b
, *inout
, vtn_access_chain_to_deref(b
, chain
));
654 case GLSL_TYPE_ARRAY
:
655 case GLSL_TYPE_STRUCT
: {
656 struct vtn_access_chain
*new_chain
=
657 vtn_access_chain_extend(b
, chain
, 1);
658 new_chain
->link
[chain
->length
].mode
= vtn_access_mode_literal
;
659 unsigned elems
= glsl_get_length(tail_type
->type
);
661 assert(*inout
== NULL
);
662 *inout
= rzalloc(b
, struct vtn_ssa_value
);
663 (*inout
)->type
= tail_type
->type
;
664 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
666 for (unsigned i
= 0; i
< elems
; i
++) {
667 new_chain
->link
[chain
->length
].id
= i
;
668 struct vtn_type
*elem_type
= base_type
== GLSL_TYPE_ARRAY
?
669 tail_type
->array_element
: tail_type
->members
[i
];
670 _vtn_variable_load_store(b
, load
, new_chain
, elem_type
,
671 &(*inout
)->elems
[i
]);
677 unreachable("Invalid access chain type");
681 struct vtn_ssa_value
*
682 vtn_variable_load(struct vtn_builder
*b
, struct vtn_access_chain
*src
)
684 if (vtn_variable_is_external_block(src
->var
)) {
685 return vtn_block_load(b
, src
);
687 struct vtn_type
*tail_type
= vtn_access_chain_tail_type(b
, src
);
688 struct vtn_ssa_value
*val
= NULL
;
689 _vtn_variable_load_store(b
, true, src
, tail_type
, &val
);
695 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
696 struct vtn_access_chain
*dest
)
698 if (vtn_variable_is_external_block(dest
->var
)) {
699 assert(dest
->var
->mode
== vtn_variable_mode_ssbo
);
700 vtn_block_store(b
, src
, dest
);
702 struct vtn_type
*tail_type
= vtn_access_chain_tail_type(b
, dest
);
703 _vtn_variable_load_store(b
, false, dest
, tail_type
, &src
);
708 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_access_chain
*dest
,
709 struct vtn_access_chain
*src
, struct vtn_type
*tail_type
)
711 enum glsl_base_type base_type
= glsl_get_base_type(tail_type
->type
);
715 case GLSL_TYPE_FLOAT
:
717 /* At this point, we have a scalar, vector, or matrix so we know that
718 * there cannot be any structure splitting still in the way. By
719 * stopping at the matrix level rather than the vector level, we
720 * ensure that matrices get loaded in the optimal way even if they
721 * are storred row-major in a UBO.
723 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
726 case GLSL_TYPE_ARRAY
:
727 case GLSL_TYPE_STRUCT
: {
728 struct vtn_access_chain
*new_src
, *new_dest
;
729 new_src
= vtn_access_chain_extend(b
, src
, 1);
730 new_dest
= vtn_access_chain_extend(b
, dest
, 1);
731 new_src
->link
[src
->length
].mode
= vtn_access_mode_literal
;
732 new_dest
->link
[dest
->length
].mode
= vtn_access_mode_literal
;
733 unsigned elems
= glsl_get_length(tail_type
->type
);
734 for (unsigned i
= 0; i
< elems
; i
++) {
735 new_src
->link
[src
->length
].id
= i
;
736 new_dest
->link
[dest
->length
].id
= i
;
737 struct vtn_type
*elem_type
= base_type
== GLSL_TYPE_ARRAY
?
738 tail_type
->array_element
: tail_type
->members
[i
];
739 _vtn_variable_copy(b
, new_dest
, new_src
, elem_type
);
745 unreachable("Invalid access chain type");
750 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_access_chain
*dest
,
751 struct vtn_access_chain
*src
)
753 struct vtn_type
*tail_type
= vtn_access_chain_tail_type(b
, src
);
754 assert(vtn_access_chain_tail_type(b
, dest
)->type
== tail_type
->type
);
756 /* TODO: At some point, we should add a special-case for when we can
757 * just emit a copy_var intrinsic.
759 _vtn_variable_copy(b
, dest
, src
, tail_type
);
763 set_mode_system_value(nir_variable_mode
*mode
)
765 assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
766 *mode
= nir_var_system_value
;
770 vtn_get_builtin_location(struct vtn_builder
*b
,
771 SpvBuiltIn builtin
, int *location
,
772 nir_variable_mode
*mode
)
775 case SpvBuiltInPosition
:
776 *location
= VARYING_SLOT_POS
;
778 case SpvBuiltInPointSize
:
779 *location
= VARYING_SLOT_PSIZ
;
781 case SpvBuiltInClipDistance
:
782 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
784 case SpvBuiltInCullDistance
:
785 /* XXX figure this out */
787 case SpvBuiltInVertexIndex
:
788 *location
= SYSTEM_VALUE_VERTEX_ID
;
789 set_mode_system_value(mode
);
791 case SpvBuiltInVertexId
:
792 /* Vulkan defines VertexID to be zero-based and reserves the new
793 * builtin keyword VertexIndex to indicate the non-zero-based value.
795 *location
= SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
796 set_mode_system_value(mode
);
798 case SpvBuiltInInstanceIndex
:
799 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
800 set_mode_system_value(mode
);
802 case SpvBuiltInInstanceId
:
803 *location
= SYSTEM_VALUE_INSTANCE_ID
;
804 set_mode_system_value(mode
);
806 case SpvBuiltInPrimitiveId
:
807 *location
= VARYING_SLOT_PRIMITIVE_ID
;
808 *mode
= nir_var_shader_out
;
810 case SpvBuiltInInvocationId
:
811 *location
= SYSTEM_VALUE_INVOCATION_ID
;
812 set_mode_system_value(mode
);
814 case SpvBuiltInLayer
:
815 *location
= VARYING_SLOT_LAYER
;
816 *mode
= nir_var_shader_out
;
818 case SpvBuiltInViewportIndex
:
819 *location
= VARYING_SLOT_VIEWPORT
;
820 if (b
->shader
->stage
== MESA_SHADER_GEOMETRY
)
821 *mode
= nir_var_shader_out
;
822 else if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
)
823 *mode
= nir_var_shader_in
;
825 unreachable("invalid stage for SpvBuiltInViewportIndex");
827 case SpvBuiltInTessLevelOuter
:
828 case SpvBuiltInTessLevelInner
:
829 case SpvBuiltInTessCoord
:
830 case SpvBuiltInPatchVertices
:
831 unreachable("no tessellation support");
832 case SpvBuiltInFragCoord
:
833 *location
= VARYING_SLOT_POS
;
834 assert(*mode
== nir_var_shader_in
);
836 case SpvBuiltInPointCoord
:
837 *location
= VARYING_SLOT_PNTC
;
838 assert(*mode
== nir_var_shader_in
);
840 case SpvBuiltInFrontFacing
:
841 *location
= VARYING_SLOT_FACE
;
842 assert(*mode
== nir_var_shader_in
);
844 case SpvBuiltInSampleId
:
845 *location
= SYSTEM_VALUE_SAMPLE_ID
;
846 set_mode_system_value(mode
);
848 case SpvBuiltInSamplePosition
:
849 *location
= SYSTEM_VALUE_SAMPLE_POS
;
850 set_mode_system_value(mode
);
852 case SpvBuiltInSampleMask
:
853 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
; /* XXX out? */
854 set_mode_system_value(mode
);
856 case SpvBuiltInFragDepth
:
857 *location
= FRAG_RESULT_DEPTH
;
858 assert(*mode
== nir_var_shader_out
);
860 case SpvBuiltInNumWorkgroups
:
861 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
862 set_mode_system_value(mode
);
864 case SpvBuiltInWorkgroupSize
:
865 /* This should already be handled */
866 unreachable("unsupported builtin");
868 case SpvBuiltInWorkgroupId
:
869 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
870 set_mode_system_value(mode
);
872 case SpvBuiltInLocalInvocationId
:
873 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
874 set_mode_system_value(mode
);
876 case SpvBuiltInLocalInvocationIndex
:
877 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
878 set_mode_system_value(mode
);
880 case SpvBuiltInGlobalInvocationId
:
881 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
882 set_mode_system_value(mode
);
884 case SpvBuiltInHelperInvocation
:
886 unreachable("unsupported builtin");
891 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
892 const struct vtn_decoration
*dec
, void *void_var
)
894 struct vtn_variable
*vtn_var
= void_var
;
896 /* Handle decorations that apply to a vtn_variable as a whole */
897 switch (dec
->decoration
) {
898 case SpvDecorationNonWritable
:
899 /* Do nothing with this for now */
901 case SpvDecorationBinding
:
902 vtn_var
->binding
= dec
->literals
[0];
904 case SpvDecorationDescriptorSet
:
905 vtn_var
->descriptor_set
= dec
->literals
[0];
908 case SpvDecorationLocation
: {
909 unsigned location
= dec
->literals
[0];
910 bool is_vertex_input
;
911 if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
&&
912 vtn_var
->mode
== vtn_variable_mode_output
) {
913 is_vertex_input
= false;
914 location
+= FRAG_RESULT_DATA0
;
915 } else if (b
->shader
->stage
== MESA_SHADER_VERTEX
&&
916 vtn_var
->mode
== vtn_variable_mode_input
) {
917 is_vertex_input
= true;
918 location
+= VERT_ATTRIB_GENERIC0
;
919 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
920 vtn_var
->mode
== vtn_variable_mode_output
) {
921 is_vertex_input
= false;
922 location
+= VARYING_SLOT_VAR0
;
924 assert(!"Location must be on input or output variable");
928 vtn_var
->var
->data
.location
= location
;
929 vtn_var
->var
->data
.explicit_location
= true;
931 assert(vtn_var
->members
);
933 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
934 for (unsigned i
= 0; i
< length
; i
++) {
935 vtn_var
->members
[i
]->data
.location
= location
;
936 vtn_var
->members
[i
]->data
.explicit_location
= true;
938 glsl_count_attribute_slots(vtn_var
->members
[i
]->interface_type
,
949 /* Now we handle decorations that apply to a particular nir_variable */
950 nir_variable
*nir_var
= vtn_var
->var
;
951 if (val
->value_type
== vtn_value_type_access_chain
) {
952 assert(val
->access_chain
->length
== 0);
953 assert(val
->access_chain
->var
== void_var
);
954 assert(member
== -1);
956 assert(val
->value_type
== vtn_value_type_type
);
958 nir_var
= vtn_var
->members
[member
];
964 switch (dec
->decoration
) {
965 case SpvDecorationRelaxedPrecision
:
966 break; /* FIXME: Do nothing with this for now. */
967 case SpvDecorationNoPerspective
:
968 nir_var
->data
.interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
970 case SpvDecorationFlat
:
971 nir_var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
973 case SpvDecorationCentroid
:
974 nir_var
->data
.centroid
= true;
976 case SpvDecorationSample
:
977 nir_var
->data
.sample
= true;
979 case SpvDecorationInvariant
:
980 nir_var
->data
.invariant
= true;
982 case SpvDecorationConstant
:
983 assert(nir_var
->constant_initializer
!= NULL
);
984 nir_var
->data
.read_only
= true;
986 case SpvDecorationNonWritable
:
987 nir_var
->data
.read_only
= true;
989 case SpvDecorationComponent
:
990 nir_var
->data
.location_frac
= dec
->literals
[0];
992 case SpvDecorationIndex
:
993 nir_var
->data
.explicit_index
= true;
994 nir_var
->data
.index
= dec
->literals
[0];
996 case SpvDecorationBuiltIn
: {
997 SpvBuiltIn builtin
= dec
->literals
[0];
999 if (builtin
== SpvBuiltInWorkgroupSize
) {
1000 /* This shouldn't be a builtin. It's actually a constant. */
1001 nir_var
->data
.mode
= nir_var_global
;
1002 nir_var
->data
.read_only
= true;
1004 nir_constant
*c
= rzalloc(nir_var
, nir_constant
);
1005 c
->value
.u
[0] = b
->shader
->info
.cs
.local_size
[0];
1006 c
->value
.u
[1] = b
->shader
->info
.cs
.local_size
[1];
1007 c
->value
.u
[2] = b
->shader
->info
.cs
.local_size
[2];
1008 nir_var
->constant_initializer
= c
;
1012 nir_variable_mode mode
= nir_var
->data
.mode
;
1013 vtn_get_builtin_location(b
, builtin
, &nir_var
->data
.location
, &mode
);
1014 nir_var
->data
.explicit_location
= true;
1015 nir_var
->data
.mode
= mode
;
1017 if (builtin
== SpvBuiltInFragCoord
|| builtin
== SpvBuiltInSamplePosition
)
1018 nir_var
->data
.origin_upper_left
= b
->origin_upper_left
;
1021 case SpvDecorationRowMajor
:
1022 case SpvDecorationColMajor
:
1023 case SpvDecorationGLSLShared
:
1024 case SpvDecorationPatch
:
1025 case SpvDecorationRestrict
:
1026 case SpvDecorationAliased
:
1027 case SpvDecorationVolatile
:
1028 case SpvDecorationCoherent
:
1029 case SpvDecorationNonReadable
:
1030 case SpvDecorationUniform
:
1031 /* This is really nice but we have no use for it right now. */
1032 case SpvDecorationCPacked
:
1033 case SpvDecorationSaturatedConversion
:
1034 case SpvDecorationStream
:
1035 case SpvDecorationOffset
:
1036 case SpvDecorationXfbBuffer
:
1037 case SpvDecorationFuncParamAttr
:
1038 case SpvDecorationFPRoundingMode
:
1039 case SpvDecorationFPFastMathMode
:
1040 case SpvDecorationLinkageAttributes
:
1041 case SpvDecorationSpecId
:
1044 unreachable("Unhandled variable decoration");
1048 /* Tries to compute the size of an interface block based on the strides and
1049 * offsets that are provided to us in the SPIR-V source.
1052 vtn_type_block_size(struct vtn_type
*type
)
1054 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
1055 switch (base_type
) {
1056 case GLSL_TYPE_UINT
:
1058 case GLSL_TYPE_FLOAT
:
1059 case GLSL_TYPE_BOOL
:
1060 case GLSL_TYPE_DOUBLE
: {
1061 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
1062 glsl_get_matrix_columns(type
->type
);
1064 assert(type
->stride
> 0);
1065 return type
->stride
* cols
;
1066 } else if (base_type
== GLSL_TYPE_DOUBLE
) {
1067 return glsl_get_vector_elements(type
->type
) * 8;
1069 return glsl_get_vector_elements(type
->type
) * 4;
1073 case GLSL_TYPE_STRUCT
:
1074 case GLSL_TYPE_INTERFACE
: {
1076 unsigned num_fields
= glsl_get_length(type
->type
);
1077 for (unsigned f
= 0; f
< num_fields
; f
++) {
1078 unsigned field_end
= type
->offsets
[f
] +
1079 vtn_type_block_size(type
->members
[f
]);
1080 size
= MAX2(size
, field_end
);
1085 case GLSL_TYPE_ARRAY
:
1086 assert(type
->stride
> 0);
1087 assert(glsl_get_length(type
->type
) > 0);
1088 return type
->stride
* glsl_get_length(type
->type
);
1091 assert(!"Invalid block type");
1097 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
1098 const uint32_t *w
, unsigned count
)
1101 case SpvOpVariable
: {
1102 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
1103 var
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1105 var
->chain
.var
= var
;
1106 var
->chain
.length
= 0;
1108 struct vtn_value
*val
=
1109 vtn_push_value(b
, w
[2], vtn_value_type_access_chain
);
1110 val
->access_chain
= &var
->chain
;
1112 struct vtn_type
*without_array
= var
->type
;
1113 while(glsl_type_is_array(without_array
->type
))
1114 without_array
= without_array
->array_element
;
1116 nir_variable_mode nir_mode
;
1117 switch ((SpvStorageClass
)w
[3]) {
1118 case SpvStorageClassUniform
:
1119 case SpvStorageClassUniformConstant
:
1120 if (without_array
->block
) {
1121 var
->mode
= vtn_variable_mode_ubo
;
1122 b
->shader
->info
.num_ubos
++;
1123 } else if (without_array
->buffer_block
) {
1124 var
->mode
= vtn_variable_mode_ssbo
;
1125 b
->shader
->info
.num_ssbos
++;
1126 } else if (glsl_type_is_image(without_array
->type
)) {
1127 var
->mode
= vtn_variable_mode_image
;
1128 nir_mode
= nir_var_uniform
;
1129 b
->shader
->info
.num_images
++;
1130 } else if (glsl_type_is_sampler(without_array
->type
)) {
1131 var
->mode
= vtn_variable_mode_sampler
;
1132 nir_mode
= nir_var_uniform
;
1133 b
->shader
->info
.num_textures
++;
1135 assert(!"Invalid uniform variable type");
1138 case SpvStorageClassPushConstant
:
1139 var
->mode
= vtn_variable_mode_push_constant
;
1140 assert(b
->shader
->num_uniforms
== 0);
1141 b
->shader
->num_uniforms
= vtn_type_block_size(var
->type
) * 4;
1143 case SpvStorageClassInput
:
1144 var
->mode
= vtn_variable_mode_input
;
1145 nir_mode
= nir_var_shader_in
;
1147 case SpvStorageClassOutput
:
1148 var
->mode
= vtn_variable_mode_output
;
1149 nir_mode
= nir_var_shader_out
;
1151 case SpvStorageClassPrivate
:
1152 var
->mode
= vtn_variable_mode_global
;
1153 nir_mode
= nir_var_global
;
1155 case SpvStorageClassFunction
:
1156 var
->mode
= vtn_variable_mode_local
;
1157 nir_mode
= nir_var_local
;
1159 case SpvStorageClassWorkgroup
:
1160 var
->mode
= vtn_variable_mode_workgroup
;
1161 nir_mode
= nir_var_shared
;
1163 case SpvStorageClassCrossWorkgroup
:
1164 case SpvStorageClassGeneric
:
1165 case SpvStorageClassAtomicCounter
:
1167 unreachable("Unhandled variable storage class");
1170 switch (var
->mode
) {
1171 case vtn_variable_mode_local
:
1172 case vtn_variable_mode_global
:
1173 case vtn_variable_mode_image
:
1174 case vtn_variable_mode_sampler
:
1175 case vtn_variable_mode_workgroup
:
1176 /* For these, we create the variable normally */
1177 var
->var
= rzalloc(b
->shader
, nir_variable
);
1178 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1179 var
->var
->type
= var
->type
->type
;
1180 var
->var
->data
.mode
= nir_mode
;
1182 switch (var
->mode
) {
1183 case vtn_variable_mode_image
:
1184 case vtn_variable_mode_sampler
:
1185 var
->var
->interface_type
= without_array
->type
;
1188 var
->var
->interface_type
= NULL
;
1193 case vtn_variable_mode_input
:
1194 case vtn_variable_mode_output
: {
1195 /* For inputs and outputs, we immediately split structures. This
1196 * is for a couple of reasons. For one, builtins may all come in
1197 * a struct and we really want those split out into separate
1198 * variables. For another, interpolation qualifiers can be
1199 * applied to members of the top-level struct ane we need to be
1200 * able to preserve that information.
1203 int array_length
= -1;
1204 struct vtn_type
*interface_type
= var
->type
;
1205 if (b
->shader
->stage
== MESA_SHADER_GEOMETRY
&&
1206 glsl_type_is_array(var
->type
->type
)) {
1207 /* In Geometry shaders (and some tessellation), inputs come
1208 * in per-vertex arrays. However, some builtins come in
1209 * non-per-vertex, hence the need for the is_array check. In
1210 * any case, there are no non-builtin arrays allowed so this
1211 * check should be sufficient.
1213 interface_type
= var
->type
->array_element
;
1214 array_length
= glsl_get_length(var
->type
->type
);
1217 if (glsl_type_is_struct(interface_type
->type
)) {
1218 /* It's a struct. Split it. */
1219 unsigned num_members
= glsl_get_length(interface_type
->type
);
1220 var
->members
= ralloc_array(b
, nir_variable
*, num_members
);
1222 for (unsigned i
= 0; i
< num_members
; i
++) {
1223 const struct glsl_type
*mtype
= interface_type
->members
[i
]->type
;
1224 if (array_length
>= 0)
1225 mtype
= glsl_array_type(mtype
, array_length
);
1227 var
->members
[i
] = rzalloc(b
->shader
, nir_variable
);
1228 var
->members
[i
]->name
=
1229 ralloc_asprintf(var
->members
[i
], "%s.%d", val
->name
, i
);
1230 var
->members
[i
]->type
= mtype
;
1231 var
->members
[i
]->interface_type
=
1232 interface_type
->members
[i
]->type
;
1233 var
->members
[i
]->data
.mode
= nir_mode
;
1236 var
->var
= rzalloc(b
->shader
, nir_variable
);
1237 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1238 var
->var
->type
= var
->type
->type
;
1239 var
->var
->interface_type
= interface_type
->type
;
1240 var
->var
->data
.mode
= nir_mode
;
1243 /* For inputs and outputs, we need to grab locations and builtin
1244 * information from the interface type.
1246 vtn_foreach_decoration(b
, interface_type
->val
, var_decoration_cb
, var
);
1249 case vtn_variable_mode_param
:
1250 unreachable("Not created through OpVariable");
1253 case vtn_variable_mode_ubo
:
1254 case vtn_variable_mode_ssbo
:
1255 case vtn_variable_mode_push_constant
:
1256 /* These don't need actual variables. */
1262 nir_constant
*constant
=
1263 vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
1264 var
->var
->constant_initializer
=
1265 nir_constant_clone(constant
, var
->var
);
1268 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
1270 if (var
->mode
== vtn_variable_mode_image
||
1271 var
->mode
== vtn_variable_mode_sampler
) {
1272 /* XXX: We still need the binding information in the nir_variable
1273 * for these. We should fix that.
1275 var
->var
->data
.binding
= var
->binding
;
1276 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
1278 if (var
->mode
== vtn_variable_mode_image
)
1279 var
->var
->data
.image
.format
= without_array
->image_format
;
1282 if (var
->mode
== vtn_variable_mode_local
) {
1283 assert(var
->members
== NULL
&& var
->var
!= NULL
);
1284 nir_function_impl_add_variable(b
->impl
, var
->var
);
1285 } else if (var
->var
) {
1286 nir_shader_add_variable(b
->shader
, var
->var
);
1287 } else if (var
->members
) {
1288 unsigned count
= glsl_get_length(without_array
->type
);
1289 for (unsigned i
= 0; i
< count
; i
++) {
1290 assert(var
->members
[i
]->data
.mode
!= nir_var_local
);
1291 nir_shader_add_variable(b
->shader
, var
->members
[i
]);
1294 assert(var
->mode
== vtn_variable_mode_ubo
||
1295 var
->mode
== vtn_variable_mode_ssbo
||
1296 var
->mode
== vtn_variable_mode_push_constant
);
1301 case SpvOpAccessChain
:
1302 case SpvOpInBoundsAccessChain
: {
1303 struct vtn_access_chain
*base
, *chain
;
1304 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
1305 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1306 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1307 * to combine an array of images with a single sampler to get an
1308 * array of sampled images that all share the same sampler.
1309 * Fortunately, this means that we can more-or-less ignore the
1310 * sampler when crawling the access chain, but it does leave us
1311 * with this rather awkward little special-case.
1313 base
= base_val
->sampled_image
->image
;
1315 assert(base_val
->value_type
== vtn_value_type_access_chain
);
1316 base
= base_val
->access_chain
;
1319 chain
= vtn_access_chain_extend(b
, base
, count
- 4);
1321 unsigned idx
= base
->length
;
1322 for (int i
= 4; i
< count
; i
++) {
1323 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
1324 if (link_val
->value_type
== vtn_value_type_constant
) {
1325 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
1326 chain
->link
[idx
].id
= link_val
->constant
->value
.u
[0];
1328 chain
->link
[idx
].mode
= vtn_access_mode_id
;
1329 chain
->link
[idx
].id
= w
[i
];
1334 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1335 struct vtn_value
*val
=
1336 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1337 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1338 val
->sampled_image
->image
= chain
;
1339 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
1341 struct vtn_value
*val
=
1342 vtn_push_value(b
, w
[2], vtn_value_type_access_chain
);
1343 val
->access_chain
= chain
;
1348 case SpvOpCopyMemory
: {
1349 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_access_chain
);
1350 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_access_chain
);
1352 vtn_variable_copy(b
, dest
->access_chain
, src
->access_chain
);
1357 struct vtn_access_chain
*src
=
1358 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1360 if (src
->var
->mode
== vtn_variable_mode_image
||
1361 src
->var
->mode
== vtn_variable_mode_sampler
) {
1362 vtn_push_value(b
, w
[2], vtn_value_type_access_chain
)->access_chain
= src
;
1366 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1367 val
->ssa
= vtn_variable_load(b
, src
);
1372 struct vtn_access_chain
*dest
=
1373 vtn_value(b
, w
[1], vtn_value_type_access_chain
)->access_chain
;
1374 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
1375 vtn_variable_store(b
, src
, dest
);
1379 case SpvOpArrayLength
: {
1380 struct vtn_access_chain
*chain
=
1381 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1383 const uint32_t offset
= chain
->var
->type
->offsets
[w
[4]];
1384 const uint32_t stride
= chain
->var
->type
->members
[w
[4]]->stride
;
1387 struct vtn_type
*type
;
1388 nir_ssa_def
*index
=
1389 get_vulkan_resource_index(b
, chain
, &type
, &chain_idx
);
1391 nir_intrinsic_instr
*instr
=
1392 nir_intrinsic_instr_create(b
->nb
.shader
,
1393 nir_intrinsic_get_buffer_size
);
1394 instr
->src
[0] = nir_src_for_ssa(index
);
1395 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
1396 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
1397 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
1399 /* array_length = max(buffer_size - offset, 0) / stride */
1400 nir_ssa_def
*array_length
=
1405 nir_imm_int(&b
->nb
, offset
)),
1406 nir_imm_int(&b
->nb
, 0u)),
1407 nir_imm_int(&b
->nb
, stride
));
1409 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1410 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
1411 val
->ssa
->def
= array_length
;
1415 case SpvOpCopyMemorySized
:
1417 unreachable("Unhandled opcode");