From 7516adeeaefe69190e61294641c67420418e5fbd Mon Sep 17 00:00:00 2001 From: Nikolay Sivov Date: Tue, 2 May 2023 18:43:38 +0200 Subject: [PATCH] vkd3d-shader/hlsl: Add support for fmod() intrinsic. --- Makefile.am | 1 + libs/vkd3d-shader/hlsl.y | 44 ++++++++++++++++++++++++++++++++++++++++ tests/fmod.shader_test | 13 ++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 tests/fmod.shader_test diff --git a/Makefile.am b/Makefile.am index f7c5c9a1..cbb1d619 100644 --- a/Makefile.am +++ b/Makefile.am @@ -71,6 +71,7 @@ vkd3d_shader_tests = \ tests/exp.shader_test \ tests/expr-indexing.shader_test \ tests/floor.shader_test \ + tests/fmod.shader_test \ tests/frac.shader_test \ tests/function-return.shader_test \ tests/hlsl-array-dimension.shader_test \ diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 75c0e421..f8d6e8e1 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -2697,6 +2697,49 @@ static bool intrinsic_floor(struct hlsl_ctx *ctx, return !!add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_FLOOR, arg, loc); } +static bool intrinsic_fmod(struct hlsl_ctx *ctx, const struct parse_initializer *params, + const struct vkd3d_shader_location *loc) +{ + struct hlsl_ir_node *x, *y, *div, *abs, *frac, *neg_frac, *ge; + struct hlsl_ir_constant *zero; + struct hlsl_ir_load *select; + unsigned int count, i; + + if (!(x = intrinsic_float_convert_arg(ctx, params, params->args[0], loc))) + return false; + + if (!(y = intrinsic_float_convert_arg(ctx, params, params->args[1], loc))) + return false; + + if (!(div = add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_DIV, x, y, loc))) + return false; + + if (!(zero = hlsl_new_constant(ctx, div->data_type, loc))) + return false; + list_add_tail(params->instrs, &zero->node.entry); + + count = hlsl_type_element_count(div->data_type); + for (i = 0; i < count; ++i) + zero->value.u[i].f = 0.0f; + + if (!(abs = add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_ABS, div, loc))) + return false; + + if (!(frac = add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_FRACT, abs, loc))) + return false; + + if (!(neg_frac = add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_NEG, frac, loc))) + return false; + + if (!(ge = add_binary_comparison_expr(ctx, params->instrs, HLSL_OP2_GEQUAL, div, &zero->node, loc))) + return false; + + if (!(select = hlsl_add_conditional(ctx, params->instrs, ge, frac, neg_frac))) + return false; + + return !!add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_MUL, &select->node, y, loc); +} + static bool intrinsic_frac(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) { @@ -3456,6 +3499,7 @@ intrinsic_functions[] = {"exp", 1, true, intrinsic_exp}, {"exp2", 1, true, intrinsic_exp2}, {"floor", 1, true, intrinsic_floor}, + {"fmod", 2, true, intrinsic_fmod}, {"frac", 1, true, intrinsic_frac}, {"ldexp", 2, true, intrinsic_ldexp}, {"length", 1, true, intrinsic_length}, diff --git a/tests/fmod.shader_test b/tests/fmod.shader_test new file mode 100644 index 00000000..4c9ff988 --- /dev/null +++ b/tests/fmod.shader_test @@ -0,0 +1,13 @@ +[pixel shader] +float4 main(uniform float4 u) : sv_target +{ + return float4(fmod(u.x, u.y), 0, 0, 0); +} + +[test] +uniform 0 float4 -0.5 6.5 0.0 0.0 +draw quad +probe all rgba (-0.5, 0.0, 0.0, 0.0) 4 +uniform 0 float4 1.1 0.3 0.0 0.0 +draw quad +probe all rgba (0.2, 0.0, 0.0, 0.0) 4