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 /* This is the first access chain so we don't have a block index */
166 vtn_assert(!block_index
);
168 vtn_assert(base
->var
);
169 switch (base
->mode
) {
170 case vtn_variable_mode_ubo
:
171 case vtn_variable_mode_ssbo
:
172 if (glsl_type_is_array(type
->type
)) {
173 /* We need at least one element in the chain */
174 vtn_assert(deref_chain
->length
>= 1);
176 nir_ssa_def
*desc_arr_idx
=
177 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1);
178 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
179 type
= type
->array_element
;
182 block_index
= vtn_variable_resource_index(b
, base
->var
, NULL
);
184 offset
= nir_imm_int(&b
->nb
, 0);
188 vtn_fail("Invalid offset pointer mode");
193 for (; idx
< deref_chain
->length
; idx
++) {
194 switch (glsl_get_base_type(type
->type
)) {
197 case GLSL_TYPE_UINT64
:
198 case GLSL_TYPE_INT64
:
199 case GLSL_TYPE_FLOAT
:
200 case GLSL_TYPE_DOUBLE
:
202 case GLSL_TYPE_ARRAY
: {
203 nir_ssa_def
*elem_offset
=
204 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
], type
->stride
);
205 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
206 type
= type
->array_element
;
210 case GLSL_TYPE_STRUCT
: {
211 vtn_assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
212 unsigned member
= deref_chain
->link
[idx
].id
;
213 nir_ssa_def
*mem_offset
= nir_imm_int(&b
->nb
, type
->offsets
[member
]);
214 offset
= nir_iadd(&b
->nb
, offset
, mem_offset
);
215 type
= type
->members
[member
];
220 vtn_fail("Invalid type for deref");
224 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
225 ptr
->mode
= base
->mode
;
227 ptr
->block_index
= block_index
;
228 ptr
->offset
= offset
;
233 /* Dereference the given base pointer by the access chain */
234 static struct vtn_pointer
*
235 vtn_pointer_dereference(struct vtn_builder
*b
,
236 struct vtn_pointer
*base
,
237 struct vtn_access_chain
*deref_chain
)
239 if (vtn_pointer_uses_ssa_offset(base
)) {
240 return vtn_ssa_offset_pointer_dereference(b
, base
, deref_chain
);
242 return vtn_access_chain_pointer_dereference(b
, base
, deref_chain
);
246 /* Crawls a chain of array derefs and rewrites the types so that the
247 * lengths stay the same but the terminal type is the one given by
248 * tail_type. This is useful for split structures.
251 rewrite_deref_types(struct vtn_builder
*b
, nir_deref
*deref
,
252 const struct glsl_type
*type
)
256 vtn_assert(deref
->child
->deref_type
== nir_deref_type_array
);
257 vtn_assert(glsl_type_is_array(deref
->type
));
258 rewrite_deref_types(b
, deref
->child
, glsl_get_array_element(type
));
263 vtn_pointer_for_variable(struct vtn_builder
*b
,
264 struct vtn_variable
*var
, struct vtn_type
*ptr_type
)
266 struct vtn_pointer
*pointer
= rzalloc(b
, struct vtn_pointer
);
268 pointer
->mode
= var
->mode
;
269 pointer
->type
= var
->type
;
270 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
271 vtn_assert(ptr_type
->deref
->type
== var
->type
->type
);
272 pointer
->ptr_type
= ptr_type
;
279 vtn_pointer_to_deref(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
281 /* Do on-the-fly copy propagation for samplers. */
282 if (ptr
->var
->copy_prop_sampler
)
283 return vtn_pointer_to_deref(b
, ptr
->var
->copy_prop_sampler
);
285 nir_deref_var
*deref_var
;
287 deref_var
= nir_deref_var_create(b
, ptr
->var
->var
);
288 /* Raw variable access */
292 vtn_assert(ptr
->var
->members
);
293 /* Create the deref_var manually. It will get filled out later. */
294 deref_var
= rzalloc(b
, nir_deref_var
);
295 deref_var
->deref
.deref_type
= nir_deref_type_var
;
298 struct vtn_access_chain
*chain
= ptr
->chain
;
301 struct vtn_type
*deref_type
= ptr
->var
->type
;
302 nir_deref
*tail
= &deref_var
->deref
;
303 nir_variable
**members
= ptr
->var
->members
;
305 for (unsigned i
= 0; i
< chain
->length
; i
++) {
306 enum glsl_base_type base_type
= glsl_get_base_type(deref_type
->type
);
310 case GLSL_TYPE_UINT64
:
311 case GLSL_TYPE_INT64
:
312 case GLSL_TYPE_FLOAT
:
313 case GLSL_TYPE_DOUBLE
:
315 case GLSL_TYPE_ARRAY
: {
316 deref_type
= deref_type
->array_element
;
318 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
319 deref_arr
->deref
.type
= deref_type
->type
;
321 if (chain
->link
[i
].mode
== vtn_access_mode_literal
) {
322 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
323 deref_arr
->base_offset
= chain
->link
[i
].id
;
325 vtn_assert(chain
->link
[i
].mode
== vtn_access_mode_id
);
326 deref_arr
->deref_array_type
= nir_deref_array_type_indirect
;
327 deref_arr
->base_offset
= 0;
328 deref_arr
->indirect
=
329 nir_src_for_ssa(vtn_ssa_value(b
, chain
->link
[i
].id
)->def
);
331 tail
->child
= &deref_arr
->deref
;
336 case GLSL_TYPE_STRUCT
: {
337 vtn_assert(chain
->link
[i
].mode
== vtn_access_mode_literal
);
338 unsigned idx
= chain
->link
[i
].id
;
339 deref_type
= deref_type
->members
[idx
];
341 /* This is a pre-split structure. */
342 deref_var
->var
= members
[idx
];
343 rewrite_deref_types(b
, &deref_var
->deref
, members
[idx
]->type
);
344 vtn_assert(tail
->type
== deref_type
->type
);
347 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, idx
);
348 deref_struct
->deref
.type
= deref_type
->type
;
349 tail
->child
= &deref_struct
->deref
;
355 vtn_fail("Invalid type for deref");
359 vtn_assert(members
== NULL
);
364 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_var
*deref
,
365 nir_deref
*tail
, struct vtn_ssa_value
*inout
)
367 /* The deref tail may contain a deref to select a component of a vector (in
368 * other words, it might not be an actual tail) so we have to save it away
369 * here since we overwrite it later.
371 nir_deref
*old_child
= tail
->child
;
373 if (glsl_type_is_vector_or_scalar(tail
->type
)) {
374 /* Terminate the deref chain in case there is one more link to pick
375 * off a component of the vector.
379 nir_intrinsic_op op
= load
? nir_intrinsic_load_var
:
380 nir_intrinsic_store_var
;
382 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
383 intrin
->variables
[0] = nir_deref_var_clone(deref
, intrin
);
384 intrin
->num_components
= glsl_get_vector_elements(tail
->type
);
387 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
388 intrin
->num_components
,
389 glsl_get_bit_size(tail
->type
),
391 inout
->def
= &intrin
->dest
.ssa
;
393 nir_intrinsic_set_write_mask(intrin
, (1 << intrin
->num_components
) - 1);
394 intrin
->src
[0] = nir_src_for_ssa(inout
->def
);
397 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
398 } else if (glsl_get_base_type(tail
->type
) == GLSL_TYPE_ARRAY
||
399 glsl_type_is_matrix(tail
->type
)) {
400 unsigned elems
= glsl_get_length(tail
->type
);
401 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
402 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
403 deref_arr
->deref
.type
= glsl_get_array_element(tail
->type
);
404 tail
->child
= &deref_arr
->deref
;
405 for (unsigned i
= 0; i
< elems
; i
++) {
406 deref_arr
->base_offset
= i
;
407 _vtn_local_load_store(b
, load
, deref
, tail
->child
, inout
->elems
[i
]);
410 vtn_assert(glsl_get_base_type(tail
->type
) == GLSL_TYPE_STRUCT
);
411 unsigned elems
= glsl_get_length(tail
->type
);
412 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, 0);
413 tail
->child
= &deref_struct
->deref
;
414 for (unsigned i
= 0; i
< elems
; i
++) {
415 deref_struct
->index
= i
;
416 deref_struct
->deref
.type
= glsl_get_struct_field(tail
->type
, i
);
417 _vtn_local_load_store(b
, load
, deref
, tail
->child
, inout
->elems
[i
]);
421 tail
->child
= old_child
;
425 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
427 struct vtn_pointer
*ptr
= vtn_value(b
, id
, vtn_value_type_pointer
)->pointer
;
428 return vtn_pointer_to_deref(b
, ptr
);
432 * Gets the NIR-level deref tail, which may have as a child an array deref
433 * selecting which component due to OpAccessChain supporting per-component
434 * indexing in SPIR-V.
437 get_deref_tail(nir_deref_var
*deref
)
439 nir_deref
*cur
= &deref
->deref
;
440 while (!glsl_type_is_vector_or_scalar(cur
->type
) && cur
->child
)
446 struct vtn_ssa_value
*
447 vtn_local_load(struct vtn_builder
*b
, nir_deref_var
*src
)
449 nir_deref
*src_tail
= get_deref_tail(src
);
450 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
451 _vtn_local_load_store(b
, true, src
, src_tail
, val
);
453 if (src_tail
->child
) {
454 nir_deref_array
*vec_deref
= nir_deref_as_array(src_tail
->child
);
455 vtn_assert(vec_deref
->deref
.child
== NULL
);
456 val
->type
= vec_deref
->deref
.type
;
457 if (vec_deref
->deref_array_type
== nir_deref_array_type_direct
)
458 val
->def
= vtn_vector_extract(b
, val
->def
, vec_deref
->base_offset
);
460 val
->def
= vtn_vector_extract_dynamic(b
, val
->def
,
461 vec_deref
->indirect
.ssa
);
468 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
471 nir_deref
*dest_tail
= get_deref_tail(dest
);
473 if (dest_tail
->child
) {
474 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
475 _vtn_local_load_store(b
, true, dest
, dest_tail
, val
);
476 nir_deref_array
*deref
= nir_deref_as_array(dest_tail
->child
);
477 vtn_assert(deref
->deref
.child
== NULL
);
478 if (deref
->deref_array_type
== nir_deref_array_type_direct
)
479 val
->def
= vtn_vector_insert(b
, val
->def
, src
->def
,
482 val
->def
= vtn_vector_insert_dynamic(b
, val
->def
, src
->def
,
483 deref
->indirect
.ssa
);
484 _vtn_local_load_store(b
, false, dest
, dest_tail
, val
);
486 _vtn_local_load_store(b
, false, dest
, dest_tail
, src
);
491 get_vulkan_resource_index(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
492 struct vtn_type
**type
, unsigned *chain_idx
)
494 /* Push constants have no explicit binding */
495 if (ptr
->mode
== vtn_variable_mode_push_constant
) {
497 *type
= ptr
->var
->type
;
501 if (glsl_type_is_array(ptr
->var
->type
->type
)) {
502 vtn_assert(ptr
->chain
->length
> 0);
503 nir_ssa_def
*desc_array_index
=
504 vtn_access_link_as_ssa(b
, ptr
->chain
->link
[0], 1);
506 *type
= ptr
->var
->type
->array_element
;
507 return vtn_variable_resource_index(b
, ptr
->var
, desc_array_index
);
510 *type
= ptr
->var
->type
;
511 return vtn_variable_resource_index(b
, ptr
->var
, NULL
);
516 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
517 nir_ssa_def
**index_out
, unsigned *end_idx_out
)
520 vtn_assert(ptr
->block_index
);
521 *index_out
= ptr
->block_index
;
526 struct vtn_type
*type
;
527 *index_out
= get_vulkan_resource_index(b
, ptr
, &type
, &idx
);
529 nir_ssa_def
*offset
= nir_imm_int(&b
->nb
, 0);
530 for (; idx
< ptr
->chain
->length
; idx
++) {
531 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
535 case GLSL_TYPE_UINT64
:
536 case GLSL_TYPE_INT64
:
537 case GLSL_TYPE_FLOAT
:
538 case GLSL_TYPE_DOUBLE
:
540 case GLSL_TYPE_ARRAY
:
541 offset
= nir_iadd(&b
->nb
, offset
,
542 vtn_access_link_as_ssa(b
, ptr
->chain
->link
[idx
],
545 type
= type
->array_element
;
548 case GLSL_TYPE_STRUCT
: {
549 vtn_assert(ptr
->chain
->link
[idx
].mode
== vtn_access_mode_literal
);
550 unsigned member
= ptr
->chain
->link
[idx
].id
;
551 offset
= nir_iadd(&b
->nb
, offset
,
552 nir_imm_int(&b
->nb
, type
->offsets
[member
]));
553 type
= type
->members
[member
];
558 vtn_fail("Invalid type for deref");
562 vtn_assert(type
== ptr
->type
);
569 /* Tries to compute the size of an interface block based on the strides and
570 * offsets that are provided to us in the SPIR-V source.
573 vtn_type_block_size(struct vtn_builder
*b
, struct vtn_type
*type
)
575 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
579 case GLSL_TYPE_UINT64
:
580 case GLSL_TYPE_INT64
:
581 case GLSL_TYPE_FLOAT
:
583 case GLSL_TYPE_DOUBLE
: {
584 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
585 glsl_get_matrix_columns(type
->type
);
587 vtn_assert(type
->stride
> 0);
588 return type
->stride
* cols
;
589 } else if (base_type
== GLSL_TYPE_DOUBLE
||
590 base_type
== GLSL_TYPE_UINT64
||
591 base_type
== GLSL_TYPE_INT64
) {
592 return glsl_get_vector_elements(type
->type
) * 8;
594 return glsl_get_vector_elements(type
->type
) * 4;
598 case GLSL_TYPE_STRUCT
:
599 case GLSL_TYPE_INTERFACE
: {
601 unsigned num_fields
= glsl_get_length(type
->type
);
602 for (unsigned f
= 0; f
< num_fields
; f
++) {
603 unsigned field_end
= type
->offsets
[f
] +
604 vtn_type_block_size(b
, type
->members
[f
]);
605 size
= MAX2(size
, field_end
);
610 case GLSL_TYPE_ARRAY
:
611 vtn_assert(type
->stride
> 0);
612 vtn_assert(glsl_get_length(type
->type
) > 0);
613 return type
->stride
* glsl_get_length(type
->type
);
616 vtn_fail("Invalid block type");
622 vtn_access_chain_get_offset_size(struct vtn_builder
*b
,
623 struct vtn_access_chain
*chain
,
624 struct vtn_type
*type
,
625 unsigned *access_offset
,
626 unsigned *access_size
)
630 for (unsigned i
= 0; i
< chain
->length
; i
++) {
631 if (chain
->link
[i
].mode
!= vtn_access_mode_literal
)
634 if (glsl_type_is_struct(type
->type
)) {
635 *access_offset
+= type
->offsets
[chain
->link
[i
].id
];
636 type
= type
->members
[chain
->link
[i
].id
];
638 *access_offset
+= type
->stride
* chain
->link
[i
].id
;
639 type
= type
->array_element
;
643 *access_size
= vtn_type_block_size(b
, type
);
647 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
648 nir_ssa_def
*index
, nir_ssa_def
*offset
,
649 unsigned access_offset
, unsigned access_size
,
650 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
)
652 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
653 instr
->num_components
= glsl_get_vector_elements(type
);
657 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
658 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
661 if (op
== nir_intrinsic_load_push_constant
) {
662 vtn_assert(access_offset
% 4 == 0);
664 nir_intrinsic_set_base(instr
, access_offset
);
665 nir_intrinsic_set_range(instr
, access_size
);
669 instr
->src
[src
++] = nir_src_for_ssa(index
);
671 if (op
== nir_intrinsic_load_push_constant
) {
672 /* We need to subtract the offset from where the intrinsic will load the
675 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
676 nir_imm_int(&b
->nb
, access_offset
)));
678 instr
->src
[src
++] = nir_src_for_ssa(offset
);
682 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
683 instr
->num_components
,
684 glsl_get_bit_size(type
), NULL
);
685 (*inout
)->def
= &instr
->dest
.ssa
;
688 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
690 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
691 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
695 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
696 nir_ssa_def
*index
, nir_ssa_def
*offset
,
697 unsigned access_offset
, unsigned access_size
,
698 struct vtn_access_chain
*chain
, unsigned chain_idx
,
699 struct vtn_type
*type
, struct vtn_ssa_value
**inout
)
701 if (chain
&& chain_idx
>= chain
->length
)
704 if (load
&& chain
== NULL
&& *inout
== NULL
)
705 *inout
= vtn_create_ssa_value(b
, type
->type
);
707 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
711 case GLSL_TYPE_UINT64
:
712 case GLSL_TYPE_INT64
:
713 case GLSL_TYPE_FLOAT
:
714 case GLSL_TYPE_DOUBLE
:
716 /* This is where things get interesting. At this point, we've hit
717 * a vector, a scalar, or a matrix.
719 if (glsl_type_is_matrix(type
->type
)) {
720 /* Loading the whole matrix */
721 struct vtn_ssa_value
*transpose
;
722 unsigned num_ops
, vec_width
, col_stride
;
723 if (type
->row_major
) {
724 num_ops
= glsl_get_vector_elements(type
->type
);
725 vec_width
= glsl_get_matrix_columns(type
->type
);
726 col_stride
= type
->array_element
->stride
;
728 const struct glsl_type
*transpose_type
=
729 glsl_matrix_type(base_type
, vec_width
, num_ops
);
730 *inout
= vtn_create_ssa_value(b
, transpose_type
);
732 transpose
= vtn_ssa_transpose(b
, *inout
);
736 num_ops
= glsl_get_matrix_columns(type
->type
);
737 vec_width
= glsl_get_vector_elements(type
->type
);
738 col_stride
= type
->stride
;
741 for (unsigned i
= 0; i
< num_ops
; i
++) {
742 nir_ssa_def
*elem_offset
=
743 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* col_stride
));
744 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
745 access_offset
, access_size
,
747 glsl_vector_type(base_type
, vec_width
));
750 if (load
&& type
->row_major
)
751 *inout
= vtn_ssa_transpose(b
, *inout
);
753 unsigned elems
= glsl_get_vector_elements(type
->type
);
754 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
755 if (elems
== 1 || type
->stride
== type_size
) {
756 /* This is a tightly-packed normal scalar or vector load */
757 vtn_assert(glsl_type_is_vector_or_scalar(type
->type
));
758 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
759 access_offset
, access_size
,
762 /* This is a strided load. We have to load N things separately.
763 * This is the single column of a row-major matrix case.
765 vtn_assert(type
->stride
> type_size
);
766 vtn_assert(type
->stride
% type_size
== 0);
768 nir_ssa_def
*per_comp
[4];
769 for (unsigned i
= 0; i
< elems
; i
++) {
770 nir_ssa_def
*elem_offset
=
771 nir_iadd(&b
->nb
, offset
,
772 nir_imm_int(&b
->nb
, i
* type
->stride
));
773 struct vtn_ssa_value
*comp
, temp_val
;
775 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
776 temp_val
.type
= glsl_scalar_type(base_type
);
779 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
780 access_offset
, access_size
,
781 &comp
, glsl_scalar_type(base_type
));
782 per_comp
[i
] = comp
->def
;
787 *inout
= vtn_create_ssa_value(b
, type
->type
);
788 (*inout
)->def
= nir_vec(&b
->nb
, per_comp
, elems
);
794 case GLSL_TYPE_ARRAY
: {
795 unsigned elems
= glsl_get_length(type
->type
);
796 for (unsigned i
= 0; i
< elems
; i
++) {
797 nir_ssa_def
*elem_off
=
798 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* type
->stride
));
799 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
800 access_offset
, access_size
,
802 type
->array_element
, &(*inout
)->elems
[i
]);
807 case GLSL_TYPE_STRUCT
: {
808 unsigned elems
= glsl_get_length(type
->type
);
809 for (unsigned i
= 0; i
< elems
; i
++) {
810 nir_ssa_def
*elem_off
=
811 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, type
->offsets
[i
]));
812 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
813 access_offset
, access_size
,
815 type
->members
[i
], &(*inout
)->elems
[i
]);
821 vtn_fail("Invalid block member type");
825 static struct vtn_ssa_value
*
826 vtn_block_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
829 unsigned access_offset
= 0, access_size
= 0;
831 case vtn_variable_mode_ubo
:
832 op
= nir_intrinsic_load_ubo
;
834 case vtn_variable_mode_ssbo
:
835 op
= nir_intrinsic_load_ssbo
;
837 case vtn_variable_mode_push_constant
:
838 op
= nir_intrinsic_load_push_constant
;
839 vtn_access_chain_get_offset_size(b
, src
->chain
, src
->var
->type
,
840 &access_offset
, &access_size
);
843 vtn_fail("Invalid block variable mode");
846 nir_ssa_def
*offset
, *index
= NULL
;
848 offset
= vtn_pointer_to_offset(b
, src
, &index
, &chain_idx
);
850 struct vtn_ssa_value
*value
= NULL
;
851 _vtn_block_load_store(b
, op
, true, index
, offset
,
852 access_offset
, access_size
,
853 src
->chain
, chain_idx
, src
->type
, &value
);
858 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
859 struct vtn_pointer
*dst
)
863 case vtn_variable_mode_ssbo
:
864 op
= nir_intrinsic_store_ssbo
;
867 vtn_fail("Invalid block variable mode");
870 nir_ssa_def
*offset
, *index
= NULL
;
872 offset
= vtn_pointer_to_offset(b
, dst
, &index
, &chain_idx
);
874 _vtn_block_load_store(b
, op
, false, index
, offset
,
875 0, 0, dst
->chain
, chain_idx
, dst
->type
, &src
);
879 vtn_pointer_is_external_block(struct vtn_pointer
*ptr
)
881 return ptr
->mode
== vtn_variable_mode_ssbo
||
882 ptr
->mode
== vtn_variable_mode_ubo
||
883 ptr
->mode
== vtn_variable_mode_push_constant
;
887 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
888 struct vtn_pointer
*ptr
,
889 struct vtn_ssa_value
**inout
)
891 enum glsl_base_type base_type
= glsl_get_base_type(ptr
->type
->type
);
895 case GLSL_TYPE_UINT64
:
896 case GLSL_TYPE_INT64
:
897 case GLSL_TYPE_FLOAT
:
899 case GLSL_TYPE_DOUBLE
:
900 /* At this point, we have a scalar, vector, or matrix so we know that
901 * there cannot be any structure splitting still in the way. By
902 * stopping at the matrix level rather than the vector level, we
903 * ensure that matrices get loaded in the optimal way even if they
904 * are storred row-major in a UBO.
907 *inout
= vtn_local_load(b
, vtn_pointer_to_deref(b
, ptr
));
909 vtn_local_store(b
, *inout
, vtn_pointer_to_deref(b
, ptr
));
913 case GLSL_TYPE_ARRAY
:
914 case GLSL_TYPE_STRUCT
: {
915 unsigned elems
= glsl_get_length(ptr
->type
->type
);
917 vtn_assert(*inout
== NULL
);
918 *inout
= rzalloc(b
, struct vtn_ssa_value
);
919 (*inout
)->type
= ptr
->type
->type
;
920 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
923 struct vtn_access_chain chain
= {
926 { .mode
= vtn_access_mode_literal
, },
929 for (unsigned i
= 0; i
< elems
; i
++) {
930 chain
.link
[0].id
= i
;
931 struct vtn_pointer
*elem
= vtn_pointer_dereference(b
, ptr
, &chain
);
932 _vtn_variable_load_store(b
, load
, elem
, &(*inout
)->elems
[i
]);
938 vtn_fail("Invalid access chain type");
942 struct vtn_ssa_value
*
943 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
945 if (vtn_pointer_is_external_block(src
)) {
946 return vtn_block_load(b
, src
);
948 struct vtn_ssa_value
*val
= NULL
;
949 _vtn_variable_load_store(b
, true, src
, &val
);
955 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
956 struct vtn_pointer
*dest
)
958 if (vtn_pointer_is_external_block(dest
)) {
959 vtn_assert(dest
->mode
== vtn_variable_mode_ssbo
);
960 vtn_block_store(b
, src
, dest
);
962 _vtn_variable_load_store(b
, false, dest
, &src
);
967 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
968 struct vtn_pointer
*src
)
970 vtn_assert(src
->type
->type
== dest
->type
->type
);
971 enum glsl_base_type base_type
= glsl_get_base_type(src
->type
->type
);
975 case GLSL_TYPE_UINT64
:
976 case GLSL_TYPE_INT64
:
977 case GLSL_TYPE_FLOAT
:
978 case GLSL_TYPE_DOUBLE
:
980 /* At this point, we have a scalar, vector, or matrix so we know that
981 * there cannot be any structure splitting still in the way. By
982 * stopping at the matrix level rather than the vector level, we
983 * ensure that matrices get loaded in the optimal way even if they
984 * are storred row-major in a UBO.
986 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
989 case GLSL_TYPE_ARRAY
:
990 case GLSL_TYPE_STRUCT
: {
991 struct vtn_access_chain chain
= {
994 { .mode
= vtn_access_mode_literal
, },
997 unsigned elems
= glsl_get_length(src
->type
->type
);
998 for (unsigned i
= 0; i
< elems
; i
++) {
999 chain
.link
[0].id
= i
;
1000 struct vtn_pointer
*src_elem
=
1001 vtn_pointer_dereference(b
, src
, &chain
);
1002 struct vtn_pointer
*dest_elem
=
1003 vtn_pointer_dereference(b
, dest
, &chain
);
1005 _vtn_variable_copy(b
, dest_elem
, src_elem
);
1011 vtn_fail("Invalid access chain type");
1016 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
1017 struct vtn_pointer
*src
)
1019 /* TODO: At some point, we should add a special-case for when we can
1020 * just emit a copy_var intrinsic.
1022 _vtn_variable_copy(b
, dest
, src
);
1026 set_mode_system_value(struct vtn_builder
*b
, nir_variable_mode
*mode
)
1028 vtn_assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
1029 *mode
= nir_var_system_value
;
1033 vtn_get_builtin_location(struct vtn_builder
*b
,
1034 SpvBuiltIn builtin
, int *location
,
1035 nir_variable_mode
*mode
)
1038 case SpvBuiltInPosition
:
1039 *location
= VARYING_SLOT_POS
;
1041 case SpvBuiltInPointSize
:
1042 *location
= VARYING_SLOT_PSIZ
;
1044 case SpvBuiltInClipDistance
:
1045 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
1047 case SpvBuiltInCullDistance
:
1048 *location
= VARYING_SLOT_CULL_DIST0
;
1050 case SpvBuiltInVertexIndex
:
1051 *location
= SYSTEM_VALUE_VERTEX_ID
;
1052 set_mode_system_value(b
, mode
);
1054 case SpvBuiltInVertexId
:
1055 /* Vulkan defines VertexID to be zero-based and reserves the new
1056 * builtin keyword VertexIndex to indicate the non-zero-based value.
1058 *location
= SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1059 set_mode_system_value(b
, mode
);
1061 case SpvBuiltInInstanceIndex
:
1062 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
1063 set_mode_system_value(b
, mode
);
1065 case SpvBuiltInInstanceId
:
1066 *location
= SYSTEM_VALUE_INSTANCE_ID
;
1067 set_mode_system_value(b
, mode
);
1069 case SpvBuiltInPrimitiveId
:
1070 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1071 vtn_assert(*mode
== nir_var_shader_in
);
1072 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1073 } else if (*mode
== nir_var_shader_out
) {
1074 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1076 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
1077 set_mode_system_value(b
, mode
);
1080 case SpvBuiltInInvocationId
:
1081 *location
= SYSTEM_VALUE_INVOCATION_ID
;
1082 set_mode_system_value(b
, mode
);
1084 case SpvBuiltInLayer
:
1085 *location
= VARYING_SLOT_LAYER
;
1086 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1087 *mode
= nir_var_shader_in
;
1088 else if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1089 *mode
= nir_var_shader_out
;
1091 vtn_fail("invalid stage for SpvBuiltInLayer");
1093 case SpvBuiltInViewportIndex
:
1094 *location
= VARYING_SLOT_VIEWPORT
;
1095 if (b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
)
1096 *mode
= nir_var_shader_out
;
1097 else if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
)
1098 *mode
= nir_var_shader_in
;
1100 vtn_fail("invalid stage for SpvBuiltInViewportIndex");
1102 case SpvBuiltInTessLevelOuter
:
1103 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
1105 case SpvBuiltInTessLevelInner
:
1106 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
1108 case SpvBuiltInTessCoord
:
1109 *location
= SYSTEM_VALUE_TESS_COORD
;
1110 set_mode_system_value(b
, mode
);
1112 case SpvBuiltInPatchVertices
:
1113 *location
= SYSTEM_VALUE_VERTICES_IN
;
1114 set_mode_system_value(b
, mode
);
1116 case SpvBuiltInFragCoord
:
1117 *location
= VARYING_SLOT_POS
;
1118 vtn_assert(*mode
== nir_var_shader_in
);
1120 case SpvBuiltInPointCoord
:
1121 *location
= VARYING_SLOT_PNTC
;
1122 vtn_assert(*mode
== nir_var_shader_in
);
1124 case SpvBuiltInFrontFacing
:
1125 *location
= SYSTEM_VALUE_FRONT_FACE
;
1126 set_mode_system_value(b
, mode
);
1128 case SpvBuiltInSampleId
:
1129 *location
= SYSTEM_VALUE_SAMPLE_ID
;
1130 set_mode_system_value(b
, mode
);
1132 case SpvBuiltInSamplePosition
:
1133 *location
= SYSTEM_VALUE_SAMPLE_POS
;
1134 set_mode_system_value(b
, mode
);
1136 case SpvBuiltInSampleMask
:
1137 if (*mode
== nir_var_shader_out
) {
1138 *location
= FRAG_RESULT_SAMPLE_MASK
;
1140 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1141 set_mode_system_value(b
, mode
);
1144 case SpvBuiltInFragDepth
:
1145 *location
= FRAG_RESULT_DEPTH
;
1146 vtn_assert(*mode
== nir_var_shader_out
);
1148 case SpvBuiltInHelperInvocation
:
1149 *location
= SYSTEM_VALUE_HELPER_INVOCATION
;
1150 set_mode_system_value(b
, mode
);
1152 case SpvBuiltInNumWorkgroups
:
1153 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1154 set_mode_system_value(b
, mode
);
1156 case SpvBuiltInWorkgroupSize
:
1157 /* This should already be handled */
1158 vtn_fail("unsupported builtin");
1160 case SpvBuiltInWorkgroupId
:
1161 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1162 set_mode_system_value(b
, mode
);
1164 case SpvBuiltInLocalInvocationId
:
1165 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1166 set_mode_system_value(b
, mode
);
1168 case SpvBuiltInLocalInvocationIndex
:
1169 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1170 set_mode_system_value(b
, mode
);
1172 case SpvBuiltInGlobalInvocationId
:
1173 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1174 set_mode_system_value(b
, mode
);
1176 case SpvBuiltInBaseVertex
:
1177 *location
= SYSTEM_VALUE_BASE_VERTEX
;
1178 set_mode_system_value(b
, mode
);
1180 case SpvBuiltInBaseInstance
:
1181 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1182 set_mode_system_value(b
, mode
);
1184 case SpvBuiltInDrawIndex
:
1185 *location
= SYSTEM_VALUE_DRAW_ID
;
1186 set_mode_system_value(b
, mode
);
1188 case SpvBuiltInViewIndex
:
1189 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1190 set_mode_system_value(b
, mode
);
1193 vtn_fail("unsupported builtin");
1198 apply_var_decoration(struct vtn_builder
*b
, nir_variable
*nir_var
,
1199 const struct vtn_decoration
*dec
)
1201 switch (dec
->decoration
) {
1202 case SpvDecorationRelaxedPrecision
:
1203 break; /* FIXME: Do nothing with this for now. */
1204 case SpvDecorationNoPerspective
:
1205 nir_var
->data
.interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1207 case SpvDecorationFlat
:
1208 nir_var
->data
.interpolation
= INTERP_MODE_FLAT
;
1210 case SpvDecorationCentroid
:
1211 nir_var
->data
.centroid
= true;
1213 case SpvDecorationSample
:
1214 nir_var
->data
.sample
= true;
1216 case SpvDecorationInvariant
:
1217 nir_var
->data
.invariant
= true;
1219 case SpvDecorationConstant
:
1220 vtn_assert(nir_var
->constant_initializer
!= NULL
);
1221 nir_var
->data
.read_only
= true;
1223 case SpvDecorationNonReadable
:
1224 nir_var
->data
.image
.write_only
= true;
1226 case SpvDecorationNonWritable
:
1227 nir_var
->data
.read_only
= true;
1228 nir_var
->data
.image
.read_only
= true;
1230 case SpvDecorationComponent
:
1231 nir_var
->data
.location_frac
= dec
->literals
[0];
1233 case SpvDecorationIndex
:
1234 nir_var
->data
.index
= dec
->literals
[0];
1236 case SpvDecorationBuiltIn
: {
1237 SpvBuiltIn builtin
= dec
->literals
[0];
1239 if (builtin
== SpvBuiltInWorkgroupSize
) {
1240 /* This shouldn't be a builtin. It's actually a constant. */
1241 nir_var
->data
.mode
= nir_var_global
;
1242 nir_var
->data
.read_only
= true;
1244 nir_constant
*c
= rzalloc(nir_var
, nir_constant
);
1245 c
->values
[0].u32
[0] = b
->shader
->info
.cs
.local_size
[0];
1246 c
->values
[0].u32
[1] = b
->shader
->info
.cs
.local_size
[1];
1247 c
->values
[0].u32
[2] = b
->shader
->info
.cs
.local_size
[2];
1248 nir_var
->constant_initializer
= c
;
1252 nir_variable_mode mode
= nir_var
->data
.mode
;
1253 vtn_get_builtin_location(b
, builtin
, &nir_var
->data
.location
, &mode
);
1254 nir_var
->data
.mode
= mode
;
1257 case SpvBuiltInTessLevelOuter
:
1258 case SpvBuiltInTessLevelInner
:
1259 nir_var
->data
.compact
= true;
1261 case SpvBuiltInSamplePosition
:
1262 nir_var
->data
.origin_upper_left
= b
->origin_upper_left
;
1264 case SpvBuiltInFragCoord
:
1265 nir_var
->data
.pixel_center_integer
= b
->pixel_center_integer
;
1272 case SpvDecorationSpecId
:
1273 case SpvDecorationRowMajor
:
1274 case SpvDecorationColMajor
:
1275 case SpvDecorationMatrixStride
:
1276 case SpvDecorationRestrict
:
1277 case SpvDecorationAliased
:
1278 case SpvDecorationVolatile
:
1279 case SpvDecorationCoherent
:
1280 case SpvDecorationUniform
:
1281 case SpvDecorationStream
:
1282 case SpvDecorationOffset
:
1283 case SpvDecorationLinkageAttributes
:
1284 break; /* Do nothing with these here */
1286 case SpvDecorationPatch
:
1287 nir_var
->data
.patch
= true;
1290 case SpvDecorationLocation
:
1291 vtn_fail("Handled above");
1293 case SpvDecorationBlock
:
1294 case SpvDecorationBufferBlock
:
1295 case SpvDecorationArrayStride
:
1296 case SpvDecorationGLSLShared
:
1297 case SpvDecorationGLSLPacked
:
1298 break; /* These can apply to a type but we don't care about them */
1300 case SpvDecorationBinding
:
1301 case SpvDecorationDescriptorSet
:
1302 case SpvDecorationNoContraction
:
1303 case SpvDecorationInputAttachmentIndex
:
1304 vtn_warn("Decoration not allowed for variable or structure member: %s",
1305 spirv_decoration_to_string(dec
->decoration
));
1308 case SpvDecorationXfbBuffer
:
1309 case SpvDecorationXfbStride
:
1310 vtn_warn("Vulkan does not have transform feedback: %s",
1311 spirv_decoration_to_string(dec
->decoration
));
1314 case SpvDecorationCPacked
:
1315 case SpvDecorationSaturatedConversion
:
1316 case SpvDecorationFuncParamAttr
:
1317 case SpvDecorationFPRoundingMode
:
1318 case SpvDecorationFPFastMathMode
:
1319 case SpvDecorationAlignment
:
1320 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1321 spirv_decoration_to_string(dec
->decoration
));
1325 vtn_fail("Unhandled decoration");
1330 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1331 const struct vtn_decoration
*dec
, void *out_is_patch
)
1333 if (dec
->decoration
== SpvDecorationPatch
) {
1334 *((bool *) out_is_patch
) = true;
1339 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1340 const struct vtn_decoration
*dec
, void *void_var
)
1342 struct vtn_variable
*vtn_var
= void_var
;
1344 /* Handle decorations that apply to a vtn_variable as a whole */
1345 switch (dec
->decoration
) {
1346 case SpvDecorationBinding
:
1347 vtn_var
->binding
= dec
->literals
[0];
1349 case SpvDecorationDescriptorSet
:
1350 vtn_var
->descriptor_set
= dec
->literals
[0];
1352 case SpvDecorationInputAttachmentIndex
:
1353 vtn_var
->input_attachment_index
= dec
->literals
[0];
1355 case SpvDecorationPatch
:
1356 vtn_var
->patch
= true;
1362 if (val
->value_type
== vtn_value_type_pointer
) {
1363 vtn_assert(val
->pointer
->var
== void_var
);
1364 vtn_assert(val
->pointer
->chain
== NULL
);
1365 vtn_assert(member
== -1);
1367 vtn_assert(val
->value_type
== vtn_value_type_type
);
1370 /* Location is odd. If applied to a split structure, we have to walk the
1371 * whole thing and accumulate the location. It's easier to handle as a
1374 if (dec
->decoration
== SpvDecorationLocation
) {
1375 unsigned location
= dec
->literals
[0];
1376 bool is_vertex_input
;
1377 if (b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
&&
1378 vtn_var
->mode
== vtn_variable_mode_output
) {
1379 is_vertex_input
= false;
1380 location
+= FRAG_RESULT_DATA0
;
1381 } else if (b
->shader
->info
.stage
== MESA_SHADER_VERTEX
&&
1382 vtn_var
->mode
== vtn_variable_mode_input
) {
1383 is_vertex_input
= true;
1384 location
+= VERT_ATTRIB_GENERIC0
;
1385 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1386 vtn_var
->mode
== vtn_variable_mode_output
) {
1387 is_vertex_input
= false;
1388 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1390 vtn_warn("Location must be on input or output variable");
1395 /* This handles the member and lone variable cases */
1396 vtn_var
->var
->data
.location
= location
;
1398 /* This handles the structure member case */
1399 vtn_assert(vtn_var
->members
);
1401 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1402 for (unsigned i
= 0; i
< length
; i
++) {
1403 vtn_var
->members
[i
]->data
.location
= location
;
1405 glsl_count_attribute_slots(vtn_var
->members
[i
]->interface_type
,
1412 vtn_assert(member
<= 0);
1413 apply_var_decoration(b
, vtn_var
->var
, dec
);
1414 } else if (vtn_var
->members
) {
1416 vtn_assert(vtn_var
->members
);
1417 apply_var_decoration(b
, vtn_var
->members
[member
], dec
);
1420 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1421 for (unsigned i
= 0; i
< length
; i
++)
1422 apply_var_decoration(b
, vtn_var
->members
[i
], dec
);
1425 /* A few variables, those with external storage, have no actual
1426 * nir_variables associated with them. Fortunately, all decorations
1427 * we care about for those variables are on the type only.
1429 vtn_assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1430 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1431 vtn_var
->mode
== vtn_variable_mode_push_constant
);
1436 static enum vtn_variable_mode
1437 vtn_storage_class_to_mode(struct vtn_builder
*b
,
1438 SpvStorageClass
class,
1439 struct vtn_type
*interface_type
,
1440 nir_variable_mode
*nir_mode_out
)
1442 enum vtn_variable_mode mode
;
1443 nir_variable_mode nir_mode
;
1445 case SpvStorageClassUniform
:
1446 if (interface_type
->block
) {
1447 mode
= vtn_variable_mode_ubo
;
1449 } else if (interface_type
->buffer_block
) {
1450 mode
= vtn_variable_mode_ssbo
;
1453 vtn_fail("Invalid uniform variable type");
1456 case SpvStorageClassStorageBuffer
:
1457 mode
= vtn_variable_mode_ssbo
;
1460 case SpvStorageClassUniformConstant
:
1461 if (glsl_type_is_image(interface_type
->type
)) {
1462 mode
= vtn_variable_mode_image
;
1463 nir_mode
= nir_var_uniform
;
1464 } else if (glsl_type_is_sampler(interface_type
->type
)) {
1465 mode
= vtn_variable_mode_sampler
;
1466 nir_mode
= nir_var_uniform
;
1468 vtn_fail("Invalid uniform constant variable type");
1471 case SpvStorageClassPushConstant
:
1472 mode
= vtn_variable_mode_push_constant
;
1473 nir_mode
= nir_var_uniform
;
1475 case SpvStorageClassInput
:
1476 mode
= vtn_variable_mode_input
;
1477 nir_mode
= nir_var_shader_in
;
1479 case SpvStorageClassOutput
:
1480 mode
= vtn_variable_mode_output
;
1481 nir_mode
= nir_var_shader_out
;
1483 case SpvStorageClassPrivate
:
1484 mode
= vtn_variable_mode_global
;
1485 nir_mode
= nir_var_global
;
1487 case SpvStorageClassFunction
:
1488 mode
= vtn_variable_mode_local
;
1489 nir_mode
= nir_var_local
;
1491 case SpvStorageClassWorkgroup
:
1492 mode
= vtn_variable_mode_workgroup
;
1493 nir_mode
= nir_var_shared
;
1495 case SpvStorageClassCrossWorkgroup
:
1496 case SpvStorageClassGeneric
:
1497 case SpvStorageClassAtomicCounter
:
1499 vtn_fail("Unhandled variable storage class");
1503 *nir_mode_out
= nir_mode
;
1509 vtn_pointer_to_ssa(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
1511 /* This pointer needs to have a pointer type with actual storage */
1512 vtn_assert(ptr
->ptr_type
);
1513 vtn_assert(ptr
->ptr_type
->type
);
1515 if (!ptr
->offset
|| !ptr
->block_index
) {
1516 /* If we don't have an offset then we must be a pointer to the variable
1519 vtn_assert(!ptr
->offset
&& !ptr
->block_index
);
1521 /* We can't handle a pointer to an array of descriptors because we have
1522 * no way of knowing later on that we need to add to update the block
1523 * index when dereferencing.
1525 vtn_assert(ptr
->var
&& ptr
->var
->type
->base_type
== vtn_base_type_struct
);
1527 struct vtn_access_chain chain
= {
1530 ptr
= vtn_ssa_offset_pointer_dereference(b
, ptr
, &chain
);
1533 vtn_assert(ptr
->offset
&& ptr
->block_index
);
1534 return nir_vec2(&b
->nb
, ptr
->block_index
, ptr
->offset
);
1537 struct vtn_pointer
*
1538 vtn_pointer_from_ssa(struct vtn_builder
*b
, nir_ssa_def
*ssa
,
1539 struct vtn_type
*ptr_type
)
1541 vtn_assert(ssa
->num_components
== 2 && ssa
->bit_size
== 32);
1542 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1543 vtn_assert(ptr_type
->deref
->base_type
!= vtn_base_type_pointer
);
1544 /* This pointer type needs to have actual storage */
1545 vtn_assert(ptr_type
->type
);
1547 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
1548 ptr
->mode
= vtn_storage_class_to_mode(b
, ptr_type
->storage_class
,
1550 ptr
->type
= ptr_type
->deref
;
1551 ptr
->ptr_type
= ptr_type
;
1552 ptr
->block_index
= nir_channel(&b
->nb
, ssa
, 0);
1553 ptr
->offset
= nir_channel(&b
->nb
, ssa
, 1);
1559 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
1561 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
1564 if (var
->mode
== vtn_variable_mode_input
) {
1565 return stage
== MESA_SHADER_TESS_CTRL
||
1566 stage
== MESA_SHADER_TESS_EVAL
||
1567 stage
== MESA_SHADER_GEOMETRY
;
1570 if (var
->mode
== vtn_variable_mode_output
)
1571 return stage
== MESA_SHADER_TESS_CTRL
;
1577 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
1578 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
1579 nir_constant
*initializer
)
1581 vtn_assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1582 struct vtn_type
*type
= ptr_type
->deref
;
1584 struct vtn_type
*without_array
= type
;
1585 while(glsl_type_is_array(without_array
->type
))
1586 without_array
= without_array
->array_element
;
1588 enum vtn_variable_mode mode
;
1589 nir_variable_mode nir_mode
;
1590 mode
= vtn_storage_class_to_mode(b
, storage_class
, without_array
, &nir_mode
);
1593 case vtn_variable_mode_ubo
:
1594 b
->shader
->info
.num_ubos
++;
1596 case vtn_variable_mode_ssbo
:
1597 b
->shader
->info
.num_ssbos
++;
1599 case vtn_variable_mode_image
:
1600 b
->shader
->info
.num_images
++;
1602 case vtn_variable_mode_sampler
:
1603 b
->shader
->info
.num_textures
++;
1605 case vtn_variable_mode_push_constant
:
1606 b
->shader
->num_uniforms
= vtn_type_block_size(b
, type
);
1609 /* No tallying is needed */
1613 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
1617 vtn_assert(val
->value_type
== vtn_value_type_pointer
);
1618 val
->pointer
= vtn_pointer_for_variable(b
, var
, ptr_type
);
1620 switch (var
->mode
) {
1621 case vtn_variable_mode_local
:
1622 case vtn_variable_mode_global
:
1623 case vtn_variable_mode_image
:
1624 case vtn_variable_mode_sampler
:
1625 case vtn_variable_mode_workgroup
:
1626 /* For these, we create the variable normally */
1627 var
->var
= rzalloc(b
->shader
, nir_variable
);
1628 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1629 var
->var
->type
= var
->type
->type
;
1630 var
->var
->data
.mode
= nir_mode
;
1632 switch (var
->mode
) {
1633 case vtn_variable_mode_image
:
1634 case vtn_variable_mode_sampler
:
1635 var
->var
->interface_type
= without_array
->type
;
1638 var
->var
->interface_type
= NULL
;
1643 case vtn_variable_mode_input
:
1644 case vtn_variable_mode_output
: {
1645 /* In order to know whether or not we're a per-vertex inout, we need
1646 * the patch qualifier. This means walking the variable decorations
1647 * early before we actually create any variables. Not a big deal.
1649 * GLSLang really likes to place decorations in the most interior
1650 * thing it possibly can. In particular, if you have a struct, it
1651 * will place the patch decorations on the struct members. This
1652 * should be handled by the variable splitting below just fine.
1654 * If you have an array-of-struct, things get even more weird as it
1655 * will place the patch decorations on the struct even though it's
1656 * inside an array and some of the members being patch and others not
1657 * makes no sense whatsoever. Since the only sensible thing is for
1658 * it to be all or nothing, we'll call it patch if any of the members
1659 * are declared patch.
1662 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
1663 if (glsl_type_is_array(var
->type
->type
) &&
1664 glsl_type_is_struct(without_array
->type
)) {
1665 vtn_foreach_decoration(b
, without_array
->val
,
1666 var_is_patch_cb
, &var
->patch
);
1669 /* For inputs and outputs, we immediately split structures. This
1670 * is for a couple of reasons. For one, builtins may all come in
1671 * a struct and we really want those split out into separate
1672 * variables. For another, interpolation qualifiers can be
1673 * applied to members of the top-level struct ane we need to be
1674 * able to preserve that information.
1677 int array_length
= -1;
1678 struct vtn_type
*interface_type
= var
->type
;
1679 if (is_per_vertex_inout(var
, b
->shader
->info
.stage
)) {
1680 /* In Geometry shaders (and some tessellation), inputs come
1681 * in per-vertex arrays. However, some builtins come in
1682 * non-per-vertex, hence the need for the is_array check. In
1683 * any case, there are no non-builtin arrays allowed so this
1684 * check should be sufficient.
1686 interface_type
= var
->type
->array_element
;
1687 array_length
= glsl_get_length(var
->type
->type
);
1690 if (glsl_type_is_struct(interface_type
->type
)) {
1691 /* It's a struct. Split it. */
1692 unsigned num_members
= glsl_get_length(interface_type
->type
);
1693 var
->members
= ralloc_array(b
, nir_variable
*, num_members
);
1695 for (unsigned i
= 0; i
< num_members
; i
++) {
1696 const struct glsl_type
*mtype
= interface_type
->members
[i
]->type
;
1697 if (array_length
>= 0)
1698 mtype
= glsl_array_type(mtype
, array_length
);
1700 var
->members
[i
] = rzalloc(b
->shader
, nir_variable
);
1701 var
->members
[i
]->name
=
1702 ralloc_asprintf(var
->members
[i
], "%s.%d", val
->name
, i
);
1703 var
->members
[i
]->type
= mtype
;
1704 var
->members
[i
]->interface_type
=
1705 interface_type
->members
[i
]->type
;
1706 var
->members
[i
]->data
.mode
= nir_mode
;
1707 var
->members
[i
]->data
.patch
= var
->patch
;
1710 var
->var
= rzalloc(b
->shader
, nir_variable
);
1711 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1712 var
->var
->type
= var
->type
->type
;
1713 var
->var
->interface_type
= interface_type
->type
;
1714 var
->var
->data
.mode
= nir_mode
;
1715 var
->var
->data
.patch
= var
->patch
;
1718 /* For inputs and outputs, we need to grab locations and builtin
1719 * information from the interface type.
1721 vtn_foreach_decoration(b
, interface_type
->val
, var_decoration_cb
, var
);
1725 case vtn_variable_mode_param
:
1726 vtn_fail("Not created through OpVariable");
1728 case vtn_variable_mode_ubo
:
1729 case vtn_variable_mode_ssbo
:
1730 case vtn_variable_mode_push_constant
:
1731 /* These don't need actual variables. */
1736 var
->var
->constant_initializer
=
1737 nir_constant_clone(initializer
, var
->var
);
1740 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
1742 if (var
->mode
== vtn_variable_mode_image
||
1743 var
->mode
== vtn_variable_mode_sampler
) {
1744 /* XXX: We still need the binding information in the nir_variable
1745 * for these. We should fix that.
1747 var
->var
->data
.binding
= var
->binding
;
1748 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
1749 var
->var
->data
.index
= var
->input_attachment_index
;
1751 if (var
->mode
== vtn_variable_mode_image
)
1752 var
->var
->data
.image
.format
= without_array
->image_format
;
1755 if (var
->mode
== vtn_variable_mode_local
) {
1756 vtn_assert(var
->members
== NULL
&& var
->var
!= NULL
);
1757 nir_function_impl_add_variable(b
->nb
.impl
, var
->var
);
1758 } else if (var
->var
) {
1759 nir_shader_add_variable(b
->shader
, var
->var
);
1760 } else if (var
->members
) {
1761 unsigned count
= glsl_get_length(without_array
->type
);
1762 for (unsigned i
= 0; i
< count
; i
++) {
1763 vtn_assert(var
->members
[i
]->data
.mode
!= nir_var_local
);
1764 nir_shader_add_variable(b
->shader
, var
->members
[i
]);
1767 vtn_assert(var
->mode
== vtn_variable_mode_ubo
||
1768 var
->mode
== vtn_variable_mode_ssbo
||
1769 var
->mode
== vtn_variable_mode_push_constant
);
1774 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
1775 const uint32_t *w
, unsigned count
)
1779 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1780 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1784 case SpvOpVariable
: {
1785 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1787 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1789 SpvStorageClass storage_class
= w
[3];
1790 nir_constant
*initializer
= NULL
;
1792 initializer
= vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
1794 vtn_create_variable(b
, val
, ptr_type
, storage_class
, initializer
);
1798 case SpvOpAccessChain
:
1799 case SpvOpPtrAccessChain
:
1800 case SpvOpInBoundsAccessChain
: {
1801 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
1802 chain
->ptr_as_array
= (opcode
== SpvOpPtrAccessChain
);
1805 for (int i
= 4; i
< count
; i
++) {
1806 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
1807 if (link_val
->value_type
== vtn_value_type_constant
) {
1808 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
1809 chain
->link
[idx
].id
= link_val
->constant
->values
[0].u32
[0];
1811 chain
->link
[idx
].mode
= vtn_access_mode_id
;
1812 chain
->link
[idx
].id
= w
[i
];
1818 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1819 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
1820 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1821 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1822 * to combine an array of images with a single sampler to get an
1823 * array of sampled images that all share the same sampler.
1824 * Fortunately, this means that we can more-or-less ignore the
1825 * sampler when crawling the access chain, but it does leave us
1826 * with this rather awkward little special-case.
1828 struct vtn_value
*val
=
1829 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1830 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1831 val
->sampled_image
->type
= base_val
->sampled_image
->type
;
1832 val
->sampled_image
->image
=
1833 vtn_pointer_dereference(b
, base_val
->sampled_image
->image
, chain
);
1834 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
1836 vtn_assert(base_val
->value_type
== vtn_value_type_pointer
);
1837 struct vtn_value
*val
=
1838 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1839 val
->pointer
= vtn_pointer_dereference(b
, base_val
->pointer
, chain
);
1840 val
->pointer
->ptr_type
= ptr_type
;
1845 case SpvOpCopyMemory
: {
1846 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
1847 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
1849 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
1854 struct vtn_type
*res_type
=
1855 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1856 struct vtn_pointer
*src
=
1857 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1859 if (src
->mode
== vtn_variable_mode_image
||
1860 src
->mode
== vtn_variable_mode_sampler
) {
1861 vtn_push_value(b
, w
[2], vtn_value_type_pointer
)->pointer
= src
;
1865 vtn_push_ssa(b
, w
[2], res_type
, vtn_variable_load(b
, src
));
1870 struct vtn_pointer
*dest
=
1871 vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
1873 if (glsl_type_is_sampler(dest
->type
->type
)) {
1874 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
1875 "propagation to workaround the problem.");
1876 vtn_assert(dest
->var
->copy_prop_sampler
== NULL
);
1877 dest
->var
->copy_prop_sampler
=
1878 vtn_value(b
, w
[2], vtn_value_type_pointer
)->pointer
;
1882 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
1883 vtn_variable_store(b
, src
, dest
);
1887 case SpvOpArrayLength
: {
1888 struct vtn_pointer
*ptr
=
1889 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1891 const uint32_t offset
= ptr
->var
->type
->offsets
[w
[4]];
1892 const uint32_t stride
= ptr
->var
->type
->members
[w
[4]]->stride
;
1895 struct vtn_type
*type
;
1896 nir_ssa_def
*index
=
1897 get_vulkan_resource_index(b
, ptr
, &type
, &chain_idx
);
1899 nir_intrinsic_instr
*instr
=
1900 nir_intrinsic_instr_create(b
->nb
.shader
,
1901 nir_intrinsic_get_buffer_size
);
1902 instr
->src
[0] = nir_src_for_ssa(index
);
1903 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
1904 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
1905 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
1907 /* array_length = max(buffer_size - offset, 0) / stride */
1908 nir_ssa_def
*array_length
=
1913 nir_imm_int(&b
->nb
, offset
)),
1914 nir_imm_int(&b
->nb
, 0u)),
1915 nir_imm_int(&b
->nb
, stride
));
1917 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1918 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
1919 val
->ssa
->def
= array_length
;
1923 case SpvOpCopyMemorySized
:
1925 vtn_fail("Unhandled opcode");