2 * Copyright © 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Jason Ekstrand (jason@jlekstrand.net)
28 #include "vtn_private.h"
29 #include "nir/nir_vla.h"
30 #include "nir/nir_control_flow.h"
31 #include "nir/nir_constant_expressions.h"
32 #include "nir/nir_deref.h"
33 #include "spirv_info.h"
35 #include "util/u_math.h"
40 vtn_log(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
41 size_t spirv_offset
, const char *message
)
43 if (b
->options
->debug
.func
) {
44 b
->options
->debug
.func(b
->options
->debug
.private_data
,
45 level
, spirv_offset
, message
);
49 if (level
>= NIR_SPIRV_DEBUG_LEVEL_WARNING
)
50 fprintf(stderr
, "%s\n", message
);
55 vtn_logf(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
56 size_t spirv_offset
, const char *fmt
, ...)
62 msg
= ralloc_vasprintf(NULL
, fmt
, args
);
65 vtn_log(b
, level
, spirv_offset
, msg
);
71 vtn_log_err(struct vtn_builder
*b
,
72 enum nir_spirv_debug_level level
, const char *prefix
,
73 const char *file
, unsigned line
,
74 const char *fmt
, va_list args
)
78 msg
= ralloc_strdup(NULL
, prefix
);
81 ralloc_asprintf_append(&msg
, " In file %s:%u\n", file
, line
);
84 ralloc_asprintf_append(&msg
, " ");
86 ralloc_vasprintf_append(&msg
, fmt
, args
);
88 ralloc_asprintf_append(&msg
, "\n %zu bytes into the SPIR-V binary",
92 ralloc_asprintf_append(&msg
,
93 "\n in SPIR-V source file %s, line %d, col %d",
94 b
->file
, b
->line
, b
->col
);
97 vtn_log(b
, level
, b
->spirv_offset
, msg
);
103 vtn_dump_shader(struct vtn_builder
*b
, const char *path
, const char *prefix
)
108 int len
= snprintf(filename
, sizeof(filename
), "%s/%s-%d.spirv",
109 path
, prefix
, idx
++);
110 if (len
< 0 || len
>= sizeof(filename
))
113 FILE *f
= fopen(filename
, "w");
117 fwrite(b
->spirv
, sizeof(*b
->spirv
), b
->spirv_word_count
, f
);
120 vtn_info("SPIR-V shader dumped to %s", filename
);
124 _vtn_warn(struct vtn_builder
*b
, const char *file
, unsigned line
,
125 const char *fmt
, ...)
130 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_WARNING
, "SPIR-V WARNING:\n",
131 file
, line
, fmt
, args
);
136 _vtn_err(struct vtn_builder
*b
, const char *file
, unsigned line
,
137 const char *fmt
, ...)
142 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V ERROR:\n",
143 file
, line
, fmt
, args
);
148 _vtn_fail(struct vtn_builder
*b
, const char *file
, unsigned line
,
149 const char *fmt
, ...)
154 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V parsing FAILED:\n",
155 file
, line
, fmt
, args
);
158 const char *dump_path
= getenv("MESA_SPIRV_FAIL_DUMP_PATH");
160 vtn_dump_shader(b
, dump_path
, "fail");
162 longjmp(b
->fail_jump
, 1);
165 struct spec_constant_value
{
173 static struct vtn_ssa_value
*
174 vtn_undef_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
176 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
179 if (glsl_type_is_vector_or_scalar(type
)) {
180 unsigned num_components
= glsl_get_vector_elements(val
->type
);
181 unsigned bit_size
= glsl_get_bit_size(val
->type
);
182 val
->def
= nir_ssa_undef(&b
->nb
, num_components
, bit_size
);
184 unsigned elems
= glsl_get_length(val
->type
);
185 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
186 if (glsl_type_is_matrix(type
)) {
187 const struct glsl_type
*elem_type
=
188 glsl_vector_type(glsl_get_base_type(type
),
189 glsl_get_vector_elements(type
));
191 for (unsigned i
= 0; i
< elems
; i
++)
192 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
193 } else if (glsl_type_is_array(type
)) {
194 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
195 for (unsigned i
= 0; i
< elems
; i
++)
196 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
198 for (unsigned i
= 0; i
< elems
; i
++) {
199 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
200 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
208 static struct vtn_ssa_value
*
209 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
210 const struct glsl_type
*type
)
212 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
217 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
220 switch (glsl_get_base_type(type
)) {
223 case GLSL_TYPE_INT16
:
224 case GLSL_TYPE_UINT16
:
225 case GLSL_TYPE_UINT8
:
227 case GLSL_TYPE_INT64
:
228 case GLSL_TYPE_UINT64
:
230 case GLSL_TYPE_FLOAT
:
231 case GLSL_TYPE_FLOAT16
:
232 case GLSL_TYPE_DOUBLE
: {
233 int bit_size
= glsl_get_bit_size(type
);
234 if (glsl_type_is_vector_or_scalar(type
)) {
235 unsigned num_components
= glsl_get_vector_elements(val
->type
);
236 nir_load_const_instr
*load
=
237 nir_load_const_instr_create(b
->shader
, num_components
, bit_size
);
239 memcpy(load
->value
, constant
->values
[0],
240 sizeof(nir_const_value
) * load
->def
.num_components
);
242 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
243 val
->def
= &load
->def
;
245 assert(glsl_type_is_matrix(type
));
246 unsigned rows
= glsl_get_vector_elements(val
->type
);
247 unsigned columns
= glsl_get_matrix_columns(val
->type
);
248 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, columns
);
250 for (unsigned i
= 0; i
< columns
; i
++) {
251 struct vtn_ssa_value
*col_val
= rzalloc(b
, struct vtn_ssa_value
);
252 col_val
->type
= glsl_get_column_type(val
->type
);
253 nir_load_const_instr
*load
=
254 nir_load_const_instr_create(b
->shader
, rows
, bit_size
);
256 memcpy(load
->value
, constant
->values
[i
],
257 sizeof(nir_const_value
) * load
->def
.num_components
);
259 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
260 col_val
->def
= &load
->def
;
262 val
->elems
[i
] = col_val
;
268 case GLSL_TYPE_ARRAY
: {
269 unsigned elems
= glsl_get_length(val
->type
);
270 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
271 const struct glsl_type
*elem_type
= glsl_get_array_element(val
->type
);
272 for (unsigned i
= 0; i
< elems
; i
++)
273 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
278 case GLSL_TYPE_STRUCT
: {
279 unsigned elems
= glsl_get_length(val
->type
);
280 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
281 for (unsigned i
= 0; i
< elems
; i
++) {
282 const struct glsl_type
*elem_type
=
283 glsl_get_struct_field(val
->type
, i
);
284 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
291 vtn_fail("bad constant type");
297 struct vtn_ssa_value
*
298 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
300 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
301 switch (val
->value_type
) {
302 case vtn_value_type_undef
:
303 return vtn_undef_ssa_value(b
, val
->type
->type
);
305 case vtn_value_type_constant
:
306 return vtn_const_ssa_value(b
, val
->constant
, val
->type
->type
);
308 case vtn_value_type_ssa
:
311 case vtn_value_type_pointer
:
312 vtn_assert(val
->pointer
->ptr_type
&& val
->pointer
->ptr_type
->type
);
313 struct vtn_ssa_value
*ssa
=
314 vtn_create_ssa_value(b
, val
->pointer
->ptr_type
->type
);
315 ssa
->def
= vtn_pointer_to_ssa(b
, val
->pointer
);
319 vtn_fail("Invalid type for an SSA value");
324 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
325 unsigned word_count
, unsigned *words_used
)
327 char *dup
= ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
329 /* Ammount of space taken by the string (including the null) */
330 unsigned len
= strlen(dup
) + 1;
331 *words_used
= DIV_ROUND_UP(len
, sizeof(*words
));
337 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
338 const uint32_t *end
, vtn_instruction_handler handler
)
344 const uint32_t *w
= start
;
346 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
347 unsigned count
= w
[0] >> SpvWordCountShift
;
348 vtn_assert(count
>= 1 && w
+ count
<= end
);
350 b
->spirv_offset
= (uint8_t *)w
- (uint8_t *)b
->spirv
;
354 break; /* Do nothing */
357 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
369 if (!handler(b
, opcode
, w
, count
))
387 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
388 const uint32_t *w
, unsigned count
)
390 const char *ext
= (const char *)&w
[2];
392 case SpvOpExtInstImport
: {
393 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
394 if (strcmp(ext
, "GLSL.std.450") == 0) {
395 val
->ext_handler
= vtn_handle_glsl450_instruction
;
396 } else if ((strcmp(ext
, "SPV_AMD_gcn_shader") == 0)
397 && (b
->options
&& b
->options
->caps
.gcn_shader
)) {
398 val
->ext_handler
= vtn_handle_amd_gcn_shader_instruction
;
399 } else if ((strcmp(ext
, "SPV_AMD_shader_trinary_minmax") == 0)
400 && (b
->options
&& b
->options
->caps
.trinary_minmax
)) {
401 val
->ext_handler
= vtn_handle_amd_shader_trinary_minmax_instruction
;
402 } else if (strcmp(ext
, "OpenCL.std") == 0) {
403 val
->ext_handler
= vtn_handle_opencl_instruction
;
405 vtn_fail("Unsupported extension: %s", ext
);
411 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
412 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
418 vtn_fail_with_opcode("Unhandled opcode", opcode
);
423 _foreach_decoration_helper(struct vtn_builder
*b
,
424 struct vtn_value
*base_value
,
426 struct vtn_value
*value
,
427 vtn_decoration_foreach_cb cb
, void *data
)
429 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
431 if (dec
->scope
== VTN_DEC_DECORATION
) {
432 member
= parent_member
;
433 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
434 vtn_fail_if(value
->value_type
!= vtn_value_type_type
||
435 value
->type
->base_type
!= vtn_base_type_struct
,
436 "OpMemberDecorate and OpGroupMemberDecorate are only "
437 "allowed on OpTypeStruct");
438 /* This means we haven't recursed yet */
439 assert(value
== base_value
);
441 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
443 vtn_fail_if(member
>= base_value
->type
->length
,
444 "OpMemberDecorate specifies member %d but the "
445 "OpTypeStruct has only %u members",
446 member
, base_value
->type
->length
);
448 /* Not a decoration */
449 assert(dec
->scope
== VTN_DEC_EXECUTION_MODE
);
454 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
455 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
458 cb(b
, base_value
, member
, dec
, data
);
463 /** Iterates (recursively if needed) over all of the decorations on a value
465 * This function iterates over all of the decorations applied to a given
466 * value. If it encounters a decoration group, it recurses into the group
467 * and iterates over all of those decorations as well.
470 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
471 vtn_decoration_foreach_cb cb
, void *data
)
473 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
477 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
478 vtn_execution_mode_foreach_cb cb
, void *data
)
480 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
481 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
484 assert(dec
->group
== NULL
);
485 cb(b
, value
, dec
, data
);
490 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
491 const uint32_t *w
, unsigned count
)
493 const uint32_t *w_end
= w
+ count
;
494 const uint32_t target
= w
[1];
498 case SpvOpDecorationGroup
:
499 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
503 case SpvOpDecorateId
:
504 case SpvOpMemberDecorate
:
505 case SpvOpDecorateStringGOOGLE
:
506 case SpvOpMemberDecorateStringGOOGLE
:
507 case SpvOpExecutionMode
:
508 case SpvOpExecutionModeId
: {
509 struct vtn_value
*val
= vtn_untyped_value(b
, target
);
511 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
514 case SpvOpDecorateId
:
515 case SpvOpDecorateStringGOOGLE
:
516 dec
->scope
= VTN_DEC_DECORATION
;
518 case SpvOpMemberDecorate
:
519 case SpvOpMemberDecorateStringGOOGLE
:
520 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
521 vtn_fail_if(dec
->scope
< VTN_DEC_STRUCT_MEMBER0
, /* overflow */
522 "Member argument of OpMemberDecorate too large");
524 case SpvOpExecutionMode
:
525 case SpvOpExecutionModeId
:
526 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
529 unreachable("Invalid decoration opcode");
531 dec
->decoration
= *(w
++);
534 /* Link into the list */
535 dec
->next
= val
->decoration
;
536 val
->decoration
= dec
;
540 case SpvOpGroupMemberDecorate
:
541 case SpvOpGroupDecorate
: {
542 struct vtn_value
*group
=
543 vtn_value(b
, target
, vtn_value_type_decoration_group
);
545 for (; w
< w_end
; w
++) {
546 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
547 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
550 if (opcode
== SpvOpGroupDecorate
) {
551 dec
->scope
= VTN_DEC_DECORATION
;
553 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
554 vtn_fail_if(dec
->scope
< 0, /* Check for overflow */
555 "Member argument of OpGroupMemberDecorate too large");
558 /* Link into the list */
559 dec
->next
= val
->decoration
;
560 val
->decoration
= dec
;
566 unreachable("Unhandled opcode");
570 struct member_decoration_ctx
{
572 struct glsl_struct_field
*fields
;
573 struct vtn_type
*type
;
577 * Returns true if the given type contains a struct decorated Block or
581 vtn_type_contains_block(struct vtn_builder
*b
, struct vtn_type
*type
)
583 switch (type
->base_type
) {
584 case vtn_base_type_array
:
585 return vtn_type_contains_block(b
, type
->array_element
);
586 case vtn_base_type_struct
:
587 if (type
->block
|| type
->buffer_block
)
589 for (unsigned i
= 0; i
< type
->length
; i
++) {
590 if (vtn_type_contains_block(b
, type
->members
[i
]))
599 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
600 * OpStore, or OpCopyMemory between them without breaking anything.
601 * Technically, the SPIR-V rules require the exact same type ID but this lets
602 * us internally be a bit looser.
605 vtn_types_compatible(struct vtn_builder
*b
,
606 struct vtn_type
*t1
, struct vtn_type
*t2
)
608 if (t1
->id
== t2
->id
)
611 if (t1
->base_type
!= t2
->base_type
)
614 switch (t1
->base_type
) {
615 case vtn_base_type_void
:
616 case vtn_base_type_scalar
:
617 case vtn_base_type_vector
:
618 case vtn_base_type_matrix
:
619 case vtn_base_type_image
:
620 case vtn_base_type_sampler
:
621 case vtn_base_type_sampled_image
:
622 return t1
->type
== t2
->type
;
624 case vtn_base_type_array
:
625 return t1
->length
== t2
->length
&&
626 vtn_types_compatible(b
, t1
->array_element
, t2
->array_element
);
628 case vtn_base_type_pointer
:
629 return vtn_types_compatible(b
, t1
->deref
, t2
->deref
);
631 case vtn_base_type_struct
:
632 if (t1
->length
!= t2
->length
)
635 for (unsigned i
= 0; i
< t1
->length
; i
++) {
636 if (!vtn_types_compatible(b
, t1
->members
[i
], t2
->members
[i
]))
641 case vtn_base_type_function
:
642 /* This case shouldn't get hit since you can't copy around function
643 * types. Just require them to be identical.
648 vtn_fail("Invalid base type");
651 /* does a shallow copy of a vtn_type */
653 static struct vtn_type
*
654 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
656 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
659 switch (src
->base_type
) {
660 case vtn_base_type_void
:
661 case vtn_base_type_scalar
:
662 case vtn_base_type_vector
:
663 case vtn_base_type_matrix
:
664 case vtn_base_type_array
:
665 case vtn_base_type_pointer
:
666 case vtn_base_type_image
:
667 case vtn_base_type_sampler
:
668 case vtn_base_type_sampled_image
:
669 /* Nothing more to do */
672 case vtn_base_type_struct
:
673 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
674 memcpy(dest
->members
, src
->members
,
675 src
->length
* sizeof(src
->members
[0]));
677 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
678 memcpy(dest
->offsets
, src
->offsets
,
679 src
->length
* sizeof(src
->offsets
[0]));
682 case vtn_base_type_function
:
683 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
684 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
691 static struct vtn_type
*
692 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
694 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
695 type
= type
->members
[member
];
697 /* We may have an array of matrices.... Oh, joy! */
698 while (glsl_type_is_array(type
->type
)) {
699 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
700 type
= type
->array_element
;
703 vtn_assert(glsl_type_is_matrix(type
->type
));
709 vtn_handle_access_qualifier(struct vtn_builder
*b
, struct vtn_type
*type
,
710 int member
, enum gl_access_qualifier access
)
712 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
713 type
= type
->members
[member
];
715 type
->access
|= access
;
719 array_stride_decoration_cb(struct vtn_builder
*b
,
720 struct vtn_value
*val
, int member
,
721 const struct vtn_decoration
*dec
, void *void_ctx
)
723 struct vtn_type
*type
= val
->type
;
725 if (dec
->decoration
== SpvDecorationArrayStride
) {
726 vtn_fail_if(dec
->operands
[0] == 0, "ArrayStride must be non-zero");
727 type
->stride
= dec
->operands
[0];
732 struct_member_decoration_cb(struct vtn_builder
*b
,
733 struct vtn_value
*val
, int member
,
734 const struct vtn_decoration
*dec
, void *void_ctx
)
736 struct member_decoration_ctx
*ctx
= void_ctx
;
741 assert(member
< ctx
->num_fields
);
743 switch (dec
->decoration
) {
744 case SpvDecorationRelaxedPrecision
:
745 case SpvDecorationUniform
:
746 break; /* FIXME: Do nothing with this for now. */
747 case SpvDecorationNonWritable
:
748 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_WRITEABLE
);
750 case SpvDecorationNonReadable
:
751 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_READABLE
);
753 case SpvDecorationVolatile
:
754 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_VOLATILE
);
756 case SpvDecorationCoherent
:
757 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_COHERENT
);
759 case SpvDecorationNoPerspective
:
760 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
762 case SpvDecorationFlat
:
763 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
765 case SpvDecorationCentroid
:
766 ctx
->fields
[member
].centroid
= true;
768 case SpvDecorationSample
:
769 ctx
->fields
[member
].sample
= true;
771 case SpvDecorationStream
:
772 /* Vulkan only allows one GS stream */
773 vtn_assert(dec
->operands
[0] == 0);
775 case SpvDecorationLocation
:
776 ctx
->fields
[member
].location
= dec
->operands
[0];
778 case SpvDecorationComponent
:
779 break; /* FIXME: What should we do with these? */
780 case SpvDecorationBuiltIn
:
781 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
782 ctx
->type
->members
[member
]->is_builtin
= true;
783 ctx
->type
->members
[member
]->builtin
= dec
->operands
[0];
784 ctx
->type
->builtin_block
= true;
786 case SpvDecorationOffset
:
787 ctx
->type
->offsets
[member
] = dec
->operands
[0];
788 ctx
->fields
[member
].offset
= dec
->operands
[0];
790 case SpvDecorationMatrixStride
:
791 /* Handled as a second pass */
793 case SpvDecorationColMajor
:
794 break; /* Nothing to do here. Column-major is the default. */
795 case SpvDecorationRowMajor
:
796 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
799 case SpvDecorationPatch
:
802 case SpvDecorationSpecId
:
803 case SpvDecorationBlock
:
804 case SpvDecorationBufferBlock
:
805 case SpvDecorationArrayStride
:
806 case SpvDecorationGLSLShared
:
807 case SpvDecorationGLSLPacked
:
808 case SpvDecorationInvariant
:
809 case SpvDecorationRestrict
:
810 case SpvDecorationAliased
:
811 case SpvDecorationConstant
:
812 case SpvDecorationIndex
:
813 case SpvDecorationBinding
:
814 case SpvDecorationDescriptorSet
:
815 case SpvDecorationLinkageAttributes
:
816 case SpvDecorationNoContraction
:
817 case SpvDecorationInputAttachmentIndex
:
818 vtn_warn("Decoration not allowed on struct members: %s",
819 spirv_decoration_to_string(dec
->decoration
));
822 case SpvDecorationXfbBuffer
:
823 case SpvDecorationXfbStride
:
824 vtn_warn("Vulkan does not have transform feedback");
827 case SpvDecorationCPacked
:
828 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
829 vtn_warn("Decoration only allowed for CL-style kernels: %s",
830 spirv_decoration_to_string(dec
->decoration
));
832 ctx
->type
->packed
= true;
835 case SpvDecorationSaturatedConversion
:
836 case SpvDecorationFuncParamAttr
:
837 case SpvDecorationFPRoundingMode
:
838 case SpvDecorationFPFastMathMode
:
839 case SpvDecorationAlignment
:
840 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
841 vtn_warn("Decoration only allowed for CL-style kernels: %s",
842 spirv_decoration_to_string(dec
->decoration
));
846 case SpvDecorationHlslSemanticGOOGLE
:
847 /* HLSL semantic decorations can safely be ignored by the driver. */
851 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
855 /** Chases the array type all the way down to the tail and rewrites the
856 * glsl_types to be based off the tail's glsl_type.
859 vtn_array_type_rewrite_glsl_type(struct vtn_type
*type
)
861 if (type
->base_type
!= vtn_base_type_array
)
864 vtn_array_type_rewrite_glsl_type(type
->array_element
);
866 type
->type
= glsl_array_type(type
->array_element
->type
,
867 type
->length
, type
->stride
);
870 /* Matrix strides are handled as a separate pass because we need to know
871 * whether the matrix is row-major or not first.
874 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
875 struct vtn_value
*val
, int member
,
876 const struct vtn_decoration
*dec
,
879 if (dec
->decoration
!= SpvDecorationMatrixStride
)
882 vtn_fail_if(member
< 0,
883 "The MatrixStride decoration is only allowed on members "
885 vtn_fail_if(dec
->operands
[0] == 0, "MatrixStride must be non-zero");
887 struct member_decoration_ctx
*ctx
= void_ctx
;
889 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
890 if (mat_type
->row_major
) {
891 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
892 mat_type
->stride
= mat_type
->array_element
->stride
;
893 mat_type
->array_element
->stride
= dec
->operands
[0];
895 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
896 dec
->operands
[0], true);
897 mat_type
->array_element
->type
= glsl_get_column_type(mat_type
->type
);
899 vtn_assert(mat_type
->array_element
->stride
> 0);
900 mat_type
->stride
= dec
->operands
[0];
902 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
903 dec
->operands
[0], false);
906 /* Now that we've replaced the glsl_type with a properly strided matrix
907 * type, rewrite the member type so that it's an array of the proper kind
910 vtn_array_type_rewrite_glsl_type(ctx
->type
->members
[member
]);
911 ctx
->fields
[member
].type
= ctx
->type
->members
[member
]->type
;
915 struct_block_decoration_cb(struct vtn_builder
*b
,
916 struct vtn_value
*val
, int member
,
917 const struct vtn_decoration
*dec
, void *ctx
)
922 struct vtn_type
*type
= val
->type
;
923 if (dec
->decoration
== SpvDecorationBlock
)
925 else if (dec
->decoration
== SpvDecorationBufferBlock
)
926 type
->buffer_block
= true;
930 type_decoration_cb(struct vtn_builder
*b
,
931 struct vtn_value
*val
, int member
,
932 const struct vtn_decoration
*dec
, void *ctx
)
934 struct vtn_type
*type
= val
->type
;
937 /* This should have been handled by OpTypeStruct */
938 assert(val
->type
->base_type
== vtn_base_type_struct
);
939 assert(member
>= 0 && member
< val
->type
->length
);
943 switch (dec
->decoration
) {
944 case SpvDecorationArrayStride
:
945 vtn_assert(type
->base_type
== vtn_base_type_array
||
946 type
->base_type
== vtn_base_type_pointer
);
948 case SpvDecorationBlock
:
949 vtn_assert(type
->base_type
== vtn_base_type_struct
);
950 vtn_assert(type
->block
);
952 case SpvDecorationBufferBlock
:
953 vtn_assert(type
->base_type
== vtn_base_type_struct
);
954 vtn_assert(type
->buffer_block
);
956 case SpvDecorationGLSLShared
:
957 case SpvDecorationGLSLPacked
:
958 /* Ignore these, since we get explicit offsets anyways */
961 case SpvDecorationRowMajor
:
962 case SpvDecorationColMajor
:
963 case SpvDecorationMatrixStride
:
964 case SpvDecorationBuiltIn
:
965 case SpvDecorationNoPerspective
:
966 case SpvDecorationFlat
:
967 case SpvDecorationPatch
:
968 case SpvDecorationCentroid
:
969 case SpvDecorationSample
:
970 case SpvDecorationVolatile
:
971 case SpvDecorationCoherent
:
972 case SpvDecorationNonWritable
:
973 case SpvDecorationNonReadable
:
974 case SpvDecorationUniform
:
975 case SpvDecorationLocation
:
976 case SpvDecorationComponent
:
977 case SpvDecorationOffset
:
978 case SpvDecorationXfbBuffer
:
979 case SpvDecorationXfbStride
:
980 case SpvDecorationHlslSemanticGOOGLE
:
981 vtn_warn("Decoration only allowed for struct members: %s",
982 spirv_decoration_to_string(dec
->decoration
));
985 case SpvDecorationStream
:
986 /* We don't need to do anything here, as stream is filled up when
987 * aplying the decoration to a variable, just check that if it is not a
988 * struct member, it should be a struct.
990 vtn_assert(type
->base_type
== vtn_base_type_struct
);
993 case SpvDecorationRelaxedPrecision
:
994 case SpvDecorationSpecId
:
995 case SpvDecorationInvariant
:
996 case SpvDecorationRestrict
:
997 case SpvDecorationAliased
:
998 case SpvDecorationConstant
:
999 case SpvDecorationIndex
:
1000 case SpvDecorationBinding
:
1001 case SpvDecorationDescriptorSet
:
1002 case SpvDecorationLinkageAttributes
:
1003 case SpvDecorationNoContraction
:
1004 case SpvDecorationInputAttachmentIndex
:
1005 vtn_warn("Decoration not allowed on types: %s",
1006 spirv_decoration_to_string(dec
->decoration
));
1009 case SpvDecorationCPacked
:
1010 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
1011 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1012 spirv_decoration_to_string(dec
->decoration
));
1014 type
->packed
= true;
1017 case SpvDecorationSaturatedConversion
:
1018 case SpvDecorationFuncParamAttr
:
1019 case SpvDecorationFPRoundingMode
:
1020 case SpvDecorationFPFastMathMode
:
1021 case SpvDecorationAlignment
:
1022 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1023 spirv_decoration_to_string(dec
->decoration
));
1027 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1032 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
1035 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
1036 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
1037 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
1038 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
1039 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
1040 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
1041 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
1042 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
1043 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
1044 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
1045 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
1046 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
1047 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
1048 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
1049 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
1050 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
1051 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
1052 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
1053 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
1054 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
1055 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
1056 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
1057 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
1058 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
1059 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
1060 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
1061 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
1062 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
1063 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
1064 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
1065 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
1066 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
1067 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
1068 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
1069 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
1070 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
1071 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
1072 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
1073 case SpvImageFormatR16ui
: return 0x8234; /* GL_R16UI */
1074 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
1076 vtn_fail("Invalid image format: %s (%u)",
1077 spirv_imageformat_to_string(format
), format
);
1081 static struct vtn_type
*
1082 vtn_type_layout_std430(struct vtn_builder
*b
, struct vtn_type
*type
,
1083 uint32_t *size_out
, uint32_t *align_out
)
1085 switch (type
->base_type
) {
1086 case vtn_base_type_scalar
: {
1087 uint32_t comp_size
= glsl_type_is_boolean(type
->type
)
1088 ? 4 : glsl_get_bit_size(type
->type
) / 8;
1089 *size_out
= comp_size
;
1090 *align_out
= comp_size
;
1094 case vtn_base_type_vector
: {
1095 uint32_t comp_size
= glsl_type_is_boolean(type
->type
)
1096 ? 4 : glsl_get_bit_size(type
->type
) / 8;
1097 unsigned align_comps
= type
->length
== 3 ? 4 : type
->length
;
1098 *size_out
= comp_size
* type
->length
,
1099 *align_out
= comp_size
* align_comps
;
1103 case vtn_base_type_matrix
:
1104 case vtn_base_type_array
: {
1105 /* We're going to add an array stride */
1106 type
= vtn_type_copy(b
, type
);
1107 uint32_t elem_size
, elem_align
;
1108 type
->array_element
= vtn_type_layout_std430(b
, type
->array_element
,
1109 &elem_size
, &elem_align
);
1110 type
->stride
= vtn_align_u32(elem_size
, elem_align
);
1111 *size_out
= type
->stride
* type
->length
;
1112 *align_out
= elem_align
;
1116 case vtn_base_type_struct
: {
1117 /* We're going to add member offsets */
1118 type
= vtn_type_copy(b
, type
);
1119 uint32_t offset
= 0;
1121 for (unsigned i
= 0; i
< type
->length
; i
++) {
1122 uint32_t mem_size
, mem_align
;
1123 type
->members
[i
] = vtn_type_layout_std430(b
, type
->members
[i
],
1124 &mem_size
, &mem_align
);
1125 offset
= vtn_align_u32(offset
, mem_align
);
1126 type
->offsets
[i
] = offset
;
1128 align
= MAX2(align
, mem_align
);
1136 unreachable("Invalid SPIR-V type for std430");
1141 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
1142 const uint32_t *w
, unsigned count
)
1144 struct vtn_value
*val
= NULL
;
1146 /* In order to properly handle forward declarations, we have to defer
1147 * allocation for pointer types.
1149 if (opcode
!= SpvOpTypePointer
&& opcode
!= SpvOpTypeForwardPointer
) {
1150 val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1151 vtn_fail_if(val
->type
!= NULL
,
1152 "Only pointers can have forward declarations");
1153 val
->type
= rzalloc(b
, struct vtn_type
);
1154 val
->type
->id
= w
[1];
1159 val
->type
->base_type
= vtn_base_type_void
;
1160 val
->type
->type
= glsl_void_type();
1163 val
->type
->base_type
= vtn_base_type_scalar
;
1164 val
->type
->type
= glsl_bool_type();
1165 val
->type
->length
= 1;
1167 case SpvOpTypeInt
: {
1168 int bit_size
= w
[2];
1169 const bool signedness
= w
[3];
1170 val
->type
->base_type
= vtn_base_type_scalar
;
1173 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1176 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1179 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1182 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1185 vtn_fail("Invalid int bit size: %u", bit_size
);
1187 val
->type
->length
= 1;
1191 case SpvOpTypeFloat
: {
1192 int bit_size
= w
[2];
1193 val
->type
->base_type
= vtn_base_type_scalar
;
1196 val
->type
->type
= glsl_float16_t_type();
1199 val
->type
->type
= glsl_float_type();
1202 val
->type
->type
= glsl_double_type();
1205 vtn_fail("Invalid float bit size: %u", bit_size
);
1207 val
->type
->length
= 1;
1211 case SpvOpTypeVector
: {
1212 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1213 unsigned elems
= w
[3];
1215 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1216 "Base type for OpTypeVector must be a scalar");
1217 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1218 "Invalid component count for OpTypeVector");
1220 val
->type
->base_type
= vtn_base_type_vector
;
1221 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1222 val
->type
->length
= elems
;
1223 val
->type
->stride
= glsl_type_is_boolean(val
->type
->type
)
1224 ? 4 : glsl_get_bit_size(base
->type
) / 8;
1225 val
->type
->array_element
= base
;
1229 case SpvOpTypeMatrix
: {
1230 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1231 unsigned columns
= w
[3];
1233 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1234 "Base type for OpTypeMatrix must be a vector");
1235 vtn_fail_if(columns
< 2 || columns
> 4,
1236 "Invalid column count for OpTypeMatrix");
1238 val
->type
->base_type
= vtn_base_type_matrix
;
1239 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1240 glsl_get_vector_elements(base
->type
),
1242 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1243 "Unsupported base type for OpTypeMatrix");
1244 assert(!glsl_type_is_error(val
->type
->type
));
1245 val
->type
->length
= columns
;
1246 val
->type
->array_element
= base
;
1247 val
->type
->row_major
= false;
1248 val
->type
->stride
= 0;
1252 case SpvOpTypeRuntimeArray
:
1253 case SpvOpTypeArray
: {
1254 struct vtn_type
*array_element
=
1255 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1257 if (opcode
== SpvOpTypeRuntimeArray
) {
1258 /* A length of 0 is used to denote unsized arrays */
1259 val
->type
->length
= 0;
1262 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->values
[0][0].u32
;
1265 val
->type
->base_type
= vtn_base_type_array
;
1266 val
->type
->array_element
= array_element
;
1267 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
1268 val
->type
->stride
= glsl_get_cl_size(array_element
->type
);
1270 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1271 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
,
1276 case SpvOpTypeStruct
: {
1277 unsigned num_fields
= count
- 2;
1278 val
->type
->base_type
= vtn_base_type_struct
;
1279 val
->type
->length
= num_fields
;
1280 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1281 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1282 val
->type
->packed
= false;
1284 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1285 for (unsigned i
= 0; i
< num_fields
; i
++) {
1286 val
->type
->members
[i
] =
1287 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
1288 fields
[i
] = (struct glsl_struct_field
) {
1289 .type
= val
->type
->members
[i
]->type
,
1290 .name
= ralloc_asprintf(b
, "field%d", i
),
1296 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1297 unsigned offset
= 0;
1298 for (unsigned i
= 0; i
< num_fields
; i
++) {
1299 offset
= align(offset
, glsl_get_cl_alignment(fields
[i
].type
));
1300 fields
[i
].offset
= offset
;
1301 offset
+= glsl_get_cl_size(fields
[i
].type
);
1305 struct member_decoration_ctx ctx
= {
1306 .num_fields
= num_fields
,
1311 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1312 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1314 vtn_foreach_decoration(b
, val
, struct_block_decoration_cb
, NULL
);
1316 const char *name
= val
->name
;
1318 if (val
->type
->block
|| val
->type
->buffer_block
) {
1319 /* Packing will be ignored since types coming from SPIR-V are
1320 * explicitly laid out.
1322 val
->type
->type
= glsl_interface_type(fields
, num_fields
,
1323 /* packing */ 0, false,
1324 name
? name
: "block");
1326 val
->type
->type
= glsl_struct_type(fields
, num_fields
,
1327 name
? name
: "struct", false);
1332 case SpvOpTypeFunction
: {
1333 val
->type
->base_type
= vtn_base_type_function
;
1334 val
->type
->type
= NULL
;
1336 val
->type
->return_type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1338 const unsigned num_params
= count
- 3;
1339 val
->type
->length
= num_params
;
1340 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1341 for (unsigned i
= 0; i
< count
- 3; i
++) {
1342 val
->type
->params
[i
] =
1343 vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
;
1348 case SpvOpTypePointer
:
1349 case SpvOpTypeForwardPointer
: {
1350 /* We can't blindly push the value because it might be a forward
1353 val
= vtn_untyped_value(b
, w
[1]);
1355 SpvStorageClass storage_class
= w
[2];
1357 if (val
->value_type
== vtn_value_type_invalid
) {
1358 val
->value_type
= vtn_value_type_type
;
1359 val
->type
= rzalloc(b
, struct vtn_type
);
1360 val
->type
->id
= w
[1];
1361 val
->type
->base_type
= vtn_base_type_pointer
;
1362 val
->type
->storage_class
= storage_class
;
1364 /* These can actually be stored to nir_variables and used as SSA
1365 * values so they need a real glsl_type.
1367 switch (storage_class
) {
1368 case SpvStorageClassUniform
:
1369 val
->type
->type
= b
->options
->ubo_ptr_type
;
1371 case SpvStorageClassStorageBuffer
:
1372 val
->type
->type
= b
->options
->ssbo_ptr_type
;
1374 case SpvStorageClassPhysicalStorageBufferEXT
:
1375 val
->type
->type
= b
->options
->phys_ssbo_ptr_type
;
1377 case SpvStorageClassPushConstant
:
1378 val
->type
->type
= b
->options
->push_const_ptr_type
;
1380 case SpvStorageClassWorkgroup
:
1381 val
->type
->type
= b
->options
->shared_ptr_type
;
1383 case SpvStorageClassCrossWorkgroup
:
1384 val
->type
->type
= b
->options
->global_ptr_type
;
1386 case SpvStorageClassFunction
:
1387 if (b
->physical_ptrs
)
1388 val
->type
->type
= b
->options
->temp_ptr_type
;
1391 /* In this case, no variable pointers are allowed so all deref
1392 * chains are complete back to the variable and it doesn't matter
1393 * what type gets used so we leave it NULL.
1398 vtn_fail_if(val
->type
->storage_class
!= storage_class
,
1399 "The storage classes of an OpTypePointer and any "
1400 "OpTypeForwardPointers that provide forward "
1401 "declarations of it must match.");
1404 if (opcode
== SpvOpTypePointer
) {
1405 vtn_fail_if(val
->type
->deref
!= NULL
,
1406 "While OpTypeForwardPointer can be used to provide a "
1407 "forward declaration of a pointer, OpTypePointer can "
1408 "only be used once for a given id.");
1410 val
->type
->deref
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
1412 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1414 if (b
->physical_ptrs
) {
1415 switch (storage_class
) {
1416 case SpvStorageClassFunction
:
1417 case SpvStorageClassWorkgroup
:
1418 case SpvStorageClassCrossWorkgroup
:
1419 val
->type
->stride
= align(glsl_get_cl_size(val
->type
->deref
->type
),
1420 glsl_get_cl_alignment(val
->type
->deref
->type
));
1427 if (storage_class
== SpvStorageClassWorkgroup
&&
1428 b
->options
->lower_workgroup_access_to_offsets
) {
1429 uint32_t size
, align
;
1430 val
->type
->deref
= vtn_type_layout_std430(b
, val
->type
->deref
,
1432 val
->type
->length
= size
;
1433 val
->type
->align
= align
;
1439 case SpvOpTypeImage
: {
1440 val
->type
->base_type
= vtn_base_type_image
;
1442 const struct vtn_type
*sampled_type
=
1443 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1445 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1446 glsl_get_bit_size(sampled_type
->type
) != 32,
1447 "Sampled type of OpTypeImage must be a 32-bit scalar");
1449 enum glsl_sampler_dim dim
;
1450 switch ((SpvDim
)w
[3]) {
1451 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1452 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1453 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1454 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1455 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1456 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1457 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1459 vtn_fail("Invalid SPIR-V image dimensionality: %s (%u)",
1460 spirv_dim_to_string((SpvDim
)w
[3]), w
[3]);
1463 /* w[4]: as per Vulkan spec "Validation Rules within a Module",
1464 * The “Depth” operand of OpTypeImage is ignored.
1466 bool is_array
= w
[5];
1467 bool multisampled
= w
[6];
1468 unsigned sampled
= w
[7];
1469 SpvImageFormat format
= w
[8];
1472 val
->type
->access_qualifier
= w
[9];
1474 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1477 if (dim
== GLSL_SAMPLER_DIM_2D
)
1478 dim
= GLSL_SAMPLER_DIM_MS
;
1479 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1480 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1482 vtn_fail("Unsupported multisampled image type");
1485 val
->type
->image_format
= translate_image_format(b
, format
);
1487 enum glsl_base_type sampled_base_type
=
1488 glsl_get_base_type(sampled_type
->type
);
1490 val
->type
->sampled
= true;
1491 val
->type
->type
= glsl_sampler_type(dim
, false, is_array
,
1493 } else if (sampled
== 2) {
1494 val
->type
->sampled
= false;
1495 val
->type
->type
= glsl_image_type(dim
, is_array
, sampled_base_type
);
1497 vtn_fail("We need to know if the image will be sampled");
1502 case SpvOpTypeSampledImage
:
1503 val
->type
->base_type
= vtn_base_type_sampled_image
;
1504 val
->type
->image
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1505 val
->type
->type
= val
->type
->image
->type
;
1508 case SpvOpTypeSampler
:
1509 /* The actual sampler type here doesn't really matter. It gets
1510 * thrown away the moment you combine it with an image. What really
1511 * matters is that it's a sampler type as opposed to an integer type
1512 * so the backend knows what to do.
1514 val
->type
->base_type
= vtn_base_type_sampler
;
1515 val
->type
->type
= glsl_bare_sampler_type();
1518 case SpvOpTypeOpaque
:
1519 case SpvOpTypeEvent
:
1520 case SpvOpTypeDeviceEvent
:
1521 case SpvOpTypeReserveId
:
1522 case SpvOpTypeQueue
:
1525 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1528 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1530 if (val
->type
->base_type
== vtn_base_type_struct
&&
1531 (val
->type
->block
|| val
->type
->buffer_block
)) {
1532 for (unsigned i
= 0; i
< val
->type
->length
; i
++) {
1533 vtn_fail_if(vtn_type_contains_block(b
, val
->type
->members
[i
]),
1534 "Block and BufferBlock decorations cannot decorate a "
1535 "structure type that is nested at any level inside "
1536 "another structure type decorated with Block or "
1542 static nir_constant
*
1543 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
1545 nir_constant
*c
= rzalloc(b
, nir_constant
);
1547 /* For pointers and other typeless things, we have to return something but
1548 * it doesn't matter what.
1553 switch (glsl_get_base_type(type
)) {
1555 case GLSL_TYPE_UINT
:
1556 case GLSL_TYPE_INT16
:
1557 case GLSL_TYPE_UINT16
:
1558 case GLSL_TYPE_UINT8
:
1559 case GLSL_TYPE_INT8
:
1560 case GLSL_TYPE_INT64
:
1561 case GLSL_TYPE_UINT64
:
1562 case GLSL_TYPE_BOOL
:
1563 case GLSL_TYPE_FLOAT
:
1564 case GLSL_TYPE_FLOAT16
:
1565 case GLSL_TYPE_DOUBLE
:
1566 /* Nothing to do here. It's already initialized to zero */
1569 case GLSL_TYPE_ARRAY
:
1570 vtn_assert(glsl_get_length(type
) > 0);
1571 c
->num_elements
= glsl_get_length(type
);
1572 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1574 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
1575 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1576 c
->elements
[i
] = c
->elements
[0];
1579 case GLSL_TYPE_STRUCT
:
1580 c
->num_elements
= glsl_get_length(type
);
1581 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1583 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
1584 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
1589 vtn_fail("Invalid type for null constant");
1596 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
1597 int member
, const struct vtn_decoration
*dec
,
1600 vtn_assert(member
== -1);
1601 if (dec
->decoration
!= SpvDecorationSpecId
)
1604 struct spec_constant_value
*const_value
= data
;
1606 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1607 if (b
->specializations
[i
].id
== dec
->operands
[0]) {
1608 if (const_value
->is_double
)
1609 const_value
->data64
= b
->specializations
[i
].data64
;
1611 const_value
->data32
= b
->specializations
[i
].data32
;
1618 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
1619 uint32_t const_value
)
1621 struct spec_constant_value data
;
1622 data
.is_double
= false;
1623 data
.data32
= const_value
;
1624 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1629 get_specialization64(struct vtn_builder
*b
, struct vtn_value
*val
,
1630 uint64_t const_value
)
1632 struct spec_constant_value data
;
1633 data
.is_double
= true;
1634 data
.data64
= const_value
;
1635 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1640 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1641 struct vtn_value
*val
,
1643 const struct vtn_decoration
*dec
,
1646 vtn_assert(member
== -1);
1647 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1648 dec
->operands
[0] != SpvBuiltInWorkgroupSize
)
1651 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1652 b
->workgroup_size_builtin
= val
;
1656 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1657 const uint32_t *w
, unsigned count
)
1659 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1660 val
->constant
= rzalloc(b
, nir_constant
);
1662 case SpvOpConstantTrue
:
1663 case SpvOpConstantFalse
:
1664 case SpvOpSpecConstantTrue
:
1665 case SpvOpSpecConstantFalse
: {
1666 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1667 "Result type of %s must be OpTypeBool",
1668 spirv_op_to_string(opcode
));
1670 uint32_t int_val
= (opcode
== SpvOpConstantTrue
||
1671 opcode
== SpvOpSpecConstantTrue
);
1673 if (opcode
== SpvOpSpecConstantTrue
||
1674 opcode
== SpvOpSpecConstantFalse
)
1675 int_val
= get_specialization(b
, val
, int_val
);
1677 val
->constant
->values
[0][0].b
= int_val
!= 0;
1681 case SpvOpConstant
: {
1682 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1683 "Result type of %s must be a scalar",
1684 spirv_op_to_string(opcode
));
1685 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1688 val
->constant
->values
[0][0].u64
= vtn_u64_literal(&w
[3]);
1691 val
->constant
->values
[0][0].u32
= w
[3];
1694 val
->constant
->values
[0][0].u16
= w
[3];
1697 val
->constant
->values
[0][0].u8
= w
[3];
1700 vtn_fail("Unsupported SpvOpConstant bit size: %u", bit_size
);
1705 case SpvOpSpecConstant
: {
1706 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1707 "Result type of %s must be a scalar",
1708 spirv_op_to_string(opcode
));
1709 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1712 val
->constant
->values
[0][0].u64
=
1713 get_specialization64(b
, val
, vtn_u64_literal(&w
[3]));
1716 val
->constant
->values
[0][0].u32
= get_specialization(b
, val
, w
[3]);
1719 val
->constant
->values
[0][0].u16
= get_specialization(b
, val
, w
[3]);
1722 val
->constant
->values
[0][0].u8
= get_specialization(b
, val
, w
[3]);
1725 vtn_fail("Unsupported SpvOpSpecConstant bit size");
1730 case SpvOpSpecConstantComposite
:
1731 case SpvOpConstantComposite
: {
1732 unsigned elem_count
= count
- 3;
1733 vtn_fail_if(elem_count
!= val
->type
->length
,
1734 "%s has %u constituents, expected %u",
1735 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1737 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1738 for (unsigned i
= 0; i
< elem_count
; i
++) {
1739 struct vtn_value
*val
= vtn_untyped_value(b
, w
[i
+ 3]);
1741 if (val
->value_type
== vtn_value_type_constant
) {
1742 elems
[i
] = val
->constant
;
1744 vtn_fail_if(val
->value_type
!= vtn_value_type_undef
,
1745 "only constants or undefs allowed for "
1746 "SpvOpConstantComposite");
1747 /* to make it easier, just insert a NULL constant for now */
1748 elems
[i
] = vtn_null_constant(b
, val
->type
->type
);
1752 switch (val
->type
->base_type
) {
1753 case vtn_base_type_vector
: {
1754 assert(glsl_type_is_vector(val
->type
->type
));
1755 for (unsigned i
= 0; i
< elem_count
; i
++)
1756 val
->constant
->values
[0][i
] = elems
[i
]->values
[0][0];
1760 case vtn_base_type_matrix
:
1761 assert(glsl_type_is_matrix(val
->type
->type
));
1762 for (unsigned i
= 0; i
< elem_count
; i
++) {
1763 unsigned components
=
1764 glsl_get_components(glsl_get_column_type(val
->type
->type
));
1765 memcpy(val
->constant
->values
[i
], elems
[i
]->values
,
1766 sizeof(nir_const_value
) * components
);
1770 case vtn_base_type_struct
:
1771 case vtn_base_type_array
:
1772 ralloc_steal(val
->constant
, elems
);
1773 val
->constant
->num_elements
= elem_count
;
1774 val
->constant
->elements
= elems
;
1778 vtn_fail("Result type of %s must be a composite type",
1779 spirv_op_to_string(opcode
));
1784 case SpvOpSpecConstantOp
: {
1785 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1787 case SpvOpVectorShuffle
: {
1788 struct vtn_value
*v0
= &b
->values
[w
[4]];
1789 struct vtn_value
*v1
= &b
->values
[w
[5]];
1791 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1792 v0
->value_type
== vtn_value_type_undef
);
1793 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1794 v1
->value_type
== vtn_value_type_undef
);
1796 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1797 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1799 vtn_assert(len0
+ len1
< 16);
1801 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1802 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1803 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1805 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1806 (void)bit_size0
; (void)bit_size1
;
1808 if (bit_size
== 64) {
1810 if (v0
->value_type
== vtn_value_type_constant
) {
1811 for (unsigned i
= 0; i
< len0
; i
++)
1812 u64
[i
] = v0
->constant
->values
[0][i
].u64
;
1814 if (v1
->value_type
== vtn_value_type_constant
) {
1815 for (unsigned i
= 0; i
< len1
; i
++)
1816 u64
[len0
+ i
] = v1
->constant
->values
[0][i
].u64
;
1819 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1820 uint32_t comp
= w
[i
+ 6];
1821 /* If component is not used, set the value to a known constant
1822 * to detect if it is wrongly used.
1824 if (comp
== (uint32_t)-1)
1825 val
->constant
->values
[0][j
].u64
= 0xdeadbeefdeadbeef;
1827 val
->constant
->values
[0][j
].u64
= u64
[comp
];
1830 /* This is for both 32-bit and 16-bit values */
1832 if (v0
->value_type
== vtn_value_type_constant
) {
1833 for (unsigned i
= 0; i
< len0
; i
++)
1834 u32
[i
] = v0
->constant
->values
[0][i
].u32
;
1836 if (v1
->value_type
== vtn_value_type_constant
) {
1837 for (unsigned i
= 0; i
< len1
; i
++)
1838 u32
[len0
+ i
] = v1
->constant
->values
[0][i
].u32
;
1841 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1842 uint32_t comp
= w
[i
+ 6];
1843 /* If component is not used, set the value to a known constant
1844 * to detect if it is wrongly used.
1846 if (comp
== (uint32_t)-1)
1847 val
->constant
->values
[0][j
].u32
= 0xdeadbeef;
1849 val
->constant
->values
[0][j
].u32
= u32
[comp
];
1855 case SpvOpCompositeExtract
:
1856 case SpvOpCompositeInsert
: {
1857 struct vtn_value
*comp
;
1858 unsigned deref_start
;
1859 struct nir_constant
**c
;
1860 if (opcode
== SpvOpCompositeExtract
) {
1861 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1863 c
= &comp
->constant
;
1865 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1867 val
->constant
= nir_constant_clone(comp
->constant
,
1874 const struct vtn_type
*type
= comp
->type
;
1875 for (unsigned i
= deref_start
; i
< count
; i
++) {
1876 vtn_fail_if(w
[i
] > type
->length
,
1877 "%uth index of %s is %u but the type has only "
1878 "%u elements", i
- deref_start
,
1879 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1881 switch (type
->base_type
) {
1882 case vtn_base_type_vector
:
1884 type
= type
->array_element
;
1887 case vtn_base_type_matrix
:
1888 assert(col
== 0 && elem
== -1);
1891 type
= type
->array_element
;
1894 case vtn_base_type_array
:
1895 c
= &(*c
)->elements
[w
[i
]];
1896 type
= type
->array_element
;
1899 case vtn_base_type_struct
:
1900 c
= &(*c
)->elements
[w
[i
]];
1901 type
= type
->members
[w
[i
]];
1905 vtn_fail("%s must only index into composite types",
1906 spirv_op_to_string(opcode
));
1910 if (opcode
== SpvOpCompositeExtract
) {
1914 unsigned num_components
= type
->length
;
1915 for (unsigned i
= 0; i
< num_components
; i
++)
1916 val
->constant
->values
[0][i
] = (*c
)->values
[col
][elem
+ i
];
1919 struct vtn_value
*insert
=
1920 vtn_value(b
, w
[4], vtn_value_type_constant
);
1921 vtn_assert(insert
->type
== type
);
1923 *c
= insert
->constant
;
1925 unsigned num_components
= type
->length
;
1926 for (unsigned i
= 0; i
< num_components
; i
++)
1927 (*c
)->values
[col
][elem
+ i
] = insert
->constant
->values
[0][i
];
1935 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
1936 nir_alu_type src_alu_type
= dst_alu_type
;
1937 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
1940 vtn_assert(count
<= 7);
1945 /* We have a source in a conversion */
1947 nir_get_nir_type_for_glsl_type(
1948 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1949 /* We use the bitsize of the conversion source to evaluate the opcode later */
1950 bit_size
= glsl_get_bit_size(
1951 vtn_value(b
, w
[4], vtn_value_type_constant
)->type
->type
);
1954 bit_size
= glsl_get_bit_size(val
->type
->type
);
1957 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
1958 nir_alu_type_get_type_size(src_alu_type
),
1959 nir_alu_type_get_type_size(dst_alu_type
));
1960 nir_const_value src
[3][NIR_MAX_VEC_COMPONENTS
];
1962 for (unsigned i
= 0; i
< count
- 4; i
++) {
1963 struct vtn_value
*src_val
=
1964 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
);
1966 /* If this is an unsized source, pull the bit size from the
1967 * source; otherwise, we'll use the bit size from the destination.
1969 if (!nir_alu_type_get_type_size(nir_op_infos
[op
].input_types
[i
]))
1970 bit_size
= glsl_get_bit_size(src_val
->type
->type
);
1972 unsigned j
= swap
? 1 - i
: i
;
1973 memcpy(src
[j
], src_val
->constant
->values
[0], sizeof(src
[j
]));
1976 /* fix up fixed size sources */
1983 for (unsigned i
= 0; i
< num_components
; ++i
) {
1985 case 64: src
[1][i
].u32
= src
[1][i
].u64
; break;
1986 case 16: src
[1][i
].u32
= src
[1][i
].u16
; break;
1987 case 8: src
[1][i
].u32
= src
[1][i
].u8
; break;
1996 nir_const_value
*srcs
[3] = {
1997 src
[0], src
[1], src
[2],
1999 nir_eval_const_opcode(op
, val
->constant
->values
[0], num_components
, bit_size
, srcs
);
2006 case SpvOpConstantNull
:
2007 val
->constant
= vtn_null_constant(b
, val
->type
->type
);
2010 case SpvOpConstantSampler
:
2011 vtn_fail("OpConstantSampler requires Kernel Capability");
2015 vtn_fail_with_opcode("Unhandled opcode", opcode
);
2018 /* Now that we have the value, update the workgroup size if needed */
2019 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
2022 struct vtn_ssa_value
*
2023 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
2025 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
2028 if (!glsl_type_is_vector_or_scalar(type
)) {
2029 unsigned elems
= glsl_get_length(type
);
2030 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2031 for (unsigned i
= 0; i
< elems
; i
++) {
2032 const struct glsl_type
*child_type
;
2034 switch (glsl_get_base_type(type
)) {
2036 case GLSL_TYPE_UINT
:
2037 case GLSL_TYPE_INT16
:
2038 case GLSL_TYPE_UINT16
:
2039 case GLSL_TYPE_UINT8
:
2040 case GLSL_TYPE_INT8
:
2041 case GLSL_TYPE_INT64
:
2042 case GLSL_TYPE_UINT64
:
2043 case GLSL_TYPE_BOOL
:
2044 case GLSL_TYPE_FLOAT
:
2045 case GLSL_TYPE_FLOAT16
:
2046 case GLSL_TYPE_DOUBLE
:
2047 child_type
= glsl_get_column_type(type
);
2049 case GLSL_TYPE_ARRAY
:
2050 child_type
= glsl_get_array_element(type
);
2052 case GLSL_TYPE_STRUCT
:
2053 case GLSL_TYPE_INTERFACE
:
2054 child_type
= glsl_get_struct_field(type
, i
);
2057 vtn_fail("unkown base type");
2060 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
2068 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
2071 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
2072 src
.src_type
= type
;
2077 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
2078 const uint32_t *w
, unsigned count
)
2080 if (opcode
== SpvOpSampledImage
) {
2081 struct vtn_value
*val
=
2082 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
2083 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
2084 val
->sampled_image
->type
=
2085 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2086 val
->sampled_image
->image
=
2087 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2088 val
->sampled_image
->sampler
=
2089 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
2091 } else if (opcode
== SpvOpImage
) {
2092 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
2093 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
2094 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
2095 val
->pointer
= src_val
->sampled_image
->image
;
2097 vtn_assert(src_val
->value_type
== vtn_value_type_pointer
);
2098 val
->pointer
= src_val
->pointer
;
2103 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2104 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2106 struct vtn_sampled_image sampled
;
2107 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
2108 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
2109 sampled
= *sampled_val
->sampled_image
;
2111 vtn_assert(sampled_val
->value_type
== vtn_value_type_pointer
);
2112 sampled
.type
= sampled_val
->pointer
->type
;
2113 sampled
.image
= NULL
;
2114 sampled
.sampler
= sampled_val
->pointer
;
2117 const struct glsl_type
*image_type
= sampled
.type
->type
;
2118 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
2119 const bool is_array
= glsl_sampler_type_is_array(image_type
);
2121 /* Figure out the base texture operation */
2124 case SpvOpImageSampleImplicitLod
:
2125 case SpvOpImageSampleDrefImplicitLod
:
2126 case SpvOpImageSampleProjImplicitLod
:
2127 case SpvOpImageSampleProjDrefImplicitLod
:
2128 texop
= nir_texop_tex
;
2131 case SpvOpImageSampleExplicitLod
:
2132 case SpvOpImageSampleDrefExplicitLod
:
2133 case SpvOpImageSampleProjExplicitLod
:
2134 case SpvOpImageSampleProjDrefExplicitLod
:
2135 texop
= nir_texop_txl
;
2138 case SpvOpImageFetch
:
2139 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
2140 texop
= nir_texop_txf_ms
;
2142 texop
= nir_texop_txf
;
2146 case SpvOpImageGather
:
2147 case SpvOpImageDrefGather
:
2148 texop
= nir_texop_tg4
;
2151 case SpvOpImageQuerySizeLod
:
2152 case SpvOpImageQuerySize
:
2153 texop
= nir_texop_txs
;
2156 case SpvOpImageQueryLod
:
2157 texop
= nir_texop_lod
;
2160 case SpvOpImageQueryLevels
:
2161 texop
= nir_texop_query_levels
;
2164 case SpvOpImageQuerySamples
:
2165 texop
= nir_texop_texture_samples
;
2169 vtn_fail_with_opcode("Unhandled opcode", opcode
);
2172 nir_tex_src srcs
[10]; /* 10 should be enough */
2173 nir_tex_src
*p
= srcs
;
2175 nir_deref_instr
*sampler
= vtn_pointer_to_deref(b
, sampled
.sampler
);
2176 nir_deref_instr
*texture
=
2177 sampled
.image
? vtn_pointer_to_deref(b
, sampled
.image
) : sampler
;
2179 p
->src
= nir_src_for_ssa(&texture
->dest
.ssa
);
2180 p
->src_type
= nir_tex_src_texture_deref
;
2190 /* These operations require a sampler */
2191 p
->src
= nir_src_for_ssa(&sampler
->dest
.ssa
);
2192 p
->src_type
= nir_tex_src_sampler_deref
;
2196 case nir_texop_txf_ms
:
2198 case nir_texop_query_levels
:
2199 case nir_texop_texture_samples
:
2200 case nir_texop_samples_identical
:
2203 case nir_texop_txf_ms_fb
:
2204 vtn_fail("unexpected nir_texop_txf_ms_fb");
2206 case nir_texop_txf_ms_mcs
:
2207 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2212 struct nir_ssa_def
*coord
;
2213 unsigned coord_components
;
2215 case SpvOpImageSampleImplicitLod
:
2216 case SpvOpImageSampleExplicitLod
:
2217 case SpvOpImageSampleDrefImplicitLod
:
2218 case SpvOpImageSampleDrefExplicitLod
:
2219 case SpvOpImageSampleProjImplicitLod
:
2220 case SpvOpImageSampleProjExplicitLod
:
2221 case SpvOpImageSampleProjDrefImplicitLod
:
2222 case SpvOpImageSampleProjDrefExplicitLod
:
2223 case SpvOpImageFetch
:
2224 case SpvOpImageGather
:
2225 case SpvOpImageDrefGather
:
2226 case SpvOpImageQueryLod
: {
2227 /* All these types have the coordinate as their first real argument */
2228 switch (sampler_dim
) {
2229 case GLSL_SAMPLER_DIM_1D
:
2230 case GLSL_SAMPLER_DIM_BUF
:
2231 coord_components
= 1;
2233 case GLSL_SAMPLER_DIM_2D
:
2234 case GLSL_SAMPLER_DIM_RECT
:
2235 case GLSL_SAMPLER_DIM_MS
:
2236 coord_components
= 2;
2238 case GLSL_SAMPLER_DIM_3D
:
2239 case GLSL_SAMPLER_DIM_CUBE
:
2240 coord_components
= 3;
2243 vtn_fail("Invalid sampler type");
2246 if (is_array
&& texop
!= nir_texop_lod
)
2249 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
2250 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2251 (1 << coord_components
) - 1));
2252 p
->src_type
= nir_tex_src_coord
;
2259 coord_components
= 0;
2264 case SpvOpImageSampleProjImplicitLod
:
2265 case SpvOpImageSampleProjExplicitLod
:
2266 case SpvOpImageSampleProjDrefImplicitLod
:
2267 case SpvOpImageSampleProjDrefExplicitLod
:
2268 /* These have the projector as the last coordinate component */
2269 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2270 p
->src_type
= nir_tex_src_projector
;
2278 bool is_shadow
= false;
2279 unsigned gather_component
= 0;
2281 case SpvOpImageSampleDrefImplicitLod
:
2282 case SpvOpImageSampleDrefExplicitLod
:
2283 case SpvOpImageSampleProjDrefImplicitLod
:
2284 case SpvOpImageSampleProjDrefExplicitLod
:
2285 case SpvOpImageDrefGather
:
2286 /* These all have an explicit depth value as their next source */
2288 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2291 case SpvOpImageGather
:
2292 /* This has a component as its next source */
2294 vtn_value(b
, w
[idx
++], vtn_value_type_constant
)->constant
->values
[0][0].u32
;
2301 /* For OpImageQuerySizeLod, we always have an LOD */
2302 if (opcode
== SpvOpImageQuerySizeLod
)
2303 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2305 /* Now we need to handle some number of optional arguments */
2306 struct vtn_value
*gather_offsets
= NULL
;
2308 uint32_t operands
= w
[idx
++];
2310 if (operands
& SpvImageOperandsBiasMask
) {
2311 vtn_assert(texop
== nir_texop_tex
);
2312 texop
= nir_texop_txb
;
2313 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
2316 if (operands
& SpvImageOperandsLodMask
) {
2317 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2318 texop
== nir_texop_txs
);
2319 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2322 if (operands
& SpvImageOperandsGradMask
) {
2323 vtn_assert(texop
== nir_texop_txl
);
2324 texop
= nir_texop_txd
;
2325 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
2326 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
2329 if (operands
& SpvImageOperandsOffsetMask
||
2330 operands
& SpvImageOperandsConstOffsetMask
)
2331 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
2333 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2334 vtn_assert(texop
== nir_texop_tg4
);
2335 gather_offsets
= vtn_value(b
, w
[idx
++], vtn_value_type_constant
);
2338 if (operands
& SpvImageOperandsSampleMask
) {
2339 vtn_assert(texop
== nir_texop_txf_ms
);
2340 texop
= nir_texop_txf_ms
;
2341 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
2344 if (operands
& SpvImageOperandsMinLodMask
) {
2345 vtn_assert(texop
== nir_texop_tex
||
2346 texop
== nir_texop_txb
||
2347 texop
== nir_texop_txd
);
2348 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_min_lod
);
2351 /* We should have now consumed exactly all of the arguments */
2352 vtn_assert(idx
== count
);
2354 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2357 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2359 instr
->coord_components
= coord_components
;
2360 instr
->sampler_dim
= sampler_dim
;
2361 instr
->is_array
= is_array
;
2362 instr
->is_shadow
= is_shadow
;
2363 instr
->is_new_style_shadow
=
2364 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2365 instr
->component
= gather_component
;
2367 if (sampled
.image
&& (sampled
.image
->access
& ACCESS_NON_UNIFORM
))
2368 instr
->texture_non_uniform
= true;
2370 if (sampled
.sampler
&& (sampled
.sampler
->access
& ACCESS_NON_UNIFORM
))
2371 instr
->sampler_non_uniform
= true;
2373 switch (glsl_get_sampler_result_type(image_type
)) {
2374 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
2375 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
2376 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
2377 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
2379 vtn_fail("Invalid base type for sampler result");
2382 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2383 nir_tex_instr_dest_size(instr
), 32, NULL
);
2385 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2386 nir_tex_instr_dest_size(instr
));
2388 if (gather_offsets
) {
2389 vtn_fail_if(gather_offsets
->type
->base_type
!= vtn_base_type_array
||
2390 gather_offsets
->type
->length
!= 4,
2391 "ConstOffsets must be an array of size four of vectors "
2392 "of two integer components");
2394 struct vtn_type
*vec_type
= gather_offsets
->type
->array_element
;
2395 vtn_fail_if(vec_type
->base_type
!= vtn_base_type_vector
||
2396 vec_type
->length
!= 2 ||
2397 !glsl_type_is_integer(vec_type
->type
),
2398 "ConstOffsets must be an array of size four of vectors "
2399 "of two integer components");
2401 unsigned bit_size
= glsl_get_bit_size(vec_type
->type
);
2402 for (uint32_t i
= 0; i
< 4; i
++) {
2403 const nir_const_value
*cvec
=
2404 gather_offsets
->constant
->elements
[i
]->values
[0];
2405 for (uint32_t j
= 0; j
< 2; j
++) {
2407 case 8: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i8
; break;
2408 case 16: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i16
; break;
2409 case 32: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i32
; break;
2410 case 64: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i64
; break;
2412 vtn_fail("Unsupported bit size: %u", bit_size
);
2418 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
2419 val
->ssa
->def
= &instr
->dest
.ssa
;
2421 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2425 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2426 const uint32_t *w
, nir_src
*src
)
2429 case SpvOpAtomicIIncrement
:
2430 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
2433 case SpvOpAtomicIDecrement
:
2434 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
2437 case SpvOpAtomicISub
:
2439 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
2442 case SpvOpAtomicCompareExchange
:
2443 case SpvOpAtomicCompareExchangeWeak
:
2444 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
2445 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
2448 case SpvOpAtomicExchange
:
2449 case SpvOpAtomicIAdd
:
2450 case SpvOpAtomicSMin
:
2451 case SpvOpAtomicUMin
:
2452 case SpvOpAtomicSMax
:
2453 case SpvOpAtomicUMax
:
2454 case SpvOpAtomicAnd
:
2456 case SpvOpAtomicXor
:
2457 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
2461 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2465 static nir_ssa_def
*
2466 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2468 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
2470 /* The image_load_store intrinsics assume a 4-dim coordinate */
2471 unsigned dim
= glsl_get_vector_elements(coord
->type
);
2472 unsigned swizzle
[4];
2473 for (unsigned i
= 0; i
< 4; i
++)
2474 swizzle
[i
] = MIN2(i
, dim
- 1);
2476 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
2479 static nir_ssa_def
*
2480 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2482 if (value
->num_components
== 4)
2486 for (unsigned i
= 0; i
< 4; i
++)
2487 swiz
[i
] = i
< value
->num_components
? i
: 0;
2488 return nir_swizzle(b
, value
, swiz
, 4, false);
2492 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2493 const uint32_t *w
, unsigned count
)
2495 /* Just get this one out of the way */
2496 if (opcode
== SpvOpImageTexelPointer
) {
2497 struct vtn_value
*val
=
2498 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2499 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2501 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2502 val
->image
->coord
= get_image_coord(b
, w
[4]);
2503 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
2507 struct vtn_image_pointer image
;
2510 case SpvOpAtomicExchange
:
2511 case SpvOpAtomicCompareExchange
:
2512 case SpvOpAtomicCompareExchangeWeak
:
2513 case SpvOpAtomicIIncrement
:
2514 case SpvOpAtomicIDecrement
:
2515 case SpvOpAtomicIAdd
:
2516 case SpvOpAtomicISub
:
2517 case SpvOpAtomicLoad
:
2518 case SpvOpAtomicSMin
:
2519 case SpvOpAtomicUMin
:
2520 case SpvOpAtomicSMax
:
2521 case SpvOpAtomicUMax
:
2522 case SpvOpAtomicAnd
:
2524 case SpvOpAtomicXor
:
2525 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
2528 case SpvOpAtomicStore
:
2529 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
2532 case SpvOpImageQuerySize
:
2533 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2535 image
.sample
= NULL
;
2538 case SpvOpImageRead
:
2539 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2540 image
.coord
= get_image_coord(b
, w
[4]);
2542 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
2543 vtn_assert(w
[5] == SpvImageOperandsSampleMask
);
2544 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
2546 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2550 case SpvOpImageWrite
:
2551 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2552 image
.coord
= get_image_coord(b
, w
[2]);
2556 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
2557 vtn_assert(w
[4] == SpvImageOperandsSampleMask
);
2558 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
2560 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2565 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2568 nir_intrinsic_op op
;
2570 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
2571 OP(ImageQuerySize
, size
)
2573 OP(ImageWrite
, store
)
2574 OP(AtomicLoad
, load
)
2575 OP(AtomicStore
, store
)
2576 OP(AtomicExchange
, atomic_exchange
)
2577 OP(AtomicCompareExchange
, atomic_comp_swap
)
2578 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2579 OP(AtomicIIncrement
, atomic_add
)
2580 OP(AtomicIDecrement
, atomic_add
)
2581 OP(AtomicIAdd
, atomic_add
)
2582 OP(AtomicISub
, atomic_add
)
2583 OP(AtomicSMin
, atomic_min
)
2584 OP(AtomicUMin
, atomic_min
)
2585 OP(AtomicSMax
, atomic_max
)
2586 OP(AtomicUMax
, atomic_max
)
2587 OP(AtomicAnd
, atomic_and
)
2588 OP(AtomicOr
, atomic_or
)
2589 OP(AtomicXor
, atomic_xor
)
2592 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2595 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
2597 nir_deref_instr
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
2598 intrin
->src
[0] = nir_src_for_ssa(&image_deref
->dest
.ssa
);
2600 /* ImageQuerySize doesn't take any extra parameters */
2601 if (opcode
!= SpvOpImageQuerySize
) {
2602 /* The image coordinate is always 4 components but we may not have that
2603 * many. Swizzle to compensate.
2605 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
2606 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
2610 case SpvOpAtomicLoad
:
2611 case SpvOpImageQuerySize
:
2612 case SpvOpImageRead
:
2614 case SpvOpAtomicStore
:
2615 case SpvOpImageWrite
: {
2616 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
2617 nir_ssa_def
*value
= vtn_ssa_value(b
, value_id
)->def
;
2618 /* nir_intrinsic_image_deref_store always takes a vec4 value */
2619 assert(op
== nir_intrinsic_image_deref_store
);
2620 intrin
->num_components
= 4;
2621 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
));
2625 case SpvOpAtomicCompareExchange
:
2626 case SpvOpAtomicCompareExchangeWeak
:
2627 case SpvOpAtomicIIncrement
:
2628 case SpvOpAtomicIDecrement
:
2629 case SpvOpAtomicExchange
:
2630 case SpvOpAtomicIAdd
:
2631 case SpvOpAtomicISub
:
2632 case SpvOpAtomicSMin
:
2633 case SpvOpAtomicUMin
:
2634 case SpvOpAtomicSMax
:
2635 case SpvOpAtomicUMax
:
2636 case SpvOpAtomicAnd
:
2638 case SpvOpAtomicXor
:
2639 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
2643 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2646 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
2647 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2648 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2650 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
2651 intrin
->num_components
= nir_intrinsic_infos
[op
].dest_components
;
2652 if (intrin
->num_components
== 0)
2653 intrin
->num_components
= dest_components
;
2655 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2656 intrin
->num_components
, 32, NULL
);
2658 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2660 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
2661 if (intrin
->num_components
!= dest_components
)
2662 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
2664 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
2665 val
->ssa
->def
= result
;
2667 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2671 static nir_intrinsic_op
2672 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2675 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2676 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2677 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2678 OP(AtomicExchange
, atomic_exchange
)
2679 OP(AtomicCompareExchange
, atomic_comp_swap
)
2680 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2681 OP(AtomicIIncrement
, atomic_add
)
2682 OP(AtomicIDecrement
, atomic_add
)
2683 OP(AtomicIAdd
, atomic_add
)
2684 OP(AtomicISub
, atomic_add
)
2685 OP(AtomicSMin
, atomic_imin
)
2686 OP(AtomicUMin
, atomic_umin
)
2687 OP(AtomicSMax
, atomic_imax
)
2688 OP(AtomicUMax
, atomic_umax
)
2689 OP(AtomicAnd
, atomic_and
)
2690 OP(AtomicOr
, atomic_or
)
2691 OP(AtomicXor
, atomic_xor
)
2694 vtn_fail_with_opcode("Invalid SSBO atomic", opcode
);
2698 static nir_intrinsic_op
2699 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2702 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
2703 OP(AtomicLoad
, read_deref
)
2704 OP(AtomicExchange
, exchange
)
2705 OP(AtomicCompareExchange
, comp_swap
)
2706 OP(AtomicCompareExchangeWeak
, comp_swap
)
2707 OP(AtomicIIncrement
, inc_deref
)
2708 OP(AtomicIDecrement
, post_dec_deref
)
2709 OP(AtomicIAdd
, add_deref
)
2710 OP(AtomicISub
, add_deref
)
2711 OP(AtomicUMin
, min_deref
)
2712 OP(AtomicUMax
, max_deref
)
2713 OP(AtomicAnd
, and_deref
)
2714 OP(AtomicOr
, or_deref
)
2715 OP(AtomicXor
, xor_deref
)
2718 /* We left the following out: AtomicStore, AtomicSMin and
2719 * AtomicSmax. Right now there are not nir intrinsics for them. At this
2720 * moment Atomic Counter support is needed for ARB_spirv support, so is
2721 * only need to support GLSL Atomic Counters that are uints and don't
2722 * allow direct storage.
2724 unreachable("Invalid uniform atomic");
2728 static nir_intrinsic_op
2729 get_shared_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2732 case SpvOpAtomicLoad
: return nir_intrinsic_load_shared
;
2733 case SpvOpAtomicStore
: return nir_intrinsic_store_shared
;
2734 #define OP(S, N) case SpvOp##S: return nir_intrinsic_shared_##N;
2735 OP(AtomicExchange
, atomic_exchange
)
2736 OP(AtomicCompareExchange
, atomic_comp_swap
)
2737 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2738 OP(AtomicIIncrement
, atomic_add
)
2739 OP(AtomicIDecrement
, atomic_add
)
2740 OP(AtomicIAdd
, atomic_add
)
2741 OP(AtomicISub
, atomic_add
)
2742 OP(AtomicSMin
, atomic_imin
)
2743 OP(AtomicUMin
, atomic_umin
)
2744 OP(AtomicSMax
, atomic_imax
)
2745 OP(AtomicUMax
, atomic_umax
)
2746 OP(AtomicAnd
, atomic_and
)
2747 OP(AtomicOr
, atomic_or
)
2748 OP(AtomicXor
, atomic_xor
)
2751 vtn_fail_with_opcode("Invalid shared atomic", opcode
);
2755 static nir_intrinsic_op
2756 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
2759 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
2760 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
2761 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
2762 OP(AtomicExchange
, atomic_exchange
)
2763 OP(AtomicCompareExchange
, atomic_comp_swap
)
2764 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2765 OP(AtomicIIncrement
, atomic_add
)
2766 OP(AtomicIDecrement
, atomic_add
)
2767 OP(AtomicIAdd
, atomic_add
)
2768 OP(AtomicISub
, atomic_add
)
2769 OP(AtomicSMin
, atomic_imin
)
2770 OP(AtomicUMin
, atomic_umin
)
2771 OP(AtomicSMax
, atomic_imax
)
2772 OP(AtomicUMax
, atomic_umax
)
2773 OP(AtomicAnd
, atomic_and
)
2774 OP(AtomicOr
, atomic_or
)
2775 OP(AtomicXor
, atomic_xor
)
2778 vtn_fail_with_opcode("Invalid shared atomic", opcode
);
2783 * Handles shared atomics, ssbo atomics and atomic counters.
2786 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
2787 const uint32_t *w
, unsigned count
)
2789 struct vtn_pointer
*ptr
;
2790 nir_intrinsic_instr
*atomic
;
2793 case SpvOpAtomicLoad
:
2794 case SpvOpAtomicExchange
:
2795 case SpvOpAtomicCompareExchange
:
2796 case SpvOpAtomicCompareExchangeWeak
:
2797 case SpvOpAtomicIIncrement
:
2798 case SpvOpAtomicIDecrement
:
2799 case SpvOpAtomicIAdd
:
2800 case SpvOpAtomicISub
:
2801 case SpvOpAtomicSMin
:
2802 case SpvOpAtomicUMin
:
2803 case SpvOpAtomicSMax
:
2804 case SpvOpAtomicUMax
:
2805 case SpvOpAtomicAnd
:
2807 case SpvOpAtomicXor
:
2808 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2811 case SpvOpAtomicStore
:
2812 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2816 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2820 SpvScope scope = w[4];
2821 SpvMemorySemanticsMask semantics = w[5];
2824 /* uniform as "atomic counter uniform" */
2825 if (ptr
->mode
== vtn_variable_mode_uniform
) {
2826 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2827 const struct glsl_type
*deref_type
= deref
->type
;
2828 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
2829 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2830 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2832 /* SSBO needs to initialize index/offset. In this case we don't need to,
2833 * as that info is already stored on the ptr->var->var nir_variable (see
2834 * vtn_create_variable)
2838 case SpvOpAtomicLoad
:
2839 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2842 case SpvOpAtomicStore
:
2843 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2844 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2847 case SpvOpAtomicExchange
:
2848 case SpvOpAtomicCompareExchange
:
2849 case SpvOpAtomicCompareExchangeWeak
:
2850 case SpvOpAtomicIIncrement
:
2851 case SpvOpAtomicIDecrement
:
2852 case SpvOpAtomicIAdd
:
2853 case SpvOpAtomicISub
:
2854 case SpvOpAtomicSMin
:
2855 case SpvOpAtomicUMin
:
2856 case SpvOpAtomicSMax
:
2857 case SpvOpAtomicUMax
:
2858 case SpvOpAtomicAnd
:
2860 case SpvOpAtomicXor
:
2861 /* Nothing: we don't need to call fill_common_atomic_sources here, as
2862 * atomic counter uniforms doesn't have sources
2867 unreachable("Invalid SPIR-V atomic");
2870 } else if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
2871 nir_ssa_def
*offset
, *index
;
2872 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
2874 nir_intrinsic_op op
;
2875 if (ptr
->mode
== vtn_variable_mode_ssbo
) {
2876 op
= get_ssbo_nir_atomic_op(b
, opcode
);
2878 vtn_assert(ptr
->mode
== vtn_variable_mode_workgroup
&&
2879 b
->options
->lower_workgroup_access_to_offsets
);
2880 op
= get_shared_nir_atomic_op(b
, opcode
);
2883 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2887 case SpvOpAtomicLoad
:
2888 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2889 nir_intrinsic_set_align(atomic
, 4, 0);
2890 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2891 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2892 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2895 case SpvOpAtomicStore
:
2896 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2897 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2898 nir_intrinsic_set_align(atomic
, 4, 0);
2899 atomic
->src
[src
++] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2900 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2901 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2902 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2905 case SpvOpAtomicExchange
:
2906 case SpvOpAtomicCompareExchange
:
2907 case SpvOpAtomicCompareExchangeWeak
:
2908 case SpvOpAtomicIIncrement
:
2909 case SpvOpAtomicIDecrement
:
2910 case SpvOpAtomicIAdd
:
2911 case SpvOpAtomicISub
:
2912 case SpvOpAtomicSMin
:
2913 case SpvOpAtomicUMin
:
2914 case SpvOpAtomicSMax
:
2915 case SpvOpAtomicUMax
:
2916 case SpvOpAtomicAnd
:
2918 case SpvOpAtomicXor
:
2919 if (ptr
->mode
== vtn_variable_mode_ssbo
)
2920 atomic
->src
[src
++] = nir_src_for_ssa(index
);
2921 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
2922 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
2926 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2929 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
2930 const struct glsl_type
*deref_type
= deref
->type
;
2931 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
2932 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2933 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
2936 case SpvOpAtomicLoad
:
2937 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2940 case SpvOpAtomicStore
:
2941 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2942 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2943 atomic
->src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2946 case SpvOpAtomicExchange
:
2947 case SpvOpAtomicCompareExchange
:
2948 case SpvOpAtomicCompareExchangeWeak
:
2949 case SpvOpAtomicIIncrement
:
2950 case SpvOpAtomicIDecrement
:
2951 case SpvOpAtomicIAdd
:
2952 case SpvOpAtomicISub
:
2953 case SpvOpAtomicSMin
:
2954 case SpvOpAtomicUMin
:
2955 case SpvOpAtomicSMax
:
2956 case SpvOpAtomicUMax
:
2957 case SpvOpAtomicAnd
:
2959 case SpvOpAtomicXor
:
2960 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
2964 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2968 if (opcode
!= SpvOpAtomicStore
) {
2969 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2971 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
2972 glsl_get_vector_elements(type
->type
),
2973 glsl_get_bit_size(type
->type
), NULL
);
2975 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2976 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
2977 val
->ssa
->def
= &atomic
->dest
.ssa
;
2978 val
->ssa
->type
= type
->type
;
2981 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
2984 static nir_alu_instr
*
2985 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
2988 switch (num_components
) {
2989 case 1: op
= nir_op_imov
; break;
2990 case 2: op
= nir_op_vec2
; break;
2991 case 3: op
= nir_op_vec3
; break;
2992 case 4: op
= nir_op_vec4
; break;
2993 default: vtn_fail("bad vector size: %u", num_components
);
2996 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
2997 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
2999 vec
->dest
.write_mask
= (1 << num_components
) - 1;
3004 struct vtn_ssa_value
*
3005 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
3007 if (src
->transposed
)
3008 return src
->transposed
;
3010 struct vtn_ssa_value
*dest
=
3011 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
3013 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
3014 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
3015 glsl_get_bit_size(src
->type
));
3016 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3017 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
3018 vec
->src
[0].swizzle
[0] = i
;
3020 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
3021 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
3022 vec
->src
[j
].swizzle
[0] = i
;
3025 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3026 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
3029 dest
->transposed
= src
;
3035 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
3037 return nir_channel(&b
->nb
, src
, index
);
3041 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
3044 nir_alu_instr
*vec
= create_vec(b
, src
->num_components
,
3047 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
3049 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
3051 vec
->src
[i
].src
= nir_src_for_ssa(src
);
3052 vec
->src
[i
].swizzle
[0] = i
;
3056 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3058 return &vec
->dest
.dest
.ssa
;
3061 static nir_ssa_def
*
3062 nir_ieq_imm(nir_builder
*b
, nir_ssa_def
*x
, uint64_t i
)
3064 return nir_ieq(b
, x
, nir_imm_intN_t(b
, i
, x
->bit_size
));
3068 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
3071 return nir_vector_extract(&b
->nb
, src
, nir_i2i(&b
->nb
, index
, 32));
3075 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
3076 nir_ssa_def
*insert
, nir_ssa_def
*index
)
3078 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
3079 for (unsigned i
= 1; i
< src
->num_components
; i
++)
3080 dest
= nir_bcsel(&b
->nb
, nir_ieq_imm(&b
->nb
, index
, i
),
3081 vtn_vector_insert(b
, src
, insert
, i
), dest
);
3086 static nir_ssa_def
*
3087 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
3088 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
3089 const uint32_t *indices
)
3091 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
3093 for (unsigned i
= 0; i
< num_components
; i
++) {
3094 uint32_t index
= indices
[i
];
3095 if (index
== 0xffffffff) {
3097 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
3098 } else if (index
< src0
->num_components
) {
3099 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
3100 vec
->src
[i
].swizzle
[0] = index
;
3102 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
3103 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
3107 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3109 return &vec
->dest
.dest
.ssa
;
3113 * Concatentates a number of vectors/scalars together to produce a vector
3115 static nir_ssa_def
*
3116 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
3117 unsigned num_srcs
, nir_ssa_def
**srcs
)
3119 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
3121 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3123 * "When constructing a vector, there must be at least two Constituent
3126 vtn_assert(num_srcs
>= 2);
3128 unsigned dest_idx
= 0;
3129 for (unsigned i
= 0; i
< num_srcs
; i
++) {
3130 nir_ssa_def
*src
= srcs
[i
];
3131 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
3132 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
3133 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
3134 vec
->src
[dest_idx
].swizzle
[0] = j
;
3139 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3141 * "When constructing a vector, the total number of components in all
3142 * the operands must equal the number of components in Result Type."
3144 vtn_assert(dest_idx
== num_components
);
3146 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3148 return &vec
->dest
.dest
.ssa
;
3151 static struct vtn_ssa_value
*
3152 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3154 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3155 dest
->type
= src
->type
;
3157 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3158 dest
->def
= src
->def
;
3160 unsigned elems
= glsl_get_length(src
->type
);
3162 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3163 for (unsigned i
= 0; i
< elems
; i
++)
3164 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3170 static struct vtn_ssa_value
*
3171 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3172 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3173 unsigned num_indices
)
3175 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3177 struct vtn_ssa_value
*cur
= dest
;
3179 for (i
= 0; i
< num_indices
- 1; i
++) {
3180 cur
= cur
->elems
[indices
[i
]];
3183 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3184 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3185 * the component granularity. In that case, the last index will be
3186 * the index to insert the scalar into the vector.
3189 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
3191 cur
->elems
[indices
[i
]] = insert
;
3197 static struct vtn_ssa_value
*
3198 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3199 const uint32_t *indices
, unsigned num_indices
)
3201 struct vtn_ssa_value
*cur
= src
;
3202 for (unsigned i
= 0; i
< num_indices
; i
++) {
3203 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3204 vtn_assert(i
== num_indices
- 1);
3205 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3206 * the component granularity. The last index will be the index of the
3207 * vector to extract.
3210 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
3211 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
3212 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
3215 cur
= cur
->elems
[indices
[i
]];
3223 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3224 const uint32_t *w
, unsigned count
)
3226 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
3227 const struct glsl_type
*type
=
3228 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
3229 val
->ssa
= vtn_create_ssa_value(b
, type
);
3232 case SpvOpVectorExtractDynamic
:
3233 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3234 vtn_ssa_value(b
, w
[4])->def
);
3237 case SpvOpVectorInsertDynamic
:
3238 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
3239 vtn_ssa_value(b
, w
[4])->def
,
3240 vtn_ssa_value(b
, w
[5])->def
);
3243 case SpvOpVectorShuffle
:
3244 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
3245 vtn_ssa_value(b
, w
[3])->def
,
3246 vtn_ssa_value(b
, w
[4])->def
,
3250 case SpvOpCompositeConstruct
: {
3251 unsigned elems
= count
- 3;
3253 if (glsl_type_is_vector_or_scalar(type
)) {
3254 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3255 for (unsigned i
= 0; i
< elems
; i
++)
3256 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
3258 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
3261 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3262 for (unsigned i
= 0; i
< elems
; i
++)
3263 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3267 case SpvOpCompositeExtract
:
3268 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3272 case SpvOpCompositeInsert
:
3273 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3274 vtn_ssa_value(b
, w
[3]),
3278 case SpvOpCopyObject
:
3279 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3283 vtn_fail_with_opcode("unknown composite operation", opcode
);
3288 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3290 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3291 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3295 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3296 SpvMemorySemanticsMask semantics
)
3298 static const SpvMemorySemanticsMask all_memory_semantics
=
3299 SpvMemorySemanticsUniformMemoryMask
|
3300 SpvMemorySemanticsWorkgroupMemoryMask
|
3301 SpvMemorySemanticsAtomicCounterMemoryMask
|
3302 SpvMemorySemanticsImageMemoryMask
;
3304 /* If we're not actually doing a memory barrier, bail */
3305 if (!(semantics
& all_memory_semantics
))
3308 /* GL and Vulkan don't have these */
3309 vtn_assert(scope
!= SpvScopeCrossDevice
);
3311 if (scope
== SpvScopeSubgroup
)
3312 return; /* Nothing to do here */
3314 if (scope
== SpvScopeWorkgroup
) {
3315 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3319 /* There's only two scopes thing left */
3320 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3322 if ((semantics
& all_memory_semantics
) == all_memory_semantics
) {
3323 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3327 /* Issue a bunch of more specific barriers */
3328 uint32_t bits
= semantics
;
3330 SpvMemorySemanticsMask semantic
= 1 << u_bit_scan(&bits
);
3332 case SpvMemorySemanticsUniformMemoryMask
:
3333 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3335 case SpvMemorySemanticsWorkgroupMemoryMask
:
3336 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3338 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3339 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3341 case SpvMemorySemanticsImageMemoryMask
:
3342 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3351 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3352 const uint32_t *w
, unsigned count
)
3355 case SpvOpEmitVertex
:
3356 case SpvOpEmitStreamVertex
:
3357 case SpvOpEndPrimitive
:
3358 case SpvOpEndStreamPrimitive
: {
3359 nir_intrinsic_op intrinsic_op
;
3361 case SpvOpEmitVertex
:
3362 case SpvOpEmitStreamVertex
:
3363 intrinsic_op
= nir_intrinsic_emit_vertex
;
3365 case SpvOpEndPrimitive
:
3366 case SpvOpEndStreamPrimitive
:
3367 intrinsic_op
= nir_intrinsic_end_primitive
;
3370 unreachable("Invalid opcode");
3373 nir_intrinsic_instr
*intrin
=
3374 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3377 case SpvOpEmitStreamVertex
:
3378 case SpvOpEndStreamPrimitive
: {
3379 unsigned stream
= vtn_constant_uint(b
, w
[1]);
3380 nir_intrinsic_set_stream_id(intrin
, stream
);
3388 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3392 case SpvOpMemoryBarrier
: {
3393 SpvScope scope
= vtn_constant_uint(b
, w
[1]);
3394 SpvMemorySemanticsMask semantics
= vtn_constant_uint(b
, w
[2]);
3395 vtn_emit_memory_barrier(b
, scope
, semantics
);
3399 case SpvOpControlBarrier
: {
3400 SpvScope execution_scope
= vtn_constant_uint(b
, w
[1]);
3401 if (execution_scope
== SpvScopeWorkgroup
)
3402 vtn_emit_barrier(b
, nir_intrinsic_barrier
);
3404 SpvScope memory_scope
= vtn_constant_uint(b
, w
[2]);
3405 SpvMemorySemanticsMask memory_semantics
= vtn_constant_uint(b
, w
[3]);
3406 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
3411 unreachable("unknown barrier instruction");
3416 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
3417 SpvExecutionMode mode
)
3420 case SpvExecutionModeInputPoints
:
3421 case SpvExecutionModeOutputPoints
:
3422 return 0; /* GL_POINTS */
3423 case SpvExecutionModeInputLines
:
3424 return 1; /* GL_LINES */
3425 case SpvExecutionModeInputLinesAdjacency
:
3426 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3427 case SpvExecutionModeTriangles
:
3428 return 4; /* GL_TRIANGLES */
3429 case SpvExecutionModeInputTrianglesAdjacency
:
3430 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3431 case SpvExecutionModeQuads
:
3432 return 7; /* GL_QUADS */
3433 case SpvExecutionModeIsolines
:
3434 return 0x8E7A; /* GL_ISOLINES */
3435 case SpvExecutionModeOutputLineStrip
:
3436 return 3; /* GL_LINE_STRIP */
3437 case SpvExecutionModeOutputTriangleStrip
:
3438 return 5; /* GL_TRIANGLE_STRIP */
3440 vtn_fail("Invalid primitive type: %s (%u)",
3441 spirv_executionmode_to_string(mode
), mode
);
3446 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
3447 SpvExecutionMode mode
)
3450 case SpvExecutionModeInputPoints
:
3452 case SpvExecutionModeInputLines
:
3454 case SpvExecutionModeInputLinesAdjacency
:
3456 case SpvExecutionModeTriangles
:
3458 case SpvExecutionModeInputTrianglesAdjacency
:
3461 vtn_fail("Invalid GS input mode: %s (%u)",
3462 spirv_executionmode_to_string(mode
), 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: %s (%u)",
3486 spirv_executionmodel_to_string(model
), model
);
3490 #define spv_check_supported(name, cap) do { \
3491 if (!(b->options && b->options->caps.name)) \
3492 vtn_warn("Unsupported SPIR-V capability: %s (%u)", \
3493 spirv_capability_to_string(cap), cap); \
3498 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
3501 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
3502 /* Let this be a name label regardless */
3503 unsigned name_words
;
3504 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
3506 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
3507 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
3510 vtn_assert(b
->entry_point
== NULL
);
3511 b
->entry_point
= entry_point
;
3515 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3516 const uint32_t *w
, unsigned count
)
3523 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
3524 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
3525 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
3526 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
3527 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
3528 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
3531 uint32_t version
= w
[2];
3534 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
3536 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
3540 case SpvOpSourceExtension
:
3541 case SpvOpSourceContinued
:
3542 case SpvOpExtension
:
3543 case SpvOpModuleProcessed
:
3544 /* Unhandled, but these are for debug so that's ok. */
3547 case SpvOpCapability
: {
3548 SpvCapability cap
= w
[1];
3550 case SpvCapabilityMatrix
:
3551 case SpvCapabilityShader
:
3552 case SpvCapabilityGeometry
:
3553 case SpvCapabilityGeometryPointSize
:
3554 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
3555 case SpvCapabilitySampledImageArrayDynamicIndexing
:
3556 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
3557 case SpvCapabilityStorageImageArrayDynamicIndexing
:
3558 case SpvCapabilityImageRect
:
3559 case SpvCapabilitySampledRect
:
3560 case SpvCapabilitySampled1D
:
3561 case SpvCapabilityImage1D
:
3562 case SpvCapabilitySampledCubeArray
:
3563 case SpvCapabilityImageCubeArray
:
3564 case SpvCapabilitySampledBuffer
:
3565 case SpvCapabilityImageBuffer
:
3566 case SpvCapabilityImageQuery
:
3567 case SpvCapabilityDerivativeControl
:
3568 case SpvCapabilityInterpolationFunction
:
3569 case SpvCapabilityMultiViewport
:
3570 case SpvCapabilitySampleRateShading
:
3571 case SpvCapabilityClipDistance
:
3572 case SpvCapabilityCullDistance
:
3573 case SpvCapabilityInputAttachment
:
3574 case SpvCapabilityImageGatherExtended
:
3575 case SpvCapabilityStorageImageExtendedFormats
:
3578 case SpvCapabilityLinkage
:
3579 case SpvCapabilityVector16
:
3580 case SpvCapabilityFloat16Buffer
:
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
);
3603 case SpvCapabilityInt8
:
3604 spv_check_supported(int8
, cap
);
3607 case SpvCapabilityTransformFeedback
:
3608 spv_check_supported(transform_feedback
, cap
);
3611 case SpvCapabilityGeometryStreams
:
3612 spv_check_supported(geometry_streams
, cap
);
3615 case SpvCapabilityInt64Atomics
:
3616 spv_check_supported(int64_atomics
, cap
);
3619 case SpvCapabilityStorageImageMultisample
:
3620 spv_check_supported(storage_image_ms
, cap
);
3623 case SpvCapabilityAddresses
:
3624 spv_check_supported(address
, cap
);
3627 case SpvCapabilityKernel
:
3628 spv_check_supported(kernel
, cap
);
3631 case SpvCapabilityImageBasic
:
3632 case SpvCapabilityImageReadWrite
:
3633 case SpvCapabilityImageMipmap
:
3634 case SpvCapabilityPipes
:
3635 case SpvCapabilityGroups
:
3636 case SpvCapabilityDeviceEnqueue
:
3637 case SpvCapabilityLiteralSampler
:
3638 case SpvCapabilityGenericPointer
:
3639 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
3640 spirv_capability_to_string(cap
));
3643 case SpvCapabilityImageMSArray
:
3644 spv_check_supported(image_ms_array
, cap
);
3647 case SpvCapabilityTessellation
:
3648 case SpvCapabilityTessellationPointSize
:
3649 spv_check_supported(tessellation
, cap
);
3652 case SpvCapabilityDrawParameters
:
3653 spv_check_supported(draw_parameters
, cap
);
3656 case SpvCapabilityStorageImageReadWithoutFormat
:
3657 spv_check_supported(image_read_without_format
, cap
);
3660 case SpvCapabilityStorageImageWriteWithoutFormat
:
3661 spv_check_supported(image_write_without_format
, cap
);
3664 case SpvCapabilityDeviceGroup
:
3665 spv_check_supported(device_group
, cap
);
3668 case SpvCapabilityMultiView
:
3669 spv_check_supported(multiview
, cap
);
3672 case SpvCapabilityGroupNonUniform
:
3673 spv_check_supported(subgroup_basic
, cap
);
3676 case SpvCapabilityGroupNonUniformVote
:
3677 spv_check_supported(subgroup_vote
, cap
);
3680 case SpvCapabilitySubgroupBallotKHR
:
3681 case SpvCapabilityGroupNonUniformBallot
:
3682 spv_check_supported(subgroup_ballot
, cap
);
3685 case SpvCapabilityGroupNonUniformShuffle
:
3686 case SpvCapabilityGroupNonUniformShuffleRelative
:
3687 spv_check_supported(subgroup_shuffle
, cap
);
3690 case SpvCapabilityGroupNonUniformQuad
:
3691 spv_check_supported(subgroup_quad
, cap
);
3694 case SpvCapabilityGroupNonUniformArithmetic
:
3695 case SpvCapabilityGroupNonUniformClustered
:
3696 spv_check_supported(subgroup_arithmetic
, cap
);
3699 case SpvCapabilityVariablePointersStorageBuffer
:
3700 case SpvCapabilityVariablePointers
:
3701 spv_check_supported(variable_pointers
, cap
);
3702 b
->variable_pointers
= true;
3705 case SpvCapabilityStorageUniformBufferBlock16
:
3706 case SpvCapabilityStorageUniform16
:
3707 case SpvCapabilityStoragePushConstant16
:
3708 case SpvCapabilityStorageInputOutput16
:
3709 spv_check_supported(storage_16bit
, cap
);
3712 case SpvCapabilityShaderViewportIndexLayerEXT
:
3713 spv_check_supported(shader_viewport_index_layer
, cap
);
3716 case SpvCapabilityStorageBuffer8BitAccess
:
3717 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
3718 case SpvCapabilityStoragePushConstant8
:
3719 spv_check_supported(storage_8bit
, cap
);
3722 case SpvCapabilityShaderNonUniformEXT
:
3723 spv_check_supported(descriptor_indexing
, cap
);
3726 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
3727 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
3728 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
3729 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
3732 case SpvCapabilityUniformBufferArrayNonUniformIndexingEXT
:
3733 case SpvCapabilitySampledImageArrayNonUniformIndexingEXT
:
3734 case SpvCapabilityStorageBufferArrayNonUniformIndexingEXT
:
3735 case SpvCapabilityStorageImageArrayNonUniformIndexingEXT
:
3736 case SpvCapabilityInputAttachmentArrayNonUniformIndexingEXT
:
3737 case SpvCapabilityUniformTexelBufferArrayNonUniformIndexingEXT
:
3738 case SpvCapabilityStorageTexelBufferArrayNonUniformIndexingEXT
:
3739 spv_check_supported(descriptor_array_non_uniform_indexing
, cap
);
3742 case SpvCapabilityRuntimeDescriptorArrayEXT
:
3743 spv_check_supported(runtime_descriptor_array
, cap
);
3746 case SpvCapabilityStencilExportEXT
:
3747 spv_check_supported(stencil_export
, cap
);
3750 case SpvCapabilitySampleMaskPostDepthCoverage
:
3751 spv_check_supported(post_depth_coverage
, cap
);
3754 case SpvCapabilityPhysicalStorageBufferAddressesEXT
:
3755 spv_check_supported(physical_storage_buffer_address
, cap
);
3758 case SpvCapabilityComputeDerivativeGroupQuadsNV
:
3759 case SpvCapabilityComputeDerivativeGroupLinearNV
:
3760 spv_check_supported(derivative_group
, cap
);
3763 case SpvCapabilityFloat16
:
3764 spv_check_supported(float16
, cap
);
3768 vtn_fail("Unhandled capability: %s (%u)",
3769 spirv_capability_to_string(cap
), cap
);
3774 case SpvOpExtInstImport
:
3775 vtn_handle_extension(b
, opcode
, w
, count
);
3778 case SpvOpMemoryModel
:
3780 case SpvAddressingModelPhysical32
:
3781 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
3782 "AddressingModelPhysical32 only supported for kernels");
3783 b
->shader
->info
.cs
.ptr_size
= 32;
3784 b
->physical_ptrs
= true;
3785 b
->options
->shared_ptr_type
= glsl_uint_type();
3786 b
->options
->global_ptr_type
= glsl_uint_type();
3787 b
->options
->temp_ptr_type
= glsl_uint_type();
3789 case SpvAddressingModelPhysical64
:
3790 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
3791 "AddressingModelPhysical64 only supported for kernels");
3792 b
->shader
->info
.cs
.ptr_size
= 64;
3793 b
->physical_ptrs
= true;
3794 b
->options
->shared_ptr_type
= glsl_uint64_t_type();
3795 b
->options
->global_ptr_type
= glsl_uint64_t_type();
3796 b
->options
->temp_ptr_type
= glsl_uint64_t_type();
3798 case SpvAddressingModelLogical
:
3799 vtn_fail_if(b
->shader
->info
.stage
>= MESA_SHADER_STAGES
,
3800 "AddressingModelLogical only supported for shaders");
3801 b
->shader
->info
.cs
.ptr_size
= 0;
3802 b
->physical_ptrs
= false;
3804 case SpvAddressingModelPhysicalStorageBuffer64EXT
:
3805 vtn_fail_if(!b
->options
||
3806 !b
->options
->caps
.physical_storage_buffer_address
,
3807 "AddressingModelPhysicalStorageBuffer64EXT not supported");
3810 vtn_fail("Unknown addressing model: %s (%u)",
3811 spirv_addressingmodel_to_string(w
[1]), w
[1]);
3815 vtn_assert(w
[2] == SpvMemoryModelSimple
||
3816 w
[2] == SpvMemoryModelGLSL450
||
3817 w
[2] == SpvMemoryModelOpenCL
);
3820 case SpvOpEntryPoint
:
3821 vtn_handle_entry_point(b
, w
, count
);
3825 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
3826 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3830 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
3833 case SpvOpMemberName
:
3837 case SpvOpExecutionMode
:
3838 case SpvOpExecutionModeId
:
3839 case SpvOpDecorationGroup
:
3841 case SpvOpDecorateId
:
3842 case SpvOpMemberDecorate
:
3843 case SpvOpGroupDecorate
:
3844 case SpvOpGroupMemberDecorate
:
3845 case SpvOpDecorateStringGOOGLE
:
3846 case SpvOpMemberDecorateStringGOOGLE
:
3847 vtn_handle_decoration(b
, opcode
, w
, count
);
3851 return false; /* End of preamble */
3858 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
3859 const struct vtn_decoration
*mode
, void *data
)
3861 vtn_assert(b
->entry_point
== entry_point
);
3863 switch(mode
->exec_mode
) {
3864 case SpvExecutionModeOriginUpperLeft
:
3865 case SpvExecutionModeOriginLowerLeft
:
3866 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3867 b
->shader
->info
.fs
.origin_upper_left
=
3868 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
3871 case SpvExecutionModeEarlyFragmentTests
:
3872 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3873 b
->shader
->info
.fs
.early_fragment_tests
= true;
3876 case SpvExecutionModePostDepthCoverage
:
3877 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3878 b
->shader
->info
.fs
.post_depth_coverage
= true;
3881 case SpvExecutionModeInvocations
:
3882 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3883 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->operands
[0]);
3886 case SpvExecutionModeDepthReplacing
:
3887 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3888 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
3890 case SpvExecutionModeDepthGreater
:
3891 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3892 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
3894 case SpvExecutionModeDepthLess
:
3895 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3896 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
3898 case SpvExecutionModeDepthUnchanged
:
3899 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3900 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
3903 case SpvExecutionModeLocalSize
:
3904 vtn_assert(gl_shader_stage_is_compute(b
->shader
->info
.stage
));
3905 b
->shader
->info
.cs
.local_size
[0] = mode
->operands
[0];
3906 b
->shader
->info
.cs
.local_size
[1] = mode
->operands
[1];
3907 b
->shader
->info
.cs
.local_size
[2] = mode
->operands
[2];
3910 case SpvExecutionModeLocalSizeId
:
3911 b
->shader
->info
.cs
.local_size
[0] = vtn_constant_uint(b
, mode
->operands
[0]);
3912 b
->shader
->info
.cs
.local_size
[1] = vtn_constant_uint(b
, mode
->operands
[1]);
3913 b
->shader
->info
.cs
.local_size
[2] = vtn_constant_uint(b
, mode
->operands
[2]);
3916 case SpvExecutionModeLocalSizeHint
:
3917 case SpvExecutionModeLocalSizeHintId
:
3918 break; /* Nothing to do with this */
3920 case SpvExecutionModeOutputVertices
:
3921 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3922 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3923 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->operands
[0];
3925 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3926 b
->shader
->info
.gs
.vertices_out
= mode
->operands
[0];
3930 case SpvExecutionModeInputPoints
:
3931 case SpvExecutionModeInputLines
:
3932 case SpvExecutionModeInputLinesAdjacency
:
3933 case SpvExecutionModeTriangles
:
3934 case SpvExecutionModeInputTrianglesAdjacency
:
3935 case SpvExecutionModeQuads
:
3936 case SpvExecutionModeIsolines
:
3937 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3938 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3939 b
->shader
->info
.tess
.primitive_mode
=
3940 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3942 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3943 b
->shader
->info
.gs
.vertices_in
=
3944 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
3945 b
->shader
->info
.gs
.input_primitive
=
3946 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3950 case SpvExecutionModeOutputPoints
:
3951 case SpvExecutionModeOutputLineStrip
:
3952 case SpvExecutionModeOutputTriangleStrip
:
3953 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3954 b
->shader
->info
.gs
.output_primitive
=
3955 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
3958 case SpvExecutionModeSpacingEqual
:
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
.spacing
= TESS_SPACING_EQUAL
;
3963 case SpvExecutionModeSpacingFractionalEven
:
3964 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3965 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3966 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
3968 case SpvExecutionModeSpacingFractionalOdd
:
3969 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3970 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3971 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
3973 case SpvExecutionModeVertexOrderCw
:
3974 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3975 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3976 b
->shader
->info
.tess
.ccw
= false;
3978 case SpvExecutionModeVertexOrderCcw
:
3979 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3980 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3981 b
->shader
->info
.tess
.ccw
= true;
3983 case SpvExecutionModePointMode
:
3984 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3985 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3986 b
->shader
->info
.tess
.point_mode
= true;
3989 case SpvExecutionModePixelCenterInteger
:
3990 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
3991 b
->shader
->info
.fs
.pixel_center_integer
= true;
3994 case SpvExecutionModeXfb
:
3995 b
->shader
->info
.has_transform_feedback_varyings
= true;
3998 case SpvExecutionModeVecTypeHint
:
4001 case SpvExecutionModeContractionOff
:
4002 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
4003 vtn_warn("ExectionMode only allowed for CL-style kernels: %s",
4004 spirv_executionmode_to_string(mode
->exec_mode
));
4009 case SpvExecutionModeStencilRefReplacingEXT
:
4010 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4013 case SpvExecutionModeDerivativeGroupQuadsNV
:
4014 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4015 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_QUADS
;
4018 case SpvExecutionModeDerivativeGroupLinearNV
:
4019 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4020 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_LINEAR
;
4024 vtn_fail("Unhandled execution mode: %s (%u)",
4025 spirv_executionmode_to_string(mode
->exec_mode
),
4031 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4032 const uint32_t *w
, unsigned count
)
4034 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
4038 case SpvOpSourceContinued
:
4039 case SpvOpSourceExtension
:
4040 case SpvOpExtension
:
4041 case SpvOpCapability
:
4042 case SpvOpExtInstImport
:
4043 case SpvOpMemoryModel
:
4044 case SpvOpEntryPoint
:
4045 case SpvOpExecutionMode
:
4048 case SpvOpMemberName
:
4049 case SpvOpDecorationGroup
:
4051 case SpvOpDecorateId
:
4052 case SpvOpMemberDecorate
:
4053 case SpvOpGroupDecorate
:
4054 case SpvOpGroupMemberDecorate
:
4055 case SpvOpDecorateStringGOOGLE
:
4056 case SpvOpMemberDecorateStringGOOGLE
:
4057 vtn_fail("Invalid opcode types and variables section");
4063 case SpvOpTypeFloat
:
4064 case SpvOpTypeVector
:
4065 case SpvOpTypeMatrix
:
4066 case SpvOpTypeImage
:
4067 case SpvOpTypeSampler
:
4068 case SpvOpTypeSampledImage
:
4069 case SpvOpTypeArray
:
4070 case SpvOpTypeRuntimeArray
:
4071 case SpvOpTypeStruct
:
4072 case SpvOpTypeOpaque
:
4073 case SpvOpTypePointer
:
4074 case SpvOpTypeForwardPointer
:
4075 case SpvOpTypeFunction
:
4076 case SpvOpTypeEvent
:
4077 case SpvOpTypeDeviceEvent
:
4078 case SpvOpTypeReserveId
:
4079 case SpvOpTypeQueue
:
4081 vtn_handle_type(b
, opcode
, w
, count
);
4084 case SpvOpConstantTrue
:
4085 case SpvOpConstantFalse
:
4087 case SpvOpConstantComposite
:
4088 case SpvOpConstantSampler
:
4089 case SpvOpConstantNull
:
4090 case SpvOpSpecConstantTrue
:
4091 case SpvOpSpecConstantFalse
:
4092 case SpvOpSpecConstant
:
4093 case SpvOpSpecConstantComposite
:
4094 case SpvOpSpecConstantOp
:
4095 vtn_handle_constant(b
, opcode
, w
, count
);
4100 vtn_handle_variables(b
, opcode
, w
, count
);
4104 return false; /* End of preamble */
4111 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4112 const uint32_t *w
, unsigned count
)
4118 case SpvOpLoopMerge
:
4119 case SpvOpSelectionMerge
:
4120 /* This is handled by cfg pre-pass and walk_blocks */
4124 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
4125 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4130 vtn_handle_extension(b
, opcode
, w
, count
);
4136 case SpvOpCopyMemory
:
4137 case SpvOpCopyMemorySized
:
4138 case SpvOpAccessChain
:
4139 case SpvOpPtrAccessChain
:
4140 case SpvOpInBoundsAccessChain
:
4141 case SpvOpInBoundsPtrAccessChain
:
4142 case SpvOpArrayLength
:
4143 case SpvOpConvertPtrToU
:
4144 case SpvOpConvertUToPtr
:
4145 vtn_handle_variables(b
, opcode
, w
, count
);
4148 case SpvOpFunctionCall
:
4149 vtn_handle_function_call(b
, opcode
, w
, count
);
4152 case SpvOpSampledImage
:
4154 case SpvOpImageSampleImplicitLod
:
4155 case SpvOpImageSampleExplicitLod
:
4156 case SpvOpImageSampleDrefImplicitLod
:
4157 case SpvOpImageSampleDrefExplicitLod
:
4158 case SpvOpImageSampleProjImplicitLod
:
4159 case SpvOpImageSampleProjExplicitLod
:
4160 case SpvOpImageSampleProjDrefImplicitLod
:
4161 case SpvOpImageSampleProjDrefExplicitLod
:
4162 case SpvOpImageFetch
:
4163 case SpvOpImageGather
:
4164 case SpvOpImageDrefGather
:
4165 case SpvOpImageQuerySizeLod
:
4166 case SpvOpImageQueryLod
:
4167 case SpvOpImageQueryLevels
:
4168 case SpvOpImageQuerySamples
:
4169 vtn_handle_texture(b
, opcode
, w
, count
);
4172 case SpvOpImageRead
:
4173 case SpvOpImageWrite
:
4174 case SpvOpImageTexelPointer
:
4175 vtn_handle_image(b
, opcode
, w
, count
);
4178 case SpvOpImageQuerySize
: {
4179 struct vtn_pointer
*image
=
4180 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
4181 if (glsl_type_is_image(image
->type
->type
)) {
4182 vtn_handle_image(b
, opcode
, w
, count
);
4184 vtn_assert(glsl_type_is_sampler(image
->type
->type
));
4185 vtn_handle_texture(b
, opcode
, w
, count
);
4190 case SpvOpAtomicLoad
:
4191 case SpvOpAtomicExchange
:
4192 case SpvOpAtomicCompareExchange
:
4193 case SpvOpAtomicCompareExchangeWeak
:
4194 case SpvOpAtomicIIncrement
:
4195 case SpvOpAtomicIDecrement
:
4196 case SpvOpAtomicIAdd
:
4197 case SpvOpAtomicISub
:
4198 case SpvOpAtomicSMin
:
4199 case SpvOpAtomicUMin
:
4200 case SpvOpAtomicSMax
:
4201 case SpvOpAtomicUMax
:
4202 case SpvOpAtomicAnd
:
4204 case SpvOpAtomicXor
: {
4205 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
4206 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4207 vtn_handle_image(b
, opcode
, w
, count
);
4209 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4210 vtn_handle_atomics(b
, opcode
, w
, count
);
4215 case SpvOpAtomicStore
: {
4216 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
4217 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4218 vtn_handle_image(b
, opcode
, w
, count
);
4220 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4221 vtn_handle_atomics(b
, opcode
, w
, count
);
4227 /* Handle OpSelect up-front here because it needs to be able to handle
4228 * pointers and not just regular vectors and scalars.
4230 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
4231 struct vtn_value
*sel_val
= vtn_untyped_value(b
, w
[3]);
4232 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
4233 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
4235 const struct glsl_type
*sel_type
;
4236 switch (res_val
->type
->base_type
) {
4237 case vtn_base_type_scalar
:
4238 sel_type
= glsl_bool_type();
4240 case vtn_base_type_vector
:
4241 sel_type
= glsl_vector_type(GLSL_TYPE_BOOL
, res_val
->type
->length
);
4243 case vtn_base_type_pointer
:
4244 /* We need to have actual storage for pointer types */
4245 vtn_fail_if(res_val
->type
->type
== NULL
,
4246 "Invalid pointer result type for OpSelect");
4247 sel_type
= glsl_bool_type();
4250 vtn_fail("Result type of OpSelect must be a scalar, vector, or pointer");
4253 if (unlikely(sel_val
->type
->type
!= sel_type
)) {
4254 if (sel_val
->type
->type
== glsl_bool_type()) {
4255 /* This case is illegal but some older versions of GLSLang produce
4256 * it. The GLSLang issue was fixed on March 30, 2017:
4258 * https://github.com/KhronosGroup/glslang/issues/809
4260 * Unfortunately, there are applications in the wild which are
4261 * shipping with this bug so it isn't nice to fail on them so we
4262 * throw a warning instead. It's not actually a problem for us as
4263 * nir_builder will just splat the condition out which is most
4264 * likely what the client wanted anyway.
4266 vtn_warn("Condition type of OpSelect must have the same number "
4267 "of components as Result Type");
4269 vtn_fail("Condition type of OpSelect must be a scalar or vector "
4270 "of Boolean type. It must have the same number of "
4271 "components as Result Type");
4275 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4276 obj2_val
->type
!= res_val
->type
,
4277 "Object types must match the result type in OpSelect");
4279 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
4280 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, res_type
->type
);
4281 ssa
->def
= nir_bcsel(&b
->nb
, vtn_ssa_value(b
, w
[3])->def
,
4282 vtn_ssa_value(b
, w
[4])->def
,
4283 vtn_ssa_value(b
, w
[5])->def
);
4284 vtn_push_ssa(b
, w
[2], res_type
, ssa
);
4293 case SpvOpConvertFToU
:
4294 case SpvOpConvertFToS
:
4295 case SpvOpConvertSToF
:
4296 case SpvOpConvertUToF
:
4300 case SpvOpQuantizeToF16
:
4301 case SpvOpPtrCastToGeneric
:
4302 case SpvOpGenericCastToPtr
:
4308 case SpvOpSignBitSet
:
4309 case SpvOpLessOrGreater
:
4311 case SpvOpUnordered
:
4326 case SpvOpVectorTimesScalar
:
4328 case SpvOpIAddCarry
:
4329 case SpvOpISubBorrow
:
4330 case SpvOpUMulExtended
:
4331 case SpvOpSMulExtended
:
4332 case SpvOpShiftRightLogical
:
4333 case SpvOpShiftRightArithmetic
:
4334 case SpvOpShiftLeftLogical
:
4335 case SpvOpLogicalEqual
:
4336 case SpvOpLogicalNotEqual
:
4337 case SpvOpLogicalOr
:
4338 case SpvOpLogicalAnd
:
4339 case SpvOpLogicalNot
:
4340 case SpvOpBitwiseOr
:
4341 case SpvOpBitwiseXor
:
4342 case SpvOpBitwiseAnd
:
4344 case SpvOpFOrdEqual
:
4345 case SpvOpFUnordEqual
:
4346 case SpvOpINotEqual
:
4347 case SpvOpFOrdNotEqual
:
4348 case SpvOpFUnordNotEqual
:
4349 case SpvOpULessThan
:
4350 case SpvOpSLessThan
:
4351 case SpvOpFOrdLessThan
:
4352 case SpvOpFUnordLessThan
:
4353 case SpvOpUGreaterThan
:
4354 case SpvOpSGreaterThan
:
4355 case SpvOpFOrdGreaterThan
:
4356 case SpvOpFUnordGreaterThan
:
4357 case SpvOpULessThanEqual
:
4358 case SpvOpSLessThanEqual
:
4359 case SpvOpFOrdLessThanEqual
:
4360 case SpvOpFUnordLessThanEqual
:
4361 case SpvOpUGreaterThanEqual
:
4362 case SpvOpSGreaterThanEqual
:
4363 case SpvOpFOrdGreaterThanEqual
:
4364 case SpvOpFUnordGreaterThanEqual
:
4370 case SpvOpFwidthFine
:
4371 case SpvOpDPdxCoarse
:
4372 case SpvOpDPdyCoarse
:
4373 case SpvOpFwidthCoarse
:
4374 case SpvOpBitFieldInsert
:
4375 case SpvOpBitFieldSExtract
:
4376 case SpvOpBitFieldUExtract
:
4377 case SpvOpBitReverse
:
4379 case SpvOpTranspose
:
4380 case SpvOpOuterProduct
:
4381 case SpvOpMatrixTimesScalar
:
4382 case SpvOpVectorTimesMatrix
:
4383 case SpvOpMatrixTimesVector
:
4384 case SpvOpMatrixTimesMatrix
:
4385 vtn_handle_alu(b
, opcode
, w
, count
);
4388 case SpvOpVectorExtractDynamic
:
4389 case SpvOpVectorInsertDynamic
:
4390 case SpvOpVectorShuffle
:
4391 case SpvOpCompositeConstruct
:
4392 case SpvOpCompositeExtract
:
4393 case SpvOpCompositeInsert
:
4394 case SpvOpCopyObject
:
4395 vtn_handle_composite(b
, opcode
, w
, count
);
4398 case SpvOpEmitVertex
:
4399 case SpvOpEndPrimitive
:
4400 case SpvOpEmitStreamVertex
:
4401 case SpvOpEndStreamPrimitive
:
4402 case SpvOpControlBarrier
:
4403 case SpvOpMemoryBarrier
:
4404 vtn_handle_barrier(b
, opcode
, w
, count
);
4407 case SpvOpGroupNonUniformElect
:
4408 case SpvOpGroupNonUniformAll
:
4409 case SpvOpGroupNonUniformAny
:
4410 case SpvOpGroupNonUniformAllEqual
:
4411 case SpvOpGroupNonUniformBroadcast
:
4412 case SpvOpGroupNonUniformBroadcastFirst
:
4413 case SpvOpGroupNonUniformBallot
:
4414 case SpvOpGroupNonUniformInverseBallot
:
4415 case SpvOpGroupNonUniformBallotBitExtract
:
4416 case SpvOpGroupNonUniformBallotBitCount
:
4417 case SpvOpGroupNonUniformBallotFindLSB
:
4418 case SpvOpGroupNonUniformBallotFindMSB
:
4419 case SpvOpGroupNonUniformShuffle
:
4420 case SpvOpGroupNonUniformShuffleXor
:
4421 case SpvOpGroupNonUniformShuffleUp
:
4422 case SpvOpGroupNonUniformShuffleDown
:
4423 case SpvOpGroupNonUniformIAdd
:
4424 case SpvOpGroupNonUniformFAdd
:
4425 case SpvOpGroupNonUniformIMul
:
4426 case SpvOpGroupNonUniformFMul
:
4427 case SpvOpGroupNonUniformSMin
:
4428 case SpvOpGroupNonUniformUMin
:
4429 case SpvOpGroupNonUniformFMin
:
4430 case SpvOpGroupNonUniformSMax
:
4431 case SpvOpGroupNonUniformUMax
:
4432 case SpvOpGroupNonUniformFMax
:
4433 case SpvOpGroupNonUniformBitwiseAnd
:
4434 case SpvOpGroupNonUniformBitwiseOr
:
4435 case SpvOpGroupNonUniformBitwiseXor
:
4436 case SpvOpGroupNonUniformLogicalAnd
:
4437 case SpvOpGroupNonUniformLogicalOr
:
4438 case SpvOpGroupNonUniformLogicalXor
:
4439 case SpvOpGroupNonUniformQuadBroadcast
:
4440 case SpvOpGroupNonUniformQuadSwap
:
4441 vtn_handle_subgroup(b
, opcode
, w
, count
);
4445 vtn_fail_with_opcode("Unhandled opcode", opcode
);
4452 vtn_create_builder(const uint32_t *words
, size_t word_count
,
4453 gl_shader_stage stage
, const char *entry_point_name
,
4454 const struct spirv_to_nir_options
*options
)
4456 /* Initialize the vtn_builder object */
4457 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
4458 struct spirv_to_nir_options
*dup_options
=
4459 ralloc(b
, struct spirv_to_nir_options
);
4460 *dup_options
= *options
;
4463 b
->spirv_word_count
= word_count
;
4467 exec_list_make_empty(&b
->functions
);
4468 b
->entry_point_stage
= stage
;
4469 b
->entry_point_name
= entry_point_name
;
4470 b
->options
= dup_options
;
4473 * Handle the SPIR-V header (first 5 dwords).
4474 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
4476 if (word_count
<= 5)
4479 if (words
[0] != SpvMagicNumber
) {
4480 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
4483 if (words
[1] < 0x10000) {
4484 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
4488 uint16_t generator_id
= words
[2] >> 16;
4489 uint16_t generator_version
= words
[2];
4491 /* The first GLSLang version bump actually 1.5 years after #179 was fixed
4492 * but this should at least let us shut the workaround off for modern
4493 * versions of GLSLang.
4495 b
->wa_glslang_179
= (generator_id
== 8 && generator_version
== 1);
4497 /* words[2] == generator magic */
4498 unsigned value_id_bound
= words
[3];
4499 if (words
[4] != 0) {
4500 vtn_err("words[4] was %u, want 0", words
[4]);
4504 b
->value_id_bound
= value_id_bound
;
4505 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
4513 static nir_function
*
4514 vtn_emit_kernel_entry_point_wrapper(struct vtn_builder
*b
,
4515 nir_function
*entry_point
)
4517 vtn_assert(entry_point
== b
->entry_point
->func
->impl
->function
);
4518 vtn_fail_if(!entry_point
->name
, "entry points are required to have a name");
4519 const char *func_name
=
4520 ralloc_asprintf(b
->shader
, "__wrapped_%s", entry_point
->name
);
4522 /* we shouldn't have any inputs yet */
4523 vtn_assert(!entry_point
->shader
->num_inputs
);
4524 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
);
4526 nir_function
*main_entry_point
= nir_function_create(b
->shader
, func_name
);
4527 main_entry_point
->impl
= nir_function_impl_create(main_entry_point
);
4528 nir_builder_init(&b
->nb
, main_entry_point
->impl
);
4529 b
->nb
.cursor
= nir_after_cf_list(&main_entry_point
->impl
->body
);
4530 b
->func_param_idx
= 0;
4532 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, entry_point
);
4534 for (unsigned i
= 0; i
< entry_point
->num_params
; ++i
) {
4535 struct vtn_type
*param_type
= b
->entry_point
->func
->type
->params
[i
];
4537 /* consider all pointers to function memory to be parameters passed
4540 bool is_by_val
= param_type
->base_type
== vtn_base_type_pointer
&&
4541 param_type
->storage_class
== SpvStorageClassFunction
;
4543 /* input variable */
4544 nir_variable
*in_var
= rzalloc(b
->nb
.shader
, nir_variable
);
4545 in_var
->data
.mode
= nir_var_shader_in
;
4546 in_var
->data
.read_only
= true;
4547 in_var
->data
.location
= i
;
4550 in_var
->type
= param_type
->deref
->type
;
4552 in_var
->type
= param_type
->type
;
4554 nir_shader_add_variable(b
->nb
.shader
, in_var
);
4555 b
->nb
.shader
->num_inputs
++;
4557 /* we have to copy the entire variable into function memory */
4559 nir_variable
*copy_var
=
4560 nir_local_variable_create(main_entry_point
->impl
, in_var
->type
,
4562 nir_copy_var(&b
->nb
, copy_var
, in_var
);
4564 nir_src_for_ssa(&nir_build_deref_var(&b
->nb
, copy_var
)->dest
.ssa
);
4566 call
->params
[i
] = nir_src_for_ssa(nir_load_var(&b
->nb
, in_var
));
4570 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
4572 return main_entry_point
;
4576 spirv_to_nir(const uint32_t *words
, size_t word_count
,
4577 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
4578 gl_shader_stage stage
, const char *entry_point_name
,
4579 const struct spirv_to_nir_options
*options
,
4580 const nir_shader_compiler_options
*nir_options
)
4583 const uint32_t *word_end
= words
+ word_count
;
4585 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
4586 stage
, entry_point_name
,
4592 /* See also _vtn_fail() */
4593 if (setjmp(b
->fail_jump
)) {
4598 /* Skip the SPIR-V header, handled at vtn_create_builder */
4601 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
4603 /* Handle all the preamble instructions */
4604 words
= vtn_foreach_instruction(b
, words
, word_end
,
4605 vtn_handle_preamble_instruction
);
4607 if (b
->entry_point
== NULL
) {
4608 vtn_fail("Entry point not found");
4613 /* Set shader info defaults */
4614 b
->shader
->info
.gs
.invocations
= 1;
4616 b
->specializations
= spec
;
4617 b
->num_specializations
= num_spec
;
4619 /* Handle all variable, type, and constant instructions */
4620 words
= vtn_foreach_instruction(b
, words
, word_end
,
4621 vtn_handle_variable_or_type_instruction
);
4623 /* Parse execution modes */
4624 vtn_foreach_execution_mode(b
, b
->entry_point
,
4625 vtn_handle_execution_mode
, NULL
);
4627 if (b
->workgroup_size_builtin
) {
4628 vtn_assert(b
->workgroup_size_builtin
->type
->type
==
4629 glsl_vector_type(GLSL_TYPE_UINT
, 3));
4631 nir_const_value
*const_size
=
4632 b
->workgroup_size_builtin
->constant
->values
[0];
4634 b
->shader
->info
.cs
.local_size
[0] = const_size
[0].u32
;
4635 b
->shader
->info
.cs
.local_size
[1] = const_size
[1].u32
;
4636 b
->shader
->info
.cs
.local_size
[2] = const_size
[2].u32
;
4639 /* Set types on all vtn_values */
4640 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
4642 vtn_build_cfg(b
, words
, word_end
);
4644 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4645 b
->entry_point
->func
->referenced
= true;
4650 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
4651 if (func
->referenced
&& !func
->emitted
) {
4652 b
->const_table
= _mesa_pointer_hash_table_create(b
);
4654 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
4660 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
4661 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
4662 vtn_assert(entry_point
);
4664 /* post process entry_points with input params */
4665 if (entry_point
->num_params
&& b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
4666 entry_point
= vtn_emit_kernel_entry_point_wrapper(b
, entry_point
);
4668 entry_point
->is_entrypoint
= true;
4670 /* When multiple shader stages exist in the same SPIR-V module, we
4671 * generate input and output variables for every stage, in the same
4672 * NIR program. These dead variables can be invalid NIR. For example,
4673 * TCS outputs must be per-vertex arrays (or decorated 'patch'), while
4674 * VS output variables wouldn't be.
4676 * To ensure we have valid NIR, we eliminate any dead inputs and outputs
4677 * right away. In order to do so, we must lower any constant initializers
4678 * on outputs so nir_remove_dead_variables sees that they're written to.
4680 nir_lower_constant_initializers(b
->shader
, nir_var_shader_out
);
4681 nir_remove_dead_variables(b
->shader
,
4682 nir_var_shader_in
| nir_var_shader_out
);
4684 /* We sometimes generate bogus derefs that, while never used, give the
4685 * validator a bit of heartburn. Run dead code to get rid of them.
4687 nir_opt_dce(b
->shader
);
4689 /* Unparent the shader from the vtn_builder before we delete the builder */
4690 ralloc_steal(NULL
, b
->shader
);