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 /* Handled as a second pass */
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");
574 /* Matrix strides are handled as a separate pass because we need to know
575 * whether the matrix is row-major or not first.
578 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
579 struct vtn_value
*val
, int member
,
580 const struct vtn_decoration
*dec
,
583 if (dec
->decoration
!= SpvDecorationMatrixStride
)
587 struct member_decoration_ctx
*ctx
= void_ctx
;
589 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
590 if (mat_type
->row_major
) {
591 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
592 mat_type
->stride
= mat_type
->array_element
->stride
;
593 mat_type
->array_element
->stride
= dec
->literals
[0];
595 assert(mat_type
->array_element
->stride
> 0);
596 mat_type
->stride
= dec
->literals
[0];
601 type_decoration_cb(struct vtn_builder
*b
,
602 struct vtn_value
*val
, int member
,
603 const struct vtn_decoration
*dec
, void *ctx
)
605 struct vtn_type
*type
= val
->type
;
610 switch (dec
->decoration
) {
611 case SpvDecorationArrayStride
:
612 type
->stride
= dec
->literals
[0];
614 case SpvDecorationBlock
:
617 case SpvDecorationBufferBlock
:
618 type
->buffer_block
= true;
620 case SpvDecorationGLSLShared
:
621 case SpvDecorationGLSLPacked
:
622 /* Ignore these, since we get explicit offsets anyways */
625 case SpvDecorationRowMajor
:
626 case SpvDecorationColMajor
:
627 case SpvDecorationMatrixStride
:
628 case SpvDecorationBuiltIn
:
629 case SpvDecorationNoPerspective
:
630 case SpvDecorationFlat
:
631 case SpvDecorationPatch
:
632 case SpvDecorationCentroid
:
633 case SpvDecorationSample
:
634 case SpvDecorationVolatile
:
635 case SpvDecorationCoherent
:
636 case SpvDecorationNonWritable
:
637 case SpvDecorationNonReadable
:
638 case SpvDecorationUniform
:
639 case SpvDecorationStream
:
640 case SpvDecorationLocation
:
641 case SpvDecorationComponent
:
642 case SpvDecorationOffset
:
643 case SpvDecorationXfbBuffer
:
644 case SpvDecorationXfbStride
:
645 vtn_warn("Decoration only allowed for struct members: %s",
646 spirv_decoration_to_string(dec
->decoration
));
649 case SpvDecorationRelaxedPrecision
:
650 case SpvDecorationSpecId
:
651 case SpvDecorationInvariant
:
652 case SpvDecorationRestrict
:
653 case SpvDecorationAliased
:
654 case SpvDecorationConstant
:
655 case SpvDecorationIndex
:
656 case SpvDecorationBinding
:
657 case SpvDecorationDescriptorSet
:
658 case SpvDecorationLinkageAttributes
:
659 case SpvDecorationNoContraction
:
660 case SpvDecorationInputAttachmentIndex
:
661 vtn_warn("Decoration not allowed on types: %s",
662 spirv_decoration_to_string(dec
->decoration
));
665 case SpvDecorationCPacked
:
666 case SpvDecorationSaturatedConversion
:
667 case SpvDecorationFuncParamAttr
:
668 case SpvDecorationFPRoundingMode
:
669 case SpvDecorationFPFastMathMode
:
670 case SpvDecorationAlignment
:
671 vtn_warn("Decoration only allowed for CL-style kernels: %s",
672 spirv_decoration_to_string(dec
->decoration
));
676 unreachable("Unhandled decoration");
681 translate_image_format(SpvImageFormat format
)
684 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
685 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
686 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
687 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
688 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
689 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
690 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
691 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
692 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
693 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
694 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
695 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
696 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
697 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
698 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
699 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
700 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
701 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
702 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
703 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
704 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
705 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
706 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
707 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
708 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
709 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
710 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
711 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
712 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
713 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
714 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
715 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
716 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
717 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
718 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
719 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
720 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
721 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
722 case SpvImageFormatR16ui
: return 0x823A; /* GL_RG16UI */
723 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
725 assert(!"Invalid image format");
731 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
732 const uint32_t *w
, unsigned count
)
734 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
736 val
->type
= rzalloc(b
, struct vtn_type
);
737 val
->type
->is_builtin
= false;
738 val
->type
->val
= val
;
742 val
->type
->type
= glsl_void_type();
745 val
->type
->type
= glsl_bool_type();
749 const bool signedness
= w
[3];
751 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
753 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
756 case SpvOpTypeFloat
: {
758 val
->type
->type
= bit_size
== 64 ? glsl_double_type() : glsl_float_type();
762 case SpvOpTypeVector
: {
763 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
764 unsigned elems
= w
[3];
766 assert(glsl_type_is_scalar(base
->type
));
767 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
769 /* Vectors implicitly have sizeof(base_type) stride. For now, this
770 * is always 4 bytes. This will have to change if we want to start
771 * supporting doubles or half-floats.
773 val
->type
->stride
= glsl_get_bit_size(base
->type
) / 8;
774 val
->type
->array_element
= base
;
778 case SpvOpTypeMatrix
: {
779 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
780 unsigned columns
= w
[3];
782 assert(glsl_type_is_vector(base
->type
));
783 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
784 glsl_get_vector_elements(base
->type
),
786 assert(!glsl_type_is_error(val
->type
->type
));
787 val
->type
->array_element
= base
;
788 val
->type
->row_major
= false;
789 val
->type
->stride
= 0;
793 case SpvOpTypeRuntimeArray
:
794 case SpvOpTypeArray
: {
795 struct vtn_type
*array_element
=
796 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
799 if (opcode
== SpvOpTypeRuntimeArray
) {
800 /* A length of 0 is used to denote unsized arrays */
804 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
807 val
->type
->type
= glsl_array_type(array_element
->type
, length
);
808 val
->type
->array_element
= array_element
;
809 val
->type
->stride
= 0;
813 case SpvOpTypeStruct
: {
814 unsigned num_fields
= count
- 2;
815 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
816 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
818 NIR_VLA(struct glsl_struct_field
, fields
, count
);
819 for (unsigned i
= 0; i
< num_fields
; i
++) {
820 val
->type
->members
[i
] =
821 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
822 fields
[i
] = (struct glsl_struct_field
) {
823 .type
= val
->type
->members
[i
]->type
,
824 .name
= ralloc_asprintf(b
, "field%d", i
),
829 struct member_decoration_ctx ctx
= {
830 .num_fields
= num_fields
,
835 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
836 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
838 const char *name
= val
->name
? val
->name
: "struct";
840 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
);
844 case SpvOpTypeFunction
: {
845 const struct glsl_type
*return_type
=
846 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
847 NIR_VLA(struct glsl_function_param
, params
, count
- 3);
848 for (unsigned i
= 0; i
< count
- 3; i
++) {
849 params
[i
].type
= vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
->type
;
853 params
[i
].out
= true;
855 val
->type
->type
= glsl_function_type(return_type
, params
, count
- 3);
859 case SpvOpTypePointer
:
860 /* FIXME: For now, we'll just do the really lame thing and return
861 * the same type. The validator should ensure that the proper number
862 * of dereferences happen
864 val
->type
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
867 case SpvOpTypeImage
: {
868 const struct glsl_type
*sampled_type
=
869 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
871 assert(glsl_type_is_vector_or_scalar(sampled_type
));
873 enum glsl_sampler_dim dim
;
874 switch ((SpvDim
)w
[3]) {
875 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
876 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
877 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
878 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
879 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
880 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
881 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
883 unreachable("Invalid SPIR-V Sampler dimension");
886 bool is_shadow
= w
[4];
887 bool is_array
= w
[5];
888 bool multisampled
= w
[6];
889 unsigned sampled
= w
[7];
890 SpvImageFormat format
= w
[8];
893 val
->type
->access_qualifier
= w
[9];
895 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
898 if (dim
== GLSL_SAMPLER_DIM_2D
)
899 dim
= GLSL_SAMPLER_DIM_MS
;
900 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
901 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
903 assert(!"Unsupported multisampled image type");
906 val
->type
->image_format
= translate_image_format(format
);
909 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
910 glsl_get_base_type(sampled_type
));
911 } else if (sampled
== 2) {
913 val
->type
->type
= glsl_image_type(dim
, is_array
,
914 glsl_get_base_type(sampled_type
));
916 assert(!"We need to know if the image will be sampled");
921 case SpvOpTypeSampledImage
:
922 val
->type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
925 case SpvOpTypeSampler
:
926 /* The actual sampler type here doesn't really matter. It gets
927 * thrown away the moment you combine it with an image. What really
928 * matters is that it's a sampler type as opposed to an integer type
929 * so the backend knows what to do.
931 val
->type
->type
= glsl_bare_sampler_type();
934 case SpvOpTypeOpaque
:
936 case SpvOpTypeDeviceEvent
:
937 case SpvOpTypeReserveId
:
941 unreachable("Unhandled opcode");
944 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
947 static nir_constant
*
948 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
950 nir_constant
*c
= rzalloc(b
, nir_constant
);
952 switch (glsl_get_base_type(type
)) {
955 case GLSL_TYPE_INT64
:
956 case GLSL_TYPE_UINT64
:
958 case GLSL_TYPE_FLOAT
:
959 case GLSL_TYPE_DOUBLE
:
960 /* Nothing to do here. It's already initialized to zero */
963 case GLSL_TYPE_ARRAY
:
964 assert(glsl_get_length(type
) > 0);
965 c
->num_elements
= glsl_get_length(type
);
966 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
968 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
969 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
970 c
->elements
[i
] = c
->elements
[0];
973 case GLSL_TYPE_STRUCT
:
974 c
->num_elements
= glsl_get_length(type
);
975 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
977 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
978 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
983 unreachable("Invalid type for null constant");
990 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
991 int member
, const struct vtn_decoration
*dec
,
994 assert(member
== -1);
995 if (dec
->decoration
!= SpvDecorationSpecId
)
998 struct spec_constant_value
*const_value
= data
;
1000 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1001 if (b
->specializations
[i
].id
== dec
->literals
[0]) {
1002 if (const_value
->is_double
)
1003 const_value
->data64
= b
->specializations
[i
].data64
;
1005 const_value
->data32
= b
->specializations
[i
].data32
;
1012 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
1013 uint32_t const_value
)
1015 struct spec_constant_value data
;
1016 data
.is_double
= false;
1017 data
.data32
= const_value
;
1018 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1023 get_specialization64(struct vtn_builder
*b
, struct vtn_value
*val
,
1024 uint64_t const_value
)
1026 struct spec_constant_value data
;
1027 data
.is_double
= true;
1028 data
.data64
= const_value
;
1029 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1034 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1035 struct vtn_value
*val
,
1037 const struct vtn_decoration
*dec
,
1040 assert(member
== -1);
1041 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1042 dec
->literals
[0] != SpvBuiltInWorkgroupSize
)
1045 assert(val
->const_type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1047 b
->shader
->info
.cs
.local_size
[0] = val
->constant
->values
[0].u32
[0];
1048 b
->shader
->info
.cs
.local_size
[1] = val
->constant
->values
[0].u32
[1];
1049 b
->shader
->info
.cs
.local_size
[2] = val
->constant
->values
[0].u32
[2];
1053 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1054 const uint32_t *w
, unsigned count
)
1056 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1057 val
->const_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
1058 val
->constant
= rzalloc(b
, nir_constant
);
1060 case SpvOpConstantTrue
:
1061 assert(val
->const_type
== glsl_bool_type());
1062 val
->constant
->values
[0].u32
[0] = NIR_TRUE
;
1064 case SpvOpConstantFalse
:
1065 assert(val
->const_type
== glsl_bool_type());
1066 val
->constant
->values
[0].u32
[0] = NIR_FALSE
;
1069 case SpvOpSpecConstantTrue
:
1070 case SpvOpSpecConstantFalse
: {
1071 assert(val
->const_type
== glsl_bool_type());
1073 get_specialization(b
, val
, (opcode
== SpvOpSpecConstantTrue
));
1074 val
->constant
->values
[0].u32
[0] = int_val
? NIR_TRUE
: NIR_FALSE
;
1078 case SpvOpConstant
: {
1079 assert(glsl_type_is_scalar(val
->const_type
));
1080 int bit_size
= glsl_get_bit_size(val
->const_type
);
1081 if (bit_size
== 64) {
1082 val
->constant
->values
->u32
[0] = w
[3];
1083 val
->constant
->values
->u32
[1] = w
[4];
1085 assert(bit_size
== 32);
1086 val
->constant
->values
->u32
[0] = w
[3];
1090 case SpvOpSpecConstant
: {
1091 assert(glsl_type_is_scalar(val
->const_type
));
1092 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1093 int bit_size
= glsl_get_bit_size(val
->const_type
);
1095 val
->constant
->values
[0].u64
[0] =
1096 get_specialization64(b
, val
, vtn_u64_literal(&w
[3]));
1098 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1101 case SpvOpSpecConstantComposite
:
1102 case SpvOpConstantComposite
: {
1103 unsigned elem_count
= count
- 3;
1104 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1105 for (unsigned i
= 0; i
< elem_count
; i
++)
1106 elems
[i
] = vtn_value(b
, w
[i
+ 3], vtn_value_type_constant
)->constant
;
1108 switch (glsl_get_base_type(val
->const_type
)) {
1109 case GLSL_TYPE_UINT
:
1111 case GLSL_TYPE_UINT64
:
1112 case GLSL_TYPE_INT64
:
1113 case GLSL_TYPE_FLOAT
:
1114 case GLSL_TYPE_BOOL
:
1115 case GLSL_TYPE_DOUBLE
: {
1116 int bit_size
= glsl_get_bit_size(val
->const_type
);
1117 if (glsl_type_is_matrix(val
->const_type
)) {
1118 assert(glsl_get_matrix_columns(val
->const_type
) == elem_count
);
1119 for (unsigned i
= 0; i
< elem_count
; i
++)
1120 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1122 assert(glsl_type_is_vector(val
->const_type
));
1123 assert(glsl_get_vector_elements(val
->const_type
) == elem_count
);
1124 for (unsigned i
= 0; i
< elem_count
; i
++) {
1125 if (bit_size
== 64) {
1126 val
->constant
->values
[0].u64
[i
] = elems
[i
]->values
[0].u64
[0];
1128 assert(bit_size
== 32);
1129 val
->constant
->values
[0].u32
[i
] = elems
[i
]->values
[0].u32
[0];
1136 case GLSL_TYPE_STRUCT
:
1137 case GLSL_TYPE_ARRAY
:
1138 ralloc_steal(val
->constant
, elems
);
1139 val
->constant
->num_elements
= elem_count
;
1140 val
->constant
->elements
= elems
;
1144 unreachable("Unsupported type for constants");
1149 case SpvOpSpecConstantOp
: {
1150 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1152 case SpvOpVectorShuffle
: {
1153 struct vtn_value
*v0
= &b
->values
[w
[4]];
1154 struct vtn_value
*v1
= &b
->values
[w
[5]];
1156 assert(v0
->value_type
== vtn_value_type_constant
||
1157 v0
->value_type
== vtn_value_type_undef
);
1158 assert(v1
->value_type
== vtn_value_type_constant
||
1159 v1
->value_type
== vtn_value_type_undef
);
1161 unsigned len0
= v0
->value_type
== vtn_value_type_constant
?
1162 glsl_get_vector_elements(v0
->const_type
) :
1163 glsl_get_vector_elements(v0
->type
->type
);
1164 unsigned len1
= v1
->value_type
== vtn_value_type_constant
?
1165 glsl_get_vector_elements(v1
->const_type
) :
1166 glsl_get_vector_elements(v1
->type
->type
);
1168 assert(len0
+ len1
< 16);
1170 unsigned bit_size
= glsl_get_bit_size(val
->const_type
);
1171 unsigned bit_size0
= v0
->value_type
== vtn_value_type_constant
?
1172 glsl_get_bit_size(v0
->const_type
) :
1173 glsl_get_bit_size(v0
->type
->type
);
1174 unsigned bit_size1
= v1
->value_type
== vtn_value_type_constant
?
1175 glsl_get_bit_size(v1
->const_type
) :
1176 glsl_get_bit_size(v1
->type
->type
);
1178 assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1179 (void)bit_size0
; (void)bit_size1
;
1181 if (bit_size
== 64) {
1183 if (v0
->value_type
== vtn_value_type_constant
) {
1184 for (unsigned i
= 0; i
< len0
; i
++)
1185 u64
[i
] = v0
->constant
->values
[0].u64
[i
];
1187 if (v1
->value_type
== vtn_value_type_constant
) {
1188 for (unsigned i
= 0; i
< len1
; i
++)
1189 u64
[len0
+ i
] = v1
->constant
->values
[0].u64
[i
];
1192 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1193 uint32_t comp
= w
[i
+ 6];
1194 /* If component is not used, set the value to a known constant
1195 * to detect if it is wrongly used.
1197 if (comp
== (uint32_t)-1)
1198 val
->constant
->values
[0].u64
[j
] = 0xdeadbeefdeadbeef;
1200 val
->constant
->values
[0].u64
[j
] = u64
[comp
];
1204 if (v0
->value_type
== vtn_value_type_constant
) {
1205 for (unsigned i
= 0; i
< len0
; i
++)
1206 u32
[i
] = v0
->constant
->values
[0].u32
[i
];
1208 if (v1
->value_type
== vtn_value_type_constant
) {
1209 for (unsigned i
= 0; i
< len1
; i
++)
1210 u32
[len0
+ i
] = v1
->constant
->values
[0].u32
[i
];
1213 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1214 uint32_t comp
= w
[i
+ 6];
1215 /* If component is not used, set the value to a known constant
1216 * to detect if it is wrongly used.
1218 if (comp
== (uint32_t)-1)
1219 val
->constant
->values
[0].u32
[j
] = 0xdeadbeef;
1221 val
->constant
->values
[0].u32
[j
] = u32
[comp
];
1227 case SpvOpCompositeExtract
:
1228 case SpvOpCompositeInsert
: {
1229 struct vtn_value
*comp
;
1230 unsigned deref_start
;
1231 struct nir_constant
**c
;
1232 if (opcode
== SpvOpCompositeExtract
) {
1233 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1235 c
= &comp
->constant
;
1237 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1239 val
->constant
= nir_constant_clone(comp
->constant
,
1246 const struct glsl_type
*type
= comp
->const_type
;
1247 for (unsigned i
= deref_start
; i
< count
; i
++) {
1248 switch (glsl_get_base_type(type
)) {
1249 case GLSL_TYPE_UINT
:
1251 case GLSL_TYPE_UINT64
:
1252 case GLSL_TYPE_INT64
:
1253 case GLSL_TYPE_FLOAT
:
1254 case GLSL_TYPE_DOUBLE
:
1255 case GLSL_TYPE_BOOL
:
1256 /* If we hit this granularity, we're picking off an element */
1257 if (glsl_type_is_matrix(type
)) {
1258 assert(col
== 0 && elem
== -1);
1261 type
= glsl_get_column_type(type
);
1263 assert(elem
<= 0 && glsl_type_is_vector(type
));
1265 type
= glsl_scalar_type(glsl_get_base_type(type
));
1269 case GLSL_TYPE_ARRAY
:
1270 c
= &(*c
)->elements
[w
[i
]];
1271 type
= glsl_get_array_element(type
);
1274 case GLSL_TYPE_STRUCT
:
1275 c
= &(*c
)->elements
[w
[i
]];
1276 type
= glsl_get_struct_field(type
, w
[i
]);
1280 unreachable("Invalid constant type");
1284 if (opcode
== SpvOpCompositeExtract
) {
1288 unsigned num_components
= glsl_get_vector_elements(type
);
1289 unsigned bit_size
= glsl_get_bit_size(type
);
1290 for (unsigned i
= 0; i
< num_components
; i
++)
1291 if (bit_size
== 64) {
1292 val
->constant
->values
[0].u64
[i
] = (*c
)->values
[col
].u64
[elem
+ i
];
1294 assert(bit_size
== 32);
1295 val
->constant
->values
[0].u32
[i
] = (*c
)->values
[col
].u32
[elem
+ i
];
1299 struct vtn_value
*insert
=
1300 vtn_value(b
, w
[4], vtn_value_type_constant
);
1301 assert(insert
->const_type
== type
);
1303 *c
= insert
->constant
;
1305 unsigned num_components
= glsl_get_vector_elements(type
);
1306 unsigned bit_size
= glsl_get_bit_size(type
);
1307 for (unsigned i
= 0; i
< num_components
; i
++)
1308 if (bit_size
== 64) {
1309 (*c
)->values
[col
].u64
[elem
+ i
] = insert
->constant
->values
[0].u64
[i
];
1311 assert(bit_size
== 32);
1312 (*c
)->values
[col
].u32
[elem
+ i
] = insert
->constant
->values
[0].u32
[i
];
1321 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->const_type
);
1322 nir_alu_type src_alu_type
= dst_alu_type
;
1323 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(opcode
, &swap
, src_alu_type
, dst_alu_type
);
1325 unsigned num_components
= glsl_get_vector_elements(val
->const_type
);
1327 glsl_get_bit_size(val
->const_type
);
1329 nir_const_value src
[4];
1331 for (unsigned i
= 0; i
< count
- 4; i
++) {
1333 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
)->constant
;
1335 unsigned j
= swap
? 1 - i
: i
;
1336 assert(bit_size
== 32);
1337 src
[j
] = c
->values
[0];
1340 val
->constant
->values
[0] =
1341 nir_eval_const_opcode(op
, num_components
, bit_size
, src
);
1348 case SpvOpConstantNull
:
1349 val
->constant
= vtn_null_constant(b
, val
->const_type
);
1352 case SpvOpConstantSampler
:
1353 assert(!"OpConstantSampler requires Kernel Capability");
1357 unreachable("Unhandled opcode");
1360 /* Now that we have the value, update the workgroup size if needed */
1361 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1365 vtn_handle_function_call(struct vtn_builder
*b
, SpvOp opcode
,
1366 const uint32_t *w
, unsigned count
)
1368 struct nir_function
*callee
=
1369 vtn_value(b
, w
[3], vtn_value_type_function
)->func
->impl
->function
;
1371 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, callee
);
1372 for (unsigned i
= 0; i
< call
->num_params
; i
++) {
1373 unsigned arg_id
= w
[4 + i
];
1374 struct vtn_value
*arg
= vtn_untyped_value(b
, arg_id
);
1375 if (arg
->value_type
== vtn_value_type_pointer
) {
1376 nir_deref_var
*d
= vtn_pointer_to_deref(b
, arg
->pointer
);
1377 call
->params
[i
] = nir_deref_var_clone(d
, call
);
1379 struct vtn_ssa_value
*arg_ssa
= vtn_ssa_value(b
, arg_id
);
1381 /* Make a temporary to store the argument in */
1383 nir_local_variable_create(b
->impl
, arg_ssa
->type
, "arg_tmp");
1384 call
->params
[i
] = nir_deref_var_create(call
, tmp
);
1386 vtn_local_store(b
, arg_ssa
, call
->params
[i
]);
1390 nir_variable
*out_tmp
= NULL
;
1391 if (!glsl_type_is_void(callee
->return_type
)) {
1392 out_tmp
= nir_local_variable_create(b
->impl
, callee
->return_type
,
1394 call
->return_deref
= nir_deref_var_create(call
, out_tmp
);
1397 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
1399 if (glsl_type_is_void(callee
->return_type
)) {
1400 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1402 struct vtn_value
*retval
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1403 retval
->ssa
= vtn_local_load(b
, call
->return_deref
);
1407 struct vtn_ssa_value
*
1408 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1410 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1413 if (!glsl_type_is_vector_or_scalar(type
)) {
1414 unsigned elems
= glsl_get_length(type
);
1415 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1416 for (unsigned i
= 0; i
< elems
; i
++) {
1417 const struct glsl_type
*child_type
;
1419 switch (glsl_get_base_type(type
)) {
1421 case GLSL_TYPE_UINT
:
1422 case GLSL_TYPE_INT64
:
1423 case GLSL_TYPE_UINT64
:
1424 case GLSL_TYPE_BOOL
:
1425 case GLSL_TYPE_FLOAT
:
1426 case GLSL_TYPE_DOUBLE
:
1427 child_type
= glsl_get_column_type(type
);
1429 case GLSL_TYPE_ARRAY
:
1430 child_type
= glsl_get_array_element(type
);
1432 case GLSL_TYPE_STRUCT
:
1433 child_type
= glsl_get_struct_field(type
, i
);
1436 unreachable("unkown base type");
1439 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
1447 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
1450 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
1451 src
.src_type
= type
;
1456 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
1457 const uint32_t *w
, unsigned count
)
1459 if (opcode
== SpvOpSampledImage
) {
1460 struct vtn_value
*val
=
1461 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1462 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1463 val
->sampled_image
->image
=
1464 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1465 val
->sampled_image
->sampler
=
1466 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
1468 } else if (opcode
== SpvOpImage
) {
1469 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1470 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
1471 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
1472 val
->pointer
= src_val
->sampled_image
->image
;
1474 assert(src_val
->value_type
== vtn_value_type_pointer
);
1475 val
->pointer
= src_val
->pointer
;
1480 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1481 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1483 struct vtn_sampled_image sampled
;
1484 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
1485 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
1486 sampled
= *sampled_val
->sampled_image
;
1488 assert(sampled_val
->value_type
== vtn_value_type_pointer
);
1489 sampled
.image
= NULL
;
1490 sampled
.sampler
= sampled_val
->pointer
;
1493 const struct glsl_type
*image_type
;
1494 if (sampled
.image
) {
1495 image_type
= sampled
.image
->var
->var
->interface_type
;
1497 image_type
= sampled
.sampler
->var
->var
->interface_type
;
1499 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
1500 const bool is_array
= glsl_sampler_type_is_array(image_type
);
1501 const bool is_shadow
= glsl_sampler_type_is_shadow(image_type
);
1503 /* Figure out the base texture operation */
1506 case SpvOpImageSampleImplicitLod
:
1507 case SpvOpImageSampleDrefImplicitLod
:
1508 case SpvOpImageSampleProjImplicitLod
:
1509 case SpvOpImageSampleProjDrefImplicitLod
:
1510 texop
= nir_texop_tex
;
1513 case SpvOpImageSampleExplicitLod
:
1514 case SpvOpImageSampleDrefExplicitLod
:
1515 case SpvOpImageSampleProjExplicitLod
:
1516 case SpvOpImageSampleProjDrefExplicitLod
:
1517 texop
= nir_texop_txl
;
1520 case SpvOpImageFetch
:
1521 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
1522 texop
= nir_texop_txf_ms
;
1524 texop
= nir_texop_txf
;
1528 case SpvOpImageGather
:
1529 case SpvOpImageDrefGather
:
1530 texop
= nir_texop_tg4
;
1533 case SpvOpImageQuerySizeLod
:
1534 case SpvOpImageQuerySize
:
1535 texop
= nir_texop_txs
;
1538 case SpvOpImageQueryLod
:
1539 texop
= nir_texop_lod
;
1542 case SpvOpImageQueryLevels
:
1543 texop
= nir_texop_query_levels
;
1546 case SpvOpImageQuerySamples
:
1547 texop
= nir_texop_texture_samples
;
1551 unreachable("Unhandled opcode");
1554 nir_tex_src srcs
[8]; /* 8 should be enough */
1555 nir_tex_src
*p
= srcs
;
1559 struct nir_ssa_def
*coord
;
1560 unsigned coord_components
;
1562 case SpvOpImageSampleImplicitLod
:
1563 case SpvOpImageSampleExplicitLod
:
1564 case SpvOpImageSampleDrefImplicitLod
:
1565 case SpvOpImageSampleDrefExplicitLod
:
1566 case SpvOpImageSampleProjImplicitLod
:
1567 case SpvOpImageSampleProjExplicitLod
:
1568 case SpvOpImageSampleProjDrefImplicitLod
:
1569 case SpvOpImageSampleProjDrefExplicitLod
:
1570 case SpvOpImageFetch
:
1571 case SpvOpImageGather
:
1572 case SpvOpImageDrefGather
:
1573 case SpvOpImageQueryLod
: {
1574 /* All these types have the coordinate as their first real argument */
1575 switch (sampler_dim
) {
1576 case GLSL_SAMPLER_DIM_1D
:
1577 case GLSL_SAMPLER_DIM_BUF
:
1578 coord_components
= 1;
1580 case GLSL_SAMPLER_DIM_2D
:
1581 case GLSL_SAMPLER_DIM_RECT
:
1582 case GLSL_SAMPLER_DIM_MS
:
1583 coord_components
= 2;
1585 case GLSL_SAMPLER_DIM_3D
:
1586 case GLSL_SAMPLER_DIM_CUBE
:
1587 coord_components
= 3;
1590 unreachable("Invalid sampler type");
1593 if (is_array
&& texop
!= nir_texop_lod
)
1596 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
1597 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
1598 (1 << coord_components
) - 1));
1599 p
->src_type
= nir_tex_src_coord
;
1606 coord_components
= 0;
1611 case SpvOpImageSampleProjImplicitLod
:
1612 case SpvOpImageSampleProjExplicitLod
:
1613 case SpvOpImageSampleProjDrefImplicitLod
:
1614 case SpvOpImageSampleProjDrefExplicitLod
:
1615 /* These have the projector as the last coordinate component */
1616 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
1617 p
->src_type
= nir_tex_src_projector
;
1625 unsigned gather_component
= 0;
1627 case SpvOpImageSampleDrefImplicitLod
:
1628 case SpvOpImageSampleDrefExplicitLod
:
1629 case SpvOpImageSampleProjDrefImplicitLod
:
1630 case SpvOpImageSampleProjDrefExplicitLod
:
1631 case SpvOpImageDrefGather
:
1632 /* These all have an explicit depth value as their next source */
1633 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
1636 case SpvOpImageGather
:
1637 /* This has a component as its next source */
1639 vtn_value(b
, w
[idx
++], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
1646 /* For OpImageQuerySizeLod, we always have an LOD */
1647 if (opcode
== SpvOpImageQuerySizeLod
)
1648 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1650 /* Now we need to handle some number of optional arguments */
1651 const struct vtn_ssa_value
*gather_offsets
= NULL
;
1653 uint32_t operands
= w
[idx
++];
1655 if (operands
& SpvImageOperandsBiasMask
) {
1656 assert(texop
== nir_texop_tex
);
1657 texop
= nir_texop_txb
;
1658 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
1661 if (operands
& SpvImageOperandsLodMask
) {
1662 assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
1663 texop
== nir_texop_txs
);
1664 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1667 if (operands
& SpvImageOperandsGradMask
) {
1668 assert(texop
== nir_texop_txl
);
1669 texop
= nir_texop_txd
;
1670 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
1671 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
1674 if (operands
& SpvImageOperandsOffsetMask
||
1675 operands
& SpvImageOperandsConstOffsetMask
)
1676 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
1678 if (operands
& SpvImageOperandsConstOffsetsMask
) {
1679 gather_offsets
= vtn_ssa_value(b
, w
[idx
++]);
1680 (*p
++) = (nir_tex_src
){};
1683 if (operands
& SpvImageOperandsSampleMask
) {
1684 assert(texop
== nir_texop_txf_ms
);
1685 texop
= nir_texop_txf_ms
;
1686 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
1689 /* We should have now consumed exactly all of the arguments */
1690 assert(idx
== count
);
1692 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
1695 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1697 instr
->coord_components
= coord_components
;
1698 instr
->sampler_dim
= sampler_dim
;
1699 instr
->is_array
= is_array
;
1700 instr
->is_shadow
= is_shadow
;
1701 instr
->is_new_style_shadow
=
1702 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
1703 instr
->component
= gather_component
;
1705 switch (glsl_get_sampler_result_type(image_type
)) {
1706 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
1707 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
1708 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
1709 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
1711 unreachable("Invalid base type for sampler result");
1714 nir_deref_var
*sampler
= vtn_pointer_to_deref(b
, sampled
.sampler
);
1715 nir_deref_var
*texture
;
1716 if (sampled
.image
) {
1717 nir_deref_var
*image
= vtn_pointer_to_deref(b
, sampled
.image
);
1723 instr
->texture
= nir_deref_var_clone(texture
, instr
);
1725 switch (instr
->op
) {
1730 /* These operations require a sampler */
1731 instr
->sampler
= nir_deref_var_clone(sampler
, instr
);
1734 case nir_texop_txf_ms
:
1738 case nir_texop_query_levels
:
1739 case nir_texop_texture_samples
:
1740 case nir_texop_samples_identical
:
1742 instr
->sampler
= NULL
;
1744 case nir_texop_txf_ms_mcs
:
1745 unreachable("unexpected nir_texop_txf_ms_mcs");
1748 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1749 nir_tex_instr_dest_size(instr
), 32, NULL
);
1751 assert(glsl_get_vector_elements(ret_type
->type
) ==
1752 nir_tex_instr_dest_size(instr
));
1755 nir_instr
*instruction
;
1756 if (gather_offsets
) {
1757 assert(glsl_get_base_type(gather_offsets
->type
) == GLSL_TYPE_ARRAY
);
1758 assert(glsl_get_length(gather_offsets
->type
) == 4);
1759 nir_tex_instr
*instrs
[4] = {instr
, NULL
, NULL
, NULL
};
1761 /* Copy the current instruction 4x */
1762 for (uint32_t i
= 1; i
< 4; i
++) {
1763 instrs
[i
] = nir_tex_instr_create(b
->shader
, instr
->num_srcs
);
1764 instrs
[i
]->op
= instr
->op
;
1765 instrs
[i
]->coord_components
= instr
->coord_components
;
1766 instrs
[i
]->sampler_dim
= instr
->sampler_dim
;
1767 instrs
[i
]->is_array
= instr
->is_array
;
1768 instrs
[i
]->is_shadow
= instr
->is_shadow
;
1769 instrs
[i
]->is_new_style_shadow
= instr
->is_new_style_shadow
;
1770 instrs
[i
]->component
= instr
->component
;
1771 instrs
[i
]->dest_type
= instr
->dest_type
;
1772 instrs
[i
]->texture
= nir_deref_var_clone(texture
, instrs
[i
]);
1773 instrs
[i
]->sampler
= NULL
;
1775 memcpy(instrs
[i
]->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1777 nir_ssa_dest_init(&instrs
[i
]->instr
, &instrs
[i
]->dest
,
1778 nir_tex_instr_dest_size(instr
), 32, NULL
);
1781 /* Fill in the last argument with the offset from the passed in offsets
1782 * and insert the instruction into the stream.
1784 for (uint32_t i
= 0; i
< 4; i
++) {
1786 src
.src
= nir_src_for_ssa(gather_offsets
->elems
[i
]->def
);
1787 src
.src_type
= nir_tex_src_offset
;
1788 instrs
[i
]->src
[instrs
[i
]->num_srcs
- 1] = src
;
1789 nir_builder_instr_insert(&b
->nb
, &instrs
[i
]->instr
);
1792 /* Combine the results of the 4 instructions by taking their .w
1795 nir_alu_instr
*vec4
= nir_alu_instr_create(b
->shader
, nir_op_vec4
);
1796 nir_ssa_dest_init(&vec4
->instr
, &vec4
->dest
.dest
, 4, 32, NULL
);
1797 vec4
->dest
.write_mask
= 0xf;
1798 for (uint32_t i
= 0; i
< 4; i
++) {
1799 vec4
->src
[i
].src
= nir_src_for_ssa(&instrs
[i
]->dest
.ssa
);
1800 vec4
->src
[i
].swizzle
[0] = 3;
1802 def
= &vec4
->dest
.dest
.ssa
;
1803 instruction
= &vec4
->instr
;
1805 def
= &instr
->dest
.ssa
;
1806 instruction
= &instr
->instr
;
1809 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
1810 val
->ssa
->def
= def
;
1812 nir_builder_instr_insert(&b
->nb
, instruction
);
1816 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
1817 const uint32_t *w
, nir_src
*src
)
1820 case SpvOpAtomicIIncrement
:
1821 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
1824 case SpvOpAtomicIDecrement
:
1825 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
1828 case SpvOpAtomicISub
:
1830 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
1833 case SpvOpAtomicCompareExchange
:
1834 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
1835 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
1838 case SpvOpAtomicExchange
:
1839 case SpvOpAtomicIAdd
:
1840 case SpvOpAtomicSMin
:
1841 case SpvOpAtomicUMin
:
1842 case SpvOpAtomicSMax
:
1843 case SpvOpAtomicUMax
:
1844 case SpvOpAtomicAnd
:
1846 case SpvOpAtomicXor
:
1847 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1851 unreachable("Invalid SPIR-V atomic");
1855 static nir_ssa_def
*
1856 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
1858 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
1860 /* The image_load_store intrinsics assume a 4-dim coordinate */
1861 unsigned dim
= glsl_get_vector_elements(coord
->type
);
1862 unsigned swizzle
[4];
1863 for (unsigned i
= 0; i
< 4; i
++)
1864 swizzle
[i
] = MIN2(i
, dim
- 1);
1866 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
1870 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
1871 const uint32_t *w
, unsigned count
)
1873 /* Just get this one out of the way */
1874 if (opcode
== SpvOpImageTexelPointer
) {
1875 struct vtn_value
*val
=
1876 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
1877 val
->image
= ralloc(b
, struct vtn_image_pointer
);
1879 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1880 val
->image
->coord
= get_image_coord(b
, w
[4]);
1881 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
1885 struct vtn_image_pointer image
;
1888 case SpvOpAtomicExchange
:
1889 case SpvOpAtomicCompareExchange
:
1890 case SpvOpAtomicCompareExchangeWeak
:
1891 case SpvOpAtomicIIncrement
:
1892 case SpvOpAtomicIDecrement
:
1893 case SpvOpAtomicIAdd
:
1894 case SpvOpAtomicISub
:
1895 case SpvOpAtomicLoad
:
1896 case SpvOpAtomicSMin
:
1897 case SpvOpAtomicUMin
:
1898 case SpvOpAtomicSMax
:
1899 case SpvOpAtomicUMax
:
1900 case SpvOpAtomicAnd
:
1902 case SpvOpAtomicXor
:
1903 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
1906 case SpvOpAtomicStore
:
1907 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
1910 case SpvOpImageQuerySize
:
1911 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1913 image
.sample
= NULL
;
1916 case SpvOpImageRead
:
1917 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1918 image
.coord
= get_image_coord(b
, w
[4]);
1920 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
1921 assert(w
[5] == SpvImageOperandsSampleMask
);
1922 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
1924 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1928 case SpvOpImageWrite
:
1929 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
1930 image
.coord
= get_image_coord(b
, w
[2]);
1934 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
1935 assert(w
[4] == SpvImageOperandsSampleMask
);
1936 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
1938 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1943 unreachable("Invalid image opcode");
1946 nir_intrinsic_op op
;
1948 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_##N; break;
1949 OP(ImageQuerySize
, size
)
1951 OP(ImageWrite
, store
)
1952 OP(AtomicLoad
, load
)
1953 OP(AtomicStore
, store
)
1954 OP(AtomicExchange
, atomic_exchange
)
1955 OP(AtomicCompareExchange
, atomic_comp_swap
)
1956 OP(AtomicIIncrement
, atomic_add
)
1957 OP(AtomicIDecrement
, atomic_add
)
1958 OP(AtomicIAdd
, atomic_add
)
1959 OP(AtomicISub
, atomic_add
)
1960 OP(AtomicSMin
, atomic_min
)
1961 OP(AtomicUMin
, atomic_min
)
1962 OP(AtomicSMax
, atomic_max
)
1963 OP(AtomicUMax
, atomic_max
)
1964 OP(AtomicAnd
, atomic_and
)
1965 OP(AtomicOr
, atomic_or
)
1966 OP(AtomicXor
, atomic_xor
)
1969 unreachable("Invalid image opcode");
1972 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
1974 nir_deref_var
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
1975 intrin
->variables
[0] = nir_deref_var_clone(image_deref
, intrin
);
1977 /* ImageQuerySize doesn't take any extra parameters */
1978 if (opcode
!= SpvOpImageQuerySize
) {
1979 /* The image coordinate is always 4 components but we may not have that
1980 * many. Swizzle to compensate.
1983 for (unsigned i
= 0; i
< 4; i
++)
1984 swiz
[i
] = i
< image
.coord
->num_components
? i
: 0;
1985 intrin
->src
[0] = nir_src_for_ssa(nir_swizzle(&b
->nb
, image
.coord
,
1987 intrin
->src
[1] = nir_src_for_ssa(image
.sample
);
1991 case SpvOpAtomicLoad
:
1992 case SpvOpImageQuerySize
:
1993 case SpvOpImageRead
:
1995 case SpvOpAtomicStore
:
1996 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
1998 case SpvOpImageWrite
:
1999 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[3])->def
);
2002 case SpvOpAtomicIIncrement
:
2003 case SpvOpAtomicIDecrement
:
2004 case SpvOpAtomicExchange
:
2005 case SpvOpAtomicIAdd
:
2006 case SpvOpAtomicSMin
:
2007 case SpvOpAtomicUMin
:
2008 case SpvOpAtomicSMax
:
2009 case SpvOpAtomicUMax
:
2010 case SpvOpAtomicAnd
:
2012 case SpvOpAtomicXor
:
2013 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[2]);
2017 unreachable("Invalid image opcode");
2020 if (opcode
!= SpvOpImageWrite
) {
2021 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2022 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2024 unsigned dest_components
=
2025 nir_intrinsic_infos
[intrin
->intrinsic
].dest_components
;
2026 if (intrin
->intrinsic
== nir_intrinsic_image_size
) {
2027 dest_components
= intrin
->num_components
=
2028 glsl_get_vector_elements(type
->type
);
2031 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2032 dest_components
, 32, NULL
);
2034 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2036 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
2037 val
->ssa
->def
= &intrin
->dest
.ssa
;
2039 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2043 static nir_intrinsic_op
2044 get_ssbo_nir_atomic_op(SpvOp opcode
)
2047 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2048 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2049 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2050 OP(AtomicExchange
, atomic_exchange
)
2051 OP(AtomicCompareExchange
, atomic_comp_swap
)
2052 OP(AtomicIIncrement
, atomic_add
)
2053 OP(AtomicIDecrement
, atomic_add
)
2054 OP(AtomicIAdd
, atomic_add
)
2055 OP(AtomicISub
, atomic_add
)
2056 OP(AtomicSMin
, atomic_imin
)
2057 OP(AtomicUMin
, atomic_umin
)
2058 OP(AtomicSMax
, atomic_imax
)
2059 OP(AtomicUMax
, atomic_umax
)
2060 OP(AtomicAnd
, atomic_and
)
2061 OP(AtomicOr
, atomic_or
)
2062 OP(AtomicXor
, atomic_xor
)
2065 unreachable("Invalid SSBO atomic");
2069 static nir_intrinsic_op
2070 get_shared_nir_atomic_op(SpvOp opcode
)
2073 case SpvOpAtomicLoad
: return nir_intrinsic_load_var
;
2074 case SpvOpAtomicStore
: return nir_intrinsic_store_var
;
2075 #define OP(S, N) case SpvOp##S: return nir_intrinsic_var_##N;
2076 OP(AtomicExchange
, atomic_exchange
)
2077 OP(AtomicCompareExchange
, atomic_comp_swap
)
2078 OP(AtomicIIncrement
, atomic_add
)
2079 OP(AtomicIDecrement
, atomic_add
)
2080 OP(AtomicIAdd
, atomic_add
)
2081 OP(AtomicISub
, atomic_add
)
2082 OP(AtomicSMin
, atomic_imin
)
2083 OP(AtomicUMin
, atomic_umin
)
2084 OP(AtomicSMax
, atomic_imax
)
2085 OP(AtomicUMax
, atomic_umax
)
2086 OP(AtomicAnd
, atomic_and
)
2087 OP(AtomicOr
, atomic_or
)
2088 OP(AtomicXor
, atomic_xor
)
2091 unreachable("Invalid shared atomic");
2096 vtn_handle_ssbo_or_shared_atomic(struct vtn_builder
*b
, SpvOp opcode
,
2097 const uint32_t *w
, unsigned count
)
2099 struct vtn_pointer
*ptr
;
2100 nir_intrinsic_instr
*atomic
;
2103 case SpvOpAtomicLoad
:
2104 case SpvOpAtomicExchange
:
2105 case SpvOpAtomicCompareExchange
:
2106 case SpvOpAtomicCompareExchangeWeak
:
2107 case SpvOpAtomicIIncrement
:
2108 case SpvOpAtomicIDecrement
:
2109 case SpvOpAtomicIAdd
:
2110 case SpvOpAtomicISub
:
2111 case SpvOpAtomicSMin
:
2112 case SpvOpAtomicUMin
:
2113 case SpvOpAtomicSMax
:
2114 case SpvOpAtomicUMax
:
2115 case SpvOpAtomicAnd
:
2117 case SpvOpAtomicXor
:
2118 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2121 case SpvOpAtomicStore
:
2122 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2126 unreachable("Invalid SPIR-V atomic");
2130 SpvScope scope = w[4];
2131 SpvMemorySemanticsMask semantics = w[5];
2134 if (ptr
->mode
== vtn_variable_mode_workgroup
) {
2135 nir_deref_var
*deref
= vtn_pointer_to_deref(b
, ptr
);
2136 const struct glsl_type
*deref_type
= nir_deref_tail(&deref
->deref
)->type
;
2137 nir_intrinsic_op op
= get_shared_nir_atomic_op(opcode
);
2138 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2139 atomic
->variables
[0] = nir_deref_var_clone(deref
, atomic
);
2142 case SpvOpAtomicLoad
:
2143 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2146 case SpvOpAtomicStore
:
2147 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2148 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2149 atomic
->src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2152 case SpvOpAtomicExchange
:
2153 case SpvOpAtomicCompareExchange
:
2154 case SpvOpAtomicCompareExchangeWeak
:
2155 case SpvOpAtomicIIncrement
:
2156 case SpvOpAtomicIDecrement
:
2157 case SpvOpAtomicIAdd
:
2158 case SpvOpAtomicISub
:
2159 case SpvOpAtomicSMin
:
2160 case SpvOpAtomicUMin
:
2161 case SpvOpAtomicSMax
:
2162 case SpvOpAtomicUMax
:
2163 case SpvOpAtomicAnd
:
2165 case SpvOpAtomicXor
:
2166 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[0]);
2170 unreachable("Invalid SPIR-V atomic");
2174 assert(ptr
->mode
== vtn_variable_mode_ssbo
);
2175 nir_ssa_def
*offset
, *index
;
2176 struct vtn_type
*type
;
2177 offset
= vtn_pointer_to_offset(b
, ptr
, &index
, &type
, NULL
, false);
2179 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(opcode
);
2181 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2184 case SpvOpAtomicLoad
:
2185 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2186 atomic
->src
[0] = nir_src_for_ssa(index
);
2187 atomic
->src
[1] = nir_src_for_ssa(offset
);
2190 case SpvOpAtomicStore
:
2191 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2192 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2193 atomic
->src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2194 atomic
->src
[1] = nir_src_for_ssa(index
);
2195 atomic
->src
[2] = nir_src_for_ssa(offset
);
2198 case SpvOpAtomicExchange
:
2199 case SpvOpAtomicCompareExchange
:
2200 case SpvOpAtomicCompareExchangeWeak
:
2201 case SpvOpAtomicIIncrement
:
2202 case SpvOpAtomicIDecrement
:
2203 case SpvOpAtomicIAdd
:
2204 case SpvOpAtomicISub
:
2205 case SpvOpAtomicSMin
:
2206 case SpvOpAtomicUMin
:
2207 case SpvOpAtomicSMax
:
2208 case SpvOpAtomicUMax
:
2209 case SpvOpAtomicAnd
:
2211 case SpvOpAtomicXor
:
2212 atomic
->src
[0] = nir_src_for_ssa(index
);
2213 atomic
->src
[1] = nir_src_for_ssa(offset
);
2214 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[2]);
2218 unreachable("Invalid SPIR-V atomic");
2222 if (opcode
!= SpvOpAtomicStore
) {
2223 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2225 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
2226 glsl_get_vector_elements(type
->type
),
2227 glsl_get_bit_size(type
->type
), NULL
);
2229 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2230 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
2231 val
->ssa
->def
= &atomic
->dest
.ssa
;
2232 val
->ssa
->type
= type
->type
;
2235 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
2238 static nir_alu_instr
*
2239 create_vec(nir_shader
*shader
, unsigned num_components
, unsigned bit_size
)
2242 switch (num_components
) {
2243 case 1: op
= nir_op_fmov
; break;
2244 case 2: op
= nir_op_vec2
; break;
2245 case 3: op
= nir_op_vec3
; break;
2246 case 4: op
= nir_op_vec4
; break;
2247 default: unreachable("bad vector size");
2250 nir_alu_instr
*vec
= nir_alu_instr_create(shader
, op
);
2251 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
2253 vec
->dest
.write_mask
= (1 << num_components
) - 1;
2258 struct vtn_ssa_value
*
2259 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
2261 if (src
->transposed
)
2262 return src
->transposed
;
2264 struct vtn_ssa_value
*dest
=
2265 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
2267 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
2268 nir_alu_instr
*vec
= create_vec(b
->shader
,
2269 glsl_get_matrix_columns(src
->type
),
2270 glsl_get_bit_size(src
->type
));
2271 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2272 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
2273 vec
->src
[0].swizzle
[0] = i
;
2275 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
2276 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
2277 vec
->src
[j
].swizzle
[0] = i
;
2280 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2281 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
2284 dest
->transposed
= src
;
2290 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
2292 unsigned swiz
[4] = { index
};
2293 return nir_swizzle(&b
->nb
, src
, swiz
, 1, true);
2297 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
2300 nir_alu_instr
*vec
= create_vec(b
->shader
, src
->num_components
,
2303 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
2305 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
2307 vec
->src
[i
].src
= nir_src_for_ssa(src
);
2308 vec
->src
[i
].swizzle
[0] = i
;
2312 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2314 return &vec
->dest
.dest
.ssa
;
2318 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2321 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
2322 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2323 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2324 vtn_vector_extract(b
, src
, i
), dest
);
2330 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2331 nir_ssa_def
*insert
, nir_ssa_def
*index
)
2333 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
2334 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2335 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2336 vtn_vector_insert(b
, src
, insert
, i
), dest
);
2341 static nir_ssa_def
*
2342 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
2343 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
2344 const uint32_t *indices
)
2346 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
, src0
->bit_size
);
2348 for (unsigned i
= 0; i
< num_components
; i
++) {
2349 uint32_t index
= indices
[i
];
2350 if (index
== 0xffffffff) {
2352 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
2353 } else if (index
< src0
->num_components
) {
2354 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
2355 vec
->src
[i
].swizzle
[0] = index
;
2357 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
2358 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
2362 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2364 return &vec
->dest
.dest
.ssa
;
2368 * Concatentates a number of vectors/scalars together to produce a vector
2370 static nir_ssa_def
*
2371 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
2372 unsigned num_srcs
, nir_ssa_def
**srcs
)
2374 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
,
2377 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2379 * "When constructing a vector, there must be at least two Constituent
2382 assert(num_srcs
>= 2);
2384 unsigned dest_idx
= 0;
2385 for (unsigned i
= 0; i
< num_srcs
; i
++) {
2386 nir_ssa_def
*src
= srcs
[i
];
2387 assert(dest_idx
+ src
->num_components
<= num_components
);
2388 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
2389 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
2390 vec
->src
[dest_idx
].swizzle
[0] = j
;
2395 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2397 * "When constructing a vector, the total number of components in all
2398 * the operands must equal the number of components in Result Type."
2400 assert(dest_idx
== num_components
);
2402 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2404 return &vec
->dest
.dest
.ssa
;
2407 static struct vtn_ssa_value
*
2408 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
2410 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
2411 dest
->type
= src
->type
;
2413 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2414 dest
->def
= src
->def
;
2416 unsigned elems
= glsl_get_length(src
->type
);
2418 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
2419 for (unsigned i
= 0; i
< elems
; i
++)
2420 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
2426 static struct vtn_ssa_value
*
2427 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2428 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
2429 unsigned num_indices
)
2431 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
2433 struct vtn_ssa_value
*cur
= dest
;
2435 for (i
= 0; i
< num_indices
- 1; i
++) {
2436 cur
= cur
->elems
[indices
[i
]];
2439 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2440 /* According to the SPIR-V spec, OpCompositeInsert may work down to
2441 * the component granularity. In that case, the last index will be
2442 * the index to insert the scalar into the vector.
2445 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
2447 cur
->elems
[indices
[i
]] = insert
;
2453 static struct vtn_ssa_value
*
2454 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2455 const uint32_t *indices
, unsigned num_indices
)
2457 struct vtn_ssa_value
*cur
= src
;
2458 for (unsigned i
= 0; i
< num_indices
; i
++) {
2459 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2460 assert(i
== num_indices
- 1);
2461 /* According to the SPIR-V spec, OpCompositeExtract may work down to
2462 * the component granularity. The last index will be the index of the
2463 * vector to extract.
2466 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
2467 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
2468 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
2471 cur
= cur
->elems
[indices
[i
]];
2479 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
2480 const uint32_t *w
, unsigned count
)
2482 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2483 const struct glsl_type
*type
=
2484 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
2485 val
->ssa
= vtn_create_ssa_value(b
, type
);
2488 case SpvOpVectorExtractDynamic
:
2489 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2490 vtn_ssa_value(b
, w
[4])->def
);
2493 case SpvOpVectorInsertDynamic
:
2494 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2495 vtn_ssa_value(b
, w
[4])->def
,
2496 vtn_ssa_value(b
, w
[5])->def
);
2499 case SpvOpVectorShuffle
:
2500 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
2501 vtn_ssa_value(b
, w
[3])->def
,
2502 vtn_ssa_value(b
, w
[4])->def
,
2506 case SpvOpCompositeConstruct
: {
2507 unsigned elems
= count
- 3;
2508 if (glsl_type_is_vector_or_scalar(type
)) {
2509 nir_ssa_def
*srcs
[4];
2510 for (unsigned i
= 0; i
< elems
; i
++)
2511 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
2513 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
2516 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2517 for (unsigned i
= 0; i
< elems
; i
++)
2518 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
2522 case SpvOpCompositeExtract
:
2523 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
2527 case SpvOpCompositeInsert
:
2528 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
2529 vtn_ssa_value(b
, w
[3]),
2533 case SpvOpCopyObject
:
2534 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
2538 unreachable("unknown composite operation");
2543 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
2544 const uint32_t *w
, unsigned count
)
2546 nir_intrinsic_op intrinsic_op
;
2548 case SpvOpEmitVertex
:
2549 case SpvOpEmitStreamVertex
:
2550 intrinsic_op
= nir_intrinsic_emit_vertex
;
2552 case SpvOpEndPrimitive
:
2553 case SpvOpEndStreamPrimitive
:
2554 intrinsic_op
= nir_intrinsic_end_primitive
;
2556 case SpvOpMemoryBarrier
:
2557 intrinsic_op
= nir_intrinsic_memory_barrier
;
2559 case SpvOpControlBarrier
:
2560 intrinsic_op
= nir_intrinsic_barrier
;
2563 unreachable("unknown barrier instruction");
2566 nir_intrinsic_instr
*intrin
=
2567 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
2569 if (opcode
== SpvOpEmitStreamVertex
|| opcode
== SpvOpEndStreamPrimitive
)
2570 nir_intrinsic_set_stream_id(intrin
, w
[1]);
2572 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2576 gl_primitive_from_spv_execution_mode(SpvExecutionMode mode
)
2579 case SpvExecutionModeInputPoints
:
2580 case SpvExecutionModeOutputPoints
:
2581 return 0; /* GL_POINTS */
2582 case SpvExecutionModeInputLines
:
2583 return 1; /* GL_LINES */
2584 case SpvExecutionModeInputLinesAdjacency
:
2585 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
2586 case SpvExecutionModeTriangles
:
2587 return 4; /* GL_TRIANGLES */
2588 case SpvExecutionModeInputTrianglesAdjacency
:
2589 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
2590 case SpvExecutionModeQuads
:
2591 return 7; /* GL_QUADS */
2592 case SpvExecutionModeIsolines
:
2593 return 0x8E7A; /* GL_ISOLINES */
2594 case SpvExecutionModeOutputLineStrip
:
2595 return 3; /* GL_LINE_STRIP */
2596 case SpvExecutionModeOutputTriangleStrip
:
2597 return 5; /* GL_TRIANGLE_STRIP */
2599 assert(!"Invalid primitive type");
2605 vertices_in_from_spv_execution_mode(SpvExecutionMode mode
)
2608 case SpvExecutionModeInputPoints
:
2610 case SpvExecutionModeInputLines
:
2612 case SpvExecutionModeInputLinesAdjacency
:
2614 case SpvExecutionModeTriangles
:
2616 case SpvExecutionModeInputTrianglesAdjacency
:
2619 assert(!"Invalid GS input mode");
2624 static gl_shader_stage
2625 stage_for_execution_model(SpvExecutionModel model
)
2628 case SpvExecutionModelVertex
:
2629 return MESA_SHADER_VERTEX
;
2630 case SpvExecutionModelTessellationControl
:
2631 return MESA_SHADER_TESS_CTRL
;
2632 case SpvExecutionModelTessellationEvaluation
:
2633 return MESA_SHADER_TESS_EVAL
;
2634 case SpvExecutionModelGeometry
:
2635 return MESA_SHADER_GEOMETRY
;
2636 case SpvExecutionModelFragment
:
2637 return MESA_SHADER_FRAGMENT
;
2638 case SpvExecutionModelGLCompute
:
2639 return MESA_SHADER_COMPUTE
;
2641 unreachable("Unsupported execution model");
2645 #define spv_check_supported(name, cap) do { \
2646 if (!(b->ext && b->ext->name)) \
2647 vtn_warn("Unsupported SPIR-V capability: %s", \
2648 spirv_capability_to_string(cap)); \
2652 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2653 const uint32_t *w
, unsigned count
)
2657 case SpvOpSourceExtension
:
2658 case SpvOpSourceContinued
:
2659 case SpvOpExtension
:
2660 /* Unhandled, but these are for debug so that's ok. */
2663 case SpvOpCapability
: {
2664 SpvCapability cap
= w
[1];
2666 case SpvCapabilityMatrix
:
2667 case SpvCapabilityShader
:
2668 case SpvCapabilityGeometry
:
2669 case SpvCapabilityGeometryPointSize
:
2670 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
2671 case SpvCapabilitySampledImageArrayDynamicIndexing
:
2672 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
2673 case SpvCapabilityStorageImageArrayDynamicIndexing
:
2674 case SpvCapabilityImageRect
:
2675 case SpvCapabilitySampledRect
:
2676 case SpvCapabilitySampled1D
:
2677 case SpvCapabilityImage1D
:
2678 case SpvCapabilitySampledCubeArray
:
2679 case SpvCapabilitySampledBuffer
:
2680 case SpvCapabilityImageBuffer
:
2681 case SpvCapabilityImageQuery
:
2682 case SpvCapabilityDerivativeControl
:
2683 case SpvCapabilityInterpolationFunction
:
2684 case SpvCapabilityMultiViewport
:
2685 case SpvCapabilitySampleRateShading
:
2686 case SpvCapabilityClipDistance
:
2687 case SpvCapabilityCullDistance
:
2688 case SpvCapabilityInputAttachment
:
2689 case SpvCapabilityImageGatherExtended
:
2690 case SpvCapabilityStorageImageExtendedFormats
:
2693 case SpvCapabilityGeometryStreams
:
2694 case SpvCapabilityLinkage
:
2695 case SpvCapabilityVector16
:
2696 case SpvCapabilityFloat16Buffer
:
2697 case SpvCapabilityFloat16
:
2698 case SpvCapabilityInt64Atomics
:
2699 case SpvCapabilityAtomicStorage
:
2700 case SpvCapabilityInt16
:
2701 case SpvCapabilityStorageImageMultisample
:
2702 case SpvCapabilityImageCubeArray
:
2703 case SpvCapabilityInt8
:
2704 case SpvCapabilitySparseResidency
:
2705 case SpvCapabilityMinLod
:
2706 case SpvCapabilityTransformFeedback
:
2707 vtn_warn("Unsupported SPIR-V capability: %s",
2708 spirv_capability_to_string(cap
));
2711 case SpvCapabilityFloat64
:
2712 spv_check_supported(float64
, cap
);
2714 case SpvCapabilityInt64
:
2715 spv_check_supported(int64
, cap
);
2718 case SpvCapabilityAddresses
:
2719 case SpvCapabilityKernel
:
2720 case SpvCapabilityImageBasic
:
2721 case SpvCapabilityImageReadWrite
:
2722 case SpvCapabilityImageMipmap
:
2723 case SpvCapabilityPipes
:
2724 case SpvCapabilityGroups
:
2725 case SpvCapabilityDeviceEnqueue
:
2726 case SpvCapabilityLiteralSampler
:
2727 case SpvCapabilityGenericPointer
:
2728 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
2729 spirv_capability_to_string(cap
));
2732 case SpvCapabilityImageMSArray
:
2733 spv_check_supported(image_ms_array
, cap
);
2736 case SpvCapabilityTessellation
:
2737 case SpvCapabilityTessellationPointSize
:
2738 spv_check_supported(tessellation
, cap
);
2741 case SpvCapabilityDrawParameters
:
2742 spv_check_supported(draw_parameters
, cap
);
2745 case SpvCapabilityStorageImageReadWithoutFormat
:
2746 spv_check_supported(image_read_without_format
, cap
);
2749 case SpvCapabilityStorageImageWriteWithoutFormat
:
2750 spv_check_supported(image_write_without_format
, cap
);
2753 case SpvCapabilityMultiView
:
2754 spv_check_supported(multiview
, cap
);
2758 unreachable("Unhandled capability");
2763 case SpvOpExtInstImport
:
2764 vtn_handle_extension(b
, opcode
, w
, count
);
2767 case SpvOpMemoryModel
:
2768 assert(w
[1] == SpvAddressingModelLogical
);
2769 assert(w
[2] == SpvMemoryModelGLSL450
);
2772 case SpvOpEntryPoint
: {
2773 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
2774 /* Let this be a name label regardless */
2775 unsigned name_words
;
2776 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
2778 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
2779 stage_for_execution_model(w
[1]) != b
->entry_point_stage
)
2782 assert(b
->entry_point
== NULL
);
2783 b
->entry_point
= entry_point
;
2788 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
2789 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2793 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2796 case SpvOpMemberName
:
2800 case SpvOpExecutionMode
:
2801 case SpvOpDecorationGroup
:
2803 case SpvOpMemberDecorate
:
2804 case SpvOpGroupDecorate
:
2805 case SpvOpGroupMemberDecorate
:
2806 vtn_handle_decoration(b
, opcode
, w
, count
);
2810 return false; /* End of preamble */
2817 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
2818 const struct vtn_decoration
*mode
, void *data
)
2820 assert(b
->entry_point
== entry_point
);
2822 switch(mode
->exec_mode
) {
2823 case SpvExecutionModeOriginUpperLeft
:
2824 case SpvExecutionModeOriginLowerLeft
:
2825 b
->origin_upper_left
=
2826 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
2829 case SpvExecutionModeEarlyFragmentTests
:
2830 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2831 b
->shader
->info
.fs
.early_fragment_tests
= true;
2834 case SpvExecutionModeInvocations
:
2835 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2836 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->literals
[0]);
2839 case SpvExecutionModeDepthReplacing
:
2840 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2841 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
2843 case SpvExecutionModeDepthGreater
:
2844 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2845 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
2847 case SpvExecutionModeDepthLess
:
2848 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2849 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
2851 case SpvExecutionModeDepthUnchanged
:
2852 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2853 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
2856 case SpvExecutionModeLocalSize
:
2857 assert(b
->shader
->stage
== MESA_SHADER_COMPUTE
);
2858 b
->shader
->info
.cs
.local_size
[0] = mode
->literals
[0];
2859 b
->shader
->info
.cs
.local_size
[1] = mode
->literals
[1];
2860 b
->shader
->info
.cs
.local_size
[2] = mode
->literals
[2];
2862 case SpvExecutionModeLocalSizeHint
:
2863 break; /* Nothing to do with this */
2865 case SpvExecutionModeOutputVertices
:
2866 if (b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2867 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
) {
2868 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->literals
[0];
2870 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2871 b
->shader
->info
.gs
.vertices_out
= mode
->literals
[0];
2875 case SpvExecutionModeInputPoints
:
2876 case SpvExecutionModeInputLines
:
2877 case SpvExecutionModeInputLinesAdjacency
:
2878 case SpvExecutionModeTriangles
:
2879 case SpvExecutionModeInputTrianglesAdjacency
:
2880 case SpvExecutionModeQuads
:
2881 case SpvExecutionModeIsolines
:
2882 if (b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2883 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
) {
2884 b
->shader
->info
.tess
.primitive_mode
=
2885 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
2887 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2888 b
->shader
->info
.gs
.vertices_in
=
2889 vertices_in_from_spv_execution_mode(mode
->exec_mode
);
2893 case SpvExecutionModeOutputPoints
:
2894 case SpvExecutionModeOutputLineStrip
:
2895 case SpvExecutionModeOutputTriangleStrip
:
2896 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2897 b
->shader
->info
.gs
.output_primitive
=
2898 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
2901 case SpvExecutionModeSpacingEqual
:
2902 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2903 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2904 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
2906 case SpvExecutionModeSpacingFractionalEven
:
2907 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2908 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2909 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
2911 case SpvExecutionModeSpacingFractionalOdd
:
2912 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2913 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2914 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
2916 case SpvExecutionModeVertexOrderCw
:
2917 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2918 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2919 /* Vulkan's notion of CCW seems to match the hardware backends,
2920 * but be the opposite of OpenGL. Currently NIR follows GL semantics,
2921 * so we set it backwards here.
2923 b
->shader
->info
.tess
.ccw
= true;
2925 case SpvExecutionModeVertexOrderCcw
:
2926 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2927 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2928 /* Backwards; see above */
2929 b
->shader
->info
.tess
.ccw
= false;
2931 case SpvExecutionModePointMode
:
2932 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2933 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2934 b
->shader
->info
.tess
.point_mode
= true;
2937 case SpvExecutionModePixelCenterInteger
:
2938 b
->pixel_center_integer
= true;
2941 case SpvExecutionModeXfb
:
2942 assert(!"Unhandled execution mode");
2945 case SpvExecutionModeVecTypeHint
:
2946 case SpvExecutionModeContractionOff
:
2950 unreachable("Unhandled execution mode");
2955 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2956 const uint32_t *w
, unsigned count
)
2960 case SpvOpSourceContinued
:
2961 case SpvOpSourceExtension
:
2962 case SpvOpExtension
:
2963 case SpvOpCapability
:
2964 case SpvOpExtInstImport
:
2965 case SpvOpMemoryModel
:
2966 case SpvOpEntryPoint
:
2967 case SpvOpExecutionMode
:
2970 case SpvOpMemberName
:
2971 case SpvOpDecorationGroup
:
2973 case SpvOpMemberDecorate
:
2974 case SpvOpGroupDecorate
:
2975 case SpvOpGroupMemberDecorate
:
2976 assert(!"Invalid opcode types and variables section");
2982 case SpvOpTypeFloat
:
2983 case SpvOpTypeVector
:
2984 case SpvOpTypeMatrix
:
2985 case SpvOpTypeImage
:
2986 case SpvOpTypeSampler
:
2987 case SpvOpTypeSampledImage
:
2988 case SpvOpTypeArray
:
2989 case SpvOpTypeRuntimeArray
:
2990 case SpvOpTypeStruct
:
2991 case SpvOpTypeOpaque
:
2992 case SpvOpTypePointer
:
2993 case SpvOpTypeFunction
:
2994 case SpvOpTypeEvent
:
2995 case SpvOpTypeDeviceEvent
:
2996 case SpvOpTypeReserveId
:
2997 case SpvOpTypeQueue
:
2999 vtn_handle_type(b
, opcode
, w
, count
);
3002 case SpvOpConstantTrue
:
3003 case SpvOpConstantFalse
:
3005 case SpvOpConstantComposite
:
3006 case SpvOpConstantSampler
:
3007 case SpvOpConstantNull
:
3008 case SpvOpSpecConstantTrue
:
3009 case SpvOpSpecConstantFalse
:
3010 case SpvOpSpecConstant
:
3011 case SpvOpSpecConstantComposite
:
3012 case SpvOpSpecConstantOp
:
3013 vtn_handle_constant(b
, opcode
, w
, count
);
3018 vtn_handle_variables(b
, opcode
, w
, count
);
3022 return false; /* End of preamble */
3029 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3030 const uint32_t *w
, unsigned count
)
3036 case SpvOpLoopMerge
:
3037 case SpvOpSelectionMerge
:
3038 /* This is handled by cfg pre-pass and walk_blocks */
3042 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
3043 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3048 vtn_handle_extension(b
, opcode
, w
, count
);
3054 case SpvOpCopyMemory
:
3055 case SpvOpCopyMemorySized
:
3056 case SpvOpAccessChain
:
3057 case SpvOpInBoundsAccessChain
:
3058 case SpvOpArrayLength
:
3059 vtn_handle_variables(b
, opcode
, w
, count
);
3062 case SpvOpFunctionCall
:
3063 vtn_handle_function_call(b
, opcode
, w
, count
);
3066 case SpvOpSampledImage
:
3068 case SpvOpImageSampleImplicitLod
:
3069 case SpvOpImageSampleExplicitLod
:
3070 case SpvOpImageSampleDrefImplicitLod
:
3071 case SpvOpImageSampleDrefExplicitLod
:
3072 case SpvOpImageSampleProjImplicitLod
:
3073 case SpvOpImageSampleProjExplicitLod
:
3074 case SpvOpImageSampleProjDrefImplicitLod
:
3075 case SpvOpImageSampleProjDrefExplicitLod
:
3076 case SpvOpImageFetch
:
3077 case SpvOpImageGather
:
3078 case SpvOpImageDrefGather
:
3079 case SpvOpImageQuerySizeLod
:
3080 case SpvOpImageQueryLod
:
3081 case SpvOpImageQueryLevels
:
3082 case SpvOpImageQuerySamples
:
3083 vtn_handle_texture(b
, opcode
, w
, count
);
3086 case SpvOpImageRead
:
3087 case SpvOpImageWrite
:
3088 case SpvOpImageTexelPointer
:
3089 vtn_handle_image(b
, opcode
, w
, count
);
3092 case SpvOpImageQuerySize
: {
3093 struct vtn_pointer
*image
=
3094 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
3095 if (image
->mode
== vtn_variable_mode_image
) {
3096 vtn_handle_image(b
, opcode
, w
, count
);
3098 assert(image
->mode
== vtn_variable_mode_sampler
);
3099 vtn_handle_texture(b
, opcode
, w
, count
);
3104 case SpvOpAtomicLoad
:
3105 case SpvOpAtomicExchange
:
3106 case SpvOpAtomicCompareExchange
:
3107 case SpvOpAtomicCompareExchangeWeak
:
3108 case SpvOpAtomicIIncrement
:
3109 case SpvOpAtomicIDecrement
:
3110 case SpvOpAtomicIAdd
:
3111 case SpvOpAtomicISub
:
3112 case SpvOpAtomicSMin
:
3113 case SpvOpAtomicUMin
:
3114 case SpvOpAtomicSMax
:
3115 case SpvOpAtomicUMax
:
3116 case SpvOpAtomicAnd
:
3118 case SpvOpAtomicXor
: {
3119 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
3120 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
3121 vtn_handle_image(b
, opcode
, w
, count
);
3123 assert(pointer
->value_type
== vtn_value_type_pointer
);
3124 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
3129 case SpvOpAtomicStore
: {
3130 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
3131 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
3132 vtn_handle_image(b
, opcode
, w
, count
);
3134 assert(pointer
->value_type
== vtn_value_type_pointer
);
3135 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
3145 case SpvOpConvertFToU
:
3146 case SpvOpConvertFToS
:
3147 case SpvOpConvertSToF
:
3148 case SpvOpConvertUToF
:
3152 case SpvOpQuantizeToF16
:
3153 case SpvOpConvertPtrToU
:
3154 case SpvOpConvertUToPtr
:
3155 case SpvOpPtrCastToGeneric
:
3156 case SpvOpGenericCastToPtr
:
3162 case SpvOpSignBitSet
:
3163 case SpvOpLessOrGreater
:
3165 case SpvOpUnordered
:
3180 case SpvOpVectorTimesScalar
:
3182 case SpvOpIAddCarry
:
3183 case SpvOpISubBorrow
:
3184 case SpvOpUMulExtended
:
3185 case SpvOpSMulExtended
:
3186 case SpvOpShiftRightLogical
:
3187 case SpvOpShiftRightArithmetic
:
3188 case SpvOpShiftLeftLogical
:
3189 case SpvOpLogicalEqual
:
3190 case SpvOpLogicalNotEqual
:
3191 case SpvOpLogicalOr
:
3192 case SpvOpLogicalAnd
:
3193 case SpvOpLogicalNot
:
3194 case SpvOpBitwiseOr
:
3195 case SpvOpBitwiseXor
:
3196 case SpvOpBitwiseAnd
:
3199 case SpvOpFOrdEqual
:
3200 case SpvOpFUnordEqual
:
3201 case SpvOpINotEqual
:
3202 case SpvOpFOrdNotEqual
:
3203 case SpvOpFUnordNotEqual
:
3204 case SpvOpULessThan
:
3205 case SpvOpSLessThan
:
3206 case SpvOpFOrdLessThan
:
3207 case SpvOpFUnordLessThan
:
3208 case SpvOpUGreaterThan
:
3209 case SpvOpSGreaterThan
:
3210 case SpvOpFOrdGreaterThan
:
3211 case SpvOpFUnordGreaterThan
:
3212 case SpvOpULessThanEqual
:
3213 case SpvOpSLessThanEqual
:
3214 case SpvOpFOrdLessThanEqual
:
3215 case SpvOpFUnordLessThanEqual
:
3216 case SpvOpUGreaterThanEqual
:
3217 case SpvOpSGreaterThanEqual
:
3218 case SpvOpFOrdGreaterThanEqual
:
3219 case SpvOpFUnordGreaterThanEqual
:
3225 case SpvOpFwidthFine
:
3226 case SpvOpDPdxCoarse
:
3227 case SpvOpDPdyCoarse
:
3228 case SpvOpFwidthCoarse
:
3229 case SpvOpBitFieldInsert
:
3230 case SpvOpBitFieldSExtract
:
3231 case SpvOpBitFieldUExtract
:
3232 case SpvOpBitReverse
:
3234 case SpvOpTranspose
:
3235 case SpvOpOuterProduct
:
3236 case SpvOpMatrixTimesScalar
:
3237 case SpvOpVectorTimesMatrix
:
3238 case SpvOpMatrixTimesVector
:
3239 case SpvOpMatrixTimesMatrix
:
3240 vtn_handle_alu(b
, opcode
, w
, count
);
3243 case SpvOpVectorExtractDynamic
:
3244 case SpvOpVectorInsertDynamic
:
3245 case SpvOpVectorShuffle
:
3246 case SpvOpCompositeConstruct
:
3247 case SpvOpCompositeExtract
:
3248 case SpvOpCompositeInsert
:
3249 case SpvOpCopyObject
:
3250 vtn_handle_composite(b
, opcode
, w
, count
);
3253 case SpvOpEmitVertex
:
3254 case SpvOpEndPrimitive
:
3255 case SpvOpEmitStreamVertex
:
3256 case SpvOpEndStreamPrimitive
:
3257 case SpvOpControlBarrier
:
3258 case SpvOpMemoryBarrier
:
3259 vtn_handle_barrier(b
, opcode
, w
, count
);
3263 unreachable("Unhandled opcode");
3270 spirv_to_nir(const uint32_t *words
, size_t word_count
,
3271 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
3272 gl_shader_stage stage
, const char *entry_point_name
,
3273 const struct nir_spirv_supported_extensions
*ext
,
3274 const nir_shader_compiler_options
*options
)
3276 const uint32_t *word_end
= words
+ word_count
;
3278 /* Handle the SPIR-V header (first 4 dwords) */
3279 assert(word_count
> 5);
3281 assert(words
[0] == SpvMagicNumber
);
3282 assert(words
[1] >= 0x10000);
3283 /* words[2] == generator magic */
3284 unsigned value_id_bound
= words
[3];
3285 assert(words
[4] == 0);
3289 /* Initialize the stn_builder object */
3290 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
3291 b
->value_id_bound
= value_id_bound
;
3292 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
3293 exec_list_make_empty(&b
->functions
);
3294 b
->entry_point_stage
= stage
;
3295 b
->entry_point_name
= entry_point_name
;
3298 /* Handle all the preamble instructions */
3299 words
= vtn_foreach_instruction(b
, words
, word_end
,
3300 vtn_handle_preamble_instruction
);
3302 if (b
->entry_point
== NULL
) {
3303 assert(!"Entry point not found");
3308 b
->shader
= nir_shader_create(NULL
, stage
, options
, NULL
);
3310 /* Set shader info defaults */
3311 b
->shader
->info
.gs
.invocations
= 1;
3313 /* Parse execution modes */
3314 vtn_foreach_execution_mode(b
, b
->entry_point
,
3315 vtn_handle_execution_mode
, NULL
);
3317 b
->specializations
= spec
;
3318 b
->num_specializations
= num_spec
;
3320 /* Handle all variable, type, and constant instructions */
3321 words
= vtn_foreach_instruction(b
, words
, word_end
,
3322 vtn_handle_variable_or_type_instruction
);
3324 vtn_build_cfg(b
, words
, word_end
);
3326 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
3327 b
->impl
= func
->impl
;
3328 b
->const_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
3329 _mesa_key_pointer_equal
);
3331 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
3334 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
3335 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
3336 assert(entry_point
);