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 "spirv_info.h"
35 vtn_log(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
36 size_t spirv_offset
, const char *message
)
38 if (b
->options
->debug
.func
) {
39 b
->options
->debug
.func(b
->options
->debug
.private_data
,
40 level
, spirv_offset
, message
);
44 if (level
>= NIR_SPIRV_DEBUG_LEVEL_WARNING
)
45 fprintf(stderr
, "%s\n", message
);
50 vtn_logf(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
51 size_t spirv_offset
, const char *fmt
, ...)
57 msg
= ralloc_vasprintf(NULL
, fmt
, args
);
60 vtn_log(b
, level
, spirv_offset
, msg
);
66 vtn_log_err(struct vtn_builder
*b
,
67 enum nir_spirv_debug_level level
, const char *prefix
,
68 const char *file
, unsigned line
,
69 const char *fmt
, va_list args
)
73 msg
= ralloc_strdup(NULL
, prefix
);
76 ralloc_asprintf_append(&msg
, " In file %s:%u\n", file
, line
);
79 ralloc_asprintf_append(&msg
, " ");
81 ralloc_vasprintf_append(&msg
, fmt
, args
);
83 ralloc_asprintf_append(&msg
, "\n %zu bytes into the SPIR-V binary",
87 ralloc_asprintf_append(&msg
,
88 "\n in SPIR-V source file %s, line %d, col %d",
89 b
->file
, b
->line
, b
->col
);
92 vtn_log(b
, level
, b
->spirv_offset
, msg
);
98 _vtn_warn(struct vtn_builder
*b
, const char *file
, unsigned line
,
104 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_WARNING
, "SPIR-V WARNING:\n",
105 file
, line
, fmt
, args
);
110 _vtn_fail(struct vtn_builder
*b
, const char *file
, unsigned line
,
111 const char *fmt
, ...)
116 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V parsing FAILED:\n",
117 file
, line
, fmt
, args
);
120 longjmp(b
->fail_jump
, 1);
123 struct spec_constant_value
{
131 static struct vtn_ssa_value
*
132 vtn_undef_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
134 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
137 if (glsl_type_is_vector_or_scalar(type
)) {
138 unsigned num_components
= glsl_get_vector_elements(val
->type
);
139 unsigned bit_size
= glsl_get_bit_size(val
->type
);
140 val
->def
= nir_ssa_undef(&b
->nb
, num_components
, bit_size
);
142 unsigned elems
= glsl_get_length(val
->type
);
143 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
144 if (glsl_type_is_matrix(type
)) {
145 const struct glsl_type
*elem_type
=
146 glsl_vector_type(glsl_get_base_type(type
),
147 glsl_get_vector_elements(type
));
149 for (unsigned i
= 0; i
< elems
; i
++)
150 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
151 } else if (glsl_type_is_array(type
)) {
152 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
153 for (unsigned i
= 0; i
< elems
; i
++)
154 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
156 for (unsigned i
= 0; i
< elems
; i
++) {
157 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
158 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
166 static struct vtn_ssa_value
*
167 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
168 const struct glsl_type
*type
)
170 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
175 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
178 switch (glsl_get_base_type(type
)) {
181 case GLSL_TYPE_INT64
:
182 case GLSL_TYPE_UINT64
:
184 case GLSL_TYPE_FLOAT
:
185 case GLSL_TYPE_DOUBLE
: {
186 int bit_size
= glsl_get_bit_size(type
);
187 if (glsl_type_is_vector_or_scalar(type
)) {
188 unsigned num_components
= glsl_get_vector_elements(val
->type
);
189 nir_load_const_instr
*load
=
190 nir_load_const_instr_create(b
->shader
, num_components
, bit_size
);
192 load
->value
= constant
->values
[0];
194 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
195 val
->def
= &load
->def
;
197 assert(glsl_type_is_matrix(type
));
198 unsigned rows
= glsl_get_vector_elements(val
->type
);
199 unsigned columns
= glsl_get_matrix_columns(val
->type
);
200 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, columns
);
202 for (unsigned i
= 0; i
< columns
; i
++) {
203 struct vtn_ssa_value
*col_val
= rzalloc(b
, struct vtn_ssa_value
);
204 col_val
->type
= glsl_get_column_type(val
->type
);
205 nir_load_const_instr
*load
=
206 nir_load_const_instr_create(b
->shader
, rows
, bit_size
);
208 load
->value
= constant
->values
[i
];
210 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
211 col_val
->def
= &load
->def
;
213 val
->elems
[i
] = col_val
;
219 case GLSL_TYPE_ARRAY
: {
220 unsigned elems
= glsl_get_length(val
->type
);
221 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
222 const struct glsl_type
*elem_type
= glsl_get_array_element(val
->type
);
223 for (unsigned i
= 0; i
< elems
; i
++)
224 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
229 case GLSL_TYPE_STRUCT
: {
230 unsigned elems
= glsl_get_length(val
->type
);
231 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
232 for (unsigned i
= 0; i
< elems
; i
++) {
233 const struct glsl_type
*elem_type
=
234 glsl_get_struct_field(val
->type
, i
);
235 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
242 unreachable("bad constant type");
248 struct vtn_ssa_value
*
249 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
251 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
252 switch (val
->value_type
) {
253 case vtn_value_type_undef
:
254 return vtn_undef_ssa_value(b
, val
->type
->type
);
256 case vtn_value_type_constant
:
257 return vtn_const_ssa_value(b
, val
->constant
, val
->const_type
);
259 case vtn_value_type_ssa
:
262 case vtn_value_type_pointer
:
263 assert(val
->pointer
->ptr_type
&& val
->pointer
->ptr_type
->type
);
264 struct vtn_ssa_value
*ssa
=
265 vtn_create_ssa_value(b
, val
->pointer
->ptr_type
->type
);
266 ssa
->def
= vtn_pointer_to_ssa(b
, val
->pointer
);
270 unreachable("Invalid type for an SSA value");
275 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
276 unsigned word_count
, unsigned *words_used
)
278 char *dup
= ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
280 /* Ammount of space taken by the string (including the null) */
281 unsigned len
= strlen(dup
) + 1;
282 *words_used
= DIV_ROUND_UP(len
, sizeof(*words
));
288 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
289 const uint32_t *end
, vtn_instruction_handler handler
)
295 const uint32_t *w
= start
;
297 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
298 unsigned count
= w
[0] >> SpvWordCountShift
;
299 assert(count
>= 1 && w
+ count
<= end
);
301 b
->spirv_offset
= (uint8_t *)w
- (uint8_t *)b
->spirv
;
305 break; /* Do nothing */
308 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
320 if (!handler(b
, opcode
, w
, count
))
338 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
339 const uint32_t *w
, unsigned count
)
342 case SpvOpExtInstImport
: {
343 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
344 if (strcmp((const char *)&w
[2], "GLSL.std.450") == 0) {
345 val
->ext_handler
= vtn_handle_glsl450_instruction
;
347 unreachable("Unsupported extension");
353 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
354 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
361 unreachable("Unhandled opcode");
366 _foreach_decoration_helper(struct vtn_builder
*b
,
367 struct vtn_value
*base_value
,
369 struct vtn_value
*value
,
370 vtn_decoration_foreach_cb cb
, void *data
)
372 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
374 if (dec
->scope
== VTN_DEC_DECORATION
) {
375 member
= parent_member
;
376 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
377 assert(parent_member
== -1);
378 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
380 /* Not a decoration */
385 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
386 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
389 cb(b
, base_value
, member
, dec
, data
);
394 /** Iterates (recursively if needed) over all of the decorations on a value
396 * This function iterates over all of the decorations applied to a given
397 * value. If it encounters a decoration group, it recurses into the group
398 * and iterates over all of those decorations as well.
401 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
402 vtn_decoration_foreach_cb cb
, void *data
)
404 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
408 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
409 vtn_execution_mode_foreach_cb cb
, void *data
)
411 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
412 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
415 assert(dec
->group
== NULL
);
416 cb(b
, value
, dec
, data
);
421 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
422 const uint32_t *w
, unsigned count
)
424 const uint32_t *w_end
= w
+ count
;
425 const uint32_t target
= w
[1];
429 case SpvOpDecorationGroup
:
430 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
434 case SpvOpMemberDecorate
:
435 case SpvOpExecutionMode
: {
436 struct vtn_value
*val
= &b
->values
[target
];
438 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
441 dec
->scope
= VTN_DEC_DECORATION
;
443 case SpvOpMemberDecorate
:
444 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
446 case SpvOpExecutionMode
:
447 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
450 unreachable("Invalid decoration opcode");
452 dec
->decoration
= *(w
++);
455 /* Link into the list */
456 dec
->next
= val
->decoration
;
457 val
->decoration
= dec
;
461 case SpvOpGroupMemberDecorate
:
462 case SpvOpGroupDecorate
: {
463 struct vtn_value
*group
=
464 vtn_value(b
, target
, vtn_value_type_decoration_group
);
466 for (; w
< w_end
; w
++) {
467 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
468 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
471 if (opcode
== SpvOpGroupDecorate
) {
472 dec
->scope
= VTN_DEC_DECORATION
;
474 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
477 /* Link into the list */
478 dec
->next
= val
->decoration
;
479 val
->decoration
= dec
;
485 unreachable("Unhandled opcode");
489 struct member_decoration_ctx
{
491 struct glsl_struct_field
*fields
;
492 struct vtn_type
*type
;
495 /* does a shallow copy of a vtn_type */
497 static struct vtn_type
*
498 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
500 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
503 switch (src
->base_type
) {
504 case vtn_base_type_void
:
505 case vtn_base_type_scalar
:
506 case vtn_base_type_vector
:
507 case vtn_base_type_matrix
:
508 case vtn_base_type_array
:
509 case vtn_base_type_pointer
:
510 case vtn_base_type_image
:
511 case vtn_base_type_sampler
:
512 /* Nothing more to do */
515 case vtn_base_type_struct
:
516 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
517 memcpy(dest
->members
, src
->members
,
518 src
->length
* sizeof(src
->members
[0]));
520 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
521 memcpy(dest
->offsets
, src
->offsets
,
522 src
->length
* sizeof(src
->offsets
[0]));
525 case vtn_base_type_function
:
526 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
527 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
534 static struct vtn_type
*
535 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
537 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
538 type
= type
->members
[member
];
540 /* We may have an array of matrices.... Oh, joy! */
541 while (glsl_type_is_array(type
->type
)) {
542 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
543 type
= type
->array_element
;
546 assert(glsl_type_is_matrix(type
->type
));
552 struct_member_decoration_cb(struct vtn_builder
*b
,
553 struct vtn_value
*val
, int member
,
554 const struct vtn_decoration
*dec
, void *void_ctx
)
556 struct member_decoration_ctx
*ctx
= void_ctx
;
561 assert(member
< ctx
->num_fields
);
563 switch (dec
->decoration
) {
564 case SpvDecorationNonWritable
:
565 case SpvDecorationNonReadable
:
566 case SpvDecorationRelaxedPrecision
:
567 case SpvDecorationVolatile
:
568 case SpvDecorationCoherent
:
569 case SpvDecorationUniform
:
570 break; /* FIXME: Do nothing with this for now. */
571 case SpvDecorationNoPerspective
:
572 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
574 case SpvDecorationFlat
:
575 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
577 case SpvDecorationCentroid
:
578 ctx
->fields
[member
].centroid
= true;
580 case SpvDecorationSample
:
581 ctx
->fields
[member
].sample
= true;
583 case SpvDecorationStream
:
584 /* Vulkan only allows one GS stream */
585 assert(dec
->literals
[0] == 0);
587 case SpvDecorationLocation
:
588 ctx
->fields
[member
].location
= dec
->literals
[0];
590 case SpvDecorationComponent
:
591 break; /* FIXME: What should we do with these? */
592 case SpvDecorationBuiltIn
:
593 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
594 ctx
->type
->members
[member
]->is_builtin
= true;
595 ctx
->type
->members
[member
]->builtin
= dec
->literals
[0];
596 ctx
->type
->builtin_block
= true;
598 case SpvDecorationOffset
:
599 ctx
->type
->offsets
[member
] = dec
->literals
[0];
601 case SpvDecorationMatrixStride
:
602 /* Handled as a second pass */
604 case SpvDecorationColMajor
:
605 break; /* Nothing to do here. Column-major is the default. */
606 case SpvDecorationRowMajor
:
607 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
610 case SpvDecorationPatch
:
613 case SpvDecorationSpecId
:
614 case SpvDecorationBlock
:
615 case SpvDecorationBufferBlock
:
616 case SpvDecorationArrayStride
:
617 case SpvDecorationGLSLShared
:
618 case SpvDecorationGLSLPacked
:
619 case SpvDecorationInvariant
:
620 case SpvDecorationRestrict
:
621 case SpvDecorationAliased
:
622 case SpvDecorationConstant
:
623 case SpvDecorationIndex
:
624 case SpvDecorationBinding
:
625 case SpvDecorationDescriptorSet
:
626 case SpvDecorationLinkageAttributes
:
627 case SpvDecorationNoContraction
:
628 case SpvDecorationInputAttachmentIndex
:
629 vtn_warn("Decoration not allowed on struct members: %s",
630 spirv_decoration_to_string(dec
->decoration
));
633 case SpvDecorationXfbBuffer
:
634 case SpvDecorationXfbStride
:
635 vtn_warn("Vulkan does not have transform feedback");
638 case SpvDecorationCPacked
:
639 case SpvDecorationSaturatedConversion
:
640 case SpvDecorationFuncParamAttr
:
641 case SpvDecorationFPRoundingMode
:
642 case SpvDecorationFPFastMathMode
:
643 case SpvDecorationAlignment
:
644 vtn_warn("Decoration only allowed for CL-style kernels: %s",
645 spirv_decoration_to_string(dec
->decoration
));
649 unreachable("Unhandled decoration");
653 /* Matrix strides are handled as a separate pass because we need to know
654 * whether the matrix is row-major or not first.
657 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
658 struct vtn_value
*val
, int member
,
659 const struct vtn_decoration
*dec
,
662 if (dec
->decoration
!= SpvDecorationMatrixStride
)
666 struct member_decoration_ctx
*ctx
= void_ctx
;
668 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
669 if (mat_type
->row_major
) {
670 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
671 mat_type
->stride
= mat_type
->array_element
->stride
;
672 mat_type
->array_element
->stride
= dec
->literals
[0];
674 assert(mat_type
->array_element
->stride
> 0);
675 mat_type
->stride
= dec
->literals
[0];
680 type_decoration_cb(struct vtn_builder
*b
,
681 struct vtn_value
*val
, int member
,
682 const struct vtn_decoration
*dec
, void *ctx
)
684 struct vtn_type
*type
= val
->type
;
689 switch (dec
->decoration
) {
690 case SpvDecorationArrayStride
:
691 assert(type
->base_type
== vtn_base_type_matrix
||
692 type
->base_type
== vtn_base_type_array
||
693 type
->base_type
== vtn_base_type_pointer
);
694 type
->stride
= dec
->literals
[0];
696 case SpvDecorationBlock
:
697 assert(type
->base_type
== vtn_base_type_struct
);
700 case SpvDecorationBufferBlock
:
701 assert(type
->base_type
== vtn_base_type_struct
);
702 type
->buffer_block
= true;
704 case SpvDecorationGLSLShared
:
705 case SpvDecorationGLSLPacked
:
706 /* Ignore these, since we get explicit offsets anyways */
709 case SpvDecorationRowMajor
:
710 case SpvDecorationColMajor
:
711 case SpvDecorationMatrixStride
:
712 case SpvDecorationBuiltIn
:
713 case SpvDecorationNoPerspective
:
714 case SpvDecorationFlat
:
715 case SpvDecorationPatch
:
716 case SpvDecorationCentroid
:
717 case SpvDecorationSample
:
718 case SpvDecorationVolatile
:
719 case SpvDecorationCoherent
:
720 case SpvDecorationNonWritable
:
721 case SpvDecorationNonReadable
:
722 case SpvDecorationUniform
:
723 case SpvDecorationStream
:
724 case SpvDecorationLocation
:
725 case SpvDecorationComponent
:
726 case SpvDecorationOffset
:
727 case SpvDecorationXfbBuffer
:
728 case SpvDecorationXfbStride
:
729 vtn_warn("Decoration only allowed for struct members: %s",
730 spirv_decoration_to_string(dec
->decoration
));
733 case SpvDecorationRelaxedPrecision
:
734 case SpvDecorationSpecId
:
735 case SpvDecorationInvariant
:
736 case SpvDecorationRestrict
:
737 case SpvDecorationAliased
:
738 case SpvDecorationConstant
:
739 case SpvDecorationIndex
:
740 case SpvDecorationBinding
:
741 case SpvDecorationDescriptorSet
:
742 case SpvDecorationLinkageAttributes
:
743 case SpvDecorationNoContraction
:
744 case SpvDecorationInputAttachmentIndex
:
745 vtn_warn("Decoration not allowed on types: %s",
746 spirv_decoration_to_string(dec
->decoration
));
749 case SpvDecorationCPacked
:
750 case SpvDecorationSaturatedConversion
:
751 case SpvDecorationFuncParamAttr
:
752 case SpvDecorationFPRoundingMode
:
753 case SpvDecorationFPFastMathMode
:
754 case SpvDecorationAlignment
:
755 vtn_warn("Decoration only allowed for CL-style kernels: %s",
756 spirv_decoration_to_string(dec
->decoration
));
760 unreachable("Unhandled decoration");
765 translate_image_format(SpvImageFormat format
)
768 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
769 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
770 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
771 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
772 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
773 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
774 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
775 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
776 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
777 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
778 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
779 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
780 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
781 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
782 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
783 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
784 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
785 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
786 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
787 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
788 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
789 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
790 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
791 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
792 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
793 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
794 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
795 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
796 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
797 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
798 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
799 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
800 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
801 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
802 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
803 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
804 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
805 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
806 case SpvImageFormatR16ui
: return 0x8234; /* GL_R16UI */
807 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
809 unreachable("Invalid image format");
815 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
816 const uint32_t *w
, unsigned count
)
818 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
820 val
->type
= rzalloc(b
, struct vtn_type
);
821 val
->type
->val
= val
;
825 val
->type
->base_type
= vtn_base_type_void
;
826 val
->type
->type
= glsl_void_type();
829 val
->type
->base_type
= vtn_base_type_scalar
;
830 val
->type
->type
= glsl_bool_type();
834 const bool signedness
= w
[3];
835 val
->type
->base_type
= vtn_base_type_scalar
;
837 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
839 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
842 case SpvOpTypeFloat
: {
844 val
->type
->base_type
= vtn_base_type_scalar
;
845 val
->type
->type
= bit_size
== 64 ? glsl_double_type() : glsl_float_type();
849 case SpvOpTypeVector
: {
850 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
851 unsigned elems
= w
[3];
853 assert(glsl_type_is_scalar(base
->type
));
854 val
->type
->base_type
= vtn_base_type_vector
;
855 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
856 val
->type
->stride
= glsl_get_bit_size(base
->type
) / 8;
857 val
->type
->array_element
= base
;
861 case SpvOpTypeMatrix
: {
862 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
863 unsigned columns
= w
[3];
865 assert(glsl_type_is_vector(base
->type
));
866 val
->type
->base_type
= vtn_base_type_matrix
;
867 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
868 glsl_get_vector_elements(base
->type
),
870 assert(!glsl_type_is_error(val
->type
->type
));
871 val
->type
->length
= columns
;
872 val
->type
->array_element
= base
;
873 val
->type
->row_major
= false;
874 val
->type
->stride
= 0;
878 case SpvOpTypeRuntimeArray
:
879 case SpvOpTypeArray
: {
880 struct vtn_type
*array_element
=
881 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
883 if (opcode
== SpvOpTypeRuntimeArray
) {
884 /* A length of 0 is used to denote unsized arrays */
885 val
->type
->length
= 0;
888 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
891 val
->type
->base_type
= vtn_base_type_array
;
892 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
);
893 val
->type
->array_element
= array_element
;
894 val
->type
->stride
= 0;
898 case SpvOpTypeStruct
: {
899 unsigned num_fields
= count
- 2;
900 val
->type
->base_type
= vtn_base_type_struct
;
901 val
->type
->length
= num_fields
;
902 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
903 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
905 NIR_VLA(struct glsl_struct_field
, fields
, count
);
906 for (unsigned i
= 0; i
< num_fields
; i
++) {
907 val
->type
->members
[i
] =
908 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
909 fields
[i
] = (struct glsl_struct_field
) {
910 .type
= val
->type
->members
[i
]->type
,
911 .name
= ralloc_asprintf(b
, "field%d", i
),
916 struct member_decoration_ctx ctx
= {
917 .num_fields
= num_fields
,
922 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
923 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
925 const char *name
= val
->name
? val
->name
: "struct";
927 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
);
931 case SpvOpTypeFunction
: {
932 val
->type
->base_type
= vtn_base_type_function
;
933 val
->type
->type
= NULL
;
935 val
->type
->return_type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
937 const unsigned num_params
= count
- 3;
938 val
->type
->length
= num_params
;
939 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
940 for (unsigned i
= 0; i
< count
- 3; i
++) {
941 val
->type
->params
[i
] =
942 vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
;
947 case SpvOpTypePointer
: {
948 SpvStorageClass storage_class
= w
[2];
949 struct vtn_type
*deref_type
=
950 vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
952 val
->type
->base_type
= vtn_base_type_pointer
;
953 val
->type
->storage_class
= storage_class
;
954 val
->type
->deref
= deref_type
;
956 if (storage_class
== SpvStorageClassUniform
||
957 storage_class
== SpvStorageClassStorageBuffer
) {
958 /* These can actually be stored to nir_variables and used as SSA
959 * values so they need a real glsl_type.
961 val
->type
->type
= glsl_vector_type(GLSL_TYPE_UINT
, 2);
966 case SpvOpTypeImage
: {
967 val
->type
->base_type
= vtn_base_type_image
;
969 const struct glsl_type
*sampled_type
=
970 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
972 assert(glsl_type_is_vector_or_scalar(sampled_type
));
974 enum glsl_sampler_dim dim
;
975 switch ((SpvDim
)w
[3]) {
976 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
977 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
978 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
979 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
980 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
981 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
982 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
984 unreachable("Invalid SPIR-V Sampler dimension");
987 bool is_shadow
= w
[4];
988 bool is_array
= w
[5];
989 bool multisampled
= w
[6];
990 unsigned sampled
= w
[7];
991 SpvImageFormat format
= w
[8];
994 val
->type
->access_qualifier
= w
[9];
996 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
999 if (dim
== GLSL_SAMPLER_DIM_2D
)
1000 dim
= GLSL_SAMPLER_DIM_MS
;
1001 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1002 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1004 unreachable("Unsupported multisampled image type");
1007 val
->type
->image_format
= translate_image_format(format
);
1010 val
->type
->sampled
= true;
1011 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
1012 glsl_get_base_type(sampled_type
));
1013 } else if (sampled
== 2) {
1015 val
->type
->sampled
= false;
1016 val
->type
->type
= glsl_image_type(dim
, is_array
,
1017 glsl_get_base_type(sampled_type
));
1019 unreachable("We need to know if the image will be sampled");
1024 case SpvOpTypeSampledImage
:
1025 val
->type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
1028 case SpvOpTypeSampler
:
1029 /* The actual sampler type here doesn't really matter. It gets
1030 * thrown away the moment you combine it with an image. What really
1031 * matters is that it's a sampler type as opposed to an integer type
1032 * so the backend knows what to do.
1034 val
->type
->base_type
= vtn_base_type_sampler
;
1035 val
->type
->type
= glsl_bare_sampler_type();
1038 case SpvOpTypeOpaque
:
1039 case SpvOpTypeEvent
:
1040 case SpvOpTypeDeviceEvent
:
1041 case SpvOpTypeReserveId
:
1042 case SpvOpTypeQueue
:
1045 unreachable("Unhandled opcode");
1048 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1051 static nir_constant
*
1052 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
1054 nir_constant
*c
= rzalloc(b
, nir_constant
);
1056 /* For pointers and other typeless things, we have to return something but
1057 * it doesn't matter what.
1062 switch (glsl_get_base_type(type
)) {
1064 case GLSL_TYPE_UINT
:
1065 case GLSL_TYPE_INT64
:
1066 case GLSL_TYPE_UINT64
:
1067 case GLSL_TYPE_BOOL
:
1068 case GLSL_TYPE_FLOAT
:
1069 case GLSL_TYPE_DOUBLE
:
1070 /* Nothing to do here. It's already initialized to zero */
1073 case GLSL_TYPE_ARRAY
:
1074 assert(glsl_get_length(type
) > 0);
1075 c
->num_elements
= glsl_get_length(type
);
1076 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1078 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
1079 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1080 c
->elements
[i
] = c
->elements
[0];
1083 case GLSL_TYPE_STRUCT
:
1084 c
->num_elements
= glsl_get_length(type
);
1085 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1087 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
1088 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
1093 unreachable("Invalid type for null constant");
1100 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
1101 int member
, const struct vtn_decoration
*dec
,
1104 assert(member
== -1);
1105 if (dec
->decoration
!= SpvDecorationSpecId
)
1108 struct spec_constant_value
*const_value
= data
;
1110 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1111 if (b
->specializations
[i
].id
== dec
->literals
[0]) {
1112 if (const_value
->is_double
)
1113 const_value
->data64
= b
->specializations
[i
].data64
;
1115 const_value
->data32
= b
->specializations
[i
].data32
;
1122 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
1123 uint32_t const_value
)
1125 struct spec_constant_value data
;
1126 data
.is_double
= false;
1127 data
.data32
= const_value
;
1128 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1133 get_specialization64(struct vtn_builder
*b
, struct vtn_value
*val
,
1134 uint64_t const_value
)
1136 struct spec_constant_value data
;
1137 data
.is_double
= true;
1138 data
.data64
= const_value
;
1139 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1144 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1145 struct vtn_value
*val
,
1147 const struct vtn_decoration
*dec
,
1150 assert(member
== -1);
1151 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1152 dec
->literals
[0] != SpvBuiltInWorkgroupSize
)
1155 assert(val
->const_type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1157 b
->shader
->info
.cs
.local_size
[0] = val
->constant
->values
[0].u32
[0];
1158 b
->shader
->info
.cs
.local_size
[1] = val
->constant
->values
[0].u32
[1];
1159 b
->shader
->info
.cs
.local_size
[2] = val
->constant
->values
[0].u32
[2];
1163 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1164 const uint32_t *w
, unsigned count
)
1166 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1167 val
->const_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
1168 val
->constant
= rzalloc(b
, nir_constant
);
1170 case SpvOpConstantTrue
:
1171 assert(val
->const_type
== glsl_bool_type());
1172 val
->constant
->values
[0].u32
[0] = NIR_TRUE
;
1174 case SpvOpConstantFalse
:
1175 assert(val
->const_type
== glsl_bool_type());
1176 val
->constant
->values
[0].u32
[0] = NIR_FALSE
;
1179 case SpvOpSpecConstantTrue
:
1180 case SpvOpSpecConstantFalse
: {
1181 assert(val
->const_type
== glsl_bool_type());
1183 get_specialization(b
, val
, (opcode
== SpvOpSpecConstantTrue
));
1184 val
->constant
->values
[0].u32
[0] = int_val
? NIR_TRUE
: NIR_FALSE
;
1188 case SpvOpConstant
: {
1189 assert(glsl_type_is_scalar(val
->const_type
));
1190 int bit_size
= glsl_get_bit_size(val
->const_type
);
1191 if (bit_size
== 64) {
1192 val
->constant
->values
->u32
[0] = w
[3];
1193 val
->constant
->values
->u32
[1] = w
[4];
1195 assert(bit_size
== 32);
1196 val
->constant
->values
->u32
[0] = w
[3];
1200 case SpvOpSpecConstant
: {
1201 assert(glsl_type_is_scalar(val
->const_type
));
1202 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1203 int bit_size
= glsl_get_bit_size(val
->const_type
);
1205 val
->constant
->values
[0].u64
[0] =
1206 get_specialization64(b
, val
, vtn_u64_literal(&w
[3]));
1208 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1211 case SpvOpSpecConstantComposite
:
1212 case SpvOpConstantComposite
: {
1213 unsigned elem_count
= count
- 3;
1214 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1215 for (unsigned i
= 0; i
< elem_count
; i
++)
1216 elems
[i
] = vtn_value(b
, w
[i
+ 3], vtn_value_type_constant
)->constant
;
1218 switch (glsl_get_base_type(val
->const_type
)) {
1219 case GLSL_TYPE_UINT
:
1221 case GLSL_TYPE_UINT64
:
1222 case GLSL_TYPE_INT64
:
1223 case GLSL_TYPE_FLOAT
:
1224 case GLSL_TYPE_BOOL
:
1225 case GLSL_TYPE_DOUBLE
: {
1226 int bit_size
= glsl_get_bit_size(val
->const_type
);
1227 if (glsl_type_is_matrix(val
->const_type
)) {
1228 assert(glsl_get_matrix_columns(val
->const_type
) == elem_count
);
1229 for (unsigned i
= 0; i
< elem_count
; i
++)
1230 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1232 assert(glsl_type_is_vector(val
->const_type
));
1233 assert(glsl_get_vector_elements(val
->const_type
) == elem_count
);
1234 for (unsigned i
= 0; i
< elem_count
; i
++) {
1235 if (bit_size
== 64) {
1236 val
->constant
->values
[0].u64
[i
] = elems
[i
]->values
[0].u64
[0];
1238 assert(bit_size
== 32);
1239 val
->constant
->values
[0].u32
[i
] = elems
[i
]->values
[0].u32
[0];
1246 case GLSL_TYPE_STRUCT
:
1247 case GLSL_TYPE_ARRAY
:
1248 ralloc_steal(val
->constant
, elems
);
1249 val
->constant
->num_elements
= elem_count
;
1250 val
->constant
->elements
= elems
;
1254 unreachable("Unsupported type for constants");
1259 case SpvOpSpecConstantOp
: {
1260 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1262 case SpvOpVectorShuffle
: {
1263 struct vtn_value
*v0
= &b
->values
[w
[4]];
1264 struct vtn_value
*v1
= &b
->values
[w
[5]];
1266 assert(v0
->value_type
== vtn_value_type_constant
||
1267 v0
->value_type
== vtn_value_type_undef
);
1268 assert(v1
->value_type
== vtn_value_type_constant
||
1269 v1
->value_type
== vtn_value_type_undef
);
1271 unsigned len0
= v0
->value_type
== vtn_value_type_constant
?
1272 glsl_get_vector_elements(v0
->const_type
) :
1273 glsl_get_vector_elements(v0
->type
->type
);
1274 unsigned len1
= v1
->value_type
== vtn_value_type_constant
?
1275 glsl_get_vector_elements(v1
->const_type
) :
1276 glsl_get_vector_elements(v1
->type
->type
);
1278 assert(len0
+ len1
< 16);
1280 unsigned bit_size
= glsl_get_bit_size(val
->const_type
);
1281 unsigned bit_size0
= v0
->value_type
== vtn_value_type_constant
?
1282 glsl_get_bit_size(v0
->const_type
) :
1283 glsl_get_bit_size(v0
->type
->type
);
1284 unsigned bit_size1
= v1
->value_type
== vtn_value_type_constant
?
1285 glsl_get_bit_size(v1
->const_type
) :
1286 glsl_get_bit_size(v1
->type
->type
);
1288 assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1289 (void)bit_size0
; (void)bit_size1
;
1291 if (bit_size
== 64) {
1293 if (v0
->value_type
== vtn_value_type_constant
) {
1294 for (unsigned i
= 0; i
< len0
; i
++)
1295 u64
[i
] = v0
->constant
->values
[0].u64
[i
];
1297 if (v1
->value_type
== vtn_value_type_constant
) {
1298 for (unsigned i
= 0; i
< len1
; i
++)
1299 u64
[len0
+ i
] = v1
->constant
->values
[0].u64
[i
];
1302 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1303 uint32_t comp
= w
[i
+ 6];
1304 /* If component is not used, set the value to a known constant
1305 * to detect if it is wrongly used.
1307 if (comp
== (uint32_t)-1)
1308 val
->constant
->values
[0].u64
[j
] = 0xdeadbeefdeadbeef;
1310 val
->constant
->values
[0].u64
[j
] = u64
[comp
];
1314 if (v0
->value_type
== vtn_value_type_constant
) {
1315 for (unsigned i
= 0; i
< len0
; i
++)
1316 u32
[i
] = v0
->constant
->values
[0].u32
[i
];
1318 if (v1
->value_type
== vtn_value_type_constant
) {
1319 for (unsigned i
= 0; i
< len1
; i
++)
1320 u32
[len0
+ i
] = v1
->constant
->values
[0].u32
[i
];
1323 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1324 uint32_t comp
= w
[i
+ 6];
1325 /* If component is not used, set the value to a known constant
1326 * to detect if it is wrongly used.
1328 if (comp
== (uint32_t)-1)
1329 val
->constant
->values
[0].u32
[j
] = 0xdeadbeef;
1331 val
->constant
->values
[0].u32
[j
] = u32
[comp
];
1337 case SpvOpCompositeExtract
:
1338 case SpvOpCompositeInsert
: {
1339 struct vtn_value
*comp
;
1340 unsigned deref_start
;
1341 struct nir_constant
**c
;
1342 if (opcode
== SpvOpCompositeExtract
) {
1343 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1345 c
= &comp
->constant
;
1347 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1349 val
->constant
= nir_constant_clone(comp
->constant
,
1356 const struct glsl_type
*type
= comp
->const_type
;
1357 for (unsigned i
= deref_start
; i
< count
; i
++) {
1358 switch (glsl_get_base_type(type
)) {
1359 case GLSL_TYPE_UINT
:
1361 case GLSL_TYPE_UINT64
:
1362 case GLSL_TYPE_INT64
:
1363 case GLSL_TYPE_FLOAT
:
1364 case GLSL_TYPE_DOUBLE
:
1365 case GLSL_TYPE_BOOL
:
1366 /* If we hit this granularity, we're picking off an element */
1367 if (glsl_type_is_matrix(type
)) {
1368 assert(col
== 0 && elem
== -1);
1371 type
= glsl_get_column_type(type
);
1373 assert(elem
<= 0 && glsl_type_is_vector(type
));
1375 type
= glsl_scalar_type(glsl_get_base_type(type
));
1379 case GLSL_TYPE_ARRAY
:
1380 c
= &(*c
)->elements
[w
[i
]];
1381 type
= glsl_get_array_element(type
);
1384 case GLSL_TYPE_STRUCT
:
1385 c
= &(*c
)->elements
[w
[i
]];
1386 type
= glsl_get_struct_field(type
, w
[i
]);
1390 unreachable("Invalid constant type");
1394 if (opcode
== SpvOpCompositeExtract
) {
1398 unsigned num_components
= glsl_get_vector_elements(type
);
1399 unsigned bit_size
= glsl_get_bit_size(type
);
1400 for (unsigned i
= 0; i
< num_components
; i
++)
1401 if (bit_size
== 64) {
1402 val
->constant
->values
[0].u64
[i
] = (*c
)->values
[col
].u64
[elem
+ i
];
1404 assert(bit_size
== 32);
1405 val
->constant
->values
[0].u32
[i
] = (*c
)->values
[col
].u32
[elem
+ i
];
1409 struct vtn_value
*insert
=
1410 vtn_value(b
, w
[4], vtn_value_type_constant
);
1411 assert(insert
->const_type
== type
);
1413 *c
= insert
->constant
;
1415 unsigned num_components
= glsl_get_vector_elements(type
);
1416 unsigned bit_size
= glsl_get_bit_size(type
);
1417 for (unsigned i
= 0; i
< num_components
; i
++)
1418 if (bit_size
== 64) {
1419 (*c
)->values
[col
].u64
[elem
+ i
] = insert
->constant
->values
[0].u64
[i
];
1421 assert(bit_size
== 32);
1422 (*c
)->values
[col
].u32
[elem
+ i
] = insert
->constant
->values
[0].u32
[i
];
1431 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->const_type
);
1432 nir_alu_type src_alu_type
= dst_alu_type
;
1433 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(opcode
, &swap
, src_alu_type
, dst_alu_type
);
1435 unsigned num_components
= glsl_get_vector_elements(val
->const_type
);
1437 glsl_get_bit_size(val
->const_type
);
1439 nir_const_value src
[4];
1441 for (unsigned i
= 0; i
< count
- 4; i
++) {
1443 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
)->constant
;
1445 unsigned j
= swap
? 1 - i
: i
;
1446 assert(bit_size
== 32);
1447 src
[j
] = c
->values
[0];
1450 val
->constant
->values
[0] =
1451 nir_eval_const_opcode(op
, num_components
, bit_size
, src
);
1458 case SpvOpConstantNull
:
1459 val
->constant
= vtn_null_constant(b
, val
->const_type
);
1462 case SpvOpConstantSampler
:
1463 unreachable("OpConstantSampler requires Kernel Capability");
1467 unreachable("Unhandled opcode");
1470 /* Now that we have the value, update the workgroup size if needed */
1471 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1475 vtn_handle_function_call(struct vtn_builder
*b
, SpvOp opcode
,
1476 const uint32_t *w
, unsigned count
)
1478 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1479 struct vtn_function
*vtn_callee
=
1480 vtn_value(b
, w
[3], vtn_value_type_function
)->func
;
1481 struct nir_function
*callee
= vtn_callee
->impl
->function
;
1483 vtn_callee
->referenced
= true;
1485 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, callee
);
1486 for (unsigned i
= 0; i
< call
->num_params
; i
++) {
1487 unsigned arg_id
= w
[4 + i
];
1488 struct vtn_value
*arg
= vtn_untyped_value(b
, arg_id
);
1489 if (arg
->value_type
== vtn_value_type_pointer
&&
1490 arg
->pointer
->ptr_type
->type
== NULL
) {
1491 nir_deref_var
*d
= vtn_pointer_to_deref(b
, arg
->pointer
);
1492 call
->params
[i
] = nir_deref_var_clone(d
, call
);
1494 struct vtn_ssa_value
*arg_ssa
= vtn_ssa_value(b
, arg_id
);
1496 /* Make a temporary to store the argument in */
1498 nir_local_variable_create(b
->nb
.impl
, arg_ssa
->type
, "arg_tmp");
1499 call
->params
[i
] = nir_deref_var_create(call
, tmp
);
1501 vtn_local_store(b
, arg_ssa
, call
->params
[i
]);
1505 nir_variable
*out_tmp
= NULL
;
1506 assert(res_type
->type
== callee
->return_type
);
1507 if (!glsl_type_is_void(callee
->return_type
)) {
1508 out_tmp
= nir_local_variable_create(b
->nb
.impl
, callee
->return_type
,
1510 call
->return_deref
= nir_deref_var_create(call
, out_tmp
);
1513 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
1515 if (glsl_type_is_void(callee
->return_type
)) {
1516 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1518 vtn_push_ssa(b
, w
[2], res_type
, vtn_local_load(b
, call
->return_deref
));
1522 struct vtn_ssa_value
*
1523 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1525 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1528 if (!glsl_type_is_vector_or_scalar(type
)) {
1529 unsigned elems
= glsl_get_length(type
);
1530 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1531 for (unsigned i
= 0; i
< elems
; i
++) {
1532 const struct glsl_type
*child_type
;
1534 switch (glsl_get_base_type(type
)) {
1536 case GLSL_TYPE_UINT
:
1537 case GLSL_TYPE_INT64
:
1538 case GLSL_TYPE_UINT64
:
1539 case GLSL_TYPE_BOOL
:
1540 case GLSL_TYPE_FLOAT
:
1541 case GLSL_TYPE_DOUBLE
:
1542 child_type
= glsl_get_column_type(type
);
1544 case GLSL_TYPE_ARRAY
:
1545 child_type
= glsl_get_array_element(type
);
1547 case GLSL_TYPE_STRUCT
:
1548 child_type
= glsl_get_struct_field(type
, i
);
1551 unreachable("unkown base type");
1554 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
1562 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
1565 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
1566 src
.src_type
= type
;
1571 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
1572 const uint32_t *w
, unsigned count
)
1574 if (opcode
== SpvOpSampledImage
) {
1575 struct vtn_value
*val
=
1576 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1577 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1578 val
->sampled_image
->type
=
1579 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1580 val
->sampled_image
->image
=
1581 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1582 val
->sampled_image
->sampler
=
1583 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
1585 } else if (opcode
== SpvOpImage
) {
1586 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1587 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
1588 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
1589 val
->pointer
= src_val
->sampled_image
->image
;
1591 assert(src_val
->value_type
== vtn_value_type_pointer
);
1592 val
->pointer
= src_val
->pointer
;
1597 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1598 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1600 struct vtn_sampled_image sampled
;
1601 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
1602 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
1603 sampled
= *sampled_val
->sampled_image
;
1605 assert(sampled_val
->value_type
== vtn_value_type_pointer
);
1606 sampled
.type
= sampled_val
->pointer
->type
;
1607 sampled
.image
= NULL
;
1608 sampled
.sampler
= sampled_val
->pointer
;
1611 const struct glsl_type
*image_type
= sampled
.type
->type
;
1612 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
1613 const bool is_array
= glsl_sampler_type_is_array(image_type
);
1614 const bool is_shadow
= glsl_sampler_type_is_shadow(image_type
);
1616 /* Figure out the base texture operation */
1619 case SpvOpImageSampleImplicitLod
:
1620 case SpvOpImageSampleDrefImplicitLod
:
1621 case SpvOpImageSampleProjImplicitLod
:
1622 case SpvOpImageSampleProjDrefImplicitLod
:
1623 texop
= nir_texop_tex
;
1626 case SpvOpImageSampleExplicitLod
:
1627 case SpvOpImageSampleDrefExplicitLod
:
1628 case SpvOpImageSampleProjExplicitLod
:
1629 case SpvOpImageSampleProjDrefExplicitLod
:
1630 texop
= nir_texop_txl
;
1633 case SpvOpImageFetch
:
1634 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
1635 texop
= nir_texop_txf_ms
;
1637 texop
= nir_texop_txf
;
1641 case SpvOpImageGather
:
1642 case SpvOpImageDrefGather
:
1643 texop
= nir_texop_tg4
;
1646 case SpvOpImageQuerySizeLod
:
1647 case SpvOpImageQuerySize
:
1648 texop
= nir_texop_txs
;
1651 case SpvOpImageQueryLod
:
1652 texop
= nir_texop_lod
;
1655 case SpvOpImageQueryLevels
:
1656 texop
= nir_texop_query_levels
;
1659 case SpvOpImageQuerySamples
:
1660 texop
= nir_texop_texture_samples
;
1664 unreachable("Unhandled opcode");
1667 nir_tex_src srcs
[8]; /* 8 should be enough */
1668 nir_tex_src
*p
= srcs
;
1672 struct nir_ssa_def
*coord
;
1673 unsigned coord_components
;
1675 case SpvOpImageSampleImplicitLod
:
1676 case SpvOpImageSampleExplicitLod
:
1677 case SpvOpImageSampleDrefImplicitLod
:
1678 case SpvOpImageSampleDrefExplicitLod
:
1679 case SpvOpImageSampleProjImplicitLod
:
1680 case SpvOpImageSampleProjExplicitLod
:
1681 case SpvOpImageSampleProjDrefImplicitLod
:
1682 case SpvOpImageSampleProjDrefExplicitLod
:
1683 case SpvOpImageFetch
:
1684 case SpvOpImageGather
:
1685 case SpvOpImageDrefGather
:
1686 case SpvOpImageQueryLod
: {
1687 /* All these types have the coordinate as their first real argument */
1688 switch (sampler_dim
) {
1689 case GLSL_SAMPLER_DIM_1D
:
1690 case GLSL_SAMPLER_DIM_BUF
:
1691 coord_components
= 1;
1693 case GLSL_SAMPLER_DIM_2D
:
1694 case GLSL_SAMPLER_DIM_RECT
:
1695 case GLSL_SAMPLER_DIM_MS
:
1696 coord_components
= 2;
1698 case GLSL_SAMPLER_DIM_3D
:
1699 case GLSL_SAMPLER_DIM_CUBE
:
1700 coord_components
= 3;
1703 unreachable("Invalid sampler type");
1706 if (is_array
&& texop
!= nir_texop_lod
)
1709 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
1710 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
1711 (1 << coord_components
) - 1));
1712 p
->src_type
= nir_tex_src_coord
;
1719 coord_components
= 0;
1724 case SpvOpImageSampleProjImplicitLod
:
1725 case SpvOpImageSampleProjExplicitLod
:
1726 case SpvOpImageSampleProjDrefImplicitLod
:
1727 case SpvOpImageSampleProjDrefExplicitLod
:
1728 /* These have the projector as the last coordinate component */
1729 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
1730 p
->src_type
= nir_tex_src_projector
;
1738 unsigned gather_component
= 0;
1740 case SpvOpImageSampleDrefImplicitLod
:
1741 case SpvOpImageSampleDrefExplicitLod
:
1742 case SpvOpImageSampleProjDrefImplicitLod
:
1743 case SpvOpImageSampleProjDrefExplicitLod
:
1744 case SpvOpImageDrefGather
:
1745 /* These all have an explicit depth value as their next source */
1746 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
1749 case SpvOpImageGather
:
1750 /* This has a component as its next source */
1752 vtn_value(b
, w
[idx
++], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
1759 /* For OpImageQuerySizeLod, we always have an LOD */
1760 if (opcode
== SpvOpImageQuerySizeLod
)
1761 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1763 /* Now we need to handle some number of optional arguments */
1764 const struct vtn_ssa_value
*gather_offsets
= NULL
;
1766 uint32_t operands
= w
[idx
++];
1768 if (operands
& SpvImageOperandsBiasMask
) {
1769 assert(texop
== nir_texop_tex
);
1770 texop
= nir_texop_txb
;
1771 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
1774 if (operands
& SpvImageOperandsLodMask
) {
1775 assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
1776 texop
== nir_texop_txs
);
1777 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1780 if (operands
& SpvImageOperandsGradMask
) {
1781 assert(texop
== nir_texop_txl
);
1782 texop
= nir_texop_txd
;
1783 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
1784 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
1787 if (operands
& SpvImageOperandsOffsetMask
||
1788 operands
& SpvImageOperandsConstOffsetMask
)
1789 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
1791 if (operands
& SpvImageOperandsConstOffsetsMask
) {
1792 gather_offsets
= vtn_ssa_value(b
, w
[idx
++]);
1793 (*p
++) = (nir_tex_src
){};
1796 if (operands
& SpvImageOperandsSampleMask
) {
1797 assert(texop
== nir_texop_txf_ms
);
1798 texop
= nir_texop_txf_ms
;
1799 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
1802 /* We should have now consumed exactly all of the arguments */
1803 assert(idx
== count
);
1805 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
1808 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1810 instr
->coord_components
= coord_components
;
1811 instr
->sampler_dim
= sampler_dim
;
1812 instr
->is_array
= is_array
;
1813 instr
->is_shadow
= is_shadow
;
1814 instr
->is_new_style_shadow
=
1815 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
1816 instr
->component
= gather_component
;
1818 switch (glsl_get_sampler_result_type(image_type
)) {
1819 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
1820 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
1821 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
1822 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
1824 unreachable("Invalid base type for sampler result");
1827 nir_deref_var
*sampler
= vtn_pointer_to_deref(b
, sampled
.sampler
);
1828 nir_deref_var
*texture
;
1829 if (sampled
.image
) {
1830 nir_deref_var
*image
= vtn_pointer_to_deref(b
, sampled
.image
);
1836 instr
->texture
= nir_deref_var_clone(texture
, instr
);
1838 switch (instr
->op
) {
1844 /* These operations require a sampler */
1845 instr
->sampler
= nir_deref_var_clone(sampler
, instr
);
1848 case nir_texop_txf_ms
:
1851 case nir_texop_query_levels
:
1852 case nir_texop_texture_samples
:
1853 case nir_texop_samples_identical
:
1855 instr
->sampler
= NULL
;
1857 case nir_texop_txf_ms_mcs
:
1858 unreachable("unexpected nir_texop_txf_ms_mcs");
1861 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1862 nir_tex_instr_dest_size(instr
), 32, NULL
);
1864 assert(glsl_get_vector_elements(ret_type
->type
) ==
1865 nir_tex_instr_dest_size(instr
));
1868 nir_instr
*instruction
;
1869 if (gather_offsets
) {
1870 assert(glsl_get_base_type(gather_offsets
->type
) == GLSL_TYPE_ARRAY
);
1871 assert(glsl_get_length(gather_offsets
->type
) == 4);
1872 nir_tex_instr
*instrs
[4] = {instr
, NULL
, NULL
, NULL
};
1874 /* Copy the current instruction 4x */
1875 for (uint32_t i
= 1; i
< 4; i
++) {
1876 instrs
[i
] = nir_tex_instr_create(b
->shader
, instr
->num_srcs
);
1877 instrs
[i
]->op
= instr
->op
;
1878 instrs
[i
]->coord_components
= instr
->coord_components
;
1879 instrs
[i
]->sampler_dim
= instr
->sampler_dim
;
1880 instrs
[i
]->is_array
= instr
->is_array
;
1881 instrs
[i
]->is_shadow
= instr
->is_shadow
;
1882 instrs
[i
]->is_new_style_shadow
= instr
->is_new_style_shadow
;
1883 instrs
[i
]->component
= instr
->component
;
1884 instrs
[i
]->dest_type
= instr
->dest_type
;
1885 instrs
[i
]->texture
= nir_deref_var_clone(texture
, instrs
[i
]);
1886 instrs
[i
]->sampler
= NULL
;
1888 memcpy(instrs
[i
]->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1890 nir_ssa_dest_init(&instrs
[i
]->instr
, &instrs
[i
]->dest
,
1891 nir_tex_instr_dest_size(instr
), 32, NULL
);
1894 /* Fill in the last argument with the offset from the passed in offsets
1895 * and insert the instruction into the stream.
1897 for (uint32_t i
= 0; i
< 4; i
++) {
1899 src
.src
= nir_src_for_ssa(gather_offsets
->elems
[i
]->def
);
1900 src
.src_type
= nir_tex_src_offset
;
1901 instrs
[i
]->src
[instrs
[i
]->num_srcs
- 1] = src
;
1902 nir_builder_instr_insert(&b
->nb
, &instrs
[i
]->instr
);
1905 /* Combine the results of the 4 instructions by taking their .w
1908 nir_alu_instr
*vec4
= nir_alu_instr_create(b
->shader
, nir_op_vec4
);
1909 nir_ssa_dest_init(&vec4
->instr
, &vec4
->dest
.dest
, 4, 32, NULL
);
1910 vec4
->dest
.write_mask
= 0xf;
1911 for (uint32_t i
= 0; i
< 4; i
++) {
1912 vec4
->src
[i
].src
= nir_src_for_ssa(&instrs
[i
]->dest
.ssa
);
1913 vec4
->src
[i
].swizzle
[0] = 3;
1915 def
= &vec4
->dest
.dest
.ssa
;
1916 instruction
= &vec4
->instr
;
1918 def
= &instr
->dest
.ssa
;
1919 instruction
= &instr
->instr
;
1922 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
1923 val
->ssa
->def
= def
;
1925 nir_builder_instr_insert(&b
->nb
, instruction
);
1929 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
1930 const uint32_t *w
, nir_src
*src
)
1933 case SpvOpAtomicIIncrement
:
1934 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
1937 case SpvOpAtomicIDecrement
:
1938 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
1941 case SpvOpAtomicISub
:
1943 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
1946 case SpvOpAtomicCompareExchange
:
1947 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
1948 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
1951 case SpvOpAtomicExchange
:
1952 case SpvOpAtomicIAdd
:
1953 case SpvOpAtomicSMin
:
1954 case SpvOpAtomicUMin
:
1955 case SpvOpAtomicSMax
:
1956 case SpvOpAtomicUMax
:
1957 case SpvOpAtomicAnd
:
1959 case SpvOpAtomicXor
:
1960 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1964 unreachable("Invalid SPIR-V atomic");
1968 static nir_ssa_def
*
1969 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
1971 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
1973 /* The image_load_store intrinsics assume a 4-dim coordinate */
1974 unsigned dim
= glsl_get_vector_elements(coord
->type
);
1975 unsigned swizzle
[4];
1976 for (unsigned i
= 0; i
< 4; i
++)
1977 swizzle
[i
] = MIN2(i
, dim
- 1);
1979 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
1983 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
1984 const uint32_t *w
, unsigned count
)
1986 /* Just get this one out of the way */
1987 if (opcode
== SpvOpImageTexelPointer
) {
1988 struct vtn_value
*val
=
1989 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
1990 val
->image
= ralloc(b
, struct vtn_image_pointer
);
1992 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1993 val
->image
->coord
= get_image_coord(b
, w
[4]);
1994 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
1998 struct vtn_image_pointer image
;
2001 case SpvOpAtomicExchange
:
2002 case SpvOpAtomicCompareExchange
:
2003 case SpvOpAtomicCompareExchangeWeak
:
2004 case SpvOpAtomicIIncrement
:
2005 case SpvOpAtomicIDecrement
:
2006 case SpvOpAtomicIAdd
:
2007 case SpvOpAtomicISub
:
2008 case SpvOpAtomicLoad
:
2009 case SpvOpAtomicSMin
:
2010 case SpvOpAtomicUMin
:
2011 case SpvOpAtomicSMax
:
2012 case SpvOpAtomicUMax
:
2013 case SpvOpAtomicAnd
:
2015 case SpvOpAtomicXor
:
2016 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
2019 case SpvOpAtomicStore
:
2020 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
2023 case SpvOpImageQuerySize
:
2024 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2026 image
.sample
= NULL
;
2029 case SpvOpImageRead
:
2030 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2031 image
.coord
= get_image_coord(b
, w
[4]);
2033 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
2034 assert(w
[5] == SpvImageOperandsSampleMask
);
2035 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
2037 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2041 case SpvOpImageWrite
:
2042 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2043 image
.coord
= get_image_coord(b
, w
[2]);
2047 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
2048 assert(w
[4] == SpvImageOperandsSampleMask
);
2049 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
2051 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2056 unreachable("Invalid image opcode");
2059 nir_intrinsic_op op
;
2061 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_##N; break;
2062 OP(ImageQuerySize
, size
)
2064 OP(ImageWrite
, store
)
2065 OP(AtomicLoad
, load
)
2066 OP(AtomicStore
, store
)
2067 OP(AtomicExchange
, atomic_exchange
)
2068 OP(AtomicCompareExchange
, atomic_comp_swap
)
2069 OP(AtomicIIncrement
, atomic_add
)
2070 OP(AtomicIDecrement
, atomic_add
)
2071 OP(AtomicIAdd
, atomic_add
)
2072 OP(AtomicISub
, atomic_add
)
2073 OP(AtomicSMin
, atomic_min
)
2074 OP(AtomicUMin
, atomic_min
)
2075 OP(AtomicSMax
, atomic_max
)
2076 OP(AtomicUMax
, atomic_max
)
2077 OP(AtomicAnd
, atomic_and
)
2078 OP(AtomicOr
, atomic_or
)
2079 OP(AtomicXor
, atomic_xor
)
2082 unreachable("Invalid image opcode");
2085 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
2087 nir_deref_var
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
2088 intrin
->variables
[0] = nir_deref_var_clone(image_deref
, intrin
);
2090 /* ImageQuerySize doesn't take any extra parameters */
2091 if (opcode
!= SpvOpImageQuerySize
) {
2092 /* The image coordinate is always 4 components but we may not have that
2093 * many. Swizzle to compensate.
2096 for (unsigned i
= 0; i
< 4; i
++)
2097 swiz
[i
] = i
< image
.coord
->num_components
? i
: 0;
2098 intrin
->src
[0] = nir_src_for_ssa(nir_swizzle(&b
->nb
, image
.coord
,
2100 intrin
->src
[1] = nir_src_for_ssa(image
.sample
);
2104 case SpvOpAtomicLoad
:
2105 case SpvOpImageQuerySize
:
2106 case SpvOpImageRead
:
2108 case SpvOpAtomicStore
:
2109 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2111 case SpvOpImageWrite
:
2112 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[3])->def
);
2115 case SpvOpAtomicCompareExchange
:
2116 case SpvOpAtomicIIncrement
:
2117 case SpvOpAtomicIDecrement
:
2118 case SpvOpAtomicExchange
:
2119 case SpvOpAtomicIAdd
:
2120 case SpvOpAtomicISub
:
2121 case SpvOpAtomicSMin
:
2122 case SpvOpAtomicUMin
:
2123 case SpvOpAtomicSMax
:
2124 case SpvOpAtomicUMax
:
2125 case SpvOpAtomicAnd
:
2127 case SpvOpAtomicXor
:
2128 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[2]);
2132 unreachable("Invalid image opcode");
2135 if (opcode
!= SpvOpImageWrite
) {
2136 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2137 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2139 unsigned dest_components
=
2140 nir_intrinsic_infos
[intrin
->intrinsic
].dest_components
;
2141 if (intrin
->intrinsic
== nir_intrinsic_image_size
) {
2142 dest_components
= intrin
->num_components
=
2143 glsl_get_vector_elements(type
->type
);
2146 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2147 dest_components
, 32, NULL
);
2149 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2151 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
2152 val
->ssa
->def
= &intrin
->dest
.ssa
;
2154 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2158 static nir_intrinsic_op
2159 get_ssbo_nir_atomic_op(SpvOp opcode
)
2162 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2163 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2164 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2165 OP(AtomicExchange
, atomic_exchange
)
2166 OP(AtomicCompareExchange
, atomic_comp_swap
)
2167 OP(AtomicIIncrement
, atomic_add
)
2168 OP(AtomicIDecrement
, atomic_add
)
2169 OP(AtomicIAdd
, atomic_add
)
2170 OP(AtomicISub
, atomic_add
)
2171 OP(AtomicSMin
, atomic_imin
)
2172 OP(AtomicUMin
, atomic_umin
)
2173 OP(AtomicSMax
, atomic_imax
)
2174 OP(AtomicUMax
, atomic_umax
)
2175 OP(AtomicAnd
, atomic_and
)
2176 OP(AtomicOr
, atomic_or
)
2177 OP(AtomicXor
, atomic_xor
)
2180 unreachable("Invalid SSBO atomic");
2184 static nir_intrinsic_op
2185 get_shared_nir_atomic_op(SpvOp opcode
)
2188 case SpvOpAtomicLoad
: return nir_intrinsic_load_var
;
2189 case SpvOpAtomicStore
: return nir_intrinsic_store_var
;
2190 #define OP(S, N) case SpvOp##S: return nir_intrinsic_var_##N;
2191 OP(AtomicExchange
, atomic_exchange
)
2192 OP(AtomicCompareExchange
, atomic_comp_swap
)
2193 OP(AtomicIIncrement
, atomic_add
)
2194 OP(AtomicIDecrement
, atomic_add
)
2195 OP(AtomicIAdd
, atomic_add
)
2196 OP(AtomicISub
, atomic_add
)
2197 OP(AtomicSMin
, atomic_imin
)
2198 OP(AtomicUMin
, atomic_umin
)
2199 OP(AtomicSMax
, atomic_imax
)
2200 OP(AtomicUMax
, atomic_umax
)
2201 OP(AtomicAnd
, atomic_and
)
2202 OP(AtomicOr
, atomic_or
)
2203 OP(AtomicXor
, atomic_xor
)
2206 unreachable("Invalid shared atomic");
2211 vtn_handle_ssbo_or_shared_atomic(struct vtn_builder
*b
, SpvOp opcode
,
2212 const uint32_t *w
, unsigned count
)
2214 struct vtn_pointer
*ptr
;
2215 nir_intrinsic_instr
*atomic
;
2218 case SpvOpAtomicLoad
:
2219 case SpvOpAtomicExchange
:
2220 case SpvOpAtomicCompareExchange
:
2221 case SpvOpAtomicCompareExchangeWeak
:
2222 case SpvOpAtomicIIncrement
:
2223 case SpvOpAtomicIDecrement
:
2224 case SpvOpAtomicIAdd
:
2225 case SpvOpAtomicISub
:
2226 case SpvOpAtomicSMin
:
2227 case SpvOpAtomicUMin
:
2228 case SpvOpAtomicSMax
:
2229 case SpvOpAtomicUMax
:
2230 case SpvOpAtomicAnd
:
2232 case SpvOpAtomicXor
:
2233 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2236 case SpvOpAtomicStore
:
2237 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2241 unreachable("Invalid SPIR-V atomic");
2245 SpvScope scope = w[4];
2246 SpvMemorySemanticsMask semantics = w[5];
2249 if (ptr
->mode
== vtn_variable_mode_workgroup
) {
2250 nir_deref_var
*deref
= vtn_pointer_to_deref(b
, ptr
);
2251 const struct glsl_type
*deref_type
= nir_deref_tail(&deref
->deref
)->type
;
2252 nir_intrinsic_op op
= get_shared_nir_atomic_op(opcode
);
2253 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2254 atomic
->variables
[0] = nir_deref_var_clone(deref
, atomic
);
2257 case SpvOpAtomicLoad
:
2258 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2261 case SpvOpAtomicStore
:
2262 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2263 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2264 atomic
->src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2267 case SpvOpAtomicExchange
:
2268 case SpvOpAtomicCompareExchange
:
2269 case SpvOpAtomicCompareExchangeWeak
:
2270 case SpvOpAtomicIIncrement
:
2271 case SpvOpAtomicIDecrement
:
2272 case SpvOpAtomicIAdd
:
2273 case SpvOpAtomicISub
:
2274 case SpvOpAtomicSMin
:
2275 case SpvOpAtomicUMin
:
2276 case SpvOpAtomicSMax
:
2277 case SpvOpAtomicUMax
:
2278 case SpvOpAtomicAnd
:
2280 case SpvOpAtomicXor
:
2281 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[0]);
2285 unreachable("Invalid SPIR-V atomic");
2289 assert(ptr
->mode
== vtn_variable_mode_ssbo
);
2290 nir_ssa_def
*offset
, *index
;
2291 offset
= vtn_pointer_to_offset(b
, ptr
, &index
, NULL
);
2293 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(opcode
);
2295 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2298 case SpvOpAtomicLoad
:
2299 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2300 atomic
->src
[0] = nir_src_for_ssa(index
);
2301 atomic
->src
[1] = nir_src_for_ssa(offset
);
2304 case SpvOpAtomicStore
:
2305 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2306 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2307 atomic
->src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2308 atomic
->src
[1] = nir_src_for_ssa(index
);
2309 atomic
->src
[2] = nir_src_for_ssa(offset
);
2312 case SpvOpAtomicExchange
:
2313 case SpvOpAtomicCompareExchange
:
2314 case SpvOpAtomicCompareExchangeWeak
:
2315 case SpvOpAtomicIIncrement
:
2316 case SpvOpAtomicIDecrement
:
2317 case SpvOpAtomicIAdd
:
2318 case SpvOpAtomicISub
:
2319 case SpvOpAtomicSMin
:
2320 case SpvOpAtomicUMin
:
2321 case SpvOpAtomicSMax
:
2322 case SpvOpAtomicUMax
:
2323 case SpvOpAtomicAnd
:
2325 case SpvOpAtomicXor
:
2326 atomic
->src
[0] = nir_src_for_ssa(index
);
2327 atomic
->src
[1] = nir_src_for_ssa(offset
);
2328 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[2]);
2332 unreachable("Invalid SPIR-V atomic");
2336 if (opcode
!= SpvOpAtomicStore
) {
2337 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2339 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
2340 glsl_get_vector_elements(type
->type
),
2341 glsl_get_bit_size(type
->type
), NULL
);
2343 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2344 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
2345 val
->ssa
->def
= &atomic
->dest
.ssa
;
2346 val
->ssa
->type
= type
->type
;
2349 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
2352 static nir_alu_instr
*
2353 create_vec(nir_shader
*shader
, unsigned num_components
, unsigned bit_size
)
2356 switch (num_components
) {
2357 case 1: op
= nir_op_fmov
; break;
2358 case 2: op
= nir_op_vec2
; break;
2359 case 3: op
= nir_op_vec3
; break;
2360 case 4: op
= nir_op_vec4
; break;
2361 default: unreachable("bad vector size");
2364 nir_alu_instr
*vec
= nir_alu_instr_create(shader
, op
);
2365 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
2367 vec
->dest
.write_mask
= (1 << num_components
) - 1;
2372 struct vtn_ssa_value
*
2373 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
2375 if (src
->transposed
)
2376 return src
->transposed
;
2378 struct vtn_ssa_value
*dest
=
2379 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
2381 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
2382 nir_alu_instr
*vec
= create_vec(b
->shader
,
2383 glsl_get_matrix_columns(src
->type
),
2384 glsl_get_bit_size(src
->type
));
2385 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2386 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
2387 vec
->src
[0].swizzle
[0] = i
;
2389 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
2390 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
2391 vec
->src
[j
].swizzle
[0] = i
;
2394 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2395 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
2398 dest
->transposed
= src
;
2404 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
2406 unsigned swiz
[4] = { index
};
2407 return nir_swizzle(&b
->nb
, src
, swiz
, 1, true);
2411 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
2414 nir_alu_instr
*vec
= create_vec(b
->shader
, src
->num_components
,
2417 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
2419 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
2421 vec
->src
[i
].src
= nir_src_for_ssa(src
);
2422 vec
->src
[i
].swizzle
[0] = i
;
2426 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2428 return &vec
->dest
.dest
.ssa
;
2432 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2435 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
2436 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2437 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2438 vtn_vector_extract(b
, src
, i
), dest
);
2444 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2445 nir_ssa_def
*insert
, nir_ssa_def
*index
)
2447 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
2448 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2449 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2450 vtn_vector_insert(b
, src
, insert
, i
), dest
);
2455 static nir_ssa_def
*
2456 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
2457 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
2458 const uint32_t *indices
)
2460 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
, src0
->bit_size
);
2462 for (unsigned i
= 0; i
< num_components
; i
++) {
2463 uint32_t index
= indices
[i
];
2464 if (index
== 0xffffffff) {
2466 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
2467 } else if (index
< src0
->num_components
) {
2468 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
2469 vec
->src
[i
].swizzle
[0] = index
;
2471 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
2472 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
2476 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2478 return &vec
->dest
.dest
.ssa
;
2482 * Concatentates a number of vectors/scalars together to produce a vector
2484 static nir_ssa_def
*
2485 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
2486 unsigned num_srcs
, nir_ssa_def
**srcs
)
2488 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
,
2491 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2493 * "When constructing a vector, there must be at least two Constituent
2496 assert(num_srcs
>= 2);
2498 unsigned dest_idx
= 0;
2499 for (unsigned i
= 0; i
< num_srcs
; i
++) {
2500 nir_ssa_def
*src
= srcs
[i
];
2501 assert(dest_idx
+ src
->num_components
<= num_components
);
2502 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
2503 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
2504 vec
->src
[dest_idx
].swizzle
[0] = j
;
2509 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2511 * "When constructing a vector, the total number of components in all
2512 * the operands must equal the number of components in Result Type."
2514 assert(dest_idx
== num_components
);
2516 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2518 return &vec
->dest
.dest
.ssa
;
2521 static struct vtn_ssa_value
*
2522 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
2524 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
2525 dest
->type
= src
->type
;
2527 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2528 dest
->def
= src
->def
;
2530 unsigned elems
= glsl_get_length(src
->type
);
2532 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
2533 for (unsigned i
= 0; i
< elems
; i
++)
2534 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
2540 static struct vtn_ssa_value
*
2541 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2542 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
2543 unsigned num_indices
)
2545 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
2547 struct vtn_ssa_value
*cur
= dest
;
2549 for (i
= 0; i
< num_indices
- 1; i
++) {
2550 cur
= cur
->elems
[indices
[i
]];
2553 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2554 /* According to the SPIR-V spec, OpCompositeInsert may work down to
2555 * the component granularity. In that case, the last index will be
2556 * the index to insert the scalar into the vector.
2559 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
2561 cur
->elems
[indices
[i
]] = insert
;
2567 static struct vtn_ssa_value
*
2568 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2569 const uint32_t *indices
, unsigned num_indices
)
2571 struct vtn_ssa_value
*cur
= src
;
2572 for (unsigned i
= 0; i
< num_indices
; i
++) {
2573 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2574 assert(i
== num_indices
- 1);
2575 /* According to the SPIR-V spec, OpCompositeExtract may work down to
2576 * the component granularity. The last index will be the index of the
2577 * vector to extract.
2580 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
2581 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
2582 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
2585 cur
= cur
->elems
[indices
[i
]];
2593 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
2594 const uint32_t *w
, unsigned count
)
2596 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2597 const struct glsl_type
*type
=
2598 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
2599 val
->ssa
= vtn_create_ssa_value(b
, type
);
2602 case SpvOpVectorExtractDynamic
:
2603 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2604 vtn_ssa_value(b
, w
[4])->def
);
2607 case SpvOpVectorInsertDynamic
:
2608 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2609 vtn_ssa_value(b
, w
[4])->def
,
2610 vtn_ssa_value(b
, w
[5])->def
);
2613 case SpvOpVectorShuffle
:
2614 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
2615 vtn_ssa_value(b
, w
[3])->def
,
2616 vtn_ssa_value(b
, w
[4])->def
,
2620 case SpvOpCompositeConstruct
: {
2621 unsigned elems
= count
- 3;
2622 if (glsl_type_is_vector_or_scalar(type
)) {
2623 nir_ssa_def
*srcs
[4];
2624 for (unsigned i
= 0; i
< elems
; i
++)
2625 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
2627 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
2630 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2631 for (unsigned i
= 0; i
< elems
; i
++)
2632 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
2636 case SpvOpCompositeExtract
:
2637 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
2641 case SpvOpCompositeInsert
:
2642 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
2643 vtn_ssa_value(b
, w
[3]),
2647 case SpvOpCopyObject
:
2648 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
2652 unreachable("unknown composite operation");
2657 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
2658 const uint32_t *w
, unsigned count
)
2660 nir_intrinsic_op intrinsic_op
;
2662 case SpvOpEmitVertex
:
2663 case SpvOpEmitStreamVertex
:
2664 intrinsic_op
= nir_intrinsic_emit_vertex
;
2666 case SpvOpEndPrimitive
:
2667 case SpvOpEndStreamPrimitive
:
2668 intrinsic_op
= nir_intrinsic_end_primitive
;
2670 case SpvOpMemoryBarrier
:
2671 intrinsic_op
= nir_intrinsic_memory_barrier
;
2673 case SpvOpControlBarrier
:
2674 intrinsic_op
= nir_intrinsic_barrier
;
2677 unreachable("unknown barrier instruction");
2680 nir_intrinsic_instr
*intrin
=
2681 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
2683 if (opcode
== SpvOpEmitStreamVertex
|| opcode
== SpvOpEndStreamPrimitive
)
2684 nir_intrinsic_set_stream_id(intrin
, w
[1]);
2686 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2690 gl_primitive_from_spv_execution_mode(SpvExecutionMode mode
)
2693 case SpvExecutionModeInputPoints
:
2694 case SpvExecutionModeOutputPoints
:
2695 return 0; /* GL_POINTS */
2696 case SpvExecutionModeInputLines
:
2697 return 1; /* GL_LINES */
2698 case SpvExecutionModeInputLinesAdjacency
:
2699 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
2700 case SpvExecutionModeTriangles
:
2701 return 4; /* GL_TRIANGLES */
2702 case SpvExecutionModeInputTrianglesAdjacency
:
2703 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
2704 case SpvExecutionModeQuads
:
2705 return 7; /* GL_QUADS */
2706 case SpvExecutionModeIsolines
:
2707 return 0x8E7A; /* GL_ISOLINES */
2708 case SpvExecutionModeOutputLineStrip
:
2709 return 3; /* GL_LINE_STRIP */
2710 case SpvExecutionModeOutputTriangleStrip
:
2711 return 5; /* GL_TRIANGLE_STRIP */
2713 unreachable("Invalid primitive type");
2719 vertices_in_from_spv_execution_mode(SpvExecutionMode mode
)
2722 case SpvExecutionModeInputPoints
:
2724 case SpvExecutionModeInputLines
:
2726 case SpvExecutionModeInputLinesAdjacency
:
2728 case SpvExecutionModeTriangles
:
2730 case SpvExecutionModeInputTrianglesAdjacency
:
2733 unreachable("Invalid GS input mode");
2738 static gl_shader_stage
2739 stage_for_execution_model(SpvExecutionModel model
)
2742 case SpvExecutionModelVertex
:
2743 return MESA_SHADER_VERTEX
;
2744 case SpvExecutionModelTessellationControl
:
2745 return MESA_SHADER_TESS_CTRL
;
2746 case SpvExecutionModelTessellationEvaluation
:
2747 return MESA_SHADER_TESS_EVAL
;
2748 case SpvExecutionModelGeometry
:
2749 return MESA_SHADER_GEOMETRY
;
2750 case SpvExecutionModelFragment
:
2751 return MESA_SHADER_FRAGMENT
;
2752 case SpvExecutionModelGLCompute
:
2753 return MESA_SHADER_COMPUTE
;
2755 unreachable("Unsupported execution model");
2759 #define spv_check_supported(name, cap) do { \
2760 if (!(b->options && b->options->caps.name)) \
2761 vtn_warn("Unsupported SPIR-V capability: %s", \
2762 spirv_capability_to_string(cap)); \
2766 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2767 const uint32_t *w
, unsigned count
)
2774 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
2775 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
2776 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
2777 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
2778 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
2779 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
2782 uint32_t version
= w
[2];
2785 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
2787 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
2791 case SpvOpSourceExtension
:
2792 case SpvOpSourceContinued
:
2793 case SpvOpExtension
:
2794 /* Unhandled, but these are for debug so that's ok. */
2797 case SpvOpCapability
: {
2798 SpvCapability cap
= w
[1];
2800 case SpvCapabilityMatrix
:
2801 case SpvCapabilityShader
:
2802 case SpvCapabilityGeometry
:
2803 case SpvCapabilityGeometryPointSize
:
2804 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
2805 case SpvCapabilitySampledImageArrayDynamicIndexing
:
2806 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
2807 case SpvCapabilityStorageImageArrayDynamicIndexing
:
2808 case SpvCapabilityImageRect
:
2809 case SpvCapabilitySampledRect
:
2810 case SpvCapabilitySampled1D
:
2811 case SpvCapabilityImage1D
:
2812 case SpvCapabilitySampledCubeArray
:
2813 case SpvCapabilityImageCubeArray
:
2814 case SpvCapabilitySampledBuffer
:
2815 case SpvCapabilityImageBuffer
:
2816 case SpvCapabilityImageQuery
:
2817 case SpvCapabilityDerivativeControl
:
2818 case SpvCapabilityInterpolationFunction
:
2819 case SpvCapabilityMultiViewport
:
2820 case SpvCapabilitySampleRateShading
:
2821 case SpvCapabilityClipDistance
:
2822 case SpvCapabilityCullDistance
:
2823 case SpvCapabilityInputAttachment
:
2824 case SpvCapabilityImageGatherExtended
:
2825 case SpvCapabilityStorageImageExtendedFormats
:
2828 case SpvCapabilityGeometryStreams
:
2829 case SpvCapabilityLinkage
:
2830 case SpvCapabilityVector16
:
2831 case SpvCapabilityFloat16Buffer
:
2832 case SpvCapabilityFloat16
:
2833 case SpvCapabilityInt64Atomics
:
2834 case SpvCapabilityAtomicStorage
:
2835 case SpvCapabilityInt16
:
2836 case SpvCapabilityStorageImageMultisample
:
2837 case SpvCapabilityInt8
:
2838 case SpvCapabilitySparseResidency
:
2839 case SpvCapabilityMinLod
:
2840 case SpvCapabilityTransformFeedback
:
2841 vtn_warn("Unsupported SPIR-V capability: %s",
2842 spirv_capability_to_string(cap
));
2845 case SpvCapabilityFloat64
:
2846 spv_check_supported(float64
, cap
);
2848 case SpvCapabilityInt64
:
2849 spv_check_supported(int64
, cap
);
2852 case SpvCapabilityAddresses
:
2853 case SpvCapabilityKernel
:
2854 case SpvCapabilityImageBasic
:
2855 case SpvCapabilityImageReadWrite
:
2856 case SpvCapabilityImageMipmap
:
2857 case SpvCapabilityPipes
:
2858 case SpvCapabilityGroups
:
2859 case SpvCapabilityDeviceEnqueue
:
2860 case SpvCapabilityLiteralSampler
:
2861 case SpvCapabilityGenericPointer
:
2862 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
2863 spirv_capability_to_string(cap
));
2866 case SpvCapabilityImageMSArray
:
2867 spv_check_supported(image_ms_array
, cap
);
2870 case SpvCapabilityTessellation
:
2871 case SpvCapabilityTessellationPointSize
:
2872 spv_check_supported(tessellation
, cap
);
2875 case SpvCapabilityDrawParameters
:
2876 spv_check_supported(draw_parameters
, cap
);
2879 case SpvCapabilityStorageImageReadWithoutFormat
:
2880 spv_check_supported(image_read_without_format
, cap
);
2883 case SpvCapabilityStorageImageWriteWithoutFormat
:
2884 spv_check_supported(image_write_without_format
, cap
);
2887 case SpvCapabilityMultiView
:
2888 spv_check_supported(multiview
, cap
);
2891 case SpvCapabilityVariablePointersStorageBuffer
:
2892 case SpvCapabilityVariablePointers
:
2893 spv_check_supported(variable_pointers
, cap
);
2897 unreachable("Unhandled capability");
2902 case SpvOpExtInstImport
:
2903 vtn_handle_extension(b
, opcode
, w
, count
);
2906 case SpvOpMemoryModel
:
2907 assert(w
[1] == SpvAddressingModelLogical
);
2908 assert(w
[2] == SpvMemoryModelSimple
||
2909 w
[2] == SpvMemoryModelGLSL450
);
2912 case SpvOpEntryPoint
: {
2913 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
2914 /* Let this be a name label regardless */
2915 unsigned name_words
;
2916 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
2918 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
2919 stage_for_execution_model(w
[1]) != b
->entry_point_stage
)
2922 assert(b
->entry_point
== NULL
);
2923 b
->entry_point
= entry_point
;
2928 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
2929 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2933 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2936 case SpvOpMemberName
:
2940 case SpvOpExecutionMode
:
2941 case SpvOpDecorationGroup
:
2943 case SpvOpMemberDecorate
:
2944 case SpvOpGroupDecorate
:
2945 case SpvOpGroupMemberDecorate
:
2946 vtn_handle_decoration(b
, opcode
, w
, count
);
2950 return false; /* End of preamble */
2957 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
2958 const struct vtn_decoration
*mode
, void *data
)
2960 assert(b
->entry_point
== entry_point
);
2962 switch(mode
->exec_mode
) {
2963 case SpvExecutionModeOriginUpperLeft
:
2964 case SpvExecutionModeOriginLowerLeft
:
2965 b
->origin_upper_left
=
2966 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
2969 case SpvExecutionModeEarlyFragmentTests
:
2970 assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
2971 b
->shader
->info
.fs
.early_fragment_tests
= true;
2974 case SpvExecutionModeInvocations
:
2975 assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
2976 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->literals
[0]);
2979 case SpvExecutionModeDepthReplacing
:
2980 assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
2981 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
2983 case SpvExecutionModeDepthGreater
:
2984 assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
2985 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
2987 case SpvExecutionModeDepthLess
:
2988 assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
2989 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
2991 case SpvExecutionModeDepthUnchanged
:
2992 assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
2993 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
2996 case SpvExecutionModeLocalSize
:
2997 assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
2998 b
->shader
->info
.cs
.local_size
[0] = mode
->literals
[0];
2999 b
->shader
->info
.cs
.local_size
[1] = mode
->literals
[1];
3000 b
->shader
->info
.cs
.local_size
[2] = mode
->literals
[2];
3002 case SpvExecutionModeLocalSizeHint
:
3003 break; /* Nothing to do with this */
3005 case SpvExecutionModeOutputVertices
:
3006 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3007 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3008 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->literals
[0];
3010 assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3011 b
->shader
->info
.gs
.vertices_out
= mode
->literals
[0];
3015 case SpvExecutionModeInputPoints
:
3016 case SpvExecutionModeInputLines
:
3017 case SpvExecutionModeInputLinesAdjacency
:
3018 case SpvExecutionModeTriangles
:
3019 case SpvExecutionModeInputTrianglesAdjacency
:
3020 case SpvExecutionModeQuads
:
3021 case SpvExecutionModeIsolines
:
3022 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3023 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3024 b
->shader
->info
.tess
.primitive_mode
=
3025 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
3027 assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3028 b
->shader
->info
.gs
.vertices_in
=
3029 vertices_in_from_spv_execution_mode(mode
->exec_mode
);
3033 case SpvExecutionModeOutputPoints
:
3034 case SpvExecutionModeOutputLineStrip
:
3035 case SpvExecutionModeOutputTriangleStrip
:
3036 assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
3037 b
->shader
->info
.gs
.output_primitive
=
3038 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
3041 case SpvExecutionModeSpacingEqual
:
3042 assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3043 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3044 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
3046 case SpvExecutionModeSpacingFractionalEven
:
3047 assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3048 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3049 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
3051 case SpvExecutionModeSpacingFractionalOdd
:
3052 assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3053 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3054 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
3056 case SpvExecutionModeVertexOrderCw
:
3057 assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3058 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3059 b
->shader
->info
.tess
.ccw
= false;
3061 case SpvExecutionModeVertexOrderCcw
:
3062 assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3063 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3064 b
->shader
->info
.tess
.ccw
= true;
3066 case SpvExecutionModePointMode
:
3067 assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
3068 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
3069 b
->shader
->info
.tess
.point_mode
= true;
3072 case SpvExecutionModePixelCenterInteger
:
3073 b
->pixel_center_integer
= true;
3076 case SpvExecutionModeXfb
:
3077 unreachable("Unhandled execution mode");
3080 case SpvExecutionModeVecTypeHint
:
3081 case SpvExecutionModeContractionOff
:
3085 unreachable("Unhandled execution mode");
3090 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3091 const uint32_t *w
, unsigned count
)
3095 case SpvOpSourceContinued
:
3096 case SpvOpSourceExtension
:
3097 case SpvOpExtension
:
3098 case SpvOpCapability
:
3099 case SpvOpExtInstImport
:
3100 case SpvOpMemoryModel
:
3101 case SpvOpEntryPoint
:
3102 case SpvOpExecutionMode
:
3105 case SpvOpMemberName
:
3106 case SpvOpDecorationGroup
:
3108 case SpvOpMemberDecorate
:
3109 case SpvOpGroupDecorate
:
3110 case SpvOpGroupMemberDecorate
:
3111 unreachable("Invalid opcode types and variables section");
3117 case SpvOpTypeFloat
:
3118 case SpvOpTypeVector
:
3119 case SpvOpTypeMatrix
:
3120 case SpvOpTypeImage
:
3121 case SpvOpTypeSampler
:
3122 case SpvOpTypeSampledImage
:
3123 case SpvOpTypeArray
:
3124 case SpvOpTypeRuntimeArray
:
3125 case SpvOpTypeStruct
:
3126 case SpvOpTypeOpaque
:
3127 case SpvOpTypePointer
:
3128 case SpvOpTypeFunction
:
3129 case SpvOpTypeEvent
:
3130 case SpvOpTypeDeviceEvent
:
3131 case SpvOpTypeReserveId
:
3132 case SpvOpTypeQueue
:
3134 vtn_handle_type(b
, opcode
, w
, count
);
3137 case SpvOpConstantTrue
:
3138 case SpvOpConstantFalse
:
3140 case SpvOpConstantComposite
:
3141 case SpvOpConstantSampler
:
3142 case SpvOpConstantNull
:
3143 case SpvOpSpecConstantTrue
:
3144 case SpvOpSpecConstantFalse
:
3145 case SpvOpSpecConstant
:
3146 case SpvOpSpecConstantComposite
:
3147 case SpvOpSpecConstantOp
:
3148 vtn_handle_constant(b
, opcode
, w
, count
);
3153 vtn_handle_variables(b
, opcode
, w
, count
);
3157 return false; /* End of preamble */
3164 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3165 const uint32_t *w
, unsigned count
)
3171 case SpvOpLoopMerge
:
3172 case SpvOpSelectionMerge
:
3173 /* This is handled by cfg pre-pass and walk_blocks */
3177 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
3178 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3183 vtn_handle_extension(b
, opcode
, w
, count
);
3189 case SpvOpCopyMemory
:
3190 case SpvOpCopyMemorySized
:
3191 case SpvOpAccessChain
:
3192 case SpvOpPtrAccessChain
:
3193 case SpvOpInBoundsAccessChain
:
3194 case SpvOpArrayLength
:
3195 vtn_handle_variables(b
, opcode
, w
, count
);
3198 case SpvOpFunctionCall
:
3199 vtn_handle_function_call(b
, opcode
, w
, count
);
3202 case SpvOpSampledImage
:
3204 case SpvOpImageSampleImplicitLod
:
3205 case SpvOpImageSampleExplicitLod
:
3206 case SpvOpImageSampleDrefImplicitLod
:
3207 case SpvOpImageSampleDrefExplicitLod
:
3208 case SpvOpImageSampleProjImplicitLod
:
3209 case SpvOpImageSampleProjExplicitLod
:
3210 case SpvOpImageSampleProjDrefImplicitLod
:
3211 case SpvOpImageSampleProjDrefExplicitLod
:
3212 case SpvOpImageFetch
:
3213 case SpvOpImageGather
:
3214 case SpvOpImageDrefGather
:
3215 case SpvOpImageQuerySizeLod
:
3216 case SpvOpImageQueryLod
:
3217 case SpvOpImageQueryLevels
:
3218 case SpvOpImageQuerySamples
:
3219 vtn_handle_texture(b
, opcode
, w
, count
);
3222 case SpvOpImageRead
:
3223 case SpvOpImageWrite
:
3224 case SpvOpImageTexelPointer
:
3225 vtn_handle_image(b
, opcode
, w
, count
);
3228 case SpvOpImageQuerySize
: {
3229 struct vtn_pointer
*image
=
3230 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
3231 if (image
->mode
== vtn_variable_mode_image
) {
3232 vtn_handle_image(b
, opcode
, w
, count
);
3234 assert(image
->mode
== vtn_variable_mode_sampler
);
3235 vtn_handle_texture(b
, opcode
, w
, count
);
3240 case SpvOpAtomicLoad
:
3241 case SpvOpAtomicExchange
:
3242 case SpvOpAtomicCompareExchange
:
3243 case SpvOpAtomicCompareExchangeWeak
:
3244 case SpvOpAtomicIIncrement
:
3245 case SpvOpAtomicIDecrement
:
3246 case SpvOpAtomicIAdd
:
3247 case SpvOpAtomicISub
:
3248 case SpvOpAtomicSMin
:
3249 case SpvOpAtomicUMin
:
3250 case SpvOpAtomicSMax
:
3251 case SpvOpAtomicUMax
:
3252 case SpvOpAtomicAnd
:
3254 case SpvOpAtomicXor
: {
3255 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
3256 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
3257 vtn_handle_image(b
, opcode
, w
, count
);
3259 assert(pointer
->value_type
== vtn_value_type_pointer
);
3260 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
3265 case SpvOpAtomicStore
: {
3266 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
3267 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
3268 vtn_handle_image(b
, opcode
, w
, count
);
3270 assert(pointer
->value_type
== vtn_value_type_pointer
);
3271 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
3277 /* Handle OpSelect up-front here because it needs to be able to handle
3278 * pointers and not just regular vectors and scalars.
3280 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3281 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, res_type
->type
);
3282 ssa
->def
= nir_bcsel(&b
->nb
, vtn_ssa_value(b
, w
[3])->def
,
3283 vtn_ssa_value(b
, w
[4])->def
,
3284 vtn_ssa_value(b
, w
[5])->def
);
3285 vtn_push_ssa(b
, w
[2], res_type
, ssa
);
3294 case SpvOpConvertFToU
:
3295 case SpvOpConvertFToS
:
3296 case SpvOpConvertSToF
:
3297 case SpvOpConvertUToF
:
3301 case SpvOpQuantizeToF16
:
3302 case SpvOpConvertPtrToU
:
3303 case SpvOpConvertUToPtr
:
3304 case SpvOpPtrCastToGeneric
:
3305 case SpvOpGenericCastToPtr
:
3311 case SpvOpSignBitSet
:
3312 case SpvOpLessOrGreater
:
3314 case SpvOpUnordered
:
3329 case SpvOpVectorTimesScalar
:
3331 case SpvOpIAddCarry
:
3332 case SpvOpISubBorrow
:
3333 case SpvOpUMulExtended
:
3334 case SpvOpSMulExtended
:
3335 case SpvOpShiftRightLogical
:
3336 case SpvOpShiftRightArithmetic
:
3337 case SpvOpShiftLeftLogical
:
3338 case SpvOpLogicalEqual
:
3339 case SpvOpLogicalNotEqual
:
3340 case SpvOpLogicalOr
:
3341 case SpvOpLogicalAnd
:
3342 case SpvOpLogicalNot
:
3343 case SpvOpBitwiseOr
:
3344 case SpvOpBitwiseXor
:
3345 case SpvOpBitwiseAnd
:
3347 case SpvOpFOrdEqual
:
3348 case SpvOpFUnordEqual
:
3349 case SpvOpINotEqual
:
3350 case SpvOpFOrdNotEqual
:
3351 case SpvOpFUnordNotEqual
:
3352 case SpvOpULessThan
:
3353 case SpvOpSLessThan
:
3354 case SpvOpFOrdLessThan
:
3355 case SpvOpFUnordLessThan
:
3356 case SpvOpUGreaterThan
:
3357 case SpvOpSGreaterThan
:
3358 case SpvOpFOrdGreaterThan
:
3359 case SpvOpFUnordGreaterThan
:
3360 case SpvOpULessThanEqual
:
3361 case SpvOpSLessThanEqual
:
3362 case SpvOpFOrdLessThanEqual
:
3363 case SpvOpFUnordLessThanEqual
:
3364 case SpvOpUGreaterThanEqual
:
3365 case SpvOpSGreaterThanEqual
:
3366 case SpvOpFOrdGreaterThanEqual
:
3367 case SpvOpFUnordGreaterThanEqual
:
3373 case SpvOpFwidthFine
:
3374 case SpvOpDPdxCoarse
:
3375 case SpvOpDPdyCoarse
:
3376 case SpvOpFwidthCoarse
:
3377 case SpvOpBitFieldInsert
:
3378 case SpvOpBitFieldSExtract
:
3379 case SpvOpBitFieldUExtract
:
3380 case SpvOpBitReverse
:
3382 case SpvOpTranspose
:
3383 case SpvOpOuterProduct
:
3384 case SpvOpMatrixTimesScalar
:
3385 case SpvOpVectorTimesMatrix
:
3386 case SpvOpMatrixTimesVector
:
3387 case SpvOpMatrixTimesMatrix
:
3388 vtn_handle_alu(b
, opcode
, w
, count
);
3391 case SpvOpVectorExtractDynamic
:
3392 case SpvOpVectorInsertDynamic
:
3393 case SpvOpVectorShuffle
:
3394 case SpvOpCompositeConstruct
:
3395 case SpvOpCompositeExtract
:
3396 case SpvOpCompositeInsert
:
3397 case SpvOpCopyObject
:
3398 vtn_handle_composite(b
, opcode
, w
, count
);
3401 case SpvOpEmitVertex
:
3402 case SpvOpEndPrimitive
:
3403 case SpvOpEmitStreamVertex
:
3404 case SpvOpEndStreamPrimitive
:
3405 case SpvOpControlBarrier
:
3406 case SpvOpMemoryBarrier
:
3407 vtn_handle_barrier(b
, opcode
, w
, count
);
3411 unreachable("Unhandled opcode");
3418 spirv_to_nir(const uint32_t *words
, size_t word_count
,
3419 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
3420 gl_shader_stage stage
, const char *entry_point_name
,
3421 const struct spirv_to_nir_options
*options
,
3422 const nir_shader_compiler_options
*nir_options
)
3424 /* Initialize the stn_builder object */
3425 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
3430 exec_list_make_empty(&b
->functions
);
3431 b
->entry_point_stage
= stage
;
3432 b
->entry_point_name
= entry_point_name
;
3433 b
->options
= options
;
3435 /* See also _vtn_fail() */
3436 if (setjmp(b
->fail_jump
)) {
3441 const uint32_t *word_end
= words
+ word_count
;
3443 /* Handle the SPIR-V header (first 4 dwords) */
3444 assert(word_count
> 5);
3446 assert(words
[0] == SpvMagicNumber
);
3447 assert(words
[1] >= 0x10000);
3448 /* words[2] == generator magic */
3449 unsigned value_id_bound
= words
[3];
3450 assert(words
[4] == 0);
3454 b
->value_id_bound
= value_id_bound
;
3455 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
3457 /* Handle all the preamble instructions */
3458 words
= vtn_foreach_instruction(b
, words
, word_end
,
3459 vtn_handle_preamble_instruction
);
3461 if (b
->entry_point
== NULL
) {
3462 assert(!"Entry point not found");
3467 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
3469 /* Set shader info defaults */
3470 b
->shader
->info
.gs
.invocations
= 1;
3472 /* Parse execution modes */
3473 vtn_foreach_execution_mode(b
, b
->entry_point
,
3474 vtn_handle_execution_mode
, NULL
);
3476 b
->specializations
= spec
;
3477 b
->num_specializations
= num_spec
;
3479 /* Handle all variable, type, and constant instructions */
3480 words
= vtn_foreach_instruction(b
, words
, word_end
,
3481 vtn_handle_variable_or_type_instruction
);
3483 vtn_build_cfg(b
, words
, word_end
);
3485 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
3486 b
->entry_point
->func
->referenced
= true;
3491 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
3492 if (func
->referenced
&& !func
->emitted
) {
3493 b
->const_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
3494 _mesa_key_pointer_equal
);
3496 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
3502 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
3503 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
3504 assert(entry_point
);
3506 /* Unparent the shader from the vtn_builder before we delete the builder */
3507 ralloc_steal(NULL
, b
->shader
);