From ba0d7248f185c8d6024f0d92b8c87b2d7153317c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20D=C3=B6singer?= <stefan@codeweavers.com>
Date: Wed, 3 Sep 2014 20:20:39 +0200
Subject: wined3d: Wait for the resource to be idle when destroying user memory
 surfaces.

---
 dlls/wined3d/surface.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/dlls/wined3d/surface.c b/dlls/wined3d/surface.c
index 82485f3..3557c00 100644
--- a/dlls/wined3d/surface.c
+++ b/dlls/wined3d/surface.c
@@ -84,6 +84,8 @@ void wined3d_surface_cleanup_cs(struct wined3d_surface *surface)
 static void surface_cleanup(struct wined3d_surface *surface)
 {
     struct wined3d_surface *overlay, *cur;
+    struct wined3d_cs *cs = surface->resource.device->cs;
+    BOOL user_mem = surface->resource.map_binding == WINED3D_LOCATION_USER_MEMORY;
 
     TRACE("surface %p.\n", surface);
 
@@ -97,7 +99,13 @@ static void surface_cleanup(struct wined3d_surface *surface)
     }
 
     resource_cleanup(&surface->resource);
-    wined3d_cs_emit_surface_cleanup(surface->resource.device->cs, surface);
+    wined3d_cs_emit_surface_cleanup(cs, surface);
+
+    /* Wait for the CS to finish operations on this surface when user memory was in use.
+     * The application is allowed to free the memory after texture / surface destruction
+     * returns. */
+    if (user_mem)
+        wined3d_resource_wait_fence(&surface->container->resource);
 }
 
 void wined3d_surface_destroy(struct wined3d_surface *surface)
-- 
2.1.3

