wined3d: Correctly display fog for right-handed projection matrix (try 3)

Joachim Priesner joachim.priesner at web.de
Tue Sep 30 04:33:21 CDT 2014


When using a right-handed projection matrix, z coordinates of vertices may be negative. This is not considered in the code generating the GLSL fog shader for fixed-function fog. The patch fixes that and adds a test.

Tested on openSUSE 13.1 and Windows 8.1.

Try 3 with tests using pixel shaders and tests for d3d8. I have not included tests using vertex shaders since the bug is in Wine's FFP implementation, so using vertex shaders would not touch the relevant code path. Also I have not included ddraw tests since I could not even get the unchanged tests to run on either Linux or Windows and I feel I have spent *way* too much time on this one-line fix already, sorry :-)

From cea85d30545386e6afb4c90042492020b0692810 Mon Sep 17 00:00:00 2001
From: Joachim Priesner <joachim.priesner at web.de>
Date: Tue, 30 Sep 2014 10:46:51 +0200
Subject: wined3d: Correctly display fog for right-handed projection matrix

---
 dlls/d3d8/tests/visual.c   | 154 +++++++++++++++++++++++++++++++++++++
 dlls/d3d9/tests/visual.c   | 184 +++++++++++++++++++++++++++++++++++++++++++++
 dlls/wined3d/glsl_shader.c |   2 +-
 3 files changed, 339 insertions(+), 1 deletion(-)

diff --git a/dlls/d3d8/tests/visual.c b/dlls/d3d8/tests/visual.c
index ba73bb6..bea5a12 100644
--- a/dlls/d3d8/tests/visual.c
+++ b/dlls/d3d8/tests/visual.c
@@ -696,6 +696,159 @@ done:
     DestroyWindow(window);
 }
 
+/* This tests fog in combination with a right-handed projection matrix. */
+static void fog_righthanded_test(void)
+{
+    DWORD pixel_shader[2] = {0, 0};
+    IDirect3DDevice8 *device;
+    unsigned int i;
+    IDirect3D8 *d3d;
+    D3DCOLOR color;
+    ULONG refcount;
+    D3DCAPS8 caps;
+    HWND window;
+    HRESULT hr;
+    union
+    {
+        float f;
+        DWORD i;
+    } start, end;
+
+    /* Basic pixel shader */
+    static const DWORD pixel_shader_code[] =
+    {
+        0xffff0101,                                                             /* ps_1_1     */
+        0x00000001, 0x800f0000, 0x90e40000,                                     /* mov r0, v0 */
+        0x0000ffff
+    };
+    static struct
+    {
+        struct vec3 position;
+        DWORD diffuse;
+    }
+    quad[] =
+    {
+        {{-1.0f, -1.0f, 10.0f}, 0xffff0000},
+        {{-1.0f,  1.0f, 10.0f}, 0xffff0000},
+        {{ 1.0f, -1.0f, 10.0f}, 0xffff0000},
+        {{ 1.0f,  1.0f, 10.0f}, 0xffff0000},
+    };
+    static const struct test_data_t
+    {
+        int pshader;
+        D3DFOGMODE vfog;
+        D3DFOGMODE tfog;
+    }
+    test_data[] =
+    {
+        {0, D3DFOG_NONE, D3DFOG_LINEAR},
+        {1, D3DFOG_NONE, D3DFOG_LINEAR},
+        {0, D3DFOG_LINEAR, D3DFOG_NONE},
+        {1, D3DFOG_LINEAR, D3DFOG_NONE},
+    };
+    /* A right-handed perspective projection matrix designed so that vertices
+     * (x in [-1..1], y in [-1..1], z = 0.1) will be mapped to the view plane.
+     * Obtained from D3DXMatrixPerspectiveFovRH with the parameters
+     * fov=84.29deg, aspect=640/480 near=0.01, far=100 */
+    static const D3DMATRIX proj_mat_rh =
+    {{{
+        0.828732f, 0.0f,       0.0f,       0.0f,
+        0.0f,      1.104976f,  0.0f,       0.0f,
+        0.0f,      0.0f,      -1.0001f,   -1.0f,
+        0.0f,      0.0f,      -0.010001f,  0.0f
+    }}};
+    static const D3DMATRIX view_mat_rh =
+    {{{
+        1.0f, 0.0f,  0.0f, 0.0f,
+        0.0f, 1.0f,  0.0f, 0.0f,
+        0.0f, 0.0f, -0.1f, 0.0f,
+        0.0f, 0.0f,  0.0f, 1.0f
+    }}};
+
+    window = CreateWindowA("static", "d3d8_test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
+            0, 0, 640, 480, NULL, NULL, NULL, NULL);
+    d3d = Direct3DCreate8(D3D_SDK_VERSION);
+    ok(!!d3d, "Failed to create a D3D object.\n");
+    if (!(device = create_device(d3d, window, window, TRUE)))
+    {
+        skip("Failed to create a D3D device, skipping tests.\n");
+        goto done;
+    }
+
+    hr = IDirect3DDevice8_GetDeviceCaps(device, &caps);
+    ok(SUCCEEDED(hr), "Failed to get device caps, hr %#x.\n", hr);
+
+    if (caps.PixelShaderVersion >= D3DPS_VERSION(1, 1))
+    {
+        hr = IDirect3DDevice8_CreatePixelShader(device, pixel_shader_code, &pixel_shader[1]);
+        ok(SUCCEEDED(hr), "CreatePixelShader failed (%08x)\n", hr);
+    } else {
+        skip("No ps_1_1 support, skipping some fog tests.\n");
+    }
+
+    start.f = 0.1f;
+    end.f = 0.9f;
+
+    /* Setup initial states: No lighting, fog on, fog color */
+    hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
+    ok(hr == D3D_OK, "Turning off lighting failed (%08x)\n", hr);
+    hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGENABLE, TRUE);
+    ok(hr == D3D_OK, "Turning on fog calculations failed (%08x)\n", hr);
+    hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGCOLOR, 0xFF00FF00 /* A nice green */);
+    ok(hr == D3D_OK, "Setting fog color failed (%08x)\n", hr);
+
+    hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGSTART, start.i);
+    ok(hr == D3D_OK, "Setting fog start failed (%08x)\n", hr);
+    hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGEND, end.i);
+    ok(hr == D3D_OK, "Setting fog end failed (%08x)\n", hr);
+
+    hr = IDirect3DDevice8_SetTransform(device, D3DTS_PROJECTION, &proj_mat_rh);
+    ok(SUCCEEDED(hr), "Failed to set projection transform, hr %#x.\n", hr);
+    hr = IDirect3DDevice8_SetTransform(device, D3DTS_VIEW, &view_mat_rh);
+    ok(SUCCEEDED(hr), "Failed to set view transform, hr %#x.\n", hr);
+
+    for (i = 0; i < sizeof(test_data)/sizeof(test_data[0]); ++i)
+    {
+        if (test_data[i].pshader != 0 && caps.PixelShaderVersion < D3DPS_VERSION(1, 1))
+        {
+            continue;
+        }
+
+        hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
+        ok(SUCCEEDED(hr), "SetVertexShader failed (%08x)\n", hr);
+        hr = IDirect3DDevice8_SetPixelShader(device, pixel_shader[test_data[i].pshader]);
+        ok(SUCCEEDED(hr), "SetPixelShader failed (%08x)\n", hr);
+        hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGVERTEXMODE, test_data[i].vfog);
+        ok(hr == D3D_OK, "Setting fog vertex mode to %d failed (%08x)\n", test_data[i].vfog, hr);
+        hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGTABLEMODE, test_data[i].tfog);
+        ok(hr == D3D_OK, "Setting fog table mode to %d failed (%08x)\n", test_data[i].tfog, hr);
+
+        hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffff00ff, 1.0f, 0);
+        ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed (%08x)\n", hr);
+        hr = IDirect3DDevice8_BeginScene(device);
+        ok(hr == D3D_OK, "BeginScene returned failed (%08x)\n", hr);
+        hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], sizeof(quad[0]));
+        ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (%08x)\n", hr);
+        hr = IDirect3DDevice8_EndScene(device);
+        ok(hr == D3D_OK, "EndScene failed (%08x)\n", hr);
+
+        color = getPixelColor(device, 320, 240);
+        ok(color == 0x0000ff00,
+                "fog ps%i fvm%i ftm%i: got color %08x, expected 0x0000ff00\n",
+                test_data[i].pshader, test_data[i].vfog, test_data[i].tfog, color);
+
+        IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
+    }
+
+    if (pixel_shader[1])
+        IDirect3DDevice8_DeletePixelShader(device, pixel_shader[1]);
+    refcount = IDirect3DDevice8_Release(device);
+    ok(!refcount, "Device has %u references left.\n", refcount);
+done:
+    IDirect3D8_Release(d3d);
+    DestroyWindow(window);
+}
+
 /* This tests fog in combination with shaders.
  * What's tested: linear fog (vertex and table) with pixel shader
  *                linear table fog with non foggy vertex shader
@@ -5092,6 +5245,7 @@ START_TEST(visual)
     offscreen_test();
     alpha_test();
     test_scalar_instructions();
+    fog_righthanded_test();
     fog_with_shader_test();
     cnd_test();
     p8_texture_test();
diff --git a/dlls/d3d9/tests/visual.c b/dlls/d3d9/tests/visual.c
index 117c8eb..788f79e 100644
--- a/dlls/d3d9/tests/visual.c
+++ b/dlls/d3d9/tests/visual.c
@@ -1825,6 +1825,189 @@ done:
     DestroyWindow(window);
 }
 
+/* This test tests fog in combination with a right-handed projection matrix. */
+static void fog_righthanded_test(void)
+{
+    IDirect3DPixelShader9 *pixel_shader[3] = {NULL, NULL, NULL};
+    IDirect3DVertexDeclaration9 *vertex_declaration = NULL;
+    IDirect3DDevice9 *device;
+    unsigned int i;
+    IDirect3D9 *d3d;
+    ULONG refcount;
+    D3DCAPS9 caps;
+    DWORD color;
+    HWND window;
+    HRESULT hr;
+    union
+    {
+        float f;
+        DWORD i;
+    } start, end;
+
+    /* basic pixel shader */
+    static const DWORD pixel_shader_code[] =
+    {
+        0xffff0101,                                                             /* ps_1_1     */
+        0x00000001, 0x800f0000, 0x90e40000,                                     /* mov r0, v0 */
+        0x0000ffff
+    };
+    static const DWORD pixel_shader_code2[] =
+    {
+        0xffff0200,                                                             /* ps_2_0     */
+        0x0200001f, 0x80000000, 0x900f0000,                                     /* dcl v0 */
+        0x02000001, 0x800f0800, 0x90e40000,                                     /* mov oC0, v0 */
+        0x0000ffff
+    };
+    struct
+    {
+        struct vec3 position;
+        DWORD diffuse;
+    }
+    quad[] =
+    {
+        {{-1.0f, -1.0f, 10.0f}, 0xffff0000},
+        {{-1.0f,  1.0f, 10.0f}, 0xffff0000},
+        {{ 1.0f, -1.0f, 10.0f}, 0xffff0000},
+        {{ 1.0f,  1.0f, 10.0f}, 0xffff0000},
+    };
+    static const D3DVERTEXELEMENT9 decl_elements[] =
+    {
+        {0,  0, D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
+        {0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT,    D3DDECLUSAGE_COLOR, 0},
+        D3DDECL_END()
+    };
+    static const struct test_data_t
+    {
+        int pshader;
+        D3DFOGMODE vfog;
+        D3DFOGMODE tfog;
+    }
+    test_data[] =
+    {
+        {0, D3DFOG_NONE, D3DFOG_LINEAR},
+        {1, D3DFOG_NONE, D3DFOG_LINEAR},
+        {2, D3DFOG_NONE, D3DFOG_LINEAR},
+        {0, D3DFOG_LINEAR, D3DFOG_NONE},
+        {1, D3DFOG_LINEAR, D3DFOG_NONE},
+        {2, D3DFOG_LINEAR, D3DFOG_NONE},
+    };
+    /* A right-handed perspective projection matrix designed so that vertices
+     * (x in [-1..1], y in [-1..1], z = 0.1) will be mapped to the view plane.
+     * Obtained from D3DXMatrixPerspectiveFovRH with the parameters
+     * fov=84.29deg, aspect=640/480 near=0.01, far=100 */
+    static const D3DMATRIX proj_mat_rh =
+    {{{
+        0.828732f, 0.0f,       0.0f,       0.0f,
+        0.0f,      1.104976f,  0.0f,       0.0f,
+        0.0f,      0.0f,      -1.0001f,   -1.0f,
+        0.0f,      0.0f,      -0.010001f,  0.0f
+    }}};
+    static const D3DMATRIX view_mat_rh =
+    {{{
+        1.0f, 0.0f,  0.0f, 0.0f,
+        0.0f, 1.0f,  0.0f, 0.0f,
+        0.0f, 0.0f, -0.1f, 0.0f,
+        0.0f, 0.0f,  0.0f, 1.0f
+    }}};
+
+    window = CreateWindowA("static", "d3d9_test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
+            0, 0, 640, 480, NULL, NULL, NULL, NULL);
+    d3d = Direct3DCreate9(D3D_SDK_VERSION);
+    ok(!!d3d, "Failed to create a D3D object.\n");
+    if (!(device = create_device(d3d, window, window, TRUE)))
+    {
+        skip("Failed to create a D3D device, skipping tests.\n");
+        goto done;
+    }
+
+    hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
+    ok(SUCCEEDED(hr), "Failed to get device caps, hr %#x.\n", hr);
+    if (caps.PixelShaderVersion >= D3DPS_VERSION(1, 1))
+    {
+        hr = IDirect3DDevice9_CreatePixelShader(device, pixel_shader_code, &pixel_shader[1]);
+        ok(SUCCEEDED(hr), "CreatePixelShader failed (%08x)\n", hr);
+
+        if (caps.PixelShaderVersion >= D3DPS_VERSION(2, 0))
+        {
+            hr = IDirect3DDevice9_CreatePixelShader(device, pixel_shader_code2, &pixel_shader[2]);
+            ok(SUCCEEDED(hr), "CreatePixelShader failed (%08x)\n", hr);
+        } else {
+            skip("No shader model 2 support, skipping some fog tests.\n");
+        }
+    }
+    else
+    {
+        skip("No shader model 1.1 support, skipping some fog tests.\n");
+    }
+
+    start.f = 0.1f;
+    end.f = 0.9f;
+
+    hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &vertex_declaration);
+    ok(SUCCEEDED(hr), "CreateVertexDeclaration failed (%08x)\n", hr);
+
+    /* Setup initial states: No lighting, fog on, fog color */
+    hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
+    ok(hr == D3D_OK, "Turning off lighting failed (%08x)\n", hr);
+    hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGENABLE, TRUE);
+    ok(hr == D3D_OK, "Turning on fog calculations failed (%08x)\n", hr);
+    hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGCOLOR, 0xff00ff00 /* A nice green */);
+    ok(hr == D3D_OK, "Setting fog color failed (%08x)\n", hr);
+    hr = IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration);
+    ok(SUCCEEDED(hr), "SetVertexDeclaration failed (%08x)\n", hr);
+
+    hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGSTART, start.i);
+    ok(hr == D3D_OK, "Setting fog start failed (%08x)\n", hr);
+    hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGEND, end.i);
+    ok(hr == D3D_OK, "Setting fog end failed (%08x)\n", hr);
+
+    hr = IDirect3DDevice9_SetTransform(device, D3DTS_PROJECTION, &proj_mat_rh);
+    ok(SUCCEEDED(hr), "Failed to set projection transform, hr %#x.\n", hr);
+    hr = IDirect3DDevice9_SetTransform(device, D3DTS_VIEW, &view_mat_rh);
+    ok(SUCCEEDED(hr), "Failed to set view transform, hr %#x.\n", hr);
+
+    for (i = 0; i < sizeof(test_data)/sizeof(test_data[0]); i++)
+    {
+        if (test_data[i].pshader != 0 && pixel_shader[test_data[i].pshader] == NULL) {
+            continue;
+        }
+
+        hr = IDirect3DDevice9_SetPixelShader(device, pixel_shader[test_data[i].pshader]);
+        ok(SUCCEEDED(hr), "SetPixelShader failed (%08x)\n", hr);
+        hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGVERTEXMODE, test_data[i].vfog);
+        ok( hr == D3D_OK, "Setting fog vertex mode to %d failed (%08x)\n", test_data[i].vfog, hr);
+        hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGTABLEMODE, test_data[i].tfog);
+        ok( hr == D3D_OK, "Setting fog table mode to %d failed (%08x)\n", test_data[i].tfog, hr);
+
+        hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffff00ff, 1.0f, 0);
+        ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed (%08x)\n", hr);
+        hr = IDirect3DDevice9_BeginScene(device);
+        ok(hr == D3D_OK, "BeginScene failed (%08x)\n", hr);
+        hr = IDirect3DDevice9_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], sizeof(quad[0]));
+        ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (%08x)\n", hr);
+        hr = IDirect3DDevice9_EndScene(device);
+        ok(hr == D3D_OK, "EndScene failed (%08x)\n", hr);
+
+        color = getPixelColor(device, 320, 240);
+        ok(color == 0x0000ff00,
+                "fog ps%i fvm%i ftm%i: got color %08x, expected 0x0000ff00\n",
+                test_data[i].pshader, test_data[i].vfog, test_data[i].tfog, color);
+
+        IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
+    }
+
+    if (pixel_shader[1])
+        IDirect3DPixelShader9_Release(pixel_shader[1]);
+    if (pixel_shader[2])
+        IDirect3DPixelShader9_Release(pixel_shader[2]);
+    IDirect3DVertexDeclaration9_Release(vertex_declaration);
+    refcount = IDirect3DDevice9_Release(device);
+    ok(!refcount, "Device has %u references left.\n", refcount);
+done:
+    IDirect3D9_Release(d3d);
+    DestroyWindow(window);
+}
+
 /* This test tests fog in combination with shaders.
  * What's tested: linear fog (vertex and table) with pixel shader
  *                linear table fog with non foggy vertex shader
@@ -16729,6 +16912,7 @@ START_TEST(visual)
     test_vshader_input();
     test_vshader_float16();
     stream_test();
+    fog_righthanded_test();
     fog_with_shader_test();
     texbem_test();
     texdepth_test();
diff --git a/dlls/wined3d/glsl_shader.c b/dlls/wined3d/glsl_shader.c
index 34cc567..f158ee2 100644
--- a/dlls/wined3d/glsl_shader.c
+++ b/dlls/wined3d/glsl_shader.c
@@ -5005,7 +5005,7 @@ static GLhandleARB shader_glsl_generate_ffp_vertex_shader(struct wined3d_shader_
                 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
                 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z * 0.5 + 0.5;\n");
             else
-                shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
+                shader_addline(buffer, "gl_FogFragCoord = abs(ec_pos.z);\n");
             break;
 
         default:
-- 
1.8.4.5




More information about the wine-patches mailing list