From 746222b349e9c009ed270fb5ca400497dfb43709 Mon Sep 17 00:00:00 2001 From: Petrichor Park Date: Fri, 21 Jun 2024 10:19:17 -0500 Subject: [PATCH] vkd3d-shader/hlsl: Implement the faceforward() intrinsic. --- Makefile.am | 1 + libs/vkd3d-shader/hlsl.y | 29 +++++++++++++++++ tests/hlsl/faceforward.shader_test | 52 ++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 tests/hlsl/faceforward.shader_test diff --git a/Makefile.am b/Makefile.am index 73b2bd7c..1d418845 100644 --- a/Makefile.am +++ b/Makefile.am @@ -108,6 +108,7 @@ vkd3d_shader_tests = \ tests/hlsl/entry-point-semantics.shader_test \ tests/hlsl/exp.shader_test \ tests/hlsl/expr-indexing.shader_test \ + tests/hlsl/faceforward.shader_test \ tests/hlsl/float-comparison.shader_test \ tests/hlsl/floor.shader_test \ tests/hlsl/fmod.shader_test \ diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 80e73e38..ed6b41bf 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -3614,6 +3614,34 @@ static bool intrinsic_exp2(struct hlsl_ctx *ctx, return !!add_unary_arithmetic_expr(ctx, params->instrs, HLSL_OP1_EXP2, arg, loc); } +static bool intrinsic_faceforward(struct hlsl_ctx *ctx, + const struct parse_initializer *params, const struct vkd3d_shader_location *loc) +{ + struct hlsl_ir_function_decl *func; + struct hlsl_type *type; + char *body; + + static const char template[] = + "%s faceforward(%s n, %s i, %s ng)\n" + "{\n" + " return dot(i, ng) < 0 ? n : -n;\n" + "}\n"; + + if (!(type = elementwise_intrinsic_get_common_type(ctx, params, loc))) + return false; + type = hlsl_get_numeric_type(ctx, type->class, HLSL_TYPE_FLOAT, type->dimx, type->dimy); + + if (!(body = hlsl_sprintf_alloc(ctx, template, + type->name, type->name, type->name, type->name))) + return false; + func = hlsl_compile_internal_function(ctx, "faceforward", body); + vkd3d_free(body); + if (!func) + return false; + + return add_user_call(ctx, func, params, loc); +} + static bool intrinsic_floor(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) { @@ -4687,6 +4715,7 @@ intrinsic_functions[] = {"dot", 2, true, intrinsic_dot}, {"exp", 1, true, intrinsic_exp}, {"exp2", 1, true, intrinsic_exp2}, + {"faceforward", 3, true, intrinsic_faceforward}, {"floor", 1, true, intrinsic_floor}, {"fmod", 2, true, intrinsic_fmod}, {"frac", 1, true, intrinsic_frac}, diff --git a/tests/hlsl/faceforward.shader_test b/tests/hlsl/faceforward.shader_test new file mode 100644 index 00000000..a7835855 --- /dev/null +++ b/tests/hlsl/faceforward.shader_test @@ -0,0 +1,52 @@ +[pixel shader] +uniform float4 n; +uniform float4 i; +uniform float4 ng; + +float4 main() : sv_target +{ + return faceforward(n, i, ng); +} + +[test] +uniform 0 float4 10.0 20.0 30.0 40.0 +uniform 4 float4 1.0 0.0 0.0 0.0 +uniform 8 float4 1.0 0.2 0.0 0.0 +todo(glsl) draw quad +probe (0, 0) rgba (-10.0, -20.0, -30.0, -40.0) + +uniform 0 float4 10.0 20.0 30.0 40.0 +uniform 4 float4 1.0 0.0 0.0 0.0 +uniform 8 float4 1.0 -0.2 0.0 0.0 +todo(glsl) draw quad +probe (0, 0) rgba (-10.0, -20.0, -30.0, -40.0) + +uniform 0 float4 10.0 20.0 30.0 40.0 +uniform 4 float4 1.0 0.0 0.0 0.0 +uniform 8 float4 -1.0 0.0 0.0 0.0 +todo(glsl) draw quad +probe (0, 0) rgba (10.0, 20.0, 30.0, 40.0) + +uniform 0 float4 10.0 20.0 30.0 40.0 +uniform 4 float4 0.0 -1.0 0.0 0.0 +uniform 8 float4 0.0 1.0 0.0 0.0 +todo(glsl) draw quad +probe (0, 0) rgba (10.0, 20.0, 30.0, 40.0) + +uniform 0 float4 10.0 20.0 30.0 40.0 +uniform 4 float4 1.0 0.0 0.0 0.0 +uniform 8 float4 0.0 1.0 0.0 0.0 +todo(glsl) draw quad +probe (0, 0) rgba (-10.0, -20.0, -30.0, -40.0) + +uniform 0 float4 10.0 20.0 30.0 40.0 +uniform 4 float4 0.0 0.0 0.0 0.0 +uniform 8 float4 0.0 1.0 0.0 0.0 +todo(glsl) draw quad +probe (0, 0) rgba (-10.0, -20.0, -30.0, -40.0) + +uniform 0 float4 10.0 20.0 30.0 40.0 +uniform 4 float4 0.0 0.0 0.0 0.0 +uniform 8 float4 0.0 0.0 0.0 0.0 +todo(glsl) draw quad +probe (0, 0) rgba (-10.0, -20.0, -30.0, -40.0)