Based on !333.
-- v2: vkd3d-shader/hlsl: Support refract() intrinsic. vkd3d-shader/hlsl: Test refract() intrinsic. vkd3d-shader/hlsl: Emit half results when operating on half arguments.
From: Giovanni Mascellani gmascellani@codeweavers.com
--- libs/vkd3d-shader/hlsl.y | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 3959ff84f..25805810f 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -2648,12 +2648,14 @@ static bool elementwise_intrinsic_convert_args(struct hlsl_ctx *ctx, static bool elementwise_intrinsic_float_convert_args(struct hlsl_ctx *ctx, const struct parse_initializer *params, const struct vkd3d_shader_location *loc) { + enum hlsl_base_type base_type; struct hlsl_type *type;
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); + base_type = type->base_type == HLSL_TYPE_HALF ? HLSL_TYPE_HALF : HLSL_TYPE_FLOAT; + type = hlsl_get_numeric_type(ctx, type->class, base_type, type->dimx, type->dimy);
return convert_args(ctx, params, type, loc); }
From: Nikolay Sivov nsivov@codeweavers.com
With some changes by Giovanni Mascellani. --- Makefile.am | 1 + tests/hlsl/refract.shader_test | 233 +++++++++++++++++++++++++++++++++ 2 files changed, 234 insertions(+) create mode 100644 tests/hlsl/refract.shader_test
diff --git a/Makefile.am b/Makefile.am index 39b041502..51d4192bd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -162,6 +162,7 @@ vkd3d_shader_tests = \ tests/hlsl/precise-modifier.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/return-implicit-conversion.shader_test \ diff --git a/tests/hlsl/refract.shader_test b/tests/hlsl/refract.shader_test new file mode 100644 index 000000000..ecd554b04 --- /dev/null +++ b/tests/hlsl/refract.shader_test @@ -0,0 +1,233 @@ +[pixel shader todo(sm<6)] +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 | 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 +probe all rgba (0.0, 0.0, 0.0, 0.0) + +[pixel shader todo(sm<6)] +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 | 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 +probe all rgba (0.0, 0.0, 0.0, 0.0) + +[pixel shader todo(sm<6)] +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 | 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 +probe all rgba (0.0, 0.0, 0.0, 0.0) + +[pixel shader todo(sm<6)] +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 | 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 +probe all rgba (0.0, 0.0, 0.0, 0.0) + +[pixel shader todo(sm<6)] +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 | 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 +probe all rgba (0.0, 0.0, 0.0, 0.0) + +[pixel shader todo(sm<6)] +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 | 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 +probe all rgba (0.0, 0.0, 0.0, 0.0) + +[pixel shader fail(sm>=6) todo(sm<6)] +float4 r; +float4 n; +float4 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 float4 0.2 0.3 0.4 0.5 +if(sm<6) todo(sm<6 | 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) probe all rgba (0.0, 0.0, 0.0, 0.0) + +[pixel shader fail] +float4x1 r; +float4 n; +float i; + +float4 main() : sv_target +{ + return refract(r, n, i); +} + +[pixel shader fail] +float1x4 r; +float4 n; +float i; + +float4 main() : sv_target +{ + return refract(r, n, i); +} + +[pixel shader fail] +float4 r; +float1x4 n; +float i; + +float4 main() : sv_target +{ + return refract(r, n, i); +} + +[pixel shader fail] +float4 r; +float4x1 n; +float i; + +float4 main() : sv_target +{ + return refract(r, n, i); +} + +[pixel shader fail] +float4 r; +float4 n; +float1x4 i; + +float4 main() : sv_target +{ + return refract(r, n, i); +} + +[pixel shader fail] +float4 r; +float4 n; +float4x1 i; + +float4 main() : sv_target +{ + return refract(r, n, i); +} + +[pixel shader todo(sm<6)] +float4 r; +float4 n; +float i; + +float detect(float4 x) +{ + return 1; +} + +float detect(half4 x) +{ + return 2; +} + +float4 main() : sv_target +{ + return float4(detect(refract((half4)r, (half4)n, (half)i)), + detect(refract(r, n, (half)i)), + detect(refract(r, (half4)n, i)), + detect(refract((half4)r, n, i))); +} + +[test] +todo(sm<6 | glsl) draw quad +if(sm<6) probe all rgba (2.0, 1.0, 1.0, 1.0) +if(sm>=6) probe all rgba (1.0, 1.0, 1.0, 1.0)
From: Nikolay Sivov nsivov@codeweavers.com
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 25805810f..72a2858eb 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -3706,6 +3706,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) { @@ -4221,6 +4274,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 ecd554b04..23b61e995 100644 --- a/tests/hlsl/refract.shader_test +++ b/tests/hlsl/refract.shader_test @@ -1,4 +1,4 @@ -[pixel shader todo(sm<6)] +[pixel shader todo(sm<4)] 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(sm<4 | 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(sm<4 | glsl) draw quad probe all rgba (0.0, 0.0, 0.0, 0.0)
-[pixel shader todo(sm<6)] +[pixel shader todo(sm<4)] 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 todo(sm<4)] 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 todo(sm<4)] 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 todo(sm<4)] 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(sm<4 | 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(sm<4 | glsl) draw quad probe all rgba (0.0, 0.0, 0.0, 0.0)
-[pixel shader todo(sm<6)] +[pixel shader todo(sm<4)] 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(sm<4 | 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(sm<4 | 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) todo(sm<4)] 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(sm<4 | 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(sm<4 | glsl) draw quad if(sm<6) probe all rgba (0.0, 0.0, 0.0, 0.0)
[pixel shader fail]