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"
29 #include "spirv_info.h"
30 #include "nir_deref.h"
32 static struct vtn_access_chain
*
33 vtn_access_chain_create(struct vtn_builder
*b
, unsigned length
)
35 struct vtn_access_chain
*chain
;
37 /* Subtract 1 from the length since there's already one built in */
38 size_t size
= sizeof(*chain
) +
39 (MAX2(length
, 1) - 1) * sizeof(chain
->link
[0]);
40 chain
= rzalloc_size(b
, size
);
41 chain
->length
= length
;
46 static struct vtn_access_chain
*
47 vtn_access_chain_extend(struct vtn_builder
*b
, struct vtn_access_chain
*old
,
50 struct vtn_access_chain
*chain
;
52 unsigned old_len
= old
? old
->length
: 0;
53 chain
= vtn_access_chain_create(b
, old_len
+ new_ids
);
55 for (unsigned i
= 0; i
< old_len
; i
++)
56 chain
->link
[i
] = old
->link
[i
];
62 vtn_pointer_uses_ssa_offset(struct vtn_builder
*b
,
63 struct vtn_pointer
*ptr
)
65 return ptr
->mode
== vtn_variable_mode_ubo
||
66 ptr
->mode
== vtn_variable_mode_ssbo
||
67 ptr
->mode
== vtn_variable_mode_push_constant
||
68 (ptr
->mode
== vtn_variable_mode_workgroup
&&
69 b
->options
->lower_workgroup_access_to_offsets
);
73 vtn_pointer_is_external_block(struct vtn_builder
*b
,
74 struct vtn_pointer
*ptr
)
76 return ptr
->mode
== vtn_variable_mode_ssbo
||
77 ptr
->mode
== vtn_variable_mode_ubo
||
78 ptr
->mode
== vtn_variable_mode_push_constant
||
79 (ptr
->mode
== vtn_variable_mode_workgroup
&&
80 b
->options
->lower_workgroup_access_to_offsets
);
83 /* Dereference the given base pointer by the access chain */
84 static struct vtn_pointer
*
85 vtn_access_chain_pointer_dereference(struct vtn_builder
*b
,
86 struct vtn_pointer
*base
,
87 struct vtn_access_chain
*deref_chain
)
89 struct vtn_access_chain
*chain
=
90 vtn_access_chain_extend(b
, base
->chain
, deref_chain
->length
);
91 struct vtn_type
*type
= base
->type
;
93 /* OpPtrAccessChain is only allowed on things which support variable
94 * pointers. For everything else, the client is expected to just pass us
95 * the right access chain.
97 vtn_assert(!deref_chain
->ptr_as_array
);
99 unsigned start
= base
->chain
? base
->chain
->length
: 0;
100 for (unsigned i
= 0; i
< deref_chain
->length
; i
++) {
101 chain
->link
[start
+ i
] = deref_chain
->link
[i
];
103 if (glsl_type_is_struct(type
->type
)) {
104 vtn_assert(deref_chain
->link
[i
].mode
== vtn_access_mode_literal
);
105 type
= type
->members
[deref_chain
->link
[i
].id
];
107 type
= type
->array_element
;
111 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
112 ptr
->mode
= base
->mode
;
114 ptr
->var
= base
->var
;
115 ptr
->deref
= base
->deref
;
122 vtn_access_link_as_ssa(struct vtn_builder
*b
, struct vtn_access_link link
,
125 vtn_assert(stride
> 0);
126 if (link
.mode
== vtn_access_mode_literal
) {
127 return nir_imm_int(&b
->nb
, link
.id
* stride
);
128 } else if (stride
== 1) {
129 nir_ssa_def
*ssa
= vtn_ssa_value(b
, link
.id
)->def
;
130 if (ssa
->bit_size
!= 32)
131 ssa
= nir_u2u32(&b
->nb
, ssa
);
134 nir_ssa_def
*src0
= vtn_ssa_value(b
, link
.id
)->def
;
135 if (src0
->bit_size
!= 32)
136 src0
= nir_u2u32(&b
->nb
, src0
);
137 return nir_imul(&b
->nb
, src0
, nir_imm_int(&b
->nb
, stride
));
142 vtn_variable_resource_index(struct vtn_builder
*b
, struct vtn_variable
*var
,
143 nir_ssa_def
*desc_array_index
)
145 if (!desc_array_index
) {
146 vtn_assert(glsl_type_is_struct(var
->type
->type
));
147 desc_array_index
= nir_imm_int(&b
->nb
, 0);
150 nir_intrinsic_instr
*instr
=
151 nir_intrinsic_instr_create(b
->nb
.shader
,
152 nir_intrinsic_vulkan_resource_index
);
153 instr
->src
[0] = nir_src_for_ssa(desc_array_index
);
154 nir_intrinsic_set_desc_set(instr
, var
->descriptor_set
);
155 nir_intrinsic_set_binding(instr
, var
->binding
);
157 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
158 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
160 return &instr
->dest
.ssa
;
164 vtn_resource_reindex(struct vtn_builder
*b
, nir_ssa_def
*base_index
,
165 nir_ssa_def
*offset_index
)
167 nir_intrinsic_instr
*instr
=
168 nir_intrinsic_instr_create(b
->nb
.shader
,
169 nir_intrinsic_vulkan_resource_reindex
);
170 instr
->src
[0] = nir_src_for_ssa(base_index
);
171 instr
->src
[1] = nir_src_for_ssa(offset_index
);
173 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
174 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
176 return &instr
->dest
.ssa
;
179 static struct vtn_pointer
*
180 vtn_ssa_offset_pointer_dereference(struct vtn_builder
*b
,
181 struct vtn_pointer
*base
,
182 struct vtn_access_chain
*deref_chain
)
184 nir_ssa_def
*block_index
= base
->block_index
;
185 nir_ssa_def
*offset
= base
->offset
;
186 struct vtn_type
*type
= base
->type
;
189 if (base
->mode
== vtn_variable_mode_ubo
||
190 base
->mode
== vtn_variable_mode_ssbo
) {
192 vtn_assert(base
->var
&& base
->type
);
193 nir_ssa_def
*desc_arr_idx
;
194 if (glsl_type_is_array(type
->type
)) {
195 if (deref_chain
->length
>= 1) {
197 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1);
199 /* This consumes a level of type */
200 type
= type
->array_element
;
202 /* This is annoying. We've been asked for a pointer to the
203 * array of UBOs/SSBOs and not a specifc buffer. Return a
204 * pointer with a descriptor index of 0 and we'll have to do
205 * a reindex later to adjust it to the right thing.
207 desc_arr_idx
= nir_imm_int(&b
->nb
, 0);
209 } else if (deref_chain
->ptr_as_array
) {
210 /* You can't have a zero-length OpPtrAccessChain */
211 vtn_assert(deref_chain
->length
>= 1);
212 desc_arr_idx
= vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1);
214 /* We have a regular non-array SSBO. */
217 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
218 } else if (deref_chain
->ptr_as_array
&&
219 type
->base_type
== vtn_base_type_struct
&& type
->block
) {
220 /* We are doing an OpPtrAccessChain on a pointer to a struct that is
221 * decorated block. This is an interesting corner in the SPIR-V
222 * spec. One interpretation would be that they client is clearly
223 * trying to treat that block as if it's an implicit array of blocks
224 * repeated in the buffer. However, the SPIR-V spec for the
225 * OpPtrAccessChain says:
227 * "Base is treated as the address of the first element of an
228 * array, and the Element element’s address is computed to be the
229 * base for the Indexes, as per OpAccessChain."
231 * Taken literally, that would mean that your struct type is supposed
232 * to be treated as an array of such a struct and, since it's
233 * decorated block, that means an array of blocks which corresponds
234 * to an array descriptor. Therefore, we need to do a reindex
235 * operation to add the index from the first link in the access chain
236 * to the index we recieved.
238 * The downside to this interpretation (there always is one) is that
239 * this might be somewhat surprising behavior to apps if they expect
240 * the implicit array behavior described above.
242 vtn_assert(deref_chain
->length
>= 1);
243 nir_ssa_def
*offset_index
=
244 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1);
247 block_index
= vtn_resource_reindex(b
, block_index
, offset_index
);
252 if (base
->mode
== vtn_variable_mode_workgroup
) {
253 /* SLM doesn't need nor have a block index */
254 vtn_assert(!block_index
);
256 /* We need the variable for the base offset */
257 vtn_assert(base
->var
);
259 /* We need ptr_type for size and alignment */
260 vtn_assert(base
->ptr_type
);
262 /* Assign location on first use so that we don't end up bloating SLM
263 * address space for variables which are never statically used.
265 if (base
->var
->shared_location
< 0) {
266 vtn_assert(base
->ptr_type
->length
> 0 && base
->ptr_type
->align
> 0);
267 b
->shader
->num_shared
= vtn_align_u32(b
->shader
->num_shared
,
268 base
->ptr_type
->align
);
269 base
->var
->shared_location
= b
->shader
->num_shared
;
270 b
->shader
->num_shared
+= base
->ptr_type
->length
;
273 offset
= nir_imm_int(&b
->nb
, base
->var
->shared_location
);
274 } else if (base
->mode
== vtn_variable_mode_push_constant
) {
275 /* Push constants neither need nor have a block index */
276 vtn_assert(!block_index
);
278 /* Start off with at the start of the push constant block. */
279 offset
= nir_imm_int(&b
->nb
, 0);
281 /* The code above should have ensured a block_index when needed. */
282 vtn_assert(block_index
);
284 /* Start off with at the start of the buffer. */
285 offset
= nir_imm_int(&b
->nb
, 0);
289 if (deref_chain
->ptr_as_array
&& idx
== 0) {
290 /* We need ptr_type for the stride */
291 vtn_assert(base
->ptr_type
);
293 /* We need at least one element in the chain */
294 vtn_assert(deref_chain
->length
>= 1);
296 nir_ssa_def
*elem_offset
=
297 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
298 base
->ptr_type
->stride
);
299 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
303 for (; idx
< deref_chain
->length
; idx
++) {
304 switch (glsl_get_base_type(type
->type
)) {
307 case GLSL_TYPE_UINT16
:
308 case GLSL_TYPE_INT16
:
309 case GLSL_TYPE_UINT8
:
311 case GLSL_TYPE_UINT64
:
312 case GLSL_TYPE_INT64
:
313 case GLSL_TYPE_FLOAT
:
314 case GLSL_TYPE_FLOAT16
:
315 case GLSL_TYPE_DOUBLE
:
317 case GLSL_TYPE_ARRAY
: {
318 nir_ssa_def
*elem_offset
=
319 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
], type
->stride
);
320 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
321 type
= type
->array_element
;
325 case GLSL_TYPE_STRUCT
: {
326 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
327 unsigned member
= deref_chain
->link
[idx
].id
;
328 nir_ssa_def
*mem_offset
= nir_imm_int(&b
->nb
, type
->offsets
[member
]);
329 offset
= nir_iadd(&b
->nb
, offset
, mem_offset
);
330 type
= type
->members
[member
];
335 vtn_fail("Invalid type for deref");
339 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
340 ptr
->mode
= base
->mode
;
342 ptr
->block_index
= block_index
;
343 ptr
->offset
= offset
;
348 /* Dereference the given base pointer by the access chain */
349 static struct vtn_pointer
*
350 vtn_pointer_dereference(struct vtn_builder
*b
,
351 struct vtn_pointer
*base
,
352 struct vtn_access_chain
*deref_chain
)
354 if (vtn_pointer_uses_ssa_offset(b
, base
)) {
355 return vtn_ssa_offset_pointer_dereference(b
, base
, deref_chain
);
357 return vtn_access_chain_pointer_dereference(b
, base
, deref_chain
);
362 vtn_pointer_for_variable(struct vtn_builder
*b
,
363 struct vtn_variable
*var
, struct vtn_type
*ptr_type
)
365 struct vtn_pointer
*pointer
= rzalloc(b
, struct vtn_pointer
);
367 pointer
->mode
= var
->mode
;
368 pointer
->type
= var
->type
;
369 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
370 vtn_assert(ptr_type
->deref
->type
== var
->type
->type
);
371 pointer
->ptr_type
= ptr_type
;
378 vtn_pointer_to_deref(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
380 /* Do on-the-fly copy propagation for samplers. */
381 if (ptr
->var
&& ptr
->var
->copy_prop_sampler
)
382 return vtn_pointer_to_deref(b
, ptr
->var
->copy_prop_sampler
);
384 nir_deref_instr
*tail
;
388 assert(ptr
->var
&& ptr
->var
->var
);
389 tail
= nir_build_deref_var(&b
->nb
, ptr
->var
->var
);
392 /* Raw variable access */
396 struct vtn_access_chain
*chain
= ptr
->chain
;
399 for (unsigned i
= 0; i
< chain
->length
; i
++) {
400 if (glsl_type_is_struct(tail
->type
)) {
401 vtn_assert(chain
->link
[i
].mode
== vtn_access_mode_literal
);
402 unsigned idx
= chain
->link
[i
].id
;
403 tail
= nir_build_deref_struct(&b
->nb
, tail
, idx
);
406 if (chain
->link
[i
].mode
== vtn_access_mode_literal
) {
407 index
= nir_imm_int(&b
->nb
, chain
->link
[i
].id
);
409 vtn_assert(chain
->link
[i
].mode
== vtn_access_mode_id
);
410 index
= vtn_ssa_value(b
, chain
->link
[i
].id
)->def
;
412 tail
= nir_build_deref_array(&b
->nb
, tail
, index
);
420 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_instr
*deref
,
421 struct vtn_ssa_value
*inout
)
423 if (glsl_type_is_vector_or_scalar(deref
->type
)) {
425 inout
->def
= nir_load_deref(&b
->nb
, deref
);
427 nir_store_deref(&b
->nb
, deref
, inout
->def
, ~0);
429 } else if (glsl_type_is_array(deref
->type
) ||
430 glsl_type_is_matrix(deref
->type
)) {
431 unsigned elems
= glsl_get_length(deref
->type
);
432 for (unsigned i
= 0; i
< elems
; i
++) {
433 nir_deref_instr
*child
=
434 nir_build_deref_array(&b
->nb
, deref
, nir_imm_int(&b
->nb
, i
));
435 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
]);
438 vtn_assert(glsl_type_is_struct(deref
->type
));
439 unsigned elems
= glsl_get_length(deref
->type
);
440 for (unsigned i
= 0; i
< elems
; i
++) {
441 nir_deref_instr
*child
= nir_build_deref_struct(&b
->nb
, deref
, i
);
442 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
]);
448 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
450 struct vtn_pointer
*ptr
= vtn_value(b
, id
, vtn_value_type_pointer
)->pointer
;
451 return vtn_pointer_to_deref(b
, ptr
);
455 * Gets the NIR-level deref tail, which may have as a child an array deref
456 * selecting which component due to OpAccessChain supporting per-component
457 * indexing in SPIR-V.
459 static nir_deref_instr
*
460 get_deref_tail(nir_deref_instr
*deref
)
462 if (deref
->deref_type
!= nir_deref_type_array
)
465 nir_deref_instr
*parent
=
466 nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
);
468 if (glsl_type_is_vector(parent
->type
))
474 struct vtn_ssa_value
*
475 vtn_local_load(struct vtn_builder
*b
, nir_deref_instr
*src
)
477 nir_deref_instr
*src_tail
= get_deref_tail(src
);
478 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
479 _vtn_local_load_store(b
, true, src_tail
, val
);
481 if (src_tail
!= src
) {
482 val
->type
= src
->type
;
483 nir_const_value
*const_index
= nir_src_as_const_value(src
->arr
.index
);
485 val
->def
= vtn_vector_extract(b
, val
->def
, const_index
->u32
[0]);
487 val
->def
= vtn_vector_extract_dynamic(b
, val
->def
, src
->arr
.index
.ssa
);
494 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
495 nir_deref_instr
*dest
)
497 nir_deref_instr
*dest_tail
= get_deref_tail(dest
);
499 if (dest_tail
!= dest
) {
500 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
501 _vtn_local_load_store(b
, true, dest_tail
, val
);
503 nir_const_value
*const_index
= nir_src_as_const_value(dest
->arr
.index
);
505 val
->def
= vtn_vector_insert(b
, val
->def
, src
->def
,
506 const_index
->u32
[0]);
508 val
->def
= vtn_vector_insert_dynamic(b
, val
->def
, src
->def
,
509 dest
->arr
.index
.ssa
);
510 _vtn_local_load_store(b
, false, dest_tail
, val
);
512 _vtn_local_load_store(b
, false, dest_tail
, src
);
517 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
518 nir_ssa_def
**index_out
)
520 assert(vtn_pointer_uses_ssa_offset(b
, ptr
));
522 struct vtn_access_chain chain
= {
525 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
527 *index_out
= ptr
->block_index
;
531 /* Tries to compute the size of an interface block based on the strides and
532 * offsets that are provided to us in the SPIR-V source.
535 vtn_type_block_size(struct vtn_builder
*b
, struct vtn_type
*type
)
537 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
541 case GLSL_TYPE_UINT16
:
542 case GLSL_TYPE_INT16
:
543 case GLSL_TYPE_UINT8
:
545 case GLSL_TYPE_UINT64
:
546 case GLSL_TYPE_INT64
:
547 case GLSL_TYPE_FLOAT
:
548 case GLSL_TYPE_FLOAT16
:
550 case GLSL_TYPE_DOUBLE
: {
551 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
552 glsl_get_matrix_columns(type
->type
);
554 vtn_assert(type
->stride
> 0);
555 return type
->stride
* cols
;
557 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
558 return glsl_get_vector_elements(type
->type
) * type_size
;
562 case GLSL_TYPE_STRUCT
:
563 case GLSL_TYPE_INTERFACE
: {
565 unsigned num_fields
= glsl_get_length(type
->type
);
566 for (unsigned f
= 0; f
< num_fields
; f
++) {
567 unsigned field_end
= type
->offsets
[f
] +
568 vtn_type_block_size(b
, type
->members
[f
]);
569 size
= MAX2(size
, field_end
);
574 case GLSL_TYPE_ARRAY
:
575 vtn_assert(type
->stride
> 0);
576 vtn_assert(glsl_get_length(type
->type
) > 0);
577 return type
->stride
* glsl_get_length(type
->type
);
580 vtn_fail("Invalid block type");
586 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
587 nir_ssa_def
*index
, nir_ssa_def
*offset
,
588 unsigned access_offset
, unsigned access_size
,
589 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
)
591 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
592 instr
->num_components
= glsl_get_vector_elements(type
);
596 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
597 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
600 if (op
== nir_intrinsic_load_push_constant
) {
601 nir_intrinsic_set_base(instr
, access_offset
);
602 nir_intrinsic_set_range(instr
, access_size
);
606 instr
->src
[src
++] = nir_src_for_ssa(index
);
608 if (op
== nir_intrinsic_load_push_constant
) {
609 /* We need to subtract the offset from where the intrinsic will load the
612 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
613 nir_imm_int(&b
->nb
, access_offset
)));
615 instr
->src
[src
++] = nir_src_for_ssa(offset
);
619 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
620 instr
->num_components
,
621 glsl_get_bit_size(type
), NULL
);
622 (*inout
)->def
= &instr
->dest
.ssa
;
625 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
627 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
628 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
632 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
633 nir_ssa_def
*index
, nir_ssa_def
*offset
,
634 unsigned access_offset
, unsigned access_size
,
635 struct vtn_type
*type
, struct vtn_ssa_value
**inout
)
637 if (load
&& *inout
== NULL
)
638 *inout
= vtn_create_ssa_value(b
, type
->type
);
640 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
644 case GLSL_TYPE_UINT16
:
645 case GLSL_TYPE_INT16
:
646 case GLSL_TYPE_UINT8
:
648 case GLSL_TYPE_UINT64
:
649 case GLSL_TYPE_INT64
:
650 case GLSL_TYPE_FLOAT
:
651 case GLSL_TYPE_FLOAT16
:
652 case GLSL_TYPE_DOUBLE
:
654 /* This is where things get interesting. At this point, we've hit
655 * a vector, a scalar, or a matrix.
657 if (glsl_type_is_matrix(type
->type
)) {
658 /* Loading the whole matrix */
659 struct vtn_ssa_value
*transpose
;
660 unsigned num_ops
, vec_width
, col_stride
;
661 if (type
->row_major
) {
662 num_ops
= glsl_get_vector_elements(type
->type
);
663 vec_width
= glsl_get_matrix_columns(type
->type
);
664 col_stride
= type
->array_element
->stride
;
666 const struct glsl_type
*transpose_type
=
667 glsl_matrix_type(base_type
, vec_width
, num_ops
);
668 *inout
= vtn_create_ssa_value(b
, transpose_type
);
670 transpose
= vtn_ssa_transpose(b
, *inout
);
674 num_ops
= glsl_get_matrix_columns(type
->type
);
675 vec_width
= glsl_get_vector_elements(type
->type
);
676 col_stride
= type
->stride
;
679 for (unsigned i
= 0; i
< num_ops
; i
++) {
680 nir_ssa_def
*elem_offset
=
681 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* col_stride
));
682 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
683 access_offset
, access_size
,
685 glsl_vector_type(base_type
, vec_width
));
688 if (load
&& type
->row_major
)
689 *inout
= vtn_ssa_transpose(b
, *inout
);
691 unsigned elems
= glsl_get_vector_elements(type
->type
);
692 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
693 if (elems
== 1 || type
->stride
== type_size
) {
694 /* This is a tightly-packed normal scalar or vector load */
695 vtn_assert(glsl_type_is_vector_or_scalar(type
->type
));
696 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
697 access_offset
, access_size
,
700 /* This is a strided load. We have to load N things separately.
701 * This is the single column of a row-major matrix case.
703 vtn_assert(type
->stride
> type_size
);
704 vtn_assert(type
->stride
% type_size
== 0);
706 nir_ssa_def
*per_comp
[4];
707 for (unsigned i
= 0; i
< elems
; i
++) {
708 nir_ssa_def
*elem_offset
=
709 nir_iadd(&b
->nb
, offset
,
710 nir_imm_int(&b
->nb
, i
* type
->stride
));
711 struct vtn_ssa_value
*comp
, temp_val
;
713 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
714 temp_val
.type
= glsl_scalar_type(base_type
);
717 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
718 access_offset
, access_size
,
719 &comp
, glsl_scalar_type(base_type
));
720 per_comp
[i
] = comp
->def
;
725 *inout
= vtn_create_ssa_value(b
, type
->type
);
726 (*inout
)->def
= nir_vec(&b
->nb
, per_comp
, elems
);
732 case GLSL_TYPE_ARRAY
: {
733 unsigned elems
= glsl_get_length(type
->type
);
734 for (unsigned i
= 0; i
< elems
; i
++) {
735 nir_ssa_def
*elem_off
=
736 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* type
->stride
));
737 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
738 access_offset
, access_size
,
739 type
->array_element
, &(*inout
)->elems
[i
]);
744 case GLSL_TYPE_STRUCT
: {
745 unsigned elems
= glsl_get_length(type
->type
);
746 for (unsigned i
= 0; i
< elems
; i
++) {
747 nir_ssa_def
*elem_off
=
748 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, type
->offsets
[i
]));
749 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
750 access_offset
, access_size
,
751 type
->members
[i
], &(*inout
)->elems
[i
]);
757 vtn_fail("Invalid block member type");
761 static struct vtn_ssa_value
*
762 vtn_block_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
765 unsigned access_offset
= 0, access_size
= 0;
767 case vtn_variable_mode_ubo
:
768 op
= nir_intrinsic_load_ubo
;
770 case vtn_variable_mode_ssbo
:
771 op
= nir_intrinsic_load_ssbo
;
773 case vtn_variable_mode_push_constant
:
774 op
= nir_intrinsic_load_push_constant
;
775 access_size
= b
->shader
->num_uniforms
;
777 case vtn_variable_mode_workgroup
:
778 op
= nir_intrinsic_load_shared
;
781 vtn_fail("Invalid block variable mode");
784 nir_ssa_def
*offset
, *index
= NULL
;
785 offset
= vtn_pointer_to_offset(b
, src
, &index
);
787 struct vtn_ssa_value
*value
= NULL
;
788 _vtn_block_load_store(b
, op
, true, index
, offset
,
789 access_offset
, access_size
,
795 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
796 struct vtn_pointer
*dst
)
800 case vtn_variable_mode_ssbo
:
801 op
= nir_intrinsic_store_ssbo
;
803 case vtn_variable_mode_workgroup
:
804 op
= nir_intrinsic_store_shared
;
807 vtn_fail("Invalid block variable mode");
810 nir_ssa_def
*offset
, *index
= NULL
;
811 offset
= vtn_pointer_to_offset(b
, dst
, &index
);
813 _vtn_block_load_store(b
, op
, false, index
, offset
,
814 0, 0, dst
->type
, &src
);
818 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
819 struct vtn_pointer
*ptr
,
820 struct vtn_ssa_value
**inout
)
822 enum glsl_base_type base_type
= glsl_get_base_type(ptr
->type
->type
);
826 case GLSL_TYPE_UINT16
:
827 case GLSL_TYPE_INT16
:
828 case GLSL_TYPE_UINT8
:
830 case GLSL_TYPE_UINT64
:
831 case GLSL_TYPE_INT64
:
832 case GLSL_TYPE_FLOAT
:
833 case GLSL_TYPE_FLOAT16
:
835 case GLSL_TYPE_DOUBLE
:
836 /* At this point, we have a scalar, vector, or matrix so we know that
837 * there cannot be any structure splitting still in the way. By
838 * stopping at the matrix level rather than the vector level, we
839 * ensure that matrices get loaded in the optimal way even if they
840 * are storred row-major in a UBO.
843 *inout
= vtn_local_load(b
, vtn_pointer_to_deref(b
, ptr
));
845 vtn_local_store(b
, *inout
, vtn_pointer_to_deref(b
, ptr
));
849 case GLSL_TYPE_ARRAY
:
850 case GLSL_TYPE_STRUCT
: {
851 unsigned elems
= glsl_get_length(ptr
->type
->type
);
853 vtn_assert(*inout
== NULL
);
854 *inout
= rzalloc(b
, struct vtn_ssa_value
);
855 (*inout
)->type
= ptr
->type
->type
;
856 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
859 struct vtn_access_chain chain
= {
862 { .mode
= vtn_access_mode_literal
, },
865 for (unsigned i
= 0; i
< elems
; i
++) {
866 chain
.link
[0].id
= i
;
867 struct vtn_pointer
*elem
= vtn_pointer_dereference(b
, ptr
, &chain
);
868 _vtn_variable_load_store(b
, load
, elem
, &(*inout
)->elems
[i
]);
874 vtn_fail("Invalid access chain type");
878 struct vtn_ssa_value
*
879 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
881 if (vtn_pointer_is_external_block(b
, src
)) {
882 return vtn_block_load(b
, src
);
884 struct vtn_ssa_value
*val
= NULL
;
885 _vtn_variable_load_store(b
, true, src
, &val
);
891 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
892 struct vtn_pointer
*dest
)
894 if (vtn_pointer_is_external_block(b
, dest
)) {
895 vtn_assert(dest
->mode
== vtn_variable_mode_ssbo
||
896 dest
->mode
== vtn_variable_mode_workgroup
);
897 vtn_block_store(b
, src
, dest
);
899 _vtn_variable_load_store(b
, false, dest
, &src
);
904 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
905 struct vtn_pointer
*src
)
907 vtn_assert(src
->type
->type
== dest
->type
->type
);
908 enum glsl_base_type base_type
= glsl_get_base_type(src
->type
->type
);
912 case GLSL_TYPE_UINT16
:
913 case GLSL_TYPE_INT16
:
914 case GLSL_TYPE_UINT8
:
916 case GLSL_TYPE_UINT64
:
917 case GLSL_TYPE_INT64
:
918 case GLSL_TYPE_FLOAT
:
919 case GLSL_TYPE_FLOAT16
:
920 case GLSL_TYPE_DOUBLE
:
922 /* At this point, we have a scalar, vector, or matrix so we know that
923 * there cannot be any structure splitting still in the way. By
924 * stopping at the matrix level rather than the vector level, we
925 * ensure that matrices get loaded in the optimal way even if they
926 * are storred row-major in a UBO.
928 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
931 case GLSL_TYPE_ARRAY
:
932 case GLSL_TYPE_STRUCT
: {
933 struct vtn_access_chain chain
= {
936 { .mode
= vtn_access_mode_literal
, },
939 unsigned elems
= glsl_get_length(src
->type
->type
);
940 for (unsigned i
= 0; i
< elems
; i
++) {
941 chain
.link
[0].id
= i
;
942 struct vtn_pointer
*src_elem
=
943 vtn_pointer_dereference(b
, src
, &chain
);
944 struct vtn_pointer
*dest_elem
=
945 vtn_pointer_dereference(b
, dest
, &chain
);
947 _vtn_variable_copy(b
, dest_elem
, src_elem
);
953 vtn_fail("Invalid access chain type");
958 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
959 struct vtn_pointer
*src
)
961 /* TODO: At some point, we should add a special-case for when we can
962 * just emit a copy_var intrinsic.
964 _vtn_variable_copy(b
, dest
, src
);
968 set_mode_system_value(struct vtn_builder
*b
, nir_variable_mode
*mode
)
970 vtn_assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
971 *mode
= nir_var_system_value
;
975 vtn_get_builtin_location(struct vtn_builder
*b
,
976 SpvBuiltIn builtin
, int *location
,
977 nir_variable_mode
*mode
)
980 case SpvBuiltInPosition
:
981 *location
= VARYING_SLOT_POS
;
983 case SpvBuiltInPointSize
:
984 *location
= VARYING_SLOT_PSIZ
;
986 case SpvBuiltInClipDistance
:
987 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
989 case SpvBuiltInCullDistance
:
990 *location
= VARYING_SLOT_CULL_DIST0
;
992 case SpvBuiltInVertexIndex
:
993 *location
= SYSTEM_VALUE_VERTEX_ID
;
994 set_mode_system_value(b
, mode
);
996 case SpvBuiltInVertexId
:
997 /* Vulkan defines VertexID to be zero-based and reserves the new
998 * builtin keyword VertexIndex to indicate the non-zero-based value.
1000 *location
= SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1001 set_mode_system_value(b
, mode
);
1003 case SpvBuiltInInstanceIndex
:
1004 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
1005 set_mode_system_value(b
, mode
);
1007 case SpvBuiltInInstanceId
:
1008 *location
= SYSTEM_VALUE_INSTANCE_ID
;
1009 set_mode_system_value(b
, mode
);
1011 case SpvBuiltInPrimitiveId
:
1012 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1013 vtn_assert(*mode
== nir_var_shader_in
);
1014 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1015 } else if (*mode
== nir_var_shader_out
) {
1016 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1018 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
1019 set_mode_system_value(b
, mode
);
1022 case SpvBuiltInInvocationId
:
1023 *location
= SYSTEM_VALUE_INVOCATION_ID
;
1024 set_mode_system_value(b
, mode
);
1026 case SpvBuiltInLayer
:
1027 *location
= VARYING_SLOT_LAYER
;
1028 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1029 *mode
= nir_var_shader_in
;
1030 else if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1031 *mode
= nir_var_shader_out
;
1032 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1033 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1034 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1035 *mode
= nir_var_shader_out
;
1037 vtn_fail("invalid stage for SpvBuiltInLayer");
1039 case SpvBuiltInViewportIndex
:
1040 *location
= VARYING_SLOT_VIEWPORT
;
1041 if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1042 *mode
= nir_var_shader_out
;
1043 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1044 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1045 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1046 *mode
= nir_var_shader_out
;
1047 else if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1048 *mode
= nir_var_shader_in
;
1050 vtn_fail("invalid stage for SpvBuiltInViewportIndex");
1052 case SpvBuiltInTessLevelOuter
:
1053 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
1055 case SpvBuiltInTessLevelInner
:
1056 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
1058 case SpvBuiltInTessCoord
:
1059 *location
= SYSTEM_VALUE_TESS_COORD
;
1060 set_mode_system_value(b
, mode
);
1062 case SpvBuiltInPatchVertices
:
1063 *location
= SYSTEM_VALUE_VERTICES_IN
;
1064 set_mode_system_value(b
, mode
);
1066 case SpvBuiltInFragCoord
:
1067 *location
= VARYING_SLOT_POS
;
1068 vtn_assert(*mode
== nir_var_shader_in
);
1070 case SpvBuiltInPointCoord
:
1071 *location
= VARYING_SLOT_PNTC
;
1072 vtn_assert(*mode
== nir_var_shader_in
);
1074 case SpvBuiltInFrontFacing
:
1075 *location
= SYSTEM_VALUE_FRONT_FACE
;
1076 set_mode_system_value(b
, mode
);
1078 case SpvBuiltInSampleId
:
1079 *location
= SYSTEM_VALUE_SAMPLE_ID
;
1080 set_mode_system_value(b
, mode
);
1082 case SpvBuiltInSamplePosition
:
1083 *location
= SYSTEM_VALUE_SAMPLE_POS
;
1084 set_mode_system_value(b
, mode
);
1086 case SpvBuiltInSampleMask
:
1087 if (*mode
== nir_var_shader_out
) {
1088 *location
= FRAG_RESULT_SAMPLE_MASK
;
1090 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1091 set_mode_system_value(b
, mode
);
1094 case SpvBuiltInFragDepth
:
1095 *location
= FRAG_RESULT_DEPTH
;
1096 vtn_assert(*mode
== nir_var_shader_out
);
1098 case SpvBuiltInHelperInvocation
:
1099 *location
= SYSTEM_VALUE_HELPER_INVOCATION
;
1100 set_mode_system_value(b
, mode
);
1102 case SpvBuiltInNumWorkgroups
:
1103 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1104 set_mode_system_value(b
, mode
);
1106 case SpvBuiltInWorkgroupSize
:
1107 *location
= SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
1108 set_mode_system_value(b
, mode
);
1110 case SpvBuiltInWorkgroupId
:
1111 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1112 set_mode_system_value(b
, mode
);
1114 case SpvBuiltInLocalInvocationId
:
1115 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1116 set_mode_system_value(b
, mode
);
1118 case SpvBuiltInLocalInvocationIndex
:
1119 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1120 set_mode_system_value(b
, mode
);
1122 case SpvBuiltInGlobalInvocationId
:
1123 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1124 set_mode_system_value(b
, mode
);
1126 case SpvBuiltInBaseVertex
:
1127 /* OpenGL gl_BaseVertex (SYSTEM_VALUE_BASE_VERTEX) is not the same
1128 * semantic as SPIR-V BaseVertex (SYSTEM_VALUE_FIRST_VERTEX).
1130 *location
= SYSTEM_VALUE_FIRST_VERTEX
;
1131 set_mode_system_value(b
, mode
);
1133 case SpvBuiltInBaseInstance
:
1134 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1135 set_mode_system_value(b
, mode
);
1137 case SpvBuiltInDrawIndex
:
1138 *location
= SYSTEM_VALUE_DRAW_ID
;
1139 set_mode_system_value(b
, mode
);
1141 case SpvBuiltInSubgroupSize
:
1142 *location
= SYSTEM_VALUE_SUBGROUP_SIZE
;
1143 set_mode_system_value(b
, mode
);
1145 case SpvBuiltInSubgroupId
:
1146 *location
= SYSTEM_VALUE_SUBGROUP_ID
;
1147 set_mode_system_value(b
, mode
);
1149 case SpvBuiltInSubgroupLocalInvocationId
:
1150 *location
= SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1151 set_mode_system_value(b
, mode
);
1153 case SpvBuiltInNumSubgroups
:
1154 *location
= SYSTEM_VALUE_NUM_SUBGROUPS
;
1155 set_mode_system_value(b
, mode
);
1157 case SpvBuiltInDeviceIndex
:
1158 *location
= SYSTEM_VALUE_DEVICE_INDEX
;
1159 set_mode_system_value(b
, mode
);
1161 case SpvBuiltInViewIndex
:
1162 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1163 set_mode_system_value(b
, mode
);
1165 case SpvBuiltInSubgroupEqMask
:
1166 *location
= SYSTEM_VALUE_SUBGROUP_EQ_MASK
,
1167 set_mode_system_value(b
, mode
);
1169 case SpvBuiltInSubgroupGeMask
:
1170 *location
= SYSTEM_VALUE_SUBGROUP_GE_MASK
,
1171 set_mode_system_value(b
, mode
);
1173 case SpvBuiltInSubgroupGtMask
:
1174 *location
= SYSTEM_VALUE_SUBGROUP_GT_MASK
,
1175 set_mode_system_value(b
, mode
);
1177 case SpvBuiltInSubgroupLeMask
:
1178 *location
= SYSTEM_VALUE_SUBGROUP_LE_MASK
,
1179 set_mode_system_value(b
, mode
);
1181 case SpvBuiltInSubgroupLtMask
:
1182 *location
= SYSTEM_VALUE_SUBGROUP_LT_MASK
,
1183 set_mode_system_value(b
, mode
);
1185 case SpvBuiltInFragStencilRefEXT
:
1186 *location
= FRAG_RESULT_STENCIL
;
1187 vtn_assert(*mode
== nir_var_shader_out
);
1190 vtn_fail("unsupported builtin");
1195 apply_var_decoration(struct vtn_builder
*b
,
1196 struct nir_variable_data
*var_data
,
1197 const struct vtn_decoration
*dec
)
1199 switch (dec
->decoration
) {
1200 case SpvDecorationRelaxedPrecision
:
1201 break; /* FIXME: Do nothing with this for now. */
1202 case SpvDecorationNoPerspective
:
1203 var_data
->interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1205 case SpvDecorationFlat
:
1206 var_data
->interpolation
= INTERP_MODE_FLAT
;
1208 case SpvDecorationCentroid
:
1209 var_data
->centroid
= true;
1211 case SpvDecorationSample
:
1212 var_data
->sample
= true;
1214 case SpvDecorationInvariant
:
1215 var_data
->invariant
= true;
1217 case SpvDecorationConstant
:
1218 var_data
->read_only
= true;
1220 case SpvDecorationNonReadable
:
1221 var_data
->image
.write_only
= true;
1223 case SpvDecorationNonWritable
:
1224 var_data
->read_only
= true;
1225 var_data
->image
.read_only
= true;
1227 case SpvDecorationRestrict
:
1228 var_data
->image
.restrict_flag
= true;
1230 case SpvDecorationVolatile
:
1231 var_data
->image
._volatile
= true;
1233 case SpvDecorationCoherent
:
1234 var_data
->image
.coherent
= true;
1236 case SpvDecorationComponent
:
1237 var_data
->location_frac
= dec
->literals
[0];
1239 case SpvDecorationIndex
:
1240 var_data
->index
= dec
->literals
[0];
1242 case SpvDecorationBuiltIn
: {
1243 SpvBuiltIn builtin
= dec
->literals
[0];
1245 nir_variable_mode mode
= var_data
->mode
;
1246 vtn_get_builtin_location(b
, builtin
, &var_data
->location
, &mode
);
1247 var_data
->mode
= mode
;
1250 case SpvBuiltInTessLevelOuter
:
1251 case SpvBuiltInTessLevelInner
:
1252 var_data
->compact
= true;
1254 case SpvBuiltInFragCoord
:
1255 var_data
->pixel_center_integer
= b
->pixel_center_integer
;
1257 case SpvBuiltInSamplePosition
:
1258 var_data
->origin_upper_left
= b
->origin_upper_left
;
1265 case SpvDecorationSpecId
:
1266 case SpvDecorationRowMajor
:
1267 case SpvDecorationColMajor
:
1268 case SpvDecorationMatrixStride
:
1269 case SpvDecorationAliased
:
1270 case SpvDecorationUniform
:
1271 case SpvDecorationStream
:
1272 case SpvDecorationOffset
:
1273 case SpvDecorationLinkageAttributes
:
1274 break; /* Do nothing with these here */
1276 case SpvDecorationPatch
:
1277 var_data
->patch
= true;
1280 case SpvDecorationLocation
:
1281 vtn_fail("Handled above");
1283 case SpvDecorationBlock
:
1284 case SpvDecorationBufferBlock
:
1285 case SpvDecorationArrayStride
:
1286 case SpvDecorationGLSLShared
:
1287 case SpvDecorationGLSLPacked
:
1288 break; /* These can apply to a type but we don't care about them */
1290 case SpvDecorationBinding
:
1291 case SpvDecorationDescriptorSet
:
1292 case SpvDecorationNoContraction
:
1293 case SpvDecorationInputAttachmentIndex
:
1294 vtn_warn("Decoration not allowed for variable or structure member: %s",
1295 spirv_decoration_to_string(dec
->decoration
));
1298 case SpvDecorationXfbBuffer
:
1299 case SpvDecorationXfbStride
:
1300 vtn_warn("Vulkan does not have transform feedback: %s",
1301 spirv_decoration_to_string(dec
->decoration
));
1304 case SpvDecorationCPacked
:
1305 case SpvDecorationSaturatedConversion
:
1306 case SpvDecorationFuncParamAttr
:
1307 case SpvDecorationFPRoundingMode
:
1308 case SpvDecorationFPFastMathMode
:
1309 case SpvDecorationAlignment
:
1310 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1311 spirv_decoration_to_string(dec
->decoration
));
1315 vtn_fail("Unhandled decoration");
1320 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1321 const struct vtn_decoration
*dec
, void *out_is_patch
)
1323 if (dec
->decoration
== SpvDecorationPatch
) {
1324 *((bool *) out_is_patch
) = true;
1329 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1330 const struct vtn_decoration
*dec
, void *void_var
)
1332 struct vtn_variable
*vtn_var
= void_var
;
1334 /* Handle decorations that apply to a vtn_variable as a whole */
1335 switch (dec
->decoration
) {
1336 case SpvDecorationBinding
:
1337 vtn_var
->binding
= dec
->literals
[0];
1338 vtn_var
->explicit_binding
= true;
1340 case SpvDecorationDescriptorSet
:
1341 vtn_var
->descriptor_set
= dec
->literals
[0];
1343 case SpvDecorationInputAttachmentIndex
:
1344 vtn_var
->input_attachment_index
= dec
->literals
[0];
1346 case SpvDecorationPatch
:
1347 vtn_var
->patch
= true;
1349 case SpvDecorationOffset
:
1350 vtn_var
->offset
= dec
->literals
[0];
1356 if (val
->value_type
== vtn_value_type_pointer
) {
1357 assert(val
->pointer
->var
== void_var
);
1358 assert(val
->pointer
->chain
== NULL
);
1359 assert(member
== -1);
1361 assert(val
->value_type
== vtn_value_type_type
);
1364 /* Location is odd. If applied to a split structure, we have to walk the
1365 * whole thing and accumulate the location. It's easier to handle as a
1368 if (dec
->decoration
== SpvDecorationLocation
) {
1369 unsigned location
= dec
->literals
[0];
1370 bool is_vertex_input
;
1371 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
&&
1372 vtn_var
->mode
== vtn_variable_mode_output
) {
1373 is_vertex_input
= false;
1374 location
+= FRAG_RESULT_DATA0
;
1375 } else if (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
&&
1376 vtn_var
->mode
== vtn_variable_mode_input
) {
1377 is_vertex_input
= true;
1378 location
+= VERT_ATTRIB_GENERIC0
;
1379 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1380 vtn_var
->mode
== vtn_variable_mode_output
) {
1381 is_vertex_input
= false;
1382 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1383 } else if (vtn_var
->mode
!= vtn_variable_mode_uniform
) {
1384 vtn_warn("Location must be on input, output, uniform, sampler or "
1389 if (vtn_var
->var
->num_members
== 0) {
1390 /* This handles the member and lone variable cases */
1391 vtn_var
->var
->data
.location
= location
;
1393 /* This handles the structure member case */
1394 assert(vtn_var
->var
->members
);
1395 for (unsigned i
= 0; i
< vtn_var
->var
->num_members
; i
++) {
1396 vtn_var
->var
->members
[i
].location
= location
;
1397 const struct glsl_type
*member_type
=
1398 glsl_get_struct_field(vtn_var
->var
->interface_type
, i
);
1399 location
+= glsl_count_attribute_slots(member_type
,
1406 if (vtn_var
->var
->num_members
== 0) {
1407 assert(member
== -1);
1408 apply_var_decoration(b
, &vtn_var
->var
->data
, dec
);
1409 } else if (member
>= 0) {
1410 /* Member decorations must come from a type */
1411 assert(val
->value_type
== vtn_value_type_type
);
1412 apply_var_decoration(b
, &vtn_var
->var
->members
[member
], dec
);
1415 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1416 for (unsigned i
= 0; i
< length
; i
++)
1417 apply_var_decoration(b
, &vtn_var
->var
->members
[i
], dec
);
1420 /* A few variables, those with external storage, have no actual
1421 * nir_variables associated with them. Fortunately, all decorations
1422 * we care about for those variables are on the type only.
1424 vtn_assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1425 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1426 vtn_var
->mode
== vtn_variable_mode_push_constant
||
1427 (vtn_var
->mode
== vtn_variable_mode_workgroup
&&
1428 b
->options
->lower_workgroup_access_to_offsets
));
1433 static enum vtn_variable_mode
1434 vtn_storage_class_to_mode(struct vtn_builder
*b
,
1435 SpvStorageClass
class,
1436 struct vtn_type
*interface_type
,
1437 nir_variable_mode
*nir_mode_out
)
1439 enum vtn_variable_mode mode
;
1440 nir_variable_mode nir_mode
;
1442 case SpvStorageClassUniform
:
1443 if (interface_type
->block
) {
1444 mode
= vtn_variable_mode_ubo
;
1446 } else if (interface_type
->buffer_block
) {
1447 mode
= vtn_variable_mode_ssbo
;
1450 /* Default-block uniforms, coming from gl_spirv */
1451 mode
= vtn_variable_mode_uniform
;
1452 nir_mode
= nir_var_uniform
;
1455 case SpvStorageClassStorageBuffer
:
1456 mode
= vtn_variable_mode_ssbo
;
1459 case SpvStorageClassUniformConstant
:
1460 mode
= vtn_variable_mode_uniform
;
1461 nir_mode
= nir_var_uniform
;
1463 case SpvStorageClassPushConstant
:
1464 mode
= vtn_variable_mode_push_constant
;
1465 nir_mode
= nir_var_uniform
;
1467 case SpvStorageClassInput
:
1468 mode
= vtn_variable_mode_input
;
1469 nir_mode
= nir_var_shader_in
;
1471 case SpvStorageClassOutput
:
1472 mode
= vtn_variable_mode_output
;
1473 nir_mode
= nir_var_shader_out
;
1475 case SpvStorageClassPrivate
:
1476 mode
= vtn_variable_mode_global
;
1477 nir_mode
= nir_var_global
;
1479 case SpvStorageClassFunction
:
1480 mode
= vtn_variable_mode_local
;
1481 nir_mode
= nir_var_local
;
1483 case SpvStorageClassWorkgroup
:
1484 mode
= vtn_variable_mode_workgroup
;
1485 nir_mode
= nir_var_shared
;
1487 case SpvStorageClassAtomicCounter
:
1488 mode
= vtn_variable_mode_uniform
;
1489 nir_mode
= nir_var_uniform
;
1491 case SpvStorageClassCrossWorkgroup
:
1492 case SpvStorageClassGeneric
:
1494 vtn_fail("Unhandled variable storage class");
1498 *nir_mode_out
= nir_mode
;
1504 vtn_pointer_to_ssa(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
1506 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
1507 /* This pointer needs to have a pointer type with actual storage */
1508 vtn_assert(ptr
->ptr_type
);
1509 vtn_assert(ptr
->ptr_type
->type
);
1512 /* If we don't have an offset then we must be a pointer to the variable
1515 vtn_assert(!ptr
->offset
&& !ptr
->block_index
);
1517 struct vtn_access_chain chain
= {
1520 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
1523 vtn_assert(ptr
->offset
);
1524 if (ptr
->block_index
) {
1525 vtn_assert(ptr
->mode
== vtn_variable_mode_ubo
||
1526 ptr
->mode
== vtn_variable_mode_ssbo
);
1527 return nir_vec2(&b
->nb
, ptr
->block_index
, ptr
->offset
);
1529 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
);
1533 return &vtn_pointer_to_deref(b
, ptr
)->dest
.ssa
;
1537 struct vtn_pointer
*
1538 vtn_pointer_from_ssa(struct vtn_builder
*b
, nir_ssa_def
*ssa
,
1539 struct vtn_type
*ptr_type
)
1541 vtn_assert(ssa
->num_components
<= 2 && ssa
->bit_size
== 32);
1542 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1544 struct vtn_type
*interface_type
= ptr_type
->deref
;
1545 while (interface_type
->base_type
== vtn_base_type_array
)
1546 interface_type
= interface_type
->array_element
;
1548 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
1549 nir_variable_mode nir_mode
;
1550 ptr
->mode
= vtn_storage_class_to_mode(b
, ptr_type
->storage_class
,
1551 interface_type
, &nir_mode
);
1552 ptr
->type
= ptr_type
->deref
;
1553 ptr
->ptr_type
= ptr_type
;
1555 if (ptr
->mode
== vtn_variable_mode_ubo
||
1556 ptr
->mode
== vtn_variable_mode_ssbo
) {
1557 /* This pointer type needs to have actual storage */
1558 vtn_assert(ptr_type
->type
);
1559 vtn_assert(ssa
->num_components
== 2);
1560 ptr
->block_index
= nir_channel(&b
->nb
, ssa
, 0);
1561 ptr
->offset
= nir_channel(&b
->nb
, ssa
, 1);
1562 } else if (ptr
->mode
== vtn_variable_mode_workgroup
||
1563 ptr
->mode
== vtn_variable_mode_push_constant
) {
1564 /* This pointer type needs to have actual storage */
1565 vtn_assert(ptr_type
->type
);
1566 vtn_assert(ssa
->num_components
== 1);
1567 ptr
->block_index
= NULL
;
1570 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
1571 ptr_type
->deref
->type
);
1578 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
1580 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
1583 if (var
->mode
== vtn_variable_mode_input
) {
1584 return stage
== MESA_SHADER_TESS_CTRL
||
1585 stage
== MESA_SHADER_TESS_EVAL
||
1586 stage
== MESA_SHADER_GEOMETRY
;
1589 if (var
->mode
== vtn_variable_mode_output
)
1590 return stage
== MESA_SHADER_TESS_CTRL
;
1596 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
1597 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
1598 nir_constant
*initializer
)
1600 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1601 struct vtn_type
*type
= ptr_type
->deref
;
1603 struct vtn_type
*without_array
= type
;
1604 while(glsl_type_is_array(without_array
->type
))
1605 without_array
= without_array
->array_element
;
1607 enum vtn_variable_mode mode
;
1608 nir_variable_mode nir_mode
;
1609 mode
= vtn_storage_class_to_mode(b
, storage_class
, without_array
, &nir_mode
);
1612 case vtn_variable_mode_ubo
:
1613 b
->shader
->info
.num_ubos
++;
1615 case vtn_variable_mode_ssbo
:
1616 b
->shader
->info
.num_ssbos
++;
1618 case vtn_variable_mode_uniform
:
1619 if (glsl_type_is_image(without_array
->type
))
1620 b
->shader
->info
.num_images
++;
1621 else if (glsl_type_is_sampler(without_array
->type
))
1622 b
->shader
->info
.num_textures
++;
1624 case vtn_variable_mode_push_constant
:
1625 b
->shader
->num_uniforms
= vtn_type_block_size(b
, type
);
1628 /* No tallying is needed */
1632 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
1636 vtn_assert(val
->value_type
== vtn_value_type_pointer
);
1637 val
->pointer
= vtn_pointer_for_variable(b
, var
, ptr_type
);
1639 switch (var
->mode
) {
1640 case vtn_variable_mode_local
:
1641 case vtn_variable_mode_global
:
1642 case vtn_variable_mode_uniform
:
1643 /* For these, we create the variable normally */
1644 var
->var
= rzalloc(b
->shader
, nir_variable
);
1645 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1646 var
->var
->type
= var
->type
->type
;
1647 var
->var
->data
.mode
= nir_mode
;
1648 var
->var
->data
.location
= -1;
1649 var
->var
->interface_type
= NULL
;
1652 case vtn_variable_mode_workgroup
:
1653 if (b
->options
->lower_workgroup_access_to_offsets
) {
1654 var
->shared_location
= -1;
1656 /* Create the variable normally */
1657 var
->var
= rzalloc(b
->shader
, nir_variable
);
1658 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1659 var
->var
->type
= var
->type
->type
;
1660 var
->var
->data
.mode
= nir_var_shared
;
1664 case vtn_variable_mode_input
:
1665 case vtn_variable_mode_output
: {
1666 /* In order to know whether or not we're a per-vertex inout, we need
1667 * the patch qualifier. This means walking the variable decorations
1668 * early before we actually create any variables. Not a big deal.
1670 * GLSLang really likes to place decorations in the most interior
1671 * thing it possibly can. In particular, if you have a struct, it
1672 * will place the patch decorations on the struct members. This
1673 * should be handled by the variable splitting below just fine.
1675 * If you have an array-of-struct, things get even more weird as it
1676 * will place the patch decorations on the struct even though it's
1677 * inside an array and some of the members being patch and others not
1678 * makes no sense whatsoever. Since the only sensible thing is for
1679 * it to be all or nothing, we'll call it patch if any of the members
1680 * are declared patch.
1683 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
1684 if (glsl_type_is_array(var
->type
->type
) &&
1685 glsl_type_is_struct(without_array
->type
)) {
1686 vtn_foreach_decoration(b
, vtn_value(b
, without_array
->id
,
1687 vtn_value_type_type
),
1688 var_is_patch_cb
, &var
->patch
);
1691 /* For inputs and outputs, we immediately split structures. This
1692 * is for a couple of reasons. For one, builtins may all come in
1693 * a struct and we really want those split out into separate
1694 * variables. For another, interpolation qualifiers can be
1695 * applied to members of the top-level struct ane we need to be
1696 * able to preserve that information.
1699 struct vtn_type
*interface_type
= var
->type
;
1700 if (is_per_vertex_inout(var
, b
->shader
->info
.stage
)) {
1701 /* In Geometry shaders (and some tessellation), inputs come
1702 * in per-vertex arrays. However, some builtins come in
1703 * non-per-vertex, hence the need for the is_array check. In
1704 * any case, there are no non-builtin arrays allowed so this
1705 * check should be sufficient.
1707 interface_type
= var
->type
->array_element
;
1710 var
->var
= rzalloc(b
->shader
, nir_variable
);
1711 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1712 var
->var
->type
= var
->type
->type
;
1713 var
->var
->interface_type
= interface_type
->type
;
1714 var
->var
->data
.mode
= nir_mode
;
1715 var
->var
->data
.patch
= var
->patch
;
1717 if (glsl_type_is_struct(interface_type
->type
)) {
1718 /* It's a struct. Set it up as per-member. */
1719 var
->var
->num_members
= glsl_get_length(interface_type
->type
);
1720 var
->var
->members
= rzalloc_array(var
->var
, struct nir_variable_data
,
1721 var
->var
->num_members
);
1723 for (unsigned i
= 0; i
< var
->var
->num_members
; i
++) {
1724 var
->var
->members
[i
].mode
= nir_mode
;
1725 var
->var
->members
[i
].patch
= var
->patch
;
1729 /* For inputs and outputs, we need to grab locations and builtin
1730 * information from the interface type.
1732 vtn_foreach_decoration(b
, vtn_value(b
, interface_type
->id
,
1733 vtn_value_type_type
),
1734 var_decoration_cb
, var
);
1738 case vtn_variable_mode_ubo
:
1739 case vtn_variable_mode_ssbo
:
1740 case vtn_variable_mode_push_constant
:
1741 /* These don't need actual variables. */
1746 var
->var
->constant_initializer
=
1747 nir_constant_clone(initializer
, var
->var
);
1750 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
1752 if (var
->mode
== vtn_variable_mode_uniform
) {
1753 /* XXX: We still need the binding information in the nir_variable
1754 * for these. We should fix that.
1756 var
->var
->data
.binding
= var
->binding
;
1757 var
->var
->data
.explicit_binding
= var
->explicit_binding
;
1758 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
1759 var
->var
->data
.index
= var
->input_attachment_index
;
1761 if (glsl_type_is_image(without_array
->type
))
1762 var
->var
->data
.image
.format
= without_array
->image_format
;
1765 if (var
->mode
== vtn_variable_mode_local
) {
1766 vtn_assert(var
->var
!= NULL
&& var
->var
->members
== NULL
);
1767 nir_function_impl_add_variable(b
->nb
.impl
, var
->var
);
1768 } else if (var
->var
) {
1769 nir_shader_add_variable(b
->shader
, var
->var
);
1771 vtn_assert(vtn_pointer_is_external_block(b
, val
->pointer
));
1776 vtn_assert_types_equal(struct vtn_builder
*b
, SpvOp opcode
,
1777 struct vtn_type
*dst_type
,
1778 struct vtn_type
*src_type
)
1780 if (dst_type
->id
== src_type
->id
)
1783 if (vtn_types_compatible(b
, dst_type
, src_type
)) {
1784 /* Early versions of GLSLang would re-emit types unnecessarily and you
1785 * would end up with OpLoad, OpStore, or OpCopyMemory opcodes which have
1786 * mismatched source and destination types.
1788 * https://github.com/KhronosGroup/glslang/issues/304
1789 * https://github.com/KhronosGroup/glslang/issues/307
1790 * https://bugs.freedesktop.org/show_bug.cgi?id=104338
1791 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
1793 vtn_warn("Source and destination types of %s do not have the same "
1794 "ID (but are compatible): %u vs %u",
1795 spirv_op_to_string(opcode
), dst_type
->id
, src_type
->id
);
1799 vtn_fail("Source and destination types of %s do not match: %s vs. %s",
1800 spirv_op_to_string(opcode
),
1801 glsl_get_type_name(dst_type
->type
),
1802 glsl_get_type_name(src_type
->type
));
1806 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
1807 const uint32_t *w
, unsigned count
)
1811 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1812 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1816 case SpvOpVariable
: {
1817 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1819 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1821 SpvStorageClass storage_class
= w
[3];
1822 nir_constant
*initializer
= NULL
;
1824 initializer
= vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
1826 vtn_create_variable(b
, val
, ptr_type
, storage_class
, initializer
);
1830 case SpvOpAccessChain
:
1831 case SpvOpPtrAccessChain
:
1832 case SpvOpInBoundsAccessChain
: {
1833 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
1834 chain
->ptr_as_array
= (opcode
== SpvOpPtrAccessChain
);
1837 for (int i
= 4; i
< count
; i
++) {
1838 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
1839 if (link_val
->value_type
== vtn_value_type_constant
) {
1840 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
1841 chain
->link
[idx
].id
= link_val
->constant
->values
[0].u32
[0];
1843 chain
->link
[idx
].mode
= vtn_access_mode_id
;
1844 chain
->link
[idx
].id
= w
[i
];
1850 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1851 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
1852 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1853 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1854 * to combine an array of images with a single sampler to get an
1855 * array of sampled images that all share the same sampler.
1856 * Fortunately, this means that we can more-or-less ignore the
1857 * sampler when crawling the access chain, but it does leave us
1858 * with this rather awkward little special-case.
1860 struct vtn_value
*val
=
1861 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1862 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1863 val
->sampled_image
->type
= base_val
->sampled_image
->type
;
1864 val
->sampled_image
->image
=
1865 vtn_pointer_dereference(b
, base_val
->sampled_image
->image
, chain
);
1866 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
1868 vtn_assert(base_val
->value_type
== vtn_value_type_pointer
);
1869 struct vtn_value
*val
=
1870 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1871 val
->pointer
= vtn_pointer_dereference(b
, base_val
->pointer
, chain
);
1872 val
->pointer
->ptr_type
= ptr_type
;
1877 case SpvOpCopyMemory
: {
1878 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
1879 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
1881 vtn_assert_types_equal(b
, opcode
, dest
->type
->deref
, src
->type
->deref
);
1883 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
1888 struct vtn_type
*res_type
=
1889 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1890 struct vtn_value
*src_val
= vtn_value(b
, w
[3], vtn_value_type_pointer
);
1891 struct vtn_pointer
*src
= src_val
->pointer
;
1893 vtn_assert_types_equal(b
, opcode
, res_type
, src_val
->type
->deref
);
1895 if (glsl_type_is_image(res_type
->type
) ||
1896 glsl_type_is_sampler(res_type
->type
)) {
1897 vtn_push_value(b
, w
[2], vtn_value_type_pointer
)->pointer
= src
;
1901 vtn_push_ssa(b
, w
[2], res_type
, vtn_variable_load(b
, src
));
1906 struct vtn_value
*dest_val
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
1907 struct vtn_pointer
*dest
= dest_val
->pointer
;
1908 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[2]);
1910 /* OpStore requires us to actually have a storage type */
1911 vtn_fail_if(dest
->type
->type
== NULL
,
1912 "Invalid destination type for OpStore");
1914 if (glsl_get_base_type(dest
->type
->type
) == GLSL_TYPE_BOOL
&&
1915 glsl_get_base_type(src_val
->type
->type
) == GLSL_TYPE_UINT
) {
1916 /* Early versions of GLSLang would use uint types for UBOs/SSBOs but
1917 * would then store them to a local variable as bool. Work around
1918 * the issue by doing an implicit conversion.
1920 * https://github.com/KhronosGroup/glslang/issues/170
1921 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
1923 vtn_warn("OpStore of value of type OpTypeInt to a pointer to type "
1924 "OpTypeBool. Doing an implicit conversion to work around "
1926 struct vtn_ssa_value
*bool_ssa
=
1927 vtn_create_ssa_value(b
, dest
->type
->type
);
1928 bool_ssa
->def
= nir_i2b(&b
->nb
, vtn_ssa_value(b
, w
[2])->def
);
1929 vtn_variable_store(b
, bool_ssa
, dest
);
1933 vtn_assert_types_equal(b
, opcode
, dest_val
->type
->deref
, src_val
->type
);
1935 if (glsl_type_is_sampler(dest
->type
->type
)) {
1936 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
1937 "propagation to workaround the problem.");
1938 vtn_assert(dest
->var
->copy_prop_sampler
== NULL
);
1939 dest
->var
->copy_prop_sampler
=
1940 vtn_value(b
, w
[2], vtn_value_type_pointer
)->pointer
;
1944 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
1945 vtn_variable_store(b
, src
, dest
);
1949 case SpvOpArrayLength
: {
1950 struct vtn_pointer
*ptr
=
1951 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1953 const uint32_t offset
= ptr
->var
->type
->offsets
[w
[4]];
1954 const uint32_t stride
= ptr
->var
->type
->members
[w
[4]]->stride
;
1956 if (!ptr
->block_index
) {
1957 struct vtn_access_chain chain
= {
1960 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
1961 vtn_assert(ptr
->block_index
);
1964 nir_intrinsic_instr
*instr
=
1965 nir_intrinsic_instr_create(b
->nb
.shader
,
1966 nir_intrinsic_get_buffer_size
);
1967 instr
->src
[0] = nir_src_for_ssa(ptr
->block_index
);
1968 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
1969 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
1970 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
1972 /* array_length = max(buffer_size - offset, 0) / stride */
1973 nir_ssa_def
*array_length
=
1978 nir_imm_int(&b
->nb
, offset
)),
1979 nir_imm_int(&b
->nb
, 0u)),
1980 nir_imm_int(&b
->nb
, stride
));
1982 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1983 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
1984 val
->ssa
->def
= array_length
;
1988 case SpvOpCopyMemorySized
:
1990 vtn_fail("Unhandled opcode");