pop 7b3ecd624d768eecb2eda237f54815110f480faa
[wine/hacks.git] / dlls / wined3d / query.c
blob5552bb1f912421e06e5f027970c561ad32e9d070
1 /*
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
24 #include "config.h"
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;
46 BOOL fence_result;
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;
65 ENTER_GL();
67 if (gl_info->supported[ARB_SYNC])
69 GLenum gl_ret = GL_EXTCALL(glClientWaitSync(query->object.sync, 0, 0));
70 checkGLcall("glClientWaitSync");
72 switch (gl_ret)
74 case GL_ALREADY_SIGNALED:
75 case GL_CONDITION_SATISFIED:
76 ret = WINED3D_EVENT_QUERY_OK;
77 break;
79 case GL_TIMEOUT_EXPIRED:
80 ret = WINED3D_EVENT_QUERY_WAITING;
81 break;
83 case GL_WAIT_FAILED:
84 default:
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;
103 else
105 ERR("Event query created despite lack of GL support\n");
106 ret = WINED3D_EVENT_QUERY_ERROR;
109 LEAVE_GL();
111 context_release(context);
112 return ret;
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);
123 if (!query->context)
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);
141 ENTER_GL();
142 if (gl_info->supported[ARB_SYNC])
144 GLenum gl_ret = GL_EXTCALL(glClientWaitSync(query->object.sync, 0, ~(GLuint64)0));
145 checkGLcall("glClientWaitSync");
147 switch (gl_ret)
149 case GL_ALREADY_SIGNALED:
150 case GL_CONDITION_SATISFIED:
151 ret = WINED3D_EVENT_QUERY_OK;
152 break;
154 /* We don't expect a timeout for a ~584 year wait */
155 default:
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;
172 else
174 ERR("Event query created without GL support\n");
175 ret = WINED3D_EVENT_QUERY_ERROR;
177 LEAVE_GL();
179 context_release(context);
180 return ret;
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;
188 if (query->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);
196 else
198 context = context_acquire(device, query->context->current_rt);
201 else
203 context = context_acquire(device, NULL);
204 context_alloc_event_query(context, query);
207 gl_info = context->gl_info;
209 ENTER_GL();
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");
229 LEAVE_GL();
231 context_release(context);
235 * Occlusion Queries:
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);
251 *ppobj = This;
252 return S_OK;
254 *ppobj = NULL;
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;
266 ULONG ref;
267 TRACE("(%p) : Releasing from %d\n", This, This->ref);
268 ref = InterlockedDecrement(&This->ref);
269 if (ref == 0) {
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);
289 return ref;
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);
304 return WINED3D_OK;
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;
313 DWORD* data = pData;
314 GLuint available;
315 GLuint samples;
316 HRESULT res;
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");
326 if(data) *data = 0;
327 return S_OK;
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");
334 return S_FALSE;
337 if (!gl_info->supported[ARB_OCCLUSION_QUERY])
339 WARN("(%p) : Occlusion queries not supported. Returning 1.\n", This);
340 *data = 1;
341 return S_OK;
344 if (query->context->tid != GetCurrentThreadId())
346 FIXME("%p Wrong thread, returning 1.\n", This);
347 *data = 1;
348 return S_OK;
351 context = context_acquire(This->device, query->context->current_rt);
353 ENTER_GL();
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);
359 if (available)
361 if (data)
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);
366 *data = samples;
368 res = S_OK;
370 else
372 res = S_FALSE;
375 LEAVE_GL();
377 context_release(context);
379 return res;
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;
385 BOOL *data = pData;
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;
391 if (!query)
393 WARN("(%p): Event query not supported by GL, reporting GPU idle\n", This);
394 *data = TRUE;
395 return S_OK;
398 ret = wined3d_event_query_test(query, This->device);
399 switch(ret)
401 case WINED3D_EVENT_QUERY_OK:
402 case WINED3D_EVENT_QUERY_NOT_STARTED:
403 *data = TRUE;
404 break;
406 case WINED3D_EVENT_QUERY_WAITING:
407 *data = FALSE;
408 break;
410 case WINED3D_EVENT_QUERY_WRONG_THREAD:
411 FIXME("(%p) Wrong thread, reporting GPU idle.\n", This);
412 *data = TRUE;
413 break;
415 case WINED3D_EVENT_QUERY_ERROR:
416 ERR("The GL event query failed, returning D3DERR_INVALIDCALL\n");
417 return WINED3DERR_INVALIDCALL;
420 return S_OK;
423 static DWORD WINAPI IWineD3DEventQueryImpl_GetDataSize(IWineD3DQuery* iface){
424 TRACE("(%p) : type D3DQUERY_EVENT\n", iface);
426 return sizeof(BOOL);
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;
437 return This->type;
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;
461 } else {
462 This->state = QUERY_SIGNALLED;
465 return WINED3D_OK;
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);
491 else
493 context = context_acquire(This->device, query->context->current_rt);
495 ENTER_GL();
496 GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB));
497 checkGLcall("glEndQuery()");
498 LEAVE_GL();
501 else
503 if (query->context) context_free_occlusion_query(query);
504 context = context_acquire(This->device, NULL);
505 context_alloc_occlusion_query(context, query);
508 ENTER_GL();
509 GL_EXTCALL(glBeginQueryARB(GL_SAMPLES_PASSED_ARB, query->id));
510 checkGLcall("glBeginQuery()");
511 LEAVE_GL();
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");
526 else
528 context = context_acquire(This->device, query->context->current_rt);
530 ENTER_GL();
531 GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB));
532 checkGLcall("glEndQuery()");
533 LEAVE_GL();
535 context_release(context);
539 } else {
540 FIXME("(%p) : Occlusion queries not supported\n", This);
543 if(dwIssueFlags & WINED3DISSUE_BEGIN) {
544 This->state = QUERY_BUILDING;
545 } else {
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;
584 switch (type)
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;
601 break;
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;
620 break;
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:
634 default:
635 FIXME("Unhandled query type %#x.\n", type);
636 return WINED3DERR_NOTAVAILABLE;
639 query->type = type;
640 query->state = QUERY_CREATED;
641 query->device = device;
642 query->parent = parent;
643 query->ref = 1;
645 return WINED3D_OK;