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
));
568 type_decoration_cb(struct vtn_builder
*b
,
569 struct vtn_value
*val
, int member
,
570 const struct vtn_decoration
*dec
, void *ctx
)
572 struct vtn_type
*type
= val
->type
;
577 switch (dec
->decoration
) {
578 case SpvDecorationArrayStride
:
579 type
->stride
= dec
->literals
[0];
581 case SpvDecorationBlock
:
584 case SpvDecorationBufferBlock
:
585 type
->buffer_block
= true;
587 case SpvDecorationGLSLShared
:
588 case SpvDecorationGLSLPacked
:
589 /* Ignore these, since we get explicit offsets anyways */
592 case SpvDecorationRowMajor
:
593 case SpvDecorationColMajor
:
594 case SpvDecorationMatrixStride
:
595 case SpvDecorationBuiltIn
:
596 case SpvDecorationNoPerspective
:
597 case SpvDecorationFlat
:
598 case SpvDecorationPatch
:
599 case SpvDecorationCentroid
:
600 case SpvDecorationSample
:
601 case SpvDecorationVolatile
:
602 case SpvDecorationCoherent
:
603 case SpvDecorationNonWritable
:
604 case SpvDecorationNonReadable
:
605 case SpvDecorationUniform
:
606 case SpvDecorationStream
:
607 case SpvDecorationLocation
:
608 case SpvDecorationComponent
:
609 case SpvDecorationOffset
:
610 case SpvDecorationXfbBuffer
:
611 case SpvDecorationXfbStride
:
612 vtn_warn("Decoration only allowed for struct members: %s",
613 spirv_decoration_to_string(dec
->decoration
));
616 case SpvDecorationRelaxedPrecision
:
617 case SpvDecorationSpecId
:
618 case SpvDecorationInvariant
:
619 case SpvDecorationRestrict
:
620 case SpvDecorationAliased
:
621 case SpvDecorationConstant
:
622 case SpvDecorationIndex
:
623 case SpvDecorationBinding
:
624 case SpvDecorationDescriptorSet
:
625 case SpvDecorationLinkageAttributes
:
626 case SpvDecorationNoContraction
:
627 case SpvDecorationInputAttachmentIndex
:
628 vtn_warn("Decoration not allowed on types: %s",
629 spirv_decoration_to_string(dec
->decoration
));
632 case SpvDecorationCPacked
:
633 case SpvDecorationSaturatedConversion
:
634 case SpvDecorationFuncParamAttr
:
635 case SpvDecorationFPRoundingMode
:
636 case SpvDecorationFPFastMathMode
:
637 case SpvDecorationAlignment
:
638 vtn_warn("Decoration only allowed for CL-style kernels: %s",
639 spirv_decoration_to_string(dec
->decoration
));
645 translate_image_format(SpvImageFormat format
)
648 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
649 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
650 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
651 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
652 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
653 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
654 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
655 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
656 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
657 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
658 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
659 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
660 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
661 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
662 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
663 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
664 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
665 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
666 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
667 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
668 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
669 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
670 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
671 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
672 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
673 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
674 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
675 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
676 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
677 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
678 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
679 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
680 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
681 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
682 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
683 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
684 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
685 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
686 case SpvImageFormatR16ui
: return 0x823A; /* GL_RG16UI */
687 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
689 assert(!"Invalid image format");
695 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
696 const uint32_t *w
, unsigned count
)
698 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
700 val
->type
= rzalloc(b
, struct vtn_type
);
701 val
->type
->is_builtin
= false;
702 val
->type
->val
= val
;
706 val
->type
->type
= glsl_void_type();
709 val
->type
->type
= glsl_bool_type();
712 const bool signedness
= w
[3];
713 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
716 case SpvOpTypeFloat
: {
718 val
->type
->type
= bit_size
== 64 ? glsl_double_type() : glsl_float_type();
722 case SpvOpTypeVector
: {
723 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
724 unsigned elems
= w
[3];
726 assert(glsl_type_is_scalar(base
->type
));
727 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
729 /* Vectors implicitly have sizeof(base_type) stride. For now, this
730 * is always 4 bytes. This will have to change if we want to start
731 * supporting doubles or half-floats.
733 val
->type
->stride
= 4;
734 val
->type
->array_element
= base
;
738 case SpvOpTypeMatrix
: {
739 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
740 unsigned columns
= w
[3];
742 assert(glsl_type_is_vector(base
->type
));
743 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
744 glsl_get_vector_elements(base
->type
),
746 assert(!glsl_type_is_error(val
->type
->type
));
747 val
->type
->array_element
= base
;
748 val
->type
->row_major
= false;
749 val
->type
->stride
= 0;
753 case SpvOpTypeRuntimeArray
:
754 case SpvOpTypeArray
: {
755 struct vtn_type
*array_element
=
756 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
759 if (opcode
== SpvOpTypeRuntimeArray
) {
760 /* A length of 0 is used to denote unsized arrays */
764 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
767 val
->type
->type
= glsl_array_type(array_element
->type
, length
);
768 val
->type
->array_element
= array_element
;
769 val
->type
->stride
= 0;
773 case SpvOpTypeStruct
: {
774 unsigned num_fields
= count
- 2;
775 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
776 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
778 NIR_VLA(struct glsl_struct_field
, fields
, count
);
779 for (unsigned i
= 0; i
< num_fields
; i
++) {
780 val
->type
->members
[i
] =
781 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
782 fields
[i
] = (struct glsl_struct_field
) {
783 .type
= val
->type
->members
[i
]->type
,
784 .name
= ralloc_asprintf(b
, "field%d", i
),
789 struct member_decoration_ctx ctx
= {
790 .num_fields
= num_fields
,
795 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
797 const char *name
= val
->name
? val
->name
: "struct";
799 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
);
803 case SpvOpTypeFunction
: {
804 const struct glsl_type
*return_type
=
805 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
806 NIR_VLA(struct glsl_function_param
, params
, count
- 3);
807 for (unsigned i
= 0; i
< count
- 3; i
++) {
808 params
[i
].type
= vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
->type
;
812 params
[i
].out
= true;
814 val
->type
->type
= glsl_function_type(return_type
, params
, count
- 3);
818 case SpvOpTypePointer
:
819 /* FIXME: For now, we'll just do the really lame thing and return
820 * the same type. The validator should ensure that the proper number
821 * of dereferences happen
823 val
->type
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
826 case SpvOpTypeImage
: {
827 const struct glsl_type
*sampled_type
=
828 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
830 assert(glsl_type_is_vector_or_scalar(sampled_type
));
832 enum glsl_sampler_dim dim
;
833 switch ((SpvDim
)w
[3]) {
834 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
835 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
836 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
837 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
838 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
839 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
840 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
842 unreachable("Invalid SPIR-V Sampler dimension");
845 bool is_shadow
= w
[4];
846 bool is_array
= w
[5];
847 bool multisampled
= w
[6];
848 unsigned sampled
= w
[7];
849 SpvImageFormat format
= w
[8];
852 val
->type
->access_qualifier
= w
[9];
854 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
857 if (dim
== GLSL_SAMPLER_DIM_2D
)
858 dim
= GLSL_SAMPLER_DIM_MS
;
859 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
860 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
862 assert(!"Unsupported multisampled image type");
865 val
->type
->image_format
= translate_image_format(format
);
868 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
869 glsl_get_base_type(sampled_type
));
870 } else if (sampled
== 2) {
871 assert((dim
== GLSL_SAMPLER_DIM_SUBPASS
||
872 dim
== GLSL_SAMPLER_DIM_SUBPASS_MS
) || format
);
874 val
->type
->type
= glsl_image_type(dim
, is_array
,
875 glsl_get_base_type(sampled_type
));
877 assert(!"We need to know if the image will be sampled");
882 case SpvOpTypeSampledImage
:
883 val
->type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
886 case SpvOpTypeSampler
:
887 /* The actual sampler type here doesn't really matter. It gets
888 * thrown away the moment you combine it with an image. What really
889 * matters is that it's a sampler type as opposed to an integer type
890 * so the backend knows what to do.
892 val
->type
->type
= glsl_bare_sampler_type();
895 case SpvOpTypeOpaque
:
897 case SpvOpTypeDeviceEvent
:
898 case SpvOpTypeReserveId
:
902 unreachable("Unhandled opcode");
905 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
908 static nir_constant
*
909 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
911 nir_constant
*c
= rzalloc(b
, nir_constant
);
913 switch (glsl_get_base_type(type
)) {
917 case GLSL_TYPE_FLOAT
:
918 case GLSL_TYPE_DOUBLE
:
919 /* Nothing to do here. It's already initialized to zero */
922 case GLSL_TYPE_ARRAY
:
923 assert(glsl_get_length(type
) > 0);
924 c
->num_elements
= glsl_get_length(type
);
925 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
927 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
928 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
929 c
->elements
[i
] = c
->elements
[0];
932 case GLSL_TYPE_STRUCT
:
933 c
->num_elements
= glsl_get_length(type
);
934 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
936 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
937 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
942 unreachable("Invalid type for null constant");
949 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
950 int member
, const struct vtn_decoration
*dec
,
953 assert(member
== -1);
954 if (dec
->decoration
!= SpvDecorationSpecId
)
957 struct spec_constant_value
*const_value
= data
;
959 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
960 if (b
->specializations
[i
].id
== dec
->literals
[0]) {
961 if (const_value
->is_double
)
962 const_value
->data64
= b
->specializations
[i
].data64
;
964 const_value
->data32
= b
->specializations
[i
].data32
;
971 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
972 uint32_t const_value
)
974 struct spec_constant_value data
;
975 data
.is_double
= false;
976 data
.data32
= const_value
;
977 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
982 get_specialization64(struct vtn_builder
*b
, struct vtn_value
*val
,
983 uint64_t const_value
)
985 struct spec_constant_value data
;
986 data
.is_double
= true;
987 data
.data64
= const_value
;
988 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
993 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
994 struct vtn_value
*val
,
996 const struct vtn_decoration
*dec
,
999 assert(member
== -1);
1000 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1001 dec
->literals
[0] != SpvBuiltInWorkgroupSize
)
1004 assert(val
->const_type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1006 b
->shader
->info
->cs
.local_size
[0] = val
->constant
->values
[0].u32
[0];
1007 b
->shader
->info
->cs
.local_size
[1] = val
->constant
->values
[0].u32
[1];
1008 b
->shader
->info
->cs
.local_size
[2] = val
->constant
->values
[0].u32
[2];
1012 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1013 const uint32_t *w
, unsigned count
)
1015 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1016 val
->const_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
1017 val
->constant
= rzalloc(b
, nir_constant
);
1019 case SpvOpConstantTrue
:
1020 assert(val
->const_type
== glsl_bool_type());
1021 val
->constant
->values
[0].u32
[0] = NIR_TRUE
;
1023 case SpvOpConstantFalse
:
1024 assert(val
->const_type
== glsl_bool_type());
1025 val
->constant
->values
[0].u32
[0] = NIR_FALSE
;
1028 case SpvOpSpecConstantTrue
:
1029 case SpvOpSpecConstantFalse
: {
1030 assert(val
->const_type
== glsl_bool_type());
1032 get_specialization(b
, val
, (opcode
== SpvOpSpecConstantTrue
));
1033 val
->constant
->values
[0].u32
[0] = int_val
? NIR_TRUE
: NIR_FALSE
;
1037 case SpvOpConstant
: {
1038 assert(glsl_type_is_scalar(val
->const_type
));
1039 int bit_size
= glsl_get_bit_size(val
->const_type
);
1040 if (bit_size
== 64) {
1041 val
->constant
->values
->u32
[0] = w
[3];
1042 val
->constant
->values
->u32
[1] = w
[4];
1044 assert(bit_size
== 32);
1045 val
->constant
->values
->u32
[0] = w
[3];
1049 case SpvOpSpecConstant
: {
1050 assert(glsl_type_is_scalar(val
->const_type
));
1051 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1052 int bit_size
= glsl_get_bit_size(val
->const_type
);
1054 val
->constant
->values
[0].u64
[0] =
1055 get_specialization64(b
, val
, vtn_u64_literal(&w
[3]));
1057 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1060 case SpvOpSpecConstantComposite
:
1061 case SpvOpConstantComposite
: {
1062 unsigned elem_count
= count
- 3;
1063 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1064 for (unsigned i
= 0; i
< elem_count
; i
++)
1065 elems
[i
] = vtn_value(b
, w
[i
+ 3], vtn_value_type_constant
)->constant
;
1067 switch (glsl_get_base_type(val
->const_type
)) {
1068 case GLSL_TYPE_UINT
:
1070 case GLSL_TYPE_FLOAT
:
1071 case GLSL_TYPE_BOOL
:
1072 case GLSL_TYPE_DOUBLE
: {
1073 int bit_size
= glsl_get_bit_size(val
->const_type
);
1074 if (glsl_type_is_matrix(val
->const_type
)) {
1075 assert(glsl_get_matrix_columns(val
->const_type
) == elem_count
);
1076 for (unsigned i
= 0; i
< elem_count
; i
++)
1077 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1079 assert(glsl_type_is_vector(val
->const_type
));
1080 assert(glsl_get_vector_elements(val
->const_type
) == elem_count
);
1081 for (unsigned i
= 0; i
< elem_count
; i
++) {
1082 if (bit_size
== 64) {
1083 val
->constant
->values
[0].u64
[i
] = elems
[i
]->values
[0].u64
[0];
1085 assert(bit_size
== 32);
1086 val
->constant
->values
[0].u32
[i
] = elems
[i
]->values
[0].u32
[0];
1093 case GLSL_TYPE_STRUCT
:
1094 case GLSL_TYPE_ARRAY
:
1095 ralloc_steal(val
->constant
, elems
);
1096 val
->constant
->num_elements
= elem_count
;
1097 val
->constant
->elements
= elems
;
1101 unreachable("Unsupported type for constants");
1106 case SpvOpSpecConstantOp
: {
1107 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1109 case SpvOpVectorShuffle
: {
1110 struct vtn_value
*v0
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1111 struct vtn_value
*v1
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1112 unsigned len0
= glsl_get_vector_elements(v0
->const_type
);
1113 unsigned len1
= glsl_get_vector_elements(v1
->const_type
);
1115 assert(len0
+ len1
< 16);
1117 unsigned bit_size
= glsl_get_bit_size(val
->const_type
);
1118 assert(bit_size
== glsl_get_bit_size(v0
->const_type
) &&
1119 bit_size
== glsl_get_bit_size(v1
->const_type
));
1121 if (bit_size
== 64) {
1123 for (unsigned i
= 0; i
< len0
; i
++)
1124 u64
[i
] = v0
->constant
->values
[0].u64
[i
];
1125 for (unsigned i
= 0; i
< len1
; i
++)
1126 u64
[len0
+ i
] = v1
->constant
->values
[0].u64
[i
];
1128 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1129 uint32_t comp
= w
[i
+ 6];
1130 /* If component is not used, set the value to a known constant
1131 * to detect if it is wrongly used.
1133 if (comp
== (uint32_t)-1)
1134 val
->constant
->values
[0].u64
[j
] = 0xdeadbeefdeadbeef;
1136 val
->constant
->values
[0].u64
[j
] = u64
[comp
];
1140 for (unsigned i
= 0; i
< len0
; i
++)
1141 u32
[i
] = v0
->constant
->values
[0].u32
[i
];
1143 for (unsigned i
= 0; i
< len1
; i
++)
1144 u32
[len0
+ i
] = v1
->constant
->values
[0].u32
[i
];
1146 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1147 uint32_t comp
= w
[i
+ 6];
1148 /* If component is not used, set the value to a known constant
1149 * to detect if it is wrongly used.
1151 if (comp
== (uint32_t)-1)
1152 val
->constant
->values
[0].u32
[j
] = 0xdeadbeef;
1154 val
->constant
->values
[0].u32
[j
] = u32
[comp
];
1160 case SpvOpCompositeExtract
:
1161 case SpvOpCompositeInsert
: {
1162 struct vtn_value
*comp
;
1163 unsigned deref_start
;
1164 struct nir_constant
**c
;
1165 if (opcode
== SpvOpCompositeExtract
) {
1166 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1168 c
= &comp
->constant
;
1170 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1172 val
->constant
= nir_constant_clone(comp
->constant
,
1179 const struct glsl_type
*type
= comp
->const_type
;
1180 for (unsigned i
= deref_start
; i
< count
; i
++) {
1181 switch (glsl_get_base_type(type
)) {
1182 case GLSL_TYPE_UINT
:
1184 case GLSL_TYPE_FLOAT
:
1185 case GLSL_TYPE_DOUBLE
:
1186 case GLSL_TYPE_BOOL
:
1187 /* If we hit this granularity, we're picking off an element */
1188 if (glsl_type_is_matrix(type
)) {
1189 assert(col
== 0 && elem
== -1);
1192 type
= glsl_get_column_type(type
);
1194 assert(elem
<= 0 && glsl_type_is_vector(type
));
1196 type
= glsl_scalar_type(glsl_get_base_type(type
));
1200 case GLSL_TYPE_ARRAY
:
1201 c
= &(*c
)->elements
[w
[i
]];
1202 type
= glsl_get_array_element(type
);
1205 case GLSL_TYPE_STRUCT
:
1206 c
= &(*c
)->elements
[w
[i
]];
1207 type
= glsl_get_struct_field(type
, w
[i
]);
1211 unreachable("Invalid constant type");
1215 if (opcode
== SpvOpCompositeExtract
) {
1219 unsigned num_components
= glsl_get_vector_elements(type
);
1220 unsigned bit_size
= glsl_get_bit_size(type
);
1221 for (unsigned i
= 0; i
< num_components
; i
++)
1222 if (bit_size
== 64) {
1223 val
->constant
->values
[0].u64
[i
] = (*c
)->values
[col
].u64
[elem
+ i
];
1225 assert(bit_size
== 32);
1226 val
->constant
->values
[0].u32
[i
] = (*c
)->values
[col
].u32
[elem
+ i
];
1230 struct vtn_value
*insert
=
1231 vtn_value(b
, w
[4], vtn_value_type_constant
);
1232 assert(insert
->const_type
== type
);
1234 *c
= insert
->constant
;
1236 unsigned num_components
= glsl_get_vector_elements(type
);
1237 unsigned bit_size
= glsl_get_bit_size(type
);
1238 for (unsigned i
= 0; i
< num_components
; i
++)
1239 if (bit_size
== 64) {
1240 (*c
)->values
[col
].u64
[elem
+ i
] = insert
->constant
->values
[0].u64
[i
];
1242 assert(bit_size
== 32);
1243 (*c
)->values
[col
].u32
[elem
+ i
] = insert
->constant
->values
[0].u32
[i
];
1252 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->const_type
);
1253 nir_alu_type src_alu_type
= dst_alu_type
;
1254 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(opcode
, &swap
, src_alu_type
, dst_alu_type
);
1256 unsigned num_components
= glsl_get_vector_elements(val
->const_type
);
1258 glsl_get_bit_size(val
->const_type
);
1260 nir_const_value src
[4];
1262 for (unsigned i
= 0; i
< count
- 4; i
++) {
1264 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
)->constant
;
1266 unsigned j
= swap
? 1 - i
: i
;
1267 assert(bit_size
== 32);
1268 src
[j
] = c
->values
[0];
1271 val
->constant
->values
[0] =
1272 nir_eval_const_opcode(op
, num_components
, bit_size
, src
);
1279 case SpvOpConstantNull
:
1280 val
->constant
= vtn_null_constant(b
, val
->const_type
);
1283 case SpvOpConstantSampler
:
1284 assert(!"OpConstantSampler requires Kernel Capability");
1288 unreachable("Unhandled opcode");
1291 /* Now that we have the value, update the workgroup size if needed */
1292 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1296 vtn_handle_function_call(struct vtn_builder
*b
, SpvOp opcode
,
1297 const uint32_t *w
, unsigned count
)
1299 struct nir_function
*callee
=
1300 vtn_value(b
, w
[3], vtn_value_type_function
)->func
->impl
->function
;
1302 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, callee
);
1303 for (unsigned i
= 0; i
< call
->num_params
; i
++) {
1304 unsigned arg_id
= w
[4 + i
];
1305 struct vtn_value
*arg
= vtn_untyped_value(b
, arg_id
);
1306 if (arg
->value_type
== vtn_value_type_access_chain
) {
1307 nir_deref_var
*d
= vtn_access_chain_to_deref(b
, arg
->access_chain
);
1308 call
->params
[i
] = nir_deref_var_clone(d
, call
);
1310 struct vtn_ssa_value
*arg_ssa
= vtn_ssa_value(b
, arg_id
);
1312 /* Make a temporary to store the argument in */
1314 nir_local_variable_create(b
->impl
, arg_ssa
->type
, "arg_tmp");
1315 call
->params
[i
] = nir_deref_var_create(call
, tmp
);
1317 vtn_local_store(b
, arg_ssa
, call
->params
[i
]);
1321 nir_variable
*out_tmp
= NULL
;
1322 if (!glsl_type_is_void(callee
->return_type
)) {
1323 out_tmp
= nir_local_variable_create(b
->impl
, callee
->return_type
,
1325 call
->return_deref
= nir_deref_var_create(call
, out_tmp
);
1328 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
1330 if (glsl_type_is_void(callee
->return_type
)) {
1331 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1333 struct vtn_value
*retval
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1334 retval
->ssa
= vtn_local_load(b
, call
->return_deref
);
1338 struct vtn_ssa_value
*
1339 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1341 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1344 if (!glsl_type_is_vector_or_scalar(type
)) {
1345 unsigned elems
= glsl_get_length(type
);
1346 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1347 for (unsigned i
= 0; i
< elems
; i
++) {
1348 const struct glsl_type
*child_type
;
1350 switch (glsl_get_base_type(type
)) {
1352 case GLSL_TYPE_UINT
:
1353 case GLSL_TYPE_BOOL
:
1354 case GLSL_TYPE_FLOAT
:
1355 case GLSL_TYPE_DOUBLE
:
1356 child_type
= glsl_get_column_type(type
);
1358 case GLSL_TYPE_ARRAY
:
1359 child_type
= glsl_get_array_element(type
);
1361 case GLSL_TYPE_STRUCT
:
1362 child_type
= glsl_get_struct_field(type
, i
);
1365 unreachable("unkown base type");
1368 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
1376 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
1379 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
1380 src
.src_type
= type
;
1385 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
1386 const uint32_t *w
, unsigned count
)
1388 if (opcode
== SpvOpSampledImage
) {
1389 struct vtn_value
*val
=
1390 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1391 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1392 val
->sampled_image
->image
=
1393 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1394 val
->sampled_image
->sampler
=
1395 vtn_value(b
, w
[4], vtn_value_type_access_chain
)->access_chain
;
1397 } else if (opcode
== SpvOpImage
) {
1398 struct vtn_value
*val
=
1399 vtn_push_value(b
, w
[2], vtn_value_type_access_chain
);
1400 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
1401 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
1402 val
->access_chain
= src_val
->sampled_image
->image
;
1404 assert(src_val
->value_type
== vtn_value_type_access_chain
);
1405 val
->access_chain
= src_val
->access_chain
;
1410 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1411 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1413 struct vtn_sampled_image sampled
;
1414 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
1415 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
1416 sampled
= *sampled_val
->sampled_image
;
1418 assert(sampled_val
->value_type
== vtn_value_type_access_chain
);
1419 sampled
.image
= NULL
;
1420 sampled
.sampler
= sampled_val
->access_chain
;
1423 const struct glsl_type
*image_type
;
1424 if (sampled
.image
) {
1425 image_type
= sampled
.image
->var
->var
->interface_type
;
1427 image_type
= sampled
.sampler
->var
->var
->interface_type
;
1429 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
1430 const bool is_array
= glsl_sampler_type_is_array(image_type
);
1431 const bool is_shadow
= glsl_sampler_type_is_shadow(image_type
);
1433 /* Figure out the base texture operation */
1436 case SpvOpImageSampleImplicitLod
:
1437 case SpvOpImageSampleDrefImplicitLod
:
1438 case SpvOpImageSampleProjImplicitLod
:
1439 case SpvOpImageSampleProjDrefImplicitLod
:
1440 texop
= nir_texop_tex
;
1443 case SpvOpImageSampleExplicitLod
:
1444 case SpvOpImageSampleDrefExplicitLod
:
1445 case SpvOpImageSampleProjExplicitLod
:
1446 case SpvOpImageSampleProjDrefExplicitLod
:
1447 texop
= nir_texop_txl
;
1450 case SpvOpImageFetch
:
1451 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
1452 texop
= nir_texop_txf_ms
;
1454 texop
= nir_texop_txf
;
1458 case SpvOpImageGather
:
1459 case SpvOpImageDrefGather
:
1460 texop
= nir_texop_tg4
;
1463 case SpvOpImageQuerySizeLod
:
1464 case SpvOpImageQuerySize
:
1465 texop
= nir_texop_txs
;
1468 case SpvOpImageQueryLod
:
1469 texop
= nir_texop_lod
;
1472 case SpvOpImageQueryLevels
:
1473 texop
= nir_texop_query_levels
;
1476 case SpvOpImageQuerySamples
:
1477 texop
= nir_texop_texture_samples
;
1481 unreachable("Unhandled opcode");
1484 nir_tex_src srcs
[8]; /* 8 should be enough */
1485 nir_tex_src
*p
= srcs
;
1489 struct nir_ssa_def
*coord
;
1490 unsigned coord_components
;
1492 case SpvOpImageSampleImplicitLod
:
1493 case SpvOpImageSampleExplicitLod
:
1494 case SpvOpImageSampleDrefImplicitLod
:
1495 case SpvOpImageSampleDrefExplicitLod
:
1496 case SpvOpImageSampleProjImplicitLod
:
1497 case SpvOpImageSampleProjExplicitLod
:
1498 case SpvOpImageSampleProjDrefImplicitLod
:
1499 case SpvOpImageSampleProjDrefExplicitLod
:
1500 case SpvOpImageFetch
:
1501 case SpvOpImageGather
:
1502 case SpvOpImageDrefGather
:
1503 case SpvOpImageQueryLod
: {
1504 /* All these types have the coordinate as their first real argument */
1505 switch (sampler_dim
) {
1506 case GLSL_SAMPLER_DIM_1D
:
1507 case GLSL_SAMPLER_DIM_BUF
:
1508 coord_components
= 1;
1510 case GLSL_SAMPLER_DIM_2D
:
1511 case GLSL_SAMPLER_DIM_RECT
:
1512 case GLSL_SAMPLER_DIM_MS
:
1513 coord_components
= 2;
1515 case GLSL_SAMPLER_DIM_3D
:
1516 case GLSL_SAMPLER_DIM_CUBE
:
1517 coord_components
= 3;
1520 unreachable("Invalid sampler type");
1523 if (is_array
&& texop
!= nir_texop_lod
)
1526 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
1527 p
->src
= nir_src_for_ssa(coord
);
1528 p
->src_type
= nir_tex_src_coord
;
1535 coord_components
= 0;
1540 case SpvOpImageSampleProjImplicitLod
:
1541 case SpvOpImageSampleProjExplicitLod
:
1542 case SpvOpImageSampleProjDrefImplicitLod
:
1543 case SpvOpImageSampleProjDrefExplicitLod
:
1544 /* These have the projector as the last coordinate component */
1545 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
1546 p
->src_type
= nir_tex_src_projector
;
1554 unsigned gather_component
= 0;
1556 case SpvOpImageSampleDrefImplicitLod
:
1557 case SpvOpImageSampleDrefExplicitLod
:
1558 case SpvOpImageSampleProjDrefImplicitLod
:
1559 case SpvOpImageSampleProjDrefExplicitLod
:
1560 case SpvOpImageDrefGather
:
1561 /* These all have an explicit depth value as their next source */
1562 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
1565 case SpvOpImageGather
:
1566 /* This has a component as its next source */
1568 vtn_value(b
, w
[idx
++], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
1575 /* For OpImageQuerySizeLod, we always have an LOD */
1576 if (opcode
== SpvOpImageQuerySizeLod
)
1577 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1579 /* Now we need to handle some number of optional arguments */
1580 const struct vtn_ssa_value
*gather_offsets
= NULL
;
1582 uint32_t operands
= w
[idx
++];
1584 if (operands
& SpvImageOperandsBiasMask
) {
1585 assert(texop
== nir_texop_tex
);
1586 texop
= nir_texop_txb
;
1587 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
1590 if (operands
& SpvImageOperandsLodMask
) {
1591 assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
1592 texop
== nir_texop_txs
);
1593 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1596 if (operands
& SpvImageOperandsGradMask
) {
1597 assert(texop
== nir_texop_txl
);
1598 texop
= nir_texop_txd
;
1599 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
1600 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
1603 if (operands
& SpvImageOperandsOffsetMask
||
1604 operands
& SpvImageOperandsConstOffsetMask
)
1605 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
1607 if (operands
& SpvImageOperandsConstOffsetsMask
) {
1608 gather_offsets
= vtn_ssa_value(b
, w
[idx
++]);
1609 (*p
++) = (nir_tex_src
){};
1612 if (operands
& SpvImageOperandsSampleMask
) {
1613 assert(texop
== nir_texop_txf_ms
);
1614 texop
= nir_texop_txf_ms
;
1615 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
1618 /* We should have now consumed exactly all of the arguments */
1619 assert(idx
== count
);
1621 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
1624 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1626 instr
->coord_components
= coord_components
;
1627 instr
->sampler_dim
= sampler_dim
;
1628 instr
->is_array
= is_array
;
1629 instr
->is_shadow
= is_shadow
;
1630 instr
->is_new_style_shadow
=
1631 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
1632 instr
->component
= gather_component
;
1634 switch (glsl_get_sampler_result_type(image_type
)) {
1635 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
1636 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
1637 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
1638 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
1640 unreachable("Invalid base type for sampler result");
1643 nir_deref_var
*sampler
= vtn_access_chain_to_deref(b
, sampled
.sampler
);
1644 nir_deref_var
*texture
;
1645 if (sampled
.image
) {
1646 nir_deref_var
*image
= vtn_access_chain_to_deref(b
, sampled
.image
);
1652 instr
->texture
= nir_deref_var_clone(texture
, instr
);
1654 switch (instr
->op
) {
1659 /* These operations require a sampler */
1660 instr
->sampler
= nir_deref_var_clone(sampler
, instr
);
1663 case nir_texop_txf_ms
:
1667 case nir_texop_query_levels
:
1668 case nir_texop_texture_samples
:
1669 case nir_texop_samples_identical
:
1671 instr
->sampler
= NULL
;
1673 case nir_texop_txf_ms_mcs
:
1674 unreachable("unexpected nir_texop_txf_ms_mcs");
1677 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1678 nir_tex_instr_dest_size(instr
), 32, NULL
);
1680 assert(glsl_get_vector_elements(ret_type
->type
) ==
1681 nir_tex_instr_dest_size(instr
));
1684 nir_instr
*instruction
;
1685 if (gather_offsets
) {
1686 assert(glsl_get_base_type(gather_offsets
->type
) == GLSL_TYPE_ARRAY
);
1687 assert(glsl_get_length(gather_offsets
->type
) == 4);
1688 nir_tex_instr
*instrs
[4] = {instr
, NULL
, NULL
, NULL
};
1690 /* Copy the current instruction 4x */
1691 for (uint32_t i
= 1; i
< 4; i
++) {
1692 instrs
[i
] = nir_tex_instr_create(b
->shader
, instr
->num_srcs
);
1693 instrs
[i
]->op
= instr
->op
;
1694 instrs
[i
]->coord_components
= instr
->coord_components
;
1695 instrs
[i
]->sampler_dim
= instr
->sampler_dim
;
1696 instrs
[i
]->is_array
= instr
->is_array
;
1697 instrs
[i
]->is_shadow
= instr
->is_shadow
;
1698 instrs
[i
]->is_new_style_shadow
= instr
->is_new_style_shadow
;
1699 instrs
[i
]->component
= instr
->component
;
1700 instrs
[i
]->dest_type
= instr
->dest_type
;
1701 instrs
[i
]->texture
= nir_deref_var_clone(texture
, instrs
[i
]);
1702 instrs
[i
]->sampler
= NULL
;
1704 memcpy(instrs
[i
]->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1706 nir_ssa_dest_init(&instrs
[i
]->instr
, &instrs
[i
]->dest
,
1707 nir_tex_instr_dest_size(instr
), 32, NULL
);
1710 /* Fill in the last argument with the offset from the passed in offsets
1711 * and insert the instruction into the stream.
1713 for (uint32_t i
= 0; i
< 4; i
++) {
1715 src
.src
= nir_src_for_ssa(gather_offsets
->elems
[i
]->def
);
1716 src
.src_type
= nir_tex_src_offset
;
1717 instrs
[i
]->src
[instrs
[i
]->num_srcs
- 1] = src
;
1718 nir_builder_instr_insert(&b
->nb
, &instrs
[i
]->instr
);
1721 /* Combine the results of the 4 instructions by taking their .w
1724 nir_alu_instr
*vec4
= nir_alu_instr_create(b
->shader
, nir_op_vec4
);
1725 nir_ssa_dest_init(&vec4
->instr
, &vec4
->dest
.dest
, 4, 32, NULL
);
1726 vec4
->dest
.write_mask
= 0xf;
1727 for (uint32_t i
= 0; i
< 4; i
++) {
1728 vec4
->src
[i
].src
= nir_src_for_ssa(&instrs
[i
]->dest
.ssa
);
1729 vec4
->src
[i
].swizzle
[0] = 3;
1731 def
= &vec4
->dest
.dest
.ssa
;
1732 instruction
= &vec4
->instr
;
1734 def
= &instr
->dest
.ssa
;
1735 instruction
= &instr
->instr
;
1738 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
1739 val
->ssa
->def
= def
;
1741 nir_builder_instr_insert(&b
->nb
, instruction
);
1745 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
1746 const uint32_t *w
, nir_src
*src
)
1749 case SpvOpAtomicIIncrement
:
1750 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
1753 case SpvOpAtomicIDecrement
:
1754 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
1757 case SpvOpAtomicISub
:
1759 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
1762 case SpvOpAtomicCompareExchange
:
1763 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
1764 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
1767 case SpvOpAtomicExchange
:
1768 case SpvOpAtomicIAdd
:
1769 case SpvOpAtomicSMin
:
1770 case SpvOpAtomicUMin
:
1771 case SpvOpAtomicSMax
:
1772 case SpvOpAtomicUMax
:
1773 case SpvOpAtomicAnd
:
1775 case SpvOpAtomicXor
:
1776 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1780 unreachable("Invalid SPIR-V atomic");
1784 static nir_ssa_def
*
1785 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
1787 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
1789 /* The image_load_store intrinsics assume a 4-dim coordinate */
1790 unsigned dim
= glsl_get_vector_elements(coord
->type
);
1791 unsigned swizzle
[4];
1792 for (unsigned i
= 0; i
< 4; i
++)
1793 swizzle
[i
] = MIN2(i
, dim
- 1);
1795 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
1799 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
1800 const uint32_t *w
, unsigned count
)
1802 /* Just get this one out of the way */
1803 if (opcode
== SpvOpImageTexelPointer
) {
1804 struct vtn_value
*val
=
1805 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
1806 val
->image
= ralloc(b
, struct vtn_image_pointer
);
1809 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1810 val
->image
->coord
= get_image_coord(b
, w
[4]);
1811 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
1815 struct vtn_image_pointer image
;
1818 case SpvOpAtomicExchange
:
1819 case SpvOpAtomicCompareExchange
:
1820 case SpvOpAtomicCompareExchangeWeak
:
1821 case SpvOpAtomicIIncrement
:
1822 case SpvOpAtomicIDecrement
:
1823 case SpvOpAtomicIAdd
:
1824 case SpvOpAtomicISub
:
1825 case SpvOpAtomicLoad
:
1826 case SpvOpAtomicSMin
:
1827 case SpvOpAtomicUMin
:
1828 case SpvOpAtomicSMax
:
1829 case SpvOpAtomicUMax
:
1830 case SpvOpAtomicAnd
:
1832 case SpvOpAtomicXor
:
1833 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
1836 case SpvOpAtomicStore
:
1837 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
1840 case SpvOpImageQuerySize
:
1842 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1844 image
.sample
= NULL
;
1847 case SpvOpImageRead
:
1849 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1850 image
.coord
= get_image_coord(b
, w
[4]);
1852 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
1853 assert(w
[5] == SpvImageOperandsSampleMask
);
1854 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
1856 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1860 case SpvOpImageWrite
:
1862 vtn_value(b
, w
[1], vtn_value_type_access_chain
)->access_chain
;
1863 image
.coord
= get_image_coord(b
, w
[2]);
1867 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
1868 assert(w
[4] == SpvImageOperandsSampleMask
);
1869 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
1871 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1876 unreachable("Invalid image opcode");
1879 nir_intrinsic_op op
;
1881 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_##N; break;
1882 OP(ImageQuerySize
, size
)
1884 OP(ImageWrite
, store
)
1885 OP(AtomicLoad
, load
)
1886 OP(AtomicStore
, store
)
1887 OP(AtomicExchange
, atomic_exchange
)
1888 OP(AtomicCompareExchange
, atomic_comp_swap
)
1889 OP(AtomicIIncrement
, atomic_add
)
1890 OP(AtomicIDecrement
, atomic_add
)
1891 OP(AtomicIAdd
, atomic_add
)
1892 OP(AtomicISub
, atomic_add
)
1893 OP(AtomicSMin
, atomic_min
)
1894 OP(AtomicUMin
, atomic_min
)
1895 OP(AtomicSMax
, atomic_max
)
1896 OP(AtomicUMax
, atomic_max
)
1897 OP(AtomicAnd
, atomic_and
)
1898 OP(AtomicOr
, atomic_or
)
1899 OP(AtomicXor
, atomic_xor
)
1902 unreachable("Invalid image opcode");
1905 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
1907 nir_deref_var
*image_deref
= vtn_access_chain_to_deref(b
, image
.image
);
1908 intrin
->variables
[0] = nir_deref_var_clone(image_deref
, intrin
);
1910 /* ImageQuerySize doesn't take any extra parameters */
1911 if (opcode
!= SpvOpImageQuerySize
) {
1912 /* The image coordinate is always 4 components but we may not have that
1913 * many. Swizzle to compensate.
1916 for (unsigned i
= 0; i
< 4; i
++)
1917 swiz
[i
] = i
< image
.coord
->num_components
? i
: 0;
1918 intrin
->src
[0] = nir_src_for_ssa(nir_swizzle(&b
->nb
, image
.coord
,
1920 intrin
->src
[1] = nir_src_for_ssa(image
.sample
);
1924 case SpvOpAtomicLoad
:
1925 case SpvOpImageQuerySize
:
1926 case SpvOpImageRead
:
1928 case SpvOpAtomicStore
:
1929 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
1931 case SpvOpImageWrite
:
1932 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[3])->def
);
1935 case SpvOpAtomicIIncrement
:
1936 case SpvOpAtomicIDecrement
:
1937 case SpvOpAtomicExchange
:
1938 case SpvOpAtomicIAdd
:
1939 case SpvOpAtomicSMin
:
1940 case SpvOpAtomicUMin
:
1941 case SpvOpAtomicSMax
:
1942 case SpvOpAtomicUMax
:
1943 case SpvOpAtomicAnd
:
1945 case SpvOpAtomicXor
:
1946 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[2]);
1950 unreachable("Invalid image opcode");
1953 if (opcode
!= SpvOpImageWrite
) {
1954 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1955 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1956 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 4, 32, NULL
);
1958 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
1960 /* The image intrinsics always return 4 channels but we may not want
1961 * that many. Emit a mov to trim it down.
1963 unsigned swiz
[4] = {0, 1, 2, 3};
1964 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
1965 val
->ssa
->def
= nir_swizzle(&b
->nb
, &intrin
->dest
.ssa
, swiz
,
1966 glsl_get_vector_elements(type
->type
), false);
1968 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
1972 static nir_intrinsic_op
1973 get_ssbo_nir_atomic_op(SpvOp opcode
)
1976 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
1977 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
1978 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
1979 OP(AtomicExchange
, atomic_exchange
)
1980 OP(AtomicCompareExchange
, atomic_comp_swap
)
1981 OP(AtomicIIncrement
, atomic_add
)
1982 OP(AtomicIDecrement
, atomic_add
)
1983 OP(AtomicIAdd
, atomic_add
)
1984 OP(AtomicISub
, atomic_add
)
1985 OP(AtomicSMin
, atomic_imin
)
1986 OP(AtomicUMin
, atomic_umin
)
1987 OP(AtomicSMax
, atomic_imax
)
1988 OP(AtomicUMax
, atomic_umax
)
1989 OP(AtomicAnd
, atomic_and
)
1990 OP(AtomicOr
, atomic_or
)
1991 OP(AtomicXor
, atomic_xor
)
1994 unreachable("Invalid SSBO atomic");
1998 static nir_intrinsic_op
1999 get_shared_nir_atomic_op(SpvOp opcode
)
2002 case SpvOpAtomicLoad
: return nir_intrinsic_load_var
;
2003 case SpvOpAtomicStore
: return nir_intrinsic_store_var
;
2004 #define OP(S, N) case SpvOp##S: return nir_intrinsic_var_##N;
2005 OP(AtomicExchange
, atomic_exchange
)
2006 OP(AtomicCompareExchange
, atomic_comp_swap
)
2007 OP(AtomicIIncrement
, atomic_add
)
2008 OP(AtomicIDecrement
, atomic_add
)
2009 OP(AtomicIAdd
, atomic_add
)
2010 OP(AtomicISub
, atomic_add
)
2011 OP(AtomicSMin
, atomic_imin
)
2012 OP(AtomicUMin
, atomic_umin
)
2013 OP(AtomicSMax
, atomic_imax
)
2014 OP(AtomicUMax
, atomic_umax
)
2015 OP(AtomicAnd
, atomic_and
)
2016 OP(AtomicOr
, atomic_or
)
2017 OP(AtomicXor
, atomic_xor
)
2020 unreachable("Invalid shared atomic");
2025 vtn_handle_ssbo_or_shared_atomic(struct vtn_builder
*b
, SpvOp opcode
,
2026 const uint32_t *w
, unsigned count
)
2028 struct vtn_access_chain
*chain
;
2029 nir_intrinsic_instr
*atomic
;
2032 case SpvOpAtomicLoad
:
2033 case SpvOpAtomicExchange
:
2034 case SpvOpAtomicCompareExchange
:
2035 case SpvOpAtomicCompareExchangeWeak
:
2036 case SpvOpAtomicIIncrement
:
2037 case SpvOpAtomicIDecrement
:
2038 case SpvOpAtomicIAdd
:
2039 case SpvOpAtomicISub
:
2040 case SpvOpAtomicSMin
:
2041 case SpvOpAtomicUMin
:
2042 case SpvOpAtomicSMax
:
2043 case SpvOpAtomicUMax
:
2044 case SpvOpAtomicAnd
:
2046 case SpvOpAtomicXor
:
2048 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
2051 case SpvOpAtomicStore
:
2053 vtn_value(b
, w
[1], vtn_value_type_access_chain
)->access_chain
;
2057 unreachable("Invalid SPIR-V atomic");
2061 SpvScope scope = w[4];
2062 SpvMemorySemanticsMask semantics = w[5];
2065 if (chain
->var
->mode
== vtn_variable_mode_workgroup
) {
2066 struct vtn_type
*type
= chain
->var
->type
;
2067 nir_deref_var
*deref
= vtn_access_chain_to_deref(b
, chain
);
2068 nir_intrinsic_op op
= get_shared_nir_atomic_op(opcode
);
2069 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2070 atomic
->variables
[0] = nir_deref_var_clone(deref
, atomic
);
2073 case SpvOpAtomicLoad
:
2074 atomic
->num_components
= glsl_get_vector_elements(type
->type
);
2077 case SpvOpAtomicStore
:
2078 atomic
->num_components
= glsl_get_vector_elements(type
->type
);
2079 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2080 atomic
->src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2083 case SpvOpAtomicExchange
:
2084 case SpvOpAtomicCompareExchange
:
2085 case SpvOpAtomicCompareExchangeWeak
:
2086 case SpvOpAtomicIIncrement
:
2087 case SpvOpAtomicIDecrement
:
2088 case SpvOpAtomicIAdd
:
2089 case SpvOpAtomicISub
:
2090 case SpvOpAtomicSMin
:
2091 case SpvOpAtomicUMin
:
2092 case SpvOpAtomicSMax
:
2093 case SpvOpAtomicUMax
:
2094 case SpvOpAtomicAnd
:
2096 case SpvOpAtomicXor
:
2097 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[0]);
2101 unreachable("Invalid SPIR-V atomic");
2105 assert(chain
->var
->mode
== vtn_variable_mode_ssbo
);
2106 struct vtn_type
*type
;
2107 nir_ssa_def
*offset
, *index
;
2108 offset
= vtn_access_chain_to_offset(b
, chain
, &index
, &type
, NULL
, false);
2110 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(opcode
);
2112 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2115 case SpvOpAtomicLoad
:
2116 atomic
->num_components
= glsl_get_vector_elements(type
->type
);
2117 atomic
->src
[0] = nir_src_for_ssa(index
);
2118 atomic
->src
[1] = nir_src_for_ssa(offset
);
2121 case SpvOpAtomicStore
:
2122 atomic
->num_components
= glsl_get_vector_elements(type
->type
);
2123 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2124 atomic
->src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2125 atomic
->src
[1] = nir_src_for_ssa(index
);
2126 atomic
->src
[2] = nir_src_for_ssa(offset
);
2129 case SpvOpAtomicExchange
:
2130 case SpvOpAtomicCompareExchange
:
2131 case SpvOpAtomicCompareExchangeWeak
:
2132 case SpvOpAtomicIIncrement
:
2133 case SpvOpAtomicIDecrement
:
2134 case SpvOpAtomicIAdd
:
2135 case SpvOpAtomicISub
:
2136 case SpvOpAtomicSMin
:
2137 case SpvOpAtomicUMin
:
2138 case SpvOpAtomicSMax
:
2139 case SpvOpAtomicUMax
:
2140 case SpvOpAtomicAnd
:
2142 case SpvOpAtomicXor
:
2143 atomic
->src
[0] = nir_src_for_ssa(index
);
2144 atomic
->src
[1] = nir_src_for_ssa(offset
);
2145 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[2]);
2149 unreachable("Invalid SPIR-V atomic");
2153 if (opcode
!= SpvOpAtomicStore
) {
2154 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2156 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
2157 glsl_get_vector_elements(type
->type
),
2158 glsl_get_bit_size(type
->type
), NULL
);
2160 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2161 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
2162 val
->ssa
->def
= &atomic
->dest
.ssa
;
2163 val
->ssa
->type
= type
->type
;
2166 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
2169 static nir_alu_instr
*
2170 create_vec(nir_shader
*shader
, unsigned num_components
, unsigned bit_size
)
2173 switch (num_components
) {
2174 case 1: op
= nir_op_fmov
; break;
2175 case 2: op
= nir_op_vec2
; break;
2176 case 3: op
= nir_op_vec3
; break;
2177 case 4: op
= nir_op_vec4
; break;
2178 default: unreachable("bad vector size");
2181 nir_alu_instr
*vec
= nir_alu_instr_create(shader
, op
);
2182 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
2184 vec
->dest
.write_mask
= (1 << num_components
) - 1;
2189 struct vtn_ssa_value
*
2190 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
2192 if (src
->transposed
)
2193 return src
->transposed
;
2195 struct vtn_ssa_value
*dest
=
2196 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
2198 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
2199 nir_alu_instr
*vec
= create_vec(b
->shader
,
2200 glsl_get_matrix_columns(src
->type
),
2201 glsl_get_bit_size(src
->type
));
2202 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2203 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
2204 vec
->src
[0].swizzle
[0] = i
;
2206 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
2207 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
2208 vec
->src
[j
].swizzle
[0] = i
;
2211 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2212 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
2215 dest
->transposed
= src
;
2221 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
2223 unsigned swiz
[4] = { index
};
2224 return nir_swizzle(&b
->nb
, src
, swiz
, 1, true);
2228 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
2231 nir_alu_instr
*vec
= create_vec(b
->shader
, src
->num_components
,
2234 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
2236 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
2238 vec
->src
[i
].src
= nir_src_for_ssa(src
);
2239 vec
->src
[i
].swizzle
[0] = i
;
2243 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2245 return &vec
->dest
.dest
.ssa
;
2249 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2252 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
2253 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2254 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2255 vtn_vector_extract(b
, src
, i
), dest
);
2261 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2262 nir_ssa_def
*insert
, nir_ssa_def
*index
)
2264 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
2265 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2266 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2267 vtn_vector_insert(b
, src
, insert
, i
), dest
);
2272 static nir_ssa_def
*
2273 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
2274 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
2275 const uint32_t *indices
)
2277 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
, src0
->bit_size
);
2279 for (unsigned i
= 0; i
< num_components
; i
++) {
2280 uint32_t index
= indices
[i
];
2281 if (index
== 0xffffffff) {
2283 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
2284 } else if (index
< src0
->num_components
) {
2285 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
2286 vec
->src
[i
].swizzle
[0] = index
;
2288 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
2289 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
2293 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2295 return &vec
->dest
.dest
.ssa
;
2299 * Concatentates a number of vectors/scalars together to produce a vector
2301 static nir_ssa_def
*
2302 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
2303 unsigned num_srcs
, nir_ssa_def
**srcs
)
2305 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
,
2308 unsigned dest_idx
= 0;
2309 for (unsigned i
= 0; i
< num_srcs
; i
++) {
2310 nir_ssa_def
*src
= srcs
[i
];
2311 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
2312 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
2313 vec
->src
[dest_idx
].swizzle
[0] = j
;
2318 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2320 return &vec
->dest
.dest
.ssa
;
2323 static struct vtn_ssa_value
*
2324 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
2326 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
2327 dest
->type
= src
->type
;
2329 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2330 dest
->def
= src
->def
;
2332 unsigned elems
= glsl_get_length(src
->type
);
2334 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
2335 for (unsigned i
= 0; i
< elems
; i
++)
2336 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
2342 static struct vtn_ssa_value
*
2343 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2344 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
2345 unsigned num_indices
)
2347 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
2349 struct vtn_ssa_value
*cur
= dest
;
2351 for (i
= 0; i
< num_indices
- 1; i
++) {
2352 cur
= cur
->elems
[indices
[i
]];
2355 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2356 /* According to the SPIR-V spec, OpCompositeInsert may work down to
2357 * the component granularity. In that case, the last index will be
2358 * the index to insert the scalar into the vector.
2361 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
2363 cur
->elems
[indices
[i
]] = insert
;
2369 static struct vtn_ssa_value
*
2370 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2371 const uint32_t *indices
, unsigned num_indices
)
2373 struct vtn_ssa_value
*cur
= src
;
2374 for (unsigned i
= 0; i
< num_indices
; i
++) {
2375 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2376 assert(i
== num_indices
- 1);
2377 /* According to the SPIR-V spec, OpCompositeExtract may work down to
2378 * the component granularity. The last index will be the index of the
2379 * vector to extract.
2382 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
2383 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
2384 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
2387 cur
= cur
->elems
[indices
[i
]];
2395 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
2396 const uint32_t *w
, unsigned count
)
2398 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2399 const struct glsl_type
*type
=
2400 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
2401 val
->ssa
= vtn_create_ssa_value(b
, type
);
2404 case SpvOpVectorExtractDynamic
:
2405 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2406 vtn_ssa_value(b
, w
[4])->def
);
2409 case SpvOpVectorInsertDynamic
:
2410 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2411 vtn_ssa_value(b
, w
[4])->def
,
2412 vtn_ssa_value(b
, w
[5])->def
);
2415 case SpvOpVectorShuffle
:
2416 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
2417 vtn_ssa_value(b
, w
[3])->def
,
2418 vtn_ssa_value(b
, w
[4])->def
,
2422 case SpvOpCompositeConstruct
: {
2423 unsigned elems
= count
- 3;
2424 if (glsl_type_is_vector_or_scalar(type
)) {
2425 nir_ssa_def
*srcs
[4];
2426 for (unsigned i
= 0; i
< elems
; i
++)
2427 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
2429 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
2432 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2433 for (unsigned i
= 0; i
< elems
; i
++)
2434 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
2438 case SpvOpCompositeExtract
:
2439 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
2443 case SpvOpCompositeInsert
:
2444 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
2445 vtn_ssa_value(b
, w
[3]),
2449 case SpvOpCopyObject
:
2450 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
2454 unreachable("unknown composite operation");
2459 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
2460 const uint32_t *w
, unsigned count
)
2462 nir_intrinsic_op intrinsic_op
;
2464 case SpvOpEmitVertex
:
2465 case SpvOpEmitStreamVertex
:
2466 intrinsic_op
= nir_intrinsic_emit_vertex
;
2468 case SpvOpEndPrimitive
:
2469 case SpvOpEndStreamPrimitive
:
2470 intrinsic_op
= nir_intrinsic_end_primitive
;
2472 case SpvOpMemoryBarrier
:
2473 intrinsic_op
= nir_intrinsic_memory_barrier
;
2475 case SpvOpControlBarrier
:
2476 intrinsic_op
= nir_intrinsic_barrier
;
2479 unreachable("unknown barrier instruction");
2482 nir_intrinsic_instr
*intrin
=
2483 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
2485 if (opcode
== SpvOpEmitStreamVertex
|| opcode
== SpvOpEndStreamPrimitive
)
2486 nir_intrinsic_set_stream_id(intrin
, w
[1]);
2488 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2492 gl_primitive_from_spv_execution_mode(SpvExecutionMode mode
)
2495 case SpvExecutionModeInputPoints
:
2496 case SpvExecutionModeOutputPoints
:
2497 return 0; /* GL_POINTS */
2498 case SpvExecutionModeInputLines
:
2499 return 1; /* GL_LINES */
2500 case SpvExecutionModeInputLinesAdjacency
:
2501 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
2502 case SpvExecutionModeTriangles
:
2503 return 4; /* GL_TRIANGLES */
2504 case SpvExecutionModeInputTrianglesAdjacency
:
2505 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
2506 case SpvExecutionModeQuads
:
2507 return 7; /* GL_QUADS */
2508 case SpvExecutionModeIsolines
:
2509 return 0x8E7A; /* GL_ISOLINES */
2510 case SpvExecutionModeOutputLineStrip
:
2511 return 3; /* GL_LINE_STRIP */
2512 case SpvExecutionModeOutputTriangleStrip
:
2513 return 5; /* GL_TRIANGLE_STRIP */
2515 assert(!"Invalid primitive type");
2521 vertices_in_from_spv_execution_mode(SpvExecutionMode mode
)
2524 case SpvExecutionModeInputPoints
:
2526 case SpvExecutionModeInputLines
:
2528 case SpvExecutionModeInputLinesAdjacency
:
2530 case SpvExecutionModeTriangles
:
2532 case SpvExecutionModeInputTrianglesAdjacency
:
2535 assert(!"Invalid GS input mode");
2540 static gl_shader_stage
2541 stage_for_execution_model(SpvExecutionModel model
)
2544 case SpvExecutionModelVertex
:
2545 return MESA_SHADER_VERTEX
;
2546 case SpvExecutionModelTessellationControl
:
2547 return MESA_SHADER_TESS_CTRL
;
2548 case SpvExecutionModelTessellationEvaluation
:
2549 return MESA_SHADER_TESS_EVAL
;
2550 case SpvExecutionModelGeometry
:
2551 return MESA_SHADER_GEOMETRY
;
2552 case SpvExecutionModelFragment
:
2553 return MESA_SHADER_FRAGMENT
;
2554 case SpvExecutionModelGLCompute
:
2555 return MESA_SHADER_COMPUTE
;
2557 unreachable("Unsupported execution model");
2561 #define spv_check_supported(name, cap) do { \
2562 if (!(b->ext && b->ext->name)) \
2563 vtn_warn("Unsupported SPIR-V capability: %s", \
2564 spirv_capability_to_string(cap)); \
2568 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2569 const uint32_t *w
, unsigned count
)
2573 case SpvOpSourceExtension
:
2574 case SpvOpSourceContinued
:
2575 case SpvOpExtension
:
2576 /* Unhandled, but these are for debug so that's ok. */
2579 case SpvOpCapability
: {
2580 SpvCapability cap
= w
[1];
2582 case SpvCapabilityMatrix
:
2583 case SpvCapabilityShader
:
2584 case SpvCapabilityGeometry
:
2585 case SpvCapabilityGeometryPointSize
:
2586 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
2587 case SpvCapabilitySampledImageArrayDynamicIndexing
:
2588 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
2589 case SpvCapabilityStorageImageArrayDynamicIndexing
:
2590 case SpvCapabilityImageRect
:
2591 case SpvCapabilitySampledRect
:
2592 case SpvCapabilitySampled1D
:
2593 case SpvCapabilityImage1D
:
2594 case SpvCapabilitySampledCubeArray
:
2595 case SpvCapabilitySampledBuffer
:
2596 case SpvCapabilityImageBuffer
:
2597 case SpvCapabilityImageQuery
:
2598 case SpvCapabilityDerivativeControl
:
2599 case SpvCapabilityInterpolationFunction
:
2600 case SpvCapabilityMultiViewport
:
2601 case SpvCapabilitySampleRateShading
:
2602 case SpvCapabilityClipDistance
:
2603 case SpvCapabilityCullDistance
:
2604 case SpvCapabilityInputAttachment
:
2605 case SpvCapabilityImageGatherExtended
:
2606 case SpvCapabilityStorageImageExtendedFormats
:
2609 case SpvCapabilityGeometryStreams
:
2610 case SpvCapabilityLinkage
:
2611 case SpvCapabilityVector16
:
2612 case SpvCapabilityFloat16Buffer
:
2613 case SpvCapabilityFloat16
:
2614 case SpvCapabilityInt64
:
2615 case SpvCapabilityInt64Atomics
:
2616 case SpvCapabilityAtomicStorage
:
2617 case SpvCapabilityInt16
:
2618 case SpvCapabilityStorageImageMultisample
:
2619 case SpvCapabilityImageCubeArray
:
2620 case SpvCapabilityInt8
:
2621 case SpvCapabilitySparseResidency
:
2622 case SpvCapabilityMinLod
:
2623 case SpvCapabilityTransformFeedback
:
2624 case SpvCapabilityStorageImageReadWithoutFormat
:
2625 case SpvCapabilityStorageImageWriteWithoutFormat
:
2626 vtn_warn("Unsupported SPIR-V capability: %s",
2627 spirv_capability_to_string(cap
));
2630 case SpvCapabilityFloat64
:
2631 spv_check_supported(float64
, cap
);
2634 case SpvCapabilityAddresses
:
2635 case SpvCapabilityKernel
:
2636 case SpvCapabilityImageBasic
:
2637 case SpvCapabilityImageReadWrite
:
2638 case SpvCapabilityImageMipmap
:
2639 case SpvCapabilityPipes
:
2640 case SpvCapabilityGroups
:
2641 case SpvCapabilityDeviceEnqueue
:
2642 case SpvCapabilityLiteralSampler
:
2643 case SpvCapabilityGenericPointer
:
2644 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
2645 spirv_capability_to_string(cap
));
2648 case SpvCapabilityImageMSArray
:
2649 spv_check_supported(image_ms_array
, cap
);
2652 case SpvCapabilityTessellation
:
2653 case SpvCapabilityTessellationPointSize
:
2654 spv_check_supported(tessellation
, cap
);
2660 case SpvOpExtInstImport
:
2661 vtn_handle_extension(b
, opcode
, w
, count
);
2664 case SpvOpMemoryModel
:
2665 assert(w
[1] == SpvAddressingModelLogical
);
2666 assert(w
[2] == SpvMemoryModelGLSL450
);
2669 case SpvOpEntryPoint
: {
2670 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
2671 /* Let this be a name label regardless */
2672 unsigned name_words
;
2673 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
2675 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
2676 stage_for_execution_model(w
[1]) != b
->entry_point_stage
)
2679 assert(b
->entry_point
== NULL
);
2680 b
->entry_point
= entry_point
;
2685 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
2686 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2690 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2693 case SpvOpMemberName
:
2697 case SpvOpExecutionMode
:
2698 case SpvOpDecorationGroup
:
2700 case SpvOpMemberDecorate
:
2701 case SpvOpGroupDecorate
:
2702 case SpvOpGroupMemberDecorate
:
2703 vtn_handle_decoration(b
, opcode
, w
, count
);
2707 return false; /* End of preamble */
2714 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
2715 const struct vtn_decoration
*mode
, void *data
)
2717 assert(b
->entry_point
== entry_point
);
2719 switch(mode
->exec_mode
) {
2720 case SpvExecutionModeOriginUpperLeft
:
2721 case SpvExecutionModeOriginLowerLeft
:
2722 b
->origin_upper_left
=
2723 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
2726 case SpvExecutionModeEarlyFragmentTests
:
2727 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2728 b
->shader
->info
->fs
.early_fragment_tests
= true;
2731 case SpvExecutionModeInvocations
:
2732 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2733 b
->shader
->info
->gs
.invocations
= MAX2(1, mode
->literals
[0]);
2736 case SpvExecutionModeDepthReplacing
:
2737 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2738 b
->shader
->info
->fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
2740 case SpvExecutionModeDepthGreater
:
2741 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2742 b
->shader
->info
->fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
2744 case SpvExecutionModeDepthLess
:
2745 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2746 b
->shader
->info
->fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
2748 case SpvExecutionModeDepthUnchanged
:
2749 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2750 b
->shader
->info
->fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
2753 case SpvExecutionModeLocalSize
:
2754 assert(b
->shader
->stage
== MESA_SHADER_COMPUTE
);
2755 b
->shader
->info
->cs
.local_size
[0] = mode
->literals
[0];
2756 b
->shader
->info
->cs
.local_size
[1] = mode
->literals
[1];
2757 b
->shader
->info
->cs
.local_size
[2] = mode
->literals
[2];
2759 case SpvExecutionModeLocalSizeHint
:
2760 break; /* Nothing to do with this */
2762 case SpvExecutionModeOutputVertices
:
2763 if (b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2764 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
) {
2765 b
->shader
->info
->tess
.tcs_vertices_out
= mode
->literals
[0];
2767 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2768 b
->shader
->info
->gs
.vertices_out
= mode
->literals
[0];
2772 case SpvExecutionModeInputPoints
:
2773 case SpvExecutionModeInputLines
:
2774 case SpvExecutionModeInputLinesAdjacency
:
2775 case SpvExecutionModeTriangles
:
2776 case SpvExecutionModeInputTrianglesAdjacency
:
2777 case SpvExecutionModeQuads
:
2778 case SpvExecutionModeIsolines
:
2779 if (b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2780 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
) {
2781 b
->shader
->info
->tess
.primitive_mode
=
2782 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
2784 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2785 b
->shader
->info
->gs
.vertices_in
=
2786 vertices_in_from_spv_execution_mode(mode
->exec_mode
);
2790 case SpvExecutionModeOutputPoints
:
2791 case SpvExecutionModeOutputLineStrip
:
2792 case SpvExecutionModeOutputTriangleStrip
:
2793 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2794 b
->shader
->info
->gs
.output_primitive
=
2795 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
2798 case SpvExecutionModeSpacingEqual
:
2799 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2800 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2801 b
->shader
->info
->tess
.spacing
= TESS_SPACING_EQUAL
;
2803 case SpvExecutionModeSpacingFractionalEven
:
2804 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2805 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2806 b
->shader
->info
->tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
2808 case SpvExecutionModeSpacingFractionalOdd
:
2809 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2810 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2811 b
->shader
->info
->tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
2813 case SpvExecutionModeVertexOrderCw
:
2814 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2815 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2816 /* Vulkan's notion of CCW seems to match the hardware backends,
2817 * but be the opposite of OpenGL. Currently NIR follows GL semantics,
2818 * so we set it backwards here.
2820 b
->shader
->info
->tess
.ccw
= true;
2822 case SpvExecutionModeVertexOrderCcw
:
2823 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2824 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2825 /* Backwards; see above */
2826 b
->shader
->info
->tess
.ccw
= false;
2828 case SpvExecutionModePointMode
:
2829 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2830 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2831 b
->shader
->info
->tess
.point_mode
= true;
2834 case SpvExecutionModePixelCenterInteger
:
2835 b
->pixel_center_integer
= true;
2838 case SpvExecutionModeXfb
:
2839 assert(!"Unhandled execution mode");
2842 case SpvExecutionModeVecTypeHint
:
2843 case SpvExecutionModeContractionOff
:
2849 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2850 const uint32_t *w
, unsigned count
)
2854 case SpvOpSourceContinued
:
2855 case SpvOpSourceExtension
:
2856 case SpvOpExtension
:
2857 case SpvOpCapability
:
2858 case SpvOpExtInstImport
:
2859 case SpvOpMemoryModel
:
2860 case SpvOpEntryPoint
:
2861 case SpvOpExecutionMode
:
2864 case SpvOpMemberName
:
2865 case SpvOpDecorationGroup
:
2867 case SpvOpMemberDecorate
:
2868 case SpvOpGroupDecorate
:
2869 case SpvOpGroupMemberDecorate
:
2870 assert(!"Invalid opcode types and variables section");
2876 case SpvOpTypeFloat
:
2877 case SpvOpTypeVector
:
2878 case SpvOpTypeMatrix
:
2879 case SpvOpTypeImage
:
2880 case SpvOpTypeSampler
:
2881 case SpvOpTypeSampledImage
:
2882 case SpvOpTypeArray
:
2883 case SpvOpTypeRuntimeArray
:
2884 case SpvOpTypeStruct
:
2885 case SpvOpTypeOpaque
:
2886 case SpvOpTypePointer
:
2887 case SpvOpTypeFunction
:
2888 case SpvOpTypeEvent
:
2889 case SpvOpTypeDeviceEvent
:
2890 case SpvOpTypeReserveId
:
2891 case SpvOpTypeQueue
:
2893 vtn_handle_type(b
, opcode
, w
, count
);
2896 case SpvOpConstantTrue
:
2897 case SpvOpConstantFalse
:
2899 case SpvOpConstantComposite
:
2900 case SpvOpConstantSampler
:
2901 case SpvOpConstantNull
:
2902 case SpvOpSpecConstantTrue
:
2903 case SpvOpSpecConstantFalse
:
2904 case SpvOpSpecConstant
:
2905 case SpvOpSpecConstantComposite
:
2906 case SpvOpSpecConstantOp
:
2907 vtn_handle_constant(b
, opcode
, w
, count
);
2911 vtn_handle_variables(b
, opcode
, w
, count
);
2915 return false; /* End of preamble */
2922 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2923 const uint32_t *w
, unsigned count
)
2929 case SpvOpLoopMerge
:
2930 case SpvOpSelectionMerge
:
2931 /* This is handled by cfg pre-pass and walk_blocks */
2935 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
2936 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2941 vtn_handle_extension(b
, opcode
, w
, count
);
2947 case SpvOpCopyMemory
:
2948 case SpvOpCopyMemorySized
:
2949 case SpvOpAccessChain
:
2950 case SpvOpInBoundsAccessChain
:
2951 case SpvOpArrayLength
:
2952 vtn_handle_variables(b
, opcode
, w
, count
);
2955 case SpvOpFunctionCall
:
2956 vtn_handle_function_call(b
, opcode
, w
, count
);
2959 case SpvOpSampledImage
:
2961 case SpvOpImageSampleImplicitLod
:
2962 case SpvOpImageSampleExplicitLod
:
2963 case SpvOpImageSampleDrefImplicitLod
:
2964 case SpvOpImageSampleDrefExplicitLod
:
2965 case SpvOpImageSampleProjImplicitLod
:
2966 case SpvOpImageSampleProjExplicitLod
:
2967 case SpvOpImageSampleProjDrefImplicitLod
:
2968 case SpvOpImageSampleProjDrefExplicitLod
:
2969 case SpvOpImageFetch
:
2970 case SpvOpImageGather
:
2971 case SpvOpImageDrefGather
:
2972 case SpvOpImageQuerySizeLod
:
2973 case SpvOpImageQueryLod
:
2974 case SpvOpImageQueryLevels
:
2975 case SpvOpImageQuerySamples
:
2976 vtn_handle_texture(b
, opcode
, w
, count
);
2979 case SpvOpImageRead
:
2980 case SpvOpImageWrite
:
2981 case SpvOpImageTexelPointer
:
2982 vtn_handle_image(b
, opcode
, w
, count
);
2985 case SpvOpImageQuerySize
: {
2986 struct vtn_access_chain
*image
=
2987 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
2988 if (glsl_type_is_image(image
->var
->var
->interface_type
)) {
2989 vtn_handle_image(b
, opcode
, w
, count
);
2991 vtn_handle_texture(b
, opcode
, w
, count
);
2996 case SpvOpAtomicLoad
:
2997 case SpvOpAtomicExchange
:
2998 case SpvOpAtomicCompareExchange
:
2999 case SpvOpAtomicCompareExchangeWeak
:
3000 case SpvOpAtomicIIncrement
:
3001 case SpvOpAtomicIDecrement
:
3002 case SpvOpAtomicIAdd
:
3003 case SpvOpAtomicISub
:
3004 case SpvOpAtomicSMin
:
3005 case SpvOpAtomicUMin
:
3006 case SpvOpAtomicSMax
:
3007 case SpvOpAtomicUMax
:
3008 case SpvOpAtomicAnd
:
3010 case SpvOpAtomicXor
: {
3011 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
3012 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
3013 vtn_handle_image(b
, opcode
, w
, count
);
3015 assert(pointer
->value_type
== vtn_value_type_access_chain
);
3016 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
3021 case SpvOpAtomicStore
: {
3022 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
3023 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
3024 vtn_handle_image(b
, opcode
, w
, count
);
3026 assert(pointer
->value_type
== vtn_value_type_access_chain
);
3027 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
3037 case SpvOpConvertFToU
:
3038 case SpvOpConvertFToS
:
3039 case SpvOpConvertSToF
:
3040 case SpvOpConvertUToF
:
3044 case SpvOpQuantizeToF16
:
3045 case SpvOpConvertPtrToU
:
3046 case SpvOpConvertUToPtr
:
3047 case SpvOpPtrCastToGeneric
:
3048 case SpvOpGenericCastToPtr
:
3054 case SpvOpSignBitSet
:
3055 case SpvOpLessOrGreater
:
3057 case SpvOpUnordered
:
3072 case SpvOpVectorTimesScalar
:
3074 case SpvOpIAddCarry
:
3075 case SpvOpISubBorrow
:
3076 case SpvOpUMulExtended
:
3077 case SpvOpSMulExtended
:
3078 case SpvOpShiftRightLogical
:
3079 case SpvOpShiftRightArithmetic
:
3080 case SpvOpShiftLeftLogical
:
3081 case SpvOpLogicalEqual
:
3082 case SpvOpLogicalNotEqual
:
3083 case SpvOpLogicalOr
:
3084 case SpvOpLogicalAnd
:
3085 case SpvOpLogicalNot
:
3086 case SpvOpBitwiseOr
:
3087 case SpvOpBitwiseXor
:
3088 case SpvOpBitwiseAnd
:
3091 case SpvOpFOrdEqual
:
3092 case SpvOpFUnordEqual
:
3093 case SpvOpINotEqual
:
3094 case SpvOpFOrdNotEqual
:
3095 case SpvOpFUnordNotEqual
:
3096 case SpvOpULessThan
:
3097 case SpvOpSLessThan
:
3098 case SpvOpFOrdLessThan
:
3099 case SpvOpFUnordLessThan
:
3100 case SpvOpUGreaterThan
:
3101 case SpvOpSGreaterThan
:
3102 case SpvOpFOrdGreaterThan
:
3103 case SpvOpFUnordGreaterThan
:
3104 case SpvOpULessThanEqual
:
3105 case SpvOpSLessThanEqual
:
3106 case SpvOpFOrdLessThanEqual
:
3107 case SpvOpFUnordLessThanEqual
:
3108 case SpvOpUGreaterThanEqual
:
3109 case SpvOpSGreaterThanEqual
:
3110 case SpvOpFOrdGreaterThanEqual
:
3111 case SpvOpFUnordGreaterThanEqual
:
3117 case SpvOpFwidthFine
:
3118 case SpvOpDPdxCoarse
:
3119 case SpvOpDPdyCoarse
:
3120 case SpvOpFwidthCoarse
:
3121 case SpvOpBitFieldInsert
:
3122 case SpvOpBitFieldSExtract
:
3123 case SpvOpBitFieldUExtract
:
3124 case SpvOpBitReverse
:
3126 case SpvOpTranspose
:
3127 case SpvOpOuterProduct
:
3128 case SpvOpMatrixTimesScalar
:
3129 case SpvOpVectorTimesMatrix
:
3130 case SpvOpMatrixTimesVector
:
3131 case SpvOpMatrixTimesMatrix
:
3132 vtn_handle_alu(b
, opcode
, w
, count
);
3135 case SpvOpVectorExtractDynamic
:
3136 case SpvOpVectorInsertDynamic
:
3137 case SpvOpVectorShuffle
:
3138 case SpvOpCompositeConstruct
:
3139 case SpvOpCompositeExtract
:
3140 case SpvOpCompositeInsert
:
3141 case SpvOpCopyObject
:
3142 vtn_handle_composite(b
, opcode
, w
, count
);
3145 case SpvOpEmitVertex
:
3146 case SpvOpEndPrimitive
:
3147 case SpvOpEmitStreamVertex
:
3148 case SpvOpEndStreamPrimitive
:
3149 case SpvOpControlBarrier
:
3150 case SpvOpMemoryBarrier
:
3151 vtn_handle_barrier(b
, opcode
, w
, count
);
3155 unreachable("Unhandled opcode");
3162 spirv_to_nir(const uint32_t *words
, size_t word_count
,
3163 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
3164 gl_shader_stage stage
, const char *entry_point_name
,
3165 const struct nir_spirv_supported_extensions
*ext
,
3166 const nir_shader_compiler_options
*options
)
3168 const uint32_t *word_end
= words
+ word_count
;
3170 /* Handle the SPIR-V header (first 4 dwords) */
3171 assert(word_count
> 5);
3173 assert(words
[0] == SpvMagicNumber
);
3174 assert(words
[1] >= 0x10000);
3175 /* words[2] == generator magic */
3176 unsigned value_id_bound
= words
[3];
3177 assert(words
[4] == 0);
3181 /* Initialize the stn_builder object */
3182 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
3183 b
->value_id_bound
= value_id_bound
;
3184 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
3185 exec_list_make_empty(&b
->functions
);
3186 b
->entry_point_stage
= stage
;
3187 b
->entry_point_name
= entry_point_name
;
3190 /* Handle all the preamble instructions */
3191 words
= vtn_foreach_instruction(b
, words
, word_end
,
3192 vtn_handle_preamble_instruction
);
3194 if (b
->entry_point
== NULL
) {
3195 assert(!"Entry point not found");
3200 b
->shader
= nir_shader_create(NULL
, stage
, options
, NULL
);
3202 /* Set shader info defaults */
3203 b
->shader
->info
->gs
.invocations
= 1;
3205 /* Parse execution modes */
3206 vtn_foreach_execution_mode(b
, b
->entry_point
,
3207 vtn_handle_execution_mode
, NULL
);
3209 b
->specializations
= spec
;
3210 b
->num_specializations
= num_spec
;
3212 /* Handle all variable, type, and constant instructions */
3213 words
= vtn_foreach_instruction(b
, words
, word_end
,
3214 vtn_handle_variable_or_type_instruction
);
3216 vtn_build_cfg(b
, words
, word_end
);
3218 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
3219 b
->impl
= func
->impl
;
3220 b
->const_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
3221 _mesa_key_pointer_equal
);
3223 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
3226 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
3227 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
3228 assert(entry_point
);