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_is_external_block(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_is_external_block(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 (ptr
->mode
== vtn_variable_mode_ubo
||
1629 ptr
->mode
== vtn_variable_mode_ssbo
) {
1630 /* This pointer type needs to have actual storage */
1631 vtn_assert(ptr_type
->type
);
1632 vtn_assert(ssa
->num_components
== 2);
1633 ptr
->block_index
= nir_channel(&b
->nb
, ssa
, 0);
1634 ptr
->offset
= nir_channel(&b
->nb
, ssa
, 1);
1635 } else if ((ptr
->mode
== vtn_variable_mode_workgroup
&&
1636 b
->options
->lower_workgroup_access_to_offsets
) ||
1637 ptr
->mode
== vtn_variable_mode_push_constant
) {
1638 /* This pointer type needs to have actual storage */
1639 vtn_assert(ptr_type
->type
);
1640 vtn_assert(ssa
->num_components
== 1);
1641 ptr
->block_index
= NULL
;
1644 assert(!vtn_pointer_is_external_block(b
, ptr
));
1645 const struct glsl_type
*deref_type
= ptr_type
->deref
->type
;
1646 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
1647 glsl_get_bare_type(deref_type
), 0);
1654 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
1656 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
1659 if (var
->mode
== vtn_variable_mode_input
) {
1660 return stage
== MESA_SHADER_TESS_CTRL
||
1661 stage
== MESA_SHADER_TESS_EVAL
||
1662 stage
== MESA_SHADER_GEOMETRY
;
1665 if (var
->mode
== vtn_variable_mode_output
)
1666 return stage
== MESA_SHADER_TESS_CTRL
;
1672 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
1673 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
1674 nir_constant
*initializer
)
1676 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1677 struct vtn_type
*type
= ptr_type
->deref
;
1679 struct vtn_type
*without_array
= type
;
1680 while(glsl_type_is_array(without_array
->type
))
1681 without_array
= without_array
->array_element
;
1683 enum vtn_variable_mode mode
;
1684 nir_variable_mode nir_mode
;
1685 mode
= vtn_storage_class_to_mode(b
, storage_class
, without_array
, &nir_mode
);
1688 case vtn_variable_mode_ubo
:
1689 b
->shader
->info
.num_ubos
++;
1691 case vtn_variable_mode_ssbo
:
1692 b
->shader
->info
.num_ssbos
++;
1694 case vtn_variable_mode_uniform
:
1695 if (glsl_type_is_image(without_array
->type
))
1696 b
->shader
->info
.num_images
++;
1697 else if (glsl_type_is_sampler(without_array
->type
))
1698 b
->shader
->info
.num_textures
++;
1700 case vtn_variable_mode_push_constant
:
1701 b
->shader
->num_uniforms
= vtn_type_block_size(b
, type
);
1704 /* No tallying is needed */
1708 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
1712 vtn_assert(val
->value_type
== vtn_value_type_pointer
);
1713 val
->pointer
= vtn_pointer_for_variable(b
, var
, ptr_type
);
1715 switch (var
->mode
) {
1716 case vtn_variable_mode_local
:
1717 case vtn_variable_mode_global
:
1718 case vtn_variable_mode_uniform
:
1719 /* For these, we create the variable normally */
1720 var
->var
= rzalloc(b
->shader
, nir_variable
);
1721 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1723 if (storage_class
== SpvStorageClassAtomicCounter
) {
1724 /* Need to tweak the nir type here as at vtn_handle_type we don't
1725 * have the access to storage_class, that is the one that points us
1726 * that is an atomic uint.
1728 var
->var
->type
= repair_atomic_type(var
->type
->type
);
1730 /* Private variables don't have any explicit layout but some layouts
1731 * may have leaked through due to type deduplication in the SPIR-V.
1733 var
->var
->type
= glsl_get_bare_type(var
->type
->type
);
1735 var
->var
->data
.mode
= nir_mode
;
1736 var
->var
->data
.location
= -1;
1737 var
->var
->interface_type
= NULL
;
1740 case vtn_variable_mode_workgroup
:
1741 if (b
->options
->lower_workgroup_access_to_offsets
) {
1742 var
->shared_location
= -1;
1744 /* Create the variable normally */
1745 var
->var
= rzalloc(b
->shader
, nir_variable
);
1746 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1747 /* Workgroup variables don't have any explicit layout but some
1748 * layouts may have leaked through due to type deduplication in the
1751 var
->var
->type
= glsl_get_bare_type(var
->type
->type
);
1752 var
->var
->data
.mode
= nir_var_shared
;
1756 case vtn_variable_mode_input
:
1757 case vtn_variable_mode_output
: {
1758 /* In order to know whether or not we're a per-vertex inout, we need
1759 * the patch qualifier. This means walking the variable decorations
1760 * early before we actually create any variables. Not a big deal.
1762 * GLSLang really likes to place decorations in the most interior
1763 * thing it possibly can. In particular, if you have a struct, it
1764 * will place the patch decorations on the struct members. This
1765 * should be handled by the variable splitting below just fine.
1767 * If you have an array-of-struct, things get even more weird as it
1768 * will place the patch decorations on the struct even though it's
1769 * inside an array and some of the members being patch and others not
1770 * makes no sense whatsoever. Since the only sensible thing is for
1771 * it to be all or nothing, we'll call it patch if any of the members
1772 * are declared patch.
1775 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
1776 if (glsl_type_is_array(var
->type
->type
) &&
1777 glsl_type_is_struct(without_array
->type
)) {
1778 vtn_foreach_decoration(b
, vtn_value(b
, without_array
->id
,
1779 vtn_value_type_type
),
1780 var_is_patch_cb
, &var
->patch
);
1783 /* For inputs and outputs, we immediately split structures. This
1784 * is for a couple of reasons. For one, builtins may all come in
1785 * a struct and we really want those split out into separate
1786 * variables. For another, interpolation qualifiers can be
1787 * applied to members of the top-level struct ane we need to be
1788 * able to preserve that information.
1791 struct vtn_type
*interface_type
= var
->type
;
1792 if (is_per_vertex_inout(var
, b
->shader
->info
.stage
)) {
1793 /* In Geometry shaders (and some tessellation), inputs come
1794 * in per-vertex arrays. However, some builtins come in
1795 * non-per-vertex, hence the need for the is_array check. In
1796 * any case, there are no non-builtin arrays allowed so this
1797 * check should be sufficient.
1799 interface_type
= var
->type
->array_element
;
1802 var
->var
= rzalloc(b
->shader
, nir_variable
);
1803 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1804 /* In Vulkan, shader I/O variables don't have any explicit layout but
1805 * some layouts may have leaked through due to type deduplication in
1808 var
->var
->type
= glsl_get_bare_type(var
->type
->type
);
1809 var
->var
->interface_type
= interface_type
->type
;
1810 var
->var
->data
.mode
= nir_mode
;
1811 var
->var
->data
.patch
= var
->patch
;
1813 if (glsl_type_is_struct(interface_type
->type
)) {
1814 /* It's a struct. Set it up as per-member. */
1815 var
->var
->num_members
= glsl_get_length(interface_type
->type
);
1816 var
->var
->members
= rzalloc_array(var
->var
, struct nir_variable_data
,
1817 var
->var
->num_members
);
1819 for (unsigned i
= 0; i
< var
->var
->num_members
; i
++) {
1820 var
->var
->members
[i
].mode
= nir_mode
;
1821 var
->var
->members
[i
].patch
= var
->patch
;
1825 /* For inputs and outputs, we need to grab locations and builtin
1826 * information from the interface type.
1828 vtn_foreach_decoration(b
, vtn_value(b
, interface_type
->id
,
1829 vtn_value_type_type
),
1830 var_decoration_cb
, var
);
1834 case vtn_variable_mode_ubo
:
1835 case vtn_variable_mode_ssbo
:
1836 case vtn_variable_mode_push_constant
:
1837 /* These don't need actual variables. */
1842 var
->var
->constant_initializer
=
1843 nir_constant_clone(initializer
, var
->var
);
1846 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
1848 if (var
->mode
== vtn_variable_mode_uniform
) {
1849 /* XXX: We still need the binding information in the nir_variable
1850 * for these. We should fix that.
1852 var
->var
->data
.binding
= var
->binding
;
1853 var
->var
->data
.explicit_binding
= var
->explicit_binding
;
1854 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
1855 var
->var
->data
.index
= var
->input_attachment_index
;
1856 var
->var
->data
.offset
= var
->offset
;
1858 if (glsl_type_is_image(without_array
->type
))
1859 var
->var
->data
.image
.format
= without_array
->image_format
;
1862 if (var
->mode
== vtn_variable_mode_local
) {
1863 vtn_assert(var
->var
!= NULL
&& var
->var
->members
== NULL
);
1864 nir_function_impl_add_variable(b
->nb
.impl
, var
->var
);
1865 } else if (var
->var
) {
1866 nir_shader_add_variable(b
->shader
, var
->var
);
1868 vtn_assert(vtn_pointer_is_external_block(b
, val
->pointer
));
1873 vtn_assert_types_equal(struct vtn_builder
*b
, SpvOp opcode
,
1874 struct vtn_type
*dst_type
,
1875 struct vtn_type
*src_type
)
1877 if (dst_type
->id
== src_type
->id
)
1880 if (vtn_types_compatible(b
, dst_type
, src_type
)) {
1881 /* Early versions of GLSLang would re-emit types unnecessarily and you
1882 * would end up with OpLoad, OpStore, or OpCopyMemory opcodes which have
1883 * mismatched source and destination types.
1885 * https://github.com/KhronosGroup/glslang/issues/304
1886 * https://github.com/KhronosGroup/glslang/issues/307
1887 * https://bugs.freedesktop.org/show_bug.cgi?id=104338
1888 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
1890 vtn_warn("Source and destination types of %s do not have the same "
1891 "ID (but are compatible): %u vs %u",
1892 spirv_op_to_string(opcode
), dst_type
->id
, src_type
->id
);
1896 vtn_fail("Source and destination types of %s do not match: %s vs. %s",
1897 spirv_op_to_string(opcode
),
1898 glsl_get_type_name(dst_type
->type
),
1899 glsl_get_type_name(src_type
->type
));
1903 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
1904 const uint32_t *w
, unsigned count
)
1908 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1909 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1913 case SpvOpVariable
: {
1914 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1916 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1918 SpvStorageClass storage_class
= w
[3];
1919 nir_constant
*initializer
= NULL
;
1921 initializer
= vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
1923 vtn_create_variable(b
, val
, ptr_type
, storage_class
, initializer
);
1927 case SpvOpAccessChain
:
1928 case SpvOpPtrAccessChain
:
1929 case SpvOpInBoundsAccessChain
: {
1930 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
1931 chain
->ptr_as_array
= (opcode
== SpvOpPtrAccessChain
);
1934 for (int i
= 4; i
< count
; i
++) {
1935 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
1936 if (link_val
->value_type
== vtn_value_type_constant
) {
1937 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
1938 switch (glsl_get_bit_size(link_val
->type
->type
)) {
1940 chain
->link
[idx
].id
= link_val
->constant
->values
[0].i8
[0];
1943 chain
->link
[idx
].id
= link_val
->constant
->values
[0].i16
[0];
1946 chain
->link
[idx
].id
= link_val
->constant
->values
[0].i32
[0];
1949 chain
->link
[idx
].id
= link_val
->constant
->values
[0].i64
[0];
1952 vtn_fail("Invalid bit size");
1955 chain
->link
[idx
].mode
= vtn_access_mode_id
;
1956 chain
->link
[idx
].id
= w
[i
];
1962 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1963 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
1964 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1965 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1966 * to combine an array of images with a single sampler to get an
1967 * array of sampled images that all share the same sampler.
1968 * Fortunately, this means that we can more-or-less ignore the
1969 * sampler when crawling the access chain, but it does leave us
1970 * with this rather awkward little special-case.
1972 struct vtn_value
*val
=
1973 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1974 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1975 val
->sampled_image
->type
= base_val
->sampled_image
->type
;
1976 val
->sampled_image
->image
=
1977 vtn_pointer_dereference(b
, base_val
->sampled_image
->image
, chain
);
1978 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
1980 vtn_assert(base_val
->value_type
== vtn_value_type_pointer
);
1981 struct vtn_value
*val
=
1982 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1983 val
->pointer
= vtn_pointer_dereference(b
, base_val
->pointer
, chain
);
1984 val
->pointer
->ptr_type
= ptr_type
;
1989 case SpvOpCopyMemory
: {
1990 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
1991 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
1993 vtn_assert_types_equal(b
, opcode
, dest
->type
->deref
, src
->type
->deref
);
1995 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
2000 struct vtn_type
*res_type
=
2001 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2002 struct vtn_value
*src_val
= vtn_value(b
, w
[3], vtn_value_type_pointer
);
2003 struct vtn_pointer
*src
= src_val
->pointer
;
2005 vtn_assert_types_equal(b
, opcode
, res_type
, src_val
->type
->deref
);
2007 if (glsl_type_is_image(res_type
->type
) ||
2008 glsl_type_is_sampler(res_type
->type
)) {
2009 vtn_push_value(b
, w
[2], vtn_value_type_pointer
)->pointer
= src
;
2013 vtn_push_ssa(b
, w
[2], res_type
, vtn_variable_load(b
, src
));
2018 struct vtn_value
*dest_val
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2019 struct vtn_pointer
*dest
= dest_val
->pointer
;
2020 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[2]);
2022 /* OpStore requires us to actually have a storage type */
2023 vtn_fail_if(dest
->type
->type
== NULL
,
2024 "Invalid destination type for OpStore");
2026 if (glsl_get_base_type(dest
->type
->type
) == GLSL_TYPE_BOOL
&&
2027 glsl_get_base_type(src_val
->type
->type
) == GLSL_TYPE_UINT
) {
2028 /* Early versions of GLSLang would use uint types for UBOs/SSBOs but
2029 * would then store them to a local variable as bool. Work around
2030 * the issue by doing an implicit conversion.
2032 * https://github.com/KhronosGroup/glslang/issues/170
2033 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2035 vtn_warn("OpStore of value of type OpTypeInt to a pointer to type "
2036 "OpTypeBool. Doing an implicit conversion to work around "
2038 struct vtn_ssa_value
*bool_ssa
=
2039 vtn_create_ssa_value(b
, dest
->type
->type
);
2040 bool_ssa
->def
= nir_i2b(&b
->nb
, vtn_ssa_value(b
, w
[2])->def
);
2041 vtn_variable_store(b
, bool_ssa
, dest
);
2045 vtn_assert_types_equal(b
, opcode
, dest_val
->type
->deref
, src_val
->type
);
2047 if (glsl_type_is_sampler(dest
->type
->type
)) {
2048 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
2049 "propagation to workaround the problem.");
2050 vtn_assert(dest
->var
->copy_prop_sampler
== NULL
);
2051 dest
->var
->copy_prop_sampler
=
2052 vtn_value(b
, w
[2], vtn_value_type_pointer
)->pointer
;
2056 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
2057 vtn_variable_store(b
, src
, dest
);
2061 case SpvOpArrayLength
: {
2062 struct vtn_pointer
*ptr
=
2063 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2065 const uint32_t offset
= ptr
->var
->type
->offsets
[w
[4]];
2066 const uint32_t stride
= ptr
->var
->type
->members
[w
[4]]->stride
;
2068 if (!ptr
->block_index
) {
2069 struct vtn_access_chain chain
= {
2072 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
2073 vtn_assert(ptr
->block_index
);
2076 nir_intrinsic_instr
*instr
=
2077 nir_intrinsic_instr_create(b
->nb
.shader
,
2078 nir_intrinsic_get_buffer_size
);
2079 instr
->src
[0] = nir_src_for_ssa(ptr
->block_index
);
2080 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
2081 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2082 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
2084 /* array_length = max(buffer_size - offset, 0) / stride */
2085 nir_ssa_def
*array_length
=
2090 nir_imm_int(&b
->nb
, offset
)),
2091 nir_imm_int(&b
->nb
, 0u)),
2092 nir_imm_int(&b
->nb
, stride
));
2094 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2095 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
2096 val
->ssa
->def
= array_length
;
2100 case SpvOpCopyMemorySized
:
2102 vtn_fail("Unhandled opcode");