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
;
398 } else if (strcmp(ext
, "OpenCL.std") == 0) {
399 val
->ext_handler
= vtn_handle_opencl_instruction
;
401 vtn_fail("Unsupported extension: %s", ext
);
407 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
408 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
414 vtn_fail("Unhandled opcode");
419 _foreach_decoration_helper(struct vtn_builder
*b
,
420 struct vtn_value
*base_value
,
422 struct vtn_value
*value
,
423 vtn_decoration_foreach_cb cb
, void *data
)
425 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
427 if (dec
->scope
== VTN_DEC_DECORATION
) {
428 member
= parent_member
;
429 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
430 vtn_fail_if(value
->value_type
!= vtn_value_type_type
||
431 value
->type
->base_type
!= vtn_base_type_struct
,
432 "OpMemberDecorate and OpGroupMemberDecorate are only "
433 "allowed on OpTypeStruct");
434 /* This means we haven't recursed yet */
435 assert(value
== base_value
);
437 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
439 vtn_fail_if(member
>= base_value
->type
->length
,
440 "OpMemberDecorate specifies member %d but the "
441 "OpTypeStruct has only %u members",
442 member
, base_value
->type
->length
);
444 /* Not a decoration */
445 assert(dec
->scope
== VTN_DEC_EXECUTION_MODE
);
450 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
451 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
454 cb(b
, base_value
, member
, dec
, data
);
459 /** Iterates (recursively if needed) over all of the decorations on a value
461 * This function iterates over all of the decorations applied to a given
462 * value. If it encounters a decoration group, it recurses into the group
463 * and iterates over all of those decorations as well.
466 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
467 vtn_decoration_foreach_cb cb
, void *data
)
469 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
473 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
474 vtn_execution_mode_foreach_cb cb
, void *data
)
476 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
477 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
480 assert(dec
->group
== NULL
);
481 cb(b
, value
, dec
, data
);
486 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
487 const uint32_t *w
, unsigned count
)
489 const uint32_t *w_end
= w
+ count
;
490 const uint32_t target
= w
[1];
494 case SpvOpDecorationGroup
:
495 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
499 case SpvOpMemberDecorate
:
500 case SpvOpDecorateStringGOOGLE
:
501 case SpvOpMemberDecorateStringGOOGLE
:
502 case SpvOpExecutionMode
:
503 case SpvOpExecutionModeId
: {
504 struct vtn_value
*val
= vtn_untyped_value(b
, target
);
506 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
509 case SpvOpDecorateStringGOOGLE
:
510 dec
->scope
= VTN_DEC_DECORATION
;
512 case SpvOpMemberDecorate
:
513 case SpvOpMemberDecorateStringGOOGLE
:
514 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
515 vtn_fail_if(dec
->scope
< VTN_DEC_STRUCT_MEMBER0
, /* overflow */
516 "Member argument of OpMemberDecorate too large");
518 case SpvOpExecutionMode
:
519 case SpvOpExecutionModeId
:
520 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
523 unreachable("Invalid decoration opcode");
525 dec
->decoration
= *(w
++);
528 /* Link into the list */
529 dec
->next
= val
->decoration
;
530 val
->decoration
= dec
;
534 case SpvOpGroupMemberDecorate
:
535 case SpvOpGroupDecorate
: {
536 struct vtn_value
*group
=
537 vtn_value(b
, target
, vtn_value_type_decoration_group
);
539 for (; w
< w_end
; w
++) {
540 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
541 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
544 if (opcode
== SpvOpGroupDecorate
) {
545 dec
->scope
= VTN_DEC_DECORATION
;
547 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
548 vtn_fail_if(dec
->scope
< 0, /* Check for overflow */
549 "Member argument of OpGroupMemberDecorate too large");
552 /* Link into the list */
553 dec
->next
= val
->decoration
;
554 val
->decoration
= dec
;
560 unreachable("Unhandled opcode");
564 struct member_decoration_ctx
{
566 struct glsl_struct_field
*fields
;
567 struct vtn_type
*type
;
571 * Returns true if the given type contains a struct decorated Block or
575 vtn_type_contains_block(struct vtn_builder
*b
, struct vtn_type
*type
)
577 switch (type
->base_type
) {
578 case vtn_base_type_array
:
579 return vtn_type_contains_block(b
, type
->array_element
);
580 case vtn_base_type_struct
:
581 if (type
->block
|| type
->buffer_block
)
583 for (unsigned i
= 0; i
< type
->length
; i
++) {
584 if (vtn_type_contains_block(b
, type
->members
[i
]))
593 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
594 * OpStore, or OpCopyMemory between them without breaking anything.
595 * Technically, the SPIR-V rules require the exact same type ID but this lets
596 * us internally be a bit looser.
599 vtn_types_compatible(struct vtn_builder
*b
,
600 struct vtn_type
*t1
, struct vtn_type
*t2
)
602 if (t1
->id
== t2
->id
)
605 if (t1
->base_type
!= t2
->base_type
)
608 switch (t1
->base_type
) {
609 case vtn_base_type_void
:
610 case vtn_base_type_scalar
:
611 case vtn_base_type_vector
:
612 case vtn_base_type_matrix
:
613 case vtn_base_type_image
:
614 case vtn_base_type_sampler
:
615 case vtn_base_type_sampled_image
:
616 return t1
->type
== t2
->type
;
618 case vtn_base_type_array
:
619 return t1
->length
== t2
->length
&&
620 vtn_types_compatible(b
, t1
->array_element
, t2
->array_element
);
622 case vtn_base_type_pointer
:
623 return vtn_types_compatible(b
, t1
->deref
, t2
->deref
);
625 case vtn_base_type_struct
:
626 if (t1
->length
!= t2
->length
)
629 for (unsigned i
= 0; i
< t1
->length
; i
++) {
630 if (!vtn_types_compatible(b
, t1
->members
[i
], t2
->members
[i
]))
635 case vtn_base_type_function
:
636 /* This case shouldn't get hit since you can't copy around function
637 * types. Just require them to be identical.
642 vtn_fail("Invalid base type");
645 /* does a shallow copy of a vtn_type */
647 static struct vtn_type
*
648 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
650 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
653 switch (src
->base_type
) {
654 case vtn_base_type_void
:
655 case vtn_base_type_scalar
:
656 case vtn_base_type_vector
:
657 case vtn_base_type_matrix
:
658 case vtn_base_type_array
:
659 case vtn_base_type_pointer
:
660 case vtn_base_type_image
:
661 case vtn_base_type_sampler
:
662 case vtn_base_type_sampled_image
:
663 /* Nothing more to do */
666 case vtn_base_type_struct
:
667 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
668 memcpy(dest
->members
, src
->members
,
669 src
->length
* sizeof(src
->members
[0]));
671 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
672 memcpy(dest
->offsets
, src
->offsets
,
673 src
->length
* sizeof(src
->offsets
[0]));
676 case vtn_base_type_function
:
677 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
678 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
685 static struct vtn_type
*
686 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
688 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
689 type
= type
->members
[member
];
691 /* We may have an array of matrices.... Oh, joy! */
692 while (glsl_type_is_array(type
->type
)) {
693 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
694 type
= type
->array_element
;
697 vtn_assert(glsl_type_is_matrix(type
->type
));
703 vtn_handle_access_qualifier(struct vtn_builder
*b
, struct vtn_type
*type
,
704 int member
, enum gl_access_qualifier access
)
706 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
707 type
= type
->members
[member
];
709 type
->access
|= access
;
713 array_stride_decoration_cb(struct vtn_builder
*b
,
714 struct vtn_value
*val
, int member
,
715 const struct vtn_decoration
*dec
, void *void_ctx
)
717 struct vtn_type
*type
= val
->type
;
719 if (dec
->decoration
== SpvDecorationArrayStride
) {
720 vtn_fail_if(dec
->literals
[0] == 0, "ArrayStride must be non-zero");
721 type
->stride
= dec
->literals
[0];
726 struct_member_decoration_cb(struct vtn_builder
*b
,
727 struct vtn_value
*val
, int member
,
728 const struct vtn_decoration
*dec
, void *void_ctx
)
730 struct member_decoration_ctx
*ctx
= void_ctx
;
735 assert(member
< ctx
->num_fields
);
737 switch (dec
->decoration
) {
738 case SpvDecorationRelaxedPrecision
:
739 case SpvDecorationUniform
:
740 break; /* FIXME: Do nothing with this for now. */
741 case SpvDecorationNonWritable
:
742 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_WRITEABLE
);
744 case SpvDecorationNonReadable
:
745 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_READABLE
);
747 case SpvDecorationVolatile
:
748 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_VOLATILE
);
750 case SpvDecorationCoherent
:
751 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_COHERENT
);
753 case SpvDecorationNoPerspective
:
754 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
756 case SpvDecorationFlat
:
757 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
759 case SpvDecorationCentroid
:
760 ctx
->fields
[member
].centroid
= true;
762 case SpvDecorationSample
:
763 ctx
->fields
[member
].sample
= true;
765 case SpvDecorationStream
:
766 /* Vulkan only allows one GS stream */
767 vtn_assert(dec
->literals
[0] == 0);
769 case SpvDecorationLocation
:
770 ctx
->fields
[member
].location
= dec
->literals
[0];
772 case SpvDecorationComponent
:
773 break; /* FIXME: What should we do with these? */
774 case SpvDecorationBuiltIn
:
775 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
776 ctx
->type
->members
[member
]->is_builtin
= true;
777 ctx
->type
->members
[member
]->builtin
= dec
->literals
[0];
778 ctx
->type
->builtin_block
= true;
780 case SpvDecorationOffset
:
781 ctx
->type
->offsets
[member
] = dec
->literals
[0];
782 ctx
->fields
[member
].offset
= dec
->literals
[0];
784 case SpvDecorationMatrixStride
:
785 /* Handled as a second pass */
787 case SpvDecorationColMajor
:
788 break; /* Nothing to do here. Column-major is the default. */
789 case SpvDecorationRowMajor
:
790 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
793 case SpvDecorationPatch
:
796 case SpvDecorationSpecId
:
797 case SpvDecorationBlock
:
798 case SpvDecorationBufferBlock
:
799 case SpvDecorationArrayStride
:
800 case SpvDecorationGLSLShared
:
801 case SpvDecorationGLSLPacked
:
802 case SpvDecorationInvariant
:
803 case SpvDecorationRestrict
:
804 case SpvDecorationAliased
:
805 case SpvDecorationConstant
:
806 case SpvDecorationIndex
:
807 case SpvDecorationBinding
:
808 case SpvDecorationDescriptorSet
:
809 case SpvDecorationLinkageAttributes
:
810 case SpvDecorationNoContraction
:
811 case SpvDecorationInputAttachmentIndex
:
812 vtn_warn("Decoration not allowed on struct members: %s",
813 spirv_decoration_to_string(dec
->decoration
));
816 case SpvDecorationXfbBuffer
:
817 case SpvDecorationXfbStride
:
818 vtn_warn("Vulkan does not have transform feedback");
821 case SpvDecorationCPacked
:
822 case SpvDecorationSaturatedConversion
:
823 case SpvDecorationFuncParamAttr
:
824 case SpvDecorationFPRoundingMode
:
825 case SpvDecorationFPFastMathMode
:
826 case SpvDecorationAlignment
:
827 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
828 vtn_warn("Decoration only allowed for CL-style kernels: %s",
829 spirv_decoration_to_string(dec
->decoration
));
833 case SpvDecorationHlslSemanticGOOGLE
:
834 /* HLSL semantic decorations can safely be ignored by the driver. */
838 vtn_fail("Unhandled decoration");
842 /** Chases the array type all the way down to the tail and rewrites the
843 * glsl_types to be based off the tail's glsl_type.
846 vtn_array_type_rewrite_glsl_type(struct vtn_type
*type
)
848 if (type
->base_type
!= vtn_base_type_array
)
851 vtn_array_type_rewrite_glsl_type(type
->array_element
);
853 type
->type
= glsl_array_type(type
->array_element
->type
,
854 type
->length
, type
->stride
);
857 /* Matrix strides are handled as a separate pass because we need to know
858 * whether the matrix is row-major or not first.
861 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
862 struct vtn_value
*val
, int member
,
863 const struct vtn_decoration
*dec
,
866 if (dec
->decoration
!= SpvDecorationMatrixStride
)
869 vtn_fail_if(member
< 0,
870 "The MatrixStride decoration is only allowed on members "
872 vtn_fail_if(dec
->literals
[0] == 0, "MatrixStride must be non-zero");
874 struct member_decoration_ctx
*ctx
= void_ctx
;
876 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
877 if (mat_type
->row_major
) {
878 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
879 mat_type
->stride
= mat_type
->array_element
->stride
;
880 mat_type
->array_element
->stride
= dec
->literals
[0];
882 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
883 dec
->literals
[0], true);
884 mat_type
->array_element
->type
= glsl_get_column_type(mat_type
->type
);
886 vtn_assert(mat_type
->array_element
->stride
> 0);
887 mat_type
->stride
= dec
->literals
[0];
889 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
890 dec
->literals
[0], false);
893 /* Now that we've replaced the glsl_type with a properly strided matrix
894 * type, rewrite the member type so that it's an array of the proper kind
897 vtn_array_type_rewrite_glsl_type(ctx
->type
->members
[member
]);
898 ctx
->fields
[member
].type
= ctx
->type
->members
[member
]->type
;
902 type_decoration_cb(struct vtn_builder
*b
,
903 struct vtn_value
*val
, int member
,
904 const struct vtn_decoration
*dec
, void *ctx
)
906 struct vtn_type
*type
= val
->type
;
909 /* This should have been handled by OpTypeStruct */
910 assert(val
->type
->base_type
== vtn_base_type_struct
);
911 assert(member
>= 0 && member
< val
->type
->length
);
915 switch (dec
->decoration
) {
916 case SpvDecorationArrayStride
:
917 vtn_assert(type
->base_type
== vtn_base_type_array
||
918 type
->base_type
== vtn_base_type_pointer
);
920 case SpvDecorationBlock
:
921 vtn_assert(type
->base_type
== vtn_base_type_struct
);
924 case SpvDecorationBufferBlock
:
925 vtn_assert(type
->base_type
== vtn_base_type_struct
);
926 type
->buffer_block
= true;
928 case SpvDecorationGLSLShared
:
929 case SpvDecorationGLSLPacked
:
930 /* Ignore these, since we get explicit offsets anyways */
933 case SpvDecorationRowMajor
:
934 case SpvDecorationColMajor
:
935 case SpvDecorationMatrixStride
:
936 case SpvDecorationBuiltIn
:
937 case SpvDecorationNoPerspective
:
938 case SpvDecorationFlat
:
939 case SpvDecorationPatch
:
940 case SpvDecorationCentroid
:
941 case SpvDecorationSample
:
942 case SpvDecorationVolatile
:
943 case SpvDecorationCoherent
:
944 case SpvDecorationNonWritable
:
945 case SpvDecorationNonReadable
:
946 case SpvDecorationUniform
:
947 case SpvDecorationLocation
:
948 case SpvDecorationComponent
:
949 case SpvDecorationOffset
:
950 case SpvDecorationXfbBuffer
:
951 case SpvDecorationXfbStride
:
952 case SpvDecorationHlslSemanticGOOGLE
:
953 vtn_warn("Decoration only allowed for struct members: %s",
954 spirv_decoration_to_string(dec
->decoration
));
957 case SpvDecorationStream
:
958 /* We don't need to do anything here, as stream is filled up when
959 * aplying the decoration to a variable, just check that if it is not a
960 * struct member, it should be a struct.
962 vtn_assert(type
->base_type
== vtn_base_type_struct
);
965 case SpvDecorationRelaxedPrecision
:
966 case SpvDecorationSpecId
:
967 case SpvDecorationInvariant
:
968 case SpvDecorationRestrict
:
969 case SpvDecorationAliased
:
970 case SpvDecorationConstant
:
971 case SpvDecorationIndex
:
972 case SpvDecorationBinding
:
973 case SpvDecorationDescriptorSet
:
974 case SpvDecorationLinkageAttributes
:
975 case SpvDecorationNoContraction
:
976 case SpvDecorationInputAttachmentIndex
:
977 vtn_warn("Decoration not allowed on types: %s",
978 spirv_decoration_to_string(dec
->decoration
));
981 case SpvDecorationCPacked
:
982 case SpvDecorationSaturatedConversion
:
983 case SpvDecorationFuncParamAttr
:
984 case SpvDecorationFPRoundingMode
:
985 case SpvDecorationFPFastMathMode
:
986 case SpvDecorationAlignment
:
987 vtn_warn("Decoration only allowed for CL-style kernels: %s",
988 spirv_decoration_to_string(dec
->decoration
));
992 vtn_fail("Unhandled decoration");
997 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
1000 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
1001 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
1002 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
1003 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
1004 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
1005 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
1006 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
1007 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
1008 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
1009 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
1010 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
1011 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
1012 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
1013 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
1014 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
1015 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
1016 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
1017 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
1018 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
1019 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
1020 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
1021 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
1022 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
1023 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
1024 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
1025 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
1026 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
1027 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
1028 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
1029 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
1030 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
1031 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
1032 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
1033 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
1034 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
1035 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
1036 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
1037 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
1038 case SpvImageFormatR16ui
: return 0x8234; /* GL_R16UI */
1039 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
1041 vtn_fail("Invalid image format");
1045 static struct vtn_type
*
1046 vtn_type_layout_std430(struct vtn_builder
*b
, struct vtn_type
*type
,
1047 uint32_t *size_out
, uint32_t *align_out
)
1049 switch (type
->base_type
) {
1050 case vtn_base_type_scalar
: {
1051 uint32_t comp_size
= glsl_type_is_boolean(type
->type
)
1052 ? 4 : glsl_get_bit_size(type
->type
) / 8;
1053 *size_out
= comp_size
;
1054 *align_out
= comp_size
;
1058 case vtn_base_type_vector
: {
1059 uint32_t comp_size
= glsl_type_is_boolean(type
->type
)
1060 ? 4 : glsl_get_bit_size(type
->type
) / 8;
1061 unsigned align_comps
= type
->length
== 3 ? 4 : type
->length
;
1062 *size_out
= comp_size
* type
->length
,
1063 *align_out
= comp_size
* align_comps
;
1067 case vtn_base_type_matrix
:
1068 case vtn_base_type_array
: {
1069 /* We're going to add an array stride */
1070 type
= vtn_type_copy(b
, type
);
1071 uint32_t elem_size
, elem_align
;
1072 type
->array_element
= vtn_type_layout_std430(b
, type
->array_element
,
1073 &elem_size
, &elem_align
);
1074 type
->stride
= vtn_align_u32(elem_size
, elem_align
);
1075 *size_out
= type
->stride
* type
->length
;
1076 *align_out
= elem_align
;
1080 case vtn_base_type_struct
: {
1081 /* We're going to add member offsets */
1082 type
= vtn_type_copy(b
, type
);
1083 uint32_t offset
= 0;
1085 for (unsigned i
= 0; i
< type
->length
; i
++) {
1086 uint32_t mem_size
, mem_align
;
1087 type
->members
[i
] = vtn_type_layout_std430(b
, type
->members
[i
],
1088 &mem_size
, &mem_align
);
1089 offset
= vtn_align_u32(offset
, mem_align
);
1090 type
->offsets
[i
] = offset
;
1092 align
= MAX2(align
, mem_align
);
1100 unreachable("Invalid SPIR-V type for std430");
1105 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
1106 const uint32_t *w
, unsigned count
)
1108 struct vtn_value
*val
= NULL
;
1110 /* In order to properly handle forward declarations, we have to defer
1111 * allocation for pointer types.
1113 if (opcode
!= SpvOpTypePointer
&& opcode
!= SpvOpTypeForwardPointer
) {
1114 val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1115 vtn_fail_if(val
->type
!= NULL
,
1116 "Only pointers can have forward declarations");
1117 val
->type
= rzalloc(b
, struct vtn_type
);
1118 val
->type
->id
= w
[1];
1123 val
->type
->base_type
= vtn_base_type_void
;
1124 val
->type
->type
= glsl_void_type();
1127 val
->type
->base_type
= vtn_base_type_scalar
;
1128 val
->type
->type
= glsl_bool_type();
1129 val
->type
->length
= 1;
1131 case SpvOpTypeInt
: {
1132 int bit_size
= w
[2];
1133 const bool signedness
= w
[3];
1134 val
->type
->base_type
= vtn_base_type_scalar
;
1137 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1140 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1143 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1146 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1149 vtn_fail("Invalid int bit size");
1151 val
->type
->length
= 1;
1155 case SpvOpTypeFloat
: {
1156 int bit_size
= w
[2];
1157 val
->type
->base_type
= vtn_base_type_scalar
;
1160 val
->type
->type
= glsl_float16_t_type();
1163 val
->type
->type
= glsl_float_type();
1166 val
->type
->type
= glsl_double_type();
1169 vtn_fail("Invalid float bit size");
1171 val
->type
->length
= 1;
1175 case SpvOpTypeVector
: {
1176 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1177 unsigned elems
= w
[3];
1179 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1180 "Base type for OpTypeVector must be a scalar");
1181 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1182 "Invalid component count for OpTypeVector");
1184 val
->type
->base_type
= vtn_base_type_vector
;
1185 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1186 val
->type
->length
= elems
;
1187 val
->type
->stride
= glsl_type_is_boolean(val
->type
->type
)
1188 ? 4 : glsl_get_bit_size(base
->type
) / 8;
1189 val
->type
->array_element
= base
;
1193 case SpvOpTypeMatrix
: {
1194 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1195 unsigned columns
= w
[3];
1197 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1198 "Base type for OpTypeMatrix must be a vector");
1199 vtn_fail_if(columns
< 2 || columns
> 4,
1200 "Invalid column count for OpTypeMatrix");
1202 val
->type
->base_type
= vtn_base_type_matrix
;
1203 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1204 glsl_get_vector_elements(base
->type
),
1206 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1207 "Unsupported base type for OpTypeMatrix");
1208 assert(!glsl_type_is_error(val
->type
->type
));
1209 val
->type
->length
= columns
;
1210 val
->type
->array_element
= base
;
1211 val
->type
->row_major
= false;
1212 val
->type
->stride
= 0;
1216 case SpvOpTypeRuntimeArray
:
1217 case SpvOpTypeArray
: {
1218 struct vtn_type
*array_element
=
1219 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1221 if (opcode
== SpvOpTypeRuntimeArray
) {
1222 /* A length of 0 is used to denote unsized arrays */
1223 val
->type
->length
= 0;
1226 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
1229 val
->type
->base_type
= vtn_base_type_array
;
1230 val
->type
->array_element
= array_element
;
1231 val
->type
->stride
= 0;
1233 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1234 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
,
1239 case SpvOpTypeStruct
: {
1240 unsigned num_fields
= count
- 2;
1241 val
->type
->base_type
= vtn_base_type_struct
;
1242 val
->type
->length
= num_fields
;
1243 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1244 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1246 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1247 for (unsigned i
= 0; i
< num_fields
; i
++) {
1248 val
->type
->members
[i
] =
1249 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
1250 fields
[i
] = (struct glsl_struct_field
) {
1251 .type
= val
->type
->members
[i
]->type
,
1252 .name
= ralloc_asprintf(b
, "field%d", i
),
1258 struct member_decoration_ctx ctx
= {
1259 .num_fields
= num_fields
,
1264 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1265 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1267 const char *name
= val
->name
? val
->name
: "struct";
1269 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
);
1273 case SpvOpTypeFunction
: {
1274 val
->type
->base_type
= vtn_base_type_function
;
1275 val
->type
->type
= NULL
;
1277 val
->type
->return_type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1279 const unsigned num_params
= count
- 3;
1280 val
->type
->length
= num_params
;
1281 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1282 for (unsigned i
= 0; i
< count
- 3; i
++) {
1283 val
->type
->params
[i
] =
1284 vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
;
1289 case SpvOpTypePointer
:
1290 case SpvOpTypeForwardPointer
: {
1291 /* We can't blindly push the value because it might be a forward
1294 val
= vtn_untyped_value(b
, w
[1]);
1296 SpvStorageClass storage_class
= w
[2];
1298 if (val
->value_type
== vtn_value_type_invalid
) {
1299 val
->value_type
= vtn_value_type_type
;
1300 val
->type
= rzalloc(b
, struct vtn_type
);
1301 val
->type
->id
= w
[1];
1302 val
->type
->base_type
= vtn_base_type_pointer
;
1303 val
->type
->storage_class
= storage_class
;
1305 /* These can actually be stored to nir_variables and used as SSA
1306 * values so they need a real glsl_type.
1308 switch (storage_class
) {
1309 case SpvStorageClassUniform
:
1310 val
->type
->type
= b
->options
->ubo_ptr_type
;
1312 case SpvStorageClassStorageBuffer
:
1313 val
->type
->type
= b
->options
->ssbo_ptr_type
;
1315 case SpvStorageClassPhysicalStorageBufferEXT
:
1316 val
->type
->type
= b
->options
->phys_ssbo_ptr_type
;
1318 case SpvStorageClassPushConstant
:
1319 val
->type
->type
= b
->options
->push_const_ptr_type
;
1321 case SpvStorageClassWorkgroup
:
1322 val
->type
->type
= b
->options
->shared_ptr_type
;
1325 /* In this case, no variable pointers are allowed so all deref
1326 * chains are complete back to the variable and it doesn't matter
1327 * what type gets used so we leave it NULL.
1332 vtn_fail_if(val
->type
->storage_class
!= storage_class
,
1333 "The storage classes of an OpTypePointer and any "
1334 "OpTypeForwardPointers that provide forward "
1335 "declarations of it must match.");
1338 if (opcode
== SpvOpTypePointer
) {
1339 vtn_fail_if(val
->type
->deref
!= NULL
,
1340 "While OpTypeForwardPointer can be used to provide a "
1341 "forward declaration of a pointer, OpTypePointer can "
1342 "only be used once for a given id.");
1344 val
->type
->deref
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
1346 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1348 if (storage_class
== SpvStorageClassWorkgroup
&&
1349 b
->options
->lower_workgroup_access_to_offsets
) {
1350 uint32_t size
, align
;
1351 val
->type
->deref
= vtn_type_layout_std430(b
, val
->type
->deref
,
1353 val
->type
->length
= size
;
1354 val
->type
->align
= align
;
1360 case SpvOpTypeImage
: {
1361 val
->type
->base_type
= vtn_base_type_image
;
1363 const struct vtn_type
*sampled_type
=
1364 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1366 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1367 glsl_get_bit_size(sampled_type
->type
) != 32,
1368 "Sampled type of OpTypeImage must be a 32-bit scalar");
1370 enum glsl_sampler_dim dim
;
1371 switch ((SpvDim
)w
[3]) {
1372 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1373 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1374 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1375 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1376 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1377 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1378 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1380 vtn_fail("Invalid SPIR-V image dimensionality");
1383 bool is_shadow
= w
[4];
1384 bool is_array
= w
[5];
1385 bool multisampled
= w
[6];
1386 unsigned sampled
= w
[7];
1387 SpvImageFormat format
= w
[8];
1390 val
->type
->access_qualifier
= w
[9];
1392 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1395 if (dim
== GLSL_SAMPLER_DIM_2D
)
1396 dim
= GLSL_SAMPLER_DIM_MS
;
1397 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1398 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1400 vtn_fail("Unsupported multisampled image type");
1403 val
->type
->image_format
= translate_image_format(b
, format
);
1405 enum glsl_base_type sampled_base_type
=
1406 glsl_get_base_type(sampled_type
->type
);
1408 val
->type
->sampled
= true;
1409 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
1411 } else if (sampled
== 2) {
1412 vtn_assert(!is_shadow
);
1413 val
->type
->sampled
= false;
1414 val
->type
->type
= glsl_image_type(dim
, is_array
, sampled_base_type
);
1416 vtn_fail("We need to know if the image will be sampled");
1421 case SpvOpTypeSampledImage
:
1422 val
->type
->base_type
= vtn_base_type_sampled_image
;
1423 val
->type
->image
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1424 val
->type
->type
= val
->type
->image
->type
;
1427 case SpvOpTypeSampler
:
1428 /* The actual sampler type here doesn't really matter. It gets
1429 * thrown away the moment you combine it with an image. What really
1430 * matters is that it's a sampler type as opposed to an integer type
1431 * so the backend knows what to do.
1433 val
->type
->base_type
= vtn_base_type_sampler
;
1434 val
->type
->type
= glsl_bare_sampler_type();
1437 case SpvOpTypeOpaque
:
1438 case SpvOpTypeEvent
:
1439 case SpvOpTypeDeviceEvent
:
1440 case SpvOpTypeReserveId
:
1441 case SpvOpTypeQueue
:
1444 vtn_fail("Unhandled opcode");
1447 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1449 if (val
->type
->base_type
== vtn_base_type_struct
&&
1450 (val
->type
->block
|| val
->type
->buffer_block
)) {
1451 for (unsigned i
= 0; i
< val
->type
->length
; i
++) {
1452 vtn_fail_if(vtn_type_contains_block(b
, val
->type
->members
[i
]),
1453 "Block and BufferBlock decorations cannot decorate a "
1454 "structure type that is nested at any level inside "
1455 "another structure type decorated with Block or "
1461 static nir_constant
*
1462 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
1464 nir_constant
*c
= rzalloc(b
, nir_constant
);
1466 /* For pointers and other typeless things, we have to return something but
1467 * it doesn't matter what.
1472 switch (glsl_get_base_type(type
)) {
1474 case GLSL_TYPE_UINT
:
1475 case GLSL_TYPE_INT16
:
1476 case GLSL_TYPE_UINT16
:
1477 case GLSL_TYPE_UINT8
:
1478 case GLSL_TYPE_INT8
:
1479 case GLSL_TYPE_INT64
:
1480 case GLSL_TYPE_UINT64
:
1481 case GLSL_TYPE_BOOL
:
1482 case GLSL_TYPE_FLOAT
:
1483 case GLSL_TYPE_FLOAT16
:
1484 case GLSL_TYPE_DOUBLE
:
1485 /* Nothing to do here. It's already initialized to zero */
1488 case GLSL_TYPE_ARRAY
:
1489 vtn_assert(glsl_get_length(type
) > 0);
1490 c
->num_elements
= glsl_get_length(type
);
1491 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1493 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
1494 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1495 c
->elements
[i
] = c
->elements
[0];
1498 case GLSL_TYPE_STRUCT
:
1499 c
->num_elements
= glsl_get_length(type
);
1500 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1502 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
1503 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
1508 vtn_fail("Invalid type for null constant");
1515 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
1516 int member
, const struct vtn_decoration
*dec
,
1519 vtn_assert(member
== -1);
1520 if (dec
->decoration
!= SpvDecorationSpecId
)
1523 struct spec_constant_value
*const_value
= data
;
1525 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1526 if (b
->specializations
[i
].id
== dec
->literals
[0]) {
1527 if (const_value
->is_double
)
1528 const_value
->data64
= b
->specializations
[i
].data64
;
1530 const_value
->data32
= b
->specializations
[i
].data32
;
1537 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
1538 uint32_t const_value
)
1540 struct spec_constant_value data
;
1541 data
.is_double
= false;
1542 data
.data32
= const_value
;
1543 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1548 get_specialization64(struct vtn_builder
*b
, struct vtn_value
*val
,
1549 uint64_t const_value
)
1551 struct spec_constant_value data
;
1552 data
.is_double
= true;
1553 data
.data64
= const_value
;
1554 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1559 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1560 struct vtn_value
*val
,
1562 const struct vtn_decoration
*dec
,
1565 vtn_assert(member
== -1);
1566 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1567 dec
->literals
[0] != SpvBuiltInWorkgroupSize
)
1570 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1571 b
->workgroup_size_builtin
= val
;
1575 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1576 const uint32_t *w
, unsigned count
)
1578 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1579 val
->constant
= rzalloc(b
, nir_constant
);
1581 case SpvOpConstantTrue
:
1582 case SpvOpConstantFalse
:
1583 case SpvOpSpecConstantTrue
:
1584 case SpvOpSpecConstantFalse
: {
1585 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1586 "Result type of %s must be OpTypeBool",
1587 spirv_op_to_string(opcode
));
1589 uint32_t int_val
= (opcode
== SpvOpConstantTrue
||
1590 opcode
== SpvOpSpecConstantTrue
);
1592 if (opcode
== SpvOpSpecConstantTrue
||
1593 opcode
== SpvOpSpecConstantFalse
)
1594 int_val
= get_specialization(b
, val
, int_val
);
1596 val
->constant
->values
[0].b
[0] = int_val
!= 0;
1600 case SpvOpConstant
: {
1601 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1602 "Result type of %s must be a scalar",
1603 spirv_op_to_string(opcode
));
1604 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1607 val
->constant
->values
->u64
[0] = vtn_u64_literal(&w
[3]);
1610 val
->constant
->values
->u32
[0] = w
[3];
1613 val
->constant
->values
->u16
[0] = w
[3];
1616 val
->constant
->values
->u8
[0] = w
[3];
1619 vtn_fail("Unsupported SpvOpConstant bit size");
1624 case SpvOpSpecConstant
: {
1625 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1626 "Result type of %s must be a scalar",
1627 spirv_op_to_string(opcode
));
1628 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1631 val
->constant
->values
[0].u64
[0] =
1632 get_specialization64(b
, val
, vtn_u64_literal(&w
[3]));
1635 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1638 val
->constant
->values
[0].u16
[0] = get_specialization(b
, val
, w
[3]);
1641 val
->constant
->values
[0].u8
[0] = get_specialization(b
, val
, w
[3]);
1644 vtn_fail("Unsupported SpvOpSpecConstant bit size");
1649 case SpvOpSpecConstantComposite
:
1650 case SpvOpConstantComposite
: {
1651 unsigned elem_count
= count
- 3;
1652 vtn_fail_if(elem_count
!= val
->type
->length
,
1653 "%s has %u constituents, expected %u",
1654 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1656 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1657 for (unsigned i
= 0; i
< elem_count
; i
++) {
1658 struct vtn_value
*val
= vtn_untyped_value(b
, w
[i
+ 3]);
1660 if (val
->value_type
== vtn_value_type_constant
) {
1661 elems
[i
] = val
->constant
;
1663 vtn_fail_if(val
->value_type
!= vtn_value_type_undef
,
1664 "only constants or undefs allowed for "
1665 "SpvOpConstantComposite");
1666 /* to make it easier, just insert a NULL constant for now */
1667 elems
[i
] = vtn_null_constant(b
, val
->type
->type
);
1671 switch (val
->type
->base_type
) {
1672 case vtn_base_type_vector
: {
1673 assert(glsl_type_is_vector(val
->type
->type
));
1674 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1675 for (unsigned i
= 0; i
< elem_count
; i
++) {
1678 val
->constant
->values
[0].u64
[i
] = elems
[i
]->values
[0].u64
[0];
1681 val
->constant
->values
[0].u32
[i
] = elems
[i
]->values
[0].u32
[0];
1684 val
->constant
->values
[0].u16
[i
] = elems
[i
]->values
[0].u16
[0];
1687 val
->constant
->values
[0].u8
[i
] = elems
[i
]->values
[0].u8
[0];
1690 val
->constant
->values
[0].b
[i
] = elems
[i
]->values
[0].b
[0];
1693 vtn_fail("Invalid SpvOpConstantComposite bit size");
1699 case vtn_base_type_matrix
:
1700 assert(glsl_type_is_matrix(val
->type
->type
));
1701 for (unsigned i
= 0; i
< elem_count
; i
++)
1702 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1705 case vtn_base_type_struct
:
1706 case vtn_base_type_array
:
1707 ralloc_steal(val
->constant
, elems
);
1708 val
->constant
->num_elements
= elem_count
;
1709 val
->constant
->elements
= elems
;
1713 vtn_fail("Result type of %s must be a composite type",
1714 spirv_op_to_string(opcode
));
1719 case SpvOpSpecConstantOp
: {
1720 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1722 case SpvOpVectorShuffle
: {
1723 struct vtn_value
*v0
= &b
->values
[w
[4]];
1724 struct vtn_value
*v1
= &b
->values
[w
[5]];
1726 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1727 v0
->value_type
== vtn_value_type_undef
);
1728 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1729 v1
->value_type
== vtn_value_type_undef
);
1731 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1732 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1734 vtn_assert(len0
+ len1
< 16);
1736 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1737 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1738 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1740 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1741 (void)bit_size0
; (void)bit_size1
;
1743 if (bit_size
== 64) {
1745 if (v0
->value_type
== vtn_value_type_constant
) {
1746 for (unsigned i
= 0; i
< len0
; i
++)
1747 u64
[i
] = v0
->constant
->values
[0].u64
[i
];
1749 if (v1
->value_type
== vtn_value_type_constant
) {
1750 for (unsigned i
= 0; i
< len1
; i
++)
1751 u64
[len0
+ i
] = v1
->constant
->values
[0].u64
[i
];
1754 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1755 uint32_t comp
= w
[i
+ 6];
1756 /* If component is not used, set the value to a known constant
1757 * to detect if it is wrongly used.
1759 if (comp
== (uint32_t)-1)
1760 val
->constant
->values
[0].u64
[j
] = 0xdeadbeefdeadbeef;
1762 val
->constant
->values
[0].u64
[j
] = u64
[comp
];
1765 /* This is for both 32-bit and 16-bit values */
1767 if (v0
->value_type
== vtn_value_type_constant
) {
1768 for (unsigned i
= 0; i
< len0
; i
++)
1769 u32
[i
] = v0
->constant
->values
[0].u32
[i
];
1771 if (v1
->value_type
== vtn_value_type_constant
) {
1772 for (unsigned i
= 0; i
< len1
; i
++)
1773 u32
[len0
+ i
] = v1
->constant
->values
[0].u32
[i
];
1776 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1777 uint32_t comp
= w
[i
+ 6];
1778 /* If component is not used, set the value to a known constant
1779 * to detect if it is wrongly used.
1781 if (comp
== (uint32_t)-1)
1782 val
->constant
->values
[0].u32
[j
] = 0xdeadbeef;
1784 val
->constant
->values
[0].u32
[j
] = u32
[comp
];
1790 case SpvOpCompositeExtract
:
1791 case SpvOpCompositeInsert
: {
1792 struct vtn_value
*comp
;
1793 unsigned deref_start
;
1794 struct nir_constant
**c
;
1795 if (opcode
== SpvOpCompositeExtract
) {
1796 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1798 c
= &comp
->constant
;
1800 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1802 val
->constant
= nir_constant_clone(comp
->constant
,
1809 const struct vtn_type
*type
= comp
->type
;
1810 for (unsigned i
= deref_start
; i
< count
; i
++) {
1811 vtn_fail_if(w
[i
] > type
->length
,
1812 "%uth index of %s is %u but the type has only "
1813 "%u elements", i
- deref_start
,
1814 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1816 switch (type
->base_type
) {
1817 case vtn_base_type_vector
:
1819 type
= type
->array_element
;
1822 case vtn_base_type_matrix
:
1823 assert(col
== 0 && elem
== -1);
1826 type
= type
->array_element
;
1829 case vtn_base_type_array
:
1830 c
= &(*c
)->elements
[w
[i
]];
1831 type
= type
->array_element
;
1834 case vtn_base_type_struct
:
1835 c
= &(*c
)->elements
[w
[i
]];
1836 type
= type
->members
[w
[i
]];
1840 vtn_fail("%s must only index into composite types",
1841 spirv_op_to_string(opcode
));
1845 if (opcode
== SpvOpCompositeExtract
) {
1849 unsigned num_components
= type
->length
;
1850 unsigned bit_size
= glsl_get_bit_size(type
->type
);
1851 for (unsigned i
= 0; i
< num_components
; i
++)
1854 val
->constant
->values
[0].u64
[i
] = (*c
)->values
[col
].u64
[elem
+ i
];
1857 val
->constant
->values
[0].u32
[i
] = (*c
)->values
[col
].u32
[elem
+ i
];
1860 val
->constant
->values
[0].u16
[i
] = (*c
)->values
[col
].u16
[elem
+ i
];
1863 val
->constant
->values
[0].u8
[i
] = (*c
)->values
[col
].u8
[elem
+ i
];
1866 val
->constant
->values
[0].b
[i
] = (*c
)->values
[col
].b
[elem
+ i
];
1869 vtn_fail("Invalid SpvOpCompositeExtract bit size");
1873 struct vtn_value
*insert
=
1874 vtn_value(b
, w
[4], vtn_value_type_constant
);
1875 vtn_assert(insert
->type
== type
);
1877 *c
= insert
->constant
;
1879 unsigned num_components
= type
->length
;
1880 unsigned bit_size
= glsl_get_bit_size(type
->type
);
1881 for (unsigned i
= 0; i
< num_components
; i
++)
1884 (*c
)->values
[col
].u64
[elem
+ i
] = insert
->constant
->values
[0].u64
[i
];
1887 (*c
)->values
[col
].u32
[elem
+ i
] = insert
->constant
->values
[0].u32
[i
];
1890 (*c
)->values
[col
].u16
[elem
+ i
] = insert
->constant
->values
[0].u16
[i
];
1893 (*c
)->values
[col
].u8
[elem
+ i
] = insert
->constant
->values
[0].u8
[i
];
1896 (*c
)->values
[col
].b
[elem
+ i
] = insert
->constant
->values
[0].b
[i
];
1899 vtn_fail("Invalid SpvOpCompositeInsert bit size");
1908 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
1909 nir_alu_type src_alu_type
= dst_alu_type
;
1910 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
1913 vtn_assert(count
<= 7);
1918 /* We have a source in a conversion */
1920 nir_get_nir_type_for_glsl_type(
1921 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1922 /* We use the bitsize of the conversion source to evaluate the opcode later */
1923 bit_size
= glsl_get_bit_size(
1924 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1927 bit_size
= glsl_get_bit_size(val
->type
->type
);
1930 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
1931 nir_alu_type_get_type_size(src_alu_type
),
1932 nir_alu_type_get_type_size(dst_alu_type
));
1933 nir_const_value src
[4];
1935 for (unsigned i
= 0; i
< count
- 4; i
++) {
1936 struct vtn_value
*src_val
=
1937 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
);
1939 /* If this is an unsized source, pull the bit size from the
1940 * source; otherwise, we'll use the bit size from the destination.
1942 if (!nir_alu_type_get_type_size(nir_op_infos
[op
].input_types
[i
]))
1943 bit_size
= glsl_get_bit_size(src_val
->type
->type
);
1945 unsigned j
= swap
? 1 - i
: i
;
1946 src
[j
] = src_val
->constant
->values
[0];
1949 /* fix up fixed size sources */
1956 for (unsigned i
= 0; i
< num_components
; ++i
) {
1958 case 64: src
[1].u32
[i
] = src
[1].u64
[i
]; break;
1959 case 16: src
[1].u32
[i
] = src
[1].u16
[i
]; break;
1960 case 8: src
[1].u32
[i
] = src
[1].u8
[i
]; break;
1969 val
->constant
->values
[0] =
1970 nir_eval_const_opcode(op
, num_components
, bit_size
, src
);
1977 case SpvOpConstantNull
:
1978 val
->constant
= vtn_null_constant(b
, val
->type
->type
);
1981 case SpvOpConstantSampler
:
1982 vtn_fail("OpConstantSampler requires Kernel Capability");
1986 vtn_fail("Unhandled opcode");
1989 /* Now that we have the value, update the workgroup size if needed */
1990 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1993 struct vtn_ssa_value
*
1994 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1996 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1999 if (!glsl_type_is_vector_or_scalar(type
)) {
2000 unsigned elems
= glsl_get_length(type
);
2001 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2002 for (unsigned i
= 0; i
< elems
; i
++) {
2003 const struct glsl_type
*child_type
;
2005 switch (glsl_get_base_type(type
)) {
2007 case GLSL_TYPE_UINT
:
2008 case GLSL_TYPE_INT16
:
2009 case GLSL_TYPE_UINT16
:
2010 case GLSL_TYPE_UINT8
:
2011 case GLSL_TYPE_INT8
:
2012 case GLSL_TYPE_INT64
:
2013 case GLSL_TYPE_UINT64
:
2014 case GLSL_TYPE_BOOL
:
2015 case GLSL_TYPE_FLOAT
:
2016 case GLSL_TYPE_FLOAT16
:
2017 case GLSL_TYPE_DOUBLE
:
2018 child_type
= glsl_get_column_type(type
);
2020 case GLSL_TYPE_ARRAY
:
2021 child_type
= glsl_get_array_element(type
);
2023 case GLSL_TYPE_STRUCT
:
2024 child_type
= glsl_get_struct_field(type
, i
);
2027 vtn_fail("unkown base type");
2030 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
2038 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
2041 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
2042 src
.src_type
= type
;
2047 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
2048 const uint32_t *w
, unsigned count
)
2050 if (opcode
== SpvOpSampledImage
) {
2051 struct vtn_value
*val
=
2052 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2053 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2054 val
->sampled_image
->type
=
2055 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2056 val
->sampled_image
->image
=
2057 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2058 val
->sampled_image
->sampler
=
2059 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
2061 } else if (opcode
== SpvOpImage
) {
2062 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2063 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
2064 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
2065 val
->pointer
= src_val
->sampled_image
->image
;
2067 vtn_assert(src_val
->value_type
== vtn_value_type_pointer
);
2068 val
->pointer
= src_val
->pointer
;
2073 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2074 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2076 struct vtn_sampled_image sampled
;
2077 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
2078 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
2079 sampled
= *sampled_val
->sampled_image
;
2081 vtn_assert(sampled_val
->value_type
== vtn_value_type_pointer
);
2082 sampled
.type
= sampled_val
->pointer
->type
;
2083 sampled
.image
= NULL
;
2084 sampled
.sampler
= sampled_val
->pointer
;
2087 const struct glsl_type
*image_type
= sampled
.type
->type
;
2088 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
2089 const bool is_array
= glsl_sampler_type_is_array(image_type
);
2091 /* Figure out the base texture operation */
2094 case SpvOpImageSampleImplicitLod
:
2095 case SpvOpImageSampleDrefImplicitLod
:
2096 case SpvOpImageSampleProjImplicitLod
:
2097 case SpvOpImageSampleProjDrefImplicitLod
:
2098 texop
= nir_texop_tex
;
2101 case SpvOpImageSampleExplicitLod
:
2102 case SpvOpImageSampleDrefExplicitLod
:
2103 case SpvOpImageSampleProjExplicitLod
:
2104 case SpvOpImageSampleProjDrefExplicitLod
:
2105 texop
= nir_texop_txl
;
2108 case SpvOpImageFetch
:
2109 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
2110 texop
= nir_texop_txf_ms
;
2112 texop
= nir_texop_txf
;
2116 case SpvOpImageGather
:
2117 case SpvOpImageDrefGather
:
2118 texop
= nir_texop_tg4
;
2121 case SpvOpImageQuerySizeLod
:
2122 case SpvOpImageQuerySize
:
2123 texop
= nir_texop_txs
;
2126 case SpvOpImageQueryLod
:
2127 texop
= nir_texop_lod
;
2130 case SpvOpImageQueryLevels
:
2131 texop
= nir_texop_query_levels
;
2134 case SpvOpImageQuerySamples
:
2135 texop
= nir_texop_texture_samples
;
2139 vtn_fail("Unhandled opcode");
2142 nir_tex_src srcs
[10]; /* 10 should be enough */
2143 nir_tex_src
*p
= srcs
;
2145 nir_deref_instr
*sampler
= vtn_pointer_to_deref(b
, sampled
.sampler
);
2146 nir_deref_instr
*texture
=
2147 sampled
.image
? vtn_pointer_to_deref(b
, sampled
.image
) : sampler
;
2149 p
->src
= nir_src_for_ssa(&texture
->dest
.ssa
);
2150 p
->src_type
= nir_tex_src_texture_deref
;
2160 /* These operations require a sampler */
2161 p
->src
= nir_src_for_ssa(&sampler
->dest
.ssa
);
2162 p
->src_type
= nir_tex_src_sampler_deref
;
2166 case nir_texop_txf_ms
:
2168 case nir_texop_query_levels
:
2169 case nir_texop_texture_samples
:
2170 case nir_texop_samples_identical
:
2173 case nir_texop_txf_ms_mcs
:
2174 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2179 struct nir_ssa_def
*coord
;
2180 unsigned coord_components
;
2182 case SpvOpImageSampleImplicitLod
:
2183 case SpvOpImageSampleExplicitLod
:
2184 case SpvOpImageSampleDrefImplicitLod
:
2185 case SpvOpImageSampleDrefExplicitLod
:
2186 case SpvOpImageSampleProjImplicitLod
:
2187 case SpvOpImageSampleProjExplicitLod
:
2188 case SpvOpImageSampleProjDrefImplicitLod
:
2189 case SpvOpImageSampleProjDrefExplicitLod
:
2190 case SpvOpImageFetch
:
2191 case SpvOpImageGather
:
2192 case SpvOpImageDrefGather
:
2193 case SpvOpImageQueryLod
: {
2194 /* All these types have the coordinate as their first real argument */
2195 switch (sampler_dim
) {
2196 case GLSL_SAMPLER_DIM_1D
:
2197 case GLSL_SAMPLER_DIM_BUF
:
2198 coord_components
= 1;
2200 case GLSL_SAMPLER_DIM_2D
:
2201 case GLSL_SAMPLER_DIM_RECT
:
2202 case GLSL_SAMPLER_DIM_MS
:
2203 coord_components
= 2;
2205 case GLSL_SAMPLER_DIM_3D
:
2206 case GLSL_SAMPLER_DIM_CUBE
:
2207 coord_components
= 3;
2210 vtn_fail("Invalid sampler type");
2213 if (is_array
&& texop
!= nir_texop_lod
)
2216 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
2217 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2218 (1 << coord_components
) - 1));
2219 p
->src_type
= nir_tex_src_coord
;
2226 coord_components
= 0;
2231 case SpvOpImageSampleProjImplicitLod
:
2232 case SpvOpImageSampleProjExplicitLod
:
2233 case SpvOpImageSampleProjDrefImplicitLod
:
2234 case SpvOpImageSampleProjDrefExplicitLod
:
2235 /* These have the projector as the last coordinate component */
2236 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2237 p
->src_type
= nir_tex_src_projector
;
2245 bool is_shadow
= false;
2246 unsigned gather_component
= 0;
2248 case SpvOpImageSampleDrefImplicitLod
:
2249 case SpvOpImageSampleDrefExplicitLod
:
2250 case SpvOpImageSampleProjDrefImplicitLod
:
2251 case SpvOpImageSampleProjDrefExplicitLod
:
2252 case SpvOpImageDrefGather
:
2253 /* These all have an explicit depth value as their next source */
2255 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2258 case SpvOpImageGather
:
2259 /* This has a component as its next source */
2261 vtn_value(b
, w
[idx
++], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
2268 /* For OpImageQuerySizeLod, we always have an LOD */
2269 if (opcode
== SpvOpImageQuerySizeLod
)
2270 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2272 /* Now we need to handle some number of optional arguments */
2273 const struct vtn_ssa_value
*gather_offsets
= NULL
;
2275 uint32_t operands
= w
[idx
++];
2277 if (operands
& SpvImageOperandsBiasMask
) {
2278 vtn_assert(texop
== nir_texop_tex
);
2279 texop
= nir_texop_txb
;
2280 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
2283 if (operands
& SpvImageOperandsLodMask
) {
2284 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2285 texop
== nir_texop_txs
);
2286 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2289 if (operands
& SpvImageOperandsGradMask
) {
2290 vtn_assert(texop
== nir_texop_txl
);
2291 texop
= nir_texop_txd
;
2292 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
2293 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
2296 if (operands
& SpvImageOperandsOffsetMask
||
2297 operands
& SpvImageOperandsConstOffsetMask
)
2298 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
2300 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2301 nir_tex_src none
= {0};
2302 gather_offsets
= vtn_ssa_value(b
, w
[idx
++]);
2306 if (operands
& SpvImageOperandsSampleMask
) {
2307 vtn_assert(texop
== nir_texop_txf_ms
);
2308 texop
= nir_texop_txf_ms
;
2309 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
2312 if (operands
& SpvImageOperandsMinLodMask
) {
2313 vtn_assert(texop
== nir_texop_tex
||
2314 texop
== nir_texop_txb
||
2315 texop
== nir_texop_txd
);
2316 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_min_lod
);
2319 /* We should have now consumed exactly all of the arguments */
2320 vtn_assert(idx
== count
);
2322 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2325 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2327 instr
->coord_components
= coord_components
;
2328 instr
->sampler_dim
= sampler_dim
;
2329 instr
->is_array
= is_array
;
2330 instr
->is_shadow
= is_shadow
;
2331 instr
->is_new_style_shadow
=
2332 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2333 instr
->component
= gather_component
;
2335 switch (glsl_get_sampler_result_type(image_type
)) {
2336 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
2337 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
2338 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
2339 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
2341 vtn_fail("Invalid base type for sampler result");
2344 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2345 nir_tex_instr_dest_size(instr
), 32, NULL
);
2347 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2348 nir_tex_instr_dest_size(instr
));
2351 nir_instr
*instruction
;
2352 if (gather_offsets
) {
2353 vtn_assert(glsl_get_base_type(gather_offsets
->type
) == GLSL_TYPE_ARRAY
);
2354 vtn_assert(glsl_get_length(gather_offsets
->type
) == 4);
2355 nir_tex_instr
*instrs
[4] = {instr
, NULL
, NULL
, NULL
};
2357 /* Copy the current instruction 4x */
2358 for (uint32_t i
= 1; i
< 4; i
++) {
2359 instrs
[i
] = nir_tex_instr_create(b
->shader
, instr
->num_srcs
);
2360 instrs
[i
]->op
= instr
->op
;
2361 instrs
[i
]->coord_components
= instr
->coord_components
;
2362 instrs
[i
]->sampler_dim
= instr
->sampler_dim
;
2363 instrs
[i
]->is_array
= instr
->is_array
;
2364 instrs
[i
]->is_shadow
= instr
->is_shadow
;
2365 instrs
[i
]->is_new_style_shadow
= instr
->is_new_style_shadow
;
2366 instrs
[i
]->component
= instr
->component
;
2367 instrs
[i
]->dest_type
= instr
->dest_type
;
2369 memcpy(instrs
[i
]->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2371 nir_ssa_dest_init(&instrs
[i
]->instr
, &instrs
[i
]->dest
,
2372 nir_tex_instr_dest_size(instr
), 32, NULL
);
2375 /* Fill in the last argument with the offset from the passed in offsets
2376 * and insert the instruction into the stream.
2378 for (uint32_t i
= 0; i
< 4; i
++) {
2380 src
.src
= nir_src_for_ssa(gather_offsets
->elems
[i
]->def
);
2381 src
.src_type
= nir_tex_src_offset
;
2382 instrs
[i
]->src
[instrs
[i
]->num_srcs
- 1] = src
;
2383 nir_builder_instr_insert(&b
->nb
, &instrs
[i
]->instr
);
2386 /* Combine the results of the 4 instructions by taking their .w
2389 nir_alu_instr
*vec4
= nir_alu_instr_create(b
->shader
, nir_op_vec4
);
2390 nir_ssa_dest_init(&vec4
->instr
, &vec4
->dest
.dest
, 4, 32, NULL
);
2391 vec4
->dest
.write_mask
= 0xf;
2392 for (uint32_t i
= 0; i
< 4; i
++) {
2393 vec4
->src
[i
].src
= nir_src_for_ssa(&instrs
[i
]->dest
.ssa
);
2394 vec4
->src
[i
].swizzle
[0] = 3;
2396 def
= &vec4
->dest
.dest
.ssa
;
2397 instruction
= &vec4
->instr
;
2399 def
= &instr
->dest
.ssa
;
2400 instruction
= &instr
->instr
;
2403 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
2404 val
->ssa
->def
= def
;
2406 nir_builder_instr_insert(&b
->nb
, instruction
);
2410 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2411 const uint32_t *w
, nir_src
*src
)
2414 case SpvOpAtomicIIncrement
:
2415 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
2418 case SpvOpAtomicIDecrement
:
2419 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
2422 case SpvOpAtomicISub
:
2424 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
2427 case SpvOpAtomicCompareExchange
:
2428 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
2429 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
2432 case SpvOpAtomicExchange
:
2433 case SpvOpAtomicIAdd
:
2434 case SpvOpAtomicSMin
:
2435 case SpvOpAtomicUMin
:
2436 case SpvOpAtomicSMax
:
2437 case SpvOpAtomicUMax
:
2438 case SpvOpAtomicAnd
:
2440 case SpvOpAtomicXor
:
2441 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
2445 vtn_fail("Invalid SPIR-V atomic");
2449 static nir_ssa_def
*
2450 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2452 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
2454 /* The image_load_store intrinsics assume a 4-dim coordinate */
2455 unsigned dim
= glsl_get_vector_elements(coord
->type
);
2456 unsigned swizzle
[4];
2457 for (unsigned i
= 0; i
< 4; i
++)
2458 swizzle
[i
] = MIN2(i
, dim
- 1);
2460 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
2463 static nir_ssa_def
*
2464 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2466 if (value
->num_components
== 4)
2470 for (unsigned i
= 0; i
< 4; i
++)
2471 swiz
[i
] = i
< value
->num_components
? i
: 0;
2472 return nir_swizzle(b
, value
, swiz
, 4, false);
2476 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2477 const uint32_t *w
, unsigned count
)
2479 /* Just get this one out of the way */
2480 if (opcode
== SpvOpImageTexelPointer
) {
2481 struct vtn_value
*val
=
2482 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2483 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2485 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2486 val
->image
->coord
= get_image_coord(b
, w
[4]);
2487 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
2491 struct vtn_image_pointer image
;
2494 case SpvOpAtomicExchange
:
2495 case SpvOpAtomicCompareExchange
:
2496 case SpvOpAtomicCompareExchangeWeak
:
2497 case SpvOpAtomicIIncrement
:
2498 case SpvOpAtomicIDecrement
:
2499 case SpvOpAtomicIAdd
:
2500 case SpvOpAtomicISub
:
2501 case SpvOpAtomicLoad
:
2502 case SpvOpAtomicSMin
:
2503 case SpvOpAtomicUMin
:
2504 case SpvOpAtomicSMax
:
2505 case SpvOpAtomicUMax
:
2506 case SpvOpAtomicAnd
:
2508 case SpvOpAtomicXor
:
2509 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
2512 case SpvOpAtomicStore
:
2513 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
2516 case SpvOpImageQuerySize
:
2517 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2519 image
.sample
= NULL
;
2522 case SpvOpImageRead
:
2523 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2524 image
.coord
= get_image_coord(b
, w
[4]);
2526 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
2527 vtn_assert(w
[5] == SpvImageOperandsSampleMask
);
2528 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
2530 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2534 case SpvOpImageWrite
:
2535 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2536 image
.coord
= get_image_coord(b
, w
[2]);
2540 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
2541 vtn_assert(w
[4] == SpvImageOperandsSampleMask
);
2542 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
2544 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2549 vtn_fail("Invalid image opcode");
2552 nir_intrinsic_op op
;
2554 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
2555 OP(ImageQuerySize
, size
)
2557 OP(ImageWrite
, store
)
2558 OP(AtomicLoad
, load
)
2559 OP(AtomicStore
, store
)
2560 OP(AtomicExchange
, atomic_exchange
)
2561 OP(AtomicCompareExchange
, atomic_comp_swap
)
2562 OP(AtomicIIncrement
, atomic_add
)
2563 OP(AtomicIDecrement
, atomic_add
)
2564 OP(AtomicIAdd
, atomic_add
)
2565 OP(AtomicISub
, atomic_add
)
2566 OP(AtomicSMin
, atomic_min
)
2567 OP(AtomicUMin
, atomic_min
)
2568 OP(AtomicSMax
, atomic_max
)
2569 OP(AtomicUMax
, atomic_max
)
2570 OP(AtomicAnd
, atomic_and
)
2571 OP(AtomicOr
, atomic_or
)
2572 OP(AtomicXor
, atomic_xor
)
2575 vtn_fail("Invalid image opcode");
2578 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
2580 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
2581 intrin
->src
[0] = nir_src_for_ssa(&image_deref
->dest
.ssa
);
2583 /* ImageQuerySize doesn't take any extra parameters */
2584 if (opcode
!= SpvOpImageQuerySize
) {
2585 /* The image coordinate is always 4 components but we may not have that
2586 * many. Swizzle to compensate.
2588 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
2589 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
2593 case SpvOpAtomicLoad
:
2594 case SpvOpImageQuerySize
:
2595 case SpvOpImageRead
:
2597 case SpvOpAtomicStore
:
2598 case SpvOpImageWrite
: {
2599 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
2600 nir_ssa_def
*value
= vtn_ssa_value(b
, value_id
)->def
;
2601 /* nir_intrinsic_image_deref_store always takes a vec4 value */
2602 assert(op
== nir_intrinsic_image_deref_store
);
2603 intrin
->num_components
= 4;
2604 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
));
2608 case SpvOpAtomicCompareExchange
:
2609 case SpvOpAtomicIIncrement
:
2610 case SpvOpAtomicIDecrement
:
2611 case SpvOpAtomicExchange
:
2612 case SpvOpAtomicIAdd
:
2613 case SpvOpAtomicISub
:
2614 case SpvOpAtomicSMin
:
2615 case SpvOpAtomicUMin
:
2616 case SpvOpAtomicSMax
:
2617 case SpvOpAtomicUMax
:
2618 case SpvOpAtomicAnd
:
2620 case SpvOpAtomicXor
:
2621 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
2625 vtn_fail("Invalid image opcode");
2628 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
2629 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2630 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2632 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
2633 intrin
->num_components
= nir_intrinsic_infos
[op
].dest_components
;
2634 if (intrin
->num_components
== 0)
2635 intrin
->num_components
= dest_components
;
2637 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2638 intrin
->num_components
, 32, NULL
);
2640 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2642 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
2643 if (intrin
->num_components
!= dest_components
)
2644 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
2646 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
2647 val
->ssa
->def
= result
;
2649 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2653 static nir_intrinsic_op
2654 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2657 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2658 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2659 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2660 OP(AtomicExchange
, atomic_exchange
)
2661 OP(AtomicCompareExchange
, atomic_comp_swap
)
2662 OP(AtomicIIncrement
, atomic_add
)
2663 OP(AtomicIDecrement
, atomic_add
)
2664 OP(AtomicIAdd
, atomic_add
)
2665 OP(AtomicISub
, atomic_add
)
2666 OP(AtomicSMin
, atomic_imin
)
2667 OP(AtomicUMin
, atomic_umin
)
2668 OP(AtomicSMax
, atomic_imax
)
2669 OP(AtomicUMax
, atomic_umax
)
2670 OP(AtomicAnd
, atomic_and
)
2671 OP(AtomicOr
, atomic_or
)
2672 OP(AtomicXor
, atomic_xor
)
2675 vtn_fail("Invalid SSBO atomic");
2679 static nir_intrinsic_op
2680 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2683 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
2684 OP(AtomicLoad
, read_deref
)
2685 OP(AtomicExchange
, exchange
)
2686 OP(AtomicCompareExchange
, comp_swap
)
2687 OP(AtomicIIncrement
, inc_deref
)
2688 OP(AtomicIDecrement
, post_dec_deref
)
2689 OP(AtomicIAdd
, add_deref
)
2690 OP(AtomicISub
, add_deref
)
2691 OP(AtomicUMin
, min_deref
)
2692 OP(AtomicUMax
, max_deref
)
2693 OP(AtomicAnd
, and_deref
)
2694 OP(AtomicOr
, or_deref
)
2695 OP(AtomicXor
, xor_deref
)
2698 /* We left the following out: AtomicStore, AtomicSMin and
2699 * AtomicSmax. Right now there are not nir intrinsics for them. At this
2700 * moment Atomic Counter support is needed for ARB_spirv support, so is
2701 * only need to support GLSL Atomic Counters that are uints and don't
2702 * allow direct storage.
2704 unreachable("Invalid uniform atomic");
2708 static nir_intrinsic_op
2709 get_shared_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2712 case SpvOpAtomicLoad
: return nir_intrinsic_load_shared
;
2713 case SpvOpAtomicStore
: return nir_intrinsic_store_shared
;
2714 #define OP(S, N) case SpvOp##S: return nir_intrinsic_shared_##N;
2715 OP(AtomicExchange
, atomic_exchange
)
2716 OP(AtomicCompareExchange
, atomic_comp_swap
)
2717 OP(AtomicIIncrement
, atomic_add
)
2718 OP(AtomicIDecrement
, atomic_add
)
2719 OP(AtomicIAdd
, atomic_add
)
2720 OP(AtomicISub
, atomic_add
)
2721 OP(AtomicSMin
, atomic_imin
)
2722 OP(AtomicUMin
, atomic_umin
)
2723 OP(AtomicSMax
, atomic_imax
)
2724 OP(AtomicUMax
, atomic_umax
)
2725 OP(AtomicAnd
, atomic_and
)
2726 OP(AtomicOr
, atomic_or
)
2727 OP(AtomicXor
, atomic_xor
)
2730 vtn_fail("Invalid shared atomic");
2734 static nir_intrinsic_op
2735 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2738 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
2739 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
2740 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
2741 OP(AtomicExchange
, atomic_exchange
)
2742 OP(AtomicCompareExchange
, atomic_comp_swap
)
2743 OP(AtomicIIncrement
, atomic_add
)
2744 OP(AtomicIDecrement
, atomic_add
)
2745 OP(AtomicIAdd
, atomic_add
)
2746 OP(AtomicISub
, atomic_add
)
2747 OP(AtomicSMin
, atomic_imin
)
2748 OP(AtomicUMin
, atomic_umin
)
2749 OP(AtomicSMax
, atomic_imax
)
2750 OP(AtomicUMax
, atomic_umax
)
2751 OP(AtomicAnd
, atomic_and
)
2752 OP(AtomicOr
, atomic_or
)
2753 OP(AtomicXor
, atomic_xor
)
2756 vtn_fail("Invalid shared atomic");
2761 * Handles shared atomics, ssbo atomics and atomic counters.
2764 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
2765 const uint32_t *w
, unsigned count
)
2767 struct vtn_pointer
*ptr
;
2768 nir_intrinsic_instr
*atomic
;
2771 case SpvOpAtomicLoad
:
2772 case SpvOpAtomicExchange
:
2773 case SpvOpAtomicCompareExchange
:
2774 case SpvOpAtomicCompareExchangeWeak
:
2775 case SpvOpAtomicIIncrement
:
2776 case SpvOpAtomicIDecrement
:
2777 case SpvOpAtomicIAdd
:
2778 case SpvOpAtomicISub
:
2779 case SpvOpAtomicSMin
:
2780 case SpvOpAtomicUMin
:
2781 case SpvOpAtomicSMax
:
2782 case SpvOpAtomicUMax
:
2783 case SpvOpAtomicAnd
:
2785 case SpvOpAtomicXor
:
2786 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2789 case SpvOpAtomicStore
:
2790 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2794 vtn_fail("Invalid SPIR-V atomic");
2798 SpvScope scope = w[4];
2799 SpvMemorySemanticsMask semantics = w[5];
2802 /* uniform as "atomic counter uniform" */
2803 if (ptr
->mode
== vtn_variable_mode_uniform
) {
2804 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2805 const struct glsl_type
*deref_type
= deref
->type
;
2806 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
2807 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2808 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2810 /* SSBO needs to initialize index/offset. In this case we don't need to,
2811 * as that info is already stored on the ptr->var->var nir_variable (see
2812 * vtn_create_variable)
2816 case SpvOpAtomicLoad
:
2817 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2820 case SpvOpAtomicStore
:
2821 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2822 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2825 case SpvOpAtomicExchange
:
2826 case SpvOpAtomicCompareExchange
:
2827 case SpvOpAtomicCompareExchangeWeak
:
2828 case SpvOpAtomicIIncrement
:
2829 case SpvOpAtomicIDecrement
:
2830 case SpvOpAtomicIAdd
:
2831 case SpvOpAtomicISub
:
2832 case SpvOpAtomicSMin
:
2833 case SpvOpAtomicUMin
:
2834 case SpvOpAtomicSMax
:
2835 case SpvOpAtomicUMax
:
2836 case SpvOpAtomicAnd
:
2838 case SpvOpAtomicXor
:
2839 /* Nothing: we don't need to call fill_common_atomic_sources here, as
2840 * atomic counter uniforms doesn't have sources
2845 unreachable("Invalid SPIR-V atomic");
2848 } else if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
2849 nir_ssa_def
*offset
, *index
;
2850 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
2852 nir_intrinsic_op op
;
2853 if (ptr
->mode
== vtn_variable_mode_ssbo
) {
2854 op
= get_ssbo_nir_atomic_op(b
, opcode
);
2856 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
&&
2857 b
->options
->lower_workgroup_access_to_offsets
);
2858 op
= get_shared_nir_atomic_op(b
, opcode
);
2861 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2865 case SpvOpAtomicLoad
:
2866 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2867 nir_intrinsic_set_align(atomic
, 4, 0);
2868 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2869 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2870 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2873 case SpvOpAtomicStore
:
2874 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2875 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2876 nir_intrinsic_set_align(atomic
, 4, 0);
2877 atomic
->src
[src
++] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2878 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2879 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2880 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2883 case SpvOpAtomicExchange
:
2884 case SpvOpAtomicCompareExchange
:
2885 case SpvOpAtomicCompareExchangeWeak
:
2886 case SpvOpAtomicIIncrement
:
2887 case SpvOpAtomicIDecrement
:
2888 case SpvOpAtomicIAdd
:
2889 case SpvOpAtomicISub
:
2890 case SpvOpAtomicSMin
:
2891 case SpvOpAtomicUMin
:
2892 case SpvOpAtomicSMax
:
2893 case SpvOpAtomicUMax
:
2894 case SpvOpAtomicAnd
:
2896 case SpvOpAtomicXor
:
2897 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2898 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2899 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2900 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
2904 vtn_fail("Invalid SPIR-V atomic");
2907 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2908 const struct glsl_type
*deref_type
= deref
->type
;
2909 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
2910 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2911 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2914 case SpvOpAtomicLoad
:
2915 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2918 case SpvOpAtomicStore
:
2919 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2920 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2921 atomic
->src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2924 case SpvOpAtomicExchange
:
2925 case SpvOpAtomicCompareExchange
:
2926 case SpvOpAtomicCompareExchangeWeak
:
2927 case SpvOpAtomicIIncrement
:
2928 case SpvOpAtomicIDecrement
:
2929 case SpvOpAtomicIAdd
:
2930 case SpvOpAtomicISub
:
2931 case SpvOpAtomicSMin
:
2932 case SpvOpAtomicUMin
:
2933 case SpvOpAtomicSMax
:
2934 case SpvOpAtomicUMax
:
2935 case SpvOpAtomicAnd
:
2937 case SpvOpAtomicXor
:
2938 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
2942 vtn_fail("Invalid SPIR-V atomic");
2946 if (opcode
!= SpvOpAtomicStore
) {
2947 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2949 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
2950 glsl_get_vector_elements(type
->type
),
2951 glsl_get_bit_size(type
->type
), NULL
);
2953 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2954 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
2955 val
->ssa
->def
= &atomic
->dest
.ssa
;
2956 val
->ssa
->type
= type
->type
;
2959 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
2962 static nir_alu_instr
*
2963 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
2966 switch (num_components
) {
2967 case 1: op
= nir_op_imov
; break;
2968 case 2: op
= nir_op_vec2
; break;
2969 case 3: op
= nir_op_vec3
; break;
2970 case 4: op
= nir_op_vec4
; break;
2971 default: vtn_fail("bad vector size");
2974 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
2975 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
2977 vec
->dest
.write_mask
= (1 << num_components
) - 1;
2982 struct vtn_ssa_value
*
2983 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
2985 if (src
->transposed
)
2986 return src
->transposed
;
2988 struct vtn_ssa_value
*dest
=
2989 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
2991 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
2992 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
2993 glsl_get_bit_size(src
->type
));
2994 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2995 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
2996 vec
->src
[0].swizzle
[0] = i
;
2998 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
2999 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
3000 vec
->src
[j
].swizzle
[0] = i
;
3003 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3004 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
3007 dest
->transposed
= src
;
3013 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
3015 return nir_channel(&b
->nb
, src
, index
);
3019 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
3022 nir_alu_instr
*vec
= create_vec(b
, src
->num_components
,
3025 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
3027 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
3029 vec
->src
[i
].src
= nir_src_for_ssa(src
);
3030 vec
->src
[i
].swizzle
[0] = i
;
3034 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3036 return &vec
->dest
.dest
.ssa
;
3039 static nir_ssa_def
*
3040 nir_ieq_imm(nir_builder
*b
, nir_ssa_def
*x
, uint64_t i
)
3042 return nir_ieq(b
, x
, nir_imm_intN_t(b
, i
, x
->bit_size
));
3046 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
3049 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
3050 for (unsigned i
= 1; i
< src
->num_components
; i
++)
3051 dest
= nir_bcsel(&b
->nb
, nir_ieq_imm(&b
->nb
, index
, i
),
3052 vtn_vector_extract(b
, src
, i
), dest
);
3058 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
3059 nir_ssa_def
*insert
, nir_ssa_def
*index
)
3061 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
3062 for (unsigned i
= 1; i
< src
->num_components
; i
++)
3063 dest
= nir_bcsel(&b
->nb
, nir_ieq_imm(&b
->nb
, index
, i
),
3064 vtn_vector_insert(b
, src
, insert
, i
), dest
);
3069 static nir_ssa_def
*
3070 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
3071 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
3072 const uint32_t *indices
)
3074 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
3076 for (unsigned i
= 0; i
< num_components
; i
++) {
3077 uint32_t index
= indices
[i
];
3078 if (index
== 0xffffffff) {
3080 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
3081 } else if (index
< src0
->num_components
) {
3082 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
3083 vec
->src
[i
].swizzle
[0] = index
;
3085 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
3086 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
3090 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3092 return &vec
->dest
.dest
.ssa
;
3096 * Concatentates a number of vectors/scalars together to produce a vector
3098 static nir_ssa_def
*
3099 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
3100 unsigned num_srcs
, nir_ssa_def
**srcs
)
3102 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
3104 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3106 * "When constructing a vector, there must be at least two Constituent
3109 vtn_assert(num_srcs
>= 2);
3111 unsigned dest_idx
= 0;
3112 for (unsigned i
= 0; i
< num_srcs
; i
++) {
3113 nir_ssa_def
*src
= srcs
[i
];
3114 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
3115 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
3116 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
3117 vec
->src
[dest_idx
].swizzle
[0] = j
;
3122 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3124 * "When constructing a vector, the total number of components in all
3125 * the operands must equal the number of components in Result Type."
3127 vtn_assert(dest_idx
== num_components
);
3129 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3131 return &vec
->dest
.dest
.ssa
;
3134 static struct vtn_ssa_value
*
3135 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3137 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3138 dest
->type
= src
->type
;
3140 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3141 dest
->def
= src
->def
;
3143 unsigned elems
= glsl_get_length(src
->type
);
3145 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3146 for (unsigned i
= 0; i
< elems
; i
++)
3147 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3153 static struct vtn_ssa_value
*
3154 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3155 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3156 unsigned num_indices
)
3158 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3160 struct vtn_ssa_value
*cur
= dest
;
3162 for (i
= 0; i
< num_indices
- 1; i
++) {
3163 cur
= cur
->elems
[indices
[i
]];
3166 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3167 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3168 * the component granularity. In that case, the last index will be
3169 * the index to insert the scalar into the vector.
3172 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
3174 cur
->elems
[indices
[i
]] = insert
;
3180 static struct vtn_ssa_value
*
3181 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3182 const uint32_t *indices
, unsigned num_indices
)
3184 struct vtn_ssa_value
*cur
= src
;
3185 for (unsigned i
= 0; i
< num_indices
; i
++) {
3186 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3187 vtn_assert(i
== num_indices
- 1);
3188 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3189 * the component granularity. The last index will be the index of the
3190 * vector to extract.
3193 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
3194 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
3195 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
3198 cur
= cur
->elems
[indices
[i
]];
3206 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3207 const uint32_t *w
, unsigned count
)
3209 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
3210 const struct glsl_type
*type
=
3211 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
3212 val
->ssa
= vtn_create_ssa_value(b
, type
);
3215 case SpvOpVectorExtractDynamic
:
3216 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3217 vtn_ssa_value(b
, w
[4])->def
);
3220 case SpvOpVectorInsertDynamic
:
3221 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3222 vtn_ssa_value(b
, w
[4])->def
,
3223 vtn_ssa_value(b
, w
[5])->def
);
3226 case SpvOpVectorShuffle
:
3227 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
3228 vtn_ssa_value(b
, w
[3])->def
,
3229 vtn_ssa_value(b
, w
[4])->def
,
3233 case SpvOpCompositeConstruct
: {
3234 unsigned elems
= count
- 3;
3236 if (glsl_type_is_vector_or_scalar(type
)) {
3237 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3238 for (unsigned i
= 0; i
< elems
; i
++)
3239 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
3241 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
3244 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3245 for (unsigned i
= 0; i
< elems
; i
++)
3246 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3250 case SpvOpCompositeExtract
:
3251 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3255 case SpvOpCompositeInsert
:
3256 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3257 vtn_ssa_value(b
, w
[3]),
3261 case SpvOpCopyObject
:
3262 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3266 vtn_fail("unknown composite operation");
3271 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3273 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3274 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3278 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3279 SpvMemorySemanticsMask semantics
)
3281 static const SpvMemorySemanticsMask all_memory_semantics
=
3282 SpvMemorySemanticsUniformMemoryMask
|
3283 SpvMemorySemanticsWorkgroupMemoryMask
|
3284 SpvMemorySemanticsAtomicCounterMemoryMask
|
3285 SpvMemorySemanticsImageMemoryMask
;
3287 /* If we're not actually doing a memory barrier, bail */
3288 if (!(semantics
& all_memory_semantics
))
3291 /* GL and Vulkan don't have these */
3292 vtn_assert(scope
!= SpvScopeCrossDevice
);
3294 if (scope
== SpvScopeSubgroup
)
3295 return; /* Nothing to do here */
3297 if (scope
== SpvScopeWorkgroup
) {
3298 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3302 /* There's only two scopes thing left */
3303 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3305 if ((semantics
& all_memory_semantics
) == all_memory_semantics
) {
3306 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3310 /* Issue a bunch of more specific barriers */
3311 uint32_t bits
= semantics
;
3313 SpvMemorySemanticsMask semantic
= 1 << u_bit_scan(&bits
);
3315 case SpvMemorySemanticsUniformMemoryMask
:
3316 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3318 case SpvMemorySemanticsWorkgroupMemoryMask
:
3319 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3321 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3322 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3324 case SpvMemorySemanticsImageMemoryMask
:
3325 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3334 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3335 const uint32_t *w
, unsigned count
)
3338 case SpvOpEmitVertex
:
3339 case SpvOpEmitStreamVertex
:
3340 case SpvOpEndPrimitive
:
3341 case SpvOpEndStreamPrimitive
: {
3342 nir_intrinsic_op intrinsic_op
;
3344 case SpvOpEmitVertex
:
3345 case SpvOpEmitStreamVertex
:
3346 intrinsic_op
= nir_intrinsic_emit_vertex
;
3348 case SpvOpEndPrimitive
:
3349 case SpvOpEndStreamPrimitive
:
3350 intrinsic_op
= nir_intrinsic_end_primitive
;
3353 unreachable("Invalid opcode");
3356 nir_intrinsic_instr
*intrin
=
3357 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3360 case SpvOpEmitStreamVertex
:
3361 case SpvOpEndStreamPrimitive
: {
3362 unsigned stream
= vtn_constant_uint(b
, w
[1]);
3363 nir_intrinsic_set_stream_id(intrin
, stream
);
3371 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3375 case SpvOpMemoryBarrier
: {
3376 SpvScope scope
= vtn_constant_uint(b
, w
[1]);
3377 SpvMemorySemanticsMask semantics
= vtn_constant_uint(b
, w
[2]);
3378 vtn_emit_memory_barrier(b
, scope
, semantics
);
3382 case SpvOpControlBarrier
: {
3383 SpvScope execution_scope
= vtn_constant_uint(b
, w
[1]);
3384 if (execution_scope
== SpvScopeWorkgroup
)
3385 vtn_emit_barrier(b
, nir_intrinsic_barrier
);
3387 SpvScope memory_scope
= vtn_constant_uint(b
, w
[2]);
3388 SpvMemorySemanticsMask memory_semantics
= vtn_constant_uint(b
, w
[3]);
3389 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
3394 unreachable("unknown barrier instruction");
3399 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
3400 SpvExecutionMode mode
)
3403 case SpvExecutionModeInputPoints
:
3404 case SpvExecutionModeOutputPoints
:
3405 return 0; /* GL_POINTS */
3406 case SpvExecutionModeInputLines
:
3407 return 1; /* GL_LINES */
3408 case SpvExecutionModeInputLinesAdjacency
:
3409 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3410 case SpvExecutionModeTriangles
:
3411 return 4; /* GL_TRIANGLES */
3412 case SpvExecutionModeInputTrianglesAdjacency
:
3413 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3414 case SpvExecutionModeQuads
:
3415 return 7; /* GL_QUADS */
3416 case SpvExecutionModeIsolines
:
3417 return 0x8E7A; /* GL_ISOLINES */
3418 case SpvExecutionModeOutputLineStrip
:
3419 return 3; /* GL_LINE_STRIP */
3420 case SpvExecutionModeOutputTriangleStrip
:
3421 return 5; /* GL_TRIANGLE_STRIP */
3423 vtn_fail("Invalid primitive type");
3428 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
3429 SpvExecutionMode mode
)
3432 case SpvExecutionModeInputPoints
:
3434 case SpvExecutionModeInputLines
:
3436 case SpvExecutionModeInputLinesAdjacency
:
3438 case SpvExecutionModeTriangles
:
3440 case SpvExecutionModeInputTrianglesAdjacency
:
3443 vtn_fail("Invalid GS input mode");
3447 static gl_shader_stage
3448 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
3451 case SpvExecutionModelVertex
:
3452 return MESA_SHADER_VERTEX
;
3453 case SpvExecutionModelTessellationControl
:
3454 return MESA_SHADER_TESS_CTRL
;
3455 case SpvExecutionModelTessellationEvaluation
:
3456 return MESA_SHADER_TESS_EVAL
;
3457 case SpvExecutionModelGeometry
:
3458 return MESA_SHADER_GEOMETRY
;
3459 case SpvExecutionModelFragment
:
3460 return MESA_SHADER_FRAGMENT
;
3461 case SpvExecutionModelGLCompute
:
3462 return MESA_SHADER_COMPUTE
;
3463 case SpvExecutionModelKernel
:
3464 return MESA_SHADER_KERNEL
;
3466 vtn_fail("Unsupported execution model");
3470 #define spv_check_supported(name, cap) do { \
3471 if (!(b->options && b->options->caps.name)) \
3472 vtn_warn("Unsupported SPIR-V capability: %s", \
3473 spirv_capability_to_string(cap)); \
3478 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
3481 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
3482 /* Let this be a name label regardless */
3483 unsigned name_words
;
3484 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
3486 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
3487 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
3490 vtn_assert(b
->entry_point
== NULL
);
3491 b
->entry_point
= entry_point
;
3495 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3496 const uint32_t *w
, unsigned count
)
3503 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
3504 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
3505 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
3506 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
3507 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
3508 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
3511 uint32_t version
= w
[2];
3514 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
3516 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
3520 case SpvOpSourceExtension
:
3521 case SpvOpSourceContinued
:
3522 case SpvOpExtension
:
3523 case SpvOpModuleProcessed
:
3524 /* Unhandled, but these are for debug so that's ok. */
3527 case SpvOpCapability
: {
3528 SpvCapability cap
= w
[1];
3530 case SpvCapabilityMatrix
:
3531 case SpvCapabilityShader
:
3532 case SpvCapabilityGeometry
:
3533 case SpvCapabilityGeometryPointSize
:
3534 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
3535 case SpvCapabilitySampledImageArrayDynamicIndexing
:
3536 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
3537 case SpvCapabilityStorageImageArrayDynamicIndexing
:
3538 case SpvCapabilityImageRect
:
3539 case SpvCapabilitySampledRect
:
3540 case SpvCapabilitySampled1D
:
3541 case SpvCapabilityImage1D
:
3542 case SpvCapabilitySampledCubeArray
:
3543 case SpvCapabilityImageCubeArray
:
3544 case SpvCapabilitySampledBuffer
:
3545 case SpvCapabilityImageBuffer
:
3546 case SpvCapabilityImageQuery
:
3547 case SpvCapabilityDerivativeControl
:
3548 case SpvCapabilityInterpolationFunction
:
3549 case SpvCapabilityMultiViewport
:
3550 case SpvCapabilitySampleRateShading
:
3551 case SpvCapabilityClipDistance
:
3552 case SpvCapabilityCullDistance
:
3553 case SpvCapabilityInputAttachment
:
3554 case SpvCapabilityImageGatherExtended
:
3555 case SpvCapabilityStorageImageExtendedFormats
:
3558 case SpvCapabilityLinkage
:
3559 case SpvCapabilityVector16
:
3560 case SpvCapabilityFloat16Buffer
:
3561 case SpvCapabilityFloat16
:
3562 case SpvCapabilitySparseResidency
:
3563 vtn_warn("Unsupported SPIR-V capability: %s",
3564 spirv_capability_to_string(cap
));
3567 case SpvCapabilityMinLod
:
3568 spv_check_supported(min_lod
, cap
);
3571 case SpvCapabilityAtomicStorage
:
3572 spv_check_supported(atomic_storage
, cap
);
3575 case SpvCapabilityFloat64
:
3576 spv_check_supported(float64
, cap
);
3578 case SpvCapabilityInt64
:
3579 spv_check_supported(int64
, cap
);
3581 case SpvCapabilityInt16
:
3582 spv_check_supported(int16
, cap
);
3585 case SpvCapabilityTransformFeedback
:
3586 spv_check_supported(transform_feedback
, cap
);
3589 case SpvCapabilityGeometryStreams
:
3590 spv_check_supported(geometry_streams
, cap
);
3593 case SpvCapabilityInt64Atomics
:
3594 spv_check_supported(int64_atomics
, cap
);
3597 case SpvCapabilityInt8
:
3598 spv_check_supported(int8
, cap
);
3601 case SpvCapabilityStorageImageMultisample
:
3602 spv_check_supported(storage_image_ms
, cap
);
3605 case SpvCapabilityAddresses
:
3606 spv_check_supported(address
, cap
);
3609 case SpvCapabilityKernel
:
3610 spv_check_supported(kernel
, cap
);
3613 case SpvCapabilityImageBasic
:
3614 case SpvCapabilityImageReadWrite
:
3615 case SpvCapabilityImageMipmap
:
3616 case SpvCapabilityPipes
:
3617 case SpvCapabilityGroups
:
3618 case SpvCapabilityDeviceEnqueue
:
3619 case SpvCapabilityLiteralSampler
:
3620 case SpvCapabilityGenericPointer
:
3621 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
3622 spirv_capability_to_string(cap
));
3625 case SpvCapabilityImageMSArray
:
3626 spv_check_supported(image_ms_array
, cap
);
3629 case SpvCapabilityTessellation
:
3630 case SpvCapabilityTessellationPointSize
:
3631 spv_check_supported(tessellation
, cap
);
3634 case SpvCapabilityDrawParameters
:
3635 spv_check_supported(draw_parameters
, cap
);
3638 case SpvCapabilityStorageImageReadWithoutFormat
:
3639 spv_check_supported(image_read_without_format
, cap
);
3642 case SpvCapabilityStorageImageWriteWithoutFormat
:
3643 spv_check_supported(image_write_without_format
, cap
);
3646 case SpvCapabilityDeviceGroup
:
3647 spv_check_supported(device_group
, cap
);
3650 case SpvCapabilityMultiView
:
3651 spv_check_supported(multiview
, cap
);
3654 case SpvCapabilityGroupNonUniform
:
3655 spv_check_supported(subgroup_basic
, cap
);
3658 case SpvCapabilityGroupNonUniformVote
:
3659 spv_check_supported(subgroup_vote
, cap
);
3662 case SpvCapabilitySubgroupBallotKHR
:
3663 case SpvCapabilityGroupNonUniformBallot
:
3664 spv_check_supported(subgroup_ballot
, cap
);
3667 case SpvCapabilityGroupNonUniformShuffle
:
3668 case SpvCapabilityGroupNonUniformShuffleRelative
:
3669 spv_check_supported(subgroup_shuffle
, cap
);
3672 case SpvCapabilityGroupNonUniformQuad
:
3673 spv_check_supported(subgroup_quad
, cap
);
3676 case SpvCapabilityGroupNonUniformArithmetic
:
3677 case SpvCapabilityGroupNonUniformClustered
:
3678 spv_check_supported(subgroup_arithmetic
, cap
);
3681 case SpvCapabilityVariablePointersStorageBuffer
:
3682 case SpvCapabilityVariablePointers
:
3683 spv_check_supported(variable_pointers
, cap
);
3684 b
->variable_pointers
= true;
3687 case SpvCapabilityStorageUniformBufferBlock16
:
3688 case SpvCapabilityStorageUniform16
:
3689 case SpvCapabilityStoragePushConstant16
:
3690 case SpvCapabilityStorageInputOutput16
:
3691 spv_check_supported(storage_16bit
, cap
);
3694 case SpvCapabilityShaderViewportIndexLayerEXT
:
3695 spv_check_supported(shader_viewport_index_layer
, cap
);
3698 case SpvCapabilityStorageBuffer8BitAccess
:
3699 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
3700 case SpvCapabilityStoragePushConstant8
:
3701 spv_check_supported(storage_8bit
, cap
);
3704 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
3705 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
3706 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
3707 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
3710 case SpvCapabilityRuntimeDescriptorArrayEXT
:
3711 spv_check_supported(runtime_descriptor_array
, cap
);
3714 case SpvCapabilityStencilExportEXT
:
3715 spv_check_supported(stencil_export
, cap
);
3718 case SpvCapabilitySampleMaskPostDepthCoverage
:
3719 spv_check_supported(post_depth_coverage
, cap
);
3722 case SpvCapabilityPhysicalStorageBufferAddressesEXT
:
3723 spv_check_supported(physical_storage_buffer_address
, cap
);
3727 vtn_fail("Unhandled capability");
3732 case SpvOpExtInstImport
:
3733 vtn_handle_extension(b
, opcode
, w
, count
);
3736 case SpvOpMemoryModel
:
3738 case SpvAddressingModelPhysical32
:
3739 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
3740 "AddressingModelPhysical32 only supported for kernels");
3741 b
->shader
->info
.cs
.ptr_size
= 32;
3742 b
->physical_ptrs
= true;
3744 case SpvAddressingModelPhysical64
:
3745 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
3746 "AddressingModelPhysical64 only supported for kernels");
3747 b
->shader
->info
.cs
.ptr_size
= 64;
3748 b
->physical_ptrs
= true;
3750 case SpvAddressingModelLogical
:
3751 vtn_fail_if(b
->shader
->info
.stage
>= MESA_SHADER_STAGES
,
3752 "AddressingModelLogical only supported for shaders");
3753 b
->shader
->info
.cs
.ptr_size
= 0;
3754 b
->physical_ptrs
= false;
3756 case SpvAddressingModelPhysicalStorageBuffer64EXT
:
3757 vtn_fail_if(!b
->options
||
3758 !b
->options
->caps
.physical_storage_buffer_address
,
3759 "AddressingModelPhysicalStorageBuffer64EXT not supported");
3762 vtn_fail("Unknown addressing model");
3766 vtn_assert(w
[2] == SpvMemoryModelSimple
||
3767 w
[2] == SpvMemoryModelGLSL450
||
3768 w
[2] == SpvMemoryModelOpenCL
);
3771 case SpvOpEntryPoint
:
3772 vtn_handle_entry_point(b
, w
, count
);
3776 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
3777 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3781 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3784 case SpvOpMemberName
:
3788 case SpvOpExecutionMode
:
3789 case SpvOpExecutionModeId
:
3790 case SpvOpDecorationGroup
:
3792 case SpvOpMemberDecorate
:
3793 case SpvOpGroupDecorate
:
3794 case SpvOpGroupMemberDecorate
:
3795 case SpvOpDecorateStringGOOGLE
:
3796 case SpvOpMemberDecorateStringGOOGLE
:
3797 vtn_handle_decoration(b
, opcode
, w
, count
);
3801 return false; /* End of preamble */
3808 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
3809 const struct vtn_decoration
*mode
, void *data
)
3811 vtn_assert(b
->entry_point
== entry_point
);
3813 switch(mode
->exec_mode
) {
3814 case SpvExecutionModeOriginUpperLeft
:
3815 case SpvExecutionModeOriginLowerLeft
:
3816 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3817 b
->shader
->info
.fs
.origin_upper_left
=
3818 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
3821 case SpvExecutionModeEarlyFragmentTests
:
3822 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3823 b
->shader
->info
.fs
.early_fragment_tests
= true;
3826 case SpvExecutionModePostDepthCoverage
:
3827 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3828 b
->shader
->info
.fs
.post_depth_coverage
= true;
3831 case SpvExecutionModeInvocations
:
3832 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3833 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->literals
[0]);
3836 case SpvExecutionModeDepthReplacing
:
3837 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3838 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
3840 case SpvExecutionModeDepthGreater
:
3841 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3842 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
3844 case SpvExecutionModeDepthLess
:
3845 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3846 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
3848 case SpvExecutionModeDepthUnchanged
:
3849 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3850 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
3853 case SpvExecutionModeLocalSize
:
3854 vtn_assert(gl_shader_stage_is_compute(b
->shader
->info
.stage
));
3855 b
->shader
->info
.cs
.local_size
[0] = mode
->literals
[0];
3856 b
->shader
->info
.cs
.local_size
[1] = mode
->literals
[1];
3857 b
->shader
->info
.cs
.local_size
[2] = mode
->literals
[2];
3860 case SpvExecutionModeLocalSizeId
:
3861 b
->shader
->info
.cs
.local_size
[0] = vtn_constant_uint(b
, mode
->literals
[0]);
3862 b
->shader
->info
.cs
.local_size
[1] = vtn_constant_uint(b
, mode
->literals
[1]);
3863 b
->shader
->info
.cs
.local_size
[2] = vtn_constant_uint(b
, mode
->literals
[2]);
3866 case SpvExecutionModeLocalSizeHint
:
3867 case SpvExecutionModeLocalSizeHintId
:
3868 break; /* Nothing to do with this */
3870 case SpvExecutionModeOutputVertices
:
3871 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3872 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3873 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->literals
[0];
3875 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3876 b
->shader
->info
.gs
.vertices_out
= mode
->literals
[0];
3880 case SpvExecutionModeInputPoints
:
3881 case SpvExecutionModeInputLines
:
3882 case SpvExecutionModeInputLinesAdjacency
:
3883 case SpvExecutionModeTriangles
:
3884 case SpvExecutionModeInputTrianglesAdjacency
:
3885 case SpvExecutionModeQuads
:
3886 case SpvExecutionModeIsolines
:
3887 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3888 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3889 b
->shader
->info
.tess
.primitive_mode
=
3890 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3892 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3893 b
->shader
->info
.gs
.vertices_in
=
3894 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
3895 b
->shader
->info
.gs
.input_primitive
=
3896 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3900 case SpvExecutionModeOutputPoints
:
3901 case SpvExecutionModeOutputLineStrip
:
3902 case SpvExecutionModeOutputTriangleStrip
:
3903 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3904 b
->shader
->info
.gs
.output_primitive
=
3905 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3908 case SpvExecutionModeSpacingEqual
:
3909 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3910 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3911 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
3913 case SpvExecutionModeSpacingFractionalEven
:
3914 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3915 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3916 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
3918 case SpvExecutionModeSpacingFractionalOdd
:
3919 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3920 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3921 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
3923 case SpvExecutionModeVertexOrderCw
:
3924 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3925 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3926 b
->shader
->info
.tess
.ccw
= false;
3928 case SpvExecutionModeVertexOrderCcw
:
3929 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3930 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3931 b
->shader
->info
.tess
.ccw
= true;
3933 case SpvExecutionModePointMode
:
3934 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3935 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3936 b
->shader
->info
.tess
.point_mode
= true;
3939 case SpvExecutionModePixelCenterInteger
:
3940 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3941 b
->shader
->info
.fs
.pixel_center_integer
= true;
3944 case SpvExecutionModeXfb
:
3945 b
->shader
->info
.has_transform_feedback_varyings
= true;
3948 case SpvExecutionModeVecTypeHint
:
3951 case SpvExecutionModeContractionOff
:
3952 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
3953 vtn_warn("ExectionMode only allowed for CL-style kernels: %s",
3954 spirv_executionmode_to_string(mode
->exec_mode
));
3959 case SpvExecutionModeStencilRefReplacingEXT
:
3960 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3964 vtn_fail("Unhandled execution mode");
3969 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3970 const uint32_t *w
, unsigned count
)
3972 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
3976 case SpvOpSourceContinued
:
3977 case SpvOpSourceExtension
:
3978 case SpvOpExtension
:
3979 case SpvOpCapability
:
3980 case SpvOpExtInstImport
:
3981 case SpvOpMemoryModel
:
3982 case SpvOpEntryPoint
:
3983 case SpvOpExecutionMode
:
3986 case SpvOpMemberName
:
3987 case SpvOpDecorationGroup
:
3989 case SpvOpMemberDecorate
:
3990 case SpvOpGroupDecorate
:
3991 case SpvOpGroupMemberDecorate
:
3992 case SpvOpDecorateStringGOOGLE
:
3993 case SpvOpMemberDecorateStringGOOGLE
:
3994 vtn_fail("Invalid opcode types and variables section");
4000 case SpvOpTypeFloat
:
4001 case SpvOpTypeVector
:
4002 case SpvOpTypeMatrix
:
4003 case SpvOpTypeImage
:
4004 case SpvOpTypeSampler
:
4005 case SpvOpTypeSampledImage
:
4006 case SpvOpTypeArray
:
4007 case SpvOpTypeRuntimeArray
:
4008 case SpvOpTypeStruct
:
4009 case SpvOpTypeOpaque
:
4010 case SpvOpTypePointer
:
4011 case SpvOpTypeForwardPointer
:
4012 case SpvOpTypeFunction
:
4013 case SpvOpTypeEvent
:
4014 case SpvOpTypeDeviceEvent
:
4015 case SpvOpTypeReserveId
:
4016 case SpvOpTypeQueue
:
4018 vtn_handle_type(b
, opcode
, w
, count
);
4021 case SpvOpConstantTrue
:
4022 case SpvOpConstantFalse
:
4024 case SpvOpConstantComposite
:
4025 case SpvOpConstantSampler
:
4026 case SpvOpConstantNull
:
4027 case SpvOpSpecConstantTrue
:
4028 case SpvOpSpecConstantFalse
:
4029 case SpvOpSpecConstant
:
4030 case SpvOpSpecConstantComposite
:
4031 case SpvOpSpecConstantOp
:
4032 vtn_handle_constant(b
, opcode
, w
, count
);
4037 vtn_handle_variables(b
, opcode
, w
, count
);
4041 return false; /* End of preamble */
4048 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4049 const uint32_t *w
, unsigned count
)
4055 case SpvOpLoopMerge
:
4056 case SpvOpSelectionMerge
:
4057 /* This is handled by cfg pre-pass and walk_blocks */
4061 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
4062 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4067 vtn_handle_extension(b
, opcode
, w
, count
);
4073 case SpvOpCopyMemory
:
4074 case SpvOpCopyMemorySized
:
4075 case SpvOpAccessChain
:
4076 case SpvOpPtrAccessChain
:
4077 case SpvOpInBoundsAccessChain
:
4078 case SpvOpArrayLength
:
4079 case SpvOpConvertPtrToU
:
4080 case SpvOpConvertUToPtr
:
4081 vtn_handle_variables(b
, opcode
, w
, count
);
4084 case SpvOpFunctionCall
:
4085 vtn_handle_function_call(b
, opcode
, w
, count
);
4088 case SpvOpSampledImage
:
4090 case SpvOpImageSampleImplicitLod
:
4091 case SpvOpImageSampleExplicitLod
:
4092 case SpvOpImageSampleDrefImplicitLod
:
4093 case SpvOpImageSampleDrefExplicitLod
:
4094 case SpvOpImageSampleProjImplicitLod
:
4095 case SpvOpImageSampleProjExplicitLod
:
4096 case SpvOpImageSampleProjDrefImplicitLod
:
4097 case SpvOpImageSampleProjDrefExplicitLod
:
4098 case SpvOpImageFetch
:
4099 case SpvOpImageGather
:
4100 case SpvOpImageDrefGather
:
4101 case SpvOpImageQuerySizeLod
:
4102 case SpvOpImageQueryLod
:
4103 case SpvOpImageQueryLevels
:
4104 case SpvOpImageQuerySamples
:
4105 vtn_handle_texture(b
, opcode
, w
, count
);
4108 case SpvOpImageRead
:
4109 case SpvOpImageWrite
:
4110 case SpvOpImageTexelPointer
:
4111 vtn_handle_image(b
, opcode
, w
, count
);
4114 case SpvOpImageQuerySize
: {
4115 struct vtn_pointer
*image
=
4116 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
4117 if (glsl_type_is_image(image
->type
->type
)) {
4118 vtn_handle_image(b
, opcode
, w
, count
);
4120 vtn_assert(glsl_type_is_sampler(image
->type
->type
));
4121 vtn_handle_texture(b
, opcode
, w
, count
);
4126 case SpvOpAtomicLoad
:
4127 case SpvOpAtomicExchange
:
4128 case SpvOpAtomicCompareExchange
:
4129 case SpvOpAtomicCompareExchangeWeak
:
4130 case SpvOpAtomicIIncrement
:
4131 case SpvOpAtomicIDecrement
:
4132 case SpvOpAtomicIAdd
:
4133 case SpvOpAtomicISub
:
4134 case SpvOpAtomicSMin
:
4135 case SpvOpAtomicUMin
:
4136 case SpvOpAtomicSMax
:
4137 case SpvOpAtomicUMax
:
4138 case SpvOpAtomicAnd
:
4140 case SpvOpAtomicXor
: {
4141 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
4142 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4143 vtn_handle_image(b
, opcode
, w
, count
);
4145 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4146 vtn_handle_atomics(b
, opcode
, w
, count
);
4151 case SpvOpAtomicStore
: {
4152 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
4153 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4154 vtn_handle_image(b
, opcode
, w
, count
);
4156 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4157 vtn_handle_atomics(b
, opcode
, w
, count
);
4163 /* Handle OpSelect up-front here because it needs to be able to handle
4164 * pointers and not just regular vectors and scalars.
4166 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
4167 struct vtn_value
*sel_val
= vtn_untyped_value(b
, w
[3]);
4168 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
4169 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
4171 const struct glsl_type
*sel_type
;
4172 switch (res_val
->type
->base_type
) {
4173 case vtn_base_type_scalar
:
4174 sel_type
= glsl_bool_type();
4176 case vtn_base_type_vector
:
4177 sel_type
= glsl_vector_type(GLSL_TYPE_BOOL
, res_val
->type
->length
);
4179 case vtn_base_type_pointer
:
4180 /* We need to have actual storage for pointer types */
4181 vtn_fail_if(res_val
->type
->type
== NULL
,
4182 "Invalid pointer result type for OpSelect");
4183 sel_type
= glsl_bool_type();
4186 vtn_fail("Result type of OpSelect must be a scalar, vector, or pointer");
4189 if (unlikely(sel_val
->type
->type
!= sel_type
)) {
4190 if (sel_val
->type
->type
== glsl_bool_type()) {
4191 /* This case is illegal but some older versions of GLSLang produce
4192 * it. The GLSLang issue was fixed on March 30, 2017:
4194 * https://github.com/KhronosGroup/glslang/issues/809
4196 * Unfortunately, there are applications in the wild which are
4197 * shipping with this bug so it isn't nice to fail on them so we
4198 * throw a warning instead. It's not actually a problem for us as
4199 * nir_builder will just splat the condition out which is most
4200 * likely what the client wanted anyway.
4202 vtn_warn("Condition type of OpSelect must have the same number "
4203 "of components as Result Type");
4205 vtn_fail("Condition type of OpSelect must be a scalar or vector "
4206 "of Boolean type. It must have the same number of "
4207 "components as Result Type");
4211 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4212 obj2_val
->type
!= res_val
->type
,
4213 "Object types must match the result type in OpSelect");
4215 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4216 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, res_type
->type
);
4217 ssa
->def
= nir_bcsel(&b
->nb
, vtn_ssa_value(b
, w
[3])->def
,
4218 vtn_ssa_value(b
, w
[4])->def
,
4219 vtn_ssa_value(b
, w
[5])->def
);
4220 vtn_push_ssa(b
, w
[2], res_type
, ssa
);
4229 case SpvOpConvertFToU
:
4230 case SpvOpConvertFToS
:
4231 case SpvOpConvertSToF
:
4232 case SpvOpConvertUToF
:
4236 case SpvOpQuantizeToF16
:
4237 case SpvOpPtrCastToGeneric
:
4238 case SpvOpGenericCastToPtr
:
4244 case SpvOpSignBitSet
:
4245 case SpvOpLessOrGreater
:
4247 case SpvOpUnordered
:
4262 case SpvOpVectorTimesScalar
:
4264 case SpvOpIAddCarry
:
4265 case SpvOpISubBorrow
:
4266 case SpvOpUMulExtended
:
4267 case SpvOpSMulExtended
:
4268 case SpvOpShiftRightLogical
:
4269 case SpvOpShiftRightArithmetic
:
4270 case SpvOpShiftLeftLogical
:
4271 case SpvOpLogicalEqual
:
4272 case SpvOpLogicalNotEqual
:
4273 case SpvOpLogicalOr
:
4274 case SpvOpLogicalAnd
:
4275 case SpvOpLogicalNot
:
4276 case SpvOpBitwiseOr
:
4277 case SpvOpBitwiseXor
:
4278 case SpvOpBitwiseAnd
:
4280 case SpvOpFOrdEqual
:
4281 case SpvOpFUnordEqual
:
4282 case SpvOpINotEqual
:
4283 case SpvOpFOrdNotEqual
:
4284 case SpvOpFUnordNotEqual
:
4285 case SpvOpULessThan
:
4286 case SpvOpSLessThan
:
4287 case SpvOpFOrdLessThan
:
4288 case SpvOpFUnordLessThan
:
4289 case SpvOpUGreaterThan
:
4290 case SpvOpSGreaterThan
:
4291 case SpvOpFOrdGreaterThan
:
4292 case SpvOpFUnordGreaterThan
:
4293 case SpvOpULessThanEqual
:
4294 case SpvOpSLessThanEqual
:
4295 case SpvOpFOrdLessThanEqual
:
4296 case SpvOpFUnordLessThanEqual
:
4297 case SpvOpUGreaterThanEqual
:
4298 case SpvOpSGreaterThanEqual
:
4299 case SpvOpFOrdGreaterThanEqual
:
4300 case SpvOpFUnordGreaterThanEqual
:
4306 case SpvOpFwidthFine
:
4307 case SpvOpDPdxCoarse
:
4308 case SpvOpDPdyCoarse
:
4309 case SpvOpFwidthCoarse
:
4310 case SpvOpBitFieldInsert
:
4311 case SpvOpBitFieldSExtract
:
4312 case SpvOpBitFieldUExtract
:
4313 case SpvOpBitReverse
:
4315 case SpvOpTranspose
:
4316 case SpvOpOuterProduct
:
4317 case SpvOpMatrixTimesScalar
:
4318 case SpvOpVectorTimesMatrix
:
4319 case SpvOpMatrixTimesVector
:
4320 case SpvOpMatrixTimesMatrix
:
4321 vtn_handle_alu(b
, opcode
, w
, count
);
4324 case SpvOpVectorExtractDynamic
:
4325 case SpvOpVectorInsertDynamic
:
4326 case SpvOpVectorShuffle
:
4327 case SpvOpCompositeConstruct
:
4328 case SpvOpCompositeExtract
:
4329 case SpvOpCompositeInsert
:
4330 case SpvOpCopyObject
:
4331 vtn_handle_composite(b
, opcode
, w
, count
);
4334 case SpvOpEmitVertex
:
4335 case SpvOpEndPrimitive
:
4336 case SpvOpEmitStreamVertex
:
4337 case SpvOpEndStreamPrimitive
:
4338 case SpvOpControlBarrier
:
4339 case SpvOpMemoryBarrier
:
4340 vtn_handle_barrier(b
, opcode
, w
, count
);
4343 case SpvOpGroupNonUniformElect
:
4344 case SpvOpGroupNonUniformAll
:
4345 case SpvOpGroupNonUniformAny
:
4346 case SpvOpGroupNonUniformAllEqual
:
4347 case SpvOpGroupNonUniformBroadcast
:
4348 case SpvOpGroupNonUniformBroadcastFirst
:
4349 case SpvOpGroupNonUniformBallot
:
4350 case SpvOpGroupNonUniformInverseBallot
:
4351 case SpvOpGroupNonUniformBallotBitExtract
:
4352 case SpvOpGroupNonUniformBallotBitCount
:
4353 case SpvOpGroupNonUniformBallotFindLSB
:
4354 case SpvOpGroupNonUniformBallotFindMSB
:
4355 case SpvOpGroupNonUniformShuffle
:
4356 case SpvOpGroupNonUniformShuffleXor
:
4357 case SpvOpGroupNonUniformShuffleUp
:
4358 case SpvOpGroupNonUniformShuffleDown
:
4359 case SpvOpGroupNonUniformIAdd
:
4360 case SpvOpGroupNonUniformFAdd
:
4361 case SpvOpGroupNonUniformIMul
:
4362 case SpvOpGroupNonUniformFMul
:
4363 case SpvOpGroupNonUniformSMin
:
4364 case SpvOpGroupNonUniformUMin
:
4365 case SpvOpGroupNonUniformFMin
:
4366 case SpvOpGroupNonUniformSMax
:
4367 case SpvOpGroupNonUniformUMax
:
4368 case SpvOpGroupNonUniformFMax
:
4369 case SpvOpGroupNonUniformBitwiseAnd
:
4370 case SpvOpGroupNonUniformBitwiseOr
:
4371 case SpvOpGroupNonUniformBitwiseXor
:
4372 case SpvOpGroupNonUniformLogicalAnd
:
4373 case SpvOpGroupNonUniformLogicalOr
:
4374 case SpvOpGroupNonUniformLogicalXor
:
4375 case SpvOpGroupNonUniformQuadBroadcast
:
4376 case SpvOpGroupNonUniformQuadSwap
:
4377 vtn_handle_subgroup(b
, opcode
, w
, count
);
4381 vtn_fail("Unhandled opcode");
4388 vtn_create_builder(const uint32_t *words
, size_t word_count
,
4389 gl_shader_stage stage
, const char *entry_point_name
,
4390 const struct spirv_to_nir_options
*options
)
4392 /* Initialize the vtn_builder object */
4393 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
4395 b
->spirv_word_count
= word_count
;
4399 exec_list_make_empty(&b
->functions
);
4400 b
->entry_point_stage
= stage
;
4401 b
->entry_point_name
= entry_point_name
;
4402 b
->options
= options
;
4405 * Handle the SPIR-V header (first 5 dwords).
4406 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
4408 if (word_count
<= 5)
4411 if (words
[0] != SpvMagicNumber
) {
4412 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
4415 if (words
[1] < 0x10000) {
4416 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
4420 uint16_t generator_id
= words
[2] >> 16;
4421 uint16_t generator_version
= words
[2];
4423 /* The first GLSLang version bump actually 1.5 years after #179 was fixed
4424 * but this should at least let us shut the workaround off for modern
4425 * versions of GLSLang.
4427 b
->wa_glslang_179
= (generator_id
== 8 && generator_version
== 1);
4429 /* words[2] == generator magic */
4430 unsigned value_id_bound
= words
[3];
4431 if (words
[4] != 0) {
4432 vtn_err("words[4] was %u, want 0", words
[4]);
4436 b
->value_id_bound
= value_id_bound
;
4437 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
4446 spirv_to_nir(const uint32_t *words
, size_t word_count
,
4447 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
4448 gl_shader_stage stage
, const char *entry_point_name
,
4449 const struct spirv_to_nir_options
*options
,
4450 const nir_shader_compiler_options
*nir_options
)
4453 const uint32_t *word_end
= words
+ word_count
;
4455 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
4456 stage
, entry_point_name
,
4462 /* See also _vtn_fail() */
4463 if (setjmp(b
->fail_jump
)) {
4468 /* Skip the SPIR-V header, handled at vtn_create_builder */
4471 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
4473 /* Handle all the preamble instructions */
4474 words
= vtn_foreach_instruction(b
, words
, word_end
,
4475 vtn_handle_preamble_instruction
);
4477 if (b
->entry_point
== NULL
) {
4478 vtn_fail("Entry point not found");
4483 /* Set shader info defaults */
4484 b
->shader
->info
.gs
.invocations
= 1;
4486 b
->specializations
= spec
;
4487 b
->num_specializations
= num_spec
;
4489 /* Handle all variable, type, and constant instructions */
4490 words
= vtn_foreach_instruction(b
, words
, word_end
,
4491 vtn_handle_variable_or_type_instruction
);
4493 /* Parse execution modes */
4494 vtn_foreach_execution_mode(b
, b
->entry_point
,
4495 vtn_handle_execution_mode
, NULL
);
4497 if (b
->workgroup_size_builtin
) {
4498 vtn_assert(b
->workgroup_size_builtin
->type
->type
==
4499 glsl_vector_type(GLSL_TYPE_UINT
, 3));
4501 nir_const_value
*const_size
=
4502 &b
->workgroup_size_builtin
->constant
->values
[0];
4504 b
->shader
->info
.cs
.local_size
[0] = const_size
->u32
[0];
4505 b
->shader
->info
.cs
.local_size
[1] = const_size
->u32
[1];
4506 b
->shader
->info
.cs
.local_size
[2] = const_size
->u32
[2];
4509 /* Set types on all vtn_values */
4510 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
4512 vtn_build_cfg(b
, words
, word_end
);
4514 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4515 b
->entry_point
->func
->referenced
= true;
4520 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
4521 if (func
->referenced
&& !func
->emitted
) {
4522 b
->const_table
= _mesa_pointer_hash_table_create(b
);
4524 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
4530 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4531 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
4532 vtn_assert(entry_point
);
4534 entry_point
->is_entrypoint
= true;
4536 /* When multiple shader stages exist in the same SPIR-V module, we
4537 * generate input and output variables for every stage, in the same
4538 * NIR program. These dead variables can be invalid NIR. For example,
4539 * TCS outputs must be per-vertex arrays (or decorated 'patch'), while
4540 * VS output variables wouldn't be.
4542 * To ensure we have valid NIR, we eliminate any dead inputs and outputs
4543 * right away. In order to do so, we must lower any constant initializers
4544 * on outputs so nir_remove_dead_variables sees that they're written to.
4546 nir_lower_constant_initializers(b
->shader
, nir_var_shader_out
);
4547 nir_remove_dead_variables(b
->shader
,
4548 nir_var_shader_in
| nir_var_shader_out
);
4550 /* We sometimes generate bogus derefs that, while never used, give the
4551 * validator a bit of heartburn. Run dead code to get rid of them.
4553 nir_opt_dce(b
->shader
);
4555 /* Unparent the shader from the vtn_builder before we delete the builder */
4556 ralloc_steal(NULL
, b
->shader
);