2 * IWineD3DQuery implementation
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wined3d_private.h"
28 * http://www.gris.uni-tuebingen.de/~bartz/Publications/paper/hww98.pdf
29 * http://oss.sgi.com/projects/ogl-sample/registry/ARB/occlusion_query.txt
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
33 #define GLINFO_LOCATION This->wineD3DDevice->adapter->gl_info
35 /* *******************************************
36 IWineD3DQuery IUnknown parts follow
37 ******************************************* */
38 static HRESULT WINAPI IWineD3DQueryImpl_QueryInterface(IWineD3DQuery *iface, REFIID riid, LPVOID *ppobj)
40 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
41 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
42 if (IsEqualGUID(riid, &IID_IUnknown)
43 || IsEqualGUID(riid, &IID_IWineD3DBase)
44 || IsEqualGUID(riid, &IID_IWineD3DQuery)) {
45 IUnknown_AddRef(iface);
53 static ULONG WINAPI IWineD3DQueryImpl_AddRef(IWineD3DQuery *iface) {
54 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
55 TRACE("(%p) : AddRef increasing from %d\n", This, This->ref);
56 return InterlockedIncrement(&This->ref);
59 static ULONG WINAPI IWineD3DQueryImpl_Release(IWineD3DQuery *iface) {
60 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
62 TRACE("(%p) : Releasing from %d\n", This, This->ref);
63 ref = InterlockedDecrement(&This->ref);
65 /* Queries are specific to the GL context that created them. Not
66 * deleting the query will obviously leak it, but that's still better
67 * than potentially deleting a different query with the same id in this
68 * context, and (still) leaking the actual query. */
69 if (This->type == WINED3DQUERYTYPE_EVENT)
71 struct wined3d_event_query *query = This->extendedData;
73 if (query->context) context_free_event_query(query);
75 else if (This->type == WINED3DQUERYTYPE_OCCLUSION)
77 struct wined3d_occlusion_query *query = This->extendedData;
79 if (query->context) context_free_occlusion_query(query);
82 HeapFree(GetProcessHeap(), 0, This->extendedData);
83 HeapFree(GetProcessHeap(), 0, This);
88 /* *******************************************
89 IWineD3DQuery IWineD3DQuery parts follow
90 ******************************************* */
91 static HRESULT WINAPI IWineD3DQueryImpl_GetParent(IWineD3DQuery *iface, IUnknown** parent){
92 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
94 *parent= (IUnknown*) parent;
95 IUnknown_AddRef(*parent);
96 TRACE("(%p) : returning %p\n", This, *parent);
100 static HRESULT WINAPI IWineD3DQueryImpl_GetDevice(IWineD3DQuery* iface, IWineD3DDevice **pDevice){
101 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
102 IWineD3DDevice_AddRef((IWineD3DDevice *)This->wineD3DDevice);
103 *pDevice = (IWineD3DDevice *)This->wineD3DDevice;
104 TRACE("(%p) returning %p\n", This, *pDevice);
109 static HRESULT WINAPI IWineD3DQueryImpl_GetData(IWineD3DQuery* iface, void* pData, DWORD dwSize, DWORD dwGetDataFlags){
110 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
113 TRACE("(%p) : type %#x, pData %p, dwSize %#x, dwGetDataFlags %#x\n", This, This->type, pData, dwSize, dwGetDataFlags);
117 case WINED3DQUERYTYPE_VCACHE:
120 WINED3DDEVINFO_VCACHE *data = pData;
121 FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_VCACHE\n", This);
122 if(pData == NULL || dwSize == 0) break;
123 data->Pattern = WINEMAKEFOURCC('C','A','C','H');
124 data->OptMethod = 0; /*0 get longest strips, 1 optimize vertex cache*/
125 data->CacheSize = 0; /*cache size, only required if OptMethod == 1*/
126 data->MagicNumber = 0; /*only required if OptMethod == 1 (used internally)*/
130 case WINED3DQUERYTYPE_RESOURCEMANAGER:
132 WINED3DDEVINFO_RESOURCEMANAGER *data = pData;
134 FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_RESOURCEMANAGER\n", This);
135 if(pData == NULL || dwSize == 0) break;
136 for(i = 0; i < WINED3DRTYPECOUNT; i++){
137 /*I'm setting the default values to 1 so as to reduce the risk of a div/0 in the caller*/
138 /* isTextureResident could be used to get some of this information */
139 data->stats[i].bThrashing = FALSE;
140 data->stats[i].ApproxBytesDownloaded = 1;
141 data->stats[i].NumEvicts = 1;
142 data->stats[i].NumVidCreates = 1;
143 data->stats[i].LastPri = 1;
144 data->stats[i].NumUsed = 1;
145 data->stats[i].NumUsedInVidMem = 1;
146 data->stats[i].WorkingSet = 1;
147 data->stats[i].WorkingSetBytes = 1;
148 data->stats[i].TotalManaged = 1;
149 data->stats[i].TotalBytes = 1;
154 case WINED3DQUERYTYPE_VERTEXSTATS:
156 WINED3DDEVINFO_VERTEXSTATS *data = pData;
157 FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_VERTEXSTATS\n", This);
158 if(pData == NULL || dwSize == 0) break;
159 data->NumRenderedTriangles = 1;
160 data->NumExtraClippingTriangles = 1;
164 case WINED3DQUERYTYPE_TIMESTAMP:
166 UINT64* data = pData;
167 FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_TIMESTAMP\n", This);
168 if(pData == NULL || dwSize == 0) break;
169 *data = 1; /*Don't know what this is supposed to be*/
172 case WINED3DQUERYTYPE_TIMESTAMPDISJOINT:
175 FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_TIMESTAMPDISJOINT\n", This);
176 if(pData == NULL || dwSize == 0) break;
177 *data = FALSE; /*Don't know what this is supposed to be*/
180 case WINED3DQUERYTYPE_TIMESTAMPFREQ:
182 UINT64* data = pData;
183 FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_TIMESTAMPFREQ\n", This);
184 if(pData == NULL || dwSize == 0) break;
185 *data = 1; /*Don't know what this is supposed to be*/
188 case WINED3DQUERYTYPE_PIPELINETIMINGS:
190 WINED3DDEVINFO_PIPELINETIMINGS *data = pData;
191 FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_PIPELINETIMINGS\n", This);
192 if(pData == NULL || dwSize == 0) break;
194 data->VertexProcessingTimePercent = 1.0f;
195 data->PixelProcessingTimePercent = 1.0f;
196 data->OtherGPUProcessingTimePercent = 97.0f;
197 data->GPUIdleTimePercent = 1.0f;
200 case WINED3DQUERYTYPE_INTERFACETIMINGS:
202 WINED3DDEVINFO_INTERFACETIMINGS *data = pData;
203 FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_INTERFACETIMINGS\n", This);
205 if(pData == NULL || dwSize == 0) break;
206 data->WaitingForGPUToUseApplicationResourceTimePercent = 1.0f;
207 data->WaitingForGPUToAcceptMoreCommandsTimePercent = 1.0f;
208 data->WaitingForGPUToStayWithinLatencyTimePercent = 1.0f;
209 data->WaitingForGPUExclusiveResourceTimePercent = 1.0f;
210 data->WaitingForGPUOtherTimePercent = 96.0f;
214 case WINED3DQUERYTYPE_VERTEXTIMINGS:
216 WINED3DDEVINFO_STAGETIMINGS *data = pData;
217 FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_VERTEXTIMINGS\n", This);
219 if(pData == NULL || dwSize == 0) break;
220 data->MemoryProcessingPercent = 50.0f;
221 data->ComputationProcessingPercent = 50.0f;
225 case WINED3DQUERYTYPE_PIXELTIMINGS:
227 WINED3DDEVINFO_STAGETIMINGS *data = pData;
228 FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_PIXELTIMINGS\n", This);
230 if(pData == NULL || dwSize == 0) break;
231 data->MemoryProcessingPercent = 50.0f;
232 data->ComputationProcessingPercent = 50.0f;
235 case WINED3DQUERYTYPE_BANDWIDTHTIMINGS:
237 WINED3DDEVINFO_BANDWIDTHTIMINGS *data = pData;
238 FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_BANDWIDTHTIMINGS\n", This);
240 if(pData == NULL || dwSize == 0) break;
241 data->MaxBandwidthUtilized = 1.0f;
242 data->FrontEndUploadMemoryUtilizedPercent = 1.0f;
243 data->VertexRateUtilizedPercent = 1.0f;
244 data->TriangleSetupRateUtilizedPercent = 1.0f;
245 data->FillRateUtilizedPercent = 97.0f;
248 case WINED3DQUERYTYPE_CACHEUTILIZATION:
250 WINED3DDEVINFO_CACHEUTILIZATION *data = pData;
251 FIXME("(%p): Unimplemented query WINED3DQUERYTYPE_CACHEUTILIZATION\n", This);
253 if(pData == NULL || dwSize == 0) break;
254 data->TextureCacheHitRate = 1.0f;
255 data->PostTransformVertexCacheHitRate = 1.0f;
261 FIXME("(%p) Unhandled query type %d\n",This , This->type);
265 /*dwGetDataFlags = 0 || D3DGETDATA_FLUSH
266 D3DGETDATA_FLUSH may return WINED3DERR_DEVICELOST if the device is lost
268 return res; /* S_OK if the query data is available*/
271 static HRESULT WINAPI IWineD3DOcclusionQueryImpl_GetData(IWineD3DQuery* iface, void* pData, DWORD dwSize, DWORD dwGetDataFlags) {
272 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *) iface;
273 struct wined3d_occlusion_query *query = This->extendedData;
279 TRACE("(%p) : type D3DQUERY_OCCLUSION, pData %p, dwSize %#x, dwGetDataFlags %#x\n", This, pData, dwSize, dwGetDataFlags);
281 if (!query->context) This->state = QUERY_CREATED;
283 if (This->state == QUERY_CREATED)
285 /* D3D allows GetData on a new query, OpenGL doesn't. So just invent the data ourselves */
286 TRACE("Query wasn't yet started, returning S_OK\n");
291 if (This->state == QUERY_BUILDING)
293 /* Msdn says this returns an error, but our tests show that S_FALSE is returned */
294 TRACE("Query is building, returning S_FALSE\n");
298 if (!GL_SUPPORT(ARB_OCCLUSION_QUERY))
300 WARN("(%p) : Occlusion queries not supported. Returning 1.\n", This);
305 if (query->context->tid != GetCurrentThreadId())
307 FIXME("%p Wrong thread, returning 1.\n", This);
312 ActivateContext(This->wineD3DDevice, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
316 GL_EXTCALL(glGetQueryObjectuivARB(query->id, GL_QUERY_RESULT_AVAILABLE_ARB, &available));
317 checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT_AVAILABLE)");
318 TRACE("(%p) : available %d.\n", This, available);
324 GL_EXTCALL(glGetQueryObjectuivARB(query->id, GL_QUERY_RESULT_ARB, &samples));
325 checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT)");
326 TRACE("(%p) : Returning %d samples.\n", This, samples);
341 static HRESULT WINAPI IWineD3DEventQueryImpl_GetData(IWineD3DQuery* iface, void* pData, DWORD dwSize, DWORD dwGetDataFlags) {
342 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *) iface;
343 struct wined3d_event_query *query = This->extendedData;
346 TRACE("(%p) : type D3DQUERY_EVENT, pData %p, dwSize %#x, dwGetDataFlags %#x\n", This, pData, dwSize, dwGetDataFlags);
348 if (!pData || !dwSize) return S_OK;
352 ERR("Query not started, returning TRUE.\n");
358 if (query->context->tid != GetCurrentThreadId())
360 /* See comment in IWineD3DQuery::Issue, event query codeblock */
361 FIXME("Wrong thread, reporting GPU idle.\n");
367 ActivateContext(This->wineD3DDevice, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
371 if (GL_SUPPORT(APPLE_FENCE))
373 *data = GL_EXTCALL(glTestFenceAPPLE(query->id));
374 checkGLcall("glTestFenceAPPLE");
376 else if (GL_SUPPORT(NV_FENCE))
378 *data = GL_EXTCALL(glTestFenceNV(query->id));
379 checkGLcall("glTestFenceNV");
383 WARN("(%p): reporting GPU idle\n", This);
392 static DWORD WINAPI IWineD3DQueryImpl_GetDataSize(IWineD3DQuery* iface){
393 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
395 TRACE("(%p) : type %#x\n", This, This->type);
397 case WINED3DQUERYTYPE_VCACHE:
398 dataSize = sizeof(WINED3DDEVINFO_VCACHE);
400 case WINED3DQUERYTYPE_RESOURCEMANAGER:
401 dataSize = sizeof(WINED3DDEVINFO_RESOURCEMANAGER);
403 case WINED3DQUERYTYPE_VERTEXSTATS:
404 dataSize = sizeof(WINED3DDEVINFO_VERTEXSTATS);
406 case WINED3DQUERYTYPE_EVENT:
407 dataSize = sizeof(BOOL);
409 case WINED3DQUERYTYPE_TIMESTAMP:
410 dataSize = sizeof(UINT64);
412 case WINED3DQUERYTYPE_TIMESTAMPDISJOINT:
413 dataSize = sizeof(BOOL);
415 case WINED3DQUERYTYPE_TIMESTAMPFREQ:
416 dataSize = sizeof(UINT64);
418 case WINED3DQUERYTYPE_PIPELINETIMINGS:
419 dataSize = sizeof(WINED3DDEVINFO_PIPELINETIMINGS);
421 case WINED3DQUERYTYPE_INTERFACETIMINGS:
422 dataSize = sizeof(WINED3DDEVINFO_INTERFACETIMINGS);
424 case WINED3DQUERYTYPE_VERTEXTIMINGS:
425 dataSize = sizeof(WINED3DDEVINFO_STAGETIMINGS);
427 case WINED3DQUERYTYPE_PIXELTIMINGS:
428 dataSize = sizeof(WINED3DDEVINFO_STAGETIMINGS);
430 case WINED3DQUERYTYPE_BANDWIDTHTIMINGS:
431 dataSize = sizeof(WINED3DQUERYTYPE_BANDWIDTHTIMINGS);
433 case WINED3DQUERYTYPE_CACHEUTILIZATION:
434 dataSize = sizeof(WINED3DDEVINFO_CACHEUTILIZATION);
437 FIXME("(%p) Unhandled query type %d\n",This , This->type);
443 static DWORD WINAPI IWineD3DEventQueryImpl_GetDataSize(IWineD3DQuery* iface){
444 TRACE("(%p) : type D3DQUERY_EVENT\n", iface);
449 static DWORD WINAPI IWineD3DOcclusionQueryImpl_GetDataSize(IWineD3DQuery* iface){
450 TRACE("(%p) : type D3DQUERY_OCCLUSION\n", iface);
452 return sizeof(DWORD);
455 static WINED3DQUERYTYPE WINAPI IWineD3DQueryImpl_GetType(IWineD3DQuery* iface){
456 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
461 static HRESULT WINAPI IWineD3DEventQueryImpl_Issue(IWineD3DQuery* iface, DWORD dwIssueFlags) {
462 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
464 TRACE("(%p) : dwIssueFlags %#x, type D3DQUERY_EVENT\n", This, dwIssueFlags);
465 if (dwIssueFlags & WINED3DISSUE_END)
467 struct wined3d_event_query *query = This->extendedData;
468 struct wined3d_context *context;
472 if (query->context->tid != GetCurrentThreadId())
474 context_free_event_query(query);
475 context = ActivateContext(This->wineD3DDevice, NULL, CTXUSAGE_RESOURCELOAD);
476 context_alloc_event_query(context, query);
480 ActivateContext(This->wineD3DDevice, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
485 context = ActivateContext(This->wineD3DDevice, NULL, CTXUSAGE_RESOURCELOAD);
486 context_alloc_event_query(context, query);
491 if (GL_SUPPORT(APPLE_FENCE))
493 GL_EXTCALL(glSetFenceAPPLE(query->id));
494 checkGLcall("glSetFenceAPPLE");
496 else if (GL_SUPPORT(NV_FENCE))
498 GL_EXTCALL(glSetFenceNV(query->id, GL_ALL_COMPLETED_NV));
499 checkGLcall("glSetFenceNV");
504 else if(dwIssueFlags & WINED3DISSUE_BEGIN)
506 /* Started implicitly at device creation */
507 ERR("Event query issued with START flag - what to do?\n");
510 if(dwIssueFlags & WINED3DISSUE_BEGIN) {
511 This->state = QUERY_BUILDING;
513 This->state = QUERY_SIGNALLED;
519 static HRESULT WINAPI IWineD3DOcclusionQueryImpl_Issue(IWineD3DQuery* iface, DWORD dwIssueFlags) {
520 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
522 if (GL_SUPPORT(ARB_OCCLUSION_QUERY))
524 struct wined3d_occlusion_query *query = This->extendedData;
525 struct wined3d_context *context;
527 /* This is allowed according to msdn and our tests. Reset the query and restart */
528 if (dwIssueFlags & WINED3DISSUE_BEGIN)
530 if (This->state == QUERY_BUILDING)
532 if (query->context->tid != GetCurrentThreadId())
534 FIXME("Wrong thread, can't restart query.\n");
536 context_free_occlusion_query(query);
537 context = ActivateContext(This->wineD3DDevice, NULL, CTXUSAGE_RESOURCELOAD);
538 context_alloc_occlusion_query(context, query);
542 ActivateContext(This->wineD3DDevice, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
545 GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB));
546 checkGLcall("glEndQuery()");
552 if (query->context) context_free_occlusion_query(query);
553 context = ActivateContext(This->wineD3DDevice, NULL, CTXUSAGE_RESOURCELOAD);
554 context_alloc_occlusion_query(context, query);
558 GL_EXTCALL(glBeginQueryARB(GL_SAMPLES_PASSED_ARB, query->id));
559 checkGLcall("glBeginQuery()");
562 if (dwIssueFlags & WINED3DISSUE_END) {
563 /* Msdn says _END on a non-building occlusion query returns an error, but
564 * our tests show that it returns OK. But OpenGL doesn't like it, so avoid
565 * generating an error
567 if (This->state == QUERY_BUILDING)
569 if (query->context->tid != GetCurrentThreadId())
571 FIXME("Wrong thread, can't end query.\n");
575 ActivateContext(This->wineD3DDevice, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
578 GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB));
579 checkGLcall("glEndQuery()");
585 FIXME("(%p) : Occlusion queries not supported\n", This);
588 if(dwIssueFlags & WINED3DISSUE_BEGIN) {
589 This->state = QUERY_BUILDING;
591 This->state = QUERY_SIGNALLED;
593 return WINED3D_OK; /* can be WINED3DERR_INVALIDCALL. */
596 static HRESULT WINAPI IWineD3DQueryImpl_Issue(IWineD3DQuery* iface, DWORD dwIssueFlags){
597 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
599 TRACE("(%p) : dwIssueFlags %#x, type %#x\n", This, dwIssueFlags, This->type);
601 /* The fixme is printed when the app asks for the resulting data */
602 WARN("(%p) : Unhandled query type %#x\n", This, This->type);
604 if(dwIssueFlags & WINED3DISSUE_BEGIN) {
605 This->state = QUERY_BUILDING;
607 This->state = QUERY_SIGNALLED;
610 return WINED3D_OK; /* can be WINED3DERR_INVALIDCALL. */
614 /**********************************************************
615 * IWineD3DQuery VTbl follows
616 **********************************************************/
618 const IWineD3DQueryVtbl IWineD3DQuery_Vtbl =
620 /*** IUnknown methods ***/
621 IWineD3DQueryImpl_QueryInterface,
622 IWineD3DQueryImpl_AddRef,
623 IWineD3DQueryImpl_Release,
624 /*** IWineD3Dquery methods ***/
625 IWineD3DQueryImpl_GetParent,
626 IWineD3DQueryImpl_GetDevice,
627 IWineD3DQueryImpl_GetData,
628 IWineD3DQueryImpl_GetDataSize,
629 IWineD3DQueryImpl_GetType,
630 IWineD3DQueryImpl_Issue
633 const IWineD3DQueryVtbl IWineD3DEventQuery_Vtbl =
635 /*** IUnknown methods ***/
636 IWineD3DQueryImpl_QueryInterface,
637 IWineD3DQueryImpl_AddRef,
638 IWineD3DQueryImpl_Release,
639 /*** IWineD3Dquery methods ***/
640 IWineD3DQueryImpl_GetParent,
641 IWineD3DQueryImpl_GetDevice,
642 IWineD3DEventQueryImpl_GetData,
643 IWineD3DEventQueryImpl_GetDataSize,
644 IWineD3DQueryImpl_GetType,
645 IWineD3DEventQueryImpl_Issue
648 const IWineD3DQueryVtbl IWineD3DOcclusionQuery_Vtbl =
650 /*** IUnknown methods ***/
651 IWineD3DQueryImpl_QueryInterface,
652 IWineD3DQueryImpl_AddRef,
653 IWineD3DQueryImpl_Release,
654 /*** IWineD3Dquery methods ***/
655 IWineD3DQueryImpl_GetParent,
656 IWineD3DQueryImpl_GetDevice,
657 IWineD3DOcclusionQueryImpl_GetData,
658 IWineD3DOcclusionQueryImpl_GetDataSize,
659 IWineD3DQueryImpl_GetType,
660 IWineD3DOcclusionQueryImpl_Issue