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
, NULL
);
194 inout
->def
= &intrin
->dest
.ssa
;
196 intrin
->const_index
[0] = (1 << intrin
->num_components
) - 1;
197 intrin
->src
[0] = nir_src_for_ssa(inout
->def
);
200 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
201 } else if (glsl_get_base_type(tail
->type
) == GLSL_TYPE_ARRAY
||
202 glsl_type_is_matrix(tail
->type
)) {
203 unsigned elems
= glsl_get_length(tail
->type
);
204 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
205 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
206 deref_arr
->deref
.type
= glsl_get_array_element(tail
->type
);
207 tail
->child
= &deref_arr
->deref
;
208 for (unsigned i
= 0; i
< elems
; i
++) {
209 deref_arr
->base_offset
= i
;
210 _vtn_local_load_store(b
, load
, deref
, tail
->child
, inout
->elems
[i
]);
213 assert(glsl_get_base_type(tail
->type
) == GLSL_TYPE_STRUCT
);
214 unsigned elems
= glsl_get_length(tail
->type
);
215 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, 0);
216 tail
->child
= &deref_struct
->deref
;
217 for (unsigned i
= 0; i
< elems
; i
++) {
218 deref_struct
->index
= i
;
219 deref_struct
->deref
.type
= glsl_get_struct_field(tail
->type
, i
);
220 _vtn_local_load_store(b
, load
, deref
, tail
->child
, inout
->elems
[i
]);
224 tail
->child
= old_child
;
228 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
230 struct vtn_access_chain
*chain
=
231 vtn_value(b
, id
, vtn_value_type_access_chain
)->access_chain
;
233 return vtn_access_chain_to_deref(b
, chain
);
237 * Gets the NIR-level deref tail, which may have as a child an array deref
238 * selecting which component due to OpAccessChain supporting per-component
239 * indexing in SPIR-V.
242 get_deref_tail(nir_deref_var
*deref
)
244 nir_deref
*cur
= &deref
->deref
;
245 while (!glsl_type_is_vector_or_scalar(cur
->type
) && cur
->child
)
251 struct vtn_ssa_value
*
252 vtn_local_load(struct vtn_builder
*b
, nir_deref_var
*src
)
254 nir_deref
*src_tail
= get_deref_tail(src
);
255 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
256 _vtn_local_load_store(b
, true, src
, src_tail
, val
);
258 if (src_tail
->child
) {
259 nir_deref_array
*vec_deref
= nir_deref_as_array(src_tail
->child
);
260 assert(vec_deref
->deref
.child
== NULL
);
261 val
->type
= vec_deref
->deref
.type
;
262 if (vec_deref
->deref_array_type
== nir_deref_array_type_direct
)
263 val
->def
= vtn_vector_extract(b
, val
->def
, vec_deref
->base_offset
);
265 val
->def
= vtn_vector_extract_dynamic(b
, val
->def
,
266 vec_deref
->indirect
.ssa
);
273 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
276 nir_deref
*dest_tail
= get_deref_tail(dest
);
278 if (dest_tail
->child
) {
279 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
280 _vtn_local_load_store(b
, true, dest
, dest_tail
, val
);
281 nir_deref_array
*deref
= nir_deref_as_array(dest_tail
->child
);
282 assert(deref
->deref
.child
== NULL
);
283 if (deref
->deref_array_type
== nir_deref_array_type_direct
)
284 val
->def
= vtn_vector_insert(b
, val
->def
, src
->def
,
287 val
->def
= vtn_vector_insert_dynamic(b
, val
->def
, src
->def
,
288 deref
->indirect
.ssa
);
289 _vtn_local_load_store(b
, false, dest
, dest_tail
, val
);
291 _vtn_local_load_store(b
, false, dest
, dest_tail
, src
);
296 get_vulkan_resource_index(struct vtn_builder
*b
, struct vtn_access_chain
*chain
,
297 struct vtn_type
**type
, unsigned *chain_idx
)
299 /* Push constants have no explicit binding */
300 if (chain
->var
->mode
== vtn_variable_mode_push_constant
) {
302 *type
= chain
->var
->type
;
306 nir_ssa_def
*array_index
;
307 if (glsl_type_is_array(chain
->var
->type
->type
)) {
308 assert(chain
->length
> 0);
309 array_index
= vtn_access_link_as_ssa(b
, chain
->link
[0], 1);
311 *type
= chain
->var
->type
->array_element
;
313 array_index
= nir_imm_int(&b
->nb
, 0);
315 *type
= chain
->var
->type
;
318 nir_intrinsic_instr
*instr
=
319 nir_intrinsic_instr_create(b
->nb
.shader
,
320 nir_intrinsic_vulkan_resource_index
);
321 instr
->src
[0] = nir_src_for_ssa(array_index
);
322 nir_intrinsic_set_desc_set(instr
, chain
->var
->descriptor_set
);
323 nir_intrinsic_set_binding(instr
, chain
->var
->binding
);
325 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
326 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
328 return &instr
->dest
.ssa
;
332 vtn_access_chain_to_offset(struct vtn_builder
*b
,
333 struct vtn_access_chain
*chain
,
334 nir_ssa_def
**index_out
, struct vtn_type
**type_out
,
335 unsigned *end_idx_out
, bool stop_at_matrix
)
338 struct vtn_type
*type
;
339 *index_out
= get_vulkan_resource_index(b
, chain
, &type
, &idx
);
341 nir_ssa_def
*offset
= nir_imm_int(&b
->nb
, 0);
342 for (; idx
< chain
->length
; idx
++) {
343 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
347 case GLSL_TYPE_FLOAT
:
348 case GLSL_TYPE_DOUBLE
:
350 /* Some users may not want matrix or vector derefs */
355 case GLSL_TYPE_ARRAY
:
356 offset
= nir_iadd(&b
->nb
, offset
,
357 vtn_access_link_as_ssa(b
, chain
->link
[idx
],
360 type
= type
->array_element
;
363 case GLSL_TYPE_STRUCT
: {
364 assert(chain
->link
[idx
].mode
== vtn_access_mode_literal
);
365 unsigned member
= chain
->link
[idx
].id
;
366 offset
= nir_iadd(&b
->nb
, offset
,
367 nir_imm_int(&b
->nb
, type
->offsets
[member
]));
368 type
= type
->members
[member
];
373 unreachable("Invalid type for deref");
386 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
387 nir_ssa_def
*index
, nir_ssa_def
*offset
,
388 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
)
390 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
391 instr
->num_components
= glsl_get_vector_elements(type
);
395 instr
->const_index
[0] = (1 << instr
->num_components
) - 1; /* write mask */
396 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
399 /* We set the base and size for push constant load to the entire push
400 * constant block for now.
402 if (op
== nir_intrinsic_load_push_constant
) {
403 instr
->const_index
[0] = 0;
404 instr
->const_index
[1] = 128;
408 instr
->src
[src
++] = nir_src_for_ssa(index
);
410 instr
->src
[src
++] = nir_src_for_ssa(offset
);
413 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
414 instr
->num_components
, NULL
);
415 (*inout
)->def
= &instr
->dest
.ssa
;
418 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
420 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
421 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
425 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
426 nir_ssa_def
*index
, nir_ssa_def
*offset
,
427 struct vtn_access_chain
*chain
, unsigned chain_idx
,
428 struct vtn_type
*type
, struct vtn_ssa_value
**inout
)
430 if (chain
&& chain_idx
>= chain
->length
)
433 if (load
&& chain
== NULL
&& *inout
== NULL
)
434 *inout
= vtn_create_ssa_value(b
, type
->type
);
436 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
440 case GLSL_TYPE_FLOAT
:
442 /* This is where things get interesting. At this point, we've hit
443 * a vector, a scalar, or a matrix.
445 if (glsl_type_is_matrix(type
->type
)) {
447 /* Loading the whole matrix */
448 struct vtn_ssa_value
*transpose
;
449 unsigned num_ops
, vec_width
;
450 if (type
->row_major
) {
451 num_ops
= glsl_get_vector_elements(type
->type
);
452 vec_width
= glsl_get_matrix_columns(type
->type
);
454 const struct glsl_type
*transpose_type
=
455 glsl_matrix_type(base_type
, vec_width
, num_ops
);
456 *inout
= vtn_create_ssa_value(b
, transpose_type
);
458 transpose
= vtn_ssa_transpose(b
, *inout
);
462 num_ops
= glsl_get_matrix_columns(type
->type
);
463 vec_width
= glsl_get_vector_elements(type
->type
);
466 for (unsigned i
= 0; i
< num_ops
; i
++) {
467 nir_ssa_def
*elem_offset
=
468 nir_iadd(&b
->nb
, offset
,
469 nir_imm_int(&b
->nb
, i
* type
->stride
));
470 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
472 glsl_vector_type(base_type
, vec_width
));
475 if (load
&& type
->row_major
)
476 *inout
= vtn_ssa_transpose(b
, *inout
);
477 } else if (type
->row_major
) {
478 /* Row-major but with an access chiain. */
479 nir_ssa_def
*col_offset
=
480 vtn_access_link_as_ssa(b
, chain
->link
[chain_idx
],
481 type
->array_element
->stride
);
482 offset
= nir_iadd(&b
->nb
, offset
, col_offset
);
484 if (chain_idx
+ 1 < chain
->length
) {
485 /* Picking off a single element */
486 nir_ssa_def
*row_offset
=
487 vtn_access_link_as_ssa(b
, chain
->link
[chain_idx
+ 1],
489 offset
= nir_iadd(&b
->nb
, offset
, row_offset
);
491 *inout
= vtn_create_ssa_value(b
, glsl_scalar_type(base_type
));
492 _vtn_load_store_tail(b
, op
, load
, index
, offset
, inout
,
493 glsl_scalar_type(base_type
));
495 /* Grabbing a column; picking one element off each row */
496 unsigned num_comps
= glsl_get_vector_elements(type
->type
);
497 const struct glsl_type
*column_type
=
498 glsl_get_column_type(type
->type
);
500 nir_ssa_def
*comps
[4];
501 for (unsigned i
= 0; i
< num_comps
; i
++) {
502 nir_ssa_def
*elem_offset
=
503 nir_iadd(&b
->nb
, offset
,
504 nir_imm_int(&b
->nb
, i
* type
->stride
));
506 struct vtn_ssa_value
*comp
, temp_val
;
508 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
509 temp_val
.type
= glsl_scalar_type(base_type
);
512 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
513 &comp
, glsl_scalar_type(base_type
));
514 comps
[i
] = comp
->def
;
519 *inout
= vtn_create_ssa_value(b
, column_type
);
521 (*inout
)->def
= nir_vec(&b
->nb
, comps
, num_comps
);
525 /* Column-major with a deref. Fall through to array case. */
526 nir_ssa_def
*col_offset
=
527 vtn_access_link_as_ssa(b
, chain
->link
[chain_idx
], type
->stride
);
528 offset
= nir_iadd(&b
->nb
, offset
, col_offset
);
530 _vtn_block_load_store(b
, op
, load
, index
, offset
,
531 chain
, chain_idx
+ 1,
532 type
->array_element
, inout
);
534 } else if (chain
== NULL
) {
535 /* Single whole vector */
536 assert(glsl_type_is_vector_or_scalar(type
->type
));
537 _vtn_load_store_tail(b
, op
, load
, index
, offset
, inout
, type
->type
);
539 /* Single component of a vector. Fall through to array case. */
540 nir_ssa_def
*elem_offset
=
541 vtn_access_link_as_ssa(b
, chain
->link
[chain_idx
], type
->stride
);
542 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
544 _vtn_block_load_store(b
, op
, load
, index
, offset
, NULL
, 0,
545 type
->array_element
, inout
);
549 case GLSL_TYPE_ARRAY
: {
550 unsigned elems
= glsl_get_length(type
->type
);
551 for (unsigned i
= 0; i
< elems
; i
++) {
552 nir_ssa_def
*elem_off
=
553 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* type
->stride
));
554 _vtn_block_load_store(b
, op
, load
, index
, elem_off
, NULL
, 0,
555 type
->array_element
, &(*inout
)->elems
[i
]);
560 case GLSL_TYPE_STRUCT
: {
561 unsigned elems
= glsl_get_length(type
->type
);
562 for (unsigned i
= 0; i
< elems
; i
++) {
563 nir_ssa_def
*elem_off
=
564 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, type
->offsets
[i
]));
565 _vtn_block_load_store(b
, op
, load
, index
, elem_off
, NULL
, 0,
566 type
->members
[i
], &(*inout
)->elems
[i
]);
572 unreachable("Invalid block member type");
576 static struct vtn_ssa_value
*
577 vtn_block_load(struct vtn_builder
*b
, struct vtn_access_chain
*src
)
580 switch (src
->var
->mode
) {
581 case vtn_variable_mode_ubo
:
582 op
= nir_intrinsic_load_ubo
;
584 case vtn_variable_mode_ssbo
:
585 op
= nir_intrinsic_load_ssbo
;
587 case vtn_variable_mode_push_constant
:
588 op
= nir_intrinsic_load_push_constant
;
591 assert(!"Invalid block variable mode");
594 nir_ssa_def
*offset
, *index
= NULL
;
595 struct vtn_type
*type
;
597 offset
= vtn_access_chain_to_offset(b
, src
, &index
, &type
, &chain_idx
, true);
599 struct vtn_ssa_value
*value
= NULL
;
600 _vtn_block_load_store(b
, op
, true, index
, offset
,
601 src
, chain_idx
, type
, &value
);
606 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
607 struct vtn_access_chain
*dst
)
609 nir_ssa_def
*offset
, *index
= NULL
;
610 struct vtn_type
*type
;
612 offset
= vtn_access_chain_to_offset(b
, dst
, &index
, &type
, &chain_idx
, true);
614 _vtn_block_load_store(b
, nir_intrinsic_store_ssbo
, false, index
, offset
,
615 dst
, chain_idx
, type
, &src
);
619 vtn_variable_is_external_block(struct vtn_variable
*var
)
621 return var
->mode
== vtn_variable_mode_ssbo
||
622 var
->mode
== vtn_variable_mode_ubo
||
623 var
->mode
== vtn_variable_mode_push_constant
;
627 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
628 struct vtn_access_chain
*chain
,
629 struct vtn_type
*tail_type
,
630 struct vtn_ssa_value
**inout
)
632 enum glsl_base_type base_type
= glsl_get_base_type(tail_type
->type
);
636 case GLSL_TYPE_FLOAT
:
638 /* At this point, we have a scalar, vector, or matrix so we know that
639 * there cannot be any structure splitting still in the way. By
640 * stopping at the matrix level rather than the vector level, we
641 * ensure that matrices get loaded in the optimal way even if they
642 * are storred row-major in a UBO.
645 *inout
= vtn_local_load(b
, vtn_access_chain_to_deref(b
, chain
));
647 vtn_local_store(b
, *inout
, vtn_access_chain_to_deref(b
, chain
));
651 case GLSL_TYPE_ARRAY
:
652 case GLSL_TYPE_STRUCT
: {
653 struct vtn_access_chain
*new_chain
=
654 vtn_access_chain_extend(b
, chain
, 1);
655 new_chain
->link
[chain
->length
].mode
= vtn_access_mode_literal
;
656 unsigned elems
= glsl_get_length(tail_type
->type
);
658 assert(*inout
== NULL
);
659 *inout
= rzalloc(b
, struct vtn_ssa_value
);
660 (*inout
)->type
= tail_type
->type
;
661 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
663 for (unsigned i
= 0; i
< elems
; i
++) {
664 new_chain
->link
[chain
->length
].id
= i
;
665 struct vtn_type
*elem_type
= base_type
== GLSL_TYPE_ARRAY
?
666 tail_type
->array_element
: tail_type
->members
[i
];
667 _vtn_variable_load_store(b
, load
, new_chain
, elem_type
,
668 &(*inout
)->elems
[i
]);
674 unreachable("Invalid access chain type");
678 struct vtn_ssa_value
*
679 vtn_variable_load(struct vtn_builder
*b
, struct vtn_access_chain
*src
)
681 if (vtn_variable_is_external_block(src
->var
)) {
682 return vtn_block_load(b
, src
);
684 struct vtn_type
*tail_type
= vtn_access_chain_tail_type(b
, src
);
685 struct vtn_ssa_value
*val
= NULL
;
686 _vtn_variable_load_store(b
, true, src
, tail_type
, &val
);
692 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
693 struct vtn_access_chain
*dest
)
695 if (vtn_variable_is_external_block(dest
->var
)) {
696 assert(dest
->var
->mode
== vtn_variable_mode_ssbo
);
697 vtn_block_store(b
, src
, dest
);
699 struct vtn_type
*tail_type
= vtn_access_chain_tail_type(b
, dest
);
700 _vtn_variable_load_store(b
, false, dest
, tail_type
, &src
);
705 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_access_chain
*dest
,
706 struct vtn_access_chain
*src
, struct vtn_type
*tail_type
)
708 enum glsl_base_type base_type
= glsl_get_base_type(tail_type
->type
);
712 case GLSL_TYPE_FLOAT
:
714 /* At this point, we have a scalar, vector, or matrix so we know that
715 * there cannot be any structure splitting still in the way. By
716 * stopping at the matrix level rather than the vector level, we
717 * ensure that matrices get loaded in the optimal way even if they
718 * are storred row-major in a UBO.
720 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
723 case GLSL_TYPE_ARRAY
:
724 case GLSL_TYPE_STRUCT
: {
725 struct vtn_access_chain
*new_src
, *new_dest
;
726 new_src
= vtn_access_chain_extend(b
, src
, 1);
727 new_dest
= vtn_access_chain_extend(b
, dest
, 1);
728 new_src
->link
[src
->length
].mode
= vtn_access_mode_literal
;
729 new_dest
->link
[dest
->length
].mode
= vtn_access_mode_literal
;
730 unsigned elems
= glsl_get_length(tail_type
->type
);
731 for (unsigned i
= 0; i
< elems
; i
++) {
732 new_src
->link
[src
->length
].id
= i
;
733 new_dest
->link
[dest
->length
].id
= i
;
734 struct vtn_type
*elem_type
= base_type
== GLSL_TYPE_ARRAY
?
735 tail_type
->array_element
: tail_type
->members
[i
];
736 _vtn_variable_copy(b
, new_dest
, new_src
, elem_type
);
742 unreachable("Invalid access chain type");
747 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_access_chain
*dest
,
748 struct vtn_access_chain
*src
)
750 struct vtn_type
*tail_type
= vtn_access_chain_tail_type(b
, src
);
751 assert(vtn_access_chain_tail_type(b
, dest
)->type
== tail_type
->type
);
753 /* TODO: At some point, we should add a special-case for when we can
754 * just emit a copy_var intrinsic.
756 _vtn_variable_copy(b
, dest
, src
, tail_type
);
760 set_mode_system_value(nir_variable_mode
*mode
)
762 assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
763 *mode
= nir_var_system_value
;
767 vtn_get_builtin_location(struct vtn_builder
*b
,
768 SpvBuiltIn builtin
, int *location
,
769 nir_variable_mode
*mode
)
772 case SpvBuiltInPosition
:
773 *location
= VARYING_SLOT_POS
;
775 case SpvBuiltInPointSize
:
776 *location
= VARYING_SLOT_PSIZ
;
778 case SpvBuiltInClipDistance
:
779 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
781 case SpvBuiltInCullDistance
:
782 /* XXX figure this out */
784 case SpvBuiltInVertexIndex
:
785 *location
= SYSTEM_VALUE_VERTEX_ID
;
786 set_mode_system_value(mode
);
788 case SpvBuiltInVertexId
:
789 /* Vulkan defines VertexID to be zero-based and reserves the new
790 * builtin keyword VertexIndex to indicate the non-zero-based value.
792 *location
= SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
793 set_mode_system_value(mode
);
795 case SpvBuiltInInstanceIndex
:
796 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
797 set_mode_system_value(mode
);
799 case SpvBuiltInInstanceId
:
800 *location
= SYSTEM_VALUE_INSTANCE_ID
;
801 set_mode_system_value(mode
);
803 case SpvBuiltInPrimitiveId
:
804 *location
= VARYING_SLOT_PRIMITIVE_ID
;
805 *mode
= nir_var_shader_out
;
807 case SpvBuiltInInvocationId
:
808 *location
= SYSTEM_VALUE_INVOCATION_ID
;
809 set_mode_system_value(mode
);
811 case SpvBuiltInLayer
:
812 *location
= VARYING_SLOT_LAYER
;
813 *mode
= nir_var_shader_out
;
815 case SpvBuiltInViewportIndex
:
816 *location
= VARYING_SLOT_VIEWPORT
;
817 if (b
->shader
->stage
== MESA_SHADER_GEOMETRY
)
818 *mode
= nir_var_shader_out
;
819 else if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
)
820 *mode
= nir_var_shader_in
;
822 unreachable("invalid stage for SpvBuiltInViewportIndex");
824 case SpvBuiltInTessLevelOuter
:
825 case SpvBuiltInTessLevelInner
:
826 case SpvBuiltInTessCoord
:
827 case SpvBuiltInPatchVertices
:
828 unreachable("no tessellation support");
829 case SpvBuiltInFragCoord
:
830 *location
= VARYING_SLOT_POS
;
831 assert(*mode
== nir_var_shader_in
);
833 case SpvBuiltInPointCoord
:
834 *location
= VARYING_SLOT_PNTC
;
835 assert(*mode
== nir_var_shader_in
);
837 case SpvBuiltInFrontFacing
:
838 *location
= VARYING_SLOT_FACE
;
839 assert(*mode
== nir_var_shader_in
);
841 case SpvBuiltInSampleId
:
842 *location
= SYSTEM_VALUE_SAMPLE_ID
;
843 set_mode_system_value(mode
);
845 case SpvBuiltInSamplePosition
:
846 *location
= SYSTEM_VALUE_SAMPLE_POS
;
847 set_mode_system_value(mode
);
849 case SpvBuiltInSampleMask
:
850 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
; /* XXX out? */
851 set_mode_system_value(mode
);
853 case SpvBuiltInFragDepth
:
854 *location
= FRAG_RESULT_DEPTH
;
855 assert(*mode
== nir_var_shader_out
);
857 case SpvBuiltInNumWorkgroups
:
858 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
859 set_mode_system_value(mode
);
861 case SpvBuiltInWorkgroupSize
:
862 /* This should already be handled */
863 unreachable("unsupported builtin");
865 case SpvBuiltInWorkgroupId
:
866 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
867 set_mode_system_value(mode
);
869 case SpvBuiltInLocalInvocationId
:
870 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
871 set_mode_system_value(mode
);
873 case SpvBuiltInLocalInvocationIndex
:
874 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
875 set_mode_system_value(mode
);
877 case SpvBuiltInGlobalInvocationId
:
878 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
879 set_mode_system_value(mode
);
881 case SpvBuiltInHelperInvocation
:
883 unreachable("unsupported builtin");
888 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
889 const struct vtn_decoration
*dec
, void *void_var
)
891 struct vtn_variable
*vtn_var
= void_var
;
893 /* Handle decorations that apply to a vtn_variable as a whole */
894 switch (dec
->decoration
) {
895 case SpvDecorationBinding
:
896 vtn_var
->binding
= dec
->literals
[0];
898 case SpvDecorationDescriptorSet
:
899 vtn_var
->descriptor_set
= dec
->literals
[0];
902 case SpvDecorationLocation
: {
903 unsigned location
= dec
->literals
[0];
904 bool is_vertex_input
;
905 if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
&&
906 vtn_var
->mode
== vtn_variable_mode_output
) {
907 is_vertex_input
= false;
908 location
+= FRAG_RESULT_DATA0
;
909 } else if (b
->shader
->stage
== MESA_SHADER_VERTEX
&&
910 vtn_var
->mode
== vtn_variable_mode_input
) {
911 is_vertex_input
= true;
912 location
+= VERT_ATTRIB_GENERIC0
;
913 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
914 vtn_var
->mode
== vtn_variable_mode_output
) {
915 is_vertex_input
= false;
916 location
+= VARYING_SLOT_VAR0
;
918 assert(!"Location must be on input or output variable");
922 vtn_var
->var
->data
.location
= location
;
923 vtn_var
->var
->data
.explicit_location
= true;
925 assert(vtn_var
->members
);
926 unsigned length
= glsl_get_length(vtn_var
->type
->type
);
927 for (unsigned i
= 0; i
< length
; i
++) {
928 vtn_var
->members
[i
]->data
.location
= location
;
929 vtn_var
->members
[i
]->data
.explicit_location
= true;
931 glsl_count_attribute_slots(vtn_var
->members
[i
]->interface_type
,
942 /* Now we handle decorations that apply to a particular nir_variable */
943 nir_variable
*nir_var
= vtn_var
->var
;
944 if (val
->value_type
== vtn_value_type_access_chain
) {
945 assert(val
->access_chain
->length
== 0);
946 assert(val
->access_chain
->var
== void_var
);
947 assert(member
== -1);
949 assert(val
->value_type
== vtn_value_type_type
);
951 nir_var
= vtn_var
->members
[member
];
957 switch (dec
->decoration
) {
958 case SpvDecorationRelaxedPrecision
:
959 break; /* FIXME: Do nothing with this for now. */
960 case SpvDecorationNoPerspective
:
961 nir_var
->data
.interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
963 case SpvDecorationFlat
:
964 nir_var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
966 case SpvDecorationCentroid
:
967 nir_var
->data
.centroid
= true;
969 case SpvDecorationSample
:
970 nir_var
->data
.sample
= true;
972 case SpvDecorationInvariant
:
973 nir_var
->data
.invariant
= true;
975 case SpvDecorationConstant
:
976 assert(nir_var
->constant_initializer
!= NULL
);
977 nir_var
->data
.read_only
= true;
979 case SpvDecorationNonWritable
:
980 nir_var
->data
.read_only
= true;
982 case SpvDecorationComponent
:
983 nir_var
->data
.location_frac
= dec
->literals
[0];
985 case SpvDecorationIndex
:
986 nir_var
->data
.explicit_index
= true;
987 nir_var
->data
.index
= dec
->literals
[0];
989 case SpvDecorationBuiltIn
: {
990 SpvBuiltIn builtin
= dec
->literals
[0];
992 if (builtin
== SpvBuiltInWorkgroupSize
) {
993 /* This shouldn't be a builtin. It's actually a constant. */
994 nir_var
->data
.mode
= nir_var_global
;
995 nir_var
->data
.read_only
= true;
997 nir_constant
*c
= rzalloc(nir_var
, nir_constant
);
998 c
->value
.u
[0] = b
->shader
->info
.cs
.local_size
[0];
999 c
->value
.u
[1] = b
->shader
->info
.cs
.local_size
[1];
1000 c
->value
.u
[2] = b
->shader
->info
.cs
.local_size
[2];
1001 nir_var
->constant_initializer
= c
;
1005 nir_variable_mode mode
= nir_var
->data
.mode
;
1006 vtn_get_builtin_location(b
, builtin
, &nir_var
->data
.location
, &mode
);
1007 nir_var
->data
.explicit_location
= true;
1008 nir_var
->data
.mode
= mode
;
1010 if (builtin
== SpvBuiltInFragCoord
|| builtin
== SpvBuiltInSamplePosition
)
1011 nir_var
->data
.origin_upper_left
= b
->origin_upper_left
;
1014 case SpvDecorationRowMajor
:
1015 case SpvDecorationColMajor
:
1016 case SpvDecorationGLSLShared
:
1017 case SpvDecorationPatch
:
1018 case SpvDecorationRestrict
:
1019 case SpvDecorationAliased
:
1020 case SpvDecorationVolatile
:
1021 case SpvDecorationCoherent
:
1022 case SpvDecorationNonReadable
:
1023 case SpvDecorationUniform
:
1024 /* This is really nice but we have no use for it right now. */
1025 case SpvDecorationCPacked
:
1026 case SpvDecorationSaturatedConversion
:
1027 case SpvDecorationStream
:
1028 case SpvDecorationOffset
:
1029 case SpvDecorationXfbBuffer
:
1030 case SpvDecorationFuncParamAttr
:
1031 case SpvDecorationFPRoundingMode
:
1032 case SpvDecorationFPFastMathMode
:
1033 case SpvDecorationLinkageAttributes
:
1034 case SpvDecorationSpecId
:
1037 unreachable("Unhandled variable decoration");
1041 /* Tries to compute the size of an interface block based on the strides and
1042 * offsets that are provided to us in the SPIR-V source.
1045 vtn_type_block_size(struct vtn_type
*type
)
1047 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
1048 switch (base_type
) {
1049 case GLSL_TYPE_UINT
:
1051 case GLSL_TYPE_FLOAT
:
1052 case GLSL_TYPE_BOOL
:
1053 case GLSL_TYPE_DOUBLE
: {
1054 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
1055 glsl_get_matrix_columns(type
->type
);
1057 assert(type
->stride
> 0);
1058 return type
->stride
* cols
;
1059 } else if (base_type
== GLSL_TYPE_DOUBLE
) {
1060 return glsl_get_vector_elements(type
->type
) * 8;
1062 return glsl_get_vector_elements(type
->type
) * 4;
1066 case GLSL_TYPE_STRUCT
:
1067 case GLSL_TYPE_INTERFACE
: {
1069 unsigned num_fields
= glsl_get_length(type
->type
);
1070 for (unsigned f
= 0; f
< num_fields
; f
++) {
1071 unsigned field_end
= type
->offsets
[f
] +
1072 vtn_type_block_size(type
->members
[f
]);
1073 size
= MAX2(size
, field_end
);
1078 case GLSL_TYPE_ARRAY
:
1079 assert(type
->stride
> 0);
1080 assert(glsl_get_length(type
->type
) > 0);
1081 return type
->stride
* glsl_get_length(type
->type
);
1084 assert(!"Invalid block type");
1090 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
1091 const uint32_t *w
, unsigned count
)
1094 case SpvOpVariable
: {
1095 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
1096 var
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1098 var
->chain
.var
= var
;
1099 var
->chain
.length
= 0;
1101 struct vtn_value
*val
=
1102 vtn_push_value(b
, w
[2], vtn_value_type_access_chain
);
1103 val
->access_chain
= &var
->chain
;
1105 struct vtn_type
*without_array
= var
->type
;
1106 while(glsl_type_is_array(without_array
->type
))
1107 without_array
= without_array
->array_element
;
1109 nir_variable_mode nir_mode
;
1110 switch ((SpvStorageClass
)w
[3]) {
1111 case SpvStorageClassUniform
:
1112 case SpvStorageClassUniformConstant
:
1113 if (without_array
->block
) {
1114 var
->mode
= vtn_variable_mode_ubo
;
1115 b
->shader
->info
.num_ubos
++;
1116 } else if (without_array
->buffer_block
) {
1117 var
->mode
= vtn_variable_mode_ssbo
;
1118 b
->shader
->info
.num_ssbos
++;
1119 } else if (glsl_type_is_image(without_array
->type
)) {
1120 var
->mode
= vtn_variable_mode_image
;
1121 nir_mode
= nir_var_uniform
;
1122 b
->shader
->info
.num_images
++;
1123 } else if (glsl_type_is_sampler(without_array
->type
)) {
1124 var
->mode
= vtn_variable_mode_sampler
;
1125 nir_mode
= nir_var_uniform
;
1126 b
->shader
->info
.num_textures
++;
1128 assert(!"Invalid uniform variable type");
1131 case SpvStorageClassPushConstant
:
1132 var
->mode
= vtn_variable_mode_push_constant
;
1133 assert(b
->shader
->num_uniforms
== 0);
1134 b
->shader
->num_uniforms
= vtn_type_block_size(var
->type
) * 4;
1136 case SpvStorageClassInput
:
1137 var
->mode
= vtn_variable_mode_input
;
1138 nir_mode
= nir_var_shader_in
;
1140 case SpvStorageClassOutput
:
1141 var
->mode
= vtn_variable_mode_output
;
1142 nir_mode
= nir_var_shader_out
;
1144 case SpvStorageClassPrivate
:
1145 var
->mode
= vtn_variable_mode_global
;
1146 nir_mode
= nir_var_global
;
1148 case SpvStorageClassFunction
:
1149 var
->mode
= vtn_variable_mode_local
;
1150 nir_mode
= nir_var_local
;
1152 case SpvStorageClassWorkgroup
:
1153 var
->mode
= vtn_variable_mode_workgroup
;
1154 nir_mode
= nir_var_shared
;
1156 case SpvStorageClassCrossWorkgroup
:
1157 case SpvStorageClassGeneric
:
1158 case SpvStorageClassAtomicCounter
:
1160 unreachable("Unhandled variable storage class");
1163 switch (var
->mode
) {
1164 case vtn_variable_mode_local
:
1165 case vtn_variable_mode_global
:
1166 case vtn_variable_mode_image
:
1167 case vtn_variable_mode_sampler
:
1168 case vtn_variable_mode_workgroup
:
1169 /* For these, we create the variable normally */
1170 var
->var
= rzalloc(b
->shader
, nir_variable
);
1171 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1172 var
->var
->type
= var
->type
->type
;
1173 var
->var
->data
.mode
= nir_mode
;
1175 switch (var
->mode
) {
1176 case vtn_variable_mode_image
:
1177 case vtn_variable_mode_sampler
:
1178 var
->var
->interface_type
= without_array
->type
;
1181 var
->var
->interface_type
= NULL
;
1186 case vtn_variable_mode_input
:
1187 case vtn_variable_mode_output
: {
1188 /* For inputs and outputs, we immediately split structures. This
1189 * is for a couple of reasons. For one, builtins may all come in
1190 * a struct and we really want those split out into separate
1191 * variables. For another, interpolation qualifiers can be
1192 * applied to members of the top-level struct ane we need to be
1193 * able to preserve that information.
1196 int array_length
= -1;
1197 struct vtn_type
*interface_type
= var
->type
;
1198 if (b
->shader
->stage
== MESA_SHADER_GEOMETRY
&&
1199 glsl_type_is_array(var
->type
->type
)) {
1200 /* In Geometry shaders (and some tessellation), inputs come
1201 * in per-vertex arrays. However, some builtins come in
1202 * non-per-vertex, hence the need for the is_array check. In
1203 * any case, there are no non-builtin arrays allowed so this
1204 * check should be sufficient.
1206 interface_type
= var
->type
->array_element
;
1207 array_length
= glsl_get_length(var
->type
->type
);
1210 if (glsl_type_is_struct(interface_type
->type
)) {
1211 /* It's a struct. Split it. */
1212 unsigned num_members
= glsl_get_length(interface_type
->type
);
1213 var
->members
= ralloc_array(b
, nir_variable
*, num_members
);
1215 for (unsigned i
= 0; i
< num_members
; i
++) {
1216 const struct glsl_type
*mtype
= interface_type
->members
[i
]->type
;
1217 if (array_length
>= 0)
1218 mtype
= glsl_array_type(mtype
, array_length
);
1220 var
->members
[i
] = rzalloc(b
->shader
, nir_variable
);
1221 var
->members
[i
]->name
=
1222 ralloc_asprintf(var
->members
[i
], "%s.%d", val
->name
, i
);
1223 var
->members
[i
]->type
= mtype
;
1224 var
->members
[i
]->interface_type
=
1225 interface_type
->members
[i
]->type
;
1226 var
->members
[i
]->data
.mode
= nir_mode
;
1229 var
->var
= rzalloc(b
->shader
, nir_variable
);
1230 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1231 var
->var
->type
= var
->type
->type
;
1232 var
->var
->interface_type
= interface_type
->type
;
1233 var
->var
->data
.mode
= nir_mode
;
1236 /* For inputs and outputs, we need to grab locations and builtin
1237 * information from the interface type.
1239 vtn_foreach_decoration(b
, interface_type
->val
, var_decoration_cb
, var
);
1242 case vtn_variable_mode_param
:
1243 unreachable("Not created through OpVariable");
1246 case vtn_variable_mode_ubo
:
1247 case vtn_variable_mode_ssbo
:
1248 case vtn_variable_mode_push_constant
:
1249 /* These don't need actual variables. */
1255 nir_constant
*constant
=
1256 vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
1257 var
->var
->constant_initializer
=
1258 nir_constant_clone(constant
, var
->var
);
1261 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
1263 if (var
->mode
== vtn_variable_mode_image
||
1264 var
->mode
== vtn_variable_mode_sampler
) {
1265 /* XXX: We still need the binding information in the nir_variable
1266 * for these. We should fix that.
1268 var
->var
->data
.binding
= var
->binding
;
1269 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
1271 if (var
->mode
== vtn_variable_mode_image
)
1272 var
->var
->data
.image
.format
= without_array
->image_format
;
1275 if (var
->mode
== vtn_variable_mode_local
) {
1276 assert(var
->members
== NULL
&& var
->var
!= NULL
);
1277 nir_function_impl_add_variable(b
->impl
, var
->var
);
1278 } else if (var
->var
) {
1279 nir_shader_add_variable(b
->shader
, var
->var
);
1280 } else if (var
->members
) {
1281 unsigned count
= glsl_get_length(without_array
->type
);
1282 for (unsigned i
= 0; i
< count
; i
++) {
1283 assert(var
->members
[i
]->data
.mode
!= nir_var_local
);
1284 nir_shader_add_variable(b
->shader
, var
->members
[i
]);
1287 assert(var
->mode
== vtn_variable_mode_ubo
||
1288 var
->mode
== vtn_variable_mode_ssbo
||
1289 var
->mode
== vtn_variable_mode_push_constant
);
1294 case SpvOpAccessChain
:
1295 case SpvOpInBoundsAccessChain
: {
1296 struct vtn_access_chain
*base
, *chain
;
1297 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
1298 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1299 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1300 * to combine an array of images with a single sampler to get an
1301 * array of sampled images that all share the same sampler.
1302 * Fortunately, this means that we can more-or-less ignore the
1303 * sampler when crawling the access chain, but it does leave us
1304 * with this rather awkward little special-case.
1306 base
= base_val
->sampled_image
->image
;
1308 assert(base_val
->value_type
== vtn_value_type_access_chain
);
1309 base
= base_val
->access_chain
;
1312 chain
= vtn_access_chain_extend(b
, base
, count
- 4);
1314 unsigned idx
= base
->length
;
1315 for (int i
= 4; i
< count
; i
++) {
1316 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
1317 if (link_val
->value_type
== vtn_value_type_constant
) {
1318 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
1319 chain
->link
[idx
].id
= link_val
->constant
->value
.u
[0];
1321 chain
->link
[idx
].mode
= vtn_access_mode_id
;
1322 chain
->link
[idx
].id
= w
[i
];
1327 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1328 struct vtn_value
*val
=
1329 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1330 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1331 val
->sampled_image
->image
= chain
;
1332 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
1334 struct vtn_value
*val
=
1335 vtn_push_value(b
, w
[2], vtn_value_type_access_chain
);
1336 val
->access_chain
= chain
;
1341 case SpvOpCopyMemory
: {
1342 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_access_chain
);
1343 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_access_chain
);
1345 vtn_variable_copy(b
, dest
->access_chain
, src
->access_chain
);
1350 struct vtn_access_chain
*src
=
1351 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1353 if (src
->var
->mode
== vtn_variable_mode_image
||
1354 src
->var
->mode
== vtn_variable_mode_sampler
) {
1355 vtn_push_value(b
, w
[2], vtn_value_type_access_chain
)->access_chain
= src
;
1359 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1360 val
->ssa
= vtn_variable_load(b
, src
);
1365 struct vtn_access_chain
*dest
=
1366 vtn_value(b
, w
[1], vtn_value_type_access_chain
)->access_chain
;
1367 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
1368 vtn_variable_store(b
, src
, dest
);
1372 case SpvOpArrayLength
: {
1373 struct vtn_access_chain
*chain
=
1374 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1376 const uint32_t offset
= chain
->var
->type
->offsets
[w
[4]];
1377 const uint32_t stride
= chain
->var
->type
->members
[w
[4]]->stride
;
1380 struct vtn_type
*type
;
1381 nir_ssa_def
*index
=
1382 get_vulkan_resource_index(b
, chain
, &type
, &chain_idx
);
1384 nir_intrinsic_instr
*instr
=
1385 nir_intrinsic_instr_create(b
->nb
.shader
,
1386 nir_intrinsic_get_buffer_size
);
1387 instr
->src
[0] = nir_src_for_ssa(index
);
1388 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, NULL
);
1389 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
1390 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
1392 /* array_length = max(buffer_size - offset, 0) / stride */
1393 nir_ssa_def
*array_length
=
1398 nir_imm_int(&b
->nb
, offset
)),
1399 nir_imm_int(&b
->nb
, 0u)),
1400 nir_imm_int(&b
->nb
, stride
));
1402 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1403 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
1404 val
->ssa
->def
= array_length
;
1408 case SpvOpCopyMemorySized
:
1410 unreachable("Unhandled opcode");