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
)
386 const char *ext
= (const char *)&w
[2];
388 case SpvOpExtInstImport
: {
389 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
390 if (strcmp(ext
, "GLSL.std.450") == 0) {
391 val
->ext_handler
= vtn_handle_glsl450_instruction
;
392 } else if ((strcmp(ext
, "SPV_AMD_gcn_shader") == 0)
393 && (b
->options
&& b
->options
->caps
.gcn_shader
)) {
394 val
->ext_handler
= vtn_handle_amd_gcn_shader_instruction
;
395 } else if ((strcmp(ext
, "SPV_AMD_shader_trinary_minmax") == 0)
396 && (b
->options
&& b
->options
->caps
.trinary_minmax
)) {
397 val
->ext_handler
= vtn_handle_amd_shader_trinary_minmax_instruction
;
399 vtn_fail("Unsupported extension: %s", ext
);
405 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
406 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
412 vtn_fail("Unhandled opcode");
417 _foreach_decoration_helper(struct vtn_builder
*b
,
418 struct vtn_value
*base_value
,
420 struct vtn_value
*value
,
421 vtn_decoration_foreach_cb cb
, void *data
)
423 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
425 if (dec
->scope
== VTN_DEC_DECORATION
) {
426 member
= parent_member
;
427 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
428 vtn_fail_if(value
->value_type
!= vtn_value_type_type
||
429 value
->type
->base_type
!= vtn_base_type_struct
,
430 "OpMemberDecorate and OpGroupMemberDecorate are only "
431 "allowed on OpTypeStruct");
432 /* This means we haven't recursed yet */
433 assert(value
== base_value
);
435 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
437 vtn_fail_if(member
>= base_value
->type
->length
,
438 "OpMemberDecorate specifies member %d but the "
439 "OpTypeStruct has only %u members",
440 member
, base_value
->type
->length
);
442 /* Not a decoration */
443 assert(dec
->scope
== VTN_DEC_EXECUTION_MODE
);
448 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
449 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
452 cb(b
, base_value
, member
, dec
, data
);
457 /** Iterates (recursively if needed) over all of the decorations on a value
459 * This function iterates over all of the decorations applied to a given
460 * value. If it encounters a decoration group, it recurses into the group
461 * and iterates over all of those decorations as well.
464 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
465 vtn_decoration_foreach_cb cb
, void *data
)
467 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
471 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
472 vtn_execution_mode_foreach_cb cb
, void *data
)
474 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
475 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
478 assert(dec
->group
== NULL
);
479 cb(b
, value
, dec
, data
);
484 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
485 const uint32_t *w
, unsigned count
)
487 const uint32_t *w_end
= w
+ count
;
488 const uint32_t target
= w
[1];
492 case SpvOpDecorationGroup
:
493 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
497 case SpvOpMemberDecorate
:
498 case SpvOpDecorateStringGOOGLE
:
499 case SpvOpMemberDecorateStringGOOGLE
:
500 case SpvOpExecutionMode
: {
501 struct vtn_value
*val
= vtn_untyped_value(b
, target
);
503 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
506 case SpvOpDecorateStringGOOGLE
:
507 dec
->scope
= VTN_DEC_DECORATION
;
509 case SpvOpMemberDecorate
:
510 case SpvOpMemberDecorateStringGOOGLE
:
511 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
512 vtn_fail_if(dec
->scope
< VTN_DEC_STRUCT_MEMBER0
, /* overflow */
513 "Member argument of OpMemberDecorate too large");
515 case SpvOpExecutionMode
:
516 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
519 unreachable("Invalid decoration opcode");
521 dec
->decoration
= *(w
++);
524 /* Link into the list */
525 dec
->next
= val
->decoration
;
526 val
->decoration
= dec
;
530 case SpvOpGroupMemberDecorate
:
531 case SpvOpGroupDecorate
: {
532 struct vtn_value
*group
=
533 vtn_value(b
, target
, vtn_value_type_decoration_group
);
535 for (; w
< w_end
; w
++) {
536 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
537 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
540 if (opcode
== SpvOpGroupDecorate
) {
541 dec
->scope
= VTN_DEC_DECORATION
;
543 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
544 vtn_fail_if(dec
->scope
< 0, /* Check for overflow */
545 "Member argument of OpGroupMemberDecorate too large");
548 /* Link into the list */
549 dec
->next
= val
->decoration
;
550 val
->decoration
= dec
;
556 unreachable("Unhandled opcode");
560 struct member_decoration_ctx
{
562 struct glsl_struct_field
*fields
;
563 struct vtn_type
*type
;
566 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
567 * OpStore, or OpCopyMemory between them without breaking anything.
568 * Technically, the SPIR-V rules require the exact same type ID but this lets
569 * us internally be a bit looser.
572 vtn_types_compatible(struct vtn_builder
*b
,
573 struct vtn_type
*t1
, struct vtn_type
*t2
)
575 if (t1
->id
== t2
->id
)
578 if (t1
->base_type
!= t2
->base_type
)
581 switch (t1
->base_type
) {
582 case vtn_base_type_void
:
583 case vtn_base_type_scalar
:
584 case vtn_base_type_vector
:
585 case vtn_base_type_matrix
:
586 case vtn_base_type_image
:
587 case vtn_base_type_sampler
:
588 case vtn_base_type_sampled_image
:
589 return t1
->type
== t2
->type
;
591 case vtn_base_type_array
:
592 return t1
->length
== t2
->length
&&
593 vtn_types_compatible(b
, t1
->array_element
, t2
->array_element
);
595 case vtn_base_type_pointer
:
596 return vtn_types_compatible(b
, t1
->deref
, t2
->deref
);
598 case vtn_base_type_struct
:
599 if (t1
->length
!= t2
->length
)
602 for (unsigned i
= 0; i
< t1
->length
; i
++) {
603 if (!vtn_types_compatible(b
, t1
->members
[i
], t2
->members
[i
]))
608 case vtn_base_type_function
:
609 /* This case shouldn't get hit since you can't copy around function
610 * types. Just require them to be identical.
615 vtn_fail("Invalid base type");
618 /* does a shallow copy of a vtn_type */
620 static struct vtn_type
*
621 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
623 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
626 switch (src
->base_type
) {
627 case vtn_base_type_void
:
628 case vtn_base_type_scalar
:
629 case vtn_base_type_vector
:
630 case vtn_base_type_matrix
:
631 case vtn_base_type_array
:
632 case vtn_base_type_pointer
:
633 case vtn_base_type_image
:
634 case vtn_base_type_sampler
:
635 case vtn_base_type_sampled_image
:
636 /* Nothing more to do */
639 case vtn_base_type_struct
:
640 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
641 memcpy(dest
->members
, src
->members
,
642 src
->length
* sizeof(src
->members
[0]));
644 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
645 memcpy(dest
->offsets
, src
->offsets
,
646 src
->length
* sizeof(src
->offsets
[0]));
649 case vtn_base_type_function
:
650 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
651 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
658 static struct vtn_type
*
659 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
661 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
662 type
= type
->members
[member
];
664 /* We may have an array of matrices.... Oh, joy! */
665 while (glsl_type_is_array(type
->type
)) {
666 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
667 type
= type
->array_element
;
670 vtn_assert(glsl_type_is_matrix(type
->type
));
676 vtn_handle_access_qualifier(struct vtn_builder
*b
, struct vtn_type
*type
,
677 int member
, enum gl_access_qualifier access
)
679 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
680 type
= type
->members
[member
];
682 type
->access
|= access
;
686 struct_member_decoration_cb(struct vtn_builder
*b
,
687 struct vtn_value
*val
, int member
,
688 const struct vtn_decoration
*dec
, void *void_ctx
)
690 struct member_decoration_ctx
*ctx
= void_ctx
;
695 assert(member
< ctx
->num_fields
);
697 switch (dec
->decoration
) {
698 case SpvDecorationRelaxedPrecision
:
699 case SpvDecorationUniform
:
700 break; /* FIXME: Do nothing with this for now. */
701 case SpvDecorationNonWritable
:
702 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_WRITEABLE
);
704 case SpvDecorationNonReadable
:
705 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_READABLE
);
707 case SpvDecorationVolatile
:
708 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_VOLATILE
);
710 case SpvDecorationCoherent
:
711 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_COHERENT
);
713 case SpvDecorationNoPerspective
:
714 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
716 case SpvDecorationFlat
:
717 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
719 case SpvDecorationCentroid
:
720 ctx
->fields
[member
].centroid
= true;
722 case SpvDecorationSample
:
723 ctx
->fields
[member
].sample
= true;
725 case SpvDecorationStream
:
726 /* Vulkan only allows one GS stream */
727 vtn_assert(dec
->literals
[0] == 0);
729 case SpvDecorationLocation
:
730 ctx
->fields
[member
].location
= dec
->literals
[0];
732 case SpvDecorationComponent
:
733 break; /* FIXME: What should we do with these? */
734 case SpvDecorationBuiltIn
:
735 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
736 ctx
->type
->members
[member
]->is_builtin
= true;
737 ctx
->type
->members
[member
]->builtin
= dec
->literals
[0];
738 ctx
->type
->builtin_block
= true;
740 case SpvDecorationOffset
:
741 ctx
->type
->offsets
[member
] = dec
->literals
[0];
743 case SpvDecorationMatrixStride
:
744 /* Handled as a second pass */
746 case SpvDecorationColMajor
:
747 break; /* Nothing to do here. Column-major is the default. */
748 case SpvDecorationRowMajor
:
749 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
752 case SpvDecorationPatch
:
755 case SpvDecorationSpecId
:
756 case SpvDecorationBlock
:
757 case SpvDecorationBufferBlock
:
758 case SpvDecorationArrayStride
:
759 case SpvDecorationGLSLShared
:
760 case SpvDecorationGLSLPacked
:
761 case SpvDecorationInvariant
:
762 case SpvDecorationRestrict
:
763 case SpvDecorationAliased
:
764 case SpvDecorationConstant
:
765 case SpvDecorationIndex
:
766 case SpvDecorationBinding
:
767 case SpvDecorationDescriptorSet
:
768 case SpvDecorationLinkageAttributes
:
769 case SpvDecorationNoContraction
:
770 case SpvDecorationInputAttachmentIndex
:
771 vtn_warn("Decoration not allowed on struct members: %s",
772 spirv_decoration_to_string(dec
->decoration
));
775 case SpvDecorationXfbBuffer
:
776 case SpvDecorationXfbStride
:
777 vtn_warn("Vulkan does not have transform feedback");
780 case SpvDecorationCPacked
:
781 case SpvDecorationSaturatedConversion
:
782 case SpvDecorationFuncParamAttr
:
783 case SpvDecorationFPRoundingMode
:
784 case SpvDecorationFPFastMathMode
:
785 case SpvDecorationAlignment
:
786 vtn_warn("Decoration only allowed for CL-style kernels: %s",
787 spirv_decoration_to_string(dec
->decoration
));
790 case SpvDecorationHlslSemanticGOOGLE
:
791 /* HLSL semantic decorations can safely be ignored by the driver. */
795 vtn_fail("Unhandled decoration");
799 /* Matrix strides are handled as a separate pass because we need to know
800 * whether the matrix is row-major or not first.
803 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
804 struct vtn_value
*val
, int member
,
805 const struct vtn_decoration
*dec
,
808 if (dec
->decoration
!= SpvDecorationMatrixStride
)
811 vtn_fail_if(member
< 0,
812 "The MatrixStride decoration is only allowed on members "
815 struct member_decoration_ctx
*ctx
= void_ctx
;
817 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
818 if (mat_type
->row_major
) {
819 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
820 mat_type
->stride
= mat_type
->array_element
->stride
;
821 mat_type
->array_element
->stride
= dec
->literals
[0];
823 vtn_assert(mat_type
->array_element
->stride
> 0);
824 mat_type
->stride
= dec
->literals
[0];
829 type_decoration_cb(struct vtn_builder
*b
,
830 struct vtn_value
*val
, int member
,
831 const struct vtn_decoration
*dec
, void *ctx
)
833 struct vtn_type
*type
= val
->type
;
836 /* This should have been handled by OpTypeStruct */
837 assert(val
->type
->base_type
== vtn_base_type_struct
);
838 assert(member
>= 0 && member
< val
->type
->length
);
842 switch (dec
->decoration
) {
843 case SpvDecorationArrayStride
:
844 vtn_assert(type
->base_type
== vtn_base_type_matrix
||
845 type
->base_type
== vtn_base_type_array
||
846 type
->base_type
== vtn_base_type_pointer
);
847 type
->stride
= dec
->literals
[0];
849 case SpvDecorationBlock
:
850 vtn_assert(type
->base_type
== vtn_base_type_struct
);
853 case SpvDecorationBufferBlock
:
854 vtn_assert(type
->base_type
== vtn_base_type_struct
);
855 type
->buffer_block
= true;
857 case SpvDecorationGLSLShared
:
858 case SpvDecorationGLSLPacked
:
859 /* Ignore these, since we get explicit offsets anyways */
862 case SpvDecorationRowMajor
:
863 case SpvDecorationColMajor
:
864 case SpvDecorationMatrixStride
:
865 case SpvDecorationBuiltIn
:
866 case SpvDecorationNoPerspective
:
867 case SpvDecorationFlat
:
868 case SpvDecorationPatch
:
869 case SpvDecorationCentroid
:
870 case SpvDecorationSample
:
871 case SpvDecorationVolatile
:
872 case SpvDecorationCoherent
:
873 case SpvDecorationNonWritable
:
874 case SpvDecorationNonReadable
:
875 case SpvDecorationUniform
:
876 case SpvDecorationLocation
:
877 case SpvDecorationComponent
:
878 case SpvDecorationOffset
:
879 case SpvDecorationXfbBuffer
:
880 case SpvDecorationXfbStride
:
881 case SpvDecorationHlslSemanticGOOGLE
:
882 vtn_warn("Decoration only allowed for struct members: %s",
883 spirv_decoration_to_string(dec
->decoration
));
886 case SpvDecorationStream
:
887 /* We don't need to do anything here, as stream is filled up when
888 * aplying the decoration to a variable, just check that if it is not a
889 * struct member, it should be a struct.
891 vtn_assert(type
->base_type
== vtn_base_type_struct
);
894 case SpvDecorationRelaxedPrecision
:
895 case SpvDecorationSpecId
:
896 case SpvDecorationInvariant
:
897 case SpvDecorationRestrict
:
898 case SpvDecorationAliased
:
899 case SpvDecorationConstant
:
900 case SpvDecorationIndex
:
901 case SpvDecorationBinding
:
902 case SpvDecorationDescriptorSet
:
903 case SpvDecorationLinkageAttributes
:
904 case SpvDecorationNoContraction
:
905 case SpvDecorationInputAttachmentIndex
:
906 vtn_warn("Decoration not allowed on types: %s",
907 spirv_decoration_to_string(dec
->decoration
));
910 case SpvDecorationCPacked
:
911 case SpvDecorationSaturatedConversion
:
912 case SpvDecorationFuncParamAttr
:
913 case SpvDecorationFPRoundingMode
:
914 case SpvDecorationFPFastMathMode
:
915 case SpvDecorationAlignment
:
916 vtn_warn("Decoration only allowed for CL-style kernels: %s",
917 spirv_decoration_to_string(dec
->decoration
));
921 vtn_fail("Unhandled decoration");
926 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
929 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
930 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
931 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
932 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
933 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
934 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
935 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
936 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
937 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
938 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
939 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
940 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
941 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
942 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
943 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
944 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
945 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
946 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
947 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
948 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
949 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
950 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
951 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
952 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
953 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
954 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
955 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
956 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
957 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
958 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
959 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
960 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
961 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
962 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
963 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
964 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
965 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
966 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
967 case SpvImageFormatR16ui
: return 0x8234; /* GL_R16UI */
968 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
970 vtn_fail("Invalid image format");
974 static struct vtn_type
*
975 vtn_type_layout_std430(struct vtn_builder
*b
, struct vtn_type
*type
,
976 uint32_t *size_out
, uint32_t *align_out
)
978 switch (type
->base_type
) {
979 case vtn_base_type_scalar
: {
980 uint32_t comp_size
= glsl_get_bit_size(type
->type
) / 8;
981 *size_out
= comp_size
;
982 *align_out
= comp_size
;
986 case vtn_base_type_vector
: {
987 uint32_t comp_size
= glsl_get_bit_size(type
->type
) / 8;
988 unsigned align_comps
= type
->length
== 3 ? 4 : type
->length
;
989 *size_out
= comp_size
* type
->length
,
990 *align_out
= comp_size
* align_comps
;
994 case vtn_base_type_matrix
:
995 case vtn_base_type_array
: {
996 /* We're going to add an array stride */
997 type
= vtn_type_copy(b
, type
);
998 uint32_t elem_size
, elem_align
;
999 type
->array_element
= vtn_type_layout_std430(b
, type
->array_element
,
1000 &elem_size
, &elem_align
);
1001 type
->stride
= vtn_align_u32(elem_size
, elem_align
);
1002 *size_out
= type
->stride
* type
->length
;
1003 *align_out
= elem_align
;
1007 case vtn_base_type_struct
: {
1008 /* We're going to add member offsets */
1009 type
= vtn_type_copy(b
, type
);
1010 uint32_t offset
= 0;
1012 for (unsigned i
= 0; i
< type
->length
; i
++) {
1013 uint32_t mem_size
, mem_align
;
1014 type
->members
[i
] = vtn_type_layout_std430(b
, type
->members
[i
],
1015 &mem_size
, &mem_align
);
1016 offset
= vtn_align_u32(offset
, mem_align
);
1017 type
->offsets
[i
] = offset
;
1019 align
= MAX2(align
, mem_align
);
1027 unreachable("Invalid SPIR-V type for std430");
1032 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
1033 const uint32_t *w
, unsigned count
)
1035 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1037 val
->type
= rzalloc(b
, struct vtn_type
);
1038 val
->type
->id
= w
[1];
1042 val
->type
->base_type
= vtn_base_type_void
;
1043 val
->type
->type
= glsl_void_type();
1046 val
->type
->base_type
= vtn_base_type_scalar
;
1047 val
->type
->type
= glsl_bool_type();
1048 val
->type
->length
= 1;
1050 case SpvOpTypeInt
: {
1051 int bit_size
= w
[2];
1052 const bool signedness
= w
[3];
1053 val
->type
->base_type
= vtn_base_type_scalar
;
1056 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1059 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1062 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1065 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1068 vtn_fail("Invalid int bit size");
1070 val
->type
->length
= 1;
1074 case SpvOpTypeFloat
: {
1075 int bit_size
= w
[2];
1076 val
->type
->base_type
= vtn_base_type_scalar
;
1079 val
->type
->type
= glsl_float16_t_type();
1082 val
->type
->type
= glsl_float_type();
1085 val
->type
->type
= glsl_double_type();
1088 vtn_fail("Invalid float bit size");
1090 val
->type
->length
= 1;
1094 case SpvOpTypeVector
: {
1095 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1096 unsigned elems
= w
[3];
1098 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1099 "Base type for OpTypeVector must be a scalar");
1100 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1101 "Invalid component count for OpTypeVector");
1103 val
->type
->base_type
= vtn_base_type_vector
;
1104 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1105 val
->type
->length
= elems
;
1106 val
->type
->stride
= glsl_get_bit_size(base
->type
) / 8;
1107 val
->type
->array_element
= base
;
1111 case SpvOpTypeMatrix
: {
1112 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1113 unsigned columns
= w
[3];
1115 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1116 "Base type for OpTypeMatrix must be a vector");
1117 vtn_fail_if(columns
< 2 || columns
> 4,
1118 "Invalid column count for OpTypeMatrix");
1120 val
->type
->base_type
= vtn_base_type_matrix
;
1121 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1122 glsl_get_vector_elements(base
->type
),
1124 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1125 "Unsupported base type for OpTypeMatrix");
1126 assert(!glsl_type_is_error(val
->type
->type
));
1127 val
->type
->length
= columns
;
1128 val
->type
->array_element
= base
;
1129 val
->type
->row_major
= false;
1130 val
->type
->stride
= 0;
1134 case SpvOpTypeRuntimeArray
:
1135 case SpvOpTypeArray
: {
1136 struct vtn_type
*array_element
=
1137 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1139 if (opcode
== SpvOpTypeRuntimeArray
) {
1140 /* A length of 0 is used to denote unsized arrays */
1141 val
->type
->length
= 0;
1144 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
1147 val
->type
->base_type
= vtn_base_type_array
;
1148 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
);
1149 val
->type
->array_element
= array_element
;
1150 val
->type
->stride
= 0;
1154 case SpvOpTypeStruct
: {
1155 unsigned num_fields
= count
- 2;
1156 val
->type
->base_type
= vtn_base_type_struct
;
1157 val
->type
->length
= num_fields
;
1158 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1159 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1161 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1162 for (unsigned i
= 0; i
< num_fields
; i
++) {
1163 val
->type
->members
[i
] =
1164 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
1165 fields
[i
] = (struct glsl_struct_field
) {
1166 .type
= val
->type
->members
[i
]->type
,
1167 .name
= ralloc_asprintf(b
, "field%d", i
),
1172 struct member_decoration_ctx ctx
= {
1173 .num_fields
= num_fields
,
1178 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1179 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1181 const char *name
= val
->name
? val
->name
: "struct";
1183 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
);
1187 case SpvOpTypeFunction
: {
1188 val
->type
->base_type
= vtn_base_type_function
;
1189 val
->type
->type
= NULL
;
1191 val
->type
->return_type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1193 const unsigned num_params
= count
- 3;
1194 val
->type
->length
= num_params
;
1195 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1196 for (unsigned i
= 0; i
< count
- 3; i
++) {
1197 val
->type
->params
[i
] =
1198 vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
;
1203 case SpvOpTypePointer
: {
1204 SpvStorageClass storage_class
= w
[2];
1205 struct vtn_type
*deref_type
=
1206 vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
1208 val
->type
->base_type
= vtn_base_type_pointer
;
1209 val
->type
->storage_class
= storage_class
;
1210 val
->type
->deref
= deref_type
;
1212 if (storage_class
== SpvStorageClassUniform
||
1213 storage_class
== SpvStorageClassStorageBuffer
) {
1214 /* These can actually be stored to nir_variables and used as SSA
1215 * values so they need a real glsl_type.
1217 val
->type
->type
= glsl_vector_type(GLSL_TYPE_UINT
, 2);
1220 if (storage_class
== SpvStorageClassPushConstant
) {
1221 /* These can actually be stored to nir_variables and used as SSA
1222 * values so they need a real glsl_type.
1224 val
->type
->type
= glsl_uint_type();
1227 if (storage_class
== SpvStorageClassWorkgroup
) {
1228 /* These can actually be stored to nir_variables and used as SSA
1229 * values so they need a real glsl_type.
1231 val
->type
->type
= glsl_uint_type();
1232 if (b
->options
->lower_workgroup_access_to_offsets
) {
1233 uint32_t size
, align
;
1234 val
->type
->deref
= vtn_type_layout_std430(b
, val
->type
->deref
,
1236 val
->type
->length
= size
;
1237 val
->type
->align
= align
;
1243 case SpvOpTypeImage
: {
1244 val
->type
->base_type
= vtn_base_type_image
;
1246 const struct vtn_type
*sampled_type
=
1247 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1249 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1250 glsl_get_bit_size(sampled_type
->type
) != 32,
1251 "Sampled type of OpTypeImage must be a 32-bit scalar");
1253 enum glsl_sampler_dim dim
;
1254 switch ((SpvDim
)w
[3]) {
1255 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1256 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1257 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1258 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1259 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1260 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1261 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1263 vtn_fail("Invalid SPIR-V image dimensionality");
1266 bool is_shadow
= w
[4];
1267 bool is_array
= w
[5];
1268 bool multisampled
= w
[6];
1269 unsigned sampled
= w
[7];
1270 SpvImageFormat format
= w
[8];
1273 val
->type
->access_qualifier
= w
[9];
1275 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1278 if (dim
== GLSL_SAMPLER_DIM_2D
)
1279 dim
= GLSL_SAMPLER_DIM_MS
;
1280 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1281 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1283 vtn_fail("Unsupported multisampled image type");
1286 val
->type
->image_format
= translate_image_format(b
, format
);
1288 enum glsl_base_type sampled_base_type
=
1289 glsl_get_base_type(sampled_type
->type
);
1291 val
->type
->sampled
= true;
1292 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
1294 } else if (sampled
== 2) {
1295 vtn_assert(!is_shadow
);
1296 val
->type
->sampled
= false;
1297 val
->type
->type
= glsl_image_type(dim
, is_array
, sampled_base_type
);
1299 vtn_fail("We need to know if the image will be sampled");
1304 case SpvOpTypeSampledImage
:
1305 val
->type
->base_type
= vtn_base_type_sampled_image
;
1306 val
->type
->image
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1307 val
->type
->type
= val
->type
->image
->type
;
1310 case SpvOpTypeSampler
:
1311 /* The actual sampler type here doesn't really matter. It gets
1312 * thrown away the moment you combine it with an image. What really
1313 * matters is that it's a sampler type as opposed to an integer type
1314 * so the backend knows what to do.
1316 val
->type
->base_type
= vtn_base_type_sampler
;
1317 val
->type
->type
= glsl_bare_sampler_type();
1320 case SpvOpTypeOpaque
:
1321 case SpvOpTypeEvent
:
1322 case SpvOpTypeDeviceEvent
:
1323 case SpvOpTypeReserveId
:
1324 case SpvOpTypeQueue
:
1327 vtn_fail("Unhandled opcode");
1330 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1333 static nir_constant
*
1334 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
1336 nir_constant
*c
= rzalloc(b
, nir_constant
);
1338 /* For pointers and other typeless things, we have to return something but
1339 * it doesn't matter what.
1344 switch (glsl_get_base_type(type
)) {
1346 case GLSL_TYPE_UINT
:
1347 case GLSL_TYPE_INT16
:
1348 case GLSL_TYPE_UINT16
:
1349 case GLSL_TYPE_UINT8
:
1350 case GLSL_TYPE_INT8
:
1351 case GLSL_TYPE_INT64
:
1352 case GLSL_TYPE_UINT64
:
1353 case GLSL_TYPE_BOOL
:
1354 case GLSL_TYPE_FLOAT
:
1355 case GLSL_TYPE_FLOAT16
:
1356 case GLSL_TYPE_DOUBLE
:
1357 /* Nothing to do here. It's already initialized to zero */
1360 case GLSL_TYPE_ARRAY
:
1361 vtn_assert(glsl_get_length(type
) > 0);
1362 c
->num_elements
= glsl_get_length(type
);
1363 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1365 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
1366 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1367 c
->elements
[i
] = c
->elements
[0];
1370 case GLSL_TYPE_STRUCT
:
1371 c
->num_elements
= glsl_get_length(type
);
1372 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1374 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
1375 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
1380 vtn_fail("Invalid type for null constant");
1387 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
1388 int member
, const struct vtn_decoration
*dec
,
1391 vtn_assert(member
== -1);
1392 if (dec
->decoration
!= SpvDecorationSpecId
)
1395 struct spec_constant_value
*const_value
= data
;
1397 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1398 if (b
->specializations
[i
].id
== dec
->literals
[0]) {
1399 if (const_value
->is_double
)
1400 const_value
->data64
= b
->specializations
[i
].data64
;
1402 const_value
->data32
= b
->specializations
[i
].data32
;
1409 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
1410 uint32_t const_value
)
1412 struct spec_constant_value data
;
1413 data
.is_double
= false;
1414 data
.data32
= const_value
;
1415 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1420 get_specialization64(struct vtn_builder
*b
, struct vtn_value
*val
,
1421 uint64_t const_value
)
1423 struct spec_constant_value data
;
1424 data
.is_double
= true;
1425 data
.data64
= const_value
;
1426 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1431 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1432 struct vtn_value
*val
,
1434 const struct vtn_decoration
*dec
,
1437 vtn_assert(member
== -1);
1438 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1439 dec
->literals
[0] != SpvBuiltInWorkgroupSize
)
1442 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1444 b
->shader
->info
.cs
.local_size
[0] = val
->constant
->values
[0].u32
[0];
1445 b
->shader
->info
.cs
.local_size
[1] = val
->constant
->values
[0].u32
[1];
1446 b
->shader
->info
.cs
.local_size
[2] = val
->constant
->values
[0].u32
[2];
1450 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1451 const uint32_t *w
, unsigned count
)
1453 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1454 val
->constant
= rzalloc(b
, nir_constant
);
1456 case SpvOpConstantTrue
:
1457 case SpvOpConstantFalse
:
1458 case SpvOpSpecConstantTrue
:
1459 case SpvOpSpecConstantFalse
: {
1460 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1461 "Result type of %s must be OpTypeBool",
1462 spirv_op_to_string(opcode
));
1464 uint32_t int_val
= (opcode
== SpvOpConstantTrue
||
1465 opcode
== SpvOpSpecConstantTrue
);
1467 if (opcode
== SpvOpSpecConstantTrue
||
1468 opcode
== SpvOpSpecConstantFalse
)
1469 int_val
= get_specialization(b
, val
, int_val
);
1471 val
->constant
->values
[0].u32
[0] = int_val
? NIR_TRUE
: NIR_FALSE
;
1475 case SpvOpConstant
: {
1476 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1477 "Result type of %s must be a scalar",
1478 spirv_op_to_string(opcode
));
1479 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1482 val
->constant
->values
->u64
[0] = vtn_u64_literal(&w
[3]);
1485 val
->constant
->values
->u32
[0] = w
[3];
1488 val
->constant
->values
->u16
[0] = w
[3];
1491 val
->constant
->values
->u8
[0] = w
[3];
1494 vtn_fail("Unsupported SpvOpConstant bit size");
1499 case SpvOpSpecConstant
: {
1500 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1501 "Result type of %s must be a scalar",
1502 spirv_op_to_string(opcode
));
1503 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1506 val
->constant
->values
[0].u64
[0] =
1507 get_specialization64(b
, val
, vtn_u64_literal(&w
[3]));
1510 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1513 val
->constant
->values
[0].u16
[0] = get_specialization(b
, val
, w
[3]);
1516 val
->constant
->values
[0].u8
[0] = get_specialization(b
, val
, w
[3]);
1519 vtn_fail("Unsupported SpvOpSpecConstant bit size");
1524 case SpvOpSpecConstantComposite
:
1525 case SpvOpConstantComposite
: {
1526 unsigned elem_count
= count
- 3;
1527 vtn_fail_if(elem_count
!= val
->type
->length
,
1528 "%s has %u constituents, expected %u",
1529 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1531 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1532 for (unsigned i
= 0; i
< elem_count
; i
++) {
1533 struct vtn_value
*val
= vtn_untyped_value(b
, w
[i
+ 3]);
1535 if (val
->value_type
== vtn_value_type_constant
) {
1536 elems
[i
] = val
->constant
;
1538 vtn_fail_if(val
->value_type
!= vtn_value_type_undef
,
1539 "only constants or undefs allowed for "
1540 "SpvOpConstantComposite");
1541 /* to make it easier, just insert a NULL constant for now */
1542 elems
[i
] = vtn_null_constant(b
, val
->type
->type
);
1546 switch (val
->type
->base_type
) {
1547 case vtn_base_type_vector
: {
1548 assert(glsl_type_is_vector(val
->type
->type
));
1549 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1550 for (unsigned i
= 0; i
< elem_count
; i
++) {
1553 val
->constant
->values
[0].u64
[i
] = elems
[i
]->values
[0].u64
[0];
1556 val
->constant
->values
[0].u32
[i
] = elems
[i
]->values
[0].u32
[0];
1559 val
->constant
->values
[0].u16
[i
] = elems
[i
]->values
[0].u16
[0];
1562 val
->constant
->values
[0].u8
[i
] = elems
[i
]->values
[0].u8
[0];
1565 val
->constant
->values
[0].b
[i
] = elems
[i
]->values
[0].b
[0];
1568 vtn_fail("Invalid SpvOpConstantComposite bit size");
1574 case vtn_base_type_matrix
:
1575 assert(glsl_type_is_matrix(val
->type
->type
));
1576 for (unsigned i
= 0; i
< elem_count
; i
++)
1577 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1580 case vtn_base_type_struct
:
1581 case vtn_base_type_array
:
1582 ralloc_steal(val
->constant
, elems
);
1583 val
->constant
->num_elements
= elem_count
;
1584 val
->constant
->elements
= elems
;
1588 vtn_fail("Result type of %s must be a composite type",
1589 spirv_op_to_string(opcode
));
1594 case SpvOpSpecConstantOp
: {
1595 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1597 case SpvOpVectorShuffle
: {
1598 struct vtn_value
*v0
= &b
->values
[w
[4]];
1599 struct vtn_value
*v1
= &b
->values
[w
[5]];
1601 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1602 v0
->value_type
== vtn_value_type_undef
);
1603 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1604 v1
->value_type
== vtn_value_type_undef
);
1606 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1607 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1609 vtn_assert(len0
+ len1
< 16);
1611 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1612 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1613 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1615 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1616 (void)bit_size0
; (void)bit_size1
;
1618 if (bit_size
== 64) {
1620 if (v0
->value_type
== vtn_value_type_constant
) {
1621 for (unsigned i
= 0; i
< len0
; i
++)
1622 u64
[i
] = v0
->constant
->values
[0].u64
[i
];
1624 if (v1
->value_type
== vtn_value_type_constant
) {
1625 for (unsigned i
= 0; i
< len1
; i
++)
1626 u64
[len0
+ i
] = v1
->constant
->values
[0].u64
[i
];
1629 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1630 uint32_t comp
= w
[i
+ 6];
1631 /* If component is not used, set the value to a known constant
1632 * to detect if it is wrongly used.
1634 if (comp
== (uint32_t)-1)
1635 val
->constant
->values
[0].u64
[j
] = 0xdeadbeefdeadbeef;
1637 val
->constant
->values
[0].u64
[j
] = u64
[comp
];
1640 /* This is for both 32-bit and 16-bit values */
1642 if (v0
->value_type
== vtn_value_type_constant
) {
1643 for (unsigned i
= 0; i
< len0
; i
++)
1644 u32
[i
] = v0
->constant
->values
[0].u32
[i
];
1646 if (v1
->value_type
== vtn_value_type_constant
) {
1647 for (unsigned i
= 0; i
< len1
; i
++)
1648 u32
[len0
+ i
] = v1
->constant
->values
[0].u32
[i
];
1651 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1652 uint32_t comp
= w
[i
+ 6];
1653 /* If component is not used, set the value to a known constant
1654 * to detect if it is wrongly used.
1656 if (comp
== (uint32_t)-1)
1657 val
->constant
->values
[0].u32
[j
] = 0xdeadbeef;
1659 val
->constant
->values
[0].u32
[j
] = u32
[comp
];
1665 case SpvOpCompositeExtract
:
1666 case SpvOpCompositeInsert
: {
1667 struct vtn_value
*comp
;
1668 unsigned deref_start
;
1669 struct nir_constant
**c
;
1670 if (opcode
== SpvOpCompositeExtract
) {
1671 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1673 c
= &comp
->constant
;
1675 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1677 val
->constant
= nir_constant_clone(comp
->constant
,
1684 const struct vtn_type
*type
= comp
->type
;
1685 for (unsigned i
= deref_start
; i
< count
; i
++) {
1686 vtn_fail_if(w
[i
] > type
->length
,
1687 "%uth index of %s is %u but the type has only "
1688 "%u elements", i
- deref_start
,
1689 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1691 switch (type
->base_type
) {
1692 case vtn_base_type_vector
:
1694 type
= type
->array_element
;
1697 case vtn_base_type_matrix
:
1698 assert(col
== 0 && elem
== -1);
1701 type
= type
->array_element
;
1704 case vtn_base_type_array
:
1705 c
= &(*c
)->elements
[w
[i
]];
1706 type
= type
->array_element
;
1709 case vtn_base_type_struct
:
1710 c
= &(*c
)->elements
[w
[i
]];
1711 type
= type
->members
[w
[i
]];
1715 vtn_fail("%s must only index into composite types",
1716 spirv_op_to_string(opcode
));
1720 if (opcode
== SpvOpCompositeExtract
) {
1724 unsigned num_components
= type
->length
;
1725 unsigned bit_size
= glsl_get_bit_size(type
->type
);
1726 for (unsigned i
= 0; i
< num_components
; i
++)
1729 val
->constant
->values
[0].u64
[i
] = (*c
)->values
[col
].u64
[elem
+ i
];
1732 val
->constant
->values
[0].u32
[i
] = (*c
)->values
[col
].u32
[elem
+ i
];
1735 val
->constant
->values
[0].u16
[i
] = (*c
)->values
[col
].u16
[elem
+ i
];
1738 val
->constant
->values
[0].u8
[i
] = (*c
)->values
[col
].u8
[elem
+ i
];
1741 val
->constant
->values
[0].b
[i
] = (*c
)->values
[col
].b
[elem
+ i
];
1744 vtn_fail("Invalid SpvOpCompositeExtract bit size");
1748 struct vtn_value
*insert
=
1749 vtn_value(b
, w
[4], vtn_value_type_constant
);
1750 vtn_assert(insert
->type
== type
);
1752 *c
= insert
->constant
;
1754 unsigned num_components
= type
->length
;
1755 unsigned bit_size
= glsl_get_bit_size(type
->type
);
1756 for (unsigned i
= 0; i
< num_components
; i
++)
1759 (*c
)->values
[col
].u64
[elem
+ i
] = insert
->constant
->values
[0].u64
[i
];
1762 (*c
)->values
[col
].u32
[elem
+ i
] = insert
->constant
->values
[0].u32
[i
];
1765 (*c
)->values
[col
].u16
[elem
+ i
] = insert
->constant
->values
[0].u16
[i
];
1768 (*c
)->values
[col
].u8
[elem
+ i
] = insert
->constant
->values
[0].u8
[i
];
1771 (*c
)->values
[col
].b
[elem
+ i
] = insert
->constant
->values
[0].b
[i
];
1774 vtn_fail("Invalid SpvOpCompositeInsert bit size");
1783 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
1784 nir_alu_type src_alu_type
= dst_alu_type
;
1785 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
1788 vtn_assert(count
<= 7);
1793 /* We have a source in a conversion */
1795 nir_get_nir_type_for_glsl_type(
1796 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1797 /* We use the bitsize of the conversion source to evaluate the opcode later */
1798 bit_size
= glsl_get_bit_size(
1799 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1802 bit_size
= glsl_get_bit_size(val
->type
->type
);
1805 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
1806 nir_alu_type_get_type_size(src_alu_type
),
1807 nir_alu_type_get_type_size(dst_alu_type
));
1808 nir_const_value src
[4];
1810 for (unsigned i
= 0; i
< count
- 4; i
++) {
1811 struct vtn_value
*src_val
=
1812 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
);
1814 /* If this is an unsized source, pull the bit size from the
1815 * source; otherwise, we'll use the bit size from the destination.
1817 if (!nir_alu_type_get_type_size(nir_op_infos
[op
].input_types
[i
]))
1818 bit_size
= glsl_get_bit_size(src_val
->type
->type
);
1820 unsigned j
= swap
? 1 - i
: i
;
1821 src
[j
] = src_val
->constant
->values
[0];
1824 /* fix up fixed size sources */
1831 for (unsigned i
= 0; i
< num_components
; ++i
) {
1833 case 64: src
[1].u32
[i
] = src
[1].u64
[i
]; break;
1834 case 16: src
[1].u32
[i
] = src
[1].u16
[i
]; break;
1835 case 8: src
[1].u32
[i
] = src
[1].u8
[i
]; break;
1844 val
->constant
->values
[0] =
1845 nir_eval_const_opcode(op
, num_components
, bit_size
, src
);
1852 case SpvOpConstantNull
:
1853 val
->constant
= vtn_null_constant(b
, val
->type
->type
);
1856 case SpvOpConstantSampler
:
1857 vtn_fail("OpConstantSampler requires Kernel Capability");
1861 vtn_fail("Unhandled opcode");
1864 /* Now that we have the value, update the workgroup size if needed */
1865 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1868 struct vtn_ssa_value
*
1869 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1871 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1874 if (!glsl_type_is_vector_or_scalar(type
)) {
1875 unsigned elems
= glsl_get_length(type
);
1876 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1877 for (unsigned i
= 0; i
< elems
; i
++) {
1878 const struct glsl_type
*child_type
;
1880 switch (glsl_get_base_type(type
)) {
1882 case GLSL_TYPE_UINT
:
1883 case GLSL_TYPE_INT16
:
1884 case GLSL_TYPE_UINT16
:
1885 case GLSL_TYPE_UINT8
:
1886 case GLSL_TYPE_INT8
:
1887 case GLSL_TYPE_INT64
:
1888 case GLSL_TYPE_UINT64
:
1889 case GLSL_TYPE_BOOL
:
1890 case GLSL_TYPE_FLOAT
:
1891 case GLSL_TYPE_FLOAT16
:
1892 case GLSL_TYPE_DOUBLE
:
1893 child_type
= glsl_get_column_type(type
);
1895 case GLSL_TYPE_ARRAY
:
1896 child_type
= glsl_get_array_element(type
);
1898 case GLSL_TYPE_STRUCT
:
1899 child_type
= glsl_get_struct_field(type
, i
);
1902 vtn_fail("unkown base type");
1905 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
1913 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
1916 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
1917 src
.src_type
= type
;
1922 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
1923 const uint32_t *w
, unsigned count
)
1925 if (opcode
== SpvOpSampledImage
) {
1926 struct vtn_value
*val
=
1927 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1928 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1929 val
->sampled_image
->type
=
1930 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1931 val
->sampled_image
->image
=
1932 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1933 val
->sampled_image
->sampler
=
1934 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
1936 } else if (opcode
== SpvOpImage
) {
1937 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1938 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
1939 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
1940 val
->pointer
= src_val
->sampled_image
->image
;
1942 vtn_assert(src_val
->value_type
== vtn_value_type_pointer
);
1943 val
->pointer
= src_val
->pointer
;
1948 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1949 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1951 struct vtn_sampled_image sampled
;
1952 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
1953 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
1954 sampled
= *sampled_val
->sampled_image
;
1956 vtn_assert(sampled_val
->value_type
== vtn_value_type_pointer
);
1957 sampled
.type
= sampled_val
->pointer
->type
;
1958 sampled
.image
= NULL
;
1959 sampled
.sampler
= sampled_val
->pointer
;
1962 const struct glsl_type
*image_type
= sampled
.type
->type
;
1963 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
1964 const bool is_array
= glsl_sampler_type_is_array(image_type
);
1966 /* Figure out the base texture operation */
1969 case SpvOpImageSampleImplicitLod
:
1970 case SpvOpImageSampleDrefImplicitLod
:
1971 case SpvOpImageSampleProjImplicitLod
:
1972 case SpvOpImageSampleProjDrefImplicitLod
:
1973 texop
= nir_texop_tex
;
1976 case SpvOpImageSampleExplicitLod
:
1977 case SpvOpImageSampleDrefExplicitLod
:
1978 case SpvOpImageSampleProjExplicitLod
:
1979 case SpvOpImageSampleProjDrefExplicitLod
:
1980 texop
= nir_texop_txl
;
1983 case SpvOpImageFetch
:
1984 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
1985 texop
= nir_texop_txf_ms
;
1987 texop
= nir_texop_txf
;
1991 case SpvOpImageGather
:
1992 case SpvOpImageDrefGather
:
1993 texop
= nir_texop_tg4
;
1996 case SpvOpImageQuerySizeLod
:
1997 case SpvOpImageQuerySize
:
1998 texop
= nir_texop_txs
;
2001 case SpvOpImageQueryLod
:
2002 texop
= nir_texop_lod
;
2005 case SpvOpImageQueryLevels
:
2006 texop
= nir_texop_query_levels
;
2009 case SpvOpImageQuerySamples
:
2010 texop
= nir_texop_texture_samples
;
2014 vtn_fail("Unhandled opcode");
2017 nir_tex_src srcs
[10]; /* 10 should be enough */
2018 nir_tex_src
*p
= srcs
;
2020 nir_deref_instr
*sampler
= vtn_pointer_to_deref(b
, sampled
.sampler
);
2021 nir_deref_instr
*texture
=
2022 sampled
.image
? vtn_pointer_to_deref(b
, sampled
.image
) : sampler
;
2024 p
->src
= nir_src_for_ssa(&texture
->dest
.ssa
);
2025 p
->src_type
= nir_tex_src_texture_deref
;
2034 /* These operations require a sampler */
2035 p
->src
= nir_src_for_ssa(&sampler
->dest
.ssa
);
2036 p
->src_type
= nir_tex_src_sampler_deref
;
2040 case nir_texop_txf_ms
:
2043 case nir_texop_query_levels
:
2044 case nir_texop_texture_samples
:
2045 case nir_texop_samples_identical
:
2048 case nir_texop_txf_ms_mcs
:
2049 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2054 struct nir_ssa_def
*coord
;
2055 unsigned coord_components
;
2057 case SpvOpImageSampleImplicitLod
:
2058 case SpvOpImageSampleExplicitLod
:
2059 case SpvOpImageSampleDrefImplicitLod
:
2060 case SpvOpImageSampleDrefExplicitLod
:
2061 case SpvOpImageSampleProjImplicitLod
:
2062 case SpvOpImageSampleProjExplicitLod
:
2063 case SpvOpImageSampleProjDrefImplicitLod
:
2064 case SpvOpImageSampleProjDrefExplicitLod
:
2065 case SpvOpImageFetch
:
2066 case SpvOpImageGather
:
2067 case SpvOpImageDrefGather
:
2068 case SpvOpImageQueryLod
: {
2069 /* All these types have the coordinate as their first real argument */
2070 switch (sampler_dim
) {
2071 case GLSL_SAMPLER_DIM_1D
:
2072 case GLSL_SAMPLER_DIM_BUF
:
2073 coord_components
= 1;
2075 case GLSL_SAMPLER_DIM_2D
:
2076 case GLSL_SAMPLER_DIM_RECT
:
2077 case GLSL_SAMPLER_DIM_MS
:
2078 coord_components
= 2;
2080 case GLSL_SAMPLER_DIM_3D
:
2081 case GLSL_SAMPLER_DIM_CUBE
:
2082 coord_components
= 3;
2085 vtn_fail("Invalid sampler type");
2088 if (is_array
&& texop
!= nir_texop_lod
)
2091 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
2092 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2093 (1 << coord_components
) - 1));
2094 p
->src_type
= nir_tex_src_coord
;
2101 coord_components
= 0;
2106 case SpvOpImageSampleProjImplicitLod
:
2107 case SpvOpImageSampleProjExplicitLod
:
2108 case SpvOpImageSampleProjDrefImplicitLod
:
2109 case SpvOpImageSampleProjDrefExplicitLod
:
2110 /* These have the projector as the last coordinate component */
2111 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2112 p
->src_type
= nir_tex_src_projector
;
2120 bool is_shadow
= false;
2121 unsigned gather_component
= 0;
2123 case SpvOpImageSampleDrefImplicitLod
:
2124 case SpvOpImageSampleDrefExplicitLod
:
2125 case SpvOpImageSampleProjDrefImplicitLod
:
2126 case SpvOpImageSampleProjDrefExplicitLod
:
2127 case SpvOpImageDrefGather
:
2128 /* These all have an explicit depth value as their next source */
2130 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2133 case SpvOpImageGather
:
2134 /* This has a component as its next source */
2136 vtn_value(b
, w
[idx
++], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
2143 /* For OpImageQuerySizeLod, we always have an LOD */
2144 if (opcode
== SpvOpImageQuerySizeLod
)
2145 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2147 /* Now we need to handle some number of optional arguments */
2148 const struct vtn_ssa_value
*gather_offsets
= NULL
;
2150 uint32_t operands
= w
[idx
++];
2152 if (operands
& SpvImageOperandsBiasMask
) {
2153 vtn_assert(texop
== nir_texop_tex
);
2154 texop
= nir_texop_txb
;
2155 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
2158 if (operands
& SpvImageOperandsLodMask
) {
2159 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2160 texop
== nir_texop_txs
);
2161 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2164 if (operands
& SpvImageOperandsGradMask
) {
2165 vtn_assert(texop
== nir_texop_txl
);
2166 texop
= nir_texop_txd
;
2167 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
2168 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
2171 if (operands
& SpvImageOperandsOffsetMask
||
2172 operands
& SpvImageOperandsConstOffsetMask
)
2173 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
2175 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2176 nir_tex_src none
= {0};
2177 gather_offsets
= vtn_ssa_value(b
, w
[idx
++]);
2181 if (operands
& SpvImageOperandsSampleMask
) {
2182 vtn_assert(texop
== nir_texop_txf_ms
);
2183 texop
= nir_texop_txf_ms
;
2184 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
2187 if (operands
& SpvImageOperandsMinLodMask
) {
2188 vtn_assert(texop
== nir_texop_tex
||
2189 texop
== nir_texop_txb
||
2190 texop
== nir_texop_txd
);
2191 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_min_lod
);
2194 /* We should have now consumed exactly all of the arguments */
2195 vtn_assert(idx
== count
);
2197 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2200 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2202 instr
->coord_components
= coord_components
;
2203 instr
->sampler_dim
= sampler_dim
;
2204 instr
->is_array
= is_array
;
2205 instr
->is_shadow
= is_shadow
;
2206 instr
->is_new_style_shadow
=
2207 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2208 instr
->component
= gather_component
;
2210 switch (glsl_get_sampler_result_type(image_type
)) {
2211 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
2212 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
2213 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
2214 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
2216 vtn_fail("Invalid base type for sampler result");
2219 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2220 nir_tex_instr_dest_size(instr
), 32, NULL
);
2222 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2223 nir_tex_instr_dest_size(instr
));
2226 nir_instr
*instruction
;
2227 if (gather_offsets
) {
2228 vtn_assert(glsl_get_base_type(gather_offsets
->type
) == GLSL_TYPE_ARRAY
);
2229 vtn_assert(glsl_get_length(gather_offsets
->type
) == 4);
2230 nir_tex_instr
*instrs
[4] = {instr
, NULL
, NULL
, NULL
};
2232 /* Copy the current instruction 4x */
2233 for (uint32_t i
= 1; i
< 4; i
++) {
2234 instrs
[i
] = nir_tex_instr_create(b
->shader
, instr
->num_srcs
);
2235 instrs
[i
]->op
= instr
->op
;
2236 instrs
[i
]->coord_components
= instr
->coord_components
;
2237 instrs
[i
]->sampler_dim
= instr
->sampler_dim
;
2238 instrs
[i
]->is_array
= instr
->is_array
;
2239 instrs
[i
]->is_shadow
= instr
->is_shadow
;
2240 instrs
[i
]->is_new_style_shadow
= instr
->is_new_style_shadow
;
2241 instrs
[i
]->component
= instr
->component
;
2242 instrs
[i
]->dest_type
= instr
->dest_type
;
2244 memcpy(instrs
[i
]->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2246 nir_ssa_dest_init(&instrs
[i
]->instr
, &instrs
[i
]->dest
,
2247 nir_tex_instr_dest_size(instr
), 32, NULL
);
2250 /* Fill in the last argument with the offset from the passed in offsets
2251 * and insert the instruction into the stream.
2253 for (uint32_t i
= 0; i
< 4; i
++) {
2255 src
.src
= nir_src_for_ssa(gather_offsets
->elems
[i
]->def
);
2256 src
.src_type
= nir_tex_src_offset
;
2257 instrs
[i
]->src
[instrs
[i
]->num_srcs
- 1] = src
;
2258 nir_builder_instr_insert(&b
->nb
, &instrs
[i
]->instr
);
2261 /* Combine the results of the 4 instructions by taking their .w
2264 nir_alu_instr
*vec4
= nir_alu_instr_create(b
->shader
, nir_op_vec4
);
2265 nir_ssa_dest_init(&vec4
->instr
, &vec4
->dest
.dest
, 4, 32, NULL
);
2266 vec4
->dest
.write_mask
= 0xf;
2267 for (uint32_t i
= 0; i
< 4; i
++) {
2268 vec4
->src
[i
].src
= nir_src_for_ssa(&instrs
[i
]->dest
.ssa
);
2269 vec4
->src
[i
].swizzle
[0] = 3;
2271 def
= &vec4
->dest
.dest
.ssa
;
2272 instruction
= &vec4
->instr
;
2274 def
= &instr
->dest
.ssa
;
2275 instruction
= &instr
->instr
;
2278 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
2279 val
->ssa
->def
= def
;
2281 nir_builder_instr_insert(&b
->nb
, instruction
);
2285 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2286 const uint32_t *w
, nir_src
*src
)
2289 case SpvOpAtomicIIncrement
:
2290 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
2293 case SpvOpAtomicIDecrement
:
2294 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
2297 case SpvOpAtomicISub
:
2299 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
2302 case SpvOpAtomicCompareExchange
:
2303 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
2304 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
2307 case SpvOpAtomicExchange
:
2308 case SpvOpAtomicIAdd
:
2309 case SpvOpAtomicSMin
:
2310 case SpvOpAtomicUMin
:
2311 case SpvOpAtomicSMax
:
2312 case SpvOpAtomicUMax
:
2313 case SpvOpAtomicAnd
:
2315 case SpvOpAtomicXor
:
2316 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
2320 vtn_fail("Invalid SPIR-V atomic");
2324 static nir_ssa_def
*
2325 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2327 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
2329 /* The image_load_store intrinsics assume a 4-dim coordinate */
2330 unsigned dim
= glsl_get_vector_elements(coord
->type
);
2331 unsigned swizzle
[4];
2332 for (unsigned i
= 0; i
< 4; i
++)
2333 swizzle
[i
] = MIN2(i
, dim
- 1);
2335 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
2338 static nir_ssa_def
*
2339 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2341 if (value
->num_components
== 4)
2345 for (unsigned i
= 0; i
< 4; i
++)
2346 swiz
[i
] = i
< value
->num_components
? i
: 0;
2347 return nir_swizzle(b
, value
, swiz
, 4, false);
2351 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2352 const uint32_t *w
, unsigned count
)
2354 /* Just get this one out of the way */
2355 if (opcode
== SpvOpImageTexelPointer
) {
2356 struct vtn_value
*val
=
2357 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2358 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2360 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2361 val
->image
->coord
= get_image_coord(b
, w
[4]);
2362 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
2366 struct vtn_image_pointer image
;
2369 case SpvOpAtomicExchange
:
2370 case SpvOpAtomicCompareExchange
:
2371 case SpvOpAtomicCompareExchangeWeak
:
2372 case SpvOpAtomicIIncrement
:
2373 case SpvOpAtomicIDecrement
:
2374 case SpvOpAtomicIAdd
:
2375 case SpvOpAtomicISub
:
2376 case SpvOpAtomicLoad
:
2377 case SpvOpAtomicSMin
:
2378 case SpvOpAtomicUMin
:
2379 case SpvOpAtomicSMax
:
2380 case SpvOpAtomicUMax
:
2381 case SpvOpAtomicAnd
:
2383 case SpvOpAtomicXor
:
2384 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
2387 case SpvOpAtomicStore
:
2388 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
2391 case SpvOpImageQuerySize
:
2392 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2394 image
.sample
= NULL
;
2397 case SpvOpImageRead
:
2398 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2399 image
.coord
= get_image_coord(b
, w
[4]);
2401 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
2402 vtn_assert(w
[5] == SpvImageOperandsSampleMask
);
2403 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
2405 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2409 case SpvOpImageWrite
:
2410 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2411 image
.coord
= get_image_coord(b
, w
[2]);
2415 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
2416 vtn_assert(w
[4] == SpvImageOperandsSampleMask
);
2417 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
2419 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2424 vtn_fail("Invalid image opcode");
2427 nir_intrinsic_op op
;
2429 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
2430 OP(ImageQuerySize
, size
)
2432 OP(ImageWrite
, store
)
2433 OP(AtomicLoad
, load
)
2434 OP(AtomicStore
, store
)
2435 OP(AtomicExchange
, atomic_exchange
)
2436 OP(AtomicCompareExchange
, atomic_comp_swap
)
2437 OP(AtomicIIncrement
, atomic_add
)
2438 OP(AtomicIDecrement
, atomic_add
)
2439 OP(AtomicIAdd
, atomic_add
)
2440 OP(AtomicISub
, atomic_add
)
2441 OP(AtomicSMin
, atomic_min
)
2442 OP(AtomicUMin
, atomic_min
)
2443 OP(AtomicSMax
, atomic_max
)
2444 OP(AtomicUMax
, atomic_max
)
2445 OP(AtomicAnd
, atomic_and
)
2446 OP(AtomicOr
, atomic_or
)
2447 OP(AtomicXor
, atomic_xor
)
2450 vtn_fail("Invalid image opcode");
2453 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
2455 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
2456 intrin
->src
[0] = nir_src_for_ssa(&image_deref
->dest
.ssa
);
2458 /* ImageQuerySize doesn't take any extra parameters */
2459 if (opcode
!= SpvOpImageQuerySize
) {
2460 /* The image coordinate is always 4 components but we may not have that
2461 * many. Swizzle to compensate.
2463 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
2464 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
2468 case SpvOpAtomicLoad
:
2469 case SpvOpImageQuerySize
:
2470 case SpvOpImageRead
:
2472 case SpvOpAtomicStore
:
2473 case SpvOpImageWrite
: {
2474 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
2475 nir_ssa_def
*value
= vtn_ssa_value(b
, value_id
)->def
;
2476 /* nir_intrinsic_image_deref_store always takes a vec4 value */
2477 assert(op
== nir_intrinsic_image_deref_store
);
2478 intrin
->num_components
= 4;
2479 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
));
2483 case SpvOpAtomicCompareExchange
:
2484 case SpvOpAtomicIIncrement
:
2485 case SpvOpAtomicIDecrement
:
2486 case SpvOpAtomicExchange
:
2487 case SpvOpAtomicIAdd
:
2488 case SpvOpAtomicISub
:
2489 case SpvOpAtomicSMin
:
2490 case SpvOpAtomicUMin
:
2491 case SpvOpAtomicSMax
:
2492 case SpvOpAtomicUMax
:
2493 case SpvOpAtomicAnd
:
2495 case SpvOpAtomicXor
:
2496 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
2500 vtn_fail("Invalid image opcode");
2503 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
2504 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2505 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2507 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
2508 intrin
->num_components
= nir_intrinsic_infos
[op
].dest_components
;
2509 if (intrin
->num_components
== 0)
2510 intrin
->num_components
= dest_components
;
2512 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2513 intrin
->num_components
, 32, NULL
);
2515 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2517 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
2518 if (intrin
->num_components
!= dest_components
)
2519 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
2521 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
2522 val
->ssa
->def
= result
;
2524 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2528 static nir_intrinsic_op
2529 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2532 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2533 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2534 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2535 OP(AtomicExchange
, atomic_exchange
)
2536 OP(AtomicCompareExchange
, atomic_comp_swap
)
2537 OP(AtomicIIncrement
, atomic_add
)
2538 OP(AtomicIDecrement
, atomic_add
)
2539 OP(AtomicIAdd
, atomic_add
)
2540 OP(AtomicISub
, atomic_add
)
2541 OP(AtomicSMin
, atomic_imin
)
2542 OP(AtomicUMin
, atomic_umin
)
2543 OP(AtomicSMax
, atomic_imax
)
2544 OP(AtomicUMax
, atomic_umax
)
2545 OP(AtomicAnd
, atomic_and
)
2546 OP(AtomicOr
, atomic_or
)
2547 OP(AtomicXor
, atomic_xor
)
2550 vtn_fail("Invalid SSBO atomic");
2554 static nir_intrinsic_op
2555 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2558 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
2559 OP(AtomicLoad
, read_deref
)
2560 OP(AtomicExchange
, exchange
)
2561 OP(AtomicCompareExchange
, comp_swap
)
2562 OP(AtomicIIncrement
, inc_deref
)
2563 OP(AtomicIDecrement
, post_dec_deref
)
2564 OP(AtomicIAdd
, add_deref
)
2565 OP(AtomicISub
, add_deref
)
2566 OP(AtomicUMin
, min_deref
)
2567 OP(AtomicUMax
, max_deref
)
2568 OP(AtomicAnd
, and_deref
)
2569 OP(AtomicOr
, or_deref
)
2570 OP(AtomicXor
, xor_deref
)
2573 /* We left the following out: AtomicStore, AtomicSMin and
2574 * AtomicSmax. Right now there are not nir intrinsics for them. At this
2575 * moment Atomic Counter support is needed for ARB_spirv support, so is
2576 * only need to support GLSL Atomic Counters that are uints and don't
2577 * allow direct storage.
2579 unreachable("Invalid uniform atomic");
2583 static nir_intrinsic_op
2584 get_shared_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2587 case SpvOpAtomicLoad
: return nir_intrinsic_load_shared
;
2588 case SpvOpAtomicStore
: return nir_intrinsic_store_shared
;
2589 #define OP(S, N) case SpvOp##S: return nir_intrinsic_shared_##N;
2590 OP(AtomicExchange
, atomic_exchange
)
2591 OP(AtomicCompareExchange
, atomic_comp_swap
)
2592 OP(AtomicIIncrement
, atomic_add
)
2593 OP(AtomicIDecrement
, atomic_add
)
2594 OP(AtomicIAdd
, atomic_add
)
2595 OP(AtomicISub
, atomic_add
)
2596 OP(AtomicSMin
, atomic_imin
)
2597 OP(AtomicUMin
, atomic_umin
)
2598 OP(AtomicSMax
, atomic_imax
)
2599 OP(AtomicUMax
, atomic_umax
)
2600 OP(AtomicAnd
, atomic_and
)
2601 OP(AtomicOr
, atomic_or
)
2602 OP(AtomicXor
, atomic_xor
)
2605 vtn_fail("Invalid shared atomic");
2609 static nir_intrinsic_op
2610 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2613 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
2614 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
2615 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
2616 OP(AtomicExchange
, atomic_exchange
)
2617 OP(AtomicCompareExchange
, atomic_comp_swap
)
2618 OP(AtomicIIncrement
, atomic_add
)
2619 OP(AtomicIDecrement
, atomic_add
)
2620 OP(AtomicIAdd
, atomic_add
)
2621 OP(AtomicISub
, atomic_add
)
2622 OP(AtomicSMin
, atomic_imin
)
2623 OP(AtomicUMin
, atomic_umin
)
2624 OP(AtomicSMax
, atomic_imax
)
2625 OP(AtomicUMax
, atomic_umax
)
2626 OP(AtomicAnd
, atomic_and
)
2627 OP(AtomicOr
, atomic_or
)
2628 OP(AtomicXor
, atomic_xor
)
2631 vtn_fail("Invalid shared atomic");
2636 * Handles shared atomics, ssbo atomics and atomic counters.
2639 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
2640 const uint32_t *w
, unsigned count
)
2642 struct vtn_pointer
*ptr
;
2643 nir_intrinsic_instr
*atomic
;
2646 case SpvOpAtomicLoad
:
2647 case SpvOpAtomicExchange
:
2648 case SpvOpAtomicCompareExchange
:
2649 case SpvOpAtomicCompareExchangeWeak
:
2650 case SpvOpAtomicIIncrement
:
2651 case SpvOpAtomicIDecrement
:
2652 case SpvOpAtomicIAdd
:
2653 case SpvOpAtomicISub
:
2654 case SpvOpAtomicSMin
:
2655 case SpvOpAtomicUMin
:
2656 case SpvOpAtomicSMax
:
2657 case SpvOpAtomicUMax
:
2658 case SpvOpAtomicAnd
:
2660 case SpvOpAtomicXor
:
2661 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2664 case SpvOpAtomicStore
:
2665 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2669 vtn_fail("Invalid SPIR-V atomic");
2673 SpvScope scope = w[4];
2674 SpvMemorySemanticsMask semantics = w[5];
2677 /* uniform as "atomic counter uniform" */
2678 if (ptr
->mode
== vtn_variable_mode_uniform
) {
2679 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2680 const struct glsl_type
*deref_type
= deref
->type
;
2681 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
2682 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2683 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2685 /* SSBO needs to initialize index/offset. In this case we don't need to,
2686 * as that info is already stored on the ptr->var->var nir_variable (see
2687 * vtn_create_variable)
2691 case SpvOpAtomicLoad
:
2692 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2695 case SpvOpAtomicStore
:
2696 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2697 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2700 case SpvOpAtomicExchange
:
2701 case SpvOpAtomicCompareExchange
:
2702 case SpvOpAtomicCompareExchangeWeak
:
2703 case SpvOpAtomicIIncrement
:
2704 case SpvOpAtomicIDecrement
:
2705 case SpvOpAtomicIAdd
:
2706 case SpvOpAtomicISub
:
2707 case SpvOpAtomicSMin
:
2708 case SpvOpAtomicUMin
:
2709 case SpvOpAtomicSMax
:
2710 case SpvOpAtomicUMax
:
2711 case SpvOpAtomicAnd
:
2713 case SpvOpAtomicXor
:
2714 /* Nothing: we don't need to call fill_common_atomic_sources here, as
2715 * atomic counter uniforms doesn't have sources
2720 unreachable("Invalid SPIR-V atomic");
2723 } else if (ptr
->mode
== vtn_variable_mode_workgroup
&&
2724 !b
->options
->lower_workgroup_access_to_offsets
) {
2725 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2726 const struct glsl_type
*deref_type
= deref
->type
;
2727 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
2728 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2729 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2732 case SpvOpAtomicLoad
:
2733 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2736 case SpvOpAtomicStore
:
2737 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2738 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2739 atomic
->src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2742 case SpvOpAtomicExchange
:
2743 case SpvOpAtomicCompareExchange
:
2744 case SpvOpAtomicCompareExchangeWeak
:
2745 case SpvOpAtomicIIncrement
:
2746 case SpvOpAtomicIDecrement
:
2747 case SpvOpAtomicIAdd
:
2748 case SpvOpAtomicISub
:
2749 case SpvOpAtomicSMin
:
2750 case SpvOpAtomicUMin
:
2751 case SpvOpAtomicSMax
:
2752 case SpvOpAtomicUMax
:
2753 case SpvOpAtomicAnd
:
2755 case SpvOpAtomicXor
:
2756 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
2760 vtn_fail("Invalid SPIR-V atomic");
2764 nir_ssa_def
*offset
, *index
;
2765 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
2767 nir_intrinsic_op op
;
2768 if (ptr
->mode
== vtn_variable_mode_ssbo
) {
2769 op
= get_ssbo_nir_atomic_op(b
, opcode
);
2771 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
&&
2772 b
->options
->lower_workgroup_access_to_offsets
);
2773 op
= get_shared_nir_atomic_op(b
, opcode
);
2776 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2780 case SpvOpAtomicLoad
:
2781 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2782 nir_intrinsic_set_align(atomic
, 4, 0);
2783 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2784 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2785 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2788 case SpvOpAtomicStore
:
2789 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2790 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2791 nir_intrinsic_set_align(atomic
, 4, 0);
2792 atomic
->src
[src
++] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2793 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2794 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2795 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2798 case SpvOpAtomicExchange
:
2799 case SpvOpAtomicCompareExchange
:
2800 case SpvOpAtomicCompareExchangeWeak
:
2801 case SpvOpAtomicIIncrement
:
2802 case SpvOpAtomicIDecrement
:
2803 case SpvOpAtomicIAdd
:
2804 case SpvOpAtomicISub
:
2805 case SpvOpAtomicSMin
:
2806 case SpvOpAtomicUMin
:
2807 case SpvOpAtomicSMax
:
2808 case SpvOpAtomicUMax
:
2809 case SpvOpAtomicAnd
:
2811 case SpvOpAtomicXor
:
2812 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2813 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2814 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2815 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
2819 vtn_fail("Invalid SPIR-V atomic");
2823 if (opcode
!= SpvOpAtomicStore
) {
2824 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2826 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
2827 glsl_get_vector_elements(type
->type
),
2828 glsl_get_bit_size(type
->type
), NULL
);
2830 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2831 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
2832 val
->ssa
->def
= &atomic
->dest
.ssa
;
2833 val
->ssa
->type
= type
->type
;
2836 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
2839 static nir_alu_instr
*
2840 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
2843 switch (num_components
) {
2844 case 1: op
= nir_op_imov
; break;
2845 case 2: op
= nir_op_vec2
; break;
2846 case 3: op
= nir_op_vec3
; break;
2847 case 4: op
= nir_op_vec4
; break;
2848 default: vtn_fail("bad vector size");
2851 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
2852 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
2854 vec
->dest
.write_mask
= (1 << num_components
) - 1;
2859 struct vtn_ssa_value
*
2860 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
2862 if (src
->transposed
)
2863 return src
->transposed
;
2865 struct vtn_ssa_value
*dest
=
2866 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
2868 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
2869 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
2870 glsl_get_bit_size(src
->type
));
2871 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2872 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
2873 vec
->src
[0].swizzle
[0] = i
;
2875 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
2876 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
2877 vec
->src
[j
].swizzle
[0] = i
;
2880 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2881 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
2884 dest
->transposed
= src
;
2890 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
2892 return nir_channel(&b
->nb
, src
, index
);
2896 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
2899 nir_alu_instr
*vec
= create_vec(b
, src
->num_components
,
2902 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
2904 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
2906 vec
->src
[i
].src
= nir_src_for_ssa(src
);
2907 vec
->src
[i
].swizzle
[0] = i
;
2911 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2913 return &vec
->dest
.dest
.ssa
;
2917 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2920 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
2921 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2922 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2923 vtn_vector_extract(b
, src
, i
), dest
);
2929 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2930 nir_ssa_def
*insert
, nir_ssa_def
*index
)
2932 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
2933 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2934 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2935 vtn_vector_insert(b
, src
, insert
, i
), dest
);
2940 static nir_ssa_def
*
2941 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
2942 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
2943 const uint32_t *indices
)
2945 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
2947 for (unsigned i
= 0; i
< num_components
; i
++) {
2948 uint32_t index
= indices
[i
];
2949 if (index
== 0xffffffff) {
2951 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
2952 } else if (index
< src0
->num_components
) {
2953 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
2954 vec
->src
[i
].swizzle
[0] = index
;
2956 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
2957 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
2961 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2963 return &vec
->dest
.dest
.ssa
;
2967 * Concatentates a number of vectors/scalars together to produce a vector
2969 static nir_ssa_def
*
2970 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
2971 unsigned num_srcs
, nir_ssa_def
**srcs
)
2973 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
2975 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2977 * "When constructing a vector, there must be at least two Constituent
2980 vtn_assert(num_srcs
>= 2);
2982 unsigned dest_idx
= 0;
2983 for (unsigned i
= 0; i
< num_srcs
; i
++) {
2984 nir_ssa_def
*src
= srcs
[i
];
2985 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
2986 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
2987 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
2988 vec
->src
[dest_idx
].swizzle
[0] = j
;
2993 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2995 * "When constructing a vector, the total number of components in all
2996 * the operands must equal the number of components in Result Type."
2998 vtn_assert(dest_idx
== num_components
);
3000 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3002 return &vec
->dest
.dest
.ssa
;
3005 static struct vtn_ssa_value
*
3006 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3008 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3009 dest
->type
= src
->type
;
3011 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3012 dest
->def
= src
->def
;
3014 unsigned elems
= glsl_get_length(src
->type
);
3016 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3017 for (unsigned i
= 0; i
< elems
; i
++)
3018 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3024 static struct vtn_ssa_value
*
3025 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3026 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3027 unsigned num_indices
)
3029 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3031 struct vtn_ssa_value
*cur
= dest
;
3033 for (i
= 0; i
< num_indices
- 1; i
++) {
3034 cur
= cur
->elems
[indices
[i
]];
3037 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3038 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3039 * the component granularity. In that case, the last index will be
3040 * the index to insert the scalar into the vector.
3043 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
3045 cur
->elems
[indices
[i
]] = insert
;
3051 static struct vtn_ssa_value
*
3052 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3053 const uint32_t *indices
, unsigned num_indices
)
3055 struct vtn_ssa_value
*cur
= src
;
3056 for (unsigned i
= 0; i
< num_indices
; i
++) {
3057 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3058 vtn_assert(i
== num_indices
- 1);
3059 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3060 * the component granularity. The last index will be the index of the
3061 * vector to extract.
3064 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
3065 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
3066 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
3069 cur
= cur
->elems
[indices
[i
]];
3077 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3078 const uint32_t *w
, unsigned count
)
3080 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
3081 const struct glsl_type
*type
=
3082 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
3083 val
->ssa
= vtn_create_ssa_value(b
, type
);
3086 case SpvOpVectorExtractDynamic
:
3087 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3088 vtn_ssa_value(b
, w
[4])->def
);
3091 case SpvOpVectorInsertDynamic
:
3092 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3093 vtn_ssa_value(b
, w
[4])->def
,
3094 vtn_ssa_value(b
, w
[5])->def
);
3097 case SpvOpVectorShuffle
:
3098 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
3099 vtn_ssa_value(b
, w
[3])->def
,
3100 vtn_ssa_value(b
, w
[4])->def
,
3104 case SpvOpCompositeConstruct
: {
3105 unsigned elems
= count
- 3;
3107 if (glsl_type_is_vector_or_scalar(type
)) {
3108 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3109 for (unsigned i
= 0; i
< elems
; i
++)
3110 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
3112 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
3115 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3116 for (unsigned i
= 0; i
< elems
; i
++)
3117 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3121 case SpvOpCompositeExtract
:
3122 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3126 case SpvOpCompositeInsert
:
3127 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3128 vtn_ssa_value(b
, w
[3]),
3132 case SpvOpCopyObject
:
3133 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3137 vtn_fail("unknown composite operation");
3142 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3144 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3145 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3149 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3150 SpvMemorySemanticsMask semantics
)
3152 static const SpvMemorySemanticsMask all_memory_semantics
=
3153 SpvMemorySemanticsUniformMemoryMask
|
3154 SpvMemorySemanticsWorkgroupMemoryMask
|
3155 SpvMemorySemanticsAtomicCounterMemoryMask
|
3156 SpvMemorySemanticsImageMemoryMask
;
3158 /* If we're not actually doing a memory barrier, bail */
3159 if (!(semantics
& all_memory_semantics
))
3162 /* GL and Vulkan don't have these */
3163 vtn_assert(scope
!= SpvScopeCrossDevice
);
3165 if (scope
== SpvScopeSubgroup
)
3166 return; /* Nothing to do here */
3168 if (scope
== SpvScopeWorkgroup
) {
3169 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3173 /* There's only two scopes thing left */
3174 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3176 if ((semantics
& all_memory_semantics
) == all_memory_semantics
) {
3177 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3181 /* Issue a bunch of more specific barriers */
3182 uint32_t bits
= semantics
;
3184 SpvMemorySemanticsMask semantic
= 1 << u_bit_scan(&bits
);
3186 case SpvMemorySemanticsUniformMemoryMask
:
3187 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3189 case SpvMemorySemanticsWorkgroupMemoryMask
:
3190 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3192 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3193 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3195 case SpvMemorySemanticsImageMemoryMask
:
3196 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3205 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3206 const uint32_t *w
, unsigned count
)
3209 case SpvOpEmitVertex
:
3210 case SpvOpEmitStreamVertex
:
3211 case SpvOpEndPrimitive
:
3212 case SpvOpEndStreamPrimitive
: {
3213 nir_intrinsic_op intrinsic_op
;
3215 case SpvOpEmitVertex
:
3216 case SpvOpEmitStreamVertex
:
3217 intrinsic_op
= nir_intrinsic_emit_vertex
;
3219 case SpvOpEndPrimitive
:
3220 case SpvOpEndStreamPrimitive
:
3221 intrinsic_op
= nir_intrinsic_end_primitive
;
3224 unreachable("Invalid opcode");
3227 nir_intrinsic_instr
*intrin
=
3228 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3231 case SpvOpEmitStreamVertex
:
3232 case SpvOpEndStreamPrimitive
: {
3233 unsigned stream
= vtn_constant_value(b
, w
[1])->values
[0].u32
[0];
3234 nir_intrinsic_set_stream_id(intrin
, stream
);
3242 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3246 case SpvOpMemoryBarrier
: {
3247 SpvScope scope
= vtn_constant_value(b
, w
[1])->values
[0].u32
[0];
3248 SpvMemorySemanticsMask semantics
=
3249 vtn_constant_value(b
, w
[2])->values
[0].u32
[0];
3250 vtn_emit_memory_barrier(b
, scope
, semantics
);
3254 case SpvOpControlBarrier
: {
3255 SpvScope execution_scope
=
3256 vtn_constant_value(b
, w
[1])->values
[0].u32
[0];
3257 if (execution_scope
== SpvScopeWorkgroup
)
3258 vtn_emit_barrier(b
, nir_intrinsic_barrier
);
3260 SpvScope memory_scope
=
3261 vtn_constant_value(b
, w
[2])->values
[0].u32
[0];
3262 SpvMemorySemanticsMask memory_semantics
=
3263 vtn_constant_value(b
, w
[3])->values
[0].u32
[0];
3264 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
3269 unreachable("unknown barrier instruction");
3274 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
3275 SpvExecutionMode mode
)
3278 case SpvExecutionModeInputPoints
:
3279 case SpvExecutionModeOutputPoints
:
3280 return 0; /* GL_POINTS */
3281 case SpvExecutionModeInputLines
:
3282 return 1; /* GL_LINES */
3283 case SpvExecutionModeInputLinesAdjacency
:
3284 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3285 case SpvExecutionModeTriangles
:
3286 return 4; /* GL_TRIANGLES */
3287 case SpvExecutionModeInputTrianglesAdjacency
:
3288 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3289 case SpvExecutionModeQuads
:
3290 return 7; /* GL_QUADS */
3291 case SpvExecutionModeIsolines
:
3292 return 0x8E7A; /* GL_ISOLINES */
3293 case SpvExecutionModeOutputLineStrip
:
3294 return 3; /* GL_LINE_STRIP */
3295 case SpvExecutionModeOutputTriangleStrip
:
3296 return 5; /* GL_TRIANGLE_STRIP */
3298 vtn_fail("Invalid primitive type");
3303 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
3304 SpvExecutionMode mode
)
3307 case SpvExecutionModeInputPoints
:
3309 case SpvExecutionModeInputLines
:
3311 case SpvExecutionModeInputLinesAdjacency
:
3313 case SpvExecutionModeTriangles
:
3315 case SpvExecutionModeInputTrianglesAdjacency
:
3318 vtn_fail("Invalid GS input mode");
3322 static gl_shader_stage
3323 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
3326 case SpvExecutionModelVertex
:
3327 return MESA_SHADER_VERTEX
;
3328 case SpvExecutionModelTessellationControl
:
3329 return MESA_SHADER_TESS_CTRL
;
3330 case SpvExecutionModelTessellationEvaluation
:
3331 return MESA_SHADER_TESS_EVAL
;
3332 case SpvExecutionModelGeometry
:
3333 return MESA_SHADER_GEOMETRY
;
3334 case SpvExecutionModelFragment
:
3335 return MESA_SHADER_FRAGMENT
;
3336 case SpvExecutionModelGLCompute
:
3337 return MESA_SHADER_COMPUTE
;
3339 vtn_fail("Unsupported execution model");
3343 #define spv_check_supported(name, cap) do { \
3344 if (!(b->options && b->options->caps.name)) \
3345 vtn_warn("Unsupported SPIR-V capability: %s", \
3346 spirv_capability_to_string(cap)); \
3351 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
3354 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
3355 /* Let this be a name label regardless */
3356 unsigned name_words
;
3357 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
3359 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
3360 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
3363 vtn_assert(b
->entry_point
== NULL
);
3364 b
->entry_point
= entry_point
;
3368 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3369 const uint32_t *w
, unsigned count
)
3376 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
3377 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
3378 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
3379 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
3380 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
3381 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
3384 uint32_t version
= w
[2];
3387 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
3389 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
3393 case SpvOpSourceExtension
:
3394 case SpvOpSourceContinued
:
3395 case SpvOpExtension
:
3396 case SpvOpModuleProcessed
:
3397 /* Unhandled, but these are for debug so that's ok. */
3400 case SpvOpCapability
: {
3401 SpvCapability cap
= w
[1];
3403 case SpvCapabilityMatrix
:
3404 case SpvCapabilityShader
:
3405 case SpvCapabilityGeometry
:
3406 case SpvCapabilityGeometryPointSize
:
3407 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
3408 case SpvCapabilitySampledImageArrayDynamicIndexing
:
3409 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
3410 case SpvCapabilityStorageImageArrayDynamicIndexing
:
3411 case SpvCapabilityImageRect
:
3412 case SpvCapabilitySampledRect
:
3413 case SpvCapabilitySampled1D
:
3414 case SpvCapabilityImage1D
:
3415 case SpvCapabilitySampledCubeArray
:
3416 case SpvCapabilityImageCubeArray
:
3417 case SpvCapabilitySampledBuffer
:
3418 case SpvCapabilityImageBuffer
:
3419 case SpvCapabilityImageQuery
:
3420 case SpvCapabilityDerivativeControl
:
3421 case SpvCapabilityInterpolationFunction
:
3422 case SpvCapabilityMultiViewport
:
3423 case SpvCapabilitySampleRateShading
:
3424 case SpvCapabilityClipDistance
:
3425 case SpvCapabilityCullDistance
:
3426 case SpvCapabilityInputAttachment
:
3427 case SpvCapabilityImageGatherExtended
:
3428 case SpvCapabilityStorageImageExtendedFormats
:
3431 case SpvCapabilityLinkage
:
3432 case SpvCapabilityVector16
:
3433 case SpvCapabilityFloat16Buffer
:
3434 case SpvCapabilityFloat16
:
3435 case SpvCapabilityStorageImageMultisample
:
3436 case SpvCapabilityInt8
:
3437 case SpvCapabilitySparseResidency
:
3438 vtn_warn("Unsupported SPIR-V capability: %s",
3439 spirv_capability_to_string(cap
));
3442 case SpvCapabilityMinLod
:
3443 spv_check_supported(min_lod
, cap
);
3446 case SpvCapabilityAtomicStorage
:
3447 spv_check_supported(atomic_storage
, cap
);
3450 case SpvCapabilityFloat64
:
3451 spv_check_supported(float64
, cap
);
3453 case SpvCapabilityInt64
:
3454 spv_check_supported(int64
, cap
);
3456 case SpvCapabilityInt16
:
3457 spv_check_supported(int16
, cap
);
3460 case SpvCapabilityTransformFeedback
:
3461 spv_check_supported(transform_feedback
, cap
);
3464 case SpvCapabilityGeometryStreams
:
3465 spv_check_supported(geometry_streams
, cap
);
3468 case SpvCapabilityInt64Atomics
:
3469 spv_check_supported(int64_atomics
, cap
);
3472 case SpvCapabilityAddresses
:
3473 case SpvCapabilityKernel
:
3474 case SpvCapabilityImageBasic
:
3475 case SpvCapabilityImageReadWrite
:
3476 case SpvCapabilityImageMipmap
:
3477 case SpvCapabilityPipes
:
3478 case SpvCapabilityGroups
:
3479 case SpvCapabilityDeviceEnqueue
:
3480 case SpvCapabilityLiteralSampler
:
3481 case SpvCapabilityGenericPointer
:
3482 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
3483 spirv_capability_to_string(cap
));
3486 case SpvCapabilityImageMSArray
:
3487 spv_check_supported(image_ms_array
, cap
);
3490 case SpvCapabilityTessellation
:
3491 case SpvCapabilityTessellationPointSize
:
3492 spv_check_supported(tessellation
, cap
);
3495 case SpvCapabilityDrawParameters
:
3496 spv_check_supported(draw_parameters
, cap
);
3499 case SpvCapabilityStorageImageReadWithoutFormat
:
3500 spv_check_supported(image_read_without_format
, cap
);
3503 case SpvCapabilityStorageImageWriteWithoutFormat
:
3504 spv_check_supported(image_write_without_format
, cap
);
3507 case SpvCapabilityDeviceGroup
:
3508 spv_check_supported(device_group
, cap
);
3511 case SpvCapabilityMultiView
:
3512 spv_check_supported(multiview
, cap
);
3515 case SpvCapabilityGroupNonUniform
:
3516 spv_check_supported(subgroup_basic
, cap
);
3519 case SpvCapabilityGroupNonUniformVote
:
3520 spv_check_supported(subgroup_vote
, cap
);
3523 case SpvCapabilitySubgroupBallotKHR
:
3524 case SpvCapabilityGroupNonUniformBallot
:
3525 spv_check_supported(subgroup_ballot
, cap
);
3528 case SpvCapabilityGroupNonUniformShuffle
:
3529 case SpvCapabilityGroupNonUniformShuffleRelative
:
3530 spv_check_supported(subgroup_shuffle
, cap
);
3533 case SpvCapabilityGroupNonUniformQuad
:
3534 spv_check_supported(subgroup_quad
, cap
);
3537 case SpvCapabilityGroupNonUniformArithmetic
:
3538 case SpvCapabilityGroupNonUniformClustered
:
3539 spv_check_supported(subgroup_arithmetic
, cap
);
3542 case SpvCapabilityVariablePointersStorageBuffer
:
3543 case SpvCapabilityVariablePointers
:
3544 spv_check_supported(variable_pointers
, cap
);
3547 case SpvCapabilityStorageUniformBufferBlock16
:
3548 case SpvCapabilityStorageUniform16
:
3549 case SpvCapabilityStoragePushConstant16
:
3550 case SpvCapabilityStorageInputOutput16
:
3551 spv_check_supported(storage_16bit
, cap
);
3554 case SpvCapabilityShaderViewportIndexLayerEXT
:
3555 spv_check_supported(shader_viewport_index_layer
, cap
);
3558 case SpvCapabilityStorageBuffer8BitAccess
:
3559 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
3560 case SpvCapabilityStoragePushConstant8
:
3561 spv_check_supported(storage_8bit
, cap
);
3564 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
3565 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
3566 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
3567 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
3570 case SpvCapabilityRuntimeDescriptorArrayEXT
:
3571 spv_check_supported(runtime_descriptor_array
, cap
);
3574 case SpvCapabilityStencilExportEXT
:
3575 spv_check_supported(stencil_export
, cap
);
3578 case SpvCapabilitySampleMaskPostDepthCoverage
:
3579 spv_check_supported(post_depth_coverage
, cap
);
3583 vtn_fail("Unhandled capability");
3588 case SpvOpExtInstImport
:
3589 vtn_handle_extension(b
, opcode
, w
, count
);
3592 case SpvOpMemoryModel
:
3593 vtn_assert(w
[1] == SpvAddressingModelLogical
);
3594 vtn_assert(w
[2] == SpvMemoryModelSimple
||
3595 w
[2] == SpvMemoryModelGLSL450
);
3598 case SpvOpEntryPoint
:
3599 vtn_handle_entry_point(b
, w
, count
);
3603 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
3604 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3608 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3611 case SpvOpMemberName
:
3615 case SpvOpExecutionMode
:
3616 case SpvOpDecorationGroup
:
3618 case SpvOpMemberDecorate
:
3619 case SpvOpGroupDecorate
:
3620 case SpvOpGroupMemberDecorate
:
3621 case SpvOpDecorateStringGOOGLE
:
3622 case SpvOpMemberDecorateStringGOOGLE
:
3623 vtn_handle_decoration(b
, opcode
, w
, count
);
3627 return false; /* End of preamble */
3634 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
3635 const struct vtn_decoration
*mode
, void *data
)
3637 vtn_assert(b
->entry_point
== entry_point
);
3639 switch(mode
->exec_mode
) {
3640 case SpvExecutionModeOriginUpperLeft
:
3641 case SpvExecutionModeOriginLowerLeft
:
3642 b
->origin_upper_left
=
3643 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
3646 case SpvExecutionModeEarlyFragmentTests
:
3647 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3648 b
->shader
->info
.fs
.early_fragment_tests
= true;
3651 case SpvExecutionModePostDepthCoverage
:
3652 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3653 b
->shader
->info
.fs
.post_depth_coverage
= true;
3656 case SpvExecutionModeInvocations
:
3657 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3658 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->literals
[0]);
3661 case SpvExecutionModeDepthReplacing
:
3662 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3663 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
3665 case SpvExecutionModeDepthGreater
:
3666 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3667 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
3669 case SpvExecutionModeDepthLess
:
3670 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3671 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
3673 case SpvExecutionModeDepthUnchanged
:
3674 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3675 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
3678 case SpvExecutionModeLocalSize
:
3679 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
3680 b
->shader
->info
.cs
.local_size
[0] = mode
->literals
[0];
3681 b
->shader
->info
.cs
.local_size
[1] = mode
->literals
[1];
3682 b
->shader
->info
.cs
.local_size
[2] = mode
->literals
[2];
3684 case SpvExecutionModeLocalSizeHint
:
3685 break; /* Nothing to do with this */
3687 case SpvExecutionModeOutputVertices
:
3688 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3689 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3690 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->literals
[0];
3692 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3693 b
->shader
->info
.gs
.vertices_out
= mode
->literals
[0];
3697 case SpvExecutionModeInputPoints
:
3698 case SpvExecutionModeInputLines
:
3699 case SpvExecutionModeInputLinesAdjacency
:
3700 case SpvExecutionModeTriangles
:
3701 case SpvExecutionModeInputTrianglesAdjacency
:
3702 case SpvExecutionModeQuads
:
3703 case SpvExecutionModeIsolines
:
3704 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3705 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3706 b
->shader
->info
.tess
.primitive_mode
=
3707 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3709 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3710 b
->shader
->info
.gs
.vertices_in
=
3711 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
3712 b
->shader
->info
.gs
.input_primitive
=
3713 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3717 case SpvExecutionModeOutputPoints
:
3718 case SpvExecutionModeOutputLineStrip
:
3719 case SpvExecutionModeOutputTriangleStrip
:
3720 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3721 b
->shader
->info
.gs
.output_primitive
=
3722 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3725 case SpvExecutionModeSpacingEqual
:
3726 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3727 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3728 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
3730 case SpvExecutionModeSpacingFractionalEven
:
3731 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3732 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3733 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
3735 case SpvExecutionModeSpacingFractionalOdd
:
3736 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3737 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3738 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
3740 case SpvExecutionModeVertexOrderCw
:
3741 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3742 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3743 b
->shader
->info
.tess
.ccw
= false;
3745 case SpvExecutionModeVertexOrderCcw
:
3746 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3747 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3748 b
->shader
->info
.tess
.ccw
= true;
3750 case SpvExecutionModePointMode
:
3751 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3752 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3753 b
->shader
->info
.tess
.point_mode
= true;
3756 case SpvExecutionModePixelCenterInteger
:
3757 b
->pixel_center_integer
= true;
3760 case SpvExecutionModeXfb
:
3761 b
->shader
->info
.has_transform_feedback_varyings
= true;
3764 case SpvExecutionModeVecTypeHint
:
3765 case SpvExecutionModeContractionOff
:
3768 case SpvExecutionModeStencilRefReplacingEXT
:
3769 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3773 vtn_fail("Unhandled execution mode");
3778 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3779 const uint32_t *w
, unsigned count
)
3781 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
3785 case SpvOpSourceContinued
:
3786 case SpvOpSourceExtension
:
3787 case SpvOpExtension
:
3788 case SpvOpCapability
:
3789 case SpvOpExtInstImport
:
3790 case SpvOpMemoryModel
:
3791 case SpvOpEntryPoint
:
3792 case SpvOpExecutionMode
:
3795 case SpvOpMemberName
:
3796 case SpvOpDecorationGroup
:
3798 case SpvOpMemberDecorate
:
3799 case SpvOpGroupDecorate
:
3800 case SpvOpGroupMemberDecorate
:
3801 case SpvOpDecorateStringGOOGLE
:
3802 case SpvOpMemberDecorateStringGOOGLE
:
3803 vtn_fail("Invalid opcode types and variables section");
3809 case SpvOpTypeFloat
:
3810 case SpvOpTypeVector
:
3811 case SpvOpTypeMatrix
:
3812 case SpvOpTypeImage
:
3813 case SpvOpTypeSampler
:
3814 case SpvOpTypeSampledImage
:
3815 case SpvOpTypeArray
:
3816 case SpvOpTypeRuntimeArray
:
3817 case SpvOpTypeStruct
:
3818 case SpvOpTypeOpaque
:
3819 case SpvOpTypePointer
:
3820 case SpvOpTypeFunction
:
3821 case SpvOpTypeEvent
:
3822 case SpvOpTypeDeviceEvent
:
3823 case SpvOpTypeReserveId
:
3824 case SpvOpTypeQueue
:
3826 vtn_handle_type(b
, opcode
, w
, count
);
3829 case SpvOpConstantTrue
:
3830 case SpvOpConstantFalse
:
3832 case SpvOpConstantComposite
:
3833 case SpvOpConstantSampler
:
3834 case SpvOpConstantNull
:
3835 case SpvOpSpecConstantTrue
:
3836 case SpvOpSpecConstantFalse
:
3837 case SpvOpSpecConstant
:
3838 case SpvOpSpecConstantComposite
:
3839 case SpvOpSpecConstantOp
:
3840 vtn_handle_constant(b
, opcode
, w
, count
);
3845 vtn_handle_variables(b
, opcode
, w
, count
);
3849 return false; /* End of preamble */
3856 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3857 const uint32_t *w
, unsigned count
)
3863 case SpvOpLoopMerge
:
3864 case SpvOpSelectionMerge
:
3865 /* This is handled by cfg pre-pass and walk_blocks */
3869 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
3870 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3875 vtn_handle_extension(b
, opcode
, w
, count
);
3881 case SpvOpCopyMemory
:
3882 case SpvOpCopyMemorySized
:
3883 case SpvOpAccessChain
:
3884 case SpvOpPtrAccessChain
:
3885 case SpvOpInBoundsAccessChain
:
3886 case SpvOpArrayLength
:
3887 vtn_handle_variables(b
, opcode
, w
, count
);
3890 case SpvOpFunctionCall
:
3891 vtn_handle_function_call(b
, opcode
, w
, count
);
3894 case SpvOpSampledImage
:
3896 case SpvOpImageSampleImplicitLod
:
3897 case SpvOpImageSampleExplicitLod
:
3898 case SpvOpImageSampleDrefImplicitLod
:
3899 case SpvOpImageSampleDrefExplicitLod
:
3900 case SpvOpImageSampleProjImplicitLod
:
3901 case SpvOpImageSampleProjExplicitLod
:
3902 case SpvOpImageSampleProjDrefImplicitLod
:
3903 case SpvOpImageSampleProjDrefExplicitLod
:
3904 case SpvOpImageFetch
:
3905 case SpvOpImageGather
:
3906 case SpvOpImageDrefGather
:
3907 case SpvOpImageQuerySizeLod
:
3908 case SpvOpImageQueryLod
:
3909 case SpvOpImageQueryLevels
:
3910 case SpvOpImageQuerySamples
:
3911 vtn_handle_texture(b
, opcode
, w
, count
);
3914 case SpvOpImageRead
:
3915 case SpvOpImageWrite
:
3916 case SpvOpImageTexelPointer
:
3917 vtn_handle_image(b
, opcode
, w
, count
);
3920 case SpvOpImageQuerySize
: {
3921 struct vtn_pointer
*image
=
3922 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
3923 if (glsl_type_is_image(image
->type
->type
)) {
3924 vtn_handle_image(b
, opcode
, w
, count
);
3926 vtn_assert(glsl_type_is_sampler(image
->type
->type
));
3927 vtn_handle_texture(b
, opcode
, w
, count
);
3932 case SpvOpAtomicLoad
:
3933 case SpvOpAtomicExchange
:
3934 case SpvOpAtomicCompareExchange
:
3935 case SpvOpAtomicCompareExchangeWeak
:
3936 case SpvOpAtomicIIncrement
:
3937 case SpvOpAtomicIDecrement
:
3938 case SpvOpAtomicIAdd
:
3939 case SpvOpAtomicISub
:
3940 case SpvOpAtomicSMin
:
3941 case SpvOpAtomicUMin
:
3942 case SpvOpAtomicSMax
:
3943 case SpvOpAtomicUMax
:
3944 case SpvOpAtomicAnd
:
3946 case SpvOpAtomicXor
: {
3947 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
3948 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
3949 vtn_handle_image(b
, opcode
, w
, count
);
3951 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
3952 vtn_handle_atomics(b
, opcode
, w
, count
);
3957 case SpvOpAtomicStore
: {
3958 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
3959 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
3960 vtn_handle_image(b
, opcode
, w
, count
);
3962 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
3963 vtn_handle_atomics(b
, opcode
, w
, count
);
3969 /* Handle OpSelect up-front here because it needs to be able to handle
3970 * pointers and not just regular vectors and scalars.
3972 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
3973 struct vtn_value
*sel_val
= vtn_untyped_value(b
, w
[3]);
3974 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
3975 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
3977 const struct glsl_type
*sel_type
;
3978 switch (res_val
->type
->base_type
) {
3979 case vtn_base_type_scalar
:
3980 sel_type
= glsl_bool_type();
3982 case vtn_base_type_vector
:
3983 sel_type
= glsl_vector_type(GLSL_TYPE_BOOL
, res_val
->type
->length
);
3985 case vtn_base_type_pointer
:
3986 /* We need to have actual storage for pointer types */
3987 vtn_fail_if(res_val
->type
->type
== NULL
,
3988 "Invalid pointer result type for OpSelect");
3989 sel_type
= glsl_bool_type();
3992 vtn_fail("Result type of OpSelect must be a scalar, vector, or pointer");
3995 if (unlikely(sel_val
->type
->type
!= sel_type
)) {
3996 if (sel_val
->type
->type
== glsl_bool_type()) {
3997 /* This case is illegal but some older versions of GLSLang produce
3998 * it. The GLSLang issue was fixed on March 30, 2017:
4000 * https://github.com/KhronosGroup/glslang/issues/809
4002 * Unfortunately, there are applications in the wild which are
4003 * shipping with this bug so it isn't nice to fail on them so we
4004 * throw a warning instead. It's not actually a problem for us as
4005 * nir_builder will just splat the condition out which is most
4006 * likely what the client wanted anyway.
4008 vtn_warn("Condition type of OpSelect must have the same number "
4009 "of components as Result Type");
4011 vtn_fail("Condition type of OpSelect must be a scalar or vector "
4012 "of Boolean type. It must have the same number of "
4013 "components as Result Type");
4017 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4018 obj2_val
->type
!= res_val
->type
,
4019 "Object types must match the result type in OpSelect");
4021 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4022 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, res_type
->type
);
4023 ssa
->def
= nir_bcsel(&b
->nb
, vtn_ssa_value(b
, w
[3])->def
,
4024 vtn_ssa_value(b
, w
[4])->def
,
4025 vtn_ssa_value(b
, w
[5])->def
);
4026 vtn_push_ssa(b
, w
[2], res_type
, ssa
);
4035 case SpvOpConvertFToU
:
4036 case SpvOpConvertFToS
:
4037 case SpvOpConvertSToF
:
4038 case SpvOpConvertUToF
:
4042 case SpvOpQuantizeToF16
:
4043 case SpvOpConvertPtrToU
:
4044 case SpvOpConvertUToPtr
:
4045 case SpvOpPtrCastToGeneric
:
4046 case SpvOpGenericCastToPtr
:
4052 case SpvOpSignBitSet
:
4053 case SpvOpLessOrGreater
:
4055 case SpvOpUnordered
:
4070 case SpvOpVectorTimesScalar
:
4072 case SpvOpIAddCarry
:
4073 case SpvOpISubBorrow
:
4074 case SpvOpUMulExtended
:
4075 case SpvOpSMulExtended
:
4076 case SpvOpShiftRightLogical
:
4077 case SpvOpShiftRightArithmetic
:
4078 case SpvOpShiftLeftLogical
:
4079 case SpvOpLogicalEqual
:
4080 case SpvOpLogicalNotEqual
:
4081 case SpvOpLogicalOr
:
4082 case SpvOpLogicalAnd
:
4083 case SpvOpLogicalNot
:
4084 case SpvOpBitwiseOr
:
4085 case SpvOpBitwiseXor
:
4086 case SpvOpBitwiseAnd
:
4088 case SpvOpFOrdEqual
:
4089 case SpvOpFUnordEqual
:
4090 case SpvOpINotEqual
:
4091 case SpvOpFOrdNotEqual
:
4092 case SpvOpFUnordNotEqual
:
4093 case SpvOpULessThan
:
4094 case SpvOpSLessThan
:
4095 case SpvOpFOrdLessThan
:
4096 case SpvOpFUnordLessThan
:
4097 case SpvOpUGreaterThan
:
4098 case SpvOpSGreaterThan
:
4099 case SpvOpFOrdGreaterThan
:
4100 case SpvOpFUnordGreaterThan
:
4101 case SpvOpULessThanEqual
:
4102 case SpvOpSLessThanEqual
:
4103 case SpvOpFOrdLessThanEqual
:
4104 case SpvOpFUnordLessThanEqual
:
4105 case SpvOpUGreaterThanEqual
:
4106 case SpvOpSGreaterThanEqual
:
4107 case SpvOpFOrdGreaterThanEqual
:
4108 case SpvOpFUnordGreaterThanEqual
:
4114 case SpvOpFwidthFine
:
4115 case SpvOpDPdxCoarse
:
4116 case SpvOpDPdyCoarse
:
4117 case SpvOpFwidthCoarse
:
4118 case SpvOpBitFieldInsert
:
4119 case SpvOpBitFieldSExtract
:
4120 case SpvOpBitFieldUExtract
:
4121 case SpvOpBitReverse
:
4123 case SpvOpTranspose
:
4124 case SpvOpOuterProduct
:
4125 case SpvOpMatrixTimesScalar
:
4126 case SpvOpVectorTimesMatrix
:
4127 case SpvOpMatrixTimesVector
:
4128 case SpvOpMatrixTimesMatrix
:
4129 vtn_handle_alu(b
, opcode
, w
, count
);
4132 case SpvOpVectorExtractDynamic
:
4133 case SpvOpVectorInsertDynamic
:
4134 case SpvOpVectorShuffle
:
4135 case SpvOpCompositeConstruct
:
4136 case SpvOpCompositeExtract
:
4137 case SpvOpCompositeInsert
:
4138 case SpvOpCopyObject
:
4139 vtn_handle_composite(b
, opcode
, w
, count
);
4142 case SpvOpEmitVertex
:
4143 case SpvOpEndPrimitive
:
4144 case SpvOpEmitStreamVertex
:
4145 case SpvOpEndStreamPrimitive
:
4146 case SpvOpControlBarrier
:
4147 case SpvOpMemoryBarrier
:
4148 vtn_handle_barrier(b
, opcode
, w
, count
);
4151 case SpvOpGroupNonUniformElect
:
4152 case SpvOpGroupNonUniformAll
:
4153 case SpvOpGroupNonUniformAny
:
4154 case SpvOpGroupNonUniformAllEqual
:
4155 case SpvOpGroupNonUniformBroadcast
:
4156 case SpvOpGroupNonUniformBroadcastFirst
:
4157 case SpvOpGroupNonUniformBallot
:
4158 case SpvOpGroupNonUniformInverseBallot
:
4159 case SpvOpGroupNonUniformBallotBitExtract
:
4160 case SpvOpGroupNonUniformBallotBitCount
:
4161 case SpvOpGroupNonUniformBallotFindLSB
:
4162 case SpvOpGroupNonUniformBallotFindMSB
:
4163 case SpvOpGroupNonUniformShuffle
:
4164 case SpvOpGroupNonUniformShuffleXor
:
4165 case SpvOpGroupNonUniformShuffleUp
:
4166 case SpvOpGroupNonUniformShuffleDown
:
4167 case SpvOpGroupNonUniformIAdd
:
4168 case SpvOpGroupNonUniformFAdd
:
4169 case SpvOpGroupNonUniformIMul
:
4170 case SpvOpGroupNonUniformFMul
:
4171 case SpvOpGroupNonUniformSMin
:
4172 case SpvOpGroupNonUniformUMin
:
4173 case SpvOpGroupNonUniformFMin
:
4174 case SpvOpGroupNonUniformSMax
:
4175 case SpvOpGroupNonUniformUMax
:
4176 case SpvOpGroupNonUniformFMax
:
4177 case SpvOpGroupNonUniformBitwiseAnd
:
4178 case SpvOpGroupNonUniformBitwiseOr
:
4179 case SpvOpGroupNonUniformBitwiseXor
:
4180 case SpvOpGroupNonUniformLogicalAnd
:
4181 case SpvOpGroupNonUniformLogicalOr
:
4182 case SpvOpGroupNonUniformLogicalXor
:
4183 case SpvOpGroupNonUniformQuadBroadcast
:
4184 case SpvOpGroupNonUniformQuadSwap
:
4185 vtn_handle_subgroup(b
, opcode
, w
, count
);
4189 vtn_fail("Unhandled opcode");
4196 vtn_create_builder(const uint32_t *words
, size_t word_count
,
4197 gl_shader_stage stage
, const char *entry_point_name
,
4198 const struct spirv_to_nir_options
*options
)
4200 /* Initialize the vtn_builder object */
4201 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
4203 b
->spirv_word_count
= word_count
;
4207 exec_list_make_empty(&b
->functions
);
4208 b
->entry_point_stage
= stage
;
4209 b
->entry_point_name
= entry_point_name
;
4210 b
->options
= options
;
4213 * Handle the SPIR-V header (first 5 dwords).
4214 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
4216 if (word_count
<= 5)
4219 if (words
[0] != SpvMagicNumber
) {
4220 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
4223 if (words
[1] < 0x10000) {
4224 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
4228 /* words[2] == generator magic */
4229 unsigned value_id_bound
= words
[3];
4230 if (words
[4] != 0) {
4231 vtn_err("words[4] was %u, want 0", words
[4]);
4235 b
->value_id_bound
= value_id_bound
;
4236 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
4245 spirv_to_nir(const uint32_t *words
, size_t word_count
,
4246 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
4247 gl_shader_stage stage
, const char *entry_point_name
,
4248 const struct spirv_to_nir_options
*options
,
4249 const nir_shader_compiler_options
*nir_options
)
4252 const uint32_t *word_end
= words
+ word_count
;
4254 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
4255 stage
, entry_point_name
,
4261 /* See also _vtn_fail() */
4262 if (setjmp(b
->fail_jump
)) {
4267 /* Skip the SPIR-V header, handled at vtn_create_builder */
4270 /* Handle all the preamble instructions */
4271 words
= vtn_foreach_instruction(b
, words
, word_end
,
4272 vtn_handle_preamble_instruction
);
4274 if (b
->entry_point
== NULL
) {
4275 vtn_fail("Entry point not found");
4280 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
4282 /* Set shader info defaults */
4283 b
->shader
->info
.gs
.invocations
= 1;
4285 /* Parse execution modes */
4286 vtn_foreach_execution_mode(b
, b
->entry_point
,
4287 vtn_handle_execution_mode
, NULL
);
4289 b
->specializations
= spec
;
4290 b
->num_specializations
= num_spec
;
4292 /* Handle all variable, type, and constant instructions */
4293 words
= vtn_foreach_instruction(b
, words
, word_end
,
4294 vtn_handle_variable_or_type_instruction
);
4296 /* Set types on all vtn_values */
4297 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
4299 vtn_build_cfg(b
, words
, word_end
);
4301 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4302 b
->entry_point
->func
->referenced
= true;
4307 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
4308 if (func
->referenced
&& !func
->emitted
) {
4309 b
->const_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
4310 _mesa_key_pointer_equal
);
4312 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
4318 /* We sometimes generate bogus derefs that, while never used, give the
4319 * validator a bit of heartburn. Run dead code to get rid of them.
4321 nir_opt_dce(b
->shader
);
4323 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4324 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
4325 vtn_assert(entry_point
);
4327 /* Unparent the shader from the vtn_builder before we delete the builder */
4328 ralloc_steal(NULL
, b
->shader
);