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 nir_ssa_undef_instr
*undef
=
42 nir_ssa_undef_instr_create(b
->shader
, num_components
);
44 nir_instr_insert_before_cf_list(&b
->impl
->body
, &undef
->instr
);
45 val
->def
= &undef
->def
;
47 unsigned elems
= glsl_get_length(val
->type
);
48 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
49 if (glsl_type_is_matrix(type
)) {
50 const struct glsl_type
*elem_type
=
51 glsl_vector_type(glsl_get_base_type(type
),
52 glsl_get_vector_elements(type
));
54 for (unsigned i
= 0; i
< elems
; i
++)
55 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
56 } else if (glsl_type_is_array(type
)) {
57 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
58 for (unsigned i
= 0; i
< elems
; i
++)
59 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
61 for (unsigned i
= 0; i
< elems
; i
++) {
62 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
63 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
71 static struct vtn_ssa_value
*
72 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
73 const struct glsl_type
*type
)
75 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
80 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
83 switch (glsl_get_base_type(type
)) {
88 case GLSL_TYPE_DOUBLE
:
89 if (glsl_type_is_vector_or_scalar(type
)) {
90 unsigned num_components
= glsl_get_vector_elements(val
->type
);
91 nir_load_const_instr
*load
=
92 nir_load_const_instr_create(b
->shader
, num_components
);
94 for (unsigned i
= 0; i
< num_components
; i
++)
95 load
->value
.u
[i
] = constant
->value
.u
[i
];
97 nir_instr_insert_before_cf_list(&b
->impl
->body
, &load
->instr
);
98 val
->def
= &load
->def
;
100 assert(glsl_type_is_matrix(type
));
101 unsigned rows
= glsl_get_vector_elements(val
->type
);
102 unsigned columns
= glsl_get_matrix_columns(val
->type
);
103 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, columns
);
105 for (unsigned i
= 0; i
< columns
; i
++) {
106 struct vtn_ssa_value
*col_val
= rzalloc(b
, struct vtn_ssa_value
);
107 col_val
->type
= glsl_get_column_type(val
->type
);
108 nir_load_const_instr
*load
=
109 nir_load_const_instr_create(b
->shader
, rows
);
111 for (unsigned j
= 0; j
< rows
; j
++)
112 load
->value
.u
[j
] = constant
->value
.u
[rows
* i
+ j
];
114 nir_instr_insert_before_cf_list(&b
->impl
->body
, &load
->instr
);
115 col_val
->def
= &load
->def
;
117 val
->elems
[i
] = col_val
;
122 case GLSL_TYPE_ARRAY
: {
123 unsigned elems
= glsl_get_length(val
->type
);
124 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
125 const struct glsl_type
*elem_type
= glsl_get_array_element(val
->type
);
126 for (unsigned i
= 0; i
< elems
; i
++)
127 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
132 case GLSL_TYPE_STRUCT
: {
133 unsigned elems
= glsl_get_length(val
->type
);
134 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
135 for (unsigned i
= 0; i
< elems
; i
++) {
136 const struct glsl_type
*elem_type
=
137 glsl_get_struct_field(val
->type
, i
);
138 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
145 unreachable("bad constant type");
151 struct vtn_ssa_value
*
152 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
154 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
155 switch (val
->value_type
) {
156 case vtn_value_type_undef
:
157 return vtn_undef_ssa_value(b
, val
->type
->type
);
159 case vtn_value_type_constant
:
160 return vtn_const_ssa_value(b
, val
->constant
, val
->const_type
);
162 case vtn_value_type_ssa
:
165 case vtn_value_type_access_chain
:
166 /* This is needed for function parameters */
167 return vtn_variable_load(b
, val
->access_chain
);
170 unreachable("Invalid type for an SSA value");
175 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
176 unsigned word_count
, unsigned *words_used
)
178 char *dup
= ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
180 /* Ammount of space taken by the string (including the null) */
181 unsigned len
= strlen(dup
) + 1;
182 *words_used
= DIV_ROUND_UP(len
, sizeof(*words
));
188 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
189 const uint32_t *end
, vtn_instruction_handler handler
)
195 const uint32_t *w
= start
;
197 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
198 unsigned count
= w
[0] >> SpvWordCountShift
;
199 assert(count
>= 1 && w
+ count
<= end
);
203 break; /* Do nothing */
206 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
218 if (!handler(b
, opcode
, w
, count
))
230 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
231 const uint32_t *w
, unsigned count
)
234 case SpvOpExtInstImport
: {
235 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
236 if (strcmp((const char *)&w
[2], "GLSL.std.450") == 0) {
237 val
->ext_handler
= vtn_handle_glsl450_instruction
;
239 assert(!"Unsupported extension");
245 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
246 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
253 unreachable("Unhandled opcode");
258 _foreach_decoration_helper(struct vtn_builder
*b
,
259 struct vtn_value
*base_value
,
261 struct vtn_value
*value
,
262 vtn_decoration_foreach_cb cb
, void *data
)
264 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
266 if (dec
->scope
== VTN_DEC_DECORATION
) {
267 member
= parent_member
;
268 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
269 assert(parent_member
== -1);
270 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
272 /* Not a decoration */
277 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
278 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
281 cb(b
, base_value
, member
, dec
, data
);
286 /** Iterates (recursively if needed) over all of the decorations on a value
288 * This function iterates over all of the decorations applied to a given
289 * value. If it encounters a decoration group, it recurses into the group
290 * and iterates over all of those decorations as well.
293 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
294 vtn_decoration_foreach_cb cb
, void *data
)
296 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
300 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
301 vtn_execution_mode_foreach_cb cb
, void *data
)
303 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
304 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
307 assert(dec
->group
== NULL
);
308 cb(b
, value
, dec
, data
);
313 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
314 const uint32_t *w
, unsigned count
)
316 const uint32_t *w_end
= w
+ count
;
317 const uint32_t target
= w
[1];
321 case SpvOpDecorationGroup
:
322 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
326 case SpvOpMemberDecorate
:
327 case SpvOpExecutionMode
: {
328 struct vtn_value
*val
= &b
->values
[target
];
330 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
333 dec
->scope
= VTN_DEC_DECORATION
;
335 case SpvOpMemberDecorate
:
336 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
338 case SpvOpExecutionMode
:
339 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
342 unreachable("Invalid decoration opcode");
344 dec
->decoration
= *(w
++);
347 /* Link into the list */
348 dec
->next
= val
->decoration
;
349 val
->decoration
= dec
;
353 case SpvOpGroupMemberDecorate
:
354 case SpvOpGroupDecorate
: {
355 struct vtn_value
*group
=
356 vtn_value(b
, target
, vtn_value_type_decoration_group
);
358 for (; w
< w_end
; w
++) {
359 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
360 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
363 if (opcode
== SpvOpGroupDecorate
) {
364 dec
->scope
= VTN_DEC_DECORATION
;
366 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
369 /* Link into the list */
370 dec
->next
= val
->decoration
;
371 val
->decoration
= dec
;
377 unreachable("Unhandled opcode");
381 struct member_decoration_ctx
{
382 struct glsl_struct_field
*fields
;
383 struct vtn_type
*type
;
386 /* does a shallow copy of a vtn_type */
388 static struct vtn_type
*
389 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
391 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
392 dest
->type
= src
->type
;
393 dest
->is_builtin
= src
->is_builtin
;
395 dest
->builtin
= src
->builtin
;
397 if (!glsl_type_is_scalar(src
->type
)) {
398 switch (glsl_get_base_type(src
->type
)) {
402 case GLSL_TYPE_FLOAT
:
403 case GLSL_TYPE_DOUBLE
:
404 case GLSL_TYPE_ARRAY
:
405 dest
->row_major
= src
->row_major
;
406 dest
->stride
= src
->stride
;
407 dest
->array_element
= src
->array_element
;
410 case GLSL_TYPE_STRUCT
: {
411 unsigned elems
= glsl_get_length(src
->type
);
413 dest
->members
= ralloc_array(b
, struct vtn_type
*, elems
);
414 memcpy(dest
->members
, src
->members
, elems
* sizeof(struct vtn_type
*));
416 dest
->offsets
= ralloc_array(b
, unsigned, elems
);
417 memcpy(dest
->offsets
, src
->offsets
, elems
* sizeof(unsigned));
422 unreachable("unhandled type");
429 static struct vtn_type
*
430 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
432 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
433 type
= type
->members
[member
];
435 /* We may have an array of matrices.... Oh, joy! */
436 while (glsl_type_is_array(type
->type
)) {
437 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
438 type
= type
->array_element
;
441 assert(glsl_type_is_matrix(type
->type
));
447 struct_member_decoration_cb(struct vtn_builder
*b
,
448 struct vtn_value
*val
, int member
,
449 const struct vtn_decoration
*dec
, void *void_ctx
)
451 struct member_decoration_ctx
*ctx
= void_ctx
;
456 switch (dec
->decoration
) {
457 case SpvDecorationRelaxedPrecision
:
458 break; /* FIXME: Do nothing with this for now. */
459 case SpvDecorationNoPerspective
:
460 ctx
->fields
[member
].interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
462 case SpvDecorationFlat
:
463 ctx
->fields
[member
].interpolation
= INTERP_QUALIFIER_FLAT
;
465 case SpvDecorationCentroid
:
466 ctx
->fields
[member
].centroid
= true;
468 case SpvDecorationSample
:
469 ctx
->fields
[member
].sample
= true;
471 case SpvDecorationLocation
:
472 ctx
->fields
[member
].location
= dec
->literals
[0];
474 case SpvDecorationBuiltIn
:
475 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
476 ctx
->type
->members
[member
]->is_builtin
= true;
477 ctx
->type
->members
[member
]->builtin
= dec
->literals
[0];
478 ctx
->type
->builtin_block
= true;
480 case SpvDecorationOffset
:
481 ctx
->type
->offsets
[member
] = dec
->literals
[0];
483 case SpvDecorationMatrixStride
:
484 mutable_matrix_member(b
, ctx
->type
, member
)->stride
= dec
->literals
[0];
486 case SpvDecorationColMajor
:
487 break; /* Nothing to do here. Column-major is the default. */
488 case SpvDecorationRowMajor
:
489 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
492 unreachable("Unhandled member decoration");
497 type_decoration_cb(struct vtn_builder
*b
,
498 struct vtn_value
*val
, int member
,
499 const struct vtn_decoration
*dec
, void *ctx
)
501 struct vtn_type
*type
= val
->type
;
506 switch (dec
->decoration
) {
507 case SpvDecorationArrayStride
:
508 type
->stride
= dec
->literals
[0];
510 case SpvDecorationBlock
:
513 case SpvDecorationBufferBlock
:
514 type
->buffer_block
= true;
516 case SpvDecorationGLSLShared
:
517 case SpvDecorationGLSLPacked
:
518 /* Ignore these, since we get explicit offsets anyways */
521 case SpvDecorationStream
:
522 assert(dec
->literals
[0] == 0);
526 unreachable("Unhandled type decoration");
531 translate_image_format(SpvImageFormat format
)
534 case SpvImageFormatUnknown
: return 0; /* GL_NONE */
535 case SpvImageFormatRgba32f
: return 0x8814; /* GL_RGBA32F */
536 case SpvImageFormatRgba16f
: return 0x881A; /* GL_RGBA16F */
537 case SpvImageFormatR32f
: return 0x822E; /* GL_R32F */
538 case SpvImageFormatRgba8
: return 0x8058; /* GL_RGBA8 */
539 case SpvImageFormatRgba8Snorm
: return 0x8F97; /* GL_RGBA8_SNORM */
540 case SpvImageFormatRg32f
: return 0x8230; /* GL_RG32F */
541 case SpvImageFormatRg16f
: return 0x822F; /* GL_RG16F */
542 case SpvImageFormatR11fG11fB10f
: return 0x8C3A; /* GL_R11F_G11F_B10F */
543 case SpvImageFormatR16f
: return 0x822D; /* GL_R16F */
544 case SpvImageFormatRgba16
: return 0x805B; /* GL_RGBA16 */
545 case SpvImageFormatRgb10A2
: return 0x8059; /* GL_RGB10_A2 */
546 case SpvImageFormatRg16
: return 0x822C; /* GL_RG16 */
547 case SpvImageFormatRg8
: return 0x822B; /* GL_RG8 */
548 case SpvImageFormatR16
: return 0x822A; /* GL_R16 */
549 case SpvImageFormatR8
: return 0x8229; /* GL_R8 */
550 case SpvImageFormatRgba16Snorm
: return 0x8F9B; /* GL_RGBA16_SNORM */
551 case SpvImageFormatRg16Snorm
: return 0x8F99; /* GL_RG16_SNORM */
552 case SpvImageFormatRg8Snorm
: return 0x8F95; /* GL_RG8_SNORM */
553 case SpvImageFormatR16Snorm
: return 0x8F98; /* GL_R16_SNORM */
554 case SpvImageFormatR8Snorm
: return 0x8F94; /* GL_R8_SNORM */
555 case SpvImageFormatRgba32i
: return 0x8D82; /* GL_RGBA32I */
556 case SpvImageFormatRgba16i
: return 0x8D88; /* GL_RGBA16I */
557 case SpvImageFormatRgba8i
: return 0x8D8E; /* GL_RGBA8I */
558 case SpvImageFormatR32i
: return 0x8235; /* GL_R32I */
559 case SpvImageFormatRg32i
: return 0x823B; /* GL_RG32I */
560 case SpvImageFormatRg16i
: return 0x8239; /* GL_RG16I */
561 case SpvImageFormatRg8i
: return 0x8237; /* GL_RG8I */
562 case SpvImageFormatR16i
: return 0x8233; /* GL_R16I */
563 case SpvImageFormatR8i
: return 0x8231; /* GL_R8I */
564 case SpvImageFormatRgba32ui
: return 0x8D70; /* GL_RGBA32UI */
565 case SpvImageFormatRgba16ui
: return 0x8D76; /* GL_RGBA16UI */
566 case SpvImageFormatRgba8ui
: return 0x8D7C; /* GL_RGBA8UI */
567 case SpvImageFormatR32ui
: return 0x8236; /* GL_R32UI */
568 case SpvImageFormatRgb10a2ui
: return 0x906F; /* GL_RGB10_A2UI */
569 case SpvImageFormatRg32ui
: return 0x823C; /* GL_RG32UI */
570 case SpvImageFormatRg16ui
: return 0x823A; /* GL_RG16UI */
571 case SpvImageFormatRg8ui
: return 0x8238; /* GL_RG8UI */
572 case SpvImageFormatR16ui
: return 0x823A; /* GL_RG16UI */
573 case SpvImageFormatR8ui
: return 0x8232; /* GL_R8UI */
575 assert(!"Invalid image format");
581 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
582 const uint32_t *w
, unsigned count
)
584 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
586 val
->type
= rzalloc(b
, struct vtn_type
);
587 val
->type
->is_builtin
= false;
588 val
->type
->val
= val
;
592 val
->type
->type
= glsl_void_type();
595 val
->type
->type
= glsl_bool_type();
598 const bool signedness
= w
[3];
599 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
603 val
->type
->type
= glsl_float_type();
606 case SpvOpTypeVector
: {
607 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
608 unsigned elems
= w
[3];
610 assert(glsl_type_is_scalar(base
->type
));
611 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
613 /* Vectors implicitly have sizeof(base_type) stride. For now, this
614 * is always 4 bytes. This will have to change if we want to start
615 * supporting doubles or half-floats.
617 val
->type
->stride
= 4;
618 val
->type
->array_element
= base
;
622 case SpvOpTypeMatrix
: {
623 struct vtn_type
*base
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
624 unsigned columns
= w
[3];
626 assert(glsl_type_is_vector(base
->type
));
627 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
628 glsl_get_vector_elements(base
->type
),
630 assert(!glsl_type_is_error(val
->type
->type
));
631 val
->type
->array_element
= base
;
632 val
->type
->row_major
= false;
633 val
->type
->stride
= 0;
637 case SpvOpTypeRuntimeArray
:
638 case SpvOpTypeArray
: {
639 struct vtn_type
*array_element
=
640 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
643 if (opcode
== SpvOpTypeRuntimeArray
) {
644 /* A length of 0 is used to denote unsized arrays */
648 vtn_value(b
, w
[3], vtn_value_type_constant
)->constant
->value
.u
[0];
651 val
->type
->type
= glsl_array_type(array_element
->type
, length
);
652 val
->type
->array_element
= array_element
;
653 val
->type
->stride
= 0;
657 case SpvOpTypeStruct
: {
658 unsigned num_fields
= count
- 2;
659 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
660 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
662 NIR_VLA(struct glsl_struct_field
, fields
, count
);
663 for (unsigned i
= 0; i
< num_fields
; i
++) {
664 val
->type
->members
[i
] =
665 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
666 fields
[i
] = (struct glsl_struct_field
) {
667 .type
= val
->type
->members
[i
]->type
,
668 .name
= ralloc_asprintf(b
, "field%d", i
),
673 struct member_decoration_ctx ctx
= {
678 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
680 const char *name
= val
->name
? val
->name
: "struct";
682 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
);
686 case SpvOpTypeFunction
: {
687 const struct glsl_type
*return_type
=
688 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
689 NIR_VLA(struct glsl_function_param
, params
, count
- 3);
690 for (unsigned i
= 0; i
< count
- 3; i
++) {
691 params
[i
].type
= vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
->type
;
695 params
[i
].out
= true;
697 val
->type
->type
= glsl_function_type(return_type
, params
, count
- 3);
701 case SpvOpTypePointer
:
702 /* FIXME: For now, we'll just do the really lame thing and return
703 * the same type. The validator should ensure that the proper number
704 * of dereferences happen
706 val
->type
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
709 case SpvOpTypeImage
: {
710 const struct glsl_type
*sampled_type
=
711 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
713 assert(glsl_type_is_vector_or_scalar(sampled_type
));
715 enum glsl_sampler_dim dim
;
716 switch ((SpvDim
)w
[3]) {
717 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
718 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
719 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
720 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
721 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
722 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
724 unreachable("Invalid SPIR-V Sampler dimension");
727 bool is_shadow
= w
[4];
728 bool is_array
= w
[5];
729 bool multisampled
= w
[6];
730 unsigned sampled
= w
[7];
731 SpvImageFormat format
= w
[8];
734 val
->type
->access_qualifier
= w
[9];
736 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
738 assert(!multisampled
&& "FIXME: Handl multi-sampled textures");
740 val
->type
->image_format
= translate_image_format(format
);
743 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
744 glsl_get_base_type(sampled_type
));
745 } else if (sampled
== 2) {
748 val
->type
->type
= glsl_image_type(dim
, is_array
,
749 glsl_get_base_type(sampled_type
));
751 assert(!"We need to know if the image will be sampled");
756 case SpvOpTypeSampledImage
:
757 val
->type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
760 case SpvOpTypeSampler
:
761 /* The actual sampler type here doesn't really matter. It gets
762 * thrown away the moment you combine it with an image. What really
763 * matters is that it's a sampler type as opposed to an integer type
764 * so the backend knows what to do.
766 * TODO: Eventually we should consider adding a "bare sampler" type
769 val
->type
->type
= glsl_sampler_type(GLSL_SAMPLER_DIM_2D
, false, false,
773 case SpvOpTypeOpaque
:
775 case SpvOpTypeDeviceEvent
:
776 case SpvOpTypeReserveId
:
780 unreachable("Unhandled opcode");
783 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
786 static nir_constant
*
787 vtn_null_constant(struct vtn_builder
*b
, const struct glsl_type
*type
)
789 nir_constant
*c
= rzalloc(b
, nir_constant
);
791 switch (glsl_get_base_type(type
)) {
795 case GLSL_TYPE_FLOAT
:
796 case GLSL_TYPE_DOUBLE
:
797 /* Nothing to do here. It's already initialized to zero */
800 case GLSL_TYPE_ARRAY
:
801 assert(glsl_get_length(type
) > 0);
802 c
->num_elements
= glsl_get_length(type
);
803 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
805 c
->elements
[0] = vtn_null_constant(b
, glsl_get_array_element(type
));
806 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
807 c
->elements
[i
] = c
->elements
[0];
810 case GLSL_TYPE_STRUCT
:
811 c
->num_elements
= glsl_get_length(type
);
812 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
814 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
815 c
->elements
[i
] = vtn_null_constant(b
, glsl_get_struct_field(type
, i
));
820 unreachable("Invalid type for null constant");
827 spec_constant_deocoration_cb(struct vtn_builder
*b
, struct vtn_value
*v
,
828 int member
, const struct vtn_decoration
*dec
,
831 assert(member
== -1);
832 if (dec
->decoration
!= SpvDecorationSpecId
)
835 uint32_t *const_value
= data
;
837 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
838 if (b
->specializations
[i
].id
== dec
->literals
[0]) {
839 *const_value
= b
->specializations
[i
].data
;
846 get_specialization(struct vtn_builder
*b
, struct vtn_value
*val
,
847 uint32_t const_value
)
849 vtn_foreach_decoration(b
, val
, spec_constant_deocoration_cb
, &const_value
);
854 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
855 const uint32_t *w
, unsigned count
)
857 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
858 val
->const_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
859 val
->constant
= rzalloc(b
, nir_constant
);
861 case SpvOpConstantTrue
:
862 assert(val
->const_type
== glsl_bool_type());
863 val
->constant
->value
.u
[0] = NIR_TRUE
;
865 case SpvOpConstantFalse
:
866 assert(val
->const_type
== glsl_bool_type());
867 val
->constant
->value
.u
[0] = NIR_FALSE
;
870 case SpvOpSpecConstantTrue
:
871 case SpvOpSpecConstantFalse
: {
872 assert(val
->const_type
== glsl_bool_type());
874 get_specialization(b
, val
, (opcode
== SpvOpSpecConstantTrue
));
875 val
->constant
->value
.u
[0] = int_val
? NIR_TRUE
: NIR_FALSE
;
880 assert(glsl_type_is_scalar(val
->const_type
));
881 val
->constant
->value
.u
[0] = w
[3];
883 case SpvOpSpecConstant
:
884 assert(glsl_type_is_scalar(val
->const_type
));
885 val
->constant
->value
.u
[0] = get_specialization(b
, val
, w
[3]);
887 case SpvOpSpecConstantComposite
:
888 case SpvOpConstantComposite
: {
889 unsigned elem_count
= count
- 3;
890 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
891 for (unsigned i
= 0; i
< elem_count
; i
++)
892 elems
[i
] = vtn_value(b
, w
[i
+ 3], vtn_value_type_constant
)->constant
;
894 switch (glsl_get_base_type(val
->const_type
)) {
897 case GLSL_TYPE_FLOAT
:
899 if (glsl_type_is_matrix(val
->const_type
)) {
900 unsigned rows
= glsl_get_vector_elements(val
->const_type
);
901 assert(glsl_get_matrix_columns(val
->const_type
) == elem_count
);
902 for (unsigned i
= 0; i
< elem_count
; i
++)
903 for (unsigned j
= 0; j
< rows
; j
++)
904 val
->constant
->value
.u
[rows
* i
+ j
] = elems
[i
]->value
.u
[j
];
906 assert(glsl_type_is_vector(val
->const_type
));
907 assert(glsl_get_vector_elements(val
->const_type
) == elem_count
);
908 for (unsigned i
= 0; i
< elem_count
; i
++)
909 val
->constant
->value
.u
[i
] = elems
[i
]->value
.u
[0];
914 case GLSL_TYPE_STRUCT
:
915 case GLSL_TYPE_ARRAY
:
916 ralloc_steal(val
->constant
, elems
);
917 val
->constant
->num_elements
= elem_count
;
918 val
->constant
->elements
= elems
;
922 unreachable("Unsupported type for constants");
927 case SpvOpSpecConstantOp
: {
928 SpvOp opcode
= get_specialization(b
, val
, w
[3]);
930 case SpvOpVectorShuffle
: {
931 struct vtn_value
*v0
= vtn_value(b
, w
[4], vtn_value_type_constant
);
932 struct vtn_value
*v1
= vtn_value(b
, w
[5], vtn_value_type_constant
);
933 unsigned len0
= glsl_get_vector_elements(v0
->const_type
);
934 unsigned len1
= glsl_get_vector_elements(v1
->const_type
);
937 for (unsigned i
= 0; i
< len0
; i
++)
938 u
[i
] = v0
->constant
->value
.u
[i
];
939 for (unsigned i
= 0; i
< len1
; i
++)
940 u
[len0
+ i
] = v1
->constant
->value
.u
[i
];
942 for (unsigned i
= 0; i
< count
- 6; i
++) {
943 uint32_t comp
= w
[i
+ 6];
944 if (comp
== (uint32_t)-1) {
945 val
->constant
->value
.u
[i
] = 0xdeadbeef;
947 val
->constant
->value
.u
[i
] = u
[comp
];
953 case SpvOpCompositeExtract
:
954 case SpvOpCompositeInsert
: {
955 struct vtn_value
*comp
;
956 unsigned deref_start
;
957 struct nir_constant
**c
;
958 if (opcode
== SpvOpCompositeExtract
) {
959 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
963 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
965 val
->constant
= nir_constant_clone(comp
->constant
,
971 const struct glsl_type
*type
= comp
->const_type
;
972 for (unsigned i
= deref_start
; i
< count
; i
++) {
973 switch (glsl_get_base_type(type
)) {
976 case GLSL_TYPE_FLOAT
:
978 /* If we hit this granularity, we're picking off an element */
982 if (glsl_type_is_matrix(type
)) {
983 elem
+= w
[i
] * glsl_get_vector_elements(type
);
984 type
= glsl_get_column_type(type
);
986 assert(glsl_type_is_vector(type
));
988 type
= glsl_scalar_type(glsl_get_base_type(type
));
992 case GLSL_TYPE_ARRAY
:
993 c
= &(*c
)->elements
[w
[i
]];
994 type
= glsl_get_array_element(type
);
997 case GLSL_TYPE_STRUCT
:
998 c
= &(*c
)->elements
[w
[i
]];
999 type
= glsl_get_struct_field(type
, w
[i
]);
1003 unreachable("Invalid constant type");
1007 if (opcode
== SpvOpCompositeExtract
) {
1011 unsigned num_components
= glsl_get_vector_elements(type
);
1012 for (unsigned i
= 0; i
< num_components
; i
++)
1013 val
->constant
->value
.u
[i
] = (*c
)->value
.u
[elem
+ i
];
1016 struct vtn_value
*insert
=
1017 vtn_value(b
, w
[4], vtn_value_type_constant
);
1018 assert(insert
->const_type
== type
);
1020 *c
= insert
->constant
;
1022 unsigned num_components
= glsl_get_vector_elements(type
);
1023 for (unsigned i
= 0; i
< num_components
; i
++)
1024 (*c
)->value
.u
[elem
+ i
] = insert
->constant
->value
.u
[i
];
1032 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(opcode
, &swap
);
1034 unsigned num_components
= glsl_get_vector_elements(val
->const_type
);
1036 nir_const_value src
[3];
1038 for (unsigned i
= 0; i
< count
- 4; i
++) {
1040 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
)->constant
;
1042 unsigned j
= swap
? 1 - i
: i
;
1043 for (unsigned k
= 0; k
< num_components
; k
++)
1044 src
[j
].u
[k
] = c
->value
.u
[k
];
1047 nir_const_value res
= nir_eval_const_opcode(op
, num_components
, src
);
1049 for (unsigned k
= 0; k
< num_components
; k
++)
1050 val
->constant
->value
.u
[k
] = res
.u
[k
];
1057 case SpvOpConstantNull
:
1058 val
->constant
= vtn_null_constant(b
, val
->const_type
);
1061 case SpvOpConstantSampler
:
1062 assert(!"OpConstantSampler requires Kernel Capability");
1066 unreachable("Unhandled opcode");
1071 vtn_handle_function_call(struct vtn_builder
*b
, SpvOp opcode
,
1072 const uint32_t *w
, unsigned count
)
1074 struct nir_function
*callee
=
1075 vtn_value(b
, w
[3], vtn_value_type_function
)->func
->impl
->function
;
1077 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, callee
);
1078 for (unsigned i
= 0; i
< call
->num_params
; i
++) {
1079 unsigned arg_id
= w
[4 + i
];
1080 struct vtn_value
*arg
= vtn_untyped_value(b
, arg_id
);
1081 if (arg
->value_type
== vtn_value_type_access_chain
) {
1082 nir_deref_var
*d
= vtn_access_chain_to_deref(b
, arg
->access_chain
);
1083 call
->params
[i
] = nir_deref_as_var(nir_copy_deref(call
, &d
->deref
));
1085 struct vtn_ssa_value
*arg_ssa
= vtn_ssa_value(b
, arg_id
);
1087 /* Make a temporary to store the argument in */
1089 nir_local_variable_create(b
->impl
, arg_ssa
->type
, "arg_tmp");
1090 call
->params
[i
] = nir_deref_var_create(call
, tmp
);
1092 vtn_local_store(b
, arg_ssa
, call
->params
[i
]);
1096 nir_variable
*out_tmp
= NULL
;
1097 if (!glsl_type_is_void(callee
->return_type
)) {
1098 out_tmp
= nir_local_variable_create(b
->impl
, callee
->return_type
,
1100 call
->return_deref
= nir_deref_var_create(call
, out_tmp
);
1103 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
1105 if (glsl_type_is_void(callee
->return_type
)) {
1106 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
1108 struct vtn_value
*retval
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1109 retval
->ssa
= vtn_local_load(b
, call
->return_deref
);
1113 struct vtn_ssa_value
*
1114 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1116 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1119 if (!glsl_type_is_vector_or_scalar(type
)) {
1120 unsigned elems
= glsl_get_length(type
);
1121 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1122 for (unsigned i
= 0; i
< elems
; i
++) {
1123 const struct glsl_type
*child_type
;
1125 switch (glsl_get_base_type(type
)) {
1127 case GLSL_TYPE_UINT
:
1128 case GLSL_TYPE_BOOL
:
1129 case GLSL_TYPE_FLOAT
:
1130 case GLSL_TYPE_DOUBLE
:
1131 child_type
= glsl_get_column_type(type
);
1133 case GLSL_TYPE_ARRAY
:
1134 child_type
= glsl_get_array_element(type
);
1136 case GLSL_TYPE_STRUCT
:
1137 child_type
= glsl_get_struct_field(type
, i
);
1140 unreachable("unkown base type");
1143 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
1151 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
1154 src
.src
= nir_src_for_ssa(vtn_ssa_value(b
, index
)->def
);
1155 src
.src_type
= type
;
1160 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
1161 const uint32_t *w
, unsigned count
)
1163 if (opcode
== SpvOpSampledImage
) {
1164 struct vtn_value
*val
=
1165 vtn_push_value(b
, w
[2], vtn_value_type_sampled_image
);
1166 val
->sampled_image
= ralloc(b
, struct vtn_sampled_image
);
1167 val
->sampled_image
->image
=
1168 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1169 val
->sampled_image
->sampler
=
1170 vtn_value(b
, w
[4], vtn_value_type_access_chain
)->access_chain
;
1172 } else if (opcode
== SpvOpImage
) {
1173 struct vtn_value
*val
=
1174 vtn_push_value(b
, w
[2], vtn_value_type_access_chain
);
1175 struct vtn_value
*src_val
= vtn_untyped_value(b
, w
[3]);
1176 if (src_val
->value_type
== vtn_value_type_sampled_image
) {
1177 val
->access_chain
= src_val
->sampled_image
->image
;
1179 assert(src_val
->value_type
== vtn_value_type_access_chain
);
1180 val
->access_chain
= src_val
->access_chain
;
1185 struct vtn_type
*ret_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1186 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1188 struct vtn_sampled_image sampled
;
1189 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
1190 if (sampled_val
->value_type
== vtn_value_type_sampled_image
) {
1191 sampled
= *sampled_val
->sampled_image
;
1193 assert(sampled_val
->value_type
== vtn_value_type_access_chain
);
1194 sampled
.image
= NULL
;
1195 sampled
.sampler
= sampled_val
->access_chain
;
1198 nir_tex_src srcs
[8]; /* 8 should be enough */
1199 nir_tex_src
*p
= srcs
;
1203 bool has_coord
= false;
1205 case SpvOpImageSampleImplicitLod
:
1206 case SpvOpImageSampleExplicitLod
:
1207 case SpvOpImageSampleDrefImplicitLod
:
1208 case SpvOpImageSampleDrefExplicitLod
:
1209 case SpvOpImageSampleProjImplicitLod
:
1210 case SpvOpImageSampleProjExplicitLod
:
1211 case SpvOpImageSampleProjDrefImplicitLod
:
1212 case SpvOpImageSampleProjDrefExplicitLod
:
1213 case SpvOpImageFetch
:
1214 case SpvOpImageGather
:
1215 case SpvOpImageDrefGather
:
1216 case SpvOpImageQueryLod
: {
1217 /* All these types have the coordinate as their first real argument */
1218 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, w
[idx
++]);
1220 p
->src
= nir_src_for_ssa(coord
->def
);
1221 p
->src_type
= nir_tex_src_coord
;
1230 /* These all have an explicit depth value as their next source */
1232 case SpvOpImageSampleDrefImplicitLod
:
1233 case SpvOpImageSampleDrefExplicitLod
:
1234 case SpvOpImageSampleProjDrefImplicitLod
:
1235 case SpvOpImageSampleProjDrefExplicitLod
:
1236 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparitor
);
1242 /* For OpImageQuerySizeLod, we always have an LOD */
1243 if (opcode
== SpvOpImageQuerySizeLod
)
1244 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1246 /* Figure out the base texture operation */
1249 case SpvOpImageSampleImplicitLod
:
1250 case SpvOpImageSampleDrefImplicitLod
:
1251 case SpvOpImageSampleProjImplicitLod
:
1252 case SpvOpImageSampleProjDrefImplicitLod
:
1253 texop
= nir_texop_tex
;
1256 case SpvOpImageSampleExplicitLod
:
1257 case SpvOpImageSampleDrefExplicitLod
:
1258 case SpvOpImageSampleProjExplicitLod
:
1259 case SpvOpImageSampleProjDrefExplicitLod
:
1260 texop
= nir_texop_txl
;
1263 case SpvOpImageFetch
:
1264 texop
= nir_texop_txf
;
1267 case SpvOpImageGather
:
1268 case SpvOpImageDrefGather
:
1269 texop
= nir_texop_tg4
;
1272 case SpvOpImageQuerySizeLod
:
1273 case SpvOpImageQuerySize
:
1274 texop
= nir_texop_txs
;
1277 case SpvOpImageQueryLod
:
1278 texop
= nir_texop_lod
;
1281 case SpvOpImageQueryLevels
:
1282 texop
= nir_texop_query_levels
;
1285 case SpvOpImageQuerySamples
:
1287 unreachable("Unhandled opcode");
1290 /* Now we need to handle some number of optional arguments */
1292 uint32_t operands
= w
[idx
++];
1294 if (operands
& SpvImageOperandsBiasMask
) {
1295 assert(texop
== nir_texop_tex
);
1296 texop
= nir_texop_txb
;
1297 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_bias
);
1300 if (operands
& SpvImageOperandsLodMask
) {
1301 assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
1302 texop
== nir_texop_txs
);
1303 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
1306 if (operands
& SpvImageOperandsGradMask
) {
1307 assert(texop
== nir_texop_tex
);
1308 texop
= nir_texop_txd
;
1309 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddx
);
1310 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ddy
);
1313 if (operands
& SpvImageOperandsOffsetMask
||
1314 operands
& SpvImageOperandsConstOffsetMask
)
1315 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_offset
);
1317 if (operands
& SpvImageOperandsConstOffsetsMask
)
1318 assert(!"Constant offsets to texture gather not yet implemented");
1320 if (operands
& SpvImageOperandsSampleMask
) {
1321 assert(texop
== nir_texop_txf
);
1322 texop
= nir_texop_txf_ms
;
1323 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
1326 /* We should have now consumed exactly all of the arguments */
1327 assert(idx
== count
);
1329 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
1332 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1334 const struct glsl_type
*image_type
;
1335 if (sampled
.image
) {
1336 image_type
= sampled
.image
->var
->var
->interface_type
;
1338 image_type
= sampled
.sampler
->var
->var
->interface_type
;
1341 instr
->sampler_dim
= glsl_get_sampler_dim(image_type
);
1342 instr
->is_array
= glsl_sampler_type_is_array(image_type
);
1343 instr
->is_shadow
= glsl_sampler_type_is_shadow(image_type
);
1344 instr
->is_new_style_shadow
= instr
->is_shadow
;
1347 switch (instr
->sampler_dim
) {
1348 case GLSL_SAMPLER_DIM_1D
:
1349 case GLSL_SAMPLER_DIM_BUF
:
1350 instr
->coord_components
= 1;
1352 case GLSL_SAMPLER_DIM_2D
:
1353 case GLSL_SAMPLER_DIM_RECT
:
1354 instr
->coord_components
= 2;
1356 case GLSL_SAMPLER_DIM_3D
:
1357 case GLSL_SAMPLER_DIM_CUBE
:
1358 case GLSL_SAMPLER_DIM_MS
:
1359 instr
->coord_components
= 3;
1362 assert("Invalid sampler type");
1365 if (instr
->is_array
)
1366 instr
->coord_components
++;
1368 instr
->coord_components
= 0;
1371 switch (glsl_get_sampler_result_type(image_type
)) {
1372 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
1373 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
1374 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_uint
; break;
1375 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
1377 unreachable("Invalid base type for sampler result");
1380 nir_deref_var
*sampler
= vtn_access_chain_to_deref(b
, sampled
.sampler
);
1381 instr
->sampler
= nir_deref_as_var(nir_copy_deref(instr
, &sampler
->deref
));
1382 if (sampled
.image
) {
1383 nir_deref_var
*image
= vtn_access_chain_to_deref(b
, sampled
.image
);
1384 instr
->texture
= nir_deref_as_var(nir_copy_deref(instr
, &image
->deref
));
1386 instr
->texture
= NULL
;
1389 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
1390 nir_tex_instr_dest_size(instr
), NULL
);
1392 assert(glsl_get_vector_elements(ret_type
->type
) ==
1393 nir_tex_instr_dest_size(instr
));
1395 val
->ssa
= vtn_create_ssa_value(b
, ret_type
->type
);
1396 val
->ssa
->def
= &instr
->dest
.ssa
;
1398 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
1401 static nir_ssa_def
*
1402 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
1404 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, value
);
1406 /* The image_load_store intrinsics assume a 4-dim coordinate */
1407 unsigned dim
= glsl_get_vector_elements(coord
->type
);
1408 unsigned swizzle
[4];
1409 for (unsigned i
= 0; i
< 4; i
++)
1410 swizzle
[i
] = MIN2(i
, dim
- 1);
1412 return nir_swizzle(&b
->nb
, coord
->def
, swizzle
, 4, false);
1416 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
1417 const uint32_t *w
, unsigned count
)
1419 /* Just get this one out of the way */
1420 if (opcode
== SpvOpImageTexelPointer
) {
1421 struct vtn_value
*val
=
1422 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
1423 val
->image
= ralloc(b
, struct vtn_image_pointer
);
1426 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1427 val
->image
->coord
= get_image_coord(b
, w
[4]);
1428 val
->image
->sample
= vtn_ssa_value(b
, w
[5])->def
;
1432 struct vtn_image_pointer image
;
1435 case SpvOpAtomicExchange
:
1436 case SpvOpAtomicCompareExchange
:
1437 case SpvOpAtomicCompareExchangeWeak
:
1438 case SpvOpAtomicIIncrement
:
1439 case SpvOpAtomicIDecrement
:
1440 case SpvOpAtomicIAdd
:
1441 case SpvOpAtomicISub
:
1442 case SpvOpAtomicSMin
:
1443 case SpvOpAtomicUMin
:
1444 case SpvOpAtomicSMax
:
1445 case SpvOpAtomicUMax
:
1446 case SpvOpAtomicAnd
:
1448 case SpvOpAtomicXor
:
1449 image
= *vtn_value(b
, w
[3], vtn_value_type_image_pointer
)->image
;
1452 case SpvOpImageQuerySize
:
1454 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1456 image
.sample
= NULL
;
1459 case SpvOpImageRead
:
1461 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1462 image
.coord
= get_image_coord(b
, w
[4]);
1464 if (count
> 5 && (w
[5] & SpvImageOperandsSampleMask
)) {
1465 assert(w
[5] == SpvImageOperandsSampleMask
);
1466 image
.sample
= vtn_ssa_value(b
, w
[6])->def
;
1468 image
.sample
= nir_ssa_undef(&b
->nb
, 1);
1472 case SpvOpImageWrite
:
1474 vtn_value(b
, w
[1], vtn_value_type_access_chain
)->access_chain
;
1475 image
.coord
= get_image_coord(b
, w
[2]);
1479 if (count
> 4 && (w
[4] & SpvImageOperandsSampleMask
)) {
1480 assert(w
[4] == SpvImageOperandsSampleMask
);
1481 image
.sample
= vtn_ssa_value(b
, w
[5])->def
;
1483 image
.sample
= nir_ssa_undef(&b
->nb
, 1);
1488 unreachable("Invalid image opcode");
1491 nir_intrinsic_op op
;
1493 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_##N; break;
1494 OP(ImageQuerySize
, size
)
1496 OP(ImageWrite
, store
)
1497 OP(AtomicExchange
, atomic_exchange
)
1498 OP(AtomicCompareExchange
, atomic_comp_swap
)
1499 OP(AtomicIIncrement
, atomic_add
)
1500 OP(AtomicIDecrement
, atomic_add
)
1501 OP(AtomicIAdd
, atomic_add
)
1502 OP(AtomicISub
, atomic_add
)
1503 OP(AtomicSMin
, atomic_min
)
1504 OP(AtomicUMin
, atomic_min
)
1505 OP(AtomicSMax
, atomic_max
)
1506 OP(AtomicUMax
, atomic_max
)
1507 OP(AtomicAnd
, atomic_and
)
1508 OP(AtomicOr
, atomic_or
)
1509 OP(AtomicXor
, atomic_xor
)
1512 unreachable("Invalid image opcode");
1515 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
1517 nir_deref_var
*image_deref
= vtn_access_chain_to_deref(b
, image
.image
);
1518 intrin
->variables
[0] =
1519 nir_deref_as_var(nir_copy_deref(&intrin
->instr
, &image_deref
->deref
));
1521 /* ImageQuerySize doesn't take any extra parameters */
1522 if (opcode
!= SpvOpImageQuerySize
) {
1523 /* The image coordinate is always 4 components but we may not have that
1524 * many. Swizzle to compensate.
1527 for (unsigned i
= 0; i
< 4; i
++)
1528 swiz
[i
] = i
< image
.coord
->num_components
? i
: 0;
1529 intrin
->src
[0] = nir_src_for_ssa(nir_swizzle(&b
->nb
, image
.coord
,
1531 intrin
->src
[1] = nir_src_for_ssa(image
.sample
);
1535 case SpvOpImageQuerySize
:
1536 case SpvOpImageRead
:
1538 case SpvOpImageWrite
:
1539 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[3])->def
);
1541 case SpvOpAtomicIIncrement
:
1542 intrin
->src
[2] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
1544 case SpvOpAtomicIDecrement
:
1545 intrin
->src
[2] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
1548 case SpvOpAtomicExchange
:
1549 case SpvOpAtomicIAdd
:
1550 case SpvOpAtomicSMin
:
1551 case SpvOpAtomicUMin
:
1552 case SpvOpAtomicSMax
:
1553 case SpvOpAtomicUMax
:
1554 case SpvOpAtomicAnd
:
1556 case SpvOpAtomicXor
:
1557 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1560 case SpvOpAtomicCompareExchange
:
1561 intrin
->src
[2] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
1562 intrin
->src
[3] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1565 case SpvOpAtomicISub
:
1566 intrin
->src
[2] = nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
1570 unreachable("Invalid image opcode");
1573 if (opcode
!= SpvOpImageWrite
) {
1574 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1575 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1576 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 4, NULL
);
1578 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
1580 /* The image intrinsics always return 4 channels but we may not want
1581 * that many. Emit a mov to trim it down.
1583 unsigned swiz
[4] = {0, 1, 2, 3};
1584 val
->ssa
= vtn_create_ssa_value(b
, type
->type
);
1585 val
->ssa
->def
= nir_swizzle(&b
->nb
, &intrin
->dest
.ssa
, swiz
,
1586 glsl_get_vector_elements(type
->type
), false);
1588 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
1592 static nir_intrinsic_op
1593 get_ssbo_nir_atomic_op(SpvOp opcode
)
1596 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
1597 OP(AtomicExchange
, atomic_exchange
)
1598 OP(AtomicCompareExchange
, atomic_comp_swap
)
1599 OP(AtomicIIncrement
, atomic_add
)
1600 OP(AtomicIDecrement
, atomic_add
)
1601 OP(AtomicIAdd
, atomic_add
)
1602 OP(AtomicISub
, atomic_add
)
1603 OP(AtomicSMin
, atomic_imin
)
1604 OP(AtomicUMin
, atomic_umin
)
1605 OP(AtomicSMax
, atomic_imax
)
1606 OP(AtomicUMax
, atomic_umax
)
1607 OP(AtomicAnd
, atomic_and
)
1608 OP(AtomicOr
, atomic_or
)
1609 OP(AtomicXor
, atomic_xor
)
1612 unreachable("Invalid SSBO atomic");
1616 static nir_intrinsic_op
1617 get_shared_nir_atomic_op(SpvOp opcode
)
1620 #define OP(S, N) case SpvOp##S: return nir_intrinsic_var_##N;
1621 OP(AtomicExchange
, atomic_exchange
)
1622 OP(AtomicCompareExchange
, atomic_comp_swap
)
1623 OP(AtomicIIncrement
, atomic_add
)
1624 OP(AtomicIDecrement
, atomic_add
)
1625 OP(AtomicIAdd
, atomic_add
)
1626 OP(AtomicISub
, atomic_add
)
1627 OP(AtomicSMin
, atomic_imin
)
1628 OP(AtomicUMin
, atomic_umin
)
1629 OP(AtomicSMax
, atomic_imax
)
1630 OP(AtomicUMax
, atomic_umax
)
1631 OP(AtomicAnd
, atomic_and
)
1632 OP(AtomicOr
, atomic_or
)
1633 OP(AtomicXor
, atomic_xor
)
1636 unreachable("Invalid shared atomic");
1641 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
1642 const uint32_t *w
, nir_src
*src
)
1645 case SpvOpAtomicIIncrement
:
1646 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
1649 case SpvOpAtomicIDecrement
:
1650 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
1653 case SpvOpAtomicISub
:
1655 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_ssa_value(b
, w
[6])->def
));
1658 case SpvOpAtomicCompareExchange
:
1659 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[7])->def
);
1660 src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[8])->def
);
1664 case SpvOpAtomicExchange
:
1665 case SpvOpAtomicIAdd
:
1666 case SpvOpAtomicSMin
:
1667 case SpvOpAtomicUMin
:
1668 case SpvOpAtomicSMax
:
1669 case SpvOpAtomicUMax
:
1670 case SpvOpAtomicAnd
:
1672 case SpvOpAtomicXor
:
1673 src
[0] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
1677 unreachable("Invalid SPIR-V atomic");
1682 vtn_handle_ssbo_or_shared_atomic(struct vtn_builder
*b
, SpvOp opcode
,
1683 const uint32_t *w
, unsigned count
)
1685 struct vtn_access_chain
*chain
=
1686 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
1687 nir_intrinsic_instr
*atomic
;
1690 SpvScope scope = w[4];
1691 SpvMemorySemanticsMask semantics = w[5];
1694 if (chain
->var
->mode
== vtn_variable_mode_workgroup
) {
1695 nir_deref
*deref
= &vtn_access_chain_to_deref(b
, chain
)->deref
;
1696 nir_intrinsic_op op
= get_shared_nir_atomic_op(opcode
);
1697 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
1698 atomic
->variables
[0] = nir_deref_as_var(nir_copy_deref(atomic
, deref
));
1699 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[0]);
1701 assert(chain
->var
->mode
== vtn_variable_mode_ssbo
);
1702 struct vtn_type
*type
;
1703 nir_ssa_def
*offset
, *index
;
1704 offset
= vtn_access_chain_to_offset(b
, chain
, &index
, &type
, NULL
, false);
1706 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(opcode
);
1708 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
1709 atomic
->src
[0] = nir_src_for_ssa(index
);
1710 atomic
->src
[1] = nir_src_for_ssa(offset
);
1711 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[2]);
1714 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
, 1, NULL
);
1716 struct vtn_type
*type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1717 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1718 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
1719 val
->ssa
->def
= &atomic
->dest
.ssa
;
1720 val
->ssa
->type
= type
->type
;
1722 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
1725 static nir_alu_instr
*
1726 create_vec(nir_shader
*shader
, unsigned num_components
)
1729 switch (num_components
) {
1730 case 1: op
= nir_op_fmov
; break;
1731 case 2: op
= nir_op_vec2
; break;
1732 case 3: op
= nir_op_vec3
; break;
1733 case 4: op
= nir_op_vec4
; break;
1734 default: unreachable("bad vector size");
1737 nir_alu_instr
*vec
= nir_alu_instr_create(shader
, op
);
1738 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
, NULL
);
1739 vec
->dest
.write_mask
= (1 << num_components
) - 1;
1744 struct vtn_ssa_value
*
1745 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
1747 if (src
->transposed
)
1748 return src
->transposed
;
1750 struct vtn_ssa_value
*dest
=
1751 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
1753 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
1754 nir_alu_instr
*vec
= create_vec(b
->shader
,
1755 glsl_get_matrix_columns(src
->type
));
1756 if (glsl_type_is_vector_or_scalar(src
->type
)) {
1757 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
1758 vec
->src
[0].swizzle
[0] = i
;
1760 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
1761 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
1762 vec
->src
[j
].swizzle
[0] = i
;
1765 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1766 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
1769 dest
->transposed
= src
;
1775 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
1777 unsigned swiz
[4] = { index
};
1778 return nir_swizzle(&b
->nb
, src
, swiz
, 1, true);
1782 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
1785 nir_alu_instr
*vec
= create_vec(b
->shader
, src
->num_components
);
1787 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
1789 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
1791 vec
->src
[i
].src
= nir_src_for_ssa(src
);
1792 vec
->src
[i
].swizzle
[0] = i
;
1796 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1798 return &vec
->dest
.dest
.ssa
;
1802 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
1805 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
1806 for (unsigned i
= 1; i
< src
->num_components
; i
++)
1807 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
1808 vtn_vector_extract(b
, src
, i
), dest
);
1814 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
1815 nir_ssa_def
*insert
, nir_ssa_def
*index
)
1817 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
1818 for (unsigned i
= 1; i
< src
->num_components
; i
++)
1819 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
1820 vtn_vector_insert(b
, src
, insert
, i
), dest
);
1825 static nir_ssa_def
*
1826 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
1827 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
1828 const uint32_t *indices
)
1830 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
);
1832 nir_ssa_undef_instr
*undef
= nir_ssa_undef_instr_create(b
->shader
, 1);
1833 nir_builder_instr_insert(&b
->nb
, &undef
->instr
);
1835 for (unsigned i
= 0; i
< num_components
; i
++) {
1836 uint32_t index
= indices
[i
];
1837 if (index
== 0xffffffff) {
1838 vec
->src
[i
].src
= nir_src_for_ssa(&undef
->def
);
1839 } else if (index
< src0
->num_components
) {
1840 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
1841 vec
->src
[i
].swizzle
[0] = index
;
1843 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
1844 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
1848 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1850 return &vec
->dest
.dest
.ssa
;
1854 * Concatentates a number of vectors/scalars together to produce a vector
1856 static nir_ssa_def
*
1857 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
1858 unsigned num_srcs
, nir_ssa_def
**srcs
)
1860 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
);
1862 unsigned dest_idx
= 0;
1863 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1864 nir_ssa_def
*src
= srcs
[i
];
1865 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
1866 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
1867 vec
->src
[dest_idx
].swizzle
[0] = j
;
1872 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1874 return &vec
->dest
.dest
.ssa
;
1877 static struct vtn_ssa_value
*
1878 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
1880 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
1881 dest
->type
= src
->type
;
1883 if (glsl_type_is_vector_or_scalar(src
->type
)) {
1884 dest
->def
= src
->def
;
1886 unsigned elems
= glsl_get_length(src
->type
);
1888 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
1889 for (unsigned i
= 0; i
< elems
; i
++)
1890 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
1896 static struct vtn_ssa_value
*
1897 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
1898 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
1899 unsigned num_indices
)
1901 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
1903 struct vtn_ssa_value
*cur
= dest
;
1905 for (i
= 0; i
< num_indices
- 1; i
++) {
1906 cur
= cur
->elems
[indices
[i
]];
1909 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
1910 /* According to the SPIR-V spec, OpCompositeInsert may work down to
1911 * the component granularity. In that case, the last index will be
1912 * the index to insert the scalar into the vector.
1915 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
1917 cur
->elems
[indices
[i
]] = insert
;
1923 static struct vtn_ssa_value
*
1924 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
1925 const uint32_t *indices
, unsigned num_indices
)
1927 struct vtn_ssa_value
*cur
= src
;
1928 for (unsigned i
= 0; i
< num_indices
; i
++) {
1929 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
1930 assert(i
== num_indices
- 1);
1931 /* According to the SPIR-V spec, OpCompositeExtract may work down to
1932 * the component granularity. The last index will be the index of the
1933 * vector to extract.
1936 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
1937 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
1938 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
1941 cur
= cur
->elems
[indices
[i
]];
1949 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
1950 const uint32_t *w
, unsigned count
)
1952 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1953 const struct glsl_type
*type
=
1954 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
1955 val
->ssa
= vtn_create_ssa_value(b
, type
);
1958 case SpvOpVectorExtractDynamic
:
1959 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
1960 vtn_ssa_value(b
, w
[4])->def
);
1963 case SpvOpVectorInsertDynamic
:
1964 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
1965 vtn_ssa_value(b
, w
[4])->def
,
1966 vtn_ssa_value(b
, w
[5])->def
);
1969 case SpvOpVectorShuffle
:
1970 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
1971 vtn_ssa_value(b
, w
[3])->def
,
1972 vtn_ssa_value(b
, w
[4])->def
,
1976 case SpvOpCompositeConstruct
: {
1977 unsigned elems
= count
- 3;
1978 if (glsl_type_is_vector_or_scalar(type
)) {
1979 nir_ssa_def
*srcs
[4];
1980 for (unsigned i
= 0; i
< elems
; i
++)
1981 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
1983 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
1986 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1987 for (unsigned i
= 0; i
< elems
; i
++)
1988 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
1992 case SpvOpCompositeExtract
:
1993 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
1997 case SpvOpCompositeInsert
:
1998 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
1999 vtn_ssa_value(b
, w
[3]),
2003 case SpvOpCopyObject
:
2004 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
2008 unreachable("unknown composite operation");
2013 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
2014 const uint32_t *w
, unsigned count
)
2016 nir_intrinsic_op intrinsic_op
;
2018 case SpvOpEmitVertex
:
2019 case SpvOpEmitStreamVertex
:
2020 intrinsic_op
= nir_intrinsic_emit_vertex
;
2022 case SpvOpEndPrimitive
:
2023 case SpvOpEndStreamPrimitive
:
2024 intrinsic_op
= nir_intrinsic_end_primitive
;
2026 case SpvOpMemoryBarrier
:
2027 intrinsic_op
= nir_intrinsic_memory_barrier
;
2029 case SpvOpControlBarrier
:
2030 intrinsic_op
= nir_intrinsic_barrier
;
2033 unreachable("unknown barrier instruction");
2036 nir_intrinsic_instr
*intrin
=
2037 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
2039 if (opcode
== SpvOpEmitStreamVertex
|| opcode
== SpvOpEndStreamPrimitive
)
2040 intrin
->const_index
[0] = w
[1];
2042 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
2046 gl_primitive_from_spv_execution_mode(SpvExecutionMode mode
)
2049 case SpvExecutionModeInputPoints
:
2050 case SpvExecutionModeOutputPoints
:
2051 return 0; /* GL_POINTS */
2052 case SpvExecutionModeInputLines
:
2053 return 1; /* GL_LINES */
2054 case SpvExecutionModeInputLinesAdjacency
:
2055 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
2056 case SpvExecutionModeTriangles
:
2057 return 4; /* GL_TRIANGLES */
2058 case SpvExecutionModeInputTrianglesAdjacency
:
2059 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
2060 case SpvExecutionModeQuads
:
2061 return 7; /* GL_QUADS */
2062 case SpvExecutionModeIsolines
:
2063 return 0x8E7A; /* GL_ISOLINES */
2064 case SpvExecutionModeOutputLineStrip
:
2065 return 3; /* GL_LINE_STRIP */
2066 case SpvExecutionModeOutputTriangleStrip
:
2067 return 5; /* GL_TRIANGLE_STRIP */
2069 assert(!"Invalid primitive type");
2075 vertices_in_from_spv_execution_mode(SpvExecutionMode mode
)
2078 case SpvExecutionModeInputPoints
:
2080 case SpvExecutionModeInputLines
:
2082 case SpvExecutionModeInputLinesAdjacency
:
2084 case SpvExecutionModeTriangles
:
2086 case SpvExecutionModeInputTrianglesAdjacency
:
2089 assert(!"Invalid GS input mode");
2094 static gl_shader_stage
2095 stage_for_execution_model(SpvExecutionModel model
)
2098 case SpvExecutionModelVertex
:
2099 return MESA_SHADER_VERTEX
;
2100 case SpvExecutionModelTessellationControl
:
2101 return MESA_SHADER_TESS_CTRL
;
2102 case SpvExecutionModelTessellationEvaluation
:
2103 return MESA_SHADER_TESS_EVAL
;
2104 case SpvExecutionModelGeometry
:
2105 return MESA_SHADER_GEOMETRY
;
2106 case SpvExecutionModelFragment
:
2107 return MESA_SHADER_FRAGMENT
;
2108 case SpvExecutionModelGLCompute
:
2109 return MESA_SHADER_COMPUTE
;
2111 unreachable("Unsupported execution model");
2116 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2117 const uint32_t *w
, unsigned count
)
2121 case SpvOpSourceExtension
:
2122 case SpvOpSourceContinued
:
2123 case SpvOpExtension
:
2124 /* Unhandled, but these are for debug so that's ok. */
2127 case SpvOpCapability
:
2128 switch ((SpvCapability
)w
[1]) {
2129 case SpvCapabilityMatrix
:
2130 case SpvCapabilityShader
:
2131 case SpvCapabilityGeometry
:
2134 assert(!"Unsupported capability");
2138 case SpvOpExtInstImport
:
2139 vtn_handle_extension(b
, opcode
, w
, count
);
2142 case SpvOpMemoryModel
:
2143 assert(w
[1] == SpvAddressingModelLogical
);
2144 assert(w
[2] == SpvMemoryModelGLSL450
);
2147 case SpvOpEntryPoint
: {
2148 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
2149 /* Let this be a name label regardless */
2150 unsigned name_words
;
2151 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
2153 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
2154 stage_for_execution_model(w
[1]) != b
->entry_point_stage
)
2157 assert(b
->entry_point
== NULL
);
2158 b
->entry_point
= entry_point
;
2163 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
2164 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2168 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
2171 case SpvOpMemberName
:
2175 case SpvOpExecutionMode
:
2176 case SpvOpDecorationGroup
:
2178 case SpvOpMemberDecorate
:
2179 case SpvOpGroupDecorate
:
2180 case SpvOpGroupMemberDecorate
:
2181 vtn_handle_decoration(b
, opcode
, w
, count
);
2185 return false; /* End of preamble */
2192 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
2193 const struct vtn_decoration
*mode
, void *data
)
2195 assert(b
->entry_point
== entry_point
);
2197 switch(mode
->exec_mode
) {
2198 case SpvExecutionModeOriginUpperLeft
:
2199 case SpvExecutionModeOriginLowerLeft
:
2200 b
->origin_upper_left
=
2201 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
2204 case SpvExecutionModeEarlyFragmentTests
:
2205 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2206 b
->shader
->info
.fs
.early_fragment_tests
= true;
2209 case SpvExecutionModeInvocations
:
2210 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2211 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->literals
[0]);
2214 case SpvExecutionModeDepthReplacing
:
2215 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2216 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
2218 case SpvExecutionModeDepthGreater
:
2219 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2220 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
2222 case SpvExecutionModeDepthLess
:
2223 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2224 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
2226 case SpvExecutionModeDepthUnchanged
:
2227 assert(b
->shader
->stage
== MESA_SHADER_FRAGMENT
);
2228 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
2231 case SpvExecutionModeLocalSize
:
2232 assert(b
->shader
->stage
== MESA_SHADER_COMPUTE
);
2233 b
->shader
->info
.cs
.local_size
[0] = mode
->literals
[0];
2234 b
->shader
->info
.cs
.local_size
[1] = mode
->literals
[1];
2235 b
->shader
->info
.cs
.local_size
[2] = mode
->literals
[2];
2237 case SpvExecutionModeLocalSizeHint
:
2238 break; /* Nothing do do with this */
2240 case SpvExecutionModeOutputVertices
:
2241 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2242 b
->shader
->info
.gs
.vertices_out
= mode
->literals
[0];
2245 case SpvExecutionModeInputPoints
:
2246 case SpvExecutionModeInputLines
:
2247 case SpvExecutionModeInputLinesAdjacency
:
2248 case SpvExecutionModeTriangles
:
2249 case SpvExecutionModeInputTrianglesAdjacency
:
2250 case SpvExecutionModeQuads
:
2251 case SpvExecutionModeIsolines
:
2252 if (b
->shader
->stage
== MESA_SHADER_GEOMETRY
) {
2253 b
->shader
->info
.gs
.vertices_in
=
2254 vertices_in_from_spv_execution_mode(mode
->exec_mode
);
2256 assert(!"Tesselation shaders not yet supported");
2260 case SpvExecutionModeOutputPoints
:
2261 case SpvExecutionModeOutputLineStrip
:
2262 case SpvExecutionModeOutputTriangleStrip
:
2263 assert(b
->shader
->stage
== MESA_SHADER_GEOMETRY
);
2264 b
->shader
->info
.gs
.output_primitive
=
2265 gl_primitive_from_spv_execution_mode(mode
->exec_mode
);
2268 case SpvExecutionModeSpacingEqual
:
2269 case SpvExecutionModeSpacingFractionalEven
:
2270 case SpvExecutionModeSpacingFractionalOdd
:
2271 case SpvExecutionModeVertexOrderCw
:
2272 case SpvExecutionModeVertexOrderCcw
:
2273 case SpvExecutionModePointMode
:
2274 assert(!"TODO: Add tessellation metadata");
2277 case SpvExecutionModePixelCenterInteger
:
2278 case SpvExecutionModeXfb
:
2279 assert(!"Unhandled execution mode");
2282 case SpvExecutionModeVecTypeHint
:
2283 case SpvExecutionModeContractionOff
:
2289 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2290 const uint32_t *w
, unsigned count
)
2294 case SpvOpSourceContinued
:
2295 case SpvOpSourceExtension
:
2296 case SpvOpExtension
:
2297 case SpvOpCapability
:
2298 case SpvOpExtInstImport
:
2299 case SpvOpMemoryModel
:
2300 case SpvOpEntryPoint
:
2301 case SpvOpExecutionMode
:
2304 case SpvOpMemberName
:
2305 case SpvOpDecorationGroup
:
2307 case SpvOpMemberDecorate
:
2308 case SpvOpGroupDecorate
:
2309 case SpvOpGroupMemberDecorate
:
2310 assert(!"Invalid opcode types and variables section");
2316 case SpvOpTypeFloat
:
2317 case SpvOpTypeVector
:
2318 case SpvOpTypeMatrix
:
2319 case SpvOpTypeImage
:
2320 case SpvOpTypeSampler
:
2321 case SpvOpTypeSampledImage
:
2322 case SpvOpTypeArray
:
2323 case SpvOpTypeRuntimeArray
:
2324 case SpvOpTypeStruct
:
2325 case SpvOpTypeOpaque
:
2326 case SpvOpTypePointer
:
2327 case SpvOpTypeFunction
:
2328 case SpvOpTypeEvent
:
2329 case SpvOpTypeDeviceEvent
:
2330 case SpvOpTypeReserveId
:
2331 case SpvOpTypeQueue
:
2333 vtn_handle_type(b
, opcode
, w
, count
);
2336 case SpvOpConstantTrue
:
2337 case SpvOpConstantFalse
:
2339 case SpvOpConstantComposite
:
2340 case SpvOpConstantSampler
:
2341 case SpvOpConstantNull
:
2342 case SpvOpSpecConstantTrue
:
2343 case SpvOpSpecConstantFalse
:
2344 case SpvOpSpecConstant
:
2345 case SpvOpSpecConstantComposite
:
2346 case SpvOpSpecConstantOp
:
2347 vtn_handle_constant(b
, opcode
, w
, count
);
2351 vtn_handle_variables(b
, opcode
, w
, count
);
2355 return false; /* End of preamble */
2362 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2363 const uint32_t *w
, unsigned count
)
2369 case SpvOpLoopMerge
:
2370 case SpvOpSelectionMerge
:
2371 /* This is handled by cfg pre-pass and walk_blocks */
2375 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
2376 val
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
2381 vtn_handle_extension(b
, opcode
, w
, count
);
2387 case SpvOpCopyMemory
:
2388 case SpvOpCopyMemorySized
:
2389 case SpvOpAccessChain
:
2390 case SpvOpInBoundsAccessChain
:
2391 case SpvOpArrayLength
:
2392 vtn_handle_variables(b
, opcode
, w
, count
);
2395 case SpvOpFunctionCall
:
2396 vtn_handle_function_call(b
, opcode
, w
, count
);
2399 case SpvOpSampledImage
:
2401 case SpvOpImageSampleImplicitLod
:
2402 case SpvOpImageSampleExplicitLod
:
2403 case SpvOpImageSampleDrefImplicitLod
:
2404 case SpvOpImageSampleDrefExplicitLod
:
2405 case SpvOpImageSampleProjImplicitLod
:
2406 case SpvOpImageSampleProjExplicitLod
:
2407 case SpvOpImageSampleProjDrefImplicitLod
:
2408 case SpvOpImageSampleProjDrefExplicitLod
:
2409 case SpvOpImageFetch
:
2410 case SpvOpImageGather
:
2411 case SpvOpImageDrefGather
:
2412 case SpvOpImageQuerySizeLod
:
2413 case SpvOpImageQueryLod
:
2414 case SpvOpImageQueryLevels
:
2415 case SpvOpImageQuerySamples
:
2416 vtn_handle_texture(b
, opcode
, w
, count
);
2419 case SpvOpImageRead
:
2420 case SpvOpImageWrite
:
2421 case SpvOpImageTexelPointer
:
2422 vtn_handle_image(b
, opcode
, w
, count
);
2425 case SpvOpImageQuerySize
: {
2426 struct vtn_access_chain
*image
=
2427 vtn_value(b
, w
[3], vtn_value_type_access_chain
)->access_chain
;
2428 if (glsl_type_is_image(image
->var
->var
->interface_type
)) {
2429 vtn_handle_image(b
, opcode
, w
, count
);
2431 vtn_handle_texture(b
, opcode
, w
, count
);
2436 case SpvOpAtomicExchange
:
2437 case SpvOpAtomicCompareExchange
:
2438 case SpvOpAtomicCompareExchangeWeak
:
2439 case SpvOpAtomicIIncrement
:
2440 case SpvOpAtomicIDecrement
:
2441 case SpvOpAtomicIAdd
:
2442 case SpvOpAtomicISub
:
2443 case SpvOpAtomicSMin
:
2444 case SpvOpAtomicUMin
:
2445 case SpvOpAtomicSMax
:
2446 case SpvOpAtomicUMax
:
2447 case SpvOpAtomicAnd
:
2449 case SpvOpAtomicXor
: {
2450 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
2451 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
2452 vtn_handle_image(b
, opcode
, w
, count
);
2454 assert(pointer
->value_type
== vtn_value_type_access_chain
);
2455 vtn_handle_ssbo_or_shared_atomic(b
, opcode
, w
, count
);
2465 case SpvOpConvertFToU
:
2466 case SpvOpConvertFToS
:
2467 case SpvOpConvertSToF
:
2468 case SpvOpConvertUToF
:
2472 case SpvOpQuantizeToF16
:
2473 case SpvOpConvertPtrToU
:
2474 case SpvOpConvertUToPtr
:
2475 case SpvOpPtrCastToGeneric
:
2476 case SpvOpGenericCastToPtr
:
2482 case SpvOpSignBitSet
:
2483 case SpvOpLessOrGreater
:
2485 case SpvOpUnordered
:
2500 case SpvOpVectorTimesScalar
:
2502 case SpvOpIAddCarry
:
2503 case SpvOpISubBorrow
:
2504 case SpvOpUMulExtended
:
2505 case SpvOpSMulExtended
:
2506 case SpvOpShiftRightLogical
:
2507 case SpvOpShiftRightArithmetic
:
2508 case SpvOpShiftLeftLogical
:
2509 case SpvOpLogicalEqual
:
2510 case SpvOpLogicalNotEqual
:
2511 case SpvOpLogicalOr
:
2512 case SpvOpLogicalAnd
:
2513 case SpvOpLogicalNot
:
2514 case SpvOpBitwiseOr
:
2515 case SpvOpBitwiseXor
:
2516 case SpvOpBitwiseAnd
:
2519 case SpvOpFOrdEqual
:
2520 case SpvOpFUnordEqual
:
2521 case SpvOpINotEqual
:
2522 case SpvOpFOrdNotEqual
:
2523 case SpvOpFUnordNotEqual
:
2524 case SpvOpULessThan
:
2525 case SpvOpSLessThan
:
2526 case SpvOpFOrdLessThan
:
2527 case SpvOpFUnordLessThan
:
2528 case SpvOpUGreaterThan
:
2529 case SpvOpSGreaterThan
:
2530 case SpvOpFOrdGreaterThan
:
2531 case SpvOpFUnordGreaterThan
:
2532 case SpvOpULessThanEqual
:
2533 case SpvOpSLessThanEqual
:
2534 case SpvOpFOrdLessThanEqual
:
2535 case SpvOpFUnordLessThanEqual
:
2536 case SpvOpUGreaterThanEqual
:
2537 case SpvOpSGreaterThanEqual
:
2538 case SpvOpFOrdGreaterThanEqual
:
2539 case SpvOpFUnordGreaterThanEqual
:
2545 case SpvOpFwidthFine
:
2546 case SpvOpDPdxCoarse
:
2547 case SpvOpDPdyCoarse
:
2548 case SpvOpFwidthCoarse
:
2549 case SpvOpBitFieldInsert
:
2550 case SpvOpBitFieldSExtract
:
2551 case SpvOpBitFieldUExtract
:
2552 case SpvOpBitReverse
:
2554 case SpvOpTranspose
:
2555 case SpvOpOuterProduct
:
2556 case SpvOpMatrixTimesScalar
:
2557 case SpvOpVectorTimesMatrix
:
2558 case SpvOpMatrixTimesVector
:
2559 case SpvOpMatrixTimesMatrix
:
2560 vtn_handle_alu(b
, opcode
, w
, count
);
2563 case SpvOpVectorExtractDynamic
:
2564 case SpvOpVectorInsertDynamic
:
2565 case SpvOpVectorShuffle
:
2566 case SpvOpCompositeConstruct
:
2567 case SpvOpCompositeExtract
:
2568 case SpvOpCompositeInsert
:
2569 case SpvOpCopyObject
:
2570 vtn_handle_composite(b
, opcode
, w
, count
);
2573 case SpvOpEmitVertex
:
2574 case SpvOpEndPrimitive
:
2575 case SpvOpEmitStreamVertex
:
2576 case SpvOpEndStreamPrimitive
:
2577 case SpvOpControlBarrier
:
2578 case SpvOpMemoryBarrier
:
2579 vtn_handle_barrier(b
, opcode
, w
, count
);
2583 unreachable("Unhandled opcode");
2590 spirv_to_nir(const uint32_t *words
, size_t word_count
,
2591 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
2592 gl_shader_stage stage
, const char *entry_point_name
,
2593 const nir_shader_compiler_options
*options
)
2595 const uint32_t *word_end
= words
+ word_count
;
2597 /* Handle the SPIR-V header (first 4 dwords) */
2598 assert(word_count
> 5);
2600 assert(words
[0] == SpvMagicNumber
);
2601 assert(words
[1] >= 0x10000);
2602 /* words[2] == generator magic */
2603 unsigned value_id_bound
= words
[3];
2604 assert(words
[4] == 0);
2608 /* Initialize the stn_builder object */
2609 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
2610 b
->value_id_bound
= value_id_bound
;
2611 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
2612 exec_list_make_empty(&b
->functions
);
2613 b
->entry_point_stage
= stage
;
2614 b
->entry_point_name
= entry_point_name
;
2616 /* Handle all the preamble instructions */
2617 words
= vtn_foreach_instruction(b
, words
, word_end
,
2618 vtn_handle_preamble_instruction
);
2620 if (b
->entry_point
== NULL
) {
2621 assert(!"Entry point not found");
2626 b
->shader
= nir_shader_create(NULL
, stage
, options
);
2628 /* Parse execution modes */
2629 vtn_foreach_execution_mode(b
, b
->entry_point
,
2630 vtn_handle_execution_mode
, NULL
);
2632 b
->specializations
= spec
;
2633 b
->num_specializations
= num_spec
;
2635 /* Handle all variable, type, and constant instructions */
2636 words
= vtn_foreach_instruction(b
, words
, word_end
,
2637 vtn_handle_variable_or_type_instruction
);
2639 vtn_build_cfg(b
, words
, word_end
);
2641 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
2642 b
->impl
= func
->impl
;
2643 b
->const_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
2644 _mesa_key_pointer_equal
);
2646 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
2649 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
2650 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
2651 assert(entry_point
);