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"
31 static struct vtn_ssa_value
*
32 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
33 const struct glsl_type
*type
)
35 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
40 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
43 switch (glsl_get_base_type(type
)) {
48 case GLSL_TYPE_DOUBLE
:
49 if (glsl_type_is_vector_or_scalar(type
)) {
50 unsigned num_components
= glsl_get_vector_elements(val
->type
);
51 nir_load_const_instr
*load
=
52 nir_load_const_instr_create(b
->shader
, num_components
);
54 for (unsigned i
= 0; i
< num_components
; i
++)
55 load
->value
.u
[i
] = constant
->value
.u
[i
];
57 nir_instr_insert_before_cf_list(&b
->impl
->body
, &load
->instr
);
58 val
->def
= &load
->def
;
60 assert(glsl_type_is_matrix(type
));
61 unsigned rows
= glsl_get_vector_elements(val
->type
);
62 unsigned columns
= glsl_get_matrix_columns(val
->type
);
63 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, columns
);
65 for (unsigned i
= 0; i
< columns
; i
++) {
66 struct vtn_ssa_value
*col_val
= rzalloc(b
, struct vtn_ssa_value
);
67 col_val
->type
= glsl_get_column_type(val
->type
);
68 nir_load_const_instr
*load
=
69 nir_load_const_instr_create(b
->shader
, rows
);
71 for (unsigned j
= 0; j
< rows
; j
++)
72 load
->value
.u
[j
] = constant
->value
.u
[rows
* i
+ j
];
74 nir_instr_insert_before_cf_list(&b
->impl
->body
, &load
->instr
);
75 col_val
->def
= &load
->def
;
77 val
->elems
[i
] = col_val
;
82 case GLSL_TYPE_ARRAY
: {
83 unsigned elems
= glsl_get_length(val
->type
);
84 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
85 const struct glsl_type
*elem_type
= glsl_get_array_element(val
->type
);
86 for (unsigned i
= 0; i
< elems
; i
++)
87 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
92 case GLSL_TYPE_STRUCT
: {
93 unsigned elems
= glsl_get_length(val
->type
);
94 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
95 for (unsigned i
= 0; i
< elems
; i
++) {
96 const struct glsl_type
*elem_type
=
97 glsl_get_struct_field(val
->type
, i
);
98 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
105 unreachable("bad constant type");
111 struct vtn_ssa_value
*
112 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
114 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
115 switch (val
->value_type
) {
116 case vtn_value_type_constant
:
117 return vtn_const_ssa_value(b
, val
->constant
, val
->const_type
);
119 case vtn_value_type_ssa
:
122 unreachable("Invalid type for an SSA value");
127 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
130 return ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
133 static const uint32_t *
134 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
135 const uint32_t *end
, vtn_instruction_handler handler
)
137 const uint32_t *w
= start
;
139 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
140 unsigned count
= w
[0] >> SpvWordCountShift
;
141 assert(count
>= 1 && w
+ count
<= end
);
143 if (!handler(b
, opcode
, w
, count
))
153 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
154 const uint32_t *w
, unsigned count
)
157 case SpvOpExtInstImport
: {
158 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
159 if (strcmp((const char *)&w
[2], "GLSL.std.450") == 0) {
160 val
->ext_handler
= vtn_handle_glsl450_instruction
;
162 assert(!"Unsupported extension");
168 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
169 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
176 unreachable("Unhandled opcode");
181 _foreach_decoration_helper(struct vtn_builder
*b
,
182 struct vtn_value
*base_value
,
184 struct vtn_value
*value
,
185 vtn_decoration_foreach_cb cb
, void *data
)
187 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
188 if (dec
->member
>= 0) {
189 assert(member
== -1);
190 member
= dec
->member
;
194 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
195 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
, cb
, data
);
197 cb(b
, base_value
, member
, dec
, data
);
202 /** Iterates (recursively if needed) over all of the decorations on a value
204 * This function iterates over all of the decorations applied to a given
205 * value. If it encounters a decoration group, it recurses into the group
206 * and iterates over all of those decorations as well.
209 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
210 vtn_decoration_foreach_cb cb
, void *data
)
212 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
216 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
217 const uint32_t *w
, unsigned count
)
219 const uint32_t *w_end
= w
+ count
;
220 const uint32_t target
= w
[1];
225 case SpvOpDecorationGroup
:
226 vtn_push_value(b
, target
, vtn_value_type_undef
);
229 case SpvOpMemberDecorate
:
232 case SpvOpDecorate
: {
233 struct vtn_value
*val
= &b
->values
[target
];
235 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
236 dec
->member
= member
;
237 dec
->decoration
= *(w
++);
240 /* Link into the list */
241 dec
->next
= val
->decoration
;
242 val
->decoration
= dec
;
246 case SpvOpGroupMemberDecorate
:
249 case SpvOpGroupDecorate
: {
250 struct vtn_value
*group
= &b
->values
[target
];
251 assert(group
->value_type
== vtn_value_type_decoration_group
);
253 for (; w
< w_end
; w
++) {
254 struct vtn_value
*val
= &b
->values
[*w
];
255 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
256 dec
->member
= member
;
259 /* Link into the list */
260 dec
->next
= val
->decoration
;
261 val
->decoration
= dec
;
267 unreachable("Unhandled opcode");
272 struct_member_decoration_cb(struct vtn_builder
*b
,
273 struct vtn_value
*val
, int member
,
274 const struct vtn_decoration
*dec
, void *void_fields
)
276 struct glsl_struct_field
*fields
= void_fields
;
281 switch (dec
->decoration
) {
282 case SpvDecorationPrecisionLow
:
283 case SpvDecorationPrecisionMedium
:
284 case SpvDecorationPrecisionHigh
:
285 break; /* FIXME: Do nothing with these for now. */
286 case SpvDecorationSmooth
:
287 fields
[member
].interpolation
= INTERP_QUALIFIER_SMOOTH
;
289 case SpvDecorationNoperspective
:
290 fields
[member
].interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
292 case SpvDecorationFlat
:
293 fields
[member
].interpolation
= INTERP_QUALIFIER_FLAT
;
295 case SpvDecorationCentroid
:
296 fields
[member
].centroid
= true;
298 case SpvDecorationSample
:
299 fields
[member
].sample
= true;
301 case SpvDecorationLocation
:
302 fields
[member
].location
= dec
->literals
[0];
305 unreachable("Unhandled member decoration");
310 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
311 const uint32_t *w
, unsigned count
)
313 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
315 val
->type
= ralloc(b
, struct vtn_type
);
316 val
->type
->is_builtin
= false;
320 val
->type
->type
= glsl_void_type();
323 val
->type
->type
= glsl_bool_type();
326 val
->type
->type
= glsl_int_type();
329 val
->type
->type
= glsl_float_type();
332 case SpvOpTypeVector
: {
333 const struct glsl_type
*base
=
334 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
335 unsigned elems
= w
[3];
337 assert(glsl_type_is_scalar(base
));
338 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
), elems
);
342 case SpvOpTypeMatrix
: {
343 const struct glsl_type
*base
=
344 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
345 unsigned columns
= w
[3];
347 assert(glsl_type_is_vector(base
));
348 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
),
349 glsl_get_vector_elements(base
),
351 val
->type
->row_major
= false;
352 val
->type
->stride
= 0;
356 case SpvOpTypeArray
: {
357 struct vtn_type
*array_element
=
358 vtn_value(b
, w
[2], vtn_value_type_type
)->type
;
359 val
->type
->type
= glsl_array_type(array_element
->type
, w
[3]);
360 val
->type
->array_element
= array_element
;
361 val
->type
->stride
= 0;
365 case SpvOpTypeStruct
: {
366 unsigned num_fields
= count
- 2;
367 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
369 NIR_VLA(struct glsl_struct_field
, fields
, count
);
370 for (unsigned i
= 0; i
< num_fields
; i
++) {
371 /* TODO: Handle decorators */
372 val
->type
->members
[i
] =
373 vtn_value(b
, w
[i
+ 2], vtn_value_type_type
)->type
;
374 fields
[i
].type
= val
->type
->members
[i
]->type
;
375 fields
[i
].name
= ralloc_asprintf(b
, "field%d", i
);
376 fields
[i
].location
= -1;
377 fields
[i
].interpolation
= 0;
378 fields
[i
].centroid
= 0;
379 fields
[i
].sample
= 0;
380 fields
[i
].matrix_layout
= 2;
381 fields
[i
].stream
= -1;
384 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, fields
);
386 const char *name
= val
->name
? val
->name
: "struct";
388 val
->type
->type
= glsl_struct_type(fields
, count
, name
);
392 case SpvOpTypeFunction
: {
393 const struct glsl_type
*return_type
=
394 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
395 NIR_VLA(struct glsl_function_param
, params
, count
- 3);
396 for (unsigned i
= 0; i
< count
- 3; i
++) {
397 params
[i
].type
= vtn_value(b
, w
[i
+ 3], vtn_value_type_type
)->type
->type
;
401 params
[i
].out
= true;
403 val
->type
->type
= glsl_function_type(return_type
, params
, count
- 3);
407 case SpvOpTypePointer
:
408 /* FIXME: For now, we'll just do the really lame thing and return
409 * the same type. The validator should ensure that the proper number
410 * of dereferences happen
412 val
->type
= vtn_value(b
, w
[3], vtn_value_type_type
)->type
;
415 case SpvOpTypeSampler
: {
416 const struct glsl_type
*sampled_type
=
417 vtn_value(b
, w
[2], vtn_value_type_type
)->type
->type
;
419 assert(glsl_type_is_vector_or_scalar(sampled_type
));
421 enum glsl_sampler_dim dim
;
422 switch ((SpvDim
)w
[3]) {
423 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
424 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
425 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
426 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
427 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
428 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
430 unreachable("Invalid SPIR-V Sampler dimension");
433 /* TODO: Handle the various texture image/filter options */
436 bool is_array
= w
[5];
437 bool is_shadow
= w
[6];
439 assert(w
[7] == 0 && "FIXME: Handl multi-sampled textures");
441 val
->type
->type
= glsl_sampler_type(dim
, is_shadow
, is_array
,
442 glsl_get_base_type(sampled_type
));
446 case SpvOpTypeRuntimeArray
:
447 case SpvOpTypeOpaque
:
449 case SpvOpTypeDeviceEvent
:
450 case SpvOpTypeReserveId
:
454 unreachable("Unhandled opcode");
459 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
460 const uint32_t *w
, unsigned count
)
462 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
463 val
->const_type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
464 val
->constant
= ralloc(b
, nir_constant
);
466 case SpvOpConstantTrue
:
467 assert(val
->const_type
== glsl_bool_type());
468 val
->constant
->value
.u
[0] = NIR_TRUE
;
470 case SpvOpConstantFalse
:
471 assert(val
->const_type
== glsl_bool_type());
472 val
->constant
->value
.u
[0] = NIR_FALSE
;
475 assert(glsl_type_is_scalar(val
->const_type
));
476 val
->constant
->value
.u
[0] = w
[3];
478 case SpvOpConstantComposite
: {
479 unsigned elem_count
= count
- 3;
480 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
481 for (unsigned i
= 0; i
< elem_count
; i
++)
482 elems
[i
] = vtn_value(b
, w
[i
+ 3], vtn_value_type_constant
)->constant
;
484 switch (glsl_get_base_type(val
->const_type
)) {
487 case GLSL_TYPE_FLOAT
:
489 if (glsl_type_is_matrix(val
->const_type
)) {
490 unsigned rows
= glsl_get_vector_elements(val
->const_type
);
491 assert(glsl_get_matrix_columns(val
->const_type
) == elem_count
);
492 for (unsigned i
= 0; i
< elem_count
; i
++)
493 for (unsigned j
= 0; j
< rows
; j
++)
494 val
->constant
->value
.u
[rows
* i
+ j
] = elems
[i
]->value
.u
[j
];
496 assert(glsl_type_is_vector(val
->const_type
));
497 assert(glsl_get_vector_elements(val
->const_type
) == elem_count
);
498 for (unsigned i
= 0; i
< elem_count
; i
++)
499 val
->constant
->value
.u
[i
] = elems
[i
]->value
.u
[0];
504 case GLSL_TYPE_STRUCT
:
505 case GLSL_TYPE_ARRAY
:
506 ralloc_steal(val
->constant
, elems
);
507 val
->constant
->elements
= elems
;
511 unreachable("Unsupported type for constants");
517 unreachable("Unhandled opcode");
522 var_decoration_cb(struct vtn_builder
*b
, struct vtn_value
*val
, int member
,
523 const struct vtn_decoration
*dec
, void *void_var
)
525 assert(val
->value_type
== vtn_value_type_deref
);
526 assert(val
->deref
->deref
.child
== NULL
);
527 assert(val
->deref
->var
== void_var
);
529 nir_variable
*var
= void_var
;
530 switch (dec
->decoration
) {
531 case SpvDecorationPrecisionLow
:
532 case SpvDecorationPrecisionMedium
:
533 case SpvDecorationPrecisionHigh
:
534 break; /* FIXME: Do nothing with these for now. */
535 case SpvDecorationSmooth
:
536 var
->data
.interpolation
= INTERP_QUALIFIER_SMOOTH
;
538 case SpvDecorationNoperspective
:
539 var
->data
.interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
541 case SpvDecorationFlat
:
542 var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
544 case SpvDecorationCentroid
:
545 var
->data
.centroid
= true;
547 case SpvDecorationSample
:
548 var
->data
.sample
= true;
550 case SpvDecorationInvariant
:
551 var
->data
.invariant
= true;
553 case SpvDecorationConstant
:
554 assert(var
->constant_initializer
!= NULL
);
555 var
->data
.read_only
= true;
557 case SpvDecorationNonwritable
:
558 var
->data
.read_only
= true;
560 case SpvDecorationLocation
:
561 var
->data
.explicit_location
= true;
562 var
->data
.location
= dec
->literals
[0];
564 case SpvDecorationComponent
:
565 var
->data
.location_frac
= dec
->literals
[0];
567 case SpvDecorationIndex
:
568 var
->data
.explicit_index
= true;
569 var
->data
.index
= dec
->literals
[0];
571 case SpvDecorationBinding
:
572 var
->data
.explicit_binding
= true;
573 var
->data
.binding
= dec
->literals
[0];
575 case SpvDecorationDescriptorSet
:
576 var
->data
.descriptor_set
= dec
->literals
[0];
578 case SpvDecorationBuiltIn
:
579 var
->data
.mode
= nir_var_system_value
;
580 var
->data
.read_only
= true;
581 switch ((SpvBuiltIn
)dec
->literals
[0]) {
582 case SpvBuiltInFrontFacing
:
583 var
->data
.location
= SYSTEM_VALUE_FRONT_FACE
;
585 case SpvBuiltInVertexId
:
586 var
->data
.location
= SYSTEM_VALUE_VERTEX_ID
;
588 case SpvBuiltInInstanceId
:
589 var
->data
.location
= SYSTEM_VALUE_INSTANCE_ID
;
591 case SpvBuiltInSampleId
:
592 var
->data
.location
= SYSTEM_VALUE_SAMPLE_ID
;
594 case SpvBuiltInSamplePosition
:
595 var
->data
.location
= SYSTEM_VALUE_SAMPLE_POS
;
597 case SpvBuiltInSampleMask
:
598 var
->data
.location
= SYSTEM_VALUE_SAMPLE_MASK_IN
;
600 case SpvBuiltInInvocationId
:
601 var
->data
.location
= SYSTEM_VALUE_INVOCATION_ID
;
603 case SpvBuiltInPrimitiveId
:
604 case SpvBuiltInPosition
:
605 case SpvBuiltInPointSize
:
606 case SpvBuiltInClipVertex
:
607 case SpvBuiltInClipDistance
:
608 case SpvBuiltInCullDistance
:
609 case SpvBuiltInLayer
:
610 case SpvBuiltInViewportIndex
:
611 case SpvBuiltInTessLevelOuter
:
612 case SpvBuiltInTessLevelInner
:
613 case SpvBuiltInTessCoord
:
614 case SpvBuiltInPatchVertices
:
615 case SpvBuiltInFragCoord
:
616 case SpvBuiltInPointCoord
:
617 case SpvBuiltInFragColor
:
618 case SpvBuiltInFragDepth
:
619 case SpvBuiltInHelperInvocation
:
620 case SpvBuiltInNumWorkgroups
:
621 case SpvBuiltInWorkgroupSize
:
622 case SpvBuiltInWorkgroupId
:
623 case SpvBuiltInLocalInvocationId
:
624 case SpvBuiltInGlobalInvocationId
:
625 case SpvBuiltInLocalInvocationIndex
:
626 case SpvBuiltInWorkDim
:
627 case SpvBuiltInGlobalSize
:
628 case SpvBuiltInEnqueuedWorkgroupSize
:
629 case SpvBuiltInGlobalOffset
:
630 case SpvBuiltInGlobalLinearId
:
631 case SpvBuiltInWorkgroupLinearId
:
632 case SpvBuiltInSubgroupSize
:
633 case SpvBuiltInSubgroupMaxSize
:
634 case SpvBuiltInNumSubgroups
:
635 case SpvBuiltInNumEnqueuedSubgroups
:
636 case SpvBuiltInSubgroupId
:
637 case SpvBuiltInSubgroupLocalInvocationId
:
638 unreachable("Unhandled builtin enum");
641 case SpvDecorationNoStaticUse
:
642 /* This can safely be ignored */
644 case SpvDecorationBlock
:
645 case SpvDecorationBufferBlock
:
646 case SpvDecorationRowMajor
:
647 case SpvDecorationColMajor
:
648 case SpvDecorationGLSLShared
:
649 case SpvDecorationGLSLStd140
:
650 case SpvDecorationGLSLStd430
:
651 case SpvDecorationGLSLPacked
:
652 case SpvDecorationPatch
:
653 case SpvDecorationRestrict
:
654 case SpvDecorationAliased
:
655 case SpvDecorationVolatile
:
656 case SpvDecorationCoherent
:
657 case SpvDecorationNonreadable
:
658 case SpvDecorationUniform
:
659 /* This is really nice but we have no use for it right now. */
660 case SpvDecorationCPacked
:
661 case SpvDecorationSaturatedConversion
:
662 case SpvDecorationStream
:
663 case SpvDecorationOffset
:
664 case SpvDecorationAlignment
:
665 case SpvDecorationXfbBuffer
:
666 case SpvDecorationStride
:
667 case SpvDecorationFuncParamAttr
:
668 case SpvDecorationFPRoundingMode
:
669 case SpvDecorationFPFastMathMode
:
670 case SpvDecorationLinkageAttributes
:
671 case SpvDecorationSpecId
:
674 unreachable("Unhandled variable decoration");
678 static struct vtn_ssa_value
*
679 _vtn_variable_load(struct vtn_builder
*b
,
680 nir_deref_var
*src_deref
, nir_deref
*src_deref_tail
)
682 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
683 val
->type
= src_deref_tail
->type
;
685 /* The deref tail may contain a deref to select a component of a vector (in
686 * other words, it might not be an actual tail) so we have to save it away
687 * here since we overwrite it later.
689 nir_deref
*old_child
= src_deref_tail
->child
;
691 if (glsl_type_is_vector_or_scalar(val
->type
)) {
692 nir_intrinsic_instr
*load
=
693 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_load_var
);
695 nir_deref_as_var(nir_copy_deref(load
, &src_deref
->deref
));
696 load
->num_components
= glsl_get_vector_elements(val
->type
);
697 nir_ssa_dest_init(&load
->instr
, &load
->dest
, load
->num_components
, NULL
);
699 nir_builder_instr_insert(&b
->nb
, &load
->instr
);
701 if (src_deref
->var
->data
.mode
== nir_var_uniform
&&
702 glsl_get_base_type(val
->type
) == GLSL_TYPE_BOOL
) {
703 /* Uniform boolean loads need to be fixed up since they're defined
704 * to be zero/nonzero rather than NIR_FALSE/NIR_TRUE.
706 val
->def
= nir_ine(&b
->nb
, &load
->dest
.ssa
, nir_imm_int(&b
->nb
, 0));
708 val
->def
= &load
->dest
.ssa
;
710 } else if (glsl_get_base_type(val
->type
) == GLSL_TYPE_ARRAY
||
711 glsl_type_is_matrix(val
->type
)) {
712 unsigned elems
= glsl_get_length(val
->type
);
713 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
715 nir_deref_array
*deref
= nir_deref_array_create(b
);
716 deref
->deref_array_type
= nir_deref_array_type_direct
;
717 deref
->deref
.type
= glsl_get_array_element(val
->type
);
718 src_deref_tail
->child
= &deref
->deref
;
719 for (unsigned i
= 0; i
< elems
; i
++) {
720 deref
->base_offset
= i
;
721 val
->elems
[i
] = _vtn_variable_load(b
, src_deref
, &deref
->deref
);
724 assert(glsl_get_base_type(val
->type
) == GLSL_TYPE_STRUCT
);
725 unsigned elems
= glsl_get_length(val
->type
);
726 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
728 nir_deref_struct
*deref
= nir_deref_struct_create(b
, 0);
729 src_deref_tail
->child
= &deref
->deref
;
730 for (unsigned i
= 0; i
< elems
; i
++) {
732 deref
->deref
.type
= glsl_get_struct_field(val
->type
, i
);
733 val
->elems
[i
] = _vtn_variable_load(b
, src_deref
, &deref
->deref
);
737 src_deref_tail
->child
= old_child
;
743 _vtn_variable_store(struct vtn_builder
*b
, nir_deref_var
*dest_deref
,
744 nir_deref
*dest_deref_tail
, struct vtn_ssa_value
*src
)
746 nir_deref
*old_child
= dest_deref_tail
->child
;
748 if (glsl_type_is_vector_or_scalar(src
->type
)) {
749 nir_intrinsic_instr
*store
=
750 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_store_var
);
751 store
->variables
[0] =
752 nir_deref_as_var(nir_copy_deref(store
, &dest_deref
->deref
));
753 store
->src
[0] = nir_src_for_ssa(src
->def
);
755 nir_builder_instr_insert(&b
->nb
, &store
->instr
);
756 } else if (glsl_get_base_type(src
->type
) == GLSL_TYPE_ARRAY
||
757 glsl_type_is_matrix(src
->type
)) {
758 unsigned elems
= glsl_get_length(src
->type
);
760 nir_deref_array
*deref
= nir_deref_array_create(b
);
761 deref
->deref_array_type
= nir_deref_array_type_direct
;
762 deref
->deref
.type
= glsl_get_array_element(src
->type
);
763 dest_deref_tail
->child
= &deref
->deref
;
764 for (unsigned i
= 0; i
< elems
; i
++) {
765 deref
->base_offset
= i
;
766 _vtn_variable_store(b
, dest_deref
, &deref
->deref
, src
->elems
[i
]);
769 assert(glsl_get_base_type(src
->type
) == GLSL_TYPE_STRUCT
);
770 unsigned elems
= glsl_get_length(src
->type
);
772 nir_deref_struct
*deref
= nir_deref_struct_create(b
, 0);
773 dest_deref_tail
->child
= &deref
->deref
;
774 for (unsigned i
= 0; i
< elems
; i
++) {
776 deref
->deref
.type
= glsl_get_struct_field(src
->type
, i
);
777 _vtn_variable_store(b
, dest_deref
, &deref
->deref
, src
->elems
[i
]);
781 dest_deref_tail
->child
= old_child
;
785 * Gets the NIR-level deref tail, which may have as a child an array deref
786 * selecting which component due to OpAccessChain supporting per-component
787 * indexing in SPIR-V.
791 get_deref_tail(nir_deref_var
*deref
)
793 nir_deref
*cur
= &deref
->deref
;
794 while (!glsl_type_is_vector_or_scalar(cur
->type
) && cur
->child
)
800 static nir_ssa_def
*vtn_vector_extract(struct vtn_builder
*b
,
801 nir_ssa_def
*src
, unsigned index
);
803 static nir_ssa_def
*vtn_vector_extract_dynamic(struct vtn_builder
*b
,
807 static struct vtn_ssa_value
*
808 vtn_variable_load(struct vtn_builder
*b
, nir_deref_var
*src
)
810 nir_deref
*src_tail
= get_deref_tail(src
);
811 struct vtn_ssa_value
*val
= _vtn_variable_load(b
, src
, src_tail
);
813 if (src_tail
->child
) {
814 nir_deref_array
*vec_deref
= nir_deref_as_array(src_tail
->child
);
815 assert(vec_deref
->deref
.child
== NULL
);
816 val
->type
= vec_deref
->deref
.type
;
817 if (vec_deref
->deref_array_type
== nir_deref_array_type_direct
)
818 val
->def
= vtn_vector_extract(b
, val
->def
, vec_deref
->base_offset
);
820 val
->def
= vtn_vector_extract_dynamic(b
, val
->def
,
821 vec_deref
->indirect
.ssa
);
827 static nir_ssa_def
* vtn_vector_insert(struct vtn_builder
*b
,
828 nir_ssa_def
*src
, nir_ssa_def
*insert
,
831 static nir_ssa_def
* vtn_vector_insert_dynamic(struct vtn_builder
*b
,
836 vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
839 nir_deref
*dest_tail
= get_deref_tail(dest
);
840 if (dest_tail
->child
) {
841 struct vtn_ssa_value
*val
= _vtn_variable_load(b
, dest
, dest_tail
);
842 nir_deref_array
*deref
= nir_deref_as_array(dest_tail
->child
);
843 assert(deref
->deref
.child
== NULL
);
844 if (deref
->deref_array_type
== nir_deref_array_type_direct
)
845 val
->def
= vtn_vector_insert(b
, val
->def
, src
->def
,
848 val
->def
= vtn_vector_insert_dynamic(b
, val
->def
, src
->def
,
849 deref
->indirect
.ssa
);
850 _vtn_variable_store(b
, dest
, dest_tail
, val
);
852 _vtn_variable_store(b
, dest
, dest_tail
, src
);
857 vtn_variable_copy(struct vtn_builder
*b
, nir_deref_var
*src
,
860 nir_deref
*src_tail
= get_deref_tail(src
);
862 if (src_tail
->child
) {
863 assert(get_deref_tail(dest
)->child
);
864 struct vtn_ssa_value
*val
= vtn_variable_load(b
, src
);
865 vtn_variable_store(b
, val
, dest
);
867 nir_intrinsic_instr
*copy
=
868 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_copy_var
);
869 copy
->variables
[0] = nir_deref_as_var(nir_copy_deref(copy
, &dest
->deref
));
870 copy
->variables
[1] = nir_deref_as_var(nir_copy_deref(copy
, &src
->deref
));
872 nir_builder_instr_insert(&b
->nb
, ©
->instr
);
877 vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
878 const uint32_t *w
, unsigned count
)
881 case SpvOpVariable
: {
882 const struct glsl_type
*type
=
883 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
884 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_deref
);
886 nir_variable
*var
= ralloc(b
->shader
, nir_variable
);
889 var
->name
= ralloc_strdup(var
, val
->name
);
891 switch ((SpvStorageClass
)w
[3]) {
892 case SpvStorageClassUniform
:
893 case SpvStorageClassUniformConstant
:
894 var
->data
.mode
= nir_var_uniform
;
895 var
->data
.read_only
= true;
896 var
->interface_type
= type
;
898 case SpvStorageClassInput
:
899 var
->data
.mode
= nir_var_shader_in
;
900 var
->data
.read_only
= true;
902 case SpvStorageClassOutput
:
903 var
->data
.mode
= nir_var_shader_out
;
905 case SpvStorageClassPrivateGlobal
:
906 var
->data
.mode
= nir_var_global
;
908 case SpvStorageClassFunction
:
909 var
->data
.mode
= nir_var_local
;
911 case SpvStorageClassWorkgroupLocal
:
912 case SpvStorageClassWorkgroupGlobal
:
913 case SpvStorageClassGeneric
:
914 case SpvStorageClassPrivate
:
915 case SpvStorageClassAtomicCounter
:
917 unreachable("Unhandled variable storage class");
922 var
->constant_initializer
=
923 vtn_value(b
, w
[4], vtn_value_type_constant
)->constant
;
926 val
->deref
= nir_deref_var_create(b
, var
);
928 vtn_foreach_decoration(b
, val
, var_decoration_cb
, var
);
930 if (b
->execution_model
== SpvExecutionModelFragment
&&
931 var
->data
.mode
== nir_var_shader_out
) {
932 var
->data
.location
+= FRAG_RESULT_DATA0
;
933 } else if (b
->execution_model
== SpvExecutionModelVertex
&&
934 var
->data
.mode
== nir_var_shader_in
) {
935 var
->data
.location
+= VERT_ATTRIB_GENERIC0
;
936 } else if (var
->data
.mode
== nir_var_shader_in
||
937 var
->data
.mode
== nir_var_shader_out
) {
938 var
->data
.location
+= VARYING_SLOT_VAR0
;
941 switch (var
->data
.mode
) {
942 case nir_var_shader_in
:
943 exec_list_push_tail(&b
->shader
->inputs
, &var
->node
);
945 case nir_var_shader_out
:
946 exec_list_push_tail(&b
->shader
->outputs
, &var
->node
);
949 exec_list_push_tail(&b
->shader
->globals
, &var
->node
);
952 exec_list_push_tail(&b
->impl
->locals
, &var
->node
);
954 case nir_var_uniform
:
955 exec_list_push_tail(&b
->shader
->uniforms
, &var
->node
);
957 case nir_var_system_value
:
958 exec_list_push_tail(&b
->shader
->system_values
, &var
->node
);
964 case SpvOpAccessChain
:
965 case SpvOpInBoundsAccessChain
: {
966 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_deref
);
967 nir_deref_var
*base
= vtn_value(b
, w
[3], vtn_value_type_deref
)->deref
;
968 val
->deref
= nir_deref_as_var(nir_copy_deref(b
, &base
->deref
));
970 nir_deref
*tail
= &val
->deref
->deref
;
974 for (unsigned i
= 0; i
< count
- 4; i
++) {
975 assert(w
[i
+ 4] < b
->value_id_bound
);
976 struct vtn_value
*idx_val
= &b
->values
[w
[i
+ 4]];
978 enum glsl_base_type base_type
= glsl_get_base_type(tail
->type
);
982 case GLSL_TYPE_FLOAT
:
983 case GLSL_TYPE_DOUBLE
:
985 case GLSL_TYPE_ARRAY
: {
986 nir_deref_array
*deref_arr
= nir_deref_array_create(b
);
987 if (base_type
== GLSL_TYPE_ARRAY
) {
988 deref_arr
->deref
.type
= glsl_get_array_element(tail
->type
);
989 } else if (glsl_type_is_matrix(tail
->type
)) {
990 deref_arr
->deref
.type
= glsl_get_column_type(tail
->type
);
992 assert(glsl_type_is_vector(tail
->type
));
993 deref_arr
->deref
.type
= glsl_scalar_type(base_type
);
996 if (idx_val
->value_type
== vtn_value_type_constant
) {
997 unsigned idx
= idx_val
->constant
->value
.u
[0];
998 deref_arr
->deref_array_type
= nir_deref_array_type_direct
;
999 deref_arr
->base_offset
= idx
;
1001 assert(idx_val
->value_type
== vtn_value_type_ssa
);
1002 deref_arr
->deref_array_type
= nir_deref_array_type_indirect
;
1003 deref_arr
->base_offset
= 0;
1004 deref_arr
->indirect
=
1005 nir_src_for_ssa(vtn_ssa_value(b
, w
[1])->def
);
1007 tail
->child
= &deref_arr
->deref
;
1011 case GLSL_TYPE_STRUCT
: {
1012 assert(idx_val
->value_type
== vtn_value_type_constant
);
1013 unsigned idx
= idx_val
->constant
->value
.u
[0];
1014 nir_deref_struct
*deref_struct
= nir_deref_struct_create(b
, idx
);
1015 deref_struct
->deref
.type
= glsl_get_struct_field(tail
->type
, idx
);
1016 tail
->child
= &deref_struct
->deref
;
1020 unreachable("Invalid type for deref");
1027 case SpvOpCopyMemory
: {
1028 nir_deref_var
*dest
= vtn_value(b
, w
[1], vtn_value_type_deref
)->deref
;
1029 nir_deref_var
*src
= vtn_value(b
, w
[2], vtn_value_type_deref
)->deref
;
1031 vtn_variable_copy(b
, src
, dest
);
1036 nir_deref_var
*src
= vtn_value(b
, w
[3], vtn_value_type_deref
)->deref
;
1037 const struct glsl_type
*src_type
= nir_deref_tail(&src
->deref
)->type
;
1039 if (glsl_get_base_type(src_type
) == GLSL_TYPE_SAMPLER
) {
1040 vtn_push_value(b
, w
[2], vtn_value_type_deref
)->deref
= src
;
1044 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1045 val
->ssa
= vtn_variable_load(b
, src
);
1050 nir_deref_var
*dest
= vtn_value(b
, w
[1], vtn_value_type_deref
)->deref
;
1051 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[2]);
1052 vtn_variable_store(b
, src
, dest
);
1056 case SpvOpVariableArray
:
1057 case SpvOpCopyMemorySized
:
1058 case SpvOpArrayLength
:
1059 case SpvOpImagePointer
:
1061 unreachable("Unhandled opcode");
1066 vtn_handle_function_call(struct vtn_builder
*b
, SpvOp opcode
,
1067 const uint32_t *w
, unsigned count
)
1069 unreachable("Unhandled opcode");
1072 static struct vtn_ssa_value
*
1073 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
1075 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
1078 if (!glsl_type_is_vector_or_scalar(type
)) {
1079 unsigned elems
= glsl_get_length(type
);
1080 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1081 for (unsigned i
= 0; i
< elems
; i
++) {
1082 const struct glsl_type
*child_type
;
1084 switch (glsl_get_base_type(type
)) {
1086 case GLSL_TYPE_UINT
:
1087 case GLSL_TYPE_BOOL
:
1088 case GLSL_TYPE_FLOAT
:
1089 case GLSL_TYPE_DOUBLE
:
1090 child_type
= glsl_get_column_type(type
);
1092 case GLSL_TYPE_ARRAY
:
1093 child_type
= glsl_get_array_element(type
);
1095 case GLSL_TYPE_STRUCT
:
1096 child_type
= glsl_get_struct_field(type
, i
);
1099 unreachable("unkown base type");
1102 val
->elems
[i
] = vtn_create_ssa_value(b
, child_type
);
1110 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
1113 src
.src
= nir_src_for_ssa(vtn_value(b
, index
, vtn_value_type_ssa
)->ssa
->def
);
1114 src
.src_type
= type
;
1119 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
1120 const uint32_t *w
, unsigned count
)
1122 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1123 nir_deref_var
*sampler
= vtn_value(b
, w
[3], vtn_value_type_deref
)->deref
;
1125 nir_tex_src srcs
[8]; /* 8 should be enough */
1126 nir_tex_src
*p
= srcs
;
1128 unsigned coord_components
= 0;
1130 case SpvOpTextureSample
:
1131 case SpvOpTextureSampleDref
:
1132 case SpvOpTextureSampleLod
:
1133 case SpvOpTextureSampleProj
:
1134 case SpvOpTextureSampleGrad
:
1135 case SpvOpTextureSampleOffset
:
1136 case SpvOpTextureSampleProjLod
:
1137 case SpvOpTextureSampleProjGrad
:
1138 case SpvOpTextureSampleLodOffset
:
1139 case SpvOpTextureSampleProjOffset
:
1140 case SpvOpTextureSampleGradOffset
:
1141 case SpvOpTextureSampleProjLodOffset
:
1142 case SpvOpTextureSampleProjGradOffset
:
1143 case SpvOpTextureFetchTexelLod
:
1144 case SpvOpTextureFetchTexelOffset
:
1145 case SpvOpTextureFetchSample
:
1146 case SpvOpTextureFetchTexel
:
1147 case SpvOpTextureGather
:
1148 case SpvOpTextureGatherOffset
:
1149 case SpvOpTextureGatherOffsets
:
1150 case SpvOpTextureQueryLod
: {
1151 /* All these types have the coordinate as their first real argument */
1152 struct vtn_ssa_value
*coord
= vtn_ssa_value(b
, w
[4]);
1153 coord_components
= glsl_get_vector_elements(coord
->type
);
1154 p
->src
= nir_src_for_ssa(coord
->def
);
1155 p
->src_type
= nir_tex_src_coord
;
1166 case SpvOpTextureSample
:
1167 texop
= nir_texop_tex
;
1170 texop
= nir_texop_txb
;
1171 *p
++ = vtn_tex_src(b
, w
[5], nir_tex_src_bias
);
1175 case SpvOpTextureSampleDref
:
1176 case SpvOpTextureSampleLod
:
1177 case SpvOpTextureSampleProj
:
1178 case SpvOpTextureSampleGrad
:
1179 case SpvOpTextureSampleOffset
:
1180 case SpvOpTextureSampleProjLod
:
1181 case SpvOpTextureSampleProjGrad
:
1182 case SpvOpTextureSampleLodOffset
:
1183 case SpvOpTextureSampleProjOffset
:
1184 case SpvOpTextureSampleGradOffset
:
1185 case SpvOpTextureSampleProjLodOffset
:
1186 case SpvOpTextureSampleProjGradOffset
:
1187 case SpvOpTextureFetchTexelLod
:
1188 case SpvOpTextureFetchTexelOffset
:
1189 case SpvOpTextureFetchSample
:
1190 case SpvOpTextureFetchTexel
:
1191 case SpvOpTextureGather
:
1192 case SpvOpTextureGatherOffset
:
1193 case SpvOpTextureGatherOffsets
:
1194 case SpvOpTextureQuerySizeLod
:
1195 case SpvOpTextureQuerySize
:
1196 case SpvOpTextureQueryLod
:
1197 case SpvOpTextureQueryLevels
:
1198 case SpvOpTextureQuerySamples
:
1200 unreachable("Unhandled opcode");
1203 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
1205 const struct glsl_type
*sampler_type
= nir_deref_tail(&sampler
->deref
)->type
;
1206 instr
->sampler_dim
= glsl_get_sampler_dim(sampler_type
);
1208 switch (glsl_get_sampler_result_type(sampler_type
)) {
1209 case GLSL_TYPE_FLOAT
: instr
->dest_type
= nir_type_float
; break;
1210 case GLSL_TYPE_INT
: instr
->dest_type
= nir_type_int
; break;
1211 case GLSL_TYPE_UINT
: instr
->dest_type
= nir_type_unsigned
; break;
1212 case GLSL_TYPE_BOOL
: instr
->dest_type
= nir_type_bool
; break;
1214 unreachable("Invalid base type for sampler result");
1218 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
1219 instr
->coord_components
= coord_components
;
1220 instr
->is_array
= glsl_sampler_type_is_array(sampler_type
);
1221 instr
->is_shadow
= glsl_sampler_type_is_shadow(sampler_type
);
1223 instr
->sampler
= nir_deref_as_var(nir_copy_deref(instr
, &sampler
->deref
));
1225 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
, 4, NULL
);
1226 val
->ssa
= vtn_create_ssa_value(b
, glsl_vector_type(GLSL_TYPE_FLOAT
, 4));
1227 val
->ssa
->def
= &instr
->dest
.ssa
;
1229 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
1233 static nir_alu_instr
*
1234 create_vec(void *mem_ctx
, unsigned num_components
)
1237 switch (num_components
) {
1238 case 1: op
= nir_op_fmov
; break;
1239 case 2: op
= nir_op_vec2
; break;
1240 case 3: op
= nir_op_vec3
; break;
1241 case 4: op
= nir_op_vec4
; break;
1242 default: unreachable("bad vector size");
1245 nir_alu_instr
*vec
= nir_alu_instr_create(mem_ctx
, op
);
1246 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
, NULL
);
1251 static struct vtn_ssa_value
*
1252 vtn_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
1254 if (src
->transposed
)
1255 return src
->transposed
;
1257 struct vtn_ssa_value
*dest
=
1258 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
1260 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
1261 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
));
1262 if (glsl_type_is_vector_or_scalar(src
->type
)) {
1263 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
1264 vec
->src
[0].swizzle
[0] = i
;
1266 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
1267 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
1268 vec
->src
[j
].swizzle
[0] = i
;
1271 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1272 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
1275 dest
->transposed
= src
;
1281 * Normally, column vectors in SPIR-V correspond to a single NIR SSA
1282 * definition. But for matrix multiplies, we want to do one routine for
1283 * multiplying a matrix by a matrix and then pretend that vectors are matrices
1284 * with one column. So we "wrap" these things, and unwrap the result before we
1288 static struct vtn_ssa_value
*
1289 vtn_wrap_matrix(struct vtn_builder
*b
, struct vtn_ssa_value
*val
)
1294 if (glsl_type_is_matrix(val
->type
))
1297 struct vtn_ssa_value
*dest
= rzalloc(b
, struct vtn_ssa_value
);
1298 dest
->type
= val
->type
;
1299 dest
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, 1);
1300 dest
->elems
[0] = val
;
1305 static struct vtn_ssa_value
*
1306 vtn_unwrap_matrix(struct vtn_ssa_value
*val
)
1308 if (glsl_type_is_matrix(val
->type
))
1311 return val
->elems
[0];
1314 static struct vtn_ssa_value
*
1315 vtn_matrix_multiply(struct vtn_builder
*b
,
1316 struct vtn_ssa_value
*_src0
, struct vtn_ssa_value
*_src1
)
1319 struct vtn_ssa_value
*src0
= vtn_wrap_matrix(b
, _src0
);
1320 struct vtn_ssa_value
*src1
= vtn_wrap_matrix(b
, _src1
);
1321 struct vtn_ssa_value
*src0_transpose
= vtn_wrap_matrix(b
, _src0
->transposed
);
1322 struct vtn_ssa_value
*src1_transpose
= vtn_wrap_matrix(b
, _src1
->transposed
);
1324 unsigned src0_rows
= glsl_get_vector_elements(src0
->type
);
1325 unsigned src0_columns
= glsl_get_matrix_columns(src0
->type
);
1326 unsigned src1_columns
= glsl_get_matrix_columns(src1
->type
);
1328 struct vtn_ssa_value
*dest
=
1329 vtn_create_ssa_value(b
, glsl_matrix_type(glsl_get_base_type(src0
->type
),
1330 src0_rows
, src1_columns
));
1332 dest
= vtn_wrap_matrix(b
, dest
);
1334 bool transpose_result
= false;
1335 if (src0_transpose
&& src1_transpose
) {
1336 /* transpose(A) * transpose(B) = transpose(B * A) */
1337 src1
= src0_transpose
;
1338 src0
= src1_transpose
;
1339 src0_transpose
= NULL
;
1340 src1_transpose
= NULL
;
1341 transpose_result
= true;
1344 if (src0_transpose
&& !src1_transpose
&&
1345 glsl_get_base_type(src0
->type
) == GLSL_TYPE_FLOAT
) {
1346 /* We already have the rows of src0 and the columns of src1 available,
1347 * so we can just take the dot product of each row with each column to
1351 for (unsigned i
= 0; i
< src1_columns
; i
++) {
1352 nir_alu_instr
*vec
= create_vec(b
, src0_rows
);
1353 for (unsigned j
= 0; j
< src0_rows
; j
++) {
1355 nir_src_for_ssa(nir_fdot(&b
->nb
, src0_transpose
->elems
[j
]->def
,
1356 src1
->elems
[i
]->def
));
1359 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1360 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
1363 /* We don't handle the case where src1 is transposed but not src0, since
1364 * the general case only uses individual components of src1 so the
1365 * optimizer should chew through the transpose we emitted for src1.
1368 for (unsigned i
= 0; i
< src1_columns
; i
++) {
1369 /* dest[i] = sum(src0[j] * src1[i][j] for all j) */
1370 dest
->elems
[i
]->def
=
1371 nir_fmul(&b
->nb
, src0
->elems
[0]->def
,
1372 vtn_vector_extract(b
, src1
->elems
[i
]->def
, 0));
1373 for (unsigned j
= 1; j
< src0_columns
; j
++) {
1374 dest
->elems
[i
]->def
=
1375 nir_fadd(&b
->nb
, dest
->elems
[i
]->def
,
1376 nir_fmul(&b
->nb
, src0
->elems
[j
]->def
,
1377 vtn_vector_extract(b
,
1378 src1
->elems
[i
]->def
, j
)));
1383 dest
= vtn_unwrap_matrix(dest
);
1385 if (transpose_result
)
1386 dest
= vtn_transpose(b
, dest
);
1391 static struct vtn_ssa_value
*
1392 vtn_mat_times_scalar(struct vtn_builder
*b
,
1393 struct vtn_ssa_value
*mat
,
1394 nir_ssa_def
*scalar
)
1396 struct vtn_ssa_value
*dest
= vtn_create_ssa_value(b
, mat
->type
);
1397 for (unsigned i
= 0; i
< glsl_get_matrix_columns(mat
->type
); i
++) {
1398 if (glsl_get_base_type(mat
->type
) == GLSL_TYPE_FLOAT
)
1399 dest
->elems
[i
]->def
= nir_fmul(&b
->nb
, mat
->elems
[i
]->def
, scalar
);
1401 dest
->elems
[i
]->def
= nir_imul(&b
->nb
, mat
->elems
[i
]->def
, scalar
);
1408 vtn_handle_matrix_alu(struct vtn_builder
*b
, SpvOp opcode
,
1409 const uint32_t *w
, unsigned count
)
1411 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1414 case SpvOpTranspose
: {
1415 struct vtn_ssa_value
*src
= vtn_ssa_value(b
, w
[3]);
1416 val
->ssa
= vtn_transpose(b
, src
);
1420 case SpvOpOuterProduct
: {
1421 struct vtn_ssa_value
*src0
= vtn_ssa_value(b
, w
[3]);
1422 struct vtn_ssa_value
*src1
= vtn_ssa_value(b
, w
[4]);
1424 val
->ssa
= vtn_matrix_multiply(b
, src0
, vtn_transpose(b
, src1
));
1428 case SpvOpMatrixTimesScalar
: {
1429 struct vtn_ssa_value
*mat
= vtn_ssa_value(b
, w
[3]);
1430 struct vtn_ssa_value
*scalar
= vtn_ssa_value(b
, w
[4]);
1432 if (mat
->transposed
) {
1433 val
->ssa
= vtn_transpose(b
, vtn_mat_times_scalar(b
, mat
->transposed
,
1436 val
->ssa
= vtn_mat_times_scalar(b
, mat
, scalar
->def
);
1441 case SpvOpVectorTimesMatrix
:
1442 case SpvOpMatrixTimesVector
:
1443 case SpvOpMatrixTimesMatrix
: {
1444 struct vtn_ssa_value
*src0
= vtn_ssa_value(b
, w
[3]);
1445 struct vtn_ssa_value
*src1
= vtn_ssa_value(b
, w
[4]);
1447 val
->ssa
= vtn_matrix_multiply(b
, src0
, src1
);
1451 default: unreachable("unknown matrix opcode");
1456 vtn_handle_alu(struct vtn_builder
*b
, SpvOp opcode
,
1457 const uint32_t *w
, unsigned count
)
1459 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1460 const struct glsl_type
*type
=
1461 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
1462 val
->ssa
= vtn_create_ssa_value(b
, type
);
1464 /* Collect the various SSA sources */
1465 unsigned num_inputs
= count
- 3;
1466 nir_ssa_def
*src
[4];
1467 for (unsigned i
= 0; i
< num_inputs
; i
++)
1468 src
[i
] = vtn_ssa_value(b
, w
[i
+ 3])->def
;
1470 /* Indicates that the first two arguments should be swapped. This is
1471 * used for implementing greater-than and less-than-or-equal.
1477 /* Basic ALU operations */
1478 case SpvOpSNegate
: op
= nir_op_ineg
; break;
1479 case SpvOpFNegate
: op
= nir_op_fneg
; break;
1480 case SpvOpNot
: op
= nir_op_inot
; break;
1483 switch (src
[0]->num_components
) {
1484 case 1: op
= nir_op_imov
; break;
1485 case 2: op
= nir_op_bany2
; break;
1486 case 3: op
= nir_op_bany3
; break;
1487 case 4: op
= nir_op_bany4
; break;
1492 switch (src
[0]->num_components
) {
1493 case 1: op
= nir_op_imov
; break;
1494 case 2: op
= nir_op_ball2
; break;
1495 case 3: op
= nir_op_ball3
; break;
1496 case 4: op
= nir_op_ball4
; break;
1500 case SpvOpIAdd
: op
= nir_op_iadd
; break;
1501 case SpvOpFAdd
: op
= nir_op_fadd
; break;
1502 case SpvOpISub
: op
= nir_op_isub
; break;
1503 case SpvOpFSub
: op
= nir_op_fsub
; break;
1504 case SpvOpIMul
: op
= nir_op_imul
; break;
1505 case SpvOpFMul
: op
= nir_op_fmul
; break;
1506 case SpvOpUDiv
: op
= nir_op_udiv
; break;
1507 case SpvOpSDiv
: op
= nir_op_idiv
; break;
1508 case SpvOpFDiv
: op
= nir_op_fdiv
; break;
1509 case SpvOpUMod
: op
= nir_op_umod
; break;
1510 case SpvOpSMod
: op
= nir_op_umod
; break; /* FIXME? */
1511 case SpvOpFMod
: op
= nir_op_fmod
; break;
1514 assert(src
[0]->num_components
== src
[1]->num_components
);
1515 switch (src
[0]->num_components
) {
1516 case 1: op
= nir_op_fmul
; break;
1517 case 2: op
= nir_op_fdot2
; break;
1518 case 3: op
= nir_op_fdot3
; break;
1519 case 4: op
= nir_op_fdot4
; break;
1523 case SpvOpShiftRightLogical
: op
= nir_op_ushr
; break;
1524 case SpvOpShiftRightArithmetic
: op
= nir_op_ishr
; break;
1525 case SpvOpShiftLeftLogical
: op
= nir_op_ishl
; break;
1526 case SpvOpLogicalOr
: op
= nir_op_ior
; break;
1527 case SpvOpLogicalXor
: op
= nir_op_ixor
; break;
1528 case SpvOpLogicalAnd
: op
= nir_op_iand
; break;
1529 case SpvOpBitwiseOr
: op
= nir_op_ior
; break;
1530 case SpvOpBitwiseXor
: op
= nir_op_ixor
; break;
1531 case SpvOpBitwiseAnd
: op
= nir_op_iand
; break;
1532 case SpvOpSelect
: op
= nir_op_bcsel
; break;
1533 case SpvOpIEqual
: op
= nir_op_ieq
; break;
1535 /* Comparisons: (TODO: How do we want to handled ordered/unordered?) */
1536 case SpvOpFOrdEqual
: op
= nir_op_feq
; break;
1537 case SpvOpFUnordEqual
: op
= nir_op_feq
; break;
1538 case SpvOpINotEqual
: op
= nir_op_ine
; break;
1539 case SpvOpFOrdNotEqual
: op
= nir_op_fne
; break;
1540 case SpvOpFUnordNotEqual
: op
= nir_op_fne
; break;
1541 case SpvOpULessThan
: op
= nir_op_ult
; break;
1542 case SpvOpSLessThan
: op
= nir_op_ilt
; break;
1543 case SpvOpFOrdLessThan
: op
= nir_op_flt
; break;
1544 case SpvOpFUnordLessThan
: op
= nir_op_flt
; break;
1545 case SpvOpUGreaterThan
: op
= nir_op_ult
; swap
= true; break;
1546 case SpvOpSGreaterThan
: op
= nir_op_ilt
; swap
= true; break;
1547 case SpvOpFOrdGreaterThan
: op
= nir_op_flt
; swap
= true; break;
1548 case SpvOpFUnordGreaterThan
: op
= nir_op_flt
; swap
= true; break;
1549 case SpvOpULessThanEqual
: op
= nir_op_uge
; swap
= true; break;
1550 case SpvOpSLessThanEqual
: op
= nir_op_ige
; swap
= true; break;
1551 case SpvOpFOrdLessThanEqual
: op
= nir_op_fge
; swap
= true; break;
1552 case SpvOpFUnordLessThanEqual
: op
= nir_op_fge
; swap
= true; break;
1553 case SpvOpUGreaterThanEqual
: op
= nir_op_uge
; break;
1554 case SpvOpSGreaterThanEqual
: op
= nir_op_ige
; break;
1555 case SpvOpFOrdGreaterThanEqual
: op
= nir_op_fge
; break;
1556 case SpvOpFUnordGreaterThanEqual
:op
= nir_op_fge
; break;
1559 case SpvOpConvertFToU
: op
= nir_op_f2u
; break;
1560 case SpvOpConvertFToS
: op
= nir_op_f2i
; break;
1561 case SpvOpConvertSToF
: op
= nir_op_i2f
; break;
1562 case SpvOpConvertUToF
: op
= nir_op_u2f
; break;
1563 case SpvOpBitcast
: op
= nir_op_imov
; break;
1566 op
= nir_op_imov
; /* TODO: NIR is 32-bit only; these are no-ops. */
1573 case SpvOpDPdx
: op
= nir_op_fddx
; break;
1574 case SpvOpDPdy
: op
= nir_op_fddy
; break;
1575 case SpvOpDPdxFine
: op
= nir_op_fddx_fine
; break;
1576 case SpvOpDPdyFine
: op
= nir_op_fddy_fine
; break;
1577 case SpvOpDPdxCoarse
: op
= nir_op_fddx_coarse
; break;
1578 case SpvOpDPdyCoarse
: op
= nir_op_fddy_coarse
; break;
1580 val
->ssa
->def
= nir_fadd(&b
->nb
,
1581 nir_fabs(&b
->nb
, nir_fddx(&b
->nb
, src
[0])),
1582 nir_fabs(&b
->nb
, nir_fddx(&b
->nb
, src
[1])));
1584 case SpvOpFwidthFine
:
1585 val
->ssa
->def
= nir_fadd(&b
->nb
,
1586 nir_fabs(&b
->nb
, nir_fddx_fine(&b
->nb
, src
[0])),
1587 nir_fabs(&b
->nb
, nir_fddx_fine(&b
->nb
, src
[1])));
1589 case SpvOpFwidthCoarse
:
1590 val
->ssa
->def
= nir_fadd(&b
->nb
,
1591 nir_fabs(&b
->nb
, nir_fddx_coarse(&b
->nb
, src
[0])),
1592 nir_fabs(&b
->nb
, nir_fddx_coarse(&b
->nb
, src
[1])));
1595 case SpvOpVectorTimesScalar
:
1596 /* The builder will take care of splatting for us. */
1597 val
->ssa
->def
= nir_fmul(&b
->nb
, src
[0], src
[1]);
1602 unreachable("No NIR equivalent");
1608 case SpvOpSignBitSet
:
1609 case SpvOpLessOrGreater
:
1611 case SpvOpUnordered
:
1613 unreachable("Unhandled opcode");
1617 nir_ssa_def
*tmp
= src
[0];
1622 nir_alu_instr
*instr
= nir_alu_instr_create(b
->shader
, op
);
1623 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
.dest
,
1624 glsl_get_vector_elements(type
), val
->name
);
1625 val
->ssa
->def
= &instr
->dest
.dest
.ssa
;
1627 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++)
1628 instr
->src
[i
].src
= nir_src_for_ssa(src
[i
]);
1630 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
1633 static nir_ssa_def
*
1634 vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
, unsigned index
)
1636 unsigned swiz
[4] = { index
};
1637 return nir_swizzle(&b
->nb
, src
, swiz
, 1, true);
1641 static nir_ssa_def
*
1642 vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
, nir_ssa_def
*insert
,
1645 nir_alu_instr
*vec
= create_vec(b
->shader
, src
->num_components
);
1647 for (unsigned i
= 0; i
< src
->num_components
; i
++) {
1649 vec
->src
[i
].src
= nir_src_for_ssa(insert
);
1651 vec
->src
[i
].src
= nir_src_for_ssa(src
);
1652 vec
->src
[i
].swizzle
[0] = i
;
1656 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1658 return &vec
->dest
.dest
.ssa
;
1661 static nir_ssa_def
*
1662 vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
1665 nir_ssa_def
*dest
= vtn_vector_extract(b
, src
, 0);
1666 for (unsigned i
= 1; i
< src
->num_components
; i
++)
1667 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
1668 vtn_vector_extract(b
, src
, i
), dest
);
1673 static nir_ssa_def
*
1674 vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
1675 nir_ssa_def
*insert
, nir_ssa_def
*index
)
1677 nir_ssa_def
*dest
= vtn_vector_insert(b
, src
, insert
, 0);
1678 for (unsigned i
= 1; i
< src
->num_components
; i
++)
1679 dest
= nir_bcsel(&b
->nb
, nir_ieq(&b
->nb
, index
, nir_imm_int(&b
->nb
, i
)),
1680 vtn_vector_insert(b
, src
, insert
, i
), dest
);
1685 static nir_ssa_def
*
1686 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
1687 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
1688 const uint32_t *indices
)
1690 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
);
1692 nir_ssa_undef_instr
*undef
= nir_ssa_undef_instr_create(b
->shader
, 1);
1693 nir_builder_instr_insert(&b
->nb
, &undef
->instr
);
1695 for (unsigned i
= 0; i
< num_components
; i
++) {
1696 uint32_t index
= indices
[i
];
1697 if (index
== 0xffffffff) {
1698 vec
->src
[i
].src
= nir_src_for_ssa(&undef
->def
);
1699 } else if (index
< src0
->num_components
) {
1700 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
1701 vec
->src
[i
].swizzle
[0] = index
;
1703 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
1704 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
1708 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1710 return &vec
->dest
.dest
.ssa
;
1714 * Concatentates a number of vectors/scalars together to produce a vector
1716 static nir_ssa_def
*
1717 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
1718 unsigned num_srcs
, nir_ssa_def
**srcs
)
1720 nir_alu_instr
*vec
= create_vec(b
->shader
, num_components
);
1722 unsigned dest_idx
= 0;
1723 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1724 nir_ssa_def
*src
= srcs
[i
];
1725 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
1726 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
1727 vec
->src
[dest_idx
].swizzle
[0] = j
;
1732 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
1734 return &vec
->dest
.dest
.ssa
;
1737 static struct vtn_ssa_value
*
1738 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
1740 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
1741 dest
->type
= src
->type
;
1743 if (glsl_type_is_vector_or_scalar(src
->type
)) {
1744 dest
->def
= src
->def
;
1746 unsigned elems
= glsl_get_length(src
->type
);
1748 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
1749 for (unsigned i
= 0; i
< elems
; i
++)
1750 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
1756 static struct vtn_ssa_value
*
1757 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
1758 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
1759 unsigned num_indices
)
1761 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
1763 struct vtn_ssa_value
*cur
= dest
;
1765 for (i
= 0; i
< num_indices
- 1; i
++) {
1766 cur
= cur
->elems
[indices
[i
]];
1769 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
1770 /* According to the SPIR-V spec, OpCompositeInsert may work down to
1771 * the component granularity. In that case, the last index will be
1772 * the index to insert the scalar into the vector.
1775 cur
->def
= vtn_vector_insert(b
, cur
->def
, insert
->def
, indices
[i
]);
1777 cur
->elems
[indices
[i
]] = insert
;
1783 static struct vtn_ssa_value
*
1784 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
1785 const uint32_t *indices
, unsigned num_indices
)
1787 struct vtn_ssa_value
*cur
= src
;
1788 for (unsigned i
= 0; i
< num_indices
; i
++) {
1789 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
1790 assert(i
== num_indices
- 1);
1791 /* According to the SPIR-V spec, OpCompositeExtract may work down to
1792 * the component granularity. The last index will be the index of the
1793 * vector to extract.
1796 struct vtn_ssa_value
*ret
= rzalloc(b
, struct vtn_ssa_value
);
1797 ret
->type
= glsl_scalar_type(glsl_get_base_type(cur
->type
));
1798 ret
->def
= vtn_vector_extract(b
, cur
->def
, indices
[i
]);
1807 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
1808 const uint32_t *w
, unsigned count
)
1810 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1811 const struct glsl_type
*type
=
1812 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
1813 val
->ssa
= vtn_create_ssa_value(b
, type
);
1816 case SpvOpVectorExtractDynamic
:
1817 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
1818 vtn_ssa_value(b
, w
[4])->def
);
1821 case SpvOpVectorInsertDynamic
:
1822 val
->ssa
->def
= vtn_vector_insert_dynamic(b
, vtn_ssa_value(b
, w
[3])->def
,
1823 vtn_ssa_value(b
, w
[4])->def
,
1824 vtn_ssa_value(b
, w
[5])->def
);
1827 case SpvOpVectorShuffle
:
1828 val
->ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
),
1829 vtn_ssa_value(b
, w
[3])->def
,
1830 vtn_ssa_value(b
, w
[4])->def
,
1834 case SpvOpCompositeConstruct
: {
1835 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
1836 unsigned elems
= count
- 3;
1837 if (glsl_type_is_vector_or_scalar(type
)) {
1838 nir_ssa_def
*srcs
[4];
1839 for (unsigned i
= 0; i
< elems
; i
++)
1840 srcs
[i
] = vtn_ssa_value(b
, w
[3 + i
])->def
;
1842 vtn_vector_construct(b
, glsl_get_vector_elements(type
),
1845 val
->ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
1846 for (unsigned i
= 0; i
< elems
; i
++)
1847 val
->ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
1851 case SpvOpCompositeExtract
:
1852 val
->ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
1856 case SpvOpCompositeInsert
:
1857 val
->ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
1858 vtn_ssa_value(b
, w
[3]),
1862 case SpvOpCopyObject
:
1863 val
->ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
1867 unreachable("unknown composite operation");
1872 vtn_phi_node_init(struct vtn_builder
*b
, struct vtn_ssa_value
*val
)
1874 if (glsl_type_is_vector_or_scalar(val
->type
)) {
1875 nir_phi_instr
*phi
= nir_phi_instr_create(b
->shader
);
1876 nir_ssa_dest_init(&phi
->instr
, &phi
->dest
,
1877 glsl_get_vector_elements(val
->type
), NULL
);
1878 exec_list_make_empty(&phi
->srcs
);
1879 nir_builder_instr_insert(&b
->nb
, &phi
->instr
);
1880 val
->def
= &phi
->dest
.ssa
;
1882 unsigned elems
= glsl_get_length(val
->type
);
1883 for (unsigned i
= 0; i
< elems
; i
++)
1884 vtn_phi_node_init(b
, val
->elems
[i
]);
1888 static struct vtn_ssa_value
*
1889 vtn_phi_node_create(struct vtn_builder
*b
, const struct glsl_type
*type
)
1891 struct vtn_ssa_value
*val
= vtn_create_ssa_value(b
, type
);
1892 vtn_phi_node_init(b
, val
);
1897 vtn_handle_phi_first_pass(struct vtn_builder
*b
, const uint32_t *w
)
1899 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
1900 const struct glsl_type
*type
=
1901 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
1902 val
->ssa
= vtn_phi_node_create(b
, type
);
1906 vtn_phi_node_add_src(struct vtn_ssa_value
*phi
, const nir_block
*pred
,
1907 struct vtn_ssa_value
*val
)
1909 assert(phi
->type
== val
->type
);
1910 if (glsl_type_is_vector_or_scalar(phi
->type
)) {
1911 nir_phi_instr
*phi_instr
= nir_instr_as_phi(phi
->def
->parent_instr
);
1912 nir_phi_src
*src
= ralloc(phi_instr
, nir_phi_src
);
1913 src
->pred
= (nir_block
*) pred
;
1914 src
->src
= nir_src_for_ssa(val
->def
);
1915 exec_list_push_tail(&phi_instr
->srcs
, &src
->node
);
1917 unsigned elems
= glsl_get_length(phi
->type
);
1918 for (unsigned i
= 0; i
< elems
; i
++)
1919 vtn_phi_node_add_src(phi
->elems
[i
], pred
, val
->elems
[i
]);
1923 static struct vtn_ssa_value
*
1924 vtn_get_phi_node_src(struct vtn_builder
*b
, nir_block
*block
,
1925 const struct glsl_type
*type
, const uint32_t *w
,
1928 struct hash_entry
*entry
= _mesa_hash_table_search(b
->block_table
, block
);
1930 struct vtn_block
*spv_block
= entry
->data
;
1931 for (unsigned off
= 4; off
< count
; off
+= 2) {
1932 if (spv_block
== vtn_value(b
, w
[off
], vtn_value_type_block
)->block
) {
1933 return vtn_ssa_value(b
, w
[off
- 1]);
1938 nir_builder_insert_before_block(&b
->nb
, block
);
1939 struct vtn_ssa_value
*phi
= vtn_phi_node_create(b
, type
);
1941 struct set_entry
*entry2
;
1942 set_foreach(block
->predecessors
, entry2
) {
1943 nir_block
*pred
= (nir_block
*) entry2
->key
;
1944 struct vtn_ssa_value
*val
= vtn_get_phi_node_src(b
, pred
, type
, w
,
1946 vtn_phi_node_add_src(phi
, pred
, val
);
1953 vtn_handle_phi_second_pass(struct vtn_builder
*b
, SpvOp opcode
,
1954 const uint32_t *w
, unsigned count
)
1956 if (opcode
== SpvOpLabel
) {
1957 b
->block
= vtn_value(b
, w
[1], vtn_value_type_block
)->block
;
1961 if (opcode
!= SpvOpPhi
)
1964 struct vtn_ssa_value
*phi
= vtn_value(b
, w
[2], vtn_value_type_ssa
)->ssa
;
1966 struct set_entry
*entry
;
1967 set_foreach(b
->block
->block
->predecessors
, entry
) {
1968 nir_block
*pred
= (nir_block
*) entry
->key
;
1970 struct vtn_ssa_value
*val
= vtn_get_phi_node_src(b
, pred
, phi
->type
, w
,
1972 vtn_phi_node_add_src(phi
, pred
, val
);
1979 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
1980 const uint32_t *w
, unsigned count
)
1984 case SpvOpSourceExtension
:
1985 case SpvOpCompileFlag
:
1986 case SpvOpExtension
:
1987 /* Unhandled, but these are for debug so that's ok. */
1990 case SpvOpExtInstImport
:
1991 vtn_handle_extension(b
, opcode
, w
, count
);
1994 case SpvOpMemoryModel
:
1995 assert(w
[1] == SpvAddressingModelLogical
);
1996 assert(w
[2] == SpvMemoryModelGLSL450
);
1999 case SpvOpEntryPoint
:
2000 assert(b
->entry_point
== NULL
);
2001 b
->entry_point
= &b
->values
[w
[2]];
2002 b
->execution_model
= w
[1];
2005 case SpvOpExecutionMode
:
2006 unreachable("Execution modes not yet implemented");
2010 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
2011 vtn_string_literal(b
, &w
[2], count
- 2);
2015 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2);
2018 case SpvOpMemberName
:
2023 break; /* Ignored for now */
2025 case SpvOpDecorationGroup
:
2027 case SpvOpMemberDecorate
:
2028 case SpvOpGroupDecorate
:
2029 case SpvOpGroupMemberDecorate
:
2030 vtn_handle_decoration(b
, opcode
, w
, count
);
2036 case SpvOpTypeFloat
:
2037 case SpvOpTypeVector
:
2038 case SpvOpTypeMatrix
:
2039 case SpvOpTypeSampler
:
2040 case SpvOpTypeArray
:
2041 case SpvOpTypeRuntimeArray
:
2042 case SpvOpTypeStruct
:
2043 case SpvOpTypeOpaque
:
2044 case SpvOpTypePointer
:
2045 case SpvOpTypeFunction
:
2046 case SpvOpTypeEvent
:
2047 case SpvOpTypeDeviceEvent
:
2048 case SpvOpTypeReserveId
:
2049 case SpvOpTypeQueue
:
2051 vtn_handle_type(b
, opcode
, w
, count
);
2054 case SpvOpConstantTrue
:
2055 case SpvOpConstantFalse
:
2057 case SpvOpConstantComposite
:
2058 case SpvOpConstantSampler
:
2059 case SpvOpConstantNullPointer
:
2060 case SpvOpConstantNullObject
:
2061 case SpvOpSpecConstantTrue
:
2062 case SpvOpSpecConstantFalse
:
2063 case SpvOpSpecConstant
:
2064 case SpvOpSpecConstantComposite
:
2065 vtn_handle_constant(b
, opcode
, w
, count
);
2069 vtn_handle_variables(b
, opcode
, w
, count
);
2073 return false; /* End of preamble */
2080 vtn_handle_first_cfg_pass_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2081 const uint32_t *w
, unsigned count
)
2084 case SpvOpFunction
: {
2085 assert(b
->func
== NULL
);
2086 b
->func
= rzalloc(b
, struct vtn_function
);
2088 const struct glsl_type
*result_type
=
2089 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
2090 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_function
);
2091 const struct glsl_type
*func_type
=
2092 vtn_value(b
, w
[4], vtn_value_type_type
)->type
->type
;
2094 assert(glsl_get_function_return_type(func_type
) == result_type
);
2096 nir_function
*func
=
2097 nir_function_create(b
->shader
, ralloc_strdup(b
->shader
, val
->name
));
2099 nir_function_overload
*overload
= nir_function_overload_create(func
);
2100 overload
->num_params
= glsl_get_length(func_type
);
2101 overload
->params
= ralloc_array(overload
, nir_parameter
,
2102 overload
->num_params
);
2103 for (unsigned i
= 0; i
< overload
->num_params
; i
++) {
2104 const struct glsl_function_param
*param
=
2105 glsl_get_function_param(func_type
, i
);
2106 overload
->params
[i
].type
= param
->type
;
2109 overload
->params
[i
].param_type
= nir_parameter_inout
;
2111 overload
->params
[i
].param_type
= nir_parameter_in
;
2115 overload
->params
[i
].param_type
= nir_parameter_out
;
2117 assert(!"Parameter is neither in nor out");
2121 b
->func
->overload
= overload
;
2125 case SpvOpFunctionEnd
:
2130 case SpvOpFunctionParameter
:
2131 break; /* Does nothing */
2134 assert(b
->block
== NULL
);
2135 b
->block
= rzalloc(b
, struct vtn_block
);
2136 b
->block
->label
= w
;
2137 vtn_push_value(b
, w
[1], vtn_value_type_block
)->block
= b
->block
;
2139 if (b
->func
->start_block
== NULL
) {
2140 /* This is the first block encountered for this function. In this
2141 * case, we set the start block and add it to the list of
2142 * implemented functions that we'll walk later.
2144 b
->func
->start_block
= b
->block
;
2145 exec_list_push_tail(&b
->functions
, &b
->func
->node
);
2151 case SpvOpBranchConditional
:
2155 case SpvOpReturnValue
:
2156 case SpvOpUnreachable
:
2158 b
->block
->branch
= w
;
2162 case SpvOpSelectionMerge
:
2163 case SpvOpLoopMerge
:
2164 assert(b
->block
&& b
->block
->merge_op
== SpvOpNop
);
2165 b
->block
->merge_op
= opcode
;
2166 b
->block
->merge_block_id
= w
[1];
2170 /* Continue on as per normal */
2178 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
2179 const uint32_t *w
, unsigned count
)
2183 struct vtn_block
*block
= vtn_value(b
, w
[1], vtn_value_type_block
)->block
;
2184 assert(block
->block
== NULL
);
2186 struct exec_node
*list_tail
= exec_list_get_tail(b
->nb
.cf_node_list
);
2187 nir_cf_node
*tail_node
= exec_node_data(nir_cf_node
, list_tail
, node
);
2188 assert(tail_node
->type
== nir_cf_node_block
);
2189 block
->block
= nir_cf_node_as_block(tail_node
);
2193 case SpvOpLoopMerge
:
2194 case SpvOpSelectionMerge
:
2195 /* This is handled by cfg pre-pass and walk_blocks */
2199 vtn_push_value(b
, w
[2], vtn_value_type_undef
);
2203 vtn_handle_extension(b
, opcode
, w
, count
);
2207 case SpvOpVariableArray
:
2210 case SpvOpCopyMemory
:
2211 case SpvOpCopyMemorySized
:
2212 case SpvOpAccessChain
:
2213 case SpvOpInBoundsAccessChain
:
2214 case SpvOpArrayLength
:
2215 case SpvOpImagePointer
:
2216 vtn_handle_variables(b
, opcode
, w
, count
);
2219 case SpvOpFunctionCall
:
2220 vtn_handle_function_call(b
, opcode
, w
, count
);
2223 case SpvOpTextureSample
:
2224 case SpvOpTextureSampleDref
:
2225 case SpvOpTextureSampleLod
:
2226 case SpvOpTextureSampleProj
:
2227 case SpvOpTextureSampleGrad
:
2228 case SpvOpTextureSampleOffset
:
2229 case SpvOpTextureSampleProjLod
:
2230 case SpvOpTextureSampleProjGrad
:
2231 case SpvOpTextureSampleLodOffset
:
2232 case SpvOpTextureSampleProjOffset
:
2233 case SpvOpTextureSampleGradOffset
:
2234 case SpvOpTextureSampleProjLodOffset
:
2235 case SpvOpTextureSampleProjGradOffset
:
2236 case SpvOpTextureFetchTexelLod
:
2237 case SpvOpTextureFetchTexelOffset
:
2238 case SpvOpTextureFetchSample
:
2239 case SpvOpTextureFetchTexel
:
2240 case SpvOpTextureGather
:
2241 case SpvOpTextureGatherOffset
:
2242 case SpvOpTextureGatherOffsets
:
2243 case SpvOpTextureQuerySizeLod
:
2244 case SpvOpTextureQuerySize
:
2245 case SpvOpTextureQueryLod
:
2246 case SpvOpTextureQueryLevels
:
2247 case SpvOpTextureQuerySamples
:
2248 vtn_handle_texture(b
, opcode
, w
, count
);
2256 case SpvOpConvertFToU
:
2257 case SpvOpConvertFToS
:
2258 case SpvOpConvertSToF
:
2259 case SpvOpConvertUToF
:
2263 case SpvOpConvertPtrToU
:
2264 case SpvOpConvertUToPtr
:
2265 case SpvOpPtrCastToGeneric
:
2266 case SpvOpGenericCastToPtr
:
2272 case SpvOpSignBitSet
:
2273 case SpvOpLessOrGreater
:
2275 case SpvOpUnordered
:
2290 case SpvOpVectorTimesScalar
:
2292 case SpvOpShiftRightLogical
:
2293 case SpvOpShiftRightArithmetic
:
2294 case SpvOpShiftLeftLogical
:
2295 case SpvOpLogicalOr
:
2296 case SpvOpLogicalXor
:
2297 case SpvOpLogicalAnd
:
2298 case SpvOpBitwiseOr
:
2299 case SpvOpBitwiseXor
:
2300 case SpvOpBitwiseAnd
:
2303 case SpvOpFOrdEqual
:
2304 case SpvOpFUnordEqual
:
2305 case SpvOpINotEqual
:
2306 case SpvOpFOrdNotEqual
:
2307 case SpvOpFUnordNotEqual
:
2308 case SpvOpULessThan
:
2309 case SpvOpSLessThan
:
2310 case SpvOpFOrdLessThan
:
2311 case SpvOpFUnordLessThan
:
2312 case SpvOpUGreaterThan
:
2313 case SpvOpSGreaterThan
:
2314 case SpvOpFOrdGreaterThan
:
2315 case SpvOpFUnordGreaterThan
:
2316 case SpvOpULessThanEqual
:
2317 case SpvOpSLessThanEqual
:
2318 case SpvOpFOrdLessThanEqual
:
2319 case SpvOpFUnordLessThanEqual
:
2320 case SpvOpUGreaterThanEqual
:
2321 case SpvOpSGreaterThanEqual
:
2322 case SpvOpFOrdGreaterThanEqual
:
2323 case SpvOpFUnordGreaterThanEqual
:
2329 case SpvOpFwidthFine
:
2330 case SpvOpDPdxCoarse
:
2331 case SpvOpDPdyCoarse
:
2332 case SpvOpFwidthCoarse
:
2333 vtn_handle_alu(b
, opcode
, w
, count
);
2336 case SpvOpTranspose
:
2337 case SpvOpOuterProduct
:
2338 case SpvOpMatrixTimesScalar
:
2339 case SpvOpVectorTimesMatrix
:
2340 case SpvOpMatrixTimesVector
:
2341 case SpvOpMatrixTimesMatrix
:
2342 vtn_handle_matrix_alu(b
, opcode
, w
, count
);
2345 case SpvOpVectorExtractDynamic
:
2346 case SpvOpVectorInsertDynamic
:
2347 case SpvOpVectorShuffle
:
2348 case SpvOpCompositeConstruct
:
2349 case SpvOpCompositeExtract
:
2350 case SpvOpCompositeInsert
:
2351 case SpvOpCopyObject
:
2352 vtn_handle_composite(b
, opcode
, w
, count
);
2356 vtn_handle_phi_first_pass(b
, w
);
2360 unreachable("Unhandled opcode");
2367 vtn_walk_blocks(struct vtn_builder
*b
, struct vtn_block
*start
,
2368 struct vtn_block
*break_block
, struct vtn_block
*cont_block
,
2369 struct vtn_block
*end_block
)
2371 struct vtn_block
*block
= start
;
2372 while (block
!= end_block
) {
2373 if (block
->merge_op
== SpvOpLoopMerge
) {
2374 /* This is the jump into a loop. */
2375 struct vtn_block
*new_cont_block
= block
;
2376 struct vtn_block
*new_break_block
=
2377 vtn_value(b
, block
->merge_block_id
, vtn_value_type_block
)->block
;
2379 nir_loop
*loop
= nir_loop_create(b
->shader
);
2380 nir_cf_node_insert_end(b
->nb
.cf_node_list
, &loop
->cf_node
);
2382 struct exec_list
*old_list
= b
->nb
.cf_node_list
;
2384 /* Reset the merge_op to prerevent infinite recursion */
2385 block
->merge_op
= SpvOpNop
;
2387 nir_builder_insert_after_cf_list(&b
->nb
, &loop
->body
);
2388 vtn_walk_blocks(b
, block
, new_break_block
, new_cont_block
, NULL
);
2390 nir_builder_insert_after_cf_list(&b
->nb
, old_list
);
2391 block
= new_break_block
;
2395 const uint32_t *w
= block
->branch
;
2396 SpvOp branch_op
= w
[0] & SpvOpCodeMask
;
2399 vtn_foreach_instruction(b
, block
->label
, block
->branch
,
2400 vtn_handle_body_instruction
);
2402 nir_cf_node
*cur_cf_node
=
2403 exec_node_data(nir_cf_node
, exec_list_get_tail(b
->nb
.cf_node_list
),
2405 nir_block
*cur_block
= nir_cf_node_as_block(cur_cf_node
);
2406 _mesa_hash_table_insert(b
->block_table
, cur_block
, block
);
2408 switch (branch_op
) {
2410 struct vtn_block
*branch_block
=
2411 vtn_value(b
, w
[1], vtn_value_type_block
)->block
;
2413 if (branch_block
== break_block
) {
2414 nir_jump_instr
*jump
= nir_jump_instr_create(b
->shader
,
2416 nir_builder_instr_insert(&b
->nb
, &jump
->instr
);
2419 } else if (branch_block
== cont_block
) {
2420 nir_jump_instr
*jump
= nir_jump_instr_create(b
->shader
,
2422 nir_builder_instr_insert(&b
->nb
, &jump
->instr
);
2425 } else if (branch_block
== end_block
) {
2426 /* We're branching to the merge block of an if, since for loops
2427 * and functions end_block == NULL, so we're done here.
2431 /* We're branching to another block, and according to the rules,
2432 * we can only branch to another block with one predecessor (so
2433 * we're the only one jumping to it) so we can just process it
2436 block
= branch_block
;
2441 case SpvOpBranchConditional
: {
2442 /* Gather up the branch blocks */
2443 struct vtn_block
*then_block
=
2444 vtn_value(b
, w
[2], vtn_value_type_block
)->block
;
2445 struct vtn_block
*else_block
=
2446 vtn_value(b
, w
[3], vtn_value_type_block
)->block
;
2448 nir_if
*if_stmt
= nir_if_create(b
->shader
);
2449 if_stmt
->condition
= nir_src_for_ssa(vtn_ssa_value(b
, w
[1])->def
);
2450 nir_cf_node_insert_end(b
->nb
.cf_node_list
, &if_stmt
->cf_node
);
2452 if (then_block
== break_block
) {
2453 nir_jump_instr
*jump
= nir_jump_instr_create(b
->shader
,
2455 nir_instr_insert_after_cf_list(&if_stmt
->then_list
,
2458 } else if (else_block
== break_block
) {
2459 nir_jump_instr
*jump
= nir_jump_instr_create(b
->shader
,
2461 nir_instr_insert_after_cf_list(&if_stmt
->else_list
,
2464 } else if (then_block
== cont_block
) {
2465 nir_jump_instr
*jump
= nir_jump_instr_create(b
->shader
,
2467 nir_instr_insert_after_cf_list(&if_stmt
->then_list
,
2470 } else if (else_block
== cont_block
) {
2471 nir_jump_instr
*jump
= nir_jump_instr_create(b
->shader
,
2473 nir_instr_insert_after_cf_list(&if_stmt
->else_list
,
2477 /* According to the rules we're branching to two blocks that don't
2478 * have any other predecessors, so we can handle this as a
2481 assert(block
->merge_op
== SpvOpSelectionMerge
);
2482 struct vtn_block
*merge_block
=
2483 vtn_value(b
, block
->merge_block_id
, vtn_value_type_block
)->block
;
2485 struct exec_list
*old_list
= b
->nb
.cf_node_list
;
2487 nir_builder_insert_after_cf_list(&b
->nb
, &if_stmt
->then_list
);
2488 vtn_walk_blocks(b
, then_block
, break_block
, cont_block
, merge_block
);
2490 nir_builder_insert_after_cf_list(&b
->nb
, &if_stmt
->else_list
);
2491 vtn_walk_blocks(b
, else_block
, break_block
, cont_block
, merge_block
);
2493 nir_builder_insert_after_cf_list(&b
->nb
, old_list
);
2494 block
= merge_block
;
2498 /* If we got here then we inserted a predicated break or continue
2499 * above and we need to handle the other case. We already set
2500 * `block` above to indicate what block to visit after the
2504 /* It's possible that the other branch is also a break/continue.
2505 * If it is, we handle that here.
2507 if (block
== break_block
) {
2508 nir_jump_instr
*jump
= nir_jump_instr_create(b
->shader
,
2510 nir_builder_instr_insert(&b
->nb
, &jump
->instr
);
2513 } else if (block
== cont_block
) {
2514 nir_jump_instr
*jump
= nir_jump_instr_create(b
->shader
,
2516 nir_builder_instr_insert(&b
->nb
, &jump
->instr
);
2521 /* If we got here then there was a predicated break/continue but
2522 * the other half of the if has stuff in it. `block` was already
2523 * set above so there is nothing left for us to do.
2529 nir_jump_instr
*jump
= nir_jump_instr_create(b
->shader
,
2531 nir_builder_instr_insert(&b
->nb
, &jump
->instr
);
2536 nir_intrinsic_instr
*discard
=
2537 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_discard
);
2538 nir_builder_instr_insert(&b
->nb
, &discard
->instr
);
2543 case SpvOpReturnValue
:
2544 case SpvOpUnreachable
:
2546 unreachable("Unhandled opcode");
2552 spirv_to_nir(const uint32_t *words
, size_t word_count
,
2553 const nir_shader_compiler_options
*options
)
2555 const uint32_t *word_end
= words
+ word_count
;
2557 /* Handle the SPIR-V header (first 4 dwords) */
2558 assert(word_count
> 5);
2560 assert(words
[0] == SpvMagicNumber
);
2561 assert(words
[1] == 99);
2562 /* words[2] == generator magic */
2563 unsigned value_id_bound
= words
[3];
2564 assert(words
[4] == 0);
2568 nir_shader
*shader
= nir_shader_create(NULL
, options
);
2570 /* Initialize the stn_builder object */
2571 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
2573 b
->value_id_bound
= value_id_bound
;
2574 b
->values
= ralloc_array(b
, struct vtn_value
, value_id_bound
);
2575 exec_list_make_empty(&b
->functions
);
2577 /* Handle all the preamble instructions */
2578 words
= vtn_foreach_instruction(b
, words
, word_end
,
2579 vtn_handle_preamble_instruction
);
2581 /* Do a very quick CFG analysis pass */
2582 vtn_foreach_instruction(b
, words
, word_end
,
2583 vtn_handle_first_cfg_pass_instruction
);
2585 foreach_list_typed(struct vtn_function
, func
, node
, &b
->functions
) {
2586 b
->impl
= nir_function_impl_create(func
->overload
);
2587 b
->const_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
2588 _mesa_key_pointer_equal
);
2589 b
->block_table
= _mesa_hash_table_create(b
, _mesa_hash_pointer
,
2590 _mesa_key_pointer_equal
);
2591 nir_builder_init(&b
->nb
, b
->impl
);
2592 nir_builder_insert_after_cf_list(&b
->nb
, &b
->impl
->body
);
2593 vtn_walk_blocks(b
, func
->start_block
, NULL
, NULL
, NULL
);
2594 vtn_foreach_instruction(b
, func
->start_block
->label
, func
->end
,
2595 vtn_handle_phi_second_pass
);