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_pointer_uses_ssa_offset(struct vtn_builder
*b
,
49 struct vtn_pointer
*ptr
)
51 return ptr
->mode
== vtn_variable_mode_ubo
||
52 ptr
->mode
== vtn_variable_mode_ssbo
||
53 ptr
->mode
== vtn_variable_mode_push_constant
||
54 (ptr
->mode
== vtn_variable_mode_workgroup
&&
55 b
->options
->lower_workgroup_access_to_offsets
);
59 vtn_pointer_is_external_block(struct vtn_builder
*b
,
60 struct vtn_pointer
*ptr
)
62 return ptr
->mode
== vtn_variable_mode_ssbo
||
63 ptr
->mode
== vtn_variable_mode_ubo
||
64 ptr
->mode
== vtn_variable_mode_push_constant
||
65 (ptr
->mode
== vtn_variable_mode_workgroup
&&
66 b
->options
->lower_workgroup_access_to_offsets
);
70 vtn_access_link_as_ssa(struct vtn_builder
*b
, struct vtn_access_link link
,
71 unsigned stride
, unsigned bit_size
)
73 vtn_assert(stride
> 0);
74 if (link
.mode
== vtn_access_mode_literal
) {
75 return nir_imm_intN_t(&b
->nb
, link
.id
* stride
, bit_size
);
77 nir_ssa_def
*ssa
= vtn_ssa_value(b
, link
.id
)->def
;
78 if (ssa
->bit_size
!= bit_size
)
79 ssa
= nir_i2i(&b
->nb
, ssa
, bit_size
);
81 ssa
= nir_imul_imm(&b
->nb
, ssa
, stride
);
86 /* Dereference the given base pointer by the access chain */
87 static struct vtn_pointer
*
88 vtn_nir_deref_pointer_dereference(struct vtn_builder
*b
,
89 struct vtn_pointer
*base
,
90 struct vtn_access_chain
*deref_chain
)
92 struct vtn_type
*type
= base
->type
;
93 enum gl_access_qualifier access
= base
->access
;
95 nir_deref_instr
*tail
;
99 assert(base
->var
&& base
->var
->var
);
100 tail
= nir_build_deref_var(&b
->nb
, base
->var
->var
);
103 /* OpPtrAccessChain is only allowed on things which support variable
104 * pointers. For everything else, the client is expected to just pass us
105 * the right access chain.
107 vtn_assert(!deref_chain
->ptr_as_array
);
109 for (unsigned i
= 0; i
< deref_chain
->length
; i
++) {
110 if (glsl_type_is_struct(type
->type
)) {
111 vtn_assert(deref_chain
->link
[i
].mode
== vtn_access_mode_literal
);
112 unsigned idx
= deref_chain
->link
[i
].id
;
113 tail
= nir_build_deref_struct(&b
->nb
, tail
, idx
);
114 type
= type
->members
[idx
];
116 nir_ssa_def
*index
= vtn_access_link_as_ssa(b
, deref_chain
->link
[i
], 1,
117 tail
->dest
.ssa
.bit_size
);
118 tail
= nir_build_deref_array(&b
->nb
, tail
, index
);
119 type
= type
->array_element
;
122 access
|= type
->access
;
125 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
126 ptr
->mode
= base
->mode
;
128 ptr
->var
= base
->var
;
130 ptr
->access
= access
;
135 static VkDescriptorType
136 vk_desc_type_for_mode(struct vtn_builder
*b
, enum vtn_variable_mode mode
)
139 case vtn_variable_mode_ubo
:
140 return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
;
141 case vtn_variable_mode_ssbo
:
142 return VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
;
144 vtn_fail("Invalid mode for vulkan_resource_index");
149 vtn_variable_resource_index(struct vtn_builder
*b
, struct vtn_variable
*var
,
150 nir_ssa_def
*desc_array_index
)
152 if (!desc_array_index
) {
153 vtn_assert(glsl_type_is_struct(var
->type
->type
));
154 desc_array_index
= nir_imm_int(&b
->nb
, 0);
157 nir_intrinsic_instr
*instr
=
158 nir_intrinsic_instr_create(b
->nb
.shader
,
159 nir_intrinsic_vulkan_resource_index
);
160 instr
->src
[0] = nir_src_for_ssa(desc_array_index
);
161 nir_intrinsic_set_desc_set(instr
, var
->descriptor_set
);
162 nir_intrinsic_set_binding(instr
, var
->binding
);
163 nir_intrinsic_set_desc_type(instr
, vk_desc_type_for_mode(b
, var
->mode
));
165 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
166 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
168 return &instr
->dest
.ssa
;
172 vtn_resource_reindex(struct vtn_builder
*b
, enum vtn_variable_mode mode
,
173 nir_ssa_def
*base_index
, nir_ssa_def
*offset_index
)
175 nir_intrinsic_instr
*instr
=
176 nir_intrinsic_instr_create(b
->nb
.shader
,
177 nir_intrinsic_vulkan_resource_reindex
);
178 instr
->src
[0] = nir_src_for_ssa(base_index
);
179 instr
->src
[1] = nir_src_for_ssa(offset_index
);
180 nir_intrinsic_set_desc_type(instr
, vk_desc_type_for_mode(b
, mode
));
182 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
183 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
185 return &instr
->dest
.ssa
;
188 static struct vtn_pointer
*
189 vtn_ssa_offset_pointer_dereference(struct vtn_builder
*b
,
190 struct vtn_pointer
*base
,
191 struct vtn_access_chain
*deref_chain
)
193 nir_ssa_def
*block_index
= base
->block_index
;
194 nir_ssa_def
*offset
= base
->offset
;
195 struct vtn_type
*type
= base
->type
;
196 enum gl_access_qualifier access
= base
->access
;
199 if (base
->mode
== vtn_variable_mode_ubo
||
200 base
->mode
== vtn_variable_mode_ssbo
) {
202 vtn_assert(base
->var
&& base
->type
);
203 nir_ssa_def
*desc_arr_idx
;
204 if (glsl_type_is_array(type
->type
)) {
205 if (deref_chain
->length
>= 1) {
207 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
209 /* This consumes a level of type */
210 type
= type
->array_element
;
211 access
|= type
->access
;
213 /* This is annoying. We've been asked for a pointer to the
214 * array of UBOs/SSBOs and not a specifc buffer. Return a
215 * pointer with a descriptor index of 0 and we'll have to do
216 * a reindex later to adjust it to the right thing.
218 desc_arr_idx
= nir_imm_int(&b
->nb
, 0);
220 } else if (deref_chain
->ptr_as_array
) {
221 /* You can't have a zero-length OpPtrAccessChain */
222 vtn_assert(deref_chain
->length
>= 1);
223 desc_arr_idx
= vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
225 /* We have a regular non-array SSBO. */
228 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
229 } else if (deref_chain
->ptr_as_array
&&
230 type
->base_type
== vtn_base_type_struct
&& type
->block
) {
231 /* We are doing an OpPtrAccessChain on a pointer to a struct that is
232 * decorated block. This is an interesting corner in the SPIR-V
233 * spec. One interpretation would be that they client is clearly
234 * trying to treat that block as if it's an implicit array of blocks
235 * repeated in the buffer. However, the SPIR-V spec for the
236 * OpPtrAccessChain says:
238 * "Base is treated as the address of the first element of an
239 * array, and the Element element’s address is computed to be the
240 * base for the Indexes, as per OpAccessChain."
242 * Taken literally, that would mean that your struct type is supposed
243 * to be treated as an array of such a struct and, since it's
244 * decorated block, that means an array of blocks which corresponds
245 * to an array descriptor. Therefore, we need to do a reindex
246 * operation to add the index from the first link in the access chain
247 * to the index we recieved.
249 * The downside to this interpretation (there always is one) is that
250 * this might be somewhat surprising behavior to apps if they expect
251 * the implicit array behavior described above.
253 vtn_assert(deref_chain
->length
>= 1);
254 nir_ssa_def
*offset_index
=
255 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
258 block_index
= vtn_resource_reindex(b
, base
->mode
,
259 block_index
, offset_index
);
264 if (base
->mode
== vtn_variable_mode_workgroup
) {
265 /* SLM doesn't need nor have a block index */
266 vtn_assert(!block_index
);
268 /* We need the variable for the base offset */
269 vtn_assert(base
->var
);
271 /* We need ptr_type for size and alignment */
272 vtn_assert(base
->ptr_type
);
274 /* Assign location on first use so that we don't end up bloating SLM
275 * address space for variables which are never statically used.
277 if (base
->var
->shared_location
< 0) {
278 vtn_assert(base
->ptr_type
->length
> 0 && base
->ptr_type
->align
> 0);
279 b
->shader
->num_shared
= vtn_align_u32(b
->shader
->num_shared
,
280 base
->ptr_type
->align
);
281 base
->var
->shared_location
= b
->shader
->num_shared
;
282 b
->shader
->num_shared
+= base
->ptr_type
->length
;
285 offset
= nir_imm_int(&b
->nb
, base
->var
->shared_location
);
286 } else if (base
->mode
== vtn_variable_mode_push_constant
) {
287 /* Push constants neither need nor have a block index */
288 vtn_assert(!block_index
);
290 /* Start off with at the start of the push constant block. */
291 offset
= nir_imm_int(&b
->nb
, 0);
293 /* The code above should have ensured a block_index when needed. */
294 vtn_assert(block_index
);
296 /* Start off with at the start of the buffer. */
297 offset
= nir_imm_int(&b
->nb
, 0);
301 if (deref_chain
->ptr_as_array
&& idx
== 0) {
302 /* We need ptr_type for the stride */
303 vtn_assert(base
->ptr_type
);
305 /* We need at least one element in the chain */
306 vtn_assert(deref_chain
->length
>= 1);
308 nir_ssa_def
*elem_offset
=
309 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
310 base
->ptr_type
->stride
, offset
->bit_size
);
311 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
315 for (; idx
< deref_chain
->length
; idx
++) {
316 switch (glsl_get_base_type(type
->type
)) {
319 case GLSL_TYPE_UINT16
:
320 case GLSL_TYPE_INT16
:
321 case GLSL_TYPE_UINT8
:
323 case GLSL_TYPE_UINT64
:
324 case GLSL_TYPE_INT64
:
325 case GLSL_TYPE_FLOAT
:
326 case GLSL_TYPE_FLOAT16
:
327 case GLSL_TYPE_DOUBLE
:
329 case GLSL_TYPE_ARRAY
: {
330 nir_ssa_def
*elem_offset
=
331 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
332 type
->stride
, offset
->bit_size
);
333 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
334 type
= type
->array_element
;
335 access
|= type
->access
;
339 case GLSL_TYPE_STRUCT
: {
340 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
341 unsigned member
= deref_chain
->link
[idx
].id
;
342 offset
= nir_iadd_imm(&b
->nb
, offset
, type
->offsets
[member
]);
343 type
= type
->members
[member
];
344 access
|= type
->access
;
349 vtn_fail("Invalid type for deref");
353 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
354 ptr
->mode
= base
->mode
;
356 ptr
->block_index
= block_index
;
357 ptr
->offset
= offset
;
358 ptr
->access
= access
;
363 /* Dereference the given base pointer by the access chain */
364 static struct vtn_pointer
*
365 vtn_pointer_dereference(struct vtn_builder
*b
,
366 struct vtn_pointer
*base
,
367 struct vtn_access_chain
*deref_chain
)
369 if (vtn_pointer_uses_ssa_offset(b
, base
)) {
370 return vtn_ssa_offset_pointer_dereference(b
, base
, deref_chain
);
372 return vtn_nir_deref_pointer_dereference(b
, base
, deref_chain
);
377 vtn_pointer_for_variable(struct vtn_builder
*b
,
378 struct vtn_variable
*var
, struct vtn_type
*ptr_type
)
380 struct vtn_pointer
*pointer
= rzalloc(b
, struct vtn_pointer
);
382 pointer
->mode
= var
->mode
;
383 pointer
->type
= var
->type
;
384 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
385 vtn_assert(ptr_type
->deref
->type
== var
->type
->type
);
386 pointer
->ptr_type
= ptr_type
;
388 pointer
->access
= var
->access
| var
->type
->access
;
393 /* Returns an atomic_uint type based on the original uint type. The returned
394 * type will be equivalent to the original one but will have an atomic_uint
395 * type as leaf instead of an uint.
397 * Manages uint scalars, arrays, and arrays of arrays of any nested depth.
399 static const struct glsl_type
*
400 repair_atomic_type(const struct glsl_type
*type
)
402 assert(glsl_get_base_type(glsl_without_array(type
)) == GLSL_TYPE_UINT
);
403 assert(glsl_type_is_scalar(glsl_without_array(type
)));
405 if (glsl_type_is_array(type
)) {
406 const struct glsl_type
*atomic
=
407 repair_atomic_type(glsl_get_array_element(type
));
409 return glsl_array_type(atomic
, glsl_get_length(type
),
410 glsl_get_explicit_stride(type
));
412 return glsl_atomic_uint_type();
417 vtn_pointer_to_deref(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
419 /* Do on-the-fly copy propagation for samplers. */
420 if (ptr
->var
&& ptr
->var
->copy_prop_sampler
)
421 return vtn_pointer_to_deref(b
, ptr
->var
->copy_prop_sampler
);
423 vtn_assert(!vtn_pointer_uses_ssa_offset(b
, ptr
));
425 struct vtn_access_chain chain
= {
428 ptr
= vtn_nir_deref_pointer_dereference(b
, ptr
, &chain
);
435 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_instr
*deref
,
436 struct vtn_ssa_value
*inout
)
438 if (glsl_type_is_vector_or_scalar(deref
->type
)) {
440 inout
->def
= nir_load_deref(&b
->nb
, deref
);
442 nir_store_deref(&b
->nb
, deref
, inout
->def
, ~0);
444 } else if (glsl_type_is_array(deref
->type
) ||
445 glsl_type_is_matrix(deref
->type
)) {
446 unsigned elems
= glsl_get_length(deref
->type
);
447 for (unsigned i
= 0; i
< elems
; i
++) {
448 nir_deref_instr
*child
=
449 nir_build_deref_array(&b
->nb
, deref
, nir_imm_int(&b
->nb
, i
));
450 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
]);
453 vtn_assert(glsl_type_is_struct(deref
->type
));
454 unsigned elems
= glsl_get_length(deref
->type
);
455 for (unsigned i
= 0; i
< elems
; i
++) {
456 nir_deref_instr
*child
= nir_build_deref_struct(&b
->nb
, deref
, i
);
457 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
]);
463 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
465 struct vtn_pointer
*ptr
= vtn_value(b
, id
, vtn_value_type_pointer
)->pointer
;
466 return vtn_pointer_to_deref(b
, ptr
);
470 * Gets the NIR-level deref tail, which may have as a child an array deref
471 * selecting which component due to OpAccessChain supporting per-component
472 * indexing in SPIR-V.
474 static nir_deref_instr
*
475 get_deref_tail(nir_deref_instr
*deref
)
477 if (deref
->deref_type
!= nir_deref_type_array
)
480 nir_deref_instr
*parent
=
481 nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
);
483 if (glsl_type_is_vector(parent
->type
))
489 struct vtn_ssa_value
*
490 vtn_local_load(struct vtn_builder
*b
, nir_deref_instr
*src
)
492 nir_deref_instr
*src_tail
= get_deref_tail(src
);
493 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
494 _vtn_local_load_store(b
, true, src_tail
, val
);
496 if (src_tail
!= src
) {
497 val
->type
= src
->type
;
498 if (nir_src_is_const(src
->arr
.index
))
499 val
->def
= vtn_vector_extract(b
, val
->def
,
500 nir_src_as_uint(src
->arr
.index
));
502 val
->def
= vtn_vector_extract_dynamic(b
, val
->def
, src
->arr
.index
.ssa
);
509 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
510 nir_deref_instr
*dest
)
512 nir_deref_instr
*dest_tail
= get_deref_tail(dest
);
514 if (dest_tail
!= dest
) {
515 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
516 _vtn_local_load_store(b
, true, dest_tail
, val
);
518 if (nir_src_is_const(dest
->arr
.index
))
519 val
->def
= vtn_vector_insert(b
, val
->def
, src
->def
,
520 nir_src_as_uint(dest
->arr
.index
));
522 val
->def
= vtn_vector_insert_dynamic(b
, val
->def
, src
->def
,
523 dest
->arr
.index
.ssa
);
524 _vtn_local_load_store(b
, false, dest_tail
, val
);
526 _vtn_local_load_store(b
, false, dest_tail
, src
);
531 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
532 nir_ssa_def
**index_out
)
534 assert(vtn_pointer_uses_ssa_offset(b
, ptr
));
536 struct vtn_access_chain chain
= {
539 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
541 *index_out
= ptr
->block_index
;
545 /* Tries to compute the size of an interface block based on the strides and
546 * offsets that are provided to us in the SPIR-V source.
549 vtn_type_block_size(struct vtn_builder
*b
, struct vtn_type
*type
)
551 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
555 case GLSL_TYPE_UINT16
:
556 case GLSL_TYPE_INT16
:
557 case GLSL_TYPE_UINT8
:
559 case GLSL_TYPE_UINT64
:
560 case GLSL_TYPE_INT64
:
561 case GLSL_TYPE_FLOAT
:
562 case GLSL_TYPE_FLOAT16
:
564 case GLSL_TYPE_DOUBLE
: {
565 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
566 glsl_get_matrix_columns(type
->type
);
568 vtn_assert(type
->stride
> 0);
569 return type
->stride
* cols
;
571 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
572 return glsl_get_vector_elements(type
->type
) * type_size
;
576 case GLSL_TYPE_STRUCT
:
577 case GLSL_TYPE_INTERFACE
: {
579 unsigned num_fields
= glsl_get_length(type
->type
);
580 for (unsigned f
= 0; f
< num_fields
; f
++) {
581 unsigned field_end
= type
->offsets
[f
] +
582 vtn_type_block_size(b
, type
->members
[f
]);
583 size
= MAX2(size
, field_end
);
588 case GLSL_TYPE_ARRAY
:
589 vtn_assert(type
->stride
> 0);
590 vtn_assert(glsl_get_length(type
->type
) > 0);
591 return type
->stride
* glsl_get_length(type
->type
);
594 vtn_fail("Invalid block type");
600 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
601 nir_ssa_def
*index
, nir_ssa_def
*offset
,
602 unsigned access_offset
, unsigned access_size
,
603 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
,
604 enum gl_access_qualifier access
)
606 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
607 instr
->num_components
= glsl_get_vector_elements(type
);
609 /* Booleans usually shouldn't show up in external memory in SPIR-V.
610 * However, they do for certain older GLSLang versions and can for shared
611 * memory when we lower access chains internally.
613 const unsigned data_bit_size
= glsl_type_is_boolean(type
) ? 32 :
614 glsl_get_bit_size(type
);
618 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
619 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
622 if (op
== nir_intrinsic_load_push_constant
) {
623 nir_intrinsic_set_base(instr
, access_offset
);
624 nir_intrinsic_set_range(instr
, access_size
);
627 if (op
== nir_intrinsic_load_ssbo
||
628 op
== nir_intrinsic_store_ssbo
) {
629 nir_intrinsic_set_access(instr
, access
);
632 /* With extensions like relaxed_block_layout, we really can't guarantee
633 * much more than scalar alignment.
635 if (op
!= nir_intrinsic_load_push_constant
)
636 nir_intrinsic_set_align(instr
, data_bit_size
/ 8, 0);
639 instr
->src
[src
++] = nir_src_for_ssa(index
);
641 if (op
== nir_intrinsic_load_push_constant
) {
642 /* We need to subtract the offset from where the intrinsic will load the
645 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
646 nir_imm_int(&b
->nb
, access_offset
)));
648 instr
->src
[src
++] = nir_src_for_ssa(offset
);
652 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
653 instr
->num_components
, data_bit_size
, NULL
);
654 (*inout
)->def
= &instr
->dest
.ssa
;
657 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
659 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
660 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
664 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
665 nir_ssa_def
*index
, nir_ssa_def
*offset
,
666 unsigned access_offset
, unsigned access_size
,
667 struct vtn_type
*type
, enum gl_access_qualifier access
,
668 struct vtn_ssa_value
**inout
)
670 if (load
&& *inout
== NULL
)
671 *inout
= vtn_create_ssa_value(b
, type
->type
);
673 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
677 case GLSL_TYPE_UINT16
:
678 case GLSL_TYPE_INT16
:
679 case GLSL_TYPE_UINT8
:
681 case GLSL_TYPE_UINT64
:
682 case GLSL_TYPE_INT64
:
683 case GLSL_TYPE_FLOAT
:
684 case GLSL_TYPE_FLOAT16
:
685 case GLSL_TYPE_DOUBLE
:
687 /* This is where things get interesting. At this point, we've hit
688 * a vector, a scalar, or a matrix.
690 if (glsl_type_is_matrix(type
->type
)) {
691 /* Loading the whole matrix */
692 struct vtn_ssa_value
*transpose
;
693 unsigned num_ops
, vec_width
, col_stride
;
694 if (type
->row_major
) {
695 num_ops
= glsl_get_vector_elements(type
->type
);
696 vec_width
= glsl_get_matrix_columns(type
->type
);
697 col_stride
= type
->array_element
->stride
;
699 const struct glsl_type
*transpose_type
=
700 glsl_matrix_type(base_type
, vec_width
, num_ops
);
701 *inout
= vtn_create_ssa_value(b
, transpose_type
);
703 transpose
= vtn_ssa_transpose(b
, *inout
);
707 num_ops
= glsl_get_matrix_columns(type
->type
);
708 vec_width
= glsl_get_vector_elements(type
->type
);
709 col_stride
= type
->stride
;
712 for (unsigned i
= 0; i
< num_ops
; i
++) {
713 nir_ssa_def
*elem_offset
=
714 nir_iadd_imm(&b
->nb
, offset
, i
* col_stride
);
715 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
716 access_offset
, access_size
,
718 glsl_vector_type(base_type
, vec_width
),
719 type
->access
| access
);
722 if (load
&& type
->row_major
)
723 *inout
= vtn_ssa_transpose(b
, *inout
);
725 unsigned elems
= glsl_get_vector_elements(type
->type
);
726 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
727 if (elems
== 1 || type
->stride
== type_size
) {
728 /* This is a tightly-packed normal scalar or vector load */
729 vtn_assert(glsl_type_is_vector_or_scalar(type
->type
));
730 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
731 access_offset
, access_size
,
733 type
->access
| access
);
735 /* This is a strided load. We have to load N things separately.
736 * This is the single column of a row-major matrix case.
738 vtn_assert(type
->stride
> type_size
);
739 vtn_assert(type
->stride
% type_size
== 0);
741 nir_ssa_def
*per_comp
[4];
742 for (unsigned i
= 0; i
< elems
; i
++) {
743 nir_ssa_def
*elem_offset
=
744 nir_iadd_imm(&b
->nb
, offset
, i
* type
->stride
);
745 struct vtn_ssa_value
*comp
, temp_val
;
747 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
748 temp_val
.type
= glsl_scalar_type(base_type
);
751 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
752 access_offset
, access_size
,
753 &comp
, glsl_scalar_type(base_type
),
754 type
->access
| access
);
755 per_comp
[i
] = comp
->def
;
760 *inout
= vtn_create_ssa_value(b
, type
->type
);
761 (*inout
)->def
= nir_vec(&b
->nb
, per_comp
, elems
);
767 case GLSL_TYPE_ARRAY
: {
768 unsigned elems
= glsl_get_length(type
->type
);
769 for (unsigned i
= 0; i
< elems
; i
++) {
770 nir_ssa_def
*elem_off
=
771 nir_iadd_imm(&b
->nb
, offset
, i
* type
->stride
);
772 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
773 access_offset
, access_size
,
775 type
->array_element
->access
| access
,
776 &(*inout
)->elems
[i
]);
781 case GLSL_TYPE_STRUCT
: {
782 unsigned elems
= glsl_get_length(type
->type
);
783 for (unsigned i
= 0; i
< elems
; i
++) {
784 nir_ssa_def
*elem_off
=
785 nir_iadd_imm(&b
->nb
, offset
, type
->offsets
[i
]);
786 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
787 access_offset
, access_size
,
789 type
->members
[i
]->access
| access
,
790 &(*inout
)->elems
[i
]);
796 vtn_fail("Invalid block member type");
800 static struct vtn_ssa_value
*
801 vtn_block_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
804 unsigned access_offset
= 0, access_size
= 0;
806 case vtn_variable_mode_ubo
:
807 op
= nir_intrinsic_load_ubo
;
809 case vtn_variable_mode_ssbo
:
810 op
= nir_intrinsic_load_ssbo
;
812 case vtn_variable_mode_push_constant
:
813 op
= nir_intrinsic_load_push_constant
;
814 access_size
= b
->shader
->num_uniforms
;
816 case vtn_variable_mode_workgroup
:
817 op
= nir_intrinsic_load_shared
;
820 vtn_fail("Invalid block variable mode");
823 nir_ssa_def
*offset
, *index
= NULL
;
824 offset
= vtn_pointer_to_offset(b
, src
, &index
);
826 struct vtn_ssa_value
*value
= NULL
;
827 _vtn_block_load_store(b
, op
, true, index
, offset
,
828 access_offset
, access_size
,
829 src
->type
, src
->access
, &value
);
834 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
835 struct vtn_pointer
*dst
)
839 case vtn_variable_mode_ssbo
:
840 op
= nir_intrinsic_store_ssbo
;
842 case vtn_variable_mode_workgroup
:
843 op
= nir_intrinsic_store_shared
;
846 vtn_fail("Invalid block variable mode");
849 nir_ssa_def
*offset
, *index
= NULL
;
850 offset
= vtn_pointer_to_offset(b
, dst
, &index
);
852 _vtn_block_load_store(b
, op
, false, index
, offset
,
853 0, 0, dst
->type
, dst
->access
, &src
);
857 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
858 struct vtn_pointer
*ptr
,
859 struct vtn_ssa_value
**inout
)
861 enum glsl_base_type base_type
= glsl_get_base_type(ptr
->type
->type
);
865 case GLSL_TYPE_UINT16
:
866 case GLSL_TYPE_INT16
:
867 case GLSL_TYPE_UINT8
:
869 case GLSL_TYPE_UINT64
:
870 case GLSL_TYPE_INT64
:
871 case GLSL_TYPE_FLOAT
:
872 case GLSL_TYPE_FLOAT16
:
874 case GLSL_TYPE_DOUBLE
:
875 /* At this point, we have a scalar, vector, or matrix so we know that
876 * there cannot be any structure splitting still in the way. By
877 * stopping at the matrix level rather than the vector level, we
878 * ensure that matrices get loaded in the optimal way even if they
879 * are storred row-major in a UBO.
882 *inout
= vtn_local_load(b
, vtn_pointer_to_deref(b
, ptr
));
884 vtn_local_store(b
, *inout
, vtn_pointer_to_deref(b
, ptr
));
888 case GLSL_TYPE_ARRAY
:
889 case GLSL_TYPE_STRUCT
: {
890 unsigned elems
= glsl_get_length(ptr
->type
->type
);
892 vtn_assert(*inout
== NULL
);
893 *inout
= rzalloc(b
, struct vtn_ssa_value
);
894 (*inout
)->type
= ptr
->type
->type
;
895 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
898 struct vtn_access_chain chain
= {
901 { .mode
= vtn_access_mode_literal
, },
904 for (unsigned i
= 0; i
< elems
; i
++) {
905 chain
.link
[0].id
= i
;
906 struct vtn_pointer
*elem
= vtn_pointer_dereference(b
, ptr
, &chain
);
907 _vtn_variable_load_store(b
, load
, elem
, &(*inout
)->elems
[i
]);
913 vtn_fail("Invalid access chain type");
917 struct vtn_ssa_value
*
918 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
920 if (vtn_pointer_uses_ssa_offset(b
, src
)) {
921 return vtn_block_load(b
, src
);
923 struct vtn_ssa_value
*val
= NULL
;
924 _vtn_variable_load_store(b
, true, src
, &val
);
930 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
931 struct vtn_pointer
*dest
)
933 if (vtn_pointer_uses_ssa_offset(b
, dest
)) {
934 vtn_assert(dest
->mode
== vtn_variable_mode_ssbo
||
935 dest
->mode
== vtn_variable_mode_workgroup
);
936 vtn_block_store(b
, src
, dest
);
938 _vtn_variable_load_store(b
, false, dest
, &src
);
943 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
944 struct vtn_pointer
*src
)
946 vtn_assert(src
->type
->type
== dest
->type
->type
);
947 enum glsl_base_type base_type
= glsl_get_base_type(src
->type
->type
);
951 case GLSL_TYPE_UINT16
:
952 case GLSL_TYPE_INT16
:
953 case GLSL_TYPE_UINT8
:
955 case GLSL_TYPE_UINT64
:
956 case GLSL_TYPE_INT64
:
957 case GLSL_TYPE_FLOAT
:
958 case GLSL_TYPE_FLOAT16
:
959 case GLSL_TYPE_DOUBLE
:
961 /* At this point, we have a scalar, vector, or matrix so we know that
962 * there cannot be any structure splitting still in the way. By
963 * stopping at the matrix level rather than the vector level, we
964 * ensure that matrices get loaded in the optimal way even if they
965 * are storred row-major in a UBO.
967 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
970 case GLSL_TYPE_ARRAY
:
971 case GLSL_TYPE_STRUCT
: {
972 struct vtn_access_chain chain
= {
975 { .mode
= vtn_access_mode_literal
, },
978 unsigned elems
= glsl_get_length(src
->type
->type
);
979 for (unsigned i
= 0; i
< elems
; i
++) {
980 chain
.link
[0].id
= i
;
981 struct vtn_pointer
*src_elem
=
982 vtn_pointer_dereference(b
, src
, &chain
);
983 struct vtn_pointer
*dest_elem
=
984 vtn_pointer_dereference(b
, dest
, &chain
);
986 _vtn_variable_copy(b
, dest_elem
, src_elem
);
992 vtn_fail("Invalid access chain type");
997 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
998 struct vtn_pointer
*src
)
1000 /* TODO: At some point, we should add a special-case for when we can
1001 * just emit a copy_var intrinsic.
1003 _vtn_variable_copy(b
, dest
, src
);
1007 set_mode_system_value(struct vtn_builder
*b
, nir_variable_mode
*mode
)
1009 vtn_assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
1010 *mode
= nir_var_system_value
;
1014 vtn_get_builtin_location(struct vtn_builder
*b
,
1015 SpvBuiltIn builtin
, int *location
,
1016 nir_variable_mode
*mode
)
1019 case SpvBuiltInPosition
:
1020 *location
= VARYING_SLOT_POS
;
1022 case SpvBuiltInPointSize
:
1023 *location
= VARYING_SLOT_PSIZ
;
1025 case SpvBuiltInClipDistance
:
1026 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
1028 case SpvBuiltInCullDistance
:
1029 *location
= VARYING_SLOT_CULL_DIST0
;
1031 case SpvBuiltInVertexId
:
1032 case SpvBuiltInVertexIndex
:
1033 /* The Vulkan spec defines VertexIndex to be non-zero-based and doesn't
1034 * allow VertexId. The ARB_gl_spirv spec defines VertexId to be the
1035 * same as gl_VertexID, which is non-zero-based, and removes
1036 * VertexIndex. Since they're both defined to be non-zero-based, we use
1037 * SYSTEM_VALUE_VERTEX_ID for both.
1039 *location
= SYSTEM_VALUE_VERTEX_ID
;
1040 set_mode_system_value(b
, mode
);
1042 case SpvBuiltInInstanceIndex
:
1043 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
1044 set_mode_system_value(b
, mode
);
1046 case SpvBuiltInInstanceId
:
1047 *location
= SYSTEM_VALUE_INSTANCE_ID
;
1048 set_mode_system_value(b
, mode
);
1050 case SpvBuiltInPrimitiveId
:
1051 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1052 vtn_assert(*mode
== nir_var_shader_in
);
1053 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1054 } else if (*mode
== nir_var_shader_out
) {
1055 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1057 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
1058 set_mode_system_value(b
, mode
);
1061 case SpvBuiltInInvocationId
:
1062 *location
= SYSTEM_VALUE_INVOCATION_ID
;
1063 set_mode_system_value(b
, mode
);
1065 case SpvBuiltInLayer
:
1066 *location
= VARYING_SLOT_LAYER
;
1067 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1068 *mode
= nir_var_shader_in
;
1069 else if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1070 *mode
= nir_var_shader_out
;
1071 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1072 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1073 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1074 *mode
= nir_var_shader_out
;
1076 vtn_fail("invalid stage for SpvBuiltInLayer");
1078 case SpvBuiltInViewportIndex
:
1079 *location
= VARYING_SLOT_VIEWPORT
;
1080 if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1081 *mode
= nir_var_shader_out
;
1082 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1083 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1084 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1085 *mode
= nir_var_shader_out
;
1086 else if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1087 *mode
= nir_var_shader_in
;
1089 vtn_fail("invalid stage for SpvBuiltInViewportIndex");
1091 case SpvBuiltInTessLevelOuter
:
1092 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
1094 case SpvBuiltInTessLevelInner
:
1095 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
1097 case SpvBuiltInTessCoord
:
1098 *location
= SYSTEM_VALUE_TESS_COORD
;
1099 set_mode_system_value(b
, mode
);
1101 case SpvBuiltInPatchVertices
:
1102 *location
= SYSTEM_VALUE_VERTICES_IN
;
1103 set_mode_system_value(b
, mode
);
1105 case SpvBuiltInFragCoord
:
1106 *location
= VARYING_SLOT_POS
;
1107 vtn_assert(*mode
== nir_var_shader_in
);
1109 case SpvBuiltInPointCoord
:
1110 *location
= VARYING_SLOT_PNTC
;
1111 vtn_assert(*mode
== nir_var_shader_in
);
1113 case SpvBuiltInFrontFacing
:
1114 *location
= SYSTEM_VALUE_FRONT_FACE
;
1115 set_mode_system_value(b
, mode
);
1117 case SpvBuiltInSampleId
:
1118 *location
= SYSTEM_VALUE_SAMPLE_ID
;
1119 set_mode_system_value(b
, mode
);
1121 case SpvBuiltInSamplePosition
:
1122 *location
= SYSTEM_VALUE_SAMPLE_POS
;
1123 set_mode_system_value(b
, mode
);
1125 case SpvBuiltInSampleMask
:
1126 if (*mode
== nir_var_shader_out
) {
1127 *location
= FRAG_RESULT_SAMPLE_MASK
;
1129 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1130 set_mode_system_value(b
, mode
);
1133 case SpvBuiltInFragDepth
:
1134 *location
= FRAG_RESULT_DEPTH
;
1135 vtn_assert(*mode
== nir_var_shader_out
);
1137 case SpvBuiltInHelperInvocation
:
1138 *location
= SYSTEM_VALUE_HELPER_INVOCATION
;
1139 set_mode_system_value(b
, mode
);
1141 case SpvBuiltInNumWorkgroups
:
1142 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1143 set_mode_system_value(b
, mode
);
1145 case SpvBuiltInWorkgroupSize
:
1146 *location
= SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
1147 set_mode_system_value(b
, mode
);
1149 case SpvBuiltInWorkgroupId
:
1150 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1151 set_mode_system_value(b
, mode
);
1153 case SpvBuiltInLocalInvocationId
:
1154 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1155 set_mode_system_value(b
, mode
);
1157 case SpvBuiltInLocalInvocationIndex
:
1158 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1159 set_mode_system_value(b
, mode
);
1161 case SpvBuiltInGlobalInvocationId
:
1162 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1163 set_mode_system_value(b
, mode
);
1165 case SpvBuiltInBaseVertex
:
1166 /* OpenGL gl_BaseVertex (SYSTEM_VALUE_BASE_VERTEX) is not the same
1167 * semantic as SPIR-V BaseVertex (SYSTEM_VALUE_FIRST_VERTEX).
1169 *location
= SYSTEM_VALUE_FIRST_VERTEX
;
1170 set_mode_system_value(b
, mode
);
1172 case SpvBuiltInBaseInstance
:
1173 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1174 set_mode_system_value(b
, mode
);
1176 case SpvBuiltInDrawIndex
:
1177 *location
= SYSTEM_VALUE_DRAW_ID
;
1178 set_mode_system_value(b
, mode
);
1180 case SpvBuiltInSubgroupSize
:
1181 *location
= SYSTEM_VALUE_SUBGROUP_SIZE
;
1182 set_mode_system_value(b
, mode
);
1184 case SpvBuiltInSubgroupId
:
1185 *location
= SYSTEM_VALUE_SUBGROUP_ID
;
1186 set_mode_system_value(b
, mode
);
1188 case SpvBuiltInSubgroupLocalInvocationId
:
1189 *location
= SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1190 set_mode_system_value(b
, mode
);
1192 case SpvBuiltInNumSubgroups
:
1193 *location
= SYSTEM_VALUE_NUM_SUBGROUPS
;
1194 set_mode_system_value(b
, mode
);
1196 case SpvBuiltInDeviceIndex
:
1197 *location
= SYSTEM_VALUE_DEVICE_INDEX
;
1198 set_mode_system_value(b
, mode
);
1200 case SpvBuiltInViewIndex
:
1201 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1202 set_mode_system_value(b
, mode
);
1204 case SpvBuiltInSubgroupEqMask
:
1205 *location
= SYSTEM_VALUE_SUBGROUP_EQ_MASK
,
1206 set_mode_system_value(b
, mode
);
1208 case SpvBuiltInSubgroupGeMask
:
1209 *location
= SYSTEM_VALUE_SUBGROUP_GE_MASK
,
1210 set_mode_system_value(b
, mode
);
1212 case SpvBuiltInSubgroupGtMask
:
1213 *location
= SYSTEM_VALUE_SUBGROUP_GT_MASK
,
1214 set_mode_system_value(b
, mode
);
1216 case SpvBuiltInSubgroupLeMask
:
1217 *location
= SYSTEM_VALUE_SUBGROUP_LE_MASK
,
1218 set_mode_system_value(b
, mode
);
1220 case SpvBuiltInSubgroupLtMask
:
1221 *location
= SYSTEM_VALUE_SUBGROUP_LT_MASK
,
1222 set_mode_system_value(b
, mode
);
1224 case SpvBuiltInFragStencilRefEXT
:
1225 *location
= FRAG_RESULT_STENCIL
;
1226 vtn_assert(*mode
== nir_var_shader_out
);
1228 case SpvBuiltInWorkDim
:
1229 *location
= SYSTEM_VALUE_WORK_DIM
;
1230 set_mode_system_value(b
, mode
);
1232 case SpvBuiltInGlobalSize
:
1233 *location
= SYSTEM_VALUE_GLOBAL_GROUP_SIZE
;
1234 set_mode_system_value(b
, mode
);
1237 vtn_fail("unsupported builtin: %u", builtin
);
1242 apply_var_decoration(struct vtn_builder
*b
,
1243 struct nir_variable_data
*var_data
,
1244 const struct vtn_decoration
*dec
)
1246 switch (dec
->decoration
) {
1247 case SpvDecorationRelaxedPrecision
:
1248 break; /* FIXME: Do nothing with this for now. */
1249 case SpvDecorationNoPerspective
:
1250 var_data
->interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1252 case SpvDecorationFlat
:
1253 var_data
->interpolation
= INTERP_MODE_FLAT
;
1255 case SpvDecorationCentroid
:
1256 var_data
->centroid
= true;
1258 case SpvDecorationSample
:
1259 var_data
->sample
= true;
1261 case SpvDecorationInvariant
:
1262 var_data
->invariant
= true;
1264 case SpvDecorationConstant
:
1265 var_data
->read_only
= true;
1267 case SpvDecorationNonReadable
:
1268 var_data
->image
.access
|= ACCESS_NON_READABLE
;
1270 case SpvDecorationNonWritable
:
1271 var_data
->read_only
= true;
1272 var_data
->image
.access
|= ACCESS_NON_WRITEABLE
;
1274 case SpvDecorationRestrict
:
1275 var_data
->image
.access
|= ACCESS_RESTRICT
;
1277 case SpvDecorationVolatile
:
1278 var_data
->image
.access
|= ACCESS_VOLATILE
;
1280 case SpvDecorationCoherent
:
1281 var_data
->image
.access
|= ACCESS_COHERENT
;
1283 case SpvDecorationComponent
:
1284 var_data
->location_frac
= dec
->literals
[0];
1286 case SpvDecorationIndex
:
1287 var_data
->index
= dec
->literals
[0];
1289 case SpvDecorationBuiltIn
: {
1290 SpvBuiltIn builtin
= dec
->literals
[0];
1292 nir_variable_mode mode
= var_data
->mode
;
1293 vtn_get_builtin_location(b
, builtin
, &var_data
->location
, &mode
);
1294 var_data
->mode
= mode
;
1297 case SpvBuiltInTessLevelOuter
:
1298 case SpvBuiltInTessLevelInner
:
1299 var_data
->compact
= true;
1301 case SpvBuiltInFragCoord
:
1302 var_data
->pixel_center_integer
= b
->pixel_center_integer
;
1304 case SpvBuiltInSamplePosition
:
1305 var_data
->origin_upper_left
= b
->origin_upper_left
;
1312 case SpvDecorationSpecId
:
1313 case SpvDecorationRowMajor
:
1314 case SpvDecorationColMajor
:
1315 case SpvDecorationMatrixStride
:
1316 case SpvDecorationAliased
:
1317 case SpvDecorationUniform
:
1318 case SpvDecorationLinkageAttributes
:
1319 break; /* Do nothing with these here */
1321 case SpvDecorationPatch
:
1322 var_data
->patch
= true;
1325 case SpvDecorationLocation
:
1326 vtn_fail("Handled above");
1328 case SpvDecorationBlock
:
1329 case SpvDecorationBufferBlock
:
1330 case SpvDecorationArrayStride
:
1331 case SpvDecorationGLSLShared
:
1332 case SpvDecorationGLSLPacked
:
1333 break; /* These can apply to a type but we don't care about them */
1335 case SpvDecorationBinding
:
1336 case SpvDecorationDescriptorSet
:
1337 case SpvDecorationNoContraction
:
1338 case SpvDecorationInputAttachmentIndex
:
1339 vtn_warn("Decoration not allowed for variable or structure member: %s",
1340 spirv_decoration_to_string(dec
->decoration
));
1343 case SpvDecorationXfbBuffer
:
1344 var_data
->explicit_xfb_buffer
= true;
1345 var_data
->xfb_buffer
= dec
->literals
[0];
1346 var_data
->always_active_io
= true;
1348 case SpvDecorationXfbStride
:
1349 var_data
->explicit_xfb_stride
= true;
1350 var_data
->xfb_stride
= dec
->literals
[0];
1352 case SpvDecorationOffset
:
1353 var_data
->explicit_offset
= true;
1354 var_data
->offset
= dec
->literals
[0];
1357 case SpvDecorationStream
:
1358 var_data
->stream
= dec
->literals
[0];
1361 case SpvDecorationCPacked
:
1362 case SpvDecorationSaturatedConversion
:
1363 case SpvDecorationFuncParamAttr
:
1364 case SpvDecorationFPRoundingMode
:
1365 case SpvDecorationFPFastMathMode
:
1366 case SpvDecorationAlignment
:
1367 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1368 spirv_decoration_to_string(dec
->decoration
));
1371 case SpvDecorationHlslSemanticGOOGLE
:
1372 /* HLSL semantic decorations can safely be ignored by the driver. */
1376 vtn_fail("Unhandled decoration");
1381 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1382 const struct vtn_decoration
*dec
, void *out_is_patch
)
1384 if (dec
->decoration
== SpvDecorationPatch
) {
1385 *((bool *) out_is_patch
) = true;
1390 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1391 const struct vtn_decoration
*dec
, void *void_var
)
1393 struct vtn_variable
*vtn_var
= void_var
;
1395 /* Handle decorations that apply to a vtn_variable as a whole */
1396 switch (dec
->decoration
) {
1397 case SpvDecorationBinding
:
1398 vtn_var
->binding
= dec
->literals
[0];
1399 vtn_var
->explicit_binding
= true;
1401 case SpvDecorationDescriptorSet
:
1402 vtn_var
->descriptor_set
= dec
->literals
[0];
1404 case SpvDecorationInputAttachmentIndex
:
1405 vtn_var
->input_attachment_index
= dec
->literals
[0];
1407 case SpvDecorationPatch
:
1408 vtn_var
->patch
= true;
1410 case SpvDecorationOffset
:
1411 vtn_var
->offset
= dec
->literals
[0];
1413 case SpvDecorationNonWritable
:
1414 vtn_var
->access
|= ACCESS_NON_WRITEABLE
;
1416 case SpvDecorationNonReadable
:
1417 vtn_var
->access
|= ACCESS_NON_READABLE
;
1419 case SpvDecorationVolatile
:
1420 vtn_var
->access
|= ACCESS_VOLATILE
;
1422 case SpvDecorationCoherent
:
1423 vtn_var
->access
|= ACCESS_COHERENT
;
1425 case SpvDecorationHlslCounterBufferGOOGLE
:
1426 /* HLSL semantic decorations can safely be ignored by the driver. */
1432 if (val
->value_type
== vtn_value_type_pointer
) {
1433 assert(val
->pointer
->var
== void_var
);
1434 assert(member
== -1);
1436 assert(val
->value_type
== vtn_value_type_type
);
1439 /* Location is odd. If applied to a split structure, we have to walk the
1440 * whole thing and accumulate the location. It's easier to handle as a
1443 if (dec
->decoration
== SpvDecorationLocation
) {
1444 unsigned location
= dec
->literals
[0];
1445 bool is_vertex_input
= false;
1446 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
&&
1447 vtn_var
->mode
== vtn_variable_mode_output
) {
1448 location
+= FRAG_RESULT_DATA0
;
1449 } else if (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
&&
1450 vtn_var
->mode
== vtn_variable_mode_input
) {
1451 is_vertex_input
= true;
1452 location
+= VERT_ATTRIB_GENERIC0
;
1453 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1454 vtn_var
->mode
== vtn_variable_mode_output
) {
1455 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1456 } else if (vtn_var
->mode
!= vtn_variable_mode_uniform
) {
1457 vtn_warn("Location must be on input, output, uniform, sampler or "
1462 if (vtn_var
->var
->num_members
== 0) {
1463 /* This handles the member and lone variable cases */
1464 vtn_var
->var
->data
.location
= location
;
1466 /* This handles the structure member case */
1467 assert(vtn_var
->var
->members
);
1468 for (unsigned i
= 0; i
< vtn_var
->var
->num_members
; i
++) {
1469 vtn_var
->var
->members
[i
].location
= location
;
1470 const struct glsl_type
*member_type
=
1471 glsl_get_struct_field(vtn_var
->var
->interface_type
, i
);
1472 location
+= glsl_count_attribute_slots(member_type
,
1479 if (vtn_var
->var
->num_members
== 0) {
1480 assert(member
== -1);
1481 apply_var_decoration(b
, &vtn_var
->var
->data
, dec
);
1482 } else if (member
>= 0) {
1483 /* Member decorations must come from a type */
1484 assert(val
->value_type
== vtn_value_type_type
);
1485 apply_var_decoration(b
, &vtn_var
->var
->members
[member
], dec
);
1488 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1489 for (unsigned i
= 0; i
< length
; i
++)
1490 apply_var_decoration(b
, &vtn_var
->var
->members
[i
], dec
);
1493 /* A few variables, those with external storage, have no actual
1494 * nir_variables associated with them. Fortunately, all decorations
1495 * we care about for those variables are on the type only.
1497 vtn_assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1498 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1499 vtn_var
->mode
== vtn_variable_mode_push_constant
||
1500 (vtn_var
->mode
== vtn_variable_mode_workgroup
&&
1501 b
->options
->lower_workgroup_access_to_offsets
));
1506 static enum vtn_variable_mode
1507 vtn_storage_class_to_mode(struct vtn_builder
*b
,
1508 SpvStorageClass
class,
1509 struct vtn_type
*interface_type
,
1510 nir_variable_mode
*nir_mode_out
)
1512 enum vtn_variable_mode mode
;
1513 nir_variable_mode nir_mode
;
1515 case SpvStorageClassUniform
:
1516 if (interface_type
->block
) {
1517 mode
= vtn_variable_mode_ubo
;
1518 nir_mode
= nir_var_ubo
;
1519 } else if (interface_type
->buffer_block
) {
1520 mode
= vtn_variable_mode_ssbo
;
1521 nir_mode
= nir_var_ssbo
;
1523 /* Default-block uniforms, coming from gl_spirv */
1524 mode
= vtn_variable_mode_uniform
;
1525 nir_mode
= nir_var_uniform
;
1528 case SpvStorageClassStorageBuffer
:
1529 mode
= vtn_variable_mode_ssbo
;
1530 nir_mode
= nir_var_ssbo
;
1532 case SpvStorageClassUniformConstant
:
1533 mode
= vtn_variable_mode_uniform
;
1534 nir_mode
= nir_var_uniform
;
1536 case SpvStorageClassPushConstant
:
1537 mode
= vtn_variable_mode_push_constant
;
1538 nir_mode
= nir_var_uniform
;
1540 case SpvStorageClassInput
:
1541 mode
= vtn_variable_mode_input
;
1542 nir_mode
= nir_var_shader_in
;
1544 case SpvStorageClassOutput
:
1545 mode
= vtn_variable_mode_output
;
1546 nir_mode
= nir_var_shader_out
;
1548 case SpvStorageClassPrivate
:
1549 mode
= vtn_variable_mode_global
;
1550 nir_mode
= nir_var_global
;
1552 case SpvStorageClassFunction
:
1553 mode
= vtn_variable_mode_local
;
1554 nir_mode
= nir_var_local
;
1556 case SpvStorageClassWorkgroup
:
1557 mode
= vtn_variable_mode_workgroup
;
1558 nir_mode
= nir_var_shared
;
1560 case SpvStorageClassAtomicCounter
:
1561 mode
= vtn_variable_mode_uniform
;
1562 nir_mode
= nir_var_uniform
;
1564 case SpvStorageClassCrossWorkgroup
:
1565 case SpvStorageClassGeneric
:
1567 vtn_fail("Unhandled variable storage class");
1571 *nir_mode_out
= nir_mode
;
1577 vtn_pointer_to_ssa(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
1579 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
1580 /* This pointer needs to have a pointer type with actual storage */
1581 vtn_assert(ptr
->ptr_type
);
1582 vtn_assert(ptr
->ptr_type
->type
);
1585 /* If we don't have an offset then we must be a pointer to the variable
1588 vtn_assert(!ptr
->offset
&& !ptr
->block_index
);
1590 struct vtn_access_chain chain
= {
1593 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
1596 vtn_assert(ptr
->offset
);
1597 if (ptr
->block_index
) {
1598 vtn_assert(ptr
->mode
== vtn_variable_mode_ubo
||
1599 ptr
->mode
== vtn_variable_mode_ssbo
);
1600 return nir_vec2(&b
->nb
, ptr
->block_index
, ptr
->offset
);
1602 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
);
1606 return &vtn_pointer_to_deref(b
, ptr
)->dest
.ssa
;
1610 struct vtn_pointer
*
1611 vtn_pointer_from_ssa(struct vtn_builder
*b
, nir_ssa_def
*ssa
,
1612 struct vtn_type
*ptr_type
)
1614 vtn_assert(ssa
->num_components
<= 2 && ssa
->bit_size
== 32);
1615 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1617 struct vtn_type
*interface_type
= ptr_type
->deref
;
1618 while (interface_type
->base_type
== vtn_base_type_array
)
1619 interface_type
= interface_type
->array_element
;
1621 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
1622 nir_variable_mode nir_mode
;
1623 ptr
->mode
= vtn_storage_class_to_mode(b
, ptr_type
->storage_class
,
1624 interface_type
, &nir_mode
);
1625 ptr
->type
= ptr_type
->deref
;
1626 ptr
->ptr_type
= ptr_type
;
1628 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
1629 /* This pointer type needs to have actual storage */
1630 vtn_assert(ptr_type
->type
);
1631 if (ptr
->mode
== vtn_variable_mode_ubo
||
1632 ptr
->mode
== vtn_variable_mode_ubo
) {
1633 vtn_assert(ssa
->num_components
== 2);
1634 ptr
->block_index
= nir_channel(&b
->nb
, ssa
, 0);
1635 ptr
->offset
= nir_channel(&b
->nb
, ssa
, 1);
1637 vtn_assert(ssa
->num_components
== 1);
1638 ptr
->block_index
= NULL
;
1642 assert(!vtn_pointer_is_external_block(b
, ptr
));
1643 const struct glsl_type
*deref_type
= ptr_type
->deref
->type
;
1644 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
1645 glsl_get_bare_type(deref_type
), 0);
1652 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
1654 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
1657 if (var
->mode
== vtn_variable_mode_input
) {
1658 return stage
== MESA_SHADER_TESS_CTRL
||
1659 stage
== MESA_SHADER_TESS_EVAL
||
1660 stage
== MESA_SHADER_GEOMETRY
;
1663 if (var
->mode
== vtn_variable_mode_output
)
1664 return stage
== MESA_SHADER_TESS_CTRL
;
1670 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
1671 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
1672 nir_constant
*initializer
)
1674 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1675 struct vtn_type
*type
= ptr_type
->deref
;
1677 struct vtn_type
*without_array
= type
;
1678 while(glsl_type_is_array(without_array
->type
))
1679 without_array
= without_array
->array_element
;
1681 enum vtn_variable_mode mode
;
1682 nir_variable_mode nir_mode
;
1683 mode
= vtn_storage_class_to_mode(b
, storage_class
, without_array
, &nir_mode
);
1686 case vtn_variable_mode_ubo
:
1687 /* There's no other way to get vtn_variable_mode_ubo */
1688 vtn_assert(without_array
->block
);
1689 b
->shader
->info
.num_ubos
++;
1691 case vtn_variable_mode_ssbo
:
1692 if (storage_class
== SpvStorageClassStorageBuffer
&&
1693 !without_array
->block
) {
1694 if (b
->variable_pointers
) {
1695 vtn_fail("Variables in the StorageBuffer storage class must "
1696 "have a struct type with the Block decoration");
1698 /* If variable pointers are not present, it's still malformed
1699 * SPIR-V but we can parse it and do the right thing anyway.
1700 * Since some of the 8-bit storage tests have bugs in this are,
1701 * just make it a warning for now.
1703 vtn_warn("Variables in the StorageBuffer storage class must "
1704 "have a struct type with the Block decoration");
1707 b
->shader
->info
.num_ssbos
++;
1709 case vtn_variable_mode_uniform
:
1710 if (glsl_type_is_image(without_array
->type
))
1711 b
->shader
->info
.num_images
++;
1712 else if (glsl_type_is_sampler(without_array
->type
))
1713 b
->shader
->info
.num_textures
++;
1715 case vtn_variable_mode_push_constant
:
1716 b
->shader
->num_uniforms
= vtn_type_block_size(b
, type
);
1719 /* No tallying is needed */
1723 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
1727 vtn_assert(val
->value_type
== vtn_value_type_pointer
);
1728 val
->pointer
= vtn_pointer_for_variable(b
, var
, ptr_type
);
1730 switch (var
->mode
) {
1731 case vtn_variable_mode_local
:
1732 case vtn_variable_mode_global
:
1733 case vtn_variable_mode_uniform
:
1734 /* For these, we create the variable normally */
1735 var
->var
= rzalloc(b
->shader
, nir_variable
);
1736 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1738 if (storage_class
== SpvStorageClassAtomicCounter
) {
1739 /* Need to tweak the nir type here as at vtn_handle_type we don't
1740 * have the access to storage_class, that is the one that points us
1741 * that is an atomic uint.
1743 var
->var
->type
= repair_atomic_type(var
->type
->type
);
1745 /* Private variables don't have any explicit layout but some layouts
1746 * may have leaked through due to type deduplication in the SPIR-V.
1748 var
->var
->type
= glsl_get_bare_type(var
->type
->type
);
1750 var
->var
->data
.mode
= nir_mode
;
1751 var
->var
->data
.location
= -1;
1752 var
->var
->interface_type
= NULL
;
1755 case vtn_variable_mode_workgroup
:
1756 if (b
->options
->lower_workgroup_access_to_offsets
) {
1757 var
->shared_location
= -1;
1759 /* Create the variable normally */
1760 var
->var
= rzalloc(b
->shader
, nir_variable
);
1761 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1762 /* Workgroup variables don't have any explicit layout but some
1763 * layouts may have leaked through due to type deduplication in the
1766 var
->var
->type
= glsl_get_bare_type(var
->type
->type
);
1767 var
->var
->data
.mode
= nir_var_shared
;
1771 case vtn_variable_mode_input
:
1772 case vtn_variable_mode_output
: {
1773 /* In order to know whether or not we're a per-vertex inout, we need
1774 * the patch qualifier. This means walking the variable decorations
1775 * early before we actually create any variables. Not a big deal.
1777 * GLSLang really likes to place decorations in the most interior
1778 * thing it possibly can. In particular, if you have a struct, it
1779 * will place the patch decorations on the struct members. This
1780 * should be handled by the variable splitting below just fine.
1782 * If you have an array-of-struct, things get even more weird as it
1783 * will place the patch decorations on the struct even though it's
1784 * inside an array and some of the members being patch and others not
1785 * makes no sense whatsoever. Since the only sensible thing is for
1786 * it to be all or nothing, we'll call it patch if any of the members
1787 * are declared patch.
1790 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
1791 if (glsl_type_is_array(var
->type
->type
) &&
1792 glsl_type_is_struct(without_array
->type
)) {
1793 vtn_foreach_decoration(b
, vtn_value(b
, without_array
->id
,
1794 vtn_value_type_type
),
1795 var_is_patch_cb
, &var
->patch
);
1798 /* For inputs and outputs, we immediately split structures. This
1799 * is for a couple of reasons. For one, builtins may all come in
1800 * a struct and we really want those split out into separate
1801 * variables. For another, interpolation qualifiers can be
1802 * applied to members of the top-level struct ane we need to be
1803 * able to preserve that information.
1806 struct vtn_type
*interface_type
= var
->type
;
1807 if (is_per_vertex_inout(var
, b
->shader
->info
.stage
)) {
1808 /* In Geometry shaders (and some tessellation), inputs come
1809 * in per-vertex arrays. However, some builtins come in
1810 * non-per-vertex, hence the need for the is_array check. In
1811 * any case, there are no non-builtin arrays allowed so this
1812 * check should be sufficient.
1814 interface_type
= var
->type
->array_element
;
1817 var
->var
= rzalloc(b
->shader
, nir_variable
);
1818 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1819 /* In Vulkan, shader I/O variables don't have any explicit layout but
1820 * some layouts may have leaked through due to type deduplication in
1823 var
->var
->type
= glsl_get_bare_type(var
->type
->type
);
1824 var
->var
->interface_type
= interface_type
->type
;
1825 var
->var
->data
.mode
= nir_mode
;
1826 var
->var
->data
.patch
= var
->patch
;
1828 if (glsl_type_is_struct(interface_type
->type
)) {
1829 /* It's a struct. Set it up as per-member. */
1830 var
->var
->num_members
= glsl_get_length(interface_type
->type
);
1831 var
->var
->members
= rzalloc_array(var
->var
, struct nir_variable_data
,
1832 var
->var
->num_members
);
1834 for (unsigned i
= 0; i
< var
->var
->num_members
; i
++) {
1835 var
->var
->members
[i
].mode
= nir_mode
;
1836 var
->var
->members
[i
].patch
= var
->patch
;
1840 /* For inputs and outputs, we need to grab locations and builtin
1841 * information from the interface type.
1843 vtn_foreach_decoration(b
, vtn_value(b
, interface_type
->id
,
1844 vtn_value_type_type
),
1845 var_decoration_cb
, var
);
1849 case vtn_variable_mode_ubo
:
1850 case vtn_variable_mode_ssbo
:
1851 case vtn_variable_mode_push_constant
:
1852 /* These don't need actual variables. */
1857 var
->var
->constant_initializer
=
1858 nir_constant_clone(initializer
, var
->var
);
1861 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
1863 if (var
->mode
== vtn_variable_mode_uniform
) {
1864 /* XXX: We still need the binding information in the nir_variable
1865 * for these. We should fix that.
1867 var
->var
->data
.binding
= var
->binding
;
1868 var
->var
->data
.explicit_binding
= var
->explicit_binding
;
1869 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
1870 var
->var
->data
.index
= var
->input_attachment_index
;
1871 var
->var
->data
.offset
= var
->offset
;
1873 if (glsl_type_is_image(without_array
->type
))
1874 var
->var
->data
.image
.format
= without_array
->image_format
;
1877 if (var
->mode
== vtn_variable_mode_local
) {
1878 vtn_assert(var
->var
!= NULL
&& var
->var
->members
== NULL
);
1879 nir_function_impl_add_variable(b
->nb
.impl
, var
->var
);
1880 } else if (var
->var
) {
1881 nir_shader_add_variable(b
->shader
, var
->var
);
1883 vtn_assert(vtn_pointer_is_external_block(b
, val
->pointer
));
1888 vtn_assert_types_equal(struct vtn_builder
*b
, SpvOp opcode
,
1889 struct vtn_type
*dst_type
,
1890 struct vtn_type
*src_type
)
1892 if (dst_type
->id
== src_type
->id
)
1895 if (vtn_types_compatible(b
, dst_type
, src_type
)) {
1896 /* Early versions of GLSLang would re-emit types unnecessarily and you
1897 * would end up with OpLoad, OpStore, or OpCopyMemory opcodes which have
1898 * mismatched source and destination types.
1900 * https://github.com/KhronosGroup/glslang/issues/304
1901 * https://github.com/KhronosGroup/glslang/issues/307
1902 * https://bugs.freedesktop.org/show_bug.cgi?id=104338
1903 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
1905 vtn_warn("Source and destination types of %s do not have the same "
1906 "ID (but are compatible): %u vs %u",
1907 spirv_op_to_string(opcode
), dst_type
->id
, src_type
->id
);
1911 vtn_fail("Source and destination types of %s do not match: %s vs. %s",
1912 spirv_op_to_string(opcode
),
1913 glsl_get_type_name(dst_type
->type
),
1914 glsl_get_type_name(src_type
->type
));
1918 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
1919 const uint32_t *w
, unsigned count
)
1923 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1924 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1928 case SpvOpVariable
: {
1929 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1931 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1933 SpvStorageClass storage_class
= w
[3];
1934 nir_constant
*initializer
= NULL
;
1936 initializer
= vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
1938 vtn_create_variable(b
, val
, ptr_type
, storage_class
, initializer
);
1942 case SpvOpAccessChain
:
1943 case SpvOpPtrAccessChain
:
1944 case SpvOpInBoundsAccessChain
: {
1945 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
1946 chain
->ptr_as_array
= (opcode
== SpvOpPtrAccessChain
);
1949 for (int i
= 4; i
< count
; i
++) {
1950 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
1951 if (link_val
->value_type
== vtn_value_type_constant
) {
1952 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
1953 switch (glsl_get_bit_size(link_val
->type
->type
)) {
1955 chain
->link
[idx
].id
= link_val
->constant
->values
[0].i8
[0];
1958 chain
->link
[idx
].id
= link_val
->constant
->values
[0].i16
[0];
1961 chain
->link
[idx
].id
= link_val
->constant
->values
[0].i32
[0];
1964 chain
->link
[idx
].id
= link_val
->constant
->values
[0].i64
[0];
1967 vtn_fail("Invalid bit size");
1970 chain
->link
[idx
].mode
= vtn_access_mode_id
;
1971 chain
->link
[idx
].id
= w
[i
];
1977 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1978 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
1979 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1980 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1981 * to combine an array of images with a single sampler to get an
1982 * array of sampled images that all share the same sampler.
1983 * Fortunately, this means that we can more-or-less ignore the
1984 * sampler when crawling the access chain, but it does leave us
1985 * with this rather awkward little special-case.
1987 struct vtn_value
*val
=
1988 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1989 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1990 val
->sampled_image
->type
= base_val
->sampled_image
->type
;
1991 val
->sampled_image
->image
=
1992 vtn_pointer_dereference(b
, base_val
->sampled_image
->image
, chain
);
1993 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
1995 vtn_assert(base_val
->value_type
== vtn_value_type_pointer
);
1996 struct vtn_value
*val
=
1997 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1998 val
->pointer
= vtn_pointer_dereference(b
, base_val
->pointer
, chain
);
1999 val
->pointer
->ptr_type
= ptr_type
;
2004 case SpvOpCopyMemory
: {
2005 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2006 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
2008 vtn_assert_types_equal(b
, opcode
, dest
->type
->deref
, src
->type
->deref
);
2010 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
2015 struct vtn_type
*res_type
=
2016 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2017 struct vtn_value
*src_val
= vtn_value(b
, w
[3], vtn_value_type_pointer
);
2018 struct vtn_pointer
*src
= src_val
->pointer
;
2020 vtn_assert_types_equal(b
, opcode
, res_type
, src_val
->type
->deref
);
2022 if (glsl_type_is_image(res_type
->type
) ||
2023 glsl_type_is_sampler(res_type
->type
)) {
2024 vtn_push_value(b
, w
[2], vtn_value_type_pointer
)->pointer
= src
;
2028 vtn_push_ssa(b
, w
[2], res_type
, vtn_variable_load(b
, src
));
2033 struct vtn_value
*dest_val
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2034 struct vtn_pointer
*dest
= dest_val
->pointer
;
2035 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[2]);
2037 /* OpStore requires us to actually have a storage type */
2038 vtn_fail_if(dest
->type
->type
== NULL
,
2039 "Invalid destination type for OpStore");
2041 if (glsl_get_base_type(dest
->type
->type
) == GLSL_TYPE_BOOL
&&
2042 glsl_get_base_type(src_val
->type
->type
) == GLSL_TYPE_UINT
) {
2043 /* Early versions of GLSLang would use uint types for UBOs/SSBOs but
2044 * would then store them to a local variable as bool. Work around
2045 * the issue by doing an implicit conversion.
2047 * https://github.com/KhronosGroup/glslang/issues/170
2048 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2050 vtn_warn("OpStore of value of type OpTypeInt to a pointer to type "
2051 "OpTypeBool. Doing an implicit conversion to work around "
2053 struct vtn_ssa_value
*bool_ssa
=
2054 vtn_create_ssa_value(b
, dest
->type
->type
);
2055 bool_ssa
->def
= nir_i2b(&b
->nb
, vtn_ssa_value(b
, w
[2])->def
);
2056 vtn_variable_store(b
, bool_ssa
, dest
);
2060 vtn_assert_types_equal(b
, opcode
, dest_val
->type
->deref
, src_val
->type
);
2062 if (glsl_type_is_sampler(dest
->type
->type
)) {
2063 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
2064 "propagation to workaround the problem.");
2065 vtn_assert(dest
->var
->copy_prop_sampler
== NULL
);
2066 dest
->var
->copy_prop_sampler
=
2067 vtn_value(b
, w
[2], vtn_value_type_pointer
)->pointer
;
2071 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
2072 vtn_variable_store(b
, src
, dest
);
2076 case SpvOpArrayLength
: {
2077 struct vtn_pointer
*ptr
=
2078 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2080 const uint32_t offset
= ptr
->var
->type
->offsets
[w
[4]];
2081 const uint32_t stride
= ptr
->var
->type
->members
[w
[4]]->stride
;
2083 if (!ptr
->block_index
) {
2084 struct vtn_access_chain chain
= {
2087 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
2088 vtn_assert(ptr
->block_index
);
2091 nir_intrinsic_instr
*instr
=
2092 nir_intrinsic_instr_create(b
->nb
.shader
,
2093 nir_intrinsic_get_buffer_size
);
2094 instr
->src
[0] = nir_src_for_ssa(ptr
->block_index
);
2095 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
2096 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2097 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
2099 /* array_length = max(buffer_size - offset, 0) / stride */
2100 nir_ssa_def
*array_length
=
2105 nir_imm_int(&b
->nb
, offset
)),
2106 nir_imm_int(&b
->nb
, 0u)),
2107 nir_imm_int(&b
->nb
, stride
));
2109 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2110 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
2111 val
->ssa
->def
= array_length
;
2115 case SpvOpCopyMemorySized
:
2117 vtn_fail("Unhandled opcode");