From ccbe36fb8d01cbbb8b1b88a6d9f89eb45a616db6 Mon Sep 17 00:00:00 2001
From: Nikolay Sivov <nsivov@codeweavers.com>
Date: Tue, 21 May 2024 18:18:11 +0200
Subject: [PATCH] vkd3d-shader/hlsl: Implement tex2Dbias().

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=56701
Signed-off-by: Nikolay Sivov <nsivov@codeweavers.com>
---
 libs/vkd3d-shader/hlsl.y           | 15 +++++++++++++--
 tests/hlsl/sample-bias.shader_test | 28 ++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y
index 4f9d2dff68..052573a23b 100644
--- a/libs/vkd3d-shader/hlsl.y
+++ b/libs/vkd3d-shader/hlsl.y
@@ -4168,11 +4168,15 @@ static bool intrinsic_tex(struct hlsl_ctx *ctx, const struct parse_initializer *
         hlsl_release_string_buffer(ctx, string);
     }
 
-    if (!strcmp(name, "tex2Dlod"))
+    if (!strcmp(name, "tex2Dbias")
+            || !strcmp(name, "tex2Dlod"))
     {
         struct hlsl_ir_node *lod, *c;
 
-        load_params.type = HLSL_RESOURCE_SAMPLE_LOD;
+        if (!strcmp(name, "tex2Dlod"))
+            load_params.type = HLSL_RESOURCE_SAMPLE_LOD;
+        else
+            load_params.type = HLSL_RESOURCE_SAMPLE_LOD_BIAS;
 
         if (!(c = hlsl_new_swizzle(ctx, HLSL_SWIZZLE(X, Y, Z, W), hlsl_sampler_dim_count(dim), params->args[1], loc)))
             return false;
@@ -4294,6 +4298,12 @@ static bool intrinsic_tex2D(struct hlsl_ctx *ctx,
     return intrinsic_tex(ctx, params, loc, "tex2D", HLSL_SAMPLER_DIM_2D);
 }
 
+static bool intrinsic_tex2Dbias(struct hlsl_ctx *ctx,
+        const struct parse_initializer *params, const struct vkd3d_shader_location *loc)
+{
+    return intrinsic_tex(ctx, params, loc, "tex2Dbias", HLSL_SAMPLER_DIM_2D);
+}
+
 static bool intrinsic_tex2Dlod(struct hlsl_ctx *ctx,
         const struct parse_initializer *params, const struct vkd3d_shader_location *loc)
 {
@@ -4514,6 +4524,7 @@ intrinsic_functions[] =
     {"tanh",                                1, true,  intrinsic_tanh},
     {"tex1D",                              -1, false, intrinsic_tex1D},
     {"tex2D",                              -1, false, intrinsic_tex2D},
+    {"tex2Dbias",                           2, false, intrinsic_tex2Dbias},
     {"tex2Dlod",                            2, false, intrinsic_tex2Dlod},
     {"tex2Dproj",                           2, false, intrinsic_tex2Dproj},
     {"tex3D",                              -1, false, intrinsic_tex3D},
diff --git a/tests/hlsl/sample-bias.shader_test b/tests/hlsl/sample-bias.shader_test
index 16761452ce..8385a85b89 100644
--- a/tests/hlsl/sample-bias.shader_test
+++ b/tests/hlsl/sample-bias.shader_test
@@ -42,3 +42,31 @@ probe all rgba (4.0, 0.0, 10.0, 0.0)
 uniform 0 float4 8.5 0.0 0.0 0.0
 todo(sm<4 | glsl) draw quad
 probe all rgba (0.0, 0.0, 10.0, 0.0)
+
+[require]
+shader model >= 3.0
+options: backcompat
+
+[pixel shader todo(sm<4) fail(sm>=6)]
+sampler s;
+float bias;
+
+float4 main(float2 coord : texcoord) : sv_target
+{
+    /* Choice of initial mipmap level is hardware-dependent, and in practice
+     * varies too much to be reasonably covered by ULPS. Quantize instead. */
+    return floor(tex2Dbias(s, float4(coord, 0, bias)) * 10);
+}
+
+[test]
+uniform 0 float4 6.5 0.0 0.0 0.0
+todo(sm<4 | glsl) draw quad
+probe all rgba (10.0, 0.0, 10.0, 0.0)
+
+uniform 0 float4 7.5 0.0 0.0 0.0
+todo(sm<4 | glsl) draw quad
+probe all rgba (4.0, 0.0, 10.0, 0.0)
+
+uniform 0 float4 8.5 0.0 0.0 0.0
+todo(sm<4 | glsl) draw quad
+probe all rgba (0.0, 0.0, 10.0, 0.0)
-- 
GitLab