This replicates what seems to be happening when using sign() with shader model 3, which is roughly...
``` int pos = (int) (x > 0); int neg = -((int) (x < 0)); int result = pos + neg; ```
Fixes https://bugs.winehq.org/show_bug.cgi?id=54826
-- v8: tests: Add tests for sign() intrinsic. vkd3d-shader/hlsl: Add support for sign() intrinsic.
From: Ethan Lee flibitijibibo@gmail.com
Signed-off-by: Ethan Lee flibitijibibo@gmail.com --- libs/vkd3d-shader/hlsl.y | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 10deda75..4cc2ab56 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -3143,6 +3143,41 @@ static bool intrinsic_saturate(struct hlsl_ctx *ctx, return !!add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_SAT, arg, loc); }
+static bool intrinsic_sign(struct hlsl_ctx *ctx, + const struct parse_initializer *params, const struct vkd3d_shader_location *loc) +{ + struct hlsl_ir_node *zero, *lt, *neg, *op1, *op2, *arg = params->args[0]; + + struct hlsl_type *int_type = hlsl_get_numeric_type(ctx, arg->data_type->class, HLSL_TYPE_INT, + arg->data_type->dimx, arg->data_type->dimy); + + if (!(zero = hlsl_new_float_constant(ctx, 0.0f, loc))) + return false; + list_add_tail(params->instrs, &zero->entry); + + /* Check if 0 < arg, cast bool to int */ + + if (!(lt = add_binary_comparison_expr(ctx, params->instrs, HLSL_OP2_LESS, zero, arg, loc))) + return false; + + if (!(op1 = add_implicit_conversion(ctx, params->instrs, lt, int_type, loc))) + return false; + + /* Check if arg < 0, cast bool to int and invert (meaning true is -1) */ + + if (!(lt = add_binary_comparison_expr(ctx, params->instrs, HLSL_OP2_LESS, arg, zero, loc))) + return false; + + if (!(op2 = add_implicit_conversion(ctx, params->instrs, lt, int_type, loc))) + return false; + + if (!(neg = add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_NEG, op2, loc))) + return false; + + /* Adding these two together will make 1 when > 0, -1 when < 0, and 0 when neither */ + return !!add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_ADD, neg, op1, loc); +} + static bool intrinsic_sin(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) { @@ -3402,6 +3437,7 @@ intrinsic_functions[] = {"round", 1, true, intrinsic_round}, {"rsqrt", 1, true, intrinsic_rsqrt}, {"saturate", 1, true, intrinsic_saturate}, + {"sign", 1, true, intrinsic_sign}, {"sin", 1, true, intrinsic_sin}, {"smoothstep", 3, true, intrinsic_smoothstep}, {"sqrt", 1, true, intrinsic_sqrt},
From: Ethan Lee flibitijibibo@gmail.com
Signed-off-by: Ethan Lee flibitijibibo@gmail.com --- Makefile.am | 1 + tests/sign.shader_test | 45 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 tests/sign.shader_test
diff --git a/Makefile.am b/Makefile.am index 3f1ec171..93c5cf59 100644 --- a/Makefile.am +++ b/Makefile.am @@ -153,6 +153,7 @@ vkd3d_shader_tests = \ tests/saturate.shader_test \ tests/shader-interstage-interface.shader_test \ tests/side-effects.shader_test \ + tests/sign.shader_test \ tests/sqrt.shader_test \ tests/step.shader_test \ tests/swizzle-constant-prop.shader_test \ diff --git a/tests/sign.shader_test b/tests/sign.shader_test new file mode 100644 index 00000000..c63f3d35 --- /dev/null +++ b/tests/sign.shader_test @@ -0,0 +1,45 @@ +[pixel shader] +uniform float f; + +float4 main() : sv_target +{ + return sign(f); +} + +[test] +uniform 0 float4 1.0 0.0 0.0 0.0 +draw quad +probe all rgba (1.0, 1.0, 1.0, 1.0) +uniform 0 float4 -1.0 0.0 0.0 0.0 +draw quad +probe all rgba (-1.0, -1.0, -1.0, -1.0) +uniform 0 float4 0.0 0.0 0.0 0.0 +draw quad +probe all rgba (0.0, 0.0, 0.0, 0.0) + +[pixel shader] +uniform float4 f; + +float4 main() : sv_target +{ + return sign(f); +} + +[test] +uniform 0 float4 1.0 2.0 3.0 4.0 +draw quad +probe all rgba (1.0, 1.0, 1.0, 1.0) + +[pixel shader] +uniform float2x2 f; + +float4 main() : sv_target +{ + return sign(f); +} + +[test] +uniform 0 float4 1.0 2.0 0.0 0.0 +uniform 4 float4 3.0 4.0 0.0 0.0 +draw quad +probe all rgba (1.0, 1.0, 1.0, 1.0)
This merge request was approved by Zebediah Figura.
Giovanni Mascellani (@giomasce) commented about libs/vkd3d-shader/hlsl.y:
+static bool intrinsic_sign(struct hlsl_ctx *ctx,
const struct parse_initializer *params, const struct vkd3d_shader_location *loc)
+{
- struct hlsl_ir_node *zero, *lt, *neg, *op1, *op2, *arg = params->args[0];
- struct hlsl_type *int_type = hlsl_get_numeric_type(ctx, arg->data_type->class, HLSL_TYPE_INT,
arg->data_type->dimx, arg->data_type->dimy);
- if (!(zero = hlsl_new_float_constant(ctx, 0.0f, loc)))
return false;
- list_add_tail(params->instrs, &zero->entry);
- /* Check if 0 < arg, cast bool to int */
- if (!(lt = add_binary_comparison_expr(ctx, params->instrs, HLSL_OP2_LESS, zero, arg, loc)))
This means that expressions of type other than `float` are casted to `float`. In principle that should be correct in the end, because AFAIU the sign should always be mapped correctly, but it makes me a bit nervous. Could the type of zero be set to coincide with the type of the argument?