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 case GLSL_TYPE_INTERFACE
: {
261 unsigned elems
= glsl_get_length(val
->type
);
262 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
263 for (unsigned i
= 0; i
< elems
; i
++) {
264 const struct glsl_type
*elem_type
=
265 glsl_get_struct_field(val
->type
, i
);
266 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
273 vtn_fail("bad constant type");
279 struct vtn_ssa_value
*
280 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
282 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
283 switch (val
->value_type
) {
284 case vtn_value_type_undef
:
285 return vtn_undef_ssa_value(b
, val
->type
->type
);
287 case vtn_value_type_constant
:
288 return vtn_const_ssa_value(b
, val
->constant
, val
->type
->type
);
290 case vtn_value_type_ssa
:
293 case vtn_value_type_pointer
:
294 vtn_assert(val
->pointer
->ptr_type
&& val
->pointer
->ptr_type
->type
);
295 struct vtn_ssa_value
*ssa
=
296 vtn_create_ssa_value(b
, val
->pointer
->ptr_type
->type
);
297 ssa
->def
= vtn_pointer_to_ssa(b
, val
->pointer
);
301 vtn_fail("Invalid type for an SSA value");
306 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
307 unsigned word_count
, unsigned *words_used
)
309 char *dup
= ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
311 /* Ammount of space taken by the string (including the null) */
312 unsigned len
= strlen(dup
) + 1;
313 *words_used
= DIV_ROUND_UP(len
, sizeof(*words
));
319 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
320 const uint32_t *end
, vtn_instruction_handler handler
)
326 const uint32_t *w
= start
;
328 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
329 unsigned count
= w
[0] >> SpvWordCountShift
;
330 vtn_assert(count
>= 1 && w
+ count
<= end
);
332 b
->spirv_offset
= (uint8_t *)w
- (uint8_t *)b
->spirv
;
336 break; /* Do nothing */
339 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
351 if (!handler(b
, opcode
, w
, count
))
369 vtn_handle_non_semantic_instruction(struct vtn_builder
*b
, SpvOp ext_opcode
,
370 const uint32_t *w
, unsigned count
)
377 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
378 const uint32_t *w
, unsigned count
)
380 const char *ext
= (const char *)&w
[2];
382 case SpvOpExtInstImport
: {
383 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
384 if (strcmp(ext
, "GLSL.std.450") == 0) {
385 val
->ext_handler
= vtn_handle_glsl450_instruction
;
386 } else if ((strcmp(ext
, "SPV_AMD_gcn_shader") == 0)
387 && (b
->options
&& b
->options
->caps
.amd_gcn_shader
)) {
388 val
->ext_handler
= vtn_handle_amd_gcn_shader_instruction
;
389 } else if ((strcmp(ext
, "SPV_AMD_shader_ballot") == 0)
390 && (b
->options
&& b
->options
->caps
.amd_shader_ballot
)) {
391 val
->ext_handler
= vtn_handle_amd_shader_ballot_instruction
;
392 } else if ((strcmp(ext
, "SPV_AMD_shader_trinary_minmax") == 0)
393 && (b
->options
&& b
->options
->caps
.amd_trinary_minmax
)) {
394 val
->ext_handler
= vtn_handle_amd_shader_trinary_minmax_instruction
;
395 } else if ((strcmp(ext
, "SPV_AMD_shader_explicit_vertex_parameter") == 0)
396 && (b
->options
&& b
->options
->caps
.amd_shader_explicit_vertex_parameter
)) {
397 val
->ext_handler
= vtn_handle_amd_shader_explicit_vertex_parameter_instruction
;
398 } else if (strcmp(ext
, "OpenCL.std") == 0) {
399 val
->ext_handler
= vtn_handle_opencl_instruction
;
400 } else if (strstr(ext
, "NonSemantic.") == ext
) {
401 val
->ext_handler
= vtn_handle_non_semantic_instruction
;
403 vtn_fail("Unsupported extension: %s", ext
);
409 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
410 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
416 vtn_fail_with_opcode("Unhandled opcode", opcode
);
421 _foreach_decoration_helper(struct vtn_builder
*b
,
422 struct vtn_value
*base_value
,
424 struct vtn_value
*value
,
425 vtn_decoration_foreach_cb cb
, void *data
)
427 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
429 if (dec
->scope
== VTN_DEC_DECORATION
) {
430 member
= parent_member
;
431 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
432 vtn_fail_if(value
->value_type
!= vtn_value_type_type
||
433 value
->type
->base_type
!= vtn_base_type_struct
,
434 "OpMemberDecorate and OpGroupMemberDecorate are only "
435 "allowed on OpTypeStruct");
436 /* This means we haven't recursed yet */
437 assert(value
== base_value
);
439 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
441 vtn_fail_if(member
>= base_value
->type
->length
,
442 "OpMemberDecorate specifies member %d but the "
443 "OpTypeStruct has only %u members",
444 member
, base_value
->type
->length
);
446 /* Not a decoration */
447 assert(dec
->scope
== VTN_DEC_EXECUTION_MODE
);
452 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
453 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
456 cb(b
, base_value
, member
, dec
, data
);
461 /** Iterates (recursively if needed) over all of the decorations on a value
463 * This function iterates over all of the decorations applied to a given
464 * value. If it encounters a decoration group, it recurses into the group
465 * and iterates over all of those decorations as well.
468 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
469 vtn_decoration_foreach_cb cb
, void *data
)
471 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
475 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
476 vtn_execution_mode_foreach_cb cb
, void *data
)
478 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
479 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
482 assert(dec
->group
== NULL
);
483 cb(b
, value
, dec
, data
);
488 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
489 const uint32_t *w
, unsigned count
)
491 const uint32_t *w_end
= w
+ count
;
492 const uint32_t target
= w
[1];
496 case SpvOpDecorationGroup
:
497 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
501 case SpvOpDecorateId
:
502 case SpvOpMemberDecorate
:
503 case SpvOpDecorateString
:
504 case SpvOpMemberDecorateString
:
505 case SpvOpExecutionMode
:
506 case SpvOpExecutionModeId
: {
507 struct vtn_value
*val
= vtn_untyped_value(b
, target
);
509 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
512 case SpvOpDecorateId
:
513 case SpvOpDecorateString
:
514 dec
->scope
= VTN_DEC_DECORATION
;
516 case SpvOpMemberDecorate
:
517 case SpvOpMemberDecorateString
:
518 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
519 vtn_fail_if(dec
->scope
< VTN_DEC_STRUCT_MEMBER0
, /* overflow */
520 "Member argument of OpMemberDecorate too large");
522 case SpvOpExecutionMode
:
523 case SpvOpExecutionModeId
:
524 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
527 unreachable("Invalid decoration opcode");
529 dec
->decoration
= *(w
++);
532 /* Link into the list */
533 dec
->next
= val
->decoration
;
534 val
->decoration
= dec
;
538 case SpvOpGroupMemberDecorate
:
539 case SpvOpGroupDecorate
: {
540 struct vtn_value
*group
=
541 vtn_value(b
, target
, vtn_value_type_decoration_group
);
543 for (; w
< w_end
; w
++) {
544 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
545 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
548 if (opcode
== SpvOpGroupDecorate
) {
549 dec
->scope
= VTN_DEC_DECORATION
;
551 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
552 vtn_fail_if(dec
->scope
< 0, /* Check for overflow */
553 "Member argument of OpGroupMemberDecorate too large");
556 /* Link into the list */
557 dec
->next
= val
->decoration
;
558 val
->decoration
= dec
;
564 unreachable("Unhandled opcode");
568 struct member_decoration_ctx
{
570 struct glsl_struct_field
*fields
;
571 struct vtn_type
*type
;
575 * Returns true if the given type contains a struct decorated Block or
579 vtn_type_contains_block(struct vtn_builder
*b
, struct vtn_type
*type
)
581 switch (type
->base_type
) {
582 case vtn_base_type_array
:
583 return vtn_type_contains_block(b
, type
->array_element
);
584 case vtn_base_type_struct
:
585 if (type
->block
|| type
->buffer_block
)
587 for (unsigned i
= 0; i
< type
->length
; i
++) {
588 if (vtn_type_contains_block(b
, type
->members
[i
]))
597 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
598 * OpStore, or OpCopyMemory between them without breaking anything.
599 * Technically, the SPIR-V rules require the exact same type ID but this lets
600 * us internally be a bit looser.
603 vtn_types_compatible(struct vtn_builder
*b
,
604 struct vtn_type
*t1
, struct vtn_type
*t2
)
606 if (t1
->id
== t2
->id
)
609 if (t1
->base_type
!= t2
->base_type
)
612 switch (t1
->base_type
) {
613 case vtn_base_type_void
:
614 case vtn_base_type_scalar
:
615 case vtn_base_type_vector
:
616 case vtn_base_type_matrix
:
617 case vtn_base_type_image
:
618 case vtn_base_type_sampler
:
619 case vtn_base_type_sampled_image
:
620 return t1
->type
== t2
->type
;
622 case vtn_base_type_array
:
623 return t1
->length
== t2
->length
&&
624 vtn_types_compatible(b
, t1
->array_element
, t2
->array_element
);
626 case vtn_base_type_pointer
:
627 return vtn_types_compatible(b
, t1
->deref
, t2
->deref
);
629 case vtn_base_type_struct
:
630 if (t1
->length
!= t2
->length
)
633 for (unsigned i
= 0; i
< t1
->length
; i
++) {
634 if (!vtn_types_compatible(b
, t1
->members
[i
], t2
->members
[i
]))
639 case vtn_base_type_function
:
640 /* This case shouldn't get hit since you can't copy around function
641 * types. Just require them to be identical.
646 vtn_fail("Invalid base type");
650 vtn_type_without_array(struct vtn_type
*type
)
652 while (type
->base_type
== vtn_base_type_array
)
653 type
= type
->array_element
;
657 /* does a shallow copy of a vtn_type */
659 static struct vtn_type
*
660 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
662 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
665 switch (src
->base_type
) {
666 case vtn_base_type_void
:
667 case vtn_base_type_scalar
:
668 case vtn_base_type_vector
:
669 case vtn_base_type_matrix
:
670 case vtn_base_type_array
:
671 case vtn_base_type_pointer
:
672 case vtn_base_type_image
:
673 case vtn_base_type_sampler
:
674 case vtn_base_type_sampled_image
:
675 /* Nothing more to do */
678 case vtn_base_type_struct
:
679 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
680 memcpy(dest
->members
, src
->members
,
681 src
->length
* sizeof(src
->members
[0]));
683 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
684 memcpy(dest
->offsets
, src
->offsets
,
685 src
->length
* sizeof(src
->offsets
[0]));
688 case vtn_base_type_function
:
689 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
690 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
697 static struct vtn_type
*
698 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
700 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
701 type
= type
->members
[member
];
703 /* We may have an array of matrices.... Oh, joy! */
704 while (glsl_type_is_array(type
->type
)) {
705 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
706 type
= type
->array_element
;
709 vtn_assert(glsl_type_is_matrix(type
->type
));
715 vtn_handle_access_qualifier(struct vtn_builder
*b
, struct vtn_type
*type
,
716 int member
, enum gl_access_qualifier access
)
718 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
719 type
= type
->members
[member
];
721 type
->access
|= access
;
725 array_stride_decoration_cb(struct vtn_builder
*b
,
726 struct vtn_value
*val
, int member
,
727 const struct vtn_decoration
*dec
, void *void_ctx
)
729 struct vtn_type
*type
= val
->type
;
731 if (dec
->decoration
== SpvDecorationArrayStride
) {
732 if (vtn_type_contains_block(b
, type
)) {
733 vtn_warn("The ArrayStride decoration cannot be applied to an array "
734 "type which contains a structure type decorated Block "
736 /* Ignore the decoration */
738 vtn_fail_if(dec
->operands
[0] == 0, "ArrayStride must be non-zero");
739 type
->stride
= dec
->operands
[0];
745 struct_member_decoration_cb(struct vtn_builder
*b
,
746 UNUSED
struct vtn_value
*val
, int member
,
747 const struct vtn_decoration
*dec
, void *void_ctx
)
749 struct member_decoration_ctx
*ctx
= void_ctx
;
754 assert(member
< ctx
->num_fields
);
756 switch (dec
->decoration
) {
757 case SpvDecorationRelaxedPrecision
:
758 case SpvDecorationUniform
:
759 case SpvDecorationUniformId
:
760 break; /* FIXME: Do nothing with this for now. */
761 case SpvDecorationNonWritable
:
762 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_WRITEABLE
);
764 case SpvDecorationNonReadable
:
765 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_READABLE
);
767 case SpvDecorationVolatile
:
768 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_VOLATILE
);
770 case SpvDecorationCoherent
:
771 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_COHERENT
);
773 case SpvDecorationNoPerspective
:
774 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
776 case SpvDecorationFlat
:
777 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
779 case SpvDecorationExplicitInterpAMD
:
780 ctx
->fields
[member
].interpolation
= INTERP_MODE_EXPLICIT
;
782 case SpvDecorationCentroid
:
783 ctx
->fields
[member
].centroid
= true;
785 case SpvDecorationSample
:
786 ctx
->fields
[member
].sample
= true;
788 case SpvDecorationStream
:
789 /* This is handled later by var_decoration_cb in vtn_variables.c */
791 case SpvDecorationLocation
:
792 ctx
->fields
[member
].location
= dec
->operands
[0];
794 case SpvDecorationComponent
:
795 break; /* FIXME: What should we do with these? */
796 case SpvDecorationBuiltIn
:
797 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
798 ctx
->type
->members
[member
]->is_builtin
= true;
799 ctx
->type
->members
[member
]->builtin
= dec
->operands
[0];
800 ctx
->type
->builtin_block
= true;
802 case SpvDecorationOffset
:
803 ctx
->type
->offsets
[member
] = dec
->operands
[0];
804 ctx
->fields
[member
].offset
= dec
->operands
[0];
806 case SpvDecorationMatrixStride
:
807 /* Handled as a second pass */
809 case SpvDecorationColMajor
:
810 break; /* Nothing to do here. Column-major is the default. */
811 case SpvDecorationRowMajor
:
812 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
815 case SpvDecorationPatch
:
818 case SpvDecorationSpecId
:
819 case SpvDecorationBlock
:
820 case SpvDecorationBufferBlock
:
821 case SpvDecorationArrayStride
:
822 case SpvDecorationGLSLShared
:
823 case SpvDecorationGLSLPacked
:
824 case SpvDecorationInvariant
:
825 case SpvDecorationRestrict
:
826 case SpvDecorationAliased
:
827 case SpvDecorationConstant
:
828 case SpvDecorationIndex
:
829 case SpvDecorationBinding
:
830 case SpvDecorationDescriptorSet
:
831 case SpvDecorationLinkageAttributes
:
832 case SpvDecorationNoContraction
:
833 case SpvDecorationInputAttachmentIndex
:
834 vtn_warn("Decoration not allowed on struct members: %s",
835 spirv_decoration_to_string(dec
->decoration
));
838 case SpvDecorationXfbBuffer
:
839 case SpvDecorationXfbStride
:
840 /* This is handled later by var_decoration_cb in vtn_variables.c */
843 case SpvDecorationCPacked
:
844 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
845 vtn_warn("Decoration only allowed for CL-style kernels: %s",
846 spirv_decoration_to_string(dec
->decoration
));
848 ctx
->type
->packed
= true;
851 case SpvDecorationSaturatedConversion
:
852 case SpvDecorationFuncParamAttr
:
853 case SpvDecorationFPRoundingMode
:
854 case SpvDecorationFPFastMathMode
:
855 case SpvDecorationAlignment
:
856 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
857 vtn_warn("Decoration only allowed for CL-style kernels: %s",
858 spirv_decoration_to_string(dec
->decoration
));
862 case SpvDecorationUserSemantic
:
863 case SpvDecorationUserTypeGOOGLE
:
864 /* User semantic decorations can safely be ignored by the driver. */
868 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
872 /** Chases the array type all the way down to the tail and rewrites the
873 * glsl_types to be based off the tail's glsl_type.
876 vtn_array_type_rewrite_glsl_type(struct vtn_type
*type
)
878 if (type
->base_type
!= vtn_base_type_array
)
881 vtn_array_type_rewrite_glsl_type(type
->array_element
);
883 type
->type
= glsl_array_type(type
->array_element
->type
,
884 type
->length
, type
->stride
);
887 /* Matrix strides are handled as a separate pass because we need to know
888 * whether the matrix is row-major or not first.
891 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
892 UNUSED
struct vtn_value
*val
, int member
,
893 const struct vtn_decoration
*dec
,
896 if (dec
->decoration
!= SpvDecorationMatrixStride
)
899 vtn_fail_if(member
< 0,
900 "The MatrixStride decoration is only allowed on members "
902 vtn_fail_if(dec
->operands
[0] == 0, "MatrixStride must be non-zero");
904 struct member_decoration_ctx
*ctx
= void_ctx
;
906 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
907 if (mat_type
->row_major
) {
908 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
909 mat_type
->stride
= mat_type
->array_element
->stride
;
910 mat_type
->array_element
->stride
= dec
->operands
[0];
912 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
913 dec
->operands
[0], true);
914 mat_type
->array_element
->type
= glsl_get_column_type(mat_type
->type
);
916 vtn_assert(mat_type
->array_element
->stride
> 0);
917 mat_type
->stride
= dec
->operands
[0];
919 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
920 dec
->operands
[0], false);
923 /* Now that we've replaced the glsl_type with a properly strided matrix
924 * type, rewrite the member type so that it's an array of the proper kind
927 vtn_array_type_rewrite_glsl_type(ctx
->type
->members
[member
]);
928 ctx
->fields
[member
].type
= ctx
->type
->members
[member
]->type
;
932 struct_block_decoration_cb(struct vtn_builder
*b
,
933 struct vtn_value
*val
, int member
,
934 const struct vtn_decoration
*dec
, void *ctx
)
939 struct vtn_type
*type
= val
->type
;
940 if (dec
->decoration
== SpvDecorationBlock
)
942 else if (dec
->decoration
== SpvDecorationBufferBlock
)
943 type
->buffer_block
= true;
947 type_decoration_cb(struct vtn_builder
*b
,
948 struct vtn_value
*val
, int member
,
949 const struct vtn_decoration
*dec
, UNUSED
void *ctx
)
951 struct vtn_type
*type
= val
->type
;
954 /* This should have been handled by OpTypeStruct */
955 assert(val
->type
->base_type
== vtn_base_type_struct
);
956 assert(member
>= 0 && member
< val
->type
->length
);
960 switch (dec
->decoration
) {
961 case SpvDecorationArrayStride
:
962 vtn_assert(type
->base_type
== vtn_base_type_array
||
963 type
->base_type
== vtn_base_type_pointer
);
965 case SpvDecorationBlock
:
966 vtn_assert(type
->base_type
== vtn_base_type_struct
);
967 vtn_assert(type
->block
);
969 case SpvDecorationBufferBlock
:
970 vtn_assert(type
->base_type
== vtn_base_type_struct
);
971 vtn_assert(type
->buffer_block
);
973 case SpvDecorationGLSLShared
:
974 case SpvDecorationGLSLPacked
:
975 /* Ignore these, since we get explicit offsets anyways */
978 case SpvDecorationRowMajor
:
979 case SpvDecorationColMajor
:
980 case SpvDecorationMatrixStride
:
981 case SpvDecorationBuiltIn
:
982 case SpvDecorationNoPerspective
:
983 case SpvDecorationFlat
:
984 case SpvDecorationPatch
:
985 case SpvDecorationCentroid
:
986 case SpvDecorationSample
:
987 case SpvDecorationExplicitInterpAMD
:
988 case SpvDecorationVolatile
:
989 case SpvDecorationCoherent
:
990 case SpvDecorationNonWritable
:
991 case SpvDecorationNonReadable
:
992 case SpvDecorationUniform
:
993 case SpvDecorationUniformId
:
994 case SpvDecorationLocation
:
995 case SpvDecorationComponent
:
996 case SpvDecorationOffset
:
997 case SpvDecorationXfbBuffer
:
998 case SpvDecorationXfbStride
:
999 case SpvDecorationUserSemantic
:
1000 vtn_warn("Decoration only allowed for struct members: %s",
1001 spirv_decoration_to_string(dec
->decoration
));
1004 case SpvDecorationStream
:
1005 /* We don't need to do anything here, as stream is filled up when
1006 * aplying the decoration to a variable, just check that if it is not a
1007 * struct member, it should be a struct.
1009 vtn_assert(type
->base_type
== vtn_base_type_struct
);
1012 case SpvDecorationRelaxedPrecision
:
1013 case SpvDecorationSpecId
:
1014 case SpvDecorationInvariant
:
1015 case SpvDecorationRestrict
:
1016 case SpvDecorationAliased
:
1017 case SpvDecorationConstant
:
1018 case SpvDecorationIndex
:
1019 case SpvDecorationBinding
:
1020 case SpvDecorationDescriptorSet
:
1021 case SpvDecorationLinkageAttributes
:
1022 case SpvDecorationNoContraction
:
1023 case SpvDecorationInputAttachmentIndex
:
1024 vtn_warn("Decoration not allowed on types: %s",
1025 spirv_decoration_to_string(dec
->decoration
));
1028 case SpvDecorationCPacked
:
1029 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
1030 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1031 spirv_decoration_to_string(dec
->decoration
));
1033 type
->packed
= true;
1036 case SpvDecorationSaturatedConversion
:
1037 case SpvDecorationFuncParamAttr
:
1038 case SpvDecorationFPRoundingMode
:
1039 case SpvDecorationFPFastMathMode
:
1040 case SpvDecorationAlignment
:
1041 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1042 spirv_decoration_to_string(dec
->decoration
));
1045 case SpvDecorationUserTypeGOOGLE
:
1046 /* User semantic decorations can safely be ignored by the driver. */
1050 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1055 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
1058 case SpvImageFormatUnknown
: return PIPE_FORMAT_NONE
;
1059 case SpvImageFormatRgba32f
: return PIPE_FORMAT_R32G32B32A32_FLOAT
;
1060 case SpvImageFormatRgba16f
: return PIPE_FORMAT_R16G16B16A16_FLOAT
;
1061 case SpvImageFormatR32f
: return PIPE_FORMAT_R32_FLOAT
;
1062 case SpvImageFormatRgba8
: return PIPE_FORMAT_R8G8B8A8_UNORM
;
1063 case SpvImageFormatRgba8Snorm
: return PIPE_FORMAT_R8G8B8A8_SNORM
;
1064 case SpvImageFormatRg32f
: return PIPE_FORMAT_R32G32_FLOAT
;
1065 case SpvImageFormatRg16f
: return PIPE_FORMAT_R16G16_FLOAT
;
1066 case SpvImageFormatR11fG11fB10f
: return PIPE_FORMAT_R11G11B10_FLOAT
;
1067 case SpvImageFormatR16f
: return PIPE_FORMAT_R16_FLOAT
;
1068 case SpvImageFormatRgba16
: return PIPE_FORMAT_R16G16B16A16_UNORM
;
1069 case SpvImageFormatRgb10A2
: return PIPE_FORMAT_R10G10B10A2_UNORM
;
1070 case SpvImageFormatRg16
: return PIPE_FORMAT_R16G16_UNORM
;
1071 case SpvImageFormatRg8
: return PIPE_FORMAT_R8G8_UNORM
;
1072 case SpvImageFormatR16
: return PIPE_FORMAT_R16_UNORM
;
1073 case SpvImageFormatR8
: return PIPE_FORMAT_R8_UNORM
;
1074 case SpvImageFormatRgba16Snorm
: return PIPE_FORMAT_R16G16B16A16_SNORM
;
1075 case SpvImageFormatRg16Snorm
: return PIPE_FORMAT_R16G16_SNORM
;
1076 case SpvImageFormatRg8Snorm
: return PIPE_FORMAT_R8G8_SNORM
;
1077 case SpvImageFormatR16Snorm
: return PIPE_FORMAT_R16_SNORM
;
1078 case SpvImageFormatR8Snorm
: return PIPE_FORMAT_R8_SNORM
;
1079 case SpvImageFormatRgba32i
: return PIPE_FORMAT_R32G32B32A32_SINT
;
1080 case SpvImageFormatRgba16i
: return PIPE_FORMAT_R16G16B16A16_SINT
;
1081 case SpvImageFormatRgba8i
: return PIPE_FORMAT_R8G8B8A8_SINT
;
1082 case SpvImageFormatR32i
: return PIPE_FORMAT_R32_SINT
;
1083 case SpvImageFormatRg32i
: return PIPE_FORMAT_R32G32_SINT
;
1084 case SpvImageFormatRg16i
: return PIPE_FORMAT_R16G16_SINT
;
1085 case SpvImageFormatRg8i
: return PIPE_FORMAT_R8G8_SINT
;
1086 case SpvImageFormatR16i
: return PIPE_FORMAT_R16_SINT
;
1087 case SpvImageFormatR8i
: return PIPE_FORMAT_R8_SINT
;
1088 case SpvImageFormatRgba32ui
: return PIPE_FORMAT_R32G32B32A32_UINT
;
1089 case SpvImageFormatRgba16ui
: return PIPE_FORMAT_R16G16B16A16_UINT
;
1090 case SpvImageFormatRgba8ui
: return PIPE_FORMAT_R8G8B8A8_UINT
;
1091 case SpvImageFormatR32ui
: return PIPE_FORMAT_R32_UINT
;
1092 case SpvImageFormatRgb10a2ui
: return PIPE_FORMAT_R10G10B10A2_UINT
;
1093 case SpvImageFormatRg32ui
: return PIPE_FORMAT_R32G32_UINT
;
1094 case SpvImageFormatRg16ui
: return PIPE_FORMAT_R16G16_UINT
;
1095 case SpvImageFormatRg8ui
: return PIPE_FORMAT_R8G8_UINT
;
1096 case SpvImageFormatR16ui
: return PIPE_FORMAT_R16_UINT
;
1097 case SpvImageFormatR8ui
: return PIPE_FORMAT_R8_UINT
;
1099 vtn_fail("Invalid image format: %s (%u)",
1100 spirv_imageformat_to_string(format
), format
);
1105 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
1106 const uint32_t *w
, unsigned count
)
1108 struct vtn_value
*val
= NULL
;
1110 /* In order to properly handle forward declarations, we have to defer
1111 * allocation for pointer types.
1113 if (opcode
!= SpvOpTypePointer
&& opcode
!= SpvOpTypeForwardPointer
) {
1114 val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1115 vtn_fail_if(val
->type
!= NULL
,
1116 "Only pointers can have forward declarations");
1117 val
->type
= rzalloc(b
, struct vtn_type
);
1118 val
->type
->id
= w
[1];
1123 val
->type
->base_type
= vtn_base_type_void
;
1124 val
->type
->type
= glsl_void_type();
1127 val
->type
->base_type
= vtn_base_type_scalar
;
1128 val
->type
->type
= glsl_bool_type();
1129 val
->type
->length
= 1;
1131 case SpvOpTypeInt
: {
1132 int bit_size
= w
[2];
1133 const bool signedness
= w
[3];
1134 val
->type
->base_type
= vtn_base_type_scalar
;
1137 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1140 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1143 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1146 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1149 vtn_fail("Invalid int bit size: %u", bit_size
);
1151 val
->type
->length
= 1;
1155 case SpvOpTypeFloat
: {
1156 int bit_size
= w
[2];
1157 val
->type
->base_type
= vtn_base_type_scalar
;
1160 val
->type
->type
= glsl_float16_t_type();
1163 val
->type
->type
= glsl_float_type();
1166 val
->type
->type
= glsl_double_type();
1169 vtn_fail("Invalid float bit size: %u", bit_size
);
1171 val
->type
->length
= 1;
1175 case SpvOpTypeVector
: {
1176 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1177 unsigned elems
= w
[3];
1179 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1180 "Base type for OpTypeVector must be a scalar");
1181 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1182 "Invalid component count for OpTypeVector");
1184 val
->type
->base_type
= vtn_base_type_vector
;
1185 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1186 val
->type
->length
= elems
;
1187 val
->type
->stride
= glsl_type_is_boolean(val
->type
->type
)
1188 ? 4 : glsl_get_bit_size(base
->type
) / 8;
1189 val
->type
->array_element
= base
;
1193 case SpvOpTypeMatrix
: {
1194 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1195 unsigned columns
= w
[3];
1197 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1198 "Base type for OpTypeMatrix must be a vector");
1199 vtn_fail_if(columns
< 2 || columns
> 4,
1200 "Invalid column count for OpTypeMatrix");
1202 val
->type
->base_type
= vtn_base_type_matrix
;
1203 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1204 glsl_get_vector_elements(base
->type
),
1206 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1207 "Unsupported base type for OpTypeMatrix");
1208 assert(!glsl_type_is_error(val
->type
->type
));
1209 val
->type
->length
= columns
;
1210 val
->type
->array_element
= base
;
1211 val
->type
->row_major
= false;
1212 val
->type
->stride
= 0;
1216 case SpvOpTypeRuntimeArray
:
1217 case SpvOpTypeArray
: {
1218 struct vtn_type
*array_element
=
1219 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1221 if (opcode
== SpvOpTypeRuntimeArray
) {
1222 /* A length of 0 is used to denote unsized arrays */
1223 val
->type
->length
= 0;
1225 val
->type
->length
= vtn_constant_uint(b
, w
[3]);
1228 val
->type
->base_type
= vtn_base_type_array
;
1229 val
->type
->array_element
= array_element
;
1230 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
1231 val
->type
->stride
= glsl_get_cl_size(array_element
->type
);
1233 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1234 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
,
1239 case SpvOpTypeStruct
: {
1240 unsigned num_fields
= count
- 2;
1241 val
->type
->base_type
= vtn_base_type_struct
;
1242 val
->type
->length
= num_fields
;
1243 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1244 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1245 val
->type
->packed
= false;
1247 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1248 for (unsigned i
= 0; i
< num_fields
; i
++) {
1249 val
->type
->members
[i
] =
1250 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
1251 fields
[i
] = (struct glsl_struct_field
) {
1252 .type
= val
->type
->members
[i
]->type
,
1253 .name
= ralloc_asprintf(b
, "field%d", i
),
1259 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1260 unsigned offset
= 0;
1261 for (unsigned i
= 0; i
< num_fields
; i
++) {
1262 offset
= align(offset
, glsl_get_cl_alignment(fields
[i
].type
));
1263 fields
[i
].offset
= offset
;
1264 offset
+= glsl_get_cl_size(fields
[i
].type
);
1268 struct member_decoration_ctx ctx
= {
1269 .num_fields
= num_fields
,
1274 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1275 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1277 vtn_foreach_decoration(b
, val
, struct_block_decoration_cb
, NULL
);
1279 const char *name
= val
->name
;
1281 if (val
->type
->block
|| val
->type
->buffer_block
) {
1282 /* Packing will be ignored since types coming from SPIR-V are
1283 * explicitly laid out.
1285 val
->type
->type
= glsl_interface_type(fields
, num_fields
,
1286 /* packing */ 0, false,
1287 name
? name
: "block");
1289 val
->type
->type
= glsl_struct_type(fields
, num_fields
,
1290 name
? name
: "struct", false);
1295 case SpvOpTypeFunction
: {
1296 val
->type
->base_type
= vtn_base_type_function
;
1297 val
->type
->type
= NULL
;
1299 val
->type
->return_type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1301 const unsigned num_params
= count
- 3;
1302 val
->type
->length
= num_params
;
1303 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1304 for (unsigned i
= 0; i
< count
- 3; i
++) {
1305 val
->type
->params
[i
] =
1306 vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
;
1311 case SpvOpTypePointer
:
1312 case SpvOpTypeForwardPointer
: {
1313 /* We can't blindly push the value because it might be a forward
1316 val
= vtn_untyped_value(b
, w
[1]);
1318 SpvStorageClass storage_class
= w
[2];
1320 if (val
->value_type
== vtn_value_type_invalid
) {
1321 val
->value_type
= vtn_value_type_type
;
1322 val
->type
= rzalloc(b
, struct vtn_type
);
1323 val
->type
->id
= w
[1];
1324 val
->type
->base_type
= vtn_base_type_pointer
;
1325 val
->type
->storage_class
= storage_class
;
1327 /* These can actually be stored to nir_variables and used as SSA
1328 * values so they need a real glsl_type.
1330 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1331 b
, storage_class
, NULL
, NULL
);
1332 val
->type
->type
= nir_address_format_to_glsl_type(
1333 vtn_mode_to_address_format(b
, mode
));
1335 vtn_fail_if(val
->type
->storage_class
!= storage_class
,
1336 "The storage classes of an OpTypePointer and any "
1337 "OpTypeForwardPointers that provide forward "
1338 "declarations of it must match.");
1341 if (opcode
== SpvOpTypePointer
) {
1342 vtn_fail_if(val
->type
->deref
!= NULL
,
1343 "While OpTypeForwardPointer can be used to provide a "
1344 "forward declaration of a pointer, OpTypePointer can "
1345 "only be used once for a given id.");
1347 val
->type
->deref
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
1349 /* Only certain storage classes use ArrayStride. The others (in
1350 * particular Workgroup) are expected to be laid out by the driver.
1352 switch (storage_class
) {
1353 case SpvStorageClassUniform
:
1354 case SpvStorageClassPushConstant
:
1355 case SpvStorageClassStorageBuffer
:
1356 case SpvStorageClassPhysicalStorageBuffer
:
1357 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1360 /* Nothing to do. */
1364 if (b
->physical_ptrs
) {
1365 switch (storage_class
) {
1366 case SpvStorageClassFunction
:
1367 case SpvStorageClassWorkgroup
:
1368 case SpvStorageClassCrossWorkgroup
:
1369 case SpvStorageClassUniformConstant
:
1370 val
->type
->stride
= align(glsl_get_cl_size(val
->type
->deref
->type
),
1371 glsl_get_cl_alignment(val
->type
->deref
->type
));
1381 case SpvOpTypeImage
: {
1382 val
->type
->base_type
= vtn_base_type_image
;
1384 const struct vtn_type
*sampled_type
=
1385 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1387 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1388 glsl_get_bit_size(sampled_type
->type
) != 32,
1389 "Sampled type of OpTypeImage must be a 32-bit scalar");
1391 enum glsl_sampler_dim dim
;
1392 switch ((SpvDim
)w
[3]) {
1393 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1394 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1395 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1396 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1397 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1398 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1399 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1401 vtn_fail("Invalid SPIR-V image dimensionality: %s (%u)",
1402 spirv_dim_to_string((SpvDim
)w
[3]), w
[3]);
1405 /* w[4]: as per Vulkan spec "Validation Rules within a Module",
1406 * The “Depth” operand of OpTypeImage is ignored.
1408 bool is_array
= w
[5];
1409 bool multisampled
= w
[6];
1410 unsigned sampled
= w
[7];
1411 SpvImageFormat format
= w
[8];
1414 val
->type
->access_qualifier
= w
[9];
1416 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1419 if (dim
== GLSL_SAMPLER_DIM_2D
)
1420 dim
= GLSL_SAMPLER_DIM_MS
;
1421 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1422 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1424 vtn_fail("Unsupported multisampled image type");
1427 val
->type
->image_format
= translate_image_format(b
, format
);
1429 enum glsl_base_type sampled_base_type
=
1430 glsl_get_base_type(sampled_type
->type
);
1432 val
->type
->sampled
= true;
1433 val
->type
->type
= glsl_sampler_type(dim
, false, is_array
,
1435 } else if (sampled
== 2) {
1436 val
->type
->sampled
= false;
1437 val
->type
->type
= glsl_image_type(dim
, is_array
, sampled_base_type
);
1439 vtn_fail("We need to know if the image will be sampled");
1444 case SpvOpTypeSampledImage
:
1445 val
->type
->base_type
= vtn_base_type_sampled_image
;
1446 val
->type
->image
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1447 val
->type
->type
= val
->type
->image
->type
;
1450 case SpvOpTypeSampler
:
1451 /* The actual sampler type here doesn't really matter. It gets
1452 * thrown away the moment you combine it with an image. What really
1453 * matters is that it's a sampler type as opposed to an integer type
1454 * so the backend knows what to do.
1456 val
->type
->base_type
= vtn_base_type_sampler
;
1457 val
->type
->type
= glsl_bare_sampler_type();
1460 case SpvOpTypeOpaque
:
1461 case SpvOpTypeEvent
:
1462 case SpvOpTypeDeviceEvent
:
1463 case SpvOpTypeReserveId
:
1464 case SpvOpTypeQueue
:
1467 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1470 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1472 if (val
->type
->base_type
== vtn_base_type_struct
&&
1473 (val
->type
->block
|| val
->type
->buffer_block
)) {
1474 for (unsigned i
= 0; i
< val
->type
->length
; i
++) {
1475 vtn_fail_if(vtn_type_contains_block(b
, val
->type
->members
[i
]),
1476 "Block and BufferBlock decorations cannot decorate a "
1477 "structure type that is nested at any level inside "
1478 "another structure type decorated with Block or "
1484 static nir_constant
*
1485 vtn_null_constant(struct vtn_builder
*b
, struct vtn_type
*type
)
1487 nir_constant
*c
= rzalloc(b
, nir_constant
);
1489 switch (type
->base_type
) {
1490 case vtn_base_type_scalar
:
1491 case vtn_base_type_vector
:
1492 /* Nothing to do here. It's already initialized to zero */
1495 case vtn_base_type_pointer
: {
1496 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1497 b
, type
->storage_class
, type
->deref
, NULL
);
1498 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
1500 const nir_const_value
*null_value
= nir_address_format_null_value(addr_format
);
1501 memcpy(c
->values
, null_value
,
1502 sizeof(nir_const_value
) * nir_address_format_num_components(addr_format
));
1506 case vtn_base_type_void
:
1507 case vtn_base_type_image
:
1508 case vtn_base_type_sampler
:
1509 case vtn_base_type_sampled_image
:
1510 case vtn_base_type_function
:
1511 /* For those we have to return something but it doesn't matter what. */
1514 case vtn_base_type_matrix
:
1515 case vtn_base_type_array
:
1516 vtn_assert(type
->length
> 0);
1517 c
->num_elements
= type
->length
;
1518 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1520 c
->elements
[0] = vtn_null_constant(b
, type
->array_element
);
1521 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1522 c
->elements
[i
] = c
->elements
[0];
1525 case vtn_base_type_struct
:
1526 c
->num_elements
= type
->length
;
1527 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1528 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
1529 c
->elements
[i
] = vtn_null_constant(b
, type
->members
[i
]);
1533 vtn_fail("Invalid type for null constant");
1540 spec_constant_decoration_cb(struct vtn_builder
*b
, UNUSED
struct vtn_value
*val
,
1541 ASSERTED
int member
,
1542 const struct vtn_decoration
*dec
, void *data
)
1544 vtn_assert(member
== -1);
1545 if (dec
->decoration
!= SpvDecorationSpecId
)
1548 nir_const_value
*value
= data
;
1549 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1550 if (b
->specializations
[i
].id
== dec
->operands
[0]) {
1551 *value
= b
->specializations
[i
].value
;
1558 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1559 struct vtn_value
*val
,
1560 ASSERTED
int member
,
1561 const struct vtn_decoration
*dec
,
1564 vtn_assert(member
== -1);
1565 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1566 dec
->operands
[0] != SpvBuiltInWorkgroupSize
)
1569 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1570 b
->workgroup_size_builtin
= val
;
1574 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1575 const uint32_t *w
, unsigned count
)
1577 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1578 val
->constant
= rzalloc(b
, nir_constant
);
1580 case SpvOpConstantTrue
:
1581 case SpvOpConstantFalse
:
1582 case SpvOpSpecConstantTrue
:
1583 case SpvOpSpecConstantFalse
: {
1584 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1585 "Result type of %s must be OpTypeBool",
1586 spirv_op_to_string(opcode
));
1588 bool bval
= (opcode
== SpvOpConstantTrue
||
1589 opcode
== SpvOpSpecConstantTrue
);
1591 nir_const_value u32val
= nir_const_value_for_uint(bval
, 32);
1593 if (opcode
== SpvOpSpecConstantTrue
||
1594 opcode
== SpvOpSpecConstantFalse
)
1595 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &u32val
);
1597 val
->constant
->values
[0].b
= u32val
.u32
!= 0;
1602 case SpvOpSpecConstant
: {
1603 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1604 "Result type of %s must be a scalar",
1605 spirv_op_to_string(opcode
));
1606 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1609 val
->constant
->values
[0].u64
= vtn_u64_literal(&w
[3]);
1612 val
->constant
->values
[0].u32
= w
[3];
1615 val
->constant
->values
[0].u16
= w
[3];
1618 val
->constant
->values
[0].u8
= w
[3];
1621 vtn_fail("Unsupported SpvOpConstant bit size: %u", bit_size
);
1624 if (opcode
== SpvOpSpecConstant
)
1625 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
,
1626 &val
->constant
->values
[0]);
1630 case SpvOpSpecConstantComposite
:
1631 case SpvOpConstantComposite
: {
1632 unsigned elem_count
= count
- 3;
1633 vtn_fail_if(elem_count
!= val
->type
->length
,
1634 "%s has %u constituents, expected %u",
1635 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1637 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1638 for (unsigned i
= 0; i
< elem_count
; i
++) {
1639 struct vtn_value
*val
= vtn_untyped_value(b
, w
[i
+ 3]);
1641 if (val
->value_type
== vtn_value_type_constant
) {
1642 elems
[i
] = val
->constant
;
1644 vtn_fail_if(val
->value_type
!= vtn_value_type_undef
,
1645 "only constants or undefs allowed for "
1646 "SpvOpConstantComposite");
1647 /* to make it easier, just insert a NULL constant for now */
1648 elems
[i
] = vtn_null_constant(b
, val
->type
);
1652 switch (val
->type
->base_type
) {
1653 case vtn_base_type_vector
: {
1654 assert(glsl_type_is_vector(val
->type
->type
));
1655 for (unsigned i
= 0; i
< elem_count
; i
++)
1656 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1660 case vtn_base_type_matrix
:
1661 case vtn_base_type_struct
:
1662 case vtn_base_type_array
:
1663 ralloc_steal(val
->constant
, elems
);
1664 val
->constant
->num_elements
= elem_count
;
1665 val
->constant
->elements
= elems
;
1669 vtn_fail("Result type of %s must be a composite type",
1670 spirv_op_to_string(opcode
));
1675 case SpvOpSpecConstantOp
: {
1676 nir_const_value u32op
= nir_const_value_for_uint(w
[3], 32);
1677 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &u32op
);
1678 SpvOp opcode
= u32op
.u32
;
1680 case SpvOpVectorShuffle
: {
1681 struct vtn_value
*v0
= &b
->values
[w
[4]];
1682 struct vtn_value
*v1
= &b
->values
[w
[5]];
1684 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1685 v0
->value_type
== vtn_value_type_undef
);
1686 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1687 v1
->value_type
== vtn_value_type_undef
);
1689 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1690 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1692 vtn_assert(len0
+ len1
< 16);
1694 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1695 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1696 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1698 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1699 (void)bit_size0
; (void)bit_size1
;
1701 nir_const_value undef
= { .u64
= 0xdeadbeefdeadbeef };
1702 nir_const_value combined
[NIR_MAX_VEC_COMPONENTS
* 2];
1704 if (v0
->value_type
== vtn_value_type_constant
) {
1705 for (unsigned i
= 0; i
< len0
; i
++)
1706 combined
[i
] = v0
->constant
->values
[i
];
1708 if (v1
->value_type
== vtn_value_type_constant
) {
1709 for (unsigned i
= 0; i
< len1
; i
++)
1710 combined
[len0
+ i
] = v1
->constant
->values
[i
];
1713 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1714 uint32_t comp
= w
[i
+ 6];
1715 if (comp
== (uint32_t)-1) {
1716 /* If component is not used, set the value to a known constant
1717 * to detect if it is wrongly used.
1719 val
->constant
->values
[j
] = undef
;
1721 vtn_fail_if(comp
>= len0
+ len1
,
1722 "All Component literals must either be FFFFFFFF "
1723 "or in [0, N - 1] (inclusive).");
1724 val
->constant
->values
[j
] = combined
[comp
];
1730 case SpvOpCompositeExtract
:
1731 case SpvOpCompositeInsert
: {
1732 struct vtn_value
*comp
;
1733 unsigned deref_start
;
1734 struct nir_constant
**c
;
1735 if (opcode
== SpvOpCompositeExtract
) {
1736 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1738 c
= &comp
->constant
;
1740 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1742 val
->constant
= nir_constant_clone(comp
->constant
,
1748 const struct vtn_type
*type
= comp
->type
;
1749 for (unsigned i
= deref_start
; i
< count
; i
++) {
1750 vtn_fail_if(w
[i
] > type
->length
,
1751 "%uth index of %s is %u but the type has only "
1752 "%u elements", i
- deref_start
,
1753 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1755 switch (type
->base_type
) {
1756 case vtn_base_type_vector
:
1758 type
= type
->array_element
;
1761 case vtn_base_type_matrix
:
1762 case vtn_base_type_array
:
1763 c
= &(*c
)->elements
[w
[i
]];
1764 type
= type
->array_element
;
1767 case vtn_base_type_struct
:
1768 c
= &(*c
)->elements
[w
[i
]];
1769 type
= type
->members
[w
[i
]];
1773 vtn_fail("%s must only index into composite types",
1774 spirv_op_to_string(opcode
));
1778 if (opcode
== SpvOpCompositeExtract
) {
1782 unsigned num_components
= type
->length
;
1783 for (unsigned i
= 0; i
< num_components
; i
++)
1784 val
->constant
->values
[i
] = (*c
)->values
[elem
+ i
];
1787 struct vtn_value
*insert
=
1788 vtn_value(b
, w
[4], vtn_value_type_constant
);
1789 vtn_assert(insert
->type
== type
);
1791 *c
= insert
->constant
;
1793 unsigned num_components
= type
->length
;
1794 for (unsigned i
= 0; i
< num_components
; i
++)
1795 (*c
)->values
[elem
+ i
] = insert
->constant
->values
[i
];
1803 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
1804 nir_alu_type src_alu_type
= dst_alu_type
;
1805 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
1808 vtn_assert(count
<= 7);
1814 /* We have a source in a conversion */
1816 nir_get_nir_type_for_glsl_type(
1817 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1818 /* We use the bitsize of the conversion source to evaluate the opcode later */
1819 bit_size
= glsl_get_bit_size(
1820 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1823 bit_size
= glsl_get_bit_size(val
->type
->type
);
1826 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
1827 nir_alu_type_get_type_size(src_alu_type
),
1828 nir_alu_type_get_type_size(dst_alu_type
));
1829 nir_const_value src
[3][NIR_MAX_VEC_COMPONENTS
];
1831 for (unsigned i
= 0; i
< count
- 4; i
++) {
1832 struct vtn_value
*src_val
=
1833 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
);
1835 /* If this is an unsized source, pull the bit size from the
1836 * source; otherwise, we'll use the bit size from the destination.
1838 if (!nir_alu_type_get_type_size(nir_op_infos
[op
].input_types
[i
]))
1839 bit_size
= glsl_get_bit_size(src_val
->type
->type
);
1841 unsigned src_comps
= nir_op_infos
[op
].input_sizes
[i
] ?
1842 nir_op_infos
[op
].input_sizes
[i
] :
1845 unsigned j
= swap
? 1 - i
: i
;
1846 for (unsigned c
= 0; c
< src_comps
; c
++)
1847 src
[j
][c
] = src_val
->constant
->values
[c
];
1850 /* fix up fixed size sources */
1857 for (unsigned i
= 0; i
< num_components
; ++i
) {
1859 case 64: src
[1][i
].u32
= src
[1][i
].u64
; break;
1860 case 16: src
[1][i
].u32
= src
[1][i
].u16
; break;
1861 case 8: src
[1][i
].u32
= src
[1][i
].u8
; break;
1870 nir_const_value
*srcs
[3] = {
1871 src
[0], src
[1], src
[2],
1873 nir_eval_const_opcode(op
, val
->constant
->values
,
1874 num_components
, bit_size
, srcs
,
1875 b
->shader
->info
.float_controls_execution_mode
);
1882 case SpvOpConstantNull
:
1883 val
->constant
= vtn_null_constant(b
, val
->type
);
1886 case SpvOpConstantSampler
:
1887 vtn_fail("OpConstantSampler requires Kernel Capability");
1891 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1894 /* Now that we have the value, update the workgroup size if needed */
1895 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1898 SpvMemorySemanticsMask
1899 vtn_storage_class_to_memory_semantics(SpvStorageClass sc
)
1902 case SpvStorageClassStorageBuffer
:
1903 case SpvStorageClassPhysicalStorageBuffer
:
1904 return SpvMemorySemanticsUniformMemoryMask
;
1905 case SpvStorageClassWorkgroup
:
1906 return SpvMemorySemanticsWorkgroupMemoryMask
;
1908 return SpvMemorySemanticsMaskNone
;
1913 vtn_split_barrier_semantics(struct vtn_builder
*b
,
1914 SpvMemorySemanticsMask semantics
,
1915 SpvMemorySemanticsMask
*before
,
1916 SpvMemorySemanticsMask
*after
)
1918 /* For memory semantics embedded in operations, we split them into up to
1919 * two barriers, to be added before and after the operation. This is less
1920 * strict than if we propagated until the final backend stage, but still
1921 * result in correct execution.
1923 * A further improvement could be pipe this information (and use!) into the
1924 * next compiler layers, at the expense of making the handling of barriers
1928 *before
= SpvMemorySemanticsMaskNone
;
1929 *after
= SpvMemorySemanticsMaskNone
;
1931 SpvMemorySemanticsMask order_semantics
=
1932 semantics
& (SpvMemorySemanticsAcquireMask
|
1933 SpvMemorySemanticsReleaseMask
|
1934 SpvMemorySemanticsAcquireReleaseMask
|
1935 SpvMemorySemanticsSequentiallyConsistentMask
);
1937 if (util_bitcount(order_semantics
) > 1) {
1938 /* Old GLSLang versions incorrectly set all the ordering bits. This was
1939 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
1940 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
1942 vtn_warn("Multiple memory ordering semantics specified, "
1943 "assuming AcquireRelease.");
1944 order_semantics
= SpvMemorySemanticsAcquireReleaseMask
;
1947 const SpvMemorySemanticsMask av_vis_semantics
=
1948 semantics
& (SpvMemorySemanticsMakeAvailableMask
|
1949 SpvMemorySemanticsMakeVisibleMask
);
1951 const SpvMemorySemanticsMask storage_semantics
=
1952 semantics
& (SpvMemorySemanticsUniformMemoryMask
|
1953 SpvMemorySemanticsSubgroupMemoryMask
|
1954 SpvMemorySemanticsWorkgroupMemoryMask
|
1955 SpvMemorySemanticsCrossWorkgroupMemoryMask
|
1956 SpvMemorySemanticsAtomicCounterMemoryMask
|
1957 SpvMemorySemanticsImageMemoryMask
|
1958 SpvMemorySemanticsOutputMemoryMask
);
1960 const SpvMemorySemanticsMask other_semantics
=
1961 semantics
& ~(order_semantics
| av_vis_semantics
| storage_semantics
);
1963 if (other_semantics
)
1964 vtn_warn("Ignoring unhandled memory semantics: %u\n", other_semantics
);
1966 /* SequentiallyConsistent is treated as AcquireRelease. */
1968 /* The RELEASE barrier happens BEFORE the operation, and it is usually
1969 * associated with a Store. All the write operations with a matching
1970 * semantics will not be reordered after the Store.
1972 if (order_semantics
& (SpvMemorySemanticsReleaseMask
|
1973 SpvMemorySemanticsAcquireReleaseMask
|
1974 SpvMemorySemanticsSequentiallyConsistentMask
)) {
1975 *before
|= SpvMemorySemanticsReleaseMask
| storage_semantics
;
1978 /* The ACQUIRE barrier happens AFTER the operation, and it is usually
1979 * associated with a Load. All the operations with a matching semantics
1980 * will not be reordered before the Load.
1982 if (order_semantics
& (SpvMemorySemanticsAcquireMask
|
1983 SpvMemorySemanticsAcquireReleaseMask
|
1984 SpvMemorySemanticsSequentiallyConsistentMask
)) {
1985 *after
|= SpvMemorySemanticsAcquireMask
| storage_semantics
;
1988 if (av_vis_semantics
& SpvMemorySemanticsMakeVisibleMask
)
1989 *before
|= SpvMemorySemanticsMakeVisibleMask
| storage_semantics
;
1991 if (av_vis_semantics
& SpvMemorySemanticsMakeAvailableMask
)
1992 *after
|= SpvMemorySemanticsMakeAvailableMask
| storage_semantics
;
1995 static nir_memory_semantics
1996 vtn_mem_semantics_to_nir_mem_semantics(struct vtn_builder
*b
,
1997 SpvMemorySemanticsMask semantics
)
1999 nir_memory_semantics nir_semantics
= 0;
2001 SpvMemorySemanticsMask order_semantics
=
2002 semantics
& (SpvMemorySemanticsAcquireMask
|
2003 SpvMemorySemanticsReleaseMask
|
2004 SpvMemorySemanticsAcquireReleaseMask
|
2005 SpvMemorySemanticsSequentiallyConsistentMask
);
2007 if (util_bitcount(order_semantics
) > 1) {
2008 /* Old GLSLang versions incorrectly set all the ordering bits. This was
2009 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
2010 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
2012 vtn_warn("Multiple memory ordering semantics bits specified, "
2013 "assuming AcquireRelease.");
2014 order_semantics
= SpvMemorySemanticsAcquireReleaseMask
;
2017 switch (order_semantics
) {
2019 /* Not an ordering barrier. */
2022 case SpvMemorySemanticsAcquireMask
:
2023 nir_semantics
= NIR_MEMORY_ACQUIRE
;
2026 case SpvMemorySemanticsReleaseMask
:
2027 nir_semantics
= NIR_MEMORY_RELEASE
;
2030 case SpvMemorySemanticsSequentiallyConsistentMask
:
2031 /* Fall through. Treated as AcquireRelease in Vulkan. */
2032 case SpvMemorySemanticsAcquireReleaseMask
:
2033 nir_semantics
= NIR_MEMORY_ACQUIRE
| NIR_MEMORY_RELEASE
;
2037 unreachable("Invalid memory order semantics");
2040 if (semantics
& SpvMemorySemanticsMakeAvailableMask
) {
2041 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2042 "To use MakeAvailable memory semantics the VulkanMemoryModel "
2043 "capability must be declared.");
2044 nir_semantics
|= NIR_MEMORY_MAKE_AVAILABLE
;
2047 if (semantics
& SpvMemorySemanticsMakeVisibleMask
) {
2048 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2049 "To use MakeVisible memory semantics the VulkanMemoryModel "
2050 "capability must be declared.");
2051 nir_semantics
|= NIR_MEMORY_MAKE_VISIBLE
;
2054 return nir_semantics
;
2057 static nir_variable_mode
2058 vtn_mem_sematics_to_nir_var_modes(struct vtn_builder
*b
,
2059 SpvMemorySemanticsMask semantics
)
2061 /* Vulkan Environment for SPIR-V says "SubgroupMemory, CrossWorkgroupMemory,
2062 * and AtomicCounterMemory are ignored".
2064 semantics
&= ~(SpvMemorySemanticsSubgroupMemoryMask
|
2065 SpvMemorySemanticsCrossWorkgroupMemoryMask
|
2066 SpvMemorySemanticsAtomicCounterMemoryMask
);
2068 /* TODO: Consider adding nir_var_mem_image mode to NIR so it can be used
2069 * for SpvMemorySemanticsImageMemoryMask.
2072 nir_variable_mode modes
= 0;
2073 if (semantics
& (SpvMemorySemanticsUniformMemoryMask
|
2074 SpvMemorySemanticsImageMemoryMask
)) {
2075 modes
|= nir_var_uniform
|
2080 if (semantics
& SpvMemorySemanticsWorkgroupMemoryMask
)
2081 modes
|= nir_var_mem_shared
;
2082 if (semantics
& SpvMemorySemanticsOutputMemoryMask
) {
2083 modes
|= nir_var_shader_out
;
2090 vtn_scope_to_nir_scope(struct vtn_builder
*b
, SpvScope scope
)
2092 nir_scope nir_scope
;
2094 case SpvScopeDevice
:
2095 vtn_fail_if(b
->options
->caps
.vk_memory_model
&&
2096 !b
->options
->caps
.vk_memory_model_device_scope
,
2097 "If the Vulkan memory model is declared and any instruction "
2098 "uses Device scope, the VulkanMemoryModelDeviceScope "
2099 "capability must be declared.");
2100 nir_scope
= NIR_SCOPE_DEVICE
;
2103 case SpvScopeQueueFamily
:
2104 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2105 "To use Queue Family scope, the VulkanMemoryModel capability "
2106 "must be declared.");
2107 nir_scope
= NIR_SCOPE_QUEUE_FAMILY
;
2110 case SpvScopeWorkgroup
:
2111 nir_scope
= NIR_SCOPE_WORKGROUP
;
2114 case SpvScopeSubgroup
:
2115 nir_scope
= NIR_SCOPE_SUBGROUP
;
2118 case SpvScopeInvocation
:
2119 nir_scope
= NIR_SCOPE_INVOCATION
;
2123 vtn_fail("Invalid memory scope");
2130 vtn_emit_scoped_control_barrier(struct vtn_builder
*b
, SpvScope exec_scope
,
2132 SpvMemorySemanticsMask semantics
)
2134 nir_memory_semantics nir_semantics
=
2135 vtn_mem_semantics_to_nir_mem_semantics(b
, semantics
);
2136 nir_variable_mode modes
= vtn_mem_sematics_to_nir_var_modes(b
, semantics
);
2137 nir_scope nir_exec_scope
= vtn_scope_to_nir_scope(b
, exec_scope
);
2139 /* Memory semantics is optional for OpControlBarrier. */
2140 nir_scope nir_mem_scope
;
2141 if (nir_semantics
== 0 || modes
== 0)
2142 nir_mem_scope
= NIR_SCOPE_NONE
;
2144 nir_mem_scope
= vtn_scope_to_nir_scope(b
, mem_scope
);
2146 nir_scoped_barrier(&b
->nb
, nir_exec_scope
, nir_mem_scope
, nir_semantics
, modes
);
2150 vtn_emit_scoped_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
2151 SpvMemorySemanticsMask semantics
)
2153 nir_variable_mode modes
= vtn_mem_sematics_to_nir_var_modes(b
, semantics
);
2154 nir_memory_semantics nir_semantics
=
2155 vtn_mem_semantics_to_nir_mem_semantics(b
, semantics
);
2157 /* No barrier to add. */
2158 if (nir_semantics
== 0 || modes
== 0)
2161 nir_scope nir_mem_scope
= vtn_scope_to_nir_scope(b
, scope
);
2162 nir_scoped_barrier(&b
->nb
, NIR_SCOPE_NONE
, nir_mem_scope
, nir_semantics
, modes
);
2165 struct vtn_ssa_value
*
2166 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
2168 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
2171 if (!glsl_type_is_vector_or_scalar(type
)) {
2172 unsigned elems
= glsl_get_length(type
);
2173 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2174 for (unsigned i
= 0; i
< elems
; i
++) {
2175 const struct glsl_type
*child_type
;
2177 switch (glsl_get_base_type(type
)) {
2179 case GLSL_TYPE_UINT
:
2180 case GLSL_TYPE_INT16
:
2181 case GLSL_TYPE_UINT16
:
2182 case GLSL_TYPE_UINT8
:
2183 case GLSL_TYPE_INT8
:
2184 case GLSL_TYPE_INT64
:
2185 case GLSL_TYPE_UINT64
:
2186 case GLSL_TYPE_BOOL
:
2187 case GLSL_TYPE_FLOAT
:
2188 case GLSL_TYPE_FLOAT16
:
2189 case GLSL_TYPE_DOUBLE
:
2190 child_type
= glsl_get_column_type(type
);
2192 case GLSL_TYPE_ARRAY
:
2193 child_type
= glsl_get_array_element(type
);
2195 case GLSL_TYPE_STRUCT
:
2196 case GLSL_TYPE_INTERFACE
:
2197 child_type
= glsl_get_struct_field(type
, i
);
2200 vtn_fail("unkown base type");
2203 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
2211 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
2214 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
2215 src
.src_type
= type
;
2220 image_operand_arg(struct vtn_builder
*b
, const uint32_t *w
, uint32_t count
,
2221 uint32_t mask_idx
, SpvImageOperandsMask op
)
2223 static const SpvImageOperandsMask ops_with_arg
=
2224 SpvImageOperandsBiasMask
|
2225 SpvImageOperandsLodMask
|
2226 SpvImageOperandsGradMask
|
2227 SpvImageOperandsConstOffsetMask
|
2228 SpvImageOperandsOffsetMask
|
2229 SpvImageOperandsConstOffsetsMask
|
2230 SpvImageOperandsSampleMask
|
2231 SpvImageOperandsMinLodMask
|
2232 SpvImageOperandsMakeTexelAvailableMask
|
2233 SpvImageOperandsMakeTexelVisibleMask
;
2235 assert(util_bitcount(op
) == 1);
2236 assert(w
[mask_idx
] & op
);
2237 assert(op
& ops_with_arg
);
2239 uint32_t idx
= util_bitcount(w
[mask_idx
] & (op
- 1) & ops_with_arg
) + 1;
2241 /* Adjust indices for operands with two arguments. */
2242 static const SpvImageOperandsMask ops_with_two_args
=
2243 SpvImageOperandsGradMask
;
2244 idx
+= util_bitcount(w
[mask_idx
] & (op
- 1) & ops_with_two_args
);
2248 vtn_fail_if(idx
+ (op
& ops_with_two_args
? 1 : 0) >= count
,
2249 "Image op claims to have %s but does not enough "
2250 "following operands", spirv_imageoperands_to_string(op
));
2256 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
2257 const uint32_t *w
, unsigned count
)
2259 if (opcode
== SpvOpSampledImage
) {
2260 struct vtn_value
*val
=
2261 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2262 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2264 /* It seems valid to use OpSampledImage with OpUndef instead of
2265 * OpTypeImage or OpTypeSampler.
2267 if (vtn_untyped_value(b
, w
[3])->value_type
== vtn_value_type_undef
) {
2268 val
->sampled_image
->image
= NULL
;
2270 val
->sampled_image
->image
=
2271 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2274 if (vtn_untyped_value(b
, w
[4])->value_type
== vtn_value_type_undef
) {
2275 val
->sampled_image
->sampler
= NULL
;
2277 val
->sampled_image
->sampler
=
2278 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
2281 } else if (opcode
== SpvOpImage
) {
2282 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
2283 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
2284 vtn_push_value_pointer(b
, w
[2], src_val
->sampled_image
->image
);
2286 vtn_assert(src_val
->value_type
== vtn_value_type_pointer
);
2287 vtn_push_value_pointer(b
, w
[2], src_val
->pointer
);
2292 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2294 struct vtn_pointer
*image
= NULL
, *sampler
= NULL
;
2295 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
2296 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
2297 image
= sampled_val
->sampled_image
->image
;
2298 sampler
= sampled_val
->sampled_image
->sampler
;
2300 vtn_assert(sampled_val
->value_type
== vtn_value_type_pointer
);
2301 image
= sampled_val
->pointer
;
2305 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
2309 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
);
2310 nir_deref_instr
*sampler_deref
=
2311 sampler
? vtn_pointer_to_deref(b
, sampler
) : NULL
;
2313 const struct glsl_type
*image_type
= sampled_val
->type
->type
;
2314 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
2315 const bool is_array
= glsl_sampler_type_is_array(image_type
);
2316 nir_alu_type dest_type
= nir_type_invalid
;
2318 /* Figure out the base texture operation */
2321 case SpvOpImageSampleImplicitLod
:
2322 case SpvOpImageSampleDrefImplicitLod
:
2323 case SpvOpImageSampleProjImplicitLod
:
2324 case SpvOpImageSampleProjDrefImplicitLod
:
2325 texop
= nir_texop_tex
;
2328 case SpvOpImageSampleExplicitLod
:
2329 case SpvOpImageSampleDrefExplicitLod
:
2330 case SpvOpImageSampleProjExplicitLod
:
2331 case SpvOpImageSampleProjDrefExplicitLod
:
2332 texop
= nir_texop_txl
;
2335 case SpvOpImageFetch
:
2336 if (sampler_dim
== GLSL_SAMPLER_DIM_MS
) {
2337 texop
= nir_texop_txf_ms
;
2339 texop
= nir_texop_txf
;
2343 case SpvOpImageGather
:
2344 case SpvOpImageDrefGather
:
2345 texop
= nir_texop_tg4
;
2348 case SpvOpImageQuerySizeLod
:
2349 case SpvOpImageQuerySize
:
2350 texop
= nir_texop_txs
;
2351 dest_type
= nir_type_int
;
2354 case SpvOpImageQueryLod
:
2355 texop
= nir_texop_lod
;
2356 dest_type
= nir_type_float
;
2359 case SpvOpImageQueryLevels
:
2360 texop
= nir_texop_query_levels
;
2361 dest_type
= nir_type_int
;
2364 case SpvOpImageQuerySamples
:
2365 texop
= nir_texop_texture_samples
;
2366 dest_type
= nir_type_int
;
2369 case SpvOpFragmentFetchAMD
:
2370 texop
= nir_texop_fragment_fetch
;
2373 case SpvOpFragmentMaskFetchAMD
:
2374 texop
= nir_texop_fragment_mask_fetch
;
2378 vtn_fail_with_opcode("Unhandled opcode", opcode
);
2381 nir_tex_src srcs
[10]; /* 10 should be enough */
2382 nir_tex_src
*p
= srcs
;
2384 p
->src
= nir_src_for_ssa(&image_deref
->dest
.ssa
);
2385 p
->src_type
= nir_tex_src_texture_deref
;
2395 vtn_fail_if(sampler
== NULL
,
2396 "%s requires an image of type OpTypeSampledImage",
2397 spirv_op_to_string(opcode
));
2398 p
->src
= nir_src_for_ssa(&sampler_deref
->dest
.ssa
);
2399 p
->src_type
= nir_tex_src_sampler_deref
;
2403 case nir_texop_txf_ms
:
2405 case nir_texop_query_levels
:
2406 case nir_texop_texture_samples
:
2407 case nir_texop_samples_identical
:
2408 case nir_texop_fragment_fetch
:
2409 case nir_texop_fragment_mask_fetch
:
2412 case nir_texop_txf_ms_fb
:
2413 vtn_fail("unexpected nir_texop_txf_ms_fb");
2415 case nir_texop_txf_ms_mcs
:
2416 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2417 case nir_texop_tex_prefetch
:
2418 vtn_fail("unexpected nir_texop_tex_prefetch");
2423 struct nir_ssa_def
*coord
;
2424 unsigned coord_components
;
2426 case SpvOpImageSampleImplicitLod
:
2427 case SpvOpImageSampleExplicitLod
:
2428 case SpvOpImageSampleDrefImplicitLod
:
2429 case SpvOpImageSampleDrefExplicitLod
:
2430 case SpvOpImageSampleProjImplicitLod
:
2431 case SpvOpImageSampleProjExplicitLod
:
2432 case SpvOpImageSampleProjDrefImplicitLod
:
2433 case SpvOpImageSampleProjDrefExplicitLod
:
2434 case SpvOpImageFetch
:
2435 case SpvOpImageGather
:
2436 case SpvOpImageDrefGather
:
2437 case SpvOpImageQueryLod
:
2438 case SpvOpFragmentFetchAMD
:
2439 case SpvOpFragmentMaskFetchAMD
: {
2440 /* All these types have the coordinate as their first real argument */
2441 coord_components
= glsl_get_sampler_dim_coordinate_components(sampler_dim
);
2443 if (is_array
&& texop
!= nir_texop_lod
)
2446 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
2447 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2448 (1 << coord_components
) - 1));
2449 p
->src_type
= nir_tex_src_coord
;
2456 coord_components
= 0;
2461 case SpvOpImageSampleProjImplicitLod
:
2462 case SpvOpImageSampleProjExplicitLod
:
2463 case SpvOpImageSampleProjDrefImplicitLod
:
2464 case SpvOpImageSampleProjDrefExplicitLod
:
2465 /* These have the projector as the last coordinate component */
2466 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2467 p
->src_type
= nir_tex_src_projector
;
2475 bool is_shadow
= false;
2476 unsigned gather_component
= 0;
2478 case SpvOpImageSampleDrefImplicitLod
:
2479 case SpvOpImageSampleDrefExplicitLod
:
2480 case SpvOpImageSampleProjDrefImplicitLod
:
2481 case SpvOpImageSampleProjDrefExplicitLod
:
2482 case SpvOpImageDrefGather
:
2483 /* These all have an explicit depth value as their next source */
2485 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2488 case SpvOpImageGather
:
2489 /* This has a component as its next source */
2490 gather_component
= vtn_constant_uint(b
, w
[idx
++]);
2497 /* For OpImageQuerySizeLod, we always have an LOD */
2498 if (opcode
== SpvOpImageQuerySizeLod
)
2499 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2501 /* For OpFragmentFetchAMD, we always have a multisample index */
2502 if (opcode
== SpvOpFragmentFetchAMD
)
2503 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
2505 /* Now we need to handle some number of optional arguments */
2506 struct vtn_value
*gather_offsets
= NULL
;
2508 uint32_t operands
= w
[idx
];
2510 if (operands
& SpvImageOperandsBiasMask
) {
2511 vtn_assert(texop
== nir_texop_tex
||
2512 texop
== nir_texop_tg4
);
2513 if (texop
== nir_texop_tex
)
2514 texop
= nir_texop_txb
;
2515 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2516 SpvImageOperandsBiasMask
);
2517 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_bias
);
2520 if (operands
& SpvImageOperandsLodMask
) {
2521 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2522 texop
== nir_texop_txs
|| texop
== nir_texop_tg4
);
2523 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2524 SpvImageOperandsLodMask
);
2525 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_lod
);
2528 if (operands
& SpvImageOperandsGradMask
) {
2529 vtn_assert(texop
== nir_texop_txl
);
2530 texop
= nir_texop_txd
;
2531 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2532 SpvImageOperandsGradMask
);
2533 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_ddx
);
2534 (*p
++) = vtn_tex_src(b
, w
[arg
+ 1], nir_tex_src_ddy
);
2537 vtn_fail_if(util_bitcount(operands
& (SpvImageOperandsConstOffsetsMask
|
2538 SpvImageOperandsOffsetMask
|
2539 SpvImageOperandsConstOffsetMask
)) > 1,
2540 "At most one of the ConstOffset, Offset, and ConstOffsets "
2541 "image operands can be used on a given instruction.");
2543 if (operands
& SpvImageOperandsOffsetMask
) {
2544 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2545 SpvImageOperandsOffsetMask
);
2546 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_offset
);
2549 if (operands
& SpvImageOperandsConstOffsetMask
) {
2550 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2551 SpvImageOperandsConstOffsetMask
);
2552 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_offset
);
2555 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2556 vtn_assert(texop
== nir_texop_tg4
);
2557 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2558 SpvImageOperandsConstOffsetsMask
);
2559 gather_offsets
= vtn_value(b
, w
[arg
], vtn_value_type_constant
);
2562 if (operands
& SpvImageOperandsSampleMask
) {
2563 vtn_assert(texop
== nir_texop_txf_ms
);
2564 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2565 SpvImageOperandsSampleMask
);
2566 texop
= nir_texop_txf_ms
;
2567 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_ms_index
);
2570 if (operands
& SpvImageOperandsMinLodMask
) {
2571 vtn_assert(texop
== nir_texop_tex
||
2572 texop
== nir_texop_txb
||
2573 texop
== nir_texop_txd
);
2574 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2575 SpvImageOperandsMinLodMask
);
2576 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_min_lod
);
2580 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2583 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2585 instr
->coord_components
= coord_components
;
2586 instr
->sampler_dim
= sampler_dim
;
2587 instr
->is_array
= is_array
;
2588 instr
->is_shadow
= is_shadow
;
2589 instr
->is_new_style_shadow
=
2590 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2591 instr
->component
= gather_component
;
2593 if (image
&& (image
->access
& ACCESS_NON_UNIFORM
))
2594 instr
->texture_non_uniform
= true;
2596 if (sampler
&& (sampler
->access
& ACCESS_NON_UNIFORM
))
2597 instr
->sampler_non_uniform
= true;
2599 /* for non-query ops, get dest_type from sampler type */
2600 if (dest_type
== nir_type_invalid
) {
2601 switch (glsl_get_sampler_result_type(image_type
)) {
2602 case GLSL_TYPE_FLOAT
: dest_type
= nir_type_float
; break;
2603 case GLSL_TYPE_INT
: dest_type
= nir_type_int
; break;
2604 case GLSL_TYPE_UINT
: dest_type
= nir_type_uint
; break;
2605 case GLSL_TYPE_BOOL
: dest_type
= nir_type_bool
; break;
2607 vtn_fail("Invalid base type for sampler result");
2611 instr
->dest_type
= dest_type
;
2613 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2614 nir_tex_instr_dest_size(instr
), 32, NULL
);
2616 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2617 nir_tex_instr_dest_size(instr
));
2619 if (gather_offsets
) {
2620 vtn_fail_if(gather_offsets
->type
->base_type
!= vtn_base_type_array
||
2621 gather_offsets
->type
->length
!= 4,
2622 "ConstOffsets must be an array of size four of vectors "
2623 "of two integer components");
2625 struct vtn_type
*vec_type
= gather_offsets
->type
->array_element
;
2626 vtn_fail_if(vec_type
->base_type
!= vtn_base_type_vector
||
2627 vec_type
->length
!= 2 ||
2628 !glsl_type_is_integer(vec_type
->type
),
2629 "ConstOffsets must be an array of size four of vectors "
2630 "of two integer components");
2632 unsigned bit_size
= glsl_get_bit_size(vec_type
->type
);
2633 for (uint32_t i
= 0; i
< 4; i
++) {
2634 const nir_const_value
*cvec
=
2635 gather_offsets
->constant
->elements
[i
]->values
;
2636 for (uint32_t j
= 0; j
< 2; j
++) {
2638 case 8: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i8
; break;
2639 case 16: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i16
; break;
2640 case 32: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i32
; break;
2641 case 64: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i64
; break;
2643 vtn_fail("Unsupported bit size: %u", bit_size
);
2649 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
2650 ssa
->def
= &instr
->dest
.ssa
;
2651 vtn_push_ssa(b
, w
[2], ret_type
, ssa
);
2653 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2657 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2658 const uint32_t *w
, nir_src
*src
)
2661 case SpvOpAtomicIIncrement
:
2662 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
2665 case SpvOpAtomicIDecrement
:
2666 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
2669 case SpvOpAtomicISub
:
2671 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
2674 case SpvOpAtomicCompareExchange
:
2675 case SpvOpAtomicCompareExchangeWeak
:
2676 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
2677 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
2680 case SpvOpAtomicExchange
:
2681 case SpvOpAtomicIAdd
:
2682 case SpvOpAtomicSMin
:
2683 case SpvOpAtomicUMin
:
2684 case SpvOpAtomicSMax
:
2685 case SpvOpAtomicUMax
:
2686 case SpvOpAtomicAnd
:
2688 case SpvOpAtomicXor
:
2689 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
2693 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2697 static nir_ssa_def
*
2698 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2700 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
2702 /* The image_load_store intrinsics assume a 4-dim coordinate */
2703 unsigned dim
= glsl_get_vector_elements(coord
->type
);
2704 unsigned swizzle
[4];
2705 for (unsigned i
= 0; i
< 4; i
++)
2706 swizzle
[i
] = MIN2(i
, dim
- 1);
2708 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4);
2711 static nir_ssa_def
*
2712 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2714 if (value
->num_components
== 4)
2718 for (unsigned i
= 0; i
< 4; i
++)
2719 swiz
[i
] = i
< value
->num_components
? i
: 0;
2720 return nir_swizzle(b
, value
, swiz
, 4);
2724 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2725 const uint32_t *w
, unsigned count
)
2727 /* Just get this one out of the way */
2728 if (opcode
== SpvOpImageTexelPointer
) {
2729 struct vtn_value
*val
=
2730 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2731 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2733 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2734 val
->image
->coord
= get_image_coord(b
, w
[4]);
2735 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
2736 val
->image
->lod
= nir_imm_int(&b
->nb
, 0);
2740 struct vtn_image_pointer image
;
2741 SpvScope scope
= SpvScopeInvocation
;
2742 SpvMemorySemanticsMask semantics
= 0;
2745 case SpvOpAtomicExchange
:
2746 case SpvOpAtomicCompareExchange
:
2747 case SpvOpAtomicCompareExchangeWeak
:
2748 case SpvOpAtomicIIncrement
:
2749 case SpvOpAtomicIDecrement
:
2750 case SpvOpAtomicIAdd
:
2751 case SpvOpAtomicISub
:
2752 case SpvOpAtomicLoad
:
2753 case SpvOpAtomicSMin
:
2754 case SpvOpAtomicUMin
:
2755 case SpvOpAtomicSMax
:
2756 case SpvOpAtomicUMax
:
2757 case SpvOpAtomicAnd
:
2759 case SpvOpAtomicXor
:
2760 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
2761 scope
= vtn_constant_uint(b
, w
[4]);
2762 semantics
= vtn_constant_uint(b
, w
[5]);
2765 case SpvOpAtomicStore
:
2766 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
2767 scope
= vtn_constant_uint(b
, w
[2]);
2768 semantics
= vtn_constant_uint(b
, w
[3]);
2771 case SpvOpImageQuerySize
:
2772 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2774 image
.sample
= NULL
;
2778 case SpvOpImageRead
: {
2779 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2780 image
.coord
= get_image_coord(b
, w
[4]);
2782 const SpvImageOperandsMask operands
=
2783 count
> 5 ? w
[5] : SpvImageOperandsMaskNone
;
2785 if (operands
& SpvImageOperandsSampleMask
) {
2786 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
2787 SpvImageOperandsSampleMask
);
2788 image
.sample
= vtn_ssa_value(b
, w
[arg
])->def
;
2790 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2793 if (operands
& SpvImageOperandsMakeTexelVisibleMask
) {
2794 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
2795 "MakeTexelVisible requires NonPrivateTexel to also be set.");
2796 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
2797 SpvImageOperandsMakeTexelVisibleMask
);
2798 semantics
= SpvMemorySemanticsMakeVisibleMask
;
2799 scope
= vtn_constant_uint(b
, w
[arg
]);
2802 if (operands
& SpvImageOperandsLodMask
) {
2803 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
2804 SpvImageOperandsLodMask
);
2805 image
.lod
= vtn_ssa_value(b
, w
[arg
])->def
;
2807 image
.lod
= nir_imm_int(&b
->nb
, 0);
2810 /* TODO: Volatile. */
2815 case SpvOpImageWrite
: {
2816 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2817 image
.coord
= get_image_coord(b
, w
[2]);
2821 const SpvImageOperandsMask operands
=
2822 count
> 4 ? w
[4] : SpvImageOperandsMaskNone
;
2824 if (operands
& SpvImageOperandsSampleMask
) {
2825 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
2826 SpvImageOperandsSampleMask
);
2827 image
.sample
= vtn_ssa_value(b
, w
[arg
])->def
;
2829 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2832 if (operands
& SpvImageOperandsMakeTexelAvailableMask
) {
2833 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
2834 "MakeTexelAvailable requires NonPrivateTexel to also be set.");
2835 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
2836 SpvImageOperandsMakeTexelAvailableMask
);
2837 semantics
= SpvMemorySemanticsMakeAvailableMask
;
2838 scope
= vtn_constant_uint(b
, w
[arg
]);
2841 if (operands
& SpvImageOperandsLodMask
) {
2842 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
2843 SpvImageOperandsLodMask
);
2844 image
.lod
= vtn_ssa_value(b
, w
[arg
])->def
;
2846 image
.lod
= nir_imm_int(&b
->nb
, 0);
2849 /* TODO: Volatile. */
2855 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2858 nir_intrinsic_op op
;
2860 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
2861 OP(ImageQuerySize
, size
)
2863 OP(ImageWrite
, store
)
2864 OP(AtomicLoad
, load
)
2865 OP(AtomicStore
, store
)
2866 OP(AtomicExchange
, atomic_exchange
)
2867 OP(AtomicCompareExchange
, atomic_comp_swap
)
2868 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2869 OP(AtomicIIncrement
, atomic_add
)
2870 OP(AtomicIDecrement
, atomic_add
)
2871 OP(AtomicIAdd
, atomic_add
)
2872 OP(AtomicISub
, atomic_add
)
2873 OP(AtomicSMin
, atomic_imin
)
2874 OP(AtomicUMin
, atomic_umin
)
2875 OP(AtomicSMax
, atomic_imax
)
2876 OP(AtomicUMax
, atomic_umax
)
2877 OP(AtomicAnd
, atomic_and
)
2878 OP(AtomicOr
, atomic_or
)
2879 OP(AtomicXor
, atomic_xor
)
2882 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2885 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
2887 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
2888 intrin
->src
[0] = nir_src_for_ssa(&image_deref
->dest
.ssa
);
2890 /* ImageQuerySize doesn't take any extra parameters */
2891 if (opcode
!= SpvOpImageQuerySize
) {
2892 /* The image coordinate is always 4 components but we may not have that
2893 * many. Swizzle to compensate.
2895 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
2896 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
2899 nir_intrinsic_set_access(intrin
, image
.image
->access
);
2902 case SpvOpAtomicLoad
:
2903 case SpvOpImageQuerySize
:
2904 case SpvOpImageRead
:
2905 if (opcode
== SpvOpImageRead
|| opcode
== SpvOpAtomicLoad
) {
2906 /* Only OpImageRead can support a lod parameter if
2907 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
2908 * intrinsics definition for atomics requires us to set it for
2911 intrin
->src
[3] = nir_src_for_ssa(image
.lod
);
2914 case SpvOpAtomicStore
:
2915 case SpvOpImageWrite
: {
2916 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
2917 nir_ssa_def
*value
= vtn_ssa_value(b
, value_id
)->def
;
2918 /* nir_intrinsic_image_deref_store always takes a vec4 value */
2919 assert(op
== nir_intrinsic_image_deref_store
);
2920 intrin
->num_components
= 4;
2921 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
));
2922 /* Only OpImageWrite can support a lod parameter if
2923 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
2924 * intrinsics definition for atomics requires us to set it for
2927 intrin
->src
[4] = nir_src_for_ssa(image
.lod
);
2931 case SpvOpAtomicCompareExchange
:
2932 case SpvOpAtomicCompareExchangeWeak
:
2933 case SpvOpAtomicIIncrement
:
2934 case SpvOpAtomicIDecrement
:
2935 case SpvOpAtomicExchange
:
2936 case SpvOpAtomicIAdd
:
2937 case SpvOpAtomicISub
:
2938 case SpvOpAtomicSMin
:
2939 case SpvOpAtomicUMin
:
2940 case SpvOpAtomicSMax
:
2941 case SpvOpAtomicUMax
:
2942 case SpvOpAtomicAnd
:
2944 case SpvOpAtomicXor
:
2945 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
2949 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2952 /* Image operations implicitly have the Image storage memory semantics. */
2953 semantics
|= SpvMemorySemanticsImageMemoryMask
;
2955 SpvMemorySemanticsMask before_semantics
;
2956 SpvMemorySemanticsMask after_semantics
;
2957 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
2959 if (before_semantics
)
2960 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
2962 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
2963 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2965 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
2966 if (nir_intrinsic_infos
[op
].dest_components
== 0)
2967 intrin
->num_components
= dest_components
;
2969 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2970 nir_intrinsic_dest_components(intrin
), 32, NULL
);
2972 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2974 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
2975 if (nir_intrinsic_dest_components(intrin
) != dest_components
)
2976 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
2978 struct vtn_value
*val
=
2979 vtn_push_ssa(b
, w
[2], type
, vtn_create_ssa_value(b
, type
->type
));
2980 val
->ssa
->def
= result
;
2982 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2985 if (after_semantics
)
2986 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
2989 static nir_intrinsic_op
2990 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2993 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2994 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2995 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2996 OP(AtomicExchange
, atomic_exchange
)
2997 OP(AtomicCompareExchange
, atomic_comp_swap
)
2998 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2999 OP(AtomicIIncrement
, atomic_add
)
3000 OP(AtomicIDecrement
, atomic_add
)
3001 OP(AtomicIAdd
, atomic_add
)
3002 OP(AtomicISub
, atomic_add
)
3003 OP(AtomicSMin
, atomic_imin
)
3004 OP(AtomicUMin
, atomic_umin
)
3005 OP(AtomicSMax
, atomic_imax
)
3006 OP(AtomicUMax
, atomic_umax
)
3007 OP(AtomicAnd
, atomic_and
)
3008 OP(AtomicOr
, atomic_or
)
3009 OP(AtomicXor
, atomic_xor
)
3012 vtn_fail_with_opcode("Invalid SSBO atomic", opcode
);
3016 static nir_intrinsic_op
3017 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3020 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
3021 OP(AtomicLoad
, read_deref
)
3022 OP(AtomicExchange
, exchange
)
3023 OP(AtomicCompareExchange
, comp_swap
)
3024 OP(AtomicCompareExchangeWeak
, comp_swap
)
3025 OP(AtomicIIncrement
, inc_deref
)
3026 OP(AtomicIDecrement
, post_dec_deref
)
3027 OP(AtomicIAdd
, add_deref
)
3028 OP(AtomicISub
, add_deref
)
3029 OP(AtomicUMin
, min_deref
)
3030 OP(AtomicUMax
, max_deref
)
3031 OP(AtomicAnd
, and_deref
)
3032 OP(AtomicOr
, or_deref
)
3033 OP(AtomicXor
, xor_deref
)
3036 /* We left the following out: AtomicStore, AtomicSMin and
3037 * AtomicSmax. Right now there are not nir intrinsics for them. At this
3038 * moment Atomic Counter support is needed for ARB_spirv support, so is
3039 * only need to support GLSL Atomic Counters that are uints and don't
3040 * allow direct storage.
3042 vtn_fail("Invalid uniform atomic");
3046 static nir_intrinsic_op
3047 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3050 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
3051 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
3052 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
3053 OP(AtomicExchange
, atomic_exchange
)
3054 OP(AtomicCompareExchange
, atomic_comp_swap
)
3055 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3056 OP(AtomicIIncrement
, atomic_add
)
3057 OP(AtomicIDecrement
, atomic_add
)
3058 OP(AtomicIAdd
, atomic_add
)
3059 OP(AtomicISub
, atomic_add
)
3060 OP(AtomicSMin
, atomic_imin
)
3061 OP(AtomicUMin
, atomic_umin
)
3062 OP(AtomicSMax
, atomic_imax
)
3063 OP(AtomicUMax
, atomic_umax
)
3064 OP(AtomicAnd
, atomic_and
)
3065 OP(AtomicOr
, atomic_or
)
3066 OP(AtomicXor
, atomic_xor
)
3069 vtn_fail_with_opcode("Invalid shared atomic", opcode
);
3074 * Handles shared atomics, ssbo atomics and atomic counters.
3077 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
3078 const uint32_t *w
, UNUSED
unsigned count
)
3080 struct vtn_pointer
*ptr
;
3081 nir_intrinsic_instr
*atomic
;
3083 SpvScope scope
= SpvScopeInvocation
;
3084 SpvMemorySemanticsMask semantics
= 0;
3087 case SpvOpAtomicLoad
:
3088 case SpvOpAtomicExchange
:
3089 case SpvOpAtomicCompareExchange
:
3090 case SpvOpAtomicCompareExchangeWeak
:
3091 case SpvOpAtomicIIncrement
:
3092 case SpvOpAtomicIDecrement
:
3093 case SpvOpAtomicIAdd
:
3094 case SpvOpAtomicISub
:
3095 case SpvOpAtomicSMin
:
3096 case SpvOpAtomicUMin
:
3097 case SpvOpAtomicSMax
:
3098 case SpvOpAtomicUMax
:
3099 case SpvOpAtomicAnd
:
3101 case SpvOpAtomicXor
:
3102 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
3103 scope
= vtn_constant_uint(b
, w
[4]);
3104 semantics
= vtn_constant_uint(b
, w
[5]);
3107 case SpvOpAtomicStore
:
3108 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
3109 scope
= vtn_constant_uint(b
, w
[2]);
3110 semantics
= vtn_constant_uint(b
, w
[3]);
3114 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3117 /* uniform as "atomic counter uniform" */
3118 if (ptr
->mode
== vtn_variable_mode_uniform
) {
3119 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3120 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
3121 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3122 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3124 /* SSBO needs to initialize index/offset. In this case we don't need to,
3125 * as that info is already stored on the ptr->var->var nir_variable (see
3126 * vtn_create_variable)
3130 case SpvOpAtomicLoad
:
3131 case SpvOpAtomicExchange
:
3132 case SpvOpAtomicCompareExchange
:
3133 case SpvOpAtomicCompareExchangeWeak
:
3134 case SpvOpAtomicIIncrement
:
3135 case SpvOpAtomicIDecrement
:
3136 case SpvOpAtomicIAdd
:
3137 case SpvOpAtomicISub
:
3138 case SpvOpAtomicSMin
:
3139 case SpvOpAtomicUMin
:
3140 case SpvOpAtomicSMax
:
3141 case SpvOpAtomicUMax
:
3142 case SpvOpAtomicAnd
:
3144 case SpvOpAtomicXor
:
3145 /* Nothing: we don't need to call fill_common_atomic_sources here, as
3146 * atomic counter uniforms doesn't have sources
3151 unreachable("Invalid SPIR-V atomic");
3154 } else if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
3155 nir_ssa_def
*offset
, *index
;
3156 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
3158 assert(ptr
->mode
== vtn_variable_mode_ssbo
);
3160 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(b
, opcode
);
3161 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3165 case SpvOpAtomicLoad
:
3166 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3167 nir_intrinsic_set_align(atomic
, 4, 0);
3168 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3169 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3170 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3173 case SpvOpAtomicStore
:
3174 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3175 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3176 nir_intrinsic_set_align(atomic
, 4, 0);
3177 atomic
->src
[src
++] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
3178 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3179 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3180 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3183 case SpvOpAtomicExchange
:
3184 case SpvOpAtomicCompareExchange
:
3185 case SpvOpAtomicCompareExchangeWeak
:
3186 case SpvOpAtomicIIncrement
:
3187 case SpvOpAtomicIDecrement
:
3188 case SpvOpAtomicIAdd
:
3189 case SpvOpAtomicISub
:
3190 case SpvOpAtomicSMin
:
3191 case SpvOpAtomicUMin
:
3192 case SpvOpAtomicSMax
:
3193 case SpvOpAtomicUMax
:
3194 case SpvOpAtomicAnd
:
3196 case SpvOpAtomicXor
:
3197 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3198 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3199 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3200 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
3204 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3207 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3208 const struct glsl_type
*deref_type
= deref
->type
;
3209 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
3210 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3211 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3214 case SpvOpAtomicLoad
:
3215 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3218 case SpvOpAtomicStore
:
3219 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3220 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3221 atomic
->src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
3224 case SpvOpAtomicExchange
:
3225 case SpvOpAtomicCompareExchange
:
3226 case SpvOpAtomicCompareExchangeWeak
:
3227 case SpvOpAtomicIIncrement
:
3228 case SpvOpAtomicIDecrement
:
3229 case SpvOpAtomicIAdd
:
3230 case SpvOpAtomicISub
:
3231 case SpvOpAtomicSMin
:
3232 case SpvOpAtomicUMin
:
3233 case SpvOpAtomicSMax
:
3234 case SpvOpAtomicUMax
:
3235 case SpvOpAtomicAnd
:
3237 case SpvOpAtomicXor
:
3238 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
3242 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3246 /* Atomic ordering operations will implicitly apply to the atomic operation
3247 * storage class, so include that too.
3249 semantics
|= vtn_storage_class_to_memory_semantics(ptr
->ptr_type
->storage_class
);
3251 SpvMemorySemanticsMask before_semantics
;
3252 SpvMemorySemanticsMask after_semantics
;
3253 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
3255 if (before_semantics
)
3256 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
3258 if (opcode
!= SpvOpAtomicStore
) {
3259 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3261 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
3262 glsl_get_vector_elements(type
->type
),
3263 glsl_get_bit_size(type
->type
), NULL
);
3265 struct vtn_ssa_value
*ssa
= rzalloc(b
, struct vtn_ssa_value
);
3266 ssa
->def
= &atomic
->dest
.ssa
;
3267 ssa
->type
= type
->type
;
3268 vtn_push_ssa(b
, w
[2], type
, ssa
);
3271 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
3273 if (after_semantics
)
3274 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
3277 static nir_alu_instr
*
3278 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
3280 nir_op op
= nir_op_vec(num_components
);
3281 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
3282 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
3284 vec
->dest
.write_mask
= (1 << num_components
) - 1;
3289 struct vtn_ssa_value
*
3290 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
3292 if (src
->transposed
)
3293 return src
->transposed
;
3295 struct vtn_ssa_value
*dest
=
3296 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
3298 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
3299 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
3300 glsl_get_bit_size(src
->type
));
3301 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3302 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
3303 vec
->src
[0].swizzle
[0] = i
;
3305 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
3306 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
3307 vec
->src
[j
].swizzle
[0] = i
;
3310 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3311 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
3314 dest
->transposed
= src
;
3319 static nir_ssa_def
*
3320 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
3321 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
3322 const uint32_t *indices
)
3324 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
3326 for (unsigned i
= 0; i
< num_components
; i
++) {
3327 uint32_t index
= indices
[i
];
3328 if (index
== 0xffffffff) {
3330 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
3331 } else if (index
< src0
->num_components
) {
3332 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
3333 vec
->src
[i
].swizzle
[0] = index
;
3335 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
3336 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
3340 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3342 return &vec
->dest
.dest
.ssa
;
3346 * Concatentates a number of vectors/scalars together to produce a vector
3348 static nir_ssa_def
*
3349 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
3350 unsigned num_srcs
, nir_ssa_def
**srcs
)
3352 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
3354 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3356 * "When constructing a vector, there must be at least two Constituent
3359 vtn_assert(num_srcs
>= 2);
3361 unsigned dest_idx
= 0;
3362 for (unsigned i
= 0; i
< num_srcs
; i
++) {
3363 nir_ssa_def
*src
= srcs
[i
];
3364 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
3365 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
3366 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
3367 vec
->src
[dest_idx
].swizzle
[0] = j
;
3372 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3374 * "When constructing a vector, the total number of components in all
3375 * the operands must equal the number of components in Result Type."
3377 vtn_assert(dest_idx
== num_components
);
3379 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3381 return &vec
->dest
.dest
.ssa
;
3384 static struct vtn_ssa_value
*
3385 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3387 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3388 dest
->type
= src
->type
;
3390 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3391 dest
->def
= src
->def
;
3393 unsigned elems
= glsl_get_length(src
->type
);
3395 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3396 for (unsigned i
= 0; i
< elems
; i
++)
3397 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3403 static struct vtn_ssa_value
*
3404 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3405 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3406 unsigned num_indices
)
3408 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3410 struct vtn_ssa_value
*cur
= dest
;
3412 for (i
= 0; i
< num_indices
- 1; i
++) {
3413 /* If we got a vector here, that means the next index will be trying to
3414 * dereference a scalar.
3416 vtn_fail_if(glsl_type_is_vector_or_scalar(cur
->type
),
3417 "OpCompositeInsert has too many indices.");
3418 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3419 "All indices in an OpCompositeInsert must be in-bounds");
3420 cur
= cur
->elems
[indices
[i
]];
3423 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3424 vtn_fail_if(indices
[i
] >= glsl_get_vector_elements(cur
->type
),
3425 "All indices in an OpCompositeInsert must be in-bounds");
3427 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3428 * the component granularity. In that case, the last index will be
3429 * the index to insert the scalar into the vector.
3432 cur
->def
= nir_vector_insert_imm(&b
->nb
, cur
->def
, insert
->def
, indices
[i
]);
3434 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3435 "All indices in an OpCompositeInsert must be in-bounds");
3436 cur
->elems
[indices
[i
]] = insert
;
3442 static struct vtn_ssa_value
*
3443 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3444 const uint32_t *indices
, unsigned num_indices
)
3446 struct vtn_ssa_value
*cur
= src
;
3447 for (unsigned i
= 0; i
< num_indices
; i
++) {
3448 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3449 vtn_assert(i
== num_indices
- 1);
3450 vtn_fail_if(indices
[i
] >= glsl_get_vector_elements(cur
->type
),
3451 "All indices in an OpCompositeExtract must be in-bounds");
3453 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3454 * the component granularity. The last index will be the index of the
3455 * vector to extract.
3458 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
3459 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
3460 ret
->def
= nir_channel(&b
->nb
, cur
->def
, indices
[i
]);
3463 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3464 "All indices in an OpCompositeExtract must be in-bounds");
3465 cur
= cur
->elems
[indices
[i
]];
3473 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3474 const uint32_t *w
, unsigned count
)
3476 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3477 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, type
->type
);
3480 case SpvOpVectorExtractDynamic
:
3481 ssa
->def
= nir_vector_extract(&b
->nb
, vtn_ssa_value(b
, w
[3])->def
,
3482 vtn_ssa_value(b
, w
[4])->def
);
3485 case SpvOpVectorInsertDynamic
:
3486 ssa
->def
= nir_vector_insert(&b
->nb
, vtn_ssa_value(b
, w
[3])->def
,
3487 vtn_ssa_value(b
, w
[4])->def
,
3488 vtn_ssa_value(b
, w
[5])->def
);
3491 case SpvOpVectorShuffle
:
3492 ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
->type
),
3493 vtn_ssa_value(b
, w
[3])->def
,
3494 vtn_ssa_value(b
, w
[4])->def
,
3498 case SpvOpCompositeConstruct
: {
3499 unsigned elems
= count
- 3;
3501 if (glsl_type_is_vector_or_scalar(type
->type
)) {
3502 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3503 for (unsigned i
= 0; i
< elems
; i
++)
3504 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
3506 vtn_vector_construct(b
, glsl_get_vector_elements(type
->type
),
3509 ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3510 for (unsigned i
= 0; i
< elems
; i
++)
3511 ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3515 case SpvOpCompositeExtract
:
3516 ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3520 case SpvOpCompositeInsert
:
3521 ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3522 vtn_ssa_value(b
, w
[3]),
3526 case SpvOpCopyLogical
:
3527 ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3529 case SpvOpCopyObject
:
3530 vtn_copy_value(b
, w
[3], w
[2]);
3534 vtn_fail_with_opcode("unknown composite operation", opcode
);
3537 vtn_push_ssa(b
, w
[2], type
, ssa
);
3541 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3543 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3544 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3548 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3549 SpvMemorySemanticsMask semantics
)
3551 if (b
->shader
->options
->use_scoped_barrier
) {
3552 vtn_emit_scoped_memory_barrier(b
, scope
, semantics
);
3556 static const SpvMemorySemanticsMask all_memory_semantics
=
3557 SpvMemorySemanticsUniformMemoryMask
|
3558 SpvMemorySemanticsWorkgroupMemoryMask
|
3559 SpvMemorySemanticsAtomicCounterMemoryMask
|
3560 SpvMemorySemanticsImageMemoryMask
|
3561 SpvMemorySemanticsOutputMemoryMask
;
3563 /* If we're not actually doing a memory barrier, bail */
3564 if (!(semantics
& all_memory_semantics
))
3567 /* GL and Vulkan don't have these */
3568 vtn_assert(scope
!= SpvScopeCrossDevice
);
3570 if (scope
== SpvScopeSubgroup
)
3571 return; /* Nothing to do here */
3573 if (scope
== SpvScopeWorkgroup
) {
3574 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3578 /* There's only two scopes thing left */
3579 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3581 /* Map the GLSL memoryBarrier() construct and any barriers with more than one
3582 * semantic to the corresponding NIR one.
3584 if (util_bitcount(semantics
& all_memory_semantics
) > 1) {
3585 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3586 if (semantics
& SpvMemorySemanticsOutputMemoryMask
) {
3587 /* GLSL memoryBarrier() (and the corresponding NIR one) doesn't include
3588 * TCS outputs, so we have to emit it's own intrinsic for that. We
3589 * then need to emit another memory_barrier to prevent moving
3590 * non-output operations to before the tcs_patch barrier.
3592 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_tcs_patch
);
3593 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3598 /* Issue a more specific barrier */
3599 switch (semantics
& all_memory_semantics
) {
3600 case SpvMemorySemanticsUniformMemoryMask
:
3601 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3603 case SpvMemorySemanticsWorkgroupMemoryMask
:
3604 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3606 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3607 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3609 case SpvMemorySemanticsImageMemoryMask
:
3610 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3612 case SpvMemorySemanticsOutputMemoryMask
:
3613 if (b
->nb
.shader
->info
.stage
== MESA_SHADER_TESS_CTRL
)
3614 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_tcs_patch
);
3622 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3623 const uint32_t *w
, UNUSED
unsigned count
)
3626 case SpvOpEmitVertex
:
3627 case SpvOpEmitStreamVertex
:
3628 case SpvOpEndPrimitive
:
3629 case SpvOpEndStreamPrimitive
: {
3630 nir_intrinsic_op intrinsic_op
;
3632 case SpvOpEmitVertex
:
3633 case SpvOpEmitStreamVertex
:
3634 intrinsic_op
= nir_intrinsic_emit_vertex
;
3636 case SpvOpEndPrimitive
:
3637 case SpvOpEndStreamPrimitive
:
3638 intrinsic_op
= nir_intrinsic_end_primitive
;
3641 unreachable("Invalid opcode");
3644 nir_intrinsic_instr
*intrin
=
3645 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3648 case SpvOpEmitStreamVertex
:
3649 case SpvOpEndStreamPrimitive
: {
3650 unsigned stream
= vtn_constant_uint(b
, w
[1]);
3651 nir_intrinsic_set_stream_id(intrin
, stream
);
3659 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3663 case SpvOpMemoryBarrier
: {
3664 SpvScope scope
= vtn_constant_uint(b
, w
[1]);
3665 SpvMemorySemanticsMask semantics
= vtn_constant_uint(b
, w
[2]);
3666 vtn_emit_memory_barrier(b
, scope
, semantics
);
3670 case SpvOpControlBarrier
: {
3671 SpvScope execution_scope
= vtn_constant_uint(b
, w
[1]);
3672 SpvScope memory_scope
= vtn_constant_uint(b
, w
[2]);
3673 SpvMemorySemanticsMask memory_semantics
= vtn_constant_uint(b
, w
[3]);
3675 /* GLSLang, prior to commit 8297936dd6eb3, emitted OpControlBarrier with
3676 * memory semantics of None for GLSL barrier().
3677 * And before that, prior to c3f1cdfa, emitted the OpControlBarrier with
3678 * Device instead of Workgroup for execution scope.
3680 if (b
->wa_glslang_cs_barrier
&&
3681 b
->nb
.shader
->info
.stage
== MESA_SHADER_COMPUTE
&&
3682 (execution_scope
== SpvScopeWorkgroup
||
3683 execution_scope
== SpvScopeDevice
) &&
3684 memory_semantics
== SpvMemorySemanticsMaskNone
) {
3685 execution_scope
= SpvScopeWorkgroup
;
3686 memory_scope
= SpvScopeWorkgroup
;
3687 memory_semantics
= SpvMemorySemanticsAcquireReleaseMask
|
3688 SpvMemorySemanticsWorkgroupMemoryMask
;
3691 /* From the SPIR-V spec:
3693 * "When used with the TessellationControl execution model, it also
3694 * implicitly synchronizes the Output Storage Class: Writes to Output
3695 * variables performed by any invocation executed prior to a
3696 * OpControlBarrier will be visible to any other invocation after
3697 * return from that OpControlBarrier."
3699 if (b
->nb
.shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
3700 memory_semantics
&= ~(SpvMemorySemanticsAcquireMask
|
3701 SpvMemorySemanticsReleaseMask
|
3702 SpvMemorySemanticsAcquireReleaseMask
|
3703 SpvMemorySemanticsSequentiallyConsistentMask
);
3704 memory_semantics
|= SpvMemorySemanticsAcquireReleaseMask
|
3705 SpvMemorySemanticsOutputMemoryMask
;
3708 if (b
->shader
->options
->use_scoped_barrier
) {
3709 vtn_emit_scoped_control_barrier(b
, execution_scope
, memory_scope
,
3712 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
3714 if (execution_scope
== SpvScopeWorkgroup
)
3715 vtn_emit_barrier(b
, nir_intrinsic_control_barrier
);
3721 unreachable("unknown barrier instruction");
3726 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
3727 SpvExecutionMode mode
)
3730 case SpvExecutionModeInputPoints
:
3731 case SpvExecutionModeOutputPoints
:
3732 return 0; /* GL_POINTS */
3733 case SpvExecutionModeInputLines
:
3734 return 1; /* GL_LINES */
3735 case SpvExecutionModeInputLinesAdjacency
:
3736 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3737 case SpvExecutionModeTriangles
:
3738 return 4; /* GL_TRIANGLES */
3739 case SpvExecutionModeInputTrianglesAdjacency
:
3740 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3741 case SpvExecutionModeQuads
:
3742 return 7; /* GL_QUADS */
3743 case SpvExecutionModeIsolines
:
3744 return 0x8E7A; /* GL_ISOLINES */
3745 case SpvExecutionModeOutputLineStrip
:
3746 return 3; /* GL_LINE_STRIP */
3747 case SpvExecutionModeOutputTriangleStrip
:
3748 return 5; /* GL_TRIANGLE_STRIP */
3750 vtn_fail("Invalid primitive type: %s (%u)",
3751 spirv_executionmode_to_string(mode
), mode
);
3756 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
3757 SpvExecutionMode mode
)
3760 case SpvExecutionModeInputPoints
:
3762 case SpvExecutionModeInputLines
:
3764 case SpvExecutionModeInputLinesAdjacency
:
3766 case SpvExecutionModeTriangles
:
3768 case SpvExecutionModeInputTrianglesAdjacency
:
3771 vtn_fail("Invalid GS input mode: %s (%u)",
3772 spirv_executionmode_to_string(mode
), mode
);
3776 static gl_shader_stage
3777 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
3780 case SpvExecutionModelVertex
:
3781 return MESA_SHADER_VERTEX
;
3782 case SpvExecutionModelTessellationControl
:
3783 return MESA_SHADER_TESS_CTRL
;
3784 case SpvExecutionModelTessellationEvaluation
:
3785 return MESA_SHADER_TESS_EVAL
;
3786 case SpvExecutionModelGeometry
:
3787 return MESA_SHADER_GEOMETRY
;
3788 case SpvExecutionModelFragment
:
3789 return MESA_SHADER_FRAGMENT
;
3790 case SpvExecutionModelGLCompute
:
3791 return MESA_SHADER_COMPUTE
;
3792 case SpvExecutionModelKernel
:
3793 return MESA_SHADER_KERNEL
;
3795 vtn_fail("Unsupported execution model: %s (%u)",
3796 spirv_executionmodel_to_string(model
), model
);
3800 #define spv_check_supported(name, cap) do { \
3801 if (!(b->options && b->options->caps.name)) \
3802 vtn_warn("Unsupported SPIR-V capability: %s (%u)", \
3803 spirv_capability_to_string(cap), cap); \
3808 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
3811 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
3812 /* Let this be a name label regardless */
3813 unsigned name_words
;
3814 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
3816 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
3817 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
3820 vtn_assert(b
->entry_point
== NULL
);
3821 b
->entry_point
= entry_point
;
3825 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3826 const uint32_t *w
, unsigned count
)
3833 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
3834 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
3835 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
3836 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
3837 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
3838 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
3841 uint32_t version
= w
[2];
3844 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
3846 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
3850 case SpvOpSourceExtension
:
3851 case SpvOpSourceContinued
:
3852 case SpvOpExtension
:
3853 case SpvOpModuleProcessed
:
3854 /* Unhandled, but these are for debug so that's ok. */
3857 case SpvOpCapability
: {
3858 SpvCapability cap
= w
[1];
3860 case SpvCapabilityMatrix
:
3861 case SpvCapabilityShader
:
3862 case SpvCapabilityGeometry
:
3863 case SpvCapabilityGeometryPointSize
:
3864 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
3865 case SpvCapabilitySampledImageArrayDynamicIndexing
:
3866 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
3867 case SpvCapabilityStorageImageArrayDynamicIndexing
:
3868 case SpvCapabilityImageRect
:
3869 case SpvCapabilitySampledRect
:
3870 case SpvCapabilitySampled1D
:
3871 case SpvCapabilityImage1D
:
3872 case SpvCapabilitySampledCubeArray
:
3873 case SpvCapabilityImageCubeArray
:
3874 case SpvCapabilitySampledBuffer
:
3875 case SpvCapabilityImageBuffer
:
3876 case SpvCapabilityImageQuery
:
3877 case SpvCapabilityDerivativeControl
:
3878 case SpvCapabilityInterpolationFunction
:
3879 case SpvCapabilityMultiViewport
:
3880 case SpvCapabilitySampleRateShading
:
3881 case SpvCapabilityClipDistance
:
3882 case SpvCapabilityCullDistance
:
3883 case SpvCapabilityInputAttachment
:
3884 case SpvCapabilityImageGatherExtended
:
3885 case SpvCapabilityStorageImageExtendedFormats
:
3886 case SpvCapabilityVector16
:
3889 case SpvCapabilityLinkage
:
3890 case SpvCapabilityFloat16Buffer
:
3891 case SpvCapabilitySparseResidency
:
3892 vtn_warn("Unsupported SPIR-V capability: %s",
3893 spirv_capability_to_string(cap
));
3896 case SpvCapabilityMinLod
:
3897 spv_check_supported(min_lod
, cap
);
3900 case SpvCapabilityAtomicStorage
:
3901 spv_check_supported(atomic_storage
, cap
);
3904 case SpvCapabilityFloat64
:
3905 spv_check_supported(float64
, cap
);
3907 case SpvCapabilityInt64
:
3908 spv_check_supported(int64
, cap
);
3910 case SpvCapabilityInt16
:
3911 spv_check_supported(int16
, cap
);
3913 case SpvCapabilityInt8
:
3914 spv_check_supported(int8
, cap
);
3917 case SpvCapabilityTransformFeedback
:
3918 spv_check_supported(transform_feedback
, cap
);
3921 case SpvCapabilityGeometryStreams
:
3922 spv_check_supported(geometry_streams
, cap
);
3925 case SpvCapabilityInt64Atomics
:
3926 spv_check_supported(int64_atomics
, cap
);
3929 case SpvCapabilityStorageImageMultisample
:
3930 spv_check_supported(storage_image_ms
, cap
);
3933 case SpvCapabilityAddresses
:
3934 spv_check_supported(address
, cap
);
3937 case SpvCapabilityKernel
:
3938 spv_check_supported(kernel
, cap
);
3941 case SpvCapabilityImageBasic
:
3942 case SpvCapabilityImageReadWrite
:
3943 case SpvCapabilityImageMipmap
:
3944 case SpvCapabilityPipes
:
3945 case SpvCapabilityDeviceEnqueue
:
3946 case SpvCapabilityLiteralSampler
:
3947 case SpvCapabilityGenericPointer
:
3948 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
3949 spirv_capability_to_string(cap
));
3952 case SpvCapabilityImageMSArray
:
3953 spv_check_supported(image_ms_array
, cap
);
3956 case SpvCapabilityTessellation
:
3957 case SpvCapabilityTessellationPointSize
:
3958 spv_check_supported(tessellation
, cap
);
3961 case SpvCapabilityDrawParameters
:
3962 spv_check_supported(draw_parameters
, cap
);
3965 case SpvCapabilityStorageImageReadWithoutFormat
:
3966 spv_check_supported(image_read_without_format
, cap
);
3969 case SpvCapabilityStorageImageWriteWithoutFormat
:
3970 spv_check_supported(image_write_without_format
, cap
);
3973 case SpvCapabilityDeviceGroup
:
3974 spv_check_supported(device_group
, cap
);
3977 case SpvCapabilityMultiView
:
3978 spv_check_supported(multiview
, cap
);
3981 case SpvCapabilityGroupNonUniform
:
3982 spv_check_supported(subgroup_basic
, cap
);
3985 case SpvCapabilitySubgroupVoteKHR
:
3986 case SpvCapabilityGroupNonUniformVote
:
3987 spv_check_supported(subgroup_vote
, cap
);
3990 case SpvCapabilitySubgroupBallotKHR
:
3991 case SpvCapabilityGroupNonUniformBallot
:
3992 spv_check_supported(subgroup_ballot
, cap
);
3995 case SpvCapabilityGroupNonUniformShuffle
:
3996 case SpvCapabilityGroupNonUniformShuffleRelative
:
3997 spv_check_supported(subgroup_shuffle
, cap
);
4000 case SpvCapabilityGroupNonUniformQuad
:
4001 spv_check_supported(subgroup_quad
, cap
);
4004 case SpvCapabilityGroupNonUniformArithmetic
:
4005 case SpvCapabilityGroupNonUniformClustered
:
4006 spv_check_supported(subgroup_arithmetic
, cap
);
4009 case SpvCapabilityGroups
:
4010 spv_check_supported(amd_shader_ballot
, cap
);
4013 case SpvCapabilityVariablePointersStorageBuffer
:
4014 case SpvCapabilityVariablePointers
:
4015 spv_check_supported(variable_pointers
, cap
);
4016 b
->variable_pointers
= true;
4019 case SpvCapabilityStorageUniformBufferBlock16
:
4020 case SpvCapabilityStorageUniform16
:
4021 case SpvCapabilityStoragePushConstant16
:
4022 case SpvCapabilityStorageInputOutput16
:
4023 spv_check_supported(storage_16bit
, cap
);
4026 case SpvCapabilityShaderLayer
:
4027 case SpvCapabilityShaderViewportIndex
:
4028 case SpvCapabilityShaderViewportIndexLayerEXT
:
4029 spv_check_supported(shader_viewport_index_layer
, cap
);
4032 case SpvCapabilityStorageBuffer8BitAccess
:
4033 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
4034 case SpvCapabilityStoragePushConstant8
:
4035 spv_check_supported(storage_8bit
, cap
);
4038 case SpvCapabilityShaderNonUniformEXT
:
4039 spv_check_supported(descriptor_indexing
, cap
);
4042 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
4043 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
4044 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
4045 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
4048 case SpvCapabilityUniformBufferArrayNonUniformIndexingEXT
:
4049 case SpvCapabilitySampledImageArrayNonUniformIndexingEXT
:
4050 case SpvCapabilityStorageBufferArrayNonUniformIndexingEXT
:
4051 case SpvCapabilityStorageImageArrayNonUniformIndexingEXT
:
4052 case SpvCapabilityInputAttachmentArrayNonUniformIndexingEXT
:
4053 case SpvCapabilityUniformTexelBufferArrayNonUniformIndexingEXT
:
4054 case SpvCapabilityStorageTexelBufferArrayNonUniformIndexingEXT
:
4055 spv_check_supported(descriptor_array_non_uniform_indexing
, cap
);
4058 case SpvCapabilityRuntimeDescriptorArrayEXT
:
4059 spv_check_supported(runtime_descriptor_array
, cap
);
4062 case SpvCapabilityStencilExportEXT
:
4063 spv_check_supported(stencil_export
, cap
);
4066 case SpvCapabilitySampleMaskPostDepthCoverage
:
4067 spv_check_supported(post_depth_coverage
, cap
);
4070 case SpvCapabilityDenormFlushToZero
:
4071 case SpvCapabilityDenormPreserve
:
4072 case SpvCapabilitySignedZeroInfNanPreserve
:
4073 case SpvCapabilityRoundingModeRTE
:
4074 case SpvCapabilityRoundingModeRTZ
:
4075 spv_check_supported(float_controls
, cap
);
4078 case SpvCapabilityPhysicalStorageBufferAddresses
:
4079 spv_check_supported(physical_storage_buffer_address
, cap
);
4082 case SpvCapabilityComputeDerivativeGroupQuadsNV
:
4083 case SpvCapabilityComputeDerivativeGroupLinearNV
:
4084 spv_check_supported(derivative_group
, cap
);
4087 case SpvCapabilityFloat16
:
4088 spv_check_supported(float16
, cap
);
4091 case SpvCapabilityFragmentShaderSampleInterlockEXT
:
4092 spv_check_supported(fragment_shader_sample_interlock
, cap
);
4095 case SpvCapabilityFragmentShaderPixelInterlockEXT
:
4096 spv_check_supported(fragment_shader_pixel_interlock
, cap
);
4099 case SpvCapabilityDemoteToHelperInvocationEXT
:
4100 spv_check_supported(demote_to_helper_invocation
, cap
);
4103 case SpvCapabilityShaderClockKHR
:
4104 spv_check_supported(shader_clock
, cap
);
4107 case SpvCapabilityVulkanMemoryModel
:
4108 spv_check_supported(vk_memory_model
, cap
);
4111 case SpvCapabilityVulkanMemoryModelDeviceScope
:
4112 spv_check_supported(vk_memory_model_device_scope
, cap
);
4115 case SpvCapabilityImageReadWriteLodAMD
:
4116 spv_check_supported(amd_image_read_write_lod
, cap
);
4119 case SpvCapabilityIntegerFunctions2INTEL
:
4120 spv_check_supported(integer_functions2
, cap
);
4123 case SpvCapabilityFragmentMaskAMD
:
4124 spv_check_supported(amd_fragment_mask
, cap
);
4127 case SpvCapabilityImageGatherBiasLodAMD
:
4128 spv_check_supported(amd_image_gather_bias_lod
, cap
);
4132 vtn_fail("Unhandled capability: %s (%u)",
4133 spirv_capability_to_string(cap
), cap
);
4138 case SpvOpExtInstImport
:
4139 vtn_handle_extension(b
, opcode
, w
, count
);
4142 case SpvOpMemoryModel
:
4144 case SpvAddressingModelPhysical32
:
4145 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4146 "AddressingModelPhysical32 only supported for kernels");
4147 b
->shader
->info
.cs
.ptr_size
= 32;
4148 b
->physical_ptrs
= true;
4149 b
->options
->shared_addr_format
= nir_address_format_32bit_global
;
4150 b
->options
->global_addr_format
= nir_address_format_32bit_global
;
4151 b
->options
->temp_addr_format
= nir_address_format_32bit_global
;
4153 case SpvAddressingModelPhysical64
:
4154 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4155 "AddressingModelPhysical64 only supported for kernels");
4156 b
->shader
->info
.cs
.ptr_size
= 64;
4157 b
->physical_ptrs
= true;
4158 b
->options
->shared_addr_format
= nir_address_format_64bit_global
;
4159 b
->options
->global_addr_format
= nir_address_format_64bit_global
;
4160 b
->options
->temp_addr_format
= nir_address_format_64bit_global
;
4162 case SpvAddressingModelLogical
:
4163 vtn_fail_if(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
,
4164 "AddressingModelLogical only supported for shaders");
4165 b
->physical_ptrs
= false;
4167 case SpvAddressingModelPhysicalStorageBuffer64
:
4168 vtn_fail_if(!b
->options
||
4169 !b
->options
->caps
.physical_storage_buffer_address
,
4170 "AddressingModelPhysicalStorageBuffer64 not supported");
4173 vtn_fail("Unknown addressing model: %s (%u)",
4174 spirv_addressingmodel_to_string(w
[1]), w
[1]);
4178 b
->mem_model
= w
[2];
4180 case SpvMemoryModelSimple
:
4181 case SpvMemoryModelGLSL450
:
4182 case SpvMemoryModelOpenCL
:
4184 case SpvMemoryModelVulkan
:
4185 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
4186 "Vulkan memory model is unsupported by this driver");
4189 vtn_fail("Unsupported memory model: %s",
4190 spirv_memorymodel_to_string(w
[2]));
4195 case SpvOpEntryPoint
:
4196 vtn_handle_entry_point(b
, w
, count
);
4200 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
4201 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4205 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4208 case SpvOpMemberName
:
4212 case SpvOpExecutionMode
:
4213 case SpvOpExecutionModeId
:
4214 case SpvOpDecorationGroup
:
4216 case SpvOpDecorateId
:
4217 case SpvOpMemberDecorate
:
4218 case SpvOpGroupDecorate
:
4219 case SpvOpGroupMemberDecorate
:
4220 case SpvOpDecorateString
:
4221 case SpvOpMemberDecorateString
:
4222 vtn_handle_decoration(b
, opcode
, w
, count
);
4225 case SpvOpExtInst
: {
4226 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4227 if (val
->ext_handler
== vtn_handle_non_semantic_instruction
) {
4228 /* NonSemantic extended instructions are acceptable in preamble. */
4229 vtn_handle_non_semantic_instruction(b
, w
[4], w
, count
);
4232 return false; /* End of preamble. */
4237 return false; /* End of preamble */
4244 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4245 const struct vtn_decoration
*mode
, UNUSED
void *data
)
4247 vtn_assert(b
->entry_point
== entry_point
);
4249 switch(mode
->exec_mode
) {
4250 case SpvExecutionModeOriginUpperLeft
:
4251 case SpvExecutionModeOriginLowerLeft
:
4252 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4253 b
->shader
->info
.fs
.origin_upper_left
=
4254 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
4257 case SpvExecutionModeEarlyFragmentTests
:
4258 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4259 b
->shader
->info
.fs
.early_fragment_tests
= true;
4262 case SpvExecutionModePostDepthCoverage
:
4263 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4264 b
->shader
->info
.fs
.post_depth_coverage
= true;
4267 case SpvExecutionModeInvocations
:
4268 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4269 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->operands
[0]);
4272 case SpvExecutionModeDepthReplacing
:
4273 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4274 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
4276 case SpvExecutionModeDepthGreater
:
4277 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4278 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
4280 case SpvExecutionModeDepthLess
:
4281 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4282 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
4284 case SpvExecutionModeDepthUnchanged
:
4285 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4286 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
4289 case SpvExecutionModeLocalSize
:
4290 vtn_assert(gl_shader_stage_is_compute(b
->shader
->info
.stage
));
4291 b
->shader
->info
.cs
.local_size
[0] = mode
->operands
[0];
4292 b
->shader
->info
.cs
.local_size
[1] = mode
->operands
[1];
4293 b
->shader
->info
.cs
.local_size
[2] = mode
->operands
[2];
4296 case SpvExecutionModeLocalSizeId
:
4297 b
->shader
->info
.cs
.local_size
[0] = vtn_constant_uint(b
, mode
->operands
[0]);
4298 b
->shader
->info
.cs
.local_size
[1] = vtn_constant_uint(b
, mode
->operands
[1]);
4299 b
->shader
->info
.cs
.local_size
[2] = vtn_constant_uint(b
, mode
->operands
[2]);
4302 case SpvExecutionModeLocalSizeHint
:
4303 case SpvExecutionModeLocalSizeHintId
:
4304 break; /* Nothing to do with this */
4306 case SpvExecutionModeOutputVertices
:
4307 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4308 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4309 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->operands
[0];
4311 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4312 b
->shader
->info
.gs
.vertices_out
= mode
->operands
[0];
4316 case SpvExecutionModeInputPoints
:
4317 case SpvExecutionModeInputLines
:
4318 case SpvExecutionModeInputLinesAdjacency
:
4319 case SpvExecutionModeTriangles
:
4320 case SpvExecutionModeInputTrianglesAdjacency
:
4321 case SpvExecutionModeQuads
:
4322 case SpvExecutionModeIsolines
:
4323 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4324 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4325 b
->shader
->info
.tess
.primitive_mode
=
4326 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4328 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4329 b
->shader
->info
.gs
.vertices_in
=
4330 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
4331 b
->shader
->info
.gs
.input_primitive
=
4332 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4336 case SpvExecutionModeOutputPoints
:
4337 case SpvExecutionModeOutputLineStrip
:
4338 case SpvExecutionModeOutputTriangleStrip
:
4339 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4340 b
->shader
->info
.gs
.output_primitive
=
4341 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4344 case SpvExecutionModeSpacingEqual
:
4345 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4346 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4347 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
4349 case SpvExecutionModeSpacingFractionalEven
:
4350 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4351 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4352 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
4354 case SpvExecutionModeSpacingFractionalOdd
:
4355 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4356 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4357 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
4359 case SpvExecutionModeVertexOrderCw
:
4360 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4361 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4362 b
->shader
->info
.tess
.ccw
= false;
4364 case SpvExecutionModeVertexOrderCcw
:
4365 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4366 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4367 b
->shader
->info
.tess
.ccw
= true;
4369 case SpvExecutionModePointMode
:
4370 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4371 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4372 b
->shader
->info
.tess
.point_mode
= true;
4375 case SpvExecutionModePixelCenterInteger
:
4376 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4377 b
->shader
->info
.fs
.pixel_center_integer
= true;
4380 case SpvExecutionModeXfb
:
4381 b
->shader
->info
.has_transform_feedback_varyings
= true;
4384 case SpvExecutionModeVecTypeHint
:
4387 case SpvExecutionModeContractionOff
:
4388 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
4389 vtn_warn("ExectionMode only allowed for CL-style kernels: %s",
4390 spirv_executionmode_to_string(mode
->exec_mode
));
4395 case SpvExecutionModeStencilRefReplacingEXT
:
4396 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4399 case SpvExecutionModeDerivativeGroupQuadsNV
:
4400 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4401 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_QUADS
;
4404 case SpvExecutionModeDerivativeGroupLinearNV
:
4405 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4406 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_LINEAR
;
4409 case SpvExecutionModePixelInterlockOrderedEXT
:
4410 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4411 b
->shader
->info
.fs
.pixel_interlock_ordered
= true;
4414 case SpvExecutionModePixelInterlockUnorderedEXT
:
4415 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4416 b
->shader
->info
.fs
.pixel_interlock_unordered
= true;
4419 case SpvExecutionModeSampleInterlockOrderedEXT
:
4420 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4421 b
->shader
->info
.fs
.sample_interlock_ordered
= true;
4424 case SpvExecutionModeSampleInterlockUnorderedEXT
:
4425 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4426 b
->shader
->info
.fs
.sample_interlock_unordered
= true;
4429 case SpvExecutionModeDenormPreserve
:
4430 case SpvExecutionModeDenormFlushToZero
:
4431 case SpvExecutionModeSignedZeroInfNanPreserve
:
4432 case SpvExecutionModeRoundingModeRTE
:
4433 case SpvExecutionModeRoundingModeRTZ
:
4434 /* Already handled in vtn_handle_rounding_mode_in_execution_mode() */
4438 vtn_fail("Unhandled execution mode: %s (%u)",
4439 spirv_executionmode_to_string(mode
->exec_mode
),
4445 vtn_handle_rounding_mode_in_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4446 const struct vtn_decoration
*mode
, void *data
)
4448 vtn_assert(b
->entry_point
== entry_point
);
4450 unsigned execution_mode
= 0;
4452 switch(mode
->exec_mode
) {
4453 case SpvExecutionModeDenormPreserve
:
4454 switch (mode
->operands
[0]) {
4455 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP16
; break;
4456 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP32
; break;
4457 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP64
; break;
4458 default: vtn_fail("Floating point type not supported");
4461 case SpvExecutionModeDenormFlushToZero
:
4462 switch (mode
->operands
[0]) {
4463 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
; break;
4464 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32
; break;
4465 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64
; break;
4466 default: vtn_fail("Floating point type not supported");
4469 case SpvExecutionModeSignedZeroInfNanPreserve
:
4470 switch (mode
->operands
[0]) {
4471 case 16: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16
; break;
4472 case 32: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32
; break;
4473 case 64: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64
; break;
4474 default: vtn_fail("Floating point type not supported");
4477 case SpvExecutionModeRoundingModeRTE
:
4478 switch (mode
->operands
[0]) {
4479 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16
; break;
4480 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32
; break;
4481 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64
; break;
4482 default: vtn_fail("Floating point type not supported");
4485 case SpvExecutionModeRoundingModeRTZ
:
4486 switch (mode
->operands
[0]) {
4487 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
; break;
4488 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
; break;
4489 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
; break;
4490 default: vtn_fail("Floating point type not supported");
4498 b
->shader
->info
.float_controls_execution_mode
|= execution_mode
;
4502 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4503 const uint32_t *w
, unsigned count
)
4505 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
4509 case SpvOpSourceContinued
:
4510 case SpvOpSourceExtension
:
4511 case SpvOpExtension
:
4512 case SpvOpCapability
:
4513 case SpvOpExtInstImport
:
4514 case SpvOpMemoryModel
:
4515 case SpvOpEntryPoint
:
4516 case SpvOpExecutionMode
:
4519 case SpvOpMemberName
:
4520 case SpvOpDecorationGroup
:
4522 case SpvOpDecorateId
:
4523 case SpvOpMemberDecorate
:
4524 case SpvOpGroupDecorate
:
4525 case SpvOpGroupMemberDecorate
:
4526 case SpvOpDecorateString
:
4527 case SpvOpMemberDecorateString
:
4528 vtn_fail("Invalid opcode types and variables section");
4534 case SpvOpTypeFloat
:
4535 case SpvOpTypeVector
:
4536 case SpvOpTypeMatrix
:
4537 case SpvOpTypeImage
:
4538 case SpvOpTypeSampler
:
4539 case SpvOpTypeSampledImage
:
4540 case SpvOpTypeArray
:
4541 case SpvOpTypeRuntimeArray
:
4542 case SpvOpTypeStruct
:
4543 case SpvOpTypeOpaque
:
4544 case SpvOpTypePointer
:
4545 case SpvOpTypeForwardPointer
:
4546 case SpvOpTypeFunction
:
4547 case SpvOpTypeEvent
:
4548 case SpvOpTypeDeviceEvent
:
4549 case SpvOpTypeReserveId
:
4550 case SpvOpTypeQueue
:
4552 vtn_handle_type(b
, opcode
, w
, count
);
4555 case SpvOpConstantTrue
:
4556 case SpvOpConstantFalse
:
4558 case SpvOpConstantComposite
:
4559 case SpvOpConstantSampler
:
4560 case SpvOpConstantNull
:
4561 case SpvOpSpecConstantTrue
:
4562 case SpvOpSpecConstantFalse
:
4563 case SpvOpSpecConstant
:
4564 case SpvOpSpecConstantComposite
:
4565 case SpvOpSpecConstantOp
:
4566 vtn_handle_constant(b
, opcode
, w
, count
);
4571 vtn_handle_variables(b
, opcode
, w
, count
);
4574 case SpvOpExtInst
: {
4575 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4576 /* NonSemantic extended instructions are acceptable in preamble, others
4577 * will indicate the end of preamble.
4579 return val
->ext_handler
== vtn_handle_non_semantic_instruction
;
4583 return false; /* End of preamble */
4589 static struct vtn_ssa_value
*
4590 vtn_nir_select(struct vtn_builder
*b
, struct vtn_ssa_value
*src0
,
4591 struct vtn_ssa_value
*src1
, struct vtn_ssa_value
*src2
)
4593 struct vtn_ssa_value
*dest
= rzalloc(b
, struct vtn_ssa_value
);
4594 dest
->type
= src1
->type
;
4596 if (glsl_type_is_vector_or_scalar(src1
->type
)) {
4597 dest
->def
= nir_bcsel(&b
->nb
, src0
->def
, src1
->def
, src2
->def
);
4599 unsigned elems
= glsl_get_length(src1
->type
);
4601 dest
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
4602 for (unsigned i
= 0; i
< elems
; i
++) {
4603 dest
->elems
[i
] = vtn_nir_select(b
, src0
,
4604 src1
->elems
[i
], src2
->elems
[i
]);
4612 vtn_handle_select(struct vtn_builder
*b
, SpvOp opcode
,
4613 const uint32_t *w
, unsigned count
)
4615 /* Handle OpSelect up-front here because it needs to be able to handle
4616 * pointers and not just regular vectors and scalars.
4618 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
4619 struct vtn_value
*cond_val
= vtn_untyped_value(b
, w
[3]);
4620 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
4621 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
4623 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4624 obj2_val
->type
!= res_val
->type
,
4625 "Object types must match the result type in OpSelect");
4627 vtn_fail_if((cond_val
->type
->base_type
!= vtn_base_type_scalar
&&
4628 cond_val
->type
->base_type
!= vtn_base_type_vector
) ||
4629 !glsl_type_is_boolean(cond_val
->type
->type
),
4630 "OpSelect must have either a vector of booleans or "
4631 "a boolean as Condition type");
4633 vtn_fail_if(cond_val
->type
->base_type
== vtn_base_type_vector
&&
4634 (res_val
->type
->base_type
!= vtn_base_type_vector
||
4635 res_val
->type
->length
!= cond_val
->type
->length
),
4636 "When Condition type in OpSelect is a vector, the Result "
4637 "type must be a vector of the same length");
4639 switch (res_val
->type
->base_type
) {
4640 case vtn_base_type_scalar
:
4641 case vtn_base_type_vector
:
4642 case vtn_base_type_matrix
:
4643 case vtn_base_type_array
:
4644 case vtn_base_type_struct
:
4647 case vtn_base_type_pointer
:
4648 /* We need to have actual storage for pointer types. */
4649 vtn_fail_if(res_val
->type
->type
== NULL
,
4650 "Invalid pointer result type for OpSelect");
4653 vtn_fail("Result type of OpSelect must be a scalar, composite, or pointer");
4656 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4657 struct vtn_ssa_value
*ssa
= vtn_nir_select(b
,
4658 vtn_ssa_value(b
, w
[3]), vtn_ssa_value(b
, w
[4]), vtn_ssa_value(b
, w
[5]));
4660 vtn_push_ssa(b
, w
[2], res_type
, ssa
);
4664 vtn_handle_ptr(struct vtn_builder
*b
, SpvOp opcode
,
4665 const uint32_t *w
, unsigned count
)
4667 struct vtn_type
*type1
= vtn_untyped_value(b
, w
[3])->type
;
4668 struct vtn_type
*type2
= vtn_untyped_value(b
, w
[4])->type
;
4669 vtn_fail_if(type1
->base_type
!= vtn_base_type_pointer
||
4670 type2
->base_type
!= vtn_base_type_pointer
,
4671 "%s operands must have pointer types",
4672 spirv_op_to_string(opcode
));
4673 vtn_fail_if(type1
->storage_class
!= type2
->storage_class
,
4674 "%s operands must have the same storage class",
4675 spirv_op_to_string(opcode
));
4677 struct vtn_type
*vtn_type
=
4678 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4679 const struct glsl_type
*type
= vtn_type
->type
;
4681 nir_address_format addr_format
= vtn_mode_to_address_format(
4682 b
, vtn_storage_class_to_mode(b
, type1
->storage_class
, NULL
, NULL
));
4687 case SpvOpPtrDiff
: {
4688 /* OpPtrDiff returns the difference in number of elements (not byte offset). */
4689 unsigned elem_size
, elem_align
;
4690 glsl_get_natural_size_align_bytes(type1
->deref
->type
,
4691 &elem_size
, &elem_align
);
4693 def
= nir_build_addr_isub(&b
->nb
,
4694 vtn_ssa_value(b
, w
[3])->def
,
4695 vtn_ssa_value(b
, w
[4])->def
,
4697 def
= nir_idiv(&b
->nb
, def
, nir_imm_intN_t(&b
->nb
, elem_size
, def
->bit_size
));
4698 def
= nir_i2i(&b
->nb
, def
, glsl_get_bit_size(type
));
4703 case SpvOpPtrNotEqual
: {
4704 def
= nir_build_addr_ieq(&b
->nb
,
4705 vtn_ssa_value(b
, w
[3])->def
,
4706 vtn_ssa_value(b
, w
[4])->def
,
4708 if (opcode
== SpvOpPtrNotEqual
)
4709 def
= nir_inot(&b
->nb
, def
);
4714 unreachable("Invalid ptr operation");
4717 struct vtn_ssa_value
*ssa_value
= vtn_create_ssa_value(b
, type
);
4718 ssa_value
->def
= def
;
4719 vtn_push_ssa(b
, w
[2], vtn_type
, ssa_value
);
4723 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4724 const uint32_t *w
, unsigned count
)
4730 case SpvOpLoopMerge
:
4731 case SpvOpSelectionMerge
:
4732 /* This is handled by cfg pre-pass and walk_blocks */
4736 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
4737 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4742 vtn_handle_extension(b
, opcode
, w
, count
);
4748 case SpvOpCopyMemory
:
4749 case SpvOpCopyMemorySized
:
4750 case SpvOpAccessChain
:
4751 case SpvOpPtrAccessChain
:
4752 case SpvOpInBoundsAccessChain
:
4753 case SpvOpInBoundsPtrAccessChain
:
4754 case SpvOpArrayLength
:
4755 case SpvOpConvertPtrToU
:
4756 case SpvOpConvertUToPtr
:
4757 vtn_handle_variables(b
, opcode
, w
, count
);
4760 case SpvOpFunctionCall
:
4761 vtn_handle_function_call(b
, opcode
, w
, count
);
4764 case SpvOpSampledImage
:
4766 case SpvOpImageSampleImplicitLod
:
4767 case SpvOpImageSampleExplicitLod
:
4768 case SpvOpImageSampleDrefImplicitLod
:
4769 case SpvOpImageSampleDrefExplicitLod
:
4770 case SpvOpImageSampleProjImplicitLod
:
4771 case SpvOpImageSampleProjExplicitLod
:
4772 case SpvOpImageSampleProjDrefImplicitLod
:
4773 case SpvOpImageSampleProjDrefExplicitLod
:
4774 case SpvOpImageFetch
:
4775 case SpvOpImageGather
:
4776 case SpvOpImageDrefGather
:
4777 case SpvOpImageQuerySizeLod
:
4778 case SpvOpImageQueryLod
:
4779 case SpvOpImageQueryLevels
:
4780 case SpvOpImageQuerySamples
:
4781 vtn_handle_texture(b
, opcode
, w
, count
);
4784 case SpvOpImageRead
:
4785 case SpvOpImageWrite
:
4786 case SpvOpImageTexelPointer
:
4787 vtn_handle_image(b
, opcode
, w
, count
);
4790 case SpvOpImageQuerySize
: {
4791 struct vtn_pointer
*image
=
4792 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
4793 if (glsl_type_is_image(image
->type
->type
)) {
4794 vtn_handle_image(b
, opcode
, w
, count
);
4796 vtn_assert(glsl_type_is_sampler(image
->type
->type
));
4797 vtn_handle_texture(b
, opcode
, w
, count
);
4802 case SpvOpFragmentMaskFetchAMD
:
4803 case SpvOpFragmentFetchAMD
:
4804 vtn_handle_texture(b
, opcode
, w
, count
);
4807 case SpvOpAtomicLoad
:
4808 case SpvOpAtomicExchange
:
4809 case SpvOpAtomicCompareExchange
:
4810 case SpvOpAtomicCompareExchangeWeak
:
4811 case SpvOpAtomicIIncrement
:
4812 case SpvOpAtomicIDecrement
:
4813 case SpvOpAtomicIAdd
:
4814 case SpvOpAtomicISub
:
4815 case SpvOpAtomicSMin
:
4816 case SpvOpAtomicUMin
:
4817 case SpvOpAtomicSMax
:
4818 case SpvOpAtomicUMax
:
4819 case SpvOpAtomicAnd
:
4821 case SpvOpAtomicXor
: {
4822 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
4823 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4824 vtn_handle_image(b
, opcode
, w
, count
);
4826 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4827 vtn_handle_atomics(b
, opcode
, w
, count
);
4832 case SpvOpAtomicStore
: {
4833 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
4834 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4835 vtn_handle_image(b
, opcode
, w
, count
);
4837 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4838 vtn_handle_atomics(b
, opcode
, w
, count
);
4844 vtn_handle_select(b
, opcode
, w
, count
);
4852 case SpvOpConvertFToU
:
4853 case SpvOpConvertFToS
:
4854 case SpvOpConvertSToF
:
4855 case SpvOpConvertUToF
:
4859 case SpvOpQuantizeToF16
:
4860 case SpvOpPtrCastToGeneric
:
4861 case SpvOpGenericCastToPtr
:
4866 case SpvOpSignBitSet
:
4867 case SpvOpLessOrGreater
:
4869 case SpvOpUnordered
:
4884 case SpvOpVectorTimesScalar
:
4886 case SpvOpIAddCarry
:
4887 case SpvOpISubBorrow
:
4888 case SpvOpUMulExtended
:
4889 case SpvOpSMulExtended
:
4890 case SpvOpShiftRightLogical
:
4891 case SpvOpShiftRightArithmetic
:
4892 case SpvOpShiftLeftLogical
:
4893 case SpvOpLogicalEqual
:
4894 case SpvOpLogicalNotEqual
:
4895 case SpvOpLogicalOr
:
4896 case SpvOpLogicalAnd
:
4897 case SpvOpLogicalNot
:
4898 case SpvOpBitwiseOr
:
4899 case SpvOpBitwiseXor
:
4900 case SpvOpBitwiseAnd
:
4902 case SpvOpFOrdEqual
:
4903 case SpvOpFUnordEqual
:
4904 case SpvOpINotEqual
:
4905 case SpvOpFOrdNotEqual
:
4906 case SpvOpFUnordNotEqual
:
4907 case SpvOpULessThan
:
4908 case SpvOpSLessThan
:
4909 case SpvOpFOrdLessThan
:
4910 case SpvOpFUnordLessThan
:
4911 case SpvOpUGreaterThan
:
4912 case SpvOpSGreaterThan
:
4913 case SpvOpFOrdGreaterThan
:
4914 case SpvOpFUnordGreaterThan
:
4915 case SpvOpULessThanEqual
:
4916 case SpvOpSLessThanEqual
:
4917 case SpvOpFOrdLessThanEqual
:
4918 case SpvOpFUnordLessThanEqual
:
4919 case SpvOpUGreaterThanEqual
:
4920 case SpvOpSGreaterThanEqual
:
4921 case SpvOpFOrdGreaterThanEqual
:
4922 case SpvOpFUnordGreaterThanEqual
:
4928 case SpvOpFwidthFine
:
4929 case SpvOpDPdxCoarse
:
4930 case SpvOpDPdyCoarse
:
4931 case SpvOpFwidthCoarse
:
4932 case SpvOpBitFieldInsert
:
4933 case SpvOpBitFieldSExtract
:
4934 case SpvOpBitFieldUExtract
:
4935 case SpvOpBitReverse
:
4937 case SpvOpTranspose
:
4938 case SpvOpOuterProduct
:
4939 case SpvOpMatrixTimesScalar
:
4940 case SpvOpVectorTimesMatrix
:
4941 case SpvOpMatrixTimesVector
:
4942 case SpvOpMatrixTimesMatrix
:
4943 case SpvOpUCountLeadingZerosINTEL
:
4944 case SpvOpUCountTrailingZerosINTEL
:
4945 case SpvOpAbsISubINTEL
:
4946 case SpvOpAbsUSubINTEL
:
4947 case SpvOpIAddSatINTEL
:
4948 case SpvOpUAddSatINTEL
:
4949 case SpvOpIAverageINTEL
:
4950 case SpvOpUAverageINTEL
:
4951 case SpvOpIAverageRoundedINTEL
:
4952 case SpvOpUAverageRoundedINTEL
:
4953 case SpvOpISubSatINTEL
:
4954 case SpvOpUSubSatINTEL
:
4955 case SpvOpIMul32x16INTEL
:
4956 case SpvOpUMul32x16INTEL
:
4957 vtn_handle_alu(b
, opcode
, w
, count
);
4961 vtn_handle_bitcast(b
, w
, count
);
4964 case SpvOpVectorExtractDynamic
:
4965 case SpvOpVectorInsertDynamic
:
4966 case SpvOpVectorShuffle
:
4967 case SpvOpCompositeConstruct
:
4968 case SpvOpCompositeExtract
:
4969 case SpvOpCompositeInsert
:
4970 case SpvOpCopyLogical
:
4971 case SpvOpCopyObject
:
4972 vtn_handle_composite(b
, opcode
, w
, count
);
4975 case SpvOpEmitVertex
:
4976 case SpvOpEndPrimitive
:
4977 case SpvOpEmitStreamVertex
:
4978 case SpvOpEndStreamPrimitive
:
4979 case SpvOpControlBarrier
:
4980 case SpvOpMemoryBarrier
:
4981 vtn_handle_barrier(b
, opcode
, w
, count
);
4984 case SpvOpGroupNonUniformElect
:
4985 case SpvOpGroupNonUniformAll
:
4986 case SpvOpGroupNonUniformAny
:
4987 case SpvOpGroupNonUniformAllEqual
:
4988 case SpvOpGroupNonUniformBroadcast
:
4989 case SpvOpGroupNonUniformBroadcastFirst
:
4990 case SpvOpGroupNonUniformBallot
:
4991 case SpvOpGroupNonUniformInverseBallot
:
4992 case SpvOpGroupNonUniformBallotBitExtract
:
4993 case SpvOpGroupNonUniformBallotBitCount
:
4994 case SpvOpGroupNonUniformBallotFindLSB
:
4995 case SpvOpGroupNonUniformBallotFindMSB
:
4996 case SpvOpGroupNonUniformShuffle
:
4997 case SpvOpGroupNonUniformShuffleXor
:
4998 case SpvOpGroupNonUniformShuffleUp
:
4999 case SpvOpGroupNonUniformShuffleDown
:
5000 case SpvOpGroupNonUniformIAdd
:
5001 case SpvOpGroupNonUniformFAdd
:
5002 case SpvOpGroupNonUniformIMul
:
5003 case SpvOpGroupNonUniformFMul
:
5004 case SpvOpGroupNonUniformSMin
:
5005 case SpvOpGroupNonUniformUMin
:
5006 case SpvOpGroupNonUniformFMin
:
5007 case SpvOpGroupNonUniformSMax
:
5008 case SpvOpGroupNonUniformUMax
:
5009 case SpvOpGroupNonUniformFMax
:
5010 case SpvOpGroupNonUniformBitwiseAnd
:
5011 case SpvOpGroupNonUniformBitwiseOr
:
5012 case SpvOpGroupNonUniformBitwiseXor
:
5013 case SpvOpGroupNonUniformLogicalAnd
:
5014 case SpvOpGroupNonUniformLogicalOr
:
5015 case SpvOpGroupNonUniformLogicalXor
:
5016 case SpvOpGroupNonUniformQuadBroadcast
:
5017 case SpvOpGroupNonUniformQuadSwap
:
5020 case SpvOpGroupBroadcast
:
5021 case SpvOpGroupIAdd
:
5022 case SpvOpGroupFAdd
:
5023 case SpvOpGroupFMin
:
5024 case SpvOpGroupUMin
:
5025 case SpvOpGroupSMin
:
5026 case SpvOpGroupFMax
:
5027 case SpvOpGroupUMax
:
5028 case SpvOpGroupSMax
:
5029 case SpvOpSubgroupBallotKHR
:
5030 case SpvOpSubgroupFirstInvocationKHR
:
5031 case SpvOpSubgroupReadInvocationKHR
:
5032 case SpvOpSubgroupAllKHR
:
5033 case SpvOpSubgroupAnyKHR
:
5034 case SpvOpSubgroupAllEqualKHR
:
5035 case SpvOpGroupIAddNonUniformAMD
:
5036 case SpvOpGroupFAddNonUniformAMD
:
5037 case SpvOpGroupFMinNonUniformAMD
:
5038 case SpvOpGroupUMinNonUniformAMD
:
5039 case SpvOpGroupSMinNonUniformAMD
:
5040 case SpvOpGroupFMaxNonUniformAMD
:
5041 case SpvOpGroupUMaxNonUniformAMD
:
5042 case SpvOpGroupSMaxNonUniformAMD
:
5043 vtn_handle_subgroup(b
, opcode
, w
, count
);
5048 case SpvOpPtrNotEqual
:
5049 vtn_handle_ptr(b
, opcode
, w
, count
);
5052 case SpvOpBeginInvocationInterlockEXT
:
5053 vtn_emit_barrier(b
, nir_intrinsic_begin_invocation_interlock
);
5056 case SpvOpEndInvocationInterlockEXT
:
5057 vtn_emit_barrier(b
, nir_intrinsic_end_invocation_interlock
);
5060 case SpvOpDemoteToHelperInvocationEXT
: {
5061 nir_intrinsic_instr
*intrin
=
5062 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_demote
);
5063 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5067 case SpvOpIsHelperInvocationEXT
: {
5068 nir_intrinsic_instr
*intrin
=
5069 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_is_helper_invocation
);
5070 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 1, 1, NULL
);
5071 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5073 struct vtn_type
*res_type
=
5074 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
5075 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, res_type
->type
);
5076 val
->def
= &intrin
->dest
.ssa
;
5078 vtn_push_ssa(b
, w
[2], res_type
, val
);
5082 case SpvOpReadClockKHR
: {
5083 SpvScope scope
= vtn_constant_uint(b
, w
[3]);
5084 nir_scope nir_scope
;
5087 case SpvScopeDevice
:
5088 nir_scope
= NIR_SCOPE_DEVICE
;
5090 case SpvScopeSubgroup
:
5091 nir_scope
= NIR_SCOPE_SUBGROUP
;
5094 vtn_fail("invalid read clock scope");
5097 /* Operation supports two result types: uvec2 and uint64_t. The NIR
5098 * intrinsic gives uvec2, so pack the result for the other case.
5100 nir_intrinsic_instr
*intrin
=
5101 nir_intrinsic_instr_create(b
->nb
.shader
, nir_intrinsic_shader_clock
);
5102 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 2, 32, NULL
);
5103 nir_intrinsic_set_memory_scope(intrin
, nir_scope
);
5104 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5106 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
5107 const struct glsl_type
*dest_type
= type
->type
;
5108 nir_ssa_def
*result
;
5110 if (glsl_type_is_vector(dest_type
)) {
5111 assert(dest_type
== glsl_vector_type(GLSL_TYPE_UINT
, 2));
5112 result
= &intrin
->dest
.ssa
;
5114 assert(glsl_type_is_scalar(dest_type
));
5115 assert(glsl_get_base_type(dest_type
) == GLSL_TYPE_UINT64
);
5116 result
= nir_pack_64_2x32(&b
->nb
, &intrin
->dest
.ssa
);
5119 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
5121 val
->ssa
= vtn_create_ssa_value(b
, dest_type
);
5122 val
->ssa
->def
= result
;
5126 case SpvOpLifetimeStart
:
5127 case SpvOpLifetimeStop
:
5131 vtn_fail_with_opcode("Unhandled opcode", opcode
);
5138 vtn_create_builder(const uint32_t *words
, size_t word_count
,
5139 gl_shader_stage stage
, const char *entry_point_name
,
5140 const struct spirv_to_nir_options
*options
)
5142 /* Initialize the vtn_builder object */
5143 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
5144 struct spirv_to_nir_options
*dup_options
=
5145 ralloc(b
, struct spirv_to_nir_options
);
5146 *dup_options
= *options
;
5149 b
->spirv_word_count
= word_count
;
5153 list_inithead(&b
->functions
);
5154 b
->entry_point_stage
= stage
;
5155 b
->entry_point_name
= entry_point_name
;
5156 b
->options
= dup_options
;
5159 * Handle the SPIR-V header (first 5 dwords).
5160 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
5162 if (word_count
<= 5)
5165 if (words
[0] != SpvMagicNumber
) {
5166 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
5169 if (words
[1] < 0x10000) {
5170 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
5174 uint16_t generator_id
= words
[2] >> 16;
5175 uint16_t generator_version
= words
[2];
5177 /* The first GLSLang version bump actually 1.5 years after #179 was fixed
5178 * but this should at least let us shut the workaround off for modern
5179 * versions of GLSLang.
5181 b
->wa_glslang_179
= (generator_id
== 8 && generator_version
== 1);
5183 /* In GLSLang commit 8297936dd6eb3, their handling of barrier() was fixed
5184 * to provide correct memory semantics on compute shader barrier()
5185 * commands. Prior to that, we need to fix them up ourselves. This
5186 * GLSLang fix caused them to bump to generator version 3.
5188 b
->wa_glslang_cs_barrier
= (generator_id
== 8 && generator_version
< 3);
5190 /* words[2] == generator magic */
5191 unsigned value_id_bound
= words
[3];
5192 if (words
[4] != 0) {
5193 vtn_err("words[4] was %u, want 0", words
[4]);
5197 b
->value_id_bound
= value_id_bound
;
5198 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
5206 static nir_function
*
5207 vtn_emit_kernel_entry_point_wrapper(struct vtn_builder
*b
,
5208 nir_function
*entry_point
)
5210 vtn_assert(entry_point
== b
->entry_point
->func
->impl
->function
);
5211 vtn_fail_if(!entry_point
->name
, "entry points are required to have a name");
5212 const char *func_name
=
5213 ralloc_asprintf(b
->shader
, "__wrapped_%s", entry_point
->name
);
5215 /* we shouldn't have any inputs yet */
5216 vtn_assert(!entry_point
->shader
->num_inputs
);
5217 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
);
5219 nir_function
*main_entry_point
= nir_function_create(b
->shader
, func_name
);
5220 main_entry_point
->impl
= nir_function_impl_create(main_entry_point
);
5221 nir_builder_init(&b
->nb
, main_entry_point
->impl
);
5222 b
->nb
.cursor
= nir_after_cf_list(&main_entry_point
->impl
->body
);
5223 b
->func_param_idx
= 0;
5225 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, entry_point
);
5227 for (unsigned i
= 0; i
< entry_point
->num_params
; ++i
) {
5228 struct vtn_type
*param_type
= b
->entry_point
->func
->type
->params
[i
];
5230 /* consider all pointers to function memory to be parameters passed
5233 bool is_by_val
= param_type
->base_type
== vtn_base_type_pointer
&&
5234 param_type
->storage_class
== SpvStorageClassFunction
;
5236 /* input variable */
5237 nir_variable
*in_var
= rzalloc(b
->nb
.shader
, nir_variable
);
5238 in_var
->data
.mode
= nir_var_shader_in
;
5239 in_var
->data
.read_only
= true;
5240 in_var
->data
.location
= i
;
5243 in_var
->type
= param_type
->deref
->type
;
5245 in_var
->type
= param_type
->type
;
5247 nir_shader_add_variable(b
->nb
.shader
, in_var
);
5248 b
->nb
.shader
->num_inputs
++;
5250 /* we have to copy the entire variable into function memory */
5252 nir_variable
*copy_var
=
5253 nir_local_variable_create(main_entry_point
->impl
, in_var
->type
,
5255 nir_copy_var(&b
->nb
, copy_var
, in_var
);
5257 nir_src_for_ssa(&nir_build_deref_var(&b
->nb
, copy_var
)->dest
.ssa
);
5259 call
->params
[i
] = nir_src_for_ssa(nir_load_var(&b
->nb
, in_var
));
5263 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
5265 return main_entry_point
;
5269 spirv_to_nir(const uint32_t *words
, size_t word_count
,
5270 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
5271 gl_shader_stage stage
, const char *entry_point_name
,
5272 const struct spirv_to_nir_options
*options
,
5273 const nir_shader_compiler_options
*nir_options
)
5276 const uint32_t *word_end
= words
+ word_count
;
5278 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
5279 stage
, entry_point_name
,
5285 /* See also _vtn_fail() */
5286 if (setjmp(b
->fail_jump
)) {
5291 /* Skip the SPIR-V header, handled at vtn_create_builder */
5294 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
5296 /* Handle all the preamble instructions */
5297 words
= vtn_foreach_instruction(b
, words
, word_end
,
5298 vtn_handle_preamble_instruction
);
5300 if (b
->entry_point
== NULL
) {
5301 vtn_fail("Entry point not found");
5306 /* Set shader info defaults */
5307 if (stage
== MESA_SHADER_GEOMETRY
)
5308 b
->shader
->info
.gs
.invocations
= 1;
5310 /* Parse rounding mode execution modes. This has to happen earlier than
5311 * other changes in the execution modes since they can affect, for example,
5312 * the result of the floating point constants.
5314 vtn_foreach_execution_mode(b
, b
->entry_point
,
5315 vtn_handle_rounding_mode_in_execution_mode
, NULL
);
5317 b
->specializations
= spec
;
5318 b
->num_specializations
= num_spec
;
5320 /* Handle all variable, type, and constant instructions */
5321 words
= vtn_foreach_instruction(b
, words
, word_end
,
5322 vtn_handle_variable_or_type_instruction
);
5324 /* Parse execution modes */
5325 vtn_foreach_execution_mode(b
, b
->entry_point
,
5326 vtn_handle_execution_mode
, NULL
);
5328 if (b
->workgroup_size_builtin
) {
5329 vtn_assert(b
->workgroup_size_builtin
->type
->type
==
5330 glsl_vector_type(GLSL_TYPE_UINT
, 3));
5332 nir_const_value
*const_size
=
5333 b
->workgroup_size_builtin
->constant
->values
;
5335 b
->shader
->info
.cs
.local_size
[0] = const_size
[0].u32
;
5336 b
->shader
->info
.cs
.local_size
[1] = const_size
[1].u32
;
5337 b
->shader
->info
.cs
.local_size
[2] = const_size
[2].u32
;
5340 /* Set types on all vtn_values */
5341 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
5343 vtn_build_cfg(b
, words
, word_end
);
5345 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5346 b
->entry_point
->func
->referenced
= true;
5351 vtn_foreach_cf_node(node
, &b
->functions
) {
5352 struct vtn_function
*func
= vtn_cf_node_as_function(node
);
5353 if (func
->referenced
&& !func
->emitted
) {
5354 b
->const_table
= _mesa_pointer_hash_table_create(b
);
5356 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
5362 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5363 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
5364 vtn_assert(entry_point
);
5366 /* post process entry_points with input params */
5367 if (entry_point
->num_params
&& b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
5368 entry_point
= vtn_emit_kernel_entry_point_wrapper(b
, entry_point
);
5370 entry_point
->is_entrypoint
= true;
5372 /* When multiple shader stages exist in the same SPIR-V module, we
5373 * generate input and output variables for every stage, in the same
5374 * NIR program. These dead variables can be invalid NIR. For example,
5375 * TCS outputs must be per-vertex arrays (or decorated 'patch'), while
5376 * VS output variables wouldn't be.
5378 * To ensure we have valid NIR, we eliminate any dead inputs and outputs
5379 * right away. In order to do so, we must lower any constant initializers
5380 * on outputs so nir_remove_dead_variables sees that they're written to.
5382 nir_lower_variable_initializers(b
->shader
, nir_var_shader_out
);
5383 nir_remove_dead_variables(b
->shader
,
5384 nir_var_shader_in
| nir_var_shader_out
, NULL
);
5386 /* We sometimes generate bogus derefs that, while never used, give the
5387 * validator a bit of heartburn. Run dead code to get rid of them.
5389 nir_opt_dce(b
->shader
);
5391 /* Unparent the shader from the vtn_builder before we delete the builder */
5392 ralloc_steal(NULL
, b
->shader
);
5394 nir_shader
*shader
= b
->shader
;