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/u_math.h"
40 vtn_log(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
41 size_t spirv_offset
, const char *message
)
43 if (b
->options
->debug
.func
) {
44 b
->options
->debug
.func(b
->options
->debug
.private_data
,
45 level
, spirv_offset
, message
);
49 if (level
>= NIR_SPIRV_DEBUG_LEVEL_WARNING
)
50 fprintf(stderr
, "%s\n", message
);
55 vtn_logf(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
56 size_t spirv_offset
, const char *fmt
, ...)
62 msg
= ralloc_vasprintf(NULL
, fmt
, args
);
65 vtn_log(b
, level
, spirv_offset
, msg
);
71 vtn_log_err(struct vtn_builder
*b
,
72 enum nir_spirv_debug_level level
, const char *prefix
,
73 const char *file
, unsigned line
,
74 const char *fmt
, va_list args
)
78 msg
= ralloc_strdup(NULL
, prefix
);
81 ralloc_asprintf_append(&msg
, " In file %s:%u\n", file
, line
);
84 ralloc_asprintf_append(&msg
, " ");
86 ralloc_vasprintf_append(&msg
, fmt
, args
);
88 ralloc_asprintf_append(&msg
, "\n %zu bytes into the SPIR-V binary",
92 ralloc_asprintf_append(&msg
,
93 "\n in SPIR-V source file %s, line %d, col %d",
94 b
->file
, b
->line
, b
->col
);
97 vtn_log(b
, level
, b
->spirv_offset
, msg
);
103 vtn_dump_shader(struct vtn_builder
*b
, const char *path
, const char *prefix
)
108 int len
= snprintf(filename
, sizeof(filename
), "%s/%s-%d.spirv",
109 path
, prefix
, idx
++);
110 if (len
< 0 || len
>= sizeof(filename
))
113 FILE *f
= fopen(filename
, "w");
117 fwrite(b
->spirv
, sizeof(*b
->spirv
), b
->spirv_word_count
, f
);
120 vtn_info("SPIR-V shader dumped to %s", filename
);
124 _vtn_warn(struct vtn_builder
*b
, const char *file
, unsigned line
,
125 const char *fmt
, ...)
130 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_WARNING
, "SPIR-V WARNING:\n",
131 file
, line
, fmt
, args
);
136 _vtn_err(struct vtn_builder
*b
, const char *file
, unsigned line
,
137 const char *fmt
, ...)
142 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V ERROR:\n",
143 file
, line
, fmt
, args
);
148 _vtn_fail(struct vtn_builder
*b
, const char *file
, unsigned line
,
149 const char *fmt
, ...)
154 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V parsing FAILED:\n",
155 file
, line
, fmt
, args
);
158 const char *dump_path
= getenv("MESA_SPIRV_FAIL_DUMP_PATH");
160 vtn_dump_shader(b
, dump_path
, "fail");
162 longjmp(b
->fail_jump
, 1);
165 struct spec_constant_value
{
173 static struct vtn_ssa_value
*
174 vtn_undef_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
176 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
179 if (glsl_type_is_vector_or_scalar(type
)) {
180 unsigned num_components
= glsl_get_vector_elements(val
->type
);
181 unsigned bit_size
= glsl_get_bit_size(val
->type
);
182 val
->def
= nir_ssa_undef(&b
->nb
, num_components
, bit_size
);
184 unsigned elems
= glsl_get_length(val
->type
);
185 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
186 if (glsl_type_is_matrix(type
)) {
187 const struct glsl_type
*elem_type
=
188 glsl_vector_type(glsl_get_base_type(type
),
189 glsl_get_vector_elements(type
));
191 for (unsigned i
= 0; i
< elems
; i
++)
192 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
193 } else if (glsl_type_is_array(type
)) {
194 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
195 for (unsigned i
= 0; i
< elems
; i
++)
196 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
198 for (unsigned i
= 0; i
< elems
; i
++) {
199 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
200 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
208 static struct vtn_ssa_value
*
209 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
210 const struct glsl_type
*type
)
212 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
217 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
220 switch (glsl_get_base_type(type
)) {
223 case GLSL_TYPE_INT16
:
224 case GLSL_TYPE_UINT16
:
225 case GLSL_TYPE_UINT8
:
227 case GLSL_TYPE_INT64
:
228 case GLSL_TYPE_UINT64
:
230 case GLSL_TYPE_FLOAT
:
231 case GLSL_TYPE_FLOAT16
:
232 case GLSL_TYPE_DOUBLE
: {
233 int bit_size
= glsl_get_bit_size(type
);
234 if (glsl_type_is_vector_or_scalar(type
)) {
235 unsigned num_components
= glsl_get_vector_elements(val
->type
);
236 nir_load_const_instr
*load
=
237 nir_load_const_instr_create(b
->shader
, num_components
, bit_size
);
239 memcpy(load
->value
, constant
->values
,
240 sizeof(nir_const_value
) * load
->def
.num_components
);
242 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
243 val
->def
= &load
->def
;
245 assert(glsl_type_is_matrix(type
));
246 unsigned columns
= glsl_get_matrix_columns(val
->type
);
247 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, columns
);
248 const struct glsl_type
*column_type
= glsl_get_column_type(val
->type
);
249 for (unsigned i
= 0; i
< columns
; i
++)
250 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
256 case GLSL_TYPE_ARRAY
: {
257 unsigned elems
= glsl_get_length(val
->type
);
258 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
259 const struct glsl_type
*elem_type
= glsl_get_array_element(val
->type
);
260 for (unsigned i
= 0; i
< elems
; i
++)
261 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
266 case GLSL_TYPE_STRUCT
: {
267 unsigned elems
= glsl_get_length(val
->type
);
268 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
269 for (unsigned i
= 0; i
< elems
; i
++) {
270 const struct glsl_type
*elem_type
=
271 glsl_get_struct_field(val
->type
, i
);
272 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
279 vtn_fail("bad constant type");
285 struct vtn_ssa_value
*
286 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
288 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
289 switch (val
->value_type
) {
290 case vtn_value_type_undef
:
291 return vtn_undef_ssa_value(b
, val
->type
->type
);
293 case vtn_value_type_constant
:
294 return vtn_const_ssa_value(b
, val
->constant
, val
->type
->type
);
296 case vtn_value_type_ssa
:
299 case vtn_value_type_pointer
:
300 vtn_assert(val
->pointer
->ptr_type
&& val
->pointer
->ptr_type
->type
);
301 struct vtn_ssa_value
*ssa
=
302 vtn_create_ssa_value(b
, val
->pointer
->ptr_type
->type
);
303 ssa
->def
= vtn_pointer_to_ssa(b
, val
->pointer
);
307 vtn_fail("Invalid type for an SSA value");
312 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
313 unsigned word_count
, unsigned *words_used
)
315 char *dup
= ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
317 /* Ammount of space taken by the string (including the null) */
318 unsigned len
= strlen(dup
) + 1;
319 *words_used
= DIV_ROUND_UP(len
, sizeof(*words
));
325 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
326 const uint32_t *end
, vtn_instruction_handler handler
)
332 const uint32_t *w
= start
;
334 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
335 unsigned count
= w
[0] >> SpvWordCountShift
;
336 vtn_assert(count
>= 1 && w
+ count
<= end
);
338 b
->spirv_offset
= (uint8_t *)w
- (uint8_t *)b
->spirv
;
342 break; /* Do nothing */
345 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
357 if (!handler(b
, opcode
, w
, count
))
375 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
376 const uint32_t *w
, unsigned count
)
378 const char *ext
= (const char *)&w
[2];
380 case SpvOpExtInstImport
: {
381 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
382 if (strcmp(ext
, "GLSL.std.450") == 0) {
383 val
->ext_handler
= vtn_handle_glsl450_instruction
;
384 } else if ((strcmp(ext
, "SPV_AMD_gcn_shader") == 0)
385 && (b
->options
&& b
->options
->caps
.amd_gcn_shader
)) {
386 val
->ext_handler
= vtn_handle_amd_gcn_shader_instruction
;
387 } else if ((strcmp(ext
, "SPV_AMD_shader_ballot") == 0)
388 && (b
->options
&& b
->options
->caps
.amd_shader_ballot
)) {
389 val
->ext_handler
= vtn_handle_amd_shader_ballot_instruction
;
390 } else if ((strcmp(ext
, "SPV_AMD_shader_trinary_minmax") == 0)
391 && (b
->options
&& b
->options
->caps
.amd_trinary_minmax
)) {
392 val
->ext_handler
= vtn_handle_amd_shader_trinary_minmax_instruction
;
393 } else if (strcmp(ext
, "OpenCL.std") == 0) {
394 val
->ext_handler
= vtn_handle_opencl_instruction
;
396 vtn_fail("Unsupported extension: %s", ext
);
402 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
403 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
409 vtn_fail_with_opcode("Unhandled opcode", opcode
);
414 _foreach_decoration_helper(struct vtn_builder
*b
,
415 struct vtn_value
*base_value
,
417 struct vtn_value
*value
,
418 vtn_decoration_foreach_cb cb
, void *data
)
420 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
422 if (dec
->scope
== VTN_DEC_DECORATION
) {
423 member
= parent_member
;
424 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
425 vtn_fail_if(value
->value_type
!= vtn_value_type_type
||
426 value
->type
->base_type
!= vtn_base_type_struct
,
427 "OpMemberDecorate and OpGroupMemberDecorate are only "
428 "allowed on OpTypeStruct");
429 /* This means we haven't recursed yet */
430 assert(value
== base_value
);
432 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
434 vtn_fail_if(member
>= base_value
->type
->length
,
435 "OpMemberDecorate specifies member %d but the "
436 "OpTypeStruct has only %u members",
437 member
, base_value
->type
->length
);
439 /* Not a decoration */
440 assert(dec
->scope
== VTN_DEC_EXECUTION_MODE
);
445 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
446 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
449 cb(b
, base_value
, member
, dec
, data
);
454 /** Iterates (recursively if needed) over all of the decorations on a value
456 * This function iterates over all of the decorations applied to a given
457 * value. If it encounters a decoration group, it recurses into the group
458 * and iterates over all of those decorations as well.
461 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
462 vtn_decoration_foreach_cb cb
, void *data
)
464 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
468 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
469 vtn_execution_mode_foreach_cb cb
, void *data
)
471 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
472 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
475 assert(dec
->group
== NULL
);
476 cb(b
, value
, dec
, data
);
481 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
482 const uint32_t *w
, unsigned count
)
484 const uint32_t *w_end
= w
+ count
;
485 const uint32_t target
= w
[1];
489 case SpvOpDecorationGroup
:
490 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
494 case SpvOpDecorateId
:
495 case SpvOpMemberDecorate
:
496 case SpvOpDecorateString
:
497 case SpvOpMemberDecorateString
:
498 case SpvOpExecutionMode
:
499 case SpvOpExecutionModeId
: {
500 struct vtn_value
*val
= vtn_untyped_value(b
, target
);
502 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
505 case SpvOpDecorateId
:
506 case SpvOpDecorateString
:
507 dec
->scope
= VTN_DEC_DECORATION
;
509 case SpvOpMemberDecorate
:
510 case SpvOpMemberDecorateString
:
511 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
512 vtn_fail_if(dec
->scope
< VTN_DEC_STRUCT_MEMBER0
, /* overflow */
513 "Member argument of OpMemberDecorate too large");
515 case SpvOpExecutionMode
:
516 case SpvOpExecutionModeId
:
517 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
520 unreachable("Invalid decoration opcode");
522 dec
->decoration
= *(w
++);
525 /* Link into the list */
526 dec
->next
= val
->decoration
;
527 val
->decoration
= dec
;
531 case SpvOpGroupMemberDecorate
:
532 case SpvOpGroupDecorate
: {
533 struct vtn_value
*group
=
534 vtn_value(b
, target
, vtn_value_type_decoration_group
);
536 for (; w
< w_end
; w
++) {
537 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
538 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
541 if (opcode
== SpvOpGroupDecorate
) {
542 dec
->scope
= VTN_DEC_DECORATION
;
544 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
545 vtn_fail_if(dec
->scope
< 0, /* Check for overflow */
546 "Member argument of OpGroupMemberDecorate too large");
549 /* Link into the list */
550 dec
->next
= val
->decoration
;
551 val
->decoration
= dec
;
557 unreachable("Unhandled opcode");
561 struct member_decoration_ctx
{
563 struct glsl_struct_field
*fields
;
564 struct vtn_type
*type
;
568 * Returns true if the given type contains a struct decorated Block or
572 vtn_type_contains_block(struct vtn_builder
*b
, struct vtn_type
*type
)
574 switch (type
->base_type
) {
575 case vtn_base_type_array
:
576 return vtn_type_contains_block(b
, type
->array_element
);
577 case vtn_base_type_struct
:
578 if (type
->block
|| type
->buffer_block
)
580 for (unsigned i
= 0; i
< type
->length
; i
++) {
581 if (vtn_type_contains_block(b
, type
->members
[i
]))
590 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
591 * OpStore, or OpCopyMemory between them without breaking anything.
592 * Technically, the SPIR-V rules require the exact same type ID but this lets
593 * us internally be a bit looser.
596 vtn_types_compatible(struct vtn_builder
*b
,
597 struct vtn_type
*t1
, struct vtn_type
*t2
)
599 if (t1
->id
== t2
->id
)
602 if (t1
->base_type
!= t2
->base_type
)
605 switch (t1
->base_type
) {
606 case vtn_base_type_void
:
607 case vtn_base_type_scalar
:
608 case vtn_base_type_vector
:
609 case vtn_base_type_matrix
:
610 case vtn_base_type_image
:
611 case vtn_base_type_sampler
:
612 case vtn_base_type_sampled_image
:
613 return t1
->type
== t2
->type
;
615 case vtn_base_type_array
:
616 return t1
->length
== t2
->length
&&
617 vtn_types_compatible(b
, t1
->array_element
, t2
->array_element
);
619 case vtn_base_type_pointer
:
620 return vtn_types_compatible(b
, t1
->deref
, t2
->deref
);
622 case vtn_base_type_struct
:
623 if (t1
->length
!= t2
->length
)
626 for (unsigned i
= 0; i
< t1
->length
; i
++) {
627 if (!vtn_types_compatible(b
, t1
->members
[i
], t2
->members
[i
]))
632 case vtn_base_type_function
:
633 /* This case shouldn't get hit since you can't copy around function
634 * types. Just require them to be identical.
639 vtn_fail("Invalid base type");
643 vtn_type_without_array(struct vtn_type
*type
)
645 while (type
->base_type
== vtn_base_type_array
)
646 type
= type
->array_element
;
650 /* does a shallow copy of a vtn_type */
652 static struct vtn_type
*
653 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
655 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
658 switch (src
->base_type
) {
659 case vtn_base_type_void
:
660 case vtn_base_type_scalar
:
661 case vtn_base_type_vector
:
662 case vtn_base_type_matrix
:
663 case vtn_base_type_array
:
664 case vtn_base_type_pointer
:
665 case vtn_base_type_image
:
666 case vtn_base_type_sampler
:
667 case vtn_base_type_sampled_image
:
668 /* Nothing more to do */
671 case vtn_base_type_struct
:
672 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
673 memcpy(dest
->members
, src
->members
,
674 src
->length
* sizeof(src
->members
[0]));
676 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
677 memcpy(dest
->offsets
, src
->offsets
,
678 src
->length
* sizeof(src
->offsets
[0]));
681 case vtn_base_type_function
:
682 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
683 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
690 static struct vtn_type
*
691 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
693 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
694 type
= type
->members
[member
];
696 /* We may have an array of matrices.... Oh, joy! */
697 while (glsl_type_is_array(type
->type
)) {
698 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
699 type
= type
->array_element
;
702 vtn_assert(glsl_type_is_matrix(type
->type
));
708 vtn_handle_access_qualifier(struct vtn_builder
*b
, struct vtn_type
*type
,
709 int member
, enum gl_access_qualifier access
)
711 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
712 type
= type
->members
[member
];
714 type
->access
|= access
;
718 array_stride_decoration_cb(struct vtn_builder
*b
,
719 struct vtn_value
*val
, int member
,
720 const struct vtn_decoration
*dec
, void *void_ctx
)
722 struct vtn_type
*type
= val
->type
;
724 if (dec
->decoration
== SpvDecorationArrayStride
) {
725 vtn_fail_if(dec
->operands
[0] == 0, "ArrayStride must be non-zero");
726 type
->stride
= dec
->operands
[0];
731 struct_member_decoration_cb(struct vtn_builder
*b
,
732 struct vtn_value
*val
, int member
,
733 const struct vtn_decoration
*dec
, void *void_ctx
)
735 struct member_decoration_ctx
*ctx
= void_ctx
;
740 assert(member
< ctx
->num_fields
);
742 switch (dec
->decoration
) {
743 case SpvDecorationRelaxedPrecision
:
744 case SpvDecorationUniform
:
745 case SpvDecorationUniformId
:
746 break; /* FIXME: Do nothing with this for now. */
747 case SpvDecorationNonWritable
:
748 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_WRITEABLE
);
750 case SpvDecorationNonReadable
:
751 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_READABLE
);
753 case SpvDecorationVolatile
:
754 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_VOLATILE
);
756 case SpvDecorationCoherent
:
757 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_COHERENT
);
759 case SpvDecorationNoPerspective
:
760 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
762 case SpvDecorationFlat
:
763 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
765 case SpvDecorationCentroid
:
766 ctx
->fields
[member
].centroid
= true;
768 case SpvDecorationSample
:
769 ctx
->fields
[member
].sample
= true;
771 case SpvDecorationStream
:
772 /* Vulkan only allows one GS stream */
773 vtn_assert(dec
->operands
[0] == 0);
775 case SpvDecorationLocation
:
776 ctx
->fields
[member
].location
= dec
->operands
[0];
778 case SpvDecorationComponent
:
779 break; /* FIXME: What should we do with these? */
780 case SpvDecorationBuiltIn
:
781 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
782 ctx
->type
->members
[member
]->is_builtin
= true;
783 ctx
->type
->members
[member
]->builtin
= dec
->operands
[0];
784 ctx
->type
->builtin_block
= true;
786 case SpvDecorationOffset
:
787 ctx
->type
->offsets
[member
] = dec
->operands
[0];
788 ctx
->fields
[member
].offset
= dec
->operands
[0];
790 case SpvDecorationMatrixStride
:
791 /* Handled as a second pass */
793 case SpvDecorationColMajor
:
794 break; /* Nothing to do here. Column-major is the default. */
795 case SpvDecorationRowMajor
:
796 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
799 case SpvDecorationPatch
:
802 case SpvDecorationSpecId
:
803 case SpvDecorationBlock
:
804 case SpvDecorationBufferBlock
:
805 case SpvDecorationArrayStride
:
806 case SpvDecorationGLSLShared
:
807 case SpvDecorationGLSLPacked
:
808 case SpvDecorationInvariant
:
809 case SpvDecorationRestrict
:
810 case SpvDecorationAliased
:
811 case SpvDecorationConstant
:
812 case SpvDecorationIndex
:
813 case SpvDecorationBinding
:
814 case SpvDecorationDescriptorSet
:
815 case SpvDecorationLinkageAttributes
:
816 case SpvDecorationNoContraction
:
817 case SpvDecorationInputAttachmentIndex
:
818 vtn_warn("Decoration not allowed on struct members: %s",
819 spirv_decoration_to_string(dec
->decoration
));
822 case SpvDecorationXfbBuffer
:
823 case SpvDecorationXfbStride
:
824 vtn_warn("Vulkan does not have transform feedback");
827 case SpvDecorationCPacked
:
828 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
829 vtn_warn("Decoration only allowed for CL-style kernels: %s",
830 spirv_decoration_to_string(dec
->decoration
));
832 ctx
->type
->packed
= true;
835 case SpvDecorationSaturatedConversion
:
836 case SpvDecorationFuncParamAttr
:
837 case SpvDecorationFPRoundingMode
:
838 case SpvDecorationFPFastMathMode
:
839 case SpvDecorationAlignment
:
840 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
841 vtn_warn("Decoration only allowed for CL-style kernels: %s",
842 spirv_decoration_to_string(dec
->decoration
));
846 case SpvDecorationUserSemantic
:
847 /* User semantic decorations can safely be ignored by the driver. */
851 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
855 /** Chases the array type all the way down to the tail and rewrites the
856 * glsl_types to be based off the tail's glsl_type.
859 vtn_array_type_rewrite_glsl_type(struct vtn_type
*type
)
861 if (type
->base_type
!= vtn_base_type_array
)
864 vtn_array_type_rewrite_glsl_type(type
->array_element
);
866 type
->type
= glsl_array_type(type
->array_element
->type
,
867 type
->length
, type
->stride
);
870 /* Matrix strides are handled as a separate pass because we need to know
871 * whether the matrix is row-major or not first.
874 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
875 struct vtn_value
*val
, int member
,
876 const struct vtn_decoration
*dec
,
879 if (dec
->decoration
!= SpvDecorationMatrixStride
)
882 vtn_fail_if(member
< 0,
883 "The MatrixStride decoration is only allowed on members "
885 vtn_fail_if(dec
->operands
[0] == 0, "MatrixStride must be non-zero");
887 struct member_decoration_ctx
*ctx
= void_ctx
;
889 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
890 if (mat_type
->row_major
) {
891 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
892 mat_type
->stride
= mat_type
->array_element
->stride
;
893 mat_type
->array_element
->stride
= dec
->operands
[0];
895 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
896 dec
->operands
[0], true);
897 mat_type
->array_element
->type
= glsl_get_column_type(mat_type
->type
);
899 vtn_assert(mat_type
->array_element
->stride
> 0);
900 mat_type
->stride
= dec
->operands
[0];
902 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
903 dec
->operands
[0], false);
906 /* Now that we've replaced the glsl_type with a properly strided matrix
907 * type, rewrite the member type so that it's an array of the proper kind
910 vtn_array_type_rewrite_glsl_type(ctx
->type
->members
[member
]);
911 ctx
->fields
[member
].type
= ctx
->type
->members
[member
]->type
;
915 struct_block_decoration_cb(struct vtn_builder
*b
,
916 struct vtn_value
*val
, int member
,
917 const struct vtn_decoration
*dec
, void *ctx
)
922 struct vtn_type
*type
= val
->type
;
923 if (dec
->decoration
== SpvDecorationBlock
)
925 else if (dec
->decoration
== SpvDecorationBufferBlock
)
926 type
->buffer_block
= true;
930 type_decoration_cb(struct vtn_builder
*b
,
931 struct vtn_value
*val
, int member
,
932 const struct vtn_decoration
*dec
, void *ctx
)
934 struct vtn_type
*type
= val
->type
;
937 /* This should have been handled by OpTypeStruct */
938 assert(val
->type
->base_type
== vtn_base_type_struct
);
939 assert(member
>= 0 && member
< val
->type
->length
);
943 switch (dec
->decoration
) {
944 case SpvDecorationArrayStride
:
945 vtn_assert(type
->base_type
== vtn_base_type_array
||
946 type
->base_type
== vtn_base_type_pointer
);
948 case SpvDecorationBlock
:
949 vtn_assert(type
->base_type
== vtn_base_type_struct
);
950 vtn_assert(type
->block
);
952 case SpvDecorationBufferBlock
:
953 vtn_assert(type
->base_type
== vtn_base_type_struct
);
954 vtn_assert(type
->buffer_block
);
956 case SpvDecorationGLSLShared
:
957 case SpvDecorationGLSLPacked
:
958 /* Ignore these, since we get explicit offsets anyways */
961 case SpvDecorationRowMajor
:
962 case SpvDecorationColMajor
:
963 case SpvDecorationMatrixStride
:
964 case SpvDecorationBuiltIn
:
965 case SpvDecorationNoPerspective
:
966 case SpvDecorationFlat
:
967 case SpvDecorationPatch
:
968 case SpvDecorationCentroid
:
969 case SpvDecorationSample
:
970 case SpvDecorationVolatile
:
971 case SpvDecorationCoherent
:
972 case SpvDecorationNonWritable
:
973 case SpvDecorationNonReadable
:
974 case SpvDecorationUniform
:
975 case SpvDecorationUniformId
:
976 case SpvDecorationLocation
:
977 case SpvDecorationComponent
:
978 case SpvDecorationOffset
:
979 case SpvDecorationXfbBuffer
:
980 case SpvDecorationXfbStride
:
981 case SpvDecorationUserSemantic
:
982 vtn_warn("Decoration only allowed for struct members: %s",
983 spirv_decoration_to_string(dec
->decoration
));
986 case SpvDecorationStream
:
987 /* We don't need to do anything here, as stream is filled up when
988 * aplying the decoration to a variable, just check that if it is not a
989 * struct member, it should be a struct.
991 vtn_assert(type
->base_type
== vtn_base_type_struct
);
994 case SpvDecorationRelaxedPrecision
:
995 case SpvDecorationSpecId
:
996 case SpvDecorationInvariant
:
997 case SpvDecorationRestrict
:
998 case SpvDecorationAliased
:
999 case SpvDecorationConstant
:
1000 case SpvDecorationIndex
:
1001 case SpvDecorationBinding
:
1002 case SpvDecorationDescriptorSet
:
1003 case SpvDecorationLinkageAttributes
:
1004 case SpvDecorationNoContraction
:
1005 case SpvDecorationInputAttachmentIndex
:
1006 vtn_warn("Decoration not allowed on types: %s",
1007 spirv_decoration_to_string(dec
->decoration
));
1010 case SpvDecorationCPacked
:
1011 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
1012 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1013 spirv_decoration_to_string(dec
->decoration
));
1015 type
->packed
= true;
1018 case SpvDecorationSaturatedConversion
:
1019 case SpvDecorationFuncParamAttr
:
1020 case SpvDecorationFPRoundingMode
:
1021 case SpvDecorationFPFastMathMode
:
1022 case SpvDecorationAlignment
:
1023 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1024 spirv_decoration_to_string(dec
->decoration
));
1028 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1033 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
1036 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
1037 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
1038 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
1039 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
1040 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
1041 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
1042 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
1043 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
1044 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
1045 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
1046 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
1047 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
1048 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
1049 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
1050 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
1051 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
1052 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
1053 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
1054 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
1055 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
1056 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
1057 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
1058 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
1059 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
1060 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
1061 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
1062 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
1063 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
1064 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
1065 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
1066 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
1067 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
1068 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
1069 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
1070 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
1071 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
1072 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
1073 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
1074 case SpvImageFormatR16ui
: return 0x8234; /* GL_R16UI */
1075 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
1077 vtn_fail("Invalid image format: %s (%u)",
1078 spirv_imageformat_to_string(format
), format
);
1082 static struct vtn_type
*
1083 vtn_type_layout_std430(struct vtn_builder
*b
, struct vtn_type
*type
,
1084 uint32_t *size_out
, uint32_t *align_out
)
1086 switch (type
->base_type
) {
1087 case vtn_base_type_scalar
: {
1088 uint32_t comp_size
= glsl_type_is_boolean(type
->type
)
1089 ? 4 : glsl_get_bit_size(type
->type
) / 8;
1090 *size_out
= comp_size
;
1091 *align_out
= comp_size
;
1095 case vtn_base_type_vector
: {
1096 uint32_t comp_size
= glsl_type_is_boolean(type
->type
)
1097 ? 4 : glsl_get_bit_size(type
->type
) / 8;
1098 unsigned align_comps
= type
->length
== 3 ? 4 : type
->length
;
1099 *size_out
= comp_size
* type
->length
,
1100 *align_out
= comp_size
* align_comps
;
1104 case vtn_base_type_matrix
:
1105 case vtn_base_type_array
: {
1106 /* We're going to add an array stride */
1107 type
= vtn_type_copy(b
, type
);
1108 uint32_t elem_size
, elem_align
;
1109 type
->array_element
= vtn_type_layout_std430(b
, type
->array_element
,
1110 &elem_size
, &elem_align
);
1111 type
->stride
= vtn_align_u32(elem_size
, elem_align
);
1112 *size_out
= type
->stride
* type
->length
;
1113 *align_out
= elem_align
;
1117 case vtn_base_type_struct
: {
1118 /* We're going to add member offsets */
1119 type
= vtn_type_copy(b
, type
);
1120 uint32_t offset
= 0;
1122 for (unsigned i
= 0; i
< type
->length
; i
++) {
1123 uint32_t mem_size
, mem_align
;
1124 type
->members
[i
] = vtn_type_layout_std430(b
, type
->members
[i
],
1125 &mem_size
, &mem_align
);
1126 offset
= vtn_align_u32(offset
, mem_align
);
1127 type
->offsets
[i
] = offset
;
1129 align
= MAX2(align
, mem_align
);
1137 unreachable("Invalid SPIR-V type for std430");
1142 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
1143 const uint32_t *w
, unsigned count
)
1145 struct vtn_value
*val
= NULL
;
1147 /* In order to properly handle forward declarations, we have to defer
1148 * allocation for pointer types.
1150 if (opcode
!= SpvOpTypePointer
&& opcode
!= SpvOpTypeForwardPointer
) {
1151 val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1152 vtn_fail_if(val
->type
!= NULL
,
1153 "Only pointers can have forward declarations");
1154 val
->type
= rzalloc(b
, struct vtn_type
);
1155 val
->type
->id
= w
[1];
1160 val
->type
->base_type
= vtn_base_type_void
;
1161 val
->type
->type
= glsl_void_type();
1164 val
->type
->base_type
= vtn_base_type_scalar
;
1165 val
->type
->type
= glsl_bool_type();
1166 val
->type
->length
= 1;
1168 case SpvOpTypeInt
: {
1169 int bit_size
= w
[2];
1170 const bool signedness
= w
[3];
1171 val
->type
->base_type
= vtn_base_type_scalar
;
1174 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1177 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1180 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1183 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1186 vtn_fail("Invalid int bit size: %u", bit_size
);
1188 val
->type
->length
= 1;
1192 case SpvOpTypeFloat
: {
1193 int bit_size
= w
[2];
1194 val
->type
->base_type
= vtn_base_type_scalar
;
1197 val
->type
->type
= glsl_float16_t_type();
1200 val
->type
->type
= glsl_float_type();
1203 val
->type
->type
= glsl_double_type();
1206 vtn_fail("Invalid float bit size: %u", bit_size
);
1208 val
->type
->length
= 1;
1212 case SpvOpTypeVector
: {
1213 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1214 unsigned elems
= w
[3];
1216 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1217 "Base type for OpTypeVector must be a scalar");
1218 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1219 "Invalid component count for OpTypeVector");
1221 val
->type
->base_type
= vtn_base_type_vector
;
1222 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1223 val
->type
->length
= elems
;
1224 val
->type
->stride
= glsl_type_is_boolean(val
->type
->type
)
1225 ? 4 : glsl_get_bit_size(base
->type
) / 8;
1226 val
->type
->array_element
= base
;
1230 case SpvOpTypeMatrix
: {
1231 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1232 unsigned columns
= w
[3];
1234 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1235 "Base type for OpTypeMatrix must be a vector");
1236 vtn_fail_if(columns
< 2 || columns
> 4,
1237 "Invalid column count for OpTypeMatrix");
1239 val
->type
->base_type
= vtn_base_type_matrix
;
1240 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1241 glsl_get_vector_elements(base
->type
),
1243 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1244 "Unsupported base type for OpTypeMatrix");
1245 assert(!glsl_type_is_error(val
->type
->type
));
1246 val
->type
->length
= columns
;
1247 val
->type
->array_element
= base
;
1248 val
->type
->row_major
= false;
1249 val
->type
->stride
= 0;
1253 case SpvOpTypeRuntimeArray
:
1254 case SpvOpTypeArray
: {
1255 struct vtn_type
*array_element
=
1256 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1258 if (opcode
== SpvOpTypeRuntimeArray
) {
1259 /* A length of 0 is used to denote unsized arrays */
1260 val
->type
->length
= 0;
1262 val
->type
->length
= vtn_constant_uint(b
, w
[3]);
1265 val
->type
->base_type
= vtn_base_type_array
;
1266 val
->type
->array_element
= array_element
;
1267 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
1268 val
->type
->stride
= glsl_get_cl_size(array_element
->type
);
1270 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1271 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
,
1276 case SpvOpTypeStruct
: {
1277 unsigned num_fields
= count
- 2;
1278 val
->type
->base_type
= vtn_base_type_struct
;
1279 val
->type
->length
= num_fields
;
1280 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1281 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1282 val
->type
->packed
= false;
1284 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1285 for (unsigned i
= 0; i
< num_fields
; i
++) {
1286 val
->type
->members
[i
] =
1287 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
1288 fields
[i
] = (struct glsl_struct_field
) {
1289 .type
= val
->type
->members
[i
]->type
,
1290 .name
= ralloc_asprintf(b
, "field%d", i
),
1296 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1297 unsigned offset
= 0;
1298 for (unsigned i
= 0; i
< num_fields
; i
++) {
1299 offset
= align(offset
, glsl_get_cl_alignment(fields
[i
].type
));
1300 fields
[i
].offset
= offset
;
1301 offset
+= glsl_get_cl_size(fields
[i
].type
);
1305 struct member_decoration_ctx ctx
= {
1306 .num_fields
= num_fields
,
1311 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1312 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1314 vtn_foreach_decoration(b
, val
, struct_block_decoration_cb
, NULL
);
1316 const char *name
= val
->name
;
1318 if (val
->type
->block
|| val
->type
->buffer_block
) {
1319 /* Packing will be ignored since types coming from SPIR-V are
1320 * explicitly laid out.
1322 val
->type
->type
= glsl_interface_type(fields
, num_fields
,
1323 /* packing */ 0, false,
1324 name
? name
: "block");
1326 val
->type
->type
= glsl_struct_type(fields
, num_fields
,
1327 name
? name
: "struct", false);
1332 case SpvOpTypeFunction
: {
1333 val
->type
->base_type
= vtn_base_type_function
;
1334 val
->type
->type
= NULL
;
1336 val
->type
->return_type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1338 const unsigned num_params
= count
- 3;
1339 val
->type
->length
= num_params
;
1340 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1341 for (unsigned i
= 0; i
< count
- 3; i
++) {
1342 val
->type
->params
[i
] =
1343 vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
;
1348 case SpvOpTypePointer
:
1349 case SpvOpTypeForwardPointer
: {
1350 /* We can't blindly push the value because it might be a forward
1353 val
= vtn_untyped_value(b
, w
[1]);
1355 SpvStorageClass storage_class
= w
[2];
1357 if (val
->value_type
== vtn_value_type_invalid
) {
1358 val
->value_type
= vtn_value_type_type
;
1359 val
->type
= rzalloc(b
, struct vtn_type
);
1360 val
->type
->id
= w
[1];
1361 val
->type
->base_type
= vtn_base_type_pointer
;
1362 val
->type
->storage_class
= storage_class
;
1364 /* These can actually be stored to nir_variables and used as SSA
1365 * values so they need a real glsl_type.
1367 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1368 b
, storage_class
, NULL
, NULL
);
1369 val
->type
->type
= nir_address_format_to_glsl_type(
1370 vtn_mode_to_address_format(b
, mode
));
1372 vtn_fail_if(val
->type
->storage_class
!= storage_class
,
1373 "The storage classes of an OpTypePointer and any "
1374 "OpTypeForwardPointers that provide forward "
1375 "declarations of it must match.");
1378 if (opcode
== SpvOpTypePointer
) {
1379 vtn_fail_if(val
->type
->deref
!= NULL
,
1380 "While OpTypeForwardPointer can be used to provide a "
1381 "forward declaration of a pointer, OpTypePointer can "
1382 "only be used once for a given id.");
1384 val
->type
->deref
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
1386 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1388 if (b
->physical_ptrs
) {
1389 switch (storage_class
) {
1390 case SpvStorageClassFunction
:
1391 case SpvStorageClassWorkgroup
:
1392 case SpvStorageClassCrossWorkgroup
:
1393 val
->type
->stride
= align(glsl_get_cl_size(val
->type
->deref
->type
),
1394 glsl_get_cl_alignment(val
->type
->deref
->type
));
1401 if (storage_class
== SpvStorageClassWorkgroup
&&
1402 b
->options
->lower_workgroup_access_to_offsets
) {
1403 uint32_t size
, align
;
1404 val
->type
->deref
= vtn_type_layout_std430(b
, val
->type
->deref
,
1406 val
->type
->length
= size
;
1407 val
->type
->align
= align
;
1413 case SpvOpTypeImage
: {
1414 val
->type
->base_type
= vtn_base_type_image
;
1416 const struct vtn_type
*sampled_type
=
1417 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1419 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1420 glsl_get_bit_size(sampled_type
->type
) != 32,
1421 "Sampled type of OpTypeImage must be a 32-bit scalar");
1423 enum glsl_sampler_dim dim
;
1424 switch ((SpvDim
)w
[3]) {
1425 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1426 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1427 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1428 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1429 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1430 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1431 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1433 vtn_fail("Invalid SPIR-V image dimensionality: %s (%u)",
1434 spirv_dim_to_string((SpvDim
)w
[3]), w
[3]);
1437 /* w[4]: as per Vulkan spec "Validation Rules within a Module",
1438 * The “Depth” operand of OpTypeImage is ignored.
1440 bool is_array
= w
[5];
1441 bool multisampled
= w
[6];
1442 unsigned sampled
= w
[7];
1443 SpvImageFormat format
= w
[8];
1446 val
->type
->access_qualifier
= w
[9];
1448 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1451 if (dim
== GLSL_SAMPLER_DIM_2D
)
1452 dim
= GLSL_SAMPLER_DIM_MS
;
1453 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1454 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1456 vtn_fail("Unsupported multisampled image type");
1459 val
->type
->image_format
= translate_image_format(b
, format
);
1461 enum glsl_base_type sampled_base_type
=
1462 glsl_get_base_type(sampled_type
->type
);
1464 val
->type
->sampled
= true;
1465 val
->type
->type
= glsl_sampler_type(dim
, false, is_array
,
1467 } else if (sampled
== 2) {
1468 val
->type
->sampled
= false;
1469 val
->type
->type
= glsl_image_type(dim
, is_array
, sampled_base_type
);
1471 vtn_fail("We need to know if the image will be sampled");
1476 case SpvOpTypeSampledImage
:
1477 val
->type
->base_type
= vtn_base_type_sampled_image
;
1478 val
->type
->image
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1479 val
->type
->type
= val
->type
->image
->type
;
1482 case SpvOpTypeSampler
:
1483 /* The actual sampler type here doesn't really matter. It gets
1484 * thrown away the moment you combine it with an image. What really
1485 * matters is that it's a sampler type as opposed to an integer type
1486 * so the backend knows what to do.
1488 val
->type
->base_type
= vtn_base_type_sampler
;
1489 val
->type
->type
= glsl_bare_sampler_type();
1492 case SpvOpTypeOpaque
:
1493 case SpvOpTypeEvent
:
1494 case SpvOpTypeDeviceEvent
:
1495 case SpvOpTypeReserveId
:
1496 case SpvOpTypeQueue
:
1499 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1502 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1504 if (val
->type
->base_type
== vtn_base_type_struct
&&
1505 (val
->type
->block
|| val
->type
->buffer_block
)) {
1506 for (unsigned i
= 0; i
< val
->type
->length
; i
++) {
1507 vtn_fail_if(vtn_type_contains_block(b
, val
->type
->members
[i
]),
1508 "Block and BufferBlock decorations cannot decorate a "
1509 "structure type that is nested at any level inside "
1510 "another structure type decorated with Block or "
1516 static nir_constant
*
1517 vtn_null_constant(struct vtn_builder
*b
, struct vtn_type
*type
)
1519 nir_constant
*c
= rzalloc(b
, nir_constant
);
1521 switch (type
->base_type
) {
1522 case vtn_base_type_scalar
:
1523 case vtn_base_type_vector
:
1524 /* Nothing to do here. It's already initialized to zero */
1527 case vtn_base_type_pointer
: {
1528 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1529 b
, type
->storage_class
, type
->deref
, NULL
);
1530 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
1532 const nir_const_value
*null_value
= nir_address_format_null_value(addr_format
);
1533 memcpy(c
->values
, null_value
,
1534 sizeof(nir_const_value
) * nir_address_format_num_components(addr_format
));
1538 case vtn_base_type_void
:
1539 case vtn_base_type_image
:
1540 case vtn_base_type_sampler
:
1541 case vtn_base_type_sampled_image
:
1542 case vtn_base_type_function
:
1543 /* For those we have to return something but it doesn't matter what. */
1546 case vtn_base_type_matrix
:
1547 case vtn_base_type_array
:
1548 vtn_assert(type
->length
> 0);
1549 c
->num_elements
= type
->length
;
1550 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1552 c
->elements
[0] = vtn_null_constant(b
, type
->array_element
);
1553 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1554 c
->elements
[i
] = c
->elements
[0];
1557 case vtn_base_type_struct
:
1558 c
->num_elements
= type
->length
;
1559 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1560 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
1561 c
->elements
[i
] = vtn_null_constant(b
, type
->members
[i
]);
1565 vtn_fail("Invalid type for null constant");
1572 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
1573 int member
, const struct vtn_decoration
*dec
,
1576 vtn_assert(member
== -1);
1577 if (dec
->decoration
!= SpvDecorationSpecId
)
1580 struct spec_constant_value
*const_value
= data
;
1582 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1583 if (b
->specializations
[i
].id
== dec
->operands
[0]) {
1584 if (const_value
->is_double
)
1585 const_value
->data64
= b
->specializations
[i
].data64
;
1587 const_value
->data32
= b
->specializations
[i
].data32
;
1594 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
1595 uint32_t const_value
)
1597 struct spec_constant_value data
;
1598 data
.is_double
= false;
1599 data
.data32
= const_value
;
1600 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1605 get_specialization64(struct vtn_builder
*b
, struct vtn_value
*val
,
1606 uint64_t const_value
)
1608 struct spec_constant_value data
;
1609 data
.is_double
= true;
1610 data
.data64
= const_value
;
1611 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1616 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1617 struct vtn_value
*val
,
1619 const struct vtn_decoration
*dec
,
1622 vtn_assert(member
== -1);
1623 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1624 dec
->operands
[0] != SpvBuiltInWorkgroupSize
)
1627 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1628 b
->workgroup_size_builtin
= val
;
1632 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1633 const uint32_t *w
, unsigned count
)
1635 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1636 val
->constant
= rzalloc(b
, nir_constant
);
1638 case SpvOpConstantTrue
:
1639 case SpvOpConstantFalse
:
1640 case SpvOpSpecConstantTrue
:
1641 case SpvOpSpecConstantFalse
: {
1642 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1643 "Result type of %s must be OpTypeBool",
1644 spirv_op_to_string(opcode
));
1646 uint32_t int_val
= (opcode
== SpvOpConstantTrue
||
1647 opcode
== SpvOpSpecConstantTrue
);
1649 if (opcode
== SpvOpSpecConstantTrue
||
1650 opcode
== SpvOpSpecConstantFalse
)
1651 int_val
= get_specialization(b
, val
, int_val
);
1653 val
->constant
->values
[0].b
= int_val
!= 0;
1657 case SpvOpConstant
: {
1658 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1659 "Result type of %s must be a scalar",
1660 spirv_op_to_string(opcode
));
1661 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1664 val
->constant
->values
[0].u64
= vtn_u64_literal(&w
[3]);
1667 val
->constant
->values
[0].u32
= w
[3];
1670 val
->constant
->values
[0].u16
= w
[3];
1673 val
->constant
->values
[0].u8
= w
[3];
1676 vtn_fail("Unsupported SpvOpConstant bit size: %u", bit_size
);
1681 case SpvOpSpecConstant
: {
1682 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1683 "Result type of %s must be a scalar",
1684 spirv_op_to_string(opcode
));
1685 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1688 val
->constant
->values
[0].u64
=
1689 get_specialization64(b
, val
, vtn_u64_literal(&w
[3]));
1692 val
->constant
->values
[0].u32
= get_specialization(b
, val
, w
[3]);
1695 val
->constant
->values
[0].u16
= get_specialization(b
, val
, w
[3]);
1698 val
->constant
->values
[0].u8
= get_specialization(b
, val
, w
[3]);
1701 vtn_fail("Unsupported SpvOpSpecConstant bit size");
1706 case SpvOpSpecConstantComposite
:
1707 case SpvOpConstantComposite
: {
1708 unsigned elem_count
= count
- 3;
1709 vtn_fail_if(elem_count
!= val
->type
->length
,
1710 "%s has %u constituents, expected %u",
1711 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1713 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1714 for (unsigned i
= 0; i
< elem_count
; i
++) {
1715 struct vtn_value
*val
= vtn_untyped_value(b
, w
[i
+ 3]);
1717 if (val
->value_type
== vtn_value_type_constant
) {
1718 elems
[i
] = val
->constant
;
1720 vtn_fail_if(val
->value_type
!= vtn_value_type_undef
,
1721 "only constants or undefs allowed for "
1722 "SpvOpConstantComposite");
1723 /* to make it easier, just insert a NULL constant for now */
1724 elems
[i
] = vtn_null_constant(b
, val
->type
);
1728 switch (val
->type
->base_type
) {
1729 case vtn_base_type_vector
: {
1730 assert(glsl_type_is_vector(val
->type
->type
));
1731 for (unsigned i
= 0; i
< elem_count
; i
++)
1732 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1736 case vtn_base_type_matrix
:
1737 case vtn_base_type_struct
:
1738 case vtn_base_type_array
:
1739 ralloc_steal(val
->constant
, elems
);
1740 val
->constant
->num_elements
= elem_count
;
1741 val
->constant
->elements
= elems
;
1745 vtn_fail("Result type of %s must be a composite type",
1746 spirv_op_to_string(opcode
));
1751 case SpvOpSpecConstantOp
: {
1752 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1754 case SpvOpVectorShuffle
: {
1755 struct vtn_value
*v0
= &b
->values
[w
[4]];
1756 struct vtn_value
*v1
= &b
->values
[w
[5]];
1758 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1759 v0
->value_type
== vtn_value_type_undef
);
1760 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1761 v1
->value_type
== vtn_value_type_undef
);
1763 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1764 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1766 vtn_assert(len0
+ len1
< 16);
1768 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1769 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1770 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1772 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1773 (void)bit_size0
; (void)bit_size1
;
1775 nir_const_value undef
= { .u64
= 0xdeadbeefdeadbeef };
1776 nir_const_value combined
[NIR_MAX_VEC_COMPONENTS
* 2];
1778 if (v0
->value_type
== vtn_value_type_constant
) {
1779 for (unsigned i
= 0; i
< len0
; i
++)
1780 combined
[i
] = v0
->constant
->values
[i
];
1782 if (v1
->value_type
== vtn_value_type_constant
) {
1783 for (unsigned i
= 0; i
< len1
; i
++)
1784 combined
[len0
+ i
] = v1
->constant
->values
[i
];
1787 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1788 uint32_t comp
= w
[i
+ 6];
1789 if (comp
== (uint32_t)-1) {
1790 /* If component is not used, set the value to a known constant
1791 * to detect if it is wrongly used.
1793 val
->constant
->values
[j
] = undef
;
1795 vtn_fail_if(comp
>= len0
+ len1
,
1796 "All Component literals must either be FFFFFFFF "
1797 "or in [0, N - 1] (inclusive).");
1798 val
->constant
->values
[j
] = combined
[comp
];
1804 case SpvOpCompositeExtract
:
1805 case SpvOpCompositeInsert
: {
1806 struct vtn_value
*comp
;
1807 unsigned deref_start
;
1808 struct nir_constant
**c
;
1809 if (opcode
== SpvOpCompositeExtract
) {
1810 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1812 c
= &comp
->constant
;
1814 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1816 val
->constant
= nir_constant_clone(comp
->constant
,
1822 const struct vtn_type
*type
= comp
->type
;
1823 for (unsigned i
= deref_start
; i
< count
; i
++) {
1824 vtn_fail_if(w
[i
] > type
->length
,
1825 "%uth index of %s is %u but the type has only "
1826 "%u elements", i
- deref_start
,
1827 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1829 switch (type
->base_type
) {
1830 case vtn_base_type_vector
:
1832 type
= type
->array_element
;
1835 case vtn_base_type_matrix
:
1836 case vtn_base_type_array
:
1837 c
= &(*c
)->elements
[w
[i
]];
1838 type
= type
->array_element
;
1841 case vtn_base_type_struct
:
1842 c
= &(*c
)->elements
[w
[i
]];
1843 type
= type
->members
[w
[i
]];
1847 vtn_fail("%s must only index into composite types",
1848 spirv_op_to_string(opcode
));
1852 if (opcode
== SpvOpCompositeExtract
) {
1856 unsigned num_components
= type
->length
;
1857 for (unsigned i
= 0; i
< num_components
; i
++)
1858 val
->constant
->values
[i
] = (*c
)->values
[elem
+ i
];
1861 struct vtn_value
*insert
=
1862 vtn_value(b
, w
[4], vtn_value_type_constant
);
1863 vtn_assert(insert
->type
== type
);
1865 *c
= insert
->constant
;
1867 unsigned num_components
= type
->length
;
1868 for (unsigned i
= 0; i
< num_components
; i
++)
1869 (*c
)->values
[elem
+ i
] = insert
->constant
->values
[i
];
1877 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
1878 nir_alu_type src_alu_type
= dst_alu_type
;
1879 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
1882 vtn_assert(count
<= 7);
1888 /* We have a source in a conversion */
1890 nir_get_nir_type_for_glsl_type(
1891 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1892 /* We use the bitsize of the conversion source to evaluate the opcode later */
1893 bit_size
= glsl_get_bit_size(
1894 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1897 bit_size
= glsl_get_bit_size(val
->type
->type
);
1900 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
1901 nir_alu_type_get_type_size(src_alu_type
),
1902 nir_alu_type_get_type_size(dst_alu_type
));
1903 nir_const_value src
[3][NIR_MAX_VEC_COMPONENTS
];
1905 for (unsigned i
= 0; i
< count
- 4; i
++) {
1906 struct vtn_value
*src_val
=
1907 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
);
1909 /* If this is an unsized source, pull the bit size from the
1910 * source; otherwise, we'll use the bit size from the destination.
1912 if (!nir_alu_type_get_type_size(nir_op_infos
[op
].input_types
[i
]))
1913 bit_size
= glsl_get_bit_size(src_val
->type
->type
);
1915 unsigned src_comps
= nir_op_infos
[op
].input_sizes
[i
] ?
1916 nir_op_infos
[op
].input_sizes
[i
] :
1919 unsigned j
= swap
? 1 - i
: i
;
1920 for (unsigned c
= 0; c
< src_comps
; c
++)
1921 src
[j
][c
] = src_val
->constant
->values
[c
];
1924 /* fix up fixed size sources */
1931 for (unsigned i
= 0; i
< num_components
; ++i
) {
1933 case 64: src
[1][i
].u32
= src
[1][i
].u64
; break;
1934 case 16: src
[1][i
].u32
= src
[1][i
].u16
; break;
1935 case 8: src
[1][i
].u32
= src
[1][i
].u8
; break;
1944 nir_const_value
*srcs
[3] = {
1945 src
[0], src
[1], src
[2],
1947 nir_eval_const_opcode(op
, val
->constant
->values
, num_components
, bit_size
, srcs
);
1954 case SpvOpConstantNull
:
1955 val
->constant
= vtn_null_constant(b
, val
->type
);
1958 case SpvOpConstantSampler
:
1959 vtn_fail("OpConstantSampler requires Kernel Capability");
1963 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1966 /* Now that we have the value, update the workgroup size if needed */
1967 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1970 struct vtn_ssa_value
*
1971 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1973 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1976 if (!glsl_type_is_vector_or_scalar(type
)) {
1977 unsigned elems
= glsl_get_length(type
);
1978 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1979 for (unsigned i
= 0; i
< elems
; i
++) {
1980 const struct glsl_type
*child_type
;
1982 switch (glsl_get_base_type(type
)) {
1984 case GLSL_TYPE_UINT
:
1985 case GLSL_TYPE_INT16
:
1986 case GLSL_TYPE_UINT16
:
1987 case GLSL_TYPE_UINT8
:
1988 case GLSL_TYPE_INT8
:
1989 case GLSL_TYPE_INT64
:
1990 case GLSL_TYPE_UINT64
:
1991 case GLSL_TYPE_BOOL
:
1992 case GLSL_TYPE_FLOAT
:
1993 case GLSL_TYPE_FLOAT16
:
1994 case GLSL_TYPE_DOUBLE
:
1995 child_type
= glsl_get_column_type(type
);
1997 case GLSL_TYPE_ARRAY
:
1998 child_type
= glsl_get_array_element(type
);
2000 case GLSL_TYPE_STRUCT
:
2001 case GLSL_TYPE_INTERFACE
:
2002 child_type
= glsl_get_struct_field(type
, i
);
2005 vtn_fail("unkown base type");
2008 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
2016 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
2019 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
2020 src
.src_type
= type
;
2025 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
2026 const uint32_t *w
, unsigned count
)
2028 if (opcode
== SpvOpSampledImage
) {
2029 struct vtn_value
*val
=
2030 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2031 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2032 val
->sampled_image
->type
=
2033 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2034 val
->sampled_image
->image
=
2035 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2036 val
->sampled_image
->sampler
=
2037 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
2039 } else if (opcode
== SpvOpImage
) {
2040 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2041 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
2042 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
2043 val
->pointer
= src_val
->sampled_image
->image
;
2045 vtn_assert(src_val
->value_type
== vtn_value_type_pointer
);
2046 val
->pointer
= src_val
->pointer
;
2051 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2052 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2054 struct vtn_sampled_image sampled
;
2055 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
2056 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
2057 sampled
= *sampled_val
->sampled_image
;
2059 vtn_assert(sampled_val
->value_type
== vtn_value_type_pointer
);
2060 sampled
.type
= sampled_val
->pointer
->type
;
2061 sampled
.image
= NULL
;
2062 sampled
.sampler
= sampled_val
->pointer
;
2065 const struct glsl_type
*image_type
= sampled
.type
->type
;
2066 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
2067 const bool is_array
= glsl_sampler_type_is_array(image_type
);
2069 /* Figure out the base texture operation */
2072 case SpvOpImageSampleImplicitLod
:
2073 case SpvOpImageSampleDrefImplicitLod
:
2074 case SpvOpImageSampleProjImplicitLod
:
2075 case SpvOpImageSampleProjDrefImplicitLod
:
2076 texop
= nir_texop_tex
;
2079 case SpvOpImageSampleExplicitLod
:
2080 case SpvOpImageSampleDrefExplicitLod
:
2081 case SpvOpImageSampleProjExplicitLod
:
2082 case SpvOpImageSampleProjDrefExplicitLod
:
2083 texop
= nir_texop_txl
;
2086 case SpvOpImageFetch
:
2087 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
2088 texop
= nir_texop_txf_ms
;
2090 texop
= nir_texop_txf
;
2094 case SpvOpImageGather
:
2095 case SpvOpImageDrefGather
:
2096 texop
= nir_texop_tg4
;
2099 case SpvOpImageQuerySizeLod
:
2100 case SpvOpImageQuerySize
:
2101 texop
= nir_texop_txs
;
2104 case SpvOpImageQueryLod
:
2105 texop
= nir_texop_lod
;
2108 case SpvOpImageQueryLevels
:
2109 texop
= nir_texop_query_levels
;
2112 case SpvOpImageQuerySamples
:
2113 texop
= nir_texop_texture_samples
;
2117 vtn_fail_with_opcode("Unhandled opcode", opcode
);
2120 nir_tex_src srcs
[10]; /* 10 should be enough */
2121 nir_tex_src
*p
= srcs
;
2123 nir_deref_instr
*sampler
= vtn_pointer_to_deref(b
, sampled
.sampler
);
2124 nir_deref_instr
*texture
=
2125 sampled
.image
? vtn_pointer_to_deref(b
, sampled
.image
) : sampler
;
2127 p
->src
= nir_src_for_ssa(&texture
->dest
.ssa
);
2128 p
->src_type
= nir_tex_src_texture_deref
;
2138 /* These operations require a sampler */
2139 p
->src
= nir_src_for_ssa(&sampler
->dest
.ssa
);
2140 p
->src_type
= nir_tex_src_sampler_deref
;
2144 case nir_texop_txf_ms
:
2146 case nir_texop_query_levels
:
2147 case nir_texop_texture_samples
:
2148 case nir_texop_samples_identical
:
2151 case nir_texop_txf_ms_fb
:
2152 vtn_fail("unexpected nir_texop_txf_ms_fb");
2154 case nir_texop_txf_ms_mcs
:
2155 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2160 struct nir_ssa_def
*coord
;
2161 unsigned coord_components
;
2163 case SpvOpImageSampleImplicitLod
:
2164 case SpvOpImageSampleExplicitLod
:
2165 case SpvOpImageSampleDrefImplicitLod
:
2166 case SpvOpImageSampleDrefExplicitLod
:
2167 case SpvOpImageSampleProjImplicitLod
:
2168 case SpvOpImageSampleProjExplicitLod
:
2169 case SpvOpImageSampleProjDrefImplicitLod
:
2170 case SpvOpImageSampleProjDrefExplicitLod
:
2171 case SpvOpImageFetch
:
2172 case SpvOpImageGather
:
2173 case SpvOpImageDrefGather
:
2174 case SpvOpImageQueryLod
: {
2175 /* All these types have the coordinate as their first real argument */
2176 switch (sampler_dim
) {
2177 case GLSL_SAMPLER_DIM_1D
:
2178 case GLSL_SAMPLER_DIM_BUF
:
2179 coord_components
= 1;
2181 case GLSL_SAMPLER_DIM_2D
:
2182 case GLSL_SAMPLER_DIM_RECT
:
2183 case GLSL_SAMPLER_DIM_MS
:
2184 coord_components
= 2;
2186 case GLSL_SAMPLER_DIM_3D
:
2187 case GLSL_SAMPLER_DIM_CUBE
:
2188 coord_components
= 3;
2191 vtn_fail("Invalid sampler type");
2194 if (is_array
&& texop
!= nir_texop_lod
)
2197 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
2198 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2199 (1 << coord_components
) - 1));
2200 p
->src_type
= nir_tex_src_coord
;
2207 coord_components
= 0;
2212 case SpvOpImageSampleProjImplicitLod
:
2213 case SpvOpImageSampleProjExplicitLod
:
2214 case SpvOpImageSampleProjDrefImplicitLod
:
2215 case SpvOpImageSampleProjDrefExplicitLod
:
2216 /* These have the projector as the last coordinate component */
2217 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2218 p
->src_type
= nir_tex_src_projector
;
2226 bool is_shadow
= false;
2227 unsigned gather_component
= 0;
2229 case SpvOpImageSampleDrefImplicitLod
:
2230 case SpvOpImageSampleDrefExplicitLod
:
2231 case SpvOpImageSampleProjDrefImplicitLod
:
2232 case SpvOpImageSampleProjDrefExplicitLod
:
2233 case SpvOpImageDrefGather
:
2234 /* These all have an explicit depth value as their next source */
2236 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2239 case SpvOpImageGather
:
2240 /* This has a component as its next source */
2241 gather_component
= vtn_constant_uint(b
, w
[idx
++]);
2248 /* For OpImageQuerySizeLod, we always have an LOD */
2249 if (opcode
== SpvOpImageQuerySizeLod
)
2250 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2252 /* Now we need to handle some number of optional arguments */
2253 struct vtn_value
*gather_offsets
= NULL
;
2255 uint32_t operands
= w
[idx
++];
2257 if (operands
& SpvImageOperandsBiasMask
) {
2258 vtn_assert(texop
== nir_texop_tex
);
2259 texop
= nir_texop_txb
;
2260 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
2263 if (operands
& SpvImageOperandsLodMask
) {
2264 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2265 texop
== nir_texop_txs
);
2266 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2269 if (operands
& SpvImageOperandsGradMask
) {
2270 vtn_assert(texop
== nir_texop_txl
);
2271 texop
= nir_texop_txd
;
2272 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
2273 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
2276 if (operands
& SpvImageOperandsOffsetMask
||
2277 operands
& SpvImageOperandsConstOffsetMask
)
2278 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
2280 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2281 vtn_assert(texop
== nir_texop_tg4
);
2282 gather_offsets
= vtn_value(b
, w
[idx
++], vtn_value_type_constant
);
2285 if (operands
& SpvImageOperandsSampleMask
) {
2286 vtn_assert(texop
== nir_texop_txf_ms
);
2287 texop
= nir_texop_txf_ms
;
2288 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
2291 if (operands
& SpvImageOperandsMinLodMask
) {
2292 vtn_assert(texop
== nir_texop_tex
||
2293 texop
== nir_texop_txb
||
2294 texop
== nir_texop_txd
);
2295 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_min_lod
);
2298 /* We should have now consumed exactly all of the arguments */
2299 vtn_assert(idx
== count
);
2301 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2304 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2306 instr
->coord_components
= coord_components
;
2307 instr
->sampler_dim
= sampler_dim
;
2308 instr
->is_array
= is_array
;
2309 instr
->is_shadow
= is_shadow
;
2310 instr
->is_new_style_shadow
=
2311 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2312 instr
->component
= gather_component
;
2314 if (sampled
.image
&& (sampled
.image
->access
& ACCESS_NON_UNIFORM
))
2315 instr
->texture_non_uniform
= true;
2317 if (sampled
.sampler
&& (sampled
.sampler
->access
& ACCESS_NON_UNIFORM
))
2318 instr
->sampler_non_uniform
= true;
2320 switch (glsl_get_sampler_result_type(image_type
)) {
2321 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
2322 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
2323 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
2324 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
2326 vtn_fail("Invalid base type for sampler result");
2329 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2330 nir_tex_instr_dest_size(instr
), 32, NULL
);
2332 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2333 nir_tex_instr_dest_size(instr
));
2335 if (gather_offsets
) {
2336 vtn_fail_if(gather_offsets
->type
->base_type
!= vtn_base_type_array
||
2337 gather_offsets
->type
->length
!= 4,
2338 "ConstOffsets must be an array of size four of vectors "
2339 "of two integer components");
2341 struct vtn_type
*vec_type
= gather_offsets
->type
->array_element
;
2342 vtn_fail_if(vec_type
->base_type
!= vtn_base_type_vector
||
2343 vec_type
->length
!= 2 ||
2344 !glsl_type_is_integer(vec_type
->type
),
2345 "ConstOffsets must be an array of size four of vectors "
2346 "of two integer components");
2348 unsigned bit_size
= glsl_get_bit_size(vec_type
->type
);
2349 for (uint32_t i
= 0; i
< 4; i
++) {
2350 const nir_const_value
*cvec
=
2351 gather_offsets
->constant
->elements
[i
]->values
;
2352 for (uint32_t j
= 0; j
< 2; j
++) {
2354 case 8: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i8
; break;
2355 case 16: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i16
; break;
2356 case 32: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i32
; break;
2357 case 64: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i64
; break;
2359 vtn_fail("Unsupported bit size: %u", bit_size
);
2365 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
2366 val
->ssa
->def
= &instr
->dest
.ssa
;
2368 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2372 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2373 const uint32_t *w
, nir_src
*src
)
2376 case SpvOpAtomicIIncrement
:
2377 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
2380 case SpvOpAtomicIDecrement
:
2381 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
2384 case SpvOpAtomicISub
:
2386 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
2389 case SpvOpAtomicCompareExchange
:
2390 case SpvOpAtomicCompareExchangeWeak
:
2391 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
2392 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
2395 case SpvOpAtomicExchange
:
2396 case SpvOpAtomicIAdd
:
2397 case SpvOpAtomicSMin
:
2398 case SpvOpAtomicUMin
:
2399 case SpvOpAtomicSMax
:
2400 case SpvOpAtomicUMax
:
2401 case SpvOpAtomicAnd
:
2403 case SpvOpAtomicXor
:
2404 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
2408 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2412 static nir_ssa_def
*
2413 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2415 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
2417 /* The image_load_store intrinsics assume a 4-dim coordinate */
2418 unsigned dim
= glsl_get_vector_elements(coord
->type
);
2419 unsigned swizzle
[4];
2420 for (unsigned i
= 0; i
< 4; i
++)
2421 swizzle
[i
] = MIN2(i
, dim
- 1);
2423 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4);
2426 static nir_ssa_def
*
2427 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2429 if (value
->num_components
== 4)
2433 for (unsigned i
= 0; i
< 4; i
++)
2434 swiz
[i
] = i
< value
->num_components
? i
: 0;
2435 return nir_swizzle(b
, value
, swiz
, 4);
2439 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2440 const uint32_t *w
, unsigned count
)
2442 /* Just get this one out of the way */
2443 if (opcode
== SpvOpImageTexelPointer
) {
2444 struct vtn_value
*val
=
2445 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2446 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2448 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2449 val
->image
->coord
= get_image_coord(b
, w
[4]);
2450 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
2454 struct vtn_image_pointer image
;
2457 case SpvOpAtomicExchange
:
2458 case SpvOpAtomicCompareExchange
:
2459 case SpvOpAtomicCompareExchangeWeak
:
2460 case SpvOpAtomicIIncrement
:
2461 case SpvOpAtomicIDecrement
:
2462 case SpvOpAtomicIAdd
:
2463 case SpvOpAtomicISub
:
2464 case SpvOpAtomicLoad
:
2465 case SpvOpAtomicSMin
:
2466 case SpvOpAtomicUMin
:
2467 case SpvOpAtomicSMax
:
2468 case SpvOpAtomicUMax
:
2469 case SpvOpAtomicAnd
:
2471 case SpvOpAtomicXor
:
2472 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
2475 case SpvOpAtomicStore
:
2476 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
2479 case SpvOpImageQuerySize
:
2480 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2482 image
.sample
= NULL
;
2485 case SpvOpImageRead
:
2486 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2487 image
.coord
= get_image_coord(b
, w
[4]);
2489 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
2490 vtn_assert(w
[5] == SpvImageOperandsSampleMask
);
2491 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
2493 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2497 case SpvOpImageWrite
:
2498 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2499 image
.coord
= get_image_coord(b
, w
[2]);
2503 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
2504 vtn_assert(w
[4] == SpvImageOperandsSampleMask
);
2505 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
2507 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2512 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2515 nir_intrinsic_op op
;
2517 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
2518 OP(ImageQuerySize
, size
)
2520 OP(ImageWrite
, store
)
2521 OP(AtomicLoad
, load
)
2522 OP(AtomicStore
, store
)
2523 OP(AtomicExchange
, atomic_exchange
)
2524 OP(AtomicCompareExchange
, atomic_comp_swap
)
2525 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2526 OP(AtomicIIncrement
, atomic_add
)
2527 OP(AtomicIDecrement
, atomic_add
)
2528 OP(AtomicIAdd
, atomic_add
)
2529 OP(AtomicISub
, atomic_add
)
2530 OP(AtomicSMin
, atomic_min
)
2531 OP(AtomicUMin
, atomic_min
)
2532 OP(AtomicSMax
, atomic_max
)
2533 OP(AtomicUMax
, atomic_max
)
2534 OP(AtomicAnd
, atomic_and
)
2535 OP(AtomicOr
, atomic_or
)
2536 OP(AtomicXor
, atomic_xor
)
2539 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2542 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
2544 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
2545 intrin
->src
[0] = nir_src_for_ssa(&image_deref
->dest
.ssa
);
2547 /* ImageQuerySize doesn't take any extra parameters */
2548 if (opcode
!= SpvOpImageQuerySize
) {
2549 /* The image coordinate is always 4 components but we may not have that
2550 * many. Swizzle to compensate.
2552 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
2553 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
2557 case SpvOpAtomicLoad
:
2558 case SpvOpImageQuerySize
:
2559 case SpvOpImageRead
:
2561 case SpvOpAtomicStore
:
2562 case SpvOpImageWrite
: {
2563 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
2564 nir_ssa_def
*value
= vtn_ssa_value(b
, value_id
)->def
;
2565 /* nir_intrinsic_image_deref_store always takes a vec4 value */
2566 assert(op
== nir_intrinsic_image_deref_store
);
2567 intrin
->num_components
= 4;
2568 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
));
2572 case SpvOpAtomicCompareExchange
:
2573 case SpvOpAtomicCompareExchangeWeak
:
2574 case SpvOpAtomicIIncrement
:
2575 case SpvOpAtomicIDecrement
:
2576 case SpvOpAtomicExchange
:
2577 case SpvOpAtomicIAdd
:
2578 case SpvOpAtomicISub
:
2579 case SpvOpAtomicSMin
:
2580 case SpvOpAtomicUMin
:
2581 case SpvOpAtomicSMax
:
2582 case SpvOpAtomicUMax
:
2583 case SpvOpAtomicAnd
:
2585 case SpvOpAtomicXor
:
2586 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
2590 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2593 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
2594 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2595 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2597 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
2598 intrin
->num_components
= nir_intrinsic_infos
[op
].dest_components
;
2599 if (intrin
->num_components
== 0)
2600 intrin
->num_components
= dest_components
;
2602 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2603 intrin
->num_components
, 32, NULL
);
2605 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2607 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
2608 if (intrin
->num_components
!= dest_components
)
2609 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
2611 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
2612 val
->ssa
->def
= result
;
2614 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2618 static nir_intrinsic_op
2619 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2622 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2623 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2624 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2625 OP(AtomicExchange
, atomic_exchange
)
2626 OP(AtomicCompareExchange
, atomic_comp_swap
)
2627 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2628 OP(AtomicIIncrement
, atomic_add
)
2629 OP(AtomicIDecrement
, atomic_add
)
2630 OP(AtomicIAdd
, atomic_add
)
2631 OP(AtomicISub
, atomic_add
)
2632 OP(AtomicSMin
, atomic_imin
)
2633 OP(AtomicUMin
, atomic_umin
)
2634 OP(AtomicSMax
, atomic_imax
)
2635 OP(AtomicUMax
, atomic_umax
)
2636 OP(AtomicAnd
, atomic_and
)
2637 OP(AtomicOr
, atomic_or
)
2638 OP(AtomicXor
, atomic_xor
)
2641 vtn_fail_with_opcode("Invalid SSBO atomic", opcode
);
2645 static nir_intrinsic_op
2646 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2649 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
2650 OP(AtomicLoad
, read_deref
)
2651 OP(AtomicExchange
, exchange
)
2652 OP(AtomicCompareExchange
, comp_swap
)
2653 OP(AtomicCompareExchangeWeak
, comp_swap
)
2654 OP(AtomicIIncrement
, inc_deref
)
2655 OP(AtomicIDecrement
, post_dec_deref
)
2656 OP(AtomicIAdd
, add_deref
)
2657 OP(AtomicISub
, add_deref
)
2658 OP(AtomicUMin
, min_deref
)
2659 OP(AtomicUMax
, max_deref
)
2660 OP(AtomicAnd
, and_deref
)
2661 OP(AtomicOr
, or_deref
)
2662 OP(AtomicXor
, xor_deref
)
2665 /* We left the following out: AtomicStore, AtomicSMin and
2666 * AtomicSmax. Right now there are not nir intrinsics for them. At this
2667 * moment Atomic Counter support is needed for ARB_spirv support, so is
2668 * only need to support GLSL Atomic Counters that are uints and don't
2669 * allow direct storage.
2671 unreachable("Invalid uniform atomic");
2675 static nir_intrinsic_op
2676 get_shared_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2679 case SpvOpAtomicLoad
: return nir_intrinsic_load_shared
;
2680 case SpvOpAtomicStore
: return nir_intrinsic_store_shared
;
2681 #define OP(S, N) case SpvOp##S: return nir_intrinsic_shared_##N;
2682 OP(AtomicExchange
, atomic_exchange
)
2683 OP(AtomicCompareExchange
, atomic_comp_swap
)
2684 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2685 OP(AtomicIIncrement
, atomic_add
)
2686 OP(AtomicIDecrement
, atomic_add
)
2687 OP(AtomicIAdd
, atomic_add
)
2688 OP(AtomicISub
, atomic_add
)
2689 OP(AtomicSMin
, atomic_imin
)
2690 OP(AtomicUMin
, atomic_umin
)
2691 OP(AtomicSMax
, atomic_imax
)
2692 OP(AtomicUMax
, atomic_umax
)
2693 OP(AtomicAnd
, atomic_and
)
2694 OP(AtomicOr
, atomic_or
)
2695 OP(AtomicXor
, atomic_xor
)
2698 vtn_fail_with_opcode("Invalid shared atomic", opcode
);
2702 static nir_intrinsic_op
2703 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2706 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
2707 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
2708 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
2709 OP(AtomicExchange
, atomic_exchange
)
2710 OP(AtomicCompareExchange
, atomic_comp_swap
)
2711 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2712 OP(AtomicIIncrement
, atomic_add
)
2713 OP(AtomicIDecrement
, atomic_add
)
2714 OP(AtomicIAdd
, atomic_add
)
2715 OP(AtomicISub
, atomic_add
)
2716 OP(AtomicSMin
, atomic_imin
)
2717 OP(AtomicUMin
, atomic_umin
)
2718 OP(AtomicSMax
, atomic_imax
)
2719 OP(AtomicUMax
, atomic_umax
)
2720 OP(AtomicAnd
, atomic_and
)
2721 OP(AtomicOr
, atomic_or
)
2722 OP(AtomicXor
, atomic_xor
)
2725 vtn_fail_with_opcode("Invalid shared atomic", opcode
);
2730 * Handles shared atomics, ssbo atomics and atomic counters.
2733 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
2734 const uint32_t *w
, unsigned count
)
2736 struct vtn_pointer
*ptr
;
2737 nir_intrinsic_instr
*atomic
;
2740 case SpvOpAtomicLoad
:
2741 case SpvOpAtomicExchange
:
2742 case SpvOpAtomicCompareExchange
:
2743 case SpvOpAtomicCompareExchangeWeak
:
2744 case SpvOpAtomicIIncrement
:
2745 case SpvOpAtomicIDecrement
:
2746 case SpvOpAtomicIAdd
:
2747 case SpvOpAtomicISub
:
2748 case SpvOpAtomicSMin
:
2749 case SpvOpAtomicUMin
:
2750 case SpvOpAtomicSMax
:
2751 case SpvOpAtomicUMax
:
2752 case SpvOpAtomicAnd
:
2754 case SpvOpAtomicXor
:
2755 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2758 case SpvOpAtomicStore
:
2759 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2763 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2767 SpvScope scope = w[4];
2768 SpvMemorySemanticsMask semantics = w[5];
2771 /* uniform as "atomic counter uniform" */
2772 if (ptr
->mode
== vtn_variable_mode_uniform
) {
2773 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2774 const struct glsl_type
*deref_type
= deref
->type
;
2775 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
2776 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2777 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2779 /* SSBO needs to initialize index/offset. In this case we don't need to,
2780 * as that info is already stored on the ptr->var->var nir_variable (see
2781 * vtn_create_variable)
2785 case SpvOpAtomicLoad
:
2786 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2789 case SpvOpAtomicStore
:
2790 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2791 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2794 case SpvOpAtomicExchange
:
2795 case SpvOpAtomicCompareExchange
:
2796 case SpvOpAtomicCompareExchangeWeak
:
2797 case SpvOpAtomicIIncrement
:
2798 case SpvOpAtomicIDecrement
:
2799 case SpvOpAtomicIAdd
:
2800 case SpvOpAtomicISub
:
2801 case SpvOpAtomicSMin
:
2802 case SpvOpAtomicUMin
:
2803 case SpvOpAtomicSMax
:
2804 case SpvOpAtomicUMax
:
2805 case SpvOpAtomicAnd
:
2807 case SpvOpAtomicXor
:
2808 /* Nothing: we don't need to call fill_common_atomic_sources here, as
2809 * atomic counter uniforms doesn't have sources
2814 unreachable("Invalid SPIR-V atomic");
2817 } else if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
2818 nir_ssa_def
*offset
, *index
;
2819 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
2821 nir_intrinsic_op op
;
2822 if (ptr
->mode
== vtn_variable_mode_ssbo
) {
2823 op
= get_ssbo_nir_atomic_op(b
, opcode
);
2825 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
&&
2826 b
->options
->lower_workgroup_access_to_offsets
);
2827 op
= get_shared_nir_atomic_op(b
, opcode
);
2830 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2834 case SpvOpAtomicLoad
:
2835 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2836 nir_intrinsic_set_align(atomic
, 4, 0);
2837 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2838 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2839 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2842 case SpvOpAtomicStore
:
2843 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2844 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2845 nir_intrinsic_set_align(atomic
, 4, 0);
2846 atomic
->src
[src
++] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2847 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2848 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2849 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2852 case SpvOpAtomicExchange
:
2853 case SpvOpAtomicCompareExchange
:
2854 case SpvOpAtomicCompareExchangeWeak
:
2855 case SpvOpAtomicIIncrement
:
2856 case SpvOpAtomicIDecrement
:
2857 case SpvOpAtomicIAdd
:
2858 case SpvOpAtomicISub
:
2859 case SpvOpAtomicSMin
:
2860 case SpvOpAtomicUMin
:
2861 case SpvOpAtomicSMax
:
2862 case SpvOpAtomicUMax
:
2863 case SpvOpAtomicAnd
:
2865 case SpvOpAtomicXor
:
2866 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2867 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2868 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2869 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
2873 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2876 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2877 const struct glsl_type
*deref_type
= deref
->type
;
2878 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
2879 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2880 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2883 case SpvOpAtomicLoad
:
2884 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2887 case SpvOpAtomicStore
:
2888 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2889 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2890 atomic
->src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2893 case SpvOpAtomicExchange
:
2894 case SpvOpAtomicCompareExchange
:
2895 case SpvOpAtomicCompareExchangeWeak
:
2896 case SpvOpAtomicIIncrement
:
2897 case SpvOpAtomicIDecrement
:
2898 case SpvOpAtomicIAdd
:
2899 case SpvOpAtomicISub
:
2900 case SpvOpAtomicSMin
:
2901 case SpvOpAtomicUMin
:
2902 case SpvOpAtomicSMax
:
2903 case SpvOpAtomicUMax
:
2904 case SpvOpAtomicAnd
:
2906 case SpvOpAtomicXor
:
2907 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
2911 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2915 if (opcode
!= SpvOpAtomicStore
) {
2916 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2918 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
2919 glsl_get_vector_elements(type
->type
),
2920 glsl_get_bit_size(type
->type
), NULL
);
2922 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2923 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
2924 val
->ssa
->def
= &atomic
->dest
.ssa
;
2925 val
->ssa
->type
= type
->type
;
2928 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
2931 static nir_alu_instr
*
2932 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
2934 nir_op op
= nir_op_vec(num_components
);
2935 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
2936 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
2938 vec
->dest
.write_mask
= (1 << num_components
) - 1;
2943 struct vtn_ssa_value
*
2944 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
2946 if (src
->transposed
)
2947 return src
->transposed
;
2949 struct vtn_ssa_value
*dest
=
2950 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
2952 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
2953 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
2954 glsl_get_bit_size(src
->type
));
2955 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2956 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
2957 vec
->src
[0].swizzle
[0] = i
;
2959 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
2960 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
2961 vec
->src
[j
].swizzle
[0] = i
;
2964 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2965 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
2968 dest
->transposed
= src
;
2974 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
2976 return nir_channel(&b
->nb
, src
, index
);
2980 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
2983 nir_alu_instr
*vec
= create_vec(b
, src
->num_components
,
2986 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
2988 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
2990 vec
->src
[i
].src
= nir_src_for_ssa(src
);
2991 vec
->src
[i
].swizzle
[0] = i
;
2995 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2997 return &vec
->dest
.dest
.ssa
;
3000 static nir_ssa_def
*
3001 nir_ieq_imm(nir_builder
*b
, nir_ssa_def
*x
, uint64_t i
)
3003 return nir_ieq(b
, x
, nir_imm_intN_t(b
, i
, x
->bit_size
));
3007 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
3010 return nir_vector_extract(&b
->nb
, src
, nir_i2i(&b
->nb
, index
, 32));
3014 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
3015 nir_ssa_def
*insert
, nir_ssa_def
*index
)
3017 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
3018 for (unsigned i
= 1; i
< src
->num_components
; i
++)
3019 dest
= nir_bcsel(&b
->nb
, nir_ieq_imm(&b
->nb
, index
, i
),
3020 vtn_vector_insert(b
, src
, insert
, i
), dest
);
3025 static nir_ssa_def
*
3026 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
3027 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
3028 const uint32_t *indices
)
3030 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
3032 for (unsigned i
= 0; i
< num_components
; i
++) {
3033 uint32_t index
= indices
[i
];
3034 if (index
== 0xffffffff) {
3036 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
3037 } else if (index
< src0
->num_components
) {
3038 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
3039 vec
->src
[i
].swizzle
[0] = index
;
3041 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
3042 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
3046 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3048 return &vec
->dest
.dest
.ssa
;
3052 * Concatentates a number of vectors/scalars together to produce a vector
3054 static nir_ssa_def
*
3055 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
3056 unsigned num_srcs
, nir_ssa_def
**srcs
)
3058 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
3060 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3062 * "When constructing a vector, there must be at least two Constituent
3065 vtn_assert(num_srcs
>= 2);
3067 unsigned dest_idx
= 0;
3068 for (unsigned i
= 0; i
< num_srcs
; i
++) {
3069 nir_ssa_def
*src
= srcs
[i
];
3070 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
3071 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
3072 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
3073 vec
->src
[dest_idx
].swizzle
[0] = j
;
3078 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3080 * "When constructing a vector, the total number of components in all
3081 * the operands must equal the number of components in Result Type."
3083 vtn_assert(dest_idx
== num_components
);
3085 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3087 return &vec
->dest
.dest
.ssa
;
3090 static struct vtn_ssa_value
*
3091 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3093 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3094 dest
->type
= src
->type
;
3096 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3097 dest
->def
= src
->def
;
3099 unsigned elems
= glsl_get_length(src
->type
);
3101 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3102 for (unsigned i
= 0; i
< elems
; i
++)
3103 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3109 static struct vtn_ssa_value
*
3110 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3111 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3112 unsigned num_indices
)
3114 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3116 struct vtn_ssa_value
*cur
= dest
;
3118 for (i
= 0; i
< num_indices
- 1; i
++) {
3119 cur
= cur
->elems
[indices
[i
]];
3122 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3123 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3124 * the component granularity. In that case, the last index will be
3125 * the index to insert the scalar into the vector.
3128 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
3130 cur
->elems
[indices
[i
]] = insert
;
3136 static struct vtn_ssa_value
*
3137 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3138 const uint32_t *indices
, unsigned num_indices
)
3140 struct vtn_ssa_value
*cur
= src
;
3141 for (unsigned i
= 0; i
< num_indices
; i
++) {
3142 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3143 vtn_assert(i
== num_indices
- 1);
3144 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3145 * the component granularity. The last index will be the index of the
3146 * vector to extract.
3149 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
3150 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
3151 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
3154 cur
= cur
->elems
[indices
[i
]];
3162 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3163 const uint32_t *w
, unsigned count
)
3165 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
3166 const struct glsl_type
*type
=
3167 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
3168 val
->ssa
= vtn_create_ssa_value(b
, type
);
3171 case SpvOpVectorExtractDynamic
:
3172 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3173 vtn_ssa_value(b
, w
[4])->def
);
3176 case SpvOpVectorInsertDynamic
:
3177 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3178 vtn_ssa_value(b
, w
[4])->def
,
3179 vtn_ssa_value(b
, w
[5])->def
);
3182 case SpvOpVectorShuffle
:
3183 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
3184 vtn_ssa_value(b
, w
[3])->def
,
3185 vtn_ssa_value(b
, w
[4])->def
,
3189 case SpvOpCompositeConstruct
: {
3190 unsigned elems
= count
- 3;
3192 if (glsl_type_is_vector_or_scalar(type
)) {
3193 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3194 for (unsigned i
= 0; i
< elems
; i
++)
3195 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
3197 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
3200 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3201 for (unsigned i
= 0; i
< elems
; i
++)
3202 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3206 case SpvOpCompositeExtract
:
3207 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3211 case SpvOpCompositeInsert
:
3212 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3213 vtn_ssa_value(b
, w
[3]),
3217 case SpvOpCopyLogical
:
3218 case SpvOpCopyObject
:
3219 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3223 vtn_fail_with_opcode("unknown composite operation", opcode
);
3228 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3230 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3231 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3235 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3236 SpvMemorySemanticsMask semantics
)
3238 static const SpvMemorySemanticsMask all_memory_semantics
=
3239 SpvMemorySemanticsUniformMemoryMask
|
3240 SpvMemorySemanticsWorkgroupMemoryMask
|
3241 SpvMemorySemanticsAtomicCounterMemoryMask
|
3242 SpvMemorySemanticsImageMemoryMask
;
3244 /* If we're not actually doing a memory barrier, bail */
3245 if (!(semantics
& all_memory_semantics
))
3248 /* GL and Vulkan don't have these */
3249 vtn_assert(scope
!= SpvScopeCrossDevice
);
3251 if (scope
== SpvScopeSubgroup
)
3252 return; /* Nothing to do here */
3254 if (scope
== SpvScopeWorkgroup
) {
3255 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3259 /* There's only two scopes thing left */
3260 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3262 if ((semantics
& all_memory_semantics
) == all_memory_semantics
) {
3263 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3267 /* Issue a bunch of more specific barriers */
3268 uint32_t bits
= semantics
;
3270 SpvMemorySemanticsMask semantic
= 1 << u_bit_scan(&bits
);
3272 case SpvMemorySemanticsUniformMemoryMask
:
3273 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3275 case SpvMemorySemanticsWorkgroupMemoryMask
:
3276 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3278 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3279 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3281 case SpvMemorySemanticsImageMemoryMask
:
3282 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3291 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3292 const uint32_t *w
, unsigned count
)
3295 case SpvOpEmitVertex
:
3296 case SpvOpEmitStreamVertex
:
3297 case SpvOpEndPrimitive
:
3298 case SpvOpEndStreamPrimitive
: {
3299 nir_intrinsic_op intrinsic_op
;
3301 case SpvOpEmitVertex
:
3302 case SpvOpEmitStreamVertex
:
3303 intrinsic_op
= nir_intrinsic_emit_vertex
;
3305 case SpvOpEndPrimitive
:
3306 case SpvOpEndStreamPrimitive
:
3307 intrinsic_op
= nir_intrinsic_end_primitive
;
3310 unreachable("Invalid opcode");
3313 nir_intrinsic_instr
*intrin
=
3314 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3317 case SpvOpEmitStreamVertex
:
3318 case SpvOpEndStreamPrimitive
: {
3319 unsigned stream
= vtn_constant_uint(b
, w
[1]);
3320 nir_intrinsic_set_stream_id(intrin
, stream
);
3328 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3332 case SpvOpMemoryBarrier
: {
3333 SpvScope scope
= vtn_constant_uint(b
, w
[1]);
3334 SpvMemorySemanticsMask semantics
= vtn_constant_uint(b
, w
[2]);
3335 vtn_emit_memory_barrier(b
, scope
, semantics
);
3339 case SpvOpControlBarrier
: {
3340 SpvScope execution_scope
= vtn_constant_uint(b
, w
[1]);
3341 if (execution_scope
== SpvScopeWorkgroup
)
3342 vtn_emit_barrier(b
, nir_intrinsic_barrier
);
3344 SpvScope memory_scope
= vtn_constant_uint(b
, w
[2]);
3345 SpvMemorySemanticsMask memory_semantics
= vtn_constant_uint(b
, w
[3]);
3346 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
3351 unreachable("unknown barrier instruction");
3356 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
3357 SpvExecutionMode mode
)
3360 case SpvExecutionModeInputPoints
:
3361 case SpvExecutionModeOutputPoints
:
3362 return 0; /* GL_POINTS */
3363 case SpvExecutionModeInputLines
:
3364 return 1; /* GL_LINES */
3365 case SpvExecutionModeInputLinesAdjacency
:
3366 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3367 case SpvExecutionModeTriangles
:
3368 return 4; /* GL_TRIANGLES */
3369 case SpvExecutionModeInputTrianglesAdjacency
:
3370 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3371 case SpvExecutionModeQuads
:
3372 return 7; /* GL_QUADS */
3373 case SpvExecutionModeIsolines
:
3374 return 0x8E7A; /* GL_ISOLINES */
3375 case SpvExecutionModeOutputLineStrip
:
3376 return 3; /* GL_LINE_STRIP */
3377 case SpvExecutionModeOutputTriangleStrip
:
3378 return 5; /* GL_TRIANGLE_STRIP */
3380 vtn_fail("Invalid primitive type: %s (%u)",
3381 spirv_executionmode_to_string(mode
), mode
);
3386 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
3387 SpvExecutionMode mode
)
3390 case SpvExecutionModeInputPoints
:
3392 case SpvExecutionModeInputLines
:
3394 case SpvExecutionModeInputLinesAdjacency
:
3396 case SpvExecutionModeTriangles
:
3398 case SpvExecutionModeInputTrianglesAdjacency
:
3401 vtn_fail("Invalid GS input mode: %s (%u)",
3402 spirv_executionmode_to_string(mode
), mode
);
3406 static gl_shader_stage
3407 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
3410 case SpvExecutionModelVertex
:
3411 return MESA_SHADER_VERTEX
;
3412 case SpvExecutionModelTessellationControl
:
3413 return MESA_SHADER_TESS_CTRL
;
3414 case SpvExecutionModelTessellationEvaluation
:
3415 return MESA_SHADER_TESS_EVAL
;
3416 case SpvExecutionModelGeometry
:
3417 return MESA_SHADER_GEOMETRY
;
3418 case SpvExecutionModelFragment
:
3419 return MESA_SHADER_FRAGMENT
;
3420 case SpvExecutionModelGLCompute
:
3421 return MESA_SHADER_COMPUTE
;
3422 case SpvExecutionModelKernel
:
3423 return MESA_SHADER_KERNEL
;
3425 vtn_fail("Unsupported execution model: %s (%u)",
3426 spirv_executionmodel_to_string(model
), model
);
3430 #define spv_check_supported(name, cap) do { \
3431 if (!(b->options && b->options->caps.name)) \
3432 vtn_warn("Unsupported SPIR-V capability: %s (%u)", \
3433 spirv_capability_to_string(cap), cap); \
3438 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
3441 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
3442 /* Let this be a name label regardless */
3443 unsigned name_words
;
3444 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
3446 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
3447 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
3450 vtn_assert(b
->entry_point
== NULL
);
3451 b
->entry_point
= entry_point
;
3455 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3456 const uint32_t *w
, unsigned count
)
3463 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
3464 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
3465 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
3466 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
3467 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
3468 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
3471 uint32_t version
= w
[2];
3474 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
3476 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
3480 case SpvOpSourceExtension
:
3481 case SpvOpSourceContinued
:
3482 case SpvOpExtension
:
3483 case SpvOpModuleProcessed
:
3484 /* Unhandled, but these are for debug so that's ok. */
3487 case SpvOpCapability
: {
3488 SpvCapability cap
= w
[1];
3490 case SpvCapabilityMatrix
:
3491 case SpvCapabilityShader
:
3492 case SpvCapabilityGeometry
:
3493 case SpvCapabilityGeometryPointSize
:
3494 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
3495 case SpvCapabilitySampledImageArrayDynamicIndexing
:
3496 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
3497 case SpvCapabilityStorageImageArrayDynamicIndexing
:
3498 case SpvCapabilityImageRect
:
3499 case SpvCapabilitySampledRect
:
3500 case SpvCapabilitySampled1D
:
3501 case SpvCapabilityImage1D
:
3502 case SpvCapabilitySampledCubeArray
:
3503 case SpvCapabilityImageCubeArray
:
3504 case SpvCapabilitySampledBuffer
:
3505 case SpvCapabilityImageBuffer
:
3506 case SpvCapabilityImageQuery
:
3507 case SpvCapabilityDerivativeControl
:
3508 case SpvCapabilityInterpolationFunction
:
3509 case SpvCapabilityMultiViewport
:
3510 case SpvCapabilitySampleRateShading
:
3511 case SpvCapabilityClipDistance
:
3512 case SpvCapabilityCullDistance
:
3513 case SpvCapabilityInputAttachment
:
3514 case SpvCapabilityImageGatherExtended
:
3515 case SpvCapabilityStorageImageExtendedFormats
:
3518 case SpvCapabilityLinkage
:
3519 case SpvCapabilityVector16
:
3520 case SpvCapabilityFloat16Buffer
:
3521 case SpvCapabilitySparseResidency
:
3522 vtn_warn("Unsupported SPIR-V capability: %s",
3523 spirv_capability_to_string(cap
));
3526 case SpvCapabilityMinLod
:
3527 spv_check_supported(min_lod
, cap
);
3530 case SpvCapabilityAtomicStorage
:
3531 spv_check_supported(atomic_storage
, cap
);
3534 case SpvCapabilityFloat64
:
3535 spv_check_supported(float64
, cap
);
3537 case SpvCapabilityInt64
:
3538 spv_check_supported(int64
, cap
);
3540 case SpvCapabilityInt16
:
3541 spv_check_supported(int16
, cap
);
3543 case SpvCapabilityInt8
:
3544 spv_check_supported(int8
, cap
);
3547 case SpvCapabilityTransformFeedback
:
3548 spv_check_supported(transform_feedback
, cap
);
3551 case SpvCapabilityGeometryStreams
:
3552 spv_check_supported(geometry_streams
, cap
);
3555 case SpvCapabilityInt64Atomics
:
3556 spv_check_supported(int64_atomics
, cap
);
3559 case SpvCapabilityStorageImageMultisample
:
3560 spv_check_supported(storage_image_ms
, cap
);
3563 case SpvCapabilityAddresses
:
3564 spv_check_supported(address
, cap
);
3567 case SpvCapabilityKernel
:
3568 spv_check_supported(kernel
, cap
);
3571 case SpvCapabilityImageBasic
:
3572 case SpvCapabilityImageReadWrite
:
3573 case SpvCapabilityImageMipmap
:
3574 case SpvCapabilityPipes
:
3575 case SpvCapabilityDeviceEnqueue
:
3576 case SpvCapabilityLiteralSampler
:
3577 case SpvCapabilityGenericPointer
:
3578 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
3579 spirv_capability_to_string(cap
));
3582 case SpvCapabilityImageMSArray
:
3583 spv_check_supported(image_ms_array
, cap
);
3586 case SpvCapabilityTessellation
:
3587 case SpvCapabilityTessellationPointSize
:
3588 spv_check_supported(tessellation
, cap
);
3591 case SpvCapabilityDrawParameters
:
3592 spv_check_supported(draw_parameters
, cap
);
3595 case SpvCapabilityStorageImageReadWithoutFormat
:
3596 spv_check_supported(image_read_without_format
, cap
);
3599 case SpvCapabilityStorageImageWriteWithoutFormat
:
3600 spv_check_supported(image_write_without_format
, cap
);
3603 case SpvCapabilityDeviceGroup
:
3604 spv_check_supported(device_group
, cap
);
3607 case SpvCapabilityMultiView
:
3608 spv_check_supported(multiview
, cap
);
3611 case SpvCapabilityGroupNonUniform
:
3612 spv_check_supported(subgroup_basic
, cap
);
3615 case SpvCapabilitySubgroupVoteKHR
:
3616 case SpvCapabilityGroupNonUniformVote
:
3617 spv_check_supported(subgroup_vote
, cap
);
3620 case SpvCapabilitySubgroupBallotKHR
:
3621 case SpvCapabilityGroupNonUniformBallot
:
3622 spv_check_supported(subgroup_ballot
, cap
);
3625 case SpvCapabilityGroupNonUniformShuffle
:
3626 case SpvCapabilityGroupNonUniformShuffleRelative
:
3627 spv_check_supported(subgroup_shuffle
, cap
);
3630 case SpvCapabilityGroupNonUniformQuad
:
3631 spv_check_supported(subgroup_quad
, cap
);
3634 case SpvCapabilityGroupNonUniformArithmetic
:
3635 case SpvCapabilityGroupNonUniformClustered
:
3636 spv_check_supported(subgroup_arithmetic
, cap
);
3639 case SpvCapabilityGroups
:
3640 spv_check_supported(amd_shader_ballot
, cap
);
3643 case SpvCapabilityVariablePointersStorageBuffer
:
3644 case SpvCapabilityVariablePointers
:
3645 spv_check_supported(variable_pointers
, cap
);
3646 b
->variable_pointers
= true;
3649 case SpvCapabilityStorageUniformBufferBlock16
:
3650 case SpvCapabilityStorageUniform16
:
3651 case SpvCapabilityStoragePushConstant16
:
3652 case SpvCapabilityStorageInputOutput16
:
3653 spv_check_supported(storage_16bit
, cap
);
3656 case SpvCapabilityShaderViewportIndexLayerEXT
:
3657 spv_check_supported(shader_viewport_index_layer
, cap
);
3660 case SpvCapabilityStorageBuffer8BitAccess
:
3661 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
3662 case SpvCapabilityStoragePushConstant8
:
3663 spv_check_supported(storage_8bit
, cap
);
3666 case SpvCapabilityShaderNonUniformEXT
:
3667 spv_check_supported(descriptor_indexing
, cap
);
3670 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
3671 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
3672 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
3673 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
3676 case SpvCapabilityUniformBufferArrayNonUniformIndexingEXT
:
3677 case SpvCapabilitySampledImageArrayNonUniformIndexingEXT
:
3678 case SpvCapabilityStorageBufferArrayNonUniformIndexingEXT
:
3679 case SpvCapabilityStorageImageArrayNonUniformIndexingEXT
:
3680 case SpvCapabilityInputAttachmentArrayNonUniformIndexingEXT
:
3681 case SpvCapabilityUniformTexelBufferArrayNonUniformIndexingEXT
:
3682 case SpvCapabilityStorageTexelBufferArrayNonUniformIndexingEXT
:
3683 spv_check_supported(descriptor_array_non_uniform_indexing
, cap
);
3686 case SpvCapabilityRuntimeDescriptorArrayEXT
:
3687 spv_check_supported(runtime_descriptor_array
, cap
);
3690 case SpvCapabilityStencilExportEXT
:
3691 spv_check_supported(stencil_export
, cap
);
3694 case SpvCapabilitySampleMaskPostDepthCoverage
:
3695 spv_check_supported(post_depth_coverage
, cap
);
3698 case SpvCapabilityPhysicalStorageBufferAddressesEXT
:
3699 spv_check_supported(physical_storage_buffer_address
, cap
);
3702 case SpvCapabilityComputeDerivativeGroupQuadsNV
:
3703 case SpvCapabilityComputeDerivativeGroupLinearNV
:
3704 spv_check_supported(derivative_group
, cap
);
3707 case SpvCapabilityFloat16
:
3708 spv_check_supported(float16
, cap
);
3711 case SpvCapabilityFragmentShaderSampleInterlockEXT
:
3712 spv_check_supported(fragment_shader_sample_interlock
, cap
);
3715 case SpvCapabilityFragmentShaderPixelInterlockEXT
:
3716 spv_check_supported(fragment_shader_pixel_interlock
, cap
);
3720 vtn_fail("Unhandled capability: %s (%u)",
3721 spirv_capability_to_string(cap
), cap
);
3726 case SpvOpExtInstImport
:
3727 vtn_handle_extension(b
, opcode
, w
, count
);
3730 case SpvOpMemoryModel
:
3732 case SpvAddressingModelPhysical32
:
3733 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
3734 "AddressingModelPhysical32 only supported for kernels");
3735 b
->shader
->info
.cs
.ptr_size
= 32;
3736 b
->physical_ptrs
= true;
3737 b
->options
->shared_addr_format
= nir_address_format_32bit_global
;
3738 b
->options
->global_addr_format
= nir_address_format_32bit_global
;
3739 b
->options
->temp_addr_format
= nir_address_format_32bit_global
;
3741 case SpvAddressingModelPhysical64
:
3742 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
3743 "AddressingModelPhysical64 only supported for kernels");
3744 b
->shader
->info
.cs
.ptr_size
= 64;
3745 b
->physical_ptrs
= true;
3746 b
->options
->shared_addr_format
= nir_address_format_64bit_global
;
3747 b
->options
->global_addr_format
= nir_address_format_64bit_global
;
3748 b
->options
->temp_addr_format
= nir_address_format_64bit_global
;
3750 case SpvAddressingModelLogical
:
3751 vtn_fail_if(b
->shader
->info
.stage
>= MESA_SHADER_STAGES
,
3752 "AddressingModelLogical only supported for shaders");
3753 b
->shader
->info
.cs
.ptr_size
= 0;
3754 b
->physical_ptrs
= false;
3756 case SpvAddressingModelPhysicalStorageBuffer64EXT
:
3757 vtn_fail_if(!b
->options
||
3758 !b
->options
->caps
.physical_storage_buffer_address
,
3759 "AddressingModelPhysicalStorageBuffer64EXT not supported");
3762 vtn_fail("Unknown addressing model: %s (%u)",
3763 spirv_addressingmodel_to_string(w
[1]), w
[1]);
3767 vtn_assert(w
[2] == SpvMemoryModelSimple
||
3768 w
[2] == SpvMemoryModelGLSL450
||
3769 w
[2] == SpvMemoryModelOpenCL
);
3772 case SpvOpEntryPoint
:
3773 vtn_handle_entry_point(b
, w
, count
);
3777 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
3778 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3782 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3785 case SpvOpMemberName
:
3789 case SpvOpExecutionMode
:
3790 case SpvOpExecutionModeId
:
3791 case SpvOpDecorationGroup
:
3793 case SpvOpDecorateId
:
3794 case SpvOpMemberDecorate
:
3795 case SpvOpGroupDecorate
:
3796 case SpvOpGroupMemberDecorate
:
3797 case SpvOpDecorateString
:
3798 case SpvOpMemberDecorateString
:
3799 vtn_handle_decoration(b
, opcode
, w
, count
);
3803 return false; /* End of preamble */
3810 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
3811 const struct vtn_decoration
*mode
, void *data
)
3813 vtn_assert(b
->entry_point
== entry_point
);
3815 switch(mode
->exec_mode
) {
3816 case SpvExecutionModeOriginUpperLeft
:
3817 case SpvExecutionModeOriginLowerLeft
:
3818 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3819 b
->shader
->info
.fs
.origin_upper_left
=
3820 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
3823 case SpvExecutionModeEarlyFragmentTests
:
3824 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3825 b
->shader
->info
.fs
.early_fragment_tests
= true;
3828 case SpvExecutionModePostDepthCoverage
:
3829 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3830 b
->shader
->info
.fs
.post_depth_coverage
= true;
3833 case SpvExecutionModeInvocations
:
3834 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3835 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->operands
[0]);
3838 case SpvExecutionModeDepthReplacing
:
3839 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3840 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
3842 case SpvExecutionModeDepthGreater
:
3843 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3844 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
3846 case SpvExecutionModeDepthLess
:
3847 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3848 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
3850 case SpvExecutionModeDepthUnchanged
:
3851 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3852 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
3855 case SpvExecutionModeLocalSize
:
3856 vtn_assert(gl_shader_stage_is_compute(b
->shader
->info
.stage
));
3857 b
->shader
->info
.cs
.local_size
[0] = mode
->operands
[0];
3858 b
->shader
->info
.cs
.local_size
[1] = mode
->operands
[1];
3859 b
->shader
->info
.cs
.local_size
[2] = mode
->operands
[2];
3862 case SpvExecutionModeLocalSizeId
:
3863 b
->shader
->info
.cs
.local_size
[0] = vtn_constant_uint(b
, mode
->operands
[0]);
3864 b
->shader
->info
.cs
.local_size
[1] = vtn_constant_uint(b
, mode
->operands
[1]);
3865 b
->shader
->info
.cs
.local_size
[2] = vtn_constant_uint(b
, mode
->operands
[2]);
3868 case SpvExecutionModeLocalSizeHint
:
3869 case SpvExecutionModeLocalSizeHintId
:
3870 break; /* Nothing to do with this */
3872 case SpvExecutionModeOutputVertices
:
3873 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3874 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3875 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->operands
[0];
3877 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3878 b
->shader
->info
.gs
.vertices_out
= mode
->operands
[0];
3882 case SpvExecutionModeInputPoints
:
3883 case SpvExecutionModeInputLines
:
3884 case SpvExecutionModeInputLinesAdjacency
:
3885 case SpvExecutionModeTriangles
:
3886 case SpvExecutionModeInputTrianglesAdjacency
:
3887 case SpvExecutionModeQuads
:
3888 case SpvExecutionModeIsolines
:
3889 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3890 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3891 b
->shader
->info
.tess
.primitive_mode
=
3892 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3894 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3895 b
->shader
->info
.gs
.vertices_in
=
3896 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
3897 b
->shader
->info
.gs
.input_primitive
=
3898 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3902 case SpvExecutionModeOutputPoints
:
3903 case SpvExecutionModeOutputLineStrip
:
3904 case SpvExecutionModeOutputTriangleStrip
:
3905 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3906 b
->shader
->info
.gs
.output_primitive
=
3907 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3910 case SpvExecutionModeSpacingEqual
:
3911 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3912 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3913 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
3915 case SpvExecutionModeSpacingFractionalEven
:
3916 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3917 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3918 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
3920 case SpvExecutionModeSpacingFractionalOdd
:
3921 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3922 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3923 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
3925 case SpvExecutionModeVertexOrderCw
:
3926 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3927 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3928 b
->shader
->info
.tess
.ccw
= false;
3930 case SpvExecutionModeVertexOrderCcw
:
3931 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3932 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3933 b
->shader
->info
.tess
.ccw
= true;
3935 case SpvExecutionModePointMode
:
3936 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3937 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3938 b
->shader
->info
.tess
.point_mode
= true;
3941 case SpvExecutionModePixelCenterInteger
:
3942 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3943 b
->shader
->info
.fs
.pixel_center_integer
= true;
3946 case SpvExecutionModeXfb
:
3947 b
->shader
->info
.has_transform_feedback_varyings
= true;
3950 case SpvExecutionModeVecTypeHint
:
3953 case SpvExecutionModeContractionOff
:
3954 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
3955 vtn_warn("ExectionMode only allowed for CL-style kernels: %s",
3956 spirv_executionmode_to_string(mode
->exec_mode
));
3961 case SpvExecutionModeStencilRefReplacingEXT
:
3962 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3965 case SpvExecutionModeDerivativeGroupQuadsNV
:
3966 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
3967 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_QUADS
;
3970 case SpvExecutionModeDerivativeGroupLinearNV
:
3971 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
3972 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_LINEAR
;
3975 case SpvExecutionModePixelInterlockOrderedEXT
:
3976 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3977 b
->shader
->info
.fs
.pixel_interlock_ordered
= true;
3980 case SpvExecutionModePixelInterlockUnorderedEXT
:
3981 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3982 b
->shader
->info
.fs
.pixel_interlock_unordered
= true;
3985 case SpvExecutionModeSampleInterlockOrderedEXT
:
3986 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3987 b
->shader
->info
.fs
.sample_interlock_ordered
= true;
3990 case SpvExecutionModeSampleInterlockUnorderedEXT
:
3991 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3992 b
->shader
->info
.fs
.sample_interlock_unordered
= true;
3996 vtn_fail("Unhandled execution mode: %s (%u)",
3997 spirv_executionmode_to_string(mode
->exec_mode
),
4003 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4004 const uint32_t *w
, unsigned count
)
4006 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
4010 case SpvOpSourceContinued
:
4011 case SpvOpSourceExtension
:
4012 case SpvOpExtension
:
4013 case SpvOpCapability
:
4014 case SpvOpExtInstImport
:
4015 case SpvOpMemoryModel
:
4016 case SpvOpEntryPoint
:
4017 case SpvOpExecutionMode
:
4020 case SpvOpMemberName
:
4021 case SpvOpDecorationGroup
:
4023 case SpvOpDecorateId
:
4024 case SpvOpMemberDecorate
:
4025 case SpvOpGroupDecorate
:
4026 case SpvOpGroupMemberDecorate
:
4027 case SpvOpDecorateString
:
4028 case SpvOpMemberDecorateString
:
4029 vtn_fail("Invalid opcode types and variables section");
4035 case SpvOpTypeFloat
:
4036 case SpvOpTypeVector
:
4037 case SpvOpTypeMatrix
:
4038 case SpvOpTypeImage
:
4039 case SpvOpTypeSampler
:
4040 case SpvOpTypeSampledImage
:
4041 case SpvOpTypeArray
:
4042 case SpvOpTypeRuntimeArray
:
4043 case SpvOpTypeStruct
:
4044 case SpvOpTypeOpaque
:
4045 case SpvOpTypePointer
:
4046 case SpvOpTypeForwardPointer
:
4047 case SpvOpTypeFunction
:
4048 case SpvOpTypeEvent
:
4049 case SpvOpTypeDeviceEvent
:
4050 case SpvOpTypeReserveId
:
4051 case SpvOpTypeQueue
:
4053 vtn_handle_type(b
, opcode
, w
, count
);
4056 case SpvOpConstantTrue
:
4057 case SpvOpConstantFalse
:
4059 case SpvOpConstantComposite
:
4060 case SpvOpConstantSampler
:
4061 case SpvOpConstantNull
:
4062 case SpvOpSpecConstantTrue
:
4063 case SpvOpSpecConstantFalse
:
4064 case SpvOpSpecConstant
:
4065 case SpvOpSpecConstantComposite
:
4066 case SpvOpSpecConstantOp
:
4067 vtn_handle_constant(b
, opcode
, w
, count
);
4072 vtn_handle_variables(b
, opcode
, w
, count
);
4076 return false; /* End of preamble */
4082 static struct vtn_ssa_value
*
4083 vtn_nir_select(struct vtn_builder
*b
, struct vtn_ssa_value
*src0
,
4084 struct vtn_ssa_value
*src1
, struct vtn_ssa_value
*src2
)
4086 struct vtn_ssa_value
*dest
= rzalloc(b
, struct vtn_ssa_value
);
4087 dest
->type
= src1
->type
;
4089 if (glsl_type_is_vector_or_scalar(src1
->type
)) {
4090 dest
->def
= nir_bcsel(&b
->nb
, src0
->def
, src1
->def
, src2
->def
);
4092 unsigned elems
= glsl_get_length(src1
->type
);
4094 dest
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
4095 for (unsigned i
= 0; i
< elems
; i
++) {
4096 dest
->elems
[i
] = vtn_nir_select(b
, src0
,
4097 src1
->elems
[i
], src2
->elems
[i
]);
4105 vtn_handle_select(struct vtn_builder
*b
, SpvOp opcode
,
4106 const uint32_t *w
, unsigned count
)
4108 /* Handle OpSelect up-front here because it needs to be able to handle
4109 * pointers and not just regular vectors and scalars.
4111 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
4112 struct vtn_value
*cond_val
= vtn_untyped_value(b
, w
[3]);
4113 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
4114 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
4116 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4117 obj2_val
->type
!= res_val
->type
,
4118 "Object types must match the result type in OpSelect");
4120 vtn_fail_if((cond_val
->type
->base_type
!= vtn_base_type_scalar
&&
4121 cond_val
->type
->base_type
!= vtn_base_type_vector
) ||
4122 !glsl_type_is_boolean(cond_val
->type
->type
),
4123 "OpSelect must have either a vector of booleans or "
4124 "a boolean as Condition type");
4126 vtn_fail_if(cond_val
->type
->base_type
== vtn_base_type_vector
&&
4127 (res_val
->type
->base_type
!= vtn_base_type_vector
||
4128 res_val
->type
->length
!= cond_val
->type
->length
),
4129 "When Condition type in OpSelect is a vector, the Result "
4130 "type must be a vector of the same length");
4132 switch (res_val
->type
->base_type
) {
4133 case vtn_base_type_scalar
:
4134 case vtn_base_type_vector
:
4135 case vtn_base_type_matrix
:
4136 case vtn_base_type_array
:
4137 case vtn_base_type_struct
:
4140 case vtn_base_type_pointer
:
4141 /* We need to have actual storage for pointer types. */
4142 vtn_fail_if(res_val
->type
->type
== NULL
,
4143 "Invalid pointer result type for OpSelect");
4146 vtn_fail("Result type of OpSelect must be a scalar, composite, or pointer");
4149 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4150 struct vtn_ssa_value
*ssa
= vtn_nir_select(b
,
4151 vtn_ssa_value(b
, w
[3]), vtn_ssa_value(b
, w
[4]), vtn_ssa_value(b
, w
[5]));
4153 vtn_push_ssa(b
, w
[2], res_type
, ssa
);
4157 vtn_handle_ptr(struct vtn_builder
*b
, SpvOp opcode
,
4158 const uint32_t *w
, unsigned count
)
4160 struct vtn_type
*type1
= vtn_untyped_value(b
, w
[3])->type
;
4161 struct vtn_type
*type2
= vtn_untyped_value(b
, w
[4])->type
;
4162 vtn_fail_if(type1
->base_type
!= vtn_base_type_pointer
||
4163 type2
->base_type
!= vtn_base_type_pointer
,
4164 "%s operands must have pointer types",
4165 spirv_op_to_string(opcode
));
4166 vtn_fail_if(type1
->storage_class
!= type2
->storage_class
,
4167 "%s operands must have the same storage class",
4168 spirv_op_to_string(opcode
));
4170 const struct glsl_type
*type
=
4171 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
4173 nir_address_format addr_format
= vtn_mode_to_address_format(
4174 b
, vtn_storage_class_to_mode(b
, type1
->storage_class
, NULL
, NULL
));
4179 case SpvOpPtrDiff
: {
4180 /* OpPtrDiff returns the difference in number of elements (not byte offset). */
4181 unsigned elem_size
, elem_align
;
4182 glsl_get_natural_size_align_bytes(type1
->deref
->type
,
4183 &elem_size
, &elem_align
);
4185 def
= nir_build_addr_isub(&b
->nb
,
4186 vtn_ssa_value(b
, w
[3])->def
,
4187 vtn_ssa_value(b
, w
[4])->def
,
4189 def
= nir_idiv(&b
->nb
, def
, nir_imm_intN_t(&b
->nb
, elem_size
, def
->bit_size
));
4190 def
= nir_i2i(&b
->nb
, def
, glsl_get_bit_size(type
));
4195 case SpvOpPtrNotEqual
: {
4196 def
= nir_build_addr_ieq(&b
->nb
,
4197 vtn_ssa_value(b
, w
[3])->def
,
4198 vtn_ssa_value(b
, w
[4])->def
,
4200 if (opcode
== SpvOpPtrNotEqual
)
4201 def
= nir_inot(&b
->nb
, def
);
4206 unreachable("Invalid ptr operation");
4209 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
4210 val
->ssa
= vtn_create_ssa_value(b
, type
);
4211 val
->ssa
->def
= def
;
4215 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4216 const uint32_t *w
, unsigned count
)
4222 case SpvOpLoopMerge
:
4223 case SpvOpSelectionMerge
:
4224 /* This is handled by cfg pre-pass and walk_blocks */
4228 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
4229 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4234 vtn_handle_extension(b
, opcode
, w
, count
);
4240 case SpvOpCopyMemory
:
4241 case SpvOpCopyMemorySized
:
4242 case SpvOpAccessChain
:
4243 case SpvOpPtrAccessChain
:
4244 case SpvOpInBoundsAccessChain
:
4245 case SpvOpInBoundsPtrAccessChain
:
4246 case SpvOpArrayLength
:
4247 case SpvOpConvertPtrToU
:
4248 case SpvOpConvertUToPtr
:
4249 vtn_handle_variables(b
, opcode
, w
, count
);
4252 case SpvOpFunctionCall
:
4253 vtn_handle_function_call(b
, opcode
, w
, count
);
4256 case SpvOpSampledImage
:
4258 case SpvOpImageSampleImplicitLod
:
4259 case SpvOpImageSampleExplicitLod
:
4260 case SpvOpImageSampleDrefImplicitLod
:
4261 case SpvOpImageSampleDrefExplicitLod
:
4262 case SpvOpImageSampleProjImplicitLod
:
4263 case SpvOpImageSampleProjExplicitLod
:
4264 case SpvOpImageSampleProjDrefImplicitLod
:
4265 case SpvOpImageSampleProjDrefExplicitLod
:
4266 case SpvOpImageFetch
:
4267 case SpvOpImageGather
:
4268 case SpvOpImageDrefGather
:
4269 case SpvOpImageQuerySizeLod
:
4270 case SpvOpImageQueryLod
:
4271 case SpvOpImageQueryLevels
:
4272 case SpvOpImageQuerySamples
:
4273 vtn_handle_texture(b
, opcode
, w
, count
);
4276 case SpvOpImageRead
:
4277 case SpvOpImageWrite
:
4278 case SpvOpImageTexelPointer
:
4279 vtn_handle_image(b
, opcode
, w
, count
);
4282 case SpvOpImageQuerySize
: {
4283 struct vtn_pointer
*image
=
4284 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
4285 if (glsl_type_is_image(image
->type
->type
)) {
4286 vtn_handle_image(b
, opcode
, w
, count
);
4288 vtn_assert(glsl_type_is_sampler(image
->type
->type
));
4289 vtn_handle_texture(b
, opcode
, w
, count
);
4294 case SpvOpAtomicLoad
:
4295 case SpvOpAtomicExchange
:
4296 case SpvOpAtomicCompareExchange
:
4297 case SpvOpAtomicCompareExchangeWeak
:
4298 case SpvOpAtomicIIncrement
:
4299 case SpvOpAtomicIDecrement
:
4300 case SpvOpAtomicIAdd
:
4301 case SpvOpAtomicISub
:
4302 case SpvOpAtomicSMin
:
4303 case SpvOpAtomicUMin
:
4304 case SpvOpAtomicSMax
:
4305 case SpvOpAtomicUMax
:
4306 case SpvOpAtomicAnd
:
4308 case SpvOpAtomicXor
: {
4309 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
4310 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4311 vtn_handle_image(b
, opcode
, w
, count
);
4313 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4314 vtn_handle_atomics(b
, opcode
, w
, count
);
4319 case SpvOpAtomicStore
: {
4320 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
4321 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4322 vtn_handle_image(b
, opcode
, w
, count
);
4324 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4325 vtn_handle_atomics(b
, opcode
, w
, count
);
4331 vtn_handle_select(b
, opcode
, w
, count
);
4339 case SpvOpConvertFToU
:
4340 case SpvOpConvertFToS
:
4341 case SpvOpConvertSToF
:
4342 case SpvOpConvertUToF
:
4346 case SpvOpQuantizeToF16
:
4347 case SpvOpPtrCastToGeneric
:
4348 case SpvOpGenericCastToPtr
:
4353 case SpvOpSignBitSet
:
4354 case SpvOpLessOrGreater
:
4356 case SpvOpUnordered
:
4371 case SpvOpVectorTimesScalar
:
4373 case SpvOpIAddCarry
:
4374 case SpvOpISubBorrow
:
4375 case SpvOpUMulExtended
:
4376 case SpvOpSMulExtended
:
4377 case SpvOpShiftRightLogical
:
4378 case SpvOpShiftRightArithmetic
:
4379 case SpvOpShiftLeftLogical
:
4380 case SpvOpLogicalEqual
:
4381 case SpvOpLogicalNotEqual
:
4382 case SpvOpLogicalOr
:
4383 case SpvOpLogicalAnd
:
4384 case SpvOpLogicalNot
:
4385 case SpvOpBitwiseOr
:
4386 case SpvOpBitwiseXor
:
4387 case SpvOpBitwiseAnd
:
4389 case SpvOpFOrdEqual
:
4390 case SpvOpFUnordEqual
:
4391 case SpvOpINotEqual
:
4392 case SpvOpFOrdNotEqual
:
4393 case SpvOpFUnordNotEqual
:
4394 case SpvOpULessThan
:
4395 case SpvOpSLessThan
:
4396 case SpvOpFOrdLessThan
:
4397 case SpvOpFUnordLessThan
:
4398 case SpvOpUGreaterThan
:
4399 case SpvOpSGreaterThan
:
4400 case SpvOpFOrdGreaterThan
:
4401 case SpvOpFUnordGreaterThan
:
4402 case SpvOpULessThanEqual
:
4403 case SpvOpSLessThanEqual
:
4404 case SpvOpFOrdLessThanEqual
:
4405 case SpvOpFUnordLessThanEqual
:
4406 case SpvOpUGreaterThanEqual
:
4407 case SpvOpSGreaterThanEqual
:
4408 case SpvOpFOrdGreaterThanEqual
:
4409 case SpvOpFUnordGreaterThanEqual
:
4415 case SpvOpFwidthFine
:
4416 case SpvOpDPdxCoarse
:
4417 case SpvOpDPdyCoarse
:
4418 case SpvOpFwidthCoarse
:
4419 case SpvOpBitFieldInsert
:
4420 case SpvOpBitFieldSExtract
:
4421 case SpvOpBitFieldUExtract
:
4422 case SpvOpBitReverse
:
4424 case SpvOpTranspose
:
4425 case SpvOpOuterProduct
:
4426 case SpvOpMatrixTimesScalar
:
4427 case SpvOpVectorTimesMatrix
:
4428 case SpvOpMatrixTimesVector
:
4429 case SpvOpMatrixTimesMatrix
:
4430 vtn_handle_alu(b
, opcode
, w
, count
);
4434 vtn_handle_bitcast(b
, w
, count
);
4437 case SpvOpVectorExtractDynamic
:
4438 case SpvOpVectorInsertDynamic
:
4439 case SpvOpVectorShuffle
:
4440 case SpvOpCompositeConstruct
:
4441 case SpvOpCompositeExtract
:
4442 case SpvOpCompositeInsert
:
4443 case SpvOpCopyLogical
:
4444 case SpvOpCopyObject
:
4445 vtn_handle_composite(b
, opcode
, w
, count
);
4448 case SpvOpEmitVertex
:
4449 case SpvOpEndPrimitive
:
4450 case SpvOpEmitStreamVertex
:
4451 case SpvOpEndStreamPrimitive
:
4452 case SpvOpControlBarrier
:
4453 case SpvOpMemoryBarrier
:
4454 vtn_handle_barrier(b
, opcode
, w
, count
);
4457 case SpvOpGroupNonUniformElect
:
4458 case SpvOpGroupNonUniformAll
:
4459 case SpvOpGroupNonUniformAny
:
4460 case SpvOpGroupNonUniformAllEqual
:
4461 case SpvOpGroupNonUniformBroadcast
:
4462 case SpvOpGroupNonUniformBroadcastFirst
:
4463 case SpvOpGroupNonUniformBallot
:
4464 case SpvOpGroupNonUniformInverseBallot
:
4465 case SpvOpGroupNonUniformBallotBitExtract
:
4466 case SpvOpGroupNonUniformBallotBitCount
:
4467 case SpvOpGroupNonUniformBallotFindLSB
:
4468 case SpvOpGroupNonUniformBallotFindMSB
:
4469 case SpvOpGroupNonUniformShuffle
:
4470 case SpvOpGroupNonUniformShuffleXor
:
4471 case SpvOpGroupNonUniformShuffleUp
:
4472 case SpvOpGroupNonUniformShuffleDown
:
4473 case SpvOpGroupNonUniformIAdd
:
4474 case SpvOpGroupNonUniformFAdd
:
4475 case SpvOpGroupNonUniformIMul
:
4476 case SpvOpGroupNonUniformFMul
:
4477 case SpvOpGroupNonUniformSMin
:
4478 case SpvOpGroupNonUniformUMin
:
4479 case SpvOpGroupNonUniformFMin
:
4480 case SpvOpGroupNonUniformSMax
:
4481 case SpvOpGroupNonUniformUMax
:
4482 case SpvOpGroupNonUniformFMax
:
4483 case SpvOpGroupNonUniformBitwiseAnd
:
4484 case SpvOpGroupNonUniformBitwiseOr
:
4485 case SpvOpGroupNonUniformBitwiseXor
:
4486 case SpvOpGroupNonUniformLogicalAnd
:
4487 case SpvOpGroupNonUniformLogicalOr
:
4488 case SpvOpGroupNonUniformLogicalXor
:
4489 case SpvOpGroupNonUniformQuadBroadcast
:
4490 case SpvOpGroupNonUniformQuadSwap
:
4493 case SpvOpGroupBroadcast
:
4494 case SpvOpGroupIAdd
:
4495 case SpvOpGroupFAdd
:
4496 case SpvOpGroupFMin
:
4497 case SpvOpGroupUMin
:
4498 case SpvOpGroupSMin
:
4499 case SpvOpGroupFMax
:
4500 case SpvOpGroupUMax
:
4501 case SpvOpGroupSMax
:
4502 case SpvOpSubgroupBallotKHR
:
4503 case SpvOpSubgroupFirstInvocationKHR
:
4504 case SpvOpSubgroupReadInvocationKHR
:
4505 case SpvOpSubgroupAllKHR
:
4506 case SpvOpSubgroupAnyKHR
:
4507 case SpvOpSubgroupAllEqualKHR
:
4508 case SpvOpGroupIAddNonUniformAMD
:
4509 case SpvOpGroupFAddNonUniformAMD
:
4510 case SpvOpGroupFMinNonUniformAMD
:
4511 case SpvOpGroupUMinNonUniformAMD
:
4512 case SpvOpGroupSMinNonUniformAMD
:
4513 case SpvOpGroupFMaxNonUniformAMD
:
4514 case SpvOpGroupUMaxNonUniformAMD
:
4515 case SpvOpGroupSMaxNonUniformAMD
:
4516 vtn_handle_subgroup(b
, opcode
, w
, count
);
4521 case SpvOpPtrNotEqual
:
4522 vtn_handle_ptr(b
, opcode
, w
, count
);
4525 case SpvOpBeginInvocationInterlockEXT
:
4526 vtn_emit_barrier(b
, nir_intrinsic_begin_invocation_interlock
);
4529 case SpvOpEndInvocationInterlockEXT
:
4530 vtn_emit_barrier(b
, nir_intrinsic_end_invocation_interlock
);
4534 vtn_fail_with_opcode("Unhandled opcode", opcode
);
4541 vtn_create_builder(const uint32_t *words
, size_t word_count
,
4542 gl_shader_stage stage
, const char *entry_point_name
,
4543 const struct spirv_to_nir_options
*options
)
4545 /* Initialize the vtn_builder object */
4546 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
4547 struct spirv_to_nir_options
*dup_options
=
4548 ralloc(b
, struct spirv_to_nir_options
);
4549 *dup_options
= *options
;
4552 b
->spirv_word_count
= word_count
;
4556 exec_list_make_empty(&b
->functions
);
4557 b
->entry_point_stage
= stage
;
4558 b
->entry_point_name
= entry_point_name
;
4559 b
->options
= dup_options
;
4562 * Handle the SPIR-V header (first 5 dwords).
4563 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
4565 if (word_count
<= 5)
4568 if (words
[0] != SpvMagicNumber
) {
4569 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
4572 if (words
[1] < 0x10000) {
4573 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
4577 uint16_t generator_id
= words
[2] >> 16;
4578 uint16_t generator_version
= words
[2];
4580 /* The first GLSLang version bump actually 1.5 years after #179 was fixed
4581 * but this should at least let us shut the workaround off for modern
4582 * versions of GLSLang.
4584 b
->wa_glslang_179
= (generator_id
== 8 && generator_version
== 1);
4586 /* words[2] == generator magic */
4587 unsigned value_id_bound
= words
[3];
4588 if (words
[4] != 0) {
4589 vtn_err("words[4] was %u, want 0", words
[4]);
4593 b
->value_id_bound
= value_id_bound
;
4594 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
4602 static nir_function
*
4603 vtn_emit_kernel_entry_point_wrapper(struct vtn_builder
*b
,
4604 nir_function
*entry_point
)
4606 vtn_assert(entry_point
== b
->entry_point
->func
->impl
->function
);
4607 vtn_fail_if(!entry_point
->name
, "entry points are required to have a name");
4608 const char *func_name
=
4609 ralloc_asprintf(b
->shader
, "__wrapped_%s", entry_point
->name
);
4611 /* we shouldn't have any inputs yet */
4612 vtn_assert(!entry_point
->shader
->num_inputs
);
4613 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
);
4615 nir_function
*main_entry_point
= nir_function_create(b
->shader
, func_name
);
4616 main_entry_point
->impl
= nir_function_impl_create(main_entry_point
);
4617 nir_builder_init(&b
->nb
, main_entry_point
->impl
);
4618 b
->nb
.cursor
= nir_after_cf_list(&main_entry_point
->impl
->body
);
4619 b
->func_param_idx
= 0;
4621 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, entry_point
);
4623 for (unsigned i
= 0; i
< entry_point
->num_params
; ++i
) {
4624 struct vtn_type
*param_type
= b
->entry_point
->func
->type
->params
[i
];
4626 /* consider all pointers to function memory to be parameters passed
4629 bool is_by_val
= param_type
->base_type
== vtn_base_type_pointer
&&
4630 param_type
->storage_class
== SpvStorageClassFunction
;
4632 /* input variable */
4633 nir_variable
*in_var
= rzalloc(b
->nb
.shader
, nir_variable
);
4634 in_var
->data
.mode
= nir_var_shader_in
;
4635 in_var
->data
.read_only
= true;
4636 in_var
->data
.location
= i
;
4639 in_var
->type
= param_type
->deref
->type
;
4641 in_var
->type
= param_type
->type
;
4643 nir_shader_add_variable(b
->nb
.shader
, in_var
);
4644 b
->nb
.shader
->num_inputs
++;
4646 /* we have to copy the entire variable into function memory */
4648 nir_variable
*copy_var
=
4649 nir_local_variable_create(main_entry_point
->impl
, in_var
->type
,
4651 nir_copy_var(&b
->nb
, copy_var
, in_var
);
4653 nir_src_for_ssa(&nir_build_deref_var(&b
->nb
, copy_var
)->dest
.ssa
);
4655 call
->params
[i
] = nir_src_for_ssa(nir_load_var(&b
->nb
, in_var
));
4659 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
4661 return main_entry_point
;
4665 spirv_to_nir(const uint32_t *words
, size_t word_count
,
4666 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
4667 gl_shader_stage stage
, const char *entry_point_name
,
4668 const struct spirv_to_nir_options
*options
,
4669 const nir_shader_compiler_options
*nir_options
)
4672 const uint32_t *word_end
= words
+ word_count
;
4674 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
4675 stage
, entry_point_name
,
4681 /* See also _vtn_fail() */
4682 if (setjmp(b
->fail_jump
)) {
4687 /* Skip the SPIR-V header, handled at vtn_create_builder */
4690 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
4692 /* Handle all the preamble instructions */
4693 words
= vtn_foreach_instruction(b
, words
, word_end
,
4694 vtn_handle_preamble_instruction
);
4696 if (b
->entry_point
== NULL
) {
4697 vtn_fail("Entry point not found");
4702 /* Set shader info defaults */
4703 b
->shader
->info
.gs
.invocations
= 1;
4705 b
->specializations
= spec
;
4706 b
->num_specializations
= num_spec
;
4708 /* Handle all variable, type, and constant instructions */
4709 words
= vtn_foreach_instruction(b
, words
, word_end
,
4710 vtn_handle_variable_or_type_instruction
);
4712 /* Parse execution modes */
4713 vtn_foreach_execution_mode(b
, b
->entry_point
,
4714 vtn_handle_execution_mode
, NULL
);
4716 if (b
->workgroup_size_builtin
) {
4717 vtn_assert(b
->workgroup_size_builtin
->type
->type
==
4718 glsl_vector_type(GLSL_TYPE_UINT
, 3));
4720 nir_const_value
*const_size
=
4721 b
->workgroup_size_builtin
->constant
->values
;
4723 b
->shader
->info
.cs
.local_size
[0] = const_size
[0].u32
;
4724 b
->shader
->info
.cs
.local_size
[1] = const_size
[1].u32
;
4725 b
->shader
->info
.cs
.local_size
[2] = const_size
[2].u32
;
4728 /* Set types on all vtn_values */
4729 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
4731 vtn_build_cfg(b
, words
, word_end
);
4733 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4734 b
->entry_point
->func
->referenced
= true;
4739 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
4740 if (func
->referenced
&& !func
->emitted
) {
4741 b
->const_table
= _mesa_pointer_hash_table_create(b
);
4743 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
4749 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4750 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
4751 vtn_assert(entry_point
);
4753 /* post process entry_points with input params */
4754 if (entry_point
->num_params
&& b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
4755 entry_point
= vtn_emit_kernel_entry_point_wrapper(b
, entry_point
);
4757 entry_point
->is_entrypoint
= true;
4759 /* When multiple shader stages exist in the same SPIR-V module, we
4760 * generate input and output variables for every stage, in the same
4761 * NIR program. These dead variables can be invalid NIR. For example,
4762 * TCS outputs must be per-vertex arrays (or decorated 'patch'), while
4763 * VS output variables wouldn't be.
4765 * To ensure we have valid NIR, we eliminate any dead inputs and outputs
4766 * right away. In order to do so, we must lower any constant initializers
4767 * on outputs so nir_remove_dead_variables sees that they're written to.
4769 nir_lower_constant_initializers(b
->shader
, nir_var_shader_out
);
4770 nir_remove_dead_variables(b
->shader
,
4771 nir_var_shader_in
| nir_var_shader_out
);
4773 /* We sometimes generate bogus derefs that, while never used, give the
4774 * validator a bit of heartburn. Run dead code to get rid of them.
4776 nir_opt_dce(b
->shader
);
4778 /* Unparent the shader from the vtn_builder before we delete the builder */
4779 ralloc_steal(NULL
, b
->shader
);
4781 nir_shader
*shader
= b
->shader
;