From 62f43eeb0211da68249ff588d2c3bb1f0a5815c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3zef=20Kucia?= Date: Fri, 6 Jan 2017 10:33:55 +0100 Subject: [PATCH] d3d8: Do not create render target view while surface refcount is equal to 0. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes a regression introduced by commit b005ad6f905dc0051b17ea45b3690e0662e48481. Signed-off-by: Józef Kucia Signed-off-by: Henri Verbeet Signed-off-by: Alexandre Julliard --- dlls/d3d8/d3d8_private.h | 4 +++- dlls/d3d8/device.c | 13 +++++++------ dlls/d3d8/surface.c | 15 ++++++++++++++- dlls/d3d8/tests/visual.c | 9 ++++----- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/dlls/d3d8/d3d8_private.h b/dlls/d3d8/d3d8_private.h index ffcd1cde730..8177b5e3d65 100644 --- a/dlls/d3d8/d3d8_private.h +++ b/dlls/d3d8/d3d8_private.h @@ -242,8 +242,10 @@ struct d3d8_surface struct d3d8_texture *texture; }; +struct wined3d_rendertarget_view *d3d8_surface_acquire_rendertarget_view(struct d3d8_surface *surface) DECLSPEC_HIDDEN; struct d3d8_device *d3d8_surface_get_device(const struct d3d8_surface *surface) DECLSPEC_HIDDEN; -struct wined3d_rendertarget_view *d3d8_surface_get_rendertarget_view(struct d3d8_surface *surface) DECLSPEC_HIDDEN; +void d3d8_surface_release_rendertarget_view(struct d3d8_surface *surface, + struct wined3d_rendertarget_view *rtv) DECLSPEC_HIDDEN; void surface_init(struct d3d8_surface *surface, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx, const struct wined3d_parent_ops **parent_ops) DECLSPEC_HIDDEN; struct d3d8_surface *unsafe_impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface) DECLSPEC_HIDDEN; diff --git a/dlls/d3d8/device.c b/dlls/d3d8/device.c index 5a5871f6bbd..43b8e2be4bb 100644 --- a/dlls/d3d8/device.c +++ b/dlls/d3d8/device.c @@ -1156,7 +1156,7 @@ static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface, struct d3d8_device *device = impl_from_IDirect3DDevice8(iface); struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target); struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil); - struct wined3d_rendertarget_view *original_dsv; + struct wined3d_rendertarget_view *original_dsv, *rtv; HRESULT hr = D3D_OK; TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil); @@ -1178,7 +1178,6 @@ static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface, /* If no render target is passed in check the size against the current RT */ if (!render_target) { - if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0))) { wined3d_mutex_unlock(); @@ -1210,11 +1209,13 @@ static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface, } original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device); - wined3d_device_set_depth_stencil_view(device->wined3d_device, - ds_impl ? d3d8_surface_get_rendertarget_view(ds_impl) : NULL); - if (render_target && FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0, - d3d8_surface_get_rendertarget_view(rt_impl), TRUE))) + rtv = ds_impl ? d3d8_surface_acquire_rendertarget_view(ds_impl) : NULL; + wined3d_device_set_depth_stencil_view(device->wined3d_device, rtv); + d3d8_surface_release_rendertarget_view(ds_impl, rtv); + rtv = render_target ? d3d8_surface_acquire_rendertarget_view(rt_impl) : NULL; + if (render_target && FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0, rtv, TRUE))) wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv); + d3d8_surface_release_rendertarget_view(rt_impl, rtv); wined3d_mutex_unlock(); diff --git a/dlls/d3d8/surface.c b/dlls/d3d8/surface.c index 137033eaa2d..81ddd8f162d 100644 --- a/dlls/d3d8/surface.c +++ b/dlls/d3d8/surface.c @@ -360,10 +360,15 @@ struct d3d8_device *d3d8_surface_get_device(const struct d3d8_surface *surface) return impl_from_IDirect3DDevice8(device); } -struct wined3d_rendertarget_view *d3d8_surface_get_rendertarget_view(struct d3d8_surface *surface) +struct wined3d_rendertarget_view *d3d8_surface_acquire_rendertarget_view(struct d3d8_surface *surface) { HRESULT hr; + /* The surface reference count can be equal to 0 when this function is + * called. In order to properly manage the render target view reference + * count, we temporarily increment the surface reference count. */ + d3d8_surface_AddRef(&surface->IDirect3DSurface8_iface); + if (surface->wined3d_rtv) return surface->wined3d_rtv; @@ -371,6 +376,7 @@ struct wined3d_rendertarget_view *d3d8_surface_get_rendertarget_view(struct d3d8 surface->sub_resource_idx, surface, &d3d8_view_wined3d_parent_ops, &surface->wined3d_rtv))) { ERR("Failed to create rendertarget view, hr %#x.\n", hr); + d3d8_surface_Release(&surface->IDirect3DSurface8_iface); return NULL; } @@ -380,6 +386,13 @@ struct wined3d_rendertarget_view *d3d8_surface_get_rendertarget_view(struct d3d8 return surface->wined3d_rtv; } +void d3d8_surface_release_rendertarget_view(struct d3d8_surface *surface, + struct wined3d_rendertarget_view *rtv) +{ + if (rtv) + d3d8_surface_Release(&surface->IDirect3DSurface8_iface); +} + struct d3d8_surface *unsafe_impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface) { if (!iface) diff --git a/dlls/d3d8/tests/visual.c b/dlls/d3d8/tests/visual.c index 0fb85d35bc6..83b4b7e146e 100644 --- a/dlls/d3d8/tests/visual.c +++ b/dlls/d3d8/tests/visual.c @@ -9684,7 +9684,6 @@ static void test_max_index16(void) DestroyWindow(window); } -/* This test exercises a regression in Wine d3d8 implementation. */ static void test_backbuffer_resize(void) { D3DPRESENT_PARAMETERS present_parameters = {0}; @@ -9719,8 +9718,8 @@ static void test_backbuffer_resize(void) goto done; } - /* In order to exercise the regression the backbuffer surface has to be - * unreferenced when SetRenderTarget() is called. */ + /* Wine d3d8 implementation had a bug which was triggered by a + * SetRenderTarget() call with an unreferenced surface. */ hr = IDirect3DDevice8_GetBackBuffer(device, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer); ok(SUCCEEDED(hr), "Failed to get backbuffer, hr %#x.\n", hr); refcount = IDirect3DSurface8_Release(backbuffer); @@ -9766,7 +9765,7 @@ static void test_backbuffer_resize(void) color = getPixelColor(device, 1, 1); ok(color == 0x00ffff00, "Got unexpected color 0x%08x.\n", color); color = getPixelColor(device, 700, 500); - todo_wine ok(color == 0x00ffff00, "Got unexpected color 0x%08x.\n", color); + ok(color == 0x00ffff00, "Got unexpected color 0x%08x.\n", color); hr = IDirect3DDevice8_BeginScene(device); ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr); @@ -9777,7 +9776,7 @@ static void test_backbuffer_resize(void) color = getPixelColor(device, 1, 1); ok(color == 0x0000ff00, "Got unexpected color 0x%08x.\n", color); color = getPixelColor(device, 700, 500); - todo_wine ok(color == 0x0000ff00, "Got unexpected color 0x%08x.\n", color); + ok(color == 0x0000ff00, "Got unexpected color 0x%08x.\n", color); refcount = IDirect3DDevice8_Release(device); ok(!refcount, "Device has %u references left.\n", refcount);