From 3dad54845eab21453441b4e16f567e36677825fe Mon Sep 17 00:00:00 2001 From: Francisco Casas Date: Fri, 19 Nov 2021 15:38:41 +0100 Subject: [PATCH] vkd3d-shader/hlsl: Implement round() intrinsic function in SM4. Signed-off-by: Francisco Casas Signed-off-by: Zebediah Figura Signed-off-by: Matteo Bruni Signed-off-by: Giovanni Mascellani Signed-off-by: Henri Verbeet Signed-off-by: Alexandre Julliard --- Makefile.am | 1 + libs/vkd3d-shader/hlsl.c | 1 + libs/vkd3d-shader/hlsl.h | 1 + libs/vkd3d-shader/hlsl.y | 8 ++++++++ libs/vkd3d-shader/hlsl_sm4.c | 4 ++++ tests/round.shader_test | 26 ++++++++++++++++++++++++++ 6 files changed, 41 insertions(+) create mode 100644 tests/round.shader_test diff --git a/Makefile.am b/Makefile.am index 3e083b0a..6d392e1d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -91,6 +91,7 @@ vkd3d_shader_tests = \ tests/preproc-invalid.shader_test \ tests/preproc-macro.shader_test \ tests/preproc-misc.shader_test \ + tests/round.shader_test \ tests/sampler.shader_test \ tests/saturate.shader_test \ tests/swizzle-0.shader_test \ diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index 1eee4278..9824c56d 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -1156,6 +1156,7 @@ const char *debug_hlsl_expr_op(enum hlsl_ir_expr_op op) [HLSL_OP1_NEG] = "-", [HLSL_OP1_NRM] = "nrm", [HLSL_OP1_RCP] = "rcp", + [HLSL_OP1_ROUND] = "round", [HLSL_OP1_RSQ] = "rsq", [HLSL_OP1_SAT] = "sat", [HLSL_OP1_SIGN] = "sign", diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index 365ef980..e7bdb45e 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -299,6 +299,7 @@ enum hlsl_ir_expr_op HLSL_OP1_NEG, HLSL_OP1_NRM, HLSL_OP1_RCP, + HLSL_OP1_ROUND, HLSL_OP1_RSQ, HLSL_OP1_SAT, HLSL_OP1_SIGN, diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index b7e0409d..abf5da91 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -1611,6 +1611,12 @@ static bool intrinsic_pow(struct hlsl_ctx *ctx, return true; } +static bool intrinsic_round(struct hlsl_ctx *ctx, + const struct parse_initializer *params, struct vkd3d_shader_location loc) +{ + return !!add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_ROUND, params->args[0], &loc); +} + static bool intrinsic_saturate(struct hlsl_ctx *ctx, const struct parse_initializer *params, struct vkd3d_shader_location loc) { @@ -1626,10 +1632,12 @@ static const struct intrinsic_function } intrinsic_functions[] = { + /* Note: these entries should be kept in alphabetical order. */ {"abs", 1, true, intrinsic_abs}, {"clamp", 3, true, intrinsic_clamp}, {"max", 2, true, intrinsic_max}, {"pow", 2, true, intrinsic_pow}, + {"round", 1, true, intrinsic_round}, {"saturate", 1, true, intrinsic_saturate}, }; diff --git a/libs/vkd3d-shader/hlsl_sm4.c b/libs/vkd3d-shader/hlsl_sm4.c index a95503ee..c0c26f80 100644 --- a/libs/vkd3d-shader/hlsl_sm4.c +++ b/libs/vkd3d-shader/hlsl_sm4.c @@ -1305,6 +1305,10 @@ static void write_sm4_expr(struct hlsl_ctx *ctx, write_sm4_unary_op(buffer, VKD3D_SM4_OP_MOV, &expr->node, arg1, VKD3D_SM4_REGISTER_MODIFIER_NEGATE); break; + case HLSL_OP1_ROUND: + write_sm4_unary_op(buffer, VKD3D_SM4_OP_ROUND_NE, &expr->node, arg1, 0); + break; + case HLSL_OP1_SAT: write_sm4_unary_op(buffer, VKD3D_SM4_OP_MOV | (VKD3D_SM4_INSTRUCTION_FLAG_SATURATE << VKD3D_SM4_INSTRUCTION_FLAGS_SHIFT), diff --git a/tests/round.shader_test b/tests/round.shader_test new file mode 100644 index 00000000..d8eb11b7 --- /dev/null +++ b/tests/round.shader_test @@ -0,0 +1,26 @@ +[pixel shader] +float4 main(uniform float4 u) : sv_target +{ + return round(u); +} + +[test] +uniform 0 float4 -0.5 6.5 7.5 3.4 +draw quad +probe all rgba (0.0, 6.0, 8.0, 3.0) 4 + + + +[pixel shader] +float4 main(uniform float4 u) : sv_target +{ + float a = round(u.r); + int2 b = round(u.gb); + float4 res = float4(b, a, u.a); + return round(res); +} + +[test] +uniform 0 float4 -0.5 6.5 7.5 3.4 +draw quad +probe all rgba (6.0, 8.0, 0.0, 3.0) 4