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
;
93 /* OpPtrAccessChain is only allowed on things which support variable
94 * pointers. For everything else, the client is expected to just pass us
95 * the right access chain.
97 vtn_assert(!deref_chain
->ptr_as_array
);
99 unsigned start
= base
->chain
? base
->chain
->length
: 0;
100 for (unsigned i
= 0; i
< deref_chain
->length
; i
++) {
101 chain
->link
[start
+ i
] = deref_chain
->link
[i
];
103 if (glsl_type_is_struct(type
->type
)) {
104 vtn_assert(deref_chain
->link
[i
].mode
== vtn_access_mode_literal
);
105 type
= type
->members
[deref_chain
->link
[i
].id
];
107 type
= type
->array_element
;
111 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
112 ptr
->mode
= base
->mode
;
114 ptr
->var
= base
->var
;
121 vtn_access_link_as_ssa(struct vtn_builder
*b
, struct vtn_access_link link
,
124 vtn_assert(stride
> 0);
125 if (link
.mode
== vtn_access_mode_literal
) {
126 return nir_imm_int(&b
->nb
, link
.id
* stride
);
127 } else if (stride
== 1) {
128 nir_ssa_def
*ssa
= vtn_ssa_value(b
, link
.id
)->def
;
129 if (ssa
->bit_size
!= 32)
130 ssa
= nir_u2u32(&b
->nb
, ssa
);
133 nir_ssa_def
*src0
= vtn_ssa_value(b
, link
.id
)->def
;
134 if (src0
->bit_size
!= 32)
135 src0
= nir_u2u32(&b
->nb
, src0
);
136 return nir_imul(&b
->nb
, src0
, nir_imm_int(&b
->nb
, stride
));
141 vtn_variable_resource_index(struct vtn_builder
*b
, struct vtn_variable
*var
,
142 nir_ssa_def
*desc_array_index
)
144 if (!desc_array_index
) {
145 vtn_assert(glsl_type_is_struct(var
->type
->type
));
146 desc_array_index
= nir_imm_int(&b
->nb
, 0);
149 nir_intrinsic_instr
*instr
=
150 nir_intrinsic_instr_create(b
->nb
.shader
,
151 nir_intrinsic_vulkan_resource_index
);
152 instr
->src
[0] = nir_src_for_ssa(desc_array_index
);
153 nir_intrinsic_set_desc_set(instr
, var
->descriptor_set
);
154 nir_intrinsic_set_binding(instr
, var
->binding
);
156 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
157 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
159 return &instr
->dest
.ssa
;
163 vtn_resource_reindex(struct vtn_builder
*b
, nir_ssa_def
*base_index
,
164 nir_ssa_def
*offset_index
)
166 nir_intrinsic_instr
*instr
=
167 nir_intrinsic_instr_create(b
->nb
.shader
,
168 nir_intrinsic_vulkan_resource_reindex
);
169 instr
->src
[0] = nir_src_for_ssa(base_index
);
170 instr
->src
[1] = nir_src_for_ssa(offset_index
);
172 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
173 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
175 return &instr
->dest
.ssa
;
178 static struct vtn_pointer
*
179 vtn_ssa_offset_pointer_dereference(struct vtn_builder
*b
,
180 struct vtn_pointer
*base
,
181 struct vtn_access_chain
*deref_chain
)
183 nir_ssa_def
*block_index
= base
->block_index
;
184 nir_ssa_def
*offset
= base
->offset
;
185 struct vtn_type
*type
= base
->type
;
188 if (base
->mode
== vtn_variable_mode_ubo
||
189 base
->mode
== vtn_variable_mode_ssbo
) {
191 vtn_assert(base
->var
&& base
->type
);
192 nir_ssa_def
*desc_arr_idx
;
193 if (glsl_type_is_array(type
->type
)) {
194 if (deref_chain
->length
>= 1) {
196 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1);
198 /* This consumes a level of type */
199 type
= type
->array_element
;
201 /* This is annoying. We've been asked for a pointer to the
202 * array of UBOs/SSBOs and not a specifc buffer. Return a
203 * pointer with a descriptor index of 0 and we'll have to do
204 * a reindex later to adjust it to the right thing.
206 desc_arr_idx
= nir_imm_int(&b
->nb
, 0);
208 } else if (deref_chain
->ptr_as_array
) {
209 /* You can't have a zero-length OpPtrAccessChain */
210 vtn_assert(deref_chain
->length
>= 1);
211 desc_arr_idx
= vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1);
213 /* We have a regular non-array SSBO. */
216 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
217 } else if (deref_chain
->ptr_as_array
&&
218 type
->base_type
== vtn_base_type_struct
&& type
->block
) {
219 /* We are doing an OpPtrAccessChain on a pointer to a struct that is
220 * decorated block. This is an interesting corner in the SPIR-V
221 * spec. One interpretation would be that they client is clearly
222 * trying to treat that block as if it's an implicit array of blocks
223 * repeated in the buffer. However, the SPIR-V spec for the
224 * OpPtrAccessChain says:
226 * "Base is treated as the address of the first element of an
227 * array, and the Element element’s address is computed to be the
228 * base for the Indexes, as per OpAccessChain."
230 * Taken literally, that would mean that your struct type is supposed
231 * to be treated as an array of such a struct and, since it's
232 * decorated block, that means an array of blocks which corresponds
233 * to an array descriptor. Therefore, we need to do a reindex
234 * operation to add the index from the first link in the access chain
235 * to the index we recieved.
237 * The downside to this interpretation (there always is one) is that
238 * this might be somewhat surprising behavior to apps if they expect
239 * the implicit array behavior described above.
241 vtn_assert(deref_chain
->length
>= 1);
242 nir_ssa_def
*offset_index
=
243 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1);
246 block_index
= vtn_resource_reindex(b
, block_index
, offset_index
);
251 if (base
->mode
== vtn_variable_mode_workgroup
) {
252 /* SLM doesn't need nor have a block index */
253 vtn_assert(!block_index
);
255 /* We need the variable for the base offset */
256 vtn_assert(base
->var
);
258 /* We need ptr_type for size and alignment */
259 vtn_assert(base
->ptr_type
);
261 /* Assign location on first use so that we don't end up bloating SLM
262 * address space for variables which are never statically used.
264 if (base
->var
->shared_location
< 0) {
265 vtn_assert(base
->ptr_type
->length
> 0 && base
->ptr_type
->align
> 0);
266 b
->shader
->num_shared
= vtn_align_u32(b
->shader
->num_shared
,
267 base
->ptr_type
->align
);
268 base
->var
->shared_location
= b
->shader
->num_shared
;
269 b
->shader
->num_shared
+= base
->ptr_type
->length
;
272 offset
= nir_imm_int(&b
->nb
, base
->var
->shared_location
);
273 } else if (base
->mode
== vtn_variable_mode_push_constant
) {
274 /* Push constants neither need nor have a block index */
275 vtn_assert(!block_index
);
277 /* Start off with at the start of the push constant block. */
278 offset
= nir_imm_int(&b
->nb
, 0);
280 /* The code above should have ensured a block_index when needed. */
281 vtn_assert(block_index
);
283 /* Start off with at the start of the buffer. */
284 offset
= nir_imm_int(&b
->nb
, 0);
288 if (deref_chain
->ptr_as_array
&& idx
== 0) {
289 /* We need ptr_type for the stride */
290 vtn_assert(base
->ptr_type
);
292 /* We need at least one element in the chain */
293 vtn_assert(deref_chain
->length
>= 1);
295 nir_ssa_def
*elem_offset
=
296 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
297 base
->ptr_type
->stride
);
298 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
302 for (; idx
< deref_chain
->length
; idx
++) {
303 switch (glsl_get_base_type(type
->type
)) {
306 case GLSL_TYPE_UINT16
:
307 case GLSL_TYPE_INT16
:
308 case GLSL_TYPE_UINT8
:
310 case GLSL_TYPE_UINT64
:
311 case GLSL_TYPE_INT64
:
312 case GLSL_TYPE_FLOAT
:
313 case GLSL_TYPE_FLOAT16
:
314 case GLSL_TYPE_DOUBLE
:
316 case GLSL_TYPE_ARRAY
: {
317 nir_ssa_def
*elem_offset
=
318 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
], type
->stride
);
319 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
320 type
= type
->array_element
;
324 case GLSL_TYPE_STRUCT
: {
325 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
326 unsigned member
= deref_chain
->link
[idx
].id
;
327 nir_ssa_def
*mem_offset
= nir_imm_int(&b
->nb
, type
->offsets
[member
]);
328 offset
= nir_iadd(&b
->nb
, offset
, mem_offset
);
329 type
= type
->members
[member
];
334 vtn_fail("Invalid type for deref");
338 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
339 ptr
->mode
= base
->mode
;
341 ptr
->block_index
= block_index
;
342 ptr
->offset
= offset
;
347 /* Dereference the given base pointer by the access chain */
348 static struct vtn_pointer
*
349 vtn_pointer_dereference(struct vtn_builder
*b
,
350 struct vtn_pointer
*base
,
351 struct vtn_access_chain
*deref_chain
)
353 if (vtn_pointer_uses_ssa_offset(b
, base
)) {
354 return vtn_ssa_offset_pointer_dereference(b
, base
, deref_chain
);
356 return vtn_access_chain_pointer_dereference(b
, base
, deref_chain
);
361 vtn_pointer_for_variable(struct vtn_builder
*b
,
362 struct vtn_variable
*var
, struct vtn_type
*ptr_type
)
364 struct vtn_pointer
*pointer
= rzalloc(b
, struct vtn_pointer
);
366 pointer
->mode
= var
->mode
;
367 pointer
->type
= var
->type
;
368 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
369 vtn_assert(ptr_type
->deref
->type
== var
->type
->type
);
370 pointer
->ptr_type
= ptr_type
;
377 vtn_pointer_to_deref(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
379 /* Do on-the-fly copy propagation for samplers. */
380 if (ptr
->var
->copy_prop_sampler
)
381 return vtn_pointer_to_deref(b
, ptr
->var
->copy_prop_sampler
);
383 nir_deref_instr
*deref_var
=
384 nir_deref_instr_create(b
->nb
.shader
, nir_deref_type_var
);
385 nir_ssa_dest_init(&deref_var
->instr
, &deref_var
->dest
, 1, 32, NULL
);
386 nir_builder_instr_insert(&b
->nb
, &deref_var
->instr
);
388 assert(ptr
->var
->var
);
389 deref_var
->mode
= ptr
->var
->var
->data
.mode
;
390 deref_var
->type
= ptr
->var
->var
->type
;
391 deref_var
->var
= ptr
->var
->var
;
392 /* Raw variable access */
396 struct vtn_access_chain
*chain
= ptr
->chain
;
399 struct vtn_type
*deref_type
= ptr
->var
->type
;
400 nir_deref_instr
*tail
= deref_var
;
402 for (unsigned i
= 0; i
< chain
->length
; i
++) {
403 enum glsl_base_type base_type
= glsl_get_base_type(deref_type
->type
);
407 case GLSL_TYPE_UINT16
:
408 case GLSL_TYPE_INT16
:
409 case GLSL_TYPE_UINT8
:
411 case GLSL_TYPE_UINT64
:
412 case GLSL_TYPE_INT64
:
413 case GLSL_TYPE_FLOAT
:
414 case GLSL_TYPE_FLOAT16
:
415 case GLSL_TYPE_DOUBLE
:
417 case GLSL_TYPE_ARRAY
: {
418 deref_type
= deref_type
->array_element
;
421 if (chain
->link
[i
].mode
== vtn_access_mode_literal
) {
422 index
= nir_imm_int(&b
->nb
, chain
->link
[i
].id
);
424 vtn_assert(chain
->link
[i
].mode
== vtn_access_mode_id
);
425 index
= vtn_ssa_value(b
, chain
->link
[i
].id
)->def
;
427 tail
= nir_build_deref_array(&b
->nb
, tail
, index
);
431 case GLSL_TYPE_STRUCT
: {
432 vtn_assert(chain
->link
[i
].mode
== vtn_access_mode_literal
);
433 unsigned idx
= chain
->link
[i
].id
;
434 deref_type
= deref_type
->members
[idx
];
435 tail
= nir_build_deref_struct(&b
->nb
, tail
, idx
);
439 vtn_fail("Invalid type for deref");
447 vtn_pointer_to_deref_var(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
449 return nir_deref_instr_to_deref(vtn_pointer_to_deref(b
, ptr
), b
);
453 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_instr
*deref
,
454 struct vtn_ssa_value
*inout
)
456 if (glsl_type_is_vector_or_scalar(deref
->type
)) {
458 inout
->def
= nir_load_deref(&b
->nb
, deref
);
460 nir_store_deref(&b
->nb
, deref
, inout
->def
, ~0);
462 } else if (glsl_type_is_array(deref
->type
) ||
463 glsl_type_is_matrix(deref
->type
)) {
464 unsigned elems
= glsl_get_length(deref
->type
);
465 for (unsigned i
= 0; i
< elems
; i
++) {
466 nir_deref_instr
*child
=
467 nir_build_deref_array(&b
->nb
, deref
, nir_imm_int(&b
->nb
, i
));
468 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
]);
471 vtn_assert(glsl_type_is_struct(deref
->type
));
472 unsigned elems
= glsl_get_length(deref
->type
);
473 for (unsigned i
= 0; i
< elems
; i
++) {
474 nir_deref_instr
*child
= nir_build_deref_struct(&b
->nb
, deref
, i
);
475 _vtn_local_load_store(b
, load
, child
, inout
->elems
[i
]);
481 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
483 struct vtn_pointer
*ptr
= vtn_value(b
, id
, vtn_value_type_pointer
)->pointer
;
484 return vtn_pointer_to_deref(b
, ptr
);
488 * Gets the NIR-level deref tail, which may have as a child an array deref
489 * selecting which component due to OpAccessChain supporting per-component
490 * indexing in SPIR-V.
492 static nir_deref_instr
*
493 get_deref_tail(nir_deref_instr
*deref
)
495 if (deref
->deref_type
!= nir_deref_type_array
)
498 nir_deref_instr
*parent
=
499 nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
);
501 if (glsl_type_is_vector(parent
->type
))
507 struct vtn_ssa_value
*
508 vtn_local_load(struct vtn_builder
*b
, nir_deref_instr
*src
)
510 nir_deref_instr
*src_tail
= get_deref_tail(src
);
511 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
512 _vtn_local_load_store(b
, true, src_tail
, val
);
514 if (src_tail
!= src
) {
515 val
->type
= src
->type
;
516 nir_const_value
*const_index
= nir_src_as_const_value(src
->arr
.index
);
518 val
->def
= vtn_vector_extract(b
, val
->def
, const_index
->u32
[0]);
520 val
->def
= vtn_vector_extract_dynamic(b
, val
->def
, src
->arr
.index
.ssa
);
527 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
528 nir_deref_instr
*dest
)
530 nir_deref_instr
*dest_tail
= get_deref_tail(dest
);
532 if (dest_tail
!= dest
) {
533 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
534 _vtn_local_load_store(b
, true, dest_tail
, val
);
536 nir_const_value
*const_index
= nir_src_as_const_value(dest
->arr
.index
);
538 val
->def
= vtn_vector_insert(b
, val
->def
, src
->def
,
539 const_index
->u32
[0]);
541 val
->def
= vtn_vector_insert_dynamic(b
, val
->def
, src
->def
,
542 dest
->arr
.index
.ssa
);
543 _vtn_local_load_store(b
, false, dest_tail
, val
);
545 _vtn_local_load_store(b
, false, dest_tail
, src
);
550 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
551 nir_ssa_def
**index_out
, unsigned *end_idx_out
)
553 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
555 struct vtn_access_chain chain
= {
558 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
560 *index_out
= ptr
->block_index
;
564 vtn_assert(ptr
->mode
== vtn_variable_mode_push_constant
);
568 struct vtn_type
*type
= ptr
->var
->type
;
569 nir_ssa_def
*offset
= nir_imm_int(&b
->nb
, 0);
572 for (; idx
< ptr
->chain
->length
; idx
++) {
573 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
577 case GLSL_TYPE_UINT16
:
578 case GLSL_TYPE_INT16
:
579 case GLSL_TYPE_UINT8
:
581 case GLSL_TYPE_UINT64
:
582 case GLSL_TYPE_INT64
:
583 case GLSL_TYPE_FLOAT
:
584 case GLSL_TYPE_FLOAT16
:
585 case GLSL_TYPE_DOUBLE
:
587 case GLSL_TYPE_ARRAY
:
588 offset
= nir_iadd(&b
->nb
, offset
,
589 vtn_access_link_as_ssa(b
, ptr
->chain
->link
[idx
],
592 type
= type
->array_element
;
595 case GLSL_TYPE_STRUCT
: {
596 vtn_assert(ptr
->chain
->link
[idx
].mode
== vtn_access_mode_literal
);
597 unsigned member
= ptr
->chain
->link
[idx
].id
;
598 offset
= nir_iadd(&b
->nb
, offset
,
599 nir_imm_int(&b
->nb
, type
->offsets
[member
]));
600 type
= type
->members
[member
];
605 vtn_fail("Invalid type for deref");
610 vtn_assert(type
== ptr
->type
);
617 /* Tries to compute the size of an interface block based on the strides and
618 * offsets that are provided to us in the SPIR-V source.
621 vtn_type_block_size(struct vtn_builder
*b
, struct vtn_type
*type
)
623 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
627 case GLSL_TYPE_UINT16
:
628 case GLSL_TYPE_INT16
:
629 case GLSL_TYPE_UINT8
:
631 case GLSL_TYPE_UINT64
:
632 case GLSL_TYPE_INT64
:
633 case GLSL_TYPE_FLOAT
:
634 case GLSL_TYPE_FLOAT16
:
636 case GLSL_TYPE_DOUBLE
: {
637 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
638 glsl_get_matrix_columns(type
->type
);
640 vtn_assert(type
->stride
> 0);
641 return type
->stride
* cols
;
643 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
644 return glsl_get_vector_elements(type
->type
) * type_size
;
648 case GLSL_TYPE_STRUCT
:
649 case GLSL_TYPE_INTERFACE
: {
651 unsigned num_fields
= glsl_get_length(type
->type
);
652 for (unsigned f
= 0; f
< num_fields
; f
++) {
653 unsigned field_end
= type
->offsets
[f
] +
654 vtn_type_block_size(b
, type
->members
[f
]);
655 size
= MAX2(size
, field_end
);
660 case GLSL_TYPE_ARRAY
:
661 vtn_assert(type
->stride
> 0);
662 vtn_assert(glsl_get_length(type
->type
) > 0);
663 return type
->stride
* glsl_get_length(type
->type
);
666 vtn_fail("Invalid block type");
672 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
673 nir_ssa_def
*index
, nir_ssa_def
*offset
,
674 unsigned access_offset
, unsigned access_size
,
675 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
)
677 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
678 instr
->num_components
= glsl_get_vector_elements(type
);
682 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
683 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
686 if (op
== nir_intrinsic_load_push_constant
) {
687 nir_intrinsic_set_base(instr
, access_offset
);
688 nir_intrinsic_set_range(instr
, access_size
);
692 instr
->src
[src
++] = nir_src_for_ssa(index
);
694 if (op
== nir_intrinsic_load_push_constant
) {
695 /* We need to subtract the offset from where the intrinsic will load the
698 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
699 nir_imm_int(&b
->nb
, access_offset
)));
701 instr
->src
[src
++] = nir_src_for_ssa(offset
);
705 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
706 instr
->num_components
,
707 glsl_get_bit_size(type
), NULL
);
708 (*inout
)->def
= &instr
->dest
.ssa
;
711 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
713 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
714 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
718 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
719 nir_ssa_def
*index
, nir_ssa_def
*offset
,
720 unsigned access_offset
, unsigned access_size
,
721 struct vtn_access_chain
*chain
, unsigned chain_idx
,
722 struct vtn_type
*type
, struct vtn_ssa_value
**inout
)
724 if (chain
&& chain_idx
>= chain
->length
)
727 if (load
&& chain
== NULL
&& *inout
== NULL
)
728 *inout
= vtn_create_ssa_value(b
, type
->type
);
730 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
734 case GLSL_TYPE_UINT16
:
735 case GLSL_TYPE_INT16
:
736 case GLSL_TYPE_UINT8
:
738 case GLSL_TYPE_UINT64
:
739 case GLSL_TYPE_INT64
:
740 case GLSL_TYPE_FLOAT
:
741 case GLSL_TYPE_FLOAT16
:
742 case GLSL_TYPE_DOUBLE
:
744 /* This is where things get interesting. At this point, we've hit
745 * a vector, a scalar, or a matrix.
747 if (glsl_type_is_matrix(type
->type
)) {
748 /* Loading the whole matrix */
749 struct vtn_ssa_value
*transpose
;
750 unsigned num_ops
, vec_width
, col_stride
;
751 if (type
->row_major
) {
752 num_ops
= glsl_get_vector_elements(type
->type
);
753 vec_width
= glsl_get_matrix_columns(type
->type
);
754 col_stride
= type
->array_element
->stride
;
756 const struct glsl_type
*transpose_type
=
757 glsl_matrix_type(base_type
, vec_width
, num_ops
);
758 *inout
= vtn_create_ssa_value(b
, transpose_type
);
760 transpose
= vtn_ssa_transpose(b
, *inout
);
764 num_ops
= glsl_get_matrix_columns(type
->type
);
765 vec_width
= glsl_get_vector_elements(type
->type
);
766 col_stride
= type
->stride
;
769 for (unsigned i
= 0; i
< num_ops
; i
++) {
770 nir_ssa_def
*elem_offset
=
771 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* col_stride
));
772 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
773 access_offset
, access_size
,
775 glsl_vector_type(base_type
, vec_width
));
778 if (load
&& type
->row_major
)
779 *inout
= vtn_ssa_transpose(b
, *inout
);
781 unsigned elems
= glsl_get_vector_elements(type
->type
);
782 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
783 if (elems
== 1 || type
->stride
== type_size
) {
784 /* This is a tightly-packed normal scalar or vector load */
785 vtn_assert(glsl_type_is_vector_or_scalar(type
->type
));
786 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
787 access_offset
, access_size
,
790 /* This is a strided load. We have to load N things separately.
791 * This is the single column of a row-major matrix case.
793 vtn_assert(type
->stride
> type_size
);
794 vtn_assert(type
->stride
% type_size
== 0);
796 nir_ssa_def
*per_comp
[4];
797 for (unsigned i
= 0; i
< elems
; i
++) {
798 nir_ssa_def
*elem_offset
=
799 nir_iadd(&b
->nb
, offset
,
800 nir_imm_int(&b
->nb
, i
* type
->stride
));
801 struct vtn_ssa_value
*comp
, temp_val
;
803 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
804 temp_val
.type
= glsl_scalar_type(base_type
);
807 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
808 access_offset
, access_size
,
809 &comp
, glsl_scalar_type(base_type
));
810 per_comp
[i
] = comp
->def
;
815 *inout
= vtn_create_ssa_value(b
, type
->type
);
816 (*inout
)->def
= nir_vec(&b
->nb
, per_comp
, elems
);
822 case GLSL_TYPE_ARRAY
: {
823 unsigned elems
= glsl_get_length(type
->type
);
824 for (unsigned i
= 0; i
< elems
; i
++) {
825 nir_ssa_def
*elem_off
=
826 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* type
->stride
));
827 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
828 access_offset
, access_size
,
830 type
->array_element
, &(*inout
)->elems
[i
]);
835 case GLSL_TYPE_STRUCT
: {
836 unsigned elems
= glsl_get_length(type
->type
);
837 for (unsigned i
= 0; i
< elems
; i
++) {
838 nir_ssa_def
*elem_off
=
839 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, type
->offsets
[i
]));
840 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
841 access_offset
, access_size
,
843 type
->members
[i
], &(*inout
)->elems
[i
]);
849 vtn_fail("Invalid block member type");
853 static struct vtn_ssa_value
*
854 vtn_block_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
857 unsigned access_offset
= 0, access_size
= 0;
859 case vtn_variable_mode_ubo
:
860 op
= nir_intrinsic_load_ubo
;
862 case vtn_variable_mode_ssbo
:
863 op
= nir_intrinsic_load_ssbo
;
865 case vtn_variable_mode_push_constant
:
866 op
= nir_intrinsic_load_push_constant
;
867 access_size
= b
->shader
->num_uniforms
;
869 case vtn_variable_mode_workgroup
:
870 op
= nir_intrinsic_load_shared
;
873 vtn_fail("Invalid block variable mode");
876 nir_ssa_def
*offset
, *index
= NULL
;
878 offset
= vtn_pointer_to_offset(b
, src
, &index
, &chain_idx
);
880 struct vtn_ssa_value
*value
= NULL
;
881 _vtn_block_load_store(b
, op
, true, index
, offset
,
882 access_offset
, access_size
,
883 src
->chain
, chain_idx
, src
->type
, &value
);
888 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
889 struct vtn_pointer
*dst
)
893 case vtn_variable_mode_ssbo
:
894 op
= nir_intrinsic_store_ssbo
;
896 case vtn_variable_mode_workgroup
:
897 op
= nir_intrinsic_store_shared
;
900 vtn_fail("Invalid block variable mode");
903 nir_ssa_def
*offset
, *index
= NULL
;
905 offset
= vtn_pointer_to_offset(b
, dst
, &index
, &chain_idx
);
907 _vtn_block_load_store(b
, op
, false, index
, offset
,
908 0, 0, dst
->chain
, chain_idx
, dst
->type
, &src
);
912 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
913 struct vtn_pointer
*ptr
,
914 struct vtn_ssa_value
**inout
)
916 enum glsl_base_type base_type
= glsl_get_base_type(ptr
->type
->type
);
920 case GLSL_TYPE_UINT16
:
921 case GLSL_TYPE_INT16
:
922 case GLSL_TYPE_UINT8
:
924 case GLSL_TYPE_UINT64
:
925 case GLSL_TYPE_INT64
:
926 case GLSL_TYPE_FLOAT
:
927 case GLSL_TYPE_FLOAT16
:
929 case GLSL_TYPE_DOUBLE
:
930 /* At this point, we have a scalar, vector, or matrix so we know that
931 * there cannot be any structure splitting still in the way. By
932 * stopping at the matrix level rather than the vector level, we
933 * ensure that matrices get loaded in the optimal way even if they
934 * are storred row-major in a UBO.
937 *inout
= vtn_local_load(b
, vtn_pointer_to_deref(b
, ptr
));
939 vtn_local_store(b
, *inout
, vtn_pointer_to_deref(b
, ptr
));
943 case GLSL_TYPE_ARRAY
:
944 case GLSL_TYPE_STRUCT
: {
945 unsigned elems
= glsl_get_length(ptr
->type
->type
);
947 vtn_assert(*inout
== NULL
);
948 *inout
= rzalloc(b
, struct vtn_ssa_value
);
949 (*inout
)->type
= ptr
->type
->type
;
950 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
953 struct vtn_access_chain chain
= {
956 { .mode
= vtn_access_mode_literal
, },
959 for (unsigned i
= 0; i
< elems
; i
++) {
960 chain
.link
[0].id
= i
;
961 struct vtn_pointer
*elem
= vtn_pointer_dereference(b
, ptr
, &chain
);
962 _vtn_variable_load_store(b
, load
, elem
, &(*inout
)->elems
[i
]);
968 vtn_fail("Invalid access chain type");
972 struct vtn_ssa_value
*
973 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
975 if (vtn_pointer_is_external_block(b
, src
)) {
976 return vtn_block_load(b
, src
);
978 struct vtn_ssa_value
*val
= NULL
;
979 _vtn_variable_load_store(b
, true, src
, &val
);
985 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
986 struct vtn_pointer
*dest
)
988 if (vtn_pointer_is_external_block(b
, dest
)) {
989 vtn_assert(dest
->mode
== vtn_variable_mode_ssbo
||
990 dest
->mode
== vtn_variable_mode_workgroup
);
991 vtn_block_store(b
, src
, dest
);
993 _vtn_variable_load_store(b
, false, dest
, &src
);
998 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
999 struct vtn_pointer
*src
)
1001 vtn_assert(src
->type
->type
== dest
->type
->type
);
1002 enum glsl_base_type base_type
= glsl_get_base_type(src
->type
->type
);
1003 switch (base_type
) {
1004 case GLSL_TYPE_UINT
:
1006 case GLSL_TYPE_UINT16
:
1007 case GLSL_TYPE_INT16
:
1008 case GLSL_TYPE_UINT8
:
1009 case GLSL_TYPE_INT8
:
1010 case GLSL_TYPE_UINT64
:
1011 case GLSL_TYPE_INT64
:
1012 case GLSL_TYPE_FLOAT
:
1013 case GLSL_TYPE_FLOAT16
:
1014 case GLSL_TYPE_DOUBLE
:
1015 case GLSL_TYPE_BOOL
:
1016 /* At this point, we have a scalar, vector, or matrix so we know that
1017 * there cannot be any structure splitting still in the way. By
1018 * stopping at the matrix level rather than the vector level, we
1019 * ensure that matrices get loaded in the optimal way even if they
1020 * are storred row-major in a UBO.
1022 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
1025 case GLSL_TYPE_ARRAY
:
1026 case GLSL_TYPE_STRUCT
: {
1027 struct vtn_access_chain chain
= {
1030 { .mode
= vtn_access_mode_literal
, },
1033 unsigned elems
= glsl_get_length(src
->type
->type
);
1034 for (unsigned i
= 0; i
< elems
; i
++) {
1035 chain
.link
[0].id
= i
;
1036 struct vtn_pointer
*src_elem
=
1037 vtn_pointer_dereference(b
, src
, &chain
);
1038 struct vtn_pointer
*dest_elem
=
1039 vtn_pointer_dereference(b
, dest
, &chain
);
1041 _vtn_variable_copy(b
, dest_elem
, src_elem
);
1047 vtn_fail("Invalid access chain type");
1052 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
1053 struct vtn_pointer
*src
)
1055 /* TODO: At some point, we should add a special-case for when we can
1056 * just emit a copy_var intrinsic.
1058 _vtn_variable_copy(b
, dest
, src
);
1062 set_mode_system_value(struct vtn_builder
*b
, nir_variable_mode
*mode
)
1064 vtn_assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
1065 *mode
= nir_var_system_value
;
1069 vtn_get_builtin_location(struct vtn_builder
*b
,
1070 SpvBuiltIn builtin
, int *location
,
1071 nir_variable_mode
*mode
)
1074 case SpvBuiltInPosition
:
1075 *location
= VARYING_SLOT_POS
;
1077 case SpvBuiltInPointSize
:
1078 *location
= VARYING_SLOT_PSIZ
;
1080 case SpvBuiltInClipDistance
:
1081 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
1083 case SpvBuiltInCullDistance
:
1084 *location
= VARYING_SLOT_CULL_DIST0
;
1086 case SpvBuiltInVertexIndex
:
1087 *location
= SYSTEM_VALUE_VERTEX_ID
;
1088 set_mode_system_value(b
, mode
);
1090 case SpvBuiltInVertexId
:
1091 /* Vulkan defines VertexID to be zero-based and reserves the new
1092 * builtin keyword VertexIndex to indicate the non-zero-based value.
1094 *location
= SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1095 set_mode_system_value(b
, mode
);
1097 case SpvBuiltInInstanceIndex
:
1098 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
1099 set_mode_system_value(b
, mode
);
1101 case SpvBuiltInInstanceId
:
1102 *location
= SYSTEM_VALUE_INSTANCE_ID
;
1103 set_mode_system_value(b
, mode
);
1105 case SpvBuiltInPrimitiveId
:
1106 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1107 vtn_assert(*mode
== nir_var_shader_in
);
1108 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1109 } else if (*mode
== nir_var_shader_out
) {
1110 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1112 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
1113 set_mode_system_value(b
, mode
);
1116 case SpvBuiltInInvocationId
:
1117 *location
= SYSTEM_VALUE_INVOCATION_ID
;
1118 set_mode_system_value(b
, mode
);
1120 case SpvBuiltInLayer
:
1121 *location
= VARYING_SLOT_LAYER
;
1122 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1123 *mode
= nir_var_shader_in
;
1124 else if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1125 *mode
= nir_var_shader_out
;
1126 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1127 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1128 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1129 *mode
= nir_var_shader_out
;
1131 vtn_fail("invalid stage for SpvBuiltInLayer");
1133 case SpvBuiltInViewportIndex
:
1134 *location
= VARYING_SLOT_VIEWPORT
;
1135 if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1136 *mode
= nir_var_shader_out
;
1137 else if (b
->options
&& b
->options
->caps
.shader_viewport_index_layer
&&
1138 (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
||
1139 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
))
1140 *mode
= nir_var_shader_out
;
1141 else if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1142 *mode
= nir_var_shader_in
;
1144 vtn_fail("invalid stage for SpvBuiltInViewportIndex");
1146 case SpvBuiltInTessLevelOuter
:
1147 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
1149 case SpvBuiltInTessLevelInner
:
1150 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
1152 case SpvBuiltInTessCoord
:
1153 *location
= SYSTEM_VALUE_TESS_COORD
;
1154 set_mode_system_value(b
, mode
);
1156 case SpvBuiltInPatchVertices
:
1157 *location
= SYSTEM_VALUE_VERTICES_IN
;
1158 set_mode_system_value(b
, mode
);
1160 case SpvBuiltInFragCoord
:
1161 *location
= VARYING_SLOT_POS
;
1162 vtn_assert(*mode
== nir_var_shader_in
);
1164 case SpvBuiltInPointCoord
:
1165 *location
= VARYING_SLOT_PNTC
;
1166 vtn_assert(*mode
== nir_var_shader_in
);
1168 case SpvBuiltInFrontFacing
:
1169 *location
= SYSTEM_VALUE_FRONT_FACE
;
1170 set_mode_system_value(b
, mode
);
1172 case SpvBuiltInSampleId
:
1173 *location
= SYSTEM_VALUE_SAMPLE_ID
;
1174 set_mode_system_value(b
, mode
);
1176 case SpvBuiltInSamplePosition
:
1177 *location
= SYSTEM_VALUE_SAMPLE_POS
;
1178 set_mode_system_value(b
, mode
);
1180 case SpvBuiltInSampleMask
:
1181 if (*mode
== nir_var_shader_out
) {
1182 *location
= FRAG_RESULT_SAMPLE_MASK
;
1184 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1185 set_mode_system_value(b
, mode
);
1188 case SpvBuiltInFragDepth
:
1189 *location
= FRAG_RESULT_DEPTH
;
1190 vtn_assert(*mode
== nir_var_shader_out
);
1192 case SpvBuiltInHelperInvocation
:
1193 *location
= SYSTEM_VALUE_HELPER_INVOCATION
;
1194 set_mode_system_value(b
, mode
);
1196 case SpvBuiltInNumWorkgroups
:
1197 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1198 set_mode_system_value(b
, mode
);
1200 case SpvBuiltInWorkgroupSize
:
1201 *location
= SYSTEM_VALUE_LOCAL_GROUP_SIZE
;
1202 set_mode_system_value(b
, mode
);
1204 case SpvBuiltInWorkgroupId
:
1205 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1206 set_mode_system_value(b
, mode
);
1208 case SpvBuiltInLocalInvocationId
:
1209 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1210 set_mode_system_value(b
, mode
);
1212 case SpvBuiltInLocalInvocationIndex
:
1213 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1214 set_mode_system_value(b
, mode
);
1216 case SpvBuiltInGlobalInvocationId
:
1217 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1218 set_mode_system_value(b
, mode
);
1220 case SpvBuiltInBaseVertex
:
1221 /* OpenGL gl_BaseVertex (SYSTEM_VALUE_BASE_VERTEX) is not the same
1222 * semantic as SPIR-V BaseVertex (SYSTEM_VALUE_FIRST_VERTEX).
1224 *location
= SYSTEM_VALUE_FIRST_VERTEX
;
1225 set_mode_system_value(b
, mode
);
1227 case SpvBuiltInBaseInstance
:
1228 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1229 set_mode_system_value(b
, mode
);
1231 case SpvBuiltInDrawIndex
:
1232 *location
= SYSTEM_VALUE_DRAW_ID
;
1233 set_mode_system_value(b
, mode
);
1235 case SpvBuiltInSubgroupSize
:
1236 *location
= SYSTEM_VALUE_SUBGROUP_SIZE
;
1237 set_mode_system_value(b
, mode
);
1239 case SpvBuiltInSubgroupId
:
1240 *location
= SYSTEM_VALUE_SUBGROUP_ID
;
1241 set_mode_system_value(b
, mode
);
1243 case SpvBuiltInSubgroupLocalInvocationId
:
1244 *location
= SYSTEM_VALUE_SUBGROUP_INVOCATION
;
1245 set_mode_system_value(b
, mode
);
1247 case SpvBuiltInNumSubgroups
:
1248 *location
= SYSTEM_VALUE_NUM_SUBGROUPS
;
1249 set_mode_system_value(b
, mode
);
1251 case SpvBuiltInDeviceIndex
:
1252 *location
= SYSTEM_VALUE_DEVICE_INDEX
;
1253 set_mode_system_value(b
, mode
);
1255 case SpvBuiltInViewIndex
:
1256 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1257 set_mode_system_value(b
, mode
);
1259 case SpvBuiltInSubgroupEqMask
:
1260 *location
= SYSTEM_VALUE_SUBGROUP_EQ_MASK
,
1261 set_mode_system_value(b
, mode
);
1263 case SpvBuiltInSubgroupGeMask
:
1264 *location
= SYSTEM_VALUE_SUBGROUP_GE_MASK
,
1265 set_mode_system_value(b
, mode
);
1267 case SpvBuiltInSubgroupGtMask
:
1268 *location
= SYSTEM_VALUE_SUBGROUP_GT_MASK
,
1269 set_mode_system_value(b
, mode
);
1271 case SpvBuiltInSubgroupLeMask
:
1272 *location
= SYSTEM_VALUE_SUBGROUP_LE_MASK
,
1273 set_mode_system_value(b
, mode
);
1275 case SpvBuiltInSubgroupLtMask
:
1276 *location
= SYSTEM_VALUE_SUBGROUP_LT_MASK
,
1277 set_mode_system_value(b
, mode
);
1279 case SpvBuiltInFragStencilRefEXT
:
1280 *location
= FRAG_RESULT_STENCIL
;
1281 vtn_assert(*mode
== nir_var_shader_out
);
1284 vtn_fail("unsupported builtin");
1289 apply_var_decoration(struct vtn_builder
*b
,
1290 struct nir_variable_data
*var_data
,
1291 const struct vtn_decoration
*dec
)
1293 switch (dec
->decoration
) {
1294 case SpvDecorationRelaxedPrecision
:
1295 break; /* FIXME: Do nothing with this for now. */
1296 case SpvDecorationNoPerspective
:
1297 var_data
->interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1299 case SpvDecorationFlat
:
1300 var_data
->interpolation
= INTERP_MODE_FLAT
;
1302 case SpvDecorationCentroid
:
1303 var_data
->centroid
= true;
1305 case SpvDecorationSample
:
1306 var_data
->sample
= true;
1308 case SpvDecorationInvariant
:
1309 var_data
->invariant
= true;
1311 case SpvDecorationConstant
:
1312 var_data
->read_only
= true;
1314 case SpvDecorationNonReadable
:
1315 var_data
->image
.write_only
= true;
1317 case SpvDecorationNonWritable
:
1318 var_data
->read_only
= true;
1319 var_data
->image
.read_only
= true;
1321 case SpvDecorationRestrict
:
1322 var_data
->image
.restrict_flag
= true;
1324 case SpvDecorationVolatile
:
1325 var_data
->image
._volatile
= true;
1327 case SpvDecorationCoherent
:
1328 var_data
->image
.coherent
= true;
1330 case SpvDecorationComponent
:
1331 var_data
->location_frac
= dec
->literals
[0];
1333 case SpvDecorationIndex
:
1334 var_data
->index
= dec
->literals
[0];
1336 case SpvDecorationBuiltIn
: {
1337 SpvBuiltIn builtin
= dec
->literals
[0];
1339 nir_variable_mode mode
= var_data
->mode
;
1340 vtn_get_builtin_location(b
, builtin
, &var_data
->location
, &mode
);
1341 var_data
->mode
= mode
;
1344 case SpvBuiltInTessLevelOuter
:
1345 case SpvBuiltInTessLevelInner
:
1346 var_data
->compact
= true;
1348 case SpvBuiltInFragCoord
:
1349 var_data
->pixel_center_integer
= b
->pixel_center_integer
;
1351 case SpvBuiltInSamplePosition
:
1352 var_data
->origin_upper_left
= b
->origin_upper_left
;
1359 case SpvDecorationSpecId
:
1360 case SpvDecorationRowMajor
:
1361 case SpvDecorationColMajor
:
1362 case SpvDecorationMatrixStride
:
1363 case SpvDecorationAliased
:
1364 case SpvDecorationUniform
:
1365 case SpvDecorationStream
:
1366 case SpvDecorationOffset
:
1367 case SpvDecorationLinkageAttributes
:
1368 break; /* Do nothing with these here */
1370 case SpvDecorationPatch
:
1371 var_data
->patch
= true;
1374 case SpvDecorationLocation
:
1375 vtn_fail("Handled above");
1377 case SpvDecorationBlock
:
1378 case SpvDecorationBufferBlock
:
1379 case SpvDecorationArrayStride
:
1380 case SpvDecorationGLSLShared
:
1381 case SpvDecorationGLSLPacked
:
1382 break; /* These can apply to a type but we don't care about them */
1384 case SpvDecorationBinding
:
1385 case SpvDecorationDescriptorSet
:
1386 case SpvDecorationNoContraction
:
1387 case SpvDecorationInputAttachmentIndex
:
1388 vtn_warn("Decoration not allowed for variable or structure member: %s",
1389 spirv_decoration_to_string(dec
->decoration
));
1392 case SpvDecorationXfbBuffer
:
1393 case SpvDecorationXfbStride
:
1394 vtn_warn("Vulkan does not have transform feedback: %s",
1395 spirv_decoration_to_string(dec
->decoration
));
1398 case SpvDecorationCPacked
:
1399 case SpvDecorationSaturatedConversion
:
1400 case SpvDecorationFuncParamAttr
:
1401 case SpvDecorationFPRoundingMode
:
1402 case SpvDecorationFPFastMathMode
:
1403 case SpvDecorationAlignment
:
1404 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1405 spirv_decoration_to_string(dec
->decoration
));
1409 vtn_fail("Unhandled decoration");
1414 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1415 const struct vtn_decoration
*dec
, void *out_is_patch
)
1417 if (dec
->decoration
== SpvDecorationPatch
) {
1418 *((bool *) out_is_patch
) = true;
1423 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1424 const struct vtn_decoration
*dec
, void *void_var
)
1426 struct vtn_variable
*vtn_var
= void_var
;
1428 /* Handle decorations that apply to a vtn_variable as a whole */
1429 switch (dec
->decoration
) {
1430 case SpvDecorationBinding
:
1431 vtn_var
->binding
= dec
->literals
[0];
1432 vtn_var
->explicit_binding
= true;
1434 case SpvDecorationDescriptorSet
:
1435 vtn_var
->descriptor_set
= dec
->literals
[0];
1437 case SpvDecorationInputAttachmentIndex
:
1438 vtn_var
->input_attachment_index
= dec
->literals
[0];
1440 case SpvDecorationPatch
:
1441 vtn_var
->patch
= true;
1447 if (val
->value_type
== vtn_value_type_pointer
) {
1448 assert(val
->pointer
->var
== void_var
);
1449 assert(val
->pointer
->chain
== NULL
);
1450 assert(member
== -1);
1452 assert(val
->value_type
== vtn_value_type_type
);
1455 /* Location is odd. If applied to a split structure, we have to walk the
1456 * whole thing and accumulate the location. It's easier to handle as a
1459 if (dec
->decoration
== SpvDecorationLocation
) {
1460 unsigned location
= dec
->literals
[0];
1461 bool is_vertex_input
;
1462 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
&&
1463 vtn_var
->mode
== vtn_variable_mode_output
) {
1464 is_vertex_input
= false;
1465 location
+= FRAG_RESULT_DATA0
;
1466 } else if (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
&&
1467 vtn_var
->mode
== vtn_variable_mode_input
) {
1468 is_vertex_input
= true;
1469 location
+= VERT_ATTRIB_GENERIC0
;
1470 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1471 vtn_var
->mode
== vtn_variable_mode_output
) {
1472 is_vertex_input
= false;
1473 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1474 } else if (vtn_var
->mode
!= vtn_variable_mode_uniform
) {
1475 vtn_warn("Location must be on input, output, uniform, sampler or "
1480 if (vtn_var
->var
->num_members
== 0) {
1481 /* This handles the member and lone variable cases */
1482 vtn_var
->var
->data
.location
= location
;
1484 /* This handles the structure member case */
1485 assert(vtn_var
->var
->members
);
1486 for (unsigned i
= 0; i
< vtn_var
->var
->num_members
; i
++) {
1487 vtn_var
->var
->members
[i
].location
= location
;
1488 const struct glsl_type
*member_type
=
1489 glsl_get_struct_field(vtn_var
->var
->interface_type
, i
);
1490 location
+= glsl_count_attribute_slots(member_type
,
1497 if (vtn_var
->var
->num_members
== 0) {
1498 assert(member
== -1);
1499 apply_var_decoration(b
, &vtn_var
->var
->data
, dec
);
1500 } else if (member
>= 0) {
1501 /* Member decorations must come from a type */
1502 assert(val
->value_type
== vtn_value_type_type
);
1503 apply_var_decoration(b
, &vtn_var
->var
->members
[member
], dec
);
1506 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1507 for (unsigned i
= 0; i
< length
; i
++)
1508 apply_var_decoration(b
, &vtn_var
->var
->members
[i
], dec
);
1511 /* A few variables, those with external storage, have no actual
1512 * nir_variables associated with them. Fortunately, all decorations
1513 * we care about for those variables are on the type only.
1515 vtn_assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1516 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1517 vtn_var
->mode
== vtn_variable_mode_push_constant
||
1518 (vtn_var
->mode
== vtn_variable_mode_workgroup
&&
1519 b
->options
->lower_workgroup_access_to_offsets
));
1524 static enum vtn_variable_mode
1525 vtn_storage_class_to_mode(struct vtn_builder
*b
,
1526 SpvStorageClass
class,
1527 struct vtn_type
*interface_type
,
1528 nir_variable_mode
*nir_mode_out
)
1530 enum vtn_variable_mode mode
;
1531 nir_variable_mode nir_mode
;
1533 case SpvStorageClassUniform
:
1534 if (interface_type
->block
) {
1535 mode
= vtn_variable_mode_ubo
;
1537 } else if (interface_type
->buffer_block
) {
1538 mode
= vtn_variable_mode_ssbo
;
1541 /* Default-block uniforms, coming from gl_spirv */
1542 mode
= vtn_variable_mode_uniform
;
1543 nir_mode
= nir_var_uniform
;
1546 case SpvStorageClassStorageBuffer
:
1547 mode
= vtn_variable_mode_ssbo
;
1550 case SpvStorageClassUniformConstant
:
1551 mode
= vtn_variable_mode_uniform
;
1552 nir_mode
= nir_var_uniform
;
1554 case SpvStorageClassPushConstant
:
1555 mode
= vtn_variable_mode_push_constant
;
1556 nir_mode
= nir_var_uniform
;
1558 case SpvStorageClassInput
:
1559 mode
= vtn_variable_mode_input
;
1560 nir_mode
= nir_var_shader_in
;
1562 case SpvStorageClassOutput
:
1563 mode
= vtn_variable_mode_output
;
1564 nir_mode
= nir_var_shader_out
;
1566 case SpvStorageClassPrivate
:
1567 mode
= vtn_variable_mode_global
;
1568 nir_mode
= nir_var_global
;
1570 case SpvStorageClassFunction
:
1571 mode
= vtn_variable_mode_local
;
1572 nir_mode
= nir_var_local
;
1574 case SpvStorageClassWorkgroup
:
1575 mode
= vtn_variable_mode_workgroup
;
1576 nir_mode
= nir_var_shared
;
1578 case SpvStorageClassCrossWorkgroup
:
1579 case SpvStorageClassGeneric
:
1580 case SpvStorageClassAtomicCounter
:
1582 vtn_fail("Unhandled variable storage class");
1586 *nir_mode_out
= nir_mode
;
1592 vtn_pointer_to_ssa(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
1594 /* This pointer needs to have a pointer type with actual storage */
1595 vtn_assert(ptr
->ptr_type
);
1596 vtn_assert(ptr
->ptr_type
->type
);
1599 /* If we don't have an offset then we must be a pointer to the variable
1602 vtn_assert(!ptr
->offset
&& !ptr
->block_index
);
1604 struct vtn_access_chain chain
= {
1607 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
1610 vtn_assert(ptr
->offset
);
1611 if (ptr
->block_index
) {
1612 vtn_assert(ptr
->mode
== vtn_variable_mode_ubo
||
1613 ptr
->mode
== vtn_variable_mode_ssbo
);
1614 return nir_vec2(&b
->nb
, ptr
->block_index
, ptr
->offset
);
1616 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
);
1621 struct vtn_pointer
*
1622 vtn_pointer_from_ssa(struct vtn_builder
*b
, nir_ssa_def
*ssa
,
1623 struct vtn_type
*ptr_type
)
1625 vtn_assert(ssa
->num_components
<= 2 && ssa
->bit_size
== 32);
1626 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1627 vtn_assert(ptr_type
->deref
->base_type
!= vtn_base_type_pointer
);
1628 /* This pointer type needs to have actual storage */
1629 vtn_assert(ptr_type
->type
);
1631 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
1632 ptr
->mode
= vtn_storage_class_to_mode(b
, ptr_type
->storage_class
,
1634 ptr
->type
= ptr_type
->deref
;
1635 ptr
->ptr_type
= ptr_type
;
1637 if (ssa
->num_components
> 1) {
1638 vtn_assert(ssa
->num_components
== 2);
1639 vtn_assert(ptr
->mode
== vtn_variable_mode_ubo
||
1640 ptr
->mode
== vtn_variable_mode_ssbo
);
1641 ptr
->block_index
= nir_channel(&b
->nb
, ssa
, 0);
1642 ptr
->offset
= nir_channel(&b
->nb
, ssa
, 1);
1644 vtn_assert(ssa
->num_components
== 1);
1645 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
||
1646 ptr
->mode
== vtn_variable_mode_push_constant
);
1647 ptr
->block_index
= NULL
;
1655 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
1657 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
1660 if (var
->mode
== vtn_variable_mode_input
) {
1661 return stage
== MESA_SHADER_TESS_CTRL
||
1662 stage
== MESA_SHADER_TESS_EVAL
||
1663 stage
== MESA_SHADER_GEOMETRY
;
1666 if (var
->mode
== vtn_variable_mode_output
)
1667 return stage
== MESA_SHADER_TESS_CTRL
;
1673 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
1674 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
1675 nir_constant
*initializer
)
1677 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1678 struct vtn_type
*type
= ptr_type
->deref
;
1680 struct vtn_type
*without_array
= type
;
1681 while(glsl_type_is_array(without_array
->type
))
1682 without_array
= without_array
->array_element
;
1684 enum vtn_variable_mode mode
;
1685 nir_variable_mode nir_mode
;
1686 mode
= vtn_storage_class_to_mode(b
, storage_class
, without_array
, &nir_mode
);
1689 case vtn_variable_mode_ubo
:
1690 b
->shader
->info
.num_ubos
++;
1692 case vtn_variable_mode_ssbo
:
1693 b
->shader
->info
.num_ssbos
++;
1695 case vtn_variable_mode_uniform
:
1696 if (glsl_type_is_image(without_array
->type
))
1697 b
->shader
->info
.num_images
++;
1698 else if (glsl_type_is_sampler(without_array
->type
))
1699 b
->shader
->info
.num_textures
++;
1701 case vtn_variable_mode_push_constant
:
1702 b
->shader
->num_uniforms
= vtn_type_block_size(b
, type
);
1705 /* No tallying is needed */
1709 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
1713 vtn_assert(val
->value_type
== vtn_value_type_pointer
);
1714 val
->pointer
= vtn_pointer_for_variable(b
, var
, ptr_type
);
1716 switch (var
->mode
) {
1717 case vtn_variable_mode_local
:
1718 case vtn_variable_mode_global
:
1719 case vtn_variable_mode_uniform
:
1720 /* For these, we create the variable normally */
1721 var
->var
= rzalloc(b
->shader
, nir_variable
);
1722 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1723 var
->var
->type
= var
->type
->type
;
1724 var
->var
->data
.mode
= nir_mode
;
1725 var
->var
->data
.location
= -1;
1726 var
->var
->interface_type
= NULL
;
1729 case vtn_variable_mode_workgroup
:
1730 if (b
->options
->lower_workgroup_access_to_offsets
) {
1731 var
->shared_location
= -1;
1733 /* Create the variable normally */
1734 var
->var
= rzalloc(b
->shader
, nir_variable
);
1735 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1736 var
->var
->type
= var
->type
->type
;
1737 var
->var
->data
.mode
= nir_var_shared
;
1741 case vtn_variable_mode_input
:
1742 case vtn_variable_mode_output
: {
1743 /* In order to know whether or not we're a per-vertex inout, we need
1744 * the patch qualifier. This means walking the variable decorations
1745 * early before we actually create any variables. Not a big deal.
1747 * GLSLang really likes to place decorations in the most interior
1748 * thing it possibly can. In particular, if you have a struct, it
1749 * will place the patch decorations on the struct members. This
1750 * should be handled by the variable splitting below just fine.
1752 * If you have an array-of-struct, things get even more weird as it
1753 * will place the patch decorations on the struct even though it's
1754 * inside an array and some of the members being patch and others not
1755 * makes no sense whatsoever. Since the only sensible thing is for
1756 * it to be all or nothing, we'll call it patch if any of the members
1757 * are declared patch.
1760 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
1761 if (glsl_type_is_array(var
->type
->type
) &&
1762 glsl_type_is_struct(without_array
->type
)) {
1763 vtn_foreach_decoration(b
, vtn_value(b
, without_array
->id
,
1764 vtn_value_type_type
),
1765 var_is_patch_cb
, &var
->patch
);
1768 /* For inputs and outputs, we immediately split structures. This
1769 * is for a couple of reasons. For one, builtins may all come in
1770 * a struct and we really want those split out into separate
1771 * variables. For another, interpolation qualifiers can be
1772 * applied to members of the top-level struct ane we need to be
1773 * able to preserve that information.
1776 struct vtn_type
*interface_type
= var
->type
;
1777 if (is_per_vertex_inout(var
, b
->shader
->info
.stage
)) {
1778 /* In Geometry shaders (and some tessellation), inputs come
1779 * in per-vertex arrays. However, some builtins come in
1780 * non-per-vertex, hence the need for the is_array check. In
1781 * any case, there are no non-builtin arrays allowed so this
1782 * check should be sufficient.
1784 interface_type
= var
->type
->array_element
;
1787 var
->var
= rzalloc(b
->shader
, nir_variable
);
1788 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1789 var
->var
->type
= var
->type
->type
;
1790 var
->var
->interface_type
= interface_type
->type
;
1791 var
->var
->data
.mode
= nir_mode
;
1792 var
->var
->data
.patch
= var
->patch
;
1794 if (glsl_type_is_struct(interface_type
->type
)) {
1795 /* It's a struct. Set it up as per-member. */
1796 var
->var
->num_members
= glsl_get_length(interface_type
->type
);
1797 var
->var
->members
= rzalloc_array(var
->var
, struct nir_variable_data
,
1798 var
->var
->num_members
);
1800 for (unsigned i
= 0; i
< var
->var
->num_members
; i
++) {
1801 var
->var
->members
[i
].mode
= nir_mode
;
1802 var
->var
->members
[i
].patch
= var
->patch
;
1806 /* For inputs and outputs, we need to grab locations and builtin
1807 * information from the interface type.
1809 vtn_foreach_decoration(b
, vtn_value(b
, interface_type
->id
,
1810 vtn_value_type_type
),
1811 var_decoration_cb
, var
);
1815 case vtn_variable_mode_param
:
1816 vtn_fail("Not created through OpVariable");
1818 case vtn_variable_mode_ubo
:
1819 case vtn_variable_mode_ssbo
:
1820 case vtn_variable_mode_push_constant
:
1821 /* These don't need actual variables. */
1826 var
->var
->constant_initializer
=
1827 nir_constant_clone(initializer
, var
->var
);
1830 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
1832 if (var
->mode
== vtn_variable_mode_uniform
) {
1833 /* XXX: We still need the binding information in the nir_variable
1834 * for these. We should fix that.
1836 var
->var
->data
.binding
= var
->binding
;
1837 var
->var
->data
.explicit_binding
= var
->explicit_binding
;
1838 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
1839 var
->var
->data
.index
= var
->input_attachment_index
;
1841 if (glsl_type_is_image(without_array
->type
))
1842 var
->var
->data
.image
.format
= without_array
->image_format
;
1845 if (var
->mode
== vtn_variable_mode_local
) {
1846 vtn_assert(var
->var
!= NULL
&& var
->var
->members
== NULL
);
1847 nir_function_impl_add_variable(b
->nb
.impl
, var
->var
);
1848 } else if (var
->var
) {
1849 nir_shader_add_variable(b
->shader
, var
->var
);
1851 vtn_assert(vtn_pointer_is_external_block(b
, val
->pointer
));
1856 vtn_assert_types_equal(struct vtn_builder
*b
, SpvOp opcode
,
1857 struct vtn_type
*dst_type
,
1858 struct vtn_type
*src_type
)
1860 if (dst_type
->id
== src_type
->id
)
1863 if (vtn_types_compatible(b
, dst_type
, src_type
)) {
1864 /* Early versions of GLSLang would re-emit types unnecessarily and you
1865 * would end up with OpLoad, OpStore, or OpCopyMemory opcodes which have
1866 * mismatched source and destination types.
1868 * https://github.com/KhronosGroup/glslang/issues/304
1869 * https://github.com/KhronosGroup/glslang/issues/307
1870 * https://bugs.freedesktop.org/show_bug.cgi?id=104338
1871 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
1873 vtn_warn("Source and destination types of %s do not have the same "
1874 "ID (but are compatible): %u vs %u",
1875 spirv_op_to_string(opcode
), dst_type
->id
, src_type
->id
);
1879 vtn_fail("Source and destination types of %s do not match: %s vs. %s",
1880 spirv_op_to_string(opcode
),
1881 glsl_get_type_name(dst_type
->type
),
1882 glsl_get_type_name(src_type
->type
));
1886 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
1887 const uint32_t *w
, unsigned count
)
1891 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1892 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1896 case SpvOpVariable
: {
1897 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1899 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1901 SpvStorageClass storage_class
= w
[3];
1902 nir_constant
*initializer
= NULL
;
1904 initializer
= vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
1906 vtn_create_variable(b
, val
, ptr_type
, storage_class
, initializer
);
1910 case SpvOpAccessChain
:
1911 case SpvOpPtrAccessChain
:
1912 case SpvOpInBoundsAccessChain
: {
1913 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
1914 chain
->ptr_as_array
= (opcode
== SpvOpPtrAccessChain
);
1917 for (int i
= 4; i
< count
; i
++) {
1918 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
1919 if (link_val
->value_type
== vtn_value_type_constant
) {
1920 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
1921 chain
->link
[idx
].id
= link_val
->constant
->values
[0].u32
[0];
1923 chain
->link
[idx
].mode
= vtn_access_mode_id
;
1924 chain
->link
[idx
].id
= w
[i
];
1930 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1931 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
1932 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1933 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1934 * to combine an array of images with a single sampler to get an
1935 * array of sampled images that all share the same sampler.
1936 * Fortunately, this means that we can more-or-less ignore the
1937 * sampler when crawling the access chain, but it does leave us
1938 * with this rather awkward little special-case.
1940 struct vtn_value
*val
=
1941 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1942 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1943 val
->sampled_image
->type
= base_val
->sampled_image
->type
;
1944 val
->sampled_image
->image
=
1945 vtn_pointer_dereference(b
, base_val
->sampled_image
->image
, chain
);
1946 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
1948 vtn_assert(base_val
->value_type
== vtn_value_type_pointer
);
1949 struct vtn_value
*val
=
1950 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1951 val
->pointer
= vtn_pointer_dereference(b
, base_val
->pointer
, chain
);
1952 val
->pointer
->ptr_type
= ptr_type
;
1957 case SpvOpCopyMemory
: {
1958 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
1959 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
1961 vtn_assert_types_equal(b
, opcode
, dest
->type
->deref
, src
->type
->deref
);
1963 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
1968 struct vtn_type
*res_type
=
1969 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1970 struct vtn_value
*src_val
= vtn_value(b
, w
[3], vtn_value_type_pointer
);
1971 struct vtn_pointer
*src
= src_val
->pointer
;
1973 vtn_assert_types_equal(b
, opcode
, res_type
, src_val
->type
->deref
);
1975 if (glsl_type_is_image(res_type
->type
) ||
1976 glsl_type_is_sampler(res_type
->type
)) {
1977 vtn_push_value(b
, w
[2], vtn_value_type_pointer
)->pointer
= src
;
1981 vtn_push_ssa(b
, w
[2], res_type
, vtn_variable_load(b
, src
));
1986 struct vtn_value
*dest_val
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
1987 struct vtn_pointer
*dest
= dest_val
->pointer
;
1988 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[2]);
1990 /* OpStore requires us to actually have a storage type */
1991 vtn_fail_if(dest
->type
->type
== NULL
,
1992 "Invalid destination type for OpStore");
1994 if (glsl_get_base_type(dest
->type
->type
) == GLSL_TYPE_BOOL
&&
1995 glsl_get_base_type(src_val
->type
->type
) == GLSL_TYPE_UINT
) {
1996 /* Early versions of GLSLang would use uint types for UBOs/SSBOs but
1997 * would then store them to a local variable as bool. Work around
1998 * the issue by doing an implicit conversion.
2000 * https://github.com/KhronosGroup/glslang/issues/170
2001 * https://bugs.freedesktop.org/show_bug.cgi?id=104424
2003 vtn_warn("OpStore of value of type OpTypeInt to a pointer to type "
2004 "OpTypeBool. Doing an implicit conversion to work around "
2006 struct vtn_ssa_value
*bool_ssa
=
2007 vtn_create_ssa_value(b
, dest
->type
->type
);
2008 bool_ssa
->def
= nir_i2b(&b
->nb
, vtn_ssa_value(b
, w
[2])->def
);
2009 vtn_variable_store(b
, bool_ssa
, dest
);
2013 vtn_assert_types_equal(b
, opcode
, dest_val
->type
->deref
, src_val
->type
);
2015 if (glsl_type_is_sampler(dest
->type
->type
)) {
2016 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
2017 "propagation to workaround the problem.");
2018 vtn_assert(dest
->var
->copy_prop_sampler
== NULL
);
2019 dest
->var
->copy_prop_sampler
=
2020 vtn_value(b
, w
[2], vtn_value_type_pointer
)->pointer
;
2024 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
2025 vtn_variable_store(b
, src
, dest
);
2029 case SpvOpArrayLength
: {
2030 struct vtn_pointer
*ptr
=
2031 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2033 const uint32_t offset
= ptr
->var
->type
->offsets
[w
[4]];
2034 const uint32_t stride
= ptr
->var
->type
->members
[w
[4]]->stride
;
2036 if (!ptr
->block_index
) {
2037 struct vtn_access_chain chain
= {
2040 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
2041 vtn_assert(ptr
->block_index
);
2044 nir_intrinsic_instr
*instr
=
2045 nir_intrinsic_instr_create(b
->nb
.shader
,
2046 nir_intrinsic_get_buffer_size
);
2047 instr
->src
[0] = nir_src_for_ssa(ptr
->block_index
);
2048 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
2049 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2050 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
2052 /* array_length = max(buffer_size - offset, 0) / stride */
2053 nir_ssa_def
*array_length
=
2058 nir_imm_int(&b
->nb
, offset
)),
2059 nir_imm_int(&b
->nb
, 0u)),
2060 nir_imm_int(&b
->nb
, stride
));
2062 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2063 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
2064 val
->ssa
->def
= array_length
;
2068 case SpvOpCopyMemorySized
:
2070 vtn_fail("Unhandled opcode");