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"
32 static struct vtn_access_chain
*
33 vtn_access_chain_create(struct vtn_builder
*b
, unsigned length
)
35 struct vtn_access_chain
*chain
;
37 /* Subtract 1 from the length since there's already one built in */
38 size_t size
= sizeof(*chain
) +
39 (MAX2(length
, 1) - 1) * sizeof(chain
->link
[0]);
40 chain
= rzalloc_size(b
, size
);
41 chain
->length
= length
;
47 vtn_pointer_uses_ssa_offset(struct vtn_builder
*b
,
48 struct vtn_pointer
*ptr
)
50 return ptr
->mode
== vtn_variable_mode_ubo
||
51 ptr
->mode
== vtn_variable_mode_ssbo
||
52 ptr
->mode
== vtn_variable_mode_push_constant
||
53 (ptr
->mode
== vtn_variable_mode_workgroup
&&
54 b
->options
->lower_workgroup_access_to_offsets
);
58 vtn_pointer_is_external_block(struct vtn_builder
*b
,
59 struct vtn_pointer
*ptr
)
61 return ptr
->mode
== vtn_variable_mode_ssbo
||
62 ptr
->mode
== vtn_variable_mode_ubo
||
63 ptr
->mode
== vtn_variable_mode_push_constant
||
64 (ptr
->mode
== vtn_variable_mode_workgroup
&&
65 b
->options
->lower_workgroup_access_to_offsets
);
69 vtn_access_link_as_ssa(struct vtn_builder
*b
, struct vtn_access_link link
,
70 unsigned stride
, unsigned bit_size
)
72 vtn_assert(stride
> 0);
73 if (link
.mode
== vtn_access_mode_literal
) {
74 return nir_imm_intN_t(&b
->nb
, link
.id
* stride
, bit_size
);
76 nir_ssa_def
*ssa
= vtn_ssa_value(b
, link
.id
)->def
;
77 if (ssa
->bit_size
!= bit_size
)
78 ssa
= nir_i2i(&b
->nb
, ssa
, bit_size
);
80 ssa
= nir_imul_imm(&b
->nb
, ssa
, stride
);
85 /* Dereference the given base pointer by the access chain */
86 static struct vtn_pointer
*
87 vtn_nir_deref_pointer_dereference(struct vtn_builder
*b
,
88 struct vtn_pointer
*base
,
89 struct vtn_access_chain
*deref_chain
)
91 struct vtn_type
*type
= base
->type
;
92 enum gl_access_qualifier access
= base
->access
;
94 nir_deref_instr
*tail
;
98 assert(base
->var
&& base
->var
->var
);
99 tail
= nir_build_deref_var(&b
->nb
, base
->var
->var
);
102 /* OpPtrAccessChain is only allowed on things which support variable
103 * pointers. For everything else, the client is expected to just pass us
104 * the right access chain.
106 vtn_assert(!deref_chain
->ptr_as_array
);
108 for (unsigned i
= 0; i
< deref_chain
->length
; i
++) {
109 if (glsl_type_is_struct(type
->type
)) {
110 vtn_assert(deref_chain
->link
[i
].mode
== vtn_access_mode_literal
);
111 unsigned idx
= deref_chain
->link
[i
].id
;
112 tail
= nir_build_deref_struct(&b
->nb
, tail
, idx
);
113 type
= type
->members
[idx
];
115 nir_ssa_def
*index
= vtn_access_link_as_ssa(b
, deref_chain
->link
[i
], 1,
116 tail
->dest
.ssa
.bit_size
);
117 tail
= nir_build_deref_array(&b
->nb
, tail
, index
);
118 type
= type
->array_element
;
121 access
|= type
->access
;
124 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
125 ptr
->mode
= base
->mode
;
127 ptr
->var
= base
->var
;
129 ptr
->access
= access
;
135 vtn_variable_resource_index(struct vtn_builder
*b
, struct vtn_variable
*var
,
136 nir_ssa_def
*desc_array_index
)
138 if (!desc_array_index
) {
139 vtn_assert(glsl_type_is_struct(var
->type
->type
));
140 desc_array_index
= nir_imm_int(&b
->nb
, 0);
143 nir_intrinsic_instr
*instr
=
144 nir_intrinsic_instr_create(b
->nb
.shader
,
145 nir_intrinsic_vulkan_resource_index
);
146 instr
->src
[0] = nir_src_for_ssa(desc_array_index
);
147 nir_intrinsic_set_desc_set(instr
, var
->descriptor_set
);
148 nir_intrinsic_set_binding(instr
, var
->binding
);
150 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
151 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
153 return &instr
->dest
.ssa
;
157 vtn_resource_reindex(struct vtn_builder
*b
, nir_ssa_def
*base_index
,
158 nir_ssa_def
*offset_index
)
160 nir_intrinsic_instr
*instr
=
161 nir_intrinsic_instr_create(b
->nb
.shader
,
162 nir_intrinsic_vulkan_resource_reindex
);
163 instr
->src
[0] = nir_src_for_ssa(base_index
);
164 instr
->src
[1] = nir_src_for_ssa(offset_index
);
166 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
167 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
169 return &instr
->dest
.ssa
;
172 static struct vtn_pointer
*
173 vtn_ssa_offset_pointer_dereference(struct vtn_builder
*b
,
174 struct vtn_pointer
*base
,
175 struct vtn_access_chain
*deref_chain
)
177 nir_ssa_def
*block_index
= base
->block_index
;
178 nir_ssa_def
*offset
= base
->offset
;
179 struct vtn_type
*type
= base
->type
;
180 enum gl_access_qualifier access
= base
->access
;
183 if (base
->mode
== vtn_variable_mode_ubo
||
184 base
->mode
== vtn_variable_mode_ssbo
) {
186 vtn_assert(base
->var
&& base
->type
);
187 nir_ssa_def
*desc_arr_idx
;
188 if (glsl_type_is_array(type
->type
)) {
189 if (deref_chain
->length
>= 1) {
191 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
193 /* This consumes a level of type */
194 type
= type
->array_element
;
195 access
|= type
->access
;
197 /* This is annoying. We've been asked for a pointer to the
198 * array of UBOs/SSBOs and not a specifc buffer. Return a
199 * pointer with a descriptor index of 0 and we'll have to do
200 * a reindex later to adjust it to the right thing.
202 desc_arr_idx
= nir_imm_int(&b
->nb
, 0);
204 } else if (deref_chain
->ptr_as_array
) {
205 /* You can't have a zero-length OpPtrAccessChain */
206 vtn_assert(deref_chain
->length
>= 1);
207 desc_arr_idx
= vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
209 /* We have a regular non-array SSBO. */
212 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
213 } else if (deref_chain
->ptr_as_array
&&
214 type
->base_type
== vtn_base_type_struct
&& type
->block
) {
215 /* We are doing an OpPtrAccessChain on a pointer to a struct that is
216 * decorated block. This is an interesting corner in the SPIR-V
217 * spec. One interpretation would be that they client is clearly
218 * trying to treat that block as if it's an implicit array of blocks
219 * repeated in the buffer. However, the SPIR-V spec for the
220 * OpPtrAccessChain says:
222 * "Base is treated as the address of the first element of an
223 * array, and the Element element’s address is computed to be the
224 * base for the Indexes, as per OpAccessChain."
226 * Taken literally, that would mean that your struct type is supposed
227 * to be treated as an array of such a struct and, since it's
228 * decorated block, that means an array of blocks which corresponds
229 * to an array descriptor. Therefore, we need to do a reindex
230 * operation to add the index from the first link in the access chain
231 * to the index we recieved.
233 * The downside to this interpretation (there always is one) is that
234 * this might be somewhat surprising behavior to apps if they expect
235 * the implicit array behavior described above.
237 vtn_assert(deref_chain
->length
>= 1);
238 nir_ssa_def
*offset_index
=
239 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1, 32);
242 block_index
= vtn_resource_reindex(b
, block_index
, offset_index
);
247 if (base
->mode
== vtn_variable_mode_workgroup
) {
248 /* SLM doesn't need nor have a block index */
249 vtn_assert(!block_index
);
251 /* We need the variable for the base offset */
252 vtn_assert(base
->var
);
254 /* We need ptr_type for size and alignment */
255 vtn_assert(base
->ptr_type
);
257 /* Assign location on first use so that we don't end up bloating SLM
258 * address space for variables which are never statically used.
260 if (base
->var
->shared_location
< 0) {
261 vtn_assert(base
->ptr_type
->length
> 0 && base
->ptr_type
->align
> 0);
262 b
->shader
->num_shared
= vtn_align_u32(b
->shader
->num_shared
,
263 base
->ptr_type
->align
);
264 base
->var
->shared_location
= b
->shader
->num_shared
;
265 b
->shader
->num_shared
+= base
->ptr_type
->length
;
268 offset
= nir_imm_int(&b
->nb
, base
->var
->shared_location
);
269 } else if (base
->mode
== vtn_variable_mode_push_constant
) {
270 /* Push constants neither need nor have a block index */
271 vtn_assert(!block_index
);
273 /* Start off with at the start of the push constant block. */
274 offset
= nir_imm_int(&b
->nb
, 0);
276 /* The code above should have ensured a block_index when needed. */
277 vtn_assert(block_index
);
279 /* Start off with at the start of the buffer. */
280 offset
= nir_imm_int(&b
->nb
, 0);
284 if (deref_chain
->ptr_as_array
&& idx
== 0) {
285 /* We need ptr_type for the stride */
286 vtn_assert(base
->ptr_type
);
288 /* We need at least one element in the chain */
289 vtn_assert(deref_chain
->length
>= 1);
291 nir_ssa_def
*elem_offset
=
292 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
293 base
->ptr_type
->stride
, offset
->bit_size
);
294 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
298 for (; idx
< deref_chain
->length
; idx
++) {
299 switch (glsl_get_base_type(type
->type
)) {
302 case GLSL_TYPE_UINT16
:
303 case GLSL_TYPE_INT16
:
304 case GLSL_TYPE_UINT8
:
306 case GLSL_TYPE_UINT64
:
307 case GLSL_TYPE_INT64
:
308 case GLSL_TYPE_FLOAT
:
309 case GLSL_TYPE_FLOAT16
:
310 case GLSL_TYPE_DOUBLE
:
312 case GLSL_TYPE_ARRAY
: {
313 nir_ssa_def
*elem_offset
=
314 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
315 type
->stride
, offset
->bit_size
);
316 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
317 type
= type
->array_element
;
318 access
|= type
->access
;
322 case GLSL_TYPE_STRUCT
: {
323 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
324 unsigned member
= deref_chain
->link
[idx
].id
;
325 offset
= nir_iadd_imm(&b
->nb
, offset
, type
->offsets
[member
]);
326 type
= type
->members
[member
];
327 access
|= type
->access
;
332 vtn_fail("Invalid type for deref");
336 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
337 ptr
->mode
= base
->mode
;
339 ptr
->block_index
= block_index
;
340 ptr
->offset
= offset
;
341 ptr
->access
= access
;
346 /* Dereference the given base pointer by the access chain */
347 static struct vtn_pointer
*
348 vtn_pointer_dereference(struct vtn_builder
*b
,
349 struct vtn_pointer
*base
,
350 struct vtn_access_chain
*deref_chain
)
352 if (vtn_pointer_uses_ssa_offset(b
, base
)) {
353 return vtn_ssa_offset_pointer_dereference(b
, base
, deref_chain
);
355 return vtn_nir_deref_pointer_dereference(b
, base
, deref_chain
);
360 vtn_pointer_for_variable(struct vtn_builder
*b
,
361 struct vtn_variable
*var
, struct vtn_type
*ptr_type
)
363 struct vtn_pointer
*pointer
= rzalloc(b
, struct vtn_pointer
);
365 pointer
->mode
= var
->mode
;
366 pointer
->type
= var
->type
;
367 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
368 vtn_assert(ptr_type
->deref
->type
== var
->type
->type
);
369 pointer
->ptr_type
= ptr_type
;
371 pointer
->access
= var
->access
| var
->type
->access
;
376 /* Returns an atomic_uint type based on the original uint type. The returned
377 * type will be equivalent to the original one but will have an atomic_uint
378 * type as leaf instead of an uint.
380 * Manages uint scalars, arrays, and arrays of arrays of any nested depth.
382 static const struct glsl_type
*
383 repair_atomic_type(const struct glsl_type
*type
)
385 assert(glsl_get_base_type(glsl_without_array(type
)) == GLSL_TYPE_UINT
);
386 assert(glsl_type_is_scalar(glsl_without_array(type
)));
388 if (glsl_type_is_array(type
)) {
389 const struct glsl_type
*atomic
=
390 repair_atomic_type(glsl_get_array_element(type
));
392 return glsl_array_type(atomic
, glsl_get_length(type
));
394 return glsl_atomic_uint_type();
399 vtn_pointer_to_deref(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
401 /* Do on-the-fly copy propagation for samplers. */
402 if (ptr
->var
&& ptr
->var
->copy_prop_sampler
)
403 return vtn_pointer_to_deref(b
, ptr
->var
->copy_prop_sampler
);
405 vtn_assert(!vtn_pointer_uses_ssa_offset(b
, ptr
));
407 struct vtn_access_chain chain
= {
410 ptr
= vtn_nir_deref_pointer_dereference(b
, ptr
, &chain
);
417 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_instr
*deref
,
418 struct vtn_ssa_value
*inout
)
420 if (glsl_type_is_vector_or_scalar(deref
->type
)) {
422 inout
->def
= nir_load_deref(&b
->nb
, deref
);
424 nir_store_deref(&b
->nb
, deref
, inout
->def
, ~0);
426 } else if (glsl_type_is_array(deref
->type
) ||
427 glsl_type_is_matrix(deref
->type
)) {
428 unsigned elems
= glsl_get_length(deref
->type
);
429 for (unsigned i
= 0; i
< elems
; i
++) {
430 nir_deref_instr
*child
=
431 nir_build_deref_array(&b
->nb
, deref
, nir_imm_int(&b
->nb
, i
));
432 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
]);
435 vtn_assert(glsl_type_is_struct(deref
->type
));
436 unsigned elems
= glsl_get_length(deref
->type
);
437 for (unsigned i
= 0; i
< elems
; i
++) {
438 nir_deref_instr
*child
= nir_build_deref_struct(&b
->nb
, deref
, i
);
439 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
]);
445 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
447 struct vtn_pointer
*ptr
= vtn_value(b
, id
, vtn_value_type_pointer
)->pointer
;
448 return vtn_pointer_to_deref(b
, ptr
);
452 * Gets the NIR-level deref tail, which may have as a child an array deref
453 * selecting which component due to OpAccessChain supporting per-component
454 * indexing in SPIR-V.
456 static nir_deref_instr
*
457 get_deref_tail(nir_deref_instr
*deref
)
459 if (deref
->deref_type
!= nir_deref_type_array
)
462 nir_deref_instr
*parent
=
463 nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
);
465 if (glsl_type_is_vector(parent
->type
))
471 struct vtn_ssa_value
*
472 vtn_local_load(struct vtn_builder
*b
, nir_deref_instr
*src
)
474 nir_deref_instr
*src_tail
= get_deref_tail(src
);
475 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
476 _vtn_local_load_store(b
, true, src_tail
, val
);
478 if (src_tail
!= src
) {
479 val
->type
= src
->type
;
480 if (nir_src_is_const(src
->arr
.index
))
481 val
->def
= vtn_vector_extract(b
, val
->def
,
482 nir_src_as_uint(src
->arr
.index
));
484 val
->def
= vtn_vector_extract_dynamic(b
, val
->def
, src
->arr
.index
.ssa
);
491 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
492 nir_deref_instr
*dest
)
494 nir_deref_instr
*dest_tail
= get_deref_tail(dest
);
496 if (dest_tail
!= dest
) {
497 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
498 _vtn_local_load_store(b
, true, dest_tail
, val
);
500 if (nir_src_is_const(dest
->arr
.index
))
501 val
->def
= vtn_vector_insert(b
, val
->def
, src
->def
,
502 nir_src_as_uint(dest
->arr
.index
));
504 val
->def
= vtn_vector_insert_dynamic(b
, val
->def
, src
->def
,
505 dest
->arr
.index
.ssa
);
506 _vtn_local_load_store(b
, false, dest_tail
, val
);
508 _vtn_local_load_store(b
, false, dest_tail
, src
);
513 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
514 nir_ssa_def
**index_out
)
516 assert(vtn_pointer_uses_ssa_offset(b
, ptr
));
518 struct vtn_access_chain chain
= {
521 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
523 *index_out
= ptr
->block_index
;
527 /* Tries to compute the size of an interface block based on the strides and
528 * offsets that are provided to us in the SPIR-V source.
531 vtn_type_block_size(struct vtn_builder
*b
, struct vtn_type
*type
)
533 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
537 case GLSL_TYPE_UINT16
:
538 case GLSL_TYPE_INT16
:
539 case GLSL_TYPE_UINT8
:
541 case GLSL_TYPE_UINT64
:
542 case GLSL_TYPE_INT64
:
543 case GLSL_TYPE_FLOAT
:
544 case GLSL_TYPE_FLOAT16
:
546 case GLSL_TYPE_DOUBLE
: {
547 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
548 glsl_get_matrix_columns(type
->type
);
550 vtn_assert(type
->stride
> 0);
551 return type
->stride
* cols
;
553 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
554 return glsl_get_vector_elements(type
->type
) * type_size
;
558 case GLSL_TYPE_STRUCT
:
559 case GLSL_TYPE_INTERFACE
: {
561 unsigned num_fields
= glsl_get_length(type
->type
);
562 for (unsigned f
= 0; f
< num_fields
; f
++) {
563 unsigned field_end
= type
->offsets
[f
] +
564 vtn_type_block_size(b
, type
->members
[f
]);
565 size
= MAX2(size
, field_end
);
570 case GLSL_TYPE_ARRAY
:
571 vtn_assert(type
->stride
> 0);
572 vtn_assert(glsl_get_length(type
->type
) > 0);
573 return type
->stride
* glsl_get_length(type
->type
);
576 vtn_fail("Invalid block type");
582 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
583 nir_ssa_def
*index
, nir_ssa_def
*offset
,
584 unsigned access_offset
, unsigned access_size
,
585 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
,
586 enum gl_access_qualifier access
)
588 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
589 instr
->num_components
= glsl_get_vector_elements(type
);
591 /* Booleans usually shouldn't show up in external memory in SPIR-V.
592 * However, they do for certain older GLSLang versions and can for shared
593 * memory when we lower access chains internally.
595 const unsigned data_bit_size
= glsl_type_is_boolean(type
) ? 32 :
596 glsl_get_bit_size(type
);
600 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
601 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
604 if (op
== nir_intrinsic_load_push_constant
) {
605 nir_intrinsic_set_base(instr
, access_offset
);
606 nir_intrinsic_set_range(instr
, access_size
);
609 if (op
== nir_intrinsic_load_ssbo
||
610 op
== nir_intrinsic_store_ssbo
) {
611 nir_intrinsic_set_access(instr
, access
);
614 /* With extensions like relaxed_block_layout, we really can't guarantee
615 * much more than scalar alignment.
617 if (op
!= nir_intrinsic_load_push_constant
)
618 nir_intrinsic_set_align(instr
, data_bit_size
/ 8, 0);
621 instr
->src
[src
++] = nir_src_for_ssa(index
);
623 if (op
== nir_intrinsic_load_push_constant
) {
624 /* We need to subtract the offset from where the intrinsic will load the
627 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
628 nir_imm_int(&b
->nb
, access_offset
)));
630 instr
->src
[src
++] = nir_src_for_ssa(offset
);
634 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
635 instr
->num_components
, data_bit_size
, NULL
);
636 (*inout
)->def
= &instr
->dest
.ssa
;
639 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
641 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
642 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
646 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
647 nir_ssa_def
*index
, nir_ssa_def
*offset
,
648 unsigned access_offset
, unsigned access_size
,
649 struct vtn_type
*type
, enum gl_access_qualifier access
,
650 struct vtn_ssa_value
**inout
)
652 if (load
&& *inout
== NULL
)
653 *inout
= vtn_create_ssa_value(b
, type
->type
);
655 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
659 case GLSL_TYPE_UINT16
:
660 case GLSL_TYPE_INT16
:
661 case GLSL_TYPE_UINT8
:
663 case GLSL_TYPE_UINT64
:
664 case GLSL_TYPE_INT64
:
665 case GLSL_TYPE_FLOAT
:
666 case GLSL_TYPE_FLOAT16
:
667 case GLSL_TYPE_DOUBLE
:
669 /* This is where things get interesting. At this point, we've hit
670 * a vector, a scalar, or a matrix.
672 if (glsl_type_is_matrix(type
->type
)) {
673 /* Loading the whole matrix */
674 struct vtn_ssa_value
*transpose
;
675 unsigned num_ops
, vec_width
, col_stride
;
676 if (type
->row_major
) {
677 num_ops
= glsl_get_vector_elements(type
->type
);
678 vec_width
= glsl_get_matrix_columns(type
->type
);
679 col_stride
= type
->array_element
->stride
;
681 const struct glsl_type
*transpose_type
=
682 glsl_matrix_type(base_type
, vec_width
, num_ops
);
683 *inout
= vtn_create_ssa_value(b
, transpose_type
);
685 transpose
= vtn_ssa_transpose(b
, *inout
);
689 num_ops
= glsl_get_matrix_columns(type
->type
);
690 vec_width
= glsl_get_vector_elements(type
->type
);
691 col_stride
= type
->stride
;
694 for (unsigned i
= 0; i
< num_ops
; i
++) {
695 nir_ssa_def
*elem_offset
=
696 nir_iadd_imm(&b
->nb
, offset
, i
* col_stride
);
697 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
698 access_offset
, access_size
,
700 glsl_vector_type(base_type
, vec_width
),
701 type
->access
| access
);
704 if (load
&& type
->row_major
)
705 *inout
= vtn_ssa_transpose(b
, *inout
);
707 unsigned elems
= glsl_get_vector_elements(type
->type
);
708 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
709 if (elems
== 1 || type
->stride
== type_size
) {
710 /* This is a tightly-packed normal scalar or vector load */
711 vtn_assert(glsl_type_is_vector_or_scalar(type
->type
));
712 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
713 access_offset
, access_size
,
715 type
->access
| access
);
717 /* This is a strided load. We have to load N things separately.
718 * This is the single column of a row-major matrix case.
720 vtn_assert(type
->stride
> type_size
);
721 vtn_assert(type
->stride
% type_size
== 0);
723 nir_ssa_def
*per_comp
[4];
724 for (unsigned i
= 0; i
< elems
; i
++) {
725 nir_ssa_def
*elem_offset
=
726 nir_iadd_imm(&b
->nb
, offset
, i
* type
->stride
);
727 struct vtn_ssa_value
*comp
, temp_val
;
729 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
730 temp_val
.type
= glsl_scalar_type(base_type
);
733 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
734 access_offset
, access_size
,
735 &comp
, glsl_scalar_type(base_type
),
736 type
->access
| access
);
737 per_comp
[i
] = comp
->def
;
742 *inout
= vtn_create_ssa_value(b
, type
->type
);
743 (*inout
)->def
= nir_vec(&b
->nb
, per_comp
, elems
);
749 case GLSL_TYPE_ARRAY
: {
750 unsigned elems
= glsl_get_length(type
->type
);
751 for (unsigned i
= 0; i
< elems
; i
++) {
752 nir_ssa_def
*elem_off
=
753 nir_iadd_imm(&b
->nb
, offset
, i
* type
->stride
);
754 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
755 access_offset
, access_size
,
757 type
->array_element
->access
| access
,
758 &(*inout
)->elems
[i
]);
763 case GLSL_TYPE_STRUCT
: {
764 unsigned elems
= glsl_get_length(type
->type
);
765 for (unsigned i
= 0; i
< elems
; i
++) {
766 nir_ssa_def
*elem_off
=
767 nir_iadd_imm(&b
->nb
, offset
, type
->offsets
[i
]);
768 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
769 access_offset
, access_size
,
771 type
->members
[i
]->access
| access
,
772 &(*inout
)->elems
[i
]);
778 vtn_fail("Invalid block member type");
782 static struct vtn_ssa_value
*
783 vtn_block_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
786 unsigned access_offset
= 0, access_size
= 0;
788 case vtn_variable_mode_ubo
:
789 op
= nir_intrinsic_load_ubo
;
791 case vtn_variable_mode_ssbo
:
792 op
= nir_intrinsic_load_ssbo
;
794 case vtn_variable_mode_push_constant
:
795 op
= nir_intrinsic_load_push_constant
;
796 access_size
= b
->shader
->num_uniforms
;
798 case vtn_variable_mode_workgroup
:
799 op
= nir_intrinsic_load_shared
;
802 vtn_fail("Invalid block variable mode");
805 nir_ssa_def
*offset
, *index
= NULL
;
806 offset
= vtn_pointer_to_offset(b
, src
, &index
);
808 struct vtn_ssa_value
*value
= NULL
;
809 _vtn_block_load_store(b
, op
, true, index
, offset
,
810 access_offset
, access_size
,
811 src
->type
, src
->access
, &value
);
816 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
817 struct vtn_pointer
*dst
)
821 case vtn_variable_mode_ssbo
:
822 op
= nir_intrinsic_store_ssbo
;
824 case vtn_variable_mode_workgroup
:
825 op
= nir_intrinsic_store_shared
;
828 vtn_fail("Invalid block variable mode");
831 nir_ssa_def
*offset
, *index
= NULL
;
832 offset
= vtn_pointer_to_offset(b
, dst
, &index
);
834 _vtn_block_load_store(b
, op
, false, index
, offset
,
835 0, 0, dst
->type
, dst
->access
, &src
);
839 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
840 struct vtn_pointer
*ptr
,
841 struct vtn_ssa_value
**inout
)
843 enum glsl_base_type base_type
= glsl_get_base_type(ptr
->type
->type
);
847 case GLSL_TYPE_UINT16
:
848 case GLSL_TYPE_INT16
:
849 case GLSL_TYPE_UINT8
:
851 case GLSL_TYPE_UINT64
:
852 case GLSL_TYPE_INT64
:
853 case GLSL_TYPE_FLOAT
:
854 case GLSL_TYPE_FLOAT16
:
856 case GLSL_TYPE_DOUBLE
:
857 /* At this point, we have a scalar, vector, or matrix so we know that
858 * there cannot be any structure splitting still in the way. By
859 * stopping at the matrix level rather than the vector level, we
860 * ensure that matrices get loaded in the optimal way even if they
861 * are storred row-major in a UBO.
864 *inout
= vtn_local_load(b
, vtn_pointer_to_deref(b
, ptr
));
866 vtn_local_store(b
, *inout
, vtn_pointer_to_deref(b
, ptr
));
870 case GLSL_TYPE_ARRAY
:
871 case GLSL_TYPE_STRUCT
: {
872 unsigned elems
= glsl_get_length(ptr
->type
->type
);
874 vtn_assert(*inout
== NULL
);
875 *inout
= rzalloc(b
, struct vtn_ssa_value
);
876 (*inout
)->type
= ptr
->type
->type
;
877 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
880 struct vtn_access_chain chain
= {
883 { .mode
= vtn_access_mode_literal
, },
886 for (unsigned i
= 0; i
< elems
; i
++) {
887 chain
.link
[0].id
= i
;
888 struct vtn_pointer
*elem
= vtn_pointer_dereference(b
, ptr
, &chain
);
889 _vtn_variable_load_store(b
, load
, elem
, &(*inout
)->elems
[i
]);
895 vtn_fail("Invalid access chain type");
899 struct vtn_ssa_value
*
900 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
902 if (vtn_pointer_is_external_block(b
, src
)) {
903 return vtn_block_load(b
, src
);
905 struct vtn_ssa_value
*val
= NULL
;
906 _vtn_variable_load_store(b
, true, src
, &val
);
912 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
913 struct vtn_pointer
*dest
)
915 if (vtn_pointer_is_external_block(b
, dest
)) {
916 vtn_assert(dest
->mode
== vtn_variable_mode_ssbo
||
917 dest
->mode
== vtn_variable_mode_workgroup
);
918 vtn_block_store(b
, src
, dest
);
920 _vtn_variable_load_store(b
, false, dest
, &src
);
925 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
926 struct vtn_pointer
*src
)
928 vtn_assert(src
->type
->type
== dest
->type
->type
);
929 enum glsl_base_type base_type
= glsl_get_base_type(src
->type
->type
);
933 case GLSL_TYPE_UINT16
:
934 case GLSL_TYPE_INT16
:
935 case GLSL_TYPE_UINT8
:
937 case GLSL_TYPE_UINT64
:
938 case GLSL_TYPE_INT64
:
939 case GLSL_TYPE_FLOAT
:
940 case GLSL_TYPE_FLOAT16
:
941 case GLSL_TYPE_DOUBLE
:
943 /* At this point, we have a scalar, vector, or matrix so we know that
944 * there cannot be any structure splitting still in the way. By
945 * stopping at the matrix level rather than the vector level, we
946 * ensure that matrices get loaded in the optimal way even if they
947 * are storred row-major in a UBO.
949 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
952 case GLSL_TYPE_ARRAY
:
953 case GLSL_TYPE_STRUCT
: {
954 struct vtn_access_chain chain
= {
957 { .mode
= vtn_access_mode_literal
, },
960 unsigned elems
= glsl_get_length(src
->type
->type
);
961 for (unsigned i
= 0; i
< elems
; i
++) {
962 chain
.link
[0].id
= i
;
963 struct vtn_pointer
*src_elem
=
964 vtn_pointer_dereference(b
, src
, &chain
);
965 struct vtn_pointer
*dest_elem
=
966 vtn_pointer_dereference(b
, dest
, &chain
);
968 _vtn_variable_copy(b
, dest_elem
, src_elem
);
974 vtn_fail("Invalid access chain type");
979 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
980 struct vtn_pointer
*src
)
982 /* TODO: At some point, we should add a special-case for when we can
983 * just emit a copy_var intrinsic.
985 _vtn_variable_copy(b
, dest
, src
);
989 set_mode_system_value(struct vtn_builder
*b
, nir_variable_mode
*mode
)
991 vtn_assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
992 *mode
= nir_var_system_value
;
996 vtn_get_builtin_location(struct vtn_builder
*b
,
997 SpvBuiltIn builtin
, int *location
,
998 nir_variable_mode
*mode
)
1001 case SpvBuiltInPosition
:
1002 *location
= VARYING_SLOT_POS
;
1004 case SpvBuiltInPointSize
:
1005 *location
= VARYING_SLOT_PSIZ
;
1007 case SpvBuiltInClipDistance
:
1008 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
1010 case SpvBuiltInCullDistance
:
1011 *location
= VARYING_SLOT_CULL_DIST0
;
1013 case SpvBuiltInVertexId
:
1014 case SpvBuiltInVertexIndex
:
1015 /* The Vulkan spec defines VertexIndex to be non-zero-based and doesn't
1016 * allow VertexId. The ARB_gl_spirv spec defines VertexId to be the
1017 * same as gl_VertexID, which is non-zero-based, and removes
1018 * VertexIndex. Since they're both defined to be non-zero-based, we use
1019 * SYSTEM_VALUE_VERTEX_ID for both.
1021 *location
= SYSTEM_VALUE_VERTEX_ID
;
1022 set_mode_system_value(b
, mode
);
1024 case SpvBuiltInInstanceIndex
:
1025 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
1026 set_mode_system_value(b
, mode
);
1028 case SpvBuiltInInstanceId
:
1029 *location
= SYSTEM_VALUE_INSTANCE_ID
;
1030 set_mode_system_value(b
, mode
);
1032 case SpvBuiltInPrimitiveId
:
1033 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1034 vtn_assert(*mode
== nir_var_shader_in
);
1035 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1036 } else if (*mode
== nir_var_shader_out
) {
1037 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1039 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
1040 set_mode_system_value(b
, mode
);
1043 case SpvBuiltInInvocationId
:
1044 *location
= SYSTEM_VALUE_INVOCATION_ID
;
1045 set_mode_system_value(b
, mode
);
1047 case SpvBuiltInLayer
:
1048 *location
= VARYING_SLOT_LAYER
;
1049 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1050 *mode
= nir_var_shader_in
;
1051 else if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1052 *mode
= nir_var_shader_out
;
1053 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1054 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1055 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1056 *mode
= nir_var_shader_out
;
1058 vtn_fail("invalid stage for SpvBuiltInLayer");
1060 case SpvBuiltInViewportIndex
:
1061 *location
= VARYING_SLOT_VIEWPORT
;
1062 if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1063 *mode
= nir_var_shader_out
;
1064 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1065 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1066 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1067 *mode
= nir_var_shader_out
;
1068 else if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1069 *mode
= nir_var_shader_in
;
1071 vtn_fail("invalid stage for SpvBuiltInViewportIndex");
1073 case SpvBuiltInTessLevelOuter
:
1074 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
1076 case SpvBuiltInTessLevelInner
:
1077 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
1079 case SpvBuiltInTessCoord
:
1080 *location
= SYSTEM_VALUE_TESS_COORD
;
1081 set_mode_system_value(b
, mode
);
1083 case SpvBuiltInPatchVertices
:
1084 *location
= SYSTEM_VALUE_VERTICES_IN
;
1085 set_mode_system_value(b
, mode
);
1087 case SpvBuiltInFragCoord
:
1088 *location
= VARYING_SLOT_POS
;
1089 vtn_assert(*mode
== nir_var_shader_in
);
1091 case SpvBuiltInPointCoord
:
1092 *location
= VARYING_SLOT_PNTC
;
1093 vtn_assert(*mode
== nir_var_shader_in
);
1095 case SpvBuiltInFrontFacing
:
1096 *location
= SYSTEM_VALUE_FRONT_FACE
;
1097 set_mode_system_value(b
, mode
);
1099 case SpvBuiltInSampleId
:
1100 *location
= SYSTEM_VALUE_SAMPLE_ID
;
1101 set_mode_system_value(b
, mode
);
1103 case SpvBuiltInSamplePosition
:
1104 *location
= SYSTEM_VALUE_SAMPLE_POS
;
1105 set_mode_system_value(b
, mode
);
1107 case SpvBuiltInSampleMask
:
1108 if (*mode
== nir_var_shader_out
) {
1109 *location
= FRAG_RESULT_SAMPLE_MASK
;
1111 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1112 set_mode_system_value(b
, mode
);
1115 case SpvBuiltInFragDepth
:
1116 *location
= FRAG_RESULT_DEPTH
;
1117 vtn_assert(*mode
== nir_var_shader_out
);
1119 case SpvBuiltInHelperInvocation
:
1120 *location
= SYSTEM_VALUE_HELPER_INVOCATION
;
1121 set_mode_system_value(b
, mode
);
1123 case SpvBuiltInNumWorkgroups
:
1124 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1125 set_mode_system_value(b
, mode
);
1127 case SpvBuiltInWorkgroupSize
:
1128 *location
= SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
1129 set_mode_system_value(b
, mode
);
1131 case SpvBuiltInWorkgroupId
:
1132 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1133 set_mode_system_value(b
, mode
);
1135 case SpvBuiltInLocalInvocationId
:
1136 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1137 set_mode_system_value(b
, mode
);
1139 case SpvBuiltInLocalInvocationIndex
:
1140 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1141 set_mode_system_value(b
, mode
);
1143 case SpvBuiltInGlobalInvocationId
:
1144 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1145 set_mode_system_value(b
, mode
);
1147 case SpvBuiltInBaseVertex
:
1148 /* OpenGL gl_BaseVertex (SYSTEM_VALUE_BASE_VERTEX) is not the same
1149 * semantic as SPIR-V BaseVertex (SYSTEM_VALUE_FIRST_VERTEX).
1151 *location
= SYSTEM_VALUE_FIRST_VERTEX
;
1152 set_mode_system_value(b
, mode
);
1154 case SpvBuiltInBaseInstance
:
1155 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1156 set_mode_system_value(b
, mode
);
1158 case SpvBuiltInDrawIndex
:
1159 *location
= SYSTEM_VALUE_DRAW_ID
;
1160 set_mode_system_value(b
, mode
);
1162 case SpvBuiltInSubgroupSize
:
1163 *location
= SYSTEM_VALUE_SUBGROUP_SIZE
;
1164 set_mode_system_value(b
, mode
);
1166 case SpvBuiltInSubgroupId
:
1167 *location
= SYSTEM_VALUE_SUBGROUP_ID
;
1168 set_mode_system_value(b
, mode
);
1170 case SpvBuiltInSubgroupLocalInvocationId
:
1171 *location
= SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1172 set_mode_system_value(b
, mode
);
1174 case SpvBuiltInNumSubgroups
:
1175 *location
= SYSTEM_VALUE_NUM_SUBGROUPS
;
1176 set_mode_system_value(b
, mode
);
1178 case SpvBuiltInDeviceIndex
:
1179 *location
= SYSTEM_VALUE_DEVICE_INDEX
;
1180 set_mode_system_value(b
, mode
);
1182 case SpvBuiltInViewIndex
:
1183 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1184 set_mode_system_value(b
, mode
);
1186 case SpvBuiltInSubgroupEqMask
:
1187 *location
= SYSTEM_VALUE_SUBGROUP_EQ_MASK
,
1188 set_mode_system_value(b
, mode
);
1190 case SpvBuiltInSubgroupGeMask
:
1191 *location
= SYSTEM_VALUE_SUBGROUP_GE_MASK
,
1192 set_mode_system_value(b
, mode
);
1194 case SpvBuiltInSubgroupGtMask
:
1195 *location
= SYSTEM_VALUE_SUBGROUP_GT_MASK
,
1196 set_mode_system_value(b
, mode
);
1198 case SpvBuiltInSubgroupLeMask
:
1199 *location
= SYSTEM_VALUE_SUBGROUP_LE_MASK
,
1200 set_mode_system_value(b
, mode
);
1202 case SpvBuiltInSubgroupLtMask
:
1203 *location
= SYSTEM_VALUE_SUBGROUP_LT_MASK
,
1204 set_mode_system_value(b
, mode
);
1206 case SpvBuiltInFragStencilRefEXT
:
1207 *location
= FRAG_RESULT_STENCIL
;
1208 vtn_assert(*mode
== nir_var_shader_out
);
1210 case SpvBuiltInWorkDim
:
1211 *location
= SYSTEM_VALUE_WORK_DIM
;
1212 set_mode_system_value(b
, mode
);
1214 case SpvBuiltInGlobalSize
:
1215 *location
= SYSTEM_VALUE_GLOBAL_GROUP_SIZE
;
1216 set_mode_system_value(b
, mode
);
1219 vtn_fail("unsupported builtin: %u", builtin
);
1224 apply_var_decoration(struct vtn_builder
*b
,
1225 struct nir_variable_data
*var_data
,
1226 const struct vtn_decoration
*dec
)
1228 switch (dec
->decoration
) {
1229 case SpvDecorationRelaxedPrecision
:
1230 break; /* FIXME: Do nothing with this for now. */
1231 case SpvDecorationNoPerspective
:
1232 var_data
->interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1234 case SpvDecorationFlat
:
1235 var_data
->interpolation
= INTERP_MODE_FLAT
;
1237 case SpvDecorationCentroid
:
1238 var_data
->centroid
= true;
1240 case SpvDecorationSample
:
1241 var_data
->sample
= true;
1243 case SpvDecorationInvariant
:
1244 var_data
->invariant
= true;
1246 case SpvDecorationConstant
:
1247 var_data
->read_only
= true;
1249 case SpvDecorationNonReadable
:
1250 var_data
->image
.access
|= ACCESS_NON_READABLE
;
1252 case SpvDecorationNonWritable
:
1253 var_data
->read_only
= true;
1254 var_data
->image
.access
|= ACCESS_NON_WRITEABLE
;
1256 case SpvDecorationRestrict
:
1257 var_data
->image
.access
|= ACCESS_RESTRICT
;
1259 case SpvDecorationVolatile
:
1260 var_data
->image
.access
|= ACCESS_VOLATILE
;
1262 case SpvDecorationCoherent
:
1263 var_data
->image
.access
|= ACCESS_COHERENT
;
1265 case SpvDecorationComponent
:
1266 var_data
->location_frac
= dec
->literals
[0];
1268 case SpvDecorationIndex
:
1269 var_data
->index
= dec
->literals
[0];
1271 case SpvDecorationBuiltIn
: {
1272 SpvBuiltIn builtin
= dec
->literals
[0];
1274 nir_variable_mode mode
= var_data
->mode
;
1275 vtn_get_builtin_location(b
, builtin
, &var_data
->location
, &mode
);
1276 var_data
->mode
= mode
;
1279 case SpvBuiltInTessLevelOuter
:
1280 case SpvBuiltInTessLevelInner
:
1281 var_data
->compact
= true;
1283 case SpvBuiltInFragCoord
:
1284 var_data
->pixel_center_integer
= b
->pixel_center_integer
;
1286 case SpvBuiltInSamplePosition
:
1287 var_data
->origin_upper_left
= b
->origin_upper_left
;
1294 case SpvDecorationSpecId
:
1295 case SpvDecorationRowMajor
:
1296 case SpvDecorationColMajor
:
1297 case SpvDecorationMatrixStride
:
1298 case SpvDecorationAliased
:
1299 case SpvDecorationUniform
:
1300 case SpvDecorationLinkageAttributes
:
1301 break; /* Do nothing with these here */
1303 case SpvDecorationPatch
:
1304 var_data
->patch
= true;
1307 case SpvDecorationLocation
:
1308 vtn_fail("Handled above");
1310 case SpvDecorationBlock
:
1311 case SpvDecorationBufferBlock
:
1312 case SpvDecorationArrayStride
:
1313 case SpvDecorationGLSLShared
:
1314 case SpvDecorationGLSLPacked
:
1315 break; /* These can apply to a type but we don't care about them */
1317 case SpvDecorationBinding
:
1318 case SpvDecorationDescriptorSet
:
1319 case SpvDecorationNoContraction
:
1320 case SpvDecorationInputAttachmentIndex
:
1321 vtn_warn("Decoration not allowed for variable or structure member: %s",
1322 spirv_decoration_to_string(dec
->decoration
));
1325 case SpvDecorationXfbBuffer
:
1326 var_data
->explicit_xfb_buffer
= true;
1327 var_data
->xfb_buffer
= dec
->literals
[0];
1328 var_data
->always_active_io
= true;
1330 case SpvDecorationXfbStride
:
1331 var_data
->explicit_xfb_stride
= true;
1332 var_data
->xfb_stride
= dec
->literals
[0];
1334 case SpvDecorationOffset
:
1335 var_data
->explicit_offset
= true;
1336 var_data
->offset
= dec
->literals
[0];
1339 case SpvDecorationStream
:
1340 var_data
->stream
= dec
->literals
[0];
1343 case SpvDecorationCPacked
:
1344 case SpvDecorationSaturatedConversion
:
1345 case SpvDecorationFuncParamAttr
:
1346 case SpvDecorationFPRoundingMode
:
1347 case SpvDecorationFPFastMathMode
:
1348 case SpvDecorationAlignment
:
1349 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1350 spirv_decoration_to_string(dec
->decoration
));
1353 case SpvDecorationHlslSemanticGOOGLE
:
1354 /* HLSL semantic decorations can safely be ignored by the driver. */
1358 vtn_fail("Unhandled decoration");
1363 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1364 const struct vtn_decoration
*dec
, void *out_is_patch
)
1366 if (dec
->decoration
== SpvDecorationPatch
) {
1367 *((bool *) out_is_patch
) = true;
1372 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1373 const struct vtn_decoration
*dec
, void *void_var
)
1375 struct vtn_variable
*vtn_var
= void_var
;
1377 /* Handle decorations that apply to a vtn_variable as a whole */
1378 switch (dec
->decoration
) {
1379 case SpvDecorationBinding
:
1380 vtn_var
->binding
= dec
->literals
[0];
1381 vtn_var
->explicit_binding
= true;
1383 case SpvDecorationDescriptorSet
:
1384 vtn_var
->descriptor_set
= dec
->literals
[0];
1386 case SpvDecorationInputAttachmentIndex
:
1387 vtn_var
->input_attachment_index
= dec
->literals
[0];
1389 case SpvDecorationPatch
:
1390 vtn_var
->patch
= true;
1392 case SpvDecorationOffset
:
1393 vtn_var
->offset
= dec
->literals
[0];
1395 case SpvDecorationNonWritable
:
1396 vtn_var
->access
|= ACCESS_NON_WRITEABLE
;
1398 case SpvDecorationNonReadable
:
1399 vtn_var
->access
|= ACCESS_NON_READABLE
;
1401 case SpvDecorationVolatile
:
1402 vtn_var
->access
|= ACCESS_VOLATILE
;
1404 case SpvDecorationCoherent
:
1405 vtn_var
->access
|= ACCESS_COHERENT
;
1407 case SpvDecorationHlslCounterBufferGOOGLE
:
1408 /* HLSL semantic decorations can safely be ignored by the driver. */
1414 if (val
->value_type
== vtn_value_type_pointer
) {
1415 assert(val
->pointer
->var
== void_var
);
1416 assert(member
== -1);
1418 assert(val
->value_type
== vtn_value_type_type
);
1421 /* Location is odd. If applied to a split structure, we have to walk the
1422 * whole thing and accumulate the location. It's easier to handle as a
1425 if (dec
->decoration
== SpvDecorationLocation
) {
1426 unsigned location
= dec
->literals
[0];
1427 bool is_vertex_input
= false;
1428 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
&&
1429 vtn_var
->mode
== vtn_variable_mode_output
) {
1430 location
+= FRAG_RESULT_DATA0
;
1431 } else if (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
&&
1432 vtn_var
->mode
== vtn_variable_mode_input
) {
1433 is_vertex_input
= true;
1434 location
+= VERT_ATTRIB_GENERIC0
;
1435 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1436 vtn_var
->mode
== vtn_variable_mode_output
) {
1437 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1438 } else if (vtn_var
->mode
!= vtn_variable_mode_uniform
) {
1439 vtn_warn("Location must be on input, output, uniform, sampler or "
1444 if (vtn_var
->var
->num_members
== 0) {
1445 /* This handles the member and lone variable cases */
1446 vtn_var
->var
->data
.location
= location
;
1448 /* This handles the structure member case */
1449 assert(vtn_var
->var
->members
);
1450 for (unsigned i
= 0; i
< vtn_var
->var
->num_members
; i
++) {
1451 vtn_var
->var
->members
[i
].location
= location
;
1452 const struct glsl_type
*member_type
=
1453 glsl_get_struct_field(vtn_var
->var
->interface_type
, i
);
1454 location
+= glsl_count_attribute_slots(member_type
,
1461 if (vtn_var
->var
->num_members
== 0) {
1462 assert(member
== -1);
1463 apply_var_decoration(b
, &vtn_var
->var
->data
, dec
);
1464 } else if (member
>= 0) {
1465 /* Member decorations must come from a type */
1466 assert(val
->value_type
== vtn_value_type_type
);
1467 apply_var_decoration(b
, &vtn_var
->var
->members
[member
], dec
);
1470 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1471 for (unsigned i
= 0; i
< length
; i
++)
1472 apply_var_decoration(b
, &vtn_var
->var
->members
[i
], dec
);
1475 /* A few variables, those with external storage, have no actual
1476 * nir_variables associated with them. Fortunately, all decorations
1477 * we care about for those variables are on the type only.
1479 vtn_assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1480 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1481 vtn_var
->mode
== vtn_variable_mode_push_constant
||
1482 (vtn_var
->mode
== vtn_variable_mode_workgroup
&&
1483 b
->options
->lower_workgroup_access_to_offsets
));
1488 static enum vtn_variable_mode
1489 vtn_storage_class_to_mode(struct vtn_builder
*b
,
1490 SpvStorageClass
class,
1491 struct vtn_type
*interface_type
,
1492 nir_variable_mode
*nir_mode_out
)
1494 enum vtn_variable_mode mode
;
1495 nir_variable_mode nir_mode
;
1497 case SpvStorageClassUniform
:
1498 if (interface_type
->block
) {
1499 mode
= vtn_variable_mode_ubo
;
1501 } else if (interface_type
->buffer_block
) {
1502 mode
= vtn_variable_mode_ssbo
;
1505 /* Default-block uniforms, coming from gl_spirv */
1506 mode
= vtn_variable_mode_uniform
;
1507 nir_mode
= nir_var_uniform
;
1510 case SpvStorageClassStorageBuffer
:
1511 mode
= vtn_variable_mode_ssbo
;
1514 case SpvStorageClassUniformConstant
:
1515 mode
= vtn_variable_mode_uniform
;
1516 nir_mode
= nir_var_uniform
;
1518 case SpvStorageClassPushConstant
:
1519 mode
= vtn_variable_mode_push_constant
;
1520 nir_mode
= nir_var_uniform
;
1522 case SpvStorageClassInput
:
1523 mode
= vtn_variable_mode_input
;
1524 nir_mode
= nir_var_shader_in
;
1526 case SpvStorageClassOutput
:
1527 mode
= vtn_variable_mode_output
;
1528 nir_mode
= nir_var_shader_out
;
1530 case SpvStorageClassPrivate
:
1531 mode
= vtn_variable_mode_global
;
1532 nir_mode
= nir_var_global
;
1534 case SpvStorageClassFunction
:
1535 mode
= vtn_variable_mode_local
;
1536 nir_mode
= nir_var_local
;
1538 case SpvStorageClassWorkgroup
:
1539 mode
= vtn_variable_mode_workgroup
;
1540 nir_mode
= nir_var_shared
;
1542 case SpvStorageClassAtomicCounter
:
1543 mode
= vtn_variable_mode_uniform
;
1544 nir_mode
= nir_var_uniform
;
1546 case SpvStorageClassCrossWorkgroup
:
1547 case SpvStorageClassGeneric
:
1549 vtn_fail("Unhandled variable storage class");
1553 *nir_mode_out
= nir_mode
;
1559 vtn_pointer_to_ssa(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
1561 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
1562 /* This pointer needs to have a pointer type with actual storage */
1563 vtn_assert(ptr
->ptr_type
);
1564 vtn_assert(ptr
->ptr_type
->type
);
1567 /* If we don't have an offset then we must be a pointer to the variable
1570 vtn_assert(!ptr
->offset
&& !ptr
->block_index
);
1572 struct vtn_access_chain chain
= {
1575 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
1578 vtn_assert(ptr
->offset
);
1579 if (ptr
->block_index
) {
1580 vtn_assert(ptr
->mode
== vtn_variable_mode_ubo
||
1581 ptr
->mode
== vtn_variable_mode_ssbo
);
1582 return nir_vec2(&b
->nb
, ptr
->block_index
, ptr
->offset
);
1584 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
);
1588 return &vtn_pointer_to_deref(b
, ptr
)->dest
.ssa
;
1592 struct vtn_pointer
*
1593 vtn_pointer_from_ssa(struct vtn_builder
*b
, nir_ssa_def
*ssa
,
1594 struct vtn_type
*ptr_type
)
1596 vtn_assert(ssa
->num_components
<= 2 && ssa
->bit_size
== 32);
1597 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1599 struct vtn_type
*interface_type
= ptr_type
->deref
;
1600 while (interface_type
->base_type
== vtn_base_type_array
)
1601 interface_type
= interface_type
->array_element
;
1603 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
1604 nir_variable_mode nir_mode
;
1605 ptr
->mode
= vtn_storage_class_to_mode(b
, ptr_type
->storage_class
,
1606 interface_type
, &nir_mode
);
1607 ptr
->type
= ptr_type
->deref
;
1608 ptr
->ptr_type
= ptr_type
;
1610 if (ptr
->mode
== vtn_variable_mode_ubo
||
1611 ptr
->mode
== vtn_variable_mode_ssbo
) {
1612 /* This pointer type needs to have actual storage */
1613 vtn_assert(ptr_type
->type
);
1614 vtn_assert(ssa
->num_components
== 2);
1615 ptr
->block_index
= nir_channel(&b
->nb
, ssa
, 0);
1616 ptr
->offset
= nir_channel(&b
->nb
, ssa
, 1);
1617 } else if ((ptr
->mode
== vtn_variable_mode_workgroup
&&
1618 b
->options
->lower_workgroup_access_to_offsets
) ||
1619 ptr
->mode
== vtn_variable_mode_push_constant
) {
1620 /* This pointer type needs to have actual storage */
1621 vtn_assert(ptr_type
->type
);
1622 vtn_assert(ssa
->num_components
== 1);
1623 ptr
->block_index
= NULL
;
1626 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
1627 ptr_type
->deref
->type
);
1634 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
1636 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
1639 if (var
->mode
== vtn_variable_mode_input
) {
1640 return stage
== MESA_SHADER_TESS_CTRL
||
1641 stage
== MESA_SHADER_TESS_EVAL
||
1642 stage
== MESA_SHADER_GEOMETRY
;
1645 if (var
->mode
== vtn_variable_mode_output
)
1646 return stage
== MESA_SHADER_TESS_CTRL
;
1652 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
1653 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
1654 nir_constant
*initializer
)
1656 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1657 struct vtn_type
*type
= ptr_type
->deref
;
1659 struct vtn_type
*without_array
= type
;
1660 while(glsl_type_is_array(without_array
->type
))
1661 without_array
= without_array
->array_element
;
1663 enum vtn_variable_mode mode
;
1664 nir_variable_mode nir_mode
;
1665 mode
= vtn_storage_class_to_mode(b
, storage_class
, without_array
, &nir_mode
);
1668 case vtn_variable_mode_ubo
:
1669 b
->shader
->info
.num_ubos
++;
1671 case vtn_variable_mode_ssbo
:
1672 b
->shader
->info
.num_ssbos
++;
1674 case vtn_variable_mode_uniform
:
1675 if (glsl_type_is_image(without_array
->type
))
1676 b
->shader
->info
.num_images
++;
1677 else if (glsl_type_is_sampler(without_array
->type
))
1678 b
->shader
->info
.num_textures
++;
1680 case vtn_variable_mode_push_constant
:
1681 b
->shader
->num_uniforms
= vtn_type_block_size(b
, type
);
1684 /* No tallying is needed */
1688 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
1692 vtn_assert(val
->value_type
== vtn_value_type_pointer
);
1693 val
->pointer
= vtn_pointer_for_variable(b
, var
, ptr_type
);
1695 switch (var
->mode
) {
1696 case vtn_variable_mode_local
:
1697 case vtn_variable_mode_global
:
1698 case vtn_variable_mode_uniform
:
1699 /* For these, we create the variable normally */
1700 var
->var
= rzalloc(b
->shader
, nir_variable
);
1701 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1703 /* Need to tweak the nir type here as at vtn_handle_type we don't have
1704 * the access to storage_class, that is the one that points us that is
1707 if (storage_class
== SpvStorageClassAtomicCounter
) {
1708 var
->var
->type
= repair_atomic_type(var
->type
->type
);
1710 var
->var
->type
= var
->type
->type
;
1712 var
->var
->data
.mode
= nir_mode
;
1713 var
->var
->data
.location
= -1;
1714 var
->var
->interface_type
= NULL
;
1717 case vtn_variable_mode_workgroup
:
1718 if (b
->options
->lower_workgroup_access_to_offsets
) {
1719 var
->shared_location
= -1;
1721 /* Create the variable normally */
1722 var
->var
= rzalloc(b
->shader
, nir_variable
);
1723 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1724 var
->var
->type
= var
->type
->type
;
1725 var
->var
->data
.mode
= nir_var_shared
;
1729 case vtn_variable_mode_input
:
1730 case vtn_variable_mode_output
: {
1731 /* In order to know whether or not we're a per-vertex inout, we need
1732 * the patch qualifier. This means walking the variable decorations
1733 * early before we actually create any variables. Not a big deal.
1735 * GLSLang really likes to place decorations in the most interior
1736 * thing it possibly can. In particular, if you have a struct, it
1737 * will place the patch decorations on the struct members. This
1738 * should be handled by the variable splitting below just fine.
1740 * If you have an array-of-struct, things get even more weird as it
1741 * will place the patch decorations on the struct even though it's
1742 * inside an array and some of the members being patch and others not
1743 * makes no sense whatsoever. Since the only sensible thing is for
1744 * it to be all or nothing, we'll call it patch if any of the members
1745 * are declared patch.
1748 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
1749 if (glsl_type_is_array(var
->type
->type
) &&
1750 glsl_type_is_struct(without_array
->type
)) {
1751 vtn_foreach_decoration(b
, vtn_value(b
, without_array
->id
,
1752 vtn_value_type_type
),
1753 var_is_patch_cb
, &var
->patch
);
1756 /* For inputs and outputs, we immediately split structures. This
1757 * is for a couple of reasons. For one, builtins may all come in
1758 * a struct and we really want those split out into separate
1759 * variables. For another, interpolation qualifiers can be
1760 * applied to members of the top-level struct ane we need to be
1761 * able to preserve that information.
1764 struct vtn_type
*interface_type
= var
->type
;
1765 if (is_per_vertex_inout(var
, b
->shader
->info
.stage
)) {
1766 /* In Geometry shaders (and some tessellation), inputs come
1767 * in per-vertex arrays. However, some builtins come in
1768 * non-per-vertex, hence the need for the is_array check. In
1769 * any case, there are no non-builtin arrays allowed so this
1770 * check should be sufficient.
1772 interface_type
= var
->type
->array_element
;
1775 var
->var
= rzalloc(b
->shader
, nir_variable
);
1776 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1777 var
->var
->type
= var
->type
->type
;
1778 var
->var
->interface_type
= interface_type
->type
;
1779 var
->var
->data
.mode
= nir_mode
;
1780 var
->var
->data
.patch
= var
->patch
;
1782 if (glsl_type_is_struct(interface_type
->type
)) {
1783 /* It's a struct. Set it up as per-member. */
1784 var
->var
->num_members
= glsl_get_length(interface_type
->type
);
1785 var
->var
->members
= rzalloc_array(var
->var
, struct nir_variable_data
,
1786 var
->var
->num_members
);
1788 for (unsigned i
= 0; i
< var
->var
->num_members
; i
++) {
1789 var
->var
->members
[i
].mode
= nir_mode
;
1790 var
->var
->members
[i
].patch
= var
->patch
;
1794 /* For inputs and outputs, we need to grab locations and builtin
1795 * information from the interface type.
1797 vtn_foreach_decoration(b
, vtn_value(b
, interface_type
->id
,
1798 vtn_value_type_type
),
1799 var_decoration_cb
, var
);
1803 case vtn_variable_mode_ubo
:
1804 case vtn_variable_mode_ssbo
:
1805 case vtn_variable_mode_push_constant
:
1806 /* These don't need actual variables. */
1811 var
->var
->constant_initializer
=
1812 nir_constant_clone(initializer
, var
->var
);
1815 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
1817 if (var
->mode
== vtn_variable_mode_uniform
) {
1818 /* XXX: We still need the binding information in the nir_variable
1819 * for these. We should fix that.
1821 var
->var
->data
.binding
= var
->binding
;
1822 var
->var
->data
.explicit_binding
= var
->explicit_binding
;
1823 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
1824 var
->var
->data
.index
= var
->input_attachment_index
;
1825 var
->var
->data
.offset
= var
->offset
;
1827 if (glsl_type_is_image(without_array
->type
))
1828 var
->var
->data
.image
.format
= without_array
->image_format
;
1831 if (var
->mode
== vtn_variable_mode_local
) {
1832 vtn_assert(var
->var
!= NULL
&& var
->var
->members
== NULL
);
1833 nir_function_impl_add_variable(b
->nb
.impl
, var
->var
);
1834 } else if (var
->var
) {
1835 nir_shader_add_variable(b
->shader
, var
->var
);
1837 vtn_assert(vtn_pointer_is_external_block(b
, val
->pointer
));
1842 vtn_assert_types_equal(struct vtn_builder
*b
, SpvOp opcode
,
1843 struct vtn_type
*dst_type
,
1844 struct vtn_type
*src_type
)
1846 if (dst_type
->id
== src_type
->id
)
1849 if (vtn_types_compatible(b
, dst_type
, src_type
)) {
1850 /* Early versions of GLSLang would re-emit types unnecessarily and you
1851 * would end up with OpLoad, OpStore, or OpCopyMemory opcodes which have
1852 * mismatched source and destination types.
1854 * https://github.com/KhronosGroup/glslang/issues/304
1855 * https://github.com/KhronosGroup/glslang/issues/307
1856 * https://bugs.freedesktop.org/show_bug.cgi?id=104338
1857 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
1859 vtn_warn("Source and destination types of %s do not have the same "
1860 "ID (but are compatible): %u vs %u",
1861 spirv_op_to_string(opcode
), dst_type
->id
, src_type
->id
);
1865 vtn_fail("Source and destination types of %s do not match: %s vs. %s",
1866 spirv_op_to_string(opcode
),
1867 glsl_get_type_name(dst_type
->type
),
1868 glsl_get_type_name(src_type
->type
));
1872 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
1873 const uint32_t *w
, unsigned count
)
1877 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1878 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1882 case SpvOpVariable
: {
1883 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1885 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1887 SpvStorageClass storage_class
= w
[3];
1888 nir_constant
*initializer
= NULL
;
1890 initializer
= vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
1892 vtn_create_variable(b
, val
, ptr_type
, storage_class
, initializer
);
1896 case SpvOpAccessChain
:
1897 case SpvOpPtrAccessChain
:
1898 case SpvOpInBoundsAccessChain
: {
1899 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
1900 chain
->ptr_as_array
= (opcode
== SpvOpPtrAccessChain
);
1903 for (int i
= 4; i
< count
; i
++) {
1904 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
1905 if (link_val
->value_type
== vtn_value_type_constant
) {
1906 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
1907 switch (glsl_get_bit_size(link_val
->type
->type
)) {
1909 chain
->link
[idx
].id
= link_val
->constant
->values
[0].i8
[0];
1912 chain
->link
[idx
].id
= link_val
->constant
->values
[0].i16
[0];
1915 chain
->link
[idx
].id
= link_val
->constant
->values
[0].i32
[0];
1918 chain
->link
[idx
].id
= link_val
->constant
->values
[0].i64
[0];
1921 vtn_fail("Invalid bit size");
1924 chain
->link
[idx
].mode
= vtn_access_mode_id
;
1925 chain
->link
[idx
].id
= w
[i
];
1931 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1932 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
1933 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1934 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1935 * to combine an array of images with a single sampler to get an
1936 * array of sampled images that all share the same sampler.
1937 * Fortunately, this means that we can more-or-less ignore the
1938 * sampler when crawling the access chain, but it does leave us
1939 * with this rather awkward little special-case.
1941 struct vtn_value
*val
=
1942 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1943 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1944 val
->sampled_image
->type
= base_val
->sampled_image
->type
;
1945 val
->sampled_image
->image
=
1946 vtn_pointer_dereference(b
, base_val
->sampled_image
->image
, chain
);
1947 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
1949 vtn_assert(base_val
->value_type
== vtn_value_type_pointer
);
1950 struct vtn_value
*val
=
1951 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1952 val
->pointer
= vtn_pointer_dereference(b
, base_val
->pointer
, chain
);
1953 val
->pointer
->ptr_type
= ptr_type
;
1958 case SpvOpCopyMemory
: {
1959 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
1960 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
1962 vtn_assert_types_equal(b
, opcode
, dest
->type
->deref
, src
->type
->deref
);
1964 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
1969 struct vtn_type
*res_type
=
1970 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1971 struct vtn_value
*src_val
= vtn_value(b
, w
[3], vtn_value_type_pointer
);
1972 struct vtn_pointer
*src
= src_val
->pointer
;
1974 vtn_assert_types_equal(b
, opcode
, res_type
, src_val
->type
->deref
);
1976 if (glsl_type_is_image(res_type
->type
) ||
1977 glsl_type_is_sampler(res_type
->type
)) {
1978 vtn_push_value(b
, w
[2], vtn_value_type_pointer
)->pointer
= src
;
1982 vtn_push_ssa(b
, w
[2], res_type
, vtn_variable_load(b
, src
));
1987 struct vtn_value
*dest_val
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
1988 struct vtn_pointer
*dest
= dest_val
->pointer
;
1989 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[2]);
1991 /* OpStore requires us to actually have a storage type */
1992 vtn_fail_if(dest
->type
->type
== NULL
,
1993 "Invalid destination type for OpStore");
1995 if (glsl_get_base_type(dest
->type
->type
) == GLSL_TYPE_BOOL
&&
1996 glsl_get_base_type(src_val
->type
->type
) == GLSL_TYPE_UINT
) {
1997 /* Early versions of GLSLang would use uint types for UBOs/SSBOs but
1998 * would then store them to a local variable as bool. Work around
1999 * the issue by doing an implicit conversion.
2001 * https://github.com/KhronosGroup/glslang/issues/170
2002 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2004 vtn_warn("OpStore of value of type OpTypeInt to a pointer to type "
2005 "OpTypeBool. Doing an implicit conversion to work around "
2007 struct vtn_ssa_value
*bool_ssa
=
2008 vtn_create_ssa_value(b
, dest
->type
->type
);
2009 bool_ssa
->def
= nir_i2b(&b
->nb
, vtn_ssa_value(b
, w
[2])->def
);
2010 vtn_variable_store(b
, bool_ssa
, dest
);
2014 vtn_assert_types_equal(b
, opcode
, dest_val
->type
->deref
, src_val
->type
);
2016 if (glsl_type_is_sampler(dest
->type
->type
)) {
2017 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
2018 "propagation to workaround the problem.");
2019 vtn_assert(dest
->var
->copy_prop_sampler
== NULL
);
2020 dest
->var
->copy_prop_sampler
=
2021 vtn_value(b
, w
[2], vtn_value_type_pointer
)->pointer
;
2025 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
2026 vtn_variable_store(b
, src
, dest
);
2030 case SpvOpArrayLength
: {
2031 struct vtn_pointer
*ptr
=
2032 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2034 const uint32_t offset
= ptr
->var
->type
->offsets
[w
[4]];
2035 const uint32_t stride
= ptr
->var
->type
->members
[w
[4]]->stride
;
2037 if (!ptr
->block_index
) {
2038 struct vtn_access_chain chain
= {
2041 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
2042 vtn_assert(ptr
->block_index
);
2045 nir_intrinsic_instr
*instr
=
2046 nir_intrinsic_instr_create(b
->nb
.shader
,
2047 nir_intrinsic_get_buffer_size
);
2048 instr
->src
[0] = nir_src_for_ssa(ptr
->block_index
);
2049 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
2050 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2051 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
2053 /* array_length = max(buffer_size - offset, 0) / stride */
2054 nir_ssa_def
*array_length
=
2059 nir_imm_int(&b
->nb
, offset
)),
2060 nir_imm_int(&b
->nb
, 0u)),
2061 nir_imm_int(&b
->nb
, stride
));
2063 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2064 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
2065 val
->ssa
->def
= array_length
;
2069 case SpvOpCopyMemorySized
:
2071 vtn_fail("Unhandled opcode");