2 * Copyright © 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Jason Ekstrand (jason@jlekstrand.net)
28 #include "vtn_private.h"
29 #include "spirv_info.h"
30 #include "nir_deref.h"
31 #include <vulkan/vulkan_core.h>
33 static struct vtn_access_chain
*
34 vtn_access_chain_create(struct vtn_builder
*b
, unsigned length
)
36 struct vtn_access_chain
*chain
;
38 /* Subtract 1 from the length since there's already one built in */
39 size_t size
= sizeof(*chain
) +
40 (MAX2(length
, 1) - 1) * sizeof(chain
->link
[0]);
41 chain
= rzalloc_size(b
, size
);
42 chain
->length
= length
;
48 vtn_mode_uses_ssa_offset(struct vtn_builder
*b
,
49 enum vtn_variable_mode mode
)
51 return ((mode
== vtn_variable_mode_ubo
||
52 mode
== vtn_variable_mode_ssbo
) &&
53 b
->options
->lower_ubo_ssbo_access_to_offsets
) ||
54 mode
== vtn_variable_mode_push_constant
||
55 (mode
== vtn_variable_mode_workgroup
&&
56 b
->options
->lower_workgroup_access_to_offsets
);
60 vtn_pointer_is_external_block(struct vtn_builder
*b
,
61 struct vtn_pointer
*ptr
)
63 return ptr
->mode
== vtn_variable_mode_ssbo
||
64 ptr
->mode
== vtn_variable_mode_ubo
||
65 ptr
->mode
== vtn_variable_mode_phys_ssbo
||
66 ptr
->mode
== vtn_variable_mode_push_constant
||
67 (ptr
->mode
== vtn_variable_mode_workgroup
&&
68 b
->options
->lower_workgroup_access_to_offsets
);
72 vtn_access_link_as_ssa(struct vtn_builder
*b
, struct vtn_access_link link
,
73 unsigned stride
, unsigned bit_size
)
75 vtn_assert(stride
> 0);
76 if (link
.mode
== vtn_access_mode_literal
) {
77 return nir_imm_intN_t(&b
->nb
, link
.id
* stride
, bit_size
);
79 nir_ssa_def
*ssa
= vtn_ssa_value(b
, link
.id
)->def
;
80 if (ssa
->bit_size
!= bit_size
)
81 ssa
= nir_i2i(&b
->nb
, ssa
, bit_size
);
82 return nir_imul_imm(&b
->nb
, ssa
, stride
);
86 static VkDescriptorType
87 vk_desc_type_for_mode(struct vtn_builder
*b
, enum vtn_variable_mode mode
)
90 case vtn_variable_mode_ubo
:
91 return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
;
92 case vtn_variable_mode_ssbo
:
93 return VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
;
95 vtn_fail("Invalid mode for vulkan_resource_index");
100 vtn_variable_resource_index(struct vtn_builder
*b
, struct vtn_variable
*var
,
101 nir_ssa_def
*desc_array_index
)
103 vtn_assert(b
->options
->environment
== NIR_SPIRV_VULKAN
);
105 if (!desc_array_index
) {
106 vtn_assert(glsl_type_is_struct_or_ifc(var
->type
->type
));
107 desc_array_index
= nir_imm_int(&b
->nb
, 0);
110 nir_intrinsic_instr
*instr
=
111 nir_intrinsic_instr_create(b
->nb
.shader
,
112 nir_intrinsic_vulkan_resource_index
);
113 instr
->src
[0] = nir_src_for_ssa(desc_array_index
);
114 nir_intrinsic_set_desc_set(instr
, var
->descriptor_set
);
115 nir_intrinsic_set_binding(instr
, var
->binding
);
116 nir_intrinsic_set_desc_type(instr
, vk_desc_type_for_mode(b
, var
->mode
));
118 vtn_fail_if(var
->mode
!= vtn_variable_mode_ubo
&&
119 var
->mode
!= vtn_variable_mode_ssbo
,
120 "Invalid mode for vulkan_resource_index");
122 nir_address_format addr_format
= vtn_mode_to_address_format(b
, var
->mode
);
123 const struct glsl_type
*index_type
=
124 b
->options
->lower_ubo_ssbo_access_to_offsets
?
125 glsl_uint_type() : nir_address_format_to_glsl_type(addr_format
);
127 instr
->num_components
= glsl_get_vector_elements(index_type
);
128 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, instr
->num_components
,
129 glsl_get_bit_size(index_type
), NULL
);
130 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
132 return &instr
->dest
.ssa
;
136 vtn_resource_reindex(struct vtn_builder
*b
, enum vtn_variable_mode mode
,
137 nir_ssa_def
*base_index
, nir_ssa_def
*offset_index
)
139 vtn_assert(b
->options
->environment
== NIR_SPIRV_VULKAN
);
141 nir_intrinsic_instr
*instr
=
142 nir_intrinsic_instr_create(b
->nb
.shader
,
143 nir_intrinsic_vulkan_resource_reindex
);
144 instr
->src
[0] = nir_src_for_ssa(base_index
);
145 instr
->src
[1] = nir_src_for_ssa(offset_index
);
146 nir_intrinsic_set_desc_type(instr
, vk_desc_type_for_mode(b
, mode
));
148 vtn_fail_if(mode
!= vtn_variable_mode_ubo
&& mode
!= vtn_variable_mode_ssbo
,
149 "Invalid mode for vulkan_resource_reindex");
151 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
152 const struct glsl_type
*index_type
=
153 b
->options
->lower_ubo_ssbo_access_to_offsets
?
154 glsl_uint_type() : nir_address_format_to_glsl_type(addr_format
);
156 instr
->num_components
= glsl_get_vector_elements(index_type
);
157 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, instr
->num_components
,
158 glsl_get_bit_size(index_type
), NULL
);
159 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
161 return &instr
->dest
.ssa
;
165 vtn_descriptor_load(struct vtn_builder
*b
, enum vtn_variable_mode mode
,
166 nir_ssa_def
*desc_index
)
168 vtn_assert(b
->options
->environment
== NIR_SPIRV_VULKAN
);
170 nir_intrinsic_instr
*desc_load
=
171 nir_intrinsic_instr_create(b
->nb
.shader
,
172 nir_intrinsic_load_vulkan_descriptor
);
173 desc_load
->src
[0] = nir_src_for_ssa(desc_index
);
174 nir_intrinsic_set_desc_type(desc_load
, vk_desc_type_for_mode(b
, mode
));
176 vtn_fail_if(mode
!= vtn_variable_mode_ubo
&& mode
!= vtn_variable_mode_ssbo
,
177 "Invalid mode for load_vulkan_descriptor");
179 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
180 const struct glsl_type
*ptr_type
=
181 nir_address_format_to_glsl_type(addr_format
);
183 desc_load
->num_components
= glsl_get_vector_elements(ptr_type
);
184 nir_ssa_dest_init(&desc_load
->instr
, &desc_load
->dest
,
185 desc_load
->num_components
,
186 glsl_get_bit_size(ptr_type
), NULL
);
187 nir_builder_instr_insert(&b
->nb
, &desc_load
->instr
);
189 return &desc_load
->dest
.ssa
;
192 /* Dereference the given base pointer by the access chain */
193 static struct vtn_pointer
*
194 vtn_nir_deref_pointer_dereference(struct vtn_builder
*b
,
195 struct vtn_pointer
*base
,
196 struct vtn_access_chain
*deref_chain
)
198 struct vtn_type
*type
= base
->type
;
199 enum gl_access_qualifier access
= base
->access
;
202 nir_deref_instr
*tail
;
205 } else if (b
->options
->environment
== NIR_SPIRV_VULKAN
&&
206 vtn_pointer_is_external_block(b
, base
)) {
207 nir_ssa_def
*block_index
= base
->block_index
;
209 /* We dereferencing an external block pointer. Correctness of this
210 * operation relies on one particular line in the SPIR-V spec, section
211 * entitled "Validation Rules for Shader Capabilities":
213 * "Block and BufferBlock decorations cannot decorate a structure
214 * type that is nested at any level inside another structure type
215 * decorated with Block or BufferBlock."
217 * This means that we can detect the point where we cross over from
218 * descriptor indexing to buffer indexing by looking for the block
219 * decorated struct type. Anything before the block decorated struct
220 * type is a descriptor indexing operation and anything after the block
221 * decorated struct is a buffer offset operation.
224 /* Figure out the descriptor array index if any
226 * Some of the Vulkan CTS tests with hand-rolled SPIR-V have been known
227 * to forget the Block or BufferBlock decoration from time to time.
228 * It's more robust if we check for both !block_index and for the type
229 * to contain a block. This way there's a decent chance that arrays of
230 * UBOs/SSBOs will work correctly even if variable pointers are
233 nir_ssa_def
*desc_arr_idx
= NULL
;
234 if (!block_index
|| vtn_type_contains_block(b
, type
)) {
235 /* If our type contains a block, then we're still outside the block
236 * and we need to process enough levels of dereferences to get inside
239 if (deref_chain
->ptr_as_array
) {
240 unsigned aoa_size
= glsl_get_aoa_size(type
->type
);
241 desc_arr_idx
= vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
242 MAX2(aoa_size
, 1), 32);
246 for (; idx
< deref_chain
->length
; idx
++) {
247 if (type
->base_type
!= vtn_base_type_array
) {
248 vtn_assert(type
->base_type
== vtn_base_type_struct
);
252 unsigned aoa_size
= glsl_get_aoa_size(type
->array_element
->type
);
253 nir_ssa_def
*arr_offset
=
254 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
255 MAX2(aoa_size
, 1), 32);
257 desc_arr_idx
= nir_iadd(&b
->nb
, desc_arr_idx
, arr_offset
);
259 desc_arr_idx
= arr_offset
;
261 type
= type
->array_element
;
262 access
|= type
->access
;
267 vtn_assert(base
->var
&& base
->type
);
268 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
269 } else if (desc_arr_idx
) {
270 block_index
= vtn_resource_reindex(b
, base
->mode
,
271 block_index
, desc_arr_idx
);
274 if (idx
== deref_chain
->length
) {
275 /* The entire deref was consumed in finding the block index. Return
276 * a pointer which just has a block index and a later access chain
277 * will dereference deeper.
279 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
280 ptr
->mode
= base
->mode
;
282 ptr
->block_index
= block_index
;
283 ptr
->access
= access
;
287 /* If we got here, there's more access chain to handle and we have the
288 * final block index. Insert a descriptor load and cast to a deref to
289 * start the deref chain.
291 nir_ssa_def
*desc
= vtn_descriptor_load(b
, base
->mode
, block_index
);
293 assert(base
->mode
== vtn_variable_mode_ssbo
||
294 base
->mode
== vtn_variable_mode_ubo
);
295 nir_variable_mode nir_mode
=
296 base
->mode
== vtn_variable_mode_ssbo
? nir_var_mem_ssbo
: nir_var_mem_ubo
;
298 tail
= nir_build_deref_cast(&b
->nb
, desc
, nir_mode
, type
->type
,
299 base
->ptr_type
->stride
);
301 assert(base
->var
&& base
->var
->var
);
302 tail
= nir_build_deref_var(&b
->nb
, base
->var
->var
);
303 if (base
->ptr_type
&& base
->ptr_type
->type
) {
304 tail
->dest
.ssa
.num_components
=
305 glsl_get_vector_elements(base
->ptr_type
->type
);
306 tail
->dest
.ssa
.bit_size
= glsl_get_bit_size(base
->ptr_type
->type
);
310 if (idx
== 0 && deref_chain
->ptr_as_array
) {
311 /* We start with a deref cast to get the stride. Hopefully, we'll be
312 * able to delete that cast eventually.
314 tail
= nir_build_deref_cast(&b
->nb
, &tail
->dest
.ssa
, tail
->mode
,
315 tail
->type
, base
->ptr_type
->stride
);
317 nir_ssa_def
*index
= vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1,
318 tail
->dest
.ssa
.bit_size
);
319 tail
= nir_build_deref_ptr_as_array(&b
->nb
, tail
, index
);
323 for (; idx
< deref_chain
->length
; idx
++) {
324 if (glsl_type_is_struct_or_ifc(type
->type
)) {
325 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
326 unsigned field
= deref_chain
->link
[idx
].id
;
327 tail
= nir_build_deref_struct(&b
->nb
, tail
, field
);
328 type
= type
->members
[field
];
330 nir_ssa_def
*arr_index
=
331 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
], 1,
332 tail
->dest
.ssa
.bit_size
);
333 tail
= nir_build_deref_array(&b
->nb
, tail
, arr_index
);
334 type
= type
->array_element
;
337 access
|= type
->access
;
340 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
341 ptr
->mode
= base
->mode
;
343 ptr
->var
= base
->var
;
345 ptr
->access
= access
;
350 static struct vtn_pointer
*
351 vtn_ssa_offset_pointer_dereference(struct vtn_builder
*b
,
352 struct vtn_pointer
*base
,
353 struct vtn_access_chain
*deref_chain
)
355 nir_ssa_def
*block_index
= base
->block_index
;
356 nir_ssa_def
*offset
= base
->offset
;
357 struct vtn_type
*type
= base
->type
;
358 enum gl_access_qualifier access
= base
->access
;
361 if (base
->mode
== vtn_variable_mode_ubo
||
362 base
->mode
== vtn_variable_mode_ssbo
) {
364 vtn_assert(base
->var
&& base
->type
);
365 nir_ssa_def
*desc_arr_idx
;
366 if (glsl_type_is_array(type
->type
)) {
367 if (deref_chain
->length
>= 1) {
369 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
371 /* This consumes a level of type */
372 type
= type
->array_element
;
373 access
|= type
->access
;
375 /* This is annoying. We've been asked for a pointer to the
376 * array of UBOs/SSBOs and not a specifc buffer. Return a
377 * pointer with a descriptor index of 0 and we'll have to do
378 * a reindex later to adjust it to the right thing.
380 desc_arr_idx
= nir_imm_int(&b
->nb
, 0);
382 } else if (deref_chain
->ptr_as_array
) {
383 /* You can't have a zero-length OpPtrAccessChain */
384 vtn_assert(deref_chain
->length
>= 1);
385 desc_arr_idx
= vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
387 /* We have a regular non-array SSBO. */
390 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
391 } else if (deref_chain
->ptr_as_array
&&
392 type
->base_type
== vtn_base_type_struct
&& type
->block
) {
393 /* We are doing an OpPtrAccessChain on a pointer to a struct that is
394 * decorated block. This is an interesting corner in the SPIR-V
395 * spec. One interpretation would be that they client is clearly
396 * trying to treat that block as if it's an implicit array of blocks
397 * repeated in the buffer. However, the SPIR-V spec for the
398 * OpPtrAccessChain says:
400 * "Base is treated as the address of the first element of an
401 * array, and the Element element’s address is computed to be the
402 * base for the Indexes, as per OpAccessChain."
404 * Taken literally, that would mean that your struct type is supposed
405 * to be treated as an array of such a struct and, since it's
406 * decorated block, that means an array of blocks which corresponds
407 * to an array descriptor. Therefore, we need to do a reindex
408 * operation to add the index from the first link in the access chain
409 * to the index we recieved.
411 * The downside to this interpretation (there always is one) is that
412 * this might be somewhat surprising behavior to apps if they expect
413 * the implicit array behavior described above.
415 vtn_assert(deref_chain
->length
>= 1);
416 nir_ssa_def
*offset_index
=
417 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
420 block_index
= vtn_resource_reindex(b
, base
->mode
,
421 block_index
, offset_index
);
426 if (base
->mode
== vtn_variable_mode_workgroup
) {
427 /* SLM doesn't need nor have a block index */
428 vtn_assert(!block_index
);
430 /* We need the variable for the base offset */
431 vtn_assert(base
->var
);
433 /* We need ptr_type for size and alignment */
434 vtn_assert(base
->ptr_type
);
436 /* Assign location on first use so that we don't end up bloating SLM
437 * address space for variables which are never statically used.
439 if (base
->var
->shared_location
< 0) {
440 vtn_assert(base
->ptr_type
->length
> 0 && base
->ptr_type
->align
> 0);
441 b
->shader
->num_shared
= vtn_align_u32(b
->shader
->num_shared
,
442 base
->ptr_type
->align
);
443 base
->var
->shared_location
= b
->shader
->num_shared
;
444 b
->shader
->num_shared
+= base
->ptr_type
->length
;
447 offset
= nir_imm_int(&b
->nb
, base
->var
->shared_location
);
448 } else if (base
->mode
== vtn_variable_mode_push_constant
) {
449 /* Push constants neither need nor have a block index */
450 vtn_assert(!block_index
);
452 /* Start off with at the start of the push constant block. */
453 offset
= nir_imm_int(&b
->nb
, 0);
455 /* The code above should have ensured a block_index when needed. */
456 vtn_assert(block_index
);
458 /* Start off with at the start of the buffer. */
459 offset
= nir_imm_int(&b
->nb
, 0);
463 if (deref_chain
->ptr_as_array
&& idx
== 0) {
464 /* We need ptr_type for the stride */
465 vtn_assert(base
->ptr_type
);
467 /* We need at least one element in the chain */
468 vtn_assert(deref_chain
->length
>= 1);
470 nir_ssa_def
*elem_offset
=
471 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
472 base
->ptr_type
->stride
, offset
->bit_size
);
473 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
477 for (; idx
< deref_chain
->length
; idx
++) {
478 switch (glsl_get_base_type(type
->type
)) {
481 case GLSL_TYPE_UINT16
:
482 case GLSL_TYPE_INT16
:
483 case GLSL_TYPE_UINT8
:
485 case GLSL_TYPE_UINT64
:
486 case GLSL_TYPE_INT64
:
487 case GLSL_TYPE_FLOAT
:
488 case GLSL_TYPE_FLOAT16
:
489 case GLSL_TYPE_DOUBLE
:
491 case GLSL_TYPE_ARRAY
: {
492 nir_ssa_def
*elem_offset
=
493 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
494 type
->stride
, offset
->bit_size
);
495 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
496 type
= type
->array_element
;
497 access
|= type
->access
;
501 case GLSL_TYPE_INTERFACE
:
502 case GLSL_TYPE_STRUCT
: {
503 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
504 unsigned member
= deref_chain
->link
[idx
].id
;
505 offset
= nir_iadd_imm(&b
->nb
, offset
, type
->offsets
[member
]);
506 type
= type
->members
[member
];
507 access
|= type
->access
;
512 vtn_fail("Invalid type for deref");
516 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
517 ptr
->mode
= base
->mode
;
519 ptr
->block_index
= block_index
;
520 ptr
->offset
= offset
;
521 ptr
->access
= access
;
526 /* Dereference the given base pointer by the access chain */
527 static struct vtn_pointer
*
528 vtn_pointer_dereference(struct vtn_builder
*b
,
529 struct vtn_pointer
*base
,
530 struct vtn_access_chain
*deref_chain
)
532 if (vtn_pointer_uses_ssa_offset(b
, base
)) {
533 return vtn_ssa_offset_pointer_dereference(b
, base
, deref_chain
);
535 return vtn_nir_deref_pointer_dereference(b
, base
, deref_chain
);
540 vtn_pointer_for_variable(struct vtn_builder
*b
,
541 struct vtn_variable
*var
, struct vtn_type
*ptr_type
)
543 struct vtn_pointer
*pointer
= rzalloc(b
, struct vtn_pointer
);
545 pointer
->mode
= var
->mode
;
546 pointer
->type
= var
->type
;
547 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
548 vtn_assert(ptr_type
->deref
->type
== var
->type
->type
);
549 pointer
->ptr_type
= ptr_type
;
551 pointer
->access
= var
->access
| var
->type
->access
;
556 /* Returns an atomic_uint type based on the original uint type. The returned
557 * type will be equivalent to the original one but will have an atomic_uint
558 * type as leaf instead of an uint.
560 * Manages uint scalars, arrays, and arrays of arrays of any nested depth.
562 static const struct glsl_type
*
563 repair_atomic_type(const struct glsl_type
*type
)
565 assert(glsl_get_base_type(glsl_without_array(type
)) == GLSL_TYPE_UINT
);
566 assert(glsl_type_is_scalar(glsl_without_array(type
)));
568 if (glsl_type_is_array(type
)) {
569 const struct glsl_type
*atomic
=
570 repair_atomic_type(glsl_get_array_element(type
));
572 return glsl_array_type(atomic
, glsl_get_length(type
),
573 glsl_get_explicit_stride(type
));
575 return glsl_atomic_uint_type();
580 vtn_pointer_to_deref(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
582 if (b
->wa_glslang_179
) {
583 /* Do on-the-fly copy propagation for samplers. */
584 if (ptr
->var
&& ptr
->var
->copy_prop_sampler
)
585 return vtn_pointer_to_deref(b
, ptr
->var
->copy_prop_sampler
);
588 vtn_assert(!vtn_pointer_uses_ssa_offset(b
, ptr
));
590 struct vtn_access_chain chain
= {
593 ptr
= vtn_nir_deref_pointer_dereference(b
, ptr
, &chain
);
600 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_instr
*deref
,
601 struct vtn_ssa_value
*inout
,
602 enum gl_access_qualifier access
)
604 if (glsl_type_is_vector_or_scalar(deref
->type
)) {
606 inout
->def
= nir_load_deref_with_access(&b
->nb
, deref
, access
);
608 nir_store_deref_with_access(&b
->nb
, deref
, inout
->def
, ~0, access
);
610 } else if (glsl_type_is_array(deref
->type
) ||
611 glsl_type_is_matrix(deref
->type
)) {
612 unsigned elems
= glsl_get_length(deref
->type
);
613 for (unsigned i
= 0; i
< elems
; i
++) {
614 nir_deref_instr
*child
=
615 nir_build_deref_array_imm(&b
->nb
, deref
, i
);
616 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
], access
);
619 vtn_assert(glsl_type_is_struct_or_ifc(deref
->type
));
620 unsigned elems
= glsl_get_length(deref
->type
);
621 for (unsigned i
= 0; i
< elems
; i
++) {
622 nir_deref_instr
*child
= nir_build_deref_struct(&b
->nb
, deref
, i
);
623 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
], access
);
629 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
631 struct vtn_pointer
*ptr
= vtn_value(b
, id
, vtn_value_type_pointer
)->pointer
;
632 return vtn_pointer_to_deref(b
, ptr
);
636 * Gets the NIR-level deref tail, which may have as a child an array deref
637 * selecting which component due to OpAccessChain supporting per-component
638 * indexing in SPIR-V.
640 static nir_deref_instr
*
641 get_deref_tail(nir_deref_instr
*deref
)
643 if (deref
->deref_type
!= nir_deref_type_array
)
646 nir_deref_instr
*parent
=
647 nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
);
649 if (glsl_type_is_vector(parent
->type
))
655 struct vtn_ssa_value
*
656 vtn_local_load(struct vtn_builder
*b
, nir_deref_instr
*src
,
657 enum gl_access_qualifier access
)
659 nir_deref_instr
*src_tail
= get_deref_tail(src
);
660 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
661 _vtn_local_load_store(b
, true, src_tail
, val
, access
);
663 if (src_tail
!= src
) {
664 val
->type
= src
->type
;
665 if (nir_src_is_const(src
->arr
.index
))
666 val
->def
= vtn_vector_extract(b
, val
->def
,
667 nir_src_as_uint(src
->arr
.index
));
669 val
->def
= vtn_vector_extract_dynamic(b
, val
->def
, src
->arr
.index
.ssa
);
676 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
677 nir_deref_instr
*dest
, enum gl_access_qualifier access
)
679 nir_deref_instr
*dest_tail
= get_deref_tail(dest
);
681 if (dest_tail
!= dest
) {
682 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
683 _vtn_local_load_store(b
, true, dest_tail
, val
, access
);
685 if (nir_src_is_const(dest
->arr
.index
))
686 val
->def
= vtn_vector_insert(b
, val
->def
, src
->def
,
687 nir_src_as_uint(dest
->arr
.index
));
689 val
->def
= vtn_vector_insert_dynamic(b
, val
->def
, src
->def
,
690 dest
->arr
.index
.ssa
);
691 _vtn_local_load_store(b
, false, dest_tail
, val
, access
);
693 _vtn_local_load_store(b
, false, dest_tail
, src
, access
);
698 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
699 nir_ssa_def
**index_out
)
701 assert(vtn_pointer_uses_ssa_offset(b
, ptr
));
703 struct vtn_access_chain chain
= {
706 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
708 *index_out
= ptr
->block_index
;
712 /* Tries to compute the size of an interface block based on the strides and
713 * offsets that are provided to us in the SPIR-V source.
716 vtn_type_block_size(struct vtn_builder
*b
, struct vtn_type
*type
)
718 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
722 case GLSL_TYPE_UINT16
:
723 case GLSL_TYPE_INT16
:
724 case GLSL_TYPE_UINT8
:
726 case GLSL_TYPE_UINT64
:
727 case GLSL_TYPE_INT64
:
728 case GLSL_TYPE_FLOAT
:
729 case GLSL_TYPE_FLOAT16
:
731 case GLSL_TYPE_DOUBLE
: {
732 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
733 glsl_get_matrix_columns(type
->type
);
735 vtn_assert(type
->stride
> 0);
736 return type
->stride
* cols
;
738 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
739 return glsl_get_vector_elements(type
->type
) * type_size
;
743 case GLSL_TYPE_STRUCT
:
744 case GLSL_TYPE_INTERFACE
: {
746 unsigned num_fields
= glsl_get_length(type
->type
);
747 for (unsigned f
= 0; f
< num_fields
; f
++) {
748 unsigned field_end
= type
->offsets
[f
] +
749 vtn_type_block_size(b
, type
->members
[f
]);
750 size
= MAX2(size
, field_end
);
755 case GLSL_TYPE_ARRAY
:
756 vtn_assert(type
->stride
> 0);
757 vtn_assert(glsl_get_length(type
->type
) > 0);
758 return type
->stride
* glsl_get_length(type
->type
);
761 vtn_fail("Invalid block type");
767 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
768 nir_ssa_def
*index
, nir_ssa_def
*offset
,
769 unsigned access_offset
, unsigned access_size
,
770 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
,
771 enum gl_access_qualifier access
)
773 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
774 instr
->num_components
= glsl_get_vector_elements(type
);
776 /* Booleans usually shouldn't show up in external memory in SPIR-V.
777 * However, they do for certain older GLSLang versions and can for shared
778 * memory when we lower access chains internally.
780 const unsigned data_bit_size
= glsl_type_is_boolean(type
) ? 32 :
781 glsl_get_bit_size(type
);
785 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
786 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
789 if (op
== nir_intrinsic_load_push_constant
) {
790 nir_intrinsic_set_base(instr
, access_offset
);
791 nir_intrinsic_set_range(instr
, access_size
);
794 if (op
== nir_intrinsic_load_ubo
||
795 op
== nir_intrinsic_load_ssbo
||
796 op
== nir_intrinsic_store_ssbo
) {
797 nir_intrinsic_set_access(instr
, access
);
800 /* With extensions like relaxed_block_layout, we really can't guarantee
801 * much more than scalar alignment.
803 if (op
!= nir_intrinsic_load_push_constant
)
804 nir_intrinsic_set_align(instr
, data_bit_size
/ 8, 0);
807 instr
->src
[src
++] = nir_src_for_ssa(index
);
809 if (op
== nir_intrinsic_load_push_constant
) {
810 /* We need to subtract the offset from where the intrinsic will load the
813 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
814 nir_imm_int(&b
->nb
, access_offset
)));
816 instr
->src
[src
++] = nir_src_for_ssa(offset
);
820 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
821 instr
->num_components
, data_bit_size
, NULL
);
822 (*inout
)->def
= &instr
->dest
.ssa
;
825 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
827 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
828 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
832 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
833 nir_ssa_def
*index
, nir_ssa_def
*offset
,
834 unsigned access_offset
, unsigned access_size
,
835 struct vtn_type
*type
, enum gl_access_qualifier access
,
836 struct vtn_ssa_value
**inout
)
838 if (load
&& *inout
== NULL
)
839 *inout
= vtn_create_ssa_value(b
, type
->type
);
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
)
973 unsigned access_offset
= 0, access_size
= 0;
975 case vtn_variable_mode_ubo
:
976 op
= nir_intrinsic_load_ubo
;
978 case vtn_variable_mode_ssbo
:
979 op
= nir_intrinsic_load_ssbo
;
981 case vtn_variable_mode_push_constant
:
982 op
= nir_intrinsic_load_push_constant
;
983 access_size
= b
->shader
->num_uniforms
;
985 case vtn_variable_mode_workgroup
:
986 op
= nir_intrinsic_load_shared
;
989 vtn_fail("Invalid block variable mode");
992 nir_ssa_def
*offset
, *index
= NULL
;
993 offset
= vtn_pointer_to_offset(b
, src
, &index
);
995 struct vtn_ssa_value
*value
= NULL
;
996 _vtn_block_load_store(b
, op
, true, index
, offset
,
997 access_offset
, access_size
,
998 src
->type
, src
->access
, &value
);
1003 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
1004 struct vtn_pointer
*dst
)
1006 nir_intrinsic_op op
;
1007 switch (dst
->mode
) {
1008 case vtn_variable_mode_ssbo
:
1009 op
= nir_intrinsic_store_ssbo
;
1011 case vtn_variable_mode_workgroup
:
1012 op
= nir_intrinsic_store_shared
;
1015 vtn_fail("Invalid block variable mode");
1018 nir_ssa_def
*offset
, *index
= NULL
;
1019 offset
= vtn_pointer_to_offset(b
, dst
, &index
);
1021 _vtn_block_load_store(b
, op
, false, index
, offset
,
1022 0, 0, dst
->type
, dst
->access
, &src
);
1026 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
1027 struct vtn_pointer
*ptr
,
1028 enum gl_access_qualifier access
,
1029 struct vtn_ssa_value
**inout
)
1031 enum glsl_base_type base_type
= glsl_get_base_type(ptr
->type
->type
);
1032 switch (base_type
) {
1033 case GLSL_TYPE_UINT
:
1035 case GLSL_TYPE_UINT16
:
1036 case GLSL_TYPE_INT16
:
1037 case GLSL_TYPE_UINT8
:
1038 case GLSL_TYPE_INT8
:
1039 case GLSL_TYPE_UINT64
:
1040 case GLSL_TYPE_INT64
:
1041 case GLSL_TYPE_FLOAT
:
1042 case GLSL_TYPE_FLOAT16
:
1043 case GLSL_TYPE_BOOL
:
1044 case GLSL_TYPE_DOUBLE
:
1045 if (glsl_type_is_vector_or_scalar(ptr
->type
->type
)) {
1046 /* We hit a vector or scalar; go ahead and emit the load[s] */
1047 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
1048 if (vtn_pointer_is_external_block(b
, ptr
)) {
1049 /* If it's external, we call nir_load/store_deref directly. The
1050 * vtn_local_load/store helpers are too clever and do magic to
1051 * avoid array derefs of vectors. That magic is both less
1052 * efficient than the direct load/store and, in the case of
1053 * stores, is broken because it creates a race condition if two
1054 * threads are writing to different components of the same vector
1055 * due to the load+insert+store it uses to emulate the array
1059 *inout
= vtn_create_ssa_value(b
, ptr
->type
->type
);
1060 (*inout
)->def
= nir_load_deref_with_access(&b
->nb
, deref
,
1061 ptr
->type
->access
| access
);
1063 nir_store_deref_with_access(&b
->nb
, deref
, (*inout
)->def
, ~0,
1064 ptr
->type
->access
| access
);
1068 *inout
= vtn_local_load(b
, deref
, ptr
->type
->access
| access
);
1070 vtn_local_store(b
, *inout
, deref
, ptr
->type
->access
| access
);
1077 case GLSL_TYPE_INTERFACE
:
1078 case GLSL_TYPE_ARRAY
:
1079 case GLSL_TYPE_STRUCT
: {
1080 unsigned elems
= glsl_get_length(ptr
->type
->type
);
1082 vtn_assert(*inout
== NULL
);
1083 *inout
= rzalloc(b
, struct vtn_ssa_value
);
1084 (*inout
)->type
= ptr
->type
->type
;
1085 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
1088 struct vtn_access_chain chain
= {
1091 { .mode
= vtn_access_mode_literal
, },
1094 for (unsigned i
= 0; i
< elems
; i
++) {
1095 chain
.link
[0].id
= i
;
1096 struct vtn_pointer
*elem
= vtn_pointer_dereference(b
, ptr
, &chain
);
1097 _vtn_variable_load_store(b
, load
, elem
, ptr
->type
->access
| access
,
1098 &(*inout
)->elems
[i
]);
1104 vtn_fail("Invalid access chain type");
1108 struct vtn_ssa_value
*
1109 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
1111 if (vtn_pointer_uses_ssa_offset(b
, src
)) {
1112 return vtn_block_load(b
, src
);
1114 struct vtn_ssa_value
*val
= NULL
;
1115 _vtn_variable_load_store(b
, true, src
, src
->access
, &val
);
1121 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
1122 struct vtn_pointer
*dest
)
1124 if (vtn_pointer_uses_ssa_offset(b
, dest
)) {
1125 vtn_assert(dest
->mode
== vtn_variable_mode_ssbo
||
1126 dest
->mode
== vtn_variable_mode_workgroup
);
1127 vtn_block_store(b
, src
, dest
);
1129 _vtn_variable_load_store(b
, false, dest
, dest
->access
, &src
);
1134 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
1135 struct vtn_pointer
*src
)
1137 vtn_assert(src
->type
->type
== dest
->type
->type
);
1138 enum glsl_base_type base_type
= glsl_get_base_type(src
->type
->type
);
1139 switch (base_type
) {
1140 case GLSL_TYPE_UINT
:
1142 case GLSL_TYPE_UINT16
:
1143 case GLSL_TYPE_INT16
:
1144 case GLSL_TYPE_UINT8
:
1145 case GLSL_TYPE_INT8
:
1146 case GLSL_TYPE_UINT64
:
1147 case GLSL_TYPE_INT64
:
1148 case GLSL_TYPE_FLOAT
:
1149 case GLSL_TYPE_FLOAT16
:
1150 case GLSL_TYPE_DOUBLE
:
1151 case GLSL_TYPE_BOOL
:
1152 /* At this point, we have a scalar, vector, or matrix so we know that
1153 * there cannot be any structure splitting still in the way. By
1154 * stopping at the matrix level rather than the vector level, we
1155 * ensure that matrices get loaded in the optimal way even if they
1156 * are storred row-major in a UBO.
1158 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
1161 case GLSL_TYPE_INTERFACE
:
1162 case GLSL_TYPE_ARRAY
:
1163 case GLSL_TYPE_STRUCT
: {
1164 struct vtn_access_chain chain
= {
1167 { .mode
= vtn_access_mode_literal
, },
1170 unsigned elems
= glsl_get_length(src
->type
->type
);
1171 for (unsigned i
= 0; i
< elems
; i
++) {
1172 chain
.link
[0].id
= i
;
1173 struct vtn_pointer
*src_elem
=
1174 vtn_pointer_dereference(b
, src
, &chain
);
1175 struct vtn_pointer
*dest_elem
=
1176 vtn_pointer_dereference(b
, dest
, &chain
);
1178 _vtn_variable_copy(b
, dest_elem
, src_elem
);
1184 vtn_fail("Invalid access chain type");
1189 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
1190 struct vtn_pointer
*src
)
1192 /* TODO: At some point, we should add a special-case for when we can
1193 * just emit a copy_var intrinsic.
1195 _vtn_variable_copy(b
, dest
, src
);
1199 set_mode_system_value(struct vtn_builder
*b
, nir_variable_mode
*mode
)
1201 vtn_assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
1202 *mode
= nir_var_system_value
;
1206 vtn_get_builtin_location(struct vtn_builder
*b
,
1207 SpvBuiltIn builtin
, int *location
,
1208 nir_variable_mode
*mode
)
1211 case SpvBuiltInPosition
:
1212 *location
= VARYING_SLOT_POS
;
1214 case SpvBuiltInPointSize
:
1215 *location
= VARYING_SLOT_PSIZ
;
1217 case SpvBuiltInClipDistance
:
1218 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
1220 case SpvBuiltInCullDistance
:
1221 *location
= VARYING_SLOT_CULL_DIST0
;
1223 case SpvBuiltInVertexId
:
1224 case SpvBuiltInVertexIndex
:
1225 /* The Vulkan spec defines VertexIndex to be non-zero-based and doesn't
1226 * allow VertexId. The ARB_gl_spirv spec defines VertexId to be the
1227 * same as gl_VertexID, which is non-zero-based, and removes
1228 * VertexIndex. Since they're both defined to be non-zero-based, we use
1229 * SYSTEM_VALUE_VERTEX_ID for both.
1231 *location
= SYSTEM_VALUE_VERTEX_ID
;
1232 set_mode_system_value(b
, mode
);
1234 case SpvBuiltInInstanceIndex
:
1235 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
1236 set_mode_system_value(b
, mode
);
1238 case SpvBuiltInInstanceId
:
1239 *location
= SYSTEM_VALUE_INSTANCE_ID
;
1240 set_mode_system_value(b
, mode
);
1242 case SpvBuiltInPrimitiveId
:
1243 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1244 vtn_assert(*mode
== nir_var_shader_in
);
1245 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1246 } else if (*mode
== nir_var_shader_out
) {
1247 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1249 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
1250 set_mode_system_value(b
, mode
);
1253 case SpvBuiltInInvocationId
:
1254 *location
= SYSTEM_VALUE_INVOCATION_ID
;
1255 set_mode_system_value(b
, mode
);
1257 case SpvBuiltInLayer
:
1258 *location
= VARYING_SLOT_LAYER
;
1259 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1260 *mode
= nir_var_shader_in
;
1261 else if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1262 *mode
= nir_var_shader_out
;
1263 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1264 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1265 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1266 *mode
= nir_var_shader_out
;
1268 vtn_fail("invalid stage for SpvBuiltInLayer");
1270 case SpvBuiltInViewportIndex
:
1271 *location
= VARYING_SLOT_VIEWPORT
;
1272 if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1273 *mode
= nir_var_shader_out
;
1274 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1275 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1276 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1277 *mode
= nir_var_shader_out
;
1278 else if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1279 *mode
= nir_var_shader_in
;
1281 vtn_fail("invalid stage for SpvBuiltInViewportIndex");
1283 case SpvBuiltInTessLevelOuter
:
1284 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
1286 case SpvBuiltInTessLevelInner
:
1287 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
1289 case SpvBuiltInTessCoord
:
1290 *location
= SYSTEM_VALUE_TESS_COORD
;
1291 set_mode_system_value(b
, mode
);
1293 case SpvBuiltInPatchVertices
:
1294 *location
= SYSTEM_VALUE_VERTICES_IN
;
1295 set_mode_system_value(b
, mode
);
1297 case SpvBuiltInFragCoord
:
1298 vtn_assert(*mode
== nir_var_shader_in
);
1299 if (b
->options
&& b
->options
->frag_coord_is_sysval
) {
1300 *mode
= nir_var_system_value
;
1301 *location
= SYSTEM_VALUE_FRAG_COORD
;
1303 *location
= VARYING_SLOT_POS
;
1306 case SpvBuiltInPointCoord
:
1307 *location
= VARYING_SLOT_PNTC
;
1308 vtn_assert(*mode
== nir_var_shader_in
);
1310 case SpvBuiltInFrontFacing
:
1311 *location
= SYSTEM_VALUE_FRONT_FACE
;
1312 set_mode_system_value(b
, mode
);
1314 case SpvBuiltInSampleId
:
1315 *location
= SYSTEM_VALUE_SAMPLE_ID
;
1316 set_mode_system_value(b
, mode
);
1318 case SpvBuiltInSamplePosition
:
1319 *location
= SYSTEM_VALUE_SAMPLE_POS
;
1320 set_mode_system_value(b
, mode
);
1322 case SpvBuiltInSampleMask
:
1323 if (*mode
== nir_var_shader_out
) {
1324 *location
= FRAG_RESULT_SAMPLE_MASK
;
1326 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1327 set_mode_system_value(b
, mode
);
1330 case SpvBuiltInFragDepth
:
1331 *location
= FRAG_RESULT_DEPTH
;
1332 vtn_assert(*mode
== nir_var_shader_out
);
1334 case SpvBuiltInHelperInvocation
:
1335 *location
= SYSTEM_VALUE_HELPER_INVOCATION
;
1336 set_mode_system_value(b
, mode
);
1338 case SpvBuiltInNumWorkgroups
:
1339 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1340 set_mode_system_value(b
, mode
);
1342 case SpvBuiltInWorkgroupSize
:
1343 *location
= SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
1344 set_mode_system_value(b
, mode
);
1346 case SpvBuiltInWorkgroupId
:
1347 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1348 set_mode_system_value(b
, mode
);
1350 case SpvBuiltInLocalInvocationId
:
1351 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1352 set_mode_system_value(b
, mode
);
1354 case SpvBuiltInLocalInvocationIndex
:
1355 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1356 set_mode_system_value(b
, mode
);
1358 case SpvBuiltInGlobalInvocationId
:
1359 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1360 set_mode_system_value(b
, mode
);
1362 case SpvBuiltInGlobalLinearId
:
1363 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX
;
1364 set_mode_system_value(b
, mode
);
1366 case SpvBuiltInBaseVertex
:
1367 /* OpenGL gl_BaseVertex (SYSTEM_VALUE_BASE_VERTEX) is not the same
1368 * semantic as SPIR-V BaseVertex (SYSTEM_VALUE_FIRST_VERTEX).
1370 *location
= SYSTEM_VALUE_FIRST_VERTEX
;
1371 set_mode_system_value(b
, mode
);
1373 case SpvBuiltInBaseInstance
:
1374 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1375 set_mode_system_value(b
, mode
);
1377 case SpvBuiltInDrawIndex
:
1378 *location
= SYSTEM_VALUE_DRAW_ID
;
1379 set_mode_system_value(b
, mode
);
1381 case SpvBuiltInSubgroupSize
:
1382 *location
= SYSTEM_VALUE_SUBGROUP_SIZE
;
1383 set_mode_system_value(b
, mode
);
1385 case SpvBuiltInSubgroupId
:
1386 *location
= SYSTEM_VALUE_SUBGROUP_ID
;
1387 set_mode_system_value(b
, mode
);
1389 case SpvBuiltInSubgroupLocalInvocationId
:
1390 *location
= SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1391 set_mode_system_value(b
, mode
);
1393 case SpvBuiltInNumSubgroups
:
1394 *location
= SYSTEM_VALUE_NUM_SUBGROUPS
;
1395 set_mode_system_value(b
, mode
);
1397 case SpvBuiltInDeviceIndex
:
1398 *location
= SYSTEM_VALUE_DEVICE_INDEX
;
1399 set_mode_system_value(b
, mode
);
1401 case SpvBuiltInViewIndex
:
1402 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1403 set_mode_system_value(b
, mode
);
1405 case SpvBuiltInSubgroupEqMask
:
1406 *location
= SYSTEM_VALUE_SUBGROUP_EQ_MASK
,
1407 set_mode_system_value(b
, mode
);
1409 case SpvBuiltInSubgroupGeMask
:
1410 *location
= SYSTEM_VALUE_SUBGROUP_GE_MASK
,
1411 set_mode_system_value(b
, mode
);
1413 case SpvBuiltInSubgroupGtMask
:
1414 *location
= SYSTEM_VALUE_SUBGROUP_GT_MASK
,
1415 set_mode_system_value(b
, mode
);
1417 case SpvBuiltInSubgroupLeMask
:
1418 *location
= SYSTEM_VALUE_SUBGROUP_LE_MASK
,
1419 set_mode_system_value(b
, mode
);
1421 case SpvBuiltInSubgroupLtMask
:
1422 *location
= SYSTEM_VALUE_SUBGROUP_LT_MASK
,
1423 set_mode_system_value(b
, mode
);
1425 case SpvBuiltInFragStencilRefEXT
:
1426 *location
= FRAG_RESULT_STENCIL
;
1427 vtn_assert(*mode
== nir_var_shader_out
);
1429 case SpvBuiltInWorkDim
:
1430 *location
= SYSTEM_VALUE_WORK_DIM
;
1431 set_mode_system_value(b
, mode
);
1433 case SpvBuiltInGlobalSize
:
1434 *location
= SYSTEM_VALUE_GLOBAL_GROUP_SIZE
;
1435 set_mode_system_value(b
, mode
);
1438 vtn_fail("Unsupported builtin: %s (%u)",
1439 spirv_builtin_to_string(builtin
), builtin
);
1444 apply_var_decoration(struct vtn_builder
*b
,
1445 struct nir_variable_data
*var_data
,
1446 const struct vtn_decoration
*dec
)
1448 switch (dec
->decoration
) {
1449 case SpvDecorationRelaxedPrecision
:
1450 break; /* FIXME: Do nothing with this for now. */
1451 case SpvDecorationNoPerspective
:
1452 var_data
->interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1454 case SpvDecorationFlat
:
1455 var_data
->interpolation
= INTERP_MODE_FLAT
;
1457 case SpvDecorationCentroid
:
1458 var_data
->centroid
= true;
1460 case SpvDecorationSample
:
1461 var_data
->sample
= true;
1463 case SpvDecorationInvariant
:
1464 var_data
->invariant
= true;
1466 case SpvDecorationConstant
:
1467 var_data
->read_only
= true;
1469 case SpvDecorationNonReadable
:
1470 var_data
->image
.access
|= ACCESS_NON_READABLE
;
1472 case SpvDecorationNonWritable
:
1473 var_data
->read_only
= true;
1474 var_data
->image
.access
|= ACCESS_NON_WRITEABLE
;
1476 case SpvDecorationRestrict
:
1477 var_data
->image
.access
|= ACCESS_RESTRICT
;
1479 case SpvDecorationVolatile
:
1480 var_data
->image
.access
|= ACCESS_VOLATILE
;
1482 case SpvDecorationCoherent
:
1483 var_data
->image
.access
|= ACCESS_COHERENT
;
1485 case SpvDecorationComponent
:
1486 var_data
->location_frac
= dec
->operands
[0];
1488 case SpvDecorationIndex
:
1489 var_data
->index
= dec
->operands
[0];
1491 case SpvDecorationBuiltIn
: {
1492 SpvBuiltIn builtin
= dec
->operands
[0];
1494 nir_variable_mode mode
= var_data
->mode
;
1495 vtn_get_builtin_location(b
, builtin
, &var_data
->location
, &mode
);
1496 var_data
->mode
= mode
;
1499 case SpvBuiltInTessLevelOuter
:
1500 case SpvBuiltInTessLevelInner
:
1501 case SpvBuiltInClipDistance
:
1502 case SpvBuiltInCullDistance
:
1503 var_data
->compact
= true;
1510 case SpvDecorationSpecId
:
1511 case SpvDecorationRowMajor
:
1512 case SpvDecorationColMajor
:
1513 case SpvDecorationMatrixStride
:
1514 case SpvDecorationAliased
:
1515 case SpvDecorationUniform
:
1516 case SpvDecorationUniformId
:
1517 case SpvDecorationLinkageAttributes
:
1518 break; /* Do nothing with these here */
1520 case SpvDecorationPatch
:
1521 var_data
->patch
= true;
1524 case SpvDecorationLocation
:
1525 vtn_fail("Handled above");
1527 case SpvDecorationBlock
:
1528 case SpvDecorationBufferBlock
:
1529 case SpvDecorationArrayStride
:
1530 case SpvDecorationGLSLShared
:
1531 case SpvDecorationGLSLPacked
:
1532 break; /* These can apply to a type but we don't care about them */
1534 case SpvDecorationBinding
:
1535 case SpvDecorationDescriptorSet
:
1536 case SpvDecorationNoContraction
:
1537 case SpvDecorationInputAttachmentIndex
:
1538 vtn_warn("Decoration not allowed for variable or structure member: %s",
1539 spirv_decoration_to_string(dec
->decoration
));
1542 case SpvDecorationXfbBuffer
:
1543 var_data
->explicit_xfb_buffer
= true;
1544 var_data
->xfb_buffer
= dec
->operands
[0];
1545 var_data
->always_active_io
= true;
1547 case SpvDecorationXfbStride
:
1548 var_data
->explicit_xfb_stride
= true;
1549 var_data
->xfb_stride
= dec
->operands
[0];
1551 case SpvDecorationOffset
:
1552 var_data
->explicit_offset
= true;
1553 var_data
->offset
= dec
->operands
[0];
1556 case SpvDecorationStream
:
1557 var_data
->stream
= dec
->operands
[0];
1560 case SpvDecorationCPacked
:
1561 case SpvDecorationSaturatedConversion
:
1562 case SpvDecorationFuncParamAttr
:
1563 case SpvDecorationFPRoundingMode
:
1564 case SpvDecorationFPFastMathMode
:
1565 case SpvDecorationAlignment
:
1566 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
1567 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1568 spirv_decoration_to_string(dec
->decoration
));
1572 case SpvDecorationUserSemantic
:
1573 /* User semantic decorations can safely be ignored by the driver. */
1576 case SpvDecorationRestrictPointerEXT
:
1577 case SpvDecorationAliasedPointerEXT
:
1578 /* TODO: We should actually plumb alias information through NIR. */
1582 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1587 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1588 const struct vtn_decoration
*dec
, void *out_is_patch
)
1590 if (dec
->decoration
== SpvDecorationPatch
) {
1591 *((bool *) out_is_patch
) = true;
1596 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1597 const struct vtn_decoration
*dec
, void *void_var
)
1599 struct vtn_variable
*vtn_var
= void_var
;
1601 /* Handle decorations that apply to a vtn_variable as a whole */
1602 switch (dec
->decoration
) {
1603 case SpvDecorationBinding
:
1604 vtn_var
->binding
= dec
->operands
[0];
1605 vtn_var
->explicit_binding
= true;
1607 case SpvDecorationDescriptorSet
:
1608 vtn_var
->descriptor_set
= dec
->operands
[0];
1610 case SpvDecorationInputAttachmentIndex
:
1611 vtn_var
->input_attachment_index
= dec
->operands
[0];
1613 case SpvDecorationPatch
:
1614 vtn_var
->patch
= true;
1616 case SpvDecorationOffset
:
1617 vtn_var
->offset
= dec
->operands
[0];
1619 case SpvDecorationNonWritable
:
1620 vtn_var
->access
|= ACCESS_NON_WRITEABLE
;
1622 case SpvDecorationNonReadable
:
1623 vtn_var
->access
|= ACCESS_NON_READABLE
;
1625 case SpvDecorationVolatile
:
1626 vtn_var
->access
|= ACCESS_VOLATILE
;
1628 case SpvDecorationCoherent
:
1629 vtn_var
->access
|= ACCESS_COHERENT
;
1631 case SpvDecorationCounterBuffer
:
1632 /* Counter buffer decorations can safely be ignored by the driver. */
1638 if (val
->value_type
== vtn_value_type_pointer
) {
1639 assert(val
->pointer
->var
== void_var
);
1640 assert(member
== -1);
1642 assert(val
->value_type
== vtn_value_type_type
);
1645 /* Location is odd. If applied to a split structure, we have to walk the
1646 * whole thing and accumulate the location. It's easier to handle as a
1649 if (dec
->decoration
== SpvDecorationLocation
) {
1650 unsigned location
= dec
->operands
[0];
1651 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
&&
1652 vtn_var
->mode
== vtn_variable_mode_output
) {
1653 location
+= FRAG_RESULT_DATA0
;
1654 } else if (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
&&
1655 vtn_var
->mode
== vtn_variable_mode_input
) {
1656 location
+= VERT_ATTRIB_GENERIC0
;
1657 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1658 vtn_var
->mode
== vtn_variable_mode_output
) {
1659 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1660 } else if (vtn_var
->mode
!= vtn_variable_mode_uniform
) {
1661 vtn_warn("Location must be on input, output, uniform, sampler or "
1666 if (vtn_var
->var
->num_members
== 0) {
1667 /* This handles the member and lone variable cases */
1668 vtn_var
->var
->data
.location
= location
;
1670 /* This handles the structure member case */
1671 assert(vtn_var
->var
->members
);
1674 vtn_var
->base_location
= location
;
1676 vtn_var
->var
->members
[member
].location
= location
;
1682 if (vtn_var
->var
->num_members
== 0) {
1683 /* We call this function on types as well as variables and not all
1684 * struct types get split so we can end up having stray member
1685 * decorations; just ignore them.
1688 apply_var_decoration(b
, &vtn_var
->var
->data
, dec
);
1689 } else if (member
>= 0) {
1690 /* Member decorations must come from a type */
1691 assert(val
->value_type
== vtn_value_type_type
);
1692 apply_var_decoration(b
, &vtn_var
->var
->members
[member
], dec
);
1695 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1696 for (unsigned i
= 0; i
< length
; i
++)
1697 apply_var_decoration(b
, &vtn_var
->var
->members
[i
], dec
);
1700 /* A few variables, those with external storage, have no actual
1701 * nir_variables associated with them. Fortunately, all decorations
1702 * we care about for those variables are on the type only.
1704 vtn_assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1705 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1706 vtn_var
->mode
== vtn_variable_mode_push_constant
||
1707 (vtn_var
->mode
== vtn_variable_mode_workgroup
&&
1708 b
->options
->lower_workgroup_access_to_offsets
));
1714 ptr_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1715 const struct vtn_decoration
*dec
, void *void_ptr
)
1717 struct vtn_pointer
*ptr
= void_ptr
;
1719 switch (dec
->decoration
) {
1720 case SpvDecorationNonUniformEXT
:
1721 ptr
->access
|= ACCESS_NON_UNIFORM
;
1729 enum vtn_variable_mode
1730 vtn_storage_class_to_mode(struct vtn_builder
*b
,
1731 SpvStorageClass
class,
1732 struct vtn_type
*interface_type
,
1733 nir_variable_mode
*nir_mode_out
)
1735 enum vtn_variable_mode mode
;
1736 nir_variable_mode nir_mode
;
1738 case SpvStorageClassUniform
:
1739 /* Assume it's an UBO if we lack the interface_type. */
1740 if (!interface_type
|| interface_type
->block
) {
1741 mode
= vtn_variable_mode_ubo
;
1742 nir_mode
= nir_var_mem_ubo
;
1743 } else if (interface_type
->buffer_block
) {
1744 mode
= vtn_variable_mode_ssbo
;
1745 nir_mode
= nir_var_mem_ssbo
;
1747 /* Default-block uniforms, coming from gl_spirv */
1748 mode
= vtn_variable_mode_uniform
;
1749 nir_mode
= nir_var_uniform
;
1752 case SpvStorageClassStorageBuffer
:
1753 mode
= vtn_variable_mode_ssbo
;
1754 nir_mode
= nir_var_mem_ssbo
;
1756 case SpvStorageClassPhysicalStorageBufferEXT
:
1757 mode
= vtn_variable_mode_phys_ssbo
;
1758 nir_mode
= nir_var_mem_global
;
1760 case SpvStorageClassUniformConstant
:
1761 mode
= vtn_variable_mode_uniform
;
1762 nir_mode
= nir_var_uniform
;
1764 case SpvStorageClassPushConstant
:
1765 mode
= vtn_variable_mode_push_constant
;
1766 nir_mode
= nir_var_uniform
;
1768 case SpvStorageClassInput
:
1769 mode
= vtn_variable_mode_input
;
1770 nir_mode
= nir_var_shader_in
;
1772 case SpvStorageClassOutput
:
1773 mode
= vtn_variable_mode_output
;
1774 nir_mode
= nir_var_shader_out
;
1776 case SpvStorageClassPrivate
:
1777 mode
= vtn_variable_mode_private
;
1778 nir_mode
= nir_var_shader_temp
;
1780 case SpvStorageClassFunction
:
1781 mode
= vtn_variable_mode_function
;
1782 nir_mode
= nir_var_function_temp
;
1784 case SpvStorageClassWorkgroup
:
1785 mode
= vtn_variable_mode_workgroup
;
1786 nir_mode
= nir_var_mem_shared
;
1788 case SpvStorageClassAtomicCounter
:
1789 mode
= vtn_variable_mode_uniform
;
1790 nir_mode
= nir_var_uniform
;
1792 case SpvStorageClassCrossWorkgroup
:
1793 mode
= vtn_variable_mode_cross_workgroup
;
1794 nir_mode
= nir_var_mem_global
;
1796 case SpvStorageClassImage
:
1797 mode
= vtn_variable_mode_image
;
1798 nir_mode
= nir_var_mem_ubo
;
1800 case SpvStorageClassGeneric
:
1802 vtn_fail("Unhandled variable storage class: %s (%u)",
1803 spirv_storageclass_to_string(class), class);
1807 *nir_mode_out
= nir_mode
;
1813 vtn_mode_to_address_format(struct vtn_builder
*b
, enum vtn_variable_mode mode
)
1816 case vtn_variable_mode_ubo
:
1817 return b
->options
->ubo_addr_format
;
1819 case vtn_variable_mode_ssbo
:
1820 return b
->options
->ssbo_addr_format
;
1822 case vtn_variable_mode_phys_ssbo
:
1823 return b
->options
->phys_ssbo_addr_format
;
1825 case vtn_variable_mode_push_constant
:
1826 return b
->options
->push_const_addr_format
;
1828 case vtn_variable_mode_workgroup
:
1829 return b
->options
->shared_addr_format
;
1831 case vtn_variable_mode_cross_workgroup
:
1832 return b
->options
->global_addr_format
;
1834 case vtn_variable_mode_function
:
1835 if (b
->physical_ptrs
)
1836 return b
->options
->temp_addr_format
;
1839 case vtn_variable_mode_private
:
1840 case vtn_variable_mode_uniform
:
1841 case vtn_variable_mode_input
:
1842 case vtn_variable_mode_output
:
1843 case vtn_variable_mode_image
:
1844 return nir_address_format_logical
;
1847 unreachable("Invalid variable mode");
1851 vtn_pointer_to_ssa(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
1853 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
1854 /* This pointer needs to have a pointer type with actual storage */
1855 vtn_assert(ptr
->ptr_type
);
1856 vtn_assert(ptr
->ptr_type
->type
);
1859 /* If we don't have an offset then we must be a pointer to the variable
1862 vtn_assert(!ptr
->offset
&& !ptr
->block_index
);
1864 struct vtn_access_chain chain
= {
1867 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
1870 vtn_assert(ptr
->offset
);
1871 if (ptr
->block_index
) {
1872 vtn_assert(ptr
->mode
== vtn_variable_mode_ubo
||
1873 ptr
->mode
== vtn_variable_mode_ssbo
);
1874 return nir_vec2(&b
->nb
, ptr
->block_index
, ptr
->offset
);
1876 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
);
1880 if (vtn_pointer_is_external_block(b
, ptr
) &&
1881 vtn_type_contains_block(b
, ptr
->type
) &&
1882 ptr
->mode
!= vtn_variable_mode_phys_ssbo
) {
1883 /* In this case, we're looking for a block index and not an actual
1886 * For PhysicalStorageBufferEXT pointers, we don't have a block index
1887 * at all because we get the pointer directly from the client. This
1888 * assumes that there will never be a SSBO binding variable using the
1889 * PhysicalStorageBufferEXT storage class. This assumption appears
1890 * to be correct according to the Vulkan spec because the table,
1891 * "Shader Resource and Storage Class Correspondence," the only the
1892 * Uniform storage class with BufferBlock or the StorageBuffer
1893 * storage class with Block can be used.
1895 if (!ptr
->block_index
) {
1896 /* If we don't have a block_index then we must be a pointer to the
1899 vtn_assert(!ptr
->deref
);
1901 struct vtn_access_chain chain
= {
1904 ptr
= vtn_nir_deref_pointer_dereference(b
, ptr
, &chain
);
1907 return ptr
->block_index
;
1909 return &vtn_pointer_to_deref(b
, ptr
)->dest
.ssa
;
1914 struct vtn_pointer
*
1915 vtn_pointer_from_ssa(struct vtn_builder
*b
, nir_ssa_def
*ssa
,
1916 struct vtn_type
*ptr_type
)
1918 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1920 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
1921 struct vtn_type
*without_array
=
1922 vtn_type_without_array(ptr_type
->deref
);
1924 nir_variable_mode nir_mode
;
1925 ptr
->mode
= vtn_storage_class_to_mode(b
, ptr_type
->storage_class
,
1926 without_array
, &nir_mode
);
1927 ptr
->type
= ptr_type
->deref
;
1928 ptr
->ptr_type
= ptr_type
;
1930 if (b
->wa_glslang_179
) {
1931 /* To work around https://github.com/KhronosGroup/glslang/issues/179 we
1932 * need to whack the mode because it creates a function parameter with
1933 * the Function storage class even though it's a pointer to a sampler.
1934 * If we don't do this, then NIR won't get rid of the deref_cast for us.
1936 if (ptr
->mode
== vtn_variable_mode_function
&&
1937 (ptr
->type
->base_type
== vtn_base_type_sampler
||
1938 ptr
->type
->base_type
== vtn_base_type_sampled_image
)) {
1939 ptr
->mode
= vtn_variable_mode_uniform
;
1940 nir_mode
= nir_var_uniform
;
1944 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
1945 /* This pointer type needs to have actual storage */
1946 vtn_assert(ptr_type
->type
);
1947 if (ptr
->mode
== vtn_variable_mode_ubo
||
1948 ptr
->mode
== vtn_variable_mode_ssbo
) {
1949 vtn_assert(ssa
->num_components
== 2);
1950 ptr
->block_index
= nir_channel(&b
->nb
, ssa
, 0);
1951 ptr
->offset
= nir_channel(&b
->nb
, ssa
, 1);
1953 vtn_assert(ssa
->num_components
== 1);
1954 ptr
->block_index
= NULL
;
1958 const struct glsl_type
*deref_type
= ptr_type
->deref
->type
;
1959 if (!vtn_pointer_is_external_block(b
, ptr
)) {
1960 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
1961 deref_type
, ptr_type
->stride
);
1962 } else if (vtn_type_contains_block(b
, ptr
->type
) &&
1963 ptr
->mode
!= vtn_variable_mode_phys_ssbo
) {
1964 /* This is a pointer to somewhere in an array of blocks, not a
1965 * pointer to somewhere inside the block. Set the block index
1966 * instead of making a cast.
1968 ptr
->block_index
= ssa
;
1970 /* This is a pointer to something internal or a pointer inside a
1971 * block. It's just a regular cast.
1973 * For PhysicalStorageBufferEXT pointers, we don't have a block index
1974 * at all because we get the pointer directly from the client. This
1975 * assumes that there will never be a SSBO binding variable using the
1976 * PhysicalStorageBufferEXT storage class. This assumption appears
1977 * to be correct according to the Vulkan spec because the table,
1978 * "Shader Resource and Storage Class Correspondence," the only the
1979 * Uniform storage class with BufferBlock or the StorageBuffer
1980 * storage class with Block can be used.
1982 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
1983 ptr_type
->deref
->type
,
1985 ptr
->deref
->dest
.ssa
.num_components
=
1986 glsl_get_vector_elements(ptr_type
->type
);
1987 ptr
->deref
->dest
.ssa
.bit_size
= glsl_get_bit_size(ptr_type
->type
);
1995 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
1997 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
2000 if (var
->mode
== vtn_variable_mode_input
) {
2001 return stage
== MESA_SHADER_TESS_CTRL
||
2002 stage
== MESA_SHADER_TESS_EVAL
||
2003 stage
== MESA_SHADER_GEOMETRY
;
2006 if (var
->mode
== vtn_variable_mode_output
)
2007 return stage
== MESA_SHADER_TESS_CTRL
;
2013 assign_missing_member_locations(struct vtn_variable
*var
)
2016 glsl_get_length(glsl_without_array(var
->type
->type
));
2017 int location
= var
->base_location
;
2019 for (unsigned i
= 0; i
< length
; i
++) {
2020 /* From the Vulkan spec:
2022 * “If the structure type is a Block but without a Location, then each
2023 * of its members must have a Location decoration.”
2026 if (var
->type
->block
) {
2027 assert(var
->base_location
!= -1 ||
2028 var
->var
->members
[i
].location
!= -1);
2031 /* From the Vulkan spec:
2033 * “Any member with its own Location decoration is assigned that
2034 * location. Each remaining member is assigned the location after the
2035 * immediately preceding member in declaration order.”
2037 if (var
->var
->members
[i
].location
!= -1)
2038 location
= var
->var
->members
[i
].location
;
2040 var
->var
->members
[i
].location
= location
;
2042 /* Below we use type instead of interface_type, because interface_type
2043 * is only available when it is a Block. This code also supports
2044 * input/outputs that are just structs
2046 const struct glsl_type
*member_type
=
2047 glsl_get_struct_field(glsl_without_array(var
->type
->type
), i
);
2050 glsl_count_attribute_slots(member_type
,
2051 false /* is_gl_vertex_input */);
2057 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
2058 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
2059 nir_constant
*initializer
)
2061 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
2062 struct vtn_type
*type
= ptr_type
->deref
;
2064 struct vtn_type
*without_array
= vtn_type_without_array(ptr_type
->deref
);
2066 enum vtn_variable_mode mode
;
2067 nir_variable_mode nir_mode
;
2068 mode
= vtn_storage_class_to_mode(b
, storage_class
, without_array
, &nir_mode
);
2071 case vtn_variable_mode_ubo
:
2072 /* There's no other way to get vtn_variable_mode_ubo */
2073 vtn_assert(without_array
->block
);
2074 b
->shader
->info
.num_ubos
++;
2076 case vtn_variable_mode_ssbo
:
2077 if (storage_class
== SpvStorageClassStorageBuffer
&&
2078 !without_array
->block
) {
2079 if (b
->variable_pointers
) {
2080 vtn_fail("Variables in the StorageBuffer storage class must "
2081 "have a struct type with the Block decoration");
2083 /* If variable pointers are not present, it's still malformed
2084 * SPIR-V but we can parse it and do the right thing anyway.
2085 * Since some of the 8-bit storage tests have bugs in this are,
2086 * just make it a warning for now.
2088 vtn_warn("Variables in the StorageBuffer storage class must "
2089 "have a struct type with the Block decoration");
2092 b
->shader
->info
.num_ssbos
++;
2094 case vtn_variable_mode_uniform
:
2095 if (glsl_type_is_image(without_array
->type
))
2096 b
->shader
->info
.num_images
++;
2097 else if (glsl_type_is_sampler(without_array
->type
))
2098 b
->shader
->info
.num_textures
++;
2100 case vtn_variable_mode_push_constant
:
2101 b
->shader
->num_uniforms
= vtn_type_block_size(b
, type
);
2104 case vtn_variable_mode_image
:
2105 vtn_fail("Cannot create a variable with the Image storage class");
2108 case vtn_variable_mode_phys_ssbo
:
2109 vtn_fail("Cannot create a variable with the "
2110 "PhysicalStorageBufferEXT storage class");
2114 /* No tallying is needed */
2118 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
2121 var
->base_location
= -1;
2123 vtn_assert(val
->value_type
== vtn_value_type_pointer
);
2124 val
->pointer
= vtn_pointer_for_variable(b
, var
, ptr_type
);
2126 switch (var
->mode
) {
2127 case vtn_variable_mode_function
:
2128 case vtn_variable_mode_private
:
2129 case vtn_variable_mode_uniform
:
2130 /* For these, we create the variable normally */
2131 var
->var
= rzalloc(b
->shader
, nir_variable
);
2132 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2134 if (storage_class
== SpvStorageClassAtomicCounter
) {
2135 /* Need to tweak the nir type here as at vtn_handle_type we don't
2136 * have the access to storage_class, that is the one that points us
2137 * that is an atomic uint.
2139 var
->var
->type
= repair_atomic_type(var
->type
->type
);
2141 /* Private variables don't have any explicit layout but some layouts
2142 * may have leaked through due to type deduplication in the SPIR-V.
2144 var
->var
->type
= var
->type
->type
;
2146 var
->var
->data
.mode
= nir_mode
;
2147 var
->var
->data
.location
= -1;
2148 var
->var
->interface_type
= NULL
;
2151 case vtn_variable_mode_ubo
:
2152 case vtn_variable_mode_ssbo
:
2153 var
->var
= rzalloc(b
->shader
, nir_variable
);
2154 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2156 var
->var
->type
= var
->type
->type
;
2157 var
->var
->interface_type
= var
->type
->type
;
2159 var
->var
->data
.mode
= nir_mode
;
2160 var
->var
->data
.location
= -1;
2164 case vtn_variable_mode_workgroup
:
2165 if (b
->options
->lower_workgroup_access_to_offsets
) {
2166 var
->shared_location
= -1;
2168 /* Create the variable normally */
2169 var
->var
= rzalloc(b
->shader
, nir_variable
);
2170 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2171 /* Workgroup variables don't have any explicit layout but some
2172 * layouts may have leaked through due to type deduplication in the
2175 var
->var
->type
= var
->type
->type
;
2176 var
->var
->data
.mode
= nir_var_mem_shared
;
2180 case vtn_variable_mode_input
:
2181 case vtn_variable_mode_output
: {
2182 /* In order to know whether or not we're a per-vertex inout, we need
2183 * the patch qualifier. This means walking the variable decorations
2184 * early before we actually create any variables. Not a big deal.
2186 * GLSLang really likes to place decorations in the most interior
2187 * thing it possibly can. In particular, if you have a struct, it
2188 * will place the patch decorations on the struct members. This
2189 * should be handled by the variable splitting below just fine.
2191 * If you have an array-of-struct, things get even more weird as it
2192 * will place the patch decorations on the struct even though it's
2193 * inside an array and some of the members being patch and others not
2194 * makes no sense whatsoever. Since the only sensible thing is for
2195 * it to be all or nothing, we'll call it patch if any of the members
2196 * are declared patch.
2199 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
2200 if (glsl_type_is_array(var
->type
->type
) &&
2201 glsl_type_is_struct_or_ifc(without_array
->type
)) {
2202 vtn_foreach_decoration(b
, vtn_value(b
, without_array
->id
,
2203 vtn_value_type_type
),
2204 var_is_patch_cb
, &var
->patch
);
2207 /* For inputs and outputs, we immediately split structures. This
2208 * is for a couple of reasons. For one, builtins may all come in
2209 * a struct and we really want those split out into separate
2210 * variables. For another, interpolation qualifiers can be
2211 * applied to members of the top-level struct ane we need to be
2212 * able to preserve that information.
2215 struct vtn_type
*per_vertex_type
= var
->type
;
2216 if (is_per_vertex_inout(var
, b
->shader
->info
.stage
)) {
2217 /* In Geometry shaders (and some tessellation), inputs come
2218 * in per-vertex arrays. However, some builtins come in
2219 * non-per-vertex, hence the need for the is_array check. In
2220 * any case, there are no non-builtin arrays allowed so this
2221 * check should be sufficient.
2223 per_vertex_type
= var
->type
->array_element
;
2226 var
->var
= rzalloc(b
->shader
, nir_variable
);
2227 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2228 /* In Vulkan, shader I/O variables don't have any explicit layout but
2229 * some layouts may have leaked through due to type deduplication in
2230 * the SPIR-V. We do, however, keep the layouts in the variable's
2231 * interface_type because we need offsets for XFB arrays of blocks.
2233 var
->var
->type
= var
->type
->type
;
2234 var
->var
->data
.mode
= nir_mode
;
2235 var
->var
->data
.patch
= var
->patch
;
2237 /* Figure out the interface block type. */
2238 struct vtn_type
*iface_type
= per_vertex_type
;
2239 if (var
->mode
== vtn_variable_mode_output
&&
2240 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
2241 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
||
2242 b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)) {
2243 /* For vertex data outputs, we can end up with arrays of blocks for
2244 * transform feedback where each array element corresponds to a
2245 * different XFB output buffer.
2247 while (iface_type
->base_type
== vtn_base_type_array
)
2248 iface_type
= iface_type
->array_element
;
2250 if (iface_type
->base_type
== vtn_base_type_struct
&& iface_type
->block
)
2251 var
->var
->interface_type
= iface_type
->type
;
2253 if (per_vertex_type
->base_type
== vtn_base_type_struct
&&
2254 per_vertex_type
->block
) {
2255 /* It's a struct. Set it up as per-member. */
2256 var
->var
->num_members
= glsl_get_length(per_vertex_type
->type
);
2257 var
->var
->members
= rzalloc_array(var
->var
, struct nir_variable_data
,
2258 var
->var
->num_members
);
2260 for (unsigned i
= 0; i
< var
->var
->num_members
; i
++) {
2261 var
->var
->members
[i
].mode
= nir_mode
;
2262 var
->var
->members
[i
].patch
= var
->patch
;
2263 var
->var
->members
[i
].location
= -1;
2267 /* For inputs and outputs, we need to grab locations and builtin
2268 * information from the per-vertex type.
2270 vtn_foreach_decoration(b
, vtn_value(b
, per_vertex_type
->id
,
2271 vtn_value_type_type
),
2272 var_decoration_cb
, var
);
2276 case vtn_variable_mode_push_constant
:
2277 case vtn_variable_mode_cross_workgroup
:
2278 /* These don't need actual variables. */
2281 case vtn_variable_mode_image
:
2282 case vtn_variable_mode_phys_ssbo
:
2283 unreachable("Should have been caught before");
2287 var
->var
->constant_initializer
=
2288 nir_constant_clone(initializer
, var
->var
);
2291 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
2292 vtn_foreach_decoration(b
, val
, ptr_decoration_cb
, val
->pointer
);
2294 if ((var
->mode
== vtn_variable_mode_input
||
2295 var
->mode
== vtn_variable_mode_output
) &&
2296 var
->var
->members
) {
2297 assign_missing_member_locations(var
);
2300 if (var
->mode
== vtn_variable_mode_uniform
||
2301 var
->mode
== vtn_variable_mode_ubo
||
2302 var
->mode
== vtn_variable_mode_ssbo
) {
2303 /* XXX: We still need the binding information in the nir_variable
2304 * for these. We should fix that.
2306 var
->var
->data
.binding
= var
->binding
;
2307 var
->var
->data
.explicit_binding
= var
->explicit_binding
;
2308 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
2309 var
->var
->data
.index
= var
->input_attachment_index
;
2310 var
->var
->data
.offset
= var
->offset
;
2312 if (glsl_type_is_image(without_array
->type
))
2313 var
->var
->data
.image
.format
= without_array
->image_format
;
2316 if (var
->mode
== vtn_variable_mode_function
) {
2317 vtn_assert(var
->var
!= NULL
&& var
->var
->members
== NULL
);
2318 nir_function_impl_add_variable(b
->nb
.impl
, var
->var
);
2319 } else if (var
->var
) {
2320 nir_shader_add_variable(b
->shader
, var
->var
);
2322 vtn_assert(vtn_pointer_is_external_block(b
, val
->pointer
));
2327 vtn_assert_types_equal(struct vtn_builder
*b
, SpvOp opcode
,
2328 struct vtn_type
*dst_type
,
2329 struct vtn_type
*src_type
)
2331 if (dst_type
->id
== src_type
->id
)
2334 if (vtn_types_compatible(b
, dst_type
, src_type
)) {
2335 /* Early versions of GLSLang would re-emit types unnecessarily and you
2336 * would end up with OpLoad, OpStore, or OpCopyMemory opcodes which have
2337 * mismatched source and destination types.
2339 * https://github.com/KhronosGroup/glslang/issues/304
2340 * https://github.com/KhronosGroup/glslang/issues/307
2341 * https://bugs.freedesktop.org/show_bug.cgi?id=104338
2342 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2344 vtn_warn("Source and destination types of %s do not have the same "
2345 "ID (but are compatible): %u vs %u",
2346 spirv_op_to_string(opcode
), dst_type
->id
, src_type
->id
);
2350 vtn_fail("Source and destination types of %s do not match: %s vs. %s",
2351 spirv_op_to_string(opcode
),
2352 glsl_get_type_name(dst_type
->type
),
2353 glsl_get_type_name(src_type
->type
));
2356 static nir_ssa_def
*
2357 nir_shrink_zero_pad_vec(nir_builder
*b
, nir_ssa_def
*val
,
2358 unsigned num_components
)
2360 if (val
->num_components
== num_components
)
2363 nir_ssa_def
*comps
[NIR_MAX_VEC_COMPONENTS
];
2364 for (unsigned i
= 0; i
< num_components
; i
++) {
2365 if (i
< val
->num_components
)
2366 comps
[i
] = nir_channel(b
, val
, i
);
2368 comps
[i
] = nir_imm_intN_t(b
, 0, val
->bit_size
);
2370 return nir_vec(b
, comps
, num_components
);
2373 static nir_ssa_def
*
2374 nir_sloppy_bitcast(nir_builder
*b
, nir_ssa_def
*val
,
2375 const struct glsl_type
*type
)
2377 const unsigned num_components
= glsl_get_vector_elements(type
);
2378 const unsigned bit_size
= glsl_get_bit_size(type
);
2380 /* First, zero-pad to ensure that the value is big enough that when we
2381 * bit-cast it, we don't loose anything.
2383 if (val
->bit_size
< bit_size
) {
2384 const unsigned src_num_components_needed
=
2385 vtn_align_u32(val
->num_components
, bit_size
/ val
->bit_size
);
2386 val
= nir_shrink_zero_pad_vec(b
, val
, src_num_components_needed
);
2389 val
= nir_bitcast_vector(b
, val
, bit_size
);
2391 return nir_shrink_zero_pad_vec(b
, val
, num_components
);
2395 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
2396 const uint32_t *w
, unsigned count
)
2400 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
2401 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2405 case SpvOpVariable
: {
2406 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2408 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2410 SpvStorageClass storage_class
= w
[3];
2411 nir_constant
*initializer
= NULL
;
2413 initializer
= vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
2415 vtn_create_variable(b
, val
, ptr_type
, storage_class
, initializer
);
2419 case SpvOpAccessChain
:
2420 case SpvOpPtrAccessChain
:
2421 case SpvOpInBoundsAccessChain
:
2422 case SpvOpInBoundsPtrAccessChain
: {
2423 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
2424 chain
->ptr_as_array
= (opcode
== SpvOpPtrAccessChain
|| opcode
== SpvOpInBoundsPtrAccessChain
);
2427 for (int i
= 4; i
< count
; i
++) {
2428 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
2429 if (link_val
->value_type
== vtn_value_type_constant
) {
2430 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
2431 chain
->link
[idx
].id
= vtn_constant_int(b
, w
[i
]);
2433 chain
->link
[idx
].mode
= vtn_access_mode_id
;
2434 chain
->link
[idx
].id
= w
[i
];
2440 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2441 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
2442 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
2443 /* This is rather insane. SPIR-V allows you to use OpSampledImage
2444 * to combine an array of images with a single sampler to get an
2445 * array of sampled images that all share the same sampler.
2446 * Fortunately, this means that we can more-or-less ignore the
2447 * sampler when crawling the access chain, but it does leave us
2448 * with this rather awkward little special-case.
2450 struct vtn_value
*val
=
2451 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2452 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2453 val
->sampled_image
->type
= base_val
->sampled_image
->type
;
2454 val
->sampled_image
->image
=
2455 vtn_pointer_dereference(b
, base_val
->sampled_image
->image
, chain
);
2456 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
2457 vtn_foreach_decoration(b
, val
, ptr_decoration_cb
,
2458 val
->sampled_image
->image
);
2459 vtn_foreach_decoration(b
, val
, ptr_decoration_cb
,
2460 val
->sampled_image
->sampler
);
2462 vtn_assert(base_val
->value_type
== vtn_value_type_pointer
);
2463 struct vtn_value
*val
=
2464 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2465 val
->pointer
= vtn_pointer_dereference(b
, base_val
->pointer
, chain
);
2466 val
->pointer
->ptr_type
= ptr_type
;
2467 vtn_foreach_decoration(b
, val
, ptr_decoration_cb
, val
->pointer
);
2472 case SpvOpCopyMemory
: {
2473 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2474 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
2476 vtn_assert_types_equal(b
, opcode
, dest
->type
->deref
, src
->type
->deref
);
2478 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
2483 struct vtn_type
*res_type
=
2484 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2485 struct vtn_value
*src_val
= vtn_value(b
, w
[3], vtn_value_type_pointer
);
2486 struct vtn_pointer
*src
= src_val
->pointer
;
2488 vtn_assert_types_equal(b
, opcode
, res_type
, src_val
->type
->deref
);
2490 if (glsl_type_is_image(res_type
->type
) ||
2491 glsl_type_is_sampler(res_type
->type
)) {
2492 vtn_push_value(b
, w
[2], vtn_value_type_pointer
)->pointer
= src
;
2496 vtn_push_ssa(b
, w
[2], res_type
, vtn_variable_load(b
, src
));
2501 struct vtn_value
*dest_val
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2502 struct vtn_pointer
*dest
= dest_val
->pointer
;
2503 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[2]);
2505 /* OpStore requires us to actually have a storage type */
2506 vtn_fail_if(dest
->type
->type
== NULL
,
2507 "Invalid destination type for OpStore");
2509 if (glsl_get_base_type(dest
->type
->type
) == GLSL_TYPE_BOOL
&&
2510 glsl_get_base_type(src_val
->type
->type
) == GLSL_TYPE_UINT
) {
2511 /* Early versions of GLSLang would use uint types for UBOs/SSBOs but
2512 * would then store them to a local variable as bool. Work around
2513 * the issue by doing an implicit conversion.
2515 * https://github.com/KhronosGroup/glslang/issues/170
2516 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2518 vtn_warn("OpStore of value of type OpTypeInt to a pointer to type "
2519 "OpTypeBool. Doing an implicit conversion to work around "
2521 struct vtn_ssa_value
*bool_ssa
=
2522 vtn_create_ssa_value(b
, dest
->type
->type
);
2523 bool_ssa
->def
= nir_i2b(&b
->nb
, vtn_ssa_value(b
, w
[2])->def
);
2524 vtn_variable_store(b
, bool_ssa
, dest
);
2528 vtn_assert_types_equal(b
, opcode
, dest_val
->type
->deref
, src_val
->type
);
2530 if (glsl_type_is_sampler(dest
->type
->type
)) {
2531 if (b
->wa_glslang_179
) {
2532 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
2533 "propagation to workaround the problem.");
2534 vtn_assert(dest
->var
->copy_prop_sampler
== NULL
);
2535 dest
->var
->copy_prop_sampler
=
2536 vtn_value(b
, w
[2], vtn_value_type_pointer
)->pointer
;
2538 vtn_fail("Vulkan does not allow OpStore of a sampler or image.");
2543 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
2544 vtn_variable_store(b
, src
, dest
);
2548 case SpvOpArrayLength
: {
2549 struct vtn_pointer
*ptr
=
2550 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2551 const uint32_t field
= w
[4];
2553 vtn_fail_if(ptr
->type
->base_type
!= vtn_base_type_struct
,
2554 "OpArrayLength must take a pointer to a structure type");
2555 vtn_fail_if(field
!= ptr
->type
->length
- 1 ||
2556 ptr
->type
->members
[field
]->base_type
!= vtn_base_type_array
,
2557 "OpArrayLength must reference the last memeber of the "
2558 "structure and that must be an array");
2560 const uint32_t offset
= ptr
->type
->offsets
[field
];
2561 const uint32_t stride
= ptr
->type
->members
[field
]->stride
;
2563 if (!ptr
->block_index
) {
2564 struct vtn_access_chain chain
= {
2567 ptr
= vtn_pointer_dereference(b
, ptr
, &chain
);
2568 vtn_assert(ptr
->block_index
);
2571 nir_intrinsic_instr
*instr
=
2572 nir_intrinsic_instr_create(b
->nb
.shader
,
2573 nir_intrinsic_get_buffer_size
);
2574 instr
->src
[0] = nir_src_for_ssa(ptr
->block_index
);
2575 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
2576 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2577 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
2579 /* array_length = max(buffer_size - offset, 0) / stride */
2580 nir_ssa_def
*array_length
=
2585 nir_imm_int(&b
->nb
, offset
)),
2586 nir_imm_int(&b
->nb
, 0u)),
2587 nir_imm_int(&b
->nb
, stride
));
2589 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2590 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
2591 val
->ssa
->def
= array_length
;
2595 case SpvOpConvertPtrToU
: {
2596 struct vtn_value
*u_val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2598 vtn_fail_if(u_val
->type
->base_type
!= vtn_base_type_vector
&&
2599 u_val
->type
->base_type
!= vtn_base_type_scalar
,
2600 "OpConvertPtrToU can only be used to cast to a vector or "
2603 /* The pointer will be converted to an SSA value automatically */
2604 nir_ssa_def
*ptr_ssa
= vtn_ssa_value(b
, w
[3])->def
;
2606 u_val
->ssa
= vtn_create_ssa_value(b
, u_val
->type
->type
);
2607 u_val
->ssa
->def
= nir_sloppy_bitcast(&b
->nb
, ptr_ssa
, u_val
->type
->type
);
2611 case SpvOpConvertUToPtr
: {
2612 struct vtn_value
*ptr_val
=
2613 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2614 struct vtn_value
*u_val
= vtn_value(b
, w
[3], vtn_value_type_ssa
);
2616 vtn_fail_if(ptr_val
->type
->type
== NULL
,
2617 "OpConvertUToPtr can only be used on physical pointers");
2619 vtn_fail_if(u_val
->type
->base_type
!= vtn_base_type_vector
&&
2620 u_val
->type
->base_type
!= vtn_base_type_scalar
,
2621 "OpConvertUToPtr can only be used to cast from a vector or "
2624 nir_ssa_def
*ptr_ssa
= nir_sloppy_bitcast(&b
->nb
, u_val
->ssa
->def
,
2625 ptr_val
->type
->type
);
2626 ptr_val
->pointer
= vtn_pointer_from_ssa(b
, ptr_ssa
, ptr_val
->type
);
2630 case SpvOpCopyMemorySized
:
2632 vtn_fail_with_opcode("Unhandled opcode", opcode
);