The first two patches here address an issue I encountered when using Wine's d3dx9 with DXVK in the game Command and Conquer 3. The game calls `D3DXLoadSurfaceFromMemory` on a multisampled render target. This ends up going through the `lock_surface()` helper in d3dx9, which eventually calls `UpdateSurface()` in `unlock_surface()` to copy the staging surface back to the multisampled render target. This succeeds on wined3d even though it shouldn't, and fails on DXVK like it should.
The second two patches address an issue reported in Empires: Dawn of the Modern World, where the game expects a device reset to reset the scene state.
-- v3: wined3d: Clear scene state on device state reset. d3d8/tests: Add a test for device reset after beginning a scene. d3d9/tests: Add a test for device reset after beginning a scene.
From: Connor McAdams cmcadams@codeweavers.com
Signed-off-by: Connor McAdams cmcadams@codeweavers.com --- dlls/d3d9/tests/visual.c | 7 +++++++ 1 file changed, 7 insertions(+)
diff --git a/dlls/d3d9/tests/visual.c b/dlls/d3d9/tests/visual.c index 724d9ef1e39..806ab8f2802 100644 --- a/dlls/d3d9/tests/visual.c +++ b/dlls/d3d9/tests/visual.c @@ -4546,6 +4546,13 @@ static void test_multisample_stretch_rect(void) hr = IDirect3DDevice9_SetRenderTarget(device, 0, rt); ok(hr == D3D_OK, "Got unexpected hr %#lx.\n", hr);
+ /* UpdateSurface does not support multisampled surfaces. */ + hr = IDirect3DDevice9_UpdateSurface(device, rt, NULL, ms_rt, NULL); + todo_wine ok(hr == D3DERR_INVALIDCALL, "Got unexpected hr %#lx.\n", hr); + + hr = IDirect3DDevice9_UpdateSurface(device, ms_rt, NULL, rt, NULL); + todo_wine ok(hr == D3DERR_INVALIDCALL, "Got unexpected hr %#lx.\n", hr); + for (i = 0; i < ARRAY_SIZE(filters); ++i) { hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0f, 0);
From: Connor McAdams cmcadams@codeweavers.com
Signed-off-by: Connor McAdams cmcadams@codeweavers.com --- dlls/d3d9/device.c | 7 +++++++ dlls/d3d9/tests/visual.c | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/dlls/d3d9/device.c b/dlls/d3d9/device.c index 408dace48ad..e5b5463dbba 100644 --- a/dlls/d3d9/device.c +++ b/dlls/d3d9/device.c @@ -1800,6 +1800,13 @@ static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface, return D3DERR_INVALIDCALL; }
+ if (src_desc.multisample_type != WINED3D_MULTISAMPLE_NONE || dst_desc.multisample_type != WINED3D_MULTISAMPLE_NONE) + { + wined3d_mutex_unlock(); + WARN("Cannot use UpdateSurface with multisampled surfaces.\n"); + return D3DERR_INVALIDCALL; + } + if (src_rect) wined3d_box_set(&src_box, src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1); else diff --git a/dlls/d3d9/tests/visual.c b/dlls/d3d9/tests/visual.c index 806ab8f2802..3f3de411143 100644 --- a/dlls/d3d9/tests/visual.c +++ b/dlls/d3d9/tests/visual.c @@ -4548,10 +4548,10 @@ static void test_multisample_stretch_rect(void)
/* UpdateSurface does not support multisampled surfaces. */ hr = IDirect3DDevice9_UpdateSurface(device, rt, NULL, ms_rt, NULL); - todo_wine ok(hr == D3DERR_INVALIDCALL, "Got unexpected hr %#lx.\n", hr); + ok(hr == D3DERR_INVALIDCALL, "Got unexpected hr %#lx.\n", hr);
hr = IDirect3DDevice9_UpdateSurface(device, ms_rt, NULL, rt, NULL); - todo_wine ok(hr == D3DERR_INVALIDCALL, "Got unexpected hr %#lx.\n", hr); + ok(hr == D3DERR_INVALIDCALL, "Got unexpected hr %#lx.\n", hr);
for (i = 0; i < ARRAY_SIZE(filters); ++i) {
From: Connor McAdams cmcadams@codeweavers.com
Signed-off-by: Connor McAdams cmcadams@codeweavers.com --- dlls/ddraw/tests/ddraw2.c | 4 ++++ dlls/ddraw/tests/ddraw4.c | 4 ++++ dlls/ddraw/tests/ddraw7.c | 4 ++++ 3 files changed, 12 insertions(+)
diff --git a/dlls/ddraw/tests/ddraw2.c b/dlls/ddraw/tests/ddraw2.c index 48a49815c58..198e40bab55 100644 --- a/dlls/ddraw/tests/ddraw2.c +++ b/dlls/ddraw/tests/ddraw2.c @@ -16670,6 +16670,8 @@ static void test_d3d_state_reset(void) ok(hr == DD_OK, "got %#lx.\n", hr); hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ZENABLE, TRUE); ok(hr == DD_OK, "got %#lx.\n", hr); + hr = IDirect3DDevice2_BeginScene(device); + ok(hr == DD_OK, "got %#lx.\n", hr);
memset(¶m, 0, sizeof(param)); hr = IDirectDraw2_EnumDisplayModes(ddraw, 0, NULL, ¶m, find_different_mode_callback); @@ -16718,6 +16720,8 @@ static void test_d3d_state_reset(void) hr = IDirect3DDevice2_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &state); ok(hr == DD_OK, "got %#lx.\n", hr); ok(state == TRUE, "got %#lx.\n", state); + hr = IDirect3DDevice2_BeginScene(device); + ok(hr == D3DERR_SCENE_IN_SCENE, "Unexpected hr %#lx.\n", hr);
hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL); ok(hr == DD_OK, "got %#lx.\n", hr); diff --git a/dlls/ddraw/tests/ddraw4.c b/dlls/ddraw/tests/ddraw4.c index 8abd9cd44fd..7f7c563303f 100644 --- a/dlls/ddraw/tests/ddraw4.c +++ b/dlls/ddraw/tests/ddraw4.c @@ -19774,6 +19774,8 @@ static void test_d3d_state_reset(void) ok(hr == DD_OK, "got %#lx.\n", hr); hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, TRUE); ok(hr == DD_OK, "got %#lx.\n", hr); + hr = IDirect3DDevice3_BeginScene(device); + ok(hr == DD_OK, "got %#lx.\n", hr);
memset(¶m, 0, sizeof(param)); hr = IDirectDraw4_EnumDisplayModes(ddraw, 0, NULL, ¶m, find_different_mode_callback); @@ -19822,6 +19824,8 @@ static void test_d3d_state_reset(void) hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &state); ok(hr == DD_OK, "got %#lx.\n", hr); ok(state == TRUE, "got %#lx.\n", state); + hr = IDirect3DDevice3_BeginScene(device); + ok(hr == D3DERR_SCENE_IN_SCENE, "Unexpected hr %#lx.\n", hr);
hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL); ok(hr == DD_OK, "got %#lx.\n", hr); diff --git a/dlls/ddraw/tests/ddraw7.c b/dlls/ddraw/tests/ddraw7.c index 3e03d3e4dd6..76c52be0258 100644 --- a/dlls/ddraw/tests/ddraw7.c +++ b/dlls/ddraw/tests/ddraw7.c @@ -20229,6 +20229,8 @@ static void test_d3d_state_reset(void) ok(hr == DD_OK, "got %#lx.\n", hr); hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ZENABLE, TRUE); ok(hr == DD_OK, "got %#lx.\n", hr); + hr = IDirect3DDevice7_BeginScene(device); + ok(hr == DD_OK, "got %#lx.\n", hr);
hr = IDirect3DDevice7_GetViewport(device, &vp1); ok(hr == DD_OK, "got %#lx.\n", hr); @@ -20269,6 +20271,8 @@ static void test_d3d_state_reset(void) hr = IDirect3DDevice7_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &state); ok(hr == DD_OK, "got %#lx.\n", hr); ok(state == TRUE, "got %#lx.\n", state); + hr = IDirect3DDevice7_BeginScene(device); + ok(hr == D3DERR_SCENE_IN_SCENE, "Unexpected hr %#lx.\n", hr);
hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL); ok(hr == DD_OK, "got %#lx.\n", hr);
From: Connor McAdams cmcadams@codeweavers.com
Signed-off-by: Connor McAdams cmcadams@codeweavers.com --- dlls/d3d9/tests/d3d9ex.c | 140 +++++++++++++++++++++++++++++++++++++++ dlls/d3d9/tests/device.c | 13 ++++ 2 files changed, 153 insertions(+)
diff --git a/dlls/d3d9/tests/d3d9ex.c b/dlls/d3d9/tests/d3d9ex.c index 8e7eec13566..e946b960055 100644 --- a/dlls/d3d9/tests/d3d9ex.c +++ b/dlls/d3d9/tests/d3d9ex.c @@ -5058,6 +5058,145 @@ static void test_desktop_window(void) IDirect3DDevice9Ex_Release(device); }
+static void test_scene(void) +{ + IDirect3DSurface9 *surface1, *surface2, *surface3; + IDirect3DSurface9 *backBuffer, *rt, *ds; + RECT rect = {0, 0, 128, 128}; + IDirect3DDevice9Ex *device; + IDirect3D9 *d3d; + ULONG refcount; + D3DCAPS9 caps; + HWND window; + HRESULT hr; + + window = create_window(); + if (!(device = create_device(window, NULL))) + { + skip("Failed to create a D3D device.\n"); + DestroyWindow(window); + return; + } + + hr = IDirect3DDevice9Ex_GetDirect3D(device, &d3d); + ok(SUCCEEDED(hr), "Failed to get Direct3D9, hr %#lx.\n", hr); + + /* Get the caps, they will be needed to tell if an operation is supposed to be valid */ + memset(&caps, 0, sizeof(caps)); + hr = IDirect3DDevice9Ex_GetDeviceCaps(device, &caps); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + + /* Test an EndScene without BeginScene. Should return an error */ + hr = IDirect3DDevice9Ex_EndScene(device); + ok(hr == D3DERR_INVALIDCALL, "Got hr %#lx.\n", hr); + + /* Test a normal BeginScene / EndScene pair, this should work */ + hr = IDirect3DDevice9Ex_BeginScene(device); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + hr = IDirect3DDevice9Ex_EndScene(device); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + + /* Test another EndScene without having begun a new scene. Should return an error */ + hr = IDirect3DDevice9Ex_EndScene(device); + ok(hr == D3DERR_INVALIDCALL, "Got hr %#lx.\n", hr); + + /* Two nested BeginScene and EndScene calls */ + hr = IDirect3DDevice9Ex_BeginScene(device); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + hr = IDirect3DDevice9Ex_BeginScene(device); + ok(hr == D3DERR_INVALIDCALL, "Got hr %#lx.\n", hr); + hr = IDirect3DDevice9Ex_EndScene(device); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + hr = IDirect3DDevice9Ex_EndScene(device); + ok(hr == D3DERR_INVALIDCALL, "Got hr %#lx.\n", hr); + + /* Calling Reset does not clear scene state, different from d3d9. */ + hr = IDirect3DDevice9Ex_BeginScene(device); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + + reset_device(device, NULL); + hr = IDirect3DDevice9Ex_EndScene(device); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + + hr = IDirect3DDevice9Ex_BeginScene(device); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + hr = IDirect3DDevice9Ex_EndScene(device); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + + /* Create some surfaces to test stretchrect between the scenes */ + hr = IDirect3DDevice9Ex_CreateOffscreenPlainSurface(device, 128, 128, + D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surface1, NULL); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + hr = IDirect3DDevice9Ex_CreateOffscreenPlainSurface(device, 128, 128, + D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surface2, NULL); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + hr = IDirect3DDevice9Ex_CreateDepthStencilSurface(device, 800, 600, + D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, FALSE, &surface3, NULL); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + hr = IDirect3DDevice9Ex_CreateRenderTarget(device, 128, 128, + D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, FALSE, &rt, NULL); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + + hr = IDirect3DDevice9Ex_GetBackBuffer(device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + hr = IDirect3DDevice9Ex_GetDepthStencilSurface(device, &ds); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + + /* First make sure a simple StretchRect call works */ + hr = IDirect3DDevice9Ex_StretchRect(device, surface1, NULL, surface2, NULL, 0); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + hr = IDirect3DDevice9Ex_StretchRect(device, backBuffer, &rect, rt, NULL, 0); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + if (0) /* Disabled for now because it crashes in wine */ + { + HRESULT expected = caps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES ? D3D_OK : D3DERR_INVALIDCALL; + hr = IDirect3DDevice9Ex_StretchRect(device, ds, NULL, surface3, NULL, 0); + ok(hr == expected, "Got unexpected hr %#lx, expected %#lx.\n", hr, expected); + } + + /* Now try it in a BeginScene - EndScene pair. Seems to be allowed in a + * BeginScene - Endscene pair with normal surfaces and render targets, but + * not depth stencil surfaces. */ + hr = IDirect3DDevice9Ex_BeginScene(device); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + + hr = IDirect3DDevice9Ex_StretchRect(device, surface1, NULL, surface2, NULL, 0); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + hr = IDirect3DDevice9Ex_StretchRect(device, backBuffer, &rect, rt, NULL, 0); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + /* This is supposed to fail inside a BeginScene - EndScene pair. */ + hr = IDirect3DDevice9Ex_StretchRect(device, ds, NULL, surface3, NULL, 0); + ok(hr == D3DERR_INVALIDCALL, "Got hr %#lx.\n", hr); + + hr = IDirect3DDevice9Ex_EndScene(device); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + + /* Does a SetRenderTarget influence BeginScene / EndScene ? + * Set a new render target, then see if it started a new scene. Flip the rt back and see if that maybe + * ended the scene. Expected result is that the scene is not affected by SetRenderTarget + */ + hr = IDirect3DDevice9Ex_SetRenderTarget(device, 0, rt); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + hr = IDirect3DDevice9Ex_BeginScene(device); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + hr = IDirect3DDevice9Ex_SetRenderTarget(device, 0, backBuffer); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + hr = IDirect3DDevice9Ex_EndScene(device); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + + IDirect3DSurface9_Release(rt); + IDirect3DSurface9_Release(ds); + IDirect3DSurface9_Release(backBuffer); + IDirect3DSurface9_Release(surface1); + IDirect3DSurface9_Release(surface2); + IDirect3DSurface9_Release(surface3); + refcount = IDirect3DDevice9Ex_Release(device); + ok(!refcount, "Device has %lu references left.\n", refcount); + + IDirect3D9_Release(d3d); + DestroyWindow(window); +} + START_TEST(d3d9ex) { DEVMODEW current_mode; @@ -5118,6 +5257,7 @@ START_TEST(d3d9ex) test_sysmem_draw(); test_pinned_buffers(); test_desktop_window(); + test_scene();
UnregisterClassA("d3d9_test_wc", GetModuleHandleA(NULL)); } diff --git a/dlls/d3d9/tests/device.c b/dlls/d3d9/tests/device.c index 53dc9ee55ed..0cd290367b5 100644 --- a/dlls/d3d9/tests/device.c +++ b/dlls/d3d9/tests/device.c @@ -2718,6 +2718,19 @@ static void test_scene(void) hr = IDirect3DDevice9_EndScene(device); ok(hr == D3DERR_INVALIDCALL, "Got hr %#lx.\n", hr);
+ /* Calling Reset clears scene state. */ + hr = IDirect3DDevice9_BeginScene(device); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + + reset_device(device, NULL); + hr = IDirect3DDevice9_EndScene(device); + todo_wine ok(hr == D3DERR_INVALIDCALL, "Got hr %#lx.\n", hr); + + hr = IDirect3DDevice9_BeginScene(device); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + hr = IDirect3DDevice9_EndScene(device); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + /* Create some surfaces to test stretchrect between the scenes */ hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 128, 128, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surface1, NULL);
From: Connor McAdams cmcadams@codeweavers.com
Signed-off-by: Connor McAdams cmcadams@codeweavers.com --- dlls/d3d8/tests/device.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
diff --git a/dlls/d3d8/tests/device.c b/dlls/d3d8/tests/device.c index 8261a3139c5..d88fa2abc19 100644 --- a/dlls/d3d8/tests/device.c +++ b/dlls/d3d8/tests/device.c @@ -2174,6 +2174,19 @@ static void test_scene(void) hr = IDirect3DDevice8_EndScene(device); ok(hr == D3DERR_INVALIDCALL, "Got hr %#lx.\n", hr);
+ /* Calling Reset clears scene state. */ + hr = IDirect3DDevice8_BeginScene(device); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + + reset_device(device, NULL); + hr = IDirect3DDevice8_EndScene(device); + todo_wine ok(hr == D3DERR_INVALIDCALL, "Got hr %#lx.\n", hr); + + hr = IDirect3DDevice8_BeginScene(device); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + hr = IDirect3DDevice8_EndScene(device); + ok(hr == S_OK, "Got hr %#lx.\n", hr); + /* StretchRect does not exit in Direct3D8, so no equivalent to the d3d9 stretchrect tests */
refcount = IDirect3DDevice8_Release(device);
From: Connor McAdams cmcadams@codeweavers.com
Signed-off-by: Connor McAdams cmcadams@codeweavers.com --- dlls/d3d8/tests/device.c | 2 +- dlls/d3d9/device.c | 1 + dlls/d3d9/tests/device.c | 2 +- dlls/wined3d/device.c | 2 ++ 4 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/dlls/d3d8/tests/device.c b/dlls/d3d8/tests/device.c index d88fa2abc19..10c06b17ba7 100644 --- a/dlls/d3d8/tests/device.c +++ b/dlls/d3d8/tests/device.c @@ -2180,7 +2180,7 @@ static void test_scene(void)
reset_device(device, NULL); hr = IDirect3DDevice8_EndScene(device); - todo_wine ok(hr == D3DERR_INVALIDCALL, "Got hr %#lx.\n", hr); + ok(hr == D3DERR_INVALIDCALL, "Got hr %#lx.\n", hr);
hr = IDirect3DDevice8_BeginScene(device); ok(hr == S_OK, "Got hr %#lx.\n", hr); diff --git a/dlls/d3d9/device.c b/dlls/d3d9/device.c index e5b5463dbba..b6a3e1f58ba 100644 --- a/dlls/d3d9/device.c +++ b/dlls/d3d9/device.c @@ -1185,6 +1185,7 @@ static HRESULT d3d9_device_reset(struct d3d9_device *device, surface->parent_device = &device->IDirect3DDevice9Ex_iface; }
+ device->in_scene = FALSE; device->device_state = D3D9_DEVICE_STATE_OK;
if (extended) diff --git a/dlls/d3d9/tests/device.c b/dlls/d3d9/tests/device.c index 0cd290367b5..62f308e0734 100644 --- a/dlls/d3d9/tests/device.c +++ b/dlls/d3d9/tests/device.c @@ -2724,7 +2724,7 @@ static void test_scene(void)
reset_device(device, NULL); hr = IDirect3DDevice9_EndScene(device); - todo_wine ok(hr == D3DERR_INVALIDCALL, "Got hr %#lx.\n", hr); + ok(hr == D3DERR_INVALIDCALL, "Got hr %#lx.\n", hr);
hr = IDirect3DDevice9_BeginScene(device); ok(hr == S_OK, "Got hr %#lx.\n", hr); diff --git a/dlls/wined3d/device.c b/dlls/wined3d/device.c index acc70256462..1d9f96e897b 100644 --- a/dlls/wined3d/device.c +++ b/dlls/wined3d/device.c @@ -5227,6 +5227,8 @@ HRESULT CDECL wined3d_device_reset(struct wined3d_device *device, if (reset_state) { TRACE("Resetting state.\n"); + if (device->inScene) + wined3d_device_end_scene(device); wined3d_device_context_emit_reset_state(&device->cs->c, false); state_cleanup(state);
On Mon Jul 15 21:41:39 2024 +0000, Elizabeth Figura wrote:
Unfortunately this won't be hit in the d3d9ex case, and a quick test shows that d3d9ex should end the scene here. So we probably do need to do wined3d_device_end_scene() from the client libraries after all :-/
Are you sure about this? I've just copied over the `test_scene()` test from d3d9's `device.c` test into `d3d9ex.c` and it doesn't seem to end the scene upon device reset. I've updated the MR branch with the d3d9ex tests.
On Tue Jul 16 12:55:13 2024 +0000, Connor McAdams wrote:
Are you sure about this? I've just copied over the `test_scene()` test from d3d9's `device.c` test into `d3d9ex.c` and it doesn't seem to end the scene upon device reset. I've updated the MR branch with the d3d9ex tests.
Gah, you're right, I got the results backwards.
This merge request was approved by Elizabeth Figura.
This merge request was approved by Jan Sikorski.