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_untyped_value(b
, dst_value_id
);
85 struct vtn_value src_copy
= *src
;
87 vtn_fail_if(dst
->value_type
!= vtn_value_type_invalid
,
88 "SPIR-V id %u has already been written by another instruction",
91 vtn_fail_if(dst
->type
->id
!= src
->type
->id
,
92 "Result Type must equal Operand type");
94 src_copy
.name
= dst
->name
;
95 src_copy
.decoration
= dst
->decoration
;
96 src_copy
.type
= dst
->type
;
99 if (dst
->value_type
== vtn_value_type_pointer
)
100 dst
->pointer
= vtn_decorate_pointer(b
, dst
, dst
->pointer
);
103 static struct vtn_access_chain
*
104 vtn_access_chain_create(struct vtn_builder
*b
, unsigned length
)
106 struct vtn_access_chain
*chain
;
108 /* Subtract 1 from the length since there's already one built in */
109 size_t size
= sizeof(*chain
) +
110 (MAX2(length
, 1) - 1) * sizeof(chain
->link
[0]);
111 chain
= rzalloc_size(b
, size
);
112 chain
->length
= length
;
118 vtn_mode_uses_ssa_offset(struct vtn_builder
*b
,
119 enum vtn_variable_mode mode
)
121 return ((mode
== vtn_variable_mode_ubo
||
122 mode
== vtn_variable_mode_ssbo
) &&
123 b
->options
->lower_ubo_ssbo_access_to_offsets
) ||
124 mode
== vtn_variable_mode_push_constant
;
128 vtn_mode_is_cross_invocation(struct vtn_builder
*b
,
129 enum vtn_variable_mode mode
)
131 return mode
== vtn_variable_mode_ssbo
||
132 mode
== vtn_variable_mode_ubo
||
133 mode
== vtn_variable_mode_phys_ssbo
||
134 mode
== vtn_variable_mode_push_constant
||
135 mode
== vtn_variable_mode_workgroup
||
136 mode
== vtn_variable_mode_cross_workgroup
;
140 vtn_pointer_is_external_block(struct vtn_builder
*b
,
141 struct vtn_pointer
*ptr
)
143 return ptr
->mode
== vtn_variable_mode_ssbo
||
144 ptr
->mode
== vtn_variable_mode_ubo
||
145 ptr
->mode
== vtn_variable_mode_phys_ssbo
||
146 ptr
->mode
== vtn_variable_mode_push_constant
;
150 vtn_access_link_as_ssa(struct vtn_builder
*b
, struct vtn_access_link link
,
151 unsigned stride
, unsigned bit_size
)
153 vtn_assert(stride
> 0);
154 if (link
.mode
== vtn_access_mode_literal
) {
155 return nir_imm_intN_t(&b
->nb
, link
.id
* stride
, bit_size
);
157 nir_ssa_def
*ssa
= vtn_ssa_value(b
, link
.id
)->def
;
158 if (ssa
->bit_size
!= bit_size
)
159 ssa
= nir_i2i(&b
->nb
, ssa
, bit_size
);
160 return nir_imul_imm(&b
->nb
, ssa
, stride
);
164 static VkDescriptorType
165 vk_desc_type_for_mode(struct vtn_builder
*b
, enum vtn_variable_mode mode
)
168 case vtn_variable_mode_ubo
:
169 return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
;
170 case vtn_variable_mode_ssbo
:
171 return VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
;
173 vtn_fail("Invalid mode for vulkan_resource_index");
178 vtn_variable_resource_index(struct vtn_builder
*b
, struct vtn_variable
*var
,
179 nir_ssa_def
*desc_array_index
)
181 vtn_assert(b
->options
->environment
== NIR_SPIRV_VULKAN
);
183 if (!desc_array_index
) {
184 vtn_assert(glsl_type_is_struct_or_ifc(var
->type
->type
));
185 desc_array_index
= nir_imm_int(&b
->nb
, 0);
188 nir_intrinsic_instr
*instr
=
189 nir_intrinsic_instr_create(b
->nb
.shader
,
190 nir_intrinsic_vulkan_resource_index
);
191 instr
->src
[0] = nir_src_for_ssa(desc_array_index
);
192 nir_intrinsic_set_desc_set(instr
, var
->descriptor_set
);
193 nir_intrinsic_set_binding(instr
, var
->binding
);
194 nir_intrinsic_set_desc_type(instr
, vk_desc_type_for_mode(b
, var
->mode
));
196 vtn_fail_if(var
->mode
!= vtn_variable_mode_ubo
&&
197 var
->mode
!= vtn_variable_mode_ssbo
,
198 "Invalid mode for vulkan_resource_index");
200 nir_address_format addr_format
= vtn_mode_to_address_format(b
, var
->mode
);
201 const struct glsl_type
*index_type
=
202 b
->options
->lower_ubo_ssbo_access_to_offsets
?
203 glsl_uint_type() : nir_address_format_to_glsl_type(addr_format
);
205 instr
->num_components
= glsl_get_vector_elements(index_type
);
206 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, instr
->num_components
,
207 glsl_get_bit_size(index_type
), NULL
);
208 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
210 return &instr
->dest
.ssa
;
214 vtn_resource_reindex(struct vtn_builder
*b
, enum vtn_variable_mode mode
,
215 nir_ssa_def
*base_index
, nir_ssa_def
*offset_index
)
217 vtn_assert(b
->options
->environment
== NIR_SPIRV_VULKAN
);
219 nir_intrinsic_instr
*instr
=
220 nir_intrinsic_instr_create(b
->nb
.shader
,
221 nir_intrinsic_vulkan_resource_reindex
);
222 instr
->src
[0] = nir_src_for_ssa(base_index
);
223 instr
->src
[1] = nir_src_for_ssa(offset_index
);
224 nir_intrinsic_set_desc_type(instr
, vk_desc_type_for_mode(b
, mode
));
226 vtn_fail_if(mode
!= vtn_variable_mode_ubo
&& mode
!= vtn_variable_mode_ssbo
,
227 "Invalid mode for vulkan_resource_reindex");
229 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
230 const struct glsl_type
*index_type
=
231 b
->options
->lower_ubo_ssbo_access_to_offsets
?
232 glsl_uint_type() : nir_address_format_to_glsl_type(addr_format
);
234 instr
->num_components
= glsl_get_vector_elements(index_type
);
235 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, instr
->num_components
,
236 glsl_get_bit_size(index_type
), NULL
);
237 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
239 return &instr
->dest
.ssa
;
243 vtn_descriptor_load(struct vtn_builder
*b
, enum vtn_variable_mode mode
,
244 nir_ssa_def
*desc_index
)
246 vtn_assert(b
->options
->environment
== NIR_SPIRV_VULKAN
);
248 nir_intrinsic_instr
*desc_load
=
249 nir_intrinsic_instr_create(b
->nb
.shader
,
250 nir_intrinsic_load_vulkan_descriptor
);
251 desc_load
->src
[0] = nir_src_for_ssa(desc_index
);
252 nir_intrinsic_set_desc_type(desc_load
, vk_desc_type_for_mode(b
, mode
));
254 vtn_fail_if(mode
!= vtn_variable_mode_ubo
&& mode
!= vtn_variable_mode_ssbo
,
255 "Invalid mode for load_vulkan_descriptor");
257 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
258 const struct glsl_type
*ptr_type
=
259 nir_address_format_to_glsl_type(addr_format
);
261 desc_load
->num_components
= glsl_get_vector_elements(ptr_type
);
262 nir_ssa_dest_init(&desc_load
->instr
, &desc_load
->dest
,
263 desc_load
->num_components
,
264 glsl_get_bit_size(ptr_type
), NULL
);
265 nir_builder_instr_insert(&b
->nb
, &desc_load
->instr
);
267 return &desc_load
->dest
.ssa
;
270 /* Dereference the given base pointer by the access chain */
271 static struct vtn_pointer
*
272 vtn_nir_deref_pointer_dereference(struct vtn_builder
*b
,
273 struct vtn_pointer
*base
,
274 struct vtn_access_chain
*deref_chain
)
276 struct vtn_type
*type
= base
->type
;
277 enum gl_access_qualifier access
= base
->access
| deref_chain
->access
;
280 nir_deref_instr
*tail
;
283 } else if (b
->options
->environment
== NIR_SPIRV_VULKAN
&&
284 vtn_pointer_is_external_block(b
, base
)) {
285 nir_ssa_def
*block_index
= base
->block_index
;
287 /* We dereferencing an external block pointer. Correctness of this
288 * operation relies on one particular line in the SPIR-V spec, section
289 * entitled "Validation Rules for Shader Capabilities":
291 * "Block and BufferBlock decorations cannot decorate a structure
292 * type that is nested at any level inside another structure type
293 * decorated with Block or BufferBlock."
295 * This means that we can detect the point where we cross over from
296 * descriptor indexing to buffer indexing by looking for the block
297 * decorated struct type. Anything before the block decorated struct
298 * type is a descriptor indexing operation and anything after the block
299 * decorated struct is a buffer offset operation.
302 /* Figure out the descriptor array index if any
304 * Some of the Vulkan CTS tests with hand-rolled SPIR-V have been known
305 * to forget the Block or BufferBlock decoration from time to time.
306 * It's more robust if we check for both !block_index and for the type
307 * to contain a block. This way there's a decent chance that arrays of
308 * UBOs/SSBOs will work correctly even if variable pointers are
311 nir_ssa_def
*desc_arr_idx
= NULL
;
312 if (!block_index
|| vtn_type_contains_block(b
, type
)) {
313 /* If our type contains a block, then we're still outside the block
314 * and we need to process enough levels of dereferences to get inside
317 if (deref_chain
->ptr_as_array
) {
318 unsigned aoa_size
= glsl_get_aoa_size(type
->type
);
319 desc_arr_idx
= vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
320 MAX2(aoa_size
, 1), 32);
324 for (; idx
< deref_chain
->length
; idx
++) {
325 if (type
->base_type
!= vtn_base_type_array
) {
326 vtn_assert(type
->base_type
== vtn_base_type_struct
);
330 unsigned aoa_size
= glsl_get_aoa_size(type
->array_element
->type
);
331 nir_ssa_def
*arr_offset
=
332 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
333 MAX2(aoa_size
, 1), 32);
335 desc_arr_idx
= nir_iadd(&b
->nb
, desc_arr_idx
, arr_offset
);
337 desc_arr_idx
= arr_offset
;
339 type
= type
->array_element
;
340 access
|= type
->access
;
345 vtn_assert(base
->var
&& base
->type
);
346 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
347 } else if (desc_arr_idx
) {
348 block_index
= vtn_resource_reindex(b
, base
->mode
,
349 block_index
, desc_arr_idx
);
352 if (idx
== deref_chain
->length
) {
353 /* The entire deref was consumed in finding the block index. Return
354 * a pointer which just has a block index and a later access chain
355 * will dereference deeper.
357 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
358 ptr
->mode
= base
->mode
;
360 ptr
->block_index
= block_index
;
361 ptr
->access
= access
;
365 /* If we got here, there's more access chain to handle and we have the
366 * final block index. Insert a descriptor load and cast to a deref to
367 * start the deref chain.
369 nir_ssa_def
*desc
= vtn_descriptor_load(b
, base
->mode
, block_index
);
371 assert(base
->mode
== vtn_variable_mode_ssbo
||
372 base
->mode
== vtn_variable_mode_ubo
);
373 nir_variable_mode nir_mode
=
374 base
->mode
== vtn_variable_mode_ssbo
? nir_var_mem_ssbo
: nir_var_mem_ubo
;
376 tail
= nir_build_deref_cast(&b
->nb
, desc
, nir_mode
,
377 vtn_type_get_nir_type(b
, type
, base
->mode
),
378 base
->ptr_type
->stride
);
380 assert(base
->var
&& base
->var
->var
);
381 tail
= nir_build_deref_var(&b
->nb
, base
->var
->var
);
382 if (base
->ptr_type
&& base
->ptr_type
->type
) {
383 tail
->dest
.ssa
.num_components
=
384 glsl_get_vector_elements(base
->ptr_type
->type
);
385 tail
->dest
.ssa
.bit_size
= glsl_get_bit_size(base
->ptr_type
->type
);
389 if (idx
== 0 && deref_chain
->ptr_as_array
) {
390 /* We start with a deref cast to get the stride. Hopefully, we'll be
391 * able to delete that cast eventually.
393 tail
= nir_build_deref_cast(&b
->nb
, &tail
->dest
.ssa
, tail
->mode
,
394 tail
->type
, base
->ptr_type
->stride
);
396 nir_ssa_def
*index
= vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1,
397 tail
->dest
.ssa
.bit_size
);
398 tail
= nir_build_deref_ptr_as_array(&b
->nb
, tail
, index
);
402 for (; idx
< deref_chain
->length
; idx
++) {
403 if (glsl_type_is_struct_or_ifc(type
->type
)) {
404 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
405 unsigned field
= deref_chain
->link
[idx
].id
;
406 tail
= nir_build_deref_struct(&b
->nb
, tail
, field
);
407 type
= type
->members
[field
];
409 nir_ssa_def
*arr_index
=
410 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
], 1,
411 tail
->dest
.ssa
.bit_size
);
412 tail
= nir_build_deref_array(&b
->nb
, tail
, arr_index
);
413 type
= type
->array_element
;
416 access
|= type
->access
;
419 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
420 ptr
->mode
= base
->mode
;
422 ptr
->var
= base
->var
;
424 ptr
->access
= access
;
429 static struct vtn_pointer
*
430 vtn_ssa_offset_pointer_dereference(struct vtn_builder
*b
,
431 struct vtn_pointer
*base
,
432 struct vtn_access_chain
*deref_chain
)
434 nir_ssa_def
*block_index
= base
->block_index
;
435 nir_ssa_def
*offset
= base
->offset
;
436 struct vtn_type
*type
= base
->type
;
437 enum gl_access_qualifier access
= base
->access
;
440 if (base
->mode
== vtn_variable_mode_ubo
||
441 base
->mode
== vtn_variable_mode_ssbo
) {
443 vtn_assert(base
->var
&& base
->type
);
444 nir_ssa_def
*desc_arr_idx
;
445 if (glsl_type_is_array(type
->type
)) {
446 if (deref_chain
->length
>= 1) {
448 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
450 /* This consumes a level of type */
451 type
= type
->array_element
;
452 access
|= type
->access
;
454 /* This is annoying. We've been asked for a pointer to the
455 * array of UBOs/SSBOs and not a specifc buffer. Return a
456 * pointer with a descriptor index of 0 and we'll have to do
457 * a reindex later to adjust it to the right thing.
459 desc_arr_idx
= nir_imm_int(&b
->nb
, 0);
461 } else if (deref_chain
->ptr_as_array
) {
462 /* You can't have a zero-length OpPtrAccessChain */
463 vtn_assert(deref_chain
->length
>= 1);
464 desc_arr_idx
= vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
466 /* We have a regular non-array SSBO. */
469 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
470 } else if (deref_chain
->ptr_as_array
&&
471 type
->base_type
== vtn_base_type_struct
&& type
->block
) {
472 /* We are doing an OpPtrAccessChain on a pointer to a struct that is
473 * decorated block. This is an interesting corner in the SPIR-V
474 * spec. One interpretation would be that they client is clearly
475 * trying to treat that block as if it's an implicit array of blocks
476 * repeated in the buffer. However, the SPIR-V spec for the
477 * OpPtrAccessChain says:
479 * "Base is treated as the address of the first element of an
480 * array, and the Element element’s address is computed to be the
481 * base for the Indexes, as per OpAccessChain."
483 * Taken literally, that would mean that your struct type is supposed
484 * to be treated as an array of such a struct and, since it's
485 * decorated block, that means an array of blocks which corresponds
486 * to an array descriptor. Therefore, we need to do a reindex
487 * operation to add the index from the first link in the access chain
488 * to the index we recieved.
490 * The downside to this interpretation (there always is one) is that
491 * this might be somewhat surprising behavior to apps if they expect
492 * the implicit array behavior described above.
494 vtn_assert(deref_chain
->length
>= 1);
495 nir_ssa_def
*offset_index
=
496 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
499 block_index
= vtn_resource_reindex(b
, base
->mode
,
500 block_index
, offset_index
);
505 if (base
->mode
== vtn_variable_mode_push_constant
) {
506 /* Push constants neither need nor have a block index */
507 vtn_assert(!block_index
);
509 /* Start off with at the start of the push constant block. */
510 offset
= nir_imm_int(&b
->nb
, 0);
512 /* The code above should have ensured a block_index when needed. */
513 vtn_assert(block_index
);
515 /* Start off with at the start of the buffer. */
516 offset
= nir_imm_int(&b
->nb
, 0);
520 if (deref_chain
->ptr_as_array
&& idx
== 0) {
521 /* We need ptr_type for the stride */
522 vtn_assert(base
->ptr_type
);
524 /* We need at least one element in the chain */
525 vtn_assert(deref_chain
->length
>= 1);
527 nir_ssa_def
*elem_offset
=
528 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
529 base
->ptr_type
->stride
, offset
->bit_size
);
530 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
534 for (; idx
< deref_chain
->length
; idx
++) {
535 switch (glsl_get_base_type(type
->type
)) {
538 case GLSL_TYPE_UINT16
:
539 case GLSL_TYPE_INT16
:
540 case GLSL_TYPE_UINT8
:
542 case GLSL_TYPE_UINT64
:
543 case GLSL_TYPE_INT64
:
544 case GLSL_TYPE_FLOAT
:
545 case GLSL_TYPE_FLOAT16
:
546 case GLSL_TYPE_DOUBLE
:
548 case GLSL_TYPE_ARRAY
: {
549 nir_ssa_def
*elem_offset
=
550 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
551 type
->stride
, offset
->bit_size
);
552 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
553 type
= type
->array_element
;
554 access
|= type
->access
;
558 case GLSL_TYPE_INTERFACE
:
559 case GLSL_TYPE_STRUCT
: {
560 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
561 unsigned member
= deref_chain
->link
[idx
].id
;
562 offset
= nir_iadd_imm(&b
->nb
, offset
, type
->offsets
[member
]);
563 type
= type
->members
[member
];
564 access
|= type
->access
;
569 vtn_fail("Invalid type for deref");
573 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
574 ptr
->mode
= base
->mode
;
576 ptr
->block_index
= block_index
;
577 ptr
->offset
= offset
;
578 ptr
->access
= access
;
583 /* Dereference the given base pointer by the access chain */
584 static struct vtn_pointer
*
585 vtn_pointer_dereference(struct vtn_builder
*b
,
586 struct vtn_pointer
*base
,
587 struct vtn_access_chain
*deref_chain
)
589 if (vtn_pointer_uses_ssa_offset(b
, base
)) {
590 return vtn_ssa_offset_pointer_dereference(b
, base
, deref_chain
);
592 return vtn_nir_deref_pointer_dereference(b
, base
, deref_chain
);
597 vtn_pointer_to_deref(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
599 vtn_assert(!vtn_pointer_uses_ssa_offset(b
, ptr
));
601 struct vtn_access_chain chain
= {
604 ptr
= vtn_nir_deref_pointer_dereference(b
, ptr
, &chain
);
611 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_instr
*deref
,
612 struct vtn_ssa_value
*inout
,
613 enum gl_access_qualifier access
)
615 if (glsl_type_is_vector_or_scalar(deref
->type
)) {
617 inout
->def
= nir_load_deref_with_access(&b
->nb
, deref
, access
);
619 nir_store_deref_with_access(&b
->nb
, deref
, inout
->def
, ~0, access
);
621 } else if (glsl_type_is_array(deref
->type
) ||
622 glsl_type_is_matrix(deref
->type
)) {
623 unsigned elems
= glsl_get_length(deref
->type
);
624 for (unsigned i
= 0; i
< elems
; i
++) {
625 nir_deref_instr
*child
=
626 nir_build_deref_array_imm(&b
->nb
, deref
, i
);
627 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
], access
);
630 vtn_assert(glsl_type_is_struct_or_ifc(deref
->type
));
631 unsigned elems
= glsl_get_length(deref
->type
);
632 for (unsigned i
= 0; i
< elems
; i
++) {
633 nir_deref_instr
*child
= nir_build_deref_struct(&b
->nb
, deref
, i
);
634 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
], access
);
640 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
642 struct vtn_pointer
*ptr
= vtn_value(b
, id
, vtn_value_type_pointer
)->pointer
;
643 return vtn_pointer_to_deref(b
, ptr
);
647 * Gets the NIR-level deref tail, which may have as a child an array deref
648 * selecting which component due to OpAccessChain supporting per-component
649 * indexing in SPIR-V.
651 static nir_deref_instr
*
652 get_deref_tail(nir_deref_instr
*deref
)
654 if (deref
->deref_type
!= nir_deref_type_array
)
657 nir_deref_instr
*parent
=
658 nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
);
660 if (glsl_type_is_vector(parent
->type
))
666 struct vtn_ssa_value
*
667 vtn_local_load(struct vtn_builder
*b
, nir_deref_instr
*src
,
668 enum gl_access_qualifier access
)
670 nir_deref_instr
*src_tail
= get_deref_tail(src
);
671 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
672 _vtn_local_load_store(b
, true, src_tail
, val
, access
);
674 if (src_tail
!= src
) {
675 val
->type
= src
->type
;
676 val
->def
= nir_vector_extract(&b
->nb
, val
->def
, src
->arr
.index
.ssa
);
683 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
684 nir_deref_instr
*dest
, enum gl_access_qualifier access
)
686 nir_deref_instr
*dest_tail
= get_deref_tail(dest
);
688 if (dest_tail
!= dest
) {
689 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
690 _vtn_local_load_store(b
, true, dest_tail
, val
, access
);
692 val
->def
= nir_vector_insert(&b
->nb
, val
->def
, src
->def
,
693 dest
->arr
.index
.ssa
);
694 _vtn_local_load_store(b
, false, dest_tail
, val
, access
);
696 _vtn_local_load_store(b
, false, dest_tail
, src
, access
);
701 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
702 nir_ssa_def
**index_out
)
704 assert(vtn_pointer_uses_ssa_offset(b
, ptr
));
706 struct vtn_access_chain chain
= {
709 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
711 *index_out
= ptr
->block_index
;
715 /* Tries to compute the size of an interface block based on the strides and
716 * offsets that are provided to us in the SPIR-V source.
719 vtn_type_block_size(struct vtn_builder
*b
, struct vtn_type
*type
)
721 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
725 case GLSL_TYPE_UINT16
:
726 case GLSL_TYPE_INT16
:
727 case GLSL_TYPE_UINT8
:
729 case GLSL_TYPE_UINT64
:
730 case GLSL_TYPE_INT64
:
731 case GLSL_TYPE_FLOAT
:
732 case GLSL_TYPE_FLOAT16
:
734 case GLSL_TYPE_DOUBLE
: {
735 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
736 glsl_get_matrix_columns(type
->type
);
738 vtn_assert(type
->stride
> 0);
739 return type
->stride
* cols
;
741 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
742 return glsl_get_vector_elements(type
->type
) * type_size
;
746 case GLSL_TYPE_STRUCT
:
747 case GLSL_TYPE_INTERFACE
: {
749 unsigned num_fields
= glsl_get_length(type
->type
);
750 for (unsigned f
= 0; f
< num_fields
; f
++) {
751 unsigned field_end
= type
->offsets
[f
] +
752 vtn_type_block_size(b
, type
->members
[f
]);
753 size
= MAX2(size
, field_end
);
758 case GLSL_TYPE_ARRAY
:
759 vtn_assert(type
->stride
> 0);
760 vtn_assert(glsl_get_length(type
->type
) > 0);
761 return type
->stride
* glsl_get_length(type
->type
);
764 vtn_fail("Invalid block type");
770 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
771 nir_ssa_def
*index
, nir_ssa_def
*offset
,
772 unsigned access_offset
, unsigned access_size
,
773 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
,
774 enum gl_access_qualifier access
)
776 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
777 instr
->num_components
= glsl_get_vector_elements(type
);
779 /* Booleans usually shouldn't show up in external memory in SPIR-V.
780 * However, they do for certain older GLSLang versions and can for shared
781 * memory when we lower access chains internally.
783 const unsigned data_bit_size
= glsl_type_is_boolean(type
) ? 32 :
784 glsl_get_bit_size(type
);
788 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
789 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
792 if (op
== nir_intrinsic_load_push_constant
) {
793 nir_intrinsic_set_base(instr
, access_offset
);
794 nir_intrinsic_set_range(instr
, access_size
);
797 if (op
== nir_intrinsic_load_ubo
||
798 op
== nir_intrinsic_load_ssbo
||
799 op
== nir_intrinsic_store_ssbo
) {
800 nir_intrinsic_set_access(instr
, access
);
803 /* With extensions like relaxed_block_layout, we really can't guarantee
804 * much more than scalar alignment.
806 if (op
!= nir_intrinsic_load_push_constant
)
807 nir_intrinsic_set_align(instr
, data_bit_size
/ 8, 0);
810 instr
->src
[src
++] = nir_src_for_ssa(index
);
812 if (op
== nir_intrinsic_load_push_constant
) {
813 /* We need to subtract the offset from where the intrinsic will load the
816 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
817 nir_imm_int(&b
->nb
, access_offset
)));
819 instr
->src
[src
++] = nir_src_for_ssa(offset
);
823 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
824 instr
->num_components
, data_bit_size
, NULL
);
825 (*inout
)->def
= &instr
->dest
.ssa
;
828 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
830 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
831 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
835 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
836 nir_ssa_def
*index
, nir_ssa_def
*offset
,
837 unsigned access_offset
, unsigned access_size
,
838 struct vtn_type
*type
, enum gl_access_qualifier access
,
839 struct vtn_ssa_value
**inout
)
841 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
845 case GLSL_TYPE_UINT16
:
846 case GLSL_TYPE_INT16
:
847 case GLSL_TYPE_UINT8
:
849 case GLSL_TYPE_UINT64
:
850 case GLSL_TYPE_INT64
:
851 case GLSL_TYPE_FLOAT
:
852 case GLSL_TYPE_FLOAT16
:
853 case GLSL_TYPE_DOUBLE
:
855 /* This is where things get interesting. At this point, we've hit
856 * a vector, a scalar, or a matrix.
858 if (glsl_type_is_matrix(type
->type
)) {
859 /* Loading the whole matrix */
860 struct vtn_ssa_value
*transpose
;
861 unsigned num_ops
, vec_width
, col_stride
;
862 if (type
->row_major
) {
863 num_ops
= glsl_get_vector_elements(type
->type
);
864 vec_width
= glsl_get_matrix_columns(type
->type
);
865 col_stride
= type
->array_element
->stride
;
867 const struct glsl_type
*transpose_type
=
868 glsl_matrix_type(base_type
, vec_width
, num_ops
);
869 *inout
= vtn_create_ssa_value(b
, transpose_type
);
871 transpose
= vtn_ssa_transpose(b
, *inout
);
875 num_ops
= glsl_get_matrix_columns(type
->type
);
876 vec_width
= glsl_get_vector_elements(type
->type
);
877 col_stride
= type
->stride
;
880 for (unsigned i
= 0; i
< num_ops
; i
++) {
881 nir_ssa_def
*elem_offset
=
882 nir_iadd_imm(&b
->nb
, offset
, i
* col_stride
);
883 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
884 access_offset
, access_size
,
886 glsl_vector_type(base_type
, vec_width
),
887 type
->access
| access
);
890 if (load
&& type
->row_major
)
891 *inout
= vtn_ssa_transpose(b
, *inout
);
893 unsigned elems
= glsl_get_vector_elements(type
->type
);
894 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
895 if (elems
== 1 || type
->stride
== type_size
) {
896 /* This is a tightly-packed normal scalar or vector load */
897 vtn_assert(glsl_type_is_vector_or_scalar(type
->type
));
898 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
899 access_offset
, access_size
,
901 type
->access
| access
);
903 /* This is a strided load. We have to load N things separately.
904 * This is the single column of a row-major matrix case.
906 vtn_assert(type
->stride
> type_size
);
907 vtn_assert(type
->stride
% type_size
== 0);
909 nir_ssa_def
*per_comp
[4];
910 for (unsigned i
= 0; i
< elems
; i
++) {
911 nir_ssa_def
*elem_offset
=
912 nir_iadd_imm(&b
->nb
, offset
, i
* type
->stride
);
913 struct vtn_ssa_value
*comp
, temp_val
;
915 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
916 temp_val
.type
= glsl_scalar_type(base_type
);
919 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
920 access_offset
, access_size
,
921 &comp
, glsl_scalar_type(base_type
),
922 type
->access
| access
);
923 per_comp
[i
] = comp
->def
;
928 *inout
= vtn_create_ssa_value(b
, type
->type
);
929 (*inout
)->def
= nir_vec(&b
->nb
, per_comp
, elems
);
935 case GLSL_TYPE_ARRAY
: {
936 unsigned elems
= glsl_get_length(type
->type
);
937 for (unsigned i
= 0; i
< elems
; i
++) {
938 nir_ssa_def
*elem_off
=
939 nir_iadd_imm(&b
->nb
, offset
, i
* type
->stride
);
940 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
941 access_offset
, access_size
,
943 type
->array_element
->access
| access
,
944 &(*inout
)->elems
[i
]);
949 case GLSL_TYPE_INTERFACE
:
950 case GLSL_TYPE_STRUCT
: {
951 unsigned elems
= glsl_get_length(type
->type
);
952 for (unsigned i
= 0; i
< elems
; i
++) {
953 nir_ssa_def
*elem_off
=
954 nir_iadd_imm(&b
->nb
, offset
, type
->offsets
[i
]);
955 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
956 access_offset
, access_size
,
958 type
->members
[i
]->access
| access
,
959 &(*inout
)->elems
[i
]);
965 vtn_fail("Invalid block member type");
969 static struct vtn_ssa_value
*
970 vtn_block_load(struct vtn_builder
*b
, struct vtn_pointer
*src
,
971 enum gl_access_qualifier access
)
974 unsigned access_offset
= 0, access_size
= 0;
976 case vtn_variable_mode_ubo
:
977 op
= nir_intrinsic_load_ubo
;
979 case vtn_variable_mode_ssbo
:
980 op
= nir_intrinsic_load_ssbo
;
982 case vtn_variable_mode_push_constant
:
983 op
= nir_intrinsic_load_push_constant
;
984 access_size
= b
->shader
->num_uniforms
;
986 case vtn_variable_mode_workgroup
:
987 op
= nir_intrinsic_load_shared
;
990 vtn_fail("Invalid block variable mode");
993 nir_ssa_def
*offset
, *index
= NULL
;
994 offset
= vtn_pointer_to_offset(b
, src
, &index
);
996 struct vtn_ssa_value
*value
= vtn_create_ssa_value(b
, src
->type
->type
);
997 _vtn_block_load_store(b
, op
, true, index
, offset
,
998 access_offset
, access_size
,
999 src
->type
, src
->access
| access
, &value
);
1004 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
1005 struct vtn_pointer
*dst
, enum gl_access_qualifier access
)
1007 nir_intrinsic_op op
;
1008 switch (dst
->mode
) {
1009 case vtn_variable_mode_ssbo
:
1010 op
= nir_intrinsic_store_ssbo
;
1012 case vtn_variable_mode_workgroup
:
1013 op
= nir_intrinsic_store_shared
;
1016 vtn_fail("Invalid block variable mode");
1019 nir_ssa_def
*offset
, *index
= NULL
;
1020 offset
= vtn_pointer_to_offset(b
, dst
, &index
);
1022 _vtn_block_load_store(b
, op
, false, index
, offset
,
1023 0, 0, dst
->type
, dst
->access
| access
, &src
);
1027 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
1028 struct vtn_pointer
*ptr
,
1029 enum gl_access_qualifier access
,
1030 struct vtn_ssa_value
**inout
)
1032 if (ptr
->mode
== vtn_variable_mode_uniform
) {
1033 if (ptr
->type
->base_type
== vtn_base_type_image
||
1034 ptr
->type
->base_type
== vtn_base_type_sampler
) {
1035 /* See also our handling of OpTypeSampler and OpTypeImage */
1037 (*inout
)->def
= vtn_pointer_to_ssa(b
, ptr
);
1039 } else if (ptr
->type
->base_type
== vtn_base_type_sampled_image
) {
1040 /* See also our handling of OpTypeSampledImage */
1042 struct vtn_sampled_image si
= {
1043 .image
= vtn_pointer_to_deref(b
, ptr
),
1044 .sampler
= vtn_pointer_to_deref(b
, ptr
),
1046 (*inout
)->def
= vtn_sampled_image_to_nir_ssa(b
, si
);
1051 enum glsl_base_type base_type
= glsl_get_base_type(ptr
->type
->type
);
1052 switch (base_type
) {
1053 case GLSL_TYPE_UINT
:
1055 case GLSL_TYPE_UINT16
:
1056 case GLSL_TYPE_INT16
:
1057 case GLSL_TYPE_UINT8
:
1058 case GLSL_TYPE_INT8
:
1059 case GLSL_TYPE_UINT64
:
1060 case GLSL_TYPE_INT64
:
1061 case GLSL_TYPE_FLOAT
:
1062 case GLSL_TYPE_FLOAT16
:
1063 case GLSL_TYPE_BOOL
:
1064 case GLSL_TYPE_DOUBLE
:
1065 if (glsl_type_is_vector_or_scalar(ptr
->type
->type
)) {
1066 /* We hit a vector or scalar; go ahead and emit the load[s] */
1067 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
1068 if (vtn_mode_is_cross_invocation(b
, ptr
->mode
)) {
1069 /* If it's cross-invocation, we call nir_load/store_deref
1070 * directly. The vtn_local_load/store helpers are too clever and
1071 * do magic to avoid array derefs of vectors. That magic is both
1072 * less efficient than the direct load/store and, in the case of
1073 * stores, is broken because it creates a race condition if two
1074 * threads are writing to different components of the same vector
1075 * due to the load+insert+store it uses to emulate the array
1079 (*inout
)->def
= nir_load_deref_with_access(&b
->nb
, deref
,
1080 ptr
->type
->access
| access
);
1082 nir_store_deref_with_access(&b
->nb
, deref
, (*inout
)->def
, ~0,
1083 ptr
->type
->access
| access
);
1087 *inout
= vtn_local_load(b
, deref
, ptr
->type
->access
| access
);
1089 vtn_local_store(b
, *inout
, deref
, ptr
->type
->access
| access
);
1096 case GLSL_TYPE_INTERFACE
:
1097 case GLSL_TYPE_ARRAY
:
1098 case GLSL_TYPE_STRUCT
: {
1099 unsigned elems
= glsl_get_length(ptr
->type
->type
);
1100 struct vtn_access_chain chain
= {
1103 { .mode
= vtn_access_mode_literal
, },
1106 for (unsigned i
= 0; i
< elems
; i
++) {
1107 chain
.link
[0].id
= i
;
1108 struct vtn_pointer
*elem
= vtn_pointer_dereference(b
, ptr
, &chain
);
1109 _vtn_variable_load_store(b
, load
, elem
, ptr
->type
->access
| access
,
1110 &(*inout
)->elems
[i
]);
1116 vtn_fail("Invalid access chain type");
1120 struct vtn_ssa_value
*
1121 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
,
1122 enum gl_access_qualifier access
)
1124 if (vtn_pointer_uses_ssa_offset(b
, src
)) {
1125 return vtn_block_load(b
, src
, access
);
1127 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src
->type
->type
);
1128 _vtn_variable_load_store(b
, true, src
, src
->access
| access
, &val
);
1134 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
1135 struct vtn_pointer
*dest
, enum gl_access_qualifier access
)
1137 if (vtn_pointer_uses_ssa_offset(b
, dest
)) {
1138 vtn_assert(dest
->mode
== vtn_variable_mode_ssbo
||
1139 dest
->mode
== vtn_variable_mode_workgroup
);
1140 vtn_block_store(b
, src
, dest
, access
);
1142 _vtn_variable_load_store(b
, false, dest
, dest
->access
| access
, &src
);
1147 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
1148 struct vtn_pointer
*src
, enum gl_access_qualifier dest_access
,
1149 enum gl_access_qualifier src_access
)
1151 vtn_assert(glsl_get_bare_type(src
->type
->type
) ==
1152 glsl_get_bare_type(dest
->type
->type
));
1153 enum glsl_base_type base_type
= glsl_get_base_type(src
->type
->type
);
1154 switch (base_type
) {
1155 case GLSL_TYPE_UINT
:
1157 case GLSL_TYPE_UINT16
:
1158 case GLSL_TYPE_INT16
:
1159 case GLSL_TYPE_UINT8
:
1160 case GLSL_TYPE_INT8
:
1161 case GLSL_TYPE_UINT64
:
1162 case GLSL_TYPE_INT64
:
1163 case GLSL_TYPE_FLOAT
:
1164 case GLSL_TYPE_FLOAT16
:
1165 case GLSL_TYPE_DOUBLE
:
1166 case GLSL_TYPE_BOOL
:
1167 /* At this point, we have a scalar, vector, or matrix so we know that
1168 * there cannot be any structure splitting still in the way. By
1169 * stopping at the matrix level rather than the vector level, we
1170 * ensure that matrices get loaded in the optimal way even if they
1171 * are storred row-major in a UBO.
1173 vtn_variable_store(b
, vtn_variable_load(b
, src
, src_access
), dest
, dest_access
);
1176 case GLSL_TYPE_INTERFACE
:
1177 case GLSL_TYPE_ARRAY
:
1178 case GLSL_TYPE_STRUCT
: {
1179 struct vtn_access_chain chain
= {
1182 { .mode
= vtn_access_mode_literal
, },
1185 unsigned elems
= glsl_get_length(src
->type
->type
);
1186 for (unsigned i
= 0; i
< elems
; i
++) {
1187 chain
.link
[0].id
= i
;
1188 struct vtn_pointer
*src_elem
=
1189 vtn_pointer_dereference(b
, src
, &chain
);
1190 struct vtn_pointer
*dest_elem
=
1191 vtn_pointer_dereference(b
, dest
, &chain
);
1193 _vtn_variable_copy(b
, dest_elem
, src_elem
, dest_access
, src_access
);
1199 vtn_fail("Invalid access chain type");
1204 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
1205 struct vtn_pointer
*src
, enum gl_access_qualifier dest_access
,
1206 enum gl_access_qualifier src_access
)
1208 /* TODO: At some point, we should add a special-case for when we can
1209 * just emit a copy_var intrinsic.
1211 _vtn_variable_copy(b
, dest
, src
, dest_access
, src_access
);
1215 set_mode_system_value(struct vtn_builder
*b
, nir_variable_mode
*mode
)
1217 vtn_assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
1218 *mode
= nir_var_system_value
;
1222 vtn_get_builtin_location(struct vtn_builder
*b
,
1223 SpvBuiltIn builtin
, int *location
,
1224 nir_variable_mode
*mode
)
1227 case SpvBuiltInPosition
:
1228 *location
= VARYING_SLOT_POS
;
1230 case SpvBuiltInPointSize
:
1231 *location
= VARYING_SLOT_PSIZ
;
1233 case SpvBuiltInClipDistance
:
1234 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
1236 case SpvBuiltInCullDistance
:
1237 *location
= VARYING_SLOT_CULL_DIST0
;
1239 case SpvBuiltInVertexId
:
1240 case SpvBuiltInVertexIndex
:
1241 /* The Vulkan spec defines VertexIndex to be non-zero-based and doesn't
1242 * allow VertexId. The ARB_gl_spirv spec defines VertexId to be the
1243 * same as gl_VertexID, which is non-zero-based, and removes
1244 * VertexIndex. Since they're both defined to be non-zero-based, we use
1245 * SYSTEM_VALUE_VERTEX_ID for both.
1247 *location
= SYSTEM_VALUE_VERTEX_ID
;
1248 set_mode_system_value(b
, mode
);
1250 case SpvBuiltInInstanceIndex
:
1251 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
1252 set_mode_system_value(b
, mode
);
1254 case SpvBuiltInInstanceId
:
1255 *location
= SYSTEM_VALUE_INSTANCE_ID
;
1256 set_mode_system_value(b
, mode
);
1258 case SpvBuiltInPrimitiveId
:
1259 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1260 vtn_assert(*mode
== nir_var_shader_in
);
1261 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1262 } else if (*mode
== nir_var_shader_out
) {
1263 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1265 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
1266 set_mode_system_value(b
, mode
);
1269 case SpvBuiltInInvocationId
:
1270 *location
= SYSTEM_VALUE_INVOCATION_ID
;
1271 set_mode_system_value(b
, mode
);
1273 case SpvBuiltInLayer
:
1274 *location
= VARYING_SLOT_LAYER
;
1275 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1276 *mode
= nir_var_shader_in
;
1277 else if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1278 *mode
= nir_var_shader_out
;
1279 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1280 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1281 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1282 *mode
= nir_var_shader_out
;
1284 vtn_fail("invalid stage for SpvBuiltInLayer");
1286 case SpvBuiltInViewportIndex
:
1287 *location
= VARYING_SLOT_VIEWPORT
;
1288 if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1289 *mode
= nir_var_shader_out
;
1290 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1291 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1292 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1293 *mode
= nir_var_shader_out
;
1294 else if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1295 *mode
= nir_var_shader_in
;
1297 vtn_fail("invalid stage for SpvBuiltInViewportIndex");
1299 case SpvBuiltInTessLevelOuter
:
1300 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
1302 case SpvBuiltInTessLevelInner
:
1303 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
1305 case SpvBuiltInTessCoord
:
1306 *location
= SYSTEM_VALUE_TESS_COORD
;
1307 set_mode_system_value(b
, mode
);
1309 case SpvBuiltInPatchVertices
:
1310 *location
= SYSTEM_VALUE_VERTICES_IN
;
1311 set_mode_system_value(b
, mode
);
1313 case SpvBuiltInFragCoord
:
1314 vtn_assert(*mode
== nir_var_shader_in
);
1315 if (b
->options
&& b
->options
->frag_coord_is_sysval
) {
1316 *mode
= nir_var_system_value
;
1317 *location
= SYSTEM_VALUE_FRAG_COORD
;
1319 *location
= VARYING_SLOT_POS
;
1322 case SpvBuiltInPointCoord
:
1323 *location
= VARYING_SLOT_PNTC
;
1324 vtn_assert(*mode
== nir_var_shader_in
);
1326 case SpvBuiltInFrontFacing
:
1327 *location
= SYSTEM_VALUE_FRONT_FACE
;
1328 set_mode_system_value(b
, mode
);
1330 case SpvBuiltInSampleId
:
1331 *location
= SYSTEM_VALUE_SAMPLE_ID
;
1332 set_mode_system_value(b
, mode
);
1334 case SpvBuiltInSamplePosition
:
1335 *location
= SYSTEM_VALUE_SAMPLE_POS
;
1336 set_mode_system_value(b
, mode
);
1338 case SpvBuiltInSampleMask
:
1339 if (*mode
== nir_var_shader_out
) {
1340 *location
= FRAG_RESULT_SAMPLE_MASK
;
1342 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1343 set_mode_system_value(b
, mode
);
1346 case SpvBuiltInFragDepth
:
1347 *location
= FRAG_RESULT_DEPTH
;
1348 vtn_assert(*mode
== nir_var_shader_out
);
1350 case SpvBuiltInHelperInvocation
:
1351 *location
= SYSTEM_VALUE_HELPER_INVOCATION
;
1352 set_mode_system_value(b
, mode
);
1354 case SpvBuiltInNumWorkgroups
:
1355 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1356 set_mode_system_value(b
, mode
);
1358 case SpvBuiltInWorkgroupSize
:
1359 *location
= SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
1360 set_mode_system_value(b
, mode
);
1362 case SpvBuiltInWorkgroupId
:
1363 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1364 set_mode_system_value(b
, mode
);
1366 case SpvBuiltInLocalInvocationId
:
1367 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1368 set_mode_system_value(b
, mode
);
1370 case SpvBuiltInLocalInvocationIndex
:
1371 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1372 set_mode_system_value(b
, mode
);
1374 case SpvBuiltInGlobalInvocationId
:
1375 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1376 set_mode_system_value(b
, mode
);
1378 case SpvBuiltInGlobalLinearId
:
1379 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX
;
1380 set_mode_system_value(b
, mode
);
1382 case SpvBuiltInGlobalOffset
:
1383 *location
= SYSTEM_VALUE_BASE_GLOBAL_INVOCATION_ID
;
1384 set_mode_system_value(b
, mode
);
1386 case SpvBuiltInBaseVertex
:
1387 /* OpenGL gl_BaseVertex (SYSTEM_VALUE_BASE_VERTEX) is not the same
1388 * semantic as Vulkan BaseVertex (SYSTEM_VALUE_FIRST_VERTEX).
1390 if (b
->options
->environment
== NIR_SPIRV_OPENGL
)
1391 *location
= SYSTEM_VALUE_BASE_VERTEX
;
1393 *location
= SYSTEM_VALUE_FIRST_VERTEX
;
1394 set_mode_system_value(b
, mode
);
1396 case SpvBuiltInBaseInstance
:
1397 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1398 set_mode_system_value(b
, mode
);
1400 case SpvBuiltInDrawIndex
:
1401 *location
= SYSTEM_VALUE_DRAW_ID
;
1402 set_mode_system_value(b
, mode
);
1404 case SpvBuiltInSubgroupSize
:
1405 *location
= SYSTEM_VALUE_SUBGROUP_SIZE
;
1406 set_mode_system_value(b
, mode
);
1408 case SpvBuiltInSubgroupId
:
1409 *location
= SYSTEM_VALUE_SUBGROUP_ID
;
1410 set_mode_system_value(b
, mode
);
1412 case SpvBuiltInSubgroupLocalInvocationId
:
1413 *location
= SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1414 set_mode_system_value(b
, mode
);
1416 case SpvBuiltInNumSubgroups
:
1417 *location
= SYSTEM_VALUE_NUM_SUBGROUPS
;
1418 set_mode_system_value(b
, mode
);
1420 case SpvBuiltInDeviceIndex
:
1421 *location
= SYSTEM_VALUE_DEVICE_INDEX
;
1422 set_mode_system_value(b
, mode
);
1424 case SpvBuiltInViewIndex
:
1425 if (b
->options
&& b
->options
->view_index_is_input
) {
1426 *location
= VARYING_SLOT_VIEW_INDEX
;
1427 vtn_assert(*mode
== nir_var_shader_in
);
1429 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1430 set_mode_system_value(b
, mode
);
1433 case SpvBuiltInSubgroupEqMask
:
1434 *location
= SYSTEM_VALUE_SUBGROUP_EQ_MASK
,
1435 set_mode_system_value(b
, mode
);
1437 case SpvBuiltInSubgroupGeMask
:
1438 *location
= SYSTEM_VALUE_SUBGROUP_GE_MASK
,
1439 set_mode_system_value(b
, mode
);
1441 case SpvBuiltInSubgroupGtMask
:
1442 *location
= SYSTEM_VALUE_SUBGROUP_GT_MASK
,
1443 set_mode_system_value(b
, mode
);
1445 case SpvBuiltInSubgroupLeMask
:
1446 *location
= SYSTEM_VALUE_SUBGROUP_LE_MASK
,
1447 set_mode_system_value(b
, mode
);
1449 case SpvBuiltInSubgroupLtMask
:
1450 *location
= SYSTEM_VALUE_SUBGROUP_LT_MASK
,
1451 set_mode_system_value(b
, mode
);
1453 case SpvBuiltInFragStencilRefEXT
:
1454 *location
= FRAG_RESULT_STENCIL
;
1455 vtn_assert(*mode
== nir_var_shader_out
);
1457 case SpvBuiltInWorkDim
:
1458 *location
= SYSTEM_VALUE_WORK_DIM
;
1459 set_mode_system_value(b
, mode
);
1461 case SpvBuiltInGlobalSize
:
1462 *location
= SYSTEM_VALUE_GLOBAL_GROUP_SIZE
;
1463 set_mode_system_value(b
, mode
);
1465 case SpvBuiltInBaryCoordNoPerspAMD
:
1466 *location
= SYSTEM_VALUE_BARYCENTRIC_LINEAR_PIXEL
;
1467 set_mode_system_value(b
, mode
);
1469 case SpvBuiltInBaryCoordNoPerspCentroidAMD
:
1470 *location
= SYSTEM_VALUE_BARYCENTRIC_LINEAR_CENTROID
;
1471 set_mode_system_value(b
, mode
);
1473 case SpvBuiltInBaryCoordNoPerspSampleAMD
:
1474 *location
= SYSTEM_VALUE_BARYCENTRIC_LINEAR_SAMPLE
;
1475 set_mode_system_value(b
, mode
);
1477 case SpvBuiltInBaryCoordSmoothAMD
:
1478 *location
= SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL
;
1479 set_mode_system_value(b
, mode
);
1481 case SpvBuiltInBaryCoordSmoothCentroidAMD
:
1482 *location
= SYSTEM_VALUE_BARYCENTRIC_PERSP_CENTROID
;
1483 set_mode_system_value(b
, mode
);
1485 case SpvBuiltInBaryCoordSmoothSampleAMD
:
1486 *location
= SYSTEM_VALUE_BARYCENTRIC_PERSP_SAMPLE
;
1487 set_mode_system_value(b
, mode
);
1489 case SpvBuiltInBaryCoordPullModelAMD
:
1490 *location
= SYSTEM_VALUE_BARYCENTRIC_PULL_MODEL
;
1491 set_mode_system_value(b
, mode
);
1494 vtn_fail("Unsupported builtin: %s (%u)",
1495 spirv_builtin_to_string(builtin
), builtin
);
1500 apply_var_decoration(struct vtn_builder
*b
,
1501 struct nir_variable_data
*var_data
,
1502 const struct vtn_decoration
*dec
)
1504 switch (dec
->decoration
) {
1505 case SpvDecorationRelaxedPrecision
:
1506 break; /* FIXME: Do nothing with this for now. */
1507 case SpvDecorationNoPerspective
:
1508 var_data
->interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1510 case SpvDecorationFlat
:
1511 var_data
->interpolation
= INTERP_MODE_FLAT
;
1513 case SpvDecorationExplicitInterpAMD
:
1514 var_data
->interpolation
= INTERP_MODE_EXPLICIT
;
1516 case SpvDecorationCentroid
:
1517 var_data
->centroid
= true;
1519 case SpvDecorationSample
:
1520 var_data
->sample
= true;
1522 case SpvDecorationInvariant
:
1523 var_data
->invariant
= true;
1525 case SpvDecorationConstant
:
1526 var_data
->read_only
= true;
1528 case SpvDecorationNonReadable
:
1529 var_data
->access
|= ACCESS_NON_READABLE
;
1531 case SpvDecorationNonWritable
:
1532 var_data
->read_only
= true;
1533 var_data
->access
|= ACCESS_NON_WRITEABLE
;
1535 case SpvDecorationRestrict
:
1536 var_data
->access
|= ACCESS_RESTRICT
;
1538 case SpvDecorationAliased
:
1539 var_data
->access
&= ~ACCESS_RESTRICT
;
1541 case SpvDecorationVolatile
:
1542 var_data
->access
|= ACCESS_VOLATILE
;
1544 case SpvDecorationCoherent
:
1545 var_data
->access
|= ACCESS_COHERENT
;
1547 case SpvDecorationComponent
:
1548 var_data
->location_frac
= dec
->operands
[0];
1550 case SpvDecorationIndex
:
1551 var_data
->index
= dec
->operands
[0];
1553 case SpvDecorationBuiltIn
: {
1554 SpvBuiltIn builtin
= dec
->operands
[0];
1556 nir_variable_mode mode
= var_data
->mode
;
1557 vtn_get_builtin_location(b
, builtin
, &var_data
->location
, &mode
);
1558 var_data
->mode
= mode
;
1561 case SpvBuiltInTessLevelOuter
:
1562 case SpvBuiltInTessLevelInner
:
1563 case SpvBuiltInClipDistance
:
1564 case SpvBuiltInCullDistance
:
1565 var_data
->compact
= true;
1572 case SpvDecorationSpecId
:
1573 case SpvDecorationRowMajor
:
1574 case SpvDecorationColMajor
:
1575 case SpvDecorationMatrixStride
:
1576 case SpvDecorationUniform
:
1577 case SpvDecorationUniformId
:
1578 case SpvDecorationLinkageAttributes
:
1579 break; /* Do nothing with these here */
1581 case SpvDecorationPatch
:
1582 var_data
->patch
= true;
1585 case SpvDecorationLocation
:
1586 vtn_fail("Handled above");
1588 case SpvDecorationBlock
:
1589 case SpvDecorationBufferBlock
:
1590 case SpvDecorationArrayStride
:
1591 case SpvDecorationGLSLShared
:
1592 case SpvDecorationGLSLPacked
:
1593 break; /* These can apply to a type but we don't care about them */
1595 case SpvDecorationBinding
:
1596 case SpvDecorationDescriptorSet
:
1597 case SpvDecorationNoContraction
:
1598 case SpvDecorationInputAttachmentIndex
:
1599 vtn_warn("Decoration not allowed for variable or structure member: %s",
1600 spirv_decoration_to_string(dec
->decoration
));
1603 case SpvDecorationXfbBuffer
:
1604 var_data
->explicit_xfb_buffer
= true;
1605 var_data
->xfb
.buffer
= dec
->operands
[0];
1606 var_data
->always_active_io
= true;
1608 case SpvDecorationXfbStride
:
1609 var_data
->explicit_xfb_stride
= true;
1610 var_data
->xfb
.stride
= dec
->operands
[0];
1612 case SpvDecorationOffset
:
1613 var_data
->explicit_offset
= true;
1614 var_data
->offset
= dec
->operands
[0];
1617 case SpvDecorationStream
:
1618 var_data
->stream
= dec
->operands
[0];
1621 case SpvDecorationCPacked
:
1622 case SpvDecorationSaturatedConversion
:
1623 case SpvDecorationFuncParamAttr
:
1624 case SpvDecorationFPRoundingMode
:
1625 case SpvDecorationFPFastMathMode
:
1626 case SpvDecorationAlignment
:
1627 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
1628 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1629 spirv_decoration_to_string(dec
->decoration
));
1633 case SpvDecorationUserSemantic
:
1634 case SpvDecorationUserTypeGOOGLE
:
1635 /* User semantic decorations can safely be ignored by the driver. */
1638 case SpvDecorationRestrictPointerEXT
:
1639 case SpvDecorationAliasedPointerEXT
:
1640 /* TODO: We should actually plumb alias information through NIR. */
1644 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1649 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1650 const struct vtn_decoration
*dec
, void *out_is_patch
)
1652 if (dec
->decoration
== SpvDecorationPatch
) {
1653 *((bool *) out_is_patch
) = true;
1658 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1659 const struct vtn_decoration
*dec
, void *void_var
)
1661 struct vtn_variable
*vtn_var
= void_var
;
1663 /* Handle decorations that apply to a vtn_variable as a whole */
1664 switch (dec
->decoration
) {
1665 case SpvDecorationBinding
:
1666 vtn_var
->binding
= dec
->operands
[0];
1667 vtn_var
->explicit_binding
= true;
1669 case SpvDecorationDescriptorSet
:
1670 vtn_var
->descriptor_set
= dec
->operands
[0];
1672 case SpvDecorationInputAttachmentIndex
:
1673 vtn_var
->input_attachment_index
= dec
->operands
[0];
1675 case SpvDecorationPatch
:
1676 vtn_var
->patch
= true;
1678 case SpvDecorationOffset
:
1679 vtn_var
->offset
= dec
->operands
[0];
1681 case SpvDecorationNonWritable
:
1682 vtn_var
->access
|= ACCESS_NON_WRITEABLE
;
1684 case SpvDecorationNonReadable
:
1685 vtn_var
->access
|= ACCESS_NON_READABLE
;
1687 case SpvDecorationVolatile
:
1688 vtn_var
->access
|= ACCESS_VOLATILE
;
1690 case SpvDecorationCoherent
:
1691 vtn_var
->access
|= ACCESS_COHERENT
;
1693 case SpvDecorationCounterBuffer
:
1694 /* Counter buffer decorations can safely be ignored by the driver. */
1700 if (val
->value_type
== vtn_value_type_pointer
) {
1701 assert(val
->pointer
->var
== void_var
);
1702 assert(member
== -1);
1704 assert(val
->value_type
== vtn_value_type_type
);
1707 /* Location is odd. If applied to a split structure, we have to walk the
1708 * whole thing and accumulate the location. It's easier to handle as a
1711 if (dec
->decoration
== SpvDecorationLocation
) {
1712 unsigned location
= dec
->operands
[0];
1713 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
&&
1714 vtn_var
->mode
== vtn_variable_mode_output
) {
1715 location
+= FRAG_RESULT_DATA0
;
1716 } else if (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
&&
1717 vtn_var
->mode
== vtn_variable_mode_input
) {
1718 location
+= VERT_ATTRIB_GENERIC0
;
1719 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1720 vtn_var
->mode
== vtn_variable_mode_output
) {
1721 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1722 } else if (vtn_var
->mode
!= vtn_variable_mode_uniform
) {
1723 vtn_warn("Location must be on input, output, uniform, sampler or "
1728 if (vtn_var
->var
->num_members
== 0) {
1729 /* This handles the member and lone variable cases */
1730 vtn_var
->var
->data
.location
= location
;
1732 /* This handles the structure member case */
1733 assert(vtn_var
->var
->members
);
1736 vtn_var
->base_location
= location
;
1738 vtn_var
->var
->members
[member
].location
= location
;
1744 if (vtn_var
->var
->num_members
== 0) {
1745 /* We call this function on types as well as variables and not all
1746 * struct types get split so we can end up having stray member
1747 * decorations; just ignore them.
1750 apply_var_decoration(b
, &vtn_var
->var
->data
, dec
);
1751 } else if (member
>= 0) {
1752 /* Member decorations must come from a type */
1753 assert(val
->value_type
== vtn_value_type_type
);
1754 apply_var_decoration(b
, &vtn_var
->var
->members
[member
], dec
);
1757 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1758 for (unsigned i
= 0; i
< length
; i
++)
1759 apply_var_decoration(b
, &vtn_var
->var
->members
[i
], dec
);
1762 /* A few variables, those with external storage, have no actual
1763 * nir_variables associated with them. Fortunately, all decorations
1764 * we care about for those variables are on the type only.
1766 vtn_assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1767 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1768 vtn_var
->mode
== vtn_variable_mode_push_constant
);
1773 enum vtn_variable_mode
1774 vtn_storage_class_to_mode(struct vtn_builder
*b
,
1775 SpvStorageClass
class,
1776 struct vtn_type
*interface_type
,
1777 nir_variable_mode
*nir_mode_out
)
1779 enum vtn_variable_mode mode
;
1780 nir_variable_mode nir_mode
;
1782 case SpvStorageClassUniform
:
1783 /* Assume it's an UBO if we lack the interface_type. */
1784 if (!interface_type
|| interface_type
->block
) {
1785 mode
= vtn_variable_mode_ubo
;
1786 nir_mode
= nir_var_mem_ubo
;
1787 } else if (interface_type
->buffer_block
) {
1788 mode
= vtn_variable_mode_ssbo
;
1789 nir_mode
= nir_var_mem_ssbo
;
1791 /* Default-block uniforms, coming from gl_spirv */
1792 mode
= vtn_variable_mode_uniform
;
1793 nir_mode
= nir_var_uniform
;
1796 case SpvStorageClassStorageBuffer
:
1797 mode
= vtn_variable_mode_ssbo
;
1798 nir_mode
= nir_var_mem_ssbo
;
1800 case SpvStorageClassPhysicalStorageBuffer
:
1801 mode
= vtn_variable_mode_phys_ssbo
;
1802 nir_mode
= nir_var_mem_global
;
1804 case SpvStorageClassUniformConstant
:
1805 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1806 if (b
->options
->constant_as_global
) {
1807 mode
= vtn_variable_mode_cross_workgroup
;
1808 nir_mode
= nir_var_mem_global
;
1810 mode
= vtn_variable_mode_ubo
;
1811 nir_mode
= nir_var_mem_ubo
;
1814 mode
= vtn_variable_mode_uniform
;
1815 nir_mode
= nir_var_uniform
;
1818 case SpvStorageClassPushConstant
:
1819 mode
= vtn_variable_mode_push_constant
;
1820 nir_mode
= nir_var_uniform
;
1822 case SpvStorageClassInput
:
1823 mode
= vtn_variable_mode_input
;
1824 nir_mode
= nir_var_shader_in
;
1826 case SpvStorageClassOutput
:
1827 mode
= vtn_variable_mode_output
;
1828 nir_mode
= nir_var_shader_out
;
1830 case SpvStorageClassPrivate
:
1831 mode
= vtn_variable_mode_private
;
1832 nir_mode
= nir_var_shader_temp
;
1834 case SpvStorageClassFunction
:
1835 mode
= vtn_variable_mode_function
;
1836 nir_mode
= nir_var_function_temp
;
1838 case SpvStorageClassWorkgroup
:
1839 mode
= vtn_variable_mode_workgroup
;
1840 nir_mode
= nir_var_mem_shared
;
1842 case SpvStorageClassAtomicCounter
:
1843 mode
= vtn_variable_mode_atomic_counter
;
1844 nir_mode
= nir_var_uniform
;
1846 case SpvStorageClassCrossWorkgroup
:
1847 mode
= vtn_variable_mode_cross_workgroup
;
1848 nir_mode
= nir_var_mem_global
;
1850 case SpvStorageClassImage
:
1851 mode
= vtn_variable_mode_image
;
1852 nir_mode
= nir_var_mem_ubo
;
1854 case SpvStorageClassGeneric
:
1856 vtn_fail("Unhandled variable storage class: %s (%u)",
1857 spirv_storageclass_to_string(class), class);
1861 *nir_mode_out
= nir_mode
;
1867 vtn_mode_to_address_format(struct vtn_builder
*b
, enum vtn_variable_mode mode
)
1870 case vtn_variable_mode_ubo
:
1871 return b
->options
->ubo_addr_format
;
1873 case vtn_variable_mode_ssbo
:
1874 return b
->options
->ssbo_addr_format
;
1876 case vtn_variable_mode_phys_ssbo
:
1877 return b
->options
->phys_ssbo_addr_format
;
1879 case vtn_variable_mode_push_constant
:
1880 return b
->options
->push_const_addr_format
;
1882 case vtn_variable_mode_workgroup
:
1883 return b
->options
->shared_addr_format
;
1885 case vtn_variable_mode_cross_workgroup
:
1886 return b
->options
->global_addr_format
;
1888 case vtn_variable_mode_function
:
1889 if (b
->physical_ptrs
)
1890 return b
->options
->temp_addr_format
;
1893 case vtn_variable_mode_private
:
1894 case vtn_variable_mode_uniform
:
1895 case vtn_variable_mode_atomic_counter
:
1896 case vtn_variable_mode_input
:
1897 case vtn_variable_mode_output
:
1898 case vtn_variable_mode_image
:
1899 return nir_address_format_logical
;
1902 unreachable("Invalid variable mode");
1906 vtn_pointer_to_ssa(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
1908 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
1909 /* This pointer needs to have a pointer type with actual storage */
1910 vtn_assert(ptr
->ptr_type
);
1911 vtn_assert(ptr
->ptr_type
->type
);
1914 /* If we don't have an offset then we must be a pointer to the variable
1917 vtn_assert(!ptr
->offset
&& !ptr
->block_index
);
1919 struct vtn_access_chain chain
= {
1922 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
1925 vtn_assert(ptr
->offset
);
1926 if (ptr
->block_index
) {
1927 vtn_assert(ptr
->mode
== vtn_variable_mode_ubo
||
1928 ptr
->mode
== vtn_variable_mode_ssbo
);
1929 return nir_vec2(&b
->nb
, ptr
->block_index
, ptr
->offset
);
1931 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
);
1935 if (vtn_pointer_is_external_block(b
, ptr
) &&
1936 vtn_type_contains_block(b
, ptr
->type
) &&
1937 ptr
->mode
!= vtn_variable_mode_phys_ssbo
) {
1938 /* In this case, we're looking for a block index and not an actual
1941 * For PhysicalStorageBuffer pointers, we don't have a block index
1942 * at all because we get the pointer directly from the client. This
1943 * assumes that there will never be a SSBO binding variable using the
1944 * PhysicalStorageBuffer storage class. This assumption appears
1945 * to be correct according to the Vulkan spec because the table,
1946 * "Shader Resource and Storage Class Correspondence," the only the
1947 * Uniform storage class with BufferBlock or the StorageBuffer
1948 * storage class with Block can be used.
1950 if (!ptr
->block_index
) {
1951 /* If we don't have a block_index then we must be a pointer to the
1954 vtn_assert(!ptr
->deref
);
1956 struct vtn_access_chain chain
= {
1959 ptr
= vtn_nir_deref_pointer_dereference(b
, ptr
, &chain
);
1962 return ptr
->block_index
;
1964 return &vtn_pointer_to_deref(b
, ptr
)->dest
.ssa
;
1969 struct vtn_pointer
*
1970 vtn_pointer_from_ssa(struct vtn_builder
*b
, nir_ssa_def
*ssa
,
1971 struct vtn_type
*ptr_type
)
1973 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1975 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
1976 struct vtn_type
*without_array
=
1977 vtn_type_without_array(ptr_type
->deref
);
1979 nir_variable_mode nir_mode
;
1980 ptr
->mode
= vtn_storage_class_to_mode(b
, ptr_type
->storage_class
,
1981 without_array
, &nir_mode
);
1982 ptr
->type
= ptr_type
->deref
;
1983 ptr
->ptr_type
= ptr_type
;
1985 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
1986 /* This pointer type needs to have actual storage */
1987 vtn_assert(ptr_type
->type
);
1988 if (ptr
->mode
== vtn_variable_mode_ubo
||
1989 ptr
->mode
== vtn_variable_mode_ssbo
) {
1990 vtn_assert(ssa
->num_components
== 2);
1991 ptr
->block_index
= nir_channel(&b
->nb
, ssa
, 0);
1992 ptr
->offset
= nir_channel(&b
->nb
, ssa
, 1);
1994 vtn_assert(ssa
->num_components
== 1);
1995 ptr
->block_index
= NULL
;
1999 const struct glsl_type
*deref_type
=
2000 vtn_type_get_nir_type(b
, ptr_type
->deref
, ptr
->mode
);
2001 if (!vtn_pointer_is_external_block(b
, ptr
)) {
2002 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
2003 deref_type
, ptr_type
->stride
);
2004 } else if (vtn_type_contains_block(b
, ptr
->type
) &&
2005 ptr
->mode
!= vtn_variable_mode_phys_ssbo
) {
2006 /* This is a pointer to somewhere in an array of blocks, not a
2007 * pointer to somewhere inside the block. Set the block index
2008 * instead of making a cast.
2010 ptr
->block_index
= ssa
;
2012 /* This is a pointer to something internal or a pointer inside a
2013 * block. It's just a regular cast.
2015 * For PhysicalStorageBuffer pointers, we don't have a block index
2016 * at all because we get the pointer directly from the client. This
2017 * assumes that there will never be a SSBO binding variable using the
2018 * PhysicalStorageBuffer storage class. This assumption appears
2019 * to be correct according to the Vulkan spec because the table,
2020 * "Shader Resource and Storage Class Correspondence," the only the
2021 * Uniform storage class with BufferBlock or the StorageBuffer
2022 * storage class with Block can be used.
2024 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
2025 deref_type
, ptr_type
->stride
);
2026 ptr
->deref
->dest
.ssa
.num_components
=
2027 glsl_get_vector_elements(ptr_type
->type
);
2028 ptr
->deref
->dest
.ssa
.bit_size
= glsl_get_bit_size(ptr_type
->type
);
2036 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
2038 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
2041 if (var
->mode
== vtn_variable_mode_input
) {
2042 return stage
== MESA_SHADER_TESS_CTRL
||
2043 stage
== MESA_SHADER_TESS_EVAL
||
2044 stage
== MESA_SHADER_GEOMETRY
;
2047 if (var
->mode
== vtn_variable_mode_output
)
2048 return stage
== MESA_SHADER_TESS_CTRL
;
2054 assign_missing_member_locations(struct vtn_variable
*var
)
2057 glsl_get_length(glsl_without_array(var
->type
->type
));
2058 int location
= var
->base_location
;
2060 for (unsigned i
= 0; i
< length
; i
++) {
2061 /* From the Vulkan spec:
2063 * “If the structure type is a Block but without a Location, then each
2064 * of its members must have a Location decoration.”
2067 if (var
->type
->block
) {
2068 assert(var
->base_location
!= -1 ||
2069 var
->var
->members
[i
].location
!= -1);
2072 /* From the Vulkan spec:
2074 * “Any member with its own Location decoration is assigned that
2075 * location. Each remaining member is assigned the location after the
2076 * immediately preceding member in declaration order.”
2078 if (var
->var
->members
[i
].location
!= -1)
2079 location
= var
->var
->members
[i
].location
;
2081 var
->var
->members
[i
].location
= location
;
2083 /* Below we use type instead of interface_type, because interface_type
2084 * is only available when it is a Block. This code also supports
2085 * input/outputs that are just structs
2087 const struct glsl_type
*member_type
=
2088 glsl_get_struct_field(glsl_without_array(var
->type
->type
), i
);
2091 glsl_count_attribute_slots(member_type
,
2092 false /* is_gl_vertex_input */);
2098 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
2099 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
2100 nir_constant
*const_initializer
, nir_variable
*var_initializer
)
2102 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
2103 struct vtn_type
*type
= ptr_type
->deref
;
2105 struct vtn_type
*without_array
= vtn_type_without_array(ptr_type
->deref
);
2107 enum vtn_variable_mode mode
;
2108 nir_variable_mode nir_mode
;
2109 mode
= vtn_storage_class_to_mode(b
, storage_class
, without_array
, &nir_mode
);
2112 case vtn_variable_mode_ubo
:
2113 /* There's no other way to get vtn_variable_mode_ubo */
2114 vtn_assert(without_array
->block
);
2115 b
->shader
->info
.num_ubos
++;
2117 case vtn_variable_mode_ssbo
:
2118 if (storage_class
== SpvStorageClassStorageBuffer
&&
2119 !without_array
->block
) {
2120 if (b
->variable_pointers
) {
2121 vtn_fail("Variables in the StorageBuffer storage class must "
2122 "have a struct type with the Block decoration");
2124 /* If variable pointers are not present, it's still malformed
2125 * SPIR-V but we can parse it and do the right thing anyway.
2126 * Since some of the 8-bit storage tests have bugs in this are,
2127 * just make it a warning for now.
2129 vtn_warn("Variables in the StorageBuffer storage class must "
2130 "have a struct type with the Block decoration");
2133 b
->shader
->info
.num_ssbos
++;
2135 case vtn_variable_mode_uniform
:
2136 if (without_array
->base_type
== vtn_base_type_image
) {
2137 if (glsl_type_is_image(without_array
->glsl_image
))
2138 b
->shader
->info
.num_images
++;
2139 else if (glsl_type_is_sampler(without_array
->glsl_image
))
2140 b
->shader
->info
.num_textures
++;
2143 case vtn_variable_mode_push_constant
:
2144 b
->shader
->num_uniforms
= vtn_type_block_size(b
, type
);
2147 case vtn_variable_mode_image
:
2148 vtn_fail("Cannot create a variable with the Image storage class");
2151 case vtn_variable_mode_phys_ssbo
:
2152 vtn_fail("Cannot create a variable with the "
2153 "PhysicalStorageBuffer storage class");
2157 /* No tallying is needed */
2161 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
2164 var
->base_location
= -1;
2166 val
->pointer
= rzalloc(b
, struct vtn_pointer
);
2167 val
->pointer
->mode
= var
->mode
;
2168 val
->pointer
->type
= var
->type
;
2169 val
->pointer
->ptr_type
= ptr_type
;
2170 val
->pointer
->var
= var
;
2171 val
->pointer
->access
= var
->type
->access
;
2173 switch (var
->mode
) {
2174 case vtn_variable_mode_function
:
2175 case vtn_variable_mode_private
:
2176 case vtn_variable_mode_uniform
:
2177 case vtn_variable_mode_atomic_counter
:
2178 /* For these, we create the variable normally */
2179 var
->var
= rzalloc(b
->shader
, nir_variable
);
2180 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2181 var
->var
->type
= vtn_type_get_nir_type(b
, var
->type
, var
->mode
);
2182 var
->var
->data
.mode
= nir_mode
;
2183 var
->var
->data
.location
= -1;
2184 var
->var
->interface_type
= NULL
;
2187 case vtn_variable_mode_ubo
:
2188 case vtn_variable_mode_ssbo
:
2189 var
->var
= rzalloc(b
->shader
, nir_variable
);
2190 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2192 var
->var
->type
= vtn_type_get_nir_type(b
, var
->type
, var
->mode
);
2193 var
->var
->interface_type
= var
->var
->type
;
2195 var
->var
->data
.mode
= nir_mode
;
2196 var
->var
->data
.location
= -1;
2200 case vtn_variable_mode_workgroup
:
2201 /* Create the variable normally */
2202 var
->var
= rzalloc(b
->shader
, nir_variable
);
2203 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2204 var
->var
->type
= vtn_type_get_nir_type(b
, var
->type
, var
->mode
);
2205 var
->var
->data
.mode
= nir_var_mem_shared
;
2208 case vtn_variable_mode_input
:
2209 case vtn_variable_mode_output
: {
2210 /* In order to know whether or not we're a per-vertex inout, we need
2211 * the patch qualifier. This means walking the variable decorations
2212 * early before we actually create any variables. Not a big deal.
2214 * GLSLang really likes to place decorations in the most interior
2215 * thing it possibly can. In particular, if you have a struct, it
2216 * will place the patch decorations on the struct members. This
2217 * should be handled by the variable splitting below just fine.
2219 * If you have an array-of-struct, things get even more weird as it
2220 * will place the patch decorations on the struct even though it's
2221 * inside an array and some of the members being patch and others not
2222 * makes no sense whatsoever. Since the only sensible thing is for
2223 * it to be all or nothing, we'll call it patch if any of the members
2224 * are declared patch.
2227 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
2228 if (glsl_type_is_array(var
->type
->type
) &&
2229 glsl_type_is_struct_or_ifc(without_array
->type
)) {
2230 vtn_foreach_decoration(b
, vtn_value(b
, without_array
->id
,
2231 vtn_value_type_type
),
2232 var_is_patch_cb
, &var
->patch
);
2235 /* For inputs and outputs, we immediately split structures. This
2236 * is for a couple of reasons. For one, builtins may all come in
2237 * a struct and we really want those split out into separate
2238 * variables. For another, interpolation qualifiers can be
2239 * applied to members of the top-level struct ane we need to be
2240 * able to preserve that information.
2243 struct vtn_type
*per_vertex_type
= var
->type
;
2244 if (is_per_vertex_inout(var
, b
->shader
->info
.stage
)) {
2245 /* In Geometry shaders (and some tessellation), inputs come
2246 * in per-vertex arrays. However, some builtins come in
2247 * non-per-vertex, hence the need for the is_array check. In
2248 * any case, there are no non-builtin arrays allowed so this
2249 * check should be sufficient.
2251 per_vertex_type
= var
->type
->array_element
;
2254 var
->var
= rzalloc(b
->shader
, nir_variable
);
2255 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2256 var
->var
->type
= vtn_type_get_nir_type(b
, var
->type
, var
->mode
);
2257 var
->var
->data
.mode
= nir_mode
;
2258 var
->var
->data
.patch
= var
->patch
;
2260 /* Figure out the interface block type. */
2261 struct vtn_type
*iface_type
= per_vertex_type
;
2262 if (var
->mode
== vtn_variable_mode_output
&&
2263 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
2264 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
||
2265 b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)) {
2266 /* For vertex data outputs, we can end up with arrays of blocks for
2267 * transform feedback where each array element corresponds to a
2268 * different XFB output buffer.
2270 while (iface_type
->base_type
== vtn_base_type_array
)
2271 iface_type
= iface_type
->array_element
;
2273 if (iface_type
->base_type
== vtn_base_type_struct
&& iface_type
->block
)
2274 var
->var
->interface_type
= vtn_type_get_nir_type(b
, iface_type
,
2277 if (per_vertex_type
->base_type
== vtn_base_type_struct
&&
2278 per_vertex_type
->block
) {
2279 /* It's a struct. Set it up as per-member. */
2280 var
->var
->num_members
= glsl_get_length(per_vertex_type
->type
);
2281 var
->var
->members
= rzalloc_array(var
->var
, struct nir_variable_data
,
2282 var
->var
->num_members
);
2284 for (unsigned i
= 0; i
< var
->var
->num_members
; i
++) {
2285 var
->var
->members
[i
].mode
= nir_mode
;
2286 var
->var
->members
[i
].patch
= var
->patch
;
2287 var
->var
->members
[i
].location
= -1;
2291 /* For inputs and outputs, we need to grab locations and builtin
2292 * information from the per-vertex type.
2294 vtn_foreach_decoration(b
, vtn_value(b
, per_vertex_type
->id
,
2295 vtn_value_type_type
),
2296 var_decoration_cb
, var
);
2300 case vtn_variable_mode_push_constant
:
2301 case vtn_variable_mode_cross_workgroup
:
2302 /* These don't need actual variables. */
2305 case vtn_variable_mode_image
:
2306 case vtn_variable_mode_phys_ssbo
:
2307 unreachable("Should have been caught before");
2310 /* We can only have one type of initializer */
2311 assert(!(const_initializer
&& var_initializer
));
2312 if (const_initializer
) {
2313 var
->var
->constant_initializer
=
2314 nir_constant_clone(const_initializer
, var
->var
);
2316 if (var_initializer
)
2317 var
->var
->pointer_initializer
= var_initializer
;
2319 if (var
->mode
== vtn_variable_mode_uniform
||
2320 var
->mode
== vtn_variable_mode_ssbo
) {
2321 /* SSBOs and images are assumed to not alias in the Simple, GLSL and Vulkan memory models */
2322 var
->var
->data
.access
|= b
->mem_model
!= SpvMemoryModelOpenCL
? ACCESS_RESTRICT
: 0;
2325 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
2326 vtn_foreach_decoration(b
, val
, ptr_decoration_cb
, val
->pointer
);
2328 /* Propagate access flags from the OpVariable decorations. */
2329 val
->pointer
->access
|= var
->access
;
2331 if ((var
->mode
== vtn_variable_mode_input
||
2332 var
->mode
== vtn_variable_mode_output
) &&
2333 var
->var
->members
) {
2334 assign_missing_member_locations(var
);
2337 if (var
->mode
== vtn_variable_mode_uniform
||
2338 var
->mode
== vtn_variable_mode_ubo
||
2339 var
->mode
== vtn_variable_mode_ssbo
||
2340 var
->mode
== vtn_variable_mode_atomic_counter
) {
2341 /* XXX: We still need the binding information in the nir_variable
2342 * for these. We should fix that.
2344 var
->var
->data
.binding
= var
->binding
;
2345 var
->var
->data
.explicit_binding
= var
->explicit_binding
;
2346 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
2347 var
->var
->data
.index
= var
->input_attachment_index
;
2348 var
->var
->data
.offset
= var
->offset
;
2350 if (glsl_type_is_image(glsl_without_array(var
->var
->type
)))
2351 var
->var
->data
.image
.format
= without_array
->image_format
;
2354 if (var
->mode
== vtn_variable_mode_function
) {
2355 vtn_assert(var
->var
!= NULL
&& var
->var
->members
== NULL
);
2356 nir_function_impl_add_variable(b
->nb
.impl
, var
->var
);
2357 } else if (var
->var
) {
2358 nir_shader_add_variable(b
->shader
, var
->var
);
2360 vtn_assert(vtn_pointer_is_external_block(b
, val
->pointer
));
2365 vtn_assert_types_equal(struct vtn_builder
*b
, SpvOp opcode
,
2366 struct vtn_type
*dst_type
,
2367 struct vtn_type
*src_type
)
2369 if (dst_type
->id
== src_type
->id
)
2372 if (vtn_types_compatible(b
, dst_type
, src_type
)) {
2373 /* Early versions of GLSLang would re-emit types unnecessarily and you
2374 * would end up with OpLoad, OpStore, or OpCopyMemory opcodes which have
2375 * mismatched source and destination types.
2377 * https://github.com/KhronosGroup/glslang/issues/304
2378 * https://github.com/KhronosGroup/glslang/issues/307
2379 * https://bugs.freedesktop.org/show_bug.cgi?id=104338
2380 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2382 vtn_warn("Source and destination types of %s do not have the same "
2383 "ID (but are compatible): %u vs %u",
2384 spirv_op_to_string(opcode
), dst_type
->id
, src_type
->id
);
2388 vtn_fail("Source and destination types of %s do not match: %s vs. %s",
2389 spirv_op_to_string(opcode
),
2390 glsl_get_type_name(dst_type
->type
),
2391 glsl_get_type_name(src_type
->type
));
2394 static nir_ssa_def
*
2395 nir_shrink_zero_pad_vec(nir_builder
*b
, nir_ssa_def
*val
,
2396 unsigned num_components
)
2398 if (val
->num_components
== num_components
)
2401 nir_ssa_def
*comps
[NIR_MAX_VEC_COMPONENTS
];
2402 for (unsigned i
= 0; i
< num_components
; i
++) {
2403 if (i
< val
->num_components
)
2404 comps
[i
] = nir_channel(b
, val
, i
);
2406 comps
[i
] = nir_imm_intN_t(b
, 0, val
->bit_size
);
2408 return nir_vec(b
, comps
, num_components
);
2411 static nir_ssa_def
*
2412 nir_sloppy_bitcast(nir_builder
*b
, nir_ssa_def
*val
,
2413 const struct glsl_type
*type
)
2415 const unsigned num_components
= glsl_get_vector_elements(type
);
2416 const unsigned bit_size
= glsl_get_bit_size(type
);
2418 /* First, zero-pad to ensure that the value is big enough that when we
2419 * bit-cast it, we don't loose anything.
2421 if (val
->bit_size
< bit_size
) {
2422 const unsigned src_num_components_needed
=
2423 vtn_align_u32(val
->num_components
, bit_size
/ val
->bit_size
);
2424 val
= nir_shrink_zero_pad_vec(b
, val
, src_num_components_needed
);
2427 val
= nir_bitcast_vector(b
, val
, bit_size
);
2429 return nir_shrink_zero_pad_vec(b
, val
, num_components
);
2433 vtn_get_mem_operands(struct vtn_builder
*b
, const uint32_t *w
, unsigned count
,
2434 unsigned *idx
, SpvMemoryAccessMask
*access
, unsigned *alignment
,
2435 SpvScope
*dest_scope
, SpvScope
*src_scope
)
2442 *access
= w
[(*idx
)++];
2443 if (*access
& SpvMemoryAccessAlignedMask
) {
2444 vtn_assert(*idx
< count
);
2445 *alignment
= w
[(*idx
)++];
2448 if (*access
& SpvMemoryAccessMakePointerAvailableMask
) {
2449 vtn_assert(*idx
< count
);
2450 vtn_assert(dest_scope
);
2451 *dest_scope
= vtn_constant_uint(b
, w
[(*idx
)++]);
2454 if (*access
& SpvMemoryAccessMakePointerVisibleMask
) {
2455 vtn_assert(*idx
< count
);
2456 vtn_assert(src_scope
);
2457 *src_scope
= vtn_constant_uint(b
, w
[(*idx
)++]);
2463 static enum gl_access_qualifier
2464 spv_access_to_gl_access(SpvMemoryAccessMask access
)
2466 if (access
& SpvMemoryAccessVolatileMask
)
2467 return ACCESS_VOLATILE
;
2473 SpvMemorySemanticsMask
2474 vtn_mode_to_memory_semantics(enum vtn_variable_mode mode
)
2477 case vtn_variable_mode_ssbo
:
2478 case vtn_variable_mode_phys_ssbo
:
2479 return SpvMemorySemanticsUniformMemoryMask
;
2480 case vtn_variable_mode_workgroup
:
2481 return SpvMemorySemanticsWorkgroupMemoryMask
;
2482 case vtn_variable_mode_cross_workgroup
:
2483 return SpvMemorySemanticsCrossWorkgroupMemoryMask
;
2484 case vtn_variable_mode_atomic_counter
:
2485 return SpvMemorySemanticsAtomicCounterMemoryMask
;
2486 case vtn_variable_mode_image
:
2487 return SpvMemorySemanticsImageMemoryMask
;
2488 case vtn_variable_mode_output
:
2489 return SpvMemorySemanticsOutputMemoryMask
;
2491 return SpvMemorySemanticsMaskNone
;
2496 vtn_emit_make_visible_barrier(struct vtn_builder
*b
, SpvMemoryAccessMask access
,
2497 SpvScope scope
, enum vtn_variable_mode mode
)
2499 if (!(access
& SpvMemoryAccessMakePointerVisibleMask
))
2502 vtn_emit_memory_barrier(b
, scope
, SpvMemorySemanticsMakeVisibleMask
|
2503 SpvMemorySemanticsAcquireMask
|
2504 vtn_mode_to_memory_semantics(mode
));
2508 vtn_emit_make_available_barrier(struct vtn_builder
*b
, SpvMemoryAccessMask access
,
2509 SpvScope scope
, enum vtn_variable_mode mode
)
2511 if (!(access
& SpvMemoryAccessMakePointerAvailableMask
))
2514 vtn_emit_memory_barrier(b
, scope
, SpvMemorySemanticsMakeAvailableMask
|
2515 SpvMemorySemanticsReleaseMask
|
2516 vtn_mode_to_memory_semantics(mode
));
2520 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
2521 const uint32_t *w
, unsigned count
)
2525 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
2526 val
->type
= vtn_get_type(b
, w
[1]);
2530 case SpvOpVariable
: {
2531 struct vtn_type
*ptr_type
= vtn_get_type(b
, w
[1]);
2533 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2535 SpvStorageClass storage_class
= w
[3];
2536 nir_constant
*const_initializer
= NULL
;
2537 nir_variable
*var_initializer
= NULL
;
2539 struct vtn_value
*init
= vtn_untyped_value(b
, w
[4]);
2540 switch (init
->value_type
) {
2541 case vtn_value_type_constant
:
2542 const_initializer
= init
->constant
;
2544 case vtn_value_type_pointer
:
2545 var_initializer
= init
->pointer
->var
->var
;
2548 vtn_fail("SPIR-V variable initializer %u must be constant or pointer",
2553 vtn_create_variable(b
, val
, ptr_type
, storage_class
, const_initializer
, var_initializer
);
2558 case SpvOpConstantSampler
: {
2559 /* Synthesize a pointer-to-sampler type, create a variable of that type,
2560 * and give the variable a constant initializer with the sampler params */
2561 struct vtn_type
*sampler_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2562 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2564 struct vtn_type
*ptr_type
= rzalloc(b
, struct vtn_type
);
2565 ptr_type
= rzalloc(b
, struct vtn_type
);
2566 ptr_type
->base_type
= vtn_base_type_pointer
;
2567 ptr_type
->deref
= sampler_type
;
2568 ptr_type
->storage_class
= SpvStorageClassUniform
;
2570 ptr_type
->type
= nir_address_format_to_glsl_type(
2571 vtn_mode_to_address_format(b
, vtn_variable_mode_function
));
2573 vtn_create_variable(b
, val
, ptr_type
, ptr_type
->storage_class
, NULL
, NULL
);
2575 nir_variable
*nir_var
= val
->pointer
->var
->var
;
2576 nir_var
->data
.sampler
.is_inline_sampler
= true;
2577 nir_var
->data
.sampler
.addressing_mode
= w
[3];
2578 nir_var
->data
.sampler
.normalized_coordinates
= w
[4];
2579 nir_var
->data
.sampler
.filter_mode
= w
[5];
2584 case SpvOpAccessChain
:
2585 case SpvOpPtrAccessChain
:
2586 case SpvOpInBoundsAccessChain
:
2587 case SpvOpInBoundsPtrAccessChain
: {
2588 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
2589 enum gl_access_qualifier access
= 0;
2590 chain
->ptr_as_array
= (opcode
== SpvOpPtrAccessChain
|| opcode
== SpvOpInBoundsPtrAccessChain
);
2593 for (int i
= 4; i
< count
; i
++) {
2594 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
2595 if (link_val
->value_type
== vtn_value_type_constant
) {
2596 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
2597 chain
->link
[idx
].id
= vtn_constant_int(b
, w
[i
]);
2599 chain
->link
[idx
].mode
= vtn_access_mode_id
;
2600 chain
->link
[idx
].id
= w
[i
];
2605 struct vtn_type
*ptr_type
= vtn_get_type(b
, w
[1]);
2606 struct vtn_pointer
*base
=
2607 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2608 struct vtn_pointer
*ptr
= vtn_pointer_dereference(b
, base
, chain
);
2609 ptr
->ptr_type
= ptr_type
;
2610 ptr
->access
|= access
;
2611 vtn_push_pointer(b
, w
[2], ptr
);
2615 case SpvOpCopyMemory
: {
2616 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2617 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
2619 vtn_assert_types_equal(b
, opcode
, dest
->type
->deref
, src
->type
->deref
);
2621 unsigned idx
= 3, dest_alignment
, src_alignment
;
2622 SpvMemoryAccessMask dest_access
, src_access
;
2623 SpvScope dest_scope
, src_scope
;
2624 vtn_get_mem_operands(b
, w
, count
, &idx
, &dest_access
, &dest_alignment
,
2625 &dest_scope
, &src_scope
);
2626 if (!vtn_get_mem_operands(b
, w
, count
, &idx
, &src_access
, &src_alignment
,
2627 NULL
, &src_scope
)) {
2628 src_alignment
= dest_alignment
;
2629 src_access
= dest_access
;
2632 vtn_emit_make_visible_barrier(b
, src_access
, src_scope
, src
->pointer
->mode
);
2634 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
,
2635 spv_access_to_gl_access(dest_access
),
2636 spv_access_to_gl_access(src_access
));
2638 vtn_emit_make_available_barrier(b
, dest_access
, dest_scope
, dest
->pointer
->mode
);
2643 struct vtn_type
*res_type
= vtn_get_type(b
, w
[1]);
2644 struct vtn_value
*src_val
= vtn_value(b
, w
[3], vtn_value_type_pointer
);
2645 struct vtn_pointer
*src
= src_val
->pointer
;
2647 vtn_assert_types_equal(b
, opcode
, res_type
, src_val
->type
->deref
);
2649 unsigned idx
= 4, alignment
;
2650 SpvMemoryAccessMask access
;
2652 vtn_get_mem_operands(b
, w
, count
, &idx
, &access
, &alignment
, NULL
, &scope
);
2654 vtn_emit_make_visible_barrier(b
, access
, scope
, src
->mode
);
2656 vtn_push_ssa_value(b
, w
[2], vtn_variable_load(b
, src
, spv_access_to_gl_access(access
)));
2661 struct vtn_value
*dest_val
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2662 struct vtn_pointer
*dest
= dest_val
->pointer
;
2663 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[2]);
2665 /* OpStore requires us to actually have a storage type */
2666 vtn_fail_if(dest
->type
->type
== NULL
,
2667 "Invalid destination type for OpStore");
2669 if (glsl_get_base_type(dest
->type
->type
) == GLSL_TYPE_BOOL
&&
2670 glsl_get_base_type(src_val
->type
->type
) == GLSL_TYPE_UINT
) {
2671 /* Early versions of GLSLang would use uint types for UBOs/SSBOs but
2672 * would then store them to a local variable as bool. Work around
2673 * the issue by doing an implicit conversion.
2675 * https://github.com/KhronosGroup/glslang/issues/170
2676 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2678 vtn_warn("OpStore of value of type OpTypeInt to a pointer to type "
2679 "OpTypeBool. Doing an implicit conversion to work around "
2681 struct vtn_ssa_value
*bool_ssa
=
2682 vtn_create_ssa_value(b
, dest
->type
->type
);
2683 bool_ssa
->def
= nir_i2b(&b
->nb
, vtn_ssa_value(b
, w
[2])->def
);
2684 vtn_variable_store(b
, bool_ssa
, dest
, 0);
2688 vtn_assert_types_equal(b
, opcode
, dest_val
->type
->deref
, src_val
->type
);
2690 unsigned idx
= 3, alignment
;
2691 SpvMemoryAccessMask access
;
2693 vtn_get_mem_operands(b
, w
, count
, &idx
, &access
, &alignment
, &scope
, NULL
);
2695 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
2696 vtn_variable_store(b
, src
, dest
, spv_access_to_gl_access(access
));
2698 vtn_emit_make_available_barrier(b
, access
, scope
, dest
->mode
);
2702 case SpvOpArrayLength
: {
2703 struct vtn_pointer
*ptr
=
2704 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2705 const uint32_t field
= w
[4];
2707 vtn_fail_if(ptr
->type
->base_type
!= vtn_base_type_struct
,
2708 "OpArrayLength must take a pointer to a structure type");
2709 vtn_fail_if(field
!= ptr
->type
->length
- 1 ||
2710 ptr
->type
->members
[field
]->base_type
!= vtn_base_type_array
,
2711 "OpArrayLength must reference the last memeber of the "
2712 "structure and that must be an array");
2714 const uint32_t offset
= ptr
->type
->offsets
[field
];
2715 const uint32_t stride
= ptr
->type
->members
[field
]->stride
;
2717 if (!ptr
->block_index
) {
2718 struct vtn_access_chain chain
= {
2721 ptr
= vtn_pointer_dereference(b
, ptr
, &chain
);
2722 vtn_assert(ptr
->block_index
);
2725 nir_intrinsic_instr
*instr
=
2726 nir_intrinsic_instr_create(b
->nb
.shader
,
2727 nir_intrinsic_get_buffer_size
);
2728 instr
->src
[0] = nir_src_for_ssa(ptr
->block_index
);
2729 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
2730 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2731 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
2733 /* array_length = max(buffer_size - offset, 0) / stride */
2734 nir_ssa_def
*array_length
=
2739 nir_imm_int(&b
->nb
, offset
)),
2740 nir_imm_int(&b
->nb
, 0u)),
2741 nir_imm_int(&b
->nb
, stride
));
2743 vtn_push_nir_ssa(b
, w
[2], array_length
);
2747 case SpvOpConvertPtrToU
: {
2748 struct vtn_type
*u_type
= vtn_get_type(b
, w
[1]);
2749 struct vtn_type
*ptr_type
= vtn_get_value_type(b
, w
[3]);
2751 vtn_fail_if(ptr_type
->base_type
!= vtn_base_type_pointer
||
2752 ptr_type
->type
== NULL
,
2753 "OpConvertPtrToU can only be used on physical pointers");
2755 vtn_fail_if(u_type
->base_type
!= vtn_base_type_vector
&&
2756 u_type
->base_type
!= vtn_base_type_scalar
,
2757 "OpConvertPtrToU can only be used to cast to a vector or "
2760 /* The pointer will be converted to an SSA value automatically */
2761 nir_ssa_def
*ptr
= vtn_get_nir_ssa(b
, w
[3]);
2762 nir_ssa_def
*u
= nir_sloppy_bitcast(&b
->nb
, ptr
, u_type
->type
);
2763 vtn_push_nir_ssa(b
, w
[2], u
);
2767 case SpvOpConvertUToPtr
: {
2768 struct vtn_type
*ptr_type
= vtn_get_type(b
, w
[1]);
2769 struct vtn_type
*u_type
= vtn_get_value_type(b
, w
[3]);
2771 vtn_fail_if(ptr_type
->base_type
!= vtn_base_type_pointer
||
2772 ptr_type
->type
== NULL
,
2773 "OpConvertUToPtr can only be used on physical pointers");
2775 vtn_fail_if(u_type
->base_type
!= vtn_base_type_vector
&&
2776 u_type
->base_type
!= vtn_base_type_scalar
,
2777 "OpConvertUToPtr can only be used to cast from a vector or "
2780 nir_ssa_def
*u
= vtn_get_nir_ssa(b
, w
[3]);
2781 nir_ssa_def
*ptr
= nir_sloppy_bitcast(&b
->nb
, u
, ptr_type
->type
);
2782 vtn_push_pointer(b
, w
[2], vtn_pointer_from_ssa(b
, ptr
, ptr_type
));
2786 case SpvOpCopyMemorySized
:
2788 vtn_fail_with_opcode("Unhandled opcode", opcode
);