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 break; /* FIXME: Do nothing with this for now. */
461 case SpvDecorationNoPerspective
:
462 ctx
->fields
[member
].interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
464 case SpvDecorationFlat
:
465 ctx
->fields
[member
].interpolation
= INTERP_QUALIFIER_FLAT
;
467 case SpvDecorationCentroid
:
468 ctx
->fields
[member
].centroid
= true;
470 case SpvDecorationSample
:
471 ctx
->fields
[member
].sample
= true;
473 case SpvDecorationLocation
:
474 ctx
->fields
[member
].location
= dec
->literals
[0];
476 case SpvDecorationBuiltIn
:
477 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
478 ctx
->type
->members
[member
]->is_builtin
= true;
479 ctx
->type
->members
[member
]->builtin
= dec
->literals
[0];
480 ctx
->type
->builtin_block
= true;
482 case SpvDecorationOffset
:
483 ctx
->type
->offsets
[member
] = dec
->literals
[0];
485 case SpvDecorationMatrixStride
:
486 mutable_matrix_member(b
, ctx
->type
, member
)->stride
= dec
->literals
[0];
488 case SpvDecorationColMajor
:
489 break; /* Nothing to do here. Column-major is the default. */
490 case SpvDecorationRowMajor
:
491 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
494 unreachable("Unhandled member decoration");
499 type_decoration_cb(struct vtn_builder
*b
,
500 struct vtn_value
*val
, int member
,
501 const struct vtn_decoration
*dec
, void *ctx
)
503 struct vtn_type
*type
= val
->type
;
508 switch (dec
->decoration
) {
509 case SpvDecorationArrayStride
:
510 type
->stride
= dec
->literals
[0];
512 case SpvDecorationBlock
:
515 case SpvDecorationBufferBlock
:
516 type
->buffer_block
= true;
518 case SpvDecorationGLSLShared
:
519 case SpvDecorationGLSLPacked
:
520 /* Ignore these, since we get explicit offsets anyways */
523 case SpvDecorationStream
:
524 assert(dec
->literals
[0] == 0);
528 unreachable("Unhandled type decoration");
533 translate_image_format(SpvImageFormat format
)
536 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
537 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
538 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
539 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
540 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
541 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
542 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
543 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
544 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
545 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
546 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
547 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
548 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
549 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
550 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
551 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
552 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
553 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
554 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
555 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
556 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
557 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
558 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
559 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
560 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
561 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
562 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
563 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
564 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
565 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
566 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
567 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
568 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
569 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
570 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
571 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
572 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
573 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
574 case SpvImageFormatR16ui
: return 0x823A; /* GL_RG16UI */
575 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
577 assert(!"Invalid image format");
583 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
584 const uint32_t *w
, unsigned count
)
586 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
588 val
->type
= rzalloc(b
, struct vtn_type
);
589 val
->type
->is_builtin
= false;
590 val
->type
->val
= val
;
594 val
->type
->type
= glsl_void_type();
597 val
->type
->type
= glsl_bool_type();
600 const bool signedness
= w
[3];
601 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
605 val
->type
->type
= glsl_float_type();
608 case SpvOpTypeVector
: {
609 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
610 unsigned elems
= w
[3];
612 assert(glsl_type_is_scalar(base
->type
));
613 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
615 /* Vectors implicitly have sizeof(base_type) stride. For now, this
616 * is always 4 bytes. This will have to change if we want to start
617 * supporting doubles or half-floats.
619 val
->type
->stride
= 4;
620 val
->type
->array_element
= base
;
624 case SpvOpTypeMatrix
: {
625 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
626 unsigned columns
= w
[3];
628 assert(glsl_type_is_vector(base
->type
));
629 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
630 glsl_get_vector_elements(base
->type
),
632 assert(!glsl_type_is_error(val
->type
->type
));
633 val
->type
->array_element
= base
;
634 val
->type
->row_major
= false;
635 val
->type
->stride
= 0;
639 case SpvOpTypeRuntimeArray
:
640 case SpvOpTypeArray
: {
641 struct vtn_type
*array_element
=
642 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
645 if (opcode
== SpvOpTypeRuntimeArray
) {
646 /* A length of 0 is used to denote unsized arrays */
650 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->value
.u
[0];
653 val
->type
->type
= glsl_array_type(array_element
->type
, length
);
654 val
->type
->array_element
= array_element
;
655 val
->type
->stride
= 0;
659 case SpvOpTypeStruct
: {
660 unsigned num_fields
= count
- 2;
661 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
662 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
664 NIR_VLA(struct glsl_struct_field
, fields
, count
);
665 for (unsigned i
= 0; i
< num_fields
; i
++) {
666 val
->type
->members
[i
] =
667 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
668 fields
[i
] = (struct glsl_struct_field
) {
669 .type
= val
->type
->members
[i
]->type
,
670 .name
= ralloc_asprintf(b
, "field%d", i
),
675 struct member_decoration_ctx ctx
= {
676 .num_fields
= num_fields
,
681 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
683 const char *name
= val
->name
? val
->name
: "struct";
685 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
);
689 case SpvOpTypeFunction
: {
690 const struct glsl_type
*return_type
=
691 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
692 NIR_VLA(struct glsl_function_param
, params
, count
- 3);
693 for (unsigned i
= 0; i
< count
- 3; i
++) {
694 params
[i
].type
= vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
->type
;
698 params
[i
].out
= true;
700 val
->type
->type
= glsl_function_type(return_type
, params
, count
- 3);
704 case SpvOpTypePointer
:
705 /* FIXME: For now, we'll just do the really lame thing and return
706 * the same type. The validator should ensure that the proper number
707 * of dereferences happen
709 val
->type
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
712 case SpvOpTypeImage
: {
713 const struct glsl_type
*sampled_type
=
714 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
716 assert(glsl_type_is_vector_or_scalar(sampled_type
));
718 enum glsl_sampler_dim dim
;
719 switch ((SpvDim
)w
[3]) {
720 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
721 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
722 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
723 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
724 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
725 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
727 unreachable("Invalid SPIR-V Sampler dimension");
730 bool is_shadow
= w
[4];
731 bool is_array
= w
[5];
732 bool multisampled
= w
[6];
733 unsigned sampled
= w
[7];
734 SpvImageFormat format
= w
[8];
737 val
->type
->access_qualifier
= w
[9];
739 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
742 assert(dim
== GLSL_SAMPLER_DIM_2D
);
743 dim
= GLSL_SAMPLER_DIM_MS
;
746 val
->type
->image_format
= translate_image_format(format
);
749 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
750 glsl_get_base_type(sampled_type
));
751 } else if (sampled
== 2) {
754 val
->type
->type
= glsl_image_type(dim
, is_array
,
755 glsl_get_base_type(sampled_type
));
757 assert(!"We need to know if the image will be sampled");
762 case SpvOpTypeSampledImage
:
763 val
->type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
766 case SpvOpTypeSampler
:
767 /* The actual sampler type here doesn't really matter. It gets
768 * thrown away the moment you combine it with an image. What really
769 * matters is that it's a sampler type as opposed to an integer type
770 * so the backend knows what to do.
772 val
->type
->type
= glsl_bare_sampler_type();
775 case SpvOpTypeOpaque
:
777 case SpvOpTypeDeviceEvent
:
778 case SpvOpTypeReserveId
:
782 unreachable("Unhandled opcode");
785 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
788 static nir_constant
*
789 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
791 nir_constant
*c
= rzalloc(b
, nir_constant
);
793 switch (glsl_get_base_type(type
)) {
797 case GLSL_TYPE_FLOAT
:
798 case GLSL_TYPE_DOUBLE
:
799 /* Nothing to do here. It's already initialized to zero */
802 case GLSL_TYPE_ARRAY
:
803 assert(glsl_get_length(type
) > 0);
804 c
->num_elements
= glsl_get_length(type
);
805 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
807 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
808 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
809 c
->elements
[i
] = c
->elements
[0];
812 case GLSL_TYPE_STRUCT
:
813 c
->num_elements
= glsl_get_length(type
);
814 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
816 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
817 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
822 unreachable("Invalid type for null constant");
829 spec_constant_deocoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
830 int member
, const struct vtn_decoration
*dec
,
833 assert(member
== -1);
834 if (dec
->decoration
!= SpvDecorationSpecId
)
837 uint32_t *const_value
= data
;
839 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
840 if (b
->specializations
[i
].id
== dec
->literals
[0]) {
841 *const_value
= b
->specializations
[i
].data
;
848 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
849 uint32_t const_value
)
851 vtn_foreach_decoration(b
, val
, spec_constant_deocoration_cb
, &const_value
);
856 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
857 const uint32_t *w
, unsigned count
)
859 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
860 val
->const_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
861 val
->constant
= rzalloc(b
, nir_constant
);
863 case SpvOpConstantTrue
:
864 assert(val
->const_type
== glsl_bool_type());
865 val
->constant
->value
.u
[0] = NIR_TRUE
;
867 case SpvOpConstantFalse
:
868 assert(val
->const_type
== glsl_bool_type());
869 val
->constant
->value
.u
[0] = NIR_FALSE
;
872 case SpvOpSpecConstantTrue
:
873 case SpvOpSpecConstantFalse
: {
874 assert(val
->const_type
== glsl_bool_type());
876 get_specialization(b
, val
, (opcode
== SpvOpSpecConstantTrue
));
877 val
->constant
->value
.u
[0] = int_val
? NIR_TRUE
: NIR_FALSE
;
882 assert(glsl_type_is_scalar(val
->const_type
));
883 val
->constant
->value
.u
[0] = w
[3];
885 case SpvOpSpecConstant
:
886 assert(glsl_type_is_scalar(val
->const_type
));
887 val
->constant
->value
.u
[0] = get_specialization(b
, val
, w
[3]);
889 case SpvOpSpecConstantComposite
:
890 case SpvOpConstantComposite
: {
891 unsigned elem_count
= count
- 3;
892 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
893 for (unsigned i
= 0; i
< elem_count
; i
++)
894 elems
[i
] = vtn_value(b
, w
[i
+ 3], vtn_value_type_constant
)->constant
;
896 switch (glsl_get_base_type(val
->const_type
)) {
899 case GLSL_TYPE_FLOAT
:
901 if (glsl_type_is_matrix(val
->const_type
)) {
902 unsigned rows
= glsl_get_vector_elements(val
->const_type
);
903 assert(glsl_get_matrix_columns(val
->const_type
) == elem_count
);
904 for (unsigned i
= 0; i
< elem_count
; i
++)
905 for (unsigned j
= 0; j
< rows
; j
++)
906 val
->constant
->value
.u
[rows
* i
+ j
] = elems
[i
]->value
.u
[j
];
908 assert(glsl_type_is_vector(val
->const_type
));
909 assert(glsl_get_vector_elements(val
->const_type
) == elem_count
);
910 for (unsigned i
= 0; i
< elem_count
; i
++)
911 val
->constant
->value
.u
[i
] = elems
[i
]->value
.u
[0];
916 case GLSL_TYPE_STRUCT
:
917 case GLSL_TYPE_ARRAY
:
918 ralloc_steal(val
->constant
, elems
);
919 val
->constant
->num_elements
= elem_count
;
920 val
->constant
->elements
= elems
;
924 unreachable("Unsupported type for constants");
929 case SpvOpSpecConstantOp
: {
930 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
932 case SpvOpVectorShuffle
: {
933 struct vtn_value
*v0
= vtn_value(b
, w
[4], vtn_value_type_constant
);
934 struct vtn_value
*v1
= vtn_value(b
, w
[5], vtn_value_type_constant
);
935 unsigned len0
= glsl_get_vector_elements(v0
->const_type
);
936 unsigned len1
= glsl_get_vector_elements(v1
->const_type
);
939 for (unsigned i
= 0; i
< len0
; i
++)
940 u
[i
] = v0
->constant
->value
.u
[i
];
941 for (unsigned i
= 0; i
< len1
; i
++)
942 u
[len0
+ i
] = v1
->constant
->value
.u
[i
];
944 for (unsigned i
= 0; i
< count
- 6; i
++) {
945 uint32_t comp
= w
[i
+ 6];
946 if (comp
== (uint32_t)-1) {
947 val
->constant
->value
.u
[i
] = 0xdeadbeef;
949 val
->constant
->value
.u
[i
] = u
[comp
];
955 case SpvOpCompositeExtract
:
956 case SpvOpCompositeInsert
: {
957 struct vtn_value
*comp
;
958 unsigned deref_start
;
959 struct nir_constant
**c
;
960 if (opcode
== SpvOpCompositeExtract
) {
961 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
965 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
967 val
->constant
= nir_constant_clone(comp
->constant
,
973 const struct glsl_type
*type
= comp
->const_type
;
974 for (unsigned i
= deref_start
; i
< count
; i
++) {
975 switch (glsl_get_base_type(type
)) {
978 case GLSL_TYPE_FLOAT
:
980 /* If we hit this granularity, we're picking off an element */
984 if (glsl_type_is_matrix(type
)) {
985 elem
+= w
[i
] * glsl_get_vector_elements(type
);
986 type
= glsl_get_column_type(type
);
988 assert(glsl_type_is_vector(type
));
990 type
= glsl_scalar_type(glsl_get_base_type(type
));
994 case GLSL_TYPE_ARRAY
:
995 c
= &(*c
)->elements
[w
[i
]];
996 type
= glsl_get_array_element(type
);
999 case GLSL_TYPE_STRUCT
:
1000 c
= &(*c
)->elements
[w
[i
]];
1001 type
= glsl_get_struct_field(type
, w
[i
]);
1005 unreachable("Invalid constant type");
1009 if (opcode
== SpvOpCompositeExtract
) {
1013 unsigned num_components
= glsl_get_vector_elements(type
);
1014 for (unsigned i
= 0; i
< num_components
; i
++)
1015 val
->constant
->value
.u
[i
] = (*c
)->value
.u
[elem
+ i
];
1018 struct vtn_value
*insert
=
1019 vtn_value(b
, w
[4], vtn_value_type_constant
);
1020 assert(insert
->const_type
== type
);
1022 *c
= insert
->constant
;
1024 unsigned num_components
= glsl_get_vector_elements(type
);
1025 for (unsigned i
= 0; i
< num_components
; i
++)
1026 (*c
)->value
.u
[elem
+ i
] = insert
->constant
->value
.u
[i
];
1034 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(opcode
, &swap
);
1036 unsigned num_components
= glsl_get_vector_elements(val
->const_type
);
1038 glsl_get_bit_size(val
->const_type
);
1040 nir_const_value src
[4];
1042 for (unsigned i
= 0; i
< count
- 4; i
++) {
1044 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
)->constant
;
1046 unsigned j
= swap
? 1 - i
: i
;
1047 assert(bit_size
== 32);
1048 for (unsigned k
= 0; k
< num_components
; k
++)
1049 src
[j
].u32
[k
] = c
->value
.u
[k
];
1052 nir_const_value res
= nir_eval_const_opcode(op
, num_components
,
1055 for (unsigned k
= 0; k
< num_components
; k
++)
1056 val
->constant
->value
.u
[k
] = res
.u32
[k
];
1063 case SpvOpConstantNull
:
1064 val
->constant
= vtn_null_constant(b
, val
->const_type
);
1067 case SpvOpConstantSampler
:
1068 assert(!"OpConstantSampler requires Kernel Capability");
1072 unreachable("Unhandled opcode");
1077 vtn_handle_function_call(struct vtn_builder
*b
, SpvOp opcode
,
1078 const uint32_t *w
, unsigned count
)
1080 struct nir_function
*callee
=
1081 vtn_value(b
, w
[3], vtn_value_type_function
)->func
->impl
->function
;
1083 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, callee
);
1084 for (unsigned i
= 0; i
< call
->num_params
; i
++) {
1085 unsigned arg_id
= w
[4 + i
];
1086 struct vtn_value
*arg
= vtn_untyped_value(b
, arg_id
);
1087 if (arg
->value_type
== vtn_value_type_access_chain
) {
1088 nir_deref_var
*d
= vtn_access_chain_to_deref(b
, arg
->access_chain
);
1089 call
->params
[i
] = nir_deref_as_var(nir_copy_deref(call
, &d
->deref
));
1091 struct vtn_ssa_value
*arg_ssa
= vtn_ssa_value(b
, arg_id
);
1093 /* Make a temporary to store the argument in */
1095 nir_local_variable_create(b
->impl
, arg_ssa
->type
, "arg_tmp");
1096 call
->params
[i
] = nir_deref_var_create(call
, tmp
);
1098 vtn_local_store(b
, arg_ssa
, call
->params
[i
]);
1102 nir_variable
*out_tmp
= NULL
;
1103 if (!glsl_type_is_void(callee
->return_type
)) {
1104 out_tmp
= nir_local_variable_create(b
->impl
, callee
->return_type
,
1106 call
->return_deref
= nir_deref_var_create(call
, out_tmp
);
1109 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
1111 if (glsl_type_is_void(callee
->return_type
)) {
1112 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1114 struct vtn_value
*retval
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1115 retval
->ssa
= vtn_local_load(b
, call
->return_deref
);
1119 struct vtn_ssa_value
*
1120 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1122 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1125 if (!glsl_type_is_vector_or_scalar(type
)) {
1126 unsigned elems
= glsl_get_length(type
);
1127 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1128 for (unsigned i
= 0; i
< elems
; i
++) {
1129 const struct glsl_type
*child_type
;
1131 switch (glsl_get_base_type(type
)) {
1133 case GLSL_TYPE_UINT
:
1134 case GLSL_TYPE_BOOL
:
1135 case GLSL_TYPE_FLOAT
:
1136 case GLSL_TYPE_DOUBLE
:
1137 child_type
= glsl_get_column_type(type
);
1139 case GLSL_TYPE_ARRAY
:
1140 child_type
= glsl_get_array_element(type
);
1142 case GLSL_TYPE_STRUCT
:
1143 child_type
= glsl_get_struct_field(type
, i
);
1146 unreachable("unkown base type");
1149 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
1157 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
1160 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
1161 src
.src_type
= type
;
1166 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
1167 const uint32_t *w
, unsigned count
)
1169 if (opcode
== SpvOpSampledImage
) {
1170 struct vtn_value
*val
=
1171 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1172 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1173 val
->sampled_image
->image
=
1174 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1175 val
->sampled_image
->sampler
=
1176 vtn_value(b
, w
[4], vtn_value_type_access_chain
)->access_chain
;
1178 } else if (opcode
== SpvOpImage
) {
1179 struct vtn_value
*val
=
1180 vtn_push_value(b
, w
[2], vtn_value_type_access_chain
);
1181 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
1182 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
1183 val
->access_chain
= src_val
->sampled_image
->image
;
1185 assert(src_val
->value_type
== vtn_value_type_access_chain
);
1186 val
->access_chain
= src_val
->access_chain
;
1191 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1192 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1194 struct vtn_sampled_image sampled
;
1195 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
1196 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
1197 sampled
= *sampled_val
->sampled_image
;
1199 assert(sampled_val
->value_type
== vtn_value_type_access_chain
);
1200 sampled
.image
= NULL
;
1201 sampled
.sampler
= sampled_val
->access_chain
;
1204 const struct glsl_type
*image_type
;
1205 if (sampled
.image
) {
1206 image_type
= sampled
.image
->var
->var
->interface_type
;
1208 image_type
= sampled
.sampler
->var
->var
->interface_type
;
1211 nir_tex_src srcs
[8]; /* 8 should be enough */
1212 nir_tex_src
*p
= srcs
;
1216 bool has_coord
= false;
1218 case SpvOpImageSampleImplicitLod
:
1219 case SpvOpImageSampleExplicitLod
:
1220 case SpvOpImageSampleDrefImplicitLod
:
1221 case SpvOpImageSampleDrefExplicitLod
:
1222 case SpvOpImageSampleProjImplicitLod
:
1223 case SpvOpImageSampleProjExplicitLod
:
1224 case SpvOpImageSampleProjDrefImplicitLod
:
1225 case SpvOpImageSampleProjDrefExplicitLod
:
1226 case SpvOpImageFetch
:
1227 case SpvOpImageGather
:
1228 case SpvOpImageDrefGather
:
1229 case SpvOpImageQueryLod
: {
1230 /* All these types have the coordinate as their first real argument */
1231 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, w
[idx
++]);
1233 p
->src
= nir_src_for_ssa(coord
->def
);
1234 p
->src_type
= nir_tex_src_coord
;
1243 /* These all have an explicit depth value as their next source */
1245 case SpvOpImageSampleDrefImplicitLod
:
1246 case SpvOpImageSampleDrefExplicitLod
:
1247 case SpvOpImageSampleProjDrefImplicitLod
:
1248 case SpvOpImageSampleProjDrefExplicitLod
:
1249 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparitor
);
1255 /* For OpImageQuerySizeLod, we always have an LOD */
1256 if (opcode
== SpvOpImageQuerySizeLod
)
1257 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1259 /* Figure out the base texture operation */
1262 case SpvOpImageSampleImplicitLod
:
1263 case SpvOpImageSampleDrefImplicitLod
:
1264 case SpvOpImageSampleProjImplicitLod
:
1265 case SpvOpImageSampleProjDrefImplicitLod
:
1266 texop
= nir_texop_tex
;
1269 case SpvOpImageSampleExplicitLod
:
1270 case SpvOpImageSampleDrefExplicitLod
:
1271 case SpvOpImageSampleProjExplicitLod
:
1272 case SpvOpImageSampleProjDrefExplicitLod
:
1273 texop
= nir_texop_txl
;
1276 case SpvOpImageFetch
:
1277 if (glsl_get_sampler_dim(image_type
) == GLSL_SAMPLER_DIM_MS
) {
1278 texop
= nir_texop_txf_ms
;
1280 texop
= nir_texop_txf
;
1284 case SpvOpImageGather
:
1285 case SpvOpImageDrefGather
:
1286 texop
= nir_texop_tg4
;
1289 case SpvOpImageQuerySizeLod
:
1290 case SpvOpImageQuerySize
:
1291 texop
= nir_texop_txs
;
1294 case SpvOpImageQueryLod
:
1295 texop
= nir_texop_lod
;
1298 case SpvOpImageQueryLevels
:
1299 texop
= nir_texop_query_levels
;
1302 case SpvOpImageQuerySamples
:
1304 unreachable("Unhandled opcode");
1307 /* Now we need to handle some number of optional arguments */
1309 uint32_t operands
= w
[idx
++];
1311 if (operands
& SpvImageOperandsBiasMask
) {
1312 assert(texop
== nir_texop_tex
);
1313 texop
= nir_texop_txb
;
1314 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
1317 if (operands
& SpvImageOperandsLodMask
) {
1318 assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
1319 texop
== nir_texop_txf_ms
|| texop
== nir_texop_txs
);
1320 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1323 if (operands
& SpvImageOperandsGradMask
) {
1324 assert(texop
== nir_texop_tex
);
1325 texop
= nir_texop_txd
;
1326 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
1327 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
1330 if (operands
& SpvImageOperandsOffsetMask
||
1331 operands
& SpvImageOperandsConstOffsetMask
)
1332 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
1334 if (operands
& SpvImageOperandsConstOffsetsMask
)
1335 assert(!"Constant offsets to texture gather not yet implemented");
1337 if (operands
& SpvImageOperandsSampleMask
) {
1338 assert(texop
== nir_texop_txf_ms
);
1339 texop
= nir_texop_txf_ms
;
1340 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
1343 /* We should have now consumed exactly all of the arguments */
1344 assert(idx
== count
);
1346 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
1349 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1351 instr
->sampler_dim
= glsl_get_sampler_dim(image_type
);
1352 instr
->is_array
= glsl_sampler_type_is_array(image_type
);
1353 instr
->is_shadow
= glsl_sampler_type_is_shadow(image_type
);
1354 instr
->is_new_style_shadow
= instr
->is_shadow
;
1357 switch (instr
->sampler_dim
) {
1358 case GLSL_SAMPLER_DIM_1D
:
1359 case GLSL_SAMPLER_DIM_BUF
:
1360 instr
->coord_components
= 1;
1362 case GLSL_SAMPLER_DIM_2D
:
1363 case GLSL_SAMPLER_DIM_RECT
:
1364 case GLSL_SAMPLER_DIM_MS
:
1365 instr
->coord_components
= 2;
1367 case GLSL_SAMPLER_DIM_3D
:
1368 case GLSL_SAMPLER_DIM_CUBE
:
1369 instr
->coord_components
= 3;
1372 assert("Invalid sampler type");
1375 if (instr
->is_array
)
1376 instr
->coord_components
++;
1378 instr
->coord_components
= 0;
1381 switch (glsl_get_sampler_result_type(image_type
)) {
1382 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
1383 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
1384 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
1385 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
1387 unreachable("Invalid base type for sampler result");
1390 nir_deref_var
*sampler
= vtn_access_chain_to_deref(b
, sampled
.sampler
);
1391 if (sampled
.image
) {
1392 nir_deref_var
*image
= vtn_access_chain_to_deref(b
, sampled
.image
);
1393 instr
->texture
= nir_deref_as_var(nir_copy_deref(instr
, &image
->deref
));
1395 instr
->texture
= nir_deref_as_var(nir_copy_deref(instr
, &sampler
->deref
));
1398 switch (instr
->op
) {
1403 /* These operations require a sampler */
1404 instr
->sampler
= nir_deref_as_var(nir_copy_deref(instr
, &sampler
->deref
));
1407 case nir_texop_txf_ms
:
1411 case nir_texop_query_levels
:
1412 case nir_texop_texture_samples
:
1413 case nir_texop_samples_identical
:
1415 instr
->sampler
= NULL
;
1417 case nir_texop_txf_ms_mcs
:
1418 unreachable("unexpected nir_texop_txf_ms_mcs");
1421 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1422 nir_tex_instr_dest_size(instr
), 32, NULL
);
1424 assert(glsl_get_vector_elements(ret_type
->type
) ==
1425 nir_tex_instr_dest_size(instr
));
1427 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
1428 val
->ssa
->def
= &instr
->dest
.ssa
;
1430 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
1433 static nir_ssa_def
*
1434 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
1436 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
1438 /* The image_load_store intrinsics assume a 4-dim coordinate */
1439 unsigned dim
= glsl_get_vector_elements(coord
->type
);
1440 unsigned swizzle
[4];
1441 for (unsigned i
= 0; i
< 4; i
++)
1442 swizzle
[i
] = MIN2(i
, dim
- 1);
1444 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
1448 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
1449 const uint32_t *w
, unsigned count
)
1451 /* Just get this one out of the way */
1452 if (opcode
== SpvOpImageTexelPointer
) {
1453 struct vtn_value
*val
=
1454 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
1455 val
->image
= ralloc(b
, struct vtn_image_pointer
);
1458 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1459 val
->image
->coord
= get_image_coord(b
, w
[4]);
1460 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
1464 struct vtn_image_pointer image
;
1467 case SpvOpAtomicExchange
:
1468 case SpvOpAtomicCompareExchange
:
1469 case SpvOpAtomicCompareExchangeWeak
:
1470 case SpvOpAtomicIIncrement
:
1471 case SpvOpAtomicIDecrement
:
1472 case SpvOpAtomicIAdd
:
1473 case SpvOpAtomicISub
:
1474 case SpvOpAtomicSMin
:
1475 case SpvOpAtomicUMin
:
1476 case SpvOpAtomicSMax
:
1477 case SpvOpAtomicUMax
:
1478 case SpvOpAtomicAnd
:
1480 case SpvOpAtomicXor
:
1481 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
1484 case SpvOpImageQuerySize
:
1486 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1488 image
.sample
= NULL
;
1491 case SpvOpImageRead
:
1493 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1494 image
.coord
= get_image_coord(b
, w
[4]);
1496 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
1497 assert(w
[5] == SpvImageOperandsSampleMask
);
1498 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
1500 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1504 case SpvOpImageWrite
:
1506 vtn_value(b
, w
[1], vtn_value_type_access_chain
)->access_chain
;
1507 image
.coord
= get_image_coord(b
, w
[2]);
1511 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
1512 assert(w
[4] == SpvImageOperandsSampleMask
);
1513 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
1515 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
1520 unreachable("Invalid image opcode");
1523 nir_intrinsic_op op
;
1525 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_##N; break;
1526 OP(ImageQuerySize
, size
)
1528 OP(ImageWrite
, store
)
1529 OP(AtomicExchange
, atomic_exchange
)
1530 OP(AtomicCompareExchange
, atomic_comp_swap
)
1531 OP(AtomicIIncrement
, atomic_add
)
1532 OP(AtomicIDecrement
, atomic_add
)
1533 OP(AtomicIAdd
, atomic_add
)
1534 OP(AtomicISub
, atomic_add
)
1535 OP(AtomicSMin
, atomic_min
)
1536 OP(AtomicUMin
, atomic_min
)
1537 OP(AtomicSMax
, atomic_max
)
1538 OP(AtomicUMax
, atomic_max
)
1539 OP(AtomicAnd
, atomic_and
)
1540 OP(AtomicOr
, atomic_or
)
1541 OP(AtomicXor
, atomic_xor
)
1544 unreachable("Invalid image opcode");
1547 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
1549 nir_deref_var
*image_deref
= vtn_access_chain_to_deref(b
, image
.image
);
1550 intrin
->variables
[0] =
1551 nir_deref_as_var(nir_copy_deref(&intrin
->instr
, &image_deref
->deref
));
1553 /* ImageQuerySize doesn't take any extra parameters */
1554 if (opcode
!= SpvOpImageQuerySize
) {
1555 /* The image coordinate is always 4 components but we may not have that
1556 * many. Swizzle to compensate.
1559 for (unsigned i
= 0; i
< 4; i
++)
1560 swiz
[i
] = i
< image
.coord
->num_components
? i
: 0;
1561 intrin
->src
[0] = nir_src_for_ssa(nir_swizzle(&b
->nb
, image
.coord
,
1563 intrin
->src
[1] = nir_src_for_ssa(image
.sample
);
1567 case SpvOpImageQuerySize
:
1568 case SpvOpImageRead
:
1570 case SpvOpImageWrite
:
1571 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[3])->def
);
1573 case SpvOpAtomicIIncrement
:
1574 intrin
->src
[2] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
1576 case SpvOpAtomicIDecrement
:
1577 intrin
->src
[2] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
1580 case SpvOpAtomicExchange
:
1581 case SpvOpAtomicIAdd
:
1582 case SpvOpAtomicSMin
:
1583 case SpvOpAtomicUMin
:
1584 case SpvOpAtomicSMax
:
1585 case SpvOpAtomicUMax
:
1586 case SpvOpAtomicAnd
:
1588 case SpvOpAtomicXor
:
1589 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1592 case SpvOpAtomicCompareExchange
:
1593 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
1594 intrin
->src
[3] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1597 case SpvOpAtomicISub
:
1598 intrin
->src
[2] = nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
1602 unreachable("Invalid image opcode");
1605 if (opcode
!= SpvOpImageWrite
) {
1606 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1607 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1608 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 4, 32, NULL
);
1610 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
1612 /* The image intrinsics always return 4 channels but we may not want
1613 * that many. Emit a mov to trim it down.
1615 unsigned swiz
[4] = {0, 1, 2, 3};
1616 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
1617 val
->ssa
->def
= nir_swizzle(&b
->nb
, &intrin
->dest
.ssa
, swiz
,
1618 glsl_get_vector_elements(type
->type
), false);
1620 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
1624 static nir_intrinsic_op
1625 get_ssbo_nir_atomic_op(SpvOp opcode
)
1628 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
1629 OP(AtomicExchange
, atomic_exchange
)
1630 OP(AtomicCompareExchange
, atomic_comp_swap
)
1631 OP(AtomicIIncrement
, atomic_add
)
1632 OP(AtomicIDecrement
, atomic_add
)
1633 OP(AtomicIAdd
, atomic_add
)
1634 OP(AtomicISub
, atomic_add
)
1635 OP(AtomicSMin
, atomic_imin
)
1636 OP(AtomicUMin
, atomic_umin
)
1637 OP(AtomicSMax
, atomic_imax
)
1638 OP(AtomicUMax
, atomic_umax
)
1639 OP(AtomicAnd
, atomic_and
)
1640 OP(AtomicOr
, atomic_or
)
1641 OP(AtomicXor
, atomic_xor
)
1644 unreachable("Invalid SSBO atomic");
1648 static nir_intrinsic_op
1649 get_shared_nir_atomic_op(SpvOp opcode
)
1652 #define OP(S, N) case SpvOp##S: return nir_intrinsic_var_##N;
1653 OP(AtomicExchange
, atomic_exchange
)
1654 OP(AtomicCompareExchange
, atomic_comp_swap
)
1655 OP(AtomicIIncrement
, atomic_add
)
1656 OP(AtomicIDecrement
, atomic_add
)
1657 OP(AtomicIAdd
, atomic_add
)
1658 OP(AtomicISub
, atomic_add
)
1659 OP(AtomicSMin
, atomic_imin
)
1660 OP(AtomicUMin
, atomic_umin
)
1661 OP(AtomicSMax
, atomic_imax
)
1662 OP(AtomicUMax
, atomic_umax
)
1663 OP(AtomicAnd
, atomic_and
)
1664 OP(AtomicOr
, atomic_or
)
1665 OP(AtomicXor
, atomic_xor
)
1668 unreachable("Invalid shared atomic");
1673 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
1674 const uint32_t *w
, nir_src
*src
)
1677 case SpvOpAtomicIIncrement
:
1678 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
1681 case SpvOpAtomicIDecrement
:
1682 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
1685 case SpvOpAtomicISub
:
1687 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
1690 case SpvOpAtomicCompareExchange
:
1691 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
1692 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
1696 case SpvOpAtomicExchange
:
1697 case SpvOpAtomicIAdd
:
1698 case SpvOpAtomicSMin
:
1699 case SpvOpAtomicUMin
:
1700 case SpvOpAtomicSMax
:
1701 case SpvOpAtomicUMax
:
1702 case SpvOpAtomicAnd
:
1704 case SpvOpAtomicXor
:
1705 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1709 unreachable("Invalid SPIR-V atomic");
1714 vtn_handle_ssbo_or_shared_atomic(struct vtn_builder
*b
, SpvOp opcode
,
1715 const uint32_t *w
, unsigned count
)
1717 struct vtn_access_chain
*chain
=
1718 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1719 nir_intrinsic_instr
*atomic
;
1722 SpvScope scope = w[4];
1723 SpvMemorySemanticsMask semantics = w[5];
1726 if (chain
->var
->mode
== vtn_variable_mode_workgroup
) {
1727 nir_deref
*deref
= &vtn_access_chain_to_deref(b
, chain
)->deref
;
1728 nir_intrinsic_op op
= get_shared_nir_atomic_op(opcode
);
1729 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
1730 atomic
->variables
[0] = nir_deref_as_var(nir_copy_deref(atomic
, deref
));
1731 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[0]);
1733 assert(chain
->var
->mode
== vtn_variable_mode_ssbo
);
1734 struct vtn_type
*type
;
1735 nir_ssa_def
*offset
, *index
;
1736 offset
= vtn_access_chain_to_offset(b
, chain
, &index
, &type
, NULL
, false);
1738 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(opcode
);
1740 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
1741 atomic
->src
[0] = nir_src_for_ssa(index
);
1742 atomic
->src
[1] = nir_src_for_ssa(offset
);
1743 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[2]);
1746 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
, 1, 32, NULL
);
1748 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1749 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1750 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
1751 val
->ssa
->def
= &atomic
->dest
.ssa
;
1752 val
->ssa
->type
= type
->type
;
1754 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
1757 static nir_alu_instr
*
1758 create_vec(nir_shader
*shader
, unsigned num_components
, unsigned bit_size
)
1761 switch (num_components
) {
1762 case 1: op
= nir_op_fmov
; break;
1763 case 2: op
= nir_op_vec2
; break;
1764 case 3: op
= nir_op_vec3
; break;
1765 case 4: op
= nir_op_vec4
; break;
1766 default: unreachable("bad vector size");
1769 nir_alu_instr
*vec
= nir_alu_instr_create(shader
, op
);
1770 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
1772 vec
->dest
.write_mask
= (1 << num_components
) - 1;
1777 struct vtn_ssa_value
*
1778 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
1780 if (src
->transposed
)
1781 return src
->transposed
;
1783 struct vtn_ssa_value
*dest
=
1784 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
1786 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
1787 nir_alu_instr
*vec
= create_vec(b
->shader
,
1788 glsl_get_matrix_columns(src
->type
),
1789 glsl_get_bit_size(src
->type
));
1790 if (glsl_type_is_vector_or_scalar(src
->type
)) {
1791 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
1792 vec
->src
[0].swizzle
[0] = i
;
1794 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
1795 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
1796 vec
->src
[j
].swizzle
[0] = i
;
1799 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1800 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
1803 dest
->transposed
= src
;
1809 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
1811 unsigned swiz
[4] = { index
};
1812 return nir_swizzle(&b
->nb
, src
, swiz
, 1, true);
1816 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
1819 nir_alu_instr
*vec
= create_vec(b
->shader
, src
->num_components
,
1822 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
1824 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
1826 vec
->src
[i
].src
= nir_src_for_ssa(src
);
1827 vec
->src
[i
].swizzle
[0] = i
;
1831 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1833 return &vec
->dest
.dest
.ssa
;
1837 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
1840 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
1841 for (unsigned i
= 1; i
< src
->num_components
; i
++)
1842 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
1843 vtn_vector_extract(b
, src
, i
), dest
);
1849 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
1850 nir_ssa_def
*insert
, nir_ssa_def
*index
)
1852 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
1853 for (unsigned i
= 1; i
< src
->num_components
; i
++)
1854 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
1855 vtn_vector_insert(b
, src
, insert
, i
), dest
);
1860 static nir_ssa_def
*
1861 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
1862 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
1863 const uint32_t *indices
)
1865 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
, src0
->bit_size
);
1867 for (unsigned i
= 0; i
< num_components
; i
++) {
1868 uint32_t index
= indices
[i
];
1869 if (index
== 0xffffffff) {
1871 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
1872 } else if (index
< src0
->num_components
) {
1873 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
1874 vec
->src
[i
].swizzle
[0] = index
;
1876 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
1877 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
1881 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1883 return &vec
->dest
.dest
.ssa
;
1887 * Concatentates a number of vectors/scalars together to produce a vector
1889 static nir_ssa_def
*
1890 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
1891 unsigned num_srcs
, nir_ssa_def
**srcs
)
1893 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
,
1896 unsigned dest_idx
= 0;
1897 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1898 nir_ssa_def
*src
= srcs
[i
];
1899 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
1900 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
1901 vec
->src
[dest_idx
].swizzle
[0] = j
;
1906 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1908 return &vec
->dest
.dest
.ssa
;
1911 static struct vtn_ssa_value
*
1912 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
1914 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
1915 dest
->type
= src
->type
;
1917 if (glsl_type_is_vector_or_scalar(src
->type
)) {
1918 dest
->def
= src
->def
;
1920 unsigned elems
= glsl_get_length(src
->type
);
1922 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
1923 for (unsigned i
= 0; i
< elems
; i
++)
1924 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
1930 static struct vtn_ssa_value
*
1931 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
1932 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
1933 unsigned num_indices
)
1935 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
1937 struct vtn_ssa_value
*cur
= dest
;
1939 for (i
= 0; i
< num_indices
- 1; i
++) {
1940 cur
= cur
->elems
[indices
[i
]];
1943 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
1944 /* According to the SPIR-V spec, OpCompositeInsert may work down to
1945 * the component granularity. In that case, the last index will be
1946 * the index to insert the scalar into the vector.
1949 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
1951 cur
->elems
[indices
[i
]] = insert
;
1957 static struct vtn_ssa_value
*
1958 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
1959 const uint32_t *indices
, unsigned num_indices
)
1961 struct vtn_ssa_value
*cur
= src
;
1962 for (unsigned i
= 0; i
< num_indices
; i
++) {
1963 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
1964 assert(i
== num_indices
- 1);
1965 /* According to the SPIR-V spec, OpCompositeExtract may work down to
1966 * the component granularity. The last index will be the index of the
1967 * vector to extract.
1970 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
1971 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
1972 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
1975 cur
= cur
->elems
[indices
[i
]];
1983 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
1984 const uint32_t *w
, unsigned count
)
1986 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1987 const struct glsl_type
*type
=
1988 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
1989 val
->ssa
= vtn_create_ssa_value(b
, type
);
1992 case SpvOpVectorExtractDynamic
:
1993 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
1994 vtn_ssa_value(b
, w
[4])->def
);
1997 case SpvOpVectorInsertDynamic
:
1998 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
1999 vtn_ssa_value(b
, w
[4])->def
,
2000 vtn_ssa_value(b
, w
[5])->def
);
2003 case SpvOpVectorShuffle
:
2004 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
2005 vtn_ssa_value(b
, w
[3])->def
,
2006 vtn_ssa_value(b
, w
[4])->def
,
2010 case SpvOpCompositeConstruct
: {
2011 unsigned elems
= count
- 3;
2012 if (glsl_type_is_vector_or_scalar(type
)) {
2013 nir_ssa_def
*srcs
[4];
2014 for (unsigned i
= 0; i
< elems
; i
++)
2015 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
2017 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
2020 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2021 for (unsigned i
= 0; i
< elems
; i
++)
2022 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
2026 case SpvOpCompositeExtract
:
2027 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
2031 case SpvOpCompositeInsert
:
2032 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
2033 vtn_ssa_value(b
, w
[3]),
2037 case SpvOpCopyObject
:
2038 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
2042 unreachable("unknown composite operation");
2047 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
2048 const uint32_t *w
, unsigned count
)
2050 nir_intrinsic_op intrinsic_op
;
2052 case SpvOpEmitVertex
:
2053 case SpvOpEmitStreamVertex
:
2054 intrinsic_op
= nir_intrinsic_emit_vertex
;
2056 case SpvOpEndPrimitive
:
2057 case SpvOpEndStreamPrimitive
:
2058 intrinsic_op
= nir_intrinsic_end_primitive
;
2060 case SpvOpMemoryBarrier
:
2061 intrinsic_op
= nir_intrinsic_memory_barrier
;
2063 case SpvOpControlBarrier
:
2064 intrinsic_op
= nir_intrinsic_barrier
;
2067 unreachable("unknown barrier instruction");
2070 nir_intrinsic_instr
*intrin
=
2071 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
2073 if (opcode
== SpvOpEmitStreamVertex
|| opcode
== SpvOpEndStreamPrimitive
)
2074 nir_intrinsic_set_stream_id(intrin
, w
[1]);
2076 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2080 gl_primitive_from_spv_execution_mode(SpvExecutionMode mode
)
2083 case SpvExecutionModeInputPoints
:
2084 case SpvExecutionModeOutputPoints
:
2085 return 0; /* GL_POINTS */
2086 case SpvExecutionModeInputLines
:
2087 return 1; /* GL_LINES */
2088 case SpvExecutionModeInputLinesAdjacency
:
2089 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
2090 case SpvExecutionModeTriangles
:
2091 return 4; /* GL_TRIANGLES */
2092 case SpvExecutionModeInputTrianglesAdjacency
:
2093 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
2094 case SpvExecutionModeQuads
:
2095 return 7; /* GL_QUADS */
2096 case SpvExecutionModeIsolines
:
2097 return 0x8E7A; /* GL_ISOLINES */
2098 case SpvExecutionModeOutputLineStrip
:
2099 return 3; /* GL_LINE_STRIP */
2100 case SpvExecutionModeOutputTriangleStrip
:
2101 return 5; /* GL_TRIANGLE_STRIP */
2103 assert(!"Invalid primitive type");
2109 vertices_in_from_spv_execution_mode(SpvExecutionMode mode
)
2112 case SpvExecutionModeInputPoints
:
2114 case SpvExecutionModeInputLines
:
2116 case SpvExecutionModeInputLinesAdjacency
:
2118 case SpvExecutionModeTriangles
:
2120 case SpvExecutionModeInputTrianglesAdjacency
:
2123 assert(!"Invalid GS input mode");
2128 static gl_shader_stage
2129 stage_for_execution_model(SpvExecutionModel model
)
2132 case SpvExecutionModelVertex
:
2133 return MESA_SHADER_VERTEX
;
2134 case SpvExecutionModelTessellationControl
:
2135 return MESA_SHADER_TESS_CTRL
;
2136 case SpvExecutionModelTessellationEvaluation
:
2137 return MESA_SHADER_TESS_EVAL
;
2138 case SpvExecutionModelGeometry
:
2139 return MESA_SHADER_GEOMETRY
;
2140 case SpvExecutionModelFragment
:
2141 return MESA_SHADER_FRAGMENT
;
2142 case SpvExecutionModelGLCompute
:
2143 return MESA_SHADER_COMPUTE
;
2145 unreachable("Unsupported execution model");
2150 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2151 const uint32_t *w
, unsigned count
)
2155 case SpvOpSourceExtension
:
2156 case SpvOpSourceContinued
:
2157 case SpvOpExtension
:
2158 /* Unhandled, but these are for debug so that's ok. */
2161 case SpvOpCapability
: {
2162 SpvCapability cap
= w
[1];
2164 case SpvCapabilityMatrix
:
2165 case SpvCapabilityShader
:
2166 case SpvCapabilityGeometry
:
2167 case SpvCapabilityTessellationPointSize
:
2168 case SpvCapabilityGeometryPointSize
:
2169 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
2170 case SpvCapabilitySampledImageArrayDynamicIndexing
:
2171 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
2172 case SpvCapabilityStorageImageArrayDynamicIndexing
:
2173 case SpvCapabilityImageRect
:
2174 case SpvCapabilitySampledRect
:
2175 case SpvCapabilitySampled1D
:
2176 case SpvCapabilityImage1D
:
2177 case SpvCapabilitySampledCubeArray
:
2178 case SpvCapabilitySampledBuffer
:
2179 case SpvCapabilityImageBuffer
:
2180 case SpvCapabilityImageQuery
:
2182 case SpvCapabilityClipDistance
:
2183 case SpvCapabilityCullDistance
:
2184 case SpvCapabilityGeometryStreams
:
2185 fprintf(stderr
, "WARNING: Unsupported SPIR-V Capability\n");
2188 assert(!"Unsupported capability");
2193 case SpvOpExtInstImport
:
2194 vtn_handle_extension(b
, opcode
, w
, count
);
2197 case SpvOpMemoryModel
:
2198 assert(w
[1] == SpvAddressingModelLogical
);
2199 assert(w
[2] == SpvMemoryModelGLSL450
);
2202 case SpvOpEntryPoint
: {
2203 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
2204 /* Let this be a name label regardless */
2205 unsigned name_words
;
2206 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
2208 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
2209 stage_for_execution_model(w
[1]) != b
->entry_point_stage
)
2212 assert(b
->entry_point
== NULL
);
2213 b
->entry_point
= entry_point
;
2218 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
2219 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2223 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2226 case SpvOpMemberName
:
2230 case SpvOpExecutionMode
:
2231 case SpvOpDecorationGroup
:
2233 case SpvOpMemberDecorate
:
2234 case SpvOpGroupDecorate
:
2235 case SpvOpGroupMemberDecorate
:
2236 vtn_handle_decoration(b
, opcode
, w
, count
);
2240 return false; /* End of preamble */
2247 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
2248 const struct vtn_decoration
*mode
, void *data
)
2250 assert(b
->entry_point
== entry_point
);
2252 switch(mode
->exec_mode
) {
2253 case SpvExecutionModeOriginUpperLeft
:
2254 case SpvExecutionModeOriginLowerLeft
:
2255 b
->origin_upper_left
=
2256 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
2259 case SpvExecutionModeEarlyFragmentTests
:
2260 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2261 b
->shader
->info
.fs
.early_fragment_tests
= true;
2264 case SpvExecutionModeInvocations
:
2265 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2266 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->literals
[0]);
2269 case SpvExecutionModeDepthReplacing
:
2270 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2271 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
2273 case SpvExecutionModeDepthGreater
:
2274 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2275 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
2277 case SpvExecutionModeDepthLess
:
2278 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2279 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
2281 case SpvExecutionModeDepthUnchanged
:
2282 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2283 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
2286 case SpvExecutionModeLocalSize
:
2287 assert(b
->shader
->stage
== MESA_SHADER_COMPUTE
);
2288 b
->shader
->info
.cs
.local_size
[0] = mode
->literals
[0];
2289 b
->shader
->info
.cs
.local_size
[1] = mode
->literals
[1];
2290 b
->shader
->info
.cs
.local_size
[2] = mode
->literals
[2];
2292 case SpvExecutionModeLocalSizeHint
:
2293 break; /* Nothing do do with this */
2295 case SpvExecutionModeOutputVertices
:
2296 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2297 b
->shader
->info
.gs
.vertices_out
= mode
->literals
[0];
2300 case SpvExecutionModeInputPoints
:
2301 case SpvExecutionModeInputLines
:
2302 case SpvExecutionModeInputLinesAdjacency
:
2303 case SpvExecutionModeTriangles
:
2304 case SpvExecutionModeInputTrianglesAdjacency
:
2305 case SpvExecutionModeQuads
:
2306 case SpvExecutionModeIsolines
:
2307 if (b
->shader
->stage
== MESA_SHADER_GEOMETRY
) {
2308 b
->shader
->info
.gs
.vertices_in
=
2309 vertices_in_from_spv_execution_mode(mode
->exec_mode
);
2311 assert(!"Tesselation shaders not yet supported");
2315 case SpvExecutionModeOutputPoints
:
2316 case SpvExecutionModeOutputLineStrip
:
2317 case SpvExecutionModeOutputTriangleStrip
:
2318 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2319 b
->shader
->info
.gs
.output_primitive
=
2320 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
2323 case SpvExecutionModeSpacingEqual
:
2324 case SpvExecutionModeSpacingFractionalEven
:
2325 case SpvExecutionModeSpacingFractionalOdd
:
2326 case SpvExecutionModeVertexOrderCw
:
2327 case SpvExecutionModeVertexOrderCcw
:
2328 case SpvExecutionModePointMode
:
2329 assert(!"TODO: Add tessellation metadata");
2332 case SpvExecutionModePixelCenterInteger
:
2333 case SpvExecutionModeXfb
:
2334 assert(!"Unhandled execution mode");
2337 case SpvExecutionModeVecTypeHint
:
2338 case SpvExecutionModeContractionOff
:
2344 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2345 const uint32_t *w
, unsigned count
)
2349 case SpvOpSourceContinued
:
2350 case SpvOpSourceExtension
:
2351 case SpvOpExtension
:
2352 case SpvOpCapability
:
2353 case SpvOpExtInstImport
:
2354 case SpvOpMemoryModel
:
2355 case SpvOpEntryPoint
:
2356 case SpvOpExecutionMode
:
2359 case SpvOpMemberName
:
2360 case SpvOpDecorationGroup
:
2362 case SpvOpMemberDecorate
:
2363 case SpvOpGroupDecorate
:
2364 case SpvOpGroupMemberDecorate
:
2365 assert(!"Invalid opcode types and variables section");
2371 case SpvOpTypeFloat
:
2372 case SpvOpTypeVector
:
2373 case SpvOpTypeMatrix
:
2374 case SpvOpTypeImage
:
2375 case SpvOpTypeSampler
:
2376 case SpvOpTypeSampledImage
:
2377 case SpvOpTypeArray
:
2378 case SpvOpTypeRuntimeArray
:
2379 case SpvOpTypeStruct
:
2380 case SpvOpTypeOpaque
:
2381 case SpvOpTypePointer
:
2382 case SpvOpTypeFunction
:
2383 case SpvOpTypeEvent
:
2384 case SpvOpTypeDeviceEvent
:
2385 case SpvOpTypeReserveId
:
2386 case SpvOpTypeQueue
:
2388 vtn_handle_type(b
, opcode
, w
, count
);
2391 case SpvOpConstantTrue
:
2392 case SpvOpConstantFalse
:
2394 case SpvOpConstantComposite
:
2395 case SpvOpConstantSampler
:
2396 case SpvOpConstantNull
:
2397 case SpvOpSpecConstantTrue
:
2398 case SpvOpSpecConstantFalse
:
2399 case SpvOpSpecConstant
:
2400 case SpvOpSpecConstantComposite
:
2401 case SpvOpSpecConstantOp
:
2402 vtn_handle_constant(b
, opcode
, w
, count
);
2406 vtn_handle_variables(b
, opcode
, w
, count
);
2410 return false; /* End of preamble */
2417 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2418 const uint32_t *w
, unsigned count
)
2424 case SpvOpLoopMerge
:
2425 case SpvOpSelectionMerge
:
2426 /* This is handled by cfg pre-pass and walk_blocks */
2430 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
2431 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2436 vtn_handle_extension(b
, opcode
, w
, count
);
2442 case SpvOpCopyMemory
:
2443 case SpvOpCopyMemorySized
:
2444 case SpvOpAccessChain
:
2445 case SpvOpInBoundsAccessChain
:
2446 case SpvOpArrayLength
:
2447 vtn_handle_variables(b
, opcode
, w
, count
);
2450 case SpvOpFunctionCall
:
2451 vtn_handle_function_call(b
, opcode
, w
, count
);
2454 case SpvOpSampledImage
:
2456 case SpvOpImageSampleImplicitLod
:
2457 case SpvOpImageSampleExplicitLod
:
2458 case SpvOpImageSampleDrefImplicitLod
:
2459 case SpvOpImageSampleDrefExplicitLod
:
2460 case SpvOpImageSampleProjImplicitLod
:
2461 case SpvOpImageSampleProjExplicitLod
:
2462 case SpvOpImageSampleProjDrefImplicitLod
:
2463 case SpvOpImageSampleProjDrefExplicitLod
:
2464 case SpvOpImageFetch
:
2465 case SpvOpImageGather
:
2466 case SpvOpImageDrefGather
:
2467 case SpvOpImageQuerySizeLod
:
2468 case SpvOpImageQueryLod
:
2469 case SpvOpImageQueryLevels
:
2470 case SpvOpImageQuerySamples
:
2471 vtn_handle_texture(b
, opcode
, w
, count
);
2474 case SpvOpImageRead
:
2475 case SpvOpImageWrite
:
2476 case SpvOpImageTexelPointer
:
2477 vtn_handle_image(b
, opcode
, w
, count
);
2480 case SpvOpImageQuerySize
: {
2481 struct vtn_access_chain
*image
=
2482 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
2483 if (glsl_type_is_image(image
->var
->var
->interface_type
)) {
2484 vtn_handle_image(b
, opcode
, w
, count
);
2486 vtn_handle_texture(b
, opcode
, w
, count
);
2491 case SpvOpAtomicExchange
:
2492 case SpvOpAtomicCompareExchange
:
2493 case SpvOpAtomicCompareExchangeWeak
:
2494 case SpvOpAtomicIIncrement
:
2495 case SpvOpAtomicIDecrement
:
2496 case SpvOpAtomicIAdd
:
2497 case SpvOpAtomicISub
:
2498 case SpvOpAtomicSMin
:
2499 case SpvOpAtomicUMin
:
2500 case SpvOpAtomicSMax
:
2501 case SpvOpAtomicUMax
:
2502 case SpvOpAtomicAnd
:
2504 case SpvOpAtomicXor
: {
2505 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
2506 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
2507 vtn_handle_image(b
, opcode
, w
, count
);
2509 assert(pointer
->value_type
== vtn_value_type_access_chain
);
2510 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
2520 case SpvOpConvertFToU
:
2521 case SpvOpConvertFToS
:
2522 case SpvOpConvertSToF
:
2523 case SpvOpConvertUToF
:
2527 case SpvOpQuantizeToF16
:
2528 case SpvOpConvertPtrToU
:
2529 case SpvOpConvertUToPtr
:
2530 case SpvOpPtrCastToGeneric
:
2531 case SpvOpGenericCastToPtr
:
2537 case SpvOpSignBitSet
:
2538 case SpvOpLessOrGreater
:
2540 case SpvOpUnordered
:
2555 case SpvOpVectorTimesScalar
:
2557 case SpvOpIAddCarry
:
2558 case SpvOpISubBorrow
:
2559 case SpvOpUMulExtended
:
2560 case SpvOpSMulExtended
:
2561 case SpvOpShiftRightLogical
:
2562 case SpvOpShiftRightArithmetic
:
2563 case SpvOpShiftLeftLogical
:
2564 case SpvOpLogicalEqual
:
2565 case SpvOpLogicalNotEqual
:
2566 case SpvOpLogicalOr
:
2567 case SpvOpLogicalAnd
:
2568 case SpvOpLogicalNot
:
2569 case SpvOpBitwiseOr
:
2570 case SpvOpBitwiseXor
:
2571 case SpvOpBitwiseAnd
:
2574 case SpvOpFOrdEqual
:
2575 case SpvOpFUnordEqual
:
2576 case SpvOpINotEqual
:
2577 case SpvOpFOrdNotEqual
:
2578 case SpvOpFUnordNotEqual
:
2579 case SpvOpULessThan
:
2580 case SpvOpSLessThan
:
2581 case SpvOpFOrdLessThan
:
2582 case SpvOpFUnordLessThan
:
2583 case SpvOpUGreaterThan
:
2584 case SpvOpSGreaterThan
:
2585 case SpvOpFOrdGreaterThan
:
2586 case SpvOpFUnordGreaterThan
:
2587 case SpvOpULessThanEqual
:
2588 case SpvOpSLessThanEqual
:
2589 case SpvOpFOrdLessThanEqual
:
2590 case SpvOpFUnordLessThanEqual
:
2591 case SpvOpUGreaterThanEqual
:
2592 case SpvOpSGreaterThanEqual
:
2593 case SpvOpFOrdGreaterThanEqual
:
2594 case SpvOpFUnordGreaterThanEqual
:
2600 case SpvOpFwidthFine
:
2601 case SpvOpDPdxCoarse
:
2602 case SpvOpDPdyCoarse
:
2603 case SpvOpFwidthCoarse
:
2604 case SpvOpBitFieldInsert
:
2605 case SpvOpBitFieldSExtract
:
2606 case SpvOpBitFieldUExtract
:
2607 case SpvOpBitReverse
:
2609 case SpvOpTranspose
:
2610 case SpvOpOuterProduct
:
2611 case SpvOpMatrixTimesScalar
:
2612 case SpvOpVectorTimesMatrix
:
2613 case SpvOpMatrixTimesVector
:
2614 case SpvOpMatrixTimesMatrix
:
2615 vtn_handle_alu(b
, opcode
, w
, count
);
2618 case SpvOpVectorExtractDynamic
:
2619 case SpvOpVectorInsertDynamic
:
2620 case SpvOpVectorShuffle
:
2621 case SpvOpCompositeConstruct
:
2622 case SpvOpCompositeExtract
:
2623 case SpvOpCompositeInsert
:
2624 case SpvOpCopyObject
:
2625 vtn_handle_composite(b
, opcode
, w
, count
);
2628 case SpvOpEmitVertex
:
2629 case SpvOpEndPrimitive
:
2630 case SpvOpEmitStreamVertex
:
2631 case SpvOpEndStreamPrimitive
:
2632 case SpvOpControlBarrier
:
2633 case SpvOpMemoryBarrier
:
2634 vtn_handle_barrier(b
, opcode
, w
, count
);
2638 unreachable("Unhandled opcode");
2645 spirv_to_nir(const uint32_t *words
, size_t word_count
,
2646 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
2647 gl_shader_stage stage
, const char *entry_point_name
,
2648 const nir_shader_compiler_options
*options
)
2650 const uint32_t *word_end
= words
+ word_count
;
2652 /* Handle the SPIR-V header (first 4 dwords) */
2653 assert(word_count
> 5);
2655 assert(words
[0] == SpvMagicNumber
);
2656 assert(words
[1] >= 0x10000);
2657 /* words[2] == generator magic */
2658 unsigned value_id_bound
= words
[3];
2659 assert(words
[4] == 0);
2663 /* Initialize the stn_builder object */
2664 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
2665 b
->value_id_bound
= value_id_bound
;
2666 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
2667 exec_list_make_empty(&b
->functions
);
2668 b
->entry_point_stage
= stage
;
2669 b
->entry_point_name
= entry_point_name
;
2671 /* Handle all the preamble instructions */
2672 words
= vtn_foreach_instruction(b
, words
, word_end
,
2673 vtn_handle_preamble_instruction
);
2675 if (b
->entry_point
== NULL
) {
2676 assert(!"Entry point not found");
2681 b
->shader
= nir_shader_create(NULL
, stage
, options
);
2683 /* Set shader info defaults */
2684 b
->shader
->info
.gs
.invocations
= 1;
2686 /* Parse execution modes */
2687 vtn_foreach_execution_mode(b
, b
->entry_point
,
2688 vtn_handle_execution_mode
, NULL
);
2690 b
->specializations
= spec
;
2691 b
->num_specializations
= num_spec
;
2693 /* Handle all variable, type, and constant instructions */
2694 words
= vtn_foreach_instruction(b
, words
, word_end
,
2695 vtn_handle_variable_or_type_instruction
);
2697 vtn_build_cfg(b
, words
, word_end
);
2699 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
2700 b
->impl
= func
->impl
;
2701 b
->const_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
2702 _mesa_key_pointer_equal
);
2704 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
2707 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
2708 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
2709 assert(entry_point
);