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 "spirv_to_nir_private.h"
30 #include "nir_control_flow.h"
32 static struct vtn_ssa_value
*
33 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
34 const struct glsl_type
*type
)
36 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
41 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
44 switch (glsl_get_base_type(type
)) {
49 case GLSL_TYPE_DOUBLE
:
50 if (glsl_type_is_vector_or_scalar(type
)) {
51 unsigned num_components
= glsl_get_vector_elements(val
->type
);
52 nir_load_const_instr
*load
=
53 nir_load_const_instr_create(b
->shader
, num_components
);
55 for (unsigned i
= 0; i
< num_components
; i
++)
56 load
->value
.u
[i
] = constant
->value
.u
[i
];
58 nir_instr_insert_before_cf_list(&b
->impl
->body
, &load
->instr
);
59 val
->def
= &load
->def
;
61 assert(glsl_type_is_matrix(type
));
62 unsigned rows
= glsl_get_vector_elements(val
->type
);
63 unsigned columns
= glsl_get_matrix_columns(val
->type
);
64 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, columns
);
66 for (unsigned i
= 0; i
< columns
; i
++) {
67 struct vtn_ssa_value
*col_val
= rzalloc(b
, struct vtn_ssa_value
);
68 col_val
->type
= glsl_get_column_type(val
->type
);
69 nir_load_const_instr
*load
=
70 nir_load_const_instr_create(b
->shader
, rows
);
72 for (unsigned j
= 0; j
< rows
; j
++)
73 load
->value
.u
[j
] = constant
->value
.u
[rows
* i
+ j
];
75 nir_instr_insert_before_cf_list(&b
->impl
->body
, &load
->instr
);
76 col_val
->def
= &load
->def
;
78 val
->elems
[i
] = col_val
;
83 case GLSL_TYPE_ARRAY
: {
84 unsigned elems
= glsl_get_length(val
->type
);
85 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
86 const struct glsl_type
*elem_type
= glsl_get_array_element(val
->type
);
87 for (unsigned i
= 0; i
< elems
; i
++)
88 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
93 case GLSL_TYPE_STRUCT
: {
94 unsigned elems
= glsl_get_length(val
->type
);
95 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
96 for (unsigned i
= 0; i
< elems
; i
++) {
97 const struct glsl_type
*elem_type
=
98 glsl_get_struct_field(val
->type
, i
);
99 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
106 unreachable("bad constant type");
112 struct vtn_ssa_value
*
113 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
115 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
116 switch (val
->value_type
) {
117 case vtn_value_type_constant
:
118 return vtn_const_ssa_value(b
, val
->constant
, val
->const_type
);
120 case vtn_value_type_ssa
:
123 unreachable("Invalid type for an SSA value");
128 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
131 return ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
134 static const uint32_t *
135 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
136 const uint32_t *end
, vtn_instruction_handler handler
)
138 const uint32_t *w
= start
;
140 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
141 unsigned count
= w
[0] >> SpvWordCountShift
;
142 assert(count
>= 1 && w
+ count
<= end
);
144 if (!handler(b
, opcode
, w
, count
))
154 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
155 const uint32_t *w
, unsigned count
)
158 case SpvOpExtInstImport
: {
159 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
160 if (strcmp((const char *)&w
[2], "GLSL.std.450") == 0) {
161 val
->ext_handler
= vtn_handle_glsl450_instruction
;
163 assert(!"Unsupported extension");
169 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
170 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
177 unreachable("Unhandled opcode");
182 _foreach_decoration_helper(struct vtn_builder
*b
,
183 struct vtn_value
*base_value
,
185 struct vtn_value
*value
,
186 vtn_decoration_foreach_cb cb
, void *data
)
188 int new_member
= member
;
190 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
191 if (dec
->member
>= 0) {
192 assert(member
== -1);
193 new_member
= dec
->member
;
197 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
198 _foreach_decoration_helper(b
, base_value
, new_member
, dec
->group
,
201 cb(b
, base_value
, new_member
, dec
, data
);
206 /** Iterates (recursively if needed) over all of the decorations on a value
208 * This function iterates over all of the decorations applied to a given
209 * value. If it encounters a decoration group, it recurses into the group
210 * and iterates over all of those decorations as well.
213 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
214 vtn_decoration_foreach_cb cb
, void *data
)
216 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
220 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
221 const uint32_t *w
, unsigned count
)
223 const uint32_t *w_end
= w
+ count
;
224 const uint32_t target
= w
[1];
229 case SpvOpDecorationGroup
:
230 vtn_push_value(b
, target
, vtn_value_type_undef
);
233 case SpvOpMemberDecorate
:
236 case SpvOpDecorate
: {
237 struct vtn_value
*val
= &b
->values
[target
];
239 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
240 dec
->member
= member
;
241 dec
->decoration
= *(w
++);
244 /* Link into the list */
245 dec
->next
= val
->decoration
;
246 val
->decoration
= dec
;
250 case SpvOpGroupMemberDecorate
:
253 case SpvOpGroupDecorate
: {
254 struct vtn_value
*group
= &b
->values
[target
];
255 assert(group
->value_type
== vtn_value_type_decoration_group
);
257 for (; w
< w_end
; w
++) {
258 struct vtn_value
*val
= &b
->values
[*w
];
259 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
260 dec
->member
= member
;
263 /* Link into the list */
264 dec
->next
= val
->decoration
;
265 val
->decoration
= dec
;
271 unreachable("Unhandled opcode");
275 struct member_decoration_ctx
{
276 struct glsl_struct_field
*fields
;
277 struct vtn_type
*type
;
280 /* does a shallow copy of a vtn_type */
282 static struct vtn_type
*
283 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
285 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
286 dest
->type
= src
->type
;
287 dest
->is_builtin
= src
->is_builtin
;
289 dest
->builtin
= src
->builtin
;
291 if (!glsl_type_is_vector_or_scalar(src
->type
)) {
292 switch (glsl_get_base_type(src
->type
)) {
293 case GLSL_TYPE_ARRAY
:
294 dest
->array_element
= src
->array_element
;
295 dest
->stride
= src
->stride
;
301 case GLSL_TYPE_FLOAT
:
302 case GLSL_TYPE_DOUBLE
:
304 dest
->row_major
= src
->row_major
;
305 dest
->stride
= src
->stride
;
308 case GLSL_TYPE_STRUCT
: {
309 unsigned elems
= glsl_get_length(src
->type
);
311 dest
->members
= ralloc_array(b
, struct vtn_type
*, elems
);
312 memcpy(dest
->members
, src
->members
, elems
* sizeof(struct vtn_type
*));
314 dest
->offsets
= ralloc_array(b
, unsigned, elems
);
315 memcpy(dest
->offsets
, src
->offsets
, elems
* sizeof(unsigned));
320 unreachable("unhandled type");
328 struct_member_decoration_cb(struct vtn_builder
*b
,
329 struct vtn_value
*val
, int member
,
330 const struct vtn_decoration
*dec
, void *void_ctx
)
332 struct member_decoration_ctx
*ctx
= void_ctx
;
337 switch (dec
->decoration
) {
338 case SpvDecorationRelaxedPrecision
:
339 break; /* FIXME: Do nothing with this for now. */
340 case SpvDecorationSmooth
:
341 ctx
->fields
[member
].interpolation
= INTERP_QUALIFIER_SMOOTH
;
343 case SpvDecorationNoperspective
:
344 ctx
->fields
[member
].interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
346 case SpvDecorationFlat
:
347 ctx
->fields
[member
].interpolation
= INTERP_QUALIFIER_FLAT
;
349 case SpvDecorationCentroid
:
350 ctx
->fields
[member
].centroid
= true;
352 case SpvDecorationSample
:
353 ctx
->fields
[member
].sample
= true;
355 case SpvDecorationLocation
:
356 ctx
->fields
[member
].location
= dec
->literals
[0];
358 case SpvDecorationBuiltIn
:
359 ctx
->type
->members
[member
] = vtn_type_copy(b
,
360 ctx
->type
->members
[member
]);
361 ctx
->type
->members
[member
]->is_builtin
= true;
362 ctx
->type
->members
[member
]->builtin
= dec
->literals
[0];
363 ctx
->type
->builtin_block
= true;
365 case SpvDecorationOffset
:
366 ctx
->type
->offsets
[member
] = dec
->literals
[0];
368 case SpvDecorationMatrixStride
:
369 ctx
->type
->members
[member
]->stride
= dec
->literals
[0];
371 case SpvDecorationColMajor
:
372 break; /* Nothing to do here. Column-major is the default. */
374 unreachable("Unhandled member decoration");
379 type_decoration_cb(struct vtn_builder
*b
,
380 struct vtn_value
*val
, int member
,
381 const struct vtn_decoration
*dec
, void *ctx
)
383 struct vtn_type
*type
= val
->type
;
388 switch (dec
->decoration
) {
389 case SpvDecorationArrayStride
:
390 type
->stride
= dec
->literals
[0];
392 case SpvDecorationBlock
:
395 case SpvDecorationBufferBlock
:
396 type
->buffer_block
= true;
398 case SpvDecorationGLSLShared
:
399 case SpvDecorationGLSLPacked
:
400 /* Ignore these, since we get explicit offsets anyways */
404 unreachable("Unhandled type decoration");
409 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
410 const uint32_t *w
, unsigned count
)
412 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
414 val
->type
= rzalloc(b
, struct vtn_type
);
415 val
->type
->is_builtin
= false;
419 val
->type
->type
= glsl_void_type();
422 val
->type
->type
= glsl_bool_type();
425 val
->type
->type
= glsl_int_type();
428 val
->type
->type
= glsl_float_type();
431 case SpvOpTypeVector
: {
432 const struct glsl_type
*base
=
433 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
434 unsigned elems
= w
[3];
436 assert(glsl_type_is_scalar(base
));
437 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
), elems
);
441 case SpvOpTypeMatrix
: {
442 struct vtn_type
*base
=
443 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
444 unsigned columns
= w
[3];
446 assert(glsl_type_is_vector(base
->type
));
447 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
448 glsl_get_vector_elements(base
->type
),
450 val
->type
->array_element
= base
;
451 val
->type
->row_major
= false;
452 val
->type
->stride
= 0;
456 case SpvOpTypeArray
: {
457 struct vtn_type
*array_element
=
458 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
459 val
->type
->type
= glsl_array_type(array_element
->type
, w
[3]);
460 val
->type
->array_element
= array_element
;
461 val
->type
->stride
= 0;
465 case SpvOpTypeStruct
: {
466 unsigned num_fields
= count
- 2;
467 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
468 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
470 NIR_VLA(struct glsl_struct_field
, fields
, count
);
471 for (unsigned i
= 0; i
< num_fields
; i
++) {
472 /* TODO: Handle decorators */
473 val
->type
->members
[i
] =
474 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
475 fields
[i
].type
= val
->type
->members
[i
]->type
;
476 fields
[i
].name
= ralloc_asprintf(b
, "field%d", i
);
477 fields
[i
].location
= -1;
478 fields
[i
].interpolation
= 0;
479 fields
[i
].centroid
= 0;
480 fields
[i
].sample
= 0;
481 fields
[i
].matrix_layout
= 2;
482 fields
[i
].stream
= -1;
485 struct member_decoration_ctx ctx
= {
490 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
492 const char *name
= val
->name
? val
->name
: "struct";
494 val
->type
->type
= glsl_struct_type(fields
, num_fields
, name
);
498 case SpvOpTypeFunction
: {
499 const struct glsl_type
*return_type
=
500 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
501 NIR_VLA(struct glsl_function_param
, params
, count
- 3);
502 for (unsigned i
= 0; i
< count
- 3; i
++) {
503 params
[i
].type
= vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
->type
;
507 params
[i
].out
= true;
509 val
->type
->type
= glsl_function_type(return_type
, params
, count
- 3);
513 case SpvOpTypePointer
:
514 /* FIXME: For now, we'll just do the really lame thing and return
515 * the same type. The validator should ensure that the proper number
516 * of dereferences happen
518 val
->type
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
521 case SpvOpTypeImage
: {
522 const struct glsl_type
*sampled_type
=
523 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
525 assert(glsl_type_is_vector_or_scalar(sampled_type
));
527 enum glsl_sampler_dim dim
;
528 switch ((SpvDim
)w
[3]) {
529 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
530 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
531 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
532 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
533 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
534 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
536 unreachable("Invalid SPIR-V Sampler dimension");
539 bool is_shadow
= w
[4];
540 bool is_array
= w
[5];
542 assert(w
[6] == 0 && "FIXME: Handl multi-sampled textures");
543 assert(w
[7] == 1 && "FIXME: Add support for non-sampled images");
545 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
546 glsl_get_base_type(sampled_type
));
550 case SpvOpTypeSampledImage
:
551 val
->type
= vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
554 case SpvOpTypeRuntimeArray
:
555 case SpvOpTypeOpaque
:
557 case SpvOpTypeDeviceEvent
:
558 case SpvOpTypeReserveId
:
562 unreachable("Unhandled opcode");
565 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
569 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
570 const uint32_t *w
, unsigned count
)
572 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
573 val
->const_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
574 val
->constant
= ralloc(b
, nir_constant
);
576 case SpvOpConstantTrue
:
577 assert(val
->const_type
== glsl_bool_type());
578 val
->constant
->value
.u
[0] = NIR_TRUE
;
580 case SpvOpConstantFalse
:
581 assert(val
->const_type
== glsl_bool_type());
582 val
->constant
->value
.u
[0] = NIR_FALSE
;
585 assert(glsl_type_is_scalar(val
->const_type
));
586 val
->constant
->value
.u
[0] = w
[3];
588 case SpvOpConstantComposite
: {
589 unsigned elem_count
= count
- 3;
590 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
591 for (unsigned i
= 0; i
< elem_count
; i
++)
592 elems
[i
] = vtn_value(b
, w
[i
+ 3], vtn_value_type_constant
)->constant
;
594 switch (glsl_get_base_type(val
->const_type
)) {
597 case GLSL_TYPE_FLOAT
:
599 if (glsl_type_is_matrix(val
->const_type
)) {
600 unsigned rows
= glsl_get_vector_elements(val
->const_type
);
601 assert(glsl_get_matrix_columns(val
->const_type
) == elem_count
);
602 for (unsigned i
= 0; i
< elem_count
; i
++)
603 for (unsigned j
= 0; j
< rows
; j
++)
604 val
->constant
->value
.u
[rows
* i
+ j
] = elems
[i
]->value
.u
[j
];
606 assert(glsl_type_is_vector(val
->const_type
));
607 assert(glsl_get_vector_elements(val
->const_type
) == elem_count
);
608 for (unsigned i
= 0; i
< elem_count
; i
++)
609 val
->constant
->value
.u
[i
] = elems
[i
]->value
.u
[0];
614 case GLSL_TYPE_STRUCT
:
615 case GLSL_TYPE_ARRAY
:
616 ralloc_steal(val
->constant
, elems
);
617 val
->constant
->elements
= elems
;
621 unreachable("Unsupported type for constants");
627 unreachable("Unhandled opcode");
632 vtn_get_builtin_location(SpvBuiltIn builtin
, int *location
,
633 nir_variable_mode
*mode
)
636 case SpvBuiltInPosition
:
637 *location
= VARYING_SLOT_POS
;
638 *mode
= nir_var_shader_out
;
640 case SpvBuiltInPointSize
:
641 *location
= VARYING_SLOT_PSIZ
;
642 *mode
= nir_var_shader_out
;
644 case SpvBuiltInClipDistance
:
645 *location
= VARYING_SLOT_CLIP_DIST0
; /* XXX CLIP_DIST1? */
646 *mode
= nir_var_shader_in
;
648 case SpvBuiltInCullDistance
:
649 /* XXX figure this out */
650 unreachable("unhandled builtin");
651 case SpvBuiltInVertexId
:
652 /* Vulkan defines VertexID to be zero-based and reserves the new
653 * builtin keyword VertexIndex to indicate the non-zero-based value.
655 *location
= SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
656 *mode
= nir_var_system_value
;
658 case SpvBuiltInInstanceId
:
659 *location
= SYSTEM_VALUE_INSTANCE_ID
;
660 *mode
= nir_var_system_value
;
662 case SpvBuiltInPrimitiveId
:
663 *location
= VARYING_SLOT_PRIMITIVE_ID
;
664 *mode
= nir_var_shader_out
;
666 case SpvBuiltInInvocationId
:
667 *location
= SYSTEM_VALUE_INVOCATION_ID
;
668 *mode
= nir_var_system_value
;
670 case SpvBuiltInLayer
:
671 *location
= VARYING_SLOT_LAYER
;
672 *mode
= nir_var_shader_out
;
674 case SpvBuiltInTessLevelOuter
:
675 case SpvBuiltInTessLevelInner
:
676 case SpvBuiltInTessCoord
:
677 case SpvBuiltInPatchVertices
:
678 unreachable("no tessellation support");
679 case SpvBuiltInFragCoord
:
680 *location
= VARYING_SLOT_POS
;
681 *mode
= nir_var_shader_in
;
683 case SpvBuiltInPointCoord
:
684 *location
= VARYING_SLOT_PNTC
;
685 *mode
= nir_var_shader_out
;
687 case SpvBuiltInFrontFacing
:
688 *location
= VARYING_SLOT_FACE
;
689 *mode
= nir_var_shader_out
;
691 case SpvBuiltInSampleId
:
692 *location
= SYSTEM_VALUE_SAMPLE_ID
;
693 *mode
= nir_var_shader_in
;
695 case SpvBuiltInSamplePosition
:
696 *location
= SYSTEM_VALUE_SAMPLE_POS
;
697 *mode
= nir_var_shader_in
;
699 case SpvBuiltInSampleMask
:
700 *location
= SYSTEM_VALUE_SAMPLE_MASK_IN
; /* XXX out? */
701 *mode
= nir_var_shader_in
;
703 case SpvBuiltInFragColor
:
704 *location
= FRAG_RESULT_COLOR
;
705 *mode
= nir_var_shader_out
;
707 case SpvBuiltInFragDepth
:
708 *location
= FRAG_RESULT_DEPTH
;
709 *mode
= nir_var_shader_out
;
711 case SpvBuiltInHelperInvocation
:
712 unreachable("unsupported builtin"); /* XXX */
714 case SpvBuiltInNumWorkgroups
:
715 case SpvBuiltInWorkgroupSize
:
716 /* these are constants, need to be handled specially */
717 unreachable("unsupported builtin");
718 case SpvBuiltInWorkgroupId
:
719 case SpvBuiltInLocalInvocationId
:
720 case SpvBuiltInGlobalInvocationId
:
721 case SpvBuiltInLocalInvocationIndex
:
722 unreachable("no compute shader support");
724 unreachable("unsupported builtin");
729 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
730 const struct vtn_decoration
*dec
, void *void_var
)
732 assert(val
->value_type
== vtn_value_type_deref
);
733 assert(val
->deref
->deref
.child
== NULL
);
734 assert(val
->deref
->var
== void_var
);
736 nir_variable
*var
= void_var
;
737 switch (dec
->decoration
) {
738 case SpvDecorationRelaxedPrecision
:
739 break; /* FIXME: Do nothing with this for now. */
740 case SpvDecorationSmooth
:
741 var
->data
.interpolation
= INTERP_QUALIFIER_SMOOTH
;
743 case SpvDecorationNoperspective
:
744 var
->data
.interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
746 case SpvDecorationFlat
:
747 var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
749 case SpvDecorationCentroid
:
750 var
->data
.centroid
= true;
752 case SpvDecorationSample
:
753 var
->data
.sample
= true;
755 case SpvDecorationInvariant
:
756 var
->data
.invariant
= true;
758 case SpvDecorationConstant
:
759 assert(var
->constant_initializer
!= NULL
);
760 var
->data
.read_only
= true;
762 case SpvDecorationNonwritable
:
763 var
->data
.read_only
= true;
765 case SpvDecorationLocation
:
766 var
->data
.explicit_location
= true;
767 var
->data
.location
= dec
->literals
[0];
769 case SpvDecorationComponent
:
770 var
->data
.location_frac
= dec
->literals
[0];
772 case SpvDecorationIndex
:
773 var
->data
.explicit_index
= true;
774 var
->data
.index
= dec
->literals
[0];
776 case SpvDecorationBinding
:
777 var
->data
.explicit_binding
= true;
778 var
->data
.binding
= dec
->literals
[0];
780 case SpvDecorationDescriptorSet
:
781 var
->data
.descriptor_set
= dec
->literals
[0];
783 case SpvDecorationBuiltIn
: {
784 nir_variable_mode mode
;
785 vtn_get_builtin_location(dec
->literals
[0], &var
->data
.location
,
787 var
->data
.mode
= mode
;
788 if (mode
== nir_var_shader_in
|| mode
== nir_var_system_value
)
789 var
->data
.read_only
= true;
790 b
->builtins
[dec
->literals
[0]] = var
;
793 case SpvDecorationNoStaticUse
:
794 /* This can safely be ignored */
796 case SpvDecorationRowMajor
:
797 case SpvDecorationColMajor
:
798 case SpvDecorationGLSLShared
:
799 case SpvDecorationPatch
:
800 case SpvDecorationRestrict
:
801 case SpvDecorationAliased
:
802 case SpvDecorationVolatile
:
803 case SpvDecorationCoherent
:
804 case SpvDecorationNonreadable
:
805 case SpvDecorationUniform
:
806 /* This is really nice but we have no use for it right now. */
807 case SpvDecorationCPacked
:
808 case SpvDecorationSaturatedConversion
:
809 case SpvDecorationStream
:
810 case SpvDecorationOffset
:
811 case SpvDecorationXfbBuffer
:
812 case SpvDecorationFuncParamAttr
:
813 case SpvDecorationFPRoundingMode
:
814 case SpvDecorationFPFastMathMode
:
815 case SpvDecorationLinkageAttributes
:
816 case SpvDecorationSpecId
:
819 unreachable("Unhandled variable decoration");
823 static nir_variable
*
824 get_builtin_variable(struct vtn_builder
*b
,
825 const struct glsl_type
*type
,
828 nir_variable
*var
= b
->builtins
[builtin
];
831 var
= ralloc(b
->shader
, nir_variable
);
834 nir_variable_mode mode
;
835 vtn_get_builtin_location(builtin
, &var
->data
.location
, &mode
);
836 var
->data
.mode
= mode
;
837 var
->name
= ralloc_strdup(var
, "builtin");
840 case nir_var_shader_in
:
841 exec_list_push_tail(&b
->shader
->inputs
, &var
->node
);
843 case nir_var_shader_out
:
844 exec_list_push_tail(&b
->shader
->outputs
, &var
->node
);
846 case nir_var_system_value
:
847 exec_list_push_tail(&b
->shader
->system_values
, &var
->node
);
850 unreachable("bad builtin mode");
853 b
->builtins
[builtin
] = var
;
860 vtn_builtin_load(struct vtn_builder
*b
,
861 struct vtn_ssa_value
*val
,
864 assert(glsl_type_is_vector_or_scalar(val
->type
));
866 nir_variable
*var
= get_builtin_variable(b
, val
->type
, builtin
);
868 nir_intrinsic_instr
*load
=
869 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_load_var
);
870 nir_ssa_dest_init(&load
->instr
, &load
->dest
,
871 glsl_get_vector_elements(val
->type
), NULL
);
873 load
->variables
[0] = nir_deref_var_create(load
, var
);
874 load
->num_components
= glsl_get_vector_elements(val
->type
);
875 nir_builder_instr_insert(&b
->nb
, &load
->instr
);
876 val
->def
= &load
->dest
.ssa
;
880 vtn_builtin_store(struct vtn_builder
*b
,
881 struct vtn_ssa_value
*val
,
884 assert(glsl_type_is_vector_or_scalar(val
->type
));
886 nir_variable
*var
= get_builtin_variable(b
, val
->type
, builtin
);
888 nir_intrinsic_instr
*store
=
889 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_store_var
);
891 store
->variables
[0] = nir_deref_var_create(store
, var
);
892 store
->num_components
= glsl_get_vector_elements(val
->type
);
893 store
->src
[0] = nir_src_for_ssa(val
->def
);
894 nir_builder_instr_insert(&b
->nb
, &store
->instr
);
897 static struct vtn_ssa_value
*
898 _vtn_variable_load(struct vtn_builder
*b
,
899 nir_deref_var
*src_deref
, struct vtn_type
*src_type
,
900 nir_deref
*src_deref_tail
)
902 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
903 val
->type
= src_deref_tail
->type
;
905 if (src_type
->is_builtin
) {
906 vtn_builtin_load(b
, val
, src_type
->builtin
);
910 /* The deref tail may contain a deref to select a component of a vector (in
911 * other words, it might not be an actual tail) so we have to save it away
912 * here since we overwrite it later.
914 nir_deref
*old_child
= src_deref_tail
->child
;
916 if (glsl_type_is_vector_or_scalar(val
->type
)) {
917 nir_intrinsic_instr
*load
=
918 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_load_var
);
920 nir_deref_as_var(nir_copy_deref(load
, &src_deref
->deref
));
921 load
->num_components
= glsl_get_vector_elements(val
->type
);
922 nir_ssa_dest_init(&load
->instr
, &load
->dest
, load
->num_components
, NULL
);
924 nir_builder_instr_insert(&b
->nb
, &load
->instr
);
926 if (src_deref
->var
->data
.mode
== nir_var_uniform
&&
927 glsl_get_base_type(val
->type
) == GLSL_TYPE_BOOL
) {
928 /* Uniform boolean loads need to be fixed up since they're defined
929 * to be zero/nonzero rather than NIR_FALSE/NIR_TRUE.
931 val
->def
= nir_ine(&b
->nb
, &load
->dest
.ssa
, nir_imm_int(&b
->nb
, 0));
933 val
->def
= &load
->dest
.ssa
;
935 } else if (glsl_get_base_type(val
->type
) == GLSL_TYPE_ARRAY
||
936 glsl_type_is_matrix(val
->type
)) {
937 unsigned elems
= glsl_get_length(val
->type
);
938 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
940 nir_deref_array
*deref
= nir_deref_array_create(b
);
941 deref
->deref_array_type
= nir_deref_array_type_direct
;
942 deref
->deref
.type
= glsl_get_array_element(val
->type
);
943 src_deref_tail
->child
= &deref
->deref
;
944 for (unsigned i
= 0; i
< elems
; i
++) {
945 deref
->base_offset
= i
;
946 val
->elems
[i
] = _vtn_variable_load(b
, src_deref
,
947 src_type
->array_element
,
951 assert(glsl_get_base_type(val
->type
) == GLSL_TYPE_STRUCT
);
952 unsigned elems
= glsl_get_length(val
->type
);
953 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
955 nir_deref_struct
*deref
= nir_deref_struct_create(b
, 0);
956 src_deref_tail
->child
= &deref
->deref
;
957 for (unsigned i
= 0; i
< elems
; i
++) {
959 deref
->deref
.type
= glsl_get_struct_field(val
->type
, i
);
960 val
->elems
[i
] = _vtn_variable_load(b
, src_deref
,
961 src_type
->members
[i
],
966 src_deref_tail
->child
= old_child
;
972 _vtn_variable_store(struct vtn_builder
*b
, struct vtn_type
*dest_type
,
973 nir_deref_var
*dest_deref
, nir_deref
*dest_deref_tail
,
974 struct vtn_ssa_value
*src
)
976 if (dest_type
->is_builtin
) {
977 vtn_builtin_store(b
, src
, dest_type
->builtin
);
981 nir_deref
*old_child
= dest_deref_tail
->child
;
983 if (glsl_type_is_vector_or_scalar(src
->type
)) {
984 nir_intrinsic_instr
*store
=
985 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_store_var
);
986 store
->variables
[0] =
987 nir_deref_as_var(nir_copy_deref(store
, &dest_deref
->deref
));
988 store
->num_components
= glsl_get_vector_elements(src
->type
);
989 store
->src
[0] = nir_src_for_ssa(src
->def
);
991 nir_builder_instr_insert(&b
->nb
, &store
->instr
);
992 } else if (glsl_get_base_type(src
->type
) == GLSL_TYPE_ARRAY
||
993 glsl_type_is_matrix(src
->type
)) {
994 unsigned elems
= glsl_get_length(src
->type
);
996 nir_deref_array
*deref
= nir_deref_array_create(b
);
997 deref
->deref_array_type
= nir_deref_array_type_direct
;
998 deref
->deref
.type
= glsl_get_array_element(src
->type
);
999 dest_deref_tail
->child
= &deref
->deref
;
1000 for (unsigned i
= 0; i
< elems
; i
++) {
1001 deref
->base_offset
= i
;
1002 _vtn_variable_store(b
, dest_type
->array_element
, dest_deref
,
1003 &deref
->deref
, src
->elems
[i
]);
1006 assert(glsl_get_base_type(src
->type
) == GLSL_TYPE_STRUCT
);
1007 unsigned elems
= glsl_get_length(src
->type
);
1009 nir_deref_struct
*deref
= nir_deref_struct_create(b
, 0);
1010 dest_deref_tail
->child
= &deref
->deref
;
1011 for (unsigned i
= 0; i
< elems
; i
++) {
1013 deref
->deref
.type
= glsl_get_struct_field(src
->type
, i
);
1014 _vtn_variable_store(b
, dest_type
->members
[i
], dest_deref
,
1015 &deref
->deref
, src
->elems
[i
]);
1019 dest_deref_tail
->child
= old_child
;
1022 static struct vtn_ssa_value
*
1023 _vtn_block_load(struct vtn_builder
*b
, nir_intrinsic_op op
,
1024 unsigned set
, nir_ssa_def
*binding
,
1025 unsigned offset
, nir_ssa_def
*indirect
,
1026 struct vtn_type
*type
)
1028 struct vtn_ssa_value
*val
= ralloc(b
, struct vtn_ssa_value
);
1029 val
->type
= type
->type
;
1030 val
->transposed
= NULL
;
1031 if (glsl_type_is_vector_or_scalar(type
->type
)) {
1032 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(b
->shader
, op
);
1033 load
->num_components
= glsl_get_vector_elements(type
->type
);
1034 load
->const_index
[0] = set
;
1035 load
->src
[0] = nir_src_for_ssa(binding
);
1036 load
->const_index
[1] = offset
;
1038 load
->src
[1] = nir_src_for_ssa(indirect
);
1039 nir_ssa_dest_init(&load
->instr
, &load
->dest
, load
->num_components
, NULL
);
1040 nir_builder_instr_insert(&b
->nb
, &load
->instr
);
1041 val
->def
= &load
->dest
.ssa
;
1043 unsigned elems
= glsl_get_length(type
->type
);
1044 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1045 if (glsl_type_is_struct(type
->type
)) {
1046 for (unsigned i
= 0; i
< elems
; i
++) {
1047 val
->elems
[i
] = _vtn_block_load(b
, op
, set
, binding
,
1048 offset
+ type
->offsets
[i
],
1049 indirect
, type
->members
[i
]);
1052 for (unsigned i
= 0; i
< elems
; i
++) {
1053 val
->elems
[i
] = _vtn_block_load(b
, op
, set
, binding
,
1054 offset
+ i
* type
->stride
,
1055 indirect
, type
->array_element
);
1063 static struct vtn_ssa_value
*
1064 vtn_block_load(struct vtn_builder
*b
, nir_deref_var
*src
,
1065 struct vtn_type
*type
, nir_deref
*src_tail
)
1067 unsigned set
= src
->var
->data
.descriptor_set
;
1069 nir_ssa_def
*binding
= nir_imm_int(&b
->nb
, src
->var
->data
.binding
);
1070 nir_deref
*deref
= &src
->deref
;
1072 /* The block variable may be an array, in which case the array index adds
1073 * an offset to the binding. Figure out that index now.
1076 if (deref
->child
->deref_type
== nir_deref_type_array
) {
1077 deref
= deref
->child
;
1078 type
= type
->array_element
;
1079 nir_deref_array
*deref_array
= nir_deref_as_array(deref
);
1080 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
) {
1081 binding
= nir_imm_int(&b
->nb
, src
->var
->data
.binding
+
1082 deref_array
->base_offset
);
1084 binding
= nir_iadd(&b
->nb
, binding
, deref_array
->indirect
.ssa
);
1088 unsigned offset
= 0;
1089 nir_ssa_def
*indirect
= NULL
;
1090 while (deref
!= src_tail
) {
1091 deref
= deref
->child
;
1092 switch (deref
->deref_type
) {
1093 case nir_deref_type_array
: {
1094 nir_deref_array
*deref_array
= nir_deref_as_array(deref
);
1095 if (deref_array
->deref_array_type
== nir_deref_array_type_direct
) {
1096 offset
+= type
->stride
* deref_array
->base_offset
;
1098 nir_ssa_def
*offset
= nir_imul(&b
->nb
, deref_array
->indirect
.ssa
,
1099 nir_imm_int(&b
->nb
, type
->stride
));
1100 indirect
= indirect
? nir_iadd(&b
->nb
, indirect
, offset
) : offset
;
1102 type
= type
->array_element
;
1106 case nir_deref_type_struct
: {
1107 nir_deref_struct
*deref_struct
= nir_deref_as_struct(deref
);
1108 offset
+= type
->offsets
[deref_struct
->index
];
1109 type
= type
->members
[deref_struct
->index
];
1114 unreachable("unknown deref type");
1119 nir_intrinsic_op op
= indirect
? nir_intrinsic_load_ubo_indirect
1120 : nir_intrinsic_load_ubo
;
1122 return _vtn_block_load(b
, op
, set
, binding
, offset
, indirect
, type
);
1126 * Gets the NIR-level deref tail, which may have as a child an array deref
1127 * selecting which component due to OpAccessChain supporting per-component
1128 * indexing in SPIR-V.
1132 get_deref_tail(nir_deref_var
*deref
)
1134 nir_deref
*cur
= &deref
->deref
;
1135 while (!glsl_type_is_vector_or_scalar(cur
->type
) && cur
->child
)
1141 static nir_ssa_def
*vtn_vector_extract(struct vtn_builder
*b
,
1142 nir_ssa_def
*src
, unsigned index
);
1144 static nir_ssa_def
*vtn_vector_extract_dynamic(struct vtn_builder
*b
,
1146 nir_ssa_def
*index
);
1148 static struct vtn_ssa_value
*
1149 vtn_variable_load(struct vtn_builder
*b
, nir_deref_var
*src
,
1150 struct vtn_type
*src_type
)
1152 nir_deref
*src_tail
= get_deref_tail(src
);
1154 struct vtn_ssa_value
*val
;
1155 if (src
->var
->interface_type
&& src
->var
->data
.mode
== nir_var_uniform
)
1156 val
= vtn_block_load(b
, src
, src_type
, src_tail
);
1158 val
= _vtn_variable_load(b
, src
, src_type
, src_tail
);
1160 if (src_tail
->child
) {
1161 nir_deref_array
*vec_deref
= nir_deref_as_array(src_tail
->child
);
1162 assert(vec_deref
->deref
.child
== NULL
);
1163 val
->type
= vec_deref
->deref
.type
;
1164 if (vec_deref
->deref_array_type
== nir_deref_array_type_direct
)
1165 val
->def
= vtn_vector_extract(b
, val
->def
, vec_deref
->base_offset
);
1167 val
->def
= vtn_vector_extract_dynamic(b
, val
->def
,
1168 vec_deref
->indirect
.ssa
);
1174 static nir_ssa_def
* vtn_vector_insert(struct vtn_builder
*b
,
1175 nir_ssa_def
*src
, nir_ssa_def
*insert
,
1178 static nir_ssa_def
* vtn_vector_insert_dynamic(struct vtn_builder
*b
,
1180 nir_ssa_def
*insert
,
1181 nir_ssa_def
*index
);
1183 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
1184 nir_deref_var
*dest
, struct vtn_type
*dest_type
)
1186 nir_deref
*dest_tail
= get_deref_tail(dest
);
1187 if (dest_tail
->child
) {
1188 struct vtn_ssa_value
*val
= _vtn_variable_load(b
, dest
, dest_type
,
1190 nir_deref_array
*deref
= nir_deref_as_array(dest_tail
->child
);
1191 assert(deref
->deref
.child
== NULL
);
1192 if (deref
->deref_array_type
== nir_deref_array_type_direct
)
1193 val
->def
= vtn_vector_insert(b
, val
->def
, src
->def
,
1194 deref
->base_offset
);
1196 val
->def
= vtn_vector_insert_dynamic(b
, val
->def
, src
->def
,
1197 deref
->indirect
.ssa
);
1198 _vtn_variable_store(b
, dest_type
, dest
, dest_tail
, val
);
1200 _vtn_variable_store(b
, dest_type
, dest
, dest_tail
, src
);
1205 vtn_variable_copy(struct vtn_builder
*b
, nir_deref_var
*src
,
1206 nir_deref_var
*dest
, struct vtn_type
*type
)
1208 nir_deref
*src_tail
= get_deref_tail(src
);
1210 if (src_tail
->child
|| src
->var
->interface_type
) {
1211 assert(get_deref_tail(dest
)->child
);
1212 struct vtn_ssa_value
*val
= vtn_variable_load(b
, src
, type
);
1213 vtn_variable_store(b
, val
, dest
, type
);
1215 nir_intrinsic_instr
*copy
=
1216 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_copy_var
);
1217 copy
->variables
[0] = nir_deref_as_var(nir_copy_deref(copy
, &dest
->deref
));
1218 copy
->variables
[1] = nir_deref_as_var(nir_copy_deref(copy
, &src
->deref
));
1220 nir_builder_instr_insert(&b
->nb
, ©
->instr
);
1225 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
1226 const uint32_t *w
, unsigned count
)
1229 case SpvOpVariable
: {
1230 struct vtn_type
*type
=
1231 vtn_value(b
, w
[1], vtn_value_type_type
)->type
;
1232 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_deref
);
1234 nir_variable
*var
= ralloc(b
->shader
, nir_variable
);
1236 var
->type
= type
->type
;
1237 var
->name
= ralloc_strdup(var
, val
->name
);
1239 bool builtin_block
= false;
1241 var
->interface_type
= type
->type
;
1242 builtin_block
= type
->builtin_block
;
1243 } else if (glsl_type_is_array(type
->type
) &&
1244 (type
->array_element
->block
||
1245 type
->array_element
->buffer_block
)) {
1246 var
->interface_type
= type
->array_element
->type
;
1247 builtin_block
= type
->array_element
->builtin_block
;
1249 var
->interface_type
= NULL
;
1252 switch ((SpvStorageClass
)w
[3]) {
1253 case SpvStorageClassUniform
:
1254 case SpvStorageClassUniformConstant
:
1255 var
->data
.mode
= nir_var_uniform
;
1256 var
->data
.read_only
= true;
1258 case SpvStorageClassInput
:
1259 var
->data
.mode
= nir_var_shader_in
;
1260 var
->data
.read_only
= true;
1262 case SpvStorageClassOutput
:
1263 var
->data
.mode
= nir_var_shader_out
;
1265 case SpvStorageClassPrivateGlobal
:
1266 var
->data
.mode
= nir_var_global
;
1268 case SpvStorageClassFunction
:
1269 var
->data
.mode
= nir_var_local
;
1271 case SpvStorageClassWorkgroupLocal
:
1272 case SpvStorageClassWorkgroupGlobal
:
1273 case SpvStorageClassGeneric
:
1274 case SpvStorageClassAtomicCounter
:
1276 unreachable("Unhandled variable storage class");
1281 var
->constant_initializer
=
1282 vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
1285 val
->deref
= nir_deref_var_create(b
, var
);
1286 val
->deref_type
= type
;
1288 if (b
->execution_model
== SpvExecutionModelFragment
&&
1289 var
->data
.mode
== nir_var_shader_out
) {
1290 var
->data
.location
+= FRAG_RESULT_DATA0
;
1291 } else if (b
->execution_model
== SpvExecutionModelVertex
&&
1292 var
->data
.mode
== nir_var_shader_in
) {
1293 var
->data
.location
+= VERT_ATTRIB_GENERIC0
;
1294 } else if (var
->data
.mode
== nir_var_shader_in
||
1295 var
->data
.mode
== nir_var_shader_out
) {
1296 var
->data
.location
+= VARYING_SLOT_VAR0
;
1299 /* We handle decorations last because decorations might cause us to
1300 * over-write other things such as the variable's location and we want
1301 * those changes to stick.
1303 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
1305 /* If this was a uniform block, then we're not going to actually use the
1306 * variable (we're only going to use it to compute offsets), so don't
1307 * declare it in the shader.
1309 if (var
->data
.mode
== nir_var_uniform
&& var
->interface_type
)
1312 /* Builtin blocks are lowered to individual variables during SPIR-V ->
1313 * NIR, so don't declare them either.
1318 switch (var
->data
.mode
) {
1319 case nir_var_shader_in
:
1320 exec_list_push_tail(&b
->shader
->inputs
, &var
->node
);
1322 case nir_var_shader_out
:
1323 exec_list_push_tail(&b
->shader
->outputs
, &var
->node
);
1325 case nir_var_global
:
1326 exec_list_push_tail(&b
->shader
->globals
, &var
->node
);
1329 exec_list_push_tail(&b
->impl
->locals
, &var
->node
);
1331 case nir_var_uniform
:
1332 exec_list_push_tail(&b
->shader
->uniforms
, &var
->node
);
1334 case nir_var_system_value
:
1335 exec_list_push_tail(&b
->shader
->system_values
, &var
->node
);
1341 case SpvOpAccessChain
:
1342 case SpvOpInBoundsAccessChain
: {
1343 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_deref
);
1344 nir_deref_var
*base
= vtn_value(b
, w
[3], vtn_value_type_deref
)->deref
;
1345 val
->deref
= nir_deref_as_var(nir_copy_deref(b
, &base
->deref
));
1346 struct vtn_type
*deref_type
= vtn_value(b
, w
[3], vtn_value_type_deref
)->deref_type
;
1348 nir_deref
*tail
= &val
->deref
->deref
;
1352 for (unsigned i
= 0; i
< count
- 4; i
++) {
1353 assert(w
[i
+ 4] < b
->value_id_bound
);
1354 struct vtn_value
*idx_val
= &b
->values
[w
[i
+ 4]];
1356 enum glsl_base_type base_type
= glsl_get_base_type(tail
->type
);
1357 switch (base_type
) {
1358 case GLSL_TYPE_UINT
:
1360 case GLSL_TYPE_FLOAT
:
1361 case GLSL_TYPE_DOUBLE
:
1362 case GLSL_TYPE_BOOL
:
1363 case GLSL_TYPE_ARRAY
: {
1364 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
1365 if (base_type
== GLSL_TYPE_ARRAY
||
1366 glsl_type_is_matrix(tail
->type
)) {
1367 deref_type
= deref_type
->array_element
;
1369 assert(glsl_type_is_vector(tail
->type
));
1370 deref_type
= ralloc(b
, struct vtn_type
);
1371 deref_type
->type
= glsl_scalar_type(base_type
);
1374 deref_arr
->deref
.type
= deref_type
->type
;
1376 if (idx_val
->value_type
== vtn_value_type_constant
) {
1377 unsigned idx
= idx_val
->constant
->value
.u
[0];
1378 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
1379 deref_arr
->base_offset
= idx
;
1381 assert(idx_val
->value_type
== vtn_value_type_ssa
);
1382 deref_arr
->deref_array_type
= nir_deref_array_type_indirect
;
1383 deref_arr
->base_offset
= 0;
1384 deref_arr
->indirect
=
1385 nir_src_for_ssa(vtn_ssa_value(b
, w
[1])->def
);
1387 tail
->child
= &deref_arr
->deref
;
1391 case GLSL_TYPE_STRUCT
: {
1392 assert(idx_val
->value_type
== vtn_value_type_constant
);
1393 unsigned idx
= idx_val
->constant
->value
.u
[0];
1394 deref_type
= deref_type
->members
[idx
];
1395 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, idx
);
1396 deref_struct
->deref
.type
= deref_type
->type
;
1397 tail
->child
= &deref_struct
->deref
;
1401 unreachable("Invalid type for deref");
1406 /* For uniform blocks, we don't resolve the access chain until we
1407 * actually access the variable, so we need to keep around the original
1408 * type of the variable.
1410 if (base
->var
->interface_type
&& base
->var
->data
.mode
== nir_var_uniform
)
1411 val
->deref_type
= vtn_value(b
, w
[3], vtn_value_type_deref
)->deref_type
;
1413 val
->deref_type
= deref_type
;
1419 case SpvOpCopyMemory
: {
1420 nir_deref_var
*dest
= vtn_value(b
, w
[1], vtn_value_type_deref
)->deref
;
1421 nir_deref_var
*src
= vtn_value(b
, w
[2], vtn_value_type_deref
)->deref
;
1422 struct vtn_type
*type
=
1423 vtn_value(b
, w
[1], vtn_value_type_deref
)->deref_type
;
1425 vtn_variable_copy(b
, src
, dest
, type
);
1430 nir_deref_var
*src
= vtn_value(b
, w
[3], vtn_value_type_deref
)->deref
;
1431 struct vtn_type
*src_type
=
1432 vtn_value(b
, w
[3], vtn_value_type_deref
)->deref_type
;
1434 if (glsl_get_base_type(src_type
->type
) == GLSL_TYPE_SAMPLER
) {
1435 vtn_push_value(b
, w
[2], vtn_value_type_deref
)->deref
= src
;
1439 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1440 val
->ssa
= vtn_variable_load(b
, src
, src_type
);
1445 nir_deref_var
*dest
= vtn_value(b
, w
[1], vtn_value_type_deref
)->deref
;
1446 struct vtn_type
*dest_type
=
1447 vtn_value(b
, w
[1], vtn_value_type_deref
)->deref_type
;
1448 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
1449 vtn_variable_store(b
, src
, dest
, dest_type
);
1453 case SpvOpCopyMemorySized
:
1454 case SpvOpArrayLength
:
1455 case SpvOpImageTexelPointer
:
1457 unreachable("Unhandled opcode");
1462 vtn_handle_function_call(struct vtn_builder
*b
, SpvOp opcode
,
1463 const uint32_t *w
, unsigned count
)
1465 unreachable("Unhandled opcode");
1468 static struct vtn_ssa_value
*
1469 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1471 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1474 if (!glsl_type_is_vector_or_scalar(type
)) {
1475 unsigned elems
= glsl_get_length(type
);
1476 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1477 for (unsigned i
= 0; i
< elems
; i
++) {
1478 const struct glsl_type
*child_type
;
1480 switch (glsl_get_base_type(type
)) {
1482 case GLSL_TYPE_UINT
:
1483 case GLSL_TYPE_BOOL
:
1484 case GLSL_TYPE_FLOAT
:
1485 case GLSL_TYPE_DOUBLE
:
1486 child_type
= glsl_get_column_type(type
);
1488 case GLSL_TYPE_ARRAY
:
1489 child_type
= glsl_get_array_element(type
);
1491 case GLSL_TYPE_STRUCT
:
1492 child_type
= glsl_get_struct_field(type
, i
);
1495 unreachable("unkown base type");
1498 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
1506 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
1509 src
.src
= nir_src_for_ssa(vtn_value(b
, index
, vtn_value_type_ssa
)->ssa
->def
);
1510 src
.src_type
= type
;
1515 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
1516 const uint32_t *w
, unsigned count
)
1518 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1519 nir_deref_var
*sampler
= vtn_value(b
, w
[3], vtn_value_type_deref
)->deref
;
1521 nir_tex_src srcs
[8]; /* 8 should be enough */
1522 nir_tex_src
*p
= srcs
;
1526 unsigned coord_components
= 0;
1528 case SpvOpImageSampleImplicitLod
:
1529 case SpvOpImageSampleExplicitLod
:
1530 case SpvOpImageSampleDrefImplicitLod
:
1531 case SpvOpImageSampleDrefExplicitLod
:
1532 case SpvOpImageSampleProjImplicitLod
:
1533 case SpvOpImageSampleProjExplicitLod
:
1534 case SpvOpImageSampleProjDrefImplicitLod
:
1535 case SpvOpImageSampleProjDrefExplicitLod
:
1536 case SpvOpImageFetch
:
1537 case SpvOpImageGather
:
1538 case SpvOpImageDrefGather
:
1539 case SpvOpImageQueryLod
: {
1540 /* All these types have the coordinate as their first real argument */
1541 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, w
[idx
++]);
1542 coord_components
= glsl_get_vector_elements(coord
->type
);
1543 p
->src
= nir_src_for_ssa(coord
->def
);
1544 p
->src_type
= nir_tex_src_coord
;
1555 case SpvOpImageSampleImplicitLod
:
1556 texop
= nir_texop_tex
;
1559 case SpvOpImageSampleExplicitLod
:
1560 case SpvOpImageSampleDrefImplicitLod
:
1561 case SpvOpImageSampleDrefExplicitLod
:
1562 case SpvOpImageSampleProjImplicitLod
:
1563 case SpvOpImageSampleProjExplicitLod
:
1564 case SpvOpImageSampleProjDrefImplicitLod
:
1565 case SpvOpImageSampleProjDrefExplicitLod
:
1566 case SpvOpImageFetch
:
1567 case SpvOpImageGather
:
1568 case SpvOpImageDrefGather
:
1569 case SpvOpImageQuerySizeLod
:
1570 case SpvOpImageQuerySize
:
1571 case SpvOpImageQueryLod
:
1572 case SpvOpImageQueryLevels
:
1573 case SpvOpImageQuerySamples
:
1575 unreachable("Unhandled opcode");
1578 /* From now on, the remaining sources are "Optional Image Operands." */
1580 /* XXX handle these (bias, lod, etc.) */
1585 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
1587 const struct glsl_type
*sampler_type
= nir_deref_tail(&sampler
->deref
)->type
;
1588 instr
->sampler_dim
= glsl_get_sampler_dim(sampler_type
);
1590 switch (glsl_get_sampler_result_type(sampler_type
)) {
1591 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
1592 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
1593 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_unsigned
; break;
1594 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
1596 unreachable("Invalid base type for sampler result");
1600 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1601 instr
->coord_components
= coord_components
;
1602 instr
->is_array
= glsl_sampler_type_is_array(sampler_type
);
1603 instr
->is_shadow
= glsl_sampler_type_is_shadow(sampler_type
);
1605 instr
->sampler
= nir_deref_as_var(nir_copy_deref(instr
, &sampler
->deref
));
1607 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 4, NULL
);
1608 val
->ssa
= vtn_create_ssa_value(b
, glsl_vector_type(GLSL_TYPE_FLOAT
, 4));
1609 val
->ssa
->def
= &instr
->dest
.ssa
;
1611 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
1615 static nir_alu_instr
*
1616 create_vec(void *mem_ctx
, unsigned num_components
)
1619 switch (num_components
) {
1620 case 1: op
= nir_op_fmov
; break;
1621 case 2: op
= nir_op_vec2
; break;
1622 case 3: op
= nir_op_vec3
; break;
1623 case 4: op
= nir_op_vec4
; break;
1624 default: unreachable("bad vector size");
1627 nir_alu_instr
*vec
= nir_alu_instr_create(mem_ctx
, op
);
1628 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
, NULL
);
1629 vec
->dest
.write_mask
= (1 << num_components
) - 1;
1634 static struct vtn_ssa_value
*
1635 vtn_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
1637 if (src
->transposed
)
1638 return src
->transposed
;
1640 struct vtn_ssa_value
*dest
=
1641 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
1643 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
1644 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
));
1645 if (glsl_type_is_vector_or_scalar(src
->type
)) {
1646 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
1647 vec
->src
[0].swizzle
[0] = i
;
1649 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
1650 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
1651 vec
->src
[j
].swizzle
[0] = i
;
1654 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1655 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
1658 dest
->transposed
= src
;
1664 * Normally, column vectors in SPIR-V correspond to a single NIR SSA
1665 * definition. But for matrix multiplies, we want to do one routine for
1666 * multiplying a matrix by a matrix and then pretend that vectors are matrices
1667 * with one column. So we "wrap" these things, and unwrap the result before we
1671 static struct vtn_ssa_value
*
1672 vtn_wrap_matrix(struct vtn_builder
*b
, struct vtn_ssa_value
*val
)
1677 if (glsl_type_is_matrix(val
->type
))
1680 struct vtn_ssa_value
*dest
= rzalloc(b
, struct vtn_ssa_value
);
1681 dest
->type
= val
->type
;
1682 dest
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, 1);
1683 dest
->elems
[0] = val
;
1688 static struct vtn_ssa_value
*
1689 vtn_unwrap_matrix(struct vtn_ssa_value
*val
)
1691 if (glsl_type_is_matrix(val
->type
))
1694 return val
->elems
[0];
1697 static struct vtn_ssa_value
*
1698 vtn_matrix_multiply(struct vtn_builder
*b
,
1699 struct vtn_ssa_value
*_src0
, struct vtn_ssa_value
*_src1
)
1702 struct vtn_ssa_value
*src0
= vtn_wrap_matrix(b
, _src0
);
1703 struct vtn_ssa_value
*src1
= vtn_wrap_matrix(b
, _src1
);
1704 struct vtn_ssa_value
*src0_transpose
= vtn_wrap_matrix(b
, _src0
->transposed
);
1705 struct vtn_ssa_value
*src1_transpose
= vtn_wrap_matrix(b
, _src1
->transposed
);
1707 unsigned src0_rows
= glsl_get_vector_elements(src0
->type
);
1708 unsigned src0_columns
= glsl_get_matrix_columns(src0
->type
);
1709 unsigned src1_columns
= glsl_get_matrix_columns(src1
->type
);
1711 struct vtn_ssa_value
*dest
=
1712 vtn_create_ssa_value(b
, glsl_matrix_type(glsl_get_base_type(src0
->type
),
1713 src0_rows
, src1_columns
));
1715 dest
= vtn_wrap_matrix(b
, dest
);
1717 bool transpose_result
= false;
1718 if (src0_transpose
&& src1_transpose
) {
1719 /* transpose(A) * transpose(B) = transpose(B * A) */
1720 src1
= src0_transpose
;
1721 src0
= src1_transpose
;
1722 src0_transpose
= NULL
;
1723 src1_transpose
= NULL
;
1724 transpose_result
= true;
1727 if (src0_transpose
&& !src1_transpose
&&
1728 glsl_get_base_type(src0
->type
) == GLSL_TYPE_FLOAT
) {
1729 /* We already have the rows of src0 and the columns of src1 available,
1730 * so we can just take the dot product of each row with each column to
1734 for (unsigned i
= 0; i
< src1_columns
; i
++) {
1735 nir_alu_instr
*vec
= create_vec(b
, src0_rows
);
1736 for (unsigned j
= 0; j
< src0_rows
; j
++) {
1738 nir_src_for_ssa(nir_fdot(&b
->nb
, src0_transpose
->elems
[j
]->def
,
1739 src1
->elems
[i
]->def
));
1742 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1743 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
1746 /* We don't handle the case where src1 is transposed but not src0, since
1747 * the general case only uses individual components of src1 so the
1748 * optimizer should chew through the transpose we emitted for src1.
1751 for (unsigned i
= 0; i
< src1_columns
; i
++) {
1752 /* dest[i] = sum(src0[j] * src1[i][j] for all j) */
1753 dest
->elems
[i
]->def
=
1754 nir_fmul(&b
->nb
, src0
->elems
[0]->def
,
1755 vtn_vector_extract(b
, src1
->elems
[i
]->def
, 0));
1756 for (unsigned j
= 1; j
< src0_columns
; j
++) {
1757 dest
->elems
[i
]->def
=
1758 nir_fadd(&b
->nb
, dest
->elems
[i
]->def
,
1759 nir_fmul(&b
->nb
, src0
->elems
[j
]->def
,
1760 vtn_vector_extract(b
,
1761 src1
->elems
[i
]->def
, j
)));
1766 dest
= vtn_unwrap_matrix(dest
);
1768 if (transpose_result
)
1769 dest
= vtn_transpose(b
, dest
);
1774 static struct vtn_ssa_value
*
1775 vtn_mat_times_scalar(struct vtn_builder
*b
,
1776 struct vtn_ssa_value
*mat
,
1777 nir_ssa_def
*scalar
)
1779 struct vtn_ssa_value
*dest
= vtn_create_ssa_value(b
, mat
->type
);
1780 for (unsigned i
= 0; i
< glsl_get_matrix_columns(mat
->type
); i
++) {
1781 if (glsl_get_base_type(mat
->type
) == GLSL_TYPE_FLOAT
)
1782 dest
->elems
[i
]->def
= nir_fmul(&b
->nb
, mat
->elems
[i
]->def
, scalar
);
1784 dest
->elems
[i
]->def
= nir_imul(&b
->nb
, mat
->elems
[i
]->def
, scalar
);
1791 vtn_handle_matrix_alu(struct vtn_builder
*b
, SpvOp opcode
,
1792 const uint32_t *w
, unsigned count
)
1794 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1797 case SpvOpTranspose
: {
1798 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[3]);
1799 val
->ssa
= vtn_transpose(b
, src
);
1803 case SpvOpOuterProduct
: {
1804 struct vtn_ssa_value
*src0
= vtn_ssa_value(b
, w
[3]);
1805 struct vtn_ssa_value
*src1
= vtn_ssa_value(b
, w
[4]);
1807 val
->ssa
= vtn_matrix_multiply(b
, src0
, vtn_transpose(b
, src1
));
1811 case SpvOpMatrixTimesScalar
: {
1812 struct vtn_ssa_value
*mat
= vtn_ssa_value(b
, w
[3]);
1813 struct vtn_ssa_value
*scalar
= vtn_ssa_value(b
, w
[4]);
1815 if (mat
->transposed
) {
1816 val
->ssa
= vtn_transpose(b
, vtn_mat_times_scalar(b
, mat
->transposed
,
1819 val
->ssa
= vtn_mat_times_scalar(b
, mat
, scalar
->def
);
1824 case SpvOpVectorTimesMatrix
:
1825 case SpvOpMatrixTimesVector
:
1826 case SpvOpMatrixTimesMatrix
: {
1827 struct vtn_ssa_value
*src0
= vtn_ssa_value(b
, w
[3]);
1828 struct vtn_ssa_value
*src1
= vtn_ssa_value(b
, w
[4]);
1830 val
->ssa
= vtn_matrix_multiply(b
, src0
, src1
);
1834 default: unreachable("unknown matrix opcode");
1839 vtn_handle_alu(struct vtn_builder
*b
, SpvOp opcode
,
1840 const uint32_t *w
, unsigned count
)
1842 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1843 const struct glsl_type
*type
=
1844 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
1845 val
->ssa
= vtn_create_ssa_value(b
, type
);
1847 /* Collect the various SSA sources */
1848 unsigned num_inputs
= count
- 3;
1849 nir_ssa_def
*src
[4];
1850 for (unsigned i
= 0; i
< num_inputs
; i
++)
1851 src
[i
] = vtn_ssa_value(b
, w
[i
+ 3])->def
;
1853 /* Indicates that the first two arguments should be swapped. This is
1854 * used for implementing greater-than and less-than-or-equal.
1860 /* Basic ALU operations */
1861 case SpvOpSNegate
: op
= nir_op_ineg
; break;
1862 case SpvOpFNegate
: op
= nir_op_fneg
; break;
1863 case SpvOpNot
: op
= nir_op_inot
; break;
1866 switch (src
[0]->num_components
) {
1867 case 1: op
= nir_op_imov
; break;
1868 case 2: op
= nir_op_bany2
; break;
1869 case 3: op
= nir_op_bany3
; break;
1870 case 4: op
= nir_op_bany4
; break;
1875 switch (src
[0]->num_components
) {
1876 case 1: op
= nir_op_imov
; break;
1877 case 2: op
= nir_op_ball2
; break;
1878 case 3: op
= nir_op_ball3
; break;
1879 case 4: op
= nir_op_ball4
; break;
1883 case SpvOpIAdd
: op
= nir_op_iadd
; break;
1884 case SpvOpFAdd
: op
= nir_op_fadd
; break;
1885 case SpvOpISub
: op
= nir_op_isub
; break;
1886 case SpvOpFSub
: op
= nir_op_fsub
; break;
1887 case SpvOpIMul
: op
= nir_op_imul
; break;
1888 case SpvOpFMul
: op
= nir_op_fmul
; break;
1889 case SpvOpUDiv
: op
= nir_op_udiv
; break;
1890 case SpvOpSDiv
: op
= nir_op_idiv
; break;
1891 case SpvOpFDiv
: op
= nir_op_fdiv
; break;
1892 case SpvOpUMod
: op
= nir_op_umod
; break;
1893 case SpvOpSMod
: op
= nir_op_umod
; break; /* FIXME? */
1894 case SpvOpFMod
: op
= nir_op_fmod
; break;
1897 assert(src
[0]->num_components
== src
[1]->num_components
);
1898 switch (src
[0]->num_components
) {
1899 case 1: op
= nir_op_fmul
; break;
1900 case 2: op
= nir_op_fdot2
; break;
1901 case 3: op
= nir_op_fdot3
; break;
1902 case 4: op
= nir_op_fdot4
; break;
1906 case SpvOpShiftRightLogical
: op
= nir_op_ushr
; break;
1907 case SpvOpShiftRightArithmetic
: op
= nir_op_ishr
; break;
1908 case SpvOpShiftLeftLogical
: op
= nir_op_ishl
; break;
1909 case SpvOpLogicalOr
: op
= nir_op_ior
; break;
1910 case SpvOpLogicalEqual
: op
= nir_op_ieq
; break;
1911 case SpvOpLogicalNotEqual
: op
= nir_op_ine
; break;
1912 case SpvOpLogicalAnd
: op
= nir_op_iand
; break;
1913 case SpvOpBitwiseOr
: op
= nir_op_ior
; break;
1914 case SpvOpBitwiseXor
: op
= nir_op_ixor
; break;
1915 case SpvOpBitwiseAnd
: op
= nir_op_iand
; break;
1916 case SpvOpSelect
: op
= nir_op_bcsel
; break;
1917 case SpvOpIEqual
: op
= nir_op_ieq
; break;
1919 /* Comparisons: (TODO: How do we want to handled ordered/unordered?) */
1920 case SpvOpFOrdEqual
: op
= nir_op_feq
; break;
1921 case SpvOpFUnordEqual
: op
= nir_op_feq
; break;
1922 case SpvOpINotEqual
: op
= nir_op_ine
; break;
1923 case SpvOpFOrdNotEqual
: op
= nir_op_fne
; break;
1924 case SpvOpFUnordNotEqual
: op
= nir_op_fne
; break;
1925 case SpvOpULessThan
: op
= nir_op_ult
; break;
1926 case SpvOpSLessThan
: op
= nir_op_ilt
; break;
1927 case SpvOpFOrdLessThan
: op
= nir_op_flt
; break;
1928 case SpvOpFUnordLessThan
: op
= nir_op_flt
; break;
1929 case SpvOpUGreaterThan
: op
= nir_op_ult
; swap
= true; break;
1930 case SpvOpSGreaterThan
: op
= nir_op_ilt
; swap
= true; break;
1931 case SpvOpFOrdGreaterThan
: op
= nir_op_flt
; swap
= true; break;
1932 case SpvOpFUnordGreaterThan
: op
= nir_op_flt
; swap
= true; break;
1933 case SpvOpULessThanEqual
: op
= nir_op_uge
; swap
= true; break;
1934 case SpvOpSLessThanEqual
: op
= nir_op_ige
; swap
= true; break;
1935 case SpvOpFOrdLessThanEqual
: op
= nir_op_fge
; swap
= true; break;
1936 case SpvOpFUnordLessThanEqual
: op
= nir_op_fge
; swap
= true; break;
1937 case SpvOpUGreaterThanEqual
: op
= nir_op_uge
; break;
1938 case SpvOpSGreaterThanEqual
: op
= nir_op_ige
; break;
1939 case SpvOpFOrdGreaterThanEqual
: op
= nir_op_fge
; break;
1940 case SpvOpFUnordGreaterThanEqual
:op
= nir_op_fge
; break;
1943 case SpvOpConvertFToU
: op
= nir_op_f2u
; break;
1944 case SpvOpConvertFToS
: op
= nir_op_f2i
; break;
1945 case SpvOpConvertSToF
: op
= nir_op_i2f
; break;
1946 case SpvOpConvertUToF
: op
= nir_op_u2f
; break;
1947 case SpvOpBitcast
: op
= nir_op_imov
; break;
1950 op
= nir_op_imov
; /* TODO: NIR is 32-bit only; these are no-ops. */
1957 case SpvOpDPdx
: op
= nir_op_fddx
; break;
1958 case SpvOpDPdy
: op
= nir_op_fddy
; break;
1959 case SpvOpDPdxFine
: op
= nir_op_fddx_fine
; break;
1960 case SpvOpDPdyFine
: op
= nir_op_fddy_fine
; break;
1961 case SpvOpDPdxCoarse
: op
= nir_op_fddx_coarse
; break;
1962 case SpvOpDPdyCoarse
: op
= nir_op_fddy_coarse
; break;
1964 val
->ssa
->def
= nir_fadd(&b
->nb
,
1965 nir_fabs(&b
->nb
, nir_fddx(&b
->nb
, src
[0])),
1966 nir_fabs(&b
->nb
, nir_fddx(&b
->nb
, src
[1])));
1968 case SpvOpFwidthFine
:
1969 val
->ssa
->def
= nir_fadd(&b
->nb
,
1970 nir_fabs(&b
->nb
, nir_fddx_fine(&b
->nb
, src
[0])),
1971 nir_fabs(&b
->nb
, nir_fddx_fine(&b
->nb
, src
[1])));
1973 case SpvOpFwidthCoarse
:
1974 val
->ssa
->def
= nir_fadd(&b
->nb
,
1975 nir_fabs(&b
->nb
, nir_fddx_coarse(&b
->nb
, src
[0])),
1976 nir_fabs(&b
->nb
, nir_fddx_coarse(&b
->nb
, src
[1])));
1979 case SpvOpVectorTimesScalar
:
1980 /* The builder will take care of splatting for us. */
1981 val
->ssa
->def
= nir_fmul(&b
->nb
, src
[0], src
[1]);
1986 unreachable("No NIR equivalent");
1992 case SpvOpSignBitSet
:
1993 case SpvOpLessOrGreater
:
1995 case SpvOpUnordered
:
1997 unreachable("Unhandled opcode");
2001 nir_ssa_def
*tmp
= src
[0];
2006 nir_alu_instr
*instr
= nir_alu_instr_create(b
->shader
, op
);
2007 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
.dest
,
2008 glsl_get_vector_elements(type
), val
->name
);
2009 instr
->dest
.write_mask
= (1 << glsl_get_vector_elements(type
)) - 1;
2010 val
->ssa
->def
= &instr
->dest
.dest
.ssa
;
2012 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++)
2013 instr
->src
[i
].src
= nir_src_for_ssa(src
[i
]);
2015 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2018 static nir_ssa_def
*
2019 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
2021 unsigned swiz
[4] = { index
};
2022 return nir_swizzle(&b
->nb
, src
, swiz
, 1, true);
2026 static nir_ssa_def
*
2027 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
2030 nir_alu_instr
*vec
= create_vec(b
->shader
, src
->num_components
);
2032 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
2034 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
2036 vec
->src
[i
].src
= nir_src_for_ssa(src
);
2037 vec
->src
[i
].swizzle
[0] = i
;
2041 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2043 return &vec
->dest
.dest
.ssa
;
2046 static nir_ssa_def
*
2047 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2050 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
2051 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2052 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2053 vtn_vector_extract(b
, src
, i
), dest
);
2058 static nir_ssa_def
*
2059 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
2060 nir_ssa_def
*insert
, nir_ssa_def
*index
)
2062 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
2063 for (unsigned i
= 1; i
< src
->num_components
; i
++)
2064 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
2065 vtn_vector_insert(b
, src
, insert
, i
), dest
);
2070 static nir_ssa_def
*
2071 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
2072 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
2073 const uint32_t *indices
)
2075 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
);
2077 nir_ssa_undef_instr
*undef
= nir_ssa_undef_instr_create(b
->shader
, 1);
2078 nir_builder_instr_insert(&b
->nb
, &undef
->instr
);
2080 for (unsigned i
= 0; i
< num_components
; i
++) {
2081 uint32_t index
= indices
[i
];
2082 if (index
== 0xffffffff) {
2083 vec
->src
[i
].src
= nir_src_for_ssa(&undef
->def
);
2084 } else if (index
< src0
->num_components
) {
2085 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
2086 vec
->src
[i
].swizzle
[0] = index
;
2088 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
2089 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
2093 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2095 return &vec
->dest
.dest
.ssa
;
2099 * Concatentates a number of vectors/scalars together to produce a vector
2101 static nir_ssa_def
*
2102 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
2103 unsigned num_srcs
, nir_ssa_def
**srcs
)
2105 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
);
2107 unsigned dest_idx
= 0;
2108 for (unsigned i
= 0; i
< num_srcs
; i
++) {
2109 nir_ssa_def
*src
= srcs
[i
];
2110 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
2111 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
2112 vec
->src
[dest_idx
].swizzle
[0] = j
;
2117 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
2119 return &vec
->dest
.dest
.ssa
;
2122 static struct vtn_ssa_value
*
2123 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
2125 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
2126 dest
->type
= src
->type
;
2128 if (glsl_type_is_vector_or_scalar(src
->type
)) {
2129 dest
->def
= src
->def
;
2131 unsigned elems
= glsl_get_length(src
->type
);
2133 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
2134 for (unsigned i
= 0; i
< elems
; i
++)
2135 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
2141 static struct vtn_ssa_value
*
2142 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2143 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
2144 unsigned num_indices
)
2146 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
2148 struct vtn_ssa_value
*cur
= dest
;
2150 for (i
= 0; i
< num_indices
- 1; i
++) {
2151 cur
= cur
->elems
[indices
[i
]];
2154 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2155 /* According to the SPIR-V spec, OpCompositeInsert may work down to
2156 * the component granularity. In that case, the last index will be
2157 * the index to insert the scalar into the vector.
2160 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
2162 cur
->elems
[indices
[i
]] = insert
;
2168 static struct vtn_ssa_value
*
2169 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
2170 const uint32_t *indices
, unsigned num_indices
)
2172 struct vtn_ssa_value
*cur
= src
;
2173 for (unsigned i
= 0; i
< num_indices
; i
++) {
2174 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
2175 assert(i
== num_indices
- 1);
2176 /* According to the SPIR-V spec, OpCompositeExtract may work down to
2177 * the component granularity. The last index will be the index of the
2178 * vector to extract.
2181 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
2182 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
2183 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
2192 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
2193 const uint32_t *w
, unsigned count
)
2195 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2196 const struct glsl_type
*type
=
2197 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
2198 val
->ssa
= vtn_create_ssa_value(b
, type
);
2201 case SpvOpVectorExtractDynamic
:
2202 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2203 vtn_ssa_value(b
, w
[4])->def
);
2206 case SpvOpVectorInsertDynamic
:
2207 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
2208 vtn_ssa_value(b
, w
[4])->def
,
2209 vtn_ssa_value(b
, w
[5])->def
);
2212 case SpvOpVectorShuffle
:
2213 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
2214 vtn_ssa_value(b
, w
[3])->def
,
2215 vtn_ssa_value(b
, w
[4])->def
,
2219 case SpvOpCompositeConstruct
: {
2220 unsigned elems
= count
- 3;
2221 if (glsl_type_is_vector_or_scalar(type
)) {
2222 nir_ssa_def
*srcs
[4];
2223 for (unsigned i
= 0; i
< elems
; i
++)
2224 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
2226 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
2229 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2230 for (unsigned i
= 0; i
< elems
; i
++)
2231 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
2235 case SpvOpCompositeExtract
:
2236 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
2240 case SpvOpCompositeInsert
:
2241 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
2242 vtn_ssa_value(b
, w
[3]),
2246 case SpvOpCopyObject
:
2247 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
2251 unreachable("unknown composite operation");
2256 vtn_phi_node_init(struct vtn_builder
*b
, struct vtn_ssa_value
*val
)
2258 if (glsl_type_is_vector_or_scalar(val
->type
)) {
2259 nir_phi_instr
*phi
= nir_phi_instr_create(b
->shader
);
2260 nir_ssa_dest_init(&phi
->instr
, &phi
->dest
,
2261 glsl_get_vector_elements(val
->type
), NULL
);
2262 exec_list_make_empty(&phi
->srcs
);
2263 nir_builder_instr_insert(&b
->nb
, &phi
->instr
);
2264 val
->def
= &phi
->dest
.ssa
;
2266 unsigned elems
= glsl_get_length(val
->type
);
2267 for (unsigned i
= 0; i
< elems
; i
++)
2268 vtn_phi_node_init(b
, val
->elems
[i
]);
2272 static struct vtn_ssa_value
*
2273 vtn_phi_node_create(struct vtn_builder
*b
, const struct glsl_type
*type
)
2275 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, type
);
2276 vtn_phi_node_init(b
, val
);
2281 vtn_handle_phi_first_pass(struct vtn_builder
*b
, const uint32_t *w
)
2283 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
2284 const struct glsl_type
*type
=
2285 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
2286 val
->ssa
= vtn_phi_node_create(b
, type
);
2290 vtn_phi_node_add_src(struct vtn_ssa_value
*phi
, const nir_block
*pred
,
2291 struct vtn_ssa_value
*val
)
2293 assert(phi
->type
== val
->type
);
2294 if (glsl_type_is_vector_or_scalar(phi
->type
)) {
2295 nir_phi_instr
*phi_instr
= nir_instr_as_phi(phi
->def
->parent_instr
);
2296 nir_phi_src
*src
= ralloc(phi_instr
, nir_phi_src
);
2297 src
->pred
= (nir_block
*) pred
;
2298 src
->src
= nir_src_for_ssa(val
->def
);
2299 exec_list_push_tail(&phi_instr
->srcs
, &src
->node
);
2301 unsigned elems
= glsl_get_length(phi
->type
);
2302 for (unsigned i
= 0; i
< elems
; i
++)
2303 vtn_phi_node_add_src(phi
->elems
[i
], pred
, val
->elems
[i
]);
2307 static struct vtn_ssa_value
*
2308 vtn_get_phi_node_src(struct vtn_builder
*b
, nir_block
*block
,
2309 const struct glsl_type
*type
, const uint32_t *w
,
2312 struct hash_entry
*entry
= _mesa_hash_table_search(b
->block_table
, block
);
2314 struct vtn_block
*spv_block
= entry
->data
;
2315 for (unsigned off
= 4; off
< count
; off
+= 2) {
2316 if (spv_block
== vtn_value(b
, w
[off
], vtn_value_type_block
)->block
) {
2317 return vtn_ssa_value(b
, w
[off
- 1]);
2322 b
->nb
.cursor
= nir_before_block(block
);
2323 struct vtn_ssa_value
*phi
= vtn_phi_node_create(b
, type
);
2325 struct set_entry
*entry2
;
2326 set_foreach(block
->predecessors
, entry2
) {
2327 nir_block
*pred
= (nir_block
*) entry2
->key
;
2328 struct vtn_ssa_value
*val
= vtn_get_phi_node_src(b
, pred
, type
, w
,
2330 vtn_phi_node_add_src(phi
, pred
, val
);
2337 vtn_handle_phi_second_pass(struct vtn_builder
*b
, SpvOp opcode
,
2338 const uint32_t *w
, unsigned count
)
2340 if (opcode
== SpvOpLabel
) {
2341 b
->block
= vtn_value(b
, w
[1], vtn_value_type_block
)->block
;
2345 if (opcode
!= SpvOpPhi
)
2348 struct vtn_ssa_value
*phi
= vtn_value(b
, w
[2], vtn_value_type_ssa
)->ssa
;
2350 struct set_entry
*entry
;
2351 set_foreach(b
->block
->block
->predecessors
, entry
) {
2352 nir_block
*pred
= (nir_block
*) entry
->key
;
2354 struct vtn_ssa_value
*val
= vtn_get_phi_node_src(b
, pred
, phi
->type
, w
,
2356 vtn_phi_node_add_src(phi
, pred
, val
);
2363 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2364 const uint32_t *w
, unsigned count
)
2368 case SpvOpSourceExtension
:
2369 case SpvOpExtension
:
2370 /* Unhandled, but these are for debug so that's ok. */
2373 case SpvOpCapability
:
2375 * TODO properly handle these and give a real error if asking for too
2378 assert(w
[1] == SpvCapabilityMatrix
||
2379 w
[1] == SpvCapabilityShader
);
2382 case SpvOpExtInstImport
:
2383 vtn_handle_extension(b
, opcode
, w
, count
);
2386 case SpvOpMemoryModel
:
2387 assert(w
[1] == SpvAddressingModelLogical
);
2388 assert(w
[2] == SpvMemoryModelGLSL450
);
2391 case SpvOpEntryPoint
:
2392 assert(b
->entry_point
== NULL
);
2393 b
->entry_point
= &b
->values
[w
[2]];
2394 b
->execution_model
= w
[1];
2397 case SpvOpExecutionMode
:
2402 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
2403 vtn_string_literal(b
, &w
[2], count
- 2);
2407 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2);
2410 case SpvOpMemberName
:
2415 break; /* Ignored for now */
2417 case SpvOpDecorationGroup
:
2419 case SpvOpMemberDecorate
:
2420 case SpvOpGroupDecorate
:
2421 case SpvOpGroupMemberDecorate
:
2422 vtn_handle_decoration(b
, opcode
, w
, count
);
2428 case SpvOpTypeFloat
:
2429 case SpvOpTypeVector
:
2430 case SpvOpTypeMatrix
:
2431 case SpvOpTypeImage
:
2432 case SpvOpTypeSampler
:
2433 case SpvOpTypeSampledImage
:
2434 case SpvOpTypeArray
:
2435 case SpvOpTypeRuntimeArray
:
2436 case SpvOpTypeStruct
:
2437 case SpvOpTypeOpaque
:
2438 case SpvOpTypePointer
:
2439 case SpvOpTypeFunction
:
2440 case SpvOpTypeEvent
:
2441 case SpvOpTypeDeviceEvent
:
2442 case SpvOpTypeReserveId
:
2443 case SpvOpTypeQueue
:
2445 vtn_handle_type(b
, opcode
, w
, count
);
2448 case SpvOpConstantTrue
:
2449 case SpvOpConstantFalse
:
2451 case SpvOpConstantComposite
:
2452 case SpvOpConstantSampler
:
2453 case SpvOpSpecConstantTrue
:
2454 case SpvOpSpecConstantFalse
:
2455 case SpvOpSpecConstant
:
2456 case SpvOpSpecConstantComposite
:
2457 vtn_handle_constant(b
, opcode
, w
, count
);
2461 vtn_handle_variables(b
, opcode
, w
, count
);
2465 return false; /* End of preamble */
2472 vtn_handle_first_cfg_pass_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2473 const uint32_t *w
, unsigned count
)
2476 case SpvOpFunction
: {
2477 assert(b
->func
== NULL
);
2478 b
->func
= rzalloc(b
, struct vtn_function
);
2480 const struct glsl_type
*result_type
=
2481 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
2482 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_function
);
2483 const struct glsl_type
*func_type
=
2484 vtn_value(b
, w
[4], vtn_value_type_type
)->type
->type
;
2486 assert(glsl_get_function_return_type(func_type
) == result_type
);
2488 nir_function
*func
=
2489 nir_function_create(b
->shader
, ralloc_strdup(b
->shader
, val
->name
));
2491 nir_function_overload
*overload
= nir_function_overload_create(func
);
2492 overload
->num_params
= glsl_get_length(func_type
);
2493 overload
->params
= ralloc_array(overload
, nir_parameter
,
2494 overload
->num_params
);
2495 for (unsigned i
= 0; i
< overload
->num_params
; i
++) {
2496 const struct glsl_function_param
*param
=
2497 glsl_get_function_param(func_type
, i
);
2498 overload
->params
[i
].type
= param
->type
;
2501 overload
->params
[i
].param_type
= nir_parameter_inout
;
2503 overload
->params
[i
].param_type
= nir_parameter_in
;
2507 overload
->params
[i
].param_type
= nir_parameter_out
;
2509 assert(!"Parameter is neither in nor out");
2513 b
->func
->overload
= overload
;
2517 case SpvOpFunctionEnd
:
2522 case SpvOpFunctionParameter
:
2523 break; /* Does nothing */
2526 assert(b
->block
== NULL
);
2527 b
->block
= rzalloc(b
, struct vtn_block
);
2528 b
->block
->label
= w
;
2529 vtn_push_value(b
, w
[1], vtn_value_type_block
)->block
= b
->block
;
2531 if (b
->func
->start_block
== NULL
) {
2532 /* This is the first block encountered for this function. In this
2533 * case, we set the start block and add it to the list of
2534 * implemented functions that we'll walk later.
2536 b
->func
->start_block
= b
->block
;
2537 exec_list_push_tail(&b
->functions
, &b
->func
->node
);
2543 case SpvOpBranchConditional
:
2547 case SpvOpReturnValue
:
2548 case SpvOpUnreachable
:
2550 b
->block
->branch
= w
;
2554 case SpvOpSelectionMerge
:
2555 case SpvOpLoopMerge
:
2556 assert(b
->block
&& b
->block
->merge_op
== SpvOpNop
);
2557 b
->block
->merge_op
= opcode
;
2558 b
->block
->merge_block_id
= w
[1];
2562 /* Continue on as per normal */
2570 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2571 const uint32_t *w
, unsigned count
)
2575 struct vtn_block
*block
= vtn_value(b
, w
[1], vtn_value_type_block
)->block
;
2576 assert(block
->block
== NULL
);
2578 block
->block
= nir_cursor_current_block(b
->nb
.cursor
);
2582 case SpvOpLoopMerge
:
2583 case SpvOpSelectionMerge
:
2584 /* This is handled by cfg pre-pass and walk_blocks */
2588 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
2592 vtn_handle_extension(b
, opcode
, w
, count
);
2598 case SpvOpCopyMemory
:
2599 case SpvOpCopyMemorySized
:
2600 case SpvOpAccessChain
:
2601 case SpvOpInBoundsAccessChain
:
2602 case SpvOpArrayLength
:
2603 case SpvOpImageTexelPointer
:
2604 vtn_handle_variables(b
, opcode
, w
, count
);
2607 case SpvOpFunctionCall
:
2608 vtn_handle_function_call(b
, opcode
, w
, count
);
2611 case SpvOpImageSampleImplicitLod
:
2612 case SpvOpImageSampleExplicitLod
:
2613 case SpvOpImageSampleDrefImplicitLod
:
2614 case SpvOpImageSampleDrefExplicitLod
:
2615 case SpvOpImageSampleProjImplicitLod
:
2616 case SpvOpImageSampleProjExplicitLod
:
2617 case SpvOpImageSampleProjDrefImplicitLod
:
2618 case SpvOpImageSampleProjDrefExplicitLod
:
2619 case SpvOpImageFetch
:
2620 case SpvOpImageGather
:
2621 case SpvOpImageDrefGather
:
2622 case SpvOpImageQuerySizeLod
:
2623 case SpvOpImageQuerySize
:
2624 case SpvOpImageQueryLod
:
2625 case SpvOpImageQueryLevels
:
2626 case SpvOpImageQuerySamples
:
2627 vtn_handle_texture(b
, opcode
, w
, count
);
2635 case SpvOpConvertFToU
:
2636 case SpvOpConvertFToS
:
2637 case SpvOpConvertSToF
:
2638 case SpvOpConvertUToF
:
2642 case SpvOpConvertPtrToU
:
2643 case SpvOpConvertUToPtr
:
2644 case SpvOpPtrCastToGeneric
:
2645 case SpvOpGenericCastToPtr
:
2651 case SpvOpSignBitSet
:
2652 case SpvOpLessOrGreater
:
2654 case SpvOpUnordered
:
2669 case SpvOpVectorTimesScalar
:
2671 case SpvOpShiftRightLogical
:
2672 case SpvOpShiftRightArithmetic
:
2673 case SpvOpShiftLeftLogical
:
2674 case SpvOpLogicalOr
:
2675 case SpvOpLogicalEqual
:
2676 case SpvOpLogicalNotEqual
:
2677 case SpvOpLogicalAnd
:
2678 case SpvOpBitwiseOr
:
2679 case SpvOpBitwiseXor
:
2680 case SpvOpBitwiseAnd
:
2683 case SpvOpFOrdEqual
:
2684 case SpvOpFUnordEqual
:
2685 case SpvOpINotEqual
:
2686 case SpvOpFOrdNotEqual
:
2687 case SpvOpFUnordNotEqual
:
2688 case SpvOpULessThan
:
2689 case SpvOpSLessThan
:
2690 case SpvOpFOrdLessThan
:
2691 case SpvOpFUnordLessThan
:
2692 case SpvOpUGreaterThan
:
2693 case SpvOpSGreaterThan
:
2694 case SpvOpFOrdGreaterThan
:
2695 case SpvOpFUnordGreaterThan
:
2696 case SpvOpULessThanEqual
:
2697 case SpvOpSLessThanEqual
:
2698 case SpvOpFOrdLessThanEqual
:
2699 case SpvOpFUnordLessThanEqual
:
2700 case SpvOpUGreaterThanEqual
:
2701 case SpvOpSGreaterThanEqual
:
2702 case SpvOpFOrdGreaterThanEqual
:
2703 case SpvOpFUnordGreaterThanEqual
:
2709 case SpvOpFwidthFine
:
2710 case SpvOpDPdxCoarse
:
2711 case SpvOpDPdyCoarse
:
2712 case SpvOpFwidthCoarse
:
2713 vtn_handle_alu(b
, opcode
, w
, count
);
2716 case SpvOpTranspose
:
2717 case SpvOpOuterProduct
:
2718 case SpvOpMatrixTimesScalar
:
2719 case SpvOpVectorTimesMatrix
:
2720 case SpvOpMatrixTimesVector
:
2721 case SpvOpMatrixTimesMatrix
:
2722 vtn_handle_matrix_alu(b
, opcode
, w
, count
);
2725 case SpvOpVectorExtractDynamic
:
2726 case SpvOpVectorInsertDynamic
:
2727 case SpvOpVectorShuffle
:
2728 case SpvOpCompositeConstruct
:
2729 case SpvOpCompositeExtract
:
2730 case SpvOpCompositeInsert
:
2731 case SpvOpCopyObject
:
2732 vtn_handle_composite(b
, opcode
, w
, count
);
2736 vtn_handle_phi_first_pass(b
, w
);
2740 unreachable("Unhandled opcode");
2747 vtn_walk_blocks(struct vtn_builder
*b
, struct vtn_block
*start
,
2748 struct vtn_block
*break_block
, struct vtn_block
*cont_block
,
2749 struct vtn_block
*end_block
)
2751 struct vtn_block
*block
= start
;
2752 while (block
!= end_block
) {
2753 if (block
->merge_op
== SpvOpLoopMerge
) {
2754 /* This is the jump into a loop. */
2755 struct vtn_block
*new_cont_block
= block
;
2756 struct vtn_block
*new_break_block
=
2757 vtn_value(b
, block
->merge_block_id
, vtn_value_type_block
)->block
;
2759 nir_loop
*loop
= nir_loop_create(b
->shader
);
2760 nir_cf_node_insert(b
->nb
.cursor
, &loop
->cf_node
);
2762 /* Reset the merge_op to prerevent infinite recursion */
2763 block
->merge_op
= SpvOpNop
;
2765 b
->nb
.cursor
= nir_after_cf_list(&loop
->body
);
2766 vtn_walk_blocks(b
, block
, new_break_block
, new_cont_block
, NULL
);
2768 b
->nb
.cursor
= nir_after_cf_node(&loop
->cf_node
);
2769 block
= new_break_block
;
2773 const uint32_t *w
= block
->branch
;
2774 SpvOp branch_op
= w
[0] & SpvOpCodeMask
;
2777 vtn_foreach_instruction(b
, block
->label
, block
->branch
,
2778 vtn_handle_body_instruction
);
2780 nir_block
*cur_block
= nir_cursor_current_block(b
->nb
.cursor
);
2781 assert(cur_block
== block
->block
);
2782 _mesa_hash_table_insert(b
->block_table
, cur_block
, block
);
2784 switch (branch_op
) {
2786 struct vtn_block
*branch_block
=
2787 vtn_value(b
, w
[1], vtn_value_type_block
)->block
;
2789 if (branch_block
== break_block
) {
2790 nir_jump_instr
*jump
= nir_jump_instr_create(b
->shader
,
2792 nir_builder_instr_insert(&b
->nb
, &jump
->instr
);
2795 } else if (branch_block
== cont_block
) {
2796 nir_jump_instr
*jump
= nir_jump_instr_create(b
->shader
,
2798 nir_builder_instr_insert(&b
->nb
, &jump
->instr
);
2801 } else if (branch_block
== end_block
) {
2802 /* We're branching to the merge block of an if, since for loops
2803 * and functions end_block == NULL, so we're done here.
2807 /* We're branching to another block, and according to the rules,
2808 * we can only branch to another block with one predecessor (so
2809 * we're the only one jumping to it) so we can just process it
2812 block
= branch_block
;
2817 case SpvOpBranchConditional
: {
2818 /* Gather up the branch blocks */
2819 struct vtn_block
*then_block
=
2820 vtn_value(b
, w
[2], vtn_value_type_block
)->block
;
2821 struct vtn_block
*else_block
=
2822 vtn_value(b
, w
[3], vtn_value_type_block
)->block
;
2824 nir_if
*if_stmt
= nir_if_create(b
->shader
);
2825 if_stmt
->condition
= nir_src_for_ssa(vtn_ssa_value(b
, w
[1])->def
);
2826 nir_cf_node_insert(b
->nb
.cursor
, &if_stmt
->cf_node
);
2828 if (then_block
== break_block
) {
2829 nir_jump_instr
*jump
= nir_jump_instr_create(b
->shader
,
2831 nir_instr_insert_after_cf_list(&if_stmt
->then_list
,
2834 } else if (else_block
== break_block
) {
2835 nir_jump_instr
*jump
= nir_jump_instr_create(b
->shader
,
2837 nir_instr_insert_after_cf_list(&if_stmt
->else_list
,
2840 } else if (then_block
== cont_block
) {
2841 nir_jump_instr
*jump
= nir_jump_instr_create(b
->shader
,
2843 nir_instr_insert_after_cf_list(&if_stmt
->then_list
,
2846 } else if (else_block
== cont_block
) {
2847 nir_jump_instr
*jump
= nir_jump_instr_create(b
->shader
,
2849 nir_instr_insert_after_cf_list(&if_stmt
->else_list
,
2853 /* According to the rules we're branching to two blocks that don't
2854 * have any other predecessors, so we can handle this as a
2857 assert(block
->merge_op
== SpvOpSelectionMerge
);
2858 struct vtn_block
*merge_block
=
2859 vtn_value(b
, block
->merge_block_id
, vtn_value_type_block
)->block
;
2861 b
->nb
.cursor
= nir_after_cf_list(&if_stmt
->then_list
);
2862 vtn_walk_blocks(b
, then_block
, break_block
, cont_block
, merge_block
);
2864 b
->nb
.cursor
= nir_after_cf_list(&if_stmt
->else_list
);
2865 vtn_walk_blocks(b
, else_block
, break_block
, cont_block
, merge_block
);
2867 b
->nb
.cursor
= nir_after_cf_node(&if_stmt
->cf_node
);
2868 block
= merge_block
;
2872 /* If we got here then we inserted a predicated break or continue
2873 * above and we need to handle the other case. We already set
2874 * `block` above to indicate what block to visit after the
2878 /* It's possible that the other branch is also a break/continue.
2879 * If it is, we handle that here.
2881 if (block
== break_block
) {
2882 nir_jump_instr
*jump
= nir_jump_instr_create(b
->shader
,
2884 nir_builder_instr_insert(&b
->nb
, &jump
->instr
);
2887 } else if (block
== cont_block
) {
2888 nir_jump_instr
*jump
= nir_jump_instr_create(b
->shader
,
2890 nir_builder_instr_insert(&b
->nb
, &jump
->instr
);
2895 /* If we got here then there was a predicated break/continue but
2896 * the other half of the if has stuff in it. `block` was already
2897 * set above so there is nothing left for us to do.
2903 nir_jump_instr
*jump
= nir_jump_instr_create(b
->shader
,
2905 nir_builder_instr_insert(&b
->nb
, &jump
->instr
);
2910 nir_intrinsic_instr
*discard
=
2911 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_discard
);
2912 nir_builder_instr_insert(&b
->nb
, &discard
->instr
);
2917 case SpvOpReturnValue
:
2918 case SpvOpUnreachable
:
2920 unreachable("Unhandled opcode");
2926 spirv_to_nir(const uint32_t *words
, size_t word_count
,
2927 gl_shader_stage stage
,
2928 const nir_shader_compiler_options
*options
)
2930 const uint32_t *word_end
= words
+ word_count
;
2932 /* Handle the SPIR-V header (first 4 dwords) */
2933 assert(word_count
> 5);
2935 assert(words
[0] == SpvMagicNumber
);
2936 assert(words
[1] == 99);
2937 /* words[2] == generator magic */
2938 unsigned value_id_bound
= words
[3];
2939 assert(words
[4] == 0);
2943 nir_shader
*shader
= nir_shader_create(NULL
, stage
, options
);
2945 /* Initialize the stn_builder object */
2946 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
2948 b
->value_id_bound
= value_id_bound
;
2949 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
2950 exec_list_make_empty(&b
->functions
);
2952 /* Handle all the preamble instructions */
2953 words
= vtn_foreach_instruction(b
, words
, word_end
,
2954 vtn_handle_preamble_instruction
);
2956 /* Do a very quick CFG analysis pass */
2957 vtn_foreach_instruction(b
, words
, word_end
,
2958 vtn_handle_first_cfg_pass_instruction
);
2960 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
2961 b
->impl
= nir_function_impl_create(func
->overload
);
2962 b
->const_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
2963 _mesa_key_pointer_equal
);
2964 b
->block_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
2965 _mesa_key_pointer_equal
);
2966 nir_builder_init(&b
->nb
, b
->impl
);
2967 b
->nb
.cursor
= nir_after_cf_list(&b
->impl
->body
);
2968 vtn_walk_blocks(b
, func
->start_block
, NULL
, NULL
, NULL
);
2969 vtn_foreach_instruction(b
, func
->start_block
->label
, func
->end
,
2970 vtn_handle_phi_second_pass
);
2973 /* Because we can still have output reads in NIR, we need to lower
2974 * outputs to temporaries before we are truely finished.
2976 nir_lower_outputs_to_temporaries(shader
);