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 "nir/nir_vla.h"
30 #include "nir/nir_control_flow.h"
31 #include "nir/nir_constant_expressions.h"
32 #include "nir/nir_deref.h"
33 #include "spirv_info.h"
35 #include "util/format/u_format.h"
36 #include "util/u_math.h"
39 #if UTIL_ARCH_BIG_ENDIAN
44 vtn_log(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
45 size_t spirv_offset
, const char *message
)
47 if (b
->options
->debug
.func
) {
48 b
->options
->debug
.func(b
->options
->debug
.private_data
,
49 level
, spirv_offset
, message
);
53 if (level
>= NIR_SPIRV_DEBUG_LEVEL_WARNING
)
54 fprintf(stderr
, "%s\n", message
);
59 vtn_logf(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
60 size_t spirv_offset
, const char *fmt
, ...)
66 msg
= ralloc_vasprintf(NULL
, fmt
, args
);
69 vtn_log(b
, level
, spirv_offset
, msg
);
75 vtn_log_err(struct vtn_builder
*b
,
76 enum nir_spirv_debug_level level
, const char *prefix
,
77 const char *file
, unsigned line
,
78 const char *fmt
, va_list args
)
82 msg
= ralloc_strdup(NULL
, prefix
);
85 ralloc_asprintf_append(&msg
, " In file %s:%u\n", file
, line
);
88 ralloc_asprintf_append(&msg
, " ");
90 ralloc_vasprintf_append(&msg
, fmt
, args
);
92 ralloc_asprintf_append(&msg
, "\n %zu bytes into the SPIR-V binary",
96 ralloc_asprintf_append(&msg
,
97 "\n in SPIR-V source file %s, line %d, col %d",
98 b
->file
, b
->line
, b
->col
);
101 vtn_log(b
, level
, b
->spirv_offset
, msg
);
107 vtn_dump_shader(struct vtn_builder
*b
, const char *path
, const char *prefix
)
112 int len
= snprintf(filename
, sizeof(filename
), "%s/%s-%d.spirv",
113 path
, prefix
, idx
++);
114 if (len
< 0 || len
>= sizeof(filename
))
117 FILE *f
= fopen(filename
, "w");
121 fwrite(b
->spirv
, sizeof(*b
->spirv
), b
->spirv_word_count
, f
);
124 vtn_info("SPIR-V shader dumped to %s", filename
);
128 _vtn_warn(struct vtn_builder
*b
, const char *file
, unsigned line
,
129 const char *fmt
, ...)
134 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_WARNING
, "SPIR-V WARNING:\n",
135 file
, line
, fmt
, args
);
140 _vtn_err(struct vtn_builder
*b
, const char *file
, unsigned line
,
141 const char *fmt
, ...)
146 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V ERROR:\n",
147 file
, line
, fmt
, args
);
152 _vtn_fail(struct vtn_builder
*b
, const char *file
, unsigned line
,
153 const char *fmt
, ...)
158 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V parsing FAILED:\n",
159 file
, line
, fmt
, args
);
162 const char *dump_path
= getenv("MESA_SPIRV_FAIL_DUMP_PATH");
164 vtn_dump_shader(b
, dump_path
, "fail");
166 longjmp(b
->fail_jump
, 1);
169 static struct vtn_ssa_value
*
170 vtn_undef_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
172 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
173 val
->type
= glsl_get_bare_type(type
);
175 if (glsl_type_is_vector_or_scalar(type
)) {
176 unsigned num_components
= glsl_get_vector_elements(val
->type
);
177 unsigned bit_size
= glsl_get_bit_size(val
->type
);
178 val
->def
= nir_ssa_undef(&b
->nb
, num_components
, bit_size
);
180 unsigned elems
= glsl_get_length(val
->type
);
181 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
182 if (glsl_type_is_array_or_matrix(type
)) {
183 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
184 for (unsigned i
= 0; i
< elems
; i
++)
185 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
187 vtn_assert(glsl_type_is_struct_or_ifc(type
));
188 for (unsigned i
= 0; i
< elems
; i
++) {
189 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
190 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
198 static struct vtn_ssa_value
*
199 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
200 const struct glsl_type
*type
)
202 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
207 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
208 val
->type
= glsl_get_bare_type(type
);
210 if (glsl_type_is_vector_or_scalar(type
)) {
211 unsigned num_components
= glsl_get_vector_elements(val
->type
);
212 unsigned bit_size
= glsl_get_bit_size(type
);
213 nir_load_const_instr
*load
=
214 nir_load_const_instr_create(b
->shader
, num_components
, bit_size
);
216 memcpy(load
->value
, constant
->values
,
217 sizeof(nir_const_value
) * num_components
);
219 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
220 val
->def
= &load
->def
;
222 unsigned elems
= glsl_get_length(val
->type
);
223 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
224 if (glsl_type_is_array_or_matrix(type
)) {
225 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
226 for (unsigned i
= 0; i
< elems
; i
++) {
227 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
231 vtn_assert(glsl_type_is_struct_or_ifc(type
));
232 for (unsigned i
= 0; i
< elems
; i
++) {
233 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
234 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
243 struct vtn_ssa_value
*
244 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
246 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
247 switch (val
->value_type
) {
248 case vtn_value_type_undef
:
249 return vtn_undef_ssa_value(b
, val
->type
->type
);
251 case vtn_value_type_constant
:
252 return vtn_const_ssa_value(b
, val
->constant
, val
->type
->type
);
254 case vtn_value_type_ssa
:
257 case vtn_value_type_pointer
:
258 vtn_assert(val
->pointer
->ptr_type
&& val
->pointer
->ptr_type
->type
);
259 struct vtn_ssa_value
*ssa
=
260 vtn_create_ssa_value(b
, val
->pointer
->ptr_type
->type
);
261 ssa
->def
= vtn_pointer_to_ssa(b
, val
->pointer
);
265 vtn_fail("Invalid type for an SSA value");
270 vtn_push_ssa_value(struct vtn_builder
*b
, uint32_t value_id
,
271 struct vtn_ssa_value
*ssa
)
273 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
275 /* See vtn_create_ssa_value */
276 vtn_fail_if(ssa
->type
!= glsl_get_bare_type(type
->type
),
277 "Type mismatch for SPIR-V SSA value");
279 struct vtn_value
*val
;
280 if (type
->base_type
== vtn_base_type_pointer
) {
281 val
= vtn_push_pointer(b
, value_id
, vtn_pointer_from_ssa(b
, ssa
->def
, type
));
283 /* Don't trip the value_type_ssa check in vtn_push_value */
284 val
= vtn_push_value(b
, value_id
, vtn_value_type_invalid
);
285 val
->value_type
= vtn_value_type_ssa
;
293 vtn_get_nir_ssa(struct vtn_builder
*b
, uint32_t value_id
)
295 struct vtn_ssa_value
*ssa
= vtn_ssa_value(b
, value_id
);
296 vtn_fail_if(!glsl_type_is_vector_or_scalar(ssa
->type
),
297 "Expected a vector or scalar type");
302 vtn_push_nir_ssa(struct vtn_builder
*b
, uint32_t value_id
, nir_ssa_def
*def
)
304 /* Types for all SPIR-V SSA values are set as part of a pre-pass so the
305 * type will be valid by the time we get here.
307 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
308 vtn_fail_if(def
->num_components
!= glsl_get_vector_elements(type
->type
) ||
309 def
->bit_size
!= glsl_get_bit_size(type
->type
),
310 "Mismatch between NIR and SPIR-V type.");
311 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, type
->type
);
313 return vtn_push_ssa_value(b
, value_id
, ssa
);
316 static nir_deref_instr
*
317 vtn_get_image(struct vtn_builder
*b
, uint32_t value_id
)
319 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
320 vtn_assert(type
->base_type
== vtn_base_type_image
);
321 return nir_build_deref_cast(&b
->nb
, vtn_get_nir_ssa(b
, value_id
),
322 nir_var_uniform
, type
->glsl_image
, 0);
326 vtn_push_image(struct vtn_builder
*b
, uint32_t value_id
,
327 nir_deref_instr
*deref
)
329 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
330 vtn_assert(type
->base_type
== vtn_base_type_image
);
331 vtn_push_nir_ssa(b
, value_id
, &deref
->dest
.ssa
);
334 static nir_deref_instr
*
335 vtn_get_sampler(struct vtn_builder
*b
, uint32_t value_id
)
337 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
338 vtn_assert(type
->base_type
== vtn_base_type_sampler
);
339 return nir_build_deref_cast(&b
->nb
, vtn_get_nir_ssa(b
, value_id
),
340 nir_var_uniform
, glsl_bare_sampler_type(), 0);
344 vtn_sampled_image_to_nir_ssa(struct vtn_builder
*b
,
345 struct vtn_sampled_image si
)
347 return nir_vec2(&b
->nb
, &si
.image
->dest
.ssa
, &si
.sampler
->dest
.ssa
);
351 vtn_push_sampled_image(struct vtn_builder
*b
, uint32_t value_id
,
352 struct vtn_sampled_image si
)
354 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
355 vtn_assert(type
->base_type
== vtn_base_type_sampled_image
);
356 vtn_push_nir_ssa(b
, value_id
, vtn_sampled_image_to_nir_ssa(b
, si
));
359 static struct vtn_sampled_image
360 vtn_get_sampled_image(struct vtn_builder
*b
, uint32_t value_id
)
362 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
363 vtn_assert(type
->base_type
== vtn_base_type_sampled_image
);
364 nir_ssa_def
*si_vec2
= vtn_get_nir_ssa(b
, value_id
);
366 struct vtn_sampled_image si
= { NULL
, };
367 si
.image
= nir_build_deref_cast(&b
->nb
, nir_channel(&b
->nb
, si_vec2
, 0),
369 type
->image
->glsl_image
, 0);
370 si
.sampler
= nir_build_deref_cast(&b
->nb
, nir_channel(&b
->nb
, si_vec2
, 1),
372 glsl_bare_sampler_type(), 0);
377 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
378 unsigned word_count
, unsigned *words_used
)
380 /* From the SPIR-V spec:
382 * "A string is interpreted as a nul-terminated stream of characters.
383 * The character set is Unicode in the UTF-8 encoding scheme. The UTF-8
384 * octets (8-bit bytes) are packed four per word, following the
385 * little-endian convention (i.e., the first octet is in the
386 * lowest-order 8 bits of the word). The final word contains the
387 * string’s nul-termination character (0), and all contents past the
388 * end of the string in the final word are padded with 0."
390 * On big-endian, we need to byte-swap.
392 #if UTIL_ARCH_BIG_ENDIAN
394 uint32_t *copy
= ralloc_array(b
, uint32_t, word_count
);
395 for (unsigned i
= 0; i
< word_count
; i
++)
396 copy
[i
] = bswap_32(words
[i
]);
401 const char *str
= (char *)words
;
402 const char *end
= memchr(str
, 0, word_count
* 4);
403 vtn_fail_if(end
== NULL
, "String is not null-terminated");
406 *words_used
= DIV_ROUND_UP(end
- str
+ 1, sizeof(*words
));
412 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
413 const uint32_t *end
, vtn_instruction_handler handler
)
419 const uint32_t *w
= start
;
421 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
422 unsigned count
= w
[0] >> SpvWordCountShift
;
423 vtn_assert(count
>= 1 && w
+ count
<= end
);
425 b
->spirv_offset
= (uint8_t *)w
- (uint8_t *)b
->spirv
;
429 break; /* Do nothing */
432 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
444 if (!handler(b
, opcode
, w
, count
))
462 vtn_handle_non_semantic_instruction(struct vtn_builder
*b
, SpvOp ext_opcode
,
463 const uint32_t *w
, unsigned count
)
470 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
471 const uint32_t *w
, unsigned count
)
474 case SpvOpExtInstImport
: {
475 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
476 const char *ext
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
477 if (strcmp(ext
, "GLSL.std.450") == 0) {
478 val
->ext_handler
= vtn_handle_glsl450_instruction
;
479 } else if ((strcmp(ext
, "SPV_AMD_gcn_shader") == 0)
480 && (b
->options
&& b
->options
->caps
.amd_gcn_shader
)) {
481 val
->ext_handler
= vtn_handle_amd_gcn_shader_instruction
;
482 } else if ((strcmp(ext
, "SPV_AMD_shader_ballot") == 0)
483 && (b
->options
&& b
->options
->caps
.amd_shader_ballot
)) {
484 val
->ext_handler
= vtn_handle_amd_shader_ballot_instruction
;
485 } else if ((strcmp(ext
, "SPV_AMD_shader_trinary_minmax") == 0)
486 && (b
->options
&& b
->options
->caps
.amd_trinary_minmax
)) {
487 val
->ext_handler
= vtn_handle_amd_shader_trinary_minmax_instruction
;
488 } else if ((strcmp(ext
, "SPV_AMD_shader_explicit_vertex_parameter") == 0)
489 && (b
->options
&& b
->options
->caps
.amd_shader_explicit_vertex_parameter
)) {
490 val
->ext_handler
= vtn_handle_amd_shader_explicit_vertex_parameter_instruction
;
491 } else if (strcmp(ext
, "OpenCL.std") == 0) {
492 val
->ext_handler
= vtn_handle_opencl_instruction
;
493 } else if (strstr(ext
, "NonSemantic.") == ext
) {
494 val
->ext_handler
= vtn_handle_non_semantic_instruction
;
496 vtn_fail("Unsupported extension: %s", ext
);
502 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
503 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
509 vtn_fail_with_opcode("Unhandled opcode", opcode
);
514 _foreach_decoration_helper(struct vtn_builder
*b
,
515 struct vtn_value
*base_value
,
517 struct vtn_value
*value
,
518 vtn_decoration_foreach_cb cb
, void *data
)
520 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
522 if (dec
->scope
== VTN_DEC_DECORATION
) {
523 member
= parent_member
;
524 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
525 vtn_fail_if(value
->value_type
!= vtn_value_type_type
||
526 value
->type
->base_type
!= vtn_base_type_struct
,
527 "OpMemberDecorate and OpGroupMemberDecorate are only "
528 "allowed on OpTypeStruct");
529 /* This means we haven't recursed yet */
530 assert(value
== base_value
);
532 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
534 vtn_fail_if(member
>= base_value
->type
->length
,
535 "OpMemberDecorate specifies member %d but the "
536 "OpTypeStruct has only %u members",
537 member
, base_value
->type
->length
);
539 /* Not a decoration */
540 assert(dec
->scope
== VTN_DEC_EXECUTION_MODE
);
545 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
546 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
549 cb(b
, base_value
, member
, dec
, data
);
554 /** Iterates (recursively if needed) over all of the decorations on a value
556 * This function iterates over all of the decorations applied to a given
557 * value. If it encounters a decoration group, it recurses into the group
558 * and iterates over all of those decorations as well.
561 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
562 vtn_decoration_foreach_cb cb
, void *data
)
564 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
568 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
569 vtn_execution_mode_foreach_cb cb
, void *data
)
571 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
572 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
575 assert(dec
->group
== NULL
);
576 cb(b
, value
, dec
, data
);
581 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
582 const uint32_t *w
, unsigned count
)
584 const uint32_t *w_end
= w
+ count
;
585 const uint32_t target
= w
[1];
589 case SpvOpDecorationGroup
:
590 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
594 case SpvOpDecorateId
:
595 case SpvOpMemberDecorate
:
596 case SpvOpDecorateString
:
597 case SpvOpMemberDecorateString
:
598 case SpvOpExecutionMode
:
599 case SpvOpExecutionModeId
: {
600 struct vtn_value
*val
= vtn_untyped_value(b
, target
);
602 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
605 case SpvOpDecorateId
:
606 case SpvOpDecorateString
:
607 dec
->scope
= VTN_DEC_DECORATION
;
609 case SpvOpMemberDecorate
:
610 case SpvOpMemberDecorateString
:
611 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
612 vtn_fail_if(dec
->scope
< VTN_DEC_STRUCT_MEMBER0
, /* overflow */
613 "Member argument of OpMemberDecorate too large");
615 case SpvOpExecutionMode
:
616 case SpvOpExecutionModeId
:
617 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
620 unreachable("Invalid decoration opcode");
622 dec
->decoration
= *(w
++);
625 /* Link into the list */
626 dec
->next
= val
->decoration
;
627 val
->decoration
= dec
;
631 case SpvOpGroupMemberDecorate
:
632 case SpvOpGroupDecorate
: {
633 struct vtn_value
*group
=
634 vtn_value(b
, target
, vtn_value_type_decoration_group
);
636 for (; w
< w_end
; w
++) {
637 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
638 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
641 if (opcode
== SpvOpGroupDecorate
) {
642 dec
->scope
= VTN_DEC_DECORATION
;
644 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
645 vtn_fail_if(dec
->scope
< 0, /* Check for overflow */
646 "Member argument of OpGroupMemberDecorate too large");
649 /* Link into the list */
650 dec
->next
= val
->decoration
;
651 val
->decoration
= dec
;
657 unreachable("Unhandled opcode");
661 struct member_decoration_ctx
{
663 struct glsl_struct_field
*fields
;
664 struct vtn_type
*type
;
668 * Returns true if the given type contains a struct decorated Block or
672 vtn_type_contains_block(struct vtn_builder
*b
, struct vtn_type
*type
)
674 switch (type
->base_type
) {
675 case vtn_base_type_array
:
676 return vtn_type_contains_block(b
, type
->array_element
);
677 case vtn_base_type_struct
:
678 if (type
->block
|| type
->buffer_block
)
680 for (unsigned i
= 0; i
< type
->length
; i
++) {
681 if (vtn_type_contains_block(b
, type
->members
[i
]))
690 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
691 * OpStore, or OpCopyMemory between them without breaking anything.
692 * Technically, the SPIR-V rules require the exact same type ID but this lets
693 * us internally be a bit looser.
696 vtn_types_compatible(struct vtn_builder
*b
,
697 struct vtn_type
*t1
, struct vtn_type
*t2
)
699 if (t1
->id
== t2
->id
)
702 if (t1
->base_type
!= t2
->base_type
)
705 switch (t1
->base_type
) {
706 case vtn_base_type_void
:
707 case vtn_base_type_scalar
:
708 case vtn_base_type_vector
:
709 case vtn_base_type_matrix
:
710 case vtn_base_type_image
:
711 case vtn_base_type_sampler
:
712 case vtn_base_type_sampled_image
:
713 return t1
->type
== t2
->type
;
715 case vtn_base_type_array
:
716 return t1
->length
== t2
->length
&&
717 vtn_types_compatible(b
, t1
->array_element
, t2
->array_element
);
719 case vtn_base_type_pointer
:
720 return vtn_types_compatible(b
, t1
->deref
, t2
->deref
);
722 case vtn_base_type_struct
:
723 if (t1
->length
!= t2
->length
)
726 for (unsigned i
= 0; i
< t1
->length
; i
++) {
727 if (!vtn_types_compatible(b
, t1
->members
[i
], t2
->members
[i
]))
732 case vtn_base_type_function
:
733 /* This case shouldn't get hit since you can't copy around function
734 * types. Just require them to be identical.
739 vtn_fail("Invalid base type");
743 vtn_type_without_array(struct vtn_type
*type
)
745 while (type
->base_type
== vtn_base_type_array
)
746 type
= type
->array_element
;
750 /* does a shallow copy of a vtn_type */
752 static struct vtn_type
*
753 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
755 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
758 switch (src
->base_type
) {
759 case vtn_base_type_void
:
760 case vtn_base_type_scalar
:
761 case vtn_base_type_vector
:
762 case vtn_base_type_matrix
:
763 case vtn_base_type_array
:
764 case vtn_base_type_pointer
:
765 case vtn_base_type_image
:
766 case vtn_base_type_sampler
:
767 case vtn_base_type_sampled_image
:
768 /* Nothing more to do */
771 case vtn_base_type_struct
:
772 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
773 memcpy(dest
->members
, src
->members
,
774 src
->length
* sizeof(src
->members
[0]));
776 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
777 memcpy(dest
->offsets
, src
->offsets
,
778 src
->length
* sizeof(src
->offsets
[0]));
781 case vtn_base_type_function
:
782 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
783 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
790 static const struct glsl_type
*
791 wrap_type_in_array(const struct glsl_type
*type
,
792 const struct glsl_type
*array_type
)
794 if (!glsl_type_is_array(array_type
))
797 const struct glsl_type
*elem_type
=
798 wrap_type_in_array(type
, glsl_get_array_element(array_type
));
799 return glsl_array_type(elem_type
, glsl_get_length(array_type
),
800 glsl_get_explicit_stride(array_type
));
804 vtn_type_needs_explicit_layout(struct vtn_builder
*b
, enum vtn_variable_mode mode
)
806 /* For OpenCL we never want to strip the info from the types, and it makes
807 * type comparisons easier in later stages.
809 if (b
->options
->environment
== NIR_SPIRV_OPENCL
)
813 case vtn_variable_mode_input
:
814 case vtn_variable_mode_output
:
815 /* Layout decorations kept because we need offsets for XFB arrays of
818 return b
->shader
->info
.has_transform_feedback_varyings
;
820 case vtn_variable_mode_ssbo
:
821 case vtn_variable_mode_phys_ssbo
:
822 case vtn_variable_mode_ubo
:
830 const struct glsl_type
*
831 vtn_type_get_nir_type(struct vtn_builder
*b
, struct vtn_type
*type
,
832 enum vtn_variable_mode mode
)
834 if (mode
== vtn_variable_mode_atomic_counter
) {
835 vtn_fail_if(glsl_without_array(type
->type
) != glsl_uint_type(),
836 "Variables in the AtomicCounter storage class should be "
837 "(possibly arrays of arrays of) uint.");
838 return wrap_type_in_array(glsl_atomic_uint_type(), type
->type
);
841 if (mode
== vtn_variable_mode_uniform
) {
842 switch (type
->base_type
) {
843 case vtn_base_type_array
: {
844 const struct glsl_type
*elem_type
=
845 vtn_type_get_nir_type(b
, type
->array_element
, mode
);
847 return glsl_array_type(elem_type
, type
->length
,
848 glsl_get_explicit_stride(type
->type
));
851 case vtn_base_type_struct
: {
852 bool need_new_struct
= false;
853 const uint32_t num_fields
= type
->length
;
854 NIR_VLA(struct glsl_struct_field
, fields
, num_fields
);
855 for (unsigned i
= 0; i
< num_fields
; i
++) {
856 fields
[i
] = *glsl_get_struct_field_data(type
->type
, i
);
857 const struct glsl_type
*field_nir_type
=
858 vtn_type_get_nir_type(b
, type
->members
[i
], mode
);
859 if (fields
[i
].type
!= field_nir_type
) {
860 fields
[i
].type
= field_nir_type
;
861 need_new_struct
= true;
864 if (need_new_struct
) {
865 if (glsl_type_is_interface(type
->type
)) {
866 return glsl_interface_type(fields
, num_fields
,
867 /* packing */ 0, false,
868 glsl_get_type_name(type
->type
));
870 return glsl_struct_type(fields
, num_fields
,
871 glsl_get_type_name(type
->type
),
872 glsl_struct_type_is_packed(type
->type
));
875 /* No changes, just pass it on */
880 case vtn_base_type_image
:
881 return type
->glsl_image
;
883 case vtn_base_type_sampler
:
884 return glsl_bare_sampler_type();
886 case vtn_base_type_sampled_image
:
887 return type
->image
->glsl_image
;
894 /* Layout decorations are allowed but ignored in certain conditions,
895 * to allow SPIR-V generators perform type deduplication. Discard
896 * unnecessary ones when passing to NIR.
898 if (!vtn_type_needs_explicit_layout(b
, mode
))
899 return glsl_get_bare_type(type
->type
);
904 static struct vtn_type
*
905 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
907 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
908 type
= type
->members
[member
];
910 /* We may have an array of matrices.... Oh, joy! */
911 while (glsl_type_is_array(type
->type
)) {
912 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
913 type
= type
->array_element
;
916 vtn_assert(glsl_type_is_matrix(type
->type
));
922 vtn_handle_access_qualifier(struct vtn_builder
*b
, struct vtn_type
*type
,
923 int member
, enum gl_access_qualifier access
)
925 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
926 type
= type
->members
[member
];
928 type
->access
|= access
;
932 array_stride_decoration_cb(struct vtn_builder
*b
,
933 struct vtn_value
*val
, int member
,
934 const struct vtn_decoration
*dec
, void *void_ctx
)
936 struct vtn_type
*type
= val
->type
;
938 if (dec
->decoration
== SpvDecorationArrayStride
) {
939 if (vtn_type_contains_block(b
, type
)) {
940 vtn_warn("The ArrayStride decoration cannot be applied to an array "
941 "type which contains a structure type decorated Block "
943 /* Ignore the decoration */
945 vtn_fail_if(dec
->operands
[0] == 0, "ArrayStride must be non-zero");
946 type
->stride
= dec
->operands
[0];
952 struct_member_decoration_cb(struct vtn_builder
*b
,
953 UNUSED
struct vtn_value
*val
, int member
,
954 const struct vtn_decoration
*dec
, void *void_ctx
)
956 struct member_decoration_ctx
*ctx
= void_ctx
;
961 assert(member
< ctx
->num_fields
);
963 switch (dec
->decoration
) {
964 case SpvDecorationRelaxedPrecision
:
965 case SpvDecorationUniform
:
966 case SpvDecorationUniformId
:
967 break; /* FIXME: Do nothing with this for now. */
968 case SpvDecorationNonWritable
:
969 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_WRITEABLE
);
971 case SpvDecorationNonReadable
:
972 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_READABLE
);
974 case SpvDecorationVolatile
:
975 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_VOLATILE
);
977 case SpvDecorationCoherent
:
978 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_COHERENT
);
980 case SpvDecorationNoPerspective
:
981 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
983 case SpvDecorationFlat
:
984 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
986 case SpvDecorationExplicitInterpAMD
:
987 ctx
->fields
[member
].interpolation
= INTERP_MODE_EXPLICIT
;
989 case SpvDecorationCentroid
:
990 ctx
->fields
[member
].centroid
= true;
992 case SpvDecorationSample
:
993 ctx
->fields
[member
].sample
= true;
995 case SpvDecorationStream
:
996 /* This is handled later by var_decoration_cb in vtn_variables.c */
998 case SpvDecorationLocation
:
999 ctx
->fields
[member
].location
= dec
->operands
[0];
1001 case SpvDecorationComponent
:
1002 break; /* FIXME: What should we do with these? */
1003 case SpvDecorationBuiltIn
:
1004 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
1005 ctx
->type
->members
[member
]->is_builtin
= true;
1006 ctx
->type
->members
[member
]->builtin
= dec
->operands
[0];
1007 ctx
->type
->builtin_block
= true;
1009 case SpvDecorationOffset
:
1010 ctx
->type
->offsets
[member
] = dec
->operands
[0];
1011 ctx
->fields
[member
].offset
= dec
->operands
[0];
1013 case SpvDecorationMatrixStride
:
1014 /* Handled as a second pass */
1016 case SpvDecorationColMajor
:
1017 break; /* Nothing to do here. Column-major is the default. */
1018 case SpvDecorationRowMajor
:
1019 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
1022 case SpvDecorationPatch
:
1025 case SpvDecorationSpecId
:
1026 case SpvDecorationBlock
:
1027 case SpvDecorationBufferBlock
:
1028 case SpvDecorationArrayStride
:
1029 case SpvDecorationGLSLShared
:
1030 case SpvDecorationGLSLPacked
:
1031 case SpvDecorationInvariant
:
1032 case SpvDecorationRestrict
:
1033 case SpvDecorationAliased
:
1034 case SpvDecorationConstant
:
1035 case SpvDecorationIndex
:
1036 case SpvDecorationBinding
:
1037 case SpvDecorationDescriptorSet
:
1038 case SpvDecorationLinkageAttributes
:
1039 case SpvDecorationNoContraction
:
1040 case SpvDecorationInputAttachmentIndex
:
1041 vtn_warn("Decoration not allowed on struct members: %s",
1042 spirv_decoration_to_string(dec
->decoration
));
1045 case SpvDecorationXfbBuffer
:
1046 case SpvDecorationXfbStride
:
1047 /* This is handled later by var_decoration_cb in vtn_variables.c */
1050 case SpvDecorationCPacked
:
1051 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
1052 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1053 spirv_decoration_to_string(dec
->decoration
));
1055 ctx
->type
->packed
= true;
1058 case SpvDecorationSaturatedConversion
:
1059 case SpvDecorationFuncParamAttr
:
1060 case SpvDecorationFPRoundingMode
:
1061 case SpvDecorationFPFastMathMode
:
1062 case SpvDecorationAlignment
:
1063 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
1064 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1065 spirv_decoration_to_string(dec
->decoration
));
1069 case SpvDecorationUserSemantic
:
1070 case SpvDecorationUserTypeGOOGLE
:
1071 /* User semantic decorations can safely be ignored by the driver. */
1075 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1079 /** Chases the array type all the way down to the tail and rewrites the
1080 * glsl_types to be based off the tail's glsl_type.
1083 vtn_array_type_rewrite_glsl_type(struct vtn_type
*type
)
1085 if (type
->base_type
!= vtn_base_type_array
)
1088 vtn_array_type_rewrite_glsl_type(type
->array_element
);
1090 type
->type
= glsl_array_type(type
->array_element
->type
,
1091 type
->length
, type
->stride
);
1094 /* Matrix strides are handled as a separate pass because we need to know
1095 * whether the matrix is row-major or not first.
1098 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
1099 UNUSED
struct vtn_value
*val
, int member
,
1100 const struct vtn_decoration
*dec
,
1103 if (dec
->decoration
!= SpvDecorationMatrixStride
)
1106 vtn_fail_if(member
< 0,
1107 "The MatrixStride decoration is only allowed on members "
1109 vtn_fail_if(dec
->operands
[0] == 0, "MatrixStride must be non-zero");
1111 struct member_decoration_ctx
*ctx
= void_ctx
;
1113 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
1114 if (mat_type
->row_major
) {
1115 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
1116 mat_type
->stride
= mat_type
->array_element
->stride
;
1117 mat_type
->array_element
->stride
= dec
->operands
[0];
1119 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
1120 dec
->operands
[0], true);
1121 mat_type
->array_element
->type
= glsl_get_column_type(mat_type
->type
);
1123 vtn_assert(mat_type
->array_element
->stride
> 0);
1124 mat_type
->stride
= dec
->operands
[0];
1126 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
1127 dec
->operands
[0], false);
1130 /* Now that we've replaced the glsl_type with a properly strided matrix
1131 * type, rewrite the member type so that it's an array of the proper kind
1134 vtn_array_type_rewrite_glsl_type(ctx
->type
->members
[member
]);
1135 ctx
->fields
[member
].type
= ctx
->type
->members
[member
]->type
;
1139 struct_block_decoration_cb(struct vtn_builder
*b
,
1140 struct vtn_value
*val
, int member
,
1141 const struct vtn_decoration
*dec
, void *ctx
)
1146 struct vtn_type
*type
= val
->type
;
1147 if (dec
->decoration
== SpvDecorationBlock
)
1149 else if (dec
->decoration
== SpvDecorationBufferBlock
)
1150 type
->buffer_block
= true;
1154 type_decoration_cb(struct vtn_builder
*b
,
1155 struct vtn_value
*val
, int member
,
1156 const struct vtn_decoration
*dec
, UNUSED
void *ctx
)
1158 struct vtn_type
*type
= val
->type
;
1161 /* This should have been handled by OpTypeStruct */
1162 assert(val
->type
->base_type
== vtn_base_type_struct
);
1163 assert(member
>= 0 && member
< val
->type
->length
);
1167 switch (dec
->decoration
) {
1168 case SpvDecorationArrayStride
:
1169 vtn_assert(type
->base_type
== vtn_base_type_array
||
1170 type
->base_type
== vtn_base_type_pointer
);
1172 case SpvDecorationBlock
:
1173 vtn_assert(type
->base_type
== vtn_base_type_struct
);
1174 vtn_assert(type
->block
);
1176 case SpvDecorationBufferBlock
:
1177 vtn_assert(type
->base_type
== vtn_base_type_struct
);
1178 vtn_assert(type
->buffer_block
);
1180 case SpvDecorationGLSLShared
:
1181 case SpvDecorationGLSLPacked
:
1182 /* Ignore these, since we get explicit offsets anyways */
1185 case SpvDecorationRowMajor
:
1186 case SpvDecorationColMajor
:
1187 case SpvDecorationMatrixStride
:
1188 case SpvDecorationBuiltIn
:
1189 case SpvDecorationNoPerspective
:
1190 case SpvDecorationFlat
:
1191 case SpvDecorationPatch
:
1192 case SpvDecorationCentroid
:
1193 case SpvDecorationSample
:
1194 case SpvDecorationExplicitInterpAMD
:
1195 case SpvDecorationVolatile
:
1196 case SpvDecorationCoherent
:
1197 case SpvDecorationNonWritable
:
1198 case SpvDecorationNonReadable
:
1199 case SpvDecorationUniform
:
1200 case SpvDecorationUniformId
:
1201 case SpvDecorationLocation
:
1202 case SpvDecorationComponent
:
1203 case SpvDecorationOffset
:
1204 case SpvDecorationXfbBuffer
:
1205 case SpvDecorationXfbStride
:
1206 case SpvDecorationUserSemantic
:
1207 vtn_warn("Decoration only allowed for struct members: %s",
1208 spirv_decoration_to_string(dec
->decoration
));
1211 case SpvDecorationStream
:
1212 /* We don't need to do anything here, as stream is filled up when
1213 * aplying the decoration to a variable, just check that if it is not a
1214 * struct member, it should be a struct.
1216 vtn_assert(type
->base_type
== vtn_base_type_struct
);
1219 case SpvDecorationRelaxedPrecision
:
1220 case SpvDecorationSpecId
:
1221 case SpvDecorationInvariant
:
1222 case SpvDecorationRestrict
:
1223 case SpvDecorationAliased
:
1224 case SpvDecorationConstant
:
1225 case SpvDecorationIndex
:
1226 case SpvDecorationBinding
:
1227 case SpvDecorationDescriptorSet
:
1228 case SpvDecorationLinkageAttributes
:
1229 case SpvDecorationNoContraction
:
1230 case SpvDecorationInputAttachmentIndex
:
1231 vtn_warn("Decoration not allowed on types: %s",
1232 spirv_decoration_to_string(dec
->decoration
));
1235 case SpvDecorationCPacked
:
1236 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
1237 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1238 spirv_decoration_to_string(dec
->decoration
));
1240 type
->packed
= true;
1243 case SpvDecorationSaturatedConversion
:
1244 case SpvDecorationFuncParamAttr
:
1245 case SpvDecorationFPRoundingMode
:
1246 case SpvDecorationFPFastMathMode
:
1247 case SpvDecorationAlignment
:
1248 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1249 spirv_decoration_to_string(dec
->decoration
));
1252 case SpvDecorationUserTypeGOOGLE
:
1253 /* User semantic decorations can safely be ignored by the driver. */
1257 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1262 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
1265 case SpvImageFormatUnknown
: return PIPE_FORMAT_NONE
;
1266 case SpvImageFormatRgba32f
: return PIPE_FORMAT_R32G32B32A32_FLOAT
;
1267 case SpvImageFormatRgba16f
: return PIPE_FORMAT_R16G16B16A16_FLOAT
;
1268 case SpvImageFormatR32f
: return PIPE_FORMAT_R32_FLOAT
;
1269 case SpvImageFormatRgba8
: return PIPE_FORMAT_R8G8B8A8_UNORM
;
1270 case SpvImageFormatRgba8Snorm
: return PIPE_FORMAT_R8G8B8A8_SNORM
;
1271 case SpvImageFormatRg32f
: return PIPE_FORMAT_R32G32_FLOAT
;
1272 case SpvImageFormatRg16f
: return PIPE_FORMAT_R16G16_FLOAT
;
1273 case SpvImageFormatR11fG11fB10f
: return PIPE_FORMAT_R11G11B10_FLOAT
;
1274 case SpvImageFormatR16f
: return PIPE_FORMAT_R16_FLOAT
;
1275 case SpvImageFormatRgba16
: return PIPE_FORMAT_R16G16B16A16_UNORM
;
1276 case SpvImageFormatRgb10A2
: return PIPE_FORMAT_R10G10B10A2_UNORM
;
1277 case SpvImageFormatRg16
: return PIPE_FORMAT_R16G16_UNORM
;
1278 case SpvImageFormatRg8
: return PIPE_FORMAT_R8G8_UNORM
;
1279 case SpvImageFormatR16
: return PIPE_FORMAT_R16_UNORM
;
1280 case SpvImageFormatR8
: return PIPE_FORMAT_R8_UNORM
;
1281 case SpvImageFormatRgba16Snorm
: return PIPE_FORMAT_R16G16B16A16_SNORM
;
1282 case SpvImageFormatRg16Snorm
: return PIPE_FORMAT_R16G16_SNORM
;
1283 case SpvImageFormatRg8Snorm
: return PIPE_FORMAT_R8G8_SNORM
;
1284 case SpvImageFormatR16Snorm
: return PIPE_FORMAT_R16_SNORM
;
1285 case SpvImageFormatR8Snorm
: return PIPE_FORMAT_R8_SNORM
;
1286 case SpvImageFormatRgba32i
: return PIPE_FORMAT_R32G32B32A32_SINT
;
1287 case SpvImageFormatRgba16i
: return PIPE_FORMAT_R16G16B16A16_SINT
;
1288 case SpvImageFormatRgba8i
: return PIPE_FORMAT_R8G8B8A8_SINT
;
1289 case SpvImageFormatR32i
: return PIPE_FORMAT_R32_SINT
;
1290 case SpvImageFormatRg32i
: return PIPE_FORMAT_R32G32_SINT
;
1291 case SpvImageFormatRg16i
: return PIPE_FORMAT_R16G16_SINT
;
1292 case SpvImageFormatRg8i
: return PIPE_FORMAT_R8G8_SINT
;
1293 case SpvImageFormatR16i
: return PIPE_FORMAT_R16_SINT
;
1294 case SpvImageFormatR8i
: return PIPE_FORMAT_R8_SINT
;
1295 case SpvImageFormatRgba32ui
: return PIPE_FORMAT_R32G32B32A32_UINT
;
1296 case SpvImageFormatRgba16ui
: return PIPE_FORMAT_R16G16B16A16_UINT
;
1297 case SpvImageFormatRgba8ui
: return PIPE_FORMAT_R8G8B8A8_UINT
;
1298 case SpvImageFormatR32ui
: return PIPE_FORMAT_R32_UINT
;
1299 case SpvImageFormatRgb10a2ui
: return PIPE_FORMAT_R10G10B10A2_UINT
;
1300 case SpvImageFormatRg32ui
: return PIPE_FORMAT_R32G32_UINT
;
1301 case SpvImageFormatRg16ui
: return PIPE_FORMAT_R16G16_UINT
;
1302 case SpvImageFormatRg8ui
: return PIPE_FORMAT_R8G8_UINT
;
1303 case SpvImageFormatR16ui
: return PIPE_FORMAT_R16_UINT
;
1304 case SpvImageFormatR8ui
: return PIPE_FORMAT_R8_UINT
;
1306 vtn_fail("Invalid image format: %s (%u)",
1307 spirv_imageformat_to_string(format
), format
);
1312 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
1313 const uint32_t *w
, unsigned count
)
1315 struct vtn_value
*val
= NULL
;
1317 /* In order to properly handle forward declarations, we have to defer
1318 * allocation for pointer types.
1320 if (opcode
!= SpvOpTypePointer
&& opcode
!= SpvOpTypeForwardPointer
) {
1321 val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1322 vtn_fail_if(val
->type
!= NULL
,
1323 "Only pointers can have forward declarations");
1324 val
->type
= rzalloc(b
, struct vtn_type
);
1325 val
->type
->id
= w
[1];
1330 val
->type
->base_type
= vtn_base_type_void
;
1331 val
->type
->type
= glsl_void_type();
1334 val
->type
->base_type
= vtn_base_type_scalar
;
1335 val
->type
->type
= glsl_bool_type();
1336 val
->type
->length
= 1;
1338 case SpvOpTypeInt
: {
1339 int bit_size
= w
[2];
1340 const bool signedness
= w
[3];
1341 val
->type
->base_type
= vtn_base_type_scalar
;
1344 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1347 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1350 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1353 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1356 vtn_fail("Invalid int bit size: %u", bit_size
);
1358 val
->type
->length
= 1;
1362 case SpvOpTypeFloat
: {
1363 int bit_size
= w
[2];
1364 val
->type
->base_type
= vtn_base_type_scalar
;
1367 val
->type
->type
= glsl_float16_t_type();
1370 val
->type
->type
= glsl_float_type();
1373 val
->type
->type
= glsl_double_type();
1376 vtn_fail("Invalid float bit size: %u", bit_size
);
1378 val
->type
->length
= 1;
1382 case SpvOpTypeVector
: {
1383 struct vtn_type
*base
= vtn_get_type(b
, w
[2]);
1384 unsigned elems
= w
[3];
1386 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1387 "Base type for OpTypeVector must be a scalar");
1388 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1389 "Invalid component count for OpTypeVector");
1391 val
->type
->base_type
= vtn_base_type_vector
;
1392 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1393 val
->type
->length
= elems
;
1394 val
->type
->stride
= glsl_type_is_boolean(val
->type
->type
)
1395 ? 4 : glsl_get_bit_size(base
->type
) / 8;
1396 val
->type
->array_element
= base
;
1400 case SpvOpTypeMatrix
: {
1401 struct vtn_type
*base
= vtn_get_type(b
, w
[2]);
1402 unsigned columns
= w
[3];
1404 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1405 "Base type for OpTypeMatrix must be a vector");
1406 vtn_fail_if(columns
< 2 || columns
> 4,
1407 "Invalid column count for OpTypeMatrix");
1409 val
->type
->base_type
= vtn_base_type_matrix
;
1410 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1411 glsl_get_vector_elements(base
->type
),
1413 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1414 "Unsupported base type for OpTypeMatrix");
1415 assert(!glsl_type_is_error(val
->type
->type
));
1416 val
->type
->length
= columns
;
1417 val
->type
->array_element
= base
;
1418 val
->type
->row_major
= false;
1419 val
->type
->stride
= 0;
1423 case SpvOpTypeRuntimeArray
:
1424 case SpvOpTypeArray
: {
1425 struct vtn_type
*array_element
= vtn_get_type(b
, w
[2]);
1427 if (opcode
== SpvOpTypeRuntimeArray
) {
1428 /* A length of 0 is used to denote unsized arrays */
1429 val
->type
->length
= 0;
1431 val
->type
->length
= vtn_constant_uint(b
, w
[3]);
1434 val
->type
->base_type
= vtn_base_type_array
;
1435 val
->type
->array_element
= array_element
;
1436 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
1437 val
->type
->stride
= glsl_get_cl_size(array_element
->type
);
1439 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1440 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
,
1445 case SpvOpTypeStruct
: {
1446 unsigned num_fields
= count
- 2;
1447 val
->type
->base_type
= vtn_base_type_struct
;
1448 val
->type
->length
= num_fields
;
1449 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1450 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1451 val
->type
->packed
= false;
1453 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1454 for (unsigned i
= 0; i
< num_fields
; i
++) {
1455 val
->type
->members
[i
] = vtn_get_type(b
, w
[i
+ 2]);
1456 fields
[i
] = (struct glsl_struct_field
) {
1457 .type
= val
->type
->members
[i
]->type
,
1458 .name
= ralloc_asprintf(b
, "field%d", i
),
1464 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1465 unsigned offset
= 0;
1466 for (unsigned i
= 0; i
< num_fields
; i
++) {
1467 offset
= align(offset
, glsl_get_cl_alignment(fields
[i
].type
));
1468 fields
[i
].offset
= offset
;
1469 offset
+= glsl_get_cl_size(fields
[i
].type
);
1473 struct member_decoration_ctx ctx
= {
1474 .num_fields
= num_fields
,
1479 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1480 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1482 vtn_foreach_decoration(b
, val
, struct_block_decoration_cb
, NULL
);
1484 const char *name
= val
->name
;
1486 if (val
->type
->block
|| val
->type
->buffer_block
) {
1487 /* Packing will be ignored since types coming from SPIR-V are
1488 * explicitly laid out.
1490 val
->type
->type
= glsl_interface_type(fields
, num_fields
,
1491 /* packing */ 0, false,
1492 name
? name
: "block");
1494 val
->type
->type
= glsl_struct_type(fields
, num_fields
,
1495 name
? name
: "struct", false);
1500 case SpvOpTypeFunction
: {
1501 val
->type
->base_type
= vtn_base_type_function
;
1502 val
->type
->type
= NULL
;
1504 val
->type
->return_type
= vtn_get_type(b
, w
[2]);
1506 const unsigned num_params
= count
- 3;
1507 val
->type
->length
= num_params
;
1508 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1509 for (unsigned i
= 0; i
< count
- 3; i
++) {
1510 val
->type
->params
[i
] = vtn_get_type(b
, w
[i
+ 3]);
1515 case SpvOpTypePointer
:
1516 case SpvOpTypeForwardPointer
: {
1517 /* We can't blindly push the value because it might be a forward
1520 val
= vtn_untyped_value(b
, w
[1]);
1522 SpvStorageClass storage_class
= w
[2];
1524 if (val
->value_type
== vtn_value_type_invalid
) {
1525 val
->value_type
= vtn_value_type_type
;
1526 val
->type
= rzalloc(b
, struct vtn_type
);
1527 val
->type
->id
= w
[1];
1528 val
->type
->base_type
= vtn_base_type_pointer
;
1529 val
->type
->storage_class
= storage_class
;
1531 /* These can actually be stored to nir_variables and used as SSA
1532 * values so they need a real glsl_type.
1534 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1535 b
, storage_class
, NULL
, NULL
);
1536 val
->type
->type
= nir_address_format_to_glsl_type(
1537 vtn_mode_to_address_format(b
, mode
));
1539 vtn_fail_if(val
->type
->storage_class
!= storage_class
,
1540 "The storage classes of an OpTypePointer and any "
1541 "OpTypeForwardPointers that provide forward "
1542 "declarations of it must match.");
1545 if (opcode
== SpvOpTypePointer
) {
1546 vtn_fail_if(val
->type
->deref
!= NULL
,
1547 "While OpTypeForwardPointer can be used to provide a "
1548 "forward declaration of a pointer, OpTypePointer can "
1549 "only be used once for a given id.");
1551 val
->type
->deref
= vtn_get_type(b
, w
[3]);
1553 /* Only certain storage classes use ArrayStride. The others (in
1554 * particular Workgroup) are expected to be laid out by the driver.
1556 switch (storage_class
) {
1557 case SpvStorageClassUniform
:
1558 case SpvStorageClassPushConstant
:
1559 case SpvStorageClassStorageBuffer
:
1560 case SpvStorageClassPhysicalStorageBuffer
:
1561 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1564 /* Nothing to do. */
1568 if (b
->physical_ptrs
) {
1569 switch (storage_class
) {
1570 case SpvStorageClassFunction
:
1571 case SpvStorageClassWorkgroup
:
1572 case SpvStorageClassCrossWorkgroup
:
1573 case SpvStorageClassUniformConstant
:
1574 val
->type
->stride
= align(glsl_get_cl_size(val
->type
->deref
->type
),
1575 glsl_get_cl_alignment(val
->type
->deref
->type
));
1585 case SpvOpTypeImage
: {
1586 val
->type
->base_type
= vtn_base_type_image
;
1588 /* Images are represented in NIR as a scalar SSA value that is the
1589 * result of a deref instruction. An OpLoad on an OpTypeImage pointer
1590 * from UniformConstant memory just takes the NIR deref from the pointer
1591 * and turns it into an SSA value.
1593 val
->type
->type
= nir_address_format_to_glsl_type(
1594 vtn_mode_to_address_format(b
, vtn_variable_mode_function
));
1596 const struct vtn_type
*sampled_type
= vtn_get_type(b
, w
[2]);
1597 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1598 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_void
,
1599 "Sampled type of OpTypeImage must be void for kernels");
1601 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1602 glsl_get_bit_size(sampled_type
->type
) != 32,
1603 "Sampled type of OpTypeImage must be a 32-bit scalar");
1606 enum glsl_sampler_dim dim
;
1607 switch ((SpvDim
)w
[3]) {
1608 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1609 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1610 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1611 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1612 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1613 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1614 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1616 vtn_fail("Invalid SPIR-V image dimensionality: %s (%u)",
1617 spirv_dim_to_string((SpvDim
)w
[3]), w
[3]);
1620 /* w[4]: as per Vulkan spec "Validation Rules within a Module",
1621 * The “Depth” operand of OpTypeImage is ignored.
1623 bool is_array
= w
[5];
1624 bool multisampled
= w
[6];
1625 unsigned sampled
= w
[7];
1626 SpvImageFormat format
= w
[8];
1629 val
->type
->access_qualifier
= w
[9];
1630 else if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
1631 /* Per the CL C spec: If no qualifier is provided, read_only is assumed. */
1632 val
->type
->access_qualifier
= SpvAccessQualifierReadOnly
;
1634 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1637 if (dim
== GLSL_SAMPLER_DIM_2D
)
1638 dim
= GLSL_SAMPLER_DIM_MS
;
1639 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1640 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1642 vtn_fail("Unsupported multisampled image type");
1645 val
->type
->image_format
= translate_image_format(b
, format
);
1647 enum glsl_base_type sampled_base_type
=
1648 glsl_get_base_type(sampled_type
->type
);
1650 val
->type
->glsl_image
= glsl_sampler_type(dim
, false, is_array
,
1652 } else if (sampled
== 2) {
1653 val
->type
->glsl_image
= glsl_image_type(dim
, is_array
,
1655 } else if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1656 val
->type
->glsl_image
= glsl_image_type(dim
, is_array
,
1659 vtn_fail("We need to know if the image will be sampled");
1664 case SpvOpTypeSampledImage
: {
1665 val
->type
->base_type
= vtn_base_type_sampled_image
;
1666 val
->type
->image
= vtn_get_type(b
, w
[2]);
1668 /* Sampled images are represented NIR as a vec2 SSA value where each
1669 * component is the result of a deref instruction. The first component
1670 * is the image and the second is the sampler. An OpLoad on an
1671 * OpTypeSampledImage pointer from UniformConstant memory just takes
1672 * the NIR deref from the pointer and duplicates it to both vector
1675 nir_address_format addr_format
=
1676 vtn_mode_to_address_format(b
, vtn_variable_mode_function
);
1677 assert(nir_address_format_num_components(addr_format
) == 1);
1678 unsigned bit_size
= nir_address_format_bit_size(addr_format
);
1679 assert(bit_size
== 32 || bit_size
== 64);
1681 enum glsl_base_type base_type
=
1682 bit_size
== 32 ? GLSL_TYPE_UINT
: GLSL_TYPE_UINT64
;
1683 val
->type
->type
= glsl_vector_type(base_type
, 2);
1687 case SpvOpTypeSampler
:
1688 val
->type
->base_type
= vtn_base_type_sampler
;
1690 /* Samplers are represented in NIR as a scalar SSA value that is the
1691 * result of a deref instruction. An OpLoad on an OpTypeSampler pointer
1692 * from UniformConstant memory just takes the NIR deref from the pointer
1693 * and turns it into an SSA value.
1695 val
->type
->type
= nir_address_format_to_glsl_type(
1696 vtn_mode_to_address_format(b
, vtn_variable_mode_function
));
1699 case SpvOpTypeOpaque
:
1700 case SpvOpTypeEvent
:
1701 case SpvOpTypeDeviceEvent
:
1702 case SpvOpTypeReserveId
:
1703 case SpvOpTypeQueue
:
1706 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1709 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1711 if (val
->type
->base_type
== vtn_base_type_struct
&&
1712 (val
->type
->block
|| val
->type
->buffer_block
)) {
1713 for (unsigned i
= 0; i
< val
->type
->length
; i
++) {
1714 vtn_fail_if(vtn_type_contains_block(b
, val
->type
->members
[i
]),
1715 "Block and BufferBlock decorations cannot decorate a "
1716 "structure type that is nested at any level inside "
1717 "another structure type decorated with Block or "
1723 static nir_constant
*
1724 vtn_null_constant(struct vtn_builder
*b
, struct vtn_type
*type
)
1726 nir_constant
*c
= rzalloc(b
, nir_constant
);
1728 switch (type
->base_type
) {
1729 case vtn_base_type_scalar
:
1730 case vtn_base_type_vector
:
1731 /* Nothing to do here. It's already initialized to zero */
1734 case vtn_base_type_pointer
: {
1735 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1736 b
, type
->storage_class
, type
->deref
, NULL
);
1737 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
1739 const nir_const_value
*null_value
= nir_address_format_null_value(addr_format
);
1740 memcpy(c
->values
, null_value
,
1741 sizeof(nir_const_value
) * nir_address_format_num_components(addr_format
));
1745 case vtn_base_type_void
:
1746 case vtn_base_type_image
:
1747 case vtn_base_type_sampler
:
1748 case vtn_base_type_sampled_image
:
1749 case vtn_base_type_function
:
1750 /* For those we have to return something but it doesn't matter what. */
1753 case vtn_base_type_matrix
:
1754 case vtn_base_type_array
:
1755 vtn_assert(type
->length
> 0);
1756 c
->num_elements
= type
->length
;
1757 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1759 c
->elements
[0] = vtn_null_constant(b
, type
->array_element
);
1760 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1761 c
->elements
[i
] = c
->elements
[0];
1764 case vtn_base_type_struct
:
1765 c
->num_elements
= type
->length
;
1766 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1767 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
1768 c
->elements
[i
] = vtn_null_constant(b
, type
->members
[i
]);
1772 vtn_fail("Invalid type for null constant");
1779 spec_constant_decoration_cb(struct vtn_builder
*b
, UNUSED
struct vtn_value
*val
,
1780 ASSERTED
int member
,
1781 const struct vtn_decoration
*dec
, void *data
)
1783 vtn_assert(member
== -1);
1784 if (dec
->decoration
!= SpvDecorationSpecId
)
1787 nir_const_value
*value
= data
;
1788 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1789 if (b
->specializations
[i
].id
== dec
->operands
[0]) {
1790 *value
= b
->specializations
[i
].value
;
1797 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1798 struct vtn_value
*val
,
1799 ASSERTED
int member
,
1800 const struct vtn_decoration
*dec
,
1803 vtn_assert(member
== -1);
1804 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1805 dec
->operands
[0] != SpvBuiltInWorkgroupSize
)
1808 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1809 b
->workgroup_size_builtin
= val
;
1813 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1814 const uint32_t *w
, unsigned count
)
1816 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1817 val
->constant
= rzalloc(b
, nir_constant
);
1819 case SpvOpConstantTrue
:
1820 case SpvOpConstantFalse
:
1821 case SpvOpSpecConstantTrue
:
1822 case SpvOpSpecConstantFalse
: {
1823 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1824 "Result type of %s must be OpTypeBool",
1825 spirv_op_to_string(opcode
));
1827 bool bval
= (opcode
== SpvOpConstantTrue
||
1828 opcode
== SpvOpSpecConstantTrue
);
1830 nir_const_value u32val
= nir_const_value_for_uint(bval
, 32);
1832 if (opcode
== SpvOpSpecConstantTrue
||
1833 opcode
== SpvOpSpecConstantFalse
)
1834 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &u32val
);
1836 val
->constant
->values
[0].b
= u32val
.u32
!= 0;
1841 case SpvOpSpecConstant
: {
1842 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1843 "Result type of %s must be a scalar",
1844 spirv_op_to_string(opcode
));
1845 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1848 val
->constant
->values
[0].u64
= vtn_u64_literal(&w
[3]);
1851 val
->constant
->values
[0].u32
= w
[3];
1854 val
->constant
->values
[0].u16
= w
[3];
1857 val
->constant
->values
[0].u8
= w
[3];
1860 vtn_fail("Unsupported SpvOpConstant bit size: %u", bit_size
);
1863 if (opcode
== SpvOpSpecConstant
)
1864 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
,
1865 &val
->constant
->values
[0]);
1869 case SpvOpSpecConstantComposite
:
1870 case SpvOpConstantComposite
: {
1871 unsigned elem_count
= count
- 3;
1872 vtn_fail_if(elem_count
!= val
->type
->length
,
1873 "%s has %u constituents, expected %u",
1874 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1876 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1877 for (unsigned i
= 0; i
< elem_count
; i
++) {
1878 struct vtn_value
*val
= vtn_untyped_value(b
, w
[i
+ 3]);
1880 if (val
->value_type
== vtn_value_type_constant
) {
1881 elems
[i
] = val
->constant
;
1883 vtn_fail_if(val
->value_type
!= vtn_value_type_undef
,
1884 "only constants or undefs allowed for "
1885 "SpvOpConstantComposite");
1886 /* to make it easier, just insert a NULL constant for now */
1887 elems
[i
] = vtn_null_constant(b
, val
->type
);
1891 switch (val
->type
->base_type
) {
1892 case vtn_base_type_vector
: {
1893 assert(glsl_type_is_vector(val
->type
->type
));
1894 for (unsigned i
= 0; i
< elem_count
; i
++)
1895 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1899 case vtn_base_type_matrix
:
1900 case vtn_base_type_struct
:
1901 case vtn_base_type_array
:
1902 ralloc_steal(val
->constant
, elems
);
1903 val
->constant
->num_elements
= elem_count
;
1904 val
->constant
->elements
= elems
;
1908 vtn_fail("Result type of %s must be a composite type",
1909 spirv_op_to_string(opcode
));
1914 case SpvOpSpecConstantOp
: {
1915 nir_const_value u32op
= nir_const_value_for_uint(w
[3], 32);
1916 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &u32op
);
1917 SpvOp opcode
= u32op
.u32
;
1919 case SpvOpVectorShuffle
: {
1920 struct vtn_value
*v0
= &b
->values
[w
[4]];
1921 struct vtn_value
*v1
= &b
->values
[w
[5]];
1923 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1924 v0
->value_type
== vtn_value_type_undef
);
1925 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1926 v1
->value_type
== vtn_value_type_undef
);
1928 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1929 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1931 vtn_assert(len0
+ len1
< 16);
1933 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1934 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1935 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1937 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1938 (void)bit_size0
; (void)bit_size1
;
1940 nir_const_value undef
= { .u64
= 0xdeadbeefdeadbeef };
1941 nir_const_value combined
[NIR_MAX_VEC_COMPONENTS
* 2];
1943 if (v0
->value_type
== vtn_value_type_constant
) {
1944 for (unsigned i
= 0; i
< len0
; i
++)
1945 combined
[i
] = v0
->constant
->values
[i
];
1947 if (v1
->value_type
== vtn_value_type_constant
) {
1948 for (unsigned i
= 0; i
< len1
; i
++)
1949 combined
[len0
+ i
] = v1
->constant
->values
[i
];
1952 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1953 uint32_t comp
= w
[i
+ 6];
1954 if (comp
== (uint32_t)-1) {
1955 /* If component is not used, set the value to a known constant
1956 * to detect if it is wrongly used.
1958 val
->constant
->values
[j
] = undef
;
1960 vtn_fail_if(comp
>= len0
+ len1
,
1961 "All Component literals must either be FFFFFFFF "
1962 "or in [0, N - 1] (inclusive).");
1963 val
->constant
->values
[j
] = combined
[comp
];
1969 case SpvOpCompositeExtract
:
1970 case SpvOpCompositeInsert
: {
1971 struct vtn_value
*comp
;
1972 unsigned deref_start
;
1973 struct nir_constant
**c
;
1974 if (opcode
== SpvOpCompositeExtract
) {
1975 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1977 c
= &comp
->constant
;
1979 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1981 val
->constant
= nir_constant_clone(comp
->constant
,
1987 const struct vtn_type
*type
= comp
->type
;
1988 for (unsigned i
= deref_start
; i
< count
; i
++) {
1989 vtn_fail_if(w
[i
] > type
->length
,
1990 "%uth index of %s is %u but the type has only "
1991 "%u elements", i
- deref_start
,
1992 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1994 switch (type
->base_type
) {
1995 case vtn_base_type_vector
:
1997 type
= type
->array_element
;
2000 case vtn_base_type_matrix
:
2001 case vtn_base_type_array
:
2002 c
= &(*c
)->elements
[w
[i
]];
2003 type
= type
->array_element
;
2006 case vtn_base_type_struct
:
2007 c
= &(*c
)->elements
[w
[i
]];
2008 type
= type
->members
[w
[i
]];
2012 vtn_fail("%s must only index into composite types",
2013 spirv_op_to_string(opcode
));
2017 if (opcode
== SpvOpCompositeExtract
) {
2021 unsigned num_components
= type
->length
;
2022 for (unsigned i
= 0; i
< num_components
; i
++)
2023 val
->constant
->values
[i
] = (*c
)->values
[elem
+ i
];
2026 struct vtn_value
*insert
=
2027 vtn_value(b
, w
[4], vtn_value_type_constant
);
2028 vtn_assert(insert
->type
== type
);
2030 *c
= insert
->constant
;
2032 unsigned num_components
= type
->length
;
2033 for (unsigned i
= 0; i
< num_components
; i
++)
2034 (*c
)->values
[elem
+ i
] = insert
->constant
->values
[i
];
2042 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
2043 nir_alu_type src_alu_type
= dst_alu_type
;
2044 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
2047 vtn_assert(count
<= 7);
2053 /* We have a source in a conversion */
2055 nir_get_nir_type_for_glsl_type(vtn_get_value_type(b
, w
[4])->type
);
2056 /* We use the bitsize of the conversion source to evaluate the opcode later */
2057 bit_size
= glsl_get_bit_size(vtn_get_value_type(b
, w
[4])->type
);
2060 bit_size
= glsl_get_bit_size(val
->type
->type
);
2063 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
2064 nir_alu_type_get_type_size(src_alu_type
),
2065 nir_alu_type_get_type_size(dst_alu_type
));
2066 nir_const_value src
[3][NIR_MAX_VEC_COMPONENTS
];
2068 for (unsigned i
= 0; i
< count
- 4; i
++) {
2069 struct vtn_value
*src_val
=
2070 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
);
2072 /* If this is an unsized source, pull the bit size from the
2073 * source; otherwise, we'll use the bit size from the destination.
2075 if (!nir_alu_type_get_type_size(nir_op_infos
[op
].input_types
[i
]))
2076 bit_size
= glsl_get_bit_size(src_val
->type
->type
);
2078 unsigned src_comps
= nir_op_infos
[op
].input_sizes
[i
] ?
2079 nir_op_infos
[op
].input_sizes
[i
] :
2082 unsigned j
= swap
? 1 - i
: i
;
2083 for (unsigned c
= 0; c
< src_comps
; c
++)
2084 src
[j
][c
] = src_val
->constant
->values
[c
];
2087 /* fix up fixed size sources */
2094 for (unsigned i
= 0; i
< num_components
; ++i
) {
2096 case 64: src
[1][i
].u32
= src
[1][i
].u64
; break;
2097 case 16: src
[1][i
].u32
= src
[1][i
].u16
; break;
2098 case 8: src
[1][i
].u32
= src
[1][i
].u8
; break;
2107 nir_const_value
*srcs
[3] = {
2108 src
[0], src
[1], src
[2],
2110 nir_eval_const_opcode(op
, val
->constant
->values
,
2111 num_components
, bit_size
, srcs
,
2112 b
->shader
->info
.float_controls_execution_mode
);
2119 case SpvOpConstantNull
:
2120 val
->constant
= vtn_null_constant(b
, val
->type
);
2124 vtn_fail_with_opcode("Unhandled opcode", opcode
);
2127 /* Now that we have the value, update the workgroup size if needed */
2128 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
2132 vtn_split_barrier_semantics(struct vtn_builder
*b
,
2133 SpvMemorySemanticsMask semantics
,
2134 SpvMemorySemanticsMask
*before
,
2135 SpvMemorySemanticsMask
*after
)
2137 /* For memory semantics embedded in operations, we split them into up to
2138 * two barriers, to be added before and after the operation. This is less
2139 * strict than if we propagated until the final backend stage, but still
2140 * result in correct execution.
2142 * A further improvement could be pipe this information (and use!) into the
2143 * next compiler layers, at the expense of making the handling of barriers
2147 *before
= SpvMemorySemanticsMaskNone
;
2148 *after
= SpvMemorySemanticsMaskNone
;
2150 SpvMemorySemanticsMask order_semantics
=
2151 semantics
& (SpvMemorySemanticsAcquireMask
|
2152 SpvMemorySemanticsReleaseMask
|
2153 SpvMemorySemanticsAcquireReleaseMask
|
2154 SpvMemorySemanticsSequentiallyConsistentMask
);
2156 if (util_bitcount(order_semantics
) > 1) {
2157 /* Old GLSLang versions incorrectly set all the ordering bits. This was
2158 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
2159 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
2161 vtn_warn("Multiple memory ordering semantics specified, "
2162 "assuming AcquireRelease.");
2163 order_semantics
= SpvMemorySemanticsAcquireReleaseMask
;
2166 const SpvMemorySemanticsMask av_vis_semantics
=
2167 semantics
& (SpvMemorySemanticsMakeAvailableMask
|
2168 SpvMemorySemanticsMakeVisibleMask
);
2170 const SpvMemorySemanticsMask storage_semantics
=
2171 semantics
& (SpvMemorySemanticsUniformMemoryMask
|
2172 SpvMemorySemanticsSubgroupMemoryMask
|
2173 SpvMemorySemanticsWorkgroupMemoryMask
|
2174 SpvMemorySemanticsCrossWorkgroupMemoryMask
|
2175 SpvMemorySemanticsAtomicCounterMemoryMask
|
2176 SpvMemorySemanticsImageMemoryMask
|
2177 SpvMemorySemanticsOutputMemoryMask
);
2179 const SpvMemorySemanticsMask other_semantics
=
2180 semantics
& ~(order_semantics
| av_vis_semantics
| storage_semantics
|
2181 SpvMemorySemanticsVolatileMask
);
2183 if (other_semantics
)
2184 vtn_warn("Ignoring unhandled memory semantics: %u\n", other_semantics
);
2186 /* SequentiallyConsistent is treated as AcquireRelease. */
2188 /* The RELEASE barrier happens BEFORE the operation, and it is usually
2189 * associated with a Store. All the write operations with a matching
2190 * semantics will not be reordered after the Store.
2192 if (order_semantics
& (SpvMemorySemanticsReleaseMask
|
2193 SpvMemorySemanticsAcquireReleaseMask
|
2194 SpvMemorySemanticsSequentiallyConsistentMask
)) {
2195 *before
|= SpvMemorySemanticsReleaseMask
| storage_semantics
;
2198 /* The ACQUIRE barrier happens AFTER the operation, and it is usually
2199 * associated with a Load. All the operations with a matching semantics
2200 * will not be reordered before the Load.
2202 if (order_semantics
& (SpvMemorySemanticsAcquireMask
|
2203 SpvMemorySemanticsAcquireReleaseMask
|
2204 SpvMemorySemanticsSequentiallyConsistentMask
)) {
2205 *after
|= SpvMemorySemanticsAcquireMask
| storage_semantics
;
2208 if (av_vis_semantics
& SpvMemorySemanticsMakeVisibleMask
)
2209 *before
|= SpvMemorySemanticsMakeVisibleMask
| storage_semantics
;
2211 if (av_vis_semantics
& SpvMemorySemanticsMakeAvailableMask
)
2212 *after
|= SpvMemorySemanticsMakeAvailableMask
| storage_semantics
;
2215 static nir_memory_semantics
2216 vtn_mem_semantics_to_nir_mem_semantics(struct vtn_builder
*b
,
2217 SpvMemorySemanticsMask semantics
)
2219 nir_memory_semantics nir_semantics
= 0;
2221 SpvMemorySemanticsMask order_semantics
=
2222 semantics
& (SpvMemorySemanticsAcquireMask
|
2223 SpvMemorySemanticsReleaseMask
|
2224 SpvMemorySemanticsAcquireReleaseMask
|
2225 SpvMemorySemanticsSequentiallyConsistentMask
);
2227 if (util_bitcount(order_semantics
) > 1) {
2228 /* Old GLSLang versions incorrectly set all the ordering bits. This was
2229 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
2230 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
2232 vtn_warn("Multiple memory ordering semantics bits specified, "
2233 "assuming AcquireRelease.");
2234 order_semantics
= SpvMemorySemanticsAcquireReleaseMask
;
2237 switch (order_semantics
) {
2239 /* Not an ordering barrier. */
2242 case SpvMemorySemanticsAcquireMask
:
2243 nir_semantics
= NIR_MEMORY_ACQUIRE
;
2246 case SpvMemorySemanticsReleaseMask
:
2247 nir_semantics
= NIR_MEMORY_RELEASE
;
2250 case SpvMemorySemanticsSequentiallyConsistentMask
:
2251 /* Fall through. Treated as AcquireRelease in Vulkan. */
2252 case SpvMemorySemanticsAcquireReleaseMask
:
2253 nir_semantics
= NIR_MEMORY_ACQUIRE
| NIR_MEMORY_RELEASE
;
2257 unreachable("Invalid memory order semantics");
2260 if (semantics
& SpvMemorySemanticsMakeAvailableMask
) {
2261 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2262 "To use MakeAvailable memory semantics the VulkanMemoryModel "
2263 "capability must be declared.");
2264 nir_semantics
|= NIR_MEMORY_MAKE_AVAILABLE
;
2267 if (semantics
& SpvMemorySemanticsMakeVisibleMask
) {
2268 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2269 "To use MakeVisible memory semantics the VulkanMemoryModel "
2270 "capability must be declared.");
2271 nir_semantics
|= NIR_MEMORY_MAKE_VISIBLE
;
2274 return nir_semantics
;
2277 static nir_variable_mode
2278 vtn_mem_sematics_to_nir_var_modes(struct vtn_builder
*b
,
2279 SpvMemorySemanticsMask semantics
)
2281 /* Vulkan Environment for SPIR-V says "SubgroupMemory, CrossWorkgroupMemory,
2282 * and AtomicCounterMemory are ignored".
2284 semantics
&= ~(SpvMemorySemanticsSubgroupMemoryMask
|
2285 SpvMemorySemanticsCrossWorkgroupMemoryMask
|
2286 SpvMemorySemanticsAtomicCounterMemoryMask
);
2288 /* TODO: Consider adding nir_var_mem_image mode to NIR so it can be used
2289 * for SpvMemorySemanticsImageMemoryMask.
2292 nir_variable_mode modes
= 0;
2293 if (semantics
& (SpvMemorySemanticsUniformMemoryMask
|
2294 SpvMemorySemanticsImageMemoryMask
)) {
2295 modes
|= nir_var_uniform
|
2300 if (semantics
& SpvMemorySemanticsWorkgroupMemoryMask
)
2301 modes
|= nir_var_mem_shared
;
2302 if (semantics
& SpvMemorySemanticsOutputMemoryMask
) {
2303 modes
|= nir_var_shader_out
;
2310 vtn_scope_to_nir_scope(struct vtn_builder
*b
, SpvScope scope
)
2312 nir_scope nir_scope
;
2314 case SpvScopeDevice
:
2315 vtn_fail_if(b
->options
->caps
.vk_memory_model
&&
2316 !b
->options
->caps
.vk_memory_model_device_scope
,
2317 "If the Vulkan memory model is declared and any instruction "
2318 "uses Device scope, the VulkanMemoryModelDeviceScope "
2319 "capability must be declared.");
2320 nir_scope
= NIR_SCOPE_DEVICE
;
2323 case SpvScopeQueueFamily
:
2324 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2325 "To use Queue Family scope, the VulkanMemoryModel capability "
2326 "must be declared.");
2327 nir_scope
= NIR_SCOPE_QUEUE_FAMILY
;
2330 case SpvScopeWorkgroup
:
2331 nir_scope
= NIR_SCOPE_WORKGROUP
;
2334 case SpvScopeSubgroup
:
2335 nir_scope
= NIR_SCOPE_SUBGROUP
;
2338 case SpvScopeInvocation
:
2339 nir_scope
= NIR_SCOPE_INVOCATION
;
2343 vtn_fail("Invalid memory scope");
2350 vtn_emit_scoped_control_barrier(struct vtn_builder
*b
, SpvScope exec_scope
,
2352 SpvMemorySemanticsMask semantics
)
2354 nir_memory_semantics nir_semantics
=
2355 vtn_mem_semantics_to_nir_mem_semantics(b
, semantics
);
2356 nir_variable_mode modes
= vtn_mem_sematics_to_nir_var_modes(b
, semantics
);
2357 nir_scope nir_exec_scope
= vtn_scope_to_nir_scope(b
, exec_scope
);
2359 /* Memory semantics is optional for OpControlBarrier. */
2360 nir_scope nir_mem_scope
;
2361 if (nir_semantics
== 0 || modes
== 0)
2362 nir_mem_scope
= NIR_SCOPE_NONE
;
2364 nir_mem_scope
= vtn_scope_to_nir_scope(b
, mem_scope
);
2366 nir_scoped_barrier(&b
->nb
, nir_exec_scope
, nir_mem_scope
, nir_semantics
, modes
);
2370 vtn_emit_scoped_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
2371 SpvMemorySemanticsMask semantics
)
2373 nir_variable_mode modes
= vtn_mem_sematics_to_nir_var_modes(b
, semantics
);
2374 nir_memory_semantics nir_semantics
=
2375 vtn_mem_semantics_to_nir_mem_semantics(b
, semantics
);
2377 /* No barrier to add. */
2378 if (nir_semantics
== 0 || modes
== 0)
2381 nir_scope nir_mem_scope
= vtn_scope_to_nir_scope(b
, scope
);
2382 nir_scoped_barrier(&b
->nb
, NIR_SCOPE_NONE
, nir_mem_scope
, nir_semantics
, modes
);
2385 struct vtn_ssa_value
*
2386 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
2388 /* Always use bare types for SSA values for a couple of reasons:
2390 * 1. Code which emits deref chains should never listen to the explicit
2391 * layout information on the SSA value if any exists. If we've
2392 * accidentally been relying on this, we want to find those bugs.
2394 * 2. We want to be able to quickly check that an SSA value being assigned
2395 * to a SPIR-V value has the right type. Using bare types everywhere
2396 * ensures that we can pointer-compare.
2398 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
2399 val
->type
= glsl_get_bare_type(type
);
2402 if (!glsl_type_is_vector_or_scalar(type
)) {
2403 unsigned elems
= glsl_get_length(val
->type
);
2404 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2405 if (glsl_type_is_array_or_matrix(type
)) {
2406 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
2407 for (unsigned i
= 0; i
< elems
; i
++)
2408 val
->elems
[i
] = vtn_create_ssa_value(b
, elem_type
);
2410 vtn_assert(glsl_type_is_struct_or_ifc(type
));
2411 for (unsigned i
= 0; i
< elems
; i
++) {
2412 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
2413 val
->elems
[i
] = vtn_create_ssa_value(b
, elem_type
);
2422 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
2425 src
.src
= nir_src_for_ssa(vtn_get_nir_ssa(b
, index
));
2426 src
.src_type
= type
;
2431 image_operand_arg(struct vtn_builder
*b
, const uint32_t *w
, uint32_t count
,
2432 uint32_t mask_idx
, SpvImageOperandsMask op
)
2434 static const SpvImageOperandsMask ops_with_arg
=
2435 SpvImageOperandsBiasMask
|
2436 SpvImageOperandsLodMask
|
2437 SpvImageOperandsGradMask
|
2438 SpvImageOperandsConstOffsetMask
|
2439 SpvImageOperandsOffsetMask
|
2440 SpvImageOperandsConstOffsetsMask
|
2441 SpvImageOperandsSampleMask
|
2442 SpvImageOperandsMinLodMask
|
2443 SpvImageOperandsMakeTexelAvailableMask
|
2444 SpvImageOperandsMakeTexelVisibleMask
;
2446 assert(util_bitcount(op
) == 1);
2447 assert(w
[mask_idx
] & op
);
2448 assert(op
& ops_with_arg
);
2450 uint32_t idx
= util_bitcount(w
[mask_idx
] & (op
- 1) & ops_with_arg
) + 1;
2452 /* Adjust indices for operands with two arguments. */
2453 static const SpvImageOperandsMask ops_with_two_args
=
2454 SpvImageOperandsGradMask
;
2455 idx
+= util_bitcount(w
[mask_idx
] & (op
- 1) & ops_with_two_args
);
2459 vtn_fail_if(idx
+ (op
& ops_with_two_args
? 1 : 0) >= count
,
2460 "Image op claims to have %s but does not enough "
2461 "following operands", spirv_imageoperands_to_string(op
));
2467 non_uniform_decoration_cb(struct vtn_builder
*b
,
2468 struct vtn_value
*val
, int member
,
2469 const struct vtn_decoration
*dec
, void *void_ctx
)
2471 enum gl_access_qualifier
*access
= void_ctx
;
2472 switch (dec
->decoration
) {
2473 case SpvDecorationNonUniformEXT
:
2474 *access
|= ACCESS_NON_UNIFORM
;
2483 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
2484 const uint32_t *w
, unsigned count
)
2486 struct vtn_type
*ret_type
= vtn_get_type(b
, w
[1]);
2488 if (opcode
== SpvOpSampledImage
) {
2489 struct vtn_sampled_image si
= {
2490 .image
= vtn_get_image(b
, w
[3]),
2491 .sampler
= vtn_get_sampler(b
, w
[4]),
2493 vtn_push_sampled_image(b
, w
[2], si
);
2495 } else if (opcode
== SpvOpImage
) {
2496 struct vtn_sampled_image si
= vtn_get_sampled_image(b
, w
[3]);
2497 vtn_push_image(b
, w
[2], si
.image
);
2501 nir_deref_instr
*image
= NULL
, *sampler
= NULL
;
2502 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
2503 if (sampled_val
->type
->base_type
== vtn_base_type_sampled_image
) {
2504 struct vtn_sampled_image si
= vtn_get_sampled_image(b
, w
[3]);
2506 sampler
= si
.sampler
;
2508 image
= vtn_get_image(b
, w
[3]);
2511 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image
->type
);
2512 const bool is_array
= glsl_sampler_type_is_array(image
->type
);
2513 nir_alu_type dest_type
= nir_type_invalid
;
2515 /* Figure out the base texture operation */
2518 case SpvOpImageSampleImplicitLod
:
2519 case SpvOpImageSampleDrefImplicitLod
:
2520 case SpvOpImageSampleProjImplicitLod
:
2521 case SpvOpImageSampleProjDrefImplicitLod
:
2522 texop
= nir_texop_tex
;
2525 case SpvOpImageSampleExplicitLod
:
2526 case SpvOpImageSampleDrefExplicitLod
:
2527 case SpvOpImageSampleProjExplicitLod
:
2528 case SpvOpImageSampleProjDrefExplicitLod
:
2529 texop
= nir_texop_txl
;
2532 case SpvOpImageFetch
:
2533 if (sampler_dim
== GLSL_SAMPLER_DIM_MS
) {
2534 texop
= nir_texop_txf_ms
;
2536 texop
= nir_texop_txf
;
2540 case SpvOpImageGather
:
2541 case SpvOpImageDrefGather
:
2542 texop
= nir_texop_tg4
;
2545 case SpvOpImageQuerySizeLod
:
2546 case SpvOpImageQuerySize
:
2547 texop
= nir_texop_txs
;
2548 dest_type
= nir_type_int
;
2551 case SpvOpImageQueryLod
:
2552 texop
= nir_texop_lod
;
2553 dest_type
= nir_type_float
;
2556 case SpvOpImageQueryLevels
:
2557 texop
= nir_texop_query_levels
;
2558 dest_type
= nir_type_int
;
2561 case SpvOpImageQuerySamples
:
2562 texop
= nir_texop_texture_samples
;
2563 dest_type
= nir_type_int
;
2566 case SpvOpFragmentFetchAMD
:
2567 texop
= nir_texop_fragment_fetch
;
2570 case SpvOpFragmentMaskFetchAMD
:
2571 texop
= nir_texop_fragment_mask_fetch
;
2575 vtn_fail_with_opcode("Unhandled opcode", opcode
);
2578 nir_tex_src srcs
[10]; /* 10 should be enough */
2579 nir_tex_src
*p
= srcs
;
2581 p
->src
= nir_src_for_ssa(&image
->dest
.ssa
);
2582 p
->src_type
= nir_tex_src_texture_deref
;
2592 vtn_fail_if(sampler
== NULL
,
2593 "%s requires an image of type OpTypeSampledImage",
2594 spirv_op_to_string(opcode
));
2595 p
->src
= nir_src_for_ssa(&sampler
->dest
.ssa
);
2596 p
->src_type
= nir_tex_src_sampler_deref
;
2600 case nir_texop_txf_ms
:
2602 case nir_texop_query_levels
:
2603 case nir_texop_texture_samples
:
2604 case nir_texop_samples_identical
:
2605 case nir_texop_fragment_fetch
:
2606 case nir_texop_fragment_mask_fetch
:
2609 case nir_texop_txf_ms_fb
:
2610 vtn_fail("unexpected nir_texop_txf_ms_fb");
2612 case nir_texop_txf_ms_mcs
:
2613 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2614 case nir_texop_tex_prefetch
:
2615 vtn_fail("unexpected nir_texop_tex_prefetch");
2620 struct nir_ssa_def
*coord
;
2621 unsigned coord_components
;
2623 case SpvOpImageSampleImplicitLod
:
2624 case SpvOpImageSampleExplicitLod
:
2625 case SpvOpImageSampleDrefImplicitLod
:
2626 case SpvOpImageSampleDrefExplicitLod
:
2627 case SpvOpImageSampleProjImplicitLod
:
2628 case SpvOpImageSampleProjExplicitLod
:
2629 case SpvOpImageSampleProjDrefImplicitLod
:
2630 case SpvOpImageSampleProjDrefExplicitLod
:
2631 case SpvOpImageFetch
:
2632 case SpvOpImageGather
:
2633 case SpvOpImageDrefGather
:
2634 case SpvOpImageQueryLod
:
2635 case SpvOpFragmentFetchAMD
:
2636 case SpvOpFragmentMaskFetchAMD
: {
2637 /* All these types have the coordinate as their first real argument */
2638 coord_components
= glsl_get_sampler_dim_coordinate_components(sampler_dim
);
2640 if (is_array
&& texop
!= nir_texop_lod
)
2643 struct vtn_ssa_value
*coord_val
= vtn_ssa_value(b
, w
[idx
++]);
2644 coord
= coord_val
->def
;
2645 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2646 (1 << coord_components
) - 1));
2648 /* OpenCL allows integer sampling coordinates */
2649 if (glsl_type_is_integer(coord_val
->type
) &&
2650 opcode
== SpvOpImageSampleExplicitLod
) {
2651 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
2652 "Unless the Kernel capability is being used, the coordinate parameter "
2653 "OpImageSampleExplicitLod must be floating point.");
2655 p
->src
= nir_src_for_ssa(nir_i2f32(&b
->nb
, p
->src
.ssa
));
2658 p
->src_type
= nir_tex_src_coord
;
2665 coord_components
= 0;
2670 case SpvOpImageSampleProjImplicitLod
:
2671 case SpvOpImageSampleProjExplicitLod
:
2672 case SpvOpImageSampleProjDrefImplicitLod
:
2673 case SpvOpImageSampleProjDrefExplicitLod
:
2674 /* These have the projector as the last coordinate component */
2675 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2676 p
->src_type
= nir_tex_src_projector
;
2684 bool is_shadow
= false;
2685 unsigned gather_component
= 0;
2687 case SpvOpImageSampleDrefImplicitLod
:
2688 case SpvOpImageSampleDrefExplicitLod
:
2689 case SpvOpImageSampleProjDrefImplicitLod
:
2690 case SpvOpImageSampleProjDrefExplicitLod
:
2691 case SpvOpImageDrefGather
:
2692 /* These all have an explicit depth value as their next source */
2694 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2697 case SpvOpImageGather
:
2698 /* This has a component as its next source */
2699 gather_component
= vtn_constant_uint(b
, w
[idx
++]);
2706 /* For OpImageQuerySizeLod, we always have an LOD */
2707 if (opcode
== SpvOpImageQuerySizeLod
)
2708 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2710 /* For OpFragmentFetchAMD, we always have a multisample index */
2711 if (opcode
== SpvOpFragmentFetchAMD
)
2712 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
2714 /* Now we need to handle some number of optional arguments */
2715 struct vtn_value
*gather_offsets
= NULL
;
2717 uint32_t operands
= w
[idx
];
2719 if (operands
& SpvImageOperandsBiasMask
) {
2720 vtn_assert(texop
== nir_texop_tex
||
2721 texop
== nir_texop_tg4
);
2722 if (texop
== nir_texop_tex
)
2723 texop
= nir_texop_txb
;
2724 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2725 SpvImageOperandsBiasMask
);
2726 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_bias
);
2729 if (operands
& SpvImageOperandsLodMask
) {
2730 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2731 texop
== nir_texop_txs
|| texop
== nir_texop_tg4
);
2732 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2733 SpvImageOperandsLodMask
);
2734 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_lod
);
2737 if (operands
& SpvImageOperandsGradMask
) {
2738 vtn_assert(texop
== nir_texop_txl
);
2739 texop
= nir_texop_txd
;
2740 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2741 SpvImageOperandsGradMask
);
2742 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_ddx
);
2743 (*p
++) = vtn_tex_src(b
, w
[arg
+ 1], nir_tex_src_ddy
);
2746 vtn_fail_if(util_bitcount(operands
& (SpvImageOperandsConstOffsetsMask
|
2747 SpvImageOperandsOffsetMask
|
2748 SpvImageOperandsConstOffsetMask
)) > 1,
2749 "At most one of the ConstOffset, Offset, and ConstOffsets "
2750 "image operands can be used on a given instruction.");
2752 if (operands
& SpvImageOperandsOffsetMask
) {
2753 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2754 SpvImageOperandsOffsetMask
);
2755 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_offset
);
2758 if (operands
& SpvImageOperandsConstOffsetMask
) {
2759 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2760 SpvImageOperandsConstOffsetMask
);
2761 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_offset
);
2764 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2765 vtn_assert(texop
== nir_texop_tg4
);
2766 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2767 SpvImageOperandsConstOffsetsMask
);
2768 gather_offsets
= vtn_value(b
, w
[arg
], vtn_value_type_constant
);
2771 if (operands
& SpvImageOperandsSampleMask
) {
2772 vtn_assert(texop
== nir_texop_txf_ms
);
2773 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2774 SpvImageOperandsSampleMask
);
2775 texop
= nir_texop_txf_ms
;
2776 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_ms_index
);
2779 if (operands
& SpvImageOperandsMinLodMask
) {
2780 vtn_assert(texop
== nir_texop_tex
||
2781 texop
== nir_texop_txb
||
2782 texop
== nir_texop_txd
);
2783 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2784 SpvImageOperandsMinLodMask
);
2785 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_min_lod
);
2789 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2792 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2794 instr
->coord_components
= coord_components
;
2795 instr
->sampler_dim
= sampler_dim
;
2796 instr
->is_array
= is_array
;
2797 instr
->is_shadow
= is_shadow
;
2798 instr
->is_new_style_shadow
=
2799 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2800 instr
->component
= gather_component
;
2802 /* The Vulkan spec says:
2804 * "If an instruction loads from or stores to a resource (including
2805 * atomics and image instructions) and the resource descriptor being
2806 * accessed is not dynamically uniform, then the operand corresponding
2807 * to that resource (e.g. the pointer or sampled image operand) must be
2808 * decorated with NonUniform."
2810 * It's very careful to specify that the exact operand must be decorated
2811 * NonUniform. The SPIR-V parser is not expected to chase through long
2812 * chains to find the NonUniform decoration. It's either right there or we
2813 * can assume it doesn't exist.
2815 enum gl_access_qualifier access
= 0;
2816 vtn_foreach_decoration(b
, sampled_val
, non_uniform_decoration_cb
, &access
);
2818 if (image
&& (access
& ACCESS_NON_UNIFORM
))
2819 instr
->texture_non_uniform
= true;
2821 if (sampler
&& (access
& ACCESS_NON_UNIFORM
))
2822 instr
->sampler_non_uniform
= true;
2824 /* for non-query ops, get dest_type from SPIR-V return type */
2825 if (dest_type
== nir_type_invalid
) {
2826 /* the return type should match the image type, unless the image type is
2827 * VOID (CL image), in which case the return type dictates the sampler
2829 enum glsl_base_type sampler_base
=
2830 glsl_get_sampler_result_type(image
->type
);
2831 enum glsl_base_type ret_base
= glsl_get_base_type(ret_type
->type
);
2832 vtn_fail_if(sampler_base
!= ret_base
&& sampler_base
!= GLSL_TYPE_VOID
,
2833 "SPIR-V return type mismatches image type. This is only valid "
2834 "for untyped images (OpenCL).");
2836 case GLSL_TYPE_FLOAT
: dest_type
= nir_type_float
; break;
2837 case GLSL_TYPE_INT
: dest_type
= nir_type_int
; break;
2838 case GLSL_TYPE_UINT
: dest_type
= nir_type_uint
; break;
2839 case GLSL_TYPE_BOOL
: dest_type
= nir_type_bool
; break;
2841 vtn_fail("Invalid base type for sampler result");
2845 instr
->dest_type
= dest_type
;
2847 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2848 nir_tex_instr_dest_size(instr
), 32, NULL
);
2850 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2851 nir_tex_instr_dest_size(instr
));
2853 if (gather_offsets
) {
2854 vtn_fail_if(gather_offsets
->type
->base_type
!= vtn_base_type_array
||
2855 gather_offsets
->type
->length
!= 4,
2856 "ConstOffsets must be an array of size four of vectors "
2857 "of two integer components");
2859 struct vtn_type
*vec_type
= gather_offsets
->type
->array_element
;
2860 vtn_fail_if(vec_type
->base_type
!= vtn_base_type_vector
||
2861 vec_type
->length
!= 2 ||
2862 !glsl_type_is_integer(vec_type
->type
),
2863 "ConstOffsets must be an array of size four of vectors "
2864 "of two integer components");
2866 unsigned bit_size
= glsl_get_bit_size(vec_type
->type
);
2867 for (uint32_t i
= 0; i
< 4; i
++) {
2868 const nir_const_value
*cvec
=
2869 gather_offsets
->constant
->elements
[i
]->values
;
2870 for (uint32_t j
= 0; j
< 2; j
++) {
2872 case 8: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i8
; break;
2873 case 16: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i16
; break;
2874 case 32: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i32
; break;
2875 case 64: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i64
; break;
2877 vtn_fail("Unsupported bit size: %u", bit_size
);
2883 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2885 vtn_push_nir_ssa(b
, w
[2], &instr
->dest
.ssa
);
2889 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2890 const uint32_t *w
, nir_src
*src
)
2892 const struct glsl_type
*type
= vtn_get_type(b
, w
[1])->type
;
2893 unsigned bit_size
= glsl_get_bit_size(type
);
2896 case SpvOpAtomicIIncrement
:
2897 src
[0] = nir_src_for_ssa(nir_imm_intN_t(&b
->nb
, 1, bit_size
));
2900 case SpvOpAtomicIDecrement
:
2901 src
[0] = nir_src_for_ssa(nir_imm_intN_t(&b
->nb
, -1, bit_size
));
2904 case SpvOpAtomicISub
:
2906 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_get_nir_ssa(b
, w
[6])));
2909 case SpvOpAtomicCompareExchange
:
2910 case SpvOpAtomicCompareExchangeWeak
:
2911 src
[0] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[8]));
2912 src
[1] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[7]));
2915 case SpvOpAtomicExchange
:
2916 case SpvOpAtomicIAdd
:
2917 case SpvOpAtomicSMin
:
2918 case SpvOpAtomicUMin
:
2919 case SpvOpAtomicSMax
:
2920 case SpvOpAtomicUMax
:
2921 case SpvOpAtomicAnd
:
2923 case SpvOpAtomicXor
:
2924 case SpvOpAtomicFAddEXT
:
2925 src
[0] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[6]));
2929 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2933 static nir_ssa_def
*
2934 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2936 nir_ssa_def
*coord
= vtn_get_nir_ssa(b
, value
);
2938 /* The image_load_store intrinsics assume a 4-dim coordinate */
2939 unsigned swizzle
[4];
2940 for (unsigned i
= 0; i
< 4; i
++)
2941 swizzle
[i
] = MIN2(i
, coord
->num_components
- 1);
2943 return nir_swizzle(&b
->nb
, coord
, swizzle
, 4);
2946 static nir_ssa_def
*
2947 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2949 if (value
->num_components
== 4)
2953 for (unsigned i
= 0; i
< 4; i
++)
2954 swiz
[i
] = i
< value
->num_components
? i
: 0;
2955 return nir_swizzle(b
, value
, swiz
, 4);
2959 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2960 const uint32_t *w
, unsigned count
)
2962 /* Just get this one out of the way */
2963 if (opcode
== SpvOpImageTexelPointer
) {
2964 struct vtn_value
*val
=
2965 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2966 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2968 val
->image
->image
= vtn_nir_deref(b
, w
[3]);
2969 val
->image
->coord
= get_image_coord(b
, w
[4]);
2970 val
->image
->sample
= vtn_get_nir_ssa(b
, w
[5]);
2971 val
->image
->lod
= nir_imm_int(&b
->nb
, 0);
2975 struct vtn_image_pointer image
;
2976 SpvScope scope
= SpvScopeInvocation
;
2977 SpvMemorySemanticsMask semantics
= 0;
2979 enum gl_access_qualifier access
= 0;
2981 struct vtn_value
*res_val
;
2983 case SpvOpAtomicExchange
:
2984 case SpvOpAtomicCompareExchange
:
2985 case SpvOpAtomicCompareExchangeWeak
:
2986 case SpvOpAtomicIIncrement
:
2987 case SpvOpAtomicIDecrement
:
2988 case SpvOpAtomicIAdd
:
2989 case SpvOpAtomicISub
:
2990 case SpvOpAtomicLoad
:
2991 case SpvOpAtomicSMin
:
2992 case SpvOpAtomicUMin
:
2993 case SpvOpAtomicSMax
:
2994 case SpvOpAtomicUMax
:
2995 case SpvOpAtomicAnd
:
2997 case SpvOpAtomicXor
:
2998 case SpvOpAtomicFAddEXT
:
2999 res_val
= vtn_value(b
, w
[3], vtn_value_type_image_pointer
);
3000 image
= *res_val
->image
;
3001 scope
= vtn_constant_uint(b
, w
[4]);
3002 semantics
= vtn_constant_uint(b
, w
[5]);
3003 access
|= ACCESS_COHERENT
;
3006 case SpvOpAtomicStore
:
3007 res_val
= vtn_value(b
, w
[1], vtn_value_type_image_pointer
);
3008 image
= *res_val
->image
;
3009 scope
= vtn_constant_uint(b
, w
[2]);
3010 semantics
= vtn_constant_uint(b
, w
[3]);
3011 access
|= ACCESS_COHERENT
;
3014 case SpvOpImageQuerySizeLod
:
3015 res_val
= vtn_untyped_value(b
, w
[3]);
3016 image
.image
= vtn_get_image(b
, w
[3]);
3018 image
.sample
= NULL
;
3019 image
.lod
= vtn_ssa_value(b
, w
[4])->def
;
3022 case SpvOpImageQuerySize
:
3023 res_val
= vtn_untyped_value(b
, w
[3]);
3024 image
.image
= vtn_get_image(b
, w
[3]);
3026 image
.sample
= NULL
;
3030 case SpvOpImageQueryFormat
:
3031 case SpvOpImageQueryOrder
:
3032 res_val
= vtn_untyped_value(b
, w
[3]);
3033 image
.image
= vtn_get_image(b
, w
[3]);
3035 image
.sample
= NULL
;
3039 case SpvOpImageRead
: {
3040 res_val
= vtn_untyped_value(b
, w
[3]);
3041 image
.image
= vtn_get_image(b
, w
[3]);
3042 image
.coord
= get_image_coord(b
, w
[4]);
3044 const SpvImageOperandsMask operands
=
3045 count
> 5 ? w
[5] : SpvImageOperandsMaskNone
;
3047 if (operands
& SpvImageOperandsSampleMask
) {
3048 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
3049 SpvImageOperandsSampleMask
);
3050 image
.sample
= vtn_get_nir_ssa(b
, w
[arg
]);
3052 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
3055 if (operands
& SpvImageOperandsMakeTexelVisibleMask
) {
3056 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
3057 "MakeTexelVisible requires NonPrivateTexel to also be set.");
3058 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
3059 SpvImageOperandsMakeTexelVisibleMask
);
3060 semantics
= SpvMemorySemanticsMakeVisibleMask
;
3061 scope
= vtn_constant_uint(b
, w
[arg
]);
3064 if (operands
& SpvImageOperandsLodMask
) {
3065 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
3066 SpvImageOperandsLodMask
);
3067 image
.lod
= vtn_get_nir_ssa(b
, w
[arg
]);
3069 image
.lod
= nir_imm_int(&b
->nb
, 0);
3072 if (operands
& SpvImageOperandsVolatileTexelMask
)
3073 access
|= ACCESS_VOLATILE
;
3078 case SpvOpImageWrite
: {
3079 res_val
= vtn_untyped_value(b
, w
[1]);
3080 image
.image
= vtn_get_image(b
, w
[1]);
3081 image
.coord
= get_image_coord(b
, w
[2]);
3085 const SpvImageOperandsMask operands
=
3086 count
> 4 ? w
[4] : SpvImageOperandsMaskNone
;
3088 if (operands
& SpvImageOperandsSampleMask
) {
3089 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
3090 SpvImageOperandsSampleMask
);
3091 image
.sample
= vtn_get_nir_ssa(b
, w
[arg
]);
3093 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
3096 if (operands
& SpvImageOperandsMakeTexelAvailableMask
) {
3097 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
3098 "MakeTexelAvailable requires NonPrivateTexel to also be set.");
3099 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
3100 SpvImageOperandsMakeTexelAvailableMask
);
3101 semantics
= SpvMemorySemanticsMakeAvailableMask
;
3102 scope
= vtn_constant_uint(b
, w
[arg
]);
3105 if (operands
& SpvImageOperandsLodMask
) {
3106 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
3107 SpvImageOperandsLodMask
);
3108 image
.lod
= vtn_get_nir_ssa(b
, w
[arg
]);
3110 image
.lod
= nir_imm_int(&b
->nb
, 0);
3113 if (operands
& SpvImageOperandsVolatileTexelMask
)
3114 access
|= ACCESS_VOLATILE
;
3120 vtn_fail_with_opcode("Invalid image opcode", opcode
);
3123 if (semantics
& SpvMemorySemanticsVolatileMask
)
3124 access
|= ACCESS_VOLATILE
;
3126 nir_intrinsic_op op
;
3128 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
3129 OP(ImageQuerySize
, size
)
3130 OP(ImageQuerySizeLod
, size
)
3132 OP(ImageWrite
, store
)
3133 OP(AtomicLoad
, load
)
3134 OP(AtomicStore
, store
)
3135 OP(AtomicExchange
, atomic_exchange
)
3136 OP(AtomicCompareExchange
, atomic_comp_swap
)
3137 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3138 OP(AtomicIIncrement
, atomic_add
)
3139 OP(AtomicIDecrement
, atomic_add
)
3140 OP(AtomicIAdd
, atomic_add
)
3141 OP(AtomicISub
, atomic_add
)
3142 OP(AtomicSMin
, atomic_imin
)
3143 OP(AtomicUMin
, atomic_umin
)
3144 OP(AtomicSMax
, atomic_imax
)
3145 OP(AtomicUMax
, atomic_umax
)
3146 OP(AtomicAnd
, atomic_and
)
3147 OP(AtomicOr
, atomic_or
)
3148 OP(AtomicXor
, atomic_xor
)
3149 OP(AtomicFAddEXT
, atomic_fadd
)
3150 OP(ImageQueryFormat
, format
)
3151 OP(ImageQueryOrder
, order
)
3154 vtn_fail_with_opcode("Invalid image opcode", opcode
);
3157 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3159 intrin
->src
[0] = nir_src_for_ssa(&image
.image
->dest
.ssa
);
3162 case SpvOpImageQuerySize
:
3163 case SpvOpImageQuerySizeLod
:
3164 case SpvOpImageQueryFormat
:
3165 case SpvOpImageQueryOrder
:
3168 /* The image coordinate is always 4 components but we may not have that
3169 * many. Swizzle to compensate.
3171 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
3172 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
3176 /* The Vulkan spec says:
3178 * "If an instruction loads from or stores to a resource (including
3179 * atomics and image instructions) and the resource descriptor being
3180 * accessed is not dynamically uniform, then the operand corresponding
3181 * to that resource (e.g. the pointer or sampled image operand) must be
3182 * decorated with NonUniform."
3184 * It's very careful to specify that the exact operand must be decorated
3185 * NonUniform. The SPIR-V parser is not expected to chase through long
3186 * chains to find the NonUniform decoration. It's either right there or we
3187 * can assume it doesn't exist.
3189 vtn_foreach_decoration(b
, res_val
, non_uniform_decoration_cb
, &access
);
3190 nir_intrinsic_set_access(intrin
, access
);
3193 case SpvOpImageQueryFormat
:
3194 case SpvOpImageQueryOrder
:
3195 /* No additional sources */
3197 case SpvOpImageQuerySize
:
3198 intrin
->src
[1] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 0));
3200 case SpvOpImageQuerySizeLod
:
3201 intrin
->src
[1] = nir_src_for_ssa(image
.lod
);
3203 case SpvOpAtomicLoad
:
3204 case SpvOpImageRead
:
3205 /* Only OpImageRead can support a lod parameter if
3206 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
3207 * intrinsics definition for atomics requires us to set it for
3210 intrin
->src
[3] = nir_src_for_ssa(image
.lod
);
3212 case SpvOpAtomicStore
:
3213 case SpvOpImageWrite
: {
3214 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
3215 struct vtn_ssa_value
*value
= vtn_ssa_value(b
, value_id
);
3216 /* nir_intrinsic_image_deref_store always takes a vec4 value */
3217 assert(op
== nir_intrinsic_image_deref_store
);
3218 intrin
->num_components
= 4;
3219 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
->def
));
3220 /* Only OpImageWrite can support a lod parameter if
3221 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
3222 * intrinsics definition for atomics requires us to set it for
3225 intrin
->src
[4] = nir_src_for_ssa(image
.lod
);
3227 if (opcode
== SpvOpImageWrite
)
3228 nir_intrinsic_set_type(intrin
, nir_get_nir_type_for_glsl_type(value
->type
));
3232 case SpvOpAtomicCompareExchange
:
3233 case SpvOpAtomicCompareExchangeWeak
:
3234 case SpvOpAtomicIIncrement
:
3235 case SpvOpAtomicIDecrement
:
3236 case SpvOpAtomicExchange
:
3237 case SpvOpAtomicIAdd
:
3238 case SpvOpAtomicISub
:
3239 case SpvOpAtomicSMin
:
3240 case SpvOpAtomicUMin
:
3241 case SpvOpAtomicSMax
:
3242 case SpvOpAtomicUMax
:
3243 case SpvOpAtomicAnd
:
3245 case SpvOpAtomicXor
:
3246 case SpvOpAtomicFAddEXT
:
3247 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
3251 vtn_fail_with_opcode("Invalid image opcode", opcode
);
3254 /* Image operations implicitly have the Image storage memory semantics. */
3255 semantics
|= SpvMemorySemanticsImageMemoryMask
;
3257 SpvMemorySemanticsMask before_semantics
;
3258 SpvMemorySemanticsMask after_semantics
;
3259 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
3261 if (before_semantics
)
3262 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
3264 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
3265 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
3267 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
3268 if (nir_intrinsic_infos
[op
].dest_components
== 0)
3269 intrin
->num_components
= dest_components
;
3271 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
3272 nir_intrinsic_dest_components(intrin
), 32, NULL
);
3274 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3276 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
3277 if (nir_intrinsic_dest_components(intrin
) != dest_components
)
3278 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
3280 vtn_push_nir_ssa(b
, w
[2], result
);
3282 if (opcode
== SpvOpImageRead
)
3283 nir_intrinsic_set_type(intrin
, nir_get_nir_type_for_glsl_type(type
->type
));
3285 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3288 if (after_semantics
)
3289 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
3292 static nir_intrinsic_op
3293 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3296 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
3297 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
3298 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
3299 OP(AtomicExchange
, atomic_exchange
)
3300 OP(AtomicCompareExchange
, atomic_comp_swap
)
3301 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3302 OP(AtomicIIncrement
, atomic_add
)
3303 OP(AtomicIDecrement
, atomic_add
)
3304 OP(AtomicIAdd
, atomic_add
)
3305 OP(AtomicISub
, atomic_add
)
3306 OP(AtomicSMin
, atomic_imin
)
3307 OP(AtomicUMin
, atomic_umin
)
3308 OP(AtomicSMax
, atomic_imax
)
3309 OP(AtomicUMax
, atomic_umax
)
3310 OP(AtomicAnd
, atomic_and
)
3311 OP(AtomicOr
, atomic_or
)
3312 OP(AtomicXor
, atomic_xor
)
3313 OP(AtomicFAddEXT
, atomic_fadd
)
3316 vtn_fail_with_opcode("Invalid SSBO atomic", opcode
);
3320 static nir_intrinsic_op
3321 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3324 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
3325 OP(AtomicLoad
, read_deref
)
3326 OP(AtomicExchange
, exchange
)
3327 OP(AtomicCompareExchange
, comp_swap
)
3328 OP(AtomicCompareExchangeWeak
, comp_swap
)
3329 OP(AtomicIIncrement
, inc_deref
)
3330 OP(AtomicIDecrement
, post_dec_deref
)
3331 OP(AtomicIAdd
, add_deref
)
3332 OP(AtomicISub
, add_deref
)
3333 OP(AtomicUMin
, min_deref
)
3334 OP(AtomicUMax
, max_deref
)
3335 OP(AtomicAnd
, and_deref
)
3336 OP(AtomicOr
, or_deref
)
3337 OP(AtomicXor
, xor_deref
)
3340 /* We left the following out: AtomicStore, AtomicSMin and
3341 * AtomicSmax. Right now there are not nir intrinsics for them. At this
3342 * moment Atomic Counter support is needed for ARB_spirv support, so is
3343 * only need to support GLSL Atomic Counters that are uints and don't
3344 * allow direct storage.
3346 vtn_fail("Invalid uniform atomic");
3350 static nir_intrinsic_op
3351 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3354 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
3355 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
3356 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
3357 OP(AtomicExchange
, atomic_exchange
)
3358 OP(AtomicCompareExchange
, atomic_comp_swap
)
3359 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3360 OP(AtomicIIncrement
, atomic_add
)
3361 OP(AtomicIDecrement
, atomic_add
)
3362 OP(AtomicIAdd
, atomic_add
)
3363 OP(AtomicISub
, atomic_add
)
3364 OP(AtomicSMin
, atomic_imin
)
3365 OP(AtomicUMin
, atomic_umin
)
3366 OP(AtomicSMax
, atomic_imax
)
3367 OP(AtomicUMax
, atomic_umax
)
3368 OP(AtomicAnd
, atomic_and
)
3369 OP(AtomicOr
, atomic_or
)
3370 OP(AtomicXor
, atomic_xor
)
3371 OP(AtomicFAddEXT
, atomic_fadd
)
3374 vtn_fail_with_opcode("Invalid shared atomic", opcode
);
3379 * Handles shared atomics, ssbo atomics and atomic counters.
3382 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
3383 const uint32_t *w
, UNUSED
unsigned count
)
3385 struct vtn_pointer
*ptr
;
3386 nir_intrinsic_instr
*atomic
;
3388 SpvScope scope
= SpvScopeInvocation
;
3389 SpvMemorySemanticsMask semantics
= 0;
3390 enum gl_access_qualifier access
= 0;
3393 case SpvOpAtomicLoad
:
3394 case SpvOpAtomicExchange
:
3395 case SpvOpAtomicCompareExchange
:
3396 case SpvOpAtomicCompareExchangeWeak
:
3397 case SpvOpAtomicIIncrement
:
3398 case SpvOpAtomicIDecrement
:
3399 case SpvOpAtomicIAdd
:
3400 case SpvOpAtomicISub
:
3401 case SpvOpAtomicSMin
:
3402 case SpvOpAtomicUMin
:
3403 case SpvOpAtomicSMax
:
3404 case SpvOpAtomicUMax
:
3405 case SpvOpAtomicAnd
:
3407 case SpvOpAtomicXor
:
3408 case SpvOpAtomicFAddEXT
:
3409 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
3410 scope
= vtn_constant_uint(b
, w
[4]);
3411 semantics
= vtn_constant_uint(b
, w
[5]);
3414 case SpvOpAtomicStore
:
3415 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
3416 scope
= vtn_constant_uint(b
, w
[2]);
3417 semantics
= vtn_constant_uint(b
, w
[3]);
3421 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3424 if (semantics
& SpvMemorySemanticsVolatileMask
)
3425 access
|= ACCESS_VOLATILE
;
3427 /* uniform as "atomic counter uniform" */
3428 if (ptr
->mode
== vtn_variable_mode_atomic_counter
) {
3429 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3430 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
3431 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3432 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3434 /* SSBO needs to initialize index/offset. In this case we don't need to,
3435 * as that info is already stored on the ptr->var->var nir_variable (see
3436 * vtn_create_variable)
3440 case SpvOpAtomicLoad
:
3441 case SpvOpAtomicExchange
:
3442 case SpvOpAtomicCompareExchange
:
3443 case SpvOpAtomicCompareExchangeWeak
:
3444 case SpvOpAtomicIIncrement
:
3445 case SpvOpAtomicIDecrement
:
3446 case SpvOpAtomicIAdd
:
3447 case SpvOpAtomicISub
:
3448 case SpvOpAtomicSMin
:
3449 case SpvOpAtomicUMin
:
3450 case SpvOpAtomicSMax
:
3451 case SpvOpAtomicUMax
:
3452 case SpvOpAtomicAnd
:
3454 case SpvOpAtomicXor
:
3455 /* Nothing: we don't need to call fill_common_atomic_sources here, as
3456 * atomic counter uniforms doesn't have sources
3461 unreachable("Invalid SPIR-V atomic");
3464 } else if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
3465 nir_ssa_def
*offset
, *index
;
3466 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
3468 assert(ptr
->mode
== vtn_variable_mode_ssbo
);
3470 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(b
, opcode
);
3471 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3473 nir_intrinsic_set_access(atomic
, access
| ACCESS_COHERENT
);
3477 case SpvOpAtomicLoad
:
3478 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3479 nir_intrinsic_set_align(atomic
, 4, 0);
3480 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3481 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3482 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3485 case SpvOpAtomicStore
:
3486 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3487 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3488 nir_intrinsic_set_align(atomic
, 4, 0);
3489 atomic
->src
[src
++] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[4]));
3490 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3491 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3492 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3495 case SpvOpAtomicExchange
:
3496 case SpvOpAtomicCompareExchange
:
3497 case SpvOpAtomicCompareExchangeWeak
:
3498 case SpvOpAtomicIIncrement
:
3499 case SpvOpAtomicIDecrement
:
3500 case SpvOpAtomicIAdd
:
3501 case SpvOpAtomicISub
:
3502 case SpvOpAtomicSMin
:
3503 case SpvOpAtomicUMin
:
3504 case SpvOpAtomicSMax
:
3505 case SpvOpAtomicUMax
:
3506 case SpvOpAtomicAnd
:
3508 case SpvOpAtomicXor
:
3509 case SpvOpAtomicFAddEXT
:
3510 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3511 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3512 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3513 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
3517 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3520 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3521 const struct glsl_type
*deref_type
= deref
->type
;
3522 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
3523 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3524 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3526 if (ptr
->mode
!= vtn_variable_mode_workgroup
)
3527 access
|= ACCESS_COHERENT
;
3529 nir_intrinsic_set_access(atomic
, access
);
3532 case SpvOpAtomicLoad
:
3533 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3536 case SpvOpAtomicStore
:
3537 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3538 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3539 atomic
->src
[1] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[4]));
3542 case SpvOpAtomicExchange
:
3543 case SpvOpAtomicCompareExchange
:
3544 case SpvOpAtomicCompareExchangeWeak
:
3545 case SpvOpAtomicIIncrement
:
3546 case SpvOpAtomicIDecrement
:
3547 case SpvOpAtomicIAdd
:
3548 case SpvOpAtomicISub
:
3549 case SpvOpAtomicSMin
:
3550 case SpvOpAtomicUMin
:
3551 case SpvOpAtomicSMax
:
3552 case SpvOpAtomicUMax
:
3553 case SpvOpAtomicAnd
:
3555 case SpvOpAtomicXor
:
3556 case SpvOpAtomicFAddEXT
:
3557 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
3561 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3565 /* Atomic ordering operations will implicitly apply to the atomic operation
3566 * storage class, so include that too.
3568 semantics
|= vtn_mode_to_memory_semantics(ptr
->mode
);
3570 SpvMemorySemanticsMask before_semantics
;
3571 SpvMemorySemanticsMask after_semantics
;
3572 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
3574 if (before_semantics
)
3575 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
3577 if (opcode
!= SpvOpAtomicStore
) {
3578 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
3580 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
3581 glsl_get_vector_elements(type
->type
),
3582 glsl_get_bit_size(type
->type
), NULL
);
3584 vtn_push_nir_ssa(b
, w
[2], &atomic
->dest
.ssa
);
3587 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
3589 if (after_semantics
)
3590 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
3593 static nir_alu_instr
*
3594 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
3596 nir_op op
= nir_op_vec(num_components
);
3597 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
3598 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
3600 vec
->dest
.write_mask
= (1 << num_components
) - 1;
3605 struct vtn_ssa_value
*
3606 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
3608 if (src
->transposed
)
3609 return src
->transposed
;
3611 struct vtn_ssa_value
*dest
=
3612 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
3614 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
3615 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
3616 glsl_get_bit_size(src
->type
));
3617 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3618 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
3619 vec
->src
[0].swizzle
[0] = i
;
3621 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
3622 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
3623 vec
->src
[j
].swizzle
[0] = i
;
3626 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3627 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
3630 dest
->transposed
= src
;
3635 static nir_ssa_def
*
3636 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
3637 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
3638 const uint32_t *indices
)
3640 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
3642 for (unsigned i
= 0; i
< num_components
; i
++) {
3643 uint32_t index
= indices
[i
];
3644 if (index
== 0xffffffff) {
3646 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
3647 } else if (index
< src0
->num_components
) {
3648 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
3649 vec
->src
[i
].swizzle
[0] = index
;
3651 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
3652 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
3656 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3658 return &vec
->dest
.dest
.ssa
;
3662 * Concatentates a number of vectors/scalars together to produce a vector
3664 static nir_ssa_def
*
3665 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
3666 unsigned num_srcs
, nir_ssa_def
**srcs
)
3668 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
3670 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3672 * "When constructing a vector, there must be at least two Constituent
3675 vtn_assert(num_srcs
>= 2);
3677 unsigned dest_idx
= 0;
3678 for (unsigned i
= 0; i
< num_srcs
; i
++) {
3679 nir_ssa_def
*src
= srcs
[i
];
3680 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
3681 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
3682 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
3683 vec
->src
[dest_idx
].swizzle
[0] = j
;
3688 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3690 * "When constructing a vector, the total number of components in all
3691 * the operands must equal the number of components in Result Type."
3693 vtn_assert(dest_idx
== num_components
);
3695 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3697 return &vec
->dest
.dest
.ssa
;
3700 static struct vtn_ssa_value
*
3701 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3703 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3704 dest
->type
= src
->type
;
3706 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3707 dest
->def
= src
->def
;
3709 unsigned elems
= glsl_get_length(src
->type
);
3711 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3712 for (unsigned i
= 0; i
< elems
; i
++)
3713 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3719 static struct vtn_ssa_value
*
3720 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3721 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3722 unsigned num_indices
)
3724 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3726 struct vtn_ssa_value
*cur
= dest
;
3728 for (i
= 0; i
< num_indices
- 1; i
++) {
3729 /* If we got a vector here, that means the next index will be trying to
3730 * dereference a scalar.
3732 vtn_fail_if(glsl_type_is_vector_or_scalar(cur
->type
),
3733 "OpCompositeInsert has too many indices.");
3734 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3735 "All indices in an OpCompositeInsert must be in-bounds");
3736 cur
= cur
->elems
[indices
[i
]];
3739 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3740 vtn_fail_if(indices
[i
] >= glsl_get_vector_elements(cur
->type
),
3741 "All indices in an OpCompositeInsert must be in-bounds");
3743 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3744 * the component granularity. In that case, the last index will be
3745 * the index to insert the scalar into the vector.
3748 cur
->def
= nir_vector_insert_imm(&b
->nb
, cur
->def
, insert
->def
, indices
[i
]);
3750 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3751 "All indices in an OpCompositeInsert must be in-bounds");
3752 cur
->elems
[indices
[i
]] = insert
;
3758 static struct vtn_ssa_value
*
3759 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3760 const uint32_t *indices
, unsigned num_indices
)
3762 struct vtn_ssa_value
*cur
= src
;
3763 for (unsigned i
= 0; i
< num_indices
; i
++) {
3764 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3765 vtn_assert(i
== num_indices
- 1);
3766 vtn_fail_if(indices
[i
] >= glsl_get_vector_elements(cur
->type
),
3767 "All indices in an OpCompositeExtract must be in-bounds");
3769 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3770 * the component granularity. The last index will be the index of the
3771 * vector to extract.
3774 const struct glsl_type
*scalar_type
=
3775 glsl_scalar_type(glsl_get_base_type(cur
->type
));
3776 struct vtn_ssa_value
*ret
= vtn_create_ssa_value(b
, scalar_type
);
3777 ret
->def
= nir_channel(&b
->nb
, cur
->def
, indices
[i
]);
3780 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3781 "All indices in an OpCompositeExtract must be in-bounds");
3782 cur
= cur
->elems
[indices
[i
]];
3790 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3791 const uint32_t *w
, unsigned count
)
3793 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
3794 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, type
->type
);
3797 case SpvOpVectorExtractDynamic
:
3798 ssa
->def
= nir_vector_extract(&b
->nb
, vtn_get_nir_ssa(b
, w
[3]),
3799 vtn_get_nir_ssa(b
, w
[4]));
3802 case SpvOpVectorInsertDynamic
:
3803 ssa
->def
= nir_vector_insert(&b
->nb
, vtn_get_nir_ssa(b
, w
[3]),
3804 vtn_get_nir_ssa(b
, w
[4]),
3805 vtn_get_nir_ssa(b
, w
[5]));
3808 case SpvOpVectorShuffle
:
3809 ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
->type
),
3810 vtn_get_nir_ssa(b
, w
[3]),
3811 vtn_get_nir_ssa(b
, w
[4]),
3815 case SpvOpCompositeConstruct
: {
3816 unsigned elems
= count
- 3;
3818 if (glsl_type_is_vector_or_scalar(type
->type
)) {
3819 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3820 for (unsigned i
= 0; i
< elems
; i
++)
3821 srcs
[i
] = vtn_get_nir_ssa(b
, w
[3 + i
]);
3823 vtn_vector_construct(b
, glsl_get_vector_elements(type
->type
),
3826 ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3827 for (unsigned i
= 0; i
< elems
; i
++)
3828 ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3832 case SpvOpCompositeExtract
:
3833 ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3837 case SpvOpCompositeInsert
:
3838 ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3839 vtn_ssa_value(b
, w
[3]),
3843 case SpvOpCopyLogical
:
3844 ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3846 case SpvOpCopyObject
:
3847 vtn_copy_value(b
, w
[3], w
[2]);
3851 vtn_fail_with_opcode("unknown composite operation", opcode
);
3854 vtn_push_ssa_value(b
, w
[2], ssa
);
3858 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3860 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3861 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3865 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3866 SpvMemorySemanticsMask semantics
)
3868 if (b
->shader
->options
->use_scoped_barrier
) {
3869 vtn_emit_scoped_memory_barrier(b
, scope
, semantics
);
3873 static const SpvMemorySemanticsMask all_memory_semantics
=
3874 SpvMemorySemanticsUniformMemoryMask
|
3875 SpvMemorySemanticsWorkgroupMemoryMask
|
3876 SpvMemorySemanticsAtomicCounterMemoryMask
|
3877 SpvMemorySemanticsImageMemoryMask
|
3878 SpvMemorySemanticsOutputMemoryMask
;
3880 /* If we're not actually doing a memory barrier, bail */
3881 if (!(semantics
& all_memory_semantics
))
3884 /* GL and Vulkan don't have these */
3885 vtn_assert(scope
!= SpvScopeCrossDevice
);
3887 if (scope
== SpvScopeSubgroup
)
3888 return; /* Nothing to do here */
3890 if (scope
== SpvScopeWorkgroup
) {
3891 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3895 /* There's only two scopes thing left */
3896 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3898 /* Map the GLSL memoryBarrier() construct and any barriers with more than one
3899 * semantic to the corresponding NIR one.
3901 if (util_bitcount(semantics
& all_memory_semantics
) > 1) {
3902 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3903 if (semantics
& SpvMemorySemanticsOutputMemoryMask
) {
3904 /* GLSL memoryBarrier() (and the corresponding NIR one) doesn't include
3905 * TCS outputs, so we have to emit it's own intrinsic for that. We
3906 * then need to emit another memory_barrier to prevent moving
3907 * non-output operations to before the tcs_patch barrier.
3909 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_tcs_patch
);
3910 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3915 /* Issue a more specific barrier */
3916 switch (semantics
& all_memory_semantics
) {
3917 case SpvMemorySemanticsUniformMemoryMask
:
3918 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3920 case SpvMemorySemanticsWorkgroupMemoryMask
:
3921 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3923 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3924 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3926 case SpvMemorySemanticsImageMemoryMask
:
3927 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3929 case SpvMemorySemanticsOutputMemoryMask
:
3930 if (b
->nb
.shader
->info
.stage
== MESA_SHADER_TESS_CTRL
)
3931 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_tcs_patch
);
3939 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3940 const uint32_t *w
, UNUSED
unsigned count
)
3943 case SpvOpEmitVertex
:
3944 case SpvOpEmitStreamVertex
:
3945 case SpvOpEndPrimitive
:
3946 case SpvOpEndStreamPrimitive
: {
3947 nir_intrinsic_op intrinsic_op
;
3949 case SpvOpEmitVertex
:
3950 case SpvOpEmitStreamVertex
:
3951 intrinsic_op
= nir_intrinsic_emit_vertex
;
3953 case SpvOpEndPrimitive
:
3954 case SpvOpEndStreamPrimitive
:
3955 intrinsic_op
= nir_intrinsic_end_primitive
;
3958 unreachable("Invalid opcode");
3961 nir_intrinsic_instr
*intrin
=
3962 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3965 case SpvOpEmitStreamVertex
:
3966 case SpvOpEndStreamPrimitive
: {
3967 unsigned stream
= vtn_constant_uint(b
, w
[1]);
3968 nir_intrinsic_set_stream_id(intrin
, stream
);
3976 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3980 case SpvOpMemoryBarrier
: {
3981 SpvScope scope
= vtn_constant_uint(b
, w
[1]);
3982 SpvMemorySemanticsMask semantics
= vtn_constant_uint(b
, w
[2]);
3983 vtn_emit_memory_barrier(b
, scope
, semantics
);
3987 case SpvOpControlBarrier
: {
3988 SpvScope execution_scope
= vtn_constant_uint(b
, w
[1]);
3989 SpvScope memory_scope
= vtn_constant_uint(b
, w
[2]);
3990 SpvMemorySemanticsMask memory_semantics
= vtn_constant_uint(b
, w
[3]);
3992 /* GLSLang, prior to commit 8297936dd6eb3, emitted OpControlBarrier with
3993 * memory semantics of None for GLSL barrier().
3994 * And before that, prior to c3f1cdfa, emitted the OpControlBarrier with
3995 * Device instead of Workgroup for execution scope.
3997 if (b
->wa_glslang_cs_barrier
&&
3998 b
->nb
.shader
->info
.stage
== MESA_SHADER_COMPUTE
&&
3999 (execution_scope
== SpvScopeWorkgroup
||
4000 execution_scope
== SpvScopeDevice
) &&
4001 memory_semantics
== SpvMemorySemanticsMaskNone
) {
4002 execution_scope
= SpvScopeWorkgroup
;
4003 memory_scope
= SpvScopeWorkgroup
;
4004 memory_semantics
= SpvMemorySemanticsAcquireReleaseMask
|
4005 SpvMemorySemanticsWorkgroupMemoryMask
;
4008 /* From the SPIR-V spec:
4010 * "When used with the TessellationControl execution model, it also
4011 * implicitly synchronizes the Output Storage Class: Writes to Output
4012 * variables performed by any invocation executed prior to a
4013 * OpControlBarrier will be visible to any other invocation after
4014 * return from that OpControlBarrier."
4016 if (b
->nb
.shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
4017 memory_semantics
&= ~(SpvMemorySemanticsAcquireMask
|
4018 SpvMemorySemanticsReleaseMask
|
4019 SpvMemorySemanticsAcquireReleaseMask
|
4020 SpvMemorySemanticsSequentiallyConsistentMask
);
4021 memory_semantics
|= SpvMemorySemanticsAcquireReleaseMask
|
4022 SpvMemorySemanticsOutputMemoryMask
;
4025 if (b
->shader
->options
->use_scoped_barrier
) {
4026 vtn_emit_scoped_control_barrier(b
, execution_scope
, memory_scope
,
4029 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
4031 if (execution_scope
== SpvScopeWorkgroup
)
4032 vtn_emit_barrier(b
, nir_intrinsic_control_barrier
);
4038 unreachable("unknown barrier instruction");
4043 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
4044 SpvExecutionMode mode
)
4047 case SpvExecutionModeInputPoints
:
4048 case SpvExecutionModeOutputPoints
:
4049 return 0; /* GL_POINTS */
4050 case SpvExecutionModeInputLines
:
4051 return 1; /* GL_LINES */
4052 case SpvExecutionModeInputLinesAdjacency
:
4053 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
4054 case SpvExecutionModeTriangles
:
4055 return 4; /* GL_TRIANGLES */
4056 case SpvExecutionModeInputTrianglesAdjacency
:
4057 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
4058 case SpvExecutionModeQuads
:
4059 return 7; /* GL_QUADS */
4060 case SpvExecutionModeIsolines
:
4061 return 0x8E7A; /* GL_ISOLINES */
4062 case SpvExecutionModeOutputLineStrip
:
4063 return 3; /* GL_LINE_STRIP */
4064 case SpvExecutionModeOutputTriangleStrip
:
4065 return 5; /* GL_TRIANGLE_STRIP */
4067 vtn_fail("Invalid primitive type: %s (%u)",
4068 spirv_executionmode_to_string(mode
), mode
);
4073 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
4074 SpvExecutionMode mode
)
4077 case SpvExecutionModeInputPoints
:
4079 case SpvExecutionModeInputLines
:
4081 case SpvExecutionModeInputLinesAdjacency
:
4083 case SpvExecutionModeTriangles
:
4085 case SpvExecutionModeInputTrianglesAdjacency
:
4088 vtn_fail("Invalid GS input mode: %s (%u)",
4089 spirv_executionmode_to_string(mode
), mode
);
4093 static gl_shader_stage
4094 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
4097 case SpvExecutionModelVertex
:
4098 return MESA_SHADER_VERTEX
;
4099 case SpvExecutionModelTessellationControl
:
4100 return MESA_SHADER_TESS_CTRL
;
4101 case SpvExecutionModelTessellationEvaluation
:
4102 return MESA_SHADER_TESS_EVAL
;
4103 case SpvExecutionModelGeometry
:
4104 return MESA_SHADER_GEOMETRY
;
4105 case SpvExecutionModelFragment
:
4106 return MESA_SHADER_FRAGMENT
;
4107 case SpvExecutionModelGLCompute
:
4108 return MESA_SHADER_COMPUTE
;
4109 case SpvExecutionModelKernel
:
4110 return MESA_SHADER_KERNEL
;
4112 vtn_fail("Unsupported execution model: %s (%u)",
4113 spirv_executionmodel_to_string(model
), model
);
4117 #define spv_check_supported(name, cap) do { \
4118 if (!(b->options && b->options->caps.name)) \
4119 vtn_warn("Unsupported SPIR-V capability: %s (%u)", \
4120 spirv_capability_to_string(cap), cap); \
4125 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
4128 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
4129 /* Let this be a name label regardless */
4130 unsigned name_words
;
4131 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
4133 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
4134 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
4137 vtn_assert(b
->entry_point
== NULL
);
4138 b
->entry_point
= entry_point
;
4142 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4143 const uint32_t *w
, unsigned count
)
4150 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
4151 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
4152 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
4153 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
4154 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
4155 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
4158 uint32_t version
= w
[2];
4161 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
4163 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
4167 case SpvOpSourceExtension
:
4168 case SpvOpSourceContinued
:
4169 case SpvOpExtension
:
4170 case SpvOpModuleProcessed
:
4171 /* Unhandled, but these are for debug so that's ok. */
4174 case SpvOpCapability
: {
4175 SpvCapability cap
= w
[1];
4177 case SpvCapabilityMatrix
:
4178 case SpvCapabilityShader
:
4179 case SpvCapabilityGeometry
:
4180 case SpvCapabilityGeometryPointSize
:
4181 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
4182 case SpvCapabilitySampledImageArrayDynamicIndexing
:
4183 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
4184 case SpvCapabilityStorageImageArrayDynamicIndexing
:
4185 case SpvCapabilityImageRect
:
4186 case SpvCapabilitySampledRect
:
4187 case SpvCapabilitySampled1D
:
4188 case SpvCapabilityImage1D
:
4189 case SpvCapabilitySampledCubeArray
:
4190 case SpvCapabilityImageCubeArray
:
4191 case SpvCapabilitySampledBuffer
:
4192 case SpvCapabilityImageBuffer
:
4193 case SpvCapabilityImageQuery
:
4194 case SpvCapabilityDerivativeControl
:
4195 case SpvCapabilityInterpolationFunction
:
4196 case SpvCapabilityMultiViewport
:
4197 case SpvCapabilitySampleRateShading
:
4198 case SpvCapabilityClipDistance
:
4199 case SpvCapabilityCullDistance
:
4200 case SpvCapabilityInputAttachment
:
4201 case SpvCapabilityImageGatherExtended
:
4202 case SpvCapabilityStorageImageExtendedFormats
:
4203 case SpvCapabilityVector16
:
4206 case SpvCapabilityLinkage
:
4207 case SpvCapabilityFloat16Buffer
:
4208 case SpvCapabilitySparseResidency
:
4209 vtn_warn("Unsupported SPIR-V capability: %s",
4210 spirv_capability_to_string(cap
));
4213 case SpvCapabilityMinLod
:
4214 spv_check_supported(min_lod
, cap
);
4217 case SpvCapabilityAtomicStorage
:
4218 spv_check_supported(atomic_storage
, cap
);
4221 case SpvCapabilityFloat64
:
4222 spv_check_supported(float64
, cap
);
4224 case SpvCapabilityInt64
:
4225 spv_check_supported(int64
, cap
);
4227 case SpvCapabilityInt16
:
4228 spv_check_supported(int16
, cap
);
4230 case SpvCapabilityInt8
:
4231 spv_check_supported(int8
, cap
);
4234 case SpvCapabilityTransformFeedback
:
4235 spv_check_supported(transform_feedback
, cap
);
4238 case SpvCapabilityGeometryStreams
:
4239 spv_check_supported(geometry_streams
, cap
);
4242 case SpvCapabilityInt64Atomics
:
4243 spv_check_supported(int64_atomics
, cap
);
4246 case SpvCapabilityStorageImageMultisample
:
4247 spv_check_supported(storage_image_ms
, cap
);
4250 case SpvCapabilityAddresses
:
4251 spv_check_supported(address
, cap
);
4254 case SpvCapabilityKernel
:
4255 spv_check_supported(kernel
, cap
);
4258 case SpvCapabilityImageBasic
:
4259 spv_check_supported(kernel_image
, cap
);
4262 case SpvCapabilityLiteralSampler
:
4263 spv_check_supported(literal_sampler
, cap
);
4266 case SpvCapabilityImageReadWrite
:
4267 case SpvCapabilityImageMipmap
:
4268 case SpvCapabilityPipes
:
4269 case SpvCapabilityDeviceEnqueue
:
4270 case SpvCapabilityGenericPointer
:
4271 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
4272 spirv_capability_to_string(cap
));
4275 case SpvCapabilityImageMSArray
:
4276 spv_check_supported(image_ms_array
, cap
);
4279 case SpvCapabilityTessellation
:
4280 case SpvCapabilityTessellationPointSize
:
4281 spv_check_supported(tessellation
, cap
);
4284 case SpvCapabilityDrawParameters
:
4285 spv_check_supported(draw_parameters
, cap
);
4288 case SpvCapabilityStorageImageReadWithoutFormat
:
4289 spv_check_supported(image_read_without_format
, cap
);
4292 case SpvCapabilityStorageImageWriteWithoutFormat
:
4293 spv_check_supported(image_write_without_format
, cap
);
4296 case SpvCapabilityDeviceGroup
:
4297 spv_check_supported(device_group
, cap
);
4300 case SpvCapabilityMultiView
:
4301 spv_check_supported(multiview
, cap
);
4304 case SpvCapabilityGroupNonUniform
:
4305 spv_check_supported(subgroup_basic
, cap
);
4308 case SpvCapabilitySubgroupVoteKHR
:
4309 case SpvCapabilityGroupNonUniformVote
:
4310 spv_check_supported(subgroup_vote
, cap
);
4313 case SpvCapabilitySubgroupBallotKHR
:
4314 case SpvCapabilityGroupNonUniformBallot
:
4315 spv_check_supported(subgroup_ballot
, cap
);
4318 case SpvCapabilityGroupNonUniformShuffle
:
4319 case SpvCapabilityGroupNonUniformShuffleRelative
:
4320 spv_check_supported(subgroup_shuffle
, cap
);
4323 case SpvCapabilityGroupNonUniformQuad
:
4324 spv_check_supported(subgroup_quad
, cap
);
4327 case SpvCapabilityGroupNonUniformArithmetic
:
4328 case SpvCapabilityGroupNonUniformClustered
:
4329 spv_check_supported(subgroup_arithmetic
, cap
);
4332 case SpvCapabilityGroups
:
4333 spv_check_supported(amd_shader_ballot
, cap
);
4336 case SpvCapabilityVariablePointersStorageBuffer
:
4337 case SpvCapabilityVariablePointers
:
4338 spv_check_supported(variable_pointers
, cap
);
4339 b
->variable_pointers
= true;
4342 case SpvCapabilityStorageUniformBufferBlock16
:
4343 case SpvCapabilityStorageUniform16
:
4344 case SpvCapabilityStoragePushConstant16
:
4345 case SpvCapabilityStorageInputOutput16
:
4346 spv_check_supported(storage_16bit
, cap
);
4349 case SpvCapabilityShaderLayer
:
4350 case SpvCapabilityShaderViewportIndex
:
4351 case SpvCapabilityShaderViewportIndexLayerEXT
:
4352 spv_check_supported(shader_viewport_index_layer
, cap
);
4355 case SpvCapabilityStorageBuffer8BitAccess
:
4356 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
4357 case SpvCapabilityStoragePushConstant8
:
4358 spv_check_supported(storage_8bit
, cap
);
4361 case SpvCapabilityShaderNonUniformEXT
:
4362 spv_check_supported(descriptor_indexing
, cap
);
4365 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
4366 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
4367 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
4368 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
4371 case SpvCapabilityUniformBufferArrayNonUniformIndexingEXT
:
4372 case SpvCapabilitySampledImageArrayNonUniformIndexingEXT
:
4373 case SpvCapabilityStorageBufferArrayNonUniformIndexingEXT
:
4374 case SpvCapabilityStorageImageArrayNonUniformIndexingEXT
:
4375 case SpvCapabilityInputAttachmentArrayNonUniformIndexingEXT
:
4376 case SpvCapabilityUniformTexelBufferArrayNonUniformIndexingEXT
:
4377 case SpvCapabilityStorageTexelBufferArrayNonUniformIndexingEXT
:
4378 spv_check_supported(descriptor_array_non_uniform_indexing
, cap
);
4381 case SpvCapabilityRuntimeDescriptorArrayEXT
:
4382 spv_check_supported(runtime_descriptor_array
, cap
);
4385 case SpvCapabilityStencilExportEXT
:
4386 spv_check_supported(stencil_export
, cap
);
4389 case SpvCapabilitySampleMaskPostDepthCoverage
:
4390 spv_check_supported(post_depth_coverage
, cap
);
4393 case SpvCapabilityDenormFlushToZero
:
4394 case SpvCapabilityDenormPreserve
:
4395 case SpvCapabilitySignedZeroInfNanPreserve
:
4396 case SpvCapabilityRoundingModeRTE
:
4397 case SpvCapabilityRoundingModeRTZ
:
4398 spv_check_supported(float_controls
, cap
);
4401 case SpvCapabilityPhysicalStorageBufferAddresses
:
4402 spv_check_supported(physical_storage_buffer_address
, cap
);
4405 case SpvCapabilityComputeDerivativeGroupQuadsNV
:
4406 case SpvCapabilityComputeDerivativeGroupLinearNV
:
4407 spv_check_supported(derivative_group
, cap
);
4410 case SpvCapabilityFloat16
:
4411 spv_check_supported(float16
, cap
);
4414 case SpvCapabilityFragmentShaderSampleInterlockEXT
:
4415 spv_check_supported(fragment_shader_sample_interlock
, cap
);
4418 case SpvCapabilityFragmentShaderPixelInterlockEXT
:
4419 spv_check_supported(fragment_shader_pixel_interlock
, cap
);
4422 case SpvCapabilityDemoteToHelperInvocationEXT
:
4423 spv_check_supported(demote_to_helper_invocation
, cap
);
4426 case SpvCapabilityShaderClockKHR
:
4427 spv_check_supported(shader_clock
, cap
);
4430 case SpvCapabilityVulkanMemoryModel
:
4431 spv_check_supported(vk_memory_model
, cap
);
4434 case SpvCapabilityVulkanMemoryModelDeviceScope
:
4435 spv_check_supported(vk_memory_model_device_scope
, cap
);
4438 case SpvCapabilityImageReadWriteLodAMD
:
4439 spv_check_supported(amd_image_read_write_lod
, cap
);
4442 case SpvCapabilityIntegerFunctions2INTEL
:
4443 spv_check_supported(integer_functions2
, cap
);
4446 case SpvCapabilityFragmentMaskAMD
:
4447 spv_check_supported(amd_fragment_mask
, cap
);
4450 case SpvCapabilityImageGatherBiasLodAMD
:
4451 spv_check_supported(amd_image_gather_bias_lod
, cap
);
4454 case SpvCapabilityAtomicFloat32AddEXT
:
4455 spv_check_supported(float32_atomic_add
, cap
);
4458 case SpvCapabilityAtomicFloat64AddEXT
:
4459 spv_check_supported(float64_atomic_add
, cap
);
4463 vtn_fail("Unhandled capability: %s (%u)",
4464 spirv_capability_to_string(cap
), cap
);
4469 case SpvOpExtInstImport
:
4470 vtn_handle_extension(b
, opcode
, w
, count
);
4473 case SpvOpMemoryModel
:
4475 case SpvAddressingModelPhysical32
:
4476 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4477 "AddressingModelPhysical32 only supported for kernels");
4478 b
->shader
->info
.cs
.ptr_size
= 32;
4479 b
->physical_ptrs
= true;
4480 assert(nir_address_format_bit_size(b
->options
->global_addr_format
) == 32);
4481 assert(nir_address_format_num_components(b
->options
->global_addr_format
) == 1);
4482 assert(nir_address_format_bit_size(b
->options
->shared_addr_format
) == 32);
4483 assert(nir_address_format_num_components(b
->options
->shared_addr_format
) == 1);
4484 if (!b
->options
->constant_as_global
) {
4485 assert(nir_address_format_bit_size(b
->options
->ubo_addr_format
) == 32);
4486 assert(nir_address_format_num_components(b
->options
->ubo_addr_format
) == 1);
4489 case SpvAddressingModelPhysical64
:
4490 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4491 "AddressingModelPhysical64 only supported for kernels");
4492 b
->shader
->info
.cs
.ptr_size
= 64;
4493 b
->physical_ptrs
= true;
4494 assert(nir_address_format_bit_size(b
->options
->global_addr_format
) == 64);
4495 assert(nir_address_format_num_components(b
->options
->global_addr_format
) == 1);
4496 assert(nir_address_format_bit_size(b
->options
->shared_addr_format
) == 64);
4497 assert(nir_address_format_num_components(b
->options
->shared_addr_format
) == 1);
4498 if (!b
->options
->constant_as_global
) {
4499 assert(nir_address_format_bit_size(b
->options
->ubo_addr_format
) == 64);
4500 assert(nir_address_format_num_components(b
->options
->ubo_addr_format
) == 1);
4503 case SpvAddressingModelLogical
:
4504 vtn_fail_if(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
,
4505 "AddressingModelLogical only supported for shaders");
4506 b
->physical_ptrs
= false;
4508 case SpvAddressingModelPhysicalStorageBuffer64
:
4509 vtn_fail_if(!b
->options
||
4510 !b
->options
->caps
.physical_storage_buffer_address
,
4511 "AddressingModelPhysicalStorageBuffer64 not supported");
4514 vtn_fail("Unknown addressing model: %s (%u)",
4515 spirv_addressingmodel_to_string(w
[1]), w
[1]);
4519 b
->mem_model
= w
[2];
4521 case SpvMemoryModelSimple
:
4522 case SpvMemoryModelGLSL450
:
4523 case SpvMemoryModelOpenCL
:
4525 case SpvMemoryModelVulkan
:
4526 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
4527 "Vulkan memory model is unsupported by this driver");
4530 vtn_fail("Unsupported memory model: %s",
4531 spirv_memorymodel_to_string(w
[2]));
4536 case SpvOpEntryPoint
:
4537 vtn_handle_entry_point(b
, w
, count
);
4541 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
4542 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4546 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4549 case SpvOpMemberName
:
4553 case SpvOpExecutionMode
:
4554 case SpvOpExecutionModeId
:
4555 case SpvOpDecorationGroup
:
4557 case SpvOpDecorateId
:
4558 case SpvOpMemberDecorate
:
4559 case SpvOpGroupDecorate
:
4560 case SpvOpGroupMemberDecorate
:
4561 case SpvOpDecorateString
:
4562 case SpvOpMemberDecorateString
:
4563 vtn_handle_decoration(b
, opcode
, w
, count
);
4566 case SpvOpExtInst
: {
4567 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4568 if (val
->ext_handler
== vtn_handle_non_semantic_instruction
) {
4569 /* NonSemantic extended instructions are acceptable in preamble. */
4570 vtn_handle_non_semantic_instruction(b
, w
[4], w
, count
);
4573 return false; /* End of preamble. */
4578 return false; /* End of preamble */
4585 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4586 const struct vtn_decoration
*mode
, UNUSED
void *data
)
4588 vtn_assert(b
->entry_point
== entry_point
);
4590 switch(mode
->exec_mode
) {
4591 case SpvExecutionModeOriginUpperLeft
:
4592 case SpvExecutionModeOriginLowerLeft
:
4593 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4594 b
->shader
->info
.fs
.origin_upper_left
=
4595 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
4598 case SpvExecutionModeEarlyFragmentTests
:
4599 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4600 b
->shader
->info
.fs
.early_fragment_tests
= true;
4603 case SpvExecutionModePostDepthCoverage
:
4604 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4605 b
->shader
->info
.fs
.post_depth_coverage
= true;
4608 case SpvExecutionModeInvocations
:
4609 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4610 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->operands
[0]);
4613 case SpvExecutionModeDepthReplacing
:
4614 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4615 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
4617 case SpvExecutionModeDepthGreater
:
4618 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4619 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
4621 case SpvExecutionModeDepthLess
:
4622 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4623 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
4625 case SpvExecutionModeDepthUnchanged
:
4626 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4627 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
4630 case SpvExecutionModeLocalSize
:
4631 vtn_assert(gl_shader_stage_is_compute(b
->shader
->info
.stage
));
4632 b
->shader
->info
.cs
.local_size
[0] = mode
->operands
[0];
4633 b
->shader
->info
.cs
.local_size
[1] = mode
->operands
[1];
4634 b
->shader
->info
.cs
.local_size
[2] = mode
->operands
[2];
4637 case SpvExecutionModeLocalSizeHint
:
4638 break; /* Nothing to do with this */
4640 case SpvExecutionModeOutputVertices
:
4641 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4642 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4643 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->operands
[0];
4645 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4646 b
->shader
->info
.gs
.vertices_out
= mode
->operands
[0];
4650 case SpvExecutionModeInputPoints
:
4651 case SpvExecutionModeInputLines
:
4652 case SpvExecutionModeInputLinesAdjacency
:
4653 case SpvExecutionModeTriangles
:
4654 case SpvExecutionModeInputTrianglesAdjacency
:
4655 case SpvExecutionModeQuads
:
4656 case SpvExecutionModeIsolines
:
4657 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4658 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4659 b
->shader
->info
.tess
.primitive_mode
=
4660 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4662 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4663 b
->shader
->info
.gs
.vertices_in
=
4664 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
4665 b
->shader
->info
.gs
.input_primitive
=
4666 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4670 case SpvExecutionModeOutputPoints
:
4671 case SpvExecutionModeOutputLineStrip
:
4672 case SpvExecutionModeOutputTriangleStrip
:
4673 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4674 b
->shader
->info
.gs
.output_primitive
=
4675 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4678 case SpvExecutionModeSpacingEqual
:
4679 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4680 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4681 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
4683 case SpvExecutionModeSpacingFractionalEven
:
4684 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4685 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4686 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
4688 case SpvExecutionModeSpacingFractionalOdd
:
4689 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4690 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4691 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
4693 case SpvExecutionModeVertexOrderCw
:
4694 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4695 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4696 b
->shader
->info
.tess
.ccw
= false;
4698 case SpvExecutionModeVertexOrderCcw
:
4699 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4700 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4701 b
->shader
->info
.tess
.ccw
= true;
4703 case SpvExecutionModePointMode
:
4704 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4705 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4706 b
->shader
->info
.tess
.point_mode
= true;
4709 case SpvExecutionModePixelCenterInteger
:
4710 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4711 b
->shader
->info
.fs
.pixel_center_integer
= true;
4714 case SpvExecutionModeXfb
:
4715 b
->shader
->info
.has_transform_feedback_varyings
= true;
4718 case SpvExecutionModeVecTypeHint
:
4721 case SpvExecutionModeContractionOff
:
4722 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
4723 vtn_warn("ExectionMode only allowed for CL-style kernels: %s",
4724 spirv_executionmode_to_string(mode
->exec_mode
));
4729 case SpvExecutionModeStencilRefReplacingEXT
:
4730 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4733 case SpvExecutionModeDerivativeGroupQuadsNV
:
4734 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4735 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_QUADS
;
4738 case SpvExecutionModeDerivativeGroupLinearNV
:
4739 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4740 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_LINEAR
;
4743 case SpvExecutionModePixelInterlockOrderedEXT
:
4744 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4745 b
->shader
->info
.fs
.pixel_interlock_ordered
= true;
4748 case SpvExecutionModePixelInterlockUnorderedEXT
:
4749 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4750 b
->shader
->info
.fs
.pixel_interlock_unordered
= true;
4753 case SpvExecutionModeSampleInterlockOrderedEXT
:
4754 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4755 b
->shader
->info
.fs
.sample_interlock_ordered
= true;
4758 case SpvExecutionModeSampleInterlockUnorderedEXT
:
4759 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4760 b
->shader
->info
.fs
.sample_interlock_unordered
= true;
4763 case SpvExecutionModeDenormPreserve
:
4764 case SpvExecutionModeDenormFlushToZero
:
4765 case SpvExecutionModeSignedZeroInfNanPreserve
:
4766 case SpvExecutionModeRoundingModeRTE
:
4767 case SpvExecutionModeRoundingModeRTZ
: {
4768 unsigned execution_mode
= 0;
4769 switch (mode
->exec_mode
) {
4770 case SpvExecutionModeDenormPreserve
:
4771 switch (mode
->operands
[0]) {
4772 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP16
; break;
4773 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP32
; break;
4774 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP64
; break;
4775 default: vtn_fail("Floating point type not supported");
4778 case SpvExecutionModeDenormFlushToZero
:
4779 switch (mode
->operands
[0]) {
4780 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
; break;
4781 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32
; break;
4782 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64
; break;
4783 default: vtn_fail("Floating point type not supported");
4786 case SpvExecutionModeSignedZeroInfNanPreserve
:
4787 switch (mode
->operands
[0]) {
4788 case 16: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16
; break;
4789 case 32: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32
; break;
4790 case 64: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64
; break;
4791 default: vtn_fail("Floating point type not supported");
4794 case SpvExecutionModeRoundingModeRTE
:
4795 switch (mode
->operands
[0]) {
4796 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16
; break;
4797 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32
; break;
4798 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64
; break;
4799 default: vtn_fail("Floating point type not supported");
4802 case SpvExecutionModeRoundingModeRTZ
:
4803 switch (mode
->operands
[0]) {
4804 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
; break;
4805 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
; break;
4806 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
; break;
4807 default: vtn_fail("Floating point type not supported");
4814 b
->shader
->info
.float_controls_execution_mode
|= execution_mode
;
4818 case SpvExecutionModeLocalSizeId
:
4819 case SpvExecutionModeLocalSizeHintId
:
4820 /* Handled later by vtn_handle_execution_mode_id(). */
4824 vtn_fail("Unhandled execution mode: %s (%u)",
4825 spirv_executionmode_to_string(mode
->exec_mode
),
4831 vtn_handle_execution_mode_id(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4832 const struct vtn_decoration
*mode
, UNUSED
void *data
)
4835 vtn_assert(b
->entry_point
== entry_point
);
4837 switch (mode
->exec_mode
) {
4838 case SpvExecutionModeLocalSizeId
:
4839 b
->shader
->info
.cs
.local_size
[0] = vtn_constant_uint(b
, mode
->operands
[0]);
4840 b
->shader
->info
.cs
.local_size
[1] = vtn_constant_uint(b
, mode
->operands
[1]);
4841 b
->shader
->info
.cs
.local_size
[2] = vtn_constant_uint(b
, mode
->operands
[2]);
4844 case SpvExecutionModeLocalSizeHintId
:
4845 /* Nothing to do with this hint. */
4849 /* Nothing to do. Literal execution modes already handled by
4850 * vtn_handle_execution_mode(). */
4856 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4857 const uint32_t *w
, unsigned count
)
4859 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
4863 case SpvOpSourceContinued
:
4864 case SpvOpSourceExtension
:
4865 case SpvOpExtension
:
4866 case SpvOpCapability
:
4867 case SpvOpExtInstImport
:
4868 case SpvOpMemoryModel
:
4869 case SpvOpEntryPoint
:
4870 case SpvOpExecutionMode
:
4873 case SpvOpMemberName
:
4874 case SpvOpDecorationGroup
:
4876 case SpvOpDecorateId
:
4877 case SpvOpMemberDecorate
:
4878 case SpvOpGroupDecorate
:
4879 case SpvOpGroupMemberDecorate
:
4880 case SpvOpDecorateString
:
4881 case SpvOpMemberDecorateString
:
4882 vtn_fail("Invalid opcode types and variables section");
4888 case SpvOpTypeFloat
:
4889 case SpvOpTypeVector
:
4890 case SpvOpTypeMatrix
:
4891 case SpvOpTypeImage
:
4892 case SpvOpTypeSampler
:
4893 case SpvOpTypeSampledImage
:
4894 case SpvOpTypeArray
:
4895 case SpvOpTypeRuntimeArray
:
4896 case SpvOpTypeStruct
:
4897 case SpvOpTypeOpaque
:
4898 case SpvOpTypePointer
:
4899 case SpvOpTypeForwardPointer
:
4900 case SpvOpTypeFunction
:
4901 case SpvOpTypeEvent
:
4902 case SpvOpTypeDeviceEvent
:
4903 case SpvOpTypeReserveId
:
4904 case SpvOpTypeQueue
:
4906 vtn_handle_type(b
, opcode
, w
, count
);
4909 case SpvOpConstantTrue
:
4910 case SpvOpConstantFalse
:
4912 case SpvOpConstantComposite
:
4913 case SpvOpConstantNull
:
4914 case SpvOpSpecConstantTrue
:
4915 case SpvOpSpecConstantFalse
:
4916 case SpvOpSpecConstant
:
4917 case SpvOpSpecConstantComposite
:
4918 case SpvOpSpecConstantOp
:
4919 vtn_handle_constant(b
, opcode
, w
, count
);
4924 case SpvOpConstantSampler
:
4925 vtn_handle_variables(b
, opcode
, w
, count
);
4928 case SpvOpExtInst
: {
4929 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4930 /* NonSemantic extended instructions are acceptable in preamble, others
4931 * will indicate the end of preamble.
4933 return val
->ext_handler
== vtn_handle_non_semantic_instruction
;
4937 return false; /* End of preamble */
4943 static struct vtn_ssa_value
*
4944 vtn_nir_select(struct vtn_builder
*b
, struct vtn_ssa_value
*src0
,
4945 struct vtn_ssa_value
*src1
, struct vtn_ssa_value
*src2
)
4947 struct vtn_ssa_value
*dest
= rzalloc(b
, struct vtn_ssa_value
);
4948 dest
->type
= src1
->type
;
4950 if (glsl_type_is_vector_or_scalar(src1
->type
)) {
4951 dest
->def
= nir_bcsel(&b
->nb
, src0
->def
, src1
->def
, src2
->def
);
4953 unsigned elems
= glsl_get_length(src1
->type
);
4955 dest
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
4956 for (unsigned i
= 0; i
< elems
; i
++) {
4957 dest
->elems
[i
] = vtn_nir_select(b
, src0
,
4958 src1
->elems
[i
], src2
->elems
[i
]);
4966 vtn_handle_select(struct vtn_builder
*b
, SpvOp opcode
,
4967 const uint32_t *w
, unsigned count
)
4969 /* Handle OpSelect up-front here because it needs to be able to handle
4970 * pointers and not just regular vectors and scalars.
4972 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
4973 struct vtn_value
*cond_val
= vtn_untyped_value(b
, w
[3]);
4974 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
4975 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
4977 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4978 obj2_val
->type
!= res_val
->type
,
4979 "Object types must match the result type in OpSelect");
4981 vtn_fail_if((cond_val
->type
->base_type
!= vtn_base_type_scalar
&&
4982 cond_val
->type
->base_type
!= vtn_base_type_vector
) ||
4983 !glsl_type_is_boolean(cond_val
->type
->type
),
4984 "OpSelect must have either a vector of booleans or "
4985 "a boolean as Condition type");
4987 vtn_fail_if(cond_val
->type
->base_type
== vtn_base_type_vector
&&
4988 (res_val
->type
->base_type
!= vtn_base_type_vector
||
4989 res_val
->type
->length
!= cond_val
->type
->length
),
4990 "When Condition type in OpSelect is a vector, the Result "
4991 "type must be a vector of the same length");
4993 switch (res_val
->type
->base_type
) {
4994 case vtn_base_type_scalar
:
4995 case vtn_base_type_vector
:
4996 case vtn_base_type_matrix
:
4997 case vtn_base_type_array
:
4998 case vtn_base_type_struct
:
5001 case vtn_base_type_pointer
:
5002 /* We need to have actual storage for pointer types. */
5003 vtn_fail_if(res_val
->type
->type
== NULL
,
5004 "Invalid pointer result type for OpSelect");
5007 vtn_fail("Result type of OpSelect must be a scalar, composite, or pointer");
5010 vtn_push_ssa_value(b
, w
[2],
5011 vtn_nir_select(b
, vtn_ssa_value(b
, w
[3]),
5012 vtn_ssa_value(b
, w
[4]),
5013 vtn_ssa_value(b
, w
[5])));
5017 vtn_handle_ptr(struct vtn_builder
*b
, SpvOp opcode
,
5018 const uint32_t *w
, unsigned count
)
5020 struct vtn_type
*type1
= vtn_get_value_type(b
, w
[3]);
5021 struct vtn_type
*type2
= vtn_get_value_type(b
, w
[4]);
5022 vtn_fail_if(type1
->base_type
!= vtn_base_type_pointer
||
5023 type2
->base_type
!= vtn_base_type_pointer
,
5024 "%s operands must have pointer types",
5025 spirv_op_to_string(opcode
));
5026 vtn_fail_if(type1
->storage_class
!= type2
->storage_class
,
5027 "%s operands must have the same storage class",
5028 spirv_op_to_string(opcode
));
5030 struct vtn_type
*vtn_type
= vtn_get_type(b
, w
[1]);
5031 const struct glsl_type
*type
= vtn_type
->type
;
5033 nir_address_format addr_format
= vtn_mode_to_address_format(
5034 b
, vtn_storage_class_to_mode(b
, type1
->storage_class
, NULL
, NULL
));
5039 case SpvOpPtrDiff
: {
5040 /* OpPtrDiff returns the difference in number of elements (not byte offset). */
5041 unsigned elem_size
, elem_align
;
5042 glsl_get_natural_size_align_bytes(type1
->deref
->type
,
5043 &elem_size
, &elem_align
);
5045 def
= nir_build_addr_isub(&b
->nb
,
5046 vtn_get_nir_ssa(b
, w
[3]),
5047 vtn_get_nir_ssa(b
, w
[4]),
5049 def
= nir_idiv(&b
->nb
, def
, nir_imm_intN_t(&b
->nb
, elem_size
, def
->bit_size
));
5050 def
= nir_i2i(&b
->nb
, def
, glsl_get_bit_size(type
));
5055 case SpvOpPtrNotEqual
: {
5056 def
= nir_build_addr_ieq(&b
->nb
,
5057 vtn_get_nir_ssa(b
, w
[3]),
5058 vtn_get_nir_ssa(b
, w
[4]),
5060 if (opcode
== SpvOpPtrNotEqual
)
5061 def
= nir_inot(&b
->nb
, def
);
5066 unreachable("Invalid ptr operation");
5069 vtn_push_nir_ssa(b
, w
[2], def
);
5073 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
5074 const uint32_t *w
, unsigned count
)
5080 case SpvOpLoopMerge
:
5081 case SpvOpSelectionMerge
:
5082 /* This is handled by cfg pre-pass and walk_blocks */
5086 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
5087 val
->type
= vtn_get_type(b
, w
[1]);
5092 vtn_handle_extension(b
, opcode
, w
, count
);
5098 case SpvOpCopyMemory
:
5099 case SpvOpCopyMemorySized
:
5100 case SpvOpAccessChain
:
5101 case SpvOpPtrAccessChain
:
5102 case SpvOpInBoundsAccessChain
:
5103 case SpvOpInBoundsPtrAccessChain
:
5104 case SpvOpArrayLength
:
5105 case SpvOpConvertPtrToU
:
5106 case SpvOpConvertUToPtr
:
5107 vtn_handle_variables(b
, opcode
, w
, count
);
5110 case SpvOpFunctionCall
:
5111 vtn_handle_function_call(b
, opcode
, w
, count
);
5114 case SpvOpSampledImage
:
5116 case SpvOpImageSampleImplicitLod
:
5117 case SpvOpImageSampleExplicitLod
:
5118 case SpvOpImageSampleDrefImplicitLod
:
5119 case SpvOpImageSampleDrefExplicitLod
:
5120 case SpvOpImageSampleProjImplicitLod
:
5121 case SpvOpImageSampleProjExplicitLod
:
5122 case SpvOpImageSampleProjDrefImplicitLod
:
5123 case SpvOpImageSampleProjDrefExplicitLod
:
5124 case SpvOpImageFetch
:
5125 case SpvOpImageGather
:
5126 case SpvOpImageDrefGather
:
5127 case SpvOpImageQueryLod
:
5128 case SpvOpImageQueryLevels
:
5129 case SpvOpImageQuerySamples
:
5130 vtn_handle_texture(b
, opcode
, w
, count
);
5133 case SpvOpImageRead
:
5134 case SpvOpImageWrite
:
5135 case SpvOpImageTexelPointer
:
5136 case SpvOpImageQueryFormat
:
5137 case SpvOpImageQueryOrder
:
5138 vtn_handle_image(b
, opcode
, w
, count
);
5141 case SpvOpImageQuerySizeLod
:
5142 case SpvOpImageQuerySize
: {
5143 struct vtn_type
*image_type
= vtn_get_value_type(b
, w
[3]);
5144 vtn_assert(image_type
->base_type
== vtn_base_type_image
);
5145 if (glsl_type_is_image(image_type
->glsl_image
)) {
5146 vtn_handle_image(b
, opcode
, w
, count
);
5148 vtn_assert(glsl_type_is_sampler(image_type
->glsl_image
));
5149 vtn_handle_texture(b
, opcode
, w
, count
);
5154 case SpvOpFragmentMaskFetchAMD
:
5155 case SpvOpFragmentFetchAMD
:
5156 vtn_handle_texture(b
, opcode
, w
, count
);
5159 case SpvOpAtomicLoad
:
5160 case SpvOpAtomicExchange
:
5161 case SpvOpAtomicCompareExchange
:
5162 case SpvOpAtomicCompareExchangeWeak
:
5163 case SpvOpAtomicIIncrement
:
5164 case SpvOpAtomicIDecrement
:
5165 case SpvOpAtomicIAdd
:
5166 case SpvOpAtomicISub
:
5167 case SpvOpAtomicSMin
:
5168 case SpvOpAtomicUMin
:
5169 case SpvOpAtomicSMax
:
5170 case SpvOpAtomicUMax
:
5171 case SpvOpAtomicAnd
:
5173 case SpvOpAtomicXor
:
5174 case SpvOpAtomicFAddEXT
: {
5175 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
5176 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
5177 vtn_handle_image(b
, opcode
, w
, count
);
5179 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
5180 vtn_handle_atomics(b
, opcode
, w
, count
);
5185 case SpvOpAtomicStore
: {
5186 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
5187 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
5188 vtn_handle_image(b
, opcode
, w
, count
);
5190 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
5191 vtn_handle_atomics(b
, opcode
, w
, count
);
5197 vtn_handle_select(b
, opcode
, w
, count
);
5205 case SpvOpConvertFToU
:
5206 case SpvOpConvertFToS
:
5207 case SpvOpConvertSToF
:
5208 case SpvOpConvertUToF
:
5212 case SpvOpQuantizeToF16
:
5213 case SpvOpPtrCastToGeneric
:
5214 case SpvOpGenericCastToPtr
:
5219 case SpvOpSignBitSet
:
5220 case SpvOpLessOrGreater
:
5222 case SpvOpUnordered
:
5237 case SpvOpVectorTimesScalar
:
5239 case SpvOpIAddCarry
:
5240 case SpvOpISubBorrow
:
5241 case SpvOpUMulExtended
:
5242 case SpvOpSMulExtended
:
5243 case SpvOpShiftRightLogical
:
5244 case SpvOpShiftRightArithmetic
:
5245 case SpvOpShiftLeftLogical
:
5246 case SpvOpLogicalEqual
:
5247 case SpvOpLogicalNotEqual
:
5248 case SpvOpLogicalOr
:
5249 case SpvOpLogicalAnd
:
5250 case SpvOpLogicalNot
:
5251 case SpvOpBitwiseOr
:
5252 case SpvOpBitwiseXor
:
5253 case SpvOpBitwiseAnd
:
5255 case SpvOpFOrdEqual
:
5256 case SpvOpFUnordEqual
:
5257 case SpvOpINotEqual
:
5258 case SpvOpFOrdNotEqual
:
5259 case SpvOpFUnordNotEqual
:
5260 case SpvOpULessThan
:
5261 case SpvOpSLessThan
:
5262 case SpvOpFOrdLessThan
:
5263 case SpvOpFUnordLessThan
:
5264 case SpvOpUGreaterThan
:
5265 case SpvOpSGreaterThan
:
5266 case SpvOpFOrdGreaterThan
:
5267 case SpvOpFUnordGreaterThan
:
5268 case SpvOpULessThanEqual
:
5269 case SpvOpSLessThanEqual
:
5270 case SpvOpFOrdLessThanEqual
:
5271 case SpvOpFUnordLessThanEqual
:
5272 case SpvOpUGreaterThanEqual
:
5273 case SpvOpSGreaterThanEqual
:
5274 case SpvOpFOrdGreaterThanEqual
:
5275 case SpvOpFUnordGreaterThanEqual
:
5281 case SpvOpFwidthFine
:
5282 case SpvOpDPdxCoarse
:
5283 case SpvOpDPdyCoarse
:
5284 case SpvOpFwidthCoarse
:
5285 case SpvOpBitFieldInsert
:
5286 case SpvOpBitFieldSExtract
:
5287 case SpvOpBitFieldUExtract
:
5288 case SpvOpBitReverse
:
5290 case SpvOpTranspose
:
5291 case SpvOpOuterProduct
:
5292 case SpvOpMatrixTimesScalar
:
5293 case SpvOpVectorTimesMatrix
:
5294 case SpvOpMatrixTimesVector
:
5295 case SpvOpMatrixTimesMatrix
:
5296 case SpvOpUCountLeadingZerosINTEL
:
5297 case SpvOpUCountTrailingZerosINTEL
:
5298 case SpvOpAbsISubINTEL
:
5299 case SpvOpAbsUSubINTEL
:
5300 case SpvOpIAddSatINTEL
:
5301 case SpvOpUAddSatINTEL
:
5302 case SpvOpIAverageINTEL
:
5303 case SpvOpUAverageINTEL
:
5304 case SpvOpIAverageRoundedINTEL
:
5305 case SpvOpUAverageRoundedINTEL
:
5306 case SpvOpISubSatINTEL
:
5307 case SpvOpUSubSatINTEL
:
5308 case SpvOpIMul32x16INTEL
:
5309 case SpvOpUMul32x16INTEL
:
5310 vtn_handle_alu(b
, opcode
, w
, count
);
5314 vtn_handle_bitcast(b
, w
, count
);
5317 case SpvOpVectorExtractDynamic
:
5318 case SpvOpVectorInsertDynamic
:
5319 case SpvOpVectorShuffle
:
5320 case SpvOpCompositeConstruct
:
5321 case SpvOpCompositeExtract
:
5322 case SpvOpCompositeInsert
:
5323 case SpvOpCopyLogical
:
5324 case SpvOpCopyObject
:
5325 vtn_handle_composite(b
, opcode
, w
, count
);
5328 case SpvOpEmitVertex
:
5329 case SpvOpEndPrimitive
:
5330 case SpvOpEmitStreamVertex
:
5331 case SpvOpEndStreamPrimitive
:
5332 case SpvOpControlBarrier
:
5333 case SpvOpMemoryBarrier
:
5334 vtn_handle_barrier(b
, opcode
, w
, count
);
5337 case SpvOpGroupNonUniformElect
:
5338 case SpvOpGroupNonUniformAll
:
5339 case SpvOpGroupNonUniformAny
:
5340 case SpvOpGroupNonUniformAllEqual
:
5341 case SpvOpGroupNonUniformBroadcast
:
5342 case SpvOpGroupNonUniformBroadcastFirst
:
5343 case SpvOpGroupNonUniformBallot
:
5344 case SpvOpGroupNonUniformInverseBallot
:
5345 case SpvOpGroupNonUniformBallotBitExtract
:
5346 case SpvOpGroupNonUniformBallotBitCount
:
5347 case SpvOpGroupNonUniformBallotFindLSB
:
5348 case SpvOpGroupNonUniformBallotFindMSB
:
5349 case SpvOpGroupNonUniformShuffle
:
5350 case SpvOpGroupNonUniformShuffleXor
:
5351 case SpvOpGroupNonUniformShuffleUp
:
5352 case SpvOpGroupNonUniformShuffleDown
:
5353 case SpvOpGroupNonUniformIAdd
:
5354 case SpvOpGroupNonUniformFAdd
:
5355 case SpvOpGroupNonUniformIMul
:
5356 case SpvOpGroupNonUniformFMul
:
5357 case SpvOpGroupNonUniformSMin
:
5358 case SpvOpGroupNonUniformUMin
:
5359 case SpvOpGroupNonUniformFMin
:
5360 case SpvOpGroupNonUniformSMax
:
5361 case SpvOpGroupNonUniformUMax
:
5362 case SpvOpGroupNonUniformFMax
:
5363 case SpvOpGroupNonUniformBitwiseAnd
:
5364 case SpvOpGroupNonUniformBitwiseOr
:
5365 case SpvOpGroupNonUniformBitwiseXor
:
5366 case SpvOpGroupNonUniformLogicalAnd
:
5367 case SpvOpGroupNonUniformLogicalOr
:
5368 case SpvOpGroupNonUniformLogicalXor
:
5369 case SpvOpGroupNonUniformQuadBroadcast
:
5370 case SpvOpGroupNonUniformQuadSwap
:
5373 case SpvOpGroupBroadcast
:
5374 case SpvOpGroupIAdd
:
5375 case SpvOpGroupFAdd
:
5376 case SpvOpGroupFMin
:
5377 case SpvOpGroupUMin
:
5378 case SpvOpGroupSMin
:
5379 case SpvOpGroupFMax
:
5380 case SpvOpGroupUMax
:
5381 case SpvOpGroupSMax
:
5382 case SpvOpSubgroupBallotKHR
:
5383 case SpvOpSubgroupFirstInvocationKHR
:
5384 case SpvOpSubgroupReadInvocationKHR
:
5385 case SpvOpSubgroupAllKHR
:
5386 case SpvOpSubgroupAnyKHR
:
5387 case SpvOpSubgroupAllEqualKHR
:
5388 case SpvOpGroupIAddNonUniformAMD
:
5389 case SpvOpGroupFAddNonUniformAMD
:
5390 case SpvOpGroupFMinNonUniformAMD
:
5391 case SpvOpGroupUMinNonUniformAMD
:
5392 case SpvOpGroupSMinNonUniformAMD
:
5393 case SpvOpGroupFMaxNonUniformAMD
:
5394 case SpvOpGroupUMaxNonUniformAMD
:
5395 case SpvOpGroupSMaxNonUniformAMD
:
5396 vtn_handle_subgroup(b
, opcode
, w
, count
);
5401 case SpvOpPtrNotEqual
:
5402 vtn_handle_ptr(b
, opcode
, w
, count
);
5405 case SpvOpBeginInvocationInterlockEXT
:
5406 vtn_emit_barrier(b
, nir_intrinsic_begin_invocation_interlock
);
5409 case SpvOpEndInvocationInterlockEXT
:
5410 vtn_emit_barrier(b
, nir_intrinsic_end_invocation_interlock
);
5413 case SpvOpDemoteToHelperInvocationEXT
: {
5414 nir_intrinsic_instr
*intrin
=
5415 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_demote
);
5416 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5420 case SpvOpIsHelperInvocationEXT
: {
5421 nir_intrinsic_instr
*intrin
=
5422 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_is_helper_invocation
);
5423 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 1, 1, NULL
);
5424 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5426 vtn_push_nir_ssa(b
, w
[2], &intrin
->dest
.ssa
);
5430 case SpvOpReadClockKHR
: {
5431 SpvScope scope
= vtn_constant_uint(b
, w
[3]);
5432 nir_scope nir_scope
;
5435 case SpvScopeDevice
:
5436 nir_scope
= NIR_SCOPE_DEVICE
;
5438 case SpvScopeSubgroup
:
5439 nir_scope
= NIR_SCOPE_SUBGROUP
;
5442 vtn_fail("invalid read clock scope");
5445 /* Operation supports two result types: uvec2 and uint64_t. The NIR
5446 * intrinsic gives uvec2, so pack the result for the other case.
5448 nir_intrinsic_instr
*intrin
=
5449 nir_intrinsic_instr_create(b
->nb
.shader
, nir_intrinsic_shader_clock
);
5450 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 2, 32, NULL
);
5451 nir_intrinsic_set_memory_scope(intrin
, nir_scope
);
5452 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5454 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
5455 const struct glsl_type
*dest_type
= type
->type
;
5456 nir_ssa_def
*result
;
5458 if (glsl_type_is_vector(dest_type
)) {
5459 assert(dest_type
== glsl_vector_type(GLSL_TYPE_UINT
, 2));
5460 result
= &intrin
->dest
.ssa
;
5462 assert(glsl_type_is_scalar(dest_type
));
5463 assert(glsl_get_base_type(dest_type
) == GLSL_TYPE_UINT64
);
5464 result
= nir_pack_64_2x32(&b
->nb
, &intrin
->dest
.ssa
);
5467 vtn_push_nir_ssa(b
, w
[2], result
);
5471 case SpvOpLifetimeStart
:
5472 case SpvOpLifetimeStop
:
5476 vtn_fail_with_opcode("Unhandled opcode", opcode
);
5483 vtn_create_builder(const uint32_t *words
, size_t word_count
,
5484 gl_shader_stage stage
, const char *entry_point_name
,
5485 const struct spirv_to_nir_options
*options
)
5487 /* Initialize the vtn_builder object */
5488 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
5489 struct spirv_to_nir_options
*dup_options
=
5490 ralloc(b
, struct spirv_to_nir_options
);
5491 *dup_options
= *options
;
5494 b
->spirv_word_count
= word_count
;
5498 list_inithead(&b
->functions
);
5499 b
->entry_point_stage
= stage
;
5500 b
->entry_point_name
= entry_point_name
;
5501 b
->options
= dup_options
;
5504 * Handle the SPIR-V header (first 5 dwords).
5505 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
5507 if (word_count
<= 5)
5510 if (words
[0] != SpvMagicNumber
) {
5511 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
5514 if (words
[1] < 0x10000) {
5515 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
5519 uint16_t generator_id
= words
[2] >> 16;
5520 uint16_t generator_version
= words
[2];
5522 /* In GLSLang commit 8297936dd6eb3, their handling of barrier() was fixed
5523 * to provide correct memory semantics on compute shader barrier()
5524 * commands. Prior to that, we need to fix them up ourselves. This
5525 * GLSLang fix caused them to bump to generator version 3.
5527 b
->wa_glslang_cs_barrier
= (generator_id
== 8 && generator_version
< 3);
5529 /* words[2] == generator magic */
5530 unsigned value_id_bound
= words
[3];
5531 if (words
[4] != 0) {
5532 vtn_err("words[4] was %u, want 0", words
[4]);
5536 b
->value_id_bound
= value_id_bound
;
5537 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
5545 static nir_function
*
5546 vtn_emit_kernel_entry_point_wrapper(struct vtn_builder
*b
,
5547 nir_function
*entry_point
)
5549 vtn_assert(entry_point
== b
->entry_point
->func
->impl
->function
);
5550 vtn_fail_if(!entry_point
->name
, "entry points are required to have a name");
5551 const char *func_name
=
5552 ralloc_asprintf(b
->shader
, "__wrapped_%s", entry_point
->name
);
5554 /* we shouldn't have any inputs yet */
5555 vtn_assert(!entry_point
->shader
->num_inputs
);
5556 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
);
5558 nir_function
*main_entry_point
= nir_function_create(b
->shader
, func_name
);
5559 main_entry_point
->impl
= nir_function_impl_create(main_entry_point
);
5560 nir_builder_init(&b
->nb
, main_entry_point
->impl
);
5561 b
->nb
.cursor
= nir_after_cf_list(&main_entry_point
->impl
->body
);
5562 b
->func_param_idx
= 0;
5564 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, entry_point
);
5566 for (unsigned i
= 0; i
< entry_point
->num_params
; ++i
) {
5567 struct vtn_type
*param_type
= b
->entry_point
->func
->type
->params
[i
];
5569 /* consider all pointers to function memory to be parameters passed
5572 bool is_by_val
= param_type
->base_type
== vtn_base_type_pointer
&&
5573 param_type
->storage_class
== SpvStorageClassFunction
;
5575 /* input variable */
5576 nir_variable
*in_var
= rzalloc(b
->nb
.shader
, nir_variable
);
5577 in_var
->data
.mode
= nir_var_uniform
;
5578 in_var
->data
.read_only
= true;
5579 in_var
->data
.location
= i
;
5580 if (param_type
->base_type
== vtn_base_type_image
) {
5581 in_var
->data
.access
= 0;
5582 if (param_type
->access_qualifier
& SpvAccessQualifierReadOnly
)
5583 in_var
->data
.access
|= ACCESS_NON_WRITEABLE
;
5584 if (param_type
->access_qualifier
& SpvAccessQualifierWriteOnly
)
5585 in_var
->data
.access
|= ACCESS_NON_READABLE
;
5589 in_var
->type
= param_type
->deref
->type
;
5590 else if (param_type
->base_type
== vtn_base_type_image
)
5591 in_var
->type
= param_type
->glsl_image
;
5592 else if (param_type
->base_type
== vtn_base_type_sampler
)
5593 in_var
->type
= glsl_bare_sampler_type();
5595 in_var
->type
= param_type
->type
;
5597 nir_shader_add_variable(b
->nb
.shader
, in_var
);
5598 b
->nb
.shader
->num_inputs
++;
5600 /* we have to copy the entire variable into function memory */
5602 nir_variable
*copy_var
=
5603 nir_local_variable_create(main_entry_point
->impl
, in_var
->type
,
5605 nir_copy_var(&b
->nb
, copy_var
, in_var
);
5607 nir_src_for_ssa(&nir_build_deref_var(&b
->nb
, copy_var
)->dest
.ssa
);
5608 } else if (param_type
->base_type
== vtn_base_type_image
||
5609 param_type
->base_type
== vtn_base_type_sampler
) {
5610 /* Don't load the var, just pass a deref of it */
5611 call
->params
[i
] = nir_src_for_ssa(&nir_build_deref_var(&b
->nb
, in_var
)->dest
.ssa
);
5613 call
->params
[i
] = nir_src_for_ssa(nir_load_var(&b
->nb
, in_var
));
5617 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
5619 return main_entry_point
;
5623 spirv_to_nir(const uint32_t *words
, size_t word_count
,
5624 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
5625 gl_shader_stage stage
, const char *entry_point_name
,
5626 const struct spirv_to_nir_options
*options
,
5627 const nir_shader_compiler_options
*nir_options
)
5630 const uint32_t *word_end
= words
+ word_count
;
5632 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
5633 stage
, entry_point_name
,
5639 /* See also _vtn_fail() */
5640 if (setjmp(b
->fail_jump
)) {
5645 /* Skip the SPIR-V header, handled at vtn_create_builder */
5648 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
5650 /* Handle all the preamble instructions */
5651 words
= vtn_foreach_instruction(b
, words
, word_end
,
5652 vtn_handle_preamble_instruction
);
5654 if (b
->entry_point
== NULL
) {
5655 vtn_fail("Entry point not found");
5660 /* Ensure a sane address mode is being used for function temps */
5661 assert(nir_address_format_bit_size(b
->options
->temp_addr_format
) == nir_get_ptr_bitsize(b
->shader
));
5662 assert(nir_address_format_num_components(b
->options
->temp_addr_format
) == 1);
5664 /* Set shader info defaults */
5665 if (stage
== MESA_SHADER_GEOMETRY
)
5666 b
->shader
->info
.gs
.invocations
= 1;
5668 /* Parse execution modes. */
5669 vtn_foreach_execution_mode(b
, b
->entry_point
,
5670 vtn_handle_execution_mode
, NULL
);
5672 b
->specializations
= spec
;
5673 b
->num_specializations
= num_spec
;
5675 /* Handle all variable, type, and constant instructions */
5676 words
= vtn_foreach_instruction(b
, words
, word_end
,
5677 vtn_handle_variable_or_type_instruction
);
5679 /* Parse execution modes that depend on IDs. Must happen after we have
5682 vtn_foreach_execution_mode(b
, b
->entry_point
,
5683 vtn_handle_execution_mode_id
, NULL
);
5685 if (b
->workgroup_size_builtin
) {
5686 vtn_assert(b
->workgroup_size_builtin
->type
->type
==
5687 glsl_vector_type(GLSL_TYPE_UINT
, 3));
5689 nir_const_value
*const_size
=
5690 b
->workgroup_size_builtin
->constant
->values
;
5692 b
->shader
->info
.cs
.local_size
[0] = const_size
[0].u32
;
5693 b
->shader
->info
.cs
.local_size
[1] = const_size
[1].u32
;
5694 b
->shader
->info
.cs
.local_size
[2] = const_size
[2].u32
;
5697 /* Set types on all vtn_values */
5698 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
5700 vtn_build_cfg(b
, words
, word_end
);
5702 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5703 b
->entry_point
->func
->referenced
= true;
5708 vtn_foreach_cf_node(node
, &b
->functions
) {
5709 struct vtn_function
*func
= vtn_cf_node_as_function(node
);
5710 if (func
->referenced
&& !func
->emitted
) {
5711 b
->const_table
= _mesa_pointer_hash_table_create(b
);
5713 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
5719 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5720 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
5721 vtn_assert(entry_point
);
5723 /* post process entry_points with input params */
5724 if (entry_point
->num_params
&& b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
5725 entry_point
= vtn_emit_kernel_entry_point_wrapper(b
, entry_point
);
5727 /* structurize the CFG */
5728 nir_lower_goto_ifs(b
->shader
);
5730 entry_point
->is_entrypoint
= true;
5732 /* When multiple shader stages exist in the same SPIR-V module, we
5733 * generate input and output variables for every stage, in the same
5734 * NIR program. These dead variables can be invalid NIR. For example,
5735 * TCS outputs must be per-vertex arrays (or decorated 'patch'), while
5736 * VS output variables wouldn't be.
5738 * To ensure we have valid NIR, we eliminate any dead inputs and outputs
5739 * right away. In order to do so, we must lower any constant initializers
5740 * on outputs so nir_remove_dead_variables sees that they're written to.
5742 nir_lower_variable_initializers(b
->shader
, nir_var_shader_out
);
5743 nir_remove_dead_variables(b
->shader
,
5744 nir_var_shader_in
| nir_var_shader_out
, NULL
);
5746 /* We sometimes generate bogus derefs that, while never used, give the
5747 * validator a bit of heartburn. Run dead code to get rid of them.
5749 nir_opt_dce(b
->shader
);
5751 /* Unparent the shader from the vtn_builder before we delete the builder */
5752 ralloc_steal(NULL
, b
->shader
);
5754 nir_shader
*shader
= b
->shader
;