2 * Copyright © 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Jason Ekstrand (jason@jlekstrand.net)
28 #include "vtn_private.h"
29 #include "nir/nir_vla.h"
30 #include "nir/nir_control_flow.h"
31 #include "nir/nir_constant_expressions.h"
32 #include "spirv_info.h"
35 _vtn_warn(const char *file
, int line
, const char *msg
, ...)
41 formatted
= ralloc_vasprintf(NULL
, msg
, args
);
44 fprintf(stderr
, "%s:%d WARNING: %s\n", file
, line
, formatted
);
46 ralloc_free(formatted
);
49 static struct vtn_ssa_value
*
50 vtn_undef_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
52 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
55 if (glsl_type_is_vector_or_scalar(type
)) {
56 unsigned num_components
= glsl_get_vector_elements(val
->type
);
57 unsigned bit_size
= glsl_get_bit_size(val
->type
);
58 val
->def
= nir_ssa_undef(&b
->nb
, num_components
, bit_size
);
60 unsigned elems
= glsl_get_length(val
->type
);
61 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
62 if (glsl_type_is_matrix(type
)) {
63 const struct glsl_type
*elem_type
=
64 glsl_vector_type(glsl_get_base_type(type
),
65 glsl_get_vector_elements(type
));
67 for (unsigned i
= 0; i
< elems
; i
++)
68 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
69 } else if (glsl_type_is_array(type
)) {
70 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
71 for (unsigned i
= 0; i
< elems
; i
++)
72 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
74 for (unsigned i
= 0; i
< elems
; i
++) {
75 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
76 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
84 static struct vtn_ssa_value
*
85 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
86 const struct glsl_type
*type
)
88 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
93 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
96 switch (glsl_get_base_type(type
)) {
100 case GLSL_TYPE_FLOAT
:
101 case GLSL_TYPE_DOUBLE
:
102 if (glsl_type_is_vector_or_scalar(type
)) {
103 unsigned num_components
= glsl_get_vector_elements(val
->type
);
104 nir_load_const_instr
*load
=
105 nir_load_const_instr_create(b
->shader
, num_components
, 32);
107 load
->value
= constant
->values
[0];
109 nir_instr_insert_before_cf_list(&b
->impl
->body
, &load
->instr
);
110 val
->def
= &load
->def
;
112 assert(glsl_type_is_matrix(type
));
113 unsigned rows
= glsl_get_vector_elements(val
->type
);
114 unsigned columns
= glsl_get_matrix_columns(val
->type
);
115 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, columns
);
117 for (unsigned i
= 0; i
< columns
; i
++) {
118 struct vtn_ssa_value
*col_val
= rzalloc(b
, struct vtn_ssa_value
);
119 col_val
->type
= glsl_get_column_type(val
->type
);
120 nir_load_const_instr
*load
=
121 nir_load_const_instr_create(b
->shader
, rows
, 32);
123 load
->value
= constant
->values
[i
];
125 nir_instr_insert_before_cf_list(&b
->impl
->body
, &load
->instr
);
126 col_val
->def
= &load
->def
;
128 val
->elems
[i
] = col_val
;
133 case GLSL_TYPE_ARRAY
: {
134 unsigned elems
= glsl_get_length(val
->type
);
135 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
136 const struct glsl_type
*elem_type
= glsl_get_array_element(val
->type
);
137 for (unsigned i
= 0; i
< elems
; i
++)
138 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
143 case GLSL_TYPE_STRUCT
: {
144 unsigned elems
= glsl_get_length(val
->type
);
145 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
146 for (unsigned i
= 0; i
< elems
; i
++) {
147 const struct glsl_type
*elem_type
=
148 glsl_get_struct_field(val
->type
, i
);
149 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
156 unreachable("bad constant type");
162 struct vtn_ssa_value
*
163 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
165 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
166 switch (val
->value_type
) {
167 case vtn_value_type_undef
:
168 return vtn_undef_ssa_value(b
, val
->type
->type
);
170 case vtn_value_type_constant
:
171 return vtn_const_ssa_value(b
, val
->constant
, val
->const_type
);
173 case vtn_value_type_ssa
:
176 case vtn_value_type_access_chain
:
177 /* This is needed for function parameters */
178 return vtn_variable_load(b
, val
->access_chain
);
181 unreachable("Invalid type for an SSA value");
186 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
187 unsigned word_count
, unsigned *words_used
)
189 char *dup
= ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
191 /* Ammount of space taken by the string (including the null) */
192 unsigned len
= strlen(dup
) + 1;
193 *words_used
= DIV_ROUND_UP(len
, sizeof(*words
));
199 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
200 const uint32_t *end
, vtn_instruction_handler handler
)
206 const uint32_t *w
= start
;
208 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
209 unsigned count
= w
[0] >> SpvWordCountShift
;
210 assert(count
>= 1 && w
+ count
<= end
);
214 break; /* Do nothing */
217 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
229 if (!handler(b
, opcode
, w
, count
))
241 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
242 const uint32_t *w
, unsigned count
)
245 case SpvOpExtInstImport
: {
246 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
247 if (strcmp((const char *)&w
[2], "GLSL.std.450") == 0) {
248 val
->ext_handler
= vtn_handle_glsl450_instruction
;
250 assert(!"Unsupported extension");
256 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
257 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
264 unreachable("Unhandled opcode");
269 _foreach_decoration_helper(struct vtn_builder
*b
,
270 struct vtn_value
*base_value
,
272 struct vtn_value
*value
,
273 vtn_decoration_foreach_cb cb
, void *data
)
275 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
277 if (dec
->scope
== VTN_DEC_DECORATION
) {
278 member
= parent_member
;
279 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
280 assert(parent_member
== -1);
281 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
283 /* Not a decoration */
288 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
289 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
292 cb(b
, base_value
, member
, dec
, data
);
297 /** Iterates (recursively if needed) over all of the decorations on a value
299 * This function iterates over all of the decorations applied to a given
300 * value. If it encounters a decoration group, it recurses into the group
301 * and iterates over all of those decorations as well.
304 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
305 vtn_decoration_foreach_cb cb
, void *data
)
307 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
311 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
312 vtn_execution_mode_foreach_cb cb
, void *data
)
314 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
315 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
318 assert(dec
->group
== NULL
);
319 cb(b
, value
, dec
, data
);
324 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
325 const uint32_t *w
, unsigned count
)
327 const uint32_t *w_end
= w
+ count
;
328 const uint32_t target
= w
[1];
332 case SpvOpDecorationGroup
:
333 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
337 case SpvOpMemberDecorate
:
338 case SpvOpExecutionMode
: {
339 struct vtn_value
*val
= &b
->values
[target
];
341 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
344 dec
->scope
= VTN_DEC_DECORATION
;
346 case SpvOpMemberDecorate
:
347 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
349 case SpvOpExecutionMode
:
350 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
353 unreachable("Invalid decoration opcode");
355 dec
->decoration
= *(w
++);
358 /* Link into the list */
359 dec
->next
= val
->decoration
;
360 val
->decoration
= dec
;
364 case SpvOpGroupMemberDecorate
:
365 case SpvOpGroupDecorate
: {
366 struct vtn_value
*group
=
367 vtn_value(b
, target
, vtn_value_type_decoration_group
);
369 for (; w
< w_end
; w
++) {
370 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
371 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
374 if (opcode
== SpvOpGroupDecorate
) {
375 dec
->scope
= VTN_DEC_DECORATION
;
377 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
380 /* Link into the list */
381 dec
->next
= val
->decoration
;
382 val
->decoration
= dec
;
388 unreachable("Unhandled opcode");
392 struct member_decoration_ctx
{
394 struct glsl_struct_field
*fields
;
395 struct vtn_type
*type
;
398 /* does a shallow copy of a vtn_type */
400 static struct vtn_type
*
401 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
403 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
404 dest
->type
= src
->type
;
405 dest
->is_builtin
= src
->is_builtin
;
407 dest
->builtin
= src
->builtin
;
409 if (!glsl_type_is_scalar(src
->type
)) {
410 switch (glsl_get_base_type(src
->type
)) {
414 case GLSL_TYPE_FLOAT
:
415 case GLSL_TYPE_DOUBLE
:
416 case GLSL_TYPE_ARRAY
:
417 dest
->row_major
= src
->row_major
;
418 dest
->stride
= src
->stride
;
419 dest
->array_element
= src
->array_element
;
422 case GLSL_TYPE_STRUCT
: {
423 unsigned elems
= glsl_get_length(src
->type
);
425 dest
->members
= ralloc_array(b
, struct vtn_type
*, elems
);
426 memcpy(dest
->members
, src
->members
, elems
* sizeof(struct vtn_type
*));
428 dest
->offsets
= ralloc_array(b
, unsigned, elems
);
429 memcpy(dest
->offsets
, src
->offsets
, elems
* sizeof(unsigned));
434 unreachable("unhandled type");
441 static struct vtn_type
*
442 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
444 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
445 type
= type
->members
[member
];
447 /* We may have an array of matrices.... Oh, joy! */
448 while (glsl_type_is_array(type
->type
)) {
449 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
450 type
= type
->array_element
;
453 assert(glsl_type_is_matrix(type
->type
));
459 struct_member_decoration_cb(struct vtn_builder
*b
,
460 struct vtn_value
*val
, int member
,
461 const struct vtn_decoration
*dec
, void *void_ctx
)
463 struct member_decoration_ctx
*ctx
= void_ctx
;
468 assert(member
< ctx
->num_fields
);
470 switch (dec
->decoration
) {
471 case SpvDecorationNonWritable
:
472 case SpvDecorationNonReadable
:
473 case SpvDecorationRelaxedPrecision
:
474 case SpvDecorationVolatile
:
475 case SpvDecorationCoherent
:
476 case SpvDecorationUniform
:
477 break; /* FIXME: Do nothing with this for now. */
478 case SpvDecorationNoPerspective
:
479 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
481 case SpvDecorationFlat
:
482 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
484 case SpvDecorationCentroid
:
485 ctx
->fields
[member
].centroid
= true;
487 case SpvDecorationSample
:
488 ctx
->fields
[member
].sample
= true;
490 case SpvDecorationStream
:
491 /* Vulkan only allows one GS stream */
492 assert(dec
->literals
[0] == 0);
494 case SpvDecorationLocation
:
495 ctx
->fields
[member
].location
= dec
->literals
[0];
497 case SpvDecorationComponent
:
498 break; /* FIXME: What should we do with these? */
499 case SpvDecorationBuiltIn
:
500 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
501 ctx
->type
->members
[member
]->is_builtin
= true;
502 ctx
->type
->members
[member
]->builtin
= dec
->literals
[0];
503 ctx
->type
->builtin_block
= true;
505 case SpvDecorationOffset
:
506 ctx
->type
->offsets
[member
] = dec
->literals
[0];
508 case SpvDecorationMatrixStride
:
509 mutable_matrix_member(b
, ctx
->type
, member
)->stride
= dec
->literals
[0];
511 case SpvDecorationColMajor
:
512 break; /* Nothing to do here. Column-major is the default. */
513 case SpvDecorationRowMajor
:
514 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
517 case SpvDecorationPatch
:
518 vtn_warn("Tessellation not yet supported");
521 case SpvDecorationSpecId
:
522 case SpvDecorationBlock
:
523 case SpvDecorationBufferBlock
:
524 case SpvDecorationArrayStride
:
525 case SpvDecorationGLSLShared
:
526 case SpvDecorationGLSLPacked
:
527 case SpvDecorationInvariant
:
528 case SpvDecorationRestrict
:
529 case SpvDecorationAliased
:
530 case SpvDecorationConstant
:
531 case SpvDecorationIndex
:
532 case SpvDecorationBinding
:
533 case SpvDecorationDescriptorSet
:
534 case SpvDecorationLinkageAttributes
:
535 case SpvDecorationNoContraction
:
536 case SpvDecorationInputAttachmentIndex
:
537 vtn_warn("Decoration not allowed on struct members: %s",
538 spirv_decoration_to_string(dec
->decoration
));
541 case SpvDecorationXfbBuffer
:
542 case SpvDecorationXfbStride
:
543 vtn_warn("Vulkan does not have transform feedback");
546 case SpvDecorationCPacked
:
547 case SpvDecorationSaturatedConversion
:
548 case SpvDecorationFuncParamAttr
:
549 case SpvDecorationFPRoundingMode
:
550 case SpvDecorationFPFastMathMode
:
551 case SpvDecorationAlignment
:
552 vtn_warn("Decoraiton only allowed for CL-style kernels: %s",
553 spirv_decoration_to_string(dec
->decoration
));
559 type_decoration_cb(struct vtn_builder
*b
,
560 struct vtn_value
*val
, int member
,
561 const struct vtn_decoration
*dec
, void *ctx
)
563 struct vtn_type
*type
= val
->type
;
568 switch (dec
->decoration
) {
569 case SpvDecorationArrayStride
:
570 type
->stride
= dec
->literals
[0];
572 case SpvDecorationBlock
:
575 case SpvDecorationBufferBlock
:
576 type
->buffer_block
= true;
578 case SpvDecorationGLSLShared
:
579 case SpvDecorationGLSLPacked
:
580 /* Ignore these, since we get explicit offsets anyways */
583 case SpvDecorationRowMajor
:
584 case SpvDecorationColMajor
:
585 case SpvDecorationMatrixStride
:
586 case SpvDecorationBuiltIn
:
587 case SpvDecorationNoPerspective
:
588 case SpvDecorationFlat
:
589 case SpvDecorationPatch
:
590 case SpvDecorationCentroid
:
591 case SpvDecorationSample
:
592 case SpvDecorationVolatile
:
593 case SpvDecorationCoherent
:
594 case SpvDecorationNonWritable
:
595 case SpvDecorationNonReadable
:
596 case SpvDecorationUniform
:
597 case SpvDecorationStream
:
598 case SpvDecorationLocation
:
599 case SpvDecorationComponent
:
600 case SpvDecorationOffset
:
601 case SpvDecorationXfbBuffer
:
602 case SpvDecorationXfbStride
:
603 vtn_warn("Decoraiton only allowed for struct members: %s",
604 spirv_decoration_to_string(dec
->decoration
));
607 case SpvDecorationRelaxedPrecision
:
608 case SpvDecorationSpecId
:
609 case SpvDecorationInvariant
:
610 case SpvDecorationRestrict
:
611 case SpvDecorationAliased
:
612 case SpvDecorationConstant
:
613 case SpvDecorationIndex
:
614 case SpvDecorationBinding
:
615 case SpvDecorationDescriptorSet
:
616 case SpvDecorationLinkageAttributes
:
617 case SpvDecorationNoContraction
:
618 case SpvDecorationInputAttachmentIndex
:
619 vtn_warn("Decoraiton not allowed on types: %s",
620 spirv_decoration_to_string(dec
->decoration
));
623 case SpvDecorationCPacked
:
624 case SpvDecorationSaturatedConversion
:
625 case SpvDecorationFuncParamAttr
:
626 case SpvDecorationFPRoundingMode
:
627 case SpvDecorationFPFastMathMode
:
628 case SpvDecorationAlignment
:
629 vtn_warn("Decoraiton only allowed for CL-style kernels: %s",
630 spirv_decoration_to_string(dec
->decoration
));
636 translate_image_format(SpvImageFormat format
)
639 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
640 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
641 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
642 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
643 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
644 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
645 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
646 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
647 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
648 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
649 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
650 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
651 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
652 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
653 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
654 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
655 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
656 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
657 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
658 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
659 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
660 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
661 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
662 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
663 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
664 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
665 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
666 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
667 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
668 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
669 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
670 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
671 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
672 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
673 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
674 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
675 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
676 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
677 case SpvImageFormatR16ui
: return 0x823A; /* GL_RG16UI */
678 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
680 assert(!"Invalid image format");
686 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
687 const uint32_t *w
, unsigned count
)
689 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
691 val
->type
= rzalloc(b
, struct vtn_type
);
692 val
->type
->is_builtin
= false;
693 val
->type
->val
= val
;
697 val
->type
->type
= glsl_void_type();
700 val
->type
->type
= glsl_bool_type();
703 const bool signedness
= w
[3];
704 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
707 case SpvOpTypeFloat
: {
709 val
->type
->type
= bit_size
== 64 ? glsl_double_type() : glsl_float_type();
713 case SpvOpTypeVector
: {
714 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
715 unsigned elems
= w
[3];
717 assert(glsl_type_is_scalar(base
->type
));
718 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
720 /* Vectors implicitly have sizeof(base_type) stride. For now, this
721 * is always 4 bytes. This will have to change if we want to start
722 * supporting doubles or half-floats.
724 val
->type
->stride
= 4;
725 val
->type
->array_element
= base
;
729 case SpvOpTypeMatrix
: {
730 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
731 unsigned columns
= w
[3];
733 assert(glsl_type_is_vector(base
->type
));
734 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
735 glsl_get_vector_elements(base
->type
),
737 assert(!glsl_type_is_error(val
->type
->type
));
738 val
->type
->array_element
= base
;
739 val
->type
->row_major
= false;
740 val
->type
->stride
= 0;
744 case SpvOpTypeRuntimeArray
:
745 case SpvOpTypeArray
: {
746 struct vtn_type
*array_element
=
747 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
750 if (opcode
== SpvOpTypeRuntimeArray
) {
751 /* A length of 0 is used to denote unsized arrays */
755 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
758 val
->type
->type
= glsl_array_type(array_element
->type
, length
);
759 val
->type
->array_element
= array_element
;
760 val
->type
->stride
= 0;
764 case SpvOpTypeStruct
: {
765 unsigned num_fields
= count
- 2;
766 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
767 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
769 NIR_VLA(struct glsl_struct_field
, fields
, count
);
770 for (unsigned i
= 0; i
< num_fields
; i
++) {
771 val
->type
->members
[i
] =
772 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
773 fields
[i
] = (struct glsl_struct_field
) {
774 .type
= val
->type
->members
[i
]->type
,
775 .name
= ralloc_asprintf(b
, "field%d", i
),
780 struct member_decoration_ctx ctx
= {
781 .num_fields
= num_fields
,
786 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
788 const char *name
= val
->name
? val
->name
: "struct";
790 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
);
794 case SpvOpTypeFunction
: {
795 const struct glsl_type
*return_type
=
796 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
797 NIR_VLA(struct glsl_function_param
, params
, count
- 3);
798 for (unsigned i
= 0; i
< count
- 3; i
++) {
799 params
[i
].type
= vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
->type
;
803 params
[i
].out
= true;
805 val
->type
->type
= glsl_function_type(return_type
, params
, count
- 3);
809 case SpvOpTypePointer
:
810 /* FIXME: For now, we'll just do the really lame thing and return
811 * the same type. The validator should ensure that the proper number
812 * of dereferences happen
814 val
->type
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
817 case SpvOpTypeImage
: {
818 const struct glsl_type
*sampled_type
=
819 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
821 assert(glsl_type_is_vector_or_scalar(sampled_type
));
823 enum glsl_sampler_dim dim
;
824 switch ((SpvDim
)w
[3]) {
825 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
826 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
827 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
828 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
829 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
830 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
831 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
833 unreachable("Invalid SPIR-V Sampler dimension");
836 bool is_shadow
= w
[4];
837 bool is_array
= w
[5];
838 bool multisampled
= w
[6];
839 unsigned sampled
= w
[7];
840 SpvImageFormat format
= w
[8];
843 val
->type
->access_qualifier
= w
[9];
845 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
848 assert(dim
== GLSL_SAMPLER_DIM_2D
);
849 dim
= GLSL_SAMPLER_DIM_MS
;
852 val
->type
->image_format
= translate_image_format(format
);
855 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
856 glsl_get_base_type(sampled_type
));
857 } else if (sampled
== 2) {
858 assert((dim
== GLSL_SAMPLER_DIM_SUBPASS
) || format
);
860 val
->type
->type
= glsl_image_type(dim
, is_array
,
861 glsl_get_base_type(sampled_type
));
863 assert(!"We need to know if the image will be sampled");
868 case SpvOpTypeSampledImage
:
869 val
->type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
872 case SpvOpTypeSampler
:
873 /* The actual sampler type here doesn't really matter. It gets
874 * thrown away the moment you combine it with an image. What really
875 * matters is that it's a sampler type as opposed to an integer type
876 * so the backend knows what to do.
878 val
->type
->type
= glsl_bare_sampler_type();
881 case SpvOpTypeOpaque
:
883 case SpvOpTypeDeviceEvent
:
884 case SpvOpTypeReserveId
:
888 unreachable("Unhandled opcode");
891 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
894 static nir_constant
*
895 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
897 nir_constant
*c
= rzalloc(b
, nir_constant
);
899 switch (glsl_get_base_type(type
)) {
903 case GLSL_TYPE_FLOAT
:
904 case GLSL_TYPE_DOUBLE
:
905 /* Nothing to do here. It's already initialized to zero */
908 case GLSL_TYPE_ARRAY
:
909 assert(glsl_get_length(type
) > 0);
910 c
->num_elements
= glsl_get_length(type
);
911 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
913 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
914 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
915 c
->elements
[i
] = c
->elements
[0];
918 case GLSL_TYPE_STRUCT
:
919 c
->num_elements
= glsl_get_length(type
);
920 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
922 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
923 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
928 unreachable("Invalid type for null constant");
935 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
936 int member
, const struct vtn_decoration
*dec
,
939 assert(member
== -1);
940 if (dec
->decoration
!= SpvDecorationSpecId
)
943 uint32_t *const_value
= data
;
945 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
946 if (b
->specializations
[i
].id
== dec
->literals
[0]) {
947 *const_value
= b
->specializations
[i
].data
;
954 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
955 uint32_t const_value
)
957 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &const_value
);
962 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
963 struct vtn_value
*val
,
965 const struct vtn_decoration
*dec
,
968 assert(member
== -1);
969 if (dec
->decoration
!= SpvDecorationBuiltIn
||
970 dec
->literals
[0] != SpvBuiltInWorkgroupSize
)
973 assert(val
->const_type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
975 b
->shader
->info
->cs
.local_size
[0] = val
->constant
->values
[0].u32
[0];
976 b
->shader
->info
->cs
.local_size
[1] = val
->constant
->values
[0].u32
[1];
977 b
->shader
->info
->cs
.local_size
[2] = val
->constant
->values
[0].u32
[2];
981 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
982 const uint32_t *w
, unsigned count
)
984 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
985 val
->const_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
986 val
->constant
= rzalloc(b
, nir_constant
);
988 case SpvOpConstantTrue
:
989 assert(val
->const_type
== glsl_bool_type());
990 val
->constant
->values
[0].u32
[0] = NIR_TRUE
;
992 case SpvOpConstantFalse
:
993 assert(val
->const_type
== glsl_bool_type());
994 val
->constant
->values
[0].u32
[0] = NIR_FALSE
;
997 case SpvOpSpecConstantTrue
:
998 case SpvOpSpecConstantFalse
: {
999 assert(val
->const_type
== glsl_bool_type());
1001 get_specialization(b
, val
, (opcode
== SpvOpSpecConstantTrue
));
1002 val
->constant
->values
[0].u32
[0] = int_val
? NIR_TRUE
: NIR_FALSE
;
1006 case SpvOpConstant
: {
1007 assert(glsl_type_is_scalar(val
->const_type
));
1008 int bit_size
= glsl_get_bit_size(val
->const_type
);
1009 if (bit_size
== 64) {
1010 val
->constant
->values
->u32
[0] = w
[3];
1011 val
->constant
->values
->u32
[1] = w
[4];
1013 assert(bit_size
== 32);
1014 val
->constant
->values
->u32
[0] = w
[3];
1018 case SpvOpSpecConstant
:
1019 assert(glsl_type_is_scalar(val
->const_type
));
1020 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1022 case SpvOpSpecConstantComposite
:
1023 case SpvOpConstantComposite
: {
1024 unsigned elem_count
= count
- 3;
1025 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1026 for (unsigned i
= 0; i
< elem_count
; i
++)
1027 elems
[i
] = vtn_value(b
, w
[i
+ 3], vtn_value_type_constant
)->constant
;
1029 switch (glsl_get_base_type(val
->const_type
)) {
1030 case GLSL_TYPE_UINT
:
1032 case GLSL_TYPE_FLOAT
:
1033 case GLSL_TYPE_BOOL
:
1034 if (glsl_type_is_matrix(val
->const_type
)) {
1035 assert(glsl_get_matrix_columns(val
->const_type
) == elem_count
);
1036 for (unsigned i
= 0; i
< elem_count
; i
++)
1037 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1039 assert(glsl_type_is_vector(val
->const_type
));
1040 assert(glsl_get_vector_elements(val
->const_type
) == elem_count
);
1041 for (unsigned i
= 0; i
< elem_count
; i
++)
1042 val
->constant
->values
[0].u32
[i
] = elems
[i
]->values
[0].u32
[0];
1047 case GLSL_TYPE_STRUCT
:
1048 case GLSL_TYPE_ARRAY
:
1049 ralloc_steal(val
->constant
, elems
);
1050 val
->constant
->num_elements
= elem_count
;
1051 val
->constant
->elements
= elems
;
1055 unreachable("Unsupported type for constants");
1060 case SpvOpSpecConstantOp
: {
1061 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1063 case SpvOpVectorShuffle
: {
1064 struct vtn_value
*v0
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1065 struct vtn_value
*v1
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1066 unsigned len0
= glsl_get_vector_elements(v0
->const_type
);
1067 unsigned len1
= glsl_get_vector_elements(v1
->const_type
);
1070 for (unsigned i
= 0; i
< len0
; i
++)
1071 u
[i
] = v0
->constant
->values
[0].u32
[i
];
1072 for (unsigned i
= 0; i
< len1
; i
++)
1073 u
[len0
+ i
] = v1
->constant
->values
[0].u32
[i
];
1075 for (unsigned i
= 0; i
< count
- 6; i
++) {
1076 uint32_t comp
= w
[i
+ 6];
1077 if (comp
== (uint32_t)-1) {
1078 val
->constant
->values
[0].u32
[i
] = 0xdeadbeef;
1080 val
->constant
->values
[0].u32
[i
] = u
[comp
];
1086 case SpvOpCompositeExtract
:
1087 case SpvOpCompositeInsert
: {
1088 struct vtn_value
*comp
;
1089 unsigned deref_start
;
1090 struct nir_constant
**c
;
1091 if (opcode
== SpvOpCompositeExtract
) {
1092 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1094 c
= &comp
->constant
;
1096 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1098 val
->constant
= nir_constant_clone(comp
->constant
,
1105 const struct glsl_type
*type
= comp
->const_type
;
1106 for (unsigned i
= deref_start
; i
< count
; i
++) {
1107 switch (glsl_get_base_type(type
)) {
1108 case GLSL_TYPE_UINT
:
1110 case GLSL_TYPE_FLOAT
:
1111 case GLSL_TYPE_BOOL
:
1112 /* If we hit this granularity, we're picking off an element */
1113 if (glsl_type_is_matrix(type
)) {
1114 assert(col
== 0 && elem
== -1);
1117 type
= glsl_get_column_type(type
);
1119 assert(elem
<= 0 && glsl_type_is_vector(type
));
1121 type
= glsl_scalar_type(glsl_get_base_type(type
));
1125 case GLSL_TYPE_ARRAY
:
1126 c
= &(*c
)->elements
[w
[i
]];
1127 type
= glsl_get_array_element(type
);
1130 case GLSL_TYPE_STRUCT
:
1131 c
= &(*c
)->elements
[w
[i
]];
1132 type
= glsl_get_struct_field(type
, w
[i
]);
1136 unreachable("Invalid constant type");
1140 if (opcode
== SpvOpCompositeExtract
) {
1144 unsigned num_components
= glsl_get_vector_elements(type
);
1145 for (unsigned i
= 0; i
< num_components
; i
++)
1146 val
->constant
->values
[0].u32
[i
] = (*c
)->values
[col
].u32
[elem
+ i
];
1149 struct vtn_value
*insert
=
1150 vtn_value(b
, w
[4], vtn_value_type_constant
);
1151 assert(insert
->const_type
== type
);
1153 *c
= insert
->constant
;
1155 unsigned num_components
= glsl_get_vector_elements(type
);
1156 for (unsigned i
= 0; i
< num_components
; i
++)
1157 (*c
)->values
[col
].u32
[elem
+ i
] = insert
->constant
->values
[0].u32
[i
];
1165 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(opcode
, &swap
);
1167 unsigned num_components
= glsl_get_vector_elements(val
->const_type
);
1169 glsl_get_bit_size(val
->const_type
);
1171 nir_const_value src
[4];
1173 for (unsigned i
= 0; i
< count
- 4; i
++) {
1175 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
)->constant
;
1177 unsigned j
= swap
? 1 - i
: i
;
1178 assert(bit_size
== 32);
1179 src
[j
] = c
->values
[0];
1182 val
->constant
->values
[0] =
1183 nir_eval_const_opcode(op
, num_components
, bit_size
, src
);
1190 case SpvOpConstantNull
:
1191 val
->constant
= vtn_null_constant(b
, val
->const_type
);
1194 case SpvOpConstantSampler
:
1195 assert(!"OpConstantSampler requires Kernel Capability");
1199 unreachable("Unhandled opcode");
1202 /* Now that we have the value, update the workgroup size if needed */
1203 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1207 vtn_handle_function_call(struct vtn_builder
*b
, SpvOp opcode
,
1208 const uint32_t *w
, unsigned count
)
1210 struct nir_function
*callee
=
1211 vtn_value(b
, w
[3], vtn_value_type_function
)->func
->impl
->function
;
1213 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, callee
);
1214 for (unsigned i
= 0; i
< call
->num_params
; i
++) {
1215 unsigned arg_id
= w
[4 + i
];
1216 struct vtn_value
*arg
= vtn_untyped_value(b
, arg_id
);
1217 if (arg
->value_type
== vtn_value_type_access_chain
) {
1218 nir_deref_var
*d
= vtn_access_chain_to_deref(b
, arg
->access_chain
);
1219 call
->params
[i
] = nir_deref_var_clone(d
, call
);
1221 struct vtn_ssa_value
*arg_ssa
= vtn_ssa_value(b
, arg_id
);
1223 /* Make a temporary to store the argument in */
1225 nir_local_variable_create(b
->impl
, arg_ssa
->type
, "arg_tmp");
1226 call
->params
[i
] = nir_deref_var_create(call
, tmp
);
1228 vtn_local_store(b
, arg_ssa
, call
->params
[i
]);
1232 nir_variable
*out_tmp
= NULL
;
1233 if (!glsl_type_is_void(callee
->return_type
)) {
1234 out_tmp
= nir_local_variable_create(b
->impl
, callee
->return_type
,
1236 call
->return_deref
= nir_deref_var_create(call
, out_tmp
);
1239 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
1241 if (glsl_type_is_void(callee
->return_type
)) {
1242 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1244 struct vtn_value
*retval
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1245 retval
->ssa
= vtn_local_load(b
, call
->return_deref
);
1249 struct vtn_ssa_value
*
1250 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1252 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1255 if (!glsl_type_is_vector_or_scalar(type
)) {
1256 unsigned elems
= glsl_get_length(type
);
1257 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1258 for (unsigned i
= 0; i
< elems
; i
++) {
1259 const struct glsl_type
*child_type
;
1261 switch (glsl_get_base_type(type
)) {
1263 case GLSL_TYPE_UINT
:
1264 case GLSL_TYPE_BOOL
:
1265 case GLSL_TYPE_FLOAT
:
1266 case GLSL_TYPE_DOUBLE
:
1267 child_type
= glsl_get_column_type(type
);
1269 case GLSL_TYPE_ARRAY
:
1270 child_type
= glsl_get_array_element(type
);
1272 case GLSL_TYPE_STRUCT
:
1273 child_type
= glsl_get_struct_field(type
, i
);
1276 unreachable("unkown base type");
1279 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
1287 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
1290 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
1291 src
.src_type
= type
;
1296 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
1297 const uint32_t *w
, unsigned count
)
1299 if (opcode
== SpvOpSampledImage
) {
1300 struct vtn_value
*val
=
1301 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1302 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1303 val
->sampled_image
->image
=
1304 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1305 val
->sampled_image
->sampler
=
1306 vtn_value(b
, w
[4], vtn_value_type_access_chain
)->access_chain
;
1308 } else if (opcode
== SpvOpImage
) {
1309 struct vtn_value
*val
=
1310 vtn_push_value(b
, w
[2], vtn_value_type_access_chain
);
1311 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
1312 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
1313 val
->access_chain
= src_val
->sampled_image
->image
;
1315 assert(src_val
->value_type
== vtn_value_type_access_chain
);
1316 val
->access_chain
= src_val
->access_chain
;
1321 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1322 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1324 struct vtn_sampled_image sampled
;
1325 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
1326 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
1327 sampled
= *sampled_val
->sampled_image
;
1329 assert(sampled_val
->value_type
== vtn_value_type_access_chain
);
1330 sampled
.image
= NULL
;
1331 sampled
.sampler
= sampled_val
->access_chain
;
1334 const struct glsl_type
*image_type
;
1335 if (sampled
.image
) {
1336 image_type
= sampled
.image
->var
->var
->interface_type
;
1338 image_type
= sampled
.sampler
->var
->var
->interface_type
;
1340 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
1341 const bool is_array
= glsl_sampler_type_is_array(image_type
);
1342 const bool is_shadow
= glsl_sampler_type_is_shadow(image_type
);
1344 /* Figure out the base texture operation */
1347 case SpvOpImageSampleImplicitLod
:
1348 case SpvOpImageSampleDrefImplicitLod
:
1349 case SpvOpImageSampleProjImplicitLod
:
1350 case SpvOpImageSampleProjDrefImplicitLod
:
1351 texop
= nir_texop_tex
;
1354 case SpvOpImageSampleExplicitLod
:
1355 case SpvOpImageSampleDrefExplicitLod
:
1356 case SpvOpImageSampleProjExplicitLod
:
1357 case SpvOpImageSampleProjDrefExplicitLod
:
1358 texop
= nir_texop_txl
;
1361 case SpvOpImageFetch
:
1362 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
1363 texop
= nir_texop_txf_ms
;
1365 texop
= nir_texop_txf
;
1369 case SpvOpImageGather
:
1370 case SpvOpImageDrefGather
:
1371 texop
= nir_texop_tg4
;
1374 case SpvOpImageQuerySizeLod
:
1375 case SpvOpImageQuerySize
:
1376 texop
= nir_texop_txs
;
1379 case SpvOpImageQueryLod
:
1380 texop
= nir_texop_lod
;
1383 case SpvOpImageQueryLevels
:
1384 texop
= nir_texop_query_levels
;
1387 case SpvOpImageQuerySamples
:
1388 texop
= nir_texop_texture_samples
;
1392 unreachable("Unhandled opcode");
1395 nir_tex_src srcs
[8]; /* 8 should be enough */
1396 nir_tex_src
*p
= srcs
;
1400 struct nir_ssa_def
*coord
;
1401 unsigned coord_components
;
1403 case SpvOpImageSampleImplicitLod
:
1404 case SpvOpImageSampleExplicitLod
:
1405 case SpvOpImageSampleDrefImplicitLod
:
1406 case SpvOpImageSampleDrefExplicitLod
:
1407 case SpvOpImageSampleProjImplicitLod
:
1408 case SpvOpImageSampleProjExplicitLod
:
1409 case SpvOpImageSampleProjDrefImplicitLod
:
1410 case SpvOpImageSampleProjDrefExplicitLod
:
1411 case SpvOpImageFetch
:
1412 case SpvOpImageGather
:
1413 case SpvOpImageDrefGather
:
1414 case SpvOpImageQueryLod
: {
1415 /* All these types have the coordinate as their first real argument */
1416 switch (sampler_dim
) {
1417 case GLSL_SAMPLER_DIM_1D
:
1418 case GLSL_SAMPLER_DIM_BUF
:
1419 coord_components
= 1;
1421 case GLSL_SAMPLER_DIM_2D
:
1422 case GLSL_SAMPLER_DIM_RECT
:
1423 case GLSL_SAMPLER_DIM_MS
:
1424 coord_components
= 2;
1426 case GLSL_SAMPLER_DIM_3D
:
1427 case GLSL_SAMPLER_DIM_CUBE
:
1428 coord_components
= 3;
1431 unreachable("Invalid sampler type");
1434 if (is_array
&& texop
!= nir_texop_lod
)
1437 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
1438 p
->src
= nir_src_for_ssa(coord
);
1439 p
->src_type
= nir_tex_src_coord
;
1446 coord_components
= 0;
1451 case SpvOpImageSampleProjImplicitLod
:
1452 case SpvOpImageSampleProjExplicitLod
:
1453 case SpvOpImageSampleProjDrefImplicitLod
:
1454 case SpvOpImageSampleProjDrefExplicitLod
:
1455 /* These have the projector as the last coordinate component */
1456 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
1457 p
->src_type
= nir_tex_src_projector
;
1465 unsigned gather_component
= 0;
1467 case SpvOpImageSampleDrefImplicitLod
:
1468 case SpvOpImageSampleDrefExplicitLod
:
1469 case SpvOpImageSampleProjDrefImplicitLod
:
1470 case SpvOpImageSampleProjDrefExplicitLod
:
1471 case SpvOpImageDrefGather
:
1472 /* These all have an explicit depth value as their next source */
1473 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
1476 case SpvOpImageGather
:
1477 /* This has a component as its next source */
1479 vtn_value(b
, w
[idx
++], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
1486 /* For OpImageQuerySizeLod, we always have an LOD */
1487 if (opcode
== SpvOpImageQuerySizeLod
)
1488 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1490 /* Now we need to handle some number of optional arguments */
1491 const struct vtn_ssa_value
*gather_offsets
= NULL
;
1493 uint32_t operands
= w
[idx
++];
1495 if (operands
& SpvImageOperandsBiasMask
) {
1496 assert(texop
== nir_texop_tex
);
1497 texop
= nir_texop_txb
;
1498 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
1501 if (operands
& SpvImageOperandsLodMask
) {
1502 assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
1503 texop
== nir_texop_txs
);
1504 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1507 if (operands
& SpvImageOperandsGradMask
) {
1508 assert(texop
== nir_texop_txl
);
1509 texop
= nir_texop_txd
;
1510 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
1511 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
1514 if (operands
& SpvImageOperandsOffsetMask
||
1515 operands
& SpvImageOperandsConstOffsetMask
)
1516 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
1518 if (operands
& SpvImageOperandsConstOffsetsMask
) {
1519 gather_offsets
= vtn_ssa_value(b
, w
[idx
++]);
1520 (*p
++) = (nir_tex_src
){};
1523 if (operands
& SpvImageOperandsSampleMask
) {
1524 assert(texop
== nir_texop_txf_ms
);
1525 texop
= nir_texop_txf_ms
;
1526 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
1529 /* We should have now consumed exactly all of the arguments */
1530 assert(idx
== count
);
1532 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
1535 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1537 instr
->coord_components
= coord_components
;
1538 instr
->sampler_dim
= sampler_dim
;
1539 instr
->is_array
= is_array
;
1540 instr
->is_shadow
= is_shadow
;
1541 instr
->is_new_style_shadow
=
1542 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
1543 instr
->component
= gather_component
;
1545 switch (glsl_get_sampler_result_type(image_type
)) {
1546 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
1547 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
1548 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
1549 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
1551 unreachable("Invalid base type for sampler result");
1554 nir_deref_var
*sampler
= vtn_access_chain_to_deref(b
, sampled
.sampler
);
1555 nir_deref_var
*texture
;
1556 if (sampled
.image
) {
1557 nir_deref_var
*image
= vtn_access_chain_to_deref(b
, sampled
.image
);
1563 instr
->texture
= nir_deref_var_clone(texture
, instr
);
1565 switch (instr
->op
) {
1570 /* These operations require a sampler */
1571 instr
->sampler
= nir_deref_var_clone(sampler
, instr
);
1574 case nir_texop_txf_ms
:
1578 case nir_texop_query_levels
:
1579 case nir_texop_texture_samples
:
1580 case nir_texop_samples_identical
:
1582 instr
->sampler
= NULL
;
1584 case nir_texop_txf_ms_mcs
:
1585 unreachable("unexpected nir_texop_txf_ms_mcs");
1588 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1589 nir_tex_instr_dest_size(instr
), 32, NULL
);
1591 assert(glsl_get_vector_elements(ret_type
->type
) ==
1592 nir_tex_instr_dest_size(instr
));
1595 nir_instr
*instruction
;
1596 if (gather_offsets
) {
1597 assert(glsl_get_base_type(gather_offsets
->type
) == GLSL_TYPE_ARRAY
);
1598 assert(glsl_get_length(gather_offsets
->type
) == 4);
1599 nir_tex_instr
*instrs
[4] = {instr
, NULL
, NULL
, NULL
};
1601 /* Copy the current instruction 4x */
1602 for (uint32_t i
= 1; i
< 4; i
++) {
1603 instrs
[i
] = nir_tex_instr_create(b
->shader
, instr
->num_srcs
);
1604 instrs
[i
]->op
= instr
->op
;
1605 instrs
[i
]->coord_components
= instr
->coord_components
;
1606 instrs
[i
]->sampler_dim
= instr
->sampler_dim
;
1607 instrs
[i
]->is_array
= instr
->is_array
;
1608 instrs
[i
]->is_shadow
= instr
->is_shadow
;
1609 instrs
[i
]->is_new_style_shadow
= instr
->is_new_style_shadow
;
1610 instrs
[i
]->component
= instr
->component
;
1611 instrs
[i
]->dest_type
= instr
->dest_type
;
1612 instrs
[i
]->texture
= nir_deref_var_clone(texture
, instrs
[i
]);
1613 instrs
[i
]->sampler
= NULL
;
1615 memcpy(instrs
[i
]->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1617 nir_ssa_dest_init(&instrs
[i
]->instr
, &instrs
[i
]->dest
,
1618 nir_tex_instr_dest_size(instr
), 32, NULL
);
1621 /* Fill in the last argument with the offset from the passed in offsets
1622 * and insert the instruction into the stream.
1624 for (uint32_t i
= 0; i
< 4; i
++) {
1626 src
.src
= nir_src_for_ssa(gather_offsets
->elems
[i
]->def
);
1627 src
.src_type
= nir_tex_src_offset
;
1628 instrs
[i
]->src
[instrs
[i
]->num_srcs
- 1] = src
;
1629 nir_builder_instr_insert(&b
->nb
, &instrs
[i
]->instr
);
1632 /* Combine the results of the 4 instructions by taking their .w
1635 nir_alu_instr
*vec4
= nir_alu_instr_create(b
->shader
, nir_op_vec4
);
1636 nir_ssa_dest_init(&vec4
->instr
, &vec4
->dest
.dest
, 4, 32, NULL
);
1637 vec4
->dest
.write_mask
= 0xf;
1638 for (uint32_t i
= 0; i
< 4; i
++) {
1639 vec4
->src
[i
].src
= nir_src_for_ssa(&instrs
[i
]->dest
.ssa
);
1640 vec4
->src
[i
].swizzle
[0] = 3;
1642 def
= &vec4
->dest
.dest
.ssa
;
1643 instruction
= &vec4
->instr
;
1645 def
= &instr
->dest
.ssa
;
1646 instruction
= &instr
->instr
;
1649 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
1650 val
->ssa
->def
= def
;
1652 nir_builder_instr_insert(&b
->nb
, instruction
);
1656 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
1657 const uint32_t *w
, nir_src
*src
)
1660 case SpvOpAtomicIIncrement
:
1661 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
1664 case SpvOpAtomicIDecrement
:
1665 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
1668 case SpvOpAtomicISub
:
1670 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
1673 case SpvOpAtomicCompareExchange
:
1674 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
1675 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
1678 case SpvOpAtomicExchange
:
1679 case SpvOpAtomicIAdd
:
1680 case SpvOpAtomicSMin
:
1681 case SpvOpAtomicUMin
:
1682 case SpvOpAtomicSMax
:
1683 case SpvOpAtomicUMax
:
1684 case SpvOpAtomicAnd
:
1686 case SpvOpAtomicXor
:
1687 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1691 unreachable("Invalid SPIR-V atomic");
1695 static nir_ssa_def
*
1696 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
1698 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
1700 /* The image_load_store intrinsics assume a 4-dim coordinate */
1701 unsigned dim
= glsl_get_vector_elements(coord
->type
);
1702 unsigned swizzle
[4];
1703 for (unsigned i
= 0; i
< 4; i
++)
1704 swizzle
[i
] = MIN2(i
, dim
- 1);
1706 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
1710 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
1711 const uint32_t *w
, unsigned count
)
1713 /* Just get this one out of the way */
1714 if (opcode
== SpvOpImageTexelPointer
) {
1715 struct vtn_value
*val
=
1716 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
1717 val
->image
= ralloc(b
, struct vtn_image_pointer
);
1720 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1721 val
->image
->coord
= get_image_coord(b
, w
[4]);
1722 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
1726 struct vtn_image_pointer image
;
1729 case SpvOpAtomicExchange
:
1730 case SpvOpAtomicCompareExchange
:
1731 case SpvOpAtomicCompareExchangeWeak
:
1732 case SpvOpAtomicIIncrement
:
1733 case SpvOpAtomicIDecrement
:
1734 case SpvOpAtomicIAdd
:
1735 case SpvOpAtomicISub
:
1736 case SpvOpAtomicLoad
:
1737 case SpvOpAtomicSMin
:
1738 case SpvOpAtomicUMin
:
1739 case SpvOpAtomicSMax
:
1740 case SpvOpAtomicUMax
:
1741 case SpvOpAtomicAnd
:
1743 case SpvOpAtomicXor
:
1744 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
1747 case SpvOpAtomicStore
:
1748 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
1751 case SpvOpImageQuerySize
:
1753 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1755 image
.sample
= NULL
;
1758 case SpvOpImageRead
:
1760 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1761 image
.coord
= get_image_coord(b
, w
[4]);
1763 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
1764 assert(w
[5] == SpvImageOperandsSampleMask
);
1765 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
1767 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1771 case SpvOpImageWrite
:
1773 vtn_value(b
, w
[1], vtn_value_type_access_chain
)->access_chain
;
1774 image
.coord
= get_image_coord(b
, w
[2]);
1778 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
1779 assert(w
[4] == SpvImageOperandsSampleMask
);
1780 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
1782 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1787 unreachable("Invalid image opcode");
1790 nir_intrinsic_op op
;
1792 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_##N; break;
1793 OP(ImageQuerySize
, size
)
1795 OP(ImageWrite
, store
)
1796 OP(AtomicLoad
, load
)
1797 OP(AtomicStore
, store
)
1798 OP(AtomicExchange
, atomic_exchange
)
1799 OP(AtomicCompareExchange
, atomic_comp_swap
)
1800 OP(AtomicIIncrement
, atomic_add
)
1801 OP(AtomicIDecrement
, atomic_add
)
1802 OP(AtomicIAdd
, atomic_add
)
1803 OP(AtomicISub
, atomic_add
)
1804 OP(AtomicSMin
, atomic_min
)
1805 OP(AtomicUMin
, atomic_min
)
1806 OP(AtomicSMax
, atomic_max
)
1807 OP(AtomicUMax
, atomic_max
)
1808 OP(AtomicAnd
, atomic_and
)
1809 OP(AtomicOr
, atomic_or
)
1810 OP(AtomicXor
, atomic_xor
)
1813 unreachable("Invalid image opcode");
1816 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
1818 nir_deref_var
*image_deref
= vtn_access_chain_to_deref(b
, image
.image
);
1819 intrin
->variables
[0] = nir_deref_var_clone(image_deref
, intrin
);
1821 /* ImageQuerySize doesn't take any extra parameters */
1822 if (opcode
!= SpvOpImageQuerySize
) {
1823 /* The image coordinate is always 4 components but we may not have that
1824 * many. Swizzle to compensate.
1827 for (unsigned i
= 0; i
< 4; i
++)
1828 swiz
[i
] = i
< image
.coord
->num_components
? i
: 0;
1829 intrin
->src
[0] = nir_src_for_ssa(nir_swizzle(&b
->nb
, image
.coord
,
1831 intrin
->src
[1] = nir_src_for_ssa(image
.sample
);
1835 case SpvOpAtomicLoad
:
1836 case SpvOpImageQuerySize
:
1837 case SpvOpImageRead
:
1839 case SpvOpAtomicStore
:
1840 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
1842 case SpvOpImageWrite
:
1843 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[3])->def
);
1846 case SpvOpAtomicIIncrement
:
1847 case SpvOpAtomicIDecrement
:
1848 case SpvOpAtomicExchange
:
1849 case SpvOpAtomicIAdd
:
1850 case SpvOpAtomicSMin
:
1851 case SpvOpAtomicUMin
:
1852 case SpvOpAtomicSMax
:
1853 case SpvOpAtomicUMax
:
1854 case SpvOpAtomicAnd
:
1856 case SpvOpAtomicXor
:
1857 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[2]);
1861 unreachable("Invalid image opcode");
1864 if (opcode
!= SpvOpImageWrite
) {
1865 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1866 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1867 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 4, 32, NULL
);
1869 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
1871 /* The image intrinsics always return 4 channels but we may not want
1872 * that many. Emit a mov to trim it down.
1874 unsigned swiz
[4] = {0, 1, 2, 3};
1875 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
1876 val
->ssa
->def
= nir_swizzle(&b
->nb
, &intrin
->dest
.ssa
, swiz
,
1877 glsl_get_vector_elements(type
->type
), false);
1879 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
1883 static nir_intrinsic_op
1884 get_ssbo_nir_atomic_op(SpvOp opcode
)
1887 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
1888 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
1889 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
1890 OP(AtomicExchange
, atomic_exchange
)
1891 OP(AtomicCompareExchange
, atomic_comp_swap
)
1892 OP(AtomicIIncrement
, atomic_add
)
1893 OP(AtomicIDecrement
, atomic_add
)
1894 OP(AtomicIAdd
, atomic_add
)
1895 OP(AtomicISub
, atomic_add
)
1896 OP(AtomicSMin
, atomic_imin
)
1897 OP(AtomicUMin
, atomic_umin
)
1898 OP(AtomicSMax
, atomic_imax
)
1899 OP(AtomicUMax
, atomic_umax
)
1900 OP(AtomicAnd
, atomic_and
)
1901 OP(AtomicOr
, atomic_or
)
1902 OP(AtomicXor
, atomic_xor
)
1905 unreachable("Invalid SSBO atomic");
1909 static nir_intrinsic_op
1910 get_shared_nir_atomic_op(SpvOp opcode
)
1913 case SpvOpAtomicLoad
: return nir_intrinsic_load_var
;
1914 case SpvOpAtomicStore
: return nir_intrinsic_store_var
;
1915 #define OP(S, N) case SpvOp##S: return nir_intrinsic_var_##N;
1916 OP(AtomicExchange
, atomic_exchange
)
1917 OP(AtomicCompareExchange
, atomic_comp_swap
)
1918 OP(AtomicIIncrement
, atomic_add
)
1919 OP(AtomicIDecrement
, atomic_add
)
1920 OP(AtomicIAdd
, atomic_add
)
1921 OP(AtomicISub
, atomic_add
)
1922 OP(AtomicSMin
, atomic_imin
)
1923 OP(AtomicUMin
, atomic_umin
)
1924 OP(AtomicSMax
, atomic_imax
)
1925 OP(AtomicUMax
, atomic_umax
)
1926 OP(AtomicAnd
, atomic_and
)
1927 OP(AtomicOr
, atomic_or
)
1928 OP(AtomicXor
, atomic_xor
)
1931 unreachable("Invalid shared atomic");
1936 vtn_handle_ssbo_or_shared_atomic(struct vtn_builder
*b
, SpvOp opcode
,
1937 const uint32_t *w
, unsigned count
)
1939 struct vtn_access_chain
*chain
;
1940 nir_intrinsic_instr
*atomic
;
1943 case SpvOpAtomicLoad
:
1944 case SpvOpAtomicExchange
:
1945 case SpvOpAtomicCompareExchange
:
1946 case SpvOpAtomicCompareExchangeWeak
:
1947 case SpvOpAtomicIIncrement
:
1948 case SpvOpAtomicIDecrement
:
1949 case SpvOpAtomicIAdd
:
1950 case SpvOpAtomicISub
:
1951 case SpvOpAtomicSMin
:
1952 case SpvOpAtomicUMin
:
1953 case SpvOpAtomicSMax
:
1954 case SpvOpAtomicUMax
:
1955 case SpvOpAtomicAnd
:
1957 case SpvOpAtomicXor
:
1959 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1962 case SpvOpAtomicStore
:
1964 vtn_value(b
, w
[1], vtn_value_type_access_chain
)->access_chain
;
1968 unreachable("Invalid SPIR-V atomic");
1972 SpvScope scope = w[4];
1973 SpvMemorySemanticsMask semantics = w[5];
1976 if (chain
->var
->mode
== vtn_variable_mode_workgroup
) {
1977 struct vtn_type
*type
= chain
->var
->type
;
1978 nir_deref_var
*deref
= vtn_access_chain_to_deref(b
, chain
);
1979 nir_intrinsic_op op
= get_shared_nir_atomic_op(opcode
);
1980 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
1981 atomic
->variables
[0] = nir_deref_var_clone(deref
, atomic
);
1984 case SpvOpAtomicLoad
:
1985 atomic
->num_components
= glsl_get_vector_elements(type
->type
);
1988 case SpvOpAtomicStore
:
1989 atomic
->num_components
= glsl_get_vector_elements(type
->type
);
1990 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
1991 atomic
->src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
1994 case SpvOpAtomicExchange
:
1995 case SpvOpAtomicCompareExchange
:
1996 case SpvOpAtomicCompareExchangeWeak
:
1997 case SpvOpAtomicIIncrement
:
1998 case SpvOpAtomicIDecrement
:
1999 case SpvOpAtomicIAdd
:
2000 case SpvOpAtomicISub
:
2001 case SpvOpAtomicSMin
:
2002 case SpvOpAtomicUMin
:
2003 case SpvOpAtomicSMax
:
2004 case SpvOpAtomicUMax
:
2005 case SpvOpAtomicAnd
:
2007 case SpvOpAtomicXor
:
2008 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[0]);
2012 unreachable("Invalid SPIR-V atomic");
2016 assert(chain
->var
->mode
== vtn_variable_mode_ssbo
);
2017 struct vtn_type
*type
;
2018 nir_ssa_def
*offset
, *index
;
2019 offset
= vtn_access_chain_to_offset(b
, chain
, &index
, &type
, NULL
, false);
2021 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(opcode
);
2023 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2026 case SpvOpAtomicLoad
:
2027 atomic
->num_components
= glsl_get_vector_elements(type
->type
);
2028 atomic
->src
[0] = nir_src_for_ssa(index
);
2029 atomic
->src
[1] = nir_src_for_ssa(offset
);
2032 case SpvOpAtomicStore
:
2033 atomic
->num_components
= glsl_get_vector_elements(type
->type
);
2034 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2035 atomic
->src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2036 atomic
->src
[1] = nir_src_for_ssa(index
);
2037 atomic
->src
[2] = nir_src_for_ssa(offset
);
2040 case SpvOpAtomicExchange
:
2041 case SpvOpAtomicCompareExchange
:
2042 case SpvOpAtomicCompareExchangeWeak
:
2043 case SpvOpAtomicIIncrement
:
2044 case SpvOpAtomicIDecrement
:
2045 case SpvOpAtomicIAdd
:
2046 case SpvOpAtomicISub
:
2047 case SpvOpAtomicSMin
:
2048 case SpvOpAtomicUMin
:
2049 case SpvOpAtomicSMax
:
2050 case SpvOpAtomicUMax
:
2051 case SpvOpAtomicAnd
:
2053 case SpvOpAtomicXor
:
2054 atomic
->src
[0] = nir_src_for_ssa(index
);
2055 atomic
->src
[1] = nir_src_for_ssa(offset
);
2056 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[2]);
2060 unreachable("Invalid SPIR-V atomic");
2064 if (opcode
!= SpvOpAtomicStore
) {
2065 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2067 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
2068 glsl_get_vector_elements(type
->type
),
2069 glsl_get_bit_size(type
->type
), NULL
);
2071 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2072 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
2073 val
->ssa
->def
= &atomic
->dest
.ssa
;
2074 val
->ssa
->type
= type
->type
;
2077 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
2080 static nir_alu_instr
*
2081 create_vec(nir_shader
*shader
, unsigned num_components
, unsigned bit_size
)
2084 switch (num_components
) {
2085 case 1: op
= nir_op_fmov
; break;
2086 case 2: op
= nir_op_vec2
; break;
2087 case 3: op
= nir_op_vec3
; break;
2088 case 4: op
= nir_op_vec4
; break;
2089 default: unreachable("bad vector size");
2092 nir_alu_instr
*vec
= nir_alu_instr_create(shader
, op
);
2093 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
2095 vec
->dest
.write_mask
= (1 << num_components
) - 1;
2100 struct vtn_ssa_value
*
2101 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
2103 if (src
->transposed
)
2104 return src
->transposed
;
2106 struct vtn_ssa_value
*dest
=
2107 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
2109 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
2110 nir_alu_instr
*vec
= create_vec(b
->shader
,
2111 glsl_get_matrix_columns(src
->type
),
2112 glsl_get_bit_size(src
->type
));
2113 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2114 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
2115 vec
->src
[0].swizzle
[0] = i
;
2117 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
2118 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
2119 vec
->src
[j
].swizzle
[0] = i
;
2122 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2123 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
2126 dest
->transposed
= src
;
2132 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
2134 unsigned swiz
[4] = { index
};
2135 return nir_swizzle(&b
->nb
, src
, swiz
, 1, true);
2139 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
2142 nir_alu_instr
*vec
= create_vec(b
->shader
, src
->num_components
,
2145 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
2147 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
2149 vec
->src
[i
].src
= nir_src_for_ssa(src
);
2150 vec
->src
[i
].swizzle
[0] = i
;
2154 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2156 return &vec
->dest
.dest
.ssa
;
2160 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2163 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
2164 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2165 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2166 vtn_vector_extract(b
, src
, i
), dest
);
2172 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2173 nir_ssa_def
*insert
, nir_ssa_def
*index
)
2175 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
2176 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2177 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2178 vtn_vector_insert(b
, src
, insert
, i
), dest
);
2183 static nir_ssa_def
*
2184 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
2185 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
2186 const uint32_t *indices
)
2188 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
, src0
->bit_size
);
2190 for (unsigned i
= 0; i
< num_components
; i
++) {
2191 uint32_t index
= indices
[i
];
2192 if (index
== 0xffffffff) {
2194 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
2195 } else if (index
< src0
->num_components
) {
2196 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
2197 vec
->src
[i
].swizzle
[0] = index
;
2199 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
2200 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
2204 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2206 return &vec
->dest
.dest
.ssa
;
2210 * Concatentates a number of vectors/scalars together to produce a vector
2212 static nir_ssa_def
*
2213 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
2214 unsigned num_srcs
, nir_ssa_def
**srcs
)
2216 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
,
2219 unsigned dest_idx
= 0;
2220 for (unsigned i
= 0; i
< num_srcs
; i
++) {
2221 nir_ssa_def
*src
= srcs
[i
];
2222 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
2223 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
2224 vec
->src
[dest_idx
].swizzle
[0] = j
;
2229 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2231 return &vec
->dest
.dest
.ssa
;
2234 static struct vtn_ssa_value
*
2235 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
2237 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
2238 dest
->type
= src
->type
;
2240 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2241 dest
->def
= src
->def
;
2243 unsigned elems
= glsl_get_length(src
->type
);
2245 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
2246 for (unsigned i
= 0; i
< elems
; i
++)
2247 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
2253 static struct vtn_ssa_value
*
2254 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2255 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
2256 unsigned num_indices
)
2258 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
2260 struct vtn_ssa_value
*cur
= dest
;
2262 for (i
= 0; i
< num_indices
- 1; i
++) {
2263 cur
= cur
->elems
[indices
[i
]];
2266 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2267 /* According to the SPIR-V spec, OpCompositeInsert may work down to
2268 * the component granularity. In that case, the last index will be
2269 * the index to insert the scalar into the vector.
2272 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
2274 cur
->elems
[indices
[i
]] = insert
;
2280 static struct vtn_ssa_value
*
2281 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2282 const uint32_t *indices
, unsigned num_indices
)
2284 struct vtn_ssa_value
*cur
= src
;
2285 for (unsigned i
= 0; i
< num_indices
; i
++) {
2286 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2287 assert(i
== num_indices
- 1);
2288 /* According to the SPIR-V spec, OpCompositeExtract may work down to
2289 * the component granularity. The last index will be the index of the
2290 * vector to extract.
2293 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
2294 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
2295 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
2298 cur
= cur
->elems
[indices
[i
]];
2306 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
2307 const uint32_t *w
, unsigned count
)
2309 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2310 const struct glsl_type
*type
=
2311 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
2312 val
->ssa
= vtn_create_ssa_value(b
, type
);
2315 case SpvOpVectorExtractDynamic
:
2316 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2317 vtn_ssa_value(b
, w
[4])->def
);
2320 case SpvOpVectorInsertDynamic
:
2321 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2322 vtn_ssa_value(b
, w
[4])->def
,
2323 vtn_ssa_value(b
, w
[5])->def
);
2326 case SpvOpVectorShuffle
:
2327 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
2328 vtn_ssa_value(b
, w
[3])->def
,
2329 vtn_ssa_value(b
, w
[4])->def
,
2333 case SpvOpCompositeConstruct
: {
2334 unsigned elems
= count
- 3;
2335 if (glsl_type_is_vector_or_scalar(type
)) {
2336 nir_ssa_def
*srcs
[4];
2337 for (unsigned i
= 0; i
< elems
; i
++)
2338 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
2340 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
2343 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2344 for (unsigned i
= 0; i
< elems
; i
++)
2345 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
2349 case SpvOpCompositeExtract
:
2350 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
2354 case SpvOpCompositeInsert
:
2355 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
2356 vtn_ssa_value(b
, w
[3]),
2360 case SpvOpCopyObject
:
2361 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
2365 unreachable("unknown composite operation");
2370 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
2371 const uint32_t *w
, unsigned count
)
2373 nir_intrinsic_op intrinsic_op
;
2375 case SpvOpEmitVertex
:
2376 case SpvOpEmitStreamVertex
:
2377 intrinsic_op
= nir_intrinsic_emit_vertex
;
2379 case SpvOpEndPrimitive
:
2380 case SpvOpEndStreamPrimitive
:
2381 intrinsic_op
= nir_intrinsic_end_primitive
;
2383 case SpvOpMemoryBarrier
:
2384 intrinsic_op
= nir_intrinsic_memory_barrier
;
2386 case SpvOpControlBarrier
:
2387 intrinsic_op
= nir_intrinsic_barrier
;
2390 unreachable("unknown barrier instruction");
2393 nir_intrinsic_instr
*intrin
=
2394 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
2396 if (opcode
== SpvOpEmitStreamVertex
|| opcode
== SpvOpEndStreamPrimitive
)
2397 nir_intrinsic_set_stream_id(intrin
, w
[1]);
2399 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2403 gl_primitive_from_spv_execution_mode(SpvExecutionMode mode
)
2406 case SpvExecutionModeInputPoints
:
2407 case SpvExecutionModeOutputPoints
:
2408 return 0; /* GL_POINTS */
2409 case SpvExecutionModeInputLines
:
2410 return 1; /* GL_LINES */
2411 case SpvExecutionModeInputLinesAdjacency
:
2412 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
2413 case SpvExecutionModeTriangles
:
2414 return 4; /* GL_TRIANGLES */
2415 case SpvExecutionModeInputTrianglesAdjacency
:
2416 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
2417 case SpvExecutionModeQuads
:
2418 return 7; /* GL_QUADS */
2419 case SpvExecutionModeIsolines
:
2420 return 0x8E7A; /* GL_ISOLINES */
2421 case SpvExecutionModeOutputLineStrip
:
2422 return 3; /* GL_LINE_STRIP */
2423 case SpvExecutionModeOutputTriangleStrip
:
2424 return 5; /* GL_TRIANGLE_STRIP */
2426 assert(!"Invalid primitive type");
2432 vertices_in_from_spv_execution_mode(SpvExecutionMode mode
)
2435 case SpvExecutionModeInputPoints
:
2437 case SpvExecutionModeInputLines
:
2439 case SpvExecutionModeInputLinesAdjacency
:
2441 case SpvExecutionModeTriangles
:
2443 case SpvExecutionModeInputTrianglesAdjacency
:
2446 assert(!"Invalid GS input mode");
2451 static gl_shader_stage
2452 stage_for_execution_model(SpvExecutionModel model
)
2455 case SpvExecutionModelVertex
:
2456 return MESA_SHADER_VERTEX
;
2457 case SpvExecutionModelTessellationControl
:
2458 return MESA_SHADER_TESS_CTRL
;
2459 case SpvExecutionModelTessellationEvaluation
:
2460 return MESA_SHADER_TESS_EVAL
;
2461 case SpvExecutionModelGeometry
:
2462 return MESA_SHADER_GEOMETRY
;
2463 case SpvExecutionModelFragment
:
2464 return MESA_SHADER_FRAGMENT
;
2465 case SpvExecutionModelGLCompute
:
2466 return MESA_SHADER_COMPUTE
;
2468 unreachable("Unsupported execution model");
2472 #define spv_check_supported(name, cap) do { \
2473 if (!(b->ext && b->ext->name)) \
2474 vtn_warn("Unsupported SPIR-V capability: %s", \
2475 spirv_capability_to_string(cap)); \
2479 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2480 const uint32_t *w
, unsigned count
)
2484 case SpvOpSourceExtension
:
2485 case SpvOpSourceContinued
:
2486 case SpvOpExtension
:
2487 /* Unhandled, but these are for debug so that's ok. */
2490 case SpvOpCapability
: {
2491 SpvCapability cap
= w
[1];
2493 case SpvCapabilityMatrix
:
2494 case SpvCapabilityShader
:
2495 case SpvCapabilityGeometry
:
2496 case SpvCapabilityGeometryPointSize
:
2497 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
2498 case SpvCapabilitySampledImageArrayDynamicIndexing
:
2499 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
2500 case SpvCapabilityStorageImageArrayDynamicIndexing
:
2501 case SpvCapabilityImageRect
:
2502 case SpvCapabilitySampledRect
:
2503 case SpvCapabilitySampled1D
:
2504 case SpvCapabilityImage1D
:
2505 case SpvCapabilitySampledCubeArray
:
2506 case SpvCapabilitySampledBuffer
:
2507 case SpvCapabilityImageBuffer
:
2508 case SpvCapabilityImageQuery
:
2509 case SpvCapabilityDerivativeControl
:
2510 case SpvCapabilityInterpolationFunction
:
2511 case SpvCapabilityMultiViewport
:
2512 case SpvCapabilitySampleRateShading
:
2513 case SpvCapabilityClipDistance
:
2514 case SpvCapabilityCullDistance
:
2515 case SpvCapabilityInputAttachment
:
2516 case SpvCapabilityImageGatherExtended
:
2517 case SpvCapabilityStorageImageExtendedFormats
:
2520 case SpvCapabilityGeometryStreams
:
2521 case SpvCapabilityTessellation
:
2522 case SpvCapabilityTessellationPointSize
:
2523 case SpvCapabilityLinkage
:
2524 case SpvCapabilityVector16
:
2525 case SpvCapabilityFloat16Buffer
:
2526 case SpvCapabilityFloat16
:
2527 case SpvCapabilityFloat64
:
2528 case SpvCapabilityInt64
:
2529 case SpvCapabilityInt64Atomics
:
2530 case SpvCapabilityAtomicStorage
:
2531 case SpvCapabilityInt16
:
2532 case SpvCapabilityStorageImageMultisample
:
2533 case SpvCapabilityImageCubeArray
:
2534 case SpvCapabilityInt8
:
2535 case SpvCapabilitySparseResidency
:
2536 case SpvCapabilityMinLod
:
2537 case SpvCapabilityTransformFeedback
:
2538 case SpvCapabilityStorageImageReadWithoutFormat
:
2539 case SpvCapabilityStorageImageWriteWithoutFormat
:
2540 vtn_warn("Unsupported SPIR-V capability: %s",
2541 spirv_capability_to_string(cap
));
2544 case SpvCapabilityAddresses
:
2545 case SpvCapabilityKernel
:
2546 case SpvCapabilityImageBasic
:
2547 case SpvCapabilityImageReadWrite
:
2548 case SpvCapabilityImageMipmap
:
2549 case SpvCapabilityPipes
:
2550 case SpvCapabilityGroups
:
2551 case SpvCapabilityDeviceEnqueue
:
2552 case SpvCapabilityLiteralSampler
:
2553 case SpvCapabilityGenericPointer
:
2554 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
2555 spirv_capability_to_string(cap
));
2558 case SpvCapabilityImageMSArray
:
2559 spv_check_supported(image_ms_array
, cap
);
2565 case SpvOpExtInstImport
:
2566 vtn_handle_extension(b
, opcode
, w
, count
);
2569 case SpvOpMemoryModel
:
2570 assert(w
[1] == SpvAddressingModelLogical
);
2571 assert(w
[2] == SpvMemoryModelGLSL450
);
2574 case SpvOpEntryPoint
: {
2575 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
2576 /* Let this be a name label regardless */
2577 unsigned name_words
;
2578 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
2580 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
2581 stage_for_execution_model(w
[1]) != b
->entry_point_stage
)
2584 assert(b
->entry_point
== NULL
);
2585 b
->entry_point
= entry_point
;
2590 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
2591 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2595 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2598 case SpvOpMemberName
:
2602 case SpvOpExecutionMode
:
2603 case SpvOpDecorationGroup
:
2605 case SpvOpMemberDecorate
:
2606 case SpvOpGroupDecorate
:
2607 case SpvOpGroupMemberDecorate
:
2608 vtn_handle_decoration(b
, opcode
, w
, count
);
2612 return false; /* End of preamble */
2619 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
2620 const struct vtn_decoration
*mode
, void *data
)
2622 assert(b
->entry_point
== entry_point
);
2624 switch(mode
->exec_mode
) {
2625 case SpvExecutionModeOriginUpperLeft
:
2626 case SpvExecutionModeOriginLowerLeft
:
2627 b
->origin_upper_left
=
2628 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
2631 case SpvExecutionModeEarlyFragmentTests
:
2632 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2633 b
->shader
->info
->fs
.early_fragment_tests
= true;
2636 case SpvExecutionModeInvocations
:
2637 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2638 b
->shader
->info
->gs
.invocations
= MAX2(1, mode
->literals
[0]);
2641 case SpvExecutionModeDepthReplacing
:
2642 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2643 b
->shader
->info
->fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
2645 case SpvExecutionModeDepthGreater
:
2646 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2647 b
->shader
->info
->fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
2649 case SpvExecutionModeDepthLess
:
2650 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2651 b
->shader
->info
->fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
2653 case SpvExecutionModeDepthUnchanged
:
2654 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2655 b
->shader
->info
->fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
2658 case SpvExecutionModeLocalSize
:
2659 assert(b
->shader
->stage
== MESA_SHADER_COMPUTE
);
2660 b
->shader
->info
->cs
.local_size
[0] = mode
->literals
[0];
2661 b
->shader
->info
->cs
.local_size
[1] = mode
->literals
[1];
2662 b
->shader
->info
->cs
.local_size
[2] = mode
->literals
[2];
2664 case SpvExecutionModeLocalSizeHint
:
2665 break; /* Nothing to do with this */
2667 case SpvExecutionModeOutputVertices
:
2668 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2669 b
->shader
->info
->gs
.vertices_out
= mode
->literals
[0];
2672 case SpvExecutionModeInputPoints
:
2673 case SpvExecutionModeInputLines
:
2674 case SpvExecutionModeInputLinesAdjacency
:
2675 case SpvExecutionModeTriangles
:
2676 case SpvExecutionModeInputTrianglesAdjacency
:
2677 case SpvExecutionModeQuads
:
2678 case SpvExecutionModeIsolines
:
2679 if (b
->shader
->stage
== MESA_SHADER_GEOMETRY
) {
2680 b
->shader
->info
->gs
.vertices_in
=
2681 vertices_in_from_spv_execution_mode(mode
->exec_mode
);
2683 assert(!"Tesselation shaders not yet supported");
2687 case SpvExecutionModeOutputPoints
:
2688 case SpvExecutionModeOutputLineStrip
:
2689 case SpvExecutionModeOutputTriangleStrip
:
2690 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2691 b
->shader
->info
->gs
.output_primitive
=
2692 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
2695 case SpvExecutionModeSpacingEqual
:
2696 case SpvExecutionModeSpacingFractionalEven
:
2697 case SpvExecutionModeSpacingFractionalOdd
:
2698 case SpvExecutionModeVertexOrderCw
:
2699 case SpvExecutionModeVertexOrderCcw
:
2700 case SpvExecutionModePointMode
:
2701 assert(!"TODO: Add tessellation metadata");
2704 case SpvExecutionModePixelCenterInteger
:
2705 b
->pixel_center_integer
= true;
2708 case SpvExecutionModeXfb
:
2709 assert(!"Unhandled execution mode");
2712 case SpvExecutionModeVecTypeHint
:
2713 case SpvExecutionModeContractionOff
:
2719 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2720 const uint32_t *w
, unsigned count
)
2724 case SpvOpSourceContinued
:
2725 case SpvOpSourceExtension
:
2726 case SpvOpExtension
:
2727 case SpvOpCapability
:
2728 case SpvOpExtInstImport
:
2729 case SpvOpMemoryModel
:
2730 case SpvOpEntryPoint
:
2731 case SpvOpExecutionMode
:
2734 case SpvOpMemberName
:
2735 case SpvOpDecorationGroup
:
2737 case SpvOpMemberDecorate
:
2738 case SpvOpGroupDecorate
:
2739 case SpvOpGroupMemberDecorate
:
2740 assert(!"Invalid opcode types and variables section");
2746 case SpvOpTypeFloat
:
2747 case SpvOpTypeVector
:
2748 case SpvOpTypeMatrix
:
2749 case SpvOpTypeImage
:
2750 case SpvOpTypeSampler
:
2751 case SpvOpTypeSampledImage
:
2752 case SpvOpTypeArray
:
2753 case SpvOpTypeRuntimeArray
:
2754 case SpvOpTypeStruct
:
2755 case SpvOpTypeOpaque
:
2756 case SpvOpTypePointer
:
2757 case SpvOpTypeFunction
:
2758 case SpvOpTypeEvent
:
2759 case SpvOpTypeDeviceEvent
:
2760 case SpvOpTypeReserveId
:
2761 case SpvOpTypeQueue
:
2763 vtn_handle_type(b
, opcode
, w
, count
);
2766 case SpvOpConstantTrue
:
2767 case SpvOpConstantFalse
:
2769 case SpvOpConstantComposite
:
2770 case SpvOpConstantSampler
:
2771 case SpvOpConstantNull
:
2772 case SpvOpSpecConstantTrue
:
2773 case SpvOpSpecConstantFalse
:
2774 case SpvOpSpecConstant
:
2775 case SpvOpSpecConstantComposite
:
2776 case SpvOpSpecConstantOp
:
2777 vtn_handle_constant(b
, opcode
, w
, count
);
2781 vtn_handle_variables(b
, opcode
, w
, count
);
2785 return false; /* End of preamble */
2792 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2793 const uint32_t *w
, unsigned count
)
2799 case SpvOpLoopMerge
:
2800 case SpvOpSelectionMerge
:
2801 /* This is handled by cfg pre-pass and walk_blocks */
2805 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
2806 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2811 vtn_handle_extension(b
, opcode
, w
, count
);
2817 case SpvOpCopyMemory
:
2818 case SpvOpCopyMemorySized
:
2819 case SpvOpAccessChain
:
2820 case SpvOpInBoundsAccessChain
:
2821 case SpvOpArrayLength
:
2822 vtn_handle_variables(b
, opcode
, w
, count
);
2825 case SpvOpFunctionCall
:
2826 vtn_handle_function_call(b
, opcode
, w
, count
);
2829 case SpvOpSampledImage
:
2831 case SpvOpImageSampleImplicitLod
:
2832 case SpvOpImageSampleExplicitLod
:
2833 case SpvOpImageSampleDrefImplicitLod
:
2834 case SpvOpImageSampleDrefExplicitLod
:
2835 case SpvOpImageSampleProjImplicitLod
:
2836 case SpvOpImageSampleProjExplicitLod
:
2837 case SpvOpImageSampleProjDrefImplicitLod
:
2838 case SpvOpImageSampleProjDrefExplicitLod
:
2839 case SpvOpImageFetch
:
2840 case SpvOpImageGather
:
2841 case SpvOpImageDrefGather
:
2842 case SpvOpImageQuerySizeLod
:
2843 case SpvOpImageQueryLod
:
2844 case SpvOpImageQueryLevels
:
2845 case SpvOpImageQuerySamples
:
2846 vtn_handle_texture(b
, opcode
, w
, count
);
2849 case SpvOpImageRead
:
2850 case SpvOpImageWrite
:
2851 case SpvOpImageTexelPointer
:
2852 vtn_handle_image(b
, opcode
, w
, count
);
2855 case SpvOpImageQuerySize
: {
2856 struct vtn_access_chain
*image
=
2857 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
2858 if (glsl_type_is_image(image
->var
->var
->interface_type
)) {
2859 vtn_handle_image(b
, opcode
, w
, count
);
2861 vtn_handle_texture(b
, opcode
, w
, count
);
2866 case SpvOpAtomicLoad
:
2867 case SpvOpAtomicExchange
:
2868 case SpvOpAtomicCompareExchange
:
2869 case SpvOpAtomicCompareExchangeWeak
:
2870 case SpvOpAtomicIIncrement
:
2871 case SpvOpAtomicIDecrement
:
2872 case SpvOpAtomicIAdd
:
2873 case SpvOpAtomicISub
:
2874 case SpvOpAtomicSMin
:
2875 case SpvOpAtomicUMin
:
2876 case SpvOpAtomicSMax
:
2877 case SpvOpAtomicUMax
:
2878 case SpvOpAtomicAnd
:
2880 case SpvOpAtomicXor
: {
2881 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
2882 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
2883 vtn_handle_image(b
, opcode
, w
, count
);
2885 assert(pointer
->value_type
== vtn_value_type_access_chain
);
2886 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
2891 case SpvOpAtomicStore
: {
2892 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
2893 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
2894 vtn_handle_image(b
, opcode
, w
, count
);
2896 assert(pointer
->value_type
== vtn_value_type_access_chain
);
2897 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
2907 case SpvOpConvertFToU
:
2908 case SpvOpConvertFToS
:
2909 case SpvOpConvertSToF
:
2910 case SpvOpConvertUToF
:
2914 case SpvOpQuantizeToF16
:
2915 case SpvOpConvertPtrToU
:
2916 case SpvOpConvertUToPtr
:
2917 case SpvOpPtrCastToGeneric
:
2918 case SpvOpGenericCastToPtr
:
2924 case SpvOpSignBitSet
:
2925 case SpvOpLessOrGreater
:
2927 case SpvOpUnordered
:
2942 case SpvOpVectorTimesScalar
:
2944 case SpvOpIAddCarry
:
2945 case SpvOpISubBorrow
:
2946 case SpvOpUMulExtended
:
2947 case SpvOpSMulExtended
:
2948 case SpvOpShiftRightLogical
:
2949 case SpvOpShiftRightArithmetic
:
2950 case SpvOpShiftLeftLogical
:
2951 case SpvOpLogicalEqual
:
2952 case SpvOpLogicalNotEqual
:
2953 case SpvOpLogicalOr
:
2954 case SpvOpLogicalAnd
:
2955 case SpvOpLogicalNot
:
2956 case SpvOpBitwiseOr
:
2957 case SpvOpBitwiseXor
:
2958 case SpvOpBitwiseAnd
:
2961 case SpvOpFOrdEqual
:
2962 case SpvOpFUnordEqual
:
2963 case SpvOpINotEqual
:
2964 case SpvOpFOrdNotEqual
:
2965 case SpvOpFUnordNotEqual
:
2966 case SpvOpULessThan
:
2967 case SpvOpSLessThan
:
2968 case SpvOpFOrdLessThan
:
2969 case SpvOpFUnordLessThan
:
2970 case SpvOpUGreaterThan
:
2971 case SpvOpSGreaterThan
:
2972 case SpvOpFOrdGreaterThan
:
2973 case SpvOpFUnordGreaterThan
:
2974 case SpvOpULessThanEqual
:
2975 case SpvOpSLessThanEqual
:
2976 case SpvOpFOrdLessThanEqual
:
2977 case SpvOpFUnordLessThanEqual
:
2978 case SpvOpUGreaterThanEqual
:
2979 case SpvOpSGreaterThanEqual
:
2980 case SpvOpFOrdGreaterThanEqual
:
2981 case SpvOpFUnordGreaterThanEqual
:
2987 case SpvOpFwidthFine
:
2988 case SpvOpDPdxCoarse
:
2989 case SpvOpDPdyCoarse
:
2990 case SpvOpFwidthCoarse
:
2991 case SpvOpBitFieldInsert
:
2992 case SpvOpBitFieldSExtract
:
2993 case SpvOpBitFieldUExtract
:
2994 case SpvOpBitReverse
:
2996 case SpvOpTranspose
:
2997 case SpvOpOuterProduct
:
2998 case SpvOpMatrixTimesScalar
:
2999 case SpvOpVectorTimesMatrix
:
3000 case SpvOpMatrixTimesVector
:
3001 case SpvOpMatrixTimesMatrix
:
3002 vtn_handle_alu(b
, opcode
, w
, count
);
3005 case SpvOpVectorExtractDynamic
:
3006 case SpvOpVectorInsertDynamic
:
3007 case SpvOpVectorShuffle
:
3008 case SpvOpCompositeConstruct
:
3009 case SpvOpCompositeExtract
:
3010 case SpvOpCompositeInsert
:
3011 case SpvOpCopyObject
:
3012 vtn_handle_composite(b
, opcode
, w
, count
);
3015 case SpvOpEmitVertex
:
3016 case SpvOpEndPrimitive
:
3017 case SpvOpEmitStreamVertex
:
3018 case SpvOpEndStreamPrimitive
:
3019 case SpvOpControlBarrier
:
3020 case SpvOpMemoryBarrier
:
3021 vtn_handle_barrier(b
, opcode
, w
, count
);
3025 unreachable("Unhandled opcode");
3032 spirv_to_nir(const uint32_t *words
, size_t word_count
,
3033 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
3034 gl_shader_stage stage
, const char *entry_point_name
,
3035 const struct nir_spirv_supported_extensions
*ext
,
3036 const nir_shader_compiler_options
*options
)
3038 const uint32_t *word_end
= words
+ word_count
;
3040 /* Handle the SPIR-V header (first 4 dwords) */
3041 assert(word_count
> 5);
3043 assert(words
[0] == SpvMagicNumber
);
3044 assert(words
[1] >= 0x10000);
3045 /* words[2] == generator magic */
3046 unsigned value_id_bound
= words
[3];
3047 assert(words
[4] == 0);
3051 /* Initialize the stn_builder object */
3052 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
3053 b
->value_id_bound
= value_id_bound
;
3054 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
3055 exec_list_make_empty(&b
->functions
);
3056 b
->entry_point_stage
= stage
;
3057 b
->entry_point_name
= entry_point_name
;
3060 /* Handle all the preamble instructions */
3061 words
= vtn_foreach_instruction(b
, words
, word_end
,
3062 vtn_handle_preamble_instruction
);
3064 if (b
->entry_point
== NULL
) {
3065 assert(!"Entry point not found");
3070 b
->shader
= nir_shader_create(NULL
, stage
, options
, NULL
);
3072 /* Set shader info defaults */
3073 b
->shader
->info
->gs
.invocations
= 1;
3075 /* Parse execution modes */
3076 vtn_foreach_execution_mode(b
, b
->entry_point
,
3077 vtn_handle_execution_mode
, NULL
);
3079 b
->specializations
= spec
;
3080 b
->num_specializations
= num_spec
;
3082 /* Handle all variable, type, and constant instructions */
3083 words
= vtn_foreach_instruction(b
, words
, word_end
,
3084 vtn_handle_variable_or_type_instruction
);
3086 vtn_build_cfg(b
, words
, word_end
);
3088 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
3089 b
->impl
= func
->impl
;
3090 b
->const_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
3091 _mesa_key_pointer_equal
);
3093 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
3096 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
3097 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
3098 assert(entry_point
);