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 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
936 struct vtn_value
*val
,
938 const struct vtn_decoration
*dec
,
941 assert(member
== -1);
942 if (dec
->decoration
!= SpvDecorationBuiltIn
||
943 dec
->literals
[0] != SpvBuiltInWorkgroupSize
)
946 assert(val
->const_type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
948 b
->shader
->info
.cs
.local_size
[0] = val
->constant
->value
.u
[0];
949 b
->shader
->info
.cs
.local_size
[1] = val
->constant
->value
.u
[1];
950 b
->shader
->info
.cs
.local_size
[2] = val
->constant
->value
.u
[2];
954 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
955 const uint32_t *w
, unsigned count
)
957 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
958 val
->const_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
959 val
->constant
= rzalloc(b
, nir_constant
);
961 case SpvOpConstantTrue
:
962 assert(val
->const_type
== glsl_bool_type());
963 val
->constant
->value
.u
[0] = NIR_TRUE
;
965 case SpvOpConstantFalse
:
966 assert(val
->const_type
== glsl_bool_type());
967 val
->constant
->value
.u
[0] = NIR_FALSE
;
970 case SpvOpSpecConstantTrue
:
971 case SpvOpSpecConstantFalse
: {
972 assert(val
->const_type
== glsl_bool_type());
974 get_specialization(b
, val
, (opcode
== SpvOpSpecConstantTrue
));
975 val
->constant
->value
.u
[0] = int_val
? NIR_TRUE
: NIR_FALSE
;
980 assert(glsl_type_is_scalar(val
->const_type
));
981 val
->constant
->value
.u
[0] = w
[3];
983 case SpvOpSpecConstant
:
984 assert(glsl_type_is_scalar(val
->const_type
));
985 val
->constant
->value
.u
[0] = get_specialization(b
, val
, w
[3]);
987 case SpvOpSpecConstantComposite
:
988 case SpvOpConstantComposite
: {
989 unsigned elem_count
= count
- 3;
990 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
991 for (unsigned i
= 0; i
< elem_count
; i
++)
992 elems
[i
] = vtn_value(b
, w
[i
+ 3], vtn_value_type_constant
)->constant
;
994 switch (glsl_get_base_type(val
->const_type
)) {
997 case GLSL_TYPE_FLOAT
:
999 if (glsl_type_is_matrix(val
->const_type
)) {
1000 unsigned rows
= glsl_get_vector_elements(val
->const_type
);
1001 assert(glsl_get_matrix_columns(val
->const_type
) == elem_count
);
1002 for (unsigned i
= 0; i
< elem_count
; i
++)
1003 for (unsigned j
= 0; j
< rows
; j
++)
1004 val
->constant
->value
.u
[rows
* i
+ j
] = elems
[i
]->value
.u
[j
];
1006 assert(glsl_type_is_vector(val
->const_type
));
1007 assert(glsl_get_vector_elements(val
->const_type
) == elem_count
);
1008 for (unsigned i
= 0; i
< elem_count
; i
++)
1009 val
->constant
->value
.u
[i
] = elems
[i
]->value
.u
[0];
1014 case GLSL_TYPE_STRUCT
:
1015 case GLSL_TYPE_ARRAY
:
1016 ralloc_steal(val
->constant
, elems
);
1017 val
->constant
->num_elements
= elem_count
;
1018 val
->constant
->elements
= elems
;
1022 unreachable("Unsupported type for constants");
1027 case SpvOpSpecConstantOp
: {
1028 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
1030 case SpvOpVectorShuffle
: {
1031 struct vtn_value
*v0
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1032 struct vtn_value
*v1
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1033 unsigned len0
= glsl_get_vector_elements(v0
->const_type
);
1034 unsigned len1
= glsl_get_vector_elements(v1
->const_type
);
1037 for (unsigned i
= 0; i
< len0
; i
++)
1038 u
[i
] = v0
->constant
->value
.u
[i
];
1039 for (unsigned i
= 0; i
< len1
; i
++)
1040 u
[len0
+ i
] = v1
->constant
->value
.u
[i
];
1042 for (unsigned i
= 0; i
< count
- 6; i
++) {
1043 uint32_t comp
= w
[i
+ 6];
1044 if (comp
== (uint32_t)-1) {
1045 val
->constant
->value
.u
[i
] = 0xdeadbeef;
1047 val
->constant
->value
.u
[i
] = u
[comp
];
1053 case SpvOpCompositeExtract
:
1054 case SpvOpCompositeInsert
: {
1055 struct vtn_value
*comp
;
1056 unsigned deref_start
;
1057 struct nir_constant
**c
;
1058 if (opcode
== SpvOpCompositeExtract
) {
1059 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1061 c
= &comp
->constant
;
1063 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1065 val
->constant
= nir_constant_clone(comp
->constant
,
1071 const struct glsl_type
*type
= comp
->const_type
;
1072 for (unsigned i
= deref_start
; i
< count
; i
++) {
1073 switch (glsl_get_base_type(type
)) {
1074 case GLSL_TYPE_UINT
:
1076 case GLSL_TYPE_FLOAT
:
1077 case GLSL_TYPE_BOOL
:
1078 /* If we hit this granularity, we're picking off an element */
1082 if (glsl_type_is_matrix(type
)) {
1083 elem
+= w
[i
] * glsl_get_vector_elements(type
);
1084 type
= glsl_get_column_type(type
);
1086 assert(glsl_type_is_vector(type
));
1088 type
= glsl_scalar_type(glsl_get_base_type(type
));
1092 case GLSL_TYPE_ARRAY
:
1093 c
= &(*c
)->elements
[w
[i
]];
1094 type
= glsl_get_array_element(type
);
1097 case GLSL_TYPE_STRUCT
:
1098 c
= &(*c
)->elements
[w
[i
]];
1099 type
= glsl_get_struct_field(type
, w
[i
]);
1103 unreachable("Invalid constant type");
1107 if (opcode
== SpvOpCompositeExtract
) {
1111 unsigned num_components
= glsl_get_vector_elements(type
);
1112 for (unsigned i
= 0; i
< num_components
; i
++)
1113 val
->constant
->value
.u
[i
] = (*c
)->value
.u
[elem
+ i
];
1116 struct vtn_value
*insert
=
1117 vtn_value(b
, w
[4], vtn_value_type_constant
);
1118 assert(insert
->const_type
== type
);
1120 *c
= insert
->constant
;
1122 unsigned num_components
= glsl_get_vector_elements(type
);
1123 for (unsigned i
= 0; i
< num_components
; i
++)
1124 (*c
)->value
.u
[elem
+ i
] = insert
->constant
->value
.u
[i
];
1132 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(opcode
, &swap
);
1134 unsigned num_components
= glsl_get_vector_elements(val
->const_type
);
1136 glsl_get_bit_size(val
->const_type
);
1138 nir_const_value src
[4];
1140 for (unsigned i
= 0; i
< count
- 4; i
++) {
1142 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
)->constant
;
1144 unsigned j
= swap
? 1 - i
: i
;
1145 assert(bit_size
== 32);
1146 for (unsigned k
= 0; k
< num_components
; k
++)
1147 src
[j
].u32
[k
] = c
->value
.u
[k
];
1150 nir_const_value res
= nir_eval_const_opcode(op
, num_components
,
1153 for (unsigned k
= 0; k
< num_components
; k
++)
1154 val
->constant
->value
.u
[k
] = res
.u32
[k
];
1162 case SpvOpConstantNull
:
1163 val
->constant
= vtn_null_constant(b
, val
->const_type
);
1166 case SpvOpConstantSampler
:
1167 assert(!"OpConstantSampler requires Kernel Capability");
1171 unreachable("Unhandled opcode");
1174 /* Now that we have the value, update the workgroup size if needed */
1175 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
1179 vtn_handle_function_call(struct vtn_builder
*b
, SpvOp opcode
,
1180 const uint32_t *w
, unsigned count
)
1182 struct nir_function
*callee
=
1183 vtn_value(b
, w
[3], vtn_value_type_function
)->func
->impl
->function
;
1185 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, callee
);
1186 for (unsigned i
= 0; i
< call
->num_params
; i
++) {
1187 unsigned arg_id
= w
[4 + i
];
1188 struct vtn_value
*arg
= vtn_untyped_value(b
, arg_id
);
1189 if (arg
->value_type
== vtn_value_type_access_chain
) {
1190 nir_deref_var
*d
= vtn_access_chain_to_deref(b
, arg
->access_chain
);
1191 call
->params
[i
] = nir_deref_as_var(nir_copy_deref(call
, &d
->deref
));
1193 struct vtn_ssa_value
*arg_ssa
= vtn_ssa_value(b
, arg_id
);
1195 /* Make a temporary to store the argument in */
1197 nir_local_variable_create(b
->impl
, arg_ssa
->type
, "arg_tmp");
1198 call
->params
[i
] = nir_deref_var_create(call
, tmp
);
1200 vtn_local_store(b
, arg_ssa
, call
->params
[i
]);
1204 nir_variable
*out_tmp
= NULL
;
1205 if (!glsl_type_is_void(callee
->return_type
)) {
1206 out_tmp
= nir_local_variable_create(b
->impl
, callee
->return_type
,
1208 call
->return_deref
= nir_deref_var_create(call
, out_tmp
);
1211 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
1213 if (glsl_type_is_void(callee
->return_type
)) {
1214 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1216 struct vtn_value
*retval
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1217 retval
->ssa
= vtn_local_load(b
, call
->return_deref
);
1221 struct vtn_ssa_value
*
1222 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1224 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1227 if (!glsl_type_is_vector_or_scalar(type
)) {
1228 unsigned elems
= glsl_get_length(type
);
1229 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1230 for (unsigned i
= 0; i
< elems
; i
++) {
1231 const struct glsl_type
*child_type
;
1233 switch (glsl_get_base_type(type
)) {
1235 case GLSL_TYPE_UINT
:
1236 case GLSL_TYPE_BOOL
:
1237 case GLSL_TYPE_FLOAT
:
1238 case GLSL_TYPE_DOUBLE
:
1239 child_type
= glsl_get_column_type(type
);
1241 case GLSL_TYPE_ARRAY
:
1242 child_type
= glsl_get_array_element(type
);
1244 case GLSL_TYPE_STRUCT
:
1245 child_type
= glsl_get_struct_field(type
, i
);
1248 unreachable("unkown base type");
1251 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
1259 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
1262 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
1263 src
.src_type
= type
;
1268 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
1269 const uint32_t *w
, unsigned count
)
1271 if (opcode
== SpvOpSampledImage
) {
1272 struct vtn_value
*val
=
1273 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1274 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1275 val
->sampled_image
->image
=
1276 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1277 val
->sampled_image
->sampler
=
1278 vtn_value(b
, w
[4], vtn_value_type_access_chain
)->access_chain
;
1280 } else if (opcode
== SpvOpImage
) {
1281 struct vtn_value
*val
=
1282 vtn_push_value(b
, w
[2], vtn_value_type_access_chain
);
1283 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
1284 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
1285 val
->access_chain
= src_val
->sampled_image
->image
;
1287 assert(src_val
->value_type
== vtn_value_type_access_chain
);
1288 val
->access_chain
= src_val
->access_chain
;
1293 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1294 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1296 struct vtn_sampled_image sampled
;
1297 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
1298 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
1299 sampled
= *sampled_val
->sampled_image
;
1301 assert(sampled_val
->value_type
== vtn_value_type_access_chain
);
1302 sampled
.image
= NULL
;
1303 sampled
.sampler
= sampled_val
->access_chain
;
1306 const struct glsl_type
*image_type
;
1307 if (sampled
.image
) {
1308 image_type
= sampled
.image
->var
->var
->interface_type
;
1310 image_type
= sampled
.sampler
->var
->var
->interface_type
;
1313 nir_tex_src srcs
[8]; /* 8 should be enough */
1314 nir_tex_src
*p
= srcs
;
1318 bool has_coord
= false;
1320 case SpvOpImageSampleImplicitLod
:
1321 case SpvOpImageSampleExplicitLod
:
1322 case SpvOpImageSampleDrefImplicitLod
:
1323 case SpvOpImageSampleDrefExplicitLod
:
1324 case SpvOpImageSampleProjImplicitLod
:
1325 case SpvOpImageSampleProjExplicitLod
:
1326 case SpvOpImageSampleProjDrefImplicitLod
:
1327 case SpvOpImageSampleProjDrefExplicitLod
:
1328 case SpvOpImageFetch
:
1329 case SpvOpImageGather
:
1330 case SpvOpImageDrefGather
:
1331 case SpvOpImageQueryLod
: {
1332 /* All these types have the coordinate as their first real argument */
1333 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, w
[idx
++]);
1335 p
->src
= nir_src_for_ssa(coord
->def
);
1336 p
->src_type
= nir_tex_src_coord
;
1345 /* These all have an explicit depth value as their next source */
1347 case SpvOpImageSampleDrefImplicitLod
:
1348 case SpvOpImageSampleDrefExplicitLod
:
1349 case SpvOpImageSampleProjDrefImplicitLod
:
1350 case SpvOpImageSampleProjDrefExplicitLod
:
1351 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparitor
);
1357 /* For OpImageQuerySizeLod, we always have an LOD */
1358 if (opcode
== SpvOpImageQuerySizeLod
)
1359 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1361 /* Figure out the base texture operation */
1364 case SpvOpImageSampleImplicitLod
:
1365 case SpvOpImageSampleDrefImplicitLod
:
1366 case SpvOpImageSampleProjImplicitLod
:
1367 case SpvOpImageSampleProjDrefImplicitLod
:
1368 texop
= nir_texop_tex
;
1371 case SpvOpImageSampleExplicitLod
:
1372 case SpvOpImageSampleDrefExplicitLod
:
1373 case SpvOpImageSampleProjExplicitLod
:
1374 case SpvOpImageSampleProjDrefExplicitLod
:
1375 texop
= nir_texop_txl
;
1378 case SpvOpImageFetch
:
1379 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
1380 texop
= nir_texop_txf_ms
;
1382 texop
= nir_texop_txf
;
1386 case SpvOpImageGather
:
1387 case SpvOpImageDrefGather
:
1388 texop
= nir_texop_tg4
;
1391 case SpvOpImageQuerySizeLod
:
1392 case SpvOpImageQuerySize
:
1393 texop
= nir_texop_txs
;
1396 case SpvOpImageQueryLod
:
1397 texop
= nir_texop_lod
;
1400 case SpvOpImageQueryLevels
:
1401 texop
= nir_texop_query_levels
;
1404 case SpvOpImageQuerySamples
:
1406 unreachable("Unhandled opcode");
1409 /* Now we need to handle some number of optional arguments */
1411 uint32_t operands
= w
[idx
++];
1413 if (operands
& SpvImageOperandsBiasMask
) {
1414 assert(texop
== nir_texop_tex
);
1415 texop
= nir_texop_txb
;
1416 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
1419 if (operands
& SpvImageOperandsLodMask
) {
1420 assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
1421 texop
== nir_texop_txf_ms
|| texop
== nir_texop_txs
);
1422 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1425 if (operands
& SpvImageOperandsGradMask
) {
1426 assert(texop
== nir_texop_tex
);
1427 texop
= nir_texop_txd
;
1428 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
1429 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
1432 if (operands
& SpvImageOperandsOffsetMask
||
1433 operands
& SpvImageOperandsConstOffsetMask
)
1434 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
1436 if (operands
& SpvImageOperandsConstOffsetsMask
)
1437 assert(!"Constant offsets to texture gather not yet implemented");
1439 if (operands
& SpvImageOperandsSampleMask
) {
1440 assert(texop
== nir_texop_txf_ms
);
1441 texop
= nir_texop_txf_ms
;
1442 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
1445 /* We should have now consumed exactly all of the arguments */
1446 assert(idx
== count
);
1448 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
1451 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1453 instr
->sampler_dim
= glsl_get_sampler_dim(image_type
);
1454 instr
->is_array
= glsl_sampler_type_is_array(image_type
);
1455 instr
->is_shadow
= glsl_sampler_type_is_shadow(image_type
);
1456 instr
->is_new_style_shadow
= instr
->is_shadow
;
1459 switch (instr
->sampler_dim
) {
1460 case GLSL_SAMPLER_DIM_1D
:
1461 case GLSL_SAMPLER_DIM_BUF
:
1462 instr
->coord_components
= 1;
1464 case GLSL_SAMPLER_DIM_2D
:
1465 case GLSL_SAMPLER_DIM_RECT
:
1466 case GLSL_SAMPLER_DIM_MS
:
1467 instr
->coord_components
= 2;
1469 case GLSL_SAMPLER_DIM_3D
:
1470 case GLSL_SAMPLER_DIM_CUBE
:
1471 instr
->coord_components
= 3;
1474 assert("Invalid sampler type");
1477 if (instr
->is_array
)
1478 instr
->coord_components
++;
1480 instr
->coord_components
= 0;
1483 switch (glsl_get_sampler_result_type(image_type
)) {
1484 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
1485 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
1486 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
1487 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
1489 unreachable("Invalid base type for sampler result");
1492 nir_deref_var
*sampler
= vtn_access_chain_to_deref(b
, sampled
.sampler
);
1493 if (sampled
.image
) {
1494 nir_deref_var
*image
= vtn_access_chain_to_deref(b
, sampled
.image
);
1495 instr
->texture
= nir_deref_as_var(nir_copy_deref(instr
, &image
->deref
));
1497 instr
->texture
= nir_deref_as_var(nir_copy_deref(instr
, &sampler
->deref
));
1500 switch (instr
->op
) {
1505 /* These operations require a sampler */
1506 instr
->sampler
= nir_deref_as_var(nir_copy_deref(instr
, &sampler
->deref
));
1509 case nir_texop_txf_ms
:
1513 case nir_texop_query_levels
:
1514 case nir_texop_texture_samples
:
1515 case nir_texop_samples_identical
:
1517 instr
->sampler
= NULL
;
1519 case nir_texop_txf_ms_mcs
:
1520 unreachable("unexpected nir_texop_txf_ms_mcs");
1523 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1524 nir_tex_instr_dest_size(instr
), 32, NULL
);
1526 assert(glsl_get_vector_elements(ret_type
->type
) ==
1527 nir_tex_instr_dest_size(instr
));
1529 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
1530 val
->ssa
->def
= &instr
->dest
.ssa
;
1532 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
1535 static nir_ssa_def
*
1536 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
1538 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
1540 /* The image_load_store intrinsics assume a 4-dim coordinate */
1541 unsigned dim
= glsl_get_vector_elements(coord
->type
);
1542 unsigned swizzle
[4];
1543 for (unsigned i
= 0; i
< 4; i
++)
1544 swizzle
[i
] = MIN2(i
, dim
- 1);
1546 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
1550 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
1551 const uint32_t *w
, unsigned count
)
1553 /* Just get this one out of the way */
1554 if (opcode
== SpvOpImageTexelPointer
) {
1555 struct vtn_value
*val
=
1556 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
1557 val
->image
= ralloc(b
, struct vtn_image_pointer
);
1560 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1561 val
->image
->coord
= get_image_coord(b
, w
[4]);
1562 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
1566 struct vtn_image_pointer image
;
1569 case SpvOpAtomicExchange
:
1570 case SpvOpAtomicCompareExchange
:
1571 case SpvOpAtomicCompareExchangeWeak
:
1572 case SpvOpAtomicIIncrement
:
1573 case SpvOpAtomicIDecrement
:
1574 case SpvOpAtomicIAdd
:
1575 case SpvOpAtomicISub
:
1576 case SpvOpAtomicSMin
:
1577 case SpvOpAtomicUMin
:
1578 case SpvOpAtomicSMax
:
1579 case SpvOpAtomicUMax
:
1580 case SpvOpAtomicAnd
:
1582 case SpvOpAtomicXor
:
1583 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
1586 case SpvOpImageQuerySize
:
1588 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1590 image
.sample
= NULL
;
1593 case SpvOpImageRead
:
1595 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1596 image
.coord
= get_image_coord(b
, w
[4]);
1598 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
1599 assert(w
[5] == SpvImageOperandsSampleMask
);
1600 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
1602 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1606 case SpvOpImageWrite
:
1608 vtn_value(b
, w
[1], vtn_value_type_access_chain
)->access_chain
;
1609 image
.coord
= get_image_coord(b
, w
[2]);
1613 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
1614 assert(w
[4] == SpvImageOperandsSampleMask
);
1615 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
1617 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1622 unreachable("Invalid image opcode");
1625 nir_intrinsic_op op
;
1627 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_##N; break;
1628 OP(ImageQuerySize
, size
)
1630 OP(ImageWrite
, store
)
1631 OP(AtomicExchange
, atomic_exchange
)
1632 OP(AtomicCompareExchange
, atomic_comp_swap
)
1633 OP(AtomicIIncrement
, atomic_add
)
1634 OP(AtomicIDecrement
, atomic_add
)
1635 OP(AtomicIAdd
, atomic_add
)
1636 OP(AtomicISub
, atomic_add
)
1637 OP(AtomicSMin
, atomic_min
)
1638 OP(AtomicUMin
, atomic_min
)
1639 OP(AtomicSMax
, atomic_max
)
1640 OP(AtomicUMax
, atomic_max
)
1641 OP(AtomicAnd
, atomic_and
)
1642 OP(AtomicOr
, atomic_or
)
1643 OP(AtomicXor
, atomic_xor
)
1646 unreachable("Invalid image opcode");
1649 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
1651 nir_deref_var
*image_deref
= vtn_access_chain_to_deref(b
, image
.image
);
1652 intrin
->variables
[0] =
1653 nir_deref_as_var(nir_copy_deref(&intrin
->instr
, &image_deref
->deref
));
1655 /* ImageQuerySize doesn't take any extra parameters */
1656 if (opcode
!= SpvOpImageQuerySize
) {
1657 /* The image coordinate is always 4 components but we may not have that
1658 * many. Swizzle to compensate.
1661 for (unsigned i
= 0; i
< 4; i
++)
1662 swiz
[i
] = i
< image
.coord
->num_components
? i
: 0;
1663 intrin
->src
[0] = nir_src_for_ssa(nir_swizzle(&b
->nb
, image
.coord
,
1665 intrin
->src
[1] = nir_src_for_ssa(image
.sample
);
1669 case SpvOpImageQuerySize
:
1670 case SpvOpImageRead
:
1672 case SpvOpImageWrite
:
1673 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[3])->def
);
1675 case SpvOpAtomicIIncrement
:
1676 intrin
->src
[2] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
1678 case SpvOpAtomicIDecrement
:
1679 intrin
->src
[2] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
1682 case SpvOpAtomicExchange
:
1683 case SpvOpAtomicIAdd
:
1684 case SpvOpAtomicSMin
:
1685 case SpvOpAtomicUMin
:
1686 case SpvOpAtomicSMax
:
1687 case SpvOpAtomicUMax
:
1688 case SpvOpAtomicAnd
:
1690 case SpvOpAtomicXor
:
1691 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1694 case SpvOpAtomicCompareExchange
:
1695 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
1696 intrin
->src
[3] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1699 case SpvOpAtomicISub
:
1700 intrin
->src
[2] = nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
1704 unreachable("Invalid image opcode");
1707 if (opcode
!= SpvOpImageWrite
) {
1708 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1709 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1710 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 4, 32, NULL
);
1712 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
1714 /* The image intrinsics always return 4 channels but we may not want
1715 * that many. Emit a mov to trim it down.
1717 unsigned swiz
[4] = {0, 1, 2, 3};
1718 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
1719 val
->ssa
->def
= nir_swizzle(&b
->nb
, &intrin
->dest
.ssa
, swiz
,
1720 glsl_get_vector_elements(type
->type
), false);
1722 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
1726 static nir_intrinsic_op
1727 get_ssbo_nir_atomic_op(SpvOp opcode
)
1730 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
1731 OP(AtomicExchange
, atomic_exchange
)
1732 OP(AtomicCompareExchange
, atomic_comp_swap
)
1733 OP(AtomicIIncrement
, atomic_add
)
1734 OP(AtomicIDecrement
, atomic_add
)
1735 OP(AtomicIAdd
, atomic_add
)
1736 OP(AtomicISub
, atomic_add
)
1737 OP(AtomicSMin
, atomic_imin
)
1738 OP(AtomicUMin
, atomic_umin
)
1739 OP(AtomicSMax
, atomic_imax
)
1740 OP(AtomicUMax
, atomic_umax
)
1741 OP(AtomicAnd
, atomic_and
)
1742 OP(AtomicOr
, atomic_or
)
1743 OP(AtomicXor
, atomic_xor
)
1746 unreachable("Invalid SSBO atomic");
1750 static nir_intrinsic_op
1751 get_shared_nir_atomic_op(SpvOp opcode
)
1754 #define OP(S, N) case SpvOp##S: return nir_intrinsic_var_##N;
1755 OP(AtomicExchange
, atomic_exchange
)
1756 OP(AtomicCompareExchange
, atomic_comp_swap
)
1757 OP(AtomicIIncrement
, atomic_add
)
1758 OP(AtomicIDecrement
, atomic_add
)
1759 OP(AtomicIAdd
, atomic_add
)
1760 OP(AtomicISub
, atomic_add
)
1761 OP(AtomicSMin
, atomic_imin
)
1762 OP(AtomicUMin
, atomic_umin
)
1763 OP(AtomicSMax
, atomic_imax
)
1764 OP(AtomicUMax
, atomic_umax
)
1765 OP(AtomicAnd
, atomic_and
)
1766 OP(AtomicOr
, atomic_or
)
1767 OP(AtomicXor
, atomic_xor
)
1770 unreachable("Invalid shared atomic");
1775 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
1776 const uint32_t *w
, nir_src
*src
)
1779 case SpvOpAtomicIIncrement
:
1780 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
1783 case SpvOpAtomicIDecrement
:
1784 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
1787 case SpvOpAtomicISub
:
1789 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
1792 case SpvOpAtomicCompareExchange
:
1793 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
1794 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
1798 case SpvOpAtomicExchange
:
1799 case SpvOpAtomicIAdd
:
1800 case SpvOpAtomicSMin
:
1801 case SpvOpAtomicUMin
:
1802 case SpvOpAtomicSMax
:
1803 case SpvOpAtomicUMax
:
1804 case SpvOpAtomicAnd
:
1806 case SpvOpAtomicXor
:
1807 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1811 unreachable("Invalid SPIR-V atomic");
1816 vtn_handle_ssbo_or_shared_atomic(struct vtn_builder
*b
, SpvOp opcode
,
1817 const uint32_t *w
, unsigned count
)
1819 struct vtn_access_chain
*chain
=
1820 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1821 nir_intrinsic_instr
*atomic
;
1824 SpvScope scope = w[4];
1825 SpvMemorySemanticsMask semantics = w[5];
1828 if (chain
->var
->mode
== vtn_variable_mode_workgroup
) {
1829 nir_deref
*deref
= &vtn_access_chain_to_deref(b
, chain
)->deref
;
1830 nir_intrinsic_op op
= get_shared_nir_atomic_op(opcode
);
1831 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
1832 atomic
->variables
[0] = nir_deref_as_var(nir_copy_deref(atomic
, deref
));
1833 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[0]);
1835 assert(chain
->var
->mode
== vtn_variable_mode_ssbo
);
1836 struct vtn_type
*type
;
1837 nir_ssa_def
*offset
, *index
;
1838 offset
= vtn_access_chain_to_offset(b
, chain
, &index
, &type
, NULL
, false);
1840 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(opcode
);
1842 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
1843 atomic
->src
[0] = nir_src_for_ssa(index
);
1844 atomic
->src
[1] = nir_src_for_ssa(offset
);
1845 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[2]);
1848 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
, 1, 32, NULL
);
1850 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1851 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1852 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
1853 val
->ssa
->def
= &atomic
->dest
.ssa
;
1854 val
->ssa
->type
= type
->type
;
1856 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
1859 static nir_alu_instr
*
1860 create_vec(nir_shader
*shader
, unsigned num_components
, unsigned bit_size
)
1863 switch (num_components
) {
1864 case 1: op
= nir_op_fmov
; break;
1865 case 2: op
= nir_op_vec2
; break;
1866 case 3: op
= nir_op_vec3
; break;
1867 case 4: op
= nir_op_vec4
; break;
1868 default: unreachable("bad vector size");
1871 nir_alu_instr
*vec
= nir_alu_instr_create(shader
, op
);
1872 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
1874 vec
->dest
.write_mask
= (1 << num_components
) - 1;
1879 struct vtn_ssa_value
*
1880 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
1882 if (src
->transposed
)
1883 return src
->transposed
;
1885 struct vtn_ssa_value
*dest
=
1886 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
1888 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
1889 nir_alu_instr
*vec
= create_vec(b
->shader
,
1890 glsl_get_matrix_columns(src
->type
),
1891 glsl_get_bit_size(src
->type
));
1892 if (glsl_type_is_vector_or_scalar(src
->type
)) {
1893 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
1894 vec
->src
[0].swizzle
[0] = i
;
1896 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
1897 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
1898 vec
->src
[j
].swizzle
[0] = i
;
1901 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1902 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
1905 dest
->transposed
= src
;
1911 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
1913 unsigned swiz
[4] = { index
};
1914 return nir_swizzle(&b
->nb
, src
, swiz
, 1, true);
1918 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
1921 nir_alu_instr
*vec
= create_vec(b
->shader
, src
->num_components
,
1924 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
1926 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
1928 vec
->src
[i
].src
= nir_src_for_ssa(src
);
1929 vec
->src
[i
].swizzle
[0] = i
;
1933 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1935 return &vec
->dest
.dest
.ssa
;
1939 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
1942 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
1943 for (unsigned i
= 1; i
< src
->num_components
; i
++)
1944 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
1945 vtn_vector_extract(b
, src
, i
), dest
);
1951 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
1952 nir_ssa_def
*insert
, nir_ssa_def
*index
)
1954 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
1955 for (unsigned i
= 1; i
< src
->num_components
; i
++)
1956 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
1957 vtn_vector_insert(b
, src
, insert
, i
), dest
);
1962 static nir_ssa_def
*
1963 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
1964 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
1965 const uint32_t *indices
)
1967 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
, src0
->bit_size
);
1969 for (unsigned i
= 0; i
< num_components
; i
++) {
1970 uint32_t index
= indices
[i
];
1971 if (index
== 0xffffffff) {
1973 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
1974 } else if (index
< src0
->num_components
) {
1975 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
1976 vec
->src
[i
].swizzle
[0] = index
;
1978 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
1979 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
1983 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1985 return &vec
->dest
.dest
.ssa
;
1989 * Concatentates a number of vectors/scalars together to produce a vector
1991 static nir_ssa_def
*
1992 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
1993 unsigned num_srcs
, nir_ssa_def
**srcs
)
1995 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
,
1998 unsigned dest_idx
= 0;
1999 for (unsigned i
= 0; i
< num_srcs
; i
++) {
2000 nir_ssa_def
*src
= srcs
[i
];
2001 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
2002 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
2003 vec
->src
[dest_idx
].swizzle
[0] = j
;
2008 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2010 return &vec
->dest
.dest
.ssa
;
2013 static struct vtn_ssa_value
*
2014 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
2016 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
2017 dest
->type
= src
->type
;
2019 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2020 dest
->def
= src
->def
;
2022 unsigned elems
= glsl_get_length(src
->type
);
2024 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
2025 for (unsigned i
= 0; i
< elems
; i
++)
2026 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
2032 static struct vtn_ssa_value
*
2033 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2034 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
2035 unsigned num_indices
)
2037 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
2039 struct vtn_ssa_value
*cur
= dest
;
2041 for (i
= 0; i
< num_indices
- 1; i
++) {
2042 cur
= cur
->elems
[indices
[i
]];
2045 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2046 /* According to the SPIR-V spec, OpCompositeInsert may work down to
2047 * the component granularity. In that case, the last index will be
2048 * the index to insert the scalar into the vector.
2051 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
2053 cur
->elems
[indices
[i
]] = insert
;
2059 static struct vtn_ssa_value
*
2060 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2061 const uint32_t *indices
, unsigned num_indices
)
2063 struct vtn_ssa_value
*cur
= src
;
2064 for (unsigned i
= 0; i
< num_indices
; i
++) {
2065 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2066 assert(i
== num_indices
- 1);
2067 /* According to the SPIR-V spec, OpCompositeExtract may work down to
2068 * the component granularity. The last index will be the index of the
2069 * vector to extract.
2072 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
2073 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
2074 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
2077 cur
= cur
->elems
[indices
[i
]];
2085 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
2086 const uint32_t *w
, unsigned count
)
2088 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2089 const struct glsl_type
*type
=
2090 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
2091 val
->ssa
= vtn_create_ssa_value(b
, type
);
2094 case SpvOpVectorExtractDynamic
:
2095 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2096 vtn_ssa_value(b
, w
[4])->def
);
2099 case SpvOpVectorInsertDynamic
:
2100 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2101 vtn_ssa_value(b
, w
[4])->def
,
2102 vtn_ssa_value(b
, w
[5])->def
);
2105 case SpvOpVectorShuffle
:
2106 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
2107 vtn_ssa_value(b
, w
[3])->def
,
2108 vtn_ssa_value(b
, w
[4])->def
,
2112 case SpvOpCompositeConstruct
: {
2113 unsigned elems
= count
- 3;
2114 if (glsl_type_is_vector_or_scalar(type
)) {
2115 nir_ssa_def
*srcs
[4];
2116 for (unsigned i
= 0; i
< elems
; i
++)
2117 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
2119 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
2122 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2123 for (unsigned i
= 0; i
< elems
; i
++)
2124 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
2128 case SpvOpCompositeExtract
:
2129 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
2133 case SpvOpCompositeInsert
:
2134 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
2135 vtn_ssa_value(b
, w
[3]),
2139 case SpvOpCopyObject
:
2140 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
2144 unreachable("unknown composite operation");
2149 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
2150 const uint32_t *w
, unsigned count
)
2152 nir_intrinsic_op intrinsic_op
;
2154 case SpvOpEmitVertex
:
2155 case SpvOpEmitStreamVertex
:
2156 intrinsic_op
= nir_intrinsic_emit_vertex
;
2158 case SpvOpEndPrimitive
:
2159 case SpvOpEndStreamPrimitive
:
2160 intrinsic_op
= nir_intrinsic_end_primitive
;
2162 case SpvOpMemoryBarrier
:
2163 intrinsic_op
= nir_intrinsic_memory_barrier
;
2165 case SpvOpControlBarrier
:
2166 intrinsic_op
= nir_intrinsic_barrier
;
2169 unreachable("unknown barrier instruction");
2172 nir_intrinsic_instr
*intrin
=
2173 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
2175 if (opcode
== SpvOpEmitStreamVertex
|| opcode
== SpvOpEndStreamPrimitive
)
2176 nir_intrinsic_set_stream_id(intrin
, w
[1]);
2178 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2182 gl_primitive_from_spv_execution_mode(SpvExecutionMode mode
)
2185 case SpvExecutionModeInputPoints
:
2186 case SpvExecutionModeOutputPoints
:
2187 return 0; /* GL_POINTS */
2188 case SpvExecutionModeInputLines
:
2189 return 1; /* GL_LINES */
2190 case SpvExecutionModeInputLinesAdjacency
:
2191 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
2192 case SpvExecutionModeTriangles
:
2193 return 4; /* GL_TRIANGLES */
2194 case SpvExecutionModeInputTrianglesAdjacency
:
2195 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
2196 case SpvExecutionModeQuads
:
2197 return 7; /* GL_QUADS */
2198 case SpvExecutionModeIsolines
:
2199 return 0x8E7A; /* GL_ISOLINES */
2200 case SpvExecutionModeOutputLineStrip
:
2201 return 3; /* GL_LINE_STRIP */
2202 case SpvExecutionModeOutputTriangleStrip
:
2203 return 5; /* GL_TRIANGLE_STRIP */
2205 assert(!"Invalid primitive type");
2211 vertices_in_from_spv_execution_mode(SpvExecutionMode mode
)
2214 case SpvExecutionModeInputPoints
:
2216 case SpvExecutionModeInputLines
:
2218 case SpvExecutionModeInputLinesAdjacency
:
2220 case SpvExecutionModeTriangles
:
2222 case SpvExecutionModeInputTrianglesAdjacency
:
2225 assert(!"Invalid GS input mode");
2230 static gl_shader_stage
2231 stage_for_execution_model(SpvExecutionModel model
)
2234 case SpvExecutionModelVertex
:
2235 return MESA_SHADER_VERTEX
;
2236 case SpvExecutionModelTessellationControl
:
2237 return MESA_SHADER_TESS_CTRL
;
2238 case SpvExecutionModelTessellationEvaluation
:
2239 return MESA_SHADER_TESS_EVAL
;
2240 case SpvExecutionModelGeometry
:
2241 return MESA_SHADER_GEOMETRY
;
2242 case SpvExecutionModelFragment
:
2243 return MESA_SHADER_FRAGMENT
;
2244 case SpvExecutionModelGLCompute
:
2245 return MESA_SHADER_COMPUTE
;
2247 unreachable("Unsupported execution model");
2252 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2253 const uint32_t *w
, unsigned count
)
2257 case SpvOpSourceExtension
:
2258 case SpvOpSourceContinued
:
2259 case SpvOpExtension
:
2260 /* Unhandled, but these are for debug so that's ok. */
2263 case SpvOpCapability
: {
2264 SpvCapability cap
= w
[1];
2266 case SpvCapabilityMatrix
:
2267 case SpvCapabilityShader
:
2268 case SpvCapabilityGeometry
:
2269 case SpvCapabilityTessellationPointSize
:
2270 case SpvCapabilityGeometryPointSize
:
2271 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
2272 case SpvCapabilitySampledImageArrayDynamicIndexing
:
2273 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
2274 case SpvCapabilityStorageImageArrayDynamicIndexing
:
2275 case SpvCapabilityImageRect
:
2276 case SpvCapabilitySampledRect
:
2277 case SpvCapabilitySampled1D
:
2278 case SpvCapabilityImage1D
:
2279 case SpvCapabilitySampledCubeArray
:
2280 case SpvCapabilitySampledBuffer
:
2281 case SpvCapabilityImageBuffer
:
2282 case SpvCapabilityImageQuery
:
2284 case SpvCapabilityClipDistance
:
2285 case SpvCapabilityCullDistance
:
2286 case SpvCapabilityGeometryStreams
:
2287 fprintf(stderr
, "WARNING: Unsupported SPIR-V Capability\n");
2290 assert(!"Unsupported capability");
2295 case SpvOpExtInstImport
:
2296 vtn_handle_extension(b
, opcode
, w
, count
);
2299 case SpvOpMemoryModel
:
2300 assert(w
[1] == SpvAddressingModelLogical
);
2301 assert(w
[2] == SpvMemoryModelGLSL450
);
2304 case SpvOpEntryPoint
: {
2305 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
2306 /* Let this be a name label regardless */
2307 unsigned name_words
;
2308 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
2310 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
2311 stage_for_execution_model(w
[1]) != b
->entry_point_stage
)
2314 assert(b
->entry_point
== NULL
);
2315 b
->entry_point
= entry_point
;
2320 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
2321 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2325 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2328 case SpvOpMemberName
:
2332 case SpvOpExecutionMode
:
2333 case SpvOpDecorationGroup
:
2335 case SpvOpMemberDecorate
:
2336 case SpvOpGroupDecorate
:
2337 case SpvOpGroupMemberDecorate
:
2338 vtn_handle_decoration(b
, opcode
, w
, count
);
2342 return false; /* End of preamble */
2349 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
2350 const struct vtn_decoration
*mode
, void *data
)
2352 assert(b
->entry_point
== entry_point
);
2354 switch(mode
->exec_mode
) {
2355 case SpvExecutionModeOriginUpperLeft
:
2356 case SpvExecutionModeOriginLowerLeft
:
2357 b
->origin_upper_left
=
2358 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
2361 case SpvExecutionModeEarlyFragmentTests
:
2362 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2363 b
->shader
->info
.fs
.early_fragment_tests
= true;
2366 case SpvExecutionModeInvocations
:
2367 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2368 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->literals
[0]);
2371 case SpvExecutionModeDepthReplacing
:
2372 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2373 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
2375 case SpvExecutionModeDepthGreater
:
2376 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2377 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
2379 case SpvExecutionModeDepthLess
:
2380 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2381 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
2383 case SpvExecutionModeDepthUnchanged
:
2384 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2385 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
2388 case SpvExecutionModeLocalSize
:
2389 assert(b
->shader
->stage
== MESA_SHADER_COMPUTE
);
2390 b
->shader
->info
.cs
.local_size
[0] = mode
->literals
[0];
2391 b
->shader
->info
.cs
.local_size
[1] = mode
->literals
[1];
2392 b
->shader
->info
.cs
.local_size
[2] = mode
->literals
[2];
2394 case SpvExecutionModeLocalSizeHint
:
2395 break; /* Nothing do do with this */
2397 case SpvExecutionModeOutputVertices
:
2398 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2399 b
->shader
->info
.gs
.vertices_out
= mode
->literals
[0];
2402 case SpvExecutionModeInputPoints
:
2403 case SpvExecutionModeInputLines
:
2404 case SpvExecutionModeInputLinesAdjacency
:
2405 case SpvExecutionModeTriangles
:
2406 case SpvExecutionModeInputTrianglesAdjacency
:
2407 case SpvExecutionModeQuads
:
2408 case SpvExecutionModeIsolines
:
2409 if (b
->shader
->stage
== MESA_SHADER_GEOMETRY
) {
2410 b
->shader
->info
.gs
.vertices_in
=
2411 vertices_in_from_spv_execution_mode(mode
->exec_mode
);
2413 assert(!"Tesselation shaders not yet supported");
2417 case SpvExecutionModeOutputPoints
:
2418 case SpvExecutionModeOutputLineStrip
:
2419 case SpvExecutionModeOutputTriangleStrip
:
2420 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2421 b
->shader
->info
.gs
.output_primitive
=
2422 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
2425 case SpvExecutionModeSpacingEqual
:
2426 case SpvExecutionModeSpacingFractionalEven
:
2427 case SpvExecutionModeSpacingFractionalOdd
:
2428 case SpvExecutionModeVertexOrderCw
:
2429 case SpvExecutionModeVertexOrderCcw
:
2430 case SpvExecutionModePointMode
:
2431 assert(!"TODO: Add tessellation metadata");
2434 case SpvExecutionModePixelCenterInteger
:
2435 b
->pixel_center_integer
= true;
2438 case SpvExecutionModeXfb
:
2439 assert(!"Unhandled execution mode");
2442 case SpvExecutionModeVecTypeHint
:
2443 case SpvExecutionModeContractionOff
:
2449 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2450 const uint32_t *w
, unsigned count
)
2454 case SpvOpSourceContinued
:
2455 case SpvOpSourceExtension
:
2456 case SpvOpExtension
:
2457 case SpvOpCapability
:
2458 case SpvOpExtInstImport
:
2459 case SpvOpMemoryModel
:
2460 case SpvOpEntryPoint
:
2461 case SpvOpExecutionMode
:
2464 case SpvOpMemberName
:
2465 case SpvOpDecorationGroup
:
2467 case SpvOpMemberDecorate
:
2468 case SpvOpGroupDecorate
:
2469 case SpvOpGroupMemberDecorate
:
2470 assert(!"Invalid opcode types and variables section");
2476 case SpvOpTypeFloat
:
2477 case SpvOpTypeVector
:
2478 case SpvOpTypeMatrix
:
2479 case SpvOpTypeImage
:
2480 case SpvOpTypeSampler
:
2481 case SpvOpTypeSampledImage
:
2482 case SpvOpTypeArray
:
2483 case SpvOpTypeRuntimeArray
:
2484 case SpvOpTypeStruct
:
2485 case SpvOpTypeOpaque
:
2486 case SpvOpTypePointer
:
2487 case SpvOpTypeFunction
:
2488 case SpvOpTypeEvent
:
2489 case SpvOpTypeDeviceEvent
:
2490 case SpvOpTypeReserveId
:
2491 case SpvOpTypeQueue
:
2493 vtn_handle_type(b
, opcode
, w
, count
);
2496 case SpvOpConstantTrue
:
2497 case SpvOpConstantFalse
:
2499 case SpvOpConstantComposite
:
2500 case SpvOpConstantSampler
:
2501 case SpvOpConstantNull
:
2502 case SpvOpSpecConstantTrue
:
2503 case SpvOpSpecConstantFalse
:
2504 case SpvOpSpecConstant
:
2505 case SpvOpSpecConstantComposite
:
2506 case SpvOpSpecConstantOp
:
2507 vtn_handle_constant(b
, opcode
, w
, count
);
2511 vtn_handle_variables(b
, opcode
, w
, count
);
2515 return false; /* End of preamble */
2522 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2523 const uint32_t *w
, unsigned count
)
2529 case SpvOpLoopMerge
:
2530 case SpvOpSelectionMerge
:
2531 /* This is handled by cfg pre-pass and walk_blocks */
2535 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
2536 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2541 vtn_handle_extension(b
, opcode
, w
, count
);
2547 case SpvOpCopyMemory
:
2548 case SpvOpCopyMemorySized
:
2549 case SpvOpAccessChain
:
2550 case SpvOpInBoundsAccessChain
:
2551 case SpvOpArrayLength
:
2552 vtn_handle_variables(b
, opcode
, w
, count
);
2555 case SpvOpFunctionCall
:
2556 vtn_handle_function_call(b
, opcode
, w
, count
);
2559 case SpvOpSampledImage
:
2561 case SpvOpImageSampleImplicitLod
:
2562 case SpvOpImageSampleExplicitLod
:
2563 case SpvOpImageSampleDrefImplicitLod
:
2564 case SpvOpImageSampleDrefExplicitLod
:
2565 case SpvOpImageSampleProjImplicitLod
:
2566 case SpvOpImageSampleProjExplicitLod
:
2567 case SpvOpImageSampleProjDrefImplicitLod
:
2568 case SpvOpImageSampleProjDrefExplicitLod
:
2569 case SpvOpImageFetch
:
2570 case SpvOpImageGather
:
2571 case SpvOpImageDrefGather
:
2572 case SpvOpImageQuerySizeLod
:
2573 case SpvOpImageQueryLod
:
2574 case SpvOpImageQueryLevels
:
2575 case SpvOpImageQuerySamples
:
2576 vtn_handle_texture(b
, opcode
, w
, count
);
2579 case SpvOpImageRead
:
2580 case SpvOpImageWrite
:
2581 case SpvOpImageTexelPointer
:
2582 vtn_handle_image(b
, opcode
, w
, count
);
2585 case SpvOpImageQuerySize
: {
2586 struct vtn_access_chain
*image
=
2587 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
2588 if (glsl_type_is_image(image
->var
->var
->interface_type
)) {
2589 vtn_handle_image(b
, opcode
, w
, count
);
2591 vtn_handle_texture(b
, opcode
, w
, count
);
2596 case SpvOpAtomicExchange
:
2597 case SpvOpAtomicCompareExchange
:
2598 case SpvOpAtomicCompareExchangeWeak
:
2599 case SpvOpAtomicIIncrement
:
2600 case SpvOpAtomicIDecrement
:
2601 case SpvOpAtomicIAdd
:
2602 case SpvOpAtomicISub
:
2603 case SpvOpAtomicSMin
:
2604 case SpvOpAtomicUMin
:
2605 case SpvOpAtomicSMax
:
2606 case SpvOpAtomicUMax
:
2607 case SpvOpAtomicAnd
:
2609 case SpvOpAtomicXor
: {
2610 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
2611 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
2612 vtn_handle_image(b
, opcode
, w
, count
);
2614 assert(pointer
->value_type
== vtn_value_type_access_chain
);
2615 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
2625 case SpvOpConvertFToU
:
2626 case SpvOpConvertFToS
:
2627 case SpvOpConvertSToF
:
2628 case SpvOpConvertUToF
:
2632 case SpvOpQuantizeToF16
:
2633 case SpvOpConvertPtrToU
:
2634 case SpvOpConvertUToPtr
:
2635 case SpvOpPtrCastToGeneric
:
2636 case SpvOpGenericCastToPtr
:
2642 case SpvOpSignBitSet
:
2643 case SpvOpLessOrGreater
:
2645 case SpvOpUnordered
:
2660 case SpvOpVectorTimesScalar
:
2662 case SpvOpIAddCarry
:
2663 case SpvOpISubBorrow
:
2664 case SpvOpUMulExtended
:
2665 case SpvOpSMulExtended
:
2666 case SpvOpShiftRightLogical
:
2667 case SpvOpShiftRightArithmetic
:
2668 case SpvOpShiftLeftLogical
:
2669 case SpvOpLogicalEqual
:
2670 case SpvOpLogicalNotEqual
:
2671 case SpvOpLogicalOr
:
2672 case SpvOpLogicalAnd
:
2673 case SpvOpLogicalNot
:
2674 case SpvOpBitwiseOr
:
2675 case SpvOpBitwiseXor
:
2676 case SpvOpBitwiseAnd
:
2679 case SpvOpFOrdEqual
:
2680 case SpvOpFUnordEqual
:
2681 case SpvOpINotEqual
:
2682 case SpvOpFOrdNotEqual
:
2683 case SpvOpFUnordNotEqual
:
2684 case SpvOpULessThan
:
2685 case SpvOpSLessThan
:
2686 case SpvOpFOrdLessThan
:
2687 case SpvOpFUnordLessThan
:
2688 case SpvOpUGreaterThan
:
2689 case SpvOpSGreaterThan
:
2690 case SpvOpFOrdGreaterThan
:
2691 case SpvOpFUnordGreaterThan
:
2692 case SpvOpULessThanEqual
:
2693 case SpvOpSLessThanEqual
:
2694 case SpvOpFOrdLessThanEqual
:
2695 case SpvOpFUnordLessThanEqual
:
2696 case SpvOpUGreaterThanEqual
:
2697 case SpvOpSGreaterThanEqual
:
2698 case SpvOpFOrdGreaterThanEqual
:
2699 case SpvOpFUnordGreaterThanEqual
:
2705 case SpvOpFwidthFine
:
2706 case SpvOpDPdxCoarse
:
2707 case SpvOpDPdyCoarse
:
2708 case SpvOpFwidthCoarse
:
2709 case SpvOpBitFieldInsert
:
2710 case SpvOpBitFieldSExtract
:
2711 case SpvOpBitFieldUExtract
:
2712 case SpvOpBitReverse
:
2714 case SpvOpTranspose
:
2715 case SpvOpOuterProduct
:
2716 case SpvOpMatrixTimesScalar
:
2717 case SpvOpVectorTimesMatrix
:
2718 case SpvOpMatrixTimesVector
:
2719 case SpvOpMatrixTimesMatrix
:
2720 vtn_handle_alu(b
, opcode
, w
, count
);
2723 case SpvOpVectorExtractDynamic
:
2724 case SpvOpVectorInsertDynamic
:
2725 case SpvOpVectorShuffle
:
2726 case SpvOpCompositeConstruct
:
2727 case SpvOpCompositeExtract
:
2728 case SpvOpCompositeInsert
:
2729 case SpvOpCopyObject
:
2730 vtn_handle_composite(b
, opcode
, w
, count
);
2733 case SpvOpEmitVertex
:
2734 case SpvOpEndPrimitive
:
2735 case SpvOpEmitStreamVertex
:
2736 case SpvOpEndStreamPrimitive
:
2737 case SpvOpControlBarrier
:
2738 case SpvOpMemoryBarrier
:
2739 vtn_handle_barrier(b
, opcode
, w
, count
);
2743 unreachable("Unhandled opcode");
2750 spirv_to_nir(const uint32_t *words
, size_t word_count
,
2751 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
2752 gl_shader_stage stage
, const char *entry_point_name
,
2753 const nir_shader_compiler_options
*options
)
2755 const uint32_t *word_end
= words
+ word_count
;
2757 /* Handle the SPIR-V header (first 4 dwords) */
2758 assert(word_count
> 5);
2760 assert(words
[0] == SpvMagicNumber
);
2761 assert(words
[1] >= 0x10000);
2762 /* words[2] == generator magic */
2763 unsigned value_id_bound
= words
[3];
2764 assert(words
[4] == 0);
2768 /* Initialize the stn_builder object */
2769 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
2770 b
->value_id_bound
= value_id_bound
;
2771 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
2772 exec_list_make_empty(&b
->functions
);
2773 b
->entry_point_stage
= stage
;
2774 b
->entry_point_name
= entry_point_name
;
2776 /* Handle all the preamble instructions */
2777 words
= vtn_foreach_instruction(b
, words
, word_end
,
2778 vtn_handle_preamble_instruction
);
2780 if (b
->entry_point
== NULL
) {
2781 assert(!"Entry point not found");
2786 b
->shader
= nir_shader_create(NULL
, stage
, options
);
2788 /* Set shader info defaults */
2789 b
->shader
->info
.gs
.invocations
= 1;
2791 /* Parse execution modes */
2792 vtn_foreach_execution_mode(b
, b
->entry_point
,
2793 vtn_handle_execution_mode
, NULL
);
2795 b
->specializations
= spec
;
2796 b
->num_specializations
= num_spec
;
2798 /* Handle all variable, type, and constant instructions */
2799 words
= vtn_foreach_instruction(b
, words
, word_end
,
2800 vtn_handle_variable_or_type_instruction
);
2802 vtn_build_cfg(b
, words
, word_end
);
2804 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
2805 b
->impl
= func
->impl
;
2806 b
->const_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
2807 _mesa_key_pointer_equal
);
2809 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
2812 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
2813 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
2814 assert(entry_point
);