2 * Copyright © 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Jason Ekstrand (jason@jlekstrand.net)
28 #include "vtn_private.h"
29 #include "nir/nir_vla.h"
30 #include "nir/nir_control_flow.h"
31 #include "nir/nir_constant_expressions.h"
32 #include "spirv_info.h"
34 struct spec_constant_value
{
43 _vtn_warn(const char *file
, int line
, const char *msg
, ...)
49 formatted
= ralloc_vasprintf(NULL
, msg
, args
);
52 fprintf(stderr
, "%s:%d WARNING: %s\n", file
, line
, formatted
);
54 ralloc_free(formatted
);
57 static struct vtn_ssa_value
*
58 vtn_undef_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
60 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
63 if (glsl_type_is_vector_or_scalar(type
)) {
64 unsigned num_components
= glsl_get_vector_elements(val
->type
);
65 unsigned bit_size
= glsl_get_bit_size(val
->type
);
66 val
->def
= nir_ssa_undef(&b
->nb
, num_components
, bit_size
);
68 unsigned elems
= glsl_get_length(val
->type
);
69 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
70 if (glsl_type_is_matrix(type
)) {
71 const struct glsl_type
*elem_type
=
72 glsl_vector_type(glsl_get_base_type(type
),
73 glsl_get_vector_elements(type
));
75 for (unsigned i
= 0; i
< elems
; i
++)
76 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
77 } else if (glsl_type_is_array(type
)) {
78 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
79 for (unsigned i
= 0; i
< elems
; i
++)
80 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
82 for (unsigned i
= 0; i
< elems
; i
++) {
83 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
84 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
92 static struct vtn_ssa_value
*
93 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
94 const struct glsl_type
*type
)
96 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
101 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
104 switch (glsl_get_base_type(type
)) {
107 case GLSL_TYPE_INT64
:
108 case GLSL_TYPE_UINT64
:
110 case GLSL_TYPE_FLOAT
:
111 case GLSL_TYPE_DOUBLE
: {
112 int bit_size
= glsl_get_bit_size(type
);
113 if (glsl_type_is_vector_or_scalar(type
)) {
114 unsigned num_components
= glsl_get_vector_elements(val
->type
);
115 nir_load_const_instr
*load
=
116 nir_load_const_instr_create(b
->shader
, num_components
, bit_size
);
118 load
->value
= constant
->values
[0];
120 nir_instr_insert_before_cf_list(&b
->impl
->body
, &load
->instr
);
121 val
->def
= &load
->def
;
123 assert(glsl_type_is_matrix(type
));
124 unsigned rows
= glsl_get_vector_elements(val
->type
);
125 unsigned columns
= glsl_get_matrix_columns(val
->type
);
126 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, columns
);
128 for (unsigned i
= 0; i
< columns
; i
++) {
129 struct vtn_ssa_value
*col_val
= rzalloc(b
, struct vtn_ssa_value
);
130 col_val
->type
= glsl_get_column_type(val
->type
);
131 nir_load_const_instr
*load
=
132 nir_load_const_instr_create(b
->shader
, rows
, bit_size
);
134 load
->value
= constant
->values
[i
];
136 nir_instr_insert_before_cf_list(&b
->impl
->body
, &load
->instr
);
137 col_val
->def
= &load
->def
;
139 val
->elems
[i
] = col_val
;
145 case GLSL_TYPE_ARRAY
: {
146 unsigned elems
= glsl_get_length(val
->type
);
147 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
148 const struct glsl_type
*elem_type
= glsl_get_array_element(val
->type
);
149 for (unsigned i
= 0; i
< elems
; i
++)
150 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
155 case GLSL_TYPE_STRUCT
: {
156 unsigned elems
= glsl_get_length(val
->type
);
157 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
158 for (unsigned i
= 0; i
< elems
; i
++) {
159 const struct glsl_type
*elem_type
=
160 glsl_get_struct_field(val
->type
, i
);
161 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
168 unreachable("bad constant type");
174 struct vtn_ssa_value
*
175 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
177 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
178 switch (val
->value_type
) {
179 case vtn_value_type_undef
:
180 return vtn_undef_ssa_value(b
, val
->type
->type
);
182 case vtn_value_type_constant
:
183 return vtn_const_ssa_value(b
, val
->constant
, val
->const_type
);
185 case vtn_value_type_ssa
:
188 case vtn_value_type_pointer
:
189 /* This is needed for function parameters */
190 return vtn_variable_load(b
, val
->pointer
);
193 unreachable("Invalid type for an SSA value");
198 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
199 unsigned word_count
, unsigned *words_used
)
201 char *dup
= ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
203 /* Ammount of space taken by the string (including the null) */
204 unsigned len
= strlen(dup
) + 1;
205 *words_used
= DIV_ROUND_UP(len
, sizeof(*words
));
211 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
212 const uint32_t *end
, vtn_instruction_handler handler
)
218 const uint32_t *w
= start
;
220 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
221 unsigned count
= w
[0] >> SpvWordCountShift
;
222 assert(count
>= 1 && w
+ count
<= end
);
226 break; /* Do nothing */
229 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
241 if (!handler(b
, opcode
, w
, count
))
253 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
254 const uint32_t *w
, unsigned count
)
257 case SpvOpExtInstImport
: {
258 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
259 if (strcmp((const char *)&w
[2], "GLSL.std.450") == 0) {
260 val
->ext_handler
= vtn_handle_glsl450_instruction
;
262 assert(!"Unsupported extension");
268 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
269 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
276 unreachable("Unhandled opcode");
281 _foreach_decoration_helper(struct vtn_builder
*b
,
282 struct vtn_value
*base_value
,
284 struct vtn_value
*value
,
285 vtn_decoration_foreach_cb cb
, void *data
)
287 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
289 if (dec
->scope
== VTN_DEC_DECORATION
) {
290 member
= parent_member
;
291 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
292 assert(parent_member
== -1);
293 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
295 /* Not a decoration */
300 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
301 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
304 cb(b
, base_value
, member
, dec
, data
);
309 /** Iterates (recursively if needed) over all of the decorations on a value
311 * This function iterates over all of the decorations applied to a given
312 * value. If it encounters a decoration group, it recurses into the group
313 * and iterates over all of those decorations as well.
316 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
317 vtn_decoration_foreach_cb cb
, void *data
)
319 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
323 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
324 vtn_execution_mode_foreach_cb cb
, void *data
)
326 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
327 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
330 assert(dec
->group
== NULL
);
331 cb(b
, value
, dec
, data
);
336 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
337 const uint32_t *w
, unsigned count
)
339 const uint32_t *w_end
= w
+ count
;
340 const uint32_t target
= w
[1];
344 case SpvOpDecorationGroup
:
345 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
349 case SpvOpMemberDecorate
:
350 case SpvOpExecutionMode
: {
351 struct vtn_value
*val
= &b
->values
[target
];
353 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
356 dec
->scope
= VTN_DEC_DECORATION
;
358 case SpvOpMemberDecorate
:
359 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
361 case SpvOpExecutionMode
:
362 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
365 unreachable("Invalid decoration opcode");
367 dec
->decoration
= *(w
++);
370 /* Link into the list */
371 dec
->next
= val
->decoration
;
372 val
->decoration
= dec
;
376 case SpvOpGroupMemberDecorate
:
377 case SpvOpGroupDecorate
: {
378 struct vtn_value
*group
=
379 vtn_value(b
, target
, vtn_value_type_decoration_group
);
381 for (; w
< w_end
; w
++) {
382 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
383 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
386 if (opcode
== SpvOpGroupDecorate
) {
387 dec
->scope
= VTN_DEC_DECORATION
;
389 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
392 /* Link into the list */
393 dec
->next
= val
->decoration
;
394 val
->decoration
= dec
;
400 unreachable("Unhandled opcode");
404 struct member_decoration_ctx
{
406 struct glsl_struct_field
*fields
;
407 struct vtn_type
*type
;
410 /* does a shallow copy of a vtn_type */
412 static struct vtn_type
*
413 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
415 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
416 dest
->type
= src
->type
;
417 dest
->is_builtin
= src
->is_builtin
;
419 dest
->builtin
= src
->builtin
;
421 if (!glsl_type_is_scalar(src
->type
)) {
422 switch (glsl_get_base_type(src
->type
)) {
425 case GLSL_TYPE_INT64
:
426 case GLSL_TYPE_UINT64
:
428 case GLSL_TYPE_FLOAT
:
429 case GLSL_TYPE_DOUBLE
:
430 case GLSL_TYPE_ARRAY
:
431 dest
->row_major
= src
->row_major
;
432 dest
->stride
= src
->stride
;
433 dest
->array_element
= src
->array_element
;
436 case GLSL_TYPE_STRUCT
: {
437 unsigned elems
= glsl_get_length(src
->type
);
439 dest
->members
= ralloc_array(b
, struct vtn_type
*, elems
);
440 memcpy(dest
->members
, src
->members
, elems
* sizeof(struct vtn_type
*));
442 dest
->offsets
= ralloc_array(b
, unsigned, elems
);
443 memcpy(dest
->offsets
, src
->offsets
, elems
* sizeof(unsigned));
448 unreachable("unhandled type");
455 static struct vtn_type
*
456 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
458 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
459 type
= type
->members
[member
];
461 /* We may have an array of matrices.... Oh, joy! */
462 while (glsl_type_is_array(type
->type
)) {
463 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
464 type
= type
->array_element
;
467 assert(glsl_type_is_matrix(type
->type
));
473 struct_member_decoration_cb(struct vtn_builder
*b
,
474 struct vtn_value
*val
, int member
,
475 const struct vtn_decoration
*dec
, void *void_ctx
)
477 struct member_decoration_ctx
*ctx
= void_ctx
;
482 assert(member
< ctx
->num_fields
);
484 switch (dec
->decoration
) {
485 case SpvDecorationNonWritable
:
486 case SpvDecorationNonReadable
:
487 case SpvDecorationRelaxedPrecision
:
488 case SpvDecorationVolatile
:
489 case SpvDecorationCoherent
:
490 case SpvDecorationUniform
:
491 break; /* FIXME: Do nothing with this for now. */
492 case SpvDecorationNoPerspective
:
493 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
495 case SpvDecorationFlat
:
496 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
498 case SpvDecorationCentroid
:
499 ctx
->fields
[member
].centroid
= true;
501 case SpvDecorationSample
:
502 ctx
->fields
[member
].sample
= true;
504 case SpvDecorationStream
:
505 /* Vulkan only allows one GS stream */
506 assert(dec
->literals
[0] == 0);
508 case SpvDecorationLocation
:
509 ctx
->fields
[member
].location
= dec
->literals
[0];
511 case SpvDecorationComponent
:
512 break; /* FIXME: What should we do with these? */
513 case SpvDecorationBuiltIn
:
514 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
515 ctx
->type
->members
[member
]->is_builtin
= true;
516 ctx
->type
->members
[member
]->builtin
= dec
->literals
[0];
517 ctx
->type
->builtin_block
= true;
519 case SpvDecorationOffset
:
520 ctx
->type
->offsets
[member
] = dec
->literals
[0];
522 case SpvDecorationMatrixStride
:
523 mutable_matrix_member(b
, ctx
->type
, member
)->stride
= dec
->literals
[0];
525 case SpvDecorationColMajor
:
526 break; /* Nothing to do here. Column-major is the default. */
527 case SpvDecorationRowMajor
:
528 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
531 case SpvDecorationPatch
:
534 case SpvDecorationSpecId
:
535 case SpvDecorationBlock
:
536 case SpvDecorationBufferBlock
:
537 case SpvDecorationArrayStride
:
538 case SpvDecorationGLSLShared
:
539 case SpvDecorationGLSLPacked
:
540 case SpvDecorationInvariant
:
541 case SpvDecorationRestrict
:
542 case SpvDecorationAliased
:
543 case SpvDecorationConstant
:
544 case SpvDecorationIndex
:
545 case SpvDecorationBinding
:
546 case SpvDecorationDescriptorSet
:
547 case SpvDecorationLinkageAttributes
:
548 case SpvDecorationNoContraction
:
549 case SpvDecorationInputAttachmentIndex
:
550 vtn_warn("Decoration not allowed on struct members: %s",
551 spirv_decoration_to_string(dec
->decoration
));
554 case SpvDecorationXfbBuffer
:
555 case SpvDecorationXfbStride
:
556 vtn_warn("Vulkan does not have transform feedback");
559 case SpvDecorationCPacked
:
560 case SpvDecorationSaturatedConversion
:
561 case SpvDecorationFuncParamAttr
:
562 case SpvDecorationFPRoundingMode
:
563 case SpvDecorationFPFastMathMode
:
564 case SpvDecorationAlignment
:
565 vtn_warn("Decoration only allowed for CL-style kernels: %s",
566 spirv_decoration_to_string(dec
->decoration
));
570 unreachable("Unhandled decoration");
575 type_decoration_cb(struct vtn_builder
*b
,
576 struct vtn_value
*val
, int member
,
577 const struct vtn_decoration
*dec
, void *ctx
)
579 struct vtn_type
*type
= val
->type
;
584 switch (dec
->decoration
) {
585 case SpvDecorationArrayStride
:
586 type
->stride
= dec
->literals
[0];
588 case SpvDecorationBlock
:
591 case SpvDecorationBufferBlock
:
592 type
->buffer_block
= true;
594 case SpvDecorationGLSLShared
:
595 case SpvDecorationGLSLPacked
:
596 /* Ignore these, since we get explicit offsets anyways */
599 case SpvDecorationRowMajor
:
600 case SpvDecorationColMajor
:
601 case SpvDecorationMatrixStride
:
602 case SpvDecorationBuiltIn
:
603 case SpvDecorationNoPerspective
:
604 case SpvDecorationFlat
:
605 case SpvDecorationPatch
:
606 case SpvDecorationCentroid
:
607 case SpvDecorationSample
:
608 case SpvDecorationVolatile
:
609 case SpvDecorationCoherent
:
610 case SpvDecorationNonWritable
:
611 case SpvDecorationNonReadable
:
612 case SpvDecorationUniform
:
613 case SpvDecorationStream
:
614 case SpvDecorationLocation
:
615 case SpvDecorationComponent
:
616 case SpvDecorationOffset
:
617 case SpvDecorationXfbBuffer
:
618 case SpvDecorationXfbStride
:
619 vtn_warn("Decoration only allowed for struct members: %s",
620 spirv_decoration_to_string(dec
->decoration
));
623 case SpvDecorationRelaxedPrecision
:
624 case SpvDecorationSpecId
:
625 case SpvDecorationInvariant
:
626 case SpvDecorationRestrict
:
627 case SpvDecorationAliased
:
628 case SpvDecorationConstant
:
629 case SpvDecorationIndex
:
630 case SpvDecorationBinding
:
631 case SpvDecorationDescriptorSet
:
632 case SpvDecorationLinkageAttributes
:
633 case SpvDecorationNoContraction
:
634 case SpvDecorationInputAttachmentIndex
:
635 vtn_warn("Decoration not allowed on types: %s",
636 spirv_decoration_to_string(dec
->decoration
));
639 case SpvDecorationCPacked
:
640 case SpvDecorationSaturatedConversion
:
641 case SpvDecorationFuncParamAttr
:
642 case SpvDecorationFPRoundingMode
:
643 case SpvDecorationFPFastMathMode
:
644 case SpvDecorationAlignment
:
645 vtn_warn("Decoration only allowed for CL-style kernels: %s",
646 spirv_decoration_to_string(dec
->decoration
));
650 unreachable("Unhandled decoration");
655 translate_image_format(SpvImageFormat format
)
658 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
659 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
660 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
661 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
662 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
663 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
664 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
665 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
666 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
667 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
668 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
669 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
670 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
671 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
672 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
673 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
674 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
675 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
676 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
677 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
678 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
679 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
680 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
681 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
682 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
683 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
684 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
685 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
686 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
687 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
688 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
689 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
690 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
691 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
692 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
693 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
694 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
695 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
696 case SpvImageFormatR16ui
: return 0x823A; /* GL_RG16UI */
697 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
699 assert(!"Invalid image format");
705 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
706 const uint32_t *w
, unsigned count
)
708 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
710 val
->type
= rzalloc(b
, struct vtn_type
);
711 val
->type
->is_builtin
= false;
712 val
->type
->val
= val
;
716 val
->type
->type
= glsl_void_type();
719 val
->type
->type
= glsl_bool_type();
723 const bool signedness
= w
[3];
725 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
727 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
730 case SpvOpTypeFloat
: {
732 val
->type
->type
= bit_size
== 64 ? glsl_double_type() : glsl_float_type();
736 case SpvOpTypeVector
: {
737 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
738 unsigned elems
= w
[3];
740 assert(glsl_type_is_scalar(base
->type
));
741 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
743 /* Vectors implicitly have sizeof(base_type) stride. For now, this
744 * is always 4 bytes. This will have to change if we want to start
745 * supporting doubles or half-floats.
747 val
->type
->stride
= 4;
748 val
->type
->array_element
= base
;
752 case SpvOpTypeMatrix
: {
753 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
754 unsigned columns
= w
[3];
756 assert(glsl_type_is_vector(base
->type
));
757 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
758 glsl_get_vector_elements(base
->type
),
760 assert(!glsl_type_is_error(val
->type
->type
));
761 val
->type
->array_element
= base
;
762 val
->type
->row_major
= false;
763 val
->type
->stride
= 0;
767 case SpvOpTypeRuntimeArray
:
768 case SpvOpTypeArray
: {
769 struct vtn_type
*array_element
=
770 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
773 if (opcode
== SpvOpTypeRuntimeArray
) {
774 /* A length of 0 is used to denote unsized arrays */
778 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
781 val
->type
->type
= glsl_array_type(array_element
->type
, length
);
782 val
->type
->array_element
= array_element
;
783 val
->type
->stride
= 0;
787 case SpvOpTypeStruct
: {
788 unsigned num_fields
= count
- 2;
789 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
790 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
792 NIR_VLA(struct glsl_struct_field
, fields
, count
);
793 for (unsigned i
= 0; i
< num_fields
; i
++) {
794 val
->type
->members
[i
] =
795 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
796 fields
[i
] = (struct glsl_struct_field
) {
797 .type
= val
->type
->members
[i
]->type
,
798 .name
= ralloc_asprintf(b
, "field%d", i
),
803 struct member_decoration_ctx ctx
= {
804 .num_fields
= num_fields
,
809 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
811 const char *name
= val
->name
? val
->name
: "struct";
813 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
);
817 case SpvOpTypeFunction
: {
818 const struct glsl_type
*return_type
=
819 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
820 NIR_VLA(struct glsl_function_param
, params
, count
- 3);
821 for (unsigned i
= 0; i
< count
- 3; i
++) {
822 params
[i
].type
= vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
->type
;
826 params
[i
].out
= true;
828 val
->type
->type
= glsl_function_type(return_type
, params
, count
- 3);
832 case SpvOpTypePointer
:
833 /* FIXME: For now, we'll just do the really lame thing and return
834 * the same type. The validator should ensure that the proper number
835 * of dereferences happen
837 val
->type
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
840 case SpvOpTypeImage
: {
841 const struct glsl_type
*sampled_type
=
842 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
844 assert(glsl_type_is_vector_or_scalar(sampled_type
));
846 enum glsl_sampler_dim dim
;
847 switch ((SpvDim
)w
[3]) {
848 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
849 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
850 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
851 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
852 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
853 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
854 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
856 unreachable("Invalid SPIR-V Sampler dimension");
859 bool is_shadow
= w
[4];
860 bool is_array
= w
[5];
861 bool multisampled
= w
[6];
862 unsigned sampled
= w
[7];
863 SpvImageFormat format
= w
[8];
866 val
->type
->access_qualifier
= w
[9];
868 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
871 if (dim
== GLSL_SAMPLER_DIM_2D
)
872 dim
= GLSL_SAMPLER_DIM_MS
;
873 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
874 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
876 assert(!"Unsupported multisampled image type");
879 val
->type
->image_format
= translate_image_format(format
);
882 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
883 glsl_get_base_type(sampled_type
));
884 } else if (sampled
== 2) {
886 val
->type
->type
= glsl_image_type(dim
, is_array
,
887 glsl_get_base_type(sampled_type
));
889 assert(!"We need to know if the image will be sampled");
894 case SpvOpTypeSampledImage
:
895 val
->type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
898 case SpvOpTypeSampler
:
899 /* The actual sampler type here doesn't really matter. It gets
900 * thrown away the moment you combine it with an image. What really
901 * matters is that it's a sampler type as opposed to an integer type
902 * so the backend knows what to do.
904 val
->type
->type
= glsl_bare_sampler_type();
907 case SpvOpTypeOpaque
:
909 case SpvOpTypeDeviceEvent
:
910 case SpvOpTypeReserveId
:
914 unreachable("Unhandled opcode");
917 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
920 static nir_constant
*
921 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
923 nir_constant
*c
= rzalloc(b
, nir_constant
);
925 switch (glsl_get_base_type(type
)) {
928 case GLSL_TYPE_INT64
:
929 case GLSL_TYPE_UINT64
:
931 case GLSL_TYPE_FLOAT
:
932 case GLSL_TYPE_DOUBLE
:
933 /* Nothing to do here. It's already initialized to zero */
936 case GLSL_TYPE_ARRAY
:
937 assert(glsl_get_length(type
) > 0);
938 c
->num_elements
= glsl_get_length(type
);
939 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
941 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
942 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
943 c
->elements
[i
] = c
->elements
[0];
946 case GLSL_TYPE_STRUCT
:
947 c
->num_elements
= glsl_get_length(type
);
948 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
950 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
951 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
956 unreachable("Invalid type for null constant");
963 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
964 int member
, const struct vtn_decoration
*dec
,
967 assert(member
== -1);
968 if (dec
->decoration
!= SpvDecorationSpecId
)
971 struct spec_constant_value
*const_value
= data
;
973 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
974 if (b
->specializations
[i
].id
== dec
->literals
[0]) {
975 if (const_value
->is_double
)
976 const_value
->data64
= b
->specializations
[i
].data64
;
978 const_value
->data32
= b
->specializations
[i
].data32
;
985 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
986 uint32_t const_value
)
988 struct spec_constant_value data
;
989 data
.is_double
= false;
990 data
.data32
= const_value
;
991 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
996 get_specialization64(struct vtn_builder
*b
, struct vtn_value
*val
,
997 uint64_t const_value
)
999 struct spec_constant_value data
;
1000 data
.is_double
= true;
1001 data
.data64
= const_value
;
1002 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1007 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1008 struct vtn_value
*val
,
1010 const struct vtn_decoration
*dec
,
1013 assert(member
== -1);
1014 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1015 dec
->literals
[0] != SpvBuiltInWorkgroupSize
)
1018 assert(val
->const_type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1020 b
->shader
->info
.cs
.local_size
[0] = val
->constant
->values
[0].u32
[0];
1021 b
->shader
->info
.cs
.local_size
[1] = val
->constant
->values
[0].u32
[1];
1022 b
->shader
->info
.cs
.local_size
[2] = val
->constant
->values
[0].u32
[2];
1026 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1027 const uint32_t *w
, unsigned count
)
1029 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1030 val
->const_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
1031 val
->constant
= rzalloc(b
, nir_constant
);
1033 case SpvOpConstantTrue
:
1034 assert(val
->const_type
== glsl_bool_type());
1035 val
->constant
->values
[0].u32
[0] = NIR_TRUE
;
1037 case SpvOpConstantFalse
:
1038 assert(val
->const_type
== glsl_bool_type());
1039 val
->constant
->values
[0].u32
[0] = NIR_FALSE
;
1042 case SpvOpSpecConstantTrue
:
1043 case SpvOpSpecConstantFalse
: {
1044 assert(val
->const_type
== glsl_bool_type());
1046 get_specialization(b
, val
, (opcode
== SpvOpSpecConstantTrue
));
1047 val
->constant
->values
[0].u32
[0] = int_val
? NIR_TRUE
: NIR_FALSE
;
1051 case SpvOpConstant
: {
1052 assert(glsl_type_is_scalar(val
->const_type
));
1053 int bit_size
= glsl_get_bit_size(val
->const_type
);
1054 if (bit_size
== 64) {
1055 val
->constant
->values
->u32
[0] = w
[3];
1056 val
->constant
->values
->u32
[1] = w
[4];
1058 assert(bit_size
== 32);
1059 val
->constant
->values
->u32
[0] = w
[3];
1063 case SpvOpSpecConstant
: {
1064 assert(glsl_type_is_scalar(val
->const_type
));
1065 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1066 int bit_size
= glsl_get_bit_size(val
->const_type
);
1068 val
->constant
->values
[0].u64
[0] =
1069 get_specialization64(b
, val
, vtn_u64_literal(&w
[3]));
1071 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1074 case SpvOpSpecConstantComposite
:
1075 case SpvOpConstantComposite
: {
1076 unsigned elem_count
= count
- 3;
1077 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1078 for (unsigned i
= 0; i
< elem_count
; i
++)
1079 elems
[i
] = vtn_value(b
, w
[i
+ 3], vtn_value_type_constant
)->constant
;
1081 switch (glsl_get_base_type(val
->const_type
)) {
1082 case GLSL_TYPE_UINT
:
1084 case GLSL_TYPE_UINT64
:
1085 case GLSL_TYPE_INT64
:
1086 case GLSL_TYPE_FLOAT
:
1087 case GLSL_TYPE_BOOL
:
1088 case GLSL_TYPE_DOUBLE
: {
1089 int bit_size
= glsl_get_bit_size(val
->const_type
);
1090 if (glsl_type_is_matrix(val
->const_type
)) {
1091 assert(glsl_get_matrix_columns(val
->const_type
) == elem_count
);
1092 for (unsigned i
= 0; i
< elem_count
; i
++)
1093 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1095 assert(glsl_type_is_vector(val
->const_type
));
1096 assert(glsl_get_vector_elements(val
->const_type
) == elem_count
);
1097 for (unsigned i
= 0; i
< elem_count
; i
++) {
1098 if (bit_size
== 64) {
1099 val
->constant
->values
[0].u64
[i
] = elems
[i
]->values
[0].u64
[0];
1101 assert(bit_size
== 32);
1102 val
->constant
->values
[0].u32
[i
] = elems
[i
]->values
[0].u32
[0];
1109 case GLSL_TYPE_STRUCT
:
1110 case GLSL_TYPE_ARRAY
:
1111 ralloc_steal(val
->constant
, elems
);
1112 val
->constant
->num_elements
= elem_count
;
1113 val
->constant
->elements
= elems
;
1117 unreachable("Unsupported type for constants");
1122 case SpvOpSpecConstantOp
: {
1123 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1125 case SpvOpVectorShuffle
: {
1126 struct vtn_value
*v0
= &b
->values
[w
[4]];
1127 struct vtn_value
*v1
= &b
->values
[w
[5]];
1129 assert(v0
->value_type
== vtn_value_type_constant
||
1130 v0
->value_type
== vtn_value_type_undef
);
1131 assert(v1
->value_type
== vtn_value_type_constant
||
1132 v1
->value_type
== vtn_value_type_undef
);
1134 unsigned len0
= v0
->value_type
== vtn_value_type_constant
?
1135 glsl_get_vector_elements(v0
->const_type
) :
1136 glsl_get_vector_elements(v0
->type
->type
);
1137 unsigned len1
= v1
->value_type
== vtn_value_type_constant
?
1138 glsl_get_vector_elements(v1
->const_type
) :
1139 glsl_get_vector_elements(v1
->type
->type
);
1141 assert(len0
+ len1
< 16);
1143 unsigned bit_size
= glsl_get_bit_size(val
->const_type
);
1144 unsigned bit_size0
= v0
->value_type
== vtn_value_type_constant
?
1145 glsl_get_bit_size(v0
->const_type
) :
1146 glsl_get_bit_size(v0
->type
->type
);
1147 unsigned bit_size1
= v1
->value_type
== vtn_value_type_constant
?
1148 glsl_get_bit_size(v1
->const_type
) :
1149 glsl_get_bit_size(v1
->type
->type
);
1151 assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1152 (void)bit_size0
; (void)bit_size1
;
1154 if (bit_size
== 64) {
1156 if (v0
->value_type
== vtn_value_type_constant
) {
1157 for (unsigned i
= 0; i
< len0
; i
++)
1158 u64
[i
] = v0
->constant
->values
[0].u64
[i
];
1160 if (v1
->value_type
== vtn_value_type_constant
) {
1161 for (unsigned i
= 0; i
< len1
; i
++)
1162 u64
[len0
+ i
] = v1
->constant
->values
[0].u64
[i
];
1165 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1166 uint32_t comp
= w
[i
+ 6];
1167 /* If component is not used, set the value to a known constant
1168 * to detect if it is wrongly used.
1170 if (comp
== (uint32_t)-1)
1171 val
->constant
->values
[0].u64
[j
] = 0xdeadbeefdeadbeef;
1173 val
->constant
->values
[0].u64
[j
] = u64
[comp
];
1177 if (v0
->value_type
== vtn_value_type_constant
) {
1178 for (unsigned i
= 0; i
< len0
; i
++)
1179 u32
[i
] = v0
->constant
->values
[0].u32
[i
];
1181 if (v1
->value_type
== vtn_value_type_constant
) {
1182 for (unsigned i
= 0; i
< len1
; i
++)
1183 u32
[len0
+ i
] = v1
->constant
->values
[0].u32
[i
];
1186 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1187 uint32_t comp
= w
[i
+ 6];
1188 /* If component is not used, set the value to a known constant
1189 * to detect if it is wrongly used.
1191 if (comp
== (uint32_t)-1)
1192 val
->constant
->values
[0].u32
[j
] = 0xdeadbeef;
1194 val
->constant
->values
[0].u32
[j
] = u32
[comp
];
1200 case SpvOpCompositeExtract
:
1201 case SpvOpCompositeInsert
: {
1202 struct vtn_value
*comp
;
1203 unsigned deref_start
;
1204 struct nir_constant
**c
;
1205 if (opcode
== SpvOpCompositeExtract
) {
1206 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1208 c
= &comp
->constant
;
1210 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1212 val
->constant
= nir_constant_clone(comp
->constant
,
1219 const struct glsl_type
*type
= comp
->const_type
;
1220 for (unsigned i
= deref_start
; i
< count
; i
++) {
1221 switch (glsl_get_base_type(type
)) {
1222 case GLSL_TYPE_UINT
:
1224 case GLSL_TYPE_UINT64
:
1225 case GLSL_TYPE_INT64
:
1226 case GLSL_TYPE_FLOAT
:
1227 case GLSL_TYPE_DOUBLE
:
1228 case GLSL_TYPE_BOOL
:
1229 /* If we hit this granularity, we're picking off an element */
1230 if (glsl_type_is_matrix(type
)) {
1231 assert(col
== 0 && elem
== -1);
1234 type
= glsl_get_column_type(type
);
1236 assert(elem
<= 0 && glsl_type_is_vector(type
));
1238 type
= glsl_scalar_type(glsl_get_base_type(type
));
1242 case GLSL_TYPE_ARRAY
:
1243 c
= &(*c
)->elements
[w
[i
]];
1244 type
= glsl_get_array_element(type
);
1247 case GLSL_TYPE_STRUCT
:
1248 c
= &(*c
)->elements
[w
[i
]];
1249 type
= glsl_get_struct_field(type
, w
[i
]);
1253 unreachable("Invalid constant type");
1257 if (opcode
== SpvOpCompositeExtract
) {
1261 unsigned num_components
= glsl_get_vector_elements(type
);
1262 unsigned bit_size
= glsl_get_bit_size(type
);
1263 for (unsigned i
= 0; i
< num_components
; i
++)
1264 if (bit_size
== 64) {
1265 val
->constant
->values
[0].u64
[i
] = (*c
)->values
[col
].u64
[elem
+ i
];
1267 assert(bit_size
== 32);
1268 val
->constant
->values
[0].u32
[i
] = (*c
)->values
[col
].u32
[elem
+ i
];
1272 struct vtn_value
*insert
=
1273 vtn_value(b
, w
[4], vtn_value_type_constant
);
1274 assert(insert
->const_type
== type
);
1276 *c
= insert
->constant
;
1278 unsigned num_components
= glsl_get_vector_elements(type
);
1279 unsigned bit_size
= glsl_get_bit_size(type
);
1280 for (unsigned i
= 0; i
< num_components
; i
++)
1281 if (bit_size
== 64) {
1282 (*c
)->values
[col
].u64
[elem
+ i
] = insert
->constant
->values
[0].u64
[i
];
1284 assert(bit_size
== 32);
1285 (*c
)->values
[col
].u32
[elem
+ i
] = insert
->constant
->values
[0].u32
[i
];
1294 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->const_type
);
1295 nir_alu_type src_alu_type
= dst_alu_type
;
1296 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(opcode
, &swap
, src_alu_type
, dst_alu_type
);
1298 unsigned num_components
= glsl_get_vector_elements(val
->const_type
);
1300 glsl_get_bit_size(val
->const_type
);
1302 nir_const_value src
[4];
1304 for (unsigned i
= 0; i
< count
- 4; i
++) {
1306 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
)->constant
;
1308 unsigned j
= swap
? 1 - i
: i
;
1309 assert(bit_size
== 32);
1310 src
[j
] = c
->values
[0];
1313 val
->constant
->values
[0] =
1314 nir_eval_const_opcode(op
, num_components
, bit_size
, src
);
1321 case SpvOpConstantNull
:
1322 val
->constant
= vtn_null_constant(b
, val
->const_type
);
1325 case SpvOpConstantSampler
:
1326 assert(!"OpConstantSampler requires Kernel Capability");
1330 unreachable("Unhandled opcode");
1333 /* Now that we have the value, update the workgroup size if needed */
1334 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1338 vtn_handle_function_call(struct vtn_builder
*b
, SpvOp opcode
,
1339 const uint32_t *w
, unsigned count
)
1341 struct nir_function
*callee
=
1342 vtn_value(b
, w
[3], vtn_value_type_function
)->func
->impl
->function
;
1344 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, callee
);
1345 for (unsigned i
= 0; i
< call
->num_params
; i
++) {
1346 unsigned arg_id
= w
[4 + i
];
1347 struct vtn_value
*arg
= vtn_untyped_value(b
, arg_id
);
1348 if (arg
->value_type
== vtn_value_type_pointer
) {
1349 nir_deref_var
*d
= vtn_pointer_to_deref(b
, arg
->pointer
);
1350 call
->params
[i
] = nir_deref_var_clone(d
, call
);
1352 struct vtn_ssa_value
*arg_ssa
= vtn_ssa_value(b
, arg_id
);
1354 /* Make a temporary to store the argument in */
1356 nir_local_variable_create(b
->impl
, arg_ssa
->type
, "arg_tmp");
1357 call
->params
[i
] = nir_deref_var_create(call
, tmp
);
1359 vtn_local_store(b
, arg_ssa
, call
->params
[i
]);
1363 nir_variable
*out_tmp
= NULL
;
1364 if (!glsl_type_is_void(callee
->return_type
)) {
1365 out_tmp
= nir_local_variable_create(b
->impl
, callee
->return_type
,
1367 call
->return_deref
= nir_deref_var_create(call
, out_tmp
);
1370 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
1372 if (glsl_type_is_void(callee
->return_type
)) {
1373 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1375 struct vtn_value
*retval
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1376 retval
->ssa
= vtn_local_load(b
, call
->return_deref
);
1380 struct vtn_ssa_value
*
1381 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1383 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1386 if (!glsl_type_is_vector_or_scalar(type
)) {
1387 unsigned elems
= glsl_get_length(type
);
1388 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1389 for (unsigned i
= 0; i
< elems
; i
++) {
1390 const struct glsl_type
*child_type
;
1392 switch (glsl_get_base_type(type
)) {
1394 case GLSL_TYPE_UINT
:
1395 case GLSL_TYPE_INT64
:
1396 case GLSL_TYPE_UINT64
:
1397 case GLSL_TYPE_BOOL
:
1398 case GLSL_TYPE_FLOAT
:
1399 case GLSL_TYPE_DOUBLE
:
1400 child_type
= glsl_get_column_type(type
);
1402 case GLSL_TYPE_ARRAY
:
1403 child_type
= glsl_get_array_element(type
);
1405 case GLSL_TYPE_STRUCT
:
1406 child_type
= glsl_get_struct_field(type
, i
);
1409 unreachable("unkown base type");
1412 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
1420 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
1423 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
1424 src
.src_type
= type
;
1429 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
1430 const uint32_t *w
, unsigned count
)
1432 if (opcode
== SpvOpSampledImage
) {
1433 struct vtn_value
*val
=
1434 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1435 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1436 val
->sampled_image
->image
=
1437 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1438 val
->sampled_image
->sampler
=
1439 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
1441 } else if (opcode
== SpvOpImage
) {
1442 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1443 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
1444 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
1445 val
->pointer
= src_val
->sampled_image
->image
;
1447 assert(src_val
->value_type
== vtn_value_type_pointer
);
1448 val
->pointer
= src_val
->pointer
;
1453 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1454 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1456 struct vtn_sampled_image sampled
;
1457 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
1458 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
1459 sampled
= *sampled_val
->sampled_image
;
1461 assert(sampled_val
->value_type
== vtn_value_type_pointer
);
1462 sampled
.image
= NULL
;
1463 sampled
.sampler
= sampled_val
->pointer
;
1466 const struct glsl_type
*image_type
;
1467 if (sampled
.image
) {
1468 image_type
= sampled
.image
->var
->var
->interface_type
;
1470 image_type
= sampled
.sampler
->var
->var
->interface_type
;
1472 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
1473 const bool is_array
= glsl_sampler_type_is_array(image_type
);
1474 const bool is_shadow
= glsl_sampler_type_is_shadow(image_type
);
1476 /* Figure out the base texture operation */
1479 case SpvOpImageSampleImplicitLod
:
1480 case SpvOpImageSampleDrefImplicitLod
:
1481 case SpvOpImageSampleProjImplicitLod
:
1482 case SpvOpImageSampleProjDrefImplicitLod
:
1483 texop
= nir_texop_tex
;
1486 case SpvOpImageSampleExplicitLod
:
1487 case SpvOpImageSampleDrefExplicitLod
:
1488 case SpvOpImageSampleProjExplicitLod
:
1489 case SpvOpImageSampleProjDrefExplicitLod
:
1490 texop
= nir_texop_txl
;
1493 case SpvOpImageFetch
:
1494 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
1495 texop
= nir_texop_txf_ms
;
1497 texop
= nir_texop_txf
;
1501 case SpvOpImageGather
:
1502 case SpvOpImageDrefGather
:
1503 texop
= nir_texop_tg4
;
1506 case SpvOpImageQuerySizeLod
:
1507 case SpvOpImageQuerySize
:
1508 texop
= nir_texop_txs
;
1511 case SpvOpImageQueryLod
:
1512 texop
= nir_texop_lod
;
1515 case SpvOpImageQueryLevels
:
1516 texop
= nir_texop_query_levels
;
1519 case SpvOpImageQuerySamples
:
1520 texop
= nir_texop_texture_samples
;
1524 unreachable("Unhandled opcode");
1527 nir_tex_src srcs
[8]; /* 8 should be enough */
1528 nir_tex_src
*p
= srcs
;
1532 struct nir_ssa_def
*coord
;
1533 unsigned coord_components
;
1535 case SpvOpImageSampleImplicitLod
:
1536 case SpvOpImageSampleExplicitLod
:
1537 case SpvOpImageSampleDrefImplicitLod
:
1538 case SpvOpImageSampleDrefExplicitLod
:
1539 case SpvOpImageSampleProjImplicitLod
:
1540 case SpvOpImageSampleProjExplicitLod
:
1541 case SpvOpImageSampleProjDrefImplicitLod
:
1542 case SpvOpImageSampleProjDrefExplicitLod
:
1543 case SpvOpImageFetch
:
1544 case SpvOpImageGather
:
1545 case SpvOpImageDrefGather
:
1546 case SpvOpImageQueryLod
: {
1547 /* All these types have the coordinate as their first real argument */
1548 switch (sampler_dim
) {
1549 case GLSL_SAMPLER_DIM_1D
:
1550 case GLSL_SAMPLER_DIM_BUF
:
1551 coord_components
= 1;
1553 case GLSL_SAMPLER_DIM_2D
:
1554 case GLSL_SAMPLER_DIM_RECT
:
1555 case GLSL_SAMPLER_DIM_MS
:
1556 coord_components
= 2;
1558 case GLSL_SAMPLER_DIM_3D
:
1559 case GLSL_SAMPLER_DIM_CUBE
:
1560 coord_components
= 3;
1563 unreachable("Invalid sampler type");
1566 if (is_array
&& texop
!= nir_texop_lod
)
1569 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
1570 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
1571 (1 << coord_components
) - 1));
1572 p
->src_type
= nir_tex_src_coord
;
1579 coord_components
= 0;
1584 case SpvOpImageSampleProjImplicitLod
:
1585 case SpvOpImageSampleProjExplicitLod
:
1586 case SpvOpImageSampleProjDrefImplicitLod
:
1587 case SpvOpImageSampleProjDrefExplicitLod
:
1588 /* These have the projector as the last coordinate component */
1589 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
1590 p
->src_type
= nir_tex_src_projector
;
1598 unsigned gather_component
= 0;
1600 case SpvOpImageSampleDrefImplicitLod
:
1601 case SpvOpImageSampleDrefExplicitLod
:
1602 case SpvOpImageSampleProjDrefImplicitLod
:
1603 case SpvOpImageSampleProjDrefExplicitLod
:
1604 case SpvOpImageDrefGather
:
1605 /* These all have an explicit depth value as their next source */
1606 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
1609 case SpvOpImageGather
:
1610 /* This has a component as its next source */
1612 vtn_value(b
, w
[idx
++], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
1619 /* For OpImageQuerySizeLod, we always have an LOD */
1620 if (opcode
== SpvOpImageQuerySizeLod
)
1621 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1623 /* Now we need to handle some number of optional arguments */
1624 const struct vtn_ssa_value
*gather_offsets
= NULL
;
1626 uint32_t operands
= w
[idx
++];
1628 if (operands
& SpvImageOperandsBiasMask
) {
1629 assert(texop
== nir_texop_tex
);
1630 texop
= nir_texop_txb
;
1631 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
1634 if (operands
& SpvImageOperandsLodMask
) {
1635 assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
1636 texop
== nir_texop_txs
);
1637 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1640 if (operands
& SpvImageOperandsGradMask
) {
1641 assert(texop
== nir_texop_txl
);
1642 texop
= nir_texop_txd
;
1643 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
1644 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
1647 if (operands
& SpvImageOperandsOffsetMask
||
1648 operands
& SpvImageOperandsConstOffsetMask
)
1649 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
1651 if (operands
& SpvImageOperandsConstOffsetsMask
) {
1652 gather_offsets
= vtn_ssa_value(b
, w
[idx
++]);
1653 (*p
++) = (nir_tex_src
){};
1656 if (operands
& SpvImageOperandsSampleMask
) {
1657 assert(texop
== nir_texop_txf_ms
);
1658 texop
= nir_texop_txf_ms
;
1659 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
1662 /* We should have now consumed exactly all of the arguments */
1663 assert(idx
== count
);
1665 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
1668 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1670 instr
->coord_components
= coord_components
;
1671 instr
->sampler_dim
= sampler_dim
;
1672 instr
->is_array
= is_array
;
1673 instr
->is_shadow
= is_shadow
;
1674 instr
->is_new_style_shadow
=
1675 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
1676 instr
->component
= gather_component
;
1678 switch (glsl_get_sampler_result_type(image_type
)) {
1679 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
1680 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
1681 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
1682 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
1684 unreachable("Invalid base type for sampler result");
1687 nir_deref_var
*sampler
= vtn_pointer_to_deref(b
, sampled
.sampler
);
1688 nir_deref_var
*texture
;
1689 if (sampled
.image
) {
1690 nir_deref_var
*image
= vtn_pointer_to_deref(b
, sampled
.image
);
1696 instr
->texture
= nir_deref_var_clone(texture
, instr
);
1698 switch (instr
->op
) {
1703 /* These operations require a sampler */
1704 instr
->sampler
= nir_deref_var_clone(sampler
, instr
);
1707 case nir_texop_txf_ms
:
1711 case nir_texop_query_levels
:
1712 case nir_texop_texture_samples
:
1713 case nir_texop_samples_identical
:
1715 instr
->sampler
= NULL
;
1717 case nir_texop_txf_ms_mcs
:
1718 unreachable("unexpected nir_texop_txf_ms_mcs");
1721 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1722 nir_tex_instr_dest_size(instr
), 32, NULL
);
1724 assert(glsl_get_vector_elements(ret_type
->type
) ==
1725 nir_tex_instr_dest_size(instr
));
1728 nir_instr
*instruction
;
1729 if (gather_offsets
) {
1730 assert(glsl_get_base_type(gather_offsets
->type
) == GLSL_TYPE_ARRAY
);
1731 assert(glsl_get_length(gather_offsets
->type
) == 4);
1732 nir_tex_instr
*instrs
[4] = {instr
, NULL
, NULL
, NULL
};
1734 /* Copy the current instruction 4x */
1735 for (uint32_t i
= 1; i
< 4; i
++) {
1736 instrs
[i
] = nir_tex_instr_create(b
->shader
, instr
->num_srcs
);
1737 instrs
[i
]->op
= instr
->op
;
1738 instrs
[i
]->coord_components
= instr
->coord_components
;
1739 instrs
[i
]->sampler_dim
= instr
->sampler_dim
;
1740 instrs
[i
]->is_array
= instr
->is_array
;
1741 instrs
[i
]->is_shadow
= instr
->is_shadow
;
1742 instrs
[i
]->is_new_style_shadow
= instr
->is_new_style_shadow
;
1743 instrs
[i
]->component
= instr
->component
;
1744 instrs
[i
]->dest_type
= instr
->dest_type
;
1745 instrs
[i
]->texture
= nir_deref_var_clone(texture
, instrs
[i
]);
1746 instrs
[i
]->sampler
= NULL
;
1748 memcpy(instrs
[i
]->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1750 nir_ssa_dest_init(&instrs
[i
]->instr
, &instrs
[i
]->dest
,
1751 nir_tex_instr_dest_size(instr
), 32, NULL
);
1754 /* Fill in the last argument with the offset from the passed in offsets
1755 * and insert the instruction into the stream.
1757 for (uint32_t i
= 0; i
< 4; i
++) {
1759 src
.src
= nir_src_for_ssa(gather_offsets
->elems
[i
]->def
);
1760 src
.src_type
= nir_tex_src_offset
;
1761 instrs
[i
]->src
[instrs
[i
]->num_srcs
- 1] = src
;
1762 nir_builder_instr_insert(&b
->nb
, &instrs
[i
]->instr
);
1765 /* Combine the results of the 4 instructions by taking their .w
1768 nir_alu_instr
*vec4
= nir_alu_instr_create(b
->shader
, nir_op_vec4
);
1769 nir_ssa_dest_init(&vec4
->instr
, &vec4
->dest
.dest
, 4, 32, NULL
);
1770 vec4
->dest
.write_mask
= 0xf;
1771 for (uint32_t i
= 0; i
< 4; i
++) {
1772 vec4
->src
[i
].src
= nir_src_for_ssa(&instrs
[i
]->dest
.ssa
);
1773 vec4
->src
[i
].swizzle
[0] = 3;
1775 def
= &vec4
->dest
.dest
.ssa
;
1776 instruction
= &vec4
->instr
;
1778 def
= &instr
->dest
.ssa
;
1779 instruction
= &instr
->instr
;
1782 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
1783 val
->ssa
->def
= def
;
1785 nir_builder_instr_insert(&b
->nb
, instruction
);
1789 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
1790 const uint32_t *w
, nir_src
*src
)
1793 case SpvOpAtomicIIncrement
:
1794 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
1797 case SpvOpAtomicIDecrement
:
1798 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
1801 case SpvOpAtomicISub
:
1803 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
1806 case SpvOpAtomicCompareExchange
:
1807 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
1808 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
1811 case SpvOpAtomicExchange
:
1812 case SpvOpAtomicIAdd
:
1813 case SpvOpAtomicSMin
:
1814 case SpvOpAtomicUMin
:
1815 case SpvOpAtomicSMax
:
1816 case SpvOpAtomicUMax
:
1817 case SpvOpAtomicAnd
:
1819 case SpvOpAtomicXor
:
1820 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1824 unreachable("Invalid SPIR-V atomic");
1828 static nir_ssa_def
*
1829 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
1831 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
1833 /* The image_load_store intrinsics assume a 4-dim coordinate */
1834 unsigned dim
= glsl_get_vector_elements(coord
->type
);
1835 unsigned swizzle
[4];
1836 for (unsigned i
= 0; i
< 4; i
++)
1837 swizzle
[i
] = MIN2(i
, dim
- 1);
1839 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
1843 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
1844 const uint32_t *w
, unsigned count
)
1846 /* Just get this one out of the way */
1847 if (opcode
== SpvOpImageTexelPointer
) {
1848 struct vtn_value
*val
=
1849 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
1850 val
->image
= ralloc(b
, struct vtn_image_pointer
);
1852 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1853 val
->image
->coord
= get_image_coord(b
, w
[4]);
1854 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
1858 struct vtn_image_pointer image
;
1861 case SpvOpAtomicExchange
:
1862 case SpvOpAtomicCompareExchange
:
1863 case SpvOpAtomicCompareExchangeWeak
:
1864 case SpvOpAtomicIIncrement
:
1865 case SpvOpAtomicIDecrement
:
1866 case SpvOpAtomicIAdd
:
1867 case SpvOpAtomicISub
:
1868 case SpvOpAtomicLoad
:
1869 case SpvOpAtomicSMin
:
1870 case SpvOpAtomicUMin
:
1871 case SpvOpAtomicSMax
:
1872 case SpvOpAtomicUMax
:
1873 case SpvOpAtomicAnd
:
1875 case SpvOpAtomicXor
:
1876 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
1879 case SpvOpAtomicStore
:
1880 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
1883 case SpvOpImageQuerySize
:
1884 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1886 image
.sample
= NULL
;
1889 case SpvOpImageRead
:
1890 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1891 image
.coord
= get_image_coord(b
, w
[4]);
1893 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
1894 assert(w
[5] == SpvImageOperandsSampleMask
);
1895 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
1897 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1901 case SpvOpImageWrite
:
1902 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
1903 image
.coord
= get_image_coord(b
, w
[2]);
1907 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
1908 assert(w
[4] == SpvImageOperandsSampleMask
);
1909 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
1911 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1916 unreachable("Invalid image opcode");
1919 nir_intrinsic_op op
;
1921 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_##N; break;
1922 OP(ImageQuerySize
, size
)
1924 OP(ImageWrite
, store
)
1925 OP(AtomicLoad
, load
)
1926 OP(AtomicStore
, store
)
1927 OP(AtomicExchange
, atomic_exchange
)
1928 OP(AtomicCompareExchange
, atomic_comp_swap
)
1929 OP(AtomicIIncrement
, atomic_add
)
1930 OP(AtomicIDecrement
, atomic_add
)
1931 OP(AtomicIAdd
, atomic_add
)
1932 OP(AtomicISub
, atomic_add
)
1933 OP(AtomicSMin
, atomic_min
)
1934 OP(AtomicUMin
, atomic_min
)
1935 OP(AtomicSMax
, atomic_max
)
1936 OP(AtomicUMax
, atomic_max
)
1937 OP(AtomicAnd
, atomic_and
)
1938 OP(AtomicOr
, atomic_or
)
1939 OP(AtomicXor
, atomic_xor
)
1942 unreachable("Invalid image opcode");
1945 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
1947 nir_deref_var
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
1948 intrin
->variables
[0] = nir_deref_var_clone(image_deref
, intrin
);
1950 /* ImageQuerySize doesn't take any extra parameters */
1951 if (opcode
!= SpvOpImageQuerySize
) {
1952 /* The image coordinate is always 4 components but we may not have that
1953 * many. Swizzle to compensate.
1956 for (unsigned i
= 0; i
< 4; i
++)
1957 swiz
[i
] = i
< image
.coord
->num_components
? i
: 0;
1958 intrin
->src
[0] = nir_src_for_ssa(nir_swizzle(&b
->nb
, image
.coord
,
1960 intrin
->src
[1] = nir_src_for_ssa(image
.sample
);
1964 case SpvOpAtomicLoad
:
1965 case SpvOpImageQuerySize
:
1966 case SpvOpImageRead
:
1968 case SpvOpAtomicStore
:
1969 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
1971 case SpvOpImageWrite
:
1972 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[3])->def
);
1975 case SpvOpAtomicIIncrement
:
1976 case SpvOpAtomicIDecrement
:
1977 case SpvOpAtomicExchange
:
1978 case SpvOpAtomicIAdd
:
1979 case SpvOpAtomicSMin
:
1980 case SpvOpAtomicUMin
:
1981 case SpvOpAtomicSMax
:
1982 case SpvOpAtomicUMax
:
1983 case SpvOpAtomicAnd
:
1985 case SpvOpAtomicXor
:
1986 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[2]);
1990 unreachable("Invalid image opcode");
1993 if (opcode
!= SpvOpImageWrite
) {
1994 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1995 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1997 unsigned dest_components
=
1998 nir_intrinsic_infos
[intrin
->intrinsic
].dest_components
;
1999 if (intrin
->intrinsic
== nir_intrinsic_image_size
) {
2000 dest_components
= intrin
->num_components
=
2001 glsl_get_vector_elements(type
->type
);
2004 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2005 dest_components
, 32, NULL
);
2007 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2009 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
2010 val
->ssa
->def
= &intrin
->dest
.ssa
;
2012 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2016 static nir_intrinsic_op
2017 get_ssbo_nir_atomic_op(SpvOp opcode
)
2020 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2021 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2022 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2023 OP(AtomicExchange
, atomic_exchange
)
2024 OP(AtomicCompareExchange
, atomic_comp_swap
)
2025 OP(AtomicIIncrement
, atomic_add
)
2026 OP(AtomicIDecrement
, atomic_add
)
2027 OP(AtomicIAdd
, atomic_add
)
2028 OP(AtomicISub
, atomic_add
)
2029 OP(AtomicSMin
, atomic_imin
)
2030 OP(AtomicUMin
, atomic_umin
)
2031 OP(AtomicSMax
, atomic_imax
)
2032 OP(AtomicUMax
, atomic_umax
)
2033 OP(AtomicAnd
, atomic_and
)
2034 OP(AtomicOr
, atomic_or
)
2035 OP(AtomicXor
, atomic_xor
)
2038 unreachable("Invalid SSBO atomic");
2042 static nir_intrinsic_op
2043 get_shared_nir_atomic_op(SpvOp opcode
)
2046 case SpvOpAtomicLoad
: return nir_intrinsic_load_var
;
2047 case SpvOpAtomicStore
: return nir_intrinsic_store_var
;
2048 #define OP(S, N) case SpvOp##S: return nir_intrinsic_var_##N;
2049 OP(AtomicExchange
, atomic_exchange
)
2050 OP(AtomicCompareExchange
, atomic_comp_swap
)
2051 OP(AtomicIIncrement
, atomic_add
)
2052 OP(AtomicIDecrement
, atomic_add
)
2053 OP(AtomicIAdd
, atomic_add
)
2054 OP(AtomicISub
, atomic_add
)
2055 OP(AtomicSMin
, atomic_imin
)
2056 OP(AtomicUMin
, atomic_umin
)
2057 OP(AtomicSMax
, atomic_imax
)
2058 OP(AtomicUMax
, atomic_umax
)
2059 OP(AtomicAnd
, atomic_and
)
2060 OP(AtomicOr
, atomic_or
)
2061 OP(AtomicXor
, atomic_xor
)
2064 unreachable("Invalid shared atomic");
2069 vtn_handle_ssbo_or_shared_atomic(struct vtn_builder
*b
, SpvOp opcode
,
2070 const uint32_t *w
, unsigned count
)
2072 struct vtn_pointer
*ptr
;
2073 nir_intrinsic_instr
*atomic
;
2076 case SpvOpAtomicLoad
:
2077 case SpvOpAtomicExchange
:
2078 case SpvOpAtomicCompareExchange
:
2079 case SpvOpAtomicCompareExchangeWeak
:
2080 case SpvOpAtomicIIncrement
:
2081 case SpvOpAtomicIDecrement
:
2082 case SpvOpAtomicIAdd
:
2083 case SpvOpAtomicISub
:
2084 case SpvOpAtomicSMin
:
2085 case SpvOpAtomicUMin
:
2086 case SpvOpAtomicSMax
:
2087 case SpvOpAtomicUMax
:
2088 case SpvOpAtomicAnd
:
2090 case SpvOpAtomicXor
:
2091 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2094 case SpvOpAtomicStore
:
2095 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2099 unreachable("Invalid SPIR-V atomic");
2103 SpvScope scope = w[4];
2104 SpvMemorySemanticsMask semantics = w[5];
2107 if (ptr
->mode
== vtn_variable_mode_workgroup
) {
2108 nir_deref_var
*deref
= vtn_pointer_to_deref(b
, ptr
);
2109 const struct glsl_type
*deref_type
= nir_deref_tail(&deref
->deref
)->type
;
2110 nir_intrinsic_op op
= get_shared_nir_atomic_op(opcode
);
2111 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2112 atomic
->variables
[0] = nir_deref_var_clone(deref
, atomic
);
2115 case SpvOpAtomicLoad
:
2116 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2119 case SpvOpAtomicStore
:
2120 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2121 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2122 atomic
->src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2125 case SpvOpAtomicExchange
:
2126 case SpvOpAtomicCompareExchange
:
2127 case SpvOpAtomicCompareExchangeWeak
:
2128 case SpvOpAtomicIIncrement
:
2129 case SpvOpAtomicIDecrement
:
2130 case SpvOpAtomicIAdd
:
2131 case SpvOpAtomicISub
:
2132 case SpvOpAtomicSMin
:
2133 case SpvOpAtomicUMin
:
2134 case SpvOpAtomicSMax
:
2135 case SpvOpAtomicUMax
:
2136 case SpvOpAtomicAnd
:
2138 case SpvOpAtomicXor
:
2139 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[0]);
2143 unreachable("Invalid SPIR-V atomic");
2147 assert(ptr
->mode
== vtn_variable_mode_ssbo
);
2148 nir_ssa_def
*offset
, *index
;
2149 struct vtn_type
*type
;
2150 offset
= vtn_pointer_to_offset(b
, ptr
, &index
, &type
, NULL
, false);
2152 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(opcode
);
2154 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2157 case SpvOpAtomicLoad
:
2158 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2159 atomic
->src
[0] = nir_src_for_ssa(index
);
2160 atomic
->src
[1] = nir_src_for_ssa(offset
);
2163 case SpvOpAtomicStore
:
2164 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2165 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2166 atomic
->src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2167 atomic
->src
[1] = nir_src_for_ssa(index
);
2168 atomic
->src
[2] = nir_src_for_ssa(offset
);
2171 case SpvOpAtomicExchange
:
2172 case SpvOpAtomicCompareExchange
:
2173 case SpvOpAtomicCompareExchangeWeak
:
2174 case SpvOpAtomicIIncrement
:
2175 case SpvOpAtomicIDecrement
:
2176 case SpvOpAtomicIAdd
:
2177 case SpvOpAtomicISub
:
2178 case SpvOpAtomicSMin
:
2179 case SpvOpAtomicUMin
:
2180 case SpvOpAtomicSMax
:
2181 case SpvOpAtomicUMax
:
2182 case SpvOpAtomicAnd
:
2184 case SpvOpAtomicXor
:
2185 atomic
->src
[0] = nir_src_for_ssa(index
);
2186 atomic
->src
[1] = nir_src_for_ssa(offset
);
2187 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[2]);
2191 unreachable("Invalid SPIR-V atomic");
2195 if (opcode
!= SpvOpAtomicStore
) {
2196 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2198 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
2199 glsl_get_vector_elements(type
->type
),
2200 glsl_get_bit_size(type
->type
), NULL
);
2202 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2203 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
2204 val
->ssa
->def
= &atomic
->dest
.ssa
;
2205 val
->ssa
->type
= type
->type
;
2208 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
2211 static nir_alu_instr
*
2212 create_vec(nir_shader
*shader
, unsigned num_components
, unsigned bit_size
)
2215 switch (num_components
) {
2216 case 1: op
= nir_op_fmov
; break;
2217 case 2: op
= nir_op_vec2
; break;
2218 case 3: op
= nir_op_vec3
; break;
2219 case 4: op
= nir_op_vec4
; break;
2220 default: unreachable("bad vector size");
2223 nir_alu_instr
*vec
= nir_alu_instr_create(shader
, op
);
2224 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
2226 vec
->dest
.write_mask
= (1 << num_components
) - 1;
2231 struct vtn_ssa_value
*
2232 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
2234 if (src
->transposed
)
2235 return src
->transposed
;
2237 struct vtn_ssa_value
*dest
=
2238 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
2240 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
2241 nir_alu_instr
*vec
= create_vec(b
->shader
,
2242 glsl_get_matrix_columns(src
->type
),
2243 glsl_get_bit_size(src
->type
));
2244 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2245 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
2246 vec
->src
[0].swizzle
[0] = i
;
2248 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
2249 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
2250 vec
->src
[j
].swizzle
[0] = i
;
2253 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2254 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
2257 dest
->transposed
= src
;
2263 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
2265 unsigned swiz
[4] = { index
};
2266 return nir_swizzle(&b
->nb
, src
, swiz
, 1, true);
2270 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
2273 nir_alu_instr
*vec
= create_vec(b
->shader
, src
->num_components
,
2276 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
2278 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
2280 vec
->src
[i
].src
= nir_src_for_ssa(src
);
2281 vec
->src
[i
].swizzle
[0] = i
;
2285 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2287 return &vec
->dest
.dest
.ssa
;
2291 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2294 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
2295 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2296 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2297 vtn_vector_extract(b
, src
, i
), dest
);
2303 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2304 nir_ssa_def
*insert
, nir_ssa_def
*index
)
2306 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
2307 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2308 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2309 vtn_vector_insert(b
, src
, insert
, i
), dest
);
2314 static nir_ssa_def
*
2315 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
2316 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
2317 const uint32_t *indices
)
2319 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
, src0
->bit_size
);
2321 for (unsigned i
= 0; i
< num_components
; i
++) {
2322 uint32_t index
= indices
[i
];
2323 if (index
== 0xffffffff) {
2325 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
2326 } else if (index
< src0
->num_components
) {
2327 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
2328 vec
->src
[i
].swizzle
[0] = index
;
2330 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
2331 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
2335 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2337 return &vec
->dest
.dest
.ssa
;
2341 * Concatentates a number of vectors/scalars together to produce a vector
2343 static nir_ssa_def
*
2344 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
2345 unsigned num_srcs
, nir_ssa_def
**srcs
)
2347 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
,
2350 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2352 * "When constructing a vector, there must be at least two Constituent
2355 assert(num_srcs
>= 2);
2357 unsigned dest_idx
= 0;
2358 for (unsigned i
= 0; i
< num_srcs
; i
++) {
2359 nir_ssa_def
*src
= srcs
[i
];
2360 assert(dest_idx
+ src
->num_components
<= num_components
);
2361 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
2362 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
2363 vec
->src
[dest_idx
].swizzle
[0] = j
;
2368 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2370 * "When constructing a vector, the total number of components in all
2371 * the operands must equal the number of components in Result Type."
2373 assert(dest_idx
== num_components
);
2375 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2377 return &vec
->dest
.dest
.ssa
;
2380 static struct vtn_ssa_value
*
2381 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
2383 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
2384 dest
->type
= src
->type
;
2386 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2387 dest
->def
= src
->def
;
2389 unsigned elems
= glsl_get_length(src
->type
);
2391 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
2392 for (unsigned i
= 0; i
< elems
; i
++)
2393 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
2399 static struct vtn_ssa_value
*
2400 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2401 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
2402 unsigned num_indices
)
2404 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
2406 struct vtn_ssa_value
*cur
= dest
;
2408 for (i
= 0; i
< num_indices
- 1; i
++) {
2409 cur
= cur
->elems
[indices
[i
]];
2412 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2413 /* According to the SPIR-V spec, OpCompositeInsert may work down to
2414 * the component granularity. In that case, the last index will be
2415 * the index to insert the scalar into the vector.
2418 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
2420 cur
->elems
[indices
[i
]] = insert
;
2426 static struct vtn_ssa_value
*
2427 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2428 const uint32_t *indices
, unsigned num_indices
)
2430 struct vtn_ssa_value
*cur
= src
;
2431 for (unsigned i
= 0; i
< num_indices
; i
++) {
2432 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2433 assert(i
== num_indices
- 1);
2434 /* According to the SPIR-V spec, OpCompositeExtract may work down to
2435 * the component granularity. The last index will be the index of the
2436 * vector to extract.
2439 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
2440 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
2441 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
2444 cur
= cur
->elems
[indices
[i
]];
2452 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
2453 const uint32_t *w
, unsigned count
)
2455 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2456 const struct glsl_type
*type
=
2457 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
2458 val
->ssa
= vtn_create_ssa_value(b
, type
);
2461 case SpvOpVectorExtractDynamic
:
2462 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2463 vtn_ssa_value(b
, w
[4])->def
);
2466 case SpvOpVectorInsertDynamic
:
2467 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2468 vtn_ssa_value(b
, w
[4])->def
,
2469 vtn_ssa_value(b
, w
[5])->def
);
2472 case SpvOpVectorShuffle
:
2473 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
2474 vtn_ssa_value(b
, w
[3])->def
,
2475 vtn_ssa_value(b
, w
[4])->def
,
2479 case SpvOpCompositeConstruct
: {
2480 unsigned elems
= count
- 3;
2481 if (glsl_type_is_vector_or_scalar(type
)) {
2482 nir_ssa_def
*srcs
[4];
2483 for (unsigned i
= 0; i
< elems
; i
++)
2484 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
2486 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
2489 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2490 for (unsigned i
= 0; i
< elems
; i
++)
2491 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
2495 case SpvOpCompositeExtract
:
2496 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
2500 case SpvOpCompositeInsert
:
2501 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
2502 vtn_ssa_value(b
, w
[3]),
2506 case SpvOpCopyObject
:
2507 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
2511 unreachable("unknown composite operation");
2516 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
2517 const uint32_t *w
, unsigned count
)
2519 nir_intrinsic_op intrinsic_op
;
2521 case SpvOpEmitVertex
:
2522 case SpvOpEmitStreamVertex
:
2523 intrinsic_op
= nir_intrinsic_emit_vertex
;
2525 case SpvOpEndPrimitive
:
2526 case SpvOpEndStreamPrimitive
:
2527 intrinsic_op
= nir_intrinsic_end_primitive
;
2529 case SpvOpMemoryBarrier
:
2530 intrinsic_op
= nir_intrinsic_memory_barrier
;
2532 case SpvOpControlBarrier
:
2533 intrinsic_op
= nir_intrinsic_barrier
;
2536 unreachable("unknown barrier instruction");
2539 nir_intrinsic_instr
*intrin
=
2540 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
2542 if (opcode
== SpvOpEmitStreamVertex
|| opcode
== SpvOpEndStreamPrimitive
)
2543 nir_intrinsic_set_stream_id(intrin
, w
[1]);
2545 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2549 gl_primitive_from_spv_execution_mode(SpvExecutionMode mode
)
2552 case SpvExecutionModeInputPoints
:
2553 case SpvExecutionModeOutputPoints
:
2554 return 0; /* GL_POINTS */
2555 case SpvExecutionModeInputLines
:
2556 return 1; /* GL_LINES */
2557 case SpvExecutionModeInputLinesAdjacency
:
2558 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
2559 case SpvExecutionModeTriangles
:
2560 return 4; /* GL_TRIANGLES */
2561 case SpvExecutionModeInputTrianglesAdjacency
:
2562 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
2563 case SpvExecutionModeQuads
:
2564 return 7; /* GL_QUADS */
2565 case SpvExecutionModeIsolines
:
2566 return 0x8E7A; /* GL_ISOLINES */
2567 case SpvExecutionModeOutputLineStrip
:
2568 return 3; /* GL_LINE_STRIP */
2569 case SpvExecutionModeOutputTriangleStrip
:
2570 return 5; /* GL_TRIANGLE_STRIP */
2572 assert(!"Invalid primitive type");
2578 vertices_in_from_spv_execution_mode(SpvExecutionMode mode
)
2581 case SpvExecutionModeInputPoints
:
2583 case SpvExecutionModeInputLines
:
2585 case SpvExecutionModeInputLinesAdjacency
:
2587 case SpvExecutionModeTriangles
:
2589 case SpvExecutionModeInputTrianglesAdjacency
:
2592 assert(!"Invalid GS input mode");
2597 static gl_shader_stage
2598 stage_for_execution_model(SpvExecutionModel model
)
2601 case SpvExecutionModelVertex
:
2602 return MESA_SHADER_VERTEX
;
2603 case SpvExecutionModelTessellationControl
:
2604 return MESA_SHADER_TESS_CTRL
;
2605 case SpvExecutionModelTessellationEvaluation
:
2606 return MESA_SHADER_TESS_EVAL
;
2607 case SpvExecutionModelGeometry
:
2608 return MESA_SHADER_GEOMETRY
;
2609 case SpvExecutionModelFragment
:
2610 return MESA_SHADER_FRAGMENT
;
2611 case SpvExecutionModelGLCompute
:
2612 return MESA_SHADER_COMPUTE
;
2614 unreachable("Unsupported execution model");
2618 #define spv_check_supported(name, cap) do { \
2619 if (!(b->ext && b->ext->name)) \
2620 vtn_warn("Unsupported SPIR-V capability: %s", \
2621 spirv_capability_to_string(cap)); \
2625 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2626 const uint32_t *w
, unsigned count
)
2630 case SpvOpSourceExtension
:
2631 case SpvOpSourceContinued
:
2632 case SpvOpExtension
:
2633 /* Unhandled, but these are for debug so that's ok. */
2636 case SpvOpCapability
: {
2637 SpvCapability cap
= w
[1];
2639 case SpvCapabilityMatrix
:
2640 case SpvCapabilityShader
:
2641 case SpvCapabilityGeometry
:
2642 case SpvCapabilityGeometryPointSize
:
2643 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
2644 case SpvCapabilitySampledImageArrayDynamicIndexing
:
2645 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
2646 case SpvCapabilityStorageImageArrayDynamicIndexing
:
2647 case SpvCapabilityImageRect
:
2648 case SpvCapabilitySampledRect
:
2649 case SpvCapabilitySampled1D
:
2650 case SpvCapabilityImage1D
:
2651 case SpvCapabilitySampledCubeArray
:
2652 case SpvCapabilitySampledBuffer
:
2653 case SpvCapabilityImageBuffer
:
2654 case SpvCapabilityImageQuery
:
2655 case SpvCapabilityDerivativeControl
:
2656 case SpvCapabilityInterpolationFunction
:
2657 case SpvCapabilityMultiViewport
:
2658 case SpvCapabilitySampleRateShading
:
2659 case SpvCapabilityClipDistance
:
2660 case SpvCapabilityCullDistance
:
2661 case SpvCapabilityInputAttachment
:
2662 case SpvCapabilityImageGatherExtended
:
2663 case SpvCapabilityStorageImageExtendedFormats
:
2666 case SpvCapabilityGeometryStreams
:
2667 case SpvCapabilityLinkage
:
2668 case SpvCapabilityVector16
:
2669 case SpvCapabilityFloat16Buffer
:
2670 case SpvCapabilityFloat16
:
2671 case SpvCapabilityInt64Atomics
:
2672 case SpvCapabilityAtomicStorage
:
2673 case SpvCapabilityInt16
:
2674 case SpvCapabilityStorageImageMultisample
:
2675 case SpvCapabilityImageCubeArray
:
2676 case SpvCapabilityInt8
:
2677 case SpvCapabilitySparseResidency
:
2678 case SpvCapabilityMinLod
:
2679 case SpvCapabilityTransformFeedback
:
2680 vtn_warn("Unsupported SPIR-V capability: %s",
2681 spirv_capability_to_string(cap
));
2684 case SpvCapabilityFloat64
:
2685 spv_check_supported(float64
, cap
);
2687 case SpvCapabilityInt64
:
2688 spv_check_supported(int64
, cap
);
2691 case SpvCapabilityAddresses
:
2692 case SpvCapabilityKernel
:
2693 case SpvCapabilityImageBasic
:
2694 case SpvCapabilityImageReadWrite
:
2695 case SpvCapabilityImageMipmap
:
2696 case SpvCapabilityPipes
:
2697 case SpvCapabilityGroups
:
2698 case SpvCapabilityDeviceEnqueue
:
2699 case SpvCapabilityLiteralSampler
:
2700 case SpvCapabilityGenericPointer
:
2701 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
2702 spirv_capability_to_string(cap
));
2705 case SpvCapabilityImageMSArray
:
2706 spv_check_supported(image_ms_array
, cap
);
2709 case SpvCapabilityTessellation
:
2710 case SpvCapabilityTessellationPointSize
:
2711 spv_check_supported(tessellation
, cap
);
2714 case SpvCapabilityDrawParameters
:
2715 spv_check_supported(draw_parameters
, cap
);
2718 case SpvCapabilityStorageImageReadWithoutFormat
:
2719 spv_check_supported(image_read_without_format
, cap
);
2722 case SpvCapabilityStorageImageWriteWithoutFormat
:
2723 spv_check_supported(image_write_without_format
, cap
);
2726 case SpvCapabilityMultiView
:
2727 spv_check_supported(multiview
, cap
);
2731 unreachable("Unhandled capability");
2736 case SpvOpExtInstImport
:
2737 vtn_handle_extension(b
, opcode
, w
, count
);
2740 case SpvOpMemoryModel
:
2741 assert(w
[1] == SpvAddressingModelLogical
);
2742 assert(w
[2] == SpvMemoryModelGLSL450
);
2745 case SpvOpEntryPoint
: {
2746 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
2747 /* Let this be a name label regardless */
2748 unsigned name_words
;
2749 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
2751 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
2752 stage_for_execution_model(w
[1]) != b
->entry_point_stage
)
2755 assert(b
->entry_point
== NULL
);
2756 b
->entry_point
= entry_point
;
2761 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
2762 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2766 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2769 case SpvOpMemberName
:
2773 case SpvOpExecutionMode
:
2774 case SpvOpDecorationGroup
:
2776 case SpvOpMemberDecorate
:
2777 case SpvOpGroupDecorate
:
2778 case SpvOpGroupMemberDecorate
:
2779 vtn_handle_decoration(b
, opcode
, w
, count
);
2783 return false; /* End of preamble */
2790 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
2791 const struct vtn_decoration
*mode
, void *data
)
2793 assert(b
->entry_point
== entry_point
);
2795 switch(mode
->exec_mode
) {
2796 case SpvExecutionModeOriginUpperLeft
:
2797 case SpvExecutionModeOriginLowerLeft
:
2798 b
->origin_upper_left
=
2799 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
2802 case SpvExecutionModeEarlyFragmentTests
:
2803 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2804 b
->shader
->info
.fs
.early_fragment_tests
= true;
2807 case SpvExecutionModeInvocations
:
2808 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2809 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->literals
[0]);
2812 case SpvExecutionModeDepthReplacing
:
2813 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2814 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
2816 case SpvExecutionModeDepthGreater
:
2817 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2818 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
2820 case SpvExecutionModeDepthLess
:
2821 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2822 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
2824 case SpvExecutionModeDepthUnchanged
:
2825 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2826 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
2829 case SpvExecutionModeLocalSize
:
2830 assert(b
->shader
->stage
== MESA_SHADER_COMPUTE
);
2831 b
->shader
->info
.cs
.local_size
[0] = mode
->literals
[0];
2832 b
->shader
->info
.cs
.local_size
[1] = mode
->literals
[1];
2833 b
->shader
->info
.cs
.local_size
[2] = mode
->literals
[2];
2835 case SpvExecutionModeLocalSizeHint
:
2836 break; /* Nothing to do with this */
2838 case SpvExecutionModeOutputVertices
:
2839 if (b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2840 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
) {
2841 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->literals
[0];
2843 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2844 b
->shader
->info
.gs
.vertices_out
= mode
->literals
[0];
2848 case SpvExecutionModeInputPoints
:
2849 case SpvExecutionModeInputLines
:
2850 case SpvExecutionModeInputLinesAdjacency
:
2851 case SpvExecutionModeTriangles
:
2852 case SpvExecutionModeInputTrianglesAdjacency
:
2853 case SpvExecutionModeQuads
:
2854 case SpvExecutionModeIsolines
:
2855 if (b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2856 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
) {
2857 b
->shader
->info
.tess
.primitive_mode
=
2858 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
2860 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2861 b
->shader
->info
.gs
.vertices_in
=
2862 vertices_in_from_spv_execution_mode(mode
->exec_mode
);
2866 case SpvExecutionModeOutputPoints
:
2867 case SpvExecutionModeOutputLineStrip
:
2868 case SpvExecutionModeOutputTriangleStrip
:
2869 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2870 b
->shader
->info
.gs
.output_primitive
=
2871 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
2874 case SpvExecutionModeSpacingEqual
:
2875 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2876 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2877 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
2879 case SpvExecutionModeSpacingFractionalEven
:
2880 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2881 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2882 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
2884 case SpvExecutionModeSpacingFractionalOdd
:
2885 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2886 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2887 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
2889 case SpvExecutionModeVertexOrderCw
:
2890 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2891 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2892 /* Vulkan's notion of CCW seems to match the hardware backends,
2893 * but be the opposite of OpenGL. Currently NIR follows GL semantics,
2894 * so we set it backwards here.
2896 b
->shader
->info
.tess
.ccw
= true;
2898 case SpvExecutionModeVertexOrderCcw
:
2899 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2900 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2901 /* Backwards; see above */
2902 b
->shader
->info
.tess
.ccw
= false;
2904 case SpvExecutionModePointMode
:
2905 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2906 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2907 b
->shader
->info
.tess
.point_mode
= true;
2910 case SpvExecutionModePixelCenterInteger
:
2911 b
->pixel_center_integer
= true;
2914 case SpvExecutionModeXfb
:
2915 assert(!"Unhandled execution mode");
2918 case SpvExecutionModeVecTypeHint
:
2919 case SpvExecutionModeContractionOff
:
2923 unreachable("Unhandled execution mode");
2928 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2929 const uint32_t *w
, unsigned count
)
2933 case SpvOpSourceContinued
:
2934 case SpvOpSourceExtension
:
2935 case SpvOpExtension
:
2936 case SpvOpCapability
:
2937 case SpvOpExtInstImport
:
2938 case SpvOpMemoryModel
:
2939 case SpvOpEntryPoint
:
2940 case SpvOpExecutionMode
:
2943 case SpvOpMemberName
:
2944 case SpvOpDecorationGroup
:
2946 case SpvOpMemberDecorate
:
2947 case SpvOpGroupDecorate
:
2948 case SpvOpGroupMemberDecorate
:
2949 assert(!"Invalid opcode types and variables section");
2955 case SpvOpTypeFloat
:
2956 case SpvOpTypeVector
:
2957 case SpvOpTypeMatrix
:
2958 case SpvOpTypeImage
:
2959 case SpvOpTypeSampler
:
2960 case SpvOpTypeSampledImage
:
2961 case SpvOpTypeArray
:
2962 case SpvOpTypeRuntimeArray
:
2963 case SpvOpTypeStruct
:
2964 case SpvOpTypeOpaque
:
2965 case SpvOpTypePointer
:
2966 case SpvOpTypeFunction
:
2967 case SpvOpTypeEvent
:
2968 case SpvOpTypeDeviceEvent
:
2969 case SpvOpTypeReserveId
:
2970 case SpvOpTypeQueue
:
2972 vtn_handle_type(b
, opcode
, w
, count
);
2975 case SpvOpConstantTrue
:
2976 case SpvOpConstantFalse
:
2978 case SpvOpConstantComposite
:
2979 case SpvOpConstantSampler
:
2980 case SpvOpConstantNull
:
2981 case SpvOpSpecConstantTrue
:
2982 case SpvOpSpecConstantFalse
:
2983 case SpvOpSpecConstant
:
2984 case SpvOpSpecConstantComposite
:
2985 case SpvOpSpecConstantOp
:
2986 vtn_handle_constant(b
, opcode
, w
, count
);
2991 vtn_handle_variables(b
, opcode
, w
, count
);
2995 return false; /* End of preamble */
3002 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3003 const uint32_t *w
, unsigned count
)
3009 case SpvOpLoopMerge
:
3010 case SpvOpSelectionMerge
:
3011 /* This is handled by cfg pre-pass and walk_blocks */
3015 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
3016 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3021 vtn_handle_extension(b
, opcode
, w
, count
);
3027 case SpvOpCopyMemory
:
3028 case SpvOpCopyMemorySized
:
3029 case SpvOpAccessChain
:
3030 case SpvOpInBoundsAccessChain
:
3031 case SpvOpArrayLength
:
3032 vtn_handle_variables(b
, opcode
, w
, count
);
3035 case SpvOpFunctionCall
:
3036 vtn_handle_function_call(b
, opcode
, w
, count
);
3039 case SpvOpSampledImage
:
3041 case SpvOpImageSampleImplicitLod
:
3042 case SpvOpImageSampleExplicitLod
:
3043 case SpvOpImageSampleDrefImplicitLod
:
3044 case SpvOpImageSampleDrefExplicitLod
:
3045 case SpvOpImageSampleProjImplicitLod
:
3046 case SpvOpImageSampleProjExplicitLod
:
3047 case SpvOpImageSampleProjDrefImplicitLod
:
3048 case SpvOpImageSampleProjDrefExplicitLod
:
3049 case SpvOpImageFetch
:
3050 case SpvOpImageGather
:
3051 case SpvOpImageDrefGather
:
3052 case SpvOpImageQuerySizeLod
:
3053 case SpvOpImageQueryLod
:
3054 case SpvOpImageQueryLevels
:
3055 case SpvOpImageQuerySamples
:
3056 vtn_handle_texture(b
, opcode
, w
, count
);
3059 case SpvOpImageRead
:
3060 case SpvOpImageWrite
:
3061 case SpvOpImageTexelPointer
:
3062 vtn_handle_image(b
, opcode
, w
, count
);
3065 case SpvOpImageQuerySize
: {
3066 struct vtn_pointer
*image
=
3067 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
3068 if (image
->mode
== vtn_variable_mode_image
) {
3069 vtn_handle_image(b
, opcode
, w
, count
);
3071 assert(image
->mode
== vtn_variable_mode_sampler
);
3072 vtn_handle_texture(b
, opcode
, w
, count
);
3077 case SpvOpAtomicLoad
:
3078 case SpvOpAtomicExchange
:
3079 case SpvOpAtomicCompareExchange
:
3080 case SpvOpAtomicCompareExchangeWeak
:
3081 case SpvOpAtomicIIncrement
:
3082 case SpvOpAtomicIDecrement
:
3083 case SpvOpAtomicIAdd
:
3084 case SpvOpAtomicISub
:
3085 case SpvOpAtomicSMin
:
3086 case SpvOpAtomicUMin
:
3087 case SpvOpAtomicSMax
:
3088 case SpvOpAtomicUMax
:
3089 case SpvOpAtomicAnd
:
3091 case SpvOpAtomicXor
: {
3092 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
3093 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
3094 vtn_handle_image(b
, opcode
, w
, count
);
3096 assert(pointer
->value_type
== vtn_value_type_pointer
);
3097 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
3102 case SpvOpAtomicStore
: {
3103 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
3104 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
3105 vtn_handle_image(b
, opcode
, w
, count
);
3107 assert(pointer
->value_type
== vtn_value_type_pointer
);
3108 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
3118 case SpvOpConvertFToU
:
3119 case SpvOpConvertFToS
:
3120 case SpvOpConvertSToF
:
3121 case SpvOpConvertUToF
:
3125 case SpvOpQuantizeToF16
:
3126 case SpvOpConvertPtrToU
:
3127 case SpvOpConvertUToPtr
:
3128 case SpvOpPtrCastToGeneric
:
3129 case SpvOpGenericCastToPtr
:
3135 case SpvOpSignBitSet
:
3136 case SpvOpLessOrGreater
:
3138 case SpvOpUnordered
:
3153 case SpvOpVectorTimesScalar
:
3155 case SpvOpIAddCarry
:
3156 case SpvOpISubBorrow
:
3157 case SpvOpUMulExtended
:
3158 case SpvOpSMulExtended
:
3159 case SpvOpShiftRightLogical
:
3160 case SpvOpShiftRightArithmetic
:
3161 case SpvOpShiftLeftLogical
:
3162 case SpvOpLogicalEqual
:
3163 case SpvOpLogicalNotEqual
:
3164 case SpvOpLogicalOr
:
3165 case SpvOpLogicalAnd
:
3166 case SpvOpLogicalNot
:
3167 case SpvOpBitwiseOr
:
3168 case SpvOpBitwiseXor
:
3169 case SpvOpBitwiseAnd
:
3172 case SpvOpFOrdEqual
:
3173 case SpvOpFUnordEqual
:
3174 case SpvOpINotEqual
:
3175 case SpvOpFOrdNotEqual
:
3176 case SpvOpFUnordNotEqual
:
3177 case SpvOpULessThan
:
3178 case SpvOpSLessThan
:
3179 case SpvOpFOrdLessThan
:
3180 case SpvOpFUnordLessThan
:
3181 case SpvOpUGreaterThan
:
3182 case SpvOpSGreaterThan
:
3183 case SpvOpFOrdGreaterThan
:
3184 case SpvOpFUnordGreaterThan
:
3185 case SpvOpULessThanEqual
:
3186 case SpvOpSLessThanEqual
:
3187 case SpvOpFOrdLessThanEqual
:
3188 case SpvOpFUnordLessThanEqual
:
3189 case SpvOpUGreaterThanEqual
:
3190 case SpvOpSGreaterThanEqual
:
3191 case SpvOpFOrdGreaterThanEqual
:
3192 case SpvOpFUnordGreaterThanEqual
:
3198 case SpvOpFwidthFine
:
3199 case SpvOpDPdxCoarse
:
3200 case SpvOpDPdyCoarse
:
3201 case SpvOpFwidthCoarse
:
3202 case SpvOpBitFieldInsert
:
3203 case SpvOpBitFieldSExtract
:
3204 case SpvOpBitFieldUExtract
:
3205 case SpvOpBitReverse
:
3207 case SpvOpTranspose
:
3208 case SpvOpOuterProduct
:
3209 case SpvOpMatrixTimesScalar
:
3210 case SpvOpVectorTimesMatrix
:
3211 case SpvOpMatrixTimesVector
:
3212 case SpvOpMatrixTimesMatrix
:
3213 vtn_handle_alu(b
, opcode
, w
, count
);
3216 case SpvOpVectorExtractDynamic
:
3217 case SpvOpVectorInsertDynamic
:
3218 case SpvOpVectorShuffle
:
3219 case SpvOpCompositeConstruct
:
3220 case SpvOpCompositeExtract
:
3221 case SpvOpCompositeInsert
:
3222 case SpvOpCopyObject
:
3223 vtn_handle_composite(b
, opcode
, w
, count
);
3226 case SpvOpEmitVertex
:
3227 case SpvOpEndPrimitive
:
3228 case SpvOpEmitStreamVertex
:
3229 case SpvOpEndStreamPrimitive
:
3230 case SpvOpControlBarrier
:
3231 case SpvOpMemoryBarrier
:
3232 vtn_handle_barrier(b
, opcode
, w
, count
);
3236 unreachable("Unhandled opcode");
3243 spirv_to_nir(const uint32_t *words
, size_t word_count
,
3244 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
3245 gl_shader_stage stage
, const char *entry_point_name
,
3246 const struct nir_spirv_supported_extensions
*ext
,
3247 const nir_shader_compiler_options
*options
)
3249 const uint32_t *word_end
= words
+ word_count
;
3251 /* Handle the SPIR-V header (first 4 dwords) */
3252 assert(word_count
> 5);
3254 assert(words
[0] == SpvMagicNumber
);
3255 assert(words
[1] >= 0x10000);
3256 /* words[2] == generator magic */
3257 unsigned value_id_bound
= words
[3];
3258 assert(words
[4] == 0);
3262 /* Initialize the stn_builder object */
3263 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
3264 b
->value_id_bound
= value_id_bound
;
3265 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
3266 exec_list_make_empty(&b
->functions
);
3267 b
->entry_point_stage
= stage
;
3268 b
->entry_point_name
= entry_point_name
;
3271 /* Handle all the preamble instructions */
3272 words
= vtn_foreach_instruction(b
, words
, word_end
,
3273 vtn_handle_preamble_instruction
);
3275 if (b
->entry_point
== NULL
) {
3276 assert(!"Entry point not found");
3281 b
->shader
= nir_shader_create(NULL
, stage
, options
, NULL
);
3283 /* Set shader info defaults */
3284 b
->shader
->info
.gs
.invocations
= 1;
3286 /* Parse execution modes */
3287 vtn_foreach_execution_mode(b
, b
->entry_point
,
3288 vtn_handle_execution_mode
, NULL
);
3290 b
->specializations
= spec
;
3291 b
->num_specializations
= num_spec
;
3293 /* Handle all variable, type, and constant instructions */
3294 words
= vtn_foreach_instruction(b
, words
, word_end
,
3295 vtn_handle_variable_or_type_instruction
);
3297 vtn_build_cfg(b
, words
, word_end
);
3299 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
3300 b
->impl
= func
->impl
;
3301 b
->const_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
3302 _mesa_key_pointer_equal
);
3304 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
3307 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
3308 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
3309 assert(entry_point
);