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"
33 static struct vtn_ssa_value
*
34 vtn_undef_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
36 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
39 if (glsl_type_is_vector_or_scalar(type
)) {
40 unsigned num_components
= glsl_get_vector_elements(val
->type
);
41 unsigned bit_size
= glsl_get_bit_size(val
->type
);
42 val
->def
= nir_ssa_undef(&b
->nb
, num_components
, bit_size
);
44 unsigned elems
= glsl_get_length(val
->type
);
45 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
46 if (glsl_type_is_matrix(type
)) {
47 const struct glsl_type
*elem_type
=
48 glsl_vector_type(glsl_get_base_type(type
),
49 glsl_get_vector_elements(type
));
51 for (unsigned i
= 0; i
< elems
; i
++)
52 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
53 } else if (glsl_type_is_array(type
)) {
54 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
55 for (unsigned i
= 0; i
< elems
; i
++)
56 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
58 for (unsigned i
= 0; i
< elems
; i
++) {
59 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
60 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
68 static struct vtn_ssa_value
*
69 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
70 const struct glsl_type
*type
)
72 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
77 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
80 switch (glsl_get_base_type(type
)) {
85 case GLSL_TYPE_DOUBLE
:
86 if (glsl_type_is_vector_or_scalar(type
)) {
87 unsigned num_components
= glsl_get_vector_elements(val
->type
);
88 nir_load_const_instr
*load
=
89 nir_load_const_instr_create(b
->shader
, num_components
, 32);
91 for (unsigned i
= 0; i
< num_components
; i
++)
92 load
->value
.u32
[i
] = constant
->value
.u
[i
];
94 nir_instr_insert_before_cf_list(&b
->impl
->body
, &load
->instr
);
95 val
->def
= &load
->def
;
97 assert(glsl_type_is_matrix(type
));
98 unsigned rows
= glsl_get_vector_elements(val
->type
);
99 unsigned columns
= glsl_get_matrix_columns(val
->type
);
100 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, columns
);
102 for (unsigned i
= 0; i
< columns
; i
++) {
103 struct vtn_ssa_value
*col_val
= rzalloc(b
, struct vtn_ssa_value
);
104 col_val
->type
= glsl_get_column_type(val
->type
);
105 nir_load_const_instr
*load
=
106 nir_load_const_instr_create(b
->shader
, rows
, 32);
108 for (unsigned j
= 0; j
< rows
; j
++)
109 load
->value
.u32
[j
] = constant
->value
.u
[rows
* i
+ j
];
111 nir_instr_insert_before_cf_list(&b
->impl
->body
, &load
->instr
);
112 col_val
->def
= &load
->def
;
114 val
->elems
[i
] = col_val
;
119 case GLSL_TYPE_ARRAY
: {
120 unsigned elems
= glsl_get_length(val
->type
);
121 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
122 const struct glsl_type
*elem_type
= glsl_get_array_element(val
->type
);
123 for (unsigned i
= 0; i
< elems
; i
++)
124 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
129 case GLSL_TYPE_STRUCT
: {
130 unsigned elems
= glsl_get_length(val
->type
);
131 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
132 for (unsigned i
= 0; i
< elems
; i
++) {
133 const struct glsl_type
*elem_type
=
134 glsl_get_struct_field(val
->type
, i
);
135 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
142 unreachable("bad constant type");
148 struct vtn_ssa_value
*
149 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
151 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
152 switch (val
->value_type
) {
153 case vtn_value_type_undef
:
154 return vtn_undef_ssa_value(b
, val
->type
->type
);
156 case vtn_value_type_constant
:
157 return vtn_const_ssa_value(b
, val
->constant
, val
->const_type
);
159 case vtn_value_type_ssa
:
162 case vtn_value_type_access_chain
:
163 /* This is needed for function parameters */
164 return vtn_variable_load(b
, val
->access_chain
);
167 unreachable("Invalid type for an SSA value");
172 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
173 unsigned word_count
, unsigned *words_used
)
175 char *dup
= ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
177 /* Ammount of space taken by the string (including the null) */
178 unsigned len
= strlen(dup
) + 1;
179 *words_used
= DIV_ROUND_UP(len
, sizeof(*words
));
185 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
186 const uint32_t *end
, vtn_instruction_handler handler
)
192 const uint32_t *w
= start
;
194 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
195 unsigned count
= w
[0] >> SpvWordCountShift
;
196 assert(count
>= 1 && w
+ count
<= end
);
200 break; /* Do nothing */
203 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
215 if (!handler(b
, opcode
, w
, count
))
227 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
228 const uint32_t *w
, unsigned count
)
231 case SpvOpExtInstImport
: {
232 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
233 if (strcmp((const char *)&w
[2], "GLSL.std.450") == 0) {
234 val
->ext_handler
= vtn_handle_glsl450_instruction
;
236 assert(!"Unsupported extension");
242 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
243 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
250 unreachable("Unhandled opcode");
255 _foreach_decoration_helper(struct vtn_builder
*b
,
256 struct vtn_value
*base_value
,
258 struct vtn_value
*value
,
259 vtn_decoration_foreach_cb cb
, void *data
)
261 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
263 if (dec
->scope
== VTN_DEC_DECORATION
) {
264 member
= parent_member
;
265 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
266 assert(parent_member
== -1);
267 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
269 /* Not a decoration */
274 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
275 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
278 cb(b
, base_value
, member
, dec
, data
);
283 /** Iterates (recursively if needed) over all of the decorations on a value
285 * This function iterates over all of the decorations applied to a given
286 * value. If it encounters a decoration group, it recurses into the group
287 * and iterates over all of those decorations as well.
290 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
291 vtn_decoration_foreach_cb cb
, void *data
)
293 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
297 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
298 vtn_execution_mode_foreach_cb cb
, void *data
)
300 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
301 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
304 assert(dec
->group
== NULL
);
305 cb(b
, value
, dec
, data
);
310 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
311 const uint32_t *w
, unsigned count
)
313 const uint32_t *w_end
= w
+ count
;
314 const uint32_t target
= w
[1];
318 case SpvOpDecorationGroup
:
319 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
323 case SpvOpMemberDecorate
:
324 case SpvOpExecutionMode
: {
325 struct vtn_value
*val
= &b
->values
[target
];
327 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
330 dec
->scope
= VTN_DEC_DECORATION
;
332 case SpvOpMemberDecorate
:
333 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
335 case SpvOpExecutionMode
:
336 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
339 unreachable("Invalid decoration opcode");
341 dec
->decoration
= *(w
++);
344 /* Link into the list */
345 dec
->next
= val
->decoration
;
346 val
->decoration
= dec
;
350 case SpvOpGroupMemberDecorate
:
351 case SpvOpGroupDecorate
: {
352 struct vtn_value
*group
=
353 vtn_value(b
, target
, vtn_value_type_decoration_group
);
355 for (; w
< w_end
; w
++) {
356 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
357 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
360 if (opcode
== SpvOpGroupDecorate
) {
361 dec
->scope
= VTN_DEC_DECORATION
;
363 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
366 /* Link into the list */
367 dec
->next
= val
->decoration
;
368 val
->decoration
= dec
;
374 unreachable("Unhandled opcode");
378 struct member_decoration_ctx
{
380 struct glsl_struct_field
*fields
;
381 struct vtn_type
*type
;
384 /* does a shallow copy of a vtn_type */
386 static struct vtn_type
*
387 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
389 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
390 dest
->type
= src
->type
;
391 dest
->is_builtin
= src
->is_builtin
;
393 dest
->builtin
= src
->builtin
;
395 if (!glsl_type_is_scalar(src
->type
)) {
396 switch (glsl_get_base_type(src
->type
)) {
400 case GLSL_TYPE_FLOAT
:
401 case GLSL_TYPE_DOUBLE
:
402 case GLSL_TYPE_ARRAY
:
403 dest
->row_major
= src
->row_major
;
404 dest
->stride
= src
->stride
;
405 dest
->array_element
= src
->array_element
;
408 case GLSL_TYPE_STRUCT
: {
409 unsigned elems
= glsl_get_length(src
->type
);
411 dest
->members
= ralloc_array(b
, struct vtn_type
*, elems
);
412 memcpy(dest
->members
, src
->members
, elems
* sizeof(struct vtn_type
*));
414 dest
->offsets
= ralloc_array(b
, unsigned, elems
);
415 memcpy(dest
->offsets
, src
->offsets
, elems
* sizeof(unsigned));
420 unreachable("unhandled type");
427 static struct vtn_type
*
428 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
430 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
431 type
= type
->members
[member
];
433 /* We may have an array of matrices.... Oh, joy! */
434 while (glsl_type_is_array(type
->type
)) {
435 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
436 type
= type
->array_element
;
439 assert(glsl_type_is_matrix(type
->type
));
445 struct_member_decoration_cb(struct vtn_builder
*b
,
446 struct vtn_value
*val
, int member
,
447 const struct vtn_decoration
*dec
, void *void_ctx
)
449 struct member_decoration_ctx
*ctx
= void_ctx
;
454 assert(member
< ctx
->num_fields
);
456 switch (dec
->decoration
) {
457 case SpvDecorationNonWritable
:
458 case SpvDecorationNonReadable
:
459 case SpvDecorationRelaxedPrecision
:
460 case SpvDecorationVolatile
:
461 case SpvDecorationCoherent
:
462 case SpvDecorationUniform
:
463 break; /* FIXME: Do nothing with this for now. */
464 case SpvDecorationNoPerspective
:
465 ctx
->fields
[member
].interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
467 case SpvDecorationFlat
:
468 ctx
->fields
[member
].interpolation
= INTERP_QUALIFIER_FLAT
;
470 case SpvDecorationCentroid
:
471 ctx
->fields
[member
].centroid
= true;
473 case SpvDecorationSample
:
474 ctx
->fields
[member
].sample
= true;
476 case SpvDecorationStream
:
477 /* Vulkan only allows one GS stream */
478 assert(dec
->literals
[0] == 0);
480 case SpvDecorationLocation
:
481 ctx
->fields
[member
].location
= dec
->literals
[0];
483 case SpvDecorationComponent
:
484 break; /* FIXME: What should we do with these? */
485 case SpvDecorationBuiltIn
:
486 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
487 ctx
->type
->members
[member
]->is_builtin
= true;
488 ctx
->type
->members
[member
]->builtin
= dec
->literals
[0];
489 ctx
->type
->builtin_block
= true;
491 case SpvDecorationOffset
:
492 ctx
->type
->offsets
[member
] = dec
->literals
[0];
494 case SpvDecorationMatrixStride
:
495 mutable_matrix_member(b
, ctx
->type
, member
)->stride
= dec
->literals
[0];
497 case SpvDecorationColMajor
:
498 break; /* Nothing to do here. Column-major is the default. */
499 case SpvDecorationRowMajor
:
500 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
503 case SpvDecorationPatch
:
504 unreachable("Tessellation not yet supported");
506 case SpvDecorationSpecId
:
507 case SpvDecorationBlock
:
508 case SpvDecorationBufferBlock
:
509 case SpvDecorationArrayStride
:
510 case SpvDecorationGLSLShared
:
511 case SpvDecorationGLSLPacked
:
512 case SpvDecorationInvariant
:
513 case SpvDecorationRestrict
:
514 case SpvDecorationAliased
:
515 case SpvDecorationConstant
:
516 case SpvDecorationIndex
:
517 case SpvDecorationBinding
:
518 case SpvDecorationDescriptorSet
:
519 case SpvDecorationNoContraction
:
520 case SpvDecorationInputAttachmentIndex
:
521 unreachable("Decoration not allowed on struct members");
523 case SpvDecorationXfbBuffer
:
524 case SpvDecorationXfbStride
:
525 unreachable("Vulkan does not have transform feedback");
527 case SpvDecorationCPacked
:
528 case SpvDecorationSaturatedConversion
:
529 case SpvDecorationFuncParamAttr
:
530 case SpvDecorationFPRoundingMode
:
531 case SpvDecorationFPFastMathMode
:
532 case SpvDecorationAlignment
:
533 unreachable("Decoraiton only allowed for CL-style kernels");
536 unreachable("Unhandled member decoration");
541 type_decoration_cb(struct vtn_builder
*b
,
542 struct vtn_value
*val
, int member
,
543 const struct vtn_decoration
*dec
, void *ctx
)
545 struct vtn_type
*type
= val
->type
;
550 switch (dec
->decoration
) {
551 case SpvDecorationArrayStride
:
552 type
->stride
= dec
->literals
[0];
554 case SpvDecorationBlock
:
557 case SpvDecorationBufferBlock
:
558 type
->buffer_block
= true;
560 case SpvDecorationGLSLShared
:
561 case SpvDecorationGLSLPacked
:
562 /* Ignore these, since we get explicit offsets anyways */
565 case SpvDecorationRowMajor
:
566 case SpvDecorationColMajor
:
567 case SpvDecorationMatrixStride
:
568 case SpvDecorationBuiltIn
:
569 case SpvDecorationNoPerspective
:
570 case SpvDecorationFlat
:
571 case SpvDecorationPatch
:
572 case SpvDecorationCentroid
:
573 case SpvDecorationSample
:
574 case SpvDecorationVolatile
:
575 case SpvDecorationCoherent
:
576 case SpvDecorationNonWritable
:
577 case SpvDecorationNonReadable
:
578 case SpvDecorationUniform
:
579 case SpvDecorationStream
:
580 case SpvDecorationLocation
:
581 case SpvDecorationComponent
:
582 case SpvDecorationOffset
:
583 case SpvDecorationXfbBuffer
:
584 case SpvDecorationXfbStride
:
585 unreachable("Decoraiton only allowed for struct members");
587 case SpvDecorationRelaxedPrecision
:
588 case SpvDecorationSpecId
:
589 case SpvDecorationInvariant
:
590 case SpvDecorationRestrict
:
591 case SpvDecorationAliased
:
592 case SpvDecorationConstant
:
593 case SpvDecorationIndex
:
594 case SpvDecorationBinding
:
595 case SpvDecorationDescriptorSet
:
596 case SpvDecorationLinkageAttributes
:
597 case SpvDecorationNoContraction
:
598 case SpvDecorationInputAttachmentIndex
:
599 unreachable("Decoraiton not allowed on types");
601 case SpvDecorationCPacked
:
602 case SpvDecorationSaturatedConversion
:
603 case SpvDecorationFuncParamAttr
:
604 case SpvDecorationFPRoundingMode
:
605 case SpvDecorationFPFastMathMode
:
606 case SpvDecorationAlignment
:
607 unreachable("Decoraiton only allowed for CL-style kernels");
612 translate_image_format(SpvImageFormat format
)
615 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
616 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
617 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
618 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
619 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
620 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
621 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
622 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
623 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
624 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
625 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
626 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
627 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
628 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
629 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
630 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
631 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
632 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
633 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
634 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
635 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
636 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
637 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
638 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
639 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
640 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
641 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
642 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
643 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
644 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
645 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
646 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
647 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
648 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
649 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
650 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
651 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
652 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
653 case SpvImageFormatR16ui
: return 0x823A; /* GL_RG16UI */
654 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
656 assert(!"Invalid image format");
662 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
663 const uint32_t *w
, unsigned count
)
665 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
667 val
->type
= rzalloc(b
, struct vtn_type
);
668 val
->type
->is_builtin
= false;
669 val
->type
->val
= val
;
673 val
->type
->type
= glsl_void_type();
676 val
->type
->type
= glsl_bool_type();
679 const bool signedness
= w
[3];
680 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
684 val
->type
->type
= glsl_float_type();
687 case SpvOpTypeVector
: {
688 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
689 unsigned elems
= w
[3];
691 assert(glsl_type_is_scalar(base
->type
));
692 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
694 /* Vectors implicitly have sizeof(base_type) stride. For now, this
695 * is always 4 bytes. This will have to change if we want to start
696 * supporting doubles or half-floats.
698 val
->type
->stride
= 4;
699 val
->type
->array_element
= base
;
703 case SpvOpTypeMatrix
: {
704 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
705 unsigned columns
= w
[3];
707 assert(glsl_type_is_vector(base
->type
));
708 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
709 glsl_get_vector_elements(base
->type
),
711 assert(!glsl_type_is_error(val
->type
->type
));
712 val
->type
->array_element
= base
;
713 val
->type
->row_major
= false;
714 val
->type
->stride
= 0;
718 case SpvOpTypeRuntimeArray
:
719 case SpvOpTypeArray
: {
720 struct vtn_type
*array_element
=
721 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
724 if (opcode
== SpvOpTypeRuntimeArray
) {
725 /* A length of 0 is used to denote unsized arrays */
729 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->value
.u
[0];
732 val
->type
->type
= glsl_array_type(array_element
->type
, length
);
733 val
->type
->array_element
= array_element
;
734 val
->type
->stride
= 0;
738 case SpvOpTypeStruct
: {
739 unsigned num_fields
= count
- 2;
740 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
741 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
743 NIR_VLA(struct glsl_struct_field
, fields
, count
);
744 for (unsigned i
= 0; i
< num_fields
; i
++) {
745 val
->type
->members
[i
] =
746 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
747 fields
[i
] = (struct glsl_struct_field
) {
748 .type
= val
->type
->members
[i
]->type
,
749 .name
= ralloc_asprintf(b
, "field%d", i
),
754 struct member_decoration_ctx ctx
= {
755 .num_fields
= num_fields
,
760 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
762 const char *name
= val
->name
? val
->name
: "struct";
764 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
);
768 case SpvOpTypeFunction
: {
769 const struct glsl_type
*return_type
=
770 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
771 NIR_VLA(struct glsl_function_param
, params
, count
- 3);
772 for (unsigned i
= 0; i
< count
- 3; i
++) {
773 params
[i
].type
= vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
->type
;
777 params
[i
].out
= true;
779 val
->type
->type
= glsl_function_type(return_type
, params
, count
- 3);
783 case SpvOpTypePointer
:
784 /* FIXME: For now, we'll just do the really lame thing and return
785 * the same type. The validator should ensure that the proper number
786 * of dereferences happen
788 val
->type
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
791 case SpvOpTypeImage
: {
792 const struct glsl_type
*sampled_type
=
793 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
795 assert(glsl_type_is_vector_or_scalar(sampled_type
));
797 enum glsl_sampler_dim dim
;
798 switch ((SpvDim
)w
[3]) {
799 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
800 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
801 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
802 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
803 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
804 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
806 unreachable("Invalid SPIR-V Sampler dimension");
809 bool is_shadow
= w
[4];
810 bool is_array
= w
[5];
811 bool multisampled
= w
[6];
812 unsigned sampled
= w
[7];
813 SpvImageFormat format
= w
[8];
816 val
->type
->access_qualifier
= w
[9];
818 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
821 assert(dim
== GLSL_SAMPLER_DIM_2D
);
822 dim
= GLSL_SAMPLER_DIM_MS
;
825 val
->type
->image_format
= translate_image_format(format
);
828 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
829 glsl_get_base_type(sampled_type
));
830 } else if (sampled
== 2) {
833 val
->type
->type
= glsl_image_type(dim
, is_array
,
834 glsl_get_base_type(sampled_type
));
836 assert(!"We need to know if the image will be sampled");
841 case SpvOpTypeSampledImage
:
842 val
->type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
845 case SpvOpTypeSampler
:
846 /* The actual sampler type here doesn't really matter. It gets
847 * thrown away the moment you combine it with an image. What really
848 * matters is that it's a sampler type as opposed to an integer type
849 * so the backend knows what to do.
851 val
->type
->type
= glsl_bare_sampler_type();
854 case SpvOpTypeOpaque
:
856 case SpvOpTypeDeviceEvent
:
857 case SpvOpTypeReserveId
:
861 unreachable("Unhandled opcode");
864 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
867 static nir_constant
*
868 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
870 nir_constant
*c
= rzalloc(b
, nir_constant
);
872 switch (glsl_get_base_type(type
)) {
876 case GLSL_TYPE_FLOAT
:
877 case GLSL_TYPE_DOUBLE
:
878 /* Nothing to do here. It's already initialized to zero */
881 case GLSL_TYPE_ARRAY
:
882 assert(glsl_get_length(type
) > 0);
883 c
->num_elements
= glsl_get_length(type
);
884 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
886 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
887 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
888 c
->elements
[i
] = c
->elements
[0];
891 case GLSL_TYPE_STRUCT
:
892 c
->num_elements
= glsl_get_length(type
);
893 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
895 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
896 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
901 unreachable("Invalid type for null constant");
908 spec_constant_deocoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
909 int member
, const struct vtn_decoration
*dec
,
912 assert(member
== -1);
913 if (dec
->decoration
!= SpvDecorationSpecId
)
916 uint32_t *const_value
= data
;
918 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
919 if (b
->specializations
[i
].id
== dec
->literals
[0]) {
920 *const_value
= b
->specializations
[i
].data
;
927 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
928 uint32_t const_value
)
930 vtn_foreach_decoration(b
, val
, spec_constant_deocoration_cb
, &const_value
);
935 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
936 const uint32_t *w
, unsigned count
)
938 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
939 val
->const_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
940 val
->constant
= rzalloc(b
, nir_constant
);
942 case SpvOpConstantTrue
:
943 assert(val
->const_type
== glsl_bool_type());
944 val
->constant
->value
.u
[0] = NIR_TRUE
;
946 case SpvOpConstantFalse
:
947 assert(val
->const_type
== glsl_bool_type());
948 val
->constant
->value
.u
[0] = NIR_FALSE
;
951 case SpvOpSpecConstantTrue
:
952 case SpvOpSpecConstantFalse
: {
953 assert(val
->const_type
== glsl_bool_type());
955 get_specialization(b
, val
, (opcode
== SpvOpSpecConstantTrue
));
956 val
->constant
->value
.u
[0] = int_val
? NIR_TRUE
: NIR_FALSE
;
961 assert(glsl_type_is_scalar(val
->const_type
));
962 val
->constant
->value
.u
[0] = w
[3];
964 case SpvOpSpecConstant
:
965 assert(glsl_type_is_scalar(val
->const_type
));
966 val
->constant
->value
.u
[0] = get_specialization(b
, val
, w
[3]);
968 case SpvOpSpecConstantComposite
:
969 case SpvOpConstantComposite
: {
970 unsigned elem_count
= count
- 3;
971 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
972 for (unsigned i
= 0; i
< elem_count
; i
++)
973 elems
[i
] = vtn_value(b
, w
[i
+ 3], vtn_value_type_constant
)->constant
;
975 switch (glsl_get_base_type(val
->const_type
)) {
978 case GLSL_TYPE_FLOAT
:
980 if (glsl_type_is_matrix(val
->const_type
)) {
981 unsigned rows
= glsl_get_vector_elements(val
->const_type
);
982 assert(glsl_get_matrix_columns(val
->const_type
) == elem_count
);
983 for (unsigned i
= 0; i
< elem_count
; i
++)
984 for (unsigned j
= 0; j
< rows
; j
++)
985 val
->constant
->value
.u
[rows
* i
+ j
] = elems
[i
]->value
.u
[j
];
987 assert(glsl_type_is_vector(val
->const_type
));
988 assert(glsl_get_vector_elements(val
->const_type
) == elem_count
);
989 for (unsigned i
= 0; i
< elem_count
; i
++)
990 val
->constant
->value
.u
[i
] = elems
[i
]->value
.u
[0];
995 case GLSL_TYPE_STRUCT
:
996 case GLSL_TYPE_ARRAY
:
997 ralloc_steal(val
->constant
, elems
);
998 val
->constant
->num_elements
= elem_count
;
999 val
->constant
->elements
= elems
;
1003 unreachable("Unsupported type for constants");
1008 case SpvOpSpecConstantOp
: {
1009 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1011 case SpvOpVectorShuffle
: {
1012 struct vtn_value
*v0
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1013 struct vtn_value
*v1
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1014 unsigned len0
= glsl_get_vector_elements(v0
->const_type
);
1015 unsigned len1
= glsl_get_vector_elements(v1
->const_type
);
1018 for (unsigned i
= 0; i
< len0
; i
++)
1019 u
[i
] = v0
->constant
->value
.u
[i
];
1020 for (unsigned i
= 0; i
< len1
; i
++)
1021 u
[len0
+ i
] = v1
->constant
->value
.u
[i
];
1023 for (unsigned i
= 0; i
< count
- 6; i
++) {
1024 uint32_t comp
= w
[i
+ 6];
1025 if (comp
== (uint32_t)-1) {
1026 val
->constant
->value
.u
[i
] = 0xdeadbeef;
1028 val
->constant
->value
.u
[i
] = u
[comp
];
1034 case SpvOpCompositeExtract
:
1035 case SpvOpCompositeInsert
: {
1036 struct vtn_value
*comp
;
1037 unsigned deref_start
;
1038 struct nir_constant
**c
;
1039 if (opcode
== SpvOpCompositeExtract
) {
1040 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1042 c
= &comp
->constant
;
1044 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1046 val
->constant
= nir_constant_clone(comp
->constant
,
1052 const struct glsl_type
*type
= comp
->const_type
;
1053 for (unsigned i
= deref_start
; i
< count
; i
++) {
1054 switch (glsl_get_base_type(type
)) {
1055 case GLSL_TYPE_UINT
:
1057 case GLSL_TYPE_FLOAT
:
1058 case GLSL_TYPE_BOOL
:
1059 /* If we hit this granularity, we're picking off an element */
1063 if (glsl_type_is_matrix(type
)) {
1064 elem
+= w
[i
] * glsl_get_vector_elements(type
);
1065 type
= glsl_get_column_type(type
);
1067 assert(glsl_type_is_vector(type
));
1069 type
= glsl_scalar_type(glsl_get_base_type(type
));
1073 case GLSL_TYPE_ARRAY
:
1074 c
= &(*c
)->elements
[w
[i
]];
1075 type
= glsl_get_array_element(type
);
1078 case GLSL_TYPE_STRUCT
:
1079 c
= &(*c
)->elements
[w
[i
]];
1080 type
= glsl_get_struct_field(type
, w
[i
]);
1084 unreachable("Invalid constant type");
1088 if (opcode
== SpvOpCompositeExtract
) {
1092 unsigned num_components
= glsl_get_vector_elements(type
);
1093 for (unsigned i
= 0; i
< num_components
; i
++)
1094 val
->constant
->value
.u
[i
] = (*c
)->value
.u
[elem
+ i
];
1097 struct vtn_value
*insert
=
1098 vtn_value(b
, w
[4], vtn_value_type_constant
);
1099 assert(insert
->const_type
== type
);
1101 *c
= insert
->constant
;
1103 unsigned num_components
= glsl_get_vector_elements(type
);
1104 for (unsigned i
= 0; i
< num_components
; i
++)
1105 (*c
)->value
.u
[elem
+ i
] = insert
->constant
->value
.u
[i
];
1113 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(opcode
, &swap
);
1115 unsigned num_components
= glsl_get_vector_elements(val
->const_type
);
1117 glsl_get_bit_size(val
->const_type
);
1119 nir_const_value src
[4];
1121 for (unsigned i
= 0; i
< count
- 4; i
++) {
1123 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
)->constant
;
1125 unsigned j
= swap
? 1 - i
: i
;
1126 assert(bit_size
== 32);
1127 for (unsigned k
= 0; k
< num_components
; k
++)
1128 src
[j
].u32
[k
] = c
->value
.u
[k
];
1131 nir_const_value res
= nir_eval_const_opcode(op
, num_components
,
1134 for (unsigned k
= 0; k
< num_components
; k
++)
1135 val
->constant
->value
.u
[k
] = res
.u32
[k
];
1142 case SpvOpConstantNull
:
1143 val
->constant
= vtn_null_constant(b
, val
->const_type
);
1146 case SpvOpConstantSampler
:
1147 assert(!"OpConstantSampler requires Kernel Capability");
1151 unreachable("Unhandled opcode");
1156 vtn_handle_function_call(struct vtn_builder
*b
, SpvOp opcode
,
1157 const uint32_t *w
, unsigned count
)
1159 struct nir_function
*callee
=
1160 vtn_value(b
, w
[3], vtn_value_type_function
)->func
->impl
->function
;
1162 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, callee
);
1163 for (unsigned i
= 0; i
< call
->num_params
; i
++) {
1164 unsigned arg_id
= w
[4 + i
];
1165 struct vtn_value
*arg
= vtn_untyped_value(b
, arg_id
);
1166 if (arg
->value_type
== vtn_value_type_access_chain
) {
1167 nir_deref_var
*d
= vtn_access_chain_to_deref(b
, arg
->access_chain
);
1168 call
->params
[i
] = nir_deref_as_var(nir_copy_deref(call
, &d
->deref
));
1170 struct vtn_ssa_value
*arg_ssa
= vtn_ssa_value(b
, arg_id
);
1172 /* Make a temporary to store the argument in */
1174 nir_local_variable_create(b
->impl
, arg_ssa
->type
, "arg_tmp");
1175 call
->params
[i
] = nir_deref_var_create(call
, tmp
);
1177 vtn_local_store(b
, arg_ssa
, call
->params
[i
]);
1181 nir_variable
*out_tmp
= NULL
;
1182 if (!glsl_type_is_void(callee
->return_type
)) {
1183 out_tmp
= nir_local_variable_create(b
->impl
, callee
->return_type
,
1185 call
->return_deref
= nir_deref_var_create(call
, out_tmp
);
1188 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
1190 if (glsl_type_is_void(callee
->return_type
)) {
1191 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1193 struct vtn_value
*retval
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1194 retval
->ssa
= vtn_local_load(b
, call
->return_deref
);
1198 struct vtn_ssa_value
*
1199 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1201 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1204 if (!glsl_type_is_vector_or_scalar(type
)) {
1205 unsigned elems
= glsl_get_length(type
);
1206 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1207 for (unsigned i
= 0; i
< elems
; i
++) {
1208 const struct glsl_type
*child_type
;
1210 switch (glsl_get_base_type(type
)) {
1212 case GLSL_TYPE_UINT
:
1213 case GLSL_TYPE_BOOL
:
1214 case GLSL_TYPE_FLOAT
:
1215 case GLSL_TYPE_DOUBLE
:
1216 child_type
= glsl_get_column_type(type
);
1218 case GLSL_TYPE_ARRAY
:
1219 child_type
= glsl_get_array_element(type
);
1221 case GLSL_TYPE_STRUCT
:
1222 child_type
= glsl_get_struct_field(type
, i
);
1225 unreachable("unkown base type");
1228 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
1236 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
1239 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
1240 src
.src_type
= type
;
1245 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
1246 const uint32_t *w
, unsigned count
)
1248 if (opcode
== SpvOpSampledImage
) {
1249 struct vtn_value
*val
=
1250 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1251 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1252 val
->sampled_image
->image
=
1253 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1254 val
->sampled_image
->sampler
=
1255 vtn_value(b
, w
[4], vtn_value_type_access_chain
)->access_chain
;
1257 } else if (opcode
== SpvOpImage
) {
1258 struct vtn_value
*val
=
1259 vtn_push_value(b
, w
[2], vtn_value_type_access_chain
);
1260 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
1261 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
1262 val
->access_chain
= src_val
->sampled_image
->image
;
1264 assert(src_val
->value_type
== vtn_value_type_access_chain
);
1265 val
->access_chain
= src_val
->access_chain
;
1270 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1271 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1273 struct vtn_sampled_image sampled
;
1274 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
1275 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
1276 sampled
= *sampled_val
->sampled_image
;
1278 assert(sampled_val
->value_type
== vtn_value_type_access_chain
);
1279 sampled
.image
= NULL
;
1280 sampled
.sampler
= sampled_val
->access_chain
;
1283 const struct glsl_type
*image_type
;
1284 if (sampled
.image
) {
1285 image_type
= sampled
.image
->var
->var
->interface_type
;
1287 image_type
= sampled
.sampler
->var
->var
->interface_type
;
1290 nir_tex_src srcs
[8]; /* 8 should be enough */
1291 nir_tex_src
*p
= srcs
;
1295 bool has_coord
= false;
1297 case SpvOpImageSampleImplicitLod
:
1298 case SpvOpImageSampleExplicitLod
:
1299 case SpvOpImageSampleDrefImplicitLod
:
1300 case SpvOpImageSampleDrefExplicitLod
:
1301 case SpvOpImageSampleProjImplicitLod
:
1302 case SpvOpImageSampleProjExplicitLod
:
1303 case SpvOpImageSampleProjDrefImplicitLod
:
1304 case SpvOpImageSampleProjDrefExplicitLod
:
1305 case SpvOpImageFetch
:
1306 case SpvOpImageGather
:
1307 case SpvOpImageDrefGather
:
1308 case SpvOpImageQueryLod
: {
1309 /* All these types have the coordinate as their first real argument */
1310 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, w
[idx
++]);
1312 p
->src
= nir_src_for_ssa(coord
->def
);
1313 p
->src_type
= nir_tex_src_coord
;
1322 /* These all have an explicit depth value as their next source */
1324 case SpvOpImageSampleDrefImplicitLod
:
1325 case SpvOpImageSampleDrefExplicitLod
:
1326 case SpvOpImageSampleProjDrefImplicitLod
:
1327 case SpvOpImageSampleProjDrefExplicitLod
:
1328 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparitor
);
1334 /* For OpImageQuerySizeLod, we always have an LOD */
1335 if (opcode
== SpvOpImageQuerySizeLod
)
1336 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1338 /* Figure out the base texture operation */
1341 case SpvOpImageSampleImplicitLod
:
1342 case SpvOpImageSampleDrefImplicitLod
:
1343 case SpvOpImageSampleProjImplicitLod
:
1344 case SpvOpImageSampleProjDrefImplicitLod
:
1345 texop
= nir_texop_tex
;
1348 case SpvOpImageSampleExplicitLod
:
1349 case SpvOpImageSampleDrefExplicitLod
:
1350 case SpvOpImageSampleProjExplicitLod
:
1351 case SpvOpImageSampleProjDrefExplicitLod
:
1352 texop
= nir_texop_txl
;
1355 case SpvOpImageFetch
:
1356 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
1357 texop
= nir_texop_txf_ms
;
1359 texop
= nir_texop_txf
;
1363 case SpvOpImageGather
:
1364 case SpvOpImageDrefGather
:
1365 texop
= nir_texop_tg4
;
1368 case SpvOpImageQuerySizeLod
:
1369 case SpvOpImageQuerySize
:
1370 texop
= nir_texop_txs
;
1373 case SpvOpImageQueryLod
:
1374 texop
= nir_texop_lod
;
1377 case SpvOpImageQueryLevels
:
1378 texop
= nir_texop_query_levels
;
1381 case SpvOpImageQuerySamples
:
1383 unreachable("Unhandled opcode");
1386 /* Now we need to handle some number of optional arguments */
1388 uint32_t operands
= w
[idx
++];
1390 if (operands
& SpvImageOperandsBiasMask
) {
1391 assert(texop
== nir_texop_tex
);
1392 texop
= nir_texop_txb
;
1393 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
1396 if (operands
& SpvImageOperandsLodMask
) {
1397 assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
1398 texop
== nir_texop_txf_ms
|| texop
== nir_texop_txs
);
1399 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1402 if (operands
& SpvImageOperandsGradMask
) {
1403 assert(texop
== nir_texop_tex
);
1404 texop
= nir_texop_txd
;
1405 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
1406 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
1409 if (operands
& SpvImageOperandsOffsetMask
||
1410 operands
& SpvImageOperandsConstOffsetMask
)
1411 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
1413 if (operands
& SpvImageOperandsConstOffsetsMask
)
1414 assert(!"Constant offsets to texture gather not yet implemented");
1416 if (operands
& SpvImageOperandsSampleMask
) {
1417 assert(texop
== nir_texop_txf_ms
);
1418 texop
= nir_texop_txf_ms
;
1419 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
1422 /* We should have now consumed exactly all of the arguments */
1423 assert(idx
== count
);
1425 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
1428 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1430 instr
->sampler_dim
= glsl_get_sampler_dim(image_type
);
1431 instr
->is_array
= glsl_sampler_type_is_array(image_type
);
1432 instr
->is_shadow
= glsl_sampler_type_is_shadow(image_type
);
1433 instr
->is_new_style_shadow
= instr
->is_shadow
;
1436 switch (instr
->sampler_dim
) {
1437 case GLSL_SAMPLER_DIM_1D
:
1438 case GLSL_SAMPLER_DIM_BUF
:
1439 instr
->coord_components
= 1;
1441 case GLSL_SAMPLER_DIM_2D
:
1442 case GLSL_SAMPLER_DIM_RECT
:
1443 case GLSL_SAMPLER_DIM_MS
:
1444 instr
->coord_components
= 2;
1446 case GLSL_SAMPLER_DIM_3D
:
1447 case GLSL_SAMPLER_DIM_CUBE
:
1448 instr
->coord_components
= 3;
1451 assert("Invalid sampler type");
1454 if (instr
->is_array
)
1455 instr
->coord_components
++;
1457 instr
->coord_components
= 0;
1460 switch (glsl_get_sampler_result_type(image_type
)) {
1461 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
1462 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
1463 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
1464 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
1466 unreachable("Invalid base type for sampler result");
1469 nir_deref_var
*sampler
= vtn_access_chain_to_deref(b
, sampled
.sampler
);
1470 if (sampled
.image
) {
1471 nir_deref_var
*image
= vtn_access_chain_to_deref(b
, sampled
.image
);
1472 instr
->texture
= nir_deref_as_var(nir_copy_deref(instr
, &image
->deref
));
1474 instr
->texture
= nir_deref_as_var(nir_copy_deref(instr
, &sampler
->deref
));
1477 switch (instr
->op
) {
1482 /* These operations require a sampler */
1483 instr
->sampler
= nir_deref_as_var(nir_copy_deref(instr
, &sampler
->deref
));
1486 case nir_texop_txf_ms
:
1490 case nir_texop_query_levels
:
1491 case nir_texop_texture_samples
:
1492 case nir_texop_samples_identical
:
1494 instr
->sampler
= NULL
;
1496 case nir_texop_txf_ms_mcs
:
1497 unreachable("unexpected nir_texop_txf_ms_mcs");
1500 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1501 nir_tex_instr_dest_size(instr
), 32, NULL
);
1503 assert(glsl_get_vector_elements(ret_type
->type
) ==
1504 nir_tex_instr_dest_size(instr
));
1506 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
1507 val
->ssa
->def
= &instr
->dest
.ssa
;
1509 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
1512 static nir_ssa_def
*
1513 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
1515 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
1517 /* The image_load_store intrinsics assume a 4-dim coordinate */
1518 unsigned dim
= glsl_get_vector_elements(coord
->type
);
1519 unsigned swizzle
[4];
1520 for (unsigned i
= 0; i
< 4; i
++)
1521 swizzle
[i
] = MIN2(i
, dim
- 1);
1523 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
1527 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
1528 const uint32_t *w
, unsigned count
)
1530 /* Just get this one out of the way */
1531 if (opcode
== SpvOpImageTexelPointer
) {
1532 struct vtn_value
*val
=
1533 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
1534 val
->image
= ralloc(b
, struct vtn_image_pointer
);
1537 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1538 val
->image
->coord
= get_image_coord(b
, w
[4]);
1539 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
1543 struct vtn_image_pointer image
;
1546 case SpvOpAtomicExchange
:
1547 case SpvOpAtomicCompareExchange
:
1548 case SpvOpAtomicCompareExchangeWeak
:
1549 case SpvOpAtomicIIncrement
:
1550 case SpvOpAtomicIDecrement
:
1551 case SpvOpAtomicIAdd
:
1552 case SpvOpAtomicISub
:
1553 case SpvOpAtomicSMin
:
1554 case SpvOpAtomicUMin
:
1555 case SpvOpAtomicSMax
:
1556 case SpvOpAtomicUMax
:
1557 case SpvOpAtomicAnd
:
1559 case SpvOpAtomicXor
:
1560 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
1563 case SpvOpImageQuerySize
:
1565 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1567 image
.sample
= NULL
;
1570 case SpvOpImageRead
:
1572 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1573 image
.coord
= get_image_coord(b
, w
[4]);
1575 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
1576 assert(w
[5] == SpvImageOperandsSampleMask
);
1577 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
1579 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1583 case SpvOpImageWrite
:
1585 vtn_value(b
, w
[1], vtn_value_type_access_chain
)->access_chain
;
1586 image
.coord
= get_image_coord(b
, w
[2]);
1590 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
1591 assert(w
[4] == SpvImageOperandsSampleMask
);
1592 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
1594 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1599 unreachable("Invalid image opcode");
1602 nir_intrinsic_op op
;
1604 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_##N; break;
1605 OP(ImageQuerySize
, size
)
1607 OP(ImageWrite
, store
)
1608 OP(AtomicExchange
, atomic_exchange
)
1609 OP(AtomicCompareExchange
, atomic_comp_swap
)
1610 OP(AtomicIIncrement
, atomic_add
)
1611 OP(AtomicIDecrement
, atomic_add
)
1612 OP(AtomicIAdd
, atomic_add
)
1613 OP(AtomicISub
, atomic_add
)
1614 OP(AtomicSMin
, atomic_min
)
1615 OP(AtomicUMin
, atomic_min
)
1616 OP(AtomicSMax
, atomic_max
)
1617 OP(AtomicUMax
, atomic_max
)
1618 OP(AtomicAnd
, atomic_and
)
1619 OP(AtomicOr
, atomic_or
)
1620 OP(AtomicXor
, atomic_xor
)
1623 unreachable("Invalid image opcode");
1626 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
1628 nir_deref_var
*image_deref
= vtn_access_chain_to_deref(b
, image
.image
);
1629 intrin
->variables
[0] =
1630 nir_deref_as_var(nir_copy_deref(&intrin
->instr
, &image_deref
->deref
));
1632 /* ImageQuerySize doesn't take any extra parameters */
1633 if (opcode
!= SpvOpImageQuerySize
) {
1634 /* The image coordinate is always 4 components but we may not have that
1635 * many. Swizzle to compensate.
1638 for (unsigned i
= 0; i
< 4; i
++)
1639 swiz
[i
] = i
< image
.coord
->num_components
? i
: 0;
1640 intrin
->src
[0] = nir_src_for_ssa(nir_swizzle(&b
->nb
, image
.coord
,
1642 intrin
->src
[1] = nir_src_for_ssa(image
.sample
);
1646 case SpvOpImageQuerySize
:
1647 case SpvOpImageRead
:
1649 case SpvOpImageWrite
:
1650 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[3])->def
);
1652 case SpvOpAtomicIIncrement
:
1653 intrin
->src
[2] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
1655 case SpvOpAtomicIDecrement
:
1656 intrin
->src
[2] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
1659 case SpvOpAtomicExchange
:
1660 case SpvOpAtomicIAdd
:
1661 case SpvOpAtomicSMin
:
1662 case SpvOpAtomicUMin
:
1663 case SpvOpAtomicSMax
:
1664 case SpvOpAtomicUMax
:
1665 case SpvOpAtomicAnd
:
1667 case SpvOpAtomicXor
:
1668 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1671 case SpvOpAtomicCompareExchange
:
1672 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
1673 intrin
->src
[3] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1676 case SpvOpAtomicISub
:
1677 intrin
->src
[2] = nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
1681 unreachable("Invalid image opcode");
1684 if (opcode
!= SpvOpImageWrite
) {
1685 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1686 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1687 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 4, 32, NULL
);
1689 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
1691 /* The image intrinsics always return 4 channels but we may not want
1692 * that many. Emit a mov to trim it down.
1694 unsigned swiz
[4] = {0, 1, 2, 3};
1695 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
1696 val
->ssa
->def
= nir_swizzle(&b
->nb
, &intrin
->dest
.ssa
, swiz
,
1697 glsl_get_vector_elements(type
->type
), false);
1699 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
1703 static nir_intrinsic_op
1704 get_ssbo_nir_atomic_op(SpvOp opcode
)
1707 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
1708 OP(AtomicExchange
, atomic_exchange
)
1709 OP(AtomicCompareExchange
, atomic_comp_swap
)
1710 OP(AtomicIIncrement
, atomic_add
)
1711 OP(AtomicIDecrement
, atomic_add
)
1712 OP(AtomicIAdd
, atomic_add
)
1713 OP(AtomicISub
, atomic_add
)
1714 OP(AtomicSMin
, atomic_imin
)
1715 OP(AtomicUMin
, atomic_umin
)
1716 OP(AtomicSMax
, atomic_imax
)
1717 OP(AtomicUMax
, atomic_umax
)
1718 OP(AtomicAnd
, atomic_and
)
1719 OP(AtomicOr
, atomic_or
)
1720 OP(AtomicXor
, atomic_xor
)
1723 unreachable("Invalid SSBO atomic");
1727 static nir_intrinsic_op
1728 get_shared_nir_atomic_op(SpvOp opcode
)
1731 #define OP(S, N) case SpvOp##S: return nir_intrinsic_var_##N;
1732 OP(AtomicExchange
, atomic_exchange
)
1733 OP(AtomicCompareExchange
, atomic_comp_swap
)
1734 OP(AtomicIIncrement
, atomic_add
)
1735 OP(AtomicIDecrement
, atomic_add
)
1736 OP(AtomicIAdd
, atomic_add
)
1737 OP(AtomicISub
, atomic_add
)
1738 OP(AtomicSMin
, atomic_imin
)
1739 OP(AtomicUMin
, atomic_umin
)
1740 OP(AtomicSMax
, atomic_imax
)
1741 OP(AtomicUMax
, atomic_umax
)
1742 OP(AtomicAnd
, atomic_and
)
1743 OP(AtomicOr
, atomic_or
)
1744 OP(AtomicXor
, atomic_xor
)
1747 unreachable("Invalid shared atomic");
1752 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
1753 const uint32_t *w
, nir_src
*src
)
1756 case SpvOpAtomicIIncrement
:
1757 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
1760 case SpvOpAtomicIDecrement
:
1761 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
1764 case SpvOpAtomicISub
:
1766 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
1769 case SpvOpAtomicCompareExchange
:
1770 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
1771 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
1775 case SpvOpAtomicExchange
:
1776 case SpvOpAtomicIAdd
:
1777 case SpvOpAtomicSMin
:
1778 case SpvOpAtomicUMin
:
1779 case SpvOpAtomicSMax
:
1780 case SpvOpAtomicUMax
:
1781 case SpvOpAtomicAnd
:
1783 case SpvOpAtomicXor
:
1784 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1788 unreachable("Invalid SPIR-V atomic");
1793 vtn_handle_ssbo_or_shared_atomic(struct vtn_builder
*b
, SpvOp opcode
,
1794 const uint32_t *w
, unsigned count
)
1796 struct vtn_access_chain
*chain
=
1797 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1798 nir_intrinsic_instr
*atomic
;
1801 SpvScope scope = w[4];
1802 SpvMemorySemanticsMask semantics = w[5];
1805 if (chain
->var
->mode
== vtn_variable_mode_workgroup
) {
1806 nir_deref
*deref
= &vtn_access_chain_to_deref(b
, chain
)->deref
;
1807 nir_intrinsic_op op
= get_shared_nir_atomic_op(opcode
);
1808 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
1809 atomic
->variables
[0] = nir_deref_as_var(nir_copy_deref(atomic
, deref
));
1810 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[0]);
1812 assert(chain
->var
->mode
== vtn_variable_mode_ssbo
);
1813 struct vtn_type
*type
;
1814 nir_ssa_def
*offset
, *index
;
1815 offset
= vtn_access_chain_to_offset(b
, chain
, &index
, &type
, NULL
, false);
1817 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(opcode
);
1819 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
1820 atomic
->src
[0] = nir_src_for_ssa(index
);
1821 atomic
->src
[1] = nir_src_for_ssa(offset
);
1822 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[2]);
1825 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
, 1, 32, NULL
);
1827 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1828 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1829 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
1830 val
->ssa
->def
= &atomic
->dest
.ssa
;
1831 val
->ssa
->type
= type
->type
;
1833 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
1836 static nir_alu_instr
*
1837 create_vec(nir_shader
*shader
, unsigned num_components
, unsigned bit_size
)
1840 switch (num_components
) {
1841 case 1: op
= nir_op_fmov
; break;
1842 case 2: op
= nir_op_vec2
; break;
1843 case 3: op
= nir_op_vec3
; break;
1844 case 4: op
= nir_op_vec4
; break;
1845 default: unreachable("bad vector size");
1848 nir_alu_instr
*vec
= nir_alu_instr_create(shader
, op
);
1849 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
1851 vec
->dest
.write_mask
= (1 << num_components
) - 1;
1856 struct vtn_ssa_value
*
1857 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
1859 if (src
->transposed
)
1860 return src
->transposed
;
1862 struct vtn_ssa_value
*dest
=
1863 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
1865 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
1866 nir_alu_instr
*vec
= create_vec(b
->shader
,
1867 glsl_get_matrix_columns(src
->type
),
1868 glsl_get_bit_size(src
->type
));
1869 if (glsl_type_is_vector_or_scalar(src
->type
)) {
1870 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
1871 vec
->src
[0].swizzle
[0] = i
;
1873 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
1874 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
1875 vec
->src
[j
].swizzle
[0] = i
;
1878 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1879 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
1882 dest
->transposed
= src
;
1888 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
1890 unsigned swiz
[4] = { index
};
1891 return nir_swizzle(&b
->nb
, src
, swiz
, 1, true);
1895 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
1898 nir_alu_instr
*vec
= create_vec(b
->shader
, src
->num_components
,
1901 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
1903 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
1905 vec
->src
[i
].src
= nir_src_for_ssa(src
);
1906 vec
->src
[i
].swizzle
[0] = i
;
1910 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1912 return &vec
->dest
.dest
.ssa
;
1916 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
1919 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
1920 for (unsigned i
= 1; i
< src
->num_components
; i
++)
1921 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
1922 vtn_vector_extract(b
, src
, i
), dest
);
1928 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
1929 nir_ssa_def
*insert
, nir_ssa_def
*index
)
1931 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
1932 for (unsigned i
= 1; i
< src
->num_components
; i
++)
1933 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
1934 vtn_vector_insert(b
, src
, insert
, i
), dest
);
1939 static nir_ssa_def
*
1940 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
1941 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
1942 const uint32_t *indices
)
1944 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
, src0
->bit_size
);
1946 for (unsigned i
= 0; i
< num_components
; i
++) {
1947 uint32_t index
= indices
[i
];
1948 if (index
== 0xffffffff) {
1950 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
1951 } else if (index
< src0
->num_components
) {
1952 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
1953 vec
->src
[i
].swizzle
[0] = index
;
1955 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
1956 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
1960 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1962 return &vec
->dest
.dest
.ssa
;
1966 * Concatentates a number of vectors/scalars together to produce a vector
1968 static nir_ssa_def
*
1969 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
1970 unsigned num_srcs
, nir_ssa_def
**srcs
)
1972 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
,
1975 unsigned dest_idx
= 0;
1976 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1977 nir_ssa_def
*src
= srcs
[i
];
1978 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
1979 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
1980 vec
->src
[dest_idx
].swizzle
[0] = j
;
1985 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1987 return &vec
->dest
.dest
.ssa
;
1990 static struct vtn_ssa_value
*
1991 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
1993 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
1994 dest
->type
= src
->type
;
1996 if (glsl_type_is_vector_or_scalar(src
->type
)) {
1997 dest
->def
= src
->def
;
1999 unsigned elems
= glsl_get_length(src
->type
);
2001 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
2002 for (unsigned i
= 0; i
< elems
; i
++)
2003 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
2009 static struct vtn_ssa_value
*
2010 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2011 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
2012 unsigned num_indices
)
2014 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
2016 struct vtn_ssa_value
*cur
= dest
;
2018 for (i
= 0; i
< num_indices
- 1; i
++) {
2019 cur
= cur
->elems
[indices
[i
]];
2022 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2023 /* According to the SPIR-V spec, OpCompositeInsert may work down to
2024 * the component granularity. In that case, the last index will be
2025 * the index to insert the scalar into the vector.
2028 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
2030 cur
->elems
[indices
[i
]] = insert
;
2036 static struct vtn_ssa_value
*
2037 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2038 const uint32_t *indices
, unsigned num_indices
)
2040 struct vtn_ssa_value
*cur
= src
;
2041 for (unsigned i
= 0; i
< num_indices
; i
++) {
2042 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2043 assert(i
== num_indices
- 1);
2044 /* According to the SPIR-V spec, OpCompositeExtract may work down to
2045 * the component granularity. The last index will be the index of the
2046 * vector to extract.
2049 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
2050 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
2051 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
2054 cur
= cur
->elems
[indices
[i
]];
2062 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
2063 const uint32_t *w
, unsigned count
)
2065 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2066 const struct glsl_type
*type
=
2067 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
2068 val
->ssa
= vtn_create_ssa_value(b
, type
);
2071 case SpvOpVectorExtractDynamic
:
2072 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2073 vtn_ssa_value(b
, w
[4])->def
);
2076 case SpvOpVectorInsertDynamic
:
2077 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2078 vtn_ssa_value(b
, w
[4])->def
,
2079 vtn_ssa_value(b
, w
[5])->def
);
2082 case SpvOpVectorShuffle
:
2083 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
2084 vtn_ssa_value(b
, w
[3])->def
,
2085 vtn_ssa_value(b
, w
[4])->def
,
2089 case SpvOpCompositeConstruct
: {
2090 unsigned elems
= count
- 3;
2091 if (glsl_type_is_vector_or_scalar(type
)) {
2092 nir_ssa_def
*srcs
[4];
2093 for (unsigned i
= 0; i
< elems
; i
++)
2094 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
2096 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
2099 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2100 for (unsigned i
= 0; i
< elems
; i
++)
2101 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
2105 case SpvOpCompositeExtract
:
2106 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
2110 case SpvOpCompositeInsert
:
2111 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
2112 vtn_ssa_value(b
, w
[3]),
2116 case SpvOpCopyObject
:
2117 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
2121 unreachable("unknown composite operation");
2126 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
2127 const uint32_t *w
, unsigned count
)
2129 nir_intrinsic_op intrinsic_op
;
2131 case SpvOpEmitVertex
:
2132 case SpvOpEmitStreamVertex
:
2133 intrinsic_op
= nir_intrinsic_emit_vertex
;
2135 case SpvOpEndPrimitive
:
2136 case SpvOpEndStreamPrimitive
:
2137 intrinsic_op
= nir_intrinsic_end_primitive
;
2139 case SpvOpMemoryBarrier
:
2140 intrinsic_op
= nir_intrinsic_memory_barrier
;
2142 case SpvOpControlBarrier
:
2143 intrinsic_op
= nir_intrinsic_barrier
;
2146 unreachable("unknown barrier instruction");
2149 nir_intrinsic_instr
*intrin
=
2150 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
2152 if (opcode
== SpvOpEmitStreamVertex
|| opcode
== SpvOpEndStreamPrimitive
)
2153 nir_intrinsic_set_stream_id(intrin
, w
[1]);
2155 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2159 gl_primitive_from_spv_execution_mode(SpvExecutionMode mode
)
2162 case SpvExecutionModeInputPoints
:
2163 case SpvExecutionModeOutputPoints
:
2164 return 0; /* GL_POINTS */
2165 case SpvExecutionModeInputLines
:
2166 return 1; /* GL_LINES */
2167 case SpvExecutionModeInputLinesAdjacency
:
2168 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
2169 case SpvExecutionModeTriangles
:
2170 return 4; /* GL_TRIANGLES */
2171 case SpvExecutionModeInputTrianglesAdjacency
:
2172 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
2173 case SpvExecutionModeQuads
:
2174 return 7; /* GL_QUADS */
2175 case SpvExecutionModeIsolines
:
2176 return 0x8E7A; /* GL_ISOLINES */
2177 case SpvExecutionModeOutputLineStrip
:
2178 return 3; /* GL_LINE_STRIP */
2179 case SpvExecutionModeOutputTriangleStrip
:
2180 return 5; /* GL_TRIANGLE_STRIP */
2182 assert(!"Invalid primitive type");
2188 vertices_in_from_spv_execution_mode(SpvExecutionMode mode
)
2191 case SpvExecutionModeInputPoints
:
2193 case SpvExecutionModeInputLines
:
2195 case SpvExecutionModeInputLinesAdjacency
:
2197 case SpvExecutionModeTriangles
:
2199 case SpvExecutionModeInputTrianglesAdjacency
:
2202 assert(!"Invalid GS input mode");
2207 static gl_shader_stage
2208 stage_for_execution_model(SpvExecutionModel model
)
2211 case SpvExecutionModelVertex
:
2212 return MESA_SHADER_VERTEX
;
2213 case SpvExecutionModelTessellationControl
:
2214 return MESA_SHADER_TESS_CTRL
;
2215 case SpvExecutionModelTessellationEvaluation
:
2216 return MESA_SHADER_TESS_EVAL
;
2217 case SpvExecutionModelGeometry
:
2218 return MESA_SHADER_GEOMETRY
;
2219 case SpvExecutionModelFragment
:
2220 return MESA_SHADER_FRAGMENT
;
2221 case SpvExecutionModelGLCompute
:
2222 return MESA_SHADER_COMPUTE
;
2224 unreachable("Unsupported execution model");
2229 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2230 const uint32_t *w
, unsigned count
)
2234 case SpvOpSourceExtension
:
2235 case SpvOpSourceContinued
:
2236 case SpvOpExtension
:
2237 /* Unhandled, but these are for debug so that's ok. */
2240 case SpvOpCapability
: {
2241 SpvCapability cap
= w
[1];
2243 case SpvCapabilityMatrix
:
2244 case SpvCapabilityShader
:
2245 case SpvCapabilityGeometry
:
2246 case SpvCapabilityTessellationPointSize
:
2247 case SpvCapabilityGeometryPointSize
:
2248 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
2249 case SpvCapabilitySampledImageArrayDynamicIndexing
:
2250 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
2251 case SpvCapabilityStorageImageArrayDynamicIndexing
:
2252 case SpvCapabilityImageRect
:
2253 case SpvCapabilitySampledRect
:
2254 case SpvCapabilitySampled1D
:
2255 case SpvCapabilityImage1D
:
2256 case SpvCapabilitySampledCubeArray
:
2257 case SpvCapabilitySampledBuffer
:
2258 case SpvCapabilityImageBuffer
:
2259 case SpvCapabilityImageQuery
:
2261 case SpvCapabilityClipDistance
:
2262 case SpvCapabilityCullDistance
:
2263 case SpvCapabilityGeometryStreams
:
2264 fprintf(stderr
, "WARNING: Unsupported SPIR-V Capability\n");
2267 assert(!"Unsupported capability");
2272 case SpvOpExtInstImport
:
2273 vtn_handle_extension(b
, opcode
, w
, count
);
2276 case SpvOpMemoryModel
:
2277 assert(w
[1] == SpvAddressingModelLogical
);
2278 assert(w
[2] == SpvMemoryModelGLSL450
);
2281 case SpvOpEntryPoint
: {
2282 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
2283 /* Let this be a name label regardless */
2284 unsigned name_words
;
2285 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
2287 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
2288 stage_for_execution_model(w
[1]) != b
->entry_point_stage
)
2291 assert(b
->entry_point
== NULL
);
2292 b
->entry_point
= entry_point
;
2297 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
2298 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2302 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2305 case SpvOpMemberName
:
2309 case SpvOpExecutionMode
:
2310 case SpvOpDecorationGroup
:
2312 case SpvOpMemberDecorate
:
2313 case SpvOpGroupDecorate
:
2314 case SpvOpGroupMemberDecorate
:
2315 vtn_handle_decoration(b
, opcode
, w
, count
);
2319 return false; /* End of preamble */
2326 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
2327 const struct vtn_decoration
*mode
, void *data
)
2329 assert(b
->entry_point
== entry_point
);
2331 switch(mode
->exec_mode
) {
2332 case SpvExecutionModeOriginUpperLeft
:
2333 case SpvExecutionModeOriginLowerLeft
:
2334 b
->origin_upper_left
=
2335 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
2338 case SpvExecutionModeEarlyFragmentTests
:
2339 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2340 b
->shader
->info
.fs
.early_fragment_tests
= true;
2343 case SpvExecutionModeInvocations
:
2344 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2345 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->literals
[0]);
2348 case SpvExecutionModeDepthReplacing
:
2349 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2350 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
2352 case SpvExecutionModeDepthGreater
:
2353 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2354 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
2356 case SpvExecutionModeDepthLess
:
2357 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2358 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
2360 case SpvExecutionModeDepthUnchanged
:
2361 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2362 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
2365 case SpvExecutionModeLocalSize
:
2366 assert(b
->shader
->stage
== MESA_SHADER_COMPUTE
);
2367 b
->shader
->info
.cs
.local_size
[0] = mode
->literals
[0];
2368 b
->shader
->info
.cs
.local_size
[1] = mode
->literals
[1];
2369 b
->shader
->info
.cs
.local_size
[2] = mode
->literals
[2];
2371 case SpvExecutionModeLocalSizeHint
:
2372 break; /* Nothing do do with this */
2374 case SpvExecutionModeOutputVertices
:
2375 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2376 b
->shader
->info
.gs
.vertices_out
= mode
->literals
[0];
2379 case SpvExecutionModeInputPoints
:
2380 case SpvExecutionModeInputLines
:
2381 case SpvExecutionModeInputLinesAdjacency
:
2382 case SpvExecutionModeTriangles
:
2383 case SpvExecutionModeInputTrianglesAdjacency
:
2384 case SpvExecutionModeQuads
:
2385 case SpvExecutionModeIsolines
:
2386 if (b
->shader
->stage
== MESA_SHADER_GEOMETRY
) {
2387 b
->shader
->info
.gs
.vertices_in
=
2388 vertices_in_from_spv_execution_mode(mode
->exec_mode
);
2390 assert(!"Tesselation shaders not yet supported");
2394 case SpvExecutionModeOutputPoints
:
2395 case SpvExecutionModeOutputLineStrip
:
2396 case SpvExecutionModeOutputTriangleStrip
:
2397 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2398 b
->shader
->info
.gs
.output_primitive
=
2399 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
2402 case SpvExecutionModeSpacingEqual
:
2403 case SpvExecutionModeSpacingFractionalEven
:
2404 case SpvExecutionModeSpacingFractionalOdd
:
2405 case SpvExecutionModeVertexOrderCw
:
2406 case SpvExecutionModeVertexOrderCcw
:
2407 case SpvExecutionModePointMode
:
2408 assert(!"TODO: Add tessellation metadata");
2411 case SpvExecutionModePixelCenterInteger
:
2412 b
->pixel_center_integer
= true;
2415 case SpvExecutionModeXfb
:
2416 assert(!"Unhandled execution mode");
2419 case SpvExecutionModeVecTypeHint
:
2420 case SpvExecutionModeContractionOff
:
2426 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2427 const uint32_t *w
, unsigned count
)
2431 case SpvOpSourceContinued
:
2432 case SpvOpSourceExtension
:
2433 case SpvOpExtension
:
2434 case SpvOpCapability
:
2435 case SpvOpExtInstImport
:
2436 case SpvOpMemoryModel
:
2437 case SpvOpEntryPoint
:
2438 case SpvOpExecutionMode
:
2441 case SpvOpMemberName
:
2442 case SpvOpDecorationGroup
:
2444 case SpvOpMemberDecorate
:
2445 case SpvOpGroupDecorate
:
2446 case SpvOpGroupMemberDecorate
:
2447 assert(!"Invalid opcode types and variables section");
2453 case SpvOpTypeFloat
:
2454 case SpvOpTypeVector
:
2455 case SpvOpTypeMatrix
:
2456 case SpvOpTypeImage
:
2457 case SpvOpTypeSampler
:
2458 case SpvOpTypeSampledImage
:
2459 case SpvOpTypeArray
:
2460 case SpvOpTypeRuntimeArray
:
2461 case SpvOpTypeStruct
:
2462 case SpvOpTypeOpaque
:
2463 case SpvOpTypePointer
:
2464 case SpvOpTypeFunction
:
2465 case SpvOpTypeEvent
:
2466 case SpvOpTypeDeviceEvent
:
2467 case SpvOpTypeReserveId
:
2468 case SpvOpTypeQueue
:
2470 vtn_handle_type(b
, opcode
, w
, count
);
2473 case SpvOpConstantTrue
:
2474 case SpvOpConstantFalse
:
2476 case SpvOpConstantComposite
:
2477 case SpvOpConstantSampler
:
2478 case SpvOpConstantNull
:
2479 case SpvOpSpecConstantTrue
:
2480 case SpvOpSpecConstantFalse
:
2481 case SpvOpSpecConstant
:
2482 case SpvOpSpecConstantComposite
:
2483 case SpvOpSpecConstantOp
:
2484 vtn_handle_constant(b
, opcode
, w
, count
);
2488 vtn_handle_variables(b
, opcode
, w
, count
);
2492 return false; /* End of preamble */
2499 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2500 const uint32_t *w
, unsigned count
)
2506 case SpvOpLoopMerge
:
2507 case SpvOpSelectionMerge
:
2508 /* This is handled by cfg pre-pass and walk_blocks */
2512 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
2513 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2518 vtn_handle_extension(b
, opcode
, w
, count
);
2524 case SpvOpCopyMemory
:
2525 case SpvOpCopyMemorySized
:
2526 case SpvOpAccessChain
:
2527 case SpvOpInBoundsAccessChain
:
2528 case SpvOpArrayLength
:
2529 vtn_handle_variables(b
, opcode
, w
, count
);
2532 case SpvOpFunctionCall
:
2533 vtn_handle_function_call(b
, opcode
, w
, count
);
2536 case SpvOpSampledImage
:
2538 case SpvOpImageSampleImplicitLod
:
2539 case SpvOpImageSampleExplicitLod
:
2540 case SpvOpImageSampleDrefImplicitLod
:
2541 case SpvOpImageSampleDrefExplicitLod
:
2542 case SpvOpImageSampleProjImplicitLod
:
2543 case SpvOpImageSampleProjExplicitLod
:
2544 case SpvOpImageSampleProjDrefImplicitLod
:
2545 case SpvOpImageSampleProjDrefExplicitLod
:
2546 case SpvOpImageFetch
:
2547 case SpvOpImageGather
:
2548 case SpvOpImageDrefGather
:
2549 case SpvOpImageQuerySizeLod
:
2550 case SpvOpImageQueryLod
:
2551 case SpvOpImageQueryLevels
:
2552 case SpvOpImageQuerySamples
:
2553 vtn_handle_texture(b
, opcode
, w
, count
);
2556 case SpvOpImageRead
:
2557 case SpvOpImageWrite
:
2558 case SpvOpImageTexelPointer
:
2559 vtn_handle_image(b
, opcode
, w
, count
);
2562 case SpvOpImageQuerySize
: {
2563 struct vtn_access_chain
*image
=
2564 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
2565 if (glsl_type_is_image(image
->var
->var
->interface_type
)) {
2566 vtn_handle_image(b
, opcode
, w
, count
);
2568 vtn_handle_texture(b
, opcode
, w
, count
);
2573 case SpvOpAtomicExchange
:
2574 case SpvOpAtomicCompareExchange
:
2575 case SpvOpAtomicCompareExchangeWeak
:
2576 case SpvOpAtomicIIncrement
:
2577 case SpvOpAtomicIDecrement
:
2578 case SpvOpAtomicIAdd
:
2579 case SpvOpAtomicISub
:
2580 case SpvOpAtomicSMin
:
2581 case SpvOpAtomicUMin
:
2582 case SpvOpAtomicSMax
:
2583 case SpvOpAtomicUMax
:
2584 case SpvOpAtomicAnd
:
2586 case SpvOpAtomicXor
: {
2587 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
2588 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
2589 vtn_handle_image(b
, opcode
, w
, count
);
2591 assert(pointer
->value_type
== vtn_value_type_access_chain
);
2592 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
2602 case SpvOpConvertFToU
:
2603 case SpvOpConvertFToS
:
2604 case SpvOpConvertSToF
:
2605 case SpvOpConvertUToF
:
2609 case SpvOpQuantizeToF16
:
2610 case SpvOpConvertPtrToU
:
2611 case SpvOpConvertUToPtr
:
2612 case SpvOpPtrCastToGeneric
:
2613 case SpvOpGenericCastToPtr
:
2619 case SpvOpSignBitSet
:
2620 case SpvOpLessOrGreater
:
2622 case SpvOpUnordered
:
2637 case SpvOpVectorTimesScalar
:
2639 case SpvOpIAddCarry
:
2640 case SpvOpISubBorrow
:
2641 case SpvOpUMulExtended
:
2642 case SpvOpSMulExtended
:
2643 case SpvOpShiftRightLogical
:
2644 case SpvOpShiftRightArithmetic
:
2645 case SpvOpShiftLeftLogical
:
2646 case SpvOpLogicalEqual
:
2647 case SpvOpLogicalNotEqual
:
2648 case SpvOpLogicalOr
:
2649 case SpvOpLogicalAnd
:
2650 case SpvOpLogicalNot
:
2651 case SpvOpBitwiseOr
:
2652 case SpvOpBitwiseXor
:
2653 case SpvOpBitwiseAnd
:
2656 case SpvOpFOrdEqual
:
2657 case SpvOpFUnordEqual
:
2658 case SpvOpINotEqual
:
2659 case SpvOpFOrdNotEqual
:
2660 case SpvOpFUnordNotEqual
:
2661 case SpvOpULessThan
:
2662 case SpvOpSLessThan
:
2663 case SpvOpFOrdLessThan
:
2664 case SpvOpFUnordLessThan
:
2665 case SpvOpUGreaterThan
:
2666 case SpvOpSGreaterThan
:
2667 case SpvOpFOrdGreaterThan
:
2668 case SpvOpFUnordGreaterThan
:
2669 case SpvOpULessThanEqual
:
2670 case SpvOpSLessThanEqual
:
2671 case SpvOpFOrdLessThanEqual
:
2672 case SpvOpFUnordLessThanEqual
:
2673 case SpvOpUGreaterThanEqual
:
2674 case SpvOpSGreaterThanEqual
:
2675 case SpvOpFOrdGreaterThanEqual
:
2676 case SpvOpFUnordGreaterThanEqual
:
2682 case SpvOpFwidthFine
:
2683 case SpvOpDPdxCoarse
:
2684 case SpvOpDPdyCoarse
:
2685 case SpvOpFwidthCoarse
:
2686 case SpvOpBitFieldInsert
:
2687 case SpvOpBitFieldSExtract
:
2688 case SpvOpBitFieldUExtract
:
2689 case SpvOpBitReverse
:
2691 case SpvOpTranspose
:
2692 case SpvOpOuterProduct
:
2693 case SpvOpMatrixTimesScalar
:
2694 case SpvOpVectorTimesMatrix
:
2695 case SpvOpMatrixTimesVector
:
2696 case SpvOpMatrixTimesMatrix
:
2697 vtn_handle_alu(b
, opcode
, w
, count
);
2700 case SpvOpVectorExtractDynamic
:
2701 case SpvOpVectorInsertDynamic
:
2702 case SpvOpVectorShuffle
:
2703 case SpvOpCompositeConstruct
:
2704 case SpvOpCompositeExtract
:
2705 case SpvOpCompositeInsert
:
2706 case SpvOpCopyObject
:
2707 vtn_handle_composite(b
, opcode
, w
, count
);
2710 case SpvOpEmitVertex
:
2711 case SpvOpEndPrimitive
:
2712 case SpvOpEmitStreamVertex
:
2713 case SpvOpEndStreamPrimitive
:
2714 case SpvOpControlBarrier
:
2715 case SpvOpMemoryBarrier
:
2716 vtn_handle_barrier(b
, opcode
, w
, count
);
2720 unreachable("Unhandled opcode");
2727 spirv_to_nir(const uint32_t *words
, size_t word_count
,
2728 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
2729 gl_shader_stage stage
, const char *entry_point_name
,
2730 const nir_shader_compiler_options
*options
)
2732 const uint32_t *word_end
= words
+ word_count
;
2734 /* Handle the SPIR-V header (first 4 dwords) */
2735 assert(word_count
> 5);
2737 assert(words
[0] == SpvMagicNumber
);
2738 assert(words
[1] >= 0x10000);
2739 /* words[2] == generator magic */
2740 unsigned value_id_bound
= words
[3];
2741 assert(words
[4] == 0);
2745 /* Initialize the stn_builder object */
2746 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
2747 b
->value_id_bound
= value_id_bound
;
2748 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
2749 exec_list_make_empty(&b
->functions
);
2750 b
->entry_point_stage
= stage
;
2751 b
->entry_point_name
= entry_point_name
;
2753 /* Handle all the preamble instructions */
2754 words
= vtn_foreach_instruction(b
, words
, word_end
,
2755 vtn_handle_preamble_instruction
);
2757 if (b
->entry_point
== NULL
) {
2758 assert(!"Entry point not found");
2763 b
->shader
= nir_shader_create(NULL
, stage
, options
);
2765 /* Set shader info defaults */
2766 b
->shader
->info
.gs
.invocations
= 1;
2768 /* Parse execution modes */
2769 vtn_foreach_execution_mode(b
, b
->entry_point
,
2770 vtn_handle_execution_mode
, NULL
);
2772 b
->specializations
= spec
;
2773 b
->num_specializations
= num_spec
;
2775 /* Handle all variable, type, and constant instructions */
2776 words
= vtn_foreach_instruction(b
, words
, word_end
,
2777 vtn_handle_variable_or_type_instruction
);
2779 vtn_build_cfg(b
, words
, word_end
);
2781 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
2782 b
->impl
= func
->impl
;
2783 b
->const_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
2784 _mesa_key_pointer_equal
);
2786 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
2789 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
2790 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
2791 assert(entry_point
);