st/nine: Fix volumetexture dtor on ctor failure
[mesa.git] / src / gallium / state_trackers / nine / query9.c
index b74a707e3f311e3ca73059c5198b4cdd3942c5bc..d98db9eba7e247b405da6493b7f3b685d8ff9252 100644 (file)
@@ -21,6 +21,7 @@
  * USE OR OTHER DEALINGS IN THE SOFTWARE. */
 
 #include "device9.h"
+#include "nine_state.h"
 #include "query9.h"
 #include "nine_helpers.h"
 #include "pipe/p_screen.h"
@@ -57,7 +58,7 @@ d3dquerytype_to_pipe_query(struct pipe_screen *screen, D3DQUERYTYPE type)
 
 #define GET_DATA_SIZE_CASE2(a, b) case D3DQUERYTYPE_##a: return sizeof(D3DDEVINFO_##b)
 #define GET_DATA_SIZE_CASET(a, b) case D3DQUERYTYPE_##a: return sizeof(b)
-static INLINE DWORD
+static inline DWORD
 nine_query_result_size(D3DQUERYTYPE type)
 {
     switch (type) {
@@ -93,8 +94,8 @@ NineQuery9_ctor( struct NineQuery9 *This,
                  struct NineUnknownParams *pParams,
                  D3DQUERYTYPE Type )
 {
-    struct pipe_context *pipe = pParams->device->pipe;
-    const unsigned ptype = d3dquerytype_to_pipe_query(pParams->device->screen, Type);
+    struct NineDevice9 *device = pParams->device;
+    const unsigned ptype = d3dquerytype_to_pipe_query(device->screen, Type);
     HRESULT hr;
 
     DBG("This=%p pParams=%p Type=%d\n", This, pParams, Type);
@@ -109,7 +110,7 @@ NineQuery9_ctor( struct NineQuery9 *This,
     user_assert(ptype != ~0, D3DERR_INVALIDCALL);
 
     if (ptype < PIPE_QUERY_TYPES) {
-        This->pq = pipe->create_query(pipe, ptype, 0);
+        This->pq = nine_context_create_query(device, ptype);
         if (!This->pq)
             return E_OUTOFMEMORY;
     } else {
@@ -132,52 +133,60 @@ NineQuery9_ctor( struct NineQuery9 *This,
 void
 NineQuery9_dtor( struct NineQuery9 *This )
 {
-    struct pipe_context *pipe = This->base.device->pipe;
+    struct NineDevice9 *device = This->base.device;
+
+    DBG("This=%p\n", This);
 
     if (This->pq) {
         if (This->state == NINE_QUERY_STATE_RUNNING)
-            pipe->end_query(pipe, This->pq);
-        pipe->destroy_query(pipe, This->pq);
+            nine_context_end_query(device, &This->counter, This->pq);
+        nine_context_destroy_query(device, This->pq);
     }
 
     NineUnknown_dtor(&This->base);
 }
 
-D3DQUERYTYPE WINAPI
+D3DQUERYTYPE NINE_WINAPI
 NineQuery9_GetType( struct NineQuery9 *This )
 {
     return This->type;
 }
 
-DWORD WINAPI
+DWORD NINE_WINAPI
 NineQuery9_GetDataSize( struct NineQuery9 *This )
 {
     return This->result_size;
 }
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineQuery9_Issue( struct NineQuery9 *This,
                   DWORD dwIssueFlags )
 {
-    struct pipe_context *pipe = This->base.device->pipe;
+    struct NineDevice9 *device = This->base.device;
 
     DBG("This=%p dwIssueFlags=%d\n", This, dwIssueFlags);
 
-    user_assert((dwIssueFlags == D3DISSUE_BEGIN && !This->instant) ||
+    user_assert((dwIssueFlags == D3DISSUE_BEGIN) ||
                 (dwIssueFlags == 0) ||
                 (dwIssueFlags == D3DISSUE_END), D3DERR_INVALIDCALL);
 
+    /* Wine tests: always return D3D_OK on D3DISSUE_BEGIN
+     * even when the call is supposed to be forbidden */
+    if (dwIssueFlags == D3DISSUE_BEGIN && This->instant)
+        return D3D_OK;
+
     if (dwIssueFlags == D3DISSUE_BEGIN) {
-        if (This->state == NINE_QUERY_STATE_RUNNING) {
-        pipe->end_query(pipe, This->pq);
-        }
-        pipe->begin_query(pipe, This->pq);
+        if (This->state == NINE_QUERY_STATE_RUNNING)
+            nine_context_end_query(device, &This->counter, This->pq);
+        nine_context_begin_query(device, &This->counter, This->pq);
         This->state = NINE_QUERY_STATE_RUNNING;
     } else {
-        if (This->state == NINE_QUERY_STATE_RUNNING) {
-            pipe->end_query(pipe, This->pq);
-            This->state = NINE_QUERY_STATE_ENDED;
-        }
+        if (This->state != NINE_QUERY_STATE_RUNNING &&
+            This->type != D3DQUERYTYPE_EVENT &&
+            This->type != D3DQUERYTYPE_TIMESTAMP)
+            nine_context_begin_query(device, &This->counter, This->pq);
+        nine_context_end_query(device, &This->counter, This->pq);
+        This->state = NINE_QUERY_STATE_ENDED;
     }
     return D3D_OK;
 }
@@ -190,15 +199,14 @@ union nine_query_result
     UINT64 u64;
 };
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineQuery9_GetData( struct NineQuery9 *This,
                     void *pData,
                     DWORD dwSize,
                     DWORD dwGetDataFlags )
 {
-    struct pipe_context *pipe = This->base.device->pipe;
-    boolean ok;
-    unsigned i;
+    struct NineDevice9 *device = This->base.device;
+    boolean ok, wait_query_result = FALSE;
     union pipe_query_result presult;
     union nine_query_result nresult;
 
@@ -213,13 +221,28 @@ NineQuery9_GetData( struct NineQuery9 *This,
     user_assert(dwGetDataFlags == 0 ||
                 dwGetDataFlags == D3DGETDATA_FLUSH, D3DERR_INVALIDCALL);
 
-    if (This->state == NINE_QUERY_STATE_FRESH)
-        return S_OK;
+    if (This->state == NINE_QUERY_STATE_FRESH) {
+        /* App forgot calling Issue. call it for it.
+         * However Wine states that return value should
+         * be S_OK, so wait for the result to return S_OK. */
+        NineQuery9_Issue(This, D3DISSUE_END);
+        wait_query_result = TRUE;
+    }
+
+    /* The documention mentions no special case for D3DQUERYTYPE_TIMESTAMP.
+     * However Windows tests show that the query always succeeds when
+     * D3DGETDATA_FLUSH is specified. */
+    if (This->type == D3DQUERYTYPE_TIMESTAMP &&
+        (dwGetDataFlags & D3DGETDATA_FLUSH))
+        wait_query_result = TRUE;
+
 
     /* Note: We ignore dwGetDataFlags, because get_query_result will
      * flush automatically if needed */
 
-    ok = pipe->get_query_result(pipe, This->pq, FALSE, &presult);
+    ok = nine_context_get_query_result(device, This->pq, &This->counter,
+                                       !!(dwGetDataFlags & D3DGETDATA_FLUSH),
+                                       wait_query_result, &presult);
 
     if (!ok) return S_FALSE;
 
@@ -240,7 +263,15 @@ NineQuery9_GetData( struct NineQuery9 *This,
         nresult.b = presult.timestamp_disjoint.disjoint;
         break;
     case D3DQUERYTYPE_TIMESTAMPFREQ:
-        nresult.u64 = presult.timestamp_disjoint.frequency;
+        /* Applications use it to convert the TIMESTAMP value to time.
+           AMD drivers on win seem to return the actual hardware clock
+           resolution and corresponding values in TIMESTAMP.
+           However, this behaviour is not easy to replicate here.
+           So instead we do what wine and opengl do, and use
+           nanoseconds TIMESTAMPs.
+           (Which is also the unit used by PIPE_QUERY_TIMESTAMP.)
+        */
+        nresult.u64 = 1000000000;
         break;
     case D3DQUERYTYPE_VERTEXSTATS:
         nresult.vertexstats.NumRenderedTriangles =