[PATCH vkd3d] vkd3d-shader/hlsl: Support casts between all numeric types on constant folding.

Francisco Casas fcasas at codeweavers.com
Wed Dec 29 08:51:54 CST 2021


Signed-off-by: Francisco Casas <fcasas at codeweavers.com>
---

This is a proposal to handle all numeric constants in the same way.

Signed-off-by: Francisco Casas <fcasas at codeweavers.com>
---
 Makefile.am                      |  1 -
 libs/vkd3d-shader/hlsl.c         | 54 ++++++++++++++++++++++++++++++++
 libs/vkd3d-shader/hlsl.h         |  3 ++
 libs/vkd3d-shader/hlsl_codegen.c | 48 ++++++++++------------------
 4 files changed, 73 insertions(+), 33 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 20fee06d..39003dfa 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -304,7 +304,6 @@ XFAIL_TESTS = \
 	tests/hlsl-storage-qualifiers.shader_test \
 	tests/hlsl-vector-indexing.shader_test \
 	tests/hlsl-vector-indexing-uniform.shader_test \
-	tests/max.shader_test \
 	tests/sampler.shader_test \
 	tests/texture-load.shader_test \
 	tests/texture-load-typed.shader_test \
diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c
index d2ea4c34..a44b638e 100644
--- a/libs/vkd3d-shader/hlsl.c
+++ b/libs/vkd3d-shader/hlsl.c
@@ -1663,6 +1663,60 @@ unsigned int hlsl_combine_swizzles(unsigned int first, unsigned int second, unsi
     return ret;
 }
 
+void hlsl_cast_constant_value(struct hlsl_ir_constant *con, enum hlsl_base_type current,
+        enum hlsl_base_type target){
+    uint32_t u; int32_t i; float f; double d;
+
+    assert(con);
+    for (int k = 0; k < 4; k++)
+    {
+        switch (current)
+        {
+            case HLSL_TYPE_FLOAT:
+            case HLSL_TYPE_HALF:
+                u = con->value[k].f;  i = con->value[k].f;  f = con->value[k].f;  d = con->value[k].f;
+                break;
+            case HLSL_TYPE_DOUBLE:
+                u = con->value[k].d;  i = con->value[k].d;  f = con->value[k].d;  d = con->value[k].d;
+                break;
+            case HLSL_TYPE_INT:
+                u = con->value[k].i;  i = con->value[k].i;  f = con->value[k].i;  d = con->value[k].i;
+                break;
+            case HLSL_TYPE_UINT:
+                u = con->value[k].u;  i = con->value[k].u;  f = con->value[k].u;  d = con->value[k].u;
+                break;
+            case HLSL_TYPE_BOOL:
+                u = !!con->value[k].u;  i = !!con->value[k].u;  f = !!con->value[k].u;  d = !!con->value[k].u;
+                break;
+            default:
+                assert(0);
+                break;
+        }
+        switch (target)
+        {
+            case HLSL_TYPE_FLOAT:
+            case HLSL_TYPE_HALF:
+                con->value[k].f = f;
+                break;
+            case HLSL_TYPE_DOUBLE:
+                con->value[k].d = d;
+                break;
+            case HLSL_TYPE_INT:
+                con->value[k].i = i;
+                break;
+            case HLSL_TYPE_UINT:
+                con->value[k].u = u;
+                break;
+            case HLSL_TYPE_BOOL:
+                con->value[k].u = (!!u) * 0xffffffff;
+                break;
+            default:
+                assert(0);
+                break;
+        }
+    }
+}
+
 static const struct hlsl_profile_info *get_target_info(const char *target)
 {
     unsigned int i;
diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h
index 57acf3a0..a07ccf17 100644
--- a/libs/vkd3d-shader/hlsl.h
+++ b/libs/vkd3d-shader/hlsl.h
@@ -755,6 +755,9 @@ unsigned int hlsl_combine_writemasks(unsigned int first, unsigned int second);
 unsigned int hlsl_map_swizzle(unsigned int swizzle, unsigned int writemask);
 unsigned int hlsl_swizzle_from_writemask(unsigned int writemask);
 
+void hlsl_cast_constant_value(struct hlsl_ir_constant *con, enum hlsl_base_type current,
+        enum hlsl_base_type target);
+
 bool hlsl_offset_from_deref(const struct hlsl_deref *deref, unsigned int *offset);
 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,
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c
index 75716bdf..ef627f2b 100644
--- a/libs/vkd3d-shader/hlsl_codegen.c
+++ b/libs/vkd3d-shader/hlsl_codegen.c
@@ -656,7 +656,7 @@ static bool fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, voi
 {
     struct hlsl_ir_constant *arg1, *arg2 = NULL, *res;
     struct hlsl_ir_expr *expr;
-    unsigned int i, dimx;
+    unsigned int i;
 
     if (instr->type != HLSL_IR_EXPR)
         return false;
@@ -670,48 +670,32 @@ static bool fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, voi
     arg1 = hlsl_ir_constant(expr->operands[0].node);
     if (expr->operands[1].node)
         arg2 = hlsl_ir_constant(expr->operands[1].node);
-    dimx = instr->data_type->dimx;
 
     if (!(res = hlsl_alloc(ctx, sizeof(*res))))
         return false;
     init_node(&res->node, HLSL_IR_CONSTANT, instr->data_type, instr->loc);
 
+    if (expr->op == HLSL_OP1_CAST && instr->data_type->base_type <= HLSL_TYPE_LAST_SCALAR)
+    {
+        if (instr->data_type->dimx != arg1->node.data_type->dimx
+                || instr->data_type->dimy != arg1->node.data_type->dimy)
+        {
+            WARN("Cast from %s to %s.\n", debug_hlsl_type(ctx, arg1->node.data_type),
+                    debug_hlsl_type(ctx, instr->data_type));
+        }
+        memcpy(res->value, arg1->value, sizeof(res->value));
+        hlsl_cast_constant_value(res, arg1->node.data_type->base_type, instr->data_type->base_type);
+        list_add_before(&expr->node.entry, &res->node.entry);
+        replace_node(&expr->node, &res->node);
+        return res;
+    }
+
     switch (instr->data_type->base_type)
     {
         case HLSL_TYPE_FLOAT:
         {
             switch (expr->op)
             {
-                case HLSL_OP1_CAST:
-                    if (instr->data_type->dimx != arg1->node.data_type->dimx
-                            || instr->data_type->dimy != arg1->node.data_type->dimy)
-                    {
-                        FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, arg1->node.data_type),
-                                debug_hlsl_type(ctx, instr->data_type));
-                        vkd3d_free(res);
-                        return false;
-                    }
-
-                    switch (arg1->node.data_type->base_type)
-                    {
-                        case HLSL_TYPE_INT:
-                            for (i = 0; i < dimx; ++i)
-                                res->value[i].f = arg1->value[i].i;
-                            break;
-
-                        case HLSL_TYPE_UINT:
-                            for (i = 0; i < dimx; ++i)
-                                res->value[i].f = arg1->value[i].u;
-                            break;
-
-                        default:
-                            FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, arg1->node.data_type),
-                                    debug_hlsl_type(ctx, instr->data_type));
-                            vkd3d_free(res);
-                            return false;
-                    }
-                    break;
-
                 default:
                     FIXME("Fold float op %#x.\n", expr->op);
                     vkd3d_free(res);
-- 
2.25.1




More information about the wine-devel mailing list