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"
34 struct spec_constant_value
{
43 _vtn_warn(const char *file
, int line
, const char *msg
, ...)
49 formatted
= ralloc_vasprintf(NULL
, msg
, args
);
52 fprintf(stderr
, "%s:%d WARNING: %s\n", file
, line
, formatted
);
54 ralloc_free(formatted
);
57 static struct vtn_ssa_value
*
58 vtn_undef_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
60 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
63 if (glsl_type_is_vector_or_scalar(type
)) {
64 unsigned num_components
= glsl_get_vector_elements(val
->type
);
65 unsigned bit_size
= glsl_get_bit_size(val
->type
);
66 val
->def
= nir_ssa_undef(&b
->nb
, num_components
, bit_size
);
68 unsigned elems
= glsl_get_length(val
->type
);
69 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
70 if (glsl_type_is_matrix(type
)) {
71 const struct glsl_type
*elem_type
=
72 glsl_vector_type(glsl_get_base_type(type
),
73 glsl_get_vector_elements(type
));
75 for (unsigned i
= 0; i
< elems
; i
++)
76 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
77 } else if (glsl_type_is_array(type
)) {
78 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
79 for (unsigned i
= 0; i
< elems
; i
++)
80 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
82 for (unsigned i
= 0; i
< elems
; i
++) {
83 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
84 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
92 static struct vtn_ssa_value
*
93 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
94 const struct glsl_type
*type
)
96 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
101 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
104 switch (glsl_get_base_type(type
)) {
107 case GLSL_TYPE_INT64
:
108 case GLSL_TYPE_UINT64
:
110 case GLSL_TYPE_FLOAT
:
111 case GLSL_TYPE_DOUBLE
: {
112 int bit_size
= glsl_get_bit_size(type
);
113 if (glsl_type_is_vector_or_scalar(type
)) {
114 unsigned num_components
= glsl_get_vector_elements(val
->type
);
115 nir_load_const_instr
*load
=
116 nir_load_const_instr_create(b
->shader
, num_components
, bit_size
);
118 load
->value
= constant
->values
[0];
120 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
121 val
->def
= &load
->def
;
123 assert(glsl_type_is_matrix(type
));
124 unsigned rows
= glsl_get_vector_elements(val
->type
);
125 unsigned columns
= glsl_get_matrix_columns(val
->type
);
126 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, columns
);
128 for (unsigned i
= 0; i
< columns
; i
++) {
129 struct vtn_ssa_value
*col_val
= rzalloc(b
, struct vtn_ssa_value
);
130 col_val
->type
= glsl_get_column_type(val
->type
);
131 nir_load_const_instr
*load
=
132 nir_load_const_instr_create(b
->shader
, rows
, bit_size
);
134 load
->value
= constant
->values
[i
];
136 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
137 col_val
->def
= &load
->def
;
139 val
->elems
[i
] = col_val
;
145 case GLSL_TYPE_ARRAY
: {
146 unsigned elems
= glsl_get_length(val
->type
);
147 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
148 const struct glsl_type
*elem_type
= glsl_get_array_element(val
->type
);
149 for (unsigned i
= 0; i
< elems
; i
++)
150 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
155 case GLSL_TYPE_STRUCT
: {
156 unsigned elems
= glsl_get_length(val
->type
);
157 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
158 for (unsigned i
= 0; i
< elems
; i
++) {
159 const struct glsl_type
*elem_type
=
160 glsl_get_struct_field(val
->type
, i
);
161 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
168 unreachable("bad constant type");
174 struct vtn_ssa_value
*
175 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
177 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
178 switch (val
->value_type
) {
179 case vtn_value_type_undef
:
180 return vtn_undef_ssa_value(b
, val
->type
->type
);
182 case vtn_value_type_constant
:
183 return vtn_const_ssa_value(b
, val
->constant
, val
->const_type
);
185 case vtn_value_type_ssa
:
188 case vtn_value_type_pointer
:
189 assert(val
->pointer
->ptr_type
&& val
->pointer
->ptr_type
->type
);
190 struct vtn_ssa_value
*ssa
=
191 vtn_create_ssa_value(b
, val
->pointer
->ptr_type
->type
);
192 ssa
->def
= vtn_pointer_to_ssa(b
, val
->pointer
);
196 unreachable("Invalid type for an SSA value");
201 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
202 unsigned word_count
, unsigned *words_used
)
204 char *dup
= ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
206 /* Ammount of space taken by the string (including the null) */
207 unsigned len
= strlen(dup
) + 1;
208 *words_used
= DIV_ROUND_UP(len
, sizeof(*words
));
214 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
215 const uint32_t *end
, vtn_instruction_handler handler
)
221 const uint32_t *w
= start
;
223 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
224 unsigned count
= w
[0] >> SpvWordCountShift
;
225 assert(count
>= 1 && w
+ count
<= end
);
229 break; /* Do nothing */
232 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
244 if (!handler(b
, opcode
, w
, count
))
256 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
257 const uint32_t *w
, unsigned count
)
260 case SpvOpExtInstImport
: {
261 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
262 if (strcmp((const char *)&w
[2], "GLSL.std.450") == 0) {
263 val
->ext_handler
= vtn_handle_glsl450_instruction
;
265 unreachable("Unsupported extension");
271 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
272 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
279 unreachable("Unhandled opcode");
284 _foreach_decoration_helper(struct vtn_builder
*b
,
285 struct vtn_value
*base_value
,
287 struct vtn_value
*value
,
288 vtn_decoration_foreach_cb cb
, void *data
)
290 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
292 if (dec
->scope
== VTN_DEC_DECORATION
) {
293 member
= parent_member
;
294 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
295 assert(parent_member
== -1);
296 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
298 /* Not a decoration */
303 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
304 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
307 cb(b
, base_value
, member
, dec
, data
);
312 /** Iterates (recursively if needed) over all of the decorations on a value
314 * This function iterates over all of the decorations applied to a given
315 * value. If it encounters a decoration group, it recurses into the group
316 * and iterates over all of those decorations as well.
319 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
320 vtn_decoration_foreach_cb cb
, void *data
)
322 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
326 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
327 vtn_execution_mode_foreach_cb cb
, void *data
)
329 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
330 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
333 assert(dec
->group
== NULL
);
334 cb(b
, value
, dec
, data
);
339 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
340 const uint32_t *w
, unsigned count
)
342 const uint32_t *w_end
= w
+ count
;
343 const uint32_t target
= w
[1];
347 case SpvOpDecorationGroup
:
348 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
352 case SpvOpMemberDecorate
:
353 case SpvOpExecutionMode
: {
354 struct vtn_value
*val
= &b
->values
[target
];
356 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
359 dec
->scope
= VTN_DEC_DECORATION
;
361 case SpvOpMemberDecorate
:
362 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
364 case SpvOpExecutionMode
:
365 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
368 unreachable("Invalid decoration opcode");
370 dec
->decoration
= *(w
++);
373 /* Link into the list */
374 dec
->next
= val
->decoration
;
375 val
->decoration
= dec
;
379 case SpvOpGroupMemberDecorate
:
380 case SpvOpGroupDecorate
: {
381 struct vtn_value
*group
=
382 vtn_value(b
, target
, vtn_value_type_decoration_group
);
384 for (; w
< w_end
; w
++) {
385 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
386 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
389 if (opcode
== SpvOpGroupDecorate
) {
390 dec
->scope
= VTN_DEC_DECORATION
;
392 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
395 /* Link into the list */
396 dec
->next
= val
->decoration
;
397 val
->decoration
= dec
;
403 unreachable("Unhandled opcode");
407 struct member_decoration_ctx
{
409 struct glsl_struct_field
*fields
;
410 struct vtn_type
*type
;
413 /* does a shallow copy of a vtn_type */
415 static struct vtn_type
*
416 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
418 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
421 switch (src
->base_type
) {
422 case vtn_base_type_void
:
423 case vtn_base_type_scalar
:
424 case vtn_base_type_vector
:
425 case vtn_base_type_matrix
:
426 case vtn_base_type_array
:
427 case vtn_base_type_pointer
:
428 case vtn_base_type_image
:
429 case vtn_base_type_sampler
:
430 /* Nothing more to do */
433 case vtn_base_type_struct
:
434 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
435 memcpy(dest
->members
, src
->members
,
436 src
->length
* sizeof(src
->members
[0]));
438 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
439 memcpy(dest
->offsets
, src
->offsets
,
440 src
->length
* sizeof(src
->offsets
[0]));
443 case vtn_base_type_function
:
444 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
445 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
452 static struct vtn_type
*
453 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
455 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
456 type
= type
->members
[member
];
458 /* We may have an array of matrices.... Oh, joy! */
459 while (glsl_type_is_array(type
->type
)) {
460 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
461 type
= type
->array_element
;
464 assert(glsl_type_is_matrix(type
->type
));
470 struct_member_decoration_cb(struct vtn_builder
*b
,
471 struct vtn_value
*val
, int member
,
472 const struct vtn_decoration
*dec
, void *void_ctx
)
474 struct member_decoration_ctx
*ctx
= void_ctx
;
479 assert(member
< ctx
->num_fields
);
481 switch (dec
->decoration
) {
482 case SpvDecorationNonWritable
:
483 case SpvDecorationNonReadable
:
484 case SpvDecorationRelaxedPrecision
:
485 case SpvDecorationVolatile
:
486 case SpvDecorationCoherent
:
487 case SpvDecorationUniform
:
488 break; /* FIXME: Do nothing with this for now. */
489 case SpvDecorationNoPerspective
:
490 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
492 case SpvDecorationFlat
:
493 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
495 case SpvDecorationCentroid
:
496 ctx
->fields
[member
].centroid
= true;
498 case SpvDecorationSample
:
499 ctx
->fields
[member
].sample
= true;
501 case SpvDecorationStream
:
502 /* Vulkan only allows one GS stream */
503 assert(dec
->literals
[0] == 0);
505 case SpvDecorationLocation
:
506 ctx
->fields
[member
].location
= dec
->literals
[0];
508 case SpvDecorationComponent
:
509 break; /* FIXME: What should we do with these? */
510 case SpvDecorationBuiltIn
:
511 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
512 ctx
->type
->members
[member
]->is_builtin
= true;
513 ctx
->type
->members
[member
]->builtin
= dec
->literals
[0];
514 ctx
->type
->builtin_block
= true;
516 case SpvDecorationOffset
:
517 ctx
->type
->offsets
[member
] = dec
->literals
[0];
519 case SpvDecorationMatrixStride
:
520 /* Handled as a second pass */
522 case SpvDecorationColMajor
:
523 break; /* Nothing to do here. Column-major is the default. */
524 case SpvDecorationRowMajor
:
525 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
528 case SpvDecorationPatch
:
531 case SpvDecorationSpecId
:
532 case SpvDecorationBlock
:
533 case SpvDecorationBufferBlock
:
534 case SpvDecorationArrayStride
:
535 case SpvDecorationGLSLShared
:
536 case SpvDecorationGLSLPacked
:
537 case SpvDecorationInvariant
:
538 case SpvDecorationRestrict
:
539 case SpvDecorationAliased
:
540 case SpvDecorationConstant
:
541 case SpvDecorationIndex
:
542 case SpvDecorationBinding
:
543 case SpvDecorationDescriptorSet
:
544 case SpvDecorationLinkageAttributes
:
545 case SpvDecorationNoContraction
:
546 case SpvDecorationInputAttachmentIndex
:
547 vtn_warn("Decoration not allowed on struct members: %s",
548 spirv_decoration_to_string(dec
->decoration
));
551 case SpvDecorationXfbBuffer
:
552 case SpvDecorationXfbStride
:
553 vtn_warn("Vulkan does not have transform feedback");
556 case SpvDecorationCPacked
:
557 case SpvDecorationSaturatedConversion
:
558 case SpvDecorationFuncParamAttr
:
559 case SpvDecorationFPRoundingMode
:
560 case SpvDecorationFPFastMathMode
:
561 case SpvDecorationAlignment
:
562 vtn_warn("Decoration only allowed for CL-style kernels: %s",
563 spirv_decoration_to_string(dec
->decoration
));
567 unreachable("Unhandled decoration");
571 /* Matrix strides are handled as a separate pass because we need to know
572 * whether the matrix is row-major or not first.
575 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
576 struct vtn_value
*val
, int member
,
577 const struct vtn_decoration
*dec
,
580 if (dec
->decoration
!= SpvDecorationMatrixStride
)
584 struct member_decoration_ctx
*ctx
= void_ctx
;
586 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
587 if (mat_type
->row_major
) {
588 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
589 mat_type
->stride
= mat_type
->array_element
->stride
;
590 mat_type
->array_element
->stride
= dec
->literals
[0];
592 assert(mat_type
->array_element
->stride
> 0);
593 mat_type
->stride
= dec
->literals
[0];
598 type_decoration_cb(struct vtn_builder
*b
,
599 struct vtn_value
*val
, int member
,
600 const struct vtn_decoration
*dec
, void *ctx
)
602 struct vtn_type
*type
= val
->type
;
607 switch (dec
->decoration
) {
608 case SpvDecorationArrayStride
:
609 assert(type
->base_type
== vtn_base_type_matrix
||
610 type
->base_type
== vtn_base_type_array
||
611 type
->base_type
== vtn_base_type_pointer
);
612 type
->stride
= dec
->literals
[0];
614 case SpvDecorationBlock
:
615 assert(type
->base_type
== vtn_base_type_struct
);
618 case SpvDecorationBufferBlock
:
619 assert(type
->base_type
== vtn_base_type_struct
);
620 type
->buffer_block
= true;
622 case SpvDecorationGLSLShared
:
623 case SpvDecorationGLSLPacked
:
624 /* Ignore these, since we get explicit offsets anyways */
627 case SpvDecorationRowMajor
:
628 case SpvDecorationColMajor
:
629 case SpvDecorationMatrixStride
:
630 case SpvDecorationBuiltIn
:
631 case SpvDecorationNoPerspective
:
632 case SpvDecorationFlat
:
633 case SpvDecorationPatch
:
634 case SpvDecorationCentroid
:
635 case SpvDecorationSample
:
636 case SpvDecorationVolatile
:
637 case SpvDecorationCoherent
:
638 case SpvDecorationNonWritable
:
639 case SpvDecorationNonReadable
:
640 case SpvDecorationUniform
:
641 case SpvDecorationStream
:
642 case SpvDecorationLocation
:
643 case SpvDecorationComponent
:
644 case SpvDecorationOffset
:
645 case SpvDecorationXfbBuffer
:
646 case SpvDecorationXfbStride
:
647 vtn_warn("Decoration only allowed for struct members: %s",
648 spirv_decoration_to_string(dec
->decoration
));
651 case SpvDecorationRelaxedPrecision
:
652 case SpvDecorationSpecId
:
653 case SpvDecorationInvariant
:
654 case SpvDecorationRestrict
:
655 case SpvDecorationAliased
:
656 case SpvDecorationConstant
:
657 case SpvDecorationIndex
:
658 case SpvDecorationBinding
:
659 case SpvDecorationDescriptorSet
:
660 case SpvDecorationLinkageAttributes
:
661 case SpvDecorationNoContraction
:
662 case SpvDecorationInputAttachmentIndex
:
663 vtn_warn("Decoration not allowed on types: %s",
664 spirv_decoration_to_string(dec
->decoration
));
667 case SpvDecorationCPacked
:
668 case SpvDecorationSaturatedConversion
:
669 case SpvDecorationFuncParamAttr
:
670 case SpvDecorationFPRoundingMode
:
671 case SpvDecorationFPFastMathMode
:
672 case SpvDecorationAlignment
:
673 vtn_warn("Decoration only allowed for CL-style kernels: %s",
674 spirv_decoration_to_string(dec
->decoration
));
678 unreachable("Unhandled decoration");
683 translate_image_format(SpvImageFormat format
)
686 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
687 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
688 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
689 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
690 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
691 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
692 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
693 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
694 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
695 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
696 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
697 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
698 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
699 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
700 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
701 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
702 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
703 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
704 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
705 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
706 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
707 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
708 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
709 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
710 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
711 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
712 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
713 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
714 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
715 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
716 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
717 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
718 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
719 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
720 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
721 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
722 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
723 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
724 case SpvImageFormatR16ui
: return 0x8234; /* GL_R16UI */
725 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
727 unreachable("Invalid image format");
733 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
734 const uint32_t *w
, unsigned count
)
736 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
738 val
->type
= rzalloc(b
, struct vtn_type
);
739 val
->type
->val
= val
;
743 val
->type
->base_type
= vtn_base_type_void
;
744 val
->type
->type
= glsl_void_type();
747 val
->type
->base_type
= vtn_base_type_scalar
;
748 val
->type
->type
= glsl_bool_type();
752 const bool signedness
= w
[3];
753 val
->type
->base_type
= vtn_base_type_scalar
;
755 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
757 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
760 case SpvOpTypeFloat
: {
762 val
->type
->base_type
= vtn_base_type_scalar
;
763 val
->type
->type
= bit_size
== 64 ? glsl_double_type() : glsl_float_type();
767 case SpvOpTypeVector
: {
768 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
769 unsigned elems
= w
[3];
771 assert(glsl_type_is_scalar(base
->type
));
772 val
->type
->base_type
= vtn_base_type_vector
;
773 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
774 val
->type
->stride
= glsl_get_bit_size(base
->type
) / 8;
775 val
->type
->array_element
= base
;
779 case SpvOpTypeMatrix
: {
780 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
781 unsigned columns
= w
[3];
783 assert(glsl_type_is_vector(base
->type
));
784 val
->type
->base_type
= vtn_base_type_matrix
;
785 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
786 glsl_get_vector_elements(base
->type
),
788 assert(!glsl_type_is_error(val
->type
->type
));
789 val
->type
->length
= columns
;
790 val
->type
->array_element
= base
;
791 val
->type
->row_major
= false;
792 val
->type
->stride
= 0;
796 case SpvOpTypeRuntimeArray
:
797 case SpvOpTypeArray
: {
798 struct vtn_type
*array_element
=
799 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
801 if (opcode
== SpvOpTypeRuntimeArray
) {
802 /* A length of 0 is used to denote unsized arrays */
803 val
->type
->length
= 0;
806 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
809 val
->type
->base_type
= vtn_base_type_array
;
810 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
);
811 val
->type
->array_element
= array_element
;
812 val
->type
->stride
= 0;
816 case SpvOpTypeStruct
: {
817 unsigned num_fields
= count
- 2;
818 val
->type
->base_type
= vtn_base_type_struct
;
819 val
->type
->length
= num_fields
;
820 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
821 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
823 NIR_VLA(struct glsl_struct_field
, fields
, count
);
824 for (unsigned i
= 0; i
< num_fields
; i
++) {
825 val
->type
->members
[i
] =
826 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
827 fields
[i
] = (struct glsl_struct_field
) {
828 .type
= val
->type
->members
[i
]->type
,
829 .name
= ralloc_asprintf(b
, "field%d", i
),
834 struct member_decoration_ctx ctx
= {
835 .num_fields
= num_fields
,
840 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
841 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
843 const char *name
= val
->name
? val
->name
: "struct";
845 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
);
849 case SpvOpTypeFunction
: {
850 val
->type
->base_type
= vtn_base_type_function
;
851 val
->type
->type
= NULL
;
853 val
->type
->return_type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
855 const unsigned num_params
= count
- 3;
856 val
->type
->length
= num_params
;
857 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
858 for (unsigned i
= 0; i
< count
- 3; i
++) {
859 val
->type
->params
[i
] =
860 vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
;
865 case SpvOpTypePointer
: {
866 SpvStorageClass storage_class
= w
[2];
867 struct vtn_type
*deref_type
=
868 vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
870 val
->type
->base_type
= vtn_base_type_pointer
;
871 val
->type
->storage_class
= storage_class
;
872 val
->type
->deref
= deref_type
;
874 if (storage_class
== SpvStorageClassUniform
||
875 storage_class
== SpvStorageClassStorageBuffer
) {
876 /* These can actually be stored to nir_variables and used as SSA
877 * values so they need a real glsl_type.
879 val
->type
->type
= glsl_vector_type(GLSL_TYPE_UINT
, 2);
884 case SpvOpTypeImage
: {
885 val
->type
->base_type
= vtn_base_type_image
;
887 const struct glsl_type
*sampled_type
=
888 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
890 assert(glsl_type_is_vector_or_scalar(sampled_type
));
892 enum glsl_sampler_dim dim
;
893 switch ((SpvDim
)w
[3]) {
894 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
895 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
896 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
897 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
898 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
899 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
900 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
902 unreachable("Invalid SPIR-V Sampler dimension");
905 bool is_shadow
= w
[4];
906 bool is_array
= w
[5];
907 bool multisampled
= w
[6];
908 unsigned sampled
= w
[7];
909 SpvImageFormat format
= w
[8];
912 val
->type
->access_qualifier
= w
[9];
914 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
917 if (dim
== GLSL_SAMPLER_DIM_2D
)
918 dim
= GLSL_SAMPLER_DIM_MS
;
919 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
920 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
922 unreachable("Unsupported multisampled image type");
925 val
->type
->image_format
= translate_image_format(format
);
928 val
->type
->sampled
= true;
929 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
930 glsl_get_base_type(sampled_type
));
931 } else if (sampled
== 2) {
933 val
->type
->sampled
= false;
934 val
->type
->type
= glsl_image_type(dim
, is_array
,
935 glsl_get_base_type(sampled_type
));
937 unreachable("We need to know if the image will be sampled");
942 case SpvOpTypeSampledImage
:
943 val
->type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
946 case SpvOpTypeSampler
:
947 /* The actual sampler type here doesn't really matter. It gets
948 * thrown away the moment you combine it with an image. What really
949 * matters is that it's a sampler type as opposed to an integer type
950 * so the backend knows what to do.
952 val
->type
->base_type
= vtn_base_type_sampler
;
953 val
->type
->type
= glsl_bare_sampler_type();
956 case SpvOpTypeOpaque
:
958 case SpvOpTypeDeviceEvent
:
959 case SpvOpTypeReserveId
:
963 unreachable("Unhandled opcode");
966 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
969 static nir_constant
*
970 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
972 nir_constant
*c
= rzalloc(b
, nir_constant
);
974 /* For pointers and other typeless things, we have to return something but
975 * it doesn't matter what.
980 switch (glsl_get_base_type(type
)) {
983 case GLSL_TYPE_INT64
:
984 case GLSL_TYPE_UINT64
:
986 case GLSL_TYPE_FLOAT
:
987 case GLSL_TYPE_DOUBLE
:
988 /* Nothing to do here. It's already initialized to zero */
991 case GLSL_TYPE_ARRAY
:
992 assert(glsl_get_length(type
) > 0);
993 c
->num_elements
= glsl_get_length(type
);
994 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
996 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
997 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
998 c
->elements
[i
] = c
->elements
[0];
1001 case GLSL_TYPE_STRUCT
:
1002 c
->num_elements
= glsl_get_length(type
);
1003 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1005 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
1006 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
1011 unreachable("Invalid type for null constant");
1018 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
1019 int member
, const struct vtn_decoration
*dec
,
1022 assert(member
== -1);
1023 if (dec
->decoration
!= SpvDecorationSpecId
)
1026 struct spec_constant_value
*const_value
= data
;
1028 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1029 if (b
->specializations
[i
].id
== dec
->literals
[0]) {
1030 if (const_value
->is_double
)
1031 const_value
->data64
= b
->specializations
[i
].data64
;
1033 const_value
->data32
= b
->specializations
[i
].data32
;
1040 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
1041 uint32_t const_value
)
1043 struct spec_constant_value data
;
1044 data
.is_double
= false;
1045 data
.data32
= const_value
;
1046 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1051 get_specialization64(struct vtn_builder
*b
, struct vtn_value
*val
,
1052 uint64_t const_value
)
1054 struct spec_constant_value data
;
1055 data
.is_double
= true;
1056 data
.data64
= const_value
;
1057 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1062 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1063 struct vtn_value
*val
,
1065 const struct vtn_decoration
*dec
,
1068 assert(member
== -1);
1069 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1070 dec
->literals
[0] != SpvBuiltInWorkgroupSize
)
1073 assert(val
->const_type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1075 b
->shader
->info
.cs
.local_size
[0] = val
->constant
->values
[0].u32
[0];
1076 b
->shader
->info
.cs
.local_size
[1] = val
->constant
->values
[0].u32
[1];
1077 b
->shader
->info
.cs
.local_size
[2] = val
->constant
->values
[0].u32
[2];
1081 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1082 const uint32_t *w
, unsigned count
)
1084 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1085 val
->const_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
1086 val
->constant
= rzalloc(b
, nir_constant
);
1088 case SpvOpConstantTrue
:
1089 assert(val
->const_type
== glsl_bool_type());
1090 val
->constant
->values
[0].u32
[0] = NIR_TRUE
;
1092 case SpvOpConstantFalse
:
1093 assert(val
->const_type
== glsl_bool_type());
1094 val
->constant
->values
[0].u32
[0] = NIR_FALSE
;
1097 case SpvOpSpecConstantTrue
:
1098 case SpvOpSpecConstantFalse
: {
1099 assert(val
->const_type
== glsl_bool_type());
1101 get_specialization(b
, val
, (opcode
== SpvOpSpecConstantTrue
));
1102 val
->constant
->values
[0].u32
[0] = int_val
? NIR_TRUE
: NIR_FALSE
;
1106 case SpvOpConstant
: {
1107 assert(glsl_type_is_scalar(val
->const_type
));
1108 int bit_size
= glsl_get_bit_size(val
->const_type
);
1109 if (bit_size
== 64) {
1110 val
->constant
->values
->u32
[0] = w
[3];
1111 val
->constant
->values
->u32
[1] = w
[4];
1113 assert(bit_size
== 32);
1114 val
->constant
->values
->u32
[0] = w
[3];
1118 case SpvOpSpecConstant
: {
1119 assert(glsl_type_is_scalar(val
->const_type
));
1120 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1121 int bit_size
= glsl_get_bit_size(val
->const_type
);
1123 val
->constant
->values
[0].u64
[0] =
1124 get_specialization64(b
, val
, vtn_u64_literal(&w
[3]));
1126 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1129 case SpvOpSpecConstantComposite
:
1130 case SpvOpConstantComposite
: {
1131 unsigned elem_count
= count
- 3;
1132 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1133 for (unsigned i
= 0; i
< elem_count
; i
++)
1134 elems
[i
] = vtn_value(b
, w
[i
+ 3], vtn_value_type_constant
)->constant
;
1136 switch (glsl_get_base_type(val
->const_type
)) {
1137 case GLSL_TYPE_UINT
:
1139 case GLSL_TYPE_UINT64
:
1140 case GLSL_TYPE_INT64
:
1141 case GLSL_TYPE_FLOAT
:
1142 case GLSL_TYPE_BOOL
:
1143 case GLSL_TYPE_DOUBLE
: {
1144 int bit_size
= glsl_get_bit_size(val
->const_type
);
1145 if (glsl_type_is_matrix(val
->const_type
)) {
1146 assert(glsl_get_matrix_columns(val
->const_type
) == elem_count
);
1147 for (unsigned i
= 0; i
< elem_count
; i
++)
1148 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1150 assert(glsl_type_is_vector(val
->const_type
));
1151 assert(glsl_get_vector_elements(val
->const_type
) == elem_count
);
1152 for (unsigned i
= 0; i
< elem_count
; i
++) {
1153 if (bit_size
== 64) {
1154 val
->constant
->values
[0].u64
[i
] = elems
[i
]->values
[0].u64
[0];
1156 assert(bit_size
== 32);
1157 val
->constant
->values
[0].u32
[i
] = elems
[i
]->values
[0].u32
[0];
1164 case GLSL_TYPE_STRUCT
:
1165 case GLSL_TYPE_ARRAY
:
1166 ralloc_steal(val
->constant
, elems
);
1167 val
->constant
->num_elements
= elem_count
;
1168 val
->constant
->elements
= elems
;
1172 unreachable("Unsupported type for constants");
1177 case SpvOpSpecConstantOp
: {
1178 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1180 case SpvOpVectorShuffle
: {
1181 struct vtn_value
*v0
= &b
->values
[w
[4]];
1182 struct vtn_value
*v1
= &b
->values
[w
[5]];
1184 assert(v0
->value_type
== vtn_value_type_constant
||
1185 v0
->value_type
== vtn_value_type_undef
);
1186 assert(v1
->value_type
== vtn_value_type_constant
||
1187 v1
->value_type
== vtn_value_type_undef
);
1189 unsigned len0
= v0
->value_type
== vtn_value_type_constant
?
1190 glsl_get_vector_elements(v0
->const_type
) :
1191 glsl_get_vector_elements(v0
->type
->type
);
1192 unsigned len1
= v1
->value_type
== vtn_value_type_constant
?
1193 glsl_get_vector_elements(v1
->const_type
) :
1194 glsl_get_vector_elements(v1
->type
->type
);
1196 assert(len0
+ len1
< 16);
1198 unsigned bit_size
= glsl_get_bit_size(val
->const_type
);
1199 unsigned bit_size0
= v0
->value_type
== vtn_value_type_constant
?
1200 glsl_get_bit_size(v0
->const_type
) :
1201 glsl_get_bit_size(v0
->type
->type
);
1202 unsigned bit_size1
= v1
->value_type
== vtn_value_type_constant
?
1203 glsl_get_bit_size(v1
->const_type
) :
1204 glsl_get_bit_size(v1
->type
->type
);
1206 assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1207 (void)bit_size0
; (void)bit_size1
;
1209 if (bit_size
== 64) {
1211 if (v0
->value_type
== vtn_value_type_constant
) {
1212 for (unsigned i
= 0; i
< len0
; i
++)
1213 u64
[i
] = v0
->constant
->values
[0].u64
[i
];
1215 if (v1
->value_type
== vtn_value_type_constant
) {
1216 for (unsigned i
= 0; i
< len1
; i
++)
1217 u64
[len0
+ i
] = v1
->constant
->values
[0].u64
[i
];
1220 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1221 uint32_t comp
= w
[i
+ 6];
1222 /* If component is not used, set the value to a known constant
1223 * to detect if it is wrongly used.
1225 if (comp
== (uint32_t)-1)
1226 val
->constant
->values
[0].u64
[j
] = 0xdeadbeefdeadbeef;
1228 val
->constant
->values
[0].u64
[j
] = u64
[comp
];
1232 if (v0
->value_type
== vtn_value_type_constant
) {
1233 for (unsigned i
= 0; i
< len0
; i
++)
1234 u32
[i
] = v0
->constant
->values
[0].u32
[i
];
1236 if (v1
->value_type
== vtn_value_type_constant
) {
1237 for (unsigned i
= 0; i
< len1
; i
++)
1238 u32
[len0
+ i
] = v1
->constant
->values
[0].u32
[i
];
1241 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1242 uint32_t comp
= w
[i
+ 6];
1243 /* If component is not used, set the value to a known constant
1244 * to detect if it is wrongly used.
1246 if (comp
== (uint32_t)-1)
1247 val
->constant
->values
[0].u32
[j
] = 0xdeadbeef;
1249 val
->constant
->values
[0].u32
[j
] = u32
[comp
];
1255 case SpvOpCompositeExtract
:
1256 case SpvOpCompositeInsert
: {
1257 struct vtn_value
*comp
;
1258 unsigned deref_start
;
1259 struct nir_constant
**c
;
1260 if (opcode
== SpvOpCompositeExtract
) {
1261 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1263 c
= &comp
->constant
;
1265 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1267 val
->constant
= nir_constant_clone(comp
->constant
,
1274 const struct glsl_type
*type
= comp
->const_type
;
1275 for (unsigned i
= deref_start
; i
< count
; i
++) {
1276 switch (glsl_get_base_type(type
)) {
1277 case GLSL_TYPE_UINT
:
1279 case GLSL_TYPE_UINT64
:
1280 case GLSL_TYPE_INT64
:
1281 case GLSL_TYPE_FLOAT
:
1282 case GLSL_TYPE_DOUBLE
:
1283 case GLSL_TYPE_BOOL
:
1284 /* If we hit this granularity, we're picking off an element */
1285 if (glsl_type_is_matrix(type
)) {
1286 assert(col
== 0 && elem
== -1);
1289 type
= glsl_get_column_type(type
);
1291 assert(elem
<= 0 && glsl_type_is_vector(type
));
1293 type
= glsl_scalar_type(glsl_get_base_type(type
));
1297 case GLSL_TYPE_ARRAY
:
1298 c
= &(*c
)->elements
[w
[i
]];
1299 type
= glsl_get_array_element(type
);
1302 case GLSL_TYPE_STRUCT
:
1303 c
= &(*c
)->elements
[w
[i
]];
1304 type
= glsl_get_struct_field(type
, w
[i
]);
1308 unreachable("Invalid constant type");
1312 if (opcode
== SpvOpCompositeExtract
) {
1316 unsigned num_components
= glsl_get_vector_elements(type
);
1317 unsigned bit_size
= glsl_get_bit_size(type
);
1318 for (unsigned i
= 0; i
< num_components
; i
++)
1319 if (bit_size
== 64) {
1320 val
->constant
->values
[0].u64
[i
] = (*c
)->values
[col
].u64
[elem
+ i
];
1322 assert(bit_size
== 32);
1323 val
->constant
->values
[0].u32
[i
] = (*c
)->values
[col
].u32
[elem
+ i
];
1327 struct vtn_value
*insert
=
1328 vtn_value(b
, w
[4], vtn_value_type_constant
);
1329 assert(insert
->const_type
== type
);
1331 *c
= insert
->constant
;
1333 unsigned num_components
= glsl_get_vector_elements(type
);
1334 unsigned bit_size
= glsl_get_bit_size(type
);
1335 for (unsigned i
= 0; i
< num_components
; i
++)
1336 if (bit_size
== 64) {
1337 (*c
)->values
[col
].u64
[elem
+ i
] = insert
->constant
->values
[0].u64
[i
];
1339 assert(bit_size
== 32);
1340 (*c
)->values
[col
].u32
[elem
+ i
] = insert
->constant
->values
[0].u32
[i
];
1349 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->const_type
);
1350 nir_alu_type src_alu_type
= dst_alu_type
;
1351 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(opcode
, &swap
, src_alu_type
, dst_alu_type
);
1353 unsigned num_components
= glsl_get_vector_elements(val
->const_type
);
1355 glsl_get_bit_size(val
->const_type
);
1357 nir_const_value src
[4];
1359 for (unsigned i
= 0; i
< count
- 4; i
++) {
1361 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
)->constant
;
1363 unsigned j
= swap
? 1 - i
: i
;
1364 assert(bit_size
== 32);
1365 src
[j
] = c
->values
[0];
1368 val
->constant
->values
[0] =
1369 nir_eval_const_opcode(op
, num_components
, bit_size
, src
);
1376 case SpvOpConstantNull
:
1377 val
->constant
= vtn_null_constant(b
, val
->const_type
);
1380 case SpvOpConstantSampler
:
1381 unreachable("OpConstantSampler requires Kernel Capability");
1385 unreachable("Unhandled opcode");
1388 /* Now that we have the value, update the workgroup size if needed */
1389 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1393 vtn_handle_function_call(struct vtn_builder
*b
, SpvOp opcode
,
1394 const uint32_t *w
, unsigned count
)
1396 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1397 struct vtn_function
*vtn_callee
=
1398 vtn_value(b
, w
[3], vtn_value_type_function
)->func
;
1399 struct nir_function
*callee
= vtn_callee
->impl
->function
;
1401 vtn_callee
->referenced
= true;
1403 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, callee
);
1404 for (unsigned i
= 0; i
< call
->num_params
; i
++) {
1405 unsigned arg_id
= w
[4 + i
];
1406 struct vtn_value
*arg
= vtn_untyped_value(b
, arg_id
);
1407 if (arg
->value_type
== vtn_value_type_pointer
&&
1408 arg
->pointer
->ptr_type
->type
== NULL
) {
1409 nir_deref_var
*d
= vtn_pointer_to_deref(b
, arg
->pointer
);
1410 call
->params
[i
] = nir_deref_var_clone(d
, call
);
1412 struct vtn_ssa_value
*arg_ssa
= vtn_ssa_value(b
, arg_id
);
1414 /* Make a temporary to store the argument in */
1416 nir_local_variable_create(b
->nb
.impl
, arg_ssa
->type
, "arg_tmp");
1417 call
->params
[i
] = nir_deref_var_create(call
, tmp
);
1419 vtn_local_store(b
, arg_ssa
, call
->params
[i
]);
1423 nir_variable
*out_tmp
= NULL
;
1424 assert(res_type
->type
== callee
->return_type
);
1425 if (!glsl_type_is_void(callee
->return_type
)) {
1426 out_tmp
= nir_local_variable_create(b
->nb
.impl
, callee
->return_type
,
1428 call
->return_deref
= nir_deref_var_create(call
, out_tmp
);
1431 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
1433 if (glsl_type_is_void(callee
->return_type
)) {
1434 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1436 vtn_push_ssa(b
, w
[2], res_type
, vtn_local_load(b
, call
->return_deref
));
1440 struct vtn_ssa_value
*
1441 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1443 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1446 if (!glsl_type_is_vector_or_scalar(type
)) {
1447 unsigned elems
= glsl_get_length(type
);
1448 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1449 for (unsigned i
= 0; i
< elems
; i
++) {
1450 const struct glsl_type
*child_type
;
1452 switch (glsl_get_base_type(type
)) {
1454 case GLSL_TYPE_UINT
:
1455 case GLSL_TYPE_INT64
:
1456 case GLSL_TYPE_UINT64
:
1457 case GLSL_TYPE_BOOL
:
1458 case GLSL_TYPE_FLOAT
:
1459 case GLSL_TYPE_DOUBLE
:
1460 child_type
= glsl_get_column_type(type
);
1462 case GLSL_TYPE_ARRAY
:
1463 child_type
= glsl_get_array_element(type
);
1465 case GLSL_TYPE_STRUCT
:
1466 child_type
= glsl_get_struct_field(type
, i
);
1469 unreachable("unkown base type");
1472 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
1480 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
1483 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
1484 src
.src_type
= type
;
1489 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
1490 const uint32_t *w
, unsigned count
)
1492 if (opcode
== SpvOpSampledImage
) {
1493 struct vtn_value
*val
=
1494 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1495 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1496 val
->sampled_image
->type
=
1497 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1498 val
->sampled_image
->image
=
1499 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1500 val
->sampled_image
->sampler
=
1501 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
1503 } else if (opcode
== SpvOpImage
) {
1504 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1505 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
1506 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
1507 val
->pointer
= src_val
->sampled_image
->image
;
1509 assert(src_val
->value_type
== vtn_value_type_pointer
);
1510 val
->pointer
= src_val
->pointer
;
1515 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1516 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1518 struct vtn_sampled_image sampled
;
1519 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
1520 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
1521 sampled
= *sampled_val
->sampled_image
;
1523 assert(sampled_val
->value_type
== vtn_value_type_pointer
);
1524 sampled
.type
= sampled_val
->pointer
->type
;
1525 sampled
.image
= NULL
;
1526 sampled
.sampler
= sampled_val
->pointer
;
1529 const struct glsl_type
*image_type
= sampled
.type
->type
;
1530 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
1531 const bool is_array
= glsl_sampler_type_is_array(image_type
);
1532 const bool is_shadow
= glsl_sampler_type_is_shadow(image_type
);
1534 /* Figure out the base texture operation */
1537 case SpvOpImageSampleImplicitLod
:
1538 case SpvOpImageSampleDrefImplicitLod
:
1539 case SpvOpImageSampleProjImplicitLod
:
1540 case SpvOpImageSampleProjDrefImplicitLod
:
1541 texop
= nir_texop_tex
;
1544 case SpvOpImageSampleExplicitLod
:
1545 case SpvOpImageSampleDrefExplicitLod
:
1546 case SpvOpImageSampleProjExplicitLod
:
1547 case SpvOpImageSampleProjDrefExplicitLod
:
1548 texop
= nir_texop_txl
;
1551 case SpvOpImageFetch
:
1552 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
1553 texop
= nir_texop_txf_ms
;
1555 texop
= nir_texop_txf
;
1559 case SpvOpImageGather
:
1560 case SpvOpImageDrefGather
:
1561 texop
= nir_texop_tg4
;
1564 case SpvOpImageQuerySizeLod
:
1565 case SpvOpImageQuerySize
:
1566 texop
= nir_texop_txs
;
1569 case SpvOpImageQueryLod
:
1570 texop
= nir_texop_lod
;
1573 case SpvOpImageQueryLevels
:
1574 texop
= nir_texop_query_levels
;
1577 case SpvOpImageQuerySamples
:
1578 texop
= nir_texop_texture_samples
;
1582 unreachable("Unhandled opcode");
1585 nir_tex_src srcs
[8]; /* 8 should be enough */
1586 nir_tex_src
*p
= srcs
;
1590 struct nir_ssa_def
*coord
;
1591 unsigned coord_components
;
1593 case SpvOpImageSampleImplicitLod
:
1594 case SpvOpImageSampleExplicitLod
:
1595 case SpvOpImageSampleDrefImplicitLod
:
1596 case SpvOpImageSampleDrefExplicitLod
:
1597 case SpvOpImageSampleProjImplicitLod
:
1598 case SpvOpImageSampleProjExplicitLod
:
1599 case SpvOpImageSampleProjDrefImplicitLod
:
1600 case SpvOpImageSampleProjDrefExplicitLod
:
1601 case SpvOpImageFetch
:
1602 case SpvOpImageGather
:
1603 case SpvOpImageDrefGather
:
1604 case SpvOpImageQueryLod
: {
1605 /* All these types have the coordinate as their first real argument */
1606 switch (sampler_dim
) {
1607 case GLSL_SAMPLER_DIM_1D
:
1608 case GLSL_SAMPLER_DIM_BUF
:
1609 coord_components
= 1;
1611 case GLSL_SAMPLER_DIM_2D
:
1612 case GLSL_SAMPLER_DIM_RECT
:
1613 case GLSL_SAMPLER_DIM_MS
:
1614 coord_components
= 2;
1616 case GLSL_SAMPLER_DIM_3D
:
1617 case GLSL_SAMPLER_DIM_CUBE
:
1618 coord_components
= 3;
1621 unreachable("Invalid sampler type");
1624 if (is_array
&& texop
!= nir_texop_lod
)
1627 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
1628 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
1629 (1 << coord_components
) - 1));
1630 p
->src_type
= nir_tex_src_coord
;
1637 coord_components
= 0;
1642 case SpvOpImageSampleProjImplicitLod
:
1643 case SpvOpImageSampleProjExplicitLod
:
1644 case SpvOpImageSampleProjDrefImplicitLod
:
1645 case SpvOpImageSampleProjDrefExplicitLod
:
1646 /* These have the projector as the last coordinate component */
1647 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
1648 p
->src_type
= nir_tex_src_projector
;
1656 unsigned gather_component
= 0;
1658 case SpvOpImageSampleDrefImplicitLod
:
1659 case SpvOpImageSampleDrefExplicitLod
:
1660 case SpvOpImageSampleProjDrefImplicitLod
:
1661 case SpvOpImageSampleProjDrefExplicitLod
:
1662 case SpvOpImageDrefGather
:
1663 /* These all have an explicit depth value as their next source */
1664 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
1667 case SpvOpImageGather
:
1668 /* This has a component as its next source */
1670 vtn_value(b
, w
[idx
++], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
1677 /* For OpImageQuerySizeLod, we always have an LOD */
1678 if (opcode
== SpvOpImageQuerySizeLod
)
1679 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1681 /* Now we need to handle some number of optional arguments */
1682 const struct vtn_ssa_value
*gather_offsets
= NULL
;
1684 uint32_t operands
= w
[idx
++];
1686 if (operands
& SpvImageOperandsBiasMask
) {
1687 assert(texop
== nir_texop_tex
);
1688 texop
= nir_texop_txb
;
1689 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
1692 if (operands
& SpvImageOperandsLodMask
) {
1693 assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
1694 texop
== nir_texop_txs
);
1695 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1698 if (operands
& SpvImageOperandsGradMask
) {
1699 assert(texop
== nir_texop_txl
);
1700 texop
= nir_texop_txd
;
1701 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
1702 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
1705 if (operands
& SpvImageOperandsOffsetMask
||
1706 operands
& SpvImageOperandsConstOffsetMask
)
1707 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
1709 if (operands
& SpvImageOperandsConstOffsetsMask
) {
1710 gather_offsets
= vtn_ssa_value(b
, w
[idx
++]);
1711 (*p
++) = (nir_tex_src
){};
1714 if (operands
& SpvImageOperandsSampleMask
) {
1715 assert(texop
== nir_texop_txf_ms
);
1716 texop
= nir_texop_txf_ms
;
1717 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
1720 /* We should have now consumed exactly all of the arguments */
1721 assert(idx
== count
);
1723 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
1726 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1728 instr
->coord_components
= coord_components
;
1729 instr
->sampler_dim
= sampler_dim
;
1730 instr
->is_array
= is_array
;
1731 instr
->is_shadow
= is_shadow
;
1732 instr
->is_new_style_shadow
=
1733 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
1734 instr
->component
= gather_component
;
1736 switch (glsl_get_sampler_result_type(image_type
)) {
1737 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
1738 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
1739 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
1740 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
1742 unreachable("Invalid base type for sampler result");
1745 nir_deref_var
*sampler
= vtn_pointer_to_deref(b
, sampled
.sampler
);
1746 nir_deref_var
*texture
;
1747 if (sampled
.image
) {
1748 nir_deref_var
*image
= vtn_pointer_to_deref(b
, sampled
.image
);
1754 instr
->texture
= nir_deref_var_clone(texture
, instr
);
1756 switch (instr
->op
) {
1762 /* These operations require a sampler */
1763 instr
->sampler
= nir_deref_var_clone(sampler
, instr
);
1766 case nir_texop_txf_ms
:
1769 case nir_texop_query_levels
:
1770 case nir_texop_texture_samples
:
1771 case nir_texop_samples_identical
:
1773 instr
->sampler
= NULL
;
1775 case nir_texop_txf_ms_mcs
:
1776 unreachable("unexpected nir_texop_txf_ms_mcs");
1779 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1780 nir_tex_instr_dest_size(instr
), 32, NULL
);
1782 assert(glsl_get_vector_elements(ret_type
->type
) ==
1783 nir_tex_instr_dest_size(instr
));
1786 nir_instr
*instruction
;
1787 if (gather_offsets
) {
1788 assert(glsl_get_base_type(gather_offsets
->type
) == GLSL_TYPE_ARRAY
);
1789 assert(glsl_get_length(gather_offsets
->type
) == 4);
1790 nir_tex_instr
*instrs
[4] = {instr
, NULL
, NULL
, NULL
};
1792 /* Copy the current instruction 4x */
1793 for (uint32_t i
= 1; i
< 4; i
++) {
1794 instrs
[i
] = nir_tex_instr_create(b
->shader
, instr
->num_srcs
);
1795 instrs
[i
]->op
= instr
->op
;
1796 instrs
[i
]->coord_components
= instr
->coord_components
;
1797 instrs
[i
]->sampler_dim
= instr
->sampler_dim
;
1798 instrs
[i
]->is_array
= instr
->is_array
;
1799 instrs
[i
]->is_shadow
= instr
->is_shadow
;
1800 instrs
[i
]->is_new_style_shadow
= instr
->is_new_style_shadow
;
1801 instrs
[i
]->component
= instr
->component
;
1802 instrs
[i
]->dest_type
= instr
->dest_type
;
1803 instrs
[i
]->texture
= nir_deref_var_clone(texture
, instrs
[i
]);
1804 instrs
[i
]->sampler
= NULL
;
1806 memcpy(instrs
[i
]->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1808 nir_ssa_dest_init(&instrs
[i
]->instr
, &instrs
[i
]->dest
,
1809 nir_tex_instr_dest_size(instr
), 32, NULL
);
1812 /* Fill in the last argument with the offset from the passed in offsets
1813 * and insert the instruction into the stream.
1815 for (uint32_t i
= 0; i
< 4; i
++) {
1817 src
.src
= nir_src_for_ssa(gather_offsets
->elems
[i
]->def
);
1818 src
.src_type
= nir_tex_src_offset
;
1819 instrs
[i
]->src
[instrs
[i
]->num_srcs
- 1] = src
;
1820 nir_builder_instr_insert(&b
->nb
, &instrs
[i
]->instr
);
1823 /* Combine the results of the 4 instructions by taking their .w
1826 nir_alu_instr
*vec4
= nir_alu_instr_create(b
->shader
, nir_op_vec4
);
1827 nir_ssa_dest_init(&vec4
->instr
, &vec4
->dest
.dest
, 4, 32, NULL
);
1828 vec4
->dest
.write_mask
= 0xf;
1829 for (uint32_t i
= 0; i
< 4; i
++) {
1830 vec4
->src
[i
].src
= nir_src_for_ssa(&instrs
[i
]->dest
.ssa
);
1831 vec4
->src
[i
].swizzle
[0] = 3;
1833 def
= &vec4
->dest
.dest
.ssa
;
1834 instruction
= &vec4
->instr
;
1836 def
= &instr
->dest
.ssa
;
1837 instruction
= &instr
->instr
;
1840 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
1841 val
->ssa
->def
= def
;
1843 nir_builder_instr_insert(&b
->nb
, instruction
);
1847 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
1848 const uint32_t *w
, nir_src
*src
)
1851 case SpvOpAtomicIIncrement
:
1852 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
1855 case SpvOpAtomicIDecrement
:
1856 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
1859 case SpvOpAtomicISub
:
1861 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
1864 case SpvOpAtomicCompareExchange
:
1865 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
1866 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
1869 case SpvOpAtomicExchange
:
1870 case SpvOpAtomicIAdd
:
1871 case SpvOpAtomicSMin
:
1872 case SpvOpAtomicUMin
:
1873 case SpvOpAtomicSMax
:
1874 case SpvOpAtomicUMax
:
1875 case SpvOpAtomicAnd
:
1877 case SpvOpAtomicXor
:
1878 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1882 unreachable("Invalid SPIR-V atomic");
1886 static nir_ssa_def
*
1887 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
1889 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
1891 /* The image_load_store intrinsics assume a 4-dim coordinate */
1892 unsigned dim
= glsl_get_vector_elements(coord
->type
);
1893 unsigned swizzle
[4];
1894 for (unsigned i
= 0; i
< 4; i
++)
1895 swizzle
[i
] = MIN2(i
, dim
- 1);
1897 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
1901 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
1902 const uint32_t *w
, unsigned count
)
1904 /* Just get this one out of the way */
1905 if (opcode
== SpvOpImageTexelPointer
) {
1906 struct vtn_value
*val
=
1907 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
1908 val
->image
= ralloc(b
, struct vtn_image_pointer
);
1910 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1911 val
->image
->coord
= get_image_coord(b
, w
[4]);
1912 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
1916 struct vtn_image_pointer image
;
1919 case SpvOpAtomicExchange
:
1920 case SpvOpAtomicCompareExchange
:
1921 case SpvOpAtomicCompareExchangeWeak
:
1922 case SpvOpAtomicIIncrement
:
1923 case SpvOpAtomicIDecrement
:
1924 case SpvOpAtomicIAdd
:
1925 case SpvOpAtomicISub
:
1926 case SpvOpAtomicLoad
:
1927 case SpvOpAtomicSMin
:
1928 case SpvOpAtomicUMin
:
1929 case SpvOpAtomicSMax
:
1930 case SpvOpAtomicUMax
:
1931 case SpvOpAtomicAnd
:
1933 case SpvOpAtomicXor
:
1934 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
1937 case SpvOpAtomicStore
:
1938 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
1941 case SpvOpImageQuerySize
:
1942 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1944 image
.sample
= NULL
;
1947 case SpvOpImageRead
:
1948 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1949 image
.coord
= get_image_coord(b
, w
[4]);
1951 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
1952 assert(w
[5] == SpvImageOperandsSampleMask
);
1953 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
1955 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1959 case SpvOpImageWrite
:
1960 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
1961 image
.coord
= get_image_coord(b
, w
[2]);
1965 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
1966 assert(w
[4] == SpvImageOperandsSampleMask
);
1967 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
1969 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1974 unreachable("Invalid image opcode");
1977 nir_intrinsic_op op
;
1979 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_##N; break;
1980 OP(ImageQuerySize
, size
)
1982 OP(ImageWrite
, store
)
1983 OP(AtomicLoad
, load
)
1984 OP(AtomicStore
, store
)
1985 OP(AtomicExchange
, atomic_exchange
)
1986 OP(AtomicCompareExchange
, atomic_comp_swap
)
1987 OP(AtomicIIncrement
, atomic_add
)
1988 OP(AtomicIDecrement
, atomic_add
)
1989 OP(AtomicIAdd
, atomic_add
)
1990 OP(AtomicISub
, atomic_add
)
1991 OP(AtomicSMin
, atomic_min
)
1992 OP(AtomicUMin
, atomic_min
)
1993 OP(AtomicSMax
, atomic_max
)
1994 OP(AtomicUMax
, atomic_max
)
1995 OP(AtomicAnd
, atomic_and
)
1996 OP(AtomicOr
, atomic_or
)
1997 OP(AtomicXor
, atomic_xor
)
2000 unreachable("Invalid image opcode");
2003 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
2005 nir_deref_var
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
2006 intrin
->variables
[0] = nir_deref_var_clone(image_deref
, intrin
);
2008 /* ImageQuerySize doesn't take any extra parameters */
2009 if (opcode
!= SpvOpImageQuerySize
) {
2010 /* The image coordinate is always 4 components but we may not have that
2011 * many. Swizzle to compensate.
2014 for (unsigned i
= 0; i
< 4; i
++)
2015 swiz
[i
] = i
< image
.coord
->num_components
? i
: 0;
2016 intrin
->src
[0] = nir_src_for_ssa(nir_swizzle(&b
->nb
, image
.coord
,
2018 intrin
->src
[1] = nir_src_for_ssa(image
.sample
);
2022 case SpvOpAtomicLoad
:
2023 case SpvOpImageQuerySize
:
2024 case SpvOpImageRead
:
2026 case SpvOpAtomicStore
:
2027 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2029 case SpvOpImageWrite
:
2030 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[3])->def
);
2033 case SpvOpAtomicCompareExchange
:
2034 case SpvOpAtomicIIncrement
:
2035 case SpvOpAtomicIDecrement
:
2036 case SpvOpAtomicExchange
:
2037 case SpvOpAtomicIAdd
:
2038 case SpvOpAtomicISub
:
2039 case SpvOpAtomicSMin
:
2040 case SpvOpAtomicUMin
:
2041 case SpvOpAtomicSMax
:
2042 case SpvOpAtomicUMax
:
2043 case SpvOpAtomicAnd
:
2045 case SpvOpAtomicXor
:
2046 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[2]);
2050 unreachable("Invalid image opcode");
2053 if (opcode
!= SpvOpImageWrite
) {
2054 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2055 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2057 unsigned dest_components
=
2058 nir_intrinsic_infos
[intrin
->intrinsic
].dest_components
;
2059 if (intrin
->intrinsic
== nir_intrinsic_image_size
) {
2060 dest_components
= intrin
->num_components
=
2061 glsl_get_vector_elements(type
->type
);
2064 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2065 dest_components
, 32, NULL
);
2067 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2069 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
2070 val
->ssa
->def
= &intrin
->dest
.ssa
;
2072 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2076 static nir_intrinsic_op
2077 get_ssbo_nir_atomic_op(SpvOp opcode
)
2080 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2081 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2082 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2083 OP(AtomicExchange
, atomic_exchange
)
2084 OP(AtomicCompareExchange
, atomic_comp_swap
)
2085 OP(AtomicIIncrement
, atomic_add
)
2086 OP(AtomicIDecrement
, atomic_add
)
2087 OP(AtomicIAdd
, atomic_add
)
2088 OP(AtomicISub
, atomic_add
)
2089 OP(AtomicSMin
, atomic_imin
)
2090 OP(AtomicUMin
, atomic_umin
)
2091 OP(AtomicSMax
, atomic_imax
)
2092 OP(AtomicUMax
, atomic_umax
)
2093 OP(AtomicAnd
, atomic_and
)
2094 OP(AtomicOr
, atomic_or
)
2095 OP(AtomicXor
, atomic_xor
)
2098 unreachable("Invalid SSBO atomic");
2102 static nir_intrinsic_op
2103 get_shared_nir_atomic_op(SpvOp opcode
)
2106 case SpvOpAtomicLoad
: return nir_intrinsic_load_var
;
2107 case SpvOpAtomicStore
: return nir_intrinsic_store_var
;
2108 #define OP(S, N) case SpvOp##S: return nir_intrinsic_var_##N;
2109 OP(AtomicExchange
, atomic_exchange
)
2110 OP(AtomicCompareExchange
, atomic_comp_swap
)
2111 OP(AtomicIIncrement
, atomic_add
)
2112 OP(AtomicIDecrement
, atomic_add
)
2113 OP(AtomicIAdd
, atomic_add
)
2114 OP(AtomicISub
, atomic_add
)
2115 OP(AtomicSMin
, atomic_imin
)
2116 OP(AtomicUMin
, atomic_umin
)
2117 OP(AtomicSMax
, atomic_imax
)
2118 OP(AtomicUMax
, atomic_umax
)
2119 OP(AtomicAnd
, atomic_and
)
2120 OP(AtomicOr
, atomic_or
)
2121 OP(AtomicXor
, atomic_xor
)
2124 unreachable("Invalid shared atomic");
2129 vtn_handle_ssbo_or_shared_atomic(struct vtn_builder
*b
, SpvOp opcode
,
2130 const uint32_t *w
, unsigned count
)
2132 struct vtn_pointer
*ptr
;
2133 nir_intrinsic_instr
*atomic
;
2136 case SpvOpAtomicLoad
:
2137 case SpvOpAtomicExchange
:
2138 case SpvOpAtomicCompareExchange
:
2139 case SpvOpAtomicCompareExchangeWeak
:
2140 case SpvOpAtomicIIncrement
:
2141 case SpvOpAtomicIDecrement
:
2142 case SpvOpAtomicIAdd
:
2143 case SpvOpAtomicISub
:
2144 case SpvOpAtomicSMin
:
2145 case SpvOpAtomicUMin
:
2146 case SpvOpAtomicSMax
:
2147 case SpvOpAtomicUMax
:
2148 case SpvOpAtomicAnd
:
2150 case SpvOpAtomicXor
:
2151 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2154 case SpvOpAtomicStore
:
2155 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2159 unreachable("Invalid SPIR-V atomic");
2163 SpvScope scope = w[4];
2164 SpvMemorySemanticsMask semantics = w[5];
2167 if (ptr
->mode
== vtn_variable_mode_workgroup
) {
2168 nir_deref_var
*deref
= vtn_pointer_to_deref(b
, ptr
);
2169 const struct glsl_type
*deref_type
= nir_deref_tail(&deref
->deref
)->type
;
2170 nir_intrinsic_op op
= get_shared_nir_atomic_op(opcode
);
2171 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2172 atomic
->variables
[0] = nir_deref_var_clone(deref
, atomic
);
2175 case SpvOpAtomicLoad
:
2176 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2179 case SpvOpAtomicStore
:
2180 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2181 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2182 atomic
->src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2185 case SpvOpAtomicExchange
:
2186 case SpvOpAtomicCompareExchange
:
2187 case SpvOpAtomicCompareExchangeWeak
:
2188 case SpvOpAtomicIIncrement
:
2189 case SpvOpAtomicIDecrement
:
2190 case SpvOpAtomicIAdd
:
2191 case SpvOpAtomicISub
:
2192 case SpvOpAtomicSMin
:
2193 case SpvOpAtomicUMin
:
2194 case SpvOpAtomicSMax
:
2195 case SpvOpAtomicUMax
:
2196 case SpvOpAtomicAnd
:
2198 case SpvOpAtomicXor
:
2199 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[0]);
2203 unreachable("Invalid SPIR-V atomic");
2207 assert(ptr
->mode
== vtn_variable_mode_ssbo
);
2208 nir_ssa_def
*offset
, *index
;
2209 offset
= vtn_pointer_to_offset(b
, ptr
, &index
, NULL
);
2211 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(opcode
);
2213 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2216 case SpvOpAtomicLoad
:
2217 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2218 atomic
->src
[0] = nir_src_for_ssa(index
);
2219 atomic
->src
[1] = nir_src_for_ssa(offset
);
2222 case SpvOpAtomicStore
:
2223 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2224 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2225 atomic
->src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2226 atomic
->src
[1] = nir_src_for_ssa(index
);
2227 atomic
->src
[2] = nir_src_for_ssa(offset
);
2230 case SpvOpAtomicExchange
:
2231 case SpvOpAtomicCompareExchange
:
2232 case SpvOpAtomicCompareExchangeWeak
:
2233 case SpvOpAtomicIIncrement
:
2234 case SpvOpAtomicIDecrement
:
2235 case SpvOpAtomicIAdd
:
2236 case SpvOpAtomicISub
:
2237 case SpvOpAtomicSMin
:
2238 case SpvOpAtomicUMin
:
2239 case SpvOpAtomicSMax
:
2240 case SpvOpAtomicUMax
:
2241 case SpvOpAtomicAnd
:
2243 case SpvOpAtomicXor
:
2244 atomic
->src
[0] = nir_src_for_ssa(index
);
2245 atomic
->src
[1] = nir_src_for_ssa(offset
);
2246 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[2]);
2250 unreachable("Invalid SPIR-V atomic");
2254 if (opcode
!= SpvOpAtomicStore
) {
2255 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2257 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
2258 glsl_get_vector_elements(type
->type
),
2259 glsl_get_bit_size(type
->type
), NULL
);
2261 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2262 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
2263 val
->ssa
->def
= &atomic
->dest
.ssa
;
2264 val
->ssa
->type
= type
->type
;
2267 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
2270 static nir_alu_instr
*
2271 create_vec(nir_shader
*shader
, unsigned num_components
, unsigned bit_size
)
2274 switch (num_components
) {
2275 case 1: op
= nir_op_fmov
; break;
2276 case 2: op
= nir_op_vec2
; break;
2277 case 3: op
= nir_op_vec3
; break;
2278 case 4: op
= nir_op_vec4
; break;
2279 default: unreachable("bad vector size");
2282 nir_alu_instr
*vec
= nir_alu_instr_create(shader
, op
);
2283 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
2285 vec
->dest
.write_mask
= (1 << num_components
) - 1;
2290 struct vtn_ssa_value
*
2291 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
2293 if (src
->transposed
)
2294 return src
->transposed
;
2296 struct vtn_ssa_value
*dest
=
2297 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
2299 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
2300 nir_alu_instr
*vec
= create_vec(b
->shader
,
2301 glsl_get_matrix_columns(src
->type
),
2302 glsl_get_bit_size(src
->type
));
2303 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2304 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
2305 vec
->src
[0].swizzle
[0] = i
;
2307 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
2308 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
2309 vec
->src
[j
].swizzle
[0] = i
;
2312 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2313 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
2316 dest
->transposed
= src
;
2322 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
2324 unsigned swiz
[4] = { index
};
2325 return nir_swizzle(&b
->nb
, src
, swiz
, 1, true);
2329 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
2332 nir_alu_instr
*vec
= create_vec(b
->shader
, src
->num_components
,
2335 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
2337 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
2339 vec
->src
[i
].src
= nir_src_for_ssa(src
);
2340 vec
->src
[i
].swizzle
[0] = i
;
2344 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2346 return &vec
->dest
.dest
.ssa
;
2350 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2353 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
2354 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2355 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2356 vtn_vector_extract(b
, src
, i
), dest
);
2362 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2363 nir_ssa_def
*insert
, nir_ssa_def
*index
)
2365 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
2366 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2367 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2368 vtn_vector_insert(b
, src
, insert
, i
), dest
);
2373 static nir_ssa_def
*
2374 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
2375 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
2376 const uint32_t *indices
)
2378 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
, src0
->bit_size
);
2380 for (unsigned i
= 0; i
< num_components
; i
++) {
2381 uint32_t index
= indices
[i
];
2382 if (index
== 0xffffffff) {
2384 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
2385 } else if (index
< src0
->num_components
) {
2386 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
2387 vec
->src
[i
].swizzle
[0] = index
;
2389 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
2390 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
2394 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2396 return &vec
->dest
.dest
.ssa
;
2400 * Concatentates a number of vectors/scalars together to produce a vector
2402 static nir_ssa_def
*
2403 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
2404 unsigned num_srcs
, nir_ssa_def
**srcs
)
2406 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
,
2409 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2411 * "When constructing a vector, there must be at least two Constituent
2414 assert(num_srcs
>= 2);
2416 unsigned dest_idx
= 0;
2417 for (unsigned i
= 0; i
< num_srcs
; i
++) {
2418 nir_ssa_def
*src
= srcs
[i
];
2419 assert(dest_idx
+ src
->num_components
<= num_components
);
2420 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
2421 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
2422 vec
->src
[dest_idx
].swizzle
[0] = j
;
2427 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2429 * "When constructing a vector, the total number of components in all
2430 * the operands must equal the number of components in Result Type."
2432 assert(dest_idx
== num_components
);
2434 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2436 return &vec
->dest
.dest
.ssa
;
2439 static struct vtn_ssa_value
*
2440 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
2442 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
2443 dest
->type
= src
->type
;
2445 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2446 dest
->def
= src
->def
;
2448 unsigned elems
= glsl_get_length(src
->type
);
2450 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
2451 for (unsigned i
= 0; i
< elems
; i
++)
2452 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
2458 static struct vtn_ssa_value
*
2459 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2460 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
2461 unsigned num_indices
)
2463 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
2465 struct vtn_ssa_value
*cur
= dest
;
2467 for (i
= 0; i
< num_indices
- 1; i
++) {
2468 cur
= cur
->elems
[indices
[i
]];
2471 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2472 /* According to the SPIR-V spec, OpCompositeInsert may work down to
2473 * the component granularity. In that case, the last index will be
2474 * the index to insert the scalar into the vector.
2477 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
2479 cur
->elems
[indices
[i
]] = insert
;
2485 static struct vtn_ssa_value
*
2486 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2487 const uint32_t *indices
, unsigned num_indices
)
2489 struct vtn_ssa_value
*cur
= src
;
2490 for (unsigned i
= 0; i
< num_indices
; i
++) {
2491 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2492 assert(i
== num_indices
- 1);
2493 /* According to the SPIR-V spec, OpCompositeExtract may work down to
2494 * the component granularity. The last index will be the index of the
2495 * vector to extract.
2498 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
2499 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
2500 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
2503 cur
= cur
->elems
[indices
[i
]];
2511 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
2512 const uint32_t *w
, unsigned count
)
2514 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2515 const struct glsl_type
*type
=
2516 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
2517 val
->ssa
= vtn_create_ssa_value(b
, type
);
2520 case SpvOpVectorExtractDynamic
:
2521 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2522 vtn_ssa_value(b
, w
[4])->def
);
2525 case SpvOpVectorInsertDynamic
:
2526 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2527 vtn_ssa_value(b
, w
[4])->def
,
2528 vtn_ssa_value(b
, w
[5])->def
);
2531 case SpvOpVectorShuffle
:
2532 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
2533 vtn_ssa_value(b
, w
[3])->def
,
2534 vtn_ssa_value(b
, w
[4])->def
,
2538 case SpvOpCompositeConstruct
: {
2539 unsigned elems
= count
- 3;
2540 if (glsl_type_is_vector_or_scalar(type
)) {
2541 nir_ssa_def
*srcs
[4];
2542 for (unsigned i
= 0; i
< elems
; i
++)
2543 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
2545 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
2548 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2549 for (unsigned i
= 0; i
< elems
; i
++)
2550 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
2554 case SpvOpCompositeExtract
:
2555 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
2559 case SpvOpCompositeInsert
:
2560 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
2561 vtn_ssa_value(b
, w
[3]),
2565 case SpvOpCopyObject
:
2566 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
2570 unreachable("unknown composite operation");
2575 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
2576 const uint32_t *w
, unsigned count
)
2578 nir_intrinsic_op intrinsic_op
;
2580 case SpvOpEmitVertex
:
2581 case SpvOpEmitStreamVertex
:
2582 intrinsic_op
= nir_intrinsic_emit_vertex
;
2584 case SpvOpEndPrimitive
:
2585 case SpvOpEndStreamPrimitive
:
2586 intrinsic_op
= nir_intrinsic_end_primitive
;
2588 case SpvOpMemoryBarrier
:
2589 intrinsic_op
= nir_intrinsic_memory_barrier
;
2591 case SpvOpControlBarrier
:
2592 intrinsic_op
= nir_intrinsic_barrier
;
2595 unreachable("unknown barrier instruction");
2598 nir_intrinsic_instr
*intrin
=
2599 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
2601 if (opcode
== SpvOpEmitStreamVertex
|| opcode
== SpvOpEndStreamPrimitive
)
2602 nir_intrinsic_set_stream_id(intrin
, w
[1]);
2604 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2608 gl_primitive_from_spv_execution_mode(SpvExecutionMode mode
)
2611 case SpvExecutionModeInputPoints
:
2612 case SpvExecutionModeOutputPoints
:
2613 return 0; /* GL_POINTS */
2614 case SpvExecutionModeInputLines
:
2615 return 1; /* GL_LINES */
2616 case SpvExecutionModeInputLinesAdjacency
:
2617 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
2618 case SpvExecutionModeTriangles
:
2619 return 4; /* GL_TRIANGLES */
2620 case SpvExecutionModeInputTrianglesAdjacency
:
2621 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
2622 case SpvExecutionModeQuads
:
2623 return 7; /* GL_QUADS */
2624 case SpvExecutionModeIsolines
:
2625 return 0x8E7A; /* GL_ISOLINES */
2626 case SpvExecutionModeOutputLineStrip
:
2627 return 3; /* GL_LINE_STRIP */
2628 case SpvExecutionModeOutputTriangleStrip
:
2629 return 5; /* GL_TRIANGLE_STRIP */
2631 unreachable("Invalid primitive type");
2637 vertices_in_from_spv_execution_mode(SpvExecutionMode mode
)
2640 case SpvExecutionModeInputPoints
:
2642 case SpvExecutionModeInputLines
:
2644 case SpvExecutionModeInputLinesAdjacency
:
2646 case SpvExecutionModeTriangles
:
2648 case SpvExecutionModeInputTrianglesAdjacency
:
2651 unreachable("Invalid GS input mode");
2656 static gl_shader_stage
2657 stage_for_execution_model(SpvExecutionModel model
)
2660 case SpvExecutionModelVertex
:
2661 return MESA_SHADER_VERTEX
;
2662 case SpvExecutionModelTessellationControl
:
2663 return MESA_SHADER_TESS_CTRL
;
2664 case SpvExecutionModelTessellationEvaluation
:
2665 return MESA_SHADER_TESS_EVAL
;
2666 case SpvExecutionModelGeometry
:
2667 return MESA_SHADER_GEOMETRY
;
2668 case SpvExecutionModelFragment
:
2669 return MESA_SHADER_FRAGMENT
;
2670 case SpvExecutionModelGLCompute
:
2671 return MESA_SHADER_COMPUTE
;
2673 unreachable("Unsupported execution model");
2677 #define spv_check_supported(name, cap) do { \
2678 if (!(b->options && b->options->caps.name)) \
2679 vtn_warn("Unsupported SPIR-V capability: %s", \
2680 spirv_capability_to_string(cap)); \
2684 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2685 const uint32_t *w
, unsigned count
)
2689 case SpvOpSourceExtension
:
2690 case SpvOpSourceContinued
:
2691 case SpvOpExtension
:
2692 /* Unhandled, but these are for debug so that's ok. */
2695 case SpvOpCapability
: {
2696 SpvCapability cap
= w
[1];
2698 case SpvCapabilityMatrix
:
2699 case SpvCapabilityShader
:
2700 case SpvCapabilityGeometry
:
2701 case SpvCapabilityGeometryPointSize
:
2702 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
2703 case SpvCapabilitySampledImageArrayDynamicIndexing
:
2704 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
2705 case SpvCapabilityStorageImageArrayDynamicIndexing
:
2706 case SpvCapabilityImageRect
:
2707 case SpvCapabilitySampledRect
:
2708 case SpvCapabilitySampled1D
:
2709 case SpvCapabilityImage1D
:
2710 case SpvCapabilitySampledCubeArray
:
2711 case SpvCapabilityImageCubeArray
:
2712 case SpvCapabilitySampledBuffer
:
2713 case SpvCapabilityImageBuffer
:
2714 case SpvCapabilityImageQuery
:
2715 case SpvCapabilityDerivativeControl
:
2716 case SpvCapabilityInterpolationFunction
:
2717 case SpvCapabilityMultiViewport
:
2718 case SpvCapabilitySampleRateShading
:
2719 case SpvCapabilityClipDistance
:
2720 case SpvCapabilityCullDistance
:
2721 case SpvCapabilityInputAttachment
:
2722 case SpvCapabilityImageGatherExtended
:
2723 case SpvCapabilityStorageImageExtendedFormats
:
2726 case SpvCapabilityGeometryStreams
:
2727 case SpvCapabilityLinkage
:
2728 case SpvCapabilityVector16
:
2729 case SpvCapabilityFloat16Buffer
:
2730 case SpvCapabilityFloat16
:
2731 case SpvCapabilityInt64Atomics
:
2732 case SpvCapabilityAtomicStorage
:
2733 case SpvCapabilityInt16
:
2734 case SpvCapabilityStorageImageMultisample
:
2735 case SpvCapabilityInt8
:
2736 case SpvCapabilitySparseResidency
:
2737 case SpvCapabilityMinLod
:
2738 case SpvCapabilityTransformFeedback
:
2739 vtn_warn("Unsupported SPIR-V capability: %s",
2740 spirv_capability_to_string(cap
));
2743 case SpvCapabilityFloat64
:
2744 spv_check_supported(float64
, cap
);
2746 case SpvCapabilityInt64
:
2747 spv_check_supported(int64
, cap
);
2750 case SpvCapabilityAddresses
:
2751 case SpvCapabilityKernel
:
2752 case SpvCapabilityImageBasic
:
2753 case SpvCapabilityImageReadWrite
:
2754 case SpvCapabilityImageMipmap
:
2755 case SpvCapabilityPipes
:
2756 case SpvCapabilityGroups
:
2757 case SpvCapabilityDeviceEnqueue
:
2758 case SpvCapabilityLiteralSampler
:
2759 case SpvCapabilityGenericPointer
:
2760 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
2761 spirv_capability_to_string(cap
));
2764 case SpvCapabilityImageMSArray
:
2765 spv_check_supported(image_ms_array
, cap
);
2768 case SpvCapabilityTessellation
:
2769 case SpvCapabilityTessellationPointSize
:
2770 spv_check_supported(tessellation
, cap
);
2773 case SpvCapabilityDrawParameters
:
2774 spv_check_supported(draw_parameters
, cap
);
2777 case SpvCapabilityStorageImageReadWithoutFormat
:
2778 spv_check_supported(image_read_without_format
, cap
);
2781 case SpvCapabilityStorageImageWriteWithoutFormat
:
2782 spv_check_supported(image_write_without_format
, cap
);
2785 case SpvCapabilityMultiView
:
2786 spv_check_supported(multiview
, cap
);
2789 case SpvCapabilityVariablePointersStorageBuffer
:
2790 case SpvCapabilityVariablePointers
:
2791 spv_check_supported(variable_pointers
, cap
);
2795 unreachable("Unhandled capability");
2800 case SpvOpExtInstImport
:
2801 vtn_handle_extension(b
, opcode
, w
, count
);
2804 case SpvOpMemoryModel
:
2805 assert(w
[1] == SpvAddressingModelLogical
);
2806 assert(w
[2] == SpvMemoryModelSimple
||
2807 w
[2] == SpvMemoryModelGLSL450
);
2810 case SpvOpEntryPoint
: {
2811 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
2812 /* Let this be a name label regardless */
2813 unsigned name_words
;
2814 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
2816 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
2817 stage_for_execution_model(w
[1]) != b
->entry_point_stage
)
2820 assert(b
->entry_point
== NULL
);
2821 b
->entry_point
= entry_point
;
2826 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
2827 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2831 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2834 case SpvOpMemberName
:
2838 case SpvOpExecutionMode
:
2839 case SpvOpDecorationGroup
:
2841 case SpvOpMemberDecorate
:
2842 case SpvOpGroupDecorate
:
2843 case SpvOpGroupMemberDecorate
:
2844 vtn_handle_decoration(b
, opcode
, w
, count
);
2848 return false; /* End of preamble */
2855 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
2856 const struct vtn_decoration
*mode
, void *data
)
2858 assert(b
->entry_point
== entry_point
);
2860 switch(mode
->exec_mode
) {
2861 case SpvExecutionModeOriginUpperLeft
:
2862 case SpvExecutionModeOriginLowerLeft
:
2863 b
->origin_upper_left
=
2864 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
2867 case SpvExecutionModeEarlyFragmentTests
:
2868 assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
2869 b
->shader
->info
.fs
.early_fragment_tests
= true;
2872 case SpvExecutionModeInvocations
:
2873 assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
2874 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->literals
[0]);
2877 case SpvExecutionModeDepthReplacing
:
2878 assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
2879 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
2881 case SpvExecutionModeDepthGreater
:
2882 assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
2883 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
2885 case SpvExecutionModeDepthLess
:
2886 assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
2887 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
2889 case SpvExecutionModeDepthUnchanged
:
2890 assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
2891 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
2894 case SpvExecutionModeLocalSize
:
2895 assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
2896 b
->shader
->info
.cs
.local_size
[0] = mode
->literals
[0];
2897 b
->shader
->info
.cs
.local_size
[1] = mode
->literals
[1];
2898 b
->shader
->info
.cs
.local_size
[2] = mode
->literals
[2];
2900 case SpvExecutionModeLocalSizeHint
:
2901 break; /* Nothing to do with this */
2903 case SpvExecutionModeOutputVertices
:
2904 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
2905 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
2906 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->literals
[0];
2908 assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
2909 b
->shader
->info
.gs
.vertices_out
= mode
->literals
[0];
2913 case SpvExecutionModeInputPoints
:
2914 case SpvExecutionModeInputLines
:
2915 case SpvExecutionModeInputLinesAdjacency
:
2916 case SpvExecutionModeTriangles
:
2917 case SpvExecutionModeInputTrianglesAdjacency
:
2918 case SpvExecutionModeQuads
:
2919 case SpvExecutionModeIsolines
:
2920 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
2921 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
2922 b
->shader
->info
.tess
.primitive_mode
=
2923 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
2925 assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
2926 b
->shader
->info
.gs
.vertices_in
=
2927 vertices_in_from_spv_execution_mode(mode
->exec_mode
);
2931 case SpvExecutionModeOutputPoints
:
2932 case SpvExecutionModeOutputLineStrip
:
2933 case SpvExecutionModeOutputTriangleStrip
:
2934 assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
2935 b
->shader
->info
.gs
.output_primitive
=
2936 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
2939 case SpvExecutionModeSpacingEqual
:
2940 assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
2941 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
2942 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
2944 case SpvExecutionModeSpacingFractionalEven
:
2945 assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
2946 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
2947 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
2949 case SpvExecutionModeSpacingFractionalOdd
:
2950 assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
2951 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
2952 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
2954 case SpvExecutionModeVertexOrderCw
:
2955 assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
2956 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
2957 b
->shader
->info
.tess
.ccw
= false;
2959 case SpvExecutionModeVertexOrderCcw
:
2960 assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
2961 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
2962 b
->shader
->info
.tess
.ccw
= true;
2964 case SpvExecutionModePointMode
:
2965 assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
2966 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
2967 b
->shader
->info
.tess
.point_mode
= true;
2970 case SpvExecutionModePixelCenterInteger
:
2971 b
->pixel_center_integer
= true;
2974 case SpvExecutionModeXfb
:
2975 unreachable("Unhandled execution mode");
2978 case SpvExecutionModeVecTypeHint
:
2979 case SpvExecutionModeContractionOff
:
2983 unreachable("Unhandled execution mode");
2988 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2989 const uint32_t *w
, unsigned count
)
2993 case SpvOpSourceContinued
:
2994 case SpvOpSourceExtension
:
2995 case SpvOpExtension
:
2996 case SpvOpCapability
:
2997 case SpvOpExtInstImport
:
2998 case SpvOpMemoryModel
:
2999 case SpvOpEntryPoint
:
3000 case SpvOpExecutionMode
:
3003 case SpvOpMemberName
:
3004 case SpvOpDecorationGroup
:
3006 case SpvOpMemberDecorate
:
3007 case SpvOpGroupDecorate
:
3008 case SpvOpGroupMemberDecorate
:
3009 unreachable("Invalid opcode types and variables section");
3015 case SpvOpTypeFloat
:
3016 case SpvOpTypeVector
:
3017 case SpvOpTypeMatrix
:
3018 case SpvOpTypeImage
:
3019 case SpvOpTypeSampler
:
3020 case SpvOpTypeSampledImage
:
3021 case SpvOpTypeArray
:
3022 case SpvOpTypeRuntimeArray
:
3023 case SpvOpTypeStruct
:
3024 case SpvOpTypeOpaque
:
3025 case SpvOpTypePointer
:
3026 case SpvOpTypeFunction
:
3027 case SpvOpTypeEvent
:
3028 case SpvOpTypeDeviceEvent
:
3029 case SpvOpTypeReserveId
:
3030 case SpvOpTypeQueue
:
3032 vtn_handle_type(b
, opcode
, w
, count
);
3035 case SpvOpConstantTrue
:
3036 case SpvOpConstantFalse
:
3038 case SpvOpConstantComposite
:
3039 case SpvOpConstantSampler
:
3040 case SpvOpConstantNull
:
3041 case SpvOpSpecConstantTrue
:
3042 case SpvOpSpecConstantFalse
:
3043 case SpvOpSpecConstant
:
3044 case SpvOpSpecConstantComposite
:
3045 case SpvOpSpecConstantOp
:
3046 vtn_handle_constant(b
, opcode
, w
, count
);
3051 vtn_handle_variables(b
, opcode
, w
, count
);
3055 return false; /* End of preamble */
3062 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3063 const uint32_t *w
, unsigned count
)
3069 case SpvOpLoopMerge
:
3070 case SpvOpSelectionMerge
:
3071 /* This is handled by cfg pre-pass and walk_blocks */
3075 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
3076 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3081 vtn_handle_extension(b
, opcode
, w
, count
);
3087 case SpvOpCopyMemory
:
3088 case SpvOpCopyMemorySized
:
3089 case SpvOpAccessChain
:
3090 case SpvOpPtrAccessChain
:
3091 case SpvOpInBoundsAccessChain
:
3092 case SpvOpArrayLength
:
3093 vtn_handle_variables(b
, opcode
, w
, count
);
3096 case SpvOpFunctionCall
:
3097 vtn_handle_function_call(b
, opcode
, w
, count
);
3100 case SpvOpSampledImage
:
3102 case SpvOpImageSampleImplicitLod
:
3103 case SpvOpImageSampleExplicitLod
:
3104 case SpvOpImageSampleDrefImplicitLod
:
3105 case SpvOpImageSampleDrefExplicitLod
:
3106 case SpvOpImageSampleProjImplicitLod
:
3107 case SpvOpImageSampleProjExplicitLod
:
3108 case SpvOpImageSampleProjDrefImplicitLod
:
3109 case SpvOpImageSampleProjDrefExplicitLod
:
3110 case SpvOpImageFetch
:
3111 case SpvOpImageGather
:
3112 case SpvOpImageDrefGather
:
3113 case SpvOpImageQuerySizeLod
:
3114 case SpvOpImageQueryLod
:
3115 case SpvOpImageQueryLevels
:
3116 case SpvOpImageQuerySamples
:
3117 vtn_handle_texture(b
, opcode
, w
, count
);
3120 case SpvOpImageRead
:
3121 case SpvOpImageWrite
:
3122 case SpvOpImageTexelPointer
:
3123 vtn_handle_image(b
, opcode
, w
, count
);
3126 case SpvOpImageQuerySize
: {
3127 struct vtn_pointer
*image
=
3128 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
3129 if (image
->mode
== vtn_variable_mode_image
) {
3130 vtn_handle_image(b
, opcode
, w
, count
);
3132 assert(image
->mode
== vtn_variable_mode_sampler
);
3133 vtn_handle_texture(b
, opcode
, w
, count
);
3138 case SpvOpAtomicLoad
:
3139 case SpvOpAtomicExchange
:
3140 case SpvOpAtomicCompareExchange
:
3141 case SpvOpAtomicCompareExchangeWeak
:
3142 case SpvOpAtomicIIncrement
:
3143 case SpvOpAtomicIDecrement
:
3144 case SpvOpAtomicIAdd
:
3145 case SpvOpAtomicISub
:
3146 case SpvOpAtomicSMin
:
3147 case SpvOpAtomicUMin
:
3148 case SpvOpAtomicSMax
:
3149 case SpvOpAtomicUMax
:
3150 case SpvOpAtomicAnd
:
3152 case SpvOpAtomicXor
: {
3153 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
3154 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
3155 vtn_handle_image(b
, opcode
, w
, count
);
3157 assert(pointer
->value_type
== vtn_value_type_pointer
);
3158 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
3163 case SpvOpAtomicStore
: {
3164 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
3165 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
3166 vtn_handle_image(b
, opcode
, w
, count
);
3168 assert(pointer
->value_type
== vtn_value_type_pointer
);
3169 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
3175 /* Handle OpSelect up-front here because it needs to be able to handle
3176 * pointers and not just regular vectors and scalars.
3178 struct vtn_type
*res_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3179 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, res_type
->type
);
3180 ssa
->def
= nir_bcsel(&b
->nb
, vtn_ssa_value(b
, w
[3])->def
,
3181 vtn_ssa_value(b
, w
[4])->def
,
3182 vtn_ssa_value(b
, w
[5])->def
);
3183 vtn_push_ssa(b
, w
[2], res_type
, ssa
);
3192 case SpvOpConvertFToU
:
3193 case SpvOpConvertFToS
:
3194 case SpvOpConvertSToF
:
3195 case SpvOpConvertUToF
:
3199 case SpvOpQuantizeToF16
:
3200 case SpvOpConvertPtrToU
:
3201 case SpvOpConvertUToPtr
:
3202 case SpvOpPtrCastToGeneric
:
3203 case SpvOpGenericCastToPtr
:
3209 case SpvOpSignBitSet
:
3210 case SpvOpLessOrGreater
:
3212 case SpvOpUnordered
:
3227 case SpvOpVectorTimesScalar
:
3229 case SpvOpIAddCarry
:
3230 case SpvOpISubBorrow
:
3231 case SpvOpUMulExtended
:
3232 case SpvOpSMulExtended
:
3233 case SpvOpShiftRightLogical
:
3234 case SpvOpShiftRightArithmetic
:
3235 case SpvOpShiftLeftLogical
:
3236 case SpvOpLogicalEqual
:
3237 case SpvOpLogicalNotEqual
:
3238 case SpvOpLogicalOr
:
3239 case SpvOpLogicalAnd
:
3240 case SpvOpLogicalNot
:
3241 case SpvOpBitwiseOr
:
3242 case SpvOpBitwiseXor
:
3243 case SpvOpBitwiseAnd
:
3245 case SpvOpFOrdEqual
:
3246 case SpvOpFUnordEqual
:
3247 case SpvOpINotEqual
:
3248 case SpvOpFOrdNotEqual
:
3249 case SpvOpFUnordNotEqual
:
3250 case SpvOpULessThan
:
3251 case SpvOpSLessThan
:
3252 case SpvOpFOrdLessThan
:
3253 case SpvOpFUnordLessThan
:
3254 case SpvOpUGreaterThan
:
3255 case SpvOpSGreaterThan
:
3256 case SpvOpFOrdGreaterThan
:
3257 case SpvOpFUnordGreaterThan
:
3258 case SpvOpULessThanEqual
:
3259 case SpvOpSLessThanEqual
:
3260 case SpvOpFOrdLessThanEqual
:
3261 case SpvOpFUnordLessThanEqual
:
3262 case SpvOpUGreaterThanEqual
:
3263 case SpvOpSGreaterThanEqual
:
3264 case SpvOpFOrdGreaterThanEqual
:
3265 case SpvOpFUnordGreaterThanEqual
:
3271 case SpvOpFwidthFine
:
3272 case SpvOpDPdxCoarse
:
3273 case SpvOpDPdyCoarse
:
3274 case SpvOpFwidthCoarse
:
3275 case SpvOpBitFieldInsert
:
3276 case SpvOpBitFieldSExtract
:
3277 case SpvOpBitFieldUExtract
:
3278 case SpvOpBitReverse
:
3280 case SpvOpTranspose
:
3281 case SpvOpOuterProduct
:
3282 case SpvOpMatrixTimesScalar
:
3283 case SpvOpVectorTimesMatrix
:
3284 case SpvOpMatrixTimesVector
:
3285 case SpvOpMatrixTimesMatrix
:
3286 vtn_handle_alu(b
, opcode
, w
, count
);
3289 case SpvOpVectorExtractDynamic
:
3290 case SpvOpVectorInsertDynamic
:
3291 case SpvOpVectorShuffle
:
3292 case SpvOpCompositeConstruct
:
3293 case SpvOpCompositeExtract
:
3294 case SpvOpCompositeInsert
:
3295 case SpvOpCopyObject
:
3296 vtn_handle_composite(b
, opcode
, w
, count
);
3299 case SpvOpEmitVertex
:
3300 case SpvOpEndPrimitive
:
3301 case SpvOpEmitStreamVertex
:
3302 case SpvOpEndStreamPrimitive
:
3303 case SpvOpControlBarrier
:
3304 case SpvOpMemoryBarrier
:
3305 vtn_handle_barrier(b
, opcode
, w
, count
);
3309 unreachable("Unhandled opcode");
3316 spirv_to_nir(const uint32_t *words
, size_t word_count
,
3317 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
3318 gl_shader_stage stage
, const char *entry_point_name
,
3319 const struct spirv_to_nir_options
*options
,
3320 const nir_shader_compiler_options
*nir_options
)
3322 /* Initialize the stn_builder object */
3323 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
3324 exec_list_make_empty(&b
->functions
);
3325 b
->entry_point_stage
= stage
;
3326 b
->entry_point_name
= entry_point_name
;
3327 b
->options
= options
;
3329 const uint32_t *word_end
= words
+ word_count
;
3331 /* Handle the SPIR-V header (first 4 dwords) */
3332 assert(word_count
> 5);
3334 assert(words
[0] == SpvMagicNumber
);
3335 assert(words
[1] >= 0x10000);
3336 /* words[2] == generator magic */
3337 unsigned value_id_bound
= words
[3];
3338 assert(words
[4] == 0);
3342 b
->value_id_bound
= value_id_bound
;
3343 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
3345 /* Handle all the preamble instructions */
3346 words
= vtn_foreach_instruction(b
, words
, word_end
,
3347 vtn_handle_preamble_instruction
);
3349 if (b
->entry_point
== NULL
) {
3350 assert(!"Entry point not found");
3355 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
3357 /* Set shader info defaults */
3358 b
->shader
->info
.gs
.invocations
= 1;
3360 /* Parse execution modes */
3361 vtn_foreach_execution_mode(b
, b
->entry_point
,
3362 vtn_handle_execution_mode
, NULL
);
3364 b
->specializations
= spec
;
3365 b
->num_specializations
= num_spec
;
3367 /* Handle all variable, type, and constant instructions */
3368 words
= vtn_foreach_instruction(b
, words
, word_end
,
3369 vtn_handle_variable_or_type_instruction
);
3371 vtn_build_cfg(b
, words
, word_end
);
3373 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
3374 b
->entry_point
->func
->referenced
= true;
3379 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
3380 if (func
->referenced
&& !func
->emitted
) {
3381 b
->const_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
3382 _mesa_key_pointer_equal
);
3384 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
3390 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
3391 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
3392 assert(entry_point
);
3394 /* Unparent the shader from the vtn_builder before we delete the builder */
3395 ralloc_steal(NULL
, b
->shader
);