[PATCH vkd3d 7/8] vkd3d-shader/hlsl: Support negation for all numeric types in fold_constants().

Francisco Casas fcasas at codeweavers.com
Thu Jan 6 11:39:48 CST 2022


Signed-off-by: Francisco Casas <fcasas at codeweavers.com>
---
 libs/vkd3d-shader/hlsl_constant_ops.c | 41 +++++++++++++++++++++++----
 1 file changed, 36 insertions(+), 5 deletions(-)

diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c
index 54c63691..2f34a730 100644
--- a/libs/vkd3d-shader/hlsl_constant_ops.c
+++ b/libs/vkd3d-shader/hlsl_constant_ops.c
@@ -103,6 +103,39 @@ static int constant_op1_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *tgt,
     return 1;
 }
 
+static int constant_op1_neg(struct hlsl_ctx *ctx, struct hlsl_ir_constant *tgt,
+        struct hlsl_ir_constant *src)
+{
+    enum hlsl_base_type type = tgt->node.data_type->base_type;
+
+    assert(type == src->node.data_type->base_type);
+
+    for (int k = 0; k < 4; k++)
+    {
+        switch (type)
+        {
+            case HLSL_TYPE_FLOAT:
+            case HLSL_TYPE_HALF:
+                tgt->value[k].f = -src->value[k].f;
+                break;
+            case HLSL_TYPE_DOUBLE:
+                tgt->value[k].d = -src->value[k].d;
+                break;
+            case HLSL_TYPE_INT:
+                tgt->value[k].i = -src->value[k].i;
+                break;
+            case HLSL_TYPE_UINT:
+                tgt->value[k].u = -src->value[k].u;
+                break;
+            default:
+                FIXME("Fold \"%s\" for type %s.", debug_hlsl_expr_op(HLSL_OP1_NEG),
+                        debug_hlsl_type(ctx, tgt->node.data_type));
+                return 0;
+        }
+    }
+    return 1;
+}
+
 static int constant_op2_add(struct hlsl_ctx *ctx, struct hlsl_ir_constant *tgt,
         struct hlsl_ir_constant *src1, struct hlsl_ir_constant *src2)
 {
@@ -171,6 +204,9 @@ bool fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *cont
         case HLSL_OP1_CAST:
             success = constant_op1_cast(ctx, res, arg1);
             break;
+        case HLSL_OP1_NEG:
+            success = constant_op1_neg(ctx, res, arg1);
+            break;
         case HLSL_OP2_ADD:
             success = constant_op2_add(ctx, res, arg1, arg2);
             break;
@@ -198,11 +234,6 @@ fallback:
         {
             switch (expr->op)
             {
-                case HLSL_OP1_NEG:
-                    for (i = 0; i < instr->data_type->dimx; ++i)
-                        res->value[i].u = -arg1->value[i].u;
-                    break;
-
                 case HLSL_OP2_MUL:
                     for (i = 0; i < instr->data_type->dimx; ++i)
                         res->value[i].u = arg1->value[i].u * arg2->value[i].u;
-- 
2.25.1




More information about the wine-devel mailing list