2 * Copyright © 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Jason Ekstrand (jason@jlekstrand.net)
28 #include "vtn_private.h"
29 #include "nir/nir_vla.h"
30 #include "nir/nir_control_flow.h"
31 #include "nir/nir_constant_expressions.h"
32 #include "nir/nir_deref.h"
33 #include "spirv_info.h"
38 vtn_log(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
39 size_t spirv_offset
, const char *message
)
41 if (b
->options
->debug
.func
) {
42 b
->options
->debug
.func(b
->options
->debug
.private_data
,
43 level
, spirv_offset
, message
);
47 if (level
>= NIR_SPIRV_DEBUG_LEVEL_WARNING
)
48 fprintf(stderr
, "%s\n", message
);
53 vtn_logf(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
54 size_t spirv_offset
, const char *fmt
, ...)
60 msg
= ralloc_vasprintf(NULL
, fmt
, args
);
63 vtn_log(b
, level
, spirv_offset
, msg
);
69 vtn_log_err(struct vtn_builder
*b
,
70 enum nir_spirv_debug_level level
, const char *prefix
,
71 const char *file
, unsigned line
,
72 const char *fmt
, va_list args
)
76 msg
= ralloc_strdup(NULL
, prefix
);
79 ralloc_asprintf_append(&msg
, " In file %s:%u\n", file
, line
);
82 ralloc_asprintf_append(&msg
, " ");
84 ralloc_vasprintf_append(&msg
, fmt
, args
);
86 ralloc_asprintf_append(&msg
, "\n %zu bytes into the SPIR-V binary",
90 ralloc_asprintf_append(&msg
,
91 "\n in SPIR-V source file %s, line %d, col %d",
92 b
->file
, b
->line
, b
->col
);
95 vtn_log(b
, level
, b
->spirv_offset
, msg
);
101 vtn_dump_shader(struct vtn_builder
*b
, const char *path
, const char *prefix
)
106 int len
= snprintf(filename
, sizeof(filename
), "%s/%s-%d.spirv",
107 path
, prefix
, idx
++);
108 if (len
< 0 || len
>= sizeof(filename
))
111 FILE *f
= fopen(filename
, "w");
115 fwrite(b
->spirv
, sizeof(*b
->spirv
), b
->spirv_word_count
, f
);
118 vtn_info("SPIR-V shader dumped to %s", filename
);
122 _vtn_warn(struct vtn_builder
*b
, const char *file
, unsigned line
,
123 const char *fmt
, ...)
128 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_WARNING
, "SPIR-V WARNING:\n",
129 file
, line
, fmt
, args
);
134 _vtn_err(struct vtn_builder
*b
, const char *file
, unsigned line
,
135 const char *fmt
, ...)
140 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V ERROR:\n",
141 file
, line
, fmt
, args
);
146 _vtn_fail(struct vtn_builder
*b
, const char *file
, unsigned line
,
147 const char *fmt
, ...)
152 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V parsing FAILED:\n",
153 file
, line
, fmt
, args
);
156 const char *dump_path
= getenv("MESA_SPIRV_FAIL_DUMP_PATH");
158 vtn_dump_shader(b
, dump_path
, "fail");
160 longjmp(b
->fail_jump
, 1);
163 struct spec_constant_value
{
171 static struct vtn_ssa_value
*
172 vtn_undef_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
174 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
177 if (glsl_type_is_vector_or_scalar(type
)) {
178 unsigned num_components
= glsl_get_vector_elements(val
->type
);
179 unsigned bit_size
= glsl_get_bit_size(val
->type
);
180 val
->def
= nir_ssa_undef(&b
->nb
, num_components
, bit_size
);
182 unsigned elems
= glsl_get_length(val
->type
);
183 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
184 if (glsl_type_is_matrix(type
)) {
185 const struct glsl_type
*elem_type
=
186 glsl_vector_type(glsl_get_base_type(type
),
187 glsl_get_vector_elements(type
));
189 for (unsigned i
= 0; i
< elems
; i
++)
190 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
191 } else if (glsl_type_is_array(type
)) {
192 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
193 for (unsigned i
= 0; i
< elems
; i
++)
194 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
196 for (unsigned i
= 0; i
< elems
; i
++) {
197 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
198 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
206 static struct vtn_ssa_value
*
207 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
208 const struct glsl_type
*type
)
210 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
215 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
218 switch (glsl_get_base_type(type
)) {
221 case GLSL_TYPE_INT16
:
222 case GLSL_TYPE_UINT16
:
223 case GLSL_TYPE_UINT8
:
225 case GLSL_TYPE_INT64
:
226 case GLSL_TYPE_UINT64
:
228 case GLSL_TYPE_FLOAT
:
229 case GLSL_TYPE_FLOAT16
:
230 case GLSL_TYPE_DOUBLE
: {
231 int bit_size
= glsl_get_bit_size(type
);
232 if (glsl_type_is_vector_or_scalar(type
)) {
233 unsigned num_components
= glsl_get_vector_elements(val
->type
);
234 nir_load_const_instr
*load
=
235 nir_load_const_instr_create(b
->shader
, num_components
, bit_size
);
237 load
->value
= constant
->values
[0];
239 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
240 val
->def
= &load
->def
;
242 assert(glsl_type_is_matrix(type
));
243 unsigned rows
= glsl_get_vector_elements(val
->type
);
244 unsigned columns
= glsl_get_matrix_columns(val
->type
);
245 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, columns
);
247 for (unsigned i
= 0; i
< columns
; i
++) {
248 struct vtn_ssa_value
*col_val
= rzalloc(b
, struct vtn_ssa_value
);
249 col_val
->type
= glsl_get_column_type(val
->type
);
250 nir_load_const_instr
*load
=
251 nir_load_const_instr_create(b
->shader
, rows
, bit_size
);
253 load
->value
= constant
->values
[i
];
255 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
256 col_val
->def
= &load
->def
;
258 val
->elems
[i
] = col_val
;
264 case GLSL_TYPE_ARRAY
: {
265 unsigned elems
= glsl_get_length(val
->type
);
266 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
267 const struct glsl_type
*elem_type
= glsl_get_array_element(val
->type
);
268 for (unsigned i
= 0; i
< elems
; i
++)
269 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
274 case GLSL_TYPE_STRUCT
: {
275 unsigned elems
= glsl_get_length(val
->type
);
276 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
277 for (unsigned i
= 0; i
< elems
; i
++) {
278 const struct glsl_type
*elem_type
=
279 glsl_get_struct_field(val
->type
, i
);
280 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
287 vtn_fail("bad constant type");
293 struct vtn_ssa_value
*
294 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
296 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
297 switch (val
->value_type
) {
298 case vtn_value_type_undef
:
299 return vtn_undef_ssa_value(b
, val
->type
->type
);
301 case vtn_value_type_constant
:
302 return vtn_const_ssa_value(b
, val
->constant
, val
->type
->type
);
304 case vtn_value_type_ssa
:
307 case vtn_value_type_pointer
:
308 vtn_assert(val
->pointer
->ptr_type
&& val
->pointer
->ptr_type
->type
);
309 struct vtn_ssa_value
*ssa
=
310 vtn_create_ssa_value(b
, val
->pointer
->ptr_type
->type
);
311 ssa
->def
= vtn_pointer_to_ssa(b
, val
->pointer
);
315 vtn_fail("Invalid type for an SSA value");
320 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
321 unsigned word_count
, unsigned *words_used
)
323 char *dup
= ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
325 /* Ammount of space taken by the string (including the null) */
326 unsigned len
= strlen(dup
) + 1;
327 *words_used
= DIV_ROUND_UP(len
, sizeof(*words
));
333 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
334 const uint32_t *end
, vtn_instruction_handler handler
)
340 const uint32_t *w
= start
;
342 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
343 unsigned count
= w
[0] >> SpvWordCountShift
;
344 vtn_assert(count
>= 1 && w
+ count
<= end
);
346 b
->spirv_offset
= (uint8_t *)w
- (uint8_t *)b
->spirv
;
350 break; /* Do nothing */
353 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
365 if (!handler(b
, opcode
, w
, count
))
383 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
384 const uint32_t *w
, unsigned count
)
386 const char *ext
= (const char *)&w
[2];
388 case SpvOpExtInstImport
: {
389 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
390 if (strcmp(ext
, "GLSL.std.450") == 0) {
391 val
->ext_handler
= vtn_handle_glsl450_instruction
;
392 } else if ((strcmp(ext
, "SPV_AMD_gcn_shader") == 0)
393 && (b
->options
&& b
->options
->caps
.gcn_shader
)) {
394 val
->ext_handler
= vtn_handle_amd_gcn_shader_instruction
;
395 } else if ((strcmp(ext
, "SPV_AMD_shader_trinary_minmax") == 0)
396 && (b
->options
&& b
->options
->caps
.trinary_minmax
)) {
397 val
->ext_handler
= vtn_handle_amd_shader_trinary_minmax_instruction
;
399 vtn_fail("Unsupported extension: %s", ext
);
405 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
406 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
412 vtn_fail("Unhandled opcode");
417 _foreach_decoration_helper(struct vtn_builder
*b
,
418 struct vtn_value
*base_value
,
420 struct vtn_value
*value
,
421 vtn_decoration_foreach_cb cb
, void *data
)
423 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
425 if (dec
->scope
== VTN_DEC_DECORATION
) {
426 member
= parent_member
;
427 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
428 vtn_fail_if(value
->value_type
!= vtn_value_type_type
||
429 value
->type
->base_type
!= vtn_base_type_struct
,
430 "OpMemberDecorate and OpGroupMemberDecorate are only "
431 "allowed on OpTypeStruct");
432 /* This means we haven't recursed yet */
433 assert(value
== base_value
);
435 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
437 vtn_fail_if(member
>= base_value
->type
->length
,
438 "OpMemberDecorate specifies member %d but the "
439 "OpTypeStruct has only %u members",
440 member
, base_value
->type
->length
);
442 /* Not a decoration */
443 assert(dec
->scope
== VTN_DEC_EXECUTION_MODE
);
448 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
449 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
452 cb(b
, base_value
, member
, dec
, data
);
457 /** Iterates (recursively if needed) over all of the decorations on a value
459 * This function iterates over all of the decorations applied to a given
460 * value. If it encounters a decoration group, it recurses into the group
461 * and iterates over all of those decorations as well.
464 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
465 vtn_decoration_foreach_cb cb
, void *data
)
467 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
471 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
472 vtn_execution_mode_foreach_cb cb
, void *data
)
474 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
475 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
478 assert(dec
->group
== NULL
);
479 cb(b
, value
, dec
, data
);
484 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
485 const uint32_t *w
, unsigned count
)
487 const uint32_t *w_end
= w
+ count
;
488 const uint32_t target
= w
[1];
492 case SpvOpDecorationGroup
:
493 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
497 case SpvOpMemberDecorate
:
498 case SpvOpDecorateStringGOOGLE
:
499 case SpvOpMemberDecorateStringGOOGLE
:
500 case SpvOpExecutionMode
: {
501 struct vtn_value
*val
= vtn_untyped_value(b
, target
);
503 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
506 case SpvOpDecorateStringGOOGLE
:
507 dec
->scope
= VTN_DEC_DECORATION
;
509 case SpvOpMemberDecorate
:
510 case SpvOpMemberDecorateStringGOOGLE
:
511 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
512 vtn_fail_if(dec
->scope
< VTN_DEC_STRUCT_MEMBER0
, /* overflow */
513 "Member argument of OpMemberDecorate too large");
515 case SpvOpExecutionMode
:
516 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
519 unreachable("Invalid decoration opcode");
521 dec
->decoration
= *(w
++);
524 /* Link into the list */
525 dec
->next
= val
->decoration
;
526 val
->decoration
= dec
;
530 case SpvOpGroupMemberDecorate
:
531 case SpvOpGroupDecorate
: {
532 struct vtn_value
*group
=
533 vtn_value(b
, target
, vtn_value_type_decoration_group
);
535 for (; w
< w_end
; w
++) {
536 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
537 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
540 if (opcode
== SpvOpGroupDecorate
) {
541 dec
->scope
= VTN_DEC_DECORATION
;
543 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
544 vtn_fail_if(dec
->scope
< 0, /* Check for overflow */
545 "Member argument of OpGroupMemberDecorate too large");
548 /* Link into the list */
549 dec
->next
= val
->decoration
;
550 val
->decoration
= dec
;
556 unreachable("Unhandled opcode");
560 struct member_decoration_ctx
{
562 struct glsl_struct_field
*fields
;
563 struct vtn_type
*type
;
567 * Returns true if the given type contains a struct decorated Block or
571 vtn_type_contains_block(struct vtn_builder
*b
, struct vtn_type
*type
)
573 switch (type
->base_type
) {
574 case vtn_base_type_array
:
575 return vtn_type_contains_block(b
, type
->array_element
);
576 case vtn_base_type_struct
:
577 if (type
->block
|| type
->buffer_block
)
579 for (unsigned i
= 0; i
< type
->length
; i
++) {
580 if (vtn_type_contains_block(b
, type
->members
[i
]))
589 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
590 * OpStore, or OpCopyMemory between them without breaking anything.
591 * Technically, the SPIR-V rules require the exact same type ID but this lets
592 * us internally be a bit looser.
595 vtn_types_compatible(struct vtn_builder
*b
,
596 struct vtn_type
*t1
, struct vtn_type
*t2
)
598 if (t1
->id
== t2
->id
)
601 if (t1
->base_type
!= t2
->base_type
)
604 switch (t1
->base_type
) {
605 case vtn_base_type_void
:
606 case vtn_base_type_scalar
:
607 case vtn_base_type_vector
:
608 case vtn_base_type_matrix
:
609 case vtn_base_type_image
:
610 case vtn_base_type_sampler
:
611 case vtn_base_type_sampled_image
:
612 return t1
->type
== t2
->type
;
614 case vtn_base_type_array
:
615 return t1
->length
== t2
->length
&&
616 vtn_types_compatible(b
, t1
->array_element
, t2
->array_element
);
618 case vtn_base_type_pointer
:
619 return vtn_types_compatible(b
, t1
->deref
, t2
->deref
);
621 case vtn_base_type_struct
:
622 if (t1
->length
!= t2
->length
)
625 for (unsigned i
= 0; i
< t1
->length
; i
++) {
626 if (!vtn_types_compatible(b
, t1
->members
[i
], t2
->members
[i
]))
631 case vtn_base_type_function
:
632 /* This case shouldn't get hit since you can't copy around function
633 * types. Just require them to be identical.
638 vtn_fail("Invalid base type");
641 /* does a shallow copy of a vtn_type */
643 static struct vtn_type
*
644 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
646 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
649 switch (src
->base_type
) {
650 case vtn_base_type_void
:
651 case vtn_base_type_scalar
:
652 case vtn_base_type_vector
:
653 case vtn_base_type_matrix
:
654 case vtn_base_type_array
:
655 case vtn_base_type_pointer
:
656 case vtn_base_type_image
:
657 case vtn_base_type_sampler
:
658 case vtn_base_type_sampled_image
:
659 /* Nothing more to do */
662 case vtn_base_type_struct
:
663 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
664 memcpy(dest
->members
, src
->members
,
665 src
->length
* sizeof(src
->members
[0]));
667 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
668 memcpy(dest
->offsets
, src
->offsets
,
669 src
->length
* sizeof(src
->offsets
[0]));
672 case vtn_base_type_function
:
673 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
674 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
681 static struct vtn_type
*
682 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
684 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
685 type
= type
->members
[member
];
687 /* We may have an array of matrices.... Oh, joy! */
688 while (glsl_type_is_array(type
->type
)) {
689 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
690 type
= type
->array_element
;
693 vtn_assert(glsl_type_is_matrix(type
->type
));
699 vtn_handle_access_qualifier(struct vtn_builder
*b
, struct vtn_type
*type
,
700 int member
, enum gl_access_qualifier access
)
702 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
703 type
= type
->members
[member
];
705 type
->access
|= access
;
709 array_stride_decoration_cb(struct vtn_builder
*b
,
710 struct vtn_value
*val
, int member
,
711 const struct vtn_decoration
*dec
, void *void_ctx
)
713 struct vtn_type
*type
= val
->type
;
715 if (dec
->decoration
== SpvDecorationArrayStride
) {
716 vtn_fail_if(dec
->literals
[0] == 0, "ArrayStride must be non-zero");
717 type
->stride
= dec
->literals
[0];
722 struct_member_decoration_cb(struct vtn_builder
*b
,
723 struct vtn_value
*val
, int member
,
724 const struct vtn_decoration
*dec
, void *void_ctx
)
726 struct member_decoration_ctx
*ctx
= void_ctx
;
731 assert(member
< ctx
->num_fields
);
733 switch (dec
->decoration
) {
734 case SpvDecorationRelaxedPrecision
:
735 case SpvDecorationUniform
:
736 break; /* FIXME: Do nothing with this for now. */
737 case SpvDecorationNonWritable
:
738 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_WRITEABLE
);
740 case SpvDecorationNonReadable
:
741 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_READABLE
);
743 case SpvDecorationVolatile
:
744 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_VOLATILE
);
746 case SpvDecorationCoherent
:
747 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_COHERENT
);
749 case SpvDecorationNoPerspective
:
750 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
752 case SpvDecorationFlat
:
753 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
755 case SpvDecorationCentroid
:
756 ctx
->fields
[member
].centroid
= true;
758 case SpvDecorationSample
:
759 ctx
->fields
[member
].sample
= true;
761 case SpvDecorationStream
:
762 /* Vulkan only allows one GS stream */
763 vtn_assert(dec
->literals
[0] == 0);
765 case SpvDecorationLocation
:
766 ctx
->fields
[member
].location
= dec
->literals
[0];
768 case SpvDecorationComponent
:
769 break; /* FIXME: What should we do with these? */
770 case SpvDecorationBuiltIn
:
771 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
772 ctx
->type
->members
[member
]->is_builtin
= true;
773 ctx
->type
->members
[member
]->builtin
= dec
->literals
[0];
774 ctx
->type
->builtin_block
= true;
776 case SpvDecorationOffset
:
777 ctx
->type
->offsets
[member
] = dec
->literals
[0];
778 ctx
->fields
[member
].offset
= dec
->literals
[0];
780 case SpvDecorationMatrixStride
:
781 /* Handled as a second pass */
783 case SpvDecorationColMajor
:
784 break; /* Nothing to do here. Column-major is the default. */
785 case SpvDecorationRowMajor
:
786 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
789 case SpvDecorationPatch
:
792 case SpvDecorationSpecId
:
793 case SpvDecorationBlock
:
794 case SpvDecorationBufferBlock
:
795 case SpvDecorationArrayStride
:
796 case SpvDecorationGLSLShared
:
797 case SpvDecorationGLSLPacked
:
798 case SpvDecorationInvariant
:
799 case SpvDecorationRestrict
:
800 case SpvDecorationAliased
:
801 case SpvDecorationConstant
:
802 case SpvDecorationIndex
:
803 case SpvDecorationBinding
:
804 case SpvDecorationDescriptorSet
:
805 case SpvDecorationLinkageAttributes
:
806 case SpvDecorationNoContraction
:
807 case SpvDecorationInputAttachmentIndex
:
808 vtn_warn("Decoration not allowed on struct members: %s",
809 spirv_decoration_to_string(dec
->decoration
));
812 case SpvDecorationXfbBuffer
:
813 case SpvDecorationXfbStride
:
814 vtn_warn("Vulkan does not have transform feedback");
817 case SpvDecorationCPacked
:
818 case SpvDecorationSaturatedConversion
:
819 case SpvDecorationFuncParamAttr
:
820 case SpvDecorationFPRoundingMode
:
821 case SpvDecorationFPFastMathMode
:
822 case SpvDecorationAlignment
:
823 vtn_warn("Decoration only allowed for CL-style kernels: %s",
824 spirv_decoration_to_string(dec
->decoration
));
827 case SpvDecorationHlslSemanticGOOGLE
:
828 /* HLSL semantic decorations can safely be ignored by the driver. */
832 vtn_fail("Unhandled decoration");
836 /** Chases the array type all the way down to the tail and rewrites the
837 * glsl_types to be based off the tail's glsl_type.
840 vtn_array_type_rewrite_glsl_type(struct vtn_type
*type
)
842 if (type
->base_type
!= vtn_base_type_array
)
845 vtn_array_type_rewrite_glsl_type(type
->array_element
);
847 type
->type
= glsl_array_type(type
->array_element
->type
,
848 type
->length
, type
->stride
);
851 /* Matrix strides are handled as a separate pass because we need to know
852 * whether the matrix is row-major or not first.
855 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
856 struct vtn_value
*val
, int member
,
857 const struct vtn_decoration
*dec
,
860 if (dec
->decoration
!= SpvDecorationMatrixStride
)
863 vtn_fail_if(member
< 0,
864 "The MatrixStride decoration is only allowed on members "
866 vtn_fail_if(dec
->literals
[0] == 0, "MatrixStride must be non-zero");
868 struct member_decoration_ctx
*ctx
= void_ctx
;
870 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
871 if (mat_type
->row_major
) {
872 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
873 mat_type
->stride
= mat_type
->array_element
->stride
;
874 mat_type
->array_element
->stride
= dec
->literals
[0];
876 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
877 dec
->literals
[0], true);
878 mat_type
->array_element
->type
= glsl_get_column_type(mat_type
->type
);
880 vtn_assert(mat_type
->array_element
->stride
> 0);
881 mat_type
->stride
= dec
->literals
[0];
883 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
884 dec
->literals
[0], false);
887 /* Now that we've replaced the glsl_type with a properly strided matrix
888 * type, rewrite the member type so that it's an array of the proper kind
891 vtn_array_type_rewrite_glsl_type(ctx
->type
->members
[member
]);
892 ctx
->fields
[member
].type
= ctx
->type
->members
[member
]->type
;
896 type_decoration_cb(struct vtn_builder
*b
,
897 struct vtn_value
*val
, int member
,
898 const struct vtn_decoration
*dec
, void *ctx
)
900 struct vtn_type
*type
= val
->type
;
903 /* This should have been handled by OpTypeStruct */
904 assert(val
->type
->base_type
== vtn_base_type_struct
);
905 assert(member
>= 0 && member
< val
->type
->length
);
909 switch (dec
->decoration
) {
910 case SpvDecorationArrayStride
:
911 vtn_assert(type
->base_type
== vtn_base_type_array
||
912 type
->base_type
== vtn_base_type_pointer
);
914 case SpvDecorationBlock
:
915 vtn_assert(type
->base_type
== vtn_base_type_struct
);
918 case SpvDecorationBufferBlock
:
919 vtn_assert(type
->base_type
== vtn_base_type_struct
);
920 type
->buffer_block
= true;
922 case SpvDecorationGLSLShared
:
923 case SpvDecorationGLSLPacked
:
924 /* Ignore these, since we get explicit offsets anyways */
927 case SpvDecorationRowMajor
:
928 case SpvDecorationColMajor
:
929 case SpvDecorationMatrixStride
:
930 case SpvDecorationBuiltIn
:
931 case SpvDecorationNoPerspective
:
932 case SpvDecorationFlat
:
933 case SpvDecorationPatch
:
934 case SpvDecorationCentroid
:
935 case SpvDecorationSample
:
936 case SpvDecorationVolatile
:
937 case SpvDecorationCoherent
:
938 case SpvDecorationNonWritable
:
939 case SpvDecorationNonReadable
:
940 case SpvDecorationUniform
:
941 case SpvDecorationLocation
:
942 case SpvDecorationComponent
:
943 case SpvDecorationOffset
:
944 case SpvDecorationXfbBuffer
:
945 case SpvDecorationXfbStride
:
946 case SpvDecorationHlslSemanticGOOGLE
:
947 vtn_warn("Decoration only allowed for struct members: %s",
948 spirv_decoration_to_string(dec
->decoration
));
951 case SpvDecorationStream
:
952 /* We don't need to do anything here, as stream is filled up when
953 * aplying the decoration to a variable, just check that if it is not a
954 * struct member, it should be a struct.
956 vtn_assert(type
->base_type
== vtn_base_type_struct
);
959 case SpvDecorationRelaxedPrecision
:
960 case SpvDecorationSpecId
:
961 case SpvDecorationInvariant
:
962 case SpvDecorationRestrict
:
963 case SpvDecorationAliased
:
964 case SpvDecorationConstant
:
965 case SpvDecorationIndex
:
966 case SpvDecorationBinding
:
967 case SpvDecorationDescriptorSet
:
968 case SpvDecorationLinkageAttributes
:
969 case SpvDecorationNoContraction
:
970 case SpvDecorationInputAttachmentIndex
:
971 vtn_warn("Decoration not allowed on types: %s",
972 spirv_decoration_to_string(dec
->decoration
));
975 case SpvDecorationCPacked
:
976 case SpvDecorationSaturatedConversion
:
977 case SpvDecorationFuncParamAttr
:
978 case SpvDecorationFPRoundingMode
:
979 case SpvDecorationFPFastMathMode
:
980 case SpvDecorationAlignment
:
981 vtn_warn("Decoration only allowed for CL-style kernels: %s",
982 spirv_decoration_to_string(dec
->decoration
));
986 vtn_fail("Unhandled decoration");
991 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
994 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
995 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
996 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
997 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
998 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
999 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
1000 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
1001 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
1002 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
1003 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
1004 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
1005 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
1006 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
1007 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
1008 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
1009 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
1010 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
1011 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
1012 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
1013 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
1014 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
1015 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
1016 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
1017 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
1018 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
1019 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
1020 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
1021 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
1022 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
1023 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
1024 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
1025 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
1026 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
1027 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
1028 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
1029 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
1030 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
1031 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
1032 case SpvImageFormatR16ui
: return 0x8234; /* GL_R16UI */
1033 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
1035 vtn_fail("Invalid image format");
1039 static struct vtn_type
*
1040 vtn_type_layout_std430(struct vtn_builder
*b
, struct vtn_type
*type
,
1041 uint32_t *size_out
, uint32_t *align_out
)
1043 switch (type
->base_type
) {
1044 case vtn_base_type_scalar
: {
1045 uint32_t comp_size
= glsl_get_bit_size(type
->type
) / 8;
1046 *size_out
= comp_size
;
1047 *align_out
= comp_size
;
1051 case vtn_base_type_vector
: {
1052 uint32_t comp_size
= glsl_get_bit_size(type
->type
) / 8;
1053 unsigned align_comps
= type
->length
== 3 ? 4 : type
->length
;
1054 *size_out
= comp_size
* type
->length
,
1055 *align_out
= comp_size
* align_comps
;
1059 case vtn_base_type_matrix
:
1060 case vtn_base_type_array
: {
1061 /* We're going to add an array stride */
1062 type
= vtn_type_copy(b
, type
);
1063 uint32_t elem_size
, elem_align
;
1064 type
->array_element
= vtn_type_layout_std430(b
, type
->array_element
,
1065 &elem_size
, &elem_align
);
1066 type
->stride
= vtn_align_u32(elem_size
, elem_align
);
1067 *size_out
= type
->stride
* type
->length
;
1068 *align_out
= elem_align
;
1072 case vtn_base_type_struct
: {
1073 /* We're going to add member offsets */
1074 type
= vtn_type_copy(b
, type
);
1075 uint32_t offset
= 0;
1077 for (unsigned i
= 0; i
< type
->length
; i
++) {
1078 uint32_t mem_size
, mem_align
;
1079 type
->members
[i
] = vtn_type_layout_std430(b
, type
->members
[i
],
1080 &mem_size
, &mem_align
);
1081 offset
= vtn_align_u32(offset
, mem_align
);
1082 type
->offsets
[i
] = offset
;
1084 align
= MAX2(align
, mem_align
);
1092 unreachable("Invalid SPIR-V type for std430");
1097 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
1098 const uint32_t *w
, unsigned count
)
1100 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1102 val
->type
= rzalloc(b
, struct vtn_type
);
1103 val
->type
->id
= w
[1];
1107 val
->type
->base_type
= vtn_base_type_void
;
1108 val
->type
->type
= glsl_void_type();
1111 val
->type
->base_type
= vtn_base_type_scalar
;
1112 val
->type
->type
= glsl_bool_type();
1113 val
->type
->length
= 1;
1115 case SpvOpTypeInt
: {
1116 int bit_size
= w
[2];
1117 const bool signedness
= w
[3];
1118 val
->type
->base_type
= vtn_base_type_scalar
;
1121 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1124 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1127 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1130 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1133 vtn_fail("Invalid int bit size");
1135 val
->type
->length
= 1;
1139 case SpvOpTypeFloat
: {
1140 int bit_size
= w
[2];
1141 val
->type
->base_type
= vtn_base_type_scalar
;
1144 val
->type
->type
= glsl_float16_t_type();
1147 val
->type
->type
= glsl_float_type();
1150 val
->type
->type
= glsl_double_type();
1153 vtn_fail("Invalid float bit size");
1155 val
->type
->length
= 1;
1159 case SpvOpTypeVector
: {
1160 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1161 unsigned elems
= w
[3];
1163 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1164 "Base type for OpTypeVector must be a scalar");
1165 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1166 "Invalid component count for OpTypeVector");
1168 val
->type
->base_type
= vtn_base_type_vector
;
1169 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1170 val
->type
->length
= elems
;
1171 val
->type
->stride
= glsl_get_bit_size(base
->type
) / 8;
1172 val
->type
->array_element
= base
;
1176 case SpvOpTypeMatrix
: {
1177 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1178 unsigned columns
= w
[3];
1180 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1181 "Base type for OpTypeMatrix must be a vector");
1182 vtn_fail_if(columns
< 2 || columns
> 4,
1183 "Invalid column count for OpTypeMatrix");
1185 val
->type
->base_type
= vtn_base_type_matrix
;
1186 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1187 glsl_get_vector_elements(base
->type
),
1189 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1190 "Unsupported base type for OpTypeMatrix");
1191 assert(!glsl_type_is_error(val
->type
->type
));
1192 val
->type
->length
= columns
;
1193 val
->type
->array_element
= base
;
1194 val
->type
->row_major
= false;
1195 val
->type
->stride
= 0;
1199 case SpvOpTypeRuntimeArray
:
1200 case SpvOpTypeArray
: {
1201 struct vtn_type
*array_element
=
1202 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1204 if (opcode
== SpvOpTypeRuntimeArray
) {
1205 /* A length of 0 is used to denote unsized arrays */
1206 val
->type
->length
= 0;
1209 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
1212 val
->type
->base_type
= vtn_base_type_array
;
1213 val
->type
->array_element
= array_element
;
1214 val
->type
->stride
= 0;
1216 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1217 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
,
1222 case SpvOpTypeStruct
: {
1223 unsigned num_fields
= count
- 2;
1224 val
->type
->base_type
= vtn_base_type_struct
;
1225 val
->type
->length
= num_fields
;
1226 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1227 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1229 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1230 for (unsigned i
= 0; i
< num_fields
; i
++) {
1231 val
->type
->members
[i
] =
1232 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
1233 fields
[i
] = (struct glsl_struct_field
) {
1234 .type
= val
->type
->members
[i
]->type
,
1235 .name
= ralloc_asprintf(b
, "field%d", i
),
1240 struct member_decoration_ctx ctx
= {
1241 .num_fields
= num_fields
,
1246 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1247 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1249 const char *name
= val
->name
? val
->name
: "struct";
1251 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
);
1255 case SpvOpTypeFunction
: {
1256 val
->type
->base_type
= vtn_base_type_function
;
1257 val
->type
->type
= NULL
;
1259 val
->type
->return_type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1261 const unsigned num_params
= count
- 3;
1262 val
->type
->length
= num_params
;
1263 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1264 for (unsigned i
= 0; i
< count
- 3; i
++) {
1265 val
->type
->params
[i
] =
1266 vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
;
1271 case SpvOpTypePointer
: {
1272 SpvStorageClass storage_class
= w
[2];
1273 struct vtn_type
*deref_type
=
1274 vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
1276 val
->type
->base_type
= vtn_base_type_pointer
;
1277 val
->type
->storage_class
= storage_class
;
1278 val
->type
->deref
= deref_type
;
1280 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1282 /* These can actually be stored to nir_variables and used as SSA
1283 * values so they need a real glsl_type.
1285 switch (storage_class
) {
1286 case SpvStorageClassUniform
:
1287 val
->type
->type
= b
->options
->ubo_ptr_type
;
1289 case SpvStorageClassStorageBuffer
:
1290 val
->type
->type
= b
->options
->ssbo_ptr_type
;
1292 case SpvStorageClassPushConstant
:
1293 val
->type
->type
= b
->options
->push_const_ptr_type
;
1295 case SpvStorageClassWorkgroup
:
1296 val
->type
->type
= b
->options
->shared_ptr_type
;
1297 if (b
->options
->lower_workgroup_access_to_offsets
) {
1298 uint32_t size
, align
;
1299 val
->type
->deref
= vtn_type_layout_std430(b
, val
->type
->deref
,
1301 val
->type
->length
= size
;
1302 val
->type
->align
= align
;
1306 /* In this case, no variable pointers are allowed so all deref chains
1307 * are complete back to the variable and it doesn't matter what type
1308 * gets used so we leave it NULL.
1315 case SpvOpTypeImage
: {
1316 val
->type
->base_type
= vtn_base_type_image
;
1318 const struct vtn_type
*sampled_type
=
1319 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1321 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1322 glsl_get_bit_size(sampled_type
->type
) != 32,
1323 "Sampled type of OpTypeImage must be a 32-bit scalar");
1325 enum glsl_sampler_dim dim
;
1326 switch ((SpvDim
)w
[3]) {
1327 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1328 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1329 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1330 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1331 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1332 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1333 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1335 vtn_fail("Invalid SPIR-V image dimensionality");
1338 bool is_shadow
= w
[4];
1339 bool is_array
= w
[5];
1340 bool multisampled
= w
[6];
1341 unsigned sampled
= w
[7];
1342 SpvImageFormat format
= w
[8];
1345 val
->type
->access_qualifier
= w
[9];
1347 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1350 if (dim
== GLSL_SAMPLER_DIM_2D
)
1351 dim
= GLSL_SAMPLER_DIM_MS
;
1352 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1353 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1355 vtn_fail("Unsupported multisampled image type");
1358 val
->type
->image_format
= translate_image_format(b
, format
);
1360 enum glsl_base_type sampled_base_type
=
1361 glsl_get_base_type(sampled_type
->type
);
1363 val
->type
->sampled
= true;
1364 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
1366 } else if (sampled
== 2) {
1367 vtn_assert(!is_shadow
);
1368 val
->type
->sampled
= false;
1369 val
->type
->type
= glsl_image_type(dim
, is_array
, sampled_base_type
);
1371 vtn_fail("We need to know if the image will be sampled");
1376 case SpvOpTypeSampledImage
:
1377 val
->type
->base_type
= vtn_base_type_sampled_image
;
1378 val
->type
->image
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1379 val
->type
->type
= val
->type
->image
->type
;
1382 case SpvOpTypeSampler
:
1383 /* The actual sampler type here doesn't really matter. It gets
1384 * thrown away the moment you combine it with an image. What really
1385 * matters is that it's a sampler type as opposed to an integer type
1386 * so the backend knows what to do.
1388 val
->type
->base_type
= vtn_base_type_sampler
;
1389 val
->type
->type
= glsl_bare_sampler_type();
1392 case SpvOpTypeOpaque
:
1393 case SpvOpTypeEvent
:
1394 case SpvOpTypeDeviceEvent
:
1395 case SpvOpTypeReserveId
:
1396 case SpvOpTypeQueue
:
1399 vtn_fail("Unhandled opcode");
1402 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1404 if (val
->type
->base_type
== vtn_base_type_struct
&&
1405 (val
->type
->block
|| val
->type
->buffer_block
)) {
1406 for (unsigned i
= 0; i
< val
->type
->length
; i
++) {
1407 vtn_fail_if(vtn_type_contains_block(b
, val
->type
->members
[i
]),
1408 "Block and BufferBlock decorations cannot decorate a "
1409 "structure type that is nested at any level inside "
1410 "another structure type decorated with Block or "
1416 static nir_constant
*
1417 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
1419 nir_constant
*c
= rzalloc(b
, nir_constant
);
1421 /* For pointers and other typeless things, we have to return something but
1422 * it doesn't matter what.
1427 switch (glsl_get_base_type(type
)) {
1429 case GLSL_TYPE_UINT
:
1430 case GLSL_TYPE_INT16
:
1431 case GLSL_TYPE_UINT16
:
1432 case GLSL_TYPE_UINT8
:
1433 case GLSL_TYPE_INT8
:
1434 case GLSL_TYPE_INT64
:
1435 case GLSL_TYPE_UINT64
:
1436 case GLSL_TYPE_BOOL
:
1437 case GLSL_TYPE_FLOAT
:
1438 case GLSL_TYPE_FLOAT16
:
1439 case GLSL_TYPE_DOUBLE
:
1440 /* Nothing to do here. It's already initialized to zero */
1443 case GLSL_TYPE_ARRAY
:
1444 vtn_assert(glsl_get_length(type
) > 0);
1445 c
->num_elements
= glsl_get_length(type
);
1446 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1448 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
1449 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1450 c
->elements
[i
] = c
->elements
[0];
1453 case GLSL_TYPE_STRUCT
:
1454 c
->num_elements
= glsl_get_length(type
);
1455 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1457 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
1458 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
1463 vtn_fail("Invalid type for null constant");
1470 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
1471 int member
, const struct vtn_decoration
*dec
,
1474 vtn_assert(member
== -1);
1475 if (dec
->decoration
!= SpvDecorationSpecId
)
1478 struct spec_constant_value
*const_value
= data
;
1480 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1481 if (b
->specializations
[i
].id
== dec
->literals
[0]) {
1482 if (const_value
->is_double
)
1483 const_value
->data64
= b
->specializations
[i
].data64
;
1485 const_value
->data32
= b
->specializations
[i
].data32
;
1492 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
1493 uint32_t const_value
)
1495 struct spec_constant_value data
;
1496 data
.is_double
= false;
1497 data
.data32
= const_value
;
1498 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1503 get_specialization64(struct vtn_builder
*b
, struct vtn_value
*val
,
1504 uint64_t const_value
)
1506 struct spec_constant_value data
;
1507 data
.is_double
= true;
1508 data
.data64
= const_value
;
1509 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1514 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1515 struct vtn_value
*val
,
1517 const struct vtn_decoration
*dec
,
1520 vtn_assert(member
== -1);
1521 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1522 dec
->literals
[0] != SpvBuiltInWorkgroupSize
)
1525 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1527 b
->shader
->info
.cs
.local_size
[0] = val
->constant
->values
[0].u32
[0];
1528 b
->shader
->info
.cs
.local_size
[1] = val
->constant
->values
[0].u32
[1];
1529 b
->shader
->info
.cs
.local_size
[2] = val
->constant
->values
[0].u32
[2];
1533 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1534 const uint32_t *w
, unsigned count
)
1536 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1537 val
->constant
= rzalloc(b
, nir_constant
);
1539 case SpvOpConstantTrue
:
1540 case SpvOpConstantFalse
:
1541 case SpvOpSpecConstantTrue
:
1542 case SpvOpSpecConstantFalse
: {
1543 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1544 "Result type of %s must be OpTypeBool",
1545 spirv_op_to_string(opcode
));
1547 uint32_t int_val
= (opcode
== SpvOpConstantTrue
||
1548 opcode
== SpvOpSpecConstantTrue
);
1550 if (opcode
== SpvOpSpecConstantTrue
||
1551 opcode
== SpvOpSpecConstantFalse
)
1552 int_val
= get_specialization(b
, val
, int_val
);
1554 val
->constant
->values
[0].b
[0] = int_val
!= 0;
1558 case SpvOpConstant
: {
1559 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1560 "Result type of %s must be a scalar",
1561 spirv_op_to_string(opcode
));
1562 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1565 val
->constant
->values
->u64
[0] = vtn_u64_literal(&w
[3]);
1568 val
->constant
->values
->u32
[0] = w
[3];
1571 val
->constant
->values
->u16
[0] = w
[3];
1574 val
->constant
->values
->u8
[0] = w
[3];
1577 vtn_fail("Unsupported SpvOpConstant bit size");
1582 case SpvOpSpecConstant
: {
1583 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1584 "Result type of %s must be a scalar",
1585 spirv_op_to_string(opcode
));
1586 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1589 val
->constant
->values
[0].u64
[0] =
1590 get_specialization64(b
, val
, vtn_u64_literal(&w
[3]));
1593 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1596 val
->constant
->values
[0].u16
[0] = get_specialization(b
, val
, w
[3]);
1599 val
->constant
->values
[0].u8
[0] = get_specialization(b
, val
, w
[3]);
1602 vtn_fail("Unsupported SpvOpSpecConstant bit size");
1607 case SpvOpSpecConstantComposite
:
1608 case SpvOpConstantComposite
: {
1609 unsigned elem_count
= count
- 3;
1610 vtn_fail_if(elem_count
!= val
->type
->length
,
1611 "%s has %u constituents, expected %u",
1612 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1614 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1615 for (unsigned i
= 0; i
< elem_count
; i
++) {
1616 struct vtn_value
*val
= vtn_untyped_value(b
, w
[i
+ 3]);
1618 if (val
->value_type
== vtn_value_type_constant
) {
1619 elems
[i
] = val
->constant
;
1621 vtn_fail_if(val
->value_type
!= vtn_value_type_undef
,
1622 "only constants or undefs allowed for "
1623 "SpvOpConstantComposite");
1624 /* to make it easier, just insert a NULL constant for now */
1625 elems
[i
] = vtn_null_constant(b
, val
->type
->type
);
1629 switch (val
->type
->base_type
) {
1630 case vtn_base_type_vector
: {
1631 assert(glsl_type_is_vector(val
->type
->type
));
1632 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1633 for (unsigned i
= 0; i
< elem_count
; i
++) {
1636 val
->constant
->values
[0].u64
[i
] = elems
[i
]->values
[0].u64
[0];
1639 val
->constant
->values
[0].u32
[i
] = elems
[i
]->values
[0].u32
[0];
1642 val
->constant
->values
[0].u16
[i
] = elems
[i
]->values
[0].u16
[0];
1645 val
->constant
->values
[0].u8
[i
] = elems
[i
]->values
[0].u8
[0];
1648 val
->constant
->values
[0].b
[i
] = elems
[i
]->values
[0].b
[0];
1651 vtn_fail("Invalid SpvOpConstantComposite bit size");
1657 case vtn_base_type_matrix
:
1658 assert(glsl_type_is_matrix(val
->type
->type
));
1659 for (unsigned i
= 0; i
< elem_count
; i
++)
1660 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1663 case vtn_base_type_struct
:
1664 case vtn_base_type_array
:
1665 ralloc_steal(val
->constant
, elems
);
1666 val
->constant
->num_elements
= elem_count
;
1667 val
->constant
->elements
= elems
;
1671 vtn_fail("Result type of %s must be a composite type",
1672 spirv_op_to_string(opcode
));
1677 case SpvOpSpecConstantOp
: {
1678 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1680 case SpvOpVectorShuffle
: {
1681 struct vtn_value
*v0
= &b
->values
[w
[4]];
1682 struct vtn_value
*v1
= &b
->values
[w
[5]];
1684 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1685 v0
->value_type
== vtn_value_type_undef
);
1686 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1687 v1
->value_type
== vtn_value_type_undef
);
1689 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1690 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1692 vtn_assert(len0
+ len1
< 16);
1694 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1695 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1696 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1698 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1699 (void)bit_size0
; (void)bit_size1
;
1701 if (bit_size
== 64) {
1703 if (v0
->value_type
== vtn_value_type_constant
) {
1704 for (unsigned i
= 0; i
< len0
; i
++)
1705 u64
[i
] = v0
->constant
->values
[0].u64
[i
];
1707 if (v1
->value_type
== vtn_value_type_constant
) {
1708 for (unsigned i
= 0; i
< len1
; i
++)
1709 u64
[len0
+ i
] = v1
->constant
->values
[0].u64
[i
];
1712 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1713 uint32_t comp
= w
[i
+ 6];
1714 /* If component is not used, set the value to a known constant
1715 * to detect if it is wrongly used.
1717 if (comp
== (uint32_t)-1)
1718 val
->constant
->values
[0].u64
[j
] = 0xdeadbeefdeadbeef;
1720 val
->constant
->values
[0].u64
[j
] = u64
[comp
];
1723 /* This is for both 32-bit and 16-bit values */
1725 if (v0
->value_type
== vtn_value_type_constant
) {
1726 for (unsigned i
= 0; i
< len0
; i
++)
1727 u32
[i
] = v0
->constant
->values
[0].u32
[i
];
1729 if (v1
->value_type
== vtn_value_type_constant
) {
1730 for (unsigned i
= 0; i
< len1
; i
++)
1731 u32
[len0
+ i
] = v1
->constant
->values
[0].u32
[i
];
1734 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1735 uint32_t comp
= w
[i
+ 6];
1736 /* If component is not used, set the value to a known constant
1737 * to detect if it is wrongly used.
1739 if (comp
== (uint32_t)-1)
1740 val
->constant
->values
[0].u32
[j
] = 0xdeadbeef;
1742 val
->constant
->values
[0].u32
[j
] = u32
[comp
];
1748 case SpvOpCompositeExtract
:
1749 case SpvOpCompositeInsert
: {
1750 struct vtn_value
*comp
;
1751 unsigned deref_start
;
1752 struct nir_constant
**c
;
1753 if (opcode
== SpvOpCompositeExtract
) {
1754 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1756 c
= &comp
->constant
;
1758 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1760 val
->constant
= nir_constant_clone(comp
->constant
,
1767 const struct vtn_type
*type
= comp
->type
;
1768 for (unsigned i
= deref_start
; i
< count
; i
++) {
1769 vtn_fail_if(w
[i
] > type
->length
,
1770 "%uth index of %s is %u but the type has only "
1771 "%u elements", i
- deref_start
,
1772 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1774 switch (type
->base_type
) {
1775 case vtn_base_type_vector
:
1777 type
= type
->array_element
;
1780 case vtn_base_type_matrix
:
1781 assert(col
== 0 && elem
== -1);
1784 type
= type
->array_element
;
1787 case vtn_base_type_array
:
1788 c
= &(*c
)->elements
[w
[i
]];
1789 type
= type
->array_element
;
1792 case vtn_base_type_struct
:
1793 c
= &(*c
)->elements
[w
[i
]];
1794 type
= type
->members
[w
[i
]];
1798 vtn_fail("%s must only index into composite types",
1799 spirv_op_to_string(opcode
));
1803 if (opcode
== SpvOpCompositeExtract
) {
1807 unsigned num_components
= type
->length
;
1808 unsigned bit_size
= glsl_get_bit_size(type
->type
);
1809 for (unsigned i
= 0; i
< num_components
; i
++)
1812 val
->constant
->values
[0].u64
[i
] = (*c
)->values
[col
].u64
[elem
+ i
];
1815 val
->constant
->values
[0].u32
[i
] = (*c
)->values
[col
].u32
[elem
+ i
];
1818 val
->constant
->values
[0].u16
[i
] = (*c
)->values
[col
].u16
[elem
+ i
];
1821 val
->constant
->values
[0].u8
[i
] = (*c
)->values
[col
].u8
[elem
+ i
];
1824 val
->constant
->values
[0].b
[i
] = (*c
)->values
[col
].b
[elem
+ i
];
1827 vtn_fail("Invalid SpvOpCompositeExtract bit size");
1831 struct vtn_value
*insert
=
1832 vtn_value(b
, w
[4], vtn_value_type_constant
);
1833 vtn_assert(insert
->type
== type
);
1835 *c
= insert
->constant
;
1837 unsigned num_components
= type
->length
;
1838 unsigned bit_size
= glsl_get_bit_size(type
->type
);
1839 for (unsigned i
= 0; i
< num_components
; i
++)
1842 (*c
)->values
[col
].u64
[elem
+ i
] = insert
->constant
->values
[0].u64
[i
];
1845 (*c
)->values
[col
].u32
[elem
+ i
] = insert
->constant
->values
[0].u32
[i
];
1848 (*c
)->values
[col
].u16
[elem
+ i
] = insert
->constant
->values
[0].u16
[i
];
1851 (*c
)->values
[col
].u8
[elem
+ i
] = insert
->constant
->values
[0].u8
[i
];
1854 (*c
)->values
[col
].b
[elem
+ i
] = insert
->constant
->values
[0].b
[i
];
1857 vtn_fail("Invalid SpvOpCompositeInsert bit size");
1866 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
1867 nir_alu_type src_alu_type
= dst_alu_type
;
1868 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
1871 vtn_assert(count
<= 7);
1876 /* We have a source in a conversion */
1878 nir_get_nir_type_for_glsl_type(
1879 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1880 /* We use the bitsize of the conversion source to evaluate the opcode later */
1881 bit_size
= glsl_get_bit_size(
1882 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1885 bit_size
= glsl_get_bit_size(val
->type
->type
);
1888 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
1889 nir_alu_type_get_type_size(src_alu_type
),
1890 nir_alu_type_get_type_size(dst_alu_type
));
1891 nir_const_value src
[4];
1893 for (unsigned i
= 0; i
< count
- 4; i
++) {
1894 struct vtn_value
*src_val
=
1895 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
);
1897 /* If this is an unsized source, pull the bit size from the
1898 * source; otherwise, we'll use the bit size from the destination.
1900 if (!nir_alu_type_get_type_size(nir_op_infos
[op
].input_types
[i
]))
1901 bit_size
= glsl_get_bit_size(src_val
->type
->type
);
1903 unsigned j
= swap
? 1 - i
: i
;
1904 src
[j
] = src_val
->constant
->values
[0];
1907 /* fix up fixed size sources */
1914 for (unsigned i
= 0; i
< num_components
; ++i
) {
1916 case 64: src
[1].u32
[i
] = src
[1].u64
[i
]; break;
1917 case 16: src
[1].u32
[i
] = src
[1].u16
[i
]; break;
1918 case 8: src
[1].u32
[i
] = src
[1].u8
[i
]; break;
1927 val
->constant
->values
[0] =
1928 nir_eval_const_opcode(op
, num_components
, bit_size
, src
);
1935 case SpvOpConstantNull
:
1936 val
->constant
= vtn_null_constant(b
, val
->type
->type
);
1939 case SpvOpConstantSampler
:
1940 vtn_fail("OpConstantSampler requires Kernel Capability");
1944 vtn_fail("Unhandled opcode");
1947 /* Now that we have the value, update the workgroup size if needed */
1948 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1951 struct vtn_ssa_value
*
1952 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1954 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1957 if (!glsl_type_is_vector_or_scalar(type
)) {
1958 unsigned elems
= glsl_get_length(type
);
1959 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1960 for (unsigned i
= 0; i
< elems
; i
++) {
1961 const struct glsl_type
*child_type
;
1963 switch (glsl_get_base_type(type
)) {
1965 case GLSL_TYPE_UINT
:
1966 case GLSL_TYPE_INT16
:
1967 case GLSL_TYPE_UINT16
:
1968 case GLSL_TYPE_UINT8
:
1969 case GLSL_TYPE_INT8
:
1970 case GLSL_TYPE_INT64
:
1971 case GLSL_TYPE_UINT64
:
1972 case GLSL_TYPE_BOOL
:
1973 case GLSL_TYPE_FLOAT
:
1974 case GLSL_TYPE_FLOAT16
:
1975 case GLSL_TYPE_DOUBLE
:
1976 child_type
= glsl_get_column_type(type
);
1978 case GLSL_TYPE_ARRAY
:
1979 child_type
= glsl_get_array_element(type
);
1981 case GLSL_TYPE_STRUCT
:
1982 child_type
= glsl_get_struct_field(type
, i
);
1985 vtn_fail("unkown base type");
1988 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
1996 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
1999 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
2000 src
.src_type
= type
;
2005 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
2006 const uint32_t *w
, unsigned count
)
2008 if (opcode
== SpvOpSampledImage
) {
2009 struct vtn_value
*val
=
2010 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2011 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2012 val
->sampled_image
->type
=
2013 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2014 val
->sampled_image
->image
=
2015 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2016 val
->sampled_image
->sampler
=
2017 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
2019 } else if (opcode
== SpvOpImage
) {
2020 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2021 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
2022 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
2023 val
->pointer
= src_val
->sampled_image
->image
;
2025 vtn_assert(src_val
->value_type
== vtn_value_type_pointer
);
2026 val
->pointer
= src_val
->pointer
;
2031 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2032 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2034 struct vtn_sampled_image sampled
;
2035 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
2036 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
2037 sampled
= *sampled_val
->sampled_image
;
2039 vtn_assert(sampled_val
->value_type
== vtn_value_type_pointer
);
2040 sampled
.type
= sampled_val
->pointer
->type
;
2041 sampled
.image
= NULL
;
2042 sampled
.sampler
= sampled_val
->pointer
;
2045 const struct glsl_type
*image_type
= sampled
.type
->type
;
2046 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
2047 const bool is_array
= glsl_sampler_type_is_array(image_type
);
2049 /* Figure out the base texture operation */
2052 case SpvOpImageSampleImplicitLod
:
2053 case SpvOpImageSampleDrefImplicitLod
:
2054 case SpvOpImageSampleProjImplicitLod
:
2055 case SpvOpImageSampleProjDrefImplicitLod
:
2056 texop
= nir_texop_tex
;
2059 case SpvOpImageSampleExplicitLod
:
2060 case SpvOpImageSampleDrefExplicitLod
:
2061 case SpvOpImageSampleProjExplicitLod
:
2062 case SpvOpImageSampleProjDrefExplicitLod
:
2063 texop
= nir_texop_txl
;
2066 case SpvOpImageFetch
:
2067 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
2068 texop
= nir_texop_txf_ms
;
2070 texop
= nir_texop_txf
;
2074 case SpvOpImageGather
:
2075 case SpvOpImageDrefGather
:
2076 texop
= nir_texop_tg4
;
2079 case SpvOpImageQuerySizeLod
:
2080 case SpvOpImageQuerySize
:
2081 texop
= nir_texop_txs
;
2084 case SpvOpImageQueryLod
:
2085 texop
= nir_texop_lod
;
2088 case SpvOpImageQueryLevels
:
2089 texop
= nir_texop_query_levels
;
2092 case SpvOpImageQuerySamples
:
2093 texop
= nir_texop_texture_samples
;
2097 vtn_fail("Unhandled opcode");
2100 nir_tex_src srcs
[10]; /* 10 should be enough */
2101 nir_tex_src
*p
= srcs
;
2103 nir_deref_instr
*sampler
= vtn_pointer_to_deref(b
, sampled
.sampler
);
2104 nir_deref_instr
*texture
=
2105 sampled
.image
? vtn_pointer_to_deref(b
, sampled
.image
) : sampler
;
2107 p
->src
= nir_src_for_ssa(&texture
->dest
.ssa
);
2108 p
->src_type
= nir_tex_src_texture_deref
;
2117 /* These operations require a sampler */
2118 p
->src
= nir_src_for_ssa(&sampler
->dest
.ssa
);
2119 p
->src_type
= nir_tex_src_sampler_deref
;
2123 case nir_texop_txf_ms
:
2126 case nir_texop_query_levels
:
2127 case nir_texop_texture_samples
:
2128 case nir_texop_samples_identical
:
2131 case nir_texop_txf_ms_mcs
:
2132 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2137 struct nir_ssa_def
*coord
;
2138 unsigned coord_components
;
2140 case SpvOpImageSampleImplicitLod
:
2141 case SpvOpImageSampleExplicitLod
:
2142 case SpvOpImageSampleDrefImplicitLod
:
2143 case SpvOpImageSampleDrefExplicitLod
:
2144 case SpvOpImageSampleProjImplicitLod
:
2145 case SpvOpImageSampleProjExplicitLod
:
2146 case SpvOpImageSampleProjDrefImplicitLod
:
2147 case SpvOpImageSampleProjDrefExplicitLod
:
2148 case SpvOpImageFetch
:
2149 case SpvOpImageGather
:
2150 case SpvOpImageDrefGather
:
2151 case SpvOpImageQueryLod
: {
2152 /* All these types have the coordinate as their first real argument */
2153 switch (sampler_dim
) {
2154 case GLSL_SAMPLER_DIM_1D
:
2155 case GLSL_SAMPLER_DIM_BUF
:
2156 coord_components
= 1;
2158 case GLSL_SAMPLER_DIM_2D
:
2159 case GLSL_SAMPLER_DIM_RECT
:
2160 case GLSL_SAMPLER_DIM_MS
:
2161 coord_components
= 2;
2163 case GLSL_SAMPLER_DIM_3D
:
2164 case GLSL_SAMPLER_DIM_CUBE
:
2165 coord_components
= 3;
2168 vtn_fail("Invalid sampler type");
2171 if (is_array
&& texop
!= nir_texop_lod
)
2174 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
2175 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2176 (1 << coord_components
) - 1));
2177 p
->src_type
= nir_tex_src_coord
;
2184 coord_components
= 0;
2189 case SpvOpImageSampleProjImplicitLod
:
2190 case SpvOpImageSampleProjExplicitLod
:
2191 case SpvOpImageSampleProjDrefImplicitLod
:
2192 case SpvOpImageSampleProjDrefExplicitLod
:
2193 /* These have the projector as the last coordinate component */
2194 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2195 p
->src_type
= nir_tex_src_projector
;
2203 bool is_shadow
= false;
2204 unsigned gather_component
= 0;
2206 case SpvOpImageSampleDrefImplicitLod
:
2207 case SpvOpImageSampleDrefExplicitLod
:
2208 case SpvOpImageSampleProjDrefImplicitLod
:
2209 case SpvOpImageSampleProjDrefExplicitLod
:
2210 case SpvOpImageDrefGather
:
2211 /* These all have an explicit depth value as their next source */
2213 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2216 case SpvOpImageGather
:
2217 /* This has a component as its next source */
2219 vtn_value(b
, w
[idx
++], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
2226 /* For OpImageQuerySizeLod, we always have an LOD */
2227 if (opcode
== SpvOpImageQuerySizeLod
)
2228 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2230 /* Now we need to handle some number of optional arguments */
2231 const struct vtn_ssa_value
*gather_offsets
= NULL
;
2233 uint32_t operands
= w
[idx
++];
2235 if (operands
& SpvImageOperandsBiasMask
) {
2236 vtn_assert(texop
== nir_texop_tex
);
2237 texop
= nir_texop_txb
;
2238 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
2241 if (operands
& SpvImageOperandsLodMask
) {
2242 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2243 texop
== nir_texop_txs
);
2244 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2247 if (operands
& SpvImageOperandsGradMask
) {
2248 vtn_assert(texop
== nir_texop_txl
);
2249 texop
= nir_texop_txd
;
2250 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
2251 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
2254 if (operands
& SpvImageOperandsOffsetMask
||
2255 operands
& SpvImageOperandsConstOffsetMask
)
2256 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
2258 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2259 nir_tex_src none
= {0};
2260 gather_offsets
= vtn_ssa_value(b
, w
[idx
++]);
2264 if (operands
& SpvImageOperandsSampleMask
) {
2265 vtn_assert(texop
== nir_texop_txf_ms
);
2266 texop
= nir_texop_txf_ms
;
2267 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
2270 if (operands
& SpvImageOperandsMinLodMask
) {
2271 vtn_assert(texop
== nir_texop_tex
||
2272 texop
== nir_texop_txb
||
2273 texop
== nir_texop_txd
);
2274 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_min_lod
);
2277 /* We should have now consumed exactly all of the arguments */
2278 vtn_assert(idx
== count
);
2280 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2283 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2285 instr
->coord_components
= coord_components
;
2286 instr
->sampler_dim
= sampler_dim
;
2287 instr
->is_array
= is_array
;
2288 instr
->is_shadow
= is_shadow
;
2289 instr
->is_new_style_shadow
=
2290 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2291 instr
->component
= gather_component
;
2293 switch (glsl_get_sampler_result_type(image_type
)) {
2294 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
2295 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
2296 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
2297 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
2299 vtn_fail("Invalid base type for sampler result");
2302 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2303 nir_tex_instr_dest_size(instr
), 32, NULL
);
2305 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2306 nir_tex_instr_dest_size(instr
));
2309 nir_instr
*instruction
;
2310 if (gather_offsets
) {
2311 vtn_assert(glsl_get_base_type(gather_offsets
->type
) == GLSL_TYPE_ARRAY
);
2312 vtn_assert(glsl_get_length(gather_offsets
->type
) == 4);
2313 nir_tex_instr
*instrs
[4] = {instr
, NULL
, NULL
, NULL
};
2315 /* Copy the current instruction 4x */
2316 for (uint32_t i
= 1; i
< 4; i
++) {
2317 instrs
[i
] = nir_tex_instr_create(b
->shader
, instr
->num_srcs
);
2318 instrs
[i
]->op
= instr
->op
;
2319 instrs
[i
]->coord_components
= instr
->coord_components
;
2320 instrs
[i
]->sampler_dim
= instr
->sampler_dim
;
2321 instrs
[i
]->is_array
= instr
->is_array
;
2322 instrs
[i
]->is_shadow
= instr
->is_shadow
;
2323 instrs
[i
]->is_new_style_shadow
= instr
->is_new_style_shadow
;
2324 instrs
[i
]->component
= instr
->component
;
2325 instrs
[i
]->dest_type
= instr
->dest_type
;
2327 memcpy(instrs
[i
]->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2329 nir_ssa_dest_init(&instrs
[i
]->instr
, &instrs
[i
]->dest
,
2330 nir_tex_instr_dest_size(instr
), 32, NULL
);
2333 /* Fill in the last argument with the offset from the passed in offsets
2334 * and insert the instruction into the stream.
2336 for (uint32_t i
= 0; i
< 4; i
++) {
2338 src
.src
= nir_src_for_ssa(gather_offsets
->elems
[i
]->def
);
2339 src
.src_type
= nir_tex_src_offset
;
2340 instrs
[i
]->src
[instrs
[i
]->num_srcs
- 1] = src
;
2341 nir_builder_instr_insert(&b
->nb
, &instrs
[i
]->instr
);
2344 /* Combine the results of the 4 instructions by taking their .w
2347 nir_alu_instr
*vec4
= nir_alu_instr_create(b
->shader
, nir_op_vec4
);
2348 nir_ssa_dest_init(&vec4
->instr
, &vec4
->dest
.dest
, 4, 32, NULL
);
2349 vec4
->dest
.write_mask
= 0xf;
2350 for (uint32_t i
= 0; i
< 4; i
++) {
2351 vec4
->src
[i
].src
= nir_src_for_ssa(&instrs
[i
]->dest
.ssa
);
2352 vec4
->src
[i
].swizzle
[0] = 3;
2354 def
= &vec4
->dest
.dest
.ssa
;
2355 instruction
= &vec4
->instr
;
2357 def
= &instr
->dest
.ssa
;
2358 instruction
= &instr
->instr
;
2361 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
2362 val
->ssa
->def
= def
;
2364 nir_builder_instr_insert(&b
->nb
, instruction
);
2368 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2369 const uint32_t *w
, nir_src
*src
)
2372 case SpvOpAtomicIIncrement
:
2373 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
2376 case SpvOpAtomicIDecrement
:
2377 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
2380 case SpvOpAtomicISub
:
2382 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
2385 case SpvOpAtomicCompareExchange
:
2386 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
2387 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
2390 case SpvOpAtomicExchange
:
2391 case SpvOpAtomicIAdd
:
2392 case SpvOpAtomicSMin
:
2393 case SpvOpAtomicUMin
:
2394 case SpvOpAtomicSMax
:
2395 case SpvOpAtomicUMax
:
2396 case SpvOpAtomicAnd
:
2398 case SpvOpAtomicXor
:
2399 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
2403 vtn_fail("Invalid SPIR-V atomic");
2407 static nir_ssa_def
*
2408 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2410 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
2412 /* The image_load_store intrinsics assume a 4-dim coordinate */
2413 unsigned dim
= glsl_get_vector_elements(coord
->type
);
2414 unsigned swizzle
[4];
2415 for (unsigned i
= 0; i
< 4; i
++)
2416 swizzle
[i
] = MIN2(i
, dim
- 1);
2418 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
2421 static nir_ssa_def
*
2422 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2424 if (value
->num_components
== 4)
2428 for (unsigned i
= 0; i
< 4; i
++)
2429 swiz
[i
] = i
< value
->num_components
? i
: 0;
2430 return nir_swizzle(b
, value
, swiz
, 4, false);
2434 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2435 const uint32_t *w
, unsigned count
)
2437 /* Just get this one out of the way */
2438 if (opcode
== SpvOpImageTexelPointer
) {
2439 struct vtn_value
*val
=
2440 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2441 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2443 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2444 val
->image
->coord
= get_image_coord(b
, w
[4]);
2445 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
2449 struct vtn_image_pointer image
;
2452 case SpvOpAtomicExchange
:
2453 case SpvOpAtomicCompareExchange
:
2454 case SpvOpAtomicCompareExchangeWeak
:
2455 case SpvOpAtomicIIncrement
:
2456 case SpvOpAtomicIDecrement
:
2457 case SpvOpAtomicIAdd
:
2458 case SpvOpAtomicISub
:
2459 case SpvOpAtomicLoad
:
2460 case SpvOpAtomicSMin
:
2461 case SpvOpAtomicUMin
:
2462 case SpvOpAtomicSMax
:
2463 case SpvOpAtomicUMax
:
2464 case SpvOpAtomicAnd
:
2466 case SpvOpAtomicXor
:
2467 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
2470 case SpvOpAtomicStore
:
2471 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
2474 case SpvOpImageQuerySize
:
2475 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2477 image
.sample
= NULL
;
2480 case SpvOpImageRead
:
2481 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2482 image
.coord
= get_image_coord(b
, w
[4]);
2484 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
2485 vtn_assert(w
[5] == SpvImageOperandsSampleMask
);
2486 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
2488 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2492 case SpvOpImageWrite
:
2493 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2494 image
.coord
= get_image_coord(b
, w
[2]);
2498 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
2499 vtn_assert(w
[4] == SpvImageOperandsSampleMask
);
2500 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
2502 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2507 vtn_fail("Invalid image opcode");
2510 nir_intrinsic_op op
;
2512 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
2513 OP(ImageQuerySize
, size
)
2515 OP(ImageWrite
, store
)
2516 OP(AtomicLoad
, load
)
2517 OP(AtomicStore
, store
)
2518 OP(AtomicExchange
, atomic_exchange
)
2519 OP(AtomicCompareExchange
, atomic_comp_swap
)
2520 OP(AtomicIIncrement
, atomic_add
)
2521 OP(AtomicIDecrement
, atomic_add
)
2522 OP(AtomicIAdd
, atomic_add
)
2523 OP(AtomicISub
, atomic_add
)
2524 OP(AtomicSMin
, atomic_min
)
2525 OP(AtomicUMin
, atomic_min
)
2526 OP(AtomicSMax
, atomic_max
)
2527 OP(AtomicUMax
, atomic_max
)
2528 OP(AtomicAnd
, atomic_and
)
2529 OP(AtomicOr
, atomic_or
)
2530 OP(AtomicXor
, atomic_xor
)
2533 vtn_fail("Invalid image opcode");
2536 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
2538 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
2539 intrin
->src
[0] = nir_src_for_ssa(&image_deref
->dest
.ssa
);
2541 /* ImageQuerySize doesn't take any extra parameters */
2542 if (opcode
!= SpvOpImageQuerySize
) {
2543 /* The image coordinate is always 4 components but we may not have that
2544 * many. Swizzle to compensate.
2546 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
2547 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
2551 case SpvOpAtomicLoad
:
2552 case SpvOpImageQuerySize
:
2553 case SpvOpImageRead
:
2555 case SpvOpAtomicStore
:
2556 case SpvOpImageWrite
: {
2557 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
2558 nir_ssa_def
*value
= vtn_ssa_value(b
, value_id
)->def
;
2559 /* nir_intrinsic_image_deref_store always takes a vec4 value */
2560 assert(op
== nir_intrinsic_image_deref_store
);
2561 intrin
->num_components
= 4;
2562 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
));
2566 case SpvOpAtomicCompareExchange
:
2567 case SpvOpAtomicIIncrement
:
2568 case SpvOpAtomicIDecrement
:
2569 case SpvOpAtomicExchange
:
2570 case SpvOpAtomicIAdd
:
2571 case SpvOpAtomicISub
:
2572 case SpvOpAtomicSMin
:
2573 case SpvOpAtomicUMin
:
2574 case SpvOpAtomicSMax
:
2575 case SpvOpAtomicUMax
:
2576 case SpvOpAtomicAnd
:
2578 case SpvOpAtomicXor
:
2579 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
2583 vtn_fail("Invalid image opcode");
2586 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
2587 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2588 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2590 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
2591 intrin
->num_components
= nir_intrinsic_infos
[op
].dest_components
;
2592 if (intrin
->num_components
== 0)
2593 intrin
->num_components
= dest_components
;
2595 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2596 intrin
->num_components
, 32, NULL
);
2598 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2600 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
2601 if (intrin
->num_components
!= dest_components
)
2602 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
2604 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
2605 val
->ssa
->def
= result
;
2607 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2611 static nir_intrinsic_op
2612 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2615 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2616 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2617 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2618 OP(AtomicExchange
, atomic_exchange
)
2619 OP(AtomicCompareExchange
, atomic_comp_swap
)
2620 OP(AtomicIIncrement
, atomic_add
)
2621 OP(AtomicIDecrement
, atomic_add
)
2622 OP(AtomicIAdd
, atomic_add
)
2623 OP(AtomicISub
, atomic_add
)
2624 OP(AtomicSMin
, atomic_imin
)
2625 OP(AtomicUMin
, atomic_umin
)
2626 OP(AtomicSMax
, atomic_imax
)
2627 OP(AtomicUMax
, atomic_umax
)
2628 OP(AtomicAnd
, atomic_and
)
2629 OP(AtomicOr
, atomic_or
)
2630 OP(AtomicXor
, atomic_xor
)
2633 vtn_fail("Invalid SSBO atomic");
2637 static nir_intrinsic_op
2638 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2641 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
2642 OP(AtomicLoad
, read_deref
)
2643 OP(AtomicExchange
, exchange
)
2644 OP(AtomicCompareExchange
, comp_swap
)
2645 OP(AtomicIIncrement
, inc_deref
)
2646 OP(AtomicIDecrement
, post_dec_deref
)
2647 OP(AtomicIAdd
, add_deref
)
2648 OP(AtomicISub
, add_deref
)
2649 OP(AtomicUMin
, min_deref
)
2650 OP(AtomicUMax
, max_deref
)
2651 OP(AtomicAnd
, and_deref
)
2652 OP(AtomicOr
, or_deref
)
2653 OP(AtomicXor
, xor_deref
)
2656 /* We left the following out: AtomicStore, AtomicSMin and
2657 * AtomicSmax. Right now there are not nir intrinsics for them. At this
2658 * moment Atomic Counter support is needed for ARB_spirv support, so is
2659 * only need to support GLSL Atomic Counters that are uints and don't
2660 * allow direct storage.
2662 unreachable("Invalid uniform atomic");
2666 static nir_intrinsic_op
2667 get_shared_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2670 case SpvOpAtomicLoad
: return nir_intrinsic_load_shared
;
2671 case SpvOpAtomicStore
: return nir_intrinsic_store_shared
;
2672 #define OP(S, N) case SpvOp##S: return nir_intrinsic_shared_##N;
2673 OP(AtomicExchange
, atomic_exchange
)
2674 OP(AtomicCompareExchange
, atomic_comp_swap
)
2675 OP(AtomicIIncrement
, atomic_add
)
2676 OP(AtomicIDecrement
, atomic_add
)
2677 OP(AtomicIAdd
, atomic_add
)
2678 OP(AtomicISub
, atomic_add
)
2679 OP(AtomicSMin
, atomic_imin
)
2680 OP(AtomicUMin
, atomic_umin
)
2681 OP(AtomicSMax
, atomic_imax
)
2682 OP(AtomicUMax
, atomic_umax
)
2683 OP(AtomicAnd
, atomic_and
)
2684 OP(AtomicOr
, atomic_or
)
2685 OP(AtomicXor
, atomic_xor
)
2688 vtn_fail("Invalid shared atomic");
2692 static nir_intrinsic_op
2693 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2696 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
2697 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
2698 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
2699 OP(AtomicExchange
, atomic_exchange
)
2700 OP(AtomicCompareExchange
, atomic_comp_swap
)
2701 OP(AtomicIIncrement
, atomic_add
)
2702 OP(AtomicIDecrement
, atomic_add
)
2703 OP(AtomicIAdd
, atomic_add
)
2704 OP(AtomicISub
, atomic_add
)
2705 OP(AtomicSMin
, atomic_imin
)
2706 OP(AtomicUMin
, atomic_umin
)
2707 OP(AtomicSMax
, atomic_imax
)
2708 OP(AtomicUMax
, atomic_umax
)
2709 OP(AtomicAnd
, atomic_and
)
2710 OP(AtomicOr
, atomic_or
)
2711 OP(AtomicXor
, atomic_xor
)
2714 vtn_fail("Invalid shared atomic");
2719 * Handles shared atomics, ssbo atomics and atomic counters.
2722 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
2723 const uint32_t *w
, unsigned count
)
2725 struct vtn_pointer
*ptr
;
2726 nir_intrinsic_instr
*atomic
;
2729 case SpvOpAtomicLoad
:
2730 case SpvOpAtomicExchange
:
2731 case SpvOpAtomicCompareExchange
:
2732 case SpvOpAtomicCompareExchangeWeak
:
2733 case SpvOpAtomicIIncrement
:
2734 case SpvOpAtomicIDecrement
:
2735 case SpvOpAtomicIAdd
:
2736 case SpvOpAtomicISub
:
2737 case SpvOpAtomicSMin
:
2738 case SpvOpAtomicUMin
:
2739 case SpvOpAtomicSMax
:
2740 case SpvOpAtomicUMax
:
2741 case SpvOpAtomicAnd
:
2743 case SpvOpAtomicXor
:
2744 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2747 case SpvOpAtomicStore
:
2748 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2752 vtn_fail("Invalid SPIR-V atomic");
2756 SpvScope scope = w[4];
2757 SpvMemorySemanticsMask semantics = w[5];
2760 /* uniform as "atomic counter uniform" */
2761 if (ptr
->mode
== vtn_variable_mode_uniform
) {
2762 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2763 const struct glsl_type
*deref_type
= deref
->type
;
2764 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
2765 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2766 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2768 /* SSBO needs to initialize index/offset. In this case we don't need to,
2769 * as that info is already stored on the ptr->var->var nir_variable (see
2770 * vtn_create_variable)
2774 case SpvOpAtomicLoad
:
2775 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2778 case SpvOpAtomicStore
:
2779 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2780 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2783 case SpvOpAtomicExchange
:
2784 case SpvOpAtomicCompareExchange
:
2785 case SpvOpAtomicCompareExchangeWeak
:
2786 case SpvOpAtomicIIncrement
:
2787 case SpvOpAtomicIDecrement
:
2788 case SpvOpAtomicIAdd
:
2789 case SpvOpAtomicISub
:
2790 case SpvOpAtomicSMin
:
2791 case SpvOpAtomicUMin
:
2792 case SpvOpAtomicSMax
:
2793 case SpvOpAtomicUMax
:
2794 case SpvOpAtomicAnd
:
2796 case SpvOpAtomicXor
:
2797 /* Nothing: we don't need to call fill_common_atomic_sources here, as
2798 * atomic counter uniforms doesn't have sources
2803 unreachable("Invalid SPIR-V atomic");
2806 } else if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
2807 nir_ssa_def
*offset
, *index
;
2808 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
2810 nir_intrinsic_op op
;
2811 if (ptr
->mode
== vtn_variable_mode_ssbo
) {
2812 op
= get_ssbo_nir_atomic_op(b
, opcode
);
2814 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
&&
2815 b
->options
->lower_workgroup_access_to_offsets
);
2816 op
= get_shared_nir_atomic_op(b
, opcode
);
2819 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2823 case SpvOpAtomicLoad
:
2824 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2825 nir_intrinsic_set_align(atomic
, 4, 0);
2826 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2827 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2828 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2831 case SpvOpAtomicStore
:
2832 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2833 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2834 nir_intrinsic_set_align(atomic
, 4, 0);
2835 atomic
->src
[src
++] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2836 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2837 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2838 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2841 case SpvOpAtomicExchange
:
2842 case SpvOpAtomicCompareExchange
:
2843 case SpvOpAtomicCompareExchangeWeak
:
2844 case SpvOpAtomicIIncrement
:
2845 case SpvOpAtomicIDecrement
:
2846 case SpvOpAtomicIAdd
:
2847 case SpvOpAtomicISub
:
2848 case SpvOpAtomicSMin
:
2849 case SpvOpAtomicUMin
:
2850 case SpvOpAtomicSMax
:
2851 case SpvOpAtomicUMax
:
2852 case SpvOpAtomicAnd
:
2854 case SpvOpAtomicXor
:
2855 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2856 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2857 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2858 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
2862 vtn_fail("Invalid SPIR-V atomic");
2865 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2866 const struct glsl_type
*deref_type
= deref
->type
;
2867 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
2868 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2869 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2872 case SpvOpAtomicLoad
:
2873 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2876 case SpvOpAtomicStore
:
2877 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2878 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2879 atomic
->src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
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 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
2900 vtn_fail("Invalid SPIR-V atomic");
2904 if (opcode
!= SpvOpAtomicStore
) {
2905 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2907 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
2908 glsl_get_vector_elements(type
->type
),
2909 glsl_get_bit_size(type
->type
), NULL
);
2911 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2912 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
2913 val
->ssa
->def
= &atomic
->dest
.ssa
;
2914 val
->ssa
->type
= type
->type
;
2917 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
2920 static nir_alu_instr
*
2921 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
2924 switch (num_components
) {
2925 case 1: op
= nir_op_imov
; break;
2926 case 2: op
= nir_op_vec2
; break;
2927 case 3: op
= nir_op_vec3
; break;
2928 case 4: op
= nir_op_vec4
; break;
2929 default: vtn_fail("bad vector size");
2932 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
2933 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
2935 vec
->dest
.write_mask
= (1 << num_components
) - 1;
2940 struct vtn_ssa_value
*
2941 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
2943 if (src
->transposed
)
2944 return src
->transposed
;
2946 struct vtn_ssa_value
*dest
=
2947 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
2949 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
2950 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
2951 glsl_get_bit_size(src
->type
));
2952 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2953 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
2954 vec
->src
[0].swizzle
[0] = i
;
2956 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
2957 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
2958 vec
->src
[j
].swizzle
[0] = i
;
2961 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2962 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
2965 dest
->transposed
= src
;
2971 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
2973 return nir_channel(&b
->nb
, src
, index
);
2977 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
2980 nir_alu_instr
*vec
= create_vec(b
, src
->num_components
,
2983 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
2985 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
2987 vec
->src
[i
].src
= nir_src_for_ssa(src
);
2988 vec
->src
[i
].swizzle
[0] = i
;
2992 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2994 return &vec
->dest
.dest
.ssa
;
2997 static nir_ssa_def
*
2998 nir_ieq_imm(nir_builder
*b
, nir_ssa_def
*x
, uint64_t i
)
3000 return nir_ieq(b
, x
, nir_imm_intN_t(b
, i
, x
->bit_size
));
3004 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
3007 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
3008 for (unsigned i
= 1; i
< src
->num_components
; i
++)
3009 dest
= nir_bcsel(&b
->nb
, nir_ieq_imm(&b
->nb
, index
, i
),
3010 vtn_vector_extract(b
, src
, i
), dest
);
3016 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
3017 nir_ssa_def
*insert
, nir_ssa_def
*index
)
3019 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
3020 for (unsigned i
= 1; i
< src
->num_components
; i
++)
3021 dest
= nir_bcsel(&b
->nb
, nir_ieq_imm(&b
->nb
, index
, i
),
3022 vtn_vector_insert(b
, src
, insert
, i
), dest
);
3027 static nir_ssa_def
*
3028 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
3029 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
3030 const uint32_t *indices
)
3032 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
3034 for (unsigned i
= 0; i
< num_components
; i
++) {
3035 uint32_t index
= indices
[i
];
3036 if (index
== 0xffffffff) {
3038 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
3039 } else if (index
< src0
->num_components
) {
3040 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
3041 vec
->src
[i
].swizzle
[0] = index
;
3043 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
3044 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
3048 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3050 return &vec
->dest
.dest
.ssa
;
3054 * Concatentates a number of vectors/scalars together to produce a vector
3056 static nir_ssa_def
*
3057 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
3058 unsigned num_srcs
, nir_ssa_def
**srcs
)
3060 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
3062 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3064 * "When constructing a vector, there must be at least two Constituent
3067 vtn_assert(num_srcs
>= 2);
3069 unsigned dest_idx
= 0;
3070 for (unsigned i
= 0; i
< num_srcs
; i
++) {
3071 nir_ssa_def
*src
= srcs
[i
];
3072 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
3073 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
3074 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
3075 vec
->src
[dest_idx
].swizzle
[0] = j
;
3080 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3082 * "When constructing a vector, the total number of components in all
3083 * the operands must equal the number of components in Result Type."
3085 vtn_assert(dest_idx
== num_components
);
3087 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3089 return &vec
->dest
.dest
.ssa
;
3092 static struct vtn_ssa_value
*
3093 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3095 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3096 dest
->type
= src
->type
;
3098 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3099 dest
->def
= src
->def
;
3101 unsigned elems
= glsl_get_length(src
->type
);
3103 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3104 for (unsigned i
= 0; i
< elems
; i
++)
3105 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3111 static struct vtn_ssa_value
*
3112 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3113 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3114 unsigned num_indices
)
3116 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3118 struct vtn_ssa_value
*cur
= dest
;
3120 for (i
= 0; i
< num_indices
- 1; i
++) {
3121 cur
= cur
->elems
[indices
[i
]];
3124 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3125 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3126 * the component granularity. In that case, the last index will be
3127 * the index to insert the scalar into the vector.
3130 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
3132 cur
->elems
[indices
[i
]] = insert
;
3138 static struct vtn_ssa_value
*
3139 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3140 const uint32_t *indices
, unsigned num_indices
)
3142 struct vtn_ssa_value
*cur
= src
;
3143 for (unsigned i
= 0; i
< num_indices
; i
++) {
3144 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3145 vtn_assert(i
== num_indices
- 1);
3146 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3147 * the component granularity. The last index will be the index of the
3148 * vector to extract.
3151 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
3152 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
3153 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
3156 cur
= cur
->elems
[indices
[i
]];
3164 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3165 const uint32_t *w
, unsigned count
)
3167 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
3168 const struct glsl_type
*type
=
3169 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
3170 val
->ssa
= vtn_create_ssa_value(b
, type
);
3173 case SpvOpVectorExtractDynamic
:
3174 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3175 vtn_ssa_value(b
, w
[4])->def
);
3178 case SpvOpVectorInsertDynamic
:
3179 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3180 vtn_ssa_value(b
, w
[4])->def
,
3181 vtn_ssa_value(b
, w
[5])->def
);
3184 case SpvOpVectorShuffle
:
3185 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
3186 vtn_ssa_value(b
, w
[3])->def
,
3187 vtn_ssa_value(b
, w
[4])->def
,
3191 case SpvOpCompositeConstruct
: {
3192 unsigned elems
= count
- 3;
3194 if (glsl_type_is_vector_or_scalar(type
)) {
3195 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3196 for (unsigned i
= 0; i
< elems
; i
++)
3197 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
3199 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
3202 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3203 for (unsigned i
= 0; i
< elems
; i
++)
3204 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3208 case SpvOpCompositeExtract
:
3209 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3213 case SpvOpCompositeInsert
:
3214 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3215 vtn_ssa_value(b
, w
[3]),
3219 case SpvOpCopyObject
:
3220 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3224 vtn_fail("unknown composite operation");
3229 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3231 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3232 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3236 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3237 SpvMemorySemanticsMask semantics
)
3239 static const SpvMemorySemanticsMask all_memory_semantics
=
3240 SpvMemorySemanticsUniformMemoryMask
|
3241 SpvMemorySemanticsWorkgroupMemoryMask
|
3242 SpvMemorySemanticsAtomicCounterMemoryMask
|
3243 SpvMemorySemanticsImageMemoryMask
;
3245 /* If we're not actually doing a memory barrier, bail */
3246 if (!(semantics
& all_memory_semantics
))
3249 /* GL and Vulkan don't have these */
3250 vtn_assert(scope
!= SpvScopeCrossDevice
);
3252 if (scope
== SpvScopeSubgroup
)
3253 return; /* Nothing to do here */
3255 if (scope
== SpvScopeWorkgroup
) {
3256 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3260 /* There's only two scopes thing left */
3261 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3263 if ((semantics
& all_memory_semantics
) == all_memory_semantics
) {
3264 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3268 /* Issue a bunch of more specific barriers */
3269 uint32_t bits
= semantics
;
3271 SpvMemorySemanticsMask semantic
= 1 << u_bit_scan(&bits
);
3273 case SpvMemorySemanticsUniformMemoryMask
:
3274 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3276 case SpvMemorySemanticsWorkgroupMemoryMask
:
3277 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3279 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3280 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3282 case SpvMemorySemanticsImageMemoryMask
:
3283 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3292 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3293 const uint32_t *w
, unsigned count
)
3296 case SpvOpEmitVertex
:
3297 case SpvOpEmitStreamVertex
:
3298 case SpvOpEndPrimitive
:
3299 case SpvOpEndStreamPrimitive
: {
3300 nir_intrinsic_op intrinsic_op
;
3302 case SpvOpEmitVertex
:
3303 case SpvOpEmitStreamVertex
:
3304 intrinsic_op
= nir_intrinsic_emit_vertex
;
3306 case SpvOpEndPrimitive
:
3307 case SpvOpEndStreamPrimitive
:
3308 intrinsic_op
= nir_intrinsic_end_primitive
;
3311 unreachable("Invalid opcode");
3314 nir_intrinsic_instr
*intrin
=
3315 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3318 case SpvOpEmitStreamVertex
:
3319 case SpvOpEndStreamPrimitive
: {
3320 unsigned stream
= vtn_constant_value(b
, w
[1])->values
[0].u32
[0];
3321 nir_intrinsic_set_stream_id(intrin
, stream
);
3329 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3333 case SpvOpMemoryBarrier
: {
3334 SpvScope scope
= vtn_constant_value(b
, w
[1])->values
[0].u32
[0];
3335 SpvMemorySemanticsMask semantics
=
3336 vtn_constant_value(b
, w
[2])->values
[0].u32
[0];
3337 vtn_emit_memory_barrier(b
, scope
, semantics
);
3341 case SpvOpControlBarrier
: {
3342 SpvScope execution_scope
=
3343 vtn_constant_value(b
, w
[1])->values
[0].u32
[0];
3344 if (execution_scope
== SpvScopeWorkgroup
)
3345 vtn_emit_barrier(b
, nir_intrinsic_barrier
);
3347 SpvScope memory_scope
=
3348 vtn_constant_value(b
, w
[2])->values
[0].u32
[0];
3349 SpvMemorySemanticsMask memory_semantics
=
3350 vtn_constant_value(b
, w
[3])->values
[0].u32
[0];
3351 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
3356 unreachable("unknown barrier instruction");
3361 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
3362 SpvExecutionMode mode
)
3365 case SpvExecutionModeInputPoints
:
3366 case SpvExecutionModeOutputPoints
:
3367 return 0; /* GL_POINTS */
3368 case SpvExecutionModeInputLines
:
3369 return 1; /* GL_LINES */
3370 case SpvExecutionModeInputLinesAdjacency
:
3371 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3372 case SpvExecutionModeTriangles
:
3373 return 4; /* GL_TRIANGLES */
3374 case SpvExecutionModeInputTrianglesAdjacency
:
3375 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3376 case SpvExecutionModeQuads
:
3377 return 7; /* GL_QUADS */
3378 case SpvExecutionModeIsolines
:
3379 return 0x8E7A; /* GL_ISOLINES */
3380 case SpvExecutionModeOutputLineStrip
:
3381 return 3; /* GL_LINE_STRIP */
3382 case SpvExecutionModeOutputTriangleStrip
:
3383 return 5; /* GL_TRIANGLE_STRIP */
3385 vtn_fail("Invalid primitive type");
3390 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
3391 SpvExecutionMode mode
)
3394 case SpvExecutionModeInputPoints
:
3396 case SpvExecutionModeInputLines
:
3398 case SpvExecutionModeInputLinesAdjacency
:
3400 case SpvExecutionModeTriangles
:
3402 case SpvExecutionModeInputTrianglesAdjacency
:
3405 vtn_fail("Invalid GS input mode");
3409 static gl_shader_stage
3410 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
3413 case SpvExecutionModelVertex
:
3414 return MESA_SHADER_VERTEX
;
3415 case SpvExecutionModelTessellationControl
:
3416 return MESA_SHADER_TESS_CTRL
;
3417 case SpvExecutionModelTessellationEvaluation
:
3418 return MESA_SHADER_TESS_EVAL
;
3419 case SpvExecutionModelGeometry
:
3420 return MESA_SHADER_GEOMETRY
;
3421 case SpvExecutionModelFragment
:
3422 return MESA_SHADER_FRAGMENT
;
3423 case SpvExecutionModelGLCompute
:
3424 return MESA_SHADER_COMPUTE
;
3425 case SpvExecutionModelKernel
:
3426 return MESA_SHADER_KERNEL
;
3428 vtn_fail("Unsupported execution model");
3432 #define spv_check_supported(name, cap) do { \
3433 if (!(b->options && b->options->caps.name)) \
3434 vtn_warn("Unsupported SPIR-V capability: %s", \
3435 spirv_capability_to_string(cap)); \
3440 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
3443 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
3444 /* Let this be a name label regardless */
3445 unsigned name_words
;
3446 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
3448 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
3449 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
3452 vtn_assert(b
->entry_point
== NULL
);
3453 b
->entry_point
= entry_point
;
3457 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3458 const uint32_t *w
, unsigned count
)
3465 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
3466 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
3467 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
3468 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
3469 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
3470 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
3473 uint32_t version
= w
[2];
3476 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
3478 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
3482 case SpvOpSourceExtension
:
3483 case SpvOpSourceContinued
:
3484 case SpvOpExtension
:
3485 case SpvOpModuleProcessed
:
3486 /* Unhandled, but these are for debug so that's ok. */
3489 case SpvOpCapability
: {
3490 SpvCapability cap
= w
[1];
3492 case SpvCapabilityMatrix
:
3493 case SpvCapabilityShader
:
3494 case SpvCapabilityGeometry
:
3495 case SpvCapabilityGeometryPointSize
:
3496 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
3497 case SpvCapabilitySampledImageArrayDynamicIndexing
:
3498 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
3499 case SpvCapabilityStorageImageArrayDynamicIndexing
:
3500 case SpvCapabilityImageRect
:
3501 case SpvCapabilitySampledRect
:
3502 case SpvCapabilitySampled1D
:
3503 case SpvCapabilityImage1D
:
3504 case SpvCapabilitySampledCubeArray
:
3505 case SpvCapabilityImageCubeArray
:
3506 case SpvCapabilitySampledBuffer
:
3507 case SpvCapabilityImageBuffer
:
3508 case SpvCapabilityImageQuery
:
3509 case SpvCapabilityDerivativeControl
:
3510 case SpvCapabilityInterpolationFunction
:
3511 case SpvCapabilityMultiViewport
:
3512 case SpvCapabilitySampleRateShading
:
3513 case SpvCapabilityClipDistance
:
3514 case SpvCapabilityCullDistance
:
3515 case SpvCapabilityInputAttachment
:
3516 case SpvCapabilityImageGatherExtended
:
3517 case SpvCapabilityStorageImageExtendedFormats
:
3520 case SpvCapabilityLinkage
:
3521 case SpvCapabilityVector16
:
3522 case SpvCapabilityFloat16Buffer
:
3523 case SpvCapabilityFloat16
:
3524 case SpvCapabilityInt8
:
3525 case SpvCapabilitySparseResidency
:
3526 vtn_warn("Unsupported SPIR-V capability: %s",
3527 spirv_capability_to_string(cap
));
3530 case SpvCapabilityMinLod
:
3531 spv_check_supported(min_lod
, cap
);
3534 case SpvCapabilityAtomicStorage
:
3535 spv_check_supported(atomic_storage
, cap
);
3538 case SpvCapabilityFloat64
:
3539 spv_check_supported(float64
, cap
);
3541 case SpvCapabilityInt64
:
3542 spv_check_supported(int64
, cap
);
3544 case SpvCapabilityInt16
:
3545 spv_check_supported(int16
, cap
);
3548 case SpvCapabilityTransformFeedback
:
3549 spv_check_supported(transform_feedback
, cap
);
3552 case SpvCapabilityGeometryStreams
:
3553 spv_check_supported(geometry_streams
, cap
);
3556 case SpvCapabilityInt64Atomics
:
3557 spv_check_supported(int64_atomics
, cap
);
3560 case SpvCapabilityStorageImageMultisample
:
3561 spv_check_supported(storage_image_ms
, cap
);
3564 case SpvCapabilityAddresses
:
3565 case SpvCapabilityKernel
:
3566 case SpvCapabilityImageBasic
:
3567 case SpvCapabilityImageReadWrite
:
3568 case SpvCapabilityImageMipmap
:
3569 case SpvCapabilityPipes
:
3570 case SpvCapabilityGroups
:
3571 case SpvCapabilityDeviceEnqueue
:
3572 case SpvCapabilityLiteralSampler
:
3573 case SpvCapabilityGenericPointer
:
3574 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
3575 spirv_capability_to_string(cap
));
3578 case SpvCapabilityImageMSArray
:
3579 spv_check_supported(image_ms_array
, cap
);
3582 case SpvCapabilityTessellation
:
3583 case SpvCapabilityTessellationPointSize
:
3584 spv_check_supported(tessellation
, cap
);
3587 case SpvCapabilityDrawParameters
:
3588 spv_check_supported(draw_parameters
, cap
);
3591 case SpvCapabilityStorageImageReadWithoutFormat
:
3592 spv_check_supported(image_read_without_format
, cap
);
3595 case SpvCapabilityStorageImageWriteWithoutFormat
:
3596 spv_check_supported(image_write_without_format
, cap
);
3599 case SpvCapabilityDeviceGroup
:
3600 spv_check_supported(device_group
, cap
);
3603 case SpvCapabilityMultiView
:
3604 spv_check_supported(multiview
, cap
);
3607 case SpvCapabilityGroupNonUniform
:
3608 spv_check_supported(subgroup_basic
, cap
);
3611 case SpvCapabilityGroupNonUniformVote
:
3612 spv_check_supported(subgroup_vote
, cap
);
3615 case SpvCapabilitySubgroupBallotKHR
:
3616 case SpvCapabilityGroupNonUniformBallot
:
3617 spv_check_supported(subgroup_ballot
, cap
);
3620 case SpvCapabilityGroupNonUniformShuffle
:
3621 case SpvCapabilityGroupNonUniformShuffleRelative
:
3622 spv_check_supported(subgroup_shuffle
, cap
);
3625 case SpvCapabilityGroupNonUniformQuad
:
3626 spv_check_supported(subgroup_quad
, cap
);
3629 case SpvCapabilityGroupNonUniformArithmetic
:
3630 case SpvCapabilityGroupNonUniformClustered
:
3631 spv_check_supported(subgroup_arithmetic
, cap
);
3634 case SpvCapabilityVariablePointersStorageBuffer
:
3635 case SpvCapabilityVariablePointers
:
3636 spv_check_supported(variable_pointers
, cap
);
3637 b
->variable_pointers
= true;
3640 case SpvCapabilityStorageUniformBufferBlock16
:
3641 case SpvCapabilityStorageUniform16
:
3642 case SpvCapabilityStoragePushConstant16
:
3643 case SpvCapabilityStorageInputOutput16
:
3644 spv_check_supported(storage_16bit
, cap
);
3647 case SpvCapabilityShaderViewportIndexLayerEXT
:
3648 spv_check_supported(shader_viewport_index_layer
, cap
);
3651 case SpvCapabilityStorageBuffer8BitAccess
:
3652 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
3653 case SpvCapabilityStoragePushConstant8
:
3654 spv_check_supported(storage_8bit
, cap
);
3657 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
3658 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
3659 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
3660 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
3663 case SpvCapabilityRuntimeDescriptorArrayEXT
:
3664 spv_check_supported(runtime_descriptor_array
, cap
);
3667 case SpvCapabilityStencilExportEXT
:
3668 spv_check_supported(stencil_export
, cap
);
3671 case SpvCapabilitySampleMaskPostDepthCoverage
:
3672 spv_check_supported(post_depth_coverage
, cap
);
3676 vtn_fail("Unhandled capability");
3681 case SpvOpExtInstImport
:
3682 vtn_handle_extension(b
, opcode
, w
, count
);
3685 case SpvOpMemoryModel
:
3686 vtn_assert(w
[1] == SpvAddressingModelLogical
);
3687 vtn_assert(w
[2] == SpvMemoryModelSimple
||
3688 w
[2] == SpvMemoryModelGLSL450
);
3691 case SpvOpEntryPoint
:
3692 vtn_handle_entry_point(b
, w
, count
);
3696 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
3697 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3701 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3704 case SpvOpMemberName
:
3708 case SpvOpExecutionMode
:
3709 case SpvOpDecorationGroup
:
3711 case SpvOpMemberDecorate
:
3712 case SpvOpGroupDecorate
:
3713 case SpvOpGroupMemberDecorate
:
3714 case SpvOpDecorateStringGOOGLE
:
3715 case SpvOpMemberDecorateStringGOOGLE
:
3716 vtn_handle_decoration(b
, opcode
, w
, count
);
3720 return false; /* End of preamble */
3727 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
3728 const struct vtn_decoration
*mode
, void *data
)
3730 vtn_assert(b
->entry_point
== entry_point
);
3732 switch(mode
->exec_mode
) {
3733 case SpvExecutionModeOriginUpperLeft
:
3734 case SpvExecutionModeOriginLowerLeft
:
3735 b
->origin_upper_left
=
3736 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
3739 case SpvExecutionModeEarlyFragmentTests
:
3740 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3741 b
->shader
->info
.fs
.early_fragment_tests
= true;
3744 case SpvExecutionModePostDepthCoverage
:
3745 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3746 b
->shader
->info
.fs
.post_depth_coverage
= true;
3749 case SpvExecutionModeInvocations
:
3750 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3751 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->literals
[0]);
3754 case SpvExecutionModeDepthReplacing
:
3755 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3756 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
3758 case SpvExecutionModeDepthGreater
:
3759 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3760 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
3762 case SpvExecutionModeDepthLess
:
3763 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3764 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
3766 case SpvExecutionModeDepthUnchanged
:
3767 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3768 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
3771 case SpvExecutionModeLocalSize
:
3772 vtn_assert(gl_shader_stage_is_compute(b
->shader
->info
.stage
));
3773 b
->shader
->info
.cs
.local_size
[0] = mode
->literals
[0];
3774 b
->shader
->info
.cs
.local_size
[1] = mode
->literals
[1];
3775 b
->shader
->info
.cs
.local_size
[2] = mode
->literals
[2];
3777 case SpvExecutionModeLocalSizeHint
:
3778 break; /* Nothing to do with this */
3780 case SpvExecutionModeOutputVertices
:
3781 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3782 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3783 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->literals
[0];
3785 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3786 b
->shader
->info
.gs
.vertices_out
= mode
->literals
[0];
3790 case SpvExecutionModeInputPoints
:
3791 case SpvExecutionModeInputLines
:
3792 case SpvExecutionModeInputLinesAdjacency
:
3793 case SpvExecutionModeTriangles
:
3794 case SpvExecutionModeInputTrianglesAdjacency
:
3795 case SpvExecutionModeQuads
:
3796 case SpvExecutionModeIsolines
:
3797 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3798 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3799 b
->shader
->info
.tess
.primitive_mode
=
3800 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3802 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3803 b
->shader
->info
.gs
.vertices_in
=
3804 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
3805 b
->shader
->info
.gs
.input_primitive
=
3806 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3810 case SpvExecutionModeOutputPoints
:
3811 case SpvExecutionModeOutputLineStrip
:
3812 case SpvExecutionModeOutputTriangleStrip
:
3813 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3814 b
->shader
->info
.gs
.output_primitive
=
3815 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3818 case SpvExecutionModeSpacingEqual
:
3819 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3820 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3821 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
3823 case SpvExecutionModeSpacingFractionalEven
:
3824 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3825 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3826 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
3828 case SpvExecutionModeSpacingFractionalOdd
:
3829 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3830 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3831 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
3833 case SpvExecutionModeVertexOrderCw
:
3834 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3835 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3836 b
->shader
->info
.tess
.ccw
= false;
3838 case SpvExecutionModeVertexOrderCcw
:
3839 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3840 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3841 b
->shader
->info
.tess
.ccw
= true;
3843 case SpvExecutionModePointMode
:
3844 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3845 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3846 b
->shader
->info
.tess
.point_mode
= true;
3849 case SpvExecutionModePixelCenterInteger
:
3850 b
->pixel_center_integer
= true;
3853 case SpvExecutionModeXfb
:
3854 b
->shader
->info
.has_transform_feedback_varyings
= true;
3857 case SpvExecutionModeVecTypeHint
:
3858 case SpvExecutionModeContractionOff
:
3861 case SpvExecutionModeStencilRefReplacingEXT
:
3862 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3866 vtn_fail("Unhandled execution mode");
3871 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3872 const uint32_t *w
, unsigned count
)
3874 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
3878 case SpvOpSourceContinued
:
3879 case SpvOpSourceExtension
:
3880 case SpvOpExtension
:
3881 case SpvOpCapability
:
3882 case SpvOpExtInstImport
:
3883 case SpvOpMemoryModel
:
3884 case SpvOpEntryPoint
:
3885 case SpvOpExecutionMode
:
3888 case SpvOpMemberName
:
3889 case SpvOpDecorationGroup
:
3891 case SpvOpMemberDecorate
:
3892 case SpvOpGroupDecorate
:
3893 case SpvOpGroupMemberDecorate
:
3894 case SpvOpDecorateStringGOOGLE
:
3895 case SpvOpMemberDecorateStringGOOGLE
:
3896 vtn_fail("Invalid opcode types and variables section");
3902 case SpvOpTypeFloat
:
3903 case SpvOpTypeVector
:
3904 case SpvOpTypeMatrix
:
3905 case SpvOpTypeImage
:
3906 case SpvOpTypeSampler
:
3907 case SpvOpTypeSampledImage
:
3908 case SpvOpTypeArray
:
3909 case SpvOpTypeRuntimeArray
:
3910 case SpvOpTypeStruct
:
3911 case SpvOpTypeOpaque
:
3912 case SpvOpTypePointer
:
3913 case SpvOpTypeFunction
:
3914 case SpvOpTypeEvent
:
3915 case SpvOpTypeDeviceEvent
:
3916 case SpvOpTypeReserveId
:
3917 case SpvOpTypeQueue
:
3919 vtn_handle_type(b
, opcode
, w
, count
);
3922 case SpvOpConstantTrue
:
3923 case SpvOpConstantFalse
:
3925 case SpvOpConstantComposite
:
3926 case SpvOpConstantSampler
:
3927 case SpvOpConstantNull
:
3928 case SpvOpSpecConstantTrue
:
3929 case SpvOpSpecConstantFalse
:
3930 case SpvOpSpecConstant
:
3931 case SpvOpSpecConstantComposite
:
3932 case SpvOpSpecConstantOp
:
3933 vtn_handle_constant(b
, opcode
, w
, count
);
3938 vtn_handle_variables(b
, opcode
, w
, count
);
3942 return false; /* End of preamble */
3949 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3950 const uint32_t *w
, unsigned count
)
3956 case SpvOpLoopMerge
:
3957 case SpvOpSelectionMerge
:
3958 /* This is handled by cfg pre-pass and walk_blocks */
3962 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
3963 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3968 vtn_handle_extension(b
, opcode
, w
, count
);
3974 case SpvOpCopyMemory
:
3975 case SpvOpCopyMemorySized
:
3976 case SpvOpAccessChain
:
3977 case SpvOpPtrAccessChain
:
3978 case SpvOpInBoundsAccessChain
:
3979 case SpvOpArrayLength
:
3980 vtn_handle_variables(b
, opcode
, w
, count
);
3983 case SpvOpFunctionCall
:
3984 vtn_handle_function_call(b
, opcode
, w
, count
);
3987 case SpvOpSampledImage
:
3989 case SpvOpImageSampleImplicitLod
:
3990 case SpvOpImageSampleExplicitLod
:
3991 case SpvOpImageSampleDrefImplicitLod
:
3992 case SpvOpImageSampleDrefExplicitLod
:
3993 case SpvOpImageSampleProjImplicitLod
:
3994 case SpvOpImageSampleProjExplicitLod
:
3995 case SpvOpImageSampleProjDrefImplicitLod
:
3996 case SpvOpImageSampleProjDrefExplicitLod
:
3997 case SpvOpImageFetch
:
3998 case SpvOpImageGather
:
3999 case SpvOpImageDrefGather
:
4000 case SpvOpImageQuerySizeLod
:
4001 case SpvOpImageQueryLod
:
4002 case SpvOpImageQueryLevels
:
4003 case SpvOpImageQuerySamples
:
4004 vtn_handle_texture(b
, opcode
, w
, count
);
4007 case SpvOpImageRead
:
4008 case SpvOpImageWrite
:
4009 case SpvOpImageTexelPointer
:
4010 vtn_handle_image(b
, opcode
, w
, count
);
4013 case SpvOpImageQuerySize
: {
4014 struct vtn_pointer
*image
=
4015 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
4016 if (glsl_type_is_image(image
->type
->type
)) {
4017 vtn_handle_image(b
, opcode
, w
, count
);
4019 vtn_assert(glsl_type_is_sampler(image
->type
->type
));
4020 vtn_handle_texture(b
, opcode
, w
, count
);
4025 case SpvOpAtomicLoad
:
4026 case SpvOpAtomicExchange
:
4027 case SpvOpAtomicCompareExchange
:
4028 case SpvOpAtomicCompareExchangeWeak
:
4029 case SpvOpAtomicIIncrement
:
4030 case SpvOpAtomicIDecrement
:
4031 case SpvOpAtomicIAdd
:
4032 case SpvOpAtomicISub
:
4033 case SpvOpAtomicSMin
:
4034 case SpvOpAtomicUMin
:
4035 case SpvOpAtomicSMax
:
4036 case SpvOpAtomicUMax
:
4037 case SpvOpAtomicAnd
:
4039 case SpvOpAtomicXor
: {
4040 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
4041 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4042 vtn_handle_image(b
, opcode
, w
, count
);
4044 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4045 vtn_handle_atomics(b
, opcode
, w
, count
);
4050 case SpvOpAtomicStore
: {
4051 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
4052 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4053 vtn_handle_image(b
, opcode
, w
, count
);
4055 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4056 vtn_handle_atomics(b
, opcode
, w
, count
);
4062 /* Handle OpSelect up-front here because it needs to be able to handle
4063 * pointers and not just regular vectors and scalars.
4065 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
4066 struct vtn_value
*sel_val
= vtn_untyped_value(b
, w
[3]);
4067 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
4068 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
4070 const struct glsl_type
*sel_type
;
4071 switch (res_val
->type
->base_type
) {
4072 case vtn_base_type_scalar
:
4073 sel_type
= glsl_bool_type();
4075 case vtn_base_type_vector
:
4076 sel_type
= glsl_vector_type(GLSL_TYPE_BOOL
, res_val
->type
->length
);
4078 case vtn_base_type_pointer
:
4079 /* We need to have actual storage for pointer types */
4080 vtn_fail_if(res_val
->type
->type
== NULL
,
4081 "Invalid pointer result type for OpSelect");
4082 sel_type
= glsl_bool_type();
4085 vtn_fail("Result type of OpSelect must be a scalar, vector, or pointer");
4088 if (unlikely(sel_val
->type
->type
!= sel_type
)) {
4089 if (sel_val
->type
->type
== glsl_bool_type()) {
4090 /* This case is illegal but some older versions of GLSLang produce
4091 * it. The GLSLang issue was fixed on March 30, 2017:
4093 * https://github.com/KhronosGroup/glslang/issues/809
4095 * Unfortunately, there are applications in the wild which are
4096 * shipping with this bug so it isn't nice to fail on them so we
4097 * throw a warning instead. It's not actually a problem for us as
4098 * nir_builder will just splat the condition out which is most
4099 * likely what the client wanted anyway.
4101 vtn_warn("Condition type of OpSelect must have the same number "
4102 "of components as Result Type");
4104 vtn_fail("Condition type of OpSelect must be a scalar or vector "
4105 "of Boolean type. It must have the same number of "
4106 "components as Result Type");
4110 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4111 obj2_val
->type
!= res_val
->type
,
4112 "Object types must match the result type in OpSelect");
4114 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4115 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, res_type
->type
);
4116 ssa
->def
= nir_bcsel(&b
->nb
, vtn_ssa_value(b
, w
[3])->def
,
4117 vtn_ssa_value(b
, w
[4])->def
,
4118 vtn_ssa_value(b
, w
[5])->def
);
4119 vtn_push_ssa(b
, w
[2], res_type
, ssa
);
4128 case SpvOpConvertFToU
:
4129 case SpvOpConvertFToS
:
4130 case SpvOpConvertSToF
:
4131 case SpvOpConvertUToF
:
4135 case SpvOpQuantizeToF16
:
4136 case SpvOpConvertPtrToU
:
4137 case SpvOpConvertUToPtr
:
4138 case SpvOpPtrCastToGeneric
:
4139 case SpvOpGenericCastToPtr
:
4145 case SpvOpSignBitSet
:
4146 case SpvOpLessOrGreater
:
4148 case SpvOpUnordered
:
4163 case SpvOpVectorTimesScalar
:
4165 case SpvOpIAddCarry
:
4166 case SpvOpISubBorrow
:
4167 case SpvOpUMulExtended
:
4168 case SpvOpSMulExtended
:
4169 case SpvOpShiftRightLogical
:
4170 case SpvOpShiftRightArithmetic
:
4171 case SpvOpShiftLeftLogical
:
4172 case SpvOpLogicalEqual
:
4173 case SpvOpLogicalNotEqual
:
4174 case SpvOpLogicalOr
:
4175 case SpvOpLogicalAnd
:
4176 case SpvOpLogicalNot
:
4177 case SpvOpBitwiseOr
:
4178 case SpvOpBitwiseXor
:
4179 case SpvOpBitwiseAnd
:
4181 case SpvOpFOrdEqual
:
4182 case SpvOpFUnordEqual
:
4183 case SpvOpINotEqual
:
4184 case SpvOpFOrdNotEqual
:
4185 case SpvOpFUnordNotEqual
:
4186 case SpvOpULessThan
:
4187 case SpvOpSLessThan
:
4188 case SpvOpFOrdLessThan
:
4189 case SpvOpFUnordLessThan
:
4190 case SpvOpUGreaterThan
:
4191 case SpvOpSGreaterThan
:
4192 case SpvOpFOrdGreaterThan
:
4193 case SpvOpFUnordGreaterThan
:
4194 case SpvOpULessThanEqual
:
4195 case SpvOpSLessThanEqual
:
4196 case SpvOpFOrdLessThanEqual
:
4197 case SpvOpFUnordLessThanEqual
:
4198 case SpvOpUGreaterThanEqual
:
4199 case SpvOpSGreaterThanEqual
:
4200 case SpvOpFOrdGreaterThanEqual
:
4201 case SpvOpFUnordGreaterThanEqual
:
4207 case SpvOpFwidthFine
:
4208 case SpvOpDPdxCoarse
:
4209 case SpvOpDPdyCoarse
:
4210 case SpvOpFwidthCoarse
:
4211 case SpvOpBitFieldInsert
:
4212 case SpvOpBitFieldSExtract
:
4213 case SpvOpBitFieldUExtract
:
4214 case SpvOpBitReverse
:
4216 case SpvOpTranspose
:
4217 case SpvOpOuterProduct
:
4218 case SpvOpMatrixTimesScalar
:
4219 case SpvOpVectorTimesMatrix
:
4220 case SpvOpMatrixTimesVector
:
4221 case SpvOpMatrixTimesMatrix
:
4222 vtn_handle_alu(b
, opcode
, w
, count
);
4225 case SpvOpVectorExtractDynamic
:
4226 case SpvOpVectorInsertDynamic
:
4227 case SpvOpVectorShuffle
:
4228 case SpvOpCompositeConstruct
:
4229 case SpvOpCompositeExtract
:
4230 case SpvOpCompositeInsert
:
4231 case SpvOpCopyObject
:
4232 vtn_handle_composite(b
, opcode
, w
, count
);
4235 case SpvOpEmitVertex
:
4236 case SpvOpEndPrimitive
:
4237 case SpvOpEmitStreamVertex
:
4238 case SpvOpEndStreamPrimitive
:
4239 case SpvOpControlBarrier
:
4240 case SpvOpMemoryBarrier
:
4241 vtn_handle_barrier(b
, opcode
, w
, count
);
4244 case SpvOpGroupNonUniformElect
:
4245 case SpvOpGroupNonUniformAll
:
4246 case SpvOpGroupNonUniformAny
:
4247 case SpvOpGroupNonUniformAllEqual
:
4248 case SpvOpGroupNonUniformBroadcast
:
4249 case SpvOpGroupNonUniformBroadcastFirst
:
4250 case SpvOpGroupNonUniformBallot
:
4251 case SpvOpGroupNonUniformInverseBallot
:
4252 case SpvOpGroupNonUniformBallotBitExtract
:
4253 case SpvOpGroupNonUniformBallotBitCount
:
4254 case SpvOpGroupNonUniformBallotFindLSB
:
4255 case SpvOpGroupNonUniformBallotFindMSB
:
4256 case SpvOpGroupNonUniformShuffle
:
4257 case SpvOpGroupNonUniformShuffleXor
:
4258 case SpvOpGroupNonUniformShuffleUp
:
4259 case SpvOpGroupNonUniformShuffleDown
:
4260 case SpvOpGroupNonUniformIAdd
:
4261 case SpvOpGroupNonUniformFAdd
:
4262 case SpvOpGroupNonUniformIMul
:
4263 case SpvOpGroupNonUniformFMul
:
4264 case SpvOpGroupNonUniformSMin
:
4265 case SpvOpGroupNonUniformUMin
:
4266 case SpvOpGroupNonUniformFMin
:
4267 case SpvOpGroupNonUniformSMax
:
4268 case SpvOpGroupNonUniformUMax
:
4269 case SpvOpGroupNonUniformFMax
:
4270 case SpvOpGroupNonUniformBitwiseAnd
:
4271 case SpvOpGroupNonUniformBitwiseOr
:
4272 case SpvOpGroupNonUniformBitwiseXor
:
4273 case SpvOpGroupNonUniformLogicalAnd
:
4274 case SpvOpGroupNonUniformLogicalOr
:
4275 case SpvOpGroupNonUniformLogicalXor
:
4276 case SpvOpGroupNonUniformQuadBroadcast
:
4277 case SpvOpGroupNonUniformQuadSwap
:
4278 vtn_handle_subgroup(b
, opcode
, w
, count
);
4282 vtn_fail("Unhandled opcode");
4289 vtn_create_builder(const uint32_t *words
, size_t word_count
,
4290 gl_shader_stage stage
, const char *entry_point_name
,
4291 const struct spirv_to_nir_options
*options
)
4293 /* Initialize the vtn_builder object */
4294 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
4296 b
->spirv_word_count
= word_count
;
4300 exec_list_make_empty(&b
->functions
);
4301 b
->entry_point_stage
= stage
;
4302 b
->entry_point_name
= entry_point_name
;
4303 b
->options
= options
;
4306 * Handle the SPIR-V header (first 5 dwords).
4307 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
4309 if (word_count
<= 5)
4312 if (words
[0] != SpvMagicNumber
) {
4313 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
4316 if (words
[1] < 0x10000) {
4317 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
4321 uint16_t generator_id
= words
[2] >> 16;
4322 uint16_t generator_version
= words
[2];
4324 /* The first GLSLang version bump actually 1.5 years after #179 was fixed
4325 * but this should at least let us shut the workaround off for modern
4326 * versions of GLSLang.
4328 b
->wa_glslang_179
= (generator_id
== 8 && generator_version
== 1);
4330 /* words[2] == generator magic */
4331 unsigned value_id_bound
= words
[3];
4332 if (words
[4] != 0) {
4333 vtn_err("words[4] was %u, want 0", words
[4]);
4337 b
->value_id_bound
= value_id_bound
;
4338 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
4347 spirv_to_nir(const uint32_t *words
, size_t word_count
,
4348 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
4349 gl_shader_stage stage
, const char *entry_point_name
,
4350 const struct spirv_to_nir_options
*options
,
4351 const nir_shader_compiler_options
*nir_options
)
4354 const uint32_t *word_end
= words
+ word_count
;
4356 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
4357 stage
, entry_point_name
,
4363 /* See also _vtn_fail() */
4364 if (setjmp(b
->fail_jump
)) {
4369 /* Skip the SPIR-V header, handled at vtn_create_builder */
4372 /* Handle all the preamble instructions */
4373 words
= vtn_foreach_instruction(b
, words
, word_end
,
4374 vtn_handle_preamble_instruction
);
4376 if (b
->entry_point
== NULL
) {
4377 vtn_fail("Entry point not found");
4382 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
4384 /* Set shader info defaults */
4385 b
->shader
->info
.gs
.invocations
= 1;
4387 /* Parse execution modes */
4388 vtn_foreach_execution_mode(b
, b
->entry_point
,
4389 vtn_handle_execution_mode
, NULL
);
4391 b
->specializations
= spec
;
4392 b
->num_specializations
= num_spec
;
4394 /* Handle all variable, type, and constant instructions */
4395 words
= vtn_foreach_instruction(b
, words
, word_end
,
4396 vtn_handle_variable_or_type_instruction
);
4398 /* Set types on all vtn_values */
4399 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
4401 vtn_build_cfg(b
, words
, word_end
);
4403 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4404 b
->entry_point
->func
->referenced
= true;
4409 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
4410 if (func
->referenced
&& !func
->emitted
) {
4411 b
->const_table
= _mesa_pointer_hash_table_create(b
);
4413 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
4419 /* We sometimes generate bogus derefs that, while never used, give the
4420 * validator a bit of heartburn. Run dead code to get rid of them.
4422 nir_opt_dce(b
->shader
);
4424 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4425 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
4426 vtn_assert(entry_point
);
4428 /* Unparent the shader from the vtn_builder before we delete the builder */
4429 ralloc_steal(NULL
, b
->shader
);
4433 entry_point
->is_entrypoint
= true;