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"
38 vtn_log(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
39 size_t spirv_offset
, const char *message
)
41 if (b
->options
->debug
.func
) {
42 b
->options
->debug
.func(b
->options
->debug
.private_data
,
43 level
, spirv_offset
, message
);
47 if (level
>= NIR_SPIRV_DEBUG_LEVEL_WARNING
)
48 fprintf(stderr
, "%s\n", message
);
53 vtn_logf(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
54 size_t spirv_offset
, const char *fmt
, ...)
60 msg
= ralloc_vasprintf(NULL
, fmt
, args
);
63 vtn_log(b
, level
, spirv_offset
, msg
);
69 vtn_log_err(struct vtn_builder
*b
,
70 enum nir_spirv_debug_level level
, const char *prefix
,
71 const char *file
, unsigned line
,
72 const char *fmt
, va_list args
)
76 msg
= ralloc_strdup(NULL
, prefix
);
79 ralloc_asprintf_append(&msg
, " In file %s:%u\n", file
, line
);
82 ralloc_asprintf_append(&msg
, " ");
84 ralloc_vasprintf_append(&msg
, fmt
, args
);
86 ralloc_asprintf_append(&msg
, "\n %zu bytes into the SPIR-V binary",
90 ralloc_asprintf_append(&msg
,
91 "\n in SPIR-V source file %s, line %d, col %d",
92 b
->file
, b
->line
, b
->col
);
95 vtn_log(b
, level
, b
->spirv_offset
, msg
);
101 vtn_dump_shader(struct vtn_builder
*b
, const char *path
, const char *prefix
)
106 int len
= snprintf(filename
, sizeof(filename
), "%s/%s-%d.spirv",
107 path
, prefix
, idx
++);
108 if (len
< 0 || len
>= sizeof(filename
))
111 FILE *f
= fopen(filename
, "w");
115 fwrite(b
->spirv
, sizeof(*b
->spirv
), b
->spirv_word_count
, f
);
118 vtn_info("SPIR-V shader dumped to %s", filename
);
122 _vtn_warn(struct vtn_builder
*b
, const char *file
, unsigned line
,
123 const char *fmt
, ...)
128 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_WARNING
, "SPIR-V WARNING:\n",
129 file
, line
, fmt
, args
);
134 _vtn_err(struct vtn_builder
*b
, const char *file
, unsigned line
,
135 const char *fmt
, ...)
140 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V ERROR:\n",
141 file
, line
, fmt
, args
);
146 _vtn_fail(struct vtn_builder
*b
, const char *file
, unsigned line
,
147 const char *fmt
, ...)
152 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V parsing FAILED:\n",
153 file
, line
, fmt
, args
);
156 const char *dump_path
= getenv("MESA_SPIRV_FAIL_DUMP_PATH");
158 vtn_dump_shader(b
, dump_path
, "fail");
160 longjmp(b
->fail_jump
, 1);
163 struct spec_constant_value
{
171 static struct vtn_ssa_value
*
172 vtn_undef_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
174 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
177 if (glsl_type_is_vector_or_scalar(type
)) {
178 unsigned num_components
= glsl_get_vector_elements(val
->type
);
179 unsigned bit_size
= glsl_get_bit_size(val
->type
);
180 val
->def
= nir_ssa_undef(&b
->nb
, num_components
, bit_size
);
182 unsigned elems
= glsl_get_length(val
->type
);
183 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
184 if (glsl_type_is_matrix(type
)) {
185 const struct glsl_type
*elem_type
=
186 glsl_vector_type(glsl_get_base_type(type
),
187 glsl_get_vector_elements(type
));
189 for (unsigned i
= 0; i
< elems
; i
++)
190 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
191 } else if (glsl_type_is_array(type
)) {
192 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
193 for (unsigned i
= 0; i
< elems
; i
++)
194 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
196 for (unsigned i
= 0; i
< elems
; i
++) {
197 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
198 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
206 static struct vtn_ssa_value
*
207 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
208 const struct glsl_type
*type
)
210 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
215 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
218 switch (glsl_get_base_type(type
)) {
221 case GLSL_TYPE_INT16
:
222 case GLSL_TYPE_UINT16
:
223 case GLSL_TYPE_UINT8
:
225 case GLSL_TYPE_INT64
:
226 case GLSL_TYPE_UINT64
:
228 case GLSL_TYPE_FLOAT
:
229 case GLSL_TYPE_FLOAT16
:
230 case GLSL_TYPE_DOUBLE
: {
231 int bit_size
= glsl_get_bit_size(type
);
232 if (glsl_type_is_vector_or_scalar(type
)) {
233 unsigned num_components
= glsl_get_vector_elements(val
->type
);
234 nir_load_const_instr
*load
=
235 nir_load_const_instr_create(b
->shader
, num_components
, bit_size
);
237 load
->value
= constant
->values
[0];
239 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
240 val
->def
= &load
->def
;
242 assert(glsl_type_is_matrix(type
));
243 unsigned rows
= glsl_get_vector_elements(val
->type
);
244 unsigned columns
= glsl_get_matrix_columns(val
->type
);
245 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, columns
);
247 for (unsigned i
= 0; i
< columns
; i
++) {
248 struct vtn_ssa_value
*col_val
= rzalloc(b
, struct vtn_ssa_value
);
249 col_val
->type
= glsl_get_column_type(val
->type
);
250 nir_load_const_instr
*load
=
251 nir_load_const_instr_create(b
->shader
, rows
, bit_size
);
253 load
->value
= constant
->values
[i
];
255 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
256 col_val
->def
= &load
->def
;
258 val
->elems
[i
] = col_val
;
264 case GLSL_TYPE_ARRAY
: {
265 unsigned elems
= glsl_get_length(val
->type
);
266 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
267 const struct glsl_type
*elem_type
= glsl_get_array_element(val
->type
);
268 for (unsigned i
= 0; i
< elems
; i
++)
269 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
274 case GLSL_TYPE_STRUCT
: {
275 unsigned elems
= glsl_get_length(val
->type
);
276 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
277 for (unsigned i
= 0; i
< elems
; i
++) {
278 const struct glsl_type
*elem_type
=
279 glsl_get_struct_field(val
->type
, i
);
280 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
287 vtn_fail("bad constant type");
293 struct vtn_ssa_value
*
294 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
296 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
297 switch (val
->value_type
) {
298 case vtn_value_type_undef
:
299 return vtn_undef_ssa_value(b
, val
->type
->type
);
301 case vtn_value_type_constant
:
302 return vtn_const_ssa_value(b
, val
->constant
, val
->type
->type
);
304 case vtn_value_type_ssa
:
307 case vtn_value_type_pointer
:
308 vtn_assert(val
->pointer
->ptr_type
&& val
->pointer
->ptr_type
->type
);
309 struct vtn_ssa_value
*ssa
=
310 vtn_create_ssa_value(b
, val
->pointer
->ptr_type
->type
);
311 ssa
->def
= vtn_pointer_to_ssa(b
, val
->pointer
);
315 vtn_fail("Invalid type for an SSA value");
320 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
321 unsigned word_count
, unsigned *words_used
)
323 char *dup
= ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
325 /* Ammount of space taken by the string (including the null) */
326 unsigned len
= strlen(dup
) + 1;
327 *words_used
= DIV_ROUND_UP(len
, sizeof(*words
));
333 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
334 const uint32_t *end
, vtn_instruction_handler handler
)
340 const uint32_t *w
= start
;
342 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
343 unsigned count
= w
[0] >> SpvWordCountShift
;
344 vtn_assert(count
>= 1 && w
+ count
<= end
);
346 b
->spirv_offset
= (uint8_t *)w
- (uint8_t *)b
->spirv
;
350 break; /* Do nothing */
353 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
365 if (!handler(b
, opcode
, w
, count
))
383 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
384 const uint32_t *w
, unsigned count
)
387 case SpvOpExtInstImport
: {
388 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
389 if (strcmp((const char *)&w
[2], "GLSL.std.450") == 0) {
390 val
->ext_handler
= vtn_handle_glsl450_instruction
;
391 } else if ((strcmp((const char *)&w
[2], "SPV_AMD_gcn_shader") == 0)
392 && (b
->options
&& b
->options
->caps
.gcn_shader
)) {
393 val
->ext_handler
= vtn_handle_amd_gcn_shader_instruction
;
394 } else if ((strcmp((const char *)&w
[2], "SPV_AMD_shader_trinary_minmax") == 0)
395 && (b
->options
&& b
->options
->caps
.trinary_minmax
)) {
396 val
->ext_handler
= vtn_handle_amd_shader_trinary_minmax_instruction
;
398 vtn_fail("Unsupported extension");
404 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
405 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
411 vtn_fail("Unhandled opcode");
416 _foreach_decoration_helper(struct vtn_builder
*b
,
417 struct vtn_value
*base_value
,
419 struct vtn_value
*value
,
420 vtn_decoration_foreach_cb cb
, void *data
)
422 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
424 if (dec
->scope
== VTN_DEC_DECORATION
) {
425 member
= parent_member
;
426 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
427 vtn_fail_if(value
->value_type
!= vtn_value_type_type
||
428 value
->type
->base_type
!= vtn_base_type_struct
,
429 "OpMemberDecorate and OpGroupMemberDecorate are only "
430 "allowed on OpTypeStruct");
431 /* This means we haven't recursed yet */
432 assert(value
== base_value
);
434 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
436 vtn_fail_if(member
>= base_value
->type
->length
,
437 "OpMemberDecorate specifies member %d but the "
438 "OpTypeStruct has only %u members",
439 member
, base_value
->type
->length
);
441 /* Not a decoration */
442 assert(dec
->scope
== VTN_DEC_EXECUTION_MODE
);
447 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
448 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
451 cb(b
, base_value
, member
, dec
, data
);
456 /** Iterates (recursively if needed) over all of the decorations on a value
458 * This function iterates over all of the decorations applied to a given
459 * value. If it encounters a decoration group, it recurses into the group
460 * and iterates over all of those decorations as well.
463 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
464 vtn_decoration_foreach_cb cb
, void *data
)
466 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
470 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
471 vtn_execution_mode_foreach_cb cb
, void *data
)
473 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
474 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
477 assert(dec
->group
== NULL
);
478 cb(b
, value
, dec
, data
);
483 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
484 const uint32_t *w
, unsigned count
)
486 const uint32_t *w_end
= w
+ count
;
487 const uint32_t target
= w
[1];
491 case SpvOpDecorationGroup
:
492 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
496 case SpvOpMemberDecorate
:
497 case SpvOpExecutionMode
: {
498 struct vtn_value
*val
= vtn_untyped_value(b
, target
);
500 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
503 dec
->scope
= VTN_DEC_DECORATION
;
505 case SpvOpMemberDecorate
:
506 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
507 vtn_fail_if(dec
->scope
< VTN_DEC_STRUCT_MEMBER0
, /* overflow */
508 "Member argument of OpMemberDecorate too large");
510 case SpvOpExecutionMode
:
511 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
514 unreachable("Invalid decoration opcode");
516 dec
->decoration
= *(w
++);
519 /* Link into the list */
520 dec
->next
= val
->decoration
;
521 val
->decoration
= dec
;
525 case SpvOpGroupMemberDecorate
:
526 case SpvOpGroupDecorate
: {
527 struct vtn_value
*group
=
528 vtn_value(b
, target
, vtn_value_type_decoration_group
);
530 for (; w
< w_end
; w
++) {
531 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
532 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
535 if (opcode
== SpvOpGroupDecorate
) {
536 dec
->scope
= VTN_DEC_DECORATION
;
538 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
539 vtn_fail_if(dec
->scope
< 0, /* Check for overflow */
540 "Member argument of OpGroupMemberDecorate too large");
543 /* Link into the list */
544 dec
->next
= val
->decoration
;
545 val
->decoration
= dec
;
551 unreachable("Unhandled opcode");
555 struct member_decoration_ctx
{
557 struct glsl_struct_field
*fields
;
558 struct vtn_type
*type
;
561 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
562 * OpStore, or OpCopyMemory between them without breaking anything.
563 * Technically, the SPIR-V rules require the exact same type ID but this lets
564 * us internally be a bit looser.
567 vtn_types_compatible(struct vtn_builder
*b
,
568 struct vtn_type
*t1
, struct vtn_type
*t2
)
570 if (t1
->id
== t2
->id
)
573 if (t1
->base_type
!= t2
->base_type
)
576 switch (t1
->base_type
) {
577 case vtn_base_type_void
:
578 case vtn_base_type_scalar
:
579 case vtn_base_type_vector
:
580 case vtn_base_type_matrix
:
581 case vtn_base_type_image
:
582 case vtn_base_type_sampler
:
583 case vtn_base_type_sampled_image
:
584 return t1
->type
== t2
->type
;
586 case vtn_base_type_array
:
587 return t1
->length
== t2
->length
&&
588 vtn_types_compatible(b
, t1
->array_element
, t2
->array_element
);
590 case vtn_base_type_pointer
:
591 return vtn_types_compatible(b
, t1
->deref
, t2
->deref
);
593 case vtn_base_type_struct
:
594 if (t1
->length
!= t2
->length
)
597 for (unsigned i
= 0; i
< t1
->length
; i
++) {
598 if (!vtn_types_compatible(b
, t1
->members
[i
], t2
->members
[i
]))
603 case vtn_base_type_function
:
604 /* This case shouldn't get hit since you can't copy around function
605 * types. Just require them to be identical.
610 vtn_fail("Invalid base type");
613 /* does a shallow copy of a vtn_type */
615 static struct vtn_type
*
616 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
618 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
621 switch (src
->base_type
) {
622 case vtn_base_type_void
:
623 case vtn_base_type_scalar
:
624 case vtn_base_type_vector
:
625 case vtn_base_type_matrix
:
626 case vtn_base_type_array
:
627 case vtn_base_type_pointer
:
628 case vtn_base_type_image
:
629 case vtn_base_type_sampler
:
630 case vtn_base_type_sampled_image
:
631 /* Nothing more to do */
634 case vtn_base_type_struct
:
635 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
636 memcpy(dest
->members
, src
->members
,
637 src
->length
* sizeof(src
->members
[0]));
639 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
640 memcpy(dest
->offsets
, src
->offsets
,
641 src
->length
* sizeof(src
->offsets
[0]));
644 case vtn_base_type_function
:
645 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
646 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
653 static struct vtn_type
*
654 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
656 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
657 type
= type
->members
[member
];
659 /* We may have an array of matrices.... Oh, joy! */
660 while (glsl_type_is_array(type
->type
)) {
661 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
662 type
= type
->array_element
;
665 vtn_assert(glsl_type_is_matrix(type
->type
));
671 struct_member_decoration_cb(struct vtn_builder
*b
,
672 struct vtn_value
*val
, int member
,
673 const struct vtn_decoration
*dec
, void *void_ctx
)
675 struct member_decoration_ctx
*ctx
= void_ctx
;
680 assert(member
< ctx
->num_fields
);
682 switch (dec
->decoration
) {
683 case SpvDecorationNonWritable
:
684 case SpvDecorationNonReadable
:
685 case SpvDecorationRelaxedPrecision
:
686 case SpvDecorationVolatile
:
687 case SpvDecorationCoherent
:
688 case SpvDecorationUniform
:
689 break; /* FIXME: Do nothing with this for now. */
690 case SpvDecorationNoPerspective
:
691 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
693 case SpvDecorationFlat
:
694 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
696 case SpvDecorationCentroid
:
697 ctx
->fields
[member
].centroid
= true;
699 case SpvDecorationSample
:
700 ctx
->fields
[member
].sample
= true;
702 case SpvDecorationStream
:
703 /* Vulkan only allows one GS stream */
704 vtn_assert(dec
->literals
[0] == 0);
706 case SpvDecorationLocation
:
707 ctx
->fields
[member
].location
= dec
->literals
[0];
709 case SpvDecorationComponent
:
710 break; /* FIXME: What should we do with these? */
711 case SpvDecorationBuiltIn
:
712 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
713 ctx
->type
->members
[member
]->is_builtin
= true;
714 ctx
->type
->members
[member
]->builtin
= dec
->literals
[0];
715 ctx
->type
->builtin_block
= true;
717 case SpvDecorationOffset
:
718 ctx
->type
->offsets
[member
] = dec
->literals
[0];
720 case SpvDecorationMatrixStride
:
721 /* Handled as a second pass */
723 case SpvDecorationColMajor
:
724 break; /* Nothing to do here. Column-major is the default. */
725 case SpvDecorationRowMajor
:
726 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
729 case SpvDecorationPatch
:
732 case SpvDecorationSpecId
:
733 case SpvDecorationBlock
:
734 case SpvDecorationBufferBlock
:
735 case SpvDecorationArrayStride
:
736 case SpvDecorationGLSLShared
:
737 case SpvDecorationGLSLPacked
:
738 case SpvDecorationInvariant
:
739 case SpvDecorationRestrict
:
740 case SpvDecorationAliased
:
741 case SpvDecorationConstant
:
742 case SpvDecorationIndex
:
743 case SpvDecorationBinding
:
744 case SpvDecorationDescriptorSet
:
745 case SpvDecorationLinkageAttributes
:
746 case SpvDecorationNoContraction
:
747 case SpvDecorationInputAttachmentIndex
:
748 vtn_warn("Decoration not allowed on struct members: %s",
749 spirv_decoration_to_string(dec
->decoration
));
752 case SpvDecorationXfbBuffer
:
753 case SpvDecorationXfbStride
:
754 vtn_warn("Vulkan does not have transform feedback");
757 case SpvDecorationCPacked
:
758 case SpvDecorationSaturatedConversion
:
759 case SpvDecorationFuncParamAttr
:
760 case SpvDecorationFPRoundingMode
:
761 case SpvDecorationFPFastMathMode
:
762 case SpvDecorationAlignment
:
763 vtn_warn("Decoration only allowed for CL-style kernels: %s",
764 spirv_decoration_to_string(dec
->decoration
));
768 vtn_fail("Unhandled decoration");
772 /* Matrix strides are handled as a separate pass because we need to know
773 * whether the matrix is row-major or not first.
776 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
777 struct vtn_value
*val
, int member
,
778 const struct vtn_decoration
*dec
,
781 if (dec
->decoration
!= SpvDecorationMatrixStride
)
784 vtn_fail_if(member
< 0,
785 "The MatrixStride decoration is only allowed on members "
788 struct member_decoration_ctx
*ctx
= void_ctx
;
790 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
791 if (mat_type
->row_major
) {
792 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
793 mat_type
->stride
= mat_type
->array_element
->stride
;
794 mat_type
->array_element
->stride
= dec
->literals
[0];
796 vtn_assert(mat_type
->array_element
->stride
> 0);
797 mat_type
->stride
= dec
->literals
[0];
802 type_decoration_cb(struct vtn_builder
*b
,
803 struct vtn_value
*val
, int member
,
804 const struct vtn_decoration
*dec
, void *ctx
)
806 struct vtn_type
*type
= val
->type
;
809 /* This should have been handled by OpTypeStruct */
810 assert(val
->type
->base_type
== vtn_base_type_struct
);
811 assert(member
>= 0 && member
< val
->type
->length
);
815 switch (dec
->decoration
) {
816 case SpvDecorationArrayStride
:
817 vtn_assert(type
->base_type
== vtn_base_type_matrix
||
818 type
->base_type
== vtn_base_type_array
||
819 type
->base_type
== vtn_base_type_pointer
);
820 type
->stride
= dec
->literals
[0];
822 case SpvDecorationBlock
:
823 vtn_assert(type
->base_type
== vtn_base_type_struct
);
826 case SpvDecorationBufferBlock
:
827 vtn_assert(type
->base_type
== vtn_base_type_struct
);
828 type
->buffer_block
= true;
830 case SpvDecorationGLSLShared
:
831 case SpvDecorationGLSLPacked
:
832 /* Ignore these, since we get explicit offsets anyways */
835 case SpvDecorationRowMajor
:
836 case SpvDecorationColMajor
:
837 case SpvDecorationMatrixStride
:
838 case SpvDecorationBuiltIn
:
839 case SpvDecorationNoPerspective
:
840 case SpvDecorationFlat
:
841 case SpvDecorationPatch
:
842 case SpvDecorationCentroid
:
843 case SpvDecorationSample
:
844 case SpvDecorationVolatile
:
845 case SpvDecorationCoherent
:
846 case SpvDecorationNonWritable
:
847 case SpvDecorationNonReadable
:
848 case SpvDecorationUniform
:
849 case SpvDecorationStream
:
850 case SpvDecorationLocation
:
851 case SpvDecorationComponent
:
852 case SpvDecorationOffset
:
853 case SpvDecorationXfbBuffer
:
854 case SpvDecorationXfbStride
:
855 vtn_warn("Decoration only allowed for struct members: %s",
856 spirv_decoration_to_string(dec
->decoration
));
859 case SpvDecorationRelaxedPrecision
:
860 case SpvDecorationSpecId
:
861 case SpvDecorationInvariant
:
862 case SpvDecorationRestrict
:
863 case SpvDecorationAliased
:
864 case SpvDecorationConstant
:
865 case SpvDecorationIndex
:
866 case SpvDecorationBinding
:
867 case SpvDecorationDescriptorSet
:
868 case SpvDecorationLinkageAttributes
:
869 case SpvDecorationNoContraction
:
870 case SpvDecorationInputAttachmentIndex
:
871 vtn_warn("Decoration not allowed on types: %s",
872 spirv_decoration_to_string(dec
->decoration
));
875 case SpvDecorationCPacked
:
876 case SpvDecorationSaturatedConversion
:
877 case SpvDecorationFuncParamAttr
:
878 case SpvDecorationFPRoundingMode
:
879 case SpvDecorationFPFastMathMode
:
880 case SpvDecorationAlignment
:
881 vtn_warn("Decoration only allowed for CL-style kernels: %s",
882 spirv_decoration_to_string(dec
->decoration
));
886 vtn_fail("Unhandled decoration");
891 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
894 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
895 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
896 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
897 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
898 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
899 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
900 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
901 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
902 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
903 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
904 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
905 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
906 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
907 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
908 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
909 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
910 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
911 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
912 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
913 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
914 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
915 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
916 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
917 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
918 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
919 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
920 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
921 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
922 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
923 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
924 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
925 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
926 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
927 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
928 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
929 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
930 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
931 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
932 case SpvImageFormatR16ui
: return 0x8234; /* GL_R16UI */
933 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
935 vtn_fail("Invalid image format");
939 static struct vtn_type
*
940 vtn_type_layout_std430(struct vtn_builder
*b
, struct vtn_type
*type
,
941 uint32_t *size_out
, uint32_t *align_out
)
943 switch (type
->base_type
) {
944 case vtn_base_type_scalar
: {
945 uint32_t comp_size
= glsl_get_bit_size(type
->type
) / 8;
946 *size_out
= comp_size
;
947 *align_out
= comp_size
;
951 case vtn_base_type_vector
: {
952 uint32_t comp_size
= glsl_get_bit_size(type
->type
) / 8;
953 unsigned align_comps
= type
->length
== 3 ? 4 : type
->length
;
954 *size_out
= comp_size
* type
->length
,
955 *align_out
= comp_size
* align_comps
;
959 case vtn_base_type_matrix
:
960 case vtn_base_type_array
: {
961 /* We're going to add an array stride */
962 type
= vtn_type_copy(b
, type
);
963 uint32_t elem_size
, elem_align
;
964 type
->array_element
= vtn_type_layout_std430(b
, type
->array_element
,
965 &elem_size
, &elem_align
);
966 type
->stride
= vtn_align_u32(elem_size
, elem_align
);
967 *size_out
= type
->stride
* type
->length
;
968 *align_out
= elem_align
;
972 case vtn_base_type_struct
: {
973 /* We're going to add member offsets */
974 type
= vtn_type_copy(b
, type
);
977 for (unsigned i
= 0; i
< type
->length
; i
++) {
978 uint32_t mem_size
, mem_align
;
979 type
->members
[i
] = vtn_type_layout_std430(b
, type
->members
[i
],
980 &mem_size
, &mem_align
);
981 offset
= vtn_align_u32(offset
, mem_align
);
982 type
->offsets
[i
] = offset
;
984 align
= MAX2(align
, mem_align
);
992 unreachable("Invalid SPIR-V type for std430");
997 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
998 const uint32_t *w
, unsigned count
)
1000 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1002 val
->type
= rzalloc(b
, struct vtn_type
);
1003 val
->type
->id
= w
[1];
1007 val
->type
->base_type
= vtn_base_type_void
;
1008 val
->type
->type
= glsl_void_type();
1011 val
->type
->base_type
= vtn_base_type_scalar
;
1012 val
->type
->type
= glsl_bool_type();
1013 val
->type
->length
= 1;
1015 case SpvOpTypeInt
: {
1016 int bit_size
= w
[2];
1017 const bool signedness
= w
[3];
1018 val
->type
->base_type
= vtn_base_type_scalar
;
1021 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1024 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1027 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1030 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1033 vtn_fail("Invalid int bit size");
1035 val
->type
->length
= 1;
1039 case SpvOpTypeFloat
: {
1040 int bit_size
= w
[2];
1041 val
->type
->base_type
= vtn_base_type_scalar
;
1044 val
->type
->type
= glsl_float16_t_type();
1047 val
->type
->type
= glsl_float_type();
1050 val
->type
->type
= glsl_double_type();
1053 vtn_fail("Invalid float bit size");
1055 val
->type
->length
= 1;
1059 case SpvOpTypeVector
: {
1060 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1061 unsigned elems
= w
[3];
1063 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1064 "Base type for OpTypeVector must be a scalar");
1065 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1066 "Invalid component count for OpTypeVector");
1068 val
->type
->base_type
= vtn_base_type_vector
;
1069 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1070 val
->type
->length
= elems
;
1071 val
->type
->stride
= glsl_get_bit_size(base
->type
) / 8;
1072 val
->type
->array_element
= base
;
1076 case SpvOpTypeMatrix
: {
1077 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1078 unsigned columns
= w
[3];
1080 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1081 "Base type for OpTypeMatrix must be a vector");
1082 vtn_fail_if(columns
< 2 || columns
> 4,
1083 "Invalid column count for OpTypeMatrix");
1085 val
->type
->base_type
= vtn_base_type_matrix
;
1086 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1087 glsl_get_vector_elements(base
->type
),
1089 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1090 "Unsupported base type for OpTypeMatrix");
1091 assert(!glsl_type_is_error(val
->type
->type
));
1092 val
->type
->length
= columns
;
1093 val
->type
->array_element
= base
;
1094 val
->type
->row_major
= false;
1095 val
->type
->stride
= 0;
1099 case SpvOpTypeRuntimeArray
:
1100 case SpvOpTypeArray
: {
1101 struct vtn_type
*array_element
=
1102 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1104 if (opcode
== SpvOpTypeRuntimeArray
) {
1105 /* A length of 0 is used to denote unsized arrays */
1106 val
->type
->length
= 0;
1109 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
1112 val
->type
->base_type
= vtn_base_type_array
;
1113 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
);
1114 val
->type
->array_element
= array_element
;
1115 val
->type
->stride
= 0;
1119 case SpvOpTypeStruct
: {
1120 unsigned num_fields
= count
- 2;
1121 val
->type
->base_type
= vtn_base_type_struct
;
1122 val
->type
->length
= num_fields
;
1123 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1124 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1126 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1127 for (unsigned i
= 0; i
< num_fields
; i
++) {
1128 val
->type
->members
[i
] =
1129 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
1130 fields
[i
] = (struct glsl_struct_field
) {
1131 .type
= val
->type
->members
[i
]->type
,
1132 .name
= ralloc_asprintf(b
, "field%d", i
),
1137 struct member_decoration_ctx ctx
= {
1138 .num_fields
= num_fields
,
1143 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1144 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1146 const char *name
= val
->name
? val
->name
: "struct";
1148 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
);
1152 case SpvOpTypeFunction
: {
1153 val
->type
->base_type
= vtn_base_type_function
;
1154 val
->type
->type
= NULL
;
1156 val
->type
->return_type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1158 const unsigned num_params
= count
- 3;
1159 val
->type
->length
= num_params
;
1160 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1161 for (unsigned i
= 0; i
< count
- 3; i
++) {
1162 val
->type
->params
[i
] =
1163 vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
;
1168 case SpvOpTypePointer
: {
1169 SpvStorageClass storage_class
= w
[2];
1170 struct vtn_type
*deref_type
=
1171 vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
1173 val
->type
->base_type
= vtn_base_type_pointer
;
1174 val
->type
->storage_class
= storage_class
;
1175 val
->type
->deref
= deref_type
;
1177 if (storage_class
== SpvStorageClassUniform
||
1178 storage_class
== SpvStorageClassStorageBuffer
) {
1179 /* These can actually be stored to nir_variables and used as SSA
1180 * values so they need a real glsl_type.
1182 val
->type
->type
= glsl_vector_type(GLSL_TYPE_UINT
, 2);
1185 if (storage_class
== SpvStorageClassPushConstant
) {
1186 /* These can actually be stored to nir_variables and used as SSA
1187 * values so they need a real glsl_type.
1189 val
->type
->type
= glsl_uint_type();
1192 if (storage_class
== SpvStorageClassWorkgroup
&&
1193 b
->options
->lower_workgroup_access_to_offsets
) {
1194 uint32_t size
, align
;
1195 val
->type
->deref
= vtn_type_layout_std430(b
, val
->type
->deref
,
1197 val
->type
->length
= size
;
1198 val
->type
->align
= align
;
1199 /* These can actually be stored to nir_variables and used as SSA
1200 * values so they need a real glsl_type.
1202 val
->type
->type
= glsl_uint_type();
1207 case SpvOpTypeImage
: {
1208 val
->type
->base_type
= vtn_base_type_image
;
1210 const struct vtn_type
*sampled_type
=
1211 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1213 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1214 glsl_get_bit_size(sampled_type
->type
) != 32,
1215 "Sampled type of OpTypeImage must be a 32-bit scalar");
1217 enum glsl_sampler_dim dim
;
1218 switch ((SpvDim
)w
[3]) {
1219 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1220 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1221 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1222 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1223 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1224 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1225 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1227 vtn_fail("Invalid SPIR-V image dimensionality");
1230 bool is_shadow
= w
[4];
1231 bool is_array
= w
[5];
1232 bool multisampled
= w
[6];
1233 unsigned sampled
= w
[7];
1234 SpvImageFormat format
= w
[8];
1237 val
->type
->access_qualifier
= w
[9];
1239 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1242 if (dim
== GLSL_SAMPLER_DIM_2D
)
1243 dim
= GLSL_SAMPLER_DIM_MS
;
1244 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1245 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1247 vtn_fail("Unsupported multisampled image type");
1250 val
->type
->image_format
= translate_image_format(b
, format
);
1252 enum glsl_base_type sampled_base_type
=
1253 glsl_get_base_type(sampled_type
->type
);
1255 val
->type
->sampled
= true;
1256 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
1258 } else if (sampled
== 2) {
1259 vtn_assert(!is_shadow
);
1260 val
->type
->sampled
= false;
1261 val
->type
->type
= glsl_image_type(dim
, is_array
, sampled_base_type
);
1263 vtn_fail("We need to know if the image will be sampled");
1268 case SpvOpTypeSampledImage
:
1269 val
->type
->base_type
= vtn_base_type_sampled_image
;
1270 val
->type
->image
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1271 val
->type
->type
= val
->type
->image
->type
;
1274 case SpvOpTypeSampler
:
1275 /* The actual sampler type here doesn't really matter. It gets
1276 * thrown away the moment you combine it with an image. What really
1277 * matters is that it's a sampler type as opposed to an integer type
1278 * so the backend knows what to do.
1280 val
->type
->base_type
= vtn_base_type_sampler
;
1281 val
->type
->type
= glsl_bare_sampler_type();
1284 case SpvOpTypeOpaque
:
1285 case SpvOpTypeEvent
:
1286 case SpvOpTypeDeviceEvent
:
1287 case SpvOpTypeReserveId
:
1288 case SpvOpTypeQueue
:
1291 vtn_fail("Unhandled opcode");
1294 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1297 static nir_constant
*
1298 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
1300 nir_constant
*c
= rzalloc(b
, nir_constant
);
1302 /* For pointers and other typeless things, we have to return something but
1303 * it doesn't matter what.
1308 switch (glsl_get_base_type(type
)) {
1310 case GLSL_TYPE_UINT
:
1311 case GLSL_TYPE_INT16
:
1312 case GLSL_TYPE_UINT16
:
1313 case GLSL_TYPE_UINT8
:
1314 case GLSL_TYPE_INT8
:
1315 case GLSL_TYPE_INT64
:
1316 case GLSL_TYPE_UINT64
:
1317 case GLSL_TYPE_BOOL
:
1318 case GLSL_TYPE_FLOAT
:
1319 case GLSL_TYPE_FLOAT16
:
1320 case GLSL_TYPE_DOUBLE
:
1321 /* Nothing to do here. It's already initialized to zero */
1324 case GLSL_TYPE_ARRAY
:
1325 vtn_assert(glsl_get_length(type
) > 0);
1326 c
->num_elements
= glsl_get_length(type
);
1327 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1329 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
1330 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1331 c
->elements
[i
] = c
->elements
[0];
1334 case GLSL_TYPE_STRUCT
:
1335 c
->num_elements
= glsl_get_length(type
);
1336 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1338 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
1339 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
1344 vtn_fail("Invalid type for null constant");
1351 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
1352 int member
, const struct vtn_decoration
*dec
,
1355 vtn_assert(member
== -1);
1356 if (dec
->decoration
!= SpvDecorationSpecId
)
1359 struct spec_constant_value
*const_value
= data
;
1361 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1362 if (b
->specializations
[i
].id
== dec
->literals
[0]) {
1363 if (const_value
->is_double
)
1364 const_value
->data64
= b
->specializations
[i
].data64
;
1366 const_value
->data32
= b
->specializations
[i
].data32
;
1373 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
1374 uint32_t const_value
)
1376 struct spec_constant_value data
;
1377 data
.is_double
= false;
1378 data
.data32
= const_value
;
1379 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1384 get_specialization64(struct vtn_builder
*b
, struct vtn_value
*val
,
1385 uint64_t const_value
)
1387 struct spec_constant_value data
;
1388 data
.is_double
= true;
1389 data
.data64
= const_value
;
1390 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1395 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1396 struct vtn_value
*val
,
1398 const struct vtn_decoration
*dec
,
1401 vtn_assert(member
== -1);
1402 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1403 dec
->literals
[0] != SpvBuiltInWorkgroupSize
)
1406 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1408 b
->shader
->info
.cs
.local_size
[0] = val
->constant
->values
[0].u32
[0];
1409 b
->shader
->info
.cs
.local_size
[1] = val
->constant
->values
[0].u32
[1];
1410 b
->shader
->info
.cs
.local_size
[2] = val
->constant
->values
[0].u32
[2];
1414 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1415 const uint32_t *w
, unsigned count
)
1417 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1418 val
->constant
= rzalloc(b
, nir_constant
);
1420 case SpvOpConstantTrue
:
1421 case SpvOpConstantFalse
:
1422 case SpvOpSpecConstantTrue
:
1423 case SpvOpSpecConstantFalse
: {
1424 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1425 "Result type of %s must be OpTypeBool",
1426 spirv_op_to_string(opcode
));
1428 uint32_t int_val
= (opcode
== SpvOpConstantTrue
||
1429 opcode
== SpvOpSpecConstantTrue
);
1431 if (opcode
== SpvOpSpecConstantTrue
||
1432 opcode
== SpvOpSpecConstantFalse
)
1433 int_val
= get_specialization(b
, val
, int_val
);
1435 val
->constant
->values
[0].u32
[0] = int_val
? NIR_TRUE
: NIR_FALSE
;
1439 case SpvOpConstant
: {
1440 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1441 "Result type of %s must be a scalar",
1442 spirv_op_to_string(opcode
));
1443 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1446 val
->constant
->values
->u64
[0] = vtn_u64_literal(&w
[3]);
1449 val
->constant
->values
->u32
[0] = w
[3];
1452 val
->constant
->values
->u16
[0] = w
[3];
1455 val
->constant
->values
->u8
[0] = w
[3];
1458 vtn_fail("Unsupported SpvOpConstant bit size");
1463 case SpvOpSpecConstant
: {
1464 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1465 "Result type of %s must be a scalar",
1466 spirv_op_to_string(opcode
));
1467 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1470 val
->constant
->values
[0].u64
[0] =
1471 get_specialization64(b
, val
, vtn_u64_literal(&w
[3]));
1474 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1477 val
->constant
->values
[0].u16
[0] = get_specialization(b
, val
, w
[3]);
1480 val
->constant
->values
[0].u8
[0] = get_specialization(b
, val
, w
[3]);
1483 vtn_fail("Unsupported SpvOpSpecConstant bit size");
1488 case SpvOpSpecConstantComposite
:
1489 case SpvOpConstantComposite
: {
1490 unsigned elem_count
= count
- 3;
1491 vtn_fail_if(elem_count
!= val
->type
->length
,
1492 "%s has %u constituents, expected %u",
1493 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1495 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1496 for (unsigned i
= 0; i
< elem_count
; i
++)
1497 elems
[i
] = vtn_value(b
, w
[i
+ 3], vtn_value_type_constant
)->constant
;
1499 switch (val
->type
->base_type
) {
1500 case vtn_base_type_vector
: {
1501 assert(glsl_type_is_vector(val
->type
->type
));
1502 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1503 for (unsigned i
= 0; i
< elem_count
; i
++) {
1506 val
->constant
->values
[0].u64
[i
] = elems
[i
]->values
[0].u64
[0];
1509 val
->constant
->values
[0].u32
[i
] = elems
[i
]->values
[0].u32
[0];
1512 val
->constant
->values
[0].u16
[i
] = elems
[i
]->values
[0].u16
[0];
1515 val
->constant
->values
[0].u8
[i
] = elems
[i
]->values
[0].u8
[0];
1518 vtn_fail("Invalid SpvOpConstantComposite bit size");
1524 case vtn_base_type_matrix
:
1525 assert(glsl_type_is_matrix(val
->type
->type
));
1526 for (unsigned i
= 0; i
< elem_count
; i
++)
1527 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1530 case vtn_base_type_struct
:
1531 case vtn_base_type_array
:
1532 ralloc_steal(val
->constant
, elems
);
1533 val
->constant
->num_elements
= elem_count
;
1534 val
->constant
->elements
= elems
;
1538 vtn_fail("Result type of %s must be a composite type",
1539 spirv_op_to_string(opcode
));
1544 case SpvOpSpecConstantOp
: {
1545 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1547 case SpvOpVectorShuffle
: {
1548 struct vtn_value
*v0
= &b
->values
[w
[4]];
1549 struct vtn_value
*v1
= &b
->values
[w
[5]];
1551 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1552 v0
->value_type
== vtn_value_type_undef
);
1553 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1554 v1
->value_type
== vtn_value_type_undef
);
1556 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1557 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1559 vtn_assert(len0
+ len1
< 16);
1561 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1562 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1563 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1565 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1566 (void)bit_size0
; (void)bit_size1
;
1568 if (bit_size
== 64) {
1570 if (v0
->value_type
== vtn_value_type_constant
) {
1571 for (unsigned i
= 0; i
< len0
; i
++)
1572 u64
[i
] = v0
->constant
->values
[0].u64
[i
];
1574 if (v1
->value_type
== vtn_value_type_constant
) {
1575 for (unsigned i
= 0; i
< len1
; i
++)
1576 u64
[len0
+ i
] = v1
->constant
->values
[0].u64
[i
];
1579 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1580 uint32_t comp
= w
[i
+ 6];
1581 /* If component is not used, set the value to a known constant
1582 * to detect if it is wrongly used.
1584 if (comp
== (uint32_t)-1)
1585 val
->constant
->values
[0].u64
[j
] = 0xdeadbeefdeadbeef;
1587 val
->constant
->values
[0].u64
[j
] = u64
[comp
];
1590 /* This is for both 32-bit and 16-bit values */
1592 if (v0
->value_type
== vtn_value_type_constant
) {
1593 for (unsigned i
= 0; i
< len0
; i
++)
1594 u32
[i
] = v0
->constant
->values
[0].u32
[i
];
1596 if (v1
->value_type
== vtn_value_type_constant
) {
1597 for (unsigned i
= 0; i
< len1
; i
++)
1598 u32
[len0
+ i
] = v1
->constant
->values
[0].u32
[i
];
1601 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1602 uint32_t comp
= w
[i
+ 6];
1603 /* If component is not used, set the value to a known constant
1604 * to detect if it is wrongly used.
1606 if (comp
== (uint32_t)-1)
1607 val
->constant
->values
[0].u32
[j
] = 0xdeadbeef;
1609 val
->constant
->values
[0].u32
[j
] = u32
[comp
];
1615 case SpvOpCompositeExtract
:
1616 case SpvOpCompositeInsert
: {
1617 struct vtn_value
*comp
;
1618 unsigned deref_start
;
1619 struct nir_constant
**c
;
1620 if (opcode
== SpvOpCompositeExtract
) {
1621 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1623 c
= &comp
->constant
;
1625 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1627 val
->constant
= nir_constant_clone(comp
->constant
,
1634 const struct vtn_type
*type
= comp
->type
;
1635 for (unsigned i
= deref_start
; i
< count
; i
++) {
1636 vtn_fail_if(w
[i
] > type
->length
,
1637 "%uth index of %s is %u but the type has only "
1638 "%u elements", i
- deref_start
,
1639 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1641 switch (type
->base_type
) {
1642 case vtn_base_type_vector
:
1644 type
= type
->array_element
;
1647 case vtn_base_type_matrix
:
1648 assert(col
== 0 && elem
== -1);
1651 type
= type
->array_element
;
1654 case vtn_base_type_array
:
1655 c
= &(*c
)->elements
[w
[i
]];
1656 type
= type
->array_element
;
1659 case vtn_base_type_struct
:
1660 c
= &(*c
)->elements
[w
[i
]];
1661 type
= type
->members
[w
[i
]];
1665 vtn_fail("%s must only index into composite types",
1666 spirv_op_to_string(opcode
));
1670 if (opcode
== SpvOpCompositeExtract
) {
1674 unsigned num_components
= type
->length
;
1675 unsigned bit_size
= glsl_get_bit_size(type
->type
);
1676 for (unsigned i
= 0; i
< num_components
; i
++)
1679 val
->constant
->values
[0].u64
[i
] = (*c
)->values
[col
].u64
[elem
+ i
];
1682 val
->constant
->values
[0].u32
[i
] = (*c
)->values
[col
].u32
[elem
+ i
];
1685 val
->constant
->values
[0].u16
[i
] = (*c
)->values
[col
].u16
[elem
+ i
];
1688 val
->constant
->values
[0].u8
[i
] = (*c
)->values
[col
].u8
[elem
+ i
];
1691 vtn_fail("Invalid SpvOpCompositeExtract bit size");
1695 struct vtn_value
*insert
=
1696 vtn_value(b
, w
[4], vtn_value_type_constant
);
1697 vtn_assert(insert
->type
== type
);
1699 *c
= insert
->constant
;
1701 unsigned num_components
= type
->length
;
1702 unsigned bit_size
= glsl_get_bit_size(type
->type
);
1703 for (unsigned i
= 0; i
< num_components
; i
++)
1706 (*c
)->values
[col
].u64
[elem
+ i
] = insert
->constant
->values
[0].u64
[i
];
1709 (*c
)->values
[col
].u32
[elem
+ i
] = insert
->constant
->values
[0].u32
[i
];
1712 (*c
)->values
[col
].u16
[elem
+ i
] = insert
->constant
->values
[0].u16
[i
];
1715 (*c
)->values
[col
].u8
[elem
+ i
] = insert
->constant
->values
[0].u8
[i
];
1718 vtn_fail("Invalid SpvOpCompositeInsert bit size");
1727 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
1728 nir_alu_type src_alu_type
= dst_alu_type
;
1729 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
1732 vtn_assert(count
<= 7);
1737 /* We have a source in a conversion */
1739 nir_get_nir_type_for_glsl_type(
1740 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1741 /* We use the bitsize of the conversion source to evaluate the opcode later */
1742 bit_size
= glsl_get_bit_size(
1743 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1746 bit_size
= glsl_get_bit_size(val
->type
->type
);
1749 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
1750 nir_alu_type_get_type_size(src_alu_type
),
1751 nir_alu_type_get_type_size(dst_alu_type
));
1752 nir_const_value src
[4];
1754 for (unsigned i
= 0; i
< count
- 4; i
++) {
1756 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
)->constant
;
1758 unsigned j
= swap
? 1 - i
: i
;
1759 src
[j
] = c
->values
[0];
1762 val
->constant
->values
[0] =
1763 nir_eval_const_opcode(op
, num_components
, bit_size
, src
);
1770 case SpvOpConstantNull
:
1771 val
->constant
= vtn_null_constant(b
, val
->type
->type
);
1774 case SpvOpConstantSampler
:
1775 vtn_fail("OpConstantSampler requires Kernel Capability");
1779 vtn_fail("Unhandled opcode");
1782 /* Now that we have the value, update the workgroup size if needed */
1783 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1787 vtn_handle_function_call(struct vtn_builder
*b
, SpvOp opcode
,
1788 const uint32_t *w
, unsigned count
)
1790 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1791 struct vtn_function
*vtn_callee
=
1792 vtn_value(b
, w
[3], vtn_value_type_function
)->func
;
1793 struct nir_function
*callee
= vtn_callee
->impl
->function
;
1795 vtn_callee
->referenced
= true;
1797 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, callee
);
1798 for (unsigned i
= 0; i
< call
->num_params
; i
++) {
1799 unsigned arg_id
= w
[4 + i
];
1800 struct vtn_value
*arg
= vtn_untyped_value(b
, arg_id
);
1801 if (arg
->value_type
== vtn_value_type_pointer
&&
1802 arg
->pointer
->ptr_type
->type
== NULL
) {
1803 nir_deref_var
*d
= vtn_pointer_to_deref_var(b
, arg
->pointer
);
1804 call
->params
[i
] = nir_deref_var_clone(d
, call
);
1806 struct vtn_ssa_value
*arg_ssa
= vtn_ssa_value(b
, arg_id
);
1808 /* Make a temporary to store the argument in */
1810 nir_local_variable_create(b
->nb
.impl
, arg_ssa
->type
, "arg_tmp");
1811 call
->params
[i
] = nir_deref_var_create(call
, tmp
);
1813 vtn_local_store(b
, arg_ssa
,
1814 nir_build_deref_for_chain(&b
->nb
, call
->params
[i
]));
1818 nir_variable
*out_tmp
= NULL
;
1819 vtn_assert(res_type
->type
== callee
->return_type
);
1820 if (!glsl_type_is_void(callee
->return_type
)) {
1821 out_tmp
= nir_local_variable_create(b
->nb
.impl
, callee
->return_type
,
1823 call
->return_deref
= nir_deref_var_create(call
, out_tmp
);
1826 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
1828 if (glsl_type_is_void(callee
->return_type
)) {
1829 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1831 nir_deref_instr
*return_deref
=
1832 nir_build_deref_for_chain(&b
->nb
, call
->return_deref
);
1833 vtn_push_ssa(b
, w
[2], res_type
, vtn_local_load(b
, return_deref
));
1837 struct vtn_ssa_value
*
1838 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1840 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1843 if (!glsl_type_is_vector_or_scalar(type
)) {
1844 unsigned elems
= glsl_get_length(type
);
1845 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1846 for (unsigned i
= 0; i
< elems
; i
++) {
1847 const struct glsl_type
*child_type
;
1849 switch (glsl_get_base_type(type
)) {
1851 case GLSL_TYPE_UINT
:
1852 case GLSL_TYPE_INT16
:
1853 case GLSL_TYPE_UINT16
:
1854 case GLSL_TYPE_UINT8
:
1855 case GLSL_TYPE_INT8
:
1856 case GLSL_TYPE_INT64
:
1857 case GLSL_TYPE_UINT64
:
1858 case GLSL_TYPE_BOOL
:
1859 case GLSL_TYPE_FLOAT
:
1860 case GLSL_TYPE_FLOAT16
:
1861 case GLSL_TYPE_DOUBLE
:
1862 child_type
= glsl_get_column_type(type
);
1864 case GLSL_TYPE_ARRAY
:
1865 child_type
= glsl_get_array_element(type
);
1867 case GLSL_TYPE_STRUCT
:
1868 child_type
= glsl_get_struct_field(type
, i
);
1871 vtn_fail("unkown base type");
1874 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
1882 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
1885 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
1886 src
.src_type
= type
;
1891 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
1892 const uint32_t *w
, unsigned count
)
1894 if (opcode
== SpvOpSampledImage
) {
1895 struct vtn_value
*val
=
1896 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1897 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1898 val
->sampled_image
->type
=
1899 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1900 val
->sampled_image
->image
=
1901 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1902 val
->sampled_image
->sampler
=
1903 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
1905 } else if (opcode
== SpvOpImage
) {
1906 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1907 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
1908 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
1909 val
->pointer
= src_val
->sampled_image
->image
;
1911 vtn_assert(src_val
->value_type
== vtn_value_type_pointer
);
1912 val
->pointer
= src_val
->pointer
;
1917 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1918 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1920 struct vtn_sampled_image sampled
;
1921 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
1922 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
1923 sampled
= *sampled_val
->sampled_image
;
1925 vtn_assert(sampled_val
->value_type
== vtn_value_type_pointer
);
1926 sampled
.type
= sampled_val
->pointer
->type
;
1927 sampled
.image
= NULL
;
1928 sampled
.sampler
= sampled_val
->pointer
;
1931 const struct glsl_type
*image_type
= sampled
.type
->type
;
1932 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
1933 const bool is_array
= glsl_sampler_type_is_array(image_type
);
1935 /* Figure out the base texture operation */
1938 case SpvOpImageSampleImplicitLod
:
1939 case SpvOpImageSampleDrefImplicitLod
:
1940 case SpvOpImageSampleProjImplicitLod
:
1941 case SpvOpImageSampleProjDrefImplicitLod
:
1942 texop
= nir_texop_tex
;
1945 case SpvOpImageSampleExplicitLod
:
1946 case SpvOpImageSampleDrefExplicitLod
:
1947 case SpvOpImageSampleProjExplicitLod
:
1948 case SpvOpImageSampleProjDrefExplicitLod
:
1949 texop
= nir_texop_txl
;
1952 case SpvOpImageFetch
:
1953 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
1954 texop
= nir_texop_txf_ms
;
1956 texop
= nir_texop_txf
;
1960 case SpvOpImageGather
:
1961 case SpvOpImageDrefGather
:
1962 texop
= nir_texop_tg4
;
1965 case SpvOpImageQuerySizeLod
:
1966 case SpvOpImageQuerySize
:
1967 texop
= nir_texop_txs
;
1970 case SpvOpImageQueryLod
:
1971 texop
= nir_texop_lod
;
1974 case SpvOpImageQueryLevels
:
1975 texop
= nir_texop_query_levels
;
1978 case SpvOpImageQuerySamples
:
1979 texop
= nir_texop_texture_samples
;
1983 vtn_fail("Unhandled opcode");
1986 nir_tex_src srcs
[10]; /* 10 should be enough */
1987 nir_tex_src
*p
= srcs
;
1989 nir_deref_instr
*sampler
= vtn_pointer_to_deref(b
, sampled
.sampler
);
1990 nir_deref_instr
*texture
=
1991 sampled
.image
? vtn_pointer_to_deref(b
, sampled
.image
) : sampler
;
1993 p
->src
= nir_src_for_ssa(&texture
->dest
.ssa
);
1994 p
->src_type
= nir_tex_src_texture_deref
;
2003 /* These operations require a sampler */
2004 p
->src
= nir_src_for_ssa(&sampler
->dest
.ssa
);
2005 p
->src_type
= nir_tex_src_sampler_deref
;
2009 case nir_texop_txf_ms
:
2012 case nir_texop_query_levels
:
2013 case nir_texop_texture_samples
:
2014 case nir_texop_samples_identical
:
2017 case nir_texop_txf_ms_mcs
:
2018 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2023 struct nir_ssa_def
*coord
;
2024 unsigned coord_components
;
2026 case SpvOpImageSampleImplicitLod
:
2027 case SpvOpImageSampleExplicitLod
:
2028 case SpvOpImageSampleDrefImplicitLod
:
2029 case SpvOpImageSampleDrefExplicitLod
:
2030 case SpvOpImageSampleProjImplicitLod
:
2031 case SpvOpImageSampleProjExplicitLod
:
2032 case SpvOpImageSampleProjDrefImplicitLod
:
2033 case SpvOpImageSampleProjDrefExplicitLod
:
2034 case SpvOpImageFetch
:
2035 case SpvOpImageGather
:
2036 case SpvOpImageDrefGather
:
2037 case SpvOpImageQueryLod
: {
2038 /* All these types have the coordinate as their first real argument */
2039 switch (sampler_dim
) {
2040 case GLSL_SAMPLER_DIM_1D
:
2041 case GLSL_SAMPLER_DIM_BUF
:
2042 coord_components
= 1;
2044 case GLSL_SAMPLER_DIM_2D
:
2045 case GLSL_SAMPLER_DIM_RECT
:
2046 case GLSL_SAMPLER_DIM_MS
:
2047 coord_components
= 2;
2049 case GLSL_SAMPLER_DIM_3D
:
2050 case GLSL_SAMPLER_DIM_CUBE
:
2051 coord_components
= 3;
2054 vtn_fail("Invalid sampler type");
2057 if (is_array
&& texop
!= nir_texop_lod
)
2060 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
2061 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2062 (1 << coord_components
) - 1));
2063 p
->src_type
= nir_tex_src_coord
;
2070 coord_components
= 0;
2075 case SpvOpImageSampleProjImplicitLod
:
2076 case SpvOpImageSampleProjExplicitLod
:
2077 case SpvOpImageSampleProjDrefImplicitLod
:
2078 case SpvOpImageSampleProjDrefExplicitLod
:
2079 /* These have the projector as the last coordinate component */
2080 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2081 p
->src_type
= nir_tex_src_projector
;
2089 bool is_shadow
= false;
2090 unsigned gather_component
= 0;
2092 case SpvOpImageSampleDrefImplicitLod
:
2093 case SpvOpImageSampleDrefExplicitLod
:
2094 case SpvOpImageSampleProjDrefImplicitLod
:
2095 case SpvOpImageSampleProjDrefExplicitLod
:
2096 case SpvOpImageDrefGather
:
2097 /* These all have an explicit depth value as their next source */
2099 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2102 case SpvOpImageGather
:
2103 /* This has a component as its next source */
2105 vtn_value(b
, w
[idx
++], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
2112 /* For OpImageQuerySizeLod, we always have an LOD */
2113 if (opcode
== SpvOpImageQuerySizeLod
)
2114 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2116 /* Now we need to handle some number of optional arguments */
2117 const struct vtn_ssa_value
*gather_offsets
= NULL
;
2119 uint32_t operands
= w
[idx
++];
2121 if (operands
& SpvImageOperandsBiasMask
) {
2122 vtn_assert(texop
== nir_texop_tex
);
2123 texop
= nir_texop_txb
;
2124 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
2127 if (operands
& SpvImageOperandsLodMask
) {
2128 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2129 texop
== nir_texop_txs
);
2130 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2133 if (operands
& SpvImageOperandsGradMask
) {
2134 vtn_assert(texop
== nir_texop_txl
);
2135 texop
= nir_texop_txd
;
2136 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
2137 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
2140 if (operands
& SpvImageOperandsOffsetMask
||
2141 operands
& SpvImageOperandsConstOffsetMask
)
2142 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
2144 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2145 nir_tex_src none
= {0};
2146 gather_offsets
= vtn_ssa_value(b
, w
[idx
++]);
2150 if (operands
& SpvImageOperandsSampleMask
) {
2151 vtn_assert(texop
== nir_texop_txf_ms
);
2152 texop
= nir_texop_txf_ms
;
2153 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
2156 /* We should have now consumed exactly all of the arguments */
2157 vtn_assert(idx
== count
);
2159 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2162 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2164 instr
->coord_components
= coord_components
;
2165 instr
->sampler_dim
= sampler_dim
;
2166 instr
->is_array
= is_array
;
2167 instr
->is_shadow
= is_shadow
;
2168 instr
->is_new_style_shadow
=
2169 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2170 instr
->component
= gather_component
;
2172 switch (glsl_get_sampler_result_type(image_type
)) {
2173 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
2174 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
2175 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
2176 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
2178 vtn_fail("Invalid base type for sampler result");
2181 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2182 nir_tex_instr_dest_size(instr
), 32, NULL
);
2184 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2185 nir_tex_instr_dest_size(instr
));
2188 nir_instr
*instruction
;
2189 if (gather_offsets
) {
2190 vtn_assert(glsl_get_base_type(gather_offsets
->type
) == GLSL_TYPE_ARRAY
);
2191 vtn_assert(glsl_get_length(gather_offsets
->type
) == 4);
2192 nir_tex_instr
*instrs
[4] = {instr
, NULL
, NULL
, NULL
};
2194 /* Copy the current instruction 4x */
2195 for (uint32_t i
= 1; i
< 4; i
++) {
2196 instrs
[i
] = nir_tex_instr_create(b
->shader
, instr
->num_srcs
);
2197 instrs
[i
]->op
= instr
->op
;
2198 instrs
[i
]->coord_components
= instr
->coord_components
;
2199 instrs
[i
]->sampler_dim
= instr
->sampler_dim
;
2200 instrs
[i
]->is_array
= instr
->is_array
;
2201 instrs
[i
]->is_shadow
= instr
->is_shadow
;
2202 instrs
[i
]->is_new_style_shadow
= instr
->is_new_style_shadow
;
2203 instrs
[i
]->component
= instr
->component
;
2204 instrs
[i
]->dest_type
= instr
->dest_type
;
2206 memcpy(instrs
[i
]->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2208 nir_ssa_dest_init(&instrs
[i
]->instr
, &instrs
[i
]->dest
,
2209 nir_tex_instr_dest_size(instr
), 32, NULL
);
2212 /* Fill in the last argument with the offset from the passed in offsets
2213 * and insert the instruction into the stream.
2215 for (uint32_t i
= 0; i
< 4; i
++) {
2217 src
.src
= nir_src_for_ssa(gather_offsets
->elems
[i
]->def
);
2218 src
.src_type
= nir_tex_src_offset
;
2219 instrs
[i
]->src
[instrs
[i
]->num_srcs
- 1] = src
;
2220 nir_builder_instr_insert(&b
->nb
, &instrs
[i
]->instr
);
2223 /* Combine the results of the 4 instructions by taking their .w
2226 nir_alu_instr
*vec4
= nir_alu_instr_create(b
->shader
, nir_op_vec4
);
2227 nir_ssa_dest_init(&vec4
->instr
, &vec4
->dest
.dest
, 4, 32, NULL
);
2228 vec4
->dest
.write_mask
= 0xf;
2229 for (uint32_t i
= 0; i
< 4; i
++) {
2230 vec4
->src
[i
].src
= nir_src_for_ssa(&instrs
[i
]->dest
.ssa
);
2231 vec4
->src
[i
].swizzle
[0] = 3;
2233 def
= &vec4
->dest
.dest
.ssa
;
2234 instruction
= &vec4
->instr
;
2236 def
= &instr
->dest
.ssa
;
2237 instruction
= &instr
->instr
;
2240 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
2241 val
->ssa
->def
= def
;
2243 nir_builder_instr_insert(&b
->nb
, instruction
);
2247 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2248 const uint32_t *w
, nir_src
*src
)
2251 case SpvOpAtomicIIncrement
:
2252 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
2255 case SpvOpAtomicIDecrement
:
2256 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
2259 case SpvOpAtomicISub
:
2261 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
2264 case SpvOpAtomicCompareExchange
:
2265 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
2266 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
2269 case SpvOpAtomicExchange
:
2270 case SpvOpAtomicIAdd
:
2271 case SpvOpAtomicSMin
:
2272 case SpvOpAtomicUMin
:
2273 case SpvOpAtomicSMax
:
2274 case SpvOpAtomicUMax
:
2275 case SpvOpAtomicAnd
:
2277 case SpvOpAtomicXor
:
2278 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
2282 vtn_fail("Invalid SPIR-V atomic");
2286 static nir_ssa_def
*
2287 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2289 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
2291 /* The image_load_store intrinsics assume a 4-dim coordinate */
2292 unsigned dim
= glsl_get_vector_elements(coord
->type
);
2293 unsigned swizzle
[4];
2294 for (unsigned i
= 0; i
< 4; i
++)
2295 swizzle
[i
] = MIN2(i
, dim
- 1);
2297 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
2301 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2302 const uint32_t *w
, unsigned count
)
2304 /* Just get this one out of the way */
2305 if (opcode
== SpvOpImageTexelPointer
) {
2306 struct vtn_value
*val
=
2307 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2308 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2310 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2311 val
->image
->coord
= get_image_coord(b
, w
[4]);
2312 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
2316 struct vtn_image_pointer image
;
2319 case SpvOpAtomicExchange
:
2320 case SpvOpAtomicCompareExchange
:
2321 case SpvOpAtomicCompareExchangeWeak
:
2322 case SpvOpAtomicIIncrement
:
2323 case SpvOpAtomicIDecrement
:
2324 case SpvOpAtomicIAdd
:
2325 case SpvOpAtomicISub
:
2326 case SpvOpAtomicLoad
:
2327 case SpvOpAtomicSMin
:
2328 case SpvOpAtomicUMin
:
2329 case SpvOpAtomicSMax
:
2330 case SpvOpAtomicUMax
:
2331 case SpvOpAtomicAnd
:
2333 case SpvOpAtomicXor
:
2334 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
2337 case SpvOpAtomicStore
:
2338 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
2341 case SpvOpImageQuerySize
:
2342 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2344 image
.sample
= NULL
;
2347 case SpvOpImageRead
:
2348 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2349 image
.coord
= get_image_coord(b
, w
[4]);
2351 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
2352 vtn_assert(w
[5] == SpvImageOperandsSampleMask
);
2353 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
2355 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2359 case SpvOpImageWrite
:
2360 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2361 image
.coord
= get_image_coord(b
, w
[2]);
2365 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
2366 vtn_assert(w
[4] == SpvImageOperandsSampleMask
);
2367 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
2369 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2374 vtn_fail("Invalid image opcode");
2377 nir_intrinsic_op op
;
2379 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
2380 OP(ImageQuerySize
, size
)
2382 OP(ImageWrite
, store
)
2383 OP(AtomicLoad
, load
)
2384 OP(AtomicStore
, store
)
2385 OP(AtomicExchange
, atomic_exchange
)
2386 OP(AtomicCompareExchange
, atomic_comp_swap
)
2387 OP(AtomicIIncrement
, atomic_add
)
2388 OP(AtomicIDecrement
, atomic_add
)
2389 OP(AtomicIAdd
, atomic_add
)
2390 OP(AtomicISub
, atomic_add
)
2391 OP(AtomicSMin
, atomic_min
)
2392 OP(AtomicUMin
, atomic_min
)
2393 OP(AtomicSMax
, atomic_max
)
2394 OP(AtomicUMax
, atomic_max
)
2395 OP(AtomicAnd
, atomic_and
)
2396 OP(AtomicOr
, atomic_or
)
2397 OP(AtomicXor
, atomic_xor
)
2400 vtn_fail("Invalid image opcode");
2403 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
2405 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
2406 intrin
->src
[0] = nir_src_for_ssa(&image_deref
->dest
.ssa
);
2408 /* ImageQuerySize doesn't take any extra parameters */
2409 if (opcode
!= SpvOpImageQuerySize
) {
2410 /* The image coordinate is always 4 components but we may not have that
2411 * many. Swizzle to compensate.
2414 for (unsigned i
= 0; i
< 4; i
++)
2415 swiz
[i
] = i
< image
.coord
->num_components
? i
: 0;
2416 intrin
->src
[1] = nir_src_for_ssa(nir_swizzle(&b
->nb
, image
.coord
,
2418 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
2422 case SpvOpAtomicLoad
:
2423 case SpvOpImageQuerySize
:
2424 case SpvOpImageRead
:
2426 case SpvOpAtomicStore
:
2427 intrin
->src
[3] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2429 case SpvOpImageWrite
:
2430 intrin
->src
[3] = nir_src_for_ssa(vtn_ssa_value(b
, w
[3])->def
);
2433 case SpvOpAtomicCompareExchange
:
2434 case SpvOpAtomicIIncrement
:
2435 case SpvOpAtomicIDecrement
:
2436 case SpvOpAtomicExchange
:
2437 case SpvOpAtomicIAdd
:
2438 case SpvOpAtomicISub
:
2439 case SpvOpAtomicSMin
:
2440 case SpvOpAtomicUMin
:
2441 case SpvOpAtomicSMax
:
2442 case SpvOpAtomicUMax
:
2443 case SpvOpAtomicAnd
:
2445 case SpvOpAtomicXor
:
2446 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
2450 vtn_fail("Invalid image opcode");
2453 if (opcode
!= SpvOpImageWrite
) {
2454 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2455 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2457 unsigned dest_components
= nir_intrinsic_dest_components(intrin
);
2458 if (intrin
->intrinsic
== nir_intrinsic_image_deref_size
) {
2459 dest_components
= intrin
->num_components
=
2460 glsl_get_vector_elements(type
->type
);
2463 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2464 dest_components
, 32, NULL
);
2466 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2468 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
2469 val
->ssa
->def
= &intrin
->dest
.ssa
;
2471 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2475 static nir_intrinsic_op
2476 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2479 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2480 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2481 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2482 OP(AtomicExchange
, atomic_exchange
)
2483 OP(AtomicCompareExchange
, atomic_comp_swap
)
2484 OP(AtomicIIncrement
, atomic_add
)
2485 OP(AtomicIDecrement
, atomic_add
)
2486 OP(AtomicIAdd
, atomic_add
)
2487 OP(AtomicISub
, atomic_add
)
2488 OP(AtomicSMin
, atomic_imin
)
2489 OP(AtomicUMin
, atomic_umin
)
2490 OP(AtomicSMax
, atomic_imax
)
2491 OP(AtomicUMax
, atomic_umax
)
2492 OP(AtomicAnd
, atomic_and
)
2493 OP(AtomicOr
, atomic_or
)
2494 OP(AtomicXor
, atomic_xor
)
2497 vtn_fail("Invalid SSBO atomic");
2501 static nir_intrinsic_op
2502 get_shared_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2505 case SpvOpAtomicLoad
: return nir_intrinsic_load_shared
;
2506 case SpvOpAtomicStore
: return nir_intrinsic_store_shared
;
2507 #define OP(S, N) case SpvOp##S: return nir_intrinsic_shared_##N;
2508 OP(AtomicExchange
, atomic_exchange
)
2509 OP(AtomicCompareExchange
, atomic_comp_swap
)
2510 OP(AtomicIIncrement
, atomic_add
)
2511 OP(AtomicIDecrement
, atomic_add
)
2512 OP(AtomicIAdd
, atomic_add
)
2513 OP(AtomicISub
, atomic_add
)
2514 OP(AtomicSMin
, atomic_imin
)
2515 OP(AtomicUMin
, atomic_umin
)
2516 OP(AtomicSMax
, atomic_imax
)
2517 OP(AtomicUMax
, atomic_umax
)
2518 OP(AtomicAnd
, atomic_and
)
2519 OP(AtomicOr
, atomic_or
)
2520 OP(AtomicXor
, atomic_xor
)
2523 vtn_fail("Invalid shared atomic");
2527 static nir_intrinsic_op
2528 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2531 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
2532 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
2533 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
2534 OP(AtomicExchange
, atomic_exchange
)
2535 OP(AtomicCompareExchange
, atomic_comp_swap
)
2536 OP(AtomicIIncrement
, atomic_add
)
2537 OP(AtomicIDecrement
, atomic_add
)
2538 OP(AtomicIAdd
, atomic_add
)
2539 OP(AtomicISub
, atomic_add
)
2540 OP(AtomicSMin
, atomic_imin
)
2541 OP(AtomicUMin
, atomic_umin
)
2542 OP(AtomicSMax
, atomic_imax
)
2543 OP(AtomicUMax
, atomic_umax
)
2544 OP(AtomicAnd
, atomic_and
)
2545 OP(AtomicOr
, atomic_or
)
2546 OP(AtomicXor
, atomic_xor
)
2549 vtn_fail("Invalid shared atomic");
2554 vtn_handle_ssbo_or_shared_atomic(struct vtn_builder
*b
, SpvOp opcode
,
2555 const uint32_t *w
, unsigned count
)
2557 struct vtn_pointer
*ptr
;
2558 nir_intrinsic_instr
*atomic
;
2561 case SpvOpAtomicLoad
:
2562 case SpvOpAtomicExchange
:
2563 case SpvOpAtomicCompareExchange
:
2564 case SpvOpAtomicCompareExchangeWeak
:
2565 case SpvOpAtomicIIncrement
:
2566 case SpvOpAtomicIDecrement
:
2567 case SpvOpAtomicIAdd
:
2568 case SpvOpAtomicISub
:
2569 case SpvOpAtomicSMin
:
2570 case SpvOpAtomicUMin
:
2571 case SpvOpAtomicSMax
:
2572 case SpvOpAtomicUMax
:
2573 case SpvOpAtomicAnd
:
2575 case SpvOpAtomicXor
:
2576 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2579 case SpvOpAtomicStore
:
2580 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2584 vtn_fail("Invalid SPIR-V atomic");
2588 SpvScope scope = w[4];
2589 SpvMemorySemanticsMask semantics = w[5];
2592 if (ptr
->mode
== vtn_variable_mode_workgroup
&&
2593 !b
->options
->lower_workgroup_access_to_offsets
) {
2594 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2595 const struct glsl_type
*deref_type
= deref
->type
;
2596 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
2597 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2598 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2601 case SpvOpAtomicLoad
:
2602 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2605 case SpvOpAtomicStore
:
2606 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2607 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2608 atomic
->src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2611 case SpvOpAtomicExchange
:
2612 case SpvOpAtomicCompareExchange
:
2613 case SpvOpAtomicCompareExchangeWeak
:
2614 case SpvOpAtomicIIncrement
:
2615 case SpvOpAtomicIDecrement
:
2616 case SpvOpAtomicIAdd
:
2617 case SpvOpAtomicISub
:
2618 case SpvOpAtomicSMin
:
2619 case SpvOpAtomicUMin
:
2620 case SpvOpAtomicSMax
:
2621 case SpvOpAtomicUMax
:
2622 case SpvOpAtomicAnd
:
2624 case SpvOpAtomicXor
:
2625 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
2629 vtn_fail("Invalid SPIR-V atomic");
2633 nir_ssa_def
*offset
, *index
;
2634 offset
= vtn_pointer_to_offset(b
, ptr
, &index
, NULL
);
2636 nir_intrinsic_op op
;
2637 if (ptr
->mode
== vtn_variable_mode_ssbo
) {
2638 op
= get_ssbo_nir_atomic_op(b
, opcode
);
2640 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
&&
2641 b
->options
->lower_workgroup_access_to_offsets
);
2642 op
= get_shared_nir_atomic_op(b
, opcode
);
2645 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2649 case SpvOpAtomicLoad
:
2650 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2651 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2652 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2653 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2656 case SpvOpAtomicStore
:
2657 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2658 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2659 atomic
->src
[src
++] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2660 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2661 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2662 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2665 case SpvOpAtomicExchange
:
2666 case SpvOpAtomicCompareExchange
:
2667 case SpvOpAtomicCompareExchangeWeak
:
2668 case SpvOpAtomicIIncrement
:
2669 case SpvOpAtomicIDecrement
:
2670 case SpvOpAtomicIAdd
:
2671 case SpvOpAtomicISub
:
2672 case SpvOpAtomicSMin
:
2673 case SpvOpAtomicUMin
:
2674 case SpvOpAtomicSMax
:
2675 case SpvOpAtomicUMax
:
2676 case SpvOpAtomicAnd
:
2678 case SpvOpAtomicXor
:
2679 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2680 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2681 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2682 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
2686 vtn_fail("Invalid SPIR-V atomic");
2690 if (opcode
!= SpvOpAtomicStore
) {
2691 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2693 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
2694 glsl_get_vector_elements(type
->type
),
2695 glsl_get_bit_size(type
->type
), NULL
);
2697 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2698 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
2699 val
->ssa
->def
= &atomic
->dest
.ssa
;
2700 val
->ssa
->type
= type
->type
;
2703 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
2706 static nir_alu_instr
*
2707 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
2710 switch (num_components
) {
2711 case 1: op
= nir_op_fmov
; break;
2712 case 2: op
= nir_op_vec2
; break;
2713 case 3: op
= nir_op_vec3
; break;
2714 case 4: op
= nir_op_vec4
; break;
2715 default: vtn_fail("bad vector size");
2718 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
2719 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
2721 vec
->dest
.write_mask
= (1 << num_components
) - 1;
2726 struct vtn_ssa_value
*
2727 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
2729 if (src
->transposed
)
2730 return src
->transposed
;
2732 struct vtn_ssa_value
*dest
=
2733 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
2735 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
2736 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
2737 glsl_get_bit_size(src
->type
));
2738 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2739 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
2740 vec
->src
[0].swizzle
[0] = i
;
2742 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
2743 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
2744 vec
->src
[j
].swizzle
[0] = i
;
2747 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2748 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
2751 dest
->transposed
= src
;
2757 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
2759 unsigned swiz
[4] = { index
};
2760 return nir_swizzle(&b
->nb
, src
, swiz
, 1, true);
2764 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
2767 nir_alu_instr
*vec
= create_vec(b
, src
->num_components
,
2770 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
2772 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
2774 vec
->src
[i
].src
= nir_src_for_ssa(src
);
2775 vec
->src
[i
].swizzle
[0] = i
;
2779 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2781 return &vec
->dest
.dest
.ssa
;
2785 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2788 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
2789 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2790 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2791 vtn_vector_extract(b
, src
, i
), dest
);
2797 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2798 nir_ssa_def
*insert
, nir_ssa_def
*index
)
2800 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
2801 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2802 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2803 vtn_vector_insert(b
, src
, insert
, i
), dest
);
2808 static nir_ssa_def
*
2809 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
2810 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
2811 const uint32_t *indices
)
2813 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
2815 for (unsigned i
= 0; i
< num_components
; i
++) {
2816 uint32_t index
= indices
[i
];
2817 if (index
== 0xffffffff) {
2819 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
2820 } else if (index
< src0
->num_components
) {
2821 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
2822 vec
->src
[i
].swizzle
[0] = index
;
2824 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
2825 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
2829 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2831 return &vec
->dest
.dest
.ssa
;
2835 * Concatentates a number of vectors/scalars together to produce a vector
2837 static nir_ssa_def
*
2838 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
2839 unsigned num_srcs
, nir_ssa_def
**srcs
)
2841 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
2843 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2845 * "When constructing a vector, there must be at least two Constituent
2848 vtn_assert(num_srcs
>= 2);
2850 unsigned dest_idx
= 0;
2851 for (unsigned i
= 0; i
< num_srcs
; i
++) {
2852 nir_ssa_def
*src
= srcs
[i
];
2853 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
2854 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
2855 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
2856 vec
->src
[dest_idx
].swizzle
[0] = j
;
2861 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2863 * "When constructing a vector, the total number of components in all
2864 * the operands must equal the number of components in Result Type."
2866 vtn_assert(dest_idx
== num_components
);
2868 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2870 return &vec
->dest
.dest
.ssa
;
2873 static struct vtn_ssa_value
*
2874 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
2876 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
2877 dest
->type
= src
->type
;
2879 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2880 dest
->def
= src
->def
;
2882 unsigned elems
= glsl_get_length(src
->type
);
2884 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
2885 for (unsigned i
= 0; i
< elems
; i
++)
2886 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
2892 static struct vtn_ssa_value
*
2893 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2894 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
2895 unsigned num_indices
)
2897 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
2899 struct vtn_ssa_value
*cur
= dest
;
2901 for (i
= 0; i
< num_indices
- 1; i
++) {
2902 cur
= cur
->elems
[indices
[i
]];
2905 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2906 /* According to the SPIR-V spec, OpCompositeInsert may work down to
2907 * the component granularity. In that case, the last index will be
2908 * the index to insert the scalar into the vector.
2911 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
2913 cur
->elems
[indices
[i
]] = insert
;
2919 static struct vtn_ssa_value
*
2920 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2921 const uint32_t *indices
, unsigned num_indices
)
2923 struct vtn_ssa_value
*cur
= src
;
2924 for (unsigned i
= 0; i
< num_indices
; i
++) {
2925 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2926 vtn_assert(i
== num_indices
- 1);
2927 /* According to the SPIR-V spec, OpCompositeExtract may work down to
2928 * the component granularity. The last index will be the index of the
2929 * vector to extract.
2932 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
2933 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
2934 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
2937 cur
= cur
->elems
[indices
[i
]];
2945 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
2946 const uint32_t *w
, unsigned count
)
2948 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2949 const struct glsl_type
*type
=
2950 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
2951 val
->ssa
= vtn_create_ssa_value(b
, type
);
2954 case SpvOpVectorExtractDynamic
:
2955 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2956 vtn_ssa_value(b
, w
[4])->def
);
2959 case SpvOpVectorInsertDynamic
:
2960 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2961 vtn_ssa_value(b
, w
[4])->def
,
2962 vtn_ssa_value(b
, w
[5])->def
);
2965 case SpvOpVectorShuffle
:
2966 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
2967 vtn_ssa_value(b
, w
[3])->def
,
2968 vtn_ssa_value(b
, w
[4])->def
,
2972 case SpvOpCompositeConstruct
: {
2973 unsigned elems
= count
- 3;
2975 if (glsl_type_is_vector_or_scalar(type
)) {
2976 nir_ssa_def
*srcs
[4];
2977 for (unsigned i
= 0; i
< elems
; i
++)
2978 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
2980 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
2983 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2984 for (unsigned i
= 0; i
< elems
; i
++)
2985 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
2989 case SpvOpCompositeExtract
:
2990 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
2994 case SpvOpCompositeInsert
:
2995 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
2996 vtn_ssa_value(b
, w
[3]),
3000 case SpvOpCopyObject
:
3001 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3005 vtn_fail("unknown composite operation");
3010 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3012 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3013 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3017 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3018 SpvMemorySemanticsMask semantics
)
3020 static const SpvMemorySemanticsMask all_memory_semantics
=
3021 SpvMemorySemanticsUniformMemoryMask
|
3022 SpvMemorySemanticsWorkgroupMemoryMask
|
3023 SpvMemorySemanticsAtomicCounterMemoryMask
|
3024 SpvMemorySemanticsImageMemoryMask
;
3026 /* If we're not actually doing a memory barrier, bail */
3027 if (!(semantics
& all_memory_semantics
))
3030 /* GL and Vulkan don't have these */
3031 vtn_assert(scope
!= SpvScopeCrossDevice
);
3033 if (scope
== SpvScopeSubgroup
)
3034 return; /* Nothing to do here */
3036 if (scope
== SpvScopeWorkgroup
) {
3037 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3041 /* There's only two scopes thing left */
3042 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3044 if ((semantics
& all_memory_semantics
) == all_memory_semantics
) {
3045 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3049 /* Issue a bunch of more specific barriers */
3050 uint32_t bits
= semantics
;
3052 SpvMemorySemanticsMask semantic
= 1 << u_bit_scan(&bits
);
3054 case SpvMemorySemanticsUniformMemoryMask
:
3055 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3057 case SpvMemorySemanticsWorkgroupMemoryMask
:
3058 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3060 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3061 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3063 case SpvMemorySemanticsImageMemoryMask
:
3064 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3073 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3074 const uint32_t *w
, unsigned count
)
3077 case SpvOpEmitVertex
:
3078 case SpvOpEmitStreamVertex
:
3079 case SpvOpEndPrimitive
:
3080 case SpvOpEndStreamPrimitive
: {
3081 nir_intrinsic_op intrinsic_op
;
3083 case SpvOpEmitVertex
:
3084 case SpvOpEmitStreamVertex
:
3085 intrinsic_op
= nir_intrinsic_emit_vertex
;
3087 case SpvOpEndPrimitive
:
3088 case SpvOpEndStreamPrimitive
:
3089 intrinsic_op
= nir_intrinsic_end_primitive
;
3092 unreachable("Invalid opcode");
3095 nir_intrinsic_instr
*intrin
=
3096 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3099 case SpvOpEmitStreamVertex
:
3100 case SpvOpEndStreamPrimitive
:
3101 nir_intrinsic_set_stream_id(intrin
, w
[1]);
3107 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3111 case SpvOpMemoryBarrier
: {
3112 SpvScope scope
= vtn_constant_value(b
, w
[1])->values
[0].u32
[0];
3113 SpvMemorySemanticsMask semantics
=
3114 vtn_constant_value(b
, w
[2])->values
[0].u32
[0];
3115 vtn_emit_memory_barrier(b
, scope
, semantics
);
3119 case SpvOpControlBarrier
: {
3120 SpvScope execution_scope
=
3121 vtn_constant_value(b
, w
[1])->values
[0].u32
[0];
3122 if (execution_scope
== SpvScopeWorkgroup
)
3123 vtn_emit_barrier(b
, nir_intrinsic_barrier
);
3125 SpvScope memory_scope
=
3126 vtn_constant_value(b
, w
[2])->values
[0].u32
[0];
3127 SpvMemorySemanticsMask memory_semantics
=
3128 vtn_constant_value(b
, w
[3])->values
[0].u32
[0];
3129 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
3134 unreachable("unknown barrier instruction");
3139 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
3140 SpvExecutionMode mode
)
3143 case SpvExecutionModeInputPoints
:
3144 case SpvExecutionModeOutputPoints
:
3145 return 0; /* GL_POINTS */
3146 case SpvExecutionModeInputLines
:
3147 return 1; /* GL_LINES */
3148 case SpvExecutionModeInputLinesAdjacency
:
3149 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3150 case SpvExecutionModeTriangles
:
3151 return 4; /* GL_TRIANGLES */
3152 case SpvExecutionModeInputTrianglesAdjacency
:
3153 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3154 case SpvExecutionModeQuads
:
3155 return 7; /* GL_QUADS */
3156 case SpvExecutionModeIsolines
:
3157 return 0x8E7A; /* GL_ISOLINES */
3158 case SpvExecutionModeOutputLineStrip
:
3159 return 3; /* GL_LINE_STRIP */
3160 case SpvExecutionModeOutputTriangleStrip
:
3161 return 5; /* GL_TRIANGLE_STRIP */
3163 vtn_fail("Invalid primitive type");
3168 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
3169 SpvExecutionMode mode
)
3172 case SpvExecutionModeInputPoints
:
3174 case SpvExecutionModeInputLines
:
3176 case SpvExecutionModeInputLinesAdjacency
:
3178 case SpvExecutionModeTriangles
:
3180 case SpvExecutionModeInputTrianglesAdjacency
:
3183 vtn_fail("Invalid GS input mode");
3187 static gl_shader_stage
3188 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
3191 case SpvExecutionModelVertex
:
3192 return MESA_SHADER_VERTEX
;
3193 case SpvExecutionModelTessellationControl
:
3194 return MESA_SHADER_TESS_CTRL
;
3195 case SpvExecutionModelTessellationEvaluation
:
3196 return MESA_SHADER_TESS_EVAL
;
3197 case SpvExecutionModelGeometry
:
3198 return MESA_SHADER_GEOMETRY
;
3199 case SpvExecutionModelFragment
:
3200 return MESA_SHADER_FRAGMENT
;
3201 case SpvExecutionModelGLCompute
:
3202 return MESA_SHADER_COMPUTE
;
3204 vtn_fail("Unsupported execution model");
3208 #define spv_check_supported(name, cap) do { \
3209 if (!(b->options && b->options->caps.name)) \
3210 vtn_warn("Unsupported SPIR-V capability: %s", \
3211 spirv_capability_to_string(cap)); \
3216 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
3219 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
3220 /* Let this be a name label regardless */
3221 unsigned name_words
;
3222 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
3224 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
3225 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
3228 vtn_assert(b
->entry_point
== NULL
);
3229 b
->entry_point
= entry_point
;
3233 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3234 const uint32_t *w
, unsigned count
)
3241 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
3242 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
3243 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
3244 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
3245 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
3246 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
3249 uint32_t version
= w
[2];
3252 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
3254 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
3258 case SpvOpSourceExtension
:
3259 case SpvOpSourceContinued
:
3260 case SpvOpExtension
:
3261 case SpvOpModuleProcessed
:
3262 /* Unhandled, but these are for debug so that's ok. */
3265 case SpvOpCapability
: {
3266 SpvCapability cap
= w
[1];
3268 case SpvCapabilityMatrix
:
3269 case SpvCapabilityShader
:
3270 case SpvCapabilityGeometry
:
3271 case SpvCapabilityGeometryPointSize
:
3272 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
3273 case SpvCapabilitySampledImageArrayDynamicIndexing
:
3274 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
3275 case SpvCapabilityStorageImageArrayDynamicIndexing
:
3276 case SpvCapabilityImageRect
:
3277 case SpvCapabilitySampledRect
:
3278 case SpvCapabilitySampled1D
:
3279 case SpvCapabilityImage1D
:
3280 case SpvCapabilitySampledCubeArray
:
3281 case SpvCapabilityImageCubeArray
:
3282 case SpvCapabilitySampledBuffer
:
3283 case SpvCapabilityImageBuffer
:
3284 case SpvCapabilityImageQuery
:
3285 case SpvCapabilityDerivativeControl
:
3286 case SpvCapabilityInterpolationFunction
:
3287 case SpvCapabilityMultiViewport
:
3288 case SpvCapabilitySampleRateShading
:
3289 case SpvCapabilityClipDistance
:
3290 case SpvCapabilityCullDistance
:
3291 case SpvCapabilityInputAttachment
:
3292 case SpvCapabilityImageGatherExtended
:
3293 case SpvCapabilityStorageImageExtendedFormats
:
3296 case SpvCapabilityGeometryStreams
:
3297 case SpvCapabilityLinkage
:
3298 case SpvCapabilityVector16
:
3299 case SpvCapabilityFloat16Buffer
:
3300 case SpvCapabilityFloat16
:
3301 case SpvCapabilityInt64Atomics
:
3302 case SpvCapabilityAtomicStorage
:
3303 case SpvCapabilityStorageImageMultisample
:
3304 case SpvCapabilityInt8
:
3305 case SpvCapabilitySparseResidency
:
3306 case SpvCapabilityMinLod
:
3307 case SpvCapabilityTransformFeedback
:
3308 vtn_warn("Unsupported SPIR-V capability: %s",
3309 spirv_capability_to_string(cap
));
3312 case SpvCapabilityFloat64
:
3313 spv_check_supported(float64
, cap
);
3315 case SpvCapabilityInt64
:
3316 spv_check_supported(int64
, cap
);
3318 case SpvCapabilityInt16
:
3319 spv_check_supported(int16
, cap
);
3322 case SpvCapabilityAddresses
:
3323 case SpvCapabilityKernel
:
3324 case SpvCapabilityImageBasic
:
3325 case SpvCapabilityImageReadWrite
:
3326 case SpvCapabilityImageMipmap
:
3327 case SpvCapabilityPipes
:
3328 case SpvCapabilityGroups
:
3329 case SpvCapabilityDeviceEnqueue
:
3330 case SpvCapabilityLiteralSampler
:
3331 case SpvCapabilityGenericPointer
:
3332 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
3333 spirv_capability_to_string(cap
));
3336 case SpvCapabilityImageMSArray
:
3337 spv_check_supported(image_ms_array
, cap
);
3340 case SpvCapabilityTessellation
:
3341 case SpvCapabilityTessellationPointSize
:
3342 spv_check_supported(tessellation
, cap
);
3345 case SpvCapabilityDrawParameters
:
3346 spv_check_supported(draw_parameters
, cap
);
3349 case SpvCapabilityStorageImageReadWithoutFormat
:
3350 spv_check_supported(image_read_without_format
, cap
);
3353 case SpvCapabilityStorageImageWriteWithoutFormat
:
3354 spv_check_supported(image_write_without_format
, cap
);
3357 case SpvCapabilityDeviceGroup
:
3358 spv_check_supported(device_group
, cap
);
3361 case SpvCapabilityMultiView
:
3362 spv_check_supported(multiview
, cap
);
3365 case SpvCapabilityGroupNonUniform
:
3366 spv_check_supported(subgroup_basic
, cap
);
3369 case SpvCapabilityGroupNonUniformVote
:
3370 spv_check_supported(subgroup_vote
, cap
);
3373 case SpvCapabilitySubgroupBallotKHR
:
3374 case SpvCapabilityGroupNonUniformBallot
:
3375 spv_check_supported(subgroup_ballot
, cap
);
3378 case SpvCapabilityGroupNonUniformShuffle
:
3379 case SpvCapabilityGroupNonUniformShuffleRelative
:
3380 spv_check_supported(subgroup_shuffle
, cap
);
3383 case SpvCapabilityGroupNonUniformQuad
:
3384 spv_check_supported(subgroup_quad
, cap
);
3387 case SpvCapabilityGroupNonUniformArithmetic
:
3388 case SpvCapabilityGroupNonUniformClustered
:
3389 spv_check_supported(subgroup_arithmetic
, cap
);
3392 case SpvCapabilityVariablePointersStorageBuffer
:
3393 case SpvCapabilityVariablePointers
:
3394 spv_check_supported(variable_pointers
, cap
);
3397 case SpvCapabilityStorageUniformBufferBlock16
:
3398 case SpvCapabilityStorageUniform16
:
3399 case SpvCapabilityStoragePushConstant16
:
3400 case SpvCapabilityStorageInputOutput16
:
3401 spv_check_supported(storage_16bit
, cap
);
3404 case SpvCapabilityShaderViewportIndexLayerEXT
:
3405 spv_check_supported(shader_viewport_index_layer
, cap
);
3408 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
3409 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
3410 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
3411 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
3414 case SpvCapabilityRuntimeDescriptorArrayEXT
:
3415 spv_check_supported(runtime_descriptor_array
, cap
);
3418 case SpvCapabilityStencilExportEXT
:
3419 spv_check_supported(stencil_export
, cap
);
3423 vtn_fail("Unhandled capability");
3428 case SpvOpExtInstImport
:
3429 vtn_handle_extension(b
, opcode
, w
, count
);
3432 case SpvOpMemoryModel
:
3433 vtn_assert(w
[1] == SpvAddressingModelLogical
);
3434 vtn_assert(w
[2] == SpvMemoryModelSimple
||
3435 w
[2] == SpvMemoryModelGLSL450
);
3438 case SpvOpEntryPoint
:
3439 vtn_handle_entry_point(b
, w
, count
);
3443 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
3444 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3448 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3451 case SpvOpMemberName
:
3455 case SpvOpExecutionMode
:
3456 case SpvOpDecorationGroup
:
3458 case SpvOpMemberDecorate
:
3459 case SpvOpGroupDecorate
:
3460 case SpvOpGroupMemberDecorate
:
3461 vtn_handle_decoration(b
, opcode
, w
, count
);
3465 return false; /* End of preamble */
3472 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
3473 const struct vtn_decoration
*mode
, void *data
)
3475 vtn_assert(b
->entry_point
== entry_point
);
3477 switch(mode
->exec_mode
) {
3478 case SpvExecutionModeOriginUpperLeft
:
3479 case SpvExecutionModeOriginLowerLeft
:
3480 b
->origin_upper_left
=
3481 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
3484 case SpvExecutionModeEarlyFragmentTests
:
3485 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3486 b
->shader
->info
.fs
.early_fragment_tests
= true;
3489 case SpvExecutionModeInvocations
:
3490 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3491 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->literals
[0]);
3494 case SpvExecutionModeDepthReplacing
:
3495 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3496 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
3498 case SpvExecutionModeDepthGreater
:
3499 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3500 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
3502 case SpvExecutionModeDepthLess
:
3503 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3504 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
3506 case SpvExecutionModeDepthUnchanged
:
3507 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3508 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
3511 case SpvExecutionModeLocalSize
:
3512 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
3513 b
->shader
->info
.cs
.local_size
[0] = mode
->literals
[0];
3514 b
->shader
->info
.cs
.local_size
[1] = mode
->literals
[1];
3515 b
->shader
->info
.cs
.local_size
[2] = mode
->literals
[2];
3517 case SpvExecutionModeLocalSizeHint
:
3518 break; /* Nothing to do with this */
3520 case SpvExecutionModeOutputVertices
:
3521 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3522 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3523 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->literals
[0];
3525 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3526 b
->shader
->info
.gs
.vertices_out
= mode
->literals
[0];
3530 case SpvExecutionModeInputPoints
:
3531 case SpvExecutionModeInputLines
:
3532 case SpvExecutionModeInputLinesAdjacency
:
3533 case SpvExecutionModeTriangles
:
3534 case SpvExecutionModeInputTrianglesAdjacency
:
3535 case SpvExecutionModeQuads
:
3536 case SpvExecutionModeIsolines
:
3537 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3538 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3539 b
->shader
->info
.tess
.primitive_mode
=
3540 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3542 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3543 b
->shader
->info
.gs
.vertices_in
=
3544 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
3548 case SpvExecutionModeOutputPoints
:
3549 case SpvExecutionModeOutputLineStrip
:
3550 case SpvExecutionModeOutputTriangleStrip
:
3551 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3552 b
->shader
->info
.gs
.output_primitive
=
3553 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3556 case SpvExecutionModeSpacingEqual
:
3557 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3558 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3559 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
3561 case SpvExecutionModeSpacingFractionalEven
:
3562 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3563 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3564 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
3566 case SpvExecutionModeSpacingFractionalOdd
:
3567 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3568 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3569 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
3571 case SpvExecutionModeVertexOrderCw
:
3572 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3573 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3574 b
->shader
->info
.tess
.ccw
= false;
3576 case SpvExecutionModeVertexOrderCcw
:
3577 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3578 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3579 b
->shader
->info
.tess
.ccw
= true;
3581 case SpvExecutionModePointMode
:
3582 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3583 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3584 b
->shader
->info
.tess
.point_mode
= true;
3587 case SpvExecutionModePixelCenterInteger
:
3588 b
->pixel_center_integer
= true;
3591 case SpvExecutionModeXfb
:
3592 vtn_fail("Unhandled execution mode");
3595 case SpvExecutionModeVecTypeHint
:
3596 case SpvExecutionModeContractionOff
:
3599 case SpvExecutionModeStencilRefReplacingEXT
:
3600 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3604 vtn_fail("Unhandled execution mode");
3609 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3610 const uint32_t *w
, unsigned count
)
3612 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
3616 case SpvOpSourceContinued
:
3617 case SpvOpSourceExtension
:
3618 case SpvOpExtension
:
3619 case SpvOpCapability
:
3620 case SpvOpExtInstImport
:
3621 case SpvOpMemoryModel
:
3622 case SpvOpEntryPoint
:
3623 case SpvOpExecutionMode
:
3626 case SpvOpMemberName
:
3627 case SpvOpDecorationGroup
:
3629 case SpvOpMemberDecorate
:
3630 case SpvOpGroupDecorate
:
3631 case SpvOpGroupMemberDecorate
:
3632 vtn_fail("Invalid opcode types and variables section");
3638 case SpvOpTypeFloat
:
3639 case SpvOpTypeVector
:
3640 case SpvOpTypeMatrix
:
3641 case SpvOpTypeImage
:
3642 case SpvOpTypeSampler
:
3643 case SpvOpTypeSampledImage
:
3644 case SpvOpTypeArray
:
3645 case SpvOpTypeRuntimeArray
:
3646 case SpvOpTypeStruct
:
3647 case SpvOpTypeOpaque
:
3648 case SpvOpTypePointer
:
3649 case SpvOpTypeFunction
:
3650 case SpvOpTypeEvent
:
3651 case SpvOpTypeDeviceEvent
:
3652 case SpvOpTypeReserveId
:
3653 case SpvOpTypeQueue
:
3655 vtn_handle_type(b
, opcode
, w
, count
);
3658 case SpvOpConstantTrue
:
3659 case SpvOpConstantFalse
:
3661 case SpvOpConstantComposite
:
3662 case SpvOpConstantSampler
:
3663 case SpvOpConstantNull
:
3664 case SpvOpSpecConstantTrue
:
3665 case SpvOpSpecConstantFalse
:
3666 case SpvOpSpecConstant
:
3667 case SpvOpSpecConstantComposite
:
3668 case SpvOpSpecConstantOp
:
3669 vtn_handle_constant(b
, opcode
, w
, count
);
3674 vtn_handle_variables(b
, opcode
, w
, count
);
3678 return false; /* End of preamble */
3685 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3686 const uint32_t *w
, unsigned count
)
3692 case SpvOpLoopMerge
:
3693 case SpvOpSelectionMerge
:
3694 /* This is handled by cfg pre-pass and walk_blocks */
3698 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
3699 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3704 vtn_handle_extension(b
, opcode
, w
, count
);
3710 case SpvOpCopyMemory
:
3711 case SpvOpCopyMemorySized
:
3712 case SpvOpAccessChain
:
3713 case SpvOpPtrAccessChain
:
3714 case SpvOpInBoundsAccessChain
:
3715 case SpvOpArrayLength
:
3716 vtn_handle_variables(b
, opcode
, w
, count
);
3719 case SpvOpFunctionCall
:
3720 vtn_handle_function_call(b
, opcode
, w
, count
);
3723 case SpvOpSampledImage
:
3725 case SpvOpImageSampleImplicitLod
:
3726 case SpvOpImageSampleExplicitLod
:
3727 case SpvOpImageSampleDrefImplicitLod
:
3728 case SpvOpImageSampleDrefExplicitLod
:
3729 case SpvOpImageSampleProjImplicitLod
:
3730 case SpvOpImageSampleProjExplicitLod
:
3731 case SpvOpImageSampleProjDrefImplicitLod
:
3732 case SpvOpImageSampleProjDrefExplicitLod
:
3733 case SpvOpImageFetch
:
3734 case SpvOpImageGather
:
3735 case SpvOpImageDrefGather
:
3736 case SpvOpImageQuerySizeLod
:
3737 case SpvOpImageQueryLod
:
3738 case SpvOpImageQueryLevels
:
3739 case SpvOpImageQuerySamples
:
3740 vtn_handle_texture(b
, opcode
, w
, count
);
3743 case SpvOpImageRead
:
3744 case SpvOpImageWrite
:
3745 case SpvOpImageTexelPointer
:
3746 vtn_handle_image(b
, opcode
, w
, count
);
3749 case SpvOpImageQuerySize
: {
3750 struct vtn_pointer
*image
=
3751 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
3752 if (glsl_type_is_image(image
->type
->type
)) {
3753 vtn_handle_image(b
, opcode
, w
, count
);
3755 vtn_assert(glsl_type_is_sampler(image
->type
->type
));
3756 vtn_handle_texture(b
, opcode
, w
, count
);
3761 case SpvOpAtomicLoad
:
3762 case SpvOpAtomicExchange
:
3763 case SpvOpAtomicCompareExchange
:
3764 case SpvOpAtomicCompareExchangeWeak
:
3765 case SpvOpAtomicIIncrement
:
3766 case SpvOpAtomicIDecrement
:
3767 case SpvOpAtomicIAdd
:
3768 case SpvOpAtomicISub
:
3769 case SpvOpAtomicSMin
:
3770 case SpvOpAtomicUMin
:
3771 case SpvOpAtomicSMax
:
3772 case SpvOpAtomicUMax
:
3773 case SpvOpAtomicAnd
:
3775 case SpvOpAtomicXor
: {
3776 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
3777 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
3778 vtn_handle_image(b
, opcode
, w
, count
);
3780 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
3781 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
3786 case SpvOpAtomicStore
: {
3787 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
3788 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
3789 vtn_handle_image(b
, opcode
, w
, count
);
3791 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
3792 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
3798 /* Handle OpSelect up-front here because it needs to be able to handle
3799 * pointers and not just regular vectors and scalars.
3801 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
3802 struct vtn_value
*sel_val
= vtn_untyped_value(b
, w
[3]);
3803 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
3804 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
3806 const struct glsl_type
*sel_type
;
3807 switch (res_val
->type
->base_type
) {
3808 case vtn_base_type_scalar
:
3809 sel_type
= glsl_bool_type();
3811 case vtn_base_type_vector
:
3812 sel_type
= glsl_vector_type(GLSL_TYPE_BOOL
, res_val
->type
->length
);
3814 case vtn_base_type_pointer
:
3815 /* We need to have actual storage for pointer types */
3816 vtn_fail_if(res_val
->type
->type
== NULL
,
3817 "Invalid pointer result type for OpSelect");
3818 sel_type
= glsl_bool_type();
3821 vtn_fail("Result type of OpSelect must be a scalar, vector, or pointer");
3824 if (unlikely(sel_val
->type
->type
!= sel_type
)) {
3825 if (sel_val
->type
->type
== glsl_bool_type()) {
3826 /* This case is illegal but some older versions of GLSLang produce
3827 * it. The GLSLang issue was fixed on March 30, 2017:
3829 * https://github.com/KhronosGroup/glslang/issues/809
3831 * Unfortunately, there are applications in the wild which are
3832 * shipping with this bug so it isn't nice to fail on them so we
3833 * throw a warning instead. It's not actually a problem for us as
3834 * nir_builder will just splat the condition out which is most
3835 * likely what the client wanted anyway.
3837 vtn_warn("Condition type of OpSelect must have the same number "
3838 "of components as Result Type");
3840 vtn_fail("Condition type of OpSelect must be a scalar or vector "
3841 "of Boolean type. It must have the same number of "
3842 "components as Result Type");
3846 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
3847 obj2_val
->type
!= res_val
->type
,
3848 "Object types must match the result type in OpSelect");
3850 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3851 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, res_type
->type
);
3852 ssa
->def
= nir_bcsel(&b
->nb
, vtn_ssa_value(b
, w
[3])->def
,
3853 vtn_ssa_value(b
, w
[4])->def
,
3854 vtn_ssa_value(b
, w
[5])->def
);
3855 vtn_push_ssa(b
, w
[2], res_type
, ssa
);
3864 case SpvOpConvertFToU
:
3865 case SpvOpConvertFToS
:
3866 case SpvOpConvertSToF
:
3867 case SpvOpConvertUToF
:
3871 case SpvOpQuantizeToF16
:
3872 case SpvOpConvertPtrToU
:
3873 case SpvOpConvertUToPtr
:
3874 case SpvOpPtrCastToGeneric
:
3875 case SpvOpGenericCastToPtr
:
3881 case SpvOpSignBitSet
:
3882 case SpvOpLessOrGreater
:
3884 case SpvOpUnordered
:
3899 case SpvOpVectorTimesScalar
:
3901 case SpvOpIAddCarry
:
3902 case SpvOpISubBorrow
:
3903 case SpvOpUMulExtended
:
3904 case SpvOpSMulExtended
:
3905 case SpvOpShiftRightLogical
:
3906 case SpvOpShiftRightArithmetic
:
3907 case SpvOpShiftLeftLogical
:
3908 case SpvOpLogicalEqual
:
3909 case SpvOpLogicalNotEqual
:
3910 case SpvOpLogicalOr
:
3911 case SpvOpLogicalAnd
:
3912 case SpvOpLogicalNot
:
3913 case SpvOpBitwiseOr
:
3914 case SpvOpBitwiseXor
:
3915 case SpvOpBitwiseAnd
:
3917 case SpvOpFOrdEqual
:
3918 case SpvOpFUnordEqual
:
3919 case SpvOpINotEqual
:
3920 case SpvOpFOrdNotEqual
:
3921 case SpvOpFUnordNotEqual
:
3922 case SpvOpULessThan
:
3923 case SpvOpSLessThan
:
3924 case SpvOpFOrdLessThan
:
3925 case SpvOpFUnordLessThan
:
3926 case SpvOpUGreaterThan
:
3927 case SpvOpSGreaterThan
:
3928 case SpvOpFOrdGreaterThan
:
3929 case SpvOpFUnordGreaterThan
:
3930 case SpvOpULessThanEqual
:
3931 case SpvOpSLessThanEqual
:
3932 case SpvOpFOrdLessThanEqual
:
3933 case SpvOpFUnordLessThanEqual
:
3934 case SpvOpUGreaterThanEqual
:
3935 case SpvOpSGreaterThanEqual
:
3936 case SpvOpFOrdGreaterThanEqual
:
3937 case SpvOpFUnordGreaterThanEqual
:
3943 case SpvOpFwidthFine
:
3944 case SpvOpDPdxCoarse
:
3945 case SpvOpDPdyCoarse
:
3946 case SpvOpFwidthCoarse
:
3947 case SpvOpBitFieldInsert
:
3948 case SpvOpBitFieldSExtract
:
3949 case SpvOpBitFieldUExtract
:
3950 case SpvOpBitReverse
:
3952 case SpvOpTranspose
:
3953 case SpvOpOuterProduct
:
3954 case SpvOpMatrixTimesScalar
:
3955 case SpvOpVectorTimesMatrix
:
3956 case SpvOpMatrixTimesVector
:
3957 case SpvOpMatrixTimesMatrix
:
3958 vtn_handle_alu(b
, opcode
, w
, count
);
3961 case SpvOpVectorExtractDynamic
:
3962 case SpvOpVectorInsertDynamic
:
3963 case SpvOpVectorShuffle
:
3964 case SpvOpCompositeConstruct
:
3965 case SpvOpCompositeExtract
:
3966 case SpvOpCompositeInsert
:
3967 case SpvOpCopyObject
:
3968 vtn_handle_composite(b
, opcode
, w
, count
);
3971 case SpvOpEmitVertex
:
3972 case SpvOpEndPrimitive
:
3973 case SpvOpEmitStreamVertex
:
3974 case SpvOpEndStreamPrimitive
:
3975 case SpvOpControlBarrier
:
3976 case SpvOpMemoryBarrier
:
3977 vtn_handle_barrier(b
, opcode
, w
, count
);
3980 case SpvOpGroupNonUniformElect
:
3981 case SpvOpGroupNonUniformAll
:
3982 case SpvOpGroupNonUniformAny
:
3983 case SpvOpGroupNonUniformAllEqual
:
3984 case SpvOpGroupNonUniformBroadcast
:
3985 case SpvOpGroupNonUniformBroadcastFirst
:
3986 case SpvOpGroupNonUniformBallot
:
3987 case SpvOpGroupNonUniformInverseBallot
:
3988 case SpvOpGroupNonUniformBallotBitExtract
:
3989 case SpvOpGroupNonUniformBallotBitCount
:
3990 case SpvOpGroupNonUniformBallotFindLSB
:
3991 case SpvOpGroupNonUniformBallotFindMSB
:
3992 case SpvOpGroupNonUniformShuffle
:
3993 case SpvOpGroupNonUniformShuffleXor
:
3994 case SpvOpGroupNonUniformShuffleUp
:
3995 case SpvOpGroupNonUniformShuffleDown
:
3996 case SpvOpGroupNonUniformIAdd
:
3997 case SpvOpGroupNonUniformFAdd
:
3998 case SpvOpGroupNonUniformIMul
:
3999 case SpvOpGroupNonUniformFMul
:
4000 case SpvOpGroupNonUniformSMin
:
4001 case SpvOpGroupNonUniformUMin
:
4002 case SpvOpGroupNonUniformFMin
:
4003 case SpvOpGroupNonUniformSMax
:
4004 case SpvOpGroupNonUniformUMax
:
4005 case SpvOpGroupNonUniformFMax
:
4006 case SpvOpGroupNonUniformBitwiseAnd
:
4007 case SpvOpGroupNonUniformBitwiseOr
:
4008 case SpvOpGroupNonUniformBitwiseXor
:
4009 case SpvOpGroupNonUniformLogicalAnd
:
4010 case SpvOpGroupNonUniformLogicalOr
:
4011 case SpvOpGroupNonUniformLogicalXor
:
4012 case SpvOpGroupNonUniformQuadBroadcast
:
4013 case SpvOpGroupNonUniformQuadSwap
:
4014 vtn_handle_subgroup(b
, opcode
, w
, count
);
4018 vtn_fail("Unhandled opcode");
4025 vtn_create_builder(const uint32_t *words
, size_t word_count
,
4026 gl_shader_stage stage
, const char *entry_point_name
,
4027 const struct spirv_to_nir_options
*options
)
4029 /* Initialize the vtn_builder object */
4030 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
4032 b
->spirv_word_count
= word_count
;
4036 exec_list_make_empty(&b
->functions
);
4037 b
->entry_point_stage
= stage
;
4038 b
->entry_point_name
= entry_point_name
;
4039 b
->options
= options
;
4042 * Handle the SPIR-V header (first 5 dwords).
4043 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
4045 if (word_count
<= 5)
4048 if (words
[0] != SpvMagicNumber
) {
4049 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
4052 if (words
[1] < 0x10000) {
4053 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
4057 /* words[2] == generator magic */
4058 unsigned value_id_bound
= words
[3];
4059 if (words
[4] != 0) {
4060 vtn_err("words[4] was %u, want 0", words
[4]);
4064 b
->value_id_bound
= value_id_bound
;
4065 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
4074 spirv_to_nir(const uint32_t *words
, size_t word_count
,
4075 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
4076 gl_shader_stage stage
, const char *entry_point_name
,
4077 const struct spirv_to_nir_options
*options
,
4078 const nir_shader_compiler_options
*nir_options
)
4081 const uint32_t *word_end
= words
+ word_count
;
4083 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
4084 stage
, entry_point_name
,
4090 /* See also _vtn_fail() */
4091 if (setjmp(b
->fail_jump
)) {
4096 /* Skip the SPIR-V header, handled at vtn_create_builder */
4099 /* Handle all the preamble instructions */
4100 words
= vtn_foreach_instruction(b
, words
, word_end
,
4101 vtn_handle_preamble_instruction
);
4103 if (b
->entry_point
== NULL
) {
4104 vtn_fail("Entry point not found");
4109 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
4111 /* Set shader info defaults */
4112 b
->shader
->info
.gs
.invocations
= 1;
4114 /* Parse execution modes */
4115 vtn_foreach_execution_mode(b
, b
->entry_point
,
4116 vtn_handle_execution_mode
, NULL
);
4118 b
->specializations
= spec
;
4119 b
->num_specializations
= num_spec
;
4121 /* Handle all variable, type, and constant instructions */
4122 words
= vtn_foreach_instruction(b
, words
, word_end
,
4123 vtn_handle_variable_or_type_instruction
);
4125 /* Set types on all vtn_values */
4126 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
4128 vtn_build_cfg(b
, words
, word_end
);
4130 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4131 b
->entry_point
->func
->referenced
= true;
4136 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
4137 if (func
->referenced
&& !func
->emitted
) {
4138 b
->const_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
4139 _mesa_key_pointer_equal
);
4141 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
4147 /* We sometimes generate bogus derefs that, while never used, give the
4148 * validator a bit of heartburn. Run dead code to get rid of them.
4150 nir_opt_dce(b
->shader
);
4152 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4153 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
4154 vtn_assert(entry_point
);
4156 /* Unparent the shader from the vtn_builder before we delete the builder */
4157 ralloc_steal(NULL
, b
->shader
);