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
;
46 static struct vtn_access_chain
*
47 vtn_access_chain_extend(struct vtn_builder
*b
, struct vtn_access_chain
*old
,
50 struct vtn_access_chain
*chain
;
52 unsigned old_len
= old
? old
->length
: 0;
53 chain
= vtn_access_chain_create(b
, old_len
+ new_ids
);
55 for (unsigned i
= 0; i
< old_len
; i
++)
56 chain
->link
[i
] = old
->link
[i
];
62 vtn_pointer_uses_ssa_offset(struct vtn_builder
*b
,
63 struct vtn_pointer
*ptr
)
65 return ptr
->mode
== vtn_variable_mode_ubo
||
66 ptr
->mode
== vtn_variable_mode_ssbo
||
67 ptr
->mode
== vtn_variable_mode_push_constant
||
68 (ptr
->mode
== vtn_variable_mode_workgroup
&&
69 b
->options
->lower_workgroup_access_to_offsets
);
73 vtn_pointer_is_external_block(struct vtn_builder
*b
,
74 struct vtn_pointer
*ptr
)
76 return ptr
->mode
== vtn_variable_mode_ssbo
||
77 ptr
->mode
== vtn_variable_mode_ubo
||
78 ptr
->mode
== vtn_variable_mode_push_constant
||
79 (ptr
->mode
== vtn_variable_mode_workgroup
&&
80 b
->options
->lower_workgroup_access_to_offsets
);
83 /* Dereference the given base pointer by the access chain */
84 static struct vtn_pointer
*
85 vtn_access_chain_pointer_dereference(struct vtn_builder
*b
,
86 struct vtn_pointer
*base
,
87 struct vtn_access_chain
*deref_chain
)
89 struct vtn_access_chain
*chain
=
90 vtn_access_chain_extend(b
, base
->chain
, deref_chain
->length
);
91 struct vtn_type
*type
= base
->type
;
92 enum gl_access_qualifier access
= base
->access
;
94 /* OpPtrAccessChain is only allowed on things which support variable
95 * pointers. For everything else, the client is expected to just pass us
96 * the right access chain.
98 vtn_assert(!deref_chain
->ptr_as_array
);
100 unsigned start
= base
->chain
? base
->chain
->length
: 0;
101 for (unsigned i
= 0; i
< deref_chain
->length
; i
++) {
102 chain
->link
[start
+ i
] = deref_chain
->link
[i
];
104 if (glsl_type_is_struct(type
->type
)) {
105 vtn_assert(deref_chain
->link
[i
].mode
== vtn_access_mode_literal
);
106 type
= type
->members
[deref_chain
->link
[i
].id
];
108 type
= type
->array_element
;
111 access
|= type
->access
;
114 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
115 ptr
->mode
= base
->mode
;
117 ptr
->var
= base
->var
;
118 ptr
->deref
= base
->deref
;
120 ptr
->access
= access
;
126 vtn_access_link_as_ssa(struct vtn_builder
*b
, struct vtn_access_link link
,
129 vtn_assert(stride
> 0);
130 if (link
.mode
== vtn_access_mode_literal
) {
131 return nir_imm_int(&b
->nb
, link
.id
* stride
);
132 } else if (stride
== 1) {
133 nir_ssa_def
*ssa
= vtn_ssa_value(b
, link
.id
)->def
;
134 if (ssa
->bit_size
!= 32)
135 ssa
= nir_u2u32(&b
->nb
, ssa
);
138 nir_ssa_def
*src0
= vtn_ssa_value(b
, link
.id
)->def
;
139 if (src0
->bit_size
!= 32)
140 src0
= nir_u2u32(&b
->nb
, src0
);
141 return nir_imul_imm(&b
->nb
, src0
, stride
);
146 vtn_variable_resource_index(struct vtn_builder
*b
, struct vtn_variable
*var
,
147 nir_ssa_def
*desc_array_index
)
149 if (!desc_array_index
) {
150 vtn_assert(glsl_type_is_struct(var
->type
->type
));
151 desc_array_index
= nir_imm_int(&b
->nb
, 0);
154 nir_intrinsic_instr
*instr
=
155 nir_intrinsic_instr_create(b
->nb
.shader
,
156 nir_intrinsic_vulkan_resource_index
);
157 instr
->src
[0] = nir_src_for_ssa(desc_array_index
);
158 nir_intrinsic_set_desc_set(instr
, var
->descriptor_set
);
159 nir_intrinsic_set_binding(instr
, var
->binding
);
161 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
162 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
164 return &instr
->dest
.ssa
;
168 vtn_resource_reindex(struct vtn_builder
*b
, nir_ssa_def
*base_index
,
169 nir_ssa_def
*offset_index
)
171 nir_intrinsic_instr
*instr
=
172 nir_intrinsic_instr_create(b
->nb
.shader
,
173 nir_intrinsic_vulkan_resource_reindex
);
174 instr
->src
[0] = nir_src_for_ssa(base_index
);
175 instr
->src
[1] = nir_src_for_ssa(offset_index
);
177 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
178 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
180 return &instr
->dest
.ssa
;
183 static struct vtn_pointer
*
184 vtn_ssa_offset_pointer_dereference(struct vtn_builder
*b
,
185 struct vtn_pointer
*base
,
186 struct vtn_access_chain
*deref_chain
)
188 nir_ssa_def
*block_index
= base
->block_index
;
189 nir_ssa_def
*offset
= base
->offset
;
190 struct vtn_type
*type
= base
->type
;
191 enum gl_access_qualifier access
= base
->access
;
194 if (base
->mode
== vtn_variable_mode_ubo
||
195 base
->mode
== vtn_variable_mode_ssbo
) {
197 vtn_assert(base
->var
&& base
->type
);
198 nir_ssa_def
*desc_arr_idx
;
199 if (glsl_type_is_array(type
->type
)) {
200 if (deref_chain
->length
>= 1) {
202 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1);
204 /* This consumes a level of type */
205 type
= type
->array_element
;
206 access
|= type
->access
;
208 /* This is annoying. We've been asked for a pointer to the
209 * array of UBOs/SSBOs and not a specifc buffer. Return a
210 * pointer with a descriptor index of 0 and we'll have to do
211 * a reindex later to adjust it to the right thing.
213 desc_arr_idx
= nir_imm_int(&b
->nb
, 0);
215 } else if (deref_chain
->ptr_as_array
) {
216 /* You can't have a zero-length OpPtrAccessChain */
217 vtn_assert(deref_chain
->length
>= 1);
218 desc_arr_idx
= vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1);
220 /* We have a regular non-array SSBO. */
223 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
224 } else if (deref_chain
->ptr_as_array
&&
225 type
->base_type
== vtn_base_type_struct
&& type
->block
) {
226 /* We are doing an OpPtrAccessChain on a pointer to a struct that is
227 * decorated block. This is an interesting corner in the SPIR-V
228 * spec. One interpretation would be that they client is clearly
229 * trying to treat that block as if it's an implicit array of blocks
230 * repeated in the buffer. However, the SPIR-V spec for the
231 * OpPtrAccessChain says:
233 * "Base is treated as the address of the first element of an
234 * array, and the Element element’s address is computed to be the
235 * base for the Indexes, as per OpAccessChain."
237 * Taken literally, that would mean that your struct type is supposed
238 * to be treated as an array of such a struct and, since it's
239 * decorated block, that means an array of blocks which corresponds
240 * to an array descriptor. Therefore, we need to do a reindex
241 * operation to add the index from the first link in the access chain
242 * to the index we recieved.
244 * The downside to this interpretation (there always is one) is that
245 * this might be somewhat surprising behavior to apps if they expect
246 * the implicit array behavior described above.
248 vtn_assert(deref_chain
->length
>= 1);
249 nir_ssa_def
*offset_index
=
250 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1);
253 block_index
= vtn_resource_reindex(b
, block_index
, offset_index
);
258 if (base
->mode
== vtn_variable_mode_workgroup
) {
259 /* SLM doesn't need nor have a block index */
260 vtn_assert(!block_index
);
262 /* We need the variable for the base offset */
263 vtn_assert(base
->var
);
265 /* We need ptr_type for size and alignment */
266 vtn_assert(base
->ptr_type
);
268 /* Assign location on first use so that we don't end up bloating SLM
269 * address space for variables which are never statically used.
271 if (base
->var
->shared_location
< 0) {
272 vtn_assert(base
->ptr_type
->length
> 0 && base
->ptr_type
->align
> 0);
273 b
->shader
->num_shared
= vtn_align_u32(b
->shader
->num_shared
,
274 base
->ptr_type
->align
);
275 base
->var
->shared_location
= b
->shader
->num_shared
;
276 b
->shader
->num_shared
+= base
->ptr_type
->length
;
279 offset
= nir_imm_int(&b
->nb
, base
->var
->shared_location
);
280 } else if (base
->mode
== vtn_variable_mode_push_constant
) {
281 /* Push constants neither need nor have a block index */
282 vtn_assert(!block_index
);
284 /* Start off with at the start of the push constant block. */
285 offset
= nir_imm_int(&b
->nb
, 0);
287 /* The code above should have ensured a block_index when needed. */
288 vtn_assert(block_index
);
290 /* Start off with at the start of the buffer. */
291 offset
= nir_imm_int(&b
->nb
, 0);
295 if (deref_chain
->ptr_as_array
&& idx
== 0) {
296 /* We need ptr_type for the stride */
297 vtn_assert(base
->ptr_type
);
299 /* We need at least one element in the chain */
300 vtn_assert(deref_chain
->length
>= 1);
302 nir_ssa_def
*elem_offset
=
303 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
304 base
->ptr_type
->stride
);
305 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
309 for (; idx
< deref_chain
->length
; idx
++) {
310 switch (glsl_get_base_type(type
->type
)) {
313 case GLSL_TYPE_UINT16
:
314 case GLSL_TYPE_INT16
:
315 case GLSL_TYPE_UINT8
:
317 case GLSL_TYPE_UINT64
:
318 case GLSL_TYPE_INT64
:
319 case GLSL_TYPE_FLOAT
:
320 case GLSL_TYPE_FLOAT16
:
321 case GLSL_TYPE_DOUBLE
:
323 case GLSL_TYPE_ARRAY
: {
324 nir_ssa_def
*elem_offset
=
325 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
], type
->stride
);
326 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
327 type
= type
->array_element
;
328 access
|= type
->access
;
332 case GLSL_TYPE_STRUCT
: {
333 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
334 unsigned member
= deref_chain
->link
[idx
].id
;
335 offset
= nir_iadd_imm(&b
->nb
, offset
, type
->offsets
[member
]);
336 type
= type
->members
[member
];
337 access
|= type
->access
;
342 vtn_fail("Invalid type for deref");
346 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
347 ptr
->mode
= base
->mode
;
349 ptr
->block_index
= block_index
;
350 ptr
->offset
= offset
;
351 ptr
->access
= access
;
356 /* Dereference the given base pointer by the access chain */
357 static struct vtn_pointer
*
358 vtn_pointer_dereference(struct vtn_builder
*b
,
359 struct vtn_pointer
*base
,
360 struct vtn_access_chain
*deref_chain
)
362 if (vtn_pointer_uses_ssa_offset(b
, base
)) {
363 return vtn_ssa_offset_pointer_dereference(b
, base
, deref_chain
);
365 return vtn_access_chain_pointer_dereference(b
, base
, deref_chain
);
370 vtn_pointer_for_variable(struct vtn_builder
*b
,
371 struct vtn_variable
*var
, struct vtn_type
*ptr_type
)
373 struct vtn_pointer
*pointer
= rzalloc(b
, struct vtn_pointer
);
375 pointer
->mode
= var
->mode
;
376 pointer
->type
= var
->type
;
377 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
378 vtn_assert(ptr_type
->deref
->type
== var
->type
->type
);
379 pointer
->ptr_type
= ptr_type
;
381 pointer
->access
= var
->access
| var
->type
->access
;
386 /* Returns an atomic_uint type based on the original uint type. The returned
387 * type will be equivalent to the original one but will have an atomic_uint
388 * type as leaf instead of an uint.
390 * Manages uint scalars, arrays, and arrays of arrays of any nested depth.
392 static const struct glsl_type
*
393 repair_atomic_type(const struct glsl_type
*type
)
395 assert(glsl_get_base_type(glsl_without_array(type
)) == GLSL_TYPE_UINT
);
396 assert(glsl_type_is_scalar(glsl_without_array(type
)));
398 if (glsl_type_is_array(type
)) {
399 const struct glsl_type
*atomic
=
400 repair_atomic_type(glsl_get_array_element(type
));
402 return glsl_array_type(atomic
, glsl_get_length(type
));
404 return glsl_atomic_uint_type();
409 vtn_pointer_to_deref(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
411 /* Do on-the-fly copy propagation for samplers. */
412 if (ptr
->var
&& ptr
->var
->copy_prop_sampler
)
413 return vtn_pointer_to_deref(b
, ptr
->var
->copy_prop_sampler
);
415 nir_deref_instr
*tail
;
419 assert(ptr
->var
&& ptr
->var
->var
);
420 tail
= nir_build_deref_var(&b
->nb
, ptr
->var
->var
);
423 /* Raw variable access */
427 struct vtn_access_chain
*chain
= ptr
->chain
;
430 for (unsigned i
= 0; i
< chain
->length
; i
++) {
431 if (glsl_type_is_struct(tail
->type
)) {
432 vtn_assert(chain
->link
[i
].mode
== vtn_access_mode_literal
);
433 unsigned idx
= chain
->link
[i
].id
;
434 tail
= nir_build_deref_struct(&b
->nb
, tail
, idx
);
437 if (chain
->link
[i
].mode
== vtn_access_mode_literal
) {
438 index
= nir_imm_int(&b
->nb
, chain
->link
[i
].id
);
440 vtn_assert(chain
->link
[i
].mode
== vtn_access_mode_id
);
441 index
= vtn_ssa_value(b
, chain
->link
[i
].id
)->def
;
443 tail
= nir_build_deref_array(&b
->nb
, tail
, index
);
451 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_instr
*deref
,
452 struct vtn_ssa_value
*inout
)
454 if (glsl_type_is_vector_or_scalar(deref
->type
)) {
456 inout
->def
= nir_load_deref(&b
->nb
, deref
);
458 nir_store_deref(&b
->nb
, deref
, inout
->def
, ~0);
460 } else if (glsl_type_is_array(deref
->type
) ||
461 glsl_type_is_matrix(deref
->type
)) {
462 unsigned elems
= glsl_get_length(deref
->type
);
463 for (unsigned i
= 0; i
< elems
; i
++) {
464 nir_deref_instr
*child
=
465 nir_build_deref_array(&b
->nb
, deref
, nir_imm_int(&b
->nb
, i
));
466 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
]);
469 vtn_assert(glsl_type_is_struct(deref
->type
));
470 unsigned elems
= glsl_get_length(deref
->type
);
471 for (unsigned i
= 0; i
< elems
; i
++) {
472 nir_deref_instr
*child
= nir_build_deref_struct(&b
->nb
, deref
, i
);
473 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
]);
479 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
481 struct vtn_pointer
*ptr
= vtn_value(b
, id
, vtn_value_type_pointer
)->pointer
;
482 return vtn_pointer_to_deref(b
, ptr
);
486 * Gets the NIR-level deref tail, which may have as a child an array deref
487 * selecting which component due to OpAccessChain supporting per-component
488 * indexing in SPIR-V.
490 static nir_deref_instr
*
491 get_deref_tail(nir_deref_instr
*deref
)
493 if (deref
->deref_type
!= nir_deref_type_array
)
496 nir_deref_instr
*parent
=
497 nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
);
499 if (glsl_type_is_vector(parent
->type
))
505 struct vtn_ssa_value
*
506 vtn_local_load(struct vtn_builder
*b
, nir_deref_instr
*src
)
508 nir_deref_instr
*src_tail
= get_deref_tail(src
);
509 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
510 _vtn_local_load_store(b
, true, src_tail
, val
);
512 if (src_tail
!= src
) {
513 val
->type
= src
->type
;
514 nir_const_value
*const_index
= nir_src_as_const_value(src
->arr
.index
);
516 val
->def
= vtn_vector_extract(b
, val
->def
, const_index
->u32
[0]);
518 val
->def
= vtn_vector_extract_dynamic(b
, val
->def
, src
->arr
.index
.ssa
);
525 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
526 nir_deref_instr
*dest
)
528 nir_deref_instr
*dest_tail
= get_deref_tail(dest
);
530 if (dest_tail
!= dest
) {
531 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
532 _vtn_local_load_store(b
, true, dest_tail
, val
);
534 nir_const_value
*const_index
= nir_src_as_const_value(dest
->arr
.index
);
536 val
->def
= vtn_vector_insert(b
, val
->def
, src
->def
,
537 const_index
->u32
[0]);
539 val
->def
= vtn_vector_insert_dynamic(b
, val
->def
, src
->def
,
540 dest
->arr
.index
.ssa
);
541 _vtn_local_load_store(b
, false, dest_tail
, val
);
543 _vtn_local_load_store(b
, false, dest_tail
, src
);
548 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
549 nir_ssa_def
**index_out
)
551 assert(vtn_pointer_uses_ssa_offset(b
, ptr
));
553 struct vtn_access_chain chain
= {
556 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
558 *index_out
= ptr
->block_index
;
562 /* Tries to compute the size of an interface block based on the strides and
563 * offsets that are provided to us in the SPIR-V source.
566 vtn_type_block_size(struct vtn_builder
*b
, struct vtn_type
*type
)
568 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
572 case GLSL_TYPE_UINT16
:
573 case GLSL_TYPE_INT16
:
574 case GLSL_TYPE_UINT8
:
576 case GLSL_TYPE_UINT64
:
577 case GLSL_TYPE_INT64
:
578 case GLSL_TYPE_FLOAT
:
579 case GLSL_TYPE_FLOAT16
:
581 case GLSL_TYPE_DOUBLE
: {
582 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
583 glsl_get_matrix_columns(type
->type
);
585 vtn_assert(type
->stride
> 0);
586 return type
->stride
* cols
;
588 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
589 return glsl_get_vector_elements(type
->type
) * type_size
;
593 case GLSL_TYPE_STRUCT
:
594 case GLSL_TYPE_INTERFACE
: {
596 unsigned num_fields
= glsl_get_length(type
->type
);
597 for (unsigned f
= 0; f
< num_fields
; f
++) {
598 unsigned field_end
= type
->offsets
[f
] +
599 vtn_type_block_size(b
, type
->members
[f
]);
600 size
= MAX2(size
, field_end
);
605 case GLSL_TYPE_ARRAY
:
606 vtn_assert(type
->stride
> 0);
607 vtn_assert(glsl_get_length(type
->type
) > 0);
608 return type
->stride
* glsl_get_length(type
->type
);
611 vtn_fail("Invalid block type");
617 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
618 nir_ssa_def
*index
, nir_ssa_def
*offset
,
619 unsigned access_offset
, unsigned access_size
,
620 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
,
621 enum gl_access_qualifier access
)
623 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
624 instr
->num_components
= glsl_get_vector_elements(type
);
626 /* Booleans usually shouldn't show up in external memory in SPIR-V.
627 * However, they do for certain older GLSLang versions and can for shared
628 * memory when we lower access chains internally.
630 const unsigned data_bit_size
= glsl_type_is_boolean(type
) ? 32 :
631 glsl_get_bit_size(type
);
635 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
636 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
639 if (op
== nir_intrinsic_load_push_constant
) {
640 nir_intrinsic_set_base(instr
, access_offset
);
641 nir_intrinsic_set_range(instr
, access_size
);
644 if (op
== nir_intrinsic_load_ssbo
||
645 op
== nir_intrinsic_store_ssbo
) {
646 nir_intrinsic_set_access(instr
, access
);
650 instr
->src
[src
++] = nir_src_for_ssa(index
);
652 if (op
== nir_intrinsic_load_push_constant
) {
653 /* We need to subtract the offset from where the intrinsic will load the
656 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
657 nir_imm_int(&b
->nb
, access_offset
)));
659 instr
->src
[src
++] = nir_src_for_ssa(offset
);
663 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
664 instr
->num_components
, data_bit_size
, NULL
);
665 (*inout
)->def
= &instr
->dest
.ssa
;
668 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
670 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
671 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
675 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
676 nir_ssa_def
*index
, nir_ssa_def
*offset
,
677 unsigned access_offset
, unsigned access_size
,
678 struct vtn_type
*type
, enum gl_access_qualifier access
,
679 struct vtn_ssa_value
**inout
)
681 if (load
&& *inout
== NULL
)
682 *inout
= vtn_create_ssa_value(b
, type
->type
);
684 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
688 case GLSL_TYPE_UINT16
:
689 case GLSL_TYPE_INT16
:
690 case GLSL_TYPE_UINT8
:
692 case GLSL_TYPE_UINT64
:
693 case GLSL_TYPE_INT64
:
694 case GLSL_TYPE_FLOAT
:
695 case GLSL_TYPE_FLOAT16
:
696 case GLSL_TYPE_DOUBLE
:
698 /* This is where things get interesting. At this point, we've hit
699 * a vector, a scalar, or a matrix.
701 if (glsl_type_is_matrix(type
->type
)) {
702 /* Loading the whole matrix */
703 struct vtn_ssa_value
*transpose
;
704 unsigned num_ops
, vec_width
, col_stride
;
705 if (type
->row_major
) {
706 num_ops
= glsl_get_vector_elements(type
->type
);
707 vec_width
= glsl_get_matrix_columns(type
->type
);
708 col_stride
= type
->array_element
->stride
;
710 const struct glsl_type
*transpose_type
=
711 glsl_matrix_type(base_type
, vec_width
, num_ops
);
712 *inout
= vtn_create_ssa_value(b
, transpose_type
);
714 transpose
= vtn_ssa_transpose(b
, *inout
);
718 num_ops
= glsl_get_matrix_columns(type
->type
);
719 vec_width
= glsl_get_vector_elements(type
->type
);
720 col_stride
= type
->stride
;
723 for (unsigned i
= 0; i
< num_ops
; i
++) {
724 nir_ssa_def
*elem_offset
=
725 nir_iadd_imm(&b
->nb
, offset
, i
* col_stride
);
726 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
727 access_offset
, access_size
,
729 glsl_vector_type(base_type
, vec_width
),
730 type
->access
| access
);
733 if (load
&& type
->row_major
)
734 *inout
= vtn_ssa_transpose(b
, *inout
);
736 unsigned elems
= glsl_get_vector_elements(type
->type
);
737 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
738 if (elems
== 1 || type
->stride
== type_size
) {
739 /* This is a tightly-packed normal scalar or vector load */
740 vtn_assert(glsl_type_is_vector_or_scalar(type
->type
));
741 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
742 access_offset
, access_size
,
744 type
->access
| access
);
746 /* This is a strided load. We have to load N things separately.
747 * This is the single column of a row-major matrix case.
749 vtn_assert(type
->stride
> type_size
);
750 vtn_assert(type
->stride
% type_size
== 0);
752 nir_ssa_def
*per_comp
[4];
753 for (unsigned i
= 0; i
< elems
; i
++) {
754 nir_ssa_def
*elem_offset
=
755 nir_iadd_imm(&b
->nb
, offset
, i
* type
->stride
);
756 struct vtn_ssa_value
*comp
, temp_val
;
758 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
759 temp_val
.type
= glsl_scalar_type(base_type
);
762 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
763 access_offset
, access_size
,
764 &comp
, glsl_scalar_type(base_type
),
765 type
->access
| access
);
766 per_comp
[i
] = comp
->def
;
771 *inout
= vtn_create_ssa_value(b
, type
->type
);
772 (*inout
)->def
= nir_vec(&b
->nb
, per_comp
, elems
);
778 case GLSL_TYPE_ARRAY
: {
779 unsigned elems
= glsl_get_length(type
->type
);
780 for (unsigned i
= 0; i
< elems
; i
++) {
781 nir_ssa_def
*elem_off
=
782 nir_iadd_imm(&b
->nb
, offset
, i
* type
->stride
);
783 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
784 access_offset
, access_size
,
786 type
->array_element
->access
| access
,
787 &(*inout
)->elems
[i
]);
792 case GLSL_TYPE_STRUCT
: {
793 unsigned elems
= glsl_get_length(type
->type
);
794 for (unsigned i
= 0; i
< elems
; i
++) {
795 nir_ssa_def
*elem_off
=
796 nir_iadd_imm(&b
->nb
, offset
, type
->offsets
[i
]);
797 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
798 access_offset
, access_size
,
800 type
->members
[i
]->access
| access
,
801 &(*inout
)->elems
[i
]);
807 vtn_fail("Invalid block member type");
811 static struct vtn_ssa_value
*
812 vtn_block_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
815 unsigned access_offset
= 0, access_size
= 0;
817 case vtn_variable_mode_ubo
:
818 op
= nir_intrinsic_load_ubo
;
820 case vtn_variable_mode_ssbo
:
821 op
= nir_intrinsic_load_ssbo
;
823 case vtn_variable_mode_push_constant
:
824 op
= nir_intrinsic_load_push_constant
;
825 access_size
= b
->shader
->num_uniforms
;
827 case vtn_variable_mode_workgroup
:
828 op
= nir_intrinsic_load_shared
;
831 vtn_fail("Invalid block variable mode");
834 nir_ssa_def
*offset
, *index
= NULL
;
835 offset
= vtn_pointer_to_offset(b
, src
, &index
);
837 struct vtn_ssa_value
*value
= NULL
;
838 _vtn_block_load_store(b
, op
, true, index
, offset
,
839 access_offset
, access_size
,
840 src
->type
, src
->access
, &value
);
845 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
846 struct vtn_pointer
*dst
)
850 case vtn_variable_mode_ssbo
:
851 op
= nir_intrinsic_store_ssbo
;
853 case vtn_variable_mode_workgroup
:
854 op
= nir_intrinsic_store_shared
;
857 vtn_fail("Invalid block variable mode");
860 nir_ssa_def
*offset
, *index
= NULL
;
861 offset
= vtn_pointer_to_offset(b
, dst
, &index
);
863 _vtn_block_load_store(b
, op
, false, index
, offset
,
864 0, 0, dst
->type
, dst
->access
, &src
);
868 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
869 struct vtn_pointer
*ptr
,
870 struct vtn_ssa_value
**inout
)
872 enum glsl_base_type base_type
= glsl_get_base_type(ptr
->type
->type
);
876 case GLSL_TYPE_UINT16
:
877 case GLSL_TYPE_INT16
:
878 case GLSL_TYPE_UINT8
:
880 case GLSL_TYPE_UINT64
:
881 case GLSL_TYPE_INT64
:
882 case GLSL_TYPE_FLOAT
:
883 case GLSL_TYPE_FLOAT16
:
885 case GLSL_TYPE_DOUBLE
:
886 /* At this point, we have a scalar, vector, or matrix so we know that
887 * there cannot be any structure splitting still in the way. By
888 * stopping at the matrix level rather than the vector level, we
889 * ensure that matrices get loaded in the optimal way even if they
890 * are storred row-major in a UBO.
893 *inout
= vtn_local_load(b
, vtn_pointer_to_deref(b
, ptr
));
895 vtn_local_store(b
, *inout
, vtn_pointer_to_deref(b
, ptr
));
899 case GLSL_TYPE_ARRAY
:
900 case GLSL_TYPE_STRUCT
: {
901 unsigned elems
= glsl_get_length(ptr
->type
->type
);
903 vtn_assert(*inout
== NULL
);
904 *inout
= rzalloc(b
, struct vtn_ssa_value
);
905 (*inout
)->type
= ptr
->type
->type
;
906 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
909 struct vtn_access_chain chain
= {
912 { .mode
= vtn_access_mode_literal
, },
915 for (unsigned i
= 0; i
< elems
; i
++) {
916 chain
.link
[0].id
= i
;
917 struct vtn_pointer
*elem
= vtn_pointer_dereference(b
, ptr
, &chain
);
918 _vtn_variable_load_store(b
, load
, elem
, &(*inout
)->elems
[i
]);
924 vtn_fail("Invalid access chain type");
928 struct vtn_ssa_value
*
929 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
931 if (vtn_pointer_is_external_block(b
, src
)) {
932 return vtn_block_load(b
, src
);
934 struct vtn_ssa_value
*val
= NULL
;
935 _vtn_variable_load_store(b
, true, src
, &val
);
941 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
942 struct vtn_pointer
*dest
)
944 if (vtn_pointer_is_external_block(b
, dest
)) {
945 vtn_assert(dest
->mode
== vtn_variable_mode_ssbo
||
946 dest
->mode
== vtn_variable_mode_workgroup
);
947 vtn_block_store(b
, src
, dest
);
949 _vtn_variable_load_store(b
, false, dest
, &src
);
954 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
955 struct vtn_pointer
*src
)
957 vtn_assert(src
->type
->type
== dest
->type
->type
);
958 enum glsl_base_type base_type
= glsl_get_base_type(src
->type
->type
);
962 case GLSL_TYPE_UINT16
:
963 case GLSL_TYPE_INT16
:
964 case GLSL_TYPE_UINT8
:
966 case GLSL_TYPE_UINT64
:
967 case GLSL_TYPE_INT64
:
968 case GLSL_TYPE_FLOAT
:
969 case GLSL_TYPE_FLOAT16
:
970 case GLSL_TYPE_DOUBLE
:
972 /* At this point, we have a scalar, vector, or matrix so we know that
973 * there cannot be any structure splitting still in the way. By
974 * stopping at the matrix level rather than the vector level, we
975 * ensure that matrices get loaded in the optimal way even if they
976 * are storred row-major in a UBO.
978 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
981 case GLSL_TYPE_ARRAY
:
982 case GLSL_TYPE_STRUCT
: {
983 struct vtn_access_chain chain
= {
986 { .mode
= vtn_access_mode_literal
, },
989 unsigned elems
= glsl_get_length(src
->type
->type
);
990 for (unsigned i
= 0; i
< elems
; i
++) {
991 chain
.link
[0].id
= i
;
992 struct vtn_pointer
*src_elem
=
993 vtn_pointer_dereference(b
, src
, &chain
);
994 struct vtn_pointer
*dest_elem
=
995 vtn_pointer_dereference(b
, dest
, &chain
);
997 _vtn_variable_copy(b
, dest_elem
, src_elem
);
1003 vtn_fail("Invalid access chain type");
1008 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
1009 struct vtn_pointer
*src
)
1011 /* TODO: At some point, we should add a special-case for when we can
1012 * just emit a copy_var intrinsic.
1014 _vtn_variable_copy(b
, dest
, src
);
1018 set_mode_system_value(struct vtn_builder
*b
, nir_variable_mode
*mode
)
1020 vtn_assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
1021 *mode
= nir_var_system_value
;
1025 vtn_get_builtin_location(struct vtn_builder
*b
,
1026 SpvBuiltIn builtin
, int *location
,
1027 nir_variable_mode
*mode
)
1030 case SpvBuiltInPosition
:
1031 *location
= VARYING_SLOT_POS
;
1033 case SpvBuiltInPointSize
:
1034 *location
= VARYING_SLOT_PSIZ
;
1036 case SpvBuiltInClipDistance
:
1037 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
1039 case SpvBuiltInCullDistance
:
1040 *location
= VARYING_SLOT_CULL_DIST0
;
1042 case SpvBuiltInVertexId
:
1043 case SpvBuiltInVertexIndex
:
1044 /* The Vulkan spec defines VertexIndex to be non-zero-based and doesn't
1045 * allow VertexId. The ARB_gl_spirv spec defines VertexId to be the
1046 * same as gl_VertexID, which is non-zero-based, and removes
1047 * VertexIndex. Since they're both defined to be non-zero-based, we use
1048 * SYSTEM_VALUE_VERTEX_ID for both.
1050 *location
= SYSTEM_VALUE_VERTEX_ID
;
1051 set_mode_system_value(b
, mode
);
1053 case SpvBuiltInInstanceIndex
:
1054 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
1055 set_mode_system_value(b
, mode
);
1057 case SpvBuiltInInstanceId
:
1058 *location
= SYSTEM_VALUE_INSTANCE_ID
;
1059 set_mode_system_value(b
, mode
);
1061 case SpvBuiltInPrimitiveId
:
1062 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1063 vtn_assert(*mode
== nir_var_shader_in
);
1064 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1065 } else if (*mode
== nir_var_shader_out
) {
1066 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1068 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
1069 set_mode_system_value(b
, mode
);
1072 case SpvBuiltInInvocationId
:
1073 *location
= SYSTEM_VALUE_INVOCATION_ID
;
1074 set_mode_system_value(b
, mode
);
1076 case SpvBuiltInLayer
:
1077 *location
= VARYING_SLOT_LAYER
;
1078 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1079 *mode
= nir_var_shader_in
;
1080 else 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
;
1087 vtn_fail("invalid stage for SpvBuiltInLayer");
1089 case SpvBuiltInViewportIndex
:
1090 *location
= VARYING_SLOT_VIEWPORT
;
1091 if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1092 *mode
= nir_var_shader_out
;
1093 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1094 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1095 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1096 *mode
= nir_var_shader_out
;
1097 else if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1098 *mode
= nir_var_shader_in
;
1100 vtn_fail("invalid stage for SpvBuiltInViewportIndex");
1102 case SpvBuiltInTessLevelOuter
:
1103 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
1105 case SpvBuiltInTessLevelInner
:
1106 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
1108 case SpvBuiltInTessCoord
:
1109 *location
= SYSTEM_VALUE_TESS_COORD
;
1110 set_mode_system_value(b
, mode
);
1112 case SpvBuiltInPatchVertices
:
1113 *location
= SYSTEM_VALUE_VERTICES_IN
;
1114 set_mode_system_value(b
, mode
);
1116 case SpvBuiltInFragCoord
:
1117 *location
= VARYING_SLOT_POS
;
1118 vtn_assert(*mode
== nir_var_shader_in
);
1120 case SpvBuiltInPointCoord
:
1121 *location
= VARYING_SLOT_PNTC
;
1122 vtn_assert(*mode
== nir_var_shader_in
);
1124 case SpvBuiltInFrontFacing
:
1125 *location
= SYSTEM_VALUE_FRONT_FACE
;
1126 set_mode_system_value(b
, mode
);
1128 case SpvBuiltInSampleId
:
1129 *location
= SYSTEM_VALUE_SAMPLE_ID
;
1130 set_mode_system_value(b
, mode
);
1132 case SpvBuiltInSamplePosition
:
1133 *location
= SYSTEM_VALUE_SAMPLE_POS
;
1134 set_mode_system_value(b
, mode
);
1136 case SpvBuiltInSampleMask
:
1137 if (*mode
== nir_var_shader_out
) {
1138 *location
= FRAG_RESULT_SAMPLE_MASK
;
1140 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1141 set_mode_system_value(b
, mode
);
1144 case SpvBuiltInFragDepth
:
1145 *location
= FRAG_RESULT_DEPTH
;
1146 vtn_assert(*mode
== nir_var_shader_out
);
1148 case SpvBuiltInHelperInvocation
:
1149 *location
= SYSTEM_VALUE_HELPER_INVOCATION
;
1150 set_mode_system_value(b
, mode
);
1152 case SpvBuiltInNumWorkgroups
:
1153 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1154 set_mode_system_value(b
, mode
);
1156 case SpvBuiltInWorkgroupSize
:
1157 *location
= SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
1158 set_mode_system_value(b
, mode
);
1160 case SpvBuiltInWorkgroupId
:
1161 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1162 set_mode_system_value(b
, mode
);
1164 case SpvBuiltInLocalInvocationId
:
1165 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1166 set_mode_system_value(b
, mode
);
1168 case SpvBuiltInLocalInvocationIndex
:
1169 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1170 set_mode_system_value(b
, mode
);
1172 case SpvBuiltInGlobalInvocationId
:
1173 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1174 set_mode_system_value(b
, mode
);
1176 case SpvBuiltInBaseVertex
:
1177 /* OpenGL gl_BaseVertex (SYSTEM_VALUE_BASE_VERTEX) is not the same
1178 * semantic as SPIR-V BaseVertex (SYSTEM_VALUE_FIRST_VERTEX).
1180 *location
= SYSTEM_VALUE_FIRST_VERTEX
;
1181 set_mode_system_value(b
, mode
);
1183 case SpvBuiltInBaseInstance
:
1184 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1185 set_mode_system_value(b
, mode
);
1187 case SpvBuiltInDrawIndex
:
1188 *location
= SYSTEM_VALUE_DRAW_ID
;
1189 set_mode_system_value(b
, mode
);
1191 case SpvBuiltInSubgroupSize
:
1192 *location
= SYSTEM_VALUE_SUBGROUP_SIZE
;
1193 set_mode_system_value(b
, mode
);
1195 case SpvBuiltInSubgroupId
:
1196 *location
= SYSTEM_VALUE_SUBGROUP_ID
;
1197 set_mode_system_value(b
, mode
);
1199 case SpvBuiltInSubgroupLocalInvocationId
:
1200 *location
= SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1201 set_mode_system_value(b
, mode
);
1203 case SpvBuiltInNumSubgroups
:
1204 *location
= SYSTEM_VALUE_NUM_SUBGROUPS
;
1205 set_mode_system_value(b
, mode
);
1207 case SpvBuiltInDeviceIndex
:
1208 *location
= SYSTEM_VALUE_DEVICE_INDEX
;
1209 set_mode_system_value(b
, mode
);
1211 case SpvBuiltInViewIndex
:
1212 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1213 set_mode_system_value(b
, mode
);
1215 case SpvBuiltInSubgroupEqMask
:
1216 *location
= SYSTEM_VALUE_SUBGROUP_EQ_MASK
,
1217 set_mode_system_value(b
, mode
);
1219 case SpvBuiltInSubgroupGeMask
:
1220 *location
= SYSTEM_VALUE_SUBGROUP_GE_MASK
,
1221 set_mode_system_value(b
, mode
);
1223 case SpvBuiltInSubgroupGtMask
:
1224 *location
= SYSTEM_VALUE_SUBGROUP_GT_MASK
,
1225 set_mode_system_value(b
, mode
);
1227 case SpvBuiltInSubgroupLeMask
:
1228 *location
= SYSTEM_VALUE_SUBGROUP_LE_MASK
,
1229 set_mode_system_value(b
, mode
);
1231 case SpvBuiltInSubgroupLtMask
:
1232 *location
= SYSTEM_VALUE_SUBGROUP_LT_MASK
,
1233 set_mode_system_value(b
, mode
);
1235 case SpvBuiltInFragStencilRefEXT
:
1236 *location
= FRAG_RESULT_STENCIL
;
1237 vtn_assert(*mode
== nir_var_shader_out
);
1239 case SpvBuiltInWorkDim
:
1240 *location
= SYSTEM_VALUE_WORK_DIM
;
1241 set_mode_system_value(b
, mode
);
1243 case SpvBuiltInGlobalSize
:
1244 *location
= SYSTEM_VALUE_GLOBAL_GROUP_SIZE
;
1245 set_mode_system_value(b
, mode
);
1248 vtn_fail("unsupported builtin: %u", builtin
);
1253 apply_var_decoration(struct vtn_builder
*b
,
1254 struct nir_variable_data
*var_data
,
1255 const struct vtn_decoration
*dec
)
1257 switch (dec
->decoration
) {
1258 case SpvDecorationRelaxedPrecision
:
1259 break; /* FIXME: Do nothing with this for now. */
1260 case SpvDecorationNoPerspective
:
1261 var_data
->interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1263 case SpvDecorationFlat
:
1264 var_data
->interpolation
= INTERP_MODE_FLAT
;
1266 case SpvDecorationCentroid
:
1267 var_data
->centroid
= true;
1269 case SpvDecorationSample
:
1270 var_data
->sample
= true;
1272 case SpvDecorationInvariant
:
1273 var_data
->invariant
= true;
1275 case SpvDecorationConstant
:
1276 var_data
->read_only
= true;
1278 case SpvDecorationNonReadable
:
1279 var_data
->image
.access
|= ACCESS_NON_READABLE
;
1281 case SpvDecorationNonWritable
:
1282 var_data
->read_only
= true;
1283 var_data
->image
.access
|= ACCESS_NON_WRITEABLE
;
1285 case SpvDecorationRestrict
:
1286 var_data
->image
.access
|= ACCESS_RESTRICT
;
1288 case SpvDecorationVolatile
:
1289 var_data
->image
.access
|= ACCESS_VOLATILE
;
1291 case SpvDecorationCoherent
:
1292 var_data
->image
.access
|= ACCESS_COHERENT
;
1294 case SpvDecorationComponent
:
1295 var_data
->location_frac
= dec
->literals
[0];
1297 case SpvDecorationIndex
:
1298 var_data
->index
= dec
->literals
[0];
1300 case SpvDecorationBuiltIn
: {
1301 SpvBuiltIn builtin
= dec
->literals
[0];
1303 nir_variable_mode mode
= var_data
->mode
;
1304 vtn_get_builtin_location(b
, builtin
, &var_data
->location
, &mode
);
1305 var_data
->mode
= mode
;
1308 case SpvBuiltInTessLevelOuter
:
1309 case SpvBuiltInTessLevelInner
:
1310 var_data
->compact
= true;
1312 case SpvBuiltInFragCoord
:
1313 var_data
->pixel_center_integer
= b
->pixel_center_integer
;
1315 case SpvBuiltInSamplePosition
:
1316 var_data
->origin_upper_left
= b
->origin_upper_left
;
1323 case SpvDecorationSpecId
:
1324 case SpvDecorationRowMajor
:
1325 case SpvDecorationColMajor
:
1326 case SpvDecorationMatrixStride
:
1327 case SpvDecorationAliased
:
1328 case SpvDecorationUniform
:
1329 case SpvDecorationLinkageAttributes
:
1330 break; /* Do nothing with these here */
1332 case SpvDecorationPatch
:
1333 var_data
->patch
= true;
1336 case SpvDecorationLocation
:
1337 vtn_fail("Handled above");
1339 case SpvDecorationBlock
:
1340 case SpvDecorationBufferBlock
:
1341 case SpvDecorationArrayStride
:
1342 case SpvDecorationGLSLShared
:
1343 case SpvDecorationGLSLPacked
:
1344 break; /* These can apply to a type but we don't care about them */
1346 case SpvDecorationBinding
:
1347 case SpvDecorationDescriptorSet
:
1348 case SpvDecorationNoContraction
:
1349 case SpvDecorationInputAttachmentIndex
:
1350 vtn_warn("Decoration not allowed for variable or structure member: %s",
1351 spirv_decoration_to_string(dec
->decoration
));
1354 case SpvDecorationXfbBuffer
:
1355 var_data
->explicit_xfb_buffer
= true;
1356 var_data
->xfb_buffer
= dec
->literals
[0];
1357 var_data
->always_active_io
= true;
1359 case SpvDecorationXfbStride
:
1360 var_data
->explicit_xfb_stride
= true;
1361 var_data
->xfb_stride
= dec
->literals
[0];
1363 case SpvDecorationOffset
:
1364 var_data
->explicit_offset
= true;
1365 var_data
->offset
= dec
->literals
[0];
1368 case SpvDecorationStream
:
1369 var_data
->stream
= dec
->literals
[0];
1372 case SpvDecorationCPacked
:
1373 case SpvDecorationSaturatedConversion
:
1374 case SpvDecorationFuncParamAttr
:
1375 case SpvDecorationFPRoundingMode
:
1376 case SpvDecorationFPFastMathMode
:
1377 case SpvDecorationAlignment
:
1378 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1379 spirv_decoration_to_string(dec
->decoration
));
1382 case SpvDecorationHlslSemanticGOOGLE
:
1383 /* HLSL semantic decorations can safely be ignored by the driver. */
1387 vtn_fail("Unhandled decoration");
1392 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1393 const struct vtn_decoration
*dec
, void *out_is_patch
)
1395 if (dec
->decoration
== SpvDecorationPatch
) {
1396 *((bool *) out_is_patch
) = true;
1401 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1402 const struct vtn_decoration
*dec
, void *void_var
)
1404 struct vtn_variable
*vtn_var
= void_var
;
1406 /* Handle decorations that apply to a vtn_variable as a whole */
1407 switch (dec
->decoration
) {
1408 case SpvDecorationBinding
:
1409 vtn_var
->binding
= dec
->literals
[0];
1410 vtn_var
->explicit_binding
= true;
1412 case SpvDecorationDescriptorSet
:
1413 vtn_var
->descriptor_set
= dec
->literals
[0];
1415 case SpvDecorationInputAttachmentIndex
:
1416 vtn_var
->input_attachment_index
= dec
->literals
[0];
1418 case SpvDecorationPatch
:
1419 vtn_var
->patch
= true;
1421 case SpvDecorationOffset
:
1422 vtn_var
->offset
= dec
->literals
[0];
1424 case SpvDecorationNonWritable
:
1425 vtn_var
->access
|= ACCESS_NON_WRITEABLE
;
1427 case SpvDecorationNonReadable
:
1428 vtn_var
->access
|= ACCESS_NON_READABLE
;
1430 case SpvDecorationVolatile
:
1431 vtn_var
->access
|= ACCESS_VOLATILE
;
1433 case SpvDecorationCoherent
:
1434 vtn_var
->access
|= ACCESS_COHERENT
;
1436 case SpvDecorationHlslCounterBufferGOOGLE
:
1437 /* HLSL semantic decorations can safely be ignored by the driver. */
1443 if (val
->value_type
== vtn_value_type_pointer
) {
1444 assert(val
->pointer
->var
== void_var
);
1445 assert(val
->pointer
->chain
== NULL
);
1446 assert(member
== -1);
1448 assert(val
->value_type
== vtn_value_type_type
);
1451 /* Location is odd. If applied to a split structure, we have to walk the
1452 * whole thing and accumulate the location. It's easier to handle as a
1455 if (dec
->decoration
== SpvDecorationLocation
) {
1456 unsigned location
= dec
->literals
[0];
1457 bool is_vertex_input
= false;
1458 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
&&
1459 vtn_var
->mode
== vtn_variable_mode_output
) {
1460 location
+= FRAG_RESULT_DATA0
;
1461 } else if (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
&&
1462 vtn_var
->mode
== vtn_variable_mode_input
) {
1463 is_vertex_input
= true;
1464 location
+= VERT_ATTRIB_GENERIC0
;
1465 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1466 vtn_var
->mode
== vtn_variable_mode_output
) {
1467 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1468 } else if (vtn_var
->mode
!= vtn_variable_mode_uniform
) {
1469 vtn_warn("Location must be on input, output, uniform, sampler or "
1474 if (vtn_var
->var
->num_members
== 0) {
1475 /* This handles the member and lone variable cases */
1476 vtn_var
->var
->data
.location
= location
;
1478 /* This handles the structure member case */
1479 assert(vtn_var
->var
->members
);
1480 for (unsigned i
= 0; i
< vtn_var
->var
->num_members
; i
++) {
1481 vtn_var
->var
->members
[i
].location
= location
;
1482 const struct glsl_type
*member_type
=
1483 glsl_get_struct_field(vtn_var
->var
->interface_type
, i
);
1484 location
+= glsl_count_attribute_slots(member_type
,
1491 if (vtn_var
->var
->num_members
== 0) {
1492 assert(member
== -1);
1493 apply_var_decoration(b
, &vtn_var
->var
->data
, dec
);
1494 } else if (member
>= 0) {
1495 /* Member decorations must come from a type */
1496 assert(val
->value_type
== vtn_value_type_type
);
1497 apply_var_decoration(b
, &vtn_var
->var
->members
[member
], dec
);
1500 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1501 for (unsigned i
= 0; i
< length
; i
++)
1502 apply_var_decoration(b
, &vtn_var
->var
->members
[i
], dec
);
1505 /* A few variables, those with external storage, have no actual
1506 * nir_variables associated with them. Fortunately, all decorations
1507 * we care about for those variables are on the type only.
1509 vtn_assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1510 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1511 vtn_var
->mode
== vtn_variable_mode_push_constant
||
1512 (vtn_var
->mode
== vtn_variable_mode_workgroup
&&
1513 b
->options
->lower_workgroup_access_to_offsets
));
1518 static enum vtn_variable_mode
1519 vtn_storage_class_to_mode(struct vtn_builder
*b
,
1520 SpvStorageClass
class,
1521 struct vtn_type
*interface_type
,
1522 nir_variable_mode
*nir_mode_out
)
1524 enum vtn_variable_mode mode
;
1525 nir_variable_mode nir_mode
;
1527 case SpvStorageClassUniform
:
1528 if (interface_type
->block
) {
1529 mode
= vtn_variable_mode_ubo
;
1531 } else if (interface_type
->buffer_block
) {
1532 mode
= vtn_variable_mode_ssbo
;
1535 /* Default-block uniforms, coming from gl_spirv */
1536 mode
= vtn_variable_mode_uniform
;
1537 nir_mode
= nir_var_uniform
;
1540 case SpvStorageClassStorageBuffer
:
1541 mode
= vtn_variable_mode_ssbo
;
1544 case SpvStorageClassUniformConstant
:
1545 mode
= vtn_variable_mode_uniform
;
1546 nir_mode
= nir_var_uniform
;
1548 case SpvStorageClassPushConstant
:
1549 mode
= vtn_variable_mode_push_constant
;
1550 nir_mode
= nir_var_uniform
;
1552 case SpvStorageClassInput
:
1553 mode
= vtn_variable_mode_input
;
1554 nir_mode
= nir_var_shader_in
;
1556 case SpvStorageClassOutput
:
1557 mode
= vtn_variable_mode_output
;
1558 nir_mode
= nir_var_shader_out
;
1560 case SpvStorageClassPrivate
:
1561 mode
= vtn_variable_mode_global
;
1562 nir_mode
= nir_var_global
;
1564 case SpvStorageClassFunction
:
1565 mode
= vtn_variable_mode_local
;
1566 nir_mode
= nir_var_local
;
1568 case SpvStorageClassWorkgroup
:
1569 mode
= vtn_variable_mode_workgroup
;
1570 nir_mode
= nir_var_shared
;
1572 case SpvStorageClassAtomicCounter
:
1573 mode
= vtn_variable_mode_uniform
;
1574 nir_mode
= nir_var_uniform
;
1576 case SpvStorageClassCrossWorkgroup
:
1577 case SpvStorageClassGeneric
:
1579 vtn_fail("Unhandled variable storage class");
1583 *nir_mode_out
= nir_mode
;
1589 vtn_pointer_to_ssa(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
1591 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
1592 /* This pointer needs to have a pointer type with actual storage */
1593 vtn_assert(ptr
->ptr_type
);
1594 vtn_assert(ptr
->ptr_type
->type
);
1597 /* If we don't have an offset then we must be a pointer to the variable
1600 vtn_assert(!ptr
->offset
&& !ptr
->block_index
);
1602 struct vtn_access_chain chain
= {
1605 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
1608 vtn_assert(ptr
->offset
);
1609 if (ptr
->block_index
) {
1610 vtn_assert(ptr
->mode
== vtn_variable_mode_ubo
||
1611 ptr
->mode
== vtn_variable_mode_ssbo
);
1612 return nir_vec2(&b
->nb
, ptr
->block_index
, ptr
->offset
);
1614 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
);
1618 return &vtn_pointer_to_deref(b
, ptr
)->dest
.ssa
;
1622 struct vtn_pointer
*
1623 vtn_pointer_from_ssa(struct vtn_builder
*b
, nir_ssa_def
*ssa
,
1624 struct vtn_type
*ptr_type
)
1626 vtn_assert(ssa
->num_components
<= 2 && ssa
->bit_size
== 32);
1627 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1629 struct vtn_type
*interface_type
= ptr_type
->deref
;
1630 while (interface_type
->base_type
== vtn_base_type_array
)
1631 interface_type
= interface_type
->array_element
;
1633 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
1634 nir_variable_mode nir_mode
;
1635 ptr
->mode
= vtn_storage_class_to_mode(b
, ptr_type
->storage_class
,
1636 interface_type
, &nir_mode
);
1637 ptr
->type
= ptr_type
->deref
;
1638 ptr
->ptr_type
= ptr_type
;
1640 if (ptr
->mode
== vtn_variable_mode_ubo
||
1641 ptr
->mode
== vtn_variable_mode_ssbo
) {
1642 /* This pointer type needs to have actual storage */
1643 vtn_assert(ptr_type
->type
);
1644 vtn_assert(ssa
->num_components
== 2);
1645 ptr
->block_index
= nir_channel(&b
->nb
, ssa
, 0);
1646 ptr
->offset
= nir_channel(&b
->nb
, ssa
, 1);
1647 } else if (ptr
->mode
== vtn_variable_mode_workgroup
||
1648 ptr
->mode
== vtn_variable_mode_push_constant
) {
1649 /* This pointer type needs to have actual storage */
1650 vtn_assert(ptr_type
->type
);
1651 vtn_assert(ssa
->num_components
== 1);
1652 ptr
->block_index
= NULL
;
1655 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
1656 ptr_type
->deref
->type
);
1663 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
1665 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
1668 if (var
->mode
== vtn_variable_mode_input
) {
1669 return stage
== MESA_SHADER_TESS_CTRL
||
1670 stage
== MESA_SHADER_TESS_EVAL
||
1671 stage
== MESA_SHADER_GEOMETRY
;
1674 if (var
->mode
== vtn_variable_mode_output
)
1675 return stage
== MESA_SHADER_TESS_CTRL
;
1681 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
1682 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
1683 nir_constant
*initializer
)
1685 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1686 struct vtn_type
*type
= ptr_type
->deref
;
1688 struct vtn_type
*without_array
= type
;
1689 while(glsl_type_is_array(without_array
->type
))
1690 without_array
= without_array
->array_element
;
1692 enum vtn_variable_mode mode
;
1693 nir_variable_mode nir_mode
;
1694 mode
= vtn_storage_class_to_mode(b
, storage_class
, without_array
, &nir_mode
);
1697 case vtn_variable_mode_ubo
:
1698 b
->shader
->info
.num_ubos
++;
1700 case vtn_variable_mode_ssbo
:
1701 b
->shader
->info
.num_ssbos
++;
1703 case vtn_variable_mode_uniform
:
1704 if (glsl_type_is_image(without_array
->type
))
1705 b
->shader
->info
.num_images
++;
1706 else if (glsl_type_is_sampler(without_array
->type
))
1707 b
->shader
->info
.num_textures
++;
1709 case vtn_variable_mode_push_constant
:
1710 b
->shader
->num_uniforms
= vtn_type_block_size(b
, type
);
1713 /* No tallying is needed */
1717 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
1721 vtn_assert(val
->value_type
== vtn_value_type_pointer
);
1722 val
->pointer
= vtn_pointer_for_variable(b
, var
, ptr_type
);
1724 switch (var
->mode
) {
1725 case vtn_variable_mode_local
:
1726 case vtn_variable_mode_global
:
1727 case vtn_variable_mode_uniform
:
1728 /* For these, we create the variable normally */
1729 var
->var
= rzalloc(b
->shader
, nir_variable
);
1730 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1732 /* Need to tweak the nir type here as at vtn_handle_type we don't have
1733 * the access to storage_class, that is the one that points us that is
1736 if (storage_class
== SpvStorageClassAtomicCounter
) {
1737 var
->var
->type
= repair_atomic_type(var
->type
->type
);
1739 var
->var
->type
= var
->type
->type
;
1741 var
->var
->data
.mode
= nir_mode
;
1742 var
->var
->data
.location
= -1;
1743 var
->var
->interface_type
= NULL
;
1746 case vtn_variable_mode_workgroup
:
1747 if (b
->options
->lower_workgroup_access_to_offsets
) {
1748 var
->shared_location
= -1;
1750 /* Create the variable normally */
1751 var
->var
= rzalloc(b
->shader
, nir_variable
);
1752 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1753 var
->var
->type
= var
->type
->type
;
1754 var
->var
->data
.mode
= nir_var_shared
;
1758 case vtn_variable_mode_input
:
1759 case vtn_variable_mode_output
: {
1760 /* In order to know whether or not we're a per-vertex inout, we need
1761 * the patch qualifier. This means walking the variable decorations
1762 * early before we actually create any variables. Not a big deal.
1764 * GLSLang really likes to place decorations in the most interior
1765 * thing it possibly can. In particular, if you have a struct, it
1766 * will place the patch decorations on the struct members. This
1767 * should be handled by the variable splitting below just fine.
1769 * If you have an array-of-struct, things get even more weird as it
1770 * will place the patch decorations on the struct even though it's
1771 * inside an array and some of the members being patch and others not
1772 * makes no sense whatsoever. Since the only sensible thing is for
1773 * it to be all or nothing, we'll call it patch if any of the members
1774 * are declared patch.
1777 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
1778 if (glsl_type_is_array(var
->type
->type
) &&
1779 glsl_type_is_struct(without_array
->type
)) {
1780 vtn_foreach_decoration(b
, vtn_value(b
, without_array
->id
,
1781 vtn_value_type_type
),
1782 var_is_patch_cb
, &var
->patch
);
1785 /* For inputs and outputs, we immediately split structures. This
1786 * is for a couple of reasons. For one, builtins may all come in
1787 * a struct and we really want those split out into separate
1788 * variables. For another, interpolation qualifiers can be
1789 * applied to members of the top-level struct ane we need to be
1790 * able to preserve that information.
1793 struct vtn_type
*interface_type
= var
->type
;
1794 if (is_per_vertex_inout(var
, b
->shader
->info
.stage
)) {
1795 /* In Geometry shaders (and some tessellation), inputs come
1796 * in per-vertex arrays. However, some builtins come in
1797 * non-per-vertex, hence the need for the is_array check. In
1798 * any case, there are no non-builtin arrays allowed so this
1799 * check should be sufficient.
1801 interface_type
= var
->type
->array_element
;
1804 var
->var
= rzalloc(b
->shader
, nir_variable
);
1805 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1806 var
->var
->type
= var
->type
->type
;
1807 var
->var
->interface_type
= interface_type
->type
;
1808 var
->var
->data
.mode
= nir_mode
;
1809 var
->var
->data
.patch
= var
->patch
;
1811 if (glsl_type_is_struct(interface_type
->type
)) {
1812 /* It's a struct. Set it up as per-member. */
1813 var
->var
->num_members
= glsl_get_length(interface_type
->type
);
1814 var
->var
->members
= rzalloc_array(var
->var
, struct nir_variable_data
,
1815 var
->var
->num_members
);
1817 for (unsigned i
= 0; i
< var
->var
->num_members
; i
++) {
1818 var
->var
->members
[i
].mode
= nir_mode
;
1819 var
->var
->members
[i
].patch
= var
->patch
;
1823 /* For inputs and outputs, we need to grab locations and builtin
1824 * information from the interface type.
1826 vtn_foreach_decoration(b
, vtn_value(b
, interface_type
->id
,
1827 vtn_value_type_type
),
1828 var_decoration_cb
, var
);
1832 case vtn_variable_mode_ubo
:
1833 case vtn_variable_mode_ssbo
:
1834 case vtn_variable_mode_push_constant
:
1835 /* These don't need actual variables. */
1840 var
->var
->constant_initializer
=
1841 nir_constant_clone(initializer
, var
->var
);
1844 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
1846 if (var
->mode
== vtn_variable_mode_uniform
) {
1847 /* XXX: We still need the binding information in the nir_variable
1848 * for these. We should fix that.
1850 var
->var
->data
.binding
= var
->binding
;
1851 var
->var
->data
.explicit_binding
= var
->explicit_binding
;
1852 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
1853 var
->var
->data
.index
= var
->input_attachment_index
;
1854 var
->var
->data
.offset
= var
->offset
;
1856 if (glsl_type_is_image(without_array
->type
))
1857 var
->var
->data
.image
.format
= without_array
->image_format
;
1860 if (var
->mode
== vtn_variable_mode_local
) {
1861 vtn_assert(var
->var
!= NULL
&& var
->var
->members
== NULL
);
1862 nir_function_impl_add_variable(b
->nb
.impl
, var
->var
);
1863 } else if (var
->var
) {
1864 nir_shader_add_variable(b
->shader
, var
->var
);
1866 vtn_assert(vtn_pointer_is_external_block(b
, val
->pointer
));
1871 vtn_assert_types_equal(struct vtn_builder
*b
, SpvOp opcode
,
1872 struct vtn_type
*dst_type
,
1873 struct vtn_type
*src_type
)
1875 if (dst_type
->id
== src_type
->id
)
1878 if (vtn_types_compatible(b
, dst_type
, src_type
)) {
1879 /* Early versions of GLSLang would re-emit types unnecessarily and you
1880 * would end up with OpLoad, OpStore, or OpCopyMemory opcodes which have
1881 * mismatched source and destination types.
1883 * https://github.com/KhronosGroup/glslang/issues/304
1884 * https://github.com/KhronosGroup/glslang/issues/307
1885 * https://bugs.freedesktop.org/show_bug.cgi?id=104338
1886 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
1888 vtn_warn("Source and destination types of %s do not have the same "
1889 "ID (but are compatible): %u vs %u",
1890 spirv_op_to_string(opcode
), dst_type
->id
, src_type
->id
);
1894 vtn_fail("Source and destination types of %s do not match: %s vs. %s",
1895 spirv_op_to_string(opcode
),
1896 glsl_get_type_name(dst_type
->type
),
1897 glsl_get_type_name(src_type
->type
));
1901 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
1902 const uint32_t *w
, unsigned count
)
1906 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1907 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1911 case SpvOpVariable
: {
1912 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1914 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1916 SpvStorageClass storage_class
= w
[3];
1917 nir_constant
*initializer
= NULL
;
1919 initializer
= vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
1921 vtn_create_variable(b
, val
, ptr_type
, storage_class
, initializer
);
1925 case SpvOpAccessChain
:
1926 case SpvOpPtrAccessChain
:
1927 case SpvOpInBoundsAccessChain
: {
1928 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
1929 chain
->ptr_as_array
= (opcode
== SpvOpPtrAccessChain
);
1932 for (int i
= 4; i
< count
; i
++) {
1933 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
1934 if (link_val
->value_type
== vtn_value_type_constant
) {
1935 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
1936 chain
->link
[idx
].id
= link_val
->constant
->values
[0].u32
[0];
1938 chain
->link
[idx
].mode
= vtn_access_mode_id
;
1939 chain
->link
[idx
].id
= w
[i
];
1945 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1946 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
1947 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1948 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1949 * to combine an array of images with a single sampler to get an
1950 * array of sampled images that all share the same sampler.
1951 * Fortunately, this means that we can more-or-less ignore the
1952 * sampler when crawling the access chain, but it does leave us
1953 * with this rather awkward little special-case.
1955 struct vtn_value
*val
=
1956 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1957 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1958 val
->sampled_image
->type
= base_val
->sampled_image
->type
;
1959 val
->sampled_image
->image
=
1960 vtn_pointer_dereference(b
, base_val
->sampled_image
->image
, chain
);
1961 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
1963 vtn_assert(base_val
->value_type
== vtn_value_type_pointer
);
1964 struct vtn_value
*val
=
1965 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1966 val
->pointer
= vtn_pointer_dereference(b
, base_val
->pointer
, chain
);
1967 val
->pointer
->ptr_type
= ptr_type
;
1972 case SpvOpCopyMemory
: {
1973 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
1974 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
1976 vtn_assert_types_equal(b
, opcode
, dest
->type
->deref
, src
->type
->deref
);
1978 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
1983 struct vtn_type
*res_type
=
1984 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1985 struct vtn_value
*src_val
= vtn_value(b
, w
[3], vtn_value_type_pointer
);
1986 struct vtn_pointer
*src
= src_val
->pointer
;
1988 vtn_assert_types_equal(b
, opcode
, res_type
, src_val
->type
->deref
);
1990 if (glsl_type_is_image(res_type
->type
) ||
1991 glsl_type_is_sampler(res_type
->type
)) {
1992 vtn_push_value(b
, w
[2], vtn_value_type_pointer
)->pointer
= src
;
1996 vtn_push_ssa(b
, w
[2], res_type
, vtn_variable_load(b
, src
));
2001 struct vtn_value
*dest_val
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2002 struct vtn_pointer
*dest
= dest_val
->pointer
;
2003 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[2]);
2005 /* OpStore requires us to actually have a storage type */
2006 vtn_fail_if(dest
->type
->type
== NULL
,
2007 "Invalid destination type for OpStore");
2009 if (glsl_get_base_type(dest
->type
->type
) == GLSL_TYPE_BOOL
&&
2010 glsl_get_base_type(src_val
->type
->type
) == GLSL_TYPE_UINT
) {
2011 /* Early versions of GLSLang would use uint types for UBOs/SSBOs but
2012 * would then store them to a local variable as bool. Work around
2013 * the issue by doing an implicit conversion.
2015 * https://github.com/KhronosGroup/glslang/issues/170
2016 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2018 vtn_warn("OpStore of value of type OpTypeInt to a pointer to type "
2019 "OpTypeBool. Doing an implicit conversion to work around "
2021 struct vtn_ssa_value
*bool_ssa
=
2022 vtn_create_ssa_value(b
, dest
->type
->type
);
2023 bool_ssa
->def
= nir_i2b(&b
->nb
, vtn_ssa_value(b
, w
[2])->def
);
2024 vtn_variable_store(b
, bool_ssa
, dest
);
2028 vtn_assert_types_equal(b
, opcode
, dest_val
->type
->deref
, src_val
->type
);
2030 if (glsl_type_is_sampler(dest
->type
->type
)) {
2031 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
2032 "propagation to workaround the problem.");
2033 vtn_assert(dest
->var
->copy_prop_sampler
== NULL
);
2034 dest
->var
->copy_prop_sampler
=
2035 vtn_value(b
, w
[2], vtn_value_type_pointer
)->pointer
;
2039 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
2040 vtn_variable_store(b
, src
, dest
);
2044 case SpvOpArrayLength
: {
2045 struct vtn_pointer
*ptr
=
2046 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2048 const uint32_t offset
= ptr
->var
->type
->offsets
[w
[4]];
2049 const uint32_t stride
= ptr
->var
->type
->members
[w
[4]]->stride
;
2051 if (!ptr
->block_index
) {
2052 struct vtn_access_chain chain
= {
2055 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
2056 vtn_assert(ptr
->block_index
);
2059 nir_intrinsic_instr
*instr
=
2060 nir_intrinsic_instr_create(b
->nb
.shader
,
2061 nir_intrinsic_get_buffer_size
);
2062 instr
->src
[0] = nir_src_for_ssa(ptr
->block_index
);
2063 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
2064 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2065 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
2067 /* array_length = max(buffer_size - offset, 0) / stride */
2068 nir_ssa_def
*array_length
=
2073 nir_imm_int(&b
->nb
, offset
)),
2074 nir_imm_int(&b
->nb
, 0u)),
2075 nir_imm_int(&b
->nb
, stride
));
2077 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2078 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
2079 val
->ssa
->def
= array_length
;
2083 case SpvOpCopyMemorySized
:
2085 vtn_fail("Unhandled opcode");