I'm working on adding all the intrinsics we haven't implemented yet. Here's sinh, cosh, and tanh.
Sinh/cosh are implemented in the same commit because they forward to the same backing function (because the identities used only differ by a plus or minus).
-- v6: vkd3d-shader/hlsl: Implement tanh. vkd3d-shader/hlsl: Implement hyperbolic sin and cos.
From: Petrichor Park ppark@codeweavers.com
--- libs/vkd3d-shader/hlsl.y | 48 +++++++++++++++++++++++++++++ tests/hlsl/trigonometry.shader_test | 12 ++++---- 2 files changed, 54 insertions(+), 6 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 7fc35d4e1..d59d937e5 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -3028,6 +3028,46 @@ static bool intrinsic_cos(struct hlsl_ctx *ctx, return !!add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_COS, arg, loc); }
+static bool write_cosh_or_sinh(struct hlsl_ctx *ctx, + const struct parse_initializer *params, const struct vkd3d_shader_location *loc, bool sinh_mode) +{ + struct hlsl_ir_function_decl *func; + struct hlsl_ir_node *arg; + const char *fn_name, *type_name; + char *body; + + static const char template[] = + "%s %s(%s x)\n" + "{\n" + " return (exp(x) %s exp(-x)) / 2;\n" + "}\n"; + static const char fn_name_sinh[] = "sinh"; + static const char fn_name_cosh[] = "cosh"; + + if (!(arg = intrinsic_float_convert_arg(ctx, params, params->args[0], loc))) + return false; + + type_name = arg->data_type->name; + fn_name = sinh_mode ? fn_name_sinh : fn_name_cosh; + + if (!(body = hlsl_sprintf_alloc(ctx, template, + type_name, fn_name, type_name, sinh_mode ? "-" : "+"))) + return false; + + func = hlsl_compile_internal_function(ctx, fn_name, body); + vkd3d_free(body); + if (!func) + return false; + + return add_user_call(ctx, func, params, loc); +} + +static bool intrinsic_cosh(struct hlsl_ctx *ctx, + const struct parse_initializer *params, const struct vkd3d_shader_location *loc) +{ + return write_cosh_or_sinh(ctx, params, loc, false); +} + static bool intrinsic_cross(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) { @@ -3873,6 +3913,12 @@ static bool intrinsic_sin(struct hlsl_ctx *ctx, return !!add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_SIN, arg, loc); }
+static bool intrinsic_sinh(struct hlsl_ctx *ctx, + const struct parse_initializer *params, const struct vkd3d_shader_location *loc) +{ + return write_cosh_or_sinh(ctx, params, loc, true); +} + /* smoothstep(a, b, x) = p^2 (3 - 2p), where p = saturate((x - a)/(b - a)) */ static bool intrinsic_smoothstep(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) @@ -4277,6 +4323,7 @@ intrinsic_functions[] = {"clamp", 3, true, intrinsic_clamp}, {"clip", 1, true, intrinsic_clip}, {"cos", 1, true, intrinsic_cos}, + {"cosh", 1, true, intrinsic_cosh}, {"cross", 2, true, intrinsic_cross}, {"ddx", 1, true, intrinsic_ddx}, {"ddx_coarse", 1, true, intrinsic_ddx_coarse}, @@ -4314,6 +4361,7 @@ intrinsic_functions[] = {"saturate", 1, true, intrinsic_saturate}, {"sign", 1, true, intrinsic_sign}, {"sin", 1, true, intrinsic_sin}, + {"sinh", 1, true, intrinsic_sinh}, {"smoothstep", 3, true, intrinsic_smoothstep}, {"sqrt", 1, true, intrinsic_sqrt}, {"step", 2, true, intrinsic_step}, diff --git a/tests/hlsl/trigonometry.shader_test b/tests/hlsl/trigonometry.shader_test index 3f7f11685..d2cfc0670 100644 --- a/tests/hlsl/trigonometry.shader_test +++ b/tests/hlsl/trigonometry.shader_test @@ -76,7 +76,7 @@ todo(sm<4 | glsl) draw quad probe all rgba (0, 1000, -1000.0, 0)
-[pixel shader todo] +[pixel shader] uniform float4 a;
float4 main() : sv_target @@ -86,15 +86,15 @@ float4 main() : sv_target
[test] uniform 0 float4 -6.28318531 -0.88137359 0.88137359 6.28318531 -todo(sm<6) draw quad +todo(glsl) draw quad probe all rgba (-267.744894, -1.0, 1.0, 267.744894) 2 uniform 0 float4 -0.0 0.0 -90.0 90.0 -todo(sm<6) draw quad +todo(glsl) draw quad % mingw does not support "inf" for scanf(), but numbers beyond FLOAT_MAX consistently result in inf. probe all rgba (0.0, 0.0, -1.0e39, 1.0e39) 1
-[pixel shader todo] +[pixel shader] uniform float4 a;
float4 main() : sv_target @@ -104,10 +104,10 @@ float4 main() : sv_target
[test] uniform 0 float4 -1.76274717 -1.3169579 1.3169579 1.76274717 -todo(sm<6) draw quad +todo(glsl) draw quad probe all rgba (3.0, 2.0, 2.0, 3.0) 2 uniform 0 float4 -0.0 0.0 -90.0 90.0 -todo(sm<6) draw quad +todo(glsl) draw quad probe all rgba (1.0, 1.0, 1.0e39, 1.0e39) 1
From: Petrichor Park ppark@codeweavers.com
--- libs/vkd3d-shader/hlsl.y | 34 +++++++++++++++++++++++++++++ tests/hlsl/trigonometry.shader_test | 6 ++--- 2 files changed, 37 insertions(+), 3 deletions(-)
diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index d59d937e5..8f7155675 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -3991,6 +3991,39 @@ static bool intrinsic_tan(struct hlsl_ctx *ctx, return !!add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_DIV, sin, cos, loc); }
+static bool intrinsic_tanh(struct hlsl_ctx *ctx, + const struct parse_initializer *params, const struct vkd3d_shader_location *loc) +{ + struct hlsl_ir_function_decl *func; + struct hlsl_ir_node *arg; + struct hlsl_type *type; + char *body; + + static const char template[] = + "%s tanh(%s x)\n" + "{\n" + " %s exp_pos, exp_neg;\n" + " exp_pos = exp(x);\n" + " exp_neg = exp(-x);\n" + " return (exp_pos - exp_neg) / (exp_pos + exp_neg);\n" + "}\n"; + + if (!(arg = intrinsic_float_convert_arg(ctx, params, params->args[0], loc))) + return false; + type = arg->data_type; + + if (!(body = hlsl_sprintf_alloc(ctx, template, + type->name, type->name, type->name))) + return false; + + func = hlsl_compile_internal_function(ctx, "tanh", body); + vkd3d_free(body); + if (!func) + return false; + + return add_user_call(ctx, func, params, loc); +} + static bool intrinsic_tex(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc, const char *name, enum hlsl_sampler_dim dim) { @@ -4366,6 +4399,7 @@ intrinsic_functions[] = {"sqrt", 1, true, intrinsic_sqrt}, {"step", 2, true, intrinsic_step}, {"tan", 1, true, intrinsic_tan}, + {"tanh", 1, true, intrinsic_tanh}, {"tex1D", -1, false, intrinsic_tex1D}, {"tex2D", -1, false, intrinsic_tex2D}, {"tex2Dlod", 2, false, intrinsic_tex2Dlod}, diff --git a/tests/hlsl/trigonometry.shader_test b/tests/hlsl/trigonometry.shader_test index d2cfc0670..cf6165876 100644 --- a/tests/hlsl/trigonometry.shader_test +++ b/tests/hlsl/trigonometry.shader_test @@ -111,7 +111,7 @@ todo(glsl) draw quad probe all rgba (1.0, 1.0, 1.0e39, 1.0e39) 1
-[pixel shader todo] +[pixel shader] uniform float4 a;
float4 main() : sv_target @@ -121,8 +121,8 @@ float4 main() : sv_target
[test] uniform 0 float4 -1.57079633 -0.54930614 0.54930614 1.57079633 -todo(sm<6) draw quad +todo(glsl) draw quad probe all rgba (-0.91715234, -0.5, 0.5, 0.91715234) 2 uniform 0 float4 -10.0 -0.0 0.0 10.0 -todo(sm<6) draw quad +todo(glsl) draw quad probe all rgba (-1.0, 0.0, 0.0, 1.0) 1
I kept the separate variable for the sinh/cosh function name because it's also used in creating the function object
This merge request was approved by Henri Verbeet.