* IWineD3DQuery implementation
*
* Copyright 2005 Oliver Stieber
- * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
+ * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
+ * Copyright 2009 Henri Verbeet for CodeWeavers.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
*/
WINE_DEFAULT_DEBUG_CHANNEL(d3d);
-#define GLINFO_LOCATION This->wineD3DDevice->adapter->gl_info
+#define GLINFO_LOCATION This->device->adapter->gl_info
/* *******************************************
IWineD3DQuery IUnknown parts follow
TRACE("(%p) : Releasing from %d\n", This, This->ref);
ref = InterlockedDecrement(&This->ref);
if (ref == 0) {
- ENTER_GL();
- if(This->type == WINED3DQUERYTYPE_EVENT) {
- if(GL_SUPPORT(APPLE_FENCE)) {
- GL_EXTCALL(glDeleteFencesAPPLE(1, &((WineQueryEventData *)(This->extendedData))->fenceId));
- checkGLcall("glDeleteFencesAPPLE");
- } else if(GL_SUPPORT(NV_FENCE)) {
- GL_EXTCALL(glDeleteFencesNV(1, &((WineQueryEventData *)(This->extendedData))->fenceId));
- checkGLcall("glDeleteFencesNV");
- }
- } else if(This->type == WINED3DQUERYTYPE_OCCLUSION && GL_SUPPORT(ARB_OCCLUSION_QUERY)) {
- GL_EXTCALL(glDeleteQueriesARB(1, &((WineQueryOcclusionData *)(This->extendedData))->queryId));
- checkGLcall("glDeleteQueriesARB");
+ /* Queries are specific to the GL context that created them. Not
+ * deleting the query will obviously leak it, but that's still better
+ * than potentially deleting a different query with the same id in this
+ * context, and (still) leaking the actual query. */
+ if (This->type == WINED3DQUERYTYPE_EVENT)
+ {
+ struct wined3d_event_query *query = This->extendedData;
+
+ if (query->context) context_free_event_query(query);
+ }
+ else if (This->type == WINED3DQUERYTYPE_OCCLUSION)
+ {
+ struct wined3d_occlusion_query *query = This->extendedData;
+
+ if (query->context) context_free_occlusion_query(query);
}
- LEAVE_GL();
HeapFree(GetProcessHeap(), 0, This->extendedData);
HeapFree(GetProcessHeap(), 0, This);
/* *******************************************
IWineD3DQuery IWineD3DQuery parts follow
******************************************* */
-static HRESULT WINAPI IWineD3DQueryImpl_GetParent(IWineD3DQuery *iface, IUnknown** parent){
- IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
+static HRESULT WINAPI IWineD3DQueryImpl_GetParent(IWineD3DQuery *iface, IUnknown **parent)
+{
+ TRACE("iface %p, parent %p.\n", iface, parent);
- *parent= (IUnknown*) parent;
+ *parent = (IUnknown *)parent;
IUnknown_AddRef(*parent);
- TRACE("(%p) : returning %p\n", This, *parent);
- return WINED3D_OK;
-}
-static HRESULT WINAPI IWineD3DQueryImpl_GetDevice(IWineD3DQuery* iface, IWineD3DDevice **pDevice){
- IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
- IWineD3DDevice_AddRef((IWineD3DDevice *)This->wineD3DDevice);
- *pDevice = (IWineD3DDevice *)This->wineD3DDevice;
- TRACE("(%p) returning %p\n", This, *pDevice);
+ TRACE("Returning %p.\n", *parent);
+
return WINED3D_OK;
}
-
static HRESULT WINAPI IWineD3DQueryImpl_GetData(IWineD3DQuery* iface, void* pData, DWORD dwSize, DWORD dwGetDataFlags){
IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
HRESULT res = S_OK;
case WINED3DQUERYTYPE_VCACHE:
{
- WINED3DDEVINFO_VCACHE *data = (WINED3DDEVINFO_VCACHE *)pData;
+ WINED3DDEVINFO_VCACHE *data = pData;
FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_VCACHE\n", This);
if(pData == NULL || dwSize == 0) break;
data->Pattern = WINEMAKEFOURCC('C','A','C','H');
break;
case WINED3DQUERYTYPE_RESOURCEMANAGER:
{
- WINED3DDEVINFO_RESOURCEMANAGER *data = (WINED3DDEVINFO_RESOURCEMANAGER *)pData;
+ WINED3DDEVINFO_RESOURCEMANAGER *data = pData;
int i;
FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_RESOURCEMANAGER\n", This);
if(pData == NULL || dwSize == 0) break;
break;
case WINED3DQUERYTYPE_VERTEXSTATS:
{
- WINED3DDEVINFO_VERTEXSTATS *data = (WINED3DDEVINFO_VERTEXSTATS *)pData;
+ WINED3DDEVINFO_VERTEXSTATS *data = pData;
FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_VERTEXSTATS\n", This);
if(pData == NULL || dwSize == 0) break;
data->NumRenderedTriangles = 1;
break;
case WINED3DQUERYTYPE_PIPELINETIMINGS:
{
- WINED3DDEVINFO_PIPELINETIMINGS *data = (WINED3DDEVINFO_PIPELINETIMINGS *)pData;
+ WINED3DDEVINFO_PIPELINETIMINGS *data = pData;
FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_PIPELINETIMINGS\n", This);
if(pData == NULL || dwSize == 0) break;
break;
case WINED3DQUERYTYPE_INTERFACETIMINGS:
{
- WINED3DDEVINFO_INTERFACETIMINGS *data = (WINED3DDEVINFO_INTERFACETIMINGS *)pData;
+ WINED3DDEVINFO_INTERFACETIMINGS *data = pData;
FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_INTERFACETIMINGS\n", This);
if(pData == NULL || dwSize == 0) break;
break;
case WINED3DQUERYTYPE_VERTEXTIMINGS:
{
- WINED3DDEVINFO_STAGETIMINGS *data = (WINED3DDEVINFO_STAGETIMINGS *)pData;
+ WINED3DDEVINFO_STAGETIMINGS *data = pData;
FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_VERTEXTIMINGS\n", This);
if(pData == NULL || dwSize == 0) break;
break;
case WINED3DQUERYTYPE_PIXELTIMINGS:
{
- WINED3DDEVINFO_STAGETIMINGS *data = (WINED3DDEVINFO_STAGETIMINGS *)pData;
+ WINED3DDEVINFO_STAGETIMINGS *data = pData;
FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_PIXELTIMINGS\n", This);
if(pData == NULL || dwSize == 0) break;
break;
case WINED3DQUERYTYPE_BANDWIDTHTIMINGS:
{
- WINED3DDEVINFO_BANDWIDTHTIMINGS *data = (WINED3DDEVINFO_BANDWIDTHTIMINGS *)pData;
+ WINED3DDEVINFO_BANDWIDTHTIMINGS *data = pData;
FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_BANDWIDTHTIMINGS\n", This);
if(pData == NULL || dwSize == 0) break;
break;
case WINED3DQUERYTYPE_CACHEUTILIZATION:
{
- WINED3DDEVINFO_CACHEUTILIZATION *data = (WINED3DDEVINFO_CACHEUTILIZATION *)pData;
+ WINED3DDEVINFO_CACHEUTILIZATION *data = pData;
FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_CACHEUTILIZATION\n", This);
if(pData == NULL || dwSize == 0) break;
static HRESULT WINAPI IWineD3DOcclusionQueryImpl_GetData(IWineD3DQuery* iface, void* pData, DWORD dwSize, DWORD dwGetDataFlags) {
IWineD3DQueryImpl *This = (IWineD3DQueryImpl *) iface;
+ struct wined3d_occlusion_query *query = This->extendedData;
+ IWineD3DDeviceImpl *device = This->device;
+ const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
+ struct wined3d_context *context;
DWORD* data = pData;
+ GLuint available;
+ GLuint samples;
HRESULT res;
+
TRACE("(%p) : type D3DQUERY_OCCLUSION, pData %p, dwSize %#x, dwGetDataFlags %#x\n", This, pData, dwSize, dwGetDataFlags);
- if(This->state == QUERY_CREATED) {
+ if (!query->context) This->state = QUERY_CREATED;
+
+ if (This->state == QUERY_CREATED)
+ {
/* D3D allows GetData on a new query, OpenGL doesn't. So just invent the data ourselves */
TRACE("Query wasn't yet started, returning S_OK\n");
- res = S_OK;
if(data) *data = 0;
- } else if(This->state == QUERY_BUILDING) {
+ return S_OK;
+ }
+
+ if (This->state == QUERY_BUILDING)
+ {
/* Msdn says this returns an error, but our tests show that S_FALSE is returned */
TRACE("Query is building, returning S_FALSE\n");
- res = S_FALSE;
- } else if (GL_SUPPORT(ARB_OCCLUSION_QUERY) &&
- ((WineQueryOcclusionData *)This->extendedData)->ctx == This->wineD3DDevice->activeContext &&
- This->wineD3DDevice->activeContext->tid == GetCurrentThreadId()) {
- GLuint available;
- GLuint samples;
- GLuint queryId = ((WineQueryOcclusionData *)This->extendedData)->queryId;
+ return S_FALSE;
+ }
- ENTER_GL();
- GL_EXTCALL(glGetQueryObjectuivARB(queryId, GL_QUERY_RESULT_AVAILABLE_ARB, &available));
- checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT_AVAILABLE)\n");
- TRACE("(%p) : available %d.\n", This, available);
-
- if (available) {
- if(data) {
- GL_EXTCALL(glGetQueryObjectuivARB(queryId, GL_QUERY_RESULT_ARB, &samples));
- checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT)\n");
- TRACE("(%p) : Returning %d samples.\n", This, samples);
- *data = samples;
- }
- res = S_OK;
- } else {
- res = S_FALSE;
- }
- LEAVE_GL();
- } else {
- WARN("(%p) : Occlusion queries not supported, or wrong context. Returning 1.\n", This);
+ if (!gl_info->supported[ARB_OCCLUSION_QUERY])
+ {
+ WARN("(%p) : Occlusion queries not supported. Returning 1.\n", This);
*data = 1;
+ return S_OK;
+ }
+
+ if (query->context->tid != GetCurrentThreadId())
+ {
+ FIXME("%p Wrong thread, returning 1.\n", This);
+ *data = 1;
+ return S_OK;
+ }
+
+ context = context_acquire(This->device, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
+
+ ENTER_GL();
+
+ GL_EXTCALL(glGetQueryObjectuivARB(query->id, GL_QUERY_RESULT_AVAILABLE_ARB, &available));
+ checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT_AVAILABLE)");
+ TRACE("(%p) : available %d.\n", This, available);
+
+ if (available)
+ {
+ if (data)
+ {
+ GL_EXTCALL(glGetQueryObjectuivARB(query->id, GL_QUERY_RESULT_ARB, &samples));
+ checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT)");
+ TRACE("(%p) : Returning %d samples.\n", This, samples);
+ *data = samples;
+ }
res = S_OK;
}
+ else
+ {
+ res = S_FALSE;
+ }
+
+ LEAVE_GL();
+
+ context_release(context);
+
return res;
}
static HRESULT WINAPI IWineD3DEventQueryImpl_GetData(IWineD3DQuery* iface, void* pData, DWORD dwSize, DWORD dwGetDataFlags) {
IWineD3DQueryImpl *This = (IWineD3DQueryImpl *) iface;
- BOOL* data = pData;
- WineD3DContext *ctx;
+ struct wined3d_event_query *query = This->extendedData;
+ struct wined3d_context *context;
+ BOOL *data = pData;
+
TRACE("(%p) : type D3DQUERY_EVENT, pData %p, dwSize %#x, dwGetDataFlags %#x\n", This, pData, dwSize, dwGetDataFlags);
- ctx = ((WineQueryEventData *)This->extendedData)->ctx;
- if(pData == NULL || dwSize == 0) {
+ if (!pData || !dwSize) return S_OK;
+
+ if (!query->context)
+ {
+ TRACE("Query not started, returning TRUE.\n");
+ *data = TRUE;
+
return S_OK;
- } if(ctx != This->wineD3DDevice->activeContext || ctx->tid != GetCurrentThreadId()) {
+ }
+
+ if (query->context->tid != GetCurrentThreadId())
+ {
/* See comment in IWineD3DQuery::Issue, event query codeblock */
- WARN("Query context not active, reporting GPU idle\n");
+ FIXME("Wrong thread, reporting GPU idle.\n");
*data = TRUE;
- } else if(GL_SUPPORT(APPLE_FENCE)) {
- ENTER_GL();
- *data = GL_EXTCALL(glTestFenceAPPLE(((WineQueryEventData *)This->extendedData)->fenceId));
+
+ return S_OK;
+ }
+
+ context = context_acquire(This->device, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
+
+ ENTER_GL();
+
+ if (context->gl_info->supported[APPLE_FENCE])
+ {
+ *data = GL_EXTCALL(glTestFenceAPPLE(query->id));
checkGLcall("glTestFenceAPPLE");
- LEAVE_GL();
- } else if(GL_SUPPORT(NV_FENCE)) {
- ENTER_GL();
- *data = GL_EXTCALL(glTestFenceNV(((WineQueryEventData *)This->extendedData)->fenceId));
+ }
+ else if (context->gl_info->supported[NV_FENCE])
+ {
+ *data = GL_EXTCALL(glTestFenceNV(query->id));
checkGLcall("glTestFenceNV");
- LEAVE_GL();
- } else {
+ }
+ else
+ {
WARN("(%p): reporting GPU idle\n", This);
*data = TRUE;
}
+ LEAVE_GL();
+
+ context_release(context);
+
return S_OK;
}
IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
TRACE("(%p) : dwIssueFlags %#x, type D3DQUERY_EVENT\n", This, dwIssueFlags);
- if (dwIssueFlags & WINED3DISSUE_END) {
- WineD3DContext *ctx = ((WineQueryEventData *)This->extendedData)->ctx;
- if(ctx != This->wineD3DDevice->activeContext || ctx->tid != GetCurrentThreadId()) {
- /* GL fences can be used only from the context that created them,
- * so if a different context is active, don't bother setting the query. The penalty
- * of a context switch is most likely higher than the gain of a correct query result
- *
- * If the query is used from a different thread, don't bother creating a multithread
- * context - there's no point in doing that as the query would be unusable anyway
- */
- WARN("Query context not active\n");
- } else if(GL_SUPPORT(APPLE_FENCE)) {
- ENTER_GL();
- GL_EXTCALL(glSetFenceAPPLE(((WineQueryEventData *)This->extendedData)->fenceId));
+ if (dwIssueFlags & WINED3DISSUE_END)
+ {
+ struct wined3d_event_query *query = This->extendedData;
+ struct wined3d_context *context;
+
+ if (query->context)
+ {
+ if (query->context->tid != GetCurrentThreadId())
+ {
+ context_free_event_query(query);
+ context = context_acquire(This->device, NULL, CTXUSAGE_RESOURCELOAD);
+ context_alloc_event_query(context, query);
+ }
+ else
+ {
+ context = context_acquire(This->device, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
+ }
+ }
+ else
+ {
+ context = context_acquire(This->device, NULL, CTXUSAGE_RESOURCELOAD);
+ context_alloc_event_query(context, query);
+ }
+
+ ENTER_GL();
+
+ if (context->gl_info->supported[APPLE_FENCE])
+ {
+ GL_EXTCALL(glSetFenceAPPLE(query->id));
checkGLcall("glSetFenceAPPLE");
- LEAVE_GL();
- } else if (GL_SUPPORT(NV_FENCE)) {
- ENTER_GL();
- GL_EXTCALL(glSetFenceNV(((WineQueryEventData *)This->extendedData)->fenceId, GL_ALL_COMPLETED_NV));
+ }
+ else if (context->gl_info->supported[NV_FENCE])
+ {
+ GL_EXTCALL(glSetFenceNV(query->id, GL_ALL_COMPLETED_NV));
checkGLcall("glSetFenceNV");
- LEAVE_GL();
}
- } else if(dwIssueFlags & WINED3DISSUE_BEGIN) {
+
+ LEAVE_GL();
+
+ context_release(context);
+ }
+ else if(dwIssueFlags & WINED3DISSUE_BEGIN)
+ {
/* Started implicitly at device creation */
ERR("Event query issued with START flag - what to do?\n");
}
static HRESULT WINAPI IWineD3DOcclusionQueryImpl_Issue(IWineD3DQuery* iface, DWORD dwIssueFlags) {
IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
+ IWineD3DDeviceImpl *device = This->device;
+ const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
- if (GL_SUPPORT(ARB_OCCLUSION_QUERY)) {
- WineD3DContext *ctx = ((WineQueryOcclusionData *)This->extendedData)->ctx;
+ if (gl_info->supported[ARB_OCCLUSION_QUERY])
+ {
+ struct wined3d_occlusion_query *query = This->extendedData;
+ struct wined3d_context *context;
+
+ /* This is allowed according to msdn and our tests. Reset the query and restart */
+ if (dwIssueFlags & WINED3DISSUE_BEGIN)
+ {
+ if (This->state == QUERY_BUILDING)
+ {
+ if (query->context->tid != GetCurrentThreadId())
+ {
+ FIXME("Wrong thread, can't restart query.\n");
+
+ context_free_occlusion_query(query);
+ context = context_acquire(This->device, NULL, CTXUSAGE_RESOURCELOAD);
+ context_alloc_occlusion_query(context, query);
+ }
+ else
+ {
+ context = context_acquire(This->device, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
- if(ctx != This->wineD3DDevice->activeContext || ctx->tid != GetCurrentThreadId()) {
- WARN("Not the owning context, can't start query\n");
- } else {
- ENTER_GL();
- /* This is allowed according to msdn and our tests. Reset the query and restart */
- if (dwIssueFlags & WINED3DISSUE_BEGIN) {
- if(This->state == QUERY_BUILDING) {
+ ENTER_GL();
GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB));
checkGLcall("glEndQuery()");
+ LEAVE_GL();
}
-
- GL_EXTCALL(glBeginQueryARB(GL_SAMPLES_PASSED_ARB, ((WineQueryOcclusionData *)This->extendedData)->queryId));
- checkGLcall("glBeginQuery()");
}
- if (dwIssueFlags & WINED3DISSUE_END) {
- /* Msdn says _END on a non-building occlusion query returns an error, but
- * our tests show that it returns OK. But OpenGL doesn't like it, so avoid
- * generating an error
- */
- if(This->state == QUERY_BUILDING) {
+ else
+ {
+ if (query->context) context_free_occlusion_query(query);
+ context = context_acquire(This->device, NULL, CTXUSAGE_RESOURCELOAD);
+ context_alloc_occlusion_query(context, query);
+ }
+
+ ENTER_GL();
+ GL_EXTCALL(glBeginQueryARB(GL_SAMPLES_PASSED_ARB, query->id));
+ checkGLcall("glBeginQuery()");
+ LEAVE_GL();
+
+ context_release(context);
+ }
+ if (dwIssueFlags & WINED3DISSUE_END) {
+ /* Msdn says _END on a non-building occlusion query returns an error, but
+ * our tests show that it returns OK. But OpenGL doesn't like it, so avoid
+ * generating an error
+ */
+ if (This->state == QUERY_BUILDING)
+ {
+ if (query->context->tid != GetCurrentThreadId())
+ {
+ FIXME("Wrong thread, can't end query.\n");
+ }
+ else
+ {
+ context = context_acquire(This->device, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
+
+ ENTER_GL();
GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB));
checkGLcall("glEndQuery()");
+ LEAVE_GL();
+
+ context_release(context);
}
}
- LEAVE_GL();
}
} else {
FIXME("(%p) : Occlusion queries not supported\n", This);
IWineD3DQueryImpl_Release,
/*** IWineD3Dquery methods ***/
IWineD3DQueryImpl_GetParent,
- IWineD3DQueryImpl_GetDevice,
IWineD3DQueryImpl_GetData,
IWineD3DQueryImpl_GetDataSize,
IWineD3DQueryImpl_GetType,
IWineD3DQueryImpl_Release,
/*** IWineD3Dquery methods ***/
IWineD3DQueryImpl_GetParent,
- IWineD3DQueryImpl_GetDevice,
IWineD3DEventQueryImpl_GetData,
IWineD3DEventQueryImpl_GetDataSize,
IWineD3DQueryImpl_GetType,
IWineD3DQueryImpl_Release,
/*** IWineD3Dquery methods ***/
IWineD3DQueryImpl_GetParent,
- IWineD3DQueryImpl_GetDevice,
IWineD3DOcclusionQueryImpl_GetData,
IWineD3DOcclusionQueryImpl_GetDataSize,
IWineD3DQueryImpl_GetType,