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
);
418 switch (src
->base_type
) {
419 case vtn_base_type_void
:
420 case vtn_base_type_scalar
:
421 case vtn_base_type_vector
:
422 case vtn_base_type_matrix
:
423 case vtn_base_type_array
:
424 case vtn_base_type_pointer
:
425 case vtn_base_type_image
:
426 case vtn_base_type_sampler
:
427 /* Nothing more to do */
430 case vtn_base_type_struct
:
431 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
432 memcpy(dest
->members
, src
->members
,
433 src
->length
* sizeof(src
->members
[0]));
435 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
436 memcpy(dest
->offsets
, src
->offsets
,
437 src
->length
* sizeof(src
->offsets
[0]));
440 case vtn_base_type_function
:
441 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
442 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
449 static struct vtn_type
*
450 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
452 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
453 type
= type
->members
[member
];
455 /* We may have an array of matrices.... Oh, joy! */
456 while (glsl_type_is_array(type
->type
)) {
457 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
458 type
= type
->array_element
;
461 assert(glsl_type_is_matrix(type
->type
));
467 struct_member_decoration_cb(struct vtn_builder
*b
,
468 struct vtn_value
*val
, int member
,
469 const struct vtn_decoration
*dec
, void *void_ctx
)
471 struct member_decoration_ctx
*ctx
= void_ctx
;
476 assert(member
< ctx
->num_fields
);
478 switch (dec
->decoration
) {
479 case SpvDecorationNonWritable
:
480 case SpvDecorationNonReadable
:
481 case SpvDecorationRelaxedPrecision
:
482 case SpvDecorationVolatile
:
483 case SpvDecorationCoherent
:
484 case SpvDecorationUniform
:
485 break; /* FIXME: Do nothing with this for now. */
486 case SpvDecorationNoPerspective
:
487 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
489 case SpvDecorationFlat
:
490 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
492 case SpvDecorationCentroid
:
493 ctx
->fields
[member
].centroid
= true;
495 case SpvDecorationSample
:
496 ctx
->fields
[member
].sample
= true;
498 case SpvDecorationStream
:
499 /* Vulkan only allows one GS stream */
500 assert(dec
->literals
[0] == 0);
502 case SpvDecorationLocation
:
503 ctx
->fields
[member
].location
= dec
->literals
[0];
505 case SpvDecorationComponent
:
506 break; /* FIXME: What should we do with these? */
507 case SpvDecorationBuiltIn
:
508 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
509 ctx
->type
->members
[member
]->is_builtin
= true;
510 ctx
->type
->members
[member
]->builtin
= dec
->literals
[0];
511 ctx
->type
->builtin_block
= true;
513 case SpvDecorationOffset
:
514 ctx
->type
->offsets
[member
] = dec
->literals
[0];
516 case SpvDecorationMatrixStride
:
517 /* Handled as a second pass */
519 case SpvDecorationColMajor
:
520 break; /* Nothing to do here. Column-major is the default. */
521 case SpvDecorationRowMajor
:
522 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
525 case SpvDecorationPatch
:
528 case SpvDecorationSpecId
:
529 case SpvDecorationBlock
:
530 case SpvDecorationBufferBlock
:
531 case SpvDecorationArrayStride
:
532 case SpvDecorationGLSLShared
:
533 case SpvDecorationGLSLPacked
:
534 case SpvDecorationInvariant
:
535 case SpvDecorationRestrict
:
536 case SpvDecorationAliased
:
537 case SpvDecorationConstant
:
538 case SpvDecorationIndex
:
539 case SpvDecorationBinding
:
540 case SpvDecorationDescriptorSet
:
541 case SpvDecorationLinkageAttributes
:
542 case SpvDecorationNoContraction
:
543 case SpvDecorationInputAttachmentIndex
:
544 vtn_warn("Decoration not allowed on struct members: %s",
545 spirv_decoration_to_string(dec
->decoration
));
548 case SpvDecorationXfbBuffer
:
549 case SpvDecorationXfbStride
:
550 vtn_warn("Vulkan does not have transform feedback");
553 case SpvDecorationCPacked
:
554 case SpvDecorationSaturatedConversion
:
555 case SpvDecorationFuncParamAttr
:
556 case SpvDecorationFPRoundingMode
:
557 case SpvDecorationFPFastMathMode
:
558 case SpvDecorationAlignment
:
559 vtn_warn("Decoration only allowed for CL-style kernels: %s",
560 spirv_decoration_to_string(dec
->decoration
));
564 unreachable("Unhandled decoration");
568 /* Matrix strides are handled as a separate pass because we need to know
569 * whether the matrix is row-major or not first.
572 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
573 struct vtn_value
*val
, int member
,
574 const struct vtn_decoration
*dec
,
577 if (dec
->decoration
!= SpvDecorationMatrixStride
)
581 struct member_decoration_ctx
*ctx
= void_ctx
;
583 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
584 if (mat_type
->row_major
) {
585 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
586 mat_type
->stride
= mat_type
->array_element
->stride
;
587 mat_type
->array_element
->stride
= dec
->literals
[0];
589 assert(mat_type
->array_element
->stride
> 0);
590 mat_type
->stride
= dec
->literals
[0];
595 type_decoration_cb(struct vtn_builder
*b
,
596 struct vtn_value
*val
, int member
,
597 const struct vtn_decoration
*dec
, void *ctx
)
599 struct vtn_type
*type
= val
->type
;
604 switch (dec
->decoration
) {
605 case SpvDecorationArrayStride
:
606 type
->stride
= dec
->literals
[0];
608 case SpvDecorationBlock
:
611 case SpvDecorationBufferBlock
:
612 type
->buffer_block
= true;
614 case SpvDecorationGLSLShared
:
615 case SpvDecorationGLSLPacked
:
616 /* Ignore these, since we get explicit offsets anyways */
619 case SpvDecorationRowMajor
:
620 case SpvDecorationColMajor
:
621 case SpvDecorationMatrixStride
:
622 case SpvDecorationBuiltIn
:
623 case SpvDecorationNoPerspective
:
624 case SpvDecorationFlat
:
625 case SpvDecorationPatch
:
626 case SpvDecorationCentroid
:
627 case SpvDecorationSample
:
628 case SpvDecorationVolatile
:
629 case SpvDecorationCoherent
:
630 case SpvDecorationNonWritable
:
631 case SpvDecorationNonReadable
:
632 case SpvDecorationUniform
:
633 case SpvDecorationStream
:
634 case SpvDecorationLocation
:
635 case SpvDecorationComponent
:
636 case SpvDecorationOffset
:
637 case SpvDecorationXfbBuffer
:
638 case SpvDecorationXfbStride
:
639 vtn_warn("Decoration only allowed for struct members: %s",
640 spirv_decoration_to_string(dec
->decoration
));
643 case SpvDecorationRelaxedPrecision
:
644 case SpvDecorationSpecId
:
645 case SpvDecorationInvariant
:
646 case SpvDecorationRestrict
:
647 case SpvDecorationAliased
:
648 case SpvDecorationConstant
:
649 case SpvDecorationIndex
:
650 case SpvDecorationBinding
:
651 case SpvDecorationDescriptorSet
:
652 case SpvDecorationLinkageAttributes
:
653 case SpvDecorationNoContraction
:
654 case SpvDecorationInputAttachmentIndex
:
655 vtn_warn("Decoration not allowed on types: %s",
656 spirv_decoration_to_string(dec
->decoration
));
659 case SpvDecorationCPacked
:
660 case SpvDecorationSaturatedConversion
:
661 case SpvDecorationFuncParamAttr
:
662 case SpvDecorationFPRoundingMode
:
663 case SpvDecorationFPFastMathMode
:
664 case SpvDecorationAlignment
:
665 vtn_warn("Decoration only allowed for CL-style kernels: %s",
666 spirv_decoration_to_string(dec
->decoration
));
670 unreachable("Unhandled decoration");
675 translate_image_format(SpvImageFormat format
)
678 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
679 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
680 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
681 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
682 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
683 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
684 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
685 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
686 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
687 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
688 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
689 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
690 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
691 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
692 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
693 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
694 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
695 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
696 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
697 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
698 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
699 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
700 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
701 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
702 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
703 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
704 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
705 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
706 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
707 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
708 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
709 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
710 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
711 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
712 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
713 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
714 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
715 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
716 case SpvImageFormatR16ui
: return 0x823A; /* GL_RG16UI */
717 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
719 assert(!"Invalid image format");
725 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
726 const uint32_t *w
, unsigned count
)
728 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
730 val
->type
= rzalloc(b
, struct vtn_type
);
731 val
->type
->val
= val
;
735 val
->type
->base_type
= vtn_base_type_void
;
736 val
->type
->type
= glsl_void_type();
739 val
->type
->base_type
= vtn_base_type_scalar
;
740 val
->type
->type
= glsl_bool_type();
744 const bool signedness
= w
[3];
745 val
->type
->base_type
= vtn_base_type_scalar
;
747 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
749 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
752 case SpvOpTypeFloat
: {
754 val
->type
->base_type
= vtn_base_type_scalar
;
755 val
->type
->type
= bit_size
== 64 ? glsl_double_type() : glsl_float_type();
759 case SpvOpTypeVector
: {
760 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
761 unsigned elems
= w
[3];
763 assert(glsl_type_is_scalar(base
->type
));
764 val
->type
->base_type
= vtn_base_type_vector
;
765 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
767 /* Vectors implicitly have sizeof(base_type) stride. For now, this
768 * is always 4 bytes. This will have to change if we want to start
769 * supporting doubles or half-floats.
771 val
->type
->stride
= glsl_get_bit_size(base
->type
) / 8;
772 val
->type
->array_element
= base
;
776 case SpvOpTypeMatrix
: {
777 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
778 unsigned columns
= w
[3];
780 assert(glsl_type_is_vector(base
->type
));
781 val
->type
->base_type
= vtn_base_type_matrix
;
782 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
783 glsl_get_vector_elements(base
->type
),
785 assert(!glsl_type_is_error(val
->type
->type
));
786 val
->type
->length
= columns
;
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
;
798 if (opcode
== SpvOpTypeRuntimeArray
) {
799 /* A length of 0 is used to denote unsized arrays */
800 val
->type
->length
= 0;
803 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
806 val
->type
->base_type
= vtn_base_type_array
;
807 val
->type
->type
= glsl_array_type(array_element
->type
, val
->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
->base_type
= vtn_base_type_struct
;
816 val
->type
->length
= num_fields
;
817 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
818 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
820 NIR_VLA(struct glsl_struct_field
, fields
, count
);
821 for (unsigned i
= 0; i
< num_fields
; i
++) {
822 val
->type
->members
[i
] =
823 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
824 fields
[i
] = (struct glsl_struct_field
) {
825 .type
= val
->type
->members
[i
]->type
,
826 .name
= ralloc_asprintf(b
, "field%d", i
),
831 struct member_decoration_ctx ctx
= {
832 .num_fields
= num_fields
,
837 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
838 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
840 const char *name
= val
->name
? val
->name
: "struct";
842 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
);
846 case SpvOpTypeFunction
: {
847 val
->type
->base_type
= vtn_base_type_function
;
848 val
->type
->type
= NULL
;
850 val
->type
->return_type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
852 const unsigned num_params
= count
- 3;
853 val
->type
->length
= num_params
;
854 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
855 for (unsigned i
= 0; i
< count
- 3; i
++) {
856 val
->type
->params
[i
] =
857 vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
;
862 case SpvOpTypePointer
: {
863 SpvStorageClass storage_class
= w
[2];
864 struct vtn_type
*deref_type
=
865 vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
867 val
->type
->base_type
= vtn_base_type_pointer
;
868 val
->type
->type
= NULL
;
869 val
->type
->storage_class
= storage_class
;
870 val
->type
->deref
= deref_type
;
874 case SpvOpTypeImage
: {
875 val
->type
->base_type
= vtn_base_type_image
;
877 const struct glsl_type
*sampled_type
=
878 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
880 assert(glsl_type_is_vector_or_scalar(sampled_type
));
882 enum glsl_sampler_dim dim
;
883 switch ((SpvDim
)w
[3]) {
884 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
885 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
886 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
887 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
888 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
889 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
890 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
892 unreachable("Invalid SPIR-V Sampler dimension");
895 bool is_shadow
= w
[4];
896 bool is_array
= w
[5];
897 bool multisampled
= w
[6];
898 unsigned sampled
= w
[7];
899 SpvImageFormat format
= w
[8];
902 val
->type
->access_qualifier
= w
[9];
904 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
907 if (dim
== GLSL_SAMPLER_DIM_2D
)
908 dim
= GLSL_SAMPLER_DIM_MS
;
909 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
910 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
912 assert(!"Unsupported multisampled image type");
915 val
->type
->image_format
= translate_image_format(format
);
918 val
->type
->sampled
= true;
919 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
920 glsl_get_base_type(sampled_type
));
921 } else if (sampled
== 2) {
923 val
->type
->sampled
= false;
924 val
->type
->type
= glsl_image_type(dim
, is_array
,
925 glsl_get_base_type(sampled_type
));
927 assert(!"We need to know if the image will be sampled");
932 case SpvOpTypeSampledImage
:
933 val
->type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
936 case SpvOpTypeSampler
:
937 /* The actual sampler type here doesn't really matter. It gets
938 * thrown away the moment you combine it with an image. What really
939 * matters is that it's a sampler type as opposed to an integer type
940 * so the backend knows what to do.
942 val
->type
->base_type
= vtn_base_type_sampler
;
943 val
->type
->type
= glsl_bare_sampler_type();
946 case SpvOpTypeOpaque
:
948 case SpvOpTypeDeviceEvent
:
949 case SpvOpTypeReserveId
:
953 unreachable("Unhandled opcode");
956 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
959 static nir_constant
*
960 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
962 nir_constant
*c
= rzalloc(b
, nir_constant
);
964 /* For pointers and other typeless things, we have to return something but
965 * it doesn't matter what.
970 switch (glsl_get_base_type(type
)) {
973 case GLSL_TYPE_INT64
:
974 case GLSL_TYPE_UINT64
:
976 case GLSL_TYPE_FLOAT
:
977 case GLSL_TYPE_DOUBLE
:
978 /* Nothing to do here. It's already initialized to zero */
981 case GLSL_TYPE_ARRAY
:
982 assert(glsl_get_length(type
) > 0);
983 c
->num_elements
= glsl_get_length(type
);
984 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
986 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
987 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
988 c
->elements
[i
] = c
->elements
[0];
991 case GLSL_TYPE_STRUCT
:
992 c
->num_elements
= glsl_get_length(type
);
993 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
995 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
996 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
1001 unreachable("Invalid type for null constant");
1008 spec_constant_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
1009 int member
, const struct vtn_decoration
*dec
,
1012 assert(member
== -1);
1013 if (dec
->decoration
!= SpvDecorationSpecId
)
1016 struct spec_constant_value
*const_value
= data
;
1018 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1019 if (b
->specializations
[i
].id
== dec
->literals
[0]) {
1020 if (const_value
->is_double
)
1021 const_value
->data64
= b
->specializations
[i
].data64
;
1023 const_value
->data32
= b
->specializations
[i
].data32
;
1030 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
1031 uint32_t const_value
)
1033 struct spec_constant_value data
;
1034 data
.is_double
= false;
1035 data
.data32
= const_value
;
1036 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1041 get_specialization64(struct vtn_builder
*b
, struct vtn_value
*val
,
1042 uint64_t const_value
)
1044 struct spec_constant_value data
;
1045 data
.is_double
= true;
1046 data
.data64
= const_value
;
1047 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &data
);
1052 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1053 struct vtn_value
*val
,
1055 const struct vtn_decoration
*dec
,
1058 assert(member
== -1);
1059 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1060 dec
->literals
[0] != SpvBuiltInWorkgroupSize
)
1063 assert(val
->const_type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1065 b
->shader
->info
.cs
.local_size
[0] = val
->constant
->values
[0].u32
[0];
1066 b
->shader
->info
.cs
.local_size
[1] = val
->constant
->values
[0].u32
[1];
1067 b
->shader
->info
.cs
.local_size
[2] = val
->constant
->values
[0].u32
[2];
1071 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1072 const uint32_t *w
, unsigned count
)
1074 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1075 val
->const_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
1076 val
->constant
= rzalloc(b
, nir_constant
);
1078 case SpvOpConstantTrue
:
1079 assert(val
->const_type
== glsl_bool_type());
1080 val
->constant
->values
[0].u32
[0] = NIR_TRUE
;
1082 case SpvOpConstantFalse
:
1083 assert(val
->const_type
== glsl_bool_type());
1084 val
->constant
->values
[0].u32
[0] = NIR_FALSE
;
1087 case SpvOpSpecConstantTrue
:
1088 case SpvOpSpecConstantFalse
: {
1089 assert(val
->const_type
== glsl_bool_type());
1091 get_specialization(b
, val
, (opcode
== SpvOpSpecConstantTrue
));
1092 val
->constant
->values
[0].u32
[0] = int_val
? NIR_TRUE
: NIR_FALSE
;
1096 case SpvOpConstant
: {
1097 assert(glsl_type_is_scalar(val
->const_type
));
1098 int bit_size
= glsl_get_bit_size(val
->const_type
);
1099 if (bit_size
== 64) {
1100 val
->constant
->values
->u32
[0] = w
[3];
1101 val
->constant
->values
->u32
[1] = w
[4];
1103 assert(bit_size
== 32);
1104 val
->constant
->values
->u32
[0] = w
[3];
1108 case SpvOpSpecConstant
: {
1109 assert(glsl_type_is_scalar(val
->const_type
));
1110 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1111 int bit_size
= glsl_get_bit_size(val
->const_type
);
1113 val
->constant
->values
[0].u64
[0] =
1114 get_specialization64(b
, val
, vtn_u64_literal(&w
[3]));
1116 val
->constant
->values
[0].u32
[0] = get_specialization(b
, val
, w
[3]);
1119 case SpvOpSpecConstantComposite
:
1120 case SpvOpConstantComposite
: {
1121 unsigned elem_count
= count
- 3;
1122 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1123 for (unsigned i
= 0; i
< elem_count
; i
++)
1124 elems
[i
] = vtn_value(b
, w
[i
+ 3], vtn_value_type_constant
)->constant
;
1126 switch (glsl_get_base_type(val
->const_type
)) {
1127 case GLSL_TYPE_UINT
:
1129 case GLSL_TYPE_UINT64
:
1130 case GLSL_TYPE_INT64
:
1131 case GLSL_TYPE_FLOAT
:
1132 case GLSL_TYPE_BOOL
:
1133 case GLSL_TYPE_DOUBLE
: {
1134 int bit_size
= glsl_get_bit_size(val
->const_type
);
1135 if (glsl_type_is_matrix(val
->const_type
)) {
1136 assert(glsl_get_matrix_columns(val
->const_type
) == elem_count
);
1137 for (unsigned i
= 0; i
< elem_count
; i
++)
1138 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1140 assert(glsl_type_is_vector(val
->const_type
));
1141 assert(glsl_get_vector_elements(val
->const_type
) == elem_count
);
1142 for (unsigned i
= 0; i
< elem_count
; i
++) {
1143 if (bit_size
== 64) {
1144 val
->constant
->values
[0].u64
[i
] = elems
[i
]->values
[0].u64
[0];
1146 assert(bit_size
== 32);
1147 val
->constant
->values
[0].u32
[i
] = elems
[i
]->values
[0].u32
[0];
1154 case GLSL_TYPE_STRUCT
:
1155 case GLSL_TYPE_ARRAY
:
1156 ralloc_steal(val
->constant
, elems
);
1157 val
->constant
->num_elements
= elem_count
;
1158 val
->constant
->elements
= elems
;
1162 unreachable("Unsupported type for constants");
1167 case SpvOpSpecConstantOp
: {
1168 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1170 case SpvOpVectorShuffle
: {
1171 struct vtn_value
*v0
= &b
->values
[w
[4]];
1172 struct vtn_value
*v1
= &b
->values
[w
[5]];
1174 assert(v0
->value_type
== vtn_value_type_constant
||
1175 v0
->value_type
== vtn_value_type_undef
);
1176 assert(v1
->value_type
== vtn_value_type_constant
||
1177 v1
->value_type
== vtn_value_type_undef
);
1179 unsigned len0
= v0
->value_type
== vtn_value_type_constant
?
1180 glsl_get_vector_elements(v0
->const_type
) :
1181 glsl_get_vector_elements(v0
->type
->type
);
1182 unsigned len1
= v1
->value_type
== vtn_value_type_constant
?
1183 glsl_get_vector_elements(v1
->const_type
) :
1184 glsl_get_vector_elements(v1
->type
->type
);
1186 assert(len0
+ len1
< 16);
1188 unsigned bit_size
= glsl_get_bit_size(val
->const_type
);
1189 unsigned bit_size0
= v0
->value_type
== vtn_value_type_constant
?
1190 glsl_get_bit_size(v0
->const_type
) :
1191 glsl_get_bit_size(v0
->type
->type
);
1192 unsigned bit_size1
= v1
->value_type
== vtn_value_type_constant
?
1193 glsl_get_bit_size(v1
->const_type
) :
1194 glsl_get_bit_size(v1
->type
->type
);
1196 assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1197 (void)bit_size0
; (void)bit_size1
;
1199 if (bit_size
== 64) {
1201 if (v0
->value_type
== vtn_value_type_constant
) {
1202 for (unsigned i
= 0; i
< len0
; i
++)
1203 u64
[i
] = v0
->constant
->values
[0].u64
[i
];
1205 if (v1
->value_type
== vtn_value_type_constant
) {
1206 for (unsigned i
= 0; i
< len1
; i
++)
1207 u64
[len0
+ i
] = v1
->constant
->values
[0].u64
[i
];
1210 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1211 uint32_t comp
= w
[i
+ 6];
1212 /* If component is not used, set the value to a known constant
1213 * to detect if it is wrongly used.
1215 if (comp
== (uint32_t)-1)
1216 val
->constant
->values
[0].u64
[j
] = 0xdeadbeefdeadbeef;
1218 val
->constant
->values
[0].u64
[j
] = u64
[comp
];
1222 if (v0
->value_type
== vtn_value_type_constant
) {
1223 for (unsigned i
= 0; i
< len0
; i
++)
1224 u32
[i
] = v0
->constant
->values
[0].u32
[i
];
1226 if (v1
->value_type
== vtn_value_type_constant
) {
1227 for (unsigned i
= 0; i
< len1
; i
++)
1228 u32
[len0
+ i
] = v1
->constant
->values
[0].u32
[i
];
1231 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1232 uint32_t comp
= w
[i
+ 6];
1233 /* If component is not used, set the value to a known constant
1234 * to detect if it is wrongly used.
1236 if (comp
== (uint32_t)-1)
1237 val
->constant
->values
[0].u32
[j
] = 0xdeadbeef;
1239 val
->constant
->values
[0].u32
[j
] = u32
[comp
];
1245 case SpvOpCompositeExtract
:
1246 case SpvOpCompositeInsert
: {
1247 struct vtn_value
*comp
;
1248 unsigned deref_start
;
1249 struct nir_constant
**c
;
1250 if (opcode
== SpvOpCompositeExtract
) {
1251 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1253 c
= &comp
->constant
;
1255 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1257 val
->constant
= nir_constant_clone(comp
->constant
,
1264 const struct glsl_type
*type
= comp
->const_type
;
1265 for (unsigned i
= deref_start
; i
< count
; i
++) {
1266 switch (glsl_get_base_type(type
)) {
1267 case GLSL_TYPE_UINT
:
1269 case GLSL_TYPE_UINT64
:
1270 case GLSL_TYPE_INT64
:
1271 case GLSL_TYPE_FLOAT
:
1272 case GLSL_TYPE_DOUBLE
:
1273 case GLSL_TYPE_BOOL
:
1274 /* If we hit this granularity, we're picking off an element */
1275 if (glsl_type_is_matrix(type
)) {
1276 assert(col
== 0 && elem
== -1);
1279 type
= glsl_get_column_type(type
);
1281 assert(elem
<= 0 && glsl_type_is_vector(type
));
1283 type
= glsl_scalar_type(glsl_get_base_type(type
));
1287 case GLSL_TYPE_ARRAY
:
1288 c
= &(*c
)->elements
[w
[i
]];
1289 type
= glsl_get_array_element(type
);
1292 case GLSL_TYPE_STRUCT
:
1293 c
= &(*c
)->elements
[w
[i
]];
1294 type
= glsl_get_struct_field(type
, w
[i
]);
1298 unreachable("Invalid constant type");
1302 if (opcode
== SpvOpCompositeExtract
) {
1306 unsigned num_components
= glsl_get_vector_elements(type
);
1307 unsigned bit_size
= glsl_get_bit_size(type
);
1308 for (unsigned i
= 0; i
< num_components
; i
++)
1309 if (bit_size
== 64) {
1310 val
->constant
->values
[0].u64
[i
] = (*c
)->values
[col
].u64
[elem
+ i
];
1312 assert(bit_size
== 32);
1313 val
->constant
->values
[0].u32
[i
] = (*c
)->values
[col
].u32
[elem
+ i
];
1317 struct vtn_value
*insert
=
1318 vtn_value(b
, w
[4], vtn_value_type_constant
);
1319 assert(insert
->const_type
== type
);
1321 *c
= insert
->constant
;
1323 unsigned num_components
= glsl_get_vector_elements(type
);
1324 unsigned bit_size
= glsl_get_bit_size(type
);
1325 for (unsigned i
= 0; i
< num_components
; i
++)
1326 if (bit_size
== 64) {
1327 (*c
)->values
[col
].u64
[elem
+ i
] = insert
->constant
->values
[0].u64
[i
];
1329 assert(bit_size
== 32);
1330 (*c
)->values
[col
].u32
[elem
+ i
] = insert
->constant
->values
[0].u32
[i
];
1339 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->const_type
);
1340 nir_alu_type src_alu_type
= dst_alu_type
;
1341 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(opcode
, &swap
, src_alu_type
, dst_alu_type
);
1343 unsigned num_components
= glsl_get_vector_elements(val
->const_type
);
1345 glsl_get_bit_size(val
->const_type
);
1347 nir_const_value src
[4];
1349 for (unsigned i
= 0; i
< count
- 4; i
++) {
1351 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
)->constant
;
1353 unsigned j
= swap
? 1 - i
: i
;
1354 assert(bit_size
== 32);
1355 src
[j
] = c
->values
[0];
1358 val
->constant
->values
[0] =
1359 nir_eval_const_opcode(op
, num_components
, bit_size
, src
);
1366 case SpvOpConstantNull
:
1367 val
->constant
= vtn_null_constant(b
, val
->const_type
);
1370 case SpvOpConstantSampler
:
1371 assert(!"OpConstantSampler requires Kernel Capability");
1375 unreachable("Unhandled opcode");
1378 /* Now that we have the value, update the workgroup size if needed */
1379 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1383 vtn_handle_function_call(struct vtn_builder
*b
, SpvOp opcode
,
1384 const uint32_t *w
, unsigned count
)
1386 struct nir_function
*callee
=
1387 vtn_value(b
, w
[3], vtn_value_type_function
)->func
->impl
->function
;
1389 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, callee
);
1390 for (unsigned i
= 0; i
< call
->num_params
; i
++) {
1391 unsigned arg_id
= w
[4 + i
];
1392 struct vtn_value
*arg
= vtn_untyped_value(b
, arg_id
);
1393 if (arg
->value_type
== vtn_value_type_pointer
) {
1394 nir_deref_var
*d
= vtn_pointer_to_deref(b
, arg
->pointer
);
1395 call
->params
[i
] = nir_deref_var_clone(d
, call
);
1397 struct vtn_ssa_value
*arg_ssa
= vtn_ssa_value(b
, arg_id
);
1399 /* Make a temporary to store the argument in */
1401 nir_local_variable_create(b
->impl
, arg_ssa
->type
, "arg_tmp");
1402 call
->params
[i
] = nir_deref_var_create(call
, tmp
);
1404 vtn_local_store(b
, arg_ssa
, call
->params
[i
]);
1408 nir_variable
*out_tmp
= NULL
;
1409 if (!glsl_type_is_void(callee
->return_type
)) {
1410 out_tmp
= nir_local_variable_create(b
->impl
, callee
->return_type
,
1412 call
->return_deref
= nir_deref_var_create(call
, out_tmp
);
1415 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
1417 if (glsl_type_is_void(callee
->return_type
)) {
1418 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1420 struct vtn_value
*retval
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1421 retval
->ssa
= vtn_local_load(b
, call
->return_deref
);
1425 struct vtn_ssa_value
*
1426 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1428 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1431 if (!glsl_type_is_vector_or_scalar(type
)) {
1432 unsigned elems
= glsl_get_length(type
);
1433 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1434 for (unsigned i
= 0; i
< elems
; i
++) {
1435 const struct glsl_type
*child_type
;
1437 switch (glsl_get_base_type(type
)) {
1439 case GLSL_TYPE_UINT
:
1440 case GLSL_TYPE_INT64
:
1441 case GLSL_TYPE_UINT64
:
1442 case GLSL_TYPE_BOOL
:
1443 case GLSL_TYPE_FLOAT
:
1444 case GLSL_TYPE_DOUBLE
:
1445 child_type
= glsl_get_column_type(type
);
1447 case GLSL_TYPE_ARRAY
:
1448 child_type
= glsl_get_array_element(type
);
1450 case GLSL_TYPE_STRUCT
:
1451 child_type
= glsl_get_struct_field(type
, i
);
1454 unreachable("unkown base type");
1457 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
1465 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
1468 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
1469 src
.src_type
= type
;
1474 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
1475 const uint32_t *w
, unsigned count
)
1477 if (opcode
== SpvOpSampledImage
) {
1478 struct vtn_value
*val
=
1479 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1480 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1481 val
->sampled_image
->image
=
1482 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1483 val
->sampled_image
->sampler
=
1484 vtn_value(b
, w
[4], vtn_value_type_pointer
)->pointer
;
1486 } else if (opcode
== SpvOpImage
) {
1487 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_pointer
);
1488 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
1489 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
1490 val
->pointer
= src_val
->sampled_image
->image
;
1492 assert(src_val
->value_type
== vtn_value_type_pointer
);
1493 val
->pointer
= src_val
->pointer
;
1498 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1499 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1501 struct vtn_sampled_image sampled
;
1502 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
1503 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
1504 sampled
= *sampled_val
->sampled_image
;
1506 assert(sampled_val
->value_type
== vtn_value_type_pointer
);
1507 sampled
.image
= NULL
;
1508 sampled
.sampler
= sampled_val
->pointer
;
1511 const struct glsl_type
*image_type
;
1512 if (sampled
.image
) {
1513 image_type
= sampled
.image
->var
->var
->interface_type
;
1515 image_type
= sampled
.sampler
->var
->var
->interface_type
;
1517 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image_type
);
1518 const bool is_array
= glsl_sampler_type_is_array(image_type
);
1519 const bool is_shadow
= glsl_sampler_type_is_shadow(image_type
);
1521 /* Figure out the base texture operation */
1524 case SpvOpImageSampleImplicitLod
:
1525 case SpvOpImageSampleDrefImplicitLod
:
1526 case SpvOpImageSampleProjImplicitLod
:
1527 case SpvOpImageSampleProjDrefImplicitLod
:
1528 texop
= nir_texop_tex
;
1531 case SpvOpImageSampleExplicitLod
:
1532 case SpvOpImageSampleDrefExplicitLod
:
1533 case SpvOpImageSampleProjExplicitLod
:
1534 case SpvOpImageSampleProjDrefExplicitLod
:
1535 texop
= nir_texop_txl
;
1538 case SpvOpImageFetch
:
1539 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
1540 texop
= nir_texop_txf_ms
;
1542 texop
= nir_texop_txf
;
1546 case SpvOpImageGather
:
1547 case SpvOpImageDrefGather
:
1548 texop
= nir_texop_tg4
;
1551 case SpvOpImageQuerySizeLod
:
1552 case SpvOpImageQuerySize
:
1553 texop
= nir_texop_txs
;
1556 case SpvOpImageQueryLod
:
1557 texop
= nir_texop_lod
;
1560 case SpvOpImageQueryLevels
:
1561 texop
= nir_texop_query_levels
;
1564 case SpvOpImageQuerySamples
:
1565 texop
= nir_texop_texture_samples
;
1569 unreachable("Unhandled opcode");
1572 nir_tex_src srcs
[8]; /* 8 should be enough */
1573 nir_tex_src
*p
= srcs
;
1577 struct nir_ssa_def
*coord
;
1578 unsigned coord_components
;
1580 case SpvOpImageSampleImplicitLod
:
1581 case SpvOpImageSampleExplicitLod
:
1582 case SpvOpImageSampleDrefImplicitLod
:
1583 case SpvOpImageSampleDrefExplicitLod
:
1584 case SpvOpImageSampleProjImplicitLod
:
1585 case SpvOpImageSampleProjExplicitLod
:
1586 case SpvOpImageSampleProjDrefImplicitLod
:
1587 case SpvOpImageSampleProjDrefExplicitLod
:
1588 case SpvOpImageFetch
:
1589 case SpvOpImageGather
:
1590 case SpvOpImageDrefGather
:
1591 case SpvOpImageQueryLod
: {
1592 /* All these types have the coordinate as their first real argument */
1593 switch (sampler_dim
) {
1594 case GLSL_SAMPLER_DIM_1D
:
1595 case GLSL_SAMPLER_DIM_BUF
:
1596 coord_components
= 1;
1598 case GLSL_SAMPLER_DIM_2D
:
1599 case GLSL_SAMPLER_DIM_RECT
:
1600 case GLSL_SAMPLER_DIM_MS
:
1601 coord_components
= 2;
1603 case GLSL_SAMPLER_DIM_3D
:
1604 case GLSL_SAMPLER_DIM_CUBE
:
1605 coord_components
= 3;
1608 unreachable("Invalid sampler type");
1611 if (is_array
&& texop
!= nir_texop_lod
)
1614 coord
= vtn_ssa_value(b
, w
[idx
++])->def
;
1615 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
1616 (1 << coord_components
) - 1));
1617 p
->src_type
= nir_tex_src_coord
;
1624 coord_components
= 0;
1629 case SpvOpImageSampleProjImplicitLod
:
1630 case SpvOpImageSampleProjExplicitLod
:
1631 case SpvOpImageSampleProjDrefImplicitLod
:
1632 case SpvOpImageSampleProjDrefExplicitLod
:
1633 /* These have the projector as the last coordinate component */
1634 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
1635 p
->src_type
= nir_tex_src_projector
;
1643 unsigned gather_component
= 0;
1645 case SpvOpImageSampleDrefImplicitLod
:
1646 case SpvOpImageSampleDrefExplicitLod
:
1647 case SpvOpImageSampleProjDrefImplicitLod
:
1648 case SpvOpImageSampleProjDrefExplicitLod
:
1649 case SpvOpImageDrefGather
:
1650 /* These all have an explicit depth value as their next source */
1651 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
1654 case SpvOpImageGather
:
1655 /* This has a component as its next source */
1657 vtn_value(b
, w
[idx
++], vtn_value_type_constant
)->constant
->values
[0].u32
[0];
1664 /* For OpImageQuerySizeLod, we always have an LOD */
1665 if (opcode
== SpvOpImageQuerySizeLod
)
1666 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1668 /* Now we need to handle some number of optional arguments */
1669 const struct vtn_ssa_value
*gather_offsets
= NULL
;
1671 uint32_t operands
= w
[idx
++];
1673 if (operands
& SpvImageOperandsBiasMask
) {
1674 assert(texop
== nir_texop_tex
);
1675 texop
= nir_texop_txb
;
1676 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
1679 if (operands
& SpvImageOperandsLodMask
) {
1680 assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
1681 texop
== nir_texop_txs
);
1682 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1685 if (operands
& SpvImageOperandsGradMask
) {
1686 assert(texop
== nir_texop_txl
);
1687 texop
= nir_texop_txd
;
1688 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
1689 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
1692 if (operands
& SpvImageOperandsOffsetMask
||
1693 operands
& SpvImageOperandsConstOffsetMask
)
1694 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
1696 if (operands
& SpvImageOperandsConstOffsetsMask
) {
1697 gather_offsets
= vtn_ssa_value(b
, w
[idx
++]);
1698 (*p
++) = (nir_tex_src
){};
1701 if (operands
& SpvImageOperandsSampleMask
) {
1702 assert(texop
== nir_texop_txf_ms
);
1703 texop
= nir_texop_txf_ms
;
1704 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
1707 /* We should have now consumed exactly all of the arguments */
1708 assert(idx
== count
);
1710 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
1713 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1715 instr
->coord_components
= coord_components
;
1716 instr
->sampler_dim
= sampler_dim
;
1717 instr
->is_array
= is_array
;
1718 instr
->is_shadow
= is_shadow
;
1719 instr
->is_new_style_shadow
=
1720 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
1721 instr
->component
= gather_component
;
1723 switch (glsl_get_sampler_result_type(image_type
)) {
1724 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
1725 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
1726 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
1727 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
1729 unreachable("Invalid base type for sampler result");
1732 nir_deref_var
*sampler
= vtn_pointer_to_deref(b
, sampled
.sampler
);
1733 nir_deref_var
*texture
;
1734 if (sampled
.image
) {
1735 nir_deref_var
*image
= vtn_pointer_to_deref(b
, sampled
.image
);
1741 instr
->texture
= nir_deref_var_clone(texture
, instr
);
1743 switch (instr
->op
) {
1748 /* These operations require a sampler */
1749 instr
->sampler
= nir_deref_var_clone(sampler
, instr
);
1752 case nir_texop_txf_ms
:
1756 case nir_texop_query_levels
:
1757 case nir_texop_texture_samples
:
1758 case nir_texop_samples_identical
:
1760 instr
->sampler
= NULL
;
1762 case nir_texop_txf_ms_mcs
:
1763 unreachable("unexpected nir_texop_txf_ms_mcs");
1766 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1767 nir_tex_instr_dest_size(instr
), 32, NULL
);
1769 assert(glsl_get_vector_elements(ret_type
->type
) ==
1770 nir_tex_instr_dest_size(instr
));
1773 nir_instr
*instruction
;
1774 if (gather_offsets
) {
1775 assert(glsl_get_base_type(gather_offsets
->type
) == GLSL_TYPE_ARRAY
);
1776 assert(glsl_get_length(gather_offsets
->type
) == 4);
1777 nir_tex_instr
*instrs
[4] = {instr
, NULL
, NULL
, NULL
};
1779 /* Copy the current instruction 4x */
1780 for (uint32_t i
= 1; i
< 4; i
++) {
1781 instrs
[i
] = nir_tex_instr_create(b
->shader
, instr
->num_srcs
);
1782 instrs
[i
]->op
= instr
->op
;
1783 instrs
[i
]->coord_components
= instr
->coord_components
;
1784 instrs
[i
]->sampler_dim
= instr
->sampler_dim
;
1785 instrs
[i
]->is_array
= instr
->is_array
;
1786 instrs
[i
]->is_shadow
= instr
->is_shadow
;
1787 instrs
[i
]->is_new_style_shadow
= instr
->is_new_style_shadow
;
1788 instrs
[i
]->component
= instr
->component
;
1789 instrs
[i
]->dest_type
= instr
->dest_type
;
1790 instrs
[i
]->texture
= nir_deref_var_clone(texture
, instrs
[i
]);
1791 instrs
[i
]->sampler
= NULL
;
1793 memcpy(instrs
[i
]->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1795 nir_ssa_dest_init(&instrs
[i
]->instr
, &instrs
[i
]->dest
,
1796 nir_tex_instr_dest_size(instr
), 32, NULL
);
1799 /* Fill in the last argument with the offset from the passed in offsets
1800 * and insert the instruction into the stream.
1802 for (uint32_t i
= 0; i
< 4; i
++) {
1804 src
.src
= nir_src_for_ssa(gather_offsets
->elems
[i
]->def
);
1805 src
.src_type
= nir_tex_src_offset
;
1806 instrs
[i
]->src
[instrs
[i
]->num_srcs
- 1] = src
;
1807 nir_builder_instr_insert(&b
->nb
, &instrs
[i
]->instr
);
1810 /* Combine the results of the 4 instructions by taking their .w
1813 nir_alu_instr
*vec4
= nir_alu_instr_create(b
->shader
, nir_op_vec4
);
1814 nir_ssa_dest_init(&vec4
->instr
, &vec4
->dest
.dest
, 4, 32, NULL
);
1815 vec4
->dest
.write_mask
= 0xf;
1816 for (uint32_t i
= 0; i
< 4; i
++) {
1817 vec4
->src
[i
].src
= nir_src_for_ssa(&instrs
[i
]->dest
.ssa
);
1818 vec4
->src
[i
].swizzle
[0] = 3;
1820 def
= &vec4
->dest
.dest
.ssa
;
1821 instruction
= &vec4
->instr
;
1823 def
= &instr
->dest
.ssa
;
1824 instruction
= &instr
->instr
;
1827 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
1828 val
->ssa
->def
= def
;
1830 nir_builder_instr_insert(&b
->nb
, instruction
);
1834 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
1835 const uint32_t *w
, nir_src
*src
)
1838 case SpvOpAtomicIIncrement
:
1839 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
1842 case SpvOpAtomicIDecrement
:
1843 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
1846 case SpvOpAtomicISub
:
1848 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
1851 case SpvOpAtomicCompareExchange
:
1852 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
1853 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
1856 case SpvOpAtomicExchange
:
1857 case SpvOpAtomicIAdd
:
1858 case SpvOpAtomicSMin
:
1859 case SpvOpAtomicUMin
:
1860 case SpvOpAtomicSMax
:
1861 case SpvOpAtomicUMax
:
1862 case SpvOpAtomicAnd
:
1864 case SpvOpAtomicXor
:
1865 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1869 unreachable("Invalid SPIR-V atomic");
1873 static nir_ssa_def
*
1874 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
1876 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
1878 /* The image_load_store intrinsics assume a 4-dim coordinate */
1879 unsigned dim
= glsl_get_vector_elements(coord
->type
);
1880 unsigned swizzle
[4];
1881 for (unsigned i
= 0; i
< 4; i
++)
1882 swizzle
[i
] = MIN2(i
, dim
- 1);
1884 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
1888 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
1889 const uint32_t *w
, unsigned count
)
1891 /* Just get this one out of the way */
1892 if (opcode
== SpvOpImageTexelPointer
) {
1893 struct vtn_value
*val
=
1894 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
1895 val
->image
= ralloc(b
, struct vtn_image_pointer
);
1897 val
->image
->image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1898 val
->image
->coord
= get_image_coord(b
, w
[4]);
1899 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
1903 struct vtn_image_pointer image
;
1906 case SpvOpAtomicExchange
:
1907 case SpvOpAtomicCompareExchange
:
1908 case SpvOpAtomicCompareExchangeWeak
:
1909 case SpvOpAtomicIIncrement
:
1910 case SpvOpAtomicIDecrement
:
1911 case SpvOpAtomicIAdd
:
1912 case SpvOpAtomicISub
:
1913 case SpvOpAtomicLoad
:
1914 case SpvOpAtomicSMin
:
1915 case SpvOpAtomicUMin
:
1916 case SpvOpAtomicSMax
:
1917 case SpvOpAtomicUMax
:
1918 case SpvOpAtomicAnd
:
1920 case SpvOpAtomicXor
:
1921 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
1924 case SpvOpAtomicStore
:
1925 image
= *vtn_value(b
, w
[1], vtn_value_type_image_pointer
)->image
;
1928 case SpvOpImageQuerySize
:
1929 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1931 image
.sample
= NULL
;
1934 case SpvOpImageRead
:
1935 image
.image
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
1936 image
.coord
= get_image_coord(b
, w
[4]);
1938 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
1939 assert(w
[5] == SpvImageOperandsSampleMask
);
1940 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
1942 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1946 case SpvOpImageWrite
:
1947 image
.image
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
1948 image
.coord
= get_image_coord(b
, w
[2]);
1952 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
1953 assert(w
[4] == SpvImageOperandsSampleMask
);
1954 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
1956 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1961 unreachable("Invalid image opcode");
1964 nir_intrinsic_op op
;
1966 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_##N; break;
1967 OP(ImageQuerySize
, size
)
1969 OP(ImageWrite
, store
)
1970 OP(AtomicLoad
, load
)
1971 OP(AtomicStore
, store
)
1972 OP(AtomicExchange
, atomic_exchange
)
1973 OP(AtomicCompareExchange
, atomic_comp_swap
)
1974 OP(AtomicIIncrement
, atomic_add
)
1975 OP(AtomicIDecrement
, atomic_add
)
1976 OP(AtomicIAdd
, atomic_add
)
1977 OP(AtomicISub
, atomic_add
)
1978 OP(AtomicSMin
, atomic_min
)
1979 OP(AtomicUMin
, atomic_min
)
1980 OP(AtomicSMax
, atomic_max
)
1981 OP(AtomicUMax
, atomic_max
)
1982 OP(AtomicAnd
, atomic_and
)
1983 OP(AtomicOr
, atomic_or
)
1984 OP(AtomicXor
, atomic_xor
)
1987 unreachable("Invalid image opcode");
1990 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
1992 nir_deref_var
*image_deref
= vtn_pointer_to_deref(b
, image
.image
);
1993 intrin
->variables
[0] = nir_deref_var_clone(image_deref
, intrin
);
1995 /* ImageQuerySize doesn't take any extra parameters */
1996 if (opcode
!= SpvOpImageQuerySize
) {
1997 /* The image coordinate is always 4 components but we may not have that
1998 * many. Swizzle to compensate.
2001 for (unsigned i
= 0; i
< 4; i
++)
2002 swiz
[i
] = i
< image
.coord
->num_components
? i
: 0;
2003 intrin
->src
[0] = nir_src_for_ssa(nir_swizzle(&b
->nb
, image
.coord
,
2005 intrin
->src
[1] = nir_src_for_ssa(image
.sample
);
2009 case SpvOpAtomicLoad
:
2010 case SpvOpImageQuerySize
:
2011 case SpvOpImageRead
:
2013 case SpvOpAtomicStore
:
2014 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2016 case SpvOpImageWrite
:
2017 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[3])->def
);
2020 case SpvOpAtomicIIncrement
:
2021 case SpvOpAtomicIDecrement
:
2022 case SpvOpAtomicExchange
:
2023 case SpvOpAtomicIAdd
:
2024 case SpvOpAtomicSMin
:
2025 case SpvOpAtomicUMin
:
2026 case SpvOpAtomicSMax
:
2027 case SpvOpAtomicUMax
:
2028 case SpvOpAtomicAnd
:
2030 case SpvOpAtomicXor
:
2031 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[2]);
2035 unreachable("Invalid image opcode");
2038 if (opcode
!= SpvOpImageWrite
) {
2039 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2040 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2042 unsigned dest_components
=
2043 nir_intrinsic_infos
[intrin
->intrinsic
].dest_components
;
2044 if (intrin
->intrinsic
== nir_intrinsic_image_size
) {
2045 dest_components
= intrin
->num_components
=
2046 glsl_get_vector_elements(type
->type
);
2049 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
2050 dest_components
, 32, NULL
);
2052 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2054 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
2055 val
->ssa
->def
= &intrin
->dest
.ssa
;
2057 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2061 static nir_intrinsic_op
2062 get_ssbo_nir_atomic_op(SpvOp opcode
)
2065 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
2066 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
2067 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2068 OP(AtomicExchange
, atomic_exchange
)
2069 OP(AtomicCompareExchange
, atomic_comp_swap
)
2070 OP(AtomicIIncrement
, atomic_add
)
2071 OP(AtomicIDecrement
, atomic_add
)
2072 OP(AtomicIAdd
, atomic_add
)
2073 OP(AtomicISub
, atomic_add
)
2074 OP(AtomicSMin
, atomic_imin
)
2075 OP(AtomicUMin
, atomic_umin
)
2076 OP(AtomicSMax
, atomic_imax
)
2077 OP(AtomicUMax
, atomic_umax
)
2078 OP(AtomicAnd
, atomic_and
)
2079 OP(AtomicOr
, atomic_or
)
2080 OP(AtomicXor
, atomic_xor
)
2083 unreachable("Invalid SSBO atomic");
2087 static nir_intrinsic_op
2088 get_shared_nir_atomic_op(SpvOp opcode
)
2091 case SpvOpAtomicLoad
: return nir_intrinsic_load_var
;
2092 case SpvOpAtomicStore
: return nir_intrinsic_store_var
;
2093 #define OP(S, N) case SpvOp##S: return nir_intrinsic_var_##N;
2094 OP(AtomicExchange
, atomic_exchange
)
2095 OP(AtomicCompareExchange
, atomic_comp_swap
)
2096 OP(AtomicIIncrement
, atomic_add
)
2097 OP(AtomicIDecrement
, atomic_add
)
2098 OP(AtomicIAdd
, atomic_add
)
2099 OP(AtomicISub
, atomic_add
)
2100 OP(AtomicSMin
, atomic_imin
)
2101 OP(AtomicUMin
, atomic_umin
)
2102 OP(AtomicSMax
, atomic_imax
)
2103 OP(AtomicUMax
, atomic_umax
)
2104 OP(AtomicAnd
, atomic_and
)
2105 OP(AtomicOr
, atomic_or
)
2106 OP(AtomicXor
, atomic_xor
)
2109 unreachable("Invalid shared atomic");
2114 vtn_handle_ssbo_or_shared_atomic(struct vtn_builder
*b
, SpvOp opcode
,
2115 const uint32_t *w
, unsigned count
)
2117 struct vtn_pointer
*ptr
;
2118 nir_intrinsic_instr
*atomic
;
2121 case SpvOpAtomicLoad
:
2122 case SpvOpAtomicExchange
:
2123 case SpvOpAtomicCompareExchange
:
2124 case SpvOpAtomicCompareExchangeWeak
:
2125 case SpvOpAtomicIIncrement
:
2126 case SpvOpAtomicIDecrement
:
2127 case SpvOpAtomicIAdd
:
2128 case SpvOpAtomicISub
:
2129 case SpvOpAtomicSMin
:
2130 case SpvOpAtomicUMin
:
2131 case SpvOpAtomicSMax
:
2132 case SpvOpAtomicUMax
:
2133 case SpvOpAtomicAnd
:
2135 case SpvOpAtomicXor
:
2136 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
2139 case SpvOpAtomicStore
:
2140 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
2144 unreachable("Invalid SPIR-V atomic");
2148 SpvScope scope = w[4];
2149 SpvMemorySemanticsMask semantics = w[5];
2152 if (ptr
->mode
== vtn_variable_mode_workgroup
) {
2153 nir_deref_var
*deref
= vtn_pointer_to_deref(b
, ptr
);
2154 const struct glsl_type
*deref_type
= nir_deref_tail(&deref
->deref
)->type
;
2155 nir_intrinsic_op op
= get_shared_nir_atomic_op(opcode
);
2156 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2157 atomic
->variables
[0] = nir_deref_var_clone(deref
, atomic
);
2160 case SpvOpAtomicLoad
:
2161 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2164 case SpvOpAtomicStore
:
2165 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
2166 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2167 atomic
->src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2170 case SpvOpAtomicExchange
:
2171 case SpvOpAtomicCompareExchange
:
2172 case SpvOpAtomicCompareExchangeWeak
:
2173 case SpvOpAtomicIIncrement
:
2174 case SpvOpAtomicIDecrement
:
2175 case SpvOpAtomicIAdd
:
2176 case SpvOpAtomicISub
:
2177 case SpvOpAtomicSMin
:
2178 case SpvOpAtomicUMin
:
2179 case SpvOpAtomicSMax
:
2180 case SpvOpAtomicUMax
:
2181 case SpvOpAtomicAnd
:
2183 case SpvOpAtomicXor
:
2184 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[0]);
2188 unreachable("Invalid SPIR-V atomic");
2192 assert(ptr
->mode
== vtn_variable_mode_ssbo
);
2193 nir_ssa_def
*offset
, *index
;
2194 offset
= vtn_pointer_to_offset(b
, ptr
, &index
, NULL
);
2196 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(opcode
);
2198 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
2201 case SpvOpAtomicLoad
:
2202 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2203 atomic
->src
[0] = nir_src_for_ssa(index
);
2204 atomic
->src
[1] = nir_src_for_ssa(offset
);
2207 case SpvOpAtomicStore
:
2208 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
2209 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
2210 atomic
->src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[4])->def
);
2211 atomic
->src
[1] = nir_src_for_ssa(index
);
2212 atomic
->src
[2] = nir_src_for_ssa(offset
);
2215 case SpvOpAtomicExchange
:
2216 case SpvOpAtomicCompareExchange
:
2217 case SpvOpAtomicCompareExchangeWeak
:
2218 case SpvOpAtomicIIncrement
:
2219 case SpvOpAtomicIDecrement
:
2220 case SpvOpAtomicIAdd
:
2221 case SpvOpAtomicISub
:
2222 case SpvOpAtomicSMin
:
2223 case SpvOpAtomicUMin
:
2224 case SpvOpAtomicSMax
:
2225 case SpvOpAtomicUMax
:
2226 case SpvOpAtomicAnd
:
2228 case SpvOpAtomicXor
:
2229 atomic
->src
[0] = nir_src_for_ssa(index
);
2230 atomic
->src
[1] = nir_src_for_ssa(offset
);
2231 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[2]);
2235 unreachable("Invalid SPIR-V atomic");
2239 if (opcode
!= SpvOpAtomicStore
) {
2240 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2242 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
2243 glsl_get_vector_elements(type
->type
),
2244 glsl_get_bit_size(type
->type
), NULL
);
2246 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2247 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
2248 val
->ssa
->def
= &atomic
->dest
.ssa
;
2249 val
->ssa
->type
= type
->type
;
2252 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
2255 static nir_alu_instr
*
2256 create_vec(nir_shader
*shader
, unsigned num_components
, unsigned bit_size
)
2259 switch (num_components
) {
2260 case 1: op
= nir_op_fmov
; break;
2261 case 2: op
= nir_op_vec2
; break;
2262 case 3: op
= nir_op_vec3
; break;
2263 case 4: op
= nir_op_vec4
; break;
2264 default: unreachable("bad vector size");
2267 nir_alu_instr
*vec
= nir_alu_instr_create(shader
, op
);
2268 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
2270 vec
->dest
.write_mask
= (1 << num_components
) - 1;
2275 struct vtn_ssa_value
*
2276 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
2278 if (src
->transposed
)
2279 return src
->transposed
;
2281 struct vtn_ssa_value
*dest
=
2282 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
2284 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
2285 nir_alu_instr
*vec
= create_vec(b
->shader
,
2286 glsl_get_matrix_columns(src
->type
),
2287 glsl_get_bit_size(src
->type
));
2288 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2289 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
2290 vec
->src
[0].swizzle
[0] = i
;
2292 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
2293 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
2294 vec
->src
[j
].swizzle
[0] = i
;
2297 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2298 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
2301 dest
->transposed
= src
;
2307 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
2309 unsigned swiz
[4] = { index
};
2310 return nir_swizzle(&b
->nb
, src
, swiz
, 1, true);
2314 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
2317 nir_alu_instr
*vec
= create_vec(b
->shader
, src
->num_components
,
2320 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
2322 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
2324 vec
->src
[i
].src
= nir_src_for_ssa(src
);
2325 vec
->src
[i
].swizzle
[0] = i
;
2329 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2331 return &vec
->dest
.dest
.ssa
;
2335 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2338 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
2339 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2340 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2341 vtn_vector_extract(b
, src
, i
), dest
);
2347 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2348 nir_ssa_def
*insert
, nir_ssa_def
*index
)
2350 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
2351 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2352 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2353 vtn_vector_insert(b
, src
, insert
, i
), dest
);
2358 static nir_ssa_def
*
2359 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
2360 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
2361 const uint32_t *indices
)
2363 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
, src0
->bit_size
);
2365 for (unsigned i
= 0; i
< num_components
; i
++) {
2366 uint32_t index
= indices
[i
];
2367 if (index
== 0xffffffff) {
2369 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
2370 } else if (index
< src0
->num_components
) {
2371 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
2372 vec
->src
[i
].swizzle
[0] = index
;
2374 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
2375 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
2379 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2381 return &vec
->dest
.dest
.ssa
;
2385 * Concatentates a number of vectors/scalars together to produce a vector
2387 static nir_ssa_def
*
2388 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
2389 unsigned num_srcs
, nir_ssa_def
**srcs
)
2391 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
,
2394 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2396 * "When constructing a vector, there must be at least two Constituent
2399 assert(num_srcs
>= 2);
2401 unsigned dest_idx
= 0;
2402 for (unsigned i
= 0; i
< num_srcs
; i
++) {
2403 nir_ssa_def
*src
= srcs
[i
];
2404 assert(dest_idx
+ src
->num_components
<= num_components
);
2405 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
2406 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
2407 vec
->src
[dest_idx
].swizzle
[0] = j
;
2412 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2414 * "When constructing a vector, the total number of components in all
2415 * the operands must equal the number of components in Result Type."
2417 assert(dest_idx
== num_components
);
2419 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2421 return &vec
->dest
.dest
.ssa
;
2424 static struct vtn_ssa_value
*
2425 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
2427 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
2428 dest
->type
= src
->type
;
2430 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2431 dest
->def
= src
->def
;
2433 unsigned elems
= glsl_get_length(src
->type
);
2435 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
2436 for (unsigned i
= 0; i
< elems
; i
++)
2437 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
2443 static struct vtn_ssa_value
*
2444 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2445 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
2446 unsigned num_indices
)
2448 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
2450 struct vtn_ssa_value
*cur
= dest
;
2452 for (i
= 0; i
< num_indices
- 1; i
++) {
2453 cur
= cur
->elems
[indices
[i
]];
2456 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2457 /* According to the SPIR-V spec, OpCompositeInsert may work down to
2458 * the component granularity. In that case, the last index will be
2459 * the index to insert the scalar into the vector.
2462 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
2464 cur
->elems
[indices
[i
]] = insert
;
2470 static struct vtn_ssa_value
*
2471 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2472 const uint32_t *indices
, unsigned num_indices
)
2474 struct vtn_ssa_value
*cur
= src
;
2475 for (unsigned i
= 0; i
< num_indices
; i
++) {
2476 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2477 assert(i
== num_indices
- 1);
2478 /* According to the SPIR-V spec, OpCompositeExtract may work down to
2479 * the component granularity. The last index will be the index of the
2480 * vector to extract.
2483 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
2484 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
2485 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
2488 cur
= cur
->elems
[indices
[i
]];
2496 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
2497 const uint32_t *w
, unsigned count
)
2499 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2500 const struct glsl_type
*type
=
2501 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
2502 val
->ssa
= vtn_create_ssa_value(b
, type
);
2505 case SpvOpVectorExtractDynamic
:
2506 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2507 vtn_ssa_value(b
, w
[4])->def
);
2510 case SpvOpVectorInsertDynamic
:
2511 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2512 vtn_ssa_value(b
, w
[4])->def
,
2513 vtn_ssa_value(b
, w
[5])->def
);
2516 case SpvOpVectorShuffle
:
2517 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
2518 vtn_ssa_value(b
, w
[3])->def
,
2519 vtn_ssa_value(b
, w
[4])->def
,
2523 case SpvOpCompositeConstruct
: {
2524 unsigned elems
= count
- 3;
2525 if (glsl_type_is_vector_or_scalar(type
)) {
2526 nir_ssa_def
*srcs
[4];
2527 for (unsigned i
= 0; i
< elems
; i
++)
2528 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
2530 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
2533 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2534 for (unsigned i
= 0; i
< elems
; i
++)
2535 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
2539 case SpvOpCompositeExtract
:
2540 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
2544 case SpvOpCompositeInsert
:
2545 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
2546 vtn_ssa_value(b
, w
[3]),
2550 case SpvOpCopyObject
:
2551 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
2555 unreachable("unknown composite operation");
2560 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
2561 const uint32_t *w
, unsigned count
)
2563 nir_intrinsic_op intrinsic_op
;
2565 case SpvOpEmitVertex
:
2566 case SpvOpEmitStreamVertex
:
2567 intrinsic_op
= nir_intrinsic_emit_vertex
;
2569 case SpvOpEndPrimitive
:
2570 case SpvOpEndStreamPrimitive
:
2571 intrinsic_op
= nir_intrinsic_end_primitive
;
2573 case SpvOpMemoryBarrier
:
2574 intrinsic_op
= nir_intrinsic_memory_barrier
;
2576 case SpvOpControlBarrier
:
2577 intrinsic_op
= nir_intrinsic_barrier
;
2580 unreachable("unknown barrier instruction");
2583 nir_intrinsic_instr
*intrin
=
2584 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
2586 if (opcode
== SpvOpEmitStreamVertex
|| opcode
== SpvOpEndStreamPrimitive
)
2587 nir_intrinsic_set_stream_id(intrin
, w
[1]);
2589 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2593 gl_primitive_from_spv_execution_mode(SpvExecutionMode mode
)
2596 case SpvExecutionModeInputPoints
:
2597 case SpvExecutionModeOutputPoints
:
2598 return 0; /* GL_POINTS */
2599 case SpvExecutionModeInputLines
:
2600 return 1; /* GL_LINES */
2601 case SpvExecutionModeInputLinesAdjacency
:
2602 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
2603 case SpvExecutionModeTriangles
:
2604 return 4; /* GL_TRIANGLES */
2605 case SpvExecutionModeInputTrianglesAdjacency
:
2606 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
2607 case SpvExecutionModeQuads
:
2608 return 7; /* GL_QUADS */
2609 case SpvExecutionModeIsolines
:
2610 return 0x8E7A; /* GL_ISOLINES */
2611 case SpvExecutionModeOutputLineStrip
:
2612 return 3; /* GL_LINE_STRIP */
2613 case SpvExecutionModeOutputTriangleStrip
:
2614 return 5; /* GL_TRIANGLE_STRIP */
2616 assert(!"Invalid primitive type");
2622 vertices_in_from_spv_execution_mode(SpvExecutionMode mode
)
2625 case SpvExecutionModeInputPoints
:
2627 case SpvExecutionModeInputLines
:
2629 case SpvExecutionModeInputLinesAdjacency
:
2631 case SpvExecutionModeTriangles
:
2633 case SpvExecutionModeInputTrianglesAdjacency
:
2636 assert(!"Invalid GS input mode");
2641 static gl_shader_stage
2642 stage_for_execution_model(SpvExecutionModel model
)
2645 case SpvExecutionModelVertex
:
2646 return MESA_SHADER_VERTEX
;
2647 case SpvExecutionModelTessellationControl
:
2648 return MESA_SHADER_TESS_CTRL
;
2649 case SpvExecutionModelTessellationEvaluation
:
2650 return MESA_SHADER_TESS_EVAL
;
2651 case SpvExecutionModelGeometry
:
2652 return MESA_SHADER_GEOMETRY
;
2653 case SpvExecutionModelFragment
:
2654 return MESA_SHADER_FRAGMENT
;
2655 case SpvExecutionModelGLCompute
:
2656 return MESA_SHADER_COMPUTE
;
2658 unreachable("Unsupported execution model");
2662 #define spv_check_supported(name, cap) do { \
2663 if (!(b->ext && b->ext->name)) \
2664 vtn_warn("Unsupported SPIR-V capability: %s", \
2665 spirv_capability_to_string(cap)); \
2669 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2670 const uint32_t *w
, unsigned count
)
2674 case SpvOpSourceExtension
:
2675 case SpvOpSourceContinued
:
2676 case SpvOpExtension
:
2677 /* Unhandled, but these are for debug so that's ok. */
2680 case SpvOpCapability
: {
2681 SpvCapability cap
= w
[1];
2683 case SpvCapabilityMatrix
:
2684 case SpvCapabilityShader
:
2685 case SpvCapabilityGeometry
:
2686 case SpvCapabilityGeometryPointSize
:
2687 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
2688 case SpvCapabilitySampledImageArrayDynamicIndexing
:
2689 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
2690 case SpvCapabilityStorageImageArrayDynamicIndexing
:
2691 case SpvCapabilityImageRect
:
2692 case SpvCapabilitySampledRect
:
2693 case SpvCapabilitySampled1D
:
2694 case SpvCapabilityImage1D
:
2695 case SpvCapabilitySampledCubeArray
:
2696 case SpvCapabilitySampledBuffer
:
2697 case SpvCapabilityImageBuffer
:
2698 case SpvCapabilityImageQuery
:
2699 case SpvCapabilityDerivativeControl
:
2700 case SpvCapabilityInterpolationFunction
:
2701 case SpvCapabilityMultiViewport
:
2702 case SpvCapabilitySampleRateShading
:
2703 case SpvCapabilityClipDistance
:
2704 case SpvCapabilityCullDistance
:
2705 case SpvCapabilityInputAttachment
:
2706 case SpvCapabilityImageGatherExtended
:
2707 case SpvCapabilityStorageImageExtendedFormats
:
2710 case SpvCapabilityGeometryStreams
:
2711 case SpvCapabilityLinkage
:
2712 case SpvCapabilityVector16
:
2713 case SpvCapabilityFloat16Buffer
:
2714 case SpvCapabilityFloat16
:
2715 case SpvCapabilityInt64Atomics
:
2716 case SpvCapabilityAtomicStorage
:
2717 case SpvCapabilityInt16
:
2718 case SpvCapabilityStorageImageMultisample
:
2719 case SpvCapabilityImageCubeArray
:
2720 case SpvCapabilityInt8
:
2721 case SpvCapabilitySparseResidency
:
2722 case SpvCapabilityMinLod
:
2723 case SpvCapabilityTransformFeedback
:
2724 vtn_warn("Unsupported SPIR-V capability: %s",
2725 spirv_capability_to_string(cap
));
2728 case SpvCapabilityFloat64
:
2729 spv_check_supported(float64
, cap
);
2731 case SpvCapabilityInt64
:
2732 spv_check_supported(int64
, cap
);
2735 case SpvCapabilityAddresses
:
2736 case SpvCapabilityKernel
:
2737 case SpvCapabilityImageBasic
:
2738 case SpvCapabilityImageReadWrite
:
2739 case SpvCapabilityImageMipmap
:
2740 case SpvCapabilityPipes
:
2741 case SpvCapabilityGroups
:
2742 case SpvCapabilityDeviceEnqueue
:
2743 case SpvCapabilityLiteralSampler
:
2744 case SpvCapabilityGenericPointer
:
2745 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
2746 spirv_capability_to_string(cap
));
2749 case SpvCapabilityImageMSArray
:
2750 spv_check_supported(image_ms_array
, cap
);
2753 case SpvCapabilityTessellation
:
2754 case SpvCapabilityTessellationPointSize
:
2755 spv_check_supported(tessellation
, cap
);
2758 case SpvCapabilityDrawParameters
:
2759 spv_check_supported(draw_parameters
, cap
);
2762 case SpvCapabilityStorageImageReadWithoutFormat
:
2763 spv_check_supported(image_read_without_format
, cap
);
2766 case SpvCapabilityStorageImageWriteWithoutFormat
:
2767 spv_check_supported(image_write_without_format
, cap
);
2770 case SpvCapabilityMultiView
:
2771 spv_check_supported(multiview
, cap
);
2775 unreachable("Unhandled capability");
2780 case SpvOpExtInstImport
:
2781 vtn_handle_extension(b
, opcode
, w
, count
);
2784 case SpvOpMemoryModel
:
2785 assert(w
[1] == SpvAddressingModelLogical
);
2786 assert(w
[2] == SpvMemoryModelGLSL450
);
2789 case SpvOpEntryPoint
: {
2790 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
2791 /* Let this be a name label regardless */
2792 unsigned name_words
;
2793 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
2795 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
2796 stage_for_execution_model(w
[1]) != b
->entry_point_stage
)
2799 assert(b
->entry_point
== NULL
);
2800 b
->entry_point
= entry_point
;
2805 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
2806 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2810 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2813 case SpvOpMemberName
:
2817 case SpvOpExecutionMode
:
2818 case SpvOpDecorationGroup
:
2820 case SpvOpMemberDecorate
:
2821 case SpvOpGroupDecorate
:
2822 case SpvOpGroupMemberDecorate
:
2823 vtn_handle_decoration(b
, opcode
, w
, count
);
2827 return false; /* End of preamble */
2834 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
2835 const struct vtn_decoration
*mode
, void *data
)
2837 assert(b
->entry_point
== entry_point
);
2839 switch(mode
->exec_mode
) {
2840 case SpvExecutionModeOriginUpperLeft
:
2841 case SpvExecutionModeOriginLowerLeft
:
2842 b
->origin_upper_left
=
2843 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
2846 case SpvExecutionModeEarlyFragmentTests
:
2847 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2848 b
->shader
->info
.fs
.early_fragment_tests
= true;
2851 case SpvExecutionModeInvocations
:
2852 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2853 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->literals
[0]);
2856 case SpvExecutionModeDepthReplacing
:
2857 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2858 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
2860 case SpvExecutionModeDepthGreater
:
2861 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2862 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
2864 case SpvExecutionModeDepthLess
:
2865 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2866 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
2868 case SpvExecutionModeDepthUnchanged
:
2869 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2870 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
2873 case SpvExecutionModeLocalSize
:
2874 assert(b
->shader
->stage
== MESA_SHADER_COMPUTE
);
2875 b
->shader
->info
.cs
.local_size
[0] = mode
->literals
[0];
2876 b
->shader
->info
.cs
.local_size
[1] = mode
->literals
[1];
2877 b
->shader
->info
.cs
.local_size
[2] = mode
->literals
[2];
2879 case SpvExecutionModeLocalSizeHint
:
2880 break; /* Nothing to do with this */
2882 case SpvExecutionModeOutputVertices
:
2883 if (b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2884 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
) {
2885 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->literals
[0];
2887 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2888 b
->shader
->info
.gs
.vertices_out
= mode
->literals
[0];
2892 case SpvExecutionModeInputPoints
:
2893 case SpvExecutionModeInputLines
:
2894 case SpvExecutionModeInputLinesAdjacency
:
2895 case SpvExecutionModeTriangles
:
2896 case SpvExecutionModeInputTrianglesAdjacency
:
2897 case SpvExecutionModeQuads
:
2898 case SpvExecutionModeIsolines
:
2899 if (b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2900 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
) {
2901 b
->shader
->info
.tess
.primitive_mode
=
2902 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
2904 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2905 b
->shader
->info
.gs
.vertices_in
=
2906 vertices_in_from_spv_execution_mode(mode
->exec_mode
);
2910 case SpvExecutionModeOutputPoints
:
2911 case SpvExecutionModeOutputLineStrip
:
2912 case SpvExecutionModeOutputTriangleStrip
:
2913 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2914 b
->shader
->info
.gs
.output_primitive
=
2915 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
2918 case SpvExecutionModeSpacingEqual
:
2919 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2920 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2921 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
2923 case SpvExecutionModeSpacingFractionalEven
:
2924 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2925 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2926 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
2928 case SpvExecutionModeSpacingFractionalOdd
:
2929 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2930 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2931 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
2933 case SpvExecutionModeVertexOrderCw
:
2934 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2935 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2936 /* Vulkan's notion of CCW seems to match the hardware backends,
2937 * but be the opposite of OpenGL. Currently NIR follows GL semantics,
2938 * so we set it backwards here.
2940 b
->shader
->info
.tess
.ccw
= true;
2942 case SpvExecutionModeVertexOrderCcw
:
2943 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2944 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2945 /* Backwards; see above */
2946 b
->shader
->info
.tess
.ccw
= false;
2948 case SpvExecutionModePointMode
:
2949 assert(b
->shader
->stage
== MESA_SHADER_TESS_CTRL
||
2950 b
->shader
->stage
== MESA_SHADER_TESS_EVAL
);
2951 b
->shader
->info
.tess
.point_mode
= true;
2954 case SpvExecutionModePixelCenterInteger
:
2955 b
->pixel_center_integer
= true;
2958 case SpvExecutionModeXfb
:
2959 assert(!"Unhandled execution mode");
2962 case SpvExecutionModeVecTypeHint
:
2963 case SpvExecutionModeContractionOff
:
2967 unreachable("Unhandled execution mode");
2972 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2973 const uint32_t *w
, unsigned count
)
2977 case SpvOpSourceContinued
:
2978 case SpvOpSourceExtension
:
2979 case SpvOpExtension
:
2980 case SpvOpCapability
:
2981 case SpvOpExtInstImport
:
2982 case SpvOpMemoryModel
:
2983 case SpvOpEntryPoint
:
2984 case SpvOpExecutionMode
:
2987 case SpvOpMemberName
:
2988 case SpvOpDecorationGroup
:
2990 case SpvOpMemberDecorate
:
2991 case SpvOpGroupDecorate
:
2992 case SpvOpGroupMemberDecorate
:
2993 assert(!"Invalid opcode types and variables section");
2999 case SpvOpTypeFloat
:
3000 case SpvOpTypeVector
:
3001 case SpvOpTypeMatrix
:
3002 case SpvOpTypeImage
:
3003 case SpvOpTypeSampler
:
3004 case SpvOpTypeSampledImage
:
3005 case SpvOpTypeArray
:
3006 case SpvOpTypeRuntimeArray
:
3007 case SpvOpTypeStruct
:
3008 case SpvOpTypeOpaque
:
3009 case SpvOpTypePointer
:
3010 case SpvOpTypeFunction
:
3011 case SpvOpTypeEvent
:
3012 case SpvOpTypeDeviceEvent
:
3013 case SpvOpTypeReserveId
:
3014 case SpvOpTypeQueue
:
3016 vtn_handle_type(b
, opcode
, w
, count
);
3019 case SpvOpConstantTrue
:
3020 case SpvOpConstantFalse
:
3022 case SpvOpConstantComposite
:
3023 case SpvOpConstantSampler
:
3024 case SpvOpConstantNull
:
3025 case SpvOpSpecConstantTrue
:
3026 case SpvOpSpecConstantFalse
:
3027 case SpvOpSpecConstant
:
3028 case SpvOpSpecConstantComposite
:
3029 case SpvOpSpecConstantOp
:
3030 vtn_handle_constant(b
, opcode
, w
, count
);
3035 vtn_handle_variables(b
, opcode
, w
, count
);
3039 return false; /* End of preamble */
3046 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3047 const uint32_t *w
, unsigned count
)
3053 case SpvOpLoopMerge
:
3054 case SpvOpSelectionMerge
:
3055 /* This is handled by cfg pre-pass and walk_blocks */
3059 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
3060 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
3065 vtn_handle_extension(b
, opcode
, w
, count
);
3071 case SpvOpCopyMemory
:
3072 case SpvOpCopyMemorySized
:
3073 case SpvOpAccessChain
:
3074 case SpvOpInBoundsAccessChain
:
3075 case SpvOpArrayLength
:
3076 vtn_handle_variables(b
, opcode
, w
, count
);
3079 case SpvOpFunctionCall
:
3080 vtn_handle_function_call(b
, opcode
, w
, count
);
3083 case SpvOpSampledImage
:
3085 case SpvOpImageSampleImplicitLod
:
3086 case SpvOpImageSampleExplicitLod
:
3087 case SpvOpImageSampleDrefImplicitLod
:
3088 case SpvOpImageSampleDrefExplicitLod
:
3089 case SpvOpImageSampleProjImplicitLod
:
3090 case SpvOpImageSampleProjExplicitLod
:
3091 case SpvOpImageSampleProjDrefImplicitLod
:
3092 case SpvOpImageSampleProjDrefExplicitLod
:
3093 case SpvOpImageFetch
:
3094 case SpvOpImageGather
:
3095 case SpvOpImageDrefGather
:
3096 case SpvOpImageQuerySizeLod
:
3097 case SpvOpImageQueryLod
:
3098 case SpvOpImageQueryLevels
:
3099 case SpvOpImageQuerySamples
:
3100 vtn_handle_texture(b
, opcode
, w
, count
);
3103 case SpvOpImageRead
:
3104 case SpvOpImageWrite
:
3105 case SpvOpImageTexelPointer
:
3106 vtn_handle_image(b
, opcode
, w
, count
);
3109 case SpvOpImageQuerySize
: {
3110 struct vtn_pointer
*image
=
3111 vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
3112 if (image
->mode
== vtn_variable_mode_image
) {
3113 vtn_handle_image(b
, opcode
, w
, count
);
3115 assert(image
->mode
== vtn_variable_mode_sampler
);
3116 vtn_handle_texture(b
, opcode
, w
, count
);
3121 case SpvOpAtomicLoad
:
3122 case SpvOpAtomicExchange
:
3123 case SpvOpAtomicCompareExchange
:
3124 case SpvOpAtomicCompareExchangeWeak
:
3125 case SpvOpAtomicIIncrement
:
3126 case SpvOpAtomicIDecrement
:
3127 case SpvOpAtomicIAdd
:
3128 case SpvOpAtomicISub
:
3129 case SpvOpAtomicSMin
:
3130 case SpvOpAtomicUMin
:
3131 case SpvOpAtomicSMax
:
3132 case SpvOpAtomicUMax
:
3133 case SpvOpAtomicAnd
:
3135 case SpvOpAtomicXor
: {
3136 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
3137 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
3138 vtn_handle_image(b
, opcode
, w
, count
);
3140 assert(pointer
->value_type
== vtn_value_type_pointer
);
3141 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
3146 case SpvOpAtomicStore
: {
3147 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
3148 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
3149 vtn_handle_image(b
, opcode
, w
, count
);
3151 assert(pointer
->value_type
== vtn_value_type_pointer
);
3152 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
3162 case SpvOpConvertFToU
:
3163 case SpvOpConvertFToS
:
3164 case SpvOpConvertSToF
:
3165 case SpvOpConvertUToF
:
3169 case SpvOpQuantizeToF16
:
3170 case SpvOpConvertPtrToU
:
3171 case SpvOpConvertUToPtr
:
3172 case SpvOpPtrCastToGeneric
:
3173 case SpvOpGenericCastToPtr
:
3179 case SpvOpSignBitSet
:
3180 case SpvOpLessOrGreater
:
3182 case SpvOpUnordered
:
3197 case SpvOpVectorTimesScalar
:
3199 case SpvOpIAddCarry
:
3200 case SpvOpISubBorrow
:
3201 case SpvOpUMulExtended
:
3202 case SpvOpSMulExtended
:
3203 case SpvOpShiftRightLogical
:
3204 case SpvOpShiftRightArithmetic
:
3205 case SpvOpShiftLeftLogical
:
3206 case SpvOpLogicalEqual
:
3207 case SpvOpLogicalNotEqual
:
3208 case SpvOpLogicalOr
:
3209 case SpvOpLogicalAnd
:
3210 case SpvOpLogicalNot
:
3211 case SpvOpBitwiseOr
:
3212 case SpvOpBitwiseXor
:
3213 case SpvOpBitwiseAnd
:
3216 case SpvOpFOrdEqual
:
3217 case SpvOpFUnordEqual
:
3218 case SpvOpINotEqual
:
3219 case SpvOpFOrdNotEqual
:
3220 case SpvOpFUnordNotEqual
:
3221 case SpvOpULessThan
:
3222 case SpvOpSLessThan
:
3223 case SpvOpFOrdLessThan
:
3224 case SpvOpFUnordLessThan
:
3225 case SpvOpUGreaterThan
:
3226 case SpvOpSGreaterThan
:
3227 case SpvOpFOrdGreaterThan
:
3228 case SpvOpFUnordGreaterThan
:
3229 case SpvOpULessThanEqual
:
3230 case SpvOpSLessThanEqual
:
3231 case SpvOpFOrdLessThanEqual
:
3232 case SpvOpFUnordLessThanEqual
:
3233 case SpvOpUGreaterThanEqual
:
3234 case SpvOpSGreaterThanEqual
:
3235 case SpvOpFOrdGreaterThanEqual
:
3236 case SpvOpFUnordGreaterThanEqual
:
3242 case SpvOpFwidthFine
:
3243 case SpvOpDPdxCoarse
:
3244 case SpvOpDPdyCoarse
:
3245 case SpvOpFwidthCoarse
:
3246 case SpvOpBitFieldInsert
:
3247 case SpvOpBitFieldSExtract
:
3248 case SpvOpBitFieldUExtract
:
3249 case SpvOpBitReverse
:
3251 case SpvOpTranspose
:
3252 case SpvOpOuterProduct
:
3253 case SpvOpMatrixTimesScalar
:
3254 case SpvOpVectorTimesMatrix
:
3255 case SpvOpMatrixTimesVector
:
3256 case SpvOpMatrixTimesMatrix
:
3257 vtn_handle_alu(b
, opcode
, w
, count
);
3260 case SpvOpVectorExtractDynamic
:
3261 case SpvOpVectorInsertDynamic
:
3262 case SpvOpVectorShuffle
:
3263 case SpvOpCompositeConstruct
:
3264 case SpvOpCompositeExtract
:
3265 case SpvOpCompositeInsert
:
3266 case SpvOpCopyObject
:
3267 vtn_handle_composite(b
, opcode
, w
, count
);
3270 case SpvOpEmitVertex
:
3271 case SpvOpEndPrimitive
:
3272 case SpvOpEmitStreamVertex
:
3273 case SpvOpEndStreamPrimitive
:
3274 case SpvOpControlBarrier
:
3275 case SpvOpMemoryBarrier
:
3276 vtn_handle_barrier(b
, opcode
, w
, count
);
3280 unreachable("Unhandled opcode");
3287 spirv_to_nir(const uint32_t *words
, size_t word_count
,
3288 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
3289 gl_shader_stage stage
, const char *entry_point_name
,
3290 const struct nir_spirv_supported_extensions
*ext
,
3291 const nir_shader_compiler_options
*options
)
3293 const uint32_t *word_end
= words
+ word_count
;
3295 /* Handle the SPIR-V header (first 4 dwords) */
3296 assert(word_count
> 5);
3298 assert(words
[0] == SpvMagicNumber
);
3299 assert(words
[1] >= 0x10000);
3300 /* words[2] == generator magic */
3301 unsigned value_id_bound
= words
[3];
3302 assert(words
[4] == 0);
3306 /* Initialize the stn_builder object */
3307 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
3308 b
->value_id_bound
= value_id_bound
;
3309 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
3310 exec_list_make_empty(&b
->functions
);
3311 b
->entry_point_stage
= stage
;
3312 b
->entry_point_name
= entry_point_name
;
3315 /* Handle all the preamble instructions */
3316 words
= vtn_foreach_instruction(b
, words
, word_end
,
3317 vtn_handle_preamble_instruction
);
3319 if (b
->entry_point
== NULL
) {
3320 assert(!"Entry point not found");
3325 b
->shader
= nir_shader_create(NULL
, stage
, options
, NULL
);
3327 /* Set shader info defaults */
3328 b
->shader
->info
.gs
.invocations
= 1;
3330 /* Parse execution modes */
3331 vtn_foreach_execution_mode(b
, b
->entry_point
,
3332 vtn_handle_execution_mode
, NULL
);
3334 b
->specializations
= spec
;
3335 b
->num_specializations
= num_spec
;
3337 /* Handle all variable, type, and constant instructions */
3338 words
= vtn_foreach_instruction(b
, words
, word_end
,
3339 vtn_handle_variable_or_type_instruction
);
3341 vtn_build_cfg(b
, words
, word_end
);
3343 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
3344 b
->impl
= func
->impl
;
3345 b
->const_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
3346 _mesa_key_pointer_equal
);
3348 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
3351 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
3352 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
3353 assert(entry_point
);