From ed3e1f3c0fb3ed7d687235e0683039169d412c99 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20D=C3=B6singer?= <stefan@codeweavers.com>
Date: Sat, 6 Jul 2013 18:31:41 +0200
Subject: wined3d: Check our CS state to find out if a query is done

---
 dlls/wined3d/cs.c              | 6 +++++-
 dlls/wined3d/query.c           | 9 +++++++++
 dlls/wined3d/wined3d_private.h | 2 ++
 3 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/dlls/wined3d/cs.c b/dlls/wined3d/cs.c
index e4d60d8..ba8e840 100644
--- a/dlls/wined3d/cs.c
+++ b/dlls/wined3d/cs.c
@@ -1705,8 +1705,12 @@ void wined3d_cs_emit_resource_unmap(struct wined3d_cs *cs, struct wined3d_resour
 static UINT wined3d_cs_exec_query_issue(struct wined3d_cs *cs, const void *data)
 {
     const struct wined3d_cs_query_issue *op = data;
+    struct wined3d_query *query = op->query;
+
+    query->query_ops->query_issue(query, op->flags);
 
-    op->query->query_ops->query_issue(op->query, op->flags);
+    if (op->flags & WINED3DISSUE_END)
+        InterlockedIncrement(&query->counter_worker);
 
     return sizeof(*op);
 }
diff --git a/dlls/wined3d/query.c b/dlls/wined3d/query.c
index 6436bd7..bea03ed 100644
--- a/dlls/wined3d/query.c
+++ b/dlls/wined3d/query.c
@@ -279,6 +279,12 @@ HRESULT CDECL wined3d_query_get_data(struct wined3d_query *query,
     TRACE("query %p, data %p, data_size %u, flags %#x.\n",
             query, data, data_size, flags);
 
+    if (query->counter_main != query->counter_worker)
+    {
+        TRACE("D3DISSUE_END command not submitted to GL yet\n");
+        return S_FALSE;
+    }
+
     wined3d_cs_emit_query_get_data(query->device->cs, query, data, data_size,
             flags, &hr);
 
@@ -296,6 +302,9 @@ HRESULT CDECL wined3d_query_issue(struct wined3d_query *query, DWORD flags)
 {
     TRACE("query %p, flags %#x.\n", query, flags);
 
+    if (flags & WINED3DISSUE_END)
+        query->counter_main++;
+
     wined3d_cs_emit_query_issue(query->device->cs, query, flags);
     return WINED3D_OK;
 }
diff --git a/dlls/wined3d/wined3d_private.h b/dlls/wined3d/wined3d_private.h
index 6175f13..3fd7fa7 100644
--- a/dlls/wined3d/wined3d_private.h
+++ b/dlls/wined3d/wined3d_private.h
@@ -2671,6 +2671,8 @@ struct wined3d_query
     enum wined3d_query_type type;
     DWORD data_size;
     void                     *extendedData;
+
+    LONG                     counter_main, counter_worker;
 };
 
 /* TODO: Add tests and support for FLOAT16_4 POSITIONT, D3DCOLOR position, other
-- 
2.1.3

