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/u_math.h"
40 vtn_log(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
41 size_t spirv_offset
, const char *message
)
43 if (b
->options
->debug
.func
) {
44 b
->options
->debug
.func(b
->options
->debug
.private_data
,
45 level
, spirv_offset
, message
);
49 if (level
>= NIR_SPIRV_DEBUG_LEVEL_WARNING
)
50 fprintf(stderr
, "%s\n", message
);
55 vtn_logf(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
56 size_t spirv_offset
, const char *fmt
, ...)
62 msg
= ralloc_vasprintf(NULL
, fmt
, args
);
65 vtn_log(b
, level
, spirv_offset
, msg
);
71 vtn_log_err(struct vtn_builder
*b
,
72 enum nir_spirv_debug_level level
, const char *prefix
,
73 const char *file
, unsigned line
,
74 const char *fmt
, va_list args
)
78 msg
= ralloc_strdup(NULL
, prefix
);
81 ralloc_asprintf_append(&msg
, " In file %s:%u\n", file
, line
);
84 ralloc_asprintf_append(&msg
, " ");
86 ralloc_vasprintf_append(&msg
, fmt
, args
);
88 ralloc_asprintf_append(&msg
, "\n %zu bytes into the SPIR-V binary",
92 ralloc_asprintf_append(&msg
,
93 "\n in SPIR-V source file %s, line %d, col %d",
94 b
->file
, b
->line
, b
->col
);
97 vtn_log(b
, level
, b
->spirv_offset
, msg
);
103 vtn_dump_shader(struct vtn_builder
*b
, const char *path
, const char *prefix
)
108 int len
= snprintf(filename
, sizeof(filename
), "%s/%s-%d.spirv",
109 path
, prefix
, idx
++);
110 if (len
< 0 || len
>= sizeof(filename
))
113 FILE *f
= fopen(filename
, "w");
117 fwrite(b
->spirv
, sizeof(*b
->spirv
), b
->spirv_word_count
, f
);
120 vtn_info("SPIR-V shader dumped to %s", filename
);
124 _vtn_warn(struct vtn_builder
*b
, const char *file
, unsigned line
,
125 const char *fmt
, ...)
130 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_WARNING
, "SPIR-V WARNING:\n",
131 file
, line
, fmt
, args
);
136 _vtn_err(struct vtn_builder
*b
, const char *file
, unsigned line
,
137 const char *fmt
, ...)
142 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V ERROR:\n",
143 file
, line
, fmt
, args
);
148 _vtn_fail(struct vtn_builder
*b
, const char *file
, unsigned line
,
149 const char *fmt
, ...)
154 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V parsing FAILED:\n",
155 file
, line
, fmt
, args
);
158 const char *dump_path
= getenv("MESA_SPIRV_FAIL_DUMP_PATH");
160 vtn_dump_shader(b
, dump_path
, "fail");
162 longjmp(b
->fail_jump
, 1);
165 struct spec_constant_value
{
173 static struct vtn_ssa_value
*
174 vtn_undef_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
176 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
179 if (glsl_type_is_vector_or_scalar(type
)) {
180 unsigned num_components
= glsl_get_vector_elements(val
->type
);
181 unsigned bit_size
= glsl_get_bit_size(val
->type
);
182 val
->def
= nir_ssa_undef(&b
->nb
, num_components
, bit_size
);
184 unsigned elems
= glsl_get_length(val
->type
);
185 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
186 if (glsl_type_is_matrix(type
)) {
187 const struct glsl_type
*elem_type
=
188 glsl_vector_type(glsl_get_base_type(type
),
189 glsl_get_vector_elements(type
));
191 for (unsigned i
= 0; i
< elems
; i
++)
192 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
193 } else if (glsl_type_is_array(type
)) {
194 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
195 for (unsigned i
= 0; i
< elems
; i
++)
196 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
198 for (unsigned i
= 0; i
< elems
; i
++) {
199 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
200 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
208 static struct vtn_ssa_value
*
209 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
210 const struct glsl_type
*type
)
212 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
217 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
220 switch (glsl_get_base_type(type
)) {
223 case GLSL_TYPE_INT16
:
224 case GLSL_TYPE_UINT16
:
225 case GLSL_TYPE_UINT8
:
227 case GLSL_TYPE_INT64
:
228 case GLSL_TYPE_UINT64
:
230 case GLSL_TYPE_FLOAT
:
231 case GLSL_TYPE_FLOAT16
:
232 case GLSL_TYPE_DOUBLE
: {
233 int bit_size
= glsl_get_bit_size(type
);
234 if (glsl_type_is_vector_or_scalar(type
)) {
235 unsigned num_components
= glsl_get_vector_elements(val
->type
);
236 nir_load_const_instr
*load
=
237 nir_load_const_instr_create(b
->shader
, num_components
, bit_size
);
239 memcpy(load
->value
, constant
->values
,
240 sizeof(nir_const_value
) * load
->def
.num_components
);
242 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
243 val
->def
= &load
->def
;
245 assert(glsl_type_is_matrix(type
));
246 unsigned columns
= glsl_get_matrix_columns(val
->type
);
247 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, columns
);
248 const struct glsl_type
*column_type
= glsl_get_column_type(val
->type
);
249 for (unsigned i
= 0; i
< columns
; i
++)
250 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
256 case GLSL_TYPE_ARRAY
: {
257 unsigned elems
= glsl_get_length(val
->type
);
258 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
259 const struct glsl_type
*elem_type
= glsl_get_array_element(val
->type
);
260 for (unsigned i
= 0; i
< elems
; i
++)
261 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
266 case GLSL_TYPE_STRUCT
: {
267 unsigned elems
= glsl_get_length(val
->type
);
268 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
269 for (unsigned i
= 0; i
< elems
; i
++) {
270 const struct glsl_type
*elem_type
=
271 glsl_get_struct_field(val
->type
, i
);
272 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
279 vtn_fail("bad constant type");
285 struct vtn_ssa_value
*
286 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
288 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
289 switch (val
->value_type
) {
290 case vtn_value_type_undef
:
291 return vtn_undef_ssa_value(b
, val
->type
->type
);
293 case vtn_value_type_constant
:
294 return vtn_const_ssa_value(b
, val
->constant
, val
->type
->type
);
296 case vtn_value_type_ssa
:
299 case vtn_value_type_pointer
:
300 vtn_assert(val
->pointer
->ptr_type
&& val
->pointer
->ptr_type
->type
);
301 struct vtn_ssa_value
*ssa
=
302 vtn_create_ssa_value(b
, val
->pointer
->ptr_type
->type
);
303 ssa
->def
= vtn_pointer_to_ssa(b
, val
->pointer
);
307 vtn_fail("Invalid type for an SSA value");
312 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
313 unsigned word_count
, unsigned *words_used
)
315 char *dup
= ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
317 /* Ammount of space taken by the string (including the null) */
318 unsigned len
= strlen(dup
) + 1;
319 *words_used
= DIV_ROUND_UP(len
, sizeof(*words
));
325 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
326 const uint32_t *end
, vtn_instruction_handler handler
)
332 const uint32_t *w
= start
;
334 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
335 unsigned count
= w
[0] >> SpvWordCountShift
;
336 vtn_assert(count
>= 1 && w
+ count
<= end
);
338 b
->spirv_offset
= (uint8_t *)w
- (uint8_t *)b
->spirv
;
342 break; /* Do nothing */
345 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
357 if (!handler(b
, opcode
, w
, count
))
375 vtn_handle_non_semantic_instruction(struct vtn_builder
*b
, SpvOp ext_opcode
,
376 const uint32_t *w
, unsigned count
)
383 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
384 const uint32_t *w
, unsigned count
)
386 const char *ext
= (const char *)&w
[2];
388 case SpvOpExtInstImport
: {
389 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
390 if (strcmp(ext
, "GLSL.std.450") == 0) {
391 val
->ext_handler
= vtn_handle_glsl450_instruction
;
392 } else if ((strcmp(ext
, "SPV_AMD_gcn_shader") == 0)
393 && (b
->options
&& b
->options
->caps
.amd_gcn_shader
)) {
394 val
->ext_handler
= vtn_handle_amd_gcn_shader_instruction
;
395 } else if ((strcmp(ext
, "SPV_AMD_shader_ballot") == 0)
396 && (b
->options
&& b
->options
->caps
.amd_shader_ballot
)) {
397 val
->ext_handler
= vtn_handle_amd_shader_ballot_instruction
;
398 } else if ((strcmp(ext
, "SPV_AMD_shader_trinary_minmax") == 0)
399 && (b
->options
&& b
->options
->caps
.amd_trinary_minmax
)) {
400 val
->ext_handler
= vtn_handle_amd_shader_trinary_minmax_instruction
;
401 } else if (strcmp(ext
, "OpenCL.std") == 0) {
402 val
->ext_handler
= vtn_handle_opencl_instruction
;
403 } else if (strstr(ext
, "NonSemantic.") == ext
) {
404 val
->ext_handler
= vtn_handle_non_semantic_instruction
;
406 vtn_fail("Unsupported extension: %s", ext
);
412 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
413 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
419 vtn_fail_with_opcode("Unhandled opcode", opcode
);
424 _foreach_decoration_helper(struct vtn_builder
*b
,
425 struct vtn_value
*base_value
,
427 struct vtn_value
*value
,
428 vtn_decoration_foreach_cb cb
, void *data
)
430 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
432 if (dec
->scope
== VTN_DEC_DECORATION
) {
433 member
= parent_member
;
434 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
435 vtn_fail_if(value
->value_type
!= vtn_value_type_type
||
436 value
->type
->base_type
!= vtn_base_type_struct
,
437 "OpMemberDecorate and OpGroupMemberDecorate are only "
438 "allowed on OpTypeStruct");
439 /* This means we haven't recursed yet */
440 assert(value
== base_value
);
442 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
444 vtn_fail_if(member
>= base_value
->type
->length
,
445 "OpMemberDecorate specifies member %d but the "
446 "OpTypeStruct has only %u members",
447 member
, base_value
->type
->length
);
449 /* Not a decoration */
450 assert(dec
->scope
== VTN_DEC_EXECUTION_MODE
);
455 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
456 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
459 cb(b
, base_value
, member
, dec
, data
);
464 /** Iterates (recursively if needed) over all of the decorations on a value
466 * This function iterates over all of the decorations applied to a given
467 * value. If it encounters a decoration group, it recurses into the group
468 * and iterates over all of those decorations as well.
471 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
472 vtn_decoration_foreach_cb cb
, void *data
)
474 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
478 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
479 vtn_execution_mode_foreach_cb cb
, void *data
)
481 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
482 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
485 assert(dec
->group
== NULL
);
486 cb(b
, value
, dec
, data
);
491 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
492 const uint32_t *w
, unsigned count
)
494 const uint32_t *w_end
= w
+ count
;
495 const uint32_t target
= w
[1];
499 case SpvOpDecorationGroup
:
500 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
504 case SpvOpDecorateId
:
505 case SpvOpMemberDecorate
:
506 case SpvOpDecorateString
:
507 case SpvOpMemberDecorateString
:
508 case SpvOpExecutionMode
:
509 case SpvOpExecutionModeId
: {
510 struct vtn_value
*val
= vtn_untyped_value(b
, target
);
512 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
515 case SpvOpDecorateId
:
516 case SpvOpDecorateString
:
517 dec
->scope
= VTN_DEC_DECORATION
;
519 case SpvOpMemberDecorate
:
520 case SpvOpMemberDecorateString
:
521 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
522 vtn_fail_if(dec
->scope
< VTN_DEC_STRUCT_MEMBER0
, /* overflow */
523 "Member argument of OpMemberDecorate too large");
525 case SpvOpExecutionMode
:
526 case SpvOpExecutionModeId
:
527 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
530 unreachable("Invalid decoration opcode");
532 dec
->decoration
= *(w
++);
535 /* Link into the list */
536 dec
->next
= val
->decoration
;
537 val
->decoration
= dec
;
541 case SpvOpGroupMemberDecorate
:
542 case SpvOpGroupDecorate
: {
543 struct vtn_value
*group
=
544 vtn_value(b
, target
, vtn_value_type_decoration_group
);
546 for (; w
< w_end
; w
++) {
547 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
548 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
551 if (opcode
== SpvOpGroupDecorate
) {
552 dec
->scope
= VTN_DEC_DECORATION
;
554 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
555 vtn_fail_if(dec
->scope
< 0, /* Check for overflow */
556 "Member argument of OpGroupMemberDecorate too large");
559 /* Link into the list */
560 dec
->next
= val
->decoration
;
561 val
->decoration
= dec
;
567 unreachable("Unhandled opcode");
571 struct member_decoration_ctx
{
573 struct glsl_struct_field
*fields
;
574 struct vtn_type
*type
;
578 * Returns true if the given type contains a struct decorated Block or
582 vtn_type_contains_block(struct vtn_builder
*b
, struct vtn_type
*type
)
584 switch (type
->base_type
) {
585 case vtn_base_type_array
:
586 return vtn_type_contains_block(b
, type
->array_element
);
587 case vtn_base_type_struct
:
588 if (type
->block
|| type
->buffer_block
)
590 for (unsigned i
= 0; i
< type
->length
; i
++) {
591 if (vtn_type_contains_block(b
, type
->members
[i
]))
600 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
601 * OpStore, or OpCopyMemory between them without breaking anything.
602 * Technically, the SPIR-V rules require the exact same type ID but this lets
603 * us internally be a bit looser.
606 vtn_types_compatible(struct vtn_builder
*b
,
607 struct vtn_type
*t1
, struct vtn_type
*t2
)
609 if (t1
->id
== t2
->id
)
612 if (t1
->base_type
!= t2
->base_type
)
615 switch (t1
->base_type
) {
616 case vtn_base_type_void
:
617 case vtn_base_type_scalar
:
618 case vtn_base_type_vector
:
619 case vtn_base_type_matrix
:
620 case vtn_base_type_image
:
621 case vtn_base_type_sampler
:
622 case vtn_base_type_sampled_image
:
623 return t1
->type
== t2
->type
;
625 case vtn_base_type_array
:
626 return t1
->length
== t2
->length
&&
627 vtn_types_compatible(b
, t1
->array_element
, t2
->array_element
);
629 case vtn_base_type_pointer
:
630 return vtn_types_compatible(b
, t1
->deref
, t2
->deref
);
632 case vtn_base_type_struct
:
633 if (t1
->length
!= t2
->length
)
636 for (unsigned i
= 0; i
< t1
->length
; i
++) {
637 if (!vtn_types_compatible(b
, t1
->members
[i
], t2
->members
[i
]))
642 case vtn_base_type_function
:
643 /* This case shouldn't get hit since you can't copy around function
644 * types. Just require them to be identical.
649 vtn_fail("Invalid base type");
653 vtn_type_without_array(struct vtn_type
*type
)
655 while (type
->base_type
== vtn_base_type_array
)
656 type
= type
->array_element
;
660 /* does a shallow copy of a vtn_type */
662 static struct vtn_type
*
663 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
665 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
668 switch (src
->base_type
) {
669 case vtn_base_type_void
:
670 case vtn_base_type_scalar
:
671 case vtn_base_type_vector
:
672 case vtn_base_type_matrix
:
673 case vtn_base_type_array
:
674 case vtn_base_type_pointer
:
675 case vtn_base_type_image
:
676 case vtn_base_type_sampler
:
677 case vtn_base_type_sampled_image
:
678 /* Nothing more to do */
681 case vtn_base_type_struct
:
682 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
683 memcpy(dest
->members
, src
->members
,
684 src
->length
* sizeof(src
->members
[0]));
686 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
687 memcpy(dest
->offsets
, src
->offsets
,
688 src
->length
* sizeof(src
->offsets
[0]));
691 case vtn_base_type_function
:
692 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
693 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
700 static struct vtn_type
*
701 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
703 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
704 type
= type
->members
[member
];
706 /* We may have an array of matrices.... Oh, joy! */
707 while (glsl_type_is_array(type
->type
)) {
708 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
709 type
= type
->array_element
;
712 vtn_assert(glsl_type_is_matrix(type
->type
));
718 vtn_handle_access_qualifier(struct vtn_builder
*b
, struct vtn_type
*type
,
719 int member
, enum gl_access_qualifier access
)
721 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
722 type
= type
->members
[member
];
724 type
->access
|= access
;
728 array_stride_decoration_cb(struct vtn_builder
*b
,
729 struct vtn_value
*val
, int member
,
730 const struct vtn_decoration
*dec
, void *void_ctx
)
732 struct vtn_type
*type
= val
->type
;
734 if (dec
->decoration
== SpvDecorationArrayStride
) {
735 if (vtn_type_contains_block(b
, type
)) {
736 vtn_warn("The ArrayStride decoration cannot be applied to an array "
737 "type which contains a structure type decorated Block "
739 /* Ignore the decoration */
741 vtn_fail_if(dec
->operands
[0] == 0, "ArrayStride must be non-zero");
742 type
->stride
= dec
->operands
[0];
748 struct_member_decoration_cb(struct vtn_builder
*b
,
749 struct vtn_value
*val
, int member
,
750 const struct vtn_decoration
*dec
, void *void_ctx
)
752 struct member_decoration_ctx
*ctx
= void_ctx
;
757 assert(member
< ctx
->num_fields
);
759 switch (dec
->decoration
) {
760 case SpvDecorationRelaxedPrecision
:
761 case SpvDecorationUniform
:
762 case SpvDecorationUniformId
:
763 break; /* FIXME: Do nothing with this for now. */
764 case SpvDecorationNonWritable
:
765 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_WRITEABLE
);
767 case SpvDecorationNonReadable
:
768 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_READABLE
);
770 case SpvDecorationVolatile
:
771 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_VOLATILE
);
773 case SpvDecorationCoherent
:
774 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_COHERENT
);
776 case SpvDecorationNoPerspective
:
777 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
779 case SpvDecorationFlat
:
780 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
782 case SpvDecorationCentroid
:
783 ctx
->fields
[member
].centroid
= true;
785 case SpvDecorationSample
:
786 ctx
->fields
[member
].sample
= true;
788 case SpvDecorationStream
:
789 /* Vulkan only allows one GS stream */
790 vtn_assert(dec
->operands
[0] == 0);
792 case SpvDecorationLocation
:
793 ctx
->fields
[member
].location
= dec
->operands
[0];
795 case SpvDecorationComponent
:
796 break; /* FIXME: What should we do with these? */
797 case SpvDecorationBuiltIn
:
798 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
799 ctx
->type
->members
[member
]->is_builtin
= true;
800 ctx
->type
->members
[member
]->builtin
= dec
->operands
[0];
801 ctx
->type
->builtin_block
= true;
803 case SpvDecorationOffset
:
804 ctx
->type
->offsets
[member
] = dec
->operands
[0];
805 ctx
->fields
[member
].offset
= dec
->operands
[0];
807 case SpvDecorationMatrixStride
:
808 /* Handled as a second pass */
810 case SpvDecorationColMajor
:
811 break; /* Nothing to do here. Column-major is the default. */
812 case SpvDecorationRowMajor
:
813 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
816 case SpvDecorationPatch
:
819 case SpvDecorationSpecId
:
820 case SpvDecorationBlock
:
821 case SpvDecorationBufferBlock
:
822 case SpvDecorationArrayStride
:
823 case SpvDecorationGLSLShared
:
824 case SpvDecorationGLSLPacked
:
825 case SpvDecorationInvariant
:
826 case SpvDecorationRestrict
:
827 case SpvDecorationAliased
:
828 case SpvDecorationConstant
:
829 case SpvDecorationIndex
:
830 case SpvDecorationBinding
:
831 case SpvDecorationDescriptorSet
:
832 case SpvDecorationLinkageAttributes
:
833 case SpvDecorationNoContraction
:
834 case SpvDecorationInputAttachmentIndex
:
835 vtn_warn("Decoration not allowed on struct members: %s",
836 spirv_decoration_to_string(dec
->decoration
));
839 case SpvDecorationXfbBuffer
:
840 case SpvDecorationXfbStride
:
841 vtn_warn("Vulkan does not have transform feedback");
844 case SpvDecorationCPacked
:
845 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
846 vtn_warn("Decoration only allowed for CL-style kernels: %s",
847 spirv_decoration_to_string(dec
->decoration
));
849 ctx
->type
->packed
= true;
852 case SpvDecorationSaturatedConversion
:
853 case SpvDecorationFuncParamAttr
:
854 case SpvDecorationFPRoundingMode
:
855 case SpvDecorationFPFastMathMode
:
856 case SpvDecorationAlignment
:
857 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
858 vtn_warn("Decoration only allowed for CL-style kernels: %s",
859 spirv_decoration_to_string(dec
->decoration
));
863 case SpvDecorationUserSemantic
:
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 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
, 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 SpvDecorationVolatile
:
988 case SpvDecorationCoherent
:
989 case SpvDecorationNonWritable
:
990 case SpvDecorationNonReadable
:
991 case SpvDecorationUniform
:
992 case SpvDecorationUniformId
:
993 case SpvDecorationLocation
:
994 case SpvDecorationComponent
:
995 case SpvDecorationOffset
:
996 case SpvDecorationXfbBuffer
:
997 case SpvDecorationXfbStride
:
998 case SpvDecorationUserSemantic
:
999 vtn_warn("Decoration only allowed for struct members: %s",
1000 spirv_decoration_to_string(dec
->decoration
));
1003 case SpvDecorationStream
:
1004 /* We don't need to do anything here, as stream is filled up when
1005 * aplying the decoration to a variable, just check that if it is not a
1006 * struct member, it should be a struct.
1008 vtn_assert(type
->base_type
== vtn_base_type_struct
);
1011 case SpvDecorationRelaxedPrecision
:
1012 case SpvDecorationSpecId
:
1013 case SpvDecorationInvariant
:
1014 case SpvDecorationRestrict
:
1015 case SpvDecorationAliased
:
1016 case SpvDecorationConstant
:
1017 case SpvDecorationIndex
:
1018 case SpvDecorationBinding
:
1019 case SpvDecorationDescriptorSet
:
1020 case SpvDecorationLinkageAttributes
:
1021 case SpvDecorationNoContraction
:
1022 case SpvDecorationInputAttachmentIndex
:
1023 vtn_warn("Decoration not allowed on types: %s",
1024 spirv_decoration_to_string(dec
->decoration
));
1027 case SpvDecorationCPacked
:
1028 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
1029 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1030 spirv_decoration_to_string(dec
->decoration
));
1032 type
->packed
= true;
1035 case SpvDecorationSaturatedConversion
:
1036 case SpvDecorationFuncParamAttr
:
1037 case SpvDecorationFPRoundingMode
:
1038 case SpvDecorationFPFastMathMode
:
1039 case SpvDecorationAlignment
:
1040 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1041 spirv_decoration_to_string(dec
->decoration
));
1045 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1050 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
1053 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
1054 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
1055 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
1056 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
1057 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
1058 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
1059 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
1060 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
1061 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
1062 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
1063 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
1064 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
1065 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
1066 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
1067 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
1068 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
1069 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
1070 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
1071 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
1072 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
1073 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
1074 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
1075 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
1076 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
1077 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
1078 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
1079 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
1080 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
1081 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
1082 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
1083 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
1084 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
1085 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
1086 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
1087 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
1088 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
1089 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
1090 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
1091 case SpvImageFormatR16ui
: return 0x8234; /* GL_R16UI */
1092 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
1094 vtn_fail("Invalid image format: %s (%u)",
1095 spirv_imageformat_to_string(format
), format
);
1100 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
1101 const uint32_t *w
, unsigned count
)
1103 struct vtn_value
*val
= NULL
;
1105 /* In order to properly handle forward declarations, we have to defer
1106 * allocation for pointer types.
1108 if (opcode
!= SpvOpTypePointer
&& opcode
!= SpvOpTypeForwardPointer
) {
1109 val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1110 vtn_fail_if(val
->type
!= NULL
,
1111 "Only pointers can have forward declarations");
1112 val
->type
= rzalloc(b
, struct vtn_type
);
1113 val
->type
->id
= w
[1];
1118 val
->type
->base_type
= vtn_base_type_void
;
1119 val
->type
->type
= glsl_void_type();
1122 val
->type
->base_type
= vtn_base_type_scalar
;
1123 val
->type
->type
= glsl_bool_type();
1124 val
->type
->length
= 1;
1126 case SpvOpTypeInt
: {
1127 int bit_size
= w
[2];
1128 const bool signedness
= w
[3];
1129 val
->type
->base_type
= vtn_base_type_scalar
;
1132 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1135 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1138 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1141 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1144 vtn_fail("Invalid int bit size: %u", bit_size
);
1146 val
->type
->length
= 1;
1150 case SpvOpTypeFloat
: {
1151 int bit_size
= w
[2];
1152 val
->type
->base_type
= vtn_base_type_scalar
;
1155 val
->type
->type
= glsl_float16_t_type();
1158 val
->type
->type
= glsl_float_type();
1161 val
->type
->type
= glsl_double_type();
1164 vtn_fail("Invalid float bit size: %u", bit_size
);
1166 val
->type
->length
= 1;
1170 case SpvOpTypeVector
: {
1171 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1172 unsigned elems
= w
[3];
1174 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1175 "Base type for OpTypeVector must be a scalar");
1176 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1177 "Invalid component count for OpTypeVector");
1179 val
->type
->base_type
= vtn_base_type_vector
;
1180 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1181 val
->type
->length
= elems
;
1182 val
->type
->stride
= glsl_type_is_boolean(val
->type
->type
)
1183 ? 4 : glsl_get_bit_size(base
->type
) / 8;
1184 val
->type
->array_element
= base
;
1188 case SpvOpTypeMatrix
: {
1189 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1190 unsigned columns
= w
[3];
1192 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1193 "Base type for OpTypeMatrix must be a vector");
1194 vtn_fail_if(columns
< 2 || columns
> 4,
1195 "Invalid column count for OpTypeMatrix");
1197 val
->type
->base_type
= vtn_base_type_matrix
;
1198 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1199 glsl_get_vector_elements(base
->type
),
1201 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1202 "Unsupported base type for OpTypeMatrix");
1203 assert(!glsl_type_is_error(val
->type
->type
));
1204 val
->type
->length
= columns
;
1205 val
->type
->array_element
= base
;
1206 val
->type
->row_major
= false;
1207 val
->type
->stride
= 0;
1211 case SpvOpTypeRuntimeArray
:
1212 case SpvOpTypeArray
: {
1213 struct vtn_type
*array_element
=
1214 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1216 if (opcode
== SpvOpTypeRuntimeArray
) {
1217 /* A length of 0 is used to denote unsized arrays */
1218 val
->type
->length
= 0;
1220 val
->type
->length
= vtn_constant_uint(b
, w
[3]);
1223 val
->type
->base_type
= vtn_base_type_array
;
1224 val
->type
->array_element
= array_element
;
1225 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
1226 val
->type
->stride
= glsl_get_cl_size(array_element
->type
);
1228 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1229 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
,
1234 case SpvOpTypeStruct
: {
1235 unsigned num_fields
= count
- 2;
1236 val
->type
->base_type
= vtn_base_type_struct
;
1237 val
->type
->length
= num_fields
;
1238 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1239 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1240 val
->type
->packed
= false;
1242 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1243 for (unsigned i
= 0; i
< num_fields
; i
++) {
1244 val
->type
->members
[i
] =
1245 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
1246 fields
[i
] = (struct glsl_struct_field
) {
1247 .type
= val
->type
->members
[i
]->type
,
1248 .name
= ralloc_asprintf(b
, "field%d", i
),
1254 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1255 unsigned offset
= 0;
1256 for (unsigned i
= 0; i
< num_fields
; i
++) {
1257 offset
= align(offset
, glsl_get_cl_alignment(fields
[i
].type
));
1258 fields
[i
].offset
= offset
;
1259 offset
+= glsl_get_cl_size(fields
[i
].type
);
1263 struct member_decoration_ctx ctx
= {
1264 .num_fields
= num_fields
,
1269 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1270 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1272 vtn_foreach_decoration(b
, val
, struct_block_decoration_cb
, NULL
);
1274 const char *name
= val
->name
;
1276 if (val
->type
->block
|| val
->type
->buffer_block
) {
1277 /* Packing will be ignored since types coming from SPIR-V are
1278 * explicitly laid out.
1280 val
->type
->type
= glsl_interface_type(fields
, num_fields
,
1281 /* packing */ 0, false,
1282 name
? name
: "block");
1284 val
->type
->type
= glsl_struct_type(fields
, num_fields
,
1285 name
? name
: "struct", false);
1290 case SpvOpTypeFunction
: {
1291 val
->type
->base_type
= vtn_base_type_function
;
1292 val
->type
->type
= NULL
;
1294 val
->type
->return_type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1296 const unsigned num_params
= count
- 3;
1297 val
->type
->length
= num_params
;
1298 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1299 for (unsigned i
= 0; i
< count
- 3; i
++) {
1300 val
->type
->params
[i
] =
1301 vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
;
1306 case SpvOpTypePointer
:
1307 case SpvOpTypeForwardPointer
: {
1308 /* We can't blindly push the value because it might be a forward
1311 val
= vtn_untyped_value(b
, w
[1]);
1313 SpvStorageClass storage_class
= w
[2];
1315 if (val
->value_type
== vtn_value_type_invalid
) {
1316 val
->value_type
= vtn_value_type_type
;
1317 val
->type
= rzalloc(b
, struct vtn_type
);
1318 val
->type
->id
= w
[1];
1319 val
->type
->base_type
= vtn_base_type_pointer
;
1320 val
->type
->storage_class
= storage_class
;
1322 /* These can actually be stored to nir_variables and used as SSA
1323 * values so they need a real glsl_type.
1325 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1326 b
, storage_class
, NULL
, NULL
);
1327 val
->type
->type
= nir_address_format_to_glsl_type(
1328 vtn_mode_to_address_format(b
, mode
));
1330 vtn_fail_if(val
->type
->storage_class
!= storage_class
,
1331 "The storage classes of an OpTypePointer and any "
1332 "OpTypeForwardPointers that provide forward "
1333 "declarations of it must match.");
1336 if (opcode
== SpvOpTypePointer
) {
1337 vtn_fail_if(val
->type
->deref
!= NULL
,
1338 "While OpTypeForwardPointer can be used to provide a "
1339 "forward declaration of a pointer, OpTypePointer can "
1340 "only be used once for a given id.");
1342 val
->type
->deref
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
1344 /* Only certain storage classes use ArrayStride. The others (in
1345 * particular Workgroup) are expected to be laid out by the driver.
1347 switch (storage_class
) {
1348 case SpvStorageClassUniform
:
1349 case SpvStorageClassPushConstant
:
1350 case SpvStorageClassStorageBuffer
:
1351 case SpvStorageClassPhysicalStorageBufferEXT
:
1352 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1355 /* Nothing to do. */
1359 if (b
->physical_ptrs
) {
1360 switch (storage_class
) {
1361 case SpvStorageClassFunction
:
1362 case SpvStorageClassWorkgroup
:
1363 case SpvStorageClassCrossWorkgroup
:
1364 case SpvStorageClassUniformConstant
:
1365 val
->type
->stride
= align(glsl_get_cl_size(val
->type
->deref
->type
),
1366 glsl_get_cl_alignment(val
->type
->deref
->type
));
1376 case SpvOpTypeImage
: {
1377 val
->type
->base_type
= vtn_base_type_image
;
1379 const struct vtn_type
*sampled_type
=
1380 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1382 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1383 glsl_get_bit_size(sampled_type
->type
) != 32,
1384 "Sampled type of OpTypeImage must be a 32-bit scalar");
1386 enum glsl_sampler_dim dim
;
1387 switch ((SpvDim
)w
[3]) {
1388 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1389 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1390 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1391 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1392 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1393 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1394 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1396 vtn_fail("Invalid SPIR-V image dimensionality: %s (%u)",
1397 spirv_dim_to_string((SpvDim
)w
[3]), w
[3]);
1400 /* w[4]: as per Vulkan spec "Validation Rules within a Module",
1401 * The “Depth” operand of OpTypeImage is ignored.
1403 bool is_array
= w
[5];
1404 bool multisampled
= w
[6];
1405 unsigned sampled
= w
[7];
1406 SpvImageFormat format
= w
[8];
1409 val
->type
->access_qualifier
= w
[9];
1411 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1414 if (dim
== GLSL_SAMPLER_DIM_2D
)
1415 dim
= GLSL_SAMPLER_DIM_MS
;
1416 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1417 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1419 vtn_fail("Unsupported multisampled image type");
1422 val
->type
->image_format
= translate_image_format(b
, format
);
1424 enum glsl_base_type sampled_base_type
=
1425 glsl_get_base_type(sampled_type
->type
);
1427 val
->type
->sampled
= true;
1428 val
->type
->type
= glsl_sampler_type(dim
, false, is_array
,
1430 } else if (sampled
== 2) {
1431 val
->type
->sampled
= false;
1432 val
->type
->type
= glsl_image_type(dim
, is_array
, sampled_base_type
);
1434 vtn_fail("We need to know if the image will be sampled");
1439 case SpvOpTypeSampledImage
:
1440 val
->type
->base_type
= vtn_base_type_sampled_image
;
1441 val
->type
->image
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1442 val
->type
->type
= val
->type
->image
->type
;
1445 case SpvOpTypeSampler
:
1446 /* The actual sampler type here doesn't really matter. It gets
1447 * thrown away the moment you combine it with an image. What really
1448 * matters is that it's a sampler type as opposed to an integer type
1449 * so the backend knows what to do.
1451 val
->type
->base_type
= vtn_base_type_sampler
;
1452 val
->type
->type
= glsl_bare_sampler_type();
1455 case SpvOpTypeOpaque
:
1456 case SpvOpTypeEvent
:
1457 case SpvOpTypeDeviceEvent
:
1458 case SpvOpTypeReserveId
:
1459 case SpvOpTypeQueue
:
1462 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1465 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1467 if (val
->type
->base_type
== vtn_base_type_struct
&&
1468 (val
->type
->block
|| val
->type
->buffer_block
)) {
1469 for (unsigned i
= 0; i
< val
->type
->length
; i
++) {
1470 vtn_fail_if(vtn_type_contains_block(b
, val
->type
->members
[i
]),
1471 "Block and BufferBlock decorations cannot decorate a "
1472 "structure type that is nested at any level inside "
1473 "another structure type decorated with Block or "
1479 static nir_constant
*
1480 vtn_null_constant(struct vtn_builder
*b
, struct vtn_type
*type
)
1482 nir_constant
*c
= rzalloc(b
, nir_constant
);
1484 switch (type
->base_type
) {
1485 case vtn_base_type_scalar
:
1486 case vtn_base_type_vector
:
1487 /* Nothing to do here. It's already initialized to zero */
1490 case vtn_base_type_pointer
: {
1491 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1492 b
, type
->storage_class
, type
->deref
, NULL
);
1493 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
1495 const nir_const_value
*null_value
= nir_address_format_null_value(addr_format
);
1496 memcpy(c
->values
, null_value
,
1497 sizeof(nir_const_value
) * nir_address_format_num_components(addr_format
));
1501 case vtn_base_type_void
:
1502 case vtn_base_type_image
:
1503 case vtn_base_type_sampler
:
1504 case vtn_base_type_sampled_image
:
1505 case vtn_base_type_function
:
1506 /* For those we have to return something but it doesn't matter what. */
1509 case vtn_base_type_matrix
:
1510 case vtn_base_type_array
:
1511 vtn_assert(type
->length
> 0);
1512 c
->num_elements
= type
->length
;
1513 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1515 c
->elements
[0] = vtn_null_constant(b
, type
->array_element
);
1516 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1517 c
->elements
[i
] = c
->elements
[0];
1520 case vtn_base_type_struct
:
1521 c
->num_elements
= type
->length
;
1522 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1523 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
1524 c
->elements
[i
] = vtn_null_constant(b
, type
->members
[i
]);
1528 vtn_fail("Invalid type for null constant");
1535 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
1536 int member
, const struct vtn_decoration
*dec
,
1539 vtn_assert(member
== -1);
1540 if (dec
->decoration
!= SpvDecorationSpecId
)
1543 struct spec_constant_value
*const_value
= data
;
1545 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1546 if (b
->specializations
[i
].id
== dec
->operands
[0]) {
1547 if (const_value
->is_double
)
1548 const_value
->data64
= b
->specializations
[i
].data64
;
1550 const_value
->data32
= b
->specializations
[i
].data32
;
1557 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
1558 uint32_t const_value
)
1560 struct spec_constant_value data
;
1561 data
.is_double
= false;
1562 data
.data32
= const_value
;
1563 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1568 get_specialization64(struct vtn_builder
*b
, struct vtn_value
*val
,
1569 uint64_t const_value
)
1571 struct spec_constant_value data
;
1572 data
.is_double
= true;
1573 data
.data64
= const_value
;
1574 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1579 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1580 struct vtn_value
*val
,
1582 const struct vtn_decoration
*dec
,
1585 vtn_assert(member
== -1);
1586 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1587 dec
->operands
[0] != SpvBuiltInWorkgroupSize
)
1590 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1591 b
->workgroup_size_builtin
= val
;
1595 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1596 const uint32_t *w
, unsigned count
)
1598 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1599 val
->constant
= rzalloc(b
, nir_constant
);
1601 case SpvOpConstantTrue
:
1602 case SpvOpConstantFalse
:
1603 case SpvOpSpecConstantTrue
:
1604 case SpvOpSpecConstantFalse
: {
1605 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1606 "Result type of %s must be OpTypeBool",
1607 spirv_op_to_string(opcode
));
1609 uint32_t int_val
= (opcode
== SpvOpConstantTrue
||
1610 opcode
== SpvOpSpecConstantTrue
);
1612 if (opcode
== SpvOpSpecConstantTrue
||
1613 opcode
== SpvOpSpecConstantFalse
)
1614 int_val
= get_specialization(b
, val
, int_val
);
1616 val
->constant
->values
[0].b
= int_val
!= 0;
1620 case SpvOpConstant
: {
1621 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1622 "Result type of %s must be a scalar",
1623 spirv_op_to_string(opcode
));
1624 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1627 val
->constant
->values
[0].u64
= vtn_u64_literal(&w
[3]);
1630 val
->constant
->values
[0].u32
= w
[3];
1633 val
->constant
->values
[0].u16
= w
[3];
1636 val
->constant
->values
[0].u8
= w
[3];
1639 vtn_fail("Unsupported SpvOpConstant bit size: %u", bit_size
);
1644 case SpvOpSpecConstant
: {
1645 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1646 "Result type of %s must be a scalar",
1647 spirv_op_to_string(opcode
));
1648 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1651 val
->constant
->values
[0].u64
=
1652 get_specialization64(b
, val
, vtn_u64_literal(&w
[3]));
1655 val
->constant
->values
[0].u32
= get_specialization(b
, val
, w
[3]);
1658 val
->constant
->values
[0].u16
= get_specialization(b
, val
, w
[3]);
1661 val
->constant
->values
[0].u8
= get_specialization(b
, val
, w
[3]);
1664 vtn_fail("Unsupported SpvOpSpecConstant bit size");
1669 case SpvOpSpecConstantComposite
:
1670 case SpvOpConstantComposite
: {
1671 unsigned elem_count
= count
- 3;
1672 vtn_fail_if(elem_count
!= val
->type
->length
,
1673 "%s has %u constituents, expected %u",
1674 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1676 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1677 for (unsigned i
= 0; i
< elem_count
; i
++) {
1678 struct vtn_value
*val
= vtn_untyped_value(b
, w
[i
+ 3]);
1680 if (val
->value_type
== vtn_value_type_constant
) {
1681 elems
[i
] = val
->constant
;
1683 vtn_fail_if(val
->value_type
!= vtn_value_type_undef
,
1684 "only constants or undefs allowed for "
1685 "SpvOpConstantComposite");
1686 /* to make it easier, just insert a NULL constant for now */
1687 elems
[i
] = vtn_null_constant(b
, val
->type
);
1691 switch (val
->type
->base_type
) {
1692 case vtn_base_type_vector
: {
1693 assert(glsl_type_is_vector(val
->type
->type
));
1694 for (unsigned i
= 0; i
< elem_count
; i
++)
1695 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1699 case vtn_base_type_matrix
:
1700 case vtn_base_type_struct
:
1701 case vtn_base_type_array
:
1702 ralloc_steal(val
->constant
, elems
);
1703 val
->constant
->num_elements
= elem_count
;
1704 val
->constant
->elements
= elems
;
1708 vtn_fail("Result type of %s must be a composite type",
1709 spirv_op_to_string(opcode
));
1714 case SpvOpSpecConstantOp
: {
1715 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1717 case SpvOpVectorShuffle
: {
1718 struct vtn_value
*v0
= &b
->values
[w
[4]];
1719 struct vtn_value
*v1
= &b
->values
[w
[5]];
1721 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1722 v0
->value_type
== vtn_value_type_undef
);
1723 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1724 v1
->value_type
== vtn_value_type_undef
);
1726 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1727 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1729 vtn_assert(len0
+ len1
< 16);
1731 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1732 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1733 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1735 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1736 (void)bit_size0
; (void)bit_size1
;
1738 nir_const_value undef
= { .u64
= 0xdeadbeefdeadbeef };
1739 nir_const_value combined
[NIR_MAX_VEC_COMPONENTS
* 2];
1741 if (v0
->value_type
== vtn_value_type_constant
) {
1742 for (unsigned i
= 0; i
< len0
; i
++)
1743 combined
[i
] = v0
->constant
->values
[i
];
1745 if (v1
->value_type
== vtn_value_type_constant
) {
1746 for (unsigned i
= 0; i
< len1
; i
++)
1747 combined
[len0
+ i
] = v1
->constant
->values
[i
];
1750 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1751 uint32_t comp
= w
[i
+ 6];
1752 if (comp
== (uint32_t)-1) {
1753 /* If component is not used, set the value to a known constant
1754 * to detect if it is wrongly used.
1756 val
->constant
->values
[j
] = undef
;
1758 vtn_fail_if(comp
>= len0
+ len1
,
1759 "All Component literals must either be FFFFFFFF "
1760 "or in [0, N - 1] (inclusive).");
1761 val
->constant
->values
[j
] = combined
[comp
];
1767 case SpvOpCompositeExtract
:
1768 case SpvOpCompositeInsert
: {
1769 struct vtn_value
*comp
;
1770 unsigned deref_start
;
1771 struct nir_constant
**c
;
1772 if (opcode
== SpvOpCompositeExtract
) {
1773 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1775 c
= &comp
->constant
;
1777 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1779 val
->constant
= nir_constant_clone(comp
->constant
,
1785 const struct vtn_type
*type
= comp
->type
;
1786 for (unsigned i
= deref_start
; i
< count
; i
++) {
1787 vtn_fail_if(w
[i
] > type
->length
,
1788 "%uth index of %s is %u but the type has only "
1789 "%u elements", i
- deref_start
,
1790 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1792 switch (type
->base_type
) {
1793 case vtn_base_type_vector
:
1795 type
= type
->array_element
;
1798 case vtn_base_type_matrix
:
1799 case vtn_base_type_array
:
1800 c
= &(*c
)->elements
[w
[i
]];
1801 type
= type
->array_element
;
1804 case vtn_base_type_struct
:
1805 c
= &(*c
)->elements
[w
[i
]];
1806 type
= type
->members
[w
[i
]];
1810 vtn_fail("%s must only index into composite types",
1811 spirv_op_to_string(opcode
));
1815 if (opcode
== SpvOpCompositeExtract
) {
1819 unsigned num_components
= type
->length
;
1820 for (unsigned i
= 0; i
< num_components
; i
++)
1821 val
->constant
->values
[i
] = (*c
)->values
[elem
+ i
];
1824 struct vtn_value
*insert
=
1825 vtn_value(b
, w
[4], vtn_value_type_constant
);
1826 vtn_assert(insert
->type
== type
);
1828 *c
= insert
->constant
;
1830 unsigned num_components
= type
->length
;
1831 for (unsigned i
= 0; i
< num_components
; i
++)
1832 (*c
)->values
[elem
+ i
] = insert
->constant
->values
[i
];
1840 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
1841 nir_alu_type src_alu_type
= dst_alu_type
;
1842 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
1845 vtn_assert(count
<= 7);
1851 /* We have a source in a conversion */
1853 nir_get_nir_type_for_glsl_type(
1854 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1855 /* We use the bitsize of the conversion source to evaluate the opcode later */
1856 bit_size
= glsl_get_bit_size(
1857 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1860 bit_size
= glsl_get_bit_size(val
->type
->type
);
1863 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
1864 nir_alu_type_get_type_size(src_alu_type
),
1865 nir_alu_type_get_type_size(dst_alu_type
));
1866 nir_const_value src
[3][NIR_MAX_VEC_COMPONENTS
];
1868 for (unsigned i
= 0; i
< count
- 4; i
++) {
1869 struct vtn_value
*src_val
=
1870 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
);
1872 /* If this is an unsized source, pull the bit size from the
1873 * source; otherwise, we'll use the bit size from the destination.
1875 if (!nir_alu_type_get_type_size(nir_op_infos
[op
].input_types
[i
]))
1876 bit_size
= glsl_get_bit_size(src_val
->type
->type
);
1878 unsigned src_comps
= nir_op_infos
[op
].input_sizes
[i
] ?
1879 nir_op_infos
[op
].input_sizes
[i
] :
1882 unsigned j
= swap
? 1 - i
: i
;
1883 for (unsigned c
= 0; c
< src_comps
; c
++)
1884 src
[j
][c
] = src_val
->constant
->values
[c
];
1887 /* fix up fixed size sources */
1894 for (unsigned i
= 0; i
< num_components
; ++i
) {
1896 case 64: src
[1][i
].u32
= src
[1][i
].u64
; break;
1897 case 16: src
[1][i
].u32
= src
[1][i
].u16
; break;
1898 case 8: src
[1][i
].u32
= src
[1][i
].u8
; break;
1907 nir_const_value
*srcs
[3] = {
1908 src
[0], src
[1], src
[2],
1910 nir_eval_const_opcode(op
, val
->constant
->values
,
1911 num_components
, bit_size
, srcs
,
1912 b
->shader
->info
.float_controls_execution_mode
);
1919 case SpvOpConstantNull
:
1920 val
->constant
= vtn_null_constant(b
, val
->type
);
1923 case SpvOpConstantSampler
:
1924 vtn_fail("OpConstantSampler requires Kernel Capability");
1928 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1931 /* Now that we have the value, update the workgroup size if needed */
1932 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1935 SpvMemorySemanticsMask
1936 vtn_storage_class_to_memory_semantics(SpvStorageClass sc
)
1939 case SpvStorageClassStorageBuffer
:
1940 case SpvStorageClassPhysicalStorageBufferEXT
:
1941 return SpvMemorySemanticsUniformMemoryMask
;
1942 case SpvStorageClassWorkgroup
:
1943 return SpvMemorySemanticsWorkgroupMemoryMask
;
1945 return SpvMemorySemanticsMaskNone
;
1950 vtn_split_barrier_semantics(struct vtn_builder
*b
,
1951 SpvMemorySemanticsMask semantics
,
1952 SpvMemorySemanticsMask
*before
,
1953 SpvMemorySemanticsMask
*after
)
1955 /* For memory semantics embedded in operations, we split them into up to
1956 * two barriers, to be added before and after the operation. This is less
1957 * strict than if we propagated until the final backend stage, but still
1958 * result in correct execution.
1960 * A further improvement could be pipe this information (and use!) into the
1961 * next compiler layers, at the expense of making the handling of barriers
1965 *before
= SpvMemorySemanticsMaskNone
;
1966 *after
= SpvMemorySemanticsMaskNone
;
1968 SpvMemorySemanticsMask order_semantics
=
1969 semantics
& (SpvMemorySemanticsAcquireMask
|
1970 SpvMemorySemanticsReleaseMask
|
1971 SpvMemorySemanticsAcquireReleaseMask
|
1972 SpvMemorySemanticsSequentiallyConsistentMask
);
1974 if (util_bitcount(order_semantics
) > 1) {
1975 /* Old GLSLang versions incorrectly set all the ordering bits. This was
1976 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
1977 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
1979 vtn_warn("Multiple memory ordering semantics specified, "
1980 "assuming AcquireRelease.");
1981 order_semantics
= SpvMemorySemanticsAcquireReleaseMask
;
1984 const SpvMemorySemanticsMask av_vis_semantics
=
1985 semantics
& (SpvMemorySemanticsMakeAvailableMask
|
1986 SpvMemorySemanticsMakeVisibleMask
);
1988 const SpvMemorySemanticsMask storage_semantics
=
1989 semantics
& (SpvMemorySemanticsUniformMemoryMask
|
1990 SpvMemorySemanticsSubgroupMemoryMask
|
1991 SpvMemorySemanticsWorkgroupMemoryMask
|
1992 SpvMemorySemanticsCrossWorkgroupMemoryMask
|
1993 SpvMemorySemanticsAtomicCounterMemoryMask
|
1994 SpvMemorySemanticsImageMemoryMask
|
1995 SpvMemorySemanticsOutputMemoryMask
);
1997 const SpvMemorySemanticsMask other_semantics
=
1998 semantics
& ~(order_semantics
| av_vis_semantics
| storage_semantics
);
2000 if (other_semantics
)
2001 vtn_warn("Ignoring unhandled memory semantics: %u\n", other_semantics
);
2003 /* SequentiallyConsistent is treated as AcquireRelease. */
2005 /* The RELEASE barrier happens BEFORE the operation, and it is usually
2006 * associated with a Store. All the write operations with a matching
2007 * semantics will not be reordered after the Store.
2009 if (order_semantics
& (SpvMemorySemanticsReleaseMask
|
2010 SpvMemorySemanticsAcquireReleaseMask
|
2011 SpvMemorySemanticsSequentiallyConsistentMask
)) {
2012 *before
|= SpvMemorySemanticsReleaseMask
| storage_semantics
;
2015 /* The ACQUIRE barrier happens AFTER the operation, and it is usually
2016 * associated with a Load. All the operations with a matching semantics
2017 * will not be reordered before the Load.
2019 if (order_semantics
& (SpvMemorySemanticsAcquireMask
|
2020 SpvMemorySemanticsAcquireReleaseMask
|
2021 SpvMemorySemanticsSequentiallyConsistentMask
)) {
2022 *after
|= SpvMemorySemanticsAcquireMask
| storage_semantics
;
2025 if (av_vis_semantics
& SpvMemorySemanticsMakeVisibleMask
)
2026 *before
|= SpvMemorySemanticsMakeVisibleMask
| storage_semantics
;
2028 if (av_vis_semantics
& SpvMemorySemanticsMakeAvailableMask
)
2029 *after
|= SpvMemorySemanticsMakeAvailableMask
| storage_semantics
;
2033 vtn_emit_scoped_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
2034 SpvMemorySemanticsMask semantics
)
2036 nir_memory_semantics nir_semantics
= 0;
2038 SpvMemorySemanticsMask order_semantics
=
2039 semantics
& (SpvMemorySemanticsAcquireMask
|
2040 SpvMemorySemanticsReleaseMask
|
2041 SpvMemorySemanticsAcquireReleaseMask
|
2042 SpvMemorySemanticsSequentiallyConsistentMask
);
2044 if (util_bitcount(order_semantics
) > 1) {
2045 /* Old GLSLang versions incorrectly set all the ordering bits. This was
2046 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
2047 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
2049 vtn_warn("Multiple memory ordering semantics bits specified, "
2050 "assuming AcquireRelease.");
2051 order_semantics
= SpvMemorySemanticsAcquireReleaseMask
;
2054 switch (order_semantics
) {
2056 /* Not an ordering barrier. */
2059 case SpvMemorySemanticsAcquireMask
:
2060 nir_semantics
= NIR_MEMORY_ACQUIRE
;
2063 case SpvMemorySemanticsReleaseMask
:
2064 nir_semantics
= NIR_MEMORY_RELEASE
;
2067 case SpvMemorySemanticsSequentiallyConsistentMask
:
2068 /* Fall through. Treated as AcquireRelease in Vulkan. */
2069 case SpvMemorySemanticsAcquireReleaseMask
:
2070 nir_semantics
= NIR_MEMORY_ACQUIRE
| NIR_MEMORY_RELEASE
;
2074 unreachable("Invalid memory order semantics");
2077 if (semantics
& SpvMemorySemanticsMakeAvailableMask
) {
2078 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2079 "To use MakeAvailable memory semantics the VulkanMemoryModel "
2080 "capability must be declared.");
2081 nir_semantics
|= NIR_MEMORY_MAKE_AVAILABLE
;
2084 if (semantics
& SpvMemorySemanticsMakeVisibleMask
) {
2085 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2086 "To use MakeVisible memory semantics the VulkanMemoryModel "
2087 "capability must be declared.");
2088 nir_semantics
|= NIR_MEMORY_MAKE_VISIBLE
;
2091 /* Vulkan Environment for SPIR-V says "SubgroupMemory, CrossWorkgroupMemory,
2092 * and AtomicCounterMemory are ignored".
2094 semantics
&= ~(SpvMemorySemanticsSubgroupMemoryMask
|
2095 SpvMemorySemanticsCrossWorkgroupMemoryMask
|
2096 SpvMemorySemanticsAtomicCounterMemoryMask
);
2098 /* TODO: Consider adding nir_var_mem_image mode to NIR so it can be used
2099 * for SpvMemorySemanticsImageMemoryMask.
2102 nir_variable_mode modes
= 0;
2103 if (semantics
& (SpvMemorySemanticsUniformMemoryMask
|
2104 SpvMemorySemanticsImageMemoryMask
))
2105 modes
|= nir_var_mem_ubo
| nir_var_mem_ssbo
| nir_var_uniform
;
2106 if (semantics
& SpvMemorySemanticsWorkgroupMemoryMask
)
2107 modes
|= nir_var_mem_shared
;
2108 if (semantics
& SpvMemorySemanticsOutputMemoryMask
) {
2109 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2110 "To use Output memory semantics, the VulkanMemoryModel "
2111 "capability must be declared.");
2112 modes
|= nir_var_shader_out
;
2115 /* No barrier to add. */
2116 if (nir_semantics
== 0 || modes
== 0)
2119 nir_scope nir_scope
;
2121 case SpvScopeDevice
:
2122 vtn_fail_if(b
->options
->caps
.vk_memory_model
&&
2123 !b
->options
->caps
.vk_memory_model_device_scope
,
2124 "If the Vulkan memory model is declared and any instruction "
2125 "uses Device scope, the VulkanMemoryModelDeviceScope "
2126 "capability must be declared.");
2127 nir_scope
= NIR_SCOPE_DEVICE
;
2130 case SpvScopeQueueFamily
:
2131 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2132 "To use Queue Family scope, the VulkanMemoryModel capability "
2133 "must be declared.");
2134 nir_scope
= NIR_SCOPE_QUEUE_FAMILY
;
2137 case SpvScopeWorkgroup
:
2138 nir_scope
= NIR_SCOPE_WORKGROUP
;
2141 case SpvScopeSubgroup
:
2142 nir_scope
= NIR_SCOPE_SUBGROUP
;
2145 case SpvScopeInvocation
:
2146 nir_scope
= NIR_SCOPE_INVOCATION
;
2150 vtn_fail("Invalid memory scope");
2153 nir_intrinsic_instr
*intrin
=
2154 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_scoped_memory_barrier
);
2155 nir_intrinsic_set_memory_semantics(intrin
, nir_semantics
);
2157 nir_intrinsic_set_memory_modes(intrin
, modes
);
2158 nir_intrinsic_set_memory_scope(intrin
, nir_scope
);
2159 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2162 struct vtn_ssa_value
*
2163 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
2165 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
2168 if (!glsl_type_is_vector_or_scalar(type
)) {
2169 unsigned elems
= glsl_get_length(type
);
2170 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2171 for (unsigned i
= 0; i
< elems
; i
++) {
2172 const struct glsl_type
*child_type
;
2174 switch (glsl_get_base_type(type
)) {
2176 case GLSL_TYPE_UINT
:
2177 case GLSL_TYPE_INT16
:
2178 case GLSL_TYPE_UINT16
:
2179 case GLSL_TYPE_UINT8
:
2180 case GLSL_TYPE_INT8
:
2181 case GLSL_TYPE_INT64
:
2182 case GLSL_TYPE_UINT64
:
2183 case GLSL_TYPE_BOOL
:
2184 case GLSL_TYPE_FLOAT
:
2185 case GLSL_TYPE_FLOAT16
:
2186 case GLSL_TYPE_DOUBLE
:
2187 child_type
= glsl_get_column_type(type
);
2189 case GLSL_TYPE_ARRAY
:
2190 child_type
= glsl_get_array_element(type
);
2192 case GLSL_TYPE_STRUCT
:
2193 case GLSL_TYPE_INTERFACE
:
2194 child_type
= glsl_get_struct_field(type
, i
);
2197 vtn_fail("unkown base type");
2200 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
2208 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
2211 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
2212 src
.src_type
= type
;
2217 image_operand_arg(struct vtn_builder
*b
, const uint32_t *w
, uint32_t count
,
2218 uint32_t mask_idx
, SpvImageOperandsMask op
)
2220 static const SpvImageOperandsMask ops_with_arg
=
2221 SpvImageOperandsBiasMask
|
2222 SpvImageOperandsLodMask
|
2223 SpvImageOperandsGradMask
|
2224 SpvImageOperandsConstOffsetMask
|
2225 SpvImageOperandsOffsetMask
|
2226 SpvImageOperandsConstOffsetsMask
|
2227 SpvImageOperandsSampleMask
|
2228 SpvImageOperandsMinLodMask
|
2229 SpvImageOperandsMakeTexelAvailableMask
|
2230 SpvImageOperandsMakeTexelVisibleMask
;
2232 assert(util_bitcount(op
) == 1);
2233 assert(w
[mask_idx
] & op
);
2234 assert(op
& ops_with_arg
);
2236 uint32_t idx
= util_bitcount(w
[mask_idx
] & (op
- 1) & ops_with_arg
) + 1;
2238 /* Adjust indices for operands with two arguments. */
2239 static const SpvImageOperandsMask ops_with_two_args
=
2240 SpvImageOperandsGradMask
;
2241 idx
+= util_bitcount(w
[mask_idx
] & (op
- 1) & ops_with_two_args
);
2245 vtn_fail_if(idx
+ (op
& ops_with_two_args
? 1 : 0) >= count
,
2246 "Image op claims to have %s but does not enough "
2247 "following operands", spirv_imageoperands_to_string(op
));
2253 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
2254 const uint32_t *w
, unsigned count
)
2256 if (opcode
== SpvOpSampledImage
) {
2257 struct vtn_value
*val
=
2258 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2259 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2260 val
->sampled_image
->image
=
2261 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2262 val
->sampled_image
->sampler
=
2263 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
2265 } else if (opcode
== SpvOpImage
) {
2266 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
2267 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
2268 vtn_push_value_pointer(b
, w
[2], src_val
->sampled_image
->image
);
2270 vtn_assert(src_val
->value_type
== vtn_value_type_pointer
);
2271 vtn_push_value_pointer(b
, w
[2], src_val
->pointer
);
2276 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2278 struct vtn_pointer
*image
= NULL
, *sampler
= NULL
;
2279 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
2280 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
2281 image
= sampled_val
->sampled_image
->image
;
2282 sampler
= sampled_val
->sampled_image
->sampler
;
2284 vtn_assert(sampled_val
->value_type
== vtn_value_type_pointer
);
2285 image
= sampled_val
->pointer
;
2288 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
);
2289 nir_deref_instr
*sampler_deref
=
2290 sampler
? vtn_pointer_to_deref(b
, sampler
) : NULL
;
2292 const struct glsl_type
*image_type
= sampled_val
->type
->type
;
2293 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
2294 const bool is_array
= glsl_sampler_type_is_array(image_type
);
2295 nir_alu_type dest_type
= nir_type_invalid
;
2297 /* Figure out the base texture operation */
2300 case SpvOpImageSampleImplicitLod
:
2301 case SpvOpImageSampleDrefImplicitLod
:
2302 case SpvOpImageSampleProjImplicitLod
:
2303 case SpvOpImageSampleProjDrefImplicitLod
:
2304 texop
= nir_texop_tex
;
2307 case SpvOpImageSampleExplicitLod
:
2308 case SpvOpImageSampleDrefExplicitLod
:
2309 case SpvOpImageSampleProjExplicitLod
:
2310 case SpvOpImageSampleProjDrefExplicitLod
:
2311 texop
= nir_texop_txl
;
2314 case SpvOpImageFetch
:
2315 if (sampler_dim
== GLSL_SAMPLER_DIM_MS
) {
2316 texop
= nir_texop_txf_ms
;
2318 texop
= nir_texop_txf
;
2322 case SpvOpImageGather
:
2323 case SpvOpImageDrefGather
:
2324 texop
= nir_texop_tg4
;
2327 case SpvOpImageQuerySizeLod
:
2328 case SpvOpImageQuerySize
:
2329 texop
= nir_texop_txs
;
2330 dest_type
= nir_type_int
;
2333 case SpvOpImageQueryLod
:
2334 texop
= nir_texop_lod
;
2335 dest_type
= nir_type_float
;
2338 case SpvOpImageQueryLevels
:
2339 texop
= nir_texop_query_levels
;
2340 dest_type
= nir_type_int
;
2343 case SpvOpImageQuerySamples
:
2344 texop
= nir_texop_texture_samples
;
2345 dest_type
= nir_type_int
;
2349 vtn_fail_with_opcode("Unhandled opcode", opcode
);
2352 nir_tex_src srcs
[10]; /* 10 should be enough */
2353 nir_tex_src
*p
= srcs
;
2355 p
->src
= nir_src_for_ssa(&image_deref
->dest
.ssa
);
2356 p
->src_type
= nir_tex_src_texture_deref
;
2366 vtn_fail_if(sampler
== NULL
,
2367 "%s requires an image of type OpTypeSampledImage",
2368 spirv_op_to_string(opcode
));
2369 p
->src
= nir_src_for_ssa(&sampler_deref
->dest
.ssa
);
2370 p
->src_type
= nir_tex_src_sampler_deref
;
2374 case nir_texop_txf_ms
:
2376 case nir_texop_query_levels
:
2377 case nir_texop_texture_samples
:
2378 case nir_texop_samples_identical
:
2381 case nir_texop_txf_ms_fb
:
2382 vtn_fail("unexpected nir_texop_txf_ms_fb");
2384 case nir_texop_txf_ms_mcs
:
2385 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2386 case nir_texop_tex_prefetch
:
2387 vtn_fail("unexpected nir_texop_tex_prefetch");
2392 struct nir_ssa_def
*coord
;
2393 unsigned coord_components
;
2395 case SpvOpImageSampleImplicitLod
:
2396 case SpvOpImageSampleExplicitLod
:
2397 case SpvOpImageSampleDrefImplicitLod
:
2398 case SpvOpImageSampleDrefExplicitLod
:
2399 case SpvOpImageSampleProjImplicitLod
:
2400 case SpvOpImageSampleProjExplicitLod
:
2401 case SpvOpImageSampleProjDrefImplicitLod
:
2402 case SpvOpImageSampleProjDrefExplicitLod
:
2403 case SpvOpImageFetch
:
2404 case SpvOpImageGather
:
2405 case SpvOpImageDrefGather
:
2406 case SpvOpImageQueryLod
: {
2407 /* All these types have the coordinate as their first real argument */
2408 switch (sampler_dim
) {
2409 case GLSL_SAMPLER_DIM_1D
:
2410 case GLSL_SAMPLER_DIM_BUF
:
2411 coord_components
= 1;
2413 case GLSL_SAMPLER_DIM_2D
:
2414 case GLSL_SAMPLER_DIM_RECT
:
2415 case GLSL_SAMPLER_DIM_MS
:
2416 coord_components
= 2;
2418 case GLSL_SAMPLER_DIM_3D
:
2419 case GLSL_SAMPLER_DIM_CUBE
:
2420 coord_components
= 3;
2423 vtn_fail("Invalid sampler type");
2426 if (is_array
&& texop
!= nir_texop_lod
)
2429 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
2430 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2431 (1 << coord_components
) - 1));
2432 p
->src_type
= nir_tex_src_coord
;
2439 coord_components
= 0;
2444 case SpvOpImageSampleProjImplicitLod
:
2445 case SpvOpImageSampleProjExplicitLod
:
2446 case SpvOpImageSampleProjDrefImplicitLod
:
2447 case SpvOpImageSampleProjDrefExplicitLod
:
2448 /* These have the projector as the last coordinate component */
2449 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2450 p
->src_type
= nir_tex_src_projector
;
2458 bool is_shadow
= false;
2459 unsigned gather_component
= 0;
2461 case SpvOpImageSampleDrefImplicitLod
:
2462 case SpvOpImageSampleDrefExplicitLod
:
2463 case SpvOpImageSampleProjDrefImplicitLod
:
2464 case SpvOpImageSampleProjDrefExplicitLod
:
2465 case SpvOpImageDrefGather
:
2466 /* These all have an explicit depth value as their next source */
2468 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2471 case SpvOpImageGather
:
2472 /* This has a component as its next source */
2473 gather_component
= vtn_constant_uint(b
, w
[idx
++]);
2480 /* For OpImageQuerySizeLod, we always have an LOD */
2481 if (opcode
== SpvOpImageQuerySizeLod
)
2482 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2484 /* Now we need to handle some number of optional arguments */
2485 struct vtn_value
*gather_offsets
= NULL
;
2487 uint32_t operands
= w
[idx
];
2489 if (operands
& SpvImageOperandsBiasMask
) {
2490 vtn_assert(texop
== nir_texop_tex
);
2491 texop
= nir_texop_txb
;
2492 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2493 SpvImageOperandsBiasMask
);
2494 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_bias
);
2497 if (operands
& SpvImageOperandsLodMask
) {
2498 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2499 texop
== nir_texop_txs
);
2500 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2501 SpvImageOperandsLodMask
);
2502 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_lod
);
2505 if (operands
& SpvImageOperandsGradMask
) {
2506 vtn_assert(texop
== nir_texop_txl
);
2507 texop
= nir_texop_txd
;
2508 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2509 SpvImageOperandsGradMask
);
2510 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_ddx
);
2511 (*p
++) = vtn_tex_src(b
, w
[arg
+ 1], nir_tex_src_ddy
);
2514 vtn_fail_if(util_bitcount(operands
& (SpvImageOperandsConstOffsetsMask
|
2515 SpvImageOperandsOffsetMask
|
2516 SpvImageOperandsConstOffsetMask
)) > 1,
2517 "At most one of the ConstOffset, Offset, and ConstOffsets "
2518 "image operands can be used on a given instruction.");
2520 if (operands
& SpvImageOperandsOffsetMask
) {
2521 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2522 SpvImageOperandsOffsetMask
);
2523 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_offset
);
2526 if (operands
& SpvImageOperandsConstOffsetMask
) {
2527 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2528 SpvImageOperandsConstOffsetMask
);
2529 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_offset
);
2532 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2533 vtn_assert(texop
== nir_texop_tg4
);
2534 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2535 SpvImageOperandsConstOffsetsMask
);
2536 gather_offsets
= vtn_value(b
, w
[arg
], vtn_value_type_constant
);
2539 if (operands
& SpvImageOperandsSampleMask
) {
2540 vtn_assert(texop
== nir_texop_txf_ms
);
2541 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2542 SpvImageOperandsSampleMask
);
2543 texop
= nir_texop_txf_ms
;
2544 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_ms_index
);
2547 if (operands
& SpvImageOperandsMinLodMask
) {
2548 vtn_assert(texop
== nir_texop_tex
||
2549 texop
== nir_texop_txb
||
2550 texop
== nir_texop_txd
);
2551 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2552 SpvImageOperandsMinLodMask
);
2553 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_min_lod
);
2557 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2560 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2562 instr
->coord_components
= coord_components
;
2563 instr
->sampler_dim
= sampler_dim
;
2564 instr
->is_array
= is_array
;
2565 instr
->is_shadow
= is_shadow
;
2566 instr
->is_new_style_shadow
=
2567 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2568 instr
->component
= gather_component
;
2570 if (image
&& (image
->access
& ACCESS_NON_UNIFORM
))
2571 instr
->texture_non_uniform
= true;
2573 if (sampler
&& (sampler
->access
& ACCESS_NON_UNIFORM
))
2574 instr
->sampler_non_uniform
= true;
2576 /* for non-query ops, get dest_type from sampler type */
2577 if (dest_type
== nir_type_invalid
) {
2578 switch (glsl_get_sampler_result_type(image_type
)) {
2579 case GLSL_TYPE_FLOAT
: dest_type
= nir_type_float
; break;
2580 case GLSL_TYPE_INT
: dest_type
= nir_type_int
; break;
2581 case GLSL_TYPE_UINT
: dest_type
= nir_type_uint
; break;
2582 case GLSL_TYPE_BOOL
: dest_type
= nir_type_bool
; break;
2584 vtn_fail("Invalid base type for sampler result");
2588 instr
->dest_type
= dest_type
;
2590 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2591 nir_tex_instr_dest_size(instr
), 32, NULL
);
2593 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2594 nir_tex_instr_dest_size(instr
));
2596 if (gather_offsets
) {
2597 vtn_fail_if(gather_offsets
->type
->base_type
!= vtn_base_type_array
||
2598 gather_offsets
->type
->length
!= 4,
2599 "ConstOffsets must be an array of size four of vectors "
2600 "of two integer components");
2602 struct vtn_type
*vec_type
= gather_offsets
->type
->array_element
;
2603 vtn_fail_if(vec_type
->base_type
!= vtn_base_type_vector
||
2604 vec_type
->length
!= 2 ||
2605 !glsl_type_is_integer(vec_type
->type
),
2606 "ConstOffsets must be an array of size four of vectors "
2607 "of two integer components");
2609 unsigned bit_size
= glsl_get_bit_size(vec_type
->type
);
2610 for (uint32_t i
= 0; i
< 4; i
++) {
2611 const nir_const_value
*cvec
=
2612 gather_offsets
->constant
->elements
[i
]->values
;
2613 for (uint32_t j
= 0; j
< 2; j
++) {
2615 case 8: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i8
; break;
2616 case 16: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i16
; break;
2617 case 32: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i32
; break;
2618 case 64: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i64
; break;
2620 vtn_fail("Unsupported bit size: %u", bit_size
);
2626 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
2627 ssa
->def
= &instr
->dest
.ssa
;
2628 vtn_push_ssa(b
, w
[2], ret_type
, ssa
);
2630 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2634 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2635 const uint32_t *w
, nir_src
*src
)
2638 case SpvOpAtomicIIncrement
:
2639 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
2642 case SpvOpAtomicIDecrement
:
2643 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
2646 case SpvOpAtomicISub
:
2648 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
2651 case SpvOpAtomicCompareExchange
:
2652 case SpvOpAtomicCompareExchangeWeak
:
2653 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
2654 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
2657 case SpvOpAtomicExchange
:
2658 case SpvOpAtomicIAdd
:
2659 case SpvOpAtomicSMin
:
2660 case SpvOpAtomicUMin
:
2661 case SpvOpAtomicSMax
:
2662 case SpvOpAtomicUMax
:
2663 case SpvOpAtomicAnd
:
2665 case SpvOpAtomicXor
:
2666 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
2670 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2674 static nir_ssa_def
*
2675 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2677 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
2679 /* The image_load_store intrinsics assume a 4-dim coordinate */
2680 unsigned dim
= glsl_get_vector_elements(coord
->type
);
2681 unsigned swizzle
[4];
2682 for (unsigned i
= 0; i
< 4; i
++)
2683 swizzle
[i
] = MIN2(i
, dim
- 1);
2685 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4);
2688 static nir_ssa_def
*
2689 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2691 if (value
->num_components
== 4)
2695 for (unsigned i
= 0; i
< 4; i
++)
2696 swiz
[i
] = i
< value
->num_components
? i
: 0;
2697 return nir_swizzle(b
, value
, swiz
, 4);
2701 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2702 const uint32_t *w
, unsigned count
)
2704 /* Just get this one out of the way */
2705 if (opcode
== SpvOpImageTexelPointer
) {
2706 struct vtn_value
*val
=
2707 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2708 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2710 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2711 val
->image
->coord
= get_image_coord(b
, w
[4]);
2712 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
2716 struct vtn_image_pointer image
;
2717 SpvScope scope
= SpvScopeInvocation
;
2718 SpvMemorySemanticsMask semantics
= 0;
2721 case SpvOpAtomicExchange
:
2722 case SpvOpAtomicCompareExchange
:
2723 case SpvOpAtomicCompareExchangeWeak
:
2724 case SpvOpAtomicIIncrement
:
2725 case SpvOpAtomicIDecrement
:
2726 case SpvOpAtomicIAdd
:
2727 case SpvOpAtomicISub
:
2728 case SpvOpAtomicLoad
:
2729 case SpvOpAtomicSMin
:
2730 case SpvOpAtomicUMin
:
2731 case SpvOpAtomicSMax
:
2732 case SpvOpAtomicUMax
:
2733 case SpvOpAtomicAnd
:
2735 case SpvOpAtomicXor
:
2736 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
2737 scope
= vtn_constant_uint(b
, w
[4]);
2738 semantics
= vtn_constant_uint(b
, w
[5]);
2741 case SpvOpAtomicStore
:
2742 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
2743 scope
= vtn_constant_uint(b
, w
[2]);
2744 semantics
= vtn_constant_uint(b
, w
[3]);
2747 case SpvOpImageQuerySize
:
2748 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2750 image
.sample
= NULL
;
2753 case SpvOpImageRead
: {
2754 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2755 image
.coord
= get_image_coord(b
, w
[4]);
2757 const SpvImageOperandsMask operands
=
2758 count
> 5 ? w
[5] : SpvImageOperandsMaskNone
;
2760 if (operands
& SpvImageOperandsSampleMask
) {
2761 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
2762 SpvImageOperandsSampleMask
);
2763 image
.sample
= vtn_ssa_value(b
, w
[arg
])->def
;
2765 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2768 if (operands
& SpvImageOperandsMakeTexelVisibleMask
) {
2769 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
2770 "MakeTexelVisible requires NonPrivateTexel to also be set.");
2771 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
2772 SpvImageOperandsMakeTexelVisibleMask
);
2773 semantics
= SpvMemorySemanticsMakeVisibleMask
;
2774 scope
= vtn_constant_uint(b
, w
[arg
]);
2777 /* TODO: Volatile. */
2782 case SpvOpImageWrite
: {
2783 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2784 image
.coord
= get_image_coord(b
, w
[2]);
2788 const SpvImageOperandsMask operands
=
2789 count
> 4 ? w
[4] : SpvImageOperandsMaskNone
;
2791 if (operands
& SpvImageOperandsSampleMask
) {
2792 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
2793 SpvImageOperandsSampleMask
);
2794 image
.sample
= vtn_ssa_value(b
, w
[arg
])->def
;
2796 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2799 if (operands
& SpvImageOperandsMakeTexelAvailableMask
) {
2800 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
2801 "MakeTexelAvailable requires NonPrivateTexel to also be set.");
2802 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
2803 SpvImageOperandsMakeTexelAvailableMask
);
2804 semantics
= SpvMemorySemanticsMakeAvailableMask
;
2805 scope
= vtn_constant_uint(b
, w
[arg
]);
2808 /* TODO: Volatile. */
2814 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2817 nir_intrinsic_op op
;
2819 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
2820 OP(ImageQuerySize
, size
)
2822 OP(ImageWrite
, store
)
2823 OP(AtomicLoad
, load
)
2824 OP(AtomicStore
, store
)
2825 OP(AtomicExchange
, atomic_exchange
)
2826 OP(AtomicCompareExchange
, atomic_comp_swap
)
2827 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2828 OP(AtomicIIncrement
, atomic_add
)
2829 OP(AtomicIDecrement
, atomic_add
)
2830 OP(AtomicIAdd
, atomic_add
)
2831 OP(AtomicISub
, atomic_add
)
2832 OP(AtomicSMin
, atomic_imin
)
2833 OP(AtomicUMin
, atomic_umin
)
2834 OP(AtomicSMax
, atomic_imax
)
2835 OP(AtomicUMax
, atomic_umax
)
2836 OP(AtomicAnd
, atomic_and
)
2837 OP(AtomicOr
, atomic_or
)
2838 OP(AtomicXor
, atomic_xor
)
2841 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2844 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
2846 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
2847 intrin
->src
[0] = nir_src_for_ssa(&image_deref
->dest
.ssa
);
2849 /* ImageQuerySize doesn't take any extra parameters */
2850 if (opcode
!= SpvOpImageQuerySize
) {
2851 /* The image coordinate is always 4 components but we may not have that
2852 * many. Swizzle to compensate.
2854 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
2855 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
2858 nir_intrinsic_set_access(intrin
, image
.image
->access
);
2861 case SpvOpAtomicLoad
:
2862 case SpvOpImageQuerySize
:
2863 case SpvOpImageRead
:
2865 case SpvOpAtomicStore
:
2866 case SpvOpImageWrite
: {
2867 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
2868 nir_ssa_def
*value
= vtn_ssa_value(b
, value_id
)->def
;
2869 /* nir_intrinsic_image_deref_store always takes a vec4 value */
2870 assert(op
== nir_intrinsic_image_deref_store
);
2871 intrin
->num_components
= 4;
2872 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
));
2876 case SpvOpAtomicCompareExchange
:
2877 case SpvOpAtomicCompareExchangeWeak
:
2878 case SpvOpAtomicIIncrement
:
2879 case SpvOpAtomicIDecrement
:
2880 case SpvOpAtomicExchange
:
2881 case SpvOpAtomicIAdd
:
2882 case SpvOpAtomicISub
:
2883 case SpvOpAtomicSMin
:
2884 case SpvOpAtomicUMin
:
2885 case SpvOpAtomicSMax
:
2886 case SpvOpAtomicUMax
:
2887 case SpvOpAtomicAnd
:
2889 case SpvOpAtomicXor
:
2890 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
2894 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2897 /* Image operations implicitly have the Image storage memory semantics. */
2898 semantics
|= SpvMemorySemanticsImageMemoryMask
;
2900 SpvMemorySemanticsMask before_semantics
;
2901 SpvMemorySemanticsMask after_semantics
;
2902 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
2904 if (before_semantics
)
2905 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
2907 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
2908 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2910 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
2911 intrin
->num_components
= nir_intrinsic_infos
[op
].dest_components
;
2912 if (intrin
->num_components
== 0)
2913 intrin
->num_components
= dest_components
;
2915 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2916 intrin
->num_components
, 32, NULL
);
2918 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2920 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
2921 if (intrin
->num_components
!= dest_components
)
2922 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
2924 struct vtn_value
*val
=
2925 vtn_push_ssa(b
, w
[2], type
, vtn_create_ssa_value(b
, type
->type
));
2926 val
->ssa
->def
= result
;
2928 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2931 if (after_semantics
)
2932 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
2935 static nir_intrinsic_op
2936 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2939 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2940 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2941 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2942 OP(AtomicExchange
, atomic_exchange
)
2943 OP(AtomicCompareExchange
, atomic_comp_swap
)
2944 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2945 OP(AtomicIIncrement
, atomic_add
)
2946 OP(AtomicIDecrement
, atomic_add
)
2947 OP(AtomicIAdd
, atomic_add
)
2948 OP(AtomicISub
, atomic_add
)
2949 OP(AtomicSMin
, atomic_imin
)
2950 OP(AtomicUMin
, atomic_umin
)
2951 OP(AtomicSMax
, atomic_imax
)
2952 OP(AtomicUMax
, atomic_umax
)
2953 OP(AtomicAnd
, atomic_and
)
2954 OP(AtomicOr
, atomic_or
)
2955 OP(AtomicXor
, atomic_xor
)
2958 vtn_fail_with_opcode("Invalid SSBO atomic", opcode
);
2962 static nir_intrinsic_op
2963 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2966 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
2967 OP(AtomicLoad
, read_deref
)
2968 OP(AtomicExchange
, exchange
)
2969 OP(AtomicCompareExchange
, comp_swap
)
2970 OP(AtomicCompareExchangeWeak
, comp_swap
)
2971 OP(AtomicIIncrement
, inc_deref
)
2972 OP(AtomicIDecrement
, post_dec_deref
)
2973 OP(AtomicIAdd
, add_deref
)
2974 OP(AtomicISub
, add_deref
)
2975 OP(AtomicUMin
, min_deref
)
2976 OP(AtomicUMax
, max_deref
)
2977 OP(AtomicAnd
, and_deref
)
2978 OP(AtomicOr
, or_deref
)
2979 OP(AtomicXor
, xor_deref
)
2982 /* We left the following out: AtomicStore, AtomicSMin and
2983 * AtomicSmax. Right now there are not nir intrinsics for them. At this
2984 * moment Atomic Counter support is needed for ARB_spirv support, so is
2985 * only need to support GLSL Atomic Counters that are uints and don't
2986 * allow direct storage.
2988 unreachable("Invalid uniform atomic");
2992 static nir_intrinsic_op
2993 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2996 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
2997 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
2998 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
2999 OP(AtomicExchange
, atomic_exchange
)
3000 OP(AtomicCompareExchange
, atomic_comp_swap
)
3001 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3002 OP(AtomicIIncrement
, atomic_add
)
3003 OP(AtomicIDecrement
, atomic_add
)
3004 OP(AtomicIAdd
, atomic_add
)
3005 OP(AtomicISub
, atomic_add
)
3006 OP(AtomicSMin
, atomic_imin
)
3007 OP(AtomicUMin
, atomic_umin
)
3008 OP(AtomicSMax
, atomic_imax
)
3009 OP(AtomicUMax
, atomic_umax
)
3010 OP(AtomicAnd
, atomic_and
)
3011 OP(AtomicOr
, atomic_or
)
3012 OP(AtomicXor
, atomic_xor
)
3015 vtn_fail_with_opcode("Invalid shared atomic", opcode
);
3020 * Handles shared atomics, ssbo atomics and atomic counters.
3023 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
3024 const uint32_t *w
, unsigned count
)
3026 struct vtn_pointer
*ptr
;
3027 nir_intrinsic_instr
*atomic
;
3029 SpvScope scope
= SpvScopeInvocation
;
3030 SpvMemorySemanticsMask semantics
= 0;
3033 case SpvOpAtomicLoad
:
3034 case SpvOpAtomicExchange
:
3035 case SpvOpAtomicCompareExchange
:
3036 case SpvOpAtomicCompareExchangeWeak
:
3037 case SpvOpAtomicIIncrement
:
3038 case SpvOpAtomicIDecrement
:
3039 case SpvOpAtomicIAdd
:
3040 case SpvOpAtomicISub
:
3041 case SpvOpAtomicSMin
:
3042 case SpvOpAtomicUMin
:
3043 case SpvOpAtomicSMax
:
3044 case SpvOpAtomicUMax
:
3045 case SpvOpAtomicAnd
:
3047 case SpvOpAtomicXor
:
3048 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
3049 scope
= vtn_constant_uint(b
, w
[4]);
3050 semantics
= vtn_constant_uint(b
, w
[5]);
3053 case SpvOpAtomicStore
:
3054 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
3055 scope
= vtn_constant_uint(b
, w
[2]);
3056 semantics
= vtn_constant_uint(b
, w
[3]);
3060 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3063 /* uniform as "atomic counter uniform" */
3064 if (ptr
->mode
== vtn_variable_mode_uniform
) {
3065 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3066 const struct glsl_type
*deref_type
= deref
->type
;
3067 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
3068 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3069 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3071 /* SSBO needs to initialize index/offset. In this case we don't need to,
3072 * as that info is already stored on the ptr->var->var nir_variable (see
3073 * vtn_create_variable)
3077 case SpvOpAtomicLoad
:
3078 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3081 case SpvOpAtomicStore
:
3082 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3083 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3086 case SpvOpAtomicExchange
:
3087 case SpvOpAtomicCompareExchange
:
3088 case SpvOpAtomicCompareExchangeWeak
:
3089 case SpvOpAtomicIIncrement
:
3090 case SpvOpAtomicIDecrement
:
3091 case SpvOpAtomicIAdd
:
3092 case SpvOpAtomicISub
:
3093 case SpvOpAtomicSMin
:
3094 case SpvOpAtomicUMin
:
3095 case SpvOpAtomicSMax
:
3096 case SpvOpAtomicUMax
:
3097 case SpvOpAtomicAnd
:
3099 case SpvOpAtomicXor
:
3100 /* Nothing: we don't need to call fill_common_atomic_sources here, as
3101 * atomic counter uniforms doesn't have sources
3106 unreachable("Invalid SPIR-V atomic");
3109 } else if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
3110 nir_ssa_def
*offset
, *index
;
3111 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
3113 assert(ptr
->mode
== vtn_variable_mode_ssbo
);
3115 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(b
, opcode
);
3116 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3120 case SpvOpAtomicLoad
:
3121 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3122 nir_intrinsic_set_align(atomic
, 4, 0);
3123 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3124 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3125 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3128 case SpvOpAtomicStore
:
3129 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3130 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3131 nir_intrinsic_set_align(atomic
, 4, 0);
3132 atomic
->src
[src
++] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
3133 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3134 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3135 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3138 case SpvOpAtomicExchange
:
3139 case SpvOpAtomicCompareExchange
:
3140 case SpvOpAtomicCompareExchangeWeak
:
3141 case SpvOpAtomicIIncrement
:
3142 case SpvOpAtomicIDecrement
:
3143 case SpvOpAtomicIAdd
:
3144 case SpvOpAtomicISub
:
3145 case SpvOpAtomicSMin
:
3146 case SpvOpAtomicUMin
:
3147 case SpvOpAtomicSMax
:
3148 case SpvOpAtomicUMax
:
3149 case SpvOpAtomicAnd
:
3151 case SpvOpAtomicXor
:
3152 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3153 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3154 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3155 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
3159 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3162 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3163 const struct glsl_type
*deref_type
= deref
->type
;
3164 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
3165 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3166 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3169 case SpvOpAtomicLoad
:
3170 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3173 case SpvOpAtomicStore
:
3174 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3175 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3176 atomic
->src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
3179 case SpvOpAtomicExchange
:
3180 case SpvOpAtomicCompareExchange
:
3181 case SpvOpAtomicCompareExchangeWeak
:
3182 case SpvOpAtomicIIncrement
:
3183 case SpvOpAtomicIDecrement
:
3184 case SpvOpAtomicIAdd
:
3185 case SpvOpAtomicISub
:
3186 case SpvOpAtomicSMin
:
3187 case SpvOpAtomicUMin
:
3188 case SpvOpAtomicSMax
:
3189 case SpvOpAtomicUMax
:
3190 case SpvOpAtomicAnd
:
3192 case SpvOpAtomicXor
:
3193 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
3197 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3201 /* Atomic ordering operations will implicitly apply to the atomic operation
3202 * storage class, so include that too.
3204 semantics
|= vtn_storage_class_to_memory_semantics(ptr
->ptr_type
->storage_class
);
3206 SpvMemorySemanticsMask before_semantics
;
3207 SpvMemorySemanticsMask after_semantics
;
3208 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
3210 if (before_semantics
)
3211 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
3213 if (opcode
!= SpvOpAtomicStore
) {
3214 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3216 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
3217 glsl_get_vector_elements(type
->type
),
3218 glsl_get_bit_size(type
->type
), NULL
);
3220 struct vtn_ssa_value
*ssa
= rzalloc(b
, struct vtn_ssa_value
);
3221 ssa
->def
= &atomic
->dest
.ssa
;
3222 ssa
->type
= type
->type
;
3223 vtn_push_ssa(b
, w
[2], type
, ssa
);
3226 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
3228 if (after_semantics
)
3229 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
3232 static nir_alu_instr
*
3233 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
3235 nir_op op
= nir_op_vec(num_components
);
3236 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
3237 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
3239 vec
->dest
.write_mask
= (1 << num_components
) - 1;
3244 struct vtn_ssa_value
*
3245 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
3247 if (src
->transposed
)
3248 return src
->transposed
;
3250 struct vtn_ssa_value
*dest
=
3251 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
3253 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
3254 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
3255 glsl_get_bit_size(src
->type
));
3256 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3257 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
3258 vec
->src
[0].swizzle
[0] = i
;
3260 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
3261 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
3262 vec
->src
[j
].swizzle
[0] = i
;
3265 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3266 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
3269 dest
->transposed
= src
;
3275 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
3277 return nir_channel(&b
->nb
, src
, index
);
3281 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
3284 nir_alu_instr
*vec
= create_vec(b
, src
->num_components
,
3287 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
3289 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
3291 vec
->src
[i
].src
= nir_src_for_ssa(src
);
3292 vec
->src
[i
].swizzle
[0] = i
;
3296 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3298 return &vec
->dest
.dest
.ssa
;
3301 static nir_ssa_def
*
3302 nir_ieq_imm(nir_builder
*b
, nir_ssa_def
*x
, uint64_t i
)
3304 return nir_ieq(b
, x
, nir_imm_intN_t(b
, i
, x
->bit_size
));
3308 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
3311 return nir_vector_extract(&b
->nb
, src
, nir_i2i(&b
->nb
, index
, 32));
3315 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
3316 nir_ssa_def
*insert
, nir_ssa_def
*index
)
3318 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
3319 for (unsigned i
= 1; i
< src
->num_components
; i
++)
3320 dest
= nir_bcsel(&b
->nb
, nir_ieq_imm(&b
->nb
, index
, i
),
3321 vtn_vector_insert(b
, src
, insert
, i
), dest
);
3326 static nir_ssa_def
*
3327 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
3328 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
3329 const uint32_t *indices
)
3331 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
3333 for (unsigned i
= 0; i
< num_components
; i
++) {
3334 uint32_t index
= indices
[i
];
3335 if (index
== 0xffffffff) {
3337 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
3338 } else if (index
< src0
->num_components
) {
3339 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
3340 vec
->src
[i
].swizzle
[0] = index
;
3342 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
3343 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
3347 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3349 return &vec
->dest
.dest
.ssa
;
3353 * Concatentates a number of vectors/scalars together to produce a vector
3355 static nir_ssa_def
*
3356 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
3357 unsigned num_srcs
, nir_ssa_def
**srcs
)
3359 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
3361 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3363 * "When constructing a vector, there must be at least two Constituent
3366 vtn_assert(num_srcs
>= 2);
3368 unsigned dest_idx
= 0;
3369 for (unsigned i
= 0; i
< num_srcs
; i
++) {
3370 nir_ssa_def
*src
= srcs
[i
];
3371 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
3372 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
3373 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
3374 vec
->src
[dest_idx
].swizzle
[0] = j
;
3379 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3381 * "When constructing a vector, the total number of components in all
3382 * the operands must equal the number of components in Result Type."
3384 vtn_assert(dest_idx
== num_components
);
3386 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3388 return &vec
->dest
.dest
.ssa
;
3391 static struct vtn_ssa_value
*
3392 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3394 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3395 dest
->type
= src
->type
;
3397 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3398 dest
->def
= src
->def
;
3400 unsigned elems
= glsl_get_length(src
->type
);
3402 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3403 for (unsigned i
= 0; i
< elems
; i
++)
3404 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3410 static struct vtn_ssa_value
*
3411 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3412 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3413 unsigned num_indices
)
3415 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3417 struct vtn_ssa_value
*cur
= dest
;
3419 for (i
= 0; i
< num_indices
- 1; i
++) {
3420 cur
= cur
->elems
[indices
[i
]];
3423 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3424 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3425 * the component granularity. In that case, the last index will be
3426 * the index to insert the scalar into the vector.
3429 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
3431 cur
->elems
[indices
[i
]] = insert
;
3437 static struct vtn_ssa_value
*
3438 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3439 const uint32_t *indices
, unsigned num_indices
)
3441 struct vtn_ssa_value
*cur
= src
;
3442 for (unsigned i
= 0; i
< num_indices
; i
++) {
3443 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3444 vtn_assert(i
== num_indices
- 1);
3445 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3446 * the component granularity. The last index will be the index of the
3447 * vector to extract.
3450 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
3451 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
3452 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
3455 cur
= cur
->elems
[indices
[i
]];
3463 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3464 const uint32_t *w
, unsigned count
)
3466 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3467 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, type
->type
);
3470 case SpvOpVectorExtractDynamic
:
3471 ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3472 vtn_ssa_value(b
, w
[4])->def
);
3475 case SpvOpVectorInsertDynamic
:
3476 ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3477 vtn_ssa_value(b
, w
[4])->def
,
3478 vtn_ssa_value(b
, w
[5])->def
);
3481 case SpvOpVectorShuffle
:
3482 ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
->type
),
3483 vtn_ssa_value(b
, w
[3])->def
,
3484 vtn_ssa_value(b
, w
[4])->def
,
3488 case SpvOpCompositeConstruct
: {
3489 unsigned elems
= count
- 3;
3491 if (glsl_type_is_vector_or_scalar(type
->type
)) {
3492 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3493 for (unsigned i
= 0; i
< elems
; i
++)
3494 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
3496 vtn_vector_construct(b
, glsl_get_vector_elements(type
->type
),
3499 ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3500 for (unsigned i
= 0; i
< elems
; i
++)
3501 ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3505 case SpvOpCompositeExtract
:
3506 ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3510 case SpvOpCompositeInsert
:
3511 ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3512 vtn_ssa_value(b
, w
[3]),
3516 case SpvOpCopyLogical
:
3517 case SpvOpCopyObject
:
3518 ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3522 vtn_fail_with_opcode("unknown composite operation", opcode
);
3525 vtn_push_ssa(b
, w
[2], type
, ssa
);
3529 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3531 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3532 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3536 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3537 SpvMemorySemanticsMask semantics
)
3539 if (b
->options
->use_scoped_memory_barrier
) {
3540 vtn_emit_scoped_memory_barrier(b
, scope
, semantics
);
3544 static const SpvMemorySemanticsMask all_memory_semantics
=
3545 SpvMemorySemanticsUniformMemoryMask
|
3546 SpvMemorySemanticsWorkgroupMemoryMask
|
3547 SpvMemorySemanticsAtomicCounterMemoryMask
|
3548 SpvMemorySemanticsImageMemoryMask
;
3550 /* If we're not actually doing a memory barrier, bail */
3551 if (!(semantics
& all_memory_semantics
))
3554 /* GL and Vulkan don't have these */
3555 vtn_assert(scope
!= SpvScopeCrossDevice
);
3557 if (scope
== SpvScopeSubgroup
)
3558 return; /* Nothing to do here */
3560 if (scope
== SpvScopeWorkgroup
) {
3561 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3565 /* There's only two scopes thing left */
3566 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3568 if ((semantics
& all_memory_semantics
) == all_memory_semantics
) {
3569 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3573 /* Issue a bunch of more specific barriers */
3574 uint32_t bits
= semantics
;
3576 SpvMemorySemanticsMask semantic
= 1 << u_bit_scan(&bits
);
3578 case SpvMemorySemanticsUniformMemoryMask
:
3579 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3581 case SpvMemorySemanticsWorkgroupMemoryMask
:
3582 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3584 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3585 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3587 case SpvMemorySemanticsImageMemoryMask
:
3588 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3597 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3598 const uint32_t *w
, unsigned count
)
3601 case SpvOpEmitVertex
:
3602 case SpvOpEmitStreamVertex
:
3603 case SpvOpEndPrimitive
:
3604 case SpvOpEndStreamPrimitive
: {
3605 nir_intrinsic_op intrinsic_op
;
3607 case SpvOpEmitVertex
:
3608 case SpvOpEmitStreamVertex
:
3609 intrinsic_op
= nir_intrinsic_emit_vertex
;
3611 case SpvOpEndPrimitive
:
3612 case SpvOpEndStreamPrimitive
:
3613 intrinsic_op
= nir_intrinsic_end_primitive
;
3616 unreachable("Invalid opcode");
3619 nir_intrinsic_instr
*intrin
=
3620 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3623 case SpvOpEmitStreamVertex
:
3624 case SpvOpEndStreamPrimitive
: {
3625 unsigned stream
= vtn_constant_uint(b
, w
[1]);
3626 nir_intrinsic_set_stream_id(intrin
, stream
);
3634 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3638 case SpvOpMemoryBarrier
: {
3639 SpvScope scope
= vtn_constant_uint(b
, w
[1]);
3640 SpvMemorySemanticsMask semantics
= vtn_constant_uint(b
, w
[2]);
3641 vtn_emit_memory_barrier(b
, scope
, semantics
);
3645 case SpvOpControlBarrier
: {
3646 SpvScope memory_scope
= vtn_constant_uint(b
, w
[2]);
3647 SpvMemorySemanticsMask memory_semantics
= vtn_constant_uint(b
, w
[3]);
3648 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
3650 SpvScope execution_scope
= vtn_constant_uint(b
, w
[1]);
3651 if (execution_scope
== SpvScopeWorkgroup
)
3652 vtn_emit_barrier(b
, nir_intrinsic_barrier
);
3657 unreachable("unknown barrier instruction");
3662 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
3663 SpvExecutionMode mode
)
3666 case SpvExecutionModeInputPoints
:
3667 case SpvExecutionModeOutputPoints
:
3668 return 0; /* GL_POINTS */
3669 case SpvExecutionModeInputLines
:
3670 return 1; /* GL_LINES */
3671 case SpvExecutionModeInputLinesAdjacency
:
3672 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3673 case SpvExecutionModeTriangles
:
3674 return 4; /* GL_TRIANGLES */
3675 case SpvExecutionModeInputTrianglesAdjacency
:
3676 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3677 case SpvExecutionModeQuads
:
3678 return 7; /* GL_QUADS */
3679 case SpvExecutionModeIsolines
:
3680 return 0x8E7A; /* GL_ISOLINES */
3681 case SpvExecutionModeOutputLineStrip
:
3682 return 3; /* GL_LINE_STRIP */
3683 case SpvExecutionModeOutputTriangleStrip
:
3684 return 5; /* GL_TRIANGLE_STRIP */
3686 vtn_fail("Invalid primitive type: %s (%u)",
3687 spirv_executionmode_to_string(mode
), mode
);
3692 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
3693 SpvExecutionMode mode
)
3696 case SpvExecutionModeInputPoints
:
3698 case SpvExecutionModeInputLines
:
3700 case SpvExecutionModeInputLinesAdjacency
:
3702 case SpvExecutionModeTriangles
:
3704 case SpvExecutionModeInputTrianglesAdjacency
:
3707 vtn_fail("Invalid GS input mode: %s (%u)",
3708 spirv_executionmode_to_string(mode
), mode
);
3712 static gl_shader_stage
3713 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
3716 case SpvExecutionModelVertex
:
3717 return MESA_SHADER_VERTEX
;
3718 case SpvExecutionModelTessellationControl
:
3719 return MESA_SHADER_TESS_CTRL
;
3720 case SpvExecutionModelTessellationEvaluation
:
3721 return MESA_SHADER_TESS_EVAL
;
3722 case SpvExecutionModelGeometry
:
3723 return MESA_SHADER_GEOMETRY
;
3724 case SpvExecutionModelFragment
:
3725 return MESA_SHADER_FRAGMENT
;
3726 case SpvExecutionModelGLCompute
:
3727 return MESA_SHADER_COMPUTE
;
3728 case SpvExecutionModelKernel
:
3729 return MESA_SHADER_KERNEL
;
3731 vtn_fail("Unsupported execution model: %s (%u)",
3732 spirv_executionmodel_to_string(model
), model
);
3736 #define spv_check_supported(name, cap) do { \
3737 if (!(b->options && b->options->caps.name)) \
3738 vtn_warn("Unsupported SPIR-V capability: %s (%u)", \
3739 spirv_capability_to_string(cap), cap); \
3744 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
3747 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
3748 /* Let this be a name label regardless */
3749 unsigned name_words
;
3750 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
3752 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
3753 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
3756 vtn_assert(b
->entry_point
== NULL
);
3757 b
->entry_point
= entry_point
;
3761 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3762 const uint32_t *w
, unsigned count
)
3769 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
3770 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
3771 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
3772 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
3773 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
3774 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
3777 uint32_t version
= w
[2];
3780 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
3782 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
3786 case SpvOpSourceExtension
:
3787 case SpvOpSourceContinued
:
3788 case SpvOpExtension
:
3789 case SpvOpModuleProcessed
:
3790 /* Unhandled, but these are for debug so that's ok. */
3793 case SpvOpCapability
: {
3794 SpvCapability cap
= w
[1];
3796 case SpvCapabilityMatrix
:
3797 case SpvCapabilityShader
:
3798 case SpvCapabilityGeometry
:
3799 case SpvCapabilityGeometryPointSize
:
3800 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
3801 case SpvCapabilitySampledImageArrayDynamicIndexing
:
3802 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
3803 case SpvCapabilityStorageImageArrayDynamicIndexing
:
3804 case SpvCapabilityImageRect
:
3805 case SpvCapabilitySampledRect
:
3806 case SpvCapabilitySampled1D
:
3807 case SpvCapabilityImage1D
:
3808 case SpvCapabilitySampledCubeArray
:
3809 case SpvCapabilityImageCubeArray
:
3810 case SpvCapabilitySampledBuffer
:
3811 case SpvCapabilityImageBuffer
:
3812 case SpvCapabilityImageQuery
:
3813 case SpvCapabilityDerivativeControl
:
3814 case SpvCapabilityInterpolationFunction
:
3815 case SpvCapabilityMultiViewport
:
3816 case SpvCapabilitySampleRateShading
:
3817 case SpvCapabilityClipDistance
:
3818 case SpvCapabilityCullDistance
:
3819 case SpvCapabilityInputAttachment
:
3820 case SpvCapabilityImageGatherExtended
:
3821 case SpvCapabilityStorageImageExtendedFormats
:
3822 case SpvCapabilityVector16
:
3825 case SpvCapabilityLinkage
:
3826 case SpvCapabilityFloat16Buffer
:
3827 case SpvCapabilitySparseResidency
:
3828 vtn_warn("Unsupported SPIR-V capability: %s",
3829 spirv_capability_to_string(cap
));
3832 case SpvCapabilityMinLod
:
3833 spv_check_supported(min_lod
, cap
);
3836 case SpvCapabilityAtomicStorage
:
3837 spv_check_supported(atomic_storage
, cap
);
3840 case SpvCapabilityFloat64
:
3841 spv_check_supported(float64
, cap
);
3843 case SpvCapabilityInt64
:
3844 spv_check_supported(int64
, cap
);
3846 case SpvCapabilityInt16
:
3847 spv_check_supported(int16
, cap
);
3849 case SpvCapabilityInt8
:
3850 spv_check_supported(int8
, cap
);
3853 case SpvCapabilityTransformFeedback
:
3854 spv_check_supported(transform_feedback
, cap
);
3857 case SpvCapabilityGeometryStreams
:
3858 spv_check_supported(geometry_streams
, cap
);
3861 case SpvCapabilityInt64Atomics
:
3862 spv_check_supported(int64_atomics
, cap
);
3865 case SpvCapabilityStorageImageMultisample
:
3866 spv_check_supported(storage_image_ms
, cap
);
3869 case SpvCapabilityAddresses
:
3870 spv_check_supported(address
, cap
);
3873 case SpvCapabilityKernel
:
3874 spv_check_supported(kernel
, cap
);
3877 case SpvCapabilityImageBasic
:
3878 case SpvCapabilityImageReadWrite
:
3879 case SpvCapabilityImageMipmap
:
3880 case SpvCapabilityPipes
:
3881 case SpvCapabilityDeviceEnqueue
:
3882 case SpvCapabilityLiteralSampler
:
3883 case SpvCapabilityGenericPointer
:
3884 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
3885 spirv_capability_to_string(cap
));
3888 case SpvCapabilityImageMSArray
:
3889 spv_check_supported(image_ms_array
, cap
);
3892 case SpvCapabilityTessellation
:
3893 case SpvCapabilityTessellationPointSize
:
3894 spv_check_supported(tessellation
, cap
);
3897 case SpvCapabilityDrawParameters
:
3898 spv_check_supported(draw_parameters
, cap
);
3901 case SpvCapabilityStorageImageReadWithoutFormat
:
3902 spv_check_supported(image_read_without_format
, cap
);
3905 case SpvCapabilityStorageImageWriteWithoutFormat
:
3906 spv_check_supported(image_write_without_format
, cap
);
3909 case SpvCapabilityDeviceGroup
:
3910 spv_check_supported(device_group
, cap
);
3913 case SpvCapabilityMultiView
:
3914 spv_check_supported(multiview
, cap
);
3917 case SpvCapabilityGroupNonUniform
:
3918 spv_check_supported(subgroup_basic
, cap
);
3921 case SpvCapabilitySubgroupVoteKHR
:
3922 case SpvCapabilityGroupNonUniformVote
:
3923 spv_check_supported(subgroup_vote
, cap
);
3926 case SpvCapabilitySubgroupBallotKHR
:
3927 case SpvCapabilityGroupNonUniformBallot
:
3928 spv_check_supported(subgroup_ballot
, cap
);
3931 case SpvCapabilityGroupNonUniformShuffle
:
3932 case SpvCapabilityGroupNonUniformShuffleRelative
:
3933 spv_check_supported(subgroup_shuffle
, cap
);
3936 case SpvCapabilityGroupNonUniformQuad
:
3937 spv_check_supported(subgroup_quad
, cap
);
3940 case SpvCapabilityGroupNonUniformArithmetic
:
3941 case SpvCapabilityGroupNonUniformClustered
:
3942 spv_check_supported(subgroup_arithmetic
, cap
);
3945 case SpvCapabilityGroups
:
3946 spv_check_supported(amd_shader_ballot
, cap
);
3949 case SpvCapabilityVariablePointersStorageBuffer
:
3950 case SpvCapabilityVariablePointers
:
3951 spv_check_supported(variable_pointers
, cap
);
3952 b
->variable_pointers
= true;
3955 case SpvCapabilityStorageUniformBufferBlock16
:
3956 case SpvCapabilityStorageUniform16
:
3957 case SpvCapabilityStoragePushConstant16
:
3958 case SpvCapabilityStorageInputOutput16
:
3959 spv_check_supported(storage_16bit
, cap
);
3962 case SpvCapabilityShaderLayer
:
3963 case SpvCapabilityShaderViewportIndex
:
3964 case SpvCapabilityShaderViewportIndexLayerEXT
:
3965 spv_check_supported(shader_viewport_index_layer
, cap
);
3968 case SpvCapabilityStorageBuffer8BitAccess
:
3969 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
3970 case SpvCapabilityStoragePushConstant8
:
3971 spv_check_supported(storage_8bit
, cap
);
3974 case SpvCapabilityShaderNonUniformEXT
:
3975 spv_check_supported(descriptor_indexing
, cap
);
3978 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
3979 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
3980 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
3981 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
3984 case SpvCapabilityUniformBufferArrayNonUniformIndexingEXT
:
3985 case SpvCapabilitySampledImageArrayNonUniformIndexingEXT
:
3986 case SpvCapabilityStorageBufferArrayNonUniformIndexingEXT
:
3987 case SpvCapabilityStorageImageArrayNonUniformIndexingEXT
:
3988 case SpvCapabilityInputAttachmentArrayNonUniformIndexingEXT
:
3989 case SpvCapabilityUniformTexelBufferArrayNonUniformIndexingEXT
:
3990 case SpvCapabilityStorageTexelBufferArrayNonUniformIndexingEXT
:
3991 spv_check_supported(descriptor_array_non_uniform_indexing
, cap
);
3994 case SpvCapabilityRuntimeDescriptorArrayEXT
:
3995 spv_check_supported(runtime_descriptor_array
, cap
);
3998 case SpvCapabilityStencilExportEXT
:
3999 spv_check_supported(stencil_export
, cap
);
4002 case SpvCapabilitySampleMaskPostDepthCoverage
:
4003 spv_check_supported(post_depth_coverage
, cap
);
4006 case SpvCapabilityDenormFlushToZero
:
4007 case SpvCapabilityDenormPreserve
:
4008 case SpvCapabilitySignedZeroInfNanPreserve
:
4009 case SpvCapabilityRoundingModeRTE
:
4010 case SpvCapabilityRoundingModeRTZ
:
4011 spv_check_supported(float_controls
, cap
);
4014 case SpvCapabilityPhysicalStorageBufferAddressesEXT
:
4015 spv_check_supported(physical_storage_buffer_address
, cap
);
4018 case SpvCapabilityComputeDerivativeGroupQuadsNV
:
4019 case SpvCapabilityComputeDerivativeGroupLinearNV
:
4020 spv_check_supported(derivative_group
, cap
);
4023 case SpvCapabilityFloat16
:
4024 spv_check_supported(float16
, cap
);
4027 case SpvCapabilityFragmentShaderSampleInterlockEXT
:
4028 spv_check_supported(fragment_shader_sample_interlock
, cap
);
4031 case SpvCapabilityFragmentShaderPixelInterlockEXT
:
4032 spv_check_supported(fragment_shader_pixel_interlock
, cap
);
4035 case SpvCapabilityDemoteToHelperInvocationEXT
:
4036 spv_check_supported(demote_to_helper_invocation
, cap
);
4039 case SpvCapabilityShaderClockKHR
:
4040 spv_check_supported(shader_clock
, cap
);
4043 case SpvCapabilityVulkanMemoryModel
:
4044 spv_check_supported(vk_memory_model
, cap
);
4047 case SpvCapabilityVulkanMemoryModelDeviceScope
:
4048 spv_check_supported(vk_memory_model_device_scope
, cap
);
4051 case SpvCapabilityImageReadWriteLodAMD
:
4052 spv_check_supported(amd_image_read_write_lod
, cap
);
4056 vtn_fail("Unhandled capability: %s (%u)",
4057 spirv_capability_to_string(cap
), cap
);
4062 case SpvOpExtInstImport
:
4063 vtn_handle_extension(b
, opcode
, w
, count
);
4066 case SpvOpMemoryModel
:
4068 case SpvAddressingModelPhysical32
:
4069 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4070 "AddressingModelPhysical32 only supported for kernels");
4071 b
->shader
->info
.cs
.ptr_size
= 32;
4072 b
->physical_ptrs
= true;
4073 b
->options
->shared_addr_format
= nir_address_format_32bit_global
;
4074 b
->options
->global_addr_format
= nir_address_format_32bit_global
;
4075 b
->options
->temp_addr_format
= nir_address_format_32bit_global
;
4077 case SpvAddressingModelPhysical64
:
4078 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4079 "AddressingModelPhysical64 only supported for kernels");
4080 b
->shader
->info
.cs
.ptr_size
= 64;
4081 b
->physical_ptrs
= true;
4082 b
->options
->shared_addr_format
= nir_address_format_64bit_global
;
4083 b
->options
->global_addr_format
= nir_address_format_64bit_global
;
4084 b
->options
->temp_addr_format
= nir_address_format_64bit_global
;
4086 case SpvAddressingModelLogical
:
4087 vtn_fail_if(b
->shader
->info
.stage
>= MESA_SHADER_STAGES
,
4088 "AddressingModelLogical only supported for shaders");
4089 b
->physical_ptrs
= false;
4091 case SpvAddressingModelPhysicalStorageBuffer64EXT
:
4092 vtn_fail_if(!b
->options
||
4093 !b
->options
->caps
.physical_storage_buffer_address
,
4094 "AddressingModelPhysicalStorageBuffer64EXT not supported");
4097 vtn_fail("Unknown addressing model: %s (%u)",
4098 spirv_addressingmodel_to_string(w
[1]), w
[1]);
4103 case SpvMemoryModelSimple
:
4104 case SpvMemoryModelGLSL450
:
4105 case SpvMemoryModelOpenCL
:
4107 case SpvMemoryModelVulkan
:
4108 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
4109 "Vulkan memory model is unsupported by this driver");
4112 vtn_fail("Unsupported memory model: %s",
4113 spirv_memorymodel_to_string(w
[2]));
4118 case SpvOpEntryPoint
:
4119 vtn_handle_entry_point(b
, w
, count
);
4123 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
4124 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4128 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4131 case SpvOpMemberName
:
4135 case SpvOpExecutionMode
:
4136 case SpvOpExecutionModeId
:
4137 case SpvOpDecorationGroup
:
4139 case SpvOpDecorateId
:
4140 case SpvOpMemberDecorate
:
4141 case SpvOpGroupDecorate
:
4142 case SpvOpGroupMemberDecorate
:
4143 case SpvOpDecorateString
:
4144 case SpvOpMemberDecorateString
:
4145 vtn_handle_decoration(b
, opcode
, w
, count
);
4148 case SpvOpExtInst
: {
4149 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4150 if (val
->ext_handler
== vtn_handle_non_semantic_instruction
) {
4151 /* NonSemantic extended instructions are acceptable in preamble. */
4152 vtn_handle_non_semantic_instruction(b
, w
[4], w
, count
);
4155 return false; /* End of preamble. */
4160 return false; /* End of preamble */
4167 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4168 const struct vtn_decoration
*mode
, void *data
)
4170 vtn_assert(b
->entry_point
== entry_point
);
4172 switch(mode
->exec_mode
) {
4173 case SpvExecutionModeOriginUpperLeft
:
4174 case SpvExecutionModeOriginLowerLeft
:
4175 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4176 b
->shader
->info
.fs
.origin_upper_left
=
4177 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
4180 case SpvExecutionModeEarlyFragmentTests
:
4181 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4182 b
->shader
->info
.fs
.early_fragment_tests
= true;
4185 case SpvExecutionModePostDepthCoverage
:
4186 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4187 b
->shader
->info
.fs
.post_depth_coverage
= true;
4190 case SpvExecutionModeInvocations
:
4191 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4192 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->operands
[0]);
4195 case SpvExecutionModeDepthReplacing
:
4196 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4197 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
4199 case SpvExecutionModeDepthGreater
:
4200 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4201 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
4203 case SpvExecutionModeDepthLess
:
4204 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4205 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
4207 case SpvExecutionModeDepthUnchanged
:
4208 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4209 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
4212 case SpvExecutionModeLocalSize
:
4213 vtn_assert(gl_shader_stage_is_compute(b
->shader
->info
.stage
));
4214 b
->shader
->info
.cs
.local_size
[0] = mode
->operands
[0];
4215 b
->shader
->info
.cs
.local_size
[1] = mode
->operands
[1];
4216 b
->shader
->info
.cs
.local_size
[2] = mode
->operands
[2];
4219 case SpvExecutionModeLocalSizeId
:
4220 b
->shader
->info
.cs
.local_size
[0] = vtn_constant_uint(b
, mode
->operands
[0]);
4221 b
->shader
->info
.cs
.local_size
[1] = vtn_constant_uint(b
, mode
->operands
[1]);
4222 b
->shader
->info
.cs
.local_size
[2] = vtn_constant_uint(b
, mode
->operands
[2]);
4225 case SpvExecutionModeLocalSizeHint
:
4226 case SpvExecutionModeLocalSizeHintId
:
4227 break; /* Nothing to do with this */
4229 case SpvExecutionModeOutputVertices
:
4230 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4231 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4232 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->operands
[0];
4234 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4235 b
->shader
->info
.gs
.vertices_out
= mode
->operands
[0];
4239 case SpvExecutionModeInputPoints
:
4240 case SpvExecutionModeInputLines
:
4241 case SpvExecutionModeInputLinesAdjacency
:
4242 case SpvExecutionModeTriangles
:
4243 case SpvExecutionModeInputTrianglesAdjacency
:
4244 case SpvExecutionModeQuads
:
4245 case SpvExecutionModeIsolines
:
4246 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4247 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4248 b
->shader
->info
.tess
.primitive_mode
=
4249 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4251 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4252 b
->shader
->info
.gs
.vertices_in
=
4253 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
4254 b
->shader
->info
.gs
.input_primitive
=
4255 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4259 case SpvExecutionModeOutputPoints
:
4260 case SpvExecutionModeOutputLineStrip
:
4261 case SpvExecutionModeOutputTriangleStrip
:
4262 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4263 b
->shader
->info
.gs
.output_primitive
=
4264 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4267 case SpvExecutionModeSpacingEqual
:
4268 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4269 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4270 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
4272 case SpvExecutionModeSpacingFractionalEven
:
4273 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4274 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4275 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
4277 case SpvExecutionModeSpacingFractionalOdd
:
4278 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4279 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4280 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
4282 case SpvExecutionModeVertexOrderCw
:
4283 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4284 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4285 b
->shader
->info
.tess
.ccw
= false;
4287 case SpvExecutionModeVertexOrderCcw
:
4288 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4289 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4290 b
->shader
->info
.tess
.ccw
= true;
4292 case SpvExecutionModePointMode
:
4293 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4294 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4295 b
->shader
->info
.tess
.point_mode
= true;
4298 case SpvExecutionModePixelCenterInteger
:
4299 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4300 b
->shader
->info
.fs
.pixel_center_integer
= true;
4303 case SpvExecutionModeXfb
:
4304 b
->shader
->info
.has_transform_feedback_varyings
= true;
4307 case SpvExecutionModeVecTypeHint
:
4310 case SpvExecutionModeContractionOff
:
4311 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
4312 vtn_warn("ExectionMode only allowed for CL-style kernels: %s",
4313 spirv_executionmode_to_string(mode
->exec_mode
));
4318 case SpvExecutionModeStencilRefReplacingEXT
:
4319 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4322 case SpvExecutionModeDerivativeGroupQuadsNV
:
4323 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4324 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_QUADS
;
4327 case SpvExecutionModeDerivativeGroupLinearNV
:
4328 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4329 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_LINEAR
;
4332 case SpvExecutionModePixelInterlockOrderedEXT
:
4333 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4334 b
->shader
->info
.fs
.pixel_interlock_ordered
= true;
4337 case SpvExecutionModePixelInterlockUnorderedEXT
:
4338 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4339 b
->shader
->info
.fs
.pixel_interlock_unordered
= true;
4342 case SpvExecutionModeSampleInterlockOrderedEXT
:
4343 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4344 b
->shader
->info
.fs
.sample_interlock_ordered
= true;
4347 case SpvExecutionModeSampleInterlockUnorderedEXT
:
4348 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4349 b
->shader
->info
.fs
.sample_interlock_unordered
= true;
4352 case SpvExecutionModeDenormPreserve
:
4353 case SpvExecutionModeDenormFlushToZero
:
4354 case SpvExecutionModeSignedZeroInfNanPreserve
:
4355 case SpvExecutionModeRoundingModeRTE
:
4356 case SpvExecutionModeRoundingModeRTZ
:
4357 /* Already handled in vtn_handle_rounding_mode_in_execution_mode() */
4361 vtn_fail("Unhandled execution mode: %s (%u)",
4362 spirv_executionmode_to_string(mode
->exec_mode
),
4368 vtn_handle_rounding_mode_in_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4369 const struct vtn_decoration
*mode
, void *data
)
4371 vtn_assert(b
->entry_point
== entry_point
);
4373 unsigned execution_mode
= 0;
4375 switch(mode
->exec_mode
) {
4376 case SpvExecutionModeDenormPreserve
:
4377 switch (mode
->operands
[0]) {
4378 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP16
; break;
4379 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP32
; break;
4380 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP64
; break;
4381 default: vtn_fail("Floating point type not supported");
4384 case SpvExecutionModeDenormFlushToZero
:
4385 switch (mode
->operands
[0]) {
4386 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
; break;
4387 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32
; break;
4388 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64
; break;
4389 default: vtn_fail("Floating point type not supported");
4392 case SpvExecutionModeSignedZeroInfNanPreserve
:
4393 switch (mode
->operands
[0]) {
4394 case 16: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16
; break;
4395 case 32: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32
; break;
4396 case 64: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64
; break;
4397 default: vtn_fail("Floating point type not supported");
4400 case SpvExecutionModeRoundingModeRTE
:
4401 switch (mode
->operands
[0]) {
4402 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16
; break;
4403 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32
; break;
4404 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64
; break;
4405 default: vtn_fail("Floating point type not supported");
4408 case SpvExecutionModeRoundingModeRTZ
:
4409 switch (mode
->operands
[0]) {
4410 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
; break;
4411 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
; break;
4412 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
; break;
4413 default: vtn_fail("Floating point type not supported");
4421 b
->shader
->info
.float_controls_execution_mode
|= execution_mode
;
4425 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4426 const uint32_t *w
, unsigned count
)
4428 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
4432 case SpvOpSourceContinued
:
4433 case SpvOpSourceExtension
:
4434 case SpvOpExtension
:
4435 case SpvOpCapability
:
4436 case SpvOpExtInstImport
:
4437 case SpvOpMemoryModel
:
4438 case SpvOpEntryPoint
:
4439 case SpvOpExecutionMode
:
4442 case SpvOpMemberName
:
4443 case SpvOpDecorationGroup
:
4445 case SpvOpDecorateId
:
4446 case SpvOpMemberDecorate
:
4447 case SpvOpGroupDecorate
:
4448 case SpvOpGroupMemberDecorate
:
4449 case SpvOpDecorateString
:
4450 case SpvOpMemberDecorateString
:
4451 vtn_fail("Invalid opcode types and variables section");
4457 case SpvOpTypeFloat
:
4458 case SpvOpTypeVector
:
4459 case SpvOpTypeMatrix
:
4460 case SpvOpTypeImage
:
4461 case SpvOpTypeSampler
:
4462 case SpvOpTypeSampledImage
:
4463 case SpvOpTypeArray
:
4464 case SpvOpTypeRuntimeArray
:
4465 case SpvOpTypeStruct
:
4466 case SpvOpTypeOpaque
:
4467 case SpvOpTypePointer
:
4468 case SpvOpTypeForwardPointer
:
4469 case SpvOpTypeFunction
:
4470 case SpvOpTypeEvent
:
4471 case SpvOpTypeDeviceEvent
:
4472 case SpvOpTypeReserveId
:
4473 case SpvOpTypeQueue
:
4475 vtn_handle_type(b
, opcode
, w
, count
);
4478 case SpvOpConstantTrue
:
4479 case SpvOpConstantFalse
:
4481 case SpvOpConstantComposite
:
4482 case SpvOpConstantSampler
:
4483 case SpvOpConstantNull
:
4484 case SpvOpSpecConstantTrue
:
4485 case SpvOpSpecConstantFalse
:
4486 case SpvOpSpecConstant
:
4487 case SpvOpSpecConstantComposite
:
4488 case SpvOpSpecConstantOp
:
4489 vtn_handle_constant(b
, opcode
, w
, count
);
4494 vtn_handle_variables(b
, opcode
, w
, count
);
4497 case SpvOpExtInst
: {
4498 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4499 /* NonSemantic extended instructions are acceptable in preamble, others
4500 * will indicate the end of preamble.
4502 return val
->ext_handler
== vtn_handle_non_semantic_instruction
;
4506 return false; /* End of preamble */
4512 static struct vtn_ssa_value
*
4513 vtn_nir_select(struct vtn_builder
*b
, struct vtn_ssa_value
*src0
,
4514 struct vtn_ssa_value
*src1
, struct vtn_ssa_value
*src2
)
4516 struct vtn_ssa_value
*dest
= rzalloc(b
, struct vtn_ssa_value
);
4517 dest
->type
= src1
->type
;
4519 if (glsl_type_is_vector_or_scalar(src1
->type
)) {
4520 dest
->def
= nir_bcsel(&b
->nb
, src0
->def
, src1
->def
, src2
->def
);
4522 unsigned elems
= glsl_get_length(src1
->type
);
4524 dest
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
4525 for (unsigned i
= 0; i
< elems
; i
++) {
4526 dest
->elems
[i
] = vtn_nir_select(b
, src0
,
4527 src1
->elems
[i
], src2
->elems
[i
]);
4535 vtn_handle_select(struct vtn_builder
*b
, SpvOp opcode
,
4536 const uint32_t *w
, unsigned count
)
4538 /* Handle OpSelect up-front here because it needs to be able to handle
4539 * pointers and not just regular vectors and scalars.
4541 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
4542 struct vtn_value
*cond_val
= vtn_untyped_value(b
, w
[3]);
4543 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
4544 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
4546 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4547 obj2_val
->type
!= res_val
->type
,
4548 "Object types must match the result type in OpSelect");
4550 vtn_fail_if((cond_val
->type
->base_type
!= vtn_base_type_scalar
&&
4551 cond_val
->type
->base_type
!= vtn_base_type_vector
) ||
4552 !glsl_type_is_boolean(cond_val
->type
->type
),
4553 "OpSelect must have either a vector of booleans or "
4554 "a boolean as Condition type");
4556 vtn_fail_if(cond_val
->type
->base_type
== vtn_base_type_vector
&&
4557 (res_val
->type
->base_type
!= vtn_base_type_vector
||
4558 res_val
->type
->length
!= cond_val
->type
->length
),
4559 "When Condition type in OpSelect is a vector, the Result "
4560 "type must be a vector of the same length");
4562 switch (res_val
->type
->base_type
) {
4563 case vtn_base_type_scalar
:
4564 case vtn_base_type_vector
:
4565 case vtn_base_type_matrix
:
4566 case vtn_base_type_array
:
4567 case vtn_base_type_struct
:
4570 case vtn_base_type_pointer
:
4571 /* We need to have actual storage for pointer types. */
4572 vtn_fail_if(res_val
->type
->type
== NULL
,
4573 "Invalid pointer result type for OpSelect");
4576 vtn_fail("Result type of OpSelect must be a scalar, composite, or pointer");
4579 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4580 struct vtn_ssa_value
*ssa
= vtn_nir_select(b
,
4581 vtn_ssa_value(b
, w
[3]), vtn_ssa_value(b
, w
[4]), vtn_ssa_value(b
, w
[5]));
4583 vtn_push_ssa(b
, w
[2], res_type
, ssa
);
4587 vtn_handle_ptr(struct vtn_builder
*b
, SpvOp opcode
,
4588 const uint32_t *w
, unsigned count
)
4590 struct vtn_type
*type1
= vtn_untyped_value(b
, w
[3])->type
;
4591 struct vtn_type
*type2
= vtn_untyped_value(b
, w
[4])->type
;
4592 vtn_fail_if(type1
->base_type
!= vtn_base_type_pointer
||
4593 type2
->base_type
!= vtn_base_type_pointer
,
4594 "%s operands must have pointer types",
4595 spirv_op_to_string(opcode
));
4596 vtn_fail_if(type1
->storage_class
!= type2
->storage_class
,
4597 "%s operands must have the same storage class",
4598 spirv_op_to_string(opcode
));
4600 struct vtn_type
*vtn_type
=
4601 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4602 const struct glsl_type
*type
= vtn_type
->type
;
4604 nir_address_format addr_format
= vtn_mode_to_address_format(
4605 b
, vtn_storage_class_to_mode(b
, type1
->storage_class
, NULL
, NULL
));
4610 case SpvOpPtrDiff
: {
4611 /* OpPtrDiff returns the difference in number of elements (not byte offset). */
4612 unsigned elem_size
, elem_align
;
4613 glsl_get_natural_size_align_bytes(type1
->deref
->type
,
4614 &elem_size
, &elem_align
);
4616 def
= nir_build_addr_isub(&b
->nb
,
4617 vtn_ssa_value(b
, w
[3])->def
,
4618 vtn_ssa_value(b
, w
[4])->def
,
4620 def
= nir_idiv(&b
->nb
, def
, nir_imm_intN_t(&b
->nb
, elem_size
, def
->bit_size
));
4621 def
= nir_i2i(&b
->nb
, def
, glsl_get_bit_size(type
));
4626 case SpvOpPtrNotEqual
: {
4627 def
= nir_build_addr_ieq(&b
->nb
,
4628 vtn_ssa_value(b
, w
[3])->def
,
4629 vtn_ssa_value(b
, w
[4])->def
,
4631 if (opcode
== SpvOpPtrNotEqual
)
4632 def
= nir_inot(&b
->nb
, def
);
4637 unreachable("Invalid ptr operation");
4640 struct vtn_ssa_value
*ssa_value
= vtn_create_ssa_value(b
, type
);
4641 ssa_value
->def
= def
;
4642 vtn_push_ssa(b
, w
[2], vtn_type
, ssa_value
);
4646 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4647 const uint32_t *w
, unsigned count
)
4653 case SpvOpLoopMerge
:
4654 case SpvOpSelectionMerge
:
4655 /* This is handled by cfg pre-pass and walk_blocks */
4659 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
4660 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4665 vtn_handle_extension(b
, opcode
, w
, count
);
4671 case SpvOpCopyMemory
:
4672 case SpvOpCopyMemorySized
:
4673 case SpvOpAccessChain
:
4674 case SpvOpPtrAccessChain
:
4675 case SpvOpInBoundsAccessChain
:
4676 case SpvOpInBoundsPtrAccessChain
:
4677 case SpvOpArrayLength
:
4678 case SpvOpConvertPtrToU
:
4679 case SpvOpConvertUToPtr
:
4680 vtn_handle_variables(b
, opcode
, w
, count
);
4683 case SpvOpFunctionCall
:
4684 vtn_handle_function_call(b
, opcode
, w
, count
);
4687 case SpvOpSampledImage
:
4689 case SpvOpImageSampleImplicitLod
:
4690 case SpvOpImageSampleExplicitLod
:
4691 case SpvOpImageSampleDrefImplicitLod
:
4692 case SpvOpImageSampleDrefExplicitLod
:
4693 case SpvOpImageSampleProjImplicitLod
:
4694 case SpvOpImageSampleProjExplicitLod
:
4695 case SpvOpImageSampleProjDrefImplicitLod
:
4696 case SpvOpImageSampleProjDrefExplicitLod
:
4697 case SpvOpImageFetch
:
4698 case SpvOpImageGather
:
4699 case SpvOpImageDrefGather
:
4700 case SpvOpImageQuerySizeLod
:
4701 case SpvOpImageQueryLod
:
4702 case SpvOpImageQueryLevels
:
4703 case SpvOpImageQuerySamples
:
4704 vtn_handle_texture(b
, opcode
, w
, count
);
4707 case SpvOpImageRead
:
4708 case SpvOpImageWrite
:
4709 case SpvOpImageTexelPointer
:
4710 vtn_handle_image(b
, opcode
, w
, count
);
4713 case SpvOpImageQuerySize
: {
4714 struct vtn_pointer
*image
=
4715 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
4716 if (glsl_type_is_image(image
->type
->type
)) {
4717 vtn_handle_image(b
, opcode
, w
, count
);
4719 vtn_assert(glsl_type_is_sampler(image
->type
->type
));
4720 vtn_handle_texture(b
, opcode
, w
, count
);
4725 case SpvOpAtomicLoad
:
4726 case SpvOpAtomicExchange
:
4727 case SpvOpAtomicCompareExchange
:
4728 case SpvOpAtomicCompareExchangeWeak
:
4729 case SpvOpAtomicIIncrement
:
4730 case SpvOpAtomicIDecrement
:
4731 case SpvOpAtomicIAdd
:
4732 case SpvOpAtomicISub
:
4733 case SpvOpAtomicSMin
:
4734 case SpvOpAtomicUMin
:
4735 case SpvOpAtomicSMax
:
4736 case SpvOpAtomicUMax
:
4737 case SpvOpAtomicAnd
:
4739 case SpvOpAtomicXor
: {
4740 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
4741 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4742 vtn_handle_image(b
, opcode
, w
, count
);
4744 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4745 vtn_handle_atomics(b
, opcode
, w
, count
);
4750 case SpvOpAtomicStore
: {
4751 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
4752 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4753 vtn_handle_image(b
, opcode
, w
, count
);
4755 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4756 vtn_handle_atomics(b
, opcode
, w
, count
);
4762 vtn_handle_select(b
, opcode
, w
, count
);
4770 case SpvOpConvertFToU
:
4771 case SpvOpConvertFToS
:
4772 case SpvOpConvertSToF
:
4773 case SpvOpConvertUToF
:
4777 case SpvOpQuantizeToF16
:
4778 case SpvOpPtrCastToGeneric
:
4779 case SpvOpGenericCastToPtr
:
4784 case SpvOpSignBitSet
:
4785 case SpvOpLessOrGreater
:
4787 case SpvOpUnordered
:
4802 case SpvOpVectorTimesScalar
:
4804 case SpvOpIAddCarry
:
4805 case SpvOpISubBorrow
:
4806 case SpvOpUMulExtended
:
4807 case SpvOpSMulExtended
:
4808 case SpvOpShiftRightLogical
:
4809 case SpvOpShiftRightArithmetic
:
4810 case SpvOpShiftLeftLogical
:
4811 case SpvOpLogicalEqual
:
4812 case SpvOpLogicalNotEqual
:
4813 case SpvOpLogicalOr
:
4814 case SpvOpLogicalAnd
:
4815 case SpvOpLogicalNot
:
4816 case SpvOpBitwiseOr
:
4817 case SpvOpBitwiseXor
:
4818 case SpvOpBitwiseAnd
:
4820 case SpvOpFOrdEqual
:
4821 case SpvOpFUnordEqual
:
4822 case SpvOpINotEqual
:
4823 case SpvOpFOrdNotEqual
:
4824 case SpvOpFUnordNotEqual
:
4825 case SpvOpULessThan
:
4826 case SpvOpSLessThan
:
4827 case SpvOpFOrdLessThan
:
4828 case SpvOpFUnordLessThan
:
4829 case SpvOpUGreaterThan
:
4830 case SpvOpSGreaterThan
:
4831 case SpvOpFOrdGreaterThan
:
4832 case SpvOpFUnordGreaterThan
:
4833 case SpvOpULessThanEqual
:
4834 case SpvOpSLessThanEqual
:
4835 case SpvOpFOrdLessThanEqual
:
4836 case SpvOpFUnordLessThanEqual
:
4837 case SpvOpUGreaterThanEqual
:
4838 case SpvOpSGreaterThanEqual
:
4839 case SpvOpFOrdGreaterThanEqual
:
4840 case SpvOpFUnordGreaterThanEqual
:
4846 case SpvOpFwidthFine
:
4847 case SpvOpDPdxCoarse
:
4848 case SpvOpDPdyCoarse
:
4849 case SpvOpFwidthCoarse
:
4850 case SpvOpBitFieldInsert
:
4851 case SpvOpBitFieldSExtract
:
4852 case SpvOpBitFieldUExtract
:
4853 case SpvOpBitReverse
:
4855 case SpvOpTranspose
:
4856 case SpvOpOuterProduct
:
4857 case SpvOpMatrixTimesScalar
:
4858 case SpvOpVectorTimesMatrix
:
4859 case SpvOpMatrixTimesVector
:
4860 case SpvOpMatrixTimesMatrix
:
4861 vtn_handle_alu(b
, opcode
, w
, count
);
4865 vtn_handle_bitcast(b
, w
, count
);
4868 case SpvOpVectorExtractDynamic
:
4869 case SpvOpVectorInsertDynamic
:
4870 case SpvOpVectorShuffle
:
4871 case SpvOpCompositeConstruct
:
4872 case SpvOpCompositeExtract
:
4873 case SpvOpCompositeInsert
:
4874 case SpvOpCopyLogical
:
4875 case SpvOpCopyObject
:
4876 vtn_handle_composite(b
, opcode
, w
, count
);
4879 case SpvOpEmitVertex
:
4880 case SpvOpEndPrimitive
:
4881 case SpvOpEmitStreamVertex
:
4882 case SpvOpEndStreamPrimitive
:
4883 case SpvOpControlBarrier
:
4884 case SpvOpMemoryBarrier
:
4885 vtn_handle_barrier(b
, opcode
, w
, count
);
4888 case SpvOpGroupNonUniformElect
:
4889 case SpvOpGroupNonUniformAll
:
4890 case SpvOpGroupNonUniformAny
:
4891 case SpvOpGroupNonUniformAllEqual
:
4892 case SpvOpGroupNonUniformBroadcast
:
4893 case SpvOpGroupNonUniformBroadcastFirst
:
4894 case SpvOpGroupNonUniformBallot
:
4895 case SpvOpGroupNonUniformInverseBallot
:
4896 case SpvOpGroupNonUniformBallotBitExtract
:
4897 case SpvOpGroupNonUniformBallotBitCount
:
4898 case SpvOpGroupNonUniformBallotFindLSB
:
4899 case SpvOpGroupNonUniformBallotFindMSB
:
4900 case SpvOpGroupNonUniformShuffle
:
4901 case SpvOpGroupNonUniformShuffleXor
:
4902 case SpvOpGroupNonUniformShuffleUp
:
4903 case SpvOpGroupNonUniformShuffleDown
:
4904 case SpvOpGroupNonUniformIAdd
:
4905 case SpvOpGroupNonUniformFAdd
:
4906 case SpvOpGroupNonUniformIMul
:
4907 case SpvOpGroupNonUniformFMul
:
4908 case SpvOpGroupNonUniformSMin
:
4909 case SpvOpGroupNonUniformUMin
:
4910 case SpvOpGroupNonUniformFMin
:
4911 case SpvOpGroupNonUniformSMax
:
4912 case SpvOpGroupNonUniformUMax
:
4913 case SpvOpGroupNonUniformFMax
:
4914 case SpvOpGroupNonUniformBitwiseAnd
:
4915 case SpvOpGroupNonUniformBitwiseOr
:
4916 case SpvOpGroupNonUniformBitwiseXor
:
4917 case SpvOpGroupNonUniformLogicalAnd
:
4918 case SpvOpGroupNonUniformLogicalOr
:
4919 case SpvOpGroupNonUniformLogicalXor
:
4920 case SpvOpGroupNonUniformQuadBroadcast
:
4921 case SpvOpGroupNonUniformQuadSwap
:
4924 case SpvOpGroupBroadcast
:
4925 case SpvOpGroupIAdd
:
4926 case SpvOpGroupFAdd
:
4927 case SpvOpGroupFMin
:
4928 case SpvOpGroupUMin
:
4929 case SpvOpGroupSMin
:
4930 case SpvOpGroupFMax
:
4931 case SpvOpGroupUMax
:
4932 case SpvOpGroupSMax
:
4933 case SpvOpSubgroupBallotKHR
:
4934 case SpvOpSubgroupFirstInvocationKHR
:
4935 case SpvOpSubgroupReadInvocationKHR
:
4936 case SpvOpSubgroupAllKHR
:
4937 case SpvOpSubgroupAnyKHR
:
4938 case SpvOpSubgroupAllEqualKHR
:
4939 case SpvOpGroupIAddNonUniformAMD
:
4940 case SpvOpGroupFAddNonUniformAMD
:
4941 case SpvOpGroupFMinNonUniformAMD
:
4942 case SpvOpGroupUMinNonUniformAMD
:
4943 case SpvOpGroupSMinNonUniformAMD
:
4944 case SpvOpGroupFMaxNonUniformAMD
:
4945 case SpvOpGroupUMaxNonUniformAMD
:
4946 case SpvOpGroupSMaxNonUniformAMD
:
4947 vtn_handle_subgroup(b
, opcode
, w
, count
);
4952 case SpvOpPtrNotEqual
:
4953 vtn_handle_ptr(b
, opcode
, w
, count
);
4956 case SpvOpBeginInvocationInterlockEXT
:
4957 vtn_emit_barrier(b
, nir_intrinsic_begin_invocation_interlock
);
4960 case SpvOpEndInvocationInterlockEXT
:
4961 vtn_emit_barrier(b
, nir_intrinsic_end_invocation_interlock
);
4964 case SpvOpDemoteToHelperInvocationEXT
: {
4965 nir_intrinsic_instr
*intrin
=
4966 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_demote
);
4967 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
4971 case SpvOpIsHelperInvocationEXT
: {
4972 nir_intrinsic_instr
*intrin
=
4973 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_is_helper_invocation
);
4974 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 1, 1, NULL
);
4975 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
4977 struct vtn_type
*res_type
=
4978 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4979 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, res_type
->type
);
4980 val
->def
= &intrin
->dest
.ssa
;
4982 vtn_push_ssa(b
, w
[2], res_type
, val
);
4986 case SpvOpReadClockKHR
: {
4987 assert(vtn_constant_uint(b
, w
[3]) == SpvScopeSubgroup
);
4989 /* Operation supports two result types: uvec2 and uint64_t. The NIR
4990 * intrinsic gives uvec2, so pack the result for the other case.
4992 nir_intrinsic_instr
*intrin
=
4993 nir_intrinsic_instr_create(b
->nb
.shader
, nir_intrinsic_shader_clock
);
4994 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 2, 32, NULL
);
4995 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
4997 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4998 const struct glsl_type
*dest_type
= type
->type
;
4999 nir_ssa_def
*result
;
5001 if (glsl_type_is_vector(dest_type
)) {
5002 assert(dest_type
== glsl_vector_type(GLSL_TYPE_UINT
, 2));
5003 result
= &intrin
->dest
.ssa
;
5005 assert(glsl_type_is_scalar(dest_type
));
5006 assert(glsl_get_base_type(dest_type
) == GLSL_TYPE_UINT64
);
5007 result
= nir_pack_64_2x32(&b
->nb
, &intrin
->dest
.ssa
);
5010 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
5012 val
->ssa
= vtn_create_ssa_value(b
, dest_type
);
5013 val
->ssa
->def
= result
;
5017 case SpvOpLifetimeStart
:
5018 case SpvOpLifetimeStop
:
5022 vtn_fail_with_opcode("Unhandled opcode", opcode
);
5029 vtn_create_builder(const uint32_t *words
, size_t word_count
,
5030 gl_shader_stage stage
, const char *entry_point_name
,
5031 const struct spirv_to_nir_options
*options
)
5033 /* Initialize the vtn_builder object */
5034 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
5035 struct spirv_to_nir_options
*dup_options
=
5036 ralloc(b
, struct spirv_to_nir_options
);
5037 *dup_options
= *options
;
5040 b
->spirv_word_count
= word_count
;
5044 exec_list_make_empty(&b
->functions
);
5045 b
->entry_point_stage
= stage
;
5046 b
->entry_point_name
= entry_point_name
;
5047 b
->options
= dup_options
;
5050 * Handle the SPIR-V header (first 5 dwords).
5051 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
5053 if (word_count
<= 5)
5056 if (words
[0] != SpvMagicNumber
) {
5057 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
5060 if (words
[1] < 0x10000) {
5061 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
5065 uint16_t generator_id
= words
[2] >> 16;
5066 uint16_t generator_version
= words
[2];
5068 /* The first GLSLang version bump actually 1.5 years after #179 was fixed
5069 * but this should at least let us shut the workaround off for modern
5070 * versions of GLSLang.
5072 b
->wa_glslang_179
= (generator_id
== 8 && generator_version
== 1);
5074 /* words[2] == generator magic */
5075 unsigned value_id_bound
= words
[3];
5076 if (words
[4] != 0) {
5077 vtn_err("words[4] was %u, want 0", words
[4]);
5081 b
->value_id_bound
= value_id_bound
;
5082 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
5090 static nir_function
*
5091 vtn_emit_kernel_entry_point_wrapper(struct vtn_builder
*b
,
5092 nir_function
*entry_point
)
5094 vtn_assert(entry_point
== b
->entry_point
->func
->impl
->function
);
5095 vtn_fail_if(!entry_point
->name
, "entry points are required to have a name");
5096 const char *func_name
=
5097 ralloc_asprintf(b
->shader
, "__wrapped_%s", entry_point
->name
);
5099 /* we shouldn't have any inputs yet */
5100 vtn_assert(!entry_point
->shader
->num_inputs
);
5101 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
);
5103 nir_function
*main_entry_point
= nir_function_create(b
->shader
, func_name
);
5104 main_entry_point
->impl
= nir_function_impl_create(main_entry_point
);
5105 nir_builder_init(&b
->nb
, main_entry_point
->impl
);
5106 b
->nb
.cursor
= nir_after_cf_list(&main_entry_point
->impl
->body
);
5107 b
->func_param_idx
= 0;
5109 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, entry_point
);
5111 for (unsigned i
= 0; i
< entry_point
->num_params
; ++i
) {
5112 struct vtn_type
*param_type
= b
->entry_point
->func
->type
->params
[i
];
5114 /* consider all pointers to function memory to be parameters passed
5117 bool is_by_val
= param_type
->base_type
== vtn_base_type_pointer
&&
5118 param_type
->storage_class
== SpvStorageClassFunction
;
5120 /* input variable */
5121 nir_variable
*in_var
= rzalloc(b
->nb
.shader
, nir_variable
);
5122 in_var
->data
.mode
= nir_var_shader_in
;
5123 in_var
->data
.read_only
= true;
5124 in_var
->data
.location
= i
;
5127 in_var
->type
= param_type
->deref
->type
;
5129 in_var
->type
= param_type
->type
;
5131 nir_shader_add_variable(b
->nb
.shader
, in_var
);
5132 b
->nb
.shader
->num_inputs
++;
5134 /* we have to copy the entire variable into function memory */
5136 nir_variable
*copy_var
=
5137 nir_local_variable_create(main_entry_point
->impl
, in_var
->type
,
5139 nir_copy_var(&b
->nb
, copy_var
, in_var
);
5141 nir_src_for_ssa(&nir_build_deref_var(&b
->nb
, copy_var
)->dest
.ssa
);
5143 call
->params
[i
] = nir_src_for_ssa(nir_load_var(&b
->nb
, in_var
));
5147 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
5149 return main_entry_point
;
5153 spirv_to_nir(const uint32_t *words
, size_t word_count
,
5154 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
5155 gl_shader_stage stage
, const char *entry_point_name
,
5156 const struct spirv_to_nir_options
*options
,
5157 const nir_shader_compiler_options
*nir_options
)
5160 const uint32_t *word_end
= words
+ word_count
;
5162 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
5163 stage
, entry_point_name
,
5169 /* See also _vtn_fail() */
5170 if (setjmp(b
->fail_jump
)) {
5175 /* Skip the SPIR-V header, handled at vtn_create_builder */
5178 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
5180 /* Handle all the preamble instructions */
5181 words
= vtn_foreach_instruction(b
, words
, word_end
,
5182 vtn_handle_preamble_instruction
);
5184 if (b
->entry_point
== NULL
) {
5185 vtn_fail("Entry point not found");
5190 /* Set shader info defaults */
5191 if (stage
== MESA_SHADER_GEOMETRY
)
5192 b
->shader
->info
.gs
.invocations
= 1;
5194 /* Parse rounding mode execution modes. This has to happen earlier than
5195 * other changes in the execution modes since they can affect, for example,
5196 * the result of the floating point constants.
5198 vtn_foreach_execution_mode(b
, b
->entry_point
,
5199 vtn_handle_rounding_mode_in_execution_mode
, NULL
);
5201 b
->specializations
= spec
;
5202 b
->num_specializations
= num_spec
;
5204 /* Handle all variable, type, and constant instructions */
5205 words
= vtn_foreach_instruction(b
, words
, word_end
,
5206 vtn_handle_variable_or_type_instruction
);
5208 /* Parse execution modes */
5209 vtn_foreach_execution_mode(b
, b
->entry_point
,
5210 vtn_handle_execution_mode
, NULL
);
5212 if (b
->workgroup_size_builtin
) {
5213 vtn_assert(b
->workgroup_size_builtin
->type
->type
==
5214 glsl_vector_type(GLSL_TYPE_UINT
, 3));
5216 nir_const_value
*const_size
=
5217 b
->workgroup_size_builtin
->constant
->values
;
5219 b
->shader
->info
.cs
.local_size
[0] = const_size
[0].u32
;
5220 b
->shader
->info
.cs
.local_size
[1] = const_size
[1].u32
;
5221 b
->shader
->info
.cs
.local_size
[2] = const_size
[2].u32
;
5224 /* Set types on all vtn_values */
5225 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
5227 vtn_build_cfg(b
, words
, word_end
);
5229 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5230 b
->entry_point
->func
->referenced
= true;
5235 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
5236 if (func
->referenced
&& !func
->emitted
) {
5237 b
->const_table
= _mesa_pointer_hash_table_create(b
);
5239 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
5245 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5246 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
5247 vtn_assert(entry_point
);
5249 /* post process entry_points with input params */
5250 if (entry_point
->num_params
&& b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
5251 entry_point
= vtn_emit_kernel_entry_point_wrapper(b
, entry_point
);
5253 entry_point
->is_entrypoint
= true;
5255 /* When multiple shader stages exist in the same SPIR-V module, we
5256 * generate input and output variables for every stage, in the same
5257 * NIR program. These dead variables can be invalid NIR. For example,
5258 * TCS outputs must be per-vertex arrays (or decorated 'patch'), while
5259 * VS output variables wouldn't be.
5261 * To ensure we have valid NIR, we eliminate any dead inputs and outputs
5262 * right away. In order to do so, we must lower any constant initializers
5263 * on outputs so nir_remove_dead_variables sees that they're written to.
5265 nir_lower_constant_initializers(b
->shader
, nir_var_shader_out
);
5266 nir_remove_dead_variables(b
->shader
,
5267 nir_var_shader_in
| nir_var_shader_out
);
5269 /* We sometimes generate bogus derefs that, while never used, give the
5270 * validator a bit of heartburn. Run dead code to get rid of them.
5272 nir_opt_dce(b
->shader
);
5274 /* Unparent the shader from the vtn_builder before we delete the builder */
5275 ralloc_steal(NULL
, b
->shader
);
5277 nir_shader
*shader
= b
->shader
;