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_extend(struct vtn_builder
*b
, struct vtn_access_chain
*old
,
35 struct vtn_access_chain
*chain
;
37 unsigned old_len
= old
? old
->length
: 0;
38 unsigned new_len
= old_len
+ new_ids
;
39 /* TODO: don't use rzalloc */
40 chain
= rzalloc_size(b
, sizeof(*chain
) + new_len
* sizeof(chain
->link
[0]));
42 chain
->length
= new_len
;
44 for (unsigned i
= 0; i
< old_len
; i
++)
45 chain
->link
[i
] = old
->link
[i
];
51 vtn_access_link_as_ssa(struct vtn_builder
*b
, struct vtn_access_link link
,
55 if (link
.mode
== vtn_access_mode_literal
) {
56 return nir_imm_int(&b
->nb
, link
.id
* stride
);
57 } else if (stride
== 1) {
58 return vtn_ssa_value(b
, link
.id
)->def
;
60 return nir_imul(&b
->nb
, vtn_ssa_value(b
, link
.id
)->def
,
61 nir_imm_int(&b
->nb
, stride
));
65 /* Crawls a chain of array derefs and rewrites the types so that the
66 * lengths stay the same but the terminal type is the one given by
67 * tail_type. This is useful for split structures.
70 rewrite_deref_types(nir_deref
*deref
, const struct glsl_type
*type
)
74 assert(deref
->child
->deref_type
== nir_deref_type_array
);
75 assert(glsl_type_is_array(deref
->type
));
76 rewrite_deref_types(deref
->child
, glsl_get_array_element(type
));
81 vtn_pointer_for_variable(struct vtn_builder
*b
,
82 struct vtn_variable
*var
)
84 struct vtn_pointer
*pointer
= rzalloc(b
, struct vtn_pointer
);
86 pointer
->mode
= var
->mode
;
87 pointer
->type
= var
->type
;
94 vtn_pointer_to_deref(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
96 /* Do on-the-fly copy propagation for samplers. */
97 if (ptr
->var
->copy_prop_sampler
)
98 return vtn_pointer_to_deref(b
, ptr
->var
->copy_prop_sampler
);
100 nir_deref_var
*deref_var
;
102 deref_var
= nir_deref_var_create(b
, ptr
->var
->var
);
103 /* Raw variable access */
107 assert(ptr
->var
->members
);
108 /* Create the deref_var manually. It will get filled out later. */
109 deref_var
= rzalloc(b
, nir_deref_var
);
110 deref_var
->deref
.deref_type
= nir_deref_type_var
;
113 struct vtn_access_chain
*chain
= ptr
->chain
;
116 struct vtn_type
*deref_type
= ptr
->var
->type
;
117 nir_deref
*tail
= &deref_var
->deref
;
118 nir_variable
**members
= ptr
->var
->members
;
120 for (unsigned i
= 0; i
< chain
->length
; i
++) {
121 enum glsl_base_type base_type
= glsl_get_base_type(deref_type
->type
);
125 case GLSL_TYPE_UINT64
:
126 case GLSL_TYPE_INT64
:
127 case GLSL_TYPE_FLOAT
:
128 case GLSL_TYPE_DOUBLE
:
130 case GLSL_TYPE_ARRAY
: {
131 deref_type
= deref_type
->array_element
;
133 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
134 deref_arr
->deref
.type
= deref_type
->type
;
136 if (chain
->link
[i
].mode
== vtn_access_mode_literal
) {
137 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
138 deref_arr
->base_offset
= chain
->link
[i
].id
;
140 assert(chain
->link
[i
].mode
== vtn_access_mode_id
);
141 deref_arr
->deref_array_type
= nir_deref_array_type_indirect
;
142 deref_arr
->base_offset
= 0;
143 deref_arr
->indirect
=
144 nir_src_for_ssa(vtn_ssa_value(b
, chain
->link
[i
].id
)->def
);
146 tail
->child
= &deref_arr
->deref
;
151 case GLSL_TYPE_STRUCT
: {
152 assert(chain
->link
[i
].mode
== vtn_access_mode_literal
);
153 unsigned idx
= chain
->link
[i
].id
;
154 deref_type
= deref_type
->members
[idx
];
156 /* This is a pre-split structure. */
157 deref_var
->var
= members
[idx
];
158 rewrite_deref_types(&deref_var
->deref
, members
[idx
]->type
);
159 assert(tail
->type
== deref_type
->type
);
162 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, idx
);
163 deref_struct
->deref
.type
= deref_type
->type
;
164 tail
->child
= &deref_struct
->deref
;
170 unreachable("Invalid type for deref");
174 assert(members
== NULL
);
179 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_var
*deref
,
180 nir_deref
*tail
, struct vtn_ssa_value
*inout
)
182 /* The deref tail may contain a deref to select a component of a vector (in
183 * other words, it might not be an actual tail) so we have to save it away
184 * here since we overwrite it later.
186 nir_deref
*old_child
= tail
->child
;
188 if (glsl_type_is_vector_or_scalar(tail
->type
)) {
189 /* Terminate the deref chain in case there is one more link to pick
190 * off a component of the vector.
194 nir_intrinsic_op op
= load
? nir_intrinsic_load_var
:
195 nir_intrinsic_store_var
;
197 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
198 intrin
->variables
[0] = nir_deref_var_clone(deref
, intrin
);
199 intrin
->num_components
= glsl_get_vector_elements(tail
->type
);
202 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
203 intrin
->num_components
,
204 glsl_get_bit_size(tail
->type
),
206 inout
->def
= &intrin
->dest
.ssa
;
208 nir_intrinsic_set_write_mask(intrin
, (1 << intrin
->num_components
) - 1);
209 intrin
->src
[0] = nir_src_for_ssa(inout
->def
);
212 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
213 } else if (glsl_get_base_type(tail
->type
) == GLSL_TYPE_ARRAY
||
214 glsl_type_is_matrix(tail
->type
)) {
215 unsigned elems
= glsl_get_length(tail
->type
);
216 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
217 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
218 deref_arr
->deref
.type
= glsl_get_array_element(tail
->type
);
219 tail
->child
= &deref_arr
->deref
;
220 for (unsigned i
= 0; i
< elems
; i
++) {
221 deref_arr
->base_offset
= i
;
222 _vtn_local_load_store(b
, load
, deref
, tail
->child
, inout
->elems
[i
]);
225 assert(glsl_get_base_type(tail
->type
) == GLSL_TYPE_STRUCT
);
226 unsigned elems
= glsl_get_length(tail
->type
);
227 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, 0);
228 tail
->child
= &deref_struct
->deref
;
229 for (unsigned i
= 0; i
< elems
; i
++) {
230 deref_struct
->index
= i
;
231 deref_struct
->deref
.type
= glsl_get_struct_field(tail
->type
, i
);
232 _vtn_local_load_store(b
, load
, deref
, tail
->child
, inout
->elems
[i
]);
236 tail
->child
= old_child
;
240 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
242 struct vtn_pointer
*ptr
= vtn_value(b
, id
, vtn_value_type_pointer
)->pointer
;
243 return vtn_pointer_to_deref(b
, ptr
);
247 * Gets the NIR-level deref tail, which may have as a child an array deref
248 * selecting which component due to OpAccessChain supporting per-component
249 * indexing in SPIR-V.
252 get_deref_tail(nir_deref_var
*deref
)
254 nir_deref
*cur
= &deref
->deref
;
255 while (!glsl_type_is_vector_or_scalar(cur
->type
) && cur
->child
)
261 struct vtn_ssa_value
*
262 vtn_local_load(struct vtn_builder
*b
, nir_deref_var
*src
)
264 nir_deref
*src_tail
= get_deref_tail(src
);
265 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
266 _vtn_local_load_store(b
, true, src
, src_tail
, val
);
268 if (src_tail
->child
) {
269 nir_deref_array
*vec_deref
= nir_deref_as_array(src_tail
->child
);
270 assert(vec_deref
->deref
.child
== NULL
);
271 val
->type
= vec_deref
->deref
.type
;
272 if (vec_deref
->deref_array_type
== nir_deref_array_type_direct
)
273 val
->def
= vtn_vector_extract(b
, val
->def
, vec_deref
->base_offset
);
275 val
->def
= vtn_vector_extract_dynamic(b
, val
->def
,
276 vec_deref
->indirect
.ssa
);
283 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
286 nir_deref
*dest_tail
= get_deref_tail(dest
);
288 if (dest_tail
->child
) {
289 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
290 _vtn_local_load_store(b
, true, dest
, dest_tail
, val
);
291 nir_deref_array
*deref
= nir_deref_as_array(dest_tail
->child
);
292 assert(deref
->deref
.child
== NULL
);
293 if (deref
->deref_array_type
== nir_deref_array_type_direct
)
294 val
->def
= vtn_vector_insert(b
, val
->def
, src
->def
,
297 val
->def
= vtn_vector_insert_dynamic(b
, val
->def
, src
->def
,
298 deref
->indirect
.ssa
);
299 _vtn_local_load_store(b
, false, dest
, dest_tail
, val
);
301 _vtn_local_load_store(b
, false, dest
, dest_tail
, src
);
306 get_vulkan_resource_index(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
307 struct vtn_type
**type
, unsigned *chain_idx
)
309 /* Push constants have no explicit binding */
310 if (ptr
->mode
== vtn_variable_mode_push_constant
) {
312 *type
= ptr
->var
->type
;
316 nir_ssa_def
*array_index
;
317 if (glsl_type_is_array(ptr
->var
->type
->type
)) {
318 assert(ptr
->chain
->length
> 0);
319 array_index
= vtn_access_link_as_ssa(b
, ptr
->chain
->link
[0], 1);
321 *type
= ptr
->var
->type
->array_element
;
323 array_index
= nir_imm_int(&b
->nb
, 0);
325 *type
= ptr
->var
->type
;
328 nir_intrinsic_instr
*instr
=
329 nir_intrinsic_instr_create(b
->nb
.shader
,
330 nir_intrinsic_vulkan_resource_index
);
331 instr
->src
[0] = nir_src_for_ssa(array_index
);
332 nir_intrinsic_set_desc_set(instr
, ptr
->var
->descriptor_set
);
333 nir_intrinsic_set_binding(instr
, ptr
->var
->binding
);
335 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
336 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
338 return &instr
->dest
.ssa
;
342 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
343 nir_ssa_def
**index_out
, unsigned *end_idx_out
)
346 struct vtn_type
*type
;
347 *index_out
= get_vulkan_resource_index(b
, ptr
, &type
, &idx
);
349 nir_ssa_def
*offset
= nir_imm_int(&b
->nb
, 0);
350 for (; idx
< ptr
->chain
->length
; idx
++) {
351 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
355 case GLSL_TYPE_UINT64
:
356 case GLSL_TYPE_INT64
:
357 case GLSL_TYPE_FLOAT
:
358 case GLSL_TYPE_DOUBLE
:
360 case GLSL_TYPE_ARRAY
:
361 offset
= nir_iadd(&b
->nb
, offset
,
362 vtn_access_link_as_ssa(b
, ptr
->chain
->link
[idx
],
365 type
= type
->array_element
;
368 case GLSL_TYPE_STRUCT
: {
369 assert(ptr
->chain
->link
[idx
].mode
== vtn_access_mode_literal
);
370 unsigned member
= ptr
->chain
->link
[idx
].id
;
371 offset
= nir_iadd(&b
->nb
, offset
,
372 nir_imm_int(&b
->nb
, type
->offsets
[member
]));
373 type
= type
->members
[member
];
378 unreachable("Invalid type for deref");
382 assert(type
== ptr
->type
);
389 /* Tries to compute the size of an interface block based on the strides and
390 * offsets that are provided to us in the SPIR-V source.
393 vtn_type_block_size(struct vtn_type
*type
)
395 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
399 case GLSL_TYPE_UINT64
:
400 case GLSL_TYPE_INT64
:
401 case GLSL_TYPE_FLOAT
:
403 case GLSL_TYPE_DOUBLE
: {
404 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
405 glsl_get_matrix_columns(type
->type
);
407 assert(type
->stride
> 0);
408 return type
->stride
* cols
;
409 } else if (base_type
== GLSL_TYPE_DOUBLE
||
410 base_type
== GLSL_TYPE_UINT64
||
411 base_type
== GLSL_TYPE_INT64
) {
412 return glsl_get_vector_elements(type
->type
) * 8;
414 return glsl_get_vector_elements(type
->type
) * 4;
418 case GLSL_TYPE_STRUCT
:
419 case GLSL_TYPE_INTERFACE
: {
421 unsigned num_fields
= glsl_get_length(type
->type
);
422 for (unsigned f
= 0; f
< num_fields
; f
++) {
423 unsigned field_end
= type
->offsets
[f
] +
424 vtn_type_block_size(type
->members
[f
]);
425 size
= MAX2(size
, field_end
);
430 case GLSL_TYPE_ARRAY
:
431 assert(type
->stride
> 0);
432 assert(glsl_get_length(type
->type
) > 0);
433 return type
->stride
* glsl_get_length(type
->type
);
436 assert(!"Invalid block type");
442 vtn_access_chain_get_offset_size(struct vtn_access_chain
*chain
,
443 struct vtn_type
*type
,
444 unsigned *access_offset
,
445 unsigned *access_size
)
449 for (unsigned i
= 0; i
< chain
->length
; i
++) {
450 if (chain
->link
[i
].mode
!= vtn_access_mode_literal
)
453 if (glsl_type_is_struct(type
->type
)) {
454 *access_offset
+= type
->offsets
[chain
->link
[i
].id
];
455 type
= type
->members
[chain
->link
[i
].id
];
457 *access_offset
+= type
->stride
* chain
->link
[i
].id
;
458 type
= type
->array_element
;
462 *access_size
= vtn_type_block_size(type
);
466 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
467 nir_ssa_def
*index
, nir_ssa_def
*offset
,
468 unsigned access_offset
, unsigned access_size
,
469 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
)
471 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
472 instr
->num_components
= glsl_get_vector_elements(type
);
476 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
477 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
480 if (op
== nir_intrinsic_load_push_constant
) {
481 assert(access_offset
% 4 == 0);
483 nir_intrinsic_set_base(instr
, access_offset
);
484 nir_intrinsic_set_range(instr
, access_size
);
488 instr
->src
[src
++] = nir_src_for_ssa(index
);
490 if (op
== nir_intrinsic_load_push_constant
) {
491 /* We need to subtract the offset from where the intrinsic will load the
494 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
495 nir_imm_int(&b
->nb
, access_offset
)));
497 instr
->src
[src
++] = nir_src_for_ssa(offset
);
501 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
502 instr
->num_components
,
503 glsl_get_bit_size(type
), NULL
);
504 (*inout
)->def
= &instr
->dest
.ssa
;
507 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
509 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
510 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
514 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
515 nir_ssa_def
*index
, nir_ssa_def
*offset
,
516 unsigned access_offset
, unsigned access_size
,
517 struct vtn_access_chain
*chain
, unsigned chain_idx
,
518 struct vtn_type
*type
, struct vtn_ssa_value
**inout
)
520 if (chain
&& chain_idx
>= chain
->length
)
523 if (load
&& chain
== NULL
&& *inout
== NULL
)
524 *inout
= vtn_create_ssa_value(b
, type
->type
);
526 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
530 case GLSL_TYPE_UINT64
:
531 case GLSL_TYPE_INT64
:
532 case GLSL_TYPE_FLOAT
:
533 case GLSL_TYPE_DOUBLE
:
535 /* This is where things get interesting. At this point, we've hit
536 * a vector, a scalar, or a matrix.
538 if (glsl_type_is_matrix(type
->type
)) {
539 /* Loading the whole matrix */
540 struct vtn_ssa_value
*transpose
;
541 unsigned num_ops
, vec_width
, col_stride
;
542 if (type
->row_major
) {
543 num_ops
= glsl_get_vector_elements(type
->type
);
544 vec_width
= glsl_get_matrix_columns(type
->type
);
545 col_stride
= type
->array_element
->stride
;
547 const struct glsl_type
*transpose_type
=
548 glsl_matrix_type(base_type
, vec_width
, num_ops
);
549 *inout
= vtn_create_ssa_value(b
, transpose_type
);
551 transpose
= vtn_ssa_transpose(b
, *inout
);
555 num_ops
= glsl_get_matrix_columns(type
->type
);
556 vec_width
= glsl_get_vector_elements(type
->type
);
557 col_stride
= type
->stride
;
560 for (unsigned i
= 0; i
< num_ops
; i
++) {
561 nir_ssa_def
*elem_offset
=
562 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* col_stride
));
563 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
564 access_offset
, access_size
,
566 glsl_vector_type(base_type
, vec_width
));
569 if (load
&& type
->row_major
)
570 *inout
= vtn_ssa_transpose(b
, *inout
);
572 unsigned elems
= glsl_get_vector_elements(type
->type
);
573 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
574 if (elems
== 1 || type
->stride
== type_size
) {
575 /* This is a tightly-packed normal scalar or vector load */
576 assert(glsl_type_is_vector_or_scalar(type
->type
));
577 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
578 access_offset
, access_size
,
581 /* This is a strided load. We have to load N things separately.
582 * This is the single column of a row-major matrix case.
584 assert(type
->stride
> type_size
);
585 assert(type
->stride
% type_size
== 0);
587 nir_ssa_def
*per_comp
[4];
588 for (unsigned i
= 0; i
< elems
; i
++) {
589 nir_ssa_def
*elem_offset
=
590 nir_iadd(&b
->nb
, offset
,
591 nir_imm_int(&b
->nb
, i
* type
->stride
));
592 struct vtn_ssa_value
*comp
, temp_val
;
594 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
595 temp_val
.type
= glsl_scalar_type(base_type
);
598 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
599 access_offset
, access_size
,
600 &comp
, glsl_scalar_type(base_type
));
601 per_comp
[i
] = comp
->def
;
606 *inout
= vtn_create_ssa_value(b
, type
->type
);
607 (*inout
)->def
= nir_vec(&b
->nb
, per_comp
, elems
);
613 case GLSL_TYPE_ARRAY
: {
614 unsigned elems
= glsl_get_length(type
->type
);
615 for (unsigned i
= 0; i
< elems
; i
++) {
616 nir_ssa_def
*elem_off
=
617 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* type
->stride
));
618 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
619 access_offset
, access_size
,
621 type
->array_element
, &(*inout
)->elems
[i
]);
626 case GLSL_TYPE_STRUCT
: {
627 unsigned elems
= glsl_get_length(type
->type
);
628 for (unsigned i
= 0; i
< elems
; i
++) {
629 nir_ssa_def
*elem_off
=
630 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, type
->offsets
[i
]));
631 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
632 access_offset
, access_size
,
634 type
->members
[i
], &(*inout
)->elems
[i
]);
640 unreachable("Invalid block member type");
644 static struct vtn_ssa_value
*
645 vtn_block_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
648 unsigned access_offset
= 0, access_size
= 0;
650 case vtn_variable_mode_ubo
:
651 op
= nir_intrinsic_load_ubo
;
653 case vtn_variable_mode_ssbo
:
654 op
= nir_intrinsic_load_ssbo
;
656 case vtn_variable_mode_push_constant
:
657 op
= nir_intrinsic_load_push_constant
;
658 vtn_access_chain_get_offset_size(src
->chain
, src
->var
->type
,
659 &access_offset
, &access_size
);
662 assert(!"Invalid block variable mode");
665 nir_ssa_def
*offset
, *index
= NULL
;
667 offset
= vtn_pointer_to_offset(b
, src
, &index
, &chain_idx
);
669 struct vtn_ssa_value
*value
= NULL
;
670 _vtn_block_load_store(b
, op
, true, index
, offset
,
671 access_offset
, access_size
,
672 src
->chain
, chain_idx
, src
->type
, &value
);
677 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
678 struct vtn_pointer
*dst
)
680 nir_ssa_def
*offset
, *index
= NULL
;
682 offset
= vtn_pointer_to_offset(b
, dst
, &index
, &chain_idx
);
684 _vtn_block_load_store(b
, nir_intrinsic_store_ssbo
, false, index
, offset
,
685 0, 0, dst
->chain
, chain_idx
, dst
->type
, &src
);
689 vtn_variable_is_external_block(struct vtn_variable
*var
)
691 return var
->mode
== vtn_variable_mode_ssbo
||
692 var
->mode
== vtn_variable_mode_ubo
||
693 var
->mode
== vtn_variable_mode_push_constant
;
697 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
698 struct vtn_pointer
*ptr
,
699 struct vtn_ssa_value
**inout
)
701 enum glsl_base_type base_type
= glsl_get_base_type(ptr
->type
->type
);
705 case GLSL_TYPE_UINT64
:
706 case GLSL_TYPE_INT64
:
707 case GLSL_TYPE_FLOAT
:
709 case GLSL_TYPE_DOUBLE
:
710 /* At this point, we have a scalar, vector, or matrix so we know that
711 * there cannot be any structure splitting still in the way. By
712 * stopping at the matrix level rather than the vector level, we
713 * ensure that matrices get loaded in the optimal way even if they
714 * are storred row-major in a UBO.
717 *inout
= vtn_local_load(b
, vtn_pointer_to_deref(b
, ptr
));
719 vtn_local_store(b
, *inout
, vtn_pointer_to_deref(b
, ptr
));
723 case GLSL_TYPE_ARRAY
:
724 case GLSL_TYPE_STRUCT
: {
725 unsigned elems
= glsl_get_length(ptr
->type
->type
);
727 assert(*inout
== NULL
);
728 *inout
= rzalloc(b
, struct vtn_ssa_value
);
729 (*inout
)->type
= ptr
->type
->type
;
730 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
733 struct vtn_pointer elem
= *ptr
;
734 elem
.chain
= vtn_access_chain_extend(b
, ptr
->chain
, 1);
735 unsigned link_idx
= ptr
->chain
? ptr
->chain
->length
: 0;
736 elem
.chain
->link
[link_idx
].mode
= vtn_access_mode_literal
;
737 for (unsigned i
= 0; i
< elems
; i
++) {
738 elem
.chain
->link
[link_idx
].id
= i
;
739 elem
.type
= (base_type
== GLSL_TYPE_ARRAY
) ? ptr
->type
->array_element
:
740 ptr
->type
->members
[i
];
741 _vtn_variable_load_store(b
, load
, &elem
, &(*inout
)->elems
[i
]);
747 unreachable("Invalid access chain type");
751 struct vtn_ssa_value
*
752 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
754 if (vtn_variable_is_external_block(src
->var
)) {
755 return vtn_block_load(b
, src
);
757 struct vtn_ssa_value
*val
= NULL
;
758 _vtn_variable_load_store(b
, true, src
, &val
);
764 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
765 struct vtn_pointer
*dest
)
767 if (vtn_variable_is_external_block(dest
->var
)) {
768 assert(dest
->var
->mode
== vtn_variable_mode_ssbo
);
769 vtn_block_store(b
, src
, dest
);
771 _vtn_variable_load_store(b
, false, dest
, &src
);
776 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
777 struct vtn_pointer
*src
)
779 assert(src
->type
->type
== dest
->type
->type
);
780 enum glsl_base_type base_type
= glsl_get_base_type(src
->type
->type
);
784 case GLSL_TYPE_UINT64
:
785 case GLSL_TYPE_INT64
:
786 case GLSL_TYPE_FLOAT
:
787 case GLSL_TYPE_DOUBLE
:
789 /* At this point, we have a scalar, vector, or matrix so we know that
790 * there cannot be any structure splitting still in the way. By
791 * stopping at the matrix level rather than the vector level, we
792 * ensure that matrices get loaded in the optimal way even if they
793 * are storred row-major in a UBO.
795 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
798 case GLSL_TYPE_ARRAY
:
799 case GLSL_TYPE_STRUCT
: {
800 struct vtn_pointer src_elem
= *src
, dest_elem
= *dest
;
801 src_elem
.chain
= vtn_access_chain_extend(b
, src
->chain
, 1);
802 dest_elem
.chain
= vtn_access_chain_extend(b
, dest
->chain
, 1);
803 src_elem
.chain
->link
[src_elem
.chain
->length
- 1].mode
= vtn_access_mode_literal
;
804 dest_elem
.chain
->link
[dest_elem
.chain
->length
- 1].mode
= vtn_access_mode_literal
;
806 unsigned elems
= glsl_get_length(src
->type
->type
);
807 for (unsigned i
= 0; i
< elems
; i
++) {
808 src_elem
.chain
->link
[src_elem
.chain
->length
- 1].id
= i
;
809 dest_elem
.chain
->link
[dest_elem
.chain
->length
- 1].id
= i
;
810 if (base_type
== GLSL_TYPE_STRUCT
) {
811 src_elem
.type
= src
->type
->members
[i
];
812 dest_elem
.type
= dest
->type
->members
[i
];
814 src_elem
.type
= src
->type
->array_element
;
815 dest_elem
.type
= dest
->type
->array_element
;
817 _vtn_variable_copy(b
, &dest_elem
, &src_elem
);
823 unreachable("Invalid access chain type");
828 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
829 struct vtn_pointer
*src
)
831 /* TODO: At some point, we should add a special-case for when we can
832 * just emit a copy_var intrinsic.
834 _vtn_variable_copy(b
, dest
, src
);
838 set_mode_system_value(nir_variable_mode
*mode
)
840 assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
841 *mode
= nir_var_system_value
;
845 vtn_get_builtin_location(struct vtn_builder
*b
,
846 SpvBuiltIn builtin
, int *location
,
847 nir_variable_mode
*mode
)
850 case SpvBuiltInPosition
:
851 *location
= VARYING_SLOT_POS
;
853 case SpvBuiltInPointSize
:
854 *location
= VARYING_SLOT_PSIZ
;
856 case SpvBuiltInClipDistance
:
857 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
859 case SpvBuiltInCullDistance
:
860 *location
= VARYING_SLOT_CULL_DIST0
;
862 case SpvBuiltInVertexIndex
:
863 *location
= SYSTEM_VALUE_VERTEX_ID
;
864 set_mode_system_value(mode
);
866 case SpvBuiltInVertexId
:
867 /* Vulkan defines VertexID to be zero-based and reserves the new
868 * builtin keyword VertexIndex to indicate the non-zero-based value.
870 *location
= SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
871 set_mode_system_value(mode
);
873 case SpvBuiltInInstanceIndex
:
874 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
875 set_mode_system_value(mode
);
877 case SpvBuiltInInstanceId
:
878 *location
= SYSTEM_VALUE_INSTANCE_ID
;
879 set_mode_system_value(mode
);
881 case SpvBuiltInPrimitiveId
:
882 if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
) {
883 assert(*mode
== nir_var_shader_in
);
884 *location
= VARYING_SLOT_PRIMITIVE_ID
;
885 } else if (*mode
== nir_var_shader_out
) {
886 *location
= VARYING_SLOT_PRIMITIVE_ID
;
888 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
889 set_mode_system_value(mode
);
892 case SpvBuiltInInvocationId
:
893 *location
= SYSTEM_VALUE_INVOCATION_ID
;
894 set_mode_system_value(mode
);
896 case SpvBuiltInLayer
:
897 *location
= VARYING_SLOT_LAYER
;
898 if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
)
899 *mode
= nir_var_shader_in
;
900 else if (b
->shader
->stage
== MESA_SHADER_GEOMETRY
)
901 *mode
= nir_var_shader_out
;
903 unreachable("invalid stage for SpvBuiltInLayer");
905 case SpvBuiltInViewportIndex
:
906 *location
= VARYING_SLOT_VIEWPORT
;
907 if (b
->shader
->stage
== MESA_SHADER_GEOMETRY
)
908 *mode
= nir_var_shader_out
;
909 else if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
)
910 *mode
= nir_var_shader_in
;
912 unreachable("invalid stage for SpvBuiltInViewportIndex");
914 case SpvBuiltInTessLevelOuter
:
915 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
917 case SpvBuiltInTessLevelInner
:
918 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
920 case SpvBuiltInTessCoord
:
921 *location
= SYSTEM_VALUE_TESS_COORD
;
922 set_mode_system_value(mode
);
924 case SpvBuiltInPatchVertices
:
925 *location
= SYSTEM_VALUE_VERTICES_IN
;
926 set_mode_system_value(mode
);
928 case SpvBuiltInFragCoord
:
929 *location
= VARYING_SLOT_POS
;
930 assert(*mode
== nir_var_shader_in
);
932 case SpvBuiltInPointCoord
:
933 *location
= VARYING_SLOT_PNTC
;
934 assert(*mode
== nir_var_shader_in
);
936 case SpvBuiltInFrontFacing
:
937 *location
= SYSTEM_VALUE_FRONT_FACE
;
938 set_mode_system_value(mode
);
940 case SpvBuiltInSampleId
:
941 *location
= SYSTEM_VALUE_SAMPLE_ID
;
942 set_mode_system_value(mode
);
944 case SpvBuiltInSamplePosition
:
945 *location
= SYSTEM_VALUE_SAMPLE_POS
;
946 set_mode_system_value(mode
);
948 case SpvBuiltInSampleMask
:
949 if (*mode
== nir_var_shader_out
) {
950 *location
= FRAG_RESULT_SAMPLE_MASK
;
952 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
953 set_mode_system_value(mode
);
956 case SpvBuiltInFragDepth
:
957 *location
= FRAG_RESULT_DEPTH
;
958 assert(*mode
== nir_var_shader_out
);
960 case SpvBuiltInNumWorkgroups
:
961 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
962 set_mode_system_value(mode
);
964 case SpvBuiltInWorkgroupSize
:
965 /* This should already be handled */
966 unreachable("unsupported builtin");
968 case SpvBuiltInWorkgroupId
:
969 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
970 set_mode_system_value(mode
);
972 case SpvBuiltInLocalInvocationId
:
973 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
974 set_mode_system_value(mode
);
976 case SpvBuiltInLocalInvocationIndex
:
977 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
978 set_mode_system_value(mode
);
980 case SpvBuiltInGlobalInvocationId
:
981 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
982 set_mode_system_value(mode
);
984 case SpvBuiltInBaseVertex
:
985 *location
= SYSTEM_VALUE_BASE_VERTEX
;
986 set_mode_system_value(mode
);
988 case SpvBuiltInBaseInstance
:
989 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
990 set_mode_system_value(mode
);
992 case SpvBuiltInDrawIndex
:
993 *location
= SYSTEM_VALUE_DRAW_ID
;
994 set_mode_system_value(mode
);
996 case SpvBuiltInViewIndex
:
997 *location
= SYSTEM_VALUE_VIEW_INDEX
;
998 set_mode_system_value(mode
);
1000 case SpvBuiltInHelperInvocation
:
1002 unreachable("unsupported builtin");
1007 apply_var_decoration(struct vtn_builder
*b
, nir_variable
*nir_var
,
1008 const struct vtn_decoration
*dec
)
1010 switch (dec
->decoration
) {
1011 case SpvDecorationRelaxedPrecision
:
1012 break; /* FIXME: Do nothing with this for now. */
1013 case SpvDecorationNoPerspective
:
1014 nir_var
->data
.interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1016 case SpvDecorationFlat
:
1017 nir_var
->data
.interpolation
= INTERP_MODE_FLAT
;
1019 case SpvDecorationCentroid
:
1020 nir_var
->data
.centroid
= true;
1022 case SpvDecorationSample
:
1023 nir_var
->data
.sample
= true;
1025 case SpvDecorationInvariant
:
1026 nir_var
->data
.invariant
= true;
1028 case SpvDecorationConstant
:
1029 assert(nir_var
->constant_initializer
!= NULL
);
1030 nir_var
->data
.read_only
= true;
1032 case SpvDecorationNonReadable
:
1033 nir_var
->data
.image
.write_only
= true;
1035 case SpvDecorationNonWritable
:
1036 nir_var
->data
.read_only
= true;
1037 nir_var
->data
.image
.read_only
= true;
1039 case SpvDecorationComponent
:
1040 nir_var
->data
.location_frac
= dec
->literals
[0];
1042 case SpvDecorationIndex
:
1043 nir_var
->data
.index
= dec
->literals
[0];
1045 case SpvDecorationBuiltIn
: {
1046 SpvBuiltIn builtin
= dec
->literals
[0];
1048 if (builtin
== SpvBuiltInWorkgroupSize
) {
1049 /* This shouldn't be a builtin. It's actually a constant. */
1050 nir_var
->data
.mode
= nir_var_global
;
1051 nir_var
->data
.read_only
= true;
1053 nir_constant
*c
= rzalloc(nir_var
, nir_constant
);
1054 c
->values
[0].u32
[0] = b
->shader
->info
.cs
.local_size
[0];
1055 c
->values
[0].u32
[1] = b
->shader
->info
.cs
.local_size
[1];
1056 c
->values
[0].u32
[2] = b
->shader
->info
.cs
.local_size
[2];
1057 nir_var
->constant_initializer
= c
;
1061 nir_variable_mode mode
= nir_var
->data
.mode
;
1062 vtn_get_builtin_location(b
, builtin
, &nir_var
->data
.location
, &mode
);
1063 nir_var
->data
.mode
= mode
;
1066 case SpvBuiltInTessLevelOuter
:
1067 case SpvBuiltInTessLevelInner
:
1068 nir_var
->data
.compact
= true;
1070 case SpvBuiltInSamplePosition
:
1071 nir_var
->data
.origin_upper_left
= b
->origin_upper_left
;
1073 case SpvBuiltInFragCoord
:
1074 nir_var
->data
.pixel_center_integer
= b
->pixel_center_integer
;
1081 case SpvDecorationSpecId
:
1082 case SpvDecorationRowMajor
:
1083 case SpvDecorationColMajor
:
1084 case SpvDecorationMatrixStride
:
1085 case SpvDecorationRestrict
:
1086 case SpvDecorationAliased
:
1087 case SpvDecorationVolatile
:
1088 case SpvDecorationCoherent
:
1089 case SpvDecorationUniform
:
1090 case SpvDecorationStream
:
1091 case SpvDecorationOffset
:
1092 case SpvDecorationLinkageAttributes
:
1093 break; /* Do nothing with these here */
1095 case SpvDecorationPatch
:
1096 nir_var
->data
.patch
= true;
1099 case SpvDecorationLocation
:
1100 unreachable("Handled above");
1102 case SpvDecorationBlock
:
1103 case SpvDecorationBufferBlock
:
1104 case SpvDecorationArrayStride
:
1105 case SpvDecorationGLSLShared
:
1106 case SpvDecorationGLSLPacked
:
1107 break; /* These can apply to a type but we don't care about them */
1109 case SpvDecorationBinding
:
1110 case SpvDecorationDescriptorSet
:
1111 case SpvDecorationNoContraction
:
1112 case SpvDecorationInputAttachmentIndex
:
1113 vtn_warn("Decoration not allowed for variable or structure member: %s",
1114 spirv_decoration_to_string(dec
->decoration
));
1117 case SpvDecorationXfbBuffer
:
1118 case SpvDecorationXfbStride
:
1119 vtn_warn("Vulkan does not have transform feedback: %s",
1120 spirv_decoration_to_string(dec
->decoration
));
1123 case SpvDecorationCPacked
:
1124 case SpvDecorationSaturatedConversion
:
1125 case SpvDecorationFuncParamAttr
:
1126 case SpvDecorationFPRoundingMode
:
1127 case SpvDecorationFPFastMathMode
:
1128 case SpvDecorationAlignment
:
1129 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1130 spirv_decoration_to_string(dec
->decoration
));
1134 unreachable("Unhandled decoration");
1139 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1140 const struct vtn_decoration
*dec
, void *out_is_patch
)
1142 if (dec
->decoration
== SpvDecorationPatch
) {
1143 *((bool *) out_is_patch
) = true;
1148 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1149 const struct vtn_decoration
*dec
, void *void_var
)
1151 struct vtn_variable
*vtn_var
= void_var
;
1153 /* Handle decorations that apply to a vtn_variable as a whole */
1154 switch (dec
->decoration
) {
1155 case SpvDecorationBinding
:
1156 vtn_var
->binding
= dec
->literals
[0];
1158 case SpvDecorationDescriptorSet
:
1159 vtn_var
->descriptor_set
= dec
->literals
[0];
1161 case SpvDecorationInputAttachmentIndex
:
1162 vtn_var
->input_attachment_index
= dec
->literals
[0];
1164 case SpvDecorationPatch
:
1165 vtn_var
->patch
= true;
1171 if (val
->value_type
== vtn_value_type_pointer
) {
1172 assert(val
->pointer
->var
== void_var
);
1173 assert(val
->pointer
->chain
== NULL
);
1174 assert(member
== -1);
1176 assert(val
->value_type
== vtn_value_type_type
);
1179 /* Location is odd. If applied to a split structure, we have to walk the
1180 * whole thing and accumulate the location. It's easier to handle as a
1183 if (dec
->decoration
== SpvDecorationLocation
) {
1184 unsigned location
= dec
->literals
[0];
1185 bool is_vertex_input
;
1186 if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
&&
1187 vtn_var
->mode
== vtn_variable_mode_output
) {
1188 is_vertex_input
= false;
1189 location
+= FRAG_RESULT_DATA0
;
1190 } else if (b
->shader
->stage
== MESA_SHADER_VERTEX
&&
1191 vtn_var
->mode
== vtn_variable_mode_input
) {
1192 is_vertex_input
= true;
1193 location
+= VERT_ATTRIB_GENERIC0
;
1194 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1195 vtn_var
->mode
== vtn_variable_mode_output
) {
1196 is_vertex_input
= false;
1197 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1199 vtn_warn("Location must be on input or output variable");
1204 /* This handles the member and lone variable cases */
1205 vtn_var
->var
->data
.location
= location
;
1207 /* This handles the structure member case */
1208 assert(vtn_var
->members
);
1210 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1211 for (unsigned i
= 0; i
< length
; i
++) {
1212 vtn_var
->members
[i
]->data
.location
= location
;
1214 glsl_count_attribute_slots(vtn_var
->members
[i
]->interface_type
,
1221 assert(member
<= 0);
1222 apply_var_decoration(b
, vtn_var
->var
, dec
);
1223 } else if (vtn_var
->members
) {
1225 assert(vtn_var
->members
);
1226 apply_var_decoration(b
, vtn_var
->members
[member
], dec
);
1229 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1230 for (unsigned i
= 0; i
< length
; i
++)
1231 apply_var_decoration(b
, vtn_var
->members
[i
], dec
);
1234 /* A few variables, those with external storage, have no actual
1235 * nir_variables associated with them. Fortunately, all decorations
1236 * we care about for those variables are on the type only.
1238 assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1239 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1240 vtn_var
->mode
== vtn_variable_mode_push_constant
);
1245 static enum vtn_variable_mode
1246 vtn_storage_class_to_mode(SpvStorageClass
class,
1247 struct vtn_type
*interface_type
,
1248 nir_variable_mode
*nir_mode_out
)
1250 enum vtn_variable_mode mode
;
1251 nir_variable_mode nir_mode
;
1253 case SpvStorageClassUniform
:
1254 if (interface_type
->block
) {
1255 mode
= vtn_variable_mode_ubo
;
1257 } else if (interface_type
->buffer_block
) {
1258 mode
= vtn_variable_mode_ssbo
;
1261 assert(!"Invalid uniform variable type");
1264 case SpvStorageClassUniformConstant
:
1265 if (glsl_type_is_image(interface_type
->type
)) {
1266 mode
= vtn_variable_mode_image
;
1267 nir_mode
= nir_var_uniform
;
1268 } else if (glsl_type_is_sampler(interface_type
->type
)) {
1269 mode
= vtn_variable_mode_sampler
;
1270 nir_mode
= nir_var_uniform
;
1272 assert(!"Invalid uniform constant variable type");
1275 case SpvStorageClassPushConstant
:
1276 mode
= vtn_variable_mode_push_constant
;
1277 nir_mode
= nir_var_uniform
;
1279 case SpvStorageClassInput
:
1280 mode
= vtn_variable_mode_input
;
1281 nir_mode
= nir_var_shader_in
;
1283 case SpvStorageClassOutput
:
1284 mode
= vtn_variable_mode_output
;
1285 nir_mode
= nir_var_shader_out
;
1287 case SpvStorageClassPrivate
:
1288 mode
= vtn_variable_mode_global
;
1289 nir_mode
= nir_var_global
;
1291 case SpvStorageClassFunction
:
1292 mode
= vtn_variable_mode_local
;
1293 nir_mode
= nir_var_local
;
1295 case SpvStorageClassWorkgroup
:
1296 mode
= vtn_variable_mode_workgroup
;
1297 nir_mode
= nir_var_shared
;
1299 case SpvStorageClassCrossWorkgroup
:
1300 case SpvStorageClassGeneric
:
1301 case SpvStorageClassAtomicCounter
:
1303 unreachable("Unhandled variable storage class");
1307 *nir_mode_out
= nir_mode
;
1313 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
1315 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
1318 if (var
->mode
== vtn_variable_mode_input
) {
1319 return stage
== MESA_SHADER_TESS_CTRL
||
1320 stage
== MESA_SHADER_TESS_EVAL
||
1321 stage
== MESA_SHADER_GEOMETRY
;
1324 if (var
->mode
== vtn_variable_mode_output
)
1325 return stage
== MESA_SHADER_TESS_CTRL
;
1331 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
1332 struct vtn_type
*type
, SpvStorageClass storage_class
,
1333 nir_constant
*initializer
)
1335 struct vtn_type
*without_array
= type
;
1336 while(glsl_type_is_array(without_array
->type
))
1337 without_array
= without_array
->array_element
;
1339 enum vtn_variable_mode mode
;
1340 nir_variable_mode nir_mode
;
1341 mode
= vtn_storage_class_to_mode(storage_class
, without_array
, &nir_mode
);
1344 case vtn_variable_mode_ubo
:
1345 b
->shader
->info
.num_ubos
++;
1347 case vtn_variable_mode_ssbo
:
1348 b
->shader
->info
.num_ssbos
++;
1350 case vtn_variable_mode_image
:
1351 b
->shader
->info
.num_images
++;
1353 case vtn_variable_mode_sampler
:
1354 b
->shader
->info
.num_textures
++;
1356 case vtn_variable_mode_push_constant
:
1357 b
->shader
->num_uniforms
= vtn_type_block_size(type
);
1360 /* No tallying is needed */
1364 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
1368 assert(val
->value_type
== vtn_value_type_pointer
);
1369 val
->pointer
= vtn_pointer_for_variable(b
, var
);
1371 switch (var
->mode
) {
1372 case vtn_variable_mode_local
:
1373 case vtn_variable_mode_global
:
1374 case vtn_variable_mode_image
:
1375 case vtn_variable_mode_sampler
:
1376 case vtn_variable_mode_workgroup
:
1377 /* For these, we create the variable normally */
1378 var
->var
= rzalloc(b
->shader
, nir_variable
);
1379 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1380 var
->var
->type
= var
->type
->type
;
1381 var
->var
->data
.mode
= nir_mode
;
1383 switch (var
->mode
) {
1384 case vtn_variable_mode_image
:
1385 case vtn_variable_mode_sampler
:
1386 var
->var
->interface_type
= without_array
->type
;
1389 var
->var
->interface_type
= NULL
;
1394 case vtn_variable_mode_input
:
1395 case vtn_variable_mode_output
: {
1396 /* In order to know whether or not we're a per-vertex inout, we need
1397 * the patch qualifier. This means walking the variable decorations
1398 * early before we actually create any variables. Not a big deal.
1400 * GLSLang really likes to place decorations in the most interior
1401 * thing it possibly can. In particular, if you have a struct, it
1402 * will place the patch decorations on the struct members. This
1403 * should be handled by the variable splitting below just fine.
1405 * If you have an array-of-struct, things get even more weird as it
1406 * will place the patch decorations on the struct even though it's
1407 * inside an array and some of the members being patch and others not
1408 * makes no sense whatsoever. Since the only sensible thing is for
1409 * it to be all or nothing, we'll call it patch if any of the members
1410 * are declared patch.
1413 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
1414 if (glsl_type_is_array(var
->type
->type
) &&
1415 glsl_type_is_struct(without_array
->type
)) {
1416 vtn_foreach_decoration(b
, without_array
->val
,
1417 var_is_patch_cb
, &var
->patch
);
1420 /* For inputs and outputs, we immediately split structures. This
1421 * is for a couple of reasons. For one, builtins may all come in
1422 * a struct and we really want those split out into separate
1423 * variables. For another, interpolation qualifiers can be
1424 * applied to members of the top-level struct ane we need to be
1425 * able to preserve that information.
1428 int array_length
= -1;
1429 struct vtn_type
*interface_type
= var
->type
;
1430 if (is_per_vertex_inout(var
, b
->shader
->stage
)) {
1431 /* In Geometry shaders (and some tessellation), inputs come
1432 * in per-vertex arrays. However, some builtins come in
1433 * non-per-vertex, hence the need for the is_array check. In
1434 * any case, there are no non-builtin arrays allowed so this
1435 * check should be sufficient.
1437 interface_type
= var
->type
->array_element
;
1438 array_length
= glsl_get_length(var
->type
->type
);
1441 if (glsl_type_is_struct(interface_type
->type
)) {
1442 /* It's a struct. Split it. */
1443 unsigned num_members
= glsl_get_length(interface_type
->type
);
1444 var
->members
= ralloc_array(b
, nir_variable
*, num_members
);
1446 for (unsigned i
= 0; i
< num_members
; i
++) {
1447 const struct glsl_type
*mtype
= interface_type
->members
[i
]->type
;
1448 if (array_length
>= 0)
1449 mtype
= glsl_array_type(mtype
, array_length
);
1451 var
->members
[i
] = rzalloc(b
->shader
, nir_variable
);
1452 var
->members
[i
]->name
=
1453 ralloc_asprintf(var
->members
[i
], "%s.%d", val
->name
, i
);
1454 var
->members
[i
]->type
= mtype
;
1455 var
->members
[i
]->interface_type
=
1456 interface_type
->members
[i
]->type
;
1457 var
->members
[i
]->data
.mode
= nir_mode
;
1458 var
->members
[i
]->data
.patch
= var
->patch
;
1461 var
->var
= rzalloc(b
->shader
, nir_variable
);
1462 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1463 var
->var
->type
= var
->type
->type
;
1464 var
->var
->interface_type
= interface_type
->type
;
1465 var
->var
->data
.mode
= nir_mode
;
1466 var
->var
->data
.patch
= var
->patch
;
1469 /* For inputs and outputs, we need to grab locations and builtin
1470 * information from the interface type.
1472 vtn_foreach_decoration(b
, interface_type
->val
, var_decoration_cb
, var
);
1476 case vtn_variable_mode_param
:
1477 unreachable("Not created through OpVariable");
1479 case vtn_variable_mode_ubo
:
1480 case vtn_variable_mode_ssbo
:
1481 case vtn_variable_mode_push_constant
:
1482 /* These don't need actual variables. */
1487 var
->var
->constant_initializer
=
1488 nir_constant_clone(initializer
, var
->var
);
1491 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
1493 if (var
->mode
== vtn_variable_mode_image
||
1494 var
->mode
== vtn_variable_mode_sampler
) {
1495 /* XXX: We still need the binding information in the nir_variable
1496 * for these. We should fix that.
1498 var
->var
->data
.binding
= var
->binding
;
1499 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
1500 var
->var
->data
.index
= var
->input_attachment_index
;
1502 if (var
->mode
== vtn_variable_mode_image
)
1503 var
->var
->data
.image
.format
= without_array
->image_format
;
1506 if (var
->mode
== vtn_variable_mode_local
) {
1507 assert(var
->members
== NULL
&& var
->var
!= NULL
);
1508 nir_function_impl_add_variable(b
->impl
, var
->var
);
1509 } else if (var
->var
) {
1510 nir_shader_add_variable(b
->shader
, var
->var
);
1511 } else if (var
->members
) {
1512 unsigned count
= glsl_get_length(without_array
->type
);
1513 for (unsigned i
= 0; i
< count
; i
++) {
1514 assert(var
->members
[i
]->data
.mode
!= nir_var_local
);
1515 nir_shader_add_variable(b
->shader
, var
->members
[i
]);
1518 assert(var
->mode
== vtn_variable_mode_ubo
||
1519 var
->mode
== vtn_variable_mode_ssbo
||
1520 var
->mode
== vtn_variable_mode_push_constant
);
1525 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
1526 const uint32_t *w
, unsigned count
)
1530 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1531 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1535 case SpvOpVariable
: {
1536 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1538 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1540 SpvStorageClass storage_class
= w
[3];
1541 nir_constant
*initializer
= NULL
;
1543 initializer
= vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
1545 vtn_create_variable(b
, val
, type
, storage_class
, initializer
);
1549 case SpvOpAccessChain
:
1550 case SpvOpInBoundsAccessChain
: {
1551 struct vtn_pointer
*base
, *ptr
;
1552 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
1553 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1554 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1555 * to combine an array of images with a single sampler to get an
1556 * array of sampled images that all share the same sampler.
1557 * Fortunately, this means that we can more-or-less ignore the
1558 * sampler when crawling the access chain, but it does leave us
1559 * with this rather awkward little special-case.
1561 base
= base_val
->sampled_image
->image
;
1563 assert(base_val
->value_type
== vtn_value_type_pointer
);
1564 base
= base_val
->pointer
;
1567 struct vtn_access_chain
*chain
=
1568 vtn_access_chain_extend(b
, base
->chain
, count
- 4);
1569 struct vtn_type
*type
= base
->type
;
1571 unsigned idx
= base
->chain
? base
->chain
->length
: 0;
1572 for (int i
= 4; i
< count
; i
++) {
1573 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
1574 if (link_val
->value_type
== vtn_value_type_constant
) {
1575 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
1576 chain
->link
[idx
].id
= link_val
->constant
->values
[0].u32
[0];
1578 chain
->link
[idx
].mode
= vtn_access_mode_id
;
1579 chain
->link
[idx
].id
= w
[i
];
1582 if (glsl_type_is_struct(type
->type
)) {
1583 assert(chain
->link
[idx
].mode
== vtn_access_mode_literal
);
1584 type
= type
->members
[chain
->link
[idx
].id
];
1586 type
= type
->array_element
;
1591 ptr
= ralloc(b
, struct vtn_pointer
);
1596 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1597 struct vtn_value
*val
=
1598 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1599 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1600 val
->sampled_image
->image
= ptr
;
1601 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
1603 struct vtn_value
*val
=
1604 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1610 case SpvOpCopyMemory
: {
1611 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
1612 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
1614 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
1619 struct vtn_pointer
*src
=
1620 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1622 if (src
->mode
== vtn_variable_mode_image
||
1623 src
->mode
== vtn_variable_mode_sampler
) {
1624 vtn_push_value(b
, w
[2], vtn_value_type_pointer
)->pointer
= src
;
1628 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1629 val
->ssa
= vtn_variable_load(b
, src
);
1634 struct vtn_pointer
*dest
=
1635 vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
1637 if (glsl_type_is_sampler(dest
->type
->type
)) {
1638 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
1639 "propagation to workaround the problem.");
1640 assert(dest
->var
->copy_prop_sampler
== NULL
);
1641 dest
->var
->copy_prop_sampler
=
1642 vtn_value(b
, w
[2], vtn_value_type_pointer
)->pointer
;
1646 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
1647 vtn_variable_store(b
, src
, dest
);
1651 case SpvOpArrayLength
: {
1652 struct vtn_pointer
*ptr
=
1653 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1655 const uint32_t offset
= ptr
->var
->type
->offsets
[w
[4]];
1656 const uint32_t stride
= ptr
->var
->type
->members
[w
[4]]->stride
;
1659 struct vtn_type
*type
;
1660 nir_ssa_def
*index
=
1661 get_vulkan_resource_index(b
, ptr
, &type
, &chain_idx
);
1663 nir_intrinsic_instr
*instr
=
1664 nir_intrinsic_instr_create(b
->nb
.shader
,
1665 nir_intrinsic_get_buffer_size
);
1666 instr
->src
[0] = nir_src_for_ssa(index
);
1667 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
1668 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
1669 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
1671 /* array_length = max(buffer_size - offset, 0) / stride */
1672 nir_ssa_def
*array_length
=
1677 nir_imm_int(&b
->nb
, offset
)),
1678 nir_imm_int(&b
->nb
, 0u)),
1679 nir_imm_int(&b
->nb
, stride
));
1681 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1682 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
1683 val
->ssa
->def
= array_length
;
1687 case SpvOpCopyMemorySized
:
1689 unreachable("Unhandled opcode");