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
)) {
108 case GLSL_TYPE_FLOAT
:
109 case GLSL_TYPE_DOUBLE
: {
110 int bit_size
= glsl_get_bit_size(type
);
111 if (glsl_type_is_vector_or_scalar(type
)) {
112 unsigned num_components
= glsl_get_vector_elements(val
->type
);
113 nir_load_const_instr
*load
=
114 nir_load_const_instr_create(b
->shader
, num_components
, bit_size
);
116 load
->value
= constant
->values
[0];
118 nir_instr_insert_before_cf_list(&b
->impl
->body
, &load
->instr
);
119 val
->def
= &load
->def
;
121 assert(glsl_type_is_matrix(type
));
122 unsigned rows
= glsl_get_vector_elements(val
->type
);
123 unsigned columns
= glsl_get_matrix_columns(val
->type
);
124 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, columns
);
126 for (unsigned i
= 0; i
< columns
; i
++) {
127 struct vtn_ssa_value
*col_val
= rzalloc(b
, struct vtn_ssa_value
);
128 col_val
->type
= glsl_get_column_type(val
->type
);
129 nir_load_const_instr
*load
=
130 nir_load_const_instr_create(b
->shader
, rows
, bit_size
);
132 load
->value
= constant
->values
[i
];
134 nir_instr_insert_before_cf_list(&b
->impl
->body
, &load
->instr
);
135 col_val
->def
= &load
->def
;
137 val
->elems
[i
] = col_val
;
143 case GLSL_TYPE_ARRAY
: {
144 unsigned elems
= glsl_get_length(val
->type
);
145 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
146 const struct glsl_type
*elem_type
= glsl_get_array_element(val
->type
);
147 for (unsigned i
= 0; i
< elems
; i
++)
148 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
153 case GLSL_TYPE_STRUCT
: {
154 unsigned elems
= glsl_get_length(val
->type
);
155 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
156 for (unsigned i
= 0; i
< elems
; i
++) {
157 const struct glsl_type
*elem_type
=
158 glsl_get_struct_field(val
->type
, i
);
159 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
166 unreachable("bad constant type");
172 struct vtn_ssa_value
*
173 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
175 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
176 switch (val
->value_type
) {
177 case vtn_value_type_undef
:
178 return vtn_undef_ssa_value(b
, val
->type
->type
);
180 case vtn_value_type_constant
:
181 return vtn_const_ssa_value(b
, val
->constant
, val
->const_type
);
183 case vtn_value_type_ssa
:
186 case vtn_value_type_access_chain
:
187 /* This is needed for function parameters */
188 return vtn_variable_load(b
, val
->access_chain
);
191 unreachable("Invalid type for an SSA value");
196 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
197 unsigned word_count
, unsigned *words_used
)
199 char *dup
= ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
201 /* Ammount of space taken by the string (including the null) */
202 unsigned len
= strlen(dup
) + 1;
203 *words_used
= DIV_ROUND_UP(len
, sizeof(*words
));
209 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
210 const uint32_t *end
, vtn_instruction_handler handler
)
216 const uint32_t *w
= start
;
218 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
219 unsigned count
= w
[0] >> SpvWordCountShift
;
220 assert(count
>= 1 && w
+ count
<= end
);
224 break; /* Do nothing */
227 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
239 if (!handler(b
, opcode
, w
, count
))
251 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
252 const uint32_t *w
, unsigned count
)
255 case SpvOpExtInstImport
: {
256 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
257 if (strcmp((const char *)&w
[2], "GLSL.std.450") == 0) {
258 val
->ext_handler
= vtn_handle_glsl450_instruction
;
260 assert(!"Unsupported extension");
266 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
267 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
274 unreachable("Unhandled opcode");
279 _foreach_decoration_helper(struct vtn_builder
*b
,
280 struct vtn_value
*base_value
,
282 struct vtn_value
*value
,
283 vtn_decoration_foreach_cb cb
, void *data
)
285 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
287 if (dec
->scope
== VTN_DEC_DECORATION
) {
288 member
= parent_member
;
289 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
290 assert(parent_member
== -1);
291 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
293 /* Not a decoration */
298 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
299 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
302 cb(b
, base_value
, member
, dec
, data
);
307 /** Iterates (recursively if needed) over all of the decorations on a value
309 * This function iterates over all of the decorations applied to a given
310 * value. If it encounters a decoration group, it recurses into the group
311 * and iterates over all of those decorations as well.
314 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
315 vtn_decoration_foreach_cb cb
, void *data
)
317 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
321 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
322 vtn_execution_mode_foreach_cb cb
, void *data
)
324 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
325 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
328 assert(dec
->group
== NULL
);
329 cb(b
, value
, dec
, data
);
334 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
335 const uint32_t *w
, unsigned count
)
337 const uint32_t *w_end
= w
+ count
;
338 const uint32_t target
= w
[1];
342 case SpvOpDecorationGroup
:
343 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
347 case SpvOpMemberDecorate
:
348 case SpvOpExecutionMode
: {
349 struct vtn_value
*val
= &b
->values
[target
];
351 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
354 dec
->scope
= VTN_DEC_DECORATION
;
356 case SpvOpMemberDecorate
:
357 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
359 case SpvOpExecutionMode
:
360 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
363 unreachable("Invalid decoration opcode");
365 dec
->decoration
= *(w
++);
368 /* Link into the list */
369 dec
->next
= val
->decoration
;
370 val
->decoration
= dec
;
374 case SpvOpGroupMemberDecorate
:
375 case SpvOpGroupDecorate
: {
376 struct vtn_value
*group
=
377 vtn_value(b
, target
, vtn_value_type_decoration_group
);
379 for (; w
< w_end
; w
++) {
380 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
381 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
384 if (opcode
== SpvOpGroupDecorate
) {
385 dec
->scope
= VTN_DEC_DECORATION
;
387 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
390 /* Link into the list */
391 dec
->next
= val
->decoration
;
392 val
->decoration
= dec
;
398 unreachable("Unhandled opcode");
402 struct member_decoration_ctx
{
404 struct glsl_struct_field
*fields
;
405 struct vtn_type
*type
;
408 /* does a shallow copy of a vtn_type */
410 static struct vtn_type
*
411 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
413 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
414 dest
->type
= src
->type
;
415 dest
->is_builtin
= src
->is_builtin
;
417 dest
->builtin
= src
->builtin
;
419 if (!glsl_type_is_scalar(src
->type
)) {
420 switch (glsl_get_base_type(src
->type
)) {
424 case GLSL_TYPE_FLOAT
:
425 case GLSL_TYPE_DOUBLE
:
426 case GLSL_TYPE_ARRAY
:
427 dest
->row_major
= src
->row_major
;
428 dest
->stride
= src
->stride
;
429 dest
->array_element
= src
->array_element
;
432 case GLSL_TYPE_STRUCT
: {
433 unsigned elems
= glsl_get_length(src
->type
);
435 dest
->members
= ralloc_array(b
, struct vtn_type
*, elems
);
436 memcpy(dest
->members
, src
->members
, elems
* sizeof(struct vtn_type
*));
438 dest
->offsets
= ralloc_array(b
, unsigned, elems
);
439 memcpy(dest
->offsets
, src
->offsets
, elems
* sizeof(unsigned));
444 unreachable("unhandled type");
451 static struct vtn_type
*
452 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
454 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
455 type
= type
->members
[member
];
457 /* We may have an array of matrices.... Oh, joy! */
458 while (glsl_type_is_array(type
->type
)) {
459 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
460 type
= type
->array_element
;
463 assert(glsl_type_is_matrix(type
->type
));
469 struct_member_decoration_cb(struct vtn_builder
*b
,
470 struct vtn_value
*val
, int member
,
471 const struct vtn_decoration
*dec
, void *void_ctx
)
473 struct member_decoration_ctx
*ctx
= void_ctx
;
478 assert(member
< ctx
->num_fields
);
480 switch (dec
->decoration
) {
481 case SpvDecorationNonWritable
:
482 case SpvDecorationNonReadable
:
483 case SpvDecorationRelaxedPrecision
:
484 case SpvDecorationVolatile
:
485 case SpvDecorationCoherent
:
486 case SpvDecorationUniform
:
487 break; /* FIXME: Do nothing with this for now. */
488 case SpvDecorationNoPerspective
:
489 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
491 case SpvDecorationFlat
:
492 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
494 case SpvDecorationCentroid
:
495 ctx
->fields
[member
].centroid
= true;
497 case SpvDecorationSample
:
498 ctx
->fields
[member
].sample
= true;
500 case SpvDecorationStream
:
501 /* Vulkan only allows one GS stream */
502 assert(dec
->literals
[0] == 0);
504 case SpvDecorationLocation
:
505 ctx
->fields
[member
].location
= dec
->literals
[0];
507 case SpvDecorationComponent
:
508 break; /* FIXME: What should we do with these? */
509 case SpvDecorationBuiltIn
:
510 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
511 ctx
->type
->members
[member
]->is_builtin
= true;
512 ctx
->type
->members
[member
]->builtin
= dec
->literals
[0];
513 ctx
->type
->builtin_block
= true;
515 case SpvDecorationOffset
:
516 ctx
->type
->offsets
[member
] = dec
->literals
[0];
518 case SpvDecorationMatrixStride
:
519 mutable_matrix_member(b
, ctx
->type
, member
)->stride
= dec
->literals
[0];
521 case SpvDecorationColMajor
:
522 break; /* Nothing to do here. Column-major is the default. */
523 case SpvDecorationRowMajor
:
524 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
527 case SpvDecorationPatch
:
530 case SpvDecorationSpecId
:
531 case SpvDecorationBlock
:
532 case SpvDecorationBufferBlock
:
533 case SpvDecorationArrayStride
:
534 case SpvDecorationGLSLShared
:
535 case SpvDecorationGLSLPacked
:
536 case SpvDecorationInvariant
:
537 case SpvDecorationRestrict
:
538 case SpvDecorationAliased
:
539 case SpvDecorationConstant
:
540 case SpvDecorationIndex
:
541 case SpvDecorationBinding
:
542 case SpvDecorationDescriptorSet
:
543 case SpvDecorationLinkageAttributes
:
544 case SpvDecorationNoContraction
:
545 case SpvDecorationInputAttachmentIndex
:
546 vtn_warn("Decoration not allowed on struct members: %s",
547 spirv_decoration_to_string(dec
->decoration
));
550 case SpvDecorationXfbBuffer
:
551 case SpvDecorationXfbStride
:
552 vtn_warn("Vulkan does not have transform feedback");
555 case SpvDecorationCPacked
:
556 case SpvDecorationSaturatedConversion
:
557 case SpvDecorationFuncParamAttr
:
558 case SpvDecorationFPRoundingMode
:
559 case SpvDecorationFPFastMathMode
:
560 case SpvDecorationAlignment
:
561 vtn_warn("Decoration only allowed for CL-style kernels: %s",
562 spirv_decoration_to_string(dec
->decoration
));
566 unreachable("Unhandled decoration");
571 type_decoration_cb(struct vtn_builder
*b
,
572 struct vtn_value
*val
, int member
,
573 const struct vtn_decoration
*dec
, void *ctx
)
575 struct vtn_type
*type
= val
->type
;
580 switch (dec
->decoration
) {
581 case SpvDecorationArrayStride
:
582 type
->stride
= dec
->literals
[0];
584 case SpvDecorationBlock
:
587 case SpvDecorationBufferBlock
:
588 type
->buffer_block
= true;
590 case SpvDecorationGLSLShared
:
591 case SpvDecorationGLSLPacked
:
592 /* Ignore these, since we get explicit offsets anyways */
595 case SpvDecorationRowMajor
:
596 case SpvDecorationColMajor
:
597 case SpvDecorationMatrixStride
:
598 case SpvDecorationBuiltIn
:
599 case SpvDecorationNoPerspective
:
600 case SpvDecorationFlat
:
601 case SpvDecorationPatch
:
602 case SpvDecorationCentroid
:
603 case SpvDecorationSample
:
604 case SpvDecorationVolatile
:
605 case SpvDecorationCoherent
:
606 case SpvDecorationNonWritable
:
607 case SpvDecorationNonReadable
:
608 case SpvDecorationUniform
:
609 case SpvDecorationStream
:
610 case SpvDecorationLocation
:
611 case SpvDecorationComponent
:
612 case SpvDecorationOffset
:
613 case SpvDecorationXfbBuffer
:
614 case SpvDecorationXfbStride
:
615 vtn_warn("Decoration only allowed for struct members: %s",
616 spirv_decoration_to_string(dec
->decoration
));
619 case SpvDecorationRelaxedPrecision
:
620 case SpvDecorationSpecId
:
621 case SpvDecorationInvariant
:
622 case SpvDecorationRestrict
:
623 case SpvDecorationAliased
:
624 case SpvDecorationConstant
:
625 case SpvDecorationIndex
:
626 case SpvDecorationBinding
:
627 case SpvDecorationDescriptorSet
:
628 case SpvDecorationLinkageAttributes
:
629 case SpvDecorationNoContraction
:
630 case SpvDecorationInputAttachmentIndex
:
631 vtn_warn("Decoration not allowed on types: %s",
632 spirv_decoration_to_string(dec
->decoration
));
635 case SpvDecorationCPacked
:
636 case SpvDecorationSaturatedConversion
:
637 case SpvDecorationFuncParamAttr
:
638 case SpvDecorationFPRoundingMode
:
639 case SpvDecorationFPFastMathMode
:
640 case SpvDecorationAlignment
:
641 vtn_warn("Decoration only allowed for CL-style kernels: %s",
642 spirv_decoration_to_string(dec
->decoration
));
646 unreachable("Unhandled decoration");
651 translate_image_format(SpvImageFormat format
)
654 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
655 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
656 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
657 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
658 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
659 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
660 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
661 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
662 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
663 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
664 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
665 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
666 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
667 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
668 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
669 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
670 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
671 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
672 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
673 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
674 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
675 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
676 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
677 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
678 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
679 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
680 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
681 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
682 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
683 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
684 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
685 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
686 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
687 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
688 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
689 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
690 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
691 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
692 case SpvImageFormatR16ui
: return 0x823A; /* GL_RG16UI */
693 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
695 assert(!"Invalid image format");
701 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
702 const uint32_t *w
, unsigned count
)
704 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
706 val
->type
= rzalloc(b
, struct vtn_type
);
707 val
->type
->is_builtin
= false;
708 val
->type
->val
= val
;
712 val
->type
->type
= glsl_void_type();
715 val
->type
->type
= glsl_bool_type();
718 const bool signedness
= w
[3];
719 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
722 case SpvOpTypeFloat
: {
724 val
->type
->type
= bit_size
== 64 ? glsl_double_type() : glsl_float_type();
728 case SpvOpTypeVector
: {
729 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
730 unsigned elems
= w
[3];
732 assert(glsl_type_is_scalar(base
->type
));
733 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
735 /* Vectors implicitly have sizeof(base_type) stride. For now, this
736 * is always 4 bytes. This will have to change if we want to start
737 * supporting doubles or half-floats.
739 val
->type
->stride
= 4;
740 val
->type
->array_element
= base
;
744 case SpvOpTypeMatrix
: {
745 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
746 unsigned columns
= w
[3];
748 assert(glsl_type_is_vector(base
->type
));
749 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
750 glsl_get_vector_elements(base
->type
),
752 assert(!glsl_type_is_error(val
->type
->type
));
753 val
->type
->array_element
= base
;
754 val
->type
->row_major
= false;
755 val
->type
->stride
= 0;
759 case SpvOpTypeRuntimeArray
:
760 case SpvOpTypeArray
: {
761 struct vtn_type
*array_element
=
762 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
765 if (opcode
== SpvOpTypeRuntimeArray
) {
766 /* A length of 0 is used to denote unsized arrays */
770 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
773 val
->type
->type
= glsl_array_type(array_element
->type
, length
);
774 val
->type
->array_element
= array_element
;
775 val
->type
->stride
= 0;
779 case SpvOpTypeStruct
: {
780 unsigned num_fields
= count
- 2;
781 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
782 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
784 NIR_VLA(struct glsl_struct_field
, fields
, count
);
785 for (unsigned i
= 0; i
< num_fields
; i
++) {
786 val
->type
->members
[i
] =
787 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
788 fields
[i
] = (struct glsl_struct_field
) {
789 .type
= val
->type
->members
[i
]->type
,
790 .name
= ralloc_asprintf(b
, "field%d", i
),
795 struct member_decoration_ctx ctx
= {
796 .num_fields
= num_fields
,
801 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
803 const char *name
= val
->name
? val
->name
: "struct";
805 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
);
809 case SpvOpTypeFunction
: {
810 const struct glsl_type
*return_type
=
811 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
812 NIR_VLA(struct glsl_function_param
, params
, count
- 3);
813 for (unsigned i
= 0; i
< count
- 3; i
++) {
814 params
[i
].type
= vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
->type
;
818 params
[i
].out
= true;
820 val
->type
->type
= glsl_function_type(return_type
, params
, count
- 3);
824 case SpvOpTypePointer
:
825 /* FIXME: For now, we'll just do the really lame thing and return
826 * the same type. The validator should ensure that the proper number
827 * of dereferences happen
829 val
->type
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
832 case SpvOpTypeImage
: {
833 const struct glsl_type
*sampled_type
=
834 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
836 assert(glsl_type_is_vector_or_scalar(sampled_type
));
838 enum glsl_sampler_dim dim
;
839 switch ((SpvDim
)w
[3]) {
840 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
841 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
842 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
843 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
844 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
845 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
846 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
848 unreachable("Invalid SPIR-V Sampler dimension");
851 bool is_shadow
= w
[4];
852 bool is_array
= w
[5];
853 bool multisampled
= w
[6];
854 unsigned sampled
= w
[7];
855 SpvImageFormat format
= w
[8];
858 val
->type
->access_qualifier
= w
[9];
860 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
863 if (dim
== GLSL_SAMPLER_DIM_2D
)
864 dim
= GLSL_SAMPLER_DIM_MS
;
865 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
866 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
868 assert(!"Unsupported multisampled image type");
871 val
->type
->image_format
= translate_image_format(format
);
874 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
875 glsl_get_base_type(sampled_type
));
876 } else if (sampled
== 2) {
877 assert((dim
== GLSL_SAMPLER_DIM_SUBPASS
||
878 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) || format
);
880 val
->type
->type
= glsl_image_type(dim
, is_array
,
881 glsl_get_base_type(sampled_type
));
883 assert(!"We need to know if the image will be sampled");
888 case SpvOpTypeSampledImage
:
889 val
->type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
892 case SpvOpTypeSampler
:
893 /* The actual sampler type here doesn't really matter. It gets
894 * thrown away the moment you combine it with an image. What really
895 * matters is that it's a sampler type as opposed to an integer type
896 * so the backend knows what to do.
898 val
->type
->type
= glsl_bare_sampler_type();
901 case SpvOpTypeOpaque
:
903 case SpvOpTypeDeviceEvent
:
904 case SpvOpTypeReserveId
:
908 unreachable("Unhandled opcode");
911 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
914 static nir_constant
*
915 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
917 nir_constant
*c
= rzalloc(b
, nir_constant
);
919 switch (glsl_get_base_type(type
)) {
923 case GLSL_TYPE_FLOAT
:
924 case GLSL_TYPE_DOUBLE
:
925 /* Nothing to do here. It's already initialized to zero */
928 case GLSL_TYPE_ARRAY
:
929 assert(glsl_get_length(type
) > 0);
930 c
->num_elements
= glsl_get_length(type
);
931 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
933 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
934 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
935 c
->elements
[i
] = c
->elements
[0];
938 case GLSL_TYPE_STRUCT
:
939 c
->num_elements
= glsl_get_length(type
);
940 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
942 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
943 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
948 unreachable("Invalid type for null constant");
955 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
956 int member
, const struct vtn_decoration
*dec
,
959 assert(member
== -1);
960 if (dec
->decoration
!= SpvDecorationSpecId
)
963 struct spec_constant_value
*const_value
= data
;
965 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
966 if (b
->specializations
[i
].id
== dec
->literals
[0]) {
967 if (const_value
->is_double
)
968 const_value
->data64
= b
->specializations
[i
].data64
;
970 const_value
->data32
= b
->specializations
[i
].data32
;
977 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
978 uint32_t const_value
)
980 struct spec_constant_value data
;
981 data
.is_double
= false;
982 data
.data32
= const_value
;
983 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
988 get_specialization64(struct vtn_builder
*b
, struct vtn_value
*val
,
989 uint64_t const_value
)
991 struct spec_constant_value data
;
992 data
.is_double
= true;
993 data
.data64
= const_value
;
994 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
999 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1000 struct vtn_value
*val
,
1002 const struct vtn_decoration
*dec
,
1005 assert(member
== -1);
1006 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1007 dec
->literals
[0] != SpvBuiltInWorkgroupSize
)
1010 assert(val
->const_type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1012 b
->shader
->info
->cs
.local_size
[0] = val
->constant
->values
[0].u32
[0];
1013 b
->shader
->info
->cs
.local_size
[1] = val
->constant
->values
[0].u32
[1];
1014 b
->shader
->info
->cs
.local_size
[2] = val
->constant
->values
[0].u32
[2];
1018 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1019 const uint32_t *w
, unsigned count
)
1021 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1022 val
->const_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
1023 val
->constant
= rzalloc(b
, nir_constant
);
1025 case SpvOpConstantTrue
:
1026 assert(val
->const_type
== glsl_bool_type());
1027 val
->constant
->values
[0].u32
[0] = NIR_TRUE
;
1029 case SpvOpConstantFalse
:
1030 assert(val
->const_type
== glsl_bool_type());
1031 val
->constant
->values
[0].u32
[0] = NIR_FALSE
;
1034 case SpvOpSpecConstantTrue
:
1035 case SpvOpSpecConstantFalse
: {
1036 assert(val
->const_type
== glsl_bool_type());
1038 get_specialization(b
, val
, (opcode
== SpvOpSpecConstantTrue
));
1039 val
->constant
->values
[0].u32
[0] = int_val
? NIR_TRUE
: NIR_FALSE
;
1043 case SpvOpConstant
: {
1044 assert(glsl_type_is_scalar(val
->const_type
));
1045 int bit_size
= glsl_get_bit_size(val
->const_type
);
1046 if (bit_size
== 64) {
1047 val
->constant
->values
->u32
[0] = w
[3];
1048 val
->constant
->values
->u32
[1] = w
[4];
1050 assert(bit_size
== 32);
1051 val
->constant
->values
->u32
[0] = w
[3];
1055 case SpvOpSpecConstant
: {
1056 assert(glsl_type_is_scalar(val
->const_type
));
1057 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1058 int bit_size
= glsl_get_bit_size(val
->const_type
);
1060 val
->constant
->values
[0].u64
[0] =
1061 get_specialization64(b
, val
, vtn_u64_literal(&w
[3]));
1063 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1066 case SpvOpSpecConstantComposite
:
1067 case SpvOpConstantComposite
: {
1068 unsigned elem_count
= count
- 3;
1069 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1070 for (unsigned i
= 0; i
< elem_count
; i
++)
1071 elems
[i
] = vtn_value(b
, w
[i
+ 3], vtn_value_type_constant
)->constant
;
1073 switch (glsl_get_base_type(val
->const_type
)) {
1074 case GLSL_TYPE_UINT
:
1076 case GLSL_TYPE_FLOAT
:
1077 case GLSL_TYPE_BOOL
:
1078 case GLSL_TYPE_DOUBLE
: {
1079 int bit_size
= glsl_get_bit_size(val
->const_type
);
1080 if (glsl_type_is_matrix(val
->const_type
)) {
1081 assert(glsl_get_matrix_columns(val
->const_type
) == elem_count
);
1082 for (unsigned i
= 0; i
< elem_count
; i
++)
1083 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1085 assert(glsl_type_is_vector(val
->const_type
));
1086 assert(glsl_get_vector_elements(val
->const_type
) == elem_count
);
1087 for (unsigned i
= 0; i
< elem_count
; i
++) {
1088 if (bit_size
== 64) {
1089 val
->constant
->values
[0].u64
[i
] = elems
[i
]->values
[0].u64
[0];
1091 assert(bit_size
== 32);
1092 val
->constant
->values
[0].u32
[i
] = elems
[i
]->values
[0].u32
[0];
1099 case GLSL_TYPE_STRUCT
:
1100 case GLSL_TYPE_ARRAY
:
1101 ralloc_steal(val
->constant
, elems
);
1102 val
->constant
->num_elements
= elem_count
;
1103 val
->constant
->elements
= elems
;
1107 unreachable("Unsupported type for constants");
1112 case SpvOpSpecConstantOp
: {
1113 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1115 case SpvOpVectorShuffle
: {
1116 struct vtn_value
*v0
= &b
->values
[w
[4]];
1117 struct vtn_value
*v1
= &b
->values
[w
[5]];
1119 assert(v0
->value_type
== vtn_value_type_constant
||
1120 v0
->value_type
== vtn_value_type_undef
);
1121 assert(v1
->value_type
== vtn_value_type_constant
||
1122 v1
->value_type
== vtn_value_type_undef
);
1124 unsigned len0
= v0
->value_type
== vtn_value_type_constant
?
1125 glsl_get_vector_elements(v0
->const_type
) :
1126 glsl_get_vector_elements(v0
->type
->type
);
1127 unsigned len1
= v1
->value_type
== vtn_value_type_constant
?
1128 glsl_get_vector_elements(v1
->const_type
) :
1129 glsl_get_vector_elements(v1
->type
->type
);
1131 assert(len0
+ len1
< 16);
1133 unsigned bit_size
= glsl_get_bit_size(val
->const_type
);
1134 unsigned bit_size0
= v0
->value_type
== vtn_value_type_constant
?
1135 glsl_get_bit_size(v0
->const_type
) :
1136 glsl_get_bit_size(v0
->type
->type
);
1137 unsigned bit_size1
= v1
->value_type
== vtn_value_type_constant
?
1138 glsl_get_bit_size(v1
->const_type
) :
1139 glsl_get_bit_size(v1
->type
->type
);
1141 assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1143 if (bit_size
== 64) {
1145 if (v0
->value_type
== vtn_value_type_constant
) {
1146 for (unsigned i
= 0; i
< len0
; i
++)
1147 u64
[i
] = v0
->constant
->values
[0].u64
[i
];
1149 if (v1
->value_type
== vtn_value_type_constant
) {
1150 for (unsigned i
= 0; i
< len1
; i
++)
1151 u64
[len0
+ i
] = v1
->constant
->values
[0].u64
[i
];
1154 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1155 uint32_t comp
= w
[i
+ 6];
1156 /* If component is not used, set the value to a known constant
1157 * to detect if it is wrongly used.
1159 if (comp
== (uint32_t)-1)
1160 val
->constant
->values
[0].u64
[j
] = 0xdeadbeefdeadbeef;
1162 val
->constant
->values
[0].u64
[j
] = u64
[comp
];
1166 if (v0
->value_type
== vtn_value_type_constant
) {
1167 for (unsigned i
= 0; i
< len0
; i
++)
1168 u32
[i
] = v0
->constant
->values
[0].u32
[i
];
1170 if (v1
->value_type
== vtn_value_type_constant
) {
1171 for (unsigned i
= 0; i
< len1
; i
++)
1172 u32
[len0
+ i
] = v1
->constant
->values
[0].u32
[i
];
1175 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1176 uint32_t comp
= w
[i
+ 6];
1177 /* If component is not used, set the value to a known constant
1178 * to detect if it is wrongly used.
1180 if (comp
== (uint32_t)-1)
1181 val
->constant
->values
[0].u32
[j
] = 0xdeadbeef;
1183 val
->constant
->values
[0].u32
[j
] = u32
[comp
];
1189 case SpvOpCompositeExtract
:
1190 case SpvOpCompositeInsert
: {
1191 struct vtn_value
*comp
;
1192 unsigned deref_start
;
1193 struct nir_constant
**c
;
1194 if (opcode
== SpvOpCompositeExtract
) {
1195 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1197 c
= &comp
->constant
;
1199 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1201 val
->constant
= nir_constant_clone(comp
->constant
,
1208 const struct glsl_type
*type
= comp
->const_type
;
1209 for (unsigned i
= deref_start
; i
< count
; i
++) {
1210 switch (glsl_get_base_type(type
)) {
1211 case GLSL_TYPE_UINT
:
1213 case GLSL_TYPE_FLOAT
:
1214 case GLSL_TYPE_DOUBLE
:
1215 case GLSL_TYPE_BOOL
:
1216 /* If we hit this granularity, we're picking off an element */
1217 if (glsl_type_is_matrix(type
)) {
1218 assert(col
== 0 && elem
== -1);
1221 type
= glsl_get_column_type(type
);
1223 assert(elem
<= 0 && glsl_type_is_vector(type
));
1225 type
= glsl_scalar_type(glsl_get_base_type(type
));
1229 case GLSL_TYPE_ARRAY
:
1230 c
= &(*c
)->elements
[w
[i
]];
1231 type
= glsl_get_array_element(type
);
1234 case GLSL_TYPE_STRUCT
:
1235 c
= &(*c
)->elements
[w
[i
]];
1236 type
= glsl_get_struct_field(type
, w
[i
]);
1240 unreachable("Invalid constant type");
1244 if (opcode
== SpvOpCompositeExtract
) {
1248 unsigned num_components
= glsl_get_vector_elements(type
);
1249 unsigned bit_size
= glsl_get_bit_size(type
);
1250 for (unsigned i
= 0; i
< num_components
; i
++)
1251 if (bit_size
== 64) {
1252 val
->constant
->values
[0].u64
[i
] = (*c
)->values
[col
].u64
[elem
+ i
];
1254 assert(bit_size
== 32);
1255 val
->constant
->values
[0].u32
[i
] = (*c
)->values
[col
].u32
[elem
+ i
];
1259 struct vtn_value
*insert
=
1260 vtn_value(b
, w
[4], vtn_value_type_constant
);
1261 assert(insert
->const_type
== type
);
1263 *c
= insert
->constant
;
1265 unsigned num_components
= glsl_get_vector_elements(type
);
1266 unsigned bit_size
= glsl_get_bit_size(type
);
1267 for (unsigned i
= 0; i
< num_components
; i
++)
1268 if (bit_size
== 64) {
1269 (*c
)->values
[col
].u64
[elem
+ i
] = insert
->constant
->values
[0].u64
[i
];
1271 assert(bit_size
== 32);
1272 (*c
)->values
[col
].u32
[elem
+ i
] = insert
->constant
->values
[0].u32
[i
];
1281 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->const_type
);
1282 nir_alu_type src_alu_type
= dst_alu_type
;
1283 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(opcode
, &swap
, src_alu_type
, dst_alu_type
);
1285 unsigned num_components
= glsl_get_vector_elements(val
->const_type
);
1287 glsl_get_bit_size(val
->const_type
);
1289 nir_const_value src
[4];
1291 for (unsigned i
= 0; i
< count
- 4; i
++) {
1293 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
)->constant
;
1295 unsigned j
= swap
? 1 - i
: i
;
1296 assert(bit_size
== 32);
1297 src
[j
] = c
->values
[0];
1300 val
->constant
->values
[0] =
1301 nir_eval_const_opcode(op
, num_components
, bit_size
, src
);
1308 case SpvOpConstantNull
:
1309 val
->constant
= vtn_null_constant(b
, val
->const_type
);
1312 case SpvOpConstantSampler
:
1313 assert(!"OpConstantSampler requires Kernel Capability");
1317 unreachable("Unhandled opcode");
1320 /* Now that we have the value, update the workgroup size if needed */
1321 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1325 vtn_handle_function_call(struct vtn_builder
*b
, SpvOp opcode
,
1326 const uint32_t *w
, unsigned count
)
1328 struct nir_function
*callee
=
1329 vtn_value(b
, w
[3], vtn_value_type_function
)->func
->impl
->function
;
1331 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, callee
);
1332 for (unsigned i
= 0; i
< call
->num_params
; i
++) {
1333 unsigned arg_id
= w
[4 + i
];
1334 struct vtn_value
*arg
= vtn_untyped_value(b
, arg_id
);
1335 if (arg
->value_type
== vtn_value_type_access_chain
) {
1336 nir_deref_var
*d
= vtn_access_chain_to_deref(b
, arg
->access_chain
);
1337 call
->params
[i
] = nir_deref_var_clone(d
, call
);
1339 struct vtn_ssa_value
*arg_ssa
= vtn_ssa_value(b
, arg_id
);
1341 /* Make a temporary to store the argument in */
1343 nir_local_variable_create(b
->impl
, arg_ssa
->type
, "arg_tmp");
1344 call
->params
[i
] = nir_deref_var_create(call
, tmp
);
1346 vtn_local_store(b
, arg_ssa
, call
->params
[i
]);
1350 nir_variable
*out_tmp
= NULL
;
1351 if (!glsl_type_is_void(callee
->return_type
)) {
1352 out_tmp
= nir_local_variable_create(b
->impl
, callee
->return_type
,
1354 call
->return_deref
= nir_deref_var_create(call
, out_tmp
);
1357 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
1359 if (glsl_type_is_void(callee
->return_type
)) {
1360 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1362 struct vtn_value
*retval
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1363 retval
->ssa
= vtn_local_load(b
, call
->return_deref
);
1367 struct vtn_ssa_value
*
1368 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1370 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1373 if (!glsl_type_is_vector_or_scalar(type
)) {
1374 unsigned elems
= glsl_get_length(type
);
1375 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1376 for (unsigned i
= 0; i
< elems
; i
++) {
1377 const struct glsl_type
*child_type
;
1379 switch (glsl_get_base_type(type
)) {
1381 case GLSL_TYPE_UINT
:
1382 case GLSL_TYPE_BOOL
:
1383 case GLSL_TYPE_FLOAT
:
1384 case GLSL_TYPE_DOUBLE
:
1385 child_type
= glsl_get_column_type(type
);
1387 case GLSL_TYPE_ARRAY
:
1388 child_type
= glsl_get_array_element(type
);
1390 case GLSL_TYPE_STRUCT
:
1391 child_type
= glsl_get_struct_field(type
, i
);
1394 unreachable("unkown base type");
1397 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
1405 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
1408 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
1409 src
.src_type
= type
;
1414 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
1415 const uint32_t *w
, unsigned count
)
1417 if (opcode
== SpvOpSampledImage
) {
1418 struct vtn_value
*val
=
1419 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1420 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1421 val
->sampled_image
->image
=
1422 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1423 val
->sampled_image
->sampler
=
1424 vtn_value(b
, w
[4], vtn_value_type_access_chain
)->access_chain
;
1426 } else if (opcode
== SpvOpImage
) {
1427 struct vtn_value
*val
=
1428 vtn_push_value(b
, w
[2], vtn_value_type_access_chain
);
1429 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
1430 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
1431 val
->access_chain
= src_val
->sampled_image
->image
;
1433 assert(src_val
->value_type
== vtn_value_type_access_chain
);
1434 val
->access_chain
= src_val
->access_chain
;
1439 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1440 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1442 struct vtn_sampled_image sampled
;
1443 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
1444 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
1445 sampled
= *sampled_val
->sampled_image
;
1447 assert(sampled_val
->value_type
== vtn_value_type_access_chain
);
1448 sampled
.image
= NULL
;
1449 sampled
.sampler
= sampled_val
->access_chain
;
1452 const struct glsl_type
*image_type
;
1453 if (sampled
.image
) {
1454 image_type
= sampled
.image
->var
->var
->interface_type
;
1456 image_type
= sampled
.sampler
->var
->var
->interface_type
;
1458 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
1459 const bool is_array
= glsl_sampler_type_is_array(image_type
);
1460 const bool is_shadow
= glsl_sampler_type_is_shadow(image_type
);
1462 /* Figure out the base texture operation */
1465 case SpvOpImageSampleImplicitLod
:
1466 case SpvOpImageSampleDrefImplicitLod
:
1467 case SpvOpImageSampleProjImplicitLod
:
1468 case SpvOpImageSampleProjDrefImplicitLod
:
1469 texop
= nir_texop_tex
;
1472 case SpvOpImageSampleExplicitLod
:
1473 case SpvOpImageSampleDrefExplicitLod
:
1474 case SpvOpImageSampleProjExplicitLod
:
1475 case SpvOpImageSampleProjDrefExplicitLod
:
1476 texop
= nir_texop_txl
;
1479 case SpvOpImageFetch
:
1480 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
1481 texop
= nir_texop_txf_ms
;
1483 texop
= nir_texop_txf
;
1487 case SpvOpImageGather
:
1488 case SpvOpImageDrefGather
:
1489 texop
= nir_texop_tg4
;
1492 case SpvOpImageQuerySizeLod
:
1493 case SpvOpImageQuerySize
:
1494 texop
= nir_texop_txs
;
1497 case SpvOpImageQueryLod
:
1498 texop
= nir_texop_lod
;
1501 case SpvOpImageQueryLevels
:
1502 texop
= nir_texop_query_levels
;
1505 case SpvOpImageQuerySamples
:
1506 texop
= nir_texop_texture_samples
;
1510 unreachable("Unhandled opcode");
1513 nir_tex_src srcs
[8]; /* 8 should be enough */
1514 nir_tex_src
*p
= srcs
;
1518 struct nir_ssa_def
*coord
;
1519 unsigned coord_components
;
1521 case SpvOpImageSampleImplicitLod
:
1522 case SpvOpImageSampleExplicitLod
:
1523 case SpvOpImageSampleDrefImplicitLod
:
1524 case SpvOpImageSampleDrefExplicitLod
:
1525 case SpvOpImageSampleProjImplicitLod
:
1526 case SpvOpImageSampleProjExplicitLod
:
1527 case SpvOpImageSampleProjDrefImplicitLod
:
1528 case SpvOpImageSampleProjDrefExplicitLod
:
1529 case SpvOpImageFetch
:
1530 case SpvOpImageGather
:
1531 case SpvOpImageDrefGather
:
1532 case SpvOpImageQueryLod
: {
1533 /* All these types have the coordinate as their first real argument */
1534 switch (sampler_dim
) {
1535 case GLSL_SAMPLER_DIM_1D
:
1536 case GLSL_SAMPLER_DIM_BUF
:
1537 coord_components
= 1;
1539 case GLSL_SAMPLER_DIM_2D
:
1540 case GLSL_SAMPLER_DIM_RECT
:
1541 case GLSL_SAMPLER_DIM_MS
:
1542 coord_components
= 2;
1544 case GLSL_SAMPLER_DIM_3D
:
1545 case GLSL_SAMPLER_DIM_CUBE
:
1546 coord_components
= 3;
1549 unreachable("Invalid sampler type");
1552 if (is_array
&& texop
!= nir_texop_lod
)
1555 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
1556 p
->src
= nir_src_for_ssa(coord
);
1557 p
->src_type
= nir_tex_src_coord
;
1564 coord_components
= 0;
1569 case SpvOpImageSampleProjImplicitLod
:
1570 case SpvOpImageSampleProjExplicitLod
:
1571 case SpvOpImageSampleProjDrefImplicitLod
:
1572 case SpvOpImageSampleProjDrefExplicitLod
:
1573 /* These have the projector as the last coordinate component */
1574 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
1575 p
->src_type
= nir_tex_src_projector
;
1583 unsigned gather_component
= 0;
1585 case SpvOpImageSampleDrefImplicitLod
:
1586 case SpvOpImageSampleDrefExplicitLod
:
1587 case SpvOpImageSampleProjDrefImplicitLod
:
1588 case SpvOpImageSampleProjDrefExplicitLod
:
1589 case SpvOpImageDrefGather
:
1590 /* These all have an explicit depth value as their next source */
1591 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
1594 case SpvOpImageGather
:
1595 /* This has a component as its next source */
1597 vtn_value(b
, w
[idx
++], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
1604 /* For OpImageQuerySizeLod, we always have an LOD */
1605 if (opcode
== SpvOpImageQuerySizeLod
)
1606 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1608 /* Now we need to handle some number of optional arguments */
1609 const struct vtn_ssa_value
*gather_offsets
= NULL
;
1611 uint32_t operands
= w
[idx
++];
1613 if (operands
& SpvImageOperandsBiasMask
) {
1614 assert(texop
== nir_texop_tex
);
1615 texop
= nir_texop_txb
;
1616 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
1619 if (operands
& SpvImageOperandsLodMask
) {
1620 assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
1621 texop
== nir_texop_txs
);
1622 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1625 if (operands
& SpvImageOperandsGradMask
) {
1626 assert(texop
== nir_texop_txl
);
1627 texop
= nir_texop_txd
;
1628 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
1629 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
1632 if (operands
& SpvImageOperandsOffsetMask
||
1633 operands
& SpvImageOperandsConstOffsetMask
)
1634 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
1636 if (operands
& SpvImageOperandsConstOffsetsMask
) {
1637 gather_offsets
= vtn_ssa_value(b
, w
[idx
++]);
1638 (*p
++) = (nir_tex_src
){};
1641 if (operands
& SpvImageOperandsSampleMask
) {
1642 assert(texop
== nir_texop_txf_ms
);
1643 texop
= nir_texop_txf_ms
;
1644 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
1647 /* We should have now consumed exactly all of the arguments */
1648 assert(idx
== count
);
1650 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
1653 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1655 instr
->coord_components
= coord_components
;
1656 instr
->sampler_dim
= sampler_dim
;
1657 instr
->is_array
= is_array
;
1658 instr
->is_shadow
= is_shadow
;
1659 instr
->is_new_style_shadow
=
1660 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
1661 instr
->component
= gather_component
;
1663 switch (glsl_get_sampler_result_type(image_type
)) {
1664 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
1665 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
1666 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
1667 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
1669 unreachable("Invalid base type for sampler result");
1672 nir_deref_var
*sampler
= vtn_access_chain_to_deref(b
, sampled
.sampler
);
1673 nir_deref_var
*texture
;
1674 if (sampled
.image
) {
1675 nir_deref_var
*image
= vtn_access_chain_to_deref(b
, sampled
.image
);
1681 instr
->texture
= nir_deref_var_clone(texture
, instr
);
1683 switch (instr
->op
) {
1688 /* These operations require a sampler */
1689 instr
->sampler
= nir_deref_var_clone(sampler
, instr
);
1692 case nir_texop_txf_ms
:
1696 case nir_texop_query_levels
:
1697 case nir_texop_texture_samples
:
1698 case nir_texop_samples_identical
:
1700 instr
->sampler
= NULL
;
1702 case nir_texop_txf_ms_mcs
:
1703 unreachable("unexpected nir_texop_txf_ms_mcs");
1706 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1707 nir_tex_instr_dest_size(instr
), 32, NULL
);
1709 assert(glsl_get_vector_elements(ret_type
->type
) ==
1710 nir_tex_instr_dest_size(instr
));
1713 nir_instr
*instruction
;
1714 if (gather_offsets
) {
1715 assert(glsl_get_base_type(gather_offsets
->type
) == GLSL_TYPE_ARRAY
);
1716 assert(glsl_get_length(gather_offsets
->type
) == 4);
1717 nir_tex_instr
*instrs
[4] = {instr
, NULL
, NULL
, NULL
};
1719 /* Copy the current instruction 4x */
1720 for (uint32_t i
= 1; i
< 4; i
++) {
1721 instrs
[i
] = nir_tex_instr_create(b
->shader
, instr
->num_srcs
);
1722 instrs
[i
]->op
= instr
->op
;
1723 instrs
[i
]->coord_components
= instr
->coord_components
;
1724 instrs
[i
]->sampler_dim
= instr
->sampler_dim
;
1725 instrs
[i
]->is_array
= instr
->is_array
;
1726 instrs
[i
]->is_shadow
= instr
->is_shadow
;
1727 instrs
[i
]->is_new_style_shadow
= instr
->is_new_style_shadow
;
1728 instrs
[i
]->component
= instr
->component
;
1729 instrs
[i
]->dest_type
= instr
->dest_type
;
1730 instrs
[i
]->texture
= nir_deref_var_clone(texture
, instrs
[i
]);
1731 instrs
[i
]->sampler
= NULL
;
1733 memcpy(instrs
[i
]->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1735 nir_ssa_dest_init(&instrs
[i
]->instr
, &instrs
[i
]->dest
,
1736 nir_tex_instr_dest_size(instr
), 32, NULL
);
1739 /* Fill in the last argument with the offset from the passed in offsets
1740 * and insert the instruction into the stream.
1742 for (uint32_t i
= 0; i
< 4; i
++) {
1744 src
.src
= nir_src_for_ssa(gather_offsets
->elems
[i
]->def
);
1745 src
.src_type
= nir_tex_src_offset
;
1746 instrs
[i
]->src
[instrs
[i
]->num_srcs
- 1] = src
;
1747 nir_builder_instr_insert(&b
->nb
, &instrs
[i
]->instr
);
1750 /* Combine the results of the 4 instructions by taking their .w
1753 nir_alu_instr
*vec4
= nir_alu_instr_create(b
->shader
, nir_op_vec4
);
1754 nir_ssa_dest_init(&vec4
->instr
, &vec4
->dest
.dest
, 4, 32, NULL
);
1755 vec4
->dest
.write_mask
= 0xf;
1756 for (uint32_t i
= 0; i
< 4; i
++) {
1757 vec4
->src
[i
].src
= nir_src_for_ssa(&instrs
[i
]->dest
.ssa
);
1758 vec4
->src
[i
].swizzle
[0] = 3;
1760 def
= &vec4
->dest
.dest
.ssa
;
1761 instruction
= &vec4
->instr
;
1763 def
= &instr
->dest
.ssa
;
1764 instruction
= &instr
->instr
;
1767 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
1768 val
->ssa
->def
= def
;
1770 nir_builder_instr_insert(&b
->nb
, instruction
);
1774 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
1775 const uint32_t *w
, nir_src
*src
)
1778 case SpvOpAtomicIIncrement
:
1779 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
1782 case SpvOpAtomicIDecrement
:
1783 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
1786 case SpvOpAtomicISub
:
1788 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
1791 case SpvOpAtomicCompareExchange
:
1792 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
1793 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
1796 case SpvOpAtomicExchange
:
1797 case SpvOpAtomicIAdd
:
1798 case SpvOpAtomicSMin
:
1799 case SpvOpAtomicUMin
:
1800 case SpvOpAtomicSMax
:
1801 case SpvOpAtomicUMax
:
1802 case SpvOpAtomicAnd
:
1804 case SpvOpAtomicXor
:
1805 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1809 unreachable("Invalid SPIR-V atomic");
1813 static nir_ssa_def
*
1814 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
1816 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
1818 /* The image_load_store intrinsics assume a 4-dim coordinate */
1819 unsigned dim
= glsl_get_vector_elements(coord
->type
);
1820 unsigned swizzle
[4];
1821 for (unsigned i
= 0; i
< 4; i
++)
1822 swizzle
[i
] = MIN2(i
, dim
- 1);
1824 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
1828 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
1829 const uint32_t *w
, unsigned count
)
1831 /* Just get this one out of the way */
1832 if (opcode
== SpvOpImageTexelPointer
) {
1833 struct vtn_value
*val
=
1834 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
1835 val
->image
= ralloc(b
, struct vtn_image_pointer
);
1838 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1839 val
->image
->coord
= get_image_coord(b
, w
[4]);
1840 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
1844 struct vtn_image_pointer image
;
1847 case SpvOpAtomicExchange
:
1848 case SpvOpAtomicCompareExchange
:
1849 case SpvOpAtomicCompareExchangeWeak
:
1850 case SpvOpAtomicIIncrement
:
1851 case SpvOpAtomicIDecrement
:
1852 case SpvOpAtomicIAdd
:
1853 case SpvOpAtomicISub
:
1854 case SpvOpAtomicLoad
:
1855 case SpvOpAtomicSMin
:
1856 case SpvOpAtomicUMin
:
1857 case SpvOpAtomicSMax
:
1858 case SpvOpAtomicUMax
:
1859 case SpvOpAtomicAnd
:
1861 case SpvOpAtomicXor
:
1862 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
1865 case SpvOpAtomicStore
:
1866 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
1869 case SpvOpImageQuerySize
:
1871 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1873 image
.sample
= NULL
;
1876 case SpvOpImageRead
:
1878 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1879 image
.coord
= get_image_coord(b
, w
[4]);
1881 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
1882 assert(w
[5] == SpvImageOperandsSampleMask
);
1883 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
1885 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1889 case SpvOpImageWrite
:
1891 vtn_value(b
, w
[1], vtn_value_type_access_chain
)->access_chain
;
1892 image
.coord
= get_image_coord(b
, w
[2]);
1896 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
1897 assert(w
[4] == SpvImageOperandsSampleMask
);
1898 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
1900 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1905 unreachable("Invalid image opcode");
1908 nir_intrinsic_op op
;
1910 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_##N; break;
1911 OP(ImageQuerySize
, size
)
1913 OP(ImageWrite
, store
)
1914 OP(AtomicLoad
, load
)
1915 OP(AtomicStore
, store
)
1916 OP(AtomicExchange
, atomic_exchange
)
1917 OP(AtomicCompareExchange
, atomic_comp_swap
)
1918 OP(AtomicIIncrement
, atomic_add
)
1919 OP(AtomicIDecrement
, atomic_add
)
1920 OP(AtomicIAdd
, atomic_add
)
1921 OP(AtomicISub
, atomic_add
)
1922 OP(AtomicSMin
, atomic_min
)
1923 OP(AtomicUMin
, atomic_min
)
1924 OP(AtomicSMax
, atomic_max
)
1925 OP(AtomicUMax
, atomic_max
)
1926 OP(AtomicAnd
, atomic_and
)
1927 OP(AtomicOr
, atomic_or
)
1928 OP(AtomicXor
, atomic_xor
)
1931 unreachable("Invalid image opcode");
1934 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
1936 nir_deref_var
*image_deref
= vtn_access_chain_to_deref(b
, image
.image
);
1937 intrin
->variables
[0] = nir_deref_var_clone(image_deref
, intrin
);
1939 /* ImageQuerySize doesn't take any extra parameters */
1940 if (opcode
!= SpvOpImageQuerySize
) {
1941 /* The image coordinate is always 4 components but we may not have that
1942 * many. Swizzle to compensate.
1945 for (unsigned i
= 0; i
< 4; i
++)
1946 swiz
[i
] = i
< image
.coord
->num_components
? i
: 0;
1947 intrin
->src
[0] = nir_src_for_ssa(nir_swizzle(&b
->nb
, image
.coord
,
1949 intrin
->src
[1] = nir_src_for_ssa(image
.sample
);
1953 case SpvOpAtomicLoad
:
1954 case SpvOpImageQuerySize
:
1955 case SpvOpImageRead
:
1957 case SpvOpAtomicStore
:
1958 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
1960 case SpvOpImageWrite
:
1961 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[3])->def
);
1964 case SpvOpAtomicIIncrement
:
1965 case SpvOpAtomicIDecrement
:
1966 case SpvOpAtomicExchange
:
1967 case SpvOpAtomicIAdd
:
1968 case SpvOpAtomicSMin
:
1969 case SpvOpAtomicUMin
:
1970 case SpvOpAtomicSMax
:
1971 case SpvOpAtomicUMax
:
1972 case SpvOpAtomicAnd
:
1974 case SpvOpAtomicXor
:
1975 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[2]);
1979 unreachable("Invalid image opcode");
1982 if (opcode
!= SpvOpImageWrite
) {
1983 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1984 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1985 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 4, 32, NULL
);
1987 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
1989 /* The image intrinsics always return 4 channels but we may not want
1990 * that many. Emit a mov to trim it down.
1992 unsigned swiz
[4] = {0, 1, 2, 3};
1993 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
1994 val
->ssa
->def
= nir_swizzle(&b
->nb
, &intrin
->dest
.ssa
, swiz
,
1995 glsl_get_vector_elements(type
->type
), false);
1997 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2001 static nir_intrinsic_op
2002 get_ssbo_nir_atomic_op(SpvOp opcode
)
2005 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2006 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2007 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2008 OP(AtomicExchange
, atomic_exchange
)
2009 OP(AtomicCompareExchange
, atomic_comp_swap
)
2010 OP(AtomicIIncrement
, atomic_add
)
2011 OP(AtomicIDecrement
, atomic_add
)
2012 OP(AtomicIAdd
, atomic_add
)
2013 OP(AtomicISub
, atomic_add
)
2014 OP(AtomicSMin
, atomic_imin
)
2015 OP(AtomicUMin
, atomic_umin
)
2016 OP(AtomicSMax
, atomic_imax
)
2017 OP(AtomicUMax
, atomic_umax
)
2018 OP(AtomicAnd
, atomic_and
)
2019 OP(AtomicOr
, atomic_or
)
2020 OP(AtomicXor
, atomic_xor
)
2023 unreachable("Invalid SSBO atomic");
2027 static nir_intrinsic_op
2028 get_shared_nir_atomic_op(SpvOp opcode
)
2031 case SpvOpAtomicLoad
: return nir_intrinsic_load_var
;
2032 case SpvOpAtomicStore
: return nir_intrinsic_store_var
;
2033 #define OP(S, N) case SpvOp##S: return nir_intrinsic_var_##N;
2034 OP(AtomicExchange
, atomic_exchange
)
2035 OP(AtomicCompareExchange
, atomic_comp_swap
)
2036 OP(AtomicIIncrement
, atomic_add
)
2037 OP(AtomicIDecrement
, atomic_add
)
2038 OP(AtomicIAdd
, atomic_add
)
2039 OP(AtomicISub
, atomic_add
)
2040 OP(AtomicSMin
, atomic_imin
)
2041 OP(AtomicUMin
, atomic_umin
)
2042 OP(AtomicSMax
, atomic_imax
)
2043 OP(AtomicUMax
, atomic_umax
)
2044 OP(AtomicAnd
, atomic_and
)
2045 OP(AtomicOr
, atomic_or
)
2046 OP(AtomicXor
, atomic_xor
)
2049 unreachable("Invalid shared atomic");
2054 vtn_handle_ssbo_or_shared_atomic(struct vtn_builder
*b
, SpvOp opcode
,
2055 const uint32_t *w
, unsigned count
)
2057 struct vtn_access_chain
*chain
;
2058 nir_intrinsic_instr
*atomic
;
2061 case SpvOpAtomicLoad
:
2062 case SpvOpAtomicExchange
:
2063 case SpvOpAtomicCompareExchange
:
2064 case SpvOpAtomicCompareExchangeWeak
:
2065 case SpvOpAtomicIIncrement
:
2066 case SpvOpAtomicIDecrement
:
2067 case SpvOpAtomicIAdd
:
2068 case SpvOpAtomicISub
:
2069 case SpvOpAtomicSMin
:
2070 case SpvOpAtomicUMin
:
2071 case SpvOpAtomicSMax
:
2072 case SpvOpAtomicUMax
:
2073 case SpvOpAtomicAnd
:
2075 case SpvOpAtomicXor
:
2077 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
2080 case SpvOpAtomicStore
:
2082 vtn_value(b
, w
[1], vtn_value_type_access_chain
)->access_chain
;
2086 unreachable("Invalid SPIR-V atomic");
2090 SpvScope scope = w[4];
2091 SpvMemorySemanticsMask semantics = w[5];
2094 if (chain
->var
->mode
== vtn_variable_mode_workgroup
) {
2095 struct vtn_type
*type
= chain
->var
->type
;
2096 nir_deref_var
*deref
= vtn_access_chain_to_deref(b
, chain
);
2097 nir_intrinsic_op op
= get_shared_nir_atomic_op(opcode
);
2098 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2099 atomic
->variables
[0] = nir_deref_var_clone(deref
, atomic
);
2102 case SpvOpAtomicLoad
:
2103 atomic
->num_components
= glsl_get_vector_elements(type
->type
);
2106 case SpvOpAtomicStore
:
2107 atomic
->num_components
= glsl_get_vector_elements(type
->type
);
2108 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2109 atomic
->src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2112 case SpvOpAtomicExchange
:
2113 case SpvOpAtomicCompareExchange
:
2114 case SpvOpAtomicCompareExchangeWeak
:
2115 case SpvOpAtomicIIncrement
:
2116 case SpvOpAtomicIDecrement
:
2117 case SpvOpAtomicIAdd
:
2118 case SpvOpAtomicISub
:
2119 case SpvOpAtomicSMin
:
2120 case SpvOpAtomicUMin
:
2121 case SpvOpAtomicSMax
:
2122 case SpvOpAtomicUMax
:
2123 case SpvOpAtomicAnd
:
2125 case SpvOpAtomicXor
:
2126 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[0]);
2130 unreachable("Invalid SPIR-V atomic");
2134 assert(chain
->var
->mode
== vtn_variable_mode_ssbo
);
2135 struct vtn_type
*type
;
2136 nir_ssa_def
*offset
, *index
;
2137 offset
= vtn_access_chain_to_offset(b
, chain
, &index
, &type
, NULL
, false);
2139 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(opcode
);
2141 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2144 case SpvOpAtomicLoad
:
2145 atomic
->num_components
= glsl_get_vector_elements(type
->type
);
2146 atomic
->src
[0] = nir_src_for_ssa(index
);
2147 atomic
->src
[1] = nir_src_for_ssa(offset
);
2150 case SpvOpAtomicStore
:
2151 atomic
->num_components
= glsl_get_vector_elements(type
->type
);
2152 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2153 atomic
->src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2154 atomic
->src
[1] = nir_src_for_ssa(index
);
2155 atomic
->src
[2] = nir_src_for_ssa(offset
);
2158 case SpvOpAtomicExchange
:
2159 case SpvOpAtomicCompareExchange
:
2160 case SpvOpAtomicCompareExchangeWeak
:
2161 case SpvOpAtomicIIncrement
:
2162 case SpvOpAtomicIDecrement
:
2163 case SpvOpAtomicIAdd
:
2164 case SpvOpAtomicISub
:
2165 case SpvOpAtomicSMin
:
2166 case SpvOpAtomicUMin
:
2167 case SpvOpAtomicSMax
:
2168 case SpvOpAtomicUMax
:
2169 case SpvOpAtomicAnd
:
2171 case SpvOpAtomicXor
:
2172 atomic
->src
[0] = nir_src_for_ssa(index
);
2173 atomic
->src
[1] = nir_src_for_ssa(offset
);
2174 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[2]);
2178 unreachable("Invalid SPIR-V atomic");
2182 if (opcode
!= SpvOpAtomicStore
) {
2183 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2185 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
2186 glsl_get_vector_elements(type
->type
),
2187 glsl_get_bit_size(type
->type
), NULL
);
2189 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2190 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
2191 val
->ssa
->def
= &atomic
->dest
.ssa
;
2192 val
->ssa
->type
= type
->type
;
2195 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
2198 static nir_alu_instr
*
2199 create_vec(nir_shader
*shader
, unsigned num_components
, unsigned bit_size
)
2202 switch (num_components
) {
2203 case 1: op
= nir_op_fmov
; break;
2204 case 2: op
= nir_op_vec2
; break;
2205 case 3: op
= nir_op_vec3
; break;
2206 case 4: op
= nir_op_vec4
; break;
2207 default: unreachable("bad vector size");
2210 nir_alu_instr
*vec
= nir_alu_instr_create(shader
, op
);
2211 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
2213 vec
->dest
.write_mask
= (1 << num_components
) - 1;
2218 struct vtn_ssa_value
*
2219 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
2221 if (src
->transposed
)
2222 return src
->transposed
;
2224 struct vtn_ssa_value
*dest
=
2225 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
2227 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
2228 nir_alu_instr
*vec
= create_vec(b
->shader
,
2229 glsl_get_matrix_columns(src
->type
),
2230 glsl_get_bit_size(src
->type
));
2231 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2232 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
2233 vec
->src
[0].swizzle
[0] = i
;
2235 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
2236 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
2237 vec
->src
[j
].swizzle
[0] = i
;
2240 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2241 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
2244 dest
->transposed
= src
;
2250 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
2252 unsigned swiz
[4] = { index
};
2253 return nir_swizzle(&b
->nb
, src
, swiz
, 1, true);
2257 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
2260 nir_alu_instr
*vec
= create_vec(b
->shader
, src
->num_components
,
2263 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
2265 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
2267 vec
->src
[i
].src
= nir_src_for_ssa(src
);
2268 vec
->src
[i
].swizzle
[0] = i
;
2272 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2274 return &vec
->dest
.dest
.ssa
;
2278 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2281 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
2282 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2283 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2284 vtn_vector_extract(b
, src
, i
), dest
);
2290 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2291 nir_ssa_def
*insert
, nir_ssa_def
*index
)
2293 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
2294 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2295 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2296 vtn_vector_insert(b
, src
, insert
, i
), dest
);
2301 static nir_ssa_def
*
2302 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
2303 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
2304 const uint32_t *indices
)
2306 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
, src0
->bit_size
);
2308 for (unsigned i
= 0; i
< num_components
; i
++) {
2309 uint32_t index
= indices
[i
];
2310 if (index
== 0xffffffff) {
2312 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
2313 } else if (index
< src0
->num_components
) {
2314 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
2315 vec
->src
[i
].swizzle
[0] = index
;
2317 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
2318 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
2322 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2324 return &vec
->dest
.dest
.ssa
;
2328 * Concatentates a number of vectors/scalars together to produce a vector
2330 static nir_ssa_def
*
2331 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
2332 unsigned num_srcs
, nir_ssa_def
**srcs
)
2334 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
,
2337 unsigned dest_idx
= 0;
2338 for (unsigned i
= 0; i
< num_srcs
; i
++) {
2339 nir_ssa_def
*src
= srcs
[i
];
2340 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
2341 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
2342 vec
->src
[dest_idx
].swizzle
[0] = j
;
2347 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2349 return &vec
->dest
.dest
.ssa
;
2352 static struct vtn_ssa_value
*
2353 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
2355 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
2356 dest
->type
= src
->type
;
2358 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2359 dest
->def
= src
->def
;
2361 unsigned elems
= glsl_get_length(src
->type
);
2363 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
2364 for (unsigned i
= 0; i
< elems
; i
++)
2365 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
2371 static struct vtn_ssa_value
*
2372 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2373 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
2374 unsigned num_indices
)
2376 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
2378 struct vtn_ssa_value
*cur
= dest
;
2380 for (i
= 0; i
< num_indices
- 1; i
++) {
2381 cur
= cur
->elems
[indices
[i
]];
2384 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2385 /* According to the SPIR-V spec, OpCompositeInsert may work down to
2386 * the component granularity. In that case, the last index will be
2387 * the index to insert the scalar into the vector.
2390 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
2392 cur
->elems
[indices
[i
]] = insert
;
2398 static struct vtn_ssa_value
*
2399 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2400 const uint32_t *indices
, unsigned num_indices
)
2402 struct vtn_ssa_value
*cur
= src
;
2403 for (unsigned i
= 0; i
< num_indices
; i
++) {
2404 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2405 assert(i
== num_indices
- 1);
2406 /* According to the SPIR-V spec, OpCompositeExtract may work down to
2407 * the component granularity. The last index will be the index of the
2408 * vector to extract.
2411 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
2412 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
2413 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
2416 cur
= cur
->elems
[indices
[i
]];
2424 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
2425 const uint32_t *w
, unsigned count
)
2427 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2428 const struct glsl_type
*type
=
2429 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
2430 val
->ssa
= vtn_create_ssa_value(b
, type
);
2433 case SpvOpVectorExtractDynamic
:
2434 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2435 vtn_ssa_value(b
, w
[4])->def
);
2438 case SpvOpVectorInsertDynamic
:
2439 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2440 vtn_ssa_value(b
, w
[4])->def
,
2441 vtn_ssa_value(b
, w
[5])->def
);
2444 case SpvOpVectorShuffle
:
2445 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
2446 vtn_ssa_value(b
, w
[3])->def
,
2447 vtn_ssa_value(b
, w
[4])->def
,
2451 case SpvOpCompositeConstruct
: {
2452 unsigned elems
= count
- 3;
2453 if (glsl_type_is_vector_or_scalar(type
)) {
2454 nir_ssa_def
*srcs
[4];
2455 for (unsigned i
= 0; i
< elems
; i
++)
2456 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
2458 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
2461 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2462 for (unsigned i
= 0; i
< elems
; i
++)
2463 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
2467 case SpvOpCompositeExtract
:
2468 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
2472 case SpvOpCompositeInsert
:
2473 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
2474 vtn_ssa_value(b
, w
[3]),
2478 case SpvOpCopyObject
:
2479 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
2483 unreachable("unknown composite operation");
2488 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
2489 const uint32_t *w
, unsigned count
)
2491 nir_intrinsic_op intrinsic_op
;
2493 case SpvOpEmitVertex
:
2494 case SpvOpEmitStreamVertex
:
2495 intrinsic_op
= nir_intrinsic_emit_vertex
;
2497 case SpvOpEndPrimitive
:
2498 case SpvOpEndStreamPrimitive
:
2499 intrinsic_op
= nir_intrinsic_end_primitive
;
2501 case SpvOpMemoryBarrier
:
2502 intrinsic_op
= nir_intrinsic_memory_barrier
;
2504 case SpvOpControlBarrier
:
2505 intrinsic_op
= nir_intrinsic_barrier
;
2508 unreachable("unknown barrier instruction");
2511 nir_intrinsic_instr
*intrin
=
2512 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
2514 if (opcode
== SpvOpEmitStreamVertex
|| opcode
== SpvOpEndStreamPrimitive
)
2515 nir_intrinsic_set_stream_id(intrin
, w
[1]);
2517 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2521 gl_primitive_from_spv_execution_mode(SpvExecutionMode mode
)
2524 case SpvExecutionModeInputPoints
:
2525 case SpvExecutionModeOutputPoints
:
2526 return 0; /* GL_POINTS */
2527 case SpvExecutionModeInputLines
:
2528 return 1; /* GL_LINES */
2529 case SpvExecutionModeInputLinesAdjacency
:
2530 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
2531 case SpvExecutionModeTriangles
:
2532 return 4; /* GL_TRIANGLES */
2533 case SpvExecutionModeInputTrianglesAdjacency
:
2534 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
2535 case SpvExecutionModeQuads
:
2536 return 7; /* GL_QUADS */
2537 case SpvExecutionModeIsolines
:
2538 return 0x8E7A; /* GL_ISOLINES */
2539 case SpvExecutionModeOutputLineStrip
:
2540 return 3; /* GL_LINE_STRIP */
2541 case SpvExecutionModeOutputTriangleStrip
:
2542 return 5; /* GL_TRIANGLE_STRIP */
2544 assert(!"Invalid primitive type");
2550 vertices_in_from_spv_execution_mode(SpvExecutionMode mode
)
2553 case SpvExecutionModeInputPoints
:
2555 case SpvExecutionModeInputLines
:
2557 case SpvExecutionModeInputLinesAdjacency
:
2559 case SpvExecutionModeTriangles
:
2561 case SpvExecutionModeInputTrianglesAdjacency
:
2564 assert(!"Invalid GS input mode");
2569 static gl_shader_stage
2570 stage_for_execution_model(SpvExecutionModel model
)
2573 case SpvExecutionModelVertex
:
2574 return MESA_SHADER_VERTEX
;
2575 case SpvExecutionModelTessellationControl
:
2576 return MESA_SHADER_TESS_CTRL
;
2577 case SpvExecutionModelTessellationEvaluation
:
2578 return MESA_SHADER_TESS_EVAL
;
2579 case SpvExecutionModelGeometry
:
2580 return MESA_SHADER_GEOMETRY
;
2581 case SpvExecutionModelFragment
:
2582 return MESA_SHADER_FRAGMENT
;
2583 case SpvExecutionModelGLCompute
:
2584 return MESA_SHADER_COMPUTE
;
2586 unreachable("Unsupported execution model");
2590 #define spv_check_supported(name, cap) do { \
2591 if (!(b->ext && b->ext->name)) \
2592 vtn_warn("Unsupported SPIR-V capability: %s", \
2593 spirv_capability_to_string(cap)); \
2597 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2598 const uint32_t *w
, unsigned count
)
2602 case SpvOpSourceExtension
:
2603 case SpvOpSourceContinued
:
2604 case SpvOpExtension
:
2605 /* Unhandled, but these are for debug so that's ok. */
2608 case SpvOpCapability
: {
2609 SpvCapability cap
= w
[1];
2611 case SpvCapabilityMatrix
:
2612 case SpvCapabilityShader
:
2613 case SpvCapabilityGeometry
:
2614 case SpvCapabilityGeometryPointSize
:
2615 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
2616 case SpvCapabilitySampledImageArrayDynamicIndexing
:
2617 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
2618 case SpvCapabilityStorageImageArrayDynamicIndexing
:
2619 case SpvCapabilityImageRect
:
2620 case SpvCapabilitySampledRect
:
2621 case SpvCapabilitySampled1D
:
2622 case SpvCapabilityImage1D
:
2623 case SpvCapabilitySampledCubeArray
:
2624 case SpvCapabilitySampledBuffer
:
2625 case SpvCapabilityImageBuffer
:
2626 case SpvCapabilityImageQuery
:
2627 case SpvCapabilityDerivativeControl
:
2628 case SpvCapabilityInterpolationFunction
:
2629 case SpvCapabilityMultiViewport
:
2630 case SpvCapabilitySampleRateShading
:
2631 case SpvCapabilityClipDistance
:
2632 case SpvCapabilityCullDistance
:
2633 case SpvCapabilityInputAttachment
:
2634 case SpvCapabilityImageGatherExtended
:
2635 case SpvCapabilityStorageImageExtendedFormats
:
2638 case SpvCapabilityGeometryStreams
:
2639 case SpvCapabilityLinkage
:
2640 case SpvCapabilityVector16
:
2641 case SpvCapabilityFloat16Buffer
:
2642 case SpvCapabilityFloat16
:
2643 case SpvCapabilityInt64
:
2644 case SpvCapabilityInt64Atomics
:
2645 case SpvCapabilityAtomicStorage
:
2646 case SpvCapabilityInt16
:
2647 case SpvCapabilityStorageImageMultisample
:
2648 case SpvCapabilityImageCubeArray
:
2649 case SpvCapabilityInt8
:
2650 case SpvCapabilitySparseResidency
:
2651 case SpvCapabilityMinLod
:
2652 case SpvCapabilityTransformFeedback
:
2653 case SpvCapabilityStorageImageReadWithoutFormat
:
2654 case SpvCapabilityStorageImageWriteWithoutFormat
:
2655 vtn_warn("Unsupported SPIR-V capability: %s",
2656 spirv_capability_to_string(cap
));
2659 case SpvCapabilityFloat64
:
2660 spv_check_supported(float64
, cap
);
2663 case SpvCapabilityAddresses
:
2664 case SpvCapabilityKernel
:
2665 case SpvCapabilityImageBasic
:
2666 case SpvCapabilityImageReadWrite
:
2667 case SpvCapabilityImageMipmap
:
2668 case SpvCapabilityPipes
:
2669 case SpvCapabilityGroups
:
2670 case SpvCapabilityDeviceEnqueue
:
2671 case SpvCapabilityLiteralSampler
:
2672 case SpvCapabilityGenericPointer
:
2673 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
2674 spirv_capability_to_string(cap
));
2677 case SpvCapabilityImageMSArray
:
2678 spv_check_supported(image_ms_array
, cap
);
2681 case SpvCapabilityTessellation
:
2682 case SpvCapabilityTessellationPointSize
:
2683 spv_check_supported(tessellation
, cap
);
2686 case SpvCapabilityDrawParameters
:
2687 spv_check_supported(draw_parameters
, cap
);
2691 unreachable("Unhandled capability");
2696 case SpvOpExtInstImport
:
2697 vtn_handle_extension(b
, opcode
, w
, count
);
2700 case SpvOpMemoryModel
:
2701 assert(w
[1] == SpvAddressingModelLogical
);
2702 assert(w
[2] == SpvMemoryModelGLSL450
);
2705 case SpvOpEntryPoint
: {
2706 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
2707 /* Let this be a name label regardless */
2708 unsigned name_words
;
2709 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
2711 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
2712 stage_for_execution_model(w
[1]) != b
->entry_point_stage
)
2715 assert(b
->entry_point
== NULL
);
2716 b
->entry_point
= entry_point
;
2721 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
2722 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2726 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2729 case SpvOpMemberName
:
2733 case SpvOpExecutionMode
:
2734 case SpvOpDecorationGroup
:
2736 case SpvOpMemberDecorate
:
2737 case SpvOpGroupDecorate
:
2738 case SpvOpGroupMemberDecorate
:
2739 vtn_handle_decoration(b
, opcode
, w
, count
);
2743 return false; /* End of preamble */
2750 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
2751 const struct vtn_decoration
*mode
, void *data
)
2753 assert(b
->entry_point
== entry_point
);
2755 switch(mode
->exec_mode
) {
2756 case SpvExecutionModeOriginUpperLeft
:
2757 case SpvExecutionModeOriginLowerLeft
:
2758 b
->origin_upper_left
=
2759 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
2762 case SpvExecutionModeEarlyFragmentTests
:
2763 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2764 b
->shader
->info
->fs
.early_fragment_tests
= true;
2767 case SpvExecutionModeInvocations
:
2768 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2769 b
->shader
->info
->gs
.invocations
= MAX2(1, mode
->literals
[0]);
2772 case SpvExecutionModeDepthReplacing
:
2773 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2774 b
->shader
->info
->fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
2776 case SpvExecutionModeDepthGreater
:
2777 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2778 b
->shader
->info
->fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
2780 case SpvExecutionModeDepthLess
:
2781 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2782 b
->shader
->info
->fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
2784 case SpvExecutionModeDepthUnchanged
:
2785 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2786 b
->shader
->info
->fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
2789 case SpvExecutionModeLocalSize
:
2790 assert(b
->shader
->stage
== MESA_SHADER_COMPUTE
);
2791 b
->shader
->info
->cs
.local_size
[0] = mode
->literals
[0];
2792 b
->shader
->info
->cs
.local_size
[1] = mode
->literals
[1];
2793 b
->shader
->info
->cs
.local_size
[2] = mode
->literals
[2];
2795 case SpvExecutionModeLocalSizeHint
:
2796 break; /* Nothing to do with this */
2798 case SpvExecutionModeOutputVertices
:
2799 if (b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2800 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
) {
2801 b
->shader
->info
->tess
.tcs_vertices_out
= mode
->literals
[0];
2803 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2804 b
->shader
->info
->gs
.vertices_out
= mode
->literals
[0];
2808 case SpvExecutionModeInputPoints
:
2809 case SpvExecutionModeInputLines
:
2810 case SpvExecutionModeInputLinesAdjacency
:
2811 case SpvExecutionModeTriangles
:
2812 case SpvExecutionModeInputTrianglesAdjacency
:
2813 case SpvExecutionModeQuads
:
2814 case SpvExecutionModeIsolines
:
2815 if (b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2816 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
) {
2817 b
->shader
->info
->tess
.primitive_mode
=
2818 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
2820 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2821 b
->shader
->info
->gs
.vertices_in
=
2822 vertices_in_from_spv_execution_mode(mode
->exec_mode
);
2826 case SpvExecutionModeOutputPoints
:
2827 case SpvExecutionModeOutputLineStrip
:
2828 case SpvExecutionModeOutputTriangleStrip
:
2829 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2830 b
->shader
->info
->gs
.output_primitive
=
2831 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
2834 case SpvExecutionModeSpacingEqual
:
2835 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2836 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2837 b
->shader
->info
->tess
.spacing
= TESS_SPACING_EQUAL
;
2839 case SpvExecutionModeSpacingFractionalEven
:
2840 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2841 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2842 b
->shader
->info
->tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
2844 case SpvExecutionModeSpacingFractionalOdd
:
2845 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2846 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2847 b
->shader
->info
->tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
2849 case SpvExecutionModeVertexOrderCw
:
2850 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2851 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2852 /* Vulkan's notion of CCW seems to match the hardware backends,
2853 * but be the opposite of OpenGL. Currently NIR follows GL semantics,
2854 * so we set it backwards here.
2856 b
->shader
->info
->tess
.ccw
= true;
2858 case SpvExecutionModeVertexOrderCcw
:
2859 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2860 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2861 /* Backwards; see above */
2862 b
->shader
->info
->tess
.ccw
= false;
2864 case SpvExecutionModePointMode
:
2865 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2866 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2867 b
->shader
->info
->tess
.point_mode
= true;
2870 case SpvExecutionModePixelCenterInteger
:
2871 b
->pixel_center_integer
= true;
2874 case SpvExecutionModeXfb
:
2875 assert(!"Unhandled execution mode");
2878 case SpvExecutionModeVecTypeHint
:
2879 case SpvExecutionModeContractionOff
:
2883 unreachable("Unhandled execution mode");
2888 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2889 const uint32_t *w
, unsigned count
)
2893 case SpvOpSourceContinued
:
2894 case SpvOpSourceExtension
:
2895 case SpvOpExtension
:
2896 case SpvOpCapability
:
2897 case SpvOpExtInstImport
:
2898 case SpvOpMemoryModel
:
2899 case SpvOpEntryPoint
:
2900 case SpvOpExecutionMode
:
2903 case SpvOpMemberName
:
2904 case SpvOpDecorationGroup
:
2906 case SpvOpMemberDecorate
:
2907 case SpvOpGroupDecorate
:
2908 case SpvOpGroupMemberDecorate
:
2909 assert(!"Invalid opcode types and variables section");
2915 case SpvOpTypeFloat
:
2916 case SpvOpTypeVector
:
2917 case SpvOpTypeMatrix
:
2918 case SpvOpTypeImage
:
2919 case SpvOpTypeSampler
:
2920 case SpvOpTypeSampledImage
:
2921 case SpvOpTypeArray
:
2922 case SpvOpTypeRuntimeArray
:
2923 case SpvOpTypeStruct
:
2924 case SpvOpTypeOpaque
:
2925 case SpvOpTypePointer
:
2926 case SpvOpTypeFunction
:
2927 case SpvOpTypeEvent
:
2928 case SpvOpTypeDeviceEvent
:
2929 case SpvOpTypeReserveId
:
2930 case SpvOpTypeQueue
:
2932 vtn_handle_type(b
, opcode
, w
, count
);
2935 case SpvOpConstantTrue
:
2936 case SpvOpConstantFalse
:
2938 case SpvOpConstantComposite
:
2939 case SpvOpConstantSampler
:
2940 case SpvOpConstantNull
:
2941 case SpvOpSpecConstantTrue
:
2942 case SpvOpSpecConstantFalse
:
2943 case SpvOpSpecConstant
:
2944 case SpvOpSpecConstantComposite
:
2945 case SpvOpSpecConstantOp
:
2946 vtn_handle_constant(b
, opcode
, w
, count
);
2951 vtn_handle_variables(b
, opcode
, w
, count
);
2955 return false; /* End of preamble */
2962 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2963 const uint32_t *w
, unsigned count
)
2969 case SpvOpLoopMerge
:
2970 case SpvOpSelectionMerge
:
2971 /* This is handled by cfg pre-pass and walk_blocks */
2975 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
2976 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2981 vtn_handle_extension(b
, opcode
, w
, count
);
2987 case SpvOpCopyMemory
:
2988 case SpvOpCopyMemorySized
:
2989 case SpvOpAccessChain
:
2990 case SpvOpInBoundsAccessChain
:
2991 case SpvOpArrayLength
:
2992 vtn_handle_variables(b
, opcode
, w
, count
);
2995 case SpvOpFunctionCall
:
2996 vtn_handle_function_call(b
, opcode
, w
, count
);
2999 case SpvOpSampledImage
:
3001 case SpvOpImageSampleImplicitLod
:
3002 case SpvOpImageSampleExplicitLod
:
3003 case SpvOpImageSampleDrefImplicitLod
:
3004 case SpvOpImageSampleDrefExplicitLod
:
3005 case SpvOpImageSampleProjImplicitLod
:
3006 case SpvOpImageSampleProjExplicitLod
:
3007 case SpvOpImageSampleProjDrefImplicitLod
:
3008 case SpvOpImageSampleProjDrefExplicitLod
:
3009 case SpvOpImageFetch
:
3010 case SpvOpImageGather
:
3011 case SpvOpImageDrefGather
:
3012 case SpvOpImageQuerySizeLod
:
3013 case SpvOpImageQueryLod
:
3014 case SpvOpImageQueryLevels
:
3015 case SpvOpImageQuerySamples
:
3016 vtn_handle_texture(b
, opcode
, w
, count
);
3019 case SpvOpImageRead
:
3020 case SpvOpImageWrite
:
3021 case SpvOpImageTexelPointer
:
3022 vtn_handle_image(b
, opcode
, w
, count
);
3025 case SpvOpImageQuerySize
: {
3026 struct vtn_access_chain
*image
=
3027 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
3028 if (glsl_type_is_image(image
->var
->var
->interface_type
)) {
3029 vtn_handle_image(b
, opcode
, w
, count
);
3031 vtn_handle_texture(b
, opcode
, w
, count
);
3036 case SpvOpAtomicLoad
:
3037 case SpvOpAtomicExchange
:
3038 case SpvOpAtomicCompareExchange
:
3039 case SpvOpAtomicCompareExchangeWeak
:
3040 case SpvOpAtomicIIncrement
:
3041 case SpvOpAtomicIDecrement
:
3042 case SpvOpAtomicIAdd
:
3043 case SpvOpAtomicISub
:
3044 case SpvOpAtomicSMin
:
3045 case SpvOpAtomicUMin
:
3046 case SpvOpAtomicSMax
:
3047 case SpvOpAtomicUMax
:
3048 case SpvOpAtomicAnd
:
3050 case SpvOpAtomicXor
: {
3051 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
3052 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
3053 vtn_handle_image(b
, opcode
, w
, count
);
3055 assert(pointer
->value_type
== vtn_value_type_access_chain
);
3056 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
3061 case SpvOpAtomicStore
: {
3062 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
3063 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
3064 vtn_handle_image(b
, opcode
, w
, count
);
3066 assert(pointer
->value_type
== vtn_value_type_access_chain
);
3067 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
3077 case SpvOpConvertFToU
:
3078 case SpvOpConvertFToS
:
3079 case SpvOpConvertSToF
:
3080 case SpvOpConvertUToF
:
3084 case SpvOpQuantizeToF16
:
3085 case SpvOpConvertPtrToU
:
3086 case SpvOpConvertUToPtr
:
3087 case SpvOpPtrCastToGeneric
:
3088 case SpvOpGenericCastToPtr
:
3094 case SpvOpSignBitSet
:
3095 case SpvOpLessOrGreater
:
3097 case SpvOpUnordered
:
3112 case SpvOpVectorTimesScalar
:
3114 case SpvOpIAddCarry
:
3115 case SpvOpISubBorrow
:
3116 case SpvOpUMulExtended
:
3117 case SpvOpSMulExtended
:
3118 case SpvOpShiftRightLogical
:
3119 case SpvOpShiftRightArithmetic
:
3120 case SpvOpShiftLeftLogical
:
3121 case SpvOpLogicalEqual
:
3122 case SpvOpLogicalNotEqual
:
3123 case SpvOpLogicalOr
:
3124 case SpvOpLogicalAnd
:
3125 case SpvOpLogicalNot
:
3126 case SpvOpBitwiseOr
:
3127 case SpvOpBitwiseXor
:
3128 case SpvOpBitwiseAnd
:
3131 case SpvOpFOrdEqual
:
3132 case SpvOpFUnordEqual
:
3133 case SpvOpINotEqual
:
3134 case SpvOpFOrdNotEqual
:
3135 case SpvOpFUnordNotEqual
:
3136 case SpvOpULessThan
:
3137 case SpvOpSLessThan
:
3138 case SpvOpFOrdLessThan
:
3139 case SpvOpFUnordLessThan
:
3140 case SpvOpUGreaterThan
:
3141 case SpvOpSGreaterThan
:
3142 case SpvOpFOrdGreaterThan
:
3143 case SpvOpFUnordGreaterThan
:
3144 case SpvOpULessThanEqual
:
3145 case SpvOpSLessThanEqual
:
3146 case SpvOpFOrdLessThanEqual
:
3147 case SpvOpFUnordLessThanEqual
:
3148 case SpvOpUGreaterThanEqual
:
3149 case SpvOpSGreaterThanEqual
:
3150 case SpvOpFOrdGreaterThanEqual
:
3151 case SpvOpFUnordGreaterThanEqual
:
3157 case SpvOpFwidthFine
:
3158 case SpvOpDPdxCoarse
:
3159 case SpvOpDPdyCoarse
:
3160 case SpvOpFwidthCoarse
:
3161 case SpvOpBitFieldInsert
:
3162 case SpvOpBitFieldSExtract
:
3163 case SpvOpBitFieldUExtract
:
3164 case SpvOpBitReverse
:
3166 case SpvOpTranspose
:
3167 case SpvOpOuterProduct
:
3168 case SpvOpMatrixTimesScalar
:
3169 case SpvOpVectorTimesMatrix
:
3170 case SpvOpMatrixTimesVector
:
3171 case SpvOpMatrixTimesMatrix
:
3172 vtn_handle_alu(b
, opcode
, w
, count
);
3175 case SpvOpVectorExtractDynamic
:
3176 case SpvOpVectorInsertDynamic
:
3177 case SpvOpVectorShuffle
:
3178 case SpvOpCompositeConstruct
:
3179 case SpvOpCompositeExtract
:
3180 case SpvOpCompositeInsert
:
3181 case SpvOpCopyObject
:
3182 vtn_handle_composite(b
, opcode
, w
, count
);
3185 case SpvOpEmitVertex
:
3186 case SpvOpEndPrimitive
:
3187 case SpvOpEmitStreamVertex
:
3188 case SpvOpEndStreamPrimitive
:
3189 case SpvOpControlBarrier
:
3190 case SpvOpMemoryBarrier
:
3191 vtn_handle_barrier(b
, opcode
, w
, count
);
3195 unreachable("Unhandled opcode");
3202 spirv_to_nir(const uint32_t *words
, size_t word_count
,
3203 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
3204 gl_shader_stage stage
, const char *entry_point_name
,
3205 const struct nir_spirv_supported_extensions
*ext
,
3206 const nir_shader_compiler_options
*options
)
3208 const uint32_t *word_end
= words
+ word_count
;
3210 /* Handle the SPIR-V header (first 4 dwords) */
3211 assert(word_count
> 5);
3213 assert(words
[0] == SpvMagicNumber
);
3214 assert(words
[1] >= 0x10000);
3215 /* words[2] == generator magic */
3216 unsigned value_id_bound
= words
[3];
3217 assert(words
[4] == 0);
3221 /* Initialize the stn_builder object */
3222 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
3223 b
->value_id_bound
= value_id_bound
;
3224 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
3225 exec_list_make_empty(&b
->functions
);
3226 b
->entry_point_stage
= stage
;
3227 b
->entry_point_name
= entry_point_name
;
3230 /* Handle all the preamble instructions */
3231 words
= vtn_foreach_instruction(b
, words
, word_end
,
3232 vtn_handle_preamble_instruction
);
3234 if (b
->entry_point
== NULL
) {
3235 assert(!"Entry point not found");
3240 b
->shader
= nir_shader_create(NULL
, stage
, options
, NULL
);
3242 /* Set shader info defaults */
3243 b
->shader
->info
->gs
.invocations
= 1;
3245 /* Parse execution modes */
3246 vtn_foreach_execution_mode(b
, b
->entry_point
,
3247 vtn_handle_execution_mode
, NULL
);
3249 b
->specializations
= spec
;
3250 b
->num_specializations
= num_spec
;
3252 /* Handle all variable, type, and constant instructions */
3253 words
= vtn_foreach_instruction(b
, words
, word_end
,
3254 vtn_handle_variable_or_type_instruction
);
3256 vtn_build_cfg(b
, words
, word_end
);
3258 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
3259 b
->impl
= func
->impl
;
3260 b
->const_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
3261 _mesa_key_pointer_equal
);
3263 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
3266 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
3267 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
3268 assert(entry_point
);