From 534d7a18db58161682a42210ced42e8e7cf55ba8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20D=C3=B6singer?= <stefan@codeweavers.com>
Date: Wed, 3 Apr 2013 18:01:34 +0200
Subject: wined3d: Wait for the cs before destroying objects

This is to avoid destroying objects that are still referenced in
commands in the cs. Later patches will migrate resource destruction to
the CS.
---
 dlls/wined3d/buffer.c            | 6 ++++++
 dlls/wined3d/cs.c                | 2 ++
 dlls/wined3d/shader.c            | 5 +++++
 dlls/wined3d/surface.c           | 6 ++++++
 dlls/wined3d/texture.c           | 6 ++++++
 dlls/wined3d/vertexdeclaration.c | 5 +++++
 dlls/wined3d/wined3d_private.h   | 1 +
 7 files changed, 31 insertions(+)

diff --git a/dlls/wined3d/buffer.c b/dlls/wined3d/buffer.c
index 8165d6b..bc34050 100644
--- a/dlls/wined3d/buffer.c
+++ b/dlls/wined3d/buffer.c
@@ -552,6 +552,12 @@ ULONG CDECL wined3d_buffer_decref(struct wined3d_buffer *buffer)
 
     if (!refcount)
     {
+        if (wined3d_settings.cs_multithreaded)
+        {
+            FIXME("Waiting for cs.\n");
+            buffer->resource.device->cs->ops->finish(buffer->resource.device->cs);
+        }
+
         if (buffer->buffer_object)
         {
             context = context_acquire(buffer->resource.device, NULL);
diff --git a/dlls/wined3d/cs.c b/dlls/wined3d/cs.c
index eac7739..8823c9a 100644
--- a/dlls/wined3d/cs.c
+++ b/dlls/wined3d/cs.c
@@ -1188,12 +1188,14 @@ static const struct wined3d_cs_ops wined3d_cs_st_ops =
 {
     wined3d_cs_st_require_space,
     wined3d_cs_st_submit,
+    wined3d_cs_st_submit,
 };
 
 static const struct wined3d_cs_ops wined3d_cs_mt_ops =
 {
     wined3d_cs_mt_require_space,
     wined3d_cs_flush_and_wait,
+    wined3d_cs_flush_and_wait,
 };
 
 /* FIXME: wined3d_device_uninit_3d() should either flush and wait, or be an
diff --git a/dlls/wined3d/shader.c b/dlls/wined3d/shader.c
index cc969b8..0faabaa 100644
--- a/dlls/wined3d/shader.c
+++ b/dlls/wined3d/shader.c
@@ -1957,6 +1957,11 @@ ULONG CDECL wined3d_shader_decref(struct wined3d_shader *shader)
 
     if (!refcount)
     {
+        if (wined3d_settings.cs_multithreaded)
+        {
+            FIXME("Waiting for cs.\n");
+            shader->device->cs->ops->finish(shader->device->cs);
+        }
         shader_cleanup(shader);
         shader->parent_ops->wined3d_object_destroyed(shader->parent);
         HeapFree(GetProcessHeap(), 0, shader);
diff --git a/dlls/wined3d/surface.c b/dlls/wined3d/surface.c
index 8fa2c9f..8f8af4d 100644
--- a/dlls/wined3d/surface.c
+++ b/dlls/wined3d/surface.c
@@ -42,6 +42,12 @@ static void surface_cleanup(struct wined3d_surface *surface)
 
     TRACE("surface %p.\n", surface);
 
+    if (wined3d_settings.cs_multithreaded)
+    {
+        FIXME("Waiting for cs.\n");
+        surface->resource.device->cs->ops->finish(surface->resource.device->cs);
+    }
+
     if (surface->resource.buffer_object || surface->rb_multisample
             || surface->rb_resolved || !list_empty(&surface->renderbuffers))
     {
diff --git a/dlls/wined3d/texture.c b/dlls/wined3d/texture.c
index ec5c847..68889e5 100644
--- a/dlls/wined3d/texture.c
+++ b/dlls/wined3d/texture.c
@@ -136,6 +136,12 @@ static void wined3d_texture_cleanup(struct wined3d_texture *texture)
 
     TRACE("texture %p.\n", texture);
 
+    if (wined3d_settings.cs_multithreaded)
+    {
+        FIXME("Waiting for cs.\n");
+        texture->resource.device->cs->ops->finish(texture->resource.device->cs);
+    }
+
     for (i = 0; i < sub_count; ++i)
     {
         struct wined3d_resource *sub_resource = texture->sub_resources[i];
diff --git a/dlls/wined3d/vertexdeclaration.c b/dlls/wined3d/vertexdeclaration.c
index cf5378c..3f4d5e4 100644
--- a/dlls/wined3d/vertexdeclaration.c
+++ b/dlls/wined3d/vertexdeclaration.c
@@ -56,6 +56,11 @@ ULONG CDECL wined3d_vertex_declaration_decref(struct wined3d_vertex_declaration
 
     if (!refcount)
     {
+        if (wined3d_settings.cs_multithreaded)
+        {
+            FIXME("Waiting for cs.\n");
+            declaration->device->cs->ops->finish(declaration->device->cs);
+        }
         HeapFree(GetProcessHeap(), 0, declaration->elements);
         declaration->parent_ops->wined3d_object_destroyed(declaration->parent);
         HeapFree(GetProcessHeap(), 0, declaration);
diff --git a/dlls/wined3d/wined3d_private.h b/dlls/wined3d/wined3d_private.h
index a48cc7d..b856b6f 100644
--- a/dlls/wined3d/wined3d_private.h
+++ b/dlls/wined3d/wined3d_private.h
@@ -2544,6 +2544,7 @@ struct wined3d_cs_ops
 {
     void *(*require_space)(struct wined3d_cs *cs, size_t size);
     void (*submit)(struct wined3d_cs *cs);
+    void (*finish)(struct wined3d_cs *cs);
 };
 
 struct wined3d_cs
-- 
2.1.3

