From 8d78e3a821f3770a359ee29e806b80cfd6e4eb15 Mon Sep 17 00:00:00 2001 From: Nikolay Sivov Date: Thu, 7 Sep 2023 14:16:15 +0200 Subject: [PATCH] vkd3d-shader/hlsl: Support refract() intrinsic. With some changes by Giovanni Mascellani. --- libs/vkd3d-shader/hlsl.y | 54 ++++++++++++++++++++++++++++++++++ tests/hlsl/refract.shader_test | 42 +++++++++++++------------- 2 files changed, 75 insertions(+), 21 deletions(-) diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 03e14d84..7fc35d4e 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -3740,6 +3740,59 @@ 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_type *i_type = params->args[2]->data_type; + struct hlsl_type *res_type, *idx_type, *scal_type; + struct parse_initializer mut_params; + struct hlsl_ir_function_decl *func; + enum hlsl_base_type base; + char *body; + + static const char template[] = + "%s refract(%s r, %s n, %s i)\n" + "{\n" + " %s d, t;\n" + " d = dot(r, n);\n" + " t = 1 - i.x * i.x * (1 - d * d);\n" + " return t >= 0.0 ? i.x * r - (i.x * d + sqrt(t)) * n : 0;\n" + "}"; + + if (r_type->class == HLSL_CLASS_MATRIX + || n_type->class == HLSL_CLASS_MATRIX + || i_type->class == HLSL_CLASS_MATRIX) + { + hlsl_error(ctx, loc, VKD3D_SHADER_ERROR_HLSL_INVALID_TYPE, "Matrix arguments are not supported."); + return false; + } + + assert(params->args_count == 3); + mut_params = *params; + mut_params.args_count = 2; + if (!(res_type = elementwise_intrinsic_get_common_type(ctx, &mut_params, loc))) + return false; + + base = expr_common_base_type(res_type->base_type, i_type->base_type); + base = base == HLSL_TYPE_HALF ? HLSL_TYPE_HALF : HLSL_TYPE_FLOAT; + res_type = convert_numeric_type(ctx, res_type, base); + idx_type = convert_numeric_type(ctx, i_type, base); + scal_type = hlsl_get_scalar_type(ctx, base); + + if (!(body = hlsl_sprintf_alloc(ctx, template, res_type->name, res_type->name, + res_type->name, idx_type->name, scal_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) { @@ -4255,6 +4308,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 index ecd554b0..53af811f 100644 --- a/tests/hlsl/refract.shader_test +++ b/tests/hlsl/refract.shader_test @@ -1,4 +1,4 @@ -[pixel shader todo(sm<6)] +[pixel shader] float4 r; float4 n; float i; @@ -12,13 +12,13 @@ float4 main() : sv_target 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 | glsl) draw quad +todo(glsl) draw quad probe all rgba (-0.550931, -0.453954, 0.3654653, -1.0248856) 32 uniform 8 float 100.0 -todo(sm<6 | glsl) draw quad +todo(glsl) draw quad probe all rgba (0.0, 0.0, 0.0, 0.0) -[pixel shader todo(sm<6)] +[pixel shader] float4 r; float4 n; float i; @@ -34,13 +34,13 @@ float4 main() : sv_target 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 | glsl) draw quad +todo(sm<4 | glsl) draw quad probe all rgba (-0.519904912, -0.4332699, 0.0, 0.0) 32 uniform 8 float 100.0 -todo(sm<6 | glsl) draw quad +todo(sm<4 | glsl) draw quad probe all rgba (0.0, 0.0, 0.0, 0.0) -[pixel shader todo(sm<6)] +[pixel shader] float4 r; float4 n; float i; @@ -55,13 +55,13 @@ float4 main() : sv_target 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 | glsl) draw quad +todo(sm<4 | glsl) draw quad probe all rgba (-0.519904912, -0.4332699, 0.0, 0.0) 32 uniform 8 float 100.0 -todo(sm<6 | glsl) draw quad +todo(sm<4 | glsl) draw quad probe all rgba (0.0, 0.0, 0.0, 0.0) -[pixel shader todo(sm<6)] +[pixel shader] float4 r; float4 n; float i; @@ -76,13 +76,13 @@ float4 main() : sv_target 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 | glsl) draw quad +todo(sm<4 | glsl) draw quad probe all rgba (-0.519904912, -0.4332699, 0.0, 0.0) 32 uniform 8 float 100.0 -todo(sm<6 | glsl) draw quad +todo(sm<4 | glsl) draw quad probe all rgba (0.0, 0.0, 0.0, 0.0) -[pixel shader todo(sm<6)] +[pixel shader] float4 r; float4 n; float i; @@ -98,13 +98,13 @@ float4 main() : sv_target 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 | glsl) draw quad +todo(glsl) draw quad probe all rgba (-0.524978, 0.0, 0.0, 0.0) 32 uniform 8 float 100.0 -todo(sm<6 | glsl) draw quad +todo(glsl) draw quad probe all rgba (0.0, 0.0, 0.0, 0.0) -[pixel shader todo(sm<6)] +[pixel shader] float4 r; float4 n; float i; @@ -118,13 +118,13 @@ float4 main() : sv_target 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 | glsl) draw quad +todo(glsl) draw quad probe all rgba (-0.5562381, -0.6762381, -0.6162381, -0.5962381) 32 uniform 8 float 100.0 -todo(sm<6 | glsl) draw quad +todo(glsl) draw quad probe all rgba (0.0, 0.0, 0.0, 0.0) -[pixel shader fail(sm>=6) todo(sm<6)] +[pixel shader fail(sm>=6)] float4 r; float4 n; float4 i; @@ -138,10 +138,10 @@ float4 main() : sv_target uniform 0 float4 0.5 -0.1 0.2 0.3 uniform 4 float4 0.6 0.4 -0.3 1.0 uniform 8 float4 0.2 0.3 0.4 0.5 -if(sm<6) todo(sm<6 | glsl) draw quad +if(sm<6) todo(glsl) draw quad if(sm<6) probe all rgba (-0.550931, -0.453954, 0.3654653, -1.0248856) 32 uniform 8 float 100.0 -if(sm<6) todo(sm<6 | glsl) draw quad +if(sm<6) todo(glsl) draw quad if(sm<6) probe all rgba (0.0, 0.0, 0.0, 0.0) [pixel shader fail]