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 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
824 vtn_warn("Decoration only allowed for CL-style kernels: %s",
825 spirv_decoration_to_string(dec
->decoration
));
829 case SpvDecorationHlslSemanticGOOGLE
:
830 /* HLSL semantic decorations can safely be ignored by the driver. */
834 vtn_fail("Unhandled decoration");
838 /** Chases the array type all the way down to the tail and rewrites the
839 * glsl_types to be based off the tail's glsl_type.
842 vtn_array_type_rewrite_glsl_type(struct vtn_type
*type
)
844 if (type
->base_type
!= vtn_base_type_array
)
847 vtn_array_type_rewrite_glsl_type(type
->array_element
);
849 type
->type
= glsl_array_type(type
->array_element
->type
,
850 type
->length
, type
->stride
);
853 /* Matrix strides are handled as a separate pass because we need to know
854 * whether the matrix is row-major or not first.
857 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
858 struct vtn_value
*val
, int member
,
859 const struct vtn_decoration
*dec
,
862 if (dec
->decoration
!= SpvDecorationMatrixStride
)
865 vtn_fail_if(member
< 0,
866 "The MatrixStride decoration is only allowed on members "
868 vtn_fail_if(dec
->literals
[0] == 0, "MatrixStride must be non-zero");
870 struct member_decoration_ctx
*ctx
= void_ctx
;
872 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
873 if (mat_type
->row_major
) {
874 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
875 mat_type
->stride
= mat_type
->array_element
->stride
;
876 mat_type
->array_element
->stride
= dec
->literals
[0];
878 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
879 dec
->literals
[0], true);
880 mat_type
->array_element
->type
= glsl_get_column_type(mat_type
->type
);
882 vtn_assert(mat_type
->array_element
->stride
> 0);
883 mat_type
->stride
= dec
->literals
[0];
885 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
886 dec
->literals
[0], false);
889 /* Now that we've replaced the glsl_type with a properly strided matrix
890 * type, rewrite the member type so that it's an array of the proper kind
893 vtn_array_type_rewrite_glsl_type(ctx
->type
->members
[member
]);
894 ctx
->fields
[member
].type
= ctx
->type
->members
[member
]->type
;
898 type_decoration_cb(struct vtn_builder
*b
,
899 struct vtn_value
*val
, int member
,
900 const struct vtn_decoration
*dec
, void *ctx
)
902 struct vtn_type
*type
= val
->type
;
905 /* This should have been handled by OpTypeStruct */
906 assert(val
->type
->base_type
== vtn_base_type_struct
);
907 assert(member
>= 0 && member
< val
->type
->length
);
911 switch (dec
->decoration
) {
912 case SpvDecorationArrayStride
:
913 vtn_assert(type
->base_type
== vtn_base_type_array
||
914 type
->base_type
== vtn_base_type_pointer
);
916 case SpvDecorationBlock
:
917 vtn_assert(type
->base_type
== vtn_base_type_struct
);
920 case SpvDecorationBufferBlock
:
921 vtn_assert(type
->base_type
== vtn_base_type_struct
);
922 type
->buffer_block
= true;
924 case SpvDecorationGLSLShared
:
925 case SpvDecorationGLSLPacked
:
926 /* Ignore these, since we get explicit offsets anyways */
929 case SpvDecorationRowMajor
:
930 case SpvDecorationColMajor
:
931 case SpvDecorationMatrixStride
:
932 case SpvDecorationBuiltIn
:
933 case SpvDecorationNoPerspective
:
934 case SpvDecorationFlat
:
935 case SpvDecorationPatch
:
936 case SpvDecorationCentroid
:
937 case SpvDecorationSample
:
938 case SpvDecorationVolatile
:
939 case SpvDecorationCoherent
:
940 case SpvDecorationNonWritable
:
941 case SpvDecorationNonReadable
:
942 case SpvDecorationUniform
:
943 case SpvDecorationLocation
:
944 case SpvDecorationComponent
:
945 case SpvDecorationOffset
:
946 case SpvDecorationXfbBuffer
:
947 case SpvDecorationXfbStride
:
948 case SpvDecorationHlslSemanticGOOGLE
:
949 vtn_warn("Decoration only allowed for struct members: %s",
950 spirv_decoration_to_string(dec
->decoration
));
953 case SpvDecorationStream
:
954 /* We don't need to do anything here, as stream is filled up when
955 * aplying the decoration to a variable, just check that if it is not a
956 * struct member, it should be a struct.
958 vtn_assert(type
->base_type
== vtn_base_type_struct
);
961 case SpvDecorationRelaxedPrecision
:
962 case SpvDecorationSpecId
:
963 case SpvDecorationInvariant
:
964 case SpvDecorationRestrict
:
965 case SpvDecorationAliased
:
966 case SpvDecorationConstant
:
967 case SpvDecorationIndex
:
968 case SpvDecorationBinding
:
969 case SpvDecorationDescriptorSet
:
970 case SpvDecorationLinkageAttributes
:
971 case SpvDecorationNoContraction
:
972 case SpvDecorationInputAttachmentIndex
:
973 vtn_warn("Decoration not allowed on types: %s",
974 spirv_decoration_to_string(dec
->decoration
));
977 case SpvDecorationCPacked
:
978 case SpvDecorationSaturatedConversion
:
979 case SpvDecorationFuncParamAttr
:
980 case SpvDecorationFPRoundingMode
:
981 case SpvDecorationFPFastMathMode
:
982 case SpvDecorationAlignment
:
983 vtn_warn("Decoration only allowed for CL-style kernels: %s",
984 spirv_decoration_to_string(dec
->decoration
));
988 vtn_fail("Unhandled decoration");
993 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
996 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
997 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
998 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
999 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
1000 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
1001 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
1002 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
1003 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
1004 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
1005 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
1006 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
1007 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
1008 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
1009 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
1010 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
1011 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
1012 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
1013 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
1014 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
1015 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
1016 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
1017 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
1018 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
1019 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
1020 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
1021 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
1022 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
1023 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
1024 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
1025 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
1026 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
1027 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
1028 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
1029 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
1030 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
1031 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
1032 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
1033 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
1034 case SpvImageFormatR16ui
: return 0x8234; /* GL_R16UI */
1035 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
1037 vtn_fail("Invalid image format");
1041 static struct vtn_type
*
1042 vtn_type_layout_std430(struct vtn_builder
*b
, struct vtn_type
*type
,
1043 uint32_t *size_out
, uint32_t *align_out
)
1045 switch (type
->base_type
) {
1046 case vtn_base_type_scalar
: {
1047 uint32_t comp_size
= glsl_type_is_boolean(type
->type
)
1048 ? 4 : glsl_get_bit_size(type
->type
) / 8;
1049 *size_out
= comp_size
;
1050 *align_out
= comp_size
;
1054 case vtn_base_type_vector
: {
1055 uint32_t comp_size
= glsl_type_is_boolean(type
->type
)
1056 ? 4 : glsl_get_bit_size(type
->type
) / 8;
1057 unsigned align_comps
= type
->length
== 3 ? 4 : type
->length
;
1058 *size_out
= comp_size
* type
->length
,
1059 *align_out
= comp_size
* align_comps
;
1063 case vtn_base_type_matrix
:
1064 case vtn_base_type_array
: {
1065 /* We're going to add an array stride */
1066 type
= vtn_type_copy(b
, type
);
1067 uint32_t elem_size
, elem_align
;
1068 type
->array_element
= vtn_type_layout_std430(b
, type
->array_element
,
1069 &elem_size
, &elem_align
);
1070 type
->stride
= vtn_align_u32(elem_size
, elem_align
);
1071 *size_out
= type
->stride
* type
->length
;
1072 *align_out
= elem_align
;
1076 case vtn_base_type_struct
: {
1077 /* We're going to add member offsets */
1078 type
= vtn_type_copy(b
, type
);
1079 uint32_t offset
= 0;
1081 for (unsigned i
= 0; i
< type
->length
; i
++) {
1082 uint32_t mem_size
, mem_align
;
1083 type
->members
[i
] = vtn_type_layout_std430(b
, type
->members
[i
],
1084 &mem_size
, &mem_align
);
1085 offset
= vtn_align_u32(offset
, mem_align
);
1086 type
->offsets
[i
] = offset
;
1088 align
= MAX2(align
, mem_align
);
1096 unreachable("Invalid SPIR-V type for std430");
1101 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
1102 const uint32_t *w
, unsigned count
)
1104 struct vtn_value
*val
= NULL
;
1106 /* In order to properly handle forward declarations, we have to defer
1107 * allocation for pointer types.
1109 if (opcode
!= SpvOpTypePointer
&& opcode
!= SpvOpTypeForwardPointer
) {
1110 val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1111 vtn_fail_if(val
->type
!= NULL
,
1112 "Only pointers can have forward declarations");
1113 val
->type
= rzalloc(b
, struct vtn_type
);
1114 val
->type
->id
= w
[1];
1119 val
->type
->base_type
= vtn_base_type_void
;
1120 val
->type
->type
= glsl_void_type();
1123 val
->type
->base_type
= vtn_base_type_scalar
;
1124 val
->type
->type
= glsl_bool_type();
1125 val
->type
->length
= 1;
1127 case SpvOpTypeInt
: {
1128 int bit_size
= w
[2];
1129 const bool signedness
= w
[3];
1130 val
->type
->base_type
= vtn_base_type_scalar
;
1133 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1136 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1139 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1142 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1145 vtn_fail("Invalid int bit size");
1147 val
->type
->length
= 1;
1151 case SpvOpTypeFloat
: {
1152 int bit_size
= w
[2];
1153 val
->type
->base_type
= vtn_base_type_scalar
;
1156 val
->type
->type
= glsl_float16_t_type();
1159 val
->type
->type
= glsl_float_type();
1162 val
->type
->type
= glsl_double_type();
1165 vtn_fail("Invalid float bit size");
1167 val
->type
->length
= 1;
1171 case SpvOpTypeVector
: {
1172 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1173 unsigned elems
= w
[3];
1175 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1176 "Base type for OpTypeVector must be a scalar");
1177 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1178 "Invalid component count for OpTypeVector");
1180 val
->type
->base_type
= vtn_base_type_vector
;
1181 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1182 val
->type
->length
= elems
;
1183 val
->type
->stride
= glsl_type_is_boolean(val
->type
->type
)
1184 ? 4 : glsl_get_bit_size(base
->type
) / 8;
1185 val
->type
->array_element
= base
;
1189 case SpvOpTypeMatrix
: {
1190 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1191 unsigned columns
= w
[3];
1193 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1194 "Base type for OpTypeMatrix must be a vector");
1195 vtn_fail_if(columns
< 2 || columns
> 4,
1196 "Invalid column count for OpTypeMatrix");
1198 val
->type
->base_type
= vtn_base_type_matrix
;
1199 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1200 glsl_get_vector_elements(base
->type
),
1202 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1203 "Unsupported base type for OpTypeMatrix");
1204 assert(!glsl_type_is_error(val
->type
->type
));
1205 val
->type
->length
= columns
;
1206 val
->type
->array_element
= base
;
1207 val
->type
->row_major
= false;
1208 val
->type
->stride
= 0;
1212 case SpvOpTypeRuntimeArray
:
1213 case SpvOpTypeArray
: {
1214 struct vtn_type
*array_element
=
1215 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1217 if (opcode
== SpvOpTypeRuntimeArray
) {
1218 /* A length of 0 is used to denote unsized arrays */
1219 val
->type
->length
= 0;
1222 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
1225 val
->type
->base_type
= vtn_base_type_array
;
1226 val
->type
->array_element
= array_element
;
1227 val
->type
->stride
= 0;
1229 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1230 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
,
1235 case SpvOpTypeStruct
: {
1236 unsigned num_fields
= count
- 2;
1237 val
->type
->base_type
= vtn_base_type_struct
;
1238 val
->type
->length
= num_fields
;
1239 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1240 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1242 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1243 for (unsigned i
= 0; i
< num_fields
; i
++) {
1244 val
->type
->members
[i
] =
1245 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
1246 fields
[i
] = (struct glsl_struct_field
) {
1247 .type
= val
->type
->members
[i
]->type
,
1248 .name
= ralloc_asprintf(b
, "field%d", i
),
1254 struct member_decoration_ctx ctx
= {
1255 .num_fields
= num_fields
,
1260 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1261 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1263 const char *name
= val
->name
? val
->name
: "struct";
1265 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
);
1269 case SpvOpTypeFunction
: {
1270 val
->type
->base_type
= vtn_base_type_function
;
1271 val
->type
->type
= NULL
;
1273 val
->type
->return_type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1275 const unsigned num_params
= count
- 3;
1276 val
->type
->length
= num_params
;
1277 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1278 for (unsigned i
= 0; i
< count
- 3; i
++) {
1279 val
->type
->params
[i
] =
1280 vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
;
1285 case SpvOpTypePointer
:
1286 case SpvOpTypeForwardPointer
: {
1287 /* We can't blindly push the value because it might be a forward
1290 val
= vtn_untyped_value(b
, w
[1]);
1292 SpvStorageClass storage_class
= w
[2];
1294 if (val
->value_type
== vtn_value_type_invalid
) {
1295 val
->value_type
= vtn_value_type_type
;
1296 val
->type
= rzalloc(b
, struct vtn_type
);
1297 val
->type
->id
= w
[1];
1298 val
->type
->base_type
= vtn_base_type_pointer
;
1299 val
->type
->storage_class
= storage_class
;
1301 /* These can actually be stored to nir_variables and used as SSA
1302 * values so they need a real glsl_type.
1304 switch (storage_class
) {
1305 case SpvStorageClassUniform
:
1306 val
->type
->type
= b
->options
->ubo_ptr_type
;
1308 case SpvStorageClassStorageBuffer
:
1309 val
->type
->type
= b
->options
->ssbo_ptr_type
;
1311 case SpvStorageClassPhysicalStorageBufferEXT
:
1312 val
->type
->type
= b
->options
->phys_ssbo_ptr_type
;
1314 case SpvStorageClassPushConstant
:
1315 val
->type
->type
= b
->options
->push_const_ptr_type
;
1317 case SpvStorageClassWorkgroup
:
1318 val
->type
->type
= b
->options
->shared_ptr_type
;
1321 /* In this case, no variable pointers are allowed so all deref
1322 * chains are complete back to the variable and it doesn't matter
1323 * what type gets used so we leave it NULL.
1328 vtn_fail_if(val
->type
->storage_class
!= storage_class
,
1329 "The storage classes of an OpTypePointer and any "
1330 "OpTypeForwardPointers that provide forward "
1331 "declarations of it must match.");
1334 if (opcode
== SpvOpTypePointer
) {
1335 vtn_fail_if(val
->type
->deref
!= NULL
,
1336 "While OpTypeForwardPointer can be used to provide a "
1337 "forward declaration of a pointer, OpTypePointer can "
1338 "only be used once for a given id.");
1340 val
->type
->deref
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
1342 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1344 if (storage_class
== SpvStorageClassWorkgroup
&&
1345 b
->options
->lower_workgroup_access_to_offsets
) {
1346 uint32_t size
, align
;
1347 val
->type
->deref
= vtn_type_layout_std430(b
, val
->type
->deref
,
1349 val
->type
->length
= size
;
1350 val
->type
->align
= align
;
1356 case SpvOpTypeImage
: {
1357 val
->type
->base_type
= vtn_base_type_image
;
1359 const struct vtn_type
*sampled_type
=
1360 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1362 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1363 glsl_get_bit_size(sampled_type
->type
) != 32,
1364 "Sampled type of OpTypeImage must be a 32-bit scalar");
1366 enum glsl_sampler_dim dim
;
1367 switch ((SpvDim
)w
[3]) {
1368 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1369 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1370 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1371 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1372 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1373 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1374 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1376 vtn_fail("Invalid SPIR-V image dimensionality");
1379 bool is_shadow
= w
[4];
1380 bool is_array
= w
[5];
1381 bool multisampled
= w
[6];
1382 unsigned sampled
= w
[7];
1383 SpvImageFormat format
= w
[8];
1386 val
->type
->access_qualifier
= w
[9];
1388 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1391 if (dim
== GLSL_SAMPLER_DIM_2D
)
1392 dim
= GLSL_SAMPLER_DIM_MS
;
1393 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1394 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1396 vtn_fail("Unsupported multisampled image type");
1399 val
->type
->image_format
= translate_image_format(b
, format
);
1401 enum glsl_base_type sampled_base_type
=
1402 glsl_get_base_type(sampled_type
->type
);
1404 val
->type
->sampled
= true;
1405 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
1407 } else if (sampled
== 2) {
1408 vtn_assert(!is_shadow
);
1409 val
->type
->sampled
= false;
1410 val
->type
->type
= glsl_image_type(dim
, is_array
, sampled_base_type
);
1412 vtn_fail("We need to know if the image will be sampled");
1417 case SpvOpTypeSampledImage
:
1418 val
->type
->base_type
= vtn_base_type_sampled_image
;
1419 val
->type
->image
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1420 val
->type
->type
= val
->type
->image
->type
;
1423 case SpvOpTypeSampler
:
1424 /* The actual sampler type here doesn't really matter. It gets
1425 * thrown away the moment you combine it with an image. What really
1426 * matters is that it's a sampler type as opposed to an integer type
1427 * so the backend knows what to do.
1429 val
->type
->base_type
= vtn_base_type_sampler
;
1430 val
->type
->type
= glsl_bare_sampler_type();
1433 case SpvOpTypeOpaque
:
1434 case SpvOpTypeEvent
:
1435 case SpvOpTypeDeviceEvent
:
1436 case SpvOpTypeReserveId
:
1437 case SpvOpTypeQueue
:
1440 vtn_fail("Unhandled opcode");
1443 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1445 if (val
->type
->base_type
== vtn_base_type_struct
&&
1446 (val
->type
->block
|| val
->type
->buffer_block
)) {
1447 for (unsigned i
= 0; i
< val
->type
->length
; i
++) {
1448 vtn_fail_if(vtn_type_contains_block(b
, val
->type
->members
[i
]),
1449 "Block and BufferBlock decorations cannot decorate a "
1450 "structure type that is nested at any level inside "
1451 "another structure type decorated with Block or "
1457 static nir_constant
*
1458 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
1460 nir_constant
*c
= rzalloc(b
, nir_constant
);
1462 /* For pointers and other typeless things, we have to return something but
1463 * it doesn't matter what.
1468 switch (glsl_get_base_type(type
)) {
1470 case GLSL_TYPE_UINT
:
1471 case GLSL_TYPE_INT16
:
1472 case GLSL_TYPE_UINT16
:
1473 case GLSL_TYPE_UINT8
:
1474 case GLSL_TYPE_INT8
:
1475 case GLSL_TYPE_INT64
:
1476 case GLSL_TYPE_UINT64
:
1477 case GLSL_TYPE_BOOL
:
1478 case GLSL_TYPE_FLOAT
:
1479 case GLSL_TYPE_FLOAT16
:
1480 case GLSL_TYPE_DOUBLE
:
1481 /* Nothing to do here. It's already initialized to zero */
1484 case GLSL_TYPE_ARRAY
:
1485 vtn_assert(glsl_get_length(type
) > 0);
1486 c
->num_elements
= glsl_get_length(type
);
1487 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1489 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
1490 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1491 c
->elements
[i
] = c
->elements
[0];
1494 case GLSL_TYPE_STRUCT
:
1495 c
->num_elements
= glsl_get_length(type
);
1496 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1498 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
1499 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
1504 vtn_fail("Invalid type for null constant");
1511 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
1512 int member
, const struct vtn_decoration
*dec
,
1515 vtn_assert(member
== -1);
1516 if (dec
->decoration
!= SpvDecorationSpecId
)
1519 struct spec_constant_value
*const_value
= data
;
1521 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1522 if (b
->specializations
[i
].id
== dec
->literals
[0]) {
1523 if (const_value
->is_double
)
1524 const_value
->data64
= b
->specializations
[i
].data64
;
1526 const_value
->data32
= b
->specializations
[i
].data32
;
1533 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
1534 uint32_t const_value
)
1536 struct spec_constant_value data
;
1537 data
.is_double
= false;
1538 data
.data32
= const_value
;
1539 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1544 get_specialization64(struct vtn_builder
*b
, struct vtn_value
*val
,
1545 uint64_t const_value
)
1547 struct spec_constant_value data
;
1548 data
.is_double
= true;
1549 data
.data64
= const_value
;
1550 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1555 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1556 struct vtn_value
*val
,
1558 const struct vtn_decoration
*dec
,
1561 vtn_assert(member
== -1);
1562 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1563 dec
->literals
[0] != SpvBuiltInWorkgroupSize
)
1566 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1568 b
->shader
->info
.cs
.local_size
[0] = val
->constant
->values
[0].u32
[0];
1569 b
->shader
->info
.cs
.local_size
[1] = val
->constant
->values
[0].u32
[1];
1570 b
->shader
->info
.cs
.local_size
[2] = val
->constant
->values
[0].u32
[2];
1574 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1575 const uint32_t *w
, unsigned count
)
1577 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1578 val
->constant
= rzalloc(b
, nir_constant
);
1580 case SpvOpConstantTrue
:
1581 case SpvOpConstantFalse
:
1582 case SpvOpSpecConstantTrue
:
1583 case SpvOpSpecConstantFalse
: {
1584 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1585 "Result type of %s must be OpTypeBool",
1586 spirv_op_to_string(opcode
));
1588 uint32_t int_val
= (opcode
== SpvOpConstantTrue
||
1589 opcode
== SpvOpSpecConstantTrue
);
1591 if (opcode
== SpvOpSpecConstantTrue
||
1592 opcode
== SpvOpSpecConstantFalse
)
1593 int_val
= get_specialization(b
, val
, int_val
);
1595 val
->constant
->values
[0].b
[0] = int_val
!= 0;
1599 case SpvOpConstant
: {
1600 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1601 "Result type of %s must be a scalar",
1602 spirv_op_to_string(opcode
));
1603 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1606 val
->constant
->values
->u64
[0] = vtn_u64_literal(&w
[3]);
1609 val
->constant
->values
->u32
[0] = w
[3];
1612 val
->constant
->values
->u16
[0] = w
[3];
1615 val
->constant
->values
->u8
[0] = w
[3];
1618 vtn_fail("Unsupported SpvOpConstant bit size");
1623 case SpvOpSpecConstant
: {
1624 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1625 "Result type of %s must be a scalar",
1626 spirv_op_to_string(opcode
));
1627 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1630 val
->constant
->values
[0].u64
[0] =
1631 get_specialization64(b
, val
, vtn_u64_literal(&w
[3]));
1634 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1637 val
->constant
->values
[0].u16
[0] = get_specialization(b
, val
, w
[3]);
1640 val
->constant
->values
[0].u8
[0] = get_specialization(b
, val
, w
[3]);
1643 vtn_fail("Unsupported SpvOpSpecConstant bit size");
1648 case SpvOpSpecConstantComposite
:
1649 case SpvOpConstantComposite
: {
1650 unsigned elem_count
= count
- 3;
1651 vtn_fail_if(elem_count
!= val
->type
->length
,
1652 "%s has %u constituents, expected %u",
1653 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1655 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1656 for (unsigned i
= 0; i
< elem_count
; i
++) {
1657 struct vtn_value
*val
= vtn_untyped_value(b
, w
[i
+ 3]);
1659 if (val
->value_type
== vtn_value_type_constant
) {
1660 elems
[i
] = val
->constant
;
1662 vtn_fail_if(val
->value_type
!= vtn_value_type_undef
,
1663 "only constants or undefs allowed for "
1664 "SpvOpConstantComposite");
1665 /* to make it easier, just insert a NULL constant for now */
1666 elems
[i
] = vtn_null_constant(b
, val
->type
->type
);
1670 switch (val
->type
->base_type
) {
1671 case vtn_base_type_vector
: {
1672 assert(glsl_type_is_vector(val
->type
->type
));
1673 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1674 for (unsigned i
= 0; i
< elem_count
; i
++) {
1677 val
->constant
->values
[0].u64
[i
] = elems
[i
]->values
[0].u64
[0];
1680 val
->constant
->values
[0].u32
[i
] = elems
[i
]->values
[0].u32
[0];
1683 val
->constant
->values
[0].u16
[i
] = elems
[i
]->values
[0].u16
[0];
1686 val
->constant
->values
[0].u8
[i
] = elems
[i
]->values
[0].u8
[0];
1689 val
->constant
->values
[0].b
[i
] = elems
[i
]->values
[0].b
[0];
1692 vtn_fail("Invalid SpvOpConstantComposite bit size");
1698 case vtn_base_type_matrix
:
1699 assert(glsl_type_is_matrix(val
->type
->type
));
1700 for (unsigned i
= 0; i
< elem_count
; i
++)
1701 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1704 case vtn_base_type_struct
:
1705 case vtn_base_type_array
:
1706 ralloc_steal(val
->constant
, elems
);
1707 val
->constant
->num_elements
= elem_count
;
1708 val
->constant
->elements
= elems
;
1712 vtn_fail("Result type of %s must be a composite type",
1713 spirv_op_to_string(opcode
));
1718 case SpvOpSpecConstantOp
: {
1719 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1721 case SpvOpVectorShuffle
: {
1722 struct vtn_value
*v0
= &b
->values
[w
[4]];
1723 struct vtn_value
*v1
= &b
->values
[w
[5]];
1725 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1726 v0
->value_type
== vtn_value_type_undef
);
1727 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1728 v1
->value_type
== vtn_value_type_undef
);
1730 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1731 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1733 vtn_assert(len0
+ len1
< 16);
1735 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1736 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1737 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1739 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1740 (void)bit_size0
; (void)bit_size1
;
1742 if (bit_size
== 64) {
1744 if (v0
->value_type
== vtn_value_type_constant
) {
1745 for (unsigned i
= 0; i
< len0
; i
++)
1746 u64
[i
] = v0
->constant
->values
[0].u64
[i
];
1748 if (v1
->value_type
== vtn_value_type_constant
) {
1749 for (unsigned i
= 0; i
< len1
; i
++)
1750 u64
[len0
+ i
] = v1
->constant
->values
[0].u64
[i
];
1753 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1754 uint32_t comp
= w
[i
+ 6];
1755 /* If component is not used, set the value to a known constant
1756 * to detect if it is wrongly used.
1758 if (comp
== (uint32_t)-1)
1759 val
->constant
->values
[0].u64
[j
] = 0xdeadbeefdeadbeef;
1761 val
->constant
->values
[0].u64
[j
] = u64
[comp
];
1764 /* This is for both 32-bit and 16-bit values */
1766 if (v0
->value_type
== vtn_value_type_constant
) {
1767 for (unsigned i
= 0; i
< len0
; i
++)
1768 u32
[i
] = v0
->constant
->values
[0].u32
[i
];
1770 if (v1
->value_type
== vtn_value_type_constant
) {
1771 for (unsigned i
= 0; i
< len1
; i
++)
1772 u32
[len0
+ i
] = v1
->constant
->values
[0].u32
[i
];
1775 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1776 uint32_t comp
= w
[i
+ 6];
1777 /* If component is not used, set the value to a known constant
1778 * to detect if it is wrongly used.
1780 if (comp
== (uint32_t)-1)
1781 val
->constant
->values
[0].u32
[j
] = 0xdeadbeef;
1783 val
->constant
->values
[0].u32
[j
] = u32
[comp
];
1789 case SpvOpCompositeExtract
:
1790 case SpvOpCompositeInsert
: {
1791 struct vtn_value
*comp
;
1792 unsigned deref_start
;
1793 struct nir_constant
**c
;
1794 if (opcode
== SpvOpCompositeExtract
) {
1795 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1797 c
= &comp
->constant
;
1799 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1801 val
->constant
= nir_constant_clone(comp
->constant
,
1808 const struct vtn_type
*type
= comp
->type
;
1809 for (unsigned i
= deref_start
; i
< count
; i
++) {
1810 vtn_fail_if(w
[i
] > type
->length
,
1811 "%uth index of %s is %u but the type has only "
1812 "%u elements", i
- deref_start
,
1813 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1815 switch (type
->base_type
) {
1816 case vtn_base_type_vector
:
1818 type
= type
->array_element
;
1821 case vtn_base_type_matrix
:
1822 assert(col
== 0 && elem
== -1);
1825 type
= type
->array_element
;
1828 case vtn_base_type_array
:
1829 c
= &(*c
)->elements
[w
[i
]];
1830 type
= type
->array_element
;
1833 case vtn_base_type_struct
:
1834 c
= &(*c
)->elements
[w
[i
]];
1835 type
= type
->members
[w
[i
]];
1839 vtn_fail("%s must only index into composite types",
1840 spirv_op_to_string(opcode
));
1844 if (opcode
== SpvOpCompositeExtract
) {
1848 unsigned num_components
= type
->length
;
1849 unsigned bit_size
= glsl_get_bit_size(type
->type
);
1850 for (unsigned i
= 0; i
< num_components
; i
++)
1853 val
->constant
->values
[0].u64
[i
] = (*c
)->values
[col
].u64
[elem
+ i
];
1856 val
->constant
->values
[0].u32
[i
] = (*c
)->values
[col
].u32
[elem
+ i
];
1859 val
->constant
->values
[0].u16
[i
] = (*c
)->values
[col
].u16
[elem
+ i
];
1862 val
->constant
->values
[0].u8
[i
] = (*c
)->values
[col
].u8
[elem
+ i
];
1865 val
->constant
->values
[0].b
[i
] = (*c
)->values
[col
].b
[elem
+ i
];
1868 vtn_fail("Invalid SpvOpCompositeExtract bit size");
1872 struct vtn_value
*insert
=
1873 vtn_value(b
, w
[4], vtn_value_type_constant
);
1874 vtn_assert(insert
->type
== type
);
1876 *c
= insert
->constant
;
1878 unsigned num_components
= type
->length
;
1879 unsigned bit_size
= glsl_get_bit_size(type
->type
);
1880 for (unsigned i
= 0; i
< num_components
; i
++)
1883 (*c
)->values
[col
].u64
[elem
+ i
] = insert
->constant
->values
[0].u64
[i
];
1886 (*c
)->values
[col
].u32
[elem
+ i
] = insert
->constant
->values
[0].u32
[i
];
1889 (*c
)->values
[col
].u16
[elem
+ i
] = insert
->constant
->values
[0].u16
[i
];
1892 (*c
)->values
[col
].u8
[elem
+ i
] = insert
->constant
->values
[0].u8
[i
];
1895 (*c
)->values
[col
].b
[elem
+ i
] = insert
->constant
->values
[0].b
[i
];
1898 vtn_fail("Invalid SpvOpCompositeInsert bit size");
1907 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
1908 nir_alu_type src_alu_type
= dst_alu_type
;
1909 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
1912 vtn_assert(count
<= 7);
1917 /* We have a source in a conversion */
1919 nir_get_nir_type_for_glsl_type(
1920 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1921 /* We use the bitsize of the conversion source to evaluate the opcode later */
1922 bit_size
= glsl_get_bit_size(
1923 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1926 bit_size
= glsl_get_bit_size(val
->type
->type
);
1929 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
1930 nir_alu_type_get_type_size(src_alu_type
),
1931 nir_alu_type_get_type_size(dst_alu_type
));
1932 nir_const_value src
[4];
1934 for (unsigned i
= 0; i
< count
- 4; i
++) {
1935 struct vtn_value
*src_val
=
1936 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
);
1938 /* If this is an unsized source, pull the bit size from the
1939 * source; otherwise, we'll use the bit size from the destination.
1941 if (!nir_alu_type_get_type_size(nir_op_infos
[op
].input_types
[i
]))
1942 bit_size
= glsl_get_bit_size(src_val
->type
->type
);
1944 unsigned j
= swap
? 1 - i
: i
;
1945 src
[j
] = src_val
->constant
->values
[0];
1948 /* fix up fixed size sources */
1955 for (unsigned i
= 0; i
< num_components
; ++i
) {
1957 case 64: src
[1].u32
[i
] = src
[1].u64
[i
]; break;
1958 case 16: src
[1].u32
[i
] = src
[1].u16
[i
]; break;
1959 case 8: src
[1].u32
[i
] = src
[1].u8
[i
]; break;
1968 val
->constant
->values
[0] =
1969 nir_eval_const_opcode(op
, num_components
, bit_size
, src
);
1976 case SpvOpConstantNull
:
1977 val
->constant
= vtn_null_constant(b
, val
->type
->type
);
1980 case SpvOpConstantSampler
:
1981 vtn_fail("OpConstantSampler requires Kernel Capability");
1985 vtn_fail("Unhandled opcode");
1988 /* Now that we have the value, update the workgroup size if needed */
1989 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1992 struct vtn_ssa_value
*
1993 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1995 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1998 if (!glsl_type_is_vector_or_scalar(type
)) {
1999 unsigned elems
= glsl_get_length(type
);
2000 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2001 for (unsigned i
= 0; i
< elems
; i
++) {
2002 const struct glsl_type
*child_type
;
2004 switch (glsl_get_base_type(type
)) {
2006 case GLSL_TYPE_UINT
:
2007 case GLSL_TYPE_INT16
:
2008 case GLSL_TYPE_UINT16
:
2009 case GLSL_TYPE_UINT8
:
2010 case GLSL_TYPE_INT8
:
2011 case GLSL_TYPE_INT64
:
2012 case GLSL_TYPE_UINT64
:
2013 case GLSL_TYPE_BOOL
:
2014 case GLSL_TYPE_FLOAT
:
2015 case GLSL_TYPE_FLOAT16
:
2016 case GLSL_TYPE_DOUBLE
:
2017 child_type
= glsl_get_column_type(type
);
2019 case GLSL_TYPE_ARRAY
:
2020 child_type
= glsl_get_array_element(type
);
2022 case GLSL_TYPE_STRUCT
:
2023 child_type
= glsl_get_struct_field(type
, i
);
2026 vtn_fail("unkown base type");
2029 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
2037 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
2040 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
2041 src
.src_type
= type
;
2046 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
2047 const uint32_t *w
, unsigned count
)
2049 if (opcode
== SpvOpSampledImage
) {
2050 struct vtn_value
*val
=
2051 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2052 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2053 val
->sampled_image
->type
=
2054 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2055 val
->sampled_image
->image
=
2056 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2057 val
->sampled_image
->sampler
=
2058 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
2060 } else if (opcode
== SpvOpImage
) {
2061 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2062 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
2063 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
2064 val
->pointer
= src_val
->sampled_image
->image
;
2066 vtn_assert(src_val
->value_type
== vtn_value_type_pointer
);
2067 val
->pointer
= src_val
->pointer
;
2072 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2073 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2075 struct vtn_sampled_image sampled
;
2076 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
2077 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
2078 sampled
= *sampled_val
->sampled_image
;
2080 vtn_assert(sampled_val
->value_type
== vtn_value_type_pointer
);
2081 sampled
.type
= sampled_val
->pointer
->type
;
2082 sampled
.image
= NULL
;
2083 sampled
.sampler
= sampled_val
->pointer
;
2086 const struct glsl_type
*image_type
= sampled
.type
->type
;
2087 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
2088 const bool is_array
= glsl_sampler_type_is_array(image_type
);
2090 /* Figure out the base texture operation */
2093 case SpvOpImageSampleImplicitLod
:
2094 case SpvOpImageSampleDrefImplicitLod
:
2095 case SpvOpImageSampleProjImplicitLod
:
2096 case SpvOpImageSampleProjDrefImplicitLod
:
2097 texop
= nir_texop_tex
;
2100 case SpvOpImageSampleExplicitLod
:
2101 case SpvOpImageSampleDrefExplicitLod
:
2102 case SpvOpImageSampleProjExplicitLod
:
2103 case SpvOpImageSampleProjDrefExplicitLod
:
2104 texop
= nir_texop_txl
;
2107 case SpvOpImageFetch
:
2108 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
2109 texop
= nir_texop_txf_ms
;
2111 texop
= nir_texop_txf
;
2115 case SpvOpImageGather
:
2116 case SpvOpImageDrefGather
:
2117 texop
= nir_texop_tg4
;
2120 case SpvOpImageQuerySizeLod
:
2121 case SpvOpImageQuerySize
:
2122 texop
= nir_texop_txs
;
2125 case SpvOpImageQueryLod
:
2126 texop
= nir_texop_lod
;
2129 case SpvOpImageQueryLevels
:
2130 texop
= nir_texop_query_levels
;
2133 case SpvOpImageQuerySamples
:
2134 texop
= nir_texop_texture_samples
;
2138 vtn_fail("Unhandled opcode");
2141 nir_tex_src srcs
[10]; /* 10 should be enough */
2142 nir_tex_src
*p
= srcs
;
2144 nir_deref_instr
*sampler
= vtn_pointer_to_deref(b
, sampled
.sampler
);
2145 nir_deref_instr
*texture
=
2146 sampled
.image
? vtn_pointer_to_deref(b
, sampled
.image
) : sampler
;
2148 p
->src
= nir_src_for_ssa(&texture
->dest
.ssa
);
2149 p
->src_type
= nir_tex_src_texture_deref
;
2158 /* These operations require a sampler */
2159 p
->src
= nir_src_for_ssa(&sampler
->dest
.ssa
);
2160 p
->src_type
= nir_tex_src_sampler_deref
;
2164 case nir_texop_txf_ms
:
2167 case nir_texop_query_levels
:
2168 case nir_texop_texture_samples
:
2169 case nir_texop_samples_identical
:
2172 case nir_texop_txf_ms_mcs
:
2173 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2178 struct nir_ssa_def
*coord
;
2179 unsigned coord_components
;
2181 case SpvOpImageSampleImplicitLod
:
2182 case SpvOpImageSampleExplicitLod
:
2183 case SpvOpImageSampleDrefImplicitLod
:
2184 case SpvOpImageSampleDrefExplicitLod
:
2185 case SpvOpImageSampleProjImplicitLod
:
2186 case SpvOpImageSampleProjExplicitLod
:
2187 case SpvOpImageSampleProjDrefImplicitLod
:
2188 case SpvOpImageSampleProjDrefExplicitLod
:
2189 case SpvOpImageFetch
:
2190 case SpvOpImageGather
:
2191 case SpvOpImageDrefGather
:
2192 case SpvOpImageQueryLod
: {
2193 /* All these types have the coordinate as their first real argument */
2194 switch (sampler_dim
) {
2195 case GLSL_SAMPLER_DIM_1D
:
2196 case GLSL_SAMPLER_DIM_BUF
:
2197 coord_components
= 1;
2199 case GLSL_SAMPLER_DIM_2D
:
2200 case GLSL_SAMPLER_DIM_RECT
:
2201 case GLSL_SAMPLER_DIM_MS
:
2202 coord_components
= 2;
2204 case GLSL_SAMPLER_DIM_3D
:
2205 case GLSL_SAMPLER_DIM_CUBE
:
2206 coord_components
= 3;
2209 vtn_fail("Invalid sampler type");
2212 if (is_array
&& texop
!= nir_texop_lod
)
2215 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
2216 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2217 (1 << coord_components
) - 1));
2218 p
->src_type
= nir_tex_src_coord
;
2225 coord_components
= 0;
2230 case SpvOpImageSampleProjImplicitLod
:
2231 case SpvOpImageSampleProjExplicitLod
:
2232 case SpvOpImageSampleProjDrefImplicitLod
:
2233 case SpvOpImageSampleProjDrefExplicitLod
:
2234 /* These have the projector as the last coordinate component */
2235 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2236 p
->src_type
= nir_tex_src_projector
;
2244 bool is_shadow
= false;
2245 unsigned gather_component
= 0;
2247 case SpvOpImageSampleDrefImplicitLod
:
2248 case SpvOpImageSampleDrefExplicitLod
:
2249 case SpvOpImageSampleProjDrefImplicitLod
:
2250 case SpvOpImageSampleProjDrefExplicitLod
:
2251 case SpvOpImageDrefGather
:
2252 /* These all have an explicit depth value as their next source */
2254 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2257 case SpvOpImageGather
:
2258 /* This has a component as its next source */
2260 vtn_value(b
, w
[idx
++], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
2267 /* For OpImageQuerySizeLod, we always have an LOD */
2268 if (opcode
== SpvOpImageQuerySizeLod
)
2269 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2271 /* Now we need to handle some number of optional arguments */
2272 const struct vtn_ssa_value
*gather_offsets
= NULL
;
2274 uint32_t operands
= w
[idx
++];
2276 if (operands
& SpvImageOperandsBiasMask
) {
2277 vtn_assert(texop
== nir_texop_tex
);
2278 texop
= nir_texop_txb
;
2279 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
2282 if (operands
& SpvImageOperandsLodMask
) {
2283 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2284 texop
== nir_texop_txs
);
2285 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2288 if (operands
& SpvImageOperandsGradMask
) {
2289 vtn_assert(texop
== nir_texop_txl
);
2290 texop
= nir_texop_txd
;
2291 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
2292 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
2295 if (operands
& SpvImageOperandsOffsetMask
||
2296 operands
& SpvImageOperandsConstOffsetMask
)
2297 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
2299 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2300 nir_tex_src none
= {0};
2301 gather_offsets
= vtn_ssa_value(b
, w
[idx
++]);
2305 if (operands
& SpvImageOperandsSampleMask
) {
2306 vtn_assert(texop
== nir_texop_txf_ms
);
2307 texop
= nir_texop_txf_ms
;
2308 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
2311 if (operands
& SpvImageOperandsMinLodMask
) {
2312 vtn_assert(texop
== nir_texop_tex
||
2313 texop
== nir_texop_txb
||
2314 texop
== nir_texop_txd
);
2315 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_min_lod
);
2318 /* We should have now consumed exactly all of the arguments */
2319 vtn_assert(idx
== count
);
2321 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2324 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2326 instr
->coord_components
= coord_components
;
2327 instr
->sampler_dim
= sampler_dim
;
2328 instr
->is_array
= is_array
;
2329 instr
->is_shadow
= is_shadow
;
2330 instr
->is_new_style_shadow
=
2331 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2332 instr
->component
= gather_component
;
2334 switch (glsl_get_sampler_result_type(image_type
)) {
2335 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
2336 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
2337 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
2338 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
2340 vtn_fail("Invalid base type for sampler result");
2343 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2344 nir_tex_instr_dest_size(instr
), 32, NULL
);
2346 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2347 nir_tex_instr_dest_size(instr
));
2350 nir_instr
*instruction
;
2351 if (gather_offsets
) {
2352 vtn_assert(glsl_get_base_type(gather_offsets
->type
) == GLSL_TYPE_ARRAY
);
2353 vtn_assert(glsl_get_length(gather_offsets
->type
) == 4);
2354 nir_tex_instr
*instrs
[4] = {instr
, NULL
, NULL
, NULL
};
2356 /* Copy the current instruction 4x */
2357 for (uint32_t i
= 1; i
< 4; i
++) {
2358 instrs
[i
] = nir_tex_instr_create(b
->shader
, instr
->num_srcs
);
2359 instrs
[i
]->op
= instr
->op
;
2360 instrs
[i
]->coord_components
= instr
->coord_components
;
2361 instrs
[i
]->sampler_dim
= instr
->sampler_dim
;
2362 instrs
[i
]->is_array
= instr
->is_array
;
2363 instrs
[i
]->is_shadow
= instr
->is_shadow
;
2364 instrs
[i
]->is_new_style_shadow
= instr
->is_new_style_shadow
;
2365 instrs
[i
]->component
= instr
->component
;
2366 instrs
[i
]->dest_type
= instr
->dest_type
;
2368 memcpy(instrs
[i
]->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2370 nir_ssa_dest_init(&instrs
[i
]->instr
, &instrs
[i
]->dest
,
2371 nir_tex_instr_dest_size(instr
), 32, NULL
);
2374 /* Fill in the last argument with the offset from the passed in offsets
2375 * and insert the instruction into the stream.
2377 for (uint32_t i
= 0; i
< 4; i
++) {
2379 src
.src
= nir_src_for_ssa(gather_offsets
->elems
[i
]->def
);
2380 src
.src_type
= nir_tex_src_offset
;
2381 instrs
[i
]->src
[instrs
[i
]->num_srcs
- 1] = src
;
2382 nir_builder_instr_insert(&b
->nb
, &instrs
[i
]->instr
);
2385 /* Combine the results of the 4 instructions by taking their .w
2388 nir_alu_instr
*vec4
= nir_alu_instr_create(b
->shader
, nir_op_vec4
);
2389 nir_ssa_dest_init(&vec4
->instr
, &vec4
->dest
.dest
, 4, 32, NULL
);
2390 vec4
->dest
.write_mask
= 0xf;
2391 for (uint32_t i
= 0; i
< 4; i
++) {
2392 vec4
->src
[i
].src
= nir_src_for_ssa(&instrs
[i
]->dest
.ssa
);
2393 vec4
->src
[i
].swizzle
[0] = 3;
2395 def
= &vec4
->dest
.dest
.ssa
;
2396 instruction
= &vec4
->instr
;
2398 def
= &instr
->dest
.ssa
;
2399 instruction
= &instr
->instr
;
2402 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
2403 val
->ssa
->def
= def
;
2405 nir_builder_instr_insert(&b
->nb
, instruction
);
2409 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2410 const uint32_t *w
, nir_src
*src
)
2413 case SpvOpAtomicIIncrement
:
2414 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
2417 case SpvOpAtomicIDecrement
:
2418 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
2421 case SpvOpAtomicISub
:
2423 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
2426 case SpvOpAtomicCompareExchange
:
2427 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
2428 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
2431 case SpvOpAtomicExchange
:
2432 case SpvOpAtomicIAdd
:
2433 case SpvOpAtomicSMin
:
2434 case SpvOpAtomicUMin
:
2435 case SpvOpAtomicSMax
:
2436 case SpvOpAtomicUMax
:
2437 case SpvOpAtomicAnd
:
2439 case SpvOpAtomicXor
:
2440 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
2444 vtn_fail("Invalid SPIR-V atomic");
2448 static nir_ssa_def
*
2449 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2451 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
2453 /* The image_load_store intrinsics assume a 4-dim coordinate */
2454 unsigned dim
= glsl_get_vector_elements(coord
->type
);
2455 unsigned swizzle
[4];
2456 for (unsigned i
= 0; i
< 4; i
++)
2457 swizzle
[i
] = MIN2(i
, dim
- 1);
2459 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
2462 static nir_ssa_def
*
2463 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2465 if (value
->num_components
== 4)
2469 for (unsigned i
= 0; i
< 4; i
++)
2470 swiz
[i
] = i
< value
->num_components
? i
: 0;
2471 return nir_swizzle(b
, value
, swiz
, 4, false);
2475 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2476 const uint32_t *w
, unsigned count
)
2478 /* Just get this one out of the way */
2479 if (opcode
== SpvOpImageTexelPointer
) {
2480 struct vtn_value
*val
=
2481 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2482 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2484 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2485 val
->image
->coord
= get_image_coord(b
, w
[4]);
2486 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
2490 struct vtn_image_pointer image
;
2493 case SpvOpAtomicExchange
:
2494 case SpvOpAtomicCompareExchange
:
2495 case SpvOpAtomicCompareExchangeWeak
:
2496 case SpvOpAtomicIIncrement
:
2497 case SpvOpAtomicIDecrement
:
2498 case SpvOpAtomicIAdd
:
2499 case SpvOpAtomicISub
:
2500 case SpvOpAtomicLoad
:
2501 case SpvOpAtomicSMin
:
2502 case SpvOpAtomicUMin
:
2503 case SpvOpAtomicSMax
:
2504 case SpvOpAtomicUMax
:
2505 case SpvOpAtomicAnd
:
2507 case SpvOpAtomicXor
:
2508 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
2511 case SpvOpAtomicStore
:
2512 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
2515 case SpvOpImageQuerySize
:
2516 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2518 image
.sample
= NULL
;
2521 case SpvOpImageRead
:
2522 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2523 image
.coord
= get_image_coord(b
, w
[4]);
2525 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
2526 vtn_assert(w
[5] == SpvImageOperandsSampleMask
);
2527 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
2529 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2533 case SpvOpImageWrite
:
2534 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2535 image
.coord
= get_image_coord(b
, w
[2]);
2539 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
2540 vtn_assert(w
[4] == SpvImageOperandsSampleMask
);
2541 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
2543 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2548 vtn_fail("Invalid image opcode");
2551 nir_intrinsic_op op
;
2553 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
2554 OP(ImageQuerySize
, size
)
2556 OP(ImageWrite
, store
)
2557 OP(AtomicLoad
, load
)
2558 OP(AtomicStore
, store
)
2559 OP(AtomicExchange
, atomic_exchange
)
2560 OP(AtomicCompareExchange
, atomic_comp_swap
)
2561 OP(AtomicIIncrement
, atomic_add
)
2562 OP(AtomicIDecrement
, atomic_add
)
2563 OP(AtomicIAdd
, atomic_add
)
2564 OP(AtomicISub
, atomic_add
)
2565 OP(AtomicSMin
, atomic_min
)
2566 OP(AtomicUMin
, atomic_min
)
2567 OP(AtomicSMax
, atomic_max
)
2568 OP(AtomicUMax
, atomic_max
)
2569 OP(AtomicAnd
, atomic_and
)
2570 OP(AtomicOr
, atomic_or
)
2571 OP(AtomicXor
, atomic_xor
)
2574 vtn_fail("Invalid image opcode");
2577 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
2579 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
2580 intrin
->src
[0] = nir_src_for_ssa(&image_deref
->dest
.ssa
);
2582 /* ImageQuerySize doesn't take any extra parameters */
2583 if (opcode
!= SpvOpImageQuerySize
) {
2584 /* The image coordinate is always 4 components but we may not have that
2585 * many. Swizzle to compensate.
2587 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
2588 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
2592 case SpvOpAtomicLoad
:
2593 case SpvOpImageQuerySize
:
2594 case SpvOpImageRead
:
2596 case SpvOpAtomicStore
:
2597 case SpvOpImageWrite
: {
2598 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
2599 nir_ssa_def
*value
= vtn_ssa_value(b
, value_id
)->def
;
2600 /* nir_intrinsic_image_deref_store always takes a vec4 value */
2601 assert(op
== nir_intrinsic_image_deref_store
);
2602 intrin
->num_components
= 4;
2603 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
));
2607 case SpvOpAtomicCompareExchange
:
2608 case SpvOpAtomicIIncrement
:
2609 case SpvOpAtomicIDecrement
:
2610 case SpvOpAtomicExchange
:
2611 case SpvOpAtomicIAdd
:
2612 case SpvOpAtomicISub
:
2613 case SpvOpAtomicSMin
:
2614 case SpvOpAtomicUMin
:
2615 case SpvOpAtomicSMax
:
2616 case SpvOpAtomicUMax
:
2617 case SpvOpAtomicAnd
:
2619 case SpvOpAtomicXor
:
2620 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
2624 vtn_fail("Invalid image opcode");
2627 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
2628 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2629 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2631 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
2632 intrin
->num_components
= nir_intrinsic_infos
[op
].dest_components
;
2633 if (intrin
->num_components
== 0)
2634 intrin
->num_components
= dest_components
;
2636 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2637 intrin
->num_components
, 32, NULL
);
2639 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2641 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
2642 if (intrin
->num_components
!= dest_components
)
2643 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
2645 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
2646 val
->ssa
->def
= result
;
2648 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2652 static nir_intrinsic_op
2653 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2656 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2657 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2658 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2659 OP(AtomicExchange
, atomic_exchange
)
2660 OP(AtomicCompareExchange
, atomic_comp_swap
)
2661 OP(AtomicIIncrement
, atomic_add
)
2662 OP(AtomicIDecrement
, atomic_add
)
2663 OP(AtomicIAdd
, atomic_add
)
2664 OP(AtomicISub
, atomic_add
)
2665 OP(AtomicSMin
, atomic_imin
)
2666 OP(AtomicUMin
, atomic_umin
)
2667 OP(AtomicSMax
, atomic_imax
)
2668 OP(AtomicUMax
, atomic_umax
)
2669 OP(AtomicAnd
, atomic_and
)
2670 OP(AtomicOr
, atomic_or
)
2671 OP(AtomicXor
, atomic_xor
)
2674 vtn_fail("Invalid SSBO atomic");
2678 static nir_intrinsic_op
2679 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2682 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
2683 OP(AtomicLoad
, read_deref
)
2684 OP(AtomicExchange
, exchange
)
2685 OP(AtomicCompareExchange
, comp_swap
)
2686 OP(AtomicIIncrement
, inc_deref
)
2687 OP(AtomicIDecrement
, post_dec_deref
)
2688 OP(AtomicIAdd
, add_deref
)
2689 OP(AtomicISub
, add_deref
)
2690 OP(AtomicUMin
, min_deref
)
2691 OP(AtomicUMax
, max_deref
)
2692 OP(AtomicAnd
, and_deref
)
2693 OP(AtomicOr
, or_deref
)
2694 OP(AtomicXor
, xor_deref
)
2697 /* We left the following out: AtomicStore, AtomicSMin and
2698 * AtomicSmax. Right now there are not nir intrinsics for them. At this
2699 * moment Atomic Counter support is needed for ARB_spirv support, so is
2700 * only need to support GLSL Atomic Counters that are uints and don't
2701 * allow direct storage.
2703 unreachable("Invalid uniform atomic");
2707 static nir_intrinsic_op
2708 get_shared_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2711 case SpvOpAtomicLoad
: return nir_intrinsic_load_shared
;
2712 case SpvOpAtomicStore
: return nir_intrinsic_store_shared
;
2713 #define OP(S, N) case SpvOp##S: return nir_intrinsic_shared_##N;
2714 OP(AtomicExchange
, atomic_exchange
)
2715 OP(AtomicCompareExchange
, atomic_comp_swap
)
2716 OP(AtomicIIncrement
, atomic_add
)
2717 OP(AtomicIDecrement
, atomic_add
)
2718 OP(AtomicIAdd
, atomic_add
)
2719 OP(AtomicISub
, atomic_add
)
2720 OP(AtomicSMin
, atomic_imin
)
2721 OP(AtomicUMin
, atomic_umin
)
2722 OP(AtomicSMax
, atomic_imax
)
2723 OP(AtomicUMax
, atomic_umax
)
2724 OP(AtomicAnd
, atomic_and
)
2725 OP(AtomicOr
, atomic_or
)
2726 OP(AtomicXor
, atomic_xor
)
2729 vtn_fail("Invalid shared atomic");
2733 static nir_intrinsic_op
2734 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2737 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
2738 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
2739 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
2740 OP(AtomicExchange
, atomic_exchange
)
2741 OP(AtomicCompareExchange
, atomic_comp_swap
)
2742 OP(AtomicIIncrement
, atomic_add
)
2743 OP(AtomicIDecrement
, atomic_add
)
2744 OP(AtomicIAdd
, atomic_add
)
2745 OP(AtomicISub
, atomic_add
)
2746 OP(AtomicSMin
, atomic_imin
)
2747 OP(AtomicUMin
, atomic_umin
)
2748 OP(AtomicSMax
, atomic_imax
)
2749 OP(AtomicUMax
, atomic_umax
)
2750 OP(AtomicAnd
, atomic_and
)
2751 OP(AtomicOr
, atomic_or
)
2752 OP(AtomicXor
, atomic_xor
)
2755 vtn_fail("Invalid shared atomic");
2760 * Handles shared atomics, ssbo atomics and atomic counters.
2763 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
2764 const uint32_t *w
, unsigned count
)
2766 struct vtn_pointer
*ptr
;
2767 nir_intrinsic_instr
*atomic
;
2770 case SpvOpAtomicLoad
:
2771 case SpvOpAtomicExchange
:
2772 case SpvOpAtomicCompareExchange
:
2773 case SpvOpAtomicCompareExchangeWeak
:
2774 case SpvOpAtomicIIncrement
:
2775 case SpvOpAtomicIDecrement
:
2776 case SpvOpAtomicIAdd
:
2777 case SpvOpAtomicISub
:
2778 case SpvOpAtomicSMin
:
2779 case SpvOpAtomicUMin
:
2780 case SpvOpAtomicSMax
:
2781 case SpvOpAtomicUMax
:
2782 case SpvOpAtomicAnd
:
2784 case SpvOpAtomicXor
:
2785 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2788 case SpvOpAtomicStore
:
2789 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2793 vtn_fail("Invalid SPIR-V atomic");
2797 SpvScope scope = w[4];
2798 SpvMemorySemanticsMask semantics = w[5];
2801 /* uniform as "atomic counter uniform" */
2802 if (ptr
->mode
== vtn_variable_mode_uniform
) {
2803 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2804 const struct glsl_type
*deref_type
= deref
->type
;
2805 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
2806 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2807 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2809 /* SSBO needs to initialize index/offset. In this case we don't need to,
2810 * as that info is already stored on the ptr->var->var nir_variable (see
2811 * vtn_create_variable)
2815 case SpvOpAtomicLoad
:
2816 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2819 case SpvOpAtomicStore
:
2820 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2821 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2824 case SpvOpAtomicExchange
:
2825 case SpvOpAtomicCompareExchange
:
2826 case SpvOpAtomicCompareExchangeWeak
:
2827 case SpvOpAtomicIIncrement
:
2828 case SpvOpAtomicIDecrement
:
2829 case SpvOpAtomicIAdd
:
2830 case SpvOpAtomicISub
:
2831 case SpvOpAtomicSMin
:
2832 case SpvOpAtomicUMin
:
2833 case SpvOpAtomicSMax
:
2834 case SpvOpAtomicUMax
:
2835 case SpvOpAtomicAnd
:
2837 case SpvOpAtomicXor
:
2838 /* Nothing: we don't need to call fill_common_atomic_sources here, as
2839 * atomic counter uniforms doesn't have sources
2844 unreachable("Invalid SPIR-V atomic");
2847 } else if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
2848 nir_ssa_def
*offset
, *index
;
2849 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
2851 nir_intrinsic_op op
;
2852 if (ptr
->mode
== vtn_variable_mode_ssbo
) {
2853 op
= get_ssbo_nir_atomic_op(b
, opcode
);
2855 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
&&
2856 b
->options
->lower_workgroup_access_to_offsets
);
2857 op
= get_shared_nir_atomic_op(b
, opcode
);
2860 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2864 case SpvOpAtomicLoad
:
2865 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2866 nir_intrinsic_set_align(atomic
, 4, 0);
2867 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2868 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2869 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2872 case SpvOpAtomicStore
:
2873 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2874 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2875 nir_intrinsic_set_align(atomic
, 4, 0);
2876 atomic
->src
[src
++] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2877 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2878 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2879 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2882 case SpvOpAtomicExchange
:
2883 case SpvOpAtomicCompareExchange
:
2884 case SpvOpAtomicCompareExchangeWeak
:
2885 case SpvOpAtomicIIncrement
:
2886 case SpvOpAtomicIDecrement
:
2887 case SpvOpAtomicIAdd
:
2888 case SpvOpAtomicISub
:
2889 case SpvOpAtomicSMin
:
2890 case SpvOpAtomicUMin
:
2891 case SpvOpAtomicSMax
:
2892 case SpvOpAtomicUMax
:
2893 case SpvOpAtomicAnd
:
2895 case SpvOpAtomicXor
:
2896 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2897 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2898 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2899 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
2903 vtn_fail("Invalid SPIR-V atomic");
2906 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2907 const struct glsl_type
*deref_type
= deref
->type
;
2908 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
2909 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2910 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2913 case SpvOpAtomicLoad
:
2914 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2917 case SpvOpAtomicStore
:
2918 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2919 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2920 atomic
->src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2923 case SpvOpAtomicExchange
:
2924 case SpvOpAtomicCompareExchange
:
2925 case SpvOpAtomicCompareExchangeWeak
:
2926 case SpvOpAtomicIIncrement
:
2927 case SpvOpAtomicIDecrement
:
2928 case SpvOpAtomicIAdd
:
2929 case SpvOpAtomicISub
:
2930 case SpvOpAtomicSMin
:
2931 case SpvOpAtomicUMin
:
2932 case SpvOpAtomicSMax
:
2933 case SpvOpAtomicUMax
:
2934 case SpvOpAtomicAnd
:
2936 case SpvOpAtomicXor
:
2937 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
2941 vtn_fail("Invalid SPIR-V atomic");
2945 if (opcode
!= SpvOpAtomicStore
) {
2946 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2948 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
2949 glsl_get_vector_elements(type
->type
),
2950 glsl_get_bit_size(type
->type
), NULL
);
2952 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2953 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
2954 val
->ssa
->def
= &atomic
->dest
.ssa
;
2955 val
->ssa
->type
= type
->type
;
2958 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
2961 static nir_alu_instr
*
2962 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
2965 switch (num_components
) {
2966 case 1: op
= nir_op_imov
; break;
2967 case 2: op
= nir_op_vec2
; break;
2968 case 3: op
= nir_op_vec3
; break;
2969 case 4: op
= nir_op_vec4
; break;
2970 default: vtn_fail("bad vector size");
2973 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
2974 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
2976 vec
->dest
.write_mask
= (1 << num_components
) - 1;
2981 struct vtn_ssa_value
*
2982 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
2984 if (src
->transposed
)
2985 return src
->transposed
;
2987 struct vtn_ssa_value
*dest
=
2988 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
2990 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
2991 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
2992 glsl_get_bit_size(src
->type
));
2993 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2994 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
2995 vec
->src
[0].swizzle
[0] = i
;
2997 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
2998 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
2999 vec
->src
[j
].swizzle
[0] = i
;
3002 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3003 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
3006 dest
->transposed
= src
;
3012 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
3014 return nir_channel(&b
->nb
, src
, index
);
3018 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
3021 nir_alu_instr
*vec
= create_vec(b
, src
->num_components
,
3024 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
3026 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
3028 vec
->src
[i
].src
= nir_src_for_ssa(src
);
3029 vec
->src
[i
].swizzle
[0] = i
;
3033 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3035 return &vec
->dest
.dest
.ssa
;
3038 static nir_ssa_def
*
3039 nir_ieq_imm(nir_builder
*b
, nir_ssa_def
*x
, uint64_t i
)
3041 return nir_ieq(b
, x
, nir_imm_intN_t(b
, i
, x
->bit_size
));
3045 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
3048 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
3049 for (unsigned i
= 1; i
< src
->num_components
; i
++)
3050 dest
= nir_bcsel(&b
->nb
, nir_ieq_imm(&b
->nb
, index
, i
),
3051 vtn_vector_extract(b
, src
, i
), dest
);
3057 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
3058 nir_ssa_def
*insert
, nir_ssa_def
*index
)
3060 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
3061 for (unsigned i
= 1; i
< src
->num_components
; i
++)
3062 dest
= nir_bcsel(&b
->nb
, nir_ieq_imm(&b
->nb
, index
, i
),
3063 vtn_vector_insert(b
, src
, insert
, i
), dest
);
3068 static nir_ssa_def
*
3069 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
3070 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
3071 const uint32_t *indices
)
3073 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
3075 for (unsigned i
= 0; i
< num_components
; i
++) {
3076 uint32_t index
= indices
[i
];
3077 if (index
== 0xffffffff) {
3079 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
3080 } else if (index
< src0
->num_components
) {
3081 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
3082 vec
->src
[i
].swizzle
[0] = index
;
3084 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
3085 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
3089 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3091 return &vec
->dest
.dest
.ssa
;
3095 * Concatentates a number of vectors/scalars together to produce a vector
3097 static nir_ssa_def
*
3098 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
3099 unsigned num_srcs
, nir_ssa_def
**srcs
)
3101 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
3103 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3105 * "When constructing a vector, there must be at least two Constituent
3108 vtn_assert(num_srcs
>= 2);
3110 unsigned dest_idx
= 0;
3111 for (unsigned i
= 0; i
< num_srcs
; i
++) {
3112 nir_ssa_def
*src
= srcs
[i
];
3113 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
3114 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
3115 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
3116 vec
->src
[dest_idx
].swizzle
[0] = j
;
3121 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3123 * "When constructing a vector, the total number of components in all
3124 * the operands must equal the number of components in Result Type."
3126 vtn_assert(dest_idx
== num_components
);
3128 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3130 return &vec
->dest
.dest
.ssa
;
3133 static struct vtn_ssa_value
*
3134 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3136 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3137 dest
->type
= src
->type
;
3139 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3140 dest
->def
= src
->def
;
3142 unsigned elems
= glsl_get_length(src
->type
);
3144 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3145 for (unsigned i
= 0; i
< elems
; i
++)
3146 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3152 static struct vtn_ssa_value
*
3153 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3154 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3155 unsigned num_indices
)
3157 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3159 struct vtn_ssa_value
*cur
= dest
;
3161 for (i
= 0; i
< num_indices
- 1; i
++) {
3162 cur
= cur
->elems
[indices
[i
]];
3165 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3166 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3167 * the component granularity. In that case, the last index will be
3168 * the index to insert the scalar into the vector.
3171 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
3173 cur
->elems
[indices
[i
]] = insert
;
3179 static struct vtn_ssa_value
*
3180 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3181 const uint32_t *indices
, unsigned num_indices
)
3183 struct vtn_ssa_value
*cur
= src
;
3184 for (unsigned i
= 0; i
< num_indices
; i
++) {
3185 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3186 vtn_assert(i
== num_indices
- 1);
3187 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3188 * the component granularity. The last index will be the index of the
3189 * vector to extract.
3192 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
3193 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
3194 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
3197 cur
= cur
->elems
[indices
[i
]];
3205 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3206 const uint32_t *w
, unsigned count
)
3208 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
3209 const struct glsl_type
*type
=
3210 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
3211 val
->ssa
= vtn_create_ssa_value(b
, type
);
3214 case SpvOpVectorExtractDynamic
:
3215 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3216 vtn_ssa_value(b
, w
[4])->def
);
3219 case SpvOpVectorInsertDynamic
:
3220 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3221 vtn_ssa_value(b
, w
[4])->def
,
3222 vtn_ssa_value(b
, w
[5])->def
);
3225 case SpvOpVectorShuffle
:
3226 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
3227 vtn_ssa_value(b
, w
[3])->def
,
3228 vtn_ssa_value(b
, w
[4])->def
,
3232 case SpvOpCompositeConstruct
: {
3233 unsigned elems
= count
- 3;
3235 if (glsl_type_is_vector_or_scalar(type
)) {
3236 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3237 for (unsigned i
= 0; i
< elems
; i
++)
3238 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
3240 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
3243 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3244 for (unsigned i
= 0; i
< elems
; i
++)
3245 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3249 case SpvOpCompositeExtract
:
3250 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3254 case SpvOpCompositeInsert
:
3255 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3256 vtn_ssa_value(b
, w
[3]),
3260 case SpvOpCopyObject
:
3261 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3265 vtn_fail("unknown composite operation");
3270 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3272 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3273 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3277 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3278 SpvMemorySemanticsMask semantics
)
3280 static const SpvMemorySemanticsMask all_memory_semantics
=
3281 SpvMemorySemanticsUniformMemoryMask
|
3282 SpvMemorySemanticsWorkgroupMemoryMask
|
3283 SpvMemorySemanticsAtomicCounterMemoryMask
|
3284 SpvMemorySemanticsImageMemoryMask
;
3286 /* If we're not actually doing a memory barrier, bail */
3287 if (!(semantics
& all_memory_semantics
))
3290 /* GL and Vulkan don't have these */
3291 vtn_assert(scope
!= SpvScopeCrossDevice
);
3293 if (scope
== SpvScopeSubgroup
)
3294 return; /* Nothing to do here */
3296 if (scope
== SpvScopeWorkgroup
) {
3297 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3301 /* There's only two scopes thing left */
3302 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3304 if ((semantics
& all_memory_semantics
) == all_memory_semantics
) {
3305 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3309 /* Issue a bunch of more specific barriers */
3310 uint32_t bits
= semantics
;
3312 SpvMemorySemanticsMask semantic
= 1 << u_bit_scan(&bits
);
3314 case SpvMemorySemanticsUniformMemoryMask
:
3315 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3317 case SpvMemorySemanticsWorkgroupMemoryMask
:
3318 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3320 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3321 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3323 case SpvMemorySemanticsImageMemoryMask
:
3324 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3333 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3334 const uint32_t *w
, unsigned count
)
3337 case SpvOpEmitVertex
:
3338 case SpvOpEmitStreamVertex
:
3339 case SpvOpEndPrimitive
:
3340 case SpvOpEndStreamPrimitive
: {
3341 nir_intrinsic_op intrinsic_op
;
3343 case SpvOpEmitVertex
:
3344 case SpvOpEmitStreamVertex
:
3345 intrinsic_op
= nir_intrinsic_emit_vertex
;
3347 case SpvOpEndPrimitive
:
3348 case SpvOpEndStreamPrimitive
:
3349 intrinsic_op
= nir_intrinsic_end_primitive
;
3352 unreachable("Invalid opcode");
3355 nir_intrinsic_instr
*intrin
=
3356 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3359 case SpvOpEmitStreamVertex
:
3360 case SpvOpEndStreamPrimitive
: {
3361 unsigned stream
= vtn_constant_value(b
, w
[1])->values
[0].u32
[0];
3362 nir_intrinsic_set_stream_id(intrin
, stream
);
3370 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3374 case SpvOpMemoryBarrier
: {
3375 SpvScope scope
= vtn_constant_value(b
, w
[1])->values
[0].u32
[0];
3376 SpvMemorySemanticsMask semantics
=
3377 vtn_constant_value(b
, w
[2])->values
[0].u32
[0];
3378 vtn_emit_memory_barrier(b
, scope
, semantics
);
3382 case SpvOpControlBarrier
: {
3383 SpvScope execution_scope
=
3384 vtn_constant_value(b
, w
[1])->values
[0].u32
[0];
3385 if (execution_scope
== SpvScopeWorkgroup
)
3386 vtn_emit_barrier(b
, nir_intrinsic_barrier
);
3388 SpvScope memory_scope
=
3389 vtn_constant_value(b
, w
[2])->values
[0].u32
[0];
3390 SpvMemorySemanticsMask memory_semantics
=
3391 vtn_constant_value(b
, w
[3])->values
[0].u32
[0];
3392 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
3397 unreachable("unknown barrier instruction");
3402 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
3403 SpvExecutionMode mode
)
3406 case SpvExecutionModeInputPoints
:
3407 case SpvExecutionModeOutputPoints
:
3408 return 0; /* GL_POINTS */
3409 case SpvExecutionModeInputLines
:
3410 return 1; /* GL_LINES */
3411 case SpvExecutionModeInputLinesAdjacency
:
3412 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3413 case SpvExecutionModeTriangles
:
3414 return 4; /* GL_TRIANGLES */
3415 case SpvExecutionModeInputTrianglesAdjacency
:
3416 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3417 case SpvExecutionModeQuads
:
3418 return 7; /* GL_QUADS */
3419 case SpvExecutionModeIsolines
:
3420 return 0x8E7A; /* GL_ISOLINES */
3421 case SpvExecutionModeOutputLineStrip
:
3422 return 3; /* GL_LINE_STRIP */
3423 case SpvExecutionModeOutputTriangleStrip
:
3424 return 5; /* GL_TRIANGLE_STRIP */
3426 vtn_fail("Invalid primitive type");
3431 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
3432 SpvExecutionMode mode
)
3435 case SpvExecutionModeInputPoints
:
3437 case SpvExecutionModeInputLines
:
3439 case SpvExecutionModeInputLinesAdjacency
:
3441 case SpvExecutionModeTriangles
:
3443 case SpvExecutionModeInputTrianglesAdjacency
:
3446 vtn_fail("Invalid GS input mode");
3450 static gl_shader_stage
3451 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
3454 case SpvExecutionModelVertex
:
3455 return MESA_SHADER_VERTEX
;
3456 case SpvExecutionModelTessellationControl
:
3457 return MESA_SHADER_TESS_CTRL
;
3458 case SpvExecutionModelTessellationEvaluation
:
3459 return MESA_SHADER_TESS_EVAL
;
3460 case SpvExecutionModelGeometry
:
3461 return MESA_SHADER_GEOMETRY
;
3462 case SpvExecutionModelFragment
:
3463 return MESA_SHADER_FRAGMENT
;
3464 case SpvExecutionModelGLCompute
:
3465 return MESA_SHADER_COMPUTE
;
3466 case SpvExecutionModelKernel
:
3467 return MESA_SHADER_KERNEL
;
3469 vtn_fail("Unsupported execution model");
3473 #define spv_check_supported(name, cap) do { \
3474 if (!(b->options && b->options->caps.name)) \
3475 vtn_warn("Unsupported SPIR-V capability: %s", \
3476 spirv_capability_to_string(cap)); \
3481 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
3484 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
3485 /* Let this be a name label regardless */
3486 unsigned name_words
;
3487 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
3489 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
3490 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
3493 vtn_assert(b
->entry_point
== NULL
);
3494 b
->entry_point
= entry_point
;
3498 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3499 const uint32_t *w
, unsigned count
)
3506 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
3507 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
3508 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
3509 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
3510 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
3511 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
3514 uint32_t version
= w
[2];
3517 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
3519 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
3523 case SpvOpSourceExtension
:
3524 case SpvOpSourceContinued
:
3525 case SpvOpExtension
:
3526 case SpvOpModuleProcessed
:
3527 /* Unhandled, but these are for debug so that's ok. */
3530 case SpvOpCapability
: {
3531 SpvCapability cap
= w
[1];
3533 case SpvCapabilityMatrix
:
3534 case SpvCapabilityShader
:
3535 case SpvCapabilityGeometry
:
3536 case SpvCapabilityGeometryPointSize
:
3537 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
3538 case SpvCapabilitySampledImageArrayDynamicIndexing
:
3539 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
3540 case SpvCapabilityStorageImageArrayDynamicIndexing
:
3541 case SpvCapabilityImageRect
:
3542 case SpvCapabilitySampledRect
:
3543 case SpvCapabilitySampled1D
:
3544 case SpvCapabilityImage1D
:
3545 case SpvCapabilitySampledCubeArray
:
3546 case SpvCapabilityImageCubeArray
:
3547 case SpvCapabilitySampledBuffer
:
3548 case SpvCapabilityImageBuffer
:
3549 case SpvCapabilityImageQuery
:
3550 case SpvCapabilityDerivativeControl
:
3551 case SpvCapabilityInterpolationFunction
:
3552 case SpvCapabilityMultiViewport
:
3553 case SpvCapabilitySampleRateShading
:
3554 case SpvCapabilityClipDistance
:
3555 case SpvCapabilityCullDistance
:
3556 case SpvCapabilityInputAttachment
:
3557 case SpvCapabilityImageGatherExtended
:
3558 case SpvCapabilityStorageImageExtendedFormats
:
3561 case SpvCapabilityLinkage
:
3562 case SpvCapabilityVector16
:
3563 case SpvCapabilityFloat16Buffer
:
3564 case SpvCapabilityFloat16
:
3565 case SpvCapabilitySparseResidency
:
3566 vtn_warn("Unsupported SPIR-V capability: %s",
3567 spirv_capability_to_string(cap
));
3570 case SpvCapabilityMinLod
:
3571 spv_check_supported(min_lod
, cap
);
3574 case SpvCapabilityAtomicStorage
:
3575 spv_check_supported(atomic_storage
, cap
);
3578 case SpvCapabilityFloat64
:
3579 spv_check_supported(float64
, cap
);
3581 case SpvCapabilityInt64
:
3582 spv_check_supported(int64
, cap
);
3584 case SpvCapabilityInt16
:
3585 spv_check_supported(int16
, cap
);
3588 case SpvCapabilityTransformFeedback
:
3589 spv_check_supported(transform_feedback
, cap
);
3592 case SpvCapabilityGeometryStreams
:
3593 spv_check_supported(geometry_streams
, cap
);
3596 case SpvCapabilityInt64Atomics
:
3597 spv_check_supported(int64_atomics
, cap
);
3599 case SpvCapabilityInt8
:
3600 spv_check_supported(int8
, cap
);
3603 case SpvCapabilityStorageImageMultisample
:
3604 spv_check_supported(storage_image_ms
, cap
);
3607 case SpvCapabilityAddresses
:
3608 spv_check_supported(address
, cap
);
3611 case SpvCapabilityKernel
:
3612 spv_check_supported(kernel
, cap
);
3615 case SpvCapabilityImageBasic
:
3616 case SpvCapabilityImageReadWrite
:
3617 case SpvCapabilityImageMipmap
:
3618 case SpvCapabilityPipes
:
3619 case SpvCapabilityGroups
:
3620 case SpvCapabilityDeviceEnqueue
:
3621 case SpvCapabilityLiteralSampler
:
3622 case SpvCapabilityGenericPointer
:
3623 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
3624 spirv_capability_to_string(cap
));
3627 case SpvCapabilityImageMSArray
:
3628 spv_check_supported(image_ms_array
, cap
);
3631 case SpvCapabilityTessellation
:
3632 case SpvCapabilityTessellationPointSize
:
3633 spv_check_supported(tessellation
, cap
);
3636 case SpvCapabilityDrawParameters
:
3637 spv_check_supported(draw_parameters
, cap
);
3640 case SpvCapabilityStorageImageReadWithoutFormat
:
3641 spv_check_supported(image_read_without_format
, cap
);
3644 case SpvCapabilityStorageImageWriteWithoutFormat
:
3645 spv_check_supported(image_write_without_format
, cap
);
3648 case SpvCapabilityDeviceGroup
:
3649 spv_check_supported(device_group
, cap
);
3652 case SpvCapabilityMultiView
:
3653 spv_check_supported(multiview
, cap
);
3656 case SpvCapabilityGroupNonUniform
:
3657 spv_check_supported(subgroup_basic
, cap
);
3660 case SpvCapabilityGroupNonUniformVote
:
3661 spv_check_supported(subgroup_vote
, cap
);
3664 case SpvCapabilitySubgroupBallotKHR
:
3665 case SpvCapabilityGroupNonUniformBallot
:
3666 spv_check_supported(subgroup_ballot
, cap
);
3669 case SpvCapabilityGroupNonUniformShuffle
:
3670 case SpvCapabilityGroupNonUniformShuffleRelative
:
3671 spv_check_supported(subgroup_shuffle
, cap
);
3674 case SpvCapabilityGroupNonUniformQuad
:
3675 spv_check_supported(subgroup_quad
, cap
);
3678 case SpvCapabilityGroupNonUniformArithmetic
:
3679 case SpvCapabilityGroupNonUniformClustered
:
3680 spv_check_supported(subgroup_arithmetic
, cap
);
3683 case SpvCapabilityVariablePointersStorageBuffer
:
3684 case SpvCapabilityVariablePointers
:
3685 spv_check_supported(variable_pointers
, cap
);
3686 b
->variable_pointers
= true;
3689 case SpvCapabilityStorageUniformBufferBlock16
:
3690 case SpvCapabilityStorageUniform16
:
3691 case SpvCapabilityStoragePushConstant16
:
3692 case SpvCapabilityStorageInputOutput16
:
3693 spv_check_supported(storage_16bit
, cap
);
3696 case SpvCapabilityShaderViewportIndexLayerEXT
:
3697 spv_check_supported(shader_viewport_index_layer
, cap
);
3700 case SpvCapabilityStorageBuffer8BitAccess
:
3701 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
3702 case SpvCapabilityStoragePushConstant8
:
3703 spv_check_supported(storage_8bit
, cap
);
3706 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
3707 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
3708 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
3709 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
3712 case SpvCapabilityRuntimeDescriptorArrayEXT
:
3713 spv_check_supported(runtime_descriptor_array
, cap
);
3716 case SpvCapabilityStencilExportEXT
:
3717 spv_check_supported(stencil_export
, cap
);
3720 case SpvCapabilitySampleMaskPostDepthCoverage
:
3721 spv_check_supported(post_depth_coverage
, cap
);
3724 case SpvCapabilityPhysicalStorageBufferAddressesEXT
:
3725 spv_check_supported(physical_storage_buffer_address
, cap
);
3729 vtn_fail("Unhandled capability");
3734 case SpvOpExtInstImport
:
3735 vtn_handle_extension(b
, opcode
, w
, count
);
3738 case SpvOpMemoryModel
:
3739 vtn_assert(w
[1] == SpvAddressingModelLogical
||
3741 b
->options
->caps
.physical_storage_buffer_address
&&
3742 w
[1] == SpvAddressingModelPhysicalStorageBuffer64EXT
));
3743 vtn_assert(w
[2] == SpvMemoryModelSimple
||
3744 w
[2] == SpvMemoryModelGLSL450
);
3747 case SpvOpEntryPoint
:
3748 vtn_handle_entry_point(b
, w
, count
);
3752 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
3753 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3757 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3760 case SpvOpMemberName
:
3764 case SpvOpExecutionMode
:
3765 case SpvOpDecorationGroup
:
3767 case SpvOpMemberDecorate
:
3768 case SpvOpGroupDecorate
:
3769 case SpvOpGroupMemberDecorate
:
3770 case SpvOpDecorateStringGOOGLE
:
3771 case SpvOpMemberDecorateStringGOOGLE
:
3772 vtn_handle_decoration(b
, opcode
, w
, count
);
3776 return false; /* End of preamble */
3783 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
3784 const struct vtn_decoration
*mode
, void *data
)
3786 vtn_assert(b
->entry_point
== entry_point
);
3788 switch(mode
->exec_mode
) {
3789 case SpvExecutionModeOriginUpperLeft
:
3790 case SpvExecutionModeOriginLowerLeft
:
3791 b
->origin_upper_left
=
3792 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
3795 case SpvExecutionModeEarlyFragmentTests
:
3796 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3797 b
->shader
->info
.fs
.early_fragment_tests
= true;
3800 case SpvExecutionModePostDepthCoverage
:
3801 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3802 b
->shader
->info
.fs
.post_depth_coverage
= true;
3805 case SpvExecutionModeInvocations
:
3806 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3807 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->literals
[0]);
3810 case SpvExecutionModeDepthReplacing
:
3811 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3812 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
3814 case SpvExecutionModeDepthGreater
:
3815 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3816 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
3818 case SpvExecutionModeDepthLess
:
3819 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3820 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
3822 case SpvExecutionModeDepthUnchanged
:
3823 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3824 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
3827 case SpvExecutionModeLocalSize
:
3828 vtn_assert(gl_shader_stage_is_compute(b
->shader
->info
.stage
));
3829 b
->shader
->info
.cs
.local_size
[0] = mode
->literals
[0];
3830 b
->shader
->info
.cs
.local_size
[1] = mode
->literals
[1];
3831 b
->shader
->info
.cs
.local_size
[2] = mode
->literals
[2];
3833 case SpvExecutionModeLocalSizeHint
:
3834 break; /* Nothing to do with this */
3836 case SpvExecutionModeOutputVertices
:
3837 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3838 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3839 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->literals
[0];
3841 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3842 b
->shader
->info
.gs
.vertices_out
= mode
->literals
[0];
3846 case SpvExecutionModeInputPoints
:
3847 case SpvExecutionModeInputLines
:
3848 case SpvExecutionModeInputLinesAdjacency
:
3849 case SpvExecutionModeTriangles
:
3850 case SpvExecutionModeInputTrianglesAdjacency
:
3851 case SpvExecutionModeQuads
:
3852 case SpvExecutionModeIsolines
:
3853 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3854 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3855 b
->shader
->info
.tess
.primitive_mode
=
3856 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3858 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3859 b
->shader
->info
.gs
.vertices_in
=
3860 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
3861 b
->shader
->info
.gs
.input_primitive
=
3862 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3866 case SpvExecutionModeOutputPoints
:
3867 case SpvExecutionModeOutputLineStrip
:
3868 case SpvExecutionModeOutputTriangleStrip
:
3869 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3870 b
->shader
->info
.gs
.output_primitive
=
3871 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3874 case SpvExecutionModeSpacingEqual
:
3875 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3876 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3877 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
3879 case SpvExecutionModeSpacingFractionalEven
:
3880 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3881 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3882 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
3884 case SpvExecutionModeSpacingFractionalOdd
:
3885 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3886 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3887 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
3889 case SpvExecutionModeVertexOrderCw
:
3890 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3891 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3892 b
->shader
->info
.tess
.ccw
= false;
3894 case SpvExecutionModeVertexOrderCcw
:
3895 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3896 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3897 b
->shader
->info
.tess
.ccw
= true;
3899 case SpvExecutionModePointMode
:
3900 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3901 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3902 b
->shader
->info
.tess
.point_mode
= true;
3905 case SpvExecutionModePixelCenterInteger
:
3906 b
->pixel_center_integer
= true;
3909 case SpvExecutionModeXfb
:
3910 b
->shader
->info
.has_transform_feedback_varyings
= true;
3913 case SpvExecutionModeVecTypeHint
:
3916 case SpvExecutionModeContractionOff
:
3917 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
3918 vtn_warn("ExectionMode only allowed for CL-style kernels: %s",
3919 spirv_executionmode_to_string(mode
->exec_mode
));
3924 case SpvExecutionModeStencilRefReplacingEXT
:
3925 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3929 vtn_fail("Unhandled execution mode");
3934 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3935 const uint32_t *w
, unsigned count
)
3937 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
3941 case SpvOpSourceContinued
:
3942 case SpvOpSourceExtension
:
3943 case SpvOpExtension
:
3944 case SpvOpCapability
:
3945 case SpvOpExtInstImport
:
3946 case SpvOpMemoryModel
:
3947 case SpvOpEntryPoint
:
3948 case SpvOpExecutionMode
:
3951 case SpvOpMemberName
:
3952 case SpvOpDecorationGroup
:
3954 case SpvOpMemberDecorate
:
3955 case SpvOpGroupDecorate
:
3956 case SpvOpGroupMemberDecorate
:
3957 case SpvOpDecorateStringGOOGLE
:
3958 case SpvOpMemberDecorateStringGOOGLE
:
3959 vtn_fail("Invalid opcode types and variables section");
3965 case SpvOpTypeFloat
:
3966 case SpvOpTypeVector
:
3967 case SpvOpTypeMatrix
:
3968 case SpvOpTypeImage
:
3969 case SpvOpTypeSampler
:
3970 case SpvOpTypeSampledImage
:
3971 case SpvOpTypeArray
:
3972 case SpvOpTypeRuntimeArray
:
3973 case SpvOpTypeStruct
:
3974 case SpvOpTypeOpaque
:
3975 case SpvOpTypePointer
:
3976 case SpvOpTypeForwardPointer
:
3977 case SpvOpTypeFunction
:
3978 case SpvOpTypeEvent
:
3979 case SpvOpTypeDeviceEvent
:
3980 case SpvOpTypeReserveId
:
3981 case SpvOpTypeQueue
:
3983 vtn_handle_type(b
, opcode
, w
, count
);
3986 case SpvOpConstantTrue
:
3987 case SpvOpConstantFalse
:
3989 case SpvOpConstantComposite
:
3990 case SpvOpConstantSampler
:
3991 case SpvOpConstantNull
:
3992 case SpvOpSpecConstantTrue
:
3993 case SpvOpSpecConstantFalse
:
3994 case SpvOpSpecConstant
:
3995 case SpvOpSpecConstantComposite
:
3996 case SpvOpSpecConstantOp
:
3997 vtn_handle_constant(b
, opcode
, w
, count
);
4002 vtn_handle_variables(b
, opcode
, w
, count
);
4006 return false; /* End of preamble */
4013 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4014 const uint32_t *w
, unsigned count
)
4020 case SpvOpLoopMerge
:
4021 case SpvOpSelectionMerge
:
4022 /* This is handled by cfg pre-pass and walk_blocks */
4026 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
4027 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4032 vtn_handle_extension(b
, opcode
, w
, count
);
4038 case SpvOpCopyMemory
:
4039 case SpvOpCopyMemorySized
:
4040 case SpvOpAccessChain
:
4041 case SpvOpPtrAccessChain
:
4042 case SpvOpInBoundsAccessChain
:
4043 case SpvOpArrayLength
:
4044 case SpvOpConvertPtrToU
:
4045 case SpvOpConvertUToPtr
:
4046 vtn_handle_variables(b
, opcode
, w
, count
);
4049 case SpvOpFunctionCall
:
4050 vtn_handle_function_call(b
, opcode
, w
, count
);
4053 case SpvOpSampledImage
:
4055 case SpvOpImageSampleImplicitLod
:
4056 case SpvOpImageSampleExplicitLod
:
4057 case SpvOpImageSampleDrefImplicitLod
:
4058 case SpvOpImageSampleDrefExplicitLod
:
4059 case SpvOpImageSampleProjImplicitLod
:
4060 case SpvOpImageSampleProjExplicitLod
:
4061 case SpvOpImageSampleProjDrefImplicitLod
:
4062 case SpvOpImageSampleProjDrefExplicitLod
:
4063 case SpvOpImageFetch
:
4064 case SpvOpImageGather
:
4065 case SpvOpImageDrefGather
:
4066 case SpvOpImageQuerySizeLod
:
4067 case SpvOpImageQueryLod
:
4068 case SpvOpImageQueryLevels
:
4069 case SpvOpImageQuerySamples
:
4070 vtn_handle_texture(b
, opcode
, w
, count
);
4073 case SpvOpImageRead
:
4074 case SpvOpImageWrite
:
4075 case SpvOpImageTexelPointer
:
4076 vtn_handle_image(b
, opcode
, w
, count
);
4079 case SpvOpImageQuerySize
: {
4080 struct vtn_pointer
*image
=
4081 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
4082 if (glsl_type_is_image(image
->type
->type
)) {
4083 vtn_handle_image(b
, opcode
, w
, count
);
4085 vtn_assert(glsl_type_is_sampler(image
->type
->type
));
4086 vtn_handle_texture(b
, opcode
, w
, count
);
4091 case SpvOpAtomicLoad
:
4092 case SpvOpAtomicExchange
:
4093 case SpvOpAtomicCompareExchange
:
4094 case SpvOpAtomicCompareExchangeWeak
:
4095 case SpvOpAtomicIIncrement
:
4096 case SpvOpAtomicIDecrement
:
4097 case SpvOpAtomicIAdd
:
4098 case SpvOpAtomicISub
:
4099 case SpvOpAtomicSMin
:
4100 case SpvOpAtomicUMin
:
4101 case SpvOpAtomicSMax
:
4102 case SpvOpAtomicUMax
:
4103 case SpvOpAtomicAnd
:
4105 case SpvOpAtomicXor
: {
4106 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
4107 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4108 vtn_handle_image(b
, opcode
, w
, count
);
4110 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4111 vtn_handle_atomics(b
, opcode
, w
, count
);
4116 case SpvOpAtomicStore
: {
4117 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
4118 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4119 vtn_handle_image(b
, opcode
, w
, count
);
4121 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4122 vtn_handle_atomics(b
, opcode
, w
, count
);
4128 /* Handle OpSelect up-front here because it needs to be able to handle
4129 * pointers and not just regular vectors and scalars.
4131 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
4132 struct vtn_value
*sel_val
= vtn_untyped_value(b
, w
[3]);
4133 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
4134 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
4136 const struct glsl_type
*sel_type
;
4137 switch (res_val
->type
->base_type
) {
4138 case vtn_base_type_scalar
:
4139 sel_type
= glsl_bool_type();
4141 case vtn_base_type_vector
:
4142 sel_type
= glsl_vector_type(GLSL_TYPE_BOOL
, res_val
->type
->length
);
4144 case vtn_base_type_pointer
:
4145 /* We need to have actual storage for pointer types */
4146 vtn_fail_if(res_val
->type
->type
== NULL
,
4147 "Invalid pointer result type for OpSelect");
4148 sel_type
= glsl_bool_type();
4151 vtn_fail("Result type of OpSelect must be a scalar, vector, or pointer");
4154 if (unlikely(sel_val
->type
->type
!= sel_type
)) {
4155 if (sel_val
->type
->type
== glsl_bool_type()) {
4156 /* This case is illegal but some older versions of GLSLang produce
4157 * it. The GLSLang issue was fixed on March 30, 2017:
4159 * https://github.com/KhronosGroup/glslang/issues/809
4161 * Unfortunately, there are applications in the wild which are
4162 * shipping with this bug so it isn't nice to fail on them so we
4163 * throw a warning instead. It's not actually a problem for us as
4164 * nir_builder will just splat the condition out which is most
4165 * likely what the client wanted anyway.
4167 vtn_warn("Condition type of OpSelect must have the same number "
4168 "of components as Result Type");
4170 vtn_fail("Condition type of OpSelect must be a scalar or vector "
4171 "of Boolean type. It must have the same number of "
4172 "components as Result Type");
4176 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4177 obj2_val
->type
!= res_val
->type
,
4178 "Object types must match the result type in OpSelect");
4180 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4181 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, res_type
->type
);
4182 ssa
->def
= nir_bcsel(&b
->nb
, vtn_ssa_value(b
, w
[3])->def
,
4183 vtn_ssa_value(b
, w
[4])->def
,
4184 vtn_ssa_value(b
, w
[5])->def
);
4185 vtn_push_ssa(b
, w
[2], res_type
, ssa
);
4194 case SpvOpConvertFToU
:
4195 case SpvOpConvertFToS
:
4196 case SpvOpConvertSToF
:
4197 case SpvOpConvertUToF
:
4201 case SpvOpQuantizeToF16
:
4202 case SpvOpPtrCastToGeneric
:
4203 case SpvOpGenericCastToPtr
:
4209 case SpvOpSignBitSet
:
4210 case SpvOpLessOrGreater
:
4212 case SpvOpUnordered
:
4227 case SpvOpVectorTimesScalar
:
4229 case SpvOpIAddCarry
:
4230 case SpvOpISubBorrow
:
4231 case SpvOpUMulExtended
:
4232 case SpvOpSMulExtended
:
4233 case SpvOpShiftRightLogical
:
4234 case SpvOpShiftRightArithmetic
:
4235 case SpvOpShiftLeftLogical
:
4236 case SpvOpLogicalEqual
:
4237 case SpvOpLogicalNotEqual
:
4238 case SpvOpLogicalOr
:
4239 case SpvOpLogicalAnd
:
4240 case SpvOpLogicalNot
:
4241 case SpvOpBitwiseOr
:
4242 case SpvOpBitwiseXor
:
4243 case SpvOpBitwiseAnd
:
4245 case SpvOpFOrdEqual
:
4246 case SpvOpFUnordEqual
:
4247 case SpvOpINotEqual
:
4248 case SpvOpFOrdNotEqual
:
4249 case SpvOpFUnordNotEqual
:
4250 case SpvOpULessThan
:
4251 case SpvOpSLessThan
:
4252 case SpvOpFOrdLessThan
:
4253 case SpvOpFUnordLessThan
:
4254 case SpvOpUGreaterThan
:
4255 case SpvOpSGreaterThan
:
4256 case SpvOpFOrdGreaterThan
:
4257 case SpvOpFUnordGreaterThan
:
4258 case SpvOpULessThanEqual
:
4259 case SpvOpSLessThanEqual
:
4260 case SpvOpFOrdLessThanEqual
:
4261 case SpvOpFUnordLessThanEqual
:
4262 case SpvOpUGreaterThanEqual
:
4263 case SpvOpSGreaterThanEqual
:
4264 case SpvOpFOrdGreaterThanEqual
:
4265 case SpvOpFUnordGreaterThanEqual
:
4271 case SpvOpFwidthFine
:
4272 case SpvOpDPdxCoarse
:
4273 case SpvOpDPdyCoarse
:
4274 case SpvOpFwidthCoarse
:
4275 case SpvOpBitFieldInsert
:
4276 case SpvOpBitFieldSExtract
:
4277 case SpvOpBitFieldUExtract
:
4278 case SpvOpBitReverse
:
4280 case SpvOpTranspose
:
4281 case SpvOpOuterProduct
:
4282 case SpvOpMatrixTimesScalar
:
4283 case SpvOpVectorTimesMatrix
:
4284 case SpvOpMatrixTimesVector
:
4285 case SpvOpMatrixTimesMatrix
:
4286 vtn_handle_alu(b
, opcode
, w
, count
);
4289 case SpvOpVectorExtractDynamic
:
4290 case SpvOpVectorInsertDynamic
:
4291 case SpvOpVectorShuffle
:
4292 case SpvOpCompositeConstruct
:
4293 case SpvOpCompositeExtract
:
4294 case SpvOpCompositeInsert
:
4295 case SpvOpCopyObject
:
4296 vtn_handle_composite(b
, opcode
, w
, count
);
4299 case SpvOpEmitVertex
:
4300 case SpvOpEndPrimitive
:
4301 case SpvOpEmitStreamVertex
:
4302 case SpvOpEndStreamPrimitive
:
4303 case SpvOpControlBarrier
:
4304 case SpvOpMemoryBarrier
:
4305 vtn_handle_barrier(b
, opcode
, w
, count
);
4308 case SpvOpGroupNonUniformElect
:
4309 case SpvOpGroupNonUniformAll
:
4310 case SpvOpGroupNonUniformAny
:
4311 case SpvOpGroupNonUniformAllEqual
:
4312 case SpvOpGroupNonUniformBroadcast
:
4313 case SpvOpGroupNonUniformBroadcastFirst
:
4314 case SpvOpGroupNonUniformBallot
:
4315 case SpvOpGroupNonUniformInverseBallot
:
4316 case SpvOpGroupNonUniformBallotBitExtract
:
4317 case SpvOpGroupNonUniformBallotBitCount
:
4318 case SpvOpGroupNonUniformBallotFindLSB
:
4319 case SpvOpGroupNonUniformBallotFindMSB
:
4320 case SpvOpGroupNonUniformShuffle
:
4321 case SpvOpGroupNonUniformShuffleXor
:
4322 case SpvOpGroupNonUniformShuffleUp
:
4323 case SpvOpGroupNonUniformShuffleDown
:
4324 case SpvOpGroupNonUniformIAdd
:
4325 case SpvOpGroupNonUniformFAdd
:
4326 case SpvOpGroupNonUniformIMul
:
4327 case SpvOpGroupNonUniformFMul
:
4328 case SpvOpGroupNonUniformSMin
:
4329 case SpvOpGroupNonUniformUMin
:
4330 case SpvOpGroupNonUniformFMin
:
4331 case SpvOpGroupNonUniformSMax
:
4332 case SpvOpGroupNonUniformUMax
:
4333 case SpvOpGroupNonUniformFMax
:
4334 case SpvOpGroupNonUniformBitwiseAnd
:
4335 case SpvOpGroupNonUniformBitwiseOr
:
4336 case SpvOpGroupNonUniformBitwiseXor
:
4337 case SpvOpGroupNonUniformLogicalAnd
:
4338 case SpvOpGroupNonUniformLogicalOr
:
4339 case SpvOpGroupNonUniformLogicalXor
:
4340 case SpvOpGroupNonUniformQuadBroadcast
:
4341 case SpvOpGroupNonUniformQuadSwap
:
4342 vtn_handle_subgroup(b
, opcode
, w
, count
);
4346 vtn_fail("Unhandled opcode");
4353 vtn_create_builder(const uint32_t *words
, size_t word_count
,
4354 gl_shader_stage stage
, const char *entry_point_name
,
4355 const struct spirv_to_nir_options
*options
)
4357 /* Initialize the vtn_builder object */
4358 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
4360 b
->spirv_word_count
= word_count
;
4364 exec_list_make_empty(&b
->functions
);
4365 b
->entry_point_stage
= stage
;
4366 b
->entry_point_name
= entry_point_name
;
4367 b
->options
= options
;
4370 * Handle the SPIR-V header (first 5 dwords).
4371 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
4373 if (word_count
<= 5)
4376 if (words
[0] != SpvMagicNumber
) {
4377 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
4380 if (words
[1] < 0x10000) {
4381 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
4385 uint16_t generator_id
= words
[2] >> 16;
4386 uint16_t generator_version
= words
[2];
4388 /* The first GLSLang version bump actually 1.5 years after #179 was fixed
4389 * but this should at least let us shut the workaround off for modern
4390 * versions of GLSLang.
4392 b
->wa_glslang_179
= (generator_id
== 8 && generator_version
== 1);
4394 /* words[2] == generator magic */
4395 unsigned value_id_bound
= words
[3];
4396 if (words
[4] != 0) {
4397 vtn_err("words[4] was %u, want 0", words
[4]);
4401 b
->value_id_bound
= value_id_bound
;
4402 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
4411 spirv_to_nir(const uint32_t *words
, size_t word_count
,
4412 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
4413 gl_shader_stage stage
, const char *entry_point_name
,
4414 const struct spirv_to_nir_options
*options
,
4415 const nir_shader_compiler_options
*nir_options
)
4418 const uint32_t *word_end
= words
+ word_count
;
4420 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
4421 stage
, entry_point_name
,
4427 /* See also _vtn_fail() */
4428 if (setjmp(b
->fail_jump
)) {
4433 /* Skip the SPIR-V header, handled at vtn_create_builder */
4436 /* Handle all the preamble instructions */
4437 words
= vtn_foreach_instruction(b
, words
, word_end
,
4438 vtn_handle_preamble_instruction
);
4440 if (b
->entry_point
== NULL
) {
4441 vtn_fail("Entry point not found");
4446 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
4448 /* Set shader info defaults */
4449 b
->shader
->info
.gs
.invocations
= 1;
4451 /* Parse execution modes */
4452 vtn_foreach_execution_mode(b
, b
->entry_point
,
4453 vtn_handle_execution_mode
, NULL
);
4455 b
->specializations
= spec
;
4456 b
->num_specializations
= num_spec
;
4458 /* Handle all variable, type, and constant instructions */
4459 words
= vtn_foreach_instruction(b
, words
, word_end
,
4460 vtn_handle_variable_or_type_instruction
);
4462 /* Set types on all vtn_values */
4463 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
4465 vtn_build_cfg(b
, words
, word_end
);
4467 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4468 b
->entry_point
->func
->referenced
= true;
4473 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
4474 if (func
->referenced
&& !func
->emitted
) {
4475 b
->const_table
= _mesa_pointer_hash_table_create(b
);
4477 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
4483 /* We sometimes generate bogus derefs that, while never used, give the
4484 * validator a bit of heartburn. Run dead code to get rid of them.
4486 nir_opt_dce(b
->shader
);
4488 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4489 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
4490 vtn_assert(entry_point
);
4492 /* Unparent the shader from the vtn_builder before we delete the builder */
4493 ralloc_steal(NULL
, b
->shader
);
4497 entry_point
->is_entrypoint
= true;