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"
35 #include "util/format/u_format.h"
36 #include "util/u_math.h"
41 vtn_log(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
42 size_t spirv_offset
, const char *message
)
44 if (b
->options
->debug
.func
) {
45 b
->options
->debug
.func(b
->options
->debug
.private_data
,
46 level
, spirv_offset
, message
);
50 if (level
>= NIR_SPIRV_DEBUG_LEVEL_WARNING
)
51 fprintf(stderr
, "%s\n", message
);
56 vtn_logf(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
57 size_t spirv_offset
, const char *fmt
, ...)
63 msg
= ralloc_vasprintf(NULL
, fmt
, args
);
66 vtn_log(b
, level
, spirv_offset
, msg
);
72 vtn_log_err(struct vtn_builder
*b
,
73 enum nir_spirv_debug_level level
, const char *prefix
,
74 const char *file
, unsigned line
,
75 const char *fmt
, va_list args
)
79 msg
= ralloc_strdup(NULL
, prefix
);
82 ralloc_asprintf_append(&msg
, " In file %s:%u\n", file
, line
);
85 ralloc_asprintf_append(&msg
, " ");
87 ralloc_vasprintf_append(&msg
, fmt
, args
);
89 ralloc_asprintf_append(&msg
, "\n %zu bytes into the SPIR-V binary",
93 ralloc_asprintf_append(&msg
,
94 "\n in SPIR-V source file %s, line %d, col %d",
95 b
->file
, b
->line
, b
->col
);
98 vtn_log(b
, level
, b
->spirv_offset
, msg
);
104 vtn_dump_shader(struct vtn_builder
*b
, const char *path
, const char *prefix
)
109 int len
= snprintf(filename
, sizeof(filename
), "%s/%s-%d.spirv",
110 path
, prefix
, idx
++);
111 if (len
< 0 || len
>= sizeof(filename
))
114 FILE *f
= fopen(filename
, "w");
118 fwrite(b
->spirv
, sizeof(*b
->spirv
), b
->spirv_word_count
, f
);
121 vtn_info("SPIR-V shader dumped to %s", filename
);
125 _vtn_warn(struct vtn_builder
*b
, const char *file
, unsigned line
,
126 const char *fmt
, ...)
131 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_WARNING
, "SPIR-V WARNING:\n",
132 file
, line
, fmt
, args
);
137 _vtn_err(struct vtn_builder
*b
, const char *file
, unsigned line
,
138 const char *fmt
, ...)
143 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V ERROR:\n",
144 file
, line
, fmt
, args
);
149 _vtn_fail(struct vtn_builder
*b
, const char *file
, unsigned line
,
150 const char *fmt
, ...)
155 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V parsing FAILED:\n",
156 file
, line
, fmt
, args
);
159 const char *dump_path
= getenv("MESA_SPIRV_FAIL_DUMP_PATH");
161 vtn_dump_shader(b
, dump_path
, "fail");
163 longjmp(b
->fail_jump
, 1);
166 static struct vtn_ssa_value
*
167 vtn_undef_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
169 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
172 if (glsl_type_is_vector_or_scalar(type
)) {
173 unsigned num_components
= glsl_get_vector_elements(val
->type
);
174 unsigned bit_size
= glsl_get_bit_size(val
->type
);
175 val
->def
= nir_ssa_undef(&b
->nb
, num_components
, bit_size
);
177 unsigned elems
= glsl_get_length(val
->type
);
178 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
179 if (glsl_type_is_array_or_matrix(type
)) {
180 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
181 for (unsigned i
= 0; i
< elems
; i
++)
182 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
184 vtn_assert(glsl_type_is_struct_or_ifc(type
));
185 for (unsigned i
= 0; i
< elems
; i
++) {
186 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
187 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
195 static struct vtn_ssa_value
*
196 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
197 const struct glsl_type
*type
)
199 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
204 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
207 if (glsl_type_is_vector_or_scalar(type
)) {
208 unsigned num_components
= glsl_get_vector_elements(val
->type
);
209 unsigned bit_size
= glsl_get_bit_size(type
);
210 nir_load_const_instr
*load
=
211 nir_load_const_instr_create(b
->shader
, num_components
, bit_size
);
213 memcpy(load
->value
, constant
->values
,
214 sizeof(nir_const_value
) * num_components
);
216 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
217 val
->def
= &load
->def
;
219 unsigned elems
= glsl_get_length(val
->type
);
220 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
221 if (glsl_type_is_array_or_matrix(type
)) {
222 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
223 for (unsigned i
= 0; i
< elems
; i
++) {
224 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
228 vtn_assert(glsl_type_is_struct_or_ifc(type
));
229 for (unsigned i
= 0; i
< elems
; i
++) {
230 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
231 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
240 struct vtn_ssa_value
*
241 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
243 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
244 switch (val
->value_type
) {
245 case vtn_value_type_undef
:
246 return vtn_undef_ssa_value(b
, val
->type
->type
);
248 case vtn_value_type_constant
:
249 return vtn_const_ssa_value(b
, val
->constant
, val
->type
->type
);
251 case vtn_value_type_ssa
:
254 case vtn_value_type_pointer
:
255 vtn_assert(val
->pointer
->ptr_type
&& val
->pointer
->ptr_type
->type
);
256 struct vtn_ssa_value
*ssa
=
257 vtn_create_ssa_value(b
, val
->pointer
->ptr_type
->type
);
258 ssa
->def
= vtn_pointer_to_ssa(b
, val
->pointer
);
262 vtn_fail("Invalid type for an SSA value");
267 vtn_push_ssa_value(struct vtn_builder
*b
, uint32_t value_id
,
268 struct vtn_ssa_value
*ssa
)
270 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
272 struct vtn_value
*val
;
273 if (type
->base_type
== vtn_base_type_pointer
) {
274 val
= vtn_push_pointer(b
, value_id
, vtn_pointer_from_ssa(b
, ssa
->def
, type
));
276 val
= vtn_push_value(b
, value_id
, vtn_value_type_ssa
);
284 vtn_get_nir_ssa(struct vtn_builder
*b
, uint32_t value_id
)
286 struct vtn_ssa_value
*ssa
= vtn_ssa_value(b
, value_id
);
287 vtn_fail_if(!glsl_type_is_vector_or_scalar(ssa
->type
),
288 "Expected a vector or scalar type");
293 vtn_push_nir_ssa(struct vtn_builder
*b
, uint32_t value_id
, nir_ssa_def
*def
)
295 /* Types for all SPIR-V SSA values are set as part of a pre-pass so the
296 * type will be valid by the time we get here.
298 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
299 vtn_fail_if(def
->num_components
!= glsl_get_vector_elements(type
->type
) ||
300 def
->bit_size
!= glsl_get_bit_size(type
->type
),
301 "Mismatch between NIR and SPIR-V type.");
302 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, type
->type
);
304 return vtn_push_ssa_value(b
, value_id
, ssa
);
308 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
309 unsigned word_count
, unsigned *words_used
)
311 char *dup
= ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
313 /* Ammount of space taken by the string (including the null) */
314 unsigned len
= strlen(dup
) + 1;
315 *words_used
= DIV_ROUND_UP(len
, sizeof(*words
));
321 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
322 const uint32_t *end
, vtn_instruction_handler handler
)
328 const uint32_t *w
= start
;
330 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
331 unsigned count
= w
[0] >> SpvWordCountShift
;
332 vtn_assert(count
>= 1 && w
+ count
<= end
);
334 b
->spirv_offset
= (uint8_t *)w
- (uint8_t *)b
->spirv
;
338 break; /* Do nothing */
341 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
353 if (!handler(b
, opcode
, w
, count
))
371 vtn_handle_non_semantic_instruction(struct vtn_builder
*b
, SpvOp ext_opcode
,
372 const uint32_t *w
, unsigned count
)
379 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
380 const uint32_t *w
, unsigned count
)
382 const char *ext
= (const char *)&w
[2];
384 case SpvOpExtInstImport
: {
385 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
386 if (strcmp(ext
, "GLSL.std.450") == 0) {
387 val
->ext_handler
= vtn_handle_glsl450_instruction
;
388 } else if ((strcmp(ext
, "SPV_AMD_gcn_shader") == 0)
389 && (b
->options
&& b
->options
->caps
.amd_gcn_shader
)) {
390 val
->ext_handler
= vtn_handle_amd_gcn_shader_instruction
;
391 } else if ((strcmp(ext
, "SPV_AMD_shader_ballot") == 0)
392 && (b
->options
&& b
->options
->caps
.amd_shader_ballot
)) {
393 val
->ext_handler
= vtn_handle_amd_shader_ballot_instruction
;
394 } else if ((strcmp(ext
, "SPV_AMD_shader_trinary_minmax") == 0)
395 && (b
->options
&& b
->options
->caps
.amd_trinary_minmax
)) {
396 val
->ext_handler
= vtn_handle_amd_shader_trinary_minmax_instruction
;
397 } else if ((strcmp(ext
, "SPV_AMD_shader_explicit_vertex_parameter") == 0)
398 && (b
->options
&& b
->options
->caps
.amd_shader_explicit_vertex_parameter
)) {
399 val
->ext_handler
= vtn_handle_amd_shader_explicit_vertex_parameter_instruction
;
400 } else if (strcmp(ext
, "OpenCL.std") == 0) {
401 val
->ext_handler
= vtn_handle_opencl_instruction
;
402 } else if (strstr(ext
, "NonSemantic.") == ext
) {
403 val
->ext_handler
= vtn_handle_non_semantic_instruction
;
405 vtn_fail("Unsupported extension: %s", ext
);
411 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
412 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
418 vtn_fail_with_opcode("Unhandled opcode", opcode
);
423 _foreach_decoration_helper(struct vtn_builder
*b
,
424 struct vtn_value
*base_value
,
426 struct vtn_value
*value
,
427 vtn_decoration_foreach_cb cb
, void *data
)
429 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
431 if (dec
->scope
== VTN_DEC_DECORATION
) {
432 member
= parent_member
;
433 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
434 vtn_fail_if(value
->value_type
!= vtn_value_type_type
||
435 value
->type
->base_type
!= vtn_base_type_struct
,
436 "OpMemberDecorate and OpGroupMemberDecorate are only "
437 "allowed on OpTypeStruct");
438 /* This means we haven't recursed yet */
439 assert(value
== base_value
);
441 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
443 vtn_fail_if(member
>= base_value
->type
->length
,
444 "OpMemberDecorate specifies member %d but the "
445 "OpTypeStruct has only %u members",
446 member
, base_value
->type
->length
);
448 /* Not a decoration */
449 assert(dec
->scope
== VTN_DEC_EXECUTION_MODE
);
454 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
455 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
458 cb(b
, base_value
, member
, dec
, data
);
463 /** Iterates (recursively if needed) over all of the decorations on a value
465 * This function iterates over all of the decorations applied to a given
466 * value. If it encounters a decoration group, it recurses into the group
467 * and iterates over all of those decorations as well.
470 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
471 vtn_decoration_foreach_cb cb
, void *data
)
473 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
477 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
478 vtn_execution_mode_foreach_cb cb
, void *data
)
480 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
481 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
484 assert(dec
->group
== NULL
);
485 cb(b
, value
, dec
, data
);
490 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
491 const uint32_t *w
, unsigned count
)
493 const uint32_t *w_end
= w
+ count
;
494 const uint32_t target
= w
[1];
498 case SpvOpDecorationGroup
:
499 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
503 case SpvOpDecorateId
:
504 case SpvOpMemberDecorate
:
505 case SpvOpDecorateString
:
506 case SpvOpMemberDecorateString
:
507 case SpvOpExecutionMode
:
508 case SpvOpExecutionModeId
: {
509 struct vtn_value
*val
= vtn_untyped_value(b
, target
);
511 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
514 case SpvOpDecorateId
:
515 case SpvOpDecorateString
:
516 dec
->scope
= VTN_DEC_DECORATION
;
518 case SpvOpMemberDecorate
:
519 case SpvOpMemberDecorateString
:
520 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
521 vtn_fail_if(dec
->scope
< VTN_DEC_STRUCT_MEMBER0
, /* overflow */
522 "Member argument of OpMemberDecorate too large");
524 case SpvOpExecutionMode
:
525 case SpvOpExecutionModeId
:
526 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
529 unreachable("Invalid decoration opcode");
531 dec
->decoration
= *(w
++);
534 /* Link into the list */
535 dec
->next
= val
->decoration
;
536 val
->decoration
= dec
;
540 case SpvOpGroupMemberDecorate
:
541 case SpvOpGroupDecorate
: {
542 struct vtn_value
*group
=
543 vtn_value(b
, target
, vtn_value_type_decoration_group
);
545 for (; w
< w_end
; w
++) {
546 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
547 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
550 if (opcode
== SpvOpGroupDecorate
) {
551 dec
->scope
= VTN_DEC_DECORATION
;
553 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
554 vtn_fail_if(dec
->scope
< 0, /* Check for overflow */
555 "Member argument of OpGroupMemberDecorate too large");
558 /* Link into the list */
559 dec
->next
= val
->decoration
;
560 val
->decoration
= dec
;
566 unreachable("Unhandled opcode");
570 struct member_decoration_ctx
{
572 struct glsl_struct_field
*fields
;
573 struct vtn_type
*type
;
577 * Returns true if the given type contains a struct decorated Block or
581 vtn_type_contains_block(struct vtn_builder
*b
, struct vtn_type
*type
)
583 switch (type
->base_type
) {
584 case vtn_base_type_array
:
585 return vtn_type_contains_block(b
, type
->array_element
);
586 case vtn_base_type_struct
:
587 if (type
->block
|| type
->buffer_block
)
589 for (unsigned i
= 0; i
< type
->length
; i
++) {
590 if (vtn_type_contains_block(b
, type
->members
[i
]))
599 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
600 * OpStore, or OpCopyMemory between them without breaking anything.
601 * Technically, the SPIR-V rules require the exact same type ID but this lets
602 * us internally be a bit looser.
605 vtn_types_compatible(struct vtn_builder
*b
,
606 struct vtn_type
*t1
, struct vtn_type
*t2
)
608 if (t1
->id
== t2
->id
)
611 if (t1
->base_type
!= t2
->base_type
)
614 switch (t1
->base_type
) {
615 case vtn_base_type_void
:
616 case vtn_base_type_scalar
:
617 case vtn_base_type_vector
:
618 case vtn_base_type_matrix
:
619 case vtn_base_type_image
:
620 case vtn_base_type_sampler
:
621 case vtn_base_type_sampled_image
:
622 return t1
->type
== t2
->type
;
624 case vtn_base_type_array
:
625 return t1
->length
== t2
->length
&&
626 vtn_types_compatible(b
, t1
->array_element
, t2
->array_element
);
628 case vtn_base_type_pointer
:
629 return vtn_types_compatible(b
, t1
->deref
, t2
->deref
);
631 case vtn_base_type_struct
:
632 if (t1
->length
!= t2
->length
)
635 for (unsigned i
= 0; i
< t1
->length
; i
++) {
636 if (!vtn_types_compatible(b
, t1
->members
[i
], t2
->members
[i
]))
641 case vtn_base_type_function
:
642 /* This case shouldn't get hit since you can't copy around function
643 * types. Just require them to be identical.
648 vtn_fail("Invalid base type");
652 vtn_type_without_array(struct vtn_type
*type
)
654 while (type
->base_type
== vtn_base_type_array
)
655 type
= type
->array_element
;
659 /* does a shallow copy of a vtn_type */
661 static struct vtn_type
*
662 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
664 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
667 switch (src
->base_type
) {
668 case vtn_base_type_void
:
669 case vtn_base_type_scalar
:
670 case vtn_base_type_vector
:
671 case vtn_base_type_matrix
:
672 case vtn_base_type_array
:
673 case vtn_base_type_pointer
:
674 case vtn_base_type_image
:
675 case vtn_base_type_sampler
:
676 case vtn_base_type_sampled_image
:
677 /* Nothing more to do */
680 case vtn_base_type_struct
:
681 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
682 memcpy(dest
->members
, src
->members
,
683 src
->length
* sizeof(src
->members
[0]));
685 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
686 memcpy(dest
->offsets
, src
->offsets
,
687 src
->length
* sizeof(src
->offsets
[0]));
690 case vtn_base_type_function
:
691 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
692 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
699 static struct vtn_type
*
700 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
702 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
703 type
= type
->members
[member
];
705 /* We may have an array of matrices.... Oh, joy! */
706 while (glsl_type_is_array(type
->type
)) {
707 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
708 type
= type
->array_element
;
711 vtn_assert(glsl_type_is_matrix(type
->type
));
717 vtn_handle_access_qualifier(struct vtn_builder
*b
, struct vtn_type
*type
,
718 int member
, enum gl_access_qualifier access
)
720 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
721 type
= type
->members
[member
];
723 type
->access
|= access
;
727 array_stride_decoration_cb(struct vtn_builder
*b
,
728 struct vtn_value
*val
, int member
,
729 const struct vtn_decoration
*dec
, void *void_ctx
)
731 struct vtn_type
*type
= val
->type
;
733 if (dec
->decoration
== SpvDecorationArrayStride
) {
734 if (vtn_type_contains_block(b
, type
)) {
735 vtn_warn("The ArrayStride decoration cannot be applied to an array "
736 "type which contains a structure type decorated Block "
738 /* Ignore the decoration */
740 vtn_fail_if(dec
->operands
[0] == 0, "ArrayStride must be non-zero");
741 type
->stride
= dec
->operands
[0];
747 struct_member_decoration_cb(struct vtn_builder
*b
,
748 UNUSED
struct vtn_value
*val
, int member
,
749 const struct vtn_decoration
*dec
, void *void_ctx
)
751 struct member_decoration_ctx
*ctx
= void_ctx
;
756 assert(member
< ctx
->num_fields
);
758 switch (dec
->decoration
) {
759 case SpvDecorationRelaxedPrecision
:
760 case SpvDecorationUniform
:
761 case SpvDecorationUniformId
:
762 break; /* FIXME: Do nothing with this for now. */
763 case SpvDecorationNonWritable
:
764 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_WRITEABLE
);
766 case SpvDecorationNonReadable
:
767 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_READABLE
);
769 case SpvDecorationVolatile
:
770 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_VOLATILE
);
772 case SpvDecorationCoherent
:
773 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_COHERENT
);
775 case SpvDecorationNoPerspective
:
776 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
778 case SpvDecorationFlat
:
779 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
781 case SpvDecorationExplicitInterpAMD
:
782 ctx
->fields
[member
].interpolation
= INTERP_MODE_EXPLICIT
;
784 case SpvDecorationCentroid
:
785 ctx
->fields
[member
].centroid
= true;
787 case SpvDecorationSample
:
788 ctx
->fields
[member
].sample
= true;
790 case SpvDecorationStream
:
791 /* This is handled later by var_decoration_cb in vtn_variables.c */
793 case SpvDecorationLocation
:
794 ctx
->fields
[member
].location
= dec
->operands
[0];
796 case SpvDecorationComponent
:
797 break; /* FIXME: What should we do with these? */
798 case SpvDecorationBuiltIn
:
799 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
800 ctx
->type
->members
[member
]->is_builtin
= true;
801 ctx
->type
->members
[member
]->builtin
= dec
->operands
[0];
802 ctx
->type
->builtin_block
= true;
804 case SpvDecorationOffset
:
805 ctx
->type
->offsets
[member
] = dec
->operands
[0];
806 ctx
->fields
[member
].offset
= dec
->operands
[0];
808 case SpvDecorationMatrixStride
:
809 /* Handled as a second pass */
811 case SpvDecorationColMajor
:
812 break; /* Nothing to do here. Column-major is the default. */
813 case SpvDecorationRowMajor
:
814 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
817 case SpvDecorationPatch
:
820 case SpvDecorationSpecId
:
821 case SpvDecorationBlock
:
822 case SpvDecorationBufferBlock
:
823 case SpvDecorationArrayStride
:
824 case SpvDecorationGLSLShared
:
825 case SpvDecorationGLSLPacked
:
826 case SpvDecorationInvariant
:
827 case SpvDecorationRestrict
:
828 case SpvDecorationAliased
:
829 case SpvDecorationConstant
:
830 case SpvDecorationIndex
:
831 case SpvDecorationBinding
:
832 case SpvDecorationDescriptorSet
:
833 case SpvDecorationLinkageAttributes
:
834 case SpvDecorationNoContraction
:
835 case SpvDecorationInputAttachmentIndex
:
836 vtn_warn("Decoration not allowed on struct members: %s",
837 spirv_decoration_to_string(dec
->decoration
));
840 case SpvDecorationXfbBuffer
:
841 case SpvDecorationXfbStride
:
842 /* This is handled later by var_decoration_cb in vtn_variables.c */
845 case SpvDecorationCPacked
:
846 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
847 vtn_warn("Decoration only allowed for CL-style kernels: %s",
848 spirv_decoration_to_string(dec
->decoration
));
850 ctx
->type
->packed
= true;
853 case SpvDecorationSaturatedConversion
:
854 case SpvDecorationFuncParamAttr
:
855 case SpvDecorationFPRoundingMode
:
856 case SpvDecorationFPFastMathMode
:
857 case SpvDecorationAlignment
:
858 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
859 vtn_warn("Decoration only allowed for CL-style kernels: %s",
860 spirv_decoration_to_string(dec
->decoration
));
864 case SpvDecorationUserSemantic
:
865 case SpvDecorationUserTypeGOOGLE
:
866 /* User semantic decorations can safely be ignored by the driver. */
870 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
874 /** Chases the array type all the way down to the tail and rewrites the
875 * glsl_types to be based off the tail's glsl_type.
878 vtn_array_type_rewrite_glsl_type(struct vtn_type
*type
)
880 if (type
->base_type
!= vtn_base_type_array
)
883 vtn_array_type_rewrite_glsl_type(type
->array_element
);
885 type
->type
= glsl_array_type(type
->array_element
->type
,
886 type
->length
, type
->stride
);
889 /* Matrix strides are handled as a separate pass because we need to know
890 * whether the matrix is row-major or not first.
893 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
894 UNUSED
struct vtn_value
*val
, int member
,
895 const struct vtn_decoration
*dec
,
898 if (dec
->decoration
!= SpvDecorationMatrixStride
)
901 vtn_fail_if(member
< 0,
902 "The MatrixStride decoration is only allowed on members "
904 vtn_fail_if(dec
->operands
[0] == 0, "MatrixStride must be non-zero");
906 struct member_decoration_ctx
*ctx
= void_ctx
;
908 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
909 if (mat_type
->row_major
) {
910 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
911 mat_type
->stride
= mat_type
->array_element
->stride
;
912 mat_type
->array_element
->stride
= dec
->operands
[0];
914 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
915 dec
->operands
[0], true);
916 mat_type
->array_element
->type
= glsl_get_column_type(mat_type
->type
);
918 vtn_assert(mat_type
->array_element
->stride
> 0);
919 mat_type
->stride
= dec
->operands
[0];
921 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
922 dec
->operands
[0], false);
925 /* Now that we've replaced the glsl_type with a properly strided matrix
926 * type, rewrite the member type so that it's an array of the proper kind
929 vtn_array_type_rewrite_glsl_type(ctx
->type
->members
[member
]);
930 ctx
->fields
[member
].type
= ctx
->type
->members
[member
]->type
;
934 struct_block_decoration_cb(struct vtn_builder
*b
,
935 struct vtn_value
*val
, int member
,
936 const struct vtn_decoration
*dec
, void *ctx
)
941 struct vtn_type
*type
= val
->type
;
942 if (dec
->decoration
== SpvDecorationBlock
)
944 else if (dec
->decoration
== SpvDecorationBufferBlock
)
945 type
->buffer_block
= true;
949 type_decoration_cb(struct vtn_builder
*b
,
950 struct vtn_value
*val
, int member
,
951 const struct vtn_decoration
*dec
, UNUSED
void *ctx
)
953 struct vtn_type
*type
= val
->type
;
956 /* This should have been handled by OpTypeStruct */
957 assert(val
->type
->base_type
== vtn_base_type_struct
);
958 assert(member
>= 0 && member
< val
->type
->length
);
962 switch (dec
->decoration
) {
963 case SpvDecorationArrayStride
:
964 vtn_assert(type
->base_type
== vtn_base_type_array
||
965 type
->base_type
== vtn_base_type_pointer
);
967 case SpvDecorationBlock
:
968 vtn_assert(type
->base_type
== vtn_base_type_struct
);
969 vtn_assert(type
->block
);
971 case SpvDecorationBufferBlock
:
972 vtn_assert(type
->base_type
== vtn_base_type_struct
);
973 vtn_assert(type
->buffer_block
);
975 case SpvDecorationGLSLShared
:
976 case SpvDecorationGLSLPacked
:
977 /* Ignore these, since we get explicit offsets anyways */
980 case SpvDecorationRowMajor
:
981 case SpvDecorationColMajor
:
982 case SpvDecorationMatrixStride
:
983 case SpvDecorationBuiltIn
:
984 case SpvDecorationNoPerspective
:
985 case SpvDecorationFlat
:
986 case SpvDecorationPatch
:
987 case SpvDecorationCentroid
:
988 case SpvDecorationSample
:
989 case SpvDecorationExplicitInterpAMD
:
990 case SpvDecorationVolatile
:
991 case SpvDecorationCoherent
:
992 case SpvDecorationNonWritable
:
993 case SpvDecorationNonReadable
:
994 case SpvDecorationUniform
:
995 case SpvDecorationUniformId
:
996 case SpvDecorationLocation
:
997 case SpvDecorationComponent
:
998 case SpvDecorationOffset
:
999 case SpvDecorationXfbBuffer
:
1000 case SpvDecorationXfbStride
:
1001 case SpvDecorationUserSemantic
:
1002 vtn_warn("Decoration only allowed for struct members: %s",
1003 spirv_decoration_to_string(dec
->decoration
));
1006 case SpvDecorationStream
:
1007 /* We don't need to do anything here, as stream is filled up when
1008 * aplying the decoration to a variable, just check that if it is not a
1009 * struct member, it should be a struct.
1011 vtn_assert(type
->base_type
== vtn_base_type_struct
);
1014 case SpvDecorationRelaxedPrecision
:
1015 case SpvDecorationSpecId
:
1016 case SpvDecorationInvariant
:
1017 case SpvDecorationRestrict
:
1018 case SpvDecorationAliased
:
1019 case SpvDecorationConstant
:
1020 case SpvDecorationIndex
:
1021 case SpvDecorationBinding
:
1022 case SpvDecorationDescriptorSet
:
1023 case SpvDecorationLinkageAttributes
:
1024 case SpvDecorationNoContraction
:
1025 case SpvDecorationInputAttachmentIndex
:
1026 vtn_warn("Decoration not allowed on types: %s",
1027 spirv_decoration_to_string(dec
->decoration
));
1030 case SpvDecorationCPacked
:
1031 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
1032 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1033 spirv_decoration_to_string(dec
->decoration
));
1035 type
->packed
= true;
1038 case SpvDecorationSaturatedConversion
:
1039 case SpvDecorationFuncParamAttr
:
1040 case SpvDecorationFPRoundingMode
:
1041 case SpvDecorationFPFastMathMode
:
1042 case SpvDecorationAlignment
:
1043 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1044 spirv_decoration_to_string(dec
->decoration
));
1047 case SpvDecorationUserTypeGOOGLE
:
1048 /* User semantic decorations can safely be ignored by the driver. */
1052 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1057 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
1060 case SpvImageFormatUnknown
: return PIPE_FORMAT_NONE
;
1061 case SpvImageFormatRgba32f
: return PIPE_FORMAT_R32G32B32A32_FLOAT
;
1062 case SpvImageFormatRgba16f
: return PIPE_FORMAT_R16G16B16A16_FLOAT
;
1063 case SpvImageFormatR32f
: return PIPE_FORMAT_R32_FLOAT
;
1064 case SpvImageFormatRgba8
: return PIPE_FORMAT_R8G8B8A8_UNORM
;
1065 case SpvImageFormatRgba8Snorm
: return PIPE_FORMAT_R8G8B8A8_SNORM
;
1066 case SpvImageFormatRg32f
: return PIPE_FORMAT_R32G32_FLOAT
;
1067 case SpvImageFormatRg16f
: return PIPE_FORMAT_R16G16_FLOAT
;
1068 case SpvImageFormatR11fG11fB10f
: return PIPE_FORMAT_R11G11B10_FLOAT
;
1069 case SpvImageFormatR16f
: return PIPE_FORMAT_R16_FLOAT
;
1070 case SpvImageFormatRgba16
: return PIPE_FORMAT_R16G16B16A16_UNORM
;
1071 case SpvImageFormatRgb10A2
: return PIPE_FORMAT_R10G10B10A2_UNORM
;
1072 case SpvImageFormatRg16
: return PIPE_FORMAT_R16G16_UNORM
;
1073 case SpvImageFormatRg8
: return PIPE_FORMAT_R8G8_UNORM
;
1074 case SpvImageFormatR16
: return PIPE_FORMAT_R16_UNORM
;
1075 case SpvImageFormatR8
: return PIPE_FORMAT_R8_UNORM
;
1076 case SpvImageFormatRgba16Snorm
: return PIPE_FORMAT_R16G16B16A16_SNORM
;
1077 case SpvImageFormatRg16Snorm
: return PIPE_FORMAT_R16G16_SNORM
;
1078 case SpvImageFormatRg8Snorm
: return PIPE_FORMAT_R8G8_SNORM
;
1079 case SpvImageFormatR16Snorm
: return PIPE_FORMAT_R16_SNORM
;
1080 case SpvImageFormatR8Snorm
: return PIPE_FORMAT_R8_SNORM
;
1081 case SpvImageFormatRgba32i
: return PIPE_FORMAT_R32G32B32A32_SINT
;
1082 case SpvImageFormatRgba16i
: return PIPE_FORMAT_R16G16B16A16_SINT
;
1083 case SpvImageFormatRgba8i
: return PIPE_FORMAT_R8G8B8A8_SINT
;
1084 case SpvImageFormatR32i
: return PIPE_FORMAT_R32_SINT
;
1085 case SpvImageFormatRg32i
: return PIPE_FORMAT_R32G32_SINT
;
1086 case SpvImageFormatRg16i
: return PIPE_FORMAT_R16G16_SINT
;
1087 case SpvImageFormatRg8i
: return PIPE_FORMAT_R8G8_SINT
;
1088 case SpvImageFormatR16i
: return PIPE_FORMAT_R16_SINT
;
1089 case SpvImageFormatR8i
: return PIPE_FORMAT_R8_SINT
;
1090 case SpvImageFormatRgba32ui
: return PIPE_FORMAT_R32G32B32A32_UINT
;
1091 case SpvImageFormatRgba16ui
: return PIPE_FORMAT_R16G16B16A16_UINT
;
1092 case SpvImageFormatRgba8ui
: return PIPE_FORMAT_R8G8B8A8_UINT
;
1093 case SpvImageFormatR32ui
: return PIPE_FORMAT_R32_UINT
;
1094 case SpvImageFormatRgb10a2ui
: return PIPE_FORMAT_R10G10B10A2_UINT
;
1095 case SpvImageFormatRg32ui
: return PIPE_FORMAT_R32G32_UINT
;
1096 case SpvImageFormatRg16ui
: return PIPE_FORMAT_R16G16_UINT
;
1097 case SpvImageFormatRg8ui
: return PIPE_FORMAT_R8G8_UINT
;
1098 case SpvImageFormatR16ui
: return PIPE_FORMAT_R16_UINT
;
1099 case SpvImageFormatR8ui
: return PIPE_FORMAT_R8_UINT
;
1101 vtn_fail("Invalid image format: %s (%u)",
1102 spirv_imageformat_to_string(format
), format
);
1107 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
1108 const uint32_t *w
, unsigned count
)
1110 struct vtn_value
*val
= NULL
;
1112 /* In order to properly handle forward declarations, we have to defer
1113 * allocation for pointer types.
1115 if (opcode
!= SpvOpTypePointer
&& opcode
!= SpvOpTypeForwardPointer
) {
1116 val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1117 vtn_fail_if(val
->type
!= NULL
,
1118 "Only pointers can have forward declarations");
1119 val
->type
= rzalloc(b
, struct vtn_type
);
1120 val
->type
->id
= w
[1];
1125 val
->type
->base_type
= vtn_base_type_void
;
1126 val
->type
->type
= glsl_void_type();
1129 val
->type
->base_type
= vtn_base_type_scalar
;
1130 val
->type
->type
= glsl_bool_type();
1131 val
->type
->length
= 1;
1133 case SpvOpTypeInt
: {
1134 int bit_size
= w
[2];
1135 const bool signedness
= w
[3];
1136 val
->type
->base_type
= vtn_base_type_scalar
;
1139 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1142 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1145 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1148 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1151 vtn_fail("Invalid int bit size: %u", bit_size
);
1153 val
->type
->length
= 1;
1157 case SpvOpTypeFloat
: {
1158 int bit_size
= w
[2];
1159 val
->type
->base_type
= vtn_base_type_scalar
;
1162 val
->type
->type
= glsl_float16_t_type();
1165 val
->type
->type
= glsl_float_type();
1168 val
->type
->type
= glsl_double_type();
1171 vtn_fail("Invalid float bit size: %u", bit_size
);
1173 val
->type
->length
= 1;
1177 case SpvOpTypeVector
: {
1178 struct vtn_type
*base
= vtn_get_type(b
, w
[2]);
1179 unsigned elems
= w
[3];
1181 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1182 "Base type for OpTypeVector must be a scalar");
1183 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1184 "Invalid component count for OpTypeVector");
1186 val
->type
->base_type
= vtn_base_type_vector
;
1187 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1188 val
->type
->length
= elems
;
1189 val
->type
->stride
= glsl_type_is_boolean(val
->type
->type
)
1190 ? 4 : glsl_get_bit_size(base
->type
) / 8;
1191 val
->type
->array_element
= base
;
1195 case SpvOpTypeMatrix
: {
1196 struct vtn_type
*base
= vtn_get_type(b
, w
[2]);
1197 unsigned columns
= w
[3];
1199 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1200 "Base type for OpTypeMatrix must be a vector");
1201 vtn_fail_if(columns
< 2 || columns
> 4,
1202 "Invalid column count for OpTypeMatrix");
1204 val
->type
->base_type
= vtn_base_type_matrix
;
1205 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1206 glsl_get_vector_elements(base
->type
),
1208 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1209 "Unsupported base type for OpTypeMatrix");
1210 assert(!glsl_type_is_error(val
->type
->type
));
1211 val
->type
->length
= columns
;
1212 val
->type
->array_element
= base
;
1213 val
->type
->row_major
= false;
1214 val
->type
->stride
= 0;
1218 case SpvOpTypeRuntimeArray
:
1219 case SpvOpTypeArray
: {
1220 struct vtn_type
*array_element
= vtn_get_type(b
, w
[2]);
1222 if (opcode
== SpvOpTypeRuntimeArray
) {
1223 /* A length of 0 is used to denote unsized arrays */
1224 val
->type
->length
= 0;
1226 val
->type
->length
= vtn_constant_uint(b
, w
[3]);
1229 val
->type
->base_type
= vtn_base_type_array
;
1230 val
->type
->array_element
= array_element
;
1231 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
1232 val
->type
->stride
= glsl_get_cl_size(array_element
->type
);
1234 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1235 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
,
1240 case SpvOpTypeStruct
: {
1241 unsigned num_fields
= count
- 2;
1242 val
->type
->base_type
= vtn_base_type_struct
;
1243 val
->type
->length
= num_fields
;
1244 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1245 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1246 val
->type
->packed
= false;
1248 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1249 for (unsigned i
= 0; i
< num_fields
; i
++) {
1250 val
->type
->members
[i
] = vtn_get_type(b
, w
[i
+ 2]);
1251 fields
[i
] = (struct glsl_struct_field
) {
1252 .type
= val
->type
->members
[i
]->type
,
1253 .name
= ralloc_asprintf(b
, "field%d", i
),
1259 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1260 unsigned offset
= 0;
1261 for (unsigned i
= 0; i
< num_fields
; i
++) {
1262 offset
= align(offset
, glsl_get_cl_alignment(fields
[i
].type
));
1263 fields
[i
].offset
= offset
;
1264 offset
+= glsl_get_cl_size(fields
[i
].type
);
1268 struct member_decoration_ctx ctx
= {
1269 .num_fields
= num_fields
,
1274 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1275 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1277 vtn_foreach_decoration(b
, val
, struct_block_decoration_cb
, NULL
);
1279 const char *name
= val
->name
;
1281 if (val
->type
->block
|| val
->type
->buffer_block
) {
1282 /* Packing will be ignored since types coming from SPIR-V are
1283 * explicitly laid out.
1285 val
->type
->type
= glsl_interface_type(fields
, num_fields
,
1286 /* packing */ 0, false,
1287 name
? name
: "block");
1289 val
->type
->type
= glsl_struct_type(fields
, num_fields
,
1290 name
? name
: "struct", false);
1295 case SpvOpTypeFunction
: {
1296 val
->type
->base_type
= vtn_base_type_function
;
1297 val
->type
->type
= NULL
;
1299 val
->type
->return_type
= vtn_get_type(b
, w
[2]);
1301 const unsigned num_params
= count
- 3;
1302 val
->type
->length
= num_params
;
1303 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1304 for (unsigned i
= 0; i
< count
- 3; i
++) {
1305 val
->type
->params
[i
] = vtn_get_type(b
, w
[i
+ 3]);
1310 case SpvOpTypePointer
:
1311 case SpvOpTypeForwardPointer
: {
1312 /* We can't blindly push the value because it might be a forward
1315 val
= vtn_untyped_value(b
, w
[1]);
1317 SpvStorageClass storage_class
= w
[2];
1319 if (val
->value_type
== vtn_value_type_invalid
) {
1320 val
->value_type
= vtn_value_type_type
;
1321 val
->type
= rzalloc(b
, struct vtn_type
);
1322 val
->type
->id
= w
[1];
1323 val
->type
->base_type
= vtn_base_type_pointer
;
1324 val
->type
->storage_class
= storage_class
;
1326 /* These can actually be stored to nir_variables and used as SSA
1327 * values so they need a real glsl_type.
1329 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1330 b
, storage_class
, NULL
, NULL
);
1331 val
->type
->type
= nir_address_format_to_glsl_type(
1332 vtn_mode_to_address_format(b
, mode
));
1334 vtn_fail_if(val
->type
->storage_class
!= storage_class
,
1335 "The storage classes of an OpTypePointer and any "
1336 "OpTypeForwardPointers that provide forward "
1337 "declarations of it must match.");
1340 if (opcode
== SpvOpTypePointer
) {
1341 vtn_fail_if(val
->type
->deref
!= NULL
,
1342 "While OpTypeForwardPointer can be used to provide a "
1343 "forward declaration of a pointer, OpTypePointer can "
1344 "only be used once for a given id.");
1346 val
->type
->deref
= vtn_get_type(b
, w
[3]);
1348 /* Only certain storage classes use ArrayStride. The others (in
1349 * particular Workgroup) are expected to be laid out by the driver.
1351 switch (storage_class
) {
1352 case SpvStorageClassUniform
:
1353 case SpvStorageClassPushConstant
:
1354 case SpvStorageClassStorageBuffer
:
1355 case SpvStorageClassPhysicalStorageBuffer
:
1356 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1359 /* Nothing to do. */
1363 if (b
->physical_ptrs
) {
1364 switch (storage_class
) {
1365 case SpvStorageClassFunction
:
1366 case SpvStorageClassWorkgroup
:
1367 case SpvStorageClassCrossWorkgroup
:
1368 case SpvStorageClassUniformConstant
:
1369 val
->type
->stride
= align(glsl_get_cl_size(val
->type
->deref
->type
),
1370 glsl_get_cl_alignment(val
->type
->deref
->type
));
1380 case SpvOpTypeImage
: {
1381 val
->type
->base_type
= vtn_base_type_image
;
1383 const struct vtn_type
*sampled_type
= vtn_get_type(b
, w
[2]);
1384 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1385 glsl_get_bit_size(sampled_type
->type
) != 32,
1386 "Sampled type of OpTypeImage must be a 32-bit scalar");
1388 enum glsl_sampler_dim dim
;
1389 switch ((SpvDim
)w
[3]) {
1390 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1391 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1392 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1393 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1394 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1395 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1396 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1398 vtn_fail("Invalid SPIR-V image dimensionality: %s (%u)",
1399 spirv_dim_to_string((SpvDim
)w
[3]), w
[3]);
1402 /* w[4]: as per Vulkan spec "Validation Rules within a Module",
1403 * The “Depth” operand of OpTypeImage is ignored.
1405 bool is_array
= w
[5];
1406 bool multisampled
= w
[6];
1407 unsigned sampled
= w
[7];
1408 SpvImageFormat format
= w
[8];
1411 val
->type
->access_qualifier
= w
[9];
1413 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1416 if (dim
== GLSL_SAMPLER_DIM_2D
)
1417 dim
= GLSL_SAMPLER_DIM_MS
;
1418 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1419 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1421 vtn_fail("Unsupported multisampled image type");
1424 val
->type
->image_format
= translate_image_format(b
, format
);
1426 enum glsl_base_type sampled_base_type
=
1427 glsl_get_base_type(sampled_type
->type
);
1429 val
->type
->sampled
= true;
1430 val
->type
->type
= glsl_sampler_type(dim
, false, is_array
,
1432 } else if (sampled
== 2) {
1433 val
->type
->sampled
= false;
1434 val
->type
->type
= glsl_image_type(dim
, is_array
, sampled_base_type
);
1436 vtn_fail("We need to know if the image will be sampled");
1441 case SpvOpTypeSampledImage
:
1442 val
->type
->base_type
= vtn_base_type_sampled_image
;
1443 val
->type
->image
= vtn_get_type(b
, w
[2]);
1444 val
->type
->type
= val
->type
->image
->type
;
1447 case SpvOpTypeSampler
:
1448 /* The actual sampler type here doesn't really matter. It gets
1449 * thrown away the moment you combine it with an image. What really
1450 * matters is that it's a sampler type as opposed to an integer type
1451 * so the backend knows what to do.
1453 val
->type
->base_type
= vtn_base_type_sampler
;
1454 val
->type
->type
= glsl_bare_sampler_type();
1457 case SpvOpTypeOpaque
:
1458 case SpvOpTypeEvent
:
1459 case SpvOpTypeDeviceEvent
:
1460 case SpvOpTypeReserveId
:
1461 case SpvOpTypeQueue
:
1464 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1467 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1469 if (val
->type
->base_type
== vtn_base_type_struct
&&
1470 (val
->type
->block
|| val
->type
->buffer_block
)) {
1471 for (unsigned i
= 0; i
< val
->type
->length
; i
++) {
1472 vtn_fail_if(vtn_type_contains_block(b
, val
->type
->members
[i
]),
1473 "Block and BufferBlock decorations cannot decorate a "
1474 "structure type that is nested at any level inside "
1475 "another structure type decorated with Block or "
1481 static nir_constant
*
1482 vtn_null_constant(struct vtn_builder
*b
, struct vtn_type
*type
)
1484 nir_constant
*c
= rzalloc(b
, nir_constant
);
1486 switch (type
->base_type
) {
1487 case vtn_base_type_scalar
:
1488 case vtn_base_type_vector
:
1489 /* Nothing to do here. It's already initialized to zero */
1492 case vtn_base_type_pointer
: {
1493 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1494 b
, type
->storage_class
, type
->deref
, NULL
);
1495 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
1497 const nir_const_value
*null_value
= nir_address_format_null_value(addr_format
);
1498 memcpy(c
->values
, null_value
,
1499 sizeof(nir_const_value
) * nir_address_format_num_components(addr_format
));
1503 case vtn_base_type_void
:
1504 case vtn_base_type_image
:
1505 case vtn_base_type_sampler
:
1506 case vtn_base_type_sampled_image
:
1507 case vtn_base_type_function
:
1508 /* For those we have to return something but it doesn't matter what. */
1511 case vtn_base_type_matrix
:
1512 case vtn_base_type_array
:
1513 vtn_assert(type
->length
> 0);
1514 c
->num_elements
= type
->length
;
1515 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1517 c
->elements
[0] = vtn_null_constant(b
, type
->array_element
);
1518 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1519 c
->elements
[i
] = c
->elements
[0];
1522 case vtn_base_type_struct
:
1523 c
->num_elements
= type
->length
;
1524 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1525 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
1526 c
->elements
[i
] = vtn_null_constant(b
, type
->members
[i
]);
1530 vtn_fail("Invalid type for null constant");
1537 spec_constant_decoration_cb(struct vtn_builder
*b
, UNUSED
struct vtn_value
*val
,
1538 ASSERTED
int member
,
1539 const struct vtn_decoration
*dec
, void *data
)
1541 vtn_assert(member
== -1);
1542 if (dec
->decoration
!= SpvDecorationSpecId
)
1545 nir_const_value
*value
= data
;
1546 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1547 if (b
->specializations
[i
].id
== dec
->operands
[0]) {
1548 *value
= b
->specializations
[i
].value
;
1555 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1556 struct vtn_value
*val
,
1557 ASSERTED
int member
,
1558 const struct vtn_decoration
*dec
,
1561 vtn_assert(member
== -1);
1562 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1563 dec
->operands
[0] != SpvBuiltInWorkgroupSize
)
1566 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1567 b
->workgroup_size_builtin
= val
;
1571 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1572 const uint32_t *w
, unsigned count
)
1574 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1575 val
->constant
= rzalloc(b
, nir_constant
);
1577 case SpvOpConstantTrue
:
1578 case SpvOpConstantFalse
:
1579 case SpvOpSpecConstantTrue
:
1580 case SpvOpSpecConstantFalse
: {
1581 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1582 "Result type of %s must be OpTypeBool",
1583 spirv_op_to_string(opcode
));
1585 bool bval
= (opcode
== SpvOpConstantTrue
||
1586 opcode
== SpvOpSpecConstantTrue
);
1588 nir_const_value u32val
= nir_const_value_for_uint(bval
, 32);
1590 if (opcode
== SpvOpSpecConstantTrue
||
1591 opcode
== SpvOpSpecConstantFalse
)
1592 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &u32val
);
1594 val
->constant
->values
[0].b
= u32val
.u32
!= 0;
1599 case SpvOpSpecConstant
: {
1600 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1601 "Result type of %s must be a scalar",
1602 spirv_op_to_string(opcode
));
1603 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1606 val
->constant
->values
[0].u64
= vtn_u64_literal(&w
[3]);
1609 val
->constant
->values
[0].u32
= w
[3];
1612 val
->constant
->values
[0].u16
= w
[3];
1615 val
->constant
->values
[0].u8
= w
[3];
1618 vtn_fail("Unsupported SpvOpConstant bit size: %u", bit_size
);
1621 if (opcode
== SpvOpSpecConstant
)
1622 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
,
1623 &val
->constant
->values
[0]);
1627 case SpvOpSpecConstantComposite
:
1628 case SpvOpConstantComposite
: {
1629 unsigned elem_count
= count
- 3;
1630 vtn_fail_if(elem_count
!= val
->type
->length
,
1631 "%s has %u constituents, expected %u",
1632 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1634 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1635 for (unsigned i
= 0; i
< elem_count
; i
++) {
1636 struct vtn_value
*val
= vtn_untyped_value(b
, w
[i
+ 3]);
1638 if (val
->value_type
== vtn_value_type_constant
) {
1639 elems
[i
] = val
->constant
;
1641 vtn_fail_if(val
->value_type
!= vtn_value_type_undef
,
1642 "only constants or undefs allowed for "
1643 "SpvOpConstantComposite");
1644 /* to make it easier, just insert a NULL constant for now */
1645 elems
[i
] = vtn_null_constant(b
, val
->type
);
1649 switch (val
->type
->base_type
) {
1650 case vtn_base_type_vector
: {
1651 assert(glsl_type_is_vector(val
->type
->type
));
1652 for (unsigned i
= 0; i
< elem_count
; i
++)
1653 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1657 case vtn_base_type_matrix
:
1658 case vtn_base_type_struct
:
1659 case vtn_base_type_array
:
1660 ralloc_steal(val
->constant
, elems
);
1661 val
->constant
->num_elements
= elem_count
;
1662 val
->constant
->elements
= elems
;
1666 vtn_fail("Result type of %s must be a composite type",
1667 spirv_op_to_string(opcode
));
1672 case SpvOpSpecConstantOp
: {
1673 nir_const_value u32op
= nir_const_value_for_uint(w
[3], 32);
1674 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &u32op
);
1675 SpvOp opcode
= u32op
.u32
;
1677 case SpvOpVectorShuffle
: {
1678 struct vtn_value
*v0
= &b
->values
[w
[4]];
1679 struct vtn_value
*v1
= &b
->values
[w
[5]];
1681 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1682 v0
->value_type
== vtn_value_type_undef
);
1683 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1684 v1
->value_type
== vtn_value_type_undef
);
1686 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1687 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1689 vtn_assert(len0
+ len1
< 16);
1691 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1692 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1693 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1695 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1696 (void)bit_size0
; (void)bit_size1
;
1698 nir_const_value undef
= { .u64
= 0xdeadbeefdeadbeef };
1699 nir_const_value combined
[NIR_MAX_VEC_COMPONENTS
* 2];
1701 if (v0
->value_type
== vtn_value_type_constant
) {
1702 for (unsigned i
= 0; i
< len0
; i
++)
1703 combined
[i
] = v0
->constant
->values
[i
];
1705 if (v1
->value_type
== vtn_value_type_constant
) {
1706 for (unsigned i
= 0; i
< len1
; i
++)
1707 combined
[len0
+ i
] = v1
->constant
->values
[i
];
1710 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1711 uint32_t comp
= w
[i
+ 6];
1712 if (comp
== (uint32_t)-1) {
1713 /* If component is not used, set the value to a known constant
1714 * to detect if it is wrongly used.
1716 val
->constant
->values
[j
] = undef
;
1718 vtn_fail_if(comp
>= len0
+ len1
,
1719 "All Component literals must either be FFFFFFFF "
1720 "or in [0, N - 1] (inclusive).");
1721 val
->constant
->values
[j
] = combined
[comp
];
1727 case SpvOpCompositeExtract
:
1728 case SpvOpCompositeInsert
: {
1729 struct vtn_value
*comp
;
1730 unsigned deref_start
;
1731 struct nir_constant
**c
;
1732 if (opcode
== SpvOpCompositeExtract
) {
1733 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1735 c
= &comp
->constant
;
1737 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1739 val
->constant
= nir_constant_clone(comp
->constant
,
1745 const struct vtn_type
*type
= comp
->type
;
1746 for (unsigned i
= deref_start
; i
< count
; i
++) {
1747 vtn_fail_if(w
[i
] > type
->length
,
1748 "%uth index of %s is %u but the type has only "
1749 "%u elements", i
- deref_start
,
1750 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1752 switch (type
->base_type
) {
1753 case vtn_base_type_vector
:
1755 type
= type
->array_element
;
1758 case vtn_base_type_matrix
:
1759 case vtn_base_type_array
:
1760 c
= &(*c
)->elements
[w
[i
]];
1761 type
= type
->array_element
;
1764 case vtn_base_type_struct
:
1765 c
= &(*c
)->elements
[w
[i
]];
1766 type
= type
->members
[w
[i
]];
1770 vtn_fail("%s must only index into composite types",
1771 spirv_op_to_string(opcode
));
1775 if (opcode
== SpvOpCompositeExtract
) {
1779 unsigned num_components
= type
->length
;
1780 for (unsigned i
= 0; i
< num_components
; i
++)
1781 val
->constant
->values
[i
] = (*c
)->values
[elem
+ i
];
1784 struct vtn_value
*insert
=
1785 vtn_value(b
, w
[4], vtn_value_type_constant
);
1786 vtn_assert(insert
->type
== type
);
1788 *c
= insert
->constant
;
1790 unsigned num_components
= type
->length
;
1791 for (unsigned i
= 0; i
< num_components
; i
++)
1792 (*c
)->values
[elem
+ i
] = insert
->constant
->values
[i
];
1800 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
1801 nir_alu_type src_alu_type
= dst_alu_type
;
1802 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
1805 vtn_assert(count
<= 7);
1811 /* We have a source in a conversion */
1813 nir_get_nir_type_for_glsl_type(vtn_get_value_type(b
, w
[4])->type
);
1814 /* We use the bitsize of the conversion source to evaluate the opcode later */
1815 bit_size
= glsl_get_bit_size(vtn_get_value_type(b
, w
[4])->type
);
1818 bit_size
= glsl_get_bit_size(val
->type
->type
);
1821 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
1822 nir_alu_type_get_type_size(src_alu_type
),
1823 nir_alu_type_get_type_size(dst_alu_type
));
1824 nir_const_value src
[3][NIR_MAX_VEC_COMPONENTS
];
1826 for (unsigned i
= 0; i
< count
- 4; i
++) {
1827 struct vtn_value
*src_val
=
1828 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
);
1830 /* If this is an unsized source, pull the bit size from the
1831 * source; otherwise, we'll use the bit size from the destination.
1833 if (!nir_alu_type_get_type_size(nir_op_infos
[op
].input_types
[i
]))
1834 bit_size
= glsl_get_bit_size(src_val
->type
->type
);
1836 unsigned src_comps
= nir_op_infos
[op
].input_sizes
[i
] ?
1837 nir_op_infos
[op
].input_sizes
[i
] :
1840 unsigned j
= swap
? 1 - i
: i
;
1841 for (unsigned c
= 0; c
< src_comps
; c
++)
1842 src
[j
][c
] = src_val
->constant
->values
[c
];
1845 /* fix up fixed size sources */
1852 for (unsigned i
= 0; i
< num_components
; ++i
) {
1854 case 64: src
[1][i
].u32
= src
[1][i
].u64
; break;
1855 case 16: src
[1][i
].u32
= src
[1][i
].u16
; break;
1856 case 8: src
[1][i
].u32
= src
[1][i
].u8
; break;
1865 nir_const_value
*srcs
[3] = {
1866 src
[0], src
[1], src
[2],
1868 nir_eval_const_opcode(op
, val
->constant
->values
,
1869 num_components
, bit_size
, srcs
,
1870 b
->shader
->info
.float_controls_execution_mode
);
1877 case SpvOpConstantNull
:
1878 val
->constant
= vtn_null_constant(b
, val
->type
);
1881 case SpvOpConstantSampler
:
1882 vtn_fail("OpConstantSampler requires Kernel Capability");
1886 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1889 /* Now that we have the value, update the workgroup size if needed */
1890 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1893 SpvMemorySemanticsMask
1894 vtn_storage_class_to_memory_semantics(SpvStorageClass sc
)
1897 case SpvStorageClassStorageBuffer
:
1898 case SpvStorageClassPhysicalStorageBuffer
:
1899 return SpvMemorySemanticsUniformMemoryMask
;
1900 case SpvStorageClassWorkgroup
:
1901 return SpvMemorySemanticsWorkgroupMemoryMask
;
1903 return SpvMemorySemanticsMaskNone
;
1908 vtn_split_barrier_semantics(struct vtn_builder
*b
,
1909 SpvMemorySemanticsMask semantics
,
1910 SpvMemorySemanticsMask
*before
,
1911 SpvMemorySemanticsMask
*after
)
1913 /* For memory semantics embedded in operations, we split them into up to
1914 * two barriers, to be added before and after the operation. This is less
1915 * strict than if we propagated until the final backend stage, but still
1916 * result in correct execution.
1918 * A further improvement could be pipe this information (and use!) into the
1919 * next compiler layers, at the expense of making the handling of barriers
1923 *before
= SpvMemorySemanticsMaskNone
;
1924 *after
= SpvMemorySemanticsMaskNone
;
1926 SpvMemorySemanticsMask order_semantics
=
1927 semantics
& (SpvMemorySemanticsAcquireMask
|
1928 SpvMemorySemanticsReleaseMask
|
1929 SpvMemorySemanticsAcquireReleaseMask
|
1930 SpvMemorySemanticsSequentiallyConsistentMask
);
1932 if (util_bitcount(order_semantics
) > 1) {
1933 /* Old GLSLang versions incorrectly set all the ordering bits. This was
1934 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
1935 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
1937 vtn_warn("Multiple memory ordering semantics specified, "
1938 "assuming AcquireRelease.");
1939 order_semantics
= SpvMemorySemanticsAcquireReleaseMask
;
1942 const SpvMemorySemanticsMask av_vis_semantics
=
1943 semantics
& (SpvMemorySemanticsMakeAvailableMask
|
1944 SpvMemorySemanticsMakeVisibleMask
);
1946 const SpvMemorySemanticsMask storage_semantics
=
1947 semantics
& (SpvMemorySemanticsUniformMemoryMask
|
1948 SpvMemorySemanticsSubgroupMemoryMask
|
1949 SpvMemorySemanticsWorkgroupMemoryMask
|
1950 SpvMemorySemanticsCrossWorkgroupMemoryMask
|
1951 SpvMemorySemanticsAtomicCounterMemoryMask
|
1952 SpvMemorySemanticsImageMemoryMask
|
1953 SpvMemorySemanticsOutputMemoryMask
);
1955 const SpvMemorySemanticsMask other_semantics
=
1956 semantics
& ~(order_semantics
| av_vis_semantics
| storage_semantics
);
1958 if (other_semantics
)
1959 vtn_warn("Ignoring unhandled memory semantics: %u\n", other_semantics
);
1961 /* SequentiallyConsistent is treated as AcquireRelease. */
1963 /* The RELEASE barrier happens BEFORE the operation, and it is usually
1964 * associated with a Store. All the write operations with a matching
1965 * semantics will not be reordered after the Store.
1967 if (order_semantics
& (SpvMemorySemanticsReleaseMask
|
1968 SpvMemorySemanticsAcquireReleaseMask
|
1969 SpvMemorySemanticsSequentiallyConsistentMask
)) {
1970 *before
|= SpvMemorySemanticsReleaseMask
| storage_semantics
;
1973 /* The ACQUIRE barrier happens AFTER the operation, and it is usually
1974 * associated with a Load. All the operations with a matching semantics
1975 * will not be reordered before the Load.
1977 if (order_semantics
& (SpvMemorySemanticsAcquireMask
|
1978 SpvMemorySemanticsAcquireReleaseMask
|
1979 SpvMemorySemanticsSequentiallyConsistentMask
)) {
1980 *after
|= SpvMemorySemanticsAcquireMask
| storage_semantics
;
1983 if (av_vis_semantics
& SpvMemorySemanticsMakeVisibleMask
)
1984 *before
|= SpvMemorySemanticsMakeVisibleMask
| storage_semantics
;
1986 if (av_vis_semantics
& SpvMemorySemanticsMakeAvailableMask
)
1987 *after
|= SpvMemorySemanticsMakeAvailableMask
| storage_semantics
;
1990 static nir_memory_semantics
1991 vtn_mem_semantics_to_nir_mem_semantics(struct vtn_builder
*b
,
1992 SpvMemorySemanticsMask semantics
)
1994 nir_memory_semantics nir_semantics
= 0;
1996 SpvMemorySemanticsMask order_semantics
=
1997 semantics
& (SpvMemorySemanticsAcquireMask
|
1998 SpvMemorySemanticsReleaseMask
|
1999 SpvMemorySemanticsAcquireReleaseMask
|
2000 SpvMemorySemanticsSequentiallyConsistentMask
);
2002 if (util_bitcount(order_semantics
) > 1) {
2003 /* Old GLSLang versions incorrectly set all the ordering bits. This was
2004 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
2005 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
2007 vtn_warn("Multiple memory ordering semantics bits specified, "
2008 "assuming AcquireRelease.");
2009 order_semantics
= SpvMemorySemanticsAcquireReleaseMask
;
2012 switch (order_semantics
) {
2014 /* Not an ordering barrier. */
2017 case SpvMemorySemanticsAcquireMask
:
2018 nir_semantics
= NIR_MEMORY_ACQUIRE
;
2021 case SpvMemorySemanticsReleaseMask
:
2022 nir_semantics
= NIR_MEMORY_RELEASE
;
2025 case SpvMemorySemanticsSequentiallyConsistentMask
:
2026 /* Fall through. Treated as AcquireRelease in Vulkan. */
2027 case SpvMemorySemanticsAcquireReleaseMask
:
2028 nir_semantics
= NIR_MEMORY_ACQUIRE
| NIR_MEMORY_RELEASE
;
2032 unreachable("Invalid memory order semantics");
2035 if (semantics
& SpvMemorySemanticsMakeAvailableMask
) {
2036 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2037 "To use MakeAvailable memory semantics the VulkanMemoryModel "
2038 "capability must be declared.");
2039 nir_semantics
|= NIR_MEMORY_MAKE_AVAILABLE
;
2042 if (semantics
& SpvMemorySemanticsMakeVisibleMask
) {
2043 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2044 "To use MakeVisible memory semantics the VulkanMemoryModel "
2045 "capability must be declared.");
2046 nir_semantics
|= NIR_MEMORY_MAKE_VISIBLE
;
2049 return nir_semantics
;
2052 static nir_variable_mode
2053 vtn_mem_sematics_to_nir_var_modes(struct vtn_builder
*b
,
2054 SpvMemorySemanticsMask semantics
)
2056 /* Vulkan Environment for SPIR-V says "SubgroupMemory, CrossWorkgroupMemory,
2057 * and AtomicCounterMemory are ignored".
2059 semantics
&= ~(SpvMemorySemanticsSubgroupMemoryMask
|
2060 SpvMemorySemanticsCrossWorkgroupMemoryMask
|
2061 SpvMemorySemanticsAtomicCounterMemoryMask
);
2063 /* TODO: Consider adding nir_var_mem_image mode to NIR so it can be used
2064 * for SpvMemorySemanticsImageMemoryMask.
2067 nir_variable_mode modes
= 0;
2068 if (semantics
& (SpvMemorySemanticsUniformMemoryMask
|
2069 SpvMemorySemanticsImageMemoryMask
)) {
2070 modes
|= nir_var_uniform
|
2075 if (semantics
& SpvMemorySemanticsWorkgroupMemoryMask
)
2076 modes
|= nir_var_mem_shared
;
2077 if (semantics
& SpvMemorySemanticsOutputMemoryMask
) {
2078 modes
|= nir_var_shader_out
;
2085 vtn_scope_to_nir_scope(struct vtn_builder
*b
, SpvScope scope
)
2087 nir_scope nir_scope
;
2089 case SpvScopeDevice
:
2090 vtn_fail_if(b
->options
->caps
.vk_memory_model
&&
2091 !b
->options
->caps
.vk_memory_model_device_scope
,
2092 "If the Vulkan memory model is declared and any instruction "
2093 "uses Device scope, the VulkanMemoryModelDeviceScope "
2094 "capability must be declared.");
2095 nir_scope
= NIR_SCOPE_DEVICE
;
2098 case SpvScopeQueueFamily
:
2099 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2100 "To use Queue Family scope, the VulkanMemoryModel capability "
2101 "must be declared.");
2102 nir_scope
= NIR_SCOPE_QUEUE_FAMILY
;
2105 case SpvScopeWorkgroup
:
2106 nir_scope
= NIR_SCOPE_WORKGROUP
;
2109 case SpvScopeSubgroup
:
2110 nir_scope
= NIR_SCOPE_SUBGROUP
;
2113 case SpvScopeInvocation
:
2114 nir_scope
= NIR_SCOPE_INVOCATION
;
2118 vtn_fail("Invalid memory scope");
2125 vtn_emit_scoped_control_barrier(struct vtn_builder
*b
, SpvScope exec_scope
,
2127 SpvMemorySemanticsMask semantics
)
2129 nir_memory_semantics nir_semantics
=
2130 vtn_mem_semantics_to_nir_mem_semantics(b
, semantics
);
2131 nir_variable_mode modes
= vtn_mem_sematics_to_nir_var_modes(b
, semantics
);
2132 nir_scope nir_exec_scope
= vtn_scope_to_nir_scope(b
, exec_scope
);
2134 /* Memory semantics is optional for OpControlBarrier. */
2135 nir_scope nir_mem_scope
;
2136 if (nir_semantics
== 0 || modes
== 0)
2137 nir_mem_scope
= NIR_SCOPE_NONE
;
2139 nir_mem_scope
= vtn_scope_to_nir_scope(b
, mem_scope
);
2141 nir_scoped_barrier(&b
->nb
, nir_exec_scope
, nir_mem_scope
, nir_semantics
, modes
);
2145 vtn_emit_scoped_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
2146 SpvMemorySemanticsMask semantics
)
2148 nir_variable_mode modes
= vtn_mem_sematics_to_nir_var_modes(b
, semantics
);
2149 nir_memory_semantics nir_semantics
=
2150 vtn_mem_semantics_to_nir_mem_semantics(b
, semantics
);
2152 /* No barrier to add. */
2153 if (nir_semantics
== 0 || modes
== 0)
2156 nir_scope nir_mem_scope
= vtn_scope_to_nir_scope(b
, scope
);
2157 nir_scoped_barrier(&b
->nb
, NIR_SCOPE_NONE
, nir_mem_scope
, nir_semantics
, modes
);
2160 struct vtn_ssa_value
*
2161 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
2163 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
2166 if (!glsl_type_is_vector_or_scalar(type
)) {
2167 unsigned elems
= glsl_get_length(val
->type
);
2168 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2169 if (glsl_type_is_array_or_matrix(type
)) {
2170 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
2171 for (unsigned i
= 0; i
< elems
; i
++)
2172 val
->elems
[i
] = vtn_create_ssa_value(b
, elem_type
);
2174 vtn_assert(glsl_type_is_struct_or_ifc(type
));
2175 for (unsigned i
= 0; i
< elems
; i
++) {
2176 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
2177 val
->elems
[i
] = vtn_create_ssa_value(b
, elem_type
);
2186 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
2189 src
.src
= nir_src_for_ssa(vtn_get_nir_ssa(b
, index
));
2190 src
.src_type
= type
;
2195 image_operand_arg(struct vtn_builder
*b
, const uint32_t *w
, uint32_t count
,
2196 uint32_t mask_idx
, SpvImageOperandsMask op
)
2198 static const SpvImageOperandsMask ops_with_arg
=
2199 SpvImageOperandsBiasMask
|
2200 SpvImageOperandsLodMask
|
2201 SpvImageOperandsGradMask
|
2202 SpvImageOperandsConstOffsetMask
|
2203 SpvImageOperandsOffsetMask
|
2204 SpvImageOperandsConstOffsetsMask
|
2205 SpvImageOperandsSampleMask
|
2206 SpvImageOperandsMinLodMask
|
2207 SpvImageOperandsMakeTexelAvailableMask
|
2208 SpvImageOperandsMakeTexelVisibleMask
;
2210 assert(util_bitcount(op
) == 1);
2211 assert(w
[mask_idx
] & op
);
2212 assert(op
& ops_with_arg
);
2214 uint32_t idx
= util_bitcount(w
[mask_idx
] & (op
- 1) & ops_with_arg
) + 1;
2216 /* Adjust indices for operands with two arguments. */
2217 static const SpvImageOperandsMask ops_with_two_args
=
2218 SpvImageOperandsGradMask
;
2219 idx
+= util_bitcount(w
[mask_idx
] & (op
- 1) & ops_with_two_args
);
2223 vtn_fail_if(idx
+ (op
& ops_with_two_args
? 1 : 0) >= count
,
2224 "Image op claims to have %s but does not enough "
2225 "following operands", spirv_imageoperands_to_string(op
));
2231 non_uniform_decoration_cb(struct vtn_builder
*b
,
2232 struct vtn_value
*val
, int member
,
2233 const struct vtn_decoration
*dec
, void *void_ctx
)
2235 enum gl_access_qualifier
*access
= void_ctx
;
2236 switch (dec
->decoration
) {
2237 case SpvDecorationNonUniformEXT
:
2238 *access
|= ACCESS_NON_UNIFORM
;
2248 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
2249 const uint32_t *w
, unsigned count
)
2251 if (opcode
== SpvOpSampledImage
) {
2252 struct vtn_value
*val
=
2253 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2254 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2256 /* It seems valid to use OpSampledImage with OpUndef instead of
2257 * OpTypeImage or OpTypeSampler.
2259 if (vtn_untyped_value(b
, w
[3])->value_type
== vtn_value_type_undef
) {
2260 val
->sampled_image
->image
= NULL
;
2262 val
->sampled_image
->image
=
2263 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2266 if (vtn_untyped_value(b
, w
[4])->value_type
== vtn_value_type_undef
) {
2267 val
->sampled_image
->sampler
= NULL
;
2269 val
->sampled_image
->sampler
=
2270 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
2273 } else if (opcode
== SpvOpImage
) {
2274 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
2275 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
2276 vtn_push_pointer(b
, w
[2], src_val
->sampled_image
->image
);
2278 vtn_assert(src_val
->value_type
== vtn_value_type_pointer
);
2279 vtn_push_pointer(b
, w
[2], src_val
->pointer
);
2284 struct vtn_type
*ret_type
= vtn_get_type(b
, w
[1]);
2286 struct vtn_pointer
*image
= NULL
, *sampler
= NULL
;
2287 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
2288 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
2289 image
= sampled_val
->sampled_image
->image
;
2290 sampler
= sampled_val
->sampled_image
->sampler
;
2292 vtn_assert(sampled_val
->value_type
== vtn_value_type_pointer
);
2293 image
= sampled_val
->pointer
;
2297 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
2301 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
);
2302 nir_deref_instr
*sampler_deref
=
2303 sampler
? vtn_pointer_to_deref(b
, sampler
) : NULL
;
2305 const struct glsl_type
*image_type
= sampled_val
->type
->type
;
2306 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
2307 const bool is_array
= glsl_sampler_type_is_array(image_type
);
2308 nir_alu_type dest_type
= nir_type_invalid
;
2310 /* Figure out the base texture operation */
2313 case SpvOpImageSampleImplicitLod
:
2314 case SpvOpImageSampleDrefImplicitLod
:
2315 case SpvOpImageSampleProjImplicitLod
:
2316 case SpvOpImageSampleProjDrefImplicitLod
:
2317 texop
= nir_texop_tex
;
2320 case SpvOpImageSampleExplicitLod
:
2321 case SpvOpImageSampleDrefExplicitLod
:
2322 case SpvOpImageSampleProjExplicitLod
:
2323 case SpvOpImageSampleProjDrefExplicitLod
:
2324 texop
= nir_texop_txl
;
2327 case SpvOpImageFetch
:
2328 if (sampler_dim
== GLSL_SAMPLER_DIM_MS
) {
2329 texop
= nir_texop_txf_ms
;
2331 texop
= nir_texop_txf
;
2335 case SpvOpImageGather
:
2336 case SpvOpImageDrefGather
:
2337 texop
= nir_texop_tg4
;
2340 case SpvOpImageQuerySizeLod
:
2341 case SpvOpImageQuerySize
:
2342 texop
= nir_texop_txs
;
2343 dest_type
= nir_type_int
;
2346 case SpvOpImageQueryLod
:
2347 texop
= nir_texop_lod
;
2348 dest_type
= nir_type_float
;
2351 case SpvOpImageQueryLevels
:
2352 texop
= nir_texop_query_levels
;
2353 dest_type
= nir_type_int
;
2356 case SpvOpImageQuerySamples
:
2357 texop
= nir_texop_texture_samples
;
2358 dest_type
= nir_type_int
;
2361 case SpvOpFragmentFetchAMD
:
2362 texop
= nir_texop_fragment_fetch
;
2365 case SpvOpFragmentMaskFetchAMD
:
2366 texop
= nir_texop_fragment_mask_fetch
;
2370 vtn_fail_with_opcode("Unhandled opcode", opcode
);
2373 nir_tex_src srcs
[10]; /* 10 should be enough */
2374 nir_tex_src
*p
= srcs
;
2376 p
->src
= nir_src_for_ssa(&image_deref
->dest
.ssa
);
2377 p
->src_type
= nir_tex_src_texture_deref
;
2387 vtn_fail_if(sampler
== NULL
,
2388 "%s requires an image of type OpTypeSampledImage",
2389 spirv_op_to_string(opcode
));
2390 p
->src
= nir_src_for_ssa(&sampler_deref
->dest
.ssa
);
2391 p
->src_type
= nir_tex_src_sampler_deref
;
2395 case nir_texop_txf_ms
:
2397 case nir_texop_query_levels
:
2398 case nir_texop_texture_samples
:
2399 case nir_texop_samples_identical
:
2400 case nir_texop_fragment_fetch
:
2401 case nir_texop_fragment_mask_fetch
:
2404 case nir_texop_txf_ms_fb
:
2405 vtn_fail("unexpected nir_texop_txf_ms_fb");
2407 case nir_texop_txf_ms_mcs
:
2408 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2409 case nir_texop_tex_prefetch
:
2410 vtn_fail("unexpected nir_texop_tex_prefetch");
2415 struct nir_ssa_def
*coord
;
2416 unsigned coord_components
;
2418 case SpvOpImageSampleImplicitLod
:
2419 case SpvOpImageSampleExplicitLod
:
2420 case SpvOpImageSampleDrefImplicitLod
:
2421 case SpvOpImageSampleDrefExplicitLod
:
2422 case SpvOpImageSampleProjImplicitLod
:
2423 case SpvOpImageSampleProjExplicitLod
:
2424 case SpvOpImageSampleProjDrefImplicitLod
:
2425 case SpvOpImageSampleProjDrefExplicitLod
:
2426 case SpvOpImageFetch
:
2427 case SpvOpImageGather
:
2428 case SpvOpImageDrefGather
:
2429 case SpvOpImageQueryLod
:
2430 case SpvOpFragmentFetchAMD
:
2431 case SpvOpFragmentMaskFetchAMD
: {
2432 /* All these types have the coordinate as their first real argument */
2433 coord_components
= glsl_get_sampler_dim_coordinate_components(sampler_dim
);
2435 if (is_array
&& texop
!= nir_texop_lod
)
2438 coord
= vtn_get_nir_ssa(b
, w
[idx
++]);
2439 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2440 (1 << coord_components
) - 1));
2441 p
->src_type
= nir_tex_src_coord
;
2448 coord_components
= 0;
2453 case SpvOpImageSampleProjImplicitLod
:
2454 case SpvOpImageSampleProjExplicitLod
:
2455 case SpvOpImageSampleProjDrefImplicitLod
:
2456 case SpvOpImageSampleProjDrefExplicitLod
:
2457 /* These have the projector as the last coordinate component */
2458 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2459 p
->src_type
= nir_tex_src_projector
;
2467 bool is_shadow
= false;
2468 unsigned gather_component
= 0;
2470 case SpvOpImageSampleDrefImplicitLod
:
2471 case SpvOpImageSampleDrefExplicitLod
:
2472 case SpvOpImageSampleProjDrefImplicitLod
:
2473 case SpvOpImageSampleProjDrefExplicitLod
:
2474 case SpvOpImageDrefGather
:
2475 /* These all have an explicit depth value as their next source */
2477 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2480 case SpvOpImageGather
:
2481 /* This has a component as its next source */
2482 gather_component
= vtn_constant_uint(b
, w
[idx
++]);
2489 /* For OpImageQuerySizeLod, we always have an LOD */
2490 if (opcode
== SpvOpImageQuerySizeLod
)
2491 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2493 /* For OpFragmentFetchAMD, we always have a multisample index */
2494 if (opcode
== SpvOpFragmentFetchAMD
)
2495 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
2497 /* Now we need to handle some number of optional arguments */
2498 struct vtn_value
*gather_offsets
= NULL
;
2500 uint32_t operands
= w
[idx
];
2502 if (operands
& SpvImageOperandsBiasMask
) {
2503 vtn_assert(texop
== nir_texop_tex
||
2504 texop
== nir_texop_tg4
);
2505 if (texop
== nir_texop_tex
)
2506 texop
= nir_texop_txb
;
2507 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2508 SpvImageOperandsBiasMask
);
2509 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_bias
);
2512 if (operands
& SpvImageOperandsLodMask
) {
2513 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2514 texop
== nir_texop_txs
|| texop
== nir_texop_tg4
);
2515 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2516 SpvImageOperandsLodMask
);
2517 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_lod
);
2520 if (operands
& SpvImageOperandsGradMask
) {
2521 vtn_assert(texop
== nir_texop_txl
);
2522 texop
= nir_texop_txd
;
2523 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2524 SpvImageOperandsGradMask
);
2525 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_ddx
);
2526 (*p
++) = vtn_tex_src(b
, w
[arg
+ 1], nir_tex_src_ddy
);
2529 vtn_fail_if(util_bitcount(operands
& (SpvImageOperandsConstOffsetsMask
|
2530 SpvImageOperandsOffsetMask
|
2531 SpvImageOperandsConstOffsetMask
)) > 1,
2532 "At most one of the ConstOffset, Offset, and ConstOffsets "
2533 "image operands can be used on a given instruction.");
2535 if (operands
& SpvImageOperandsOffsetMask
) {
2536 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2537 SpvImageOperandsOffsetMask
);
2538 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_offset
);
2541 if (operands
& SpvImageOperandsConstOffsetMask
) {
2542 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2543 SpvImageOperandsConstOffsetMask
);
2544 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_offset
);
2547 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2548 vtn_assert(texop
== nir_texop_tg4
);
2549 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2550 SpvImageOperandsConstOffsetsMask
);
2551 gather_offsets
= vtn_value(b
, w
[arg
], vtn_value_type_constant
);
2554 if (operands
& SpvImageOperandsSampleMask
) {
2555 vtn_assert(texop
== nir_texop_txf_ms
);
2556 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2557 SpvImageOperandsSampleMask
);
2558 texop
= nir_texop_txf_ms
;
2559 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_ms_index
);
2562 if (operands
& SpvImageOperandsMinLodMask
) {
2563 vtn_assert(texop
== nir_texop_tex
||
2564 texop
== nir_texop_txb
||
2565 texop
== nir_texop_txd
);
2566 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2567 SpvImageOperandsMinLodMask
);
2568 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_min_lod
);
2572 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2575 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2577 instr
->coord_components
= coord_components
;
2578 instr
->sampler_dim
= sampler_dim
;
2579 instr
->is_array
= is_array
;
2580 instr
->is_shadow
= is_shadow
;
2581 instr
->is_new_style_shadow
=
2582 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2583 instr
->component
= gather_component
;
2585 /* The Vulkan spec says:
2587 * "If an instruction loads from or stores to a resource (including
2588 * atomics and image instructions) and the resource descriptor being
2589 * accessed is not dynamically uniform, then the operand corresponding
2590 * to that resource (e.g. the pointer or sampled image operand) must be
2591 * decorated with NonUniform."
2593 * It's very careful to specify that the exact operand must be decorated
2594 * NonUniform. The SPIR-V parser is not expected to chase through long
2595 * chains to find the NonUniform decoration. It's either right there or we
2596 * can assume it doesn't exist.
2598 enum gl_access_qualifier access
= 0;
2599 vtn_foreach_decoration(b
, sampled_val
, non_uniform_decoration_cb
, &access
);
2601 if (image
&& (access
& ACCESS_NON_UNIFORM
))
2602 instr
->texture_non_uniform
= true;
2604 if (sampler
&& (access
& ACCESS_NON_UNIFORM
))
2605 instr
->sampler_non_uniform
= true;
2607 /* for non-query ops, get dest_type from sampler type */
2608 if (dest_type
== nir_type_invalid
) {
2609 switch (glsl_get_sampler_result_type(image_type
)) {
2610 case GLSL_TYPE_FLOAT
: dest_type
= nir_type_float
; break;
2611 case GLSL_TYPE_INT
: dest_type
= nir_type_int
; break;
2612 case GLSL_TYPE_UINT
: dest_type
= nir_type_uint
; break;
2613 case GLSL_TYPE_BOOL
: dest_type
= nir_type_bool
; break;
2615 vtn_fail("Invalid base type for sampler result");
2619 instr
->dest_type
= dest_type
;
2621 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2622 nir_tex_instr_dest_size(instr
), 32, NULL
);
2624 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2625 nir_tex_instr_dest_size(instr
));
2627 if (gather_offsets
) {
2628 vtn_fail_if(gather_offsets
->type
->base_type
!= vtn_base_type_array
||
2629 gather_offsets
->type
->length
!= 4,
2630 "ConstOffsets must be an array of size four of vectors "
2631 "of two integer components");
2633 struct vtn_type
*vec_type
= gather_offsets
->type
->array_element
;
2634 vtn_fail_if(vec_type
->base_type
!= vtn_base_type_vector
||
2635 vec_type
->length
!= 2 ||
2636 !glsl_type_is_integer(vec_type
->type
),
2637 "ConstOffsets must be an array of size four of vectors "
2638 "of two integer components");
2640 unsigned bit_size
= glsl_get_bit_size(vec_type
->type
);
2641 for (uint32_t i
= 0; i
< 4; i
++) {
2642 const nir_const_value
*cvec
=
2643 gather_offsets
->constant
->elements
[i
]->values
;
2644 for (uint32_t j
= 0; j
< 2; j
++) {
2646 case 8: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i8
; break;
2647 case 16: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i16
; break;
2648 case 32: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i32
; break;
2649 case 64: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i64
; break;
2651 vtn_fail("Unsupported bit size: %u", bit_size
);
2657 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2659 vtn_push_nir_ssa(b
, w
[2], &instr
->dest
.ssa
);
2663 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2664 const uint32_t *w
, nir_src
*src
)
2667 case SpvOpAtomicIIncrement
:
2668 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
2671 case SpvOpAtomicIDecrement
:
2672 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
2675 case SpvOpAtomicISub
:
2677 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_get_nir_ssa(b
, w
[6])));
2680 case SpvOpAtomicCompareExchange
:
2681 case SpvOpAtomicCompareExchangeWeak
:
2682 src
[0] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[8]));
2683 src
[1] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[7]));
2686 case SpvOpAtomicExchange
:
2687 case SpvOpAtomicIAdd
:
2688 case SpvOpAtomicSMin
:
2689 case SpvOpAtomicUMin
:
2690 case SpvOpAtomicSMax
:
2691 case SpvOpAtomicUMax
:
2692 case SpvOpAtomicAnd
:
2694 case SpvOpAtomicXor
:
2695 case SpvOpAtomicFAddEXT
:
2696 src
[0] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[6]));
2700 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2704 static nir_ssa_def
*
2705 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2707 nir_ssa_def
*coord
= vtn_get_nir_ssa(b
, value
);
2709 /* The image_load_store intrinsics assume a 4-dim coordinate */
2710 unsigned swizzle
[4];
2711 for (unsigned i
= 0; i
< 4; i
++)
2712 swizzle
[i
] = MIN2(i
, coord
->num_components
- 1);
2714 return nir_swizzle(&b
->nb
, coord
, swizzle
, 4);
2717 static nir_ssa_def
*
2718 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2720 if (value
->num_components
== 4)
2724 for (unsigned i
= 0; i
< 4; i
++)
2725 swiz
[i
] = i
< value
->num_components
? i
: 0;
2726 return nir_swizzle(b
, value
, swiz
, 4);
2730 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2731 const uint32_t *w
, unsigned count
)
2733 /* Just get this one out of the way */
2734 if (opcode
== SpvOpImageTexelPointer
) {
2735 struct vtn_value
*val
=
2736 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2737 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2739 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2740 val
->image
->coord
= get_image_coord(b
, w
[4]);
2741 val
->image
->sample
= vtn_get_nir_ssa(b
, w
[5]);
2742 val
->image
->lod
= nir_imm_int(&b
->nb
, 0);
2746 struct vtn_image_pointer image
;
2747 SpvScope scope
= SpvScopeInvocation
;
2748 SpvMemorySemanticsMask semantics
= 0;
2750 struct vtn_value
*res_val
;
2752 case SpvOpAtomicExchange
:
2753 case SpvOpAtomicCompareExchange
:
2754 case SpvOpAtomicCompareExchangeWeak
:
2755 case SpvOpAtomicIIncrement
:
2756 case SpvOpAtomicIDecrement
:
2757 case SpvOpAtomicIAdd
:
2758 case SpvOpAtomicISub
:
2759 case SpvOpAtomicLoad
:
2760 case SpvOpAtomicSMin
:
2761 case SpvOpAtomicUMin
:
2762 case SpvOpAtomicSMax
:
2763 case SpvOpAtomicUMax
:
2764 case SpvOpAtomicAnd
:
2766 case SpvOpAtomicXor
:
2767 case SpvOpAtomicFAddEXT
:
2768 res_val
= vtn_value(b
, w
[3], vtn_value_type_image_pointer
);
2769 image
= *res_val
->image
;
2770 scope
= vtn_constant_uint(b
, w
[4]);
2771 semantics
= vtn_constant_uint(b
, w
[5]);
2774 case SpvOpAtomicStore
:
2775 res_val
= vtn_value(b
, w
[1], vtn_value_type_image_pointer
);
2776 image
= *res_val
->image
;
2777 scope
= vtn_constant_uint(b
, w
[2]);
2778 semantics
= vtn_constant_uint(b
, w
[3]);
2781 case SpvOpImageQuerySize
:
2782 res_val
= vtn_value(b
, w
[3], vtn_value_type_pointer
);
2783 image
.image
= res_val
->pointer
;
2785 image
.sample
= NULL
;
2789 case SpvOpImageRead
: {
2790 res_val
= vtn_value(b
, w
[3], vtn_value_type_pointer
);
2791 image
.image
= res_val
->pointer
;
2792 image
.coord
= get_image_coord(b
, w
[4]);
2794 const SpvImageOperandsMask operands
=
2795 count
> 5 ? w
[5] : SpvImageOperandsMaskNone
;
2797 if (operands
& SpvImageOperandsSampleMask
) {
2798 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
2799 SpvImageOperandsSampleMask
);
2800 image
.sample
= vtn_get_nir_ssa(b
, w
[arg
]);
2802 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2805 if (operands
& SpvImageOperandsMakeTexelVisibleMask
) {
2806 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
2807 "MakeTexelVisible requires NonPrivateTexel to also be set.");
2808 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
2809 SpvImageOperandsMakeTexelVisibleMask
);
2810 semantics
= SpvMemorySemanticsMakeVisibleMask
;
2811 scope
= vtn_constant_uint(b
, w
[arg
]);
2814 if (operands
& SpvImageOperandsLodMask
) {
2815 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
2816 SpvImageOperandsLodMask
);
2817 image
.lod
= vtn_get_nir_ssa(b
, w
[arg
]);
2819 image
.lod
= nir_imm_int(&b
->nb
, 0);
2822 /* TODO: Volatile. */
2827 case SpvOpImageWrite
: {
2828 res_val
= vtn_value(b
, w
[1], vtn_value_type_pointer
);
2829 image
.image
= res_val
->pointer
;
2830 image
.coord
= get_image_coord(b
, w
[2]);
2834 const SpvImageOperandsMask operands
=
2835 count
> 4 ? w
[4] : SpvImageOperandsMaskNone
;
2837 if (operands
& SpvImageOperandsSampleMask
) {
2838 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
2839 SpvImageOperandsSampleMask
);
2840 image
.sample
= vtn_get_nir_ssa(b
, w
[arg
]);
2842 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2845 if (operands
& SpvImageOperandsMakeTexelAvailableMask
) {
2846 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
2847 "MakeTexelAvailable requires NonPrivateTexel to also be set.");
2848 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
2849 SpvImageOperandsMakeTexelAvailableMask
);
2850 semantics
= SpvMemorySemanticsMakeAvailableMask
;
2851 scope
= vtn_constant_uint(b
, w
[arg
]);
2854 if (operands
& SpvImageOperandsLodMask
) {
2855 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
2856 SpvImageOperandsLodMask
);
2857 image
.lod
= vtn_get_nir_ssa(b
, w
[arg
]);
2859 image
.lod
= nir_imm_int(&b
->nb
, 0);
2862 /* TODO: Volatile. */
2868 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2871 nir_intrinsic_op op
;
2873 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
2874 OP(ImageQuerySize
, size
)
2876 OP(ImageWrite
, store
)
2877 OP(AtomicLoad
, load
)
2878 OP(AtomicStore
, store
)
2879 OP(AtomicExchange
, atomic_exchange
)
2880 OP(AtomicCompareExchange
, atomic_comp_swap
)
2881 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2882 OP(AtomicIIncrement
, atomic_add
)
2883 OP(AtomicIDecrement
, atomic_add
)
2884 OP(AtomicIAdd
, atomic_add
)
2885 OP(AtomicISub
, atomic_add
)
2886 OP(AtomicSMin
, atomic_imin
)
2887 OP(AtomicUMin
, atomic_umin
)
2888 OP(AtomicSMax
, atomic_imax
)
2889 OP(AtomicUMax
, atomic_umax
)
2890 OP(AtomicAnd
, atomic_and
)
2891 OP(AtomicOr
, atomic_or
)
2892 OP(AtomicXor
, atomic_xor
)
2893 OP(AtomicFAddEXT
, atomic_fadd
)
2896 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2899 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
2901 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
2902 intrin
->src
[0] = nir_src_for_ssa(&image_deref
->dest
.ssa
);
2904 /* ImageQuerySize doesn't take any extra parameters */
2905 if (opcode
!= SpvOpImageQuerySize
) {
2906 /* The image coordinate is always 4 components but we may not have that
2907 * many. Swizzle to compensate.
2909 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
2910 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
2913 /* The Vulkan spec says:
2915 * "If an instruction loads from or stores to a resource (including
2916 * atomics and image instructions) and the resource descriptor being
2917 * accessed is not dynamically uniform, then the operand corresponding
2918 * to that resource (e.g. the pointer or sampled image operand) must be
2919 * decorated with NonUniform."
2921 * It's very careful to specify that the exact operand must be decorated
2922 * NonUniform. The SPIR-V parser is not expected to chase through long
2923 * chains to find the NonUniform decoration. It's either right there or we
2924 * can assume it doesn't exist.
2926 enum gl_access_qualifier access
= 0;
2927 vtn_foreach_decoration(b
, res_val
, non_uniform_decoration_cb
, &access
);
2928 nir_intrinsic_set_access(intrin
, access
);
2931 case SpvOpAtomicLoad
:
2932 case SpvOpImageQuerySize
:
2933 case SpvOpImageRead
:
2934 if (opcode
== SpvOpImageRead
|| opcode
== SpvOpAtomicLoad
) {
2935 /* Only OpImageRead can support a lod parameter if
2936 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
2937 * intrinsics definition for atomics requires us to set it for
2940 intrin
->src
[3] = nir_src_for_ssa(image
.lod
);
2943 case SpvOpAtomicStore
:
2944 case SpvOpImageWrite
: {
2945 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
2946 nir_ssa_def
*value
= vtn_get_nir_ssa(b
, value_id
);
2947 /* nir_intrinsic_image_deref_store always takes a vec4 value */
2948 assert(op
== nir_intrinsic_image_deref_store
);
2949 intrin
->num_components
= 4;
2950 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
));
2951 /* Only OpImageWrite can support a lod parameter if
2952 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
2953 * intrinsics definition for atomics requires us to set it for
2956 intrin
->src
[4] = nir_src_for_ssa(image
.lod
);
2960 case SpvOpAtomicCompareExchange
:
2961 case SpvOpAtomicCompareExchangeWeak
:
2962 case SpvOpAtomicIIncrement
:
2963 case SpvOpAtomicIDecrement
:
2964 case SpvOpAtomicExchange
:
2965 case SpvOpAtomicIAdd
:
2966 case SpvOpAtomicISub
:
2967 case SpvOpAtomicSMin
:
2968 case SpvOpAtomicUMin
:
2969 case SpvOpAtomicSMax
:
2970 case SpvOpAtomicUMax
:
2971 case SpvOpAtomicAnd
:
2973 case SpvOpAtomicXor
:
2974 case SpvOpAtomicFAddEXT
:
2975 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
2979 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2982 /* Image operations implicitly have the Image storage memory semantics. */
2983 semantics
|= SpvMemorySemanticsImageMemoryMask
;
2985 SpvMemorySemanticsMask before_semantics
;
2986 SpvMemorySemanticsMask after_semantics
;
2987 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
2989 if (before_semantics
)
2990 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
2992 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
2993 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
2995 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
2996 if (nir_intrinsic_infos
[op
].dest_components
== 0)
2997 intrin
->num_components
= dest_components
;
2999 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
3000 nir_intrinsic_dest_components(intrin
), 32, NULL
);
3002 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3004 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
3005 if (nir_intrinsic_dest_components(intrin
) != dest_components
)
3006 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
3008 vtn_push_nir_ssa(b
, w
[2], result
);
3010 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3013 if (after_semantics
)
3014 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
3017 static nir_intrinsic_op
3018 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3021 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
3022 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
3023 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
3024 OP(AtomicExchange
, atomic_exchange
)
3025 OP(AtomicCompareExchange
, atomic_comp_swap
)
3026 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3027 OP(AtomicIIncrement
, atomic_add
)
3028 OP(AtomicIDecrement
, atomic_add
)
3029 OP(AtomicIAdd
, atomic_add
)
3030 OP(AtomicISub
, atomic_add
)
3031 OP(AtomicSMin
, atomic_imin
)
3032 OP(AtomicUMin
, atomic_umin
)
3033 OP(AtomicSMax
, atomic_imax
)
3034 OP(AtomicUMax
, atomic_umax
)
3035 OP(AtomicAnd
, atomic_and
)
3036 OP(AtomicOr
, atomic_or
)
3037 OP(AtomicXor
, atomic_xor
)
3038 OP(AtomicFAddEXT
, atomic_fadd
)
3041 vtn_fail_with_opcode("Invalid SSBO atomic", opcode
);
3045 static nir_intrinsic_op
3046 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3049 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
3050 OP(AtomicLoad
, read_deref
)
3051 OP(AtomicExchange
, exchange
)
3052 OP(AtomicCompareExchange
, comp_swap
)
3053 OP(AtomicCompareExchangeWeak
, comp_swap
)
3054 OP(AtomicIIncrement
, inc_deref
)
3055 OP(AtomicIDecrement
, post_dec_deref
)
3056 OP(AtomicIAdd
, add_deref
)
3057 OP(AtomicISub
, add_deref
)
3058 OP(AtomicUMin
, min_deref
)
3059 OP(AtomicUMax
, max_deref
)
3060 OP(AtomicAnd
, and_deref
)
3061 OP(AtomicOr
, or_deref
)
3062 OP(AtomicXor
, xor_deref
)
3065 /* We left the following out: AtomicStore, AtomicSMin and
3066 * AtomicSmax. Right now there are not nir intrinsics for them. At this
3067 * moment Atomic Counter support is needed for ARB_spirv support, so is
3068 * only need to support GLSL Atomic Counters that are uints and don't
3069 * allow direct storage.
3071 vtn_fail("Invalid uniform atomic");
3075 static nir_intrinsic_op
3076 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3079 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
3080 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
3081 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
3082 OP(AtomicExchange
, atomic_exchange
)
3083 OP(AtomicCompareExchange
, atomic_comp_swap
)
3084 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3085 OP(AtomicIIncrement
, atomic_add
)
3086 OP(AtomicIDecrement
, atomic_add
)
3087 OP(AtomicIAdd
, atomic_add
)
3088 OP(AtomicISub
, atomic_add
)
3089 OP(AtomicSMin
, atomic_imin
)
3090 OP(AtomicUMin
, atomic_umin
)
3091 OP(AtomicSMax
, atomic_imax
)
3092 OP(AtomicUMax
, atomic_umax
)
3093 OP(AtomicAnd
, atomic_and
)
3094 OP(AtomicOr
, atomic_or
)
3095 OP(AtomicXor
, atomic_xor
)
3096 OP(AtomicFAddEXT
, atomic_fadd
)
3099 vtn_fail_with_opcode("Invalid shared atomic", opcode
);
3104 * Handles shared atomics, ssbo atomics and atomic counters.
3107 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
3108 const uint32_t *w
, UNUSED
unsigned count
)
3110 struct vtn_pointer
*ptr
;
3111 nir_intrinsic_instr
*atomic
;
3113 SpvScope scope
= SpvScopeInvocation
;
3114 SpvMemorySemanticsMask semantics
= 0;
3117 case SpvOpAtomicLoad
:
3118 case SpvOpAtomicExchange
:
3119 case SpvOpAtomicCompareExchange
:
3120 case SpvOpAtomicCompareExchangeWeak
:
3121 case SpvOpAtomicIIncrement
:
3122 case SpvOpAtomicIDecrement
:
3123 case SpvOpAtomicIAdd
:
3124 case SpvOpAtomicISub
:
3125 case SpvOpAtomicSMin
:
3126 case SpvOpAtomicUMin
:
3127 case SpvOpAtomicSMax
:
3128 case SpvOpAtomicUMax
:
3129 case SpvOpAtomicAnd
:
3131 case SpvOpAtomicXor
:
3132 case SpvOpAtomicFAddEXT
:
3133 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
3134 scope
= vtn_constant_uint(b
, w
[4]);
3135 semantics
= vtn_constant_uint(b
, w
[5]);
3138 case SpvOpAtomicStore
:
3139 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
3140 scope
= vtn_constant_uint(b
, w
[2]);
3141 semantics
= vtn_constant_uint(b
, w
[3]);
3145 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3148 /* uniform as "atomic counter uniform" */
3149 if (ptr
->mode
== vtn_variable_mode_uniform
) {
3150 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3151 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
3152 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3153 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3155 /* SSBO needs to initialize index/offset. In this case we don't need to,
3156 * as that info is already stored on the ptr->var->var nir_variable (see
3157 * vtn_create_variable)
3161 case SpvOpAtomicLoad
:
3162 case SpvOpAtomicExchange
:
3163 case SpvOpAtomicCompareExchange
:
3164 case SpvOpAtomicCompareExchangeWeak
:
3165 case SpvOpAtomicIIncrement
:
3166 case SpvOpAtomicIDecrement
:
3167 case SpvOpAtomicIAdd
:
3168 case SpvOpAtomicISub
:
3169 case SpvOpAtomicSMin
:
3170 case SpvOpAtomicUMin
:
3171 case SpvOpAtomicSMax
:
3172 case SpvOpAtomicUMax
:
3173 case SpvOpAtomicAnd
:
3175 case SpvOpAtomicXor
:
3176 /* Nothing: we don't need to call fill_common_atomic_sources here, as
3177 * atomic counter uniforms doesn't have sources
3182 unreachable("Invalid SPIR-V atomic");
3185 } else if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
3186 nir_ssa_def
*offset
, *index
;
3187 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
3189 assert(ptr
->mode
== vtn_variable_mode_ssbo
);
3191 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(b
, opcode
);
3192 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3196 case SpvOpAtomicLoad
:
3197 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3198 nir_intrinsic_set_align(atomic
, 4, 0);
3199 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3200 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3201 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3204 case SpvOpAtomicStore
:
3205 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3206 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3207 nir_intrinsic_set_align(atomic
, 4, 0);
3208 atomic
->src
[src
++] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[4]));
3209 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3210 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3211 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3214 case SpvOpAtomicExchange
:
3215 case SpvOpAtomicCompareExchange
:
3216 case SpvOpAtomicCompareExchangeWeak
:
3217 case SpvOpAtomicIIncrement
:
3218 case SpvOpAtomicIDecrement
:
3219 case SpvOpAtomicIAdd
:
3220 case SpvOpAtomicISub
:
3221 case SpvOpAtomicSMin
:
3222 case SpvOpAtomicUMin
:
3223 case SpvOpAtomicSMax
:
3224 case SpvOpAtomicUMax
:
3225 case SpvOpAtomicAnd
:
3227 case SpvOpAtomicXor
:
3228 case SpvOpAtomicFAddEXT
:
3229 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3230 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3231 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3232 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
3236 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3239 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3240 const struct glsl_type
*deref_type
= deref
->type
;
3241 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
3242 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3243 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3246 case SpvOpAtomicLoad
:
3247 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3250 case SpvOpAtomicStore
:
3251 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3252 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3253 atomic
->src
[1] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[4]));
3256 case SpvOpAtomicExchange
:
3257 case SpvOpAtomicCompareExchange
:
3258 case SpvOpAtomicCompareExchangeWeak
:
3259 case SpvOpAtomicIIncrement
:
3260 case SpvOpAtomicIDecrement
:
3261 case SpvOpAtomicIAdd
:
3262 case SpvOpAtomicISub
:
3263 case SpvOpAtomicSMin
:
3264 case SpvOpAtomicUMin
:
3265 case SpvOpAtomicSMax
:
3266 case SpvOpAtomicUMax
:
3267 case SpvOpAtomicAnd
:
3269 case SpvOpAtomicXor
:
3270 case SpvOpAtomicFAddEXT
:
3271 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
3275 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3279 /* Atomic ordering operations will implicitly apply to the atomic operation
3280 * storage class, so include that too.
3282 semantics
|= vtn_storage_class_to_memory_semantics(ptr
->ptr_type
->storage_class
);
3284 SpvMemorySemanticsMask before_semantics
;
3285 SpvMemorySemanticsMask after_semantics
;
3286 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
3288 if (before_semantics
)
3289 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
3291 if (opcode
!= SpvOpAtomicStore
) {
3292 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
3294 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
3295 glsl_get_vector_elements(type
->type
),
3296 glsl_get_bit_size(type
->type
), NULL
);
3298 vtn_push_nir_ssa(b
, w
[2], &atomic
->dest
.ssa
);
3301 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
3303 if (after_semantics
)
3304 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
3307 static nir_alu_instr
*
3308 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
3310 nir_op op
= nir_op_vec(num_components
);
3311 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
3312 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
3314 vec
->dest
.write_mask
= (1 << num_components
) - 1;
3319 struct vtn_ssa_value
*
3320 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
3322 if (src
->transposed
)
3323 return src
->transposed
;
3325 struct vtn_ssa_value
*dest
=
3326 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
3328 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
3329 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
3330 glsl_get_bit_size(src
->type
));
3331 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3332 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
3333 vec
->src
[0].swizzle
[0] = i
;
3335 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
3336 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
3337 vec
->src
[j
].swizzle
[0] = i
;
3340 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3341 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
3344 dest
->transposed
= src
;
3349 static nir_ssa_def
*
3350 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
3351 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
3352 const uint32_t *indices
)
3354 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
3356 for (unsigned i
= 0; i
< num_components
; i
++) {
3357 uint32_t index
= indices
[i
];
3358 if (index
== 0xffffffff) {
3360 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
3361 } else if (index
< src0
->num_components
) {
3362 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
3363 vec
->src
[i
].swizzle
[0] = index
;
3365 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
3366 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
3370 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3372 return &vec
->dest
.dest
.ssa
;
3376 * Concatentates a number of vectors/scalars together to produce a vector
3378 static nir_ssa_def
*
3379 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
3380 unsigned num_srcs
, nir_ssa_def
**srcs
)
3382 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
3384 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3386 * "When constructing a vector, there must be at least two Constituent
3389 vtn_assert(num_srcs
>= 2);
3391 unsigned dest_idx
= 0;
3392 for (unsigned i
= 0; i
< num_srcs
; i
++) {
3393 nir_ssa_def
*src
= srcs
[i
];
3394 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
3395 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
3396 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
3397 vec
->src
[dest_idx
].swizzle
[0] = j
;
3402 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3404 * "When constructing a vector, the total number of components in all
3405 * the operands must equal the number of components in Result Type."
3407 vtn_assert(dest_idx
== num_components
);
3409 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3411 return &vec
->dest
.dest
.ssa
;
3414 static struct vtn_ssa_value
*
3415 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3417 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3418 dest
->type
= src
->type
;
3420 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3421 dest
->def
= src
->def
;
3423 unsigned elems
= glsl_get_length(src
->type
);
3425 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3426 for (unsigned i
= 0; i
< elems
; i
++)
3427 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3433 static struct vtn_ssa_value
*
3434 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3435 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3436 unsigned num_indices
)
3438 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3440 struct vtn_ssa_value
*cur
= dest
;
3442 for (i
= 0; i
< num_indices
- 1; i
++) {
3443 /* If we got a vector here, that means the next index will be trying to
3444 * dereference a scalar.
3446 vtn_fail_if(glsl_type_is_vector_or_scalar(cur
->type
),
3447 "OpCompositeInsert has too many indices.");
3448 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3449 "All indices in an OpCompositeInsert must be in-bounds");
3450 cur
= cur
->elems
[indices
[i
]];
3453 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3454 vtn_fail_if(indices
[i
] >= glsl_get_vector_elements(cur
->type
),
3455 "All indices in an OpCompositeInsert must be in-bounds");
3457 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3458 * the component granularity. In that case, the last index will be
3459 * the index to insert the scalar into the vector.
3462 cur
->def
= nir_vector_insert_imm(&b
->nb
, cur
->def
, insert
->def
, indices
[i
]);
3464 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3465 "All indices in an OpCompositeInsert must be in-bounds");
3466 cur
->elems
[indices
[i
]] = insert
;
3472 static struct vtn_ssa_value
*
3473 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3474 const uint32_t *indices
, unsigned num_indices
)
3476 struct vtn_ssa_value
*cur
= src
;
3477 for (unsigned i
= 0; i
< num_indices
; i
++) {
3478 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3479 vtn_assert(i
== num_indices
- 1);
3480 vtn_fail_if(indices
[i
] >= glsl_get_vector_elements(cur
->type
),
3481 "All indices in an OpCompositeExtract must be in-bounds");
3483 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3484 * the component granularity. The last index will be the index of the
3485 * vector to extract.
3488 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
3489 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
3490 ret
->def
= nir_channel(&b
->nb
, cur
->def
, indices
[i
]);
3493 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3494 "All indices in an OpCompositeExtract must be in-bounds");
3495 cur
= cur
->elems
[indices
[i
]];
3503 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3504 const uint32_t *w
, unsigned count
)
3506 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
3507 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, type
->type
);
3510 case SpvOpVectorExtractDynamic
:
3511 ssa
->def
= nir_vector_extract(&b
->nb
, vtn_get_nir_ssa(b
, w
[3]),
3512 vtn_get_nir_ssa(b
, w
[4]));
3515 case SpvOpVectorInsertDynamic
:
3516 ssa
->def
= nir_vector_insert(&b
->nb
, vtn_get_nir_ssa(b
, w
[3]),
3517 vtn_get_nir_ssa(b
, w
[4]),
3518 vtn_get_nir_ssa(b
, w
[5]));
3521 case SpvOpVectorShuffle
:
3522 ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
->type
),
3523 vtn_get_nir_ssa(b
, w
[3]),
3524 vtn_get_nir_ssa(b
, w
[4]),
3528 case SpvOpCompositeConstruct
: {
3529 unsigned elems
= count
- 3;
3531 if (glsl_type_is_vector_or_scalar(type
->type
)) {
3532 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3533 for (unsigned i
= 0; i
< elems
; i
++)
3534 srcs
[i
] = vtn_get_nir_ssa(b
, w
[3 + i
]);
3536 vtn_vector_construct(b
, glsl_get_vector_elements(type
->type
),
3539 ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3540 for (unsigned i
= 0; i
< elems
; i
++)
3541 ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3545 case SpvOpCompositeExtract
:
3546 ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3550 case SpvOpCompositeInsert
:
3551 ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3552 vtn_ssa_value(b
, w
[3]),
3556 case SpvOpCopyLogical
:
3557 ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3559 case SpvOpCopyObject
:
3560 vtn_copy_value(b
, w
[3], w
[2]);
3564 vtn_fail_with_opcode("unknown composite operation", opcode
);
3567 vtn_push_ssa_value(b
, w
[2], ssa
);
3571 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3573 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3574 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3578 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3579 SpvMemorySemanticsMask semantics
)
3581 if (b
->shader
->options
->use_scoped_barrier
) {
3582 vtn_emit_scoped_memory_barrier(b
, scope
, semantics
);
3586 static const SpvMemorySemanticsMask all_memory_semantics
=
3587 SpvMemorySemanticsUniformMemoryMask
|
3588 SpvMemorySemanticsWorkgroupMemoryMask
|
3589 SpvMemorySemanticsAtomicCounterMemoryMask
|
3590 SpvMemorySemanticsImageMemoryMask
|
3591 SpvMemorySemanticsOutputMemoryMask
;
3593 /* If we're not actually doing a memory barrier, bail */
3594 if (!(semantics
& all_memory_semantics
))
3597 /* GL and Vulkan don't have these */
3598 vtn_assert(scope
!= SpvScopeCrossDevice
);
3600 if (scope
== SpvScopeSubgroup
)
3601 return; /* Nothing to do here */
3603 if (scope
== SpvScopeWorkgroup
) {
3604 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3608 /* There's only two scopes thing left */
3609 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3611 /* Map the GLSL memoryBarrier() construct and any barriers with more than one
3612 * semantic to the corresponding NIR one.
3614 if (util_bitcount(semantics
& all_memory_semantics
) > 1) {
3615 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3616 if (semantics
& SpvMemorySemanticsOutputMemoryMask
) {
3617 /* GLSL memoryBarrier() (and the corresponding NIR one) doesn't include
3618 * TCS outputs, so we have to emit it's own intrinsic for that. We
3619 * then need to emit another memory_barrier to prevent moving
3620 * non-output operations to before the tcs_patch barrier.
3622 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_tcs_patch
);
3623 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3628 /* Issue a more specific barrier */
3629 switch (semantics
& all_memory_semantics
) {
3630 case SpvMemorySemanticsUniformMemoryMask
:
3631 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3633 case SpvMemorySemanticsWorkgroupMemoryMask
:
3634 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3636 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3637 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3639 case SpvMemorySemanticsImageMemoryMask
:
3640 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3642 case SpvMemorySemanticsOutputMemoryMask
:
3643 if (b
->nb
.shader
->info
.stage
== MESA_SHADER_TESS_CTRL
)
3644 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_tcs_patch
);
3652 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3653 const uint32_t *w
, UNUSED
unsigned count
)
3656 case SpvOpEmitVertex
:
3657 case SpvOpEmitStreamVertex
:
3658 case SpvOpEndPrimitive
:
3659 case SpvOpEndStreamPrimitive
: {
3660 nir_intrinsic_op intrinsic_op
;
3662 case SpvOpEmitVertex
:
3663 case SpvOpEmitStreamVertex
:
3664 intrinsic_op
= nir_intrinsic_emit_vertex
;
3666 case SpvOpEndPrimitive
:
3667 case SpvOpEndStreamPrimitive
:
3668 intrinsic_op
= nir_intrinsic_end_primitive
;
3671 unreachable("Invalid opcode");
3674 nir_intrinsic_instr
*intrin
=
3675 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3678 case SpvOpEmitStreamVertex
:
3679 case SpvOpEndStreamPrimitive
: {
3680 unsigned stream
= vtn_constant_uint(b
, w
[1]);
3681 nir_intrinsic_set_stream_id(intrin
, stream
);
3689 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3693 case SpvOpMemoryBarrier
: {
3694 SpvScope scope
= vtn_constant_uint(b
, w
[1]);
3695 SpvMemorySemanticsMask semantics
= vtn_constant_uint(b
, w
[2]);
3696 vtn_emit_memory_barrier(b
, scope
, semantics
);
3700 case SpvOpControlBarrier
: {
3701 SpvScope execution_scope
= vtn_constant_uint(b
, w
[1]);
3702 SpvScope memory_scope
= vtn_constant_uint(b
, w
[2]);
3703 SpvMemorySemanticsMask memory_semantics
= vtn_constant_uint(b
, w
[3]);
3705 /* GLSLang, prior to commit 8297936dd6eb3, emitted OpControlBarrier with
3706 * memory semantics of None for GLSL barrier().
3707 * And before that, prior to c3f1cdfa, emitted the OpControlBarrier with
3708 * Device instead of Workgroup for execution scope.
3710 if (b
->wa_glslang_cs_barrier
&&
3711 b
->nb
.shader
->info
.stage
== MESA_SHADER_COMPUTE
&&
3712 (execution_scope
== SpvScopeWorkgroup
||
3713 execution_scope
== SpvScopeDevice
) &&
3714 memory_semantics
== SpvMemorySemanticsMaskNone
) {
3715 execution_scope
= SpvScopeWorkgroup
;
3716 memory_scope
= SpvScopeWorkgroup
;
3717 memory_semantics
= SpvMemorySemanticsAcquireReleaseMask
|
3718 SpvMemorySemanticsWorkgroupMemoryMask
;
3721 /* From the SPIR-V spec:
3723 * "When used with the TessellationControl execution model, it also
3724 * implicitly synchronizes the Output Storage Class: Writes to Output
3725 * variables performed by any invocation executed prior to a
3726 * OpControlBarrier will be visible to any other invocation after
3727 * return from that OpControlBarrier."
3729 if (b
->nb
.shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
3730 memory_semantics
&= ~(SpvMemorySemanticsAcquireMask
|
3731 SpvMemorySemanticsReleaseMask
|
3732 SpvMemorySemanticsAcquireReleaseMask
|
3733 SpvMemorySemanticsSequentiallyConsistentMask
);
3734 memory_semantics
|= SpvMemorySemanticsAcquireReleaseMask
|
3735 SpvMemorySemanticsOutputMemoryMask
;
3738 if (b
->shader
->options
->use_scoped_barrier
) {
3739 vtn_emit_scoped_control_barrier(b
, execution_scope
, memory_scope
,
3742 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
3744 if (execution_scope
== SpvScopeWorkgroup
)
3745 vtn_emit_barrier(b
, nir_intrinsic_control_barrier
);
3751 unreachable("unknown barrier instruction");
3756 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
3757 SpvExecutionMode mode
)
3760 case SpvExecutionModeInputPoints
:
3761 case SpvExecutionModeOutputPoints
:
3762 return 0; /* GL_POINTS */
3763 case SpvExecutionModeInputLines
:
3764 return 1; /* GL_LINES */
3765 case SpvExecutionModeInputLinesAdjacency
:
3766 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3767 case SpvExecutionModeTriangles
:
3768 return 4; /* GL_TRIANGLES */
3769 case SpvExecutionModeInputTrianglesAdjacency
:
3770 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3771 case SpvExecutionModeQuads
:
3772 return 7; /* GL_QUADS */
3773 case SpvExecutionModeIsolines
:
3774 return 0x8E7A; /* GL_ISOLINES */
3775 case SpvExecutionModeOutputLineStrip
:
3776 return 3; /* GL_LINE_STRIP */
3777 case SpvExecutionModeOutputTriangleStrip
:
3778 return 5; /* GL_TRIANGLE_STRIP */
3780 vtn_fail("Invalid primitive type: %s (%u)",
3781 spirv_executionmode_to_string(mode
), mode
);
3786 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
3787 SpvExecutionMode mode
)
3790 case SpvExecutionModeInputPoints
:
3792 case SpvExecutionModeInputLines
:
3794 case SpvExecutionModeInputLinesAdjacency
:
3796 case SpvExecutionModeTriangles
:
3798 case SpvExecutionModeInputTrianglesAdjacency
:
3801 vtn_fail("Invalid GS input mode: %s (%u)",
3802 spirv_executionmode_to_string(mode
), mode
);
3806 static gl_shader_stage
3807 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
3810 case SpvExecutionModelVertex
:
3811 return MESA_SHADER_VERTEX
;
3812 case SpvExecutionModelTessellationControl
:
3813 return MESA_SHADER_TESS_CTRL
;
3814 case SpvExecutionModelTessellationEvaluation
:
3815 return MESA_SHADER_TESS_EVAL
;
3816 case SpvExecutionModelGeometry
:
3817 return MESA_SHADER_GEOMETRY
;
3818 case SpvExecutionModelFragment
:
3819 return MESA_SHADER_FRAGMENT
;
3820 case SpvExecutionModelGLCompute
:
3821 return MESA_SHADER_COMPUTE
;
3822 case SpvExecutionModelKernel
:
3823 return MESA_SHADER_KERNEL
;
3825 vtn_fail("Unsupported execution model: %s (%u)",
3826 spirv_executionmodel_to_string(model
), model
);
3830 #define spv_check_supported(name, cap) do { \
3831 if (!(b->options && b->options->caps.name)) \
3832 vtn_warn("Unsupported SPIR-V capability: %s (%u)", \
3833 spirv_capability_to_string(cap), cap); \
3838 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
3841 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
3842 /* Let this be a name label regardless */
3843 unsigned name_words
;
3844 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
3846 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
3847 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
3850 vtn_assert(b
->entry_point
== NULL
);
3851 b
->entry_point
= entry_point
;
3855 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3856 const uint32_t *w
, unsigned count
)
3863 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
3864 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
3865 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
3866 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
3867 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
3868 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
3871 uint32_t version
= w
[2];
3874 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
3876 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
3880 case SpvOpSourceExtension
:
3881 case SpvOpSourceContinued
:
3882 case SpvOpExtension
:
3883 case SpvOpModuleProcessed
:
3884 /* Unhandled, but these are for debug so that's ok. */
3887 case SpvOpCapability
: {
3888 SpvCapability cap
= w
[1];
3890 case SpvCapabilityMatrix
:
3891 case SpvCapabilityShader
:
3892 case SpvCapabilityGeometry
:
3893 case SpvCapabilityGeometryPointSize
:
3894 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
3895 case SpvCapabilitySampledImageArrayDynamicIndexing
:
3896 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
3897 case SpvCapabilityStorageImageArrayDynamicIndexing
:
3898 case SpvCapabilityImageRect
:
3899 case SpvCapabilitySampledRect
:
3900 case SpvCapabilitySampled1D
:
3901 case SpvCapabilityImage1D
:
3902 case SpvCapabilitySampledCubeArray
:
3903 case SpvCapabilityImageCubeArray
:
3904 case SpvCapabilitySampledBuffer
:
3905 case SpvCapabilityImageBuffer
:
3906 case SpvCapabilityImageQuery
:
3907 case SpvCapabilityDerivativeControl
:
3908 case SpvCapabilityInterpolationFunction
:
3909 case SpvCapabilityMultiViewport
:
3910 case SpvCapabilitySampleRateShading
:
3911 case SpvCapabilityClipDistance
:
3912 case SpvCapabilityCullDistance
:
3913 case SpvCapabilityInputAttachment
:
3914 case SpvCapabilityImageGatherExtended
:
3915 case SpvCapabilityStorageImageExtendedFormats
:
3916 case SpvCapabilityVector16
:
3919 case SpvCapabilityLinkage
:
3920 case SpvCapabilityFloat16Buffer
:
3921 case SpvCapabilitySparseResidency
:
3922 vtn_warn("Unsupported SPIR-V capability: %s",
3923 spirv_capability_to_string(cap
));
3926 case SpvCapabilityMinLod
:
3927 spv_check_supported(min_lod
, cap
);
3930 case SpvCapabilityAtomicStorage
:
3931 spv_check_supported(atomic_storage
, cap
);
3934 case SpvCapabilityFloat64
:
3935 spv_check_supported(float64
, cap
);
3937 case SpvCapabilityInt64
:
3938 spv_check_supported(int64
, cap
);
3940 case SpvCapabilityInt16
:
3941 spv_check_supported(int16
, cap
);
3943 case SpvCapabilityInt8
:
3944 spv_check_supported(int8
, cap
);
3947 case SpvCapabilityTransformFeedback
:
3948 spv_check_supported(transform_feedback
, cap
);
3951 case SpvCapabilityGeometryStreams
:
3952 spv_check_supported(geometry_streams
, cap
);
3955 case SpvCapabilityInt64Atomics
:
3956 spv_check_supported(int64_atomics
, cap
);
3959 case SpvCapabilityStorageImageMultisample
:
3960 spv_check_supported(storage_image_ms
, cap
);
3963 case SpvCapabilityAddresses
:
3964 spv_check_supported(address
, cap
);
3967 case SpvCapabilityKernel
:
3968 spv_check_supported(kernel
, cap
);
3971 case SpvCapabilityImageBasic
:
3972 case SpvCapabilityImageReadWrite
:
3973 case SpvCapabilityImageMipmap
:
3974 case SpvCapabilityPipes
:
3975 case SpvCapabilityDeviceEnqueue
:
3976 case SpvCapabilityLiteralSampler
:
3977 case SpvCapabilityGenericPointer
:
3978 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
3979 spirv_capability_to_string(cap
));
3982 case SpvCapabilityImageMSArray
:
3983 spv_check_supported(image_ms_array
, cap
);
3986 case SpvCapabilityTessellation
:
3987 case SpvCapabilityTessellationPointSize
:
3988 spv_check_supported(tessellation
, cap
);
3991 case SpvCapabilityDrawParameters
:
3992 spv_check_supported(draw_parameters
, cap
);
3995 case SpvCapabilityStorageImageReadWithoutFormat
:
3996 spv_check_supported(image_read_without_format
, cap
);
3999 case SpvCapabilityStorageImageWriteWithoutFormat
:
4000 spv_check_supported(image_write_without_format
, cap
);
4003 case SpvCapabilityDeviceGroup
:
4004 spv_check_supported(device_group
, cap
);
4007 case SpvCapabilityMultiView
:
4008 spv_check_supported(multiview
, cap
);
4011 case SpvCapabilityGroupNonUniform
:
4012 spv_check_supported(subgroup_basic
, cap
);
4015 case SpvCapabilitySubgroupVoteKHR
:
4016 case SpvCapabilityGroupNonUniformVote
:
4017 spv_check_supported(subgroup_vote
, cap
);
4020 case SpvCapabilitySubgroupBallotKHR
:
4021 case SpvCapabilityGroupNonUniformBallot
:
4022 spv_check_supported(subgroup_ballot
, cap
);
4025 case SpvCapabilityGroupNonUniformShuffle
:
4026 case SpvCapabilityGroupNonUniformShuffleRelative
:
4027 spv_check_supported(subgroup_shuffle
, cap
);
4030 case SpvCapabilityGroupNonUniformQuad
:
4031 spv_check_supported(subgroup_quad
, cap
);
4034 case SpvCapabilityGroupNonUniformArithmetic
:
4035 case SpvCapabilityGroupNonUniformClustered
:
4036 spv_check_supported(subgroup_arithmetic
, cap
);
4039 case SpvCapabilityGroups
:
4040 spv_check_supported(amd_shader_ballot
, cap
);
4043 case SpvCapabilityVariablePointersStorageBuffer
:
4044 case SpvCapabilityVariablePointers
:
4045 spv_check_supported(variable_pointers
, cap
);
4046 b
->variable_pointers
= true;
4049 case SpvCapabilityStorageUniformBufferBlock16
:
4050 case SpvCapabilityStorageUniform16
:
4051 case SpvCapabilityStoragePushConstant16
:
4052 case SpvCapabilityStorageInputOutput16
:
4053 spv_check_supported(storage_16bit
, cap
);
4056 case SpvCapabilityShaderLayer
:
4057 case SpvCapabilityShaderViewportIndex
:
4058 case SpvCapabilityShaderViewportIndexLayerEXT
:
4059 spv_check_supported(shader_viewport_index_layer
, cap
);
4062 case SpvCapabilityStorageBuffer8BitAccess
:
4063 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
4064 case SpvCapabilityStoragePushConstant8
:
4065 spv_check_supported(storage_8bit
, cap
);
4068 case SpvCapabilityShaderNonUniformEXT
:
4069 spv_check_supported(descriptor_indexing
, cap
);
4072 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
4073 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
4074 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
4075 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
4078 case SpvCapabilityUniformBufferArrayNonUniformIndexingEXT
:
4079 case SpvCapabilitySampledImageArrayNonUniformIndexingEXT
:
4080 case SpvCapabilityStorageBufferArrayNonUniformIndexingEXT
:
4081 case SpvCapabilityStorageImageArrayNonUniformIndexingEXT
:
4082 case SpvCapabilityInputAttachmentArrayNonUniformIndexingEXT
:
4083 case SpvCapabilityUniformTexelBufferArrayNonUniformIndexingEXT
:
4084 case SpvCapabilityStorageTexelBufferArrayNonUniformIndexingEXT
:
4085 spv_check_supported(descriptor_array_non_uniform_indexing
, cap
);
4088 case SpvCapabilityRuntimeDescriptorArrayEXT
:
4089 spv_check_supported(runtime_descriptor_array
, cap
);
4092 case SpvCapabilityStencilExportEXT
:
4093 spv_check_supported(stencil_export
, cap
);
4096 case SpvCapabilitySampleMaskPostDepthCoverage
:
4097 spv_check_supported(post_depth_coverage
, cap
);
4100 case SpvCapabilityDenormFlushToZero
:
4101 case SpvCapabilityDenormPreserve
:
4102 case SpvCapabilitySignedZeroInfNanPreserve
:
4103 case SpvCapabilityRoundingModeRTE
:
4104 case SpvCapabilityRoundingModeRTZ
:
4105 spv_check_supported(float_controls
, cap
);
4108 case SpvCapabilityPhysicalStorageBufferAddresses
:
4109 spv_check_supported(physical_storage_buffer_address
, cap
);
4112 case SpvCapabilityComputeDerivativeGroupQuadsNV
:
4113 case SpvCapabilityComputeDerivativeGroupLinearNV
:
4114 spv_check_supported(derivative_group
, cap
);
4117 case SpvCapabilityFloat16
:
4118 spv_check_supported(float16
, cap
);
4121 case SpvCapabilityFragmentShaderSampleInterlockEXT
:
4122 spv_check_supported(fragment_shader_sample_interlock
, cap
);
4125 case SpvCapabilityFragmentShaderPixelInterlockEXT
:
4126 spv_check_supported(fragment_shader_pixel_interlock
, cap
);
4129 case SpvCapabilityDemoteToHelperInvocationEXT
:
4130 spv_check_supported(demote_to_helper_invocation
, cap
);
4133 case SpvCapabilityShaderClockKHR
:
4134 spv_check_supported(shader_clock
, cap
);
4137 case SpvCapabilityVulkanMemoryModel
:
4138 spv_check_supported(vk_memory_model
, cap
);
4141 case SpvCapabilityVulkanMemoryModelDeviceScope
:
4142 spv_check_supported(vk_memory_model_device_scope
, cap
);
4145 case SpvCapabilityImageReadWriteLodAMD
:
4146 spv_check_supported(amd_image_read_write_lod
, cap
);
4149 case SpvCapabilityIntegerFunctions2INTEL
:
4150 spv_check_supported(integer_functions2
, cap
);
4153 case SpvCapabilityFragmentMaskAMD
:
4154 spv_check_supported(amd_fragment_mask
, cap
);
4157 case SpvCapabilityImageGatherBiasLodAMD
:
4158 spv_check_supported(amd_image_gather_bias_lod
, cap
);
4161 case SpvCapabilityAtomicFloat32AddEXT
:
4162 spv_check_supported(float32_atomic_add
, cap
);
4165 case SpvCapabilityAtomicFloat64AddEXT
:
4166 spv_check_supported(float64_atomic_add
, cap
);
4170 vtn_fail("Unhandled capability: %s (%u)",
4171 spirv_capability_to_string(cap
), cap
);
4176 case SpvOpExtInstImport
:
4177 vtn_handle_extension(b
, opcode
, w
, count
);
4180 case SpvOpMemoryModel
:
4182 case SpvAddressingModelPhysical32
:
4183 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4184 "AddressingModelPhysical32 only supported for kernels");
4185 b
->shader
->info
.cs
.ptr_size
= 32;
4186 b
->physical_ptrs
= true;
4187 b
->options
->shared_addr_format
= nir_address_format_32bit_global
;
4188 b
->options
->global_addr_format
= nir_address_format_32bit_global
;
4189 b
->options
->temp_addr_format
= nir_address_format_32bit_global
;
4191 case SpvAddressingModelPhysical64
:
4192 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4193 "AddressingModelPhysical64 only supported for kernels");
4194 b
->shader
->info
.cs
.ptr_size
= 64;
4195 b
->physical_ptrs
= true;
4196 b
->options
->shared_addr_format
= nir_address_format_64bit_global
;
4197 b
->options
->global_addr_format
= nir_address_format_64bit_global
;
4198 b
->options
->temp_addr_format
= nir_address_format_64bit_global
;
4200 case SpvAddressingModelLogical
:
4201 vtn_fail_if(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
,
4202 "AddressingModelLogical only supported for shaders");
4203 b
->physical_ptrs
= false;
4205 case SpvAddressingModelPhysicalStorageBuffer64
:
4206 vtn_fail_if(!b
->options
||
4207 !b
->options
->caps
.physical_storage_buffer_address
,
4208 "AddressingModelPhysicalStorageBuffer64 not supported");
4211 vtn_fail("Unknown addressing model: %s (%u)",
4212 spirv_addressingmodel_to_string(w
[1]), w
[1]);
4216 b
->mem_model
= w
[2];
4218 case SpvMemoryModelSimple
:
4219 case SpvMemoryModelGLSL450
:
4220 case SpvMemoryModelOpenCL
:
4222 case SpvMemoryModelVulkan
:
4223 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
4224 "Vulkan memory model is unsupported by this driver");
4227 vtn_fail("Unsupported memory model: %s",
4228 spirv_memorymodel_to_string(w
[2]));
4233 case SpvOpEntryPoint
:
4234 vtn_handle_entry_point(b
, w
, count
);
4238 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
4239 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4243 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4246 case SpvOpMemberName
:
4250 case SpvOpExecutionMode
:
4251 case SpvOpExecutionModeId
:
4252 case SpvOpDecorationGroup
:
4254 case SpvOpDecorateId
:
4255 case SpvOpMemberDecorate
:
4256 case SpvOpGroupDecorate
:
4257 case SpvOpGroupMemberDecorate
:
4258 case SpvOpDecorateString
:
4259 case SpvOpMemberDecorateString
:
4260 vtn_handle_decoration(b
, opcode
, w
, count
);
4263 case SpvOpExtInst
: {
4264 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4265 if (val
->ext_handler
== vtn_handle_non_semantic_instruction
) {
4266 /* NonSemantic extended instructions are acceptable in preamble. */
4267 vtn_handle_non_semantic_instruction(b
, w
[4], w
, count
);
4270 return false; /* End of preamble. */
4275 return false; /* End of preamble */
4282 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4283 const struct vtn_decoration
*mode
, UNUSED
void *data
)
4285 vtn_assert(b
->entry_point
== entry_point
);
4287 switch(mode
->exec_mode
) {
4288 case SpvExecutionModeOriginUpperLeft
:
4289 case SpvExecutionModeOriginLowerLeft
:
4290 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4291 b
->shader
->info
.fs
.origin_upper_left
=
4292 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
4295 case SpvExecutionModeEarlyFragmentTests
:
4296 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4297 b
->shader
->info
.fs
.early_fragment_tests
= true;
4300 case SpvExecutionModePostDepthCoverage
:
4301 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4302 b
->shader
->info
.fs
.post_depth_coverage
= true;
4305 case SpvExecutionModeInvocations
:
4306 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4307 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->operands
[0]);
4310 case SpvExecutionModeDepthReplacing
:
4311 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4312 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
4314 case SpvExecutionModeDepthGreater
:
4315 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4316 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
4318 case SpvExecutionModeDepthLess
:
4319 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4320 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
4322 case SpvExecutionModeDepthUnchanged
:
4323 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4324 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
4327 case SpvExecutionModeLocalSize
:
4328 vtn_assert(gl_shader_stage_is_compute(b
->shader
->info
.stage
));
4329 b
->shader
->info
.cs
.local_size
[0] = mode
->operands
[0];
4330 b
->shader
->info
.cs
.local_size
[1] = mode
->operands
[1];
4331 b
->shader
->info
.cs
.local_size
[2] = mode
->operands
[2];
4334 case SpvExecutionModeLocalSizeId
:
4335 b
->shader
->info
.cs
.local_size
[0] = vtn_constant_uint(b
, mode
->operands
[0]);
4336 b
->shader
->info
.cs
.local_size
[1] = vtn_constant_uint(b
, mode
->operands
[1]);
4337 b
->shader
->info
.cs
.local_size
[2] = vtn_constant_uint(b
, mode
->operands
[2]);
4340 case SpvExecutionModeLocalSizeHint
:
4341 case SpvExecutionModeLocalSizeHintId
:
4342 break; /* Nothing to do with this */
4344 case SpvExecutionModeOutputVertices
:
4345 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4346 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4347 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->operands
[0];
4349 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4350 b
->shader
->info
.gs
.vertices_out
= mode
->operands
[0];
4354 case SpvExecutionModeInputPoints
:
4355 case SpvExecutionModeInputLines
:
4356 case SpvExecutionModeInputLinesAdjacency
:
4357 case SpvExecutionModeTriangles
:
4358 case SpvExecutionModeInputTrianglesAdjacency
:
4359 case SpvExecutionModeQuads
:
4360 case SpvExecutionModeIsolines
:
4361 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4362 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4363 b
->shader
->info
.tess
.primitive_mode
=
4364 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4366 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4367 b
->shader
->info
.gs
.vertices_in
=
4368 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
4369 b
->shader
->info
.gs
.input_primitive
=
4370 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4374 case SpvExecutionModeOutputPoints
:
4375 case SpvExecutionModeOutputLineStrip
:
4376 case SpvExecutionModeOutputTriangleStrip
:
4377 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4378 b
->shader
->info
.gs
.output_primitive
=
4379 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4382 case SpvExecutionModeSpacingEqual
:
4383 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4384 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4385 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
4387 case SpvExecutionModeSpacingFractionalEven
:
4388 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4389 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4390 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
4392 case SpvExecutionModeSpacingFractionalOdd
:
4393 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4394 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4395 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
4397 case SpvExecutionModeVertexOrderCw
:
4398 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4399 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4400 b
->shader
->info
.tess
.ccw
= false;
4402 case SpvExecutionModeVertexOrderCcw
:
4403 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4404 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4405 b
->shader
->info
.tess
.ccw
= true;
4407 case SpvExecutionModePointMode
:
4408 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4409 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4410 b
->shader
->info
.tess
.point_mode
= true;
4413 case SpvExecutionModePixelCenterInteger
:
4414 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4415 b
->shader
->info
.fs
.pixel_center_integer
= true;
4418 case SpvExecutionModeXfb
:
4419 b
->shader
->info
.has_transform_feedback_varyings
= true;
4422 case SpvExecutionModeVecTypeHint
:
4425 case SpvExecutionModeContractionOff
:
4426 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
4427 vtn_warn("ExectionMode only allowed for CL-style kernels: %s",
4428 spirv_executionmode_to_string(mode
->exec_mode
));
4433 case SpvExecutionModeStencilRefReplacingEXT
:
4434 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4437 case SpvExecutionModeDerivativeGroupQuadsNV
:
4438 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4439 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_QUADS
;
4442 case SpvExecutionModeDerivativeGroupLinearNV
:
4443 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4444 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_LINEAR
;
4447 case SpvExecutionModePixelInterlockOrderedEXT
:
4448 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4449 b
->shader
->info
.fs
.pixel_interlock_ordered
= true;
4452 case SpvExecutionModePixelInterlockUnorderedEXT
:
4453 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4454 b
->shader
->info
.fs
.pixel_interlock_unordered
= true;
4457 case SpvExecutionModeSampleInterlockOrderedEXT
:
4458 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4459 b
->shader
->info
.fs
.sample_interlock_ordered
= true;
4462 case SpvExecutionModeSampleInterlockUnorderedEXT
:
4463 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4464 b
->shader
->info
.fs
.sample_interlock_unordered
= true;
4467 case SpvExecutionModeDenormPreserve
:
4468 case SpvExecutionModeDenormFlushToZero
:
4469 case SpvExecutionModeSignedZeroInfNanPreserve
:
4470 case SpvExecutionModeRoundingModeRTE
:
4471 case SpvExecutionModeRoundingModeRTZ
:
4472 /* Already handled in vtn_handle_rounding_mode_in_execution_mode() */
4476 vtn_fail("Unhandled execution mode: %s (%u)",
4477 spirv_executionmode_to_string(mode
->exec_mode
),
4483 vtn_handle_rounding_mode_in_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4484 const struct vtn_decoration
*mode
, void *data
)
4486 vtn_assert(b
->entry_point
== entry_point
);
4488 unsigned execution_mode
= 0;
4490 switch(mode
->exec_mode
) {
4491 case SpvExecutionModeDenormPreserve
:
4492 switch (mode
->operands
[0]) {
4493 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP16
; break;
4494 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP32
; break;
4495 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP64
; break;
4496 default: vtn_fail("Floating point type not supported");
4499 case SpvExecutionModeDenormFlushToZero
:
4500 switch (mode
->operands
[0]) {
4501 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
; break;
4502 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32
; break;
4503 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64
; break;
4504 default: vtn_fail("Floating point type not supported");
4507 case SpvExecutionModeSignedZeroInfNanPreserve
:
4508 switch (mode
->operands
[0]) {
4509 case 16: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16
; break;
4510 case 32: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32
; break;
4511 case 64: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64
; break;
4512 default: vtn_fail("Floating point type not supported");
4515 case SpvExecutionModeRoundingModeRTE
:
4516 switch (mode
->operands
[0]) {
4517 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16
; break;
4518 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32
; break;
4519 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64
; break;
4520 default: vtn_fail("Floating point type not supported");
4523 case SpvExecutionModeRoundingModeRTZ
:
4524 switch (mode
->operands
[0]) {
4525 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
; break;
4526 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
; break;
4527 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
; break;
4528 default: vtn_fail("Floating point type not supported");
4536 b
->shader
->info
.float_controls_execution_mode
|= execution_mode
;
4540 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4541 const uint32_t *w
, unsigned count
)
4543 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
4547 case SpvOpSourceContinued
:
4548 case SpvOpSourceExtension
:
4549 case SpvOpExtension
:
4550 case SpvOpCapability
:
4551 case SpvOpExtInstImport
:
4552 case SpvOpMemoryModel
:
4553 case SpvOpEntryPoint
:
4554 case SpvOpExecutionMode
:
4557 case SpvOpMemberName
:
4558 case SpvOpDecorationGroup
:
4560 case SpvOpDecorateId
:
4561 case SpvOpMemberDecorate
:
4562 case SpvOpGroupDecorate
:
4563 case SpvOpGroupMemberDecorate
:
4564 case SpvOpDecorateString
:
4565 case SpvOpMemberDecorateString
:
4566 vtn_fail("Invalid opcode types and variables section");
4572 case SpvOpTypeFloat
:
4573 case SpvOpTypeVector
:
4574 case SpvOpTypeMatrix
:
4575 case SpvOpTypeImage
:
4576 case SpvOpTypeSampler
:
4577 case SpvOpTypeSampledImage
:
4578 case SpvOpTypeArray
:
4579 case SpvOpTypeRuntimeArray
:
4580 case SpvOpTypeStruct
:
4581 case SpvOpTypeOpaque
:
4582 case SpvOpTypePointer
:
4583 case SpvOpTypeForwardPointer
:
4584 case SpvOpTypeFunction
:
4585 case SpvOpTypeEvent
:
4586 case SpvOpTypeDeviceEvent
:
4587 case SpvOpTypeReserveId
:
4588 case SpvOpTypeQueue
:
4590 vtn_handle_type(b
, opcode
, w
, count
);
4593 case SpvOpConstantTrue
:
4594 case SpvOpConstantFalse
:
4596 case SpvOpConstantComposite
:
4597 case SpvOpConstantSampler
:
4598 case SpvOpConstantNull
:
4599 case SpvOpSpecConstantTrue
:
4600 case SpvOpSpecConstantFalse
:
4601 case SpvOpSpecConstant
:
4602 case SpvOpSpecConstantComposite
:
4603 case SpvOpSpecConstantOp
:
4604 vtn_handle_constant(b
, opcode
, w
, count
);
4609 vtn_handle_variables(b
, opcode
, w
, count
);
4612 case SpvOpExtInst
: {
4613 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4614 /* NonSemantic extended instructions are acceptable in preamble, others
4615 * will indicate the end of preamble.
4617 return val
->ext_handler
== vtn_handle_non_semantic_instruction
;
4621 return false; /* End of preamble */
4627 static struct vtn_ssa_value
*
4628 vtn_nir_select(struct vtn_builder
*b
, struct vtn_ssa_value
*src0
,
4629 struct vtn_ssa_value
*src1
, struct vtn_ssa_value
*src2
)
4631 struct vtn_ssa_value
*dest
= rzalloc(b
, struct vtn_ssa_value
);
4632 dest
->type
= src1
->type
;
4634 if (glsl_type_is_vector_or_scalar(src1
->type
)) {
4635 dest
->def
= nir_bcsel(&b
->nb
, src0
->def
, src1
->def
, src2
->def
);
4637 unsigned elems
= glsl_get_length(src1
->type
);
4639 dest
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
4640 for (unsigned i
= 0; i
< elems
; i
++) {
4641 dest
->elems
[i
] = vtn_nir_select(b
, src0
,
4642 src1
->elems
[i
], src2
->elems
[i
]);
4650 vtn_handle_select(struct vtn_builder
*b
, SpvOp opcode
,
4651 const uint32_t *w
, unsigned count
)
4653 /* Handle OpSelect up-front here because it needs to be able to handle
4654 * pointers and not just regular vectors and scalars.
4656 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
4657 struct vtn_value
*cond_val
= vtn_untyped_value(b
, w
[3]);
4658 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
4659 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
4661 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4662 obj2_val
->type
!= res_val
->type
,
4663 "Object types must match the result type in OpSelect");
4665 vtn_fail_if((cond_val
->type
->base_type
!= vtn_base_type_scalar
&&
4666 cond_val
->type
->base_type
!= vtn_base_type_vector
) ||
4667 !glsl_type_is_boolean(cond_val
->type
->type
),
4668 "OpSelect must have either a vector of booleans or "
4669 "a boolean as Condition type");
4671 vtn_fail_if(cond_val
->type
->base_type
== vtn_base_type_vector
&&
4672 (res_val
->type
->base_type
!= vtn_base_type_vector
||
4673 res_val
->type
->length
!= cond_val
->type
->length
),
4674 "When Condition type in OpSelect is a vector, the Result "
4675 "type must be a vector of the same length");
4677 switch (res_val
->type
->base_type
) {
4678 case vtn_base_type_scalar
:
4679 case vtn_base_type_vector
:
4680 case vtn_base_type_matrix
:
4681 case vtn_base_type_array
:
4682 case vtn_base_type_struct
:
4685 case vtn_base_type_pointer
:
4686 /* We need to have actual storage for pointer types. */
4687 vtn_fail_if(res_val
->type
->type
== NULL
,
4688 "Invalid pointer result type for OpSelect");
4691 vtn_fail("Result type of OpSelect must be a scalar, composite, or pointer");
4694 vtn_push_ssa_value(b
, w
[2],
4695 vtn_nir_select(b
, vtn_ssa_value(b
, w
[3]),
4696 vtn_ssa_value(b
, w
[4]),
4697 vtn_ssa_value(b
, w
[5])));
4701 vtn_handle_ptr(struct vtn_builder
*b
, SpvOp opcode
,
4702 const uint32_t *w
, unsigned count
)
4704 struct vtn_type
*type1
= vtn_get_value_type(b
, w
[3]);
4705 struct vtn_type
*type2
= vtn_get_value_type(b
, w
[4]);
4706 vtn_fail_if(type1
->base_type
!= vtn_base_type_pointer
||
4707 type2
->base_type
!= vtn_base_type_pointer
,
4708 "%s operands must have pointer types",
4709 spirv_op_to_string(opcode
));
4710 vtn_fail_if(type1
->storage_class
!= type2
->storage_class
,
4711 "%s operands must have the same storage class",
4712 spirv_op_to_string(opcode
));
4714 struct vtn_type
*vtn_type
= vtn_get_type(b
, w
[1]);
4715 const struct glsl_type
*type
= vtn_type
->type
;
4717 nir_address_format addr_format
= vtn_mode_to_address_format(
4718 b
, vtn_storage_class_to_mode(b
, type1
->storage_class
, NULL
, NULL
));
4723 case SpvOpPtrDiff
: {
4724 /* OpPtrDiff returns the difference in number of elements (not byte offset). */
4725 unsigned elem_size
, elem_align
;
4726 glsl_get_natural_size_align_bytes(type1
->deref
->type
,
4727 &elem_size
, &elem_align
);
4729 def
= nir_build_addr_isub(&b
->nb
,
4730 vtn_get_nir_ssa(b
, w
[3]),
4731 vtn_get_nir_ssa(b
, w
[4]),
4733 def
= nir_idiv(&b
->nb
, def
, nir_imm_intN_t(&b
->nb
, elem_size
, def
->bit_size
));
4734 def
= nir_i2i(&b
->nb
, def
, glsl_get_bit_size(type
));
4739 case SpvOpPtrNotEqual
: {
4740 def
= nir_build_addr_ieq(&b
->nb
,
4741 vtn_get_nir_ssa(b
, w
[3]),
4742 vtn_get_nir_ssa(b
, w
[4]),
4744 if (opcode
== SpvOpPtrNotEqual
)
4745 def
= nir_inot(&b
->nb
, def
);
4750 unreachable("Invalid ptr operation");
4753 vtn_push_nir_ssa(b
, w
[2], def
);
4757 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4758 const uint32_t *w
, unsigned count
)
4764 case SpvOpLoopMerge
:
4765 case SpvOpSelectionMerge
:
4766 /* This is handled by cfg pre-pass and walk_blocks */
4770 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
4771 val
->type
= vtn_get_type(b
, w
[1]);
4776 vtn_handle_extension(b
, opcode
, w
, count
);
4782 case SpvOpCopyMemory
:
4783 case SpvOpCopyMemorySized
:
4784 case SpvOpAccessChain
:
4785 case SpvOpPtrAccessChain
:
4786 case SpvOpInBoundsAccessChain
:
4787 case SpvOpInBoundsPtrAccessChain
:
4788 case SpvOpArrayLength
:
4789 case SpvOpConvertPtrToU
:
4790 case SpvOpConvertUToPtr
:
4791 vtn_handle_variables(b
, opcode
, w
, count
);
4794 case SpvOpFunctionCall
:
4795 vtn_handle_function_call(b
, opcode
, w
, count
);
4798 case SpvOpSampledImage
:
4800 case SpvOpImageSampleImplicitLod
:
4801 case SpvOpImageSampleExplicitLod
:
4802 case SpvOpImageSampleDrefImplicitLod
:
4803 case SpvOpImageSampleDrefExplicitLod
:
4804 case SpvOpImageSampleProjImplicitLod
:
4805 case SpvOpImageSampleProjExplicitLod
:
4806 case SpvOpImageSampleProjDrefImplicitLod
:
4807 case SpvOpImageSampleProjDrefExplicitLod
:
4808 case SpvOpImageFetch
:
4809 case SpvOpImageGather
:
4810 case SpvOpImageDrefGather
:
4811 case SpvOpImageQuerySizeLod
:
4812 case SpvOpImageQueryLod
:
4813 case SpvOpImageQueryLevels
:
4814 case SpvOpImageQuerySamples
:
4815 vtn_handle_texture(b
, opcode
, w
, count
);
4818 case SpvOpImageRead
:
4819 case SpvOpImageWrite
:
4820 case SpvOpImageTexelPointer
:
4821 vtn_handle_image(b
, opcode
, w
, count
);
4824 case SpvOpImageQuerySize
: {
4825 struct vtn_pointer
*image
=
4826 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
4827 if (glsl_type_is_image(image
->type
->type
)) {
4828 vtn_handle_image(b
, opcode
, w
, count
);
4830 vtn_assert(glsl_type_is_sampler(image
->type
->type
));
4831 vtn_handle_texture(b
, opcode
, w
, count
);
4836 case SpvOpFragmentMaskFetchAMD
:
4837 case SpvOpFragmentFetchAMD
:
4838 vtn_handle_texture(b
, opcode
, w
, count
);
4841 case SpvOpAtomicLoad
:
4842 case SpvOpAtomicExchange
:
4843 case SpvOpAtomicCompareExchange
:
4844 case SpvOpAtomicCompareExchangeWeak
:
4845 case SpvOpAtomicIIncrement
:
4846 case SpvOpAtomicIDecrement
:
4847 case SpvOpAtomicIAdd
:
4848 case SpvOpAtomicISub
:
4849 case SpvOpAtomicSMin
:
4850 case SpvOpAtomicUMin
:
4851 case SpvOpAtomicSMax
:
4852 case SpvOpAtomicUMax
:
4853 case SpvOpAtomicAnd
:
4855 case SpvOpAtomicXor
:
4856 case SpvOpAtomicFAddEXT
: {
4857 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
4858 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4859 vtn_handle_image(b
, opcode
, w
, count
);
4861 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4862 vtn_handle_atomics(b
, opcode
, w
, count
);
4867 case SpvOpAtomicStore
: {
4868 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
4869 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4870 vtn_handle_image(b
, opcode
, w
, count
);
4872 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4873 vtn_handle_atomics(b
, opcode
, w
, count
);
4879 vtn_handle_select(b
, opcode
, w
, count
);
4887 case SpvOpConvertFToU
:
4888 case SpvOpConvertFToS
:
4889 case SpvOpConvertSToF
:
4890 case SpvOpConvertUToF
:
4894 case SpvOpQuantizeToF16
:
4895 case SpvOpPtrCastToGeneric
:
4896 case SpvOpGenericCastToPtr
:
4901 case SpvOpSignBitSet
:
4902 case SpvOpLessOrGreater
:
4904 case SpvOpUnordered
:
4919 case SpvOpVectorTimesScalar
:
4921 case SpvOpIAddCarry
:
4922 case SpvOpISubBorrow
:
4923 case SpvOpUMulExtended
:
4924 case SpvOpSMulExtended
:
4925 case SpvOpShiftRightLogical
:
4926 case SpvOpShiftRightArithmetic
:
4927 case SpvOpShiftLeftLogical
:
4928 case SpvOpLogicalEqual
:
4929 case SpvOpLogicalNotEqual
:
4930 case SpvOpLogicalOr
:
4931 case SpvOpLogicalAnd
:
4932 case SpvOpLogicalNot
:
4933 case SpvOpBitwiseOr
:
4934 case SpvOpBitwiseXor
:
4935 case SpvOpBitwiseAnd
:
4937 case SpvOpFOrdEqual
:
4938 case SpvOpFUnordEqual
:
4939 case SpvOpINotEqual
:
4940 case SpvOpFOrdNotEqual
:
4941 case SpvOpFUnordNotEqual
:
4942 case SpvOpULessThan
:
4943 case SpvOpSLessThan
:
4944 case SpvOpFOrdLessThan
:
4945 case SpvOpFUnordLessThan
:
4946 case SpvOpUGreaterThan
:
4947 case SpvOpSGreaterThan
:
4948 case SpvOpFOrdGreaterThan
:
4949 case SpvOpFUnordGreaterThan
:
4950 case SpvOpULessThanEqual
:
4951 case SpvOpSLessThanEqual
:
4952 case SpvOpFOrdLessThanEqual
:
4953 case SpvOpFUnordLessThanEqual
:
4954 case SpvOpUGreaterThanEqual
:
4955 case SpvOpSGreaterThanEqual
:
4956 case SpvOpFOrdGreaterThanEqual
:
4957 case SpvOpFUnordGreaterThanEqual
:
4963 case SpvOpFwidthFine
:
4964 case SpvOpDPdxCoarse
:
4965 case SpvOpDPdyCoarse
:
4966 case SpvOpFwidthCoarse
:
4967 case SpvOpBitFieldInsert
:
4968 case SpvOpBitFieldSExtract
:
4969 case SpvOpBitFieldUExtract
:
4970 case SpvOpBitReverse
:
4972 case SpvOpTranspose
:
4973 case SpvOpOuterProduct
:
4974 case SpvOpMatrixTimesScalar
:
4975 case SpvOpVectorTimesMatrix
:
4976 case SpvOpMatrixTimesVector
:
4977 case SpvOpMatrixTimesMatrix
:
4978 case SpvOpUCountLeadingZerosINTEL
:
4979 case SpvOpUCountTrailingZerosINTEL
:
4980 case SpvOpAbsISubINTEL
:
4981 case SpvOpAbsUSubINTEL
:
4982 case SpvOpIAddSatINTEL
:
4983 case SpvOpUAddSatINTEL
:
4984 case SpvOpIAverageINTEL
:
4985 case SpvOpUAverageINTEL
:
4986 case SpvOpIAverageRoundedINTEL
:
4987 case SpvOpUAverageRoundedINTEL
:
4988 case SpvOpISubSatINTEL
:
4989 case SpvOpUSubSatINTEL
:
4990 case SpvOpIMul32x16INTEL
:
4991 case SpvOpUMul32x16INTEL
:
4992 vtn_handle_alu(b
, opcode
, w
, count
);
4996 vtn_handle_bitcast(b
, w
, count
);
4999 case SpvOpVectorExtractDynamic
:
5000 case SpvOpVectorInsertDynamic
:
5001 case SpvOpVectorShuffle
:
5002 case SpvOpCompositeConstruct
:
5003 case SpvOpCompositeExtract
:
5004 case SpvOpCompositeInsert
:
5005 case SpvOpCopyLogical
:
5006 case SpvOpCopyObject
:
5007 vtn_handle_composite(b
, opcode
, w
, count
);
5010 case SpvOpEmitVertex
:
5011 case SpvOpEndPrimitive
:
5012 case SpvOpEmitStreamVertex
:
5013 case SpvOpEndStreamPrimitive
:
5014 case SpvOpControlBarrier
:
5015 case SpvOpMemoryBarrier
:
5016 vtn_handle_barrier(b
, opcode
, w
, count
);
5019 case SpvOpGroupNonUniformElect
:
5020 case SpvOpGroupNonUniformAll
:
5021 case SpvOpGroupNonUniformAny
:
5022 case SpvOpGroupNonUniformAllEqual
:
5023 case SpvOpGroupNonUniformBroadcast
:
5024 case SpvOpGroupNonUniformBroadcastFirst
:
5025 case SpvOpGroupNonUniformBallot
:
5026 case SpvOpGroupNonUniformInverseBallot
:
5027 case SpvOpGroupNonUniformBallotBitExtract
:
5028 case SpvOpGroupNonUniformBallotBitCount
:
5029 case SpvOpGroupNonUniformBallotFindLSB
:
5030 case SpvOpGroupNonUniformBallotFindMSB
:
5031 case SpvOpGroupNonUniformShuffle
:
5032 case SpvOpGroupNonUniformShuffleXor
:
5033 case SpvOpGroupNonUniformShuffleUp
:
5034 case SpvOpGroupNonUniformShuffleDown
:
5035 case SpvOpGroupNonUniformIAdd
:
5036 case SpvOpGroupNonUniformFAdd
:
5037 case SpvOpGroupNonUniformIMul
:
5038 case SpvOpGroupNonUniformFMul
:
5039 case SpvOpGroupNonUniformSMin
:
5040 case SpvOpGroupNonUniformUMin
:
5041 case SpvOpGroupNonUniformFMin
:
5042 case SpvOpGroupNonUniformSMax
:
5043 case SpvOpGroupNonUniformUMax
:
5044 case SpvOpGroupNonUniformFMax
:
5045 case SpvOpGroupNonUniformBitwiseAnd
:
5046 case SpvOpGroupNonUniformBitwiseOr
:
5047 case SpvOpGroupNonUniformBitwiseXor
:
5048 case SpvOpGroupNonUniformLogicalAnd
:
5049 case SpvOpGroupNonUniformLogicalOr
:
5050 case SpvOpGroupNonUniformLogicalXor
:
5051 case SpvOpGroupNonUniformQuadBroadcast
:
5052 case SpvOpGroupNonUniformQuadSwap
:
5055 case SpvOpGroupBroadcast
:
5056 case SpvOpGroupIAdd
:
5057 case SpvOpGroupFAdd
:
5058 case SpvOpGroupFMin
:
5059 case SpvOpGroupUMin
:
5060 case SpvOpGroupSMin
:
5061 case SpvOpGroupFMax
:
5062 case SpvOpGroupUMax
:
5063 case SpvOpGroupSMax
:
5064 case SpvOpSubgroupBallotKHR
:
5065 case SpvOpSubgroupFirstInvocationKHR
:
5066 case SpvOpSubgroupReadInvocationKHR
:
5067 case SpvOpSubgroupAllKHR
:
5068 case SpvOpSubgroupAnyKHR
:
5069 case SpvOpSubgroupAllEqualKHR
:
5070 case SpvOpGroupIAddNonUniformAMD
:
5071 case SpvOpGroupFAddNonUniformAMD
:
5072 case SpvOpGroupFMinNonUniformAMD
:
5073 case SpvOpGroupUMinNonUniformAMD
:
5074 case SpvOpGroupSMinNonUniformAMD
:
5075 case SpvOpGroupFMaxNonUniformAMD
:
5076 case SpvOpGroupUMaxNonUniformAMD
:
5077 case SpvOpGroupSMaxNonUniformAMD
:
5078 vtn_handle_subgroup(b
, opcode
, w
, count
);
5083 case SpvOpPtrNotEqual
:
5084 vtn_handle_ptr(b
, opcode
, w
, count
);
5087 case SpvOpBeginInvocationInterlockEXT
:
5088 vtn_emit_barrier(b
, nir_intrinsic_begin_invocation_interlock
);
5091 case SpvOpEndInvocationInterlockEXT
:
5092 vtn_emit_barrier(b
, nir_intrinsic_end_invocation_interlock
);
5095 case SpvOpDemoteToHelperInvocationEXT
: {
5096 nir_intrinsic_instr
*intrin
=
5097 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_demote
);
5098 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5102 case SpvOpIsHelperInvocationEXT
: {
5103 nir_intrinsic_instr
*intrin
=
5104 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_is_helper_invocation
);
5105 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 1, 1, NULL
);
5106 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5108 vtn_push_nir_ssa(b
, w
[2], &intrin
->dest
.ssa
);
5112 case SpvOpReadClockKHR
: {
5113 SpvScope scope
= vtn_constant_uint(b
, w
[3]);
5114 nir_scope nir_scope
;
5117 case SpvScopeDevice
:
5118 nir_scope
= NIR_SCOPE_DEVICE
;
5120 case SpvScopeSubgroup
:
5121 nir_scope
= NIR_SCOPE_SUBGROUP
;
5124 vtn_fail("invalid read clock scope");
5127 /* Operation supports two result types: uvec2 and uint64_t. The NIR
5128 * intrinsic gives uvec2, so pack the result for the other case.
5130 nir_intrinsic_instr
*intrin
=
5131 nir_intrinsic_instr_create(b
->nb
.shader
, nir_intrinsic_shader_clock
);
5132 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 2, 32, NULL
);
5133 nir_intrinsic_set_memory_scope(intrin
, nir_scope
);
5134 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5136 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
5137 const struct glsl_type
*dest_type
= type
->type
;
5138 nir_ssa_def
*result
;
5140 if (glsl_type_is_vector(dest_type
)) {
5141 assert(dest_type
== glsl_vector_type(GLSL_TYPE_UINT
, 2));
5142 result
= &intrin
->dest
.ssa
;
5144 assert(glsl_type_is_scalar(dest_type
));
5145 assert(glsl_get_base_type(dest_type
) == GLSL_TYPE_UINT64
);
5146 result
= nir_pack_64_2x32(&b
->nb
, &intrin
->dest
.ssa
);
5149 vtn_push_nir_ssa(b
, w
[2], result
);
5153 case SpvOpLifetimeStart
:
5154 case SpvOpLifetimeStop
:
5158 vtn_fail_with_opcode("Unhandled opcode", opcode
);
5165 vtn_create_builder(const uint32_t *words
, size_t word_count
,
5166 gl_shader_stage stage
, const char *entry_point_name
,
5167 const struct spirv_to_nir_options
*options
)
5169 /* Initialize the vtn_builder object */
5170 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
5171 struct spirv_to_nir_options
*dup_options
=
5172 ralloc(b
, struct spirv_to_nir_options
);
5173 *dup_options
= *options
;
5176 b
->spirv_word_count
= word_count
;
5180 list_inithead(&b
->functions
);
5181 b
->entry_point_stage
= stage
;
5182 b
->entry_point_name
= entry_point_name
;
5183 b
->options
= dup_options
;
5186 * Handle the SPIR-V header (first 5 dwords).
5187 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
5189 if (word_count
<= 5)
5192 if (words
[0] != SpvMagicNumber
) {
5193 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
5196 if (words
[1] < 0x10000) {
5197 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
5201 uint16_t generator_id
= words
[2] >> 16;
5202 uint16_t generator_version
= words
[2];
5204 /* The first GLSLang version bump actually 1.5 years after #179 was fixed
5205 * but this should at least let us shut the workaround off for modern
5206 * versions of GLSLang.
5208 b
->wa_glslang_179
= (generator_id
== 8 && generator_version
== 1);
5210 /* In GLSLang commit 8297936dd6eb3, their handling of barrier() was fixed
5211 * to provide correct memory semantics on compute shader barrier()
5212 * commands. Prior to that, we need to fix them up ourselves. This
5213 * GLSLang fix caused them to bump to generator version 3.
5215 b
->wa_glslang_cs_barrier
= (generator_id
== 8 && generator_version
< 3);
5217 /* words[2] == generator magic */
5218 unsigned value_id_bound
= words
[3];
5219 if (words
[4] != 0) {
5220 vtn_err("words[4] was %u, want 0", words
[4]);
5224 b
->value_id_bound
= value_id_bound
;
5225 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
5233 static nir_function
*
5234 vtn_emit_kernel_entry_point_wrapper(struct vtn_builder
*b
,
5235 nir_function
*entry_point
)
5237 vtn_assert(entry_point
== b
->entry_point
->func
->impl
->function
);
5238 vtn_fail_if(!entry_point
->name
, "entry points are required to have a name");
5239 const char *func_name
=
5240 ralloc_asprintf(b
->shader
, "__wrapped_%s", entry_point
->name
);
5242 /* we shouldn't have any inputs yet */
5243 vtn_assert(!entry_point
->shader
->num_inputs
);
5244 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
);
5246 nir_function
*main_entry_point
= nir_function_create(b
->shader
, func_name
);
5247 main_entry_point
->impl
= nir_function_impl_create(main_entry_point
);
5248 nir_builder_init(&b
->nb
, main_entry_point
->impl
);
5249 b
->nb
.cursor
= nir_after_cf_list(&main_entry_point
->impl
->body
);
5250 b
->func_param_idx
= 0;
5252 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, entry_point
);
5254 for (unsigned i
= 0; i
< entry_point
->num_params
; ++i
) {
5255 struct vtn_type
*param_type
= b
->entry_point
->func
->type
->params
[i
];
5257 /* consider all pointers to function memory to be parameters passed
5260 bool is_by_val
= param_type
->base_type
== vtn_base_type_pointer
&&
5261 param_type
->storage_class
== SpvStorageClassFunction
;
5263 /* input variable */
5264 nir_variable
*in_var
= rzalloc(b
->nb
.shader
, nir_variable
);
5265 in_var
->data
.mode
= nir_var_shader_in
;
5266 in_var
->data
.read_only
= true;
5267 in_var
->data
.location
= i
;
5270 in_var
->type
= param_type
->deref
->type
;
5272 in_var
->type
= param_type
->type
;
5274 nir_shader_add_variable(b
->nb
.shader
, in_var
);
5275 b
->nb
.shader
->num_inputs
++;
5277 /* we have to copy the entire variable into function memory */
5279 nir_variable
*copy_var
=
5280 nir_local_variable_create(main_entry_point
->impl
, in_var
->type
,
5282 nir_copy_var(&b
->nb
, copy_var
, in_var
);
5284 nir_src_for_ssa(&nir_build_deref_var(&b
->nb
, copy_var
)->dest
.ssa
);
5286 call
->params
[i
] = nir_src_for_ssa(nir_load_var(&b
->nb
, in_var
));
5290 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
5292 return main_entry_point
;
5296 spirv_to_nir(const uint32_t *words
, size_t word_count
,
5297 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
5298 gl_shader_stage stage
, const char *entry_point_name
,
5299 const struct spirv_to_nir_options
*options
,
5300 const nir_shader_compiler_options
*nir_options
)
5303 const uint32_t *word_end
= words
+ word_count
;
5305 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
5306 stage
, entry_point_name
,
5312 /* See also _vtn_fail() */
5313 if (setjmp(b
->fail_jump
)) {
5318 /* Skip the SPIR-V header, handled at vtn_create_builder */
5321 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
5323 /* Handle all the preamble instructions */
5324 words
= vtn_foreach_instruction(b
, words
, word_end
,
5325 vtn_handle_preamble_instruction
);
5327 if (b
->entry_point
== NULL
) {
5328 vtn_fail("Entry point not found");
5333 /* Set shader info defaults */
5334 if (stage
== MESA_SHADER_GEOMETRY
)
5335 b
->shader
->info
.gs
.invocations
= 1;
5337 /* Parse rounding mode execution modes. This has to happen earlier than
5338 * other changes in the execution modes since they can affect, for example,
5339 * the result of the floating point constants.
5341 vtn_foreach_execution_mode(b
, b
->entry_point
,
5342 vtn_handle_rounding_mode_in_execution_mode
, NULL
);
5344 b
->specializations
= spec
;
5345 b
->num_specializations
= num_spec
;
5347 /* Handle all variable, type, and constant instructions */
5348 words
= vtn_foreach_instruction(b
, words
, word_end
,
5349 vtn_handle_variable_or_type_instruction
);
5351 /* Parse execution modes */
5352 vtn_foreach_execution_mode(b
, b
->entry_point
,
5353 vtn_handle_execution_mode
, NULL
);
5355 if (b
->workgroup_size_builtin
) {
5356 vtn_assert(b
->workgroup_size_builtin
->type
->type
==
5357 glsl_vector_type(GLSL_TYPE_UINT
, 3));
5359 nir_const_value
*const_size
=
5360 b
->workgroup_size_builtin
->constant
->values
;
5362 b
->shader
->info
.cs
.local_size
[0] = const_size
[0].u32
;
5363 b
->shader
->info
.cs
.local_size
[1] = const_size
[1].u32
;
5364 b
->shader
->info
.cs
.local_size
[2] = const_size
[2].u32
;
5367 /* Set types on all vtn_values */
5368 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
5370 vtn_build_cfg(b
, words
, word_end
);
5372 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5373 b
->entry_point
->func
->referenced
= true;
5378 vtn_foreach_cf_node(node
, &b
->functions
) {
5379 struct vtn_function
*func
= vtn_cf_node_as_function(node
);
5380 if (func
->referenced
&& !func
->emitted
) {
5381 b
->const_table
= _mesa_pointer_hash_table_create(b
);
5383 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
5389 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5390 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
5391 vtn_assert(entry_point
);
5393 /* post process entry_points with input params */
5394 if (entry_point
->num_params
&& b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
5395 entry_point
= vtn_emit_kernel_entry_point_wrapper(b
, entry_point
);
5397 entry_point
->is_entrypoint
= true;
5399 /* When multiple shader stages exist in the same SPIR-V module, we
5400 * generate input and output variables for every stage, in the same
5401 * NIR program. These dead variables can be invalid NIR. For example,
5402 * TCS outputs must be per-vertex arrays (or decorated 'patch'), while
5403 * VS output variables wouldn't be.
5405 * To ensure we have valid NIR, we eliminate any dead inputs and outputs
5406 * right away. In order to do so, we must lower any constant initializers
5407 * on outputs so nir_remove_dead_variables sees that they're written to.
5409 nir_lower_variable_initializers(b
->shader
, nir_var_shader_out
);
5410 nir_remove_dead_variables(b
->shader
,
5411 nir_var_shader_in
| nir_var_shader_out
, NULL
);
5413 /* We sometimes generate bogus derefs that, while never used, give the
5414 * validator a bit of heartburn. Run dead code to get rid of them.
5416 nir_opt_dce(b
->shader
);
5418 /* Unparent the shader from the vtn_builder before we delete the builder */
5419 ralloc_steal(NULL
, b
->shader
);
5421 nir_shader
*shader
= b
->shader
;