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 load
->value
= constant
->values
[0];
241 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
242 val
->def
= &load
->def
;
244 assert(glsl_type_is_matrix(type
));
245 unsigned rows
= glsl_get_vector_elements(val
->type
);
246 unsigned columns
= glsl_get_matrix_columns(val
->type
);
247 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, columns
);
249 for (unsigned i
= 0; i
< columns
; i
++) {
250 struct vtn_ssa_value
*col_val
= rzalloc(b
, struct vtn_ssa_value
);
251 col_val
->type
= glsl_get_column_type(val
->type
);
252 nir_load_const_instr
*load
=
253 nir_load_const_instr_create(b
->shader
, rows
, bit_size
);
255 load
->value
= constant
->values
[i
];
257 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
258 col_val
->def
= &load
->def
;
260 val
->elems
[i
] = col_val
;
266 case GLSL_TYPE_ARRAY
: {
267 unsigned elems
= glsl_get_length(val
->type
);
268 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
269 const struct glsl_type
*elem_type
= glsl_get_array_element(val
->type
);
270 for (unsigned i
= 0; i
< elems
; i
++)
271 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
276 case GLSL_TYPE_STRUCT
: {
277 unsigned elems
= glsl_get_length(val
->type
);
278 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
279 for (unsigned i
= 0; i
< elems
; i
++) {
280 const struct glsl_type
*elem_type
=
281 glsl_get_struct_field(val
->type
, i
);
282 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
289 vtn_fail("bad constant type");
295 struct vtn_ssa_value
*
296 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
298 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
299 switch (val
->value_type
) {
300 case vtn_value_type_undef
:
301 return vtn_undef_ssa_value(b
, val
->type
->type
);
303 case vtn_value_type_constant
:
304 return vtn_const_ssa_value(b
, val
->constant
, val
->type
->type
);
306 case vtn_value_type_ssa
:
309 case vtn_value_type_pointer
:
310 vtn_assert(val
->pointer
->ptr_type
&& val
->pointer
->ptr_type
->type
);
311 struct vtn_ssa_value
*ssa
=
312 vtn_create_ssa_value(b
, val
->pointer
->ptr_type
->type
);
313 ssa
->def
= vtn_pointer_to_ssa(b
, val
->pointer
);
317 vtn_fail("Invalid type for an SSA value");
322 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
323 unsigned word_count
, unsigned *words_used
)
325 char *dup
= ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
327 /* Ammount of space taken by the string (including the null) */
328 unsigned len
= strlen(dup
) + 1;
329 *words_used
= DIV_ROUND_UP(len
, sizeof(*words
));
335 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
336 const uint32_t *end
, vtn_instruction_handler handler
)
342 const uint32_t *w
= start
;
344 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
345 unsigned count
= w
[0] >> SpvWordCountShift
;
346 vtn_assert(count
>= 1 && w
+ count
<= end
);
348 b
->spirv_offset
= (uint8_t *)w
- (uint8_t *)b
->spirv
;
352 break; /* Do nothing */
355 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
367 if (!handler(b
, opcode
, w
, count
))
385 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
386 const uint32_t *w
, unsigned count
)
388 const char *ext
= (const char *)&w
[2];
390 case SpvOpExtInstImport
: {
391 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
392 if (strcmp(ext
, "GLSL.std.450") == 0) {
393 val
->ext_handler
= vtn_handle_glsl450_instruction
;
394 } else if ((strcmp(ext
, "SPV_AMD_gcn_shader") == 0)
395 && (b
->options
&& b
->options
->caps
.gcn_shader
)) {
396 val
->ext_handler
= vtn_handle_amd_gcn_shader_instruction
;
397 } else if ((strcmp(ext
, "SPV_AMD_shader_trinary_minmax") == 0)
398 && (b
->options
&& b
->options
->caps
.trinary_minmax
)) {
399 val
->ext_handler
= vtn_handle_amd_shader_trinary_minmax_instruction
;
400 } else if (strcmp(ext
, "OpenCL.std") == 0) {
401 val
->ext_handler
= vtn_handle_opencl_instruction
;
403 vtn_fail("Unsupported extension: %s", ext
);
409 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
410 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
416 vtn_fail("Unhandled opcode");
421 _foreach_decoration_helper(struct vtn_builder
*b
,
422 struct vtn_value
*base_value
,
424 struct vtn_value
*value
,
425 vtn_decoration_foreach_cb cb
, void *data
)
427 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
429 if (dec
->scope
== VTN_DEC_DECORATION
) {
430 member
= parent_member
;
431 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
432 vtn_fail_if(value
->value_type
!= vtn_value_type_type
||
433 value
->type
->base_type
!= vtn_base_type_struct
,
434 "OpMemberDecorate and OpGroupMemberDecorate are only "
435 "allowed on OpTypeStruct");
436 /* This means we haven't recursed yet */
437 assert(value
== base_value
);
439 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
441 vtn_fail_if(member
>= base_value
->type
->length
,
442 "OpMemberDecorate specifies member %d but the "
443 "OpTypeStruct has only %u members",
444 member
, base_value
->type
->length
);
446 /* Not a decoration */
447 assert(dec
->scope
== VTN_DEC_EXECUTION_MODE
);
452 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
453 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
456 cb(b
, base_value
, member
, dec
, data
);
461 /** Iterates (recursively if needed) over all of the decorations on a value
463 * This function iterates over all of the decorations applied to a given
464 * value. If it encounters a decoration group, it recurses into the group
465 * and iterates over all of those decorations as well.
468 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
469 vtn_decoration_foreach_cb cb
, void *data
)
471 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
475 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
476 vtn_execution_mode_foreach_cb cb
, void *data
)
478 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
479 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
482 assert(dec
->group
== NULL
);
483 cb(b
, value
, dec
, data
);
488 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
489 const uint32_t *w
, unsigned count
)
491 const uint32_t *w_end
= w
+ count
;
492 const uint32_t target
= w
[1];
496 case SpvOpDecorationGroup
:
497 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
501 case SpvOpMemberDecorate
:
502 case SpvOpDecorateStringGOOGLE
:
503 case SpvOpMemberDecorateStringGOOGLE
:
504 case SpvOpExecutionMode
:
505 case SpvOpExecutionModeId
: {
506 struct vtn_value
*val
= vtn_untyped_value(b
, target
);
508 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
511 case SpvOpDecorateStringGOOGLE
:
512 dec
->scope
= VTN_DEC_DECORATION
;
514 case SpvOpMemberDecorate
:
515 case SpvOpMemberDecorateStringGOOGLE
:
516 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
517 vtn_fail_if(dec
->scope
< VTN_DEC_STRUCT_MEMBER0
, /* overflow */
518 "Member argument of OpMemberDecorate too large");
520 case SpvOpExecutionMode
:
521 case SpvOpExecutionModeId
:
522 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
525 unreachable("Invalid decoration opcode");
527 dec
->decoration
= *(w
++);
530 /* Link into the list */
531 dec
->next
= val
->decoration
;
532 val
->decoration
= dec
;
536 case SpvOpGroupMemberDecorate
:
537 case SpvOpGroupDecorate
: {
538 struct vtn_value
*group
=
539 vtn_value(b
, target
, vtn_value_type_decoration_group
);
541 for (; w
< w_end
; w
++) {
542 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
543 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
546 if (opcode
== SpvOpGroupDecorate
) {
547 dec
->scope
= VTN_DEC_DECORATION
;
549 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
550 vtn_fail_if(dec
->scope
< 0, /* Check for overflow */
551 "Member argument of OpGroupMemberDecorate too large");
554 /* Link into the list */
555 dec
->next
= val
->decoration
;
556 val
->decoration
= dec
;
562 unreachable("Unhandled opcode");
566 struct member_decoration_ctx
{
568 struct glsl_struct_field
*fields
;
569 struct vtn_type
*type
;
573 * Returns true if the given type contains a struct decorated Block or
577 vtn_type_contains_block(struct vtn_builder
*b
, struct vtn_type
*type
)
579 switch (type
->base_type
) {
580 case vtn_base_type_array
:
581 return vtn_type_contains_block(b
, type
->array_element
);
582 case vtn_base_type_struct
:
583 if (type
->block
|| type
->buffer_block
)
585 for (unsigned i
= 0; i
< type
->length
; i
++) {
586 if (vtn_type_contains_block(b
, type
->members
[i
]))
595 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
596 * OpStore, or OpCopyMemory between them without breaking anything.
597 * Technically, the SPIR-V rules require the exact same type ID but this lets
598 * us internally be a bit looser.
601 vtn_types_compatible(struct vtn_builder
*b
,
602 struct vtn_type
*t1
, struct vtn_type
*t2
)
604 if (t1
->id
== t2
->id
)
607 if (t1
->base_type
!= t2
->base_type
)
610 switch (t1
->base_type
) {
611 case vtn_base_type_void
:
612 case vtn_base_type_scalar
:
613 case vtn_base_type_vector
:
614 case vtn_base_type_matrix
:
615 case vtn_base_type_image
:
616 case vtn_base_type_sampler
:
617 case vtn_base_type_sampled_image
:
618 return t1
->type
== t2
->type
;
620 case vtn_base_type_array
:
621 return t1
->length
== t2
->length
&&
622 vtn_types_compatible(b
, t1
->array_element
, t2
->array_element
);
624 case vtn_base_type_pointer
:
625 return vtn_types_compatible(b
, t1
->deref
, t2
->deref
);
627 case vtn_base_type_struct
:
628 if (t1
->length
!= t2
->length
)
631 for (unsigned i
= 0; i
< t1
->length
; i
++) {
632 if (!vtn_types_compatible(b
, t1
->members
[i
], t2
->members
[i
]))
637 case vtn_base_type_function
:
638 /* This case shouldn't get hit since you can't copy around function
639 * types. Just require them to be identical.
644 vtn_fail("Invalid base type");
647 /* does a shallow copy of a vtn_type */
649 static struct vtn_type
*
650 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
652 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
655 switch (src
->base_type
) {
656 case vtn_base_type_void
:
657 case vtn_base_type_scalar
:
658 case vtn_base_type_vector
:
659 case vtn_base_type_matrix
:
660 case vtn_base_type_array
:
661 case vtn_base_type_pointer
:
662 case vtn_base_type_image
:
663 case vtn_base_type_sampler
:
664 case vtn_base_type_sampled_image
:
665 /* Nothing more to do */
668 case vtn_base_type_struct
:
669 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
670 memcpy(dest
->members
, src
->members
,
671 src
->length
* sizeof(src
->members
[0]));
673 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
674 memcpy(dest
->offsets
, src
->offsets
,
675 src
->length
* sizeof(src
->offsets
[0]));
678 case vtn_base_type_function
:
679 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
680 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
687 static struct vtn_type
*
688 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
690 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
691 type
= type
->members
[member
];
693 /* We may have an array of matrices.... Oh, joy! */
694 while (glsl_type_is_array(type
->type
)) {
695 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
696 type
= type
->array_element
;
699 vtn_assert(glsl_type_is_matrix(type
->type
));
705 vtn_handle_access_qualifier(struct vtn_builder
*b
, struct vtn_type
*type
,
706 int member
, enum gl_access_qualifier access
)
708 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
709 type
= type
->members
[member
];
711 type
->access
|= access
;
715 array_stride_decoration_cb(struct vtn_builder
*b
,
716 struct vtn_value
*val
, int member
,
717 const struct vtn_decoration
*dec
, void *void_ctx
)
719 struct vtn_type
*type
= val
->type
;
721 if (dec
->decoration
== SpvDecorationArrayStride
) {
722 vtn_fail_if(dec
->literals
[0] == 0, "ArrayStride must be non-zero");
723 type
->stride
= dec
->literals
[0];
728 struct_member_decoration_cb(struct vtn_builder
*b
,
729 struct vtn_value
*val
, int member
,
730 const struct vtn_decoration
*dec
, void *void_ctx
)
732 struct member_decoration_ctx
*ctx
= void_ctx
;
737 assert(member
< ctx
->num_fields
);
739 switch (dec
->decoration
) {
740 case SpvDecorationRelaxedPrecision
:
741 case SpvDecorationUniform
:
742 break; /* FIXME: Do nothing with this for now. */
743 case SpvDecorationNonWritable
:
744 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_WRITEABLE
);
746 case SpvDecorationNonReadable
:
747 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_READABLE
);
749 case SpvDecorationVolatile
:
750 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_VOLATILE
);
752 case SpvDecorationCoherent
:
753 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_COHERENT
);
755 case SpvDecorationNoPerspective
:
756 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
758 case SpvDecorationFlat
:
759 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
761 case SpvDecorationCentroid
:
762 ctx
->fields
[member
].centroid
= true;
764 case SpvDecorationSample
:
765 ctx
->fields
[member
].sample
= true;
767 case SpvDecorationStream
:
768 /* Vulkan only allows one GS stream */
769 vtn_assert(dec
->literals
[0] == 0);
771 case SpvDecorationLocation
:
772 ctx
->fields
[member
].location
= dec
->literals
[0];
774 case SpvDecorationComponent
:
775 break; /* FIXME: What should we do with these? */
776 case SpvDecorationBuiltIn
:
777 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
778 ctx
->type
->members
[member
]->is_builtin
= true;
779 ctx
->type
->members
[member
]->builtin
= dec
->literals
[0];
780 ctx
->type
->builtin_block
= true;
782 case SpvDecorationOffset
:
783 ctx
->type
->offsets
[member
] = dec
->literals
[0];
784 ctx
->fields
[member
].offset
= dec
->literals
[0];
786 case SpvDecorationMatrixStride
:
787 /* Handled as a second pass */
789 case SpvDecorationColMajor
:
790 break; /* Nothing to do here. Column-major is the default. */
791 case SpvDecorationRowMajor
:
792 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
795 case SpvDecorationPatch
:
798 case SpvDecorationSpecId
:
799 case SpvDecorationBlock
:
800 case SpvDecorationBufferBlock
:
801 case SpvDecorationArrayStride
:
802 case SpvDecorationGLSLShared
:
803 case SpvDecorationGLSLPacked
:
804 case SpvDecorationInvariant
:
805 case SpvDecorationRestrict
:
806 case SpvDecorationAliased
:
807 case SpvDecorationConstant
:
808 case SpvDecorationIndex
:
809 case SpvDecorationBinding
:
810 case SpvDecorationDescriptorSet
:
811 case SpvDecorationLinkageAttributes
:
812 case SpvDecorationNoContraction
:
813 case SpvDecorationInputAttachmentIndex
:
814 vtn_warn("Decoration not allowed on struct members: %s",
815 spirv_decoration_to_string(dec
->decoration
));
818 case SpvDecorationXfbBuffer
:
819 case SpvDecorationXfbStride
:
820 vtn_warn("Vulkan does not have transform feedback");
823 case SpvDecorationCPacked
:
824 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
825 vtn_warn("Decoration only allowed for CL-style kernels: %s",
826 spirv_decoration_to_string(dec
->decoration
));
828 ctx
->type
->packed
= true;
831 case SpvDecorationSaturatedConversion
:
832 case SpvDecorationFuncParamAttr
:
833 case SpvDecorationFPRoundingMode
:
834 case SpvDecorationFPFastMathMode
:
835 case SpvDecorationAlignment
:
836 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
837 vtn_warn("Decoration only allowed for CL-style kernels: %s",
838 spirv_decoration_to_string(dec
->decoration
));
842 case SpvDecorationHlslSemanticGOOGLE
:
843 /* HLSL semantic decorations can safely be ignored by the driver. */
847 vtn_fail("Unhandled decoration");
851 /** Chases the array type all the way down to the tail and rewrites the
852 * glsl_types to be based off the tail's glsl_type.
855 vtn_array_type_rewrite_glsl_type(struct vtn_type
*type
)
857 if (type
->base_type
!= vtn_base_type_array
)
860 vtn_array_type_rewrite_glsl_type(type
->array_element
);
862 type
->type
= glsl_array_type(type
->array_element
->type
,
863 type
->length
, type
->stride
);
866 /* Matrix strides are handled as a separate pass because we need to know
867 * whether the matrix is row-major or not first.
870 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
871 struct vtn_value
*val
, int member
,
872 const struct vtn_decoration
*dec
,
875 if (dec
->decoration
!= SpvDecorationMatrixStride
)
878 vtn_fail_if(member
< 0,
879 "The MatrixStride decoration is only allowed on members "
881 vtn_fail_if(dec
->literals
[0] == 0, "MatrixStride must be non-zero");
883 struct member_decoration_ctx
*ctx
= void_ctx
;
885 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
886 if (mat_type
->row_major
) {
887 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
888 mat_type
->stride
= mat_type
->array_element
->stride
;
889 mat_type
->array_element
->stride
= dec
->literals
[0];
891 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
892 dec
->literals
[0], true);
893 mat_type
->array_element
->type
= glsl_get_column_type(mat_type
->type
);
895 vtn_assert(mat_type
->array_element
->stride
> 0);
896 mat_type
->stride
= dec
->literals
[0];
898 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
899 dec
->literals
[0], false);
902 /* Now that we've replaced the glsl_type with a properly strided matrix
903 * type, rewrite the member type so that it's an array of the proper kind
906 vtn_array_type_rewrite_glsl_type(ctx
->type
->members
[member
]);
907 ctx
->fields
[member
].type
= ctx
->type
->members
[member
]->type
;
911 type_decoration_cb(struct vtn_builder
*b
,
912 struct vtn_value
*val
, int member
,
913 const struct vtn_decoration
*dec
, void *ctx
)
915 struct vtn_type
*type
= val
->type
;
918 /* This should have been handled by OpTypeStruct */
919 assert(val
->type
->base_type
== vtn_base_type_struct
);
920 assert(member
>= 0 && member
< val
->type
->length
);
924 switch (dec
->decoration
) {
925 case SpvDecorationArrayStride
:
926 vtn_assert(type
->base_type
== vtn_base_type_array
||
927 type
->base_type
== vtn_base_type_pointer
);
929 case SpvDecorationBlock
:
930 vtn_assert(type
->base_type
== vtn_base_type_struct
);
933 case SpvDecorationBufferBlock
:
934 vtn_assert(type
->base_type
== vtn_base_type_struct
);
935 type
->buffer_block
= true;
937 case SpvDecorationGLSLShared
:
938 case SpvDecorationGLSLPacked
:
939 /* Ignore these, since we get explicit offsets anyways */
942 case SpvDecorationRowMajor
:
943 case SpvDecorationColMajor
:
944 case SpvDecorationMatrixStride
:
945 case SpvDecorationBuiltIn
:
946 case SpvDecorationNoPerspective
:
947 case SpvDecorationFlat
:
948 case SpvDecorationPatch
:
949 case SpvDecorationCentroid
:
950 case SpvDecorationSample
:
951 case SpvDecorationVolatile
:
952 case SpvDecorationCoherent
:
953 case SpvDecorationNonWritable
:
954 case SpvDecorationNonReadable
:
955 case SpvDecorationUniform
:
956 case SpvDecorationLocation
:
957 case SpvDecorationComponent
:
958 case SpvDecorationOffset
:
959 case SpvDecorationXfbBuffer
:
960 case SpvDecorationXfbStride
:
961 case SpvDecorationHlslSemanticGOOGLE
:
962 vtn_warn("Decoration only allowed for struct members: %s",
963 spirv_decoration_to_string(dec
->decoration
));
966 case SpvDecorationStream
:
967 /* We don't need to do anything here, as stream is filled up when
968 * aplying the decoration to a variable, just check that if it is not a
969 * struct member, it should be a struct.
971 vtn_assert(type
->base_type
== vtn_base_type_struct
);
974 case SpvDecorationRelaxedPrecision
:
975 case SpvDecorationSpecId
:
976 case SpvDecorationInvariant
:
977 case SpvDecorationRestrict
:
978 case SpvDecorationAliased
:
979 case SpvDecorationConstant
:
980 case SpvDecorationIndex
:
981 case SpvDecorationBinding
:
982 case SpvDecorationDescriptorSet
:
983 case SpvDecorationLinkageAttributes
:
984 case SpvDecorationNoContraction
:
985 case SpvDecorationInputAttachmentIndex
:
986 vtn_warn("Decoration not allowed on types: %s",
987 spirv_decoration_to_string(dec
->decoration
));
990 case SpvDecorationCPacked
:
991 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
992 vtn_warn("Decoration only allowed for CL-style kernels: %s",
993 spirv_decoration_to_string(dec
->decoration
));
998 case SpvDecorationSaturatedConversion
:
999 case SpvDecorationFuncParamAttr
:
1000 case SpvDecorationFPRoundingMode
:
1001 case SpvDecorationFPFastMathMode
:
1002 case SpvDecorationAlignment
:
1003 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1004 spirv_decoration_to_string(dec
->decoration
));
1008 vtn_fail("Unhandled decoration");
1013 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
1016 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
1017 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
1018 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
1019 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
1020 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
1021 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
1022 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
1023 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
1024 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
1025 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
1026 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
1027 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
1028 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
1029 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
1030 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
1031 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
1032 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
1033 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
1034 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
1035 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
1036 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
1037 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
1038 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
1039 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
1040 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
1041 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
1042 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
1043 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
1044 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
1045 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
1046 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
1047 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
1048 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
1049 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
1050 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
1051 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
1052 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
1053 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
1054 case SpvImageFormatR16ui
: return 0x8234; /* GL_R16UI */
1055 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
1057 vtn_fail("Invalid image format");
1061 static struct vtn_type
*
1062 vtn_type_layout_std430(struct vtn_builder
*b
, struct vtn_type
*type
,
1063 uint32_t *size_out
, uint32_t *align_out
)
1065 switch (type
->base_type
) {
1066 case vtn_base_type_scalar
: {
1067 uint32_t comp_size
= glsl_type_is_boolean(type
->type
)
1068 ? 4 : glsl_get_bit_size(type
->type
) / 8;
1069 *size_out
= comp_size
;
1070 *align_out
= comp_size
;
1074 case vtn_base_type_vector
: {
1075 uint32_t comp_size
= glsl_type_is_boolean(type
->type
)
1076 ? 4 : glsl_get_bit_size(type
->type
) / 8;
1077 unsigned align_comps
= type
->length
== 3 ? 4 : type
->length
;
1078 *size_out
= comp_size
* type
->length
,
1079 *align_out
= comp_size
* align_comps
;
1083 case vtn_base_type_matrix
:
1084 case vtn_base_type_array
: {
1085 /* We're going to add an array stride */
1086 type
= vtn_type_copy(b
, type
);
1087 uint32_t elem_size
, elem_align
;
1088 type
->array_element
= vtn_type_layout_std430(b
, type
->array_element
,
1089 &elem_size
, &elem_align
);
1090 type
->stride
= vtn_align_u32(elem_size
, elem_align
);
1091 *size_out
= type
->stride
* type
->length
;
1092 *align_out
= elem_align
;
1096 case vtn_base_type_struct
: {
1097 /* We're going to add member offsets */
1098 type
= vtn_type_copy(b
, type
);
1099 uint32_t offset
= 0;
1101 for (unsigned i
= 0; i
< type
->length
; i
++) {
1102 uint32_t mem_size
, mem_align
;
1103 type
->members
[i
] = vtn_type_layout_std430(b
, type
->members
[i
],
1104 &mem_size
, &mem_align
);
1105 offset
= vtn_align_u32(offset
, mem_align
);
1106 type
->offsets
[i
] = offset
;
1108 align
= MAX2(align
, mem_align
);
1116 unreachable("Invalid SPIR-V type for std430");
1121 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
1122 const uint32_t *w
, unsigned count
)
1124 struct vtn_value
*val
= NULL
;
1126 /* In order to properly handle forward declarations, we have to defer
1127 * allocation for pointer types.
1129 if (opcode
!= SpvOpTypePointer
&& opcode
!= SpvOpTypeForwardPointer
) {
1130 val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1131 vtn_fail_if(val
->type
!= NULL
,
1132 "Only pointers can have forward declarations");
1133 val
->type
= rzalloc(b
, struct vtn_type
);
1134 val
->type
->id
= w
[1];
1139 val
->type
->base_type
= vtn_base_type_void
;
1140 val
->type
->type
= glsl_void_type();
1143 val
->type
->base_type
= vtn_base_type_scalar
;
1144 val
->type
->type
= glsl_bool_type();
1145 val
->type
->length
= 1;
1147 case SpvOpTypeInt
: {
1148 int bit_size
= w
[2];
1149 const bool signedness
= w
[3];
1150 val
->type
->base_type
= vtn_base_type_scalar
;
1153 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1156 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1159 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1162 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1165 vtn_fail("Invalid int bit size");
1167 val
->type
->length
= 1;
1171 case SpvOpTypeFloat
: {
1172 int bit_size
= w
[2];
1173 val
->type
->base_type
= vtn_base_type_scalar
;
1176 val
->type
->type
= glsl_float16_t_type();
1179 val
->type
->type
= glsl_float_type();
1182 val
->type
->type
= glsl_double_type();
1185 vtn_fail("Invalid float bit size");
1187 val
->type
->length
= 1;
1191 case SpvOpTypeVector
: {
1192 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1193 unsigned elems
= w
[3];
1195 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1196 "Base type for OpTypeVector must be a scalar");
1197 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1198 "Invalid component count for OpTypeVector");
1200 val
->type
->base_type
= vtn_base_type_vector
;
1201 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1202 val
->type
->length
= elems
;
1203 val
->type
->stride
= glsl_type_is_boolean(val
->type
->type
)
1204 ? 4 : glsl_get_bit_size(base
->type
) / 8;
1205 val
->type
->array_element
= base
;
1209 case SpvOpTypeMatrix
: {
1210 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1211 unsigned columns
= w
[3];
1213 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1214 "Base type for OpTypeMatrix must be a vector");
1215 vtn_fail_if(columns
< 2 || columns
> 4,
1216 "Invalid column count for OpTypeMatrix");
1218 val
->type
->base_type
= vtn_base_type_matrix
;
1219 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1220 glsl_get_vector_elements(base
->type
),
1222 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1223 "Unsupported base type for OpTypeMatrix");
1224 assert(!glsl_type_is_error(val
->type
->type
));
1225 val
->type
->length
= columns
;
1226 val
->type
->array_element
= base
;
1227 val
->type
->row_major
= false;
1228 val
->type
->stride
= 0;
1232 case SpvOpTypeRuntimeArray
:
1233 case SpvOpTypeArray
: {
1234 struct vtn_type
*array_element
=
1235 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1237 if (opcode
== SpvOpTypeRuntimeArray
) {
1238 /* A length of 0 is used to denote unsized arrays */
1239 val
->type
->length
= 0;
1242 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
1245 val
->type
->base_type
= vtn_base_type_array
;
1246 val
->type
->array_element
= array_element
;
1247 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
1248 val
->type
->stride
= glsl_get_cl_size(array_element
->type
);
1250 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1251 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
,
1256 case SpvOpTypeStruct
: {
1257 unsigned num_fields
= count
- 2;
1258 val
->type
->base_type
= vtn_base_type_struct
;
1259 val
->type
->length
= num_fields
;
1260 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1261 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1262 val
->type
->packed
= false;
1264 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1265 for (unsigned i
= 0; i
< num_fields
; i
++) {
1266 val
->type
->members
[i
] =
1267 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
1268 fields
[i
] = (struct glsl_struct_field
) {
1269 .type
= val
->type
->members
[i
]->type
,
1270 .name
= ralloc_asprintf(b
, "field%d", i
),
1276 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1277 unsigned offset
= 0;
1278 for (unsigned i
= 0; i
< num_fields
; i
++) {
1279 offset
= align(offset
, glsl_get_cl_alignment(fields
[i
].type
));
1280 fields
[i
].offset
= offset
;
1281 offset
+= glsl_get_cl_size(fields
[i
].type
);
1285 struct member_decoration_ctx ctx
= {
1286 .num_fields
= num_fields
,
1291 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1292 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1294 const char *name
= val
->name
? val
->name
: "struct";
1296 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
, false);
1300 case SpvOpTypeFunction
: {
1301 val
->type
->base_type
= vtn_base_type_function
;
1302 val
->type
->type
= NULL
;
1304 val
->type
->return_type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1306 const unsigned num_params
= count
- 3;
1307 val
->type
->length
= num_params
;
1308 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1309 for (unsigned i
= 0; i
< count
- 3; i
++) {
1310 val
->type
->params
[i
] =
1311 vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
;
1316 case SpvOpTypePointer
:
1317 case SpvOpTypeForwardPointer
: {
1318 /* We can't blindly push the value because it might be a forward
1321 val
= vtn_untyped_value(b
, w
[1]);
1323 SpvStorageClass storage_class
= w
[2];
1325 if (val
->value_type
== vtn_value_type_invalid
) {
1326 val
->value_type
= vtn_value_type_type
;
1327 val
->type
= rzalloc(b
, struct vtn_type
);
1328 val
->type
->id
= w
[1];
1329 val
->type
->base_type
= vtn_base_type_pointer
;
1330 val
->type
->storage_class
= storage_class
;
1332 /* These can actually be stored to nir_variables and used as SSA
1333 * values so they need a real glsl_type.
1335 switch (storage_class
) {
1336 case SpvStorageClassUniform
:
1337 val
->type
->type
= b
->options
->ubo_ptr_type
;
1339 case SpvStorageClassStorageBuffer
:
1340 val
->type
->type
= b
->options
->ssbo_ptr_type
;
1342 case SpvStorageClassPhysicalStorageBufferEXT
:
1343 val
->type
->type
= b
->options
->phys_ssbo_ptr_type
;
1345 case SpvStorageClassPushConstant
:
1346 val
->type
->type
= b
->options
->push_const_ptr_type
;
1348 case SpvStorageClassWorkgroup
:
1349 val
->type
->type
= b
->options
->shared_ptr_type
;
1351 case SpvStorageClassCrossWorkgroup
:
1352 val
->type
->type
= b
->options
->global_ptr_type
;
1354 case SpvStorageClassFunction
:
1355 if (b
->physical_ptrs
)
1356 val
->type
->type
= b
->options
->temp_ptr_type
;
1359 /* In this case, no variable pointers are allowed so all deref
1360 * chains are complete back to the variable and it doesn't matter
1361 * what type gets used so we leave it NULL.
1366 vtn_fail_if(val
->type
->storage_class
!= storage_class
,
1367 "The storage classes of an OpTypePointer and any "
1368 "OpTypeForwardPointers that provide forward "
1369 "declarations of it must match.");
1372 if (opcode
== SpvOpTypePointer
) {
1373 vtn_fail_if(val
->type
->deref
!= NULL
,
1374 "While OpTypeForwardPointer can be used to provide a "
1375 "forward declaration of a pointer, OpTypePointer can "
1376 "only be used once for a given id.");
1378 val
->type
->deref
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
1380 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1382 if (b
->physical_ptrs
) {
1383 switch (storage_class
) {
1384 case SpvStorageClassFunction
:
1385 case SpvStorageClassWorkgroup
:
1386 case SpvStorageClassCrossWorkgroup
:
1387 val
->type
->stride
= align(glsl_get_cl_size(val
->type
->deref
->type
),
1388 glsl_get_cl_alignment(val
->type
->deref
->type
));
1395 if (storage_class
== SpvStorageClassWorkgroup
&&
1396 b
->options
->lower_workgroup_access_to_offsets
) {
1397 uint32_t size
, align
;
1398 val
->type
->deref
= vtn_type_layout_std430(b
, val
->type
->deref
,
1400 val
->type
->length
= size
;
1401 val
->type
->align
= align
;
1407 case SpvOpTypeImage
: {
1408 val
->type
->base_type
= vtn_base_type_image
;
1410 const struct vtn_type
*sampled_type
=
1411 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1413 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1414 glsl_get_bit_size(sampled_type
->type
) != 32,
1415 "Sampled type of OpTypeImage must be a 32-bit scalar");
1417 enum glsl_sampler_dim dim
;
1418 switch ((SpvDim
)w
[3]) {
1419 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1420 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1421 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1422 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1423 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1424 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1425 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1427 vtn_fail("Invalid SPIR-V image dimensionality");
1430 /* w[4]: as per Vulkan spec "Validation Rules within a Module",
1431 * The “Depth” operand of OpTypeImage is ignored.
1433 bool is_array
= w
[5];
1434 bool multisampled
= w
[6];
1435 unsigned sampled
= w
[7];
1436 SpvImageFormat format
= w
[8];
1439 val
->type
->access_qualifier
= w
[9];
1441 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1444 if (dim
== GLSL_SAMPLER_DIM_2D
)
1445 dim
= GLSL_SAMPLER_DIM_MS
;
1446 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1447 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1449 vtn_fail("Unsupported multisampled image type");
1452 val
->type
->image_format
= translate_image_format(b
, format
);
1454 enum glsl_base_type sampled_base_type
=
1455 glsl_get_base_type(sampled_type
->type
);
1457 val
->type
->sampled
= true;
1458 val
->type
->type
= glsl_sampler_type(dim
, false, is_array
,
1460 } else if (sampled
== 2) {
1461 val
->type
->sampled
= false;
1462 val
->type
->type
= glsl_image_type(dim
, is_array
, sampled_base_type
);
1464 vtn_fail("We need to know if the image will be sampled");
1469 case SpvOpTypeSampledImage
:
1470 val
->type
->base_type
= vtn_base_type_sampled_image
;
1471 val
->type
->image
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1472 val
->type
->type
= val
->type
->image
->type
;
1475 case SpvOpTypeSampler
:
1476 /* The actual sampler type here doesn't really matter. It gets
1477 * thrown away the moment you combine it with an image. What really
1478 * matters is that it's a sampler type as opposed to an integer type
1479 * so the backend knows what to do.
1481 val
->type
->base_type
= vtn_base_type_sampler
;
1482 val
->type
->type
= glsl_bare_sampler_type();
1485 case SpvOpTypeOpaque
:
1486 case SpvOpTypeEvent
:
1487 case SpvOpTypeDeviceEvent
:
1488 case SpvOpTypeReserveId
:
1489 case SpvOpTypeQueue
:
1492 vtn_fail("Unhandled opcode");
1495 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1497 if (val
->type
->base_type
== vtn_base_type_struct
&&
1498 (val
->type
->block
|| val
->type
->buffer_block
)) {
1499 for (unsigned i
= 0; i
< val
->type
->length
; i
++) {
1500 vtn_fail_if(vtn_type_contains_block(b
, val
->type
->members
[i
]),
1501 "Block and BufferBlock decorations cannot decorate a "
1502 "structure type that is nested at any level inside "
1503 "another structure type decorated with Block or "
1509 static nir_constant
*
1510 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
1512 nir_constant
*c
= rzalloc(b
, nir_constant
);
1514 /* For pointers and other typeless things, we have to return something but
1515 * it doesn't matter what.
1520 switch (glsl_get_base_type(type
)) {
1522 case GLSL_TYPE_UINT
:
1523 case GLSL_TYPE_INT16
:
1524 case GLSL_TYPE_UINT16
:
1525 case GLSL_TYPE_UINT8
:
1526 case GLSL_TYPE_INT8
:
1527 case GLSL_TYPE_INT64
:
1528 case GLSL_TYPE_UINT64
:
1529 case GLSL_TYPE_BOOL
:
1530 case GLSL_TYPE_FLOAT
:
1531 case GLSL_TYPE_FLOAT16
:
1532 case GLSL_TYPE_DOUBLE
:
1533 /* Nothing to do here. It's already initialized to zero */
1536 case GLSL_TYPE_ARRAY
:
1537 vtn_assert(glsl_get_length(type
) > 0);
1538 c
->num_elements
= glsl_get_length(type
);
1539 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1541 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
1542 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1543 c
->elements
[i
] = c
->elements
[0];
1546 case GLSL_TYPE_STRUCT
:
1547 c
->num_elements
= glsl_get_length(type
);
1548 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1550 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
1551 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
1556 vtn_fail("Invalid type for null constant");
1563 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
1564 int member
, const struct vtn_decoration
*dec
,
1567 vtn_assert(member
== -1);
1568 if (dec
->decoration
!= SpvDecorationSpecId
)
1571 struct spec_constant_value
*const_value
= data
;
1573 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1574 if (b
->specializations
[i
].id
== dec
->literals
[0]) {
1575 if (const_value
->is_double
)
1576 const_value
->data64
= b
->specializations
[i
].data64
;
1578 const_value
->data32
= b
->specializations
[i
].data32
;
1585 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
1586 uint32_t const_value
)
1588 struct spec_constant_value data
;
1589 data
.is_double
= false;
1590 data
.data32
= const_value
;
1591 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1596 get_specialization64(struct vtn_builder
*b
, struct vtn_value
*val
,
1597 uint64_t const_value
)
1599 struct spec_constant_value data
;
1600 data
.is_double
= true;
1601 data
.data64
= const_value
;
1602 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1607 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1608 struct vtn_value
*val
,
1610 const struct vtn_decoration
*dec
,
1613 vtn_assert(member
== -1);
1614 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1615 dec
->literals
[0] != SpvBuiltInWorkgroupSize
)
1618 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1619 b
->workgroup_size_builtin
= val
;
1623 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1624 const uint32_t *w
, unsigned count
)
1626 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1627 val
->constant
= rzalloc(b
, nir_constant
);
1629 case SpvOpConstantTrue
:
1630 case SpvOpConstantFalse
:
1631 case SpvOpSpecConstantTrue
:
1632 case SpvOpSpecConstantFalse
: {
1633 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1634 "Result type of %s must be OpTypeBool",
1635 spirv_op_to_string(opcode
));
1637 uint32_t int_val
= (opcode
== SpvOpConstantTrue
||
1638 opcode
== SpvOpSpecConstantTrue
);
1640 if (opcode
== SpvOpSpecConstantTrue
||
1641 opcode
== SpvOpSpecConstantFalse
)
1642 int_val
= get_specialization(b
, val
, int_val
);
1644 val
->constant
->values
[0].b
[0] = int_val
!= 0;
1648 case SpvOpConstant
: {
1649 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1650 "Result type of %s must be a scalar",
1651 spirv_op_to_string(opcode
));
1652 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1655 val
->constant
->values
->u64
[0] = vtn_u64_literal(&w
[3]);
1658 val
->constant
->values
->u32
[0] = w
[3];
1661 val
->constant
->values
->u16
[0] = w
[3];
1664 val
->constant
->values
->u8
[0] = w
[3];
1667 vtn_fail("Unsupported SpvOpConstant bit size");
1672 case SpvOpSpecConstant
: {
1673 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1674 "Result type of %s must be a scalar",
1675 spirv_op_to_string(opcode
));
1676 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1679 val
->constant
->values
[0].u64
[0] =
1680 get_specialization64(b
, val
, vtn_u64_literal(&w
[3]));
1683 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1686 val
->constant
->values
[0].u16
[0] = get_specialization(b
, val
, w
[3]);
1689 val
->constant
->values
[0].u8
[0] = get_specialization(b
, val
, w
[3]);
1692 vtn_fail("Unsupported SpvOpSpecConstant bit size");
1697 case SpvOpSpecConstantComposite
:
1698 case SpvOpConstantComposite
: {
1699 unsigned elem_count
= count
- 3;
1700 vtn_fail_if(elem_count
!= val
->type
->length
,
1701 "%s has %u constituents, expected %u",
1702 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1704 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1705 for (unsigned i
= 0; i
< elem_count
; i
++) {
1706 struct vtn_value
*val
= vtn_untyped_value(b
, w
[i
+ 3]);
1708 if (val
->value_type
== vtn_value_type_constant
) {
1709 elems
[i
] = val
->constant
;
1711 vtn_fail_if(val
->value_type
!= vtn_value_type_undef
,
1712 "only constants or undefs allowed for "
1713 "SpvOpConstantComposite");
1714 /* to make it easier, just insert a NULL constant for now */
1715 elems
[i
] = vtn_null_constant(b
, val
->type
->type
);
1719 switch (val
->type
->base_type
) {
1720 case vtn_base_type_vector
: {
1721 assert(glsl_type_is_vector(val
->type
->type
));
1722 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1723 for (unsigned i
= 0; i
< elem_count
; i
++) {
1726 val
->constant
->values
[0].u64
[i
] = elems
[i
]->values
[0].u64
[0];
1729 val
->constant
->values
[0].u32
[i
] = elems
[i
]->values
[0].u32
[0];
1732 val
->constant
->values
[0].u16
[i
] = elems
[i
]->values
[0].u16
[0];
1735 val
->constant
->values
[0].u8
[i
] = elems
[i
]->values
[0].u8
[0];
1738 val
->constant
->values
[0].b
[i
] = elems
[i
]->values
[0].b
[0];
1741 vtn_fail("Invalid SpvOpConstantComposite bit size");
1747 case vtn_base_type_matrix
:
1748 assert(glsl_type_is_matrix(val
->type
->type
));
1749 for (unsigned i
= 0; i
< elem_count
; i
++)
1750 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1753 case vtn_base_type_struct
:
1754 case vtn_base_type_array
:
1755 ralloc_steal(val
->constant
, elems
);
1756 val
->constant
->num_elements
= elem_count
;
1757 val
->constant
->elements
= elems
;
1761 vtn_fail("Result type of %s must be a composite type",
1762 spirv_op_to_string(opcode
));
1767 case SpvOpSpecConstantOp
: {
1768 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1770 case SpvOpVectorShuffle
: {
1771 struct vtn_value
*v0
= &b
->values
[w
[4]];
1772 struct vtn_value
*v1
= &b
->values
[w
[5]];
1774 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1775 v0
->value_type
== vtn_value_type_undef
);
1776 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1777 v1
->value_type
== vtn_value_type_undef
);
1779 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1780 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1782 vtn_assert(len0
+ len1
< 16);
1784 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1785 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1786 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1788 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1789 (void)bit_size0
; (void)bit_size1
;
1791 if (bit_size
== 64) {
1793 if (v0
->value_type
== vtn_value_type_constant
) {
1794 for (unsigned i
= 0; i
< len0
; i
++)
1795 u64
[i
] = v0
->constant
->values
[0].u64
[i
];
1797 if (v1
->value_type
== vtn_value_type_constant
) {
1798 for (unsigned i
= 0; i
< len1
; i
++)
1799 u64
[len0
+ i
] = v1
->constant
->values
[0].u64
[i
];
1802 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1803 uint32_t comp
= w
[i
+ 6];
1804 /* If component is not used, set the value to a known constant
1805 * to detect if it is wrongly used.
1807 if (comp
== (uint32_t)-1)
1808 val
->constant
->values
[0].u64
[j
] = 0xdeadbeefdeadbeef;
1810 val
->constant
->values
[0].u64
[j
] = u64
[comp
];
1813 /* This is for both 32-bit and 16-bit values */
1815 if (v0
->value_type
== vtn_value_type_constant
) {
1816 for (unsigned i
= 0; i
< len0
; i
++)
1817 u32
[i
] = v0
->constant
->values
[0].u32
[i
];
1819 if (v1
->value_type
== vtn_value_type_constant
) {
1820 for (unsigned i
= 0; i
< len1
; i
++)
1821 u32
[len0
+ i
] = v1
->constant
->values
[0].u32
[i
];
1824 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1825 uint32_t comp
= w
[i
+ 6];
1826 /* If component is not used, set the value to a known constant
1827 * to detect if it is wrongly used.
1829 if (comp
== (uint32_t)-1)
1830 val
->constant
->values
[0].u32
[j
] = 0xdeadbeef;
1832 val
->constant
->values
[0].u32
[j
] = u32
[comp
];
1838 case SpvOpCompositeExtract
:
1839 case SpvOpCompositeInsert
: {
1840 struct vtn_value
*comp
;
1841 unsigned deref_start
;
1842 struct nir_constant
**c
;
1843 if (opcode
== SpvOpCompositeExtract
) {
1844 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1846 c
= &comp
->constant
;
1848 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1850 val
->constant
= nir_constant_clone(comp
->constant
,
1857 const struct vtn_type
*type
= comp
->type
;
1858 for (unsigned i
= deref_start
; i
< count
; i
++) {
1859 vtn_fail_if(w
[i
] > type
->length
,
1860 "%uth index of %s is %u but the type has only "
1861 "%u elements", i
- deref_start
,
1862 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1864 switch (type
->base_type
) {
1865 case vtn_base_type_vector
:
1867 type
= type
->array_element
;
1870 case vtn_base_type_matrix
:
1871 assert(col
== 0 && elem
== -1);
1874 type
= type
->array_element
;
1877 case vtn_base_type_array
:
1878 c
= &(*c
)->elements
[w
[i
]];
1879 type
= type
->array_element
;
1882 case vtn_base_type_struct
:
1883 c
= &(*c
)->elements
[w
[i
]];
1884 type
= type
->members
[w
[i
]];
1888 vtn_fail("%s must only index into composite types",
1889 spirv_op_to_string(opcode
));
1893 if (opcode
== SpvOpCompositeExtract
) {
1897 unsigned num_components
= type
->length
;
1898 unsigned bit_size
= glsl_get_bit_size(type
->type
);
1899 for (unsigned i
= 0; i
< num_components
; i
++)
1902 val
->constant
->values
[0].u64
[i
] = (*c
)->values
[col
].u64
[elem
+ i
];
1905 val
->constant
->values
[0].u32
[i
] = (*c
)->values
[col
].u32
[elem
+ i
];
1908 val
->constant
->values
[0].u16
[i
] = (*c
)->values
[col
].u16
[elem
+ i
];
1911 val
->constant
->values
[0].u8
[i
] = (*c
)->values
[col
].u8
[elem
+ i
];
1914 val
->constant
->values
[0].b
[i
] = (*c
)->values
[col
].b
[elem
+ i
];
1917 vtn_fail("Invalid SpvOpCompositeExtract bit size");
1921 struct vtn_value
*insert
=
1922 vtn_value(b
, w
[4], vtn_value_type_constant
);
1923 vtn_assert(insert
->type
== type
);
1925 *c
= insert
->constant
;
1927 unsigned num_components
= type
->length
;
1928 unsigned bit_size
= glsl_get_bit_size(type
->type
);
1929 for (unsigned i
= 0; i
< num_components
; i
++)
1932 (*c
)->values
[col
].u64
[elem
+ i
] = insert
->constant
->values
[0].u64
[i
];
1935 (*c
)->values
[col
].u32
[elem
+ i
] = insert
->constant
->values
[0].u32
[i
];
1938 (*c
)->values
[col
].u16
[elem
+ i
] = insert
->constant
->values
[0].u16
[i
];
1941 (*c
)->values
[col
].u8
[elem
+ i
] = insert
->constant
->values
[0].u8
[i
];
1944 (*c
)->values
[col
].b
[elem
+ i
] = insert
->constant
->values
[0].b
[i
];
1947 vtn_fail("Invalid SpvOpCompositeInsert bit size");
1956 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
1957 nir_alu_type src_alu_type
= dst_alu_type
;
1958 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
1961 vtn_assert(count
<= 7);
1966 /* We have a source in a conversion */
1968 nir_get_nir_type_for_glsl_type(
1969 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1970 /* We use the bitsize of the conversion source to evaluate the opcode later */
1971 bit_size
= glsl_get_bit_size(
1972 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1975 bit_size
= glsl_get_bit_size(val
->type
->type
);
1978 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
1979 nir_alu_type_get_type_size(src_alu_type
),
1980 nir_alu_type_get_type_size(dst_alu_type
));
1981 nir_const_value src
[4];
1983 for (unsigned i
= 0; i
< count
- 4; i
++) {
1984 struct vtn_value
*src_val
=
1985 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
);
1987 /* If this is an unsized source, pull the bit size from the
1988 * source; otherwise, we'll use the bit size from the destination.
1990 if (!nir_alu_type_get_type_size(nir_op_infos
[op
].input_types
[i
]))
1991 bit_size
= glsl_get_bit_size(src_val
->type
->type
);
1993 unsigned j
= swap
? 1 - i
: i
;
1994 src
[j
] = src_val
->constant
->values
[0];
1997 /* fix up fixed size sources */
2004 for (unsigned i
= 0; i
< num_components
; ++i
) {
2006 case 64: src
[1].u32
[i
] = src
[1].u64
[i
]; break;
2007 case 16: src
[1].u32
[i
] = src
[1].u16
[i
]; break;
2008 case 8: src
[1].u32
[i
] = src
[1].u8
[i
]; break;
2017 val
->constant
->values
[0] =
2018 nir_eval_const_opcode(op
, num_components
, bit_size
, src
);
2025 case SpvOpConstantNull
:
2026 val
->constant
= vtn_null_constant(b
, val
->type
->type
);
2029 case SpvOpConstantSampler
:
2030 vtn_fail("OpConstantSampler requires Kernel Capability");
2034 vtn_fail("Unhandled opcode");
2037 /* Now that we have the value, update the workgroup size if needed */
2038 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
2041 struct vtn_ssa_value
*
2042 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
2044 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
2047 if (!glsl_type_is_vector_or_scalar(type
)) {
2048 unsigned elems
= glsl_get_length(type
);
2049 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2050 for (unsigned i
= 0; i
< elems
; i
++) {
2051 const struct glsl_type
*child_type
;
2053 switch (glsl_get_base_type(type
)) {
2055 case GLSL_TYPE_UINT
:
2056 case GLSL_TYPE_INT16
:
2057 case GLSL_TYPE_UINT16
:
2058 case GLSL_TYPE_UINT8
:
2059 case GLSL_TYPE_INT8
:
2060 case GLSL_TYPE_INT64
:
2061 case GLSL_TYPE_UINT64
:
2062 case GLSL_TYPE_BOOL
:
2063 case GLSL_TYPE_FLOAT
:
2064 case GLSL_TYPE_FLOAT16
:
2065 case GLSL_TYPE_DOUBLE
:
2066 child_type
= glsl_get_column_type(type
);
2068 case GLSL_TYPE_ARRAY
:
2069 child_type
= glsl_get_array_element(type
);
2071 case GLSL_TYPE_STRUCT
:
2072 child_type
= glsl_get_struct_field(type
, i
);
2075 vtn_fail("unkown base type");
2078 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
2086 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
2089 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
2090 src
.src_type
= type
;
2095 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
2096 const uint32_t *w
, unsigned count
)
2098 if (opcode
== SpvOpSampledImage
) {
2099 struct vtn_value
*val
=
2100 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2101 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2102 val
->sampled_image
->type
=
2103 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2104 val
->sampled_image
->image
=
2105 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2106 val
->sampled_image
->sampler
=
2107 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
2109 } else if (opcode
== SpvOpImage
) {
2110 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2111 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
2112 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
2113 val
->pointer
= src_val
->sampled_image
->image
;
2115 vtn_assert(src_val
->value_type
== vtn_value_type_pointer
);
2116 val
->pointer
= src_val
->pointer
;
2121 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2122 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2124 struct vtn_sampled_image sampled
;
2125 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
2126 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
2127 sampled
= *sampled_val
->sampled_image
;
2129 vtn_assert(sampled_val
->value_type
== vtn_value_type_pointer
);
2130 sampled
.type
= sampled_val
->pointer
->type
;
2131 sampled
.image
= NULL
;
2132 sampled
.sampler
= sampled_val
->pointer
;
2135 const struct glsl_type
*image_type
= sampled
.type
->type
;
2136 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
2137 const bool is_array
= glsl_sampler_type_is_array(image_type
);
2139 /* Figure out the base texture operation */
2142 case SpvOpImageSampleImplicitLod
:
2143 case SpvOpImageSampleDrefImplicitLod
:
2144 case SpvOpImageSampleProjImplicitLod
:
2145 case SpvOpImageSampleProjDrefImplicitLod
:
2146 texop
= nir_texop_tex
;
2149 case SpvOpImageSampleExplicitLod
:
2150 case SpvOpImageSampleDrefExplicitLod
:
2151 case SpvOpImageSampleProjExplicitLod
:
2152 case SpvOpImageSampleProjDrefExplicitLod
:
2153 texop
= nir_texop_txl
;
2156 case SpvOpImageFetch
:
2157 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
2158 texop
= nir_texop_txf_ms
;
2160 texop
= nir_texop_txf
;
2164 case SpvOpImageGather
:
2165 case SpvOpImageDrefGather
:
2166 texop
= nir_texop_tg4
;
2169 case SpvOpImageQuerySizeLod
:
2170 case SpvOpImageQuerySize
:
2171 texop
= nir_texop_txs
;
2174 case SpvOpImageQueryLod
:
2175 texop
= nir_texop_lod
;
2178 case SpvOpImageQueryLevels
:
2179 texop
= nir_texop_query_levels
;
2182 case SpvOpImageQuerySamples
:
2183 texop
= nir_texop_texture_samples
;
2187 vtn_fail("Unhandled opcode");
2190 nir_tex_src srcs
[10]; /* 10 should be enough */
2191 nir_tex_src
*p
= srcs
;
2193 nir_deref_instr
*sampler
= vtn_pointer_to_deref(b
, sampled
.sampler
);
2194 nir_deref_instr
*texture
=
2195 sampled
.image
? vtn_pointer_to_deref(b
, sampled
.image
) : sampler
;
2197 p
->src
= nir_src_for_ssa(&texture
->dest
.ssa
);
2198 p
->src_type
= nir_tex_src_texture_deref
;
2208 /* These operations require a sampler */
2209 p
->src
= nir_src_for_ssa(&sampler
->dest
.ssa
);
2210 p
->src_type
= nir_tex_src_sampler_deref
;
2214 case nir_texop_txf_ms
:
2216 case nir_texop_query_levels
:
2217 case nir_texop_texture_samples
:
2218 case nir_texop_samples_identical
:
2221 case nir_texop_txf_ms_mcs
:
2222 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2227 struct nir_ssa_def
*coord
;
2228 unsigned coord_components
;
2230 case SpvOpImageSampleImplicitLod
:
2231 case SpvOpImageSampleExplicitLod
:
2232 case SpvOpImageSampleDrefImplicitLod
:
2233 case SpvOpImageSampleDrefExplicitLod
:
2234 case SpvOpImageSampleProjImplicitLod
:
2235 case SpvOpImageSampleProjExplicitLod
:
2236 case SpvOpImageSampleProjDrefImplicitLod
:
2237 case SpvOpImageSampleProjDrefExplicitLod
:
2238 case SpvOpImageFetch
:
2239 case SpvOpImageGather
:
2240 case SpvOpImageDrefGather
:
2241 case SpvOpImageQueryLod
: {
2242 /* All these types have the coordinate as their first real argument */
2243 switch (sampler_dim
) {
2244 case GLSL_SAMPLER_DIM_1D
:
2245 case GLSL_SAMPLER_DIM_BUF
:
2246 coord_components
= 1;
2248 case GLSL_SAMPLER_DIM_2D
:
2249 case GLSL_SAMPLER_DIM_RECT
:
2250 case GLSL_SAMPLER_DIM_MS
:
2251 coord_components
= 2;
2253 case GLSL_SAMPLER_DIM_3D
:
2254 case GLSL_SAMPLER_DIM_CUBE
:
2255 coord_components
= 3;
2258 vtn_fail("Invalid sampler type");
2261 if (is_array
&& texop
!= nir_texop_lod
)
2264 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
2265 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2266 (1 << coord_components
) - 1));
2267 p
->src_type
= nir_tex_src_coord
;
2274 coord_components
= 0;
2279 case SpvOpImageSampleProjImplicitLod
:
2280 case SpvOpImageSampleProjExplicitLod
:
2281 case SpvOpImageSampleProjDrefImplicitLod
:
2282 case SpvOpImageSampleProjDrefExplicitLod
:
2283 /* These have the projector as the last coordinate component */
2284 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2285 p
->src_type
= nir_tex_src_projector
;
2293 bool is_shadow
= false;
2294 unsigned gather_component
= 0;
2296 case SpvOpImageSampleDrefImplicitLod
:
2297 case SpvOpImageSampleDrefExplicitLod
:
2298 case SpvOpImageSampleProjDrefImplicitLod
:
2299 case SpvOpImageSampleProjDrefExplicitLod
:
2300 case SpvOpImageDrefGather
:
2301 /* These all have an explicit depth value as their next source */
2303 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2306 case SpvOpImageGather
:
2307 /* This has a component as its next source */
2309 vtn_value(b
, w
[idx
++], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
2316 /* For OpImageQuerySizeLod, we always have an LOD */
2317 if (opcode
== SpvOpImageQuerySizeLod
)
2318 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2320 /* Now we need to handle some number of optional arguments */
2321 struct vtn_value
*gather_offsets
= NULL
;
2323 uint32_t operands
= w
[idx
++];
2325 if (operands
& SpvImageOperandsBiasMask
) {
2326 vtn_assert(texop
== nir_texop_tex
);
2327 texop
= nir_texop_txb
;
2328 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
2331 if (operands
& SpvImageOperandsLodMask
) {
2332 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2333 texop
== nir_texop_txs
);
2334 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2337 if (operands
& SpvImageOperandsGradMask
) {
2338 vtn_assert(texop
== nir_texop_txl
);
2339 texop
= nir_texop_txd
;
2340 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
2341 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
2344 if (operands
& SpvImageOperandsOffsetMask
||
2345 operands
& SpvImageOperandsConstOffsetMask
)
2346 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
2348 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2349 vtn_assert(texop
== nir_texop_tg4
);
2350 gather_offsets
= vtn_value(b
, w
[idx
++], vtn_value_type_constant
);
2353 if (operands
& SpvImageOperandsSampleMask
) {
2354 vtn_assert(texop
== nir_texop_txf_ms
);
2355 texop
= nir_texop_txf_ms
;
2356 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
2359 if (operands
& SpvImageOperandsMinLodMask
) {
2360 vtn_assert(texop
== nir_texop_tex
||
2361 texop
== nir_texop_txb
||
2362 texop
== nir_texop_txd
);
2363 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_min_lod
);
2366 /* We should have now consumed exactly all of the arguments */
2367 vtn_assert(idx
== count
);
2369 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2372 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2374 instr
->coord_components
= coord_components
;
2375 instr
->sampler_dim
= sampler_dim
;
2376 instr
->is_array
= is_array
;
2377 instr
->is_shadow
= is_shadow
;
2378 instr
->is_new_style_shadow
=
2379 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2380 instr
->component
= gather_component
;
2382 switch (glsl_get_sampler_result_type(image_type
)) {
2383 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
2384 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
2385 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
2386 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
2388 vtn_fail("Invalid base type for sampler result");
2391 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2392 nir_tex_instr_dest_size(instr
), 32, NULL
);
2394 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2395 nir_tex_instr_dest_size(instr
));
2397 if (gather_offsets
) {
2398 vtn_fail_if(gather_offsets
->type
->base_type
!= vtn_base_type_array
||
2399 gather_offsets
->type
->length
!= 4,
2400 "ConstOffsets must be an array of size four of vectors "
2401 "of two integer components");
2403 struct vtn_type
*vec_type
= gather_offsets
->type
->array_element
;
2404 vtn_fail_if(vec_type
->base_type
!= vtn_base_type_vector
||
2405 vec_type
->length
!= 2 ||
2406 !glsl_type_is_integer(vec_type
->type
),
2407 "ConstOffsets must be an array of size four of vectors "
2408 "of two integer components");
2410 unsigned bit_size
= glsl_get_bit_size(vec_type
->type
);
2411 for (uint32_t i
= 0; i
< 4; i
++) {
2412 const nir_const_value
*cvec
=
2413 &gather_offsets
->constant
->elements
[i
]->values
[0];
2414 for (uint32_t j
= 0; j
< 2; j
++) {
2416 case 8: instr
->tg4_offsets
[i
][j
] = cvec
->i8
[j
]; break;
2417 case 16: instr
->tg4_offsets
[i
][j
] = cvec
->i16
[j
]; break;
2418 case 32: instr
->tg4_offsets
[i
][j
] = cvec
->i32
[j
]; break;
2419 case 64: instr
->tg4_offsets
[i
][j
] = cvec
->i64
[j
]; break;
2421 vtn_fail("Unsupported bit size");
2427 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
2428 val
->ssa
->def
= &instr
->dest
.ssa
;
2430 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2434 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2435 const uint32_t *w
, nir_src
*src
)
2438 case SpvOpAtomicIIncrement
:
2439 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
2442 case SpvOpAtomicIDecrement
:
2443 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
2446 case SpvOpAtomicISub
:
2448 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
2451 case SpvOpAtomicCompareExchange
:
2452 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
2453 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
2456 case SpvOpAtomicExchange
:
2457 case SpvOpAtomicIAdd
:
2458 case SpvOpAtomicSMin
:
2459 case SpvOpAtomicUMin
:
2460 case SpvOpAtomicSMax
:
2461 case SpvOpAtomicUMax
:
2462 case SpvOpAtomicAnd
:
2464 case SpvOpAtomicXor
:
2465 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
2469 vtn_fail("Invalid SPIR-V atomic");
2473 static nir_ssa_def
*
2474 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2476 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
2478 /* The image_load_store intrinsics assume a 4-dim coordinate */
2479 unsigned dim
= glsl_get_vector_elements(coord
->type
);
2480 unsigned swizzle
[4];
2481 for (unsigned i
= 0; i
< 4; i
++)
2482 swizzle
[i
] = MIN2(i
, dim
- 1);
2484 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
2487 static nir_ssa_def
*
2488 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2490 if (value
->num_components
== 4)
2494 for (unsigned i
= 0; i
< 4; i
++)
2495 swiz
[i
] = i
< value
->num_components
? i
: 0;
2496 return nir_swizzle(b
, value
, swiz
, 4, false);
2500 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2501 const uint32_t *w
, unsigned count
)
2503 /* Just get this one out of the way */
2504 if (opcode
== SpvOpImageTexelPointer
) {
2505 struct vtn_value
*val
=
2506 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2507 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2509 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2510 val
->image
->coord
= get_image_coord(b
, w
[4]);
2511 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
2515 struct vtn_image_pointer image
;
2518 case SpvOpAtomicExchange
:
2519 case SpvOpAtomicCompareExchange
:
2520 case SpvOpAtomicCompareExchangeWeak
:
2521 case SpvOpAtomicIIncrement
:
2522 case SpvOpAtomicIDecrement
:
2523 case SpvOpAtomicIAdd
:
2524 case SpvOpAtomicISub
:
2525 case SpvOpAtomicLoad
:
2526 case SpvOpAtomicSMin
:
2527 case SpvOpAtomicUMin
:
2528 case SpvOpAtomicSMax
:
2529 case SpvOpAtomicUMax
:
2530 case SpvOpAtomicAnd
:
2532 case SpvOpAtomicXor
:
2533 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
2536 case SpvOpAtomicStore
:
2537 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
2540 case SpvOpImageQuerySize
:
2541 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2543 image
.sample
= NULL
;
2546 case SpvOpImageRead
:
2547 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2548 image
.coord
= get_image_coord(b
, w
[4]);
2550 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
2551 vtn_assert(w
[5] == SpvImageOperandsSampleMask
);
2552 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
2554 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2558 case SpvOpImageWrite
:
2559 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2560 image
.coord
= get_image_coord(b
, w
[2]);
2564 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
2565 vtn_assert(w
[4] == SpvImageOperandsSampleMask
);
2566 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
2568 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2573 vtn_fail("Invalid image opcode");
2576 nir_intrinsic_op op
;
2578 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
2579 OP(ImageQuerySize
, size
)
2581 OP(ImageWrite
, store
)
2582 OP(AtomicLoad
, load
)
2583 OP(AtomicStore
, store
)
2584 OP(AtomicExchange
, atomic_exchange
)
2585 OP(AtomicCompareExchange
, atomic_comp_swap
)
2586 OP(AtomicIIncrement
, atomic_add
)
2587 OP(AtomicIDecrement
, atomic_add
)
2588 OP(AtomicIAdd
, atomic_add
)
2589 OP(AtomicISub
, atomic_add
)
2590 OP(AtomicSMin
, atomic_min
)
2591 OP(AtomicUMin
, atomic_min
)
2592 OP(AtomicSMax
, atomic_max
)
2593 OP(AtomicUMax
, atomic_max
)
2594 OP(AtomicAnd
, atomic_and
)
2595 OP(AtomicOr
, atomic_or
)
2596 OP(AtomicXor
, atomic_xor
)
2599 vtn_fail("Invalid image opcode");
2602 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
2604 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
2605 intrin
->src
[0] = nir_src_for_ssa(&image_deref
->dest
.ssa
);
2607 /* ImageQuerySize doesn't take any extra parameters */
2608 if (opcode
!= SpvOpImageQuerySize
) {
2609 /* The image coordinate is always 4 components but we may not have that
2610 * many. Swizzle to compensate.
2612 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
2613 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
2617 case SpvOpAtomicLoad
:
2618 case SpvOpImageQuerySize
:
2619 case SpvOpImageRead
:
2621 case SpvOpAtomicStore
:
2622 case SpvOpImageWrite
: {
2623 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
2624 nir_ssa_def
*value
= vtn_ssa_value(b
, value_id
)->def
;
2625 /* nir_intrinsic_image_deref_store always takes a vec4 value */
2626 assert(op
== nir_intrinsic_image_deref_store
);
2627 intrin
->num_components
= 4;
2628 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
));
2632 case SpvOpAtomicCompareExchange
:
2633 case SpvOpAtomicIIncrement
:
2634 case SpvOpAtomicIDecrement
:
2635 case SpvOpAtomicExchange
:
2636 case SpvOpAtomicIAdd
:
2637 case SpvOpAtomicISub
:
2638 case SpvOpAtomicSMin
:
2639 case SpvOpAtomicUMin
:
2640 case SpvOpAtomicSMax
:
2641 case SpvOpAtomicUMax
:
2642 case SpvOpAtomicAnd
:
2644 case SpvOpAtomicXor
:
2645 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
2649 vtn_fail("Invalid image opcode");
2652 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
2653 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2654 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2656 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
2657 intrin
->num_components
= nir_intrinsic_infos
[op
].dest_components
;
2658 if (intrin
->num_components
== 0)
2659 intrin
->num_components
= dest_components
;
2661 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2662 intrin
->num_components
, 32, NULL
);
2664 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2666 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
2667 if (intrin
->num_components
!= dest_components
)
2668 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
2670 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
2671 val
->ssa
->def
= result
;
2673 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2677 static nir_intrinsic_op
2678 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2681 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2682 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2683 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2684 OP(AtomicExchange
, atomic_exchange
)
2685 OP(AtomicCompareExchange
, atomic_comp_swap
)
2686 OP(AtomicIIncrement
, atomic_add
)
2687 OP(AtomicIDecrement
, atomic_add
)
2688 OP(AtomicIAdd
, atomic_add
)
2689 OP(AtomicISub
, atomic_add
)
2690 OP(AtomicSMin
, atomic_imin
)
2691 OP(AtomicUMin
, atomic_umin
)
2692 OP(AtomicSMax
, atomic_imax
)
2693 OP(AtomicUMax
, atomic_umax
)
2694 OP(AtomicAnd
, atomic_and
)
2695 OP(AtomicOr
, atomic_or
)
2696 OP(AtomicXor
, atomic_xor
)
2699 vtn_fail("Invalid SSBO atomic");
2703 static nir_intrinsic_op
2704 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2707 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
2708 OP(AtomicLoad
, read_deref
)
2709 OP(AtomicExchange
, exchange
)
2710 OP(AtomicCompareExchange
, comp_swap
)
2711 OP(AtomicIIncrement
, inc_deref
)
2712 OP(AtomicIDecrement
, post_dec_deref
)
2713 OP(AtomicIAdd
, add_deref
)
2714 OP(AtomicISub
, add_deref
)
2715 OP(AtomicUMin
, min_deref
)
2716 OP(AtomicUMax
, max_deref
)
2717 OP(AtomicAnd
, and_deref
)
2718 OP(AtomicOr
, or_deref
)
2719 OP(AtomicXor
, xor_deref
)
2722 /* We left the following out: AtomicStore, AtomicSMin and
2723 * AtomicSmax. Right now there are not nir intrinsics for them. At this
2724 * moment Atomic Counter support is needed for ARB_spirv support, so is
2725 * only need to support GLSL Atomic Counters that are uints and don't
2726 * allow direct storage.
2728 unreachable("Invalid uniform atomic");
2732 static nir_intrinsic_op
2733 get_shared_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2736 case SpvOpAtomicLoad
: return nir_intrinsic_load_shared
;
2737 case SpvOpAtomicStore
: return nir_intrinsic_store_shared
;
2738 #define OP(S, N) case SpvOp##S: return nir_intrinsic_shared_##N;
2739 OP(AtomicExchange
, atomic_exchange
)
2740 OP(AtomicCompareExchange
, atomic_comp_swap
)
2741 OP(AtomicIIncrement
, atomic_add
)
2742 OP(AtomicIDecrement
, atomic_add
)
2743 OP(AtomicIAdd
, atomic_add
)
2744 OP(AtomicISub
, atomic_add
)
2745 OP(AtomicSMin
, atomic_imin
)
2746 OP(AtomicUMin
, atomic_umin
)
2747 OP(AtomicSMax
, atomic_imax
)
2748 OP(AtomicUMax
, atomic_umax
)
2749 OP(AtomicAnd
, atomic_and
)
2750 OP(AtomicOr
, atomic_or
)
2751 OP(AtomicXor
, atomic_xor
)
2754 vtn_fail("Invalid shared atomic");
2758 static nir_intrinsic_op
2759 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2762 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
2763 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
2764 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
2765 OP(AtomicExchange
, atomic_exchange
)
2766 OP(AtomicCompareExchange
, atomic_comp_swap
)
2767 OP(AtomicIIncrement
, atomic_add
)
2768 OP(AtomicIDecrement
, atomic_add
)
2769 OP(AtomicIAdd
, atomic_add
)
2770 OP(AtomicISub
, atomic_add
)
2771 OP(AtomicSMin
, atomic_imin
)
2772 OP(AtomicUMin
, atomic_umin
)
2773 OP(AtomicSMax
, atomic_imax
)
2774 OP(AtomicUMax
, atomic_umax
)
2775 OP(AtomicAnd
, atomic_and
)
2776 OP(AtomicOr
, atomic_or
)
2777 OP(AtomicXor
, atomic_xor
)
2780 vtn_fail("Invalid shared atomic");
2785 * Handles shared atomics, ssbo atomics and atomic counters.
2788 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
2789 const uint32_t *w
, unsigned count
)
2791 struct vtn_pointer
*ptr
;
2792 nir_intrinsic_instr
*atomic
;
2795 case SpvOpAtomicLoad
:
2796 case SpvOpAtomicExchange
:
2797 case SpvOpAtomicCompareExchange
:
2798 case SpvOpAtomicCompareExchangeWeak
:
2799 case SpvOpAtomicIIncrement
:
2800 case SpvOpAtomicIDecrement
:
2801 case SpvOpAtomicIAdd
:
2802 case SpvOpAtomicISub
:
2803 case SpvOpAtomicSMin
:
2804 case SpvOpAtomicUMin
:
2805 case SpvOpAtomicSMax
:
2806 case SpvOpAtomicUMax
:
2807 case SpvOpAtomicAnd
:
2809 case SpvOpAtomicXor
:
2810 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2813 case SpvOpAtomicStore
:
2814 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2818 vtn_fail("Invalid SPIR-V atomic");
2822 SpvScope scope = w[4];
2823 SpvMemorySemanticsMask semantics = w[5];
2826 /* uniform as "atomic counter uniform" */
2827 if (ptr
->mode
== vtn_variable_mode_uniform
) {
2828 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2829 const struct glsl_type
*deref_type
= deref
->type
;
2830 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
2831 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2832 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2834 /* SSBO needs to initialize index/offset. In this case we don't need to,
2835 * as that info is already stored on the ptr->var->var nir_variable (see
2836 * vtn_create_variable)
2840 case SpvOpAtomicLoad
:
2841 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2844 case SpvOpAtomicStore
:
2845 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2846 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2849 case SpvOpAtomicExchange
:
2850 case SpvOpAtomicCompareExchange
:
2851 case SpvOpAtomicCompareExchangeWeak
:
2852 case SpvOpAtomicIIncrement
:
2853 case SpvOpAtomicIDecrement
:
2854 case SpvOpAtomicIAdd
:
2855 case SpvOpAtomicISub
:
2856 case SpvOpAtomicSMin
:
2857 case SpvOpAtomicUMin
:
2858 case SpvOpAtomicSMax
:
2859 case SpvOpAtomicUMax
:
2860 case SpvOpAtomicAnd
:
2862 case SpvOpAtomicXor
:
2863 /* Nothing: we don't need to call fill_common_atomic_sources here, as
2864 * atomic counter uniforms doesn't have sources
2869 unreachable("Invalid SPIR-V atomic");
2872 } else if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
2873 nir_ssa_def
*offset
, *index
;
2874 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
2876 nir_intrinsic_op op
;
2877 if (ptr
->mode
== vtn_variable_mode_ssbo
) {
2878 op
= get_ssbo_nir_atomic_op(b
, opcode
);
2880 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
&&
2881 b
->options
->lower_workgroup_access_to_offsets
);
2882 op
= get_shared_nir_atomic_op(b
, opcode
);
2885 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2889 case SpvOpAtomicLoad
:
2890 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2891 nir_intrinsic_set_align(atomic
, 4, 0);
2892 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2893 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2894 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2897 case SpvOpAtomicStore
:
2898 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2899 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2900 nir_intrinsic_set_align(atomic
, 4, 0);
2901 atomic
->src
[src
++] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2902 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2903 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2904 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2907 case SpvOpAtomicExchange
:
2908 case SpvOpAtomicCompareExchange
:
2909 case SpvOpAtomicCompareExchangeWeak
:
2910 case SpvOpAtomicIIncrement
:
2911 case SpvOpAtomicIDecrement
:
2912 case SpvOpAtomicIAdd
:
2913 case SpvOpAtomicISub
:
2914 case SpvOpAtomicSMin
:
2915 case SpvOpAtomicUMin
:
2916 case SpvOpAtomicSMax
:
2917 case SpvOpAtomicUMax
:
2918 case SpvOpAtomicAnd
:
2920 case SpvOpAtomicXor
:
2921 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2922 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2923 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2924 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
2928 vtn_fail("Invalid SPIR-V atomic");
2931 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2932 const struct glsl_type
*deref_type
= deref
->type
;
2933 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
2934 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2935 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2938 case SpvOpAtomicLoad
:
2939 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2942 case SpvOpAtomicStore
:
2943 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2944 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2945 atomic
->src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2948 case SpvOpAtomicExchange
:
2949 case SpvOpAtomicCompareExchange
:
2950 case SpvOpAtomicCompareExchangeWeak
:
2951 case SpvOpAtomicIIncrement
:
2952 case SpvOpAtomicIDecrement
:
2953 case SpvOpAtomicIAdd
:
2954 case SpvOpAtomicISub
:
2955 case SpvOpAtomicSMin
:
2956 case SpvOpAtomicUMin
:
2957 case SpvOpAtomicSMax
:
2958 case SpvOpAtomicUMax
:
2959 case SpvOpAtomicAnd
:
2961 case SpvOpAtomicXor
:
2962 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
2966 vtn_fail("Invalid SPIR-V atomic");
2970 if (opcode
!= SpvOpAtomicStore
) {
2971 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2973 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
2974 glsl_get_vector_elements(type
->type
),
2975 glsl_get_bit_size(type
->type
), NULL
);
2977 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2978 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
2979 val
->ssa
->def
= &atomic
->dest
.ssa
;
2980 val
->ssa
->type
= type
->type
;
2983 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
2986 static nir_alu_instr
*
2987 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
2990 switch (num_components
) {
2991 case 1: op
= nir_op_imov
; break;
2992 case 2: op
= nir_op_vec2
; break;
2993 case 3: op
= nir_op_vec3
; break;
2994 case 4: op
= nir_op_vec4
; break;
2995 default: vtn_fail("bad vector size");
2998 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
2999 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
3001 vec
->dest
.write_mask
= (1 << num_components
) - 1;
3006 struct vtn_ssa_value
*
3007 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
3009 if (src
->transposed
)
3010 return src
->transposed
;
3012 struct vtn_ssa_value
*dest
=
3013 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
3015 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
3016 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
3017 glsl_get_bit_size(src
->type
));
3018 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3019 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
3020 vec
->src
[0].swizzle
[0] = i
;
3022 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
3023 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
3024 vec
->src
[j
].swizzle
[0] = i
;
3027 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3028 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
3031 dest
->transposed
= src
;
3037 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
3039 return nir_channel(&b
->nb
, src
, index
);
3043 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
3046 nir_alu_instr
*vec
= create_vec(b
, src
->num_components
,
3049 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
3051 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
3053 vec
->src
[i
].src
= nir_src_for_ssa(src
);
3054 vec
->src
[i
].swizzle
[0] = i
;
3058 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3060 return &vec
->dest
.dest
.ssa
;
3063 static nir_ssa_def
*
3064 nir_ieq_imm(nir_builder
*b
, nir_ssa_def
*x
, uint64_t i
)
3066 return nir_ieq(b
, x
, nir_imm_intN_t(b
, i
, x
->bit_size
));
3070 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
3073 return nir_vector_extract(&b
->nb
, src
, nir_i2i(&b
->nb
, index
, 32));
3077 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
3078 nir_ssa_def
*insert
, nir_ssa_def
*index
)
3080 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
3081 for (unsigned i
= 1; i
< src
->num_components
; i
++)
3082 dest
= nir_bcsel(&b
->nb
, nir_ieq_imm(&b
->nb
, index
, i
),
3083 vtn_vector_insert(b
, src
, insert
, i
), dest
);
3088 static nir_ssa_def
*
3089 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
3090 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
3091 const uint32_t *indices
)
3093 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
3095 for (unsigned i
= 0; i
< num_components
; i
++) {
3096 uint32_t index
= indices
[i
];
3097 if (index
== 0xffffffff) {
3099 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
3100 } else if (index
< src0
->num_components
) {
3101 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
3102 vec
->src
[i
].swizzle
[0] = index
;
3104 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
3105 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
3109 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3111 return &vec
->dest
.dest
.ssa
;
3115 * Concatentates a number of vectors/scalars together to produce a vector
3117 static nir_ssa_def
*
3118 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
3119 unsigned num_srcs
, nir_ssa_def
**srcs
)
3121 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
3123 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3125 * "When constructing a vector, there must be at least two Constituent
3128 vtn_assert(num_srcs
>= 2);
3130 unsigned dest_idx
= 0;
3131 for (unsigned i
= 0; i
< num_srcs
; i
++) {
3132 nir_ssa_def
*src
= srcs
[i
];
3133 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
3134 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
3135 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
3136 vec
->src
[dest_idx
].swizzle
[0] = j
;
3141 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3143 * "When constructing a vector, the total number of components in all
3144 * the operands must equal the number of components in Result Type."
3146 vtn_assert(dest_idx
== num_components
);
3148 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3150 return &vec
->dest
.dest
.ssa
;
3153 static struct vtn_ssa_value
*
3154 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3156 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3157 dest
->type
= src
->type
;
3159 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3160 dest
->def
= src
->def
;
3162 unsigned elems
= glsl_get_length(src
->type
);
3164 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3165 for (unsigned i
= 0; i
< elems
; i
++)
3166 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3172 static struct vtn_ssa_value
*
3173 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3174 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3175 unsigned num_indices
)
3177 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3179 struct vtn_ssa_value
*cur
= dest
;
3181 for (i
= 0; i
< num_indices
- 1; i
++) {
3182 cur
= cur
->elems
[indices
[i
]];
3185 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3186 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3187 * the component granularity. In that case, the last index will be
3188 * the index to insert the scalar into the vector.
3191 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
3193 cur
->elems
[indices
[i
]] = insert
;
3199 static struct vtn_ssa_value
*
3200 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3201 const uint32_t *indices
, unsigned num_indices
)
3203 struct vtn_ssa_value
*cur
= src
;
3204 for (unsigned i
= 0; i
< num_indices
; i
++) {
3205 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3206 vtn_assert(i
== num_indices
- 1);
3207 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3208 * the component granularity. The last index will be the index of the
3209 * vector to extract.
3212 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
3213 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
3214 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
3217 cur
= cur
->elems
[indices
[i
]];
3225 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3226 const uint32_t *w
, unsigned count
)
3228 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
3229 const struct glsl_type
*type
=
3230 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
3231 val
->ssa
= vtn_create_ssa_value(b
, type
);
3234 case SpvOpVectorExtractDynamic
:
3235 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3236 vtn_ssa_value(b
, w
[4])->def
);
3239 case SpvOpVectorInsertDynamic
:
3240 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3241 vtn_ssa_value(b
, w
[4])->def
,
3242 vtn_ssa_value(b
, w
[5])->def
);
3245 case SpvOpVectorShuffle
:
3246 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
3247 vtn_ssa_value(b
, w
[3])->def
,
3248 vtn_ssa_value(b
, w
[4])->def
,
3252 case SpvOpCompositeConstruct
: {
3253 unsigned elems
= count
- 3;
3255 if (glsl_type_is_vector_or_scalar(type
)) {
3256 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3257 for (unsigned i
= 0; i
< elems
; i
++)
3258 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
3260 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
3263 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3264 for (unsigned i
= 0; i
< elems
; i
++)
3265 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3269 case SpvOpCompositeExtract
:
3270 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3274 case SpvOpCompositeInsert
:
3275 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3276 vtn_ssa_value(b
, w
[3]),
3280 case SpvOpCopyObject
:
3281 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3285 vtn_fail("unknown composite operation");
3290 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3292 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3293 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3297 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3298 SpvMemorySemanticsMask semantics
)
3300 static const SpvMemorySemanticsMask all_memory_semantics
=
3301 SpvMemorySemanticsUniformMemoryMask
|
3302 SpvMemorySemanticsWorkgroupMemoryMask
|
3303 SpvMemorySemanticsAtomicCounterMemoryMask
|
3304 SpvMemorySemanticsImageMemoryMask
;
3306 /* If we're not actually doing a memory barrier, bail */
3307 if (!(semantics
& all_memory_semantics
))
3310 /* GL and Vulkan don't have these */
3311 vtn_assert(scope
!= SpvScopeCrossDevice
);
3313 if (scope
== SpvScopeSubgroup
)
3314 return; /* Nothing to do here */
3316 if (scope
== SpvScopeWorkgroup
) {
3317 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3321 /* There's only two scopes thing left */
3322 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3324 if ((semantics
& all_memory_semantics
) == all_memory_semantics
) {
3325 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3329 /* Issue a bunch of more specific barriers */
3330 uint32_t bits
= semantics
;
3332 SpvMemorySemanticsMask semantic
= 1 << u_bit_scan(&bits
);
3334 case SpvMemorySemanticsUniformMemoryMask
:
3335 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3337 case SpvMemorySemanticsWorkgroupMemoryMask
:
3338 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3340 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3341 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3343 case SpvMemorySemanticsImageMemoryMask
:
3344 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3353 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3354 const uint32_t *w
, unsigned count
)
3357 case SpvOpEmitVertex
:
3358 case SpvOpEmitStreamVertex
:
3359 case SpvOpEndPrimitive
:
3360 case SpvOpEndStreamPrimitive
: {
3361 nir_intrinsic_op intrinsic_op
;
3363 case SpvOpEmitVertex
:
3364 case SpvOpEmitStreamVertex
:
3365 intrinsic_op
= nir_intrinsic_emit_vertex
;
3367 case SpvOpEndPrimitive
:
3368 case SpvOpEndStreamPrimitive
:
3369 intrinsic_op
= nir_intrinsic_end_primitive
;
3372 unreachable("Invalid opcode");
3375 nir_intrinsic_instr
*intrin
=
3376 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3379 case SpvOpEmitStreamVertex
:
3380 case SpvOpEndStreamPrimitive
: {
3381 unsigned stream
= vtn_constant_uint(b
, w
[1]);
3382 nir_intrinsic_set_stream_id(intrin
, stream
);
3390 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3394 case SpvOpMemoryBarrier
: {
3395 SpvScope scope
= vtn_constant_uint(b
, w
[1]);
3396 SpvMemorySemanticsMask semantics
= vtn_constant_uint(b
, w
[2]);
3397 vtn_emit_memory_barrier(b
, scope
, semantics
);
3401 case SpvOpControlBarrier
: {
3402 SpvScope execution_scope
= vtn_constant_uint(b
, w
[1]);
3403 if (execution_scope
== SpvScopeWorkgroup
)
3404 vtn_emit_barrier(b
, nir_intrinsic_barrier
);
3406 SpvScope memory_scope
= vtn_constant_uint(b
, w
[2]);
3407 SpvMemorySemanticsMask memory_semantics
= vtn_constant_uint(b
, w
[3]);
3408 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
3413 unreachable("unknown barrier instruction");
3418 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
3419 SpvExecutionMode mode
)
3422 case SpvExecutionModeInputPoints
:
3423 case SpvExecutionModeOutputPoints
:
3424 return 0; /* GL_POINTS */
3425 case SpvExecutionModeInputLines
:
3426 return 1; /* GL_LINES */
3427 case SpvExecutionModeInputLinesAdjacency
:
3428 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3429 case SpvExecutionModeTriangles
:
3430 return 4; /* GL_TRIANGLES */
3431 case SpvExecutionModeInputTrianglesAdjacency
:
3432 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3433 case SpvExecutionModeQuads
:
3434 return 7; /* GL_QUADS */
3435 case SpvExecutionModeIsolines
:
3436 return 0x8E7A; /* GL_ISOLINES */
3437 case SpvExecutionModeOutputLineStrip
:
3438 return 3; /* GL_LINE_STRIP */
3439 case SpvExecutionModeOutputTriangleStrip
:
3440 return 5; /* GL_TRIANGLE_STRIP */
3442 vtn_fail("Invalid primitive type");
3447 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
3448 SpvExecutionMode mode
)
3451 case SpvExecutionModeInputPoints
:
3453 case SpvExecutionModeInputLines
:
3455 case SpvExecutionModeInputLinesAdjacency
:
3457 case SpvExecutionModeTriangles
:
3459 case SpvExecutionModeInputTrianglesAdjacency
:
3462 vtn_fail("Invalid GS input mode");
3466 static gl_shader_stage
3467 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
3470 case SpvExecutionModelVertex
:
3471 return MESA_SHADER_VERTEX
;
3472 case SpvExecutionModelTessellationControl
:
3473 return MESA_SHADER_TESS_CTRL
;
3474 case SpvExecutionModelTessellationEvaluation
:
3475 return MESA_SHADER_TESS_EVAL
;
3476 case SpvExecutionModelGeometry
:
3477 return MESA_SHADER_GEOMETRY
;
3478 case SpvExecutionModelFragment
:
3479 return MESA_SHADER_FRAGMENT
;
3480 case SpvExecutionModelGLCompute
:
3481 return MESA_SHADER_COMPUTE
;
3482 case SpvExecutionModelKernel
:
3483 return MESA_SHADER_KERNEL
;
3485 vtn_fail("Unsupported execution model");
3489 #define spv_check_supported(name, cap) do { \
3490 if (!(b->options && b->options->caps.name)) \
3491 vtn_warn("Unsupported SPIR-V capability: %s", \
3492 spirv_capability_to_string(cap)); \
3497 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
3500 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
3501 /* Let this be a name label regardless */
3502 unsigned name_words
;
3503 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
3505 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
3506 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
3509 vtn_assert(b
->entry_point
== NULL
);
3510 b
->entry_point
= entry_point
;
3514 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3515 const uint32_t *w
, unsigned count
)
3522 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
3523 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
3524 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
3525 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
3526 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
3527 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
3530 uint32_t version
= w
[2];
3533 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
3535 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
3539 case SpvOpSourceExtension
:
3540 case SpvOpSourceContinued
:
3541 case SpvOpExtension
:
3542 case SpvOpModuleProcessed
:
3543 /* Unhandled, but these are for debug so that's ok. */
3546 case SpvOpCapability
: {
3547 SpvCapability cap
= w
[1];
3549 case SpvCapabilityMatrix
:
3550 case SpvCapabilityShader
:
3551 case SpvCapabilityGeometry
:
3552 case SpvCapabilityGeometryPointSize
:
3553 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
3554 case SpvCapabilitySampledImageArrayDynamicIndexing
:
3555 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
3556 case SpvCapabilityStorageImageArrayDynamicIndexing
:
3557 case SpvCapabilityImageRect
:
3558 case SpvCapabilitySampledRect
:
3559 case SpvCapabilitySampled1D
:
3560 case SpvCapabilityImage1D
:
3561 case SpvCapabilitySampledCubeArray
:
3562 case SpvCapabilityImageCubeArray
:
3563 case SpvCapabilitySampledBuffer
:
3564 case SpvCapabilityImageBuffer
:
3565 case SpvCapabilityImageQuery
:
3566 case SpvCapabilityDerivativeControl
:
3567 case SpvCapabilityInterpolationFunction
:
3568 case SpvCapabilityMultiViewport
:
3569 case SpvCapabilitySampleRateShading
:
3570 case SpvCapabilityClipDistance
:
3571 case SpvCapabilityCullDistance
:
3572 case SpvCapabilityInputAttachment
:
3573 case SpvCapabilityImageGatherExtended
:
3574 case SpvCapabilityStorageImageExtendedFormats
:
3577 case SpvCapabilityLinkage
:
3578 case SpvCapabilityVector16
:
3579 case SpvCapabilityFloat16Buffer
:
3580 case SpvCapabilityFloat16
:
3581 case SpvCapabilitySparseResidency
:
3582 vtn_warn("Unsupported SPIR-V capability: %s",
3583 spirv_capability_to_string(cap
));
3586 case SpvCapabilityMinLod
:
3587 spv_check_supported(min_lod
, cap
);
3590 case SpvCapabilityAtomicStorage
:
3591 spv_check_supported(atomic_storage
, cap
);
3594 case SpvCapabilityFloat64
:
3595 spv_check_supported(float64
, cap
);
3597 case SpvCapabilityInt64
:
3598 spv_check_supported(int64
, cap
);
3600 case SpvCapabilityInt16
:
3601 spv_check_supported(int16
, cap
);
3604 case SpvCapabilityTransformFeedback
:
3605 spv_check_supported(transform_feedback
, cap
);
3608 case SpvCapabilityGeometryStreams
:
3609 spv_check_supported(geometry_streams
, cap
);
3612 case SpvCapabilityInt64Atomics
:
3613 spv_check_supported(int64_atomics
, cap
);
3616 case SpvCapabilityInt8
:
3617 spv_check_supported(int8
, cap
);
3620 case SpvCapabilityStorageImageMultisample
:
3621 spv_check_supported(storage_image_ms
, cap
);
3624 case SpvCapabilityAddresses
:
3625 spv_check_supported(address
, cap
);
3628 case SpvCapabilityKernel
:
3629 spv_check_supported(kernel
, cap
);
3632 case SpvCapabilityImageBasic
:
3633 case SpvCapabilityImageReadWrite
:
3634 case SpvCapabilityImageMipmap
:
3635 case SpvCapabilityPipes
:
3636 case SpvCapabilityGroups
:
3637 case SpvCapabilityDeviceEnqueue
:
3638 case SpvCapabilityLiteralSampler
:
3639 case SpvCapabilityGenericPointer
:
3640 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
3641 spirv_capability_to_string(cap
));
3644 case SpvCapabilityImageMSArray
:
3645 spv_check_supported(image_ms_array
, cap
);
3648 case SpvCapabilityTessellation
:
3649 case SpvCapabilityTessellationPointSize
:
3650 spv_check_supported(tessellation
, cap
);
3653 case SpvCapabilityDrawParameters
:
3654 spv_check_supported(draw_parameters
, cap
);
3657 case SpvCapabilityStorageImageReadWithoutFormat
:
3658 spv_check_supported(image_read_without_format
, cap
);
3661 case SpvCapabilityStorageImageWriteWithoutFormat
:
3662 spv_check_supported(image_write_without_format
, cap
);
3665 case SpvCapabilityDeviceGroup
:
3666 spv_check_supported(device_group
, cap
);
3669 case SpvCapabilityMultiView
:
3670 spv_check_supported(multiview
, cap
);
3673 case SpvCapabilityGroupNonUniform
:
3674 spv_check_supported(subgroup_basic
, cap
);
3677 case SpvCapabilityGroupNonUniformVote
:
3678 spv_check_supported(subgroup_vote
, cap
);
3681 case SpvCapabilitySubgroupBallotKHR
:
3682 case SpvCapabilityGroupNonUniformBallot
:
3683 spv_check_supported(subgroup_ballot
, cap
);
3686 case SpvCapabilityGroupNonUniformShuffle
:
3687 case SpvCapabilityGroupNonUniformShuffleRelative
:
3688 spv_check_supported(subgroup_shuffle
, cap
);
3691 case SpvCapabilityGroupNonUniformQuad
:
3692 spv_check_supported(subgroup_quad
, cap
);
3695 case SpvCapabilityGroupNonUniformArithmetic
:
3696 case SpvCapabilityGroupNonUniformClustered
:
3697 spv_check_supported(subgroup_arithmetic
, cap
);
3700 case SpvCapabilityVariablePointersStorageBuffer
:
3701 case SpvCapabilityVariablePointers
:
3702 spv_check_supported(variable_pointers
, cap
);
3703 b
->variable_pointers
= true;
3706 case SpvCapabilityStorageUniformBufferBlock16
:
3707 case SpvCapabilityStorageUniform16
:
3708 case SpvCapabilityStoragePushConstant16
:
3709 case SpvCapabilityStorageInputOutput16
:
3710 spv_check_supported(storage_16bit
, cap
);
3713 case SpvCapabilityShaderViewportIndexLayerEXT
:
3714 spv_check_supported(shader_viewport_index_layer
, cap
);
3717 case SpvCapabilityStorageBuffer8BitAccess
:
3718 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
3719 case SpvCapabilityStoragePushConstant8
:
3720 spv_check_supported(storage_8bit
, cap
);
3723 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
3724 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
3725 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
3726 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
3729 case SpvCapabilityRuntimeDescriptorArrayEXT
:
3730 spv_check_supported(runtime_descriptor_array
, cap
);
3733 case SpvCapabilityStencilExportEXT
:
3734 spv_check_supported(stencil_export
, cap
);
3737 case SpvCapabilitySampleMaskPostDepthCoverage
:
3738 spv_check_supported(post_depth_coverage
, cap
);
3741 case SpvCapabilityPhysicalStorageBufferAddressesEXT
:
3742 spv_check_supported(physical_storage_buffer_address
, cap
);
3746 vtn_fail("Unhandled capability");
3751 case SpvOpExtInstImport
:
3752 vtn_handle_extension(b
, opcode
, w
, count
);
3755 case SpvOpMemoryModel
:
3757 case SpvAddressingModelPhysical32
:
3758 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
3759 "AddressingModelPhysical32 only supported for kernels");
3760 b
->shader
->info
.cs
.ptr_size
= 32;
3761 b
->physical_ptrs
= true;
3762 b
->options
->shared_ptr_type
= glsl_uint_type();
3763 b
->options
->global_ptr_type
= glsl_uint_type();
3764 b
->options
->temp_ptr_type
= glsl_uint_type();
3766 case SpvAddressingModelPhysical64
:
3767 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
3768 "AddressingModelPhysical64 only supported for kernels");
3769 b
->shader
->info
.cs
.ptr_size
= 64;
3770 b
->physical_ptrs
= true;
3771 b
->options
->shared_ptr_type
= glsl_uint64_t_type();
3772 b
->options
->global_ptr_type
= glsl_uint64_t_type();
3773 b
->options
->temp_ptr_type
= glsl_uint64_t_type();
3775 case SpvAddressingModelLogical
:
3776 vtn_fail_if(b
->shader
->info
.stage
>= MESA_SHADER_STAGES
,
3777 "AddressingModelLogical only supported for shaders");
3778 b
->shader
->info
.cs
.ptr_size
= 0;
3779 b
->physical_ptrs
= false;
3781 case SpvAddressingModelPhysicalStorageBuffer64EXT
:
3782 vtn_fail_if(!b
->options
||
3783 !b
->options
->caps
.physical_storage_buffer_address
,
3784 "AddressingModelPhysicalStorageBuffer64EXT not supported");
3787 vtn_fail("Unknown addressing model");
3791 vtn_assert(w
[2] == SpvMemoryModelSimple
||
3792 w
[2] == SpvMemoryModelGLSL450
||
3793 w
[2] == SpvMemoryModelOpenCL
);
3796 case SpvOpEntryPoint
:
3797 vtn_handle_entry_point(b
, w
, count
);
3801 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
3802 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3806 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3809 case SpvOpMemberName
:
3813 case SpvOpExecutionMode
:
3814 case SpvOpExecutionModeId
:
3815 case SpvOpDecorationGroup
:
3817 case SpvOpMemberDecorate
:
3818 case SpvOpGroupDecorate
:
3819 case SpvOpGroupMemberDecorate
:
3820 case SpvOpDecorateStringGOOGLE
:
3821 case SpvOpMemberDecorateStringGOOGLE
:
3822 vtn_handle_decoration(b
, opcode
, w
, count
);
3826 return false; /* End of preamble */
3833 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
3834 const struct vtn_decoration
*mode
, void *data
)
3836 vtn_assert(b
->entry_point
== entry_point
);
3838 switch(mode
->exec_mode
) {
3839 case SpvExecutionModeOriginUpperLeft
:
3840 case SpvExecutionModeOriginLowerLeft
:
3841 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3842 b
->shader
->info
.fs
.origin_upper_left
=
3843 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
3846 case SpvExecutionModeEarlyFragmentTests
:
3847 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3848 b
->shader
->info
.fs
.early_fragment_tests
= true;
3851 case SpvExecutionModePostDepthCoverage
:
3852 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3853 b
->shader
->info
.fs
.post_depth_coverage
= true;
3856 case SpvExecutionModeInvocations
:
3857 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3858 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->literals
[0]);
3861 case SpvExecutionModeDepthReplacing
:
3862 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3863 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
3865 case SpvExecutionModeDepthGreater
:
3866 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3867 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
3869 case SpvExecutionModeDepthLess
:
3870 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3871 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
3873 case SpvExecutionModeDepthUnchanged
:
3874 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3875 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
3878 case SpvExecutionModeLocalSize
:
3879 vtn_assert(gl_shader_stage_is_compute(b
->shader
->info
.stage
));
3880 b
->shader
->info
.cs
.local_size
[0] = mode
->literals
[0];
3881 b
->shader
->info
.cs
.local_size
[1] = mode
->literals
[1];
3882 b
->shader
->info
.cs
.local_size
[2] = mode
->literals
[2];
3885 case SpvExecutionModeLocalSizeId
:
3886 b
->shader
->info
.cs
.local_size
[0] = vtn_constant_uint(b
, mode
->literals
[0]);
3887 b
->shader
->info
.cs
.local_size
[1] = vtn_constant_uint(b
, mode
->literals
[1]);
3888 b
->shader
->info
.cs
.local_size
[2] = vtn_constant_uint(b
, mode
->literals
[2]);
3891 case SpvExecutionModeLocalSizeHint
:
3892 case SpvExecutionModeLocalSizeHintId
:
3893 break; /* Nothing to do with this */
3895 case SpvExecutionModeOutputVertices
:
3896 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3897 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3898 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->literals
[0];
3900 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3901 b
->shader
->info
.gs
.vertices_out
= mode
->literals
[0];
3905 case SpvExecutionModeInputPoints
:
3906 case SpvExecutionModeInputLines
:
3907 case SpvExecutionModeInputLinesAdjacency
:
3908 case SpvExecutionModeTriangles
:
3909 case SpvExecutionModeInputTrianglesAdjacency
:
3910 case SpvExecutionModeQuads
:
3911 case SpvExecutionModeIsolines
:
3912 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3913 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3914 b
->shader
->info
.tess
.primitive_mode
=
3915 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3917 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3918 b
->shader
->info
.gs
.vertices_in
=
3919 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
3920 b
->shader
->info
.gs
.input_primitive
=
3921 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3925 case SpvExecutionModeOutputPoints
:
3926 case SpvExecutionModeOutputLineStrip
:
3927 case SpvExecutionModeOutputTriangleStrip
:
3928 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3929 b
->shader
->info
.gs
.output_primitive
=
3930 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3933 case SpvExecutionModeSpacingEqual
:
3934 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3935 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3936 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
3938 case SpvExecutionModeSpacingFractionalEven
:
3939 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3940 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3941 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
3943 case SpvExecutionModeSpacingFractionalOdd
:
3944 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3945 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3946 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
3948 case SpvExecutionModeVertexOrderCw
:
3949 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3950 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3951 b
->shader
->info
.tess
.ccw
= false;
3953 case SpvExecutionModeVertexOrderCcw
:
3954 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3955 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3956 b
->shader
->info
.tess
.ccw
= true;
3958 case SpvExecutionModePointMode
:
3959 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3960 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3961 b
->shader
->info
.tess
.point_mode
= true;
3964 case SpvExecutionModePixelCenterInteger
:
3965 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3966 b
->shader
->info
.fs
.pixel_center_integer
= true;
3969 case SpvExecutionModeXfb
:
3970 b
->shader
->info
.has_transform_feedback_varyings
= true;
3973 case SpvExecutionModeVecTypeHint
:
3976 case SpvExecutionModeContractionOff
:
3977 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
3978 vtn_warn("ExectionMode only allowed for CL-style kernels: %s",
3979 spirv_executionmode_to_string(mode
->exec_mode
));
3984 case SpvExecutionModeStencilRefReplacingEXT
:
3985 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3989 vtn_fail("Unhandled execution mode");
3994 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3995 const uint32_t *w
, unsigned count
)
3997 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
4001 case SpvOpSourceContinued
:
4002 case SpvOpSourceExtension
:
4003 case SpvOpExtension
:
4004 case SpvOpCapability
:
4005 case SpvOpExtInstImport
:
4006 case SpvOpMemoryModel
:
4007 case SpvOpEntryPoint
:
4008 case SpvOpExecutionMode
:
4011 case SpvOpMemberName
:
4012 case SpvOpDecorationGroup
:
4014 case SpvOpMemberDecorate
:
4015 case SpvOpGroupDecorate
:
4016 case SpvOpGroupMemberDecorate
:
4017 case SpvOpDecorateStringGOOGLE
:
4018 case SpvOpMemberDecorateStringGOOGLE
:
4019 vtn_fail("Invalid opcode types and variables section");
4025 case SpvOpTypeFloat
:
4026 case SpvOpTypeVector
:
4027 case SpvOpTypeMatrix
:
4028 case SpvOpTypeImage
:
4029 case SpvOpTypeSampler
:
4030 case SpvOpTypeSampledImage
:
4031 case SpvOpTypeArray
:
4032 case SpvOpTypeRuntimeArray
:
4033 case SpvOpTypeStruct
:
4034 case SpvOpTypeOpaque
:
4035 case SpvOpTypePointer
:
4036 case SpvOpTypeForwardPointer
:
4037 case SpvOpTypeFunction
:
4038 case SpvOpTypeEvent
:
4039 case SpvOpTypeDeviceEvent
:
4040 case SpvOpTypeReserveId
:
4041 case SpvOpTypeQueue
:
4043 vtn_handle_type(b
, opcode
, w
, count
);
4046 case SpvOpConstantTrue
:
4047 case SpvOpConstantFalse
:
4049 case SpvOpConstantComposite
:
4050 case SpvOpConstantSampler
:
4051 case SpvOpConstantNull
:
4052 case SpvOpSpecConstantTrue
:
4053 case SpvOpSpecConstantFalse
:
4054 case SpvOpSpecConstant
:
4055 case SpvOpSpecConstantComposite
:
4056 case SpvOpSpecConstantOp
:
4057 vtn_handle_constant(b
, opcode
, w
, count
);
4062 vtn_handle_variables(b
, opcode
, w
, count
);
4066 return false; /* End of preamble */
4073 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4074 const uint32_t *w
, unsigned count
)
4080 case SpvOpLoopMerge
:
4081 case SpvOpSelectionMerge
:
4082 /* This is handled by cfg pre-pass and walk_blocks */
4086 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
4087 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4092 vtn_handle_extension(b
, opcode
, w
, count
);
4098 case SpvOpCopyMemory
:
4099 case SpvOpCopyMemorySized
:
4100 case SpvOpAccessChain
:
4101 case SpvOpPtrAccessChain
:
4102 case SpvOpInBoundsAccessChain
:
4103 case SpvOpInBoundsPtrAccessChain
:
4104 case SpvOpArrayLength
:
4105 case SpvOpConvertPtrToU
:
4106 case SpvOpConvertUToPtr
:
4107 vtn_handle_variables(b
, opcode
, w
, count
);
4110 case SpvOpFunctionCall
:
4111 vtn_handle_function_call(b
, opcode
, w
, count
);
4114 case SpvOpSampledImage
:
4116 case SpvOpImageSampleImplicitLod
:
4117 case SpvOpImageSampleExplicitLod
:
4118 case SpvOpImageSampleDrefImplicitLod
:
4119 case SpvOpImageSampleDrefExplicitLod
:
4120 case SpvOpImageSampleProjImplicitLod
:
4121 case SpvOpImageSampleProjExplicitLod
:
4122 case SpvOpImageSampleProjDrefImplicitLod
:
4123 case SpvOpImageSampleProjDrefExplicitLod
:
4124 case SpvOpImageFetch
:
4125 case SpvOpImageGather
:
4126 case SpvOpImageDrefGather
:
4127 case SpvOpImageQuerySizeLod
:
4128 case SpvOpImageQueryLod
:
4129 case SpvOpImageQueryLevels
:
4130 case SpvOpImageQuerySamples
:
4131 vtn_handle_texture(b
, opcode
, w
, count
);
4134 case SpvOpImageRead
:
4135 case SpvOpImageWrite
:
4136 case SpvOpImageTexelPointer
:
4137 vtn_handle_image(b
, opcode
, w
, count
);
4140 case SpvOpImageQuerySize
: {
4141 struct vtn_pointer
*image
=
4142 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
4143 if (glsl_type_is_image(image
->type
->type
)) {
4144 vtn_handle_image(b
, opcode
, w
, count
);
4146 vtn_assert(glsl_type_is_sampler(image
->type
->type
));
4147 vtn_handle_texture(b
, opcode
, w
, count
);
4152 case SpvOpAtomicLoad
:
4153 case SpvOpAtomicExchange
:
4154 case SpvOpAtomicCompareExchange
:
4155 case SpvOpAtomicCompareExchangeWeak
:
4156 case SpvOpAtomicIIncrement
:
4157 case SpvOpAtomicIDecrement
:
4158 case SpvOpAtomicIAdd
:
4159 case SpvOpAtomicISub
:
4160 case SpvOpAtomicSMin
:
4161 case SpvOpAtomicUMin
:
4162 case SpvOpAtomicSMax
:
4163 case SpvOpAtomicUMax
:
4164 case SpvOpAtomicAnd
:
4166 case SpvOpAtomicXor
: {
4167 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
4168 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4169 vtn_handle_image(b
, opcode
, w
, count
);
4171 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4172 vtn_handle_atomics(b
, opcode
, w
, count
);
4177 case SpvOpAtomicStore
: {
4178 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
4179 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4180 vtn_handle_image(b
, opcode
, w
, count
);
4182 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4183 vtn_handle_atomics(b
, opcode
, w
, count
);
4189 /* Handle OpSelect up-front here because it needs to be able to handle
4190 * pointers and not just regular vectors and scalars.
4192 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
4193 struct vtn_value
*sel_val
= vtn_untyped_value(b
, w
[3]);
4194 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
4195 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
4197 const struct glsl_type
*sel_type
;
4198 switch (res_val
->type
->base_type
) {
4199 case vtn_base_type_scalar
:
4200 sel_type
= glsl_bool_type();
4202 case vtn_base_type_vector
:
4203 sel_type
= glsl_vector_type(GLSL_TYPE_BOOL
, res_val
->type
->length
);
4205 case vtn_base_type_pointer
:
4206 /* We need to have actual storage for pointer types */
4207 vtn_fail_if(res_val
->type
->type
== NULL
,
4208 "Invalid pointer result type for OpSelect");
4209 sel_type
= glsl_bool_type();
4212 vtn_fail("Result type of OpSelect must be a scalar, vector, or pointer");
4215 if (unlikely(sel_val
->type
->type
!= sel_type
)) {
4216 if (sel_val
->type
->type
== glsl_bool_type()) {
4217 /* This case is illegal but some older versions of GLSLang produce
4218 * it. The GLSLang issue was fixed on March 30, 2017:
4220 * https://github.com/KhronosGroup/glslang/issues/809
4222 * Unfortunately, there are applications in the wild which are
4223 * shipping with this bug so it isn't nice to fail on them so we
4224 * throw a warning instead. It's not actually a problem for us as
4225 * nir_builder will just splat the condition out which is most
4226 * likely what the client wanted anyway.
4228 vtn_warn("Condition type of OpSelect must have the same number "
4229 "of components as Result Type");
4231 vtn_fail("Condition type of OpSelect must be a scalar or vector "
4232 "of Boolean type. It must have the same number of "
4233 "components as Result Type");
4237 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4238 obj2_val
->type
!= res_val
->type
,
4239 "Object types must match the result type in OpSelect");
4241 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4242 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, res_type
->type
);
4243 ssa
->def
= nir_bcsel(&b
->nb
, vtn_ssa_value(b
, w
[3])->def
,
4244 vtn_ssa_value(b
, w
[4])->def
,
4245 vtn_ssa_value(b
, w
[5])->def
);
4246 vtn_push_ssa(b
, w
[2], res_type
, ssa
);
4255 case SpvOpConvertFToU
:
4256 case SpvOpConvertFToS
:
4257 case SpvOpConvertSToF
:
4258 case SpvOpConvertUToF
:
4262 case SpvOpQuantizeToF16
:
4263 case SpvOpPtrCastToGeneric
:
4264 case SpvOpGenericCastToPtr
:
4270 case SpvOpSignBitSet
:
4271 case SpvOpLessOrGreater
:
4273 case SpvOpUnordered
:
4288 case SpvOpVectorTimesScalar
:
4290 case SpvOpIAddCarry
:
4291 case SpvOpISubBorrow
:
4292 case SpvOpUMulExtended
:
4293 case SpvOpSMulExtended
:
4294 case SpvOpShiftRightLogical
:
4295 case SpvOpShiftRightArithmetic
:
4296 case SpvOpShiftLeftLogical
:
4297 case SpvOpLogicalEqual
:
4298 case SpvOpLogicalNotEqual
:
4299 case SpvOpLogicalOr
:
4300 case SpvOpLogicalAnd
:
4301 case SpvOpLogicalNot
:
4302 case SpvOpBitwiseOr
:
4303 case SpvOpBitwiseXor
:
4304 case SpvOpBitwiseAnd
:
4306 case SpvOpFOrdEqual
:
4307 case SpvOpFUnordEqual
:
4308 case SpvOpINotEqual
:
4309 case SpvOpFOrdNotEqual
:
4310 case SpvOpFUnordNotEqual
:
4311 case SpvOpULessThan
:
4312 case SpvOpSLessThan
:
4313 case SpvOpFOrdLessThan
:
4314 case SpvOpFUnordLessThan
:
4315 case SpvOpUGreaterThan
:
4316 case SpvOpSGreaterThan
:
4317 case SpvOpFOrdGreaterThan
:
4318 case SpvOpFUnordGreaterThan
:
4319 case SpvOpULessThanEqual
:
4320 case SpvOpSLessThanEqual
:
4321 case SpvOpFOrdLessThanEqual
:
4322 case SpvOpFUnordLessThanEqual
:
4323 case SpvOpUGreaterThanEqual
:
4324 case SpvOpSGreaterThanEqual
:
4325 case SpvOpFOrdGreaterThanEqual
:
4326 case SpvOpFUnordGreaterThanEqual
:
4332 case SpvOpFwidthFine
:
4333 case SpvOpDPdxCoarse
:
4334 case SpvOpDPdyCoarse
:
4335 case SpvOpFwidthCoarse
:
4336 case SpvOpBitFieldInsert
:
4337 case SpvOpBitFieldSExtract
:
4338 case SpvOpBitFieldUExtract
:
4339 case SpvOpBitReverse
:
4341 case SpvOpTranspose
:
4342 case SpvOpOuterProduct
:
4343 case SpvOpMatrixTimesScalar
:
4344 case SpvOpVectorTimesMatrix
:
4345 case SpvOpMatrixTimesVector
:
4346 case SpvOpMatrixTimesMatrix
:
4347 vtn_handle_alu(b
, opcode
, w
, count
);
4350 case SpvOpVectorExtractDynamic
:
4351 case SpvOpVectorInsertDynamic
:
4352 case SpvOpVectorShuffle
:
4353 case SpvOpCompositeConstruct
:
4354 case SpvOpCompositeExtract
:
4355 case SpvOpCompositeInsert
:
4356 case SpvOpCopyObject
:
4357 vtn_handle_composite(b
, opcode
, w
, count
);
4360 case SpvOpEmitVertex
:
4361 case SpvOpEndPrimitive
:
4362 case SpvOpEmitStreamVertex
:
4363 case SpvOpEndStreamPrimitive
:
4364 case SpvOpControlBarrier
:
4365 case SpvOpMemoryBarrier
:
4366 vtn_handle_barrier(b
, opcode
, w
, count
);
4369 case SpvOpGroupNonUniformElect
:
4370 case SpvOpGroupNonUniformAll
:
4371 case SpvOpGroupNonUniformAny
:
4372 case SpvOpGroupNonUniformAllEqual
:
4373 case SpvOpGroupNonUniformBroadcast
:
4374 case SpvOpGroupNonUniformBroadcastFirst
:
4375 case SpvOpGroupNonUniformBallot
:
4376 case SpvOpGroupNonUniformInverseBallot
:
4377 case SpvOpGroupNonUniformBallotBitExtract
:
4378 case SpvOpGroupNonUniformBallotBitCount
:
4379 case SpvOpGroupNonUniformBallotFindLSB
:
4380 case SpvOpGroupNonUniformBallotFindMSB
:
4381 case SpvOpGroupNonUniformShuffle
:
4382 case SpvOpGroupNonUniformShuffleXor
:
4383 case SpvOpGroupNonUniformShuffleUp
:
4384 case SpvOpGroupNonUniformShuffleDown
:
4385 case SpvOpGroupNonUniformIAdd
:
4386 case SpvOpGroupNonUniformFAdd
:
4387 case SpvOpGroupNonUniformIMul
:
4388 case SpvOpGroupNonUniformFMul
:
4389 case SpvOpGroupNonUniformSMin
:
4390 case SpvOpGroupNonUniformUMin
:
4391 case SpvOpGroupNonUniformFMin
:
4392 case SpvOpGroupNonUniformSMax
:
4393 case SpvOpGroupNonUniformUMax
:
4394 case SpvOpGroupNonUniformFMax
:
4395 case SpvOpGroupNonUniformBitwiseAnd
:
4396 case SpvOpGroupNonUniformBitwiseOr
:
4397 case SpvOpGroupNonUniformBitwiseXor
:
4398 case SpvOpGroupNonUniformLogicalAnd
:
4399 case SpvOpGroupNonUniformLogicalOr
:
4400 case SpvOpGroupNonUniformLogicalXor
:
4401 case SpvOpGroupNonUniformQuadBroadcast
:
4402 case SpvOpGroupNonUniformQuadSwap
:
4403 vtn_handle_subgroup(b
, opcode
, w
, count
);
4407 vtn_fail("Unhandled opcode");
4414 vtn_create_builder(const uint32_t *words
, size_t word_count
,
4415 gl_shader_stage stage
, const char *entry_point_name
,
4416 const struct spirv_to_nir_options
*options
)
4418 /* Initialize the vtn_builder object */
4419 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
4420 struct spirv_to_nir_options
*dup_options
=
4421 ralloc(b
, struct spirv_to_nir_options
);
4422 *dup_options
= *options
;
4425 b
->spirv_word_count
= word_count
;
4429 exec_list_make_empty(&b
->functions
);
4430 b
->entry_point_stage
= stage
;
4431 b
->entry_point_name
= entry_point_name
;
4432 b
->options
= dup_options
;
4435 * Handle the SPIR-V header (first 5 dwords).
4436 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
4438 if (word_count
<= 5)
4441 if (words
[0] != SpvMagicNumber
) {
4442 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
4445 if (words
[1] < 0x10000) {
4446 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
4450 uint16_t generator_id
= words
[2] >> 16;
4451 uint16_t generator_version
= words
[2];
4453 /* The first GLSLang version bump actually 1.5 years after #179 was fixed
4454 * but this should at least let us shut the workaround off for modern
4455 * versions of GLSLang.
4457 b
->wa_glslang_179
= (generator_id
== 8 && generator_version
== 1);
4459 /* words[2] == generator magic */
4460 unsigned value_id_bound
= words
[3];
4461 if (words
[4] != 0) {
4462 vtn_err("words[4] was %u, want 0", words
[4]);
4466 b
->value_id_bound
= value_id_bound
;
4467 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
4475 static nir_function
*
4476 vtn_emit_kernel_entry_point_wrapper(struct vtn_builder
*b
,
4477 nir_function
*entry_point
)
4479 vtn_assert(entry_point
== b
->entry_point
->func
->impl
->function
);
4480 vtn_fail_if(!entry_point
->name
, "entry points are required to have a name");
4481 const char *func_name
=
4482 ralloc_asprintf(b
->shader
, "__wrapped_%s", entry_point
->name
);
4484 /* we shouldn't have any inputs yet */
4485 vtn_assert(!entry_point
->shader
->num_inputs
);
4486 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
);
4488 nir_function
*main_entry_point
= nir_function_create(b
->shader
, func_name
);
4489 main_entry_point
->impl
= nir_function_impl_create(main_entry_point
);
4490 nir_builder_init(&b
->nb
, main_entry_point
->impl
);
4491 b
->nb
.cursor
= nir_after_cf_list(&main_entry_point
->impl
->body
);
4492 b
->func_param_idx
= 0;
4494 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, entry_point
);
4496 for (unsigned i
= 0; i
< entry_point
->num_params
; ++i
) {
4497 struct vtn_type
*param_type
= b
->entry_point
->func
->type
->params
[i
];
4499 /* consider all pointers to function memory to be parameters passed
4502 bool is_by_val
= param_type
->base_type
== vtn_base_type_pointer
&&
4503 param_type
->storage_class
== SpvStorageClassFunction
;
4505 /* input variable */
4506 nir_variable
*in_var
= rzalloc(b
->nb
.shader
, nir_variable
);
4507 in_var
->data
.mode
= nir_var_shader_in
;
4508 in_var
->data
.read_only
= true;
4509 in_var
->data
.location
= i
;
4512 in_var
->type
= param_type
->deref
->type
;
4514 in_var
->type
= param_type
->type
;
4516 nir_shader_add_variable(b
->nb
.shader
, in_var
);
4517 b
->nb
.shader
->num_inputs
++;
4519 /* we have to copy the entire variable into function memory */
4521 nir_variable
*copy_var
=
4522 nir_local_variable_create(main_entry_point
->impl
, in_var
->type
,
4524 nir_copy_var(&b
->nb
, copy_var
, in_var
);
4526 nir_src_for_ssa(&nir_build_deref_var(&b
->nb
, copy_var
)->dest
.ssa
);
4528 call
->params
[i
] = nir_src_for_ssa(nir_load_var(&b
->nb
, in_var
));
4532 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
4534 return main_entry_point
;
4538 spirv_to_nir(const uint32_t *words
, size_t word_count
,
4539 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
4540 gl_shader_stage stage
, const char *entry_point_name
,
4541 const struct spirv_to_nir_options
*options
,
4542 const nir_shader_compiler_options
*nir_options
)
4545 const uint32_t *word_end
= words
+ word_count
;
4547 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
4548 stage
, entry_point_name
,
4554 /* See also _vtn_fail() */
4555 if (setjmp(b
->fail_jump
)) {
4560 /* Skip the SPIR-V header, handled at vtn_create_builder */
4563 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
4565 /* Handle all the preamble instructions */
4566 words
= vtn_foreach_instruction(b
, words
, word_end
,
4567 vtn_handle_preamble_instruction
);
4569 if (b
->entry_point
== NULL
) {
4570 vtn_fail("Entry point not found");
4575 /* Set shader info defaults */
4576 b
->shader
->info
.gs
.invocations
= 1;
4578 b
->specializations
= spec
;
4579 b
->num_specializations
= num_spec
;
4581 /* Handle all variable, type, and constant instructions */
4582 words
= vtn_foreach_instruction(b
, words
, word_end
,
4583 vtn_handle_variable_or_type_instruction
);
4585 /* Parse execution modes */
4586 vtn_foreach_execution_mode(b
, b
->entry_point
,
4587 vtn_handle_execution_mode
, NULL
);
4589 if (b
->workgroup_size_builtin
) {
4590 vtn_assert(b
->workgroup_size_builtin
->type
->type
==
4591 glsl_vector_type(GLSL_TYPE_UINT
, 3));
4593 nir_const_value
*const_size
=
4594 &b
->workgroup_size_builtin
->constant
->values
[0];
4596 b
->shader
->info
.cs
.local_size
[0] = const_size
->u32
[0];
4597 b
->shader
->info
.cs
.local_size
[1] = const_size
->u32
[1];
4598 b
->shader
->info
.cs
.local_size
[2] = const_size
->u32
[2];
4601 /* Set types on all vtn_values */
4602 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
4604 vtn_build_cfg(b
, words
, word_end
);
4606 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4607 b
->entry_point
->func
->referenced
= true;
4612 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
4613 if (func
->referenced
&& !func
->emitted
) {
4614 b
->const_table
= _mesa_pointer_hash_table_create(b
);
4616 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
4622 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4623 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
4624 vtn_assert(entry_point
);
4626 /* post process entry_points with input params */
4627 if (entry_point
->num_params
&& b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
4628 entry_point
= vtn_emit_kernel_entry_point_wrapper(b
, entry_point
);
4630 entry_point
->is_entrypoint
= true;
4632 /* When multiple shader stages exist in the same SPIR-V module, we
4633 * generate input and output variables for every stage, in the same
4634 * NIR program. These dead variables can be invalid NIR. For example,
4635 * TCS outputs must be per-vertex arrays (or decorated 'patch'), while
4636 * VS output variables wouldn't be.
4638 * To ensure we have valid NIR, we eliminate any dead inputs and outputs
4639 * right away. In order to do so, we must lower any constant initializers
4640 * on outputs so nir_remove_dead_variables sees that they're written to.
4642 nir_lower_constant_initializers(b
->shader
, nir_var_shader_out
);
4643 nir_remove_dead_variables(b
->shader
,
4644 nir_var_shader_in
| nir_var_shader_out
);
4646 /* We sometimes generate bogus derefs that, while never used, give the
4647 * validator a bit of heartburn. Run dead code to get rid of them.
4649 nir_opt_dce(b
->shader
);
4651 /* Unparent the shader from the vtn_builder before we delete the builder */
4652 ralloc_steal(NULL
, b
->shader
);