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 new_len
= old
->length
+ new_ids
;
38 /* TODO: don't use rzalloc */
39 chain
= rzalloc_size(b
, sizeof(*chain
) + new_len
* sizeof(chain
->link
[0]));
41 chain
->var
= old
->var
;
42 chain
->length
= new_len
;
44 for (unsigned i
= 0; i
< old
->length
; 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 static struct vtn_type
*
66 vtn_access_chain_tail_type(struct vtn_builder
*b
,
67 struct vtn_access_chain
*chain
)
69 struct vtn_type
*type
= chain
->var
->type
;
70 for (unsigned i
= 0; i
< chain
->length
; i
++) {
71 if (glsl_type_is_struct(type
->type
)) {
72 assert(chain
->link
[i
].mode
== vtn_access_mode_literal
);
73 type
= type
->members
[chain
->link
[i
].id
];
75 type
= type
->array_element
;
81 /* Crawls a chain of array derefs and rewrites the types so that the
82 * lengths stay the same but the terminal type is the one given by
83 * tail_type. This is useful for split structures.
86 rewrite_deref_types(nir_deref
*deref
, const struct glsl_type
*type
)
90 assert(deref
->child
->deref_type
== nir_deref_type_array
);
91 assert(glsl_type_is_array(deref
->type
));
92 rewrite_deref_types(deref
->child
, glsl_get_array_element(type
));
97 vtn_access_chain_to_deref(struct vtn_builder
*b
, struct vtn_access_chain
*chain
)
99 /* Do on-the-fly copy propagation for samplers. */
100 if (chain
->var
->copy_prop_sampler
)
101 return vtn_access_chain_to_deref(b
, chain
->var
->copy_prop_sampler
);
103 nir_deref_var
*deref_var
;
104 if (chain
->var
->var
) {
105 deref_var
= nir_deref_var_create(b
, chain
->var
->var
);
107 assert(chain
->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_type
*deref_type
= chain
->var
->type
;
114 nir_deref
*tail
= &deref_var
->deref
;
115 nir_variable
**members
= chain
->var
->members
;
117 for (unsigned i
= 0; i
< chain
->length
; i
++) {
118 enum glsl_base_type base_type
= glsl_get_base_type(deref_type
->type
);
122 case GLSL_TYPE_UINT64
:
123 case GLSL_TYPE_INT64
:
124 case GLSL_TYPE_FLOAT
:
125 case GLSL_TYPE_DOUBLE
:
127 case GLSL_TYPE_ARRAY
: {
128 deref_type
= deref_type
->array_element
;
130 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
131 deref_arr
->deref
.type
= deref_type
->type
;
133 if (chain
->link
[i
].mode
== vtn_access_mode_literal
) {
134 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
135 deref_arr
->base_offset
= chain
->link
[i
].id
;
137 assert(chain
->link
[i
].mode
== vtn_access_mode_id
);
138 deref_arr
->deref_array_type
= nir_deref_array_type_indirect
;
139 deref_arr
->base_offset
= 0;
140 deref_arr
->indirect
=
141 nir_src_for_ssa(vtn_ssa_value(b
, chain
->link
[i
].id
)->def
);
143 tail
->child
= &deref_arr
->deref
;
148 case GLSL_TYPE_STRUCT
: {
149 assert(chain
->link
[i
].mode
== vtn_access_mode_literal
);
150 unsigned idx
= chain
->link
[i
].id
;
151 deref_type
= deref_type
->members
[idx
];
153 /* This is a pre-split structure. */
154 deref_var
->var
= members
[idx
];
155 rewrite_deref_types(&deref_var
->deref
, members
[idx
]->type
);
156 assert(tail
->type
== deref_type
->type
);
159 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, idx
);
160 deref_struct
->deref
.type
= deref_type
->type
;
161 tail
->child
= &deref_struct
->deref
;
167 unreachable("Invalid type for deref");
171 assert(members
== NULL
);
176 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_var
*deref
,
177 nir_deref
*tail
, struct vtn_ssa_value
*inout
)
179 /* The deref tail may contain a deref to select a component of a vector (in
180 * other words, it might not be an actual tail) so we have to save it away
181 * here since we overwrite it later.
183 nir_deref
*old_child
= tail
->child
;
185 if (glsl_type_is_vector_or_scalar(tail
->type
)) {
186 /* Terminate the deref chain in case there is one more link to pick
187 * off a component of the vector.
191 nir_intrinsic_op op
= load
? nir_intrinsic_load_var
:
192 nir_intrinsic_store_var
;
194 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
195 intrin
->variables
[0] = nir_deref_var_clone(deref
, intrin
);
196 intrin
->num_components
= glsl_get_vector_elements(tail
->type
);
199 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
200 intrin
->num_components
,
201 glsl_get_bit_size(tail
->type
),
203 inout
->def
= &intrin
->dest
.ssa
;
205 nir_intrinsic_set_write_mask(intrin
, (1 << intrin
->num_components
) - 1);
206 intrin
->src
[0] = nir_src_for_ssa(inout
->def
);
209 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
210 } else if (glsl_get_base_type(tail
->type
) == GLSL_TYPE_ARRAY
||
211 glsl_type_is_matrix(tail
->type
)) {
212 unsigned elems
= glsl_get_length(tail
->type
);
213 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
214 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
215 deref_arr
->deref
.type
= glsl_get_array_element(tail
->type
);
216 tail
->child
= &deref_arr
->deref
;
217 for (unsigned i
= 0; i
< elems
; i
++) {
218 deref_arr
->base_offset
= i
;
219 _vtn_local_load_store(b
, load
, deref
, tail
->child
, inout
->elems
[i
]);
222 assert(glsl_get_base_type(tail
->type
) == GLSL_TYPE_STRUCT
);
223 unsigned elems
= glsl_get_length(tail
->type
);
224 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, 0);
225 tail
->child
= &deref_struct
->deref
;
226 for (unsigned i
= 0; i
< elems
; i
++) {
227 deref_struct
->index
= i
;
228 deref_struct
->deref
.type
= glsl_get_struct_field(tail
->type
, i
);
229 _vtn_local_load_store(b
, load
, deref
, tail
->child
, inout
->elems
[i
]);
233 tail
->child
= old_child
;
237 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
239 struct vtn_access_chain
*chain
=
240 vtn_value(b
, id
, vtn_value_type_access_chain
)->access_chain
;
242 return vtn_access_chain_to_deref(b
, chain
);
246 * Gets the NIR-level deref tail, which may have as a child an array deref
247 * selecting which component due to OpAccessChain supporting per-component
248 * indexing in SPIR-V.
251 get_deref_tail(nir_deref_var
*deref
)
253 nir_deref
*cur
= &deref
->deref
;
254 while (!glsl_type_is_vector_or_scalar(cur
->type
) && cur
->child
)
260 struct vtn_ssa_value
*
261 vtn_local_load(struct vtn_builder
*b
, nir_deref_var
*src
)
263 nir_deref
*src_tail
= get_deref_tail(src
);
264 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
265 _vtn_local_load_store(b
, true, src
, src_tail
, val
);
267 if (src_tail
->child
) {
268 nir_deref_array
*vec_deref
= nir_deref_as_array(src_tail
->child
);
269 assert(vec_deref
->deref
.child
== NULL
);
270 val
->type
= vec_deref
->deref
.type
;
271 if (vec_deref
->deref_array_type
== nir_deref_array_type_direct
)
272 val
->def
= vtn_vector_extract(b
, val
->def
, vec_deref
->base_offset
);
274 val
->def
= vtn_vector_extract_dynamic(b
, val
->def
,
275 vec_deref
->indirect
.ssa
);
282 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
285 nir_deref
*dest_tail
= get_deref_tail(dest
);
287 if (dest_tail
->child
) {
288 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
289 _vtn_local_load_store(b
, true, dest
, dest_tail
, val
);
290 nir_deref_array
*deref
= nir_deref_as_array(dest_tail
->child
);
291 assert(deref
->deref
.child
== NULL
);
292 if (deref
->deref_array_type
== nir_deref_array_type_direct
)
293 val
->def
= vtn_vector_insert(b
, val
->def
, src
->def
,
296 val
->def
= vtn_vector_insert_dynamic(b
, val
->def
, src
->def
,
297 deref
->indirect
.ssa
);
298 _vtn_local_load_store(b
, false, dest
, dest_tail
, val
);
300 _vtn_local_load_store(b
, false, dest
, dest_tail
, src
);
305 get_vulkan_resource_index(struct vtn_builder
*b
, struct vtn_access_chain
*chain
,
306 struct vtn_type
**type
, unsigned *chain_idx
)
308 /* Push constants have no explicit binding */
309 if (chain
->var
->mode
== vtn_variable_mode_push_constant
) {
311 *type
= chain
->var
->type
;
315 nir_ssa_def
*array_index
;
316 if (glsl_type_is_array(chain
->var
->type
->type
)) {
317 assert(chain
->length
> 0);
318 array_index
= vtn_access_link_as_ssa(b
, chain
->link
[0], 1);
320 *type
= chain
->var
->type
->array_element
;
322 array_index
= nir_imm_int(&b
->nb
, 0);
324 *type
= chain
->var
->type
;
327 nir_intrinsic_instr
*instr
=
328 nir_intrinsic_instr_create(b
->nb
.shader
,
329 nir_intrinsic_vulkan_resource_index
);
330 instr
->src
[0] = nir_src_for_ssa(array_index
);
331 nir_intrinsic_set_desc_set(instr
, chain
->var
->descriptor_set
);
332 nir_intrinsic_set_binding(instr
, chain
->var
->binding
);
334 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
335 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
337 return &instr
->dest
.ssa
;
341 vtn_access_chain_to_offset(struct vtn_builder
*b
,
342 struct vtn_access_chain
*chain
,
343 nir_ssa_def
**index_out
, struct vtn_type
**type_out
,
344 unsigned *end_idx_out
, bool stop_at_matrix
)
347 struct vtn_type
*type
;
348 *index_out
= get_vulkan_resource_index(b
, chain
, &type
, &idx
);
350 nir_ssa_def
*offset
= nir_imm_int(&b
->nb
, 0);
351 for (; idx
< chain
->length
; idx
++) {
352 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
356 case GLSL_TYPE_UINT64
:
357 case GLSL_TYPE_INT64
:
358 case GLSL_TYPE_FLOAT
:
359 case GLSL_TYPE_DOUBLE
:
361 /* Some users may not want matrix or vector derefs */
366 case GLSL_TYPE_ARRAY
:
367 offset
= nir_iadd(&b
->nb
, offset
,
368 vtn_access_link_as_ssa(b
, chain
->link
[idx
],
371 type
= type
->array_element
;
374 case GLSL_TYPE_STRUCT
: {
375 assert(chain
->link
[idx
].mode
== vtn_access_mode_literal
);
376 unsigned member
= chain
->link
[idx
].id
;
377 offset
= nir_iadd(&b
->nb
, offset
,
378 nir_imm_int(&b
->nb
, type
->offsets
[member
]));
379 type
= type
->members
[member
];
384 unreachable("Invalid type for deref");
396 /* Tries to compute the size of an interface block based on the strides and
397 * offsets that are provided to us in the SPIR-V source.
400 vtn_type_block_size(struct vtn_type
*type
)
402 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
406 case GLSL_TYPE_UINT64
:
407 case GLSL_TYPE_INT64
:
408 case GLSL_TYPE_FLOAT
:
410 case GLSL_TYPE_DOUBLE
: {
411 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
412 glsl_get_matrix_columns(type
->type
);
414 assert(type
->stride
> 0);
415 return type
->stride
* cols
;
416 } else if (base_type
== GLSL_TYPE_DOUBLE
||
417 base_type
== GLSL_TYPE_UINT64
||
418 base_type
== GLSL_TYPE_INT64
) {
419 return glsl_get_vector_elements(type
->type
) * 8;
421 return glsl_get_vector_elements(type
->type
) * 4;
425 case GLSL_TYPE_STRUCT
:
426 case GLSL_TYPE_INTERFACE
: {
428 unsigned num_fields
= glsl_get_length(type
->type
);
429 for (unsigned f
= 0; f
< num_fields
; f
++) {
430 unsigned field_end
= type
->offsets
[f
] +
431 vtn_type_block_size(type
->members
[f
]);
432 size
= MAX2(size
, field_end
);
437 case GLSL_TYPE_ARRAY
:
438 assert(type
->stride
> 0);
439 assert(glsl_get_length(type
->type
) > 0);
440 return type
->stride
* glsl_get_length(type
->type
);
443 assert(!"Invalid block type");
449 vtn_access_chain_get_offset_size(struct vtn_access_chain
*chain
,
450 unsigned *access_offset
,
451 unsigned *access_size
)
453 /* Only valid for push constants accesses now. */
454 assert(chain
->var
->mode
== vtn_variable_mode_push_constant
);
456 struct vtn_type
*type
= chain
->var
->type
;
460 for (unsigned i
= 0; i
< chain
->length
; i
++) {
461 if (chain
->link
[i
].mode
!= vtn_access_mode_literal
)
464 if (glsl_type_is_struct(type
->type
)) {
465 *access_offset
+= type
->offsets
[chain
->link
[i
].id
];
466 type
= type
->members
[chain
->link
[i
].id
];
468 *access_offset
+= type
->stride
* chain
->link
[i
].id
;
469 type
= type
->array_element
;
473 *access_size
= vtn_type_block_size(type
);
477 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
478 nir_ssa_def
*index
, nir_ssa_def
*offset
,
479 unsigned access_offset
, unsigned access_size
,
480 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
)
482 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
483 instr
->num_components
= glsl_get_vector_elements(type
);
487 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
488 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
491 if (op
== nir_intrinsic_load_push_constant
) {
492 assert(access_offset
% 4 == 0);
494 nir_intrinsic_set_base(instr
, access_offset
);
495 nir_intrinsic_set_range(instr
, access_size
);
499 instr
->src
[src
++] = nir_src_for_ssa(index
);
501 if (op
== nir_intrinsic_load_push_constant
) {
502 /* We need to subtract the offset from where the intrinsic will load the
505 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
506 nir_imm_int(&b
->nb
, access_offset
)));
508 instr
->src
[src
++] = nir_src_for_ssa(offset
);
512 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
513 instr
->num_components
,
514 glsl_get_bit_size(type
), NULL
);
515 (*inout
)->def
= &instr
->dest
.ssa
;
518 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
520 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
521 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
525 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
526 nir_ssa_def
*index
, nir_ssa_def
*offset
,
527 unsigned access_offset
, unsigned access_size
,
528 struct vtn_access_chain
*chain
, unsigned chain_idx
,
529 struct vtn_type
*type
, struct vtn_ssa_value
**inout
)
531 if (chain
&& chain_idx
>= chain
->length
)
534 if (load
&& chain
== NULL
&& *inout
== NULL
)
535 *inout
= vtn_create_ssa_value(b
, type
->type
);
537 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
541 case GLSL_TYPE_UINT64
:
542 case GLSL_TYPE_INT64
:
543 case GLSL_TYPE_FLOAT
:
544 case GLSL_TYPE_DOUBLE
:
546 /* This is where things get interesting. At this point, we've hit
547 * a vector, a scalar, or a matrix.
549 if (glsl_type_is_matrix(type
->type
)) {
551 /* Loading the whole matrix */
552 struct vtn_ssa_value
*transpose
;
553 unsigned num_ops
, vec_width
;
554 if (type
->row_major
) {
555 num_ops
= glsl_get_vector_elements(type
->type
);
556 vec_width
= glsl_get_matrix_columns(type
->type
);
558 const struct glsl_type
*transpose_type
=
559 glsl_matrix_type(base_type
, vec_width
, num_ops
);
560 *inout
= vtn_create_ssa_value(b
, transpose_type
);
562 transpose
= vtn_ssa_transpose(b
, *inout
);
566 num_ops
= glsl_get_matrix_columns(type
->type
);
567 vec_width
= glsl_get_vector_elements(type
->type
);
570 for (unsigned i
= 0; i
< num_ops
; i
++) {
571 nir_ssa_def
*elem_offset
=
572 nir_iadd(&b
->nb
, offset
,
573 nir_imm_int(&b
->nb
, i
* type
->stride
));
574 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
575 access_offset
, access_size
,
577 glsl_vector_type(base_type
, vec_width
));
580 if (load
&& type
->row_major
)
581 *inout
= vtn_ssa_transpose(b
, *inout
);
582 } else if (type
->row_major
) {
583 /* Row-major but with an access chiain. */
584 nir_ssa_def
*col_offset
=
585 vtn_access_link_as_ssa(b
, chain
->link
[chain_idx
],
586 type
->array_element
->stride
);
587 offset
= nir_iadd(&b
->nb
, offset
, col_offset
);
589 if (chain_idx
+ 1 < chain
->length
) {
590 /* Picking off a single element */
591 nir_ssa_def
*row_offset
=
592 vtn_access_link_as_ssa(b
, chain
->link
[chain_idx
+ 1],
594 offset
= nir_iadd(&b
->nb
, offset
, row_offset
);
596 *inout
= vtn_create_ssa_value(b
, glsl_scalar_type(base_type
));
597 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
598 access_offset
, access_size
,
599 inout
, glsl_scalar_type(base_type
));
601 /* Grabbing a column; picking one element off each row */
602 unsigned num_comps
= glsl_get_vector_elements(type
->type
);
603 const struct glsl_type
*column_type
=
604 glsl_get_column_type(type
->type
);
606 nir_ssa_def
*comps
[4];
607 for (unsigned i
= 0; i
< num_comps
; i
++) {
608 nir_ssa_def
*elem_offset
=
609 nir_iadd(&b
->nb
, offset
,
610 nir_imm_int(&b
->nb
, i
* type
->stride
));
612 struct vtn_ssa_value
*comp
, temp_val
;
614 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
615 temp_val
.type
= glsl_scalar_type(base_type
);
618 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
619 access_offset
, access_size
,
620 &comp
, glsl_scalar_type(base_type
));
621 comps
[i
] = comp
->def
;
626 *inout
= vtn_create_ssa_value(b
, column_type
);
628 (*inout
)->def
= nir_vec(&b
->nb
, comps
, num_comps
);
632 /* Column-major with a deref. Fall through to array case. */
633 nir_ssa_def
*col_offset
=
634 vtn_access_link_as_ssa(b
, chain
->link
[chain_idx
], type
->stride
);
635 offset
= nir_iadd(&b
->nb
, offset
, col_offset
);
637 _vtn_block_load_store(b
, op
, load
, index
, offset
,
638 access_offset
, access_size
,
639 chain
, chain_idx
+ 1,
640 type
->array_element
, inout
);
642 } else if (chain
== NULL
) {
643 /* Single whole vector */
644 assert(glsl_type_is_vector_or_scalar(type
->type
));
645 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
646 access_offset
, access_size
,
649 /* Single component of a vector. Fall through to array case. */
650 nir_ssa_def
*elem_offset
=
651 vtn_access_link_as_ssa(b
, chain
->link
[chain_idx
], type
->stride
);
652 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
654 _vtn_block_load_store(b
, op
, load
, index
, offset
,
655 access_offset
, access_size
,
657 type
->array_element
, inout
);
661 case GLSL_TYPE_ARRAY
: {
662 unsigned elems
= glsl_get_length(type
->type
);
663 for (unsigned i
= 0; i
< elems
; i
++) {
664 nir_ssa_def
*elem_off
=
665 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* type
->stride
));
666 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
667 access_offset
, access_size
,
669 type
->array_element
, &(*inout
)->elems
[i
]);
674 case GLSL_TYPE_STRUCT
: {
675 unsigned elems
= glsl_get_length(type
->type
);
676 for (unsigned i
= 0; i
< elems
; i
++) {
677 nir_ssa_def
*elem_off
=
678 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, type
->offsets
[i
]));
679 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
680 access_offset
, access_size
,
682 type
->members
[i
], &(*inout
)->elems
[i
]);
688 unreachable("Invalid block member type");
692 static struct vtn_ssa_value
*
693 vtn_block_load(struct vtn_builder
*b
, struct vtn_access_chain
*src
)
696 unsigned access_offset
= 0, access_size
= 0;
697 switch (src
->var
->mode
) {
698 case vtn_variable_mode_ubo
:
699 op
= nir_intrinsic_load_ubo
;
701 case vtn_variable_mode_ssbo
:
702 op
= nir_intrinsic_load_ssbo
;
704 case vtn_variable_mode_push_constant
:
705 op
= nir_intrinsic_load_push_constant
;
706 vtn_access_chain_get_offset_size(src
, &access_offset
, &access_size
);
709 assert(!"Invalid block variable mode");
712 nir_ssa_def
*offset
, *index
= NULL
;
713 struct vtn_type
*type
;
715 offset
= vtn_access_chain_to_offset(b
, src
, &index
, &type
, &chain_idx
, true);
717 struct vtn_ssa_value
*value
= NULL
;
718 _vtn_block_load_store(b
, op
, true, index
, offset
,
719 access_offset
, access_size
,
720 src
, chain_idx
, type
, &value
);
725 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
726 struct vtn_access_chain
*dst
)
728 nir_ssa_def
*offset
, *index
= NULL
;
729 struct vtn_type
*type
;
731 offset
= vtn_access_chain_to_offset(b
, dst
, &index
, &type
, &chain_idx
, true);
733 _vtn_block_load_store(b
, nir_intrinsic_store_ssbo
, false, index
, offset
,
734 0, 0, dst
, chain_idx
, type
, &src
);
738 vtn_variable_is_external_block(struct vtn_variable
*var
)
740 return var
->mode
== vtn_variable_mode_ssbo
||
741 var
->mode
== vtn_variable_mode_ubo
||
742 var
->mode
== vtn_variable_mode_push_constant
;
746 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
747 struct vtn_access_chain
*chain
,
748 struct vtn_type
*tail_type
,
749 struct vtn_ssa_value
**inout
)
751 enum glsl_base_type base_type
= glsl_get_base_type(tail_type
->type
);
755 case GLSL_TYPE_UINT64
:
756 case GLSL_TYPE_INT64
:
757 case GLSL_TYPE_FLOAT
:
759 case GLSL_TYPE_DOUBLE
:
760 /* At this point, we have a scalar, vector, or matrix so we know that
761 * there cannot be any structure splitting still in the way. By
762 * stopping at the matrix level rather than the vector level, we
763 * ensure that matrices get loaded in the optimal way even if they
764 * are storred row-major in a UBO.
767 *inout
= vtn_local_load(b
, vtn_access_chain_to_deref(b
, chain
));
769 vtn_local_store(b
, *inout
, vtn_access_chain_to_deref(b
, chain
));
773 case GLSL_TYPE_ARRAY
:
774 case GLSL_TYPE_STRUCT
: {
775 struct vtn_access_chain
*new_chain
=
776 vtn_access_chain_extend(b
, chain
, 1);
777 new_chain
->link
[chain
->length
].mode
= vtn_access_mode_literal
;
778 unsigned elems
= glsl_get_length(tail_type
->type
);
780 assert(*inout
== NULL
);
781 *inout
= rzalloc(b
, struct vtn_ssa_value
);
782 (*inout
)->type
= tail_type
->type
;
783 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
785 for (unsigned i
= 0; i
< elems
; i
++) {
786 new_chain
->link
[chain
->length
].id
= i
;
787 struct vtn_type
*elem_type
= base_type
== GLSL_TYPE_ARRAY
?
788 tail_type
->array_element
: tail_type
->members
[i
];
789 _vtn_variable_load_store(b
, load
, new_chain
, elem_type
,
790 &(*inout
)->elems
[i
]);
796 unreachable("Invalid access chain type");
800 struct vtn_ssa_value
*
801 vtn_variable_load(struct vtn_builder
*b
, struct vtn_access_chain
*src
)
803 if (vtn_variable_is_external_block(src
->var
)) {
804 return vtn_block_load(b
, src
);
806 struct vtn_type
*tail_type
= vtn_access_chain_tail_type(b
, src
);
807 struct vtn_ssa_value
*val
= NULL
;
808 _vtn_variable_load_store(b
, true, src
, tail_type
, &val
);
814 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
815 struct vtn_access_chain
*dest
)
817 if (vtn_variable_is_external_block(dest
->var
)) {
818 assert(dest
->var
->mode
== vtn_variable_mode_ssbo
);
819 vtn_block_store(b
, src
, dest
);
821 struct vtn_type
*tail_type
= vtn_access_chain_tail_type(b
, dest
);
822 _vtn_variable_load_store(b
, false, dest
, tail_type
, &src
);
827 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_access_chain
*dest
,
828 struct vtn_access_chain
*src
, struct vtn_type
*tail_type
)
830 enum glsl_base_type base_type
= glsl_get_base_type(tail_type
->type
);
834 case GLSL_TYPE_UINT64
:
835 case GLSL_TYPE_INT64
:
836 case GLSL_TYPE_FLOAT
:
837 case GLSL_TYPE_DOUBLE
:
839 /* At this point, we have a scalar, vector, or matrix so we know that
840 * there cannot be any structure splitting still in the way. By
841 * stopping at the matrix level rather than the vector level, we
842 * ensure that matrices get loaded in the optimal way even if they
843 * are storred row-major in a UBO.
845 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
848 case GLSL_TYPE_ARRAY
:
849 case GLSL_TYPE_STRUCT
: {
850 struct vtn_access_chain
*new_src
, *new_dest
;
851 new_src
= vtn_access_chain_extend(b
, src
, 1);
852 new_dest
= vtn_access_chain_extend(b
, dest
, 1);
853 new_src
->link
[src
->length
].mode
= vtn_access_mode_literal
;
854 new_dest
->link
[dest
->length
].mode
= vtn_access_mode_literal
;
855 unsigned elems
= glsl_get_length(tail_type
->type
);
856 for (unsigned i
= 0; i
< elems
; i
++) {
857 new_src
->link
[src
->length
].id
= i
;
858 new_dest
->link
[dest
->length
].id
= i
;
859 struct vtn_type
*elem_type
= base_type
== GLSL_TYPE_ARRAY
?
860 tail_type
->array_element
: tail_type
->members
[i
];
861 _vtn_variable_copy(b
, new_dest
, new_src
, elem_type
);
867 unreachable("Invalid access chain type");
872 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_access_chain
*dest
,
873 struct vtn_access_chain
*src
)
875 struct vtn_type
*tail_type
= vtn_access_chain_tail_type(b
, src
);
876 assert(vtn_access_chain_tail_type(b
, dest
)->type
== tail_type
->type
);
878 /* TODO: At some point, we should add a special-case for when we can
879 * just emit a copy_var intrinsic.
881 _vtn_variable_copy(b
, dest
, src
, tail_type
);
885 set_mode_system_value(nir_variable_mode
*mode
)
887 assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
888 *mode
= nir_var_system_value
;
892 vtn_get_builtin_location(struct vtn_builder
*b
,
893 SpvBuiltIn builtin
, int *location
,
894 nir_variable_mode
*mode
)
897 case SpvBuiltInPosition
:
898 *location
= VARYING_SLOT_POS
;
900 case SpvBuiltInPointSize
:
901 *location
= VARYING_SLOT_PSIZ
;
903 case SpvBuiltInClipDistance
:
904 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
906 case SpvBuiltInCullDistance
:
907 *location
= VARYING_SLOT_CULL_DIST0
;
909 case SpvBuiltInVertexIndex
:
910 *location
= SYSTEM_VALUE_VERTEX_ID
;
911 set_mode_system_value(mode
);
913 case SpvBuiltInVertexId
:
914 /* Vulkan defines VertexID to be zero-based and reserves the new
915 * builtin keyword VertexIndex to indicate the non-zero-based value.
917 *location
= SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
918 set_mode_system_value(mode
);
920 case SpvBuiltInInstanceIndex
:
921 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
922 set_mode_system_value(mode
);
924 case SpvBuiltInInstanceId
:
925 *location
= SYSTEM_VALUE_INSTANCE_ID
;
926 set_mode_system_value(mode
);
928 case SpvBuiltInPrimitiveId
:
929 if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
) {
930 assert(*mode
== nir_var_shader_in
);
931 *location
= VARYING_SLOT_PRIMITIVE_ID
;
932 } else if (*mode
== nir_var_shader_out
) {
933 *location
= VARYING_SLOT_PRIMITIVE_ID
;
935 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
936 set_mode_system_value(mode
);
939 case SpvBuiltInInvocationId
:
940 *location
= SYSTEM_VALUE_INVOCATION_ID
;
941 set_mode_system_value(mode
);
943 case SpvBuiltInLayer
:
944 *location
= VARYING_SLOT_LAYER
;
945 if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
)
946 *mode
= nir_var_shader_in
;
947 else if (b
->shader
->stage
== MESA_SHADER_GEOMETRY
)
948 *mode
= nir_var_shader_out
;
950 unreachable("invalid stage for SpvBuiltInLayer");
952 case SpvBuiltInViewportIndex
:
953 *location
= VARYING_SLOT_VIEWPORT
;
954 if (b
->shader
->stage
== MESA_SHADER_GEOMETRY
)
955 *mode
= nir_var_shader_out
;
956 else if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
)
957 *mode
= nir_var_shader_in
;
959 unreachable("invalid stage for SpvBuiltInViewportIndex");
961 case SpvBuiltInTessLevelOuter
:
962 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
964 case SpvBuiltInTessLevelInner
:
965 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
967 case SpvBuiltInTessCoord
:
968 *location
= SYSTEM_VALUE_TESS_COORD
;
969 set_mode_system_value(mode
);
971 case SpvBuiltInPatchVertices
:
972 *location
= SYSTEM_VALUE_VERTICES_IN
;
973 set_mode_system_value(mode
);
975 case SpvBuiltInFragCoord
:
976 *location
= VARYING_SLOT_POS
;
977 assert(*mode
== nir_var_shader_in
);
979 case SpvBuiltInPointCoord
:
980 *location
= VARYING_SLOT_PNTC
;
981 assert(*mode
== nir_var_shader_in
);
983 case SpvBuiltInFrontFacing
:
984 *location
= SYSTEM_VALUE_FRONT_FACE
;
985 set_mode_system_value(mode
);
987 case SpvBuiltInSampleId
:
988 *location
= SYSTEM_VALUE_SAMPLE_ID
;
989 set_mode_system_value(mode
);
991 case SpvBuiltInSamplePosition
:
992 *location
= SYSTEM_VALUE_SAMPLE_POS
;
993 set_mode_system_value(mode
);
995 case SpvBuiltInSampleMask
:
996 if (*mode
== nir_var_shader_out
) {
997 *location
= FRAG_RESULT_SAMPLE_MASK
;
999 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1000 set_mode_system_value(mode
);
1003 case SpvBuiltInFragDepth
:
1004 *location
= FRAG_RESULT_DEPTH
;
1005 assert(*mode
== nir_var_shader_out
);
1007 case SpvBuiltInNumWorkgroups
:
1008 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1009 set_mode_system_value(mode
);
1011 case SpvBuiltInWorkgroupSize
:
1012 /* This should already be handled */
1013 unreachable("unsupported builtin");
1015 case SpvBuiltInWorkgroupId
:
1016 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1017 set_mode_system_value(mode
);
1019 case SpvBuiltInLocalInvocationId
:
1020 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1021 set_mode_system_value(mode
);
1023 case SpvBuiltInLocalInvocationIndex
:
1024 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1025 set_mode_system_value(mode
);
1027 case SpvBuiltInGlobalInvocationId
:
1028 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1029 set_mode_system_value(mode
);
1031 case SpvBuiltInBaseVertex
:
1032 *location
= SYSTEM_VALUE_BASE_VERTEX
;
1033 set_mode_system_value(mode
);
1035 case SpvBuiltInBaseInstance
:
1036 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1037 set_mode_system_value(mode
);
1039 case SpvBuiltInDrawIndex
:
1040 *location
= SYSTEM_VALUE_DRAW_ID
;
1041 set_mode_system_value(mode
);
1043 case SpvBuiltInViewIndex
:
1044 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1045 set_mode_system_value(mode
);
1047 case SpvBuiltInHelperInvocation
:
1049 unreachable("unsupported builtin");
1054 apply_var_decoration(struct vtn_builder
*b
, nir_variable
*nir_var
,
1055 const struct vtn_decoration
*dec
)
1057 switch (dec
->decoration
) {
1058 case SpvDecorationRelaxedPrecision
:
1059 break; /* FIXME: Do nothing with this for now. */
1060 case SpvDecorationNoPerspective
:
1061 nir_var
->data
.interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1063 case SpvDecorationFlat
:
1064 nir_var
->data
.interpolation
= INTERP_MODE_FLAT
;
1066 case SpvDecorationCentroid
:
1067 nir_var
->data
.centroid
= true;
1069 case SpvDecorationSample
:
1070 nir_var
->data
.sample
= true;
1072 case SpvDecorationInvariant
:
1073 nir_var
->data
.invariant
= true;
1075 case SpvDecorationConstant
:
1076 assert(nir_var
->constant_initializer
!= NULL
);
1077 nir_var
->data
.read_only
= true;
1079 case SpvDecorationNonReadable
:
1080 nir_var
->data
.image
.write_only
= true;
1082 case SpvDecorationNonWritable
:
1083 nir_var
->data
.read_only
= true;
1084 nir_var
->data
.image
.read_only
= true;
1086 case SpvDecorationComponent
:
1087 nir_var
->data
.location_frac
= dec
->literals
[0];
1089 case SpvDecorationIndex
:
1090 nir_var
->data
.index
= dec
->literals
[0];
1092 case SpvDecorationBuiltIn
: {
1093 SpvBuiltIn builtin
= dec
->literals
[0];
1095 if (builtin
== SpvBuiltInWorkgroupSize
) {
1096 /* This shouldn't be a builtin. It's actually a constant. */
1097 nir_var
->data
.mode
= nir_var_global
;
1098 nir_var
->data
.read_only
= true;
1100 nir_constant
*c
= rzalloc(nir_var
, nir_constant
);
1101 c
->values
[0].u32
[0] = b
->shader
->info
.cs
.local_size
[0];
1102 c
->values
[0].u32
[1] = b
->shader
->info
.cs
.local_size
[1];
1103 c
->values
[0].u32
[2] = b
->shader
->info
.cs
.local_size
[2];
1104 nir_var
->constant_initializer
= c
;
1108 nir_variable_mode mode
= nir_var
->data
.mode
;
1109 vtn_get_builtin_location(b
, builtin
, &nir_var
->data
.location
, &mode
);
1110 nir_var
->data
.mode
= mode
;
1113 case SpvBuiltInTessLevelOuter
:
1114 case SpvBuiltInTessLevelInner
:
1115 nir_var
->data
.compact
= true;
1117 case SpvBuiltInSamplePosition
:
1118 nir_var
->data
.origin_upper_left
= b
->origin_upper_left
;
1120 case SpvBuiltInFragCoord
:
1121 nir_var
->data
.pixel_center_integer
= b
->pixel_center_integer
;
1128 case SpvDecorationSpecId
:
1129 case SpvDecorationRowMajor
:
1130 case SpvDecorationColMajor
:
1131 case SpvDecorationMatrixStride
:
1132 case SpvDecorationRestrict
:
1133 case SpvDecorationAliased
:
1134 case SpvDecorationVolatile
:
1135 case SpvDecorationCoherent
:
1136 case SpvDecorationUniform
:
1137 case SpvDecorationStream
:
1138 case SpvDecorationOffset
:
1139 case SpvDecorationLinkageAttributes
:
1140 break; /* Do nothing with these here */
1142 case SpvDecorationPatch
:
1143 nir_var
->data
.patch
= true;
1146 case SpvDecorationLocation
:
1147 unreachable("Handled above");
1149 case SpvDecorationBlock
:
1150 case SpvDecorationBufferBlock
:
1151 case SpvDecorationArrayStride
:
1152 case SpvDecorationGLSLShared
:
1153 case SpvDecorationGLSLPacked
:
1154 break; /* These can apply to a type but we don't care about them */
1156 case SpvDecorationBinding
:
1157 case SpvDecorationDescriptorSet
:
1158 case SpvDecorationNoContraction
:
1159 case SpvDecorationInputAttachmentIndex
:
1160 vtn_warn("Decoration not allowed for variable or structure member: %s",
1161 spirv_decoration_to_string(dec
->decoration
));
1164 case SpvDecorationXfbBuffer
:
1165 case SpvDecorationXfbStride
:
1166 vtn_warn("Vulkan does not have transform feedback: %s",
1167 spirv_decoration_to_string(dec
->decoration
));
1170 case SpvDecorationCPacked
:
1171 case SpvDecorationSaturatedConversion
:
1172 case SpvDecorationFuncParamAttr
:
1173 case SpvDecorationFPRoundingMode
:
1174 case SpvDecorationFPFastMathMode
:
1175 case SpvDecorationAlignment
:
1176 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1177 spirv_decoration_to_string(dec
->decoration
));
1181 unreachable("Unhandled decoration");
1186 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1187 const struct vtn_decoration
*dec
, void *out_is_patch
)
1189 if (dec
->decoration
== SpvDecorationPatch
) {
1190 *((bool *) out_is_patch
) = true;
1195 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1196 const struct vtn_decoration
*dec
, void *void_var
)
1198 struct vtn_variable
*vtn_var
= void_var
;
1200 /* Handle decorations that apply to a vtn_variable as a whole */
1201 switch (dec
->decoration
) {
1202 case SpvDecorationBinding
:
1203 vtn_var
->binding
= dec
->literals
[0];
1205 case SpvDecorationDescriptorSet
:
1206 vtn_var
->descriptor_set
= dec
->literals
[0];
1208 case SpvDecorationInputAttachmentIndex
:
1209 vtn_var
->input_attachment_index
= dec
->literals
[0];
1211 case SpvDecorationPatch
:
1212 vtn_var
->patch
= true;
1218 if (val
->value_type
== vtn_value_type_access_chain
) {
1219 assert(val
->access_chain
->length
== 0);
1220 assert(val
->access_chain
->var
== void_var
);
1221 assert(member
== -1);
1223 assert(val
->value_type
== vtn_value_type_type
);
1226 /* Location is odd. If applied to a split structure, we have to walk the
1227 * whole thing and accumulate the location. It's easier to handle as a
1230 if (dec
->decoration
== SpvDecorationLocation
) {
1231 unsigned location
= dec
->literals
[0];
1232 bool is_vertex_input
;
1233 if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
&&
1234 vtn_var
->mode
== vtn_variable_mode_output
) {
1235 is_vertex_input
= false;
1236 location
+= FRAG_RESULT_DATA0
;
1237 } else if (b
->shader
->stage
== MESA_SHADER_VERTEX
&&
1238 vtn_var
->mode
== vtn_variable_mode_input
) {
1239 is_vertex_input
= true;
1240 location
+= VERT_ATTRIB_GENERIC0
;
1241 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1242 vtn_var
->mode
== vtn_variable_mode_output
) {
1243 is_vertex_input
= false;
1244 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1246 vtn_warn("Location must be on input or output variable");
1251 /* This handles the member and lone variable cases */
1252 vtn_var
->var
->data
.location
= location
;
1254 /* This handles the structure member case */
1255 assert(vtn_var
->members
);
1257 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1258 for (unsigned i
= 0; i
< length
; i
++) {
1259 vtn_var
->members
[i
]->data
.location
= location
;
1261 glsl_count_attribute_slots(vtn_var
->members
[i
]->interface_type
,
1268 assert(member
<= 0);
1269 apply_var_decoration(b
, vtn_var
->var
, dec
);
1270 } else if (vtn_var
->members
) {
1272 assert(vtn_var
->members
);
1273 apply_var_decoration(b
, vtn_var
->members
[member
], dec
);
1276 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1277 for (unsigned i
= 0; i
< length
; i
++)
1278 apply_var_decoration(b
, vtn_var
->members
[i
], dec
);
1281 /* A few variables, those with external storage, have no actual
1282 * nir_variables associated with them. Fortunately, all decorations
1283 * we care about for those variables are on the type only.
1285 assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1286 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1287 vtn_var
->mode
== vtn_variable_mode_push_constant
);
1292 static enum vtn_variable_mode
1293 vtn_storage_class_to_mode(SpvStorageClass
class,
1294 struct vtn_type
*interface_type
,
1295 nir_variable_mode
*nir_mode_out
)
1297 enum vtn_variable_mode mode
;
1298 nir_variable_mode nir_mode
;
1300 case SpvStorageClassUniform
:
1301 if (interface_type
->block
) {
1302 mode
= vtn_variable_mode_ubo
;
1304 } else if (interface_type
->buffer_block
) {
1305 mode
= vtn_variable_mode_ssbo
;
1308 assert(!"Invalid uniform variable type");
1311 case SpvStorageClassUniformConstant
:
1312 if (glsl_type_is_image(interface_type
->type
)) {
1313 mode
= vtn_variable_mode_image
;
1314 nir_mode
= nir_var_uniform
;
1315 } else if (glsl_type_is_sampler(interface_type
->type
)) {
1316 mode
= vtn_variable_mode_sampler
;
1317 nir_mode
= nir_var_uniform
;
1319 assert(!"Invalid uniform constant variable type");
1322 case SpvStorageClassPushConstant
:
1323 mode
= vtn_variable_mode_push_constant
;
1324 nir_mode
= nir_var_uniform
;
1326 case SpvStorageClassInput
:
1327 mode
= vtn_variable_mode_input
;
1328 nir_mode
= nir_var_shader_in
;
1330 case SpvStorageClassOutput
:
1331 mode
= vtn_variable_mode_output
;
1332 nir_mode
= nir_var_shader_out
;
1334 case SpvStorageClassPrivate
:
1335 mode
= vtn_variable_mode_global
;
1336 nir_mode
= nir_var_global
;
1338 case SpvStorageClassFunction
:
1339 mode
= vtn_variable_mode_local
;
1340 nir_mode
= nir_var_local
;
1342 case SpvStorageClassWorkgroup
:
1343 mode
= vtn_variable_mode_workgroup
;
1344 nir_mode
= nir_var_shared
;
1346 case SpvStorageClassCrossWorkgroup
:
1347 case SpvStorageClassGeneric
:
1348 case SpvStorageClassAtomicCounter
:
1350 unreachable("Unhandled variable storage class");
1354 *nir_mode_out
= nir_mode
;
1360 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
1362 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
1365 if (var
->mode
== vtn_variable_mode_input
) {
1366 return stage
== MESA_SHADER_TESS_CTRL
||
1367 stage
== MESA_SHADER_TESS_EVAL
||
1368 stage
== MESA_SHADER_GEOMETRY
;
1371 if (var
->mode
== vtn_variable_mode_output
)
1372 return stage
== MESA_SHADER_TESS_CTRL
;
1378 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
1379 const uint32_t *w
, unsigned count
)
1383 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1384 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1388 case SpvOpVariable
: {
1389 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
1390 var
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1392 var
->chain
.var
= var
;
1393 var
->chain
.length
= 0;
1395 struct vtn_value
*val
=
1396 vtn_push_value(b
, w
[2], vtn_value_type_access_chain
);
1397 val
->access_chain
= &var
->chain
;
1399 struct vtn_type
*without_array
= var
->type
;
1400 while(glsl_type_is_array(without_array
->type
))
1401 without_array
= without_array
->array_element
;
1403 nir_variable_mode nir_mode
;
1404 var
->mode
= vtn_storage_class_to_mode(w
[3], without_array
, &nir_mode
);
1406 switch (var
->mode
) {
1407 case vtn_variable_mode_ubo
:
1408 b
->shader
->info
.num_ubos
++;
1410 case vtn_variable_mode_ssbo
:
1411 b
->shader
->info
.num_ssbos
++;
1413 case vtn_variable_mode_image
:
1414 b
->shader
->info
.num_images
++;
1416 case vtn_variable_mode_sampler
:
1417 b
->shader
->info
.num_textures
++;
1419 case vtn_variable_mode_push_constant
:
1420 b
->shader
->num_uniforms
= vtn_type_block_size(var
->type
);
1423 /* No tallying is needed */
1427 switch (var
->mode
) {
1428 case vtn_variable_mode_local
:
1429 case vtn_variable_mode_global
:
1430 case vtn_variable_mode_image
:
1431 case vtn_variable_mode_sampler
:
1432 case vtn_variable_mode_workgroup
:
1433 /* For these, we create the variable normally */
1434 var
->var
= rzalloc(b
->shader
, nir_variable
);
1435 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1436 var
->var
->type
= var
->type
->type
;
1437 var
->var
->data
.mode
= nir_mode
;
1439 switch (var
->mode
) {
1440 case vtn_variable_mode_image
:
1441 case vtn_variable_mode_sampler
:
1442 var
->var
->interface_type
= without_array
->type
;
1445 var
->var
->interface_type
= NULL
;
1450 case vtn_variable_mode_input
:
1451 case vtn_variable_mode_output
: {
1452 /* In order to know whether or not we're a per-vertex inout, we need
1453 * the patch qualifier. This means walking the variable decorations
1454 * early before we actually create any variables. Not a big deal.
1456 * GLSLang really likes to place decorations in the most interior
1457 * thing it possibly can. In particular, if you have a struct, it
1458 * will place the patch decorations on the struct members. This
1459 * should be handled by the variable splitting below just fine.
1461 * If you have an array-of-struct, things get even more weird as it
1462 * will place the patch decorations on the struct even though it's
1463 * inside an array and some of the members being patch and others not
1464 * makes no sense whatsoever. Since the only sensible thing is for
1465 * it to be all or nothing, we'll call it patch if any of the members
1466 * are declared patch.
1469 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
1470 if (glsl_type_is_array(var
->type
->type
) &&
1471 glsl_type_is_struct(without_array
->type
)) {
1472 vtn_foreach_decoration(b
, without_array
->val
,
1473 var_is_patch_cb
, &var
->patch
);
1476 /* For inputs and outputs, we immediately split structures. This
1477 * is for a couple of reasons. For one, builtins may all come in
1478 * a struct and we really want those split out into separate
1479 * variables. For another, interpolation qualifiers can be
1480 * applied to members of the top-level struct ane we need to be
1481 * able to preserve that information.
1484 int array_length
= -1;
1485 struct vtn_type
*interface_type
= var
->type
;
1486 if (is_per_vertex_inout(var
, b
->shader
->stage
)) {
1487 /* In Geometry shaders (and some tessellation), inputs come
1488 * in per-vertex arrays. However, some builtins come in
1489 * non-per-vertex, hence the need for the is_array check. In
1490 * any case, there are no non-builtin arrays allowed so this
1491 * check should be sufficient.
1493 interface_type
= var
->type
->array_element
;
1494 array_length
= glsl_get_length(var
->type
->type
);
1497 if (glsl_type_is_struct(interface_type
->type
)) {
1498 /* It's a struct. Split it. */
1499 unsigned num_members
= glsl_get_length(interface_type
->type
);
1500 var
->members
= ralloc_array(b
, nir_variable
*, num_members
);
1502 for (unsigned i
= 0; i
< num_members
; i
++) {
1503 const struct glsl_type
*mtype
= interface_type
->members
[i
]->type
;
1504 if (array_length
>= 0)
1505 mtype
= glsl_array_type(mtype
, array_length
);
1507 var
->members
[i
] = rzalloc(b
->shader
, nir_variable
);
1508 var
->members
[i
]->name
=
1509 ralloc_asprintf(var
->members
[i
], "%s.%d", val
->name
, i
);
1510 var
->members
[i
]->type
= mtype
;
1511 var
->members
[i
]->interface_type
=
1512 interface_type
->members
[i
]->type
;
1513 var
->members
[i
]->data
.mode
= nir_mode
;
1514 var
->members
[i
]->data
.patch
= var
->patch
;
1517 var
->var
= rzalloc(b
->shader
, nir_variable
);
1518 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1519 var
->var
->type
= var
->type
->type
;
1520 var
->var
->interface_type
= interface_type
->type
;
1521 var
->var
->data
.mode
= nir_mode
;
1522 var
->var
->data
.patch
= var
->patch
;
1525 /* For inputs and outputs, we need to grab locations and builtin
1526 * information from the interface type.
1528 vtn_foreach_decoration(b
, interface_type
->val
, var_decoration_cb
, var
);
1532 case vtn_variable_mode_param
:
1533 unreachable("Not created through OpVariable");
1535 case vtn_variable_mode_ubo
:
1536 case vtn_variable_mode_ssbo
:
1537 case vtn_variable_mode_push_constant
:
1538 /* These don't need actual variables. */
1544 nir_constant
*constant
=
1545 vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
1546 var
->var
->constant_initializer
=
1547 nir_constant_clone(constant
, var
->var
);
1550 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
1552 if (var
->mode
== vtn_variable_mode_image
||
1553 var
->mode
== vtn_variable_mode_sampler
) {
1554 /* XXX: We still need the binding information in the nir_variable
1555 * for these. We should fix that.
1557 var
->var
->data
.binding
= var
->binding
;
1558 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
1559 var
->var
->data
.index
= var
->input_attachment_index
;
1561 if (var
->mode
== vtn_variable_mode_image
)
1562 var
->var
->data
.image
.format
= without_array
->image_format
;
1565 if (var
->mode
== vtn_variable_mode_local
) {
1566 assert(var
->members
== NULL
&& var
->var
!= NULL
);
1567 nir_function_impl_add_variable(b
->impl
, var
->var
);
1568 } else if (var
->var
) {
1569 nir_shader_add_variable(b
->shader
, var
->var
);
1570 } else if (var
->members
) {
1571 unsigned count
= glsl_get_length(without_array
->type
);
1572 for (unsigned i
= 0; i
< count
; i
++) {
1573 assert(var
->members
[i
]->data
.mode
!= nir_var_local
);
1574 nir_shader_add_variable(b
->shader
, var
->members
[i
]);
1577 assert(var
->mode
== vtn_variable_mode_ubo
||
1578 var
->mode
== vtn_variable_mode_ssbo
||
1579 var
->mode
== vtn_variable_mode_push_constant
);
1584 case SpvOpAccessChain
:
1585 case SpvOpInBoundsAccessChain
: {
1586 struct vtn_access_chain
*base
, *chain
;
1587 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
1588 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1589 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1590 * to combine an array of images with a single sampler to get an
1591 * array of sampled images that all share the same sampler.
1592 * Fortunately, this means that we can more-or-less ignore the
1593 * sampler when crawling the access chain, but it does leave us
1594 * with this rather awkward little special-case.
1596 base
= base_val
->sampled_image
->image
;
1598 assert(base_val
->value_type
== vtn_value_type_access_chain
);
1599 base
= base_val
->access_chain
;
1602 chain
= vtn_access_chain_extend(b
, base
, count
- 4);
1604 unsigned idx
= base
->length
;
1605 for (int i
= 4; i
< count
; i
++) {
1606 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
1607 if (link_val
->value_type
== vtn_value_type_constant
) {
1608 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
1609 chain
->link
[idx
].id
= link_val
->constant
->values
[0].u32
[0];
1611 chain
->link
[idx
].mode
= vtn_access_mode_id
;
1612 chain
->link
[idx
].id
= w
[i
];
1617 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1618 struct vtn_value
*val
=
1619 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1620 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1621 val
->sampled_image
->image
= chain
;
1622 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
1624 struct vtn_value
*val
=
1625 vtn_push_value(b
, w
[2], vtn_value_type_access_chain
);
1626 val
->access_chain
= chain
;
1631 case SpvOpCopyMemory
: {
1632 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_access_chain
);
1633 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_access_chain
);
1635 vtn_variable_copy(b
, dest
->access_chain
, src
->access_chain
);
1640 struct vtn_access_chain
*src
=
1641 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1643 if (src
->var
->mode
== vtn_variable_mode_image
||
1644 src
->var
->mode
== vtn_variable_mode_sampler
) {
1645 vtn_push_value(b
, w
[2], vtn_value_type_access_chain
)->access_chain
= src
;
1649 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1650 val
->ssa
= vtn_variable_load(b
, src
);
1655 struct vtn_access_chain
*dest
=
1656 vtn_value(b
, w
[1], vtn_value_type_access_chain
)->access_chain
;
1658 if (glsl_type_is_sampler(dest
->var
->type
->type
)) {
1659 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
1660 "propagation to workaround the problem.");
1661 assert(dest
->var
->copy_prop_sampler
== NULL
);
1662 dest
->var
->copy_prop_sampler
=
1663 vtn_value(b
, w
[2], vtn_value_type_access_chain
)->access_chain
;
1667 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
1668 vtn_variable_store(b
, src
, dest
);
1672 case SpvOpArrayLength
: {
1673 struct vtn_access_chain
*chain
=
1674 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1676 const uint32_t offset
= chain
->var
->type
->offsets
[w
[4]];
1677 const uint32_t stride
= chain
->var
->type
->members
[w
[4]]->stride
;
1680 struct vtn_type
*type
;
1681 nir_ssa_def
*index
=
1682 get_vulkan_resource_index(b
, chain
, &type
, &chain_idx
);
1684 nir_intrinsic_instr
*instr
=
1685 nir_intrinsic_instr_create(b
->nb
.shader
,
1686 nir_intrinsic_get_buffer_size
);
1687 instr
->src
[0] = nir_src_for_ssa(index
);
1688 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
1689 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
1690 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
1692 /* array_length = max(buffer_size - offset, 0) / stride */
1693 nir_ssa_def
*array_length
=
1698 nir_imm_int(&b
->nb
, offset
)),
1699 nir_imm_int(&b
->nb
, 0u)),
1700 nir_imm_int(&b
->nb
, stride
));
1702 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1703 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
1704 val
->ssa
->def
= array_length
;
1708 case SpvOpCopyMemorySized
:
1710 unreachable("Unhandled opcode");