From: Nikolay Sivov nsivov@codeweavers.com
Signed-off-by: Nikolay Sivov nsivov@codeweavers.com --- Makefile.am | 2 + libs/vkd3d-shader/hlsl.y | 48 +++++++++++++ tests/hlsl/refract.shader_test | 125 +++++++++++++++++++++++++++++++++ 3 files changed, 175 insertions(+) create mode 100644 tests/hlsl/refract.shader_test
diff --git a/Makefile.am b/Makefile.am index 06fd43ab0..ba47ba0d7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -157,8 +157,10 @@ vkd3d_shader_tests = \ tests/hlsl/pow.shader_test \ tests/hlsl/rasteriser-ordered-views.shader_test \ tests/hlsl/reflect.shader_test \ + tests/hlsl/refract.shader_test \ tests/hlsl/register-reservations-numeric.shader_test \ tests/hlsl/register-reservations-resources.shader_test \ + tests/hlsl/register-reservations.shader_test \ tests/hlsl/return-implicit-conversion.shader_test \ tests/hlsl/return.shader_test \ tests/hlsl/round.shader_test \ diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index b484a9524..28683f8bf 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -3734,6 +3734,53 @@ static bool intrinsic_reflect(struct hlsl_ctx *ctx, return !!add_binary_arithmetic_expr(ctx, params->instrs, HLSL_OP2_ADD, i, neg, loc); }
+static bool intrinsic_refract(struct hlsl_ctx *ctx, + const struct parse_initializer *params, const struct vkd3d_shader_location *loc) +{ + struct hlsl_type *r_type = params->args[0]->data_type; + struct hlsl_type *n_type = params->args[1]->data_type; + struct hlsl_ir_function_decl *func; + struct hlsl_type *type; + unsigned int dim; + char *body; + + static const char template[] = + "%s refract(%s r, %s n, float i)\n" + "{\n" + " float d = dot(r, n);\n" + " float t = 1 - i * i * (1 - d * d);\n" + " return t >= 0.0 ? i * r - (i * d + sqrt(t)) * n : 0;\n" + "}"; + + if (r_type->class == HLSL_CLASS_MATRIX + || n_type->class == HLSL_CLASS_MATRIX) + { + hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE, "Matrix arguments are not supported."); + return false; + } + + if (r_type->class == HLSL_CLASS_VECTOR && n_type->class == HLSL_CLASS_VECTOR) + { + dim = min(hlsl_type_component_count(r_type), hlsl_type_component_count(n_type)); + } + else + { + dim = max(hlsl_type_component_count(r_type), hlsl_type_component_count(n_type)); + } + + type = hlsl_get_vector_type(ctx, HLSL_TYPE_FLOAT, dim); + + if (!(body = hlsl_sprintf_alloc(ctx, template, type->name, type->name, type->name))) + return false; + + func = hlsl_compile_internal_function(ctx, "refract", body); + vkd3d_free(body); + if (!func) + return false; + + return add_user_call(ctx, func, params, loc); +} + static bool intrinsic_round(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) { @@ -4249,6 +4296,7 @@ intrinsic_functions[] = {"pow", 2, true, intrinsic_pow}, {"radians", 1, true, intrinsic_radians}, {"reflect", 2, true, intrinsic_reflect}, + {"refract", 3, true, intrinsic_refract}, {"round", 1, true, intrinsic_round}, {"rsqrt", 1, true, intrinsic_rsqrt}, {"saturate", 1, true, intrinsic_saturate}, diff --git a/tests/hlsl/refract.shader_test b/tests/hlsl/refract.shader_test new file mode 100644 index 000000000..cb5784d6b --- /dev/null +++ b/tests/hlsl/refract.shader_test @@ -0,0 +1,125 @@ +[pixel shader] +float4 r; +float4 n; +float i; + +float4 main() : sv_target +{ + return refract(r, n, i); +} + +[test] +uniform 0 float4 0.5 -0.1 0.2 0.3 +uniform 4 float4 0.6 0.4 -0.3 1.0 +uniform 8 float 0.2 +todo(sm>=6) draw quad +probe all rgba (-0.550931, -0.453954, 0.3654653, -1.0248856) 32 +uniform 8 float 100.0 +todo(sm>=6) draw quad +probe all rgba (0.0, 0.0, 0.0, 0.0) + +[pixel shader] +float4 r; +float4 n; +float i; + +float4 main() : sv_target +{ + float2 _r = float2(r.x, r.y); + float2 _n = float2(n.x, n.y); + return float4(refract(_r, _n, i), 0, 0); +} + +[test] +uniform 0 float4 0.5 -0.1 0.2 0.3 +uniform 4 float4 0.6 0.4 -0.3 1.0 +uniform 8 float 0.2 +todo(sm>=6) draw quad +probe all rgba (-0.519904912, -0.4332699, 0.0, 0.0) 32 +uniform 8 float 100.0 +todo(sm>=6) draw quad +probe all rgba (0.0, 0.0, 0.0, 0.0) + +[pixel shader] +float4 r; +float4 n; +float i; + +float4 main() : sv_target +{ + float2 _r = float2(r.x, r.y); + return float4(refract(_r, n, i), 0, 0); +} + +[test] +uniform 0 float4 0.5 -0.1 0.2 0.3 +uniform 4 float4 0.6 0.4 -0.3 1.0 +uniform 8 float 0.2 +todo(sm>=6) draw quad +probe all rgba (-0.519904912, -0.4332699, 0.0, 0.0) 32 +uniform 8 float 100.0 +todo(sm>=6) draw quad +probe all rgba (0.0, 0.0, 0.0, 0.0) + +[pixel shader] +float4 r; +float4 n; +float i; + +float4 main() : sv_target +{ + float2 _n = float2(n.x, n.y); + return float4(refract(r, _n, i), 0, 0); +} + +[test] +uniform 0 float4 0.5 -0.1 0.2 0.3 +uniform 4 float4 0.6 0.4 -0.3 1.0 +uniform 8 float 0.2 +todo(sm>=6) draw quad +probe all rgba (-0.519904912, -0.4332699, 0.0, 0.0) 32 +uniform 8 float 100.0 +todo(sm>=6) draw quad +probe all rgba (0.0, 0.0, 0.0, 0.0) + +[pixel shader] +float4 r; +float4 n; +float i; + +float4 main() : sv_target +{ + float1 _r = float1(r.x); + float1 _n = float1(n.x); + return float4(refract(_r, _n, i), 0, 0, 0); +} + +[test] +uniform 0 float4 0.5 -0.1 0.2 0.3 +uniform 4 float4 0.6 0.4 -0.3 1.0 +uniform 8 float 0.2 +todo(sm>=6) draw quad +probe all rgba (-0.524978, 0.0, 0.0, 0.0) 32 +uniform 8 float 100.0 +todo(sm>=6) draw quad +probe all rgba (0.0, 0.0, 0.0, 0.0) + +[pixel shader] +float4 r; +float4 n; +float i; + +float4 main() : sv_target +{ + return refract(r, n.x, i); +} + +[test] +uniform 0 float4 0.5 -0.1 0.2 0.3 +uniform 4 float4 0.6 0.4 -0.3 1.0 +uniform 8 float 0.2 +todo(sm>=6) draw quad +probe all rgba (-0.5562381, -0.6762381, -0.6162381, -0.5962381) 32 +uniform 8 float 100.0 +todo(sm>=6) draw quad +probe all rgba (0.0, 0.0, 0.0, 0.0)