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_copy_value(struct vtn_builder
*b
, uint32_t src_value_id
,
81 uint32_t dst_value_id
)
83 struct vtn_value
*src
= vtn_untyped_value(b
, src_value_id
);
84 struct vtn_value
*dst
= vtn_push_value(b
, dst_value_id
, src
->value_type
);
85 struct vtn_value src_copy
= *src
;
87 vtn_fail_if(dst
->type
->id
!= src
->type
->id
,
88 "Result Type must equal Operand type");
90 src_copy
.name
= dst
->name
;
91 src_copy
.decoration
= dst
->decoration
;
92 src_copy
.type
= dst
->type
;
95 if (dst
->value_type
== vtn_value_type_pointer
)
96 dst
->pointer
= vtn_decorate_pointer(b
, dst
, dst
->pointer
);
99 static struct vtn_access_chain
*
100 vtn_access_chain_create(struct vtn_builder
*b
, unsigned length
)
102 struct vtn_access_chain
*chain
;
104 /* Subtract 1 from the length since there's already one built in */
105 size_t size
= sizeof(*chain
) +
106 (MAX2(length
, 1) - 1) * sizeof(chain
->link
[0]);
107 chain
= rzalloc_size(b
, size
);
108 chain
->length
= length
;
114 vtn_mode_uses_ssa_offset(struct vtn_builder
*b
,
115 enum vtn_variable_mode mode
)
117 return ((mode
== vtn_variable_mode_ubo
||
118 mode
== vtn_variable_mode_ssbo
) &&
119 b
->options
->lower_ubo_ssbo_access_to_offsets
) ||
120 mode
== vtn_variable_mode_push_constant
;
124 vtn_pointer_is_external_block(struct vtn_builder
*b
,
125 struct vtn_pointer
*ptr
)
127 return ptr
->mode
== vtn_variable_mode_ssbo
||
128 ptr
->mode
== vtn_variable_mode_ubo
||
129 ptr
->mode
== vtn_variable_mode_phys_ssbo
||
130 ptr
->mode
== vtn_variable_mode_push_constant
;
134 vtn_access_link_as_ssa(struct vtn_builder
*b
, struct vtn_access_link link
,
135 unsigned stride
, unsigned bit_size
)
137 vtn_assert(stride
> 0);
138 if (link
.mode
== vtn_access_mode_literal
) {
139 return nir_imm_intN_t(&b
->nb
, link
.id
* stride
, bit_size
);
141 nir_ssa_def
*ssa
= vtn_ssa_value(b
, link
.id
)->def
;
142 if (ssa
->bit_size
!= bit_size
)
143 ssa
= nir_i2i(&b
->nb
, ssa
, bit_size
);
144 return nir_imul_imm(&b
->nb
, ssa
, stride
);
148 static VkDescriptorType
149 vk_desc_type_for_mode(struct vtn_builder
*b
, enum vtn_variable_mode mode
)
152 case vtn_variable_mode_ubo
:
153 return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
;
154 case vtn_variable_mode_ssbo
:
155 return VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
;
157 vtn_fail("Invalid mode for vulkan_resource_index");
162 vtn_variable_resource_index(struct vtn_builder
*b
, struct vtn_variable
*var
,
163 nir_ssa_def
*desc_array_index
)
165 vtn_assert(b
->options
->environment
== NIR_SPIRV_VULKAN
);
167 if (!desc_array_index
) {
168 vtn_assert(glsl_type_is_struct_or_ifc(var
->type
->type
));
169 desc_array_index
= nir_imm_int(&b
->nb
, 0);
172 nir_intrinsic_instr
*instr
=
173 nir_intrinsic_instr_create(b
->nb
.shader
,
174 nir_intrinsic_vulkan_resource_index
);
175 instr
->src
[0] = nir_src_for_ssa(desc_array_index
);
176 nir_intrinsic_set_desc_set(instr
, var
->descriptor_set
);
177 nir_intrinsic_set_binding(instr
, var
->binding
);
178 nir_intrinsic_set_desc_type(instr
, vk_desc_type_for_mode(b
, var
->mode
));
180 vtn_fail_if(var
->mode
!= vtn_variable_mode_ubo
&&
181 var
->mode
!= vtn_variable_mode_ssbo
,
182 "Invalid mode for vulkan_resource_index");
184 nir_address_format addr_format
= vtn_mode_to_address_format(b
, var
->mode
);
185 const struct glsl_type
*index_type
=
186 b
->options
->lower_ubo_ssbo_access_to_offsets
?
187 glsl_uint_type() : nir_address_format_to_glsl_type(addr_format
);
189 instr
->num_components
= glsl_get_vector_elements(index_type
);
190 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, instr
->num_components
,
191 glsl_get_bit_size(index_type
), NULL
);
192 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
194 return &instr
->dest
.ssa
;
198 vtn_resource_reindex(struct vtn_builder
*b
, enum vtn_variable_mode mode
,
199 nir_ssa_def
*base_index
, nir_ssa_def
*offset_index
)
201 vtn_assert(b
->options
->environment
== NIR_SPIRV_VULKAN
);
203 nir_intrinsic_instr
*instr
=
204 nir_intrinsic_instr_create(b
->nb
.shader
,
205 nir_intrinsic_vulkan_resource_reindex
);
206 instr
->src
[0] = nir_src_for_ssa(base_index
);
207 instr
->src
[1] = nir_src_for_ssa(offset_index
);
208 nir_intrinsic_set_desc_type(instr
, vk_desc_type_for_mode(b
, mode
));
210 vtn_fail_if(mode
!= vtn_variable_mode_ubo
&& mode
!= vtn_variable_mode_ssbo
,
211 "Invalid mode for vulkan_resource_reindex");
213 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
214 const struct glsl_type
*index_type
=
215 b
->options
->lower_ubo_ssbo_access_to_offsets
?
216 glsl_uint_type() : nir_address_format_to_glsl_type(addr_format
);
218 instr
->num_components
= glsl_get_vector_elements(index_type
);
219 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, instr
->num_components
,
220 glsl_get_bit_size(index_type
), NULL
);
221 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
223 return &instr
->dest
.ssa
;
227 vtn_descriptor_load(struct vtn_builder
*b
, enum vtn_variable_mode mode
,
228 nir_ssa_def
*desc_index
)
230 vtn_assert(b
->options
->environment
== NIR_SPIRV_VULKAN
);
232 nir_intrinsic_instr
*desc_load
=
233 nir_intrinsic_instr_create(b
->nb
.shader
,
234 nir_intrinsic_load_vulkan_descriptor
);
235 desc_load
->src
[0] = nir_src_for_ssa(desc_index
);
236 nir_intrinsic_set_desc_type(desc_load
, vk_desc_type_for_mode(b
, mode
));
238 vtn_fail_if(mode
!= vtn_variable_mode_ubo
&& mode
!= vtn_variable_mode_ssbo
,
239 "Invalid mode for load_vulkan_descriptor");
241 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
242 const struct glsl_type
*ptr_type
=
243 nir_address_format_to_glsl_type(addr_format
);
245 desc_load
->num_components
= glsl_get_vector_elements(ptr_type
);
246 nir_ssa_dest_init(&desc_load
->instr
, &desc_load
->dest
,
247 desc_load
->num_components
,
248 glsl_get_bit_size(ptr_type
), NULL
);
249 nir_builder_instr_insert(&b
->nb
, &desc_load
->instr
);
251 return &desc_load
->dest
.ssa
;
254 /* Dereference the given base pointer by the access chain */
255 static struct vtn_pointer
*
256 vtn_nir_deref_pointer_dereference(struct vtn_builder
*b
,
257 struct vtn_pointer
*base
,
258 struct vtn_access_chain
*deref_chain
)
260 struct vtn_type
*type
= base
->type
;
261 enum gl_access_qualifier access
= base
->access
| deref_chain
->access
;
264 nir_deref_instr
*tail
;
267 } else if (b
->options
->environment
== NIR_SPIRV_VULKAN
&&
268 vtn_pointer_is_external_block(b
, base
)) {
269 nir_ssa_def
*block_index
= base
->block_index
;
271 /* We dereferencing an external block pointer. Correctness of this
272 * operation relies on one particular line in the SPIR-V spec, section
273 * entitled "Validation Rules for Shader Capabilities":
275 * "Block and BufferBlock decorations cannot decorate a structure
276 * type that is nested at any level inside another structure type
277 * decorated with Block or BufferBlock."
279 * This means that we can detect the point where we cross over from
280 * descriptor indexing to buffer indexing by looking for the block
281 * decorated struct type. Anything before the block decorated struct
282 * type is a descriptor indexing operation and anything after the block
283 * decorated struct is a buffer offset operation.
286 /* Figure out the descriptor array index if any
288 * Some of the Vulkan CTS tests with hand-rolled SPIR-V have been known
289 * to forget the Block or BufferBlock decoration from time to time.
290 * It's more robust if we check for both !block_index and for the type
291 * to contain a block. This way there's a decent chance that arrays of
292 * UBOs/SSBOs will work correctly even if variable pointers are
295 nir_ssa_def
*desc_arr_idx
= NULL
;
296 if (!block_index
|| vtn_type_contains_block(b
, type
)) {
297 /* If our type contains a block, then we're still outside the block
298 * and we need to process enough levels of dereferences to get inside
301 if (deref_chain
->ptr_as_array
) {
302 unsigned aoa_size
= glsl_get_aoa_size(type
->type
);
303 desc_arr_idx
= vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
304 MAX2(aoa_size
, 1), 32);
308 for (; idx
< deref_chain
->length
; idx
++) {
309 if (type
->base_type
!= vtn_base_type_array
) {
310 vtn_assert(type
->base_type
== vtn_base_type_struct
);
314 unsigned aoa_size
= glsl_get_aoa_size(type
->array_element
->type
);
315 nir_ssa_def
*arr_offset
=
316 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
317 MAX2(aoa_size
, 1), 32);
319 desc_arr_idx
= nir_iadd(&b
->nb
, desc_arr_idx
, arr_offset
);
321 desc_arr_idx
= arr_offset
;
323 type
= type
->array_element
;
324 access
|= type
->access
;
329 vtn_assert(base
->var
&& base
->type
);
330 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
331 } else if (desc_arr_idx
) {
332 block_index
= vtn_resource_reindex(b
, base
->mode
,
333 block_index
, desc_arr_idx
);
336 if (idx
== deref_chain
->length
) {
337 /* The entire deref was consumed in finding the block index. Return
338 * a pointer which just has a block index and a later access chain
339 * will dereference deeper.
341 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
342 ptr
->mode
= base
->mode
;
344 ptr
->block_index
= block_index
;
345 ptr
->access
= access
;
349 /* If we got here, there's more access chain to handle and we have the
350 * final block index. Insert a descriptor load and cast to a deref to
351 * start the deref chain.
353 nir_ssa_def
*desc
= vtn_descriptor_load(b
, base
->mode
, block_index
);
355 assert(base
->mode
== vtn_variable_mode_ssbo
||
356 base
->mode
== vtn_variable_mode_ubo
);
357 nir_variable_mode nir_mode
=
358 base
->mode
== vtn_variable_mode_ssbo
? nir_var_mem_ssbo
: nir_var_mem_ubo
;
360 tail
= nir_build_deref_cast(&b
->nb
, desc
, nir_mode
, type
->type
,
361 base
->ptr_type
->stride
);
363 assert(base
->var
&& base
->var
->var
);
364 tail
= nir_build_deref_var(&b
->nb
, base
->var
->var
);
365 if (base
->ptr_type
&& base
->ptr_type
->type
) {
366 tail
->dest
.ssa
.num_components
=
367 glsl_get_vector_elements(base
->ptr_type
->type
);
368 tail
->dest
.ssa
.bit_size
= glsl_get_bit_size(base
->ptr_type
->type
);
372 if (idx
== 0 && deref_chain
->ptr_as_array
) {
373 /* We start with a deref cast to get the stride. Hopefully, we'll be
374 * able to delete that cast eventually.
376 tail
= nir_build_deref_cast(&b
->nb
, &tail
->dest
.ssa
, tail
->mode
,
377 tail
->type
, base
->ptr_type
->stride
);
379 nir_ssa_def
*index
= vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1,
380 tail
->dest
.ssa
.bit_size
);
381 tail
= nir_build_deref_ptr_as_array(&b
->nb
, tail
, index
);
385 for (; idx
< deref_chain
->length
; idx
++) {
386 if (glsl_type_is_struct_or_ifc(type
->type
)) {
387 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
388 unsigned field
= deref_chain
->link
[idx
].id
;
389 tail
= nir_build_deref_struct(&b
->nb
, tail
, field
);
390 type
= type
->members
[field
];
392 nir_ssa_def
*arr_index
=
393 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
], 1,
394 tail
->dest
.ssa
.bit_size
);
395 tail
= nir_build_deref_array(&b
->nb
, tail
, arr_index
);
396 type
= type
->array_element
;
399 access
|= type
->access
;
402 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
403 ptr
->mode
= base
->mode
;
405 ptr
->var
= base
->var
;
407 ptr
->access
= access
;
412 static struct vtn_pointer
*
413 vtn_ssa_offset_pointer_dereference(struct vtn_builder
*b
,
414 struct vtn_pointer
*base
,
415 struct vtn_access_chain
*deref_chain
)
417 nir_ssa_def
*block_index
= base
->block_index
;
418 nir_ssa_def
*offset
= base
->offset
;
419 struct vtn_type
*type
= base
->type
;
420 enum gl_access_qualifier access
= base
->access
;
423 if (base
->mode
== vtn_variable_mode_ubo
||
424 base
->mode
== vtn_variable_mode_ssbo
) {
426 vtn_assert(base
->var
&& base
->type
);
427 nir_ssa_def
*desc_arr_idx
;
428 if (glsl_type_is_array(type
->type
)) {
429 if (deref_chain
->length
>= 1) {
431 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
433 /* This consumes a level of type */
434 type
= type
->array_element
;
435 access
|= type
->access
;
437 /* This is annoying. We've been asked for a pointer to the
438 * array of UBOs/SSBOs and not a specifc buffer. Return a
439 * pointer with a descriptor index of 0 and we'll have to do
440 * a reindex later to adjust it to the right thing.
442 desc_arr_idx
= nir_imm_int(&b
->nb
, 0);
444 } else if (deref_chain
->ptr_as_array
) {
445 /* You can't have a zero-length OpPtrAccessChain */
446 vtn_assert(deref_chain
->length
>= 1);
447 desc_arr_idx
= vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
449 /* We have a regular non-array SSBO. */
452 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
453 } else if (deref_chain
->ptr_as_array
&&
454 type
->base_type
== vtn_base_type_struct
&& type
->block
) {
455 /* We are doing an OpPtrAccessChain on a pointer to a struct that is
456 * decorated block. This is an interesting corner in the SPIR-V
457 * spec. One interpretation would be that they client is clearly
458 * trying to treat that block as if it's an implicit array of blocks
459 * repeated in the buffer. However, the SPIR-V spec for the
460 * OpPtrAccessChain says:
462 * "Base is treated as the address of the first element of an
463 * array, and the Element element’s address is computed to be the
464 * base for the Indexes, as per OpAccessChain."
466 * Taken literally, that would mean that your struct type is supposed
467 * to be treated as an array of such a struct and, since it's
468 * decorated block, that means an array of blocks which corresponds
469 * to an array descriptor. Therefore, we need to do a reindex
470 * operation to add the index from the first link in the access chain
471 * to the index we recieved.
473 * The downside to this interpretation (there always is one) is that
474 * this might be somewhat surprising behavior to apps if they expect
475 * the implicit array behavior described above.
477 vtn_assert(deref_chain
->length
>= 1);
478 nir_ssa_def
*offset_index
=
479 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
482 block_index
= vtn_resource_reindex(b
, base
->mode
,
483 block_index
, offset_index
);
488 if (base
->mode
== vtn_variable_mode_workgroup
) {
489 /* SLM doesn't need nor have a block index */
490 vtn_assert(!block_index
);
492 /* We need the variable for the base offset */
493 vtn_assert(base
->var
);
495 /* We need ptr_type for size and alignment */
496 vtn_assert(base
->ptr_type
);
498 /* Assign location on first use so that we don't end up bloating SLM
499 * address space for variables which are never statically used.
501 if (base
->var
->shared_location
< 0) {
502 vtn_assert(base
->ptr_type
->length
> 0 && base
->ptr_type
->align
> 0);
503 b
->shader
->num_shared
= vtn_align_u32(b
->shader
->num_shared
,
504 base
->ptr_type
->align
);
505 base
->var
->shared_location
= b
->shader
->num_shared
;
506 b
->shader
->num_shared
+= base
->ptr_type
->length
;
509 offset
= nir_imm_int(&b
->nb
, base
->var
->shared_location
);
510 } else if (base
->mode
== vtn_variable_mode_push_constant
) {
511 /* Push constants neither need nor have a block index */
512 vtn_assert(!block_index
);
514 /* Start off with at the start of the push constant block. */
515 offset
= nir_imm_int(&b
->nb
, 0);
517 /* The code above should have ensured a block_index when needed. */
518 vtn_assert(block_index
);
520 /* Start off with at the start of the buffer. */
521 offset
= nir_imm_int(&b
->nb
, 0);
525 if (deref_chain
->ptr_as_array
&& idx
== 0) {
526 /* We need ptr_type for the stride */
527 vtn_assert(base
->ptr_type
);
529 /* We need at least one element in the chain */
530 vtn_assert(deref_chain
->length
>= 1);
532 nir_ssa_def
*elem_offset
=
533 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
534 base
->ptr_type
->stride
, offset
->bit_size
);
535 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
539 for (; idx
< deref_chain
->length
; idx
++) {
540 switch (glsl_get_base_type(type
->type
)) {
543 case GLSL_TYPE_UINT16
:
544 case GLSL_TYPE_INT16
:
545 case GLSL_TYPE_UINT8
:
547 case GLSL_TYPE_UINT64
:
548 case GLSL_TYPE_INT64
:
549 case GLSL_TYPE_FLOAT
:
550 case GLSL_TYPE_FLOAT16
:
551 case GLSL_TYPE_DOUBLE
:
553 case GLSL_TYPE_ARRAY
: {
554 nir_ssa_def
*elem_offset
=
555 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
556 type
->stride
, offset
->bit_size
);
557 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
558 type
= type
->array_element
;
559 access
|= type
->access
;
563 case GLSL_TYPE_INTERFACE
:
564 case GLSL_TYPE_STRUCT
: {
565 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
566 unsigned member
= deref_chain
->link
[idx
].id
;
567 offset
= nir_iadd_imm(&b
->nb
, offset
, type
->offsets
[member
]);
568 type
= type
->members
[member
];
569 access
|= type
->access
;
574 vtn_fail("Invalid type for deref");
578 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
579 ptr
->mode
= base
->mode
;
581 ptr
->block_index
= block_index
;
582 ptr
->offset
= offset
;
583 ptr
->access
= access
;
588 /* Dereference the given base pointer by the access chain */
589 static struct vtn_pointer
*
590 vtn_pointer_dereference(struct vtn_builder
*b
,
591 struct vtn_pointer
*base
,
592 struct vtn_access_chain
*deref_chain
)
594 if (vtn_pointer_uses_ssa_offset(b
, base
)) {
595 return vtn_ssa_offset_pointer_dereference(b
, base
, deref_chain
);
597 return vtn_nir_deref_pointer_dereference(b
, base
, deref_chain
);
601 /* Returns an atomic_uint type based on the original uint type. The returned
602 * type will be equivalent to the original one but will have an atomic_uint
603 * type as leaf instead of an uint.
605 * Manages uint scalars, arrays, and arrays of arrays of any nested depth.
607 static const struct glsl_type
*
608 repair_atomic_type(const struct glsl_type
*type
)
610 assert(glsl_get_base_type(glsl_without_array(type
)) == GLSL_TYPE_UINT
);
611 assert(glsl_type_is_scalar(glsl_without_array(type
)));
613 if (glsl_type_is_array(type
)) {
614 const struct glsl_type
*atomic
=
615 repair_atomic_type(glsl_get_array_element(type
));
617 return glsl_array_type(atomic
, glsl_get_length(type
),
618 glsl_get_explicit_stride(type
));
620 return glsl_atomic_uint_type();
625 vtn_pointer_to_deref(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
627 if (b
->wa_glslang_179
) {
628 /* Do on-the-fly copy propagation for samplers. */
629 if (ptr
->var
&& ptr
->var
->copy_prop_sampler
)
630 return vtn_pointer_to_deref(b
, ptr
->var
->copy_prop_sampler
);
633 vtn_assert(!vtn_pointer_uses_ssa_offset(b
, ptr
));
635 struct vtn_access_chain chain
= {
638 ptr
= vtn_nir_deref_pointer_dereference(b
, ptr
, &chain
);
645 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_instr
*deref
,
646 struct vtn_ssa_value
*inout
,
647 enum gl_access_qualifier access
)
649 if (glsl_type_is_vector_or_scalar(deref
->type
)) {
651 inout
->def
= nir_load_deref_with_access(&b
->nb
, deref
, access
);
653 nir_store_deref_with_access(&b
->nb
, deref
, inout
->def
, ~0, access
);
655 } else if (glsl_type_is_array(deref
->type
) ||
656 glsl_type_is_matrix(deref
->type
)) {
657 unsigned elems
= glsl_get_length(deref
->type
);
658 for (unsigned i
= 0; i
< elems
; i
++) {
659 nir_deref_instr
*child
=
660 nir_build_deref_array_imm(&b
->nb
, deref
, i
);
661 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
], access
);
664 vtn_assert(glsl_type_is_struct_or_ifc(deref
->type
));
665 unsigned elems
= glsl_get_length(deref
->type
);
666 for (unsigned i
= 0; i
< elems
; i
++) {
667 nir_deref_instr
*child
= nir_build_deref_struct(&b
->nb
, deref
, i
);
668 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
], access
);
674 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
676 struct vtn_pointer
*ptr
= vtn_value(b
, id
, vtn_value_type_pointer
)->pointer
;
677 return vtn_pointer_to_deref(b
, ptr
);
681 * Gets the NIR-level deref tail, which may have as a child an array deref
682 * selecting which component due to OpAccessChain supporting per-component
683 * indexing in SPIR-V.
685 static nir_deref_instr
*
686 get_deref_tail(nir_deref_instr
*deref
)
688 if (deref
->deref_type
!= nir_deref_type_array
)
691 nir_deref_instr
*parent
=
692 nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
);
694 if (glsl_type_is_vector(parent
->type
))
700 struct vtn_ssa_value
*
701 vtn_local_load(struct vtn_builder
*b
, nir_deref_instr
*src
,
702 enum gl_access_qualifier access
)
704 nir_deref_instr
*src_tail
= get_deref_tail(src
);
705 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
706 _vtn_local_load_store(b
, true, src_tail
, val
, access
);
708 if (src_tail
!= src
) {
709 val
->type
= src
->type
;
710 val
->def
= nir_vector_extract(&b
->nb
, val
->def
, src
->arr
.index
.ssa
);
717 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
718 nir_deref_instr
*dest
, enum gl_access_qualifier access
)
720 nir_deref_instr
*dest_tail
= get_deref_tail(dest
);
722 if (dest_tail
!= dest
) {
723 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
724 _vtn_local_load_store(b
, true, dest_tail
, val
, access
);
726 val
->def
= nir_vector_insert(&b
->nb
, val
->def
, src
->def
,
727 dest
->arr
.index
.ssa
);
728 _vtn_local_load_store(b
, false, dest_tail
, val
, access
);
730 _vtn_local_load_store(b
, false, dest_tail
, src
, access
);
735 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
736 nir_ssa_def
**index_out
)
738 assert(vtn_pointer_uses_ssa_offset(b
, ptr
));
740 struct vtn_access_chain chain
= {
743 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
745 *index_out
= ptr
->block_index
;
749 /* Tries to compute the size of an interface block based on the strides and
750 * offsets that are provided to us in the SPIR-V source.
753 vtn_type_block_size(struct vtn_builder
*b
, struct vtn_type
*type
)
755 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
759 case GLSL_TYPE_UINT16
:
760 case GLSL_TYPE_INT16
:
761 case GLSL_TYPE_UINT8
:
763 case GLSL_TYPE_UINT64
:
764 case GLSL_TYPE_INT64
:
765 case GLSL_TYPE_FLOAT
:
766 case GLSL_TYPE_FLOAT16
:
768 case GLSL_TYPE_DOUBLE
: {
769 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
770 glsl_get_matrix_columns(type
->type
);
772 vtn_assert(type
->stride
> 0);
773 return type
->stride
* cols
;
775 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
776 return glsl_get_vector_elements(type
->type
) * type_size
;
780 case GLSL_TYPE_STRUCT
:
781 case GLSL_TYPE_INTERFACE
: {
783 unsigned num_fields
= glsl_get_length(type
->type
);
784 for (unsigned f
= 0; f
< num_fields
; f
++) {
785 unsigned field_end
= type
->offsets
[f
] +
786 vtn_type_block_size(b
, type
->members
[f
]);
787 size
= MAX2(size
, field_end
);
792 case GLSL_TYPE_ARRAY
:
793 vtn_assert(type
->stride
> 0);
794 vtn_assert(glsl_get_length(type
->type
) > 0);
795 return type
->stride
* glsl_get_length(type
->type
);
798 vtn_fail("Invalid block type");
804 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
805 nir_ssa_def
*index
, nir_ssa_def
*offset
,
806 unsigned access_offset
, unsigned access_size
,
807 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
,
808 enum gl_access_qualifier access
)
810 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
811 instr
->num_components
= glsl_get_vector_elements(type
);
813 /* Booleans usually shouldn't show up in external memory in SPIR-V.
814 * However, they do for certain older GLSLang versions and can for shared
815 * memory when we lower access chains internally.
817 const unsigned data_bit_size
= glsl_type_is_boolean(type
) ? 32 :
818 glsl_get_bit_size(type
);
822 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
823 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
826 if (op
== nir_intrinsic_load_push_constant
) {
827 nir_intrinsic_set_base(instr
, access_offset
);
828 nir_intrinsic_set_range(instr
, access_size
);
831 if (op
== nir_intrinsic_load_ubo
||
832 op
== nir_intrinsic_load_ssbo
||
833 op
== nir_intrinsic_store_ssbo
) {
834 nir_intrinsic_set_access(instr
, access
);
837 /* With extensions like relaxed_block_layout, we really can't guarantee
838 * much more than scalar alignment.
840 if (op
!= nir_intrinsic_load_push_constant
)
841 nir_intrinsic_set_align(instr
, data_bit_size
/ 8, 0);
844 instr
->src
[src
++] = nir_src_for_ssa(index
);
846 if (op
== nir_intrinsic_load_push_constant
) {
847 /* We need to subtract the offset from where the intrinsic will load the
850 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
851 nir_imm_int(&b
->nb
, access_offset
)));
853 instr
->src
[src
++] = nir_src_for_ssa(offset
);
857 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
858 instr
->num_components
, data_bit_size
, NULL
);
859 (*inout
)->def
= &instr
->dest
.ssa
;
862 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
864 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
865 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
869 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
870 nir_ssa_def
*index
, nir_ssa_def
*offset
,
871 unsigned access_offset
, unsigned access_size
,
872 struct vtn_type
*type
, enum gl_access_qualifier access
,
873 struct vtn_ssa_value
**inout
)
875 if (load
&& *inout
== NULL
)
876 *inout
= vtn_create_ssa_value(b
, type
->type
);
878 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
882 case GLSL_TYPE_UINT16
:
883 case GLSL_TYPE_INT16
:
884 case GLSL_TYPE_UINT8
:
886 case GLSL_TYPE_UINT64
:
887 case GLSL_TYPE_INT64
:
888 case GLSL_TYPE_FLOAT
:
889 case GLSL_TYPE_FLOAT16
:
890 case GLSL_TYPE_DOUBLE
:
892 /* This is where things get interesting. At this point, we've hit
893 * a vector, a scalar, or a matrix.
895 if (glsl_type_is_matrix(type
->type
)) {
896 /* Loading the whole matrix */
897 struct vtn_ssa_value
*transpose
;
898 unsigned num_ops
, vec_width
, col_stride
;
899 if (type
->row_major
) {
900 num_ops
= glsl_get_vector_elements(type
->type
);
901 vec_width
= glsl_get_matrix_columns(type
->type
);
902 col_stride
= type
->array_element
->stride
;
904 const struct glsl_type
*transpose_type
=
905 glsl_matrix_type(base_type
, vec_width
, num_ops
);
906 *inout
= vtn_create_ssa_value(b
, transpose_type
);
908 transpose
= vtn_ssa_transpose(b
, *inout
);
912 num_ops
= glsl_get_matrix_columns(type
->type
);
913 vec_width
= glsl_get_vector_elements(type
->type
);
914 col_stride
= type
->stride
;
917 for (unsigned i
= 0; i
< num_ops
; i
++) {
918 nir_ssa_def
*elem_offset
=
919 nir_iadd_imm(&b
->nb
, offset
, i
* col_stride
);
920 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
921 access_offset
, access_size
,
923 glsl_vector_type(base_type
, vec_width
),
924 type
->access
| access
);
927 if (load
&& type
->row_major
)
928 *inout
= vtn_ssa_transpose(b
, *inout
);
930 unsigned elems
= glsl_get_vector_elements(type
->type
);
931 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
932 if (elems
== 1 || type
->stride
== type_size
) {
933 /* This is a tightly-packed normal scalar or vector load */
934 vtn_assert(glsl_type_is_vector_or_scalar(type
->type
));
935 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
936 access_offset
, access_size
,
938 type
->access
| access
);
940 /* This is a strided load. We have to load N things separately.
941 * This is the single column of a row-major matrix case.
943 vtn_assert(type
->stride
> type_size
);
944 vtn_assert(type
->stride
% type_size
== 0);
946 nir_ssa_def
*per_comp
[4];
947 for (unsigned i
= 0; i
< elems
; i
++) {
948 nir_ssa_def
*elem_offset
=
949 nir_iadd_imm(&b
->nb
, offset
, i
* type
->stride
);
950 struct vtn_ssa_value
*comp
, temp_val
;
952 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
953 temp_val
.type
= glsl_scalar_type(base_type
);
956 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
957 access_offset
, access_size
,
958 &comp
, glsl_scalar_type(base_type
),
959 type
->access
| access
);
960 per_comp
[i
] = comp
->def
;
965 *inout
= vtn_create_ssa_value(b
, type
->type
);
966 (*inout
)->def
= nir_vec(&b
->nb
, per_comp
, elems
);
972 case GLSL_TYPE_ARRAY
: {
973 unsigned elems
= glsl_get_length(type
->type
);
974 for (unsigned i
= 0; i
< elems
; i
++) {
975 nir_ssa_def
*elem_off
=
976 nir_iadd_imm(&b
->nb
, offset
, i
* type
->stride
);
977 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
978 access_offset
, access_size
,
980 type
->array_element
->access
| access
,
981 &(*inout
)->elems
[i
]);
986 case GLSL_TYPE_INTERFACE
:
987 case GLSL_TYPE_STRUCT
: {
988 unsigned elems
= glsl_get_length(type
->type
);
989 for (unsigned i
= 0; i
< elems
; i
++) {
990 nir_ssa_def
*elem_off
=
991 nir_iadd_imm(&b
->nb
, offset
, type
->offsets
[i
]);
992 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
993 access_offset
, access_size
,
995 type
->members
[i
]->access
| access
,
996 &(*inout
)->elems
[i
]);
1002 vtn_fail("Invalid block member type");
1006 static struct vtn_ssa_value
*
1007 vtn_block_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
1009 nir_intrinsic_op op
;
1010 unsigned access_offset
= 0, access_size
= 0;
1011 switch (src
->mode
) {
1012 case vtn_variable_mode_ubo
:
1013 op
= nir_intrinsic_load_ubo
;
1015 case vtn_variable_mode_ssbo
:
1016 op
= nir_intrinsic_load_ssbo
;
1018 case vtn_variable_mode_push_constant
:
1019 op
= nir_intrinsic_load_push_constant
;
1020 access_size
= b
->shader
->num_uniforms
;
1022 case vtn_variable_mode_workgroup
:
1023 op
= nir_intrinsic_load_shared
;
1026 vtn_fail("Invalid block variable mode");
1029 nir_ssa_def
*offset
, *index
= NULL
;
1030 offset
= vtn_pointer_to_offset(b
, src
, &index
);
1032 struct vtn_ssa_value
*value
= NULL
;
1033 _vtn_block_load_store(b
, op
, true, index
, offset
,
1034 access_offset
, access_size
,
1035 src
->type
, src
->access
, &value
);
1040 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
1041 struct vtn_pointer
*dst
)
1043 nir_intrinsic_op op
;
1044 switch (dst
->mode
) {
1045 case vtn_variable_mode_ssbo
:
1046 op
= nir_intrinsic_store_ssbo
;
1048 case vtn_variable_mode_workgroup
:
1049 op
= nir_intrinsic_store_shared
;
1052 vtn_fail("Invalid block variable mode");
1055 nir_ssa_def
*offset
, *index
= NULL
;
1056 offset
= vtn_pointer_to_offset(b
, dst
, &index
);
1058 _vtn_block_load_store(b
, op
, false, index
, offset
,
1059 0, 0, dst
->type
, dst
->access
, &src
);
1063 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
1064 struct vtn_pointer
*ptr
,
1065 enum gl_access_qualifier access
,
1066 struct vtn_ssa_value
**inout
)
1068 enum glsl_base_type base_type
= glsl_get_base_type(ptr
->type
->type
);
1069 switch (base_type
) {
1070 case GLSL_TYPE_UINT
:
1072 case GLSL_TYPE_UINT16
:
1073 case GLSL_TYPE_INT16
:
1074 case GLSL_TYPE_UINT8
:
1075 case GLSL_TYPE_INT8
:
1076 case GLSL_TYPE_UINT64
:
1077 case GLSL_TYPE_INT64
:
1078 case GLSL_TYPE_FLOAT
:
1079 case GLSL_TYPE_FLOAT16
:
1080 case GLSL_TYPE_BOOL
:
1081 case GLSL_TYPE_DOUBLE
:
1082 if (glsl_type_is_vector_or_scalar(ptr
->type
->type
)) {
1083 /* We hit a vector or scalar; go ahead and emit the load[s] */
1084 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
1085 if (vtn_pointer_is_external_block(b
, ptr
)) {
1086 /* If it's external, we call nir_load/store_deref directly. The
1087 * vtn_local_load/store helpers are too clever and do magic to
1088 * avoid array derefs of vectors. That magic is both less
1089 * efficient than the direct load/store and, in the case of
1090 * stores, is broken because it creates a race condition if two
1091 * threads are writing to different components of the same vector
1092 * due to the load+insert+store it uses to emulate the array
1096 *inout
= vtn_create_ssa_value(b
, ptr
->type
->type
);
1097 (*inout
)->def
= nir_load_deref_with_access(&b
->nb
, deref
,
1098 ptr
->type
->access
| access
);
1100 nir_store_deref_with_access(&b
->nb
, deref
, (*inout
)->def
, ~0,
1101 ptr
->type
->access
| access
);
1105 *inout
= vtn_local_load(b
, deref
, ptr
->type
->access
| access
);
1107 vtn_local_store(b
, *inout
, deref
, ptr
->type
->access
| access
);
1114 case GLSL_TYPE_INTERFACE
:
1115 case GLSL_TYPE_ARRAY
:
1116 case GLSL_TYPE_STRUCT
: {
1117 unsigned elems
= glsl_get_length(ptr
->type
->type
);
1119 vtn_assert(*inout
== NULL
);
1120 *inout
= rzalloc(b
, struct vtn_ssa_value
);
1121 (*inout
)->type
= ptr
->type
->type
;
1122 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
1125 struct vtn_access_chain chain
= {
1128 { .mode
= vtn_access_mode_literal
, },
1131 for (unsigned i
= 0; i
< elems
; i
++) {
1132 chain
.link
[0].id
= i
;
1133 struct vtn_pointer
*elem
= vtn_pointer_dereference(b
, ptr
, &chain
);
1134 _vtn_variable_load_store(b
, load
, elem
, ptr
->type
->access
| access
,
1135 &(*inout
)->elems
[i
]);
1141 vtn_fail("Invalid access chain type");
1145 struct vtn_ssa_value
*
1146 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
1148 if (vtn_pointer_uses_ssa_offset(b
, src
)) {
1149 return vtn_block_load(b
, src
);
1151 struct vtn_ssa_value
*val
= NULL
;
1152 _vtn_variable_load_store(b
, true, src
, src
->access
, &val
);
1158 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
1159 struct vtn_pointer
*dest
)
1161 if (vtn_pointer_uses_ssa_offset(b
, dest
)) {
1162 vtn_assert(dest
->mode
== vtn_variable_mode_ssbo
||
1163 dest
->mode
== vtn_variable_mode_workgroup
);
1164 vtn_block_store(b
, src
, dest
);
1166 _vtn_variable_load_store(b
, false, dest
, dest
->access
, &src
);
1171 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
1172 struct vtn_pointer
*src
)
1174 vtn_assert(src
->type
->type
== dest
->type
->type
);
1175 enum glsl_base_type base_type
= glsl_get_base_type(src
->type
->type
);
1176 switch (base_type
) {
1177 case GLSL_TYPE_UINT
:
1179 case GLSL_TYPE_UINT16
:
1180 case GLSL_TYPE_INT16
:
1181 case GLSL_TYPE_UINT8
:
1182 case GLSL_TYPE_INT8
:
1183 case GLSL_TYPE_UINT64
:
1184 case GLSL_TYPE_INT64
:
1185 case GLSL_TYPE_FLOAT
:
1186 case GLSL_TYPE_FLOAT16
:
1187 case GLSL_TYPE_DOUBLE
:
1188 case GLSL_TYPE_BOOL
:
1189 /* At this point, we have a scalar, vector, or matrix so we know that
1190 * there cannot be any structure splitting still in the way. By
1191 * stopping at the matrix level rather than the vector level, we
1192 * ensure that matrices get loaded in the optimal way even if they
1193 * are storred row-major in a UBO.
1195 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
1198 case GLSL_TYPE_INTERFACE
:
1199 case GLSL_TYPE_ARRAY
:
1200 case GLSL_TYPE_STRUCT
: {
1201 struct vtn_access_chain chain
= {
1204 { .mode
= vtn_access_mode_literal
, },
1207 unsigned elems
= glsl_get_length(src
->type
->type
);
1208 for (unsigned i
= 0; i
< elems
; i
++) {
1209 chain
.link
[0].id
= i
;
1210 struct vtn_pointer
*src_elem
=
1211 vtn_pointer_dereference(b
, src
, &chain
);
1212 struct vtn_pointer
*dest_elem
=
1213 vtn_pointer_dereference(b
, dest
, &chain
);
1215 _vtn_variable_copy(b
, dest_elem
, src_elem
);
1221 vtn_fail("Invalid access chain type");
1226 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
1227 struct vtn_pointer
*src
)
1229 /* TODO: At some point, we should add a special-case for when we can
1230 * just emit a copy_var intrinsic.
1232 _vtn_variable_copy(b
, dest
, src
);
1236 set_mode_system_value(struct vtn_builder
*b
, nir_variable_mode
*mode
)
1238 vtn_assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
1239 *mode
= nir_var_system_value
;
1243 vtn_get_builtin_location(struct vtn_builder
*b
,
1244 SpvBuiltIn builtin
, int *location
,
1245 nir_variable_mode
*mode
)
1248 case SpvBuiltInPosition
:
1249 *location
= VARYING_SLOT_POS
;
1251 case SpvBuiltInPointSize
:
1252 *location
= VARYING_SLOT_PSIZ
;
1254 case SpvBuiltInClipDistance
:
1255 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
1257 case SpvBuiltInCullDistance
:
1258 *location
= VARYING_SLOT_CULL_DIST0
;
1260 case SpvBuiltInVertexId
:
1261 case SpvBuiltInVertexIndex
:
1262 /* The Vulkan spec defines VertexIndex to be non-zero-based and doesn't
1263 * allow VertexId. The ARB_gl_spirv spec defines VertexId to be the
1264 * same as gl_VertexID, which is non-zero-based, and removes
1265 * VertexIndex. Since they're both defined to be non-zero-based, we use
1266 * SYSTEM_VALUE_VERTEX_ID for both.
1268 *location
= SYSTEM_VALUE_VERTEX_ID
;
1269 set_mode_system_value(b
, mode
);
1271 case SpvBuiltInInstanceIndex
:
1272 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
1273 set_mode_system_value(b
, mode
);
1275 case SpvBuiltInInstanceId
:
1276 *location
= SYSTEM_VALUE_INSTANCE_ID
;
1277 set_mode_system_value(b
, mode
);
1279 case SpvBuiltInPrimitiveId
:
1280 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1281 vtn_assert(*mode
== nir_var_shader_in
);
1282 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1283 } else if (*mode
== nir_var_shader_out
) {
1284 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1286 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
1287 set_mode_system_value(b
, mode
);
1290 case SpvBuiltInInvocationId
:
1291 *location
= SYSTEM_VALUE_INVOCATION_ID
;
1292 set_mode_system_value(b
, mode
);
1294 case SpvBuiltInLayer
:
1295 *location
= VARYING_SLOT_LAYER
;
1296 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1297 *mode
= nir_var_shader_in
;
1298 else if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1299 *mode
= nir_var_shader_out
;
1300 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1301 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1302 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1303 *mode
= nir_var_shader_out
;
1305 vtn_fail("invalid stage for SpvBuiltInLayer");
1307 case SpvBuiltInViewportIndex
:
1308 *location
= VARYING_SLOT_VIEWPORT
;
1309 if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1310 *mode
= nir_var_shader_out
;
1311 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1312 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1313 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1314 *mode
= nir_var_shader_out
;
1315 else if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1316 *mode
= nir_var_shader_in
;
1318 vtn_fail("invalid stage for SpvBuiltInViewportIndex");
1320 case SpvBuiltInTessLevelOuter
:
1321 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
1323 case SpvBuiltInTessLevelInner
:
1324 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
1326 case SpvBuiltInTessCoord
:
1327 *location
= SYSTEM_VALUE_TESS_COORD
;
1328 set_mode_system_value(b
, mode
);
1330 case SpvBuiltInPatchVertices
:
1331 *location
= SYSTEM_VALUE_VERTICES_IN
;
1332 set_mode_system_value(b
, mode
);
1334 case SpvBuiltInFragCoord
:
1335 vtn_assert(*mode
== nir_var_shader_in
);
1336 if (b
->options
&& b
->options
->frag_coord_is_sysval
) {
1337 *mode
= nir_var_system_value
;
1338 *location
= SYSTEM_VALUE_FRAG_COORD
;
1340 *location
= VARYING_SLOT_POS
;
1343 case SpvBuiltInPointCoord
:
1344 *location
= VARYING_SLOT_PNTC
;
1345 vtn_assert(*mode
== nir_var_shader_in
);
1347 case SpvBuiltInFrontFacing
:
1348 *location
= SYSTEM_VALUE_FRONT_FACE
;
1349 set_mode_system_value(b
, mode
);
1351 case SpvBuiltInSampleId
:
1352 *location
= SYSTEM_VALUE_SAMPLE_ID
;
1353 set_mode_system_value(b
, mode
);
1355 case SpvBuiltInSamplePosition
:
1356 *location
= SYSTEM_VALUE_SAMPLE_POS
;
1357 set_mode_system_value(b
, mode
);
1359 case SpvBuiltInSampleMask
:
1360 if (*mode
== nir_var_shader_out
) {
1361 *location
= FRAG_RESULT_SAMPLE_MASK
;
1363 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1364 set_mode_system_value(b
, mode
);
1367 case SpvBuiltInFragDepth
:
1368 *location
= FRAG_RESULT_DEPTH
;
1369 vtn_assert(*mode
== nir_var_shader_out
);
1371 case SpvBuiltInHelperInvocation
:
1372 *location
= SYSTEM_VALUE_HELPER_INVOCATION
;
1373 set_mode_system_value(b
, mode
);
1375 case SpvBuiltInNumWorkgroups
:
1376 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1377 set_mode_system_value(b
, mode
);
1379 case SpvBuiltInWorkgroupSize
:
1380 *location
= SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
1381 set_mode_system_value(b
, mode
);
1383 case SpvBuiltInWorkgroupId
:
1384 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1385 set_mode_system_value(b
, mode
);
1387 case SpvBuiltInLocalInvocationId
:
1388 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1389 set_mode_system_value(b
, mode
);
1391 case SpvBuiltInLocalInvocationIndex
:
1392 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1393 set_mode_system_value(b
, mode
);
1395 case SpvBuiltInGlobalInvocationId
:
1396 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1397 set_mode_system_value(b
, mode
);
1399 case SpvBuiltInGlobalLinearId
:
1400 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX
;
1401 set_mode_system_value(b
, mode
);
1403 case SpvBuiltInBaseVertex
:
1404 /* OpenGL gl_BaseVertex (SYSTEM_VALUE_BASE_VERTEX) is not the same
1405 * semantic as Vulkan BaseVertex (SYSTEM_VALUE_FIRST_VERTEX).
1407 if (b
->options
->environment
== NIR_SPIRV_OPENGL
)
1408 *location
= SYSTEM_VALUE_BASE_VERTEX
;
1410 *location
= SYSTEM_VALUE_FIRST_VERTEX
;
1411 set_mode_system_value(b
, mode
);
1413 case SpvBuiltInBaseInstance
:
1414 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1415 set_mode_system_value(b
, mode
);
1417 case SpvBuiltInDrawIndex
:
1418 *location
= SYSTEM_VALUE_DRAW_ID
;
1419 set_mode_system_value(b
, mode
);
1421 case SpvBuiltInSubgroupSize
:
1422 *location
= SYSTEM_VALUE_SUBGROUP_SIZE
;
1423 set_mode_system_value(b
, mode
);
1425 case SpvBuiltInSubgroupId
:
1426 *location
= SYSTEM_VALUE_SUBGROUP_ID
;
1427 set_mode_system_value(b
, mode
);
1429 case SpvBuiltInSubgroupLocalInvocationId
:
1430 *location
= SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1431 set_mode_system_value(b
, mode
);
1433 case SpvBuiltInNumSubgroups
:
1434 *location
= SYSTEM_VALUE_NUM_SUBGROUPS
;
1435 set_mode_system_value(b
, mode
);
1437 case SpvBuiltInDeviceIndex
:
1438 *location
= SYSTEM_VALUE_DEVICE_INDEX
;
1439 set_mode_system_value(b
, mode
);
1441 case SpvBuiltInViewIndex
:
1442 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1443 set_mode_system_value(b
, mode
);
1445 case SpvBuiltInSubgroupEqMask
:
1446 *location
= SYSTEM_VALUE_SUBGROUP_EQ_MASK
,
1447 set_mode_system_value(b
, mode
);
1449 case SpvBuiltInSubgroupGeMask
:
1450 *location
= SYSTEM_VALUE_SUBGROUP_GE_MASK
,
1451 set_mode_system_value(b
, mode
);
1453 case SpvBuiltInSubgroupGtMask
:
1454 *location
= SYSTEM_VALUE_SUBGROUP_GT_MASK
,
1455 set_mode_system_value(b
, mode
);
1457 case SpvBuiltInSubgroupLeMask
:
1458 *location
= SYSTEM_VALUE_SUBGROUP_LE_MASK
,
1459 set_mode_system_value(b
, mode
);
1461 case SpvBuiltInSubgroupLtMask
:
1462 *location
= SYSTEM_VALUE_SUBGROUP_LT_MASK
,
1463 set_mode_system_value(b
, mode
);
1465 case SpvBuiltInFragStencilRefEXT
:
1466 *location
= FRAG_RESULT_STENCIL
;
1467 vtn_assert(*mode
== nir_var_shader_out
);
1469 case SpvBuiltInWorkDim
:
1470 *location
= SYSTEM_VALUE_WORK_DIM
;
1471 set_mode_system_value(b
, mode
);
1473 case SpvBuiltInGlobalSize
:
1474 *location
= SYSTEM_VALUE_GLOBAL_GROUP_SIZE
;
1475 set_mode_system_value(b
, mode
);
1477 case SpvBuiltInBaryCoordNoPerspAMD
:
1478 *location
= SYSTEM_VALUE_BARYCENTRIC_LINEAR_PIXEL
;
1479 set_mode_system_value(b
, mode
);
1481 case SpvBuiltInBaryCoordNoPerspCentroidAMD
:
1482 *location
= SYSTEM_VALUE_BARYCENTRIC_LINEAR_CENTROID
;
1483 set_mode_system_value(b
, mode
);
1485 case SpvBuiltInBaryCoordNoPerspSampleAMD
:
1486 *location
= SYSTEM_VALUE_BARYCENTRIC_LINEAR_SAMPLE
;
1487 set_mode_system_value(b
, mode
);
1489 case SpvBuiltInBaryCoordSmoothAMD
:
1490 *location
= SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL
;
1491 set_mode_system_value(b
, mode
);
1493 case SpvBuiltInBaryCoordSmoothCentroidAMD
:
1494 *location
= SYSTEM_VALUE_BARYCENTRIC_PERSP_CENTROID
;
1495 set_mode_system_value(b
, mode
);
1497 case SpvBuiltInBaryCoordSmoothSampleAMD
:
1498 *location
= SYSTEM_VALUE_BARYCENTRIC_PERSP_SAMPLE
;
1499 set_mode_system_value(b
, mode
);
1501 case SpvBuiltInBaryCoordPullModelAMD
:
1502 *location
= SYSTEM_VALUE_BARYCENTRIC_PULL_MODEL
;
1503 set_mode_system_value(b
, mode
);
1506 vtn_fail("Unsupported builtin: %s (%u)",
1507 spirv_builtin_to_string(builtin
), builtin
);
1512 apply_var_decoration(struct vtn_builder
*b
,
1513 struct nir_variable_data
*var_data
,
1514 const struct vtn_decoration
*dec
)
1516 switch (dec
->decoration
) {
1517 case SpvDecorationRelaxedPrecision
:
1518 break; /* FIXME: Do nothing with this for now. */
1519 case SpvDecorationNoPerspective
:
1520 var_data
->interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1522 case SpvDecorationFlat
:
1523 var_data
->interpolation
= INTERP_MODE_FLAT
;
1525 case SpvDecorationExplicitInterpAMD
:
1526 var_data
->interpolation
= INTERP_MODE_EXPLICIT
;
1528 case SpvDecorationCentroid
:
1529 var_data
->centroid
= true;
1531 case SpvDecorationSample
:
1532 var_data
->sample
= true;
1534 case SpvDecorationInvariant
:
1535 var_data
->invariant
= true;
1537 case SpvDecorationConstant
:
1538 var_data
->read_only
= true;
1540 case SpvDecorationNonReadable
:
1541 var_data
->access
|= ACCESS_NON_READABLE
;
1543 case SpvDecorationNonWritable
:
1544 var_data
->read_only
= true;
1545 var_data
->access
|= ACCESS_NON_WRITEABLE
;
1547 case SpvDecorationRestrict
:
1548 var_data
->access
|= ACCESS_RESTRICT
;
1550 case SpvDecorationAliased
:
1551 var_data
->access
&= ~ACCESS_RESTRICT
;
1553 case SpvDecorationVolatile
:
1554 var_data
->access
|= ACCESS_VOLATILE
;
1556 case SpvDecorationCoherent
:
1557 var_data
->access
|= ACCESS_COHERENT
;
1559 case SpvDecorationComponent
:
1560 var_data
->location_frac
= dec
->operands
[0];
1562 case SpvDecorationIndex
:
1563 var_data
->index
= dec
->operands
[0];
1565 case SpvDecorationBuiltIn
: {
1566 SpvBuiltIn builtin
= dec
->operands
[0];
1568 nir_variable_mode mode
= var_data
->mode
;
1569 vtn_get_builtin_location(b
, builtin
, &var_data
->location
, &mode
);
1570 var_data
->mode
= mode
;
1573 case SpvBuiltInTessLevelOuter
:
1574 case SpvBuiltInTessLevelInner
:
1575 case SpvBuiltInClipDistance
:
1576 case SpvBuiltInCullDistance
:
1577 var_data
->compact
= true;
1584 case SpvDecorationSpecId
:
1585 case SpvDecorationRowMajor
:
1586 case SpvDecorationColMajor
:
1587 case SpvDecorationMatrixStride
:
1588 case SpvDecorationUniform
:
1589 case SpvDecorationUniformId
:
1590 case SpvDecorationLinkageAttributes
:
1591 break; /* Do nothing with these here */
1593 case SpvDecorationPatch
:
1594 var_data
->patch
= true;
1597 case SpvDecorationLocation
:
1598 vtn_fail("Handled above");
1600 case SpvDecorationBlock
:
1601 case SpvDecorationBufferBlock
:
1602 case SpvDecorationArrayStride
:
1603 case SpvDecorationGLSLShared
:
1604 case SpvDecorationGLSLPacked
:
1605 break; /* These can apply to a type but we don't care about them */
1607 case SpvDecorationBinding
:
1608 case SpvDecorationDescriptorSet
:
1609 case SpvDecorationNoContraction
:
1610 case SpvDecorationInputAttachmentIndex
:
1611 vtn_warn("Decoration not allowed for variable or structure member: %s",
1612 spirv_decoration_to_string(dec
->decoration
));
1615 case SpvDecorationXfbBuffer
:
1616 var_data
->explicit_xfb_buffer
= true;
1617 var_data
->xfb
.buffer
= dec
->operands
[0];
1618 var_data
->always_active_io
= true;
1620 case SpvDecorationXfbStride
:
1621 var_data
->explicit_xfb_stride
= true;
1622 var_data
->xfb
.stride
= dec
->operands
[0];
1624 case SpvDecorationOffset
:
1625 var_data
->explicit_offset
= true;
1626 var_data
->offset
= dec
->operands
[0];
1629 case SpvDecorationStream
:
1630 var_data
->stream
= dec
->operands
[0];
1633 case SpvDecorationCPacked
:
1634 case SpvDecorationSaturatedConversion
:
1635 case SpvDecorationFuncParamAttr
:
1636 case SpvDecorationFPRoundingMode
:
1637 case SpvDecorationFPFastMathMode
:
1638 case SpvDecorationAlignment
:
1639 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
1640 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1641 spirv_decoration_to_string(dec
->decoration
));
1645 case SpvDecorationUserSemantic
:
1646 case SpvDecorationUserTypeGOOGLE
:
1647 /* User semantic decorations can safely be ignored by the driver. */
1650 case SpvDecorationRestrictPointerEXT
:
1651 case SpvDecorationAliasedPointerEXT
:
1652 /* TODO: We should actually plumb alias information through NIR. */
1656 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1661 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1662 const struct vtn_decoration
*dec
, void *out_is_patch
)
1664 if (dec
->decoration
== SpvDecorationPatch
) {
1665 *((bool *) out_is_patch
) = true;
1670 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1671 const struct vtn_decoration
*dec
, void *void_var
)
1673 struct vtn_variable
*vtn_var
= void_var
;
1675 /* Handle decorations that apply to a vtn_variable as a whole */
1676 switch (dec
->decoration
) {
1677 case SpvDecorationBinding
:
1678 vtn_var
->binding
= dec
->operands
[0];
1679 vtn_var
->explicit_binding
= true;
1681 case SpvDecorationDescriptorSet
:
1682 vtn_var
->descriptor_set
= dec
->operands
[0];
1684 case SpvDecorationInputAttachmentIndex
:
1685 vtn_var
->input_attachment_index
= dec
->operands
[0];
1687 case SpvDecorationPatch
:
1688 vtn_var
->patch
= true;
1690 case SpvDecorationOffset
:
1691 vtn_var
->offset
= dec
->operands
[0];
1693 case SpvDecorationNonWritable
:
1694 vtn_var
->access
|= ACCESS_NON_WRITEABLE
;
1696 case SpvDecorationNonReadable
:
1697 vtn_var
->access
|= ACCESS_NON_READABLE
;
1699 case SpvDecorationVolatile
:
1700 vtn_var
->access
|= ACCESS_VOLATILE
;
1702 case SpvDecorationCoherent
:
1703 vtn_var
->access
|= ACCESS_COHERENT
;
1705 case SpvDecorationCounterBuffer
:
1706 /* Counter buffer decorations can safely be ignored by the driver. */
1712 if (val
->value_type
== vtn_value_type_pointer
) {
1713 assert(val
->pointer
->var
== void_var
);
1714 assert(member
== -1);
1716 assert(val
->value_type
== vtn_value_type_type
);
1719 /* Location is odd. If applied to a split structure, we have to walk the
1720 * whole thing and accumulate the location. It's easier to handle as a
1723 if (dec
->decoration
== SpvDecorationLocation
) {
1724 unsigned location
= dec
->operands
[0];
1725 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
&&
1726 vtn_var
->mode
== vtn_variable_mode_output
) {
1727 location
+= FRAG_RESULT_DATA0
;
1728 } else if (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
&&
1729 vtn_var
->mode
== vtn_variable_mode_input
) {
1730 location
+= VERT_ATTRIB_GENERIC0
;
1731 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1732 vtn_var
->mode
== vtn_variable_mode_output
) {
1733 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1734 } else if (vtn_var
->mode
!= vtn_variable_mode_uniform
) {
1735 vtn_warn("Location must be on input, output, uniform, sampler or "
1740 if (vtn_var
->var
->num_members
== 0) {
1741 /* This handles the member and lone variable cases */
1742 vtn_var
->var
->data
.location
= location
;
1744 /* This handles the structure member case */
1745 assert(vtn_var
->var
->members
);
1748 vtn_var
->base_location
= location
;
1750 vtn_var
->var
->members
[member
].location
= location
;
1756 if (vtn_var
->var
->num_members
== 0) {
1757 /* We call this function on types as well as variables and not all
1758 * struct types get split so we can end up having stray member
1759 * decorations; just ignore them.
1762 apply_var_decoration(b
, &vtn_var
->var
->data
, dec
);
1763 } else if (member
>= 0) {
1764 /* Member decorations must come from a type */
1765 assert(val
->value_type
== vtn_value_type_type
);
1766 apply_var_decoration(b
, &vtn_var
->var
->members
[member
], dec
);
1769 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1770 for (unsigned i
= 0; i
< length
; i
++)
1771 apply_var_decoration(b
, &vtn_var
->var
->members
[i
], dec
);
1774 /* A few variables, those with external storage, have no actual
1775 * nir_variables associated with them. Fortunately, all decorations
1776 * we care about for those variables are on the type only.
1778 vtn_assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1779 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1780 vtn_var
->mode
== vtn_variable_mode_push_constant
);
1785 enum vtn_variable_mode
1786 vtn_storage_class_to_mode(struct vtn_builder
*b
,
1787 SpvStorageClass
class,
1788 struct vtn_type
*interface_type
,
1789 nir_variable_mode
*nir_mode_out
)
1791 enum vtn_variable_mode mode
;
1792 nir_variable_mode nir_mode
;
1794 case SpvStorageClassUniform
:
1795 /* Assume it's an UBO if we lack the interface_type. */
1796 if (!interface_type
|| interface_type
->block
) {
1797 mode
= vtn_variable_mode_ubo
;
1798 nir_mode
= nir_var_mem_ubo
;
1799 } else if (interface_type
->buffer_block
) {
1800 mode
= vtn_variable_mode_ssbo
;
1801 nir_mode
= nir_var_mem_ssbo
;
1803 /* Default-block uniforms, coming from gl_spirv */
1804 mode
= vtn_variable_mode_uniform
;
1805 nir_mode
= nir_var_uniform
;
1808 case SpvStorageClassStorageBuffer
:
1809 mode
= vtn_variable_mode_ssbo
;
1810 nir_mode
= nir_var_mem_ssbo
;
1812 case SpvStorageClassPhysicalStorageBuffer
:
1813 mode
= vtn_variable_mode_phys_ssbo
;
1814 nir_mode
= nir_var_mem_global
;
1816 case SpvStorageClassUniformConstant
:
1817 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1818 if (b
->options
->constant_as_global
) {
1819 mode
= vtn_variable_mode_cross_workgroup
;
1820 nir_mode
= nir_var_mem_global
;
1822 mode
= vtn_variable_mode_ubo
;
1823 nir_mode
= nir_var_mem_ubo
;
1826 mode
= vtn_variable_mode_uniform
;
1827 nir_mode
= nir_var_uniform
;
1830 case SpvStorageClassPushConstant
:
1831 mode
= vtn_variable_mode_push_constant
;
1832 nir_mode
= nir_var_uniform
;
1834 case SpvStorageClassInput
:
1835 mode
= vtn_variable_mode_input
;
1836 nir_mode
= nir_var_shader_in
;
1838 case SpvStorageClassOutput
:
1839 mode
= vtn_variable_mode_output
;
1840 nir_mode
= nir_var_shader_out
;
1842 case SpvStorageClassPrivate
:
1843 mode
= vtn_variable_mode_private
;
1844 nir_mode
= nir_var_shader_temp
;
1846 case SpvStorageClassFunction
:
1847 mode
= vtn_variable_mode_function
;
1848 nir_mode
= nir_var_function_temp
;
1850 case SpvStorageClassWorkgroup
:
1851 mode
= vtn_variable_mode_workgroup
;
1852 nir_mode
= nir_var_mem_shared
;
1854 case SpvStorageClassAtomicCounter
:
1855 mode
= vtn_variable_mode_uniform
;
1856 nir_mode
= nir_var_uniform
;
1858 case SpvStorageClassCrossWorkgroup
:
1859 mode
= vtn_variable_mode_cross_workgroup
;
1860 nir_mode
= nir_var_mem_global
;
1862 case SpvStorageClassImage
:
1863 mode
= vtn_variable_mode_image
;
1864 nir_mode
= nir_var_mem_ubo
;
1866 case SpvStorageClassGeneric
:
1868 vtn_fail("Unhandled variable storage class: %s (%u)",
1869 spirv_storageclass_to_string(class), class);
1873 *nir_mode_out
= nir_mode
;
1879 vtn_mode_to_address_format(struct vtn_builder
*b
, enum vtn_variable_mode mode
)
1882 case vtn_variable_mode_ubo
:
1883 return b
->options
->ubo_addr_format
;
1885 case vtn_variable_mode_ssbo
:
1886 return b
->options
->ssbo_addr_format
;
1888 case vtn_variable_mode_phys_ssbo
:
1889 return b
->options
->phys_ssbo_addr_format
;
1891 case vtn_variable_mode_push_constant
:
1892 return b
->options
->push_const_addr_format
;
1894 case vtn_variable_mode_workgroup
:
1895 return b
->options
->shared_addr_format
;
1897 case vtn_variable_mode_cross_workgroup
:
1898 return b
->options
->global_addr_format
;
1900 case vtn_variable_mode_function
:
1901 if (b
->physical_ptrs
)
1902 return b
->options
->temp_addr_format
;
1905 case vtn_variable_mode_private
:
1906 case vtn_variable_mode_uniform
:
1907 case vtn_variable_mode_input
:
1908 case vtn_variable_mode_output
:
1909 case vtn_variable_mode_image
:
1910 return nir_address_format_logical
;
1913 unreachable("Invalid variable mode");
1917 vtn_pointer_to_ssa(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
1919 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
1920 /* This pointer needs to have a pointer type with actual storage */
1921 vtn_assert(ptr
->ptr_type
);
1922 vtn_assert(ptr
->ptr_type
->type
);
1925 /* If we don't have an offset then we must be a pointer to the variable
1928 vtn_assert(!ptr
->offset
&& !ptr
->block_index
);
1930 struct vtn_access_chain chain
= {
1933 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
1936 vtn_assert(ptr
->offset
);
1937 if (ptr
->block_index
) {
1938 vtn_assert(ptr
->mode
== vtn_variable_mode_ubo
||
1939 ptr
->mode
== vtn_variable_mode_ssbo
);
1940 return nir_vec2(&b
->nb
, ptr
->block_index
, ptr
->offset
);
1942 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
);
1946 if (vtn_pointer_is_external_block(b
, ptr
) &&
1947 vtn_type_contains_block(b
, ptr
->type
) &&
1948 ptr
->mode
!= vtn_variable_mode_phys_ssbo
) {
1949 /* In this case, we're looking for a block index and not an actual
1952 * For PhysicalStorageBuffer pointers, we don't have a block index
1953 * at all because we get the pointer directly from the client. This
1954 * assumes that there will never be a SSBO binding variable using the
1955 * PhysicalStorageBuffer storage class. This assumption appears
1956 * to be correct according to the Vulkan spec because the table,
1957 * "Shader Resource and Storage Class Correspondence," the only the
1958 * Uniform storage class with BufferBlock or the StorageBuffer
1959 * storage class with Block can be used.
1961 if (!ptr
->block_index
) {
1962 /* If we don't have a block_index then we must be a pointer to the
1965 vtn_assert(!ptr
->deref
);
1967 struct vtn_access_chain chain
= {
1970 ptr
= vtn_nir_deref_pointer_dereference(b
, ptr
, &chain
);
1973 return ptr
->block_index
;
1975 return &vtn_pointer_to_deref(b
, ptr
)->dest
.ssa
;
1980 struct vtn_pointer
*
1981 vtn_pointer_from_ssa(struct vtn_builder
*b
, nir_ssa_def
*ssa
,
1982 struct vtn_type
*ptr_type
)
1984 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1986 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
1987 struct vtn_type
*without_array
=
1988 vtn_type_without_array(ptr_type
->deref
);
1990 nir_variable_mode nir_mode
;
1991 ptr
->mode
= vtn_storage_class_to_mode(b
, ptr_type
->storage_class
,
1992 without_array
, &nir_mode
);
1993 ptr
->type
= ptr_type
->deref
;
1994 ptr
->ptr_type
= ptr_type
;
1996 if (b
->wa_glslang_179
) {
1997 /* To work around https://github.com/KhronosGroup/glslang/issues/179 we
1998 * need to whack the mode because it creates a function parameter with
1999 * the Function storage class even though it's a pointer to a sampler.
2000 * If we don't do this, then NIR won't get rid of the deref_cast for us.
2002 if (ptr
->mode
== vtn_variable_mode_function
&&
2003 (ptr
->type
->base_type
== vtn_base_type_sampler
||
2004 ptr
->type
->base_type
== vtn_base_type_sampled_image
)) {
2005 ptr
->mode
= vtn_variable_mode_uniform
;
2006 nir_mode
= nir_var_uniform
;
2010 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
2011 /* This pointer type needs to have actual storage */
2012 vtn_assert(ptr_type
->type
);
2013 if (ptr
->mode
== vtn_variable_mode_ubo
||
2014 ptr
->mode
== vtn_variable_mode_ssbo
) {
2015 vtn_assert(ssa
->num_components
== 2);
2016 ptr
->block_index
= nir_channel(&b
->nb
, ssa
, 0);
2017 ptr
->offset
= nir_channel(&b
->nb
, ssa
, 1);
2019 vtn_assert(ssa
->num_components
== 1);
2020 ptr
->block_index
= NULL
;
2024 const struct glsl_type
*deref_type
= ptr_type
->deref
->type
;
2025 if (!vtn_pointer_is_external_block(b
, ptr
)) {
2026 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
2027 deref_type
, ptr_type
->stride
);
2028 } else if (vtn_type_contains_block(b
, ptr
->type
) &&
2029 ptr
->mode
!= vtn_variable_mode_phys_ssbo
) {
2030 /* This is a pointer to somewhere in an array of blocks, not a
2031 * pointer to somewhere inside the block. Set the block index
2032 * instead of making a cast.
2034 ptr
->block_index
= ssa
;
2036 /* This is a pointer to something internal or a pointer inside a
2037 * block. It's just a regular cast.
2039 * For PhysicalStorageBuffer pointers, we don't have a block index
2040 * at all because we get the pointer directly from the client. This
2041 * assumes that there will never be a SSBO binding variable using the
2042 * PhysicalStorageBuffer storage class. This assumption appears
2043 * to be correct according to the Vulkan spec because the table,
2044 * "Shader Resource and Storage Class Correspondence," the only the
2045 * Uniform storage class with BufferBlock or the StorageBuffer
2046 * storage class with Block can be used.
2048 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
2049 ptr_type
->deref
->type
,
2051 ptr
->deref
->dest
.ssa
.num_components
=
2052 glsl_get_vector_elements(ptr_type
->type
);
2053 ptr
->deref
->dest
.ssa
.bit_size
= glsl_get_bit_size(ptr_type
->type
);
2061 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
2063 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
2066 if (var
->mode
== vtn_variable_mode_input
) {
2067 return stage
== MESA_SHADER_TESS_CTRL
||
2068 stage
== MESA_SHADER_TESS_EVAL
||
2069 stage
== MESA_SHADER_GEOMETRY
;
2072 if (var
->mode
== vtn_variable_mode_output
)
2073 return stage
== MESA_SHADER_TESS_CTRL
;
2079 assign_missing_member_locations(struct vtn_variable
*var
)
2082 glsl_get_length(glsl_without_array(var
->type
->type
));
2083 int location
= var
->base_location
;
2085 for (unsigned i
= 0; i
< length
; i
++) {
2086 /* From the Vulkan spec:
2088 * “If the structure type is a Block but without a Location, then each
2089 * of its members must have a Location decoration.”
2092 if (var
->type
->block
) {
2093 assert(var
->base_location
!= -1 ||
2094 var
->var
->members
[i
].location
!= -1);
2097 /* From the Vulkan spec:
2099 * “Any member with its own Location decoration is assigned that
2100 * location. Each remaining member is assigned the location after the
2101 * immediately preceding member in declaration order.”
2103 if (var
->var
->members
[i
].location
!= -1)
2104 location
= var
->var
->members
[i
].location
;
2106 var
->var
->members
[i
].location
= location
;
2108 /* Below we use type instead of interface_type, because interface_type
2109 * is only available when it is a Block. This code also supports
2110 * input/outputs that are just structs
2112 const struct glsl_type
*member_type
=
2113 glsl_get_struct_field(glsl_without_array(var
->type
->type
), i
);
2116 glsl_count_attribute_slots(member_type
,
2117 false /* is_gl_vertex_input */);
2123 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
2124 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
2125 nir_constant
*const_initializer
, nir_variable
*var_initializer
)
2127 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
2128 struct vtn_type
*type
= ptr_type
->deref
;
2130 struct vtn_type
*without_array
= vtn_type_without_array(ptr_type
->deref
);
2132 enum vtn_variable_mode mode
;
2133 nir_variable_mode nir_mode
;
2134 mode
= vtn_storage_class_to_mode(b
, storage_class
, without_array
, &nir_mode
);
2137 case vtn_variable_mode_ubo
:
2138 /* There's no other way to get vtn_variable_mode_ubo */
2139 vtn_assert(without_array
->block
);
2140 b
->shader
->info
.num_ubos
++;
2142 case vtn_variable_mode_ssbo
:
2143 if (storage_class
== SpvStorageClassStorageBuffer
&&
2144 !without_array
->block
) {
2145 if (b
->variable_pointers
) {
2146 vtn_fail("Variables in the StorageBuffer storage class must "
2147 "have a struct type with the Block decoration");
2149 /* If variable pointers are not present, it's still malformed
2150 * SPIR-V but we can parse it and do the right thing anyway.
2151 * Since some of the 8-bit storage tests have bugs in this are,
2152 * just make it a warning for now.
2154 vtn_warn("Variables in the StorageBuffer storage class must "
2155 "have a struct type with the Block decoration");
2158 b
->shader
->info
.num_ssbos
++;
2160 case vtn_variable_mode_uniform
:
2161 if (glsl_type_is_image(without_array
->type
))
2162 b
->shader
->info
.num_images
++;
2163 else if (glsl_type_is_sampler(without_array
->type
))
2164 b
->shader
->info
.num_textures
++;
2166 case vtn_variable_mode_push_constant
:
2167 b
->shader
->num_uniforms
= vtn_type_block_size(b
, type
);
2170 case vtn_variable_mode_image
:
2171 vtn_fail("Cannot create a variable with the Image storage class");
2174 case vtn_variable_mode_phys_ssbo
:
2175 vtn_fail("Cannot create a variable with the "
2176 "PhysicalStorageBuffer storage class");
2180 /* No tallying is needed */
2184 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
2187 var
->base_location
= -1;
2189 val
->pointer
= rzalloc(b
, struct vtn_pointer
);
2190 val
->pointer
->mode
= var
->mode
;
2191 val
->pointer
->type
= var
->type
;
2192 val
->pointer
->ptr_type
= ptr_type
;
2193 val
->pointer
->var
= var
;
2194 val
->pointer
->access
= var
->type
->access
;
2196 switch (var
->mode
) {
2197 case vtn_variable_mode_function
:
2198 case vtn_variable_mode_private
:
2199 case vtn_variable_mode_uniform
:
2200 /* For these, we create the variable normally */
2201 var
->var
= rzalloc(b
->shader
, nir_variable
);
2202 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2204 if (storage_class
== SpvStorageClassAtomicCounter
) {
2205 /* Need to tweak the nir type here as at vtn_handle_type we don't
2206 * have the access to storage_class, that is the one that points us
2207 * that is an atomic uint.
2209 var
->var
->type
= repair_atomic_type(var
->type
->type
);
2211 /* Private variables don't have any explicit layout but some layouts
2212 * may have leaked through due to type deduplication in the SPIR-V.
2214 var
->var
->type
= var
->type
->type
;
2216 var
->var
->data
.mode
= nir_mode
;
2217 var
->var
->data
.location
= -1;
2218 var
->var
->interface_type
= NULL
;
2221 case vtn_variable_mode_ubo
:
2222 case vtn_variable_mode_ssbo
:
2223 var
->var
= rzalloc(b
->shader
, nir_variable
);
2224 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2226 var
->var
->type
= var
->type
->type
;
2227 var
->var
->interface_type
= var
->type
->type
;
2229 var
->var
->data
.mode
= nir_mode
;
2230 var
->var
->data
.location
= -1;
2234 case vtn_variable_mode_workgroup
:
2235 /* Create the variable normally */
2236 var
->var
= rzalloc(b
->shader
, nir_variable
);
2237 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2238 /* Workgroup variables don't have any explicit layout but some
2239 * layouts may have leaked through due to type deduplication in the
2242 var
->var
->type
= var
->type
->type
;
2243 var
->var
->data
.mode
= nir_var_mem_shared
;
2246 case vtn_variable_mode_input
:
2247 case vtn_variable_mode_output
: {
2248 /* In order to know whether or not we're a per-vertex inout, we need
2249 * the patch qualifier. This means walking the variable decorations
2250 * early before we actually create any variables. Not a big deal.
2252 * GLSLang really likes to place decorations in the most interior
2253 * thing it possibly can. In particular, if you have a struct, it
2254 * will place the patch decorations on the struct members. This
2255 * should be handled by the variable splitting below just fine.
2257 * If you have an array-of-struct, things get even more weird as it
2258 * will place the patch decorations on the struct even though it's
2259 * inside an array and some of the members being patch and others not
2260 * makes no sense whatsoever. Since the only sensible thing is for
2261 * it to be all or nothing, we'll call it patch if any of the members
2262 * are declared patch.
2265 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
2266 if (glsl_type_is_array(var
->type
->type
) &&
2267 glsl_type_is_struct_or_ifc(without_array
->type
)) {
2268 vtn_foreach_decoration(b
, vtn_value(b
, without_array
->id
,
2269 vtn_value_type_type
),
2270 var_is_patch_cb
, &var
->patch
);
2273 /* For inputs and outputs, we immediately split structures. This
2274 * is for a couple of reasons. For one, builtins may all come in
2275 * a struct and we really want those split out into separate
2276 * variables. For another, interpolation qualifiers can be
2277 * applied to members of the top-level struct ane we need to be
2278 * able to preserve that information.
2281 struct vtn_type
*per_vertex_type
= var
->type
;
2282 if (is_per_vertex_inout(var
, b
->shader
->info
.stage
)) {
2283 /* In Geometry shaders (and some tessellation), inputs come
2284 * in per-vertex arrays. However, some builtins come in
2285 * non-per-vertex, hence the need for the is_array check. In
2286 * any case, there are no non-builtin arrays allowed so this
2287 * check should be sufficient.
2289 per_vertex_type
= var
->type
->array_element
;
2292 var
->var
= rzalloc(b
->shader
, nir_variable
);
2293 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2294 /* In Vulkan, shader I/O variables don't have any explicit layout but
2295 * some layouts may have leaked through due to type deduplication in
2296 * the SPIR-V. We do, however, keep the layouts in the variable's
2297 * interface_type because we need offsets for XFB arrays of blocks.
2299 var
->var
->type
= var
->type
->type
;
2300 var
->var
->data
.mode
= nir_mode
;
2301 var
->var
->data
.patch
= var
->patch
;
2303 /* Figure out the interface block type. */
2304 struct vtn_type
*iface_type
= per_vertex_type
;
2305 if (var
->mode
== vtn_variable_mode_output
&&
2306 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
2307 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
||
2308 b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)) {
2309 /* For vertex data outputs, we can end up with arrays of blocks for
2310 * transform feedback where each array element corresponds to a
2311 * different XFB output buffer.
2313 while (iface_type
->base_type
== vtn_base_type_array
)
2314 iface_type
= iface_type
->array_element
;
2316 if (iface_type
->base_type
== vtn_base_type_struct
&& iface_type
->block
)
2317 var
->var
->interface_type
= iface_type
->type
;
2319 if (per_vertex_type
->base_type
== vtn_base_type_struct
&&
2320 per_vertex_type
->block
) {
2321 /* It's a struct. Set it up as per-member. */
2322 var
->var
->num_members
= glsl_get_length(per_vertex_type
->type
);
2323 var
->var
->members
= rzalloc_array(var
->var
, struct nir_variable_data
,
2324 var
->var
->num_members
);
2326 for (unsigned i
= 0; i
< var
->var
->num_members
; i
++) {
2327 var
->var
->members
[i
].mode
= nir_mode
;
2328 var
->var
->members
[i
].patch
= var
->patch
;
2329 var
->var
->members
[i
].location
= -1;
2333 /* For inputs and outputs, we need to grab locations and builtin
2334 * information from the per-vertex type.
2336 vtn_foreach_decoration(b
, vtn_value(b
, per_vertex_type
->id
,
2337 vtn_value_type_type
),
2338 var_decoration_cb
, var
);
2342 case vtn_variable_mode_push_constant
:
2343 case vtn_variable_mode_cross_workgroup
:
2344 /* These don't need actual variables. */
2347 case vtn_variable_mode_image
:
2348 case vtn_variable_mode_phys_ssbo
:
2349 unreachable("Should have been caught before");
2352 /* We can only have one type of initializer */
2353 assert(!(const_initializer
&& var_initializer
));
2354 if (const_initializer
) {
2355 var
->var
->constant_initializer
=
2356 nir_constant_clone(const_initializer
, var
->var
);
2358 if (var_initializer
)
2359 var
->var
->pointer_initializer
= var_initializer
;
2361 if (var
->mode
== vtn_variable_mode_uniform
||
2362 var
->mode
== vtn_variable_mode_ssbo
) {
2363 /* SSBOs and images are assumed to not alias in the Simple, GLSL and Vulkan memory models */
2364 var
->var
->data
.access
|= b
->mem_model
!= SpvMemoryModelOpenCL
? ACCESS_RESTRICT
: 0;
2367 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
2368 vtn_foreach_decoration(b
, val
, ptr_decoration_cb
, val
->pointer
);
2370 /* Propagate access flags from the OpVariable decorations. */
2371 val
->pointer
->access
|= var
->access
;
2373 if ((var
->mode
== vtn_variable_mode_input
||
2374 var
->mode
== vtn_variable_mode_output
) &&
2375 var
->var
->members
) {
2376 assign_missing_member_locations(var
);
2379 if (var
->mode
== vtn_variable_mode_uniform
||
2380 var
->mode
== vtn_variable_mode_ubo
||
2381 var
->mode
== vtn_variable_mode_ssbo
) {
2382 /* XXX: We still need the binding information in the nir_variable
2383 * for these. We should fix that.
2385 var
->var
->data
.binding
= var
->binding
;
2386 var
->var
->data
.explicit_binding
= var
->explicit_binding
;
2387 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
2388 var
->var
->data
.index
= var
->input_attachment_index
;
2389 var
->var
->data
.offset
= var
->offset
;
2391 if (glsl_type_is_image(without_array
->type
))
2392 var
->var
->data
.image
.format
= without_array
->image_format
;
2395 if (var
->mode
== vtn_variable_mode_function
) {
2396 vtn_assert(var
->var
!= NULL
&& var
->var
->members
== NULL
);
2397 nir_function_impl_add_variable(b
->nb
.impl
, var
->var
);
2398 } else if (var
->var
) {
2399 nir_shader_add_variable(b
->shader
, var
->var
);
2401 vtn_assert(vtn_pointer_is_external_block(b
, val
->pointer
));
2406 vtn_assert_types_equal(struct vtn_builder
*b
, SpvOp opcode
,
2407 struct vtn_type
*dst_type
,
2408 struct vtn_type
*src_type
)
2410 if (dst_type
->id
== src_type
->id
)
2413 if (vtn_types_compatible(b
, dst_type
, src_type
)) {
2414 /* Early versions of GLSLang would re-emit types unnecessarily and you
2415 * would end up with OpLoad, OpStore, or OpCopyMemory opcodes which have
2416 * mismatched source and destination types.
2418 * https://github.com/KhronosGroup/glslang/issues/304
2419 * https://github.com/KhronosGroup/glslang/issues/307
2420 * https://bugs.freedesktop.org/show_bug.cgi?id=104338
2421 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2423 vtn_warn("Source and destination types of %s do not have the same "
2424 "ID (but are compatible): %u vs %u",
2425 spirv_op_to_string(opcode
), dst_type
->id
, src_type
->id
);
2429 vtn_fail("Source and destination types of %s do not match: %s vs. %s",
2430 spirv_op_to_string(opcode
),
2431 glsl_get_type_name(dst_type
->type
),
2432 glsl_get_type_name(src_type
->type
));
2435 static nir_ssa_def
*
2436 nir_shrink_zero_pad_vec(nir_builder
*b
, nir_ssa_def
*val
,
2437 unsigned num_components
)
2439 if (val
->num_components
== num_components
)
2442 nir_ssa_def
*comps
[NIR_MAX_VEC_COMPONENTS
];
2443 for (unsigned i
= 0; i
< num_components
; i
++) {
2444 if (i
< val
->num_components
)
2445 comps
[i
] = nir_channel(b
, val
, i
);
2447 comps
[i
] = nir_imm_intN_t(b
, 0, val
->bit_size
);
2449 return nir_vec(b
, comps
, num_components
);
2452 static nir_ssa_def
*
2453 nir_sloppy_bitcast(nir_builder
*b
, nir_ssa_def
*val
,
2454 const struct glsl_type
*type
)
2456 const unsigned num_components
= glsl_get_vector_elements(type
);
2457 const unsigned bit_size
= glsl_get_bit_size(type
);
2459 /* First, zero-pad to ensure that the value is big enough that when we
2460 * bit-cast it, we don't loose anything.
2462 if (val
->bit_size
< bit_size
) {
2463 const unsigned src_num_components_needed
=
2464 vtn_align_u32(val
->num_components
, bit_size
/ val
->bit_size
);
2465 val
= nir_shrink_zero_pad_vec(b
, val
, src_num_components_needed
);
2468 val
= nir_bitcast_vector(b
, val
, bit_size
);
2470 return nir_shrink_zero_pad_vec(b
, val
, num_components
);
2474 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
2475 const uint32_t *w
, unsigned count
)
2479 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
2480 val
->type
= vtn_get_type(b
, w
[1]);
2484 case SpvOpVariable
: {
2485 struct vtn_type
*ptr_type
= vtn_get_type(b
, w
[1]);
2487 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2489 SpvStorageClass storage_class
= w
[3];
2490 nir_constant
*const_initializer
= NULL
;
2491 nir_variable
*var_initializer
= NULL
;
2493 struct vtn_value
*init
= vtn_untyped_value(b
, w
[4]);
2494 switch (init
->value_type
) {
2495 case vtn_value_type_constant
:
2496 const_initializer
= init
->constant
;
2498 case vtn_value_type_pointer
:
2499 var_initializer
= init
->pointer
->var
->var
;
2502 vtn_fail("SPIR-V variable initializer %u must be constant or pointer",
2507 vtn_create_variable(b
, val
, ptr_type
, storage_class
, const_initializer
, var_initializer
);
2512 case SpvOpAccessChain
:
2513 case SpvOpPtrAccessChain
:
2514 case SpvOpInBoundsAccessChain
:
2515 case SpvOpInBoundsPtrAccessChain
: {
2516 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
2517 enum gl_access_qualifier access
= 0;
2518 chain
->ptr_as_array
= (opcode
== SpvOpPtrAccessChain
|| opcode
== SpvOpInBoundsPtrAccessChain
);
2521 for (int i
= 4; i
< count
; i
++) {
2522 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
2523 if (link_val
->value_type
== vtn_value_type_constant
) {
2524 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
2525 chain
->link
[idx
].id
= vtn_constant_int(b
, w
[i
]);
2527 chain
->link
[idx
].mode
= vtn_access_mode_id
;
2528 chain
->link
[idx
].id
= w
[i
];
2533 struct vtn_type
*ptr_type
= vtn_get_type(b
, w
[1]);
2534 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
2535 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
2536 /* This is rather insane. SPIR-V allows you to use OpSampledImage
2537 * to combine an array of images with a single sampler to get an
2538 * array of sampled images that all share the same sampler.
2539 * Fortunately, this means that we can more-or-less ignore the
2540 * sampler when crawling the access chain, but it does leave us
2541 * with this rather awkward little special-case.
2543 struct vtn_value
*val
=
2544 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2545 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2546 val
->sampled_image
->image
=
2547 vtn_pointer_dereference(b
, base_val
->sampled_image
->image
, chain
);
2548 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
2549 val
->sampled_image
->image
=
2550 vtn_decorate_pointer(b
, val
, val
->sampled_image
->image
);
2551 val
->sampled_image
->sampler
=
2552 vtn_decorate_pointer(b
, val
, val
->sampled_image
->sampler
);
2554 vtn_assert(base_val
->value_type
== vtn_value_type_pointer
);
2555 struct vtn_pointer
*ptr
=
2556 vtn_pointer_dereference(b
, base_val
->pointer
, chain
);
2557 ptr
->ptr_type
= ptr_type
;
2558 ptr
->access
|= access
;
2559 vtn_push_pointer(b
, w
[2], ptr
);
2564 case SpvOpCopyMemory
: {
2565 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2566 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
2568 vtn_assert_types_equal(b
, opcode
, dest
->type
->deref
, src
->type
->deref
);
2570 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
2575 struct vtn_type
*res_type
= vtn_get_type(b
, w
[1]);
2576 struct vtn_value
*src_val
= vtn_value(b
, w
[3], vtn_value_type_pointer
);
2577 struct vtn_pointer
*src
= src_val
->pointer
;
2579 vtn_assert_types_equal(b
, opcode
, res_type
, src_val
->type
->deref
);
2581 if (res_type
->base_type
== vtn_base_type_image
||
2582 res_type
->base_type
== vtn_base_type_sampler
) {
2583 vtn_push_pointer(b
, w
[2], src
);
2585 } else if (res_type
->base_type
== vtn_base_type_sampled_image
) {
2586 struct vtn_value
*val
=
2587 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2588 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2589 val
->sampled_image
->image
= val
->sampled_image
->sampler
=
2590 vtn_decorate_pointer(b
, val
, src
);
2596 SpvMemoryAccessMask access
= w
[4];
2597 if (access
& SpvMemoryAccessAlignedMask
)
2600 if (access
& SpvMemoryAccessMakePointerVisibleMask
) {
2601 SpvMemorySemanticsMask semantics
=
2602 SpvMemorySemanticsMakeVisibleMask
|
2603 vtn_storage_class_to_memory_semantics(src
->ptr_type
->storage_class
);
2605 SpvScope scope
= vtn_constant_uint(b
, w
[idx
]);
2606 vtn_emit_memory_barrier(b
, scope
, semantics
);
2610 vtn_push_ssa_value(b
, w
[2], vtn_variable_load(b
, src
));
2615 struct vtn_value
*dest_val
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2616 struct vtn_pointer
*dest
= dest_val
->pointer
;
2617 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[2]);
2619 /* OpStore requires us to actually have a storage type */
2620 vtn_fail_if(dest
->type
->type
== NULL
,
2621 "Invalid destination type for OpStore");
2623 if (glsl_get_base_type(dest
->type
->type
) == GLSL_TYPE_BOOL
&&
2624 glsl_get_base_type(src_val
->type
->type
) == GLSL_TYPE_UINT
) {
2625 /* Early versions of GLSLang would use uint types for UBOs/SSBOs but
2626 * would then store them to a local variable as bool. Work around
2627 * the issue by doing an implicit conversion.
2629 * https://github.com/KhronosGroup/glslang/issues/170
2630 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2632 vtn_warn("OpStore of value of type OpTypeInt to a pointer to type "
2633 "OpTypeBool. Doing an implicit conversion to work around "
2635 struct vtn_ssa_value
*bool_ssa
=
2636 vtn_create_ssa_value(b
, dest
->type
->type
);
2637 bool_ssa
->def
= nir_i2b(&b
->nb
, vtn_ssa_value(b
, w
[2])->def
);
2638 vtn_variable_store(b
, bool_ssa
, dest
);
2642 vtn_assert_types_equal(b
, opcode
, dest_val
->type
->deref
, src_val
->type
);
2644 if (glsl_type_is_sampler(dest
->type
->type
)) {
2645 if (b
->wa_glslang_179
) {
2646 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
2647 "propagation to workaround the problem.");
2648 vtn_assert(dest
->var
->copy_prop_sampler
== NULL
);
2649 struct vtn_value
*v
= vtn_untyped_value(b
, w
[2]);
2650 if (v
->value_type
== vtn_value_type_sampled_image
) {
2651 dest
->var
->copy_prop_sampler
= v
->sampled_image
->sampler
;
2653 vtn_assert(v
->value_type
== vtn_value_type_pointer
);
2654 dest
->var
->copy_prop_sampler
= v
->pointer
;
2657 vtn_fail("Vulkan does not allow OpStore of a sampler or image.");
2662 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
2663 vtn_variable_store(b
, src
, dest
);
2667 SpvMemoryAccessMask access
= w
[3];
2669 if (access
& SpvMemoryAccessAlignedMask
)
2672 if (access
& SpvMemoryAccessMakePointerAvailableMask
) {
2673 SpvMemorySemanticsMask semantics
=
2674 SpvMemorySemanticsMakeAvailableMask
|
2675 vtn_storage_class_to_memory_semantics(dest
->ptr_type
->storage_class
);
2676 SpvScope scope
= vtn_constant_uint(b
, w
[idx
]);
2677 vtn_emit_memory_barrier(b
, scope
, semantics
);
2683 case SpvOpArrayLength
: {
2684 struct vtn_pointer
*ptr
=
2685 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2686 const uint32_t field
= w
[4];
2688 vtn_fail_if(ptr
->type
->base_type
!= vtn_base_type_struct
,
2689 "OpArrayLength must take a pointer to a structure type");
2690 vtn_fail_if(field
!= ptr
->type
->length
- 1 ||
2691 ptr
->type
->members
[field
]->base_type
!= vtn_base_type_array
,
2692 "OpArrayLength must reference the last memeber of the "
2693 "structure and that must be an array");
2695 const uint32_t offset
= ptr
->type
->offsets
[field
];
2696 const uint32_t stride
= ptr
->type
->members
[field
]->stride
;
2698 if (!ptr
->block_index
) {
2699 struct vtn_access_chain chain
= {
2702 ptr
= vtn_pointer_dereference(b
, ptr
, &chain
);
2703 vtn_assert(ptr
->block_index
);
2706 nir_intrinsic_instr
*instr
=
2707 nir_intrinsic_instr_create(b
->nb
.shader
,
2708 nir_intrinsic_get_buffer_size
);
2709 instr
->src
[0] = nir_src_for_ssa(ptr
->block_index
);
2710 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
2711 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2712 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
2714 /* array_length = max(buffer_size - offset, 0) / stride */
2715 nir_ssa_def
*array_length
=
2720 nir_imm_int(&b
->nb
, offset
)),
2721 nir_imm_int(&b
->nb
, 0u)),
2722 nir_imm_int(&b
->nb
, stride
));
2724 vtn_push_nir_ssa(b
, w
[2], array_length
);
2728 case SpvOpConvertPtrToU
: {
2729 struct vtn_type
*u_type
= vtn_get_type(b
, w
[1]);
2730 struct vtn_type
*ptr_type
= vtn_get_value_type(b
, w
[3]);
2732 vtn_fail_if(ptr_type
->base_type
!= vtn_base_type_pointer
||
2733 ptr_type
->type
== NULL
,
2734 "OpConvertPtrToU can only be used on physical pointers");
2736 vtn_fail_if(u_type
->base_type
!= vtn_base_type_vector
&&
2737 u_type
->base_type
!= vtn_base_type_scalar
,
2738 "OpConvertPtrToU can only be used to cast to a vector or "
2741 /* The pointer will be converted to an SSA value automatically */
2742 nir_ssa_def
*ptr
= vtn_get_nir_ssa(b
, w
[3]);
2743 nir_ssa_def
*u
= nir_sloppy_bitcast(&b
->nb
, ptr
, u_type
->type
);
2744 vtn_push_nir_ssa(b
, w
[2], u
);
2748 case SpvOpConvertUToPtr
: {
2749 struct vtn_type
*ptr_type
= vtn_get_type(b
, w
[1]);
2750 struct vtn_type
*u_type
= vtn_get_value_type(b
, w
[3]);
2752 vtn_fail_if(ptr_type
->base_type
!= vtn_base_type_pointer
||
2753 ptr_type
->type
== NULL
,
2754 "OpConvertUToPtr can only be used on physical pointers");
2756 vtn_fail_if(u_type
->base_type
!= vtn_base_type_vector
&&
2757 u_type
->base_type
!= vtn_base_type_scalar
,
2758 "OpConvertUToPtr can only be used to cast from a vector or "
2761 nir_ssa_def
*u
= vtn_get_nir_ssa(b
, w
[3]);
2762 nir_ssa_def
*ptr
= nir_sloppy_bitcast(&b
->nb
, u
, ptr_type
->type
);
2763 vtn_push_pointer(b
, w
[2], vtn_pointer_from_ssa(b
, ptr
, ptr_type
));
2767 case SpvOpCopyMemorySized
:
2769 vtn_fail_with_opcode("Unhandled opcode", opcode
);