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 /* User semantic decorations can safely be ignored by the driver. */
866 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
870 /** Chases the array type all the way down to the tail and rewrites the
871 * glsl_types to be based off the tail's glsl_type.
874 vtn_array_type_rewrite_glsl_type(struct vtn_type
*type
)
876 if (type
->base_type
!= vtn_base_type_array
)
879 vtn_array_type_rewrite_glsl_type(type
->array_element
);
881 type
->type
= glsl_array_type(type
->array_element
->type
,
882 type
->length
, type
->stride
);
885 /* Matrix strides are handled as a separate pass because we need to know
886 * whether the matrix is row-major or not first.
889 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
890 UNUSED
struct vtn_value
*val
, int member
,
891 const struct vtn_decoration
*dec
,
894 if (dec
->decoration
!= SpvDecorationMatrixStride
)
897 vtn_fail_if(member
< 0,
898 "The MatrixStride decoration is only allowed on members "
900 vtn_fail_if(dec
->operands
[0] == 0, "MatrixStride must be non-zero");
902 struct member_decoration_ctx
*ctx
= void_ctx
;
904 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
905 if (mat_type
->row_major
) {
906 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
907 mat_type
->stride
= mat_type
->array_element
->stride
;
908 mat_type
->array_element
->stride
= dec
->operands
[0];
910 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
911 dec
->operands
[0], true);
912 mat_type
->array_element
->type
= glsl_get_column_type(mat_type
->type
);
914 vtn_assert(mat_type
->array_element
->stride
> 0);
915 mat_type
->stride
= dec
->operands
[0];
917 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
918 dec
->operands
[0], false);
921 /* Now that we've replaced the glsl_type with a properly strided matrix
922 * type, rewrite the member type so that it's an array of the proper kind
925 vtn_array_type_rewrite_glsl_type(ctx
->type
->members
[member
]);
926 ctx
->fields
[member
].type
= ctx
->type
->members
[member
]->type
;
930 struct_block_decoration_cb(struct vtn_builder
*b
,
931 struct vtn_value
*val
, int member
,
932 const struct vtn_decoration
*dec
, void *ctx
)
937 struct vtn_type
*type
= val
->type
;
938 if (dec
->decoration
== SpvDecorationBlock
)
940 else if (dec
->decoration
== SpvDecorationBufferBlock
)
941 type
->buffer_block
= true;
945 type_decoration_cb(struct vtn_builder
*b
,
946 struct vtn_value
*val
, int member
,
947 const struct vtn_decoration
*dec
, UNUSED
void *ctx
)
949 struct vtn_type
*type
= val
->type
;
952 /* This should have been handled by OpTypeStruct */
953 assert(val
->type
->base_type
== vtn_base_type_struct
);
954 assert(member
>= 0 && member
< val
->type
->length
);
958 switch (dec
->decoration
) {
959 case SpvDecorationArrayStride
:
960 vtn_assert(type
->base_type
== vtn_base_type_array
||
961 type
->base_type
== vtn_base_type_pointer
);
963 case SpvDecorationBlock
:
964 vtn_assert(type
->base_type
== vtn_base_type_struct
);
965 vtn_assert(type
->block
);
967 case SpvDecorationBufferBlock
:
968 vtn_assert(type
->base_type
== vtn_base_type_struct
);
969 vtn_assert(type
->buffer_block
);
971 case SpvDecorationGLSLShared
:
972 case SpvDecorationGLSLPacked
:
973 /* Ignore these, since we get explicit offsets anyways */
976 case SpvDecorationRowMajor
:
977 case SpvDecorationColMajor
:
978 case SpvDecorationMatrixStride
:
979 case SpvDecorationBuiltIn
:
980 case SpvDecorationNoPerspective
:
981 case SpvDecorationFlat
:
982 case SpvDecorationPatch
:
983 case SpvDecorationCentroid
:
984 case SpvDecorationSample
:
985 case SpvDecorationExplicitInterpAMD
:
986 case SpvDecorationVolatile
:
987 case SpvDecorationCoherent
:
988 case SpvDecorationNonWritable
:
989 case SpvDecorationNonReadable
:
990 case SpvDecorationUniform
:
991 case SpvDecorationUniformId
:
992 case SpvDecorationLocation
:
993 case SpvDecorationComponent
:
994 case SpvDecorationOffset
:
995 case SpvDecorationXfbBuffer
:
996 case SpvDecorationXfbStride
:
997 case SpvDecorationUserSemantic
:
998 vtn_warn("Decoration only allowed for struct members: %s",
999 spirv_decoration_to_string(dec
->decoration
));
1002 case SpvDecorationStream
:
1003 /* We don't need to do anything here, as stream is filled up when
1004 * aplying the decoration to a variable, just check that if it is not a
1005 * struct member, it should be a struct.
1007 vtn_assert(type
->base_type
== vtn_base_type_struct
);
1010 case SpvDecorationRelaxedPrecision
:
1011 case SpvDecorationSpecId
:
1012 case SpvDecorationInvariant
:
1013 case SpvDecorationRestrict
:
1014 case SpvDecorationAliased
:
1015 case SpvDecorationConstant
:
1016 case SpvDecorationIndex
:
1017 case SpvDecorationBinding
:
1018 case SpvDecorationDescriptorSet
:
1019 case SpvDecorationLinkageAttributes
:
1020 case SpvDecorationNoContraction
:
1021 case SpvDecorationInputAttachmentIndex
:
1022 vtn_warn("Decoration not allowed on types: %s",
1023 spirv_decoration_to_string(dec
->decoration
));
1026 case SpvDecorationCPacked
:
1027 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
1028 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1029 spirv_decoration_to_string(dec
->decoration
));
1031 type
->packed
= true;
1034 case SpvDecorationSaturatedConversion
:
1035 case SpvDecorationFuncParamAttr
:
1036 case SpvDecorationFPRoundingMode
:
1037 case SpvDecorationFPFastMathMode
:
1038 case SpvDecorationAlignment
:
1039 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1040 spirv_decoration_to_string(dec
->decoration
));
1044 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1049 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
1052 case SpvImageFormatUnknown
: return PIPE_FORMAT_NONE
;
1053 case SpvImageFormatRgba32f
: return PIPE_FORMAT_R32G32B32A32_FLOAT
;
1054 case SpvImageFormatRgba16f
: return PIPE_FORMAT_R16G16B16A16_FLOAT
;
1055 case SpvImageFormatR32f
: return PIPE_FORMAT_R32_FLOAT
;
1056 case SpvImageFormatRgba8
: return PIPE_FORMAT_R8G8B8A8_UNORM
;
1057 case SpvImageFormatRgba8Snorm
: return PIPE_FORMAT_R8G8B8A8_SNORM
;
1058 case SpvImageFormatRg32f
: return PIPE_FORMAT_R32G32_FLOAT
;
1059 case SpvImageFormatRg16f
: return PIPE_FORMAT_R16G16_FLOAT
;
1060 case SpvImageFormatR11fG11fB10f
: return PIPE_FORMAT_R11G11B10_FLOAT
;
1061 case SpvImageFormatR16f
: return PIPE_FORMAT_R16_FLOAT
;
1062 case SpvImageFormatRgba16
: return PIPE_FORMAT_R16G16B16A16_UNORM
;
1063 case SpvImageFormatRgb10A2
: return PIPE_FORMAT_R10G10B10A2_UNORM
;
1064 case SpvImageFormatRg16
: return PIPE_FORMAT_R16G16_UNORM
;
1065 case SpvImageFormatRg8
: return PIPE_FORMAT_R8G8_UNORM
;
1066 case SpvImageFormatR16
: return PIPE_FORMAT_R16_UNORM
;
1067 case SpvImageFormatR8
: return PIPE_FORMAT_R8_UNORM
;
1068 case SpvImageFormatRgba16Snorm
: return PIPE_FORMAT_R16G16B16A16_SNORM
;
1069 case SpvImageFormatRg16Snorm
: return PIPE_FORMAT_R16G16_SNORM
;
1070 case SpvImageFormatRg8Snorm
: return PIPE_FORMAT_R8G8_SNORM
;
1071 case SpvImageFormatR16Snorm
: return PIPE_FORMAT_R16_SNORM
;
1072 case SpvImageFormatR8Snorm
: return PIPE_FORMAT_R8_SNORM
;
1073 case SpvImageFormatRgba32i
: return PIPE_FORMAT_R32G32B32A32_SINT
;
1074 case SpvImageFormatRgba16i
: return PIPE_FORMAT_R16G16B16A16_SINT
;
1075 case SpvImageFormatRgba8i
: return PIPE_FORMAT_R8G8B8A8_SINT
;
1076 case SpvImageFormatR32i
: return PIPE_FORMAT_R32_SINT
;
1077 case SpvImageFormatRg32i
: return PIPE_FORMAT_R32G32_SINT
;
1078 case SpvImageFormatRg16i
: return PIPE_FORMAT_R16G16_SINT
;
1079 case SpvImageFormatRg8i
: return PIPE_FORMAT_R8G8_SINT
;
1080 case SpvImageFormatR16i
: return PIPE_FORMAT_R16_SINT
;
1081 case SpvImageFormatR8i
: return PIPE_FORMAT_R8_SINT
;
1082 case SpvImageFormatRgba32ui
: return PIPE_FORMAT_R32G32B32A32_UINT
;
1083 case SpvImageFormatRgba16ui
: return PIPE_FORMAT_R16G16B16A16_UINT
;
1084 case SpvImageFormatRgba8ui
: return PIPE_FORMAT_R8G8B8A8_UINT
;
1085 case SpvImageFormatR32ui
: return PIPE_FORMAT_R32_UINT
;
1086 case SpvImageFormatRgb10a2ui
: return PIPE_FORMAT_R10G10B10A2_UINT
;
1087 case SpvImageFormatRg32ui
: return PIPE_FORMAT_R32G32_UINT
;
1088 case SpvImageFormatRg16ui
: return PIPE_FORMAT_R16G16_UINT
;
1089 case SpvImageFormatRg8ui
: return PIPE_FORMAT_R8G8_UINT
;
1090 case SpvImageFormatR16ui
: return PIPE_FORMAT_R16_UINT
;
1091 case SpvImageFormatR8ui
: return PIPE_FORMAT_R8_UINT
;
1093 vtn_fail("Invalid image format: %s (%u)",
1094 spirv_imageformat_to_string(format
), format
);
1099 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
1100 const uint32_t *w
, unsigned count
)
1102 struct vtn_value
*val
= NULL
;
1104 /* In order to properly handle forward declarations, we have to defer
1105 * allocation for pointer types.
1107 if (opcode
!= SpvOpTypePointer
&& opcode
!= SpvOpTypeForwardPointer
) {
1108 val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1109 vtn_fail_if(val
->type
!= NULL
,
1110 "Only pointers can have forward declarations");
1111 val
->type
= rzalloc(b
, struct vtn_type
);
1112 val
->type
->id
= w
[1];
1117 val
->type
->base_type
= vtn_base_type_void
;
1118 val
->type
->type
= glsl_void_type();
1121 val
->type
->base_type
= vtn_base_type_scalar
;
1122 val
->type
->type
= glsl_bool_type();
1123 val
->type
->length
= 1;
1125 case SpvOpTypeInt
: {
1126 int bit_size
= w
[2];
1127 const bool signedness
= w
[3];
1128 val
->type
->base_type
= vtn_base_type_scalar
;
1131 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1134 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1137 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1140 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1143 vtn_fail("Invalid int bit size: %u", bit_size
);
1145 val
->type
->length
= 1;
1149 case SpvOpTypeFloat
: {
1150 int bit_size
= w
[2];
1151 val
->type
->base_type
= vtn_base_type_scalar
;
1154 val
->type
->type
= glsl_float16_t_type();
1157 val
->type
->type
= glsl_float_type();
1160 val
->type
->type
= glsl_double_type();
1163 vtn_fail("Invalid float bit size: %u", bit_size
);
1165 val
->type
->length
= 1;
1169 case SpvOpTypeVector
: {
1170 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1171 unsigned elems
= w
[3];
1173 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1174 "Base type for OpTypeVector must be a scalar");
1175 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1176 "Invalid component count for OpTypeVector");
1178 val
->type
->base_type
= vtn_base_type_vector
;
1179 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1180 val
->type
->length
= elems
;
1181 val
->type
->stride
= glsl_type_is_boolean(val
->type
->type
)
1182 ? 4 : glsl_get_bit_size(base
->type
) / 8;
1183 val
->type
->array_element
= base
;
1187 case SpvOpTypeMatrix
: {
1188 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1189 unsigned columns
= w
[3];
1191 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1192 "Base type for OpTypeMatrix must be a vector");
1193 vtn_fail_if(columns
< 2 || columns
> 4,
1194 "Invalid column count for OpTypeMatrix");
1196 val
->type
->base_type
= vtn_base_type_matrix
;
1197 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1198 glsl_get_vector_elements(base
->type
),
1200 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1201 "Unsupported base type for OpTypeMatrix");
1202 assert(!glsl_type_is_error(val
->type
->type
));
1203 val
->type
->length
= columns
;
1204 val
->type
->array_element
= base
;
1205 val
->type
->row_major
= false;
1206 val
->type
->stride
= 0;
1210 case SpvOpTypeRuntimeArray
:
1211 case SpvOpTypeArray
: {
1212 struct vtn_type
*array_element
=
1213 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1215 if (opcode
== SpvOpTypeRuntimeArray
) {
1216 /* A length of 0 is used to denote unsized arrays */
1217 val
->type
->length
= 0;
1219 val
->type
->length
= vtn_constant_uint(b
, w
[3]);
1222 val
->type
->base_type
= vtn_base_type_array
;
1223 val
->type
->array_element
= array_element
;
1224 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
1225 val
->type
->stride
= glsl_get_cl_size(array_element
->type
);
1227 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1228 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
,
1233 case SpvOpTypeStruct
: {
1234 unsigned num_fields
= count
- 2;
1235 val
->type
->base_type
= vtn_base_type_struct
;
1236 val
->type
->length
= num_fields
;
1237 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1238 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1239 val
->type
->packed
= false;
1241 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1242 for (unsigned i
= 0; i
< num_fields
; i
++) {
1243 val
->type
->members
[i
] =
1244 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
1245 fields
[i
] = (struct glsl_struct_field
) {
1246 .type
= val
->type
->members
[i
]->type
,
1247 .name
= ralloc_asprintf(b
, "field%d", i
),
1253 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1254 unsigned offset
= 0;
1255 for (unsigned i
= 0; i
< num_fields
; i
++) {
1256 offset
= align(offset
, glsl_get_cl_alignment(fields
[i
].type
));
1257 fields
[i
].offset
= offset
;
1258 offset
+= glsl_get_cl_size(fields
[i
].type
);
1262 struct member_decoration_ctx ctx
= {
1263 .num_fields
= num_fields
,
1268 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1269 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1271 vtn_foreach_decoration(b
, val
, struct_block_decoration_cb
, NULL
);
1273 const char *name
= val
->name
;
1275 if (val
->type
->block
|| val
->type
->buffer_block
) {
1276 /* Packing will be ignored since types coming from SPIR-V are
1277 * explicitly laid out.
1279 val
->type
->type
= glsl_interface_type(fields
, num_fields
,
1280 /* packing */ 0, false,
1281 name
? name
: "block");
1283 val
->type
->type
= glsl_struct_type(fields
, num_fields
,
1284 name
? name
: "struct", false);
1289 case SpvOpTypeFunction
: {
1290 val
->type
->base_type
= vtn_base_type_function
;
1291 val
->type
->type
= NULL
;
1293 val
->type
->return_type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1295 const unsigned num_params
= count
- 3;
1296 val
->type
->length
= num_params
;
1297 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1298 for (unsigned i
= 0; i
< count
- 3; i
++) {
1299 val
->type
->params
[i
] =
1300 vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
;
1305 case SpvOpTypePointer
:
1306 case SpvOpTypeForwardPointer
: {
1307 /* We can't blindly push the value because it might be a forward
1310 val
= vtn_untyped_value(b
, w
[1]);
1312 SpvStorageClass storage_class
= w
[2];
1314 if (val
->value_type
== vtn_value_type_invalid
) {
1315 val
->value_type
= vtn_value_type_type
;
1316 val
->type
= rzalloc(b
, struct vtn_type
);
1317 val
->type
->id
= w
[1];
1318 val
->type
->base_type
= vtn_base_type_pointer
;
1319 val
->type
->storage_class
= storage_class
;
1321 /* These can actually be stored to nir_variables and used as SSA
1322 * values so they need a real glsl_type.
1324 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1325 b
, storage_class
, NULL
, NULL
);
1326 val
->type
->type
= nir_address_format_to_glsl_type(
1327 vtn_mode_to_address_format(b
, mode
));
1329 vtn_fail_if(val
->type
->storage_class
!= storage_class
,
1330 "The storage classes of an OpTypePointer and any "
1331 "OpTypeForwardPointers that provide forward "
1332 "declarations of it must match.");
1335 if (opcode
== SpvOpTypePointer
) {
1336 vtn_fail_if(val
->type
->deref
!= NULL
,
1337 "While OpTypeForwardPointer can be used to provide a "
1338 "forward declaration of a pointer, OpTypePointer can "
1339 "only be used once for a given id.");
1341 val
->type
->deref
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
1343 /* Only certain storage classes use ArrayStride. The others (in
1344 * particular Workgroup) are expected to be laid out by the driver.
1346 switch (storage_class
) {
1347 case SpvStorageClassUniform
:
1348 case SpvStorageClassPushConstant
:
1349 case SpvStorageClassStorageBuffer
:
1350 case SpvStorageClassPhysicalStorageBuffer
:
1351 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1354 /* Nothing to do. */
1358 if (b
->physical_ptrs
) {
1359 switch (storage_class
) {
1360 case SpvStorageClassFunction
:
1361 case SpvStorageClassWorkgroup
:
1362 case SpvStorageClassCrossWorkgroup
:
1363 case SpvStorageClassUniformConstant
:
1364 val
->type
->stride
= align(glsl_get_cl_size(val
->type
->deref
->type
),
1365 glsl_get_cl_alignment(val
->type
->deref
->type
));
1375 case SpvOpTypeImage
: {
1376 val
->type
->base_type
= vtn_base_type_image
;
1378 const struct vtn_type
*sampled_type
=
1379 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1381 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1382 glsl_get_bit_size(sampled_type
->type
) != 32,
1383 "Sampled type of OpTypeImage must be a 32-bit scalar");
1385 enum glsl_sampler_dim dim
;
1386 switch ((SpvDim
)w
[3]) {
1387 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1388 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1389 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1390 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1391 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1392 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1393 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1395 vtn_fail("Invalid SPIR-V image dimensionality: %s (%u)",
1396 spirv_dim_to_string((SpvDim
)w
[3]), w
[3]);
1399 /* w[4]: as per Vulkan spec "Validation Rules within a Module",
1400 * The “Depth” operand of OpTypeImage is ignored.
1402 bool is_array
= w
[5];
1403 bool multisampled
= w
[6];
1404 unsigned sampled
= w
[7];
1405 SpvImageFormat format
= w
[8];
1408 val
->type
->access_qualifier
= w
[9];
1410 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1413 if (dim
== GLSL_SAMPLER_DIM_2D
)
1414 dim
= GLSL_SAMPLER_DIM_MS
;
1415 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1416 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1418 vtn_fail("Unsupported multisampled image type");
1421 val
->type
->image_format
= translate_image_format(b
, format
);
1423 enum glsl_base_type sampled_base_type
=
1424 glsl_get_base_type(sampled_type
->type
);
1426 val
->type
->sampled
= true;
1427 val
->type
->type
= glsl_sampler_type(dim
, false, is_array
,
1429 } else if (sampled
== 2) {
1430 val
->type
->sampled
= false;
1431 val
->type
->type
= glsl_image_type(dim
, is_array
, sampled_base_type
);
1433 vtn_fail("We need to know if the image will be sampled");
1438 case SpvOpTypeSampledImage
:
1439 val
->type
->base_type
= vtn_base_type_sampled_image
;
1440 val
->type
->image
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1441 val
->type
->type
= val
->type
->image
->type
;
1444 case SpvOpTypeSampler
:
1445 /* The actual sampler type here doesn't really matter. It gets
1446 * thrown away the moment you combine it with an image. What really
1447 * matters is that it's a sampler type as opposed to an integer type
1448 * so the backend knows what to do.
1450 val
->type
->base_type
= vtn_base_type_sampler
;
1451 val
->type
->type
= glsl_bare_sampler_type();
1454 case SpvOpTypeOpaque
:
1455 case SpvOpTypeEvent
:
1456 case SpvOpTypeDeviceEvent
:
1457 case SpvOpTypeReserveId
:
1458 case SpvOpTypeQueue
:
1461 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1464 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1466 if (val
->type
->base_type
== vtn_base_type_struct
&&
1467 (val
->type
->block
|| val
->type
->buffer_block
)) {
1468 for (unsigned i
= 0; i
< val
->type
->length
; i
++) {
1469 vtn_fail_if(vtn_type_contains_block(b
, val
->type
->members
[i
]),
1470 "Block and BufferBlock decorations cannot decorate a "
1471 "structure type that is nested at any level inside "
1472 "another structure type decorated with Block or "
1478 static nir_constant
*
1479 vtn_null_constant(struct vtn_builder
*b
, struct vtn_type
*type
)
1481 nir_constant
*c
= rzalloc(b
, nir_constant
);
1483 switch (type
->base_type
) {
1484 case vtn_base_type_scalar
:
1485 case vtn_base_type_vector
:
1486 /* Nothing to do here. It's already initialized to zero */
1489 case vtn_base_type_pointer
: {
1490 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1491 b
, type
->storage_class
, type
->deref
, NULL
);
1492 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
1494 const nir_const_value
*null_value
= nir_address_format_null_value(addr_format
);
1495 memcpy(c
->values
, null_value
,
1496 sizeof(nir_const_value
) * nir_address_format_num_components(addr_format
));
1500 case vtn_base_type_void
:
1501 case vtn_base_type_image
:
1502 case vtn_base_type_sampler
:
1503 case vtn_base_type_sampled_image
:
1504 case vtn_base_type_function
:
1505 /* For those we have to return something but it doesn't matter what. */
1508 case vtn_base_type_matrix
:
1509 case vtn_base_type_array
:
1510 vtn_assert(type
->length
> 0);
1511 c
->num_elements
= type
->length
;
1512 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1514 c
->elements
[0] = vtn_null_constant(b
, type
->array_element
);
1515 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1516 c
->elements
[i
] = c
->elements
[0];
1519 case vtn_base_type_struct
:
1520 c
->num_elements
= type
->length
;
1521 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1522 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
1523 c
->elements
[i
] = vtn_null_constant(b
, type
->members
[i
]);
1527 vtn_fail("Invalid type for null constant");
1534 spec_constant_decoration_cb(struct vtn_builder
*b
, UNUSED
struct vtn_value
*val
,
1535 ASSERTED
int member
,
1536 const struct vtn_decoration
*dec
, void *data
)
1538 vtn_assert(member
== -1);
1539 if (dec
->decoration
!= SpvDecorationSpecId
)
1542 nir_const_value
*value
= data
;
1543 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1544 if (b
->specializations
[i
].id
== dec
->operands
[0]) {
1545 *value
= b
->specializations
[i
].value
;
1552 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1553 struct vtn_value
*val
,
1554 ASSERTED
int member
,
1555 const struct vtn_decoration
*dec
,
1558 vtn_assert(member
== -1);
1559 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1560 dec
->operands
[0] != SpvBuiltInWorkgroupSize
)
1563 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1564 b
->workgroup_size_builtin
= val
;
1568 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1569 const uint32_t *w
, unsigned count
)
1571 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1572 val
->constant
= rzalloc(b
, nir_constant
);
1574 case SpvOpConstantTrue
:
1575 case SpvOpConstantFalse
:
1576 case SpvOpSpecConstantTrue
:
1577 case SpvOpSpecConstantFalse
: {
1578 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1579 "Result type of %s must be OpTypeBool",
1580 spirv_op_to_string(opcode
));
1582 bool bval
= (opcode
== SpvOpConstantTrue
||
1583 opcode
== SpvOpSpecConstantTrue
);
1585 nir_const_value u32val
= nir_const_value_for_uint(bval
, 32);
1587 if (opcode
== SpvOpSpecConstantTrue
||
1588 opcode
== SpvOpSpecConstantFalse
)
1589 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &u32val
);
1591 val
->constant
->values
[0].b
= u32val
.u32
!= 0;
1596 case SpvOpSpecConstant
: {
1597 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1598 "Result type of %s must be a scalar",
1599 spirv_op_to_string(opcode
));
1600 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1603 val
->constant
->values
[0].u64
= vtn_u64_literal(&w
[3]);
1606 val
->constant
->values
[0].u32
= w
[3];
1609 val
->constant
->values
[0].u16
= w
[3];
1612 val
->constant
->values
[0].u8
= w
[3];
1615 vtn_fail("Unsupported SpvOpConstant bit size: %u", bit_size
);
1618 if (opcode
== SpvOpSpecConstant
)
1619 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
,
1620 &val
->constant
->values
[0]);
1624 case SpvOpSpecConstantComposite
:
1625 case SpvOpConstantComposite
: {
1626 unsigned elem_count
= count
- 3;
1627 vtn_fail_if(elem_count
!= val
->type
->length
,
1628 "%s has %u constituents, expected %u",
1629 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1631 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1632 for (unsigned i
= 0; i
< elem_count
; i
++) {
1633 struct vtn_value
*val
= vtn_untyped_value(b
, w
[i
+ 3]);
1635 if (val
->value_type
== vtn_value_type_constant
) {
1636 elems
[i
] = val
->constant
;
1638 vtn_fail_if(val
->value_type
!= vtn_value_type_undef
,
1639 "only constants or undefs allowed for "
1640 "SpvOpConstantComposite");
1641 /* to make it easier, just insert a NULL constant for now */
1642 elems
[i
] = vtn_null_constant(b
, val
->type
);
1646 switch (val
->type
->base_type
) {
1647 case vtn_base_type_vector
: {
1648 assert(glsl_type_is_vector(val
->type
->type
));
1649 for (unsigned i
= 0; i
< elem_count
; i
++)
1650 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1654 case vtn_base_type_matrix
:
1655 case vtn_base_type_struct
:
1656 case vtn_base_type_array
:
1657 ralloc_steal(val
->constant
, elems
);
1658 val
->constant
->num_elements
= elem_count
;
1659 val
->constant
->elements
= elems
;
1663 vtn_fail("Result type of %s must be a composite type",
1664 spirv_op_to_string(opcode
));
1669 case SpvOpSpecConstantOp
: {
1670 nir_const_value u32op
= nir_const_value_for_uint(w
[3], 32);
1671 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &u32op
);
1672 SpvOp opcode
= u32op
.u32
;
1674 case SpvOpVectorShuffle
: {
1675 struct vtn_value
*v0
= &b
->values
[w
[4]];
1676 struct vtn_value
*v1
= &b
->values
[w
[5]];
1678 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1679 v0
->value_type
== vtn_value_type_undef
);
1680 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1681 v1
->value_type
== vtn_value_type_undef
);
1683 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1684 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1686 vtn_assert(len0
+ len1
< 16);
1688 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1689 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1690 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1692 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1693 (void)bit_size0
; (void)bit_size1
;
1695 nir_const_value undef
= { .u64
= 0xdeadbeefdeadbeef };
1696 nir_const_value combined
[NIR_MAX_VEC_COMPONENTS
* 2];
1698 if (v0
->value_type
== vtn_value_type_constant
) {
1699 for (unsigned i
= 0; i
< len0
; i
++)
1700 combined
[i
] = v0
->constant
->values
[i
];
1702 if (v1
->value_type
== vtn_value_type_constant
) {
1703 for (unsigned i
= 0; i
< len1
; i
++)
1704 combined
[len0
+ i
] = v1
->constant
->values
[i
];
1707 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1708 uint32_t comp
= w
[i
+ 6];
1709 if (comp
== (uint32_t)-1) {
1710 /* If component is not used, set the value to a known constant
1711 * to detect if it is wrongly used.
1713 val
->constant
->values
[j
] = undef
;
1715 vtn_fail_if(comp
>= len0
+ len1
,
1716 "All Component literals must either be FFFFFFFF "
1717 "or in [0, N - 1] (inclusive).");
1718 val
->constant
->values
[j
] = combined
[comp
];
1724 case SpvOpCompositeExtract
:
1725 case SpvOpCompositeInsert
: {
1726 struct vtn_value
*comp
;
1727 unsigned deref_start
;
1728 struct nir_constant
**c
;
1729 if (opcode
== SpvOpCompositeExtract
) {
1730 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1732 c
= &comp
->constant
;
1734 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1736 val
->constant
= nir_constant_clone(comp
->constant
,
1742 const struct vtn_type
*type
= comp
->type
;
1743 for (unsigned i
= deref_start
; i
< count
; i
++) {
1744 vtn_fail_if(w
[i
] > type
->length
,
1745 "%uth index of %s is %u but the type has only "
1746 "%u elements", i
- deref_start
,
1747 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1749 switch (type
->base_type
) {
1750 case vtn_base_type_vector
:
1752 type
= type
->array_element
;
1755 case vtn_base_type_matrix
:
1756 case vtn_base_type_array
:
1757 c
= &(*c
)->elements
[w
[i
]];
1758 type
= type
->array_element
;
1761 case vtn_base_type_struct
:
1762 c
= &(*c
)->elements
[w
[i
]];
1763 type
= type
->members
[w
[i
]];
1767 vtn_fail("%s must only index into composite types",
1768 spirv_op_to_string(opcode
));
1772 if (opcode
== SpvOpCompositeExtract
) {
1776 unsigned num_components
= type
->length
;
1777 for (unsigned i
= 0; i
< num_components
; i
++)
1778 val
->constant
->values
[i
] = (*c
)->values
[elem
+ i
];
1781 struct vtn_value
*insert
=
1782 vtn_value(b
, w
[4], vtn_value_type_constant
);
1783 vtn_assert(insert
->type
== type
);
1785 *c
= insert
->constant
;
1787 unsigned num_components
= type
->length
;
1788 for (unsigned i
= 0; i
< num_components
; i
++)
1789 (*c
)->values
[elem
+ i
] = insert
->constant
->values
[i
];
1797 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
1798 nir_alu_type src_alu_type
= dst_alu_type
;
1799 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
1802 vtn_assert(count
<= 7);
1808 /* We have a source in a conversion */
1810 nir_get_nir_type_for_glsl_type(
1811 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1812 /* We use the bitsize of the conversion source to evaluate the opcode later */
1813 bit_size
= glsl_get_bit_size(
1814 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1817 bit_size
= glsl_get_bit_size(val
->type
->type
);
1820 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
1821 nir_alu_type_get_type_size(src_alu_type
),
1822 nir_alu_type_get_type_size(dst_alu_type
));
1823 nir_const_value src
[3][NIR_MAX_VEC_COMPONENTS
];
1825 for (unsigned i
= 0; i
< count
- 4; i
++) {
1826 struct vtn_value
*src_val
=
1827 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
);
1829 /* If this is an unsized source, pull the bit size from the
1830 * source; otherwise, we'll use the bit size from the destination.
1832 if (!nir_alu_type_get_type_size(nir_op_infos
[op
].input_types
[i
]))
1833 bit_size
= glsl_get_bit_size(src_val
->type
->type
);
1835 unsigned src_comps
= nir_op_infos
[op
].input_sizes
[i
] ?
1836 nir_op_infos
[op
].input_sizes
[i
] :
1839 unsigned j
= swap
? 1 - i
: i
;
1840 for (unsigned c
= 0; c
< src_comps
; c
++)
1841 src
[j
][c
] = src_val
->constant
->values
[c
];
1844 /* fix up fixed size sources */
1851 for (unsigned i
= 0; i
< num_components
; ++i
) {
1853 case 64: src
[1][i
].u32
= src
[1][i
].u64
; break;
1854 case 16: src
[1][i
].u32
= src
[1][i
].u16
; break;
1855 case 8: src
[1][i
].u32
= src
[1][i
].u8
; break;
1864 nir_const_value
*srcs
[3] = {
1865 src
[0], src
[1], src
[2],
1867 nir_eval_const_opcode(op
, val
->constant
->values
,
1868 num_components
, bit_size
, srcs
,
1869 b
->shader
->info
.float_controls_execution_mode
);
1876 case SpvOpConstantNull
:
1877 val
->constant
= vtn_null_constant(b
, val
->type
);
1880 case SpvOpConstantSampler
:
1881 vtn_fail("OpConstantSampler requires Kernel Capability");
1885 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1888 /* Now that we have the value, update the workgroup size if needed */
1889 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1892 SpvMemorySemanticsMask
1893 vtn_storage_class_to_memory_semantics(SpvStorageClass sc
)
1896 case SpvStorageClassStorageBuffer
:
1897 case SpvStorageClassPhysicalStorageBuffer
:
1898 return SpvMemorySemanticsUniformMemoryMask
;
1899 case SpvStorageClassWorkgroup
:
1900 return SpvMemorySemanticsWorkgroupMemoryMask
;
1902 return SpvMemorySemanticsMaskNone
;
1907 vtn_split_barrier_semantics(struct vtn_builder
*b
,
1908 SpvMemorySemanticsMask semantics
,
1909 SpvMemorySemanticsMask
*before
,
1910 SpvMemorySemanticsMask
*after
)
1912 /* For memory semantics embedded in operations, we split them into up to
1913 * two barriers, to be added before and after the operation. This is less
1914 * strict than if we propagated until the final backend stage, but still
1915 * result in correct execution.
1917 * A further improvement could be pipe this information (and use!) into the
1918 * next compiler layers, at the expense of making the handling of barriers
1922 *before
= SpvMemorySemanticsMaskNone
;
1923 *after
= SpvMemorySemanticsMaskNone
;
1925 SpvMemorySemanticsMask order_semantics
=
1926 semantics
& (SpvMemorySemanticsAcquireMask
|
1927 SpvMemorySemanticsReleaseMask
|
1928 SpvMemorySemanticsAcquireReleaseMask
|
1929 SpvMemorySemanticsSequentiallyConsistentMask
);
1931 if (util_bitcount(order_semantics
) > 1) {
1932 /* Old GLSLang versions incorrectly set all the ordering bits. This was
1933 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
1934 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
1936 vtn_warn("Multiple memory ordering semantics specified, "
1937 "assuming AcquireRelease.");
1938 order_semantics
= SpvMemorySemanticsAcquireReleaseMask
;
1941 const SpvMemorySemanticsMask av_vis_semantics
=
1942 semantics
& (SpvMemorySemanticsMakeAvailableMask
|
1943 SpvMemorySemanticsMakeVisibleMask
);
1945 const SpvMemorySemanticsMask storage_semantics
=
1946 semantics
& (SpvMemorySemanticsUniformMemoryMask
|
1947 SpvMemorySemanticsSubgroupMemoryMask
|
1948 SpvMemorySemanticsWorkgroupMemoryMask
|
1949 SpvMemorySemanticsCrossWorkgroupMemoryMask
|
1950 SpvMemorySemanticsAtomicCounterMemoryMask
|
1951 SpvMemorySemanticsImageMemoryMask
|
1952 SpvMemorySemanticsOutputMemoryMask
);
1954 const SpvMemorySemanticsMask other_semantics
=
1955 semantics
& ~(order_semantics
| av_vis_semantics
| storage_semantics
);
1957 if (other_semantics
)
1958 vtn_warn("Ignoring unhandled memory semantics: %u\n", other_semantics
);
1960 /* SequentiallyConsistent is treated as AcquireRelease. */
1962 /* The RELEASE barrier happens BEFORE the operation, and it is usually
1963 * associated with a Store. All the write operations with a matching
1964 * semantics will not be reordered after the Store.
1966 if (order_semantics
& (SpvMemorySemanticsReleaseMask
|
1967 SpvMemorySemanticsAcquireReleaseMask
|
1968 SpvMemorySemanticsSequentiallyConsistentMask
)) {
1969 *before
|= SpvMemorySemanticsReleaseMask
| storage_semantics
;
1972 /* The ACQUIRE barrier happens AFTER the operation, and it is usually
1973 * associated with a Load. All the operations with a matching semantics
1974 * will not be reordered before the Load.
1976 if (order_semantics
& (SpvMemorySemanticsAcquireMask
|
1977 SpvMemorySemanticsAcquireReleaseMask
|
1978 SpvMemorySemanticsSequentiallyConsistentMask
)) {
1979 *after
|= SpvMemorySemanticsAcquireMask
| storage_semantics
;
1982 if (av_vis_semantics
& SpvMemorySemanticsMakeVisibleMask
)
1983 *before
|= SpvMemorySemanticsMakeVisibleMask
| storage_semantics
;
1985 if (av_vis_semantics
& SpvMemorySemanticsMakeAvailableMask
)
1986 *after
|= SpvMemorySemanticsMakeAvailableMask
| storage_semantics
;
1990 vtn_emit_scoped_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
1991 SpvMemorySemanticsMask semantics
)
1993 nir_memory_semantics nir_semantics
= 0;
1995 SpvMemorySemanticsMask order_semantics
=
1996 semantics
& (SpvMemorySemanticsAcquireMask
|
1997 SpvMemorySemanticsReleaseMask
|
1998 SpvMemorySemanticsAcquireReleaseMask
|
1999 SpvMemorySemanticsSequentiallyConsistentMask
);
2001 if (util_bitcount(order_semantics
) > 1) {
2002 /* Old GLSLang versions incorrectly set all the ordering bits. This was
2003 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
2004 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
2006 vtn_warn("Multiple memory ordering semantics bits specified, "
2007 "assuming AcquireRelease.");
2008 order_semantics
= SpvMemorySemanticsAcquireReleaseMask
;
2011 switch (order_semantics
) {
2013 /* Not an ordering barrier. */
2016 case SpvMemorySemanticsAcquireMask
:
2017 nir_semantics
= NIR_MEMORY_ACQUIRE
;
2020 case SpvMemorySemanticsReleaseMask
:
2021 nir_semantics
= NIR_MEMORY_RELEASE
;
2024 case SpvMemorySemanticsSequentiallyConsistentMask
:
2025 /* Fall through. Treated as AcquireRelease in Vulkan. */
2026 case SpvMemorySemanticsAcquireReleaseMask
:
2027 nir_semantics
= NIR_MEMORY_ACQUIRE
| NIR_MEMORY_RELEASE
;
2031 unreachable("Invalid memory order semantics");
2034 if (semantics
& SpvMemorySemanticsMakeAvailableMask
) {
2035 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2036 "To use MakeAvailable memory semantics the VulkanMemoryModel "
2037 "capability must be declared.");
2038 nir_semantics
|= NIR_MEMORY_MAKE_AVAILABLE
;
2041 if (semantics
& SpvMemorySemanticsMakeVisibleMask
) {
2042 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2043 "To use MakeVisible memory semantics the VulkanMemoryModel "
2044 "capability must be declared.");
2045 nir_semantics
|= NIR_MEMORY_MAKE_VISIBLE
;
2048 /* Vulkan Environment for SPIR-V says "SubgroupMemory, CrossWorkgroupMemory,
2049 * and AtomicCounterMemory are ignored".
2051 semantics
&= ~(SpvMemorySemanticsSubgroupMemoryMask
|
2052 SpvMemorySemanticsCrossWorkgroupMemoryMask
|
2053 SpvMemorySemanticsAtomicCounterMemoryMask
);
2055 /* TODO: Consider adding nir_var_mem_image mode to NIR so it can be used
2056 * for SpvMemorySemanticsImageMemoryMask.
2059 nir_variable_mode modes
= 0;
2060 if (semantics
& (SpvMemorySemanticsUniformMemoryMask
|
2061 SpvMemorySemanticsImageMemoryMask
)) {
2062 modes
|= nir_var_uniform
|
2067 if (semantics
& SpvMemorySemanticsWorkgroupMemoryMask
)
2068 modes
|= nir_var_mem_shared
;
2069 if (semantics
& SpvMemorySemanticsOutputMemoryMask
) {
2070 modes
|= nir_var_shader_out
;
2073 /* No barrier to add. */
2074 if (nir_semantics
== 0 || modes
== 0)
2077 nir_scope nir_scope
;
2079 case SpvScopeDevice
:
2080 vtn_fail_if(b
->options
->caps
.vk_memory_model
&&
2081 !b
->options
->caps
.vk_memory_model_device_scope
,
2082 "If the Vulkan memory model is declared and any instruction "
2083 "uses Device scope, the VulkanMemoryModelDeviceScope "
2084 "capability must be declared.");
2085 nir_scope
= NIR_SCOPE_DEVICE
;
2088 case SpvScopeQueueFamily
:
2089 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2090 "To use Queue Family scope, the VulkanMemoryModel capability "
2091 "must be declared.");
2092 nir_scope
= NIR_SCOPE_QUEUE_FAMILY
;
2095 case SpvScopeWorkgroup
:
2096 nir_scope
= NIR_SCOPE_WORKGROUP
;
2099 case SpvScopeSubgroup
:
2100 nir_scope
= NIR_SCOPE_SUBGROUP
;
2103 case SpvScopeInvocation
:
2104 nir_scope
= NIR_SCOPE_INVOCATION
;
2108 vtn_fail("Invalid memory scope");
2111 nir_intrinsic_instr
*intrin
=
2112 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_scoped_memory_barrier
);
2113 nir_intrinsic_set_memory_semantics(intrin
, nir_semantics
);
2115 nir_intrinsic_set_memory_modes(intrin
, modes
);
2116 nir_intrinsic_set_memory_scope(intrin
, nir_scope
);
2117 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2120 struct vtn_ssa_value
*
2121 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
2123 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
2126 if (!glsl_type_is_vector_or_scalar(type
)) {
2127 unsigned elems
= glsl_get_length(type
);
2128 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2129 for (unsigned i
= 0; i
< elems
; i
++) {
2130 const struct glsl_type
*child_type
;
2132 switch (glsl_get_base_type(type
)) {
2134 case GLSL_TYPE_UINT
:
2135 case GLSL_TYPE_INT16
:
2136 case GLSL_TYPE_UINT16
:
2137 case GLSL_TYPE_UINT8
:
2138 case GLSL_TYPE_INT8
:
2139 case GLSL_TYPE_INT64
:
2140 case GLSL_TYPE_UINT64
:
2141 case GLSL_TYPE_BOOL
:
2142 case GLSL_TYPE_FLOAT
:
2143 case GLSL_TYPE_FLOAT16
:
2144 case GLSL_TYPE_DOUBLE
:
2145 child_type
= glsl_get_column_type(type
);
2147 case GLSL_TYPE_ARRAY
:
2148 child_type
= glsl_get_array_element(type
);
2150 case GLSL_TYPE_STRUCT
:
2151 case GLSL_TYPE_INTERFACE
:
2152 child_type
= glsl_get_struct_field(type
, i
);
2155 vtn_fail("unkown base type");
2158 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
2166 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
2169 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
2170 src
.src_type
= type
;
2175 image_operand_arg(struct vtn_builder
*b
, const uint32_t *w
, uint32_t count
,
2176 uint32_t mask_idx
, SpvImageOperandsMask op
)
2178 static const SpvImageOperandsMask ops_with_arg
=
2179 SpvImageOperandsBiasMask
|
2180 SpvImageOperandsLodMask
|
2181 SpvImageOperandsGradMask
|
2182 SpvImageOperandsConstOffsetMask
|
2183 SpvImageOperandsOffsetMask
|
2184 SpvImageOperandsConstOffsetsMask
|
2185 SpvImageOperandsSampleMask
|
2186 SpvImageOperandsMinLodMask
|
2187 SpvImageOperandsMakeTexelAvailableMask
|
2188 SpvImageOperandsMakeTexelVisibleMask
;
2190 assert(util_bitcount(op
) == 1);
2191 assert(w
[mask_idx
] & op
);
2192 assert(op
& ops_with_arg
);
2194 uint32_t idx
= util_bitcount(w
[mask_idx
] & (op
- 1) & ops_with_arg
) + 1;
2196 /* Adjust indices for operands with two arguments. */
2197 static const SpvImageOperandsMask ops_with_two_args
=
2198 SpvImageOperandsGradMask
;
2199 idx
+= util_bitcount(w
[mask_idx
] & (op
- 1) & ops_with_two_args
);
2203 vtn_fail_if(idx
+ (op
& ops_with_two_args
? 1 : 0) >= count
,
2204 "Image op claims to have %s but does not enough "
2205 "following operands", spirv_imageoperands_to_string(op
));
2211 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
2212 const uint32_t *w
, unsigned count
)
2214 if (opcode
== SpvOpSampledImage
) {
2215 struct vtn_value
*val
=
2216 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2217 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2218 val
->sampled_image
->image
=
2219 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2220 val
->sampled_image
->sampler
=
2221 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
2223 } else if (opcode
== SpvOpImage
) {
2224 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
2225 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
2226 vtn_push_value_pointer(b
, w
[2], src_val
->sampled_image
->image
);
2228 vtn_assert(src_val
->value_type
== vtn_value_type_pointer
);
2229 vtn_push_value_pointer(b
, w
[2], src_val
->pointer
);
2234 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2236 struct vtn_pointer
*image
= NULL
, *sampler
= NULL
;
2237 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
2238 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
2239 image
= sampled_val
->sampled_image
->image
;
2240 sampler
= sampled_val
->sampled_image
->sampler
;
2242 vtn_assert(sampled_val
->value_type
== vtn_value_type_pointer
);
2243 image
= sampled_val
->pointer
;
2246 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
);
2247 nir_deref_instr
*sampler_deref
=
2248 sampler
? vtn_pointer_to_deref(b
, sampler
) : NULL
;
2250 const struct glsl_type
*image_type
= sampled_val
->type
->type
;
2251 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
2252 const bool is_array
= glsl_sampler_type_is_array(image_type
);
2253 nir_alu_type dest_type
= nir_type_invalid
;
2255 /* Figure out the base texture operation */
2258 case SpvOpImageSampleImplicitLod
:
2259 case SpvOpImageSampleDrefImplicitLod
:
2260 case SpvOpImageSampleProjImplicitLod
:
2261 case SpvOpImageSampleProjDrefImplicitLod
:
2262 texop
= nir_texop_tex
;
2265 case SpvOpImageSampleExplicitLod
:
2266 case SpvOpImageSampleDrefExplicitLod
:
2267 case SpvOpImageSampleProjExplicitLod
:
2268 case SpvOpImageSampleProjDrefExplicitLod
:
2269 texop
= nir_texop_txl
;
2272 case SpvOpImageFetch
:
2273 if (sampler_dim
== GLSL_SAMPLER_DIM_MS
) {
2274 texop
= nir_texop_txf_ms
;
2276 texop
= nir_texop_txf
;
2280 case SpvOpImageGather
:
2281 case SpvOpImageDrefGather
:
2282 texop
= nir_texop_tg4
;
2285 case SpvOpImageQuerySizeLod
:
2286 case SpvOpImageQuerySize
:
2287 texop
= nir_texop_txs
;
2288 dest_type
= nir_type_int
;
2291 case SpvOpImageQueryLod
:
2292 texop
= nir_texop_lod
;
2293 dest_type
= nir_type_float
;
2296 case SpvOpImageQueryLevels
:
2297 texop
= nir_texop_query_levels
;
2298 dest_type
= nir_type_int
;
2301 case SpvOpImageQuerySamples
:
2302 texop
= nir_texop_texture_samples
;
2303 dest_type
= nir_type_int
;
2306 case SpvOpFragmentFetchAMD
:
2307 texop
= nir_texop_fragment_fetch
;
2310 case SpvOpFragmentMaskFetchAMD
:
2311 texop
= nir_texop_fragment_mask_fetch
;
2315 vtn_fail_with_opcode("Unhandled opcode", opcode
);
2318 nir_tex_src srcs
[10]; /* 10 should be enough */
2319 nir_tex_src
*p
= srcs
;
2321 p
->src
= nir_src_for_ssa(&image_deref
->dest
.ssa
);
2322 p
->src_type
= nir_tex_src_texture_deref
;
2332 vtn_fail_if(sampler
== NULL
,
2333 "%s requires an image of type OpTypeSampledImage",
2334 spirv_op_to_string(opcode
));
2335 p
->src
= nir_src_for_ssa(&sampler_deref
->dest
.ssa
);
2336 p
->src_type
= nir_tex_src_sampler_deref
;
2340 case nir_texop_txf_ms
:
2342 case nir_texop_query_levels
:
2343 case nir_texop_texture_samples
:
2344 case nir_texop_samples_identical
:
2345 case nir_texop_fragment_fetch
:
2346 case nir_texop_fragment_mask_fetch
:
2349 case nir_texop_txf_ms_fb
:
2350 vtn_fail("unexpected nir_texop_txf_ms_fb");
2352 case nir_texop_txf_ms_mcs
:
2353 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2354 case nir_texop_tex_prefetch
:
2355 vtn_fail("unexpected nir_texop_tex_prefetch");
2360 struct nir_ssa_def
*coord
;
2361 unsigned coord_components
;
2363 case SpvOpImageSampleImplicitLod
:
2364 case SpvOpImageSampleExplicitLod
:
2365 case SpvOpImageSampleDrefImplicitLod
:
2366 case SpvOpImageSampleDrefExplicitLod
:
2367 case SpvOpImageSampleProjImplicitLod
:
2368 case SpvOpImageSampleProjExplicitLod
:
2369 case SpvOpImageSampleProjDrefImplicitLod
:
2370 case SpvOpImageSampleProjDrefExplicitLod
:
2371 case SpvOpImageFetch
:
2372 case SpvOpImageGather
:
2373 case SpvOpImageDrefGather
:
2374 case SpvOpImageQueryLod
:
2375 case SpvOpFragmentFetchAMD
:
2376 case SpvOpFragmentMaskFetchAMD
: {
2377 /* All these types have the coordinate as their first real argument */
2378 coord_components
= glsl_get_sampler_dim_coordinate_components(sampler_dim
);
2380 if (is_array
&& texop
!= nir_texop_lod
)
2383 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
2384 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2385 (1 << coord_components
) - 1));
2386 p
->src_type
= nir_tex_src_coord
;
2393 coord_components
= 0;
2398 case SpvOpImageSampleProjImplicitLod
:
2399 case SpvOpImageSampleProjExplicitLod
:
2400 case SpvOpImageSampleProjDrefImplicitLod
:
2401 case SpvOpImageSampleProjDrefExplicitLod
:
2402 /* These have the projector as the last coordinate component */
2403 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2404 p
->src_type
= nir_tex_src_projector
;
2412 bool is_shadow
= false;
2413 unsigned gather_component
= 0;
2415 case SpvOpImageSampleDrefImplicitLod
:
2416 case SpvOpImageSampleDrefExplicitLod
:
2417 case SpvOpImageSampleProjDrefImplicitLod
:
2418 case SpvOpImageSampleProjDrefExplicitLod
:
2419 case SpvOpImageDrefGather
:
2420 /* These all have an explicit depth value as their next source */
2422 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2425 case SpvOpImageGather
:
2426 /* This has a component as its next source */
2427 gather_component
= vtn_constant_uint(b
, w
[idx
++]);
2434 /* For OpImageQuerySizeLod, we always have an LOD */
2435 if (opcode
== SpvOpImageQuerySizeLod
)
2436 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2438 /* For OpFragmentFetchAMD, we always have a multisample index */
2439 if (opcode
== SpvOpFragmentFetchAMD
)
2440 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
2442 /* Now we need to handle some number of optional arguments */
2443 struct vtn_value
*gather_offsets
= NULL
;
2445 uint32_t operands
= w
[idx
];
2447 if (operands
& SpvImageOperandsBiasMask
) {
2448 vtn_assert(texop
== nir_texop_tex
||
2449 texop
== nir_texop_tg4
);
2450 if (texop
== nir_texop_tex
)
2451 texop
= nir_texop_txb
;
2452 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2453 SpvImageOperandsBiasMask
);
2454 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_bias
);
2457 if (operands
& SpvImageOperandsLodMask
) {
2458 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2459 texop
== nir_texop_txs
|| texop
== nir_texop_tg4
);
2460 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2461 SpvImageOperandsLodMask
);
2462 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_lod
);
2465 if (operands
& SpvImageOperandsGradMask
) {
2466 vtn_assert(texop
== nir_texop_txl
);
2467 texop
= nir_texop_txd
;
2468 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2469 SpvImageOperandsGradMask
);
2470 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_ddx
);
2471 (*p
++) = vtn_tex_src(b
, w
[arg
+ 1], nir_tex_src_ddy
);
2474 vtn_fail_if(util_bitcount(operands
& (SpvImageOperandsConstOffsetsMask
|
2475 SpvImageOperandsOffsetMask
|
2476 SpvImageOperandsConstOffsetMask
)) > 1,
2477 "At most one of the ConstOffset, Offset, and ConstOffsets "
2478 "image operands can be used on a given instruction.");
2480 if (operands
& SpvImageOperandsOffsetMask
) {
2481 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2482 SpvImageOperandsOffsetMask
);
2483 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_offset
);
2486 if (operands
& SpvImageOperandsConstOffsetMask
) {
2487 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2488 SpvImageOperandsConstOffsetMask
);
2489 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_offset
);
2492 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2493 vtn_assert(texop
== nir_texop_tg4
);
2494 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2495 SpvImageOperandsConstOffsetsMask
);
2496 gather_offsets
= vtn_value(b
, w
[arg
], vtn_value_type_constant
);
2499 if (operands
& SpvImageOperandsSampleMask
) {
2500 vtn_assert(texop
== nir_texop_txf_ms
);
2501 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2502 SpvImageOperandsSampleMask
);
2503 texop
= nir_texop_txf_ms
;
2504 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_ms_index
);
2507 if (operands
& SpvImageOperandsMinLodMask
) {
2508 vtn_assert(texop
== nir_texop_tex
||
2509 texop
== nir_texop_txb
||
2510 texop
== nir_texop_txd
);
2511 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2512 SpvImageOperandsMinLodMask
);
2513 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_min_lod
);
2517 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2520 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2522 instr
->coord_components
= coord_components
;
2523 instr
->sampler_dim
= sampler_dim
;
2524 instr
->is_array
= is_array
;
2525 instr
->is_shadow
= is_shadow
;
2526 instr
->is_new_style_shadow
=
2527 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2528 instr
->component
= gather_component
;
2530 if (image
&& (image
->access
& ACCESS_NON_UNIFORM
))
2531 instr
->texture_non_uniform
= true;
2533 if (sampler
&& (sampler
->access
& ACCESS_NON_UNIFORM
))
2534 instr
->sampler_non_uniform
= true;
2536 /* for non-query ops, get dest_type from sampler type */
2537 if (dest_type
== nir_type_invalid
) {
2538 switch (glsl_get_sampler_result_type(image_type
)) {
2539 case GLSL_TYPE_FLOAT
: dest_type
= nir_type_float
; break;
2540 case GLSL_TYPE_INT
: dest_type
= nir_type_int
; break;
2541 case GLSL_TYPE_UINT
: dest_type
= nir_type_uint
; break;
2542 case GLSL_TYPE_BOOL
: dest_type
= nir_type_bool
; break;
2544 vtn_fail("Invalid base type for sampler result");
2548 instr
->dest_type
= dest_type
;
2550 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2551 nir_tex_instr_dest_size(instr
), 32, NULL
);
2553 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2554 nir_tex_instr_dest_size(instr
));
2556 if (gather_offsets
) {
2557 vtn_fail_if(gather_offsets
->type
->base_type
!= vtn_base_type_array
||
2558 gather_offsets
->type
->length
!= 4,
2559 "ConstOffsets must be an array of size four of vectors "
2560 "of two integer components");
2562 struct vtn_type
*vec_type
= gather_offsets
->type
->array_element
;
2563 vtn_fail_if(vec_type
->base_type
!= vtn_base_type_vector
||
2564 vec_type
->length
!= 2 ||
2565 !glsl_type_is_integer(vec_type
->type
),
2566 "ConstOffsets must be an array of size four of vectors "
2567 "of two integer components");
2569 unsigned bit_size
= glsl_get_bit_size(vec_type
->type
);
2570 for (uint32_t i
= 0; i
< 4; i
++) {
2571 const nir_const_value
*cvec
=
2572 gather_offsets
->constant
->elements
[i
]->values
;
2573 for (uint32_t j
= 0; j
< 2; j
++) {
2575 case 8: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i8
; break;
2576 case 16: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i16
; break;
2577 case 32: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i32
; break;
2578 case 64: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i64
; break;
2580 vtn_fail("Unsupported bit size: %u", bit_size
);
2586 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
2587 ssa
->def
= &instr
->dest
.ssa
;
2588 vtn_push_ssa(b
, w
[2], ret_type
, ssa
);
2590 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2594 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2595 const uint32_t *w
, nir_src
*src
)
2598 case SpvOpAtomicIIncrement
:
2599 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
2602 case SpvOpAtomicIDecrement
:
2603 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
2606 case SpvOpAtomicISub
:
2608 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
2611 case SpvOpAtomicCompareExchange
:
2612 case SpvOpAtomicCompareExchangeWeak
:
2613 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
2614 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
2617 case SpvOpAtomicExchange
:
2618 case SpvOpAtomicIAdd
:
2619 case SpvOpAtomicSMin
:
2620 case SpvOpAtomicUMin
:
2621 case SpvOpAtomicSMax
:
2622 case SpvOpAtomicUMax
:
2623 case SpvOpAtomicAnd
:
2625 case SpvOpAtomicXor
:
2626 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
2630 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2634 static nir_ssa_def
*
2635 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2637 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
2639 /* The image_load_store intrinsics assume a 4-dim coordinate */
2640 unsigned dim
= glsl_get_vector_elements(coord
->type
);
2641 unsigned swizzle
[4];
2642 for (unsigned i
= 0; i
< 4; i
++)
2643 swizzle
[i
] = MIN2(i
, dim
- 1);
2645 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4);
2648 static nir_ssa_def
*
2649 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2651 if (value
->num_components
== 4)
2655 for (unsigned i
= 0; i
< 4; i
++)
2656 swiz
[i
] = i
< value
->num_components
? i
: 0;
2657 return nir_swizzle(b
, value
, swiz
, 4);
2661 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2662 const uint32_t *w
, unsigned count
)
2664 /* Just get this one out of the way */
2665 if (opcode
== SpvOpImageTexelPointer
) {
2666 struct vtn_value
*val
=
2667 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2668 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2670 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2671 val
->image
->coord
= get_image_coord(b
, w
[4]);
2672 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
2673 val
->image
->lod
= nir_imm_int(&b
->nb
, 0);
2677 struct vtn_image_pointer image
;
2678 SpvScope scope
= SpvScopeInvocation
;
2679 SpvMemorySemanticsMask semantics
= 0;
2682 case SpvOpAtomicExchange
:
2683 case SpvOpAtomicCompareExchange
:
2684 case SpvOpAtomicCompareExchangeWeak
:
2685 case SpvOpAtomicIIncrement
:
2686 case SpvOpAtomicIDecrement
:
2687 case SpvOpAtomicIAdd
:
2688 case SpvOpAtomicISub
:
2689 case SpvOpAtomicLoad
:
2690 case SpvOpAtomicSMin
:
2691 case SpvOpAtomicUMin
:
2692 case SpvOpAtomicSMax
:
2693 case SpvOpAtomicUMax
:
2694 case SpvOpAtomicAnd
:
2696 case SpvOpAtomicXor
:
2697 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
2698 scope
= vtn_constant_uint(b
, w
[4]);
2699 semantics
= vtn_constant_uint(b
, w
[5]);
2702 case SpvOpAtomicStore
:
2703 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
2704 scope
= vtn_constant_uint(b
, w
[2]);
2705 semantics
= vtn_constant_uint(b
, w
[3]);
2708 case SpvOpImageQuerySize
:
2709 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2711 image
.sample
= NULL
;
2715 case SpvOpImageRead
: {
2716 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2717 image
.coord
= get_image_coord(b
, w
[4]);
2719 const SpvImageOperandsMask operands
=
2720 count
> 5 ? w
[5] : SpvImageOperandsMaskNone
;
2722 if (operands
& SpvImageOperandsSampleMask
) {
2723 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
2724 SpvImageOperandsSampleMask
);
2725 image
.sample
= vtn_ssa_value(b
, w
[arg
])->def
;
2727 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2730 if (operands
& SpvImageOperandsMakeTexelVisibleMask
) {
2731 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
2732 "MakeTexelVisible requires NonPrivateTexel to also be set.");
2733 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
2734 SpvImageOperandsMakeTexelVisibleMask
);
2735 semantics
= SpvMemorySemanticsMakeVisibleMask
;
2736 scope
= vtn_constant_uint(b
, w
[arg
]);
2739 if (operands
& SpvImageOperandsLodMask
) {
2740 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
2741 SpvImageOperandsLodMask
);
2742 image
.lod
= vtn_ssa_value(b
, w
[arg
])->def
;
2744 image
.lod
= nir_imm_int(&b
->nb
, 0);
2747 /* TODO: Volatile. */
2752 case SpvOpImageWrite
: {
2753 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2754 image
.coord
= get_image_coord(b
, w
[2]);
2758 const SpvImageOperandsMask operands
=
2759 count
> 4 ? w
[4] : SpvImageOperandsMaskNone
;
2761 if (operands
& SpvImageOperandsSampleMask
) {
2762 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
2763 SpvImageOperandsSampleMask
);
2764 image
.sample
= vtn_ssa_value(b
, w
[arg
])->def
;
2766 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2769 if (operands
& SpvImageOperandsMakeTexelAvailableMask
) {
2770 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
2771 "MakeTexelAvailable requires NonPrivateTexel to also be set.");
2772 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
2773 SpvImageOperandsMakeTexelAvailableMask
);
2774 semantics
= SpvMemorySemanticsMakeAvailableMask
;
2775 scope
= vtn_constant_uint(b
, w
[arg
]);
2778 if (operands
& SpvImageOperandsLodMask
) {
2779 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
2780 SpvImageOperandsLodMask
);
2781 image
.lod
= vtn_ssa_value(b
, w
[arg
])->def
;
2783 image
.lod
= nir_imm_int(&b
->nb
, 0);
2786 /* TODO: Volatile. */
2792 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2795 nir_intrinsic_op op
;
2797 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
2798 OP(ImageQuerySize
, size
)
2800 OP(ImageWrite
, store
)
2801 OP(AtomicLoad
, load
)
2802 OP(AtomicStore
, store
)
2803 OP(AtomicExchange
, atomic_exchange
)
2804 OP(AtomicCompareExchange
, atomic_comp_swap
)
2805 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2806 OP(AtomicIIncrement
, atomic_add
)
2807 OP(AtomicIDecrement
, atomic_add
)
2808 OP(AtomicIAdd
, atomic_add
)
2809 OP(AtomicISub
, atomic_add
)
2810 OP(AtomicSMin
, atomic_imin
)
2811 OP(AtomicUMin
, atomic_umin
)
2812 OP(AtomicSMax
, atomic_imax
)
2813 OP(AtomicUMax
, atomic_umax
)
2814 OP(AtomicAnd
, atomic_and
)
2815 OP(AtomicOr
, atomic_or
)
2816 OP(AtomicXor
, atomic_xor
)
2819 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2822 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
2824 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
2825 intrin
->src
[0] = nir_src_for_ssa(&image_deref
->dest
.ssa
);
2827 /* ImageQuerySize doesn't take any extra parameters */
2828 if (opcode
!= SpvOpImageQuerySize
) {
2829 /* The image coordinate is always 4 components but we may not have that
2830 * many. Swizzle to compensate.
2832 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
2833 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
2836 nir_intrinsic_set_access(intrin
, image
.image
->access
);
2839 case SpvOpAtomicLoad
:
2840 case SpvOpImageQuerySize
:
2841 case SpvOpImageRead
:
2842 if (opcode
== SpvOpImageRead
|| opcode
== SpvOpAtomicLoad
) {
2843 /* Only OpImageRead can support a lod parameter if
2844 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
2845 * intrinsics definition for atomics requires us to set it for
2848 intrin
->src
[3] = nir_src_for_ssa(image
.lod
);
2851 case SpvOpAtomicStore
:
2852 case SpvOpImageWrite
: {
2853 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
2854 nir_ssa_def
*value
= vtn_ssa_value(b
, value_id
)->def
;
2855 /* nir_intrinsic_image_deref_store always takes a vec4 value */
2856 assert(op
== nir_intrinsic_image_deref_store
);
2857 intrin
->num_components
= 4;
2858 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
));
2859 /* Only OpImageWrite can support a lod parameter if
2860 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
2861 * intrinsics definition for atomics requires us to set it for
2864 intrin
->src
[4] = nir_src_for_ssa(image
.lod
);
2868 case SpvOpAtomicCompareExchange
:
2869 case SpvOpAtomicCompareExchangeWeak
:
2870 case SpvOpAtomicIIncrement
:
2871 case SpvOpAtomicIDecrement
:
2872 case SpvOpAtomicExchange
:
2873 case SpvOpAtomicIAdd
:
2874 case SpvOpAtomicISub
:
2875 case SpvOpAtomicSMin
:
2876 case SpvOpAtomicUMin
:
2877 case SpvOpAtomicSMax
:
2878 case SpvOpAtomicUMax
:
2879 case SpvOpAtomicAnd
:
2881 case SpvOpAtomicXor
:
2882 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
2886 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2889 /* Image operations implicitly have the Image storage memory semantics. */
2890 semantics
|= SpvMemorySemanticsImageMemoryMask
;
2892 SpvMemorySemanticsMask before_semantics
;
2893 SpvMemorySemanticsMask after_semantics
;
2894 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
2896 if (before_semantics
)
2897 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
2899 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
2900 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2902 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
2903 intrin
->num_components
= nir_intrinsic_infos
[op
].dest_components
;
2904 if (intrin
->num_components
== 0)
2905 intrin
->num_components
= dest_components
;
2907 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2908 intrin
->num_components
, 32, NULL
);
2910 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2912 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
2913 if (intrin
->num_components
!= dest_components
)
2914 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
2916 struct vtn_value
*val
=
2917 vtn_push_ssa(b
, w
[2], type
, vtn_create_ssa_value(b
, type
->type
));
2918 val
->ssa
->def
= result
;
2920 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2923 if (after_semantics
)
2924 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
2927 static nir_intrinsic_op
2928 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2931 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2932 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2933 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2934 OP(AtomicExchange
, atomic_exchange
)
2935 OP(AtomicCompareExchange
, atomic_comp_swap
)
2936 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2937 OP(AtomicIIncrement
, atomic_add
)
2938 OP(AtomicIDecrement
, atomic_add
)
2939 OP(AtomicIAdd
, atomic_add
)
2940 OP(AtomicISub
, atomic_add
)
2941 OP(AtomicSMin
, atomic_imin
)
2942 OP(AtomicUMin
, atomic_umin
)
2943 OP(AtomicSMax
, atomic_imax
)
2944 OP(AtomicUMax
, atomic_umax
)
2945 OP(AtomicAnd
, atomic_and
)
2946 OP(AtomicOr
, atomic_or
)
2947 OP(AtomicXor
, atomic_xor
)
2950 vtn_fail_with_opcode("Invalid SSBO atomic", opcode
);
2954 static nir_intrinsic_op
2955 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2958 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
2959 OP(AtomicLoad
, read_deref
)
2960 OP(AtomicExchange
, exchange
)
2961 OP(AtomicCompareExchange
, comp_swap
)
2962 OP(AtomicCompareExchangeWeak
, comp_swap
)
2963 OP(AtomicIIncrement
, inc_deref
)
2964 OP(AtomicIDecrement
, post_dec_deref
)
2965 OP(AtomicIAdd
, add_deref
)
2966 OP(AtomicISub
, add_deref
)
2967 OP(AtomicUMin
, min_deref
)
2968 OP(AtomicUMax
, max_deref
)
2969 OP(AtomicAnd
, and_deref
)
2970 OP(AtomicOr
, or_deref
)
2971 OP(AtomicXor
, xor_deref
)
2974 /* We left the following out: AtomicStore, AtomicSMin and
2975 * AtomicSmax. Right now there are not nir intrinsics for them. At this
2976 * moment Atomic Counter support is needed for ARB_spirv support, so is
2977 * only need to support GLSL Atomic Counters that are uints and don't
2978 * allow direct storage.
2980 vtn_fail("Invalid uniform atomic");
2984 static nir_intrinsic_op
2985 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2988 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
2989 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
2990 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
2991 OP(AtomicExchange
, atomic_exchange
)
2992 OP(AtomicCompareExchange
, atomic_comp_swap
)
2993 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2994 OP(AtomicIIncrement
, atomic_add
)
2995 OP(AtomicIDecrement
, atomic_add
)
2996 OP(AtomicIAdd
, atomic_add
)
2997 OP(AtomicISub
, atomic_add
)
2998 OP(AtomicSMin
, atomic_imin
)
2999 OP(AtomicUMin
, atomic_umin
)
3000 OP(AtomicSMax
, atomic_imax
)
3001 OP(AtomicUMax
, atomic_umax
)
3002 OP(AtomicAnd
, atomic_and
)
3003 OP(AtomicOr
, atomic_or
)
3004 OP(AtomicXor
, atomic_xor
)
3007 vtn_fail_with_opcode("Invalid shared atomic", opcode
);
3012 * Handles shared atomics, ssbo atomics and atomic counters.
3015 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
3016 const uint32_t *w
, UNUSED
unsigned count
)
3018 struct vtn_pointer
*ptr
;
3019 nir_intrinsic_instr
*atomic
;
3021 SpvScope scope
= SpvScopeInvocation
;
3022 SpvMemorySemanticsMask semantics
= 0;
3025 case SpvOpAtomicLoad
:
3026 case SpvOpAtomicExchange
:
3027 case SpvOpAtomicCompareExchange
:
3028 case SpvOpAtomicCompareExchangeWeak
:
3029 case SpvOpAtomicIIncrement
:
3030 case SpvOpAtomicIDecrement
:
3031 case SpvOpAtomicIAdd
:
3032 case SpvOpAtomicISub
:
3033 case SpvOpAtomicSMin
:
3034 case SpvOpAtomicUMin
:
3035 case SpvOpAtomicSMax
:
3036 case SpvOpAtomicUMax
:
3037 case SpvOpAtomicAnd
:
3039 case SpvOpAtomicXor
:
3040 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
3041 scope
= vtn_constant_uint(b
, w
[4]);
3042 semantics
= vtn_constant_uint(b
, w
[5]);
3045 case SpvOpAtomicStore
:
3046 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
3047 scope
= vtn_constant_uint(b
, w
[2]);
3048 semantics
= vtn_constant_uint(b
, w
[3]);
3052 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3055 /* uniform as "atomic counter uniform" */
3056 if (ptr
->mode
== vtn_variable_mode_uniform
) {
3057 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3058 const struct glsl_type
*deref_type
= deref
->type
;
3059 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
3060 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3061 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3063 /* SSBO needs to initialize index/offset. In this case we don't need to,
3064 * as that info is already stored on the ptr->var->var nir_variable (see
3065 * vtn_create_variable)
3069 case SpvOpAtomicLoad
:
3070 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3073 case SpvOpAtomicStore
:
3074 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3075 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3078 case SpvOpAtomicExchange
:
3079 case SpvOpAtomicCompareExchange
:
3080 case SpvOpAtomicCompareExchangeWeak
:
3081 case SpvOpAtomicIIncrement
:
3082 case SpvOpAtomicIDecrement
:
3083 case SpvOpAtomicIAdd
:
3084 case SpvOpAtomicISub
:
3085 case SpvOpAtomicSMin
:
3086 case SpvOpAtomicUMin
:
3087 case SpvOpAtomicSMax
:
3088 case SpvOpAtomicUMax
:
3089 case SpvOpAtomicAnd
:
3091 case SpvOpAtomicXor
:
3092 /* Nothing: we don't need to call fill_common_atomic_sources here, as
3093 * atomic counter uniforms doesn't have sources
3098 unreachable("Invalid SPIR-V atomic");
3101 } else if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
3102 nir_ssa_def
*offset
, *index
;
3103 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
3105 assert(ptr
->mode
== vtn_variable_mode_ssbo
);
3107 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(b
, opcode
);
3108 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3112 case SpvOpAtomicLoad
:
3113 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3114 nir_intrinsic_set_align(atomic
, 4, 0);
3115 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3116 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3117 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3120 case SpvOpAtomicStore
:
3121 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3122 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3123 nir_intrinsic_set_align(atomic
, 4, 0);
3124 atomic
->src
[src
++] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
3125 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3126 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3127 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3130 case SpvOpAtomicExchange
:
3131 case SpvOpAtomicCompareExchange
:
3132 case SpvOpAtomicCompareExchangeWeak
:
3133 case SpvOpAtomicIIncrement
:
3134 case SpvOpAtomicIDecrement
:
3135 case SpvOpAtomicIAdd
:
3136 case SpvOpAtomicISub
:
3137 case SpvOpAtomicSMin
:
3138 case SpvOpAtomicUMin
:
3139 case SpvOpAtomicSMax
:
3140 case SpvOpAtomicUMax
:
3141 case SpvOpAtomicAnd
:
3143 case SpvOpAtomicXor
:
3144 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3145 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3146 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3147 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
3151 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3154 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3155 const struct glsl_type
*deref_type
= deref
->type
;
3156 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
3157 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3158 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3161 case SpvOpAtomicLoad
:
3162 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3165 case SpvOpAtomicStore
:
3166 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3167 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3168 atomic
->src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
3171 case SpvOpAtomicExchange
:
3172 case SpvOpAtomicCompareExchange
:
3173 case SpvOpAtomicCompareExchangeWeak
:
3174 case SpvOpAtomicIIncrement
:
3175 case SpvOpAtomicIDecrement
:
3176 case SpvOpAtomicIAdd
:
3177 case SpvOpAtomicISub
:
3178 case SpvOpAtomicSMin
:
3179 case SpvOpAtomicUMin
:
3180 case SpvOpAtomicSMax
:
3181 case SpvOpAtomicUMax
:
3182 case SpvOpAtomicAnd
:
3184 case SpvOpAtomicXor
:
3185 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
3189 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3193 /* Atomic ordering operations will implicitly apply to the atomic operation
3194 * storage class, so include that too.
3196 semantics
|= vtn_storage_class_to_memory_semantics(ptr
->ptr_type
->storage_class
);
3198 SpvMemorySemanticsMask before_semantics
;
3199 SpvMemorySemanticsMask after_semantics
;
3200 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
3202 if (before_semantics
)
3203 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
3205 if (opcode
!= SpvOpAtomicStore
) {
3206 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3208 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
3209 glsl_get_vector_elements(type
->type
),
3210 glsl_get_bit_size(type
->type
), NULL
);
3212 struct vtn_ssa_value
*ssa
= rzalloc(b
, struct vtn_ssa_value
);
3213 ssa
->def
= &atomic
->dest
.ssa
;
3214 ssa
->type
= type
->type
;
3215 vtn_push_ssa(b
, w
[2], type
, ssa
);
3218 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
3220 if (after_semantics
)
3221 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
3224 static nir_alu_instr
*
3225 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
3227 nir_op op
= nir_op_vec(num_components
);
3228 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
3229 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
3231 vec
->dest
.write_mask
= (1 << num_components
) - 1;
3236 struct vtn_ssa_value
*
3237 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
3239 if (src
->transposed
)
3240 return src
->transposed
;
3242 struct vtn_ssa_value
*dest
=
3243 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
3245 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
3246 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
3247 glsl_get_bit_size(src
->type
));
3248 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3249 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
3250 vec
->src
[0].swizzle
[0] = i
;
3252 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
3253 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
3254 vec
->src
[j
].swizzle
[0] = i
;
3257 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3258 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
3261 dest
->transposed
= src
;
3266 static nir_ssa_def
*
3267 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
3268 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
3269 const uint32_t *indices
)
3271 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
3273 for (unsigned i
= 0; i
< num_components
; i
++) {
3274 uint32_t index
= indices
[i
];
3275 if (index
== 0xffffffff) {
3277 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
3278 } else if (index
< src0
->num_components
) {
3279 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
3280 vec
->src
[i
].swizzle
[0] = index
;
3282 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
3283 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
3287 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3289 return &vec
->dest
.dest
.ssa
;
3293 * Concatentates a number of vectors/scalars together to produce a vector
3295 static nir_ssa_def
*
3296 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
3297 unsigned num_srcs
, nir_ssa_def
**srcs
)
3299 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
3301 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3303 * "When constructing a vector, there must be at least two Constituent
3306 vtn_assert(num_srcs
>= 2);
3308 unsigned dest_idx
= 0;
3309 for (unsigned i
= 0; i
< num_srcs
; i
++) {
3310 nir_ssa_def
*src
= srcs
[i
];
3311 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
3312 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
3313 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
3314 vec
->src
[dest_idx
].swizzle
[0] = j
;
3319 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3321 * "When constructing a vector, the total number of components in all
3322 * the operands must equal the number of components in Result Type."
3324 vtn_assert(dest_idx
== num_components
);
3326 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3328 return &vec
->dest
.dest
.ssa
;
3331 static struct vtn_ssa_value
*
3332 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3334 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3335 dest
->type
= src
->type
;
3337 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3338 dest
->def
= src
->def
;
3340 unsigned elems
= glsl_get_length(src
->type
);
3342 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3343 for (unsigned i
= 0; i
< elems
; i
++)
3344 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3350 static struct vtn_ssa_value
*
3351 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3352 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3353 unsigned num_indices
)
3355 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3357 struct vtn_ssa_value
*cur
= dest
;
3359 for (i
= 0; i
< num_indices
- 1; i
++) {
3360 /* If we got a vector here, that means the next index will be trying to
3361 * dereference a scalar.
3363 vtn_fail_if(glsl_type_is_vector_or_scalar(cur
->type
),
3364 "OpCompositeInsert has too many indices.");
3365 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3366 "All indices in an OpCompositeInsert must be in-bounds");
3367 cur
= cur
->elems
[indices
[i
]];
3370 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3371 vtn_fail_if(indices
[i
] >= glsl_get_vector_elements(cur
->type
),
3372 "All indices in an OpCompositeInsert must be in-bounds");
3374 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3375 * the component granularity. In that case, the last index will be
3376 * the index to insert the scalar into the vector.
3379 cur
->def
= nir_vector_insert_imm(&b
->nb
, cur
->def
, insert
->def
, indices
[i
]);
3381 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3382 "All indices in an OpCompositeInsert must be in-bounds");
3383 cur
->elems
[indices
[i
]] = insert
;
3389 static struct vtn_ssa_value
*
3390 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3391 const uint32_t *indices
, unsigned num_indices
)
3393 struct vtn_ssa_value
*cur
= src
;
3394 for (unsigned i
= 0; i
< num_indices
; i
++) {
3395 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3396 vtn_assert(i
== num_indices
- 1);
3397 vtn_fail_if(indices
[i
] >= glsl_get_vector_elements(cur
->type
),
3398 "All indices in an OpCompositeExtract must be in-bounds");
3400 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3401 * the component granularity. The last index will be the index of the
3402 * vector to extract.
3405 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
3406 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
3407 ret
->def
= nir_channel(&b
->nb
, cur
->def
, indices
[i
]);
3410 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3411 "All indices in an OpCompositeExtract must be in-bounds");
3412 cur
= cur
->elems
[indices
[i
]];
3420 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3421 const uint32_t *w
, unsigned count
)
3423 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3424 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, type
->type
);
3427 case SpvOpVectorExtractDynamic
:
3428 ssa
->def
= nir_vector_extract(&b
->nb
, vtn_ssa_value(b
, w
[3])->def
,
3429 vtn_ssa_value(b
, w
[4])->def
);
3432 case SpvOpVectorInsertDynamic
:
3433 ssa
->def
= nir_vector_insert(&b
->nb
, vtn_ssa_value(b
, w
[3])->def
,
3434 vtn_ssa_value(b
, w
[4])->def
,
3435 vtn_ssa_value(b
, w
[5])->def
);
3438 case SpvOpVectorShuffle
:
3439 ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
->type
),
3440 vtn_ssa_value(b
, w
[3])->def
,
3441 vtn_ssa_value(b
, w
[4])->def
,
3445 case SpvOpCompositeConstruct
: {
3446 unsigned elems
= count
- 3;
3448 if (glsl_type_is_vector_or_scalar(type
->type
)) {
3449 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3450 for (unsigned i
= 0; i
< elems
; i
++)
3451 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
3453 vtn_vector_construct(b
, glsl_get_vector_elements(type
->type
),
3456 ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3457 for (unsigned i
= 0; i
< elems
; i
++)
3458 ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3462 case SpvOpCompositeExtract
:
3463 ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3467 case SpvOpCompositeInsert
:
3468 ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3469 vtn_ssa_value(b
, w
[3]),
3473 case SpvOpCopyLogical
:
3474 ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3476 case SpvOpCopyObject
:
3477 vtn_copy_value(b
, w
[3], w
[2]);
3481 vtn_fail_with_opcode("unknown composite operation", opcode
);
3484 vtn_push_ssa(b
, w
[2], type
, ssa
);
3488 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3490 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3491 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3495 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3496 SpvMemorySemanticsMask semantics
)
3498 if (b
->shader
->options
->use_scoped_memory_barrier
) {
3499 vtn_emit_scoped_memory_barrier(b
, scope
, semantics
);
3503 static const SpvMemorySemanticsMask all_memory_semantics
=
3504 SpvMemorySemanticsUniformMemoryMask
|
3505 SpvMemorySemanticsWorkgroupMemoryMask
|
3506 SpvMemorySemanticsAtomicCounterMemoryMask
|
3507 SpvMemorySemanticsImageMemoryMask
|
3508 SpvMemorySemanticsOutputMemoryMask
;
3510 /* If we're not actually doing a memory barrier, bail */
3511 if (!(semantics
& all_memory_semantics
))
3514 /* GL and Vulkan don't have these */
3515 vtn_assert(scope
!= SpvScopeCrossDevice
);
3517 if (scope
== SpvScopeSubgroup
)
3518 return; /* Nothing to do here */
3520 if (scope
== SpvScopeWorkgroup
) {
3521 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3525 /* There's only two scopes thing left */
3526 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3528 /* Map the GLSL memoryBarrier() construct to the corresponding NIR one. */
3529 static const SpvMemorySemanticsMask glsl_memory_barrier
=
3530 SpvMemorySemanticsUniformMemoryMask
|
3531 SpvMemorySemanticsWorkgroupMemoryMask
|
3532 SpvMemorySemanticsImageMemoryMask
;
3533 if ((semantics
& glsl_memory_barrier
) == glsl_memory_barrier
) {
3534 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3535 semantics
&= ~(glsl_memory_barrier
| SpvMemorySemanticsAtomicCounterMemoryMask
);
3538 /* Issue a bunch of more specific barriers */
3539 uint32_t bits
= semantics
;
3541 SpvMemorySemanticsMask semantic
= 1 << u_bit_scan(&bits
);
3543 case SpvMemorySemanticsUniformMemoryMask
:
3544 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3546 case SpvMemorySemanticsWorkgroupMemoryMask
:
3547 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3549 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3550 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3552 case SpvMemorySemanticsImageMemoryMask
:
3553 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3555 case SpvMemorySemanticsOutputMemoryMask
:
3556 if (b
->nb
.shader
->info
.stage
== MESA_SHADER_TESS_CTRL
)
3557 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_tcs_patch
);
3566 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3567 const uint32_t *w
, UNUSED
unsigned count
)
3570 case SpvOpEmitVertex
:
3571 case SpvOpEmitStreamVertex
:
3572 case SpvOpEndPrimitive
:
3573 case SpvOpEndStreamPrimitive
: {
3574 nir_intrinsic_op intrinsic_op
;
3576 case SpvOpEmitVertex
:
3577 case SpvOpEmitStreamVertex
:
3578 intrinsic_op
= nir_intrinsic_emit_vertex
;
3580 case SpvOpEndPrimitive
:
3581 case SpvOpEndStreamPrimitive
:
3582 intrinsic_op
= nir_intrinsic_end_primitive
;
3585 unreachable("Invalid opcode");
3588 nir_intrinsic_instr
*intrin
=
3589 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3592 case SpvOpEmitStreamVertex
:
3593 case SpvOpEndStreamPrimitive
: {
3594 unsigned stream
= vtn_constant_uint(b
, w
[1]);
3595 nir_intrinsic_set_stream_id(intrin
, stream
);
3603 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3607 case SpvOpMemoryBarrier
: {
3608 SpvScope scope
= vtn_constant_uint(b
, w
[1]);
3609 SpvMemorySemanticsMask semantics
= vtn_constant_uint(b
, w
[2]);
3610 vtn_emit_memory_barrier(b
, scope
, semantics
);
3614 case SpvOpControlBarrier
: {
3615 SpvScope execution_scope
= vtn_constant_uint(b
, w
[1]);
3616 SpvScope memory_scope
= vtn_constant_uint(b
, w
[2]);
3617 SpvMemorySemanticsMask memory_semantics
= vtn_constant_uint(b
, w
[3]);
3619 /* GLSLang, prior to commit 8297936dd6eb3, emitted OpControlBarrier with
3620 * memory semantics of None for GLSL barrier().
3621 * And before that, prior to c3f1cdfa, emitted the OpControlBarrier with
3622 * Device instead of Workgroup for execution scope.
3624 if (b
->wa_glslang_cs_barrier
&&
3625 b
->nb
.shader
->info
.stage
== MESA_SHADER_COMPUTE
&&
3626 (execution_scope
== SpvScopeWorkgroup
||
3627 execution_scope
== SpvScopeDevice
) &&
3628 memory_semantics
== SpvMemorySemanticsMaskNone
) {
3629 execution_scope
= SpvScopeWorkgroup
;
3630 memory_scope
= SpvScopeWorkgroup
;
3631 memory_semantics
= SpvMemorySemanticsAcquireReleaseMask
|
3632 SpvMemorySemanticsWorkgroupMemoryMask
;
3635 /* From the SPIR-V spec:
3637 * "When used with the TessellationControl execution model, it also
3638 * implicitly synchronizes the Output Storage Class: Writes to Output
3639 * variables performed by any invocation executed prior to a
3640 * OpControlBarrier will be visible to any other invocation after
3641 * return from that OpControlBarrier."
3643 if (b
->nb
.shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
3644 memory_semantics
&= ~(SpvMemorySemanticsAcquireMask
|
3645 SpvMemorySemanticsReleaseMask
|
3646 SpvMemorySemanticsAcquireReleaseMask
|
3647 SpvMemorySemanticsSequentiallyConsistentMask
);
3648 memory_semantics
|= SpvMemorySemanticsAcquireReleaseMask
|
3649 SpvMemorySemanticsOutputMemoryMask
;
3652 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
3654 if (execution_scope
== SpvScopeWorkgroup
)
3655 vtn_emit_barrier(b
, nir_intrinsic_control_barrier
);
3660 unreachable("unknown barrier instruction");
3665 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
3666 SpvExecutionMode mode
)
3669 case SpvExecutionModeInputPoints
:
3670 case SpvExecutionModeOutputPoints
:
3671 return 0; /* GL_POINTS */
3672 case SpvExecutionModeInputLines
:
3673 return 1; /* GL_LINES */
3674 case SpvExecutionModeInputLinesAdjacency
:
3675 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3676 case SpvExecutionModeTriangles
:
3677 return 4; /* GL_TRIANGLES */
3678 case SpvExecutionModeInputTrianglesAdjacency
:
3679 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3680 case SpvExecutionModeQuads
:
3681 return 7; /* GL_QUADS */
3682 case SpvExecutionModeIsolines
:
3683 return 0x8E7A; /* GL_ISOLINES */
3684 case SpvExecutionModeOutputLineStrip
:
3685 return 3; /* GL_LINE_STRIP */
3686 case SpvExecutionModeOutputTriangleStrip
:
3687 return 5; /* GL_TRIANGLE_STRIP */
3689 vtn_fail("Invalid primitive type: %s (%u)",
3690 spirv_executionmode_to_string(mode
), mode
);
3695 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
3696 SpvExecutionMode mode
)
3699 case SpvExecutionModeInputPoints
:
3701 case SpvExecutionModeInputLines
:
3703 case SpvExecutionModeInputLinesAdjacency
:
3705 case SpvExecutionModeTriangles
:
3707 case SpvExecutionModeInputTrianglesAdjacency
:
3710 vtn_fail("Invalid GS input mode: %s (%u)",
3711 spirv_executionmode_to_string(mode
), mode
);
3715 static gl_shader_stage
3716 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
3719 case SpvExecutionModelVertex
:
3720 return MESA_SHADER_VERTEX
;
3721 case SpvExecutionModelTessellationControl
:
3722 return MESA_SHADER_TESS_CTRL
;
3723 case SpvExecutionModelTessellationEvaluation
:
3724 return MESA_SHADER_TESS_EVAL
;
3725 case SpvExecutionModelGeometry
:
3726 return MESA_SHADER_GEOMETRY
;
3727 case SpvExecutionModelFragment
:
3728 return MESA_SHADER_FRAGMENT
;
3729 case SpvExecutionModelGLCompute
:
3730 return MESA_SHADER_COMPUTE
;
3731 case SpvExecutionModelKernel
:
3732 return MESA_SHADER_KERNEL
;
3734 vtn_fail("Unsupported execution model: %s (%u)",
3735 spirv_executionmodel_to_string(model
), model
);
3739 #define spv_check_supported(name, cap) do { \
3740 if (!(b->options && b->options->caps.name)) \
3741 vtn_warn("Unsupported SPIR-V capability: %s (%u)", \
3742 spirv_capability_to_string(cap), cap); \
3747 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
3750 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
3751 /* Let this be a name label regardless */
3752 unsigned name_words
;
3753 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
3755 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
3756 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
3759 vtn_assert(b
->entry_point
== NULL
);
3760 b
->entry_point
= entry_point
;
3764 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3765 const uint32_t *w
, unsigned count
)
3772 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
3773 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
3774 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
3775 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
3776 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
3777 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
3780 uint32_t version
= w
[2];
3783 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
3785 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
3789 case SpvOpSourceExtension
:
3790 case SpvOpSourceContinued
:
3791 case SpvOpExtension
:
3792 case SpvOpModuleProcessed
:
3793 /* Unhandled, but these are for debug so that's ok. */
3796 case SpvOpCapability
: {
3797 SpvCapability cap
= w
[1];
3799 case SpvCapabilityMatrix
:
3800 case SpvCapabilityShader
:
3801 case SpvCapabilityGeometry
:
3802 case SpvCapabilityGeometryPointSize
:
3803 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
3804 case SpvCapabilitySampledImageArrayDynamicIndexing
:
3805 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
3806 case SpvCapabilityStorageImageArrayDynamicIndexing
:
3807 case SpvCapabilityImageRect
:
3808 case SpvCapabilitySampledRect
:
3809 case SpvCapabilitySampled1D
:
3810 case SpvCapabilityImage1D
:
3811 case SpvCapabilitySampledCubeArray
:
3812 case SpvCapabilityImageCubeArray
:
3813 case SpvCapabilitySampledBuffer
:
3814 case SpvCapabilityImageBuffer
:
3815 case SpvCapabilityImageQuery
:
3816 case SpvCapabilityDerivativeControl
:
3817 case SpvCapabilityInterpolationFunction
:
3818 case SpvCapabilityMultiViewport
:
3819 case SpvCapabilitySampleRateShading
:
3820 case SpvCapabilityClipDistance
:
3821 case SpvCapabilityCullDistance
:
3822 case SpvCapabilityInputAttachment
:
3823 case SpvCapabilityImageGatherExtended
:
3824 case SpvCapabilityStorageImageExtendedFormats
:
3825 case SpvCapabilityVector16
:
3828 case SpvCapabilityLinkage
:
3829 case SpvCapabilityFloat16Buffer
:
3830 case SpvCapabilitySparseResidency
:
3831 vtn_warn("Unsupported SPIR-V capability: %s",
3832 spirv_capability_to_string(cap
));
3835 case SpvCapabilityMinLod
:
3836 spv_check_supported(min_lod
, cap
);
3839 case SpvCapabilityAtomicStorage
:
3840 spv_check_supported(atomic_storage
, cap
);
3843 case SpvCapabilityFloat64
:
3844 spv_check_supported(float64
, cap
);
3846 case SpvCapabilityInt64
:
3847 spv_check_supported(int64
, cap
);
3849 case SpvCapabilityInt16
:
3850 spv_check_supported(int16
, cap
);
3852 case SpvCapabilityInt8
:
3853 spv_check_supported(int8
, cap
);
3856 case SpvCapabilityTransformFeedback
:
3857 spv_check_supported(transform_feedback
, cap
);
3860 case SpvCapabilityGeometryStreams
:
3861 spv_check_supported(geometry_streams
, cap
);
3864 case SpvCapabilityInt64Atomics
:
3865 spv_check_supported(int64_atomics
, cap
);
3868 case SpvCapabilityStorageImageMultisample
:
3869 spv_check_supported(storage_image_ms
, cap
);
3872 case SpvCapabilityAddresses
:
3873 spv_check_supported(address
, cap
);
3876 case SpvCapabilityKernel
:
3877 spv_check_supported(kernel
, cap
);
3880 case SpvCapabilityImageBasic
:
3881 case SpvCapabilityImageReadWrite
:
3882 case SpvCapabilityImageMipmap
:
3883 case SpvCapabilityPipes
:
3884 case SpvCapabilityDeviceEnqueue
:
3885 case SpvCapabilityLiteralSampler
:
3886 case SpvCapabilityGenericPointer
:
3887 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
3888 spirv_capability_to_string(cap
));
3891 case SpvCapabilityImageMSArray
:
3892 spv_check_supported(image_ms_array
, cap
);
3895 case SpvCapabilityTessellation
:
3896 case SpvCapabilityTessellationPointSize
:
3897 spv_check_supported(tessellation
, cap
);
3900 case SpvCapabilityDrawParameters
:
3901 spv_check_supported(draw_parameters
, cap
);
3904 case SpvCapabilityStorageImageReadWithoutFormat
:
3905 spv_check_supported(image_read_without_format
, cap
);
3908 case SpvCapabilityStorageImageWriteWithoutFormat
:
3909 spv_check_supported(image_write_without_format
, cap
);
3912 case SpvCapabilityDeviceGroup
:
3913 spv_check_supported(device_group
, cap
);
3916 case SpvCapabilityMultiView
:
3917 spv_check_supported(multiview
, cap
);
3920 case SpvCapabilityGroupNonUniform
:
3921 spv_check_supported(subgroup_basic
, cap
);
3924 case SpvCapabilitySubgroupVoteKHR
:
3925 case SpvCapabilityGroupNonUniformVote
:
3926 spv_check_supported(subgroup_vote
, cap
);
3929 case SpvCapabilitySubgroupBallotKHR
:
3930 case SpvCapabilityGroupNonUniformBallot
:
3931 spv_check_supported(subgroup_ballot
, cap
);
3934 case SpvCapabilityGroupNonUniformShuffle
:
3935 case SpvCapabilityGroupNonUniformShuffleRelative
:
3936 spv_check_supported(subgroup_shuffle
, cap
);
3939 case SpvCapabilityGroupNonUniformQuad
:
3940 spv_check_supported(subgroup_quad
, cap
);
3943 case SpvCapabilityGroupNonUniformArithmetic
:
3944 case SpvCapabilityGroupNonUniformClustered
:
3945 spv_check_supported(subgroup_arithmetic
, cap
);
3948 case SpvCapabilityGroups
:
3949 spv_check_supported(amd_shader_ballot
, cap
);
3952 case SpvCapabilityVariablePointersStorageBuffer
:
3953 case SpvCapabilityVariablePointers
:
3954 spv_check_supported(variable_pointers
, cap
);
3955 b
->variable_pointers
= true;
3958 case SpvCapabilityStorageUniformBufferBlock16
:
3959 case SpvCapabilityStorageUniform16
:
3960 case SpvCapabilityStoragePushConstant16
:
3961 case SpvCapabilityStorageInputOutput16
:
3962 spv_check_supported(storage_16bit
, cap
);
3965 case SpvCapabilityShaderLayer
:
3966 case SpvCapabilityShaderViewportIndex
:
3967 case SpvCapabilityShaderViewportIndexLayerEXT
:
3968 spv_check_supported(shader_viewport_index_layer
, cap
);
3971 case SpvCapabilityStorageBuffer8BitAccess
:
3972 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
3973 case SpvCapabilityStoragePushConstant8
:
3974 spv_check_supported(storage_8bit
, cap
);
3977 case SpvCapabilityShaderNonUniformEXT
:
3978 spv_check_supported(descriptor_indexing
, cap
);
3981 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
3982 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
3983 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
3984 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
3987 case SpvCapabilityUniformBufferArrayNonUniformIndexingEXT
:
3988 case SpvCapabilitySampledImageArrayNonUniformIndexingEXT
:
3989 case SpvCapabilityStorageBufferArrayNonUniformIndexingEXT
:
3990 case SpvCapabilityStorageImageArrayNonUniformIndexingEXT
:
3991 case SpvCapabilityInputAttachmentArrayNonUniformIndexingEXT
:
3992 case SpvCapabilityUniformTexelBufferArrayNonUniformIndexingEXT
:
3993 case SpvCapabilityStorageTexelBufferArrayNonUniformIndexingEXT
:
3994 spv_check_supported(descriptor_array_non_uniform_indexing
, cap
);
3997 case SpvCapabilityRuntimeDescriptorArrayEXT
:
3998 spv_check_supported(runtime_descriptor_array
, cap
);
4001 case SpvCapabilityStencilExportEXT
:
4002 spv_check_supported(stencil_export
, cap
);
4005 case SpvCapabilitySampleMaskPostDepthCoverage
:
4006 spv_check_supported(post_depth_coverage
, cap
);
4009 case SpvCapabilityDenormFlushToZero
:
4010 case SpvCapabilityDenormPreserve
:
4011 case SpvCapabilitySignedZeroInfNanPreserve
:
4012 case SpvCapabilityRoundingModeRTE
:
4013 case SpvCapabilityRoundingModeRTZ
:
4014 spv_check_supported(float_controls
, cap
);
4017 case SpvCapabilityPhysicalStorageBufferAddresses
:
4018 spv_check_supported(physical_storage_buffer_address
, cap
);
4021 case SpvCapabilityComputeDerivativeGroupQuadsNV
:
4022 case SpvCapabilityComputeDerivativeGroupLinearNV
:
4023 spv_check_supported(derivative_group
, cap
);
4026 case SpvCapabilityFloat16
:
4027 spv_check_supported(float16
, cap
);
4030 case SpvCapabilityFragmentShaderSampleInterlockEXT
:
4031 spv_check_supported(fragment_shader_sample_interlock
, cap
);
4034 case SpvCapabilityFragmentShaderPixelInterlockEXT
:
4035 spv_check_supported(fragment_shader_pixel_interlock
, cap
);
4038 case SpvCapabilityDemoteToHelperInvocationEXT
:
4039 spv_check_supported(demote_to_helper_invocation
, cap
);
4042 case SpvCapabilityShaderClockKHR
:
4043 spv_check_supported(shader_clock
, cap
);
4046 case SpvCapabilityVulkanMemoryModel
:
4047 spv_check_supported(vk_memory_model
, cap
);
4050 case SpvCapabilityVulkanMemoryModelDeviceScope
:
4051 spv_check_supported(vk_memory_model_device_scope
, cap
);
4054 case SpvCapabilityImageReadWriteLodAMD
:
4055 spv_check_supported(amd_image_read_write_lod
, cap
);
4058 case SpvCapabilityIntegerFunctions2INTEL
:
4059 spv_check_supported(integer_functions2
, cap
);
4062 case SpvCapabilityFragmentMaskAMD
:
4063 spv_check_supported(amd_fragment_mask
, cap
);
4066 case SpvCapabilityImageGatherBiasLodAMD
:
4067 spv_check_supported(amd_image_gather_bias_lod
, cap
);
4071 vtn_fail("Unhandled capability: %s (%u)",
4072 spirv_capability_to_string(cap
), cap
);
4077 case SpvOpExtInstImport
:
4078 vtn_handle_extension(b
, opcode
, w
, count
);
4081 case SpvOpMemoryModel
:
4083 case SpvAddressingModelPhysical32
:
4084 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4085 "AddressingModelPhysical32 only supported for kernels");
4086 b
->shader
->info
.cs
.ptr_size
= 32;
4087 b
->physical_ptrs
= true;
4088 b
->options
->shared_addr_format
= nir_address_format_32bit_global
;
4089 b
->options
->global_addr_format
= nir_address_format_32bit_global
;
4090 b
->options
->temp_addr_format
= nir_address_format_32bit_global
;
4092 case SpvAddressingModelPhysical64
:
4093 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4094 "AddressingModelPhysical64 only supported for kernels");
4095 b
->shader
->info
.cs
.ptr_size
= 64;
4096 b
->physical_ptrs
= true;
4097 b
->options
->shared_addr_format
= nir_address_format_64bit_global
;
4098 b
->options
->global_addr_format
= nir_address_format_64bit_global
;
4099 b
->options
->temp_addr_format
= nir_address_format_64bit_global
;
4101 case SpvAddressingModelLogical
:
4102 vtn_fail_if(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
,
4103 "AddressingModelLogical only supported for shaders");
4104 b
->physical_ptrs
= false;
4106 case SpvAddressingModelPhysicalStorageBuffer64
:
4107 vtn_fail_if(!b
->options
||
4108 !b
->options
->caps
.physical_storage_buffer_address
,
4109 "AddressingModelPhysicalStorageBuffer64 not supported");
4112 vtn_fail("Unknown addressing model: %s (%u)",
4113 spirv_addressingmodel_to_string(w
[1]), w
[1]);
4118 case SpvMemoryModelSimple
:
4119 case SpvMemoryModelGLSL450
:
4120 case SpvMemoryModelOpenCL
:
4122 case SpvMemoryModelVulkan
:
4123 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
4124 "Vulkan memory model is unsupported by this driver");
4127 vtn_fail("Unsupported memory model: %s",
4128 spirv_memorymodel_to_string(w
[2]));
4133 case SpvOpEntryPoint
:
4134 vtn_handle_entry_point(b
, w
, count
);
4138 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
4139 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4143 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4146 case SpvOpMemberName
:
4150 case SpvOpExecutionMode
:
4151 case SpvOpExecutionModeId
:
4152 case SpvOpDecorationGroup
:
4154 case SpvOpDecorateId
:
4155 case SpvOpMemberDecorate
:
4156 case SpvOpGroupDecorate
:
4157 case SpvOpGroupMemberDecorate
:
4158 case SpvOpDecorateString
:
4159 case SpvOpMemberDecorateString
:
4160 vtn_handle_decoration(b
, opcode
, w
, count
);
4163 case SpvOpExtInst
: {
4164 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4165 if (val
->ext_handler
== vtn_handle_non_semantic_instruction
) {
4166 /* NonSemantic extended instructions are acceptable in preamble. */
4167 vtn_handle_non_semantic_instruction(b
, w
[4], w
, count
);
4170 return false; /* End of preamble. */
4175 return false; /* End of preamble */
4182 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4183 const struct vtn_decoration
*mode
, UNUSED
void *data
)
4185 vtn_assert(b
->entry_point
== entry_point
);
4187 switch(mode
->exec_mode
) {
4188 case SpvExecutionModeOriginUpperLeft
:
4189 case SpvExecutionModeOriginLowerLeft
:
4190 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4191 b
->shader
->info
.fs
.origin_upper_left
=
4192 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
4195 case SpvExecutionModeEarlyFragmentTests
:
4196 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4197 b
->shader
->info
.fs
.early_fragment_tests
= true;
4200 case SpvExecutionModePostDepthCoverage
:
4201 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4202 b
->shader
->info
.fs
.post_depth_coverage
= true;
4205 case SpvExecutionModeInvocations
:
4206 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4207 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->operands
[0]);
4210 case SpvExecutionModeDepthReplacing
:
4211 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4212 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
4214 case SpvExecutionModeDepthGreater
:
4215 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4216 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
4218 case SpvExecutionModeDepthLess
:
4219 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4220 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
4222 case SpvExecutionModeDepthUnchanged
:
4223 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4224 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
4227 case SpvExecutionModeLocalSize
:
4228 vtn_assert(gl_shader_stage_is_compute(b
->shader
->info
.stage
));
4229 b
->shader
->info
.cs
.local_size
[0] = mode
->operands
[0];
4230 b
->shader
->info
.cs
.local_size
[1] = mode
->operands
[1];
4231 b
->shader
->info
.cs
.local_size
[2] = mode
->operands
[2];
4234 case SpvExecutionModeLocalSizeId
:
4235 b
->shader
->info
.cs
.local_size
[0] = vtn_constant_uint(b
, mode
->operands
[0]);
4236 b
->shader
->info
.cs
.local_size
[1] = vtn_constant_uint(b
, mode
->operands
[1]);
4237 b
->shader
->info
.cs
.local_size
[2] = vtn_constant_uint(b
, mode
->operands
[2]);
4240 case SpvExecutionModeLocalSizeHint
:
4241 case SpvExecutionModeLocalSizeHintId
:
4242 break; /* Nothing to do with this */
4244 case SpvExecutionModeOutputVertices
:
4245 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4246 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4247 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->operands
[0];
4249 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4250 b
->shader
->info
.gs
.vertices_out
= mode
->operands
[0];
4254 case SpvExecutionModeInputPoints
:
4255 case SpvExecutionModeInputLines
:
4256 case SpvExecutionModeInputLinesAdjacency
:
4257 case SpvExecutionModeTriangles
:
4258 case SpvExecutionModeInputTrianglesAdjacency
:
4259 case SpvExecutionModeQuads
:
4260 case SpvExecutionModeIsolines
:
4261 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4262 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4263 b
->shader
->info
.tess
.primitive_mode
=
4264 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4266 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4267 b
->shader
->info
.gs
.vertices_in
=
4268 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
4269 b
->shader
->info
.gs
.input_primitive
=
4270 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4274 case SpvExecutionModeOutputPoints
:
4275 case SpvExecutionModeOutputLineStrip
:
4276 case SpvExecutionModeOutputTriangleStrip
:
4277 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4278 b
->shader
->info
.gs
.output_primitive
=
4279 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4282 case SpvExecutionModeSpacingEqual
:
4283 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4284 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4285 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
4287 case SpvExecutionModeSpacingFractionalEven
:
4288 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4289 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4290 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
4292 case SpvExecutionModeSpacingFractionalOdd
:
4293 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4294 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4295 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
4297 case SpvExecutionModeVertexOrderCw
:
4298 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4299 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4300 b
->shader
->info
.tess
.ccw
= false;
4302 case SpvExecutionModeVertexOrderCcw
:
4303 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4304 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4305 b
->shader
->info
.tess
.ccw
= true;
4307 case SpvExecutionModePointMode
:
4308 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4309 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4310 b
->shader
->info
.tess
.point_mode
= true;
4313 case SpvExecutionModePixelCenterInteger
:
4314 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4315 b
->shader
->info
.fs
.pixel_center_integer
= true;
4318 case SpvExecutionModeXfb
:
4319 b
->shader
->info
.has_transform_feedback_varyings
= true;
4322 case SpvExecutionModeVecTypeHint
:
4325 case SpvExecutionModeContractionOff
:
4326 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
4327 vtn_warn("ExectionMode only allowed for CL-style kernels: %s",
4328 spirv_executionmode_to_string(mode
->exec_mode
));
4333 case SpvExecutionModeStencilRefReplacingEXT
:
4334 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4337 case SpvExecutionModeDerivativeGroupQuadsNV
:
4338 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4339 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_QUADS
;
4342 case SpvExecutionModeDerivativeGroupLinearNV
:
4343 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4344 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_LINEAR
;
4347 case SpvExecutionModePixelInterlockOrderedEXT
:
4348 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4349 b
->shader
->info
.fs
.pixel_interlock_ordered
= true;
4352 case SpvExecutionModePixelInterlockUnorderedEXT
:
4353 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4354 b
->shader
->info
.fs
.pixel_interlock_unordered
= true;
4357 case SpvExecutionModeSampleInterlockOrderedEXT
:
4358 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4359 b
->shader
->info
.fs
.sample_interlock_ordered
= true;
4362 case SpvExecutionModeSampleInterlockUnorderedEXT
:
4363 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4364 b
->shader
->info
.fs
.sample_interlock_unordered
= true;
4367 case SpvExecutionModeDenormPreserve
:
4368 case SpvExecutionModeDenormFlushToZero
:
4369 case SpvExecutionModeSignedZeroInfNanPreserve
:
4370 case SpvExecutionModeRoundingModeRTE
:
4371 case SpvExecutionModeRoundingModeRTZ
:
4372 /* Already handled in vtn_handle_rounding_mode_in_execution_mode() */
4376 vtn_fail("Unhandled execution mode: %s (%u)",
4377 spirv_executionmode_to_string(mode
->exec_mode
),
4383 vtn_handle_rounding_mode_in_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4384 const struct vtn_decoration
*mode
, void *data
)
4386 vtn_assert(b
->entry_point
== entry_point
);
4388 unsigned execution_mode
= 0;
4390 switch(mode
->exec_mode
) {
4391 case SpvExecutionModeDenormPreserve
:
4392 switch (mode
->operands
[0]) {
4393 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP16
; break;
4394 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP32
; break;
4395 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP64
; break;
4396 default: vtn_fail("Floating point type not supported");
4399 case SpvExecutionModeDenormFlushToZero
:
4400 switch (mode
->operands
[0]) {
4401 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
; break;
4402 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32
; break;
4403 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64
; break;
4404 default: vtn_fail("Floating point type not supported");
4407 case SpvExecutionModeSignedZeroInfNanPreserve
:
4408 switch (mode
->operands
[0]) {
4409 case 16: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16
; break;
4410 case 32: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32
; break;
4411 case 64: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64
; break;
4412 default: vtn_fail("Floating point type not supported");
4415 case SpvExecutionModeRoundingModeRTE
:
4416 switch (mode
->operands
[0]) {
4417 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16
; break;
4418 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32
; break;
4419 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64
; break;
4420 default: vtn_fail("Floating point type not supported");
4423 case SpvExecutionModeRoundingModeRTZ
:
4424 switch (mode
->operands
[0]) {
4425 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
; break;
4426 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
; break;
4427 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
; break;
4428 default: vtn_fail("Floating point type not supported");
4436 b
->shader
->info
.float_controls_execution_mode
|= execution_mode
;
4440 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4441 const uint32_t *w
, unsigned count
)
4443 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
4447 case SpvOpSourceContinued
:
4448 case SpvOpSourceExtension
:
4449 case SpvOpExtension
:
4450 case SpvOpCapability
:
4451 case SpvOpExtInstImport
:
4452 case SpvOpMemoryModel
:
4453 case SpvOpEntryPoint
:
4454 case SpvOpExecutionMode
:
4457 case SpvOpMemberName
:
4458 case SpvOpDecorationGroup
:
4460 case SpvOpDecorateId
:
4461 case SpvOpMemberDecorate
:
4462 case SpvOpGroupDecorate
:
4463 case SpvOpGroupMemberDecorate
:
4464 case SpvOpDecorateString
:
4465 case SpvOpMemberDecorateString
:
4466 vtn_fail("Invalid opcode types and variables section");
4472 case SpvOpTypeFloat
:
4473 case SpvOpTypeVector
:
4474 case SpvOpTypeMatrix
:
4475 case SpvOpTypeImage
:
4476 case SpvOpTypeSampler
:
4477 case SpvOpTypeSampledImage
:
4478 case SpvOpTypeArray
:
4479 case SpvOpTypeRuntimeArray
:
4480 case SpvOpTypeStruct
:
4481 case SpvOpTypeOpaque
:
4482 case SpvOpTypePointer
:
4483 case SpvOpTypeForwardPointer
:
4484 case SpvOpTypeFunction
:
4485 case SpvOpTypeEvent
:
4486 case SpvOpTypeDeviceEvent
:
4487 case SpvOpTypeReserveId
:
4488 case SpvOpTypeQueue
:
4490 vtn_handle_type(b
, opcode
, w
, count
);
4493 case SpvOpConstantTrue
:
4494 case SpvOpConstantFalse
:
4496 case SpvOpConstantComposite
:
4497 case SpvOpConstantSampler
:
4498 case SpvOpConstantNull
:
4499 case SpvOpSpecConstantTrue
:
4500 case SpvOpSpecConstantFalse
:
4501 case SpvOpSpecConstant
:
4502 case SpvOpSpecConstantComposite
:
4503 case SpvOpSpecConstantOp
:
4504 vtn_handle_constant(b
, opcode
, w
, count
);
4509 vtn_handle_variables(b
, opcode
, w
, count
);
4512 case SpvOpExtInst
: {
4513 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4514 /* NonSemantic extended instructions are acceptable in preamble, others
4515 * will indicate the end of preamble.
4517 return val
->ext_handler
== vtn_handle_non_semantic_instruction
;
4521 return false; /* End of preamble */
4527 static struct vtn_ssa_value
*
4528 vtn_nir_select(struct vtn_builder
*b
, struct vtn_ssa_value
*src0
,
4529 struct vtn_ssa_value
*src1
, struct vtn_ssa_value
*src2
)
4531 struct vtn_ssa_value
*dest
= rzalloc(b
, struct vtn_ssa_value
);
4532 dest
->type
= src1
->type
;
4534 if (glsl_type_is_vector_or_scalar(src1
->type
)) {
4535 dest
->def
= nir_bcsel(&b
->nb
, src0
->def
, src1
->def
, src2
->def
);
4537 unsigned elems
= glsl_get_length(src1
->type
);
4539 dest
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
4540 for (unsigned i
= 0; i
< elems
; i
++) {
4541 dest
->elems
[i
] = vtn_nir_select(b
, src0
,
4542 src1
->elems
[i
], src2
->elems
[i
]);
4550 vtn_handle_select(struct vtn_builder
*b
, SpvOp opcode
,
4551 const uint32_t *w
, unsigned count
)
4553 /* Handle OpSelect up-front here because it needs to be able to handle
4554 * pointers and not just regular vectors and scalars.
4556 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
4557 struct vtn_value
*cond_val
= vtn_untyped_value(b
, w
[3]);
4558 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
4559 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
4561 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4562 obj2_val
->type
!= res_val
->type
,
4563 "Object types must match the result type in OpSelect");
4565 vtn_fail_if((cond_val
->type
->base_type
!= vtn_base_type_scalar
&&
4566 cond_val
->type
->base_type
!= vtn_base_type_vector
) ||
4567 !glsl_type_is_boolean(cond_val
->type
->type
),
4568 "OpSelect must have either a vector of booleans or "
4569 "a boolean as Condition type");
4571 vtn_fail_if(cond_val
->type
->base_type
== vtn_base_type_vector
&&
4572 (res_val
->type
->base_type
!= vtn_base_type_vector
||
4573 res_val
->type
->length
!= cond_val
->type
->length
),
4574 "When Condition type in OpSelect is a vector, the Result "
4575 "type must be a vector of the same length");
4577 switch (res_val
->type
->base_type
) {
4578 case vtn_base_type_scalar
:
4579 case vtn_base_type_vector
:
4580 case vtn_base_type_matrix
:
4581 case vtn_base_type_array
:
4582 case vtn_base_type_struct
:
4585 case vtn_base_type_pointer
:
4586 /* We need to have actual storage for pointer types. */
4587 vtn_fail_if(res_val
->type
->type
== NULL
,
4588 "Invalid pointer result type for OpSelect");
4591 vtn_fail("Result type of OpSelect must be a scalar, composite, or pointer");
4594 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4595 struct vtn_ssa_value
*ssa
= vtn_nir_select(b
,
4596 vtn_ssa_value(b
, w
[3]), vtn_ssa_value(b
, w
[4]), vtn_ssa_value(b
, w
[5]));
4598 vtn_push_ssa(b
, w
[2], res_type
, ssa
);
4602 vtn_handle_ptr(struct vtn_builder
*b
, SpvOp opcode
,
4603 const uint32_t *w
, unsigned count
)
4605 struct vtn_type
*type1
= vtn_untyped_value(b
, w
[3])->type
;
4606 struct vtn_type
*type2
= vtn_untyped_value(b
, w
[4])->type
;
4607 vtn_fail_if(type1
->base_type
!= vtn_base_type_pointer
||
4608 type2
->base_type
!= vtn_base_type_pointer
,
4609 "%s operands must have pointer types",
4610 spirv_op_to_string(opcode
));
4611 vtn_fail_if(type1
->storage_class
!= type2
->storage_class
,
4612 "%s operands must have the same storage class",
4613 spirv_op_to_string(opcode
));
4615 struct vtn_type
*vtn_type
=
4616 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4617 const struct glsl_type
*type
= vtn_type
->type
;
4619 nir_address_format addr_format
= vtn_mode_to_address_format(
4620 b
, vtn_storage_class_to_mode(b
, type1
->storage_class
, NULL
, NULL
));
4625 case SpvOpPtrDiff
: {
4626 /* OpPtrDiff returns the difference in number of elements (not byte offset). */
4627 unsigned elem_size
, elem_align
;
4628 glsl_get_natural_size_align_bytes(type1
->deref
->type
,
4629 &elem_size
, &elem_align
);
4631 def
= nir_build_addr_isub(&b
->nb
,
4632 vtn_ssa_value(b
, w
[3])->def
,
4633 vtn_ssa_value(b
, w
[4])->def
,
4635 def
= nir_idiv(&b
->nb
, def
, nir_imm_intN_t(&b
->nb
, elem_size
, def
->bit_size
));
4636 def
= nir_i2i(&b
->nb
, def
, glsl_get_bit_size(type
));
4641 case SpvOpPtrNotEqual
: {
4642 def
= nir_build_addr_ieq(&b
->nb
,
4643 vtn_ssa_value(b
, w
[3])->def
,
4644 vtn_ssa_value(b
, w
[4])->def
,
4646 if (opcode
== SpvOpPtrNotEqual
)
4647 def
= nir_inot(&b
->nb
, def
);
4652 unreachable("Invalid ptr operation");
4655 struct vtn_ssa_value
*ssa_value
= vtn_create_ssa_value(b
, type
);
4656 ssa_value
->def
= def
;
4657 vtn_push_ssa(b
, w
[2], vtn_type
, ssa_value
);
4661 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4662 const uint32_t *w
, unsigned count
)
4668 case SpvOpLoopMerge
:
4669 case SpvOpSelectionMerge
:
4670 /* This is handled by cfg pre-pass and walk_blocks */
4674 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
4675 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4680 vtn_handle_extension(b
, opcode
, w
, count
);
4686 case SpvOpCopyMemory
:
4687 case SpvOpCopyMemorySized
:
4688 case SpvOpAccessChain
:
4689 case SpvOpPtrAccessChain
:
4690 case SpvOpInBoundsAccessChain
:
4691 case SpvOpInBoundsPtrAccessChain
:
4692 case SpvOpArrayLength
:
4693 case SpvOpConvertPtrToU
:
4694 case SpvOpConvertUToPtr
:
4695 vtn_handle_variables(b
, opcode
, w
, count
);
4698 case SpvOpFunctionCall
:
4699 vtn_handle_function_call(b
, opcode
, w
, count
);
4702 case SpvOpSampledImage
:
4704 case SpvOpImageSampleImplicitLod
:
4705 case SpvOpImageSampleExplicitLod
:
4706 case SpvOpImageSampleDrefImplicitLod
:
4707 case SpvOpImageSampleDrefExplicitLod
:
4708 case SpvOpImageSampleProjImplicitLod
:
4709 case SpvOpImageSampleProjExplicitLod
:
4710 case SpvOpImageSampleProjDrefImplicitLod
:
4711 case SpvOpImageSampleProjDrefExplicitLod
:
4712 case SpvOpImageFetch
:
4713 case SpvOpImageGather
:
4714 case SpvOpImageDrefGather
:
4715 case SpvOpImageQuerySizeLod
:
4716 case SpvOpImageQueryLod
:
4717 case SpvOpImageQueryLevels
:
4718 case SpvOpImageQuerySamples
:
4719 vtn_handle_texture(b
, opcode
, w
, count
);
4722 case SpvOpImageRead
:
4723 case SpvOpImageWrite
:
4724 case SpvOpImageTexelPointer
:
4725 vtn_handle_image(b
, opcode
, w
, count
);
4728 case SpvOpImageQuerySize
: {
4729 struct vtn_pointer
*image
=
4730 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
4731 if (glsl_type_is_image(image
->type
->type
)) {
4732 vtn_handle_image(b
, opcode
, w
, count
);
4734 vtn_assert(glsl_type_is_sampler(image
->type
->type
));
4735 vtn_handle_texture(b
, opcode
, w
, count
);
4740 case SpvOpFragmentMaskFetchAMD
:
4741 case SpvOpFragmentFetchAMD
:
4742 vtn_handle_texture(b
, opcode
, w
, count
);
4745 case SpvOpAtomicLoad
:
4746 case SpvOpAtomicExchange
:
4747 case SpvOpAtomicCompareExchange
:
4748 case SpvOpAtomicCompareExchangeWeak
:
4749 case SpvOpAtomicIIncrement
:
4750 case SpvOpAtomicIDecrement
:
4751 case SpvOpAtomicIAdd
:
4752 case SpvOpAtomicISub
:
4753 case SpvOpAtomicSMin
:
4754 case SpvOpAtomicUMin
:
4755 case SpvOpAtomicSMax
:
4756 case SpvOpAtomicUMax
:
4757 case SpvOpAtomicAnd
:
4759 case SpvOpAtomicXor
: {
4760 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
4761 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4762 vtn_handle_image(b
, opcode
, w
, count
);
4764 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4765 vtn_handle_atomics(b
, opcode
, w
, count
);
4770 case SpvOpAtomicStore
: {
4771 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
4772 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4773 vtn_handle_image(b
, opcode
, w
, count
);
4775 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4776 vtn_handle_atomics(b
, opcode
, w
, count
);
4782 vtn_handle_select(b
, opcode
, w
, count
);
4790 case SpvOpConvertFToU
:
4791 case SpvOpConvertFToS
:
4792 case SpvOpConvertSToF
:
4793 case SpvOpConvertUToF
:
4797 case SpvOpQuantizeToF16
:
4798 case SpvOpPtrCastToGeneric
:
4799 case SpvOpGenericCastToPtr
:
4804 case SpvOpSignBitSet
:
4805 case SpvOpLessOrGreater
:
4807 case SpvOpUnordered
:
4822 case SpvOpVectorTimesScalar
:
4824 case SpvOpIAddCarry
:
4825 case SpvOpISubBorrow
:
4826 case SpvOpUMulExtended
:
4827 case SpvOpSMulExtended
:
4828 case SpvOpShiftRightLogical
:
4829 case SpvOpShiftRightArithmetic
:
4830 case SpvOpShiftLeftLogical
:
4831 case SpvOpLogicalEqual
:
4832 case SpvOpLogicalNotEqual
:
4833 case SpvOpLogicalOr
:
4834 case SpvOpLogicalAnd
:
4835 case SpvOpLogicalNot
:
4836 case SpvOpBitwiseOr
:
4837 case SpvOpBitwiseXor
:
4838 case SpvOpBitwiseAnd
:
4840 case SpvOpFOrdEqual
:
4841 case SpvOpFUnordEqual
:
4842 case SpvOpINotEqual
:
4843 case SpvOpFOrdNotEqual
:
4844 case SpvOpFUnordNotEqual
:
4845 case SpvOpULessThan
:
4846 case SpvOpSLessThan
:
4847 case SpvOpFOrdLessThan
:
4848 case SpvOpFUnordLessThan
:
4849 case SpvOpUGreaterThan
:
4850 case SpvOpSGreaterThan
:
4851 case SpvOpFOrdGreaterThan
:
4852 case SpvOpFUnordGreaterThan
:
4853 case SpvOpULessThanEqual
:
4854 case SpvOpSLessThanEqual
:
4855 case SpvOpFOrdLessThanEqual
:
4856 case SpvOpFUnordLessThanEqual
:
4857 case SpvOpUGreaterThanEqual
:
4858 case SpvOpSGreaterThanEqual
:
4859 case SpvOpFOrdGreaterThanEqual
:
4860 case SpvOpFUnordGreaterThanEqual
:
4866 case SpvOpFwidthFine
:
4867 case SpvOpDPdxCoarse
:
4868 case SpvOpDPdyCoarse
:
4869 case SpvOpFwidthCoarse
:
4870 case SpvOpBitFieldInsert
:
4871 case SpvOpBitFieldSExtract
:
4872 case SpvOpBitFieldUExtract
:
4873 case SpvOpBitReverse
:
4875 case SpvOpTranspose
:
4876 case SpvOpOuterProduct
:
4877 case SpvOpMatrixTimesScalar
:
4878 case SpvOpVectorTimesMatrix
:
4879 case SpvOpMatrixTimesVector
:
4880 case SpvOpMatrixTimesMatrix
:
4881 case SpvOpUCountLeadingZerosINTEL
:
4882 case SpvOpUCountTrailingZerosINTEL
:
4883 case SpvOpAbsISubINTEL
:
4884 case SpvOpAbsUSubINTEL
:
4885 case SpvOpIAddSatINTEL
:
4886 case SpvOpUAddSatINTEL
:
4887 case SpvOpIAverageINTEL
:
4888 case SpvOpUAverageINTEL
:
4889 case SpvOpIAverageRoundedINTEL
:
4890 case SpvOpUAverageRoundedINTEL
:
4891 case SpvOpISubSatINTEL
:
4892 case SpvOpUSubSatINTEL
:
4893 case SpvOpIMul32x16INTEL
:
4894 case SpvOpUMul32x16INTEL
:
4895 vtn_handle_alu(b
, opcode
, w
, count
);
4899 vtn_handle_bitcast(b
, w
, count
);
4902 case SpvOpVectorExtractDynamic
:
4903 case SpvOpVectorInsertDynamic
:
4904 case SpvOpVectorShuffle
:
4905 case SpvOpCompositeConstruct
:
4906 case SpvOpCompositeExtract
:
4907 case SpvOpCompositeInsert
:
4908 case SpvOpCopyLogical
:
4909 case SpvOpCopyObject
:
4910 vtn_handle_composite(b
, opcode
, w
, count
);
4913 case SpvOpEmitVertex
:
4914 case SpvOpEndPrimitive
:
4915 case SpvOpEmitStreamVertex
:
4916 case SpvOpEndStreamPrimitive
:
4917 case SpvOpControlBarrier
:
4918 case SpvOpMemoryBarrier
:
4919 vtn_handle_barrier(b
, opcode
, w
, count
);
4922 case SpvOpGroupNonUniformElect
:
4923 case SpvOpGroupNonUniformAll
:
4924 case SpvOpGroupNonUniformAny
:
4925 case SpvOpGroupNonUniformAllEqual
:
4926 case SpvOpGroupNonUniformBroadcast
:
4927 case SpvOpGroupNonUniformBroadcastFirst
:
4928 case SpvOpGroupNonUniformBallot
:
4929 case SpvOpGroupNonUniformInverseBallot
:
4930 case SpvOpGroupNonUniformBallotBitExtract
:
4931 case SpvOpGroupNonUniformBallotBitCount
:
4932 case SpvOpGroupNonUniformBallotFindLSB
:
4933 case SpvOpGroupNonUniformBallotFindMSB
:
4934 case SpvOpGroupNonUniformShuffle
:
4935 case SpvOpGroupNonUniformShuffleXor
:
4936 case SpvOpGroupNonUniformShuffleUp
:
4937 case SpvOpGroupNonUniformShuffleDown
:
4938 case SpvOpGroupNonUniformIAdd
:
4939 case SpvOpGroupNonUniformFAdd
:
4940 case SpvOpGroupNonUniformIMul
:
4941 case SpvOpGroupNonUniformFMul
:
4942 case SpvOpGroupNonUniformSMin
:
4943 case SpvOpGroupNonUniformUMin
:
4944 case SpvOpGroupNonUniformFMin
:
4945 case SpvOpGroupNonUniformSMax
:
4946 case SpvOpGroupNonUniformUMax
:
4947 case SpvOpGroupNonUniformFMax
:
4948 case SpvOpGroupNonUniformBitwiseAnd
:
4949 case SpvOpGroupNonUniformBitwiseOr
:
4950 case SpvOpGroupNonUniformBitwiseXor
:
4951 case SpvOpGroupNonUniformLogicalAnd
:
4952 case SpvOpGroupNonUniformLogicalOr
:
4953 case SpvOpGroupNonUniformLogicalXor
:
4954 case SpvOpGroupNonUniformQuadBroadcast
:
4955 case SpvOpGroupNonUniformQuadSwap
:
4958 case SpvOpGroupBroadcast
:
4959 case SpvOpGroupIAdd
:
4960 case SpvOpGroupFAdd
:
4961 case SpvOpGroupFMin
:
4962 case SpvOpGroupUMin
:
4963 case SpvOpGroupSMin
:
4964 case SpvOpGroupFMax
:
4965 case SpvOpGroupUMax
:
4966 case SpvOpGroupSMax
:
4967 case SpvOpSubgroupBallotKHR
:
4968 case SpvOpSubgroupFirstInvocationKHR
:
4969 case SpvOpSubgroupReadInvocationKHR
:
4970 case SpvOpSubgroupAllKHR
:
4971 case SpvOpSubgroupAnyKHR
:
4972 case SpvOpSubgroupAllEqualKHR
:
4973 case SpvOpGroupIAddNonUniformAMD
:
4974 case SpvOpGroupFAddNonUniformAMD
:
4975 case SpvOpGroupFMinNonUniformAMD
:
4976 case SpvOpGroupUMinNonUniformAMD
:
4977 case SpvOpGroupSMinNonUniformAMD
:
4978 case SpvOpGroupFMaxNonUniformAMD
:
4979 case SpvOpGroupUMaxNonUniformAMD
:
4980 case SpvOpGroupSMaxNonUniformAMD
:
4981 vtn_handle_subgroup(b
, opcode
, w
, count
);
4986 case SpvOpPtrNotEqual
:
4987 vtn_handle_ptr(b
, opcode
, w
, count
);
4990 case SpvOpBeginInvocationInterlockEXT
:
4991 vtn_emit_barrier(b
, nir_intrinsic_begin_invocation_interlock
);
4994 case SpvOpEndInvocationInterlockEXT
:
4995 vtn_emit_barrier(b
, nir_intrinsic_end_invocation_interlock
);
4998 case SpvOpDemoteToHelperInvocationEXT
: {
4999 nir_intrinsic_instr
*intrin
=
5000 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_demote
);
5001 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5005 case SpvOpIsHelperInvocationEXT
: {
5006 nir_intrinsic_instr
*intrin
=
5007 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_is_helper_invocation
);
5008 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 1, 1, NULL
);
5009 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5011 struct vtn_type
*res_type
=
5012 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
5013 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, res_type
->type
);
5014 val
->def
= &intrin
->dest
.ssa
;
5016 vtn_push_ssa(b
, w
[2], res_type
, val
);
5020 case SpvOpReadClockKHR
: {
5021 SpvScope scope
= vtn_constant_uint(b
, w
[3]);
5022 nir_scope nir_scope
;
5025 case SpvScopeDevice
:
5026 nir_scope
= NIR_SCOPE_DEVICE
;
5028 case SpvScopeSubgroup
:
5029 nir_scope
= NIR_SCOPE_SUBGROUP
;
5032 vtn_fail("invalid read clock scope");
5035 /* Operation supports two result types: uvec2 and uint64_t. The NIR
5036 * intrinsic gives uvec2, so pack the result for the other case.
5038 nir_intrinsic_instr
*intrin
=
5039 nir_intrinsic_instr_create(b
->nb
.shader
, nir_intrinsic_shader_clock
);
5040 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 2, 32, NULL
);
5041 nir_intrinsic_set_memory_scope(intrin
, nir_scope
);
5042 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5044 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
5045 const struct glsl_type
*dest_type
= type
->type
;
5046 nir_ssa_def
*result
;
5048 if (glsl_type_is_vector(dest_type
)) {
5049 assert(dest_type
== glsl_vector_type(GLSL_TYPE_UINT
, 2));
5050 result
= &intrin
->dest
.ssa
;
5052 assert(glsl_type_is_scalar(dest_type
));
5053 assert(glsl_get_base_type(dest_type
) == GLSL_TYPE_UINT64
);
5054 result
= nir_pack_64_2x32(&b
->nb
, &intrin
->dest
.ssa
);
5057 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
5059 val
->ssa
= vtn_create_ssa_value(b
, dest_type
);
5060 val
->ssa
->def
= result
;
5064 case SpvOpLifetimeStart
:
5065 case SpvOpLifetimeStop
:
5069 vtn_fail_with_opcode("Unhandled opcode", opcode
);
5076 vtn_create_builder(const uint32_t *words
, size_t word_count
,
5077 gl_shader_stage stage
, const char *entry_point_name
,
5078 const struct spirv_to_nir_options
*options
)
5080 /* Initialize the vtn_builder object */
5081 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
5082 struct spirv_to_nir_options
*dup_options
=
5083 ralloc(b
, struct spirv_to_nir_options
);
5084 *dup_options
= *options
;
5087 b
->spirv_word_count
= word_count
;
5091 list_inithead(&b
->functions
);
5092 b
->entry_point_stage
= stage
;
5093 b
->entry_point_name
= entry_point_name
;
5094 b
->options
= dup_options
;
5097 * Handle the SPIR-V header (first 5 dwords).
5098 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
5100 if (word_count
<= 5)
5103 if (words
[0] != SpvMagicNumber
) {
5104 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
5107 if (words
[1] < 0x10000) {
5108 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
5112 uint16_t generator_id
= words
[2] >> 16;
5113 uint16_t generator_version
= words
[2];
5115 /* The first GLSLang version bump actually 1.5 years after #179 was fixed
5116 * but this should at least let us shut the workaround off for modern
5117 * versions of GLSLang.
5119 b
->wa_glslang_179
= (generator_id
== 8 && generator_version
== 1);
5121 /* In GLSLang commit 8297936dd6eb3, their handling of barrier() was fixed
5122 * to provide correct memory semantics on compute shader barrier()
5123 * commands. Prior to that, we need to fix them up ourselves. This
5124 * GLSLang fix caused them to bump to generator version 3.
5126 b
->wa_glslang_cs_barrier
= (generator_id
== 8 && generator_version
< 3);
5128 /* words[2] == generator magic */
5129 unsigned value_id_bound
= words
[3];
5130 if (words
[4] != 0) {
5131 vtn_err("words[4] was %u, want 0", words
[4]);
5135 b
->value_id_bound
= value_id_bound
;
5136 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
5144 static nir_function
*
5145 vtn_emit_kernel_entry_point_wrapper(struct vtn_builder
*b
,
5146 nir_function
*entry_point
)
5148 vtn_assert(entry_point
== b
->entry_point
->func
->impl
->function
);
5149 vtn_fail_if(!entry_point
->name
, "entry points are required to have a name");
5150 const char *func_name
=
5151 ralloc_asprintf(b
->shader
, "__wrapped_%s", entry_point
->name
);
5153 /* we shouldn't have any inputs yet */
5154 vtn_assert(!entry_point
->shader
->num_inputs
);
5155 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
);
5157 nir_function
*main_entry_point
= nir_function_create(b
->shader
, func_name
);
5158 main_entry_point
->impl
= nir_function_impl_create(main_entry_point
);
5159 nir_builder_init(&b
->nb
, main_entry_point
->impl
);
5160 b
->nb
.cursor
= nir_after_cf_list(&main_entry_point
->impl
->body
);
5161 b
->func_param_idx
= 0;
5163 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, entry_point
);
5165 for (unsigned i
= 0; i
< entry_point
->num_params
; ++i
) {
5166 struct vtn_type
*param_type
= b
->entry_point
->func
->type
->params
[i
];
5168 /* consider all pointers to function memory to be parameters passed
5171 bool is_by_val
= param_type
->base_type
== vtn_base_type_pointer
&&
5172 param_type
->storage_class
== SpvStorageClassFunction
;
5174 /* input variable */
5175 nir_variable
*in_var
= rzalloc(b
->nb
.shader
, nir_variable
);
5176 in_var
->data
.mode
= nir_var_shader_in
;
5177 in_var
->data
.read_only
= true;
5178 in_var
->data
.location
= i
;
5181 in_var
->type
= param_type
->deref
->type
;
5183 in_var
->type
= param_type
->type
;
5185 nir_shader_add_variable(b
->nb
.shader
, in_var
);
5186 b
->nb
.shader
->num_inputs
++;
5188 /* we have to copy the entire variable into function memory */
5190 nir_variable
*copy_var
=
5191 nir_local_variable_create(main_entry_point
->impl
, in_var
->type
,
5193 nir_copy_var(&b
->nb
, copy_var
, in_var
);
5195 nir_src_for_ssa(&nir_build_deref_var(&b
->nb
, copy_var
)->dest
.ssa
);
5197 call
->params
[i
] = nir_src_for_ssa(nir_load_var(&b
->nb
, in_var
));
5201 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
5203 return main_entry_point
;
5207 spirv_to_nir(const uint32_t *words
, size_t word_count
,
5208 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
5209 gl_shader_stage stage
, const char *entry_point_name
,
5210 const struct spirv_to_nir_options
*options
,
5211 const nir_shader_compiler_options
*nir_options
)
5214 const uint32_t *word_end
= words
+ word_count
;
5216 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
5217 stage
, entry_point_name
,
5223 /* See also _vtn_fail() */
5224 if (setjmp(b
->fail_jump
)) {
5229 /* Skip the SPIR-V header, handled at vtn_create_builder */
5232 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
5234 /* Handle all the preamble instructions */
5235 words
= vtn_foreach_instruction(b
, words
, word_end
,
5236 vtn_handle_preamble_instruction
);
5238 if (b
->entry_point
== NULL
) {
5239 vtn_fail("Entry point not found");
5244 /* Set shader info defaults */
5245 if (stage
== MESA_SHADER_GEOMETRY
)
5246 b
->shader
->info
.gs
.invocations
= 1;
5248 /* Parse rounding mode execution modes. This has to happen earlier than
5249 * other changes in the execution modes since they can affect, for example,
5250 * the result of the floating point constants.
5252 vtn_foreach_execution_mode(b
, b
->entry_point
,
5253 vtn_handle_rounding_mode_in_execution_mode
, NULL
);
5255 b
->specializations
= spec
;
5256 b
->num_specializations
= num_spec
;
5258 /* Handle all variable, type, and constant instructions */
5259 words
= vtn_foreach_instruction(b
, words
, word_end
,
5260 vtn_handle_variable_or_type_instruction
);
5262 /* Parse execution modes */
5263 vtn_foreach_execution_mode(b
, b
->entry_point
,
5264 vtn_handle_execution_mode
, NULL
);
5266 if (b
->workgroup_size_builtin
) {
5267 vtn_assert(b
->workgroup_size_builtin
->type
->type
==
5268 glsl_vector_type(GLSL_TYPE_UINT
, 3));
5270 nir_const_value
*const_size
=
5271 b
->workgroup_size_builtin
->constant
->values
;
5273 b
->shader
->info
.cs
.local_size
[0] = const_size
[0].u32
;
5274 b
->shader
->info
.cs
.local_size
[1] = const_size
[1].u32
;
5275 b
->shader
->info
.cs
.local_size
[2] = const_size
[2].u32
;
5278 /* Set types on all vtn_values */
5279 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
5281 vtn_build_cfg(b
, words
, word_end
);
5283 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5284 b
->entry_point
->func
->referenced
= true;
5289 vtn_foreach_cf_node(node
, &b
->functions
) {
5290 struct vtn_function
*func
= vtn_cf_node_as_function(node
);
5291 if (func
->referenced
&& !func
->emitted
) {
5292 b
->const_table
= _mesa_pointer_hash_table_create(b
);
5294 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
5300 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5301 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
5302 vtn_assert(entry_point
);
5304 /* post process entry_points with input params */
5305 if (entry_point
->num_params
&& b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
5306 entry_point
= vtn_emit_kernel_entry_point_wrapper(b
, entry_point
);
5308 entry_point
->is_entrypoint
= true;
5310 /* When multiple shader stages exist in the same SPIR-V module, we
5311 * generate input and output variables for every stage, in the same
5312 * NIR program. These dead variables can be invalid NIR. For example,
5313 * TCS outputs must be per-vertex arrays (or decorated 'patch'), while
5314 * VS output variables wouldn't be.
5316 * To ensure we have valid NIR, we eliminate any dead inputs and outputs
5317 * right away. In order to do so, we must lower any constant initializers
5318 * on outputs so nir_remove_dead_variables sees that they're written to.
5320 nir_lower_variable_initializers(b
->shader
, nir_var_shader_out
);
5321 nir_remove_dead_variables(b
->shader
,
5322 nir_var_shader_in
| nir_var_shader_out
);
5324 /* We sometimes generate bogus derefs that, while never used, give the
5325 * validator a bit of heartburn. Run dead code to get rid of them.
5327 nir_opt_dce(b
->shader
);
5329 /* Unparent the shader from the vtn_builder before we delete the builder */
5330 ralloc_steal(NULL
, b
->shader
);
5332 nir_shader
*shader
= b
->shader
;