2 * Copyright © 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Jason Ekstrand (jason@jlekstrand.net)
28 #include "vtn_private.h"
29 #include "spirv_info.h"
31 static struct vtn_access_chain
*
32 vtn_access_chain_create(struct vtn_builder
*b
, unsigned length
)
34 struct vtn_access_chain
*chain
;
36 /* Subtract 1 from the length since there's already one built in */
37 size_t size
= sizeof(*chain
) +
38 (MAX2(length
, 1) - 1) * sizeof(chain
->link
[0]);
39 chain
= rzalloc_size(b
, size
);
40 chain
->length
= length
;
45 static struct vtn_access_chain
*
46 vtn_access_chain_extend(struct vtn_builder
*b
, struct vtn_access_chain
*old
,
49 struct vtn_access_chain
*chain
;
51 unsigned old_len
= old
? old
->length
: 0;
52 chain
= vtn_access_chain_create(b
, old_len
+ new_ids
);
54 for (unsigned i
= 0; i
< old_len
; i
++)
55 chain
->link
[i
] = old
->link
[i
];
60 /* Dereference the given base pointer by the access chain */
61 static struct vtn_pointer
*
62 vtn_access_chain_pointer_dereference(struct vtn_builder
*b
,
63 struct vtn_pointer
*base
,
64 struct vtn_access_chain
*deref_chain
)
66 struct vtn_access_chain
*chain
=
67 vtn_access_chain_extend(b
, base
->chain
, deref_chain
->length
);
68 struct vtn_type
*type
= base
->type
;
70 /* OpPtrAccessChain is only allowed on things which support variable
71 * pointers. For everything else, the client is expected to just pass us
72 * the right access chain.
74 assert(!deref_chain
->ptr_as_array
);
76 unsigned start
= base
->chain
? base
->chain
->length
: 0;
77 for (unsigned i
= 0; i
< deref_chain
->length
; i
++) {
78 chain
->link
[start
+ i
] = deref_chain
->link
[i
];
80 if (glsl_type_is_struct(type
->type
)) {
81 assert(deref_chain
->link
[i
].mode
== vtn_access_mode_literal
);
82 type
= type
->members
[deref_chain
->link
[i
].id
];
84 type
= type
->array_element
;
88 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
89 ptr
->mode
= base
->mode
;
98 vtn_access_link_as_ssa(struct vtn_builder
*b
, struct vtn_access_link link
,
102 if (link
.mode
== vtn_access_mode_literal
) {
103 return nir_imm_int(&b
->nb
, link
.id
* stride
);
104 } else if (stride
== 1) {
105 return vtn_ssa_value(b
, link
.id
)->def
;
107 return nir_imul(&b
->nb
, vtn_ssa_value(b
, link
.id
)->def
,
108 nir_imm_int(&b
->nb
, stride
));
113 vtn_variable_resource_index(struct vtn_builder
*b
, struct vtn_variable
*var
,
114 nir_ssa_def
*desc_array_index
)
116 if (!desc_array_index
) {
117 assert(glsl_type_is_struct(var
->type
->type
));
118 desc_array_index
= nir_imm_int(&b
->nb
, 0);
121 nir_intrinsic_instr
*instr
=
122 nir_intrinsic_instr_create(b
->nb
.shader
,
123 nir_intrinsic_vulkan_resource_index
);
124 instr
->src
[0] = nir_src_for_ssa(desc_array_index
);
125 nir_intrinsic_set_desc_set(instr
, var
->descriptor_set
);
126 nir_intrinsic_set_binding(instr
, var
->binding
);
128 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
129 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
131 return &instr
->dest
.ssa
;
134 static struct vtn_pointer
*
135 vtn_ssa_offset_pointer_dereference(struct vtn_builder
*b
,
136 struct vtn_pointer
*base
,
137 struct vtn_access_chain
*deref_chain
)
139 nir_ssa_def
*block_index
= base
->block_index
;
140 nir_ssa_def
*offset
= base
->offset
;
141 struct vtn_type
*type
= base
->type
;
144 if (deref_chain
->ptr_as_array
) {
145 /* We need ptr_type for the stride */
146 assert(base
->ptr_type
);
147 /* This must be a pointer to an actual element somewhere */
148 assert(block_index
&& offset
);
149 /* We need at least one element in the chain */
150 assert(deref_chain
->length
>= 1);
152 nir_ssa_def
*elem_offset
=
153 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
],
154 base
->ptr_type
->stride
);
155 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
161 if (glsl_type_is_array(type
->type
)) {
162 /* We need at least one element in the chain */
163 assert(deref_chain
->length
>= 1);
165 nir_ssa_def
*desc_arr_idx
=
166 vtn_access_link_as_ssa(b
, deref_chain
->link
[0], 1);
167 block_index
= vtn_variable_resource_index(b
, base
->var
, desc_arr_idx
);
168 type
= type
->array_element
;
171 block_index
= vtn_variable_resource_index(b
, base
->var
, NULL
);
174 /* This is the first access chain so we also need an offset */
176 offset
= nir_imm_int(&b
->nb
, 0);
180 for (; idx
< deref_chain
->length
; idx
++) {
181 switch (glsl_get_base_type(type
->type
)) {
184 case GLSL_TYPE_UINT64
:
185 case GLSL_TYPE_INT64
:
186 case GLSL_TYPE_FLOAT
:
187 case GLSL_TYPE_DOUBLE
:
189 case GLSL_TYPE_ARRAY
: {
190 nir_ssa_def
*elem_offset
=
191 vtn_access_link_as_ssa(b
, deref_chain
->link
[idx
], type
->stride
);
192 offset
= nir_iadd(&b
->nb
, offset
, elem_offset
);
193 type
= type
->array_element
;
197 case GLSL_TYPE_STRUCT
: {
198 assert(deref_chain
->link
[idx
].mode
== vtn_access_mode_literal
);
199 unsigned member
= deref_chain
->link
[idx
].id
;
200 nir_ssa_def
*mem_offset
= nir_imm_int(&b
->nb
, type
->offsets
[member
]);
201 offset
= nir_iadd(&b
->nb
, offset
, mem_offset
);
202 type
= type
->members
[member
];
207 unreachable("Invalid type for deref");
211 struct vtn_pointer
*ptr
= rzalloc(b
, struct vtn_pointer
);
212 ptr
->mode
= base
->mode
;
214 ptr
->block_index
= block_index
;
215 ptr
->offset
= offset
;
220 /* Dereference the given base pointer by the access chain */
221 static struct vtn_pointer
*
222 vtn_pointer_dereference(struct vtn_builder
*b
,
223 struct vtn_pointer
*base
,
224 struct vtn_access_chain
*deref_chain
)
226 if (base
->mode
== vtn_variable_mode_ubo
||
227 base
->mode
== vtn_variable_mode_ssbo
) {
228 return vtn_ssa_offset_pointer_dereference(b
, base
, deref_chain
);
230 return vtn_access_chain_pointer_dereference(b
, base
, deref_chain
);
234 /* Crawls a chain of array derefs and rewrites the types so that the
235 * lengths stay the same but the terminal type is the one given by
236 * tail_type. This is useful for split structures.
239 rewrite_deref_types(nir_deref
*deref
, const struct glsl_type
*type
)
243 assert(deref
->child
->deref_type
== nir_deref_type_array
);
244 assert(glsl_type_is_array(deref
->type
));
245 rewrite_deref_types(deref
->child
, glsl_get_array_element(type
));
250 vtn_pointer_for_variable(struct vtn_builder
*b
,
251 struct vtn_variable
*var
, struct vtn_type
*ptr_type
)
253 struct vtn_pointer
*pointer
= rzalloc(b
, struct vtn_pointer
);
255 pointer
->mode
= var
->mode
;
256 pointer
->type
= var
->type
;
257 assert(ptr_type
->base_type
== vtn_base_type_pointer
);
258 assert(ptr_type
->deref
->type
== var
->type
->type
);
259 pointer
->ptr_type
= ptr_type
;
266 vtn_pointer_to_deref(struct vtn_builder
*b
, struct vtn_pointer
*ptr
)
268 /* Do on-the-fly copy propagation for samplers. */
269 if (ptr
->var
->copy_prop_sampler
)
270 return vtn_pointer_to_deref(b
, ptr
->var
->copy_prop_sampler
);
272 nir_deref_var
*deref_var
;
274 deref_var
= nir_deref_var_create(b
, ptr
->var
->var
);
275 /* Raw variable access */
279 assert(ptr
->var
->members
);
280 /* Create the deref_var manually. It will get filled out later. */
281 deref_var
= rzalloc(b
, nir_deref_var
);
282 deref_var
->deref
.deref_type
= nir_deref_type_var
;
285 struct vtn_access_chain
*chain
= ptr
->chain
;
288 struct vtn_type
*deref_type
= ptr
->var
->type
;
289 nir_deref
*tail
= &deref_var
->deref
;
290 nir_variable
**members
= ptr
->var
->members
;
292 for (unsigned i
= 0; i
< chain
->length
; i
++) {
293 enum glsl_base_type base_type
= glsl_get_base_type(deref_type
->type
);
297 case GLSL_TYPE_UINT64
:
298 case GLSL_TYPE_INT64
:
299 case GLSL_TYPE_FLOAT
:
300 case GLSL_TYPE_DOUBLE
:
302 case GLSL_TYPE_ARRAY
: {
303 deref_type
= deref_type
->array_element
;
305 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
306 deref_arr
->deref
.type
= deref_type
->type
;
308 if (chain
->link
[i
].mode
== vtn_access_mode_literal
) {
309 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
310 deref_arr
->base_offset
= chain
->link
[i
].id
;
312 assert(chain
->link
[i
].mode
== vtn_access_mode_id
);
313 deref_arr
->deref_array_type
= nir_deref_array_type_indirect
;
314 deref_arr
->base_offset
= 0;
315 deref_arr
->indirect
=
316 nir_src_for_ssa(vtn_ssa_value(b
, chain
->link
[i
].id
)->def
);
318 tail
->child
= &deref_arr
->deref
;
323 case GLSL_TYPE_STRUCT
: {
324 assert(chain
->link
[i
].mode
== vtn_access_mode_literal
);
325 unsigned idx
= chain
->link
[i
].id
;
326 deref_type
= deref_type
->members
[idx
];
328 /* This is a pre-split structure. */
329 deref_var
->var
= members
[idx
];
330 rewrite_deref_types(&deref_var
->deref
, members
[idx
]->type
);
331 assert(tail
->type
== deref_type
->type
);
334 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, idx
);
335 deref_struct
->deref
.type
= deref_type
->type
;
336 tail
->child
= &deref_struct
->deref
;
342 unreachable("Invalid type for deref");
346 assert(members
== NULL
);
351 _vtn_local_load_store(struct vtn_builder
*b
, bool load
, nir_deref_var
*deref
,
352 nir_deref
*tail
, struct vtn_ssa_value
*inout
)
354 /* The deref tail may contain a deref to select a component of a vector (in
355 * other words, it might not be an actual tail) so we have to save it away
356 * here since we overwrite it later.
358 nir_deref
*old_child
= tail
->child
;
360 if (glsl_type_is_vector_or_scalar(tail
->type
)) {
361 /* Terminate the deref chain in case there is one more link to pick
362 * off a component of the vector.
366 nir_intrinsic_op op
= load
? nir_intrinsic_load_var
:
367 nir_intrinsic_store_var
;
369 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
370 intrin
->variables
[0] = nir_deref_var_clone(deref
, intrin
);
371 intrin
->num_components
= glsl_get_vector_elements(tail
->type
);
374 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
375 intrin
->num_components
,
376 glsl_get_bit_size(tail
->type
),
378 inout
->def
= &intrin
->dest
.ssa
;
380 nir_intrinsic_set_write_mask(intrin
, (1 << intrin
->num_components
) - 1);
381 intrin
->src
[0] = nir_src_for_ssa(inout
->def
);
384 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
385 } else if (glsl_get_base_type(tail
->type
) == GLSL_TYPE_ARRAY
||
386 glsl_type_is_matrix(tail
->type
)) {
387 unsigned elems
= glsl_get_length(tail
->type
);
388 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
389 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
390 deref_arr
->deref
.type
= glsl_get_array_element(tail
->type
);
391 tail
->child
= &deref_arr
->deref
;
392 for (unsigned i
= 0; i
< elems
; i
++) {
393 deref_arr
->base_offset
= i
;
394 _vtn_local_load_store(b
, load
, deref
, tail
->child
, inout
->elems
[i
]);
397 assert(glsl_get_base_type(tail
->type
) == GLSL_TYPE_STRUCT
);
398 unsigned elems
= glsl_get_length(tail
->type
);
399 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, 0);
400 tail
->child
= &deref_struct
->deref
;
401 for (unsigned i
= 0; i
< elems
; i
++) {
402 deref_struct
->index
= i
;
403 deref_struct
->deref
.type
= glsl_get_struct_field(tail
->type
, i
);
404 _vtn_local_load_store(b
, load
, deref
, tail
->child
, inout
->elems
[i
]);
408 tail
->child
= old_child
;
412 vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
)
414 struct vtn_pointer
*ptr
= vtn_value(b
, id
, vtn_value_type_pointer
)->pointer
;
415 return vtn_pointer_to_deref(b
, ptr
);
419 * Gets the NIR-level deref tail, which may have as a child an array deref
420 * selecting which component due to OpAccessChain supporting per-component
421 * indexing in SPIR-V.
424 get_deref_tail(nir_deref_var
*deref
)
426 nir_deref
*cur
= &deref
->deref
;
427 while (!glsl_type_is_vector_or_scalar(cur
->type
) && cur
->child
)
433 struct vtn_ssa_value
*
434 vtn_local_load(struct vtn_builder
*b
, nir_deref_var
*src
)
436 nir_deref
*src_tail
= get_deref_tail(src
);
437 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, src_tail
->type
);
438 _vtn_local_load_store(b
, true, src
, src_tail
, val
);
440 if (src_tail
->child
) {
441 nir_deref_array
*vec_deref
= nir_deref_as_array(src_tail
->child
);
442 assert(vec_deref
->deref
.child
== NULL
);
443 val
->type
= vec_deref
->deref
.type
;
444 if (vec_deref
->deref_array_type
== nir_deref_array_type_direct
)
445 val
->def
= vtn_vector_extract(b
, val
->def
, vec_deref
->base_offset
);
447 val
->def
= vtn_vector_extract_dynamic(b
, val
->def
,
448 vec_deref
->indirect
.ssa
);
455 vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
458 nir_deref
*dest_tail
= get_deref_tail(dest
);
460 if (dest_tail
->child
) {
461 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, dest_tail
->type
);
462 _vtn_local_load_store(b
, true, dest
, dest_tail
, val
);
463 nir_deref_array
*deref
= nir_deref_as_array(dest_tail
->child
);
464 assert(deref
->deref
.child
== NULL
);
465 if (deref
->deref_array_type
== nir_deref_array_type_direct
)
466 val
->def
= vtn_vector_insert(b
, val
->def
, src
->def
,
469 val
->def
= vtn_vector_insert_dynamic(b
, val
->def
, src
->def
,
470 deref
->indirect
.ssa
);
471 _vtn_local_load_store(b
, false, dest
, dest_tail
, val
);
473 _vtn_local_load_store(b
, false, dest
, dest_tail
, src
);
478 get_vulkan_resource_index(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
479 struct vtn_type
**type
, unsigned *chain_idx
)
481 /* Push constants have no explicit binding */
482 if (ptr
->mode
== vtn_variable_mode_push_constant
) {
484 *type
= ptr
->var
->type
;
488 if (glsl_type_is_array(ptr
->var
->type
->type
)) {
489 assert(ptr
->chain
->length
> 0);
490 nir_ssa_def
*desc_array_index
=
491 vtn_access_link_as_ssa(b
, ptr
->chain
->link
[0], 1);
493 *type
= ptr
->var
->type
->array_element
;
494 return vtn_variable_resource_index(b
, ptr
->var
, desc_array_index
);
497 *type
= ptr
->var
->type
;
498 return vtn_variable_resource_index(b
, ptr
->var
, NULL
);
503 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
504 nir_ssa_def
**index_out
, unsigned *end_idx_out
)
507 assert(ptr
->block_index
);
508 *index_out
= ptr
->block_index
;
513 struct vtn_type
*type
;
514 *index_out
= get_vulkan_resource_index(b
, ptr
, &type
, &idx
);
516 nir_ssa_def
*offset
= nir_imm_int(&b
->nb
, 0);
517 for (; idx
< ptr
->chain
->length
; idx
++) {
518 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
522 case GLSL_TYPE_UINT64
:
523 case GLSL_TYPE_INT64
:
524 case GLSL_TYPE_FLOAT
:
525 case GLSL_TYPE_DOUBLE
:
527 case GLSL_TYPE_ARRAY
:
528 offset
= nir_iadd(&b
->nb
, offset
,
529 vtn_access_link_as_ssa(b
, ptr
->chain
->link
[idx
],
532 type
= type
->array_element
;
535 case GLSL_TYPE_STRUCT
: {
536 assert(ptr
->chain
->link
[idx
].mode
== vtn_access_mode_literal
);
537 unsigned member
= ptr
->chain
->link
[idx
].id
;
538 offset
= nir_iadd(&b
->nb
, offset
,
539 nir_imm_int(&b
->nb
, type
->offsets
[member
]));
540 type
= type
->members
[member
];
545 unreachable("Invalid type for deref");
549 assert(type
== ptr
->type
);
556 /* Tries to compute the size of an interface block based on the strides and
557 * offsets that are provided to us in the SPIR-V source.
560 vtn_type_block_size(struct vtn_type
*type
)
562 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
566 case GLSL_TYPE_UINT64
:
567 case GLSL_TYPE_INT64
:
568 case GLSL_TYPE_FLOAT
:
570 case GLSL_TYPE_DOUBLE
: {
571 unsigned cols
= type
->row_major
? glsl_get_vector_elements(type
->type
) :
572 glsl_get_matrix_columns(type
->type
);
574 assert(type
->stride
> 0);
575 return type
->stride
* cols
;
576 } else if (base_type
== GLSL_TYPE_DOUBLE
||
577 base_type
== GLSL_TYPE_UINT64
||
578 base_type
== GLSL_TYPE_INT64
) {
579 return glsl_get_vector_elements(type
->type
) * 8;
581 return glsl_get_vector_elements(type
->type
) * 4;
585 case GLSL_TYPE_STRUCT
:
586 case GLSL_TYPE_INTERFACE
: {
588 unsigned num_fields
= glsl_get_length(type
->type
);
589 for (unsigned f
= 0; f
< num_fields
; f
++) {
590 unsigned field_end
= type
->offsets
[f
] +
591 vtn_type_block_size(type
->members
[f
]);
592 size
= MAX2(size
, field_end
);
597 case GLSL_TYPE_ARRAY
:
598 assert(type
->stride
> 0);
599 assert(glsl_get_length(type
->type
) > 0);
600 return type
->stride
* glsl_get_length(type
->type
);
603 assert(!"Invalid block type");
609 vtn_access_chain_get_offset_size(struct vtn_access_chain
*chain
,
610 struct vtn_type
*type
,
611 unsigned *access_offset
,
612 unsigned *access_size
)
616 for (unsigned i
= 0; i
< chain
->length
; i
++) {
617 if (chain
->link
[i
].mode
!= vtn_access_mode_literal
)
620 if (glsl_type_is_struct(type
->type
)) {
621 *access_offset
+= type
->offsets
[chain
->link
[i
].id
];
622 type
= type
->members
[chain
->link
[i
].id
];
624 *access_offset
+= type
->stride
* chain
->link
[i
].id
;
625 type
= type
->array_element
;
629 *access_size
= vtn_type_block_size(type
);
633 _vtn_load_store_tail(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
634 nir_ssa_def
*index
, nir_ssa_def
*offset
,
635 unsigned access_offset
, unsigned access_size
,
636 struct vtn_ssa_value
**inout
, const struct glsl_type
*type
)
638 nir_intrinsic_instr
*instr
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
639 instr
->num_components
= glsl_get_vector_elements(type
);
643 nir_intrinsic_set_write_mask(instr
, (1 << instr
->num_components
) - 1);
644 instr
->src
[src
++] = nir_src_for_ssa((*inout
)->def
);
647 if (op
== nir_intrinsic_load_push_constant
) {
648 assert(access_offset
% 4 == 0);
650 nir_intrinsic_set_base(instr
, access_offset
);
651 nir_intrinsic_set_range(instr
, access_size
);
655 instr
->src
[src
++] = nir_src_for_ssa(index
);
657 if (op
== nir_intrinsic_load_push_constant
) {
658 /* We need to subtract the offset from where the intrinsic will load the
661 nir_src_for_ssa(nir_isub(&b
->nb
, offset
,
662 nir_imm_int(&b
->nb
, access_offset
)));
664 instr
->src
[src
++] = nir_src_for_ssa(offset
);
668 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
669 instr
->num_components
,
670 glsl_get_bit_size(type
), NULL
);
671 (*inout
)->def
= &instr
->dest
.ssa
;
674 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
676 if (load
&& glsl_get_base_type(type
) == GLSL_TYPE_BOOL
)
677 (*inout
)->def
= nir_ine(&b
->nb
, (*inout
)->def
, nir_imm_int(&b
->nb
, 0));
681 _vtn_block_load_store(struct vtn_builder
*b
, nir_intrinsic_op op
, bool load
,
682 nir_ssa_def
*index
, nir_ssa_def
*offset
,
683 unsigned access_offset
, unsigned access_size
,
684 struct vtn_access_chain
*chain
, unsigned chain_idx
,
685 struct vtn_type
*type
, struct vtn_ssa_value
**inout
)
687 if (chain
&& chain_idx
>= chain
->length
)
690 if (load
&& chain
== NULL
&& *inout
== NULL
)
691 *inout
= vtn_create_ssa_value(b
, type
->type
);
693 enum glsl_base_type base_type
= glsl_get_base_type(type
->type
);
697 case GLSL_TYPE_UINT64
:
698 case GLSL_TYPE_INT64
:
699 case GLSL_TYPE_FLOAT
:
700 case GLSL_TYPE_DOUBLE
:
702 /* This is where things get interesting. At this point, we've hit
703 * a vector, a scalar, or a matrix.
705 if (glsl_type_is_matrix(type
->type
)) {
706 /* Loading the whole matrix */
707 struct vtn_ssa_value
*transpose
;
708 unsigned num_ops
, vec_width
, col_stride
;
709 if (type
->row_major
) {
710 num_ops
= glsl_get_vector_elements(type
->type
);
711 vec_width
= glsl_get_matrix_columns(type
->type
);
712 col_stride
= type
->array_element
->stride
;
714 const struct glsl_type
*transpose_type
=
715 glsl_matrix_type(base_type
, vec_width
, num_ops
);
716 *inout
= vtn_create_ssa_value(b
, transpose_type
);
718 transpose
= vtn_ssa_transpose(b
, *inout
);
722 num_ops
= glsl_get_matrix_columns(type
->type
);
723 vec_width
= glsl_get_vector_elements(type
->type
);
724 col_stride
= type
->stride
;
727 for (unsigned i
= 0; i
< num_ops
; i
++) {
728 nir_ssa_def
*elem_offset
=
729 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* col_stride
));
730 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
731 access_offset
, access_size
,
733 glsl_vector_type(base_type
, vec_width
));
736 if (load
&& type
->row_major
)
737 *inout
= vtn_ssa_transpose(b
, *inout
);
739 unsigned elems
= glsl_get_vector_elements(type
->type
);
740 unsigned type_size
= glsl_get_bit_size(type
->type
) / 8;
741 if (elems
== 1 || type
->stride
== type_size
) {
742 /* This is a tightly-packed normal scalar or vector load */
743 assert(glsl_type_is_vector_or_scalar(type
->type
));
744 _vtn_load_store_tail(b
, op
, load
, index
, offset
,
745 access_offset
, access_size
,
748 /* This is a strided load. We have to load N things separately.
749 * This is the single column of a row-major matrix case.
751 assert(type
->stride
> type_size
);
752 assert(type
->stride
% type_size
== 0);
754 nir_ssa_def
*per_comp
[4];
755 for (unsigned i
= 0; i
< elems
; i
++) {
756 nir_ssa_def
*elem_offset
=
757 nir_iadd(&b
->nb
, offset
,
758 nir_imm_int(&b
->nb
, i
* type
->stride
));
759 struct vtn_ssa_value
*comp
, temp_val
;
761 temp_val
.def
= nir_channel(&b
->nb
, (*inout
)->def
, i
);
762 temp_val
.type
= glsl_scalar_type(base_type
);
765 _vtn_load_store_tail(b
, op
, load
, index
, elem_offset
,
766 access_offset
, access_size
,
767 &comp
, glsl_scalar_type(base_type
));
768 per_comp
[i
] = comp
->def
;
773 *inout
= vtn_create_ssa_value(b
, type
->type
);
774 (*inout
)->def
= nir_vec(&b
->nb
, per_comp
, elems
);
780 case GLSL_TYPE_ARRAY
: {
781 unsigned elems
= glsl_get_length(type
->type
);
782 for (unsigned i
= 0; i
< elems
; i
++) {
783 nir_ssa_def
*elem_off
=
784 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, i
* type
->stride
));
785 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
786 access_offset
, access_size
,
788 type
->array_element
, &(*inout
)->elems
[i
]);
793 case GLSL_TYPE_STRUCT
: {
794 unsigned elems
= glsl_get_length(type
->type
);
795 for (unsigned i
= 0; i
< elems
; i
++) {
796 nir_ssa_def
*elem_off
=
797 nir_iadd(&b
->nb
, offset
, nir_imm_int(&b
->nb
, type
->offsets
[i
]));
798 _vtn_block_load_store(b
, op
, load
, index
, elem_off
,
799 access_offset
, access_size
,
801 type
->members
[i
], &(*inout
)->elems
[i
]);
807 unreachable("Invalid block member type");
811 static struct vtn_ssa_value
*
812 vtn_block_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
815 unsigned access_offset
= 0, access_size
= 0;
817 case vtn_variable_mode_ubo
:
818 op
= nir_intrinsic_load_ubo
;
820 case vtn_variable_mode_ssbo
:
821 op
= nir_intrinsic_load_ssbo
;
823 case vtn_variable_mode_push_constant
:
824 op
= nir_intrinsic_load_push_constant
;
825 vtn_access_chain_get_offset_size(src
->chain
, src
->var
->type
,
826 &access_offset
, &access_size
);
829 assert(!"Invalid block variable mode");
832 nir_ssa_def
*offset
, *index
= NULL
;
834 offset
= vtn_pointer_to_offset(b
, src
, &index
, &chain_idx
);
836 struct vtn_ssa_value
*value
= NULL
;
837 _vtn_block_load_store(b
, op
, true, index
, offset
,
838 access_offset
, access_size
,
839 src
->chain
, chain_idx
, src
->type
, &value
);
844 vtn_block_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
845 struct vtn_pointer
*dst
)
847 nir_ssa_def
*offset
, *index
= NULL
;
849 offset
= vtn_pointer_to_offset(b
, dst
, &index
, &chain_idx
);
851 _vtn_block_load_store(b
, nir_intrinsic_store_ssbo
, false, index
, offset
,
852 0, 0, dst
->chain
, chain_idx
, dst
->type
, &src
);
856 vtn_pointer_is_external_block(struct vtn_pointer
*ptr
)
858 return ptr
->mode
== vtn_variable_mode_ssbo
||
859 ptr
->mode
== vtn_variable_mode_ubo
||
860 ptr
->mode
== vtn_variable_mode_push_constant
;
864 _vtn_variable_load_store(struct vtn_builder
*b
, bool load
,
865 struct vtn_pointer
*ptr
,
866 struct vtn_ssa_value
**inout
)
868 enum glsl_base_type base_type
= glsl_get_base_type(ptr
->type
->type
);
872 case GLSL_TYPE_UINT64
:
873 case GLSL_TYPE_INT64
:
874 case GLSL_TYPE_FLOAT
:
876 case GLSL_TYPE_DOUBLE
:
877 /* At this point, we have a scalar, vector, or matrix so we know that
878 * there cannot be any structure splitting still in the way. By
879 * stopping at the matrix level rather than the vector level, we
880 * ensure that matrices get loaded in the optimal way even if they
881 * are storred row-major in a UBO.
884 *inout
= vtn_local_load(b
, vtn_pointer_to_deref(b
, ptr
));
886 vtn_local_store(b
, *inout
, vtn_pointer_to_deref(b
, ptr
));
890 case GLSL_TYPE_ARRAY
:
891 case GLSL_TYPE_STRUCT
: {
892 unsigned elems
= glsl_get_length(ptr
->type
->type
);
894 assert(*inout
== NULL
);
895 *inout
= rzalloc(b
, struct vtn_ssa_value
);
896 (*inout
)->type
= ptr
->type
->type
;
897 (*inout
)->elems
= rzalloc_array(b
, struct vtn_ssa_value
*, elems
);
900 struct vtn_access_chain chain
= {
903 { .mode
= vtn_access_mode_literal
, },
906 for (unsigned i
= 0; i
< elems
; i
++) {
907 chain
.link
[0].id
= i
;
908 struct vtn_pointer
*elem
= vtn_pointer_dereference(b
, ptr
, &chain
);
909 _vtn_variable_load_store(b
, load
, elem
, &(*inout
)->elems
[i
]);
915 unreachable("Invalid access chain type");
919 struct vtn_ssa_value
*
920 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
)
922 if (vtn_pointer_is_external_block(src
)) {
923 return vtn_block_load(b
, src
);
925 struct vtn_ssa_value
*val
= NULL
;
926 _vtn_variable_load_store(b
, true, src
, &val
);
932 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
933 struct vtn_pointer
*dest
)
935 if (vtn_pointer_is_external_block(dest
)) {
936 assert(dest
->mode
== vtn_variable_mode_ssbo
);
937 vtn_block_store(b
, src
, dest
);
939 _vtn_variable_load_store(b
, false, dest
, &src
);
944 _vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
945 struct vtn_pointer
*src
)
947 assert(src
->type
->type
== dest
->type
->type
);
948 enum glsl_base_type base_type
= glsl_get_base_type(src
->type
->type
);
952 case GLSL_TYPE_UINT64
:
953 case GLSL_TYPE_INT64
:
954 case GLSL_TYPE_FLOAT
:
955 case GLSL_TYPE_DOUBLE
:
957 /* At this point, we have a scalar, vector, or matrix so we know that
958 * there cannot be any structure splitting still in the way. By
959 * stopping at the matrix level rather than the vector level, we
960 * ensure that matrices get loaded in the optimal way even if they
961 * are storred row-major in a UBO.
963 vtn_variable_store(b
, vtn_variable_load(b
, src
), dest
);
966 case GLSL_TYPE_ARRAY
:
967 case GLSL_TYPE_STRUCT
: {
968 struct vtn_access_chain chain
= {
971 { .mode
= vtn_access_mode_literal
, },
974 unsigned elems
= glsl_get_length(src
->type
->type
);
975 for (unsigned i
= 0; i
< elems
; i
++) {
976 chain
.link
[0].id
= i
;
977 struct vtn_pointer
*src_elem
=
978 vtn_pointer_dereference(b
, src
, &chain
);
979 struct vtn_pointer
*dest_elem
=
980 vtn_pointer_dereference(b
, dest
, &chain
);
982 _vtn_variable_copy(b
, dest_elem
, src_elem
);
988 unreachable("Invalid access chain type");
993 vtn_variable_copy(struct vtn_builder
*b
, struct vtn_pointer
*dest
,
994 struct vtn_pointer
*src
)
996 /* TODO: At some point, we should add a special-case for when we can
997 * just emit a copy_var intrinsic.
999 _vtn_variable_copy(b
, dest
, src
);
1003 set_mode_system_value(nir_variable_mode
*mode
)
1005 assert(*mode
== nir_var_system_value
|| *mode
== nir_var_shader_in
);
1006 *mode
= nir_var_system_value
;
1010 vtn_get_builtin_location(struct vtn_builder
*b
,
1011 SpvBuiltIn builtin
, int *location
,
1012 nir_variable_mode
*mode
)
1015 case SpvBuiltInPosition
:
1016 *location
= VARYING_SLOT_POS
;
1018 case SpvBuiltInPointSize
:
1019 *location
= VARYING_SLOT_PSIZ
;
1021 case SpvBuiltInClipDistance
:
1022 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
1024 case SpvBuiltInCullDistance
:
1025 *location
= VARYING_SLOT_CULL_DIST0
;
1027 case SpvBuiltInVertexIndex
:
1028 *location
= SYSTEM_VALUE_VERTEX_ID
;
1029 set_mode_system_value(mode
);
1031 case SpvBuiltInVertexId
:
1032 /* Vulkan defines VertexID to be zero-based and reserves the new
1033 * builtin keyword VertexIndex to indicate the non-zero-based value.
1035 *location
= SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1036 set_mode_system_value(mode
);
1038 case SpvBuiltInInstanceIndex
:
1039 *location
= SYSTEM_VALUE_INSTANCE_INDEX
;
1040 set_mode_system_value(mode
);
1042 case SpvBuiltInInstanceId
:
1043 *location
= SYSTEM_VALUE_INSTANCE_ID
;
1044 set_mode_system_value(mode
);
1046 case SpvBuiltInPrimitiveId
:
1047 if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
) {
1048 assert(*mode
== nir_var_shader_in
);
1049 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1050 } else if (*mode
== nir_var_shader_out
) {
1051 *location
= VARYING_SLOT_PRIMITIVE_ID
;
1053 *location
= SYSTEM_VALUE_PRIMITIVE_ID
;
1054 set_mode_system_value(mode
);
1057 case SpvBuiltInInvocationId
:
1058 *location
= SYSTEM_VALUE_INVOCATION_ID
;
1059 set_mode_system_value(mode
);
1061 case SpvBuiltInLayer
:
1062 *location
= VARYING_SLOT_LAYER
;
1063 if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
)
1064 *mode
= nir_var_shader_in
;
1065 else if (b
->shader
->stage
== MESA_SHADER_GEOMETRY
)
1066 *mode
= nir_var_shader_out
;
1068 unreachable("invalid stage for SpvBuiltInLayer");
1070 case SpvBuiltInViewportIndex
:
1071 *location
= VARYING_SLOT_VIEWPORT
;
1072 if (b
->shader
->stage
== MESA_SHADER_GEOMETRY
)
1073 *mode
= nir_var_shader_out
;
1074 else if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
)
1075 *mode
= nir_var_shader_in
;
1077 unreachable("invalid stage for SpvBuiltInViewportIndex");
1079 case SpvBuiltInTessLevelOuter
:
1080 *location
= VARYING_SLOT_TESS_LEVEL_OUTER
;
1082 case SpvBuiltInTessLevelInner
:
1083 *location
= VARYING_SLOT_TESS_LEVEL_INNER
;
1085 case SpvBuiltInTessCoord
:
1086 *location
= SYSTEM_VALUE_TESS_COORD
;
1087 set_mode_system_value(mode
);
1089 case SpvBuiltInPatchVertices
:
1090 *location
= SYSTEM_VALUE_VERTICES_IN
;
1091 set_mode_system_value(mode
);
1093 case SpvBuiltInFragCoord
:
1094 *location
= VARYING_SLOT_POS
;
1095 assert(*mode
== nir_var_shader_in
);
1097 case SpvBuiltInPointCoord
:
1098 *location
= VARYING_SLOT_PNTC
;
1099 assert(*mode
== nir_var_shader_in
);
1101 case SpvBuiltInFrontFacing
:
1102 *location
= SYSTEM_VALUE_FRONT_FACE
;
1103 set_mode_system_value(mode
);
1105 case SpvBuiltInSampleId
:
1106 *location
= SYSTEM_VALUE_SAMPLE_ID
;
1107 set_mode_system_value(mode
);
1109 case SpvBuiltInSamplePosition
:
1110 *location
= SYSTEM_VALUE_SAMPLE_POS
;
1111 set_mode_system_value(mode
);
1113 case SpvBuiltInSampleMask
:
1114 if (*mode
== nir_var_shader_out
) {
1115 *location
= FRAG_RESULT_SAMPLE_MASK
;
1117 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
1118 set_mode_system_value(mode
);
1121 case SpvBuiltInFragDepth
:
1122 *location
= FRAG_RESULT_DEPTH
;
1123 assert(*mode
== nir_var_shader_out
);
1125 case SpvBuiltInNumWorkgroups
:
1126 *location
= SYSTEM_VALUE_NUM_WORK_GROUPS
;
1127 set_mode_system_value(mode
);
1129 case SpvBuiltInWorkgroupSize
:
1130 /* This should already be handled */
1131 unreachable("unsupported builtin");
1133 case SpvBuiltInWorkgroupId
:
1134 *location
= SYSTEM_VALUE_WORK_GROUP_ID
;
1135 set_mode_system_value(mode
);
1137 case SpvBuiltInLocalInvocationId
:
1138 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1139 set_mode_system_value(mode
);
1141 case SpvBuiltInLocalInvocationIndex
:
1142 *location
= SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1143 set_mode_system_value(mode
);
1145 case SpvBuiltInGlobalInvocationId
:
1146 *location
= SYSTEM_VALUE_GLOBAL_INVOCATION_ID
;
1147 set_mode_system_value(mode
);
1149 case SpvBuiltInBaseVertex
:
1150 *location
= SYSTEM_VALUE_BASE_VERTEX
;
1151 set_mode_system_value(mode
);
1153 case SpvBuiltInBaseInstance
:
1154 *location
= SYSTEM_VALUE_BASE_INSTANCE
;
1155 set_mode_system_value(mode
);
1157 case SpvBuiltInDrawIndex
:
1158 *location
= SYSTEM_VALUE_DRAW_ID
;
1159 set_mode_system_value(mode
);
1161 case SpvBuiltInViewIndex
:
1162 *location
= SYSTEM_VALUE_VIEW_INDEX
;
1163 set_mode_system_value(mode
);
1165 case SpvBuiltInHelperInvocation
:
1167 unreachable("unsupported builtin");
1172 apply_var_decoration(struct vtn_builder
*b
, nir_variable
*nir_var
,
1173 const struct vtn_decoration
*dec
)
1175 switch (dec
->decoration
) {
1176 case SpvDecorationRelaxedPrecision
:
1177 break; /* FIXME: Do nothing with this for now. */
1178 case SpvDecorationNoPerspective
:
1179 nir_var
->data
.interpolation
= INTERP_MODE_NOPERSPECTIVE
;
1181 case SpvDecorationFlat
:
1182 nir_var
->data
.interpolation
= INTERP_MODE_FLAT
;
1184 case SpvDecorationCentroid
:
1185 nir_var
->data
.centroid
= true;
1187 case SpvDecorationSample
:
1188 nir_var
->data
.sample
= true;
1190 case SpvDecorationInvariant
:
1191 nir_var
->data
.invariant
= true;
1193 case SpvDecorationConstant
:
1194 assert(nir_var
->constant_initializer
!= NULL
);
1195 nir_var
->data
.read_only
= true;
1197 case SpvDecorationNonReadable
:
1198 nir_var
->data
.image
.write_only
= true;
1200 case SpvDecorationNonWritable
:
1201 nir_var
->data
.read_only
= true;
1202 nir_var
->data
.image
.read_only
= true;
1204 case SpvDecorationComponent
:
1205 nir_var
->data
.location_frac
= dec
->literals
[0];
1207 case SpvDecorationIndex
:
1208 nir_var
->data
.index
= dec
->literals
[0];
1210 case SpvDecorationBuiltIn
: {
1211 SpvBuiltIn builtin
= dec
->literals
[0];
1213 if (builtin
== SpvBuiltInWorkgroupSize
) {
1214 /* This shouldn't be a builtin. It's actually a constant. */
1215 nir_var
->data
.mode
= nir_var_global
;
1216 nir_var
->data
.read_only
= true;
1218 nir_constant
*c
= rzalloc(nir_var
, nir_constant
);
1219 c
->values
[0].u32
[0] = b
->shader
->info
.cs
.local_size
[0];
1220 c
->values
[0].u32
[1] = b
->shader
->info
.cs
.local_size
[1];
1221 c
->values
[0].u32
[2] = b
->shader
->info
.cs
.local_size
[2];
1222 nir_var
->constant_initializer
= c
;
1226 nir_variable_mode mode
= nir_var
->data
.mode
;
1227 vtn_get_builtin_location(b
, builtin
, &nir_var
->data
.location
, &mode
);
1228 nir_var
->data
.mode
= mode
;
1231 case SpvBuiltInTessLevelOuter
:
1232 case SpvBuiltInTessLevelInner
:
1233 nir_var
->data
.compact
= true;
1235 case SpvBuiltInSamplePosition
:
1236 nir_var
->data
.origin_upper_left
= b
->origin_upper_left
;
1238 case SpvBuiltInFragCoord
:
1239 nir_var
->data
.pixel_center_integer
= b
->pixel_center_integer
;
1246 case SpvDecorationSpecId
:
1247 case SpvDecorationRowMajor
:
1248 case SpvDecorationColMajor
:
1249 case SpvDecorationMatrixStride
:
1250 case SpvDecorationRestrict
:
1251 case SpvDecorationAliased
:
1252 case SpvDecorationVolatile
:
1253 case SpvDecorationCoherent
:
1254 case SpvDecorationUniform
:
1255 case SpvDecorationStream
:
1256 case SpvDecorationOffset
:
1257 case SpvDecorationLinkageAttributes
:
1258 break; /* Do nothing with these here */
1260 case SpvDecorationPatch
:
1261 nir_var
->data
.patch
= true;
1264 case SpvDecorationLocation
:
1265 unreachable("Handled above");
1267 case SpvDecorationBlock
:
1268 case SpvDecorationBufferBlock
:
1269 case SpvDecorationArrayStride
:
1270 case SpvDecorationGLSLShared
:
1271 case SpvDecorationGLSLPacked
:
1272 break; /* These can apply to a type but we don't care about them */
1274 case SpvDecorationBinding
:
1275 case SpvDecorationDescriptorSet
:
1276 case SpvDecorationNoContraction
:
1277 case SpvDecorationInputAttachmentIndex
:
1278 vtn_warn("Decoration not allowed for variable or structure member: %s",
1279 spirv_decoration_to_string(dec
->decoration
));
1282 case SpvDecorationXfbBuffer
:
1283 case SpvDecorationXfbStride
:
1284 vtn_warn("Vulkan does not have transform feedback: %s",
1285 spirv_decoration_to_string(dec
->decoration
));
1288 case SpvDecorationCPacked
:
1289 case SpvDecorationSaturatedConversion
:
1290 case SpvDecorationFuncParamAttr
:
1291 case SpvDecorationFPRoundingMode
:
1292 case SpvDecorationFPFastMathMode
:
1293 case SpvDecorationAlignment
:
1294 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1295 spirv_decoration_to_string(dec
->decoration
));
1299 unreachable("Unhandled decoration");
1304 var_is_patch_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1305 const struct vtn_decoration
*dec
, void *out_is_patch
)
1307 if (dec
->decoration
== SpvDecorationPatch
) {
1308 *((bool *) out_is_patch
) = true;
1313 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
1314 const struct vtn_decoration
*dec
, void *void_var
)
1316 struct vtn_variable
*vtn_var
= void_var
;
1318 /* Handle decorations that apply to a vtn_variable as a whole */
1319 switch (dec
->decoration
) {
1320 case SpvDecorationBinding
:
1321 vtn_var
->binding
= dec
->literals
[0];
1323 case SpvDecorationDescriptorSet
:
1324 vtn_var
->descriptor_set
= dec
->literals
[0];
1326 case SpvDecorationInputAttachmentIndex
:
1327 vtn_var
->input_attachment_index
= dec
->literals
[0];
1329 case SpvDecorationPatch
:
1330 vtn_var
->patch
= true;
1336 if (val
->value_type
== vtn_value_type_pointer
) {
1337 assert(val
->pointer
->var
== void_var
);
1338 assert(val
->pointer
->chain
== NULL
);
1339 assert(member
== -1);
1341 assert(val
->value_type
== vtn_value_type_type
);
1344 /* Location is odd. If applied to a split structure, we have to walk the
1345 * whole thing and accumulate the location. It's easier to handle as a
1348 if (dec
->decoration
== SpvDecorationLocation
) {
1349 unsigned location
= dec
->literals
[0];
1350 bool is_vertex_input
;
1351 if (b
->shader
->stage
== MESA_SHADER_FRAGMENT
&&
1352 vtn_var
->mode
== vtn_variable_mode_output
) {
1353 is_vertex_input
= false;
1354 location
+= FRAG_RESULT_DATA0
;
1355 } else if (b
->shader
->stage
== MESA_SHADER_VERTEX
&&
1356 vtn_var
->mode
== vtn_variable_mode_input
) {
1357 is_vertex_input
= true;
1358 location
+= VERT_ATTRIB_GENERIC0
;
1359 } else if (vtn_var
->mode
== vtn_variable_mode_input
||
1360 vtn_var
->mode
== vtn_variable_mode_output
) {
1361 is_vertex_input
= false;
1362 location
+= vtn_var
->patch
? VARYING_SLOT_PATCH0
: VARYING_SLOT_VAR0
;
1364 vtn_warn("Location must be on input or output variable");
1369 /* This handles the member and lone variable cases */
1370 vtn_var
->var
->data
.location
= location
;
1372 /* This handles the structure member case */
1373 assert(vtn_var
->members
);
1375 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1376 for (unsigned i
= 0; i
< length
; i
++) {
1377 vtn_var
->members
[i
]->data
.location
= location
;
1379 glsl_count_attribute_slots(vtn_var
->members
[i
]->interface_type
,
1386 assert(member
<= 0);
1387 apply_var_decoration(b
, vtn_var
->var
, dec
);
1388 } else if (vtn_var
->members
) {
1390 assert(vtn_var
->members
);
1391 apply_var_decoration(b
, vtn_var
->members
[member
], dec
);
1394 glsl_get_length(glsl_without_array(vtn_var
->type
->type
));
1395 for (unsigned i
= 0; i
< length
; i
++)
1396 apply_var_decoration(b
, vtn_var
->members
[i
], dec
);
1399 /* A few variables, those with external storage, have no actual
1400 * nir_variables associated with them. Fortunately, all decorations
1401 * we care about for those variables are on the type only.
1403 assert(vtn_var
->mode
== vtn_variable_mode_ubo
||
1404 vtn_var
->mode
== vtn_variable_mode_ssbo
||
1405 vtn_var
->mode
== vtn_variable_mode_push_constant
);
1410 static enum vtn_variable_mode
1411 vtn_storage_class_to_mode(SpvStorageClass
class,
1412 struct vtn_type
*interface_type
,
1413 nir_variable_mode
*nir_mode_out
)
1415 enum vtn_variable_mode mode
;
1416 nir_variable_mode nir_mode
;
1418 case SpvStorageClassUniform
:
1419 if (interface_type
->block
) {
1420 mode
= vtn_variable_mode_ubo
;
1422 } else if (interface_type
->buffer_block
) {
1423 mode
= vtn_variable_mode_ssbo
;
1426 assert(!"Invalid uniform variable type");
1429 case SpvStorageClassStorageBuffer
:
1430 mode
= vtn_variable_mode_ssbo
;
1433 case SpvStorageClassUniformConstant
:
1434 if (glsl_type_is_image(interface_type
->type
)) {
1435 mode
= vtn_variable_mode_image
;
1436 nir_mode
= nir_var_uniform
;
1437 } else if (glsl_type_is_sampler(interface_type
->type
)) {
1438 mode
= vtn_variable_mode_sampler
;
1439 nir_mode
= nir_var_uniform
;
1441 assert(!"Invalid uniform constant variable type");
1444 case SpvStorageClassPushConstant
:
1445 mode
= vtn_variable_mode_push_constant
;
1446 nir_mode
= nir_var_uniform
;
1448 case SpvStorageClassInput
:
1449 mode
= vtn_variable_mode_input
;
1450 nir_mode
= nir_var_shader_in
;
1452 case SpvStorageClassOutput
:
1453 mode
= vtn_variable_mode_output
;
1454 nir_mode
= nir_var_shader_out
;
1456 case SpvStorageClassPrivate
:
1457 mode
= vtn_variable_mode_global
;
1458 nir_mode
= nir_var_global
;
1460 case SpvStorageClassFunction
:
1461 mode
= vtn_variable_mode_local
;
1462 nir_mode
= nir_var_local
;
1464 case SpvStorageClassWorkgroup
:
1465 mode
= vtn_variable_mode_workgroup
;
1466 nir_mode
= nir_var_shared
;
1468 case SpvStorageClassCrossWorkgroup
:
1469 case SpvStorageClassGeneric
:
1470 case SpvStorageClassAtomicCounter
:
1472 unreachable("Unhandled variable storage class");
1476 *nir_mode_out
= nir_mode
;
1482 is_per_vertex_inout(const struct vtn_variable
*var
, gl_shader_stage stage
)
1484 if (var
->patch
|| !glsl_type_is_array(var
->type
->type
))
1487 if (var
->mode
== vtn_variable_mode_input
) {
1488 return stage
== MESA_SHADER_TESS_CTRL
||
1489 stage
== MESA_SHADER_TESS_EVAL
||
1490 stage
== MESA_SHADER_GEOMETRY
;
1493 if (var
->mode
== vtn_variable_mode_output
)
1494 return stage
== MESA_SHADER_TESS_CTRL
;
1500 vtn_create_variable(struct vtn_builder
*b
, struct vtn_value
*val
,
1501 struct vtn_type
*ptr_type
, SpvStorageClass storage_class
,
1502 nir_constant
*initializer
)
1504 assert(ptr_type
->base_type
== vtn_base_type_pointer
);
1505 struct vtn_type
*type
= ptr_type
->deref
;
1506 assert(type
->base_type
!= vtn_base_type_pointer
);
1508 struct vtn_type
*without_array
= type
;
1509 while(glsl_type_is_array(without_array
->type
))
1510 without_array
= without_array
->array_element
;
1512 enum vtn_variable_mode mode
;
1513 nir_variable_mode nir_mode
;
1514 mode
= vtn_storage_class_to_mode(storage_class
, without_array
, &nir_mode
);
1517 case vtn_variable_mode_ubo
:
1518 b
->shader
->info
.num_ubos
++;
1520 case vtn_variable_mode_ssbo
:
1521 b
->shader
->info
.num_ssbos
++;
1523 case vtn_variable_mode_image
:
1524 b
->shader
->info
.num_images
++;
1526 case vtn_variable_mode_sampler
:
1527 b
->shader
->info
.num_textures
++;
1529 case vtn_variable_mode_push_constant
:
1530 b
->shader
->num_uniforms
= vtn_type_block_size(type
);
1533 /* No tallying is needed */
1537 struct vtn_variable
*var
= rzalloc(b
, struct vtn_variable
);
1541 assert(val
->value_type
== vtn_value_type_pointer
);
1542 val
->pointer
= vtn_pointer_for_variable(b
, var
, ptr_type
);
1544 switch (var
->mode
) {
1545 case vtn_variable_mode_local
:
1546 case vtn_variable_mode_global
:
1547 case vtn_variable_mode_image
:
1548 case vtn_variable_mode_sampler
:
1549 case vtn_variable_mode_workgroup
:
1550 /* For these, we create the variable normally */
1551 var
->var
= rzalloc(b
->shader
, nir_variable
);
1552 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1553 var
->var
->type
= var
->type
->type
;
1554 var
->var
->data
.mode
= nir_mode
;
1556 switch (var
->mode
) {
1557 case vtn_variable_mode_image
:
1558 case vtn_variable_mode_sampler
:
1559 var
->var
->interface_type
= without_array
->type
;
1562 var
->var
->interface_type
= NULL
;
1567 case vtn_variable_mode_input
:
1568 case vtn_variable_mode_output
: {
1569 /* In order to know whether or not we're a per-vertex inout, we need
1570 * the patch qualifier. This means walking the variable decorations
1571 * early before we actually create any variables. Not a big deal.
1573 * GLSLang really likes to place decorations in the most interior
1574 * thing it possibly can. In particular, if you have a struct, it
1575 * will place the patch decorations on the struct members. This
1576 * should be handled by the variable splitting below just fine.
1578 * If you have an array-of-struct, things get even more weird as it
1579 * will place the patch decorations on the struct even though it's
1580 * inside an array and some of the members being patch and others not
1581 * makes no sense whatsoever. Since the only sensible thing is for
1582 * it to be all or nothing, we'll call it patch if any of the members
1583 * are declared patch.
1586 vtn_foreach_decoration(b
, val
, var_is_patch_cb
, &var
->patch
);
1587 if (glsl_type_is_array(var
->type
->type
) &&
1588 glsl_type_is_struct(without_array
->type
)) {
1589 vtn_foreach_decoration(b
, without_array
->val
,
1590 var_is_patch_cb
, &var
->patch
);
1593 /* For inputs and outputs, we immediately split structures. This
1594 * is for a couple of reasons. For one, builtins may all come in
1595 * a struct and we really want those split out into separate
1596 * variables. For another, interpolation qualifiers can be
1597 * applied to members of the top-level struct ane we need to be
1598 * able to preserve that information.
1601 int array_length
= -1;
1602 struct vtn_type
*interface_type
= var
->type
;
1603 if (is_per_vertex_inout(var
, b
->shader
->stage
)) {
1604 /* In Geometry shaders (and some tessellation), inputs come
1605 * in per-vertex arrays. However, some builtins come in
1606 * non-per-vertex, hence the need for the is_array check. In
1607 * any case, there are no non-builtin arrays allowed so this
1608 * check should be sufficient.
1610 interface_type
= var
->type
->array_element
;
1611 array_length
= glsl_get_length(var
->type
->type
);
1614 if (glsl_type_is_struct(interface_type
->type
)) {
1615 /* It's a struct. Split it. */
1616 unsigned num_members
= glsl_get_length(interface_type
->type
);
1617 var
->members
= ralloc_array(b
, nir_variable
*, num_members
);
1619 for (unsigned i
= 0; i
< num_members
; i
++) {
1620 const struct glsl_type
*mtype
= interface_type
->members
[i
]->type
;
1621 if (array_length
>= 0)
1622 mtype
= glsl_array_type(mtype
, array_length
);
1624 var
->members
[i
] = rzalloc(b
->shader
, nir_variable
);
1625 var
->members
[i
]->name
=
1626 ralloc_asprintf(var
->members
[i
], "%s.%d", val
->name
, i
);
1627 var
->members
[i
]->type
= mtype
;
1628 var
->members
[i
]->interface_type
=
1629 interface_type
->members
[i
]->type
;
1630 var
->members
[i
]->data
.mode
= nir_mode
;
1631 var
->members
[i
]->data
.patch
= var
->patch
;
1634 var
->var
= rzalloc(b
->shader
, nir_variable
);
1635 var
->var
->name
= ralloc_strdup(var
->var
, val
->name
);
1636 var
->var
->type
= var
->type
->type
;
1637 var
->var
->interface_type
= interface_type
->type
;
1638 var
->var
->data
.mode
= nir_mode
;
1639 var
->var
->data
.patch
= var
->patch
;
1642 /* For inputs and outputs, we need to grab locations and builtin
1643 * information from the interface type.
1645 vtn_foreach_decoration(b
, interface_type
->val
, var_decoration_cb
, var
);
1649 case vtn_variable_mode_param
:
1650 unreachable("Not created through OpVariable");
1652 case vtn_variable_mode_ubo
:
1653 case vtn_variable_mode_ssbo
:
1654 case vtn_variable_mode_push_constant
:
1655 /* These don't need actual variables. */
1660 var
->var
->constant_initializer
=
1661 nir_constant_clone(initializer
, var
->var
);
1664 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
1666 if (var
->mode
== vtn_variable_mode_image
||
1667 var
->mode
== vtn_variable_mode_sampler
) {
1668 /* XXX: We still need the binding information in the nir_variable
1669 * for these. We should fix that.
1671 var
->var
->data
.binding
= var
->binding
;
1672 var
->var
->data
.descriptor_set
= var
->descriptor_set
;
1673 var
->var
->data
.index
= var
->input_attachment_index
;
1675 if (var
->mode
== vtn_variable_mode_image
)
1676 var
->var
->data
.image
.format
= without_array
->image_format
;
1679 if (var
->mode
== vtn_variable_mode_local
) {
1680 assert(var
->members
== NULL
&& var
->var
!= NULL
);
1681 nir_function_impl_add_variable(b
->impl
, var
->var
);
1682 } else if (var
->var
) {
1683 nir_shader_add_variable(b
->shader
, var
->var
);
1684 } else if (var
->members
) {
1685 unsigned count
= glsl_get_length(without_array
->type
);
1686 for (unsigned i
= 0; i
< count
; i
++) {
1687 assert(var
->members
[i
]->data
.mode
!= nir_var_local
);
1688 nir_shader_add_variable(b
->shader
, var
->members
[i
]);
1691 assert(var
->mode
== vtn_variable_mode_ubo
||
1692 var
->mode
== vtn_variable_mode_ssbo
||
1693 var
->mode
== vtn_variable_mode_push_constant
);
1698 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
1699 const uint32_t *w
, unsigned count
)
1703 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1704 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1708 case SpvOpVariable
: {
1709 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1711 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1713 SpvStorageClass storage_class
= w
[3];
1714 nir_constant
*initializer
= NULL
;
1716 initializer
= vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
1718 vtn_create_variable(b
, val
, ptr_type
, storage_class
, initializer
);
1722 case SpvOpAccessChain
:
1723 case SpvOpPtrAccessChain
:
1724 case SpvOpInBoundsAccessChain
: {
1725 struct vtn_access_chain
*chain
= vtn_access_chain_create(b
, count
- 4);
1726 chain
->ptr_as_array
= (opcode
== SpvOpPtrAccessChain
);
1729 for (int i
= 4; i
< count
; i
++) {
1730 struct vtn_value
*link_val
= vtn_untyped_value(b
, w
[i
]);
1731 if (link_val
->value_type
== vtn_value_type_constant
) {
1732 chain
->link
[idx
].mode
= vtn_access_mode_literal
;
1733 chain
->link
[idx
].id
= link_val
->constant
->values
[0].u32
[0];
1735 chain
->link
[idx
].mode
= vtn_access_mode_id
;
1736 chain
->link
[idx
].id
= w
[i
];
1742 struct vtn_type
*ptr_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1743 struct vtn_value
*base_val
= vtn_untyped_value(b
, w
[3]);
1744 if (base_val
->value_type
== vtn_value_type_sampled_image
) {
1745 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1746 * to combine an array of images with a single sampler to get an
1747 * array of sampled images that all share the same sampler.
1748 * Fortunately, this means that we can more-or-less ignore the
1749 * sampler when crawling the access chain, but it does leave us
1750 * with this rather awkward little special-case.
1752 struct vtn_value
*val
=
1753 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1754 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1755 val
->sampled_image
->image
=
1756 vtn_pointer_dereference(b
, base_val
->sampled_image
->image
, chain
);
1757 val
->sampled_image
->sampler
= base_val
->sampled_image
->sampler
;
1759 assert(base_val
->value_type
== vtn_value_type_pointer
);
1760 struct vtn_value
*val
=
1761 vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1762 val
->pointer
= vtn_pointer_dereference(b
, base_val
->pointer
, chain
);
1763 val
->pointer
->ptr_type
= ptr_type
;
1768 case SpvOpCopyMemory
: {
1769 struct vtn_value
*dest
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
1770 struct vtn_value
*src
= vtn_value(b
, w
[2], vtn_value_type_pointer
);
1772 vtn_variable_copy(b
, dest
->pointer
, src
->pointer
);
1777 struct vtn_pointer
*src
=
1778 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1780 if (src
->mode
== vtn_variable_mode_image
||
1781 src
->mode
== vtn_variable_mode_sampler
) {
1782 vtn_push_value(b
, w
[2], vtn_value_type_pointer
)->pointer
= src
;
1786 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1787 val
->ssa
= vtn_variable_load(b
, src
);
1792 struct vtn_pointer
*dest
=
1793 vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
1795 if (glsl_type_is_sampler(dest
->type
->type
)) {
1796 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
1797 "propagation to workaround the problem.");
1798 assert(dest
->var
->copy_prop_sampler
== NULL
);
1799 dest
->var
->copy_prop_sampler
=
1800 vtn_value(b
, w
[2], vtn_value_type_pointer
)->pointer
;
1804 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
1805 vtn_variable_store(b
, src
, dest
);
1809 case SpvOpArrayLength
: {
1810 struct vtn_pointer
*ptr
=
1811 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1813 const uint32_t offset
= ptr
->var
->type
->offsets
[w
[4]];
1814 const uint32_t stride
= ptr
->var
->type
->members
[w
[4]]->stride
;
1817 struct vtn_type
*type
;
1818 nir_ssa_def
*index
=
1819 get_vulkan_resource_index(b
, ptr
, &type
, &chain_idx
);
1821 nir_intrinsic_instr
*instr
=
1822 nir_intrinsic_instr_create(b
->nb
.shader
,
1823 nir_intrinsic_get_buffer_size
);
1824 instr
->src
[0] = nir_src_for_ssa(index
);
1825 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 1, 32, NULL
);
1826 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
1827 nir_ssa_def
*buf_size
= &instr
->dest
.ssa
;
1829 /* array_length = max(buffer_size - offset, 0) / stride */
1830 nir_ssa_def
*array_length
=
1835 nir_imm_int(&b
->nb
, offset
)),
1836 nir_imm_int(&b
->nb
, 0u)),
1837 nir_imm_int(&b
->nb
, stride
));
1839 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1840 val
->ssa
= vtn_create_ssa_value(b
, glsl_uint_type());
1841 val
->ssa
->def
= array_length
;
1845 case SpvOpCopyMemorySized
:
1847 unreachable("Unhandled opcode");