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 *location
= VARYING_SLOT_POS
;
1299 vtn_assert(*mode
== nir_var_shader_in
);
1301 case SpvBuiltInPointCoord
:
1302 *location
= VARYING_SLOT_PNTC
;
1303 vtn_assert(*mode
== nir_var_shader_in
);
1305 case SpvBuiltInFrontFacing
:
1306 *location
= SYSTEM_VALUE_FRONT_FACE
;
1307 set_mode_system_value(b
, mode
);
1309 case SpvBuiltInSampleId
:
1310 *location
= SYSTEM_VALUE_SAMPLE_ID
;
1311 set_mode_system_value(b
, mode
);
1313 case SpvBuiltInSamplePosition
:
1314 *location
= SYSTEM_VALUE_SAMPLE_POS
;
1315 set_mode_system_value(b
, mode
);
1317 case SpvBuiltInSampleMask
:
1318 if (*mode
== nir_var_shader_out
) {
1319 *location
= FRAG_RESULT_SAMPLE_MASK
;
1321 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1322 set_mode_system_value(b
, mode
);
1325 case SpvBuiltInFragDepth
:
1326 *location
= FRAG_RESULT_DEPTH
;
1327 vtn_assert(*mode
== nir_var_shader_out
);
1329 case SpvBuiltInHelperInvocation
:
1330 *location
= SYSTEM_VALUE_HELPER_INVOCATION
;
1331 set_mode_system_value(b
, mode
);
1333 case SpvBuiltInNumWorkgroups
:
1334 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1335 set_mode_system_value(b
, mode
);
1337 case SpvBuiltInWorkgroupSize
:
1338 *location
= SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
1339 set_mode_system_value(b
, mode
);
1341 case SpvBuiltInWorkgroupId
:
1342 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1343 set_mode_system_value(b
, mode
);
1345 case SpvBuiltInLocalInvocationId
:
1346 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1347 set_mode_system_value(b
, mode
);
1349 case SpvBuiltInLocalInvocationIndex
:
1350 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1351 set_mode_system_value(b
, mode
);
1353 case SpvBuiltInGlobalInvocationId
:
1354 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1355 set_mode_system_value(b
, mode
);
1357 case SpvBuiltInGlobalLinearId
:
1358 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX
;
1359 set_mode_system_value(b
, mode
);
1361 case SpvBuiltInBaseVertex
:
1362 /* OpenGL gl_BaseVertex (SYSTEM_VALUE_BASE_VERTEX) is not the same
1363 * semantic as SPIR-V BaseVertex (SYSTEM_VALUE_FIRST_VERTEX).
1365 *location
= SYSTEM_VALUE_FIRST_VERTEX
;
1366 set_mode_system_value(b
, mode
);
1368 case SpvBuiltInBaseInstance
:
1369 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1370 set_mode_system_value(b
, mode
);
1372 case SpvBuiltInDrawIndex
:
1373 *location
= SYSTEM_VALUE_DRAW_ID
;
1374 set_mode_system_value(b
, mode
);
1376 case SpvBuiltInSubgroupSize
:
1377 *location
= SYSTEM_VALUE_SUBGROUP_SIZE
;
1378 set_mode_system_value(b
, mode
);
1380 case SpvBuiltInSubgroupId
:
1381 *location
= SYSTEM_VALUE_SUBGROUP_ID
;
1382 set_mode_system_value(b
, mode
);
1384 case SpvBuiltInSubgroupLocalInvocationId
:
1385 *location
= SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1386 set_mode_system_value(b
, mode
);
1388 case SpvBuiltInNumSubgroups
:
1389 *location
= SYSTEM_VALUE_NUM_SUBGROUPS
;
1390 set_mode_system_value(b
, mode
);
1392 case SpvBuiltInDeviceIndex
:
1393 *location
= SYSTEM_VALUE_DEVICE_INDEX
;
1394 set_mode_system_value(b
, mode
);
1396 case SpvBuiltInViewIndex
:
1397 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1398 set_mode_system_value(b
, mode
);
1400 case SpvBuiltInSubgroupEqMask
:
1401 *location
= SYSTEM_VALUE_SUBGROUP_EQ_MASK
,
1402 set_mode_system_value(b
, mode
);
1404 case SpvBuiltInSubgroupGeMask
:
1405 *location
= SYSTEM_VALUE_SUBGROUP_GE_MASK
,
1406 set_mode_system_value(b
, mode
);
1408 case SpvBuiltInSubgroupGtMask
:
1409 *location
= SYSTEM_VALUE_SUBGROUP_GT_MASK
,
1410 set_mode_system_value(b
, mode
);
1412 case SpvBuiltInSubgroupLeMask
:
1413 *location
= SYSTEM_VALUE_SUBGROUP_LE_MASK
,
1414 set_mode_system_value(b
, mode
);
1416 case SpvBuiltInSubgroupLtMask
:
1417 *location
= SYSTEM_VALUE_SUBGROUP_LT_MASK
,
1418 set_mode_system_value(b
, mode
);
1420 case SpvBuiltInFragStencilRefEXT
:
1421 *location
= FRAG_RESULT_STENCIL
;
1422 vtn_assert(*mode
== nir_var_shader_out
);
1424 case SpvBuiltInWorkDim
:
1425 *location
= SYSTEM_VALUE_WORK_DIM
;
1426 set_mode_system_value(b
, mode
);
1428 case SpvBuiltInGlobalSize
:
1429 *location
= SYSTEM_VALUE_GLOBAL_GROUP_SIZE
;
1430 set_mode_system_value(b
, mode
);
1433 vtn_fail("Unsupported builtin: %s (%u)",
1434 spirv_builtin_to_string(builtin
), builtin
);
1439 apply_var_decoration(struct vtn_builder
*b
,
1440 struct nir_variable_data
*var_data
,
1441 const struct vtn_decoration
*dec
)
1443 switch (dec
->decoration
) {
1444 case SpvDecorationRelaxedPrecision
:
1445 break; /* FIXME: Do nothing with this for now. */
1446 case SpvDecorationNoPerspective
:
1447 var_data
->interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1449 case SpvDecorationFlat
:
1450 var_data
->interpolation
= INTERP_MODE_FLAT
;
1452 case SpvDecorationCentroid
:
1453 var_data
->centroid
= true;
1455 case SpvDecorationSample
:
1456 var_data
->sample
= true;
1458 case SpvDecorationInvariant
:
1459 var_data
->invariant
= true;
1461 case SpvDecorationConstant
:
1462 var_data
->read_only
= true;
1464 case SpvDecorationNonReadable
:
1465 var_data
->image
.access
|= ACCESS_NON_READABLE
;
1467 case SpvDecorationNonWritable
:
1468 var_data
->read_only
= true;
1469 var_data
->image
.access
|= ACCESS_NON_WRITEABLE
;
1471 case SpvDecorationRestrict
:
1472 var_data
->image
.access
|= ACCESS_RESTRICT
;
1474 case SpvDecorationVolatile
:
1475 var_data
->image
.access
|= ACCESS_VOLATILE
;
1477 case SpvDecorationCoherent
:
1478 var_data
->image
.access
|= ACCESS_COHERENT
;
1480 case SpvDecorationComponent
:
1481 var_data
->location_frac
= dec
->operands
[0];
1483 case SpvDecorationIndex
:
1484 var_data
->index
= dec
->operands
[0];
1486 case SpvDecorationBuiltIn
: {
1487 SpvBuiltIn builtin
= dec
->operands
[0];
1489 nir_variable_mode mode
= var_data
->mode
;
1490 vtn_get_builtin_location(b
, builtin
, &var_data
->location
, &mode
);
1491 var_data
->mode
= mode
;
1494 case SpvBuiltInTessLevelOuter
:
1495 case SpvBuiltInTessLevelInner
:
1496 case SpvBuiltInClipDistance
:
1497 case SpvBuiltInCullDistance
:
1498 var_data
->compact
= true;
1505 case SpvDecorationSpecId
:
1506 case SpvDecorationRowMajor
:
1507 case SpvDecorationColMajor
:
1508 case SpvDecorationMatrixStride
:
1509 case SpvDecorationAliased
:
1510 case SpvDecorationUniform
:
1511 case SpvDecorationUniformId
:
1512 case SpvDecorationLinkageAttributes
:
1513 break; /* Do nothing with these here */
1515 case SpvDecorationPatch
:
1516 var_data
->patch
= true;
1519 case SpvDecorationLocation
:
1520 vtn_fail("Handled above");
1522 case SpvDecorationBlock
:
1523 case SpvDecorationBufferBlock
:
1524 case SpvDecorationArrayStride
:
1525 case SpvDecorationGLSLShared
:
1526 case SpvDecorationGLSLPacked
:
1527 break; /* These can apply to a type but we don't care about them */
1529 case SpvDecorationBinding
:
1530 case SpvDecorationDescriptorSet
:
1531 case SpvDecorationNoContraction
:
1532 case SpvDecorationInputAttachmentIndex
:
1533 vtn_warn("Decoration not allowed for variable or structure member: %s",
1534 spirv_decoration_to_string(dec
->decoration
));
1537 case SpvDecorationXfbBuffer
:
1538 var_data
->explicit_xfb_buffer
= true;
1539 var_data
->xfb_buffer
= dec
->operands
[0];
1540 var_data
->always_active_io
= true;
1542 case SpvDecorationXfbStride
:
1543 var_data
->explicit_xfb_stride
= true;
1544 var_data
->xfb_stride
= dec
->operands
[0];
1546 case SpvDecorationOffset
:
1547 var_data
->explicit_offset
= true;
1548 var_data
->offset
= dec
->operands
[0];
1551 case SpvDecorationStream
:
1552 var_data
->stream
= dec
->operands
[0];
1555 case SpvDecorationCPacked
:
1556 case SpvDecorationSaturatedConversion
:
1557 case SpvDecorationFuncParamAttr
:
1558 case SpvDecorationFPRoundingMode
:
1559 case SpvDecorationFPFastMathMode
:
1560 case SpvDecorationAlignment
:
1561 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
1562 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1563 spirv_decoration_to_string(dec
->decoration
));
1567 case SpvDecorationUserSemantic
:
1568 /* User semantic decorations can safely be ignored by the driver. */
1571 case SpvDecorationRestrictPointerEXT
:
1572 case SpvDecorationAliasedPointerEXT
:
1573 /* TODO: We should actually plumb alias information through NIR. */
1577 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1582 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1583 const struct vtn_decoration
*dec
, void *out_is_patch
)
1585 if (dec
->decoration
== SpvDecorationPatch
) {
1586 *((bool *) out_is_patch
) = true;
1591 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1592 const struct vtn_decoration
*dec
, void *void_var
)
1594 struct vtn_variable
*vtn_var
= void_var
;
1596 /* Handle decorations that apply to a vtn_variable as a whole */
1597 switch (dec
->decoration
) {
1598 case SpvDecorationBinding
:
1599 vtn_var
->binding
= dec
->operands
[0];
1600 vtn_var
->explicit_binding
= true;
1602 case SpvDecorationDescriptorSet
:
1603 vtn_var
->descriptor_set
= dec
->operands
[0];
1605 case SpvDecorationInputAttachmentIndex
:
1606 vtn_var
->input_attachment_index
= dec
->operands
[0];
1608 case SpvDecorationPatch
:
1609 vtn_var
->patch
= true;
1611 case SpvDecorationOffset
:
1612 vtn_var
->offset
= dec
->operands
[0];
1614 case SpvDecorationNonWritable
:
1615 vtn_var
->access
|= ACCESS_NON_WRITEABLE
;
1617 case SpvDecorationNonReadable
:
1618 vtn_var
->access
|= ACCESS_NON_READABLE
;
1620 case SpvDecorationVolatile
:
1621 vtn_var
->access
|= ACCESS_VOLATILE
;
1623 case SpvDecorationCoherent
:
1624 vtn_var
->access
|= ACCESS_COHERENT
;
1626 case SpvDecorationCounterBuffer
:
1627 /* Counter buffer decorations can safely be ignored by the driver. */
1633 if (val
->value_type
== vtn_value_type_pointer
) {
1634 assert(val
->pointer
->var
== void_var
);
1635 assert(member
== -1);
1637 assert(val
->value_type
== vtn_value_type_type
);
1640 /* Location is odd. If applied to a split structure, we have to walk the
1641 * whole thing and accumulate the location. It's easier to handle as a
1644 if (dec
->decoration
== SpvDecorationLocation
) {
1645 unsigned location
= dec
->operands
[0];
1646 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
&&
1647 vtn_var
->mode
== vtn_variable_mode_output
) {
1648 location
+= FRAG_RESULT_DATA0
;
1649 } else if (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
&&
1650 vtn_var
->mode
== vtn_variable_mode_input
) {
1651 location
+= VERT_ATTRIB_GENERIC0
;
1652 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1653 vtn_var
->mode
== vtn_variable_mode_output
) {
1654 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1655 } else if (vtn_var
->mode
!= vtn_variable_mode_uniform
) {
1656 vtn_warn("Location must be on input, output, uniform, sampler or "
1661 if (vtn_var
->var
->num_members
== 0) {
1662 /* This handles the member and lone variable cases */
1663 vtn_var
->var
->data
.location
= location
;
1665 /* This handles the structure member case */
1666 assert(vtn_var
->var
->members
);
1669 vtn_var
->base_location
= location
;
1671 vtn_var
->var
->members
[member
].location
= location
;
1677 if (vtn_var
->var
->num_members
== 0) {
1678 /* We call this function on types as well as variables and not all
1679 * struct types get split so we can end up having stray member
1680 * decorations; just ignore them.
1683 apply_var_decoration(b
, &vtn_var
->var
->data
, dec
);
1684 } else if (member
>= 0) {
1685 /* Member decorations must come from a type */
1686 assert(val
->value_type
== vtn_value_type_type
);
1687 apply_var_decoration(b
, &vtn_var
->var
->members
[member
], dec
);
1690 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1691 for (unsigned i
= 0; i
< length
; i
++)
1692 apply_var_decoration(b
, &vtn_var
->var
->members
[i
], dec
);
1695 /* A few variables, those with external storage, have no actual
1696 * nir_variables associated with them. Fortunately, all decorations
1697 * we care about for those variables are on the type only.
1699 vtn_assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1700 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1701 vtn_var
->mode
== vtn_variable_mode_push_constant
||
1702 (vtn_var
->mode
== vtn_variable_mode_workgroup
&&
1703 b
->options
->lower_workgroup_access_to_offsets
));
1709 ptr_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1710 const struct vtn_decoration
*dec
, void *void_ptr
)
1712 struct vtn_pointer
*ptr
= void_ptr
;
1714 switch (dec
->decoration
) {
1715 case SpvDecorationNonUniformEXT
:
1716 ptr
->access
|= ACCESS_NON_UNIFORM
;
1724 enum vtn_variable_mode
1725 vtn_storage_class_to_mode(struct vtn_builder
*b
,
1726 SpvStorageClass
class,
1727 struct vtn_type
*interface_type
,
1728 nir_variable_mode
*nir_mode_out
)
1730 enum vtn_variable_mode mode
;
1731 nir_variable_mode nir_mode
;
1733 case SpvStorageClassUniform
:
1734 /* Assume it's an UBO if we lack the interface_type. */
1735 if (!interface_type
|| interface_type
->block
) {
1736 mode
= vtn_variable_mode_ubo
;
1737 nir_mode
= nir_var_mem_ubo
;
1738 } else if (interface_type
->buffer_block
) {
1739 mode
= vtn_variable_mode_ssbo
;
1740 nir_mode
= nir_var_mem_ssbo
;
1742 /* Default-block uniforms, coming from gl_spirv */
1743 mode
= vtn_variable_mode_uniform
;
1744 nir_mode
= nir_var_uniform
;
1747 case SpvStorageClassStorageBuffer
:
1748 mode
= vtn_variable_mode_ssbo
;
1749 nir_mode
= nir_var_mem_ssbo
;
1751 case SpvStorageClassPhysicalStorageBufferEXT
:
1752 mode
= vtn_variable_mode_phys_ssbo
;
1753 nir_mode
= nir_var_mem_global
;
1755 case SpvStorageClassUniformConstant
:
1756 mode
= vtn_variable_mode_uniform
;
1757 nir_mode
= nir_var_uniform
;
1759 case SpvStorageClassPushConstant
:
1760 mode
= vtn_variable_mode_push_constant
;
1761 nir_mode
= nir_var_uniform
;
1763 case SpvStorageClassInput
:
1764 mode
= vtn_variable_mode_input
;
1765 nir_mode
= nir_var_shader_in
;
1767 case SpvStorageClassOutput
:
1768 mode
= vtn_variable_mode_output
;
1769 nir_mode
= nir_var_shader_out
;
1771 case SpvStorageClassPrivate
:
1772 mode
= vtn_variable_mode_private
;
1773 nir_mode
= nir_var_shader_temp
;
1775 case SpvStorageClassFunction
:
1776 mode
= vtn_variable_mode_function
;
1777 nir_mode
= nir_var_function_temp
;
1779 case SpvStorageClassWorkgroup
:
1780 mode
= vtn_variable_mode_workgroup
;
1781 nir_mode
= nir_var_mem_shared
;
1783 case SpvStorageClassAtomicCounter
:
1784 mode
= vtn_variable_mode_uniform
;
1785 nir_mode
= nir_var_uniform
;
1787 case SpvStorageClassCrossWorkgroup
:
1788 mode
= vtn_variable_mode_cross_workgroup
;
1789 nir_mode
= nir_var_mem_global
;
1791 case SpvStorageClassImage
:
1792 mode
= vtn_variable_mode_image
;
1793 nir_mode
= nir_var_mem_ubo
;
1795 case SpvStorageClassGeneric
:
1797 vtn_fail("Unhandled variable storage class: %s (%u)",
1798 spirv_storageclass_to_string(class), class);
1802 *nir_mode_out
= nir_mode
;
1808 vtn_mode_to_address_format(struct vtn_builder
*b
, enum vtn_variable_mode mode
)
1811 case vtn_variable_mode_ubo
:
1812 return b
->options
->ubo_addr_format
;
1814 case vtn_variable_mode_ssbo
:
1815 return b
->options
->ssbo_addr_format
;
1817 case vtn_variable_mode_phys_ssbo
:
1818 return b
->options
->phys_ssbo_addr_format
;
1820 case vtn_variable_mode_push_constant
:
1821 return b
->options
->push_const_addr_format
;
1823 case vtn_variable_mode_workgroup
:
1824 return b
->options
->shared_addr_format
;
1826 case vtn_variable_mode_cross_workgroup
:
1827 return b
->options
->global_addr_format
;
1829 case vtn_variable_mode_function
:
1830 if (b
->physical_ptrs
)
1831 return b
->options
->temp_addr_format
;
1834 case vtn_variable_mode_private
:
1835 case vtn_variable_mode_uniform
:
1836 case vtn_variable_mode_input
:
1837 case vtn_variable_mode_output
:
1838 case vtn_variable_mode_image
:
1839 return nir_address_format_logical
;
1842 unreachable("Invalid variable mode");
1846 vtn_pointer_to_ssa(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
1848 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
1849 /* This pointer needs to have a pointer type with actual storage */
1850 vtn_assert(ptr
->ptr_type
);
1851 vtn_assert(ptr
->ptr_type
->type
);
1854 /* If we don't have an offset then we must be a pointer to the variable
1857 vtn_assert(!ptr
->offset
&& !ptr
->block_index
);
1859 struct vtn_access_chain chain
= {
1862 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
1865 vtn_assert(ptr
->offset
);
1866 if (ptr
->block_index
) {
1867 vtn_assert(ptr
->mode
== vtn_variable_mode_ubo
||
1868 ptr
->mode
== vtn_variable_mode_ssbo
);
1869 return nir_vec2(&b
->nb
, ptr
->block_index
, ptr
->offset
);
1871 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
);
1875 if (vtn_pointer_is_external_block(b
, ptr
) &&
1876 vtn_type_contains_block(b
, ptr
->type
) &&
1877 ptr
->mode
!= vtn_variable_mode_phys_ssbo
) {
1878 /* In this case, we're looking for a block index and not an actual
1881 * For PhysicalStorageBufferEXT pointers, we don't have a block index
1882 * at all because we get the pointer directly from the client. This
1883 * assumes that there will never be a SSBO binding variable using the
1884 * PhysicalStorageBufferEXT storage class. This assumption appears
1885 * to be correct according to the Vulkan spec because the table,
1886 * "Shader Resource and Storage Class Correspondence," the only the
1887 * Uniform storage class with BufferBlock or the StorageBuffer
1888 * storage class with Block can be used.
1890 if (!ptr
->block_index
) {
1891 /* If we don't have a block_index then we must be a pointer to the
1894 vtn_assert(!ptr
->deref
);
1896 struct vtn_access_chain chain
= {
1899 ptr
= vtn_nir_deref_pointer_dereference(b
, ptr
, &chain
);
1902 return ptr
->block_index
;
1904 return &vtn_pointer_to_deref(b
, ptr
)->dest
.ssa
;
1909 struct vtn_pointer
*
1910 vtn_pointer_from_ssa(struct vtn_builder
*b
, nir_ssa_def
*ssa
,
1911 struct vtn_type
*ptr_type
)
1913 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1915 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
1916 struct vtn_type
*without_array
=
1917 vtn_type_without_array(ptr_type
->deref
);
1919 nir_variable_mode nir_mode
;
1920 ptr
->mode
= vtn_storage_class_to_mode(b
, ptr_type
->storage_class
,
1921 without_array
, &nir_mode
);
1922 ptr
->type
= ptr_type
->deref
;
1923 ptr
->ptr_type
= ptr_type
;
1925 if (b
->wa_glslang_179
) {
1926 /* To work around https://github.com/KhronosGroup/glslang/issues/179 we
1927 * need to whack the mode because it creates a function parameter with
1928 * the Function storage class even though it's a pointer to a sampler.
1929 * If we don't do this, then NIR won't get rid of the deref_cast for us.
1931 if (ptr
->mode
== vtn_variable_mode_function
&&
1932 (ptr
->type
->base_type
== vtn_base_type_sampler
||
1933 ptr
->type
->base_type
== vtn_base_type_sampled_image
)) {
1934 ptr
->mode
= vtn_variable_mode_uniform
;
1935 nir_mode
= nir_var_uniform
;
1939 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
1940 /* This pointer type needs to have actual storage */
1941 vtn_assert(ptr_type
->type
);
1942 if (ptr
->mode
== vtn_variable_mode_ubo
||
1943 ptr
->mode
== vtn_variable_mode_ssbo
) {
1944 vtn_assert(ssa
->num_components
== 2);
1945 ptr
->block_index
= nir_channel(&b
->nb
, ssa
, 0);
1946 ptr
->offset
= nir_channel(&b
->nb
, ssa
, 1);
1948 vtn_assert(ssa
->num_components
== 1);
1949 ptr
->block_index
= NULL
;
1953 const struct glsl_type
*deref_type
= ptr_type
->deref
->type
;
1954 if (!vtn_pointer_is_external_block(b
, ptr
)) {
1955 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
1956 deref_type
, ptr_type
->stride
);
1957 } else if (vtn_type_contains_block(b
, ptr
->type
) &&
1958 ptr
->mode
!= vtn_variable_mode_phys_ssbo
) {
1959 /* This is a pointer to somewhere in an array of blocks, not a
1960 * pointer to somewhere inside the block. Set the block index
1961 * instead of making a cast.
1963 ptr
->block_index
= ssa
;
1965 /* This is a pointer to something internal or a pointer inside a
1966 * block. It's just a regular cast.
1968 * For PhysicalStorageBufferEXT pointers, we don't have a block index
1969 * at all because we get the pointer directly from the client. This
1970 * assumes that there will never be a SSBO binding variable using the
1971 * PhysicalStorageBufferEXT storage class. This assumption appears
1972 * to be correct according to the Vulkan spec because the table,
1973 * "Shader Resource and Storage Class Correspondence," the only the
1974 * Uniform storage class with BufferBlock or the StorageBuffer
1975 * storage class with Block can be used.
1977 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
1978 ptr_type
->deref
->type
,
1980 ptr
->deref
->dest
.ssa
.num_components
=
1981 glsl_get_vector_elements(ptr_type
->type
);
1982 ptr
->deref
->dest
.ssa
.bit_size
= glsl_get_bit_size(ptr_type
->type
);
1990 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
1992 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
1995 if (var
->mode
== vtn_variable_mode_input
) {
1996 return stage
== MESA_SHADER_TESS_CTRL
||
1997 stage
== MESA_SHADER_TESS_EVAL
||
1998 stage
== MESA_SHADER_GEOMETRY
;
2001 if (var
->mode
== vtn_variable_mode_output
)
2002 return stage
== MESA_SHADER_TESS_CTRL
;
2008 assign_missing_member_locations(struct vtn_variable
*var
)
2011 glsl_get_length(glsl_without_array(var
->type
->type
));
2012 int location
= var
->base_location
;
2014 for (unsigned i
= 0; i
< length
; i
++) {
2015 /* From the Vulkan spec:
2017 * “If the structure type is a Block but without a Location, then each
2018 * of its members must have a Location decoration.”
2021 if (var
->type
->block
) {
2022 assert(var
->base_location
!= -1 ||
2023 var
->var
->members
[i
].location
!= -1);
2026 /* From the Vulkan spec:
2028 * “Any member with its own Location decoration is assigned that
2029 * location. Each remaining member is assigned the location after the
2030 * immediately preceding member in declaration order.”
2032 if (var
->var
->members
[i
].location
!= -1)
2033 location
= var
->var
->members
[i
].location
;
2035 var
->var
->members
[i
].location
= location
;
2037 /* Below we use type instead of interface_type, because interface_type
2038 * is only available when it is a Block. This code also supports
2039 * input/outputs that are just structs
2041 const struct glsl_type
*member_type
=
2042 glsl_get_struct_field(glsl_without_array(var
->type
->type
), i
);
2045 glsl_count_attribute_slots(member_type
,
2046 false /* is_gl_vertex_input */);
2052 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
2053 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
2054 nir_constant
*initializer
)
2056 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
2057 struct vtn_type
*type
= ptr_type
->deref
;
2059 struct vtn_type
*without_array
= vtn_type_without_array(ptr_type
->deref
);
2061 enum vtn_variable_mode mode
;
2062 nir_variable_mode nir_mode
;
2063 mode
= vtn_storage_class_to_mode(b
, storage_class
, without_array
, &nir_mode
);
2066 case vtn_variable_mode_ubo
:
2067 /* There's no other way to get vtn_variable_mode_ubo */
2068 vtn_assert(without_array
->block
);
2069 b
->shader
->info
.num_ubos
++;
2071 case vtn_variable_mode_ssbo
:
2072 if (storage_class
== SpvStorageClassStorageBuffer
&&
2073 !without_array
->block
) {
2074 if (b
->variable_pointers
) {
2075 vtn_fail("Variables in the StorageBuffer storage class must "
2076 "have a struct type with the Block decoration");
2078 /* If variable pointers are not present, it's still malformed
2079 * SPIR-V but we can parse it and do the right thing anyway.
2080 * Since some of the 8-bit storage tests have bugs in this are,
2081 * just make it a warning for now.
2083 vtn_warn("Variables in the StorageBuffer storage class must "
2084 "have a struct type with the Block decoration");
2087 b
->shader
->info
.num_ssbos
++;
2089 case vtn_variable_mode_uniform
:
2090 if (glsl_type_is_image(without_array
->type
))
2091 b
->shader
->info
.num_images
++;
2092 else if (glsl_type_is_sampler(without_array
->type
))
2093 b
->shader
->info
.num_textures
++;
2095 case vtn_variable_mode_push_constant
:
2096 b
->shader
->num_uniforms
= vtn_type_block_size(b
, type
);
2099 case vtn_variable_mode_image
:
2100 vtn_fail("Cannot create a variable with the Image storage class");
2103 case vtn_variable_mode_phys_ssbo
:
2104 vtn_fail("Cannot create a variable with the "
2105 "PhysicalStorageBufferEXT storage class");
2109 /* No tallying is needed */
2113 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
2116 var
->base_location
= -1;
2118 vtn_assert(val
->value_type
== vtn_value_type_pointer
);
2119 val
->pointer
= vtn_pointer_for_variable(b
, var
, ptr_type
);
2121 switch (var
->mode
) {
2122 case vtn_variable_mode_function
:
2123 case vtn_variable_mode_private
:
2124 case vtn_variable_mode_uniform
:
2125 /* For these, we create the variable normally */
2126 var
->var
= rzalloc(b
->shader
, nir_variable
);
2127 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2129 if (storage_class
== SpvStorageClassAtomicCounter
) {
2130 /* Need to tweak the nir type here as at vtn_handle_type we don't
2131 * have the access to storage_class, that is the one that points us
2132 * that is an atomic uint.
2134 var
->var
->type
= repair_atomic_type(var
->type
->type
);
2136 /* Private variables don't have any explicit layout but some layouts
2137 * may have leaked through due to type deduplication in the SPIR-V.
2139 var
->var
->type
= var
->type
->type
;
2141 var
->var
->data
.mode
= nir_mode
;
2142 var
->var
->data
.location
= -1;
2143 var
->var
->interface_type
= NULL
;
2146 case vtn_variable_mode_workgroup
:
2147 if (b
->options
->lower_workgroup_access_to_offsets
) {
2148 var
->shared_location
= -1;
2150 /* Create the variable normally */
2151 var
->var
= rzalloc(b
->shader
, nir_variable
);
2152 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2153 /* Workgroup variables don't have any explicit layout but some
2154 * layouts may have leaked through due to type deduplication in the
2157 var
->var
->type
= var
->type
->type
;
2158 var
->var
->data
.mode
= nir_var_mem_shared
;
2162 case vtn_variable_mode_input
:
2163 case vtn_variable_mode_output
: {
2164 /* In order to know whether or not we're a per-vertex inout, we need
2165 * the patch qualifier. This means walking the variable decorations
2166 * early before we actually create any variables. Not a big deal.
2168 * GLSLang really likes to place decorations in the most interior
2169 * thing it possibly can. In particular, if you have a struct, it
2170 * will place the patch decorations on the struct members. This
2171 * should be handled by the variable splitting below just fine.
2173 * If you have an array-of-struct, things get even more weird as it
2174 * will place the patch decorations on the struct even though it's
2175 * inside an array and some of the members being patch and others not
2176 * makes no sense whatsoever. Since the only sensible thing is for
2177 * it to be all or nothing, we'll call it patch if any of the members
2178 * are declared patch.
2181 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
2182 if (glsl_type_is_array(var
->type
->type
) &&
2183 glsl_type_is_struct_or_ifc(without_array
->type
)) {
2184 vtn_foreach_decoration(b
, vtn_value(b
, without_array
->id
,
2185 vtn_value_type_type
),
2186 var_is_patch_cb
, &var
->patch
);
2189 /* For inputs and outputs, we immediately split structures. This
2190 * is for a couple of reasons. For one, builtins may all come in
2191 * a struct and we really want those split out into separate
2192 * variables. For another, interpolation qualifiers can be
2193 * applied to members of the top-level struct ane we need to be
2194 * able to preserve that information.
2197 struct vtn_type
*per_vertex_type
= var
->type
;
2198 if (is_per_vertex_inout(var
, b
->shader
->info
.stage
)) {
2199 /* In Geometry shaders (and some tessellation), inputs come
2200 * in per-vertex arrays. However, some builtins come in
2201 * non-per-vertex, hence the need for the is_array check. In
2202 * any case, there are no non-builtin arrays allowed so this
2203 * check should be sufficient.
2205 per_vertex_type
= var
->type
->array_element
;
2208 var
->var
= rzalloc(b
->shader
, nir_variable
);
2209 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
2210 /* In Vulkan, shader I/O variables don't have any explicit layout but
2211 * some layouts may have leaked through due to type deduplication in
2212 * the SPIR-V. We do, however, keep the layouts in the variable's
2213 * interface_type because we need offsets for XFB arrays of blocks.
2215 var
->var
->type
= var
->type
->type
;
2216 var
->var
->data
.mode
= nir_mode
;
2217 var
->var
->data
.patch
= var
->patch
;
2219 /* Figure out the interface block type. */
2220 struct vtn_type
*iface_type
= per_vertex_type
;
2221 if (var
->mode
== vtn_variable_mode_output
&&
2222 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
2223 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
||
2224 b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)) {
2225 /* For vertex data outputs, we can end up with arrays of blocks for
2226 * transform feedback where each array element corresponds to a
2227 * different XFB output buffer.
2229 while (iface_type
->base_type
== vtn_base_type_array
)
2230 iface_type
= iface_type
->array_element
;
2232 if (iface_type
->base_type
== vtn_base_type_struct
&& iface_type
->block
)
2233 var
->var
->interface_type
= iface_type
->type
;
2235 if (per_vertex_type
->base_type
== vtn_base_type_struct
&&
2236 per_vertex_type
->block
) {
2237 /* It's a struct. Set it up as per-member. */
2238 var
->var
->num_members
= glsl_get_length(per_vertex_type
->type
);
2239 var
->var
->members
= rzalloc_array(var
->var
, struct nir_variable_data
,
2240 var
->var
->num_members
);
2242 for (unsigned i
= 0; i
< var
->var
->num_members
; i
++) {
2243 var
->var
->members
[i
].mode
= nir_mode
;
2244 var
->var
->members
[i
].patch
= var
->patch
;
2245 var
->var
->members
[i
].location
= -1;
2249 /* For inputs and outputs, we need to grab locations and builtin
2250 * information from the per-vertex type.
2252 vtn_foreach_decoration(b
, vtn_value(b
, per_vertex_type
->id
,
2253 vtn_value_type_type
),
2254 var_decoration_cb
, var
);
2258 case vtn_variable_mode_ubo
:
2259 case vtn_variable_mode_ssbo
:
2260 case vtn_variable_mode_push_constant
:
2261 case vtn_variable_mode_cross_workgroup
:
2262 /* These don't need actual variables. */
2265 case vtn_variable_mode_image
:
2266 case vtn_variable_mode_phys_ssbo
:
2267 unreachable("Should have been caught before");
2271 var
->var
->constant_initializer
=
2272 nir_constant_clone(initializer
, var
->var
);
2275 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
2276 vtn_foreach_decoration(b
, val
, ptr_decoration_cb
, val
->pointer
);
2278 if ((var
->mode
== vtn_variable_mode_input
||
2279 var
->mode
== vtn_variable_mode_output
) &&
2280 var
->var
->members
) {
2281 assign_missing_member_locations(var
);
2284 if (var
->mode
== vtn_variable_mode_uniform
) {
2285 /* XXX: We still need the binding information in the nir_variable
2286 * for these. We should fix that.
2288 var
->var
->data
.binding
= var
->binding
;
2289 var
->var
->data
.explicit_binding
= var
->explicit_binding
;
2290 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
2291 var
->var
->data
.index
= var
->input_attachment_index
;
2292 var
->var
->data
.offset
= var
->offset
;
2294 if (glsl_type_is_image(without_array
->type
))
2295 var
->var
->data
.image
.format
= without_array
->image_format
;
2298 if (var
->mode
== vtn_variable_mode_function
) {
2299 vtn_assert(var
->var
!= NULL
&& var
->var
->members
== NULL
);
2300 nir_function_impl_add_variable(b
->nb
.impl
, var
->var
);
2301 } else if (var
->var
) {
2302 nir_shader_add_variable(b
->shader
, var
->var
);
2304 vtn_assert(vtn_pointer_is_external_block(b
, val
->pointer
));
2309 vtn_assert_types_equal(struct vtn_builder
*b
, SpvOp opcode
,
2310 struct vtn_type
*dst_type
,
2311 struct vtn_type
*src_type
)
2313 if (dst_type
->id
== src_type
->id
)
2316 if (vtn_types_compatible(b
, dst_type
, src_type
)) {
2317 /* Early versions of GLSLang would re-emit types unnecessarily and you
2318 * would end up with OpLoad, OpStore, or OpCopyMemory opcodes which have
2319 * mismatched source and destination types.
2321 * https://github.com/KhronosGroup/glslang/issues/304
2322 * https://github.com/KhronosGroup/glslang/issues/307
2323 * https://bugs.freedesktop.org/show_bug.cgi?id=104338
2324 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2326 vtn_warn("Source and destination types of %s do not have the same "
2327 "ID (but are compatible): %u vs %u",
2328 spirv_op_to_string(opcode
), dst_type
->id
, src_type
->id
);
2332 vtn_fail("Source and destination types of %s do not match: %s vs. %s",
2333 spirv_op_to_string(opcode
),
2334 glsl_get_type_name(dst_type
->type
),
2335 glsl_get_type_name(src_type
->type
));
2338 static nir_ssa_def
*
2339 nir_shrink_zero_pad_vec(nir_builder
*b
, nir_ssa_def
*val
,
2340 unsigned num_components
)
2342 if (val
->num_components
== num_components
)
2345 nir_ssa_def
*comps
[NIR_MAX_VEC_COMPONENTS
];
2346 for (unsigned i
= 0; i
< num_components
; i
++) {
2347 if (i
< val
->num_components
)
2348 comps
[i
] = nir_channel(b
, val
, i
);
2350 comps
[i
] = nir_imm_intN_t(b
, 0, val
->bit_size
);
2352 return nir_vec(b
, comps
, num_components
);
2355 static nir_ssa_def
*
2356 nir_sloppy_bitcast(nir_builder
*b
, nir_ssa_def
*val
,
2357 const struct glsl_type
*type
)
2359 const unsigned num_components
= glsl_get_vector_elements(type
);
2360 const unsigned bit_size
= glsl_get_bit_size(type
);
2362 /* First, zero-pad to ensure that the value is big enough that when we
2363 * bit-cast it, we don't loose anything.
2365 if (val
->bit_size
< bit_size
) {
2366 const unsigned src_num_components_needed
=
2367 vtn_align_u32(val
->num_components
, bit_size
/ val
->bit_size
);
2368 val
= nir_shrink_zero_pad_vec(b
, val
, src_num_components_needed
);
2371 val
= nir_bitcast_vector(b
, val
, bit_size
);
2373 return nir_shrink_zero_pad_vec(b
, val
, num_components
);
2377 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
2378 const uint32_t *w
, unsigned count
)
2382 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
2383 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2387 case SpvOpVariable
: {
2388 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2390 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2392 SpvStorageClass storage_class
= w
[3];
2393 nir_constant
*initializer
= NULL
;
2395 initializer
= vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
2397 vtn_create_variable(b
, val
, ptr_type
, storage_class
, initializer
);
2401 case SpvOpAccessChain
:
2402 case SpvOpPtrAccessChain
:
2403 case SpvOpInBoundsAccessChain
:
2404 case SpvOpInBoundsPtrAccessChain
: {
2405 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
2406 chain
->ptr_as_array
= (opcode
== SpvOpPtrAccessChain
|| opcode
== SpvOpInBoundsPtrAccessChain
);
2409 for (int i
= 4; i
< count
; i
++) {
2410 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
2411 if (link_val
->value_type
== vtn_value_type_constant
) {
2412 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
2413 chain
->link
[idx
].id
= vtn_constant_int(b
, w
[i
]);
2415 chain
->link
[idx
].mode
= vtn_access_mode_id
;
2416 chain
->link
[idx
].id
= w
[i
];
2422 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2423 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
2424 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
2425 /* This is rather insane. SPIR-V allows you to use OpSampledImage
2426 * to combine an array of images with a single sampler to get an
2427 * array of sampled images that all share the same sampler.
2428 * Fortunately, this means that we can more-or-less ignore the
2429 * sampler when crawling the access chain, but it does leave us
2430 * with this rather awkward little special-case.
2432 struct vtn_value
*val
=
2433 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2434 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2435 val
->sampled_image
->type
= base_val
->sampled_image
->type
;
2436 val
->sampled_image
->image
=
2437 vtn_pointer_dereference(b
, base_val
->sampled_image
->image
, chain
);
2438 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
2439 vtn_foreach_decoration(b
, val
, ptr_decoration_cb
,
2440 val
->sampled_image
->image
);
2441 vtn_foreach_decoration(b
, val
, ptr_decoration_cb
,
2442 val
->sampled_image
->sampler
);
2444 vtn_assert(base_val
->value_type
== vtn_value_type_pointer
);
2445 struct vtn_value
*val
=
2446 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2447 val
->pointer
= vtn_pointer_dereference(b
, base_val
->pointer
, chain
);
2448 val
->pointer
->ptr_type
= ptr_type
;
2449 vtn_foreach_decoration(b
, val
, ptr_decoration_cb
, val
->pointer
);
2454 case SpvOpCopyMemory
: {
2455 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2456 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
2458 vtn_assert_types_equal(b
, opcode
, dest
->type
->deref
, src
->type
->deref
);
2460 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
2465 struct vtn_type
*res_type
=
2466 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2467 struct vtn_value
*src_val
= vtn_value(b
, w
[3], vtn_value_type_pointer
);
2468 struct vtn_pointer
*src
= src_val
->pointer
;
2470 vtn_assert_types_equal(b
, opcode
, res_type
, src_val
->type
->deref
);
2472 if (glsl_type_is_image(res_type
->type
) ||
2473 glsl_type_is_sampler(res_type
->type
)) {
2474 vtn_push_value(b
, w
[2], vtn_value_type_pointer
)->pointer
= src
;
2478 vtn_push_ssa(b
, w
[2], res_type
, vtn_variable_load(b
, src
));
2483 struct vtn_value
*dest_val
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2484 struct vtn_pointer
*dest
= dest_val
->pointer
;
2485 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[2]);
2487 /* OpStore requires us to actually have a storage type */
2488 vtn_fail_if(dest
->type
->type
== NULL
,
2489 "Invalid destination type for OpStore");
2491 if (glsl_get_base_type(dest
->type
->type
) == GLSL_TYPE_BOOL
&&
2492 glsl_get_base_type(src_val
->type
->type
) == GLSL_TYPE_UINT
) {
2493 /* Early versions of GLSLang would use uint types for UBOs/SSBOs but
2494 * would then store them to a local variable as bool. Work around
2495 * the issue by doing an implicit conversion.
2497 * https://github.com/KhronosGroup/glslang/issues/170
2498 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2500 vtn_warn("OpStore of value of type OpTypeInt to a pointer to type "
2501 "OpTypeBool. Doing an implicit conversion to work around "
2503 struct vtn_ssa_value
*bool_ssa
=
2504 vtn_create_ssa_value(b
, dest
->type
->type
);
2505 bool_ssa
->def
= nir_i2b(&b
->nb
, vtn_ssa_value(b
, w
[2])->def
);
2506 vtn_variable_store(b
, bool_ssa
, dest
);
2510 vtn_assert_types_equal(b
, opcode
, dest_val
->type
->deref
, src_val
->type
);
2512 if (glsl_type_is_sampler(dest
->type
->type
)) {
2513 if (b
->wa_glslang_179
) {
2514 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
2515 "propagation to workaround the problem.");
2516 vtn_assert(dest
->var
->copy_prop_sampler
== NULL
);
2517 dest
->var
->copy_prop_sampler
=
2518 vtn_value(b
, w
[2], vtn_value_type_pointer
)->pointer
;
2520 vtn_fail("Vulkan does not allow OpStore of a sampler or image.");
2525 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
2526 vtn_variable_store(b
, src
, dest
);
2530 case SpvOpArrayLength
: {
2531 struct vtn_pointer
*ptr
=
2532 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2533 const uint32_t field
= w
[4];
2535 vtn_fail_if(ptr
->type
->base_type
!= vtn_base_type_struct
,
2536 "OpArrayLength must take a pointer to a structure type");
2537 vtn_fail_if(field
!= ptr
->type
->length
- 1 ||
2538 ptr
->type
->members
[field
]->base_type
!= vtn_base_type_array
,
2539 "OpArrayLength must reference the last memeber of the "
2540 "structure and that must be an array");
2542 const uint32_t offset
= ptr
->type
->offsets
[field
];
2543 const uint32_t stride
= ptr
->type
->members
[field
]->stride
;
2545 if (!ptr
->block_index
) {
2546 struct vtn_access_chain chain
= {
2549 ptr
= vtn_pointer_dereference(b
, ptr
, &chain
);
2550 vtn_assert(ptr
->block_index
);
2553 nir_intrinsic_instr
*instr
=
2554 nir_intrinsic_instr_create(b
->nb
.shader
,
2555 nir_intrinsic_get_buffer_size
);
2556 instr
->src
[0] = nir_src_for_ssa(ptr
->block_index
);
2557 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
2558 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2559 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
2561 /* array_length = max(buffer_size - offset, 0) / stride */
2562 nir_ssa_def
*array_length
=
2567 nir_imm_int(&b
->nb
, offset
)),
2568 nir_imm_int(&b
->nb
, 0u)),
2569 nir_imm_int(&b
->nb
, stride
));
2571 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2572 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
2573 val
->ssa
->def
= array_length
;
2577 case SpvOpConvertPtrToU
: {
2578 struct vtn_value
*u_val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2580 vtn_fail_if(u_val
->type
->base_type
!= vtn_base_type_vector
&&
2581 u_val
->type
->base_type
!= vtn_base_type_scalar
,
2582 "OpConvertPtrToU can only be used to cast to a vector or "
2585 /* The pointer will be converted to an SSA value automatically */
2586 nir_ssa_def
*ptr_ssa
= vtn_ssa_value(b
, w
[3])->def
;
2588 u_val
->ssa
= vtn_create_ssa_value(b
, u_val
->type
->type
);
2589 u_val
->ssa
->def
= nir_sloppy_bitcast(&b
->nb
, ptr_ssa
, u_val
->type
->type
);
2593 case SpvOpConvertUToPtr
: {
2594 struct vtn_value
*ptr_val
=
2595 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2596 struct vtn_value
*u_val
= vtn_value(b
, w
[3], vtn_value_type_ssa
);
2598 vtn_fail_if(ptr_val
->type
->type
== NULL
,
2599 "OpConvertUToPtr can only be used on physical pointers");
2601 vtn_fail_if(u_val
->type
->base_type
!= vtn_base_type_vector
&&
2602 u_val
->type
->base_type
!= vtn_base_type_scalar
,
2603 "OpConvertUToPtr can only be used to cast from a vector or "
2606 nir_ssa_def
*ptr_ssa
= nir_sloppy_bitcast(&b
->nb
, u_val
->ssa
->def
,
2607 ptr_val
->type
->type
);
2608 ptr_val
->pointer
= vtn_pointer_from_ssa(b
, ptr_ssa
, ptr_val
->type
);
2612 case SpvOpCopyMemorySized
:
2614 vtn_fail_with_opcode("Unhandled opcode", opcode
);