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"
41 vtn_log(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
42 size_t spirv_offset
, const char *message
)
44 if (b
->options
->debug
.func
) {
45 b
->options
->debug
.func(b
->options
->debug
.private_data
,
46 level
, spirv_offset
, message
);
50 if (level
>= NIR_SPIRV_DEBUG_LEVEL_WARNING
)
51 fprintf(stderr
, "%s\n", message
);
56 vtn_logf(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
57 size_t spirv_offset
, const char *fmt
, ...)
63 msg
= ralloc_vasprintf(NULL
, fmt
, args
);
66 vtn_log(b
, level
, spirv_offset
, msg
);
72 vtn_log_err(struct vtn_builder
*b
,
73 enum nir_spirv_debug_level level
, const char *prefix
,
74 const char *file
, unsigned line
,
75 const char *fmt
, va_list args
)
79 msg
= ralloc_strdup(NULL
, prefix
);
82 ralloc_asprintf_append(&msg
, " In file %s:%u\n", file
, line
);
85 ralloc_asprintf_append(&msg
, " ");
87 ralloc_vasprintf_append(&msg
, fmt
, args
);
89 ralloc_asprintf_append(&msg
, "\n %zu bytes into the SPIR-V binary",
93 ralloc_asprintf_append(&msg
,
94 "\n in SPIR-V source file %s, line %d, col %d",
95 b
->file
, b
->line
, b
->col
);
98 vtn_log(b
, level
, b
->spirv_offset
, msg
);
104 vtn_dump_shader(struct vtn_builder
*b
, const char *path
, const char *prefix
)
109 int len
= snprintf(filename
, sizeof(filename
), "%s/%s-%d.spirv",
110 path
, prefix
, idx
++);
111 if (len
< 0 || len
>= sizeof(filename
))
114 FILE *f
= fopen(filename
, "w");
118 fwrite(b
->spirv
, sizeof(*b
->spirv
), b
->spirv_word_count
, f
);
121 vtn_info("SPIR-V shader dumped to %s", filename
);
125 _vtn_warn(struct vtn_builder
*b
, const char *file
, unsigned line
,
126 const char *fmt
, ...)
131 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_WARNING
, "SPIR-V WARNING:\n",
132 file
, line
, fmt
, args
);
137 _vtn_err(struct vtn_builder
*b
, const char *file
, unsigned line
,
138 const char *fmt
, ...)
143 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V ERROR:\n",
144 file
, line
, fmt
, args
);
149 _vtn_fail(struct vtn_builder
*b
, const char *file
, unsigned line
,
150 const char *fmt
, ...)
155 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V parsing FAILED:\n",
156 file
, line
, fmt
, args
);
159 const char *dump_path
= getenv("MESA_SPIRV_FAIL_DUMP_PATH");
161 vtn_dump_shader(b
, dump_path
, "fail");
163 longjmp(b
->fail_jump
, 1);
166 static struct vtn_ssa_value
*
167 vtn_undef_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
169 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
172 if (glsl_type_is_vector_or_scalar(type
)) {
173 unsigned num_components
= glsl_get_vector_elements(val
->type
);
174 unsigned bit_size
= glsl_get_bit_size(val
->type
);
175 val
->def
= nir_ssa_undef(&b
->nb
, num_components
, bit_size
);
177 unsigned elems
= glsl_get_length(val
->type
);
178 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
179 if (glsl_type_is_matrix(type
)) {
180 const struct glsl_type
*elem_type
=
181 glsl_vector_type(glsl_get_base_type(type
),
182 glsl_get_vector_elements(type
));
184 for (unsigned i
= 0; i
< elems
; i
++)
185 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
186 } else if (glsl_type_is_array(type
)) {
187 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
188 for (unsigned i
= 0; i
< elems
; i
++)
189 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
191 for (unsigned i
= 0; i
< elems
; i
++) {
192 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
193 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
201 static struct vtn_ssa_value
*
202 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
203 const struct glsl_type
*type
)
205 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
210 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
213 switch (glsl_get_base_type(type
)) {
216 case GLSL_TYPE_INT16
:
217 case GLSL_TYPE_UINT16
:
218 case GLSL_TYPE_UINT8
:
220 case GLSL_TYPE_INT64
:
221 case GLSL_TYPE_UINT64
:
223 case GLSL_TYPE_FLOAT
:
224 case GLSL_TYPE_FLOAT16
:
225 case GLSL_TYPE_DOUBLE
: {
226 int bit_size
= glsl_get_bit_size(type
);
227 if (glsl_type_is_vector_or_scalar(type
)) {
228 unsigned num_components
= glsl_get_vector_elements(val
->type
);
229 nir_load_const_instr
*load
=
230 nir_load_const_instr_create(b
->shader
, num_components
, bit_size
);
232 memcpy(load
->value
, constant
->values
,
233 sizeof(nir_const_value
) * load
->def
.num_components
);
235 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
236 val
->def
= &load
->def
;
238 assert(glsl_type_is_matrix(type
));
239 unsigned columns
= glsl_get_matrix_columns(val
->type
);
240 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, columns
);
241 const struct glsl_type
*column_type
= glsl_get_column_type(val
->type
);
242 for (unsigned i
= 0; i
< columns
; i
++)
243 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
249 case GLSL_TYPE_ARRAY
: {
250 unsigned elems
= glsl_get_length(val
->type
);
251 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
252 const struct glsl_type
*elem_type
= glsl_get_array_element(val
->type
);
253 for (unsigned i
= 0; i
< elems
; i
++)
254 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
259 case GLSL_TYPE_STRUCT
: {
260 unsigned elems
= glsl_get_length(val
->type
);
261 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
262 for (unsigned i
= 0; i
< elems
; i
++) {
263 const struct glsl_type
*elem_type
=
264 glsl_get_struct_field(val
->type
, i
);
265 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
272 vtn_fail("bad constant type");
278 struct vtn_ssa_value
*
279 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
281 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
282 switch (val
->value_type
) {
283 case vtn_value_type_undef
:
284 return vtn_undef_ssa_value(b
, val
->type
->type
);
286 case vtn_value_type_constant
:
287 return vtn_const_ssa_value(b
, val
->constant
, val
->type
->type
);
289 case vtn_value_type_ssa
:
292 case vtn_value_type_pointer
:
293 vtn_assert(val
->pointer
->ptr_type
&& val
->pointer
->ptr_type
->type
);
294 struct vtn_ssa_value
*ssa
=
295 vtn_create_ssa_value(b
, val
->pointer
->ptr_type
->type
);
296 ssa
->def
= vtn_pointer_to_ssa(b
, val
->pointer
);
300 vtn_fail("Invalid type for an SSA value");
305 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
306 unsigned word_count
, unsigned *words_used
)
308 char *dup
= ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
310 /* Ammount of space taken by the string (including the null) */
311 unsigned len
= strlen(dup
) + 1;
312 *words_used
= DIV_ROUND_UP(len
, sizeof(*words
));
318 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
319 const uint32_t *end
, vtn_instruction_handler handler
)
325 const uint32_t *w
= start
;
327 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
328 unsigned count
= w
[0] >> SpvWordCountShift
;
329 vtn_assert(count
>= 1 && w
+ count
<= end
);
331 b
->spirv_offset
= (uint8_t *)w
- (uint8_t *)b
->spirv
;
335 break; /* Do nothing */
338 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
350 if (!handler(b
, opcode
, w
, count
))
368 vtn_handle_non_semantic_instruction(struct vtn_builder
*b
, SpvOp ext_opcode
,
369 const uint32_t *w
, unsigned count
)
376 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
377 const uint32_t *w
, unsigned count
)
379 const char *ext
= (const char *)&w
[2];
381 case SpvOpExtInstImport
: {
382 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
383 if (strcmp(ext
, "GLSL.std.450") == 0) {
384 val
->ext_handler
= vtn_handle_glsl450_instruction
;
385 } else if ((strcmp(ext
, "SPV_AMD_gcn_shader") == 0)
386 && (b
->options
&& b
->options
->caps
.amd_gcn_shader
)) {
387 val
->ext_handler
= vtn_handle_amd_gcn_shader_instruction
;
388 } else if ((strcmp(ext
, "SPV_AMD_shader_ballot") == 0)
389 && (b
->options
&& b
->options
->caps
.amd_shader_ballot
)) {
390 val
->ext_handler
= vtn_handle_amd_shader_ballot_instruction
;
391 } else if ((strcmp(ext
, "SPV_AMD_shader_trinary_minmax") == 0)
392 && (b
->options
&& b
->options
->caps
.amd_trinary_minmax
)) {
393 val
->ext_handler
= vtn_handle_amd_shader_trinary_minmax_instruction
;
394 } else if ((strcmp(ext
, "SPV_AMD_shader_explicit_vertex_parameter") == 0)
395 && (b
->options
&& b
->options
->caps
.amd_shader_explicit_vertex_parameter
)) {
396 val
->ext_handler
= vtn_handle_amd_shader_explicit_vertex_parameter_instruction
;
397 } else if (strcmp(ext
, "OpenCL.std") == 0) {
398 val
->ext_handler
= vtn_handle_opencl_instruction
;
399 } else if (strstr(ext
, "NonSemantic.") == ext
) {
400 val
->ext_handler
= vtn_handle_non_semantic_instruction
;
402 vtn_fail("Unsupported extension: %s", ext
);
408 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
409 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
415 vtn_fail_with_opcode("Unhandled opcode", opcode
);
420 _foreach_decoration_helper(struct vtn_builder
*b
,
421 struct vtn_value
*base_value
,
423 struct vtn_value
*value
,
424 vtn_decoration_foreach_cb cb
, void *data
)
426 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
428 if (dec
->scope
== VTN_DEC_DECORATION
) {
429 member
= parent_member
;
430 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
431 vtn_fail_if(value
->value_type
!= vtn_value_type_type
||
432 value
->type
->base_type
!= vtn_base_type_struct
,
433 "OpMemberDecorate and OpGroupMemberDecorate are only "
434 "allowed on OpTypeStruct");
435 /* This means we haven't recursed yet */
436 assert(value
== base_value
);
438 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
440 vtn_fail_if(member
>= base_value
->type
->length
,
441 "OpMemberDecorate specifies member %d but the "
442 "OpTypeStruct has only %u members",
443 member
, base_value
->type
->length
);
445 /* Not a decoration */
446 assert(dec
->scope
== VTN_DEC_EXECUTION_MODE
);
451 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
452 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
455 cb(b
, base_value
, member
, dec
, data
);
460 /** Iterates (recursively if needed) over all of the decorations on a value
462 * This function iterates over all of the decorations applied to a given
463 * value. If it encounters a decoration group, it recurses into the group
464 * and iterates over all of those decorations as well.
467 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
468 vtn_decoration_foreach_cb cb
, void *data
)
470 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
474 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
475 vtn_execution_mode_foreach_cb cb
, void *data
)
477 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
478 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
481 assert(dec
->group
== NULL
);
482 cb(b
, value
, dec
, data
);
487 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
488 const uint32_t *w
, unsigned count
)
490 const uint32_t *w_end
= w
+ count
;
491 const uint32_t target
= w
[1];
495 case SpvOpDecorationGroup
:
496 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
500 case SpvOpDecorateId
:
501 case SpvOpMemberDecorate
:
502 case SpvOpDecorateString
:
503 case SpvOpMemberDecorateString
:
504 case SpvOpExecutionMode
:
505 case SpvOpExecutionModeId
: {
506 struct vtn_value
*val
= vtn_untyped_value(b
, target
);
508 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
511 case SpvOpDecorateId
:
512 case SpvOpDecorateString
:
513 dec
->scope
= VTN_DEC_DECORATION
;
515 case SpvOpMemberDecorate
:
516 case SpvOpMemberDecorateString
:
517 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
518 vtn_fail_if(dec
->scope
< VTN_DEC_STRUCT_MEMBER0
, /* overflow */
519 "Member argument of OpMemberDecorate too large");
521 case SpvOpExecutionMode
:
522 case SpvOpExecutionModeId
:
523 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
526 unreachable("Invalid decoration opcode");
528 dec
->decoration
= *(w
++);
531 /* Link into the list */
532 dec
->next
= val
->decoration
;
533 val
->decoration
= dec
;
537 case SpvOpGroupMemberDecorate
:
538 case SpvOpGroupDecorate
: {
539 struct vtn_value
*group
=
540 vtn_value(b
, target
, vtn_value_type_decoration_group
);
542 for (; w
< w_end
; w
++) {
543 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
544 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
547 if (opcode
== SpvOpGroupDecorate
) {
548 dec
->scope
= VTN_DEC_DECORATION
;
550 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
551 vtn_fail_if(dec
->scope
< 0, /* Check for overflow */
552 "Member argument of OpGroupMemberDecorate too large");
555 /* Link into the list */
556 dec
->next
= val
->decoration
;
557 val
->decoration
= dec
;
563 unreachable("Unhandled opcode");
567 struct member_decoration_ctx
{
569 struct glsl_struct_field
*fields
;
570 struct vtn_type
*type
;
574 * Returns true if the given type contains a struct decorated Block or
578 vtn_type_contains_block(struct vtn_builder
*b
, struct vtn_type
*type
)
580 switch (type
->base_type
) {
581 case vtn_base_type_array
:
582 return vtn_type_contains_block(b
, type
->array_element
);
583 case vtn_base_type_struct
:
584 if (type
->block
|| type
->buffer_block
)
586 for (unsigned i
= 0; i
< type
->length
; i
++) {
587 if (vtn_type_contains_block(b
, type
->members
[i
]))
596 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
597 * OpStore, or OpCopyMemory between them without breaking anything.
598 * Technically, the SPIR-V rules require the exact same type ID but this lets
599 * us internally be a bit looser.
602 vtn_types_compatible(struct vtn_builder
*b
,
603 struct vtn_type
*t1
, struct vtn_type
*t2
)
605 if (t1
->id
== t2
->id
)
608 if (t1
->base_type
!= t2
->base_type
)
611 switch (t1
->base_type
) {
612 case vtn_base_type_void
:
613 case vtn_base_type_scalar
:
614 case vtn_base_type_vector
:
615 case vtn_base_type_matrix
:
616 case vtn_base_type_image
:
617 case vtn_base_type_sampler
:
618 case vtn_base_type_sampled_image
:
619 return t1
->type
== t2
->type
;
621 case vtn_base_type_array
:
622 return t1
->length
== t2
->length
&&
623 vtn_types_compatible(b
, t1
->array_element
, t2
->array_element
);
625 case vtn_base_type_pointer
:
626 return vtn_types_compatible(b
, t1
->deref
, t2
->deref
);
628 case vtn_base_type_struct
:
629 if (t1
->length
!= t2
->length
)
632 for (unsigned i
= 0; i
< t1
->length
; i
++) {
633 if (!vtn_types_compatible(b
, t1
->members
[i
], t2
->members
[i
]))
638 case vtn_base_type_function
:
639 /* This case shouldn't get hit since you can't copy around function
640 * types. Just require them to be identical.
645 vtn_fail("Invalid base type");
649 vtn_type_without_array(struct vtn_type
*type
)
651 while (type
->base_type
== vtn_base_type_array
)
652 type
= type
->array_element
;
656 /* does a shallow copy of a vtn_type */
658 static struct vtn_type
*
659 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
661 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
664 switch (src
->base_type
) {
665 case vtn_base_type_void
:
666 case vtn_base_type_scalar
:
667 case vtn_base_type_vector
:
668 case vtn_base_type_matrix
:
669 case vtn_base_type_array
:
670 case vtn_base_type_pointer
:
671 case vtn_base_type_image
:
672 case vtn_base_type_sampler
:
673 case vtn_base_type_sampled_image
:
674 /* Nothing more to do */
677 case vtn_base_type_struct
:
678 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
679 memcpy(dest
->members
, src
->members
,
680 src
->length
* sizeof(src
->members
[0]));
682 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
683 memcpy(dest
->offsets
, src
->offsets
,
684 src
->length
* sizeof(src
->offsets
[0]));
687 case vtn_base_type_function
:
688 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
689 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
696 static struct vtn_type
*
697 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
699 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
700 type
= type
->members
[member
];
702 /* We may have an array of matrices.... Oh, joy! */
703 while (glsl_type_is_array(type
->type
)) {
704 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
705 type
= type
->array_element
;
708 vtn_assert(glsl_type_is_matrix(type
->type
));
714 vtn_handle_access_qualifier(struct vtn_builder
*b
, struct vtn_type
*type
,
715 int member
, enum gl_access_qualifier access
)
717 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
718 type
= type
->members
[member
];
720 type
->access
|= access
;
724 array_stride_decoration_cb(struct vtn_builder
*b
,
725 struct vtn_value
*val
, int member
,
726 const struct vtn_decoration
*dec
, void *void_ctx
)
728 struct vtn_type
*type
= val
->type
;
730 if (dec
->decoration
== SpvDecorationArrayStride
) {
731 if (vtn_type_contains_block(b
, type
)) {
732 vtn_warn("The ArrayStride decoration cannot be applied to an array "
733 "type which contains a structure type decorated Block "
735 /* Ignore the decoration */
737 vtn_fail_if(dec
->operands
[0] == 0, "ArrayStride must be non-zero");
738 type
->stride
= dec
->operands
[0];
744 struct_member_decoration_cb(struct vtn_builder
*b
,
745 UNUSED
struct vtn_value
*val
, int member
,
746 const struct vtn_decoration
*dec
, void *void_ctx
)
748 struct member_decoration_ctx
*ctx
= void_ctx
;
753 assert(member
< ctx
->num_fields
);
755 switch (dec
->decoration
) {
756 case SpvDecorationRelaxedPrecision
:
757 case SpvDecorationUniform
:
758 case SpvDecorationUniformId
:
759 break; /* FIXME: Do nothing with this for now. */
760 case SpvDecorationNonWritable
:
761 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_WRITEABLE
);
763 case SpvDecorationNonReadable
:
764 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_READABLE
);
766 case SpvDecorationVolatile
:
767 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_VOLATILE
);
769 case SpvDecorationCoherent
:
770 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_COHERENT
);
772 case SpvDecorationNoPerspective
:
773 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
775 case SpvDecorationFlat
:
776 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
778 case SpvDecorationExplicitInterpAMD
:
779 ctx
->fields
[member
].interpolation
= INTERP_MODE_EXPLICIT
;
781 case SpvDecorationCentroid
:
782 ctx
->fields
[member
].centroid
= true;
784 case SpvDecorationSample
:
785 ctx
->fields
[member
].sample
= true;
787 case SpvDecorationStream
:
788 /* This is handled later by var_decoration_cb in vtn_variables.c */
790 case SpvDecorationLocation
:
791 ctx
->fields
[member
].location
= dec
->operands
[0];
793 case SpvDecorationComponent
:
794 break; /* FIXME: What should we do with these? */
795 case SpvDecorationBuiltIn
:
796 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
797 ctx
->type
->members
[member
]->is_builtin
= true;
798 ctx
->type
->members
[member
]->builtin
= dec
->operands
[0];
799 ctx
->type
->builtin_block
= true;
801 case SpvDecorationOffset
:
802 ctx
->type
->offsets
[member
] = dec
->operands
[0];
803 ctx
->fields
[member
].offset
= dec
->operands
[0];
805 case SpvDecorationMatrixStride
:
806 /* Handled as a second pass */
808 case SpvDecorationColMajor
:
809 break; /* Nothing to do here. Column-major is the default. */
810 case SpvDecorationRowMajor
:
811 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
814 case SpvDecorationPatch
:
817 case SpvDecorationSpecId
:
818 case SpvDecorationBlock
:
819 case SpvDecorationBufferBlock
:
820 case SpvDecorationArrayStride
:
821 case SpvDecorationGLSLShared
:
822 case SpvDecorationGLSLPacked
:
823 case SpvDecorationInvariant
:
824 case SpvDecorationRestrict
:
825 case SpvDecorationAliased
:
826 case SpvDecorationConstant
:
827 case SpvDecorationIndex
:
828 case SpvDecorationBinding
:
829 case SpvDecorationDescriptorSet
:
830 case SpvDecorationLinkageAttributes
:
831 case SpvDecorationNoContraction
:
832 case SpvDecorationInputAttachmentIndex
:
833 vtn_warn("Decoration not allowed on struct members: %s",
834 spirv_decoration_to_string(dec
->decoration
));
837 case SpvDecorationXfbBuffer
:
838 case SpvDecorationXfbStride
:
839 /* This is handled later by var_decoration_cb in vtn_variables.c */
842 case SpvDecorationCPacked
:
843 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
844 vtn_warn("Decoration only allowed for CL-style kernels: %s",
845 spirv_decoration_to_string(dec
->decoration
));
847 ctx
->type
->packed
= true;
850 case SpvDecorationSaturatedConversion
:
851 case SpvDecorationFuncParamAttr
:
852 case SpvDecorationFPRoundingMode
:
853 case SpvDecorationFPFastMathMode
:
854 case SpvDecorationAlignment
:
855 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
856 vtn_warn("Decoration only allowed for CL-style kernels: %s",
857 spirv_decoration_to_string(dec
->decoration
));
861 case SpvDecorationUserSemantic
:
862 case SpvDecorationUserTypeGOOGLE
:
863 /* User semantic decorations can safely be ignored by the driver. */
867 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
871 /** Chases the array type all the way down to the tail and rewrites the
872 * glsl_types to be based off the tail's glsl_type.
875 vtn_array_type_rewrite_glsl_type(struct vtn_type
*type
)
877 if (type
->base_type
!= vtn_base_type_array
)
880 vtn_array_type_rewrite_glsl_type(type
->array_element
);
882 type
->type
= glsl_array_type(type
->array_element
->type
,
883 type
->length
, type
->stride
);
886 /* Matrix strides are handled as a separate pass because we need to know
887 * whether the matrix is row-major or not first.
890 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
891 UNUSED
struct vtn_value
*val
, int member
,
892 const struct vtn_decoration
*dec
,
895 if (dec
->decoration
!= SpvDecorationMatrixStride
)
898 vtn_fail_if(member
< 0,
899 "The MatrixStride decoration is only allowed on members "
901 vtn_fail_if(dec
->operands
[0] == 0, "MatrixStride must be non-zero");
903 struct member_decoration_ctx
*ctx
= void_ctx
;
905 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
906 if (mat_type
->row_major
) {
907 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
908 mat_type
->stride
= mat_type
->array_element
->stride
;
909 mat_type
->array_element
->stride
= dec
->operands
[0];
911 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
912 dec
->operands
[0], true);
913 mat_type
->array_element
->type
= glsl_get_column_type(mat_type
->type
);
915 vtn_assert(mat_type
->array_element
->stride
> 0);
916 mat_type
->stride
= dec
->operands
[0];
918 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
919 dec
->operands
[0], false);
922 /* Now that we've replaced the glsl_type with a properly strided matrix
923 * type, rewrite the member type so that it's an array of the proper kind
926 vtn_array_type_rewrite_glsl_type(ctx
->type
->members
[member
]);
927 ctx
->fields
[member
].type
= ctx
->type
->members
[member
]->type
;
931 struct_block_decoration_cb(struct vtn_builder
*b
,
932 struct vtn_value
*val
, int member
,
933 const struct vtn_decoration
*dec
, void *ctx
)
938 struct vtn_type
*type
= val
->type
;
939 if (dec
->decoration
== SpvDecorationBlock
)
941 else if (dec
->decoration
== SpvDecorationBufferBlock
)
942 type
->buffer_block
= true;
946 type_decoration_cb(struct vtn_builder
*b
,
947 struct vtn_value
*val
, int member
,
948 const struct vtn_decoration
*dec
, UNUSED
void *ctx
)
950 struct vtn_type
*type
= val
->type
;
953 /* This should have been handled by OpTypeStruct */
954 assert(val
->type
->base_type
== vtn_base_type_struct
);
955 assert(member
>= 0 && member
< val
->type
->length
);
959 switch (dec
->decoration
) {
960 case SpvDecorationArrayStride
:
961 vtn_assert(type
->base_type
== vtn_base_type_array
||
962 type
->base_type
== vtn_base_type_pointer
);
964 case SpvDecorationBlock
:
965 vtn_assert(type
->base_type
== vtn_base_type_struct
);
966 vtn_assert(type
->block
);
968 case SpvDecorationBufferBlock
:
969 vtn_assert(type
->base_type
== vtn_base_type_struct
);
970 vtn_assert(type
->buffer_block
);
972 case SpvDecorationGLSLShared
:
973 case SpvDecorationGLSLPacked
:
974 /* Ignore these, since we get explicit offsets anyways */
977 case SpvDecorationRowMajor
:
978 case SpvDecorationColMajor
:
979 case SpvDecorationMatrixStride
:
980 case SpvDecorationBuiltIn
:
981 case SpvDecorationNoPerspective
:
982 case SpvDecorationFlat
:
983 case SpvDecorationPatch
:
984 case SpvDecorationCentroid
:
985 case SpvDecorationSample
:
986 case SpvDecorationExplicitInterpAMD
:
987 case SpvDecorationVolatile
:
988 case SpvDecorationCoherent
:
989 case SpvDecorationNonWritable
:
990 case SpvDecorationNonReadable
:
991 case SpvDecorationUniform
:
992 case SpvDecorationUniformId
:
993 case SpvDecorationLocation
:
994 case SpvDecorationComponent
:
995 case SpvDecorationOffset
:
996 case SpvDecorationXfbBuffer
:
997 case SpvDecorationXfbStride
:
998 case SpvDecorationUserSemantic
:
999 vtn_warn("Decoration only allowed for struct members: %s",
1000 spirv_decoration_to_string(dec
->decoration
));
1003 case SpvDecorationStream
:
1004 /* We don't need to do anything here, as stream is filled up when
1005 * aplying the decoration to a variable, just check that if it is not a
1006 * struct member, it should be a struct.
1008 vtn_assert(type
->base_type
== vtn_base_type_struct
);
1011 case SpvDecorationRelaxedPrecision
:
1012 case SpvDecorationSpecId
:
1013 case SpvDecorationInvariant
:
1014 case SpvDecorationRestrict
:
1015 case SpvDecorationAliased
:
1016 case SpvDecorationConstant
:
1017 case SpvDecorationIndex
:
1018 case SpvDecorationBinding
:
1019 case SpvDecorationDescriptorSet
:
1020 case SpvDecorationLinkageAttributes
:
1021 case SpvDecorationNoContraction
:
1022 case SpvDecorationInputAttachmentIndex
:
1023 vtn_warn("Decoration not allowed on types: %s",
1024 spirv_decoration_to_string(dec
->decoration
));
1027 case SpvDecorationCPacked
:
1028 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
1029 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1030 spirv_decoration_to_string(dec
->decoration
));
1032 type
->packed
= true;
1035 case SpvDecorationSaturatedConversion
:
1036 case SpvDecorationFuncParamAttr
:
1037 case SpvDecorationFPRoundingMode
:
1038 case SpvDecorationFPFastMathMode
:
1039 case SpvDecorationAlignment
:
1040 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1041 spirv_decoration_to_string(dec
->decoration
));
1044 case SpvDecorationUserTypeGOOGLE
:
1045 /* User semantic decorations can safely be ignored by the driver. */
1049 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1054 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
1057 case SpvImageFormatUnknown
: return PIPE_FORMAT_NONE
;
1058 case SpvImageFormatRgba32f
: return PIPE_FORMAT_R32G32B32A32_FLOAT
;
1059 case SpvImageFormatRgba16f
: return PIPE_FORMAT_R16G16B16A16_FLOAT
;
1060 case SpvImageFormatR32f
: return PIPE_FORMAT_R32_FLOAT
;
1061 case SpvImageFormatRgba8
: return PIPE_FORMAT_R8G8B8A8_UNORM
;
1062 case SpvImageFormatRgba8Snorm
: return PIPE_FORMAT_R8G8B8A8_SNORM
;
1063 case SpvImageFormatRg32f
: return PIPE_FORMAT_R32G32_FLOAT
;
1064 case SpvImageFormatRg16f
: return PIPE_FORMAT_R16G16_FLOAT
;
1065 case SpvImageFormatR11fG11fB10f
: return PIPE_FORMAT_R11G11B10_FLOAT
;
1066 case SpvImageFormatR16f
: return PIPE_FORMAT_R16_FLOAT
;
1067 case SpvImageFormatRgba16
: return PIPE_FORMAT_R16G16B16A16_UNORM
;
1068 case SpvImageFormatRgb10A2
: return PIPE_FORMAT_R10G10B10A2_UNORM
;
1069 case SpvImageFormatRg16
: return PIPE_FORMAT_R16G16_UNORM
;
1070 case SpvImageFormatRg8
: return PIPE_FORMAT_R8G8_UNORM
;
1071 case SpvImageFormatR16
: return PIPE_FORMAT_R16_UNORM
;
1072 case SpvImageFormatR8
: return PIPE_FORMAT_R8_UNORM
;
1073 case SpvImageFormatRgba16Snorm
: return PIPE_FORMAT_R16G16B16A16_SNORM
;
1074 case SpvImageFormatRg16Snorm
: return PIPE_FORMAT_R16G16_SNORM
;
1075 case SpvImageFormatRg8Snorm
: return PIPE_FORMAT_R8G8_SNORM
;
1076 case SpvImageFormatR16Snorm
: return PIPE_FORMAT_R16_SNORM
;
1077 case SpvImageFormatR8Snorm
: return PIPE_FORMAT_R8_SNORM
;
1078 case SpvImageFormatRgba32i
: return PIPE_FORMAT_R32G32B32A32_SINT
;
1079 case SpvImageFormatRgba16i
: return PIPE_FORMAT_R16G16B16A16_SINT
;
1080 case SpvImageFormatRgba8i
: return PIPE_FORMAT_R8G8B8A8_SINT
;
1081 case SpvImageFormatR32i
: return PIPE_FORMAT_R32_SINT
;
1082 case SpvImageFormatRg32i
: return PIPE_FORMAT_R32G32_SINT
;
1083 case SpvImageFormatRg16i
: return PIPE_FORMAT_R16G16_SINT
;
1084 case SpvImageFormatRg8i
: return PIPE_FORMAT_R8G8_SINT
;
1085 case SpvImageFormatR16i
: return PIPE_FORMAT_R16_SINT
;
1086 case SpvImageFormatR8i
: return PIPE_FORMAT_R8_SINT
;
1087 case SpvImageFormatRgba32ui
: return PIPE_FORMAT_R32G32B32A32_UINT
;
1088 case SpvImageFormatRgba16ui
: return PIPE_FORMAT_R16G16B16A16_UINT
;
1089 case SpvImageFormatRgba8ui
: return PIPE_FORMAT_R8G8B8A8_UINT
;
1090 case SpvImageFormatR32ui
: return PIPE_FORMAT_R32_UINT
;
1091 case SpvImageFormatRgb10a2ui
: return PIPE_FORMAT_R10G10B10A2_UINT
;
1092 case SpvImageFormatRg32ui
: return PIPE_FORMAT_R32G32_UINT
;
1093 case SpvImageFormatRg16ui
: return PIPE_FORMAT_R16G16_UINT
;
1094 case SpvImageFormatRg8ui
: return PIPE_FORMAT_R8G8_UINT
;
1095 case SpvImageFormatR16ui
: return PIPE_FORMAT_R16_UINT
;
1096 case SpvImageFormatR8ui
: return PIPE_FORMAT_R8_UINT
;
1098 vtn_fail("Invalid image format: %s (%u)",
1099 spirv_imageformat_to_string(format
), format
);
1104 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
1105 const uint32_t *w
, unsigned count
)
1107 struct vtn_value
*val
= NULL
;
1109 /* In order to properly handle forward declarations, we have to defer
1110 * allocation for pointer types.
1112 if (opcode
!= SpvOpTypePointer
&& opcode
!= SpvOpTypeForwardPointer
) {
1113 val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1114 vtn_fail_if(val
->type
!= NULL
,
1115 "Only pointers can have forward declarations");
1116 val
->type
= rzalloc(b
, struct vtn_type
);
1117 val
->type
->id
= w
[1];
1122 val
->type
->base_type
= vtn_base_type_void
;
1123 val
->type
->type
= glsl_void_type();
1126 val
->type
->base_type
= vtn_base_type_scalar
;
1127 val
->type
->type
= glsl_bool_type();
1128 val
->type
->length
= 1;
1130 case SpvOpTypeInt
: {
1131 int bit_size
= w
[2];
1132 const bool signedness
= w
[3];
1133 val
->type
->base_type
= vtn_base_type_scalar
;
1136 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1139 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1142 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1145 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1148 vtn_fail("Invalid int bit size: %u", bit_size
);
1150 val
->type
->length
= 1;
1154 case SpvOpTypeFloat
: {
1155 int bit_size
= w
[2];
1156 val
->type
->base_type
= vtn_base_type_scalar
;
1159 val
->type
->type
= glsl_float16_t_type();
1162 val
->type
->type
= glsl_float_type();
1165 val
->type
->type
= glsl_double_type();
1168 vtn_fail("Invalid float bit size: %u", bit_size
);
1170 val
->type
->length
= 1;
1174 case SpvOpTypeVector
: {
1175 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1176 unsigned elems
= w
[3];
1178 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1179 "Base type for OpTypeVector must be a scalar");
1180 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1181 "Invalid component count for OpTypeVector");
1183 val
->type
->base_type
= vtn_base_type_vector
;
1184 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1185 val
->type
->length
= elems
;
1186 val
->type
->stride
= glsl_type_is_boolean(val
->type
->type
)
1187 ? 4 : glsl_get_bit_size(base
->type
) / 8;
1188 val
->type
->array_element
= base
;
1192 case SpvOpTypeMatrix
: {
1193 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1194 unsigned columns
= w
[3];
1196 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1197 "Base type for OpTypeMatrix must be a vector");
1198 vtn_fail_if(columns
< 2 || columns
> 4,
1199 "Invalid column count for OpTypeMatrix");
1201 val
->type
->base_type
= vtn_base_type_matrix
;
1202 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1203 glsl_get_vector_elements(base
->type
),
1205 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1206 "Unsupported base type for OpTypeMatrix");
1207 assert(!glsl_type_is_error(val
->type
->type
));
1208 val
->type
->length
= columns
;
1209 val
->type
->array_element
= base
;
1210 val
->type
->row_major
= false;
1211 val
->type
->stride
= 0;
1215 case SpvOpTypeRuntimeArray
:
1216 case SpvOpTypeArray
: {
1217 struct vtn_type
*array_element
=
1218 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1220 if (opcode
== SpvOpTypeRuntimeArray
) {
1221 /* A length of 0 is used to denote unsized arrays */
1222 val
->type
->length
= 0;
1224 val
->type
->length
= vtn_constant_uint(b
, w
[3]);
1227 val
->type
->base_type
= vtn_base_type_array
;
1228 val
->type
->array_element
= array_element
;
1229 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
1230 val
->type
->stride
= glsl_get_cl_size(array_element
->type
);
1232 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1233 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
,
1238 case SpvOpTypeStruct
: {
1239 unsigned num_fields
= count
- 2;
1240 val
->type
->base_type
= vtn_base_type_struct
;
1241 val
->type
->length
= num_fields
;
1242 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1243 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1244 val
->type
->packed
= false;
1246 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1247 for (unsigned i
= 0; i
< num_fields
; i
++) {
1248 val
->type
->members
[i
] =
1249 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
1250 fields
[i
] = (struct glsl_struct_field
) {
1251 .type
= val
->type
->members
[i
]->type
,
1252 .name
= ralloc_asprintf(b
, "field%d", i
),
1258 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1259 unsigned offset
= 0;
1260 for (unsigned i
= 0; i
< num_fields
; i
++) {
1261 offset
= align(offset
, glsl_get_cl_alignment(fields
[i
].type
));
1262 fields
[i
].offset
= offset
;
1263 offset
+= glsl_get_cl_size(fields
[i
].type
);
1267 struct member_decoration_ctx ctx
= {
1268 .num_fields
= num_fields
,
1273 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1274 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1276 vtn_foreach_decoration(b
, val
, struct_block_decoration_cb
, NULL
);
1278 const char *name
= val
->name
;
1280 if (val
->type
->block
|| val
->type
->buffer_block
) {
1281 /* Packing will be ignored since types coming from SPIR-V are
1282 * explicitly laid out.
1284 val
->type
->type
= glsl_interface_type(fields
, num_fields
,
1285 /* packing */ 0, false,
1286 name
? name
: "block");
1288 val
->type
->type
= glsl_struct_type(fields
, num_fields
,
1289 name
? name
: "struct", false);
1294 case SpvOpTypeFunction
: {
1295 val
->type
->base_type
= vtn_base_type_function
;
1296 val
->type
->type
= NULL
;
1298 val
->type
->return_type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1300 const unsigned num_params
= count
- 3;
1301 val
->type
->length
= num_params
;
1302 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1303 for (unsigned i
= 0; i
< count
- 3; i
++) {
1304 val
->type
->params
[i
] =
1305 vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
;
1310 case SpvOpTypePointer
:
1311 case SpvOpTypeForwardPointer
: {
1312 /* We can't blindly push the value because it might be a forward
1315 val
= vtn_untyped_value(b
, w
[1]);
1317 SpvStorageClass storage_class
= w
[2];
1319 if (val
->value_type
== vtn_value_type_invalid
) {
1320 val
->value_type
= vtn_value_type_type
;
1321 val
->type
= rzalloc(b
, struct vtn_type
);
1322 val
->type
->id
= w
[1];
1323 val
->type
->base_type
= vtn_base_type_pointer
;
1324 val
->type
->storage_class
= storage_class
;
1326 /* These can actually be stored to nir_variables and used as SSA
1327 * values so they need a real glsl_type.
1329 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1330 b
, storage_class
, NULL
, NULL
);
1331 val
->type
->type
= nir_address_format_to_glsl_type(
1332 vtn_mode_to_address_format(b
, mode
));
1334 vtn_fail_if(val
->type
->storage_class
!= storage_class
,
1335 "The storage classes of an OpTypePointer and any "
1336 "OpTypeForwardPointers that provide forward "
1337 "declarations of it must match.");
1340 if (opcode
== SpvOpTypePointer
) {
1341 vtn_fail_if(val
->type
->deref
!= NULL
,
1342 "While OpTypeForwardPointer can be used to provide a "
1343 "forward declaration of a pointer, OpTypePointer can "
1344 "only be used once for a given id.");
1346 val
->type
->deref
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
1348 /* Only certain storage classes use ArrayStride. The others (in
1349 * particular Workgroup) are expected to be laid out by the driver.
1351 switch (storage_class
) {
1352 case SpvStorageClassUniform
:
1353 case SpvStorageClassPushConstant
:
1354 case SpvStorageClassStorageBuffer
:
1355 case SpvStorageClassPhysicalStorageBuffer
:
1356 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1359 /* Nothing to do. */
1363 if (b
->physical_ptrs
) {
1364 switch (storage_class
) {
1365 case SpvStorageClassFunction
:
1366 case SpvStorageClassWorkgroup
:
1367 case SpvStorageClassCrossWorkgroup
:
1368 case SpvStorageClassUniformConstant
:
1369 val
->type
->stride
= align(glsl_get_cl_size(val
->type
->deref
->type
),
1370 glsl_get_cl_alignment(val
->type
->deref
->type
));
1380 case SpvOpTypeImage
: {
1381 val
->type
->base_type
= vtn_base_type_image
;
1383 const struct vtn_type
*sampled_type
=
1384 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1386 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1387 glsl_get_bit_size(sampled_type
->type
) != 32,
1388 "Sampled type of OpTypeImage must be a 32-bit scalar");
1390 enum glsl_sampler_dim dim
;
1391 switch ((SpvDim
)w
[3]) {
1392 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1393 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1394 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1395 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1396 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1397 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1398 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1400 vtn_fail("Invalid SPIR-V image dimensionality: %s (%u)",
1401 spirv_dim_to_string((SpvDim
)w
[3]), w
[3]);
1404 /* w[4]: as per Vulkan spec "Validation Rules within a Module",
1405 * The “Depth” operand of OpTypeImage is ignored.
1407 bool is_array
= w
[5];
1408 bool multisampled
= w
[6];
1409 unsigned sampled
= w
[7];
1410 SpvImageFormat format
= w
[8];
1413 val
->type
->access_qualifier
= w
[9];
1415 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1418 if (dim
== GLSL_SAMPLER_DIM_2D
)
1419 dim
= GLSL_SAMPLER_DIM_MS
;
1420 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1421 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1423 vtn_fail("Unsupported multisampled image type");
1426 val
->type
->image_format
= translate_image_format(b
, format
);
1428 enum glsl_base_type sampled_base_type
=
1429 glsl_get_base_type(sampled_type
->type
);
1431 val
->type
->sampled
= true;
1432 val
->type
->type
= glsl_sampler_type(dim
, false, is_array
,
1434 } else if (sampled
== 2) {
1435 val
->type
->sampled
= false;
1436 val
->type
->type
= glsl_image_type(dim
, is_array
, sampled_base_type
);
1438 vtn_fail("We need to know if the image will be sampled");
1443 case SpvOpTypeSampledImage
:
1444 val
->type
->base_type
= vtn_base_type_sampled_image
;
1445 val
->type
->image
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1446 val
->type
->type
= val
->type
->image
->type
;
1449 case SpvOpTypeSampler
:
1450 /* The actual sampler type here doesn't really matter. It gets
1451 * thrown away the moment you combine it with an image. What really
1452 * matters is that it's a sampler type as opposed to an integer type
1453 * so the backend knows what to do.
1455 val
->type
->base_type
= vtn_base_type_sampler
;
1456 val
->type
->type
= glsl_bare_sampler_type();
1459 case SpvOpTypeOpaque
:
1460 case SpvOpTypeEvent
:
1461 case SpvOpTypeDeviceEvent
:
1462 case SpvOpTypeReserveId
:
1463 case SpvOpTypeQueue
:
1466 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1469 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1471 if (val
->type
->base_type
== vtn_base_type_struct
&&
1472 (val
->type
->block
|| val
->type
->buffer_block
)) {
1473 for (unsigned i
= 0; i
< val
->type
->length
; i
++) {
1474 vtn_fail_if(vtn_type_contains_block(b
, val
->type
->members
[i
]),
1475 "Block and BufferBlock decorations cannot decorate a "
1476 "structure type that is nested at any level inside "
1477 "another structure type decorated with Block or "
1483 static nir_constant
*
1484 vtn_null_constant(struct vtn_builder
*b
, struct vtn_type
*type
)
1486 nir_constant
*c
= rzalloc(b
, nir_constant
);
1488 switch (type
->base_type
) {
1489 case vtn_base_type_scalar
:
1490 case vtn_base_type_vector
:
1491 /* Nothing to do here. It's already initialized to zero */
1494 case vtn_base_type_pointer
: {
1495 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1496 b
, type
->storage_class
, type
->deref
, NULL
);
1497 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
1499 const nir_const_value
*null_value
= nir_address_format_null_value(addr_format
);
1500 memcpy(c
->values
, null_value
,
1501 sizeof(nir_const_value
) * nir_address_format_num_components(addr_format
));
1505 case vtn_base_type_void
:
1506 case vtn_base_type_image
:
1507 case vtn_base_type_sampler
:
1508 case vtn_base_type_sampled_image
:
1509 case vtn_base_type_function
:
1510 /* For those we have to return something but it doesn't matter what. */
1513 case vtn_base_type_matrix
:
1514 case vtn_base_type_array
:
1515 vtn_assert(type
->length
> 0);
1516 c
->num_elements
= type
->length
;
1517 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1519 c
->elements
[0] = vtn_null_constant(b
, type
->array_element
);
1520 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1521 c
->elements
[i
] = c
->elements
[0];
1524 case vtn_base_type_struct
:
1525 c
->num_elements
= type
->length
;
1526 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1527 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
1528 c
->elements
[i
] = vtn_null_constant(b
, type
->members
[i
]);
1532 vtn_fail("Invalid type for null constant");
1539 spec_constant_decoration_cb(struct vtn_builder
*b
, UNUSED
struct vtn_value
*val
,
1540 ASSERTED
int member
,
1541 const struct vtn_decoration
*dec
, void *data
)
1543 vtn_assert(member
== -1);
1544 if (dec
->decoration
!= SpvDecorationSpecId
)
1547 nir_const_value
*value
= data
;
1548 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1549 if (b
->specializations
[i
].id
== dec
->operands
[0]) {
1550 *value
= b
->specializations
[i
].value
;
1557 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1558 struct vtn_value
*val
,
1559 ASSERTED
int member
,
1560 const struct vtn_decoration
*dec
,
1563 vtn_assert(member
== -1);
1564 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1565 dec
->operands
[0] != SpvBuiltInWorkgroupSize
)
1568 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1569 b
->workgroup_size_builtin
= val
;
1573 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1574 const uint32_t *w
, unsigned count
)
1576 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1577 val
->constant
= rzalloc(b
, nir_constant
);
1579 case SpvOpConstantTrue
:
1580 case SpvOpConstantFalse
:
1581 case SpvOpSpecConstantTrue
:
1582 case SpvOpSpecConstantFalse
: {
1583 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1584 "Result type of %s must be OpTypeBool",
1585 spirv_op_to_string(opcode
));
1587 bool bval
= (opcode
== SpvOpConstantTrue
||
1588 opcode
== SpvOpSpecConstantTrue
);
1590 nir_const_value u32val
= nir_const_value_for_uint(bval
, 32);
1592 if (opcode
== SpvOpSpecConstantTrue
||
1593 opcode
== SpvOpSpecConstantFalse
)
1594 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &u32val
);
1596 val
->constant
->values
[0].b
= u32val
.u32
!= 0;
1601 case SpvOpSpecConstant
: {
1602 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1603 "Result type of %s must be a scalar",
1604 spirv_op_to_string(opcode
));
1605 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1608 val
->constant
->values
[0].u64
= vtn_u64_literal(&w
[3]);
1611 val
->constant
->values
[0].u32
= w
[3];
1614 val
->constant
->values
[0].u16
= w
[3];
1617 val
->constant
->values
[0].u8
= w
[3];
1620 vtn_fail("Unsupported SpvOpConstant bit size: %u", bit_size
);
1623 if (opcode
== SpvOpSpecConstant
)
1624 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
,
1625 &val
->constant
->values
[0]);
1629 case SpvOpSpecConstantComposite
:
1630 case SpvOpConstantComposite
: {
1631 unsigned elem_count
= count
- 3;
1632 vtn_fail_if(elem_count
!= val
->type
->length
,
1633 "%s has %u constituents, expected %u",
1634 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1636 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1637 for (unsigned i
= 0; i
< elem_count
; i
++) {
1638 struct vtn_value
*val
= vtn_untyped_value(b
, w
[i
+ 3]);
1640 if (val
->value_type
== vtn_value_type_constant
) {
1641 elems
[i
] = val
->constant
;
1643 vtn_fail_if(val
->value_type
!= vtn_value_type_undef
,
1644 "only constants or undefs allowed for "
1645 "SpvOpConstantComposite");
1646 /* to make it easier, just insert a NULL constant for now */
1647 elems
[i
] = vtn_null_constant(b
, val
->type
);
1651 switch (val
->type
->base_type
) {
1652 case vtn_base_type_vector
: {
1653 assert(glsl_type_is_vector(val
->type
->type
));
1654 for (unsigned i
= 0; i
< elem_count
; i
++)
1655 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1659 case vtn_base_type_matrix
:
1660 case vtn_base_type_struct
:
1661 case vtn_base_type_array
:
1662 ralloc_steal(val
->constant
, elems
);
1663 val
->constant
->num_elements
= elem_count
;
1664 val
->constant
->elements
= elems
;
1668 vtn_fail("Result type of %s must be a composite type",
1669 spirv_op_to_string(opcode
));
1674 case SpvOpSpecConstantOp
: {
1675 nir_const_value u32op
= nir_const_value_for_uint(w
[3], 32);
1676 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &u32op
);
1677 SpvOp opcode
= u32op
.u32
;
1679 case SpvOpVectorShuffle
: {
1680 struct vtn_value
*v0
= &b
->values
[w
[4]];
1681 struct vtn_value
*v1
= &b
->values
[w
[5]];
1683 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1684 v0
->value_type
== vtn_value_type_undef
);
1685 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1686 v1
->value_type
== vtn_value_type_undef
);
1688 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1689 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1691 vtn_assert(len0
+ len1
< 16);
1693 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1694 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1695 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1697 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1698 (void)bit_size0
; (void)bit_size1
;
1700 nir_const_value undef
= { .u64
= 0xdeadbeefdeadbeef };
1701 nir_const_value combined
[NIR_MAX_VEC_COMPONENTS
* 2];
1703 if (v0
->value_type
== vtn_value_type_constant
) {
1704 for (unsigned i
= 0; i
< len0
; i
++)
1705 combined
[i
] = v0
->constant
->values
[i
];
1707 if (v1
->value_type
== vtn_value_type_constant
) {
1708 for (unsigned i
= 0; i
< len1
; i
++)
1709 combined
[len0
+ i
] = v1
->constant
->values
[i
];
1712 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1713 uint32_t comp
= w
[i
+ 6];
1714 if (comp
== (uint32_t)-1) {
1715 /* If component is not used, set the value to a known constant
1716 * to detect if it is wrongly used.
1718 val
->constant
->values
[j
] = undef
;
1720 vtn_fail_if(comp
>= len0
+ len1
,
1721 "All Component literals must either be FFFFFFFF "
1722 "or in [0, N - 1] (inclusive).");
1723 val
->constant
->values
[j
] = combined
[comp
];
1729 case SpvOpCompositeExtract
:
1730 case SpvOpCompositeInsert
: {
1731 struct vtn_value
*comp
;
1732 unsigned deref_start
;
1733 struct nir_constant
**c
;
1734 if (opcode
== SpvOpCompositeExtract
) {
1735 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1737 c
= &comp
->constant
;
1739 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1741 val
->constant
= nir_constant_clone(comp
->constant
,
1747 const struct vtn_type
*type
= comp
->type
;
1748 for (unsigned i
= deref_start
; i
< count
; i
++) {
1749 vtn_fail_if(w
[i
] > type
->length
,
1750 "%uth index of %s is %u but the type has only "
1751 "%u elements", i
- deref_start
,
1752 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1754 switch (type
->base_type
) {
1755 case vtn_base_type_vector
:
1757 type
= type
->array_element
;
1760 case vtn_base_type_matrix
:
1761 case vtn_base_type_array
:
1762 c
= &(*c
)->elements
[w
[i
]];
1763 type
= type
->array_element
;
1766 case vtn_base_type_struct
:
1767 c
= &(*c
)->elements
[w
[i
]];
1768 type
= type
->members
[w
[i
]];
1772 vtn_fail("%s must only index into composite types",
1773 spirv_op_to_string(opcode
));
1777 if (opcode
== SpvOpCompositeExtract
) {
1781 unsigned num_components
= type
->length
;
1782 for (unsigned i
= 0; i
< num_components
; i
++)
1783 val
->constant
->values
[i
] = (*c
)->values
[elem
+ i
];
1786 struct vtn_value
*insert
=
1787 vtn_value(b
, w
[4], vtn_value_type_constant
);
1788 vtn_assert(insert
->type
== type
);
1790 *c
= insert
->constant
;
1792 unsigned num_components
= type
->length
;
1793 for (unsigned i
= 0; i
< num_components
; i
++)
1794 (*c
)->values
[elem
+ i
] = insert
->constant
->values
[i
];
1802 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
1803 nir_alu_type src_alu_type
= dst_alu_type
;
1804 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
1807 vtn_assert(count
<= 7);
1813 /* We have a source in a conversion */
1815 nir_get_nir_type_for_glsl_type(
1816 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1817 /* We use the bitsize of the conversion source to evaluate the opcode later */
1818 bit_size
= glsl_get_bit_size(
1819 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1822 bit_size
= glsl_get_bit_size(val
->type
->type
);
1825 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
1826 nir_alu_type_get_type_size(src_alu_type
),
1827 nir_alu_type_get_type_size(dst_alu_type
));
1828 nir_const_value src
[3][NIR_MAX_VEC_COMPONENTS
];
1830 for (unsigned i
= 0; i
< count
- 4; i
++) {
1831 struct vtn_value
*src_val
=
1832 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
);
1834 /* If this is an unsized source, pull the bit size from the
1835 * source; otherwise, we'll use the bit size from the destination.
1837 if (!nir_alu_type_get_type_size(nir_op_infos
[op
].input_types
[i
]))
1838 bit_size
= glsl_get_bit_size(src_val
->type
->type
);
1840 unsigned src_comps
= nir_op_infos
[op
].input_sizes
[i
] ?
1841 nir_op_infos
[op
].input_sizes
[i
] :
1844 unsigned j
= swap
? 1 - i
: i
;
1845 for (unsigned c
= 0; c
< src_comps
; c
++)
1846 src
[j
][c
] = src_val
->constant
->values
[c
];
1849 /* fix up fixed size sources */
1856 for (unsigned i
= 0; i
< num_components
; ++i
) {
1858 case 64: src
[1][i
].u32
= src
[1][i
].u64
; break;
1859 case 16: src
[1][i
].u32
= src
[1][i
].u16
; break;
1860 case 8: src
[1][i
].u32
= src
[1][i
].u8
; break;
1869 nir_const_value
*srcs
[3] = {
1870 src
[0], src
[1], src
[2],
1872 nir_eval_const_opcode(op
, val
->constant
->values
,
1873 num_components
, bit_size
, srcs
,
1874 b
->shader
->info
.float_controls_execution_mode
);
1881 case SpvOpConstantNull
:
1882 val
->constant
= vtn_null_constant(b
, val
->type
);
1885 case SpvOpConstantSampler
:
1886 vtn_fail("OpConstantSampler requires Kernel Capability");
1890 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1893 /* Now that we have the value, update the workgroup size if needed */
1894 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1897 SpvMemorySemanticsMask
1898 vtn_storage_class_to_memory_semantics(SpvStorageClass sc
)
1901 case SpvStorageClassStorageBuffer
:
1902 case SpvStorageClassPhysicalStorageBuffer
:
1903 return SpvMemorySemanticsUniformMemoryMask
;
1904 case SpvStorageClassWorkgroup
:
1905 return SpvMemorySemanticsWorkgroupMemoryMask
;
1907 return SpvMemorySemanticsMaskNone
;
1912 vtn_split_barrier_semantics(struct vtn_builder
*b
,
1913 SpvMemorySemanticsMask semantics
,
1914 SpvMemorySemanticsMask
*before
,
1915 SpvMemorySemanticsMask
*after
)
1917 /* For memory semantics embedded in operations, we split them into up to
1918 * two barriers, to be added before and after the operation. This is less
1919 * strict than if we propagated until the final backend stage, but still
1920 * result in correct execution.
1922 * A further improvement could be pipe this information (and use!) into the
1923 * next compiler layers, at the expense of making the handling of barriers
1927 *before
= SpvMemorySemanticsMaskNone
;
1928 *after
= SpvMemorySemanticsMaskNone
;
1930 SpvMemorySemanticsMask order_semantics
=
1931 semantics
& (SpvMemorySemanticsAcquireMask
|
1932 SpvMemorySemanticsReleaseMask
|
1933 SpvMemorySemanticsAcquireReleaseMask
|
1934 SpvMemorySemanticsSequentiallyConsistentMask
);
1936 if (util_bitcount(order_semantics
) > 1) {
1937 /* Old GLSLang versions incorrectly set all the ordering bits. This was
1938 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
1939 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
1941 vtn_warn("Multiple memory ordering semantics specified, "
1942 "assuming AcquireRelease.");
1943 order_semantics
= SpvMemorySemanticsAcquireReleaseMask
;
1946 const SpvMemorySemanticsMask av_vis_semantics
=
1947 semantics
& (SpvMemorySemanticsMakeAvailableMask
|
1948 SpvMemorySemanticsMakeVisibleMask
);
1950 const SpvMemorySemanticsMask storage_semantics
=
1951 semantics
& (SpvMemorySemanticsUniformMemoryMask
|
1952 SpvMemorySemanticsSubgroupMemoryMask
|
1953 SpvMemorySemanticsWorkgroupMemoryMask
|
1954 SpvMemorySemanticsCrossWorkgroupMemoryMask
|
1955 SpvMemorySemanticsAtomicCounterMemoryMask
|
1956 SpvMemorySemanticsImageMemoryMask
|
1957 SpvMemorySemanticsOutputMemoryMask
);
1959 const SpvMemorySemanticsMask other_semantics
=
1960 semantics
& ~(order_semantics
| av_vis_semantics
| storage_semantics
);
1962 if (other_semantics
)
1963 vtn_warn("Ignoring unhandled memory semantics: %u\n", other_semantics
);
1965 /* SequentiallyConsistent is treated as AcquireRelease. */
1967 /* The RELEASE barrier happens BEFORE the operation, and it is usually
1968 * associated with a Store. All the write operations with a matching
1969 * semantics will not be reordered after the Store.
1971 if (order_semantics
& (SpvMemorySemanticsReleaseMask
|
1972 SpvMemorySemanticsAcquireReleaseMask
|
1973 SpvMemorySemanticsSequentiallyConsistentMask
)) {
1974 *before
|= SpvMemorySemanticsReleaseMask
| storage_semantics
;
1977 /* The ACQUIRE barrier happens AFTER the operation, and it is usually
1978 * associated with a Load. All the operations with a matching semantics
1979 * will not be reordered before the Load.
1981 if (order_semantics
& (SpvMemorySemanticsAcquireMask
|
1982 SpvMemorySemanticsAcquireReleaseMask
|
1983 SpvMemorySemanticsSequentiallyConsistentMask
)) {
1984 *after
|= SpvMemorySemanticsAcquireMask
| storage_semantics
;
1987 if (av_vis_semantics
& SpvMemorySemanticsMakeVisibleMask
)
1988 *before
|= SpvMemorySemanticsMakeVisibleMask
| storage_semantics
;
1990 if (av_vis_semantics
& SpvMemorySemanticsMakeAvailableMask
)
1991 *after
|= SpvMemorySemanticsMakeAvailableMask
| storage_semantics
;
1994 static nir_memory_semantics
1995 vtn_mem_semantics_to_nir_mem_semantics(struct vtn_builder
*b
,
1996 SpvMemorySemanticsMask semantics
)
1998 nir_memory_semantics nir_semantics
= 0;
2000 SpvMemorySemanticsMask order_semantics
=
2001 semantics
& (SpvMemorySemanticsAcquireMask
|
2002 SpvMemorySemanticsReleaseMask
|
2003 SpvMemorySemanticsAcquireReleaseMask
|
2004 SpvMemorySemanticsSequentiallyConsistentMask
);
2006 if (util_bitcount(order_semantics
) > 1) {
2007 /* Old GLSLang versions incorrectly set all the ordering bits. This was
2008 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
2009 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
2011 vtn_warn("Multiple memory ordering semantics bits specified, "
2012 "assuming AcquireRelease.");
2013 order_semantics
= SpvMemorySemanticsAcquireReleaseMask
;
2016 switch (order_semantics
) {
2018 /* Not an ordering barrier. */
2021 case SpvMemorySemanticsAcquireMask
:
2022 nir_semantics
= NIR_MEMORY_ACQUIRE
;
2025 case SpvMemorySemanticsReleaseMask
:
2026 nir_semantics
= NIR_MEMORY_RELEASE
;
2029 case SpvMemorySemanticsSequentiallyConsistentMask
:
2030 /* Fall through. Treated as AcquireRelease in Vulkan. */
2031 case SpvMemorySemanticsAcquireReleaseMask
:
2032 nir_semantics
= NIR_MEMORY_ACQUIRE
| NIR_MEMORY_RELEASE
;
2036 unreachable("Invalid memory order semantics");
2039 if (semantics
& SpvMemorySemanticsMakeAvailableMask
) {
2040 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2041 "To use MakeAvailable memory semantics the VulkanMemoryModel "
2042 "capability must be declared.");
2043 nir_semantics
|= NIR_MEMORY_MAKE_AVAILABLE
;
2046 if (semantics
& SpvMemorySemanticsMakeVisibleMask
) {
2047 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2048 "To use MakeVisible memory semantics the VulkanMemoryModel "
2049 "capability must be declared.");
2050 nir_semantics
|= NIR_MEMORY_MAKE_VISIBLE
;
2053 return nir_semantics
;
2056 static nir_variable_mode
2057 vtn_mem_sematics_to_nir_var_modes(struct vtn_builder
*b
,
2058 SpvMemorySemanticsMask semantics
)
2060 /* Vulkan Environment for SPIR-V says "SubgroupMemory, CrossWorkgroupMemory,
2061 * and AtomicCounterMemory are ignored".
2063 semantics
&= ~(SpvMemorySemanticsSubgroupMemoryMask
|
2064 SpvMemorySemanticsCrossWorkgroupMemoryMask
|
2065 SpvMemorySemanticsAtomicCounterMemoryMask
);
2067 /* TODO: Consider adding nir_var_mem_image mode to NIR so it can be used
2068 * for SpvMemorySemanticsImageMemoryMask.
2071 nir_variable_mode modes
= 0;
2072 if (semantics
& (SpvMemorySemanticsUniformMemoryMask
|
2073 SpvMemorySemanticsImageMemoryMask
)) {
2074 modes
|= nir_var_uniform
|
2079 if (semantics
& SpvMemorySemanticsWorkgroupMemoryMask
)
2080 modes
|= nir_var_mem_shared
;
2081 if (semantics
& SpvMemorySemanticsOutputMemoryMask
) {
2082 modes
|= nir_var_shader_out
;
2089 vtn_scope_to_nir_scope(struct vtn_builder
*b
, SpvScope scope
)
2091 nir_scope nir_scope
;
2093 case SpvScopeDevice
:
2094 vtn_fail_if(b
->options
->caps
.vk_memory_model
&&
2095 !b
->options
->caps
.vk_memory_model_device_scope
,
2096 "If the Vulkan memory model is declared and any instruction "
2097 "uses Device scope, the VulkanMemoryModelDeviceScope "
2098 "capability must be declared.");
2099 nir_scope
= NIR_SCOPE_DEVICE
;
2102 case SpvScopeQueueFamily
:
2103 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2104 "To use Queue Family scope, the VulkanMemoryModel capability "
2105 "must be declared.");
2106 nir_scope
= NIR_SCOPE_QUEUE_FAMILY
;
2109 case SpvScopeWorkgroup
:
2110 nir_scope
= NIR_SCOPE_WORKGROUP
;
2113 case SpvScopeSubgroup
:
2114 nir_scope
= NIR_SCOPE_SUBGROUP
;
2117 case SpvScopeInvocation
:
2118 nir_scope
= NIR_SCOPE_INVOCATION
;
2122 vtn_fail("Invalid memory scope");
2129 vtn_emit_scoped_control_barrier(struct vtn_builder
*b
, SpvScope exec_scope
,
2131 SpvMemorySemanticsMask semantics
)
2133 nir_memory_semantics nir_semantics
;
2134 nir_variable_mode modes
;
2136 nir_semantics
= vtn_mem_semantics_to_nir_mem_semantics(b
, semantics
);
2137 modes
= vtn_mem_sematics_to_nir_var_modes(b
, semantics
);
2139 /* No barrier to add. */
2140 if (nir_semantics
== 0 || modes
== 0)
2143 nir_scope nir_exec_scope
= vtn_scope_to_nir_scope(b
, exec_scope
);
2144 nir_scope nir_mem_scope
= vtn_scope_to_nir_scope(b
, mem_scope
);
2145 nir_scoped_barrier(&b
->nb
, nir_exec_scope
, nir_mem_scope
, nir_semantics
, modes
);
2149 vtn_emit_scoped_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
2150 SpvMemorySemanticsMask semantics
)
2152 nir_variable_mode modes
= vtn_mem_sematics_to_nir_var_modes(b
, semantics
);
2153 nir_memory_semantics nir_semantics
=
2154 vtn_mem_semantics_to_nir_mem_semantics(b
, semantics
);
2156 /* No barrier to add. */
2157 if (nir_semantics
== 0 || modes
== 0)
2160 nir_scope nir_mem_scope
= vtn_scope_to_nir_scope(b
, scope
);
2161 nir_scoped_barrier(&b
->nb
, NIR_SCOPE_NONE
, nir_mem_scope
, nir_semantics
, modes
);
2164 struct vtn_ssa_value
*
2165 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
2167 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
2170 if (!glsl_type_is_vector_or_scalar(type
)) {
2171 unsigned elems
= glsl_get_length(type
);
2172 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2173 for (unsigned i
= 0; i
< elems
; i
++) {
2174 const struct glsl_type
*child_type
;
2176 switch (glsl_get_base_type(type
)) {
2178 case GLSL_TYPE_UINT
:
2179 case GLSL_TYPE_INT16
:
2180 case GLSL_TYPE_UINT16
:
2181 case GLSL_TYPE_UINT8
:
2182 case GLSL_TYPE_INT8
:
2183 case GLSL_TYPE_INT64
:
2184 case GLSL_TYPE_UINT64
:
2185 case GLSL_TYPE_BOOL
:
2186 case GLSL_TYPE_FLOAT
:
2187 case GLSL_TYPE_FLOAT16
:
2188 case GLSL_TYPE_DOUBLE
:
2189 child_type
= glsl_get_column_type(type
);
2191 case GLSL_TYPE_ARRAY
:
2192 child_type
= glsl_get_array_element(type
);
2194 case GLSL_TYPE_STRUCT
:
2195 case GLSL_TYPE_INTERFACE
:
2196 child_type
= glsl_get_struct_field(type
, i
);
2199 vtn_fail("unkown base type");
2202 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
2210 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
2213 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
2214 src
.src_type
= type
;
2219 image_operand_arg(struct vtn_builder
*b
, const uint32_t *w
, uint32_t count
,
2220 uint32_t mask_idx
, SpvImageOperandsMask op
)
2222 static const SpvImageOperandsMask ops_with_arg
=
2223 SpvImageOperandsBiasMask
|
2224 SpvImageOperandsLodMask
|
2225 SpvImageOperandsGradMask
|
2226 SpvImageOperandsConstOffsetMask
|
2227 SpvImageOperandsOffsetMask
|
2228 SpvImageOperandsConstOffsetsMask
|
2229 SpvImageOperandsSampleMask
|
2230 SpvImageOperandsMinLodMask
|
2231 SpvImageOperandsMakeTexelAvailableMask
|
2232 SpvImageOperandsMakeTexelVisibleMask
;
2234 assert(util_bitcount(op
) == 1);
2235 assert(w
[mask_idx
] & op
);
2236 assert(op
& ops_with_arg
);
2238 uint32_t idx
= util_bitcount(w
[mask_idx
] & (op
- 1) & ops_with_arg
) + 1;
2240 /* Adjust indices for operands with two arguments. */
2241 static const SpvImageOperandsMask ops_with_two_args
=
2242 SpvImageOperandsGradMask
;
2243 idx
+= util_bitcount(w
[mask_idx
] & (op
- 1) & ops_with_two_args
);
2247 vtn_fail_if(idx
+ (op
& ops_with_two_args
? 1 : 0) >= count
,
2248 "Image op claims to have %s but does not enough "
2249 "following operands", spirv_imageoperands_to_string(op
));
2255 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
2256 const uint32_t *w
, unsigned count
)
2258 if (opcode
== SpvOpSampledImage
) {
2259 struct vtn_value
*val
=
2260 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2261 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2262 val
->sampled_image
->image
=
2263 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2264 val
->sampled_image
->sampler
=
2265 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
2267 } else if (opcode
== SpvOpImage
) {
2268 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
2269 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
2270 vtn_push_value_pointer(b
, w
[2], src_val
->sampled_image
->image
);
2272 vtn_assert(src_val
->value_type
== vtn_value_type_pointer
);
2273 vtn_push_value_pointer(b
, w
[2], src_val
->pointer
);
2278 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2280 struct vtn_pointer
*image
= NULL
, *sampler
= NULL
;
2281 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
2282 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
2283 image
= sampled_val
->sampled_image
->image
;
2284 sampler
= sampled_val
->sampled_image
->sampler
;
2286 vtn_assert(sampled_val
->value_type
== vtn_value_type_pointer
);
2287 image
= sampled_val
->pointer
;
2290 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
);
2291 nir_deref_instr
*sampler_deref
=
2292 sampler
? vtn_pointer_to_deref(b
, sampler
) : NULL
;
2294 const struct glsl_type
*image_type
= sampled_val
->type
->type
;
2295 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
2296 const bool is_array
= glsl_sampler_type_is_array(image_type
);
2297 nir_alu_type dest_type
= nir_type_invalid
;
2299 /* Figure out the base texture operation */
2302 case SpvOpImageSampleImplicitLod
:
2303 case SpvOpImageSampleDrefImplicitLod
:
2304 case SpvOpImageSampleProjImplicitLod
:
2305 case SpvOpImageSampleProjDrefImplicitLod
:
2306 texop
= nir_texop_tex
;
2309 case SpvOpImageSampleExplicitLod
:
2310 case SpvOpImageSampleDrefExplicitLod
:
2311 case SpvOpImageSampleProjExplicitLod
:
2312 case SpvOpImageSampleProjDrefExplicitLod
:
2313 texop
= nir_texop_txl
;
2316 case SpvOpImageFetch
:
2317 if (sampler_dim
== GLSL_SAMPLER_DIM_MS
) {
2318 texop
= nir_texop_txf_ms
;
2320 texop
= nir_texop_txf
;
2324 case SpvOpImageGather
:
2325 case SpvOpImageDrefGather
:
2326 texop
= nir_texop_tg4
;
2329 case SpvOpImageQuerySizeLod
:
2330 case SpvOpImageQuerySize
:
2331 texop
= nir_texop_txs
;
2332 dest_type
= nir_type_int
;
2335 case SpvOpImageQueryLod
:
2336 texop
= nir_texop_lod
;
2337 dest_type
= nir_type_float
;
2340 case SpvOpImageQueryLevels
:
2341 texop
= nir_texop_query_levels
;
2342 dest_type
= nir_type_int
;
2345 case SpvOpImageQuerySamples
:
2346 texop
= nir_texop_texture_samples
;
2347 dest_type
= nir_type_int
;
2350 case SpvOpFragmentFetchAMD
:
2351 texop
= nir_texop_fragment_fetch
;
2354 case SpvOpFragmentMaskFetchAMD
:
2355 texop
= nir_texop_fragment_mask_fetch
;
2359 vtn_fail_with_opcode("Unhandled opcode", opcode
);
2362 nir_tex_src srcs
[10]; /* 10 should be enough */
2363 nir_tex_src
*p
= srcs
;
2365 p
->src
= nir_src_for_ssa(&image_deref
->dest
.ssa
);
2366 p
->src_type
= nir_tex_src_texture_deref
;
2376 vtn_fail_if(sampler
== NULL
,
2377 "%s requires an image of type OpTypeSampledImage",
2378 spirv_op_to_string(opcode
));
2379 p
->src
= nir_src_for_ssa(&sampler_deref
->dest
.ssa
);
2380 p
->src_type
= nir_tex_src_sampler_deref
;
2384 case nir_texop_txf_ms
:
2386 case nir_texop_query_levels
:
2387 case nir_texop_texture_samples
:
2388 case nir_texop_samples_identical
:
2389 case nir_texop_fragment_fetch
:
2390 case nir_texop_fragment_mask_fetch
:
2393 case nir_texop_txf_ms_fb
:
2394 vtn_fail("unexpected nir_texop_txf_ms_fb");
2396 case nir_texop_txf_ms_mcs
:
2397 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2398 case nir_texop_tex_prefetch
:
2399 vtn_fail("unexpected nir_texop_tex_prefetch");
2404 struct nir_ssa_def
*coord
;
2405 unsigned coord_components
;
2407 case SpvOpImageSampleImplicitLod
:
2408 case SpvOpImageSampleExplicitLod
:
2409 case SpvOpImageSampleDrefImplicitLod
:
2410 case SpvOpImageSampleDrefExplicitLod
:
2411 case SpvOpImageSampleProjImplicitLod
:
2412 case SpvOpImageSampleProjExplicitLod
:
2413 case SpvOpImageSampleProjDrefImplicitLod
:
2414 case SpvOpImageSampleProjDrefExplicitLod
:
2415 case SpvOpImageFetch
:
2416 case SpvOpImageGather
:
2417 case SpvOpImageDrefGather
:
2418 case SpvOpImageQueryLod
:
2419 case SpvOpFragmentFetchAMD
:
2420 case SpvOpFragmentMaskFetchAMD
: {
2421 /* All these types have the coordinate as their first real argument */
2422 coord_components
= glsl_get_sampler_dim_coordinate_components(sampler_dim
);
2424 if (is_array
&& texop
!= nir_texop_lod
)
2427 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
2428 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2429 (1 << coord_components
) - 1));
2430 p
->src_type
= nir_tex_src_coord
;
2437 coord_components
= 0;
2442 case SpvOpImageSampleProjImplicitLod
:
2443 case SpvOpImageSampleProjExplicitLod
:
2444 case SpvOpImageSampleProjDrefImplicitLod
:
2445 case SpvOpImageSampleProjDrefExplicitLod
:
2446 /* These have the projector as the last coordinate component */
2447 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2448 p
->src_type
= nir_tex_src_projector
;
2456 bool is_shadow
= false;
2457 unsigned gather_component
= 0;
2459 case SpvOpImageSampleDrefImplicitLod
:
2460 case SpvOpImageSampleDrefExplicitLod
:
2461 case SpvOpImageSampleProjDrefImplicitLod
:
2462 case SpvOpImageSampleProjDrefExplicitLod
:
2463 case SpvOpImageDrefGather
:
2464 /* These all have an explicit depth value as their next source */
2466 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2469 case SpvOpImageGather
:
2470 /* This has a component as its next source */
2471 gather_component
= vtn_constant_uint(b
, w
[idx
++]);
2478 /* For OpImageQuerySizeLod, we always have an LOD */
2479 if (opcode
== SpvOpImageQuerySizeLod
)
2480 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2482 /* For OpFragmentFetchAMD, we always have a multisample index */
2483 if (opcode
== SpvOpFragmentFetchAMD
)
2484 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
2486 /* Now we need to handle some number of optional arguments */
2487 struct vtn_value
*gather_offsets
= NULL
;
2489 uint32_t operands
= w
[idx
];
2491 if (operands
& SpvImageOperandsBiasMask
) {
2492 vtn_assert(texop
== nir_texop_tex
||
2493 texop
== nir_texop_tg4
);
2494 if (texop
== nir_texop_tex
)
2495 texop
= nir_texop_txb
;
2496 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2497 SpvImageOperandsBiasMask
);
2498 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_bias
);
2501 if (operands
& SpvImageOperandsLodMask
) {
2502 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2503 texop
== nir_texop_txs
|| texop
== nir_texop_tg4
);
2504 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2505 SpvImageOperandsLodMask
);
2506 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_lod
);
2509 if (operands
& SpvImageOperandsGradMask
) {
2510 vtn_assert(texop
== nir_texop_txl
);
2511 texop
= nir_texop_txd
;
2512 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2513 SpvImageOperandsGradMask
);
2514 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_ddx
);
2515 (*p
++) = vtn_tex_src(b
, w
[arg
+ 1], nir_tex_src_ddy
);
2518 vtn_fail_if(util_bitcount(operands
& (SpvImageOperandsConstOffsetsMask
|
2519 SpvImageOperandsOffsetMask
|
2520 SpvImageOperandsConstOffsetMask
)) > 1,
2521 "At most one of the ConstOffset, Offset, and ConstOffsets "
2522 "image operands can be used on a given instruction.");
2524 if (operands
& SpvImageOperandsOffsetMask
) {
2525 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2526 SpvImageOperandsOffsetMask
);
2527 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_offset
);
2530 if (operands
& SpvImageOperandsConstOffsetMask
) {
2531 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2532 SpvImageOperandsConstOffsetMask
);
2533 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_offset
);
2536 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2537 vtn_assert(texop
== nir_texop_tg4
);
2538 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2539 SpvImageOperandsConstOffsetsMask
);
2540 gather_offsets
= vtn_value(b
, w
[arg
], vtn_value_type_constant
);
2543 if (operands
& SpvImageOperandsSampleMask
) {
2544 vtn_assert(texop
== nir_texop_txf_ms
);
2545 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2546 SpvImageOperandsSampleMask
);
2547 texop
= nir_texop_txf_ms
;
2548 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_ms_index
);
2551 if (operands
& SpvImageOperandsMinLodMask
) {
2552 vtn_assert(texop
== nir_texop_tex
||
2553 texop
== nir_texop_txb
||
2554 texop
== nir_texop_txd
);
2555 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2556 SpvImageOperandsMinLodMask
);
2557 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_min_lod
);
2561 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2564 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2566 instr
->coord_components
= coord_components
;
2567 instr
->sampler_dim
= sampler_dim
;
2568 instr
->is_array
= is_array
;
2569 instr
->is_shadow
= is_shadow
;
2570 instr
->is_new_style_shadow
=
2571 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2572 instr
->component
= gather_component
;
2574 if (image
&& (image
->access
& ACCESS_NON_UNIFORM
))
2575 instr
->texture_non_uniform
= true;
2577 if (sampler
&& (sampler
->access
& ACCESS_NON_UNIFORM
))
2578 instr
->sampler_non_uniform
= true;
2580 /* for non-query ops, get dest_type from sampler type */
2581 if (dest_type
== nir_type_invalid
) {
2582 switch (glsl_get_sampler_result_type(image_type
)) {
2583 case GLSL_TYPE_FLOAT
: dest_type
= nir_type_float
; break;
2584 case GLSL_TYPE_INT
: dest_type
= nir_type_int
; break;
2585 case GLSL_TYPE_UINT
: dest_type
= nir_type_uint
; break;
2586 case GLSL_TYPE_BOOL
: dest_type
= nir_type_bool
; break;
2588 vtn_fail("Invalid base type for sampler result");
2592 instr
->dest_type
= dest_type
;
2594 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2595 nir_tex_instr_dest_size(instr
), 32, NULL
);
2597 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2598 nir_tex_instr_dest_size(instr
));
2600 if (gather_offsets
) {
2601 vtn_fail_if(gather_offsets
->type
->base_type
!= vtn_base_type_array
||
2602 gather_offsets
->type
->length
!= 4,
2603 "ConstOffsets must be an array of size four of vectors "
2604 "of two integer components");
2606 struct vtn_type
*vec_type
= gather_offsets
->type
->array_element
;
2607 vtn_fail_if(vec_type
->base_type
!= vtn_base_type_vector
||
2608 vec_type
->length
!= 2 ||
2609 !glsl_type_is_integer(vec_type
->type
),
2610 "ConstOffsets must be an array of size four of vectors "
2611 "of two integer components");
2613 unsigned bit_size
= glsl_get_bit_size(vec_type
->type
);
2614 for (uint32_t i
= 0; i
< 4; i
++) {
2615 const nir_const_value
*cvec
=
2616 gather_offsets
->constant
->elements
[i
]->values
;
2617 for (uint32_t j
= 0; j
< 2; j
++) {
2619 case 8: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i8
; break;
2620 case 16: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i16
; break;
2621 case 32: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i32
; break;
2622 case 64: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i64
; break;
2624 vtn_fail("Unsupported bit size: %u", bit_size
);
2630 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
2631 ssa
->def
= &instr
->dest
.ssa
;
2632 vtn_push_ssa(b
, w
[2], ret_type
, ssa
);
2634 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2638 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2639 const uint32_t *w
, nir_src
*src
)
2642 case SpvOpAtomicIIncrement
:
2643 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
2646 case SpvOpAtomicIDecrement
:
2647 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
2650 case SpvOpAtomicISub
:
2652 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
2655 case SpvOpAtomicCompareExchange
:
2656 case SpvOpAtomicCompareExchangeWeak
:
2657 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
2658 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
2661 case SpvOpAtomicExchange
:
2662 case SpvOpAtomicIAdd
:
2663 case SpvOpAtomicSMin
:
2664 case SpvOpAtomicUMin
:
2665 case SpvOpAtomicSMax
:
2666 case SpvOpAtomicUMax
:
2667 case SpvOpAtomicAnd
:
2669 case SpvOpAtomicXor
:
2670 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
2674 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2678 static nir_ssa_def
*
2679 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2681 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
2683 /* The image_load_store intrinsics assume a 4-dim coordinate */
2684 unsigned dim
= glsl_get_vector_elements(coord
->type
);
2685 unsigned swizzle
[4];
2686 for (unsigned i
= 0; i
< 4; i
++)
2687 swizzle
[i
] = MIN2(i
, dim
- 1);
2689 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4);
2692 static nir_ssa_def
*
2693 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2695 if (value
->num_components
== 4)
2699 for (unsigned i
= 0; i
< 4; i
++)
2700 swiz
[i
] = i
< value
->num_components
? i
: 0;
2701 return nir_swizzle(b
, value
, swiz
, 4);
2705 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2706 const uint32_t *w
, unsigned count
)
2708 /* Just get this one out of the way */
2709 if (opcode
== SpvOpImageTexelPointer
) {
2710 struct vtn_value
*val
=
2711 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2712 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2714 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2715 val
->image
->coord
= get_image_coord(b
, w
[4]);
2716 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
2717 val
->image
->lod
= nir_imm_int(&b
->nb
, 0);
2721 struct vtn_image_pointer image
;
2722 SpvScope scope
= SpvScopeInvocation
;
2723 SpvMemorySemanticsMask semantics
= 0;
2726 case SpvOpAtomicExchange
:
2727 case SpvOpAtomicCompareExchange
:
2728 case SpvOpAtomicCompareExchangeWeak
:
2729 case SpvOpAtomicIIncrement
:
2730 case SpvOpAtomicIDecrement
:
2731 case SpvOpAtomicIAdd
:
2732 case SpvOpAtomicISub
:
2733 case SpvOpAtomicLoad
:
2734 case SpvOpAtomicSMin
:
2735 case SpvOpAtomicUMin
:
2736 case SpvOpAtomicSMax
:
2737 case SpvOpAtomicUMax
:
2738 case SpvOpAtomicAnd
:
2740 case SpvOpAtomicXor
:
2741 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
2742 scope
= vtn_constant_uint(b
, w
[4]);
2743 semantics
= vtn_constant_uint(b
, w
[5]);
2746 case SpvOpAtomicStore
:
2747 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
2748 scope
= vtn_constant_uint(b
, w
[2]);
2749 semantics
= vtn_constant_uint(b
, w
[3]);
2752 case SpvOpImageQuerySize
:
2753 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2755 image
.sample
= NULL
;
2759 case SpvOpImageRead
: {
2760 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2761 image
.coord
= get_image_coord(b
, w
[4]);
2763 const SpvImageOperandsMask operands
=
2764 count
> 5 ? w
[5] : SpvImageOperandsMaskNone
;
2766 if (operands
& SpvImageOperandsSampleMask
) {
2767 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
2768 SpvImageOperandsSampleMask
);
2769 image
.sample
= vtn_ssa_value(b
, w
[arg
])->def
;
2771 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2774 if (operands
& SpvImageOperandsMakeTexelVisibleMask
) {
2775 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
2776 "MakeTexelVisible requires NonPrivateTexel to also be set.");
2777 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
2778 SpvImageOperandsMakeTexelVisibleMask
);
2779 semantics
= SpvMemorySemanticsMakeVisibleMask
;
2780 scope
= vtn_constant_uint(b
, w
[arg
]);
2783 if (operands
& SpvImageOperandsLodMask
) {
2784 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
2785 SpvImageOperandsLodMask
);
2786 image
.lod
= vtn_ssa_value(b
, w
[arg
])->def
;
2788 image
.lod
= nir_imm_int(&b
->nb
, 0);
2791 /* TODO: Volatile. */
2796 case SpvOpImageWrite
: {
2797 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2798 image
.coord
= get_image_coord(b
, w
[2]);
2802 const SpvImageOperandsMask operands
=
2803 count
> 4 ? w
[4] : SpvImageOperandsMaskNone
;
2805 if (operands
& SpvImageOperandsSampleMask
) {
2806 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
2807 SpvImageOperandsSampleMask
);
2808 image
.sample
= vtn_ssa_value(b
, w
[arg
])->def
;
2810 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2813 if (operands
& SpvImageOperandsMakeTexelAvailableMask
) {
2814 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
2815 "MakeTexelAvailable requires NonPrivateTexel to also be set.");
2816 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
2817 SpvImageOperandsMakeTexelAvailableMask
);
2818 semantics
= SpvMemorySemanticsMakeAvailableMask
;
2819 scope
= vtn_constant_uint(b
, w
[arg
]);
2822 if (operands
& SpvImageOperandsLodMask
) {
2823 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
2824 SpvImageOperandsLodMask
);
2825 image
.lod
= vtn_ssa_value(b
, w
[arg
])->def
;
2827 image
.lod
= nir_imm_int(&b
->nb
, 0);
2830 /* TODO: Volatile. */
2836 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2839 nir_intrinsic_op op
;
2841 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
2842 OP(ImageQuerySize
, size
)
2844 OP(ImageWrite
, store
)
2845 OP(AtomicLoad
, load
)
2846 OP(AtomicStore
, store
)
2847 OP(AtomicExchange
, atomic_exchange
)
2848 OP(AtomicCompareExchange
, atomic_comp_swap
)
2849 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2850 OP(AtomicIIncrement
, atomic_add
)
2851 OP(AtomicIDecrement
, atomic_add
)
2852 OP(AtomicIAdd
, atomic_add
)
2853 OP(AtomicISub
, atomic_add
)
2854 OP(AtomicSMin
, atomic_imin
)
2855 OP(AtomicUMin
, atomic_umin
)
2856 OP(AtomicSMax
, atomic_imax
)
2857 OP(AtomicUMax
, atomic_umax
)
2858 OP(AtomicAnd
, atomic_and
)
2859 OP(AtomicOr
, atomic_or
)
2860 OP(AtomicXor
, atomic_xor
)
2863 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2866 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
2868 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
2869 intrin
->src
[0] = nir_src_for_ssa(&image_deref
->dest
.ssa
);
2871 /* ImageQuerySize doesn't take any extra parameters */
2872 if (opcode
!= SpvOpImageQuerySize
) {
2873 /* The image coordinate is always 4 components but we may not have that
2874 * many. Swizzle to compensate.
2876 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
2877 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
2880 nir_intrinsic_set_access(intrin
, image
.image
->access
);
2883 case SpvOpAtomicLoad
:
2884 case SpvOpImageQuerySize
:
2885 case SpvOpImageRead
:
2886 if (opcode
== SpvOpImageRead
|| opcode
== SpvOpAtomicLoad
) {
2887 /* Only OpImageRead can support a lod parameter if
2888 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
2889 * intrinsics definition for atomics requires us to set it for
2892 intrin
->src
[3] = nir_src_for_ssa(image
.lod
);
2895 case SpvOpAtomicStore
:
2896 case SpvOpImageWrite
: {
2897 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
2898 nir_ssa_def
*value
= vtn_ssa_value(b
, value_id
)->def
;
2899 /* nir_intrinsic_image_deref_store always takes a vec4 value */
2900 assert(op
== nir_intrinsic_image_deref_store
);
2901 intrin
->num_components
= 4;
2902 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
));
2903 /* Only OpImageWrite can support a lod parameter if
2904 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
2905 * intrinsics definition for atomics requires us to set it for
2908 intrin
->src
[4] = nir_src_for_ssa(image
.lod
);
2912 case SpvOpAtomicCompareExchange
:
2913 case SpvOpAtomicCompareExchangeWeak
:
2914 case SpvOpAtomicIIncrement
:
2915 case SpvOpAtomicIDecrement
:
2916 case SpvOpAtomicExchange
:
2917 case SpvOpAtomicIAdd
:
2918 case SpvOpAtomicISub
:
2919 case SpvOpAtomicSMin
:
2920 case SpvOpAtomicUMin
:
2921 case SpvOpAtomicSMax
:
2922 case SpvOpAtomicUMax
:
2923 case SpvOpAtomicAnd
:
2925 case SpvOpAtomicXor
:
2926 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
2930 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2933 /* Image operations implicitly have the Image storage memory semantics. */
2934 semantics
|= SpvMemorySemanticsImageMemoryMask
;
2936 SpvMemorySemanticsMask before_semantics
;
2937 SpvMemorySemanticsMask after_semantics
;
2938 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
2940 if (before_semantics
)
2941 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
2943 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
2944 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2946 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
2947 intrin
->num_components
= nir_intrinsic_infos
[op
].dest_components
;
2948 if (intrin
->num_components
== 0)
2949 intrin
->num_components
= dest_components
;
2951 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2952 intrin
->num_components
, 32, NULL
);
2954 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2956 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
2957 if (intrin
->num_components
!= dest_components
)
2958 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
2960 struct vtn_value
*val
=
2961 vtn_push_ssa(b
, w
[2], type
, vtn_create_ssa_value(b
, type
->type
));
2962 val
->ssa
->def
= result
;
2964 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2967 if (after_semantics
)
2968 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
2971 static nir_intrinsic_op
2972 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2975 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2976 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2977 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2978 OP(AtomicExchange
, atomic_exchange
)
2979 OP(AtomicCompareExchange
, atomic_comp_swap
)
2980 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2981 OP(AtomicIIncrement
, atomic_add
)
2982 OP(AtomicIDecrement
, atomic_add
)
2983 OP(AtomicIAdd
, atomic_add
)
2984 OP(AtomicISub
, atomic_add
)
2985 OP(AtomicSMin
, atomic_imin
)
2986 OP(AtomicUMin
, atomic_umin
)
2987 OP(AtomicSMax
, atomic_imax
)
2988 OP(AtomicUMax
, atomic_umax
)
2989 OP(AtomicAnd
, atomic_and
)
2990 OP(AtomicOr
, atomic_or
)
2991 OP(AtomicXor
, atomic_xor
)
2994 vtn_fail_with_opcode("Invalid SSBO atomic", opcode
);
2998 static nir_intrinsic_op
2999 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3002 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
3003 OP(AtomicLoad
, read_deref
)
3004 OP(AtomicExchange
, exchange
)
3005 OP(AtomicCompareExchange
, comp_swap
)
3006 OP(AtomicCompareExchangeWeak
, comp_swap
)
3007 OP(AtomicIIncrement
, inc_deref
)
3008 OP(AtomicIDecrement
, post_dec_deref
)
3009 OP(AtomicIAdd
, add_deref
)
3010 OP(AtomicISub
, add_deref
)
3011 OP(AtomicUMin
, min_deref
)
3012 OP(AtomicUMax
, max_deref
)
3013 OP(AtomicAnd
, and_deref
)
3014 OP(AtomicOr
, or_deref
)
3015 OP(AtomicXor
, xor_deref
)
3018 /* We left the following out: AtomicStore, AtomicSMin and
3019 * AtomicSmax. Right now there are not nir intrinsics for them. At this
3020 * moment Atomic Counter support is needed for ARB_spirv support, so is
3021 * only need to support GLSL Atomic Counters that are uints and don't
3022 * allow direct storage.
3024 vtn_fail("Invalid uniform atomic");
3028 static nir_intrinsic_op
3029 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3032 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
3033 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
3034 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
3035 OP(AtomicExchange
, atomic_exchange
)
3036 OP(AtomicCompareExchange
, atomic_comp_swap
)
3037 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3038 OP(AtomicIIncrement
, atomic_add
)
3039 OP(AtomicIDecrement
, atomic_add
)
3040 OP(AtomicIAdd
, atomic_add
)
3041 OP(AtomicISub
, atomic_add
)
3042 OP(AtomicSMin
, atomic_imin
)
3043 OP(AtomicUMin
, atomic_umin
)
3044 OP(AtomicSMax
, atomic_imax
)
3045 OP(AtomicUMax
, atomic_umax
)
3046 OP(AtomicAnd
, atomic_and
)
3047 OP(AtomicOr
, atomic_or
)
3048 OP(AtomicXor
, atomic_xor
)
3051 vtn_fail_with_opcode("Invalid shared atomic", opcode
);
3056 * Handles shared atomics, ssbo atomics and atomic counters.
3059 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
3060 const uint32_t *w
, UNUSED
unsigned count
)
3062 struct vtn_pointer
*ptr
;
3063 nir_intrinsic_instr
*atomic
;
3065 SpvScope scope
= SpvScopeInvocation
;
3066 SpvMemorySemanticsMask semantics
= 0;
3069 case SpvOpAtomicLoad
:
3070 case SpvOpAtomicExchange
:
3071 case SpvOpAtomicCompareExchange
:
3072 case SpvOpAtomicCompareExchangeWeak
:
3073 case SpvOpAtomicIIncrement
:
3074 case SpvOpAtomicIDecrement
:
3075 case SpvOpAtomicIAdd
:
3076 case SpvOpAtomicISub
:
3077 case SpvOpAtomicSMin
:
3078 case SpvOpAtomicUMin
:
3079 case SpvOpAtomicSMax
:
3080 case SpvOpAtomicUMax
:
3081 case SpvOpAtomicAnd
:
3083 case SpvOpAtomicXor
:
3084 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
3085 scope
= vtn_constant_uint(b
, w
[4]);
3086 semantics
= vtn_constant_uint(b
, w
[5]);
3089 case SpvOpAtomicStore
:
3090 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
3091 scope
= vtn_constant_uint(b
, w
[2]);
3092 semantics
= vtn_constant_uint(b
, w
[3]);
3096 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3099 /* uniform as "atomic counter uniform" */
3100 if (ptr
->mode
== vtn_variable_mode_uniform
) {
3101 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3102 const struct glsl_type
*deref_type
= deref
->type
;
3103 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
3104 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3105 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3107 /* SSBO needs to initialize index/offset. In this case we don't need to,
3108 * as that info is already stored on the ptr->var->var nir_variable (see
3109 * vtn_create_variable)
3113 case SpvOpAtomicLoad
:
3114 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3117 case SpvOpAtomicStore
:
3118 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3119 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3122 case SpvOpAtomicExchange
:
3123 case SpvOpAtomicCompareExchange
:
3124 case SpvOpAtomicCompareExchangeWeak
:
3125 case SpvOpAtomicIIncrement
:
3126 case SpvOpAtomicIDecrement
:
3127 case SpvOpAtomicIAdd
:
3128 case SpvOpAtomicISub
:
3129 case SpvOpAtomicSMin
:
3130 case SpvOpAtomicUMin
:
3131 case SpvOpAtomicSMax
:
3132 case SpvOpAtomicUMax
:
3133 case SpvOpAtomicAnd
:
3135 case SpvOpAtomicXor
:
3136 /* Nothing: we don't need to call fill_common_atomic_sources here, as
3137 * atomic counter uniforms doesn't have sources
3142 unreachable("Invalid SPIR-V atomic");
3145 } else if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
3146 nir_ssa_def
*offset
, *index
;
3147 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
3149 assert(ptr
->mode
== vtn_variable_mode_ssbo
);
3151 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(b
, opcode
);
3152 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3156 case SpvOpAtomicLoad
:
3157 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3158 nir_intrinsic_set_align(atomic
, 4, 0);
3159 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3160 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3161 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3164 case SpvOpAtomicStore
:
3165 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3166 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3167 nir_intrinsic_set_align(atomic
, 4, 0);
3168 atomic
->src
[src
++] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
3169 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3170 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3171 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3174 case SpvOpAtomicExchange
:
3175 case SpvOpAtomicCompareExchange
:
3176 case SpvOpAtomicCompareExchangeWeak
:
3177 case SpvOpAtomicIIncrement
:
3178 case SpvOpAtomicIDecrement
:
3179 case SpvOpAtomicIAdd
:
3180 case SpvOpAtomicISub
:
3181 case SpvOpAtomicSMin
:
3182 case SpvOpAtomicUMin
:
3183 case SpvOpAtomicSMax
:
3184 case SpvOpAtomicUMax
:
3185 case SpvOpAtomicAnd
:
3187 case SpvOpAtomicXor
:
3188 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3189 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3190 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3191 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
3195 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3198 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3199 const struct glsl_type
*deref_type
= deref
->type
;
3200 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
3201 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3202 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3205 case SpvOpAtomicLoad
:
3206 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3209 case SpvOpAtomicStore
:
3210 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3211 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3212 atomic
->src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
3215 case SpvOpAtomicExchange
:
3216 case SpvOpAtomicCompareExchange
:
3217 case SpvOpAtomicCompareExchangeWeak
:
3218 case SpvOpAtomicIIncrement
:
3219 case SpvOpAtomicIDecrement
:
3220 case SpvOpAtomicIAdd
:
3221 case SpvOpAtomicISub
:
3222 case SpvOpAtomicSMin
:
3223 case SpvOpAtomicUMin
:
3224 case SpvOpAtomicSMax
:
3225 case SpvOpAtomicUMax
:
3226 case SpvOpAtomicAnd
:
3228 case SpvOpAtomicXor
:
3229 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
3233 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3237 /* Atomic ordering operations will implicitly apply to the atomic operation
3238 * storage class, so include that too.
3240 semantics
|= vtn_storage_class_to_memory_semantics(ptr
->ptr_type
->storage_class
);
3242 SpvMemorySemanticsMask before_semantics
;
3243 SpvMemorySemanticsMask after_semantics
;
3244 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
3246 if (before_semantics
)
3247 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
3249 if (opcode
!= SpvOpAtomicStore
) {
3250 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3252 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
3253 glsl_get_vector_elements(type
->type
),
3254 glsl_get_bit_size(type
->type
), NULL
);
3256 struct vtn_ssa_value
*ssa
= rzalloc(b
, struct vtn_ssa_value
);
3257 ssa
->def
= &atomic
->dest
.ssa
;
3258 ssa
->type
= type
->type
;
3259 vtn_push_ssa(b
, w
[2], type
, ssa
);
3262 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
3264 if (after_semantics
)
3265 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
3268 static nir_alu_instr
*
3269 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
3271 nir_op op
= nir_op_vec(num_components
);
3272 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
3273 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
3275 vec
->dest
.write_mask
= (1 << num_components
) - 1;
3280 struct vtn_ssa_value
*
3281 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
3283 if (src
->transposed
)
3284 return src
->transposed
;
3286 struct vtn_ssa_value
*dest
=
3287 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
3289 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
3290 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
3291 glsl_get_bit_size(src
->type
));
3292 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3293 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
3294 vec
->src
[0].swizzle
[0] = i
;
3296 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
3297 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
3298 vec
->src
[j
].swizzle
[0] = i
;
3301 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3302 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
3305 dest
->transposed
= src
;
3310 static nir_ssa_def
*
3311 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
3312 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
3313 const uint32_t *indices
)
3315 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
3317 for (unsigned i
= 0; i
< num_components
; i
++) {
3318 uint32_t index
= indices
[i
];
3319 if (index
== 0xffffffff) {
3321 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
3322 } else if (index
< src0
->num_components
) {
3323 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
3324 vec
->src
[i
].swizzle
[0] = index
;
3326 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
3327 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
3331 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3333 return &vec
->dest
.dest
.ssa
;
3337 * Concatentates a number of vectors/scalars together to produce a vector
3339 static nir_ssa_def
*
3340 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
3341 unsigned num_srcs
, nir_ssa_def
**srcs
)
3343 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
3345 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3347 * "When constructing a vector, there must be at least two Constituent
3350 vtn_assert(num_srcs
>= 2);
3352 unsigned dest_idx
= 0;
3353 for (unsigned i
= 0; i
< num_srcs
; i
++) {
3354 nir_ssa_def
*src
= srcs
[i
];
3355 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
3356 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
3357 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
3358 vec
->src
[dest_idx
].swizzle
[0] = j
;
3363 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3365 * "When constructing a vector, the total number of components in all
3366 * the operands must equal the number of components in Result Type."
3368 vtn_assert(dest_idx
== num_components
);
3370 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3372 return &vec
->dest
.dest
.ssa
;
3375 static struct vtn_ssa_value
*
3376 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3378 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3379 dest
->type
= src
->type
;
3381 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3382 dest
->def
= src
->def
;
3384 unsigned elems
= glsl_get_length(src
->type
);
3386 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3387 for (unsigned i
= 0; i
< elems
; i
++)
3388 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3394 static struct vtn_ssa_value
*
3395 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3396 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3397 unsigned num_indices
)
3399 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3401 struct vtn_ssa_value
*cur
= dest
;
3403 for (i
= 0; i
< num_indices
- 1; i
++) {
3404 /* If we got a vector here, that means the next index will be trying to
3405 * dereference a scalar.
3407 vtn_fail_if(glsl_type_is_vector_or_scalar(cur
->type
),
3408 "OpCompositeInsert has too many indices.");
3409 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3410 "All indices in an OpCompositeInsert must be in-bounds");
3411 cur
= cur
->elems
[indices
[i
]];
3414 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3415 vtn_fail_if(indices
[i
] >= glsl_get_vector_elements(cur
->type
),
3416 "All indices in an OpCompositeInsert must be in-bounds");
3418 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3419 * the component granularity. In that case, the last index will be
3420 * the index to insert the scalar into the vector.
3423 cur
->def
= nir_vector_insert_imm(&b
->nb
, cur
->def
, insert
->def
, indices
[i
]);
3425 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3426 "All indices in an OpCompositeInsert must be in-bounds");
3427 cur
->elems
[indices
[i
]] = insert
;
3433 static struct vtn_ssa_value
*
3434 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3435 const uint32_t *indices
, unsigned num_indices
)
3437 struct vtn_ssa_value
*cur
= src
;
3438 for (unsigned i
= 0; i
< num_indices
; i
++) {
3439 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3440 vtn_assert(i
== num_indices
- 1);
3441 vtn_fail_if(indices
[i
] >= glsl_get_vector_elements(cur
->type
),
3442 "All indices in an OpCompositeExtract must be in-bounds");
3444 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3445 * the component granularity. The last index will be the index of the
3446 * vector to extract.
3449 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
3450 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
3451 ret
->def
= nir_channel(&b
->nb
, cur
->def
, indices
[i
]);
3454 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3455 "All indices in an OpCompositeExtract must be in-bounds");
3456 cur
= cur
->elems
[indices
[i
]];
3464 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3465 const uint32_t *w
, unsigned count
)
3467 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3468 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, type
->type
);
3471 case SpvOpVectorExtractDynamic
:
3472 ssa
->def
= nir_vector_extract(&b
->nb
, vtn_ssa_value(b
, w
[3])->def
,
3473 vtn_ssa_value(b
, w
[4])->def
);
3476 case SpvOpVectorInsertDynamic
:
3477 ssa
->def
= nir_vector_insert(&b
->nb
, vtn_ssa_value(b
, w
[3])->def
,
3478 vtn_ssa_value(b
, w
[4])->def
,
3479 vtn_ssa_value(b
, w
[5])->def
);
3482 case SpvOpVectorShuffle
:
3483 ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
->type
),
3484 vtn_ssa_value(b
, w
[3])->def
,
3485 vtn_ssa_value(b
, w
[4])->def
,
3489 case SpvOpCompositeConstruct
: {
3490 unsigned elems
= count
- 3;
3492 if (glsl_type_is_vector_or_scalar(type
->type
)) {
3493 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3494 for (unsigned i
= 0; i
< elems
; i
++)
3495 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
3497 vtn_vector_construct(b
, glsl_get_vector_elements(type
->type
),
3500 ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3501 for (unsigned i
= 0; i
< elems
; i
++)
3502 ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3506 case SpvOpCompositeExtract
:
3507 ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3511 case SpvOpCompositeInsert
:
3512 ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3513 vtn_ssa_value(b
, w
[3]),
3517 case SpvOpCopyLogical
:
3518 ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3520 case SpvOpCopyObject
:
3521 vtn_copy_value(b
, w
[3], w
[2]);
3525 vtn_fail_with_opcode("unknown composite operation", opcode
);
3528 vtn_push_ssa(b
, w
[2], type
, ssa
);
3532 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3534 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3535 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3539 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3540 SpvMemorySemanticsMask semantics
)
3542 if (b
->shader
->options
->use_scoped_barrier
) {
3543 vtn_emit_scoped_memory_barrier(b
, scope
, semantics
);
3547 static const SpvMemorySemanticsMask all_memory_semantics
=
3548 SpvMemorySemanticsUniformMemoryMask
|
3549 SpvMemorySemanticsWorkgroupMemoryMask
|
3550 SpvMemorySemanticsAtomicCounterMemoryMask
|
3551 SpvMemorySemanticsImageMemoryMask
|
3552 SpvMemorySemanticsOutputMemoryMask
;
3554 /* If we're not actually doing a memory barrier, bail */
3555 if (!(semantics
& all_memory_semantics
))
3558 /* GL and Vulkan don't have these */
3559 vtn_assert(scope
!= SpvScopeCrossDevice
);
3561 if (scope
== SpvScopeSubgroup
)
3562 return; /* Nothing to do here */
3564 if (scope
== SpvScopeWorkgroup
) {
3565 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3569 /* There's only two scopes thing left */
3570 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3572 /* Map the GLSL memoryBarrier() construct to the corresponding NIR one. */
3573 static const SpvMemorySemanticsMask glsl_memory_barrier
=
3574 SpvMemorySemanticsUniformMemoryMask
|
3575 SpvMemorySemanticsWorkgroupMemoryMask
|
3576 SpvMemorySemanticsImageMemoryMask
;
3577 if ((semantics
& glsl_memory_barrier
) == glsl_memory_barrier
) {
3578 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3579 semantics
&= ~(glsl_memory_barrier
| SpvMemorySemanticsAtomicCounterMemoryMask
);
3582 /* Issue a bunch of more specific barriers */
3583 uint32_t bits
= semantics
;
3585 SpvMemorySemanticsMask semantic
= 1 << u_bit_scan(&bits
);
3587 case SpvMemorySemanticsUniformMemoryMask
:
3588 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3590 case SpvMemorySemanticsWorkgroupMemoryMask
:
3591 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3593 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3594 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3596 case SpvMemorySemanticsImageMemoryMask
:
3597 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3599 case SpvMemorySemanticsOutputMemoryMask
:
3600 if (b
->nb
.shader
->info
.stage
== MESA_SHADER_TESS_CTRL
)
3601 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_tcs_patch
);
3610 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3611 const uint32_t *w
, UNUSED
unsigned count
)
3614 case SpvOpEmitVertex
:
3615 case SpvOpEmitStreamVertex
:
3616 case SpvOpEndPrimitive
:
3617 case SpvOpEndStreamPrimitive
: {
3618 nir_intrinsic_op intrinsic_op
;
3620 case SpvOpEmitVertex
:
3621 case SpvOpEmitStreamVertex
:
3622 intrinsic_op
= nir_intrinsic_emit_vertex
;
3624 case SpvOpEndPrimitive
:
3625 case SpvOpEndStreamPrimitive
:
3626 intrinsic_op
= nir_intrinsic_end_primitive
;
3629 unreachable("Invalid opcode");
3632 nir_intrinsic_instr
*intrin
=
3633 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3636 case SpvOpEmitStreamVertex
:
3637 case SpvOpEndStreamPrimitive
: {
3638 unsigned stream
= vtn_constant_uint(b
, w
[1]);
3639 nir_intrinsic_set_stream_id(intrin
, stream
);
3647 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3651 case SpvOpMemoryBarrier
: {
3652 SpvScope scope
= vtn_constant_uint(b
, w
[1]);
3653 SpvMemorySemanticsMask semantics
= vtn_constant_uint(b
, w
[2]);
3654 vtn_emit_memory_barrier(b
, scope
, semantics
);
3658 case SpvOpControlBarrier
: {
3659 SpvScope execution_scope
= vtn_constant_uint(b
, w
[1]);
3660 SpvScope memory_scope
= vtn_constant_uint(b
, w
[2]);
3661 SpvMemorySemanticsMask memory_semantics
= vtn_constant_uint(b
, w
[3]);
3663 /* GLSLang, prior to commit 8297936dd6eb3, emitted OpControlBarrier with
3664 * memory semantics of None for GLSL barrier().
3665 * And before that, prior to c3f1cdfa, emitted the OpControlBarrier with
3666 * Device instead of Workgroup for execution scope.
3668 if (b
->wa_glslang_cs_barrier
&&
3669 b
->nb
.shader
->info
.stage
== MESA_SHADER_COMPUTE
&&
3670 (execution_scope
== SpvScopeWorkgroup
||
3671 execution_scope
== SpvScopeDevice
) &&
3672 memory_semantics
== SpvMemorySemanticsMaskNone
) {
3673 execution_scope
= SpvScopeWorkgroup
;
3674 memory_scope
= SpvScopeWorkgroup
;
3675 memory_semantics
= SpvMemorySemanticsAcquireReleaseMask
|
3676 SpvMemorySemanticsWorkgroupMemoryMask
;
3679 /* From the SPIR-V spec:
3681 * "When used with the TessellationControl execution model, it also
3682 * implicitly synchronizes the Output Storage Class: Writes to Output
3683 * variables performed by any invocation executed prior to a
3684 * OpControlBarrier will be visible to any other invocation after
3685 * return from that OpControlBarrier."
3687 if (b
->nb
.shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
3688 memory_semantics
&= ~(SpvMemorySemanticsAcquireMask
|
3689 SpvMemorySemanticsReleaseMask
|
3690 SpvMemorySemanticsAcquireReleaseMask
|
3691 SpvMemorySemanticsSequentiallyConsistentMask
);
3692 memory_semantics
|= SpvMemorySemanticsAcquireReleaseMask
|
3693 SpvMemorySemanticsOutputMemoryMask
;
3696 if (b
->shader
->options
->use_scoped_barrier
) {
3697 vtn_emit_scoped_control_barrier(b
, execution_scope
, memory_scope
,
3700 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
3702 if (execution_scope
== SpvScopeWorkgroup
)
3703 vtn_emit_barrier(b
, nir_intrinsic_control_barrier
);
3709 unreachable("unknown barrier instruction");
3714 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
3715 SpvExecutionMode mode
)
3718 case SpvExecutionModeInputPoints
:
3719 case SpvExecutionModeOutputPoints
:
3720 return 0; /* GL_POINTS */
3721 case SpvExecutionModeInputLines
:
3722 return 1; /* GL_LINES */
3723 case SpvExecutionModeInputLinesAdjacency
:
3724 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3725 case SpvExecutionModeTriangles
:
3726 return 4; /* GL_TRIANGLES */
3727 case SpvExecutionModeInputTrianglesAdjacency
:
3728 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3729 case SpvExecutionModeQuads
:
3730 return 7; /* GL_QUADS */
3731 case SpvExecutionModeIsolines
:
3732 return 0x8E7A; /* GL_ISOLINES */
3733 case SpvExecutionModeOutputLineStrip
:
3734 return 3; /* GL_LINE_STRIP */
3735 case SpvExecutionModeOutputTriangleStrip
:
3736 return 5; /* GL_TRIANGLE_STRIP */
3738 vtn_fail("Invalid primitive type: %s (%u)",
3739 spirv_executionmode_to_string(mode
), mode
);
3744 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
3745 SpvExecutionMode mode
)
3748 case SpvExecutionModeInputPoints
:
3750 case SpvExecutionModeInputLines
:
3752 case SpvExecutionModeInputLinesAdjacency
:
3754 case SpvExecutionModeTriangles
:
3756 case SpvExecutionModeInputTrianglesAdjacency
:
3759 vtn_fail("Invalid GS input mode: %s (%u)",
3760 spirv_executionmode_to_string(mode
), mode
);
3764 static gl_shader_stage
3765 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
3768 case SpvExecutionModelVertex
:
3769 return MESA_SHADER_VERTEX
;
3770 case SpvExecutionModelTessellationControl
:
3771 return MESA_SHADER_TESS_CTRL
;
3772 case SpvExecutionModelTessellationEvaluation
:
3773 return MESA_SHADER_TESS_EVAL
;
3774 case SpvExecutionModelGeometry
:
3775 return MESA_SHADER_GEOMETRY
;
3776 case SpvExecutionModelFragment
:
3777 return MESA_SHADER_FRAGMENT
;
3778 case SpvExecutionModelGLCompute
:
3779 return MESA_SHADER_COMPUTE
;
3780 case SpvExecutionModelKernel
:
3781 return MESA_SHADER_KERNEL
;
3783 vtn_fail("Unsupported execution model: %s (%u)",
3784 spirv_executionmodel_to_string(model
), model
);
3788 #define spv_check_supported(name, cap) do { \
3789 if (!(b->options && b->options->caps.name)) \
3790 vtn_warn("Unsupported SPIR-V capability: %s (%u)", \
3791 spirv_capability_to_string(cap), cap); \
3796 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
3799 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
3800 /* Let this be a name label regardless */
3801 unsigned name_words
;
3802 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
3804 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
3805 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
3808 vtn_assert(b
->entry_point
== NULL
);
3809 b
->entry_point
= entry_point
;
3813 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3814 const uint32_t *w
, unsigned count
)
3821 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
3822 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
3823 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
3824 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
3825 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
3826 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
3829 uint32_t version
= w
[2];
3832 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
3834 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
3838 case SpvOpSourceExtension
:
3839 case SpvOpSourceContinued
:
3840 case SpvOpExtension
:
3841 case SpvOpModuleProcessed
:
3842 /* Unhandled, but these are for debug so that's ok. */
3845 case SpvOpCapability
: {
3846 SpvCapability cap
= w
[1];
3848 case SpvCapabilityMatrix
:
3849 case SpvCapabilityShader
:
3850 case SpvCapabilityGeometry
:
3851 case SpvCapabilityGeometryPointSize
:
3852 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
3853 case SpvCapabilitySampledImageArrayDynamicIndexing
:
3854 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
3855 case SpvCapabilityStorageImageArrayDynamicIndexing
:
3856 case SpvCapabilityImageRect
:
3857 case SpvCapabilitySampledRect
:
3858 case SpvCapabilitySampled1D
:
3859 case SpvCapabilityImage1D
:
3860 case SpvCapabilitySampledCubeArray
:
3861 case SpvCapabilityImageCubeArray
:
3862 case SpvCapabilitySampledBuffer
:
3863 case SpvCapabilityImageBuffer
:
3864 case SpvCapabilityImageQuery
:
3865 case SpvCapabilityDerivativeControl
:
3866 case SpvCapabilityInterpolationFunction
:
3867 case SpvCapabilityMultiViewport
:
3868 case SpvCapabilitySampleRateShading
:
3869 case SpvCapabilityClipDistance
:
3870 case SpvCapabilityCullDistance
:
3871 case SpvCapabilityInputAttachment
:
3872 case SpvCapabilityImageGatherExtended
:
3873 case SpvCapabilityStorageImageExtendedFormats
:
3874 case SpvCapabilityVector16
:
3877 case SpvCapabilityLinkage
:
3878 case SpvCapabilityFloat16Buffer
:
3879 case SpvCapabilitySparseResidency
:
3880 vtn_warn("Unsupported SPIR-V capability: %s",
3881 spirv_capability_to_string(cap
));
3884 case SpvCapabilityMinLod
:
3885 spv_check_supported(min_lod
, cap
);
3888 case SpvCapabilityAtomicStorage
:
3889 spv_check_supported(atomic_storage
, cap
);
3892 case SpvCapabilityFloat64
:
3893 spv_check_supported(float64
, cap
);
3895 case SpvCapabilityInt64
:
3896 spv_check_supported(int64
, cap
);
3898 case SpvCapabilityInt16
:
3899 spv_check_supported(int16
, cap
);
3901 case SpvCapabilityInt8
:
3902 spv_check_supported(int8
, cap
);
3905 case SpvCapabilityTransformFeedback
:
3906 spv_check_supported(transform_feedback
, cap
);
3909 case SpvCapabilityGeometryStreams
:
3910 spv_check_supported(geometry_streams
, cap
);
3913 case SpvCapabilityInt64Atomics
:
3914 spv_check_supported(int64_atomics
, cap
);
3917 case SpvCapabilityStorageImageMultisample
:
3918 spv_check_supported(storage_image_ms
, cap
);
3921 case SpvCapabilityAddresses
:
3922 spv_check_supported(address
, cap
);
3925 case SpvCapabilityKernel
:
3926 spv_check_supported(kernel
, cap
);
3929 case SpvCapabilityImageBasic
:
3930 case SpvCapabilityImageReadWrite
:
3931 case SpvCapabilityImageMipmap
:
3932 case SpvCapabilityPipes
:
3933 case SpvCapabilityDeviceEnqueue
:
3934 case SpvCapabilityLiteralSampler
:
3935 case SpvCapabilityGenericPointer
:
3936 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
3937 spirv_capability_to_string(cap
));
3940 case SpvCapabilityImageMSArray
:
3941 spv_check_supported(image_ms_array
, cap
);
3944 case SpvCapabilityTessellation
:
3945 case SpvCapabilityTessellationPointSize
:
3946 spv_check_supported(tessellation
, cap
);
3949 case SpvCapabilityDrawParameters
:
3950 spv_check_supported(draw_parameters
, cap
);
3953 case SpvCapabilityStorageImageReadWithoutFormat
:
3954 spv_check_supported(image_read_without_format
, cap
);
3957 case SpvCapabilityStorageImageWriteWithoutFormat
:
3958 spv_check_supported(image_write_without_format
, cap
);
3961 case SpvCapabilityDeviceGroup
:
3962 spv_check_supported(device_group
, cap
);
3965 case SpvCapabilityMultiView
:
3966 spv_check_supported(multiview
, cap
);
3969 case SpvCapabilityGroupNonUniform
:
3970 spv_check_supported(subgroup_basic
, cap
);
3973 case SpvCapabilitySubgroupVoteKHR
:
3974 case SpvCapabilityGroupNonUniformVote
:
3975 spv_check_supported(subgroup_vote
, cap
);
3978 case SpvCapabilitySubgroupBallotKHR
:
3979 case SpvCapabilityGroupNonUniformBallot
:
3980 spv_check_supported(subgroup_ballot
, cap
);
3983 case SpvCapabilityGroupNonUniformShuffle
:
3984 case SpvCapabilityGroupNonUniformShuffleRelative
:
3985 spv_check_supported(subgroup_shuffle
, cap
);
3988 case SpvCapabilityGroupNonUniformQuad
:
3989 spv_check_supported(subgroup_quad
, cap
);
3992 case SpvCapabilityGroupNonUniformArithmetic
:
3993 case SpvCapabilityGroupNonUniformClustered
:
3994 spv_check_supported(subgroup_arithmetic
, cap
);
3997 case SpvCapabilityGroups
:
3998 spv_check_supported(amd_shader_ballot
, cap
);
4001 case SpvCapabilityVariablePointersStorageBuffer
:
4002 case SpvCapabilityVariablePointers
:
4003 spv_check_supported(variable_pointers
, cap
);
4004 b
->variable_pointers
= true;
4007 case SpvCapabilityStorageUniformBufferBlock16
:
4008 case SpvCapabilityStorageUniform16
:
4009 case SpvCapabilityStoragePushConstant16
:
4010 case SpvCapabilityStorageInputOutput16
:
4011 spv_check_supported(storage_16bit
, cap
);
4014 case SpvCapabilityShaderLayer
:
4015 case SpvCapabilityShaderViewportIndex
:
4016 case SpvCapabilityShaderViewportIndexLayerEXT
:
4017 spv_check_supported(shader_viewport_index_layer
, cap
);
4020 case SpvCapabilityStorageBuffer8BitAccess
:
4021 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
4022 case SpvCapabilityStoragePushConstant8
:
4023 spv_check_supported(storage_8bit
, cap
);
4026 case SpvCapabilityShaderNonUniformEXT
:
4027 spv_check_supported(descriptor_indexing
, cap
);
4030 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
4031 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
4032 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
4033 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
4036 case SpvCapabilityUniformBufferArrayNonUniformIndexingEXT
:
4037 case SpvCapabilitySampledImageArrayNonUniformIndexingEXT
:
4038 case SpvCapabilityStorageBufferArrayNonUniformIndexingEXT
:
4039 case SpvCapabilityStorageImageArrayNonUniformIndexingEXT
:
4040 case SpvCapabilityInputAttachmentArrayNonUniformIndexingEXT
:
4041 case SpvCapabilityUniformTexelBufferArrayNonUniformIndexingEXT
:
4042 case SpvCapabilityStorageTexelBufferArrayNonUniformIndexingEXT
:
4043 spv_check_supported(descriptor_array_non_uniform_indexing
, cap
);
4046 case SpvCapabilityRuntimeDescriptorArrayEXT
:
4047 spv_check_supported(runtime_descriptor_array
, cap
);
4050 case SpvCapabilityStencilExportEXT
:
4051 spv_check_supported(stencil_export
, cap
);
4054 case SpvCapabilitySampleMaskPostDepthCoverage
:
4055 spv_check_supported(post_depth_coverage
, cap
);
4058 case SpvCapabilityDenormFlushToZero
:
4059 case SpvCapabilityDenormPreserve
:
4060 case SpvCapabilitySignedZeroInfNanPreserve
:
4061 case SpvCapabilityRoundingModeRTE
:
4062 case SpvCapabilityRoundingModeRTZ
:
4063 spv_check_supported(float_controls
, cap
);
4066 case SpvCapabilityPhysicalStorageBufferAddresses
:
4067 spv_check_supported(physical_storage_buffer_address
, cap
);
4070 case SpvCapabilityComputeDerivativeGroupQuadsNV
:
4071 case SpvCapabilityComputeDerivativeGroupLinearNV
:
4072 spv_check_supported(derivative_group
, cap
);
4075 case SpvCapabilityFloat16
:
4076 spv_check_supported(float16
, cap
);
4079 case SpvCapabilityFragmentShaderSampleInterlockEXT
:
4080 spv_check_supported(fragment_shader_sample_interlock
, cap
);
4083 case SpvCapabilityFragmentShaderPixelInterlockEXT
:
4084 spv_check_supported(fragment_shader_pixel_interlock
, cap
);
4087 case SpvCapabilityDemoteToHelperInvocationEXT
:
4088 spv_check_supported(demote_to_helper_invocation
, cap
);
4091 case SpvCapabilityShaderClockKHR
:
4092 spv_check_supported(shader_clock
, cap
);
4095 case SpvCapabilityVulkanMemoryModel
:
4096 spv_check_supported(vk_memory_model
, cap
);
4099 case SpvCapabilityVulkanMemoryModelDeviceScope
:
4100 spv_check_supported(vk_memory_model_device_scope
, cap
);
4103 case SpvCapabilityImageReadWriteLodAMD
:
4104 spv_check_supported(amd_image_read_write_lod
, cap
);
4107 case SpvCapabilityIntegerFunctions2INTEL
:
4108 spv_check_supported(integer_functions2
, cap
);
4111 case SpvCapabilityFragmentMaskAMD
:
4112 spv_check_supported(amd_fragment_mask
, cap
);
4115 case SpvCapabilityImageGatherBiasLodAMD
:
4116 spv_check_supported(amd_image_gather_bias_lod
, cap
);
4120 vtn_fail("Unhandled capability: %s (%u)",
4121 spirv_capability_to_string(cap
), cap
);
4126 case SpvOpExtInstImport
:
4127 vtn_handle_extension(b
, opcode
, w
, count
);
4130 case SpvOpMemoryModel
:
4132 case SpvAddressingModelPhysical32
:
4133 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4134 "AddressingModelPhysical32 only supported for kernels");
4135 b
->shader
->info
.cs
.ptr_size
= 32;
4136 b
->physical_ptrs
= true;
4137 b
->options
->shared_addr_format
= nir_address_format_32bit_global
;
4138 b
->options
->global_addr_format
= nir_address_format_32bit_global
;
4139 b
->options
->temp_addr_format
= nir_address_format_32bit_global
;
4141 case SpvAddressingModelPhysical64
:
4142 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4143 "AddressingModelPhysical64 only supported for kernels");
4144 b
->shader
->info
.cs
.ptr_size
= 64;
4145 b
->physical_ptrs
= true;
4146 b
->options
->shared_addr_format
= nir_address_format_64bit_global
;
4147 b
->options
->global_addr_format
= nir_address_format_64bit_global
;
4148 b
->options
->temp_addr_format
= nir_address_format_64bit_global
;
4150 case SpvAddressingModelLogical
:
4151 vtn_fail_if(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
,
4152 "AddressingModelLogical only supported for shaders");
4153 b
->physical_ptrs
= false;
4155 case SpvAddressingModelPhysicalStorageBuffer64
:
4156 vtn_fail_if(!b
->options
||
4157 !b
->options
->caps
.physical_storage_buffer_address
,
4158 "AddressingModelPhysicalStorageBuffer64 not supported");
4161 vtn_fail("Unknown addressing model: %s (%u)",
4162 spirv_addressingmodel_to_string(w
[1]), w
[1]);
4167 case SpvMemoryModelSimple
:
4168 case SpvMemoryModelGLSL450
:
4169 case SpvMemoryModelOpenCL
:
4171 case SpvMemoryModelVulkan
:
4172 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
4173 "Vulkan memory model is unsupported by this driver");
4176 vtn_fail("Unsupported memory model: %s",
4177 spirv_memorymodel_to_string(w
[2]));
4182 case SpvOpEntryPoint
:
4183 vtn_handle_entry_point(b
, w
, count
);
4187 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
4188 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4192 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4195 case SpvOpMemberName
:
4199 case SpvOpExecutionMode
:
4200 case SpvOpExecutionModeId
:
4201 case SpvOpDecorationGroup
:
4203 case SpvOpDecorateId
:
4204 case SpvOpMemberDecorate
:
4205 case SpvOpGroupDecorate
:
4206 case SpvOpGroupMemberDecorate
:
4207 case SpvOpDecorateString
:
4208 case SpvOpMemberDecorateString
:
4209 vtn_handle_decoration(b
, opcode
, w
, count
);
4212 case SpvOpExtInst
: {
4213 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4214 if (val
->ext_handler
== vtn_handle_non_semantic_instruction
) {
4215 /* NonSemantic extended instructions are acceptable in preamble. */
4216 vtn_handle_non_semantic_instruction(b
, w
[4], w
, count
);
4219 return false; /* End of preamble. */
4224 return false; /* End of preamble */
4231 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4232 const struct vtn_decoration
*mode
, UNUSED
void *data
)
4234 vtn_assert(b
->entry_point
== entry_point
);
4236 switch(mode
->exec_mode
) {
4237 case SpvExecutionModeOriginUpperLeft
:
4238 case SpvExecutionModeOriginLowerLeft
:
4239 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4240 b
->shader
->info
.fs
.origin_upper_left
=
4241 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
4244 case SpvExecutionModeEarlyFragmentTests
:
4245 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4246 b
->shader
->info
.fs
.early_fragment_tests
= true;
4249 case SpvExecutionModePostDepthCoverage
:
4250 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4251 b
->shader
->info
.fs
.post_depth_coverage
= true;
4254 case SpvExecutionModeInvocations
:
4255 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4256 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->operands
[0]);
4259 case SpvExecutionModeDepthReplacing
:
4260 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4261 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
4263 case SpvExecutionModeDepthGreater
:
4264 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4265 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
4267 case SpvExecutionModeDepthLess
:
4268 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4269 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
4271 case SpvExecutionModeDepthUnchanged
:
4272 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4273 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
4276 case SpvExecutionModeLocalSize
:
4277 vtn_assert(gl_shader_stage_is_compute(b
->shader
->info
.stage
));
4278 b
->shader
->info
.cs
.local_size
[0] = mode
->operands
[0];
4279 b
->shader
->info
.cs
.local_size
[1] = mode
->operands
[1];
4280 b
->shader
->info
.cs
.local_size
[2] = mode
->operands
[2];
4283 case SpvExecutionModeLocalSizeId
:
4284 b
->shader
->info
.cs
.local_size
[0] = vtn_constant_uint(b
, mode
->operands
[0]);
4285 b
->shader
->info
.cs
.local_size
[1] = vtn_constant_uint(b
, mode
->operands
[1]);
4286 b
->shader
->info
.cs
.local_size
[2] = vtn_constant_uint(b
, mode
->operands
[2]);
4289 case SpvExecutionModeLocalSizeHint
:
4290 case SpvExecutionModeLocalSizeHintId
:
4291 break; /* Nothing to do with this */
4293 case SpvExecutionModeOutputVertices
:
4294 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4295 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4296 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->operands
[0];
4298 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4299 b
->shader
->info
.gs
.vertices_out
= mode
->operands
[0];
4303 case SpvExecutionModeInputPoints
:
4304 case SpvExecutionModeInputLines
:
4305 case SpvExecutionModeInputLinesAdjacency
:
4306 case SpvExecutionModeTriangles
:
4307 case SpvExecutionModeInputTrianglesAdjacency
:
4308 case SpvExecutionModeQuads
:
4309 case SpvExecutionModeIsolines
:
4310 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4311 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4312 b
->shader
->info
.tess
.primitive_mode
=
4313 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4315 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4316 b
->shader
->info
.gs
.vertices_in
=
4317 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
4318 b
->shader
->info
.gs
.input_primitive
=
4319 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4323 case SpvExecutionModeOutputPoints
:
4324 case SpvExecutionModeOutputLineStrip
:
4325 case SpvExecutionModeOutputTriangleStrip
:
4326 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4327 b
->shader
->info
.gs
.output_primitive
=
4328 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4331 case SpvExecutionModeSpacingEqual
:
4332 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4333 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4334 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
4336 case SpvExecutionModeSpacingFractionalEven
:
4337 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4338 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4339 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
4341 case SpvExecutionModeSpacingFractionalOdd
:
4342 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4343 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4344 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
4346 case SpvExecutionModeVertexOrderCw
:
4347 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4348 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4349 b
->shader
->info
.tess
.ccw
= false;
4351 case SpvExecutionModeVertexOrderCcw
:
4352 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4353 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4354 b
->shader
->info
.tess
.ccw
= true;
4356 case SpvExecutionModePointMode
:
4357 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4358 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4359 b
->shader
->info
.tess
.point_mode
= true;
4362 case SpvExecutionModePixelCenterInteger
:
4363 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4364 b
->shader
->info
.fs
.pixel_center_integer
= true;
4367 case SpvExecutionModeXfb
:
4368 b
->shader
->info
.has_transform_feedback_varyings
= true;
4371 case SpvExecutionModeVecTypeHint
:
4374 case SpvExecutionModeContractionOff
:
4375 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
4376 vtn_warn("ExectionMode only allowed for CL-style kernels: %s",
4377 spirv_executionmode_to_string(mode
->exec_mode
));
4382 case SpvExecutionModeStencilRefReplacingEXT
:
4383 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4386 case SpvExecutionModeDerivativeGroupQuadsNV
:
4387 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4388 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_QUADS
;
4391 case SpvExecutionModeDerivativeGroupLinearNV
:
4392 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4393 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_LINEAR
;
4396 case SpvExecutionModePixelInterlockOrderedEXT
:
4397 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4398 b
->shader
->info
.fs
.pixel_interlock_ordered
= true;
4401 case SpvExecutionModePixelInterlockUnorderedEXT
:
4402 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4403 b
->shader
->info
.fs
.pixel_interlock_unordered
= true;
4406 case SpvExecutionModeSampleInterlockOrderedEXT
:
4407 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4408 b
->shader
->info
.fs
.sample_interlock_ordered
= true;
4411 case SpvExecutionModeSampleInterlockUnorderedEXT
:
4412 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4413 b
->shader
->info
.fs
.sample_interlock_unordered
= true;
4416 case SpvExecutionModeDenormPreserve
:
4417 case SpvExecutionModeDenormFlushToZero
:
4418 case SpvExecutionModeSignedZeroInfNanPreserve
:
4419 case SpvExecutionModeRoundingModeRTE
:
4420 case SpvExecutionModeRoundingModeRTZ
:
4421 /* Already handled in vtn_handle_rounding_mode_in_execution_mode() */
4425 vtn_fail("Unhandled execution mode: %s (%u)",
4426 spirv_executionmode_to_string(mode
->exec_mode
),
4432 vtn_handle_rounding_mode_in_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4433 const struct vtn_decoration
*mode
, void *data
)
4435 vtn_assert(b
->entry_point
== entry_point
);
4437 unsigned execution_mode
= 0;
4439 switch(mode
->exec_mode
) {
4440 case SpvExecutionModeDenormPreserve
:
4441 switch (mode
->operands
[0]) {
4442 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP16
; break;
4443 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP32
; break;
4444 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP64
; break;
4445 default: vtn_fail("Floating point type not supported");
4448 case SpvExecutionModeDenormFlushToZero
:
4449 switch (mode
->operands
[0]) {
4450 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
; break;
4451 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32
; break;
4452 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64
; break;
4453 default: vtn_fail("Floating point type not supported");
4456 case SpvExecutionModeSignedZeroInfNanPreserve
:
4457 switch (mode
->operands
[0]) {
4458 case 16: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16
; break;
4459 case 32: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32
; break;
4460 case 64: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64
; break;
4461 default: vtn_fail("Floating point type not supported");
4464 case SpvExecutionModeRoundingModeRTE
:
4465 switch (mode
->operands
[0]) {
4466 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16
; break;
4467 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32
; break;
4468 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64
; break;
4469 default: vtn_fail("Floating point type not supported");
4472 case SpvExecutionModeRoundingModeRTZ
:
4473 switch (mode
->operands
[0]) {
4474 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
; break;
4475 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
; break;
4476 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
; break;
4477 default: vtn_fail("Floating point type not supported");
4485 b
->shader
->info
.float_controls_execution_mode
|= execution_mode
;
4489 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4490 const uint32_t *w
, unsigned count
)
4492 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
4496 case SpvOpSourceContinued
:
4497 case SpvOpSourceExtension
:
4498 case SpvOpExtension
:
4499 case SpvOpCapability
:
4500 case SpvOpExtInstImport
:
4501 case SpvOpMemoryModel
:
4502 case SpvOpEntryPoint
:
4503 case SpvOpExecutionMode
:
4506 case SpvOpMemberName
:
4507 case SpvOpDecorationGroup
:
4509 case SpvOpDecorateId
:
4510 case SpvOpMemberDecorate
:
4511 case SpvOpGroupDecorate
:
4512 case SpvOpGroupMemberDecorate
:
4513 case SpvOpDecorateString
:
4514 case SpvOpMemberDecorateString
:
4515 vtn_fail("Invalid opcode types and variables section");
4521 case SpvOpTypeFloat
:
4522 case SpvOpTypeVector
:
4523 case SpvOpTypeMatrix
:
4524 case SpvOpTypeImage
:
4525 case SpvOpTypeSampler
:
4526 case SpvOpTypeSampledImage
:
4527 case SpvOpTypeArray
:
4528 case SpvOpTypeRuntimeArray
:
4529 case SpvOpTypeStruct
:
4530 case SpvOpTypeOpaque
:
4531 case SpvOpTypePointer
:
4532 case SpvOpTypeForwardPointer
:
4533 case SpvOpTypeFunction
:
4534 case SpvOpTypeEvent
:
4535 case SpvOpTypeDeviceEvent
:
4536 case SpvOpTypeReserveId
:
4537 case SpvOpTypeQueue
:
4539 vtn_handle_type(b
, opcode
, w
, count
);
4542 case SpvOpConstantTrue
:
4543 case SpvOpConstantFalse
:
4545 case SpvOpConstantComposite
:
4546 case SpvOpConstantSampler
:
4547 case SpvOpConstantNull
:
4548 case SpvOpSpecConstantTrue
:
4549 case SpvOpSpecConstantFalse
:
4550 case SpvOpSpecConstant
:
4551 case SpvOpSpecConstantComposite
:
4552 case SpvOpSpecConstantOp
:
4553 vtn_handle_constant(b
, opcode
, w
, count
);
4558 vtn_handle_variables(b
, opcode
, w
, count
);
4561 case SpvOpExtInst
: {
4562 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4563 /* NonSemantic extended instructions are acceptable in preamble, others
4564 * will indicate the end of preamble.
4566 return val
->ext_handler
== vtn_handle_non_semantic_instruction
;
4570 return false; /* End of preamble */
4576 static struct vtn_ssa_value
*
4577 vtn_nir_select(struct vtn_builder
*b
, struct vtn_ssa_value
*src0
,
4578 struct vtn_ssa_value
*src1
, struct vtn_ssa_value
*src2
)
4580 struct vtn_ssa_value
*dest
= rzalloc(b
, struct vtn_ssa_value
);
4581 dest
->type
= src1
->type
;
4583 if (glsl_type_is_vector_or_scalar(src1
->type
)) {
4584 dest
->def
= nir_bcsel(&b
->nb
, src0
->def
, src1
->def
, src2
->def
);
4586 unsigned elems
= glsl_get_length(src1
->type
);
4588 dest
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
4589 for (unsigned i
= 0; i
< elems
; i
++) {
4590 dest
->elems
[i
] = vtn_nir_select(b
, src0
,
4591 src1
->elems
[i
], src2
->elems
[i
]);
4599 vtn_handle_select(struct vtn_builder
*b
, SpvOp opcode
,
4600 const uint32_t *w
, unsigned count
)
4602 /* Handle OpSelect up-front here because it needs to be able to handle
4603 * pointers and not just regular vectors and scalars.
4605 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
4606 struct vtn_value
*cond_val
= vtn_untyped_value(b
, w
[3]);
4607 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
4608 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
4610 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4611 obj2_val
->type
!= res_val
->type
,
4612 "Object types must match the result type in OpSelect");
4614 vtn_fail_if((cond_val
->type
->base_type
!= vtn_base_type_scalar
&&
4615 cond_val
->type
->base_type
!= vtn_base_type_vector
) ||
4616 !glsl_type_is_boolean(cond_val
->type
->type
),
4617 "OpSelect must have either a vector of booleans or "
4618 "a boolean as Condition type");
4620 vtn_fail_if(cond_val
->type
->base_type
== vtn_base_type_vector
&&
4621 (res_val
->type
->base_type
!= vtn_base_type_vector
||
4622 res_val
->type
->length
!= cond_val
->type
->length
),
4623 "When Condition type in OpSelect is a vector, the Result "
4624 "type must be a vector of the same length");
4626 switch (res_val
->type
->base_type
) {
4627 case vtn_base_type_scalar
:
4628 case vtn_base_type_vector
:
4629 case vtn_base_type_matrix
:
4630 case vtn_base_type_array
:
4631 case vtn_base_type_struct
:
4634 case vtn_base_type_pointer
:
4635 /* We need to have actual storage for pointer types. */
4636 vtn_fail_if(res_val
->type
->type
== NULL
,
4637 "Invalid pointer result type for OpSelect");
4640 vtn_fail("Result type of OpSelect must be a scalar, composite, or pointer");
4643 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4644 struct vtn_ssa_value
*ssa
= vtn_nir_select(b
,
4645 vtn_ssa_value(b
, w
[3]), vtn_ssa_value(b
, w
[4]), vtn_ssa_value(b
, w
[5]));
4647 vtn_push_ssa(b
, w
[2], res_type
, ssa
);
4651 vtn_handle_ptr(struct vtn_builder
*b
, SpvOp opcode
,
4652 const uint32_t *w
, unsigned count
)
4654 struct vtn_type
*type1
= vtn_untyped_value(b
, w
[3])->type
;
4655 struct vtn_type
*type2
= vtn_untyped_value(b
, w
[4])->type
;
4656 vtn_fail_if(type1
->base_type
!= vtn_base_type_pointer
||
4657 type2
->base_type
!= vtn_base_type_pointer
,
4658 "%s operands must have pointer types",
4659 spirv_op_to_string(opcode
));
4660 vtn_fail_if(type1
->storage_class
!= type2
->storage_class
,
4661 "%s operands must have the same storage class",
4662 spirv_op_to_string(opcode
));
4664 struct vtn_type
*vtn_type
=
4665 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4666 const struct glsl_type
*type
= vtn_type
->type
;
4668 nir_address_format addr_format
= vtn_mode_to_address_format(
4669 b
, vtn_storage_class_to_mode(b
, type1
->storage_class
, NULL
, NULL
));
4674 case SpvOpPtrDiff
: {
4675 /* OpPtrDiff returns the difference in number of elements (not byte offset). */
4676 unsigned elem_size
, elem_align
;
4677 glsl_get_natural_size_align_bytes(type1
->deref
->type
,
4678 &elem_size
, &elem_align
);
4680 def
= nir_build_addr_isub(&b
->nb
,
4681 vtn_ssa_value(b
, w
[3])->def
,
4682 vtn_ssa_value(b
, w
[4])->def
,
4684 def
= nir_idiv(&b
->nb
, def
, nir_imm_intN_t(&b
->nb
, elem_size
, def
->bit_size
));
4685 def
= nir_i2i(&b
->nb
, def
, glsl_get_bit_size(type
));
4690 case SpvOpPtrNotEqual
: {
4691 def
= nir_build_addr_ieq(&b
->nb
,
4692 vtn_ssa_value(b
, w
[3])->def
,
4693 vtn_ssa_value(b
, w
[4])->def
,
4695 if (opcode
== SpvOpPtrNotEqual
)
4696 def
= nir_inot(&b
->nb
, def
);
4701 unreachable("Invalid ptr operation");
4704 struct vtn_ssa_value
*ssa_value
= vtn_create_ssa_value(b
, type
);
4705 ssa_value
->def
= def
;
4706 vtn_push_ssa(b
, w
[2], vtn_type
, ssa_value
);
4710 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4711 const uint32_t *w
, unsigned count
)
4717 case SpvOpLoopMerge
:
4718 case SpvOpSelectionMerge
:
4719 /* This is handled by cfg pre-pass and walk_blocks */
4723 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
4724 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4729 vtn_handle_extension(b
, opcode
, w
, count
);
4735 case SpvOpCopyMemory
:
4736 case SpvOpCopyMemorySized
:
4737 case SpvOpAccessChain
:
4738 case SpvOpPtrAccessChain
:
4739 case SpvOpInBoundsAccessChain
:
4740 case SpvOpInBoundsPtrAccessChain
:
4741 case SpvOpArrayLength
:
4742 case SpvOpConvertPtrToU
:
4743 case SpvOpConvertUToPtr
:
4744 vtn_handle_variables(b
, opcode
, w
, count
);
4747 case SpvOpFunctionCall
:
4748 vtn_handle_function_call(b
, opcode
, w
, count
);
4751 case SpvOpSampledImage
:
4753 case SpvOpImageSampleImplicitLod
:
4754 case SpvOpImageSampleExplicitLod
:
4755 case SpvOpImageSampleDrefImplicitLod
:
4756 case SpvOpImageSampleDrefExplicitLod
:
4757 case SpvOpImageSampleProjImplicitLod
:
4758 case SpvOpImageSampleProjExplicitLod
:
4759 case SpvOpImageSampleProjDrefImplicitLod
:
4760 case SpvOpImageSampleProjDrefExplicitLod
:
4761 case SpvOpImageFetch
:
4762 case SpvOpImageGather
:
4763 case SpvOpImageDrefGather
:
4764 case SpvOpImageQuerySizeLod
:
4765 case SpvOpImageQueryLod
:
4766 case SpvOpImageQueryLevels
:
4767 case SpvOpImageQuerySamples
:
4768 vtn_handle_texture(b
, opcode
, w
, count
);
4771 case SpvOpImageRead
:
4772 case SpvOpImageWrite
:
4773 case SpvOpImageTexelPointer
:
4774 vtn_handle_image(b
, opcode
, w
, count
);
4777 case SpvOpImageQuerySize
: {
4778 struct vtn_pointer
*image
=
4779 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
4780 if (glsl_type_is_image(image
->type
->type
)) {
4781 vtn_handle_image(b
, opcode
, w
, count
);
4783 vtn_assert(glsl_type_is_sampler(image
->type
->type
));
4784 vtn_handle_texture(b
, opcode
, w
, count
);
4789 case SpvOpFragmentMaskFetchAMD
:
4790 case SpvOpFragmentFetchAMD
:
4791 vtn_handle_texture(b
, opcode
, w
, count
);
4794 case SpvOpAtomicLoad
:
4795 case SpvOpAtomicExchange
:
4796 case SpvOpAtomicCompareExchange
:
4797 case SpvOpAtomicCompareExchangeWeak
:
4798 case SpvOpAtomicIIncrement
:
4799 case SpvOpAtomicIDecrement
:
4800 case SpvOpAtomicIAdd
:
4801 case SpvOpAtomicISub
:
4802 case SpvOpAtomicSMin
:
4803 case SpvOpAtomicUMin
:
4804 case SpvOpAtomicSMax
:
4805 case SpvOpAtomicUMax
:
4806 case SpvOpAtomicAnd
:
4808 case SpvOpAtomicXor
: {
4809 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
4810 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4811 vtn_handle_image(b
, opcode
, w
, count
);
4813 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4814 vtn_handle_atomics(b
, opcode
, w
, count
);
4819 case SpvOpAtomicStore
: {
4820 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
4821 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4822 vtn_handle_image(b
, opcode
, w
, count
);
4824 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4825 vtn_handle_atomics(b
, opcode
, w
, count
);
4831 vtn_handle_select(b
, opcode
, w
, count
);
4839 case SpvOpConvertFToU
:
4840 case SpvOpConvertFToS
:
4841 case SpvOpConvertSToF
:
4842 case SpvOpConvertUToF
:
4846 case SpvOpQuantizeToF16
:
4847 case SpvOpPtrCastToGeneric
:
4848 case SpvOpGenericCastToPtr
:
4853 case SpvOpSignBitSet
:
4854 case SpvOpLessOrGreater
:
4856 case SpvOpUnordered
:
4871 case SpvOpVectorTimesScalar
:
4873 case SpvOpIAddCarry
:
4874 case SpvOpISubBorrow
:
4875 case SpvOpUMulExtended
:
4876 case SpvOpSMulExtended
:
4877 case SpvOpShiftRightLogical
:
4878 case SpvOpShiftRightArithmetic
:
4879 case SpvOpShiftLeftLogical
:
4880 case SpvOpLogicalEqual
:
4881 case SpvOpLogicalNotEqual
:
4882 case SpvOpLogicalOr
:
4883 case SpvOpLogicalAnd
:
4884 case SpvOpLogicalNot
:
4885 case SpvOpBitwiseOr
:
4886 case SpvOpBitwiseXor
:
4887 case SpvOpBitwiseAnd
:
4889 case SpvOpFOrdEqual
:
4890 case SpvOpFUnordEqual
:
4891 case SpvOpINotEqual
:
4892 case SpvOpFOrdNotEqual
:
4893 case SpvOpFUnordNotEqual
:
4894 case SpvOpULessThan
:
4895 case SpvOpSLessThan
:
4896 case SpvOpFOrdLessThan
:
4897 case SpvOpFUnordLessThan
:
4898 case SpvOpUGreaterThan
:
4899 case SpvOpSGreaterThan
:
4900 case SpvOpFOrdGreaterThan
:
4901 case SpvOpFUnordGreaterThan
:
4902 case SpvOpULessThanEqual
:
4903 case SpvOpSLessThanEqual
:
4904 case SpvOpFOrdLessThanEqual
:
4905 case SpvOpFUnordLessThanEqual
:
4906 case SpvOpUGreaterThanEqual
:
4907 case SpvOpSGreaterThanEqual
:
4908 case SpvOpFOrdGreaterThanEqual
:
4909 case SpvOpFUnordGreaterThanEqual
:
4915 case SpvOpFwidthFine
:
4916 case SpvOpDPdxCoarse
:
4917 case SpvOpDPdyCoarse
:
4918 case SpvOpFwidthCoarse
:
4919 case SpvOpBitFieldInsert
:
4920 case SpvOpBitFieldSExtract
:
4921 case SpvOpBitFieldUExtract
:
4922 case SpvOpBitReverse
:
4924 case SpvOpTranspose
:
4925 case SpvOpOuterProduct
:
4926 case SpvOpMatrixTimesScalar
:
4927 case SpvOpVectorTimesMatrix
:
4928 case SpvOpMatrixTimesVector
:
4929 case SpvOpMatrixTimesMatrix
:
4930 case SpvOpUCountLeadingZerosINTEL
:
4931 case SpvOpUCountTrailingZerosINTEL
:
4932 case SpvOpAbsISubINTEL
:
4933 case SpvOpAbsUSubINTEL
:
4934 case SpvOpIAddSatINTEL
:
4935 case SpvOpUAddSatINTEL
:
4936 case SpvOpIAverageINTEL
:
4937 case SpvOpUAverageINTEL
:
4938 case SpvOpIAverageRoundedINTEL
:
4939 case SpvOpUAverageRoundedINTEL
:
4940 case SpvOpISubSatINTEL
:
4941 case SpvOpUSubSatINTEL
:
4942 case SpvOpIMul32x16INTEL
:
4943 case SpvOpUMul32x16INTEL
:
4944 vtn_handle_alu(b
, opcode
, w
, count
);
4948 vtn_handle_bitcast(b
, w
, count
);
4951 case SpvOpVectorExtractDynamic
:
4952 case SpvOpVectorInsertDynamic
:
4953 case SpvOpVectorShuffle
:
4954 case SpvOpCompositeConstruct
:
4955 case SpvOpCompositeExtract
:
4956 case SpvOpCompositeInsert
:
4957 case SpvOpCopyLogical
:
4958 case SpvOpCopyObject
:
4959 vtn_handle_composite(b
, opcode
, w
, count
);
4962 case SpvOpEmitVertex
:
4963 case SpvOpEndPrimitive
:
4964 case SpvOpEmitStreamVertex
:
4965 case SpvOpEndStreamPrimitive
:
4966 case SpvOpControlBarrier
:
4967 case SpvOpMemoryBarrier
:
4968 vtn_handle_barrier(b
, opcode
, w
, count
);
4971 case SpvOpGroupNonUniformElect
:
4972 case SpvOpGroupNonUniformAll
:
4973 case SpvOpGroupNonUniformAny
:
4974 case SpvOpGroupNonUniformAllEqual
:
4975 case SpvOpGroupNonUniformBroadcast
:
4976 case SpvOpGroupNonUniformBroadcastFirst
:
4977 case SpvOpGroupNonUniformBallot
:
4978 case SpvOpGroupNonUniformInverseBallot
:
4979 case SpvOpGroupNonUniformBallotBitExtract
:
4980 case SpvOpGroupNonUniformBallotBitCount
:
4981 case SpvOpGroupNonUniformBallotFindLSB
:
4982 case SpvOpGroupNonUniformBallotFindMSB
:
4983 case SpvOpGroupNonUniformShuffle
:
4984 case SpvOpGroupNonUniformShuffleXor
:
4985 case SpvOpGroupNonUniformShuffleUp
:
4986 case SpvOpGroupNonUniformShuffleDown
:
4987 case SpvOpGroupNonUniformIAdd
:
4988 case SpvOpGroupNonUniformFAdd
:
4989 case SpvOpGroupNonUniformIMul
:
4990 case SpvOpGroupNonUniformFMul
:
4991 case SpvOpGroupNonUniformSMin
:
4992 case SpvOpGroupNonUniformUMin
:
4993 case SpvOpGroupNonUniformFMin
:
4994 case SpvOpGroupNonUniformSMax
:
4995 case SpvOpGroupNonUniformUMax
:
4996 case SpvOpGroupNonUniformFMax
:
4997 case SpvOpGroupNonUniformBitwiseAnd
:
4998 case SpvOpGroupNonUniformBitwiseOr
:
4999 case SpvOpGroupNonUniformBitwiseXor
:
5000 case SpvOpGroupNonUniformLogicalAnd
:
5001 case SpvOpGroupNonUniformLogicalOr
:
5002 case SpvOpGroupNonUniformLogicalXor
:
5003 case SpvOpGroupNonUniformQuadBroadcast
:
5004 case SpvOpGroupNonUniformQuadSwap
:
5007 case SpvOpGroupBroadcast
:
5008 case SpvOpGroupIAdd
:
5009 case SpvOpGroupFAdd
:
5010 case SpvOpGroupFMin
:
5011 case SpvOpGroupUMin
:
5012 case SpvOpGroupSMin
:
5013 case SpvOpGroupFMax
:
5014 case SpvOpGroupUMax
:
5015 case SpvOpGroupSMax
:
5016 case SpvOpSubgroupBallotKHR
:
5017 case SpvOpSubgroupFirstInvocationKHR
:
5018 case SpvOpSubgroupReadInvocationKHR
:
5019 case SpvOpSubgroupAllKHR
:
5020 case SpvOpSubgroupAnyKHR
:
5021 case SpvOpSubgroupAllEqualKHR
:
5022 case SpvOpGroupIAddNonUniformAMD
:
5023 case SpvOpGroupFAddNonUniformAMD
:
5024 case SpvOpGroupFMinNonUniformAMD
:
5025 case SpvOpGroupUMinNonUniformAMD
:
5026 case SpvOpGroupSMinNonUniformAMD
:
5027 case SpvOpGroupFMaxNonUniformAMD
:
5028 case SpvOpGroupUMaxNonUniformAMD
:
5029 case SpvOpGroupSMaxNonUniformAMD
:
5030 vtn_handle_subgroup(b
, opcode
, w
, count
);
5035 case SpvOpPtrNotEqual
:
5036 vtn_handle_ptr(b
, opcode
, w
, count
);
5039 case SpvOpBeginInvocationInterlockEXT
:
5040 vtn_emit_barrier(b
, nir_intrinsic_begin_invocation_interlock
);
5043 case SpvOpEndInvocationInterlockEXT
:
5044 vtn_emit_barrier(b
, nir_intrinsic_end_invocation_interlock
);
5047 case SpvOpDemoteToHelperInvocationEXT
: {
5048 nir_intrinsic_instr
*intrin
=
5049 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_demote
);
5050 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5054 case SpvOpIsHelperInvocationEXT
: {
5055 nir_intrinsic_instr
*intrin
=
5056 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_is_helper_invocation
);
5057 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 1, 1, NULL
);
5058 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5060 struct vtn_type
*res_type
=
5061 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
5062 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, res_type
->type
);
5063 val
->def
= &intrin
->dest
.ssa
;
5065 vtn_push_ssa(b
, w
[2], res_type
, val
);
5069 case SpvOpReadClockKHR
: {
5070 SpvScope scope
= vtn_constant_uint(b
, w
[3]);
5071 nir_scope nir_scope
;
5074 case SpvScopeDevice
:
5075 nir_scope
= NIR_SCOPE_DEVICE
;
5077 case SpvScopeSubgroup
:
5078 nir_scope
= NIR_SCOPE_SUBGROUP
;
5081 vtn_fail("invalid read clock scope");
5084 /* Operation supports two result types: uvec2 and uint64_t. The NIR
5085 * intrinsic gives uvec2, so pack the result for the other case.
5087 nir_intrinsic_instr
*intrin
=
5088 nir_intrinsic_instr_create(b
->nb
.shader
, nir_intrinsic_shader_clock
);
5089 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 2, 32, NULL
);
5090 nir_intrinsic_set_memory_scope(intrin
, nir_scope
);
5091 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5093 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
5094 const struct glsl_type
*dest_type
= type
->type
;
5095 nir_ssa_def
*result
;
5097 if (glsl_type_is_vector(dest_type
)) {
5098 assert(dest_type
== glsl_vector_type(GLSL_TYPE_UINT
, 2));
5099 result
= &intrin
->dest
.ssa
;
5101 assert(glsl_type_is_scalar(dest_type
));
5102 assert(glsl_get_base_type(dest_type
) == GLSL_TYPE_UINT64
);
5103 result
= nir_pack_64_2x32(&b
->nb
, &intrin
->dest
.ssa
);
5106 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
5108 val
->ssa
= vtn_create_ssa_value(b
, dest_type
);
5109 val
->ssa
->def
= result
;
5113 case SpvOpLifetimeStart
:
5114 case SpvOpLifetimeStop
:
5118 vtn_fail_with_opcode("Unhandled opcode", opcode
);
5125 vtn_create_builder(const uint32_t *words
, size_t word_count
,
5126 gl_shader_stage stage
, const char *entry_point_name
,
5127 const struct spirv_to_nir_options
*options
)
5129 /* Initialize the vtn_builder object */
5130 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
5131 struct spirv_to_nir_options
*dup_options
=
5132 ralloc(b
, struct spirv_to_nir_options
);
5133 *dup_options
= *options
;
5136 b
->spirv_word_count
= word_count
;
5140 list_inithead(&b
->functions
);
5141 b
->entry_point_stage
= stage
;
5142 b
->entry_point_name
= entry_point_name
;
5143 b
->options
= dup_options
;
5146 * Handle the SPIR-V header (first 5 dwords).
5147 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
5149 if (word_count
<= 5)
5152 if (words
[0] != SpvMagicNumber
) {
5153 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
5156 if (words
[1] < 0x10000) {
5157 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
5161 uint16_t generator_id
= words
[2] >> 16;
5162 uint16_t generator_version
= words
[2];
5164 /* The first GLSLang version bump actually 1.5 years after #179 was fixed
5165 * but this should at least let us shut the workaround off for modern
5166 * versions of GLSLang.
5168 b
->wa_glslang_179
= (generator_id
== 8 && generator_version
== 1);
5170 /* In GLSLang commit 8297936dd6eb3, their handling of barrier() was fixed
5171 * to provide correct memory semantics on compute shader barrier()
5172 * commands. Prior to that, we need to fix them up ourselves. This
5173 * GLSLang fix caused them to bump to generator version 3.
5175 b
->wa_glslang_cs_barrier
= (generator_id
== 8 && generator_version
< 3);
5177 /* words[2] == generator magic */
5178 unsigned value_id_bound
= words
[3];
5179 if (words
[4] != 0) {
5180 vtn_err("words[4] was %u, want 0", words
[4]);
5184 b
->value_id_bound
= value_id_bound
;
5185 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
5193 static nir_function
*
5194 vtn_emit_kernel_entry_point_wrapper(struct vtn_builder
*b
,
5195 nir_function
*entry_point
)
5197 vtn_assert(entry_point
== b
->entry_point
->func
->impl
->function
);
5198 vtn_fail_if(!entry_point
->name
, "entry points are required to have a name");
5199 const char *func_name
=
5200 ralloc_asprintf(b
->shader
, "__wrapped_%s", entry_point
->name
);
5202 /* we shouldn't have any inputs yet */
5203 vtn_assert(!entry_point
->shader
->num_inputs
);
5204 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
);
5206 nir_function
*main_entry_point
= nir_function_create(b
->shader
, func_name
);
5207 main_entry_point
->impl
= nir_function_impl_create(main_entry_point
);
5208 nir_builder_init(&b
->nb
, main_entry_point
->impl
);
5209 b
->nb
.cursor
= nir_after_cf_list(&main_entry_point
->impl
->body
);
5210 b
->func_param_idx
= 0;
5212 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, entry_point
);
5214 for (unsigned i
= 0; i
< entry_point
->num_params
; ++i
) {
5215 struct vtn_type
*param_type
= b
->entry_point
->func
->type
->params
[i
];
5217 /* consider all pointers to function memory to be parameters passed
5220 bool is_by_val
= param_type
->base_type
== vtn_base_type_pointer
&&
5221 param_type
->storage_class
== SpvStorageClassFunction
;
5223 /* input variable */
5224 nir_variable
*in_var
= rzalloc(b
->nb
.shader
, nir_variable
);
5225 in_var
->data
.mode
= nir_var_shader_in
;
5226 in_var
->data
.read_only
= true;
5227 in_var
->data
.location
= i
;
5230 in_var
->type
= param_type
->deref
->type
;
5232 in_var
->type
= param_type
->type
;
5234 nir_shader_add_variable(b
->nb
.shader
, in_var
);
5235 b
->nb
.shader
->num_inputs
++;
5237 /* we have to copy the entire variable into function memory */
5239 nir_variable
*copy_var
=
5240 nir_local_variable_create(main_entry_point
->impl
, in_var
->type
,
5242 nir_copy_var(&b
->nb
, copy_var
, in_var
);
5244 nir_src_for_ssa(&nir_build_deref_var(&b
->nb
, copy_var
)->dest
.ssa
);
5246 call
->params
[i
] = nir_src_for_ssa(nir_load_var(&b
->nb
, in_var
));
5250 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
5252 return main_entry_point
;
5256 spirv_to_nir(const uint32_t *words
, size_t word_count
,
5257 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
5258 gl_shader_stage stage
, const char *entry_point_name
,
5259 const struct spirv_to_nir_options
*options
,
5260 const nir_shader_compiler_options
*nir_options
)
5263 const uint32_t *word_end
= words
+ word_count
;
5265 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
5266 stage
, entry_point_name
,
5272 /* See also _vtn_fail() */
5273 if (setjmp(b
->fail_jump
)) {
5278 /* Skip the SPIR-V header, handled at vtn_create_builder */
5281 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
5283 /* Handle all the preamble instructions */
5284 words
= vtn_foreach_instruction(b
, words
, word_end
,
5285 vtn_handle_preamble_instruction
);
5287 if (b
->entry_point
== NULL
) {
5288 vtn_fail("Entry point not found");
5293 /* Set shader info defaults */
5294 if (stage
== MESA_SHADER_GEOMETRY
)
5295 b
->shader
->info
.gs
.invocations
= 1;
5297 /* Parse rounding mode execution modes. This has to happen earlier than
5298 * other changes in the execution modes since they can affect, for example,
5299 * the result of the floating point constants.
5301 vtn_foreach_execution_mode(b
, b
->entry_point
,
5302 vtn_handle_rounding_mode_in_execution_mode
, NULL
);
5304 b
->specializations
= spec
;
5305 b
->num_specializations
= num_spec
;
5307 /* Handle all variable, type, and constant instructions */
5308 words
= vtn_foreach_instruction(b
, words
, word_end
,
5309 vtn_handle_variable_or_type_instruction
);
5311 /* Parse execution modes */
5312 vtn_foreach_execution_mode(b
, b
->entry_point
,
5313 vtn_handle_execution_mode
, NULL
);
5315 if (b
->workgroup_size_builtin
) {
5316 vtn_assert(b
->workgroup_size_builtin
->type
->type
==
5317 glsl_vector_type(GLSL_TYPE_UINT
, 3));
5319 nir_const_value
*const_size
=
5320 b
->workgroup_size_builtin
->constant
->values
;
5322 b
->shader
->info
.cs
.local_size
[0] = const_size
[0].u32
;
5323 b
->shader
->info
.cs
.local_size
[1] = const_size
[1].u32
;
5324 b
->shader
->info
.cs
.local_size
[2] = const_size
[2].u32
;
5327 /* Set types on all vtn_values */
5328 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
5330 vtn_build_cfg(b
, words
, word_end
);
5332 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5333 b
->entry_point
->func
->referenced
= true;
5338 vtn_foreach_cf_node(node
, &b
->functions
) {
5339 struct vtn_function
*func
= vtn_cf_node_as_function(node
);
5340 if (func
->referenced
&& !func
->emitted
) {
5341 b
->const_table
= _mesa_pointer_hash_table_create(b
);
5343 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
5349 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5350 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
5351 vtn_assert(entry_point
);
5353 /* post process entry_points with input params */
5354 if (entry_point
->num_params
&& b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
5355 entry_point
= vtn_emit_kernel_entry_point_wrapper(b
, entry_point
);
5357 entry_point
->is_entrypoint
= true;
5359 /* When multiple shader stages exist in the same SPIR-V module, we
5360 * generate input and output variables for every stage, in the same
5361 * NIR program. These dead variables can be invalid NIR. For example,
5362 * TCS outputs must be per-vertex arrays (or decorated 'patch'), while
5363 * VS output variables wouldn't be.
5365 * To ensure we have valid NIR, we eliminate any dead inputs and outputs
5366 * right away. In order to do so, we must lower any constant initializers
5367 * on outputs so nir_remove_dead_variables sees that they're written to.
5369 nir_lower_variable_initializers(b
->shader
, nir_var_shader_out
);
5370 nir_remove_dead_variables(b
->shader
,
5371 nir_var_shader_in
| nir_var_shader_out
, NULL
);
5373 /* We sometimes generate bogus derefs that, while never used, give the
5374 * validator a bit of heartburn. Run dead code to get rid of them.
5376 nir_opt_dce(b
->shader
);
5378 /* Unparent the shader from the vtn_builder before we delete the builder */
5379 ralloc_steal(NULL
, b
->shader
);
5381 nir_shader
*shader
= b
->shader
;