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
;
567 * Returns true if the given type contains a struct decorated Block or
571 vtn_type_contains_block(struct vtn_builder
*b
, struct vtn_type
*type
)
573 switch (type
->base_type
) {
574 case vtn_base_type_array
:
575 return vtn_type_contains_block(b
, type
->array_element
);
576 case vtn_base_type_struct
:
577 if (type
->block
|| type
->buffer_block
)
579 for (unsigned i
= 0; i
< type
->length
; i
++) {
580 if (vtn_type_contains_block(b
, type
->members
[i
]))
589 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
590 * OpStore, or OpCopyMemory between them without breaking anything.
591 * Technically, the SPIR-V rules require the exact same type ID but this lets
592 * us internally be a bit looser.
595 vtn_types_compatible(struct vtn_builder
*b
,
596 struct vtn_type
*t1
, struct vtn_type
*t2
)
598 if (t1
->id
== t2
->id
)
601 if (t1
->base_type
!= t2
->base_type
)
604 switch (t1
->base_type
) {
605 case vtn_base_type_void
:
606 case vtn_base_type_scalar
:
607 case vtn_base_type_vector
:
608 case vtn_base_type_matrix
:
609 case vtn_base_type_image
:
610 case vtn_base_type_sampler
:
611 case vtn_base_type_sampled_image
:
612 return t1
->type
== t2
->type
;
614 case vtn_base_type_array
:
615 return t1
->length
== t2
->length
&&
616 vtn_types_compatible(b
, t1
->array_element
, t2
->array_element
);
618 case vtn_base_type_pointer
:
619 return vtn_types_compatible(b
, t1
->deref
, t2
->deref
);
621 case vtn_base_type_struct
:
622 if (t1
->length
!= t2
->length
)
625 for (unsigned i
= 0; i
< t1
->length
; i
++) {
626 if (!vtn_types_compatible(b
, t1
->members
[i
], t2
->members
[i
]))
631 case vtn_base_type_function
:
632 /* This case shouldn't get hit since you can't copy around function
633 * types. Just require them to be identical.
638 vtn_fail("Invalid base type");
641 /* does a shallow copy of a vtn_type */
643 static struct vtn_type
*
644 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
646 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
649 switch (src
->base_type
) {
650 case vtn_base_type_void
:
651 case vtn_base_type_scalar
:
652 case vtn_base_type_vector
:
653 case vtn_base_type_matrix
:
654 case vtn_base_type_array
:
655 case vtn_base_type_pointer
:
656 case vtn_base_type_image
:
657 case vtn_base_type_sampler
:
658 case vtn_base_type_sampled_image
:
659 /* Nothing more to do */
662 case vtn_base_type_struct
:
663 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
664 memcpy(dest
->members
, src
->members
,
665 src
->length
* sizeof(src
->members
[0]));
667 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
668 memcpy(dest
->offsets
, src
->offsets
,
669 src
->length
* sizeof(src
->offsets
[0]));
672 case vtn_base_type_function
:
673 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
674 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
681 static struct vtn_type
*
682 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
684 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
685 type
= type
->members
[member
];
687 /* We may have an array of matrices.... Oh, joy! */
688 while (glsl_type_is_array(type
->type
)) {
689 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
690 type
= type
->array_element
;
693 vtn_assert(glsl_type_is_matrix(type
->type
));
699 vtn_handle_access_qualifier(struct vtn_builder
*b
, struct vtn_type
*type
,
700 int member
, enum gl_access_qualifier access
)
702 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
703 type
= type
->members
[member
];
705 type
->access
|= access
;
709 array_stride_decoration_cb(struct vtn_builder
*b
,
710 struct vtn_value
*val
, int member
,
711 const struct vtn_decoration
*dec
, void *void_ctx
)
713 struct vtn_type
*type
= val
->type
;
715 if (dec
->decoration
== SpvDecorationArrayStride
) {
716 vtn_fail_if(dec
->literals
[0] == 0, "ArrayStride must be non-zero");
717 type
->stride
= dec
->literals
[0];
722 struct_member_decoration_cb(struct vtn_builder
*b
,
723 struct vtn_value
*val
, int member
,
724 const struct vtn_decoration
*dec
, void *void_ctx
)
726 struct member_decoration_ctx
*ctx
= void_ctx
;
731 assert(member
< ctx
->num_fields
);
733 switch (dec
->decoration
) {
734 case SpvDecorationRelaxedPrecision
:
735 case SpvDecorationUniform
:
736 break; /* FIXME: Do nothing with this for now. */
737 case SpvDecorationNonWritable
:
738 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_WRITEABLE
);
740 case SpvDecorationNonReadable
:
741 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_READABLE
);
743 case SpvDecorationVolatile
:
744 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_VOLATILE
);
746 case SpvDecorationCoherent
:
747 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_COHERENT
);
749 case SpvDecorationNoPerspective
:
750 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
752 case SpvDecorationFlat
:
753 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
755 case SpvDecorationCentroid
:
756 ctx
->fields
[member
].centroid
= true;
758 case SpvDecorationSample
:
759 ctx
->fields
[member
].sample
= true;
761 case SpvDecorationStream
:
762 /* Vulkan only allows one GS stream */
763 vtn_assert(dec
->literals
[0] == 0);
765 case SpvDecorationLocation
:
766 ctx
->fields
[member
].location
= dec
->literals
[0];
768 case SpvDecorationComponent
:
769 break; /* FIXME: What should we do with these? */
770 case SpvDecorationBuiltIn
:
771 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
772 ctx
->type
->members
[member
]->is_builtin
= true;
773 ctx
->type
->members
[member
]->builtin
= dec
->literals
[0];
774 ctx
->type
->builtin_block
= true;
776 case SpvDecorationOffset
:
777 ctx
->type
->offsets
[member
] = dec
->literals
[0];
778 ctx
->fields
[member
].offset
= dec
->literals
[0];
780 case SpvDecorationMatrixStride
:
781 /* Handled as a second pass */
783 case SpvDecorationColMajor
:
784 break; /* Nothing to do here. Column-major is the default. */
785 case SpvDecorationRowMajor
:
786 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
789 case SpvDecorationPatch
:
792 case SpvDecorationSpecId
:
793 case SpvDecorationBlock
:
794 case SpvDecorationBufferBlock
:
795 case SpvDecorationArrayStride
:
796 case SpvDecorationGLSLShared
:
797 case SpvDecorationGLSLPacked
:
798 case SpvDecorationInvariant
:
799 case SpvDecorationRestrict
:
800 case SpvDecorationAliased
:
801 case SpvDecorationConstant
:
802 case SpvDecorationIndex
:
803 case SpvDecorationBinding
:
804 case SpvDecorationDescriptorSet
:
805 case SpvDecorationLinkageAttributes
:
806 case SpvDecorationNoContraction
:
807 case SpvDecorationInputAttachmentIndex
:
808 vtn_warn("Decoration not allowed on struct members: %s",
809 spirv_decoration_to_string(dec
->decoration
));
812 case SpvDecorationXfbBuffer
:
813 case SpvDecorationXfbStride
:
814 vtn_warn("Vulkan does not have transform feedback");
817 case SpvDecorationCPacked
:
818 case SpvDecorationSaturatedConversion
:
819 case SpvDecorationFuncParamAttr
:
820 case SpvDecorationFPRoundingMode
:
821 case SpvDecorationFPFastMathMode
:
822 case SpvDecorationAlignment
:
823 vtn_warn("Decoration only allowed for CL-style kernels: %s",
824 spirv_decoration_to_string(dec
->decoration
));
827 case SpvDecorationHlslSemanticGOOGLE
:
828 /* HLSL semantic decorations can safely be ignored by the driver. */
832 vtn_fail("Unhandled decoration");
836 /** Chases the array type all the way down to the tail and rewrites the
837 * glsl_types to be based off the tail's glsl_type.
840 vtn_array_type_rewrite_glsl_type(struct vtn_type
*type
)
842 if (type
->base_type
!= vtn_base_type_array
)
845 vtn_array_type_rewrite_glsl_type(type
->array_element
);
847 type
->type
= glsl_array_type(type
->array_element
->type
,
848 type
->length
, type
->stride
);
851 /* Matrix strides are handled as a separate pass because we need to know
852 * whether the matrix is row-major or not first.
855 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
856 struct vtn_value
*val
, int member
,
857 const struct vtn_decoration
*dec
,
860 if (dec
->decoration
!= SpvDecorationMatrixStride
)
863 vtn_fail_if(member
< 0,
864 "The MatrixStride decoration is only allowed on members "
866 vtn_fail_if(dec
->literals
[0] == 0, "MatrixStride must be non-zero");
868 struct member_decoration_ctx
*ctx
= void_ctx
;
870 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
871 if (mat_type
->row_major
) {
872 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
873 mat_type
->stride
= mat_type
->array_element
->stride
;
874 mat_type
->array_element
->stride
= dec
->literals
[0];
876 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
877 dec
->literals
[0], true);
878 mat_type
->array_element
->type
= glsl_get_column_type(mat_type
->type
);
880 vtn_assert(mat_type
->array_element
->stride
> 0);
881 mat_type
->stride
= dec
->literals
[0];
883 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
884 dec
->literals
[0], false);
887 /* Now that we've replaced the glsl_type with a properly strided matrix
888 * type, rewrite the member type so that it's an array of the proper kind
891 vtn_array_type_rewrite_glsl_type(ctx
->type
->members
[member
]);
892 ctx
->fields
[member
].type
= ctx
->type
->members
[member
]->type
;
896 type_decoration_cb(struct vtn_builder
*b
,
897 struct vtn_value
*val
, int member
,
898 const struct vtn_decoration
*dec
, void *ctx
)
900 struct vtn_type
*type
= val
->type
;
903 /* This should have been handled by OpTypeStruct */
904 assert(val
->type
->base_type
== vtn_base_type_struct
);
905 assert(member
>= 0 && member
< val
->type
->length
);
909 switch (dec
->decoration
) {
910 case SpvDecorationArrayStride
:
911 vtn_assert(type
->base_type
== vtn_base_type_array
||
912 type
->base_type
== vtn_base_type_pointer
);
914 case SpvDecorationBlock
:
915 vtn_assert(type
->base_type
== vtn_base_type_struct
);
918 case SpvDecorationBufferBlock
:
919 vtn_assert(type
->base_type
== vtn_base_type_struct
);
920 type
->buffer_block
= true;
922 case SpvDecorationGLSLShared
:
923 case SpvDecorationGLSLPacked
:
924 /* Ignore these, since we get explicit offsets anyways */
927 case SpvDecorationRowMajor
:
928 case SpvDecorationColMajor
:
929 case SpvDecorationMatrixStride
:
930 case SpvDecorationBuiltIn
:
931 case SpvDecorationNoPerspective
:
932 case SpvDecorationFlat
:
933 case SpvDecorationPatch
:
934 case SpvDecorationCentroid
:
935 case SpvDecorationSample
:
936 case SpvDecorationVolatile
:
937 case SpvDecorationCoherent
:
938 case SpvDecorationNonWritable
:
939 case SpvDecorationNonReadable
:
940 case SpvDecorationUniform
:
941 case SpvDecorationLocation
:
942 case SpvDecorationComponent
:
943 case SpvDecorationOffset
:
944 case SpvDecorationXfbBuffer
:
945 case SpvDecorationXfbStride
:
946 case SpvDecorationHlslSemanticGOOGLE
:
947 vtn_warn("Decoration only allowed for struct members: %s",
948 spirv_decoration_to_string(dec
->decoration
));
951 case SpvDecorationStream
:
952 /* We don't need to do anything here, as stream is filled up when
953 * aplying the decoration to a variable, just check that if it is not a
954 * struct member, it should be a struct.
956 vtn_assert(type
->base_type
== vtn_base_type_struct
);
959 case SpvDecorationRelaxedPrecision
:
960 case SpvDecorationSpecId
:
961 case SpvDecorationInvariant
:
962 case SpvDecorationRestrict
:
963 case SpvDecorationAliased
:
964 case SpvDecorationConstant
:
965 case SpvDecorationIndex
:
966 case SpvDecorationBinding
:
967 case SpvDecorationDescriptorSet
:
968 case SpvDecorationLinkageAttributes
:
969 case SpvDecorationNoContraction
:
970 case SpvDecorationInputAttachmentIndex
:
971 vtn_warn("Decoration not allowed on types: %s",
972 spirv_decoration_to_string(dec
->decoration
));
975 case SpvDecorationCPacked
:
976 case SpvDecorationSaturatedConversion
:
977 case SpvDecorationFuncParamAttr
:
978 case SpvDecorationFPRoundingMode
:
979 case SpvDecorationFPFastMathMode
:
980 case SpvDecorationAlignment
:
981 vtn_warn("Decoration only allowed for CL-style kernels: %s",
982 spirv_decoration_to_string(dec
->decoration
));
986 vtn_fail("Unhandled decoration");
991 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
994 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
995 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
996 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
997 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
998 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
999 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
1000 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
1001 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
1002 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
1003 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
1004 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
1005 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
1006 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
1007 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
1008 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
1009 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
1010 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
1011 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
1012 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
1013 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
1014 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
1015 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
1016 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
1017 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
1018 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
1019 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
1020 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
1021 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
1022 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
1023 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
1024 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
1025 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
1026 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
1027 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
1028 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
1029 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
1030 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
1031 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
1032 case SpvImageFormatR16ui
: return 0x8234; /* GL_R16UI */
1033 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
1035 vtn_fail("Invalid image format");
1039 static struct vtn_type
*
1040 vtn_type_layout_std430(struct vtn_builder
*b
, struct vtn_type
*type
,
1041 uint32_t *size_out
, uint32_t *align_out
)
1043 switch (type
->base_type
) {
1044 case vtn_base_type_scalar
: {
1045 uint32_t comp_size
= glsl_get_bit_size(type
->type
) / 8;
1046 *size_out
= comp_size
;
1047 *align_out
= comp_size
;
1051 case vtn_base_type_vector
: {
1052 uint32_t comp_size
= glsl_get_bit_size(type
->type
) / 8;
1053 unsigned align_comps
= type
->length
== 3 ? 4 : type
->length
;
1054 *size_out
= comp_size
* type
->length
,
1055 *align_out
= comp_size
* align_comps
;
1059 case vtn_base_type_matrix
:
1060 case vtn_base_type_array
: {
1061 /* We're going to add an array stride */
1062 type
= vtn_type_copy(b
, type
);
1063 uint32_t elem_size
, elem_align
;
1064 type
->array_element
= vtn_type_layout_std430(b
, type
->array_element
,
1065 &elem_size
, &elem_align
);
1066 type
->stride
= vtn_align_u32(elem_size
, elem_align
);
1067 *size_out
= type
->stride
* type
->length
;
1068 *align_out
= elem_align
;
1072 case vtn_base_type_struct
: {
1073 /* We're going to add member offsets */
1074 type
= vtn_type_copy(b
, type
);
1075 uint32_t offset
= 0;
1077 for (unsigned i
= 0; i
< type
->length
; i
++) {
1078 uint32_t mem_size
, mem_align
;
1079 type
->members
[i
] = vtn_type_layout_std430(b
, type
->members
[i
],
1080 &mem_size
, &mem_align
);
1081 offset
= vtn_align_u32(offset
, mem_align
);
1082 type
->offsets
[i
] = offset
;
1084 align
= MAX2(align
, mem_align
);
1092 unreachable("Invalid SPIR-V type for std430");
1097 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
1098 const uint32_t *w
, unsigned count
)
1100 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1102 val
->type
= rzalloc(b
, struct vtn_type
);
1103 val
->type
->id
= w
[1];
1107 val
->type
->base_type
= vtn_base_type_void
;
1108 val
->type
->type
= glsl_void_type();
1111 val
->type
->base_type
= vtn_base_type_scalar
;
1112 val
->type
->type
= glsl_bool_type();
1113 val
->type
->length
= 1;
1115 case SpvOpTypeInt
: {
1116 int bit_size
= w
[2];
1117 const bool signedness
= w
[3];
1118 val
->type
->base_type
= vtn_base_type_scalar
;
1121 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1124 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1127 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1130 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1133 vtn_fail("Invalid int bit size");
1135 val
->type
->length
= 1;
1139 case SpvOpTypeFloat
: {
1140 int bit_size
= w
[2];
1141 val
->type
->base_type
= vtn_base_type_scalar
;
1144 val
->type
->type
= glsl_float16_t_type();
1147 val
->type
->type
= glsl_float_type();
1150 val
->type
->type
= glsl_double_type();
1153 vtn_fail("Invalid float bit size");
1155 val
->type
->length
= 1;
1159 case SpvOpTypeVector
: {
1160 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1161 unsigned elems
= w
[3];
1163 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1164 "Base type for OpTypeVector must be a scalar");
1165 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1166 "Invalid component count for OpTypeVector");
1168 val
->type
->base_type
= vtn_base_type_vector
;
1169 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1170 val
->type
->length
= elems
;
1171 val
->type
->stride
= glsl_get_bit_size(base
->type
) / 8;
1172 val
->type
->array_element
= base
;
1176 case SpvOpTypeMatrix
: {
1177 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1178 unsigned columns
= w
[3];
1180 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1181 "Base type for OpTypeMatrix must be a vector");
1182 vtn_fail_if(columns
< 2 || columns
> 4,
1183 "Invalid column count for OpTypeMatrix");
1185 val
->type
->base_type
= vtn_base_type_matrix
;
1186 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1187 glsl_get_vector_elements(base
->type
),
1189 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1190 "Unsupported base type for OpTypeMatrix");
1191 assert(!glsl_type_is_error(val
->type
->type
));
1192 val
->type
->length
= columns
;
1193 val
->type
->array_element
= base
;
1194 val
->type
->row_major
= false;
1195 val
->type
->stride
= 0;
1199 case SpvOpTypeRuntimeArray
:
1200 case SpvOpTypeArray
: {
1201 struct vtn_type
*array_element
=
1202 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1204 if (opcode
== SpvOpTypeRuntimeArray
) {
1205 /* A length of 0 is used to denote unsized arrays */
1206 val
->type
->length
= 0;
1209 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
1212 val
->type
->base_type
= vtn_base_type_array
;
1213 val
->type
->array_element
= array_element
;
1214 val
->type
->stride
= 0;
1216 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1217 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
,
1222 case SpvOpTypeStruct
: {
1223 unsigned num_fields
= count
- 2;
1224 val
->type
->base_type
= vtn_base_type_struct
;
1225 val
->type
->length
= num_fields
;
1226 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1227 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1229 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1230 for (unsigned i
= 0; i
< num_fields
; i
++) {
1231 val
->type
->members
[i
] =
1232 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
1233 fields
[i
] = (struct glsl_struct_field
) {
1234 .type
= val
->type
->members
[i
]->type
,
1235 .name
= ralloc_asprintf(b
, "field%d", i
),
1240 struct member_decoration_ctx ctx
= {
1241 .num_fields
= num_fields
,
1246 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1247 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1249 const char *name
= val
->name
? val
->name
: "struct";
1251 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
);
1255 case SpvOpTypeFunction
: {
1256 val
->type
->base_type
= vtn_base_type_function
;
1257 val
->type
->type
= NULL
;
1259 val
->type
->return_type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1261 const unsigned num_params
= count
- 3;
1262 val
->type
->length
= num_params
;
1263 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1264 for (unsigned i
= 0; i
< count
- 3; i
++) {
1265 val
->type
->params
[i
] =
1266 vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
;
1271 case SpvOpTypePointer
: {
1272 SpvStorageClass storage_class
= w
[2];
1273 struct vtn_type
*deref_type
=
1274 vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
1276 val
->type
->base_type
= vtn_base_type_pointer
;
1277 val
->type
->storage_class
= storage_class
;
1278 val
->type
->deref
= deref_type
;
1280 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1282 if (storage_class
== SpvStorageClassUniform
||
1283 storage_class
== SpvStorageClassStorageBuffer
) {
1284 /* These can actually be stored to nir_variables and used as SSA
1285 * values so they need a real glsl_type.
1287 val
->type
->type
= glsl_vector_type(GLSL_TYPE_UINT
, 2);
1290 if (storage_class
== SpvStorageClassPushConstant
) {
1291 /* These can actually be stored to nir_variables and used as SSA
1292 * values so they need a real glsl_type.
1294 val
->type
->type
= glsl_uint_type();
1297 if (storage_class
== SpvStorageClassWorkgroup
) {
1298 /* These can actually be stored to nir_variables and used as SSA
1299 * values so they need a real glsl_type.
1301 val
->type
->type
= glsl_uint_type();
1302 if (b
->options
->lower_workgroup_access_to_offsets
) {
1303 uint32_t size
, align
;
1304 val
->type
->deref
= vtn_type_layout_std430(b
, val
->type
->deref
,
1306 val
->type
->length
= size
;
1307 val
->type
->align
= align
;
1313 case SpvOpTypeImage
: {
1314 val
->type
->base_type
= vtn_base_type_image
;
1316 const struct vtn_type
*sampled_type
=
1317 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1319 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1320 glsl_get_bit_size(sampled_type
->type
) != 32,
1321 "Sampled type of OpTypeImage must be a 32-bit scalar");
1323 enum glsl_sampler_dim dim
;
1324 switch ((SpvDim
)w
[3]) {
1325 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1326 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1327 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1328 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1329 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1330 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1331 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1333 vtn_fail("Invalid SPIR-V image dimensionality");
1336 bool is_shadow
= w
[4];
1337 bool is_array
= w
[5];
1338 bool multisampled
= w
[6];
1339 unsigned sampled
= w
[7];
1340 SpvImageFormat format
= w
[8];
1343 val
->type
->access_qualifier
= w
[9];
1345 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1348 if (dim
== GLSL_SAMPLER_DIM_2D
)
1349 dim
= GLSL_SAMPLER_DIM_MS
;
1350 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1351 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1353 vtn_fail("Unsupported multisampled image type");
1356 val
->type
->image_format
= translate_image_format(b
, format
);
1358 enum glsl_base_type sampled_base_type
=
1359 glsl_get_base_type(sampled_type
->type
);
1361 val
->type
->sampled
= true;
1362 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
1364 } else if (sampled
== 2) {
1365 vtn_assert(!is_shadow
);
1366 val
->type
->sampled
= false;
1367 val
->type
->type
= glsl_image_type(dim
, is_array
, sampled_base_type
);
1369 vtn_fail("We need to know if the image will be sampled");
1374 case SpvOpTypeSampledImage
:
1375 val
->type
->base_type
= vtn_base_type_sampled_image
;
1376 val
->type
->image
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1377 val
->type
->type
= val
->type
->image
->type
;
1380 case SpvOpTypeSampler
:
1381 /* The actual sampler type here doesn't really matter. It gets
1382 * thrown away the moment you combine it with an image. What really
1383 * matters is that it's a sampler type as opposed to an integer type
1384 * so the backend knows what to do.
1386 val
->type
->base_type
= vtn_base_type_sampler
;
1387 val
->type
->type
= glsl_bare_sampler_type();
1390 case SpvOpTypeOpaque
:
1391 case SpvOpTypeEvent
:
1392 case SpvOpTypeDeviceEvent
:
1393 case SpvOpTypeReserveId
:
1394 case SpvOpTypeQueue
:
1397 vtn_fail("Unhandled opcode");
1400 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1402 if (val
->type
->base_type
== vtn_base_type_struct
&&
1403 (val
->type
->block
|| val
->type
->buffer_block
)) {
1404 for (unsigned i
= 0; i
< val
->type
->length
; i
++) {
1405 vtn_fail_if(vtn_type_contains_block(b
, val
->type
->members
[i
]),
1406 "Block and BufferBlock decorations cannot decorate a "
1407 "structure type that is nested at any level inside "
1408 "another structure type decorated with Block or "
1414 static nir_constant
*
1415 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
1417 nir_constant
*c
= rzalloc(b
, nir_constant
);
1419 /* For pointers and other typeless things, we have to return something but
1420 * it doesn't matter what.
1425 switch (glsl_get_base_type(type
)) {
1427 case GLSL_TYPE_UINT
:
1428 case GLSL_TYPE_INT16
:
1429 case GLSL_TYPE_UINT16
:
1430 case GLSL_TYPE_UINT8
:
1431 case GLSL_TYPE_INT8
:
1432 case GLSL_TYPE_INT64
:
1433 case GLSL_TYPE_UINT64
:
1434 case GLSL_TYPE_BOOL
:
1435 case GLSL_TYPE_FLOAT
:
1436 case GLSL_TYPE_FLOAT16
:
1437 case GLSL_TYPE_DOUBLE
:
1438 /* Nothing to do here. It's already initialized to zero */
1441 case GLSL_TYPE_ARRAY
:
1442 vtn_assert(glsl_get_length(type
) > 0);
1443 c
->num_elements
= glsl_get_length(type
);
1444 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1446 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
1447 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1448 c
->elements
[i
] = c
->elements
[0];
1451 case GLSL_TYPE_STRUCT
:
1452 c
->num_elements
= glsl_get_length(type
);
1453 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1455 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
1456 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
1461 vtn_fail("Invalid type for null constant");
1468 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
1469 int member
, const struct vtn_decoration
*dec
,
1472 vtn_assert(member
== -1);
1473 if (dec
->decoration
!= SpvDecorationSpecId
)
1476 struct spec_constant_value
*const_value
= data
;
1478 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1479 if (b
->specializations
[i
].id
== dec
->literals
[0]) {
1480 if (const_value
->is_double
)
1481 const_value
->data64
= b
->specializations
[i
].data64
;
1483 const_value
->data32
= b
->specializations
[i
].data32
;
1490 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
1491 uint32_t const_value
)
1493 struct spec_constant_value data
;
1494 data
.is_double
= false;
1495 data
.data32
= const_value
;
1496 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1501 get_specialization64(struct vtn_builder
*b
, struct vtn_value
*val
,
1502 uint64_t const_value
)
1504 struct spec_constant_value data
;
1505 data
.is_double
= true;
1506 data
.data64
= const_value
;
1507 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1512 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1513 struct vtn_value
*val
,
1515 const struct vtn_decoration
*dec
,
1518 vtn_assert(member
== -1);
1519 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1520 dec
->literals
[0] != SpvBuiltInWorkgroupSize
)
1523 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1525 b
->shader
->info
.cs
.local_size
[0] = val
->constant
->values
[0].u32
[0];
1526 b
->shader
->info
.cs
.local_size
[1] = val
->constant
->values
[0].u32
[1];
1527 b
->shader
->info
.cs
.local_size
[2] = val
->constant
->values
[0].u32
[2];
1531 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1532 const uint32_t *w
, unsigned count
)
1534 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1535 val
->constant
= rzalloc(b
, nir_constant
);
1537 case SpvOpConstantTrue
:
1538 case SpvOpConstantFalse
:
1539 case SpvOpSpecConstantTrue
:
1540 case SpvOpSpecConstantFalse
: {
1541 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1542 "Result type of %s must be OpTypeBool",
1543 spirv_op_to_string(opcode
));
1545 uint32_t int_val
= (opcode
== SpvOpConstantTrue
||
1546 opcode
== SpvOpSpecConstantTrue
);
1548 if (opcode
== SpvOpSpecConstantTrue
||
1549 opcode
== SpvOpSpecConstantFalse
)
1550 int_val
= get_specialization(b
, val
, int_val
);
1552 val
->constant
->values
[0].b
[0] = int_val
!= 0;
1556 case SpvOpConstant
: {
1557 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1558 "Result type of %s must be a scalar",
1559 spirv_op_to_string(opcode
));
1560 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1563 val
->constant
->values
->u64
[0] = vtn_u64_literal(&w
[3]);
1566 val
->constant
->values
->u32
[0] = w
[3];
1569 val
->constant
->values
->u16
[0] = w
[3];
1572 val
->constant
->values
->u8
[0] = w
[3];
1575 vtn_fail("Unsupported SpvOpConstant bit size");
1580 case SpvOpSpecConstant
: {
1581 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1582 "Result type of %s must be a scalar",
1583 spirv_op_to_string(opcode
));
1584 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1587 val
->constant
->values
[0].u64
[0] =
1588 get_specialization64(b
, val
, vtn_u64_literal(&w
[3]));
1591 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1594 val
->constant
->values
[0].u16
[0] = get_specialization(b
, val
, w
[3]);
1597 val
->constant
->values
[0].u8
[0] = get_specialization(b
, val
, w
[3]);
1600 vtn_fail("Unsupported SpvOpSpecConstant bit size");
1605 case SpvOpSpecConstantComposite
:
1606 case SpvOpConstantComposite
: {
1607 unsigned elem_count
= count
- 3;
1608 vtn_fail_if(elem_count
!= val
->type
->length
,
1609 "%s has %u constituents, expected %u",
1610 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1612 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1613 for (unsigned i
= 0; i
< elem_count
; i
++) {
1614 struct vtn_value
*val
= vtn_untyped_value(b
, w
[i
+ 3]);
1616 if (val
->value_type
== vtn_value_type_constant
) {
1617 elems
[i
] = val
->constant
;
1619 vtn_fail_if(val
->value_type
!= vtn_value_type_undef
,
1620 "only constants or undefs allowed for "
1621 "SpvOpConstantComposite");
1622 /* to make it easier, just insert a NULL constant for now */
1623 elems
[i
] = vtn_null_constant(b
, val
->type
->type
);
1627 switch (val
->type
->base_type
) {
1628 case vtn_base_type_vector
: {
1629 assert(glsl_type_is_vector(val
->type
->type
));
1630 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1631 for (unsigned i
= 0; i
< elem_count
; i
++) {
1634 val
->constant
->values
[0].u64
[i
] = elems
[i
]->values
[0].u64
[0];
1637 val
->constant
->values
[0].u32
[i
] = elems
[i
]->values
[0].u32
[0];
1640 val
->constant
->values
[0].u16
[i
] = elems
[i
]->values
[0].u16
[0];
1643 val
->constant
->values
[0].u8
[i
] = elems
[i
]->values
[0].u8
[0];
1646 val
->constant
->values
[0].b
[i
] = elems
[i
]->values
[0].b
[0];
1649 vtn_fail("Invalid SpvOpConstantComposite bit size");
1655 case vtn_base_type_matrix
:
1656 assert(glsl_type_is_matrix(val
->type
->type
));
1657 for (unsigned i
= 0; i
< elem_count
; i
++)
1658 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1661 case vtn_base_type_struct
:
1662 case vtn_base_type_array
:
1663 ralloc_steal(val
->constant
, elems
);
1664 val
->constant
->num_elements
= elem_count
;
1665 val
->constant
->elements
= elems
;
1669 vtn_fail("Result type of %s must be a composite type",
1670 spirv_op_to_string(opcode
));
1675 case SpvOpSpecConstantOp
: {
1676 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1678 case SpvOpVectorShuffle
: {
1679 struct vtn_value
*v0
= &b
->values
[w
[4]];
1680 struct vtn_value
*v1
= &b
->values
[w
[5]];
1682 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1683 v0
->value_type
== vtn_value_type_undef
);
1684 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1685 v1
->value_type
== vtn_value_type_undef
);
1687 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1688 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1690 vtn_assert(len0
+ len1
< 16);
1692 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1693 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1694 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1696 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1697 (void)bit_size0
; (void)bit_size1
;
1699 if (bit_size
== 64) {
1701 if (v0
->value_type
== vtn_value_type_constant
) {
1702 for (unsigned i
= 0; i
< len0
; i
++)
1703 u64
[i
] = v0
->constant
->values
[0].u64
[i
];
1705 if (v1
->value_type
== vtn_value_type_constant
) {
1706 for (unsigned i
= 0; i
< len1
; i
++)
1707 u64
[len0
+ i
] = v1
->constant
->values
[0].u64
[i
];
1710 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1711 uint32_t comp
= w
[i
+ 6];
1712 /* If component is not used, set the value to a known constant
1713 * to detect if it is wrongly used.
1715 if (comp
== (uint32_t)-1)
1716 val
->constant
->values
[0].u64
[j
] = 0xdeadbeefdeadbeef;
1718 val
->constant
->values
[0].u64
[j
] = u64
[comp
];
1721 /* This is for both 32-bit and 16-bit values */
1723 if (v0
->value_type
== vtn_value_type_constant
) {
1724 for (unsigned i
= 0; i
< len0
; i
++)
1725 u32
[i
] = v0
->constant
->values
[0].u32
[i
];
1727 if (v1
->value_type
== vtn_value_type_constant
) {
1728 for (unsigned i
= 0; i
< len1
; i
++)
1729 u32
[len0
+ i
] = v1
->constant
->values
[0].u32
[i
];
1732 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1733 uint32_t comp
= w
[i
+ 6];
1734 /* If component is not used, set the value to a known constant
1735 * to detect if it is wrongly used.
1737 if (comp
== (uint32_t)-1)
1738 val
->constant
->values
[0].u32
[j
] = 0xdeadbeef;
1740 val
->constant
->values
[0].u32
[j
] = u32
[comp
];
1746 case SpvOpCompositeExtract
:
1747 case SpvOpCompositeInsert
: {
1748 struct vtn_value
*comp
;
1749 unsigned deref_start
;
1750 struct nir_constant
**c
;
1751 if (opcode
== SpvOpCompositeExtract
) {
1752 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1754 c
= &comp
->constant
;
1756 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1758 val
->constant
= nir_constant_clone(comp
->constant
,
1765 const struct vtn_type
*type
= comp
->type
;
1766 for (unsigned i
= deref_start
; i
< count
; i
++) {
1767 vtn_fail_if(w
[i
] > type
->length
,
1768 "%uth index of %s is %u but the type has only "
1769 "%u elements", i
- deref_start
,
1770 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1772 switch (type
->base_type
) {
1773 case vtn_base_type_vector
:
1775 type
= type
->array_element
;
1778 case vtn_base_type_matrix
:
1779 assert(col
== 0 && elem
== -1);
1782 type
= type
->array_element
;
1785 case vtn_base_type_array
:
1786 c
= &(*c
)->elements
[w
[i
]];
1787 type
= type
->array_element
;
1790 case vtn_base_type_struct
:
1791 c
= &(*c
)->elements
[w
[i
]];
1792 type
= type
->members
[w
[i
]];
1796 vtn_fail("%s must only index into composite types",
1797 spirv_op_to_string(opcode
));
1801 if (opcode
== SpvOpCompositeExtract
) {
1805 unsigned num_components
= type
->length
;
1806 unsigned bit_size
= glsl_get_bit_size(type
->type
);
1807 for (unsigned i
= 0; i
< num_components
; i
++)
1810 val
->constant
->values
[0].u64
[i
] = (*c
)->values
[col
].u64
[elem
+ i
];
1813 val
->constant
->values
[0].u32
[i
] = (*c
)->values
[col
].u32
[elem
+ i
];
1816 val
->constant
->values
[0].u16
[i
] = (*c
)->values
[col
].u16
[elem
+ i
];
1819 val
->constant
->values
[0].u8
[i
] = (*c
)->values
[col
].u8
[elem
+ i
];
1822 val
->constant
->values
[0].b
[i
] = (*c
)->values
[col
].b
[elem
+ i
];
1825 vtn_fail("Invalid SpvOpCompositeExtract bit size");
1829 struct vtn_value
*insert
=
1830 vtn_value(b
, w
[4], vtn_value_type_constant
);
1831 vtn_assert(insert
->type
== type
);
1833 *c
= insert
->constant
;
1835 unsigned num_components
= type
->length
;
1836 unsigned bit_size
= glsl_get_bit_size(type
->type
);
1837 for (unsigned i
= 0; i
< num_components
; i
++)
1840 (*c
)->values
[col
].u64
[elem
+ i
] = insert
->constant
->values
[0].u64
[i
];
1843 (*c
)->values
[col
].u32
[elem
+ i
] = insert
->constant
->values
[0].u32
[i
];
1846 (*c
)->values
[col
].u16
[elem
+ i
] = insert
->constant
->values
[0].u16
[i
];
1849 (*c
)->values
[col
].u8
[elem
+ i
] = insert
->constant
->values
[0].u8
[i
];
1852 (*c
)->values
[col
].b
[elem
+ i
] = insert
->constant
->values
[0].b
[i
];
1855 vtn_fail("Invalid SpvOpCompositeInsert bit size");
1864 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
1865 nir_alu_type src_alu_type
= dst_alu_type
;
1866 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
1869 vtn_assert(count
<= 7);
1874 /* We have a source in a conversion */
1876 nir_get_nir_type_for_glsl_type(
1877 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1878 /* We use the bitsize of the conversion source to evaluate the opcode later */
1879 bit_size
= glsl_get_bit_size(
1880 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1883 bit_size
= glsl_get_bit_size(val
->type
->type
);
1886 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
1887 nir_alu_type_get_type_size(src_alu_type
),
1888 nir_alu_type_get_type_size(dst_alu_type
));
1889 nir_const_value src
[4];
1891 for (unsigned i
= 0; i
< count
- 4; i
++) {
1892 struct vtn_value
*src_val
=
1893 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
);
1895 /* If this is an unsized source, pull the bit size from the
1896 * source; otherwise, we'll use the bit size from the destination.
1898 if (!nir_alu_type_get_type_size(nir_op_infos
[op
].input_types
[i
]))
1899 bit_size
= glsl_get_bit_size(src_val
->type
->type
);
1901 unsigned j
= swap
? 1 - i
: i
;
1902 src
[j
] = src_val
->constant
->values
[0];
1905 /* fix up fixed size sources */
1912 for (unsigned i
= 0; i
< num_components
; ++i
) {
1914 case 64: src
[1].u32
[i
] = src
[1].u64
[i
]; break;
1915 case 16: src
[1].u32
[i
] = src
[1].u16
[i
]; break;
1916 case 8: src
[1].u32
[i
] = src
[1].u8
[i
]; break;
1925 val
->constant
->values
[0] =
1926 nir_eval_const_opcode(op
, num_components
, bit_size
, src
);
1933 case SpvOpConstantNull
:
1934 val
->constant
= vtn_null_constant(b
, val
->type
->type
);
1937 case SpvOpConstantSampler
:
1938 vtn_fail("OpConstantSampler requires Kernel Capability");
1942 vtn_fail("Unhandled opcode");
1945 /* Now that we have the value, update the workgroup size if needed */
1946 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1949 struct vtn_ssa_value
*
1950 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1952 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1955 if (!glsl_type_is_vector_or_scalar(type
)) {
1956 unsigned elems
= glsl_get_length(type
);
1957 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1958 for (unsigned i
= 0; i
< elems
; i
++) {
1959 const struct glsl_type
*child_type
;
1961 switch (glsl_get_base_type(type
)) {
1963 case GLSL_TYPE_UINT
:
1964 case GLSL_TYPE_INT16
:
1965 case GLSL_TYPE_UINT16
:
1966 case GLSL_TYPE_UINT8
:
1967 case GLSL_TYPE_INT8
:
1968 case GLSL_TYPE_INT64
:
1969 case GLSL_TYPE_UINT64
:
1970 case GLSL_TYPE_BOOL
:
1971 case GLSL_TYPE_FLOAT
:
1972 case GLSL_TYPE_FLOAT16
:
1973 case GLSL_TYPE_DOUBLE
:
1974 child_type
= glsl_get_column_type(type
);
1976 case GLSL_TYPE_ARRAY
:
1977 child_type
= glsl_get_array_element(type
);
1979 case GLSL_TYPE_STRUCT
:
1980 child_type
= glsl_get_struct_field(type
, i
);
1983 vtn_fail("unkown base type");
1986 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
1994 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
1997 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
1998 src
.src_type
= type
;
2003 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
2004 const uint32_t *w
, unsigned count
)
2006 if (opcode
== SpvOpSampledImage
) {
2007 struct vtn_value
*val
=
2008 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2009 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2010 val
->sampled_image
->type
=
2011 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2012 val
->sampled_image
->image
=
2013 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2014 val
->sampled_image
->sampler
=
2015 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
2017 } else if (opcode
== SpvOpImage
) {
2018 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2019 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
2020 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
2021 val
->pointer
= src_val
->sampled_image
->image
;
2023 vtn_assert(src_val
->value_type
== vtn_value_type_pointer
);
2024 val
->pointer
= src_val
->pointer
;
2029 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2030 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2032 struct vtn_sampled_image sampled
;
2033 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
2034 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
2035 sampled
= *sampled_val
->sampled_image
;
2037 vtn_assert(sampled_val
->value_type
== vtn_value_type_pointer
);
2038 sampled
.type
= sampled_val
->pointer
->type
;
2039 sampled
.image
= NULL
;
2040 sampled
.sampler
= sampled_val
->pointer
;
2043 const struct glsl_type
*image_type
= sampled
.type
->type
;
2044 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
2045 const bool is_array
= glsl_sampler_type_is_array(image_type
);
2047 /* Figure out the base texture operation */
2050 case SpvOpImageSampleImplicitLod
:
2051 case SpvOpImageSampleDrefImplicitLod
:
2052 case SpvOpImageSampleProjImplicitLod
:
2053 case SpvOpImageSampleProjDrefImplicitLod
:
2054 texop
= nir_texop_tex
;
2057 case SpvOpImageSampleExplicitLod
:
2058 case SpvOpImageSampleDrefExplicitLod
:
2059 case SpvOpImageSampleProjExplicitLod
:
2060 case SpvOpImageSampleProjDrefExplicitLod
:
2061 texop
= nir_texop_txl
;
2064 case SpvOpImageFetch
:
2065 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
2066 texop
= nir_texop_txf_ms
;
2068 texop
= nir_texop_txf
;
2072 case SpvOpImageGather
:
2073 case SpvOpImageDrefGather
:
2074 texop
= nir_texop_tg4
;
2077 case SpvOpImageQuerySizeLod
:
2078 case SpvOpImageQuerySize
:
2079 texop
= nir_texop_txs
;
2082 case SpvOpImageQueryLod
:
2083 texop
= nir_texop_lod
;
2086 case SpvOpImageQueryLevels
:
2087 texop
= nir_texop_query_levels
;
2090 case SpvOpImageQuerySamples
:
2091 texop
= nir_texop_texture_samples
;
2095 vtn_fail("Unhandled opcode");
2098 nir_tex_src srcs
[10]; /* 10 should be enough */
2099 nir_tex_src
*p
= srcs
;
2101 nir_deref_instr
*sampler
= vtn_pointer_to_deref(b
, sampled
.sampler
);
2102 nir_deref_instr
*texture
=
2103 sampled
.image
? vtn_pointer_to_deref(b
, sampled
.image
) : sampler
;
2105 p
->src
= nir_src_for_ssa(&texture
->dest
.ssa
);
2106 p
->src_type
= nir_tex_src_texture_deref
;
2115 /* These operations require a sampler */
2116 p
->src
= nir_src_for_ssa(&sampler
->dest
.ssa
);
2117 p
->src_type
= nir_tex_src_sampler_deref
;
2121 case nir_texop_txf_ms
:
2124 case nir_texop_query_levels
:
2125 case nir_texop_texture_samples
:
2126 case nir_texop_samples_identical
:
2129 case nir_texop_txf_ms_mcs
:
2130 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2135 struct nir_ssa_def
*coord
;
2136 unsigned coord_components
;
2138 case SpvOpImageSampleImplicitLod
:
2139 case SpvOpImageSampleExplicitLod
:
2140 case SpvOpImageSampleDrefImplicitLod
:
2141 case SpvOpImageSampleDrefExplicitLod
:
2142 case SpvOpImageSampleProjImplicitLod
:
2143 case SpvOpImageSampleProjExplicitLod
:
2144 case SpvOpImageSampleProjDrefImplicitLod
:
2145 case SpvOpImageSampleProjDrefExplicitLod
:
2146 case SpvOpImageFetch
:
2147 case SpvOpImageGather
:
2148 case SpvOpImageDrefGather
:
2149 case SpvOpImageQueryLod
: {
2150 /* All these types have the coordinate as their first real argument */
2151 switch (sampler_dim
) {
2152 case GLSL_SAMPLER_DIM_1D
:
2153 case GLSL_SAMPLER_DIM_BUF
:
2154 coord_components
= 1;
2156 case GLSL_SAMPLER_DIM_2D
:
2157 case GLSL_SAMPLER_DIM_RECT
:
2158 case GLSL_SAMPLER_DIM_MS
:
2159 coord_components
= 2;
2161 case GLSL_SAMPLER_DIM_3D
:
2162 case GLSL_SAMPLER_DIM_CUBE
:
2163 coord_components
= 3;
2166 vtn_fail("Invalid sampler type");
2169 if (is_array
&& texop
!= nir_texop_lod
)
2172 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
2173 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2174 (1 << coord_components
) - 1));
2175 p
->src_type
= nir_tex_src_coord
;
2182 coord_components
= 0;
2187 case SpvOpImageSampleProjImplicitLod
:
2188 case SpvOpImageSampleProjExplicitLod
:
2189 case SpvOpImageSampleProjDrefImplicitLod
:
2190 case SpvOpImageSampleProjDrefExplicitLod
:
2191 /* These have the projector as the last coordinate component */
2192 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2193 p
->src_type
= nir_tex_src_projector
;
2201 bool is_shadow
= false;
2202 unsigned gather_component
= 0;
2204 case SpvOpImageSampleDrefImplicitLod
:
2205 case SpvOpImageSampleDrefExplicitLod
:
2206 case SpvOpImageSampleProjDrefImplicitLod
:
2207 case SpvOpImageSampleProjDrefExplicitLod
:
2208 case SpvOpImageDrefGather
:
2209 /* These all have an explicit depth value as their next source */
2211 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2214 case SpvOpImageGather
:
2215 /* This has a component as its next source */
2217 vtn_value(b
, w
[idx
++], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
2224 /* For OpImageQuerySizeLod, we always have an LOD */
2225 if (opcode
== SpvOpImageQuerySizeLod
)
2226 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2228 /* Now we need to handle some number of optional arguments */
2229 const struct vtn_ssa_value
*gather_offsets
= NULL
;
2231 uint32_t operands
= w
[idx
++];
2233 if (operands
& SpvImageOperandsBiasMask
) {
2234 vtn_assert(texop
== nir_texop_tex
);
2235 texop
= nir_texop_txb
;
2236 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
2239 if (operands
& SpvImageOperandsLodMask
) {
2240 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2241 texop
== nir_texop_txs
);
2242 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2245 if (operands
& SpvImageOperandsGradMask
) {
2246 vtn_assert(texop
== nir_texop_txl
);
2247 texop
= nir_texop_txd
;
2248 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
2249 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
2252 if (operands
& SpvImageOperandsOffsetMask
||
2253 operands
& SpvImageOperandsConstOffsetMask
)
2254 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
2256 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2257 nir_tex_src none
= {0};
2258 gather_offsets
= vtn_ssa_value(b
, w
[idx
++]);
2262 if (operands
& SpvImageOperandsSampleMask
) {
2263 vtn_assert(texop
== nir_texop_txf_ms
);
2264 texop
= nir_texop_txf_ms
;
2265 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
2268 if (operands
& SpvImageOperandsMinLodMask
) {
2269 vtn_assert(texop
== nir_texop_tex
||
2270 texop
== nir_texop_txb
||
2271 texop
== nir_texop_txd
);
2272 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_min_lod
);
2275 /* We should have now consumed exactly all of the arguments */
2276 vtn_assert(idx
== count
);
2278 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2281 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2283 instr
->coord_components
= coord_components
;
2284 instr
->sampler_dim
= sampler_dim
;
2285 instr
->is_array
= is_array
;
2286 instr
->is_shadow
= is_shadow
;
2287 instr
->is_new_style_shadow
=
2288 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2289 instr
->component
= gather_component
;
2291 switch (glsl_get_sampler_result_type(image_type
)) {
2292 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
2293 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
2294 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
2295 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
2297 vtn_fail("Invalid base type for sampler result");
2300 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2301 nir_tex_instr_dest_size(instr
), 32, NULL
);
2303 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2304 nir_tex_instr_dest_size(instr
));
2307 nir_instr
*instruction
;
2308 if (gather_offsets
) {
2309 vtn_assert(glsl_get_base_type(gather_offsets
->type
) == GLSL_TYPE_ARRAY
);
2310 vtn_assert(glsl_get_length(gather_offsets
->type
) == 4);
2311 nir_tex_instr
*instrs
[4] = {instr
, NULL
, NULL
, NULL
};
2313 /* Copy the current instruction 4x */
2314 for (uint32_t i
= 1; i
< 4; i
++) {
2315 instrs
[i
] = nir_tex_instr_create(b
->shader
, instr
->num_srcs
);
2316 instrs
[i
]->op
= instr
->op
;
2317 instrs
[i
]->coord_components
= instr
->coord_components
;
2318 instrs
[i
]->sampler_dim
= instr
->sampler_dim
;
2319 instrs
[i
]->is_array
= instr
->is_array
;
2320 instrs
[i
]->is_shadow
= instr
->is_shadow
;
2321 instrs
[i
]->is_new_style_shadow
= instr
->is_new_style_shadow
;
2322 instrs
[i
]->component
= instr
->component
;
2323 instrs
[i
]->dest_type
= instr
->dest_type
;
2325 memcpy(instrs
[i
]->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2327 nir_ssa_dest_init(&instrs
[i
]->instr
, &instrs
[i
]->dest
,
2328 nir_tex_instr_dest_size(instr
), 32, NULL
);
2331 /* Fill in the last argument with the offset from the passed in offsets
2332 * and insert the instruction into the stream.
2334 for (uint32_t i
= 0; i
< 4; i
++) {
2336 src
.src
= nir_src_for_ssa(gather_offsets
->elems
[i
]->def
);
2337 src
.src_type
= nir_tex_src_offset
;
2338 instrs
[i
]->src
[instrs
[i
]->num_srcs
- 1] = src
;
2339 nir_builder_instr_insert(&b
->nb
, &instrs
[i
]->instr
);
2342 /* Combine the results of the 4 instructions by taking their .w
2345 nir_alu_instr
*vec4
= nir_alu_instr_create(b
->shader
, nir_op_vec4
);
2346 nir_ssa_dest_init(&vec4
->instr
, &vec4
->dest
.dest
, 4, 32, NULL
);
2347 vec4
->dest
.write_mask
= 0xf;
2348 for (uint32_t i
= 0; i
< 4; i
++) {
2349 vec4
->src
[i
].src
= nir_src_for_ssa(&instrs
[i
]->dest
.ssa
);
2350 vec4
->src
[i
].swizzle
[0] = 3;
2352 def
= &vec4
->dest
.dest
.ssa
;
2353 instruction
= &vec4
->instr
;
2355 def
= &instr
->dest
.ssa
;
2356 instruction
= &instr
->instr
;
2359 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
2360 val
->ssa
->def
= def
;
2362 nir_builder_instr_insert(&b
->nb
, instruction
);
2366 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2367 const uint32_t *w
, nir_src
*src
)
2370 case SpvOpAtomicIIncrement
:
2371 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
2374 case SpvOpAtomicIDecrement
:
2375 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
2378 case SpvOpAtomicISub
:
2380 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
2383 case SpvOpAtomicCompareExchange
:
2384 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
2385 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
2388 case SpvOpAtomicExchange
:
2389 case SpvOpAtomicIAdd
:
2390 case SpvOpAtomicSMin
:
2391 case SpvOpAtomicUMin
:
2392 case SpvOpAtomicSMax
:
2393 case SpvOpAtomicUMax
:
2394 case SpvOpAtomicAnd
:
2396 case SpvOpAtomicXor
:
2397 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
2401 vtn_fail("Invalid SPIR-V atomic");
2405 static nir_ssa_def
*
2406 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2408 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
2410 /* The image_load_store intrinsics assume a 4-dim coordinate */
2411 unsigned dim
= glsl_get_vector_elements(coord
->type
);
2412 unsigned swizzle
[4];
2413 for (unsigned i
= 0; i
< 4; i
++)
2414 swizzle
[i
] = MIN2(i
, dim
- 1);
2416 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
2419 static nir_ssa_def
*
2420 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2422 if (value
->num_components
== 4)
2426 for (unsigned i
= 0; i
< 4; i
++)
2427 swiz
[i
] = i
< value
->num_components
? i
: 0;
2428 return nir_swizzle(b
, value
, swiz
, 4, false);
2432 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2433 const uint32_t *w
, unsigned count
)
2435 /* Just get this one out of the way */
2436 if (opcode
== SpvOpImageTexelPointer
) {
2437 struct vtn_value
*val
=
2438 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2439 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2441 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2442 val
->image
->coord
= get_image_coord(b
, w
[4]);
2443 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
2447 struct vtn_image_pointer image
;
2450 case SpvOpAtomicExchange
:
2451 case SpvOpAtomicCompareExchange
:
2452 case SpvOpAtomicCompareExchangeWeak
:
2453 case SpvOpAtomicIIncrement
:
2454 case SpvOpAtomicIDecrement
:
2455 case SpvOpAtomicIAdd
:
2456 case SpvOpAtomicISub
:
2457 case SpvOpAtomicLoad
:
2458 case SpvOpAtomicSMin
:
2459 case SpvOpAtomicUMin
:
2460 case SpvOpAtomicSMax
:
2461 case SpvOpAtomicUMax
:
2462 case SpvOpAtomicAnd
:
2464 case SpvOpAtomicXor
:
2465 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
2468 case SpvOpAtomicStore
:
2469 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
2472 case SpvOpImageQuerySize
:
2473 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2475 image
.sample
= NULL
;
2478 case SpvOpImageRead
:
2479 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2480 image
.coord
= get_image_coord(b
, w
[4]);
2482 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
2483 vtn_assert(w
[5] == SpvImageOperandsSampleMask
);
2484 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
2486 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2490 case SpvOpImageWrite
:
2491 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2492 image
.coord
= get_image_coord(b
, w
[2]);
2496 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
2497 vtn_assert(w
[4] == SpvImageOperandsSampleMask
);
2498 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
2500 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2505 vtn_fail("Invalid image opcode");
2508 nir_intrinsic_op op
;
2510 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
2511 OP(ImageQuerySize
, size
)
2513 OP(ImageWrite
, store
)
2514 OP(AtomicLoad
, load
)
2515 OP(AtomicStore
, store
)
2516 OP(AtomicExchange
, atomic_exchange
)
2517 OP(AtomicCompareExchange
, atomic_comp_swap
)
2518 OP(AtomicIIncrement
, atomic_add
)
2519 OP(AtomicIDecrement
, atomic_add
)
2520 OP(AtomicIAdd
, atomic_add
)
2521 OP(AtomicISub
, atomic_add
)
2522 OP(AtomicSMin
, atomic_min
)
2523 OP(AtomicUMin
, atomic_min
)
2524 OP(AtomicSMax
, atomic_max
)
2525 OP(AtomicUMax
, atomic_max
)
2526 OP(AtomicAnd
, atomic_and
)
2527 OP(AtomicOr
, atomic_or
)
2528 OP(AtomicXor
, atomic_xor
)
2531 vtn_fail("Invalid image opcode");
2534 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
2536 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
2537 intrin
->src
[0] = nir_src_for_ssa(&image_deref
->dest
.ssa
);
2539 /* ImageQuerySize doesn't take any extra parameters */
2540 if (opcode
!= SpvOpImageQuerySize
) {
2541 /* The image coordinate is always 4 components but we may not have that
2542 * many. Swizzle to compensate.
2544 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
2545 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
2549 case SpvOpAtomicLoad
:
2550 case SpvOpImageQuerySize
:
2551 case SpvOpImageRead
:
2553 case SpvOpAtomicStore
:
2554 case SpvOpImageWrite
: {
2555 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
2556 nir_ssa_def
*value
= vtn_ssa_value(b
, value_id
)->def
;
2557 /* nir_intrinsic_image_deref_store always takes a vec4 value */
2558 assert(op
== nir_intrinsic_image_deref_store
);
2559 intrin
->num_components
= 4;
2560 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
));
2564 case SpvOpAtomicCompareExchange
:
2565 case SpvOpAtomicIIncrement
:
2566 case SpvOpAtomicIDecrement
:
2567 case SpvOpAtomicExchange
:
2568 case SpvOpAtomicIAdd
:
2569 case SpvOpAtomicISub
:
2570 case SpvOpAtomicSMin
:
2571 case SpvOpAtomicUMin
:
2572 case SpvOpAtomicSMax
:
2573 case SpvOpAtomicUMax
:
2574 case SpvOpAtomicAnd
:
2576 case SpvOpAtomicXor
:
2577 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
2581 vtn_fail("Invalid image opcode");
2584 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
2585 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2586 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2588 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
2589 intrin
->num_components
= nir_intrinsic_infos
[op
].dest_components
;
2590 if (intrin
->num_components
== 0)
2591 intrin
->num_components
= dest_components
;
2593 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2594 intrin
->num_components
, 32, NULL
);
2596 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2598 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
2599 if (intrin
->num_components
!= dest_components
)
2600 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
2602 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
2603 val
->ssa
->def
= result
;
2605 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2609 static nir_intrinsic_op
2610 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2613 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2614 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2615 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##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 SSBO atomic");
2635 static nir_intrinsic_op
2636 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2639 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
2640 OP(AtomicLoad
, read_deref
)
2641 OP(AtomicExchange
, exchange
)
2642 OP(AtomicCompareExchange
, comp_swap
)
2643 OP(AtomicIIncrement
, inc_deref
)
2644 OP(AtomicIDecrement
, post_dec_deref
)
2645 OP(AtomicIAdd
, add_deref
)
2646 OP(AtomicISub
, add_deref
)
2647 OP(AtomicUMin
, min_deref
)
2648 OP(AtomicUMax
, max_deref
)
2649 OP(AtomicAnd
, and_deref
)
2650 OP(AtomicOr
, or_deref
)
2651 OP(AtomicXor
, xor_deref
)
2654 /* We left the following out: AtomicStore, AtomicSMin and
2655 * AtomicSmax. Right now there are not nir intrinsics for them. At this
2656 * moment Atomic Counter support is needed for ARB_spirv support, so is
2657 * only need to support GLSL Atomic Counters that are uints and don't
2658 * allow direct storage.
2660 unreachable("Invalid uniform atomic");
2664 static nir_intrinsic_op
2665 get_shared_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2668 case SpvOpAtomicLoad
: return nir_intrinsic_load_shared
;
2669 case SpvOpAtomicStore
: return nir_intrinsic_store_shared
;
2670 #define OP(S, N) case SpvOp##S: return nir_intrinsic_shared_##N;
2671 OP(AtomicExchange
, atomic_exchange
)
2672 OP(AtomicCompareExchange
, atomic_comp_swap
)
2673 OP(AtomicIIncrement
, atomic_add
)
2674 OP(AtomicIDecrement
, atomic_add
)
2675 OP(AtomicIAdd
, atomic_add
)
2676 OP(AtomicISub
, atomic_add
)
2677 OP(AtomicSMin
, atomic_imin
)
2678 OP(AtomicUMin
, atomic_umin
)
2679 OP(AtomicSMax
, atomic_imax
)
2680 OP(AtomicUMax
, atomic_umax
)
2681 OP(AtomicAnd
, atomic_and
)
2682 OP(AtomicOr
, atomic_or
)
2683 OP(AtomicXor
, atomic_xor
)
2686 vtn_fail("Invalid shared atomic");
2690 static nir_intrinsic_op
2691 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2694 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
2695 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
2696 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
2697 OP(AtomicExchange
, atomic_exchange
)
2698 OP(AtomicCompareExchange
, atomic_comp_swap
)
2699 OP(AtomicIIncrement
, atomic_add
)
2700 OP(AtomicIDecrement
, atomic_add
)
2701 OP(AtomicIAdd
, atomic_add
)
2702 OP(AtomicISub
, atomic_add
)
2703 OP(AtomicSMin
, atomic_imin
)
2704 OP(AtomicUMin
, atomic_umin
)
2705 OP(AtomicSMax
, atomic_imax
)
2706 OP(AtomicUMax
, atomic_umax
)
2707 OP(AtomicAnd
, atomic_and
)
2708 OP(AtomicOr
, atomic_or
)
2709 OP(AtomicXor
, atomic_xor
)
2712 vtn_fail("Invalid shared atomic");
2717 * Handles shared atomics, ssbo atomics and atomic counters.
2720 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
2721 const uint32_t *w
, unsigned count
)
2723 struct vtn_pointer
*ptr
;
2724 nir_intrinsic_instr
*atomic
;
2727 case SpvOpAtomicLoad
:
2728 case SpvOpAtomicExchange
:
2729 case SpvOpAtomicCompareExchange
:
2730 case SpvOpAtomicCompareExchangeWeak
:
2731 case SpvOpAtomicIIncrement
:
2732 case SpvOpAtomicIDecrement
:
2733 case SpvOpAtomicIAdd
:
2734 case SpvOpAtomicISub
:
2735 case SpvOpAtomicSMin
:
2736 case SpvOpAtomicUMin
:
2737 case SpvOpAtomicSMax
:
2738 case SpvOpAtomicUMax
:
2739 case SpvOpAtomicAnd
:
2741 case SpvOpAtomicXor
:
2742 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2745 case SpvOpAtomicStore
:
2746 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2750 vtn_fail("Invalid SPIR-V atomic");
2754 SpvScope scope = w[4];
2755 SpvMemorySemanticsMask semantics = w[5];
2758 /* uniform as "atomic counter uniform" */
2759 if (ptr
->mode
== vtn_variable_mode_uniform
) {
2760 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2761 const struct glsl_type
*deref_type
= deref
->type
;
2762 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
2763 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2764 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2766 /* SSBO needs to initialize index/offset. In this case we don't need to,
2767 * as that info is already stored on the ptr->var->var nir_variable (see
2768 * vtn_create_variable)
2772 case SpvOpAtomicLoad
:
2773 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2776 case SpvOpAtomicStore
:
2777 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2778 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2781 case SpvOpAtomicExchange
:
2782 case SpvOpAtomicCompareExchange
:
2783 case SpvOpAtomicCompareExchangeWeak
:
2784 case SpvOpAtomicIIncrement
:
2785 case SpvOpAtomicIDecrement
:
2786 case SpvOpAtomicIAdd
:
2787 case SpvOpAtomicISub
:
2788 case SpvOpAtomicSMin
:
2789 case SpvOpAtomicUMin
:
2790 case SpvOpAtomicSMax
:
2791 case SpvOpAtomicUMax
:
2792 case SpvOpAtomicAnd
:
2794 case SpvOpAtomicXor
:
2795 /* Nothing: we don't need to call fill_common_atomic_sources here, as
2796 * atomic counter uniforms doesn't have sources
2801 unreachable("Invalid SPIR-V atomic");
2804 } else if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
2805 nir_ssa_def
*offset
, *index
;
2806 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
2808 nir_intrinsic_op op
;
2809 if (ptr
->mode
== vtn_variable_mode_ssbo
) {
2810 op
= get_ssbo_nir_atomic_op(b
, opcode
);
2812 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
&&
2813 b
->options
->lower_workgroup_access_to_offsets
);
2814 op
= get_shared_nir_atomic_op(b
, opcode
);
2817 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2821 case SpvOpAtomicLoad
:
2822 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2823 nir_intrinsic_set_align(atomic
, 4, 0);
2824 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2825 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2826 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2829 case SpvOpAtomicStore
:
2830 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2831 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2832 nir_intrinsic_set_align(atomic
, 4, 0);
2833 atomic
->src
[src
++] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2834 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2835 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2836 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2839 case SpvOpAtomicExchange
:
2840 case SpvOpAtomicCompareExchange
:
2841 case SpvOpAtomicCompareExchangeWeak
:
2842 case SpvOpAtomicIIncrement
:
2843 case SpvOpAtomicIDecrement
:
2844 case SpvOpAtomicIAdd
:
2845 case SpvOpAtomicISub
:
2846 case SpvOpAtomicSMin
:
2847 case SpvOpAtomicUMin
:
2848 case SpvOpAtomicSMax
:
2849 case SpvOpAtomicUMax
:
2850 case SpvOpAtomicAnd
:
2852 case SpvOpAtomicXor
:
2853 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2854 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2855 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2856 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
2860 vtn_fail("Invalid SPIR-V atomic");
2863 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2864 const struct glsl_type
*deref_type
= deref
->type
;
2865 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
2866 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2867 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2870 case SpvOpAtomicLoad
:
2871 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2874 case SpvOpAtomicStore
:
2875 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2876 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2877 atomic
->src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2880 case SpvOpAtomicExchange
:
2881 case SpvOpAtomicCompareExchange
:
2882 case SpvOpAtomicCompareExchangeWeak
:
2883 case SpvOpAtomicIIncrement
:
2884 case SpvOpAtomicIDecrement
:
2885 case SpvOpAtomicIAdd
:
2886 case SpvOpAtomicISub
:
2887 case SpvOpAtomicSMin
:
2888 case SpvOpAtomicUMin
:
2889 case SpvOpAtomicSMax
:
2890 case SpvOpAtomicUMax
:
2891 case SpvOpAtomicAnd
:
2893 case SpvOpAtomicXor
:
2894 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
2898 vtn_fail("Invalid SPIR-V atomic");
2902 if (opcode
!= SpvOpAtomicStore
) {
2903 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2905 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
2906 glsl_get_vector_elements(type
->type
),
2907 glsl_get_bit_size(type
->type
), NULL
);
2909 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2910 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
2911 val
->ssa
->def
= &atomic
->dest
.ssa
;
2912 val
->ssa
->type
= type
->type
;
2915 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
2918 static nir_alu_instr
*
2919 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
2922 switch (num_components
) {
2923 case 1: op
= nir_op_imov
; break;
2924 case 2: op
= nir_op_vec2
; break;
2925 case 3: op
= nir_op_vec3
; break;
2926 case 4: op
= nir_op_vec4
; break;
2927 default: vtn_fail("bad vector size");
2930 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
2931 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
2933 vec
->dest
.write_mask
= (1 << num_components
) - 1;
2938 struct vtn_ssa_value
*
2939 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
2941 if (src
->transposed
)
2942 return src
->transposed
;
2944 struct vtn_ssa_value
*dest
=
2945 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
2947 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
2948 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
2949 glsl_get_bit_size(src
->type
));
2950 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2951 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
2952 vec
->src
[0].swizzle
[0] = i
;
2954 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
2955 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
2956 vec
->src
[j
].swizzle
[0] = i
;
2959 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2960 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
2963 dest
->transposed
= src
;
2969 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
2971 return nir_channel(&b
->nb
, src
, index
);
2975 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
2978 nir_alu_instr
*vec
= create_vec(b
, src
->num_components
,
2981 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
2983 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
2985 vec
->src
[i
].src
= nir_src_for_ssa(src
);
2986 vec
->src
[i
].swizzle
[0] = i
;
2990 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2992 return &vec
->dest
.dest
.ssa
;
2995 static nir_ssa_def
*
2996 nir_ieq_imm(nir_builder
*b
, nir_ssa_def
*x
, uint64_t i
)
2998 return nir_ieq(b
, x
, nir_imm_intN_t(b
, i
, x
->bit_size
));
3002 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
3005 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
3006 for (unsigned i
= 1; i
< src
->num_components
; i
++)
3007 dest
= nir_bcsel(&b
->nb
, nir_ieq_imm(&b
->nb
, index
, i
),
3008 vtn_vector_extract(b
, src
, i
), dest
);
3014 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
3015 nir_ssa_def
*insert
, nir_ssa_def
*index
)
3017 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
3018 for (unsigned i
= 1; i
< src
->num_components
; i
++)
3019 dest
= nir_bcsel(&b
->nb
, nir_ieq_imm(&b
->nb
, index
, i
),
3020 vtn_vector_insert(b
, src
, insert
, i
), dest
);
3025 static nir_ssa_def
*
3026 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
3027 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
3028 const uint32_t *indices
)
3030 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
3032 for (unsigned i
= 0; i
< num_components
; i
++) {
3033 uint32_t index
= indices
[i
];
3034 if (index
== 0xffffffff) {
3036 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
3037 } else if (index
< src0
->num_components
) {
3038 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
3039 vec
->src
[i
].swizzle
[0] = index
;
3041 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
3042 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
3046 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3048 return &vec
->dest
.dest
.ssa
;
3052 * Concatentates a number of vectors/scalars together to produce a vector
3054 static nir_ssa_def
*
3055 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
3056 unsigned num_srcs
, nir_ssa_def
**srcs
)
3058 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
3060 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3062 * "When constructing a vector, there must be at least two Constituent
3065 vtn_assert(num_srcs
>= 2);
3067 unsigned dest_idx
= 0;
3068 for (unsigned i
= 0; i
< num_srcs
; i
++) {
3069 nir_ssa_def
*src
= srcs
[i
];
3070 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
3071 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
3072 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
3073 vec
->src
[dest_idx
].swizzle
[0] = j
;
3078 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3080 * "When constructing a vector, the total number of components in all
3081 * the operands must equal the number of components in Result Type."
3083 vtn_assert(dest_idx
== num_components
);
3085 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3087 return &vec
->dest
.dest
.ssa
;
3090 static struct vtn_ssa_value
*
3091 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3093 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3094 dest
->type
= src
->type
;
3096 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3097 dest
->def
= src
->def
;
3099 unsigned elems
= glsl_get_length(src
->type
);
3101 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3102 for (unsigned i
= 0; i
< elems
; i
++)
3103 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3109 static struct vtn_ssa_value
*
3110 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3111 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3112 unsigned num_indices
)
3114 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3116 struct vtn_ssa_value
*cur
= dest
;
3118 for (i
= 0; i
< num_indices
- 1; i
++) {
3119 cur
= cur
->elems
[indices
[i
]];
3122 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3123 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3124 * the component granularity. In that case, the last index will be
3125 * the index to insert the scalar into the vector.
3128 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
3130 cur
->elems
[indices
[i
]] = insert
;
3136 static struct vtn_ssa_value
*
3137 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3138 const uint32_t *indices
, unsigned num_indices
)
3140 struct vtn_ssa_value
*cur
= src
;
3141 for (unsigned i
= 0; i
< num_indices
; i
++) {
3142 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3143 vtn_assert(i
== num_indices
- 1);
3144 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3145 * the component granularity. The last index will be the index of the
3146 * vector to extract.
3149 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
3150 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
3151 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
3154 cur
= cur
->elems
[indices
[i
]];
3162 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3163 const uint32_t *w
, unsigned count
)
3165 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
3166 const struct glsl_type
*type
=
3167 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
3168 val
->ssa
= vtn_create_ssa_value(b
, type
);
3171 case SpvOpVectorExtractDynamic
:
3172 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3173 vtn_ssa_value(b
, w
[4])->def
);
3176 case SpvOpVectorInsertDynamic
:
3177 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3178 vtn_ssa_value(b
, w
[4])->def
,
3179 vtn_ssa_value(b
, w
[5])->def
);
3182 case SpvOpVectorShuffle
:
3183 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
3184 vtn_ssa_value(b
, w
[3])->def
,
3185 vtn_ssa_value(b
, w
[4])->def
,
3189 case SpvOpCompositeConstruct
: {
3190 unsigned elems
= count
- 3;
3192 if (glsl_type_is_vector_or_scalar(type
)) {
3193 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3194 for (unsigned i
= 0; i
< elems
; i
++)
3195 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
3197 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
3200 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3201 for (unsigned i
= 0; i
< elems
; i
++)
3202 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3206 case SpvOpCompositeExtract
:
3207 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3211 case SpvOpCompositeInsert
:
3212 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3213 vtn_ssa_value(b
, w
[3]),
3217 case SpvOpCopyObject
:
3218 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3222 vtn_fail("unknown composite operation");
3227 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3229 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3230 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3234 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3235 SpvMemorySemanticsMask semantics
)
3237 static const SpvMemorySemanticsMask all_memory_semantics
=
3238 SpvMemorySemanticsUniformMemoryMask
|
3239 SpvMemorySemanticsWorkgroupMemoryMask
|
3240 SpvMemorySemanticsAtomicCounterMemoryMask
|
3241 SpvMemorySemanticsImageMemoryMask
;
3243 /* If we're not actually doing a memory barrier, bail */
3244 if (!(semantics
& all_memory_semantics
))
3247 /* GL and Vulkan don't have these */
3248 vtn_assert(scope
!= SpvScopeCrossDevice
);
3250 if (scope
== SpvScopeSubgroup
)
3251 return; /* Nothing to do here */
3253 if (scope
== SpvScopeWorkgroup
) {
3254 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3258 /* There's only two scopes thing left */
3259 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3261 if ((semantics
& all_memory_semantics
) == all_memory_semantics
) {
3262 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3266 /* Issue a bunch of more specific barriers */
3267 uint32_t bits
= semantics
;
3269 SpvMemorySemanticsMask semantic
= 1 << u_bit_scan(&bits
);
3271 case SpvMemorySemanticsUniformMemoryMask
:
3272 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3274 case SpvMemorySemanticsWorkgroupMemoryMask
:
3275 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3277 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3278 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3280 case SpvMemorySemanticsImageMemoryMask
:
3281 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3290 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3291 const uint32_t *w
, unsigned count
)
3294 case SpvOpEmitVertex
:
3295 case SpvOpEmitStreamVertex
:
3296 case SpvOpEndPrimitive
:
3297 case SpvOpEndStreamPrimitive
: {
3298 nir_intrinsic_op intrinsic_op
;
3300 case SpvOpEmitVertex
:
3301 case SpvOpEmitStreamVertex
:
3302 intrinsic_op
= nir_intrinsic_emit_vertex
;
3304 case SpvOpEndPrimitive
:
3305 case SpvOpEndStreamPrimitive
:
3306 intrinsic_op
= nir_intrinsic_end_primitive
;
3309 unreachable("Invalid opcode");
3312 nir_intrinsic_instr
*intrin
=
3313 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3316 case SpvOpEmitStreamVertex
:
3317 case SpvOpEndStreamPrimitive
: {
3318 unsigned stream
= vtn_constant_value(b
, w
[1])->values
[0].u32
[0];
3319 nir_intrinsic_set_stream_id(intrin
, stream
);
3327 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3331 case SpvOpMemoryBarrier
: {
3332 SpvScope scope
= vtn_constant_value(b
, w
[1])->values
[0].u32
[0];
3333 SpvMemorySemanticsMask semantics
=
3334 vtn_constant_value(b
, w
[2])->values
[0].u32
[0];
3335 vtn_emit_memory_barrier(b
, scope
, semantics
);
3339 case SpvOpControlBarrier
: {
3340 SpvScope execution_scope
=
3341 vtn_constant_value(b
, w
[1])->values
[0].u32
[0];
3342 if (execution_scope
== SpvScopeWorkgroup
)
3343 vtn_emit_barrier(b
, nir_intrinsic_barrier
);
3345 SpvScope memory_scope
=
3346 vtn_constant_value(b
, w
[2])->values
[0].u32
[0];
3347 SpvMemorySemanticsMask memory_semantics
=
3348 vtn_constant_value(b
, w
[3])->values
[0].u32
[0];
3349 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
3354 unreachable("unknown barrier instruction");
3359 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
3360 SpvExecutionMode mode
)
3363 case SpvExecutionModeInputPoints
:
3364 case SpvExecutionModeOutputPoints
:
3365 return 0; /* GL_POINTS */
3366 case SpvExecutionModeInputLines
:
3367 return 1; /* GL_LINES */
3368 case SpvExecutionModeInputLinesAdjacency
:
3369 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3370 case SpvExecutionModeTriangles
:
3371 return 4; /* GL_TRIANGLES */
3372 case SpvExecutionModeInputTrianglesAdjacency
:
3373 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3374 case SpvExecutionModeQuads
:
3375 return 7; /* GL_QUADS */
3376 case SpvExecutionModeIsolines
:
3377 return 0x8E7A; /* GL_ISOLINES */
3378 case SpvExecutionModeOutputLineStrip
:
3379 return 3; /* GL_LINE_STRIP */
3380 case SpvExecutionModeOutputTriangleStrip
:
3381 return 5; /* GL_TRIANGLE_STRIP */
3383 vtn_fail("Invalid primitive type");
3388 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
3389 SpvExecutionMode mode
)
3392 case SpvExecutionModeInputPoints
:
3394 case SpvExecutionModeInputLines
:
3396 case SpvExecutionModeInputLinesAdjacency
:
3398 case SpvExecutionModeTriangles
:
3400 case SpvExecutionModeInputTrianglesAdjacency
:
3403 vtn_fail("Invalid GS input mode");
3407 static gl_shader_stage
3408 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
3411 case SpvExecutionModelVertex
:
3412 return MESA_SHADER_VERTEX
;
3413 case SpvExecutionModelTessellationControl
:
3414 return MESA_SHADER_TESS_CTRL
;
3415 case SpvExecutionModelTessellationEvaluation
:
3416 return MESA_SHADER_TESS_EVAL
;
3417 case SpvExecutionModelGeometry
:
3418 return MESA_SHADER_GEOMETRY
;
3419 case SpvExecutionModelFragment
:
3420 return MESA_SHADER_FRAGMENT
;
3421 case SpvExecutionModelGLCompute
:
3422 return MESA_SHADER_COMPUTE
;
3424 vtn_fail("Unsupported execution model");
3428 #define spv_check_supported(name, cap) do { \
3429 if (!(b->options && b->options->caps.name)) \
3430 vtn_warn("Unsupported SPIR-V capability: %s", \
3431 spirv_capability_to_string(cap)); \
3436 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
3439 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
3440 /* Let this be a name label regardless */
3441 unsigned name_words
;
3442 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
3444 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
3445 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
3448 vtn_assert(b
->entry_point
== NULL
);
3449 b
->entry_point
= entry_point
;
3453 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3454 const uint32_t *w
, unsigned count
)
3461 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
3462 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
3463 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
3464 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
3465 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
3466 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
3469 uint32_t version
= w
[2];
3472 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
3474 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
3478 case SpvOpSourceExtension
:
3479 case SpvOpSourceContinued
:
3480 case SpvOpExtension
:
3481 case SpvOpModuleProcessed
:
3482 /* Unhandled, but these are for debug so that's ok. */
3485 case SpvOpCapability
: {
3486 SpvCapability cap
= w
[1];
3488 case SpvCapabilityMatrix
:
3489 case SpvCapabilityShader
:
3490 case SpvCapabilityGeometry
:
3491 case SpvCapabilityGeometryPointSize
:
3492 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
3493 case SpvCapabilitySampledImageArrayDynamicIndexing
:
3494 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
3495 case SpvCapabilityStorageImageArrayDynamicIndexing
:
3496 case SpvCapabilityImageRect
:
3497 case SpvCapabilitySampledRect
:
3498 case SpvCapabilitySampled1D
:
3499 case SpvCapabilityImage1D
:
3500 case SpvCapabilitySampledCubeArray
:
3501 case SpvCapabilityImageCubeArray
:
3502 case SpvCapabilitySampledBuffer
:
3503 case SpvCapabilityImageBuffer
:
3504 case SpvCapabilityImageQuery
:
3505 case SpvCapabilityDerivativeControl
:
3506 case SpvCapabilityInterpolationFunction
:
3507 case SpvCapabilityMultiViewport
:
3508 case SpvCapabilitySampleRateShading
:
3509 case SpvCapabilityClipDistance
:
3510 case SpvCapabilityCullDistance
:
3511 case SpvCapabilityInputAttachment
:
3512 case SpvCapabilityImageGatherExtended
:
3513 case SpvCapabilityStorageImageExtendedFormats
:
3516 case SpvCapabilityLinkage
:
3517 case SpvCapabilityVector16
:
3518 case SpvCapabilityFloat16Buffer
:
3519 case SpvCapabilityFloat16
:
3520 case SpvCapabilityInt8
:
3521 case SpvCapabilitySparseResidency
:
3522 vtn_warn("Unsupported SPIR-V capability: %s",
3523 spirv_capability_to_string(cap
));
3526 case SpvCapabilityMinLod
:
3527 spv_check_supported(min_lod
, cap
);
3530 case SpvCapabilityAtomicStorage
:
3531 spv_check_supported(atomic_storage
, cap
);
3534 case SpvCapabilityFloat64
:
3535 spv_check_supported(float64
, cap
);
3537 case SpvCapabilityInt64
:
3538 spv_check_supported(int64
, cap
);
3540 case SpvCapabilityInt16
:
3541 spv_check_supported(int16
, cap
);
3544 case SpvCapabilityTransformFeedback
:
3545 spv_check_supported(transform_feedback
, cap
);
3548 case SpvCapabilityGeometryStreams
:
3549 spv_check_supported(geometry_streams
, cap
);
3552 case SpvCapabilityInt64Atomics
:
3553 spv_check_supported(int64_atomics
, cap
);
3556 case SpvCapabilityStorageImageMultisample
:
3557 spv_check_supported(storage_image_ms
, cap
);
3560 case SpvCapabilityAddresses
:
3561 case SpvCapabilityKernel
:
3562 case SpvCapabilityImageBasic
:
3563 case SpvCapabilityImageReadWrite
:
3564 case SpvCapabilityImageMipmap
:
3565 case SpvCapabilityPipes
:
3566 case SpvCapabilityGroups
:
3567 case SpvCapabilityDeviceEnqueue
:
3568 case SpvCapabilityLiteralSampler
:
3569 case SpvCapabilityGenericPointer
:
3570 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
3571 spirv_capability_to_string(cap
));
3574 case SpvCapabilityImageMSArray
:
3575 spv_check_supported(image_ms_array
, cap
);
3578 case SpvCapabilityTessellation
:
3579 case SpvCapabilityTessellationPointSize
:
3580 spv_check_supported(tessellation
, cap
);
3583 case SpvCapabilityDrawParameters
:
3584 spv_check_supported(draw_parameters
, cap
);
3587 case SpvCapabilityStorageImageReadWithoutFormat
:
3588 spv_check_supported(image_read_without_format
, cap
);
3591 case SpvCapabilityStorageImageWriteWithoutFormat
:
3592 spv_check_supported(image_write_without_format
, cap
);
3595 case SpvCapabilityDeviceGroup
:
3596 spv_check_supported(device_group
, cap
);
3599 case SpvCapabilityMultiView
:
3600 spv_check_supported(multiview
, cap
);
3603 case SpvCapabilityGroupNonUniform
:
3604 spv_check_supported(subgroup_basic
, cap
);
3607 case SpvCapabilityGroupNonUniformVote
:
3608 spv_check_supported(subgroup_vote
, cap
);
3611 case SpvCapabilitySubgroupBallotKHR
:
3612 case SpvCapabilityGroupNonUniformBallot
:
3613 spv_check_supported(subgroup_ballot
, cap
);
3616 case SpvCapabilityGroupNonUniformShuffle
:
3617 case SpvCapabilityGroupNonUniformShuffleRelative
:
3618 spv_check_supported(subgroup_shuffle
, cap
);
3621 case SpvCapabilityGroupNonUniformQuad
:
3622 spv_check_supported(subgroup_quad
, cap
);
3625 case SpvCapabilityGroupNonUniformArithmetic
:
3626 case SpvCapabilityGroupNonUniformClustered
:
3627 spv_check_supported(subgroup_arithmetic
, cap
);
3630 case SpvCapabilityVariablePointersStorageBuffer
:
3631 case SpvCapabilityVariablePointers
:
3632 spv_check_supported(variable_pointers
, cap
);
3633 b
->variable_pointers
= true;
3636 case SpvCapabilityStorageUniformBufferBlock16
:
3637 case SpvCapabilityStorageUniform16
:
3638 case SpvCapabilityStoragePushConstant16
:
3639 case SpvCapabilityStorageInputOutput16
:
3640 spv_check_supported(storage_16bit
, cap
);
3643 case SpvCapabilityShaderViewportIndexLayerEXT
:
3644 spv_check_supported(shader_viewport_index_layer
, cap
);
3647 case SpvCapabilityStorageBuffer8BitAccess
:
3648 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
3649 case SpvCapabilityStoragePushConstant8
:
3650 spv_check_supported(storage_8bit
, cap
);
3653 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
3654 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
3655 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
3656 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
3659 case SpvCapabilityRuntimeDescriptorArrayEXT
:
3660 spv_check_supported(runtime_descriptor_array
, cap
);
3663 case SpvCapabilityStencilExportEXT
:
3664 spv_check_supported(stencil_export
, cap
);
3667 case SpvCapabilitySampleMaskPostDepthCoverage
:
3668 spv_check_supported(post_depth_coverage
, cap
);
3672 vtn_fail("Unhandled capability");
3677 case SpvOpExtInstImport
:
3678 vtn_handle_extension(b
, opcode
, w
, count
);
3681 case SpvOpMemoryModel
:
3682 vtn_assert(w
[1] == SpvAddressingModelLogical
);
3683 vtn_assert(w
[2] == SpvMemoryModelSimple
||
3684 w
[2] == SpvMemoryModelGLSL450
);
3687 case SpvOpEntryPoint
:
3688 vtn_handle_entry_point(b
, w
, count
);
3692 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
3693 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3697 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3700 case SpvOpMemberName
:
3704 case SpvOpExecutionMode
:
3705 case SpvOpDecorationGroup
:
3707 case SpvOpMemberDecorate
:
3708 case SpvOpGroupDecorate
:
3709 case SpvOpGroupMemberDecorate
:
3710 case SpvOpDecorateStringGOOGLE
:
3711 case SpvOpMemberDecorateStringGOOGLE
:
3712 vtn_handle_decoration(b
, opcode
, w
, count
);
3716 return false; /* End of preamble */
3723 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
3724 const struct vtn_decoration
*mode
, void *data
)
3726 vtn_assert(b
->entry_point
== entry_point
);
3728 switch(mode
->exec_mode
) {
3729 case SpvExecutionModeOriginUpperLeft
:
3730 case SpvExecutionModeOriginLowerLeft
:
3731 b
->origin_upper_left
=
3732 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
3735 case SpvExecutionModeEarlyFragmentTests
:
3736 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3737 b
->shader
->info
.fs
.early_fragment_tests
= true;
3740 case SpvExecutionModePostDepthCoverage
:
3741 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3742 b
->shader
->info
.fs
.post_depth_coverage
= true;
3745 case SpvExecutionModeInvocations
:
3746 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3747 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->literals
[0]);
3750 case SpvExecutionModeDepthReplacing
:
3751 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3752 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
3754 case SpvExecutionModeDepthGreater
:
3755 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3756 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
3758 case SpvExecutionModeDepthLess
:
3759 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3760 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
3762 case SpvExecutionModeDepthUnchanged
:
3763 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3764 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
3767 case SpvExecutionModeLocalSize
:
3768 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
3769 b
->shader
->info
.cs
.local_size
[0] = mode
->literals
[0];
3770 b
->shader
->info
.cs
.local_size
[1] = mode
->literals
[1];
3771 b
->shader
->info
.cs
.local_size
[2] = mode
->literals
[2];
3773 case SpvExecutionModeLocalSizeHint
:
3774 break; /* Nothing to do with this */
3776 case SpvExecutionModeOutputVertices
:
3777 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3778 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3779 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->literals
[0];
3781 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3782 b
->shader
->info
.gs
.vertices_out
= mode
->literals
[0];
3786 case SpvExecutionModeInputPoints
:
3787 case SpvExecutionModeInputLines
:
3788 case SpvExecutionModeInputLinesAdjacency
:
3789 case SpvExecutionModeTriangles
:
3790 case SpvExecutionModeInputTrianglesAdjacency
:
3791 case SpvExecutionModeQuads
:
3792 case SpvExecutionModeIsolines
:
3793 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3794 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3795 b
->shader
->info
.tess
.primitive_mode
=
3796 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3798 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3799 b
->shader
->info
.gs
.vertices_in
=
3800 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
3801 b
->shader
->info
.gs
.input_primitive
=
3802 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3806 case SpvExecutionModeOutputPoints
:
3807 case SpvExecutionModeOutputLineStrip
:
3808 case SpvExecutionModeOutputTriangleStrip
:
3809 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3810 b
->shader
->info
.gs
.output_primitive
=
3811 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3814 case SpvExecutionModeSpacingEqual
:
3815 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3816 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3817 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
3819 case SpvExecutionModeSpacingFractionalEven
:
3820 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3821 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3822 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
3824 case SpvExecutionModeSpacingFractionalOdd
:
3825 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3826 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3827 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
3829 case SpvExecutionModeVertexOrderCw
:
3830 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3831 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3832 b
->shader
->info
.tess
.ccw
= false;
3834 case SpvExecutionModeVertexOrderCcw
:
3835 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3836 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3837 b
->shader
->info
.tess
.ccw
= true;
3839 case SpvExecutionModePointMode
:
3840 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3841 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3842 b
->shader
->info
.tess
.point_mode
= true;
3845 case SpvExecutionModePixelCenterInteger
:
3846 b
->pixel_center_integer
= true;
3849 case SpvExecutionModeXfb
:
3850 b
->shader
->info
.has_transform_feedback_varyings
= true;
3853 case SpvExecutionModeVecTypeHint
:
3854 case SpvExecutionModeContractionOff
:
3857 case SpvExecutionModeStencilRefReplacingEXT
:
3858 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3862 vtn_fail("Unhandled execution mode");
3867 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3868 const uint32_t *w
, unsigned count
)
3870 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
3874 case SpvOpSourceContinued
:
3875 case SpvOpSourceExtension
:
3876 case SpvOpExtension
:
3877 case SpvOpCapability
:
3878 case SpvOpExtInstImport
:
3879 case SpvOpMemoryModel
:
3880 case SpvOpEntryPoint
:
3881 case SpvOpExecutionMode
:
3884 case SpvOpMemberName
:
3885 case SpvOpDecorationGroup
:
3887 case SpvOpMemberDecorate
:
3888 case SpvOpGroupDecorate
:
3889 case SpvOpGroupMemberDecorate
:
3890 case SpvOpDecorateStringGOOGLE
:
3891 case SpvOpMemberDecorateStringGOOGLE
:
3892 vtn_fail("Invalid opcode types and variables section");
3898 case SpvOpTypeFloat
:
3899 case SpvOpTypeVector
:
3900 case SpvOpTypeMatrix
:
3901 case SpvOpTypeImage
:
3902 case SpvOpTypeSampler
:
3903 case SpvOpTypeSampledImage
:
3904 case SpvOpTypeArray
:
3905 case SpvOpTypeRuntimeArray
:
3906 case SpvOpTypeStruct
:
3907 case SpvOpTypeOpaque
:
3908 case SpvOpTypePointer
:
3909 case SpvOpTypeFunction
:
3910 case SpvOpTypeEvent
:
3911 case SpvOpTypeDeviceEvent
:
3912 case SpvOpTypeReserveId
:
3913 case SpvOpTypeQueue
:
3915 vtn_handle_type(b
, opcode
, w
, count
);
3918 case SpvOpConstantTrue
:
3919 case SpvOpConstantFalse
:
3921 case SpvOpConstantComposite
:
3922 case SpvOpConstantSampler
:
3923 case SpvOpConstantNull
:
3924 case SpvOpSpecConstantTrue
:
3925 case SpvOpSpecConstantFalse
:
3926 case SpvOpSpecConstant
:
3927 case SpvOpSpecConstantComposite
:
3928 case SpvOpSpecConstantOp
:
3929 vtn_handle_constant(b
, opcode
, w
, count
);
3934 vtn_handle_variables(b
, opcode
, w
, count
);
3938 return false; /* End of preamble */
3945 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3946 const uint32_t *w
, unsigned count
)
3952 case SpvOpLoopMerge
:
3953 case SpvOpSelectionMerge
:
3954 /* This is handled by cfg pre-pass and walk_blocks */
3958 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
3959 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3964 vtn_handle_extension(b
, opcode
, w
, count
);
3970 case SpvOpCopyMemory
:
3971 case SpvOpCopyMemorySized
:
3972 case SpvOpAccessChain
:
3973 case SpvOpPtrAccessChain
:
3974 case SpvOpInBoundsAccessChain
:
3975 case SpvOpArrayLength
:
3976 vtn_handle_variables(b
, opcode
, w
, count
);
3979 case SpvOpFunctionCall
:
3980 vtn_handle_function_call(b
, opcode
, w
, count
);
3983 case SpvOpSampledImage
:
3985 case SpvOpImageSampleImplicitLod
:
3986 case SpvOpImageSampleExplicitLod
:
3987 case SpvOpImageSampleDrefImplicitLod
:
3988 case SpvOpImageSampleDrefExplicitLod
:
3989 case SpvOpImageSampleProjImplicitLod
:
3990 case SpvOpImageSampleProjExplicitLod
:
3991 case SpvOpImageSampleProjDrefImplicitLod
:
3992 case SpvOpImageSampleProjDrefExplicitLod
:
3993 case SpvOpImageFetch
:
3994 case SpvOpImageGather
:
3995 case SpvOpImageDrefGather
:
3996 case SpvOpImageQuerySizeLod
:
3997 case SpvOpImageQueryLod
:
3998 case SpvOpImageQueryLevels
:
3999 case SpvOpImageQuerySamples
:
4000 vtn_handle_texture(b
, opcode
, w
, count
);
4003 case SpvOpImageRead
:
4004 case SpvOpImageWrite
:
4005 case SpvOpImageTexelPointer
:
4006 vtn_handle_image(b
, opcode
, w
, count
);
4009 case SpvOpImageQuerySize
: {
4010 struct vtn_pointer
*image
=
4011 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
4012 if (glsl_type_is_image(image
->type
->type
)) {
4013 vtn_handle_image(b
, opcode
, w
, count
);
4015 vtn_assert(glsl_type_is_sampler(image
->type
->type
));
4016 vtn_handle_texture(b
, opcode
, w
, count
);
4021 case SpvOpAtomicLoad
:
4022 case SpvOpAtomicExchange
:
4023 case SpvOpAtomicCompareExchange
:
4024 case SpvOpAtomicCompareExchangeWeak
:
4025 case SpvOpAtomicIIncrement
:
4026 case SpvOpAtomicIDecrement
:
4027 case SpvOpAtomicIAdd
:
4028 case SpvOpAtomicISub
:
4029 case SpvOpAtomicSMin
:
4030 case SpvOpAtomicUMin
:
4031 case SpvOpAtomicSMax
:
4032 case SpvOpAtomicUMax
:
4033 case SpvOpAtomicAnd
:
4035 case SpvOpAtomicXor
: {
4036 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
4037 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4038 vtn_handle_image(b
, opcode
, w
, count
);
4040 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4041 vtn_handle_atomics(b
, opcode
, w
, count
);
4046 case SpvOpAtomicStore
: {
4047 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
4048 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4049 vtn_handle_image(b
, opcode
, w
, count
);
4051 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4052 vtn_handle_atomics(b
, opcode
, w
, count
);
4058 /* Handle OpSelect up-front here because it needs to be able to handle
4059 * pointers and not just regular vectors and scalars.
4061 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
4062 struct vtn_value
*sel_val
= vtn_untyped_value(b
, w
[3]);
4063 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
4064 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
4066 const struct glsl_type
*sel_type
;
4067 switch (res_val
->type
->base_type
) {
4068 case vtn_base_type_scalar
:
4069 sel_type
= glsl_bool_type();
4071 case vtn_base_type_vector
:
4072 sel_type
= glsl_vector_type(GLSL_TYPE_BOOL
, res_val
->type
->length
);
4074 case vtn_base_type_pointer
:
4075 /* We need to have actual storage for pointer types */
4076 vtn_fail_if(res_val
->type
->type
== NULL
,
4077 "Invalid pointer result type for OpSelect");
4078 sel_type
= glsl_bool_type();
4081 vtn_fail("Result type of OpSelect must be a scalar, vector, or pointer");
4084 if (unlikely(sel_val
->type
->type
!= sel_type
)) {
4085 if (sel_val
->type
->type
== glsl_bool_type()) {
4086 /* This case is illegal but some older versions of GLSLang produce
4087 * it. The GLSLang issue was fixed on March 30, 2017:
4089 * https://github.com/KhronosGroup/glslang/issues/809
4091 * Unfortunately, there are applications in the wild which are
4092 * shipping with this bug so it isn't nice to fail on them so we
4093 * throw a warning instead. It's not actually a problem for us as
4094 * nir_builder will just splat the condition out which is most
4095 * likely what the client wanted anyway.
4097 vtn_warn("Condition type of OpSelect must have the same number "
4098 "of components as Result Type");
4100 vtn_fail("Condition type of OpSelect must be a scalar or vector "
4101 "of Boolean type. It must have the same number of "
4102 "components as Result Type");
4106 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4107 obj2_val
->type
!= res_val
->type
,
4108 "Object types must match the result type in OpSelect");
4110 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4111 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, res_type
->type
);
4112 ssa
->def
= nir_bcsel(&b
->nb
, vtn_ssa_value(b
, w
[3])->def
,
4113 vtn_ssa_value(b
, w
[4])->def
,
4114 vtn_ssa_value(b
, w
[5])->def
);
4115 vtn_push_ssa(b
, w
[2], res_type
, ssa
);
4124 case SpvOpConvertFToU
:
4125 case SpvOpConvertFToS
:
4126 case SpvOpConvertSToF
:
4127 case SpvOpConvertUToF
:
4131 case SpvOpQuantizeToF16
:
4132 case SpvOpConvertPtrToU
:
4133 case SpvOpConvertUToPtr
:
4134 case SpvOpPtrCastToGeneric
:
4135 case SpvOpGenericCastToPtr
:
4141 case SpvOpSignBitSet
:
4142 case SpvOpLessOrGreater
:
4144 case SpvOpUnordered
:
4159 case SpvOpVectorTimesScalar
:
4161 case SpvOpIAddCarry
:
4162 case SpvOpISubBorrow
:
4163 case SpvOpUMulExtended
:
4164 case SpvOpSMulExtended
:
4165 case SpvOpShiftRightLogical
:
4166 case SpvOpShiftRightArithmetic
:
4167 case SpvOpShiftLeftLogical
:
4168 case SpvOpLogicalEqual
:
4169 case SpvOpLogicalNotEqual
:
4170 case SpvOpLogicalOr
:
4171 case SpvOpLogicalAnd
:
4172 case SpvOpLogicalNot
:
4173 case SpvOpBitwiseOr
:
4174 case SpvOpBitwiseXor
:
4175 case SpvOpBitwiseAnd
:
4177 case SpvOpFOrdEqual
:
4178 case SpvOpFUnordEqual
:
4179 case SpvOpINotEqual
:
4180 case SpvOpFOrdNotEqual
:
4181 case SpvOpFUnordNotEqual
:
4182 case SpvOpULessThan
:
4183 case SpvOpSLessThan
:
4184 case SpvOpFOrdLessThan
:
4185 case SpvOpFUnordLessThan
:
4186 case SpvOpUGreaterThan
:
4187 case SpvOpSGreaterThan
:
4188 case SpvOpFOrdGreaterThan
:
4189 case SpvOpFUnordGreaterThan
:
4190 case SpvOpULessThanEqual
:
4191 case SpvOpSLessThanEqual
:
4192 case SpvOpFOrdLessThanEqual
:
4193 case SpvOpFUnordLessThanEqual
:
4194 case SpvOpUGreaterThanEqual
:
4195 case SpvOpSGreaterThanEqual
:
4196 case SpvOpFOrdGreaterThanEqual
:
4197 case SpvOpFUnordGreaterThanEqual
:
4203 case SpvOpFwidthFine
:
4204 case SpvOpDPdxCoarse
:
4205 case SpvOpDPdyCoarse
:
4206 case SpvOpFwidthCoarse
:
4207 case SpvOpBitFieldInsert
:
4208 case SpvOpBitFieldSExtract
:
4209 case SpvOpBitFieldUExtract
:
4210 case SpvOpBitReverse
:
4212 case SpvOpTranspose
:
4213 case SpvOpOuterProduct
:
4214 case SpvOpMatrixTimesScalar
:
4215 case SpvOpVectorTimesMatrix
:
4216 case SpvOpMatrixTimesVector
:
4217 case SpvOpMatrixTimesMatrix
:
4218 vtn_handle_alu(b
, opcode
, w
, count
);
4221 case SpvOpVectorExtractDynamic
:
4222 case SpvOpVectorInsertDynamic
:
4223 case SpvOpVectorShuffle
:
4224 case SpvOpCompositeConstruct
:
4225 case SpvOpCompositeExtract
:
4226 case SpvOpCompositeInsert
:
4227 case SpvOpCopyObject
:
4228 vtn_handle_composite(b
, opcode
, w
, count
);
4231 case SpvOpEmitVertex
:
4232 case SpvOpEndPrimitive
:
4233 case SpvOpEmitStreamVertex
:
4234 case SpvOpEndStreamPrimitive
:
4235 case SpvOpControlBarrier
:
4236 case SpvOpMemoryBarrier
:
4237 vtn_handle_barrier(b
, opcode
, w
, count
);
4240 case SpvOpGroupNonUniformElect
:
4241 case SpvOpGroupNonUniformAll
:
4242 case SpvOpGroupNonUniformAny
:
4243 case SpvOpGroupNonUniformAllEqual
:
4244 case SpvOpGroupNonUniformBroadcast
:
4245 case SpvOpGroupNonUniformBroadcastFirst
:
4246 case SpvOpGroupNonUniformBallot
:
4247 case SpvOpGroupNonUniformInverseBallot
:
4248 case SpvOpGroupNonUniformBallotBitExtract
:
4249 case SpvOpGroupNonUniformBallotBitCount
:
4250 case SpvOpGroupNonUniformBallotFindLSB
:
4251 case SpvOpGroupNonUniformBallotFindMSB
:
4252 case SpvOpGroupNonUniformShuffle
:
4253 case SpvOpGroupNonUniformShuffleXor
:
4254 case SpvOpGroupNonUniformShuffleUp
:
4255 case SpvOpGroupNonUniformShuffleDown
:
4256 case SpvOpGroupNonUniformIAdd
:
4257 case SpvOpGroupNonUniformFAdd
:
4258 case SpvOpGroupNonUniformIMul
:
4259 case SpvOpGroupNonUniformFMul
:
4260 case SpvOpGroupNonUniformSMin
:
4261 case SpvOpGroupNonUniformUMin
:
4262 case SpvOpGroupNonUniformFMin
:
4263 case SpvOpGroupNonUniformSMax
:
4264 case SpvOpGroupNonUniformUMax
:
4265 case SpvOpGroupNonUniformFMax
:
4266 case SpvOpGroupNonUniformBitwiseAnd
:
4267 case SpvOpGroupNonUniformBitwiseOr
:
4268 case SpvOpGroupNonUniformBitwiseXor
:
4269 case SpvOpGroupNonUniformLogicalAnd
:
4270 case SpvOpGroupNonUniformLogicalOr
:
4271 case SpvOpGroupNonUniformLogicalXor
:
4272 case SpvOpGroupNonUniformQuadBroadcast
:
4273 case SpvOpGroupNonUniformQuadSwap
:
4274 vtn_handle_subgroup(b
, opcode
, w
, count
);
4278 vtn_fail("Unhandled opcode");
4285 vtn_create_builder(const uint32_t *words
, size_t word_count
,
4286 gl_shader_stage stage
, const char *entry_point_name
,
4287 const struct spirv_to_nir_options
*options
)
4289 /* Initialize the vtn_builder object */
4290 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
4292 b
->spirv_word_count
= word_count
;
4296 exec_list_make_empty(&b
->functions
);
4297 b
->entry_point_stage
= stage
;
4298 b
->entry_point_name
= entry_point_name
;
4299 b
->options
= options
;
4302 * Handle the SPIR-V header (first 5 dwords).
4303 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
4305 if (word_count
<= 5)
4308 if (words
[0] != SpvMagicNumber
) {
4309 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
4312 if (words
[1] < 0x10000) {
4313 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
4317 /* words[2] == generator magic */
4318 unsigned value_id_bound
= words
[3];
4319 if (words
[4] != 0) {
4320 vtn_err("words[4] was %u, want 0", words
[4]);
4324 b
->value_id_bound
= value_id_bound
;
4325 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
4334 spirv_to_nir(const uint32_t *words
, size_t word_count
,
4335 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
4336 gl_shader_stage stage
, const char *entry_point_name
,
4337 const struct spirv_to_nir_options
*options
,
4338 const nir_shader_compiler_options
*nir_options
)
4341 const uint32_t *word_end
= words
+ word_count
;
4343 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
4344 stage
, entry_point_name
,
4350 /* See also _vtn_fail() */
4351 if (setjmp(b
->fail_jump
)) {
4356 /* Skip the SPIR-V header, handled at vtn_create_builder */
4359 /* Handle all the preamble instructions */
4360 words
= vtn_foreach_instruction(b
, words
, word_end
,
4361 vtn_handle_preamble_instruction
);
4363 if (b
->entry_point
== NULL
) {
4364 vtn_fail("Entry point not found");
4369 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
4371 /* Set shader info defaults */
4372 b
->shader
->info
.gs
.invocations
= 1;
4374 /* Parse execution modes */
4375 vtn_foreach_execution_mode(b
, b
->entry_point
,
4376 vtn_handle_execution_mode
, NULL
);
4378 b
->specializations
= spec
;
4379 b
->num_specializations
= num_spec
;
4381 /* Handle all variable, type, and constant instructions */
4382 words
= vtn_foreach_instruction(b
, words
, word_end
,
4383 vtn_handle_variable_or_type_instruction
);
4385 /* Set types on all vtn_values */
4386 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
4388 vtn_build_cfg(b
, words
, word_end
);
4390 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4391 b
->entry_point
->func
->referenced
= true;
4396 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
4397 if (func
->referenced
&& !func
->emitted
) {
4398 b
->const_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
4399 _mesa_key_pointer_equal
);
4401 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
4407 /* We sometimes generate bogus derefs that, while never used, give the
4408 * validator a bit of heartburn. Run dead code to get rid of them.
4410 nir_opt_dce(b
->shader
);
4412 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4413 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
4414 vtn_assert(entry_point
);
4416 /* Unparent the shader from the vtn_builder before we delete the builder */
4417 ralloc_steal(NULL
, b
->shader
);