[PATCH vkd3d 1/6] vkd3d-shader/hlsl: Fold constant swizzles.

Giovanni Mascellani gmascellani at codeweavers.com
Mon Jun 27 10:22:38 CDT 2022


From: Zebediah Figura <zfigura at codeweavers.com>

Signed-off-by: Giovanni Mascellani <gmascellani at codeweavers.com>
---
 libs/vkd3d-shader/hlsl.h              |  3 ++-
 libs/vkd3d-shader/hlsl_codegen.c      |  3 ++-
 libs/vkd3d-shader/hlsl_constant_ops.c | 31 ++++++++++++++++++++++++++-
 3 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h
index 28b2ff1b..3c1dd08d 100644
--- a/libs/vkd3d-shader/hlsl.h
+++ b/libs/vkd3d-shader/hlsl.h
@@ -800,7 +800,8 @@ bool hlsl_offset_from_deref(struct hlsl_ctx *ctx, const struct hlsl_deref *deref
 unsigned int hlsl_offset_from_deref_safe(struct hlsl_ctx *ctx, const struct hlsl_deref *deref);
 struct hlsl_reg hlsl_reg_from_deref(struct hlsl_ctx *ctx, const struct hlsl_deref *deref);
 
-bool hlsl_fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context);
+bool hlsl_fold_constant_exprs(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context);
+bool hlsl_fold_constant_swizzles(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context);
 
 bool hlsl_sm1_register_from_semantic(struct hlsl_ctx *ctx, const struct hlsl_semantic *semantic,
         bool output, D3DSHADER_PARAM_REGISTER_TYPE *type, unsigned int *reg);
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c
index 1a5e9055..bc7f0a62 100644
--- a/libs/vkd3d-shader/hlsl_codegen.c
+++ b/libs/vkd3d-shader/hlsl_codegen.c
@@ -1946,7 +1946,8 @@ int hlsl_emit_bytecode(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry
     transform_ir(ctx, lower_casts_to_bool, body, NULL);
     do
     {
-        progress = transform_ir(ctx, hlsl_fold_constants, body, NULL);
+        progress = transform_ir(ctx, hlsl_fold_constant_exprs, body, NULL);
+        progress |= transform_ir(ctx, hlsl_fold_constant_swizzles, body, NULL);
         progress |= copy_propagation_execute(ctx, body);
         progress |= transform_ir(ctx, remove_trivial_swizzles, body, NULL);
     }
diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c
index d8787c21..64067397 100644
--- a/libs/vkd3d-shader/hlsl_constant_ops.c
+++ b/libs/vkd3d-shader/hlsl_constant_ops.c
@@ -369,7 +369,7 @@ static bool fold_mod(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst,
     return true;
 }
 
-bool hlsl_fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context)
+bool hlsl_fold_constant_exprs(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context)
 {
     struct hlsl_ir_constant *arg1, *arg2 = NULL, *res;
     struct hlsl_ir_expr *expr;
@@ -447,3 +447,32 @@ bool hlsl_fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void
     }
     return success;
 }
+
+bool hlsl_fold_constant_swizzles(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context)
+{
+    struct hlsl_ir_constant *value, *res;
+    struct hlsl_ir_swizzle *swizzle;
+    unsigned int i, swizzle_bits;
+
+    if (instr->type != HLSL_IR_SWIZZLE)
+        return false;
+    swizzle = hlsl_ir_swizzle(instr);
+    if (swizzle->val.node->type != HLSL_IR_CONSTANT)
+        return false;
+    value = hlsl_ir_constant(swizzle->val.node);
+
+    if (!(res = hlsl_alloc(ctx, sizeof(*res))))
+        return false;
+    init_node(&res->node, HLSL_IR_CONSTANT, instr->data_type, instr->loc);
+
+    swizzle_bits = swizzle->swizzle;
+    for (i = 0; i < swizzle->node.data_type->dimx; ++i)
+    {
+        res->value[i] = value->value[swizzle_bits & 3];
+        swizzle_bits >>= 2;
+    }
+
+    list_add_before(&swizzle->node.entry, &res->node.entry);
+    hlsl_replace_node(&swizzle->node, &res->node);
+    return true;
+}
-- 
2.36.1




More information about the wine-devel mailing list