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
)
2893 case SpvOpAtomicIIncrement
:
2894 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
2897 case SpvOpAtomicIDecrement
:
2898 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
2901 case SpvOpAtomicISub
:
2903 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_get_nir_ssa(b
, w
[6])));
2906 case SpvOpAtomicCompareExchange
:
2907 case SpvOpAtomicCompareExchangeWeak
:
2908 src
[0] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[8]));
2909 src
[1] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[7]));
2912 case SpvOpAtomicExchange
:
2913 case SpvOpAtomicIAdd
:
2914 case SpvOpAtomicSMin
:
2915 case SpvOpAtomicUMin
:
2916 case SpvOpAtomicSMax
:
2917 case SpvOpAtomicUMax
:
2918 case SpvOpAtomicAnd
:
2920 case SpvOpAtomicXor
:
2921 case SpvOpAtomicFAddEXT
:
2922 src
[0] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[6]));
2926 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2930 static nir_ssa_def
*
2931 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2933 nir_ssa_def
*coord
= vtn_get_nir_ssa(b
, value
);
2935 /* The image_load_store intrinsics assume a 4-dim coordinate */
2936 unsigned swizzle
[4];
2937 for (unsigned i
= 0; i
< 4; i
++)
2938 swizzle
[i
] = MIN2(i
, coord
->num_components
- 1);
2940 return nir_swizzle(&b
->nb
, coord
, swizzle
, 4);
2943 static nir_ssa_def
*
2944 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2946 if (value
->num_components
== 4)
2950 for (unsigned i
= 0; i
< 4; i
++)
2951 swiz
[i
] = i
< value
->num_components
? i
: 0;
2952 return nir_swizzle(b
, value
, swiz
, 4);
2956 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2957 const uint32_t *w
, unsigned count
)
2959 /* Just get this one out of the way */
2960 if (opcode
== SpvOpImageTexelPointer
) {
2961 struct vtn_value
*val
=
2962 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2963 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2965 val
->image
->image
= vtn_nir_deref(b
, w
[3]);
2966 val
->image
->coord
= get_image_coord(b
, w
[4]);
2967 val
->image
->sample
= vtn_get_nir_ssa(b
, w
[5]);
2968 val
->image
->lod
= nir_imm_int(&b
->nb
, 0);
2972 struct vtn_image_pointer image
;
2973 SpvScope scope
= SpvScopeInvocation
;
2974 SpvMemorySemanticsMask semantics
= 0;
2976 enum gl_access_qualifier access
= 0;
2978 struct vtn_value
*res_val
;
2980 case SpvOpAtomicExchange
:
2981 case SpvOpAtomicCompareExchange
:
2982 case SpvOpAtomicCompareExchangeWeak
:
2983 case SpvOpAtomicIIncrement
:
2984 case SpvOpAtomicIDecrement
:
2985 case SpvOpAtomicIAdd
:
2986 case SpvOpAtomicISub
:
2987 case SpvOpAtomicLoad
:
2988 case SpvOpAtomicSMin
:
2989 case SpvOpAtomicUMin
:
2990 case SpvOpAtomicSMax
:
2991 case SpvOpAtomicUMax
:
2992 case SpvOpAtomicAnd
:
2994 case SpvOpAtomicXor
:
2995 case SpvOpAtomicFAddEXT
:
2996 res_val
= vtn_value(b
, w
[3], vtn_value_type_image_pointer
);
2997 image
= *res_val
->image
;
2998 scope
= vtn_constant_uint(b
, w
[4]);
2999 semantics
= vtn_constant_uint(b
, w
[5]);
3000 access
|= ACCESS_COHERENT
;
3003 case SpvOpAtomicStore
:
3004 res_val
= vtn_value(b
, w
[1], vtn_value_type_image_pointer
);
3005 image
= *res_val
->image
;
3006 scope
= vtn_constant_uint(b
, w
[2]);
3007 semantics
= vtn_constant_uint(b
, w
[3]);
3008 access
|= ACCESS_COHERENT
;
3011 case SpvOpImageQuerySizeLod
:
3012 res_val
= vtn_untyped_value(b
, w
[3]);
3013 image
.image
= vtn_get_image(b
, w
[3]);
3015 image
.sample
= NULL
;
3016 image
.lod
= vtn_ssa_value(b
, w
[4])->def
;
3019 case SpvOpImageQuerySize
:
3020 res_val
= vtn_untyped_value(b
, w
[3]);
3021 image
.image
= vtn_get_image(b
, w
[3]);
3023 image
.sample
= NULL
;
3027 case SpvOpImageQueryFormat
:
3028 case SpvOpImageQueryOrder
:
3029 res_val
= vtn_untyped_value(b
, w
[3]);
3030 image
.image
= vtn_get_image(b
, w
[3]);
3032 image
.sample
= NULL
;
3036 case SpvOpImageRead
: {
3037 res_val
= vtn_untyped_value(b
, w
[3]);
3038 image
.image
= vtn_get_image(b
, w
[3]);
3039 image
.coord
= get_image_coord(b
, w
[4]);
3041 const SpvImageOperandsMask operands
=
3042 count
> 5 ? w
[5] : SpvImageOperandsMaskNone
;
3044 if (operands
& SpvImageOperandsSampleMask
) {
3045 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
3046 SpvImageOperandsSampleMask
);
3047 image
.sample
= vtn_get_nir_ssa(b
, w
[arg
]);
3049 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
3052 if (operands
& SpvImageOperandsMakeTexelVisibleMask
) {
3053 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
3054 "MakeTexelVisible requires NonPrivateTexel to also be set.");
3055 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
3056 SpvImageOperandsMakeTexelVisibleMask
);
3057 semantics
= SpvMemorySemanticsMakeVisibleMask
;
3058 scope
= vtn_constant_uint(b
, w
[arg
]);
3061 if (operands
& SpvImageOperandsLodMask
) {
3062 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
3063 SpvImageOperandsLodMask
);
3064 image
.lod
= vtn_get_nir_ssa(b
, w
[arg
]);
3066 image
.lod
= nir_imm_int(&b
->nb
, 0);
3069 if (operands
& SpvImageOperandsVolatileTexelMask
)
3070 access
|= ACCESS_VOLATILE
;
3075 case SpvOpImageWrite
: {
3076 res_val
= vtn_untyped_value(b
, w
[1]);
3077 image
.image
= vtn_get_image(b
, w
[1]);
3078 image
.coord
= get_image_coord(b
, w
[2]);
3082 const SpvImageOperandsMask operands
=
3083 count
> 4 ? w
[4] : SpvImageOperandsMaskNone
;
3085 if (operands
& SpvImageOperandsSampleMask
) {
3086 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
3087 SpvImageOperandsSampleMask
);
3088 image
.sample
= vtn_get_nir_ssa(b
, w
[arg
]);
3090 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
3093 if (operands
& SpvImageOperandsMakeTexelAvailableMask
) {
3094 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
3095 "MakeTexelAvailable requires NonPrivateTexel to also be set.");
3096 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
3097 SpvImageOperandsMakeTexelAvailableMask
);
3098 semantics
= SpvMemorySemanticsMakeAvailableMask
;
3099 scope
= vtn_constant_uint(b
, w
[arg
]);
3102 if (operands
& SpvImageOperandsLodMask
) {
3103 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
3104 SpvImageOperandsLodMask
);
3105 image
.lod
= vtn_get_nir_ssa(b
, w
[arg
]);
3107 image
.lod
= nir_imm_int(&b
->nb
, 0);
3110 if (operands
& SpvImageOperandsVolatileTexelMask
)
3111 access
|= ACCESS_VOLATILE
;
3117 vtn_fail_with_opcode("Invalid image opcode", opcode
);
3120 if (semantics
& SpvMemorySemanticsVolatileMask
)
3121 access
|= ACCESS_VOLATILE
;
3123 nir_intrinsic_op op
;
3125 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
3126 OP(ImageQuerySize
, size
)
3127 OP(ImageQuerySizeLod
, size
)
3129 OP(ImageWrite
, store
)
3130 OP(AtomicLoad
, load
)
3131 OP(AtomicStore
, store
)
3132 OP(AtomicExchange
, atomic_exchange
)
3133 OP(AtomicCompareExchange
, atomic_comp_swap
)
3134 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3135 OP(AtomicIIncrement
, atomic_add
)
3136 OP(AtomicIDecrement
, atomic_add
)
3137 OP(AtomicIAdd
, atomic_add
)
3138 OP(AtomicISub
, atomic_add
)
3139 OP(AtomicSMin
, atomic_imin
)
3140 OP(AtomicUMin
, atomic_umin
)
3141 OP(AtomicSMax
, atomic_imax
)
3142 OP(AtomicUMax
, atomic_umax
)
3143 OP(AtomicAnd
, atomic_and
)
3144 OP(AtomicOr
, atomic_or
)
3145 OP(AtomicXor
, atomic_xor
)
3146 OP(AtomicFAddEXT
, atomic_fadd
)
3147 OP(ImageQueryFormat
, format
)
3148 OP(ImageQueryOrder
, order
)
3151 vtn_fail_with_opcode("Invalid image opcode", opcode
);
3154 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3156 intrin
->src
[0] = nir_src_for_ssa(&image
.image
->dest
.ssa
);
3159 case SpvOpImageQuerySize
:
3160 case SpvOpImageQuerySizeLod
:
3161 case SpvOpImageQueryFormat
:
3162 case SpvOpImageQueryOrder
:
3165 /* The image coordinate is always 4 components but we may not have that
3166 * many. Swizzle to compensate.
3168 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
3169 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
3173 /* The Vulkan spec says:
3175 * "If an instruction loads from or stores to a resource (including
3176 * atomics and image instructions) and the resource descriptor being
3177 * accessed is not dynamically uniform, then the operand corresponding
3178 * to that resource (e.g. the pointer or sampled image operand) must be
3179 * decorated with NonUniform."
3181 * It's very careful to specify that the exact operand must be decorated
3182 * NonUniform. The SPIR-V parser is not expected to chase through long
3183 * chains to find the NonUniform decoration. It's either right there or we
3184 * can assume it doesn't exist.
3186 vtn_foreach_decoration(b
, res_val
, non_uniform_decoration_cb
, &access
);
3187 nir_intrinsic_set_access(intrin
, access
);
3190 case SpvOpImageQueryFormat
:
3191 case SpvOpImageQueryOrder
:
3192 /* No additional sources */
3194 case SpvOpImageQuerySize
:
3195 intrin
->src
[1] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 0));
3197 case SpvOpImageQuerySizeLod
:
3198 intrin
->src
[1] = nir_src_for_ssa(image
.lod
);
3200 case SpvOpAtomicLoad
:
3201 case SpvOpImageRead
:
3202 /* Only OpImageRead can support a lod parameter if
3203 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
3204 * intrinsics definition for atomics requires us to set it for
3207 intrin
->src
[3] = nir_src_for_ssa(image
.lod
);
3209 case SpvOpAtomicStore
:
3210 case SpvOpImageWrite
: {
3211 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
3212 struct vtn_ssa_value
*value
= vtn_ssa_value(b
, value_id
);
3213 /* nir_intrinsic_image_deref_store always takes a vec4 value */
3214 assert(op
== nir_intrinsic_image_deref_store
);
3215 intrin
->num_components
= 4;
3216 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
->def
));
3217 /* Only OpImageWrite can support a lod parameter if
3218 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
3219 * intrinsics definition for atomics requires us to set it for
3222 intrin
->src
[4] = nir_src_for_ssa(image
.lod
);
3224 if (opcode
== SpvOpImageWrite
)
3225 nir_intrinsic_set_type(intrin
, nir_get_nir_type_for_glsl_type(value
->type
));
3229 case SpvOpAtomicCompareExchange
:
3230 case SpvOpAtomicCompareExchangeWeak
:
3231 case SpvOpAtomicIIncrement
:
3232 case SpvOpAtomicIDecrement
:
3233 case SpvOpAtomicExchange
:
3234 case SpvOpAtomicIAdd
:
3235 case SpvOpAtomicISub
:
3236 case SpvOpAtomicSMin
:
3237 case SpvOpAtomicUMin
:
3238 case SpvOpAtomicSMax
:
3239 case SpvOpAtomicUMax
:
3240 case SpvOpAtomicAnd
:
3242 case SpvOpAtomicXor
:
3243 case SpvOpAtomicFAddEXT
:
3244 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
3248 vtn_fail_with_opcode("Invalid image opcode", opcode
);
3251 /* Image operations implicitly have the Image storage memory semantics. */
3252 semantics
|= SpvMemorySemanticsImageMemoryMask
;
3254 SpvMemorySemanticsMask before_semantics
;
3255 SpvMemorySemanticsMask after_semantics
;
3256 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
3258 if (before_semantics
)
3259 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
3261 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
3262 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
3264 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
3265 if (nir_intrinsic_infos
[op
].dest_components
== 0)
3266 intrin
->num_components
= dest_components
;
3268 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
3269 nir_intrinsic_dest_components(intrin
), 32, NULL
);
3271 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3273 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
3274 if (nir_intrinsic_dest_components(intrin
) != dest_components
)
3275 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
3277 vtn_push_nir_ssa(b
, w
[2], result
);
3279 if (opcode
== SpvOpImageRead
)
3280 nir_intrinsic_set_type(intrin
, nir_get_nir_type_for_glsl_type(type
->type
));
3282 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3285 if (after_semantics
)
3286 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
3289 static nir_intrinsic_op
3290 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3293 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
3294 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
3295 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
3296 OP(AtomicExchange
, atomic_exchange
)
3297 OP(AtomicCompareExchange
, atomic_comp_swap
)
3298 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3299 OP(AtomicIIncrement
, atomic_add
)
3300 OP(AtomicIDecrement
, atomic_add
)
3301 OP(AtomicIAdd
, atomic_add
)
3302 OP(AtomicISub
, atomic_add
)
3303 OP(AtomicSMin
, atomic_imin
)
3304 OP(AtomicUMin
, atomic_umin
)
3305 OP(AtomicSMax
, atomic_imax
)
3306 OP(AtomicUMax
, atomic_umax
)
3307 OP(AtomicAnd
, atomic_and
)
3308 OP(AtomicOr
, atomic_or
)
3309 OP(AtomicXor
, atomic_xor
)
3310 OP(AtomicFAddEXT
, atomic_fadd
)
3313 vtn_fail_with_opcode("Invalid SSBO atomic", opcode
);
3317 static nir_intrinsic_op
3318 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3321 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
3322 OP(AtomicLoad
, read_deref
)
3323 OP(AtomicExchange
, exchange
)
3324 OP(AtomicCompareExchange
, comp_swap
)
3325 OP(AtomicCompareExchangeWeak
, comp_swap
)
3326 OP(AtomicIIncrement
, inc_deref
)
3327 OP(AtomicIDecrement
, post_dec_deref
)
3328 OP(AtomicIAdd
, add_deref
)
3329 OP(AtomicISub
, add_deref
)
3330 OP(AtomicUMin
, min_deref
)
3331 OP(AtomicUMax
, max_deref
)
3332 OP(AtomicAnd
, and_deref
)
3333 OP(AtomicOr
, or_deref
)
3334 OP(AtomicXor
, xor_deref
)
3337 /* We left the following out: AtomicStore, AtomicSMin and
3338 * AtomicSmax. Right now there are not nir intrinsics for them. At this
3339 * moment Atomic Counter support is needed for ARB_spirv support, so is
3340 * only need to support GLSL Atomic Counters that are uints and don't
3341 * allow direct storage.
3343 vtn_fail("Invalid uniform atomic");
3347 static nir_intrinsic_op
3348 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3351 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
3352 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
3353 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
3354 OP(AtomicExchange
, atomic_exchange
)
3355 OP(AtomicCompareExchange
, atomic_comp_swap
)
3356 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3357 OP(AtomicIIncrement
, atomic_add
)
3358 OP(AtomicIDecrement
, atomic_add
)
3359 OP(AtomicIAdd
, atomic_add
)
3360 OP(AtomicISub
, atomic_add
)
3361 OP(AtomicSMin
, atomic_imin
)
3362 OP(AtomicUMin
, atomic_umin
)
3363 OP(AtomicSMax
, atomic_imax
)
3364 OP(AtomicUMax
, atomic_umax
)
3365 OP(AtomicAnd
, atomic_and
)
3366 OP(AtomicOr
, atomic_or
)
3367 OP(AtomicXor
, atomic_xor
)
3368 OP(AtomicFAddEXT
, atomic_fadd
)
3371 vtn_fail_with_opcode("Invalid shared atomic", opcode
);
3376 * Handles shared atomics, ssbo atomics and atomic counters.
3379 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
3380 const uint32_t *w
, UNUSED
unsigned count
)
3382 struct vtn_pointer
*ptr
;
3383 nir_intrinsic_instr
*atomic
;
3385 SpvScope scope
= SpvScopeInvocation
;
3386 SpvMemorySemanticsMask semantics
= 0;
3387 enum gl_access_qualifier access
= 0;
3390 case SpvOpAtomicLoad
:
3391 case SpvOpAtomicExchange
:
3392 case SpvOpAtomicCompareExchange
:
3393 case SpvOpAtomicCompareExchangeWeak
:
3394 case SpvOpAtomicIIncrement
:
3395 case SpvOpAtomicIDecrement
:
3396 case SpvOpAtomicIAdd
:
3397 case SpvOpAtomicISub
:
3398 case SpvOpAtomicSMin
:
3399 case SpvOpAtomicUMin
:
3400 case SpvOpAtomicSMax
:
3401 case SpvOpAtomicUMax
:
3402 case SpvOpAtomicAnd
:
3404 case SpvOpAtomicXor
:
3405 case SpvOpAtomicFAddEXT
:
3406 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
3407 scope
= vtn_constant_uint(b
, w
[4]);
3408 semantics
= vtn_constant_uint(b
, w
[5]);
3411 case SpvOpAtomicStore
:
3412 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
3413 scope
= vtn_constant_uint(b
, w
[2]);
3414 semantics
= vtn_constant_uint(b
, w
[3]);
3418 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3421 if (semantics
& SpvMemorySemanticsVolatileMask
)
3422 access
|= ACCESS_VOLATILE
;
3424 /* uniform as "atomic counter uniform" */
3425 if (ptr
->mode
== vtn_variable_mode_atomic_counter
) {
3426 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3427 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
3428 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3429 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3431 /* SSBO needs to initialize index/offset. In this case we don't need to,
3432 * as that info is already stored on the ptr->var->var nir_variable (see
3433 * vtn_create_variable)
3437 case SpvOpAtomicLoad
:
3438 case SpvOpAtomicExchange
:
3439 case SpvOpAtomicCompareExchange
:
3440 case SpvOpAtomicCompareExchangeWeak
:
3441 case SpvOpAtomicIIncrement
:
3442 case SpvOpAtomicIDecrement
:
3443 case SpvOpAtomicIAdd
:
3444 case SpvOpAtomicISub
:
3445 case SpvOpAtomicSMin
:
3446 case SpvOpAtomicUMin
:
3447 case SpvOpAtomicSMax
:
3448 case SpvOpAtomicUMax
:
3449 case SpvOpAtomicAnd
:
3451 case SpvOpAtomicXor
:
3452 /* Nothing: we don't need to call fill_common_atomic_sources here, as
3453 * atomic counter uniforms doesn't have sources
3458 unreachable("Invalid SPIR-V atomic");
3461 } else if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
3462 nir_ssa_def
*offset
, *index
;
3463 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
3465 assert(ptr
->mode
== vtn_variable_mode_ssbo
);
3467 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(b
, opcode
);
3468 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3470 nir_intrinsic_set_access(atomic
, access
| ACCESS_COHERENT
);
3474 case SpvOpAtomicLoad
:
3475 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3476 nir_intrinsic_set_align(atomic
, 4, 0);
3477 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3478 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3479 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3482 case SpvOpAtomicStore
:
3483 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3484 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3485 nir_intrinsic_set_align(atomic
, 4, 0);
3486 atomic
->src
[src
++] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[4]));
3487 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3488 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3489 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3492 case SpvOpAtomicExchange
:
3493 case SpvOpAtomicCompareExchange
:
3494 case SpvOpAtomicCompareExchangeWeak
:
3495 case SpvOpAtomicIIncrement
:
3496 case SpvOpAtomicIDecrement
:
3497 case SpvOpAtomicIAdd
:
3498 case SpvOpAtomicISub
:
3499 case SpvOpAtomicSMin
:
3500 case SpvOpAtomicUMin
:
3501 case SpvOpAtomicSMax
:
3502 case SpvOpAtomicUMax
:
3503 case SpvOpAtomicAnd
:
3505 case SpvOpAtomicXor
:
3506 case SpvOpAtomicFAddEXT
:
3507 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3508 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3509 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3510 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
3514 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3517 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3518 const struct glsl_type
*deref_type
= deref
->type
;
3519 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
3520 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3521 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3523 if (ptr
->mode
!= vtn_variable_mode_workgroup
)
3524 access
|= ACCESS_COHERENT
;
3526 nir_intrinsic_set_access(atomic
, access
);
3529 case SpvOpAtomicLoad
:
3530 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3533 case SpvOpAtomicStore
:
3534 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3535 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3536 atomic
->src
[1] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[4]));
3539 case SpvOpAtomicExchange
:
3540 case SpvOpAtomicCompareExchange
:
3541 case SpvOpAtomicCompareExchangeWeak
:
3542 case SpvOpAtomicIIncrement
:
3543 case SpvOpAtomicIDecrement
:
3544 case SpvOpAtomicIAdd
:
3545 case SpvOpAtomicISub
:
3546 case SpvOpAtomicSMin
:
3547 case SpvOpAtomicUMin
:
3548 case SpvOpAtomicSMax
:
3549 case SpvOpAtomicUMax
:
3550 case SpvOpAtomicAnd
:
3552 case SpvOpAtomicXor
:
3553 case SpvOpAtomicFAddEXT
:
3554 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
3558 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3562 /* Atomic ordering operations will implicitly apply to the atomic operation
3563 * storage class, so include that too.
3565 semantics
|= vtn_mode_to_memory_semantics(ptr
->mode
);
3567 SpvMemorySemanticsMask before_semantics
;
3568 SpvMemorySemanticsMask after_semantics
;
3569 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
3571 if (before_semantics
)
3572 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
3574 if (opcode
!= SpvOpAtomicStore
) {
3575 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
3577 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
3578 glsl_get_vector_elements(type
->type
),
3579 glsl_get_bit_size(type
->type
), NULL
);
3581 vtn_push_nir_ssa(b
, w
[2], &atomic
->dest
.ssa
);
3584 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
3586 if (after_semantics
)
3587 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
3590 static nir_alu_instr
*
3591 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
3593 nir_op op
= nir_op_vec(num_components
);
3594 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
3595 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
3597 vec
->dest
.write_mask
= (1 << num_components
) - 1;
3602 struct vtn_ssa_value
*
3603 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
3605 if (src
->transposed
)
3606 return src
->transposed
;
3608 struct vtn_ssa_value
*dest
=
3609 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
3611 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
3612 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
3613 glsl_get_bit_size(src
->type
));
3614 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3615 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
3616 vec
->src
[0].swizzle
[0] = i
;
3618 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
3619 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
3620 vec
->src
[j
].swizzle
[0] = i
;
3623 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3624 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
3627 dest
->transposed
= src
;
3632 static nir_ssa_def
*
3633 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
3634 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
3635 const uint32_t *indices
)
3637 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
3639 for (unsigned i
= 0; i
< num_components
; i
++) {
3640 uint32_t index
= indices
[i
];
3641 if (index
== 0xffffffff) {
3643 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
3644 } else if (index
< src0
->num_components
) {
3645 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
3646 vec
->src
[i
].swizzle
[0] = index
;
3648 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
3649 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
3653 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3655 return &vec
->dest
.dest
.ssa
;
3659 * Concatentates a number of vectors/scalars together to produce a vector
3661 static nir_ssa_def
*
3662 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
3663 unsigned num_srcs
, nir_ssa_def
**srcs
)
3665 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
3667 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3669 * "When constructing a vector, there must be at least two Constituent
3672 vtn_assert(num_srcs
>= 2);
3674 unsigned dest_idx
= 0;
3675 for (unsigned i
= 0; i
< num_srcs
; i
++) {
3676 nir_ssa_def
*src
= srcs
[i
];
3677 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
3678 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
3679 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
3680 vec
->src
[dest_idx
].swizzle
[0] = j
;
3685 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3687 * "When constructing a vector, the total number of components in all
3688 * the operands must equal the number of components in Result Type."
3690 vtn_assert(dest_idx
== num_components
);
3692 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3694 return &vec
->dest
.dest
.ssa
;
3697 static struct vtn_ssa_value
*
3698 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3700 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3701 dest
->type
= src
->type
;
3703 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3704 dest
->def
= src
->def
;
3706 unsigned elems
= glsl_get_length(src
->type
);
3708 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3709 for (unsigned i
= 0; i
< elems
; i
++)
3710 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3716 static struct vtn_ssa_value
*
3717 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3718 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3719 unsigned num_indices
)
3721 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3723 struct vtn_ssa_value
*cur
= dest
;
3725 for (i
= 0; i
< num_indices
- 1; i
++) {
3726 /* If we got a vector here, that means the next index will be trying to
3727 * dereference a scalar.
3729 vtn_fail_if(glsl_type_is_vector_or_scalar(cur
->type
),
3730 "OpCompositeInsert has too many indices.");
3731 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3732 "All indices in an OpCompositeInsert must be in-bounds");
3733 cur
= cur
->elems
[indices
[i
]];
3736 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3737 vtn_fail_if(indices
[i
] >= glsl_get_vector_elements(cur
->type
),
3738 "All indices in an OpCompositeInsert must be in-bounds");
3740 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3741 * the component granularity. In that case, the last index will be
3742 * the index to insert the scalar into the vector.
3745 cur
->def
= nir_vector_insert_imm(&b
->nb
, cur
->def
, insert
->def
, indices
[i
]);
3747 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3748 "All indices in an OpCompositeInsert must be in-bounds");
3749 cur
->elems
[indices
[i
]] = insert
;
3755 static struct vtn_ssa_value
*
3756 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3757 const uint32_t *indices
, unsigned num_indices
)
3759 struct vtn_ssa_value
*cur
= src
;
3760 for (unsigned i
= 0; i
< num_indices
; i
++) {
3761 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3762 vtn_assert(i
== num_indices
- 1);
3763 vtn_fail_if(indices
[i
] >= glsl_get_vector_elements(cur
->type
),
3764 "All indices in an OpCompositeExtract must be in-bounds");
3766 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3767 * the component granularity. The last index will be the index of the
3768 * vector to extract.
3771 const struct glsl_type
*scalar_type
=
3772 glsl_scalar_type(glsl_get_base_type(cur
->type
));
3773 struct vtn_ssa_value
*ret
= vtn_create_ssa_value(b
, scalar_type
);
3774 ret
->def
= nir_channel(&b
->nb
, cur
->def
, indices
[i
]);
3777 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3778 "All indices in an OpCompositeExtract must be in-bounds");
3779 cur
= cur
->elems
[indices
[i
]];
3787 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3788 const uint32_t *w
, unsigned count
)
3790 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
3791 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, type
->type
);
3794 case SpvOpVectorExtractDynamic
:
3795 ssa
->def
= nir_vector_extract(&b
->nb
, vtn_get_nir_ssa(b
, w
[3]),
3796 vtn_get_nir_ssa(b
, w
[4]));
3799 case SpvOpVectorInsertDynamic
:
3800 ssa
->def
= nir_vector_insert(&b
->nb
, vtn_get_nir_ssa(b
, w
[3]),
3801 vtn_get_nir_ssa(b
, w
[4]),
3802 vtn_get_nir_ssa(b
, w
[5]));
3805 case SpvOpVectorShuffle
:
3806 ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
->type
),
3807 vtn_get_nir_ssa(b
, w
[3]),
3808 vtn_get_nir_ssa(b
, w
[4]),
3812 case SpvOpCompositeConstruct
: {
3813 unsigned elems
= count
- 3;
3815 if (glsl_type_is_vector_or_scalar(type
->type
)) {
3816 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3817 for (unsigned i
= 0; i
< elems
; i
++)
3818 srcs
[i
] = vtn_get_nir_ssa(b
, w
[3 + i
]);
3820 vtn_vector_construct(b
, glsl_get_vector_elements(type
->type
),
3823 ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3824 for (unsigned i
= 0; i
< elems
; i
++)
3825 ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3829 case SpvOpCompositeExtract
:
3830 ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3834 case SpvOpCompositeInsert
:
3835 ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3836 vtn_ssa_value(b
, w
[3]),
3840 case SpvOpCopyLogical
:
3841 ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3843 case SpvOpCopyObject
:
3844 vtn_copy_value(b
, w
[3], w
[2]);
3848 vtn_fail_with_opcode("unknown composite operation", opcode
);
3851 vtn_push_ssa_value(b
, w
[2], ssa
);
3855 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3857 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3858 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3862 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3863 SpvMemorySemanticsMask semantics
)
3865 if (b
->shader
->options
->use_scoped_barrier
) {
3866 vtn_emit_scoped_memory_barrier(b
, scope
, semantics
);
3870 static const SpvMemorySemanticsMask all_memory_semantics
=
3871 SpvMemorySemanticsUniformMemoryMask
|
3872 SpvMemorySemanticsWorkgroupMemoryMask
|
3873 SpvMemorySemanticsAtomicCounterMemoryMask
|
3874 SpvMemorySemanticsImageMemoryMask
|
3875 SpvMemorySemanticsOutputMemoryMask
;
3877 /* If we're not actually doing a memory barrier, bail */
3878 if (!(semantics
& all_memory_semantics
))
3881 /* GL and Vulkan don't have these */
3882 vtn_assert(scope
!= SpvScopeCrossDevice
);
3884 if (scope
== SpvScopeSubgroup
)
3885 return; /* Nothing to do here */
3887 if (scope
== SpvScopeWorkgroup
) {
3888 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3892 /* There's only two scopes thing left */
3893 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3895 /* Map the GLSL memoryBarrier() construct and any barriers with more than one
3896 * semantic to the corresponding NIR one.
3898 if (util_bitcount(semantics
& all_memory_semantics
) > 1) {
3899 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3900 if (semantics
& SpvMemorySemanticsOutputMemoryMask
) {
3901 /* GLSL memoryBarrier() (and the corresponding NIR one) doesn't include
3902 * TCS outputs, so we have to emit it's own intrinsic for that. We
3903 * then need to emit another memory_barrier to prevent moving
3904 * non-output operations to before the tcs_patch barrier.
3906 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_tcs_patch
);
3907 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3912 /* Issue a more specific barrier */
3913 switch (semantics
& all_memory_semantics
) {
3914 case SpvMemorySemanticsUniformMemoryMask
:
3915 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3917 case SpvMemorySemanticsWorkgroupMemoryMask
:
3918 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3920 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3921 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3923 case SpvMemorySemanticsImageMemoryMask
:
3924 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3926 case SpvMemorySemanticsOutputMemoryMask
:
3927 if (b
->nb
.shader
->info
.stage
== MESA_SHADER_TESS_CTRL
)
3928 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_tcs_patch
);
3936 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3937 const uint32_t *w
, UNUSED
unsigned count
)
3940 case SpvOpEmitVertex
:
3941 case SpvOpEmitStreamVertex
:
3942 case SpvOpEndPrimitive
:
3943 case SpvOpEndStreamPrimitive
: {
3944 nir_intrinsic_op intrinsic_op
;
3946 case SpvOpEmitVertex
:
3947 case SpvOpEmitStreamVertex
:
3948 intrinsic_op
= nir_intrinsic_emit_vertex
;
3950 case SpvOpEndPrimitive
:
3951 case SpvOpEndStreamPrimitive
:
3952 intrinsic_op
= nir_intrinsic_end_primitive
;
3955 unreachable("Invalid opcode");
3958 nir_intrinsic_instr
*intrin
=
3959 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3962 case SpvOpEmitStreamVertex
:
3963 case SpvOpEndStreamPrimitive
: {
3964 unsigned stream
= vtn_constant_uint(b
, w
[1]);
3965 nir_intrinsic_set_stream_id(intrin
, stream
);
3973 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3977 case SpvOpMemoryBarrier
: {
3978 SpvScope scope
= vtn_constant_uint(b
, w
[1]);
3979 SpvMemorySemanticsMask semantics
= vtn_constant_uint(b
, w
[2]);
3980 vtn_emit_memory_barrier(b
, scope
, semantics
);
3984 case SpvOpControlBarrier
: {
3985 SpvScope execution_scope
= vtn_constant_uint(b
, w
[1]);
3986 SpvScope memory_scope
= vtn_constant_uint(b
, w
[2]);
3987 SpvMemorySemanticsMask memory_semantics
= vtn_constant_uint(b
, w
[3]);
3989 /* GLSLang, prior to commit 8297936dd6eb3, emitted OpControlBarrier with
3990 * memory semantics of None for GLSL barrier().
3991 * And before that, prior to c3f1cdfa, emitted the OpControlBarrier with
3992 * Device instead of Workgroup for execution scope.
3994 if (b
->wa_glslang_cs_barrier
&&
3995 b
->nb
.shader
->info
.stage
== MESA_SHADER_COMPUTE
&&
3996 (execution_scope
== SpvScopeWorkgroup
||
3997 execution_scope
== SpvScopeDevice
) &&
3998 memory_semantics
== SpvMemorySemanticsMaskNone
) {
3999 execution_scope
= SpvScopeWorkgroup
;
4000 memory_scope
= SpvScopeWorkgroup
;
4001 memory_semantics
= SpvMemorySemanticsAcquireReleaseMask
|
4002 SpvMemorySemanticsWorkgroupMemoryMask
;
4005 /* From the SPIR-V spec:
4007 * "When used with the TessellationControl execution model, it also
4008 * implicitly synchronizes the Output Storage Class: Writes to Output
4009 * variables performed by any invocation executed prior to a
4010 * OpControlBarrier will be visible to any other invocation after
4011 * return from that OpControlBarrier."
4013 if (b
->nb
.shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
4014 memory_semantics
&= ~(SpvMemorySemanticsAcquireMask
|
4015 SpvMemorySemanticsReleaseMask
|
4016 SpvMemorySemanticsAcquireReleaseMask
|
4017 SpvMemorySemanticsSequentiallyConsistentMask
);
4018 memory_semantics
|= SpvMemorySemanticsAcquireReleaseMask
|
4019 SpvMemorySemanticsOutputMemoryMask
;
4022 if (b
->shader
->options
->use_scoped_barrier
) {
4023 vtn_emit_scoped_control_barrier(b
, execution_scope
, memory_scope
,
4026 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
4028 if (execution_scope
== SpvScopeWorkgroup
)
4029 vtn_emit_barrier(b
, nir_intrinsic_control_barrier
);
4035 unreachable("unknown barrier instruction");
4040 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
4041 SpvExecutionMode mode
)
4044 case SpvExecutionModeInputPoints
:
4045 case SpvExecutionModeOutputPoints
:
4046 return 0; /* GL_POINTS */
4047 case SpvExecutionModeInputLines
:
4048 return 1; /* GL_LINES */
4049 case SpvExecutionModeInputLinesAdjacency
:
4050 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
4051 case SpvExecutionModeTriangles
:
4052 return 4; /* GL_TRIANGLES */
4053 case SpvExecutionModeInputTrianglesAdjacency
:
4054 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
4055 case SpvExecutionModeQuads
:
4056 return 7; /* GL_QUADS */
4057 case SpvExecutionModeIsolines
:
4058 return 0x8E7A; /* GL_ISOLINES */
4059 case SpvExecutionModeOutputLineStrip
:
4060 return 3; /* GL_LINE_STRIP */
4061 case SpvExecutionModeOutputTriangleStrip
:
4062 return 5; /* GL_TRIANGLE_STRIP */
4064 vtn_fail("Invalid primitive type: %s (%u)",
4065 spirv_executionmode_to_string(mode
), mode
);
4070 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
4071 SpvExecutionMode mode
)
4074 case SpvExecutionModeInputPoints
:
4076 case SpvExecutionModeInputLines
:
4078 case SpvExecutionModeInputLinesAdjacency
:
4080 case SpvExecutionModeTriangles
:
4082 case SpvExecutionModeInputTrianglesAdjacency
:
4085 vtn_fail("Invalid GS input mode: %s (%u)",
4086 spirv_executionmode_to_string(mode
), mode
);
4090 static gl_shader_stage
4091 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
4094 case SpvExecutionModelVertex
:
4095 return MESA_SHADER_VERTEX
;
4096 case SpvExecutionModelTessellationControl
:
4097 return MESA_SHADER_TESS_CTRL
;
4098 case SpvExecutionModelTessellationEvaluation
:
4099 return MESA_SHADER_TESS_EVAL
;
4100 case SpvExecutionModelGeometry
:
4101 return MESA_SHADER_GEOMETRY
;
4102 case SpvExecutionModelFragment
:
4103 return MESA_SHADER_FRAGMENT
;
4104 case SpvExecutionModelGLCompute
:
4105 return MESA_SHADER_COMPUTE
;
4106 case SpvExecutionModelKernel
:
4107 return MESA_SHADER_KERNEL
;
4109 vtn_fail("Unsupported execution model: %s (%u)",
4110 spirv_executionmodel_to_string(model
), model
);
4114 #define spv_check_supported(name, cap) do { \
4115 if (!(b->options && b->options->caps.name)) \
4116 vtn_warn("Unsupported SPIR-V capability: %s (%u)", \
4117 spirv_capability_to_string(cap), cap); \
4122 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
4125 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
4126 /* Let this be a name label regardless */
4127 unsigned name_words
;
4128 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
4130 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
4131 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
4134 vtn_assert(b
->entry_point
== NULL
);
4135 b
->entry_point
= entry_point
;
4139 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4140 const uint32_t *w
, unsigned count
)
4147 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
4148 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
4149 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
4150 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
4151 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
4152 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
4155 uint32_t version
= w
[2];
4158 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
4160 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
4164 case SpvOpSourceExtension
:
4165 case SpvOpSourceContinued
:
4166 case SpvOpExtension
:
4167 case SpvOpModuleProcessed
:
4168 /* Unhandled, but these are for debug so that's ok. */
4171 case SpvOpCapability
: {
4172 SpvCapability cap
= w
[1];
4174 case SpvCapabilityMatrix
:
4175 case SpvCapabilityShader
:
4176 case SpvCapabilityGeometry
:
4177 case SpvCapabilityGeometryPointSize
:
4178 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
4179 case SpvCapabilitySampledImageArrayDynamicIndexing
:
4180 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
4181 case SpvCapabilityStorageImageArrayDynamicIndexing
:
4182 case SpvCapabilityImageRect
:
4183 case SpvCapabilitySampledRect
:
4184 case SpvCapabilitySampled1D
:
4185 case SpvCapabilityImage1D
:
4186 case SpvCapabilitySampledCubeArray
:
4187 case SpvCapabilityImageCubeArray
:
4188 case SpvCapabilitySampledBuffer
:
4189 case SpvCapabilityImageBuffer
:
4190 case SpvCapabilityImageQuery
:
4191 case SpvCapabilityDerivativeControl
:
4192 case SpvCapabilityInterpolationFunction
:
4193 case SpvCapabilityMultiViewport
:
4194 case SpvCapabilitySampleRateShading
:
4195 case SpvCapabilityClipDistance
:
4196 case SpvCapabilityCullDistance
:
4197 case SpvCapabilityInputAttachment
:
4198 case SpvCapabilityImageGatherExtended
:
4199 case SpvCapabilityStorageImageExtendedFormats
:
4200 case SpvCapabilityVector16
:
4203 case SpvCapabilityLinkage
:
4204 case SpvCapabilityFloat16Buffer
:
4205 case SpvCapabilitySparseResidency
:
4206 vtn_warn("Unsupported SPIR-V capability: %s",
4207 spirv_capability_to_string(cap
));
4210 case SpvCapabilityMinLod
:
4211 spv_check_supported(min_lod
, cap
);
4214 case SpvCapabilityAtomicStorage
:
4215 spv_check_supported(atomic_storage
, cap
);
4218 case SpvCapabilityFloat64
:
4219 spv_check_supported(float64
, cap
);
4221 case SpvCapabilityInt64
:
4222 spv_check_supported(int64
, cap
);
4224 case SpvCapabilityInt16
:
4225 spv_check_supported(int16
, cap
);
4227 case SpvCapabilityInt8
:
4228 spv_check_supported(int8
, cap
);
4231 case SpvCapabilityTransformFeedback
:
4232 spv_check_supported(transform_feedback
, cap
);
4235 case SpvCapabilityGeometryStreams
:
4236 spv_check_supported(geometry_streams
, cap
);
4239 case SpvCapabilityInt64Atomics
:
4240 spv_check_supported(int64_atomics
, cap
);
4243 case SpvCapabilityStorageImageMultisample
:
4244 spv_check_supported(storage_image_ms
, cap
);
4247 case SpvCapabilityAddresses
:
4248 spv_check_supported(address
, cap
);
4251 case SpvCapabilityKernel
:
4252 spv_check_supported(kernel
, cap
);
4255 case SpvCapabilityImageBasic
:
4256 spv_check_supported(kernel_image
, cap
);
4259 case SpvCapabilityLiteralSampler
:
4260 spv_check_supported(literal_sampler
, cap
);
4263 case SpvCapabilityImageReadWrite
:
4264 case SpvCapabilityImageMipmap
:
4265 case SpvCapabilityPipes
:
4266 case SpvCapabilityDeviceEnqueue
:
4267 case SpvCapabilityGenericPointer
:
4268 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
4269 spirv_capability_to_string(cap
));
4272 case SpvCapabilityImageMSArray
:
4273 spv_check_supported(image_ms_array
, cap
);
4276 case SpvCapabilityTessellation
:
4277 case SpvCapabilityTessellationPointSize
:
4278 spv_check_supported(tessellation
, cap
);
4281 case SpvCapabilityDrawParameters
:
4282 spv_check_supported(draw_parameters
, cap
);
4285 case SpvCapabilityStorageImageReadWithoutFormat
:
4286 spv_check_supported(image_read_without_format
, cap
);
4289 case SpvCapabilityStorageImageWriteWithoutFormat
:
4290 spv_check_supported(image_write_without_format
, cap
);
4293 case SpvCapabilityDeviceGroup
:
4294 spv_check_supported(device_group
, cap
);
4297 case SpvCapabilityMultiView
:
4298 spv_check_supported(multiview
, cap
);
4301 case SpvCapabilityGroupNonUniform
:
4302 spv_check_supported(subgroup_basic
, cap
);
4305 case SpvCapabilitySubgroupVoteKHR
:
4306 case SpvCapabilityGroupNonUniformVote
:
4307 spv_check_supported(subgroup_vote
, cap
);
4310 case SpvCapabilitySubgroupBallotKHR
:
4311 case SpvCapabilityGroupNonUniformBallot
:
4312 spv_check_supported(subgroup_ballot
, cap
);
4315 case SpvCapabilityGroupNonUniformShuffle
:
4316 case SpvCapabilityGroupNonUniformShuffleRelative
:
4317 spv_check_supported(subgroup_shuffle
, cap
);
4320 case SpvCapabilityGroupNonUniformQuad
:
4321 spv_check_supported(subgroup_quad
, cap
);
4324 case SpvCapabilityGroupNonUniformArithmetic
:
4325 case SpvCapabilityGroupNonUniformClustered
:
4326 spv_check_supported(subgroup_arithmetic
, cap
);
4329 case SpvCapabilityGroups
:
4330 spv_check_supported(amd_shader_ballot
, cap
);
4333 case SpvCapabilityVariablePointersStorageBuffer
:
4334 case SpvCapabilityVariablePointers
:
4335 spv_check_supported(variable_pointers
, cap
);
4336 b
->variable_pointers
= true;
4339 case SpvCapabilityStorageUniformBufferBlock16
:
4340 case SpvCapabilityStorageUniform16
:
4341 case SpvCapabilityStoragePushConstant16
:
4342 case SpvCapabilityStorageInputOutput16
:
4343 spv_check_supported(storage_16bit
, cap
);
4346 case SpvCapabilityShaderLayer
:
4347 case SpvCapabilityShaderViewportIndex
:
4348 case SpvCapabilityShaderViewportIndexLayerEXT
:
4349 spv_check_supported(shader_viewport_index_layer
, cap
);
4352 case SpvCapabilityStorageBuffer8BitAccess
:
4353 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
4354 case SpvCapabilityStoragePushConstant8
:
4355 spv_check_supported(storage_8bit
, cap
);
4358 case SpvCapabilityShaderNonUniformEXT
:
4359 spv_check_supported(descriptor_indexing
, cap
);
4362 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
4363 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
4364 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
4365 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
4368 case SpvCapabilityUniformBufferArrayNonUniformIndexingEXT
:
4369 case SpvCapabilitySampledImageArrayNonUniformIndexingEXT
:
4370 case SpvCapabilityStorageBufferArrayNonUniformIndexingEXT
:
4371 case SpvCapabilityStorageImageArrayNonUniformIndexingEXT
:
4372 case SpvCapabilityInputAttachmentArrayNonUniformIndexingEXT
:
4373 case SpvCapabilityUniformTexelBufferArrayNonUniformIndexingEXT
:
4374 case SpvCapabilityStorageTexelBufferArrayNonUniformIndexingEXT
:
4375 spv_check_supported(descriptor_array_non_uniform_indexing
, cap
);
4378 case SpvCapabilityRuntimeDescriptorArrayEXT
:
4379 spv_check_supported(runtime_descriptor_array
, cap
);
4382 case SpvCapabilityStencilExportEXT
:
4383 spv_check_supported(stencil_export
, cap
);
4386 case SpvCapabilitySampleMaskPostDepthCoverage
:
4387 spv_check_supported(post_depth_coverage
, cap
);
4390 case SpvCapabilityDenormFlushToZero
:
4391 case SpvCapabilityDenormPreserve
:
4392 case SpvCapabilitySignedZeroInfNanPreserve
:
4393 case SpvCapabilityRoundingModeRTE
:
4394 case SpvCapabilityRoundingModeRTZ
:
4395 spv_check_supported(float_controls
, cap
);
4398 case SpvCapabilityPhysicalStorageBufferAddresses
:
4399 spv_check_supported(physical_storage_buffer_address
, cap
);
4402 case SpvCapabilityComputeDerivativeGroupQuadsNV
:
4403 case SpvCapabilityComputeDerivativeGroupLinearNV
:
4404 spv_check_supported(derivative_group
, cap
);
4407 case SpvCapabilityFloat16
:
4408 spv_check_supported(float16
, cap
);
4411 case SpvCapabilityFragmentShaderSampleInterlockEXT
:
4412 spv_check_supported(fragment_shader_sample_interlock
, cap
);
4415 case SpvCapabilityFragmentShaderPixelInterlockEXT
:
4416 spv_check_supported(fragment_shader_pixel_interlock
, cap
);
4419 case SpvCapabilityDemoteToHelperInvocationEXT
:
4420 spv_check_supported(demote_to_helper_invocation
, cap
);
4423 case SpvCapabilityShaderClockKHR
:
4424 spv_check_supported(shader_clock
, cap
);
4427 case SpvCapabilityVulkanMemoryModel
:
4428 spv_check_supported(vk_memory_model
, cap
);
4431 case SpvCapabilityVulkanMemoryModelDeviceScope
:
4432 spv_check_supported(vk_memory_model_device_scope
, cap
);
4435 case SpvCapabilityImageReadWriteLodAMD
:
4436 spv_check_supported(amd_image_read_write_lod
, cap
);
4439 case SpvCapabilityIntegerFunctions2INTEL
:
4440 spv_check_supported(integer_functions2
, cap
);
4443 case SpvCapabilityFragmentMaskAMD
:
4444 spv_check_supported(amd_fragment_mask
, cap
);
4447 case SpvCapabilityImageGatherBiasLodAMD
:
4448 spv_check_supported(amd_image_gather_bias_lod
, cap
);
4451 case SpvCapabilityAtomicFloat32AddEXT
:
4452 spv_check_supported(float32_atomic_add
, cap
);
4455 case SpvCapabilityAtomicFloat64AddEXT
:
4456 spv_check_supported(float64_atomic_add
, cap
);
4460 vtn_fail("Unhandled capability: %s (%u)",
4461 spirv_capability_to_string(cap
), cap
);
4466 case SpvOpExtInstImport
:
4467 vtn_handle_extension(b
, opcode
, w
, count
);
4470 case SpvOpMemoryModel
:
4472 case SpvAddressingModelPhysical32
:
4473 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4474 "AddressingModelPhysical32 only supported for kernels");
4475 b
->shader
->info
.cs
.ptr_size
= 32;
4476 b
->physical_ptrs
= true;
4477 assert(nir_address_format_bit_size(b
->options
->global_addr_format
) == 32);
4478 assert(nir_address_format_num_components(b
->options
->global_addr_format
) == 1);
4479 assert(nir_address_format_bit_size(b
->options
->shared_addr_format
) == 32);
4480 assert(nir_address_format_num_components(b
->options
->shared_addr_format
) == 1);
4481 if (!b
->options
->constant_as_global
) {
4482 assert(nir_address_format_bit_size(b
->options
->ubo_addr_format
) == 32);
4483 assert(nir_address_format_num_components(b
->options
->ubo_addr_format
) == 1);
4486 case SpvAddressingModelPhysical64
:
4487 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4488 "AddressingModelPhysical64 only supported for kernels");
4489 b
->shader
->info
.cs
.ptr_size
= 64;
4490 b
->physical_ptrs
= true;
4491 assert(nir_address_format_bit_size(b
->options
->global_addr_format
) == 64);
4492 assert(nir_address_format_num_components(b
->options
->global_addr_format
) == 1);
4493 assert(nir_address_format_bit_size(b
->options
->shared_addr_format
) == 64);
4494 assert(nir_address_format_num_components(b
->options
->shared_addr_format
) == 1);
4495 if (!b
->options
->constant_as_global
) {
4496 assert(nir_address_format_bit_size(b
->options
->ubo_addr_format
) == 64);
4497 assert(nir_address_format_num_components(b
->options
->ubo_addr_format
) == 1);
4500 case SpvAddressingModelLogical
:
4501 vtn_fail_if(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
,
4502 "AddressingModelLogical only supported for shaders");
4503 b
->physical_ptrs
= false;
4505 case SpvAddressingModelPhysicalStorageBuffer64
:
4506 vtn_fail_if(!b
->options
||
4507 !b
->options
->caps
.physical_storage_buffer_address
,
4508 "AddressingModelPhysicalStorageBuffer64 not supported");
4511 vtn_fail("Unknown addressing model: %s (%u)",
4512 spirv_addressingmodel_to_string(w
[1]), w
[1]);
4516 b
->mem_model
= w
[2];
4518 case SpvMemoryModelSimple
:
4519 case SpvMemoryModelGLSL450
:
4520 case SpvMemoryModelOpenCL
:
4522 case SpvMemoryModelVulkan
:
4523 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
4524 "Vulkan memory model is unsupported by this driver");
4527 vtn_fail("Unsupported memory model: %s",
4528 spirv_memorymodel_to_string(w
[2]));
4533 case SpvOpEntryPoint
:
4534 vtn_handle_entry_point(b
, w
, count
);
4538 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
4539 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4543 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4546 case SpvOpMemberName
:
4550 case SpvOpExecutionMode
:
4551 case SpvOpExecutionModeId
:
4552 case SpvOpDecorationGroup
:
4554 case SpvOpDecorateId
:
4555 case SpvOpMemberDecorate
:
4556 case SpvOpGroupDecorate
:
4557 case SpvOpGroupMemberDecorate
:
4558 case SpvOpDecorateString
:
4559 case SpvOpMemberDecorateString
:
4560 vtn_handle_decoration(b
, opcode
, w
, count
);
4563 case SpvOpExtInst
: {
4564 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4565 if (val
->ext_handler
== vtn_handle_non_semantic_instruction
) {
4566 /* NonSemantic extended instructions are acceptable in preamble. */
4567 vtn_handle_non_semantic_instruction(b
, w
[4], w
, count
);
4570 return false; /* End of preamble. */
4575 return false; /* End of preamble */
4582 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4583 const struct vtn_decoration
*mode
, UNUSED
void *data
)
4585 vtn_assert(b
->entry_point
== entry_point
);
4587 switch(mode
->exec_mode
) {
4588 case SpvExecutionModeOriginUpperLeft
:
4589 case SpvExecutionModeOriginLowerLeft
:
4590 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4591 b
->shader
->info
.fs
.origin_upper_left
=
4592 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
4595 case SpvExecutionModeEarlyFragmentTests
:
4596 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4597 b
->shader
->info
.fs
.early_fragment_tests
= true;
4600 case SpvExecutionModePostDepthCoverage
:
4601 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4602 b
->shader
->info
.fs
.post_depth_coverage
= true;
4605 case SpvExecutionModeInvocations
:
4606 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4607 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->operands
[0]);
4610 case SpvExecutionModeDepthReplacing
:
4611 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4612 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
4614 case SpvExecutionModeDepthGreater
:
4615 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4616 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
4618 case SpvExecutionModeDepthLess
:
4619 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4620 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
4622 case SpvExecutionModeDepthUnchanged
:
4623 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4624 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
4627 case SpvExecutionModeLocalSize
:
4628 vtn_assert(gl_shader_stage_is_compute(b
->shader
->info
.stage
));
4629 b
->shader
->info
.cs
.local_size
[0] = mode
->operands
[0];
4630 b
->shader
->info
.cs
.local_size
[1] = mode
->operands
[1];
4631 b
->shader
->info
.cs
.local_size
[2] = mode
->operands
[2];
4634 case SpvExecutionModeLocalSizeHint
:
4635 break; /* Nothing to do with this */
4637 case SpvExecutionModeOutputVertices
:
4638 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4639 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4640 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->operands
[0];
4642 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4643 b
->shader
->info
.gs
.vertices_out
= mode
->operands
[0];
4647 case SpvExecutionModeInputPoints
:
4648 case SpvExecutionModeInputLines
:
4649 case SpvExecutionModeInputLinesAdjacency
:
4650 case SpvExecutionModeTriangles
:
4651 case SpvExecutionModeInputTrianglesAdjacency
:
4652 case SpvExecutionModeQuads
:
4653 case SpvExecutionModeIsolines
:
4654 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4655 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4656 b
->shader
->info
.tess
.primitive_mode
=
4657 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4659 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4660 b
->shader
->info
.gs
.vertices_in
=
4661 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
4662 b
->shader
->info
.gs
.input_primitive
=
4663 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4667 case SpvExecutionModeOutputPoints
:
4668 case SpvExecutionModeOutputLineStrip
:
4669 case SpvExecutionModeOutputTriangleStrip
:
4670 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4671 b
->shader
->info
.gs
.output_primitive
=
4672 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4675 case SpvExecutionModeSpacingEqual
:
4676 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4677 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4678 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
4680 case SpvExecutionModeSpacingFractionalEven
:
4681 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4682 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4683 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
4685 case SpvExecutionModeSpacingFractionalOdd
:
4686 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4687 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4688 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
4690 case SpvExecutionModeVertexOrderCw
:
4691 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4692 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4693 b
->shader
->info
.tess
.ccw
= false;
4695 case SpvExecutionModeVertexOrderCcw
:
4696 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4697 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4698 b
->shader
->info
.tess
.ccw
= true;
4700 case SpvExecutionModePointMode
:
4701 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4702 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4703 b
->shader
->info
.tess
.point_mode
= true;
4706 case SpvExecutionModePixelCenterInteger
:
4707 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4708 b
->shader
->info
.fs
.pixel_center_integer
= true;
4711 case SpvExecutionModeXfb
:
4712 b
->shader
->info
.has_transform_feedback_varyings
= true;
4715 case SpvExecutionModeVecTypeHint
:
4718 case SpvExecutionModeContractionOff
:
4719 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
4720 vtn_warn("ExectionMode only allowed for CL-style kernels: %s",
4721 spirv_executionmode_to_string(mode
->exec_mode
));
4726 case SpvExecutionModeStencilRefReplacingEXT
:
4727 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4730 case SpvExecutionModeDerivativeGroupQuadsNV
:
4731 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4732 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_QUADS
;
4735 case SpvExecutionModeDerivativeGroupLinearNV
:
4736 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4737 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_LINEAR
;
4740 case SpvExecutionModePixelInterlockOrderedEXT
:
4741 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4742 b
->shader
->info
.fs
.pixel_interlock_ordered
= true;
4745 case SpvExecutionModePixelInterlockUnorderedEXT
:
4746 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4747 b
->shader
->info
.fs
.pixel_interlock_unordered
= true;
4750 case SpvExecutionModeSampleInterlockOrderedEXT
:
4751 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4752 b
->shader
->info
.fs
.sample_interlock_ordered
= true;
4755 case SpvExecutionModeSampleInterlockUnorderedEXT
:
4756 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4757 b
->shader
->info
.fs
.sample_interlock_unordered
= true;
4760 case SpvExecutionModeDenormPreserve
:
4761 case SpvExecutionModeDenormFlushToZero
:
4762 case SpvExecutionModeSignedZeroInfNanPreserve
:
4763 case SpvExecutionModeRoundingModeRTE
:
4764 case SpvExecutionModeRoundingModeRTZ
: {
4765 unsigned execution_mode
= 0;
4766 switch (mode
->exec_mode
) {
4767 case SpvExecutionModeDenormPreserve
:
4768 switch (mode
->operands
[0]) {
4769 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP16
; break;
4770 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP32
; break;
4771 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP64
; break;
4772 default: vtn_fail("Floating point type not supported");
4775 case SpvExecutionModeDenormFlushToZero
:
4776 switch (mode
->operands
[0]) {
4777 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
; break;
4778 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32
; break;
4779 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64
; break;
4780 default: vtn_fail("Floating point type not supported");
4783 case SpvExecutionModeSignedZeroInfNanPreserve
:
4784 switch (mode
->operands
[0]) {
4785 case 16: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16
; break;
4786 case 32: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32
; break;
4787 case 64: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64
; break;
4788 default: vtn_fail("Floating point type not supported");
4791 case SpvExecutionModeRoundingModeRTE
:
4792 switch (mode
->operands
[0]) {
4793 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16
; break;
4794 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32
; break;
4795 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64
; break;
4796 default: vtn_fail("Floating point type not supported");
4799 case SpvExecutionModeRoundingModeRTZ
:
4800 switch (mode
->operands
[0]) {
4801 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
; break;
4802 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
; break;
4803 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
; break;
4804 default: vtn_fail("Floating point type not supported");
4811 b
->shader
->info
.float_controls_execution_mode
|= execution_mode
;
4815 case SpvExecutionModeLocalSizeId
:
4816 case SpvExecutionModeLocalSizeHintId
:
4817 /* Handled later by vtn_handle_execution_mode_id(). */
4821 vtn_fail("Unhandled execution mode: %s (%u)",
4822 spirv_executionmode_to_string(mode
->exec_mode
),
4828 vtn_handle_execution_mode_id(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4829 const struct vtn_decoration
*mode
, UNUSED
void *data
)
4832 vtn_assert(b
->entry_point
== entry_point
);
4834 switch (mode
->exec_mode
) {
4835 case SpvExecutionModeLocalSizeId
:
4836 b
->shader
->info
.cs
.local_size
[0] = vtn_constant_uint(b
, mode
->operands
[0]);
4837 b
->shader
->info
.cs
.local_size
[1] = vtn_constant_uint(b
, mode
->operands
[1]);
4838 b
->shader
->info
.cs
.local_size
[2] = vtn_constant_uint(b
, mode
->operands
[2]);
4841 case SpvExecutionModeLocalSizeHintId
:
4842 /* Nothing to do with this hint. */
4846 /* Nothing to do. Literal execution modes already handled by
4847 * vtn_handle_execution_mode(). */
4853 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4854 const uint32_t *w
, unsigned count
)
4856 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
4860 case SpvOpSourceContinued
:
4861 case SpvOpSourceExtension
:
4862 case SpvOpExtension
:
4863 case SpvOpCapability
:
4864 case SpvOpExtInstImport
:
4865 case SpvOpMemoryModel
:
4866 case SpvOpEntryPoint
:
4867 case SpvOpExecutionMode
:
4870 case SpvOpMemberName
:
4871 case SpvOpDecorationGroup
:
4873 case SpvOpDecorateId
:
4874 case SpvOpMemberDecorate
:
4875 case SpvOpGroupDecorate
:
4876 case SpvOpGroupMemberDecorate
:
4877 case SpvOpDecorateString
:
4878 case SpvOpMemberDecorateString
:
4879 vtn_fail("Invalid opcode types and variables section");
4885 case SpvOpTypeFloat
:
4886 case SpvOpTypeVector
:
4887 case SpvOpTypeMatrix
:
4888 case SpvOpTypeImage
:
4889 case SpvOpTypeSampler
:
4890 case SpvOpTypeSampledImage
:
4891 case SpvOpTypeArray
:
4892 case SpvOpTypeRuntimeArray
:
4893 case SpvOpTypeStruct
:
4894 case SpvOpTypeOpaque
:
4895 case SpvOpTypePointer
:
4896 case SpvOpTypeForwardPointer
:
4897 case SpvOpTypeFunction
:
4898 case SpvOpTypeEvent
:
4899 case SpvOpTypeDeviceEvent
:
4900 case SpvOpTypeReserveId
:
4901 case SpvOpTypeQueue
:
4903 vtn_handle_type(b
, opcode
, w
, count
);
4906 case SpvOpConstantTrue
:
4907 case SpvOpConstantFalse
:
4909 case SpvOpConstantComposite
:
4910 case SpvOpConstantNull
:
4911 case SpvOpSpecConstantTrue
:
4912 case SpvOpSpecConstantFalse
:
4913 case SpvOpSpecConstant
:
4914 case SpvOpSpecConstantComposite
:
4915 case SpvOpSpecConstantOp
:
4916 vtn_handle_constant(b
, opcode
, w
, count
);
4921 case SpvOpConstantSampler
:
4922 vtn_handle_variables(b
, opcode
, w
, count
);
4925 case SpvOpExtInst
: {
4926 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4927 /* NonSemantic extended instructions are acceptable in preamble, others
4928 * will indicate the end of preamble.
4930 return val
->ext_handler
== vtn_handle_non_semantic_instruction
;
4934 return false; /* End of preamble */
4940 static struct vtn_ssa_value
*
4941 vtn_nir_select(struct vtn_builder
*b
, struct vtn_ssa_value
*src0
,
4942 struct vtn_ssa_value
*src1
, struct vtn_ssa_value
*src2
)
4944 struct vtn_ssa_value
*dest
= rzalloc(b
, struct vtn_ssa_value
);
4945 dest
->type
= src1
->type
;
4947 if (glsl_type_is_vector_or_scalar(src1
->type
)) {
4948 dest
->def
= nir_bcsel(&b
->nb
, src0
->def
, src1
->def
, src2
->def
);
4950 unsigned elems
= glsl_get_length(src1
->type
);
4952 dest
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
4953 for (unsigned i
= 0; i
< elems
; i
++) {
4954 dest
->elems
[i
] = vtn_nir_select(b
, src0
,
4955 src1
->elems
[i
], src2
->elems
[i
]);
4963 vtn_handle_select(struct vtn_builder
*b
, SpvOp opcode
,
4964 const uint32_t *w
, unsigned count
)
4966 /* Handle OpSelect up-front here because it needs to be able to handle
4967 * pointers and not just regular vectors and scalars.
4969 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
4970 struct vtn_value
*cond_val
= vtn_untyped_value(b
, w
[3]);
4971 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
4972 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
4974 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4975 obj2_val
->type
!= res_val
->type
,
4976 "Object types must match the result type in OpSelect");
4978 vtn_fail_if((cond_val
->type
->base_type
!= vtn_base_type_scalar
&&
4979 cond_val
->type
->base_type
!= vtn_base_type_vector
) ||
4980 !glsl_type_is_boolean(cond_val
->type
->type
),
4981 "OpSelect must have either a vector of booleans or "
4982 "a boolean as Condition type");
4984 vtn_fail_if(cond_val
->type
->base_type
== vtn_base_type_vector
&&
4985 (res_val
->type
->base_type
!= vtn_base_type_vector
||
4986 res_val
->type
->length
!= cond_val
->type
->length
),
4987 "When Condition type in OpSelect is a vector, the Result "
4988 "type must be a vector of the same length");
4990 switch (res_val
->type
->base_type
) {
4991 case vtn_base_type_scalar
:
4992 case vtn_base_type_vector
:
4993 case vtn_base_type_matrix
:
4994 case vtn_base_type_array
:
4995 case vtn_base_type_struct
:
4998 case vtn_base_type_pointer
:
4999 /* We need to have actual storage for pointer types. */
5000 vtn_fail_if(res_val
->type
->type
== NULL
,
5001 "Invalid pointer result type for OpSelect");
5004 vtn_fail("Result type of OpSelect must be a scalar, composite, or pointer");
5007 vtn_push_ssa_value(b
, w
[2],
5008 vtn_nir_select(b
, vtn_ssa_value(b
, w
[3]),
5009 vtn_ssa_value(b
, w
[4]),
5010 vtn_ssa_value(b
, w
[5])));
5014 vtn_handle_ptr(struct vtn_builder
*b
, SpvOp opcode
,
5015 const uint32_t *w
, unsigned count
)
5017 struct vtn_type
*type1
= vtn_get_value_type(b
, w
[3]);
5018 struct vtn_type
*type2
= vtn_get_value_type(b
, w
[4]);
5019 vtn_fail_if(type1
->base_type
!= vtn_base_type_pointer
||
5020 type2
->base_type
!= vtn_base_type_pointer
,
5021 "%s operands must have pointer types",
5022 spirv_op_to_string(opcode
));
5023 vtn_fail_if(type1
->storage_class
!= type2
->storage_class
,
5024 "%s operands must have the same storage class",
5025 spirv_op_to_string(opcode
));
5027 struct vtn_type
*vtn_type
= vtn_get_type(b
, w
[1]);
5028 const struct glsl_type
*type
= vtn_type
->type
;
5030 nir_address_format addr_format
= vtn_mode_to_address_format(
5031 b
, vtn_storage_class_to_mode(b
, type1
->storage_class
, NULL
, NULL
));
5036 case SpvOpPtrDiff
: {
5037 /* OpPtrDiff returns the difference in number of elements (not byte offset). */
5038 unsigned elem_size
, elem_align
;
5039 glsl_get_natural_size_align_bytes(type1
->deref
->type
,
5040 &elem_size
, &elem_align
);
5042 def
= nir_build_addr_isub(&b
->nb
,
5043 vtn_get_nir_ssa(b
, w
[3]),
5044 vtn_get_nir_ssa(b
, w
[4]),
5046 def
= nir_idiv(&b
->nb
, def
, nir_imm_intN_t(&b
->nb
, elem_size
, def
->bit_size
));
5047 def
= nir_i2i(&b
->nb
, def
, glsl_get_bit_size(type
));
5052 case SpvOpPtrNotEqual
: {
5053 def
= nir_build_addr_ieq(&b
->nb
,
5054 vtn_get_nir_ssa(b
, w
[3]),
5055 vtn_get_nir_ssa(b
, w
[4]),
5057 if (opcode
== SpvOpPtrNotEqual
)
5058 def
= nir_inot(&b
->nb
, def
);
5063 unreachable("Invalid ptr operation");
5066 vtn_push_nir_ssa(b
, w
[2], def
);
5070 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
5071 const uint32_t *w
, unsigned count
)
5077 case SpvOpLoopMerge
:
5078 case SpvOpSelectionMerge
:
5079 /* This is handled by cfg pre-pass and walk_blocks */
5083 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
5084 val
->type
= vtn_get_type(b
, w
[1]);
5089 vtn_handle_extension(b
, opcode
, w
, count
);
5095 case SpvOpCopyMemory
:
5096 case SpvOpCopyMemorySized
:
5097 case SpvOpAccessChain
:
5098 case SpvOpPtrAccessChain
:
5099 case SpvOpInBoundsAccessChain
:
5100 case SpvOpInBoundsPtrAccessChain
:
5101 case SpvOpArrayLength
:
5102 case SpvOpConvertPtrToU
:
5103 case SpvOpConvertUToPtr
:
5104 vtn_handle_variables(b
, opcode
, w
, count
);
5107 case SpvOpFunctionCall
:
5108 vtn_handle_function_call(b
, opcode
, w
, count
);
5111 case SpvOpSampledImage
:
5113 case SpvOpImageSampleImplicitLod
:
5114 case SpvOpImageSampleExplicitLod
:
5115 case SpvOpImageSampleDrefImplicitLod
:
5116 case SpvOpImageSampleDrefExplicitLod
:
5117 case SpvOpImageSampleProjImplicitLod
:
5118 case SpvOpImageSampleProjExplicitLod
:
5119 case SpvOpImageSampleProjDrefImplicitLod
:
5120 case SpvOpImageSampleProjDrefExplicitLod
:
5121 case SpvOpImageFetch
:
5122 case SpvOpImageGather
:
5123 case SpvOpImageDrefGather
:
5124 case SpvOpImageQueryLod
:
5125 case SpvOpImageQueryLevels
:
5126 case SpvOpImageQuerySamples
:
5127 vtn_handle_texture(b
, opcode
, w
, count
);
5130 case SpvOpImageRead
:
5131 case SpvOpImageWrite
:
5132 case SpvOpImageTexelPointer
:
5133 case SpvOpImageQueryFormat
:
5134 case SpvOpImageQueryOrder
:
5135 vtn_handle_image(b
, opcode
, w
, count
);
5138 case SpvOpImageQuerySizeLod
:
5139 case SpvOpImageQuerySize
: {
5140 struct vtn_type
*image_type
= vtn_get_value_type(b
, w
[3]);
5141 vtn_assert(image_type
->base_type
== vtn_base_type_image
);
5142 if (glsl_type_is_image(image_type
->glsl_image
)) {
5143 vtn_handle_image(b
, opcode
, w
, count
);
5145 vtn_assert(glsl_type_is_sampler(image_type
->glsl_image
));
5146 vtn_handle_texture(b
, opcode
, w
, count
);
5151 case SpvOpFragmentMaskFetchAMD
:
5152 case SpvOpFragmentFetchAMD
:
5153 vtn_handle_texture(b
, opcode
, w
, count
);
5156 case SpvOpAtomicLoad
:
5157 case SpvOpAtomicExchange
:
5158 case SpvOpAtomicCompareExchange
:
5159 case SpvOpAtomicCompareExchangeWeak
:
5160 case SpvOpAtomicIIncrement
:
5161 case SpvOpAtomicIDecrement
:
5162 case SpvOpAtomicIAdd
:
5163 case SpvOpAtomicISub
:
5164 case SpvOpAtomicSMin
:
5165 case SpvOpAtomicUMin
:
5166 case SpvOpAtomicSMax
:
5167 case SpvOpAtomicUMax
:
5168 case SpvOpAtomicAnd
:
5170 case SpvOpAtomicXor
:
5171 case SpvOpAtomicFAddEXT
: {
5172 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
5173 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
5174 vtn_handle_image(b
, opcode
, w
, count
);
5176 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
5177 vtn_handle_atomics(b
, opcode
, w
, count
);
5182 case SpvOpAtomicStore
: {
5183 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
5184 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
5185 vtn_handle_image(b
, opcode
, w
, count
);
5187 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
5188 vtn_handle_atomics(b
, opcode
, w
, count
);
5194 vtn_handle_select(b
, opcode
, w
, count
);
5202 case SpvOpConvertFToU
:
5203 case SpvOpConvertFToS
:
5204 case SpvOpConvertSToF
:
5205 case SpvOpConvertUToF
:
5209 case SpvOpQuantizeToF16
:
5210 case SpvOpPtrCastToGeneric
:
5211 case SpvOpGenericCastToPtr
:
5216 case SpvOpSignBitSet
:
5217 case SpvOpLessOrGreater
:
5219 case SpvOpUnordered
:
5234 case SpvOpVectorTimesScalar
:
5236 case SpvOpIAddCarry
:
5237 case SpvOpISubBorrow
:
5238 case SpvOpUMulExtended
:
5239 case SpvOpSMulExtended
:
5240 case SpvOpShiftRightLogical
:
5241 case SpvOpShiftRightArithmetic
:
5242 case SpvOpShiftLeftLogical
:
5243 case SpvOpLogicalEqual
:
5244 case SpvOpLogicalNotEqual
:
5245 case SpvOpLogicalOr
:
5246 case SpvOpLogicalAnd
:
5247 case SpvOpLogicalNot
:
5248 case SpvOpBitwiseOr
:
5249 case SpvOpBitwiseXor
:
5250 case SpvOpBitwiseAnd
:
5252 case SpvOpFOrdEqual
:
5253 case SpvOpFUnordEqual
:
5254 case SpvOpINotEqual
:
5255 case SpvOpFOrdNotEqual
:
5256 case SpvOpFUnordNotEqual
:
5257 case SpvOpULessThan
:
5258 case SpvOpSLessThan
:
5259 case SpvOpFOrdLessThan
:
5260 case SpvOpFUnordLessThan
:
5261 case SpvOpUGreaterThan
:
5262 case SpvOpSGreaterThan
:
5263 case SpvOpFOrdGreaterThan
:
5264 case SpvOpFUnordGreaterThan
:
5265 case SpvOpULessThanEqual
:
5266 case SpvOpSLessThanEqual
:
5267 case SpvOpFOrdLessThanEqual
:
5268 case SpvOpFUnordLessThanEqual
:
5269 case SpvOpUGreaterThanEqual
:
5270 case SpvOpSGreaterThanEqual
:
5271 case SpvOpFOrdGreaterThanEqual
:
5272 case SpvOpFUnordGreaterThanEqual
:
5278 case SpvOpFwidthFine
:
5279 case SpvOpDPdxCoarse
:
5280 case SpvOpDPdyCoarse
:
5281 case SpvOpFwidthCoarse
:
5282 case SpvOpBitFieldInsert
:
5283 case SpvOpBitFieldSExtract
:
5284 case SpvOpBitFieldUExtract
:
5285 case SpvOpBitReverse
:
5287 case SpvOpTranspose
:
5288 case SpvOpOuterProduct
:
5289 case SpvOpMatrixTimesScalar
:
5290 case SpvOpVectorTimesMatrix
:
5291 case SpvOpMatrixTimesVector
:
5292 case SpvOpMatrixTimesMatrix
:
5293 case SpvOpUCountLeadingZerosINTEL
:
5294 case SpvOpUCountTrailingZerosINTEL
:
5295 case SpvOpAbsISubINTEL
:
5296 case SpvOpAbsUSubINTEL
:
5297 case SpvOpIAddSatINTEL
:
5298 case SpvOpUAddSatINTEL
:
5299 case SpvOpIAverageINTEL
:
5300 case SpvOpUAverageINTEL
:
5301 case SpvOpIAverageRoundedINTEL
:
5302 case SpvOpUAverageRoundedINTEL
:
5303 case SpvOpISubSatINTEL
:
5304 case SpvOpUSubSatINTEL
:
5305 case SpvOpIMul32x16INTEL
:
5306 case SpvOpUMul32x16INTEL
:
5307 vtn_handle_alu(b
, opcode
, w
, count
);
5311 vtn_handle_bitcast(b
, w
, count
);
5314 case SpvOpVectorExtractDynamic
:
5315 case SpvOpVectorInsertDynamic
:
5316 case SpvOpVectorShuffle
:
5317 case SpvOpCompositeConstruct
:
5318 case SpvOpCompositeExtract
:
5319 case SpvOpCompositeInsert
:
5320 case SpvOpCopyLogical
:
5321 case SpvOpCopyObject
:
5322 vtn_handle_composite(b
, opcode
, w
, count
);
5325 case SpvOpEmitVertex
:
5326 case SpvOpEndPrimitive
:
5327 case SpvOpEmitStreamVertex
:
5328 case SpvOpEndStreamPrimitive
:
5329 case SpvOpControlBarrier
:
5330 case SpvOpMemoryBarrier
:
5331 vtn_handle_barrier(b
, opcode
, w
, count
);
5334 case SpvOpGroupNonUniformElect
:
5335 case SpvOpGroupNonUniformAll
:
5336 case SpvOpGroupNonUniformAny
:
5337 case SpvOpGroupNonUniformAllEqual
:
5338 case SpvOpGroupNonUniformBroadcast
:
5339 case SpvOpGroupNonUniformBroadcastFirst
:
5340 case SpvOpGroupNonUniformBallot
:
5341 case SpvOpGroupNonUniformInverseBallot
:
5342 case SpvOpGroupNonUniformBallotBitExtract
:
5343 case SpvOpGroupNonUniformBallotBitCount
:
5344 case SpvOpGroupNonUniformBallotFindLSB
:
5345 case SpvOpGroupNonUniformBallotFindMSB
:
5346 case SpvOpGroupNonUniformShuffle
:
5347 case SpvOpGroupNonUniformShuffleXor
:
5348 case SpvOpGroupNonUniformShuffleUp
:
5349 case SpvOpGroupNonUniformShuffleDown
:
5350 case SpvOpGroupNonUniformIAdd
:
5351 case SpvOpGroupNonUniformFAdd
:
5352 case SpvOpGroupNonUniformIMul
:
5353 case SpvOpGroupNonUniformFMul
:
5354 case SpvOpGroupNonUniformSMin
:
5355 case SpvOpGroupNonUniformUMin
:
5356 case SpvOpGroupNonUniformFMin
:
5357 case SpvOpGroupNonUniformSMax
:
5358 case SpvOpGroupNonUniformUMax
:
5359 case SpvOpGroupNonUniformFMax
:
5360 case SpvOpGroupNonUniformBitwiseAnd
:
5361 case SpvOpGroupNonUniformBitwiseOr
:
5362 case SpvOpGroupNonUniformBitwiseXor
:
5363 case SpvOpGroupNonUniformLogicalAnd
:
5364 case SpvOpGroupNonUniformLogicalOr
:
5365 case SpvOpGroupNonUniformLogicalXor
:
5366 case SpvOpGroupNonUniformQuadBroadcast
:
5367 case SpvOpGroupNonUniformQuadSwap
:
5370 case SpvOpGroupBroadcast
:
5371 case SpvOpGroupIAdd
:
5372 case SpvOpGroupFAdd
:
5373 case SpvOpGroupFMin
:
5374 case SpvOpGroupUMin
:
5375 case SpvOpGroupSMin
:
5376 case SpvOpGroupFMax
:
5377 case SpvOpGroupUMax
:
5378 case SpvOpGroupSMax
:
5379 case SpvOpSubgroupBallotKHR
:
5380 case SpvOpSubgroupFirstInvocationKHR
:
5381 case SpvOpSubgroupReadInvocationKHR
:
5382 case SpvOpSubgroupAllKHR
:
5383 case SpvOpSubgroupAnyKHR
:
5384 case SpvOpSubgroupAllEqualKHR
:
5385 case SpvOpGroupIAddNonUniformAMD
:
5386 case SpvOpGroupFAddNonUniformAMD
:
5387 case SpvOpGroupFMinNonUniformAMD
:
5388 case SpvOpGroupUMinNonUniformAMD
:
5389 case SpvOpGroupSMinNonUniformAMD
:
5390 case SpvOpGroupFMaxNonUniformAMD
:
5391 case SpvOpGroupUMaxNonUniformAMD
:
5392 case SpvOpGroupSMaxNonUniformAMD
:
5393 vtn_handle_subgroup(b
, opcode
, w
, count
);
5398 case SpvOpPtrNotEqual
:
5399 vtn_handle_ptr(b
, opcode
, w
, count
);
5402 case SpvOpBeginInvocationInterlockEXT
:
5403 vtn_emit_barrier(b
, nir_intrinsic_begin_invocation_interlock
);
5406 case SpvOpEndInvocationInterlockEXT
:
5407 vtn_emit_barrier(b
, nir_intrinsic_end_invocation_interlock
);
5410 case SpvOpDemoteToHelperInvocationEXT
: {
5411 nir_intrinsic_instr
*intrin
=
5412 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_demote
);
5413 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5417 case SpvOpIsHelperInvocationEXT
: {
5418 nir_intrinsic_instr
*intrin
=
5419 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_is_helper_invocation
);
5420 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 1, 1, NULL
);
5421 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5423 vtn_push_nir_ssa(b
, w
[2], &intrin
->dest
.ssa
);
5427 case SpvOpReadClockKHR
: {
5428 SpvScope scope
= vtn_constant_uint(b
, w
[3]);
5429 nir_scope nir_scope
;
5432 case SpvScopeDevice
:
5433 nir_scope
= NIR_SCOPE_DEVICE
;
5435 case SpvScopeSubgroup
:
5436 nir_scope
= NIR_SCOPE_SUBGROUP
;
5439 vtn_fail("invalid read clock scope");
5442 /* Operation supports two result types: uvec2 and uint64_t. The NIR
5443 * intrinsic gives uvec2, so pack the result for the other case.
5445 nir_intrinsic_instr
*intrin
=
5446 nir_intrinsic_instr_create(b
->nb
.shader
, nir_intrinsic_shader_clock
);
5447 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 2, 32, NULL
);
5448 nir_intrinsic_set_memory_scope(intrin
, nir_scope
);
5449 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5451 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
5452 const struct glsl_type
*dest_type
= type
->type
;
5453 nir_ssa_def
*result
;
5455 if (glsl_type_is_vector(dest_type
)) {
5456 assert(dest_type
== glsl_vector_type(GLSL_TYPE_UINT
, 2));
5457 result
= &intrin
->dest
.ssa
;
5459 assert(glsl_type_is_scalar(dest_type
));
5460 assert(glsl_get_base_type(dest_type
) == GLSL_TYPE_UINT64
);
5461 result
= nir_pack_64_2x32(&b
->nb
, &intrin
->dest
.ssa
);
5464 vtn_push_nir_ssa(b
, w
[2], result
);
5468 case SpvOpLifetimeStart
:
5469 case SpvOpLifetimeStop
:
5473 vtn_fail_with_opcode("Unhandled opcode", opcode
);
5480 vtn_create_builder(const uint32_t *words
, size_t word_count
,
5481 gl_shader_stage stage
, const char *entry_point_name
,
5482 const struct spirv_to_nir_options
*options
)
5484 /* Initialize the vtn_builder object */
5485 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
5486 struct spirv_to_nir_options
*dup_options
=
5487 ralloc(b
, struct spirv_to_nir_options
);
5488 *dup_options
= *options
;
5491 b
->spirv_word_count
= word_count
;
5495 list_inithead(&b
->functions
);
5496 b
->entry_point_stage
= stage
;
5497 b
->entry_point_name
= entry_point_name
;
5498 b
->options
= dup_options
;
5501 * Handle the SPIR-V header (first 5 dwords).
5502 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
5504 if (word_count
<= 5)
5507 if (words
[0] != SpvMagicNumber
) {
5508 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
5511 if (words
[1] < 0x10000) {
5512 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
5516 uint16_t generator_id
= words
[2] >> 16;
5517 uint16_t generator_version
= words
[2];
5519 /* In GLSLang commit 8297936dd6eb3, their handling of barrier() was fixed
5520 * to provide correct memory semantics on compute shader barrier()
5521 * commands. Prior to that, we need to fix them up ourselves. This
5522 * GLSLang fix caused them to bump to generator version 3.
5524 b
->wa_glslang_cs_barrier
= (generator_id
== 8 && generator_version
< 3);
5526 /* words[2] == generator magic */
5527 unsigned value_id_bound
= words
[3];
5528 if (words
[4] != 0) {
5529 vtn_err("words[4] was %u, want 0", words
[4]);
5533 b
->value_id_bound
= value_id_bound
;
5534 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
5542 static nir_function
*
5543 vtn_emit_kernel_entry_point_wrapper(struct vtn_builder
*b
,
5544 nir_function
*entry_point
)
5546 vtn_assert(entry_point
== b
->entry_point
->func
->impl
->function
);
5547 vtn_fail_if(!entry_point
->name
, "entry points are required to have a name");
5548 const char *func_name
=
5549 ralloc_asprintf(b
->shader
, "__wrapped_%s", entry_point
->name
);
5551 /* we shouldn't have any inputs yet */
5552 vtn_assert(!entry_point
->shader
->num_inputs
);
5553 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
);
5555 nir_function
*main_entry_point
= nir_function_create(b
->shader
, func_name
);
5556 main_entry_point
->impl
= nir_function_impl_create(main_entry_point
);
5557 nir_builder_init(&b
->nb
, main_entry_point
->impl
);
5558 b
->nb
.cursor
= nir_after_cf_list(&main_entry_point
->impl
->body
);
5559 b
->func_param_idx
= 0;
5561 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, entry_point
);
5563 for (unsigned i
= 0; i
< entry_point
->num_params
; ++i
) {
5564 struct vtn_type
*param_type
= b
->entry_point
->func
->type
->params
[i
];
5566 /* consider all pointers to function memory to be parameters passed
5569 bool is_by_val
= param_type
->base_type
== vtn_base_type_pointer
&&
5570 param_type
->storage_class
== SpvStorageClassFunction
;
5572 /* input variable */
5573 nir_variable
*in_var
= rzalloc(b
->nb
.shader
, nir_variable
);
5574 in_var
->data
.mode
= nir_var_uniform
;
5575 in_var
->data
.read_only
= true;
5576 in_var
->data
.location
= i
;
5577 if (param_type
->base_type
== vtn_base_type_image
) {
5578 in_var
->data
.access
= 0;
5579 if (param_type
->access_qualifier
& SpvAccessQualifierReadOnly
)
5580 in_var
->data
.access
|= ACCESS_NON_WRITEABLE
;
5581 if (param_type
->access_qualifier
& SpvAccessQualifierWriteOnly
)
5582 in_var
->data
.access
|= ACCESS_NON_READABLE
;
5586 in_var
->type
= param_type
->deref
->type
;
5587 else if (param_type
->base_type
== vtn_base_type_image
)
5588 in_var
->type
= param_type
->glsl_image
;
5589 else if (param_type
->base_type
== vtn_base_type_sampler
)
5590 in_var
->type
= glsl_bare_sampler_type();
5592 in_var
->type
= param_type
->type
;
5594 nir_shader_add_variable(b
->nb
.shader
, in_var
);
5595 b
->nb
.shader
->num_inputs
++;
5597 /* we have to copy the entire variable into function memory */
5599 nir_variable
*copy_var
=
5600 nir_local_variable_create(main_entry_point
->impl
, in_var
->type
,
5602 nir_copy_var(&b
->nb
, copy_var
, in_var
);
5604 nir_src_for_ssa(&nir_build_deref_var(&b
->nb
, copy_var
)->dest
.ssa
);
5605 } else if (param_type
->base_type
== vtn_base_type_image
||
5606 param_type
->base_type
== vtn_base_type_sampler
) {
5607 /* Don't load the var, just pass a deref of it */
5608 call
->params
[i
] = nir_src_for_ssa(&nir_build_deref_var(&b
->nb
, in_var
)->dest
.ssa
);
5610 call
->params
[i
] = nir_src_for_ssa(nir_load_var(&b
->nb
, in_var
));
5614 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
5616 return main_entry_point
;
5620 spirv_to_nir(const uint32_t *words
, size_t word_count
,
5621 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
5622 gl_shader_stage stage
, const char *entry_point_name
,
5623 const struct spirv_to_nir_options
*options
,
5624 const nir_shader_compiler_options
*nir_options
)
5627 const uint32_t *word_end
= words
+ word_count
;
5629 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
5630 stage
, entry_point_name
,
5636 /* See also _vtn_fail() */
5637 if (setjmp(b
->fail_jump
)) {
5642 /* Skip the SPIR-V header, handled at vtn_create_builder */
5645 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
5647 /* Handle all the preamble instructions */
5648 words
= vtn_foreach_instruction(b
, words
, word_end
,
5649 vtn_handle_preamble_instruction
);
5651 if (b
->entry_point
== NULL
) {
5652 vtn_fail("Entry point not found");
5657 /* Ensure a sane address mode is being used for function temps */
5658 assert(nir_address_format_bit_size(b
->options
->temp_addr_format
) == nir_get_ptr_bitsize(b
->shader
));
5659 assert(nir_address_format_num_components(b
->options
->temp_addr_format
) == 1);
5661 /* Set shader info defaults */
5662 if (stage
== MESA_SHADER_GEOMETRY
)
5663 b
->shader
->info
.gs
.invocations
= 1;
5665 /* Parse execution modes. */
5666 vtn_foreach_execution_mode(b
, b
->entry_point
,
5667 vtn_handle_execution_mode
, NULL
);
5669 b
->specializations
= spec
;
5670 b
->num_specializations
= num_spec
;
5672 /* Handle all variable, type, and constant instructions */
5673 words
= vtn_foreach_instruction(b
, words
, word_end
,
5674 vtn_handle_variable_or_type_instruction
);
5676 /* Parse execution modes that depend on IDs. Must happen after we have
5679 vtn_foreach_execution_mode(b
, b
->entry_point
,
5680 vtn_handle_execution_mode_id
, NULL
);
5682 if (b
->workgroup_size_builtin
) {
5683 vtn_assert(b
->workgroup_size_builtin
->type
->type
==
5684 glsl_vector_type(GLSL_TYPE_UINT
, 3));
5686 nir_const_value
*const_size
=
5687 b
->workgroup_size_builtin
->constant
->values
;
5689 b
->shader
->info
.cs
.local_size
[0] = const_size
[0].u32
;
5690 b
->shader
->info
.cs
.local_size
[1] = const_size
[1].u32
;
5691 b
->shader
->info
.cs
.local_size
[2] = const_size
[2].u32
;
5694 /* Set types on all vtn_values */
5695 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
5697 vtn_build_cfg(b
, words
, word_end
);
5699 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5700 b
->entry_point
->func
->referenced
= true;
5705 vtn_foreach_cf_node(node
, &b
->functions
) {
5706 struct vtn_function
*func
= vtn_cf_node_as_function(node
);
5707 if (func
->referenced
&& !func
->emitted
) {
5708 b
->const_table
= _mesa_pointer_hash_table_create(b
);
5710 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
5716 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5717 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
5718 vtn_assert(entry_point
);
5720 /* post process entry_points with input params */
5721 if (entry_point
->num_params
&& b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
5722 entry_point
= vtn_emit_kernel_entry_point_wrapper(b
, entry_point
);
5724 /* structurize the CFG */
5725 nir_lower_goto_ifs(b
->shader
);
5727 entry_point
->is_entrypoint
= true;
5729 /* When multiple shader stages exist in the same SPIR-V module, we
5730 * generate input and output variables for every stage, in the same
5731 * NIR program. These dead variables can be invalid NIR. For example,
5732 * TCS outputs must be per-vertex arrays (or decorated 'patch'), while
5733 * VS output variables wouldn't be.
5735 * To ensure we have valid NIR, we eliminate any dead inputs and outputs
5736 * right away. In order to do so, we must lower any constant initializers
5737 * on outputs so nir_remove_dead_variables sees that they're written to.
5739 nir_lower_variable_initializers(b
->shader
, nir_var_shader_out
);
5740 nir_remove_dead_variables(b
->shader
,
5741 nir_var_shader_in
| nir_var_shader_out
, NULL
);
5743 /* We sometimes generate bogus derefs that, while never used, give the
5744 * validator a bit of heartburn. Run dead code to get rid of them.
5746 nir_opt_dce(b
->shader
);
5748 /* Unparent the shader from the vtn_builder before we delete the builder */
5749 ralloc_steal(NULL
, b
->shader
);
5751 nir_shader
*shader
= b
->shader
;