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>
34 ptr_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
35 const struct vtn_decoration
*dec
, void *void_ptr
)
37 struct vtn_pointer
*ptr
= void_ptr
;
39 switch (dec
->decoration
) {
40 case SpvDecorationNonUniformEXT
:
41 ptr
->access
|= ACCESS_NON_UNIFORM
;
49 static struct vtn_pointer
*
50 vtn_decorate_pointer(struct vtn_builder
*b
, struct vtn_value
*val
,
51 struct vtn_pointer
*ptr
)
53 struct vtn_pointer dummy
= { .access
= 0 };
54 vtn_foreach_decoration(b
, val
, ptr_decoration_cb
, &dummy
);
56 /* If we're adding access flags, make a copy of the pointer. We could
57 * probably just OR them in without doing so but this prevents us from
58 * leaking them any further than actually specified in the SPIR-V.
60 if (dummy
.access
& ~ptr
->access
) {
61 struct vtn_pointer
*copy
= ralloc(b
, struct vtn_pointer
);
63 copy
->access
|= dummy
.access
;
71 vtn_push_pointer(struct vtn_builder
*b
, uint32_t value_id
,
72 struct vtn_pointer
*ptr
)
74 struct vtn_value
*val
= vtn_push_value(b
, value_id
, vtn_value_type_pointer
);
75 val
->pointer
= vtn_decorate_pointer(b
, val
, ptr
);
80 vtn_push_ssa(struct vtn_builder
*b
, uint32_t value_id
,
81 struct vtn_type
*type
, struct vtn_ssa_value
*ssa
)
83 struct vtn_value
*val
;
84 if (type
->base_type
== vtn_base_type_pointer
) {
85 val
= vtn_push_pointer(b
, value_id
, vtn_pointer_from_ssa(b
, ssa
->def
, type
));
87 val
= vtn_push_value(b
, value_id
, vtn_value_type_ssa
);
94 vtn_copy_value(struct vtn_builder
*b
, uint32_t src_value_id
,
95 uint32_t dst_value_id
)
97 struct vtn_value
*src
= vtn_untyped_value(b
, src_value_id
);
98 struct vtn_value
*dst
= vtn_push_value(b
, dst_value_id
, src
->value_type
);
99 struct vtn_value src_copy
= *src
;
101 vtn_fail_if(dst
->type
->id
!= src
->type
->id
,
102 "Result Type must equal Operand type");
104 src_copy
.name
= dst
->name
;
105 src_copy
.decoration
= dst
->decoration
;
106 src_copy
.type
= dst
->type
;
109 if (dst
->value_type
== vtn_value_type_pointer
)
110 dst
->pointer
= vtn_decorate_pointer(b
, dst
, dst
->pointer
);
113 static struct vtn_access_chain
*
114 vtn_access_chain_create(struct vtn_builder
*b
, unsigned length
)
116 struct vtn_access_chain
*chain
;
118 /* Subtract 1 from the length since there's already one built in */
119 size_t size
= sizeof(*chain
) +
120 (MAX2(length
, 1) - 1) * sizeof(chain
->link
[0]);
121 chain
= rzalloc_size(b
, size
);
122 chain
->length
= length
;
128 vtn_mode_uses_ssa_offset(struct vtn_builder
*b
,
129 enum vtn_variable_mode mode
)
131 return ((mode
== vtn_variable_mode_ubo
||
132 mode
== vtn_variable_mode_ssbo
) &&
133 b
->options
->lower_ubo_ssbo_access_to_offsets
) ||
134 mode
== vtn_variable_mode_push_constant
;
138 vtn_pointer_is_external_block(struct vtn_builder
*b
,
139 struct vtn_pointer
*ptr
)
141 return ptr
->mode
== vtn_variable_mode_ssbo
||
142 ptr
->mode
== vtn_variable_mode_ubo
||
143 ptr
->mode
== vtn_variable_mode_phys_ssbo
||
144 ptr
->mode
== vtn_variable_mode_push_constant
;
148 vtn_access_link_as_ssa(struct vtn_builder
*b
, struct vtn_access_link link
,
149 unsigned stride
, unsigned bit_size
)
151 vtn_assert(stride
> 0);
152 if (link
.mode
== vtn_access_mode_literal
) {
153 return nir_imm_intN_t(&b
->nb
, link
.id
* stride
, bit_size
);
155 nir_ssa_def
*ssa
= vtn_ssa_value(b
, link
.id
)->def
;
156 if (ssa
->bit_size
!= bit_size
)
157 ssa
= nir_i2i(&b
->nb
, ssa
, bit_size
);
158 return nir_imul_imm(&b
->nb
, ssa
, stride
);
162 static VkDescriptorType
163 vk_desc_type_for_mode(struct vtn_builder
*b
, enum vtn_variable_mode mode
)
166 case vtn_variable_mode_ubo
:
167 return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
;
168 case vtn_variable_mode_ssbo
:
169 return VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
;
171 vtn_fail("Invalid mode for vulkan_resource_index");
176 vtn_variable_resource_index(struct vtn_builder
*b
, struct vtn_variable
*var
,
177 nir_ssa_def
*desc_array_index
)
179 vtn_assert(b
->options
->environment
== NIR_SPIRV_VULKAN
);
181 if (!desc_array_index
) {
182 vtn_assert(glsl_type_is_struct_or_ifc(var
->type
->type
));
183 desc_array_index
= nir_imm_int(&b
->nb
, 0);
186 nir_intrinsic_instr
*instr
=
187 nir_intrinsic_instr_create(b
->nb
.shader
,
188 nir_intrinsic_vulkan_resource_index
);
189 instr
->src
[0] = nir_src_for_ssa(desc_array_index
);
190 nir_intrinsic_set_desc_set(instr
, var
->descriptor_set
);
191 nir_intrinsic_set_binding(instr
, var
->binding
);
192 nir_intrinsic_set_desc_type(instr
, vk_desc_type_for_mode(b
, var
->mode
));
194 vtn_fail_if(var
->mode
!= vtn_variable_mode_ubo
&&
195 var
->mode
!= vtn_variable_mode_ssbo
,
196 "Invalid mode for vulkan_resource_index");
198 nir_address_format addr_format
= vtn_mode_to_address_format(b
, var
->mode
);
199 const struct glsl_type
*index_type
=
200 b
->options
->lower_ubo_ssbo_access_to_offsets
?
201 glsl_uint_type() : nir_address_format_to_glsl_type(addr_format
);
203 instr
->num_components
= glsl_get_vector_elements(index_type
);
204 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, instr
->num_components
,
205 glsl_get_bit_size(index_type
), NULL
);
206 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
208 return &instr
->dest
.ssa
;
212 vtn_resource_reindex(struct vtn_builder
*b
, enum vtn_variable_mode mode
,
213 nir_ssa_def
*base_index
, nir_ssa_def
*offset_index
)
215 vtn_assert(b
->options
->environment
== NIR_SPIRV_VULKAN
);
217 nir_intrinsic_instr
*instr
=
218 nir_intrinsic_instr_create(b
->nb
.shader
,
219 nir_intrinsic_vulkan_resource_reindex
);
220 instr
->src
[0] = nir_src_for_ssa(base_index
);
221 instr
->src
[1] = nir_src_for_ssa(offset_index
);
222 nir_intrinsic_set_desc_type(instr
, vk_desc_type_for_mode(b
, mode
));
224 vtn_fail_if(mode
!= vtn_variable_mode_ubo
&& mode
!= vtn_variable_mode_ssbo
,
225 "Invalid mode for vulkan_resource_reindex");
227 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
228 const struct glsl_type
*index_type
=
229 b
->options
->lower_ubo_ssbo_access_to_offsets
?
230 glsl_uint_type() : nir_address_format_to_glsl_type(addr_format
);
232 instr
->num_components
= glsl_get_vector_elements(index_type
);
233 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, instr
->num_components
,
234 glsl_get_bit_size(index_type
), NULL
);
235 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
237 return &instr
->dest
.ssa
;
241 vtn_descriptor_load(struct vtn_builder
*b
, enum vtn_variable_mode mode
,
242 nir_ssa_def
*desc_index
)
244 vtn_assert(b
->options
->environment
== NIR_SPIRV_VULKAN
);
246 nir_intrinsic_instr
*desc_load
=
247 nir_intrinsic_instr_create(b
->nb
.shader
,
248 nir_intrinsic_load_vulkan_descriptor
);
249 desc_load
->src
[0] = nir_src_for_ssa(desc_index
);
250 nir_intrinsic_set_desc_type(desc_load
, vk_desc_type_for_mode(b
, mode
));
252 vtn_fail_if(mode
!= vtn_variable_mode_ubo
&& mode
!= vtn_variable_mode_ssbo
,
253 "Invalid mode for load_vulkan_descriptor");
255 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
256 const struct glsl_type
*ptr_type
=
257 nir_address_format_to_glsl_type(addr_format
);
259 desc_load
->num_components
= glsl_get_vector_elements(ptr_type
);
260 nir_ssa_dest_init(&desc_load
->instr
, &desc_load
->dest
,
261 desc_load
->num_components
,
262 glsl_get_bit_size(ptr_type
), NULL
);
263 nir_builder_instr_insert(&b
->nb
, &desc_load
->instr
);
265 return &desc_load
->dest
.ssa
;
268 /* Dereference the given base pointer by the access chain */
269 static struct vtn_pointer
*
270 vtn_nir_deref_pointer_dereference(struct vtn_builder
*b
,
271 struct vtn_pointer
*base
,
272 struct vtn_access_chain
*deref_chain
)
274 struct vtn_type
*type
= base
->type
;
275 enum gl_access_qualifier access
= base
->access
| deref_chain
->access
;
278 nir_deref_instr
*tail
;
281 } else if (b
->options
->environment
== NIR_SPIRV_VULKAN
&&
282 vtn_pointer_is_external_block(b
, base
)) {
283 nir_ssa_def
*block_index
= base
->block_index
;
285 /* We dereferencing an external block pointer. Correctness of this
286 * operation relies on one particular line in the SPIR-V spec, section
287 * entitled "Validation Rules for Shader Capabilities":
289 * "Block and BufferBlock decorations cannot decorate a structure
290 * type that is nested at any level inside another structure type
291 * decorated with Block or BufferBlock."
293 * This means that we can detect the point where we cross over from
294 * descriptor indexing to buffer indexing by looking for the block
295 * decorated struct type. Anything before the block decorated struct
296 * type is a descriptor indexing operation and anything after the block
297 * decorated struct is a buffer offset operation.
300 /* Figure out the descriptor array index if any
302 * Some of the Vulkan CTS tests with hand-rolled SPIR-V have been known
303 * to forget the Block or BufferBlock decoration from time to time.
304 * It's more robust if we check for both !block_index and for the type
305 * to contain a block. This way there's a decent chance that arrays of
306 * UBOs/SSBOs will work correctly even if variable pointers are
309 nir_ssa_def
*desc_arr_idx
= NULL
;
310 if (!block_index
|| vtn_type_contains_block(b
, type
)) {
311 /* If our type contains a block, then we're still outside the block
312 * and we need to process enough levels of dereferences to get inside
315 if (deref_chain
->ptr_as_array
) {
316 unsigned aoa_size
= glsl_get_aoa_size(type
->type
);
317 desc_arr_idx
= vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
318 MAX2(aoa_size
, 1), 32);
322 for (; idx
< deref_chain
->length
; idx
++) {
323 if (type
->base_type
!= vtn_base_type_array
) {
324 vtn_assert(type
->base_type
== vtn_base_type_struct
);
328 unsigned aoa_size
= glsl_get_aoa_size(type
->array_element
->type
);
329 nir_ssa_def
*arr_offset
=
330 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
331 MAX2(aoa_size
, 1), 32);
333 desc_arr_idx
= nir_iadd(&b
->nb
, desc_arr_idx
, arr_offset
);
335 desc_arr_idx
= arr_offset
;
337 type
= type
->array_element
;
338 access
|= type
->access
;
343 vtn_assert(base
->var
&& base
->type
);
344 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
345 } else if (desc_arr_idx
) {
346 block_index
= vtn_resource_reindex(b
, base
->mode
,
347 block_index
, desc_arr_idx
);
350 if (idx
== deref_chain
->length
) {
351 /* The entire deref was consumed in finding the block index. Return
352 * a pointer which just has a block index and a later access chain
353 * will dereference deeper.
355 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
356 ptr
->mode
= base
->mode
;
358 ptr
->block_index
= block_index
;
359 ptr
->access
= access
;
363 /* If we got here, there's more access chain to handle and we have the
364 * final block index. Insert a descriptor load and cast to a deref to
365 * start the deref chain.
367 nir_ssa_def
*desc
= vtn_descriptor_load(b
, base
->mode
, block_index
);
369 assert(base
->mode
== vtn_variable_mode_ssbo
||
370 base
->mode
== vtn_variable_mode_ubo
);
371 nir_variable_mode nir_mode
=
372 base
->mode
== vtn_variable_mode_ssbo
? nir_var_mem_ssbo
: nir_var_mem_ubo
;
374 tail
= nir_build_deref_cast(&b
->nb
, desc
, nir_mode
, type
->type
,
375 base
->ptr_type
->stride
);
377 assert(base
->var
&& base
->var
->var
);
378 tail
= nir_build_deref_var(&b
->nb
, base
->var
->var
);
379 if (base
->ptr_type
&& base
->ptr_type
->type
) {
380 tail
->dest
.ssa
.num_components
=
381 glsl_get_vector_elements(base
->ptr_type
->type
);
382 tail
->dest
.ssa
.bit_size
= glsl_get_bit_size(base
->ptr_type
->type
);
386 if (idx
== 0 && deref_chain
->ptr_as_array
) {
387 /* We start with a deref cast to get the stride. Hopefully, we'll be
388 * able to delete that cast eventually.
390 tail
= nir_build_deref_cast(&b
->nb
, &tail
->dest
.ssa
, tail
->mode
,
391 tail
->type
, base
->ptr_type
->stride
);
393 nir_ssa_def
*index
= vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1,
394 tail
->dest
.ssa
.bit_size
);
395 tail
= nir_build_deref_ptr_as_array(&b
->nb
, tail
, index
);
399 for (; idx
< deref_chain
->length
; idx
++) {
400 if (glsl_type_is_struct_or_ifc(type
->type
)) {
401 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
402 unsigned field
= deref_chain
->link
[idx
].id
;
403 tail
= nir_build_deref_struct(&b
->nb
, tail
, field
);
404 type
= type
->members
[field
];
406 nir_ssa_def
*arr_index
=
407 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
], 1,
408 tail
->dest
.ssa
.bit_size
);
409 tail
= nir_build_deref_array(&b
->nb
, tail
, arr_index
);
410 type
= type
->array_element
;
413 access
|= type
->access
;
416 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
417 ptr
->mode
= base
->mode
;
419 ptr
->var
= base
->var
;
421 ptr
->access
= access
;
426 static struct vtn_pointer
*
427 vtn_ssa_offset_pointer_dereference(struct vtn_builder
*b
,
428 struct vtn_pointer
*base
,
429 struct vtn_access_chain
*deref_chain
)
431 nir_ssa_def
*block_index
= base
->block_index
;
432 nir_ssa_def
*offset
= base
->offset
;
433 struct vtn_type
*type
= base
->type
;
434 enum gl_access_qualifier access
= base
->access
;
437 if (base
->mode
== vtn_variable_mode_ubo
||
438 base
->mode
== vtn_variable_mode_ssbo
) {
440 vtn_assert(base
->var
&& base
->type
);
441 nir_ssa_def
*desc_arr_idx
;
442 if (glsl_type_is_array(type
->type
)) {
443 if (deref_chain
->length
>= 1) {
445 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
447 /* This consumes a level of type */
448 type
= type
->array_element
;
449 access
|= type
->access
;
451 /* This is annoying. We've been asked for a pointer to the
452 * array of UBOs/SSBOs and not a specifc buffer. Return a
453 * pointer with a descriptor index of 0 and we'll have to do
454 * a reindex later to adjust it to the right thing.
456 desc_arr_idx
= nir_imm_int(&b
->nb
, 0);
458 } else if (deref_chain
->ptr_as_array
) {
459 /* You can't have a zero-length OpPtrAccessChain */
460 vtn_assert(deref_chain
->length
>= 1);
461 desc_arr_idx
= vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
463 /* We have a regular non-array SSBO. */
466 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
467 } else if (deref_chain
->ptr_as_array
&&
468 type
->base_type
== vtn_base_type_struct
&& type
->block
) {
469 /* We are doing an OpPtrAccessChain on a pointer to a struct that is
470 * decorated block. This is an interesting corner in the SPIR-V
471 * spec. One interpretation would be that they client is clearly
472 * trying to treat that block as if it's an implicit array of blocks
473 * repeated in the buffer. However, the SPIR-V spec for the
474 * OpPtrAccessChain says:
476 * "Base is treated as the address of the first element of an
477 * array, and the Element element’s address is computed to be the
478 * base for the Indexes, as per OpAccessChain."
480 * Taken literally, that would mean that your struct type is supposed
481 * to be treated as an array of such a struct and, since it's
482 * decorated block, that means an array of blocks which corresponds
483 * to an array descriptor. Therefore, we need to do a reindex
484 * operation to add the index from the first link in the access chain
485 * to the index we recieved.
487 * The downside to this interpretation (there always is one) is that
488 * this might be somewhat surprising behavior to apps if they expect
489 * the implicit array behavior described above.
491 vtn_assert(deref_chain
->length
>= 1);
492 nir_ssa_def
*offset_index
=
493 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
496 block_index
= vtn_resource_reindex(b
, base
->mode
,
497 block_index
, offset_index
);
502 if (base
->mode
== vtn_variable_mode_workgroup
) {
503 /* SLM doesn't need nor have a block index */
504 vtn_assert(!block_index
);
506 /* We need the variable for the base offset */
507 vtn_assert(base
->var
);
509 /* We need ptr_type for size and alignment */
510 vtn_assert(base
->ptr_type
);
512 /* Assign location on first use so that we don't end up bloating SLM
513 * address space for variables which are never statically used.
515 if (base
->var
->shared_location
< 0) {
516 vtn_assert(base
->ptr_type
->length
> 0 && base
->ptr_type
->align
> 0);
517 b
->shader
->num_shared
= vtn_align_u32(b
->shader
->num_shared
,
518 base
->ptr_type
->align
);
519 base
->var
->shared_location
= b
->shader
->num_shared
;
520 b
->shader
->num_shared
+= base
->ptr_type
->length
;
523 offset
= nir_imm_int(&b
->nb
, base
->var
->shared_location
);
524 } else if (base
->mode
== vtn_variable_mode_push_constant
) {
525 /* Push constants neither need nor have a block index */
526 vtn_assert(!block_index
);
528 /* Start off with at the start of the push constant block. */
529 offset
= nir_imm_int(&b
->nb
, 0);
531 /* The code above should have ensured a block_index when needed. */
532 vtn_assert(block_index
);
534 /* Start off with at the start of the buffer. */
535 offset
= nir_imm_int(&b
->nb
, 0);
539 if (deref_chain
->ptr_as_array
&& idx
== 0) {
540 /* We need ptr_type for the stride */
541 vtn_assert(base
->ptr_type
);
543 /* We need at least one element in the chain */
544 vtn_assert(deref_chain
->length
>= 1);
546 nir_ssa_def
*elem_offset
=
547 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
548 base
->ptr_type
->stride
, offset
->bit_size
);
549 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
553 for (; idx
< deref_chain
->length
; idx
++) {
554 switch (glsl_get_base_type(type
->type
)) {
557 case GLSL_TYPE_UINT16
:
558 case GLSL_TYPE_INT16
:
559 case GLSL_TYPE_UINT8
:
561 case GLSL_TYPE_UINT64
:
562 case GLSL_TYPE_INT64
:
563 case GLSL_TYPE_FLOAT
:
564 case GLSL_TYPE_FLOAT16
:
565 case GLSL_TYPE_DOUBLE
:
567 case GLSL_TYPE_ARRAY
: {
568 nir_ssa_def
*elem_offset
=
569 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
570 type
->stride
, offset
->bit_size
);
571 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
572 type
= type
->array_element
;
573 access
|= type
->access
;
577 case GLSL_TYPE_INTERFACE
:
578 case GLSL_TYPE_STRUCT
: {
579 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
580 unsigned member
= deref_chain
->link
[idx
].id
;
581 offset
= nir_iadd_imm(&b
->nb
, offset
, type
->offsets
[member
]);
582 type
= type
->members
[member
];
583 access
|= type
->access
;
588 vtn_fail("Invalid type for deref");
592 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
593 ptr
->mode
= base
->mode
;
595 ptr
->block_index
= block_index
;
596 ptr
->offset
= offset
;
597 ptr
->access
= access
;
602 /* Dereference the given base pointer by the access chain */
603 static struct vtn_pointer
*
604 vtn_pointer_dereference(struct vtn_builder
*b
,
605 struct vtn_pointer
*base
,
606 struct vtn_access_chain
*deref_chain
)
608 if (vtn_pointer_uses_ssa_offset(b
, base
)) {
609 return vtn_ssa_offset_pointer_dereference(b
, base
, deref_chain
);
611 return vtn_nir_deref_pointer_dereference(b
, base
, deref_chain
);
615 /* Returns an atomic_uint type based on the original uint type. The returned
616 * type will be equivalent to the original one but will have an atomic_uint
617 * type as leaf instead of an uint.
619 * Manages uint scalars, arrays, and arrays of arrays of any nested depth.
621 static const struct glsl_type
*
622 repair_atomic_type(const struct glsl_type
*type
)
624 assert(glsl_get_base_type(glsl_without_array(type
)) == GLSL_TYPE_UINT
);
625 assert(glsl_type_is_scalar(glsl_without_array(type
)));
627 if (glsl_type_is_array(type
)) {
628 const struct glsl_type
*atomic
=
629 repair_atomic_type(glsl_get_array_element(type
));
631 return glsl_array_type(atomic
, glsl_get_length(type
),
632 glsl_get_explicit_stride(type
));
634 return glsl_atomic_uint_type();
639 vtn_pointer_to_deref(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
641 if (b
->wa_glslang_179
) {
642 /* Do on-the-fly copy propagation for samplers. */
643 if (ptr
->var
&& ptr
->var
->copy_prop_sampler
)
644 return vtn_pointer_to_deref(b
, ptr
->var
->copy_prop_sampler
);
647 vtn_assert(!vtn_pointer_uses_ssa_offset(b
, ptr
));
649 struct vtn_access_chain chain
= {
652 ptr
= vtn_nir_deref_pointer_dereference(b
, ptr
, &chain
);
659 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_instr
*deref
,
660 struct vtn_ssa_value
*inout
,
661 enum gl_access_qualifier access
)
663 if (glsl_type_is_vector_or_scalar(deref
->type
)) {
665 inout
->def
= nir_load_deref_with_access(&b
->nb
, deref
, access
);
667 nir_store_deref_with_access(&b
->nb
, deref
, inout
->def
, ~0, access
);
669 } else if (glsl_type_is_array(deref
->type
) ||
670 glsl_type_is_matrix(deref
->type
)) {
671 unsigned elems
= glsl_get_length(deref
->type
);
672 for (unsigned i
= 0; i
< elems
; i
++) {
673 nir_deref_instr
*child
=
674 nir_build_deref_array_imm(&b
->nb
, deref
, i
);
675 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
], access
);
678 vtn_assert(glsl_type_is_struct_or_ifc(deref
->type
));
679 unsigned elems
= glsl_get_length(deref
->type
);
680 for (unsigned i
= 0; i
< elems
; i
++) {
681 nir_deref_instr
*child
= nir_build_deref_struct(&b
->nb
, deref
, i
);
682 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
], access
);
688 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
690 struct vtn_pointer
*ptr
= vtn_value(b
, id
, vtn_value_type_pointer
)->pointer
;
691 return vtn_pointer_to_deref(b
, ptr
);
695 * Gets the NIR-level deref tail, which may have as a child an array deref
696 * selecting which component due to OpAccessChain supporting per-component
697 * indexing in SPIR-V.
699 static nir_deref_instr
*
700 get_deref_tail(nir_deref_instr
*deref
)
702 if (deref
->deref_type
!= nir_deref_type_array
)
705 nir_deref_instr
*parent
=
706 nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
);
708 if (glsl_type_is_vector(parent
->type
))
714 struct vtn_ssa_value
*
715 vtn_local_load(struct vtn_builder
*b
, nir_deref_instr
*src
,
716 enum gl_access_qualifier access
)
718 nir_deref_instr
*src_tail
= get_deref_tail(src
);
719 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
720 _vtn_local_load_store(b
, true, src_tail
, val
, access
);
722 if (src_tail
!= src
) {
723 val
->type
= src
->type
;
724 val
->def
= nir_vector_extract(&b
->nb
, val
->def
, src
->arr
.index
.ssa
);
731 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
732 nir_deref_instr
*dest
, enum gl_access_qualifier access
)
734 nir_deref_instr
*dest_tail
= get_deref_tail(dest
);
736 if (dest_tail
!= dest
) {
737 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
738 _vtn_local_load_store(b
, true, dest_tail
, val
, access
);
740 val
->def
= nir_vector_insert(&b
->nb
, val
->def
, src
->def
,
741 dest
->arr
.index
.ssa
);
742 _vtn_local_load_store(b
, false, dest_tail
, val
, access
);
744 _vtn_local_load_store(b
, false, dest_tail
, src
, access
);
749 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
750 nir_ssa_def
**index_out
)
752 assert(vtn_pointer_uses_ssa_offset(b
, ptr
));
754 struct vtn_access_chain chain
= {
757 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
759 *index_out
= ptr
->block_index
;
763 /* Tries to compute the size of an interface block based on the strides and
764 * offsets that are provided to us in the SPIR-V source.
767 vtn_type_block_size(struct vtn_builder
*b
, struct vtn_type
*type
)
769 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
773 case GLSL_TYPE_UINT16
:
774 case GLSL_TYPE_INT16
:
775 case GLSL_TYPE_UINT8
:
777 case GLSL_TYPE_UINT64
:
778 case GLSL_TYPE_INT64
:
779 case GLSL_TYPE_FLOAT
:
780 case GLSL_TYPE_FLOAT16
:
782 case GLSL_TYPE_DOUBLE
: {
783 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
784 glsl_get_matrix_columns(type
->type
);
786 vtn_assert(type
->stride
> 0);
787 return type
->stride
* cols
;
789 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
790 return glsl_get_vector_elements(type
->type
) * type_size
;
794 case GLSL_TYPE_STRUCT
:
795 case GLSL_TYPE_INTERFACE
: {
797 unsigned num_fields
= glsl_get_length(type
->type
);
798 for (unsigned f
= 0; f
< num_fields
; f
++) {
799 unsigned field_end
= type
->offsets
[f
] +
800 vtn_type_block_size(b
, type
->members
[f
]);
801 size
= MAX2(size
, field_end
);
806 case GLSL_TYPE_ARRAY
:
807 vtn_assert(type
->stride
> 0);
808 vtn_assert(glsl_get_length(type
->type
) > 0);
809 return type
->stride
* glsl_get_length(type
->type
);
812 vtn_fail("Invalid block type");
818 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
819 nir_ssa_def
*index
, nir_ssa_def
*offset
,
820 unsigned access_offset
, unsigned access_size
,
821 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
,
822 enum gl_access_qualifier access
)
824 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
825 instr
->num_components
= glsl_get_vector_elements(type
);
827 /* Booleans usually shouldn't show up in external memory in SPIR-V.
828 * However, they do for certain older GLSLang versions and can for shared
829 * memory when we lower access chains internally.
831 const unsigned data_bit_size
= glsl_type_is_boolean(type
) ? 32 :
832 glsl_get_bit_size(type
);
836 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
837 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
840 if (op
== nir_intrinsic_load_push_constant
) {
841 nir_intrinsic_set_base(instr
, access_offset
);
842 nir_intrinsic_set_range(instr
, access_size
);
845 if (op
== nir_intrinsic_load_ubo
||
846 op
== nir_intrinsic_load_ssbo
||
847 op
== nir_intrinsic_store_ssbo
) {
848 nir_intrinsic_set_access(instr
, access
);
851 /* With extensions like relaxed_block_layout, we really can't guarantee
852 * much more than scalar alignment.
854 if (op
!= nir_intrinsic_load_push_constant
)
855 nir_intrinsic_set_align(instr
, data_bit_size
/ 8, 0);
858 instr
->src
[src
++] = nir_src_for_ssa(index
);
860 if (op
== nir_intrinsic_load_push_constant
) {
861 /* We need to subtract the offset from where the intrinsic will load the
864 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
865 nir_imm_int(&b
->nb
, access_offset
)));
867 instr
->src
[src
++] = nir_src_for_ssa(offset
);
871 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
872 instr
->num_components
, data_bit_size
, NULL
);
873 (*inout
)->def
= &instr
->dest
.ssa
;
876 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
878 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
879 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
883 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
884 nir_ssa_def
*index
, nir_ssa_def
*offset
,
885 unsigned access_offset
, unsigned access_size
,
886 struct vtn_type
*type
, enum gl_access_qualifier access
,
887 struct vtn_ssa_value
**inout
)
889 if (load
&& *inout
== NULL
)
890 *inout
= vtn_create_ssa_value(b
, type
->type
);
892 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
896 case GLSL_TYPE_UINT16
:
897 case GLSL_TYPE_INT16
:
898 case GLSL_TYPE_UINT8
:
900 case GLSL_TYPE_UINT64
:
901 case GLSL_TYPE_INT64
:
902 case GLSL_TYPE_FLOAT
:
903 case GLSL_TYPE_FLOAT16
:
904 case GLSL_TYPE_DOUBLE
:
906 /* This is where things get interesting. At this point, we've hit
907 * a vector, a scalar, or a matrix.
909 if (glsl_type_is_matrix(type
->type
)) {
910 /* Loading the whole matrix */
911 struct vtn_ssa_value
*transpose
;
912 unsigned num_ops
, vec_width
, col_stride
;
913 if (type
->row_major
) {
914 num_ops
= glsl_get_vector_elements(type
->type
);
915 vec_width
= glsl_get_matrix_columns(type
->type
);
916 col_stride
= type
->array_element
->stride
;
918 const struct glsl_type
*transpose_type
=
919 glsl_matrix_type(base_type
, vec_width
, num_ops
);
920 *inout
= vtn_create_ssa_value(b
, transpose_type
);
922 transpose
= vtn_ssa_transpose(b
, *inout
);
926 num_ops
= glsl_get_matrix_columns(type
->type
);
927 vec_width
= glsl_get_vector_elements(type
->type
);
928 col_stride
= type
->stride
;
931 for (unsigned i
= 0; i
< num_ops
; i
++) {
932 nir_ssa_def
*elem_offset
=
933 nir_iadd_imm(&b
->nb
, offset
, i
* col_stride
);
934 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
935 access_offset
, access_size
,
937 glsl_vector_type(base_type
, vec_width
),
938 type
->access
| access
);
941 if (load
&& type
->row_major
)
942 *inout
= vtn_ssa_transpose(b
, *inout
);
944 unsigned elems
= glsl_get_vector_elements(type
->type
);
945 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
946 if (elems
== 1 || type
->stride
== type_size
) {
947 /* This is a tightly-packed normal scalar or vector load */
948 vtn_assert(glsl_type_is_vector_or_scalar(type
->type
));
949 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
950 access_offset
, access_size
,
952 type
->access
| access
);
954 /* This is a strided load. We have to load N things separately.
955 * This is the single column of a row-major matrix case.
957 vtn_assert(type
->stride
> type_size
);
958 vtn_assert(type
->stride
% type_size
== 0);
960 nir_ssa_def
*per_comp
[4];
961 for (unsigned i
= 0; i
< elems
; i
++) {
962 nir_ssa_def
*elem_offset
=
963 nir_iadd_imm(&b
->nb
, offset
, i
* type
->stride
);
964 struct vtn_ssa_value
*comp
, temp_val
;
966 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
967 temp_val
.type
= glsl_scalar_type(base_type
);
970 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
971 access_offset
, access_size
,
972 &comp
, glsl_scalar_type(base_type
),
973 type
->access
| access
);
974 per_comp
[i
] = comp
->def
;
979 *inout
= vtn_create_ssa_value(b
, type
->type
);
980 (*inout
)->def
= nir_vec(&b
->nb
, per_comp
, elems
);
986 case GLSL_TYPE_ARRAY
: {
987 unsigned elems
= glsl_get_length(type
->type
);
988 for (unsigned i
= 0; i
< elems
; i
++) {
989 nir_ssa_def
*elem_off
=
990 nir_iadd_imm(&b
->nb
, offset
, i
* type
->stride
);
991 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
992 access_offset
, access_size
,
994 type
->array_element
->access
| access
,
995 &(*inout
)->elems
[i
]);
1000 case GLSL_TYPE_INTERFACE
:
1001 case GLSL_TYPE_STRUCT
: {
1002 unsigned elems
= glsl_get_length(type
->type
);
1003 for (unsigned i
= 0; i
< elems
; i
++) {
1004 nir_ssa_def
*elem_off
=
1005 nir_iadd_imm(&b
->nb
, offset
, type
->offsets
[i
]);
1006 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
1007 access_offset
, access_size
,
1009 type
->members
[i
]->access
| access
,
1010 &(*inout
)->elems
[i
]);
1016 vtn_fail("Invalid block member type");
1020 static struct vtn_ssa_value
*
1021 vtn_block_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
1023 nir_intrinsic_op op
;
1024 unsigned access_offset
= 0, access_size
= 0;
1025 switch (src
->mode
) {
1026 case vtn_variable_mode_ubo
:
1027 op
= nir_intrinsic_load_ubo
;
1029 case vtn_variable_mode_ssbo
:
1030 op
= nir_intrinsic_load_ssbo
;
1032 case vtn_variable_mode_push_constant
:
1033 op
= nir_intrinsic_load_push_constant
;
1034 access_size
= b
->shader
->num_uniforms
;
1036 case vtn_variable_mode_workgroup
:
1037 op
= nir_intrinsic_load_shared
;
1040 vtn_fail("Invalid block variable mode");
1043 nir_ssa_def
*offset
, *index
= NULL
;
1044 offset
= vtn_pointer_to_offset(b
, src
, &index
);
1046 struct vtn_ssa_value
*value
= NULL
;
1047 _vtn_block_load_store(b
, op
, true, index
, offset
,
1048 access_offset
, access_size
,
1049 src
->type
, src
->access
, &value
);
1054 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
1055 struct vtn_pointer
*dst
)
1057 nir_intrinsic_op op
;
1058 switch (dst
->mode
) {
1059 case vtn_variable_mode_ssbo
:
1060 op
= nir_intrinsic_store_ssbo
;
1062 case vtn_variable_mode_workgroup
:
1063 op
= nir_intrinsic_store_shared
;
1066 vtn_fail("Invalid block variable mode");
1069 nir_ssa_def
*offset
, *index
= NULL
;
1070 offset
= vtn_pointer_to_offset(b
, dst
, &index
);
1072 _vtn_block_load_store(b
, op
, false, index
, offset
,
1073 0, 0, dst
->type
, dst
->access
, &src
);
1077 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
1078 struct vtn_pointer
*ptr
,
1079 enum gl_access_qualifier access
,
1080 struct vtn_ssa_value
**inout
)
1082 enum glsl_base_type base_type
= glsl_get_base_type(ptr
->type
->type
);
1083 switch (base_type
) {
1084 case GLSL_TYPE_UINT
:
1086 case GLSL_TYPE_UINT16
:
1087 case GLSL_TYPE_INT16
:
1088 case GLSL_TYPE_UINT8
:
1089 case GLSL_TYPE_INT8
:
1090 case GLSL_TYPE_UINT64
:
1091 case GLSL_TYPE_INT64
:
1092 case GLSL_TYPE_FLOAT
:
1093 case GLSL_TYPE_FLOAT16
:
1094 case GLSL_TYPE_BOOL
:
1095 case GLSL_TYPE_DOUBLE
:
1096 if (glsl_type_is_vector_or_scalar(ptr
->type
->type
)) {
1097 /* We hit a vector or scalar; go ahead and emit the load[s] */
1098 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
1099 if (vtn_pointer_is_external_block(b
, ptr
)) {
1100 /* If it's external, we call nir_load/store_deref directly. The
1101 * vtn_local_load/store helpers are too clever and do magic to
1102 * avoid array derefs of vectors. That magic is both less
1103 * efficient than the direct load/store and, in the case of
1104 * stores, is broken because it creates a race condition if two
1105 * threads are writing to different components of the same vector
1106 * due to the load+insert+store it uses to emulate the array
1110 *inout
= vtn_create_ssa_value(b
, ptr
->type
->type
);
1111 (*inout
)->def
= nir_load_deref_with_access(&b
->nb
, deref
,
1112 ptr
->type
->access
| access
);
1114 nir_store_deref_with_access(&b
->nb
, deref
, (*inout
)->def
, ~0,
1115 ptr
->type
->access
| access
);
1119 *inout
= vtn_local_load(b
, deref
, ptr
->type
->access
| access
);
1121 vtn_local_store(b
, *inout
, deref
, ptr
->type
->access
| access
);
1128 case GLSL_TYPE_INTERFACE
:
1129 case GLSL_TYPE_ARRAY
:
1130 case GLSL_TYPE_STRUCT
: {
1131 unsigned elems
= glsl_get_length(ptr
->type
->type
);
1133 vtn_assert(*inout
== NULL
);
1134 *inout
= rzalloc(b
, struct vtn_ssa_value
);
1135 (*inout
)->type
= ptr
->type
->type
;
1136 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
1139 struct vtn_access_chain chain
= {
1142 { .mode
= vtn_access_mode_literal
, },
1145 for (unsigned i
= 0; i
< elems
; i
++) {
1146 chain
.link
[0].id
= i
;
1147 struct vtn_pointer
*elem
= vtn_pointer_dereference(b
, ptr
, &chain
);
1148 _vtn_variable_load_store(b
, load
, elem
, ptr
->type
->access
| access
,
1149 &(*inout
)->elems
[i
]);
1155 vtn_fail("Invalid access chain type");
1159 struct vtn_ssa_value
*
1160 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
1162 if (vtn_pointer_uses_ssa_offset(b
, src
)) {
1163 return vtn_block_load(b
, src
);
1165 struct vtn_ssa_value
*val
= NULL
;
1166 _vtn_variable_load_store(b
, true, src
, src
->access
, &val
);
1172 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
1173 struct vtn_pointer
*dest
)
1175 if (vtn_pointer_uses_ssa_offset(b
, dest
)) {
1176 vtn_assert(dest
->mode
== vtn_variable_mode_ssbo
||
1177 dest
->mode
== vtn_variable_mode_workgroup
);
1178 vtn_block_store(b
, src
, dest
);
1180 _vtn_variable_load_store(b
, false, dest
, dest
->access
, &src
);
1185 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
1186 struct vtn_pointer
*src
)
1188 vtn_assert(src
->type
->type
== dest
->type
->type
);
1189 enum glsl_base_type base_type
= glsl_get_base_type(src
->type
->type
);
1190 switch (base_type
) {
1191 case GLSL_TYPE_UINT
:
1193 case GLSL_TYPE_UINT16
:
1194 case GLSL_TYPE_INT16
:
1195 case GLSL_TYPE_UINT8
:
1196 case GLSL_TYPE_INT8
:
1197 case GLSL_TYPE_UINT64
:
1198 case GLSL_TYPE_INT64
:
1199 case GLSL_TYPE_FLOAT
:
1200 case GLSL_TYPE_FLOAT16
:
1201 case GLSL_TYPE_DOUBLE
:
1202 case GLSL_TYPE_BOOL
:
1203 /* At this point, we have a scalar, vector, or matrix so we know that
1204 * there cannot be any structure splitting still in the way. By
1205 * stopping at the matrix level rather than the vector level, we
1206 * ensure that matrices get loaded in the optimal way even if they
1207 * are storred row-major in a UBO.
1209 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
1212 case GLSL_TYPE_INTERFACE
:
1213 case GLSL_TYPE_ARRAY
:
1214 case GLSL_TYPE_STRUCT
: {
1215 struct vtn_access_chain chain
= {
1218 { .mode
= vtn_access_mode_literal
, },
1221 unsigned elems
= glsl_get_length(src
->type
->type
);
1222 for (unsigned i
= 0; i
< elems
; i
++) {
1223 chain
.link
[0].id
= i
;
1224 struct vtn_pointer
*src_elem
=
1225 vtn_pointer_dereference(b
, src
, &chain
);
1226 struct vtn_pointer
*dest_elem
=
1227 vtn_pointer_dereference(b
, dest
, &chain
);
1229 _vtn_variable_copy(b
, dest_elem
, src_elem
);
1235 vtn_fail("Invalid access chain type");
1240 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
1241 struct vtn_pointer
*src
)
1243 /* TODO: At some point, we should add a special-case for when we can
1244 * just emit a copy_var intrinsic.
1246 _vtn_variable_copy(b
, dest
, src
);
1250 set_mode_system_value(struct vtn_builder
*b
, nir_variable_mode
*mode
)
1252 vtn_assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
1253 *mode
= nir_var_system_value
;
1257 vtn_get_builtin_location(struct vtn_builder
*b
,
1258 SpvBuiltIn builtin
, int *location
,
1259 nir_variable_mode
*mode
)
1262 case SpvBuiltInPosition
:
1263 *location
= VARYING_SLOT_POS
;
1265 case SpvBuiltInPointSize
:
1266 *location
= VARYING_SLOT_PSIZ
;
1268 case SpvBuiltInClipDistance
:
1269 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
1271 case SpvBuiltInCullDistance
:
1272 *location
= VARYING_SLOT_CULL_DIST0
;
1274 case SpvBuiltInVertexId
:
1275 case SpvBuiltInVertexIndex
:
1276 /* The Vulkan spec defines VertexIndex to be non-zero-based and doesn't
1277 * allow VertexId. The ARB_gl_spirv spec defines VertexId to be the
1278 * same as gl_VertexID, which is non-zero-based, and removes
1279 * VertexIndex. Since they're both defined to be non-zero-based, we use
1280 * SYSTEM_VALUE_VERTEX_ID for both.
1282 *location
= SYSTEM_VALUE_VERTEX_ID
;
1283 set_mode_system_value(b
, mode
);
1285 case SpvBuiltInInstanceIndex
:
1286 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
1287 set_mode_system_value(b
, mode
);
1289 case SpvBuiltInInstanceId
:
1290 *location
= SYSTEM_VALUE_INSTANCE_ID
;
1291 set_mode_system_value(b
, mode
);
1293 case SpvBuiltInPrimitiveId
:
1294 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1295 vtn_assert(*mode
== nir_var_shader_in
);
1296 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1297 } else if (*mode
== nir_var_shader_out
) {
1298 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1300 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
1301 set_mode_system_value(b
, mode
);
1304 case SpvBuiltInInvocationId
:
1305 *location
= SYSTEM_VALUE_INVOCATION_ID
;
1306 set_mode_system_value(b
, mode
);
1308 case SpvBuiltInLayer
:
1309 *location
= VARYING_SLOT_LAYER
;
1310 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1311 *mode
= nir_var_shader_in
;
1312 else if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1313 *mode
= nir_var_shader_out
;
1314 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1315 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1316 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1317 *mode
= nir_var_shader_out
;
1319 vtn_fail("invalid stage for SpvBuiltInLayer");
1321 case SpvBuiltInViewportIndex
:
1322 *location
= VARYING_SLOT_VIEWPORT
;
1323 if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1324 *mode
= nir_var_shader_out
;
1325 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1326 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1327 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1328 *mode
= nir_var_shader_out
;
1329 else if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1330 *mode
= nir_var_shader_in
;
1332 vtn_fail("invalid stage for SpvBuiltInViewportIndex");
1334 case SpvBuiltInTessLevelOuter
:
1335 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
1337 case SpvBuiltInTessLevelInner
:
1338 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
1340 case SpvBuiltInTessCoord
:
1341 *location
= SYSTEM_VALUE_TESS_COORD
;
1342 set_mode_system_value(b
, mode
);
1344 case SpvBuiltInPatchVertices
:
1345 *location
= SYSTEM_VALUE_VERTICES_IN
;
1346 set_mode_system_value(b
, mode
);
1348 case SpvBuiltInFragCoord
:
1349 vtn_assert(*mode
== nir_var_shader_in
);
1350 if (b
->options
&& b
->options
->frag_coord_is_sysval
) {
1351 *mode
= nir_var_system_value
;
1352 *location
= SYSTEM_VALUE_FRAG_COORD
;
1354 *location
= VARYING_SLOT_POS
;
1357 case SpvBuiltInPointCoord
:
1358 *location
= VARYING_SLOT_PNTC
;
1359 vtn_assert(*mode
== nir_var_shader_in
);
1361 case SpvBuiltInFrontFacing
:
1362 *location
= SYSTEM_VALUE_FRONT_FACE
;
1363 set_mode_system_value(b
, mode
);
1365 case SpvBuiltInSampleId
:
1366 *location
= SYSTEM_VALUE_SAMPLE_ID
;
1367 set_mode_system_value(b
, mode
);
1369 case SpvBuiltInSamplePosition
:
1370 *location
= SYSTEM_VALUE_SAMPLE_POS
;
1371 set_mode_system_value(b
, mode
);
1373 case SpvBuiltInSampleMask
:
1374 if (*mode
== nir_var_shader_out
) {
1375 *location
= FRAG_RESULT_SAMPLE_MASK
;
1377 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1378 set_mode_system_value(b
, mode
);
1381 case SpvBuiltInFragDepth
:
1382 *location
= FRAG_RESULT_DEPTH
;
1383 vtn_assert(*mode
== nir_var_shader_out
);
1385 case SpvBuiltInHelperInvocation
:
1386 *location
= SYSTEM_VALUE_HELPER_INVOCATION
;
1387 set_mode_system_value(b
, mode
);
1389 case SpvBuiltInNumWorkgroups
:
1390 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1391 set_mode_system_value(b
, mode
);
1393 case SpvBuiltInWorkgroupSize
:
1394 *location
= SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
1395 set_mode_system_value(b
, mode
);
1397 case SpvBuiltInWorkgroupId
:
1398 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1399 set_mode_system_value(b
, mode
);
1401 case SpvBuiltInLocalInvocationId
:
1402 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1403 set_mode_system_value(b
, mode
);
1405 case SpvBuiltInLocalInvocationIndex
:
1406 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1407 set_mode_system_value(b
, mode
);
1409 case SpvBuiltInGlobalInvocationId
:
1410 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1411 set_mode_system_value(b
, mode
);
1413 case SpvBuiltInGlobalLinearId
:
1414 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX
;
1415 set_mode_system_value(b
, mode
);
1417 case SpvBuiltInBaseVertex
:
1418 /* OpenGL gl_BaseVertex (SYSTEM_VALUE_BASE_VERTEX) is not the same
1419 * semantic as Vulkan BaseVertex (SYSTEM_VALUE_FIRST_VERTEX).
1421 if (b
->options
->environment
== NIR_SPIRV_OPENGL
)
1422 *location
= SYSTEM_VALUE_BASE_VERTEX
;
1424 *location
= SYSTEM_VALUE_FIRST_VERTEX
;
1425 set_mode_system_value(b
, mode
);
1427 case SpvBuiltInBaseInstance
:
1428 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1429 set_mode_system_value(b
, mode
);
1431 case SpvBuiltInDrawIndex
:
1432 *location
= SYSTEM_VALUE_DRAW_ID
;
1433 set_mode_system_value(b
, mode
);
1435 case SpvBuiltInSubgroupSize
:
1436 *location
= SYSTEM_VALUE_SUBGROUP_SIZE
;
1437 set_mode_system_value(b
, mode
);
1439 case SpvBuiltInSubgroupId
:
1440 *location
= SYSTEM_VALUE_SUBGROUP_ID
;
1441 set_mode_system_value(b
, mode
);
1443 case SpvBuiltInSubgroupLocalInvocationId
:
1444 *location
= SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1445 set_mode_system_value(b
, mode
);
1447 case SpvBuiltInNumSubgroups
:
1448 *location
= SYSTEM_VALUE_NUM_SUBGROUPS
;
1449 set_mode_system_value(b
, mode
);
1451 case SpvBuiltInDeviceIndex
:
1452 *location
= SYSTEM_VALUE_DEVICE_INDEX
;
1453 set_mode_system_value(b
, mode
);
1455 case SpvBuiltInViewIndex
:
1456 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1457 set_mode_system_value(b
, mode
);
1459 case SpvBuiltInSubgroupEqMask
:
1460 *location
= SYSTEM_VALUE_SUBGROUP_EQ_MASK
,
1461 set_mode_system_value(b
, mode
);
1463 case SpvBuiltInSubgroupGeMask
:
1464 *location
= SYSTEM_VALUE_SUBGROUP_GE_MASK
,
1465 set_mode_system_value(b
, mode
);
1467 case SpvBuiltInSubgroupGtMask
:
1468 *location
= SYSTEM_VALUE_SUBGROUP_GT_MASK
,
1469 set_mode_system_value(b
, mode
);
1471 case SpvBuiltInSubgroupLeMask
:
1472 *location
= SYSTEM_VALUE_SUBGROUP_LE_MASK
,
1473 set_mode_system_value(b
, mode
);
1475 case SpvBuiltInSubgroupLtMask
:
1476 *location
= SYSTEM_VALUE_SUBGROUP_LT_MASK
,
1477 set_mode_system_value(b
, mode
);
1479 case SpvBuiltInFragStencilRefEXT
:
1480 *location
= FRAG_RESULT_STENCIL
;
1481 vtn_assert(*mode
== nir_var_shader_out
);
1483 case SpvBuiltInWorkDim
:
1484 *location
= SYSTEM_VALUE_WORK_DIM
;
1485 set_mode_system_value(b
, mode
);
1487 case SpvBuiltInGlobalSize
:
1488 *location
= SYSTEM_VALUE_GLOBAL_GROUP_SIZE
;
1489 set_mode_system_value(b
, mode
);
1491 case SpvBuiltInBaryCoordNoPerspAMD
:
1492 *location
= SYSTEM_VALUE_BARYCENTRIC_LINEAR_PIXEL
;
1493 set_mode_system_value(b
, mode
);
1495 case SpvBuiltInBaryCoordNoPerspCentroidAMD
:
1496 *location
= SYSTEM_VALUE_BARYCENTRIC_LINEAR_CENTROID
;
1497 set_mode_system_value(b
, mode
);
1499 case SpvBuiltInBaryCoordNoPerspSampleAMD
:
1500 *location
= SYSTEM_VALUE_BARYCENTRIC_LINEAR_SAMPLE
;
1501 set_mode_system_value(b
, mode
);
1503 case SpvBuiltInBaryCoordSmoothAMD
:
1504 *location
= SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL
;
1505 set_mode_system_value(b
, mode
);
1507 case SpvBuiltInBaryCoordSmoothCentroidAMD
:
1508 *location
= SYSTEM_VALUE_BARYCENTRIC_PERSP_CENTROID
;
1509 set_mode_system_value(b
, mode
);
1511 case SpvBuiltInBaryCoordSmoothSampleAMD
:
1512 *location
= SYSTEM_VALUE_BARYCENTRIC_PERSP_SAMPLE
;
1513 set_mode_system_value(b
, mode
);
1515 case SpvBuiltInBaryCoordPullModelAMD
:
1516 *location
= SYSTEM_VALUE_BARYCENTRIC_PULL_MODEL
;
1517 set_mode_system_value(b
, mode
);
1520 vtn_fail("Unsupported builtin: %s (%u)",
1521 spirv_builtin_to_string(builtin
), builtin
);
1526 apply_var_decoration(struct vtn_builder
*b
,
1527 struct nir_variable_data
*var_data
,
1528 const struct vtn_decoration
*dec
)
1530 switch (dec
->decoration
) {
1531 case SpvDecorationRelaxedPrecision
:
1532 break; /* FIXME: Do nothing with this for now. */
1533 case SpvDecorationNoPerspective
:
1534 var_data
->interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1536 case SpvDecorationFlat
:
1537 var_data
->interpolation
= INTERP_MODE_FLAT
;
1539 case SpvDecorationExplicitInterpAMD
:
1540 var_data
->interpolation
= INTERP_MODE_EXPLICIT
;
1542 case SpvDecorationCentroid
:
1543 var_data
->centroid
= true;
1545 case SpvDecorationSample
:
1546 var_data
->sample
= true;
1548 case SpvDecorationInvariant
:
1549 var_data
->invariant
= true;
1551 case SpvDecorationConstant
:
1552 var_data
->read_only
= true;
1554 case SpvDecorationNonReadable
:
1555 var_data
->access
|= ACCESS_NON_READABLE
;
1557 case SpvDecorationNonWritable
:
1558 var_data
->read_only
= true;
1559 var_data
->access
|= ACCESS_NON_WRITEABLE
;
1561 case SpvDecorationRestrict
:
1562 var_data
->access
|= ACCESS_RESTRICT
;
1564 case SpvDecorationAliased
:
1565 var_data
->access
&= ~ACCESS_RESTRICT
;
1567 case SpvDecorationVolatile
:
1568 var_data
->access
|= ACCESS_VOLATILE
;
1570 case SpvDecorationCoherent
:
1571 var_data
->access
|= ACCESS_COHERENT
;
1573 case SpvDecorationComponent
:
1574 var_data
->location_frac
= dec
->operands
[0];
1576 case SpvDecorationIndex
:
1577 var_data
->index
= dec
->operands
[0];
1579 case SpvDecorationBuiltIn
: {
1580 SpvBuiltIn builtin
= dec
->operands
[0];
1582 nir_variable_mode mode
= var_data
->mode
;
1583 vtn_get_builtin_location(b
, builtin
, &var_data
->location
, &mode
);
1584 var_data
->mode
= mode
;
1587 case SpvBuiltInTessLevelOuter
:
1588 case SpvBuiltInTessLevelInner
:
1589 case SpvBuiltInClipDistance
:
1590 case SpvBuiltInCullDistance
:
1591 var_data
->compact
= true;
1598 case SpvDecorationSpecId
:
1599 case SpvDecorationRowMajor
:
1600 case SpvDecorationColMajor
:
1601 case SpvDecorationMatrixStride
:
1602 case SpvDecorationUniform
:
1603 case SpvDecorationUniformId
:
1604 case SpvDecorationLinkageAttributes
:
1605 break; /* Do nothing with these here */
1607 case SpvDecorationPatch
:
1608 var_data
->patch
= true;
1611 case SpvDecorationLocation
:
1612 vtn_fail("Handled above");
1614 case SpvDecorationBlock
:
1615 case SpvDecorationBufferBlock
:
1616 case SpvDecorationArrayStride
:
1617 case SpvDecorationGLSLShared
:
1618 case SpvDecorationGLSLPacked
:
1619 break; /* These can apply to a type but we don't care about them */
1621 case SpvDecorationBinding
:
1622 case SpvDecorationDescriptorSet
:
1623 case SpvDecorationNoContraction
:
1624 case SpvDecorationInputAttachmentIndex
:
1625 vtn_warn("Decoration not allowed for variable or structure member: %s",
1626 spirv_decoration_to_string(dec
->decoration
));
1629 case SpvDecorationXfbBuffer
:
1630 var_data
->explicit_xfb_buffer
= true;
1631 var_data
->xfb
.buffer
= dec
->operands
[0];
1632 var_data
->always_active_io
= true;
1634 case SpvDecorationXfbStride
:
1635 var_data
->explicit_xfb_stride
= true;
1636 var_data
->xfb
.stride
= dec
->operands
[0];
1638 case SpvDecorationOffset
:
1639 var_data
->explicit_offset
= true;
1640 var_data
->offset
= dec
->operands
[0];
1643 case SpvDecorationStream
:
1644 var_data
->stream
= dec
->operands
[0];
1647 case SpvDecorationCPacked
:
1648 case SpvDecorationSaturatedConversion
:
1649 case SpvDecorationFuncParamAttr
:
1650 case SpvDecorationFPRoundingMode
:
1651 case SpvDecorationFPFastMathMode
:
1652 case SpvDecorationAlignment
:
1653 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
1654 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1655 spirv_decoration_to_string(dec
->decoration
));
1659 case SpvDecorationUserSemantic
:
1660 case SpvDecorationUserTypeGOOGLE
:
1661 /* User semantic decorations can safely be ignored by the driver. */
1664 case SpvDecorationRestrictPointerEXT
:
1665 case SpvDecorationAliasedPointerEXT
:
1666 /* TODO: We should actually plumb alias information through NIR. */
1670 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1675 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1676 const struct vtn_decoration
*dec
, void *out_is_patch
)
1678 if (dec
->decoration
== SpvDecorationPatch
) {
1679 *((bool *) out_is_patch
) = true;
1684 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1685 const struct vtn_decoration
*dec
, void *void_var
)
1687 struct vtn_variable
*vtn_var
= void_var
;
1689 /* Handle decorations that apply to a vtn_variable as a whole */
1690 switch (dec
->decoration
) {
1691 case SpvDecorationBinding
:
1692 vtn_var
->binding
= dec
->operands
[0];
1693 vtn_var
->explicit_binding
= true;
1695 case SpvDecorationDescriptorSet
:
1696 vtn_var
->descriptor_set
= dec
->operands
[0];
1698 case SpvDecorationInputAttachmentIndex
:
1699 vtn_var
->input_attachment_index
= dec
->operands
[0];
1701 case SpvDecorationPatch
:
1702 vtn_var
->patch
= true;
1704 case SpvDecorationOffset
:
1705 vtn_var
->offset
= dec
->operands
[0];
1707 case SpvDecorationNonWritable
:
1708 vtn_var
->access
|= ACCESS_NON_WRITEABLE
;
1710 case SpvDecorationNonReadable
:
1711 vtn_var
->access
|= ACCESS_NON_READABLE
;
1713 case SpvDecorationVolatile
:
1714 vtn_var
->access
|= ACCESS_VOLATILE
;
1716 case SpvDecorationCoherent
:
1717 vtn_var
->access
|= ACCESS_COHERENT
;
1719 case SpvDecorationCounterBuffer
:
1720 /* Counter buffer decorations can safely be ignored by the driver. */
1726 if (val
->value_type
== vtn_value_type_pointer
) {
1727 assert(val
->pointer
->var
== void_var
);
1728 assert(member
== -1);
1730 assert(val
->value_type
== vtn_value_type_type
);
1733 /* Location is odd. If applied to a split structure, we have to walk the
1734 * whole thing and accumulate the location. It's easier to handle as a
1737 if (dec
->decoration
== SpvDecorationLocation
) {
1738 unsigned location
= dec
->operands
[0];
1739 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
&&
1740 vtn_var
->mode
== vtn_variable_mode_output
) {
1741 location
+= FRAG_RESULT_DATA0
;
1742 } else if (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
&&
1743 vtn_var
->mode
== vtn_variable_mode_input
) {
1744 location
+= VERT_ATTRIB_GENERIC0
;
1745 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1746 vtn_var
->mode
== vtn_variable_mode_output
) {
1747 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1748 } else if (vtn_var
->mode
!= vtn_variable_mode_uniform
) {
1749 vtn_warn("Location must be on input, output, uniform, sampler or "
1754 if (vtn_var
->var
->num_members
== 0) {
1755 /* This handles the member and lone variable cases */
1756 vtn_var
->var
->data
.location
= location
;
1758 /* This handles the structure member case */
1759 assert(vtn_var
->var
->members
);
1762 vtn_var
->base_location
= location
;
1764 vtn_var
->var
->members
[member
].location
= location
;
1770 if (vtn_var
->var
->num_members
== 0) {
1771 /* We call this function on types as well as variables and not all
1772 * struct types get split so we can end up having stray member
1773 * decorations; just ignore them.
1776 apply_var_decoration(b
, &vtn_var
->var
->data
, dec
);
1777 } else if (member
>= 0) {
1778 /* Member decorations must come from a type */
1779 assert(val
->value_type
== vtn_value_type_type
);
1780 apply_var_decoration(b
, &vtn_var
->var
->members
[member
], dec
);
1783 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1784 for (unsigned i
= 0; i
< length
; i
++)
1785 apply_var_decoration(b
, &vtn_var
->var
->members
[i
], dec
);
1788 /* A few variables, those with external storage, have no actual
1789 * nir_variables associated with them. Fortunately, all decorations
1790 * we care about for those variables are on the type only.
1792 vtn_assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1793 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1794 vtn_var
->mode
== vtn_variable_mode_push_constant
);
1799 enum vtn_variable_mode
1800 vtn_storage_class_to_mode(struct vtn_builder
*b
,
1801 SpvStorageClass
class,
1802 struct vtn_type
*interface_type
,
1803 nir_variable_mode
*nir_mode_out
)
1805 enum vtn_variable_mode mode
;
1806 nir_variable_mode nir_mode
;
1808 case SpvStorageClassUniform
:
1809 /* Assume it's an UBO if we lack the interface_type. */
1810 if (!interface_type
|| interface_type
->block
) {
1811 mode
= vtn_variable_mode_ubo
;
1812 nir_mode
= nir_var_mem_ubo
;
1813 } else if (interface_type
->buffer_block
) {
1814 mode
= vtn_variable_mode_ssbo
;
1815 nir_mode
= nir_var_mem_ssbo
;
1817 /* Default-block uniforms, coming from gl_spirv */
1818 mode
= vtn_variable_mode_uniform
;
1819 nir_mode
= nir_var_uniform
;
1822 case SpvStorageClassStorageBuffer
:
1823 mode
= vtn_variable_mode_ssbo
;
1824 nir_mode
= nir_var_mem_ssbo
;
1826 case SpvStorageClassPhysicalStorageBuffer
:
1827 mode
= vtn_variable_mode_phys_ssbo
;
1828 nir_mode
= nir_var_mem_global
;
1830 case SpvStorageClassUniformConstant
:
1831 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1832 if (b
->options
->constant_as_global
) {
1833 mode
= vtn_variable_mode_cross_workgroup
;
1834 nir_mode
= nir_var_mem_global
;
1836 mode
= vtn_variable_mode_ubo
;
1837 nir_mode
= nir_var_mem_ubo
;
1840 mode
= vtn_variable_mode_uniform
;
1841 nir_mode
= nir_var_uniform
;
1844 case SpvStorageClassPushConstant
:
1845 mode
= vtn_variable_mode_push_constant
;
1846 nir_mode
= nir_var_uniform
;
1848 case SpvStorageClassInput
:
1849 mode
= vtn_variable_mode_input
;
1850 nir_mode
= nir_var_shader_in
;
1852 case SpvStorageClassOutput
:
1853 mode
= vtn_variable_mode_output
;
1854 nir_mode
= nir_var_shader_out
;
1856 case SpvStorageClassPrivate
:
1857 mode
= vtn_variable_mode_private
;
1858 nir_mode
= nir_var_shader_temp
;
1860 case SpvStorageClassFunction
:
1861 mode
= vtn_variable_mode_function
;
1862 nir_mode
= nir_var_function_temp
;
1864 case SpvStorageClassWorkgroup
:
1865 mode
= vtn_variable_mode_workgroup
;
1866 nir_mode
= nir_var_mem_shared
;
1868 case SpvStorageClassAtomicCounter
:
1869 mode
= vtn_variable_mode_uniform
;
1870 nir_mode
= nir_var_uniform
;
1872 case SpvStorageClassCrossWorkgroup
:
1873 mode
= vtn_variable_mode_cross_workgroup
;
1874 nir_mode
= nir_var_mem_global
;
1876 case SpvStorageClassImage
:
1877 mode
= vtn_variable_mode_image
;
1878 nir_mode
= nir_var_mem_ubo
;
1880 case SpvStorageClassGeneric
:
1882 vtn_fail("Unhandled variable storage class: %s (%u)",
1883 spirv_storageclass_to_string(class), class);
1887 *nir_mode_out
= nir_mode
;
1893 vtn_mode_to_address_format(struct vtn_builder
*b
, enum vtn_variable_mode mode
)
1896 case vtn_variable_mode_ubo
:
1897 return b
->options
->ubo_addr_format
;
1899 case vtn_variable_mode_ssbo
:
1900 return b
->options
->ssbo_addr_format
;
1902 case vtn_variable_mode_phys_ssbo
:
1903 return b
->options
->phys_ssbo_addr_format
;
1905 case vtn_variable_mode_push_constant
:
1906 return b
->options
->push_const_addr_format
;
1908 case vtn_variable_mode_workgroup
:
1909 return b
->options
->shared_addr_format
;
1911 case vtn_variable_mode_cross_workgroup
:
1912 return b
->options
->global_addr_format
;
1914 case vtn_variable_mode_function
:
1915 if (b
->physical_ptrs
)
1916 return b
->options
->temp_addr_format
;
1919 case vtn_variable_mode_private
:
1920 case vtn_variable_mode_uniform
:
1921 case vtn_variable_mode_input
:
1922 case vtn_variable_mode_output
:
1923 case vtn_variable_mode_image
:
1924 return nir_address_format_logical
;
1927 unreachable("Invalid variable mode");
1931 vtn_pointer_to_ssa(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
1933 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
1934 /* This pointer needs to have a pointer type with actual storage */
1935 vtn_assert(ptr
->ptr_type
);
1936 vtn_assert(ptr
->ptr_type
->type
);
1939 /* If we don't have an offset then we must be a pointer to the variable
1942 vtn_assert(!ptr
->offset
&& !ptr
->block_index
);
1944 struct vtn_access_chain chain
= {
1947 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
1950 vtn_assert(ptr
->offset
);
1951 if (ptr
->block_index
) {
1952 vtn_assert(ptr
->mode
== vtn_variable_mode_ubo
||
1953 ptr
->mode
== vtn_variable_mode_ssbo
);
1954 return nir_vec2(&b
->nb
, ptr
->block_index
, ptr
->offset
);
1956 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
);
1960 if (vtn_pointer_is_external_block(b
, ptr
) &&
1961 vtn_type_contains_block(b
, ptr
->type
) &&
1962 ptr
->mode
!= vtn_variable_mode_phys_ssbo
) {
1963 /* In this case, we're looking for a block index and not an actual
1966 * For PhysicalStorageBuffer pointers, we don't have a block index
1967 * at all because we get the pointer directly from the client. This
1968 * assumes that there will never be a SSBO binding variable using the
1969 * PhysicalStorageBuffer storage class. This assumption appears
1970 * to be correct according to the Vulkan spec because the table,
1971 * "Shader Resource and Storage Class Correspondence," the only the
1972 * Uniform storage class with BufferBlock or the StorageBuffer
1973 * storage class with Block can be used.
1975 if (!ptr
->block_index
) {
1976 /* If we don't have a block_index then we must be a pointer to the
1979 vtn_assert(!ptr
->deref
);
1981 struct vtn_access_chain chain
= {
1984 ptr
= vtn_nir_deref_pointer_dereference(b
, ptr
, &chain
);
1987 return ptr
->block_index
;
1989 return &vtn_pointer_to_deref(b
, ptr
)->dest
.ssa
;
1994 struct vtn_pointer
*
1995 vtn_pointer_from_ssa(struct vtn_builder
*b
, nir_ssa_def
*ssa
,
1996 struct vtn_type
*ptr_type
)
1998 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
2000 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
2001 struct vtn_type
*without_array
=
2002 vtn_type_without_array(ptr_type
->deref
);
2004 nir_variable_mode nir_mode
;
2005 ptr
->mode
= vtn_storage_class_to_mode(b
, ptr_type
->storage_class
,
2006 without_array
, &nir_mode
);
2007 ptr
->type
= ptr_type
->deref
;
2008 ptr
->ptr_type
= ptr_type
;
2010 if (b
->wa_glslang_179
) {
2011 /* To work around https://github.com/KhronosGroup/glslang/issues/179 we
2012 * need to whack the mode because it creates a function parameter with
2013 * the Function storage class even though it's a pointer to a sampler.
2014 * If we don't do this, then NIR won't get rid of the deref_cast for us.
2016 if (ptr
->mode
== vtn_variable_mode_function
&&
2017 (ptr
->type
->base_type
== vtn_base_type_sampler
||
2018 ptr
->type
->base_type
== vtn_base_type_sampled_image
)) {
2019 ptr
->mode
= vtn_variable_mode_uniform
;
2020 nir_mode
= nir_var_uniform
;
2024 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
2025 /* This pointer type needs to have actual storage */
2026 vtn_assert(ptr_type
->type
);
2027 if (ptr
->mode
== vtn_variable_mode_ubo
||
2028 ptr
->mode
== vtn_variable_mode_ssbo
) {
2029 vtn_assert(ssa
->num_components
== 2);
2030 ptr
->block_index
= nir_channel(&b
->nb
, ssa
, 0);
2031 ptr
->offset
= nir_channel(&b
->nb
, ssa
, 1);
2033 vtn_assert(ssa
->num_components
== 1);
2034 ptr
->block_index
= NULL
;
2038 const struct glsl_type
*deref_type
= ptr_type
->deref
->type
;
2039 if (!vtn_pointer_is_external_block(b
, ptr
)) {
2040 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
2041 deref_type
, ptr_type
->stride
);
2042 } else if (vtn_type_contains_block(b
, ptr
->type
) &&
2043 ptr
->mode
!= vtn_variable_mode_phys_ssbo
) {
2044 /* This is a pointer to somewhere in an array of blocks, not a
2045 * pointer to somewhere inside the block. Set the block index
2046 * instead of making a cast.
2048 ptr
->block_index
= ssa
;
2050 /* This is a pointer to something internal or a pointer inside a
2051 * block. It's just a regular cast.
2053 * For PhysicalStorageBuffer pointers, we don't have a block index
2054 * at all because we get the pointer directly from the client. This
2055 * assumes that there will never be a SSBO binding variable using the
2056 * PhysicalStorageBuffer storage class. This assumption appears
2057 * to be correct according to the Vulkan spec because the table,
2058 * "Shader Resource and Storage Class Correspondence," the only the
2059 * Uniform storage class with BufferBlock or the StorageBuffer
2060 * storage class with Block can be used.
2062 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
2063 ptr_type
->deref
->type
,
2065 ptr
->deref
->dest
.ssa
.num_components
=
2066 glsl_get_vector_elements(ptr_type
->type
);
2067 ptr
->deref
->dest
.ssa
.bit_size
= glsl_get_bit_size(ptr_type
->type
);
2075 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
2077 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
2080 if (var
->mode
== vtn_variable_mode_input
) {
2081 return stage
== MESA_SHADER_TESS_CTRL
||
2082 stage
== MESA_SHADER_TESS_EVAL
||
2083 stage
== MESA_SHADER_GEOMETRY
;
2086 if (var
->mode
== vtn_variable_mode_output
)
2087 return stage
== MESA_SHADER_TESS_CTRL
;
2093 assign_missing_member_locations(struct vtn_variable
*var
)
2096 glsl_get_length(glsl_without_array(var
->type
->type
));
2097 int location
= var
->base_location
;
2099 for (unsigned i
= 0; i
< length
; i
++) {
2100 /* From the Vulkan spec:
2102 * “If the structure type is a Block but without a Location, then each
2103 * of its members must have a Location decoration.”
2106 if (var
->type
->block
) {
2107 assert(var
->base_location
!= -1 ||
2108 var
->var
->members
[i
].location
!= -1);
2111 /* From the Vulkan spec:
2113 * “Any member with its own Location decoration is assigned that
2114 * location. Each remaining member is assigned the location after the
2115 * immediately preceding member in declaration order.”
2117 if (var
->var
->members
[i
].location
!= -1)
2118 location
= var
->var
->members
[i
].location
;
2120 var
->var
->members
[i
].location
= location
;
2122 /* Below we use type instead of interface_type, because interface_type
2123 * is only available when it is a Block. This code also supports
2124 * input/outputs that are just structs
2126 const struct glsl_type
*member_type
=
2127 glsl_get_struct_field(glsl_without_array(var
->type
->type
), i
);
2130 glsl_count_attribute_slots(member_type
,
2131 false /* is_gl_vertex_input */);
2137 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
2138 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
2139 nir_constant
*const_initializer
, nir_variable
*var_initializer
)
2141 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
2142 struct vtn_type
*type
= ptr_type
->deref
;
2144 struct vtn_type
*without_array
= vtn_type_without_array(ptr_type
->deref
);
2146 enum vtn_variable_mode mode
;
2147 nir_variable_mode nir_mode
;
2148 mode
= vtn_storage_class_to_mode(b
, storage_class
, without_array
, &nir_mode
);
2151 case vtn_variable_mode_ubo
:
2152 /* There's no other way to get vtn_variable_mode_ubo */
2153 vtn_assert(without_array
->block
);
2154 b
->shader
->info
.num_ubos
++;
2156 case vtn_variable_mode_ssbo
:
2157 if (storage_class
== SpvStorageClassStorageBuffer
&&
2158 !without_array
->block
) {
2159 if (b
->variable_pointers
) {
2160 vtn_fail("Variables in the StorageBuffer storage class must "
2161 "have a struct type with the Block decoration");
2163 /* If variable pointers are not present, it's still malformed
2164 * SPIR-V but we can parse it and do the right thing anyway.
2165 * Since some of the 8-bit storage tests have bugs in this are,
2166 * just make it a warning for now.
2168 vtn_warn("Variables in the StorageBuffer storage class must "
2169 "have a struct type with the Block decoration");
2172 b
->shader
->info
.num_ssbos
++;
2174 case vtn_variable_mode_uniform
:
2175 if (glsl_type_is_image(without_array
->type
))
2176 b
->shader
->info
.num_images
++;
2177 else if (glsl_type_is_sampler(without_array
->type
))
2178 b
->shader
->info
.num_textures
++;
2180 case vtn_variable_mode_push_constant
:
2181 b
->shader
->num_uniforms
= vtn_type_block_size(b
, type
);
2184 case vtn_variable_mode_image
:
2185 vtn_fail("Cannot create a variable with the Image storage class");
2188 case vtn_variable_mode_phys_ssbo
:
2189 vtn_fail("Cannot create a variable with the "
2190 "PhysicalStorageBuffer storage class");
2194 /* No tallying is needed */
2198 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
2201 var
->base_location
= -1;
2203 val
->pointer
= rzalloc(b
, struct vtn_pointer
);
2204 val
->pointer
->mode
= var
->mode
;
2205 val
->pointer
->type
= var
->type
;
2206 val
->pointer
->ptr_type
= ptr_type
;
2207 val
->pointer
->var
= var
;
2208 val
->pointer
->access
= var
->type
->access
;
2210 switch (var
->mode
) {
2211 case vtn_variable_mode_function
:
2212 case vtn_variable_mode_private
:
2213 case vtn_variable_mode_uniform
:
2214 /* For these, we create the variable normally */
2215 var
->var
= rzalloc(b
->shader
, nir_variable
);
2216 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2218 if (storage_class
== SpvStorageClassAtomicCounter
) {
2219 /* Need to tweak the nir type here as at vtn_handle_type we don't
2220 * have the access to storage_class, that is the one that points us
2221 * that is an atomic uint.
2223 var
->var
->type
= repair_atomic_type(var
->type
->type
);
2225 /* Private variables don't have any explicit layout but some layouts
2226 * may have leaked through due to type deduplication in the SPIR-V.
2228 var
->var
->type
= var
->type
->type
;
2230 var
->var
->data
.mode
= nir_mode
;
2231 var
->var
->data
.location
= -1;
2232 var
->var
->interface_type
= NULL
;
2235 case vtn_variable_mode_ubo
:
2236 case vtn_variable_mode_ssbo
:
2237 var
->var
= rzalloc(b
->shader
, nir_variable
);
2238 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2240 var
->var
->type
= var
->type
->type
;
2241 var
->var
->interface_type
= var
->type
->type
;
2243 var
->var
->data
.mode
= nir_mode
;
2244 var
->var
->data
.location
= -1;
2248 case vtn_variable_mode_workgroup
:
2249 /* Create the variable normally */
2250 var
->var
= rzalloc(b
->shader
, nir_variable
);
2251 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2252 /* Workgroup variables don't have any explicit layout but some
2253 * layouts may have leaked through due to type deduplication in the
2256 var
->var
->type
= var
->type
->type
;
2257 var
->var
->data
.mode
= nir_var_mem_shared
;
2260 case vtn_variable_mode_input
:
2261 case vtn_variable_mode_output
: {
2262 /* In order to know whether or not we're a per-vertex inout, we need
2263 * the patch qualifier. This means walking the variable decorations
2264 * early before we actually create any variables. Not a big deal.
2266 * GLSLang really likes to place decorations in the most interior
2267 * thing it possibly can. In particular, if you have a struct, it
2268 * will place the patch decorations on the struct members. This
2269 * should be handled by the variable splitting below just fine.
2271 * If you have an array-of-struct, things get even more weird as it
2272 * will place the patch decorations on the struct even though it's
2273 * inside an array and some of the members being patch and others not
2274 * makes no sense whatsoever. Since the only sensible thing is for
2275 * it to be all or nothing, we'll call it patch if any of the members
2276 * are declared patch.
2279 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
2280 if (glsl_type_is_array(var
->type
->type
) &&
2281 glsl_type_is_struct_or_ifc(without_array
->type
)) {
2282 vtn_foreach_decoration(b
, vtn_value(b
, without_array
->id
,
2283 vtn_value_type_type
),
2284 var_is_patch_cb
, &var
->patch
);
2287 /* For inputs and outputs, we immediately split structures. This
2288 * is for a couple of reasons. For one, builtins may all come in
2289 * a struct and we really want those split out into separate
2290 * variables. For another, interpolation qualifiers can be
2291 * applied to members of the top-level struct ane we need to be
2292 * able to preserve that information.
2295 struct vtn_type
*per_vertex_type
= var
->type
;
2296 if (is_per_vertex_inout(var
, b
->shader
->info
.stage
)) {
2297 /* In Geometry shaders (and some tessellation), inputs come
2298 * in per-vertex arrays. However, some builtins come in
2299 * non-per-vertex, hence the need for the is_array check. In
2300 * any case, there are no non-builtin arrays allowed so this
2301 * check should be sufficient.
2303 per_vertex_type
= var
->type
->array_element
;
2306 var
->var
= rzalloc(b
->shader
, nir_variable
);
2307 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2308 /* In Vulkan, shader I/O variables don't have any explicit layout but
2309 * some layouts may have leaked through due to type deduplication in
2310 * the SPIR-V. We do, however, keep the layouts in the variable's
2311 * interface_type because we need offsets for XFB arrays of blocks.
2313 var
->var
->type
= var
->type
->type
;
2314 var
->var
->data
.mode
= nir_mode
;
2315 var
->var
->data
.patch
= var
->patch
;
2317 /* Figure out the interface block type. */
2318 struct vtn_type
*iface_type
= per_vertex_type
;
2319 if (var
->mode
== vtn_variable_mode_output
&&
2320 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
2321 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
||
2322 b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)) {
2323 /* For vertex data outputs, we can end up with arrays of blocks for
2324 * transform feedback where each array element corresponds to a
2325 * different XFB output buffer.
2327 while (iface_type
->base_type
== vtn_base_type_array
)
2328 iface_type
= iface_type
->array_element
;
2330 if (iface_type
->base_type
== vtn_base_type_struct
&& iface_type
->block
)
2331 var
->var
->interface_type
= iface_type
->type
;
2333 if (per_vertex_type
->base_type
== vtn_base_type_struct
&&
2334 per_vertex_type
->block
) {
2335 /* It's a struct. Set it up as per-member. */
2336 var
->var
->num_members
= glsl_get_length(per_vertex_type
->type
);
2337 var
->var
->members
= rzalloc_array(var
->var
, struct nir_variable_data
,
2338 var
->var
->num_members
);
2340 for (unsigned i
= 0; i
< var
->var
->num_members
; i
++) {
2341 var
->var
->members
[i
].mode
= nir_mode
;
2342 var
->var
->members
[i
].patch
= var
->patch
;
2343 var
->var
->members
[i
].location
= -1;
2347 /* For inputs and outputs, we need to grab locations and builtin
2348 * information from the per-vertex type.
2350 vtn_foreach_decoration(b
, vtn_value(b
, per_vertex_type
->id
,
2351 vtn_value_type_type
),
2352 var_decoration_cb
, var
);
2356 case vtn_variable_mode_push_constant
:
2357 case vtn_variable_mode_cross_workgroup
:
2358 /* These don't need actual variables. */
2361 case vtn_variable_mode_image
:
2362 case vtn_variable_mode_phys_ssbo
:
2363 unreachable("Should have been caught before");
2366 /* We can only have one type of initializer */
2367 assert(!(const_initializer
&& var_initializer
));
2368 if (const_initializer
) {
2369 var
->var
->constant_initializer
=
2370 nir_constant_clone(const_initializer
, var
->var
);
2372 if (var_initializer
)
2373 var
->var
->pointer_initializer
= var_initializer
;
2375 if (var
->mode
== vtn_variable_mode_uniform
||
2376 var
->mode
== vtn_variable_mode_ssbo
) {
2377 /* SSBOs and images are assumed to not alias in the Simple, GLSL and Vulkan memory models */
2378 var
->var
->data
.access
|= b
->mem_model
!= SpvMemoryModelOpenCL
? ACCESS_RESTRICT
: 0;
2381 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
2382 vtn_foreach_decoration(b
, val
, ptr_decoration_cb
, val
->pointer
);
2384 /* Propagate access flags from the OpVariable decorations. */
2385 val
->pointer
->access
|= var
->access
;
2387 if ((var
->mode
== vtn_variable_mode_input
||
2388 var
->mode
== vtn_variable_mode_output
) &&
2389 var
->var
->members
) {
2390 assign_missing_member_locations(var
);
2393 if (var
->mode
== vtn_variable_mode_uniform
||
2394 var
->mode
== vtn_variable_mode_ubo
||
2395 var
->mode
== vtn_variable_mode_ssbo
) {
2396 /* XXX: We still need the binding information in the nir_variable
2397 * for these. We should fix that.
2399 var
->var
->data
.binding
= var
->binding
;
2400 var
->var
->data
.explicit_binding
= var
->explicit_binding
;
2401 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
2402 var
->var
->data
.index
= var
->input_attachment_index
;
2403 var
->var
->data
.offset
= var
->offset
;
2405 if (glsl_type_is_image(without_array
->type
))
2406 var
->var
->data
.image
.format
= without_array
->image_format
;
2409 if (var
->mode
== vtn_variable_mode_function
) {
2410 vtn_assert(var
->var
!= NULL
&& var
->var
->members
== NULL
);
2411 nir_function_impl_add_variable(b
->nb
.impl
, var
->var
);
2412 } else if (var
->var
) {
2413 nir_shader_add_variable(b
->shader
, var
->var
);
2415 vtn_assert(vtn_pointer_is_external_block(b
, val
->pointer
));
2420 vtn_assert_types_equal(struct vtn_builder
*b
, SpvOp opcode
,
2421 struct vtn_type
*dst_type
,
2422 struct vtn_type
*src_type
)
2424 if (dst_type
->id
== src_type
->id
)
2427 if (vtn_types_compatible(b
, dst_type
, src_type
)) {
2428 /* Early versions of GLSLang would re-emit types unnecessarily and you
2429 * would end up with OpLoad, OpStore, or OpCopyMemory opcodes which have
2430 * mismatched source and destination types.
2432 * https://github.com/KhronosGroup/glslang/issues/304
2433 * https://github.com/KhronosGroup/glslang/issues/307
2434 * https://bugs.freedesktop.org/show_bug.cgi?id=104338
2435 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2437 vtn_warn("Source and destination types of %s do not have the same "
2438 "ID (but are compatible): %u vs %u",
2439 spirv_op_to_string(opcode
), dst_type
->id
, src_type
->id
);
2443 vtn_fail("Source and destination types of %s do not match: %s vs. %s",
2444 spirv_op_to_string(opcode
),
2445 glsl_get_type_name(dst_type
->type
),
2446 glsl_get_type_name(src_type
->type
));
2449 static nir_ssa_def
*
2450 nir_shrink_zero_pad_vec(nir_builder
*b
, nir_ssa_def
*val
,
2451 unsigned num_components
)
2453 if (val
->num_components
== num_components
)
2456 nir_ssa_def
*comps
[NIR_MAX_VEC_COMPONENTS
];
2457 for (unsigned i
= 0; i
< num_components
; i
++) {
2458 if (i
< val
->num_components
)
2459 comps
[i
] = nir_channel(b
, val
, i
);
2461 comps
[i
] = nir_imm_intN_t(b
, 0, val
->bit_size
);
2463 return nir_vec(b
, comps
, num_components
);
2466 static nir_ssa_def
*
2467 nir_sloppy_bitcast(nir_builder
*b
, nir_ssa_def
*val
,
2468 const struct glsl_type
*type
)
2470 const unsigned num_components
= glsl_get_vector_elements(type
);
2471 const unsigned bit_size
= glsl_get_bit_size(type
);
2473 /* First, zero-pad to ensure that the value is big enough that when we
2474 * bit-cast it, we don't loose anything.
2476 if (val
->bit_size
< bit_size
) {
2477 const unsigned src_num_components_needed
=
2478 vtn_align_u32(val
->num_components
, bit_size
/ val
->bit_size
);
2479 val
= nir_shrink_zero_pad_vec(b
, val
, src_num_components_needed
);
2482 val
= nir_bitcast_vector(b
, val
, bit_size
);
2484 return nir_shrink_zero_pad_vec(b
, val
, num_components
);
2488 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
2489 const uint32_t *w
, unsigned count
)
2493 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
2494 val
->type
= vtn_get_type(b
, w
[1]);
2498 case SpvOpVariable
: {
2499 struct vtn_type
*ptr_type
= vtn_get_type(b
, w
[1]);
2501 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2503 SpvStorageClass storage_class
= w
[3];
2504 nir_constant
*const_initializer
= NULL
;
2505 nir_variable
*var_initializer
= NULL
;
2507 struct vtn_value
*init
= vtn_untyped_value(b
, w
[4]);
2508 switch (init
->value_type
) {
2509 case vtn_value_type_constant
:
2510 const_initializer
= init
->constant
;
2512 case vtn_value_type_pointer
:
2513 var_initializer
= init
->pointer
->var
->var
;
2516 vtn_fail("SPIR-V variable initializer %u must be constant or pointer",
2521 vtn_create_variable(b
, val
, ptr_type
, storage_class
, const_initializer
, var_initializer
);
2526 case SpvOpAccessChain
:
2527 case SpvOpPtrAccessChain
:
2528 case SpvOpInBoundsAccessChain
:
2529 case SpvOpInBoundsPtrAccessChain
: {
2530 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
2531 enum gl_access_qualifier access
= 0;
2532 chain
->ptr_as_array
= (opcode
== SpvOpPtrAccessChain
|| opcode
== SpvOpInBoundsPtrAccessChain
);
2535 for (int i
= 4; i
< count
; i
++) {
2536 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
2537 if (link_val
->value_type
== vtn_value_type_constant
) {
2538 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
2539 chain
->link
[idx
].id
= vtn_constant_int(b
, w
[i
]);
2541 chain
->link
[idx
].mode
= vtn_access_mode_id
;
2542 chain
->link
[idx
].id
= w
[i
];
2547 struct vtn_type
*ptr_type
= vtn_get_type(b
, w
[1]);
2548 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
2549 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
2550 /* This is rather insane. SPIR-V allows you to use OpSampledImage
2551 * to combine an array of images with a single sampler to get an
2552 * array of sampled images that all share the same sampler.
2553 * Fortunately, this means that we can more-or-less ignore the
2554 * sampler when crawling the access chain, but it does leave us
2555 * with this rather awkward little special-case.
2557 struct vtn_value
*val
=
2558 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2559 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2560 val
->sampled_image
->image
=
2561 vtn_pointer_dereference(b
, base_val
->sampled_image
->image
, chain
);
2562 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
2563 val
->sampled_image
->image
=
2564 vtn_decorate_pointer(b
, val
, val
->sampled_image
->image
);
2565 val
->sampled_image
->sampler
=
2566 vtn_decorate_pointer(b
, val
, val
->sampled_image
->sampler
);
2568 vtn_assert(base_val
->value_type
== vtn_value_type_pointer
);
2569 struct vtn_pointer
*ptr
=
2570 vtn_pointer_dereference(b
, base_val
->pointer
, chain
);
2571 ptr
->ptr_type
= ptr_type
;
2572 ptr
->access
|= access
;
2573 vtn_push_pointer(b
, w
[2], ptr
);
2578 case SpvOpCopyMemory
: {
2579 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2580 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
2582 vtn_assert_types_equal(b
, opcode
, dest
->type
->deref
, src
->type
->deref
);
2584 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
2589 struct vtn_type
*res_type
= vtn_get_type(b
, w
[1]);
2590 struct vtn_value
*src_val
= vtn_value(b
, w
[3], vtn_value_type_pointer
);
2591 struct vtn_pointer
*src
= src_val
->pointer
;
2593 vtn_assert_types_equal(b
, opcode
, res_type
, src_val
->type
->deref
);
2595 if (res_type
->base_type
== vtn_base_type_image
||
2596 res_type
->base_type
== vtn_base_type_sampler
) {
2597 vtn_push_pointer(b
, w
[2], src
);
2599 } else if (res_type
->base_type
== vtn_base_type_sampled_image
) {
2600 struct vtn_value
*val
=
2601 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2602 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2603 val
->sampled_image
->image
= val
->sampled_image
->sampler
=
2604 vtn_decorate_pointer(b
, val
, src
);
2610 SpvMemoryAccessMask access
= w
[4];
2611 if (access
& SpvMemoryAccessAlignedMask
)
2614 if (access
& SpvMemoryAccessMakePointerVisibleMask
) {
2615 SpvMemorySemanticsMask semantics
=
2616 SpvMemorySemanticsMakeVisibleMask
|
2617 vtn_storage_class_to_memory_semantics(src
->ptr_type
->storage_class
);
2619 SpvScope scope
= vtn_constant_uint(b
, w
[idx
]);
2620 vtn_emit_memory_barrier(b
, scope
, semantics
);
2624 vtn_push_ssa(b
, w
[2], res_type
, vtn_variable_load(b
, src
));
2629 struct vtn_value
*dest_val
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2630 struct vtn_pointer
*dest
= dest_val
->pointer
;
2631 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[2]);
2633 /* OpStore requires us to actually have a storage type */
2634 vtn_fail_if(dest
->type
->type
== NULL
,
2635 "Invalid destination type for OpStore");
2637 if (glsl_get_base_type(dest
->type
->type
) == GLSL_TYPE_BOOL
&&
2638 glsl_get_base_type(src_val
->type
->type
) == GLSL_TYPE_UINT
) {
2639 /* Early versions of GLSLang would use uint types for UBOs/SSBOs but
2640 * would then store them to a local variable as bool. Work around
2641 * the issue by doing an implicit conversion.
2643 * https://github.com/KhronosGroup/glslang/issues/170
2644 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2646 vtn_warn("OpStore of value of type OpTypeInt to a pointer to type "
2647 "OpTypeBool. Doing an implicit conversion to work around "
2649 struct vtn_ssa_value
*bool_ssa
=
2650 vtn_create_ssa_value(b
, dest
->type
->type
);
2651 bool_ssa
->def
= nir_i2b(&b
->nb
, vtn_ssa_value(b
, w
[2])->def
);
2652 vtn_variable_store(b
, bool_ssa
, dest
);
2656 vtn_assert_types_equal(b
, opcode
, dest_val
->type
->deref
, src_val
->type
);
2658 if (glsl_type_is_sampler(dest
->type
->type
)) {
2659 if (b
->wa_glslang_179
) {
2660 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
2661 "propagation to workaround the problem.");
2662 vtn_assert(dest
->var
->copy_prop_sampler
== NULL
);
2663 struct vtn_value
*v
= vtn_untyped_value(b
, w
[2]);
2664 if (v
->value_type
== vtn_value_type_sampled_image
) {
2665 dest
->var
->copy_prop_sampler
= v
->sampled_image
->sampler
;
2667 vtn_assert(v
->value_type
== vtn_value_type_pointer
);
2668 dest
->var
->copy_prop_sampler
= v
->pointer
;
2671 vtn_fail("Vulkan does not allow OpStore of a sampler or image.");
2676 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
2677 vtn_variable_store(b
, src
, dest
);
2681 SpvMemoryAccessMask access
= w
[3];
2683 if (access
& SpvMemoryAccessAlignedMask
)
2686 if (access
& SpvMemoryAccessMakePointerAvailableMask
) {
2687 SpvMemorySemanticsMask semantics
=
2688 SpvMemorySemanticsMakeAvailableMask
|
2689 vtn_storage_class_to_memory_semantics(dest
->ptr_type
->storage_class
);
2690 SpvScope scope
= vtn_constant_uint(b
, w
[idx
]);
2691 vtn_emit_memory_barrier(b
, scope
, semantics
);
2697 case SpvOpArrayLength
: {
2698 struct vtn_pointer
*ptr
=
2699 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2700 const uint32_t field
= w
[4];
2702 vtn_fail_if(ptr
->type
->base_type
!= vtn_base_type_struct
,
2703 "OpArrayLength must take a pointer to a structure type");
2704 vtn_fail_if(field
!= ptr
->type
->length
- 1 ||
2705 ptr
->type
->members
[field
]->base_type
!= vtn_base_type_array
,
2706 "OpArrayLength must reference the last memeber of the "
2707 "structure and that must be an array");
2709 const uint32_t offset
= ptr
->type
->offsets
[field
];
2710 const uint32_t stride
= ptr
->type
->members
[field
]->stride
;
2712 if (!ptr
->block_index
) {
2713 struct vtn_access_chain chain
= {
2716 ptr
= vtn_pointer_dereference(b
, ptr
, &chain
);
2717 vtn_assert(ptr
->block_index
);
2720 nir_intrinsic_instr
*instr
=
2721 nir_intrinsic_instr_create(b
->nb
.shader
,
2722 nir_intrinsic_get_buffer_size
);
2723 instr
->src
[0] = nir_src_for_ssa(ptr
->block_index
);
2724 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
2725 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2726 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
2728 /* array_length = max(buffer_size - offset, 0) / stride */
2729 nir_ssa_def
*array_length
=
2734 nir_imm_int(&b
->nb
, offset
)),
2735 nir_imm_int(&b
->nb
, 0u)),
2736 nir_imm_int(&b
->nb
, stride
));
2738 vtn_push_nir_ssa(b
, w
[2], array_length
);
2742 case SpvOpConvertPtrToU
: {
2743 struct vtn_value
*u_val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2745 vtn_fail_if(u_val
->type
->base_type
!= vtn_base_type_vector
&&
2746 u_val
->type
->base_type
!= vtn_base_type_scalar
,
2747 "OpConvertPtrToU can only be used to cast to a vector or "
2750 /* The pointer will be converted to an SSA value automatically */
2751 struct vtn_ssa_value
*ptr_ssa
= vtn_ssa_value(b
, w
[3]);
2753 u_val
->ssa
= vtn_create_ssa_value(b
, u_val
->type
->type
);
2754 u_val
->ssa
->def
= nir_sloppy_bitcast(&b
->nb
, ptr_ssa
->def
, u_val
->type
->type
);
2758 case SpvOpConvertUToPtr
: {
2759 struct vtn_value
*ptr_val
=
2760 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2761 struct vtn_value
*u_val
= vtn_untyped_value(b
, w
[3]);
2763 vtn_fail_if(ptr_val
->type
->type
== NULL
,
2764 "OpConvertUToPtr can only be used on physical pointers");
2766 vtn_fail_if(u_val
->type
->base_type
!= vtn_base_type_vector
&&
2767 u_val
->type
->base_type
!= vtn_base_type_scalar
,
2768 "OpConvertUToPtr can only be used to cast from a vector or "
2771 struct vtn_ssa_value
*u_ssa
= vtn_ssa_value(b
, w
[3]);
2772 nir_ssa_def
*ptr_ssa
= nir_sloppy_bitcast(&b
->nb
, u_ssa
->def
,
2773 ptr_val
->type
->type
);
2774 ptr_val
->pointer
= vtn_pointer_from_ssa(b
, ptr_ssa
, ptr_val
->type
);
2775 vtn_foreach_decoration(b
, ptr_val
, ptr_decoration_cb
, ptr_val
->pointer
);
2779 case SpvOpCopyMemorySized
:
2781 vtn_fail_with_opcode("Unhandled opcode", opcode
);