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"
31 static struct vtn_access_chain
*
32 vtn_access_chain_create(struct vtn_builder
*b
, unsigned length
)
34 struct vtn_access_chain
*chain
;
36 /* Subtract 1 from the length since there's already one built in */
37 size_t size
= sizeof(*chain
) +
38 (MAX2(length
, 1) - 1) * sizeof(chain
->link
[0]);
39 chain
= rzalloc_size(b
, size
);
40 chain
->length
= length
;
45 static struct vtn_access_chain
*
46 vtn_access_chain_extend(struct vtn_builder
*b
, struct vtn_access_chain
*old
,
49 struct vtn_access_chain
*chain
;
51 unsigned old_len
= old
? old
->length
: 0;
52 chain
= vtn_access_chain_create(b
, old_len
+ new_ids
);
54 for (unsigned i
= 0; i
< old_len
; i
++)
55 chain
->link
[i
] = old
->link
[i
];
61 vtn_pointer_uses_ssa_offset(struct vtn_builder
*b
,
62 struct vtn_pointer
*ptr
)
64 return ptr
->mode
== vtn_variable_mode_ubo
||
65 ptr
->mode
== vtn_variable_mode_ssbo
;
69 vtn_pointer_is_external_block(struct vtn_builder
*b
,
70 struct vtn_pointer
*ptr
)
72 return ptr
->mode
== vtn_variable_mode_ssbo
||
73 ptr
->mode
== vtn_variable_mode_ubo
||
74 ptr
->mode
== vtn_variable_mode_push_constant
;
77 /* Dereference the given base pointer by the access chain */
78 static struct vtn_pointer
*
79 vtn_access_chain_pointer_dereference(struct vtn_builder
*b
,
80 struct vtn_pointer
*base
,
81 struct vtn_access_chain
*deref_chain
)
83 struct vtn_access_chain
*chain
=
84 vtn_access_chain_extend(b
, base
->chain
, deref_chain
->length
);
85 struct vtn_type
*type
= base
->type
;
87 /* OpPtrAccessChain is only allowed on things which support variable
88 * pointers. For everything else, the client is expected to just pass us
89 * the right access chain.
91 vtn_assert(!deref_chain
->ptr_as_array
);
93 unsigned start
= base
->chain
? base
->chain
->length
: 0;
94 for (unsigned i
= 0; i
< deref_chain
->length
; i
++) {
95 chain
->link
[start
+ i
] = deref_chain
->link
[i
];
97 if (glsl_type_is_struct(type
->type
)) {
98 vtn_assert(deref_chain
->link
[i
].mode
== vtn_access_mode_literal
);
99 type
= type
->members
[deref_chain
->link
[i
].id
];
101 type
= type
->array_element
;
105 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
106 ptr
->mode
= base
->mode
;
108 ptr
->var
= base
->var
;
115 vtn_access_link_as_ssa(struct vtn_builder
*b
, struct vtn_access_link link
,
118 vtn_assert(stride
> 0);
119 if (link
.mode
== vtn_access_mode_literal
) {
120 return nir_imm_int(&b
->nb
, link
.id
* stride
);
121 } else if (stride
== 1) {
122 nir_ssa_def
*ssa
= vtn_ssa_value(b
, link
.id
)->def
;
123 if (ssa
->bit_size
!= 32)
124 ssa
= nir_u2u32(&b
->nb
, ssa
);
127 nir_ssa_def
*src0
= vtn_ssa_value(b
, link
.id
)->def
;
128 if (src0
->bit_size
!= 32)
129 src0
= nir_u2u32(&b
->nb
, src0
);
130 return nir_imul(&b
->nb
, src0
, nir_imm_int(&b
->nb
, stride
));
135 vtn_variable_resource_index(struct vtn_builder
*b
, struct vtn_variable
*var
,
136 nir_ssa_def
*desc_array_index
)
138 if (!desc_array_index
) {
139 vtn_assert(glsl_type_is_struct(var
->type
->type
));
140 desc_array_index
= nir_imm_int(&b
->nb
, 0);
143 nir_intrinsic_instr
*instr
=
144 nir_intrinsic_instr_create(b
->nb
.shader
,
145 nir_intrinsic_vulkan_resource_index
);
146 instr
->src
[0] = nir_src_for_ssa(desc_array_index
);
147 nir_intrinsic_set_desc_set(instr
, var
->descriptor_set
);
148 nir_intrinsic_set_binding(instr
, var
->binding
);
150 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
151 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
153 return &instr
->dest
.ssa
;
156 static struct vtn_pointer
*
157 vtn_ssa_offset_pointer_dereference(struct vtn_builder
*b
,
158 struct vtn_pointer
*base
,
159 struct vtn_access_chain
*deref_chain
)
161 nir_ssa_def
*block_index
= base
->block_index
;
162 nir_ssa_def
*offset
= base
->offset
;
163 struct vtn_type
*type
= base
->type
;
166 if (deref_chain
->ptr_as_array
) {
167 /* We need ptr_type for the stride */
168 vtn_assert(base
->ptr_type
);
169 /* This must be a pointer to an actual element somewhere */
170 vtn_assert(block_index
&& offset
);
171 /* We need at least one element in the chain */
172 vtn_assert(deref_chain
->length
>= 1);
174 nir_ssa_def
*elem_offset
=
175 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
176 base
->ptr_type
->stride
);
177 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
182 /* This is the first access chain so we don't have a block index */
183 vtn_assert(!block_index
);
185 vtn_assert(base
->var
);
186 switch (base
->mode
) {
187 case vtn_variable_mode_ubo
:
188 case vtn_variable_mode_ssbo
:
189 if (glsl_type_is_array(type
->type
)) {
190 /* We need at least one element in the chain */
191 vtn_assert(deref_chain
->length
>= 1);
193 nir_ssa_def
*desc_arr_idx
=
194 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1);
195 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
196 type
= type
->array_element
;
199 block_index
= vtn_variable_resource_index(b
, base
->var
, NULL
);
201 offset
= nir_imm_int(&b
->nb
, 0);
205 vtn_fail("Invalid offset pointer mode");
210 for (; idx
< deref_chain
->length
; idx
++) {
211 switch (glsl_get_base_type(type
->type
)) {
214 case GLSL_TYPE_UINT64
:
215 case GLSL_TYPE_INT64
:
216 case GLSL_TYPE_FLOAT
:
217 case GLSL_TYPE_DOUBLE
:
219 case GLSL_TYPE_ARRAY
: {
220 nir_ssa_def
*elem_offset
=
221 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
], type
->stride
);
222 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
223 type
= type
->array_element
;
227 case GLSL_TYPE_STRUCT
: {
228 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
229 unsigned member
= deref_chain
->link
[idx
].id
;
230 nir_ssa_def
*mem_offset
= nir_imm_int(&b
->nb
, type
->offsets
[member
]);
231 offset
= nir_iadd(&b
->nb
, offset
, mem_offset
);
232 type
= type
->members
[member
];
237 vtn_fail("Invalid type for deref");
241 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
242 ptr
->mode
= base
->mode
;
244 ptr
->block_index
= block_index
;
245 ptr
->offset
= offset
;
250 /* Dereference the given base pointer by the access chain */
251 static struct vtn_pointer
*
252 vtn_pointer_dereference(struct vtn_builder
*b
,
253 struct vtn_pointer
*base
,
254 struct vtn_access_chain
*deref_chain
)
256 if (vtn_pointer_uses_ssa_offset(b
, base
)) {
257 return vtn_ssa_offset_pointer_dereference(b
, base
, deref_chain
);
259 return vtn_access_chain_pointer_dereference(b
, base
, deref_chain
);
263 /* Crawls a chain of array derefs and rewrites the types so that the
264 * lengths stay the same but the terminal type is the one given by
265 * tail_type. This is useful for split structures.
268 rewrite_deref_types(struct vtn_builder
*b
, nir_deref
*deref
,
269 const struct glsl_type
*type
)
273 vtn_assert(deref
->child
->deref_type
== nir_deref_type_array
);
274 vtn_assert(glsl_type_is_array(deref
->type
));
275 rewrite_deref_types(b
, deref
->child
, glsl_get_array_element(type
));
280 vtn_pointer_for_variable(struct vtn_builder
*b
,
281 struct vtn_variable
*var
, struct vtn_type
*ptr_type
)
283 struct vtn_pointer
*pointer
= rzalloc(b
, struct vtn_pointer
);
285 pointer
->mode
= var
->mode
;
286 pointer
->type
= var
->type
;
287 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
288 vtn_assert(ptr_type
->deref
->type
== var
->type
->type
);
289 pointer
->ptr_type
= ptr_type
;
296 vtn_pointer_to_deref(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
298 /* Do on-the-fly copy propagation for samplers. */
299 if (ptr
->var
->copy_prop_sampler
)
300 return vtn_pointer_to_deref(b
, ptr
->var
->copy_prop_sampler
);
302 nir_deref_var
*deref_var
;
304 deref_var
= nir_deref_var_create(b
, ptr
->var
->var
);
305 /* Raw variable access */
309 vtn_assert(ptr
->var
->members
);
310 /* Create the deref_var manually. It will get filled out later. */
311 deref_var
= rzalloc(b
, nir_deref_var
);
312 deref_var
->deref
.deref_type
= nir_deref_type_var
;
315 struct vtn_access_chain
*chain
= ptr
->chain
;
318 struct vtn_type
*deref_type
= ptr
->var
->type
;
319 nir_deref
*tail
= &deref_var
->deref
;
320 nir_variable
**members
= ptr
->var
->members
;
322 for (unsigned i
= 0; i
< chain
->length
; i
++) {
323 enum glsl_base_type base_type
= glsl_get_base_type(deref_type
->type
);
327 case GLSL_TYPE_UINT64
:
328 case GLSL_TYPE_INT64
:
329 case GLSL_TYPE_FLOAT
:
330 case GLSL_TYPE_DOUBLE
:
332 case GLSL_TYPE_ARRAY
: {
333 deref_type
= deref_type
->array_element
;
335 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
336 deref_arr
->deref
.type
= deref_type
->type
;
338 if (chain
->link
[i
].mode
== vtn_access_mode_literal
) {
339 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
340 deref_arr
->base_offset
= chain
->link
[i
].id
;
342 vtn_assert(chain
->link
[i
].mode
== vtn_access_mode_id
);
343 deref_arr
->deref_array_type
= nir_deref_array_type_indirect
;
344 deref_arr
->base_offset
= 0;
345 deref_arr
->indirect
=
346 nir_src_for_ssa(vtn_ssa_value(b
, chain
->link
[i
].id
)->def
);
348 tail
->child
= &deref_arr
->deref
;
353 case GLSL_TYPE_STRUCT
: {
354 vtn_assert(chain
->link
[i
].mode
== vtn_access_mode_literal
);
355 unsigned idx
= chain
->link
[i
].id
;
356 deref_type
= deref_type
->members
[idx
];
358 /* This is a pre-split structure. */
359 deref_var
->var
= members
[idx
];
360 rewrite_deref_types(b
, &deref_var
->deref
, members
[idx
]->type
);
361 vtn_assert(tail
->type
== deref_type
->type
);
364 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, idx
);
365 deref_struct
->deref
.type
= deref_type
->type
;
366 tail
->child
= &deref_struct
->deref
;
372 vtn_fail("Invalid type for deref");
376 vtn_assert(members
== NULL
);
381 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_var
*deref
,
382 nir_deref
*tail
, struct vtn_ssa_value
*inout
)
384 /* The deref tail may contain a deref to select a component of a vector (in
385 * other words, it might not be an actual tail) so we have to save it away
386 * here since we overwrite it later.
388 nir_deref
*old_child
= tail
->child
;
390 if (glsl_type_is_vector_or_scalar(tail
->type
)) {
391 /* Terminate the deref chain in case there is one more link to pick
392 * off a component of the vector.
396 nir_intrinsic_op op
= load
? nir_intrinsic_load_var
:
397 nir_intrinsic_store_var
;
399 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
400 intrin
->variables
[0] = nir_deref_var_clone(deref
, intrin
);
401 intrin
->num_components
= glsl_get_vector_elements(tail
->type
);
404 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
405 intrin
->num_components
,
406 glsl_get_bit_size(tail
->type
),
408 inout
->def
= &intrin
->dest
.ssa
;
410 nir_intrinsic_set_write_mask(intrin
, (1 << intrin
->num_components
) - 1);
411 intrin
->src
[0] = nir_src_for_ssa(inout
->def
);
414 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
415 } else if (glsl_get_base_type(tail
->type
) == GLSL_TYPE_ARRAY
||
416 glsl_type_is_matrix(tail
->type
)) {
417 unsigned elems
= glsl_get_length(tail
->type
);
418 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
419 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
420 deref_arr
->deref
.type
= glsl_get_array_element(tail
->type
);
421 tail
->child
= &deref_arr
->deref
;
422 for (unsigned i
= 0; i
< elems
; i
++) {
423 deref_arr
->base_offset
= i
;
424 _vtn_local_load_store(b
, load
, deref
, tail
->child
, inout
->elems
[i
]);
427 vtn_assert(glsl_get_base_type(tail
->type
) == GLSL_TYPE_STRUCT
);
428 unsigned elems
= glsl_get_length(tail
->type
);
429 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, 0);
430 tail
->child
= &deref_struct
->deref
;
431 for (unsigned i
= 0; i
< elems
; i
++) {
432 deref_struct
->index
= i
;
433 deref_struct
->deref
.type
= glsl_get_struct_field(tail
->type
, i
);
434 _vtn_local_load_store(b
, load
, deref
, tail
->child
, inout
->elems
[i
]);
438 tail
->child
= old_child
;
442 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
444 struct vtn_pointer
*ptr
= vtn_value(b
, id
, vtn_value_type_pointer
)->pointer
;
445 return vtn_pointer_to_deref(b
, ptr
);
449 * Gets the NIR-level deref tail, which may have as a child an array deref
450 * selecting which component due to OpAccessChain supporting per-component
451 * indexing in SPIR-V.
454 get_deref_tail(nir_deref_var
*deref
)
456 nir_deref
*cur
= &deref
->deref
;
457 while (!glsl_type_is_vector_or_scalar(cur
->type
) && cur
->child
)
463 struct vtn_ssa_value
*
464 vtn_local_load(struct vtn_builder
*b
, nir_deref_var
*src
)
466 nir_deref
*src_tail
= get_deref_tail(src
);
467 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
468 _vtn_local_load_store(b
, true, src
, src_tail
, val
);
470 if (src_tail
->child
) {
471 nir_deref_array
*vec_deref
= nir_deref_as_array(src_tail
->child
);
472 vtn_assert(vec_deref
->deref
.child
== NULL
);
473 val
->type
= vec_deref
->deref
.type
;
474 if (vec_deref
->deref_array_type
== nir_deref_array_type_direct
)
475 val
->def
= vtn_vector_extract(b
, val
->def
, vec_deref
->base_offset
);
477 val
->def
= vtn_vector_extract_dynamic(b
, val
->def
,
478 vec_deref
->indirect
.ssa
);
485 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
488 nir_deref
*dest_tail
= get_deref_tail(dest
);
490 if (dest_tail
->child
) {
491 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
492 _vtn_local_load_store(b
, true, dest
, dest_tail
, val
);
493 nir_deref_array
*deref
= nir_deref_as_array(dest_tail
->child
);
494 vtn_assert(deref
->deref
.child
== NULL
);
495 if (deref
->deref_array_type
== nir_deref_array_type_direct
)
496 val
->def
= vtn_vector_insert(b
, val
->def
, src
->def
,
499 val
->def
= vtn_vector_insert_dynamic(b
, val
->def
, src
->def
,
500 deref
->indirect
.ssa
);
501 _vtn_local_load_store(b
, false, dest
, dest_tail
, val
);
503 _vtn_local_load_store(b
, false, dest
, dest_tail
, src
);
508 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
509 nir_ssa_def
**index_out
, unsigned *end_idx_out
)
511 if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
513 struct vtn_access_chain chain
= {
516 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
518 *index_out
= ptr
->block_index
;
522 vtn_assert(ptr
->mode
== vtn_variable_mode_push_constant
);
526 struct vtn_type
*type
= ptr
->var
->type
;
527 nir_ssa_def
*offset
= nir_imm_int(&b
->nb
, 0);
528 for (; idx
< ptr
->chain
->length
; idx
++) {
529 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
533 case GLSL_TYPE_UINT64
:
534 case GLSL_TYPE_INT64
:
535 case GLSL_TYPE_FLOAT
:
536 case GLSL_TYPE_DOUBLE
:
538 case GLSL_TYPE_ARRAY
:
539 offset
= nir_iadd(&b
->nb
, offset
,
540 vtn_access_link_as_ssa(b
, ptr
->chain
->link
[idx
],
543 type
= type
->array_element
;
546 case GLSL_TYPE_STRUCT
: {
547 vtn_assert(ptr
->chain
->link
[idx
].mode
== vtn_access_mode_literal
);
548 unsigned member
= ptr
->chain
->link
[idx
].id
;
549 offset
= nir_iadd(&b
->nb
, offset
,
550 nir_imm_int(&b
->nb
, type
->offsets
[member
]));
551 type
= type
->members
[member
];
556 vtn_fail("Invalid type for deref");
560 vtn_assert(type
== ptr
->type
);
567 /* Tries to compute the size of an interface block based on the strides and
568 * offsets that are provided to us in the SPIR-V source.
571 vtn_type_block_size(struct vtn_builder
*b
, struct vtn_type
*type
)
573 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
577 case GLSL_TYPE_UINT64
:
578 case GLSL_TYPE_INT64
:
579 case GLSL_TYPE_FLOAT
:
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
;
587 } else if (base_type
== GLSL_TYPE_DOUBLE
||
588 base_type
== GLSL_TYPE_UINT64
||
589 base_type
== GLSL_TYPE_INT64
) {
590 return glsl_get_vector_elements(type
->type
) * 8;
592 return glsl_get_vector_elements(type
->type
) * 4;
596 case GLSL_TYPE_STRUCT
:
597 case GLSL_TYPE_INTERFACE
: {
599 unsigned num_fields
= glsl_get_length(type
->type
);
600 for (unsigned f
= 0; f
< num_fields
; f
++) {
601 unsigned field_end
= type
->offsets
[f
] +
602 vtn_type_block_size(b
, type
->members
[f
]);
603 size
= MAX2(size
, field_end
);
608 case GLSL_TYPE_ARRAY
:
609 vtn_assert(type
->stride
> 0);
610 vtn_assert(glsl_get_length(type
->type
) > 0);
611 return type
->stride
* glsl_get_length(type
->type
);
614 vtn_fail("Invalid block type");
620 vtn_access_chain_get_offset_size(struct vtn_builder
*b
,
621 struct vtn_access_chain
*chain
,
622 struct vtn_type
*type
,
623 unsigned *access_offset
,
624 unsigned *access_size
)
628 for (unsigned i
= 0; i
< chain
->length
; i
++) {
629 if (chain
->link
[i
].mode
!= vtn_access_mode_literal
)
632 if (glsl_type_is_struct(type
->type
)) {
633 *access_offset
+= type
->offsets
[chain
->link
[i
].id
];
634 type
= type
->members
[chain
->link
[i
].id
];
636 *access_offset
+= type
->stride
* chain
->link
[i
].id
;
637 type
= type
->array_element
;
641 *access_size
= vtn_type_block_size(b
, type
);
645 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
646 nir_ssa_def
*index
, nir_ssa_def
*offset
,
647 unsigned access_offset
, unsigned access_size
,
648 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
)
650 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
651 instr
->num_components
= glsl_get_vector_elements(type
);
655 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
656 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
659 if (op
== nir_intrinsic_load_push_constant
) {
660 vtn_assert(access_offset
% 4 == 0);
662 nir_intrinsic_set_base(instr
, access_offset
);
663 nir_intrinsic_set_range(instr
, access_size
);
667 instr
->src
[src
++] = nir_src_for_ssa(index
);
669 if (op
== nir_intrinsic_load_push_constant
) {
670 /* We need to subtract the offset from where the intrinsic will load the
673 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
674 nir_imm_int(&b
->nb
, access_offset
)));
676 instr
->src
[src
++] = nir_src_for_ssa(offset
);
680 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
681 instr
->num_components
,
682 glsl_get_bit_size(type
), NULL
);
683 (*inout
)->def
= &instr
->dest
.ssa
;
686 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
688 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
689 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
693 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
694 nir_ssa_def
*index
, nir_ssa_def
*offset
,
695 unsigned access_offset
, unsigned access_size
,
696 struct vtn_access_chain
*chain
, unsigned chain_idx
,
697 struct vtn_type
*type
, struct vtn_ssa_value
**inout
)
699 if (chain
&& chain_idx
>= chain
->length
)
702 if (load
&& chain
== NULL
&& *inout
== NULL
)
703 *inout
= vtn_create_ssa_value(b
, type
->type
);
705 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
709 case GLSL_TYPE_UINT64
:
710 case GLSL_TYPE_INT64
:
711 case GLSL_TYPE_FLOAT
:
712 case GLSL_TYPE_DOUBLE
:
714 /* This is where things get interesting. At this point, we've hit
715 * a vector, a scalar, or a matrix.
717 if (glsl_type_is_matrix(type
->type
)) {
718 /* Loading the whole matrix */
719 struct vtn_ssa_value
*transpose
;
720 unsigned num_ops
, vec_width
, col_stride
;
721 if (type
->row_major
) {
722 num_ops
= glsl_get_vector_elements(type
->type
);
723 vec_width
= glsl_get_matrix_columns(type
->type
);
724 col_stride
= type
->array_element
->stride
;
726 const struct glsl_type
*transpose_type
=
727 glsl_matrix_type(base_type
, vec_width
, num_ops
);
728 *inout
= vtn_create_ssa_value(b
, transpose_type
);
730 transpose
= vtn_ssa_transpose(b
, *inout
);
734 num_ops
= glsl_get_matrix_columns(type
->type
);
735 vec_width
= glsl_get_vector_elements(type
->type
);
736 col_stride
= type
->stride
;
739 for (unsigned i
= 0; i
< num_ops
; i
++) {
740 nir_ssa_def
*elem_offset
=
741 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* col_stride
));
742 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
743 access_offset
, access_size
,
745 glsl_vector_type(base_type
, vec_width
));
748 if (load
&& type
->row_major
)
749 *inout
= vtn_ssa_transpose(b
, *inout
);
751 unsigned elems
= glsl_get_vector_elements(type
->type
);
752 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
753 if (elems
== 1 || type
->stride
== type_size
) {
754 /* This is a tightly-packed normal scalar or vector load */
755 vtn_assert(glsl_type_is_vector_or_scalar(type
->type
));
756 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
757 access_offset
, access_size
,
760 /* This is a strided load. We have to load N things separately.
761 * This is the single column of a row-major matrix case.
763 vtn_assert(type
->stride
> type_size
);
764 vtn_assert(type
->stride
% type_size
== 0);
766 nir_ssa_def
*per_comp
[4];
767 for (unsigned i
= 0; i
< elems
; i
++) {
768 nir_ssa_def
*elem_offset
=
769 nir_iadd(&b
->nb
, offset
,
770 nir_imm_int(&b
->nb
, i
* type
->stride
));
771 struct vtn_ssa_value
*comp
, temp_val
;
773 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
774 temp_val
.type
= glsl_scalar_type(base_type
);
777 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
778 access_offset
, access_size
,
779 &comp
, glsl_scalar_type(base_type
));
780 per_comp
[i
] = comp
->def
;
785 *inout
= vtn_create_ssa_value(b
, type
->type
);
786 (*inout
)->def
= nir_vec(&b
->nb
, per_comp
, elems
);
792 case GLSL_TYPE_ARRAY
: {
793 unsigned elems
= glsl_get_length(type
->type
);
794 for (unsigned i
= 0; i
< elems
; i
++) {
795 nir_ssa_def
*elem_off
=
796 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* type
->stride
));
797 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
798 access_offset
, access_size
,
800 type
->array_element
, &(*inout
)->elems
[i
]);
805 case GLSL_TYPE_STRUCT
: {
806 unsigned elems
= glsl_get_length(type
->type
);
807 for (unsigned i
= 0; i
< elems
; i
++) {
808 nir_ssa_def
*elem_off
=
809 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, type
->offsets
[i
]));
810 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
811 access_offset
, access_size
,
813 type
->members
[i
], &(*inout
)->elems
[i
]);
819 vtn_fail("Invalid block member type");
823 static struct vtn_ssa_value
*
824 vtn_block_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
827 unsigned access_offset
= 0, access_size
= 0;
829 case vtn_variable_mode_ubo
:
830 op
= nir_intrinsic_load_ubo
;
832 case vtn_variable_mode_ssbo
:
833 op
= nir_intrinsic_load_ssbo
;
835 case vtn_variable_mode_push_constant
:
836 op
= nir_intrinsic_load_push_constant
;
837 vtn_access_chain_get_offset_size(b
, src
->chain
, src
->var
->type
,
838 &access_offset
, &access_size
);
841 vtn_fail("Invalid block variable mode");
844 nir_ssa_def
*offset
, *index
= NULL
;
846 offset
= vtn_pointer_to_offset(b
, src
, &index
, &chain_idx
);
848 struct vtn_ssa_value
*value
= NULL
;
849 _vtn_block_load_store(b
, op
, true, index
, offset
,
850 access_offset
, access_size
,
851 src
->chain
, chain_idx
, src
->type
, &value
);
856 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
857 struct vtn_pointer
*dst
)
861 case vtn_variable_mode_ssbo
:
862 op
= nir_intrinsic_store_ssbo
;
865 vtn_fail("Invalid block variable mode");
868 nir_ssa_def
*offset
, *index
= NULL
;
870 offset
= vtn_pointer_to_offset(b
, dst
, &index
, &chain_idx
);
872 _vtn_block_load_store(b
, op
, false, index
, offset
,
873 0, 0, dst
->chain
, chain_idx
, dst
->type
, &src
);
877 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
878 struct vtn_pointer
*ptr
,
879 struct vtn_ssa_value
**inout
)
881 enum glsl_base_type base_type
= glsl_get_base_type(ptr
->type
->type
);
885 case GLSL_TYPE_UINT64
:
886 case GLSL_TYPE_INT64
:
887 case GLSL_TYPE_FLOAT
:
889 case GLSL_TYPE_DOUBLE
:
890 /* At this point, we have a scalar, vector, or matrix so we know that
891 * there cannot be any structure splitting still in the way. By
892 * stopping at the matrix level rather than the vector level, we
893 * ensure that matrices get loaded in the optimal way even if they
894 * are storred row-major in a UBO.
897 *inout
= vtn_local_load(b
, vtn_pointer_to_deref(b
, ptr
));
899 vtn_local_store(b
, *inout
, vtn_pointer_to_deref(b
, ptr
));
903 case GLSL_TYPE_ARRAY
:
904 case GLSL_TYPE_STRUCT
: {
905 unsigned elems
= glsl_get_length(ptr
->type
->type
);
907 vtn_assert(*inout
== NULL
);
908 *inout
= rzalloc(b
, struct vtn_ssa_value
);
909 (*inout
)->type
= ptr
->type
->type
;
910 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
913 struct vtn_access_chain chain
= {
916 { .mode
= vtn_access_mode_literal
, },
919 for (unsigned i
= 0; i
< elems
; i
++) {
920 chain
.link
[0].id
= i
;
921 struct vtn_pointer
*elem
= vtn_pointer_dereference(b
, ptr
, &chain
);
922 _vtn_variable_load_store(b
, load
, elem
, &(*inout
)->elems
[i
]);
928 vtn_fail("Invalid access chain type");
932 struct vtn_ssa_value
*
933 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
935 if (vtn_pointer_is_external_block(b
, src
)) {
936 return vtn_block_load(b
, src
);
938 struct vtn_ssa_value
*val
= NULL
;
939 _vtn_variable_load_store(b
, true, src
, &val
);
945 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
946 struct vtn_pointer
*dest
)
948 if (vtn_pointer_is_external_block(b
, dest
)) {
949 vtn_assert(dest
->mode
== vtn_variable_mode_ssbo
);
950 vtn_block_store(b
, src
, dest
);
952 _vtn_variable_load_store(b
, false, dest
, &src
);
957 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
958 struct vtn_pointer
*src
)
960 vtn_assert(src
->type
->type
== dest
->type
->type
);
961 enum glsl_base_type base_type
= glsl_get_base_type(src
->type
->type
);
965 case GLSL_TYPE_UINT64
:
966 case GLSL_TYPE_INT64
:
967 case GLSL_TYPE_FLOAT
:
968 case GLSL_TYPE_DOUBLE
:
970 /* At this point, we have a scalar, vector, or matrix so we know that
971 * there cannot be any structure splitting still in the way. By
972 * stopping at the matrix level rather than the vector level, we
973 * ensure that matrices get loaded in the optimal way even if they
974 * are storred row-major in a UBO.
976 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
979 case GLSL_TYPE_ARRAY
:
980 case GLSL_TYPE_STRUCT
: {
981 struct vtn_access_chain chain
= {
984 { .mode
= vtn_access_mode_literal
, },
987 unsigned elems
= glsl_get_length(src
->type
->type
);
988 for (unsigned i
= 0; i
< elems
; i
++) {
989 chain
.link
[0].id
= i
;
990 struct vtn_pointer
*src_elem
=
991 vtn_pointer_dereference(b
, src
, &chain
);
992 struct vtn_pointer
*dest_elem
=
993 vtn_pointer_dereference(b
, dest
, &chain
);
995 _vtn_variable_copy(b
, dest_elem
, src_elem
);
1001 vtn_fail("Invalid access chain type");
1006 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
1007 struct vtn_pointer
*src
)
1009 /* TODO: At some point, we should add a special-case for when we can
1010 * just emit a copy_var intrinsic.
1012 _vtn_variable_copy(b
, dest
, src
);
1016 set_mode_system_value(struct vtn_builder
*b
, nir_variable_mode
*mode
)
1018 vtn_assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
1019 *mode
= nir_var_system_value
;
1023 vtn_get_builtin_location(struct vtn_builder
*b
,
1024 SpvBuiltIn builtin
, int *location
,
1025 nir_variable_mode
*mode
)
1028 case SpvBuiltInPosition
:
1029 *location
= VARYING_SLOT_POS
;
1031 case SpvBuiltInPointSize
:
1032 *location
= VARYING_SLOT_PSIZ
;
1034 case SpvBuiltInClipDistance
:
1035 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
1037 case SpvBuiltInCullDistance
:
1038 *location
= VARYING_SLOT_CULL_DIST0
;
1040 case SpvBuiltInVertexIndex
:
1041 *location
= SYSTEM_VALUE_VERTEX_ID
;
1042 set_mode_system_value(b
, mode
);
1044 case SpvBuiltInVertexId
:
1045 /* Vulkan defines VertexID to be zero-based and reserves the new
1046 * builtin keyword VertexIndex to indicate the non-zero-based value.
1048 *location
= SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1049 set_mode_system_value(b
, mode
);
1051 case SpvBuiltInInstanceIndex
:
1052 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
1053 set_mode_system_value(b
, mode
);
1055 case SpvBuiltInInstanceId
:
1056 *location
= SYSTEM_VALUE_INSTANCE_ID
;
1057 set_mode_system_value(b
, mode
);
1059 case SpvBuiltInPrimitiveId
:
1060 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1061 vtn_assert(*mode
== nir_var_shader_in
);
1062 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1063 } else if (*mode
== nir_var_shader_out
) {
1064 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1066 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
1067 set_mode_system_value(b
, mode
);
1070 case SpvBuiltInInvocationId
:
1071 *location
= SYSTEM_VALUE_INVOCATION_ID
;
1072 set_mode_system_value(b
, mode
);
1074 case SpvBuiltInLayer
:
1075 *location
= VARYING_SLOT_LAYER
;
1076 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1077 *mode
= nir_var_shader_in
;
1078 else if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
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
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1088 *mode
= nir_var_shader_in
;
1090 vtn_fail("invalid stage for SpvBuiltInViewportIndex");
1092 case SpvBuiltInTessLevelOuter
:
1093 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
1095 case SpvBuiltInTessLevelInner
:
1096 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
1098 case SpvBuiltInTessCoord
:
1099 *location
= SYSTEM_VALUE_TESS_COORD
;
1100 set_mode_system_value(b
, mode
);
1102 case SpvBuiltInPatchVertices
:
1103 *location
= SYSTEM_VALUE_VERTICES_IN
;
1104 set_mode_system_value(b
, mode
);
1106 case SpvBuiltInFragCoord
:
1107 *location
= VARYING_SLOT_POS
;
1108 vtn_assert(*mode
== nir_var_shader_in
);
1110 case SpvBuiltInPointCoord
:
1111 *location
= VARYING_SLOT_PNTC
;
1112 vtn_assert(*mode
== nir_var_shader_in
);
1114 case SpvBuiltInFrontFacing
:
1115 *location
= SYSTEM_VALUE_FRONT_FACE
;
1116 set_mode_system_value(b
, mode
);
1118 case SpvBuiltInSampleId
:
1119 *location
= SYSTEM_VALUE_SAMPLE_ID
;
1120 set_mode_system_value(b
, mode
);
1122 case SpvBuiltInSamplePosition
:
1123 *location
= SYSTEM_VALUE_SAMPLE_POS
;
1124 set_mode_system_value(b
, mode
);
1126 case SpvBuiltInSampleMask
:
1127 if (*mode
== nir_var_shader_out
) {
1128 *location
= FRAG_RESULT_SAMPLE_MASK
;
1130 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1131 set_mode_system_value(b
, mode
);
1134 case SpvBuiltInFragDepth
:
1135 *location
= FRAG_RESULT_DEPTH
;
1136 vtn_assert(*mode
== nir_var_shader_out
);
1138 case SpvBuiltInHelperInvocation
:
1139 *location
= SYSTEM_VALUE_HELPER_INVOCATION
;
1140 set_mode_system_value(b
, mode
);
1142 case SpvBuiltInNumWorkgroups
:
1143 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1144 set_mode_system_value(b
, mode
);
1146 case SpvBuiltInWorkgroupSize
:
1147 /* This should already be handled */
1148 vtn_fail("unsupported builtin");
1150 case SpvBuiltInWorkgroupId
:
1151 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1152 set_mode_system_value(b
, mode
);
1154 case SpvBuiltInLocalInvocationId
:
1155 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1156 set_mode_system_value(b
, mode
);
1158 case SpvBuiltInLocalInvocationIndex
:
1159 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1160 set_mode_system_value(b
, mode
);
1162 case SpvBuiltInGlobalInvocationId
:
1163 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1164 set_mode_system_value(b
, mode
);
1166 case SpvBuiltInBaseVertex
:
1167 *location
= SYSTEM_VALUE_BASE_VERTEX
;
1168 set_mode_system_value(b
, mode
);
1170 case SpvBuiltInBaseInstance
:
1171 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1172 set_mode_system_value(b
, mode
);
1174 case SpvBuiltInDrawIndex
:
1175 *location
= SYSTEM_VALUE_DRAW_ID
;
1176 set_mode_system_value(b
, mode
);
1178 case SpvBuiltInViewIndex
:
1179 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1180 set_mode_system_value(b
, mode
);
1183 vtn_fail("unsupported builtin");
1188 apply_var_decoration(struct vtn_builder
*b
, nir_variable
*nir_var
,
1189 const struct vtn_decoration
*dec
)
1191 switch (dec
->decoration
) {
1192 case SpvDecorationRelaxedPrecision
:
1193 break; /* FIXME: Do nothing with this for now. */
1194 case SpvDecorationNoPerspective
:
1195 nir_var
->data
.interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1197 case SpvDecorationFlat
:
1198 nir_var
->data
.interpolation
= INTERP_MODE_FLAT
;
1200 case SpvDecorationCentroid
:
1201 nir_var
->data
.centroid
= true;
1203 case SpvDecorationSample
:
1204 nir_var
->data
.sample
= true;
1206 case SpvDecorationInvariant
:
1207 nir_var
->data
.invariant
= true;
1209 case SpvDecorationConstant
:
1210 vtn_assert(nir_var
->constant_initializer
!= NULL
);
1211 nir_var
->data
.read_only
= true;
1213 case SpvDecorationNonReadable
:
1214 nir_var
->data
.image
.write_only
= true;
1216 case SpvDecorationNonWritable
:
1217 nir_var
->data
.read_only
= true;
1218 nir_var
->data
.image
.read_only
= true;
1220 case SpvDecorationComponent
:
1221 nir_var
->data
.location_frac
= dec
->literals
[0];
1223 case SpvDecorationIndex
:
1224 nir_var
->data
.index
= dec
->literals
[0];
1226 case SpvDecorationBuiltIn
: {
1227 SpvBuiltIn builtin
= dec
->literals
[0];
1229 if (builtin
== SpvBuiltInWorkgroupSize
) {
1230 /* This shouldn't be a builtin. It's actually a constant. */
1231 nir_var
->data
.mode
= nir_var_global
;
1232 nir_var
->data
.read_only
= true;
1234 nir_constant
*c
= rzalloc(nir_var
, nir_constant
);
1235 c
->values
[0].u32
[0] = b
->shader
->info
.cs
.local_size
[0];
1236 c
->values
[0].u32
[1] = b
->shader
->info
.cs
.local_size
[1];
1237 c
->values
[0].u32
[2] = b
->shader
->info
.cs
.local_size
[2];
1238 nir_var
->constant_initializer
= c
;
1242 nir_variable_mode mode
= nir_var
->data
.mode
;
1243 vtn_get_builtin_location(b
, builtin
, &nir_var
->data
.location
, &mode
);
1244 nir_var
->data
.mode
= mode
;
1247 case SpvBuiltInTessLevelOuter
:
1248 case SpvBuiltInTessLevelInner
:
1249 nir_var
->data
.compact
= true;
1251 case SpvBuiltInSamplePosition
:
1252 nir_var
->data
.origin_upper_left
= b
->origin_upper_left
;
1254 case SpvBuiltInFragCoord
:
1255 nir_var
->data
.pixel_center_integer
= b
->pixel_center_integer
;
1262 case SpvDecorationSpecId
:
1263 case SpvDecorationRowMajor
:
1264 case SpvDecorationColMajor
:
1265 case SpvDecorationMatrixStride
:
1266 case SpvDecorationRestrict
:
1267 case SpvDecorationAliased
:
1268 case SpvDecorationVolatile
:
1269 case SpvDecorationCoherent
:
1270 case SpvDecorationUniform
:
1271 case SpvDecorationStream
:
1272 case SpvDecorationOffset
:
1273 case SpvDecorationLinkageAttributes
:
1274 break; /* Do nothing with these here */
1276 case SpvDecorationPatch
:
1277 nir_var
->data
.patch
= true;
1280 case SpvDecorationLocation
:
1281 vtn_fail("Handled above");
1283 case SpvDecorationBlock
:
1284 case SpvDecorationBufferBlock
:
1285 case SpvDecorationArrayStride
:
1286 case SpvDecorationGLSLShared
:
1287 case SpvDecorationGLSLPacked
:
1288 break; /* These can apply to a type but we don't care about them */
1290 case SpvDecorationBinding
:
1291 case SpvDecorationDescriptorSet
:
1292 case SpvDecorationNoContraction
:
1293 case SpvDecorationInputAttachmentIndex
:
1294 vtn_warn("Decoration not allowed for variable or structure member: %s",
1295 spirv_decoration_to_string(dec
->decoration
));
1298 case SpvDecorationXfbBuffer
:
1299 case SpvDecorationXfbStride
:
1300 vtn_warn("Vulkan does not have transform feedback: %s",
1301 spirv_decoration_to_string(dec
->decoration
));
1304 case SpvDecorationCPacked
:
1305 case SpvDecorationSaturatedConversion
:
1306 case SpvDecorationFuncParamAttr
:
1307 case SpvDecorationFPRoundingMode
:
1308 case SpvDecorationFPFastMathMode
:
1309 case SpvDecorationAlignment
:
1310 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1311 spirv_decoration_to_string(dec
->decoration
));
1315 vtn_fail("Unhandled decoration");
1320 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1321 const struct vtn_decoration
*dec
, void *out_is_patch
)
1323 if (dec
->decoration
== SpvDecorationPatch
) {
1324 *((bool *) out_is_patch
) = true;
1329 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1330 const struct vtn_decoration
*dec
, void *void_var
)
1332 struct vtn_variable
*vtn_var
= void_var
;
1334 /* Handle decorations that apply to a vtn_variable as a whole */
1335 switch (dec
->decoration
) {
1336 case SpvDecorationBinding
:
1337 vtn_var
->binding
= dec
->literals
[0];
1339 case SpvDecorationDescriptorSet
:
1340 vtn_var
->descriptor_set
= dec
->literals
[0];
1342 case SpvDecorationInputAttachmentIndex
:
1343 vtn_var
->input_attachment_index
= dec
->literals
[0];
1345 case SpvDecorationPatch
:
1346 vtn_var
->patch
= true;
1352 if (val
->value_type
== vtn_value_type_pointer
) {
1353 vtn_assert(val
->pointer
->var
== void_var
);
1354 vtn_assert(val
->pointer
->chain
== NULL
);
1355 vtn_assert(member
== -1);
1357 vtn_assert(val
->value_type
== vtn_value_type_type
);
1360 /* Location is odd. If applied to a split structure, we have to walk the
1361 * whole thing and accumulate the location. It's easier to handle as a
1364 if (dec
->decoration
== SpvDecorationLocation
) {
1365 unsigned location
= dec
->literals
[0];
1366 bool is_vertex_input
;
1367 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
&&
1368 vtn_var
->mode
== vtn_variable_mode_output
) {
1369 is_vertex_input
= false;
1370 location
+= FRAG_RESULT_DATA0
;
1371 } else if (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
&&
1372 vtn_var
->mode
== vtn_variable_mode_input
) {
1373 is_vertex_input
= true;
1374 location
+= VERT_ATTRIB_GENERIC0
;
1375 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1376 vtn_var
->mode
== vtn_variable_mode_output
) {
1377 is_vertex_input
= false;
1378 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1380 vtn_warn("Location must be on input or output variable");
1385 /* This handles the member and lone variable cases */
1386 vtn_var
->var
->data
.location
= location
;
1388 /* This handles the structure member case */
1389 vtn_assert(vtn_var
->members
);
1391 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1392 for (unsigned i
= 0; i
< length
; i
++) {
1393 vtn_var
->members
[i
]->data
.location
= location
;
1395 glsl_count_attribute_slots(vtn_var
->members
[i
]->interface_type
,
1402 vtn_assert(member
<= 0);
1403 apply_var_decoration(b
, vtn_var
->var
, dec
);
1404 } else if (vtn_var
->members
) {
1406 vtn_assert(vtn_var
->members
);
1407 apply_var_decoration(b
, vtn_var
->members
[member
], dec
);
1410 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1411 for (unsigned i
= 0; i
< length
; i
++)
1412 apply_var_decoration(b
, vtn_var
->members
[i
], dec
);
1415 /* A few variables, those with external storage, have no actual
1416 * nir_variables associated with them. Fortunately, all decorations
1417 * we care about for those variables are on the type only.
1419 vtn_assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1420 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1421 vtn_var
->mode
== vtn_variable_mode_push_constant
);
1426 static enum vtn_variable_mode
1427 vtn_storage_class_to_mode(struct vtn_builder
*b
,
1428 SpvStorageClass
class,
1429 struct vtn_type
*interface_type
,
1430 nir_variable_mode
*nir_mode_out
)
1432 enum vtn_variable_mode mode
;
1433 nir_variable_mode nir_mode
;
1435 case SpvStorageClassUniform
:
1436 if (interface_type
->block
) {
1437 mode
= vtn_variable_mode_ubo
;
1439 } else if (interface_type
->buffer_block
) {
1440 mode
= vtn_variable_mode_ssbo
;
1443 vtn_fail("Invalid uniform variable type");
1446 case SpvStorageClassStorageBuffer
:
1447 mode
= vtn_variable_mode_ssbo
;
1450 case SpvStorageClassUniformConstant
:
1451 if (glsl_type_is_image(interface_type
->type
)) {
1452 mode
= vtn_variable_mode_image
;
1453 nir_mode
= nir_var_uniform
;
1454 } else if (glsl_type_is_sampler(interface_type
->type
)) {
1455 mode
= vtn_variable_mode_sampler
;
1456 nir_mode
= nir_var_uniform
;
1458 vtn_fail("Invalid uniform constant variable type");
1461 case SpvStorageClassPushConstant
:
1462 mode
= vtn_variable_mode_push_constant
;
1463 nir_mode
= nir_var_uniform
;
1465 case SpvStorageClassInput
:
1466 mode
= vtn_variable_mode_input
;
1467 nir_mode
= nir_var_shader_in
;
1469 case SpvStorageClassOutput
:
1470 mode
= vtn_variable_mode_output
;
1471 nir_mode
= nir_var_shader_out
;
1473 case SpvStorageClassPrivate
:
1474 mode
= vtn_variable_mode_global
;
1475 nir_mode
= nir_var_global
;
1477 case SpvStorageClassFunction
:
1478 mode
= vtn_variable_mode_local
;
1479 nir_mode
= nir_var_local
;
1481 case SpvStorageClassWorkgroup
:
1482 mode
= vtn_variable_mode_workgroup
;
1483 nir_mode
= nir_var_shared
;
1485 case SpvStorageClassCrossWorkgroup
:
1486 case SpvStorageClassGeneric
:
1487 case SpvStorageClassAtomicCounter
:
1489 vtn_fail("Unhandled variable storage class");
1493 *nir_mode_out
= nir_mode
;
1499 vtn_pointer_to_ssa(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
1501 /* This pointer needs to have a pointer type with actual storage */
1502 vtn_assert(ptr
->ptr_type
);
1503 vtn_assert(ptr
->ptr_type
->type
);
1505 if (!ptr
->offset
|| !ptr
->block_index
) {
1506 /* If we don't have an offset then we must be a pointer to the variable
1509 vtn_assert(!ptr
->offset
&& !ptr
->block_index
);
1511 /* We can't handle a pointer to an array of descriptors because we have
1512 * no way of knowing later on that we need to add to update the block
1513 * index when dereferencing.
1515 vtn_assert(ptr
->var
&& ptr
->var
->type
->base_type
== vtn_base_type_struct
);
1517 struct vtn_access_chain chain
= {
1520 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
1523 vtn_assert(ptr
->offset
&& ptr
->block_index
);
1524 return nir_vec2(&b
->nb
, ptr
->block_index
, ptr
->offset
);
1527 struct vtn_pointer
*
1528 vtn_pointer_from_ssa(struct vtn_builder
*b
, nir_ssa_def
*ssa
,
1529 struct vtn_type
*ptr_type
)
1531 vtn_assert(ssa
->num_components
== 2 && ssa
->bit_size
== 32);
1532 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1533 vtn_assert(ptr_type
->deref
->base_type
!= vtn_base_type_pointer
);
1534 /* This pointer type needs to have actual storage */
1535 vtn_assert(ptr_type
->type
);
1537 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
1538 ptr
->mode
= vtn_storage_class_to_mode(b
, ptr_type
->storage_class
,
1540 ptr
->type
= ptr_type
->deref
;
1541 ptr
->ptr_type
= ptr_type
;
1542 ptr
->block_index
= nir_channel(&b
->nb
, ssa
, 0);
1543 ptr
->offset
= nir_channel(&b
->nb
, ssa
, 1);
1549 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
1551 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
1554 if (var
->mode
== vtn_variable_mode_input
) {
1555 return stage
== MESA_SHADER_TESS_CTRL
||
1556 stage
== MESA_SHADER_TESS_EVAL
||
1557 stage
== MESA_SHADER_GEOMETRY
;
1560 if (var
->mode
== vtn_variable_mode_output
)
1561 return stage
== MESA_SHADER_TESS_CTRL
;
1567 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
1568 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
1569 nir_constant
*initializer
)
1571 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1572 struct vtn_type
*type
= ptr_type
->deref
;
1574 struct vtn_type
*without_array
= type
;
1575 while(glsl_type_is_array(without_array
->type
))
1576 without_array
= without_array
->array_element
;
1578 enum vtn_variable_mode mode
;
1579 nir_variable_mode nir_mode
;
1580 mode
= vtn_storage_class_to_mode(b
, storage_class
, without_array
, &nir_mode
);
1583 case vtn_variable_mode_ubo
:
1584 b
->shader
->info
.num_ubos
++;
1586 case vtn_variable_mode_ssbo
:
1587 b
->shader
->info
.num_ssbos
++;
1589 case vtn_variable_mode_image
:
1590 b
->shader
->info
.num_images
++;
1592 case vtn_variable_mode_sampler
:
1593 b
->shader
->info
.num_textures
++;
1595 case vtn_variable_mode_push_constant
:
1596 b
->shader
->num_uniforms
= vtn_type_block_size(b
, type
);
1599 /* No tallying is needed */
1603 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
1607 vtn_assert(val
->value_type
== vtn_value_type_pointer
);
1608 val
->pointer
= vtn_pointer_for_variable(b
, var
, ptr_type
);
1610 switch (var
->mode
) {
1611 case vtn_variable_mode_local
:
1612 case vtn_variable_mode_global
:
1613 case vtn_variable_mode_image
:
1614 case vtn_variable_mode_sampler
:
1615 case vtn_variable_mode_workgroup
:
1616 /* For these, we create the variable normally */
1617 var
->var
= rzalloc(b
->shader
, nir_variable
);
1618 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1619 var
->var
->type
= var
->type
->type
;
1620 var
->var
->data
.mode
= nir_mode
;
1622 switch (var
->mode
) {
1623 case vtn_variable_mode_image
:
1624 case vtn_variable_mode_sampler
:
1625 var
->var
->interface_type
= without_array
->type
;
1628 var
->var
->interface_type
= NULL
;
1633 case vtn_variable_mode_input
:
1634 case vtn_variable_mode_output
: {
1635 /* In order to know whether or not we're a per-vertex inout, we need
1636 * the patch qualifier. This means walking the variable decorations
1637 * early before we actually create any variables. Not a big deal.
1639 * GLSLang really likes to place decorations in the most interior
1640 * thing it possibly can. In particular, if you have a struct, it
1641 * will place the patch decorations on the struct members. This
1642 * should be handled by the variable splitting below just fine.
1644 * If you have an array-of-struct, things get even more weird as it
1645 * will place the patch decorations on the struct even though it's
1646 * inside an array and some of the members being patch and others not
1647 * makes no sense whatsoever. Since the only sensible thing is for
1648 * it to be all or nothing, we'll call it patch if any of the members
1649 * are declared patch.
1652 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
1653 if (glsl_type_is_array(var
->type
->type
) &&
1654 glsl_type_is_struct(without_array
->type
)) {
1655 vtn_foreach_decoration(b
, without_array
->val
,
1656 var_is_patch_cb
, &var
->patch
);
1659 /* For inputs and outputs, we immediately split structures. This
1660 * is for a couple of reasons. For one, builtins may all come in
1661 * a struct and we really want those split out into separate
1662 * variables. For another, interpolation qualifiers can be
1663 * applied to members of the top-level struct ane we need to be
1664 * able to preserve that information.
1667 int array_length
= -1;
1668 struct vtn_type
*interface_type
= var
->type
;
1669 if (is_per_vertex_inout(var
, b
->shader
->info
.stage
)) {
1670 /* In Geometry shaders (and some tessellation), inputs come
1671 * in per-vertex arrays. However, some builtins come in
1672 * non-per-vertex, hence the need for the is_array check. In
1673 * any case, there are no non-builtin arrays allowed so this
1674 * check should be sufficient.
1676 interface_type
= var
->type
->array_element
;
1677 array_length
= glsl_get_length(var
->type
->type
);
1680 if (glsl_type_is_struct(interface_type
->type
)) {
1681 /* It's a struct. Split it. */
1682 unsigned num_members
= glsl_get_length(interface_type
->type
);
1683 var
->members
= ralloc_array(b
, nir_variable
*, num_members
);
1685 for (unsigned i
= 0; i
< num_members
; i
++) {
1686 const struct glsl_type
*mtype
= interface_type
->members
[i
]->type
;
1687 if (array_length
>= 0)
1688 mtype
= glsl_array_type(mtype
, array_length
);
1690 var
->members
[i
] = rzalloc(b
->shader
, nir_variable
);
1691 var
->members
[i
]->name
=
1692 ralloc_asprintf(var
->members
[i
], "%s.%d", val
->name
, i
);
1693 var
->members
[i
]->type
= mtype
;
1694 var
->members
[i
]->interface_type
=
1695 interface_type
->members
[i
]->type
;
1696 var
->members
[i
]->data
.mode
= nir_mode
;
1697 var
->members
[i
]->data
.patch
= var
->patch
;
1700 var
->var
= rzalloc(b
->shader
, nir_variable
);
1701 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1702 var
->var
->type
= var
->type
->type
;
1703 var
->var
->interface_type
= interface_type
->type
;
1704 var
->var
->data
.mode
= nir_mode
;
1705 var
->var
->data
.patch
= var
->patch
;
1708 /* For inputs and outputs, we need to grab locations and builtin
1709 * information from the interface type.
1711 vtn_foreach_decoration(b
, interface_type
->val
, var_decoration_cb
, var
);
1715 case vtn_variable_mode_param
:
1716 vtn_fail("Not created through OpVariable");
1718 case vtn_variable_mode_ubo
:
1719 case vtn_variable_mode_ssbo
:
1720 case vtn_variable_mode_push_constant
:
1721 /* These don't need actual variables. */
1726 var
->var
->constant_initializer
=
1727 nir_constant_clone(initializer
, var
->var
);
1730 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
1732 if (var
->mode
== vtn_variable_mode_image
||
1733 var
->mode
== vtn_variable_mode_sampler
) {
1734 /* XXX: We still need the binding information in the nir_variable
1735 * for these. We should fix that.
1737 var
->var
->data
.binding
= var
->binding
;
1738 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
1739 var
->var
->data
.index
= var
->input_attachment_index
;
1741 if (var
->mode
== vtn_variable_mode_image
)
1742 var
->var
->data
.image
.format
= without_array
->image_format
;
1745 if (var
->mode
== vtn_variable_mode_local
) {
1746 vtn_assert(var
->members
== NULL
&& var
->var
!= NULL
);
1747 nir_function_impl_add_variable(b
->nb
.impl
, var
->var
);
1748 } else if (var
->var
) {
1749 nir_shader_add_variable(b
->shader
, var
->var
);
1750 } else if (var
->members
) {
1751 unsigned count
= glsl_get_length(without_array
->type
);
1752 for (unsigned i
= 0; i
< count
; i
++) {
1753 vtn_assert(var
->members
[i
]->data
.mode
!= nir_var_local
);
1754 nir_shader_add_variable(b
->shader
, var
->members
[i
]);
1757 vtn_assert(vtn_pointer_is_external_block(b
, val
->pointer
));
1762 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
1763 const uint32_t *w
, unsigned count
)
1767 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1768 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1772 case SpvOpVariable
: {
1773 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1775 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1777 SpvStorageClass storage_class
= w
[3];
1778 nir_constant
*initializer
= NULL
;
1780 initializer
= vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
1782 vtn_create_variable(b
, val
, ptr_type
, storage_class
, initializer
);
1786 case SpvOpAccessChain
:
1787 case SpvOpPtrAccessChain
:
1788 case SpvOpInBoundsAccessChain
: {
1789 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
1790 chain
->ptr_as_array
= (opcode
== SpvOpPtrAccessChain
);
1793 for (int i
= 4; i
< count
; i
++) {
1794 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
1795 if (link_val
->value_type
== vtn_value_type_constant
) {
1796 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
1797 chain
->link
[idx
].id
= link_val
->constant
->values
[0].u32
[0];
1799 chain
->link
[idx
].mode
= vtn_access_mode_id
;
1800 chain
->link
[idx
].id
= w
[i
];
1806 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1807 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
1808 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1809 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1810 * to combine an array of images with a single sampler to get an
1811 * array of sampled images that all share the same sampler.
1812 * Fortunately, this means that we can more-or-less ignore the
1813 * sampler when crawling the access chain, but it does leave us
1814 * with this rather awkward little special-case.
1816 struct vtn_value
*val
=
1817 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1818 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1819 val
->sampled_image
->type
= base_val
->sampled_image
->type
;
1820 val
->sampled_image
->image
=
1821 vtn_pointer_dereference(b
, base_val
->sampled_image
->image
, chain
);
1822 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
1824 vtn_assert(base_val
->value_type
== vtn_value_type_pointer
);
1825 struct vtn_value
*val
=
1826 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1827 val
->pointer
= vtn_pointer_dereference(b
, base_val
->pointer
, chain
);
1828 val
->pointer
->ptr_type
= ptr_type
;
1833 case SpvOpCopyMemory
: {
1834 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
1835 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
1837 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
1842 struct vtn_type
*res_type
=
1843 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1844 struct vtn_pointer
*src
=
1845 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1847 if (src
->mode
== vtn_variable_mode_image
||
1848 src
->mode
== vtn_variable_mode_sampler
) {
1849 vtn_push_value(b
, w
[2], vtn_value_type_pointer
)->pointer
= src
;
1853 vtn_push_ssa(b
, w
[2], res_type
, vtn_variable_load(b
, src
));
1858 struct vtn_pointer
*dest
=
1859 vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
1861 if (glsl_type_is_sampler(dest
->type
->type
)) {
1862 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
1863 "propagation to workaround the problem.");
1864 vtn_assert(dest
->var
->copy_prop_sampler
== NULL
);
1865 dest
->var
->copy_prop_sampler
=
1866 vtn_value(b
, w
[2], vtn_value_type_pointer
)->pointer
;
1870 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
1871 vtn_variable_store(b
, src
, dest
);
1875 case SpvOpArrayLength
: {
1876 struct vtn_pointer
*ptr
=
1877 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1879 const uint32_t offset
= ptr
->var
->type
->offsets
[w
[4]];
1880 const uint32_t stride
= ptr
->var
->type
->members
[w
[4]]->stride
;
1882 if (!ptr
->block_index
) {
1883 struct vtn_access_chain chain
= {
1886 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
1887 vtn_assert(ptr
->block_index
);
1890 nir_intrinsic_instr
*instr
=
1891 nir_intrinsic_instr_create(b
->nb
.shader
,
1892 nir_intrinsic_get_buffer_size
);
1893 instr
->src
[0] = nir_src_for_ssa(ptr
->block_index
);
1894 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
1895 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
1896 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
1898 /* array_length = max(buffer_size - offset, 0) / stride */
1899 nir_ssa_def
*array_length
=
1904 nir_imm_int(&b
->nb
, offset
)),
1905 nir_imm_int(&b
->nb
, 0u)),
1906 nir_imm_int(&b
->nb
, stride
));
1908 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1909 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
1910 val
->ssa
->def
= array_length
;
1914 case SpvOpCopyMemorySized
:
1916 vtn_fail("Unhandled opcode");