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
);
628 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
629 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
632 if (op
== nir_intrinsic_load_push_constant
) {
633 nir_intrinsic_set_base(instr
, access_offset
);
634 nir_intrinsic_set_range(instr
, access_size
);
637 if (op
== nir_intrinsic_load_ssbo
||
638 op
== nir_intrinsic_store_ssbo
) {
639 nir_intrinsic_set_access(instr
, access
);
643 instr
->src
[src
++] = nir_src_for_ssa(index
);
645 if (op
== nir_intrinsic_load_push_constant
) {
646 /* We need to subtract the offset from where the intrinsic will load the
649 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
650 nir_imm_int(&b
->nb
, access_offset
)));
652 instr
->src
[src
++] = nir_src_for_ssa(offset
);
656 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
657 instr
->num_components
,
658 glsl_get_bit_size(type
), NULL
);
659 (*inout
)->def
= &instr
->dest
.ssa
;
662 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
664 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
665 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
669 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
670 nir_ssa_def
*index
, nir_ssa_def
*offset
,
671 unsigned access_offset
, unsigned access_size
,
672 struct vtn_type
*type
, enum gl_access_qualifier access
,
673 struct vtn_ssa_value
**inout
)
675 if (load
&& *inout
== NULL
)
676 *inout
= vtn_create_ssa_value(b
, type
->type
);
678 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
682 case GLSL_TYPE_UINT16
:
683 case GLSL_TYPE_INT16
:
684 case GLSL_TYPE_UINT8
:
686 case GLSL_TYPE_UINT64
:
687 case GLSL_TYPE_INT64
:
688 case GLSL_TYPE_FLOAT
:
689 case GLSL_TYPE_FLOAT16
:
690 case GLSL_TYPE_DOUBLE
:
692 /* This is where things get interesting. At this point, we've hit
693 * a vector, a scalar, or a matrix.
695 if (glsl_type_is_matrix(type
->type
)) {
696 /* Loading the whole matrix */
697 struct vtn_ssa_value
*transpose
;
698 unsigned num_ops
, vec_width
, col_stride
;
699 if (type
->row_major
) {
700 num_ops
= glsl_get_vector_elements(type
->type
);
701 vec_width
= glsl_get_matrix_columns(type
->type
);
702 col_stride
= type
->array_element
->stride
;
704 const struct glsl_type
*transpose_type
=
705 glsl_matrix_type(base_type
, vec_width
, num_ops
);
706 *inout
= vtn_create_ssa_value(b
, transpose_type
);
708 transpose
= vtn_ssa_transpose(b
, *inout
);
712 num_ops
= glsl_get_matrix_columns(type
->type
);
713 vec_width
= glsl_get_vector_elements(type
->type
);
714 col_stride
= type
->stride
;
717 for (unsigned i
= 0; i
< num_ops
; i
++) {
718 nir_ssa_def
*elem_offset
=
719 nir_iadd_imm(&b
->nb
, offset
, i
* col_stride
);
720 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
721 access_offset
, access_size
,
723 glsl_vector_type(base_type
, vec_width
),
724 type
->access
| access
);
727 if (load
&& type
->row_major
)
728 *inout
= vtn_ssa_transpose(b
, *inout
);
730 unsigned elems
= glsl_get_vector_elements(type
->type
);
731 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
732 if (elems
== 1 || type
->stride
== type_size
) {
733 /* This is a tightly-packed normal scalar or vector load */
734 vtn_assert(glsl_type_is_vector_or_scalar(type
->type
));
735 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
736 access_offset
, access_size
,
738 type
->access
| access
);
740 /* This is a strided load. We have to load N things separately.
741 * This is the single column of a row-major matrix case.
743 vtn_assert(type
->stride
> type_size
);
744 vtn_assert(type
->stride
% type_size
== 0);
746 nir_ssa_def
*per_comp
[4];
747 for (unsigned i
= 0; i
< elems
; i
++) {
748 nir_ssa_def
*elem_offset
=
749 nir_iadd_imm(&b
->nb
, offset
, i
* type
->stride
);
750 struct vtn_ssa_value
*comp
, temp_val
;
752 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
753 temp_val
.type
= glsl_scalar_type(base_type
);
756 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
757 access_offset
, access_size
,
758 &comp
, glsl_scalar_type(base_type
),
759 type
->access
| access
);
760 per_comp
[i
] = comp
->def
;
765 *inout
= vtn_create_ssa_value(b
, type
->type
);
766 (*inout
)->def
= nir_vec(&b
->nb
, per_comp
, elems
);
772 case GLSL_TYPE_ARRAY
: {
773 unsigned elems
= glsl_get_length(type
->type
);
774 for (unsigned i
= 0; i
< elems
; i
++) {
775 nir_ssa_def
*elem_off
=
776 nir_iadd_imm(&b
->nb
, offset
, i
* type
->stride
);
777 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
778 access_offset
, access_size
,
780 type
->array_element
->access
| access
,
781 &(*inout
)->elems
[i
]);
786 case GLSL_TYPE_STRUCT
: {
787 unsigned elems
= glsl_get_length(type
->type
);
788 for (unsigned i
= 0; i
< elems
; i
++) {
789 nir_ssa_def
*elem_off
=
790 nir_iadd_imm(&b
->nb
, offset
, type
->offsets
[i
]);
791 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
792 access_offset
, access_size
,
794 type
->members
[i
]->access
| access
,
795 &(*inout
)->elems
[i
]);
801 vtn_fail("Invalid block member type");
805 static struct vtn_ssa_value
*
806 vtn_block_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
809 unsigned access_offset
= 0, access_size
= 0;
811 case vtn_variable_mode_ubo
:
812 op
= nir_intrinsic_load_ubo
;
814 case vtn_variable_mode_ssbo
:
815 op
= nir_intrinsic_load_ssbo
;
817 case vtn_variable_mode_push_constant
:
818 op
= nir_intrinsic_load_push_constant
;
819 access_size
= b
->shader
->num_uniforms
;
821 case vtn_variable_mode_workgroup
:
822 op
= nir_intrinsic_load_shared
;
825 vtn_fail("Invalid block variable mode");
828 nir_ssa_def
*offset
, *index
= NULL
;
829 offset
= vtn_pointer_to_offset(b
, src
, &index
);
831 struct vtn_ssa_value
*value
= NULL
;
832 _vtn_block_load_store(b
, op
, true, index
, offset
,
833 access_offset
, access_size
,
834 src
->type
, src
->access
, &value
);
839 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
840 struct vtn_pointer
*dst
)
844 case vtn_variable_mode_ssbo
:
845 op
= nir_intrinsic_store_ssbo
;
847 case vtn_variable_mode_workgroup
:
848 op
= nir_intrinsic_store_shared
;
851 vtn_fail("Invalid block variable mode");
854 nir_ssa_def
*offset
, *index
= NULL
;
855 offset
= vtn_pointer_to_offset(b
, dst
, &index
);
857 _vtn_block_load_store(b
, op
, false, index
, offset
,
858 0, 0, dst
->type
, dst
->access
, &src
);
862 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
863 struct vtn_pointer
*ptr
,
864 struct vtn_ssa_value
**inout
)
866 enum glsl_base_type base_type
= glsl_get_base_type(ptr
->type
->type
);
870 case GLSL_TYPE_UINT16
:
871 case GLSL_TYPE_INT16
:
872 case GLSL_TYPE_UINT8
:
874 case GLSL_TYPE_UINT64
:
875 case GLSL_TYPE_INT64
:
876 case GLSL_TYPE_FLOAT
:
877 case GLSL_TYPE_FLOAT16
:
879 case GLSL_TYPE_DOUBLE
:
880 /* At this point, we have a scalar, vector, or matrix so we know that
881 * there cannot be any structure splitting still in the way. By
882 * stopping at the matrix level rather than the vector level, we
883 * ensure that matrices get loaded in the optimal way even if they
884 * are storred row-major in a UBO.
887 *inout
= vtn_local_load(b
, vtn_pointer_to_deref(b
, ptr
));
889 vtn_local_store(b
, *inout
, vtn_pointer_to_deref(b
, ptr
));
893 case GLSL_TYPE_ARRAY
:
894 case GLSL_TYPE_STRUCT
: {
895 unsigned elems
= glsl_get_length(ptr
->type
->type
);
897 vtn_assert(*inout
== NULL
);
898 *inout
= rzalloc(b
, struct vtn_ssa_value
);
899 (*inout
)->type
= ptr
->type
->type
;
900 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
903 struct vtn_access_chain chain
= {
906 { .mode
= vtn_access_mode_literal
, },
909 for (unsigned i
= 0; i
< elems
; i
++) {
910 chain
.link
[0].id
= i
;
911 struct vtn_pointer
*elem
= vtn_pointer_dereference(b
, ptr
, &chain
);
912 _vtn_variable_load_store(b
, load
, elem
, &(*inout
)->elems
[i
]);
918 vtn_fail("Invalid access chain type");
922 struct vtn_ssa_value
*
923 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
925 if (vtn_pointer_is_external_block(b
, src
)) {
926 return vtn_block_load(b
, src
);
928 struct vtn_ssa_value
*val
= NULL
;
929 _vtn_variable_load_store(b
, true, src
, &val
);
935 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
936 struct vtn_pointer
*dest
)
938 if (vtn_pointer_is_external_block(b
, dest
)) {
939 vtn_assert(dest
->mode
== vtn_variable_mode_ssbo
||
940 dest
->mode
== vtn_variable_mode_workgroup
);
941 vtn_block_store(b
, src
, dest
);
943 _vtn_variable_load_store(b
, false, dest
, &src
);
948 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
949 struct vtn_pointer
*src
)
951 vtn_assert(src
->type
->type
== dest
->type
->type
);
952 enum glsl_base_type base_type
= glsl_get_base_type(src
->type
->type
);
956 case GLSL_TYPE_UINT16
:
957 case GLSL_TYPE_INT16
:
958 case GLSL_TYPE_UINT8
:
960 case GLSL_TYPE_UINT64
:
961 case GLSL_TYPE_INT64
:
962 case GLSL_TYPE_FLOAT
:
963 case GLSL_TYPE_FLOAT16
:
964 case GLSL_TYPE_DOUBLE
:
966 /* At this point, we have a scalar, vector, or matrix so we know that
967 * there cannot be any structure splitting still in the way. By
968 * stopping at the matrix level rather than the vector level, we
969 * ensure that matrices get loaded in the optimal way even if they
970 * are storred row-major in a UBO.
972 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
975 case GLSL_TYPE_ARRAY
:
976 case GLSL_TYPE_STRUCT
: {
977 struct vtn_access_chain chain
= {
980 { .mode
= vtn_access_mode_literal
, },
983 unsigned elems
= glsl_get_length(src
->type
->type
);
984 for (unsigned i
= 0; i
< elems
; i
++) {
985 chain
.link
[0].id
= i
;
986 struct vtn_pointer
*src_elem
=
987 vtn_pointer_dereference(b
, src
, &chain
);
988 struct vtn_pointer
*dest_elem
=
989 vtn_pointer_dereference(b
, dest
, &chain
);
991 _vtn_variable_copy(b
, dest_elem
, src_elem
);
997 vtn_fail("Invalid access chain type");
1002 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
1003 struct vtn_pointer
*src
)
1005 /* TODO: At some point, we should add a special-case for when we can
1006 * just emit a copy_var intrinsic.
1008 _vtn_variable_copy(b
, dest
, src
);
1012 set_mode_system_value(struct vtn_builder
*b
, nir_variable_mode
*mode
)
1014 vtn_assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
1015 *mode
= nir_var_system_value
;
1019 vtn_get_builtin_location(struct vtn_builder
*b
,
1020 SpvBuiltIn builtin
, int *location
,
1021 nir_variable_mode
*mode
)
1024 case SpvBuiltInPosition
:
1025 *location
= VARYING_SLOT_POS
;
1027 case SpvBuiltInPointSize
:
1028 *location
= VARYING_SLOT_PSIZ
;
1030 case SpvBuiltInClipDistance
:
1031 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
1033 case SpvBuiltInCullDistance
:
1034 *location
= VARYING_SLOT_CULL_DIST0
;
1036 case SpvBuiltInVertexId
:
1037 case SpvBuiltInVertexIndex
:
1038 /* The Vulkan spec defines VertexIndex to be non-zero-based and doesn't
1039 * allow VertexId. The ARB_gl_spirv spec defines VertexId to be the
1040 * same as gl_VertexID, which is non-zero-based, and removes
1041 * VertexIndex. Since they're both defined to be non-zero-based, we use
1042 * SYSTEM_VALUE_VERTEX_ID for both.
1044 *location
= SYSTEM_VALUE_VERTEX_ID
;
1045 set_mode_system_value(b
, mode
);
1047 case SpvBuiltInInstanceIndex
:
1048 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
1049 set_mode_system_value(b
, mode
);
1051 case SpvBuiltInInstanceId
:
1052 *location
= SYSTEM_VALUE_INSTANCE_ID
;
1053 set_mode_system_value(b
, mode
);
1055 case SpvBuiltInPrimitiveId
:
1056 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1057 vtn_assert(*mode
== nir_var_shader_in
);
1058 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1059 } else if (*mode
== nir_var_shader_out
) {
1060 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1062 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
1063 set_mode_system_value(b
, mode
);
1066 case SpvBuiltInInvocationId
:
1067 *location
= SYSTEM_VALUE_INVOCATION_ID
;
1068 set_mode_system_value(b
, mode
);
1070 case SpvBuiltInLayer
:
1071 *location
= VARYING_SLOT_LAYER
;
1072 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1073 *mode
= nir_var_shader_in
;
1074 else if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1075 *mode
= nir_var_shader_out
;
1076 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1077 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1078 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1079 *mode
= nir_var_shader_out
;
1081 vtn_fail("invalid stage for SpvBuiltInLayer");
1083 case SpvBuiltInViewportIndex
:
1084 *location
= VARYING_SLOT_VIEWPORT
;
1085 if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1086 *mode
= nir_var_shader_out
;
1087 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1088 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1089 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1090 *mode
= nir_var_shader_out
;
1091 else if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1092 *mode
= nir_var_shader_in
;
1094 vtn_fail("invalid stage for SpvBuiltInViewportIndex");
1096 case SpvBuiltInTessLevelOuter
:
1097 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
1099 case SpvBuiltInTessLevelInner
:
1100 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
1102 case SpvBuiltInTessCoord
:
1103 *location
= SYSTEM_VALUE_TESS_COORD
;
1104 set_mode_system_value(b
, mode
);
1106 case SpvBuiltInPatchVertices
:
1107 *location
= SYSTEM_VALUE_VERTICES_IN
;
1108 set_mode_system_value(b
, mode
);
1110 case SpvBuiltInFragCoord
:
1111 *location
= VARYING_SLOT_POS
;
1112 vtn_assert(*mode
== nir_var_shader_in
);
1114 case SpvBuiltInPointCoord
:
1115 *location
= VARYING_SLOT_PNTC
;
1116 vtn_assert(*mode
== nir_var_shader_in
);
1118 case SpvBuiltInFrontFacing
:
1119 *location
= SYSTEM_VALUE_FRONT_FACE
;
1120 set_mode_system_value(b
, mode
);
1122 case SpvBuiltInSampleId
:
1123 *location
= SYSTEM_VALUE_SAMPLE_ID
;
1124 set_mode_system_value(b
, mode
);
1126 case SpvBuiltInSamplePosition
:
1127 *location
= SYSTEM_VALUE_SAMPLE_POS
;
1128 set_mode_system_value(b
, mode
);
1130 case SpvBuiltInSampleMask
:
1131 if (*mode
== nir_var_shader_out
) {
1132 *location
= FRAG_RESULT_SAMPLE_MASK
;
1134 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1135 set_mode_system_value(b
, mode
);
1138 case SpvBuiltInFragDepth
:
1139 *location
= FRAG_RESULT_DEPTH
;
1140 vtn_assert(*mode
== nir_var_shader_out
);
1142 case SpvBuiltInHelperInvocation
:
1143 *location
= SYSTEM_VALUE_HELPER_INVOCATION
;
1144 set_mode_system_value(b
, mode
);
1146 case SpvBuiltInNumWorkgroups
:
1147 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1148 set_mode_system_value(b
, mode
);
1150 case SpvBuiltInWorkgroupSize
:
1151 *location
= SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
1152 set_mode_system_value(b
, mode
);
1154 case SpvBuiltInWorkgroupId
:
1155 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1156 set_mode_system_value(b
, mode
);
1158 case SpvBuiltInLocalInvocationId
:
1159 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1160 set_mode_system_value(b
, mode
);
1162 case SpvBuiltInLocalInvocationIndex
:
1163 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1164 set_mode_system_value(b
, mode
);
1166 case SpvBuiltInGlobalInvocationId
:
1167 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1168 set_mode_system_value(b
, mode
);
1170 case SpvBuiltInBaseVertex
:
1171 /* OpenGL gl_BaseVertex (SYSTEM_VALUE_BASE_VERTEX) is not the same
1172 * semantic as SPIR-V BaseVertex (SYSTEM_VALUE_FIRST_VERTEX).
1174 *location
= SYSTEM_VALUE_FIRST_VERTEX
;
1175 set_mode_system_value(b
, mode
);
1177 case SpvBuiltInBaseInstance
:
1178 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1179 set_mode_system_value(b
, mode
);
1181 case SpvBuiltInDrawIndex
:
1182 *location
= SYSTEM_VALUE_DRAW_ID
;
1183 set_mode_system_value(b
, mode
);
1185 case SpvBuiltInSubgroupSize
:
1186 *location
= SYSTEM_VALUE_SUBGROUP_SIZE
;
1187 set_mode_system_value(b
, mode
);
1189 case SpvBuiltInSubgroupId
:
1190 *location
= SYSTEM_VALUE_SUBGROUP_ID
;
1191 set_mode_system_value(b
, mode
);
1193 case SpvBuiltInSubgroupLocalInvocationId
:
1194 *location
= SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1195 set_mode_system_value(b
, mode
);
1197 case SpvBuiltInNumSubgroups
:
1198 *location
= SYSTEM_VALUE_NUM_SUBGROUPS
;
1199 set_mode_system_value(b
, mode
);
1201 case SpvBuiltInDeviceIndex
:
1202 *location
= SYSTEM_VALUE_DEVICE_INDEX
;
1203 set_mode_system_value(b
, mode
);
1205 case SpvBuiltInViewIndex
:
1206 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1207 set_mode_system_value(b
, mode
);
1209 case SpvBuiltInSubgroupEqMask
:
1210 *location
= SYSTEM_VALUE_SUBGROUP_EQ_MASK
,
1211 set_mode_system_value(b
, mode
);
1213 case SpvBuiltInSubgroupGeMask
:
1214 *location
= SYSTEM_VALUE_SUBGROUP_GE_MASK
,
1215 set_mode_system_value(b
, mode
);
1217 case SpvBuiltInSubgroupGtMask
:
1218 *location
= SYSTEM_VALUE_SUBGROUP_GT_MASK
,
1219 set_mode_system_value(b
, mode
);
1221 case SpvBuiltInSubgroupLeMask
:
1222 *location
= SYSTEM_VALUE_SUBGROUP_LE_MASK
,
1223 set_mode_system_value(b
, mode
);
1225 case SpvBuiltInSubgroupLtMask
:
1226 *location
= SYSTEM_VALUE_SUBGROUP_LT_MASK
,
1227 set_mode_system_value(b
, mode
);
1229 case SpvBuiltInFragStencilRefEXT
:
1230 *location
= FRAG_RESULT_STENCIL
;
1231 vtn_assert(*mode
== nir_var_shader_out
);
1233 case SpvBuiltInWorkDim
:
1234 *location
= SYSTEM_VALUE_WORK_DIM
;
1235 set_mode_system_value(b
, mode
);
1237 case SpvBuiltInGlobalSize
:
1238 *location
= SYSTEM_VALUE_GLOBAL_GROUP_SIZE
;
1239 set_mode_system_value(b
, mode
);
1242 vtn_fail("unsupported builtin: %u", builtin
);
1247 apply_var_decoration(struct vtn_builder
*b
,
1248 struct nir_variable_data
*var_data
,
1249 const struct vtn_decoration
*dec
)
1251 switch (dec
->decoration
) {
1252 case SpvDecorationRelaxedPrecision
:
1253 break; /* FIXME: Do nothing with this for now. */
1254 case SpvDecorationNoPerspective
:
1255 var_data
->interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1257 case SpvDecorationFlat
:
1258 var_data
->interpolation
= INTERP_MODE_FLAT
;
1260 case SpvDecorationCentroid
:
1261 var_data
->centroid
= true;
1263 case SpvDecorationSample
:
1264 var_data
->sample
= true;
1266 case SpvDecorationInvariant
:
1267 var_data
->invariant
= true;
1269 case SpvDecorationConstant
:
1270 var_data
->read_only
= true;
1272 case SpvDecorationNonReadable
:
1273 var_data
->image
.access
|= ACCESS_NON_READABLE
;
1275 case SpvDecorationNonWritable
:
1276 var_data
->read_only
= true;
1277 var_data
->image
.access
|= ACCESS_NON_WRITEABLE
;
1279 case SpvDecorationRestrict
:
1280 var_data
->image
.access
|= ACCESS_RESTRICT
;
1282 case SpvDecorationVolatile
:
1283 var_data
->image
.access
|= ACCESS_VOLATILE
;
1285 case SpvDecorationCoherent
:
1286 var_data
->image
.access
|= ACCESS_COHERENT
;
1288 case SpvDecorationComponent
:
1289 var_data
->location_frac
= dec
->literals
[0];
1291 case SpvDecorationIndex
:
1292 var_data
->index
= dec
->literals
[0];
1294 case SpvDecorationBuiltIn
: {
1295 SpvBuiltIn builtin
= dec
->literals
[0];
1297 nir_variable_mode mode
= var_data
->mode
;
1298 vtn_get_builtin_location(b
, builtin
, &var_data
->location
, &mode
);
1299 var_data
->mode
= mode
;
1302 case SpvBuiltInTessLevelOuter
:
1303 case SpvBuiltInTessLevelInner
:
1304 var_data
->compact
= true;
1306 case SpvBuiltInFragCoord
:
1307 var_data
->pixel_center_integer
= b
->pixel_center_integer
;
1309 case SpvBuiltInSamplePosition
:
1310 var_data
->origin_upper_left
= b
->origin_upper_left
;
1317 case SpvDecorationSpecId
:
1318 case SpvDecorationRowMajor
:
1319 case SpvDecorationColMajor
:
1320 case SpvDecorationMatrixStride
:
1321 case SpvDecorationAliased
:
1322 case SpvDecorationUniform
:
1323 case SpvDecorationLinkageAttributes
:
1324 break; /* Do nothing with these here */
1326 case SpvDecorationPatch
:
1327 var_data
->patch
= true;
1330 case SpvDecorationLocation
:
1331 vtn_fail("Handled above");
1333 case SpvDecorationBlock
:
1334 case SpvDecorationBufferBlock
:
1335 case SpvDecorationArrayStride
:
1336 case SpvDecorationGLSLShared
:
1337 case SpvDecorationGLSLPacked
:
1338 break; /* These can apply to a type but we don't care about them */
1340 case SpvDecorationBinding
:
1341 case SpvDecorationDescriptorSet
:
1342 case SpvDecorationNoContraction
:
1343 case SpvDecorationInputAttachmentIndex
:
1344 vtn_warn("Decoration not allowed for variable or structure member: %s",
1345 spirv_decoration_to_string(dec
->decoration
));
1348 case SpvDecorationXfbBuffer
:
1349 var_data
->explicit_xfb_buffer
= true;
1350 var_data
->xfb_buffer
= dec
->literals
[0];
1351 var_data
->always_active_io
= true;
1353 case SpvDecorationXfbStride
:
1354 var_data
->explicit_xfb_stride
= true;
1355 var_data
->xfb_stride
= dec
->literals
[0];
1357 case SpvDecorationOffset
:
1358 var_data
->explicit_offset
= true;
1359 var_data
->offset
= dec
->literals
[0];
1362 case SpvDecorationStream
:
1363 var_data
->stream
= dec
->literals
[0];
1366 case SpvDecorationCPacked
:
1367 case SpvDecorationSaturatedConversion
:
1368 case SpvDecorationFuncParamAttr
:
1369 case SpvDecorationFPRoundingMode
:
1370 case SpvDecorationFPFastMathMode
:
1371 case SpvDecorationAlignment
:
1372 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1373 spirv_decoration_to_string(dec
->decoration
));
1376 case SpvDecorationHlslSemanticGOOGLE
:
1377 /* HLSL semantic decorations can safely be ignored by the driver. */
1381 vtn_fail("Unhandled decoration");
1386 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1387 const struct vtn_decoration
*dec
, void *out_is_patch
)
1389 if (dec
->decoration
== SpvDecorationPatch
) {
1390 *((bool *) out_is_patch
) = true;
1395 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1396 const struct vtn_decoration
*dec
, void *void_var
)
1398 struct vtn_variable
*vtn_var
= void_var
;
1400 /* Handle decorations that apply to a vtn_variable as a whole */
1401 switch (dec
->decoration
) {
1402 case SpvDecorationBinding
:
1403 vtn_var
->binding
= dec
->literals
[0];
1404 vtn_var
->explicit_binding
= true;
1406 case SpvDecorationDescriptorSet
:
1407 vtn_var
->descriptor_set
= dec
->literals
[0];
1409 case SpvDecorationInputAttachmentIndex
:
1410 vtn_var
->input_attachment_index
= dec
->literals
[0];
1412 case SpvDecorationPatch
:
1413 vtn_var
->patch
= true;
1415 case SpvDecorationOffset
:
1416 vtn_var
->offset
= dec
->literals
[0];
1418 case SpvDecorationNonWritable
:
1419 vtn_var
->access
|= ACCESS_NON_WRITEABLE
;
1421 case SpvDecorationNonReadable
:
1422 vtn_var
->access
|= ACCESS_NON_READABLE
;
1424 case SpvDecorationVolatile
:
1425 vtn_var
->access
|= ACCESS_VOLATILE
;
1427 case SpvDecorationCoherent
:
1428 vtn_var
->access
|= ACCESS_COHERENT
;
1430 case SpvDecorationHlslCounterBufferGOOGLE
:
1431 /* HLSL semantic decorations can safely be ignored by the driver. */
1437 if (val
->value_type
== vtn_value_type_pointer
) {
1438 assert(val
->pointer
->var
== void_var
);
1439 assert(val
->pointer
->chain
== NULL
);
1440 assert(member
== -1);
1442 assert(val
->value_type
== vtn_value_type_type
);
1445 /* Location is odd. If applied to a split structure, we have to walk the
1446 * whole thing and accumulate the location. It's easier to handle as a
1449 if (dec
->decoration
== SpvDecorationLocation
) {
1450 unsigned location
= dec
->literals
[0];
1451 bool is_vertex_input
= false;
1452 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
&&
1453 vtn_var
->mode
== vtn_variable_mode_output
) {
1454 location
+= FRAG_RESULT_DATA0
;
1455 } else if (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
&&
1456 vtn_var
->mode
== vtn_variable_mode_input
) {
1457 is_vertex_input
= true;
1458 location
+= VERT_ATTRIB_GENERIC0
;
1459 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1460 vtn_var
->mode
== vtn_variable_mode_output
) {
1461 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1462 } else if (vtn_var
->mode
!= vtn_variable_mode_uniform
) {
1463 vtn_warn("Location must be on input, output, uniform, sampler or "
1468 if (vtn_var
->var
->num_members
== 0) {
1469 /* This handles the member and lone variable cases */
1470 vtn_var
->var
->data
.location
= location
;
1472 /* This handles the structure member case */
1473 assert(vtn_var
->var
->members
);
1474 for (unsigned i
= 0; i
< vtn_var
->var
->num_members
; i
++) {
1475 vtn_var
->var
->members
[i
].location
= location
;
1476 const struct glsl_type
*member_type
=
1477 glsl_get_struct_field(vtn_var
->var
->interface_type
, i
);
1478 location
+= glsl_count_attribute_slots(member_type
,
1485 if (vtn_var
->var
->num_members
== 0) {
1486 assert(member
== -1);
1487 apply_var_decoration(b
, &vtn_var
->var
->data
, dec
);
1488 } else if (member
>= 0) {
1489 /* Member decorations must come from a type */
1490 assert(val
->value_type
== vtn_value_type_type
);
1491 apply_var_decoration(b
, &vtn_var
->var
->members
[member
], dec
);
1494 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1495 for (unsigned i
= 0; i
< length
; i
++)
1496 apply_var_decoration(b
, &vtn_var
->var
->members
[i
], dec
);
1499 /* A few variables, those with external storage, have no actual
1500 * nir_variables associated with them. Fortunately, all decorations
1501 * we care about for those variables are on the type only.
1503 vtn_assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1504 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1505 vtn_var
->mode
== vtn_variable_mode_push_constant
||
1506 (vtn_var
->mode
== vtn_variable_mode_workgroup
&&
1507 b
->options
->lower_workgroup_access_to_offsets
));
1512 static enum vtn_variable_mode
1513 vtn_storage_class_to_mode(struct vtn_builder
*b
,
1514 SpvStorageClass
class,
1515 struct vtn_type
*interface_type
,
1516 nir_variable_mode
*nir_mode_out
)
1518 enum vtn_variable_mode mode
;
1519 nir_variable_mode nir_mode
;
1521 case SpvStorageClassUniform
:
1522 if (interface_type
->block
) {
1523 mode
= vtn_variable_mode_ubo
;
1525 } else if (interface_type
->buffer_block
) {
1526 mode
= vtn_variable_mode_ssbo
;
1529 /* Default-block uniforms, coming from gl_spirv */
1530 mode
= vtn_variable_mode_uniform
;
1531 nir_mode
= nir_var_uniform
;
1534 case SpvStorageClassStorageBuffer
:
1535 mode
= vtn_variable_mode_ssbo
;
1538 case SpvStorageClassUniformConstant
:
1539 mode
= vtn_variable_mode_uniform
;
1540 nir_mode
= nir_var_uniform
;
1542 case SpvStorageClassPushConstant
:
1543 mode
= vtn_variable_mode_push_constant
;
1544 nir_mode
= nir_var_uniform
;
1546 case SpvStorageClassInput
:
1547 mode
= vtn_variable_mode_input
;
1548 nir_mode
= nir_var_shader_in
;
1550 case SpvStorageClassOutput
:
1551 mode
= vtn_variable_mode_output
;
1552 nir_mode
= nir_var_shader_out
;
1554 case SpvStorageClassPrivate
:
1555 mode
= vtn_variable_mode_global
;
1556 nir_mode
= nir_var_global
;
1558 case SpvStorageClassFunction
:
1559 mode
= vtn_variable_mode_local
;
1560 nir_mode
= nir_var_local
;
1562 case SpvStorageClassWorkgroup
:
1563 mode
= vtn_variable_mode_workgroup
;
1564 nir_mode
= nir_var_shared
;
1566 case SpvStorageClassAtomicCounter
:
1567 mode
= vtn_variable_mode_uniform
;
1568 nir_mode
= nir_var_uniform
;
1570 case SpvStorageClassCrossWorkgroup
:
1571 case SpvStorageClassGeneric
:
1573 vtn_fail("Unhandled variable storage class");
1577 *nir_mode_out
= nir_mode
;
1583 vtn_pointer_to_ssa(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
1585 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
1586 /* This pointer needs to have a pointer type with actual storage */
1587 vtn_assert(ptr
->ptr_type
);
1588 vtn_assert(ptr
->ptr_type
->type
);
1591 /* If we don't have an offset then we must be a pointer to the variable
1594 vtn_assert(!ptr
->offset
&& !ptr
->block_index
);
1596 struct vtn_access_chain chain
= {
1599 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
1602 vtn_assert(ptr
->offset
);
1603 if (ptr
->block_index
) {
1604 vtn_assert(ptr
->mode
== vtn_variable_mode_ubo
||
1605 ptr
->mode
== vtn_variable_mode_ssbo
);
1606 return nir_vec2(&b
->nb
, ptr
->block_index
, ptr
->offset
);
1608 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
);
1612 return &vtn_pointer_to_deref(b
, ptr
)->dest
.ssa
;
1616 struct vtn_pointer
*
1617 vtn_pointer_from_ssa(struct vtn_builder
*b
, nir_ssa_def
*ssa
,
1618 struct vtn_type
*ptr_type
)
1620 vtn_assert(ssa
->num_components
<= 2 && ssa
->bit_size
== 32);
1621 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1623 struct vtn_type
*interface_type
= ptr_type
->deref
;
1624 while (interface_type
->base_type
== vtn_base_type_array
)
1625 interface_type
= interface_type
->array_element
;
1627 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
1628 nir_variable_mode nir_mode
;
1629 ptr
->mode
= vtn_storage_class_to_mode(b
, ptr_type
->storage_class
,
1630 interface_type
, &nir_mode
);
1631 ptr
->type
= ptr_type
->deref
;
1632 ptr
->ptr_type
= ptr_type
;
1634 if (ptr
->mode
== vtn_variable_mode_ubo
||
1635 ptr
->mode
== vtn_variable_mode_ssbo
) {
1636 /* This pointer type needs to have actual storage */
1637 vtn_assert(ptr_type
->type
);
1638 vtn_assert(ssa
->num_components
== 2);
1639 ptr
->block_index
= nir_channel(&b
->nb
, ssa
, 0);
1640 ptr
->offset
= nir_channel(&b
->nb
, ssa
, 1);
1641 } else if (ptr
->mode
== vtn_variable_mode_workgroup
||
1642 ptr
->mode
== vtn_variable_mode_push_constant
) {
1643 /* This pointer type needs to have actual storage */
1644 vtn_assert(ptr_type
->type
);
1645 vtn_assert(ssa
->num_components
== 1);
1646 ptr
->block_index
= NULL
;
1649 ptr
->deref
= nir_build_deref_cast(&b
->nb
, ssa
, nir_mode
,
1650 ptr_type
->deref
->type
);
1657 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
1659 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
1662 if (var
->mode
== vtn_variable_mode_input
) {
1663 return stage
== MESA_SHADER_TESS_CTRL
||
1664 stage
== MESA_SHADER_TESS_EVAL
||
1665 stage
== MESA_SHADER_GEOMETRY
;
1668 if (var
->mode
== vtn_variable_mode_output
)
1669 return stage
== MESA_SHADER_TESS_CTRL
;
1675 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
1676 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
1677 nir_constant
*initializer
)
1679 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1680 struct vtn_type
*type
= ptr_type
->deref
;
1682 struct vtn_type
*without_array
= type
;
1683 while(glsl_type_is_array(without_array
->type
))
1684 without_array
= without_array
->array_element
;
1686 enum vtn_variable_mode mode
;
1687 nir_variable_mode nir_mode
;
1688 mode
= vtn_storage_class_to_mode(b
, storage_class
, without_array
, &nir_mode
);
1691 case vtn_variable_mode_ubo
:
1692 b
->shader
->info
.num_ubos
++;
1694 case vtn_variable_mode_ssbo
:
1695 b
->shader
->info
.num_ssbos
++;
1697 case vtn_variable_mode_uniform
:
1698 if (glsl_type_is_image(without_array
->type
))
1699 b
->shader
->info
.num_images
++;
1700 else if (glsl_type_is_sampler(without_array
->type
))
1701 b
->shader
->info
.num_textures
++;
1703 case vtn_variable_mode_push_constant
:
1704 b
->shader
->num_uniforms
= vtn_type_block_size(b
, type
);
1707 /* No tallying is needed */
1711 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
1715 vtn_assert(val
->value_type
== vtn_value_type_pointer
);
1716 val
->pointer
= vtn_pointer_for_variable(b
, var
, ptr_type
);
1718 switch (var
->mode
) {
1719 case vtn_variable_mode_local
:
1720 case vtn_variable_mode_global
:
1721 case vtn_variable_mode_uniform
:
1722 /* For these, we create the variable normally */
1723 var
->var
= rzalloc(b
->shader
, nir_variable
);
1724 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1726 /* Need to tweak the nir type here as at vtn_handle_type we don't have
1727 * the access to storage_class, that is the one that points us that is
1730 if (storage_class
== SpvStorageClassAtomicCounter
) {
1731 var
->var
->type
= repair_atomic_type(var
->type
->type
);
1733 var
->var
->type
= var
->type
->type
;
1735 var
->var
->data
.mode
= nir_mode
;
1736 var
->var
->data
.location
= -1;
1737 var
->var
->interface_type
= NULL
;
1740 case vtn_variable_mode_workgroup
:
1741 if (b
->options
->lower_workgroup_access_to_offsets
) {
1742 var
->shared_location
= -1;
1744 /* Create the variable normally */
1745 var
->var
= rzalloc(b
->shader
, nir_variable
);
1746 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1747 var
->var
->type
= var
->type
->type
;
1748 var
->var
->data
.mode
= nir_var_shared
;
1752 case vtn_variable_mode_input
:
1753 case vtn_variable_mode_output
: {
1754 /* In order to know whether or not we're a per-vertex inout, we need
1755 * the patch qualifier. This means walking the variable decorations
1756 * early before we actually create any variables. Not a big deal.
1758 * GLSLang really likes to place decorations in the most interior
1759 * thing it possibly can. In particular, if you have a struct, it
1760 * will place the patch decorations on the struct members. This
1761 * should be handled by the variable splitting below just fine.
1763 * If you have an array-of-struct, things get even more weird as it
1764 * will place the patch decorations on the struct even though it's
1765 * inside an array and some of the members being patch and others not
1766 * makes no sense whatsoever. Since the only sensible thing is for
1767 * it to be all or nothing, we'll call it patch if any of the members
1768 * are declared patch.
1771 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
1772 if (glsl_type_is_array(var
->type
->type
) &&
1773 glsl_type_is_struct(without_array
->type
)) {
1774 vtn_foreach_decoration(b
, vtn_value(b
, without_array
->id
,
1775 vtn_value_type_type
),
1776 var_is_patch_cb
, &var
->patch
);
1779 /* For inputs and outputs, we immediately split structures. This
1780 * is for a couple of reasons. For one, builtins may all come in
1781 * a struct and we really want those split out into separate
1782 * variables. For another, interpolation qualifiers can be
1783 * applied to members of the top-level struct ane we need to be
1784 * able to preserve that information.
1787 struct vtn_type
*interface_type
= var
->type
;
1788 if (is_per_vertex_inout(var
, b
->shader
->info
.stage
)) {
1789 /* In Geometry shaders (and some tessellation), inputs come
1790 * in per-vertex arrays. However, some builtins come in
1791 * non-per-vertex, hence the need for the is_array check. In
1792 * any case, there are no non-builtin arrays allowed so this
1793 * check should be sufficient.
1795 interface_type
= var
->type
->array_element
;
1798 var
->var
= rzalloc(b
->shader
, nir_variable
);
1799 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1800 var
->var
->type
= var
->type
->type
;
1801 var
->var
->interface_type
= interface_type
->type
;
1802 var
->var
->data
.mode
= nir_mode
;
1803 var
->var
->data
.patch
= var
->patch
;
1805 if (glsl_type_is_struct(interface_type
->type
)) {
1806 /* It's a struct. Set it up as per-member. */
1807 var
->var
->num_members
= glsl_get_length(interface_type
->type
);
1808 var
->var
->members
= rzalloc_array(var
->var
, struct nir_variable_data
,
1809 var
->var
->num_members
);
1811 for (unsigned i
= 0; i
< var
->var
->num_members
; i
++) {
1812 var
->var
->members
[i
].mode
= nir_mode
;
1813 var
->var
->members
[i
].patch
= var
->patch
;
1817 /* For inputs and outputs, we need to grab locations and builtin
1818 * information from the interface type.
1820 vtn_foreach_decoration(b
, vtn_value(b
, interface_type
->id
,
1821 vtn_value_type_type
),
1822 var_decoration_cb
, var
);
1826 case vtn_variable_mode_ubo
:
1827 case vtn_variable_mode_ssbo
:
1828 case vtn_variable_mode_push_constant
:
1829 /* These don't need actual variables. */
1834 var
->var
->constant_initializer
=
1835 nir_constant_clone(initializer
, var
->var
);
1838 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
1840 if (var
->mode
== vtn_variable_mode_uniform
) {
1841 /* XXX: We still need the binding information in the nir_variable
1842 * for these. We should fix that.
1844 var
->var
->data
.binding
= var
->binding
;
1845 var
->var
->data
.explicit_binding
= var
->explicit_binding
;
1846 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
1847 var
->var
->data
.index
= var
->input_attachment_index
;
1848 var
->var
->data
.offset
= var
->offset
;
1850 if (glsl_type_is_image(without_array
->type
))
1851 var
->var
->data
.image
.format
= without_array
->image_format
;
1854 if (var
->mode
== vtn_variable_mode_local
) {
1855 vtn_assert(var
->var
!= NULL
&& var
->var
->members
== NULL
);
1856 nir_function_impl_add_variable(b
->nb
.impl
, var
->var
);
1857 } else if (var
->var
) {
1858 nir_shader_add_variable(b
->shader
, var
->var
);
1860 vtn_assert(vtn_pointer_is_external_block(b
, val
->pointer
));
1865 vtn_assert_types_equal(struct vtn_builder
*b
, SpvOp opcode
,
1866 struct vtn_type
*dst_type
,
1867 struct vtn_type
*src_type
)
1869 if (dst_type
->id
== src_type
->id
)
1872 if (vtn_types_compatible(b
, dst_type
, src_type
)) {
1873 /* Early versions of GLSLang would re-emit types unnecessarily and you
1874 * would end up with OpLoad, OpStore, or OpCopyMemory opcodes which have
1875 * mismatched source and destination types.
1877 * https://github.com/KhronosGroup/glslang/issues/304
1878 * https://github.com/KhronosGroup/glslang/issues/307
1879 * https://bugs.freedesktop.org/show_bug.cgi?id=104338
1880 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
1882 vtn_warn("Source and destination types of %s do not have the same "
1883 "ID (but are compatible): %u vs %u",
1884 spirv_op_to_string(opcode
), dst_type
->id
, src_type
->id
);
1888 vtn_fail("Source and destination types of %s do not match: %s vs. %s",
1889 spirv_op_to_string(opcode
),
1890 glsl_get_type_name(dst_type
->type
),
1891 glsl_get_type_name(src_type
->type
));
1895 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
1896 const uint32_t *w
, unsigned count
)
1900 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1901 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1905 case SpvOpVariable
: {
1906 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1908 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1910 SpvStorageClass storage_class
= w
[3];
1911 nir_constant
*initializer
= NULL
;
1913 initializer
= vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
1915 vtn_create_variable(b
, val
, ptr_type
, storage_class
, initializer
);
1919 case SpvOpAccessChain
:
1920 case SpvOpPtrAccessChain
:
1921 case SpvOpInBoundsAccessChain
: {
1922 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
1923 chain
->ptr_as_array
= (opcode
== SpvOpPtrAccessChain
);
1926 for (int i
= 4; i
< count
; i
++) {
1927 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
1928 if (link_val
->value_type
== vtn_value_type_constant
) {
1929 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
1930 chain
->link
[idx
].id
= link_val
->constant
->values
[0].u32
[0];
1932 chain
->link
[idx
].mode
= vtn_access_mode_id
;
1933 chain
->link
[idx
].id
= w
[i
];
1939 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1940 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
1941 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1942 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1943 * to combine an array of images with a single sampler to get an
1944 * array of sampled images that all share the same sampler.
1945 * Fortunately, this means that we can more-or-less ignore the
1946 * sampler when crawling the access chain, but it does leave us
1947 * with this rather awkward little special-case.
1949 struct vtn_value
*val
=
1950 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1951 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1952 val
->sampled_image
->type
= base_val
->sampled_image
->type
;
1953 val
->sampled_image
->image
=
1954 vtn_pointer_dereference(b
, base_val
->sampled_image
->image
, chain
);
1955 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
1957 vtn_assert(base_val
->value_type
== vtn_value_type_pointer
);
1958 struct vtn_value
*val
=
1959 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1960 val
->pointer
= vtn_pointer_dereference(b
, base_val
->pointer
, chain
);
1961 val
->pointer
->ptr_type
= ptr_type
;
1966 case SpvOpCopyMemory
: {
1967 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
1968 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
1970 vtn_assert_types_equal(b
, opcode
, dest
->type
->deref
, src
->type
->deref
);
1972 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
1977 struct vtn_type
*res_type
=
1978 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1979 struct vtn_value
*src_val
= vtn_value(b
, w
[3], vtn_value_type_pointer
);
1980 struct vtn_pointer
*src
= src_val
->pointer
;
1982 vtn_assert_types_equal(b
, opcode
, res_type
, src_val
->type
->deref
);
1984 if (glsl_type_is_image(res_type
->type
) ||
1985 glsl_type_is_sampler(res_type
->type
)) {
1986 vtn_push_value(b
, w
[2], vtn_value_type_pointer
)->pointer
= src
;
1990 vtn_push_ssa(b
, w
[2], res_type
, vtn_variable_load(b
, src
));
1995 struct vtn_value
*dest_val
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
1996 struct vtn_pointer
*dest
= dest_val
->pointer
;
1997 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[2]);
1999 /* OpStore requires us to actually have a storage type */
2000 vtn_fail_if(dest
->type
->type
== NULL
,
2001 "Invalid destination type for OpStore");
2003 if (glsl_get_base_type(dest
->type
->type
) == GLSL_TYPE_BOOL
&&
2004 glsl_get_base_type(src_val
->type
->type
) == GLSL_TYPE_UINT
) {
2005 /* Early versions of GLSLang would use uint types for UBOs/SSBOs but
2006 * would then store them to a local variable as bool. Work around
2007 * the issue by doing an implicit conversion.
2009 * https://github.com/KhronosGroup/glslang/issues/170
2010 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2012 vtn_warn("OpStore of value of type OpTypeInt to a pointer to type "
2013 "OpTypeBool. Doing an implicit conversion to work around "
2015 struct vtn_ssa_value
*bool_ssa
=
2016 vtn_create_ssa_value(b
, dest
->type
->type
);
2017 bool_ssa
->def
= nir_i2b(&b
->nb
, vtn_ssa_value(b
, w
[2])->def
);
2018 vtn_variable_store(b
, bool_ssa
, dest
);
2022 vtn_assert_types_equal(b
, opcode
, dest_val
->type
->deref
, src_val
->type
);
2024 if (glsl_type_is_sampler(dest
->type
->type
)) {
2025 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
2026 "propagation to workaround the problem.");
2027 vtn_assert(dest
->var
->copy_prop_sampler
== NULL
);
2028 dest
->var
->copy_prop_sampler
=
2029 vtn_value(b
, w
[2], vtn_value_type_pointer
)->pointer
;
2033 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
2034 vtn_variable_store(b
, src
, dest
);
2038 case SpvOpArrayLength
: {
2039 struct vtn_pointer
*ptr
=
2040 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2042 const uint32_t offset
= ptr
->var
->type
->offsets
[w
[4]];
2043 const uint32_t stride
= ptr
->var
->type
->members
[w
[4]]->stride
;
2045 if (!ptr
->block_index
) {
2046 struct vtn_access_chain chain
= {
2049 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
2050 vtn_assert(ptr
->block_index
);
2053 nir_intrinsic_instr
*instr
=
2054 nir_intrinsic_instr_create(b
->nb
.shader
,
2055 nir_intrinsic_get_buffer_size
);
2056 instr
->src
[0] = nir_src_for_ssa(ptr
->block_index
);
2057 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
2058 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2059 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
2061 /* array_length = max(buffer_size - offset, 0) / stride */
2062 nir_ssa_def
*array_length
=
2067 nir_imm_int(&b
->nb
, offset
)),
2068 nir_imm_int(&b
->nb
, 0u)),
2069 nir_imm_int(&b
->nb
, stride
));
2071 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2072 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
2073 val
->ssa
->def
= array_length
;
2077 case SpvOpCopyMemorySized
:
2079 vtn_fail("Unhandled opcode");