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 unsigned start
= base
->chain
? base
->chain
->length
: 0;
71 for (unsigned i
= 0; i
< deref_chain
->length
; i
++) {
72 chain
->link
[start
+ i
] = deref_chain
->link
[i
];
74 if (glsl_type_is_struct(type
->type
)) {
75 assert(deref_chain
->link
[i
].mode
== vtn_access_mode_literal
);
76 type
= type
->members
[deref_chain
->link
[i
].id
];
78 type
= type
->array_element
;
82 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
83 ptr
->mode
= base
->mode
;
92 vtn_access_link_as_ssa(struct vtn_builder
*b
, struct vtn_access_link link
,
96 if (link
.mode
== vtn_access_mode_literal
) {
97 return nir_imm_int(&b
->nb
, link
.id
* stride
);
98 } else if (stride
== 1) {
99 return vtn_ssa_value(b
, link
.id
)->def
;
101 return nir_imul(&b
->nb
, vtn_ssa_value(b
, link
.id
)->def
,
102 nir_imm_int(&b
->nb
, stride
));
107 vtn_variable_resource_index(struct vtn_builder
*b
, struct vtn_variable
*var
,
108 nir_ssa_def
*desc_array_index
)
110 if (!desc_array_index
) {
111 assert(glsl_type_is_struct(var
->type
->type
));
112 desc_array_index
= nir_imm_int(&b
->nb
, 0);
115 nir_intrinsic_instr
*instr
=
116 nir_intrinsic_instr_create(b
->nb
.shader
,
117 nir_intrinsic_vulkan_resource_index
);
118 instr
->src
[0] = nir_src_for_ssa(desc_array_index
);
119 nir_intrinsic_set_desc_set(instr
, var
->descriptor_set
);
120 nir_intrinsic_set_binding(instr
, var
->binding
);
122 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
123 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
125 return &instr
->dest
.ssa
;
128 static struct vtn_pointer
*
129 vtn_ssa_offset_pointer_dereference(struct vtn_builder
*b
,
130 struct vtn_pointer
*base
,
131 struct vtn_access_chain
*deref_chain
)
133 nir_ssa_def
*block_index
= base
->block_index
;
134 nir_ssa_def
*offset
= base
->offset
;
135 struct vtn_type
*type
= base
->type
;
140 if (glsl_type_is_array(type
->type
)) {
141 /* We need at least one element in the chain */
142 assert(deref_chain
->length
>= 1);
144 nir_ssa_def
*desc_arr_idx
=
145 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1);
146 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
147 type
= type
->array_element
;
150 block_index
= vtn_variable_resource_index(b
, base
->var
, NULL
);
153 /* This is the first access chain so we also need an offset */
155 offset
= nir_imm_int(&b
->nb
, 0);
159 for (; idx
< deref_chain
->length
; idx
++) {
160 switch (glsl_get_base_type(type
->type
)) {
163 case GLSL_TYPE_UINT64
:
164 case GLSL_TYPE_INT64
:
165 case GLSL_TYPE_FLOAT
:
166 case GLSL_TYPE_DOUBLE
:
168 case GLSL_TYPE_ARRAY
: {
169 nir_ssa_def
*elem_offset
=
170 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
], type
->stride
);
171 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
172 type
= type
->array_element
;
176 case GLSL_TYPE_STRUCT
: {
177 assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
178 unsigned member
= deref_chain
->link
[idx
].id
;
179 nir_ssa_def
*mem_offset
= nir_imm_int(&b
->nb
, type
->offsets
[member
]);
180 offset
= nir_iadd(&b
->nb
, offset
, mem_offset
);
181 type
= type
->members
[member
];
186 unreachable("Invalid type for deref");
190 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
191 ptr
->mode
= base
->mode
;
193 ptr
->block_index
= block_index
;
194 ptr
->offset
= offset
;
199 /* Dereference the given base pointer by the access chain */
200 static struct vtn_pointer
*
201 vtn_pointer_dereference(struct vtn_builder
*b
,
202 struct vtn_pointer
*base
,
203 struct vtn_access_chain
*deref_chain
)
205 if (base
->mode
== vtn_variable_mode_ubo
||
206 base
->mode
== vtn_variable_mode_ssbo
) {
207 return vtn_ssa_offset_pointer_dereference(b
, base
, deref_chain
);
209 return vtn_access_chain_pointer_dereference(b
, base
, deref_chain
);
213 /* Crawls a chain of array derefs and rewrites the types so that the
214 * lengths stay the same but the terminal type is the one given by
215 * tail_type. This is useful for split structures.
218 rewrite_deref_types(nir_deref
*deref
, const struct glsl_type
*type
)
222 assert(deref
->child
->deref_type
== nir_deref_type_array
);
223 assert(glsl_type_is_array(deref
->type
));
224 rewrite_deref_types(deref
->child
, glsl_get_array_element(type
));
229 vtn_pointer_for_variable(struct vtn_builder
*b
,
230 struct vtn_variable
*var
, struct vtn_type
*ptr_type
)
232 struct vtn_pointer
*pointer
= rzalloc(b
, struct vtn_pointer
);
234 pointer
->mode
= var
->mode
;
235 pointer
->type
= var
->type
;
236 assert(ptr_type
->base_type
== vtn_base_type_pointer
);
237 assert(ptr_type
->deref
->type
== var
->type
->type
);
238 pointer
->ptr_type
= ptr_type
;
245 vtn_pointer_to_deref(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
247 /* Do on-the-fly copy propagation for samplers. */
248 if (ptr
->var
->copy_prop_sampler
)
249 return vtn_pointer_to_deref(b
, ptr
->var
->copy_prop_sampler
);
251 nir_deref_var
*deref_var
;
253 deref_var
= nir_deref_var_create(b
, ptr
->var
->var
);
254 /* Raw variable access */
258 assert(ptr
->var
->members
);
259 /* Create the deref_var manually. It will get filled out later. */
260 deref_var
= rzalloc(b
, nir_deref_var
);
261 deref_var
->deref
.deref_type
= nir_deref_type_var
;
264 struct vtn_access_chain
*chain
= ptr
->chain
;
267 struct vtn_type
*deref_type
= ptr
->var
->type
;
268 nir_deref
*tail
= &deref_var
->deref
;
269 nir_variable
**members
= ptr
->var
->members
;
271 for (unsigned i
= 0; i
< chain
->length
; i
++) {
272 enum glsl_base_type base_type
= glsl_get_base_type(deref_type
->type
);
276 case GLSL_TYPE_UINT64
:
277 case GLSL_TYPE_INT64
:
278 case GLSL_TYPE_FLOAT
:
279 case GLSL_TYPE_DOUBLE
:
281 case GLSL_TYPE_ARRAY
: {
282 deref_type
= deref_type
->array_element
;
284 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
285 deref_arr
->deref
.type
= deref_type
->type
;
287 if (chain
->link
[i
].mode
== vtn_access_mode_literal
) {
288 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
289 deref_arr
->base_offset
= chain
->link
[i
].id
;
291 assert(chain
->link
[i
].mode
== vtn_access_mode_id
);
292 deref_arr
->deref_array_type
= nir_deref_array_type_indirect
;
293 deref_arr
->base_offset
= 0;
294 deref_arr
->indirect
=
295 nir_src_for_ssa(vtn_ssa_value(b
, chain
->link
[i
].id
)->def
);
297 tail
->child
= &deref_arr
->deref
;
302 case GLSL_TYPE_STRUCT
: {
303 assert(chain
->link
[i
].mode
== vtn_access_mode_literal
);
304 unsigned idx
= chain
->link
[i
].id
;
305 deref_type
= deref_type
->members
[idx
];
307 /* This is a pre-split structure. */
308 deref_var
->var
= members
[idx
];
309 rewrite_deref_types(&deref_var
->deref
, members
[idx
]->type
);
310 assert(tail
->type
== deref_type
->type
);
313 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, idx
);
314 deref_struct
->deref
.type
= deref_type
->type
;
315 tail
->child
= &deref_struct
->deref
;
321 unreachable("Invalid type for deref");
325 assert(members
== NULL
);
330 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_var
*deref
,
331 nir_deref
*tail
, struct vtn_ssa_value
*inout
)
333 /* The deref tail may contain a deref to select a component of a vector (in
334 * other words, it might not be an actual tail) so we have to save it away
335 * here since we overwrite it later.
337 nir_deref
*old_child
= tail
->child
;
339 if (glsl_type_is_vector_or_scalar(tail
->type
)) {
340 /* Terminate the deref chain in case there is one more link to pick
341 * off a component of the vector.
345 nir_intrinsic_op op
= load
? nir_intrinsic_load_var
:
346 nir_intrinsic_store_var
;
348 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
349 intrin
->variables
[0] = nir_deref_var_clone(deref
, intrin
);
350 intrin
->num_components
= glsl_get_vector_elements(tail
->type
);
353 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
354 intrin
->num_components
,
355 glsl_get_bit_size(tail
->type
),
357 inout
->def
= &intrin
->dest
.ssa
;
359 nir_intrinsic_set_write_mask(intrin
, (1 << intrin
->num_components
) - 1);
360 intrin
->src
[0] = nir_src_for_ssa(inout
->def
);
363 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
364 } else if (glsl_get_base_type(tail
->type
) == GLSL_TYPE_ARRAY
||
365 glsl_type_is_matrix(tail
->type
)) {
366 unsigned elems
= glsl_get_length(tail
->type
);
367 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
368 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
369 deref_arr
->deref
.type
= glsl_get_array_element(tail
->type
);
370 tail
->child
= &deref_arr
->deref
;
371 for (unsigned i
= 0; i
< elems
; i
++) {
372 deref_arr
->base_offset
= i
;
373 _vtn_local_load_store(b
, load
, deref
, tail
->child
, inout
->elems
[i
]);
376 assert(glsl_get_base_type(tail
->type
) == GLSL_TYPE_STRUCT
);
377 unsigned elems
= glsl_get_length(tail
->type
);
378 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, 0);
379 tail
->child
= &deref_struct
->deref
;
380 for (unsigned i
= 0; i
< elems
; i
++) {
381 deref_struct
->index
= i
;
382 deref_struct
->deref
.type
= glsl_get_struct_field(tail
->type
, i
);
383 _vtn_local_load_store(b
, load
, deref
, tail
->child
, inout
->elems
[i
]);
387 tail
->child
= old_child
;
391 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
393 struct vtn_pointer
*ptr
= vtn_value(b
, id
, vtn_value_type_pointer
)->pointer
;
394 return vtn_pointer_to_deref(b
, ptr
);
398 * Gets the NIR-level deref tail, which may have as a child an array deref
399 * selecting which component due to OpAccessChain supporting per-component
400 * indexing in SPIR-V.
403 get_deref_tail(nir_deref_var
*deref
)
405 nir_deref
*cur
= &deref
->deref
;
406 while (!glsl_type_is_vector_or_scalar(cur
->type
) && cur
->child
)
412 struct vtn_ssa_value
*
413 vtn_local_load(struct vtn_builder
*b
, nir_deref_var
*src
)
415 nir_deref
*src_tail
= get_deref_tail(src
);
416 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
417 _vtn_local_load_store(b
, true, src
, src_tail
, val
);
419 if (src_tail
->child
) {
420 nir_deref_array
*vec_deref
= nir_deref_as_array(src_tail
->child
);
421 assert(vec_deref
->deref
.child
== NULL
);
422 val
->type
= vec_deref
->deref
.type
;
423 if (vec_deref
->deref_array_type
== nir_deref_array_type_direct
)
424 val
->def
= vtn_vector_extract(b
, val
->def
, vec_deref
->base_offset
);
426 val
->def
= vtn_vector_extract_dynamic(b
, val
->def
,
427 vec_deref
->indirect
.ssa
);
434 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
437 nir_deref
*dest_tail
= get_deref_tail(dest
);
439 if (dest_tail
->child
) {
440 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
441 _vtn_local_load_store(b
, true, dest
, dest_tail
, val
);
442 nir_deref_array
*deref
= nir_deref_as_array(dest_tail
->child
);
443 assert(deref
->deref
.child
== NULL
);
444 if (deref
->deref_array_type
== nir_deref_array_type_direct
)
445 val
->def
= vtn_vector_insert(b
, val
->def
, src
->def
,
448 val
->def
= vtn_vector_insert_dynamic(b
, val
->def
, src
->def
,
449 deref
->indirect
.ssa
);
450 _vtn_local_load_store(b
, false, dest
, dest_tail
, val
);
452 _vtn_local_load_store(b
, false, dest
, dest_tail
, src
);
457 get_vulkan_resource_index(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
458 struct vtn_type
**type
, unsigned *chain_idx
)
460 /* Push constants have no explicit binding */
461 if (ptr
->mode
== vtn_variable_mode_push_constant
) {
463 *type
= ptr
->var
->type
;
467 if (glsl_type_is_array(ptr
->var
->type
->type
)) {
468 assert(ptr
->chain
->length
> 0);
469 nir_ssa_def
*desc_array_index
=
470 vtn_access_link_as_ssa(b
, ptr
->chain
->link
[0], 1);
472 *type
= ptr
->var
->type
->array_element
;
473 return vtn_variable_resource_index(b
, ptr
->var
, desc_array_index
);
476 *type
= ptr
->var
->type
;
477 return vtn_variable_resource_index(b
, ptr
->var
, NULL
);
482 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
483 nir_ssa_def
**index_out
, unsigned *end_idx_out
)
486 assert(ptr
->block_index
);
487 *index_out
= ptr
->block_index
;
492 struct vtn_type
*type
;
493 *index_out
= get_vulkan_resource_index(b
, ptr
, &type
, &idx
);
495 nir_ssa_def
*offset
= nir_imm_int(&b
->nb
, 0);
496 for (; idx
< ptr
->chain
->length
; idx
++) {
497 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
501 case GLSL_TYPE_UINT64
:
502 case GLSL_TYPE_INT64
:
503 case GLSL_TYPE_FLOAT
:
504 case GLSL_TYPE_DOUBLE
:
506 case GLSL_TYPE_ARRAY
:
507 offset
= nir_iadd(&b
->nb
, offset
,
508 vtn_access_link_as_ssa(b
, ptr
->chain
->link
[idx
],
511 type
= type
->array_element
;
514 case GLSL_TYPE_STRUCT
: {
515 assert(ptr
->chain
->link
[idx
].mode
== vtn_access_mode_literal
);
516 unsigned member
= ptr
->chain
->link
[idx
].id
;
517 offset
= nir_iadd(&b
->nb
, offset
,
518 nir_imm_int(&b
->nb
, type
->offsets
[member
]));
519 type
= type
->members
[member
];
524 unreachable("Invalid type for deref");
528 assert(type
== ptr
->type
);
535 /* Tries to compute the size of an interface block based on the strides and
536 * offsets that are provided to us in the SPIR-V source.
539 vtn_type_block_size(struct vtn_type
*type
)
541 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
545 case GLSL_TYPE_UINT64
:
546 case GLSL_TYPE_INT64
:
547 case GLSL_TYPE_FLOAT
:
549 case GLSL_TYPE_DOUBLE
: {
550 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
551 glsl_get_matrix_columns(type
->type
);
553 assert(type
->stride
> 0);
554 return type
->stride
* cols
;
555 } else if (base_type
== GLSL_TYPE_DOUBLE
||
556 base_type
== GLSL_TYPE_UINT64
||
557 base_type
== GLSL_TYPE_INT64
) {
558 return glsl_get_vector_elements(type
->type
) * 8;
560 return glsl_get_vector_elements(type
->type
) * 4;
564 case GLSL_TYPE_STRUCT
:
565 case GLSL_TYPE_INTERFACE
: {
567 unsigned num_fields
= glsl_get_length(type
->type
);
568 for (unsigned f
= 0; f
< num_fields
; f
++) {
569 unsigned field_end
= type
->offsets
[f
] +
570 vtn_type_block_size(type
->members
[f
]);
571 size
= MAX2(size
, field_end
);
576 case GLSL_TYPE_ARRAY
:
577 assert(type
->stride
> 0);
578 assert(glsl_get_length(type
->type
) > 0);
579 return type
->stride
* glsl_get_length(type
->type
);
582 assert(!"Invalid block type");
588 vtn_access_chain_get_offset_size(struct vtn_access_chain
*chain
,
589 struct vtn_type
*type
,
590 unsigned *access_offset
,
591 unsigned *access_size
)
595 for (unsigned i
= 0; i
< chain
->length
; i
++) {
596 if (chain
->link
[i
].mode
!= vtn_access_mode_literal
)
599 if (glsl_type_is_struct(type
->type
)) {
600 *access_offset
+= type
->offsets
[chain
->link
[i
].id
];
601 type
= type
->members
[chain
->link
[i
].id
];
603 *access_offset
+= type
->stride
* chain
->link
[i
].id
;
604 type
= type
->array_element
;
608 *access_size
= vtn_type_block_size(type
);
612 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
613 nir_ssa_def
*index
, nir_ssa_def
*offset
,
614 unsigned access_offset
, unsigned access_size
,
615 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
)
617 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
618 instr
->num_components
= glsl_get_vector_elements(type
);
622 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
623 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
626 if (op
== nir_intrinsic_load_push_constant
) {
627 assert(access_offset
% 4 == 0);
629 nir_intrinsic_set_base(instr
, access_offset
);
630 nir_intrinsic_set_range(instr
, access_size
);
634 instr
->src
[src
++] = nir_src_for_ssa(index
);
636 if (op
== nir_intrinsic_load_push_constant
) {
637 /* We need to subtract the offset from where the intrinsic will load the
640 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
641 nir_imm_int(&b
->nb
, access_offset
)));
643 instr
->src
[src
++] = nir_src_for_ssa(offset
);
647 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
648 instr
->num_components
,
649 glsl_get_bit_size(type
), NULL
);
650 (*inout
)->def
= &instr
->dest
.ssa
;
653 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
655 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
656 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
660 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
661 nir_ssa_def
*index
, nir_ssa_def
*offset
,
662 unsigned access_offset
, unsigned access_size
,
663 struct vtn_access_chain
*chain
, unsigned chain_idx
,
664 struct vtn_type
*type
, struct vtn_ssa_value
**inout
)
666 if (chain
&& chain_idx
>= chain
->length
)
669 if (load
&& chain
== NULL
&& *inout
== NULL
)
670 *inout
= vtn_create_ssa_value(b
, type
->type
);
672 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
676 case GLSL_TYPE_UINT64
:
677 case GLSL_TYPE_INT64
:
678 case GLSL_TYPE_FLOAT
:
679 case GLSL_TYPE_DOUBLE
:
681 /* This is where things get interesting. At this point, we've hit
682 * a vector, a scalar, or a matrix.
684 if (glsl_type_is_matrix(type
->type
)) {
685 /* Loading the whole matrix */
686 struct vtn_ssa_value
*transpose
;
687 unsigned num_ops
, vec_width
, col_stride
;
688 if (type
->row_major
) {
689 num_ops
= glsl_get_vector_elements(type
->type
);
690 vec_width
= glsl_get_matrix_columns(type
->type
);
691 col_stride
= type
->array_element
->stride
;
693 const struct glsl_type
*transpose_type
=
694 glsl_matrix_type(base_type
, vec_width
, num_ops
);
695 *inout
= vtn_create_ssa_value(b
, transpose_type
);
697 transpose
= vtn_ssa_transpose(b
, *inout
);
701 num_ops
= glsl_get_matrix_columns(type
->type
);
702 vec_width
= glsl_get_vector_elements(type
->type
);
703 col_stride
= type
->stride
;
706 for (unsigned i
= 0; i
< num_ops
; i
++) {
707 nir_ssa_def
*elem_offset
=
708 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* col_stride
));
709 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
710 access_offset
, access_size
,
712 glsl_vector_type(base_type
, vec_width
));
715 if (load
&& type
->row_major
)
716 *inout
= vtn_ssa_transpose(b
, *inout
);
718 unsigned elems
= glsl_get_vector_elements(type
->type
);
719 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
720 if (elems
== 1 || type
->stride
== type_size
) {
721 /* This is a tightly-packed normal scalar or vector load */
722 assert(glsl_type_is_vector_or_scalar(type
->type
));
723 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
724 access_offset
, access_size
,
727 /* This is a strided load. We have to load N things separately.
728 * This is the single column of a row-major matrix case.
730 assert(type
->stride
> type_size
);
731 assert(type
->stride
% type_size
== 0);
733 nir_ssa_def
*per_comp
[4];
734 for (unsigned i
= 0; i
< elems
; i
++) {
735 nir_ssa_def
*elem_offset
=
736 nir_iadd(&b
->nb
, offset
,
737 nir_imm_int(&b
->nb
, i
* type
->stride
));
738 struct vtn_ssa_value
*comp
, temp_val
;
740 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
741 temp_val
.type
= glsl_scalar_type(base_type
);
744 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
745 access_offset
, access_size
,
746 &comp
, glsl_scalar_type(base_type
));
747 per_comp
[i
] = comp
->def
;
752 *inout
= vtn_create_ssa_value(b
, type
->type
);
753 (*inout
)->def
= nir_vec(&b
->nb
, per_comp
, elems
);
759 case GLSL_TYPE_ARRAY
: {
760 unsigned elems
= glsl_get_length(type
->type
);
761 for (unsigned i
= 0; i
< elems
; i
++) {
762 nir_ssa_def
*elem_off
=
763 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* type
->stride
));
764 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
765 access_offset
, access_size
,
767 type
->array_element
, &(*inout
)->elems
[i
]);
772 case GLSL_TYPE_STRUCT
: {
773 unsigned elems
= glsl_get_length(type
->type
);
774 for (unsigned i
= 0; i
< elems
; i
++) {
775 nir_ssa_def
*elem_off
=
776 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, type
->offsets
[i
]));
777 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
778 access_offset
, access_size
,
780 type
->members
[i
], &(*inout
)->elems
[i
]);
786 unreachable("Invalid block member type");
790 static struct vtn_ssa_value
*
791 vtn_block_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
794 unsigned access_offset
= 0, access_size
= 0;
796 case vtn_variable_mode_ubo
:
797 op
= nir_intrinsic_load_ubo
;
799 case vtn_variable_mode_ssbo
:
800 op
= nir_intrinsic_load_ssbo
;
802 case vtn_variable_mode_push_constant
:
803 op
= nir_intrinsic_load_push_constant
;
804 vtn_access_chain_get_offset_size(src
->chain
, src
->var
->type
,
805 &access_offset
, &access_size
);
808 assert(!"Invalid block variable mode");
811 nir_ssa_def
*offset
, *index
= NULL
;
813 offset
= vtn_pointer_to_offset(b
, src
, &index
, &chain_idx
);
815 struct vtn_ssa_value
*value
= NULL
;
816 _vtn_block_load_store(b
, op
, true, index
, offset
,
817 access_offset
, access_size
,
818 src
->chain
, chain_idx
, src
->type
, &value
);
823 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
824 struct vtn_pointer
*dst
)
826 nir_ssa_def
*offset
, *index
= NULL
;
828 offset
= vtn_pointer_to_offset(b
, dst
, &index
, &chain_idx
);
830 _vtn_block_load_store(b
, nir_intrinsic_store_ssbo
, false, index
, offset
,
831 0, 0, dst
->chain
, chain_idx
, dst
->type
, &src
);
835 vtn_pointer_is_external_block(struct vtn_pointer
*ptr
)
837 return ptr
->mode
== vtn_variable_mode_ssbo
||
838 ptr
->mode
== vtn_variable_mode_ubo
||
839 ptr
->mode
== vtn_variable_mode_push_constant
;
843 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
844 struct vtn_pointer
*ptr
,
845 struct vtn_ssa_value
**inout
)
847 enum glsl_base_type base_type
= glsl_get_base_type(ptr
->type
->type
);
851 case GLSL_TYPE_UINT64
:
852 case GLSL_TYPE_INT64
:
853 case GLSL_TYPE_FLOAT
:
855 case GLSL_TYPE_DOUBLE
:
856 /* At this point, we have a scalar, vector, or matrix so we know that
857 * there cannot be any structure splitting still in the way. By
858 * stopping at the matrix level rather than the vector level, we
859 * ensure that matrices get loaded in the optimal way even if they
860 * are storred row-major in a UBO.
863 *inout
= vtn_local_load(b
, vtn_pointer_to_deref(b
, ptr
));
865 vtn_local_store(b
, *inout
, vtn_pointer_to_deref(b
, ptr
));
869 case GLSL_TYPE_ARRAY
:
870 case GLSL_TYPE_STRUCT
: {
871 unsigned elems
= glsl_get_length(ptr
->type
->type
);
873 assert(*inout
== NULL
);
874 *inout
= rzalloc(b
, struct vtn_ssa_value
);
875 (*inout
)->type
= ptr
->type
->type
;
876 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
879 struct vtn_access_chain chain
= {
882 { .mode
= vtn_access_mode_literal
, },
885 for (unsigned i
= 0; i
< elems
; i
++) {
886 chain
.link
[0].id
= i
;
887 struct vtn_pointer
*elem
= vtn_pointer_dereference(b
, ptr
, &chain
);
888 _vtn_variable_load_store(b
, load
, elem
, &(*inout
)->elems
[i
]);
894 unreachable("Invalid access chain type");
898 struct vtn_ssa_value
*
899 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
901 if (vtn_pointer_is_external_block(src
)) {
902 return vtn_block_load(b
, src
);
904 struct vtn_ssa_value
*val
= NULL
;
905 _vtn_variable_load_store(b
, true, src
, &val
);
911 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
912 struct vtn_pointer
*dest
)
914 if (vtn_pointer_is_external_block(dest
)) {
915 assert(dest
->mode
== vtn_variable_mode_ssbo
);
916 vtn_block_store(b
, src
, dest
);
918 _vtn_variable_load_store(b
, false, dest
, &src
);
923 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
924 struct vtn_pointer
*src
)
926 assert(src
->type
->type
== dest
->type
->type
);
927 enum glsl_base_type base_type
= glsl_get_base_type(src
->type
->type
);
931 case GLSL_TYPE_UINT64
:
932 case GLSL_TYPE_INT64
:
933 case GLSL_TYPE_FLOAT
:
934 case GLSL_TYPE_DOUBLE
:
936 /* At this point, we have a scalar, vector, or matrix so we know that
937 * there cannot be any structure splitting still in the way. By
938 * stopping at the matrix level rather than the vector level, we
939 * ensure that matrices get loaded in the optimal way even if they
940 * are storred row-major in a UBO.
942 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
945 case GLSL_TYPE_ARRAY
:
946 case GLSL_TYPE_STRUCT
: {
947 struct vtn_access_chain chain
= {
950 { .mode
= vtn_access_mode_literal
, },
953 unsigned elems
= glsl_get_length(src
->type
->type
);
954 for (unsigned i
= 0; i
< elems
; i
++) {
955 chain
.link
[0].id
= i
;
956 struct vtn_pointer
*src_elem
=
957 vtn_pointer_dereference(b
, src
, &chain
);
958 struct vtn_pointer
*dest_elem
=
959 vtn_pointer_dereference(b
, dest
, &chain
);
961 _vtn_variable_copy(b
, dest_elem
, src_elem
);
967 unreachable("Invalid access chain type");
972 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
973 struct vtn_pointer
*src
)
975 /* TODO: At some point, we should add a special-case for when we can
976 * just emit a copy_var intrinsic.
978 _vtn_variable_copy(b
, dest
, src
);
982 set_mode_system_value(nir_variable_mode
*mode
)
984 assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
985 *mode
= nir_var_system_value
;
989 vtn_get_builtin_location(struct vtn_builder
*b
,
990 SpvBuiltIn builtin
, int *location
,
991 nir_variable_mode
*mode
)
994 case SpvBuiltInPosition
:
995 *location
= VARYING_SLOT_POS
;
997 case SpvBuiltInPointSize
:
998 *location
= VARYING_SLOT_PSIZ
;
1000 case SpvBuiltInClipDistance
:
1001 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
1003 case SpvBuiltInCullDistance
:
1004 *location
= VARYING_SLOT_CULL_DIST0
;
1006 case SpvBuiltInVertexIndex
:
1007 *location
= SYSTEM_VALUE_VERTEX_ID
;
1008 set_mode_system_value(mode
);
1010 case SpvBuiltInVertexId
:
1011 /* Vulkan defines VertexID to be zero-based and reserves the new
1012 * builtin keyword VertexIndex to indicate the non-zero-based value.
1014 *location
= SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1015 set_mode_system_value(mode
);
1017 case SpvBuiltInInstanceIndex
:
1018 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
1019 set_mode_system_value(mode
);
1021 case SpvBuiltInInstanceId
:
1022 *location
= SYSTEM_VALUE_INSTANCE_ID
;
1023 set_mode_system_value(mode
);
1025 case SpvBuiltInPrimitiveId
:
1026 if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
) {
1027 assert(*mode
== nir_var_shader_in
);
1028 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1029 } else if (*mode
== nir_var_shader_out
) {
1030 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1032 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
1033 set_mode_system_value(mode
);
1036 case SpvBuiltInInvocationId
:
1037 *location
= SYSTEM_VALUE_INVOCATION_ID
;
1038 set_mode_system_value(mode
);
1040 case SpvBuiltInLayer
:
1041 *location
= VARYING_SLOT_LAYER
;
1042 if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
)
1043 *mode
= nir_var_shader_in
;
1044 else if (b
->shader
->stage
== MESA_SHADER_GEOMETRY
)
1045 *mode
= nir_var_shader_out
;
1047 unreachable("invalid stage for SpvBuiltInLayer");
1049 case SpvBuiltInViewportIndex
:
1050 *location
= VARYING_SLOT_VIEWPORT
;
1051 if (b
->shader
->stage
== MESA_SHADER_GEOMETRY
)
1052 *mode
= nir_var_shader_out
;
1053 else if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
)
1054 *mode
= nir_var_shader_in
;
1056 unreachable("invalid stage for SpvBuiltInViewportIndex");
1058 case SpvBuiltInTessLevelOuter
:
1059 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
1061 case SpvBuiltInTessLevelInner
:
1062 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
1064 case SpvBuiltInTessCoord
:
1065 *location
= SYSTEM_VALUE_TESS_COORD
;
1066 set_mode_system_value(mode
);
1068 case SpvBuiltInPatchVertices
:
1069 *location
= SYSTEM_VALUE_VERTICES_IN
;
1070 set_mode_system_value(mode
);
1072 case SpvBuiltInFragCoord
:
1073 *location
= VARYING_SLOT_POS
;
1074 assert(*mode
== nir_var_shader_in
);
1076 case SpvBuiltInPointCoord
:
1077 *location
= VARYING_SLOT_PNTC
;
1078 assert(*mode
== nir_var_shader_in
);
1080 case SpvBuiltInFrontFacing
:
1081 *location
= SYSTEM_VALUE_FRONT_FACE
;
1082 set_mode_system_value(mode
);
1084 case SpvBuiltInSampleId
:
1085 *location
= SYSTEM_VALUE_SAMPLE_ID
;
1086 set_mode_system_value(mode
);
1088 case SpvBuiltInSamplePosition
:
1089 *location
= SYSTEM_VALUE_SAMPLE_POS
;
1090 set_mode_system_value(mode
);
1092 case SpvBuiltInSampleMask
:
1093 if (*mode
== nir_var_shader_out
) {
1094 *location
= FRAG_RESULT_SAMPLE_MASK
;
1096 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1097 set_mode_system_value(mode
);
1100 case SpvBuiltInFragDepth
:
1101 *location
= FRAG_RESULT_DEPTH
;
1102 assert(*mode
== nir_var_shader_out
);
1104 case SpvBuiltInNumWorkgroups
:
1105 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1106 set_mode_system_value(mode
);
1108 case SpvBuiltInWorkgroupSize
:
1109 /* This should already be handled */
1110 unreachable("unsupported builtin");
1112 case SpvBuiltInWorkgroupId
:
1113 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1114 set_mode_system_value(mode
);
1116 case SpvBuiltInLocalInvocationId
:
1117 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1118 set_mode_system_value(mode
);
1120 case SpvBuiltInLocalInvocationIndex
:
1121 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1122 set_mode_system_value(mode
);
1124 case SpvBuiltInGlobalInvocationId
:
1125 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1126 set_mode_system_value(mode
);
1128 case SpvBuiltInBaseVertex
:
1129 *location
= SYSTEM_VALUE_BASE_VERTEX
;
1130 set_mode_system_value(mode
);
1132 case SpvBuiltInBaseInstance
:
1133 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1134 set_mode_system_value(mode
);
1136 case SpvBuiltInDrawIndex
:
1137 *location
= SYSTEM_VALUE_DRAW_ID
;
1138 set_mode_system_value(mode
);
1140 case SpvBuiltInViewIndex
:
1141 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1142 set_mode_system_value(mode
);
1144 case SpvBuiltInHelperInvocation
:
1146 unreachable("unsupported builtin");
1151 apply_var_decoration(struct vtn_builder
*b
, nir_variable
*nir_var
,
1152 const struct vtn_decoration
*dec
)
1154 switch (dec
->decoration
) {
1155 case SpvDecorationRelaxedPrecision
:
1156 break; /* FIXME: Do nothing with this for now. */
1157 case SpvDecorationNoPerspective
:
1158 nir_var
->data
.interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1160 case SpvDecorationFlat
:
1161 nir_var
->data
.interpolation
= INTERP_MODE_FLAT
;
1163 case SpvDecorationCentroid
:
1164 nir_var
->data
.centroid
= true;
1166 case SpvDecorationSample
:
1167 nir_var
->data
.sample
= true;
1169 case SpvDecorationInvariant
:
1170 nir_var
->data
.invariant
= true;
1172 case SpvDecorationConstant
:
1173 assert(nir_var
->constant_initializer
!= NULL
);
1174 nir_var
->data
.read_only
= true;
1176 case SpvDecorationNonReadable
:
1177 nir_var
->data
.image
.write_only
= true;
1179 case SpvDecorationNonWritable
:
1180 nir_var
->data
.read_only
= true;
1181 nir_var
->data
.image
.read_only
= true;
1183 case SpvDecorationComponent
:
1184 nir_var
->data
.location_frac
= dec
->literals
[0];
1186 case SpvDecorationIndex
:
1187 nir_var
->data
.index
= dec
->literals
[0];
1189 case SpvDecorationBuiltIn
: {
1190 SpvBuiltIn builtin
= dec
->literals
[0];
1192 if (builtin
== SpvBuiltInWorkgroupSize
) {
1193 /* This shouldn't be a builtin. It's actually a constant. */
1194 nir_var
->data
.mode
= nir_var_global
;
1195 nir_var
->data
.read_only
= true;
1197 nir_constant
*c
= rzalloc(nir_var
, nir_constant
);
1198 c
->values
[0].u32
[0] = b
->shader
->info
.cs
.local_size
[0];
1199 c
->values
[0].u32
[1] = b
->shader
->info
.cs
.local_size
[1];
1200 c
->values
[0].u32
[2] = b
->shader
->info
.cs
.local_size
[2];
1201 nir_var
->constant_initializer
= c
;
1205 nir_variable_mode mode
= nir_var
->data
.mode
;
1206 vtn_get_builtin_location(b
, builtin
, &nir_var
->data
.location
, &mode
);
1207 nir_var
->data
.mode
= mode
;
1210 case SpvBuiltInTessLevelOuter
:
1211 case SpvBuiltInTessLevelInner
:
1212 nir_var
->data
.compact
= true;
1214 case SpvBuiltInSamplePosition
:
1215 nir_var
->data
.origin_upper_left
= b
->origin_upper_left
;
1217 case SpvBuiltInFragCoord
:
1218 nir_var
->data
.pixel_center_integer
= b
->pixel_center_integer
;
1225 case SpvDecorationSpecId
:
1226 case SpvDecorationRowMajor
:
1227 case SpvDecorationColMajor
:
1228 case SpvDecorationMatrixStride
:
1229 case SpvDecorationRestrict
:
1230 case SpvDecorationAliased
:
1231 case SpvDecorationVolatile
:
1232 case SpvDecorationCoherent
:
1233 case SpvDecorationUniform
:
1234 case SpvDecorationStream
:
1235 case SpvDecorationOffset
:
1236 case SpvDecorationLinkageAttributes
:
1237 break; /* Do nothing with these here */
1239 case SpvDecorationPatch
:
1240 nir_var
->data
.patch
= true;
1243 case SpvDecorationLocation
:
1244 unreachable("Handled above");
1246 case SpvDecorationBlock
:
1247 case SpvDecorationBufferBlock
:
1248 case SpvDecorationArrayStride
:
1249 case SpvDecorationGLSLShared
:
1250 case SpvDecorationGLSLPacked
:
1251 break; /* These can apply to a type but we don't care about them */
1253 case SpvDecorationBinding
:
1254 case SpvDecorationDescriptorSet
:
1255 case SpvDecorationNoContraction
:
1256 case SpvDecorationInputAttachmentIndex
:
1257 vtn_warn("Decoration not allowed for variable or structure member: %s",
1258 spirv_decoration_to_string(dec
->decoration
));
1261 case SpvDecorationXfbBuffer
:
1262 case SpvDecorationXfbStride
:
1263 vtn_warn("Vulkan does not have transform feedback: %s",
1264 spirv_decoration_to_string(dec
->decoration
));
1267 case SpvDecorationCPacked
:
1268 case SpvDecorationSaturatedConversion
:
1269 case SpvDecorationFuncParamAttr
:
1270 case SpvDecorationFPRoundingMode
:
1271 case SpvDecorationFPFastMathMode
:
1272 case SpvDecorationAlignment
:
1273 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1274 spirv_decoration_to_string(dec
->decoration
));
1278 unreachable("Unhandled decoration");
1283 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1284 const struct vtn_decoration
*dec
, void *out_is_patch
)
1286 if (dec
->decoration
== SpvDecorationPatch
) {
1287 *((bool *) out_is_patch
) = true;
1292 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1293 const struct vtn_decoration
*dec
, void *void_var
)
1295 struct vtn_variable
*vtn_var
= void_var
;
1297 /* Handle decorations that apply to a vtn_variable as a whole */
1298 switch (dec
->decoration
) {
1299 case SpvDecorationBinding
:
1300 vtn_var
->binding
= dec
->literals
[0];
1302 case SpvDecorationDescriptorSet
:
1303 vtn_var
->descriptor_set
= dec
->literals
[0];
1305 case SpvDecorationInputAttachmentIndex
:
1306 vtn_var
->input_attachment_index
= dec
->literals
[0];
1308 case SpvDecorationPatch
:
1309 vtn_var
->patch
= true;
1315 if (val
->value_type
== vtn_value_type_pointer
) {
1316 assert(val
->pointer
->var
== void_var
);
1317 assert(val
->pointer
->chain
== NULL
);
1318 assert(member
== -1);
1320 assert(val
->value_type
== vtn_value_type_type
);
1323 /* Location is odd. If applied to a split structure, we have to walk the
1324 * whole thing and accumulate the location. It's easier to handle as a
1327 if (dec
->decoration
== SpvDecorationLocation
) {
1328 unsigned location
= dec
->literals
[0];
1329 bool is_vertex_input
;
1330 if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
&&
1331 vtn_var
->mode
== vtn_variable_mode_output
) {
1332 is_vertex_input
= false;
1333 location
+= FRAG_RESULT_DATA0
;
1334 } else if (b
->shader
->stage
== MESA_SHADER_VERTEX
&&
1335 vtn_var
->mode
== vtn_variable_mode_input
) {
1336 is_vertex_input
= true;
1337 location
+= VERT_ATTRIB_GENERIC0
;
1338 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1339 vtn_var
->mode
== vtn_variable_mode_output
) {
1340 is_vertex_input
= false;
1341 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1343 vtn_warn("Location must be on input or output variable");
1348 /* This handles the member and lone variable cases */
1349 vtn_var
->var
->data
.location
= location
;
1351 /* This handles the structure member case */
1352 assert(vtn_var
->members
);
1354 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1355 for (unsigned i
= 0; i
< length
; i
++) {
1356 vtn_var
->members
[i
]->data
.location
= location
;
1358 glsl_count_attribute_slots(vtn_var
->members
[i
]->interface_type
,
1365 assert(member
<= 0);
1366 apply_var_decoration(b
, vtn_var
->var
, dec
);
1367 } else if (vtn_var
->members
) {
1369 assert(vtn_var
->members
);
1370 apply_var_decoration(b
, vtn_var
->members
[member
], dec
);
1373 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1374 for (unsigned i
= 0; i
< length
; i
++)
1375 apply_var_decoration(b
, vtn_var
->members
[i
], dec
);
1378 /* A few variables, those with external storage, have no actual
1379 * nir_variables associated with them. Fortunately, all decorations
1380 * we care about for those variables are on the type only.
1382 assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1383 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1384 vtn_var
->mode
== vtn_variable_mode_push_constant
);
1389 static enum vtn_variable_mode
1390 vtn_storage_class_to_mode(SpvStorageClass
class,
1391 struct vtn_type
*interface_type
,
1392 nir_variable_mode
*nir_mode_out
)
1394 enum vtn_variable_mode mode
;
1395 nir_variable_mode nir_mode
;
1397 case SpvStorageClassUniform
:
1398 if (interface_type
->block
) {
1399 mode
= vtn_variable_mode_ubo
;
1401 } else if (interface_type
->buffer_block
) {
1402 mode
= vtn_variable_mode_ssbo
;
1405 assert(!"Invalid uniform variable type");
1408 case SpvStorageClassStorageBuffer
:
1409 mode
= vtn_variable_mode_ssbo
;
1412 case SpvStorageClassUniformConstant
:
1413 if (glsl_type_is_image(interface_type
->type
)) {
1414 mode
= vtn_variable_mode_image
;
1415 nir_mode
= nir_var_uniform
;
1416 } else if (glsl_type_is_sampler(interface_type
->type
)) {
1417 mode
= vtn_variable_mode_sampler
;
1418 nir_mode
= nir_var_uniform
;
1420 assert(!"Invalid uniform constant variable type");
1423 case SpvStorageClassPushConstant
:
1424 mode
= vtn_variable_mode_push_constant
;
1425 nir_mode
= nir_var_uniform
;
1427 case SpvStorageClassInput
:
1428 mode
= vtn_variable_mode_input
;
1429 nir_mode
= nir_var_shader_in
;
1431 case SpvStorageClassOutput
:
1432 mode
= vtn_variable_mode_output
;
1433 nir_mode
= nir_var_shader_out
;
1435 case SpvStorageClassPrivate
:
1436 mode
= vtn_variable_mode_global
;
1437 nir_mode
= nir_var_global
;
1439 case SpvStorageClassFunction
:
1440 mode
= vtn_variable_mode_local
;
1441 nir_mode
= nir_var_local
;
1443 case SpvStorageClassWorkgroup
:
1444 mode
= vtn_variable_mode_workgroup
;
1445 nir_mode
= nir_var_shared
;
1447 case SpvStorageClassCrossWorkgroup
:
1448 case SpvStorageClassGeneric
:
1449 case SpvStorageClassAtomicCounter
:
1451 unreachable("Unhandled variable storage class");
1455 *nir_mode_out
= nir_mode
;
1461 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
1463 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
1466 if (var
->mode
== vtn_variable_mode_input
) {
1467 return stage
== MESA_SHADER_TESS_CTRL
||
1468 stage
== MESA_SHADER_TESS_EVAL
||
1469 stage
== MESA_SHADER_GEOMETRY
;
1472 if (var
->mode
== vtn_variable_mode_output
)
1473 return stage
== MESA_SHADER_TESS_CTRL
;
1479 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
1480 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
1481 nir_constant
*initializer
)
1483 assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1484 struct vtn_type
*type
= ptr_type
->deref
;
1485 assert(type
->base_type
!= vtn_base_type_pointer
);
1487 struct vtn_type
*without_array
= type
;
1488 while(glsl_type_is_array(without_array
->type
))
1489 without_array
= without_array
->array_element
;
1491 enum vtn_variable_mode mode
;
1492 nir_variable_mode nir_mode
;
1493 mode
= vtn_storage_class_to_mode(storage_class
, without_array
, &nir_mode
);
1496 case vtn_variable_mode_ubo
:
1497 b
->shader
->info
.num_ubos
++;
1499 case vtn_variable_mode_ssbo
:
1500 b
->shader
->info
.num_ssbos
++;
1502 case vtn_variable_mode_image
:
1503 b
->shader
->info
.num_images
++;
1505 case vtn_variable_mode_sampler
:
1506 b
->shader
->info
.num_textures
++;
1508 case vtn_variable_mode_push_constant
:
1509 b
->shader
->num_uniforms
= vtn_type_block_size(type
);
1512 /* No tallying is needed */
1516 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
1520 assert(val
->value_type
== vtn_value_type_pointer
);
1521 val
->pointer
= vtn_pointer_for_variable(b
, var
, ptr_type
);
1523 switch (var
->mode
) {
1524 case vtn_variable_mode_local
:
1525 case vtn_variable_mode_global
:
1526 case vtn_variable_mode_image
:
1527 case vtn_variable_mode_sampler
:
1528 case vtn_variable_mode_workgroup
:
1529 /* For these, we create the variable normally */
1530 var
->var
= rzalloc(b
->shader
, nir_variable
);
1531 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1532 var
->var
->type
= var
->type
->type
;
1533 var
->var
->data
.mode
= nir_mode
;
1535 switch (var
->mode
) {
1536 case vtn_variable_mode_image
:
1537 case vtn_variable_mode_sampler
:
1538 var
->var
->interface_type
= without_array
->type
;
1541 var
->var
->interface_type
= NULL
;
1546 case vtn_variable_mode_input
:
1547 case vtn_variable_mode_output
: {
1548 /* In order to know whether or not we're a per-vertex inout, we need
1549 * the patch qualifier. This means walking the variable decorations
1550 * early before we actually create any variables. Not a big deal.
1552 * GLSLang really likes to place decorations in the most interior
1553 * thing it possibly can. In particular, if you have a struct, it
1554 * will place the patch decorations on the struct members. This
1555 * should be handled by the variable splitting below just fine.
1557 * If you have an array-of-struct, things get even more weird as it
1558 * will place the patch decorations on the struct even though it's
1559 * inside an array and some of the members being patch and others not
1560 * makes no sense whatsoever. Since the only sensible thing is for
1561 * it to be all or nothing, we'll call it patch if any of the members
1562 * are declared patch.
1565 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
1566 if (glsl_type_is_array(var
->type
->type
) &&
1567 glsl_type_is_struct(without_array
->type
)) {
1568 vtn_foreach_decoration(b
, without_array
->val
,
1569 var_is_patch_cb
, &var
->patch
);
1572 /* For inputs and outputs, we immediately split structures. This
1573 * is for a couple of reasons. For one, builtins may all come in
1574 * a struct and we really want those split out into separate
1575 * variables. For another, interpolation qualifiers can be
1576 * applied to members of the top-level struct ane we need to be
1577 * able to preserve that information.
1580 int array_length
= -1;
1581 struct vtn_type
*interface_type
= var
->type
;
1582 if (is_per_vertex_inout(var
, b
->shader
->stage
)) {
1583 /* In Geometry shaders (and some tessellation), inputs come
1584 * in per-vertex arrays. However, some builtins come in
1585 * non-per-vertex, hence the need for the is_array check. In
1586 * any case, there are no non-builtin arrays allowed so this
1587 * check should be sufficient.
1589 interface_type
= var
->type
->array_element
;
1590 array_length
= glsl_get_length(var
->type
->type
);
1593 if (glsl_type_is_struct(interface_type
->type
)) {
1594 /* It's a struct. Split it. */
1595 unsigned num_members
= glsl_get_length(interface_type
->type
);
1596 var
->members
= ralloc_array(b
, nir_variable
*, num_members
);
1598 for (unsigned i
= 0; i
< num_members
; i
++) {
1599 const struct glsl_type
*mtype
= interface_type
->members
[i
]->type
;
1600 if (array_length
>= 0)
1601 mtype
= glsl_array_type(mtype
, array_length
);
1603 var
->members
[i
] = rzalloc(b
->shader
, nir_variable
);
1604 var
->members
[i
]->name
=
1605 ralloc_asprintf(var
->members
[i
], "%s.%d", val
->name
, i
);
1606 var
->members
[i
]->type
= mtype
;
1607 var
->members
[i
]->interface_type
=
1608 interface_type
->members
[i
]->type
;
1609 var
->members
[i
]->data
.mode
= nir_mode
;
1610 var
->members
[i
]->data
.patch
= var
->patch
;
1613 var
->var
= rzalloc(b
->shader
, nir_variable
);
1614 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1615 var
->var
->type
= var
->type
->type
;
1616 var
->var
->interface_type
= interface_type
->type
;
1617 var
->var
->data
.mode
= nir_mode
;
1618 var
->var
->data
.patch
= var
->patch
;
1621 /* For inputs and outputs, we need to grab locations and builtin
1622 * information from the interface type.
1624 vtn_foreach_decoration(b
, interface_type
->val
, var_decoration_cb
, var
);
1628 case vtn_variable_mode_param
:
1629 unreachable("Not created through OpVariable");
1631 case vtn_variable_mode_ubo
:
1632 case vtn_variable_mode_ssbo
:
1633 case vtn_variable_mode_push_constant
:
1634 /* These don't need actual variables. */
1639 var
->var
->constant_initializer
=
1640 nir_constant_clone(initializer
, var
->var
);
1643 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
1645 if (var
->mode
== vtn_variable_mode_image
||
1646 var
->mode
== vtn_variable_mode_sampler
) {
1647 /* XXX: We still need the binding information in the nir_variable
1648 * for these. We should fix that.
1650 var
->var
->data
.binding
= var
->binding
;
1651 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
1652 var
->var
->data
.index
= var
->input_attachment_index
;
1654 if (var
->mode
== vtn_variable_mode_image
)
1655 var
->var
->data
.image
.format
= without_array
->image_format
;
1658 if (var
->mode
== vtn_variable_mode_local
) {
1659 assert(var
->members
== NULL
&& var
->var
!= NULL
);
1660 nir_function_impl_add_variable(b
->impl
, var
->var
);
1661 } else if (var
->var
) {
1662 nir_shader_add_variable(b
->shader
, var
->var
);
1663 } else if (var
->members
) {
1664 unsigned count
= glsl_get_length(without_array
->type
);
1665 for (unsigned i
= 0; i
< count
; i
++) {
1666 assert(var
->members
[i
]->data
.mode
!= nir_var_local
);
1667 nir_shader_add_variable(b
->shader
, var
->members
[i
]);
1670 assert(var
->mode
== vtn_variable_mode_ubo
||
1671 var
->mode
== vtn_variable_mode_ssbo
||
1672 var
->mode
== vtn_variable_mode_push_constant
);
1677 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
1678 const uint32_t *w
, unsigned count
)
1682 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1683 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1687 case SpvOpVariable
: {
1688 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1690 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1692 SpvStorageClass storage_class
= w
[3];
1693 nir_constant
*initializer
= NULL
;
1695 initializer
= vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
1697 vtn_create_variable(b
, val
, ptr_type
, storage_class
, initializer
);
1701 case SpvOpAccessChain
:
1702 case SpvOpInBoundsAccessChain
: {
1703 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
1706 for (int i
= 4; i
< count
; i
++) {
1707 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
1708 if (link_val
->value_type
== vtn_value_type_constant
) {
1709 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
1710 chain
->link
[idx
].id
= link_val
->constant
->values
[0].u32
[0];
1712 chain
->link
[idx
].mode
= vtn_access_mode_id
;
1713 chain
->link
[idx
].id
= w
[i
];
1719 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1720 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
1721 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1722 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1723 * to combine an array of images with a single sampler to get an
1724 * array of sampled images that all share the same sampler.
1725 * Fortunately, this means that we can more-or-less ignore the
1726 * sampler when crawling the access chain, but it does leave us
1727 * with this rather awkward little special-case.
1729 struct vtn_value
*val
=
1730 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1731 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1732 val
->sampled_image
->image
=
1733 vtn_pointer_dereference(b
, base_val
->sampled_image
->image
, chain
);
1734 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
1736 assert(base_val
->value_type
== vtn_value_type_pointer
);
1737 struct vtn_value
*val
=
1738 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1739 val
->pointer
= vtn_pointer_dereference(b
, base_val
->pointer
, chain
);
1740 val
->pointer
->ptr_type
= ptr_type
;
1745 case SpvOpCopyMemory
: {
1746 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
1747 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
1749 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
1754 struct vtn_pointer
*src
=
1755 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1757 if (src
->mode
== vtn_variable_mode_image
||
1758 src
->mode
== vtn_variable_mode_sampler
) {
1759 vtn_push_value(b
, w
[2], vtn_value_type_pointer
)->pointer
= src
;
1763 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1764 val
->ssa
= vtn_variable_load(b
, src
);
1769 struct vtn_pointer
*dest
=
1770 vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
1772 if (glsl_type_is_sampler(dest
->type
->type
)) {
1773 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
1774 "propagation to workaround the problem.");
1775 assert(dest
->var
->copy_prop_sampler
== NULL
);
1776 dest
->var
->copy_prop_sampler
=
1777 vtn_value(b
, w
[2], vtn_value_type_pointer
)->pointer
;
1781 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
1782 vtn_variable_store(b
, src
, dest
);
1786 case SpvOpArrayLength
: {
1787 struct vtn_pointer
*ptr
=
1788 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1790 const uint32_t offset
= ptr
->var
->type
->offsets
[w
[4]];
1791 const uint32_t stride
= ptr
->var
->type
->members
[w
[4]]->stride
;
1794 struct vtn_type
*type
;
1795 nir_ssa_def
*index
=
1796 get_vulkan_resource_index(b
, ptr
, &type
, &chain_idx
);
1798 nir_intrinsic_instr
*instr
=
1799 nir_intrinsic_instr_create(b
->nb
.shader
,
1800 nir_intrinsic_get_buffer_size
);
1801 instr
->src
[0] = nir_src_for_ssa(index
);
1802 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
1803 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
1804 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
1806 /* array_length = max(buffer_size - offset, 0) / stride */
1807 nir_ssa_def
*array_length
=
1812 nir_imm_int(&b
->nb
, offset
)),
1813 nir_imm_int(&b
->nb
, 0u)),
1814 nir_imm_int(&b
->nb
, stride
));
1816 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1817 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
1818 val
->ssa
->def
= array_length
;
1822 case SpvOpCopyMemorySized
:
1824 unreachable("Unhandled opcode");