2 * IWineD3DQuery implementation
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6 * Copyright 2009 Henri Verbeet for CodeWeavers.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 #define GLINFO_LOCATION (*gl_info)
30 BOOL wined3d_event_query_supported(const struct wined3d_gl_info *gl_info)
32 return gl_info->supported[ARB_SYNC] || gl_info->supported[NV_FENCE] || gl_info->supported[APPLE_FENCE];
35 void wined3d_event_query_destroy(struct wined3d_event_query *query)
37 if (query->context) context_free_event_query(query);
38 HeapFree(GetProcessHeap(), 0, query);
41 enum wined3d_event_query_result wined3d_event_query_test(struct wined3d_event_query *query, IWineD3DDeviceImpl *device)
43 struct wined3d_context *context;
44 const struct wined3d_gl_info *gl_info;
45 enum wined3d_event_query_result ret;
48 TRACE("(%p) : device %p\n", query, device);
50 if (query->context == NULL)
52 TRACE("Query not started\n");
53 return WINED3D_EVENT_QUERY_NOT_STARTED;
56 if (!query->context->gl_info->supported[ARB_SYNC] && query->context->tid != GetCurrentThreadId())
58 WARN("Event query tested from wrong thread\n");
59 return WINED3D_EVENT_QUERY_WRONG_THREAD;
62 context = context_acquire(device, query->context->current_rt);
63 gl_info = context->gl_info;
67 if (gl_info->supported[ARB_SYNC])
69 GLenum gl_ret = GL_EXTCALL(glClientWaitSync(query->object.sync, 0, 0));
70 checkGLcall("glClientWaitSync");
74 case GL_ALREADY_SIGNALED:
75 case GL_CONDITION_SATISFIED:
76 ret = WINED3D_EVENT_QUERY_OK;
79 case GL_TIMEOUT_EXPIRED:
80 ret = WINED3D_EVENT_QUERY_WAITING;
85 ERR("glClientWaitSync returned %#x.\n", gl_ret);
86 ret = WINED3D_EVENT_QUERY_ERROR;
89 else if (gl_info->supported[APPLE_FENCE])
91 fence_result = GL_EXTCALL(glTestFenceAPPLE(query->object.id));
92 checkGLcall("glTestFenceAPPLE");
93 if (fence_result) ret = WINED3D_EVENT_QUERY_OK;
94 else ret = WINED3D_EVENT_QUERY_WAITING;
96 else if (gl_info->supported[NV_FENCE])
98 fence_result = GL_EXTCALL(glTestFenceNV(query->object.id));
99 checkGLcall("glTestFenceNV");
100 if (fence_result) ret = WINED3D_EVENT_QUERY_OK;
101 else ret = WINED3D_EVENT_QUERY_WAITING;
105 ERR("Event query created despite lack of GL support\n");
106 ret = WINED3D_EVENT_QUERY_ERROR;
111 context_release(context);
115 enum wined3d_event_query_result wined3d_event_query_finish(struct wined3d_event_query *query, IWineD3DDeviceImpl *device)
117 struct wined3d_context *context;
118 const struct wined3d_gl_info *gl_info;
119 enum wined3d_event_query_result ret;
121 TRACE("(%p)\n", query);
125 TRACE("Query not started\n");
126 return WINED3D_EVENT_QUERY_NOT_STARTED;
128 gl_info = query->context->gl_info;
130 if (query->context->tid != GetCurrentThreadId() && !gl_info->supported[ARB_SYNC])
132 /* A glFinish does not reliably wait for draws in other contexts. The caller has
133 * to find its own way to cope with the thread switch
135 WARN("Event query finished from wrong thread\n");
136 return WINED3D_EVENT_QUERY_WRONG_THREAD;
139 context = context_acquire(device, query->context->current_rt);
142 if (gl_info->supported[ARB_SYNC])
144 GLenum gl_ret = GL_EXTCALL(glClientWaitSync(query->object.sync, 0, ~(GLuint64)0));
145 checkGLcall("glClientWaitSync");
149 case GL_ALREADY_SIGNALED:
150 case GL_CONDITION_SATISFIED:
151 ret = WINED3D_EVENT_QUERY_OK;
154 /* We don't expect a timeout for a ~584 year wait */
156 ERR("glClientWaitSync returned %#x.\n", gl_ret);
157 ret = WINED3D_EVENT_QUERY_ERROR;
160 else if (context->gl_info->supported[APPLE_FENCE])
162 GL_EXTCALL(glFinishFenceAPPLE(query->object.id));
163 checkGLcall("glFinishFenceAPPLE");
164 ret = WINED3D_EVENT_QUERY_OK;
166 else if (context->gl_info->supported[NV_FENCE])
168 GL_EXTCALL(glFinishFenceNV(query->object.id));
169 checkGLcall("glFinishFenceNV");
170 ret = WINED3D_EVENT_QUERY_OK;
174 ERR("Event query created without GL support\n");
175 ret = WINED3D_EVENT_QUERY_ERROR;
179 context_release(context);
183 void wined3d_event_query_issue(struct wined3d_event_query *query, IWineD3DDeviceImpl *device)
185 const struct wined3d_gl_info *gl_info;
186 struct wined3d_context *context;
190 if (!query->context->gl_info->supported[ARB_SYNC] && query->context->tid != GetCurrentThreadId())
192 context_free_event_query(query);
193 context = context_acquire(device, NULL);
194 context_alloc_event_query(context, query);
198 context = context_acquire(device, query->context->current_rt);
203 context = context_acquire(device, NULL);
204 context_alloc_event_query(context, query);
207 gl_info = context->gl_info;
211 if (gl_info->supported[ARB_SYNC])
213 if (query->object.sync) GL_EXTCALL(glDeleteSync(query->object.sync));
214 checkGLcall("glDeleteSync");
215 query->object.sync = GL_EXTCALL(glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
216 checkGLcall("glFenceSync");
218 else if (gl_info->supported[APPLE_FENCE])
220 GL_EXTCALL(glSetFenceAPPLE(query->object.id));
221 checkGLcall("glSetFenceAPPLE");
223 else if (gl_info->supported[NV_FENCE])
225 GL_EXTCALL(glSetFenceNV(query->object.id, GL_ALL_COMPLETED_NV));
226 checkGLcall("glSetFenceNV");
231 context_release(context);
236 * http://www.gris.uni-tuebingen.de/~bartz/Publications/paper/hww98.pdf
237 * http://oss.sgi.com/projects/ogl-sample/registry/ARB/occlusion_query.txt
240 /* *******************************************
241 IWineD3DQuery IUnknown parts follow
242 ******************************************* */
243 static HRESULT WINAPI IWineD3DQueryImpl_QueryInterface(IWineD3DQuery *iface, REFIID riid, LPVOID *ppobj)
245 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
246 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
247 if (IsEqualGUID(riid, &IID_IUnknown)
248 || IsEqualGUID(riid, &IID_IWineD3DBase)
249 || IsEqualGUID(riid, &IID_IWineD3DQuery)) {
250 IUnknown_AddRef(iface);
255 return E_NOINTERFACE;
258 static ULONG WINAPI IWineD3DQueryImpl_AddRef(IWineD3DQuery *iface) {
259 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
260 TRACE("(%p) : AddRef increasing from %d\n", This, This->ref);
261 return InterlockedIncrement(&This->ref);
264 static ULONG WINAPI IWineD3DQueryImpl_Release(IWineD3DQuery *iface) {
265 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
267 TRACE("(%p) : Releasing from %d\n", This, This->ref);
268 ref = InterlockedDecrement(&This->ref);
270 /* Queries are specific to the GL context that created them. Not
271 * deleting the query will obviously leak it, but that's still better
272 * than potentially deleting a different query with the same id in this
273 * context, and (still) leaking the actual query. */
274 if (This->type == WINED3DQUERYTYPE_EVENT)
276 struct wined3d_event_query *query = This->extendedData;
277 if (query) wined3d_event_query_destroy(query);
279 else if (This->type == WINED3DQUERYTYPE_OCCLUSION)
281 struct wined3d_occlusion_query *query = This->extendedData;
283 if (query->context) context_free_occlusion_query(query);
284 HeapFree(GetProcessHeap(), 0, This->extendedData);
287 HeapFree(GetProcessHeap(), 0, This);
292 /* *******************************************
293 IWineD3DQuery IWineD3DQuery parts follow
294 ******************************************* */
295 static HRESULT WINAPI IWineD3DQueryImpl_GetParent(IWineD3DQuery *iface, IUnknown **parent)
297 TRACE("iface %p, parent %p.\n", iface, parent);
299 *parent = (IUnknown *)parent;
300 IUnknown_AddRef(*parent);
302 TRACE("Returning %p.\n", *parent);
307 static HRESULT WINAPI IWineD3DOcclusionQueryImpl_GetData(IWineD3DQuery* iface, void* pData, DWORD dwSize, DWORD dwGetDataFlags) {
308 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *) iface;
309 struct wined3d_occlusion_query *query = This->extendedData;
310 IWineD3DDeviceImpl *device = This->device;
311 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
312 struct wined3d_context *context;
318 TRACE("(%p) : type D3DQUERY_OCCLUSION, pData %p, dwSize %#x, dwGetDataFlags %#x\n", This, pData, dwSize, dwGetDataFlags);
320 if (!query->context) This->state = QUERY_CREATED;
322 if (This->state == QUERY_CREATED)
324 /* D3D allows GetData on a new query, OpenGL doesn't. So just invent the data ourselves */
325 TRACE("Query wasn't yet started, returning S_OK\n");
330 if (This->state == QUERY_BUILDING)
332 /* Msdn says this returns an error, but our tests show that S_FALSE is returned */
333 TRACE("Query is building, returning S_FALSE\n");
337 if (!gl_info->supported[ARB_OCCLUSION_QUERY])
339 WARN("(%p) : Occlusion queries not supported. Returning 1.\n", This);
344 if (query->context->tid != GetCurrentThreadId())
346 FIXME("%p Wrong thread, returning 1.\n", This);
351 context = context_acquire(This->device, query->context->current_rt);
355 GL_EXTCALL(glGetQueryObjectuivARB(query->id, GL_QUERY_RESULT_AVAILABLE_ARB, &available));
356 checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT_AVAILABLE)");
357 TRACE("(%p) : available %d.\n", This, available);
363 GL_EXTCALL(glGetQueryObjectuivARB(query->id, GL_QUERY_RESULT_ARB, &samples));
364 checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT)");
365 TRACE("(%p) : Returning %d samples.\n", This, samples);
377 context_release(context);
382 static HRESULT WINAPI IWineD3DEventQueryImpl_GetData(IWineD3DQuery* iface, void* pData, DWORD dwSize, DWORD dwGetDataFlags) {
383 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *) iface;
384 struct wined3d_event_query *query = This->extendedData;
386 enum wined3d_event_query_result ret;
388 TRACE("(%p) : type D3DQUERY_EVENT, pData %p, dwSize %#x, dwGetDataFlags %#x\n", This, pData, dwSize, dwGetDataFlags);
390 if (!pData || !dwSize) return S_OK;
393 WARN("(%p): Event query not supported by GL, reporting GPU idle\n", This);
398 ret = wined3d_event_query_test(query, This->device);
401 case WINED3D_EVENT_QUERY_OK:
402 case WINED3D_EVENT_QUERY_NOT_STARTED:
406 case WINED3D_EVENT_QUERY_WAITING:
410 case WINED3D_EVENT_QUERY_WRONG_THREAD:
411 FIXME("(%p) Wrong thread, reporting GPU idle.\n", This);
415 case WINED3D_EVENT_QUERY_ERROR:
416 ERR("The GL event query failed, returning D3DERR_INVALIDCALL\n");
417 return WINED3DERR_INVALIDCALL;
423 static DWORD WINAPI IWineD3DEventQueryImpl_GetDataSize(IWineD3DQuery* iface){
424 TRACE("(%p) : type D3DQUERY_EVENT\n", iface);
429 static DWORD WINAPI IWineD3DOcclusionQueryImpl_GetDataSize(IWineD3DQuery* iface){
430 TRACE("(%p) : type D3DQUERY_OCCLUSION\n", iface);
432 return sizeof(DWORD);
435 static WINED3DQUERYTYPE WINAPI IWineD3DQueryImpl_GetType(IWineD3DQuery* iface){
436 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
440 static HRESULT WINAPI IWineD3DEventQueryImpl_Issue(IWineD3DQuery* iface, DWORD dwIssueFlags) {
441 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
443 TRACE("(%p) : dwIssueFlags %#x, type D3DQUERY_EVENT\n", This, dwIssueFlags);
444 if (dwIssueFlags & WINED3DISSUE_END)
446 struct wined3d_event_query *query = This->extendedData;
448 /* Faked event query support */
449 if (!query) return WINED3D_OK;
451 wined3d_event_query_issue(query, This->device);
453 else if(dwIssueFlags & WINED3DISSUE_BEGIN)
455 /* Started implicitly at device creation */
456 ERR("Event query issued with START flag - what to do?\n");
459 if(dwIssueFlags & WINED3DISSUE_BEGIN) {
460 This->state = QUERY_BUILDING;
462 This->state = QUERY_SIGNALLED;
468 static HRESULT WINAPI IWineD3DOcclusionQueryImpl_Issue(IWineD3DQuery* iface, DWORD dwIssueFlags) {
469 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
470 IWineD3DDeviceImpl *device = This->device;
471 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
473 if (gl_info->supported[ARB_OCCLUSION_QUERY])
475 struct wined3d_occlusion_query *query = This->extendedData;
476 struct wined3d_context *context;
478 /* This is allowed according to msdn and our tests. Reset the query and restart */
479 if (dwIssueFlags & WINED3DISSUE_BEGIN)
481 if (This->state == QUERY_BUILDING)
483 if (query->context->tid != GetCurrentThreadId())
485 FIXME("Wrong thread, can't restart query.\n");
487 context_free_occlusion_query(query);
488 context = context_acquire(This->device, NULL);
489 context_alloc_occlusion_query(context, query);
493 context = context_acquire(This->device, query->context->current_rt);
496 GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB));
497 checkGLcall("glEndQuery()");
503 if (query->context) context_free_occlusion_query(query);
504 context = context_acquire(This->device, NULL);
505 context_alloc_occlusion_query(context, query);
509 GL_EXTCALL(glBeginQueryARB(GL_SAMPLES_PASSED_ARB, query->id));
510 checkGLcall("glBeginQuery()");
513 context_release(context);
515 if (dwIssueFlags & WINED3DISSUE_END) {
516 /* Msdn says _END on a non-building occlusion query returns an error, but
517 * our tests show that it returns OK. But OpenGL doesn't like it, so avoid
518 * generating an error
520 if (This->state == QUERY_BUILDING)
522 if (query->context->tid != GetCurrentThreadId())
524 FIXME("Wrong thread, can't end query.\n");
528 context = context_acquire(This->device, query->context->current_rt);
531 GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB));
532 checkGLcall("glEndQuery()");
535 context_release(context);
540 FIXME("(%p) : Occlusion queries not supported\n", This);
543 if(dwIssueFlags & WINED3DISSUE_BEGIN) {
544 This->state = QUERY_BUILDING;
546 This->state = QUERY_SIGNALLED;
548 return WINED3D_OK; /* can be WINED3DERR_INVALIDCALL. */
551 static const IWineD3DQueryVtbl IWineD3DEventQuery_Vtbl =
553 /*** IUnknown methods ***/
554 IWineD3DQueryImpl_QueryInterface,
555 IWineD3DQueryImpl_AddRef,
556 IWineD3DQueryImpl_Release,
557 /*** IWineD3Dquery methods ***/
558 IWineD3DQueryImpl_GetParent,
559 IWineD3DEventQueryImpl_GetData,
560 IWineD3DEventQueryImpl_GetDataSize,
561 IWineD3DQueryImpl_GetType,
562 IWineD3DEventQueryImpl_Issue
565 static const IWineD3DQueryVtbl IWineD3DOcclusionQuery_Vtbl =
567 /*** IUnknown methods ***/
568 IWineD3DQueryImpl_QueryInterface,
569 IWineD3DQueryImpl_AddRef,
570 IWineD3DQueryImpl_Release,
571 /*** IWineD3Dquery methods ***/
572 IWineD3DQueryImpl_GetParent,
573 IWineD3DOcclusionQueryImpl_GetData,
574 IWineD3DOcclusionQueryImpl_GetDataSize,
575 IWineD3DQueryImpl_GetType,
576 IWineD3DOcclusionQueryImpl_Issue
579 HRESULT query_init(IWineD3DQueryImpl *query, IWineD3DDeviceImpl *device,
580 WINED3DQUERYTYPE type, IUnknown *parent)
582 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
586 case WINED3DQUERYTYPE_OCCLUSION:
587 TRACE("Occlusion query.\n");
588 if (!gl_info->supported[ARB_OCCLUSION_QUERY])
590 WARN("Unsupported in local OpenGL implementation: ARB_OCCLUSION_QUERY.\n");
591 return WINED3DERR_NOTAVAILABLE;
593 query->lpVtbl = &IWineD3DOcclusionQuery_Vtbl;
594 query->extendedData = HeapAlloc(GetProcessHeap(), 0, sizeof(struct wined3d_occlusion_query));
595 if (!query->extendedData)
597 ERR("Failed to allocate occlusion query extended data.\n");
598 return E_OUTOFMEMORY;
600 ((struct wined3d_occlusion_query *)query->extendedData)->context = NULL;
603 case WINED3DQUERYTYPE_EVENT:
604 TRACE("Event query.\n");
605 if (!wined3d_event_query_supported(gl_info))
607 /* Half-Life 2 needs this query. It does not render the main
608 * menu correctly otherwise. Pretend to support it, faking
609 * this query does not do much harm except potentially
610 * lowering performance. */
611 FIXME("Event query: Unimplemented, but pretending to be supported.\n");
613 query->lpVtbl = &IWineD3DEventQuery_Vtbl;
614 query->extendedData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct wined3d_event_query));
615 if (!query->extendedData)
617 ERR("Failed to allocate event query memory.\n");
618 return E_OUTOFMEMORY;
622 case WINED3DQUERYTYPE_VCACHE:
623 case WINED3DQUERYTYPE_RESOURCEMANAGER:
624 case WINED3DQUERYTYPE_VERTEXSTATS:
625 case WINED3DQUERYTYPE_TIMESTAMP:
626 case WINED3DQUERYTYPE_TIMESTAMPDISJOINT:
627 case WINED3DQUERYTYPE_TIMESTAMPFREQ:
628 case WINED3DQUERYTYPE_PIPELINETIMINGS:
629 case WINED3DQUERYTYPE_INTERFACETIMINGS:
630 case WINED3DQUERYTYPE_VERTEXTIMINGS:
631 case WINED3DQUERYTYPE_PIXELTIMINGS:
632 case WINED3DQUERYTYPE_BANDWIDTHTIMINGS:
633 case WINED3DQUERYTYPE_CACHEUTILIZATION:
635 FIXME("Unhandled query type %#x.\n", type);
636 return WINED3DERR_NOTAVAILABLE;
640 query->state = QUERY_CREATED;
641 query->device = device;
642 query->parent = parent;