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_pointer_is_external_block(struct vtn_builder
*b
,
129 struct vtn_pointer
*ptr
)
131 return ptr
->mode
== vtn_variable_mode_ssbo
||
132 ptr
->mode
== vtn_variable_mode_ubo
||
133 ptr
->mode
== vtn_variable_mode_phys_ssbo
||
134 ptr
->mode
== vtn_variable_mode_push_constant
;
138 vtn_access_link_as_ssa(struct vtn_builder
*b
, struct vtn_access_link link
,
139 unsigned stride
, unsigned bit_size
)
141 vtn_assert(stride
> 0);
142 if (link
.mode
== vtn_access_mode_literal
) {
143 return nir_imm_intN_t(&b
->nb
, link
.id
* stride
, bit_size
);
145 nir_ssa_def
*ssa
= vtn_ssa_value(b
, link
.id
)->def
;
146 if (ssa
->bit_size
!= bit_size
)
147 ssa
= nir_i2i(&b
->nb
, ssa
, bit_size
);
148 return nir_imul_imm(&b
->nb
, ssa
, stride
);
152 static VkDescriptorType
153 vk_desc_type_for_mode(struct vtn_builder
*b
, enum vtn_variable_mode mode
)
156 case vtn_variable_mode_ubo
:
157 return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
;
158 case vtn_variable_mode_ssbo
:
159 return VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
;
161 vtn_fail("Invalid mode for vulkan_resource_index");
166 vtn_variable_resource_index(struct vtn_builder
*b
, struct vtn_variable
*var
,
167 nir_ssa_def
*desc_array_index
)
169 vtn_assert(b
->options
->environment
== NIR_SPIRV_VULKAN
);
171 if (!desc_array_index
) {
172 vtn_assert(glsl_type_is_struct_or_ifc(var
->type
->type
));
173 desc_array_index
= nir_imm_int(&b
->nb
, 0);
176 nir_intrinsic_instr
*instr
=
177 nir_intrinsic_instr_create(b
->nb
.shader
,
178 nir_intrinsic_vulkan_resource_index
);
179 instr
->src
[0] = nir_src_for_ssa(desc_array_index
);
180 nir_intrinsic_set_desc_set(instr
, var
->descriptor_set
);
181 nir_intrinsic_set_binding(instr
, var
->binding
);
182 nir_intrinsic_set_desc_type(instr
, vk_desc_type_for_mode(b
, var
->mode
));
184 vtn_fail_if(var
->mode
!= vtn_variable_mode_ubo
&&
185 var
->mode
!= vtn_variable_mode_ssbo
,
186 "Invalid mode for vulkan_resource_index");
188 nir_address_format addr_format
= vtn_mode_to_address_format(b
, var
->mode
);
189 const struct glsl_type
*index_type
=
190 b
->options
->lower_ubo_ssbo_access_to_offsets
?
191 glsl_uint_type() : nir_address_format_to_glsl_type(addr_format
);
193 instr
->num_components
= glsl_get_vector_elements(index_type
);
194 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, instr
->num_components
,
195 glsl_get_bit_size(index_type
), NULL
);
196 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
198 return &instr
->dest
.ssa
;
202 vtn_resource_reindex(struct vtn_builder
*b
, enum vtn_variable_mode mode
,
203 nir_ssa_def
*base_index
, nir_ssa_def
*offset_index
)
205 vtn_assert(b
->options
->environment
== NIR_SPIRV_VULKAN
);
207 nir_intrinsic_instr
*instr
=
208 nir_intrinsic_instr_create(b
->nb
.shader
,
209 nir_intrinsic_vulkan_resource_reindex
);
210 instr
->src
[0] = nir_src_for_ssa(base_index
);
211 instr
->src
[1] = nir_src_for_ssa(offset_index
);
212 nir_intrinsic_set_desc_type(instr
, vk_desc_type_for_mode(b
, mode
));
214 vtn_fail_if(mode
!= vtn_variable_mode_ubo
&& mode
!= vtn_variable_mode_ssbo
,
215 "Invalid mode for vulkan_resource_reindex");
217 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
218 const struct glsl_type
*index_type
=
219 b
->options
->lower_ubo_ssbo_access_to_offsets
?
220 glsl_uint_type() : nir_address_format_to_glsl_type(addr_format
);
222 instr
->num_components
= glsl_get_vector_elements(index_type
);
223 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, instr
->num_components
,
224 glsl_get_bit_size(index_type
), NULL
);
225 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
227 return &instr
->dest
.ssa
;
231 vtn_descriptor_load(struct vtn_builder
*b
, enum vtn_variable_mode mode
,
232 nir_ssa_def
*desc_index
)
234 vtn_assert(b
->options
->environment
== NIR_SPIRV_VULKAN
);
236 nir_intrinsic_instr
*desc_load
=
237 nir_intrinsic_instr_create(b
->nb
.shader
,
238 nir_intrinsic_load_vulkan_descriptor
);
239 desc_load
->src
[0] = nir_src_for_ssa(desc_index
);
240 nir_intrinsic_set_desc_type(desc_load
, vk_desc_type_for_mode(b
, mode
));
242 vtn_fail_if(mode
!= vtn_variable_mode_ubo
&& mode
!= vtn_variable_mode_ssbo
,
243 "Invalid mode for load_vulkan_descriptor");
245 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
246 const struct glsl_type
*ptr_type
=
247 nir_address_format_to_glsl_type(addr_format
);
249 desc_load
->num_components
= glsl_get_vector_elements(ptr_type
);
250 nir_ssa_dest_init(&desc_load
->instr
, &desc_load
->dest
,
251 desc_load
->num_components
,
252 glsl_get_bit_size(ptr_type
), NULL
);
253 nir_builder_instr_insert(&b
->nb
, &desc_load
->instr
);
255 return &desc_load
->dest
.ssa
;
258 /* Dereference the given base pointer by the access chain */
259 static struct vtn_pointer
*
260 vtn_nir_deref_pointer_dereference(struct vtn_builder
*b
,
261 struct vtn_pointer
*base
,
262 struct vtn_access_chain
*deref_chain
)
264 struct vtn_type
*type
= base
->type
;
265 enum gl_access_qualifier access
= base
->access
| deref_chain
->access
;
268 nir_deref_instr
*tail
;
271 } else if (b
->options
->environment
== NIR_SPIRV_VULKAN
&&
272 vtn_pointer_is_external_block(b
, base
)) {
273 nir_ssa_def
*block_index
= base
->block_index
;
275 /* We dereferencing an external block pointer. Correctness of this
276 * operation relies on one particular line in the SPIR-V spec, section
277 * entitled "Validation Rules for Shader Capabilities":
279 * "Block and BufferBlock decorations cannot decorate a structure
280 * type that is nested at any level inside another structure type
281 * decorated with Block or BufferBlock."
283 * This means that we can detect the point where we cross over from
284 * descriptor indexing to buffer indexing by looking for the block
285 * decorated struct type. Anything before the block decorated struct
286 * type is a descriptor indexing operation and anything after the block
287 * decorated struct is a buffer offset operation.
290 /* Figure out the descriptor array index if any
292 * Some of the Vulkan CTS tests with hand-rolled SPIR-V have been known
293 * to forget the Block or BufferBlock decoration from time to time.
294 * It's more robust if we check for both !block_index and for the type
295 * to contain a block. This way there's a decent chance that arrays of
296 * UBOs/SSBOs will work correctly even if variable pointers are
299 nir_ssa_def
*desc_arr_idx
= NULL
;
300 if (!block_index
|| vtn_type_contains_block(b
, type
)) {
301 /* If our type contains a block, then we're still outside the block
302 * and we need to process enough levels of dereferences to get inside
305 if (deref_chain
->ptr_as_array
) {
306 unsigned aoa_size
= glsl_get_aoa_size(type
->type
);
307 desc_arr_idx
= vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
308 MAX2(aoa_size
, 1), 32);
312 for (; idx
< deref_chain
->length
; idx
++) {
313 if (type
->base_type
!= vtn_base_type_array
) {
314 vtn_assert(type
->base_type
== vtn_base_type_struct
);
318 unsigned aoa_size
= glsl_get_aoa_size(type
->array_element
->type
);
319 nir_ssa_def
*arr_offset
=
320 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
321 MAX2(aoa_size
, 1), 32);
323 desc_arr_idx
= nir_iadd(&b
->nb
, desc_arr_idx
, arr_offset
);
325 desc_arr_idx
= arr_offset
;
327 type
= type
->array_element
;
328 access
|= type
->access
;
333 vtn_assert(base
->var
&& base
->type
);
334 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
335 } else if (desc_arr_idx
) {
336 block_index
= vtn_resource_reindex(b
, base
->mode
,
337 block_index
, desc_arr_idx
);
340 if (idx
== deref_chain
->length
) {
341 /* The entire deref was consumed in finding the block index. Return
342 * a pointer which just has a block index and a later access chain
343 * will dereference deeper.
345 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
346 ptr
->mode
= base
->mode
;
348 ptr
->block_index
= block_index
;
349 ptr
->access
= access
;
353 /* If we got here, there's more access chain to handle and we have the
354 * final block index. Insert a descriptor load and cast to a deref to
355 * start the deref chain.
357 nir_ssa_def
*desc
= vtn_descriptor_load(b
, base
->mode
, block_index
);
359 assert(base
->mode
== vtn_variable_mode_ssbo
||
360 base
->mode
== vtn_variable_mode_ubo
);
361 nir_variable_mode nir_mode
=
362 base
->mode
== vtn_variable_mode_ssbo
? nir_var_mem_ssbo
: nir_var_mem_ubo
;
364 tail
= nir_build_deref_cast(&b
->nb
, desc
, nir_mode
,
365 vtn_type_get_nir_type(b
, type
, base
->mode
),
366 base
->ptr_type
->stride
);
368 assert(base
->var
&& base
->var
->var
);
369 tail
= nir_build_deref_var(&b
->nb
, base
->var
->var
);
370 if (base
->ptr_type
&& base
->ptr_type
->type
) {
371 tail
->dest
.ssa
.num_components
=
372 glsl_get_vector_elements(base
->ptr_type
->type
);
373 tail
->dest
.ssa
.bit_size
= glsl_get_bit_size(base
->ptr_type
->type
);
377 if (idx
== 0 && deref_chain
->ptr_as_array
) {
378 /* We start with a deref cast to get the stride. Hopefully, we'll be
379 * able to delete that cast eventually.
381 tail
= nir_build_deref_cast(&b
->nb
, &tail
->dest
.ssa
, tail
->mode
,
382 tail
->type
, base
->ptr_type
->stride
);
384 nir_ssa_def
*index
= vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1,
385 tail
->dest
.ssa
.bit_size
);
386 tail
= nir_build_deref_ptr_as_array(&b
->nb
, tail
, index
);
390 for (; idx
< deref_chain
->length
; idx
++) {
391 if (glsl_type_is_struct_or_ifc(type
->type
)) {
392 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
393 unsigned field
= deref_chain
->link
[idx
].id
;
394 tail
= nir_build_deref_struct(&b
->nb
, tail
, field
);
395 type
= type
->members
[field
];
397 nir_ssa_def
*arr_index
=
398 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
], 1,
399 tail
->dest
.ssa
.bit_size
);
400 tail
= nir_build_deref_array(&b
->nb
, tail
, arr_index
);
401 type
= type
->array_element
;
404 access
|= type
->access
;
407 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
408 ptr
->mode
= base
->mode
;
410 ptr
->var
= base
->var
;
412 ptr
->access
= access
;
417 static struct vtn_pointer
*
418 vtn_ssa_offset_pointer_dereference(struct vtn_builder
*b
,
419 struct vtn_pointer
*base
,
420 struct vtn_access_chain
*deref_chain
)
422 nir_ssa_def
*block_index
= base
->block_index
;
423 nir_ssa_def
*offset
= base
->offset
;
424 struct vtn_type
*type
= base
->type
;
425 enum gl_access_qualifier access
= base
->access
;
428 if (base
->mode
== vtn_variable_mode_ubo
||
429 base
->mode
== vtn_variable_mode_ssbo
) {
431 vtn_assert(base
->var
&& base
->type
);
432 nir_ssa_def
*desc_arr_idx
;
433 if (glsl_type_is_array(type
->type
)) {
434 if (deref_chain
->length
>= 1) {
436 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
438 /* This consumes a level of type */
439 type
= type
->array_element
;
440 access
|= type
->access
;
442 /* This is annoying. We've been asked for a pointer to the
443 * array of UBOs/SSBOs and not a specifc buffer. Return a
444 * pointer with a descriptor index of 0 and we'll have to do
445 * a reindex later to adjust it to the right thing.
447 desc_arr_idx
= nir_imm_int(&b
->nb
, 0);
449 } else if (deref_chain
->ptr_as_array
) {
450 /* You can't have a zero-length OpPtrAccessChain */
451 vtn_assert(deref_chain
->length
>= 1);
452 desc_arr_idx
= vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
454 /* We have a regular non-array SSBO. */
457 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
458 } else if (deref_chain
->ptr_as_array
&&
459 type
->base_type
== vtn_base_type_struct
&& type
->block
) {
460 /* We are doing an OpPtrAccessChain on a pointer to a struct that is
461 * decorated block. This is an interesting corner in the SPIR-V
462 * spec. One interpretation would be that they client is clearly
463 * trying to treat that block as if it's an implicit array of blocks
464 * repeated in the buffer. However, the SPIR-V spec for the
465 * OpPtrAccessChain says:
467 * "Base is treated as the address of the first element of an
468 * array, and the Element element’s address is computed to be the
469 * base for the Indexes, as per OpAccessChain."
471 * Taken literally, that would mean that your struct type is supposed
472 * to be treated as an array of such a struct and, since it's
473 * decorated block, that means an array of blocks which corresponds
474 * to an array descriptor. Therefore, we need to do a reindex
475 * operation to add the index from the first link in the access chain
476 * to the index we recieved.
478 * The downside to this interpretation (there always is one) is that
479 * this might be somewhat surprising behavior to apps if they expect
480 * the implicit array behavior described above.
482 vtn_assert(deref_chain
->length
>= 1);
483 nir_ssa_def
*offset_index
=
484 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
487 block_index
= vtn_resource_reindex(b
, base
->mode
,
488 block_index
, offset_index
);
493 if (base
->mode
== vtn_variable_mode_workgroup
) {
494 /* SLM doesn't need nor have a block index */
495 vtn_assert(!block_index
);
497 /* We need the variable for the base offset */
498 vtn_assert(base
->var
);
500 /* We need ptr_type for size and alignment */
501 vtn_assert(base
->ptr_type
);
503 /* Assign location on first use so that we don't end up bloating SLM
504 * address space for variables which are never statically used.
506 if (base
->var
->shared_location
< 0) {
507 vtn_assert(base
->ptr_type
->length
> 0 && base
->ptr_type
->align
> 0);
508 b
->shader
->num_shared
= vtn_align_u32(b
->shader
->num_shared
,
509 base
->ptr_type
->align
);
510 base
->var
->shared_location
= b
->shader
->num_shared
;
511 b
->shader
->num_shared
+= base
->ptr_type
->length
;
514 offset
= nir_imm_int(&b
->nb
, base
->var
->shared_location
);
515 } else if (base
->mode
== vtn_variable_mode_push_constant
) {
516 /* Push constants neither need nor have a block index */
517 vtn_assert(!block_index
);
519 /* Start off with at the start of the push constant block. */
520 offset
= nir_imm_int(&b
->nb
, 0);
522 /* The code above should have ensured a block_index when needed. */
523 vtn_assert(block_index
);
525 /* Start off with at the start of the buffer. */
526 offset
= nir_imm_int(&b
->nb
, 0);
530 if (deref_chain
->ptr_as_array
&& idx
== 0) {
531 /* We need ptr_type for the stride */
532 vtn_assert(base
->ptr_type
);
534 /* We need at least one element in the chain */
535 vtn_assert(deref_chain
->length
>= 1);
537 nir_ssa_def
*elem_offset
=
538 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
539 base
->ptr_type
->stride
, offset
->bit_size
);
540 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
544 for (; idx
< deref_chain
->length
; idx
++) {
545 switch (glsl_get_base_type(type
->type
)) {
548 case GLSL_TYPE_UINT16
:
549 case GLSL_TYPE_INT16
:
550 case GLSL_TYPE_UINT8
:
552 case GLSL_TYPE_UINT64
:
553 case GLSL_TYPE_INT64
:
554 case GLSL_TYPE_FLOAT
:
555 case GLSL_TYPE_FLOAT16
:
556 case GLSL_TYPE_DOUBLE
:
558 case GLSL_TYPE_ARRAY
: {
559 nir_ssa_def
*elem_offset
=
560 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
561 type
->stride
, offset
->bit_size
);
562 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
563 type
= type
->array_element
;
564 access
|= type
->access
;
568 case GLSL_TYPE_INTERFACE
:
569 case GLSL_TYPE_STRUCT
: {
570 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
571 unsigned member
= deref_chain
->link
[idx
].id
;
572 offset
= nir_iadd_imm(&b
->nb
, offset
, type
->offsets
[member
]);
573 type
= type
->members
[member
];
574 access
|= type
->access
;
579 vtn_fail("Invalid type for deref");
583 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
584 ptr
->mode
= base
->mode
;
586 ptr
->block_index
= block_index
;
587 ptr
->offset
= offset
;
588 ptr
->access
= access
;
593 /* Dereference the given base pointer by the access chain */
594 static struct vtn_pointer
*
595 vtn_pointer_dereference(struct vtn_builder
*b
,
596 struct vtn_pointer
*base
,
597 struct vtn_access_chain
*deref_chain
)
599 if (vtn_pointer_uses_ssa_offset(b
, base
)) {
600 return vtn_ssa_offset_pointer_dereference(b
, base
, deref_chain
);
602 return vtn_nir_deref_pointer_dereference(b
, base
, deref_chain
);
607 vtn_pointer_to_deref(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
609 if (b
->wa_glslang_179
) {
610 /* Do on-the-fly copy propagation for samplers. */
611 if (ptr
->var
&& ptr
->var
->copy_prop_sampler
)
612 return vtn_pointer_to_deref(b
, ptr
->var
->copy_prop_sampler
);
615 vtn_assert(!vtn_pointer_uses_ssa_offset(b
, ptr
));
617 struct vtn_access_chain chain
= {
620 ptr
= vtn_nir_deref_pointer_dereference(b
, ptr
, &chain
);
627 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_instr
*deref
,
628 struct vtn_ssa_value
*inout
,
629 enum gl_access_qualifier access
)
631 if (glsl_type_is_vector_or_scalar(deref
->type
)) {
633 inout
->def
= nir_load_deref_with_access(&b
->nb
, deref
, access
);
635 nir_store_deref_with_access(&b
->nb
, deref
, inout
->def
, ~0, access
);
637 } else if (glsl_type_is_array(deref
->type
) ||
638 glsl_type_is_matrix(deref
->type
)) {
639 unsigned elems
= glsl_get_length(deref
->type
);
640 for (unsigned i
= 0; i
< elems
; i
++) {
641 nir_deref_instr
*child
=
642 nir_build_deref_array_imm(&b
->nb
, deref
, i
);
643 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
], access
);
646 vtn_assert(glsl_type_is_struct_or_ifc(deref
->type
));
647 unsigned elems
= glsl_get_length(deref
->type
);
648 for (unsigned i
= 0; i
< elems
; i
++) {
649 nir_deref_instr
*child
= nir_build_deref_struct(&b
->nb
, deref
, i
);
650 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
], access
);
656 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
658 struct vtn_pointer
*ptr
= vtn_value(b
, id
, vtn_value_type_pointer
)->pointer
;
659 return vtn_pointer_to_deref(b
, ptr
);
663 * Gets the NIR-level deref tail, which may have as a child an array deref
664 * selecting which component due to OpAccessChain supporting per-component
665 * indexing in SPIR-V.
667 static nir_deref_instr
*
668 get_deref_tail(nir_deref_instr
*deref
)
670 if (deref
->deref_type
!= nir_deref_type_array
)
673 nir_deref_instr
*parent
=
674 nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
);
676 if (glsl_type_is_vector(parent
->type
))
682 struct vtn_ssa_value
*
683 vtn_local_load(struct vtn_builder
*b
, nir_deref_instr
*src
,
684 enum gl_access_qualifier access
)
686 nir_deref_instr
*src_tail
= get_deref_tail(src
);
687 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
688 _vtn_local_load_store(b
, true, src_tail
, val
, access
);
690 if (src_tail
!= src
) {
691 val
->type
= src
->type
;
692 val
->def
= nir_vector_extract(&b
->nb
, val
->def
, src
->arr
.index
.ssa
);
699 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
700 nir_deref_instr
*dest
, enum gl_access_qualifier access
)
702 nir_deref_instr
*dest_tail
= get_deref_tail(dest
);
704 if (dest_tail
!= dest
) {
705 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
706 _vtn_local_load_store(b
, true, dest_tail
, val
, access
);
708 val
->def
= nir_vector_insert(&b
->nb
, val
->def
, src
->def
,
709 dest
->arr
.index
.ssa
);
710 _vtn_local_load_store(b
, false, dest_tail
, val
, access
);
712 _vtn_local_load_store(b
, false, dest_tail
, src
, access
);
717 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
718 nir_ssa_def
**index_out
)
720 assert(vtn_pointer_uses_ssa_offset(b
, ptr
));
722 struct vtn_access_chain chain
= {
725 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
727 *index_out
= ptr
->block_index
;
731 /* Tries to compute the size of an interface block based on the strides and
732 * offsets that are provided to us in the SPIR-V source.
735 vtn_type_block_size(struct vtn_builder
*b
, struct vtn_type
*type
)
737 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
741 case GLSL_TYPE_UINT16
:
742 case GLSL_TYPE_INT16
:
743 case GLSL_TYPE_UINT8
:
745 case GLSL_TYPE_UINT64
:
746 case GLSL_TYPE_INT64
:
747 case GLSL_TYPE_FLOAT
:
748 case GLSL_TYPE_FLOAT16
:
750 case GLSL_TYPE_DOUBLE
: {
751 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
752 glsl_get_matrix_columns(type
->type
);
754 vtn_assert(type
->stride
> 0);
755 return type
->stride
* cols
;
757 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
758 return glsl_get_vector_elements(type
->type
) * type_size
;
762 case GLSL_TYPE_STRUCT
:
763 case GLSL_TYPE_INTERFACE
: {
765 unsigned num_fields
= glsl_get_length(type
->type
);
766 for (unsigned f
= 0; f
< num_fields
; f
++) {
767 unsigned field_end
= type
->offsets
[f
] +
768 vtn_type_block_size(b
, type
->members
[f
]);
769 size
= MAX2(size
, field_end
);
774 case GLSL_TYPE_ARRAY
:
775 vtn_assert(type
->stride
> 0);
776 vtn_assert(glsl_get_length(type
->type
) > 0);
777 return type
->stride
* glsl_get_length(type
->type
);
780 vtn_fail("Invalid block type");
786 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
787 nir_ssa_def
*index
, nir_ssa_def
*offset
,
788 unsigned access_offset
, unsigned access_size
,
789 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
,
790 enum gl_access_qualifier access
)
792 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
793 instr
->num_components
= glsl_get_vector_elements(type
);
795 /* Booleans usually shouldn't show up in external memory in SPIR-V.
796 * However, they do for certain older GLSLang versions and can for shared
797 * memory when we lower access chains internally.
799 const unsigned data_bit_size
= glsl_type_is_boolean(type
) ? 32 :
800 glsl_get_bit_size(type
);
804 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
805 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
808 if (op
== nir_intrinsic_load_push_constant
) {
809 nir_intrinsic_set_base(instr
, access_offset
);
810 nir_intrinsic_set_range(instr
, access_size
);
813 if (op
== nir_intrinsic_load_ubo
||
814 op
== nir_intrinsic_load_ssbo
||
815 op
== nir_intrinsic_store_ssbo
) {
816 nir_intrinsic_set_access(instr
, access
);
819 /* With extensions like relaxed_block_layout, we really can't guarantee
820 * much more than scalar alignment.
822 if (op
!= nir_intrinsic_load_push_constant
)
823 nir_intrinsic_set_align(instr
, data_bit_size
/ 8, 0);
826 instr
->src
[src
++] = nir_src_for_ssa(index
);
828 if (op
== nir_intrinsic_load_push_constant
) {
829 /* We need to subtract the offset from where the intrinsic will load the
832 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
833 nir_imm_int(&b
->nb
, access_offset
)));
835 instr
->src
[src
++] = nir_src_for_ssa(offset
);
839 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
840 instr
->num_components
, data_bit_size
, NULL
);
841 (*inout
)->def
= &instr
->dest
.ssa
;
844 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
846 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
847 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
851 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
852 nir_ssa_def
*index
, nir_ssa_def
*offset
,
853 unsigned access_offset
, unsigned access_size
,
854 struct vtn_type
*type
, enum gl_access_qualifier access
,
855 struct vtn_ssa_value
**inout
)
857 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
861 case GLSL_TYPE_UINT16
:
862 case GLSL_TYPE_INT16
:
863 case GLSL_TYPE_UINT8
:
865 case GLSL_TYPE_UINT64
:
866 case GLSL_TYPE_INT64
:
867 case GLSL_TYPE_FLOAT
:
868 case GLSL_TYPE_FLOAT16
:
869 case GLSL_TYPE_DOUBLE
:
871 /* This is where things get interesting. At this point, we've hit
872 * a vector, a scalar, or a matrix.
874 if (glsl_type_is_matrix(type
->type
)) {
875 /* Loading the whole matrix */
876 struct vtn_ssa_value
*transpose
;
877 unsigned num_ops
, vec_width
, col_stride
;
878 if (type
->row_major
) {
879 num_ops
= glsl_get_vector_elements(type
->type
);
880 vec_width
= glsl_get_matrix_columns(type
->type
);
881 col_stride
= type
->array_element
->stride
;
883 const struct glsl_type
*transpose_type
=
884 glsl_matrix_type(base_type
, vec_width
, num_ops
);
885 *inout
= vtn_create_ssa_value(b
, transpose_type
);
887 transpose
= vtn_ssa_transpose(b
, *inout
);
891 num_ops
= glsl_get_matrix_columns(type
->type
);
892 vec_width
= glsl_get_vector_elements(type
->type
);
893 col_stride
= type
->stride
;
896 for (unsigned i
= 0; i
< num_ops
; i
++) {
897 nir_ssa_def
*elem_offset
=
898 nir_iadd_imm(&b
->nb
, offset
, i
* col_stride
);
899 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
900 access_offset
, access_size
,
902 glsl_vector_type(base_type
, vec_width
),
903 type
->access
| access
);
906 if (load
&& type
->row_major
)
907 *inout
= vtn_ssa_transpose(b
, *inout
);
909 unsigned elems
= glsl_get_vector_elements(type
->type
);
910 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
911 if (elems
== 1 || type
->stride
== type_size
) {
912 /* This is a tightly-packed normal scalar or vector load */
913 vtn_assert(glsl_type_is_vector_or_scalar(type
->type
));
914 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
915 access_offset
, access_size
,
917 type
->access
| access
);
919 /* This is a strided load. We have to load N things separately.
920 * This is the single column of a row-major matrix case.
922 vtn_assert(type
->stride
> type_size
);
923 vtn_assert(type
->stride
% type_size
== 0);
925 nir_ssa_def
*per_comp
[4];
926 for (unsigned i
= 0; i
< elems
; i
++) {
927 nir_ssa_def
*elem_offset
=
928 nir_iadd_imm(&b
->nb
, offset
, i
* type
->stride
);
929 struct vtn_ssa_value
*comp
, temp_val
;
931 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
932 temp_val
.type
= glsl_scalar_type(base_type
);
935 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
936 access_offset
, access_size
,
937 &comp
, glsl_scalar_type(base_type
),
938 type
->access
| access
);
939 per_comp
[i
] = comp
->def
;
944 *inout
= vtn_create_ssa_value(b
, type
->type
);
945 (*inout
)->def
= nir_vec(&b
->nb
, per_comp
, elems
);
951 case GLSL_TYPE_ARRAY
: {
952 unsigned elems
= glsl_get_length(type
->type
);
953 for (unsigned i
= 0; i
< elems
; i
++) {
954 nir_ssa_def
*elem_off
=
955 nir_iadd_imm(&b
->nb
, offset
, i
* type
->stride
);
956 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
957 access_offset
, access_size
,
959 type
->array_element
->access
| access
,
960 &(*inout
)->elems
[i
]);
965 case GLSL_TYPE_INTERFACE
:
966 case GLSL_TYPE_STRUCT
: {
967 unsigned elems
= glsl_get_length(type
->type
);
968 for (unsigned i
= 0; i
< elems
; i
++) {
969 nir_ssa_def
*elem_off
=
970 nir_iadd_imm(&b
->nb
, offset
, type
->offsets
[i
]);
971 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
972 access_offset
, access_size
,
974 type
->members
[i
]->access
| access
,
975 &(*inout
)->elems
[i
]);
981 vtn_fail("Invalid block member type");
985 static struct vtn_ssa_value
*
986 vtn_block_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
989 unsigned access_offset
= 0, access_size
= 0;
991 case vtn_variable_mode_ubo
:
992 op
= nir_intrinsic_load_ubo
;
994 case vtn_variable_mode_ssbo
:
995 op
= nir_intrinsic_load_ssbo
;
997 case vtn_variable_mode_push_constant
:
998 op
= nir_intrinsic_load_push_constant
;
999 access_size
= b
->shader
->num_uniforms
;
1001 case vtn_variable_mode_workgroup
:
1002 op
= nir_intrinsic_load_shared
;
1005 vtn_fail("Invalid block variable mode");
1008 nir_ssa_def
*offset
, *index
= NULL
;
1009 offset
= vtn_pointer_to_offset(b
, src
, &index
);
1011 struct vtn_ssa_value
*value
= vtn_create_ssa_value(b
, src
->type
->type
);
1012 _vtn_block_load_store(b
, op
, true, index
, offset
,
1013 access_offset
, access_size
,
1014 src
->type
, src
->access
, &value
);
1019 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
1020 struct vtn_pointer
*dst
)
1022 nir_intrinsic_op op
;
1023 switch (dst
->mode
) {
1024 case vtn_variable_mode_ssbo
:
1025 op
= nir_intrinsic_store_ssbo
;
1027 case vtn_variable_mode_workgroup
:
1028 op
= nir_intrinsic_store_shared
;
1031 vtn_fail("Invalid block variable mode");
1034 nir_ssa_def
*offset
, *index
= NULL
;
1035 offset
= vtn_pointer_to_offset(b
, dst
, &index
);
1037 _vtn_block_load_store(b
, op
, false, index
, offset
,
1038 0, 0, dst
->type
, dst
->access
, &src
);
1042 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
1043 struct vtn_pointer
*ptr
,
1044 enum gl_access_qualifier access
,
1045 struct vtn_ssa_value
**inout
)
1047 enum glsl_base_type base_type
= glsl_get_base_type(ptr
->type
->type
);
1048 switch (base_type
) {
1049 case GLSL_TYPE_UINT
:
1051 case GLSL_TYPE_UINT16
:
1052 case GLSL_TYPE_INT16
:
1053 case GLSL_TYPE_UINT8
:
1054 case GLSL_TYPE_INT8
:
1055 case GLSL_TYPE_UINT64
:
1056 case GLSL_TYPE_INT64
:
1057 case GLSL_TYPE_FLOAT
:
1058 case GLSL_TYPE_FLOAT16
:
1059 case GLSL_TYPE_BOOL
:
1060 case GLSL_TYPE_DOUBLE
:
1061 if (glsl_type_is_vector_or_scalar(ptr
->type
->type
)) {
1062 /* We hit a vector or scalar; go ahead and emit the load[s] */
1063 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
1064 if (vtn_pointer_is_external_block(b
, ptr
)) {
1065 /* If it's external, we call nir_load/store_deref directly. The
1066 * vtn_local_load/store helpers are too clever and do magic to
1067 * avoid array derefs of vectors. That magic is both less
1068 * efficient than the direct load/store and, in the case of
1069 * stores, is broken because it creates a race condition if two
1070 * threads are writing to different components of the same vector
1071 * due to the load+insert+store it uses to emulate the array
1075 (*inout
)->def
= nir_load_deref_with_access(&b
->nb
, deref
,
1076 ptr
->type
->access
| access
);
1078 nir_store_deref_with_access(&b
->nb
, deref
, (*inout
)->def
, ~0,
1079 ptr
->type
->access
| access
);
1083 *inout
= vtn_local_load(b
, deref
, ptr
->type
->access
| access
);
1085 vtn_local_store(b
, *inout
, deref
, ptr
->type
->access
| access
);
1092 case GLSL_TYPE_INTERFACE
:
1093 case GLSL_TYPE_ARRAY
:
1094 case GLSL_TYPE_STRUCT
: {
1095 unsigned elems
= glsl_get_length(ptr
->type
->type
);
1096 struct vtn_access_chain chain
= {
1099 { .mode
= vtn_access_mode_literal
, },
1102 for (unsigned i
= 0; i
< elems
; i
++) {
1103 chain
.link
[0].id
= i
;
1104 struct vtn_pointer
*elem
= vtn_pointer_dereference(b
, ptr
, &chain
);
1105 _vtn_variable_load_store(b
, load
, elem
, ptr
->type
->access
| access
,
1106 &(*inout
)->elems
[i
]);
1112 vtn_fail("Invalid access chain type");
1116 struct vtn_ssa_value
*
1117 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
1119 if (vtn_pointer_uses_ssa_offset(b
, src
)) {
1120 return vtn_block_load(b
, src
);
1122 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src
->type
->type
);
1123 _vtn_variable_load_store(b
, true, src
, src
->access
, &val
);
1129 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
1130 struct vtn_pointer
*dest
)
1132 if (vtn_pointer_uses_ssa_offset(b
, dest
)) {
1133 vtn_assert(dest
->mode
== vtn_variable_mode_ssbo
||
1134 dest
->mode
== vtn_variable_mode_workgroup
);
1135 vtn_block_store(b
, src
, dest
);
1137 _vtn_variable_load_store(b
, false, dest
, dest
->access
, &src
);
1142 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
1143 struct vtn_pointer
*src
)
1145 vtn_assert(src
->type
->type
== dest
->type
->type
);
1146 enum glsl_base_type base_type
= glsl_get_base_type(src
->type
->type
);
1147 switch (base_type
) {
1148 case GLSL_TYPE_UINT
:
1150 case GLSL_TYPE_UINT16
:
1151 case GLSL_TYPE_INT16
:
1152 case GLSL_TYPE_UINT8
:
1153 case GLSL_TYPE_INT8
:
1154 case GLSL_TYPE_UINT64
:
1155 case GLSL_TYPE_INT64
:
1156 case GLSL_TYPE_FLOAT
:
1157 case GLSL_TYPE_FLOAT16
:
1158 case GLSL_TYPE_DOUBLE
:
1159 case GLSL_TYPE_BOOL
:
1160 /* At this point, we have a scalar, vector, or matrix so we know that
1161 * there cannot be any structure splitting still in the way. By
1162 * stopping at the matrix level rather than the vector level, we
1163 * ensure that matrices get loaded in the optimal way even if they
1164 * are storred row-major in a UBO.
1166 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
1169 case GLSL_TYPE_INTERFACE
:
1170 case GLSL_TYPE_ARRAY
:
1171 case GLSL_TYPE_STRUCT
: {
1172 struct vtn_access_chain chain
= {
1175 { .mode
= vtn_access_mode_literal
, },
1178 unsigned elems
= glsl_get_length(src
->type
->type
);
1179 for (unsigned i
= 0; i
< elems
; i
++) {
1180 chain
.link
[0].id
= i
;
1181 struct vtn_pointer
*src_elem
=
1182 vtn_pointer_dereference(b
, src
, &chain
);
1183 struct vtn_pointer
*dest_elem
=
1184 vtn_pointer_dereference(b
, dest
, &chain
);
1186 _vtn_variable_copy(b
, dest_elem
, src_elem
);
1192 vtn_fail("Invalid access chain type");
1197 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
1198 struct vtn_pointer
*src
)
1200 /* TODO: At some point, we should add a special-case for when we can
1201 * just emit a copy_var intrinsic.
1203 _vtn_variable_copy(b
, dest
, src
);
1207 set_mode_system_value(struct vtn_builder
*b
, nir_variable_mode
*mode
)
1209 vtn_assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
1210 *mode
= nir_var_system_value
;
1214 vtn_get_builtin_location(struct vtn_builder
*b
,
1215 SpvBuiltIn builtin
, int *location
,
1216 nir_variable_mode
*mode
)
1219 case SpvBuiltInPosition
:
1220 *location
= VARYING_SLOT_POS
;
1222 case SpvBuiltInPointSize
:
1223 *location
= VARYING_SLOT_PSIZ
;
1225 case SpvBuiltInClipDistance
:
1226 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
1228 case SpvBuiltInCullDistance
:
1229 *location
= VARYING_SLOT_CULL_DIST0
;
1231 case SpvBuiltInVertexId
:
1232 case SpvBuiltInVertexIndex
:
1233 /* The Vulkan spec defines VertexIndex to be non-zero-based and doesn't
1234 * allow VertexId. The ARB_gl_spirv spec defines VertexId to be the
1235 * same as gl_VertexID, which is non-zero-based, and removes
1236 * VertexIndex. Since they're both defined to be non-zero-based, we use
1237 * SYSTEM_VALUE_VERTEX_ID for both.
1239 *location
= SYSTEM_VALUE_VERTEX_ID
;
1240 set_mode_system_value(b
, mode
);
1242 case SpvBuiltInInstanceIndex
:
1243 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
1244 set_mode_system_value(b
, mode
);
1246 case SpvBuiltInInstanceId
:
1247 *location
= SYSTEM_VALUE_INSTANCE_ID
;
1248 set_mode_system_value(b
, mode
);
1250 case SpvBuiltInPrimitiveId
:
1251 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1252 vtn_assert(*mode
== nir_var_shader_in
);
1253 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1254 } else if (*mode
== nir_var_shader_out
) {
1255 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1257 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
1258 set_mode_system_value(b
, mode
);
1261 case SpvBuiltInInvocationId
:
1262 *location
= SYSTEM_VALUE_INVOCATION_ID
;
1263 set_mode_system_value(b
, mode
);
1265 case SpvBuiltInLayer
:
1266 *location
= VARYING_SLOT_LAYER
;
1267 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1268 *mode
= nir_var_shader_in
;
1269 else if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1270 *mode
= nir_var_shader_out
;
1271 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1272 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1273 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1274 *mode
= nir_var_shader_out
;
1276 vtn_fail("invalid stage for SpvBuiltInLayer");
1278 case SpvBuiltInViewportIndex
:
1279 *location
= VARYING_SLOT_VIEWPORT
;
1280 if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1281 *mode
= nir_var_shader_out
;
1282 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1283 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1284 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1285 *mode
= nir_var_shader_out
;
1286 else if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1287 *mode
= nir_var_shader_in
;
1289 vtn_fail("invalid stage for SpvBuiltInViewportIndex");
1291 case SpvBuiltInTessLevelOuter
:
1292 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
1294 case SpvBuiltInTessLevelInner
:
1295 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
1297 case SpvBuiltInTessCoord
:
1298 *location
= SYSTEM_VALUE_TESS_COORD
;
1299 set_mode_system_value(b
, mode
);
1301 case SpvBuiltInPatchVertices
:
1302 *location
= SYSTEM_VALUE_VERTICES_IN
;
1303 set_mode_system_value(b
, mode
);
1305 case SpvBuiltInFragCoord
:
1306 vtn_assert(*mode
== nir_var_shader_in
);
1307 if (b
->options
&& b
->options
->frag_coord_is_sysval
) {
1308 *mode
= nir_var_system_value
;
1309 *location
= SYSTEM_VALUE_FRAG_COORD
;
1311 *location
= VARYING_SLOT_POS
;
1314 case SpvBuiltInPointCoord
:
1315 *location
= VARYING_SLOT_PNTC
;
1316 vtn_assert(*mode
== nir_var_shader_in
);
1318 case SpvBuiltInFrontFacing
:
1319 *location
= SYSTEM_VALUE_FRONT_FACE
;
1320 set_mode_system_value(b
, mode
);
1322 case SpvBuiltInSampleId
:
1323 *location
= SYSTEM_VALUE_SAMPLE_ID
;
1324 set_mode_system_value(b
, mode
);
1326 case SpvBuiltInSamplePosition
:
1327 *location
= SYSTEM_VALUE_SAMPLE_POS
;
1328 set_mode_system_value(b
, mode
);
1330 case SpvBuiltInSampleMask
:
1331 if (*mode
== nir_var_shader_out
) {
1332 *location
= FRAG_RESULT_SAMPLE_MASK
;
1334 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1335 set_mode_system_value(b
, mode
);
1338 case SpvBuiltInFragDepth
:
1339 *location
= FRAG_RESULT_DEPTH
;
1340 vtn_assert(*mode
== nir_var_shader_out
);
1342 case SpvBuiltInHelperInvocation
:
1343 *location
= SYSTEM_VALUE_HELPER_INVOCATION
;
1344 set_mode_system_value(b
, mode
);
1346 case SpvBuiltInNumWorkgroups
:
1347 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1348 set_mode_system_value(b
, mode
);
1350 case SpvBuiltInWorkgroupSize
:
1351 *location
= SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
1352 set_mode_system_value(b
, mode
);
1354 case SpvBuiltInWorkgroupId
:
1355 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1356 set_mode_system_value(b
, mode
);
1358 case SpvBuiltInLocalInvocationId
:
1359 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1360 set_mode_system_value(b
, mode
);
1362 case SpvBuiltInLocalInvocationIndex
:
1363 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1364 set_mode_system_value(b
, mode
);
1366 case SpvBuiltInGlobalInvocationId
:
1367 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1368 set_mode_system_value(b
, mode
);
1370 case SpvBuiltInGlobalLinearId
:
1371 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX
;
1372 set_mode_system_value(b
, mode
);
1374 case SpvBuiltInBaseVertex
:
1375 /* OpenGL gl_BaseVertex (SYSTEM_VALUE_BASE_VERTEX) is not the same
1376 * semantic as Vulkan BaseVertex (SYSTEM_VALUE_FIRST_VERTEX).
1378 if (b
->options
->environment
== NIR_SPIRV_OPENGL
)
1379 *location
= SYSTEM_VALUE_BASE_VERTEX
;
1381 *location
= SYSTEM_VALUE_FIRST_VERTEX
;
1382 set_mode_system_value(b
, mode
);
1384 case SpvBuiltInBaseInstance
:
1385 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1386 set_mode_system_value(b
, mode
);
1388 case SpvBuiltInDrawIndex
:
1389 *location
= SYSTEM_VALUE_DRAW_ID
;
1390 set_mode_system_value(b
, mode
);
1392 case SpvBuiltInSubgroupSize
:
1393 *location
= SYSTEM_VALUE_SUBGROUP_SIZE
;
1394 set_mode_system_value(b
, mode
);
1396 case SpvBuiltInSubgroupId
:
1397 *location
= SYSTEM_VALUE_SUBGROUP_ID
;
1398 set_mode_system_value(b
, mode
);
1400 case SpvBuiltInSubgroupLocalInvocationId
:
1401 *location
= SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1402 set_mode_system_value(b
, mode
);
1404 case SpvBuiltInNumSubgroups
:
1405 *location
= SYSTEM_VALUE_NUM_SUBGROUPS
;
1406 set_mode_system_value(b
, mode
);
1408 case SpvBuiltInDeviceIndex
:
1409 *location
= SYSTEM_VALUE_DEVICE_INDEX
;
1410 set_mode_system_value(b
, mode
);
1412 case SpvBuiltInViewIndex
:
1413 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1414 set_mode_system_value(b
, mode
);
1416 case SpvBuiltInSubgroupEqMask
:
1417 *location
= SYSTEM_VALUE_SUBGROUP_EQ_MASK
,
1418 set_mode_system_value(b
, mode
);
1420 case SpvBuiltInSubgroupGeMask
:
1421 *location
= SYSTEM_VALUE_SUBGROUP_GE_MASK
,
1422 set_mode_system_value(b
, mode
);
1424 case SpvBuiltInSubgroupGtMask
:
1425 *location
= SYSTEM_VALUE_SUBGROUP_GT_MASK
,
1426 set_mode_system_value(b
, mode
);
1428 case SpvBuiltInSubgroupLeMask
:
1429 *location
= SYSTEM_VALUE_SUBGROUP_LE_MASK
,
1430 set_mode_system_value(b
, mode
);
1432 case SpvBuiltInSubgroupLtMask
:
1433 *location
= SYSTEM_VALUE_SUBGROUP_LT_MASK
,
1434 set_mode_system_value(b
, mode
);
1436 case SpvBuiltInFragStencilRefEXT
:
1437 *location
= FRAG_RESULT_STENCIL
;
1438 vtn_assert(*mode
== nir_var_shader_out
);
1440 case SpvBuiltInWorkDim
:
1441 *location
= SYSTEM_VALUE_WORK_DIM
;
1442 set_mode_system_value(b
, mode
);
1444 case SpvBuiltInGlobalSize
:
1445 *location
= SYSTEM_VALUE_GLOBAL_GROUP_SIZE
;
1446 set_mode_system_value(b
, mode
);
1448 case SpvBuiltInBaryCoordNoPerspAMD
:
1449 *location
= SYSTEM_VALUE_BARYCENTRIC_LINEAR_PIXEL
;
1450 set_mode_system_value(b
, mode
);
1452 case SpvBuiltInBaryCoordNoPerspCentroidAMD
:
1453 *location
= SYSTEM_VALUE_BARYCENTRIC_LINEAR_CENTROID
;
1454 set_mode_system_value(b
, mode
);
1456 case SpvBuiltInBaryCoordNoPerspSampleAMD
:
1457 *location
= SYSTEM_VALUE_BARYCENTRIC_LINEAR_SAMPLE
;
1458 set_mode_system_value(b
, mode
);
1460 case SpvBuiltInBaryCoordSmoothAMD
:
1461 *location
= SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL
;
1462 set_mode_system_value(b
, mode
);
1464 case SpvBuiltInBaryCoordSmoothCentroidAMD
:
1465 *location
= SYSTEM_VALUE_BARYCENTRIC_PERSP_CENTROID
;
1466 set_mode_system_value(b
, mode
);
1468 case SpvBuiltInBaryCoordSmoothSampleAMD
:
1469 *location
= SYSTEM_VALUE_BARYCENTRIC_PERSP_SAMPLE
;
1470 set_mode_system_value(b
, mode
);
1472 case SpvBuiltInBaryCoordPullModelAMD
:
1473 *location
= SYSTEM_VALUE_BARYCENTRIC_PULL_MODEL
;
1474 set_mode_system_value(b
, mode
);
1477 vtn_fail("Unsupported builtin: %s (%u)",
1478 spirv_builtin_to_string(builtin
), builtin
);
1483 apply_var_decoration(struct vtn_builder
*b
,
1484 struct nir_variable_data
*var_data
,
1485 const struct vtn_decoration
*dec
)
1487 switch (dec
->decoration
) {
1488 case SpvDecorationRelaxedPrecision
:
1489 break; /* FIXME: Do nothing with this for now. */
1490 case SpvDecorationNoPerspective
:
1491 var_data
->interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1493 case SpvDecorationFlat
:
1494 var_data
->interpolation
= INTERP_MODE_FLAT
;
1496 case SpvDecorationExplicitInterpAMD
:
1497 var_data
->interpolation
= INTERP_MODE_EXPLICIT
;
1499 case SpvDecorationCentroid
:
1500 var_data
->centroid
= true;
1502 case SpvDecorationSample
:
1503 var_data
->sample
= true;
1505 case SpvDecorationInvariant
:
1506 var_data
->invariant
= true;
1508 case SpvDecorationConstant
:
1509 var_data
->read_only
= true;
1511 case SpvDecorationNonReadable
:
1512 var_data
->access
|= ACCESS_NON_READABLE
;
1514 case SpvDecorationNonWritable
:
1515 var_data
->read_only
= true;
1516 var_data
->access
|= ACCESS_NON_WRITEABLE
;
1518 case SpvDecorationRestrict
:
1519 var_data
->access
|= ACCESS_RESTRICT
;
1521 case SpvDecorationAliased
:
1522 var_data
->access
&= ~ACCESS_RESTRICT
;
1524 case SpvDecorationVolatile
:
1525 var_data
->access
|= ACCESS_VOLATILE
;
1527 case SpvDecorationCoherent
:
1528 var_data
->access
|= ACCESS_COHERENT
;
1530 case SpvDecorationComponent
:
1531 var_data
->location_frac
= dec
->operands
[0];
1533 case SpvDecorationIndex
:
1534 var_data
->index
= dec
->operands
[0];
1536 case SpvDecorationBuiltIn
: {
1537 SpvBuiltIn builtin
= dec
->operands
[0];
1539 nir_variable_mode mode
= var_data
->mode
;
1540 vtn_get_builtin_location(b
, builtin
, &var_data
->location
, &mode
);
1541 var_data
->mode
= mode
;
1544 case SpvBuiltInTessLevelOuter
:
1545 case SpvBuiltInTessLevelInner
:
1546 case SpvBuiltInClipDistance
:
1547 case SpvBuiltInCullDistance
:
1548 var_data
->compact
= true;
1555 case SpvDecorationSpecId
:
1556 case SpvDecorationRowMajor
:
1557 case SpvDecorationColMajor
:
1558 case SpvDecorationMatrixStride
:
1559 case SpvDecorationUniform
:
1560 case SpvDecorationUniformId
:
1561 case SpvDecorationLinkageAttributes
:
1562 break; /* Do nothing with these here */
1564 case SpvDecorationPatch
:
1565 var_data
->patch
= true;
1568 case SpvDecorationLocation
:
1569 vtn_fail("Handled above");
1571 case SpvDecorationBlock
:
1572 case SpvDecorationBufferBlock
:
1573 case SpvDecorationArrayStride
:
1574 case SpvDecorationGLSLShared
:
1575 case SpvDecorationGLSLPacked
:
1576 break; /* These can apply to a type but we don't care about them */
1578 case SpvDecorationBinding
:
1579 case SpvDecorationDescriptorSet
:
1580 case SpvDecorationNoContraction
:
1581 case SpvDecorationInputAttachmentIndex
:
1582 vtn_warn("Decoration not allowed for variable or structure member: %s",
1583 spirv_decoration_to_string(dec
->decoration
));
1586 case SpvDecorationXfbBuffer
:
1587 var_data
->explicit_xfb_buffer
= true;
1588 var_data
->xfb
.buffer
= dec
->operands
[0];
1589 var_data
->always_active_io
= true;
1591 case SpvDecorationXfbStride
:
1592 var_data
->explicit_xfb_stride
= true;
1593 var_data
->xfb
.stride
= dec
->operands
[0];
1595 case SpvDecorationOffset
:
1596 var_data
->explicit_offset
= true;
1597 var_data
->offset
= dec
->operands
[0];
1600 case SpvDecorationStream
:
1601 var_data
->stream
= dec
->operands
[0];
1604 case SpvDecorationCPacked
:
1605 case SpvDecorationSaturatedConversion
:
1606 case SpvDecorationFuncParamAttr
:
1607 case SpvDecorationFPRoundingMode
:
1608 case SpvDecorationFPFastMathMode
:
1609 case SpvDecorationAlignment
:
1610 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
1611 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1612 spirv_decoration_to_string(dec
->decoration
));
1616 case SpvDecorationUserSemantic
:
1617 case SpvDecorationUserTypeGOOGLE
:
1618 /* User semantic decorations can safely be ignored by the driver. */
1621 case SpvDecorationRestrictPointerEXT
:
1622 case SpvDecorationAliasedPointerEXT
:
1623 /* TODO: We should actually plumb alias information through NIR. */
1627 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1632 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1633 const struct vtn_decoration
*dec
, void *out_is_patch
)
1635 if (dec
->decoration
== SpvDecorationPatch
) {
1636 *((bool *) out_is_patch
) = true;
1641 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1642 const struct vtn_decoration
*dec
, void *void_var
)
1644 struct vtn_variable
*vtn_var
= void_var
;
1646 /* Handle decorations that apply to a vtn_variable as a whole */
1647 switch (dec
->decoration
) {
1648 case SpvDecorationBinding
:
1649 vtn_var
->binding
= dec
->operands
[0];
1650 vtn_var
->explicit_binding
= true;
1652 case SpvDecorationDescriptorSet
:
1653 vtn_var
->descriptor_set
= dec
->operands
[0];
1655 case SpvDecorationInputAttachmentIndex
:
1656 vtn_var
->input_attachment_index
= dec
->operands
[0];
1658 case SpvDecorationPatch
:
1659 vtn_var
->patch
= true;
1661 case SpvDecorationOffset
:
1662 vtn_var
->offset
= dec
->operands
[0];
1664 case SpvDecorationNonWritable
:
1665 vtn_var
->access
|= ACCESS_NON_WRITEABLE
;
1667 case SpvDecorationNonReadable
:
1668 vtn_var
->access
|= ACCESS_NON_READABLE
;
1670 case SpvDecorationVolatile
:
1671 vtn_var
->access
|= ACCESS_VOLATILE
;
1673 case SpvDecorationCoherent
:
1674 vtn_var
->access
|= ACCESS_COHERENT
;
1676 case SpvDecorationCounterBuffer
:
1677 /* Counter buffer decorations can safely be ignored by the driver. */
1683 if (val
->value_type
== vtn_value_type_pointer
) {
1684 assert(val
->pointer
->var
== void_var
);
1685 assert(member
== -1);
1687 assert(val
->value_type
== vtn_value_type_type
);
1690 /* Location is odd. If applied to a split structure, we have to walk the
1691 * whole thing and accumulate the location. It's easier to handle as a
1694 if (dec
->decoration
== SpvDecorationLocation
) {
1695 unsigned location
= dec
->operands
[0];
1696 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
&&
1697 vtn_var
->mode
== vtn_variable_mode_output
) {
1698 location
+= FRAG_RESULT_DATA0
;
1699 } else if (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
&&
1700 vtn_var
->mode
== vtn_variable_mode_input
) {
1701 location
+= VERT_ATTRIB_GENERIC0
;
1702 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1703 vtn_var
->mode
== vtn_variable_mode_output
) {
1704 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1705 } else if (vtn_var
->mode
!= vtn_variable_mode_uniform
) {
1706 vtn_warn("Location must be on input, output, uniform, sampler or "
1711 if (vtn_var
->var
->num_members
== 0) {
1712 /* This handles the member and lone variable cases */
1713 vtn_var
->var
->data
.location
= location
;
1715 /* This handles the structure member case */
1716 assert(vtn_var
->var
->members
);
1719 vtn_var
->base_location
= location
;
1721 vtn_var
->var
->members
[member
].location
= location
;
1727 if (vtn_var
->var
->num_members
== 0) {
1728 /* We call this function on types as well as variables and not all
1729 * struct types get split so we can end up having stray member
1730 * decorations; just ignore them.
1733 apply_var_decoration(b
, &vtn_var
->var
->data
, dec
);
1734 } else if (member
>= 0) {
1735 /* Member decorations must come from a type */
1736 assert(val
->value_type
== vtn_value_type_type
);
1737 apply_var_decoration(b
, &vtn_var
->var
->members
[member
], dec
);
1740 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1741 for (unsigned i
= 0; i
< length
; i
++)
1742 apply_var_decoration(b
, &vtn_var
->var
->members
[i
], dec
);
1745 /* A few variables, those with external storage, have no actual
1746 * nir_variables associated with them. Fortunately, all decorations
1747 * we care about for those variables are on the type only.
1749 vtn_assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1750 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1751 vtn_var
->mode
== vtn_variable_mode_push_constant
);
1756 enum vtn_variable_mode
1757 vtn_storage_class_to_mode(struct vtn_builder
*b
,
1758 SpvStorageClass
class,
1759 struct vtn_type
*interface_type
,
1760 nir_variable_mode
*nir_mode_out
)
1762 enum vtn_variable_mode mode
;
1763 nir_variable_mode nir_mode
;
1765 case SpvStorageClassUniform
:
1766 /* Assume it's an UBO if we lack the interface_type. */
1767 if (!interface_type
|| interface_type
->block
) {
1768 mode
= vtn_variable_mode_ubo
;
1769 nir_mode
= nir_var_mem_ubo
;
1770 } else if (interface_type
->buffer_block
) {
1771 mode
= vtn_variable_mode_ssbo
;
1772 nir_mode
= nir_var_mem_ssbo
;
1774 /* Default-block uniforms, coming from gl_spirv */
1775 mode
= vtn_variable_mode_uniform
;
1776 nir_mode
= nir_var_uniform
;
1779 case SpvStorageClassStorageBuffer
:
1780 mode
= vtn_variable_mode_ssbo
;
1781 nir_mode
= nir_var_mem_ssbo
;
1783 case SpvStorageClassPhysicalStorageBuffer
:
1784 mode
= vtn_variable_mode_phys_ssbo
;
1785 nir_mode
= nir_var_mem_global
;
1787 case SpvStorageClassUniformConstant
:
1788 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1789 if (b
->options
->constant_as_global
) {
1790 mode
= vtn_variable_mode_cross_workgroup
;
1791 nir_mode
= nir_var_mem_global
;
1793 mode
= vtn_variable_mode_ubo
;
1794 nir_mode
= nir_var_mem_ubo
;
1797 mode
= vtn_variable_mode_uniform
;
1798 nir_mode
= nir_var_uniform
;
1801 case SpvStorageClassPushConstant
:
1802 mode
= vtn_variable_mode_push_constant
;
1803 nir_mode
= nir_var_uniform
;
1805 case SpvStorageClassInput
:
1806 mode
= vtn_variable_mode_input
;
1807 nir_mode
= nir_var_shader_in
;
1809 case SpvStorageClassOutput
:
1810 mode
= vtn_variable_mode_output
;
1811 nir_mode
= nir_var_shader_out
;
1813 case SpvStorageClassPrivate
:
1814 mode
= vtn_variable_mode_private
;
1815 nir_mode
= nir_var_shader_temp
;
1817 case SpvStorageClassFunction
:
1818 mode
= vtn_variable_mode_function
;
1819 nir_mode
= nir_var_function_temp
;
1821 case SpvStorageClassWorkgroup
:
1822 mode
= vtn_variable_mode_workgroup
;
1823 nir_mode
= nir_var_mem_shared
;
1825 case SpvStorageClassAtomicCounter
:
1826 mode
= vtn_variable_mode_atomic_counter
;
1827 nir_mode
= nir_var_uniform
;
1829 case SpvStorageClassCrossWorkgroup
:
1830 mode
= vtn_variable_mode_cross_workgroup
;
1831 nir_mode
= nir_var_mem_global
;
1833 case SpvStorageClassImage
:
1834 mode
= vtn_variable_mode_image
;
1835 nir_mode
= nir_var_mem_ubo
;
1837 case SpvStorageClassGeneric
:
1839 vtn_fail("Unhandled variable storage class: %s (%u)",
1840 spirv_storageclass_to_string(class), class);
1844 *nir_mode_out
= nir_mode
;
1850 vtn_mode_to_address_format(struct vtn_builder
*b
, enum vtn_variable_mode mode
)
1853 case vtn_variable_mode_ubo
:
1854 return b
->options
->ubo_addr_format
;
1856 case vtn_variable_mode_ssbo
:
1857 return b
->options
->ssbo_addr_format
;
1859 case vtn_variable_mode_phys_ssbo
:
1860 return b
->options
->phys_ssbo_addr_format
;
1862 case vtn_variable_mode_push_constant
:
1863 return b
->options
->push_const_addr_format
;
1865 case vtn_variable_mode_workgroup
:
1866 return b
->options
->shared_addr_format
;
1868 case vtn_variable_mode_cross_workgroup
:
1869 return b
->options
->global_addr_format
;
1871 case vtn_variable_mode_function
:
1872 if (b
->physical_ptrs
)
1873 return b
->options
->temp_addr_format
;
1876 case vtn_variable_mode_private
:
1877 case vtn_variable_mode_uniform
:
1878 case vtn_variable_mode_atomic_counter
:
1879 case vtn_variable_mode_input
:
1880 case vtn_variable_mode_output
:
1881 case vtn_variable_mode_image
:
1882 return nir_address_format_logical
;
1885 unreachable("Invalid variable mode");
1889 vtn_pointer_to_ssa(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
1891 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
1892 /* This pointer needs to have a pointer type with actual storage */
1893 vtn_assert(ptr
->ptr_type
);
1894 vtn_assert(ptr
->ptr_type
->type
);
1897 /* If we don't have an offset then we must be a pointer to the variable
1900 vtn_assert(!ptr
->offset
&& !ptr
->block_index
);
1902 struct vtn_access_chain chain
= {
1905 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
1908 vtn_assert(ptr
->offset
);
1909 if (ptr
->block_index
) {
1910 vtn_assert(ptr
->mode
== vtn_variable_mode_ubo
||
1911 ptr
->mode
== vtn_variable_mode_ssbo
);
1912 return nir_vec2(&b
->nb
, ptr
->block_index
, ptr
->offset
);
1914 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
);
1918 if (vtn_pointer_is_external_block(b
, ptr
) &&
1919 vtn_type_contains_block(b
, ptr
->type
) &&
1920 ptr
->mode
!= vtn_variable_mode_phys_ssbo
) {
1921 /* In this case, we're looking for a block index and not an actual
1924 * For PhysicalStorageBuffer pointers, we don't have a block index
1925 * at all because we get the pointer directly from the client. This
1926 * assumes that there will never be a SSBO binding variable using the
1927 * PhysicalStorageBuffer storage class. This assumption appears
1928 * to be correct according to the Vulkan spec because the table,
1929 * "Shader Resource and Storage Class Correspondence," the only the
1930 * Uniform storage class with BufferBlock or the StorageBuffer
1931 * storage class with Block can be used.
1933 if (!ptr
->block_index
) {
1934 /* If we don't have a block_index then we must be a pointer to the
1937 vtn_assert(!ptr
->deref
);
1939 struct vtn_access_chain chain
= {
1942 ptr
= vtn_nir_deref_pointer_dereference(b
, ptr
, &chain
);
1945 return ptr
->block_index
;
1947 return &vtn_pointer_to_deref(b
, ptr
)->dest
.ssa
;
1952 struct vtn_pointer
*
1953 vtn_pointer_from_ssa(struct vtn_builder
*b
, nir_ssa_def
*ssa
,
1954 struct vtn_type
*ptr_type
)
1956 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1958 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
1959 struct vtn_type
*without_array
=
1960 vtn_type_without_array(ptr_type
->deref
);
1962 nir_variable_mode nir_mode
;
1963 ptr
->mode
= vtn_storage_class_to_mode(b
, ptr_type
->storage_class
,
1964 without_array
, &nir_mode
);
1965 ptr
->type
= ptr_type
->deref
;
1966 ptr
->ptr_type
= ptr_type
;
1968 if (b
->wa_glslang_179
) {
1969 /* To work around https://github.com/KhronosGroup/glslang/issues/179 we
1970 * need to whack the mode because it creates a function parameter with
1971 * the Function storage class even though it's a pointer to a sampler.
1972 * If we don't do this, then NIR won't get rid of the deref_cast for us.
1974 if (ptr
->mode
== vtn_variable_mode_function
&&
1975 (ptr
->type
->base_type
== vtn_base_type_sampler
||
1976 ptr
->type
->base_type
== vtn_base_type_sampled_image
)) {
1977 ptr
->mode
= vtn_variable_mode_uniform
;
1978 nir_mode
= nir_var_uniform
;
1982 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
1983 /* This pointer type needs to have actual storage */
1984 vtn_assert(ptr_type
->type
);
1985 if (ptr
->mode
== vtn_variable_mode_ubo
||
1986 ptr
->mode
== vtn_variable_mode_ssbo
) {
1987 vtn_assert(ssa
->num_components
== 2);
1988 ptr
->block_index
= nir_channel(&b
->nb
, ssa
, 0);
1989 ptr
->offset
= nir_channel(&b
->nb
, ssa
, 1);
1991 vtn_assert(ssa
->num_components
== 1);
1992 ptr
->block_index
= NULL
;
1996 const struct glsl_type
*deref_type
=
1997 vtn_type_get_nir_type(b
, ptr_type
->deref
, ptr
->mode
);
1998 if (!vtn_pointer_is_external_block(b
, ptr
)) {
1999 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
2000 deref_type
, ptr_type
->stride
);
2001 } else if (vtn_type_contains_block(b
, ptr
->type
) &&
2002 ptr
->mode
!= vtn_variable_mode_phys_ssbo
) {
2003 /* This is a pointer to somewhere in an array of blocks, not a
2004 * pointer to somewhere inside the block. Set the block index
2005 * instead of making a cast.
2007 ptr
->block_index
= ssa
;
2009 /* This is a pointer to something internal or a pointer inside a
2010 * block. It's just a regular cast.
2012 * For PhysicalStorageBuffer pointers, we don't have a block index
2013 * at all because we get the pointer directly from the client. This
2014 * assumes that there will never be a SSBO binding variable using the
2015 * PhysicalStorageBuffer storage class. This assumption appears
2016 * to be correct according to the Vulkan spec because the table,
2017 * "Shader Resource and Storage Class Correspondence," the only the
2018 * Uniform storage class with BufferBlock or the StorageBuffer
2019 * storage class with Block can be used.
2021 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
2022 deref_type
, ptr_type
->stride
);
2023 ptr
->deref
->dest
.ssa
.num_components
=
2024 glsl_get_vector_elements(ptr_type
->type
);
2025 ptr
->deref
->dest
.ssa
.bit_size
= glsl_get_bit_size(ptr_type
->type
);
2033 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
2035 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
2038 if (var
->mode
== vtn_variable_mode_input
) {
2039 return stage
== MESA_SHADER_TESS_CTRL
||
2040 stage
== MESA_SHADER_TESS_EVAL
||
2041 stage
== MESA_SHADER_GEOMETRY
;
2044 if (var
->mode
== vtn_variable_mode_output
)
2045 return stage
== MESA_SHADER_TESS_CTRL
;
2051 assign_missing_member_locations(struct vtn_variable
*var
)
2054 glsl_get_length(glsl_without_array(var
->type
->type
));
2055 int location
= var
->base_location
;
2057 for (unsigned i
= 0; i
< length
; i
++) {
2058 /* From the Vulkan spec:
2060 * “If the structure type is a Block but without a Location, then each
2061 * of its members must have a Location decoration.”
2064 if (var
->type
->block
) {
2065 assert(var
->base_location
!= -1 ||
2066 var
->var
->members
[i
].location
!= -1);
2069 /* From the Vulkan spec:
2071 * “Any member with its own Location decoration is assigned that
2072 * location. Each remaining member is assigned the location after the
2073 * immediately preceding member in declaration order.”
2075 if (var
->var
->members
[i
].location
!= -1)
2076 location
= var
->var
->members
[i
].location
;
2078 var
->var
->members
[i
].location
= location
;
2080 /* Below we use type instead of interface_type, because interface_type
2081 * is only available when it is a Block. This code also supports
2082 * input/outputs that are just structs
2084 const struct glsl_type
*member_type
=
2085 glsl_get_struct_field(glsl_without_array(var
->type
->type
), i
);
2088 glsl_count_attribute_slots(member_type
,
2089 false /* is_gl_vertex_input */);
2095 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
2096 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
2097 nir_constant
*const_initializer
, nir_variable
*var_initializer
)
2099 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
2100 struct vtn_type
*type
= ptr_type
->deref
;
2102 struct vtn_type
*without_array
= vtn_type_without_array(ptr_type
->deref
);
2104 enum vtn_variable_mode mode
;
2105 nir_variable_mode nir_mode
;
2106 mode
= vtn_storage_class_to_mode(b
, storage_class
, without_array
, &nir_mode
);
2109 case vtn_variable_mode_ubo
:
2110 /* There's no other way to get vtn_variable_mode_ubo */
2111 vtn_assert(without_array
->block
);
2112 b
->shader
->info
.num_ubos
++;
2114 case vtn_variable_mode_ssbo
:
2115 if (storage_class
== SpvStorageClassStorageBuffer
&&
2116 !without_array
->block
) {
2117 if (b
->variable_pointers
) {
2118 vtn_fail("Variables in the StorageBuffer storage class must "
2119 "have a struct type with the Block decoration");
2121 /* If variable pointers are not present, it's still malformed
2122 * SPIR-V but we can parse it and do the right thing anyway.
2123 * Since some of the 8-bit storage tests have bugs in this are,
2124 * just make it a warning for now.
2126 vtn_warn("Variables in the StorageBuffer storage class must "
2127 "have a struct type with the Block decoration");
2130 b
->shader
->info
.num_ssbos
++;
2132 case vtn_variable_mode_uniform
:
2133 if (glsl_type_is_image(without_array
->type
))
2134 b
->shader
->info
.num_images
++;
2135 else if (glsl_type_is_sampler(without_array
->type
))
2136 b
->shader
->info
.num_textures
++;
2138 case vtn_variable_mode_push_constant
:
2139 b
->shader
->num_uniforms
= vtn_type_block_size(b
, type
);
2142 case vtn_variable_mode_image
:
2143 vtn_fail("Cannot create a variable with the Image storage class");
2146 case vtn_variable_mode_phys_ssbo
:
2147 vtn_fail("Cannot create a variable with the "
2148 "PhysicalStorageBuffer storage class");
2152 /* No tallying is needed */
2156 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
2159 var
->base_location
= -1;
2161 val
->pointer
= rzalloc(b
, struct vtn_pointer
);
2162 val
->pointer
->mode
= var
->mode
;
2163 val
->pointer
->type
= var
->type
;
2164 val
->pointer
->ptr_type
= ptr_type
;
2165 val
->pointer
->var
= var
;
2166 val
->pointer
->access
= var
->type
->access
;
2168 switch (var
->mode
) {
2169 case vtn_variable_mode_function
:
2170 case vtn_variable_mode_private
:
2171 case vtn_variable_mode_uniform
:
2172 case vtn_variable_mode_atomic_counter
:
2173 /* For these, we create the variable normally */
2174 var
->var
= rzalloc(b
->shader
, nir_variable
);
2175 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2176 var
->var
->type
= vtn_type_get_nir_type(b
, var
->type
, var
->mode
);
2177 var
->var
->data
.mode
= nir_mode
;
2178 var
->var
->data
.location
= -1;
2179 var
->var
->interface_type
= NULL
;
2182 case vtn_variable_mode_ubo
:
2183 case vtn_variable_mode_ssbo
:
2184 var
->var
= rzalloc(b
->shader
, nir_variable
);
2185 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2187 var
->var
->type
= vtn_type_get_nir_type(b
, var
->type
, var
->mode
);
2188 var
->var
->interface_type
= var
->var
->type
;
2190 var
->var
->data
.mode
= nir_mode
;
2191 var
->var
->data
.location
= -1;
2195 case vtn_variable_mode_workgroup
:
2196 /* Create the variable normally */
2197 var
->var
= rzalloc(b
->shader
, nir_variable
);
2198 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2199 /* Workgroup variables don't have any explicit layout but some
2200 * layouts may have leaked through due to type deduplication in the
2203 var
->var
->type
= vtn_type_get_nir_type(b
, var
->type
, var
->mode
);
2204 var
->var
->data
.mode
= nir_var_mem_shared
;
2207 case vtn_variable_mode_input
:
2208 case vtn_variable_mode_output
: {
2209 /* In order to know whether or not we're a per-vertex inout, we need
2210 * the patch qualifier. This means walking the variable decorations
2211 * early before we actually create any variables. Not a big deal.
2213 * GLSLang really likes to place decorations in the most interior
2214 * thing it possibly can. In particular, if you have a struct, it
2215 * will place the patch decorations on the struct members. This
2216 * should be handled by the variable splitting below just fine.
2218 * If you have an array-of-struct, things get even more weird as it
2219 * will place the patch decorations on the struct even though it's
2220 * inside an array and some of the members being patch and others not
2221 * makes no sense whatsoever. Since the only sensible thing is for
2222 * it to be all or nothing, we'll call it patch if any of the members
2223 * are declared patch.
2226 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
2227 if (glsl_type_is_array(var
->type
->type
) &&
2228 glsl_type_is_struct_or_ifc(without_array
->type
)) {
2229 vtn_foreach_decoration(b
, vtn_value(b
, without_array
->id
,
2230 vtn_value_type_type
),
2231 var_is_patch_cb
, &var
->patch
);
2234 /* For inputs and outputs, we immediately split structures. This
2235 * is for a couple of reasons. For one, builtins may all come in
2236 * a struct and we really want those split out into separate
2237 * variables. For another, interpolation qualifiers can be
2238 * applied to members of the top-level struct ane we need to be
2239 * able to preserve that information.
2242 struct vtn_type
*per_vertex_type
= var
->type
;
2243 if (is_per_vertex_inout(var
, b
->shader
->info
.stage
)) {
2244 /* In Geometry shaders (and some tessellation), inputs come
2245 * in per-vertex arrays. However, some builtins come in
2246 * non-per-vertex, hence the need for the is_array check. In
2247 * any case, there are no non-builtin arrays allowed so this
2248 * check should be sufficient.
2250 per_vertex_type
= var
->type
->array_element
;
2253 var
->var
= rzalloc(b
->shader
, nir_variable
);
2254 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2255 /* In Vulkan, shader I/O variables don't have any explicit layout but
2256 * some layouts may have leaked through due to type deduplication in
2257 * the SPIR-V. We do, however, keep the layouts in the variable's
2258 * interface_type because we need offsets for XFB arrays of blocks.
2260 var
->var
->type
= vtn_type_get_nir_type(b
, var
->type
, var
->mode
);
2261 var
->var
->data
.mode
= nir_mode
;
2262 var
->var
->data
.patch
= var
->patch
;
2264 /* Figure out the interface block type. */
2265 struct vtn_type
*iface_type
= per_vertex_type
;
2266 if (var
->mode
== vtn_variable_mode_output
&&
2267 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
2268 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
||
2269 b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)) {
2270 /* For vertex data outputs, we can end up with arrays of blocks for
2271 * transform feedback where each array element corresponds to a
2272 * different XFB output buffer.
2274 while (iface_type
->base_type
== vtn_base_type_array
)
2275 iface_type
= iface_type
->array_element
;
2277 if (iface_type
->base_type
== vtn_base_type_struct
&& iface_type
->block
)
2278 var
->var
->interface_type
= vtn_type_get_nir_type(b
, iface_type
,
2281 if (per_vertex_type
->base_type
== vtn_base_type_struct
&&
2282 per_vertex_type
->block
) {
2283 /* It's a struct. Set it up as per-member. */
2284 var
->var
->num_members
= glsl_get_length(per_vertex_type
->type
);
2285 var
->var
->members
= rzalloc_array(var
->var
, struct nir_variable_data
,
2286 var
->var
->num_members
);
2288 for (unsigned i
= 0; i
< var
->var
->num_members
; i
++) {
2289 var
->var
->members
[i
].mode
= nir_mode
;
2290 var
->var
->members
[i
].patch
= var
->patch
;
2291 var
->var
->members
[i
].location
= -1;
2295 /* For inputs and outputs, we need to grab locations and builtin
2296 * information from the per-vertex type.
2298 vtn_foreach_decoration(b
, vtn_value(b
, per_vertex_type
->id
,
2299 vtn_value_type_type
),
2300 var_decoration_cb
, var
);
2304 case vtn_variable_mode_push_constant
:
2305 case vtn_variable_mode_cross_workgroup
:
2306 /* These don't need actual variables. */
2309 case vtn_variable_mode_image
:
2310 case vtn_variable_mode_phys_ssbo
:
2311 unreachable("Should have been caught before");
2314 /* We can only have one type of initializer */
2315 assert(!(const_initializer
&& var_initializer
));
2316 if (const_initializer
) {
2317 var
->var
->constant_initializer
=
2318 nir_constant_clone(const_initializer
, var
->var
);
2320 if (var_initializer
)
2321 var
->var
->pointer_initializer
= var_initializer
;
2323 if (var
->mode
== vtn_variable_mode_uniform
||
2324 var
->mode
== vtn_variable_mode_ssbo
) {
2325 /* SSBOs and images are assumed to not alias in the Simple, GLSL and Vulkan memory models */
2326 var
->var
->data
.access
|= b
->mem_model
!= SpvMemoryModelOpenCL
? ACCESS_RESTRICT
: 0;
2329 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
2330 vtn_foreach_decoration(b
, val
, ptr_decoration_cb
, val
->pointer
);
2332 /* Propagate access flags from the OpVariable decorations. */
2333 val
->pointer
->access
|= var
->access
;
2335 if ((var
->mode
== vtn_variable_mode_input
||
2336 var
->mode
== vtn_variable_mode_output
) &&
2337 var
->var
->members
) {
2338 assign_missing_member_locations(var
);
2341 if (var
->mode
== vtn_variable_mode_uniform
||
2342 var
->mode
== vtn_variable_mode_ubo
||
2343 var
->mode
== vtn_variable_mode_ssbo
) {
2344 /* XXX: We still need the binding information in the nir_variable
2345 * for these. We should fix that.
2347 var
->var
->data
.binding
= var
->binding
;
2348 var
->var
->data
.explicit_binding
= var
->explicit_binding
;
2349 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
2350 var
->var
->data
.index
= var
->input_attachment_index
;
2351 var
->var
->data
.offset
= var
->offset
;
2353 if (glsl_type_is_image(without_array
->type
))
2354 var
->var
->data
.image
.format
= without_array
->image_format
;
2357 if (var
->mode
== vtn_variable_mode_function
) {
2358 vtn_assert(var
->var
!= NULL
&& var
->var
->members
== NULL
);
2359 nir_function_impl_add_variable(b
->nb
.impl
, var
->var
);
2360 } else if (var
->var
) {
2361 nir_shader_add_variable(b
->shader
, var
->var
);
2363 vtn_assert(vtn_pointer_is_external_block(b
, val
->pointer
));
2368 vtn_assert_types_equal(struct vtn_builder
*b
, SpvOp opcode
,
2369 struct vtn_type
*dst_type
,
2370 struct vtn_type
*src_type
)
2372 if (dst_type
->id
== src_type
->id
)
2375 if (vtn_types_compatible(b
, dst_type
, src_type
)) {
2376 /* Early versions of GLSLang would re-emit types unnecessarily and you
2377 * would end up with OpLoad, OpStore, or OpCopyMemory opcodes which have
2378 * mismatched source and destination types.
2380 * https://github.com/KhronosGroup/glslang/issues/304
2381 * https://github.com/KhronosGroup/glslang/issues/307
2382 * https://bugs.freedesktop.org/show_bug.cgi?id=104338
2383 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2385 vtn_warn("Source and destination types of %s do not have the same "
2386 "ID (but are compatible): %u vs %u",
2387 spirv_op_to_string(opcode
), dst_type
->id
, src_type
->id
);
2391 vtn_fail("Source and destination types of %s do not match: %s vs. %s",
2392 spirv_op_to_string(opcode
),
2393 glsl_get_type_name(dst_type
->type
),
2394 glsl_get_type_name(src_type
->type
));
2397 static nir_ssa_def
*
2398 nir_shrink_zero_pad_vec(nir_builder
*b
, nir_ssa_def
*val
,
2399 unsigned num_components
)
2401 if (val
->num_components
== num_components
)
2404 nir_ssa_def
*comps
[NIR_MAX_VEC_COMPONENTS
];
2405 for (unsigned i
= 0; i
< num_components
; i
++) {
2406 if (i
< val
->num_components
)
2407 comps
[i
] = nir_channel(b
, val
, i
);
2409 comps
[i
] = nir_imm_intN_t(b
, 0, val
->bit_size
);
2411 return nir_vec(b
, comps
, num_components
);
2414 static nir_ssa_def
*
2415 nir_sloppy_bitcast(nir_builder
*b
, nir_ssa_def
*val
,
2416 const struct glsl_type
*type
)
2418 const unsigned num_components
= glsl_get_vector_elements(type
);
2419 const unsigned bit_size
= glsl_get_bit_size(type
);
2421 /* First, zero-pad to ensure that the value is big enough that when we
2422 * bit-cast it, we don't loose anything.
2424 if (val
->bit_size
< bit_size
) {
2425 const unsigned src_num_components_needed
=
2426 vtn_align_u32(val
->num_components
, bit_size
/ val
->bit_size
);
2427 val
= nir_shrink_zero_pad_vec(b
, val
, src_num_components_needed
);
2430 val
= nir_bitcast_vector(b
, val
, bit_size
);
2432 return nir_shrink_zero_pad_vec(b
, val
, num_components
);
2436 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
2437 const uint32_t *w
, unsigned count
)
2441 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
2442 val
->type
= vtn_get_type(b
, w
[1]);
2446 case SpvOpVariable
: {
2447 struct vtn_type
*ptr_type
= vtn_get_type(b
, w
[1]);
2449 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2451 SpvStorageClass storage_class
= w
[3];
2452 nir_constant
*const_initializer
= NULL
;
2453 nir_variable
*var_initializer
= NULL
;
2455 struct vtn_value
*init
= vtn_untyped_value(b
, w
[4]);
2456 switch (init
->value_type
) {
2457 case vtn_value_type_constant
:
2458 const_initializer
= init
->constant
;
2460 case vtn_value_type_pointer
:
2461 var_initializer
= init
->pointer
->var
->var
;
2464 vtn_fail("SPIR-V variable initializer %u must be constant or pointer",
2469 vtn_create_variable(b
, val
, ptr_type
, storage_class
, const_initializer
, var_initializer
);
2474 case SpvOpAccessChain
:
2475 case SpvOpPtrAccessChain
:
2476 case SpvOpInBoundsAccessChain
:
2477 case SpvOpInBoundsPtrAccessChain
: {
2478 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
2479 enum gl_access_qualifier access
= 0;
2480 chain
->ptr_as_array
= (opcode
== SpvOpPtrAccessChain
|| opcode
== SpvOpInBoundsPtrAccessChain
);
2483 for (int i
= 4; i
< count
; i
++) {
2484 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
2485 if (link_val
->value_type
== vtn_value_type_constant
) {
2486 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
2487 chain
->link
[idx
].id
= vtn_constant_int(b
, w
[i
]);
2489 chain
->link
[idx
].mode
= vtn_access_mode_id
;
2490 chain
->link
[idx
].id
= w
[i
];
2495 struct vtn_type
*ptr_type
= vtn_get_type(b
, w
[1]);
2496 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
2497 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
2498 /* This is rather insane. SPIR-V allows you to use OpSampledImage
2499 * to combine an array of images with a single sampler to get an
2500 * array of sampled images that all share the same sampler.
2501 * Fortunately, this means that we can more-or-less ignore the
2502 * sampler when crawling the access chain, but it does leave us
2503 * with this rather awkward little special-case.
2505 struct vtn_value
*val
=
2506 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2507 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2508 val
->sampled_image
->image
=
2509 vtn_pointer_dereference(b
, base_val
->sampled_image
->image
, chain
);
2510 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
2511 val
->sampled_image
->image
=
2512 vtn_decorate_pointer(b
, val
, val
->sampled_image
->image
);
2513 val
->sampled_image
->sampler
=
2514 vtn_decorate_pointer(b
, val
, val
->sampled_image
->sampler
);
2516 vtn_assert(base_val
->value_type
== vtn_value_type_pointer
);
2517 struct vtn_pointer
*ptr
=
2518 vtn_pointer_dereference(b
, base_val
->pointer
, chain
);
2519 ptr
->ptr_type
= ptr_type
;
2520 ptr
->access
|= access
;
2521 vtn_push_pointer(b
, w
[2], ptr
);
2526 case SpvOpCopyMemory
: {
2527 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2528 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
2530 vtn_assert_types_equal(b
, opcode
, dest
->type
->deref
, src
->type
->deref
);
2532 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
2537 struct vtn_type
*res_type
= vtn_get_type(b
, w
[1]);
2538 struct vtn_value
*src_val
= vtn_value(b
, w
[3], vtn_value_type_pointer
);
2539 struct vtn_pointer
*src
= src_val
->pointer
;
2541 vtn_assert_types_equal(b
, opcode
, res_type
, src_val
->type
->deref
);
2543 if (res_type
->base_type
== vtn_base_type_image
||
2544 res_type
->base_type
== vtn_base_type_sampler
) {
2545 vtn_push_pointer(b
, w
[2], src
);
2547 } else if (res_type
->base_type
== vtn_base_type_sampled_image
) {
2548 struct vtn_value
*val
=
2549 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2550 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2551 val
->sampled_image
->image
= val
->sampled_image
->sampler
=
2552 vtn_decorate_pointer(b
, val
, src
);
2558 SpvMemoryAccessMask access
= w
[4];
2559 if (access
& SpvMemoryAccessAlignedMask
)
2562 if (access
& SpvMemoryAccessMakePointerVisibleMask
) {
2563 SpvMemorySemanticsMask semantics
=
2564 SpvMemorySemanticsMakeVisibleMask
|
2565 vtn_storage_class_to_memory_semantics(src
->ptr_type
->storage_class
);
2567 SpvScope scope
= vtn_constant_uint(b
, w
[idx
]);
2568 vtn_emit_memory_barrier(b
, scope
, semantics
);
2572 vtn_push_ssa_value(b
, w
[2], vtn_variable_load(b
, src
));
2577 struct vtn_value
*dest_val
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2578 struct vtn_pointer
*dest
= dest_val
->pointer
;
2579 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[2]);
2581 /* OpStore requires us to actually have a storage type */
2582 vtn_fail_if(dest
->type
->type
== NULL
,
2583 "Invalid destination type for OpStore");
2585 if (glsl_get_base_type(dest
->type
->type
) == GLSL_TYPE_BOOL
&&
2586 glsl_get_base_type(src_val
->type
->type
) == GLSL_TYPE_UINT
) {
2587 /* Early versions of GLSLang would use uint types for UBOs/SSBOs but
2588 * would then store them to a local variable as bool. Work around
2589 * the issue by doing an implicit conversion.
2591 * https://github.com/KhronosGroup/glslang/issues/170
2592 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2594 vtn_warn("OpStore of value of type OpTypeInt to a pointer to type "
2595 "OpTypeBool. Doing an implicit conversion to work around "
2597 struct vtn_ssa_value
*bool_ssa
=
2598 vtn_create_ssa_value(b
, dest
->type
->type
);
2599 bool_ssa
->def
= nir_i2b(&b
->nb
, vtn_ssa_value(b
, w
[2])->def
);
2600 vtn_variable_store(b
, bool_ssa
, dest
);
2604 vtn_assert_types_equal(b
, opcode
, dest_val
->type
->deref
, src_val
->type
);
2606 if (glsl_type_is_sampler(dest
->type
->type
)) {
2607 if (b
->wa_glslang_179
) {
2608 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
2609 "propagation to workaround the problem.");
2610 vtn_assert(dest
->var
->copy_prop_sampler
== NULL
);
2611 struct vtn_value
*v
= vtn_untyped_value(b
, w
[2]);
2612 if (v
->value_type
== vtn_value_type_sampled_image
) {
2613 dest
->var
->copy_prop_sampler
= v
->sampled_image
->sampler
;
2615 vtn_assert(v
->value_type
== vtn_value_type_pointer
);
2616 dest
->var
->copy_prop_sampler
= v
->pointer
;
2619 vtn_fail("Vulkan does not allow OpStore of a sampler or image.");
2624 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
2625 vtn_variable_store(b
, src
, dest
);
2629 SpvMemoryAccessMask access
= w
[3];
2631 if (access
& SpvMemoryAccessAlignedMask
)
2634 if (access
& SpvMemoryAccessMakePointerAvailableMask
) {
2635 SpvMemorySemanticsMask semantics
=
2636 SpvMemorySemanticsMakeAvailableMask
|
2637 vtn_storage_class_to_memory_semantics(dest
->ptr_type
->storage_class
);
2638 SpvScope scope
= vtn_constant_uint(b
, w
[idx
]);
2639 vtn_emit_memory_barrier(b
, scope
, semantics
);
2645 case SpvOpArrayLength
: {
2646 struct vtn_pointer
*ptr
=
2647 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2648 const uint32_t field
= w
[4];
2650 vtn_fail_if(ptr
->type
->base_type
!= vtn_base_type_struct
,
2651 "OpArrayLength must take a pointer to a structure type");
2652 vtn_fail_if(field
!= ptr
->type
->length
- 1 ||
2653 ptr
->type
->members
[field
]->base_type
!= vtn_base_type_array
,
2654 "OpArrayLength must reference the last memeber of the "
2655 "structure and that must be an array");
2657 const uint32_t offset
= ptr
->type
->offsets
[field
];
2658 const uint32_t stride
= ptr
->type
->members
[field
]->stride
;
2660 if (!ptr
->block_index
) {
2661 struct vtn_access_chain chain
= {
2664 ptr
= vtn_pointer_dereference(b
, ptr
, &chain
);
2665 vtn_assert(ptr
->block_index
);
2668 nir_intrinsic_instr
*instr
=
2669 nir_intrinsic_instr_create(b
->nb
.shader
,
2670 nir_intrinsic_get_buffer_size
);
2671 instr
->src
[0] = nir_src_for_ssa(ptr
->block_index
);
2672 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
2673 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2674 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
2676 /* array_length = max(buffer_size - offset, 0) / stride */
2677 nir_ssa_def
*array_length
=
2682 nir_imm_int(&b
->nb
, offset
)),
2683 nir_imm_int(&b
->nb
, 0u)),
2684 nir_imm_int(&b
->nb
, stride
));
2686 vtn_push_nir_ssa(b
, w
[2], array_length
);
2690 case SpvOpConvertPtrToU
: {
2691 struct vtn_type
*u_type
= vtn_get_type(b
, w
[1]);
2692 struct vtn_type
*ptr_type
= vtn_get_value_type(b
, w
[3]);
2694 vtn_fail_if(ptr_type
->base_type
!= vtn_base_type_pointer
||
2695 ptr_type
->type
== NULL
,
2696 "OpConvertPtrToU can only be used on physical pointers");
2698 vtn_fail_if(u_type
->base_type
!= vtn_base_type_vector
&&
2699 u_type
->base_type
!= vtn_base_type_scalar
,
2700 "OpConvertPtrToU can only be used to cast to a vector or "
2703 /* The pointer will be converted to an SSA value automatically */
2704 nir_ssa_def
*ptr
= vtn_get_nir_ssa(b
, w
[3]);
2705 nir_ssa_def
*u
= nir_sloppy_bitcast(&b
->nb
, ptr
, u_type
->type
);
2706 vtn_push_nir_ssa(b
, w
[2], u
);
2710 case SpvOpConvertUToPtr
: {
2711 struct vtn_type
*ptr_type
= vtn_get_type(b
, w
[1]);
2712 struct vtn_type
*u_type
= vtn_get_value_type(b
, w
[3]);
2714 vtn_fail_if(ptr_type
->base_type
!= vtn_base_type_pointer
||
2715 ptr_type
->type
== NULL
,
2716 "OpConvertUToPtr can only be used on physical pointers");
2718 vtn_fail_if(u_type
->base_type
!= vtn_base_type_vector
&&
2719 u_type
->base_type
!= vtn_base_type_scalar
,
2720 "OpConvertUToPtr can only be used to cast from a vector or "
2723 nir_ssa_def
*u
= vtn_get_nir_ssa(b
, w
[3]);
2724 nir_ssa_def
*ptr
= nir_sloppy_bitcast(&b
->nb
, u
, ptr_type
->type
);
2725 vtn_push_pointer(b
, w
[2], vtn_pointer_from_ssa(b
, ptr
, ptr_type
));
2729 case SpvOpCopyMemorySized
:
2731 vtn_fail_with_opcode("Unhandled opcode", opcode
);