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
];
60 /* Dereference the given base pointer by the access chain */
61 static struct vtn_pointer
*
62 vtn_access_chain_pointer_dereference(struct vtn_builder
*b
,
63 struct vtn_pointer
*base
,
64 struct vtn_access_chain
*deref_chain
)
66 struct vtn_access_chain
*chain
=
67 vtn_access_chain_extend(b
, base
->chain
, deref_chain
->length
);
68 struct vtn_type
*type
= base
->type
;
70 /* OpPtrAccessChain is only allowed on things which support variable
71 * pointers. For everything else, the client is expected to just pass us
72 * the right access chain.
74 vtn_assert(!deref_chain
->ptr_as_array
);
76 unsigned start
= base
->chain
? base
->chain
->length
: 0;
77 for (unsigned i
= 0; i
< deref_chain
->length
; i
++) {
78 chain
->link
[start
+ i
] = deref_chain
->link
[i
];
80 if (glsl_type_is_struct(type
->type
)) {
81 vtn_assert(deref_chain
->link
[i
].mode
== vtn_access_mode_literal
);
82 type
= type
->members
[deref_chain
->link
[i
].id
];
84 type
= type
->array_element
;
88 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
89 ptr
->mode
= base
->mode
;
98 vtn_access_link_as_ssa(struct vtn_builder
*b
, struct vtn_access_link link
,
101 vtn_assert(stride
> 0);
102 if (link
.mode
== vtn_access_mode_literal
) {
103 return nir_imm_int(&b
->nb
, link
.id
* stride
);
104 } else if (stride
== 1) {
105 nir_ssa_def
*ssa
= vtn_ssa_value(b
, link
.id
)->def
;
106 if (ssa
->bit_size
!= 32)
107 ssa
= nir_u2u32(&b
->nb
, ssa
);
110 nir_ssa_def
*src0
= vtn_ssa_value(b
, link
.id
)->def
;
111 if (src0
->bit_size
!= 32)
112 src0
= nir_u2u32(&b
->nb
, src0
);
113 return nir_imul(&b
->nb
, src0
, nir_imm_int(&b
->nb
, stride
));
118 vtn_variable_resource_index(struct vtn_builder
*b
, struct vtn_variable
*var
,
119 nir_ssa_def
*desc_array_index
)
121 if (!desc_array_index
) {
122 vtn_assert(glsl_type_is_struct(var
->type
->type
));
123 desc_array_index
= nir_imm_int(&b
->nb
, 0);
126 nir_intrinsic_instr
*instr
=
127 nir_intrinsic_instr_create(b
->nb
.shader
,
128 nir_intrinsic_vulkan_resource_index
);
129 instr
->src
[0] = nir_src_for_ssa(desc_array_index
);
130 nir_intrinsic_set_desc_set(instr
, var
->descriptor_set
);
131 nir_intrinsic_set_binding(instr
, var
->binding
);
133 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
134 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
136 return &instr
->dest
.ssa
;
139 static struct vtn_pointer
*
140 vtn_ssa_offset_pointer_dereference(struct vtn_builder
*b
,
141 struct vtn_pointer
*base
,
142 struct vtn_access_chain
*deref_chain
)
144 nir_ssa_def
*block_index
= base
->block_index
;
145 nir_ssa_def
*offset
= base
->offset
;
146 struct vtn_type
*type
= base
->type
;
149 if (deref_chain
->ptr_as_array
) {
150 /* We need ptr_type for the stride */
151 vtn_assert(base
->ptr_type
);
152 /* This must be a pointer to an actual element somewhere */
153 vtn_assert(block_index
&& offset
);
154 /* We need at least one element in the chain */
155 vtn_assert(deref_chain
->length
>= 1);
157 nir_ssa_def
*elem_offset
=
158 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
159 base
->ptr_type
->stride
);
160 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
165 vtn_assert(base
->var
);
166 if (glsl_type_is_array(type
->type
)) {
167 /* We need at least one element in the chain */
168 vtn_assert(deref_chain
->length
>= 1);
170 nir_ssa_def
*desc_arr_idx
=
171 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1);
172 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
173 type
= type
->array_element
;
176 block_index
= vtn_variable_resource_index(b
, base
->var
, NULL
);
179 /* This is the first access chain so we also need an offset */
181 offset
= nir_imm_int(&b
->nb
, 0);
185 for (; idx
< deref_chain
->length
; idx
++) {
186 switch (glsl_get_base_type(type
->type
)) {
189 case GLSL_TYPE_UINT64
:
190 case GLSL_TYPE_INT64
:
191 case GLSL_TYPE_FLOAT
:
192 case GLSL_TYPE_DOUBLE
:
194 case GLSL_TYPE_ARRAY
: {
195 nir_ssa_def
*elem_offset
=
196 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
], type
->stride
);
197 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
198 type
= type
->array_element
;
202 case GLSL_TYPE_STRUCT
: {
203 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
204 unsigned member
= deref_chain
->link
[idx
].id
;
205 nir_ssa_def
*mem_offset
= nir_imm_int(&b
->nb
, type
->offsets
[member
]);
206 offset
= nir_iadd(&b
->nb
, offset
, mem_offset
);
207 type
= type
->members
[member
];
212 vtn_fail("Invalid type for deref");
216 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
217 ptr
->mode
= base
->mode
;
219 ptr
->block_index
= block_index
;
220 ptr
->offset
= offset
;
225 /* Dereference the given base pointer by the access chain */
226 static struct vtn_pointer
*
227 vtn_pointer_dereference(struct vtn_builder
*b
,
228 struct vtn_pointer
*base
,
229 struct vtn_access_chain
*deref_chain
)
231 if (vtn_pointer_uses_ssa_offset(base
)) {
232 return vtn_ssa_offset_pointer_dereference(b
, base
, deref_chain
);
234 return vtn_access_chain_pointer_dereference(b
, base
, deref_chain
);
238 /* Crawls a chain of array derefs and rewrites the types so that the
239 * lengths stay the same but the terminal type is the one given by
240 * tail_type. This is useful for split structures.
243 rewrite_deref_types(struct vtn_builder
*b
, nir_deref
*deref
,
244 const struct glsl_type
*type
)
248 vtn_assert(deref
->child
->deref_type
== nir_deref_type_array
);
249 vtn_assert(glsl_type_is_array(deref
->type
));
250 rewrite_deref_types(b
, deref
->child
, glsl_get_array_element(type
));
255 vtn_pointer_for_variable(struct vtn_builder
*b
,
256 struct vtn_variable
*var
, struct vtn_type
*ptr_type
)
258 struct vtn_pointer
*pointer
= rzalloc(b
, struct vtn_pointer
);
260 pointer
->mode
= var
->mode
;
261 pointer
->type
= var
->type
;
262 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
263 vtn_assert(ptr_type
->deref
->type
== var
->type
->type
);
264 pointer
->ptr_type
= ptr_type
;
271 vtn_pointer_to_deref(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
273 /* Do on-the-fly copy propagation for samplers. */
274 if (ptr
->var
->copy_prop_sampler
)
275 return vtn_pointer_to_deref(b
, ptr
->var
->copy_prop_sampler
);
277 nir_deref_var
*deref_var
;
279 deref_var
= nir_deref_var_create(b
, ptr
->var
->var
);
280 /* Raw variable access */
284 vtn_assert(ptr
->var
->members
);
285 /* Create the deref_var manually. It will get filled out later. */
286 deref_var
= rzalloc(b
, nir_deref_var
);
287 deref_var
->deref
.deref_type
= nir_deref_type_var
;
290 struct vtn_access_chain
*chain
= ptr
->chain
;
293 struct vtn_type
*deref_type
= ptr
->var
->type
;
294 nir_deref
*tail
= &deref_var
->deref
;
295 nir_variable
**members
= ptr
->var
->members
;
297 for (unsigned i
= 0; i
< chain
->length
; i
++) {
298 enum glsl_base_type base_type
= glsl_get_base_type(deref_type
->type
);
302 case GLSL_TYPE_UINT64
:
303 case GLSL_TYPE_INT64
:
304 case GLSL_TYPE_FLOAT
:
305 case GLSL_TYPE_DOUBLE
:
307 case GLSL_TYPE_ARRAY
: {
308 deref_type
= deref_type
->array_element
;
310 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
311 deref_arr
->deref
.type
= deref_type
->type
;
313 if (chain
->link
[i
].mode
== vtn_access_mode_literal
) {
314 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
315 deref_arr
->base_offset
= chain
->link
[i
].id
;
317 vtn_assert(chain
->link
[i
].mode
== vtn_access_mode_id
);
318 deref_arr
->deref_array_type
= nir_deref_array_type_indirect
;
319 deref_arr
->base_offset
= 0;
320 deref_arr
->indirect
=
321 nir_src_for_ssa(vtn_ssa_value(b
, chain
->link
[i
].id
)->def
);
323 tail
->child
= &deref_arr
->deref
;
328 case GLSL_TYPE_STRUCT
: {
329 vtn_assert(chain
->link
[i
].mode
== vtn_access_mode_literal
);
330 unsigned idx
= chain
->link
[i
].id
;
331 deref_type
= deref_type
->members
[idx
];
333 /* This is a pre-split structure. */
334 deref_var
->var
= members
[idx
];
335 rewrite_deref_types(b
, &deref_var
->deref
, members
[idx
]->type
);
336 vtn_assert(tail
->type
== deref_type
->type
);
339 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, idx
);
340 deref_struct
->deref
.type
= deref_type
->type
;
341 tail
->child
= &deref_struct
->deref
;
347 vtn_fail("Invalid type for deref");
351 vtn_assert(members
== NULL
);
356 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_var
*deref
,
357 nir_deref
*tail
, struct vtn_ssa_value
*inout
)
359 /* The deref tail may contain a deref to select a component of a vector (in
360 * other words, it might not be an actual tail) so we have to save it away
361 * here since we overwrite it later.
363 nir_deref
*old_child
= tail
->child
;
365 if (glsl_type_is_vector_or_scalar(tail
->type
)) {
366 /* Terminate the deref chain in case there is one more link to pick
367 * off a component of the vector.
371 nir_intrinsic_op op
= load
? nir_intrinsic_load_var
:
372 nir_intrinsic_store_var
;
374 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
375 intrin
->variables
[0] = nir_deref_var_clone(deref
, intrin
);
376 intrin
->num_components
= glsl_get_vector_elements(tail
->type
);
379 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
380 intrin
->num_components
,
381 glsl_get_bit_size(tail
->type
),
383 inout
->def
= &intrin
->dest
.ssa
;
385 nir_intrinsic_set_write_mask(intrin
, (1 << intrin
->num_components
) - 1);
386 intrin
->src
[0] = nir_src_for_ssa(inout
->def
);
389 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
390 } else if (glsl_get_base_type(tail
->type
) == GLSL_TYPE_ARRAY
||
391 glsl_type_is_matrix(tail
->type
)) {
392 unsigned elems
= glsl_get_length(tail
->type
);
393 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
394 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
395 deref_arr
->deref
.type
= glsl_get_array_element(tail
->type
);
396 tail
->child
= &deref_arr
->deref
;
397 for (unsigned i
= 0; i
< elems
; i
++) {
398 deref_arr
->base_offset
= i
;
399 _vtn_local_load_store(b
, load
, deref
, tail
->child
, inout
->elems
[i
]);
402 vtn_assert(glsl_get_base_type(tail
->type
) == GLSL_TYPE_STRUCT
);
403 unsigned elems
= glsl_get_length(tail
->type
);
404 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, 0);
405 tail
->child
= &deref_struct
->deref
;
406 for (unsigned i
= 0; i
< elems
; i
++) {
407 deref_struct
->index
= i
;
408 deref_struct
->deref
.type
= glsl_get_struct_field(tail
->type
, i
);
409 _vtn_local_load_store(b
, load
, deref
, tail
->child
, inout
->elems
[i
]);
413 tail
->child
= old_child
;
417 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
419 struct vtn_pointer
*ptr
= vtn_value(b
, id
, vtn_value_type_pointer
)->pointer
;
420 return vtn_pointer_to_deref(b
, ptr
);
424 * Gets the NIR-level deref tail, which may have as a child an array deref
425 * selecting which component due to OpAccessChain supporting per-component
426 * indexing in SPIR-V.
429 get_deref_tail(nir_deref_var
*deref
)
431 nir_deref
*cur
= &deref
->deref
;
432 while (!glsl_type_is_vector_or_scalar(cur
->type
) && cur
->child
)
438 struct vtn_ssa_value
*
439 vtn_local_load(struct vtn_builder
*b
, nir_deref_var
*src
)
441 nir_deref
*src_tail
= get_deref_tail(src
);
442 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
443 _vtn_local_load_store(b
, true, src
, src_tail
, val
);
445 if (src_tail
->child
) {
446 nir_deref_array
*vec_deref
= nir_deref_as_array(src_tail
->child
);
447 vtn_assert(vec_deref
->deref
.child
== NULL
);
448 val
->type
= vec_deref
->deref
.type
;
449 if (vec_deref
->deref_array_type
== nir_deref_array_type_direct
)
450 val
->def
= vtn_vector_extract(b
, val
->def
, vec_deref
->base_offset
);
452 val
->def
= vtn_vector_extract_dynamic(b
, val
->def
,
453 vec_deref
->indirect
.ssa
);
460 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
463 nir_deref
*dest_tail
= get_deref_tail(dest
);
465 if (dest_tail
->child
) {
466 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
467 _vtn_local_load_store(b
, true, dest
, dest_tail
, val
);
468 nir_deref_array
*deref
= nir_deref_as_array(dest_tail
->child
);
469 vtn_assert(deref
->deref
.child
== NULL
);
470 if (deref
->deref_array_type
== nir_deref_array_type_direct
)
471 val
->def
= vtn_vector_insert(b
, val
->def
, src
->def
,
474 val
->def
= vtn_vector_insert_dynamic(b
, val
->def
, src
->def
,
475 deref
->indirect
.ssa
);
476 _vtn_local_load_store(b
, false, dest
, dest_tail
, val
);
478 _vtn_local_load_store(b
, false, dest
, dest_tail
, src
);
483 get_vulkan_resource_index(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
484 struct vtn_type
**type
, unsigned *chain_idx
)
486 /* Push constants have no explicit binding */
487 if (ptr
->mode
== vtn_variable_mode_push_constant
) {
489 *type
= ptr
->var
->type
;
493 if (glsl_type_is_array(ptr
->var
->type
->type
)) {
494 vtn_assert(ptr
->chain
->length
> 0);
495 nir_ssa_def
*desc_array_index
=
496 vtn_access_link_as_ssa(b
, ptr
->chain
->link
[0], 1);
498 *type
= ptr
->var
->type
->array_element
;
499 return vtn_variable_resource_index(b
, ptr
->var
, desc_array_index
);
502 *type
= ptr
->var
->type
;
503 return vtn_variable_resource_index(b
, ptr
->var
, NULL
);
508 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
509 nir_ssa_def
**index_out
, unsigned *end_idx_out
)
512 vtn_assert(ptr
->block_index
);
513 *index_out
= ptr
->block_index
;
518 struct vtn_type
*type
;
519 *index_out
= get_vulkan_resource_index(b
, ptr
, &type
, &idx
);
521 nir_ssa_def
*offset
= nir_imm_int(&b
->nb
, 0);
522 for (; idx
< ptr
->chain
->length
; idx
++) {
523 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
527 case GLSL_TYPE_UINT64
:
528 case GLSL_TYPE_INT64
:
529 case GLSL_TYPE_FLOAT
:
530 case GLSL_TYPE_DOUBLE
:
532 case GLSL_TYPE_ARRAY
:
533 offset
= nir_iadd(&b
->nb
, offset
,
534 vtn_access_link_as_ssa(b
, ptr
->chain
->link
[idx
],
537 type
= type
->array_element
;
540 case GLSL_TYPE_STRUCT
: {
541 vtn_assert(ptr
->chain
->link
[idx
].mode
== vtn_access_mode_literal
);
542 unsigned member
= ptr
->chain
->link
[idx
].id
;
543 offset
= nir_iadd(&b
->nb
, offset
,
544 nir_imm_int(&b
->nb
, type
->offsets
[member
]));
545 type
= type
->members
[member
];
550 vtn_fail("Invalid type for deref");
554 vtn_assert(type
== ptr
->type
);
561 /* Tries to compute the size of an interface block based on the strides and
562 * offsets that are provided to us in the SPIR-V source.
565 vtn_type_block_size(struct vtn_builder
*b
, struct vtn_type
*type
)
567 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
571 case GLSL_TYPE_UINT64
:
572 case GLSL_TYPE_INT64
:
573 case GLSL_TYPE_FLOAT
:
575 case GLSL_TYPE_DOUBLE
: {
576 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
577 glsl_get_matrix_columns(type
->type
);
579 vtn_assert(type
->stride
> 0);
580 return type
->stride
* cols
;
581 } else if (base_type
== GLSL_TYPE_DOUBLE
||
582 base_type
== GLSL_TYPE_UINT64
||
583 base_type
== GLSL_TYPE_INT64
) {
584 return glsl_get_vector_elements(type
->type
) * 8;
586 return glsl_get_vector_elements(type
->type
) * 4;
590 case GLSL_TYPE_STRUCT
:
591 case GLSL_TYPE_INTERFACE
: {
593 unsigned num_fields
= glsl_get_length(type
->type
);
594 for (unsigned f
= 0; f
< num_fields
; f
++) {
595 unsigned field_end
= type
->offsets
[f
] +
596 vtn_type_block_size(b
, type
->members
[f
]);
597 size
= MAX2(size
, field_end
);
602 case GLSL_TYPE_ARRAY
:
603 vtn_assert(type
->stride
> 0);
604 vtn_assert(glsl_get_length(type
->type
) > 0);
605 return type
->stride
* glsl_get_length(type
->type
);
608 vtn_fail("Invalid block type");
614 vtn_access_chain_get_offset_size(struct vtn_builder
*b
,
615 struct vtn_access_chain
*chain
,
616 struct vtn_type
*type
,
617 unsigned *access_offset
,
618 unsigned *access_size
)
622 for (unsigned i
= 0; i
< chain
->length
; i
++) {
623 if (chain
->link
[i
].mode
!= vtn_access_mode_literal
)
626 if (glsl_type_is_struct(type
->type
)) {
627 *access_offset
+= type
->offsets
[chain
->link
[i
].id
];
628 type
= type
->members
[chain
->link
[i
].id
];
630 *access_offset
+= type
->stride
* chain
->link
[i
].id
;
631 type
= type
->array_element
;
635 *access_size
= vtn_type_block_size(b
, type
);
639 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
640 nir_ssa_def
*index
, nir_ssa_def
*offset
,
641 unsigned access_offset
, unsigned access_size
,
642 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
)
644 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
645 instr
->num_components
= glsl_get_vector_elements(type
);
649 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
650 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
653 if (op
== nir_intrinsic_load_push_constant
) {
654 vtn_assert(access_offset
% 4 == 0);
656 nir_intrinsic_set_base(instr
, access_offset
);
657 nir_intrinsic_set_range(instr
, access_size
);
661 instr
->src
[src
++] = nir_src_for_ssa(index
);
663 if (op
== nir_intrinsic_load_push_constant
) {
664 /* We need to subtract the offset from where the intrinsic will load the
667 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
668 nir_imm_int(&b
->nb
, access_offset
)));
670 instr
->src
[src
++] = nir_src_for_ssa(offset
);
674 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
675 instr
->num_components
,
676 glsl_get_bit_size(type
), NULL
);
677 (*inout
)->def
= &instr
->dest
.ssa
;
680 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
682 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
683 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
687 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
688 nir_ssa_def
*index
, nir_ssa_def
*offset
,
689 unsigned access_offset
, unsigned access_size
,
690 struct vtn_access_chain
*chain
, unsigned chain_idx
,
691 struct vtn_type
*type
, struct vtn_ssa_value
**inout
)
693 if (chain
&& chain_idx
>= chain
->length
)
696 if (load
&& chain
== NULL
&& *inout
== NULL
)
697 *inout
= vtn_create_ssa_value(b
, type
->type
);
699 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
703 case GLSL_TYPE_UINT64
:
704 case GLSL_TYPE_INT64
:
705 case GLSL_TYPE_FLOAT
:
706 case GLSL_TYPE_DOUBLE
:
708 /* This is where things get interesting. At this point, we've hit
709 * a vector, a scalar, or a matrix.
711 if (glsl_type_is_matrix(type
->type
)) {
712 /* Loading the whole matrix */
713 struct vtn_ssa_value
*transpose
;
714 unsigned num_ops
, vec_width
, col_stride
;
715 if (type
->row_major
) {
716 num_ops
= glsl_get_vector_elements(type
->type
);
717 vec_width
= glsl_get_matrix_columns(type
->type
);
718 col_stride
= type
->array_element
->stride
;
720 const struct glsl_type
*transpose_type
=
721 glsl_matrix_type(base_type
, vec_width
, num_ops
);
722 *inout
= vtn_create_ssa_value(b
, transpose_type
);
724 transpose
= vtn_ssa_transpose(b
, *inout
);
728 num_ops
= glsl_get_matrix_columns(type
->type
);
729 vec_width
= glsl_get_vector_elements(type
->type
);
730 col_stride
= type
->stride
;
733 for (unsigned i
= 0; i
< num_ops
; i
++) {
734 nir_ssa_def
*elem_offset
=
735 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* col_stride
));
736 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
737 access_offset
, access_size
,
739 glsl_vector_type(base_type
, vec_width
));
742 if (load
&& type
->row_major
)
743 *inout
= vtn_ssa_transpose(b
, *inout
);
745 unsigned elems
= glsl_get_vector_elements(type
->type
);
746 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
747 if (elems
== 1 || type
->stride
== type_size
) {
748 /* This is a tightly-packed normal scalar or vector load */
749 vtn_assert(glsl_type_is_vector_or_scalar(type
->type
));
750 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
751 access_offset
, access_size
,
754 /* This is a strided load. We have to load N things separately.
755 * This is the single column of a row-major matrix case.
757 vtn_assert(type
->stride
> type_size
);
758 vtn_assert(type
->stride
% type_size
== 0);
760 nir_ssa_def
*per_comp
[4];
761 for (unsigned i
= 0; i
< elems
; i
++) {
762 nir_ssa_def
*elem_offset
=
763 nir_iadd(&b
->nb
, offset
,
764 nir_imm_int(&b
->nb
, i
* type
->stride
));
765 struct vtn_ssa_value
*comp
, temp_val
;
767 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
768 temp_val
.type
= glsl_scalar_type(base_type
);
771 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
772 access_offset
, access_size
,
773 &comp
, glsl_scalar_type(base_type
));
774 per_comp
[i
] = comp
->def
;
779 *inout
= vtn_create_ssa_value(b
, type
->type
);
780 (*inout
)->def
= nir_vec(&b
->nb
, per_comp
, elems
);
786 case GLSL_TYPE_ARRAY
: {
787 unsigned elems
= glsl_get_length(type
->type
);
788 for (unsigned i
= 0; i
< elems
; i
++) {
789 nir_ssa_def
*elem_off
=
790 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* type
->stride
));
791 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
792 access_offset
, access_size
,
794 type
->array_element
, &(*inout
)->elems
[i
]);
799 case GLSL_TYPE_STRUCT
: {
800 unsigned elems
= glsl_get_length(type
->type
);
801 for (unsigned i
= 0; i
< elems
; i
++) {
802 nir_ssa_def
*elem_off
=
803 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, type
->offsets
[i
]));
804 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
805 access_offset
, access_size
,
807 type
->members
[i
], &(*inout
)->elems
[i
]);
813 vtn_fail("Invalid block member type");
817 static struct vtn_ssa_value
*
818 vtn_block_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
821 unsigned access_offset
= 0, access_size
= 0;
823 case vtn_variable_mode_ubo
:
824 op
= nir_intrinsic_load_ubo
;
826 case vtn_variable_mode_ssbo
:
827 op
= nir_intrinsic_load_ssbo
;
829 case vtn_variable_mode_push_constant
:
830 op
= nir_intrinsic_load_push_constant
;
831 vtn_access_chain_get_offset_size(b
, src
->chain
, src
->var
->type
,
832 &access_offset
, &access_size
);
835 vtn_fail("Invalid block variable mode");
838 nir_ssa_def
*offset
, *index
= NULL
;
840 offset
= vtn_pointer_to_offset(b
, src
, &index
, &chain_idx
);
842 struct vtn_ssa_value
*value
= NULL
;
843 _vtn_block_load_store(b
, op
, true, index
, offset
,
844 access_offset
, access_size
,
845 src
->chain
, chain_idx
, src
->type
, &value
);
850 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
851 struct vtn_pointer
*dst
)
853 nir_ssa_def
*offset
, *index
= NULL
;
855 offset
= vtn_pointer_to_offset(b
, dst
, &index
, &chain_idx
);
857 _vtn_block_load_store(b
, nir_intrinsic_store_ssbo
, false, index
, offset
,
858 0, 0, dst
->chain
, chain_idx
, dst
->type
, &src
);
862 vtn_pointer_is_external_block(struct vtn_pointer
*ptr
)
864 return ptr
->mode
== vtn_variable_mode_ssbo
||
865 ptr
->mode
== vtn_variable_mode_ubo
||
866 ptr
->mode
== vtn_variable_mode_push_constant
;
870 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
871 struct vtn_pointer
*ptr
,
872 struct vtn_ssa_value
**inout
)
874 enum glsl_base_type base_type
= glsl_get_base_type(ptr
->type
->type
);
878 case GLSL_TYPE_UINT64
:
879 case GLSL_TYPE_INT64
:
880 case GLSL_TYPE_FLOAT
:
882 case GLSL_TYPE_DOUBLE
:
883 /* At this point, we have a scalar, vector, or matrix so we know that
884 * there cannot be any structure splitting still in the way. By
885 * stopping at the matrix level rather than the vector level, we
886 * ensure that matrices get loaded in the optimal way even if they
887 * are storred row-major in a UBO.
890 *inout
= vtn_local_load(b
, vtn_pointer_to_deref(b
, ptr
));
892 vtn_local_store(b
, *inout
, vtn_pointer_to_deref(b
, ptr
));
896 case GLSL_TYPE_ARRAY
:
897 case GLSL_TYPE_STRUCT
: {
898 unsigned elems
= glsl_get_length(ptr
->type
->type
);
900 vtn_assert(*inout
== NULL
);
901 *inout
= rzalloc(b
, struct vtn_ssa_value
);
902 (*inout
)->type
= ptr
->type
->type
;
903 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
906 struct vtn_access_chain chain
= {
909 { .mode
= vtn_access_mode_literal
, },
912 for (unsigned i
= 0; i
< elems
; i
++) {
913 chain
.link
[0].id
= i
;
914 struct vtn_pointer
*elem
= vtn_pointer_dereference(b
, ptr
, &chain
);
915 _vtn_variable_load_store(b
, load
, elem
, &(*inout
)->elems
[i
]);
921 vtn_fail("Invalid access chain type");
925 struct vtn_ssa_value
*
926 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
928 if (vtn_pointer_is_external_block(src
)) {
929 return vtn_block_load(b
, src
);
931 struct vtn_ssa_value
*val
= NULL
;
932 _vtn_variable_load_store(b
, true, src
, &val
);
938 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
939 struct vtn_pointer
*dest
)
941 if (vtn_pointer_is_external_block(dest
)) {
942 vtn_assert(dest
->mode
== vtn_variable_mode_ssbo
);
943 vtn_block_store(b
, src
, dest
);
945 _vtn_variable_load_store(b
, false, dest
, &src
);
950 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
951 struct vtn_pointer
*src
)
953 vtn_assert(src
->type
->type
== dest
->type
->type
);
954 enum glsl_base_type base_type
= glsl_get_base_type(src
->type
->type
);
958 case GLSL_TYPE_UINT64
:
959 case GLSL_TYPE_INT64
:
960 case GLSL_TYPE_FLOAT
:
961 case GLSL_TYPE_DOUBLE
:
963 /* At this point, we have a scalar, vector, or matrix so we know that
964 * there cannot be any structure splitting still in the way. By
965 * stopping at the matrix level rather than the vector level, we
966 * ensure that matrices get loaded in the optimal way even if they
967 * are storred row-major in a UBO.
969 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
972 case GLSL_TYPE_ARRAY
:
973 case GLSL_TYPE_STRUCT
: {
974 struct vtn_access_chain chain
= {
977 { .mode
= vtn_access_mode_literal
, },
980 unsigned elems
= glsl_get_length(src
->type
->type
);
981 for (unsigned i
= 0; i
< elems
; i
++) {
982 chain
.link
[0].id
= i
;
983 struct vtn_pointer
*src_elem
=
984 vtn_pointer_dereference(b
, src
, &chain
);
985 struct vtn_pointer
*dest_elem
=
986 vtn_pointer_dereference(b
, dest
, &chain
);
988 _vtn_variable_copy(b
, dest_elem
, src_elem
);
994 vtn_fail("Invalid access chain type");
999 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
1000 struct vtn_pointer
*src
)
1002 /* TODO: At some point, we should add a special-case for when we can
1003 * just emit a copy_var intrinsic.
1005 _vtn_variable_copy(b
, dest
, src
);
1009 set_mode_system_value(struct vtn_builder
*b
, nir_variable_mode
*mode
)
1011 vtn_assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
1012 *mode
= nir_var_system_value
;
1016 vtn_get_builtin_location(struct vtn_builder
*b
,
1017 SpvBuiltIn builtin
, int *location
,
1018 nir_variable_mode
*mode
)
1021 case SpvBuiltInPosition
:
1022 *location
= VARYING_SLOT_POS
;
1024 case SpvBuiltInPointSize
:
1025 *location
= VARYING_SLOT_PSIZ
;
1027 case SpvBuiltInClipDistance
:
1028 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
1030 case SpvBuiltInCullDistance
:
1031 *location
= VARYING_SLOT_CULL_DIST0
;
1033 case SpvBuiltInVertexIndex
:
1034 *location
= SYSTEM_VALUE_VERTEX_ID
;
1035 set_mode_system_value(b
, mode
);
1037 case SpvBuiltInVertexId
:
1038 /* Vulkan defines VertexID to be zero-based and reserves the new
1039 * builtin keyword VertexIndex to indicate the non-zero-based value.
1041 *location
= SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1042 set_mode_system_value(b
, mode
);
1044 case SpvBuiltInInstanceIndex
:
1045 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
1046 set_mode_system_value(b
, mode
);
1048 case SpvBuiltInInstanceId
:
1049 *location
= SYSTEM_VALUE_INSTANCE_ID
;
1050 set_mode_system_value(b
, mode
);
1052 case SpvBuiltInPrimitiveId
:
1053 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1054 vtn_assert(*mode
== nir_var_shader_in
);
1055 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1056 } else if (*mode
== nir_var_shader_out
) {
1057 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1059 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
1060 set_mode_system_value(b
, mode
);
1063 case SpvBuiltInInvocationId
:
1064 *location
= SYSTEM_VALUE_INVOCATION_ID
;
1065 set_mode_system_value(b
, mode
);
1067 case SpvBuiltInLayer
:
1068 *location
= VARYING_SLOT_LAYER
;
1069 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1070 *mode
= nir_var_shader_in
;
1071 else if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1072 *mode
= nir_var_shader_out
;
1074 vtn_fail("invalid stage for SpvBuiltInLayer");
1076 case SpvBuiltInViewportIndex
:
1077 *location
= VARYING_SLOT_VIEWPORT
;
1078 if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1079 *mode
= nir_var_shader_out
;
1080 else if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1081 *mode
= nir_var_shader_in
;
1083 vtn_fail("invalid stage for SpvBuiltInViewportIndex");
1085 case SpvBuiltInTessLevelOuter
:
1086 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
1088 case SpvBuiltInTessLevelInner
:
1089 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
1091 case SpvBuiltInTessCoord
:
1092 *location
= SYSTEM_VALUE_TESS_COORD
;
1093 set_mode_system_value(b
, mode
);
1095 case SpvBuiltInPatchVertices
:
1096 *location
= SYSTEM_VALUE_VERTICES_IN
;
1097 set_mode_system_value(b
, mode
);
1099 case SpvBuiltInFragCoord
:
1100 *location
= VARYING_SLOT_POS
;
1101 vtn_assert(*mode
== nir_var_shader_in
);
1103 case SpvBuiltInPointCoord
:
1104 *location
= VARYING_SLOT_PNTC
;
1105 vtn_assert(*mode
== nir_var_shader_in
);
1107 case SpvBuiltInFrontFacing
:
1108 *location
= SYSTEM_VALUE_FRONT_FACE
;
1109 set_mode_system_value(b
, mode
);
1111 case SpvBuiltInSampleId
:
1112 *location
= SYSTEM_VALUE_SAMPLE_ID
;
1113 set_mode_system_value(b
, mode
);
1115 case SpvBuiltInSamplePosition
:
1116 *location
= SYSTEM_VALUE_SAMPLE_POS
;
1117 set_mode_system_value(b
, mode
);
1119 case SpvBuiltInSampleMask
:
1120 if (*mode
== nir_var_shader_out
) {
1121 *location
= FRAG_RESULT_SAMPLE_MASK
;
1123 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1124 set_mode_system_value(b
, mode
);
1127 case SpvBuiltInFragDepth
:
1128 *location
= FRAG_RESULT_DEPTH
;
1129 vtn_assert(*mode
== nir_var_shader_out
);
1131 case SpvBuiltInHelperInvocation
:
1132 *location
= SYSTEM_VALUE_HELPER_INVOCATION
;
1133 set_mode_system_value(b
, mode
);
1135 case SpvBuiltInNumWorkgroups
:
1136 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1137 set_mode_system_value(b
, mode
);
1139 case SpvBuiltInWorkgroupSize
:
1140 /* This should already be handled */
1141 vtn_fail("unsupported builtin");
1143 case SpvBuiltInWorkgroupId
:
1144 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1145 set_mode_system_value(b
, mode
);
1147 case SpvBuiltInLocalInvocationId
:
1148 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1149 set_mode_system_value(b
, mode
);
1151 case SpvBuiltInLocalInvocationIndex
:
1152 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1153 set_mode_system_value(b
, mode
);
1155 case SpvBuiltInGlobalInvocationId
:
1156 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1157 set_mode_system_value(b
, mode
);
1159 case SpvBuiltInBaseVertex
:
1160 *location
= SYSTEM_VALUE_BASE_VERTEX
;
1161 set_mode_system_value(b
, mode
);
1163 case SpvBuiltInBaseInstance
:
1164 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1165 set_mode_system_value(b
, mode
);
1167 case SpvBuiltInDrawIndex
:
1168 *location
= SYSTEM_VALUE_DRAW_ID
;
1169 set_mode_system_value(b
, mode
);
1171 case SpvBuiltInViewIndex
:
1172 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1173 set_mode_system_value(b
, mode
);
1176 vtn_fail("unsupported builtin");
1181 apply_var_decoration(struct vtn_builder
*b
, nir_variable
*nir_var
,
1182 const struct vtn_decoration
*dec
)
1184 switch (dec
->decoration
) {
1185 case SpvDecorationRelaxedPrecision
:
1186 break; /* FIXME: Do nothing with this for now. */
1187 case SpvDecorationNoPerspective
:
1188 nir_var
->data
.interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1190 case SpvDecorationFlat
:
1191 nir_var
->data
.interpolation
= INTERP_MODE_FLAT
;
1193 case SpvDecorationCentroid
:
1194 nir_var
->data
.centroid
= true;
1196 case SpvDecorationSample
:
1197 nir_var
->data
.sample
= true;
1199 case SpvDecorationInvariant
:
1200 nir_var
->data
.invariant
= true;
1202 case SpvDecorationConstant
:
1203 vtn_assert(nir_var
->constant_initializer
!= NULL
);
1204 nir_var
->data
.read_only
= true;
1206 case SpvDecorationNonReadable
:
1207 nir_var
->data
.image
.write_only
= true;
1209 case SpvDecorationNonWritable
:
1210 nir_var
->data
.read_only
= true;
1211 nir_var
->data
.image
.read_only
= true;
1213 case SpvDecorationComponent
:
1214 nir_var
->data
.location_frac
= dec
->literals
[0];
1216 case SpvDecorationIndex
:
1217 nir_var
->data
.index
= dec
->literals
[0];
1219 case SpvDecorationBuiltIn
: {
1220 SpvBuiltIn builtin
= dec
->literals
[0];
1222 if (builtin
== SpvBuiltInWorkgroupSize
) {
1223 /* This shouldn't be a builtin. It's actually a constant. */
1224 nir_var
->data
.mode
= nir_var_global
;
1225 nir_var
->data
.read_only
= true;
1227 nir_constant
*c
= rzalloc(nir_var
, nir_constant
);
1228 c
->values
[0].u32
[0] = b
->shader
->info
.cs
.local_size
[0];
1229 c
->values
[0].u32
[1] = b
->shader
->info
.cs
.local_size
[1];
1230 c
->values
[0].u32
[2] = b
->shader
->info
.cs
.local_size
[2];
1231 nir_var
->constant_initializer
= c
;
1235 nir_variable_mode mode
= nir_var
->data
.mode
;
1236 vtn_get_builtin_location(b
, builtin
, &nir_var
->data
.location
, &mode
);
1237 nir_var
->data
.mode
= mode
;
1240 case SpvBuiltInTessLevelOuter
:
1241 case SpvBuiltInTessLevelInner
:
1242 nir_var
->data
.compact
= true;
1244 case SpvBuiltInSamplePosition
:
1245 nir_var
->data
.origin_upper_left
= b
->origin_upper_left
;
1247 case SpvBuiltInFragCoord
:
1248 nir_var
->data
.pixel_center_integer
= b
->pixel_center_integer
;
1255 case SpvDecorationSpecId
:
1256 case SpvDecorationRowMajor
:
1257 case SpvDecorationColMajor
:
1258 case SpvDecorationMatrixStride
:
1259 case SpvDecorationRestrict
:
1260 case SpvDecorationAliased
:
1261 case SpvDecorationVolatile
:
1262 case SpvDecorationCoherent
:
1263 case SpvDecorationUniform
:
1264 case SpvDecorationStream
:
1265 case SpvDecorationOffset
:
1266 case SpvDecorationLinkageAttributes
:
1267 break; /* Do nothing with these here */
1269 case SpvDecorationPatch
:
1270 nir_var
->data
.patch
= true;
1273 case SpvDecorationLocation
:
1274 vtn_fail("Handled above");
1276 case SpvDecorationBlock
:
1277 case SpvDecorationBufferBlock
:
1278 case SpvDecorationArrayStride
:
1279 case SpvDecorationGLSLShared
:
1280 case SpvDecorationGLSLPacked
:
1281 break; /* These can apply to a type but we don't care about them */
1283 case SpvDecorationBinding
:
1284 case SpvDecorationDescriptorSet
:
1285 case SpvDecorationNoContraction
:
1286 case SpvDecorationInputAttachmentIndex
:
1287 vtn_warn("Decoration not allowed for variable or structure member: %s",
1288 spirv_decoration_to_string(dec
->decoration
));
1291 case SpvDecorationXfbBuffer
:
1292 case SpvDecorationXfbStride
:
1293 vtn_warn("Vulkan does not have transform feedback: %s",
1294 spirv_decoration_to_string(dec
->decoration
));
1297 case SpvDecorationCPacked
:
1298 case SpvDecorationSaturatedConversion
:
1299 case SpvDecorationFuncParamAttr
:
1300 case SpvDecorationFPRoundingMode
:
1301 case SpvDecorationFPFastMathMode
:
1302 case SpvDecorationAlignment
:
1303 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1304 spirv_decoration_to_string(dec
->decoration
));
1308 vtn_fail("Unhandled decoration");
1313 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1314 const struct vtn_decoration
*dec
, void *out_is_patch
)
1316 if (dec
->decoration
== SpvDecorationPatch
) {
1317 *((bool *) out_is_patch
) = true;
1322 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1323 const struct vtn_decoration
*dec
, void *void_var
)
1325 struct vtn_variable
*vtn_var
= void_var
;
1327 /* Handle decorations that apply to a vtn_variable as a whole */
1328 switch (dec
->decoration
) {
1329 case SpvDecorationBinding
:
1330 vtn_var
->binding
= dec
->literals
[0];
1332 case SpvDecorationDescriptorSet
:
1333 vtn_var
->descriptor_set
= dec
->literals
[0];
1335 case SpvDecorationInputAttachmentIndex
:
1336 vtn_var
->input_attachment_index
= dec
->literals
[0];
1338 case SpvDecorationPatch
:
1339 vtn_var
->patch
= true;
1345 if (val
->value_type
== vtn_value_type_pointer
) {
1346 vtn_assert(val
->pointer
->var
== void_var
);
1347 vtn_assert(val
->pointer
->chain
== NULL
);
1348 vtn_assert(member
== -1);
1350 vtn_assert(val
->value_type
== vtn_value_type_type
);
1353 /* Location is odd. If applied to a split structure, we have to walk the
1354 * whole thing and accumulate the location. It's easier to handle as a
1357 if (dec
->decoration
== SpvDecorationLocation
) {
1358 unsigned location
= dec
->literals
[0];
1359 bool is_vertex_input
;
1360 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
&&
1361 vtn_var
->mode
== vtn_variable_mode_output
) {
1362 is_vertex_input
= false;
1363 location
+= FRAG_RESULT_DATA0
;
1364 } else if (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
&&
1365 vtn_var
->mode
== vtn_variable_mode_input
) {
1366 is_vertex_input
= true;
1367 location
+= VERT_ATTRIB_GENERIC0
;
1368 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1369 vtn_var
->mode
== vtn_variable_mode_output
) {
1370 is_vertex_input
= false;
1371 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1373 vtn_warn("Location must be on input or output variable");
1378 /* This handles the member and lone variable cases */
1379 vtn_var
->var
->data
.location
= location
;
1381 /* This handles the structure member case */
1382 vtn_assert(vtn_var
->members
);
1384 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1385 for (unsigned i
= 0; i
< length
; i
++) {
1386 vtn_var
->members
[i
]->data
.location
= location
;
1388 glsl_count_attribute_slots(vtn_var
->members
[i
]->interface_type
,
1395 vtn_assert(member
<= 0);
1396 apply_var_decoration(b
, vtn_var
->var
, dec
);
1397 } else if (vtn_var
->members
) {
1399 vtn_assert(vtn_var
->members
);
1400 apply_var_decoration(b
, vtn_var
->members
[member
], dec
);
1403 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1404 for (unsigned i
= 0; i
< length
; i
++)
1405 apply_var_decoration(b
, vtn_var
->members
[i
], dec
);
1408 /* A few variables, those with external storage, have no actual
1409 * nir_variables associated with them. Fortunately, all decorations
1410 * we care about for those variables are on the type only.
1412 vtn_assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1413 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1414 vtn_var
->mode
== vtn_variable_mode_push_constant
);
1419 static enum vtn_variable_mode
1420 vtn_storage_class_to_mode(struct vtn_builder
*b
,
1421 SpvStorageClass
class,
1422 struct vtn_type
*interface_type
,
1423 nir_variable_mode
*nir_mode_out
)
1425 enum vtn_variable_mode mode
;
1426 nir_variable_mode nir_mode
;
1428 case SpvStorageClassUniform
:
1429 if (interface_type
->block
) {
1430 mode
= vtn_variable_mode_ubo
;
1432 } else if (interface_type
->buffer_block
) {
1433 mode
= vtn_variable_mode_ssbo
;
1436 vtn_fail("Invalid uniform variable type");
1439 case SpvStorageClassStorageBuffer
:
1440 mode
= vtn_variable_mode_ssbo
;
1443 case SpvStorageClassUniformConstant
:
1444 if (glsl_type_is_image(interface_type
->type
)) {
1445 mode
= vtn_variable_mode_image
;
1446 nir_mode
= nir_var_uniform
;
1447 } else if (glsl_type_is_sampler(interface_type
->type
)) {
1448 mode
= vtn_variable_mode_sampler
;
1449 nir_mode
= nir_var_uniform
;
1451 vtn_fail("Invalid uniform constant variable type");
1454 case SpvStorageClassPushConstant
:
1455 mode
= vtn_variable_mode_push_constant
;
1456 nir_mode
= nir_var_uniform
;
1458 case SpvStorageClassInput
:
1459 mode
= vtn_variable_mode_input
;
1460 nir_mode
= nir_var_shader_in
;
1462 case SpvStorageClassOutput
:
1463 mode
= vtn_variable_mode_output
;
1464 nir_mode
= nir_var_shader_out
;
1466 case SpvStorageClassPrivate
:
1467 mode
= vtn_variable_mode_global
;
1468 nir_mode
= nir_var_global
;
1470 case SpvStorageClassFunction
:
1471 mode
= vtn_variable_mode_local
;
1472 nir_mode
= nir_var_local
;
1474 case SpvStorageClassWorkgroup
:
1475 mode
= vtn_variable_mode_workgroup
;
1476 nir_mode
= nir_var_shared
;
1478 case SpvStorageClassCrossWorkgroup
:
1479 case SpvStorageClassGeneric
:
1480 case SpvStorageClassAtomicCounter
:
1482 vtn_fail("Unhandled variable storage class");
1486 *nir_mode_out
= nir_mode
;
1492 vtn_pointer_to_ssa(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
1494 /* This pointer needs to have a pointer type with actual storage */
1495 vtn_assert(ptr
->ptr_type
);
1496 vtn_assert(ptr
->ptr_type
->type
);
1498 if (ptr
->offset
&& ptr
->block_index
) {
1499 return nir_vec2(&b
->nb
, ptr
->block_index
, ptr
->offset
);
1501 /* If we don't have an offset or block index, then we must be a pointer
1502 * to the variable itself.
1504 vtn_assert(!ptr
->offset
&& !ptr
->block_index
);
1506 /* We can't handle a pointer to an array of descriptors because we have
1507 * no way of knowing later on that we need to add to update the block
1508 * index when dereferencing.
1510 vtn_assert(ptr
->var
&& ptr
->var
->type
->base_type
== vtn_base_type_struct
);
1512 return nir_vec2(&b
->nb
, vtn_variable_resource_index(b
, ptr
->var
, NULL
),
1513 nir_imm_int(&b
->nb
, 0));
1517 struct vtn_pointer
*
1518 vtn_pointer_from_ssa(struct vtn_builder
*b
, nir_ssa_def
*ssa
,
1519 struct vtn_type
*ptr_type
)
1521 vtn_assert(ssa
->num_components
== 2 && ssa
->bit_size
== 32);
1522 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1523 vtn_assert(ptr_type
->deref
->base_type
!= vtn_base_type_pointer
);
1524 /* This pointer type needs to have actual storage */
1525 vtn_assert(ptr_type
->type
);
1527 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
1528 ptr
->mode
= vtn_storage_class_to_mode(b
, ptr_type
->storage_class
,
1530 ptr
->type
= ptr_type
->deref
;
1531 ptr
->ptr_type
= ptr_type
;
1532 ptr
->block_index
= nir_channel(&b
->nb
, ssa
, 0);
1533 ptr
->offset
= nir_channel(&b
->nb
, ssa
, 1);
1539 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
1541 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
1544 if (var
->mode
== vtn_variable_mode_input
) {
1545 return stage
== MESA_SHADER_TESS_CTRL
||
1546 stage
== MESA_SHADER_TESS_EVAL
||
1547 stage
== MESA_SHADER_GEOMETRY
;
1550 if (var
->mode
== vtn_variable_mode_output
)
1551 return stage
== MESA_SHADER_TESS_CTRL
;
1557 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
1558 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
1559 nir_constant
*initializer
)
1561 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1562 struct vtn_type
*type
= ptr_type
->deref
;
1564 struct vtn_type
*without_array
= type
;
1565 while(glsl_type_is_array(without_array
->type
))
1566 without_array
= without_array
->array_element
;
1568 enum vtn_variable_mode mode
;
1569 nir_variable_mode nir_mode
;
1570 mode
= vtn_storage_class_to_mode(b
, storage_class
, without_array
, &nir_mode
);
1573 case vtn_variable_mode_ubo
:
1574 b
->shader
->info
.num_ubos
++;
1576 case vtn_variable_mode_ssbo
:
1577 b
->shader
->info
.num_ssbos
++;
1579 case vtn_variable_mode_image
:
1580 b
->shader
->info
.num_images
++;
1582 case vtn_variable_mode_sampler
:
1583 b
->shader
->info
.num_textures
++;
1585 case vtn_variable_mode_push_constant
:
1586 b
->shader
->num_uniforms
= vtn_type_block_size(b
, type
);
1589 /* No tallying is needed */
1593 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
1597 vtn_assert(val
->value_type
== vtn_value_type_pointer
);
1598 val
->pointer
= vtn_pointer_for_variable(b
, var
, ptr_type
);
1600 switch (var
->mode
) {
1601 case vtn_variable_mode_local
:
1602 case vtn_variable_mode_global
:
1603 case vtn_variable_mode_image
:
1604 case vtn_variable_mode_sampler
:
1605 case vtn_variable_mode_workgroup
:
1606 /* For these, we create the variable normally */
1607 var
->var
= rzalloc(b
->shader
, nir_variable
);
1608 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1609 var
->var
->type
= var
->type
->type
;
1610 var
->var
->data
.mode
= nir_mode
;
1612 switch (var
->mode
) {
1613 case vtn_variable_mode_image
:
1614 case vtn_variable_mode_sampler
:
1615 var
->var
->interface_type
= without_array
->type
;
1618 var
->var
->interface_type
= NULL
;
1623 case vtn_variable_mode_input
:
1624 case vtn_variable_mode_output
: {
1625 /* In order to know whether or not we're a per-vertex inout, we need
1626 * the patch qualifier. This means walking the variable decorations
1627 * early before we actually create any variables. Not a big deal.
1629 * GLSLang really likes to place decorations in the most interior
1630 * thing it possibly can. In particular, if you have a struct, it
1631 * will place the patch decorations on the struct members. This
1632 * should be handled by the variable splitting below just fine.
1634 * If you have an array-of-struct, things get even more weird as it
1635 * will place the patch decorations on the struct even though it's
1636 * inside an array and some of the members being patch and others not
1637 * makes no sense whatsoever. Since the only sensible thing is for
1638 * it to be all or nothing, we'll call it patch if any of the members
1639 * are declared patch.
1642 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
1643 if (glsl_type_is_array(var
->type
->type
) &&
1644 glsl_type_is_struct(without_array
->type
)) {
1645 vtn_foreach_decoration(b
, without_array
->val
,
1646 var_is_patch_cb
, &var
->patch
);
1649 /* For inputs and outputs, we immediately split structures. This
1650 * is for a couple of reasons. For one, builtins may all come in
1651 * a struct and we really want those split out into separate
1652 * variables. For another, interpolation qualifiers can be
1653 * applied to members of the top-level struct ane we need to be
1654 * able to preserve that information.
1657 int array_length
= -1;
1658 struct vtn_type
*interface_type
= var
->type
;
1659 if (is_per_vertex_inout(var
, b
->shader
->info
.stage
)) {
1660 /* In Geometry shaders (and some tessellation), inputs come
1661 * in per-vertex arrays. However, some builtins come in
1662 * non-per-vertex, hence the need for the is_array check. In
1663 * any case, there are no non-builtin arrays allowed so this
1664 * check should be sufficient.
1666 interface_type
= var
->type
->array_element
;
1667 array_length
= glsl_get_length(var
->type
->type
);
1670 if (glsl_type_is_struct(interface_type
->type
)) {
1671 /* It's a struct. Split it. */
1672 unsigned num_members
= glsl_get_length(interface_type
->type
);
1673 var
->members
= ralloc_array(b
, nir_variable
*, num_members
);
1675 for (unsigned i
= 0; i
< num_members
; i
++) {
1676 const struct glsl_type
*mtype
= interface_type
->members
[i
]->type
;
1677 if (array_length
>= 0)
1678 mtype
= glsl_array_type(mtype
, array_length
);
1680 var
->members
[i
] = rzalloc(b
->shader
, nir_variable
);
1681 var
->members
[i
]->name
=
1682 ralloc_asprintf(var
->members
[i
], "%s.%d", val
->name
, i
);
1683 var
->members
[i
]->type
= mtype
;
1684 var
->members
[i
]->interface_type
=
1685 interface_type
->members
[i
]->type
;
1686 var
->members
[i
]->data
.mode
= nir_mode
;
1687 var
->members
[i
]->data
.patch
= var
->patch
;
1690 var
->var
= rzalloc(b
->shader
, nir_variable
);
1691 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1692 var
->var
->type
= var
->type
->type
;
1693 var
->var
->interface_type
= interface_type
->type
;
1694 var
->var
->data
.mode
= nir_mode
;
1695 var
->var
->data
.patch
= var
->patch
;
1698 /* For inputs and outputs, we need to grab locations and builtin
1699 * information from the interface type.
1701 vtn_foreach_decoration(b
, interface_type
->val
, var_decoration_cb
, var
);
1705 case vtn_variable_mode_param
:
1706 vtn_fail("Not created through OpVariable");
1708 case vtn_variable_mode_ubo
:
1709 case vtn_variable_mode_ssbo
:
1710 case vtn_variable_mode_push_constant
:
1711 /* These don't need actual variables. */
1716 var
->var
->constant_initializer
=
1717 nir_constant_clone(initializer
, var
->var
);
1720 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
1722 if (var
->mode
== vtn_variable_mode_image
||
1723 var
->mode
== vtn_variable_mode_sampler
) {
1724 /* XXX: We still need the binding information in the nir_variable
1725 * for these. We should fix that.
1727 var
->var
->data
.binding
= var
->binding
;
1728 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
1729 var
->var
->data
.index
= var
->input_attachment_index
;
1731 if (var
->mode
== vtn_variable_mode_image
)
1732 var
->var
->data
.image
.format
= without_array
->image_format
;
1735 if (var
->mode
== vtn_variable_mode_local
) {
1736 vtn_assert(var
->members
== NULL
&& var
->var
!= NULL
);
1737 nir_function_impl_add_variable(b
->nb
.impl
, var
->var
);
1738 } else if (var
->var
) {
1739 nir_shader_add_variable(b
->shader
, var
->var
);
1740 } else if (var
->members
) {
1741 unsigned count
= glsl_get_length(without_array
->type
);
1742 for (unsigned i
= 0; i
< count
; i
++) {
1743 vtn_assert(var
->members
[i
]->data
.mode
!= nir_var_local
);
1744 nir_shader_add_variable(b
->shader
, var
->members
[i
]);
1747 vtn_assert(var
->mode
== vtn_variable_mode_ubo
||
1748 var
->mode
== vtn_variable_mode_ssbo
||
1749 var
->mode
== vtn_variable_mode_push_constant
);
1754 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
1755 const uint32_t *w
, unsigned count
)
1759 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1760 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1764 case SpvOpVariable
: {
1765 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1767 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1769 SpvStorageClass storage_class
= w
[3];
1770 nir_constant
*initializer
= NULL
;
1772 initializer
= vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
1774 vtn_create_variable(b
, val
, ptr_type
, storage_class
, initializer
);
1778 case SpvOpAccessChain
:
1779 case SpvOpPtrAccessChain
:
1780 case SpvOpInBoundsAccessChain
: {
1781 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
1782 chain
->ptr_as_array
= (opcode
== SpvOpPtrAccessChain
);
1785 for (int i
= 4; i
< count
; i
++) {
1786 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
1787 if (link_val
->value_type
== vtn_value_type_constant
) {
1788 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
1789 chain
->link
[idx
].id
= link_val
->constant
->values
[0].u32
[0];
1791 chain
->link
[idx
].mode
= vtn_access_mode_id
;
1792 chain
->link
[idx
].id
= w
[i
];
1798 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1799 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
1800 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1801 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1802 * to combine an array of images with a single sampler to get an
1803 * array of sampled images that all share the same sampler.
1804 * Fortunately, this means that we can more-or-less ignore the
1805 * sampler when crawling the access chain, but it does leave us
1806 * with this rather awkward little special-case.
1808 struct vtn_value
*val
=
1809 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1810 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1811 val
->sampled_image
->type
= base_val
->sampled_image
->type
;
1812 val
->sampled_image
->image
=
1813 vtn_pointer_dereference(b
, base_val
->sampled_image
->image
, chain
);
1814 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
1816 vtn_assert(base_val
->value_type
== vtn_value_type_pointer
);
1817 struct vtn_value
*val
=
1818 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1819 val
->pointer
= vtn_pointer_dereference(b
, base_val
->pointer
, chain
);
1820 val
->pointer
->ptr_type
= ptr_type
;
1825 case SpvOpCopyMemory
: {
1826 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
1827 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
1829 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
1834 struct vtn_type
*res_type
=
1835 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1836 struct vtn_pointer
*src
=
1837 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1839 if (src
->mode
== vtn_variable_mode_image
||
1840 src
->mode
== vtn_variable_mode_sampler
) {
1841 vtn_push_value(b
, w
[2], vtn_value_type_pointer
)->pointer
= src
;
1845 vtn_push_ssa(b
, w
[2], res_type
, vtn_variable_load(b
, src
));
1850 struct vtn_pointer
*dest
=
1851 vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
1853 if (glsl_type_is_sampler(dest
->type
->type
)) {
1854 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
1855 "propagation to workaround the problem.");
1856 vtn_assert(dest
->var
->copy_prop_sampler
== NULL
);
1857 dest
->var
->copy_prop_sampler
=
1858 vtn_value(b
, w
[2], vtn_value_type_pointer
)->pointer
;
1862 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
1863 vtn_variable_store(b
, src
, dest
);
1867 case SpvOpArrayLength
: {
1868 struct vtn_pointer
*ptr
=
1869 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1871 const uint32_t offset
= ptr
->var
->type
->offsets
[w
[4]];
1872 const uint32_t stride
= ptr
->var
->type
->members
[w
[4]]->stride
;
1875 struct vtn_type
*type
;
1876 nir_ssa_def
*index
=
1877 get_vulkan_resource_index(b
, ptr
, &type
, &chain_idx
);
1879 nir_intrinsic_instr
*instr
=
1880 nir_intrinsic_instr_create(b
->nb
.shader
,
1881 nir_intrinsic_get_buffer_size
);
1882 instr
->src
[0] = nir_src_for_ssa(index
);
1883 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
1884 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
1885 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
1887 /* array_length = max(buffer_size - offset, 0) / stride */
1888 nir_ssa_def
*array_length
=
1893 nir_imm_int(&b
->nb
, offset
)),
1894 nir_imm_int(&b
->nb
, 0u)),
1895 nir_imm_int(&b
->nb
, stride
));
1897 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1898 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
1899 val
->ssa
->def
= array_length
;
1903 case SpvOpCopyMemorySized
:
1905 vtn_fail("Unhandled opcode");