makefiles: Output all the import lib dependencies explicitly for each module.
[wine/multimedia.git] / dlls / wined3d / query.c
blob59e385ea50a425f3c327bea979a817fc514c1ea0
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 HRESULT wined3d_event_query_init(const struct wined3d_gl_info *gl_info, struct wined3d_event_query **query)
32 struct wined3d_event_query *ret;
33 *query = NULL;
34 if (!gl_info->supported[ARB_SYNC] && !gl_info->supported[NV_FENCE]
35 && !gl_info->supported[APPLE_FENCE]) return E_NOTIMPL;
37 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(*ret));
38 if (!ret)
40 ERR("Failed to allocate a wined3d event query structure.\n");
41 return E_OUTOFMEMORY;
43 ret->context = NULL;
44 *query = ret;
45 return WINED3D_OK;
48 void wined3d_event_query_destroy(struct wined3d_event_query *query)
50 if (query->context) context_free_event_query(query);
51 HeapFree(GetProcessHeap(), 0, query);
54 enum wined3d_event_query_result wined3d_event_query_test(struct wined3d_event_query *query, IWineD3DDeviceImpl *device)
56 struct wined3d_context *context;
57 const struct wined3d_gl_info *gl_info;
58 enum wined3d_event_query_result ret;
59 BOOL fence_result;
61 TRACE("(%p) : device %p\n", query, device);
63 if (query->context == NULL)
65 TRACE("Query not started\n");
66 return WINED3D_EVENT_QUERY_NOT_STARTED;
69 if (!query->context->gl_info->supported[ARB_SYNC] && query->context->tid != GetCurrentThreadId())
71 WARN("Event query tested from wrong thread\n");
72 return WINED3D_EVENT_QUERY_WRONG_THREAD;
75 context = context_acquire(device, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
76 gl_info = context->gl_info;
78 ENTER_GL();
80 if (gl_info->supported[ARB_SYNC])
82 GLenum gl_ret = GL_EXTCALL(glClientWaitSync(query->object.sync, 0, 0));
83 checkGLcall("glClientWaitSync");
85 switch (gl_ret)
87 case GL_ALREADY_SIGNALED:
88 case GL_CONDITION_SATISFIED:
89 ret = WINED3D_EVENT_QUERY_OK;
90 break;
92 case GL_TIMEOUT_EXPIRED:
93 ret = WINED3D_EVENT_QUERY_WAITING;
94 break;
96 case GL_WAIT_FAILED:
97 default:
98 ERR("glClientWaitSync returned %#x.\n", gl_ret);
99 ret = WINED3D_EVENT_QUERY_ERROR;
102 else if (gl_info->supported[APPLE_FENCE])
104 fence_result = GL_EXTCALL(glTestFenceAPPLE(query->object.id));
105 checkGLcall("glTestFenceAPPLE");
106 if (fence_result) ret = WINED3D_EVENT_QUERY_OK;
107 else ret = WINED3D_EVENT_QUERY_WAITING;
109 else if (gl_info->supported[NV_FENCE])
111 fence_result = GL_EXTCALL(glTestFenceNV(query->object.id));
112 checkGLcall("glTestFenceNV");
113 if (fence_result) ret = WINED3D_EVENT_QUERY_OK;
114 else ret = WINED3D_EVENT_QUERY_WAITING;
116 else
118 ERR("Event query created despite lack of GL support\n");
119 ret = WINED3D_EVENT_QUERY_ERROR;
122 LEAVE_GL();
124 context_release(context);
125 return ret;
128 enum wined3d_event_query_result wined3d_event_query_finish(struct wined3d_event_query *query, IWineD3DDeviceImpl *device)
130 struct wined3d_context *context;
131 const struct wined3d_gl_info *gl_info;
132 enum wined3d_event_query_result ret;
134 TRACE("(%p)\n", query);
136 if (!query->context)
138 TRACE("Query not started\n");
139 return WINED3D_EVENT_QUERY_NOT_STARTED;
141 gl_info = query->context->gl_info;
143 if (query->context->tid != GetCurrentThreadId() && !gl_info->supported[ARB_SYNC])
145 /* A glFinish does not reliably wait for draws in other contexts. The caller has
146 * to find its own way to cope with the thread switch
148 WARN("Event query finished from wrong thread\n");
149 return WINED3D_EVENT_QUERY_WRONG_THREAD;
152 context = context_acquire(device, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
154 ENTER_GL();
155 if (gl_info->supported[ARB_SYNC])
157 GLenum gl_ret = GL_EXTCALL(glClientWaitSync(query->object.sync, 0, ~(GLuint64)0));
158 checkGLcall("glClientWaitSync");
160 switch (gl_ret)
162 case GL_ALREADY_SIGNALED:
163 case GL_CONDITION_SATISFIED:
164 ret = WINED3D_EVENT_QUERY_OK;
165 break;
167 /* We don't expect a timeout for a ~584 year wait */
168 default:
169 ERR("glClientWaitSync returned %#x.\n", gl_ret);
170 ret = WINED3D_EVENT_QUERY_ERROR;
173 else if (context->gl_info->supported[APPLE_FENCE])
175 GL_EXTCALL(glFinishFenceAPPLE(query->object.id));
176 checkGLcall("glFinishFenceAPPLE");
177 ret = WINED3D_EVENT_QUERY_OK;
179 else if (context->gl_info->supported[NV_FENCE])
181 GL_EXTCALL(glFinishFenceNV(query->object.id));
182 checkGLcall("glFinishFenceNV");
183 ret = WINED3D_EVENT_QUERY_OK;
185 else
187 ERR("Event query created without GL support\n");
188 ret = WINED3D_EVENT_QUERY_ERROR;
190 LEAVE_GL();
192 context_release(context);
193 return ret;
196 void wined3d_event_query_issue(struct wined3d_event_query *query, IWineD3DDeviceImpl *device)
198 const struct wined3d_gl_info *gl_info;
199 struct wined3d_context *context;
201 if (query->context)
203 if (!query->context->gl_info->supported[ARB_SYNC] && query->context->tid != GetCurrentThreadId())
205 context_free_event_query(query);
206 context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
207 context_alloc_event_query(context, query);
209 else
211 context = context_acquire(device, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
214 else
216 context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
217 context_alloc_event_query(context, query);
220 gl_info = context->gl_info;
222 ENTER_GL();
224 if (gl_info->supported[ARB_SYNC])
226 if (query->object.sync) GL_EXTCALL(glDeleteSync(query->object.sync));
227 checkGLcall("glDeleteSync");
228 query->object.sync = GL_EXTCALL(glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
229 checkGLcall("glFenceSync");
231 else if (gl_info->supported[APPLE_FENCE])
233 GL_EXTCALL(glSetFenceAPPLE(query->object.id));
234 checkGLcall("glSetFenceAPPLE");
236 else if (gl_info->supported[NV_FENCE])
238 GL_EXTCALL(glSetFenceNV(query->object.id, GL_ALL_COMPLETED_NV));
239 checkGLcall("glSetFenceNV");
242 LEAVE_GL();
244 context_release(context);
248 * Occlusion Queries:
249 * http://www.gris.uni-tuebingen.de/~bartz/Publications/paper/hww98.pdf
250 * http://oss.sgi.com/projects/ogl-sample/registry/ARB/occlusion_query.txt
253 /* *******************************************
254 IWineD3DQuery IUnknown parts follow
255 ******************************************* */
256 static HRESULT WINAPI IWineD3DQueryImpl_QueryInterface(IWineD3DQuery *iface, REFIID riid, LPVOID *ppobj)
258 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
259 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
260 if (IsEqualGUID(riid, &IID_IUnknown)
261 || IsEqualGUID(riid, &IID_IWineD3DBase)
262 || IsEqualGUID(riid, &IID_IWineD3DQuery)) {
263 IUnknown_AddRef(iface);
264 *ppobj = This;
265 return S_OK;
267 *ppobj = NULL;
268 return E_NOINTERFACE;
271 static ULONG WINAPI IWineD3DQueryImpl_AddRef(IWineD3DQuery *iface) {
272 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
273 TRACE("(%p) : AddRef increasing from %d\n", This, This->ref);
274 return InterlockedIncrement(&This->ref);
277 static ULONG WINAPI IWineD3DQueryImpl_Release(IWineD3DQuery *iface) {
278 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
279 ULONG ref;
280 TRACE("(%p) : Releasing from %d\n", This, This->ref);
281 ref = InterlockedDecrement(&This->ref);
282 if (ref == 0) {
283 /* Queries are specific to the GL context that created them. Not
284 * deleting the query will obviously leak it, but that's still better
285 * than potentially deleting a different query with the same id in this
286 * context, and (still) leaking the actual query. */
287 if (This->type == WINED3DQUERYTYPE_EVENT)
289 struct wined3d_event_query *query = This->extendedData;
290 if (query) wined3d_event_query_destroy(query);
292 else if (This->type == WINED3DQUERYTYPE_OCCLUSION)
294 struct wined3d_occlusion_query *query = This->extendedData;
296 if (query->context) context_free_occlusion_query(query);
297 HeapFree(GetProcessHeap(), 0, This->extendedData);
300 HeapFree(GetProcessHeap(), 0, This);
302 return ref;
305 /* *******************************************
306 IWineD3DQuery IWineD3DQuery parts follow
307 ******************************************* */
308 static HRESULT WINAPI IWineD3DQueryImpl_GetParent(IWineD3DQuery *iface, IUnknown **parent)
310 TRACE("iface %p, parent %p.\n", iface, parent);
312 *parent = (IUnknown *)parent;
313 IUnknown_AddRef(*parent);
315 TRACE("Returning %p.\n", *parent);
317 return WINED3D_OK;
320 static HRESULT WINAPI IWineD3DOcclusionQueryImpl_GetData(IWineD3DQuery* iface, void* pData, DWORD dwSize, DWORD dwGetDataFlags) {
321 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *) iface;
322 struct wined3d_occlusion_query *query = This->extendedData;
323 IWineD3DDeviceImpl *device = This->device;
324 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
325 struct wined3d_context *context;
326 DWORD* data = pData;
327 GLuint available;
328 GLuint samples;
329 HRESULT res;
331 TRACE("(%p) : type D3DQUERY_OCCLUSION, pData %p, dwSize %#x, dwGetDataFlags %#x\n", This, pData, dwSize, dwGetDataFlags);
333 if (!query->context) This->state = QUERY_CREATED;
335 if (This->state == QUERY_CREATED)
337 /* D3D allows GetData on a new query, OpenGL doesn't. So just invent the data ourselves */
338 TRACE("Query wasn't yet started, returning S_OK\n");
339 if(data) *data = 0;
340 return S_OK;
343 if (This->state == QUERY_BUILDING)
345 /* Msdn says this returns an error, but our tests show that S_FALSE is returned */
346 TRACE("Query is building, returning S_FALSE\n");
347 return S_FALSE;
350 if (!gl_info->supported[ARB_OCCLUSION_QUERY])
352 WARN("(%p) : Occlusion queries not supported. Returning 1.\n", This);
353 *data = 1;
354 return S_OK;
357 if (query->context->tid != GetCurrentThreadId())
359 FIXME("%p Wrong thread, returning 1.\n", This);
360 *data = 1;
361 return S_OK;
364 context = context_acquire(This->device, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
366 ENTER_GL();
368 GL_EXTCALL(glGetQueryObjectuivARB(query->id, GL_QUERY_RESULT_AVAILABLE_ARB, &available));
369 checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT_AVAILABLE)");
370 TRACE("(%p) : available %d.\n", This, available);
372 if (available)
374 if (data)
376 GL_EXTCALL(glGetQueryObjectuivARB(query->id, GL_QUERY_RESULT_ARB, &samples));
377 checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT)");
378 TRACE("(%p) : Returning %d samples.\n", This, samples);
379 *data = samples;
381 res = S_OK;
383 else
385 res = S_FALSE;
388 LEAVE_GL();
390 context_release(context);
392 return res;
395 static HRESULT WINAPI IWineD3DEventQueryImpl_GetData(IWineD3DQuery* iface, void* pData, DWORD dwSize, DWORD dwGetDataFlags) {
396 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *) iface;
397 struct wined3d_event_query *query = This->extendedData;
398 BOOL *data = pData;
399 enum wined3d_event_query_result ret;
401 TRACE("(%p) : type D3DQUERY_EVENT, pData %p, dwSize %#x, dwGetDataFlags %#x\n", This, pData, dwSize, dwGetDataFlags);
403 if (!pData || !dwSize) return S_OK;
404 if (!query)
406 WARN("(%p): Event query not supported by GL, reporting GPU idle\n", This);
407 *data = TRUE;
408 return S_OK;
411 ret = wined3d_event_query_test(query, This->device);
412 switch(ret)
414 case WINED3D_EVENT_QUERY_OK:
415 case WINED3D_EVENT_QUERY_NOT_STARTED:
416 *data = TRUE;
417 break;
419 case WINED3D_EVENT_QUERY_WAITING:
420 *data = FALSE;
421 break;
423 case WINED3D_EVENT_QUERY_WRONG_THREAD:
424 FIXME("(%p) Wrong thread, reporting GPU idle.\n", This);
425 *data = TRUE;
426 break;
428 case WINED3D_EVENT_QUERY_ERROR:
429 ERR("The GL event query failed, returning D3DERR_INVALIDCALL\n");
430 return WINED3DERR_INVALIDCALL;
433 return S_OK;
436 static DWORD WINAPI IWineD3DEventQueryImpl_GetDataSize(IWineD3DQuery* iface){
437 TRACE("(%p) : type D3DQUERY_EVENT\n", iface);
439 return sizeof(BOOL);
442 static DWORD WINAPI IWineD3DOcclusionQueryImpl_GetDataSize(IWineD3DQuery* iface){
443 TRACE("(%p) : type D3DQUERY_OCCLUSION\n", iface);
445 return sizeof(DWORD);
448 static WINED3DQUERYTYPE WINAPI IWineD3DQueryImpl_GetType(IWineD3DQuery* iface){
449 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
450 return This->type;
453 static HRESULT WINAPI IWineD3DEventQueryImpl_Issue(IWineD3DQuery* iface, DWORD dwIssueFlags) {
454 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
456 TRACE("(%p) : dwIssueFlags %#x, type D3DQUERY_EVENT\n", This, dwIssueFlags);
457 if (dwIssueFlags & WINED3DISSUE_END)
459 struct wined3d_event_query *query = This->extendedData;
461 /* Faked event query support */
462 if (!query) return WINED3D_OK;
464 wined3d_event_query_issue(query, This->device);
466 else if(dwIssueFlags & WINED3DISSUE_BEGIN)
468 /* Started implicitly at device creation */
469 ERR("Event query issued with START flag - what to do?\n");
472 if(dwIssueFlags & WINED3DISSUE_BEGIN) {
473 This->state = QUERY_BUILDING;
474 } else {
475 This->state = QUERY_SIGNALLED;
478 return WINED3D_OK;
481 static HRESULT WINAPI IWineD3DOcclusionQueryImpl_Issue(IWineD3DQuery* iface, DWORD dwIssueFlags) {
482 IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
483 IWineD3DDeviceImpl *device = This->device;
484 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
486 if (gl_info->supported[ARB_OCCLUSION_QUERY])
488 struct wined3d_occlusion_query *query = This->extendedData;
489 struct wined3d_context *context;
491 /* This is allowed according to msdn and our tests. Reset the query and restart */
492 if (dwIssueFlags & WINED3DISSUE_BEGIN)
494 if (This->state == QUERY_BUILDING)
496 if (query->context->tid != GetCurrentThreadId())
498 FIXME("Wrong thread, can't restart query.\n");
500 context_free_occlusion_query(query);
501 context = context_acquire(This->device, NULL, CTXUSAGE_RESOURCELOAD);
502 context_alloc_occlusion_query(context, query);
504 else
506 context = context_acquire(This->device, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
508 ENTER_GL();
509 GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB));
510 checkGLcall("glEndQuery()");
511 LEAVE_GL();
514 else
516 if (query->context) context_free_occlusion_query(query);
517 context = context_acquire(This->device, NULL, CTXUSAGE_RESOURCELOAD);
518 context_alloc_occlusion_query(context, query);
521 ENTER_GL();
522 GL_EXTCALL(glBeginQueryARB(GL_SAMPLES_PASSED_ARB, query->id));
523 checkGLcall("glBeginQuery()");
524 LEAVE_GL();
526 context_release(context);
528 if (dwIssueFlags & WINED3DISSUE_END) {
529 /* Msdn says _END on a non-building occlusion query returns an error, but
530 * our tests show that it returns OK. But OpenGL doesn't like it, so avoid
531 * generating an error
533 if (This->state == QUERY_BUILDING)
535 if (query->context->tid != GetCurrentThreadId())
537 FIXME("Wrong thread, can't end query.\n");
539 else
541 context = context_acquire(This->device, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
543 ENTER_GL();
544 GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB));
545 checkGLcall("glEndQuery()");
546 LEAVE_GL();
548 context_release(context);
552 } else {
553 FIXME("(%p) : Occlusion queries not supported\n", This);
556 if(dwIssueFlags & WINED3DISSUE_BEGIN) {
557 This->state = QUERY_BUILDING;
558 } else {
559 This->state = QUERY_SIGNALLED;
561 return WINED3D_OK; /* can be WINED3DERR_INVALIDCALL. */
564 static const IWineD3DQueryVtbl IWineD3DEventQuery_Vtbl =
566 /*** IUnknown methods ***/
567 IWineD3DQueryImpl_QueryInterface,
568 IWineD3DQueryImpl_AddRef,
569 IWineD3DQueryImpl_Release,
570 /*** IWineD3Dquery methods ***/
571 IWineD3DQueryImpl_GetParent,
572 IWineD3DEventQueryImpl_GetData,
573 IWineD3DEventQueryImpl_GetDataSize,
574 IWineD3DQueryImpl_GetType,
575 IWineD3DEventQueryImpl_Issue
578 static const IWineD3DQueryVtbl IWineD3DOcclusionQuery_Vtbl =
580 /*** IUnknown methods ***/
581 IWineD3DQueryImpl_QueryInterface,
582 IWineD3DQueryImpl_AddRef,
583 IWineD3DQueryImpl_Release,
584 /*** IWineD3Dquery methods ***/
585 IWineD3DQueryImpl_GetParent,
586 IWineD3DOcclusionQueryImpl_GetData,
587 IWineD3DOcclusionQueryImpl_GetDataSize,
588 IWineD3DQueryImpl_GetType,
589 IWineD3DOcclusionQueryImpl_Issue
592 HRESULT query_init(IWineD3DQueryImpl *query, IWineD3DDeviceImpl *device,
593 WINED3DQUERYTYPE type, IUnknown *parent)
595 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
596 HRESULT hr;
598 switch (type)
600 case WINED3DQUERYTYPE_OCCLUSION:
601 TRACE("Occlusion query.\n");
602 if (!gl_info->supported[ARB_OCCLUSION_QUERY])
604 WARN("Unsupported in local OpenGL implementation: ARB_OCCLUSION_QUERY.\n");
605 return WINED3DERR_NOTAVAILABLE;
607 query->lpVtbl = &IWineD3DOcclusionQuery_Vtbl;
608 query->extendedData = HeapAlloc(GetProcessHeap(), 0, sizeof(struct wined3d_occlusion_query));
609 if (!query->extendedData)
611 ERR("Failed to allocate occlusion query extended data.\n");
612 return E_OUTOFMEMORY;
614 ((struct wined3d_occlusion_query *)query->extendedData)->context = NULL;
615 break;
617 case WINED3DQUERYTYPE_EVENT:
618 TRACE("Event query.\n");
619 query->lpVtbl = &IWineD3DEventQuery_Vtbl;
620 hr = wined3d_event_query_init(gl_info, (struct wined3d_event_query **) &query->extendedData);
621 if (hr == E_NOTIMPL)
623 /* Half-Life 2 needs this query. It does not render the main
624 * menu correctly otherwise. Pretend to support it, faking
625 * this query does not do much harm except potentially
626 * lowering performance. */
627 FIXME("Event query: Unimplemented, but pretending to be supported.\n");
629 else if(FAILED(hr))
631 return hr;
633 break;
635 case WINED3DQUERYTYPE_VCACHE:
636 case WINED3DQUERYTYPE_RESOURCEMANAGER:
637 case WINED3DQUERYTYPE_VERTEXSTATS:
638 case WINED3DQUERYTYPE_TIMESTAMP:
639 case WINED3DQUERYTYPE_TIMESTAMPDISJOINT:
640 case WINED3DQUERYTYPE_TIMESTAMPFREQ:
641 case WINED3DQUERYTYPE_PIPELINETIMINGS:
642 case WINED3DQUERYTYPE_INTERFACETIMINGS:
643 case WINED3DQUERYTYPE_VERTEXTIMINGS:
644 case WINED3DQUERYTYPE_PIXELTIMINGS:
645 case WINED3DQUERYTYPE_BANDWIDTHTIMINGS:
646 case WINED3DQUERYTYPE_CACHEUTILIZATION:
647 default:
648 FIXME("Unhandled query type %#x.\n", type);
649 return WINED3DERR_NOTAVAILABLE;
652 query->type = type;
653 query->state = QUERY_CREATED;
654 query->device = device;
655 query->parent = parent;
656 query->ref = 1;
658 return WINED3D_OK;