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_extension(struct vtn_builder
*b
, SpvOp opcode
,
376 const uint32_t *w
, unsigned count
)
378 const char *ext
= (const char *)&w
[2];
380 case SpvOpExtInstImport
: {
381 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
382 if (strcmp(ext
, "GLSL.std.450") == 0) {
383 val
->ext_handler
= vtn_handle_glsl450_instruction
;
384 } else if ((strcmp(ext
, "SPV_AMD_gcn_shader") == 0)
385 && (b
->options
&& b
->options
->caps
.amd_gcn_shader
)) {
386 val
->ext_handler
= vtn_handle_amd_gcn_shader_instruction
;
387 } else if ((strcmp(ext
, "SPV_AMD_shader_ballot") == 0)
388 && (b
->options
&& b
->options
->caps
.amd_shader_ballot
)) {
389 val
->ext_handler
= vtn_handle_amd_shader_ballot_instruction
;
390 } else if ((strcmp(ext
, "SPV_AMD_shader_trinary_minmax") == 0)
391 && (b
->options
&& b
->options
->caps
.amd_trinary_minmax
)) {
392 val
->ext_handler
= vtn_handle_amd_shader_trinary_minmax_instruction
;
393 } else if (strcmp(ext
, "OpenCL.std") == 0) {
394 val
->ext_handler
= vtn_handle_opencl_instruction
;
396 vtn_fail("Unsupported extension: %s", ext
);
402 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
403 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
409 vtn_fail_with_opcode("Unhandled opcode", opcode
);
414 _foreach_decoration_helper(struct vtn_builder
*b
,
415 struct vtn_value
*base_value
,
417 struct vtn_value
*value
,
418 vtn_decoration_foreach_cb cb
, void *data
)
420 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
422 if (dec
->scope
== VTN_DEC_DECORATION
) {
423 member
= parent_member
;
424 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
425 vtn_fail_if(value
->value_type
!= vtn_value_type_type
||
426 value
->type
->base_type
!= vtn_base_type_struct
,
427 "OpMemberDecorate and OpGroupMemberDecorate are only "
428 "allowed on OpTypeStruct");
429 /* This means we haven't recursed yet */
430 assert(value
== base_value
);
432 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
434 vtn_fail_if(member
>= base_value
->type
->length
,
435 "OpMemberDecorate specifies member %d but the "
436 "OpTypeStruct has only %u members",
437 member
, base_value
->type
->length
);
439 /* Not a decoration */
440 assert(dec
->scope
== VTN_DEC_EXECUTION_MODE
);
445 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
446 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
449 cb(b
, base_value
, member
, dec
, data
);
454 /** Iterates (recursively if needed) over all of the decorations on a value
456 * This function iterates over all of the decorations applied to a given
457 * value. If it encounters a decoration group, it recurses into the group
458 * and iterates over all of those decorations as well.
461 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
462 vtn_decoration_foreach_cb cb
, void *data
)
464 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
468 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
469 vtn_execution_mode_foreach_cb cb
, void *data
)
471 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
472 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
475 assert(dec
->group
== NULL
);
476 cb(b
, value
, dec
, data
);
481 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
482 const uint32_t *w
, unsigned count
)
484 const uint32_t *w_end
= w
+ count
;
485 const uint32_t target
= w
[1];
489 case SpvOpDecorationGroup
:
490 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
494 case SpvOpDecorateId
:
495 case SpvOpMemberDecorate
:
496 case SpvOpDecorateString
:
497 case SpvOpMemberDecorateString
:
498 case SpvOpExecutionMode
:
499 case SpvOpExecutionModeId
: {
500 struct vtn_value
*val
= vtn_untyped_value(b
, target
);
502 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
505 case SpvOpDecorateId
:
506 case SpvOpDecorateString
:
507 dec
->scope
= VTN_DEC_DECORATION
;
509 case SpvOpMemberDecorate
:
510 case SpvOpMemberDecorateString
:
511 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
512 vtn_fail_if(dec
->scope
< VTN_DEC_STRUCT_MEMBER0
, /* overflow */
513 "Member argument of OpMemberDecorate too large");
515 case SpvOpExecutionMode
:
516 case SpvOpExecutionModeId
:
517 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
520 unreachable("Invalid decoration opcode");
522 dec
->decoration
= *(w
++);
525 /* Link into the list */
526 dec
->next
= val
->decoration
;
527 val
->decoration
= dec
;
531 case SpvOpGroupMemberDecorate
:
532 case SpvOpGroupDecorate
: {
533 struct vtn_value
*group
=
534 vtn_value(b
, target
, vtn_value_type_decoration_group
);
536 for (; w
< w_end
; w
++) {
537 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
538 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
541 if (opcode
== SpvOpGroupDecorate
) {
542 dec
->scope
= VTN_DEC_DECORATION
;
544 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
545 vtn_fail_if(dec
->scope
< 0, /* Check for overflow */
546 "Member argument of OpGroupMemberDecorate too large");
549 /* Link into the list */
550 dec
->next
= val
->decoration
;
551 val
->decoration
= dec
;
557 unreachable("Unhandled opcode");
561 struct member_decoration_ctx
{
563 struct glsl_struct_field
*fields
;
564 struct vtn_type
*type
;
568 * Returns true if the given type contains a struct decorated Block or
572 vtn_type_contains_block(struct vtn_builder
*b
, struct vtn_type
*type
)
574 switch (type
->base_type
) {
575 case vtn_base_type_array
:
576 return vtn_type_contains_block(b
, type
->array_element
);
577 case vtn_base_type_struct
:
578 if (type
->block
|| type
->buffer_block
)
580 for (unsigned i
= 0; i
< type
->length
; i
++) {
581 if (vtn_type_contains_block(b
, type
->members
[i
]))
590 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
591 * OpStore, or OpCopyMemory between them without breaking anything.
592 * Technically, the SPIR-V rules require the exact same type ID but this lets
593 * us internally be a bit looser.
596 vtn_types_compatible(struct vtn_builder
*b
,
597 struct vtn_type
*t1
, struct vtn_type
*t2
)
599 if (t1
->id
== t2
->id
)
602 if (t1
->base_type
!= t2
->base_type
)
605 switch (t1
->base_type
) {
606 case vtn_base_type_void
:
607 case vtn_base_type_scalar
:
608 case vtn_base_type_vector
:
609 case vtn_base_type_matrix
:
610 case vtn_base_type_image
:
611 case vtn_base_type_sampler
:
612 case vtn_base_type_sampled_image
:
613 return t1
->type
== t2
->type
;
615 case vtn_base_type_array
:
616 return t1
->length
== t2
->length
&&
617 vtn_types_compatible(b
, t1
->array_element
, t2
->array_element
);
619 case vtn_base_type_pointer
:
620 return vtn_types_compatible(b
, t1
->deref
, t2
->deref
);
622 case vtn_base_type_struct
:
623 if (t1
->length
!= t2
->length
)
626 for (unsigned i
= 0; i
< t1
->length
; i
++) {
627 if (!vtn_types_compatible(b
, t1
->members
[i
], t2
->members
[i
]))
632 case vtn_base_type_function
:
633 /* This case shouldn't get hit since you can't copy around function
634 * types. Just require them to be identical.
639 vtn_fail("Invalid base type");
643 vtn_type_without_array(struct vtn_type
*type
)
645 while (type
->base_type
== vtn_base_type_array
)
646 type
= type
->array_element
;
650 /* does a shallow copy of a vtn_type */
652 static struct vtn_type
*
653 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
655 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
658 switch (src
->base_type
) {
659 case vtn_base_type_void
:
660 case vtn_base_type_scalar
:
661 case vtn_base_type_vector
:
662 case vtn_base_type_matrix
:
663 case vtn_base_type_array
:
664 case vtn_base_type_pointer
:
665 case vtn_base_type_image
:
666 case vtn_base_type_sampler
:
667 case vtn_base_type_sampled_image
:
668 /* Nothing more to do */
671 case vtn_base_type_struct
:
672 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
673 memcpy(dest
->members
, src
->members
,
674 src
->length
* sizeof(src
->members
[0]));
676 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
677 memcpy(dest
->offsets
, src
->offsets
,
678 src
->length
* sizeof(src
->offsets
[0]));
681 case vtn_base_type_function
:
682 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
683 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
690 static struct vtn_type
*
691 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
693 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
694 type
= type
->members
[member
];
696 /* We may have an array of matrices.... Oh, joy! */
697 while (glsl_type_is_array(type
->type
)) {
698 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
699 type
= type
->array_element
;
702 vtn_assert(glsl_type_is_matrix(type
->type
));
708 vtn_handle_access_qualifier(struct vtn_builder
*b
, struct vtn_type
*type
,
709 int member
, enum gl_access_qualifier access
)
711 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
712 type
= type
->members
[member
];
714 type
->access
|= access
;
718 array_stride_decoration_cb(struct vtn_builder
*b
,
719 struct vtn_value
*val
, int member
,
720 const struct vtn_decoration
*dec
, void *void_ctx
)
722 struct vtn_type
*type
= val
->type
;
724 if (dec
->decoration
== SpvDecorationArrayStride
) {
725 if (vtn_type_contains_block(b
, type
)) {
726 vtn_warn("The ArrayStride decoration cannot be applied to an array "
727 "type which contains a structure type decorated Block "
729 /* Ignore the decoration */
731 vtn_fail_if(dec
->operands
[0] == 0, "ArrayStride must be non-zero");
732 type
->stride
= dec
->operands
[0];
738 struct_member_decoration_cb(struct vtn_builder
*b
,
739 struct vtn_value
*val
, int member
,
740 const struct vtn_decoration
*dec
, void *void_ctx
)
742 struct member_decoration_ctx
*ctx
= void_ctx
;
747 assert(member
< ctx
->num_fields
);
749 switch (dec
->decoration
) {
750 case SpvDecorationRelaxedPrecision
:
751 case SpvDecorationUniform
:
752 case SpvDecorationUniformId
:
753 break; /* FIXME: Do nothing with this for now. */
754 case SpvDecorationNonWritable
:
755 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_WRITEABLE
);
757 case SpvDecorationNonReadable
:
758 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_READABLE
);
760 case SpvDecorationVolatile
:
761 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_VOLATILE
);
763 case SpvDecorationCoherent
:
764 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_COHERENT
);
766 case SpvDecorationNoPerspective
:
767 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
769 case SpvDecorationFlat
:
770 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
772 case SpvDecorationCentroid
:
773 ctx
->fields
[member
].centroid
= true;
775 case SpvDecorationSample
:
776 ctx
->fields
[member
].sample
= true;
778 case SpvDecorationStream
:
779 /* Vulkan only allows one GS stream */
780 vtn_assert(dec
->operands
[0] == 0);
782 case SpvDecorationLocation
:
783 ctx
->fields
[member
].location
= dec
->operands
[0];
785 case SpvDecorationComponent
:
786 break; /* FIXME: What should we do with these? */
787 case SpvDecorationBuiltIn
:
788 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
789 ctx
->type
->members
[member
]->is_builtin
= true;
790 ctx
->type
->members
[member
]->builtin
= dec
->operands
[0];
791 ctx
->type
->builtin_block
= true;
793 case SpvDecorationOffset
:
794 ctx
->type
->offsets
[member
] = dec
->operands
[0];
795 ctx
->fields
[member
].offset
= dec
->operands
[0];
797 case SpvDecorationMatrixStride
:
798 /* Handled as a second pass */
800 case SpvDecorationColMajor
:
801 break; /* Nothing to do here. Column-major is the default. */
802 case SpvDecorationRowMajor
:
803 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
806 case SpvDecorationPatch
:
809 case SpvDecorationSpecId
:
810 case SpvDecorationBlock
:
811 case SpvDecorationBufferBlock
:
812 case SpvDecorationArrayStride
:
813 case SpvDecorationGLSLShared
:
814 case SpvDecorationGLSLPacked
:
815 case SpvDecorationInvariant
:
816 case SpvDecorationRestrict
:
817 case SpvDecorationAliased
:
818 case SpvDecorationConstant
:
819 case SpvDecorationIndex
:
820 case SpvDecorationBinding
:
821 case SpvDecorationDescriptorSet
:
822 case SpvDecorationLinkageAttributes
:
823 case SpvDecorationNoContraction
:
824 case SpvDecorationInputAttachmentIndex
:
825 vtn_warn("Decoration not allowed on struct members: %s",
826 spirv_decoration_to_string(dec
->decoration
));
829 case SpvDecorationXfbBuffer
:
830 case SpvDecorationXfbStride
:
831 vtn_warn("Vulkan does not have transform feedback");
834 case SpvDecorationCPacked
:
835 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
836 vtn_warn("Decoration only allowed for CL-style kernels: %s",
837 spirv_decoration_to_string(dec
->decoration
));
839 ctx
->type
->packed
= true;
842 case SpvDecorationSaturatedConversion
:
843 case SpvDecorationFuncParamAttr
:
844 case SpvDecorationFPRoundingMode
:
845 case SpvDecorationFPFastMathMode
:
846 case SpvDecorationAlignment
:
847 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
848 vtn_warn("Decoration only allowed for CL-style kernels: %s",
849 spirv_decoration_to_string(dec
->decoration
));
853 case SpvDecorationUserSemantic
:
854 /* User semantic decorations can safely be ignored by the driver. */
858 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
862 /** Chases the array type all the way down to the tail and rewrites the
863 * glsl_types to be based off the tail's glsl_type.
866 vtn_array_type_rewrite_glsl_type(struct vtn_type
*type
)
868 if (type
->base_type
!= vtn_base_type_array
)
871 vtn_array_type_rewrite_glsl_type(type
->array_element
);
873 type
->type
= glsl_array_type(type
->array_element
->type
,
874 type
->length
, type
->stride
);
877 /* Matrix strides are handled as a separate pass because we need to know
878 * whether the matrix is row-major or not first.
881 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
882 struct vtn_value
*val
, int member
,
883 const struct vtn_decoration
*dec
,
886 if (dec
->decoration
!= SpvDecorationMatrixStride
)
889 vtn_fail_if(member
< 0,
890 "The MatrixStride decoration is only allowed on members "
892 vtn_fail_if(dec
->operands
[0] == 0, "MatrixStride must be non-zero");
894 struct member_decoration_ctx
*ctx
= void_ctx
;
896 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
897 if (mat_type
->row_major
) {
898 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
899 mat_type
->stride
= mat_type
->array_element
->stride
;
900 mat_type
->array_element
->stride
= dec
->operands
[0];
902 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
903 dec
->operands
[0], true);
904 mat_type
->array_element
->type
= glsl_get_column_type(mat_type
->type
);
906 vtn_assert(mat_type
->array_element
->stride
> 0);
907 mat_type
->stride
= dec
->operands
[0];
909 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
910 dec
->operands
[0], false);
913 /* Now that we've replaced the glsl_type with a properly strided matrix
914 * type, rewrite the member type so that it's an array of the proper kind
917 vtn_array_type_rewrite_glsl_type(ctx
->type
->members
[member
]);
918 ctx
->fields
[member
].type
= ctx
->type
->members
[member
]->type
;
922 struct_block_decoration_cb(struct vtn_builder
*b
,
923 struct vtn_value
*val
, int member
,
924 const struct vtn_decoration
*dec
, void *ctx
)
929 struct vtn_type
*type
= val
->type
;
930 if (dec
->decoration
== SpvDecorationBlock
)
932 else if (dec
->decoration
== SpvDecorationBufferBlock
)
933 type
->buffer_block
= true;
937 type_decoration_cb(struct vtn_builder
*b
,
938 struct vtn_value
*val
, int member
,
939 const struct vtn_decoration
*dec
, void *ctx
)
941 struct vtn_type
*type
= val
->type
;
944 /* This should have been handled by OpTypeStruct */
945 assert(val
->type
->base_type
== vtn_base_type_struct
);
946 assert(member
>= 0 && member
< val
->type
->length
);
950 switch (dec
->decoration
) {
951 case SpvDecorationArrayStride
:
952 vtn_assert(type
->base_type
== vtn_base_type_array
||
953 type
->base_type
== vtn_base_type_pointer
);
955 case SpvDecorationBlock
:
956 vtn_assert(type
->base_type
== vtn_base_type_struct
);
957 vtn_assert(type
->block
);
959 case SpvDecorationBufferBlock
:
960 vtn_assert(type
->base_type
== vtn_base_type_struct
);
961 vtn_assert(type
->buffer_block
);
963 case SpvDecorationGLSLShared
:
964 case SpvDecorationGLSLPacked
:
965 /* Ignore these, since we get explicit offsets anyways */
968 case SpvDecorationRowMajor
:
969 case SpvDecorationColMajor
:
970 case SpvDecorationMatrixStride
:
971 case SpvDecorationBuiltIn
:
972 case SpvDecorationNoPerspective
:
973 case SpvDecorationFlat
:
974 case SpvDecorationPatch
:
975 case SpvDecorationCentroid
:
976 case SpvDecorationSample
:
977 case SpvDecorationVolatile
:
978 case SpvDecorationCoherent
:
979 case SpvDecorationNonWritable
:
980 case SpvDecorationNonReadable
:
981 case SpvDecorationUniform
:
982 case SpvDecorationUniformId
:
983 case SpvDecorationLocation
:
984 case SpvDecorationComponent
:
985 case SpvDecorationOffset
:
986 case SpvDecorationXfbBuffer
:
987 case SpvDecorationXfbStride
:
988 case SpvDecorationUserSemantic
:
989 vtn_warn("Decoration only allowed for struct members: %s",
990 spirv_decoration_to_string(dec
->decoration
));
993 case SpvDecorationStream
:
994 /* We don't need to do anything here, as stream is filled up when
995 * aplying the decoration to a variable, just check that if it is not a
996 * struct member, it should be a struct.
998 vtn_assert(type
->base_type
== vtn_base_type_struct
);
1001 case SpvDecorationRelaxedPrecision
:
1002 case SpvDecorationSpecId
:
1003 case SpvDecorationInvariant
:
1004 case SpvDecorationRestrict
:
1005 case SpvDecorationAliased
:
1006 case SpvDecorationConstant
:
1007 case SpvDecorationIndex
:
1008 case SpvDecorationBinding
:
1009 case SpvDecorationDescriptorSet
:
1010 case SpvDecorationLinkageAttributes
:
1011 case SpvDecorationNoContraction
:
1012 case SpvDecorationInputAttachmentIndex
:
1013 vtn_warn("Decoration not allowed on types: %s",
1014 spirv_decoration_to_string(dec
->decoration
));
1017 case SpvDecorationCPacked
:
1018 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
1019 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1020 spirv_decoration_to_string(dec
->decoration
));
1022 type
->packed
= true;
1025 case SpvDecorationSaturatedConversion
:
1026 case SpvDecorationFuncParamAttr
:
1027 case SpvDecorationFPRoundingMode
:
1028 case SpvDecorationFPFastMathMode
:
1029 case SpvDecorationAlignment
:
1030 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1031 spirv_decoration_to_string(dec
->decoration
));
1035 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1040 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
1043 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
1044 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
1045 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
1046 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
1047 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
1048 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
1049 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
1050 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
1051 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
1052 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
1053 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
1054 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
1055 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
1056 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
1057 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
1058 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
1059 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
1060 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
1061 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
1062 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
1063 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
1064 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
1065 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
1066 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
1067 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
1068 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
1069 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
1070 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
1071 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
1072 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
1073 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
1074 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
1075 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
1076 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
1077 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
1078 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
1079 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
1080 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
1081 case SpvImageFormatR16ui
: return 0x8234; /* GL_R16UI */
1082 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
1084 vtn_fail("Invalid image format: %s (%u)",
1085 spirv_imageformat_to_string(format
), format
);
1089 static struct vtn_type
*
1090 vtn_type_layout_std430(struct vtn_builder
*b
, struct vtn_type
*type
,
1091 uint32_t *size_out
, uint32_t *align_out
)
1093 switch (type
->base_type
) {
1094 case vtn_base_type_scalar
: {
1095 uint32_t comp_size
= glsl_type_is_boolean(type
->type
)
1096 ? 4 : glsl_get_bit_size(type
->type
) / 8;
1097 *size_out
= comp_size
;
1098 *align_out
= comp_size
;
1102 case vtn_base_type_vector
: {
1103 uint32_t comp_size
= glsl_type_is_boolean(type
->type
)
1104 ? 4 : glsl_get_bit_size(type
->type
) / 8;
1105 unsigned align_comps
= type
->length
== 3 ? 4 : type
->length
;
1106 *size_out
= comp_size
* type
->length
,
1107 *align_out
= comp_size
* align_comps
;
1111 case vtn_base_type_matrix
:
1112 case vtn_base_type_array
: {
1113 /* We're going to add an array stride */
1114 type
= vtn_type_copy(b
, type
);
1115 uint32_t elem_size
, elem_align
;
1116 type
->array_element
= vtn_type_layout_std430(b
, type
->array_element
,
1117 &elem_size
, &elem_align
);
1118 type
->stride
= vtn_align_u32(elem_size
, elem_align
);
1119 *size_out
= type
->stride
* type
->length
;
1120 *align_out
= elem_align
;
1124 case vtn_base_type_struct
: {
1125 /* We're going to add member offsets */
1126 type
= vtn_type_copy(b
, type
);
1127 uint32_t offset
= 0;
1129 for (unsigned i
= 0; i
< type
->length
; i
++) {
1130 uint32_t mem_size
, mem_align
;
1131 type
->members
[i
] = vtn_type_layout_std430(b
, type
->members
[i
],
1132 &mem_size
, &mem_align
);
1133 offset
= vtn_align_u32(offset
, mem_align
);
1134 type
->offsets
[i
] = offset
;
1136 align
= MAX2(align
, mem_align
);
1144 unreachable("Invalid SPIR-V type for std430");
1149 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
1150 const uint32_t *w
, unsigned count
)
1152 struct vtn_value
*val
= NULL
;
1154 /* In order to properly handle forward declarations, we have to defer
1155 * allocation for pointer types.
1157 if (opcode
!= SpvOpTypePointer
&& opcode
!= SpvOpTypeForwardPointer
) {
1158 val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1159 vtn_fail_if(val
->type
!= NULL
,
1160 "Only pointers can have forward declarations");
1161 val
->type
= rzalloc(b
, struct vtn_type
);
1162 val
->type
->id
= w
[1];
1167 val
->type
->base_type
= vtn_base_type_void
;
1168 val
->type
->type
= glsl_void_type();
1171 val
->type
->base_type
= vtn_base_type_scalar
;
1172 val
->type
->type
= glsl_bool_type();
1173 val
->type
->length
= 1;
1175 case SpvOpTypeInt
: {
1176 int bit_size
= w
[2];
1177 const bool signedness
= w
[3];
1178 val
->type
->base_type
= vtn_base_type_scalar
;
1181 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1184 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1187 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1190 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1193 vtn_fail("Invalid int bit size: %u", bit_size
);
1195 val
->type
->length
= 1;
1199 case SpvOpTypeFloat
: {
1200 int bit_size
= w
[2];
1201 val
->type
->base_type
= vtn_base_type_scalar
;
1204 val
->type
->type
= glsl_float16_t_type();
1207 val
->type
->type
= glsl_float_type();
1210 val
->type
->type
= glsl_double_type();
1213 vtn_fail("Invalid float bit size: %u", bit_size
);
1215 val
->type
->length
= 1;
1219 case SpvOpTypeVector
: {
1220 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1221 unsigned elems
= w
[3];
1223 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1224 "Base type for OpTypeVector must be a scalar");
1225 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1226 "Invalid component count for OpTypeVector");
1228 val
->type
->base_type
= vtn_base_type_vector
;
1229 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1230 val
->type
->length
= elems
;
1231 val
->type
->stride
= glsl_type_is_boolean(val
->type
->type
)
1232 ? 4 : glsl_get_bit_size(base
->type
) / 8;
1233 val
->type
->array_element
= base
;
1237 case SpvOpTypeMatrix
: {
1238 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1239 unsigned columns
= w
[3];
1241 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1242 "Base type for OpTypeMatrix must be a vector");
1243 vtn_fail_if(columns
< 2 || columns
> 4,
1244 "Invalid column count for OpTypeMatrix");
1246 val
->type
->base_type
= vtn_base_type_matrix
;
1247 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1248 glsl_get_vector_elements(base
->type
),
1250 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1251 "Unsupported base type for OpTypeMatrix");
1252 assert(!glsl_type_is_error(val
->type
->type
));
1253 val
->type
->length
= columns
;
1254 val
->type
->array_element
= base
;
1255 val
->type
->row_major
= false;
1256 val
->type
->stride
= 0;
1260 case SpvOpTypeRuntimeArray
:
1261 case SpvOpTypeArray
: {
1262 struct vtn_type
*array_element
=
1263 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1265 if (opcode
== SpvOpTypeRuntimeArray
) {
1266 /* A length of 0 is used to denote unsized arrays */
1267 val
->type
->length
= 0;
1269 val
->type
->length
= vtn_constant_uint(b
, w
[3]);
1272 val
->type
->base_type
= vtn_base_type_array
;
1273 val
->type
->array_element
= array_element
;
1274 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
1275 val
->type
->stride
= glsl_get_cl_size(array_element
->type
);
1277 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1278 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
,
1283 case SpvOpTypeStruct
: {
1284 unsigned num_fields
= count
- 2;
1285 val
->type
->base_type
= vtn_base_type_struct
;
1286 val
->type
->length
= num_fields
;
1287 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1288 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1289 val
->type
->packed
= false;
1291 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1292 for (unsigned i
= 0; i
< num_fields
; i
++) {
1293 val
->type
->members
[i
] =
1294 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
1295 fields
[i
] = (struct glsl_struct_field
) {
1296 .type
= val
->type
->members
[i
]->type
,
1297 .name
= ralloc_asprintf(b
, "field%d", i
),
1303 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1304 unsigned offset
= 0;
1305 for (unsigned i
= 0; i
< num_fields
; i
++) {
1306 offset
= align(offset
, glsl_get_cl_alignment(fields
[i
].type
));
1307 fields
[i
].offset
= offset
;
1308 offset
+= glsl_get_cl_size(fields
[i
].type
);
1312 struct member_decoration_ctx ctx
= {
1313 .num_fields
= num_fields
,
1318 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1319 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1321 vtn_foreach_decoration(b
, val
, struct_block_decoration_cb
, NULL
);
1323 const char *name
= val
->name
;
1325 if (val
->type
->block
|| val
->type
->buffer_block
) {
1326 /* Packing will be ignored since types coming from SPIR-V are
1327 * explicitly laid out.
1329 val
->type
->type
= glsl_interface_type(fields
, num_fields
,
1330 /* packing */ 0, false,
1331 name
? name
: "block");
1333 val
->type
->type
= glsl_struct_type(fields
, num_fields
,
1334 name
? name
: "struct", false);
1339 case SpvOpTypeFunction
: {
1340 val
->type
->base_type
= vtn_base_type_function
;
1341 val
->type
->type
= NULL
;
1343 val
->type
->return_type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1345 const unsigned num_params
= count
- 3;
1346 val
->type
->length
= num_params
;
1347 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1348 for (unsigned i
= 0; i
< count
- 3; i
++) {
1349 val
->type
->params
[i
] =
1350 vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
;
1355 case SpvOpTypePointer
:
1356 case SpvOpTypeForwardPointer
: {
1357 /* We can't blindly push the value because it might be a forward
1360 val
= vtn_untyped_value(b
, w
[1]);
1362 SpvStorageClass storage_class
= w
[2];
1364 if (val
->value_type
== vtn_value_type_invalid
) {
1365 val
->value_type
= vtn_value_type_type
;
1366 val
->type
= rzalloc(b
, struct vtn_type
);
1367 val
->type
->id
= w
[1];
1368 val
->type
->base_type
= vtn_base_type_pointer
;
1369 val
->type
->storage_class
= storage_class
;
1371 /* These can actually be stored to nir_variables and used as SSA
1372 * values so they need a real glsl_type.
1374 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1375 b
, storage_class
, NULL
, NULL
);
1376 val
->type
->type
= nir_address_format_to_glsl_type(
1377 vtn_mode_to_address_format(b
, mode
));
1379 vtn_fail_if(val
->type
->storage_class
!= storage_class
,
1380 "The storage classes of an OpTypePointer and any "
1381 "OpTypeForwardPointers that provide forward "
1382 "declarations of it must match.");
1385 if (opcode
== SpvOpTypePointer
) {
1386 vtn_fail_if(val
->type
->deref
!= NULL
,
1387 "While OpTypeForwardPointer can be used to provide a "
1388 "forward declaration of a pointer, OpTypePointer can "
1389 "only be used once for a given id.");
1391 val
->type
->deref
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
1393 /* Only certain storage classes use ArrayStride. The others (in
1394 * particular Workgroup) are expected to be laid out by the driver.
1396 switch (storage_class
) {
1397 case SpvStorageClassUniform
:
1398 case SpvStorageClassPushConstant
:
1399 case SpvStorageClassStorageBuffer
:
1400 case SpvStorageClassPhysicalStorageBufferEXT
:
1401 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1404 /* Nothing to do. */
1408 if (b
->physical_ptrs
) {
1409 switch (storage_class
) {
1410 case SpvStorageClassFunction
:
1411 case SpvStorageClassWorkgroup
:
1412 case SpvStorageClassCrossWorkgroup
:
1413 val
->type
->stride
= align(glsl_get_cl_size(val
->type
->deref
->type
),
1414 glsl_get_cl_alignment(val
->type
->deref
->type
));
1419 } else if (storage_class
== SpvStorageClassWorkgroup
&&
1420 b
->options
->lower_workgroup_access_to_offsets
) {
1421 /* Lay out Workgroup types so it can be lowered to offsets during
1422 * SPIR-V to NIR conversion. When not lowering to offsets, the
1423 * stride will be calculated by the driver.
1425 uint32_t size
, align
;
1426 val
->type
->deref
= vtn_type_layout_std430(b
, val
->type
->deref
,
1428 val
->type
->length
= size
;
1429 val
->type
->align
= align
;
1430 val
->type
->stride
= vtn_align_u32(size
, align
);
1436 case SpvOpTypeImage
: {
1437 val
->type
->base_type
= vtn_base_type_image
;
1439 const struct vtn_type
*sampled_type
=
1440 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1442 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1443 glsl_get_bit_size(sampled_type
->type
) != 32,
1444 "Sampled type of OpTypeImage must be a 32-bit scalar");
1446 enum glsl_sampler_dim dim
;
1447 switch ((SpvDim
)w
[3]) {
1448 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1449 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1450 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1451 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1452 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1453 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1454 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1456 vtn_fail("Invalid SPIR-V image dimensionality: %s (%u)",
1457 spirv_dim_to_string((SpvDim
)w
[3]), w
[3]);
1460 /* w[4]: as per Vulkan spec "Validation Rules within a Module",
1461 * The “Depth” operand of OpTypeImage is ignored.
1463 bool is_array
= w
[5];
1464 bool multisampled
= w
[6];
1465 unsigned sampled
= w
[7];
1466 SpvImageFormat format
= w
[8];
1469 val
->type
->access_qualifier
= w
[9];
1471 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1474 if (dim
== GLSL_SAMPLER_DIM_2D
)
1475 dim
= GLSL_SAMPLER_DIM_MS
;
1476 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1477 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1479 vtn_fail("Unsupported multisampled image type");
1482 val
->type
->image_format
= translate_image_format(b
, format
);
1484 enum glsl_base_type sampled_base_type
=
1485 glsl_get_base_type(sampled_type
->type
);
1487 val
->type
->sampled
= true;
1488 val
->type
->type
= glsl_sampler_type(dim
, false, is_array
,
1490 } else if (sampled
== 2) {
1491 val
->type
->sampled
= false;
1492 val
->type
->type
= glsl_image_type(dim
, is_array
, sampled_base_type
);
1494 vtn_fail("We need to know if the image will be sampled");
1499 case SpvOpTypeSampledImage
:
1500 val
->type
->base_type
= vtn_base_type_sampled_image
;
1501 val
->type
->image
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1502 val
->type
->type
= val
->type
->image
->type
;
1505 case SpvOpTypeSampler
:
1506 /* The actual sampler type here doesn't really matter. It gets
1507 * thrown away the moment you combine it with an image. What really
1508 * matters is that it's a sampler type as opposed to an integer type
1509 * so the backend knows what to do.
1511 val
->type
->base_type
= vtn_base_type_sampler
;
1512 val
->type
->type
= glsl_bare_sampler_type();
1515 case SpvOpTypeOpaque
:
1516 case SpvOpTypeEvent
:
1517 case SpvOpTypeDeviceEvent
:
1518 case SpvOpTypeReserveId
:
1519 case SpvOpTypeQueue
:
1522 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1525 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1527 if (val
->type
->base_type
== vtn_base_type_struct
&&
1528 (val
->type
->block
|| val
->type
->buffer_block
)) {
1529 for (unsigned i
= 0; i
< val
->type
->length
; i
++) {
1530 vtn_fail_if(vtn_type_contains_block(b
, val
->type
->members
[i
]),
1531 "Block and BufferBlock decorations cannot decorate a "
1532 "structure type that is nested at any level inside "
1533 "another structure type decorated with Block or "
1539 static nir_constant
*
1540 vtn_null_constant(struct vtn_builder
*b
, struct vtn_type
*type
)
1542 nir_constant
*c
= rzalloc(b
, nir_constant
);
1544 switch (type
->base_type
) {
1545 case vtn_base_type_scalar
:
1546 case vtn_base_type_vector
:
1547 /* Nothing to do here. It's already initialized to zero */
1550 case vtn_base_type_pointer
: {
1551 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1552 b
, type
->storage_class
, type
->deref
, NULL
);
1553 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
1555 const nir_const_value
*null_value
= nir_address_format_null_value(addr_format
);
1556 memcpy(c
->values
, null_value
,
1557 sizeof(nir_const_value
) * nir_address_format_num_components(addr_format
));
1561 case vtn_base_type_void
:
1562 case vtn_base_type_image
:
1563 case vtn_base_type_sampler
:
1564 case vtn_base_type_sampled_image
:
1565 case vtn_base_type_function
:
1566 /* For those we have to return something but it doesn't matter what. */
1569 case vtn_base_type_matrix
:
1570 case vtn_base_type_array
:
1571 vtn_assert(type
->length
> 0);
1572 c
->num_elements
= type
->length
;
1573 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1575 c
->elements
[0] = vtn_null_constant(b
, type
->array_element
);
1576 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1577 c
->elements
[i
] = c
->elements
[0];
1580 case vtn_base_type_struct
:
1581 c
->num_elements
= type
->length
;
1582 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1583 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
1584 c
->elements
[i
] = vtn_null_constant(b
, type
->members
[i
]);
1588 vtn_fail("Invalid type for null constant");
1595 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
1596 int member
, const struct vtn_decoration
*dec
,
1599 vtn_assert(member
== -1);
1600 if (dec
->decoration
!= SpvDecorationSpecId
)
1603 struct spec_constant_value
*const_value
= data
;
1605 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1606 if (b
->specializations
[i
].id
== dec
->operands
[0]) {
1607 if (const_value
->is_double
)
1608 const_value
->data64
= b
->specializations
[i
].data64
;
1610 const_value
->data32
= b
->specializations
[i
].data32
;
1617 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
1618 uint32_t const_value
)
1620 struct spec_constant_value data
;
1621 data
.is_double
= false;
1622 data
.data32
= const_value
;
1623 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1628 get_specialization64(struct vtn_builder
*b
, struct vtn_value
*val
,
1629 uint64_t const_value
)
1631 struct spec_constant_value data
;
1632 data
.is_double
= true;
1633 data
.data64
= const_value
;
1634 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1639 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1640 struct vtn_value
*val
,
1642 const struct vtn_decoration
*dec
,
1645 vtn_assert(member
== -1);
1646 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1647 dec
->operands
[0] != SpvBuiltInWorkgroupSize
)
1650 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1651 b
->workgroup_size_builtin
= val
;
1655 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1656 const uint32_t *w
, unsigned count
)
1658 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1659 val
->constant
= rzalloc(b
, nir_constant
);
1661 case SpvOpConstantTrue
:
1662 case SpvOpConstantFalse
:
1663 case SpvOpSpecConstantTrue
:
1664 case SpvOpSpecConstantFalse
: {
1665 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1666 "Result type of %s must be OpTypeBool",
1667 spirv_op_to_string(opcode
));
1669 uint32_t int_val
= (opcode
== SpvOpConstantTrue
||
1670 opcode
== SpvOpSpecConstantTrue
);
1672 if (opcode
== SpvOpSpecConstantTrue
||
1673 opcode
== SpvOpSpecConstantFalse
)
1674 int_val
= get_specialization(b
, val
, int_val
);
1676 val
->constant
->values
[0].b
= int_val
!= 0;
1680 case SpvOpConstant
: {
1681 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1682 "Result type of %s must be a scalar",
1683 spirv_op_to_string(opcode
));
1684 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1687 val
->constant
->values
[0].u64
= vtn_u64_literal(&w
[3]);
1690 val
->constant
->values
[0].u32
= w
[3];
1693 val
->constant
->values
[0].u16
= w
[3];
1696 val
->constant
->values
[0].u8
= w
[3];
1699 vtn_fail("Unsupported SpvOpConstant bit size: %u", bit_size
);
1704 case SpvOpSpecConstant
: {
1705 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1706 "Result type of %s must be a scalar",
1707 spirv_op_to_string(opcode
));
1708 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1711 val
->constant
->values
[0].u64
=
1712 get_specialization64(b
, val
, vtn_u64_literal(&w
[3]));
1715 val
->constant
->values
[0].u32
= get_specialization(b
, val
, w
[3]);
1718 val
->constant
->values
[0].u16
= get_specialization(b
, val
, w
[3]);
1721 val
->constant
->values
[0].u8
= get_specialization(b
, val
, w
[3]);
1724 vtn_fail("Unsupported SpvOpSpecConstant bit size");
1729 case SpvOpSpecConstantComposite
:
1730 case SpvOpConstantComposite
: {
1731 unsigned elem_count
= count
- 3;
1732 vtn_fail_if(elem_count
!= val
->type
->length
,
1733 "%s has %u constituents, expected %u",
1734 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1736 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1737 for (unsigned i
= 0; i
< elem_count
; i
++) {
1738 struct vtn_value
*val
= vtn_untyped_value(b
, w
[i
+ 3]);
1740 if (val
->value_type
== vtn_value_type_constant
) {
1741 elems
[i
] = val
->constant
;
1743 vtn_fail_if(val
->value_type
!= vtn_value_type_undef
,
1744 "only constants or undefs allowed for "
1745 "SpvOpConstantComposite");
1746 /* to make it easier, just insert a NULL constant for now */
1747 elems
[i
] = vtn_null_constant(b
, val
->type
);
1751 switch (val
->type
->base_type
) {
1752 case vtn_base_type_vector
: {
1753 assert(glsl_type_is_vector(val
->type
->type
));
1754 for (unsigned i
= 0; i
< elem_count
; i
++)
1755 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1759 case vtn_base_type_matrix
:
1760 case vtn_base_type_struct
:
1761 case vtn_base_type_array
:
1762 ralloc_steal(val
->constant
, elems
);
1763 val
->constant
->num_elements
= elem_count
;
1764 val
->constant
->elements
= elems
;
1768 vtn_fail("Result type of %s must be a composite type",
1769 spirv_op_to_string(opcode
));
1774 case SpvOpSpecConstantOp
: {
1775 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1777 case SpvOpVectorShuffle
: {
1778 struct vtn_value
*v0
= &b
->values
[w
[4]];
1779 struct vtn_value
*v1
= &b
->values
[w
[5]];
1781 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1782 v0
->value_type
== vtn_value_type_undef
);
1783 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1784 v1
->value_type
== vtn_value_type_undef
);
1786 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1787 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1789 vtn_assert(len0
+ len1
< 16);
1791 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1792 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1793 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1795 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1796 (void)bit_size0
; (void)bit_size1
;
1798 nir_const_value undef
= { .u64
= 0xdeadbeefdeadbeef };
1799 nir_const_value combined
[NIR_MAX_VEC_COMPONENTS
* 2];
1801 if (v0
->value_type
== vtn_value_type_constant
) {
1802 for (unsigned i
= 0; i
< len0
; i
++)
1803 combined
[i
] = v0
->constant
->values
[i
];
1805 if (v1
->value_type
== vtn_value_type_constant
) {
1806 for (unsigned i
= 0; i
< len1
; i
++)
1807 combined
[len0
+ i
] = v1
->constant
->values
[i
];
1810 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1811 uint32_t comp
= w
[i
+ 6];
1812 if (comp
== (uint32_t)-1) {
1813 /* If component is not used, set the value to a known constant
1814 * to detect if it is wrongly used.
1816 val
->constant
->values
[j
] = undef
;
1818 vtn_fail_if(comp
>= len0
+ len1
,
1819 "All Component literals must either be FFFFFFFF "
1820 "or in [0, N - 1] (inclusive).");
1821 val
->constant
->values
[j
] = combined
[comp
];
1827 case SpvOpCompositeExtract
:
1828 case SpvOpCompositeInsert
: {
1829 struct vtn_value
*comp
;
1830 unsigned deref_start
;
1831 struct nir_constant
**c
;
1832 if (opcode
== SpvOpCompositeExtract
) {
1833 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1835 c
= &comp
->constant
;
1837 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1839 val
->constant
= nir_constant_clone(comp
->constant
,
1845 const struct vtn_type
*type
= comp
->type
;
1846 for (unsigned i
= deref_start
; i
< count
; i
++) {
1847 vtn_fail_if(w
[i
] > type
->length
,
1848 "%uth index of %s is %u but the type has only "
1849 "%u elements", i
- deref_start
,
1850 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1852 switch (type
->base_type
) {
1853 case vtn_base_type_vector
:
1855 type
= type
->array_element
;
1858 case vtn_base_type_matrix
:
1859 case vtn_base_type_array
:
1860 c
= &(*c
)->elements
[w
[i
]];
1861 type
= type
->array_element
;
1864 case vtn_base_type_struct
:
1865 c
= &(*c
)->elements
[w
[i
]];
1866 type
= type
->members
[w
[i
]];
1870 vtn_fail("%s must only index into composite types",
1871 spirv_op_to_string(opcode
));
1875 if (opcode
== SpvOpCompositeExtract
) {
1879 unsigned num_components
= type
->length
;
1880 for (unsigned i
= 0; i
< num_components
; i
++)
1881 val
->constant
->values
[i
] = (*c
)->values
[elem
+ i
];
1884 struct vtn_value
*insert
=
1885 vtn_value(b
, w
[4], vtn_value_type_constant
);
1886 vtn_assert(insert
->type
== type
);
1888 *c
= insert
->constant
;
1890 unsigned num_components
= type
->length
;
1891 for (unsigned i
= 0; i
< num_components
; i
++)
1892 (*c
)->values
[elem
+ i
] = insert
->constant
->values
[i
];
1900 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
1901 nir_alu_type src_alu_type
= dst_alu_type
;
1902 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
1905 vtn_assert(count
<= 7);
1911 /* We have a source in a conversion */
1913 nir_get_nir_type_for_glsl_type(
1914 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1915 /* We use the bitsize of the conversion source to evaluate the opcode later */
1916 bit_size
= glsl_get_bit_size(
1917 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1920 bit_size
= glsl_get_bit_size(val
->type
->type
);
1923 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
1924 nir_alu_type_get_type_size(src_alu_type
),
1925 nir_alu_type_get_type_size(dst_alu_type
));
1926 nir_const_value src
[3][NIR_MAX_VEC_COMPONENTS
];
1928 for (unsigned i
= 0; i
< count
- 4; i
++) {
1929 struct vtn_value
*src_val
=
1930 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
);
1932 /* If this is an unsized source, pull the bit size from the
1933 * source; otherwise, we'll use the bit size from the destination.
1935 if (!nir_alu_type_get_type_size(nir_op_infos
[op
].input_types
[i
]))
1936 bit_size
= glsl_get_bit_size(src_val
->type
->type
);
1938 unsigned src_comps
= nir_op_infos
[op
].input_sizes
[i
] ?
1939 nir_op_infos
[op
].input_sizes
[i
] :
1942 unsigned j
= swap
? 1 - i
: i
;
1943 for (unsigned c
= 0; c
< src_comps
; c
++)
1944 src
[j
][c
] = src_val
->constant
->values
[c
];
1947 /* fix up fixed size sources */
1954 for (unsigned i
= 0; i
< num_components
; ++i
) {
1956 case 64: src
[1][i
].u32
= src
[1][i
].u64
; break;
1957 case 16: src
[1][i
].u32
= src
[1][i
].u16
; break;
1958 case 8: src
[1][i
].u32
= src
[1][i
].u8
; break;
1967 nir_const_value
*srcs
[3] = {
1968 src
[0], src
[1], src
[2],
1970 nir_eval_const_opcode(op
, val
->constant
->values
, num_components
, bit_size
, srcs
);
1977 case SpvOpConstantNull
:
1978 val
->constant
= vtn_null_constant(b
, val
->type
);
1981 case SpvOpConstantSampler
:
1982 vtn_fail("OpConstantSampler requires Kernel Capability");
1986 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1989 /* Now that we have the value, update the workgroup size if needed */
1990 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1993 struct vtn_ssa_value
*
1994 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1996 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1999 if (!glsl_type_is_vector_or_scalar(type
)) {
2000 unsigned elems
= glsl_get_length(type
);
2001 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2002 for (unsigned i
= 0; i
< elems
; i
++) {
2003 const struct glsl_type
*child_type
;
2005 switch (glsl_get_base_type(type
)) {
2007 case GLSL_TYPE_UINT
:
2008 case GLSL_TYPE_INT16
:
2009 case GLSL_TYPE_UINT16
:
2010 case GLSL_TYPE_UINT8
:
2011 case GLSL_TYPE_INT8
:
2012 case GLSL_TYPE_INT64
:
2013 case GLSL_TYPE_UINT64
:
2014 case GLSL_TYPE_BOOL
:
2015 case GLSL_TYPE_FLOAT
:
2016 case GLSL_TYPE_FLOAT16
:
2017 case GLSL_TYPE_DOUBLE
:
2018 child_type
= glsl_get_column_type(type
);
2020 case GLSL_TYPE_ARRAY
:
2021 child_type
= glsl_get_array_element(type
);
2023 case GLSL_TYPE_STRUCT
:
2024 case GLSL_TYPE_INTERFACE
:
2025 child_type
= glsl_get_struct_field(type
, i
);
2028 vtn_fail("unkown base type");
2031 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
2039 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
2042 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
2043 src
.src_type
= type
;
2048 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
2049 const uint32_t *w
, unsigned count
)
2051 if (opcode
== SpvOpSampledImage
) {
2052 struct vtn_value
*val
=
2053 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2054 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2055 val
->sampled_image
->type
=
2056 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2057 val
->sampled_image
->image
=
2058 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2059 val
->sampled_image
->sampler
=
2060 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
2062 } else if (opcode
== SpvOpImage
) {
2063 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
2064 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
2065 vtn_push_value_pointer(b
, w
[2], src_val
->sampled_image
->image
);
2067 vtn_assert(src_val
->value_type
== vtn_value_type_pointer
);
2068 vtn_push_value_pointer(b
, w
[2], src_val
->pointer
);
2073 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2075 struct vtn_sampled_image sampled
;
2076 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
2077 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
2078 sampled
= *sampled_val
->sampled_image
;
2080 vtn_assert(sampled_val
->value_type
== vtn_value_type_pointer
);
2081 sampled
.type
= sampled_val
->pointer
->type
;
2082 sampled
.image
= NULL
;
2083 sampled
.sampler
= sampled_val
->pointer
;
2086 const struct glsl_type
*image_type
= sampled
.type
->type
;
2087 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
2088 const bool is_array
= glsl_sampler_type_is_array(image_type
);
2090 /* Figure out the base texture operation */
2093 case SpvOpImageSampleImplicitLod
:
2094 case SpvOpImageSampleDrefImplicitLod
:
2095 case SpvOpImageSampleProjImplicitLod
:
2096 case SpvOpImageSampleProjDrefImplicitLod
:
2097 texop
= nir_texop_tex
;
2100 case SpvOpImageSampleExplicitLod
:
2101 case SpvOpImageSampleDrefExplicitLod
:
2102 case SpvOpImageSampleProjExplicitLod
:
2103 case SpvOpImageSampleProjDrefExplicitLod
:
2104 texop
= nir_texop_txl
;
2107 case SpvOpImageFetch
:
2108 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
2109 texop
= nir_texop_txf_ms
;
2111 texop
= nir_texop_txf
;
2115 case SpvOpImageGather
:
2116 case SpvOpImageDrefGather
:
2117 texop
= nir_texop_tg4
;
2120 case SpvOpImageQuerySizeLod
:
2121 case SpvOpImageQuerySize
:
2122 texop
= nir_texop_txs
;
2125 case SpvOpImageQueryLod
:
2126 texop
= nir_texop_lod
;
2129 case SpvOpImageQueryLevels
:
2130 texop
= nir_texop_query_levels
;
2133 case SpvOpImageQuerySamples
:
2134 texop
= nir_texop_texture_samples
;
2138 vtn_fail_with_opcode("Unhandled opcode", opcode
);
2141 nir_tex_src srcs
[10]; /* 10 should be enough */
2142 nir_tex_src
*p
= srcs
;
2144 nir_deref_instr
*sampler
= vtn_pointer_to_deref(b
, sampled
.sampler
);
2145 nir_deref_instr
*texture
=
2146 sampled
.image
? vtn_pointer_to_deref(b
, sampled
.image
) : sampler
;
2148 p
->src
= nir_src_for_ssa(&texture
->dest
.ssa
);
2149 p
->src_type
= nir_tex_src_texture_deref
;
2159 /* These operations require a sampler */
2160 p
->src
= nir_src_for_ssa(&sampler
->dest
.ssa
);
2161 p
->src_type
= nir_tex_src_sampler_deref
;
2165 case nir_texop_txf_ms
:
2167 case nir_texop_query_levels
:
2168 case nir_texop_texture_samples
:
2169 case nir_texop_samples_identical
:
2172 case nir_texop_txf_ms_fb
:
2173 vtn_fail("unexpected nir_texop_txf_ms_fb");
2175 case nir_texop_txf_ms_mcs
:
2176 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2181 struct nir_ssa_def
*coord
;
2182 unsigned coord_components
;
2184 case SpvOpImageSampleImplicitLod
:
2185 case SpvOpImageSampleExplicitLod
:
2186 case SpvOpImageSampleDrefImplicitLod
:
2187 case SpvOpImageSampleDrefExplicitLod
:
2188 case SpvOpImageSampleProjImplicitLod
:
2189 case SpvOpImageSampleProjExplicitLod
:
2190 case SpvOpImageSampleProjDrefImplicitLod
:
2191 case SpvOpImageSampleProjDrefExplicitLod
:
2192 case SpvOpImageFetch
:
2193 case SpvOpImageGather
:
2194 case SpvOpImageDrefGather
:
2195 case SpvOpImageQueryLod
: {
2196 /* All these types have the coordinate as their first real argument */
2197 switch (sampler_dim
) {
2198 case GLSL_SAMPLER_DIM_1D
:
2199 case GLSL_SAMPLER_DIM_BUF
:
2200 coord_components
= 1;
2202 case GLSL_SAMPLER_DIM_2D
:
2203 case GLSL_SAMPLER_DIM_RECT
:
2204 case GLSL_SAMPLER_DIM_MS
:
2205 coord_components
= 2;
2207 case GLSL_SAMPLER_DIM_3D
:
2208 case GLSL_SAMPLER_DIM_CUBE
:
2209 coord_components
= 3;
2212 vtn_fail("Invalid sampler type");
2215 if (is_array
&& texop
!= nir_texop_lod
)
2218 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
2219 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2220 (1 << coord_components
) - 1));
2221 p
->src_type
= nir_tex_src_coord
;
2228 coord_components
= 0;
2233 case SpvOpImageSampleProjImplicitLod
:
2234 case SpvOpImageSampleProjExplicitLod
:
2235 case SpvOpImageSampleProjDrefImplicitLod
:
2236 case SpvOpImageSampleProjDrefExplicitLod
:
2237 /* These have the projector as the last coordinate component */
2238 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2239 p
->src_type
= nir_tex_src_projector
;
2247 bool is_shadow
= false;
2248 unsigned gather_component
= 0;
2250 case SpvOpImageSampleDrefImplicitLod
:
2251 case SpvOpImageSampleDrefExplicitLod
:
2252 case SpvOpImageSampleProjDrefImplicitLod
:
2253 case SpvOpImageSampleProjDrefExplicitLod
:
2254 case SpvOpImageDrefGather
:
2255 /* These all have an explicit depth value as their next source */
2257 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2260 case SpvOpImageGather
:
2261 /* This has a component as its next source */
2262 gather_component
= vtn_constant_uint(b
, w
[idx
++]);
2269 /* For OpImageQuerySizeLod, we always have an LOD */
2270 if (opcode
== SpvOpImageQuerySizeLod
)
2271 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2273 /* Now we need to handle some number of optional arguments */
2274 struct vtn_value
*gather_offsets
= NULL
;
2276 uint32_t operands
= w
[idx
++];
2278 if (operands
& SpvImageOperandsBiasMask
) {
2279 vtn_assert(texop
== nir_texop_tex
);
2280 texop
= nir_texop_txb
;
2281 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
2284 if (operands
& SpvImageOperandsLodMask
) {
2285 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2286 texop
== nir_texop_txs
);
2287 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2290 if (operands
& SpvImageOperandsGradMask
) {
2291 vtn_assert(texop
== nir_texop_txl
);
2292 texop
= nir_texop_txd
;
2293 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
2294 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
2297 if (operands
& SpvImageOperandsOffsetMask
||
2298 operands
& SpvImageOperandsConstOffsetMask
)
2299 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
2301 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2302 vtn_assert(texop
== nir_texop_tg4
);
2303 gather_offsets
= vtn_value(b
, w
[idx
++], vtn_value_type_constant
);
2306 if (operands
& SpvImageOperandsSampleMask
) {
2307 vtn_assert(texop
== nir_texop_txf_ms
);
2308 texop
= nir_texop_txf_ms
;
2309 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
2312 if (operands
& SpvImageOperandsMinLodMask
) {
2313 vtn_assert(texop
== nir_texop_tex
||
2314 texop
== nir_texop_txb
||
2315 texop
== nir_texop_txd
);
2316 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_min_lod
);
2319 /* We should have now consumed exactly all of the arguments */
2320 vtn_assert(idx
== count
);
2322 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2325 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2327 instr
->coord_components
= coord_components
;
2328 instr
->sampler_dim
= sampler_dim
;
2329 instr
->is_array
= is_array
;
2330 instr
->is_shadow
= is_shadow
;
2331 instr
->is_new_style_shadow
=
2332 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2333 instr
->component
= gather_component
;
2335 if (sampled
.image
&& (sampled
.image
->access
& ACCESS_NON_UNIFORM
))
2336 instr
->texture_non_uniform
= true;
2338 if (sampled
.sampler
&& (sampled
.sampler
->access
& ACCESS_NON_UNIFORM
))
2339 instr
->sampler_non_uniform
= true;
2341 switch (glsl_get_sampler_result_type(image_type
)) {
2342 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
2343 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
2344 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
2345 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
2347 vtn_fail("Invalid base type for sampler result");
2350 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2351 nir_tex_instr_dest_size(instr
), 32, NULL
);
2353 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2354 nir_tex_instr_dest_size(instr
));
2356 if (gather_offsets
) {
2357 vtn_fail_if(gather_offsets
->type
->base_type
!= vtn_base_type_array
||
2358 gather_offsets
->type
->length
!= 4,
2359 "ConstOffsets must be an array of size four of vectors "
2360 "of two integer components");
2362 struct vtn_type
*vec_type
= gather_offsets
->type
->array_element
;
2363 vtn_fail_if(vec_type
->base_type
!= vtn_base_type_vector
||
2364 vec_type
->length
!= 2 ||
2365 !glsl_type_is_integer(vec_type
->type
),
2366 "ConstOffsets must be an array of size four of vectors "
2367 "of two integer components");
2369 unsigned bit_size
= glsl_get_bit_size(vec_type
->type
);
2370 for (uint32_t i
= 0; i
< 4; i
++) {
2371 const nir_const_value
*cvec
=
2372 gather_offsets
->constant
->elements
[i
]->values
;
2373 for (uint32_t j
= 0; j
< 2; j
++) {
2375 case 8: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i8
; break;
2376 case 16: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i16
; break;
2377 case 32: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i32
; break;
2378 case 64: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i64
; break;
2380 vtn_fail("Unsupported bit size: %u", bit_size
);
2386 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
2387 ssa
->def
= &instr
->dest
.ssa
;
2388 vtn_push_ssa(b
, w
[2], ret_type
, ssa
);
2390 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2394 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2395 const uint32_t *w
, nir_src
*src
)
2398 case SpvOpAtomicIIncrement
:
2399 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
2402 case SpvOpAtomicIDecrement
:
2403 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
2406 case SpvOpAtomicISub
:
2408 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
2411 case SpvOpAtomicCompareExchange
:
2412 case SpvOpAtomicCompareExchangeWeak
:
2413 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
2414 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
2417 case SpvOpAtomicExchange
:
2418 case SpvOpAtomicIAdd
:
2419 case SpvOpAtomicSMin
:
2420 case SpvOpAtomicUMin
:
2421 case SpvOpAtomicSMax
:
2422 case SpvOpAtomicUMax
:
2423 case SpvOpAtomicAnd
:
2425 case SpvOpAtomicXor
:
2426 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
2430 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2434 static nir_ssa_def
*
2435 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2437 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
2439 /* The image_load_store intrinsics assume a 4-dim coordinate */
2440 unsigned dim
= glsl_get_vector_elements(coord
->type
);
2441 unsigned swizzle
[4];
2442 for (unsigned i
= 0; i
< 4; i
++)
2443 swizzle
[i
] = MIN2(i
, dim
- 1);
2445 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4);
2448 static nir_ssa_def
*
2449 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2451 if (value
->num_components
== 4)
2455 for (unsigned i
= 0; i
< 4; i
++)
2456 swiz
[i
] = i
< value
->num_components
? i
: 0;
2457 return nir_swizzle(b
, value
, swiz
, 4);
2461 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2462 const uint32_t *w
, unsigned count
)
2464 /* Just get this one out of the way */
2465 if (opcode
== SpvOpImageTexelPointer
) {
2466 struct vtn_value
*val
=
2467 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2468 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2470 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2471 val
->image
->coord
= get_image_coord(b
, w
[4]);
2472 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
2476 struct vtn_image_pointer image
;
2479 case SpvOpAtomicExchange
:
2480 case SpvOpAtomicCompareExchange
:
2481 case SpvOpAtomicCompareExchangeWeak
:
2482 case SpvOpAtomicIIncrement
:
2483 case SpvOpAtomicIDecrement
:
2484 case SpvOpAtomicIAdd
:
2485 case SpvOpAtomicISub
:
2486 case SpvOpAtomicLoad
:
2487 case SpvOpAtomicSMin
:
2488 case SpvOpAtomicUMin
:
2489 case SpvOpAtomicSMax
:
2490 case SpvOpAtomicUMax
:
2491 case SpvOpAtomicAnd
:
2493 case SpvOpAtomicXor
:
2494 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
2497 case SpvOpAtomicStore
:
2498 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
2501 case SpvOpImageQuerySize
:
2502 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2504 image
.sample
= NULL
;
2507 case SpvOpImageRead
:
2508 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2509 image
.coord
= get_image_coord(b
, w
[4]);
2511 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
2512 vtn_assert(w
[5] == SpvImageOperandsSampleMask
);
2513 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
2515 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2519 case SpvOpImageWrite
:
2520 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2521 image
.coord
= get_image_coord(b
, w
[2]);
2525 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
2526 vtn_assert(w
[4] == SpvImageOperandsSampleMask
);
2527 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
2529 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2534 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2537 nir_intrinsic_op op
;
2539 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
2540 OP(ImageQuerySize
, size
)
2542 OP(ImageWrite
, store
)
2543 OP(AtomicLoad
, load
)
2544 OP(AtomicStore
, store
)
2545 OP(AtomicExchange
, atomic_exchange
)
2546 OP(AtomicCompareExchange
, atomic_comp_swap
)
2547 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2548 OP(AtomicIIncrement
, atomic_add
)
2549 OP(AtomicIDecrement
, atomic_add
)
2550 OP(AtomicIAdd
, atomic_add
)
2551 OP(AtomicISub
, atomic_add
)
2552 OP(AtomicSMin
, atomic_min
)
2553 OP(AtomicUMin
, atomic_min
)
2554 OP(AtomicSMax
, atomic_max
)
2555 OP(AtomicUMax
, atomic_max
)
2556 OP(AtomicAnd
, atomic_and
)
2557 OP(AtomicOr
, atomic_or
)
2558 OP(AtomicXor
, atomic_xor
)
2561 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2564 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
2566 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
2567 intrin
->src
[0] = nir_src_for_ssa(&image_deref
->dest
.ssa
);
2569 /* ImageQuerySize doesn't take any extra parameters */
2570 if (opcode
!= SpvOpImageQuerySize
) {
2571 /* The image coordinate is always 4 components but we may not have that
2572 * many. Swizzle to compensate.
2574 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
2575 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
2578 nir_intrinsic_set_access(intrin
, image
.image
->access
);
2581 case SpvOpAtomicLoad
:
2582 case SpvOpImageQuerySize
:
2583 case SpvOpImageRead
:
2585 case SpvOpAtomicStore
:
2586 case SpvOpImageWrite
: {
2587 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
2588 nir_ssa_def
*value
= vtn_ssa_value(b
, value_id
)->def
;
2589 /* nir_intrinsic_image_deref_store always takes a vec4 value */
2590 assert(op
== nir_intrinsic_image_deref_store
);
2591 intrin
->num_components
= 4;
2592 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
));
2596 case SpvOpAtomicCompareExchange
:
2597 case SpvOpAtomicCompareExchangeWeak
:
2598 case SpvOpAtomicIIncrement
:
2599 case SpvOpAtomicIDecrement
:
2600 case SpvOpAtomicExchange
:
2601 case SpvOpAtomicIAdd
:
2602 case SpvOpAtomicISub
:
2603 case SpvOpAtomicSMin
:
2604 case SpvOpAtomicUMin
:
2605 case SpvOpAtomicSMax
:
2606 case SpvOpAtomicUMax
:
2607 case SpvOpAtomicAnd
:
2609 case SpvOpAtomicXor
:
2610 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
2614 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2617 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
2618 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2620 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
2621 intrin
->num_components
= nir_intrinsic_infos
[op
].dest_components
;
2622 if (intrin
->num_components
== 0)
2623 intrin
->num_components
= dest_components
;
2625 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2626 intrin
->num_components
, 32, NULL
);
2628 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2630 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
2631 if (intrin
->num_components
!= dest_components
)
2632 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
2634 struct vtn_value
*val
=
2635 vtn_push_ssa(b
, w
[2], type
, vtn_create_ssa_value(b
, type
->type
));
2636 val
->ssa
->def
= result
;
2638 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2642 static nir_intrinsic_op
2643 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2646 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2647 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2648 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2649 OP(AtomicExchange
, atomic_exchange
)
2650 OP(AtomicCompareExchange
, atomic_comp_swap
)
2651 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2652 OP(AtomicIIncrement
, atomic_add
)
2653 OP(AtomicIDecrement
, atomic_add
)
2654 OP(AtomicIAdd
, atomic_add
)
2655 OP(AtomicISub
, atomic_add
)
2656 OP(AtomicSMin
, atomic_imin
)
2657 OP(AtomicUMin
, atomic_umin
)
2658 OP(AtomicSMax
, atomic_imax
)
2659 OP(AtomicUMax
, atomic_umax
)
2660 OP(AtomicAnd
, atomic_and
)
2661 OP(AtomicOr
, atomic_or
)
2662 OP(AtomicXor
, atomic_xor
)
2665 vtn_fail_with_opcode("Invalid SSBO atomic", opcode
);
2669 static nir_intrinsic_op
2670 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2673 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
2674 OP(AtomicLoad
, read_deref
)
2675 OP(AtomicExchange
, exchange
)
2676 OP(AtomicCompareExchange
, comp_swap
)
2677 OP(AtomicCompareExchangeWeak
, comp_swap
)
2678 OP(AtomicIIncrement
, inc_deref
)
2679 OP(AtomicIDecrement
, post_dec_deref
)
2680 OP(AtomicIAdd
, add_deref
)
2681 OP(AtomicISub
, add_deref
)
2682 OP(AtomicUMin
, min_deref
)
2683 OP(AtomicUMax
, max_deref
)
2684 OP(AtomicAnd
, and_deref
)
2685 OP(AtomicOr
, or_deref
)
2686 OP(AtomicXor
, xor_deref
)
2689 /* We left the following out: AtomicStore, AtomicSMin and
2690 * AtomicSmax. Right now there are not nir intrinsics for them. At this
2691 * moment Atomic Counter support is needed for ARB_spirv support, so is
2692 * only need to support GLSL Atomic Counters that are uints and don't
2693 * allow direct storage.
2695 unreachable("Invalid uniform atomic");
2699 static nir_intrinsic_op
2700 get_shared_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2703 case SpvOpAtomicLoad
: return nir_intrinsic_load_shared
;
2704 case SpvOpAtomicStore
: return nir_intrinsic_store_shared
;
2705 #define OP(S, N) case SpvOp##S: return nir_intrinsic_shared_##N;
2706 OP(AtomicExchange
, atomic_exchange
)
2707 OP(AtomicCompareExchange
, atomic_comp_swap
)
2708 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2709 OP(AtomicIIncrement
, atomic_add
)
2710 OP(AtomicIDecrement
, atomic_add
)
2711 OP(AtomicIAdd
, atomic_add
)
2712 OP(AtomicISub
, atomic_add
)
2713 OP(AtomicSMin
, atomic_imin
)
2714 OP(AtomicUMin
, atomic_umin
)
2715 OP(AtomicSMax
, atomic_imax
)
2716 OP(AtomicUMax
, atomic_umax
)
2717 OP(AtomicAnd
, atomic_and
)
2718 OP(AtomicOr
, atomic_or
)
2719 OP(AtomicXor
, atomic_xor
)
2722 vtn_fail_with_opcode("Invalid shared atomic", opcode
);
2726 static nir_intrinsic_op
2727 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2730 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
2731 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
2732 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
2733 OP(AtomicExchange
, atomic_exchange
)
2734 OP(AtomicCompareExchange
, atomic_comp_swap
)
2735 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2736 OP(AtomicIIncrement
, atomic_add
)
2737 OP(AtomicIDecrement
, atomic_add
)
2738 OP(AtomicIAdd
, atomic_add
)
2739 OP(AtomicISub
, atomic_add
)
2740 OP(AtomicSMin
, atomic_imin
)
2741 OP(AtomicUMin
, atomic_umin
)
2742 OP(AtomicSMax
, atomic_imax
)
2743 OP(AtomicUMax
, atomic_umax
)
2744 OP(AtomicAnd
, atomic_and
)
2745 OP(AtomicOr
, atomic_or
)
2746 OP(AtomicXor
, atomic_xor
)
2749 vtn_fail_with_opcode("Invalid shared atomic", opcode
);
2754 * Handles shared atomics, ssbo atomics and atomic counters.
2757 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
2758 const uint32_t *w
, unsigned count
)
2760 struct vtn_pointer
*ptr
;
2761 nir_intrinsic_instr
*atomic
;
2764 case SpvOpAtomicLoad
:
2765 case SpvOpAtomicExchange
:
2766 case SpvOpAtomicCompareExchange
:
2767 case SpvOpAtomicCompareExchangeWeak
:
2768 case SpvOpAtomicIIncrement
:
2769 case SpvOpAtomicIDecrement
:
2770 case SpvOpAtomicIAdd
:
2771 case SpvOpAtomicISub
:
2772 case SpvOpAtomicSMin
:
2773 case SpvOpAtomicUMin
:
2774 case SpvOpAtomicSMax
:
2775 case SpvOpAtomicUMax
:
2776 case SpvOpAtomicAnd
:
2778 case SpvOpAtomicXor
:
2779 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2782 case SpvOpAtomicStore
:
2783 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2787 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2791 SpvScope scope = w[4];
2792 SpvMemorySemanticsMask semantics = w[5];
2795 /* uniform as "atomic counter uniform" */
2796 if (ptr
->mode
== vtn_variable_mode_uniform
) {
2797 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2798 const struct glsl_type
*deref_type
= deref
->type
;
2799 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
2800 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2801 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2803 /* SSBO needs to initialize index/offset. In this case we don't need to,
2804 * as that info is already stored on the ptr->var->var nir_variable (see
2805 * vtn_create_variable)
2809 case SpvOpAtomicLoad
:
2810 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2813 case SpvOpAtomicStore
:
2814 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2815 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2818 case SpvOpAtomicExchange
:
2819 case SpvOpAtomicCompareExchange
:
2820 case SpvOpAtomicCompareExchangeWeak
:
2821 case SpvOpAtomicIIncrement
:
2822 case SpvOpAtomicIDecrement
:
2823 case SpvOpAtomicIAdd
:
2824 case SpvOpAtomicISub
:
2825 case SpvOpAtomicSMin
:
2826 case SpvOpAtomicUMin
:
2827 case SpvOpAtomicSMax
:
2828 case SpvOpAtomicUMax
:
2829 case SpvOpAtomicAnd
:
2831 case SpvOpAtomicXor
:
2832 /* Nothing: we don't need to call fill_common_atomic_sources here, as
2833 * atomic counter uniforms doesn't have sources
2838 unreachable("Invalid SPIR-V atomic");
2841 } else if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
2842 nir_ssa_def
*offset
, *index
;
2843 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
2845 nir_intrinsic_op op
;
2846 if (ptr
->mode
== vtn_variable_mode_ssbo
) {
2847 op
= get_ssbo_nir_atomic_op(b
, opcode
);
2849 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
&&
2850 b
->options
->lower_workgroup_access_to_offsets
);
2851 op
= get_shared_nir_atomic_op(b
, opcode
);
2854 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2858 case SpvOpAtomicLoad
:
2859 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2860 nir_intrinsic_set_align(atomic
, 4, 0);
2861 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2862 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2863 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2866 case SpvOpAtomicStore
:
2867 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2868 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2869 nir_intrinsic_set_align(atomic
, 4, 0);
2870 atomic
->src
[src
++] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2871 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2872 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2873 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2876 case SpvOpAtomicExchange
:
2877 case SpvOpAtomicCompareExchange
:
2878 case SpvOpAtomicCompareExchangeWeak
:
2879 case SpvOpAtomicIIncrement
:
2880 case SpvOpAtomicIDecrement
:
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 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2891 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2892 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2893 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
2897 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2900 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2901 const struct glsl_type
*deref_type
= deref
->type
;
2902 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
2903 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2904 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2907 case SpvOpAtomicLoad
:
2908 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2911 case SpvOpAtomicStore
:
2912 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2913 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2914 atomic
->src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2917 case SpvOpAtomicExchange
:
2918 case SpvOpAtomicCompareExchange
:
2919 case SpvOpAtomicCompareExchangeWeak
:
2920 case SpvOpAtomicIIncrement
:
2921 case SpvOpAtomicIDecrement
:
2922 case SpvOpAtomicIAdd
:
2923 case SpvOpAtomicISub
:
2924 case SpvOpAtomicSMin
:
2925 case SpvOpAtomicUMin
:
2926 case SpvOpAtomicSMax
:
2927 case SpvOpAtomicUMax
:
2928 case SpvOpAtomicAnd
:
2930 case SpvOpAtomicXor
:
2931 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
2935 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2939 if (opcode
!= SpvOpAtomicStore
) {
2940 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2942 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
2943 glsl_get_vector_elements(type
->type
),
2944 glsl_get_bit_size(type
->type
), NULL
);
2946 struct vtn_ssa_value
*ssa
= rzalloc(b
, struct vtn_ssa_value
);
2947 ssa
->def
= &atomic
->dest
.ssa
;
2948 ssa
->type
= type
->type
;
2949 vtn_push_ssa(b
, w
[2], type
, ssa
);
2952 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
2955 static nir_alu_instr
*
2956 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
2958 nir_op op
= nir_op_vec(num_components
);
2959 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
2960 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
2962 vec
->dest
.write_mask
= (1 << num_components
) - 1;
2967 struct vtn_ssa_value
*
2968 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
2970 if (src
->transposed
)
2971 return src
->transposed
;
2973 struct vtn_ssa_value
*dest
=
2974 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
2976 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
2977 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
2978 glsl_get_bit_size(src
->type
));
2979 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2980 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
2981 vec
->src
[0].swizzle
[0] = i
;
2983 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
2984 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
2985 vec
->src
[j
].swizzle
[0] = i
;
2988 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2989 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
2992 dest
->transposed
= src
;
2998 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
3000 return nir_channel(&b
->nb
, src
, index
);
3004 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
3007 nir_alu_instr
*vec
= create_vec(b
, src
->num_components
,
3010 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
3012 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
3014 vec
->src
[i
].src
= nir_src_for_ssa(src
);
3015 vec
->src
[i
].swizzle
[0] = i
;
3019 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3021 return &vec
->dest
.dest
.ssa
;
3024 static nir_ssa_def
*
3025 nir_ieq_imm(nir_builder
*b
, nir_ssa_def
*x
, uint64_t i
)
3027 return nir_ieq(b
, x
, nir_imm_intN_t(b
, i
, x
->bit_size
));
3031 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
3034 return nir_vector_extract(&b
->nb
, src
, nir_i2i(&b
->nb
, index
, 32));
3038 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
3039 nir_ssa_def
*insert
, nir_ssa_def
*index
)
3041 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
3042 for (unsigned i
= 1; i
< src
->num_components
; i
++)
3043 dest
= nir_bcsel(&b
->nb
, nir_ieq_imm(&b
->nb
, index
, i
),
3044 vtn_vector_insert(b
, src
, insert
, i
), dest
);
3049 static nir_ssa_def
*
3050 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
3051 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
3052 const uint32_t *indices
)
3054 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
3056 for (unsigned i
= 0; i
< num_components
; i
++) {
3057 uint32_t index
= indices
[i
];
3058 if (index
== 0xffffffff) {
3060 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
3061 } else if (index
< src0
->num_components
) {
3062 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
3063 vec
->src
[i
].swizzle
[0] = index
;
3065 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
3066 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
3070 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3072 return &vec
->dest
.dest
.ssa
;
3076 * Concatentates a number of vectors/scalars together to produce a vector
3078 static nir_ssa_def
*
3079 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
3080 unsigned num_srcs
, nir_ssa_def
**srcs
)
3082 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
3084 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3086 * "When constructing a vector, there must be at least two Constituent
3089 vtn_assert(num_srcs
>= 2);
3091 unsigned dest_idx
= 0;
3092 for (unsigned i
= 0; i
< num_srcs
; i
++) {
3093 nir_ssa_def
*src
= srcs
[i
];
3094 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
3095 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
3096 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
3097 vec
->src
[dest_idx
].swizzle
[0] = j
;
3102 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3104 * "When constructing a vector, the total number of components in all
3105 * the operands must equal the number of components in Result Type."
3107 vtn_assert(dest_idx
== num_components
);
3109 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3111 return &vec
->dest
.dest
.ssa
;
3114 static struct vtn_ssa_value
*
3115 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3117 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3118 dest
->type
= src
->type
;
3120 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3121 dest
->def
= src
->def
;
3123 unsigned elems
= glsl_get_length(src
->type
);
3125 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3126 for (unsigned i
= 0; i
< elems
; i
++)
3127 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3133 static struct vtn_ssa_value
*
3134 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3135 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3136 unsigned num_indices
)
3138 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3140 struct vtn_ssa_value
*cur
= dest
;
3142 for (i
= 0; i
< num_indices
- 1; i
++) {
3143 cur
= cur
->elems
[indices
[i
]];
3146 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3147 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3148 * the component granularity. In that case, the last index will be
3149 * the index to insert the scalar into the vector.
3152 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
3154 cur
->elems
[indices
[i
]] = insert
;
3160 static struct vtn_ssa_value
*
3161 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3162 const uint32_t *indices
, unsigned num_indices
)
3164 struct vtn_ssa_value
*cur
= src
;
3165 for (unsigned i
= 0; i
< num_indices
; i
++) {
3166 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3167 vtn_assert(i
== num_indices
- 1);
3168 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3169 * the component granularity. The last index will be the index of the
3170 * vector to extract.
3173 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
3174 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
3175 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
3178 cur
= cur
->elems
[indices
[i
]];
3186 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3187 const uint32_t *w
, unsigned count
)
3189 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3190 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, type
->type
);
3193 case SpvOpVectorExtractDynamic
:
3194 ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3195 vtn_ssa_value(b
, w
[4])->def
);
3198 case SpvOpVectorInsertDynamic
:
3199 ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3200 vtn_ssa_value(b
, w
[4])->def
,
3201 vtn_ssa_value(b
, w
[5])->def
);
3204 case SpvOpVectorShuffle
:
3205 ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
->type
),
3206 vtn_ssa_value(b
, w
[3])->def
,
3207 vtn_ssa_value(b
, w
[4])->def
,
3211 case SpvOpCompositeConstruct
: {
3212 unsigned elems
= count
- 3;
3214 if (glsl_type_is_vector_or_scalar(type
->type
)) {
3215 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3216 for (unsigned i
= 0; i
< elems
; i
++)
3217 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
3219 vtn_vector_construct(b
, glsl_get_vector_elements(type
->type
),
3222 ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3223 for (unsigned i
= 0; i
< elems
; i
++)
3224 ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3228 case SpvOpCompositeExtract
:
3229 ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3233 case SpvOpCompositeInsert
:
3234 ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3235 vtn_ssa_value(b
, w
[3]),
3239 case SpvOpCopyLogical
:
3240 case SpvOpCopyObject
:
3241 ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3245 vtn_fail_with_opcode("unknown composite operation", opcode
);
3248 vtn_push_ssa(b
, w
[2], type
, ssa
);
3252 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3254 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3255 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3259 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3260 SpvMemorySemanticsMask semantics
)
3262 static const SpvMemorySemanticsMask all_memory_semantics
=
3263 SpvMemorySemanticsUniformMemoryMask
|
3264 SpvMemorySemanticsWorkgroupMemoryMask
|
3265 SpvMemorySemanticsAtomicCounterMemoryMask
|
3266 SpvMemorySemanticsImageMemoryMask
;
3268 /* If we're not actually doing a memory barrier, bail */
3269 if (!(semantics
& all_memory_semantics
))
3272 /* GL and Vulkan don't have these */
3273 vtn_assert(scope
!= SpvScopeCrossDevice
);
3275 if (scope
== SpvScopeSubgroup
)
3276 return; /* Nothing to do here */
3278 if (scope
== SpvScopeWorkgroup
) {
3279 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3283 /* There's only two scopes thing left */
3284 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3286 if ((semantics
& all_memory_semantics
) == all_memory_semantics
) {
3287 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3291 /* Issue a bunch of more specific barriers */
3292 uint32_t bits
= semantics
;
3294 SpvMemorySemanticsMask semantic
= 1 << u_bit_scan(&bits
);
3296 case SpvMemorySemanticsUniformMemoryMask
:
3297 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3299 case SpvMemorySemanticsWorkgroupMemoryMask
:
3300 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3302 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3303 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3305 case SpvMemorySemanticsImageMemoryMask
:
3306 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3315 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3316 const uint32_t *w
, unsigned count
)
3319 case SpvOpEmitVertex
:
3320 case SpvOpEmitStreamVertex
:
3321 case SpvOpEndPrimitive
:
3322 case SpvOpEndStreamPrimitive
: {
3323 nir_intrinsic_op intrinsic_op
;
3325 case SpvOpEmitVertex
:
3326 case SpvOpEmitStreamVertex
:
3327 intrinsic_op
= nir_intrinsic_emit_vertex
;
3329 case SpvOpEndPrimitive
:
3330 case SpvOpEndStreamPrimitive
:
3331 intrinsic_op
= nir_intrinsic_end_primitive
;
3334 unreachable("Invalid opcode");
3337 nir_intrinsic_instr
*intrin
=
3338 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3341 case SpvOpEmitStreamVertex
:
3342 case SpvOpEndStreamPrimitive
: {
3343 unsigned stream
= vtn_constant_uint(b
, w
[1]);
3344 nir_intrinsic_set_stream_id(intrin
, stream
);
3352 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3356 case SpvOpMemoryBarrier
: {
3357 SpvScope scope
= vtn_constant_uint(b
, w
[1]);
3358 SpvMemorySemanticsMask semantics
= vtn_constant_uint(b
, w
[2]);
3359 vtn_emit_memory_barrier(b
, scope
, semantics
);
3363 case SpvOpControlBarrier
: {
3364 SpvScope memory_scope
= vtn_constant_uint(b
, w
[2]);
3365 SpvMemorySemanticsMask memory_semantics
= vtn_constant_uint(b
, w
[3]);
3366 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
3368 SpvScope execution_scope
= vtn_constant_uint(b
, w
[1]);
3369 if (execution_scope
== SpvScopeWorkgroup
)
3370 vtn_emit_barrier(b
, nir_intrinsic_barrier
);
3375 unreachable("unknown barrier instruction");
3380 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
3381 SpvExecutionMode mode
)
3384 case SpvExecutionModeInputPoints
:
3385 case SpvExecutionModeOutputPoints
:
3386 return 0; /* GL_POINTS */
3387 case SpvExecutionModeInputLines
:
3388 return 1; /* GL_LINES */
3389 case SpvExecutionModeInputLinesAdjacency
:
3390 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3391 case SpvExecutionModeTriangles
:
3392 return 4; /* GL_TRIANGLES */
3393 case SpvExecutionModeInputTrianglesAdjacency
:
3394 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3395 case SpvExecutionModeQuads
:
3396 return 7; /* GL_QUADS */
3397 case SpvExecutionModeIsolines
:
3398 return 0x8E7A; /* GL_ISOLINES */
3399 case SpvExecutionModeOutputLineStrip
:
3400 return 3; /* GL_LINE_STRIP */
3401 case SpvExecutionModeOutputTriangleStrip
:
3402 return 5; /* GL_TRIANGLE_STRIP */
3404 vtn_fail("Invalid primitive type: %s (%u)",
3405 spirv_executionmode_to_string(mode
), mode
);
3410 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
3411 SpvExecutionMode mode
)
3414 case SpvExecutionModeInputPoints
:
3416 case SpvExecutionModeInputLines
:
3418 case SpvExecutionModeInputLinesAdjacency
:
3420 case SpvExecutionModeTriangles
:
3422 case SpvExecutionModeInputTrianglesAdjacency
:
3425 vtn_fail("Invalid GS input mode: %s (%u)",
3426 spirv_executionmode_to_string(mode
), mode
);
3430 static gl_shader_stage
3431 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
3434 case SpvExecutionModelVertex
:
3435 return MESA_SHADER_VERTEX
;
3436 case SpvExecutionModelTessellationControl
:
3437 return MESA_SHADER_TESS_CTRL
;
3438 case SpvExecutionModelTessellationEvaluation
:
3439 return MESA_SHADER_TESS_EVAL
;
3440 case SpvExecutionModelGeometry
:
3441 return MESA_SHADER_GEOMETRY
;
3442 case SpvExecutionModelFragment
:
3443 return MESA_SHADER_FRAGMENT
;
3444 case SpvExecutionModelGLCompute
:
3445 return MESA_SHADER_COMPUTE
;
3446 case SpvExecutionModelKernel
:
3447 return MESA_SHADER_KERNEL
;
3449 vtn_fail("Unsupported execution model: %s (%u)",
3450 spirv_executionmodel_to_string(model
), model
);
3454 #define spv_check_supported(name, cap) do { \
3455 if (!(b->options && b->options->caps.name)) \
3456 vtn_warn("Unsupported SPIR-V capability: %s (%u)", \
3457 spirv_capability_to_string(cap), cap); \
3462 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
3465 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
3466 /* Let this be a name label regardless */
3467 unsigned name_words
;
3468 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
3470 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
3471 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
3474 vtn_assert(b
->entry_point
== NULL
);
3475 b
->entry_point
= entry_point
;
3479 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3480 const uint32_t *w
, unsigned count
)
3487 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
3488 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
3489 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
3490 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
3491 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
3492 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
3495 uint32_t version
= w
[2];
3498 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
3500 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
3504 case SpvOpSourceExtension
:
3505 case SpvOpSourceContinued
:
3506 case SpvOpExtension
:
3507 case SpvOpModuleProcessed
:
3508 /* Unhandled, but these are for debug so that's ok. */
3511 case SpvOpCapability
: {
3512 SpvCapability cap
= w
[1];
3514 case SpvCapabilityMatrix
:
3515 case SpvCapabilityShader
:
3516 case SpvCapabilityGeometry
:
3517 case SpvCapabilityGeometryPointSize
:
3518 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
3519 case SpvCapabilitySampledImageArrayDynamicIndexing
:
3520 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
3521 case SpvCapabilityStorageImageArrayDynamicIndexing
:
3522 case SpvCapabilityImageRect
:
3523 case SpvCapabilitySampledRect
:
3524 case SpvCapabilitySampled1D
:
3525 case SpvCapabilityImage1D
:
3526 case SpvCapabilitySampledCubeArray
:
3527 case SpvCapabilityImageCubeArray
:
3528 case SpvCapabilitySampledBuffer
:
3529 case SpvCapabilityImageBuffer
:
3530 case SpvCapabilityImageQuery
:
3531 case SpvCapabilityDerivativeControl
:
3532 case SpvCapabilityInterpolationFunction
:
3533 case SpvCapabilityMultiViewport
:
3534 case SpvCapabilitySampleRateShading
:
3535 case SpvCapabilityClipDistance
:
3536 case SpvCapabilityCullDistance
:
3537 case SpvCapabilityInputAttachment
:
3538 case SpvCapabilityImageGatherExtended
:
3539 case SpvCapabilityStorageImageExtendedFormats
:
3542 case SpvCapabilityLinkage
:
3543 case SpvCapabilityVector16
:
3544 case SpvCapabilityFloat16Buffer
:
3545 case SpvCapabilitySparseResidency
:
3546 vtn_warn("Unsupported SPIR-V capability: %s",
3547 spirv_capability_to_string(cap
));
3550 case SpvCapabilityMinLod
:
3551 spv_check_supported(min_lod
, cap
);
3554 case SpvCapabilityAtomicStorage
:
3555 spv_check_supported(atomic_storage
, cap
);
3558 case SpvCapabilityFloat64
:
3559 spv_check_supported(float64
, cap
);
3561 case SpvCapabilityInt64
:
3562 spv_check_supported(int64
, cap
);
3564 case SpvCapabilityInt16
:
3565 spv_check_supported(int16
, cap
);
3567 case SpvCapabilityInt8
:
3568 spv_check_supported(int8
, cap
);
3571 case SpvCapabilityTransformFeedback
:
3572 spv_check_supported(transform_feedback
, cap
);
3575 case SpvCapabilityGeometryStreams
:
3576 spv_check_supported(geometry_streams
, cap
);
3579 case SpvCapabilityInt64Atomics
:
3580 spv_check_supported(int64_atomics
, cap
);
3583 case SpvCapabilityStorageImageMultisample
:
3584 spv_check_supported(storage_image_ms
, cap
);
3587 case SpvCapabilityAddresses
:
3588 spv_check_supported(address
, cap
);
3591 case SpvCapabilityKernel
:
3592 spv_check_supported(kernel
, cap
);
3595 case SpvCapabilityImageBasic
:
3596 case SpvCapabilityImageReadWrite
:
3597 case SpvCapabilityImageMipmap
:
3598 case SpvCapabilityPipes
:
3599 case SpvCapabilityDeviceEnqueue
:
3600 case SpvCapabilityLiteralSampler
:
3601 case SpvCapabilityGenericPointer
:
3602 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
3603 spirv_capability_to_string(cap
));
3606 case SpvCapabilityImageMSArray
:
3607 spv_check_supported(image_ms_array
, cap
);
3610 case SpvCapabilityTessellation
:
3611 case SpvCapabilityTessellationPointSize
:
3612 spv_check_supported(tessellation
, cap
);
3615 case SpvCapabilityDrawParameters
:
3616 spv_check_supported(draw_parameters
, cap
);
3619 case SpvCapabilityStorageImageReadWithoutFormat
:
3620 spv_check_supported(image_read_without_format
, cap
);
3623 case SpvCapabilityStorageImageWriteWithoutFormat
:
3624 spv_check_supported(image_write_without_format
, cap
);
3627 case SpvCapabilityDeviceGroup
:
3628 spv_check_supported(device_group
, cap
);
3631 case SpvCapabilityMultiView
:
3632 spv_check_supported(multiview
, cap
);
3635 case SpvCapabilityGroupNonUniform
:
3636 spv_check_supported(subgroup_basic
, cap
);
3639 case SpvCapabilitySubgroupVoteKHR
:
3640 case SpvCapabilityGroupNonUniformVote
:
3641 spv_check_supported(subgroup_vote
, cap
);
3644 case SpvCapabilitySubgroupBallotKHR
:
3645 case SpvCapabilityGroupNonUniformBallot
:
3646 spv_check_supported(subgroup_ballot
, cap
);
3649 case SpvCapabilityGroupNonUniformShuffle
:
3650 case SpvCapabilityGroupNonUniformShuffleRelative
:
3651 spv_check_supported(subgroup_shuffle
, cap
);
3654 case SpvCapabilityGroupNonUniformQuad
:
3655 spv_check_supported(subgroup_quad
, cap
);
3658 case SpvCapabilityGroupNonUniformArithmetic
:
3659 case SpvCapabilityGroupNonUniformClustered
:
3660 spv_check_supported(subgroup_arithmetic
, cap
);
3663 case SpvCapabilityGroups
:
3664 spv_check_supported(amd_shader_ballot
, cap
);
3667 case SpvCapabilityVariablePointersStorageBuffer
:
3668 case SpvCapabilityVariablePointers
:
3669 spv_check_supported(variable_pointers
, cap
);
3670 b
->variable_pointers
= true;
3673 case SpvCapabilityStorageUniformBufferBlock16
:
3674 case SpvCapabilityStorageUniform16
:
3675 case SpvCapabilityStoragePushConstant16
:
3676 case SpvCapabilityStorageInputOutput16
:
3677 spv_check_supported(storage_16bit
, cap
);
3680 case SpvCapabilityShaderViewportIndexLayerEXT
:
3681 spv_check_supported(shader_viewport_index_layer
, cap
);
3684 case SpvCapabilityStorageBuffer8BitAccess
:
3685 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
3686 case SpvCapabilityStoragePushConstant8
:
3687 spv_check_supported(storage_8bit
, cap
);
3690 case SpvCapabilityShaderNonUniformEXT
:
3691 spv_check_supported(descriptor_indexing
, cap
);
3694 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
3695 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
3696 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
3697 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
3700 case SpvCapabilityUniformBufferArrayNonUniformIndexingEXT
:
3701 case SpvCapabilitySampledImageArrayNonUniformIndexingEXT
:
3702 case SpvCapabilityStorageBufferArrayNonUniformIndexingEXT
:
3703 case SpvCapabilityStorageImageArrayNonUniformIndexingEXT
:
3704 case SpvCapabilityInputAttachmentArrayNonUniformIndexingEXT
:
3705 case SpvCapabilityUniformTexelBufferArrayNonUniformIndexingEXT
:
3706 case SpvCapabilityStorageTexelBufferArrayNonUniformIndexingEXT
:
3707 spv_check_supported(descriptor_array_non_uniform_indexing
, cap
);
3710 case SpvCapabilityRuntimeDescriptorArrayEXT
:
3711 spv_check_supported(runtime_descriptor_array
, cap
);
3714 case SpvCapabilityStencilExportEXT
:
3715 spv_check_supported(stencil_export
, cap
);
3718 case SpvCapabilitySampleMaskPostDepthCoverage
:
3719 spv_check_supported(post_depth_coverage
, cap
);
3722 case SpvCapabilityPhysicalStorageBufferAddressesEXT
:
3723 spv_check_supported(physical_storage_buffer_address
, cap
);
3726 case SpvCapabilityComputeDerivativeGroupQuadsNV
:
3727 case SpvCapabilityComputeDerivativeGroupLinearNV
:
3728 spv_check_supported(derivative_group
, cap
);
3731 case SpvCapabilityFloat16
:
3732 spv_check_supported(float16
, cap
);
3735 case SpvCapabilityFragmentShaderSampleInterlockEXT
:
3736 spv_check_supported(fragment_shader_sample_interlock
, cap
);
3739 case SpvCapabilityFragmentShaderPixelInterlockEXT
:
3740 spv_check_supported(fragment_shader_pixel_interlock
, cap
);
3743 case SpvCapabilityDemoteToHelperInvocationEXT
:
3744 spv_check_supported(demote_to_helper_invocation
, cap
);
3748 vtn_fail("Unhandled capability: %s (%u)",
3749 spirv_capability_to_string(cap
), cap
);
3754 case SpvOpExtInstImport
:
3755 vtn_handle_extension(b
, opcode
, w
, count
);
3758 case SpvOpMemoryModel
:
3760 case SpvAddressingModelPhysical32
:
3761 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
3762 "AddressingModelPhysical32 only supported for kernels");
3763 b
->shader
->info
.cs
.ptr_size
= 32;
3764 b
->physical_ptrs
= true;
3765 b
->options
->shared_addr_format
= nir_address_format_32bit_global
;
3766 b
->options
->global_addr_format
= nir_address_format_32bit_global
;
3767 b
->options
->temp_addr_format
= nir_address_format_32bit_global
;
3769 case SpvAddressingModelPhysical64
:
3770 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
3771 "AddressingModelPhysical64 only supported for kernels");
3772 b
->shader
->info
.cs
.ptr_size
= 64;
3773 b
->physical_ptrs
= true;
3774 b
->options
->shared_addr_format
= nir_address_format_64bit_global
;
3775 b
->options
->global_addr_format
= nir_address_format_64bit_global
;
3776 b
->options
->temp_addr_format
= nir_address_format_64bit_global
;
3778 case SpvAddressingModelLogical
:
3779 vtn_fail_if(b
->shader
->info
.stage
>= MESA_SHADER_STAGES
,
3780 "AddressingModelLogical only supported for shaders");
3781 b
->shader
->info
.cs
.ptr_size
= 0;
3782 b
->physical_ptrs
= false;
3784 case SpvAddressingModelPhysicalStorageBuffer64EXT
:
3785 vtn_fail_if(!b
->options
||
3786 !b
->options
->caps
.physical_storage_buffer_address
,
3787 "AddressingModelPhysicalStorageBuffer64EXT not supported");
3790 vtn_fail("Unknown addressing model: %s (%u)",
3791 spirv_addressingmodel_to_string(w
[1]), w
[1]);
3795 vtn_assert(w
[2] == SpvMemoryModelSimple
||
3796 w
[2] == SpvMemoryModelGLSL450
||
3797 w
[2] == SpvMemoryModelOpenCL
);
3800 case SpvOpEntryPoint
:
3801 vtn_handle_entry_point(b
, w
, count
);
3805 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
3806 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3810 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3813 case SpvOpMemberName
:
3817 case SpvOpExecutionMode
:
3818 case SpvOpExecutionModeId
:
3819 case SpvOpDecorationGroup
:
3821 case SpvOpDecorateId
:
3822 case SpvOpMemberDecorate
:
3823 case SpvOpGroupDecorate
:
3824 case SpvOpGroupMemberDecorate
:
3825 case SpvOpDecorateString
:
3826 case SpvOpMemberDecorateString
:
3827 vtn_handle_decoration(b
, opcode
, w
, count
);
3831 return false; /* End of preamble */
3838 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
3839 const struct vtn_decoration
*mode
, void *data
)
3841 vtn_assert(b
->entry_point
== entry_point
);
3843 switch(mode
->exec_mode
) {
3844 case SpvExecutionModeOriginUpperLeft
:
3845 case SpvExecutionModeOriginLowerLeft
:
3846 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3847 b
->shader
->info
.fs
.origin_upper_left
=
3848 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
3851 case SpvExecutionModeEarlyFragmentTests
:
3852 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3853 b
->shader
->info
.fs
.early_fragment_tests
= true;
3856 case SpvExecutionModePostDepthCoverage
:
3857 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3858 b
->shader
->info
.fs
.post_depth_coverage
= true;
3861 case SpvExecutionModeInvocations
:
3862 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3863 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->operands
[0]);
3866 case SpvExecutionModeDepthReplacing
:
3867 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3868 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
3870 case SpvExecutionModeDepthGreater
:
3871 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3872 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
3874 case SpvExecutionModeDepthLess
:
3875 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3876 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
3878 case SpvExecutionModeDepthUnchanged
:
3879 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3880 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
3883 case SpvExecutionModeLocalSize
:
3884 vtn_assert(gl_shader_stage_is_compute(b
->shader
->info
.stage
));
3885 b
->shader
->info
.cs
.local_size
[0] = mode
->operands
[0];
3886 b
->shader
->info
.cs
.local_size
[1] = mode
->operands
[1];
3887 b
->shader
->info
.cs
.local_size
[2] = mode
->operands
[2];
3890 case SpvExecutionModeLocalSizeId
:
3891 b
->shader
->info
.cs
.local_size
[0] = vtn_constant_uint(b
, mode
->operands
[0]);
3892 b
->shader
->info
.cs
.local_size
[1] = vtn_constant_uint(b
, mode
->operands
[1]);
3893 b
->shader
->info
.cs
.local_size
[2] = vtn_constant_uint(b
, mode
->operands
[2]);
3896 case SpvExecutionModeLocalSizeHint
:
3897 case SpvExecutionModeLocalSizeHintId
:
3898 break; /* Nothing to do with this */
3900 case SpvExecutionModeOutputVertices
:
3901 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3902 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3903 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->operands
[0];
3905 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3906 b
->shader
->info
.gs
.vertices_out
= mode
->operands
[0];
3910 case SpvExecutionModeInputPoints
:
3911 case SpvExecutionModeInputLines
:
3912 case SpvExecutionModeInputLinesAdjacency
:
3913 case SpvExecutionModeTriangles
:
3914 case SpvExecutionModeInputTrianglesAdjacency
:
3915 case SpvExecutionModeQuads
:
3916 case SpvExecutionModeIsolines
:
3917 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3918 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3919 b
->shader
->info
.tess
.primitive_mode
=
3920 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3922 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3923 b
->shader
->info
.gs
.vertices_in
=
3924 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
3925 b
->shader
->info
.gs
.input_primitive
=
3926 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3930 case SpvExecutionModeOutputPoints
:
3931 case SpvExecutionModeOutputLineStrip
:
3932 case SpvExecutionModeOutputTriangleStrip
:
3933 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3934 b
->shader
->info
.gs
.output_primitive
=
3935 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3938 case SpvExecutionModeSpacingEqual
:
3939 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3940 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3941 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
3943 case SpvExecutionModeSpacingFractionalEven
:
3944 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3945 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3946 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
3948 case SpvExecutionModeSpacingFractionalOdd
:
3949 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3950 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3951 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
3953 case SpvExecutionModeVertexOrderCw
:
3954 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3955 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3956 b
->shader
->info
.tess
.ccw
= false;
3958 case SpvExecutionModeVertexOrderCcw
:
3959 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3960 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3961 b
->shader
->info
.tess
.ccw
= true;
3963 case SpvExecutionModePointMode
:
3964 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3965 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3966 b
->shader
->info
.tess
.point_mode
= true;
3969 case SpvExecutionModePixelCenterInteger
:
3970 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3971 b
->shader
->info
.fs
.pixel_center_integer
= true;
3974 case SpvExecutionModeXfb
:
3975 b
->shader
->info
.has_transform_feedback_varyings
= true;
3978 case SpvExecutionModeVecTypeHint
:
3981 case SpvExecutionModeContractionOff
:
3982 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
3983 vtn_warn("ExectionMode only allowed for CL-style kernels: %s",
3984 spirv_executionmode_to_string(mode
->exec_mode
));
3989 case SpvExecutionModeStencilRefReplacingEXT
:
3990 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3993 case SpvExecutionModeDerivativeGroupQuadsNV
:
3994 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
3995 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_QUADS
;
3998 case SpvExecutionModeDerivativeGroupLinearNV
:
3999 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4000 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_LINEAR
;
4003 case SpvExecutionModePixelInterlockOrderedEXT
:
4004 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4005 b
->shader
->info
.fs
.pixel_interlock_ordered
= true;
4008 case SpvExecutionModePixelInterlockUnorderedEXT
:
4009 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4010 b
->shader
->info
.fs
.pixel_interlock_unordered
= true;
4013 case SpvExecutionModeSampleInterlockOrderedEXT
:
4014 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4015 b
->shader
->info
.fs
.sample_interlock_ordered
= true;
4018 case SpvExecutionModeSampleInterlockUnorderedEXT
:
4019 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4020 b
->shader
->info
.fs
.sample_interlock_unordered
= true;
4024 vtn_fail("Unhandled execution mode: %s (%u)",
4025 spirv_executionmode_to_string(mode
->exec_mode
),
4031 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4032 const uint32_t *w
, unsigned count
)
4034 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
4038 case SpvOpSourceContinued
:
4039 case SpvOpSourceExtension
:
4040 case SpvOpExtension
:
4041 case SpvOpCapability
:
4042 case SpvOpExtInstImport
:
4043 case SpvOpMemoryModel
:
4044 case SpvOpEntryPoint
:
4045 case SpvOpExecutionMode
:
4048 case SpvOpMemberName
:
4049 case SpvOpDecorationGroup
:
4051 case SpvOpDecorateId
:
4052 case SpvOpMemberDecorate
:
4053 case SpvOpGroupDecorate
:
4054 case SpvOpGroupMemberDecorate
:
4055 case SpvOpDecorateString
:
4056 case SpvOpMemberDecorateString
:
4057 vtn_fail("Invalid opcode types and variables section");
4063 case SpvOpTypeFloat
:
4064 case SpvOpTypeVector
:
4065 case SpvOpTypeMatrix
:
4066 case SpvOpTypeImage
:
4067 case SpvOpTypeSampler
:
4068 case SpvOpTypeSampledImage
:
4069 case SpvOpTypeArray
:
4070 case SpvOpTypeRuntimeArray
:
4071 case SpvOpTypeStruct
:
4072 case SpvOpTypeOpaque
:
4073 case SpvOpTypePointer
:
4074 case SpvOpTypeForwardPointer
:
4075 case SpvOpTypeFunction
:
4076 case SpvOpTypeEvent
:
4077 case SpvOpTypeDeviceEvent
:
4078 case SpvOpTypeReserveId
:
4079 case SpvOpTypeQueue
:
4081 vtn_handle_type(b
, opcode
, w
, count
);
4084 case SpvOpConstantTrue
:
4085 case SpvOpConstantFalse
:
4087 case SpvOpConstantComposite
:
4088 case SpvOpConstantSampler
:
4089 case SpvOpConstantNull
:
4090 case SpvOpSpecConstantTrue
:
4091 case SpvOpSpecConstantFalse
:
4092 case SpvOpSpecConstant
:
4093 case SpvOpSpecConstantComposite
:
4094 case SpvOpSpecConstantOp
:
4095 vtn_handle_constant(b
, opcode
, w
, count
);
4100 vtn_handle_variables(b
, opcode
, w
, count
);
4104 return false; /* End of preamble */
4110 static struct vtn_ssa_value
*
4111 vtn_nir_select(struct vtn_builder
*b
, struct vtn_ssa_value
*src0
,
4112 struct vtn_ssa_value
*src1
, struct vtn_ssa_value
*src2
)
4114 struct vtn_ssa_value
*dest
= rzalloc(b
, struct vtn_ssa_value
);
4115 dest
->type
= src1
->type
;
4117 if (glsl_type_is_vector_or_scalar(src1
->type
)) {
4118 dest
->def
= nir_bcsel(&b
->nb
, src0
->def
, src1
->def
, src2
->def
);
4120 unsigned elems
= glsl_get_length(src1
->type
);
4122 dest
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
4123 for (unsigned i
= 0; i
< elems
; i
++) {
4124 dest
->elems
[i
] = vtn_nir_select(b
, src0
,
4125 src1
->elems
[i
], src2
->elems
[i
]);
4133 vtn_handle_select(struct vtn_builder
*b
, SpvOp opcode
,
4134 const uint32_t *w
, unsigned count
)
4136 /* Handle OpSelect up-front here because it needs to be able to handle
4137 * pointers and not just regular vectors and scalars.
4139 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
4140 struct vtn_value
*cond_val
= vtn_untyped_value(b
, w
[3]);
4141 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
4142 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
4144 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4145 obj2_val
->type
!= res_val
->type
,
4146 "Object types must match the result type in OpSelect");
4148 vtn_fail_if((cond_val
->type
->base_type
!= vtn_base_type_scalar
&&
4149 cond_val
->type
->base_type
!= vtn_base_type_vector
) ||
4150 !glsl_type_is_boolean(cond_val
->type
->type
),
4151 "OpSelect must have either a vector of booleans or "
4152 "a boolean as Condition type");
4154 vtn_fail_if(cond_val
->type
->base_type
== vtn_base_type_vector
&&
4155 (res_val
->type
->base_type
!= vtn_base_type_vector
||
4156 res_val
->type
->length
!= cond_val
->type
->length
),
4157 "When Condition type in OpSelect is a vector, the Result "
4158 "type must be a vector of the same length");
4160 switch (res_val
->type
->base_type
) {
4161 case vtn_base_type_scalar
:
4162 case vtn_base_type_vector
:
4163 case vtn_base_type_matrix
:
4164 case vtn_base_type_array
:
4165 case vtn_base_type_struct
:
4168 case vtn_base_type_pointer
:
4169 /* We need to have actual storage for pointer types. */
4170 vtn_fail_if(res_val
->type
->type
== NULL
,
4171 "Invalid pointer result type for OpSelect");
4174 vtn_fail("Result type of OpSelect must be a scalar, composite, or pointer");
4177 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4178 struct vtn_ssa_value
*ssa
= vtn_nir_select(b
,
4179 vtn_ssa_value(b
, w
[3]), vtn_ssa_value(b
, w
[4]), vtn_ssa_value(b
, w
[5]));
4181 vtn_push_ssa(b
, w
[2], res_type
, ssa
);
4185 vtn_handle_ptr(struct vtn_builder
*b
, SpvOp opcode
,
4186 const uint32_t *w
, unsigned count
)
4188 struct vtn_type
*type1
= vtn_untyped_value(b
, w
[3])->type
;
4189 struct vtn_type
*type2
= vtn_untyped_value(b
, w
[4])->type
;
4190 vtn_fail_if(type1
->base_type
!= vtn_base_type_pointer
||
4191 type2
->base_type
!= vtn_base_type_pointer
,
4192 "%s operands must have pointer types",
4193 spirv_op_to_string(opcode
));
4194 vtn_fail_if(type1
->storage_class
!= type2
->storage_class
,
4195 "%s operands must have the same storage class",
4196 spirv_op_to_string(opcode
));
4198 struct vtn_type
*vtn_type
=
4199 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4200 const struct glsl_type
*type
= vtn_type
->type
;
4202 nir_address_format addr_format
= vtn_mode_to_address_format(
4203 b
, vtn_storage_class_to_mode(b
, type1
->storage_class
, NULL
, NULL
));
4208 case SpvOpPtrDiff
: {
4209 /* OpPtrDiff returns the difference in number of elements (not byte offset). */
4210 unsigned elem_size
, elem_align
;
4211 glsl_get_natural_size_align_bytes(type1
->deref
->type
,
4212 &elem_size
, &elem_align
);
4214 def
= nir_build_addr_isub(&b
->nb
,
4215 vtn_ssa_value(b
, w
[3])->def
,
4216 vtn_ssa_value(b
, w
[4])->def
,
4218 def
= nir_idiv(&b
->nb
, def
, nir_imm_intN_t(&b
->nb
, elem_size
, def
->bit_size
));
4219 def
= nir_i2i(&b
->nb
, def
, glsl_get_bit_size(type
));
4224 case SpvOpPtrNotEqual
: {
4225 def
= nir_build_addr_ieq(&b
->nb
,
4226 vtn_ssa_value(b
, w
[3])->def
,
4227 vtn_ssa_value(b
, w
[4])->def
,
4229 if (opcode
== SpvOpPtrNotEqual
)
4230 def
= nir_inot(&b
->nb
, def
);
4235 unreachable("Invalid ptr operation");
4238 struct vtn_ssa_value
*ssa_value
= vtn_create_ssa_value(b
, type
);
4239 ssa_value
->def
= def
;
4240 vtn_push_ssa(b
, w
[2], vtn_type
, ssa_value
);
4244 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4245 const uint32_t *w
, unsigned count
)
4251 case SpvOpLoopMerge
:
4252 case SpvOpSelectionMerge
:
4253 /* This is handled by cfg pre-pass and walk_blocks */
4257 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
4258 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4263 vtn_handle_extension(b
, opcode
, w
, count
);
4269 case SpvOpCopyMemory
:
4270 case SpvOpCopyMemorySized
:
4271 case SpvOpAccessChain
:
4272 case SpvOpPtrAccessChain
:
4273 case SpvOpInBoundsAccessChain
:
4274 case SpvOpInBoundsPtrAccessChain
:
4275 case SpvOpArrayLength
:
4276 case SpvOpConvertPtrToU
:
4277 case SpvOpConvertUToPtr
:
4278 vtn_handle_variables(b
, opcode
, w
, count
);
4281 case SpvOpFunctionCall
:
4282 vtn_handle_function_call(b
, opcode
, w
, count
);
4285 case SpvOpSampledImage
:
4287 case SpvOpImageSampleImplicitLod
:
4288 case SpvOpImageSampleExplicitLod
:
4289 case SpvOpImageSampleDrefImplicitLod
:
4290 case SpvOpImageSampleDrefExplicitLod
:
4291 case SpvOpImageSampleProjImplicitLod
:
4292 case SpvOpImageSampleProjExplicitLod
:
4293 case SpvOpImageSampleProjDrefImplicitLod
:
4294 case SpvOpImageSampleProjDrefExplicitLod
:
4295 case SpvOpImageFetch
:
4296 case SpvOpImageGather
:
4297 case SpvOpImageDrefGather
:
4298 case SpvOpImageQuerySizeLod
:
4299 case SpvOpImageQueryLod
:
4300 case SpvOpImageQueryLevels
:
4301 case SpvOpImageQuerySamples
:
4302 vtn_handle_texture(b
, opcode
, w
, count
);
4305 case SpvOpImageRead
:
4306 case SpvOpImageWrite
:
4307 case SpvOpImageTexelPointer
:
4308 vtn_handle_image(b
, opcode
, w
, count
);
4311 case SpvOpImageQuerySize
: {
4312 struct vtn_pointer
*image
=
4313 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
4314 if (glsl_type_is_image(image
->type
->type
)) {
4315 vtn_handle_image(b
, opcode
, w
, count
);
4317 vtn_assert(glsl_type_is_sampler(image
->type
->type
));
4318 vtn_handle_texture(b
, opcode
, w
, count
);
4323 case SpvOpAtomicLoad
:
4324 case SpvOpAtomicExchange
:
4325 case SpvOpAtomicCompareExchange
:
4326 case SpvOpAtomicCompareExchangeWeak
:
4327 case SpvOpAtomicIIncrement
:
4328 case SpvOpAtomicIDecrement
:
4329 case SpvOpAtomicIAdd
:
4330 case SpvOpAtomicISub
:
4331 case SpvOpAtomicSMin
:
4332 case SpvOpAtomicUMin
:
4333 case SpvOpAtomicSMax
:
4334 case SpvOpAtomicUMax
:
4335 case SpvOpAtomicAnd
:
4337 case SpvOpAtomicXor
: {
4338 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
4339 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4340 vtn_handle_image(b
, opcode
, w
, count
);
4342 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4343 vtn_handle_atomics(b
, opcode
, w
, count
);
4348 case SpvOpAtomicStore
: {
4349 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
4350 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4351 vtn_handle_image(b
, opcode
, w
, count
);
4353 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4354 vtn_handle_atomics(b
, opcode
, w
, count
);
4360 vtn_handle_select(b
, opcode
, w
, count
);
4368 case SpvOpConvertFToU
:
4369 case SpvOpConvertFToS
:
4370 case SpvOpConvertSToF
:
4371 case SpvOpConvertUToF
:
4375 case SpvOpQuantizeToF16
:
4376 case SpvOpPtrCastToGeneric
:
4377 case SpvOpGenericCastToPtr
:
4382 case SpvOpSignBitSet
:
4383 case SpvOpLessOrGreater
:
4385 case SpvOpUnordered
:
4400 case SpvOpVectorTimesScalar
:
4402 case SpvOpIAddCarry
:
4403 case SpvOpISubBorrow
:
4404 case SpvOpUMulExtended
:
4405 case SpvOpSMulExtended
:
4406 case SpvOpShiftRightLogical
:
4407 case SpvOpShiftRightArithmetic
:
4408 case SpvOpShiftLeftLogical
:
4409 case SpvOpLogicalEqual
:
4410 case SpvOpLogicalNotEqual
:
4411 case SpvOpLogicalOr
:
4412 case SpvOpLogicalAnd
:
4413 case SpvOpLogicalNot
:
4414 case SpvOpBitwiseOr
:
4415 case SpvOpBitwiseXor
:
4416 case SpvOpBitwiseAnd
:
4418 case SpvOpFOrdEqual
:
4419 case SpvOpFUnordEqual
:
4420 case SpvOpINotEqual
:
4421 case SpvOpFOrdNotEqual
:
4422 case SpvOpFUnordNotEqual
:
4423 case SpvOpULessThan
:
4424 case SpvOpSLessThan
:
4425 case SpvOpFOrdLessThan
:
4426 case SpvOpFUnordLessThan
:
4427 case SpvOpUGreaterThan
:
4428 case SpvOpSGreaterThan
:
4429 case SpvOpFOrdGreaterThan
:
4430 case SpvOpFUnordGreaterThan
:
4431 case SpvOpULessThanEqual
:
4432 case SpvOpSLessThanEqual
:
4433 case SpvOpFOrdLessThanEqual
:
4434 case SpvOpFUnordLessThanEqual
:
4435 case SpvOpUGreaterThanEqual
:
4436 case SpvOpSGreaterThanEqual
:
4437 case SpvOpFOrdGreaterThanEqual
:
4438 case SpvOpFUnordGreaterThanEqual
:
4444 case SpvOpFwidthFine
:
4445 case SpvOpDPdxCoarse
:
4446 case SpvOpDPdyCoarse
:
4447 case SpvOpFwidthCoarse
:
4448 case SpvOpBitFieldInsert
:
4449 case SpvOpBitFieldSExtract
:
4450 case SpvOpBitFieldUExtract
:
4451 case SpvOpBitReverse
:
4453 case SpvOpTranspose
:
4454 case SpvOpOuterProduct
:
4455 case SpvOpMatrixTimesScalar
:
4456 case SpvOpVectorTimesMatrix
:
4457 case SpvOpMatrixTimesVector
:
4458 case SpvOpMatrixTimesMatrix
:
4459 vtn_handle_alu(b
, opcode
, w
, count
);
4463 vtn_handle_bitcast(b
, w
, count
);
4466 case SpvOpVectorExtractDynamic
:
4467 case SpvOpVectorInsertDynamic
:
4468 case SpvOpVectorShuffle
:
4469 case SpvOpCompositeConstruct
:
4470 case SpvOpCompositeExtract
:
4471 case SpvOpCompositeInsert
:
4472 case SpvOpCopyLogical
:
4473 case SpvOpCopyObject
:
4474 vtn_handle_composite(b
, opcode
, w
, count
);
4477 case SpvOpEmitVertex
:
4478 case SpvOpEndPrimitive
:
4479 case SpvOpEmitStreamVertex
:
4480 case SpvOpEndStreamPrimitive
:
4481 case SpvOpControlBarrier
:
4482 case SpvOpMemoryBarrier
:
4483 vtn_handle_barrier(b
, opcode
, w
, count
);
4486 case SpvOpGroupNonUniformElect
:
4487 case SpvOpGroupNonUniformAll
:
4488 case SpvOpGroupNonUniformAny
:
4489 case SpvOpGroupNonUniformAllEqual
:
4490 case SpvOpGroupNonUniformBroadcast
:
4491 case SpvOpGroupNonUniformBroadcastFirst
:
4492 case SpvOpGroupNonUniformBallot
:
4493 case SpvOpGroupNonUniformInverseBallot
:
4494 case SpvOpGroupNonUniformBallotBitExtract
:
4495 case SpvOpGroupNonUniformBallotBitCount
:
4496 case SpvOpGroupNonUniformBallotFindLSB
:
4497 case SpvOpGroupNonUniformBallotFindMSB
:
4498 case SpvOpGroupNonUniformShuffle
:
4499 case SpvOpGroupNonUniformShuffleXor
:
4500 case SpvOpGroupNonUniformShuffleUp
:
4501 case SpvOpGroupNonUniformShuffleDown
:
4502 case SpvOpGroupNonUniformIAdd
:
4503 case SpvOpGroupNonUniformFAdd
:
4504 case SpvOpGroupNonUniformIMul
:
4505 case SpvOpGroupNonUniformFMul
:
4506 case SpvOpGroupNonUniformSMin
:
4507 case SpvOpGroupNonUniformUMin
:
4508 case SpvOpGroupNonUniformFMin
:
4509 case SpvOpGroupNonUniformSMax
:
4510 case SpvOpGroupNonUniformUMax
:
4511 case SpvOpGroupNonUniformFMax
:
4512 case SpvOpGroupNonUniformBitwiseAnd
:
4513 case SpvOpGroupNonUniformBitwiseOr
:
4514 case SpvOpGroupNonUniformBitwiseXor
:
4515 case SpvOpGroupNonUniformLogicalAnd
:
4516 case SpvOpGroupNonUniformLogicalOr
:
4517 case SpvOpGroupNonUniformLogicalXor
:
4518 case SpvOpGroupNonUniformQuadBroadcast
:
4519 case SpvOpGroupNonUniformQuadSwap
:
4522 case SpvOpGroupBroadcast
:
4523 case SpvOpGroupIAdd
:
4524 case SpvOpGroupFAdd
:
4525 case SpvOpGroupFMin
:
4526 case SpvOpGroupUMin
:
4527 case SpvOpGroupSMin
:
4528 case SpvOpGroupFMax
:
4529 case SpvOpGroupUMax
:
4530 case SpvOpGroupSMax
:
4531 case SpvOpSubgroupBallotKHR
:
4532 case SpvOpSubgroupFirstInvocationKHR
:
4533 case SpvOpSubgroupReadInvocationKHR
:
4534 case SpvOpSubgroupAllKHR
:
4535 case SpvOpSubgroupAnyKHR
:
4536 case SpvOpSubgroupAllEqualKHR
:
4537 case SpvOpGroupIAddNonUniformAMD
:
4538 case SpvOpGroupFAddNonUniformAMD
:
4539 case SpvOpGroupFMinNonUniformAMD
:
4540 case SpvOpGroupUMinNonUniformAMD
:
4541 case SpvOpGroupSMinNonUniformAMD
:
4542 case SpvOpGroupFMaxNonUniformAMD
:
4543 case SpvOpGroupUMaxNonUniformAMD
:
4544 case SpvOpGroupSMaxNonUniformAMD
:
4545 vtn_handle_subgroup(b
, opcode
, w
, count
);
4550 case SpvOpPtrNotEqual
:
4551 vtn_handle_ptr(b
, opcode
, w
, count
);
4554 case SpvOpBeginInvocationInterlockEXT
:
4555 vtn_emit_barrier(b
, nir_intrinsic_begin_invocation_interlock
);
4558 case SpvOpEndInvocationInterlockEXT
:
4559 vtn_emit_barrier(b
, nir_intrinsic_end_invocation_interlock
);
4562 case SpvOpDemoteToHelperInvocationEXT
: {
4563 nir_intrinsic_instr
*intrin
=
4564 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_demote
);
4565 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
4569 case SpvOpIsHelperInvocationEXT
: {
4570 nir_intrinsic_instr
*intrin
=
4571 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_is_helper_invocation
);
4572 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 1, 1, NULL
);
4573 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
4575 struct vtn_type
*res_type
=
4576 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4577 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, res_type
->type
);
4578 val
->def
= &intrin
->dest
.ssa
;
4580 vtn_push_ssa(b
, w
[2], res_type
, val
);
4585 vtn_fail_with_opcode("Unhandled opcode", opcode
);
4592 vtn_create_builder(const uint32_t *words
, size_t word_count
,
4593 gl_shader_stage stage
, const char *entry_point_name
,
4594 const struct spirv_to_nir_options
*options
)
4596 /* Initialize the vtn_builder object */
4597 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
4598 struct spirv_to_nir_options
*dup_options
=
4599 ralloc(b
, struct spirv_to_nir_options
);
4600 *dup_options
= *options
;
4603 b
->spirv_word_count
= word_count
;
4607 exec_list_make_empty(&b
->functions
);
4608 b
->entry_point_stage
= stage
;
4609 b
->entry_point_name
= entry_point_name
;
4610 b
->options
= dup_options
;
4613 * Handle the SPIR-V header (first 5 dwords).
4614 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
4616 if (word_count
<= 5)
4619 if (words
[0] != SpvMagicNumber
) {
4620 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
4623 if (words
[1] < 0x10000) {
4624 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
4628 uint16_t generator_id
= words
[2] >> 16;
4629 uint16_t generator_version
= words
[2];
4631 /* The first GLSLang version bump actually 1.5 years after #179 was fixed
4632 * but this should at least let us shut the workaround off for modern
4633 * versions of GLSLang.
4635 b
->wa_glslang_179
= (generator_id
== 8 && generator_version
== 1);
4637 /* words[2] == generator magic */
4638 unsigned value_id_bound
= words
[3];
4639 if (words
[4] != 0) {
4640 vtn_err("words[4] was %u, want 0", words
[4]);
4644 b
->value_id_bound
= value_id_bound
;
4645 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
4653 static nir_function
*
4654 vtn_emit_kernel_entry_point_wrapper(struct vtn_builder
*b
,
4655 nir_function
*entry_point
)
4657 vtn_assert(entry_point
== b
->entry_point
->func
->impl
->function
);
4658 vtn_fail_if(!entry_point
->name
, "entry points are required to have a name");
4659 const char *func_name
=
4660 ralloc_asprintf(b
->shader
, "__wrapped_%s", entry_point
->name
);
4662 /* we shouldn't have any inputs yet */
4663 vtn_assert(!entry_point
->shader
->num_inputs
);
4664 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
);
4666 nir_function
*main_entry_point
= nir_function_create(b
->shader
, func_name
);
4667 main_entry_point
->impl
= nir_function_impl_create(main_entry_point
);
4668 nir_builder_init(&b
->nb
, main_entry_point
->impl
);
4669 b
->nb
.cursor
= nir_after_cf_list(&main_entry_point
->impl
->body
);
4670 b
->func_param_idx
= 0;
4672 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, entry_point
);
4674 for (unsigned i
= 0; i
< entry_point
->num_params
; ++i
) {
4675 struct vtn_type
*param_type
= b
->entry_point
->func
->type
->params
[i
];
4677 /* consider all pointers to function memory to be parameters passed
4680 bool is_by_val
= param_type
->base_type
== vtn_base_type_pointer
&&
4681 param_type
->storage_class
== SpvStorageClassFunction
;
4683 /* input variable */
4684 nir_variable
*in_var
= rzalloc(b
->nb
.shader
, nir_variable
);
4685 in_var
->data
.mode
= nir_var_shader_in
;
4686 in_var
->data
.read_only
= true;
4687 in_var
->data
.location
= i
;
4690 in_var
->type
= param_type
->deref
->type
;
4692 in_var
->type
= param_type
->type
;
4694 nir_shader_add_variable(b
->nb
.shader
, in_var
);
4695 b
->nb
.shader
->num_inputs
++;
4697 /* we have to copy the entire variable into function memory */
4699 nir_variable
*copy_var
=
4700 nir_local_variable_create(main_entry_point
->impl
, in_var
->type
,
4702 nir_copy_var(&b
->nb
, copy_var
, in_var
);
4704 nir_src_for_ssa(&nir_build_deref_var(&b
->nb
, copy_var
)->dest
.ssa
);
4706 call
->params
[i
] = nir_src_for_ssa(nir_load_var(&b
->nb
, in_var
));
4710 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
4712 return main_entry_point
;
4716 spirv_to_nir(const uint32_t *words
, size_t word_count
,
4717 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
4718 gl_shader_stage stage
, const char *entry_point_name
,
4719 const struct spirv_to_nir_options
*options
,
4720 const nir_shader_compiler_options
*nir_options
)
4723 const uint32_t *word_end
= words
+ word_count
;
4725 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
4726 stage
, entry_point_name
,
4732 /* See also _vtn_fail() */
4733 if (setjmp(b
->fail_jump
)) {
4738 /* Skip the SPIR-V header, handled at vtn_create_builder */
4741 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
4743 /* Handle all the preamble instructions */
4744 words
= vtn_foreach_instruction(b
, words
, word_end
,
4745 vtn_handle_preamble_instruction
);
4747 if (b
->entry_point
== NULL
) {
4748 vtn_fail("Entry point not found");
4753 /* Set shader info defaults */
4754 b
->shader
->info
.gs
.invocations
= 1;
4756 b
->specializations
= spec
;
4757 b
->num_specializations
= num_spec
;
4759 /* Handle all variable, type, and constant instructions */
4760 words
= vtn_foreach_instruction(b
, words
, word_end
,
4761 vtn_handle_variable_or_type_instruction
);
4763 /* Parse execution modes */
4764 vtn_foreach_execution_mode(b
, b
->entry_point
,
4765 vtn_handle_execution_mode
, NULL
);
4767 if (b
->workgroup_size_builtin
) {
4768 vtn_assert(b
->workgroup_size_builtin
->type
->type
==
4769 glsl_vector_type(GLSL_TYPE_UINT
, 3));
4771 nir_const_value
*const_size
=
4772 b
->workgroup_size_builtin
->constant
->values
;
4774 b
->shader
->info
.cs
.local_size
[0] = const_size
[0].u32
;
4775 b
->shader
->info
.cs
.local_size
[1] = const_size
[1].u32
;
4776 b
->shader
->info
.cs
.local_size
[2] = const_size
[2].u32
;
4779 /* Set types on all vtn_values */
4780 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
4782 vtn_build_cfg(b
, words
, word_end
);
4784 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4785 b
->entry_point
->func
->referenced
= true;
4790 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
4791 if (func
->referenced
&& !func
->emitted
) {
4792 b
->const_table
= _mesa_pointer_hash_table_create(b
);
4794 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
4800 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4801 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
4802 vtn_assert(entry_point
);
4804 /* post process entry_points with input params */
4805 if (entry_point
->num_params
&& b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
4806 entry_point
= vtn_emit_kernel_entry_point_wrapper(b
, entry_point
);
4808 entry_point
->is_entrypoint
= true;
4810 /* When multiple shader stages exist in the same SPIR-V module, we
4811 * generate input and output variables for every stage, in the same
4812 * NIR program. These dead variables can be invalid NIR. For example,
4813 * TCS outputs must be per-vertex arrays (or decorated 'patch'), while
4814 * VS output variables wouldn't be.
4816 * To ensure we have valid NIR, we eliminate any dead inputs and outputs
4817 * right away. In order to do so, we must lower any constant initializers
4818 * on outputs so nir_remove_dead_variables sees that they're written to.
4820 nir_lower_constant_initializers(b
->shader
, nir_var_shader_out
);
4821 nir_remove_dead_variables(b
->shader
,
4822 nir_var_shader_in
| nir_var_shader_out
);
4824 /* We sometimes generate bogus derefs that, while never used, give the
4825 * validator a bit of heartburn. Run dead code to get rid of them.
4827 nir_opt_dce(b
->shader
);
4829 /* Unparent the shader from the vtn_builder before we delete the builder */
4830 ralloc_steal(NULL
, b
->shader
);
4832 nir_shader
*shader
= b
->shader
;