-- v7: vkd3d-shader/hlsl: Support distance() intrinsic. vkd3d-shader/hlsl: Support rsqrt() intrinsic.
From: Nikolay Sivov nsivov@codeweavers.com
--- libs/vkd3d-shader/hlsl.y | 12 ++++++++++++ tests/sqrt.shader_test | 13 +++++++++++++ 2 files changed, 25 insertions(+)
diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 6c618912..c95f93e9 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -2957,6 +2957,17 @@ static bool intrinsic_round(struct hlsl_ctx *ctx, return !!add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_ROUND, arg, loc); }
+static bool intrinsic_rsqrt(struct hlsl_ctx *ctx, + const struct parse_initializer *params, const struct vkd3d_shader_location *loc) +{ + struct hlsl_ir_node *arg; + + if (!(arg = intrinsic_float_convert_arg(ctx, params, params->args[0], loc))) + return false; + + return !!add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_RSQ, arg, loc); +} + static bool intrinsic_saturate(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) { @@ -3224,6 +3235,7 @@ intrinsic_functions[] = {"pow", 2, true, intrinsic_pow}, {"reflect", 2, true, intrinsic_reflect}, {"round", 1, true, intrinsic_round}, + {"rsqrt", 1, true, intrinsic_rsqrt}, {"saturate", 1, true, intrinsic_saturate}, {"sin", 1, true, intrinsic_sin}, {"smoothstep", 3, true, intrinsic_smoothstep}, diff --git a/tests/sqrt.shader_test b/tests/sqrt.shader_test index 5d048b4f..78d89d38 100644 --- a/tests/sqrt.shader_test +++ b/tests/sqrt.shader_test @@ -10,3 +10,16 @@ float4 main() : sv_target uniform 0 float4 1.0 9.0 32.3 46.5 draw quad probe all rgba (1.0, 3.0, 5.683309, 6.819091) 1 + +[pixel shader] +uniform float4 f; + +float4 main() : sv_target +{ + return rsqrt(f); +} + +[test] +uniform 0 float4 1.0 9.0 4.0 16.0 +draw quad +probe all rgba (1.0, 0.33333333, 0.5, 0.25) 1
From: Nikolay Sivov nsivov@codeweavers.com
--- Makefile.am | 1 + libs/vkd3d-shader/hlsl.y | 24 ++++++++++++++++++++++++ tests/distance.shader_test | 23 +++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 tests/distance.shader_test
diff --git a/Makefile.am b/Makefile.am index ebda192d..9f812cef 100644 --- a/Makefile.am +++ b/Makefile.am @@ -61,6 +61,7 @@ vkd3d_shader_tests = \ tests/cbuffer.shader_test \ tests/compute.shader_test \ tests/conditional.shader_test \ + tests/distance.shader_test \ tests/entry-point-semantics.shader_test \ tests/exp.shader_test \ tests/floor.shader_test \ diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index c95f93e9..9ed0a415 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -2549,6 +2549,29 @@ static bool intrinsic_cross(struct hlsl_ctx *ctx, return !!add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_ADD, mul2, mul1_neg, loc); }
+static bool intrinsic_distance(struct hlsl_ctx *ctx, + const struct parse_initializer *params, const struct vkd3d_shader_location *loc) +{ + struct hlsl_ir_node *arg1, *arg2, *neg, *add, *dot; + + if (!(arg1 = intrinsic_float_convert_arg(ctx, params, params->args[0], loc))) + return false; + + if (!(arg2 = intrinsic_float_convert_arg(ctx, params, params->args[1], loc))) + return false; + + if (!(neg = add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_NEG, arg2, loc))) + return false; + + if (!(add = add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_ADD, arg1, neg, loc))) + return false; + + if (!(dot = add_binary_dot_expr(ctx, params->instrs, add, add, loc))) + return false; + + return !!add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_SQRT, dot, loc); +} + static bool intrinsic_dot(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) { @@ -3219,6 +3242,7 @@ intrinsic_functions[] = {"clamp", 3, true, intrinsic_clamp}, {"cos", 1, true, intrinsic_cos}, {"cross", 2, true, intrinsic_cross}, + {"distance", 2, true, intrinsic_distance}, {"dot", 2, true, intrinsic_dot}, {"exp", 1, true, intrinsic_exp}, {"exp2", 1, true, intrinsic_exp2}, diff --git a/tests/distance.shader_test b/tests/distance.shader_test new file mode 100644 index 00000000..3f544645 --- /dev/null +++ b/tests/distance.shader_test @@ -0,0 +1,23 @@ +[pixel shader] +uniform float4 x; +uniform float4 y; + +float4 main() : sv_target +{ + return distance(x, y); +} + +[test] +uniform 0 float4 -2.0 3.0 4.0 0.1 +uniform 4 float4 2.0 -1.0 4.0 5.0 +draw quad +probe all rgba (7.483983, 7.483983, 7.483983, 7.483983) 1 + +[pixel shader] +uniform int4 x; +uniform int4 y; + +float4 main() : sv_target +{ + return distance(x, y); +}
On Thu Feb 23 20:27:55 2023 +0000, Francisco Casas wrote:
Currently, `distance()` fails when passing `int` parameters:
int a, b; float4 main() : sv_target { return distance(a, b); }
vkd3d/libs/vkd3d-shader/hlsl_sm4.c:1800: write_sm4_expr: Assertion `type_is_float(dst_type)' failed. Aborted (core dumped)
it is worth noting that `add_binary_arithmetic_expr()` results in a value of the common type (which is `int` if both arguments are `int`).
Thanks, it should do explicit conversion apparently. I pushed just that. Tests results unfortunately differ again, between d3d9 and d3d10+.
This merge request was approved by Francisco Casas.