[PATCH vkd3d 06/17] vkd3d-shader/hlsl: Lower scalar-to-vector casts to swizzles.
Matteo Bruni
mbruni at codeweavers.com
Wed Dec 1 10:14:55 CST 2021
From: Zebediah Figura <zfigura at codeweavers.com>
Signed-off-by: Matteo Bruni <mbruni at codeweavers.com>
---
changelog: Create a new cast instruction instead of reusing the
existing one. Also I added a comment to clarify why the cast is
preserved e.g. instead of skipping the cast instruction and swizzling
the cast operand directly.
Makefile.am | 1 -
libs/vkd3d-shader/hlsl_codegen.c | 38 ++++++++++++++++++++++++++++++++
2 files changed, 38 insertions(+), 1 deletion(-)
diff --git a/Makefile.am b/Makefile.am
index be3d8ec1..4010533a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -290,7 +290,6 @@ XFAIL_TESTS = \
tests/conditional.shader_test \
tests/hlsl-array-dimension.shader_test \
tests/hlsl-bool-cast.shader_test \
- tests/hlsl-comma.shader_test \
tests/hlsl-cross.shader_test \
tests/hlsl-duplicate-modifiers.shader_test \
tests/hlsl-for.shader_test \
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c
index 6773ab81..3cc41d4e 100644
--- a/libs/vkd3d-shader/hlsl_codegen.c
+++ b/libs/vkd3d-shader/hlsl_codegen.c
@@ -237,6 +237,43 @@ static void replace_node(struct hlsl_ir_node *old, struct hlsl_ir_node *new)
hlsl_free_instr(old);
}
+/* Lower casts from vec1 to vecN to swizzles. */
+static bool lower_broadcasts(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context)
+{
+ const struct hlsl_type *src_type, *dst_type;
+ struct hlsl_type *dst_scalar_type;
+ struct hlsl_ir_expr *cast;
+
+ if (instr->type != HLSL_IR_EXPR)
+ return false;
+ cast = hlsl_ir_expr(instr);
+ src_type = cast->operands[0].node->data_type;
+ dst_type = cast->node.data_type;
+
+ if (cast->op == HLSL_OP1_CAST
+ && src_type->type <= HLSL_CLASS_VECTOR && dst_type->type <= HLSL_CLASS_VECTOR
+ && src_type->dimx == 1)
+ {
+ struct hlsl_ir_swizzle *swizzle;
+ struct hlsl_ir_expr *new_cast;
+
+ dst_scalar_type = hlsl_get_scalar_type(ctx, dst_type->base_type);
+ /* We need to preserve the cast since it might be doing more than just
+ * turning the scalar into a vector. */
+ if (!(new_cast = hlsl_new_cast(ctx, cast->operands[0].node, dst_scalar_type, &cast->node.loc)))
+ return false;
+ list_add_after(&cast->node.entry, &new_cast->node.entry);
+ if (!(swizzle = hlsl_new_swizzle(ctx, HLSL_SWIZZLE(X, X, X, X), dst_type->dimx, &new_cast->node, &cast->node.loc)))
+ return false;
+ list_add_after(&new_cast->node.entry, &swizzle->node.entry);
+
+ replace_node(&cast->node, &swizzle->node);
+ return true;
+ }
+
+ return false;
+}
+
struct copy_propagation_value
{
struct hlsl_ir_node *node;
@@ -1804,6 +1841,7 @@ int hlsl_emit_dxbc(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry_fun
append_output_var_copy(ctx, &body->instrs, entry_func->return_var);
}
+ transform_ir(ctx, lower_broadcasts, body, NULL);
while (transform_ir(ctx, fold_redundant_casts, body, NULL));
do
{
--
2.26.3
More information about the wine-devel
mailing list