wined3d: Use an internal call table for query operations.
[wine.git] / dlls / wined3d / query.c
blobca7f48a78912ff8ee55eae8f8d0451f749d495cb
1 /*
2 * IWineD3DQuery implementation
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6 * Copyright 2009-2010 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);
29 BOOL wined3d_event_query_supported(const struct wined3d_gl_info *gl_info)
31 return gl_info->supported[ARB_SYNC] || gl_info->supported[NV_FENCE] || gl_info->supported[APPLE_FENCE];
34 void wined3d_event_query_destroy(struct wined3d_event_query *query)
36 if (query->context) context_free_event_query(query);
37 HeapFree(GetProcessHeap(), 0, query);
40 enum wined3d_event_query_result wined3d_event_query_test(struct wined3d_event_query *query, IWineD3DDeviceImpl *device)
42 struct wined3d_context *context;
43 const struct wined3d_gl_info *gl_info;
44 enum wined3d_event_query_result ret;
45 BOOL fence_result;
47 TRACE("(%p) : device %p\n", query, device);
49 if (!query->context)
51 TRACE("Query not started\n");
52 return WINED3D_EVENT_QUERY_NOT_STARTED;
55 if (!query->context->gl_info->supported[ARB_SYNC] && query->context->tid != GetCurrentThreadId())
57 WARN("Event query tested from wrong thread\n");
58 return WINED3D_EVENT_QUERY_WRONG_THREAD;
61 context = context_acquire(device, query->context->current_rt);
62 gl_info = context->gl_info;
64 ENTER_GL();
66 if (gl_info->supported[ARB_SYNC])
68 GLenum gl_ret = GL_EXTCALL(glClientWaitSync(query->object.sync, 0, 0));
69 checkGLcall("glClientWaitSync");
71 switch (gl_ret)
73 case GL_ALREADY_SIGNALED:
74 case GL_CONDITION_SATISFIED:
75 ret = WINED3D_EVENT_QUERY_OK;
76 break;
78 case GL_TIMEOUT_EXPIRED:
79 ret = WINED3D_EVENT_QUERY_WAITING;
80 break;
82 case GL_WAIT_FAILED:
83 default:
84 ERR("glClientWaitSync returned %#x.\n", gl_ret);
85 ret = WINED3D_EVENT_QUERY_ERROR;
88 else if (gl_info->supported[APPLE_FENCE])
90 fence_result = GL_EXTCALL(glTestFenceAPPLE(query->object.id));
91 checkGLcall("glTestFenceAPPLE");
92 if (fence_result) ret = WINED3D_EVENT_QUERY_OK;
93 else ret = WINED3D_EVENT_QUERY_WAITING;
95 else if (gl_info->supported[NV_FENCE])
97 fence_result = GL_EXTCALL(glTestFenceNV(query->object.id));
98 checkGLcall("glTestFenceNV");
99 if (fence_result) ret = WINED3D_EVENT_QUERY_OK;
100 else ret = WINED3D_EVENT_QUERY_WAITING;
102 else
104 ERR("Event query created despite lack of GL support\n");
105 ret = WINED3D_EVENT_QUERY_ERROR;
108 LEAVE_GL();
110 context_release(context);
111 return ret;
114 enum wined3d_event_query_result wined3d_event_query_finish(struct wined3d_event_query *query, IWineD3DDeviceImpl *device)
116 struct wined3d_context *context;
117 const struct wined3d_gl_info *gl_info;
118 enum wined3d_event_query_result ret;
120 TRACE("(%p)\n", query);
122 if (!query->context)
124 TRACE("Query not started\n");
125 return WINED3D_EVENT_QUERY_NOT_STARTED;
127 gl_info = query->context->gl_info;
129 if (query->context->tid != GetCurrentThreadId() && !gl_info->supported[ARB_SYNC])
131 /* A glFinish does not reliably wait for draws in other contexts. The caller has
132 * to find its own way to cope with the thread switch
134 WARN("Event query finished from wrong thread\n");
135 return WINED3D_EVENT_QUERY_WRONG_THREAD;
138 context = context_acquire(device, query->context->current_rt);
140 ENTER_GL();
141 if (gl_info->supported[ARB_SYNC])
143 GLenum gl_ret = GL_EXTCALL(glClientWaitSync(query->object.sync, 0, ~(GLuint64)0));
144 checkGLcall("glClientWaitSync");
146 switch (gl_ret)
148 case GL_ALREADY_SIGNALED:
149 case GL_CONDITION_SATISFIED:
150 ret = WINED3D_EVENT_QUERY_OK;
151 break;
153 /* We don't expect a timeout for a ~584 year wait */
154 default:
155 ERR("glClientWaitSync returned %#x.\n", gl_ret);
156 ret = WINED3D_EVENT_QUERY_ERROR;
159 else if (context->gl_info->supported[APPLE_FENCE])
161 GL_EXTCALL(glFinishFenceAPPLE(query->object.id));
162 checkGLcall("glFinishFenceAPPLE");
163 ret = WINED3D_EVENT_QUERY_OK;
165 else if (context->gl_info->supported[NV_FENCE])
167 GL_EXTCALL(glFinishFenceNV(query->object.id));
168 checkGLcall("glFinishFenceNV");
169 ret = WINED3D_EVENT_QUERY_OK;
171 else
173 ERR("Event query created without GL support\n");
174 ret = WINED3D_EVENT_QUERY_ERROR;
176 LEAVE_GL();
178 context_release(context);
179 return ret;
182 void wined3d_event_query_issue(struct wined3d_event_query *query, IWineD3DDeviceImpl *device)
184 const struct wined3d_gl_info *gl_info;
185 struct wined3d_context *context;
187 if (query->context)
189 if (!query->context->gl_info->supported[ARB_SYNC] && query->context->tid != GetCurrentThreadId())
191 context_free_event_query(query);
192 context = context_acquire(device, NULL);
193 context_alloc_event_query(context, query);
195 else
197 context = context_acquire(device, query->context->current_rt);
200 else
202 context = context_acquire(device, NULL);
203 context_alloc_event_query(context, query);
206 gl_info = context->gl_info;
208 ENTER_GL();
210 if (gl_info->supported[ARB_SYNC])
212 if (query->object.sync) GL_EXTCALL(glDeleteSync(query->object.sync));
213 checkGLcall("glDeleteSync");
214 query->object.sync = GL_EXTCALL(glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
215 checkGLcall("glFenceSync");
217 else if (gl_info->supported[APPLE_FENCE])
219 GL_EXTCALL(glSetFenceAPPLE(query->object.id));
220 checkGLcall("glSetFenceAPPLE");
222 else if (gl_info->supported[NV_FENCE])
224 GL_EXTCALL(glSetFenceNV(query->object.id, GL_ALL_COMPLETED_NV));
225 checkGLcall("glSetFenceNV");
228 LEAVE_GL();
230 context_release(context);
233 static HRESULT WINAPI IWineD3DQueryImpl_QueryInterface(IWineD3DQuery *iface, REFIID riid, void **object)
235 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
237 if (IsEqualGUID(riid, &IID_IWineD3DQuery)
238 || IsEqualGUID(riid, &IID_IUnknown))
240 IUnknown_AddRef(iface);
241 *object = iface;
242 return S_OK;
245 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
247 *object = NULL;
248 return E_NOINTERFACE;
251 static ULONG WINAPI IWineD3DQueryImpl_AddRef(IWineD3DQuery *iface) {
252 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
253 TRACE("(%p) : AddRef increasing from %d\n", This, This->ref);
254 return InterlockedIncrement(&This->ref);
257 static ULONG WINAPI IWineD3DQueryImpl_Release(IWineD3DQuery *iface) {
258 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
259 ULONG ref;
260 TRACE("(%p) : Releasing from %d\n", This, This->ref);
261 ref = InterlockedDecrement(&This->ref);
263 if (!ref)
265 /* Queries are specific to the GL context that created them. Not
266 * deleting the query will obviously leak it, but that's still better
267 * than potentially deleting a different query with the same id in this
268 * context, and (still) leaking the actual query. */
269 if (This->type == WINED3DQUERYTYPE_EVENT)
271 struct wined3d_event_query *query = This->extendedData;
272 if (query) wined3d_event_query_destroy(query);
274 else if (This->type == WINED3DQUERYTYPE_OCCLUSION)
276 struct wined3d_occlusion_query *query = This->extendedData;
278 if (query->context) context_free_occlusion_query(query);
279 HeapFree(GetProcessHeap(), 0, This->extendedData);
282 HeapFree(GetProcessHeap(), 0, This);
284 return ref;
287 static HRESULT WINAPI IWineD3DQueryImpl_GetData(IWineD3DQuery *iface,
288 void *data, DWORD data_size, DWORD flags)
290 struct IWineD3DQueryImpl *query = (struct IWineD3DQueryImpl *)iface;
292 TRACE("iface %p, data %p, data_size %u, flags %#x.\n",
293 iface, data, data_size, flags);
295 return query->query_ops->query_get_data(query, data, data_size, flags);
298 static DWORD WINAPI IWineD3DQueryImpl_GetDataSize(IWineD3DQuery *iface)
300 struct IWineD3DQueryImpl *query = (struct IWineD3DQueryImpl *)iface;
302 TRACE("iface %p.\n", iface);
304 return query->data_size;
307 static HRESULT WINAPI IWineD3DQueryImpl_Issue(IWineD3DQuery *iface, DWORD flags)
309 struct IWineD3DQueryImpl *query = (struct IWineD3DQueryImpl *)iface;
311 TRACE("iface %p, flags %#x.\n", iface, flags);
313 return query->query_ops->query_issue(query, flags);
316 static HRESULT wined3d_occlusion_query_ops_get_data(struct IWineD3DQueryImpl *query,
317 void *pData, DWORD dwSize, DWORD flags)
319 struct wined3d_occlusion_query *oq = query->extendedData;
320 IWineD3DDeviceImpl *device = query->device;
321 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
322 struct wined3d_context *context;
323 DWORD* data = pData;
324 GLuint available;
325 GLuint samples;
326 HRESULT res;
328 TRACE("(%p) : type D3DQUERY_OCCLUSION, pData %p, dwSize %#x, flags %#x.\n", query, pData, dwSize, flags);
330 if (!oq->context)
331 query->state = QUERY_CREATED;
333 if (query->state == QUERY_CREATED)
335 /* D3D allows GetData on a new query, OpenGL doesn't. So just invent the data ourselves */
336 TRACE("Query wasn't yet started, returning S_OK\n");
337 if(data) *data = 0;
338 return S_OK;
341 if (query->state == QUERY_BUILDING)
343 /* Msdn says this returns an error, but our tests show that S_FALSE is returned */
344 TRACE("Query is building, returning S_FALSE\n");
345 return S_FALSE;
348 if (!gl_info->supported[ARB_OCCLUSION_QUERY])
350 WARN("%p Occlusion queries not supported. Returning 1.\n", query);
351 *data = 1;
352 return S_OK;
355 if (oq->context->tid != GetCurrentThreadId())
357 FIXME("%p Wrong thread, returning 1.\n", query);
358 *data = 1;
359 return S_OK;
362 context = context_acquire(query->device, oq->context->current_rt);
364 ENTER_GL();
366 GL_EXTCALL(glGetQueryObjectuivARB(oq->id, GL_QUERY_RESULT_AVAILABLE_ARB, &available));
367 checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT_AVAILABLE)");
368 TRACE("available %#x.\n", available);
370 if (available)
372 if (data)
374 GL_EXTCALL(glGetQueryObjectuivARB(oq->id, GL_QUERY_RESULT_ARB, &samples));
375 checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT)");
376 TRACE("Returning %d samples.\n", samples);
377 *data = samples;
379 res = S_OK;
381 else
383 res = S_FALSE;
386 LEAVE_GL();
388 context_release(context);
390 return res;
393 static HRESULT wined3d_event_query_ops_get_data(IWineD3DQueryImpl *query,
394 void *pData, DWORD dwSize, DWORD flags)
396 struct wined3d_event_query *event_query = query->extendedData;
397 BOOL *data = pData;
398 enum wined3d_event_query_result ret;
400 TRACE("query %p, pData %p, dwSize %#x, flags %#x.\n", query, pData, dwSize, flags);
402 if (!pData || !dwSize) return S_OK;
403 if (!event_query)
405 WARN("Event query not supported by GL, reporting GPU idle.\n");
406 *data = TRUE;
407 return S_OK;
410 ret = wined3d_event_query_test(event_query, query->device);
411 switch(ret)
413 case WINED3D_EVENT_QUERY_OK:
414 case WINED3D_EVENT_QUERY_NOT_STARTED:
415 *data = TRUE;
416 break;
418 case WINED3D_EVENT_QUERY_WAITING:
419 *data = FALSE;
420 break;
422 case WINED3D_EVENT_QUERY_WRONG_THREAD:
423 FIXME("(%p) Wrong thread, reporting GPU idle.\n", query);
424 *data = TRUE;
425 break;
427 case WINED3D_EVENT_QUERY_ERROR:
428 ERR("The GL event query failed, returning D3DERR_INVALIDCALL\n");
429 return WINED3DERR_INVALIDCALL;
432 return S_OK;
435 static WINED3DQUERYTYPE WINAPI IWineD3DQueryImpl_GetType(IWineD3DQuery* iface){
436 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
437 return This->type;
440 static HRESULT wined3d_event_query_ops_issue(struct IWineD3DQueryImpl *query, DWORD flags)
442 TRACE("query %p, flags %#x.\n", query, flags);
444 TRACE("(%p) : flags %#x, type D3DQUERY_EVENT\n", query, flags);
445 if (flags & WINED3DISSUE_END)
447 struct wined3d_event_query *event_query = query->extendedData;
449 /* Faked event query support */
450 if (!event_query) return WINED3D_OK;
452 wined3d_event_query_issue(event_query, query->device);
454 else if (flags & WINED3DISSUE_BEGIN)
456 /* Started implicitly at device creation */
457 ERR("Event query issued with START flag - what to do?\n");
460 if (flags & WINED3DISSUE_BEGIN)
461 query->state = QUERY_BUILDING;
462 else
463 query->state = QUERY_SIGNALLED;
465 return WINED3D_OK;
468 static HRESULT wined3d_occlusion_query_ops_issue(struct IWineD3DQueryImpl *query, DWORD flags)
470 IWineD3DDeviceImpl *device = query->device;
471 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
473 TRACE("query %p, flags %#x.\n", query, flags);
475 if (gl_info->supported[ARB_OCCLUSION_QUERY])
477 struct wined3d_occlusion_query *oq = query->extendedData;
478 struct wined3d_context *context;
480 /* This is allowed according to msdn and our tests. Reset the query and restart */
481 if (flags & WINED3DISSUE_BEGIN)
483 if (query->state == QUERY_BUILDING)
485 if (oq->context->tid != GetCurrentThreadId())
487 FIXME("Wrong thread, can't restart query.\n");
489 context_free_occlusion_query(oq);
490 context = context_acquire(query->device, NULL);
491 context_alloc_occlusion_query(context, oq);
493 else
495 context = context_acquire(query->device, oq->context->current_rt);
497 ENTER_GL();
498 GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB));
499 checkGLcall("glEndQuery()");
500 LEAVE_GL();
503 else
505 if (oq->context) context_free_occlusion_query(oq);
506 context = context_acquire(query->device, NULL);
507 context_alloc_occlusion_query(context, oq);
510 ENTER_GL();
511 GL_EXTCALL(glBeginQueryARB(GL_SAMPLES_PASSED_ARB, oq->id));
512 checkGLcall("glBeginQuery()");
513 LEAVE_GL();
515 context_release(context);
517 if (flags & WINED3DISSUE_END)
519 /* Msdn says _END on a non-building occlusion query returns an error, but
520 * our tests show that it returns OK. But OpenGL doesn't like it, so avoid
521 * generating an error
523 if (query->state == QUERY_BUILDING)
525 if (oq->context->tid != GetCurrentThreadId())
527 FIXME("Wrong thread, can't end query.\n");
529 else
531 context = context_acquire(query->device, oq->context->current_rt);
533 ENTER_GL();
534 GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB));
535 checkGLcall("glEndQuery()");
536 LEAVE_GL();
538 context_release(context);
543 else
545 FIXME("%p Occlusion queries not supported.\n", query);
548 if (flags & WINED3DISSUE_BEGIN)
549 query->state = QUERY_BUILDING;
550 else
551 query->state = QUERY_SIGNALLED;
553 return WINED3D_OK; /* can be WINED3DERR_INVALIDCALL. */
556 static const struct wined3d_query_ops event_query_ops =
558 wined3d_event_query_ops_get_data,
559 wined3d_event_query_ops_issue,
562 static const struct wined3d_query_ops occlusion_query_ops =
564 wined3d_occlusion_query_ops_get_data,
565 wined3d_occlusion_query_ops_issue,
568 static const struct IWineD3DQueryVtbl IWineD3DQuery_Vtbl =
570 /*** IUnknown methods ***/
571 IWineD3DQueryImpl_QueryInterface,
572 IWineD3DQueryImpl_AddRef,
573 IWineD3DQueryImpl_Release,
574 /*** IWineD3Dquery methods ***/
575 IWineD3DQueryImpl_GetData,
576 IWineD3DQueryImpl_GetDataSize,
577 IWineD3DQueryImpl_GetType,
578 IWineD3DQueryImpl_Issue,
581 HRESULT query_init(IWineD3DQueryImpl *query, IWineD3DDeviceImpl *device, WINED3DQUERYTYPE type)
583 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
585 switch (type)
587 case WINED3DQUERYTYPE_OCCLUSION:
588 TRACE("Occlusion query.\n");
589 if (!gl_info->supported[ARB_OCCLUSION_QUERY])
591 WARN("Unsupported in local OpenGL implementation: ARB_OCCLUSION_QUERY.\n");
592 return WINED3DERR_NOTAVAILABLE;
594 query->query_ops = &occlusion_query_ops;
595 query->data_size = sizeof(DWORD);
596 query->extendedData = HeapAlloc(GetProcessHeap(), 0, sizeof(struct wined3d_occlusion_query));
597 if (!query->extendedData)
599 ERR("Failed to allocate occlusion query extended data.\n");
600 return E_OUTOFMEMORY;
602 ((struct wined3d_occlusion_query *)query->extendedData)->context = NULL;
603 break;
605 case WINED3DQUERYTYPE_EVENT:
606 TRACE("Event query.\n");
607 if (!wined3d_event_query_supported(gl_info))
609 /* Half-Life 2 needs this query. It does not render the main
610 * menu correctly otherwise. Pretend to support it, faking
611 * this query does not do much harm except potentially
612 * lowering performance. */
613 FIXME("Event query: Unimplemented, but pretending to be supported.\n");
615 query->query_ops = &event_query_ops;
616 query->data_size = sizeof(BOOL);
617 query->extendedData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct wined3d_event_query));
618 if (!query->extendedData)
620 ERR("Failed to allocate event query memory.\n");
621 return E_OUTOFMEMORY;
623 break;
625 case WINED3DQUERYTYPE_VCACHE:
626 case WINED3DQUERYTYPE_RESOURCEMANAGER:
627 case WINED3DQUERYTYPE_VERTEXSTATS:
628 case WINED3DQUERYTYPE_TIMESTAMP:
629 case WINED3DQUERYTYPE_TIMESTAMPDISJOINT:
630 case WINED3DQUERYTYPE_TIMESTAMPFREQ:
631 case WINED3DQUERYTYPE_PIPELINETIMINGS:
632 case WINED3DQUERYTYPE_INTERFACETIMINGS:
633 case WINED3DQUERYTYPE_VERTEXTIMINGS:
634 case WINED3DQUERYTYPE_PIXELTIMINGS:
635 case WINED3DQUERYTYPE_BANDWIDTHTIMINGS:
636 case WINED3DQUERYTYPE_CACHEUTILIZATION:
637 default:
638 FIXME("Unhandled query type %#x.\n", type);
639 return WINED3DERR_NOTAVAILABLE;
642 query->lpVtbl = &IWineD3DQuery_Vtbl;
643 query->type = type;
644 query->state = QUERY_CREATED;
645 query->device = device;
646 query->ref = 1;
648 return WINED3D_OK;