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"
31 #include <vulkan/vulkan_core.h>
33 static struct vtn_access_chain
*
34 vtn_access_chain_create(struct vtn_builder
*b
, unsigned length
)
36 struct vtn_access_chain
*chain
;
38 /* Subtract 1 from the length since there's already one built in */
39 size_t size
= sizeof(*chain
) +
40 (MAX2(length
, 1) - 1) * sizeof(chain
->link
[0]);
41 chain
= rzalloc_size(b
, size
);
42 chain
->length
= length
;
48 vtn_pointer_uses_ssa_offset(struct vtn_builder
*b
,
49 struct vtn_pointer
*ptr
)
51 return ((ptr
->mode
== vtn_variable_mode_ubo
||
52 ptr
->mode
== vtn_variable_mode_ssbo
) &&
53 b
->options
->lower_ubo_ssbo_access_to_offsets
) ||
54 ptr
->mode
== vtn_variable_mode_push_constant
||
55 (ptr
->mode
== vtn_variable_mode_workgroup
&&
56 b
->options
->lower_workgroup_access_to_offsets
);
60 vtn_pointer_is_external_block(struct vtn_builder
*b
,
61 struct vtn_pointer
*ptr
)
63 return ptr
->mode
== vtn_variable_mode_ssbo
||
64 ptr
->mode
== vtn_variable_mode_ubo
||
65 ptr
->mode
== vtn_variable_mode_phys_ssbo
||
66 ptr
->mode
== vtn_variable_mode_push_constant
||
67 (ptr
->mode
== vtn_variable_mode_workgroup
&&
68 b
->options
->lower_workgroup_access_to_offsets
);
72 vtn_access_link_as_ssa(struct vtn_builder
*b
, struct vtn_access_link link
,
73 unsigned stride
, unsigned bit_size
)
75 vtn_assert(stride
> 0);
76 if (link
.mode
== vtn_access_mode_literal
) {
77 return nir_imm_intN_t(&b
->nb
, link
.id
* stride
, bit_size
);
79 nir_ssa_def
*ssa
= vtn_ssa_value(b
, link
.id
)->def
;
80 if (ssa
->bit_size
!= bit_size
)
81 ssa
= nir_i2i(&b
->nb
, ssa
, bit_size
);
83 ssa
= nir_imul_imm(&b
->nb
, ssa
, stride
);
88 static VkDescriptorType
89 vk_desc_type_for_mode(struct vtn_builder
*b
, enum vtn_variable_mode mode
)
92 case vtn_variable_mode_ubo
:
93 return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
;
94 case vtn_variable_mode_ssbo
:
95 return VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
;
97 vtn_fail("Invalid mode for vulkan_resource_index");
102 vtn_variable_resource_index(struct vtn_builder
*b
, struct vtn_variable
*var
,
103 nir_ssa_def
*desc_array_index
)
105 if (!desc_array_index
) {
106 vtn_assert(glsl_type_is_struct(var
->type
->type
));
107 desc_array_index
= nir_imm_int(&b
->nb
, 0);
110 nir_intrinsic_instr
*instr
=
111 nir_intrinsic_instr_create(b
->nb
.shader
,
112 nir_intrinsic_vulkan_resource_index
);
113 instr
->src
[0] = nir_src_for_ssa(desc_array_index
);
114 nir_intrinsic_set_desc_set(instr
, var
->descriptor_set
);
115 nir_intrinsic_set_binding(instr
, var
->binding
);
116 nir_intrinsic_set_desc_type(instr
, vk_desc_type_for_mode(b
, var
->mode
));
118 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
119 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
121 return &instr
->dest
.ssa
;
125 vtn_resource_reindex(struct vtn_builder
*b
, enum vtn_variable_mode mode
,
126 nir_ssa_def
*base_index
, nir_ssa_def
*offset_index
)
128 nir_intrinsic_instr
*instr
=
129 nir_intrinsic_instr_create(b
->nb
.shader
,
130 nir_intrinsic_vulkan_resource_reindex
);
131 instr
->src
[0] = nir_src_for_ssa(base_index
);
132 instr
->src
[1] = nir_src_for_ssa(offset_index
);
133 nir_intrinsic_set_desc_type(instr
, vk_desc_type_for_mode(b
, mode
));
135 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
136 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
138 return &instr
->dest
.ssa
;
142 vtn_descriptor_load(struct vtn_builder
*b
, enum vtn_variable_mode mode
,
143 const struct glsl_type
*desc_type
, nir_ssa_def
*desc_index
)
145 nir_intrinsic_instr
*desc_load
=
146 nir_intrinsic_instr_create(b
->nb
.shader
,
147 nir_intrinsic_load_vulkan_descriptor
);
148 desc_load
->src
[0] = nir_src_for_ssa(desc_index
);
149 desc_load
->num_components
= glsl_get_vector_elements(desc_type
);
150 nir_intrinsic_set_desc_type(desc_load
, vk_desc_type_for_mode(b
, mode
));
151 nir_ssa_dest_init(&desc_load
->instr
, &desc_load
->dest
,
152 desc_load
->num_components
,
153 glsl_get_bit_size(desc_type
), NULL
);
154 nir_builder_instr_insert(&b
->nb
, &desc_load
->instr
);
156 return &desc_load
->dest
.ssa
;
159 /* Dereference the given base pointer by the access chain */
160 static struct vtn_pointer
*
161 vtn_nir_deref_pointer_dereference(struct vtn_builder
*b
,
162 struct vtn_pointer
*base
,
163 struct vtn_access_chain
*deref_chain
)
165 struct vtn_type
*type
= base
->type
;
166 enum gl_access_qualifier access
= base
->access
;
169 nir_deref_instr
*tail
;
172 } else if (vtn_pointer_is_external_block(b
, base
)) {
173 nir_ssa_def
*block_index
= base
->block_index
;
175 /* We dereferencing an external block pointer. Correctness of this
176 * operation relies on one particular line in the SPIR-V spec, section
177 * entitled "Validation Rules for Shader Capabilities":
179 * "Block and BufferBlock decorations cannot decorate a structure
180 * type that is nested at any level inside another structure type
181 * decorated with Block or BufferBlock."
183 * This means that we can detect the point where we cross over from
184 * descriptor indexing to buffer indexing by looking for the block
185 * decorated struct type. Anything before the block decorated struct
186 * type is a descriptor indexing operation and anything after the block
187 * decorated struct is a buffer offset operation.
190 /* Figure out the descriptor array index if any
192 * Some of the Vulkan CTS tests with hand-rolled SPIR-V have been known
193 * to forget the Block or BufferBlock decoration from time to time.
194 * It's more robust if we check for both !block_index and for the type
195 * to contain a block. This way there's a decent chance that arrays of
196 * UBOs/SSBOs will work correctly even if variable pointers are
199 nir_ssa_def
*desc_arr_idx
= NULL
;
200 if (!block_index
|| vtn_type_contains_block(b
, type
)) {
201 /* If our type contains a block, then we're still outside the block
202 * and we need to process enough levels of dereferences to get inside
205 if (deref_chain
->ptr_as_array
) {
206 unsigned aoa_size
= glsl_get_aoa_size(type
->type
);
207 desc_arr_idx
= vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
208 MAX2(aoa_size
, 1), 32);
212 for (; idx
< deref_chain
->length
; idx
++) {
213 if (type
->base_type
!= vtn_base_type_array
) {
214 vtn_assert(type
->base_type
== vtn_base_type_struct
);
218 unsigned aoa_size
= glsl_get_aoa_size(type
->array_element
->type
);
219 nir_ssa_def
*arr_offset
=
220 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
221 MAX2(aoa_size
, 1), 32);
223 desc_arr_idx
= nir_iadd(&b
->nb
, desc_arr_idx
, arr_offset
);
225 desc_arr_idx
= arr_offset
;
227 type
= type
->array_element
;
228 access
|= type
->access
;
233 vtn_assert(base
->var
&& base
->type
);
234 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
235 } else if (desc_arr_idx
) {
236 block_index
= vtn_resource_reindex(b
, base
->mode
,
237 block_index
, desc_arr_idx
);
240 if (idx
== deref_chain
->length
) {
241 /* The entire deref was consumed in finding the block index. Return
242 * a pointer which just has a block index and a later access chain
243 * will dereference deeper.
245 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
246 ptr
->mode
= base
->mode
;
248 ptr
->block_index
= block_index
;
249 ptr
->access
= access
;
253 /* If we got here, there's more access chain to handle and we have the
254 * final block index. Insert a descriptor load and cast to a deref to
255 * start the deref chain.
258 vtn_descriptor_load(b
, base
->mode
, base
->ptr_type
->type
, block_index
);
260 assert(base
->mode
== vtn_variable_mode_ssbo
||
261 base
->mode
== vtn_variable_mode_ubo
);
262 nir_variable_mode nir_mode
=
263 base
->mode
== vtn_variable_mode_ssbo
? nir_var_mem_ssbo
: nir_var_mem_ubo
;
265 tail
= nir_build_deref_cast(&b
->nb
, desc
, nir_mode
, type
->type
,
266 base
->ptr_type
->stride
);
268 assert(base
->var
&& base
->var
->var
);
269 tail
= nir_build_deref_var(&b
->nb
, base
->var
->var
);
270 if (base
->ptr_type
&& base
->ptr_type
->type
) {
271 tail
->dest
.ssa
.num_components
=
272 glsl_get_vector_elements(base
->ptr_type
->type
);
273 tail
->dest
.ssa
.bit_size
= glsl_get_bit_size(base
->ptr_type
->type
);
277 if (idx
== 0 && deref_chain
->ptr_as_array
) {
278 /* We start with a deref cast to get the stride. Hopefully, we'll be
279 * able to delete that cast eventually.
281 tail
= nir_build_deref_cast(&b
->nb
, &tail
->dest
.ssa
, tail
->mode
,
282 tail
->type
, base
->ptr_type
->stride
);
284 nir_ssa_def
*index
= vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1,
285 tail
->dest
.ssa
.bit_size
);
286 tail
= nir_build_deref_ptr_as_array(&b
->nb
, tail
, index
);
290 for (; idx
< deref_chain
->length
; idx
++) {
291 if (glsl_type_is_struct(type
->type
)) {
292 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
293 unsigned field
= deref_chain
->link
[idx
].id
;
294 tail
= nir_build_deref_struct(&b
->nb
, tail
, field
);
295 type
= type
->members
[field
];
297 nir_ssa_def
*arr_index
=
298 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
], 1,
299 tail
->dest
.ssa
.bit_size
);
300 tail
= nir_build_deref_array(&b
->nb
, tail
, arr_index
);
301 type
= type
->array_element
;
304 access
|= type
->access
;
307 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
308 ptr
->mode
= base
->mode
;
310 ptr
->var
= base
->var
;
312 ptr
->access
= access
;
317 static struct vtn_pointer
*
318 vtn_ssa_offset_pointer_dereference(struct vtn_builder
*b
,
319 struct vtn_pointer
*base
,
320 struct vtn_access_chain
*deref_chain
)
322 nir_ssa_def
*block_index
= base
->block_index
;
323 nir_ssa_def
*offset
= base
->offset
;
324 struct vtn_type
*type
= base
->type
;
325 enum gl_access_qualifier access
= base
->access
;
328 if (base
->mode
== vtn_variable_mode_ubo
||
329 base
->mode
== vtn_variable_mode_ssbo
) {
331 vtn_assert(base
->var
&& base
->type
);
332 nir_ssa_def
*desc_arr_idx
;
333 if (glsl_type_is_array(type
->type
)) {
334 if (deref_chain
->length
>= 1) {
336 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
338 /* This consumes a level of type */
339 type
= type
->array_element
;
340 access
|= type
->access
;
342 /* This is annoying. We've been asked for a pointer to the
343 * array of UBOs/SSBOs and not a specifc buffer. Return a
344 * pointer with a descriptor index of 0 and we'll have to do
345 * a reindex later to adjust it to the right thing.
347 desc_arr_idx
= nir_imm_int(&b
->nb
, 0);
349 } else if (deref_chain
->ptr_as_array
) {
350 /* You can't have a zero-length OpPtrAccessChain */
351 vtn_assert(deref_chain
->length
>= 1);
352 desc_arr_idx
= vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
354 /* We have a regular non-array SSBO. */
357 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
358 } else if (deref_chain
->ptr_as_array
&&
359 type
->base_type
== vtn_base_type_struct
&& type
->block
) {
360 /* We are doing an OpPtrAccessChain on a pointer to a struct that is
361 * decorated block. This is an interesting corner in the SPIR-V
362 * spec. One interpretation would be that they client is clearly
363 * trying to treat that block as if it's an implicit array of blocks
364 * repeated in the buffer. However, the SPIR-V spec for the
365 * OpPtrAccessChain says:
367 * "Base is treated as the address of the first element of an
368 * array, and the Element element’s address is computed to be the
369 * base for the Indexes, as per OpAccessChain."
371 * Taken literally, that would mean that your struct type is supposed
372 * to be treated as an array of such a struct and, since it's
373 * decorated block, that means an array of blocks which corresponds
374 * to an array descriptor. Therefore, we need to do a reindex
375 * operation to add the index from the first link in the access chain
376 * to the index we recieved.
378 * The downside to this interpretation (there always is one) is that
379 * this might be somewhat surprising behavior to apps if they expect
380 * the implicit array behavior described above.
382 vtn_assert(deref_chain
->length
>= 1);
383 nir_ssa_def
*offset_index
=
384 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
387 block_index
= vtn_resource_reindex(b
, base
->mode
,
388 block_index
, offset_index
);
393 if (base
->mode
== vtn_variable_mode_workgroup
) {
394 /* SLM doesn't need nor have a block index */
395 vtn_assert(!block_index
);
397 /* We need the variable for the base offset */
398 vtn_assert(base
->var
);
400 /* We need ptr_type for size and alignment */
401 vtn_assert(base
->ptr_type
);
403 /* Assign location on first use so that we don't end up bloating SLM
404 * address space for variables which are never statically used.
406 if (base
->var
->shared_location
< 0) {
407 vtn_assert(base
->ptr_type
->length
> 0 && base
->ptr_type
->align
> 0);
408 b
->shader
->num_shared
= vtn_align_u32(b
->shader
->num_shared
,
409 base
->ptr_type
->align
);
410 base
->var
->shared_location
= b
->shader
->num_shared
;
411 b
->shader
->num_shared
+= base
->ptr_type
->length
;
414 offset
= nir_imm_int(&b
->nb
, base
->var
->shared_location
);
415 } else if (base
->mode
== vtn_variable_mode_push_constant
) {
416 /* Push constants neither need nor have a block index */
417 vtn_assert(!block_index
);
419 /* Start off with at the start of the push constant block. */
420 offset
= nir_imm_int(&b
->nb
, 0);
422 /* The code above should have ensured a block_index when needed. */
423 vtn_assert(block_index
);
425 /* Start off with at the start of the buffer. */
426 offset
= nir_imm_int(&b
->nb
, 0);
430 if (deref_chain
->ptr_as_array
&& idx
== 0) {
431 /* We need ptr_type for the stride */
432 vtn_assert(base
->ptr_type
);
434 /* We need at least one element in the chain */
435 vtn_assert(deref_chain
->length
>= 1);
437 nir_ssa_def
*elem_offset
=
438 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
439 base
->ptr_type
->stride
, offset
->bit_size
);
440 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
444 for (; idx
< deref_chain
->length
; idx
++) {
445 switch (glsl_get_base_type(type
->type
)) {
448 case GLSL_TYPE_UINT16
:
449 case GLSL_TYPE_INT16
:
450 case GLSL_TYPE_UINT8
:
452 case GLSL_TYPE_UINT64
:
453 case GLSL_TYPE_INT64
:
454 case GLSL_TYPE_FLOAT
:
455 case GLSL_TYPE_FLOAT16
:
456 case GLSL_TYPE_DOUBLE
:
458 case GLSL_TYPE_ARRAY
: {
459 nir_ssa_def
*elem_offset
=
460 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
461 type
->stride
, offset
->bit_size
);
462 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
463 type
= type
->array_element
;
464 access
|= type
->access
;
468 case GLSL_TYPE_STRUCT
: {
469 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
470 unsigned member
= deref_chain
->link
[idx
].id
;
471 offset
= nir_iadd_imm(&b
->nb
, offset
, type
->offsets
[member
]);
472 type
= type
->members
[member
];
473 access
|= type
->access
;
478 vtn_fail("Invalid type for deref");
482 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
483 ptr
->mode
= base
->mode
;
485 ptr
->block_index
= block_index
;
486 ptr
->offset
= offset
;
487 ptr
->access
= access
;
492 /* Dereference the given base pointer by the access chain */
493 static struct vtn_pointer
*
494 vtn_pointer_dereference(struct vtn_builder
*b
,
495 struct vtn_pointer
*base
,
496 struct vtn_access_chain
*deref_chain
)
498 if (vtn_pointer_uses_ssa_offset(b
, base
)) {
499 return vtn_ssa_offset_pointer_dereference(b
, base
, deref_chain
);
501 return vtn_nir_deref_pointer_dereference(b
, base
, deref_chain
);
506 vtn_pointer_for_variable(struct vtn_builder
*b
,
507 struct vtn_variable
*var
, struct vtn_type
*ptr_type
)
509 struct vtn_pointer
*pointer
= rzalloc(b
, struct vtn_pointer
);
511 pointer
->mode
= var
->mode
;
512 pointer
->type
= var
->type
;
513 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
514 vtn_assert(ptr_type
->deref
->type
== var
->type
->type
);
515 pointer
->ptr_type
= ptr_type
;
517 pointer
->access
= var
->access
| var
->type
->access
;
522 /* Returns an atomic_uint type based on the original uint type. The returned
523 * type will be equivalent to the original one but will have an atomic_uint
524 * type as leaf instead of an uint.
526 * Manages uint scalars, arrays, and arrays of arrays of any nested depth.
528 static const struct glsl_type
*
529 repair_atomic_type(const struct glsl_type
*type
)
531 assert(glsl_get_base_type(glsl_without_array(type
)) == GLSL_TYPE_UINT
);
532 assert(glsl_type_is_scalar(glsl_without_array(type
)));
534 if (glsl_type_is_array(type
)) {
535 const struct glsl_type
*atomic
=
536 repair_atomic_type(glsl_get_array_element(type
));
538 return glsl_array_type(atomic
, glsl_get_length(type
),
539 glsl_get_explicit_stride(type
));
541 return glsl_atomic_uint_type();
546 vtn_pointer_to_deref(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
548 if (b
->wa_glslang_179
) {
549 /* Do on-the-fly copy propagation for samplers. */
550 if (ptr
->var
&& ptr
->var
->copy_prop_sampler
)
551 return vtn_pointer_to_deref(b
, ptr
->var
->copy_prop_sampler
);
554 vtn_assert(!vtn_pointer_uses_ssa_offset(b
, ptr
));
556 struct vtn_access_chain chain
= {
559 ptr
= vtn_nir_deref_pointer_dereference(b
, ptr
, &chain
);
566 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_instr
*deref
,
567 struct vtn_ssa_value
*inout
)
569 if (glsl_type_is_vector_or_scalar(deref
->type
)) {
571 inout
->def
= nir_load_deref(&b
->nb
, deref
);
573 nir_store_deref(&b
->nb
, deref
, inout
->def
, ~0);
575 } else if (glsl_type_is_array(deref
->type
) ||
576 glsl_type_is_matrix(deref
->type
)) {
577 unsigned elems
= glsl_get_length(deref
->type
);
578 for (unsigned i
= 0; i
< elems
; i
++) {
579 nir_deref_instr
*child
=
580 nir_build_deref_array(&b
->nb
, deref
, nir_imm_int(&b
->nb
, i
));
581 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
]);
584 vtn_assert(glsl_type_is_struct(deref
->type
));
585 unsigned elems
= glsl_get_length(deref
->type
);
586 for (unsigned i
= 0; i
< elems
; i
++) {
587 nir_deref_instr
*child
= nir_build_deref_struct(&b
->nb
, deref
, i
);
588 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
]);
594 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
596 struct vtn_pointer
*ptr
= vtn_value(b
, id
, vtn_value_type_pointer
)->pointer
;
597 return vtn_pointer_to_deref(b
, ptr
);
601 * Gets the NIR-level deref tail, which may have as a child an array deref
602 * selecting which component due to OpAccessChain supporting per-component
603 * indexing in SPIR-V.
605 static nir_deref_instr
*
606 get_deref_tail(nir_deref_instr
*deref
)
608 if (deref
->deref_type
!= nir_deref_type_array
)
611 nir_deref_instr
*parent
=
612 nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
);
614 if (glsl_type_is_vector(parent
->type
))
620 struct vtn_ssa_value
*
621 vtn_local_load(struct vtn_builder
*b
, nir_deref_instr
*src
)
623 nir_deref_instr
*src_tail
= get_deref_tail(src
);
624 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
625 _vtn_local_load_store(b
, true, src_tail
, val
);
627 if (src_tail
!= src
) {
628 val
->type
= src
->type
;
629 if (nir_src_is_const(src
->arr
.index
))
630 val
->def
= vtn_vector_extract(b
, val
->def
,
631 nir_src_as_uint(src
->arr
.index
));
633 val
->def
= vtn_vector_extract_dynamic(b
, val
->def
, src
->arr
.index
.ssa
);
640 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
641 nir_deref_instr
*dest
)
643 nir_deref_instr
*dest_tail
= get_deref_tail(dest
);
645 if (dest_tail
!= dest
) {
646 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
647 _vtn_local_load_store(b
, true, dest_tail
, val
);
649 if (nir_src_is_const(dest
->arr
.index
))
650 val
->def
= vtn_vector_insert(b
, val
->def
, src
->def
,
651 nir_src_as_uint(dest
->arr
.index
));
653 val
->def
= vtn_vector_insert_dynamic(b
, val
->def
, src
->def
,
654 dest
->arr
.index
.ssa
);
655 _vtn_local_load_store(b
, false, dest_tail
, val
);
657 _vtn_local_load_store(b
, false, dest_tail
, src
);
662 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
663 nir_ssa_def
**index_out
)
665 assert(vtn_pointer_uses_ssa_offset(b
, ptr
));
667 struct vtn_access_chain chain
= {
670 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
672 *index_out
= ptr
->block_index
;
676 /* Tries to compute the size of an interface block based on the strides and
677 * offsets that are provided to us in the SPIR-V source.
680 vtn_type_block_size(struct vtn_builder
*b
, struct vtn_type
*type
)
682 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
686 case GLSL_TYPE_UINT16
:
687 case GLSL_TYPE_INT16
:
688 case GLSL_TYPE_UINT8
:
690 case GLSL_TYPE_UINT64
:
691 case GLSL_TYPE_INT64
:
692 case GLSL_TYPE_FLOAT
:
693 case GLSL_TYPE_FLOAT16
:
695 case GLSL_TYPE_DOUBLE
: {
696 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
697 glsl_get_matrix_columns(type
->type
);
699 vtn_assert(type
->stride
> 0);
700 return type
->stride
* cols
;
702 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
703 return glsl_get_vector_elements(type
->type
) * type_size
;
707 case GLSL_TYPE_STRUCT
:
708 case GLSL_TYPE_INTERFACE
: {
710 unsigned num_fields
= glsl_get_length(type
->type
);
711 for (unsigned f
= 0; f
< num_fields
; f
++) {
712 unsigned field_end
= type
->offsets
[f
] +
713 vtn_type_block_size(b
, type
->members
[f
]);
714 size
= MAX2(size
, field_end
);
719 case GLSL_TYPE_ARRAY
:
720 vtn_assert(type
->stride
> 0);
721 vtn_assert(glsl_get_length(type
->type
) > 0);
722 return type
->stride
* glsl_get_length(type
->type
);
725 vtn_fail("Invalid block type");
731 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
732 nir_ssa_def
*index
, nir_ssa_def
*offset
,
733 unsigned access_offset
, unsigned access_size
,
734 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
,
735 enum gl_access_qualifier access
)
737 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
738 instr
->num_components
= glsl_get_vector_elements(type
);
740 /* Booleans usually shouldn't show up in external memory in SPIR-V.
741 * However, they do for certain older GLSLang versions and can for shared
742 * memory when we lower access chains internally.
744 const unsigned data_bit_size
= glsl_type_is_boolean(type
) ? 32 :
745 glsl_get_bit_size(type
);
749 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
750 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
753 if (op
== nir_intrinsic_load_push_constant
) {
754 nir_intrinsic_set_base(instr
, access_offset
);
755 nir_intrinsic_set_range(instr
, access_size
);
758 if (op
== nir_intrinsic_load_ssbo
||
759 op
== nir_intrinsic_store_ssbo
) {
760 nir_intrinsic_set_access(instr
, access
);
763 /* With extensions like relaxed_block_layout, we really can't guarantee
764 * much more than scalar alignment.
766 if (op
!= nir_intrinsic_load_push_constant
)
767 nir_intrinsic_set_align(instr
, data_bit_size
/ 8, 0);
770 instr
->src
[src
++] = nir_src_for_ssa(index
);
772 if (op
== nir_intrinsic_load_push_constant
) {
773 /* We need to subtract the offset from where the intrinsic will load the
776 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
777 nir_imm_int(&b
->nb
, access_offset
)));
779 instr
->src
[src
++] = nir_src_for_ssa(offset
);
783 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
784 instr
->num_components
, data_bit_size
, NULL
);
785 (*inout
)->def
= &instr
->dest
.ssa
;
788 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
790 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
791 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
795 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
796 nir_ssa_def
*index
, nir_ssa_def
*offset
,
797 unsigned access_offset
, unsigned access_size
,
798 struct vtn_type
*type
, enum gl_access_qualifier access
,
799 struct vtn_ssa_value
**inout
)
801 if (load
&& *inout
== NULL
)
802 *inout
= vtn_create_ssa_value(b
, type
->type
);
804 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
808 case GLSL_TYPE_UINT16
:
809 case GLSL_TYPE_INT16
:
810 case GLSL_TYPE_UINT8
:
812 case GLSL_TYPE_UINT64
:
813 case GLSL_TYPE_INT64
:
814 case GLSL_TYPE_FLOAT
:
815 case GLSL_TYPE_FLOAT16
:
816 case GLSL_TYPE_DOUBLE
:
818 /* This is where things get interesting. At this point, we've hit
819 * a vector, a scalar, or a matrix.
821 if (glsl_type_is_matrix(type
->type
)) {
822 /* Loading the whole matrix */
823 struct vtn_ssa_value
*transpose
;
824 unsigned num_ops
, vec_width
, col_stride
;
825 if (type
->row_major
) {
826 num_ops
= glsl_get_vector_elements(type
->type
);
827 vec_width
= glsl_get_matrix_columns(type
->type
);
828 col_stride
= type
->array_element
->stride
;
830 const struct glsl_type
*transpose_type
=
831 glsl_matrix_type(base_type
, vec_width
, num_ops
);
832 *inout
= vtn_create_ssa_value(b
, transpose_type
);
834 transpose
= vtn_ssa_transpose(b
, *inout
);
838 num_ops
= glsl_get_matrix_columns(type
->type
);
839 vec_width
= glsl_get_vector_elements(type
->type
);
840 col_stride
= type
->stride
;
843 for (unsigned i
= 0; i
< num_ops
; i
++) {
844 nir_ssa_def
*elem_offset
=
845 nir_iadd_imm(&b
->nb
, offset
, i
* col_stride
);
846 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
847 access_offset
, access_size
,
849 glsl_vector_type(base_type
, vec_width
),
850 type
->access
| access
);
853 if (load
&& type
->row_major
)
854 *inout
= vtn_ssa_transpose(b
, *inout
);
856 unsigned elems
= glsl_get_vector_elements(type
->type
);
857 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
858 if (elems
== 1 || type
->stride
== type_size
) {
859 /* This is a tightly-packed normal scalar or vector load */
860 vtn_assert(glsl_type_is_vector_or_scalar(type
->type
));
861 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
862 access_offset
, access_size
,
864 type
->access
| access
);
866 /* This is a strided load. We have to load N things separately.
867 * This is the single column of a row-major matrix case.
869 vtn_assert(type
->stride
> type_size
);
870 vtn_assert(type
->stride
% type_size
== 0);
872 nir_ssa_def
*per_comp
[4];
873 for (unsigned i
= 0; i
< elems
; i
++) {
874 nir_ssa_def
*elem_offset
=
875 nir_iadd_imm(&b
->nb
, offset
, i
* type
->stride
);
876 struct vtn_ssa_value
*comp
, temp_val
;
878 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
879 temp_val
.type
= glsl_scalar_type(base_type
);
882 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
883 access_offset
, access_size
,
884 &comp
, glsl_scalar_type(base_type
),
885 type
->access
| access
);
886 per_comp
[i
] = comp
->def
;
891 *inout
= vtn_create_ssa_value(b
, type
->type
);
892 (*inout
)->def
= nir_vec(&b
->nb
, per_comp
, elems
);
898 case GLSL_TYPE_ARRAY
: {
899 unsigned elems
= glsl_get_length(type
->type
);
900 for (unsigned i
= 0; i
< elems
; i
++) {
901 nir_ssa_def
*elem_off
=
902 nir_iadd_imm(&b
->nb
, offset
, i
* type
->stride
);
903 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
904 access_offset
, access_size
,
906 type
->array_element
->access
| access
,
907 &(*inout
)->elems
[i
]);
912 case GLSL_TYPE_STRUCT
: {
913 unsigned elems
= glsl_get_length(type
->type
);
914 for (unsigned i
= 0; i
< elems
; i
++) {
915 nir_ssa_def
*elem_off
=
916 nir_iadd_imm(&b
->nb
, offset
, type
->offsets
[i
]);
917 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
918 access_offset
, access_size
,
920 type
->members
[i
]->access
| access
,
921 &(*inout
)->elems
[i
]);
927 vtn_fail("Invalid block member type");
931 static struct vtn_ssa_value
*
932 vtn_block_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
935 unsigned access_offset
= 0, access_size
= 0;
937 case vtn_variable_mode_ubo
:
938 op
= nir_intrinsic_load_ubo
;
940 case vtn_variable_mode_ssbo
:
941 op
= nir_intrinsic_load_ssbo
;
943 case vtn_variable_mode_push_constant
:
944 op
= nir_intrinsic_load_push_constant
;
945 access_size
= b
->shader
->num_uniforms
;
947 case vtn_variable_mode_workgroup
:
948 op
= nir_intrinsic_load_shared
;
951 vtn_fail("Invalid block variable mode");
954 nir_ssa_def
*offset
, *index
= NULL
;
955 offset
= vtn_pointer_to_offset(b
, src
, &index
);
957 struct vtn_ssa_value
*value
= NULL
;
958 _vtn_block_load_store(b
, op
, true, index
, offset
,
959 access_offset
, access_size
,
960 src
->type
, src
->access
, &value
);
965 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
966 struct vtn_pointer
*dst
)
970 case vtn_variable_mode_ssbo
:
971 op
= nir_intrinsic_store_ssbo
;
973 case vtn_variable_mode_workgroup
:
974 op
= nir_intrinsic_store_shared
;
977 vtn_fail("Invalid block variable mode");
980 nir_ssa_def
*offset
, *index
= NULL
;
981 offset
= vtn_pointer_to_offset(b
, dst
, &index
);
983 _vtn_block_load_store(b
, op
, false, index
, offset
,
984 0, 0, dst
->type
, dst
->access
, &src
);
988 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
989 struct vtn_pointer
*ptr
,
990 struct vtn_ssa_value
**inout
)
992 enum glsl_base_type base_type
= glsl_get_base_type(ptr
->type
->type
);
996 case GLSL_TYPE_UINT16
:
997 case GLSL_TYPE_INT16
:
998 case GLSL_TYPE_UINT8
:
1000 case GLSL_TYPE_UINT64
:
1001 case GLSL_TYPE_INT64
:
1002 case GLSL_TYPE_FLOAT
:
1003 case GLSL_TYPE_FLOAT16
:
1004 case GLSL_TYPE_BOOL
:
1005 case GLSL_TYPE_DOUBLE
:
1006 if (glsl_type_is_vector_or_scalar(ptr
->type
->type
)) {
1007 /* We hit a vector or scalar; go ahead and emit the load[s] */
1008 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
1009 if (vtn_pointer_is_external_block(b
, ptr
)) {
1010 /* If it's external, we call nir_load/store_deref directly. The
1011 * vtn_local_load/store helpers are too clever and do magic to
1012 * avoid array derefs of vectors. That magic is both less
1013 * efficient than the direct load/store and, in the case of
1014 * stores, is broken because it creates a race condition if two
1015 * threads are writing to different components of the same vector
1016 * due to the load+insert+store it uses to emulate the array
1020 *inout
= vtn_create_ssa_value(b
, ptr
->type
->type
);
1021 (*inout
)->def
= nir_load_deref(&b
->nb
, deref
);
1023 nir_store_deref(&b
->nb
, deref
, (*inout
)->def
, ~0);
1027 *inout
= vtn_local_load(b
, deref
);
1029 vtn_local_store(b
, *inout
, deref
);
1036 case GLSL_TYPE_ARRAY
:
1037 case GLSL_TYPE_STRUCT
: {
1038 unsigned elems
= glsl_get_length(ptr
->type
->type
);
1040 vtn_assert(*inout
== NULL
);
1041 *inout
= rzalloc(b
, struct vtn_ssa_value
);
1042 (*inout
)->type
= ptr
->type
->type
;
1043 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
1046 struct vtn_access_chain chain
= {
1049 { .mode
= vtn_access_mode_literal
, },
1052 for (unsigned i
= 0; i
< elems
; i
++) {
1053 chain
.link
[0].id
= i
;
1054 struct vtn_pointer
*elem
= vtn_pointer_dereference(b
, ptr
, &chain
);
1055 _vtn_variable_load_store(b
, load
, elem
, &(*inout
)->elems
[i
]);
1061 vtn_fail("Invalid access chain type");
1065 struct vtn_ssa_value
*
1066 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
1068 if (vtn_pointer_uses_ssa_offset(b
, src
)) {
1069 return vtn_block_load(b
, src
);
1071 struct vtn_ssa_value
*val
= NULL
;
1072 _vtn_variable_load_store(b
, true, src
, &val
);
1078 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
1079 struct vtn_pointer
*dest
)
1081 if (vtn_pointer_uses_ssa_offset(b
, dest
)) {
1082 vtn_assert(dest
->mode
== vtn_variable_mode_ssbo
||
1083 dest
->mode
== vtn_variable_mode_workgroup
);
1084 vtn_block_store(b
, src
, dest
);
1086 _vtn_variable_load_store(b
, false, dest
, &src
);
1091 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
1092 struct vtn_pointer
*src
)
1094 vtn_assert(src
->type
->type
== dest
->type
->type
);
1095 enum glsl_base_type base_type
= glsl_get_base_type(src
->type
->type
);
1096 switch (base_type
) {
1097 case GLSL_TYPE_UINT
:
1099 case GLSL_TYPE_UINT16
:
1100 case GLSL_TYPE_INT16
:
1101 case GLSL_TYPE_UINT8
:
1102 case GLSL_TYPE_INT8
:
1103 case GLSL_TYPE_UINT64
:
1104 case GLSL_TYPE_INT64
:
1105 case GLSL_TYPE_FLOAT
:
1106 case GLSL_TYPE_FLOAT16
:
1107 case GLSL_TYPE_DOUBLE
:
1108 case GLSL_TYPE_BOOL
:
1109 /* At this point, we have a scalar, vector, or matrix so we know that
1110 * there cannot be any structure splitting still in the way. By
1111 * stopping at the matrix level rather than the vector level, we
1112 * ensure that matrices get loaded in the optimal way even if they
1113 * are storred row-major in a UBO.
1115 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
1118 case GLSL_TYPE_ARRAY
:
1119 case GLSL_TYPE_STRUCT
: {
1120 struct vtn_access_chain chain
= {
1123 { .mode
= vtn_access_mode_literal
, },
1126 unsigned elems
= glsl_get_length(src
->type
->type
);
1127 for (unsigned i
= 0; i
< elems
; i
++) {
1128 chain
.link
[0].id
= i
;
1129 struct vtn_pointer
*src_elem
=
1130 vtn_pointer_dereference(b
, src
, &chain
);
1131 struct vtn_pointer
*dest_elem
=
1132 vtn_pointer_dereference(b
, dest
, &chain
);
1134 _vtn_variable_copy(b
, dest_elem
, src_elem
);
1140 vtn_fail("Invalid access chain type");
1145 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
1146 struct vtn_pointer
*src
)
1148 /* TODO: At some point, we should add a special-case for when we can
1149 * just emit a copy_var intrinsic.
1151 _vtn_variable_copy(b
, dest
, src
);
1155 set_mode_system_value(struct vtn_builder
*b
, nir_variable_mode
*mode
)
1157 vtn_assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
1158 *mode
= nir_var_system_value
;
1162 vtn_get_builtin_location(struct vtn_builder
*b
,
1163 SpvBuiltIn builtin
, int *location
,
1164 nir_variable_mode
*mode
)
1167 case SpvBuiltInPosition
:
1168 *location
= VARYING_SLOT_POS
;
1170 case SpvBuiltInPointSize
:
1171 *location
= VARYING_SLOT_PSIZ
;
1173 case SpvBuiltInClipDistance
:
1174 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
1176 case SpvBuiltInCullDistance
:
1177 *location
= VARYING_SLOT_CULL_DIST0
;
1179 case SpvBuiltInVertexId
:
1180 case SpvBuiltInVertexIndex
:
1181 /* The Vulkan spec defines VertexIndex to be non-zero-based and doesn't
1182 * allow VertexId. The ARB_gl_spirv spec defines VertexId to be the
1183 * same as gl_VertexID, which is non-zero-based, and removes
1184 * VertexIndex. Since they're both defined to be non-zero-based, we use
1185 * SYSTEM_VALUE_VERTEX_ID for both.
1187 *location
= SYSTEM_VALUE_VERTEX_ID
;
1188 set_mode_system_value(b
, mode
);
1190 case SpvBuiltInInstanceIndex
:
1191 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
1192 set_mode_system_value(b
, mode
);
1194 case SpvBuiltInInstanceId
:
1195 *location
= SYSTEM_VALUE_INSTANCE_ID
;
1196 set_mode_system_value(b
, mode
);
1198 case SpvBuiltInPrimitiveId
:
1199 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1200 vtn_assert(*mode
== nir_var_shader_in
);
1201 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1202 } else if (*mode
== nir_var_shader_out
) {
1203 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1205 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
1206 set_mode_system_value(b
, mode
);
1209 case SpvBuiltInInvocationId
:
1210 *location
= SYSTEM_VALUE_INVOCATION_ID
;
1211 set_mode_system_value(b
, mode
);
1213 case SpvBuiltInLayer
:
1214 *location
= VARYING_SLOT_LAYER
;
1215 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1216 *mode
= nir_var_shader_in
;
1217 else if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1218 *mode
= nir_var_shader_out
;
1219 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1220 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1221 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1222 *mode
= nir_var_shader_out
;
1224 vtn_fail("invalid stage for SpvBuiltInLayer");
1226 case SpvBuiltInViewportIndex
:
1227 *location
= VARYING_SLOT_VIEWPORT
;
1228 if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1229 *mode
= nir_var_shader_out
;
1230 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1231 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1232 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1233 *mode
= nir_var_shader_out
;
1234 else if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1235 *mode
= nir_var_shader_in
;
1237 vtn_fail("invalid stage for SpvBuiltInViewportIndex");
1239 case SpvBuiltInTessLevelOuter
:
1240 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
1242 case SpvBuiltInTessLevelInner
:
1243 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
1245 case SpvBuiltInTessCoord
:
1246 *location
= SYSTEM_VALUE_TESS_COORD
;
1247 set_mode_system_value(b
, mode
);
1249 case SpvBuiltInPatchVertices
:
1250 *location
= SYSTEM_VALUE_VERTICES_IN
;
1251 set_mode_system_value(b
, mode
);
1253 case SpvBuiltInFragCoord
:
1254 *location
= VARYING_SLOT_POS
;
1255 vtn_assert(*mode
== nir_var_shader_in
);
1257 case SpvBuiltInPointCoord
:
1258 *location
= VARYING_SLOT_PNTC
;
1259 vtn_assert(*mode
== nir_var_shader_in
);
1261 case SpvBuiltInFrontFacing
:
1262 *location
= SYSTEM_VALUE_FRONT_FACE
;
1263 set_mode_system_value(b
, mode
);
1265 case SpvBuiltInSampleId
:
1266 *location
= SYSTEM_VALUE_SAMPLE_ID
;
1267 set_mode_system_value(b
, mode
);
1269 case SpvBuiltInSamplePosition
:
1270 *location
= SYSTEM_VALUE_SAMPLE_POS
;
1271 set_mode_system_value(b
, mode
);
1273 case SpvBuiltInSampleMask
:
1274 if (*mode
== nir_var_shader_out
) {
1275 *location
= FRAG_RESULT_SAMPLE_MASK
;
1277 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1278 set_mode_system_value(b
, mode
);
1281 case SpvBuiltInFragDepth
:
1282 *location
= FRAG_RESULT_DEPTH
;
1283 vtn_assert(*mode
== nir_var_shader_out
);
1285 case SpvBuiltInHelperInvocation
:
1286 *location
= SYSTEM_VALUE_HELPER_INVOCATION
;
1287 set_mode_system_value(b
, mode
);
1289 case SpvBuiltInNumWorkgroups
:
1290 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1291 set_mode_system_value(b
, mode
);
1293 case SpvBuiltInWorkgroupSize
:
1294 *location
= SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
1295 set_mode_system_value(b
, mode
);
1297 case SpvBuiltInWorkgroupId
:
1298 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1299 set_mode_system_value(b
, mode
);
1301 case SpvBuiltInLocalInvocationId
:
1302 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1303 set_mode_system_value(b
, mode
);
1305 case SpvBuiltInLocalInvocationIndex
:
1306 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1307 set_mode_system_value(b
, mode
);
1309 case SpvBuiltInGlobalInvocationId
:
1310 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1311 set_mode_system_value(b
, mode
);
1313 case SpvBuiltInBaseVertex
:
1314 /* OpenGL gl_BaseVertex (SYSTEM_VALUE_BASE_VERTEX) is not the same
1315 * semantic as SPIR-V BaseVertex (SYSTEM_VALUE_FIRST_VERTEX).
1317 *location
= SYSTEM_VALUE_FIRST_VERTEX
;
1318 set_mode_system_value(b
, mode
);
1320 case SpvBuiltInBaseInstance
:
1321 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1322 set_mode_system_value(b
, mode
);
1324 case SpvBuiltInDrawIndex
:
1325 *location
= SYSTEM_VALUE_DRAW_ID
;
1326 set_mode_system_value(b
, mode
);
1328 case SpvBuiltInSubgroupSize
:
1329 *location
= SYSTEM_VALUE_SUBGROUP_SIZE
;
1330 set_mode_system_value(b
, mode
);
1332 case SpvBuiltInSubgroupId
:
1333 *location
= SYSTEM_VALUE_SUBGROUP_ID
;
1334 set_mode_system_value(b
, mode
);
1336 case SpvBuiltInSubgroupLocalInvocationId
:
1337 *location
= SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1338 set_mode_system_value(b
, mode
);
1340 case SpvBuiltInNumSubgroups
:
1341 *location
= SYSTEM_VALUE_NUM_SUBGROUPS
;
1342 set_mode_system_value(b
, mode
);
1344 case SpvBuiltInDeviceIndex
:
1345 *location
= SYSTEM_VALUE_DEVICE_INDEX
;
1346 set_mode_system_value(b
, mode
);
1348 case SpvBuiltInViewIndex
:
1349 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1350 set_mode_system_value(b
, mode
);
1352 case SpvBuiltInSubgroupEqMask
:
1353 *location
= SYSTEM_VALUE_SUBGROUP_EQ_MASK
,
1354 set_mode_system_value(b
, mode
);
1356 case SpvBuiltInSubgroupGeMask
:
1357 *location
= SYSTEM_VALUE_SUBGROUP_GE_MASK
,
1358 set_mode_system_value(b
, mode
);
1360 case SpvBuiltInSubgroupGtMask
:
1361 *location
= SYSTEM_VALUE_SUBGROUP_GT_MASK
,
1362 set_mode_system_value(b
, mode
);
1364 case SpvBuiltInSubgroupLeMask
:
1365 *location
= SYSTEM_VALUE_SUBGROUP_LE_MASK
,
1366 set_mode_system_value(b
, mode
);
1368 case SpvBuiltInSubgroupLtMask
:
1369 *location
= SYSTEM_VALUE_SUBGROUP_LT_MASK
,
1370 set_mode_system_value(b
, mode
);
1372 case SpvBuiltInFragStencilRefEXT
:
1373 *location
= FRAG_RESULT_STENCIL
;
1374 vtn_assert(*mode
== nir_var_shader_out
);
1376 case SpvBuiltInWorkDim
:
1377 *location
= SYSTEM_VALUE_WORK_DIM
;
1378 set_mode_system_value(b
, mode
);
1380 case SpvBuiltInGlobalSize
:
1381 *location
= SYSTEM_VALUE_GLOBAL_GROUP_SIZE
;
1382 set_mode_system_value(b
, mode
);
1385 vtn_fail("unsupported builtin: %u", builtin
);
1390 apply_var_decoration(struct vtn_builder
*b
,
1391 struct nir_variable_data
*var_data
,
1392 const struct vtn_decoration
*dec
)
1394 switch (dec
->decoration
) {
1395 case SpvDecorationRelaxedPrecision
:
1396 break; /* FIXME: Do nothing with this for now. */
1397 case SpvDecorationNoPerspective
:
1398 var_data
->interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1400 case SpvDecorationFlat
:
1401 var_data
->interpolation
= INTERP_MODE_FLAT
;
1403 case SpvDecorationCentroid
:
1404 var_data
->centroid
= true;
1406 case SpvDecorationSample
:
1407 var_data
->sample
= true;
1409 case SpvDecorationInvariant
:
1410 var_data
->invariant
= true;
1412 case SpvDecorationConstant
:
1413 var_data
->read_only
= true;
1415 case SpvDecorationNonReadable
:
1416 var_data
->image
.access
|= ACCESS_NON_READABLE
;
1418 case SpvDecorationNonWritable
:
1419 var_data
->read_only
= true;
1420 var_data
->image
.access
|= ACCESS_NON_WRITEABLE
;
1422 case SpvDecorationRestrict
:
1423 var_data
->image
.access
|= ACCESS_RESTRICT
;
1425 case SpvDecorationVolatile
:
1426 var_data
->image
.access
|= ACCESS_VOLATILE
;
1428 case SpvDecorationCoherent
:
1429 var_data
->image
.access
|= ACCESS_COHERENT
;
1431 case SpvDecorationComponent
:
1432 var_data
->location_frac
= dec
->literals
[0];
1434 case SpvDecorationIndex
:
1435 var_data
->index
= dec
->literals
[0];
1437 case SpvDecorationBuiltIn
: {
1438 SpvBuiltIn builtin
= dec
->literals
[0];
1440 nir_variable_mode mode
= var_data
->mode
;
1441 vtn_get_builtin_location(b
, builtin
, &var_data
->location
, &mode
);
1442 var_data
->mode
= mode
;
1445 case SpvBuiltInTessLevelOuter
:
1446 case SpvBuiltInTessLevelInner
:
1447 var_data
->compact
= true;
1449 case SpvBuiltInFragCoord
:
1450 var_data
->pixel_center_integer
= b
->pixel_center_integer
;
1452 case SpvBuiltInSamplePosition
:
1453 var_data
->origin_upper_left
= b
->origin_upper_left
;
1460 case SpvDecorationSpecId
:
1461 case SpvDecorationRowMajor
:
1462 case SpvDecorationColMajor
:
1463 case SpvDecorationMatrixStride
:
1464 case SpvDecorationAliased
:
1465 case SpvDecorationUniform
:
1466 case SpvDecorationLinkageAttributes
:
1467 break; /* Do nothing with these here */
1469 case SpvDecorationPatch
:
1470 var_data
->patch
= true;
1473 case SpvDecorationLocation
:
1474 vtn_fail("Handled above");
1476 case SpvDecorationBlock
:
1477 case SpvDecorationBufferBlock
:
1478 case SpvDecorationArrayStride
:
1479 case SpvDecorationGLSLShared
:
1480 case SpvDecorationGLSLPacked
:
1481 break; /* These can apply to a type but we don't care about them */
1483 case SpvDecorationBinding
:
1484 case SpvDecorationDescriptorSet
:
1485 case SpvDecorationNoContraction
:
1486 case SpvDecorationInputAttachmentIndex
:
1487 vtn_warn("Decoration not allowed for variable or structure member: %s",
1488 spirv_decoration_to_string(dec
->decoration
));
1491 case SpvDecorationXfbBuffer
:
1492 var_data
->explicit_xfb_buffer
= true;
1493 var_data
->xfb_buffer
= dec
->literals
[0];
1494 var_data
->always_active_io
= true;
1496 case SpvDecorationXfbStride
:
1497 var_data
->explicit_xfb_stride
= true;
1498 var_data
->xfb_stride
= dec
->literals
[0];
1500 case SpvDecorationOffset
:
1501 var_data
->explicit_offset
= true;
1502 var_data
->offset
= dec
->literals
[0];
1505 case SpvDecorationStream
:
1506 var_data
->stream
= dec
->literals
[0];
1509 case SpvDecorationCPacked
:
1510 case SpvDecorationSaturatedConversion
:
1511 case SpvDecorationFuncParamAttr
:
1512 case SpvDecorationFPRoundingMode
:
1513 case SpvDecorationFPFastMathMode
:
1514 case SpvDecorationAlignment
:
1515 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
1516 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1517 spirv_decoration_to_string(dec
->decoration
));
1521 case SpvDecorationHlslSemanticGOOGLE
:
1522 /* HLSL semantic decorations can safely be ignored by the driver. */
1525 case SpvDecorationRestrictPointerEXT
:
1526 case SpvDecorationAliasedPointerEXT
:
1527 /* TODO: We should actually plumb alias information through NIR. */
1531 vtn_fail("Unhandled decoration");
1536 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1537 const struct vtn_decoration
*dec
, void *out_is_patch
)
1539 if (dec
->decoration
== SpvDecorationPatch
) {
1540 *((bool *) out_is_patch
) = true;
1545 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1546 const struct vtn_decoration
*dec
, void *void_var
)
1548 struct vtn_variable
*vtn_var
= void_var
;
1550 /* Handle decorations that apply to a vtn_variable as a whole */
1551 switch (dec
->decoration
) {
1552 case SpvDecorationBinding
:
1553 vtn_var
->binding
= dec
->literals
[0];
1554 vtn_var
->explicit_binding
= true;
1556 case SpvDecorationDescriptorSet
:
1557 vtn_var
->descriptor_set
= dec
->literals
[0];
1559 case SpvDecorationInputAttachmentIndex
:
1560 vtn_var
->input_attachment_index
= dec
->literals
[0];
1562 case SpvDecorationPatch
:
1563 vtn_var
->patch
= true;
1565 case SpvDecorationOffset
:
1566 vtn_var
->offset
= dec
->literals
[0];
1568 case SpvDecorationNonWritable
:
1569 vtn_var
->access
|= ACCESS_NON_WRITEABLE
;
1571 case SpvDecorationNonReadable
:
1572 vtn_var
->access
|= ACCESS_NON_READABLE
;
1574 case SpvDecorationVolatile
:
1575 vtn_var
->access
|= ACCESS_VOLATILE
;
1577 case SpvDecorationCoherent
:
1578 vtn_var
->access
|= ACCESS_COHERENT
;
1580 case SpvDecorationHlslCounterBufferGOOGLE
:
1581 /* HLSL semantic decorations can safely be ignored by the driver. */
1587 if (val
->value_type
== vtn_value_type_pointer
) {
1588 assert(val
->pointer
->var
== void_var
);
1589 assert(member
== -1);
1591 assert(val
->value_type
== vtn_value_type_type
);
1594 /* Location is odd. If applied to a split structure, we have to walk the
1595 * whole thing and accumulate the location. It's easier to handle as a
1598 if (dec
->decoration
== SpvDecorationLocation
) {
1599 unsigned location
= dec
->literals
[0];
1600 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
&&
1601 vtn_var
->mode
== vtn_variable_mode_output
) {
1602 location
+= FRAG_RESULT_DATA0
;
1603 } else if (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
&&
1604 vtn_var
->mode
== vtn_variable_mode_input
) {
1605 location
+= VERT_ATTRIB_GENERIC0
;
1606 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1607 vtn_var
->mode
== vtn_variable_mode_output
) {
1608 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1609 } else if (vtn_var
->mode
!= vtn_variable_mode_uniform
) {
1610 vtn_warn("Location must be on input, output, uniform, sampler or "
1615 if (vtn_var
->var
->num_members
== 0) {
1616 /* This handles the member and lone variable cases */
1617 vtn_var
->var
->data
.location
= location
;
1619 /* This handles the structure member case */
1620 assert(vtn_var
->var
->members
);
1623 vtn_var
->base_location
= location
;
1625 vtn_var
->var
->members
[member
].location
= location
;
1631 if (vtn_var
->var
->num_members
== 0) {
1632 /* We call this function on types as well as variables and not all
1633 * struct types get split so we can end up having stray member
1634 * decorations; just ignore them.
1637 apply_var_decoration(b
, &vtn_var
->var
->data
, dec
);
1638 } else if (member
>= 0) {
1639 /* Member decorations must come from a type */
1640 assert(val
->value_type
== vtn_value_type_type
);
1641 apply_var_decoration(b
, &vtn_var
->var
->members
[member
], dec
);
1644 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1645 for (unsigned i
= 0; i
< length
; i
++)
1646 apply_var_decoration(b
, &vtn_var
->var
->members
[i
], dec
);
1649 /* A few variables, those with external storage, have no actual
1650 * nir_variables associated with them. Fortunately, all decorations
1651 * we care about for those variables are on the type only.
1653 vtn_assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1654 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1655 vtn_var
->mode
== vtn_variable_mode_push_constant
||
1656 (vtn_var
->mode
== vtn_variable_mode_workgroup
&&
1657 b
->options
->lower_workgroup_access_to_offsets
));
1662 static enum vtn_variable_mode
1663 vtn_storage_class_to_mode(struct vtn_builder
*b
,
1664 SpvStorageClass
class,
1665 struct vtn_type
*interface_type
,
1666 nir_variable_mode
*nir_mode_out
)
1668 enum vtn_variable_mode mode
;
1669 nir_variable_mode nir_mode
;
1671 case SpvStorageClassUniform
:
1672 if (interface_type
->block
) {
1673 mode
= vtn_variable_mode_ubo
;
1674 nir_mode
= nir_var_mem_ubo
;
1675 } else if (interface_type
->buffer_block
) {
1676 mode
= vtn_variable_mode_ssbo
;
1677 nir_mode
= nir_var_mem_ssbo
;
1679 /* Default-block uniforms, coming from gl_spirv */
1680 mode
= vtn_variable_mode_uniform
;
1681 nir_mode
= nir_var_uniform
;
1684 case SpvStorageClassStorageBuffer
:
1685 mode
= vtn_variable_mode_ssbo
;
1686 nir_mode
= nir_var_mem_ssbo
;
1688 case SpvStorageClassPhysicalStorageBufferEXT
:
1689 mode
= vtn_variable_mode_phys_ssbo
;
1690 nir_mode
= nir_var_mem_global
;
1692 case SpvStorageClassUniformConstant
:
1693 mode
= vtn_variable_mode_uniform
;
1694 nir_mode
= nir_var_uniform
;
1696 case SpvStorageClassPushConstant
:
1697 mode
= vtn_variable_mode_push_constant
;
1698 nir_mode
= nir_var_uniform
;
1700 case SpvStorageClassInput
:
1701 mode
= vtn_variable_mode_input
;
1702 nir_mode
= nir_var_shader_in
;
1704 case SpvStorageClassOutput
:
1705 mode
= vtn_variable_mode_output
;
1706 nir_mode
= nir_var_shader_out
;
1708 case SpvStorageClassPrivate
:
1709 mode
= vtn_variable_mode_private
;
1710 nir_mode
= nir_var_shader_temp
;
1712 case SpvStorageClassFunction
:
1713 mode
= vtn_variable_mode_function
;
1714 nir_mode
= nir_var_function_temp
;
1716 case SpvStorageClassWorkgroup
:
1717 mode
= vtn_variable_mode_workgroup
;
1718 nir_mode
= nir_var_mem_shared
;
1720 case SpvStorageClassAtomicCounter
:
1721 mode
= vtn_variable_mode_uniform
;
1722 nir_mode
= nir_var_uniform
;
1724 case SpvStorageClassCrossWorkgroup
:
1725 mode
= vtn_variable_mode_cross_workgroup
;
1726 nir_mode
= nir_var_mem_global
;
1728 case SpvStorageClassGeneric
:
1730 vtn_fail("Unhandled variable storage class");
1734 *nir_mode_out
= nir_mode
;
1740 vtn_pointer_to_ssa(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
1742 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
1743 /* This pointer needs to have a pointer type with actual storage */
1744 vtn_assert(ptr
->ptr_type
);
1745 vtn_assert(ptr
->ptr_type
->type
);
1748 /* If we don't have an offset then we must be a pointer to the variable
1751 vtn_assert(!ptr
->offset
&& !ptr
->block_index
);
1753 struct vtn_access_chain chain
= {
1756 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
1759 vtn_assert(ptr
->offset
);
1760 if (ptr
->block_index
) {
1761 vtn_assert(ptr
->mode
== vtn_variable_mode_ubo
||
1762 ptr
->mode
== vtn_variable_mode_ssbo
);
1763 return nir_vec2(&b
->nb
, ptr
->block_index
, ptr
->offset
);
1765 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
);
1769 if (vtn_pointer_is_external_block(b
, ptr
) &&
1770 vtn_type_contains_block(b
, ptr
->type
) &&
1771 ptr
->mode
!= vtn_variable_mode_phys_ssbo
) {
1772 const unsigned bit_size
= glsl_get_bit_size(ptr
->ptr_type
->type
);
1773 const unsigned num_components
=
1774 glsl_get_vector_elements(ptr
->ptr_type
->type
);
1776 /* In this case, we're looking for a block index and not an actual
1779 * For PhysicalStorageBufferEXT pointers, we don't have a block index
1780 * at all because we get the pointer directly from the client. This
1781 * assumes that there will never be a SSBO binding variable using the
1782 * PhysicalStorageBufferEXT storage class. This assumption appears
1783 * to be correct according to the Vulkan spec because the table,
1784 * "Shader Resource and Storage Class Correspondence," the only the
1785 * Uniform storage class with BufferBlock or the StorageBuffer
1786 * storage class with Block can be used.
1788 if (!ptr
->block_index
) {
1789 /* If we don't have a block_index then we must be a pointer to the
1792 vtn_assert(!ptr
->deref
);
1794 struct vtn_access_chain chain
= {
1797 ptr
= vtn_nir_deref_pointer_dereference(b
, ptr
, &chain
);
1800 /* A block index is just a 32-bit value but the pointer has some
1801 * other dimensionality. Cram it in there and we'll unpack it later
1802 * in vtn_pointer_from_ssa.
1804 const unsigned swiz
[4] = { 0, };
1805 return nir_swizzle(&b
->nb
, nir_u2u(&b
->nb
, ptr
->block_index
, bit_size
),
1806 swiz
, num_components
, false);
1808 return &vtn_pointer_to_deref(b
, ptr
)->dest
.ssa
;
1813 struct vtn_pointer
*
1814 vtn_pointer_from_ssa(struct vtn_builder
*b
, nir_ssa_def
*ssa
,
1815 struct vtn_type
*ptr_type
)
1817 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1819 struct vtn_type
*interface_type
= ptr_type
->deref
;
1820 while (interface_type
->base_type
== vtn_base_type_array
)
1821 interface_type
= interface_type
->array_element
;
1823 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
1824 nir_variable_mode nir_mode
;
1825 ptr
->mode
= vtn_storage_class_to_mode(b
, ptr_type
->storage_class
,
1826 interface_type
, &nir_mode
);
1827 ptr
->type
= ptr_type
->deref
;
1828 ptr
->ptr_type
= ptr_type
;
1830 if (b
->wa_glslang_179
) {
1831 /* To work around https://github.com/KhronosGroup/glslang/issues/179 we
1832 * need to whack the mode because it creates a function parameter with
1833 * the Function storage class even though it's a pointer to a sampler.
1834 * If we don't do this, then NIR won't get rid of the deref_cast for us.
1836 if (ptr
->mode
== vtn_variable_mode_function
&&
1837 (ptr
->type
->base_type
== vtn_base_type_sampler
||
1838 ptr
->type
->base_type
== vtn_base_type_sampled_image
)) {
1839 ptr
->mode
= vtn_variable_mode_uniform
;
1840 nir_mode
= nir_var_uniform
;
1844 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
1845 /* This pointer type needs to have actual storage */
1846 vtn_assert(ptr_type
->type
);
1847 if (ptr
->mode
== vtn_variable_mode_ubo
||
1848 ptr
->mode
== vtn_variable_mode_ssbo
) {
1849 vtn_assert(ssa
->num_components
== 2);
1850 ptr
->block_index
= nir_channel(&b
->nb
, ssa
, 0);
1851 ptr
->offset
= nir_channel(&b
->nb
, ssa
, 1);
1853 vtn_assert(ssa
->num_components
== 1);
1854 ptr
->block_index
= NULL
;
1858 const struct glsl_type
*deref_type
= ptr_type
->deref
->type
;
1859 if (!vtn_pointer_is_external_block(b
, ptr
)) {
1860 assert(ssa
->bit_size
== 32 && ssa
->num_components
== 1);
1861 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
1862 glsl_get_bare_type(deref_type
), 0);
1863 } else if (vtn_type_contains_block(b
, ptr
->type
) &&
1864 ptr
->mode
!= vtn_variable_mode_phys_ssbo
) {
1865 /* This is a pointer to somewhere in an array of blocks, not a
1866 * pointer to somewhere inside the block. We squashed it into a
1867 * random vector type before so just pick off the first channel and
1868 * cast it back to 32 bits.
1870 ptr
->block_index
= nir_u2u32(&b
->nb
, nir_channel(&b
->nb
, ssa
, 0));
1872 /* This is a pointer to something internal or a pointer inside a
1873 * block. It's just a regular cast.
1875 * For PhysicalStorageBufferEXT pointers, we don't have a block index
1876 * at all because we get the pointer directly from the client. This
1877 * assumes that there will never be a SSBO binding variable using the
1878 * PhysicalStorageBufferEXT storage class. This assumption appears
1879 * to be correct according to the Vulkan spec because the table,
1880 * "Shader Resource and Storage Class Correspondence," the only the
1881 * Uniform storage class with BufferBlock or the StorageBuffer
1882 * storage class with Block can be used.
1884 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
1885 ptr_type
->deref
->type
,
1887 ptr
->deref
->dest
.ssa
.num_components
=
1888 glsl_get_vector_elements(ptr_type
->type
);
1889 ptr
->deref
->dest
.ssa
.bit_size
= glsl_get_bit_size(ptr_type
->type
);
1897 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
1899 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
1902 if (var
->mode
== vtn_variable_mode_input
) {
1903 return stage
== MESA_SHADER_TESS_CTRL
||
1904 stage
== MESA_SHADER_TESS_EVAL
||
1905 stage
== MESA_SHADER_GEOMETRY
;
1908 if (var
->mode
== vtn_variable_mode_output
)
1909 return stage
== MESA_SHADER_TESS_CTRL
;
1915 assign_missing_member_locations(struct vtn_variable
*var
,
1916 bool is_vertex_input
)
1919 glsl_get_length(glsl_without_array(var
->type
->type
));
1920 int location
= var
->base_location
;
1922 for (unsigned i
= 0; i
< length
; i
++) {
1923 /* From the Vulkan spec:
1925 * “If the structure type is a Block but without a Location, then each
1926 * of its members must have a Location decoration.”
1929 if (var
->type
->block
) {
1930 assert(var
->base_location
!= -1 ||
1931 var
->var
->members
[i
].location
!= -1);
1934 /* From the Vulkan spec:
1936 * “Any member with its own Location decoration is assigned that
1937 * location. Each remaining member is assigned the location after the
1938 * immediately preceding member in declaration order.”
1940 if (var
->var
->members
[i
].location
!= -1)
1941 location
= var
->var
->members
[i
].location
;
1943 var
->var
->members
[i
].location
= location
;
1945 /* Below we use type instead of interface_type, because interface_type
1946 * is only available when it is a Block. This code also supports
1947 * input/outputs that are just structs
1949 const struct glsl_type
*member_type
=
1950 glsl_get_struct_field(glsl_without_array(var
->type
->type
), i
);
1953 glsl_count_attribute_slots(member_type
, is_vertex_input
);
1959 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
1960 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
1961 nir_constant
*initializer
)
1963 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1964 struct vtn_type
*type
= ptr_type
->deref
;
1966 struct vtn_type
*without_array
= type
;
1967 while(glsl_type_is_array(without_array
->type
))
1968 without_array
= without_array
->array_element
;
1970 enum vtn_variable_mode mode
;
1971 nir_variable_mode nir_mode
;
1972 mode
= vtn_storage_class_to_mode(b
, storage_class
, without_array
, &nir_mode
);
1975 case vtn_variable_mode_ubo
:
1976 /* There's no other way to get vtn_variable_mode_ubo */
1977 vtn_assert(without_array
->block
);
1978 b
->shader
->info
.num_ubos
++;
1980 case vtn_variable_mode_ssbo
:
1981 if (storage_class
== SpvStorageClassStorageBuffer
&&
1982 !without_array
->block
) {
1983 if (b
->variable_pointers
) {
1984 vtn_fail("Variables in the StorageBuffer storage class must "
1985 "have a struct type with the Block decoration");
1987 /* If variable pointers are not present, it's still malformed
1988 * SPIR-V but we can parse it and do the right thing anyway.
1989 * Since some of the 8-bit storage tests have bugs in this are,
1990 * just make it a warning for now.
1992 vtn_warn("Variables in the StorageBuffer storage class must "
1993 "have a struct type with the Block decoration");
1996 b
->shader
->info
.num_ssbos
++;
1998 case vtn_variable_mode_uniform
:
1999 if (glsl_type_is_image(without_array
->type
))
2000 b
->shader
->info
.num_images
++;
2001 else if (glsl_type_is_sampler(without_array
->type
))
2002 b
->shader
->info
.num_textures
++;
2004 case vtn_variable_mode_push_constant
:
2005 b
->shader
->num_uniforms
= vtn_type_block_size(b
, type
);
2008 case vtn_variable_mode_phys_ssbo
:
2009 vtn_fail("Cannot create a variable with the "
2010 "PhysicalStorageBufferEXT storage class");
2014 /* No tallying is needed */
2018 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
2021 var
->base_location
= -1;
2023 vtn_assert(val
->value_type
== vtn_value_type_pointer
);
2024 val
->pointer
= vtn_pointer_for_variable(b
, var
, ptr_type
);
2026 switch (var
->mode
) {
2027 case vtn_variable_mode_function
:
2028 case vtn_variable_mode_private
:
2029 case vtn_variable_mode_uniform
:
2030 /* For these, we create the variable normally */
2031 var
->var
= rzalloc(b
->shader
, nir_variable
);
2032 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2034 if (storage_class
== SpvStorageClassAtomicCounter
) {
2035 /* Need to tweak the nir type here as at vtn_handle_type we don't
2036 * have the access to storage_class, that is the one that points us
2037 * that is an atomic uint.
2039 var
->var
->type
= repair_atomic_type(var
->type
->type
);
2041 /* Private variables don't have any explicit layout but some layouts
2042 * may have leaked through due to type deduplication in the SPIR-V.
2044 var
->var
->type
= glsl_get_bare_type(var
->type
->type
);
2046 var
->var
->data
.mode
= nir_mode
;
2047 var
->var
->data
.location
= -1;
2048 var
->var
->interface_type
= NULL
;
2051 case vtn_variable_mode_workgroup
:
2052 if (b
->options
->lower_workgroup_access_to_offsets
) {
2053 var
->shared_location
= -1;
2055 /* Create the variable normally */
2056 var
->var
= rzalloc(b
->shader
, nir_variable
);
2057 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2058 /* Workgroup variables don't have any explicit layout but some
2059 * layouts may have leaked through due to type deduplication in the
2062 var
->var
->type
= glsl_get_bare_type(var
->type
->type
);
2063 var
->var
->data
.mode
= nir_var_mem_shared
;
2067 case vtn_variable_mode_input
:
2068 case vtn_variable_mode_output
: {
2069 /* In order to know whether or not we're a per-vertex inout, we need
2070 * the patch qualifier. This means walking the variable decorations
2071 * early before we actually create any variables. Not a big deal.
2073 * GLSLang really likes to place decorations in the most interior
2074 * thing it possibly can. In particular, if you have a struct, it
2075 * will place the patch decorations on the struct members. This
2076 * should be handled by the variable splitting below just fine.
2078 * If you have an array-of-struct, things get even more weird as it
2079 * will place the patch decorations on the struct even though it's
2080 * inside an array and some of the members being patch and others not
2081 * makes no sense whatsoever. Since the only sensible thing is for
2082 * it to be all or nothing, we'll call it patch if any of the members
2083 * are declared patch.
2086 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
2087 if (glsl_type_is_array(var
->type
->type
) &&
2088 glsl_type_is_struct(without_array
->type
)) {
2089 vtn_foreach_decoration(b
, vtn_value(b
, without_array
->id
,
2090 vtn_value_type_type
),
2091 var_is_patch_cb
, &var
->patch
);
2094 /* For inputs and outputs, we immediately split structures. This
2095 * is for a couple of reasons. For one, builtins may all come in
2096 * a struct and we really want those split out into separate
2097 * variables. For another, interpolation qualifiers can be
2098 * applied to members of the top-level struct ane we need to be
2099 * able to preserve that information.
2102 struct vtn_type
*per_vertex_type
= var
->type
;
2103 if (is_per_vertex_inout(var
, b
->shader
->info
.stage
)) {
2104 /* In Geometry shaders (and some tessellation), inputs come
2105 * in per-vertex arrays. However, some builtins come in
2106 * non-per-vertex, hence the need for the is_array check. In
2107 * any case, there are no non-builtin arrays allowed so this
2108 * check should be sufficient.
2110 per_vertex_type
= var
->type
->array_element
;
2113 var
->var
= rzalloc(b
->shader
, nir_variable
);
2114 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2115 /* In Vulkan, shader I/O variables don't have any explicit layout but
2116 * some layouts may have leaked through due to type deduplication in
2117 * the SPIR-V. We do, however, keep the layouts in the variable's
2118 * interface_type because we need offsets for XFB arrays of blocks.
2120 var
->var
->type
= glsl_get_bare_type(var
->type
->type
);
2121 var
->var
->data
.mode
= nir_mode
;
2122 var
->var
->data
.patch
= var
->patch
;
2124 /* Figure out the interface block type. */
2125 struct vtn_type
*iface_type
= per_vertex_type
;
2126 if (var
->mode
== vtn_variable_mode_output
&&
2127 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
2128 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
||
2129 b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)) {
2130 /* For vertex data outputs, we can end up with arrays of blocks for
2131 * transform feedback where each array element corresponds to a
2132 * different XFB output buffer.
2134 while (iface_type
->base_type
== vtn_base_type_array
)
2135 iface_type
= iface_type
->array_element
;
2137 if (iface_type
->base_type
== vtn_base_type_struct
&& iface_type
->block
)
2138 var
->var
->interface_type
= iface_type
->type
;
2140 if (per_vertex_type
->base_type
== vtn_base_type_struct
&&
2141 per_vertex_type
->block
) {
2142 /* It's a struct. Set it up as per-member. */
2143 var
->var
->num_members
= glsl_get_length(per_vertex_type
->type
);
2144 var
->var
->members
= rzalloc_array(var
->var
, struct nir_variable_data
,
2145 var
->var
->num_members
);
2147 for (unsigned i
= 0; i
< var
->var
->num_members
; i
++) {
2148 var
->var
->members
[i
].mode
= nir_mode
;
2149 var
->var
->members
[i
].patch
= var
->patch
;
2150 var
->var
->members
[i
].location
= -1;
2154 /* For inputs and outputs, we need to grab locations and builtin
2155 * information from the per-vertex type.
2157 vtn_foreach_decoration(b
, vtn_value(b
, per_vertex_type
->id
,
2158 vtn_value_type_type
),
2159 var_decoration_cb
, var
);
2163 case vtn_variable_mode_ubo
:
2164 case vtn_variable_mode_ssbo
:
2165 case vtn_variable_mode_push_constant
:
2166 case vtn_variable_mode_cross_workgroup
:
2167 /* These don't need actual variables. */
2170 case vtn_variable_mode_phys_ssbo
:
2171 unreachable("Should have been caught before");
2175 var
->var
->constant_initializer
=
2176 nir_constant_clone(initializer
, var
->var
);
2179 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
2181 if ((var
->mode
== vtn_variable_mode_input
||
2182 var
->mode
== vtn_variable_mode_output
) &&
2183 var
->var
->members
) {
2184 bool is_vertex_input
= (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
&&
2185 var
->mode
== vtn_variable_mode_input
);
2186 assign_missing_member_locations(var
, is_vertex_input
);
2189 if (var
->mode
== vtn_variable_mode_uniform
) {
2190 /* XXX: We still need the binding information in the nir_variable
2191 * for these. We should fix that.
2193 var
->var
->data
.binding
= var
->binding
;
2194 var
->var
->data
.explicit_binding
= var
->explicit_binding
;
2195 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
2196 var
->var
->data
.index
= var
->input_attachment_index
;
2197 var
->var
->data
.offset
= var
->offset
;
2199 if (glsl_type_is_image(without_array
->type
))
2200 var
->var
->data
.image
.format
= without_array
->image_format
;
2203 if (var
->mode
== vtn_variable_mode_function
) {
2204 vtn_assert(var
->var
!= NULL
&& var
->var
->members
== NULL
);
2205 nir_function_impl_add_variable(b
->nb
.impl
, var
->var
);
2206 } else if (var
->var
) {
2207 nir_shader_add_variable(b
->shader
, var
->var
);
2209 vtn_assert(vtn_pointer_is_external_block(b
, val
->pointer
));
2214 vtn_assert_types_equal(struct vtn_builder
*b
, SpvOp opcode
,
2215 struct vtn_type
*dst_type
,
2216 struct vtn_type
*src_type
)
2218 if (dst_type
->id
== src_type
->id
)
2221 if (vtn_types_compatible(b
, dst_type
, src_type
)) {
2222 /* Early versions of GLSLang would re-emit types unnecessarily and you
2223 * would end up with OpLoad, OpStore, or OpCopyMemory opcodes which have
2224 * mismatched source and destination types.
2226 * https://github.com/KhronosGroup/glslang/issues/304
2227 * https://github.com/KhronosGroup/glslang/issues/307
2228 * https://bugs.freedesktop.org/show_bug.cgi?id=104338
2229 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2231 vtn_warn("Source and destination types of %s do not have the same "
2232 "ID (but are compatible): %u vs %u",
2233 spirv_op_to_string(opcode
), dst_type
->id
, src_type
->id
);
2237 vtn_fail("Source and destination types of %s do not match: %s vs. %s",
2238 spirv_op_to_string(opcode
),
2239 glsl_get_type_name(dst_type
->type
),
2240 glsl_get_type_name(src_type
->type
));
2243 static nir_ssa_def
*
2244 nir_shrink_zero_pad_vec(nir_builder
*b
, nir_ssa_def
*val
,
2245 unsigned num_components
)
2247 if (val
->num_components
== num_components
)
2250 nir_ssa_def
*comps
[NIR_MAX_VEC_COMPONENTS
];
2251 for (unsigned i
= 0; i
< num_components
; i
++) {
2252 if (i
< val
->num_components
)
2253 comps
[i
] = nir_channel(b
, val
, i
);
2255 comps
[i
] = nir_imm_intN_t(b
, 0, val
->bit_size
);
2257 return nir_vec(b
, comps
, num_components
);
2260 static nir_ssa_def
*
2261 nir_sloppy_bitcast(nir_builder
*b
, nir_ssa_def
*val
,
2262 const struct glsl_type
*type
)
2264 const unsigned num_components
= glsl_get_vector_elements(type
);
2265 const unsigned bit_size
= glsl_get_bit_size(type
);
2267 /* First, zero-pad to ensure that the value is big enough that when we
2268 * bit-cast it, we don't loose anything.
2270 if (val
->bit_size
< bit_size
) {
2271 const unsigned src_num_components_needed
=
2272 vtn_align_u32(val
->num_components
, bit_size
/ val
->bit_size
);
2273 val
= nir_shrink_zero_pad_vec(b
, val
, src_num_components_needed
);
2276 val
= nir_bitcast_vector(b
, val
, bit_size
);
2278 return nir_shrink_zero_pad_vec(b
, val
, num_components
);
2282 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
2283 const uint32_t *w
, unsigned count
)
2287 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
2288 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2292 case SpvOpVariable
: {
2293 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2295 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2297 SpvStorageClass storage_class
= w
[3];
2298 nir_constant
*initializer
= NULL
;
2300 initializer
= vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
2302 vtn_create_variable(b
, val
, ptr_type
, storage_class
, initializer
);
2306 case SpvOpAccessChain
:
2307 case SpvOpPtrAccessChain
:
2308 case SpvOpInBoundsAccessChain
: {
2309 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
2310 chain
->ptr_as_array
= (opcode
== SpvOpPtrAccessChain
);
2313 for (int i
= 4; i
< count
; i
++) {
2314 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
2315 if (link_val
->value_type
== vtn_value_type_constant
) {
2316 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
2317 switch (glsl_get_bit_size(link_val
->type
->type
)) {
2319 chain
->link
[idx
].id
= link_val
->constant
->values
[0].i8
[0];
2322 chain
->link
[idx
].id
= link_val
->constant
->values
[0].i16
[0];
2325 chain
->link
[idx
].id
= link_val
->constant
->values
[0].i32
[0];
2328 chain
->link
[idx
].id
= link_val
->constant
->values
[0].i64
[0];
2331 vtn_fail("Invalid bit size");
2334 chain
->link
[idx
].mode
= vtn_access_mode_id
;
2335 chain
->link
[idx
].id
= w
[i
];
2341 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2342 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
2343 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
2344 /* This is rather insane. SPIR-V allows you to use OpSampledImage
2345 * to combine an array of images with a single sampler to get an
2346 * array of sampled images that all share the same sampler.
2347 * Fortunately, this means that we can more-or-less ignore the
2348 * sampler when crawling the access chain, but it does leave us
2349 * with this rather awkward little special-case.
2351 struct vtn_value
*val
=
2352 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2353 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2354 val
->sampled_image
->type
= base_val
->sampled_image
->type
;
2355 val
->sampled_image
->image
=
2356 vtn_pointer_dereference(b
, base_val
->sampled_image
->image
, chain
);
2357 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
2359 vtn_assert(base_val
->value_type
== vtn_value_type_pointer
);
2360 struct vtn_value
*val
=
2361 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2362 val
->pointer
= vtn_pointer_dereference(b
, base_val
->pointer
, chain
);
2363 val
->pointer
->ptr_type
= ptr_type
;
2368 case SpvOpCopyMemory
: {
2369 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2370 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
2372 vtn_assert_types_equal(b
, opcode
, dest
->type
->deref
, src
->type
->deref
);
2374 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
2379 struct vtn_type
*res_type
=
2380 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2381 struct vtn_value
*src_val
= vtn_value(b
, w
[3], vtn_value_type_pointer
);
2382 struct vtn_pointer
*src
= src_val
->pointer
;
2384 vtn_assert_types_equal(b
, opcode
, res_type
, src_val
->type
->deref
);
2386 if (glsl_type_is_image(res_type
->type
) ||
2387 glsl_type_is_sampler(res_type
->type
)) {
2388 vtn_push_value(b
, w
[2], vtn_value_type_pointer
)->pointer
= src
;
2392 vtn_push_ssa(b
, w
[2], res_type
, vtn_variable_load(b
, src
));
2397 struct vtn_value
*dest_val
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2398 struct vtn_pointer
*dest
= dest_val
->pointer
;
2399 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[2]);
2401 /* OpStore requires us to actually have a storage type */
2402 vtn_fail_if(dest
->type
->type
== NULL
,
2403 "Invalid destination type for OpStore");
2405 if (glsl_get_base_type(dest
->type
->type
) == GLSL_TYPE_BOOL
&&
2406 glsl_get_base_type(src_val
->type
->type
) == GLSL_TYPE_UINT
) {
2407 /* Early versions of GLSLang would use uint types for UBOs/SSBOs but
2408 * would then store them to a local variable as bool. Work around
2409 * the issue by doing an implicit conversion.
2411 * https://github.com/KhronosGroup/glslang/issues/170
2412 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2414 vtn_warn("OpStore of value of type OpTypeInt to a pointer to type "
2415 "OpTypeBool. Doing an implicit conversion to work around "
2417 struct vtn_ssa_value
*bool_ssa
=
2418 vtn_create_ssa_value(b
, dest
->type
->type
);
2419 bool_ssa
->def
= nir_i2b(&b
->nb
, vtn_ssa_value(b
, w
[2])->def
);
2420 vtn_variable_store(b
, bool_ssa
, dest
);
2424 vtn_assert_types_equal(b
, opcode
, dest_val
->type
->deref
, src_val
->type
);
2426 if (glsl_type_is_sampler(dest
->type
->type
)) {
2427 if (b
->wa_glslang_179
) {
2428 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
2429 "propagation to workaround the problem.");
2430 vtn_assert(dest
->var
->copy_prop_sampler
== NULL
);
2431 dest
->var
->copy_prop_sampler
=
2432 vtn_value(b
, w
[2], vtn_value_type_pointer
)->pointer
;
2434 vtn_fail("Vulkan does not allow OpStore of a sampler or image.");
2439 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
2440 vtn_variable_store(b
, src
, dest
);
2444 case SpvOpArrayLength
: {
2445 struct vtn_pointer
*ptr
=
2446 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2448 const uint32_t offset
= ptr
->var
->type
->offsets
[w
[4]];
2449 const uint32_t stride
= ptr
->var
->type
->members
[w
[4]]->stride
;
2451 if (!ptr
->block_index
) {
2452 struct vtn_access_chain chain
= {
2455 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
2456 vtn_assert(ptr
->block_index
);
2459 nir_intrinsic_instr
*instr
=
2460 nir_intrinsic_instr_create(b
->nb
.shader
,
2461 nir_intrinsic_get_buffer_size
);
2462 instr
->src
[0] = nir_src_for_ssa(ptr
->block_index
);
2463 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
2464 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2465 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
2467 /* array_length = max(buffer_size - offset, 0) / stride */
2468 nir_ssa_def
*array_length
=
2473 nir_imm_int(&b
->nb
, offset
)),
2474 nir_imm_int(&b
->nb
, 0u)),
2475 nir_imm_int(&b
->nb
, stride
));
2477 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2478 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
2479 val
->ssa
->def
= array_length
;
2483 case SpvOpConvertPtrToU
: {
2484 struct vtn_value
*u_val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2486 vtn_fail_if(u_val
->type
->base_type
!= vtn_base_type_vector
&&
2487 u_val
->type
->base_type
!= vtn_base_type_scalar
,
2488 "OpConvertPtrToU can only be used to cast to a vector or "
2491 /* The pointer will be converted to an SSA value automatically */
2492 nir_ssa_def
*ptr_ssa
= vtn_ssa_value(b
, w
[3])->def
;
2494 u_val
->ssa
= vtn_create_ssa_value(b
, u_val
->type
->type
);
2495 u_val
->ssa
->def
= nir_sloppy_bitcast(&b
->nb
, ptr_ssa
, u_val
->type
->type
);
2499 case SpvOpConvertUToPtr
: {
2500 struct vtn_value
*ptr_val
=
2501 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2502 struct vtn_value
*u_val
= vtn_value(b
, w
[3], vtn_value_type_ssa
);
2504 vtn_fail_if(ptr_val
->type
->type
== NULL
,
2505 "OpConvertUToPtr can only be used on physical pointers");
2507 vtn_fail_if(u_val
->type
->base_type
!= vtn_base_type_vector
&&
2508 u_val
->type
->base_type
!= vtn_base_type_scalar
,
2509 "OpConvertUToPtr can only be used to cast from a vector or "
2512 nir_ssa_def
*ptr_ssa
= nir_sloppy_bitcast(&b
->nb
, u_val
->ssa
->def
,
2513 ptr_val
->type
->type
);
2514 ptr_val
->pointer
= vtn_pointer_from_ssa(b
, ptr_ssa
, ptr_val
->type
);
2518 case SpvOpCopyMemorySized
:
2520 vtn_fail("Unhandled opcode");