[PATCH vkd3d 6/6] vkd3d-shader/hlsl: Lower casts to bool to comparisons.

Giovanni Mascellani gmascellani at codeweavers.com
Wed Mar 23 09:27:29 CDT 2022


Signed-off-by: Giovanni Mascellani <gmascellani at codeweavers.com>
---
 libs/vkd3d-shader/hlsl_codegen.c      | 32 +++++++++++++++++++++++++++
 libs/vkd3d-shader/hlsl_constant_ops.c | 10 ++-------
 libs/vkd3d-shader/hlsl_sm4.c          |  4 ++--
 3 files changed, 36 insertions(+), 10 deletions(-)

diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c
index 3d0f9e41..7222ff8a 100644
--- a/libs/vkd3d-shader/hlsl_codegen.c
+++ b/libs/vkd3d-shader/hlsl_codegen.c
@@ -745,6 +745,37 @@ static bool lower_division(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, voi
     return true;
 }
 
+static bool lower_cast_to_bool(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context)
+{
+    struct hlsl_type *type = instr->data_type, *arg_type;
+    struct hlsl_ir_constant *zero;
+    struct hlsl_ir_expr *expr;
+
+    if (instr->type != HLSL_IR_EXPR)
+        return false;
+    expr = hlsl_ir_expr(instr);
+    if (expr->op != HLSL_OP1_CAST)
+        return false;
+    arg_type = expr->operands[0].node->data_type;
+    if (type->type > HLSL_CLASS_VECTOR || arg_type->type > HLSL_CLASS_VECTOR)
+        return false;
+    if (type->base_type != HLSL_TYPE_BOOL)
+        return false;
+
+    /* Narrowing casts should already have been lowered. */
+    assert(type->dimx == arg_type->dimx);
+
+    zero = hlsl_new_constant(ctx, arg_type, &instr->loc);
+    if (!zero)
+        return false;
+    list_add_before(&instr->entry, &zero->node.entry);
+
+    expr->op = HLSL_OP2_NEQUAL;
+    hlsl_src_from_node(&expr->operands[1], &zero->node);
+
+    return true;
+}
+
 static bool dce(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context)
 {
     switch (instr->type)
@@ -1642,6 +1673,7 @@ int hlsl_emit_bytecode(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry
     }
 
     transform_ir(ctx, lower_broadcasts, body, NULL);
+    transform_ir(ctx, lower_cast_to_bool, body, NULL);
     while (transform_ir(ctx, fold_redundant_casts, body, NULL));
     do
     {
diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c
index 2f090863..be06332e 100644
--- a/libs/vkd3d-shader/hlsl_constant_ops.c
+++ b/libs/vkd3d-shader/hlsl_constant_ops.c
@@ -27,7 +27,6 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct
     int32_t i;
     double d;
     float f;
-    bool b;
 
     if (dst->node.data_type->dimx != src->node.data_type->dimx
             || dst->node.data_type->dimy != src->node.data_type->dimy)
@@ -47,7 +46,6 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct
                 i = src->value[k].f;
                 f = src->value[k].f;
                 d = src->value[k].f;
-                b = src->value[k].f;
                 break;
 
             case HLSL_TYPE_DOUBLE:
@@ -55,7 +53,6 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct
                 i = src->value[k].d;
                 f = src->value[k].d;
                 d = src->value[k].d;
-                b = src->value[k].d;
                 break;
 
             case HLSL_TYPE_INT:
@@ -63,7 +60,6 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct
                 i = src->value[k].i;
                 f = src->value[k].i;
                 d = src->value[k].i;
-                b = src->value[k].i;
                 break;
 
             case HLSL_TYPE_UINT:
@@ -71,7 +67,6 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct
                 i = src->value[k].u;
                 f = src->value[k].u;
                 d = src->value[k].u;
-                b = src->value[k].u;
                 break;
 
             case HLSL_TYPE_BOOL:
@@ -79,7 +74,6 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct
                 i = !!src->value[k].b;
                 f = !!src->value[k].b;
                 d = !!src->value[k].b;
-                b = !!src->value[k].b;
                 break;
 
             default:
@@ -108,8 +102,8 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct
                 break;
 
             case HLSL_TYPE_BOOL:
-                dst->value[k].b = b;
-                break;
+                /* Casts to bool should have already been lowered. */
+                assert(0);
 
             default:
                 FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, src->node.data_type),
diff --git a/libs/vkd3d-shader/hlsl_sm4.c b/libs/vkd3d-shader/hlsl_sm4.c
index af5c777f..49ab1a21 100644
--- a/libs/vkd3d-shader/hlsl_sm4.c
+++ b/libs/vkd3d-shader/hlsl_sm4.c
@@ -1533,8 +1533,8 @@ static void write_sm4_cast(struct hlsl_ctx *ctx,
             break;
 
         case HLSL_TYPE_BOOL:
-            hlsl_fixme(ctx, &expr->node.loc, "SM4 cast to bool.\n");
-            break;
+            /* Casts to bool should have already been lowered. */
+            assert(0);
 
         default:
             assert(0);
-- 
2.35.1




More information about the wine-devel mailing list