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"
29 * http://www.gris.uni-tuebingen.de/~bartz/Publications/paper/hww98.pdf
30 * http://oss.sgi.com/projects/ogl-sample/registry/ARB/occlusion_query.txt
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
34 #define GLINFO_LOCATION (*gl_info)
36 /* *******************************************
37 IWineD3DQuery IUnknown parts follow
38 ******************************************* */
39 static HRESULT WINAPI
IWineD3DQueryImpl_QueryInterface(IWineD3DQuery
*iface
, REFIID riid
, LPVOID
*ppobj
)
41 IWineD3DQueryImpl
*This
= (IWineD3DQueryImpl
*)iface
;
42 TRACE("(%p)->(%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
43 if (IsEqualGUID(riid
, &IID_IUnknown
)
44 || IsEqualGUID(riid
, &IID_IWineD3DBase
)
45 || IsEqualGUID(riid
, &IID_IWineD3DQuery
)) {
46 IUnknown_AddRef(iface
);
54 static ULONG WINAPI
IWineD3DQueryImpl_AddRef(IWineD3DQuery
*iface
) {
55 IWineD3DQueryImpl
*This
= (IWineD3DQueryImpl
*)iface
;
56 TRACE("(%p) : AddRef increasing from %d\n", This
, This
->ref
);
57 return InterlockedIncrement(&This
->ref
);
60 static ULONG WINAPI
IWineD3DQueryImpl_Release(IWineD3DQuery
*iface
) {
61 IWineD3DQueryImpl
*This
= (IWineD3DQueryImpl
*)iface
;
63 TRACE("(%p) : Releasing from %d\n", This
, This
->ref
);
64 ref
= InterlockedDecrement(&This
->ref
);
66 /* Queries are specific to the GL context that created them. Not
67 * deleting the query will obviously leak it, but that's still better
68 * than potentially deleting a different query with the same id in this
69 * context, and (still) leaking the actual query. */
70 if (This
->type
== WINED3DQUERYTYPE_EVENT
)
72 struct wined3d_event_query
*query
= This
->extendedData
;
74 if (query
->context
) context_free_event_query(query
);
76 else if (This
->type
== WINED3DQUERYTYPE_OCCLUSION
)
78 struct wined3d_occlusion_query
*query
= This
->extendedData
;
80 if (query
->context
) context_free_occlusion_query(query
);
83 HeapFree(GetProcessHeap(), 0, This
->extendedData
);
84 HeapFree(GetProcessHeap(), 0, This
);
89 /* *******************************************
90 IWineD3DQuery IWineD3DQuery parts follow
91 ******************************************* */
92 static HRESULT WINAPI
IWineD3DQueryImpl_GetParent(IWineD3DQuery
*iface
, IUnknown
**parent
)
94 TRACE("iface %p, parent %p.\n", iface
, parent
);
96 *parent
= (IUnknown
*)parent
;
97 IUnknown_AddRef(*parent
);
99 TRACE("Returning %p.\n", *parent
);
104 static HRESULT WINAPI
IWineD3DOcclusionQueryImpl_GetData(IWineD3DQuery
* iface
, void* pData
, DWORD dwSize
, DWORD dwGetDataFlags
) {
105 IWineD3DQueryImpl
*This
= (IWineD3DQueryImpl
*) iface
;
106 struct wined3d_occlusion_query
*query
= This
->extendedData
;
107 IWineD3DDeviceImpl
*device
= This
->device
;
108 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
109 struct wined3d_context
*context
;
115 TRACE("(%p) : type D3DQUERY_OCCLUSION, pData %p, dwSize %#x, dwGetDataFlags %#x\n", This
, pData
, dwSize
, dwGetDataFlags
);
117 if (!query
->context
) This
->state
= QUERY_CREATED
;
119 if (This
->state
== QUERY_CREATED
)
121 /* D3D allows GetData on a new query, OpenGL doesn't. So just invent the data ourselves */
122 TRACE("Query wasn't yet started, returning S_OK\n");
127 if (This
->state
== QUERY_BUILDING
)
129 /* Msdn says this returns an error, but our tests show that S_FALSE is returned */
130 TRACE("Query is building, returning S_FALSE\n");
134 if (!gl_info
->supported
[ARB_OCCLUSION_QUERY
])
136 WARN("(%p) : Occlusion queries not supported. Returning 1.\n", This
);
141 if (query
->context
->tid
!= GetCurrentThreadId())
143 FIXME("%p Wrong thread, returning 1.\n", This
);
148 context
= context_acquire(This
->device
, query
->context
->current_rt
, CTXUSAGE_RESOURCELOAD
);
152 GL_EXTCALL(glGetQueryObjectuivARB(query
->id
, GL_QUERY_RESULT_AVAILABLE_ARB
, &available
));
153 checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT_AVAILABLE)");
154 TRACE("(%p) : available %d.\n", This
, available
);
160 GL_EXTCALL(glGetQueryObjectuivARB(query
->id
, GL_QUERY_RESULT_ARB
, &samples
));
161 checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT)");
162 TRACE("(%p) : Returning %d samples.\n", This
, samples
);
174 context_release(context
);
179 static HRESULT WINAPI
IWineD3DEventQueryImpl_GetData(IWineD3DQuery
* iface
, void* pData
, DWORD dwSize
, DWORD dwGetDataFlags
) {
180 IWineD3DQueryImpl
*This
= (IWineD3DQueryImpl
*) iface
;
181 struct wined3d_event_query
*query
= This
->extendedData
;
182 const struct wined3d_gl_info
*gl_info
;
183 struct wined3d_context
*context
;
186 TRACE("(%p) : type D3DQUERY_EVENT, pData %p, dwSize %#x, dwGetDataFlags %#x\n", This
, pData
, dwSize
, dwGetDataFlags
);
188 if (!pData
|| !dwSize
) return S_OK
;
192 TRACE("Query not started, returning TRUE.\n");
198 if (!query
->context
->gl_info
->supported
[ARB_SYNC
] && query
->context
->tid
!= GetCurrentThreadId())
200 /* See comment in IWineD3DQuery::Issue, event query codeblock */
201 FIXME("Wrong thread, reporting GPU idle.\n");
207 context
= context_acquire(This
->device
, query
->context
->current_rt
, CTXUSAGE_RESOURCELOAD
);
208 gl_info
= context
->gl_info
;
212 if (gl_info
->supported
[ARB_SYNC
])
214 GLenum ret
= GL_EXTCALL(glClientWaitSync(query
->object
.sync
, 0, 0));
215 checkGLcall("glClientWaitSync");
219 case GL_ALREADY_SIGNALED
:
220 case GL_CONDITION_SATISFIED
:
224 case GL_TIMEOUT_EXPIRED
:
230 ERR("glClientWaitSync returned %#x.\n", ret
);
235 else if (gl_info
->supported
[APPLE_FENCE
])
237 *data
= GL_EXTCALL(glTestFenceAPPLE(query
->object
.id
));
238 checkGLcall("glTestFenceAPPLE");
240 else if (gl_info
->supported
[NV_FENCE
])
242 *data
= GL_EXTCALL(glTestFenceNV(query
->object
.id
));
243 checkGLcall("glTestFenceNV");
247 WARN("(%p): reporting GPU idle\n", This
);
253 context_release(context
);
258 static DWORD WINAPI
IWineD3DEventQueryImpl_GetDataSize(IWineD3DQuery
* iface
){
259 TRACE("(%p) : type D3DQUERY_EVENT\n", iface
);
264 static DWORD WINAPI
IWineD3DOcclusionQueryImpl_GetDataSize(IWineD3DQuery
* iface
){
265 TRACE("(%p) : type D3DQUERY_OCCLUSION\n", iface
);
267 return sizeof(DWORD
);
270 static WINED3DQUERYTYPE WINAPI
IWineD3DQueryImpl_GetType(IWineD3DQuery
* iface
){
271 IWineD3DQueryImpl
*This
= (IWineD3DQueryImpl
*)iface
;
276 static HRESULT WINAPI
IWineD3DEventQueryImpl_Issue(IWineD3DQuery
* iface
, DWORD dwIssueFlags
) {
277 IWineD3DQueryImpl
*This
= (IWineD3DQueryImpl
*)iface
;
279 TRACE("(%p) : dwIssueFlags %#x, type D3DQUERY_EVENT\n", This
, dwIssueFlags
);
280 if (dwIssueFlags
& WINED3DISSUE_END
)
282 struct wined3d_event_query
*query
= This
->extendedData
;
283 const struct wined3d_gl_info
*gl_info
;
284 struct wined3d_context
*context
;
288 if (!query
->context
->gl_info
->supported
[ARB_SYNC
] && query
->context
->tid
!= GetCurrentThreadId())
290 context_free_event_query(query
);
291 context
= context_acquire(This
->device
, NULL
, CTXUSAGE_RESOURCELOAD
);
292 context_alloc_event_query(context
, query
);
296 context
= context_acquire(This
->device
, query
->context
->current_rt
, CTXUSAGE_RESOURCELOAD
);
301 context
= context_acquire(This
->device
, NULL
, CTXUSAGE_RESOURCELOAD
);
302 context_alloc_event_query(context
, query
);
305 gl_info
= context
->gl_info
;
309 if (gl_info
->supported
[ARB_SYNC
])
311 if (query
->object
.sync
) GL_EXTCALL(glDeleteSync(query
->object
.sync
));
312 checkGLcall("glDeleteSync");
313 query
->object
.sync
= GL_EXTCALL(glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE
, 0));
314 checkGLcall("glFenceSync");
316 else if (gl_info
->supported
[APPLE_FENCE
])
318 GL_EXTCALL(glSetFenceAPPLE(query
->object
.id
));
319 checkGLcall("glSetFenceAPPLE");
321 else if (gl_info
->supported
[NV_FENCE
])
323 GL_EXTCALL(glSetFenceNV(query
->object
.id
, GL_ALL_COMPLETED_NV
));
324 checkGLcall("glSetFenceNV");
329 context_release(context
);
331 else if(dwIssueFlags
& WINED3DISSUE_BEGIN
)
333 /* Started implicitly at device creation */
334 ERR("Event query issued with START flag - what to do?\n");
337 if(dwIssueFlags
& WINED3DISSUE_BEGIN
) {
338 This
->state
= QUERY_BUILDING
;
340 This
->state
= QUERY_SIGNALLED
;
346 static HRESULT WINAPI
IWineD3DOcclusionQueryImpl_Issue(IWineD3DQuery
* iface
, DWORD dwIssueFlags
) {
347 IWineD3DQueryImpl
*This
= (IWineD3DQueryImpl
*)iface
;
348 IWineD3DDeviceImpl
*device
= This
->device
;
349 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
351 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
353 struct wined3d_occlusion_query
*query
= This
->extendedData
;
354 struct wined3d_context
*context
;
356 /* This is allowed according to msdn and our tests. Reset the query and restart */
357 if (dwIssueFlags
& WINED3DISSUE_BEGIN
)
359 if (This
->state
== QUERY_BUILDING
)
361 if (query
->context
->tid
!= GetCurrentThreadId())
363 FIXME("Wrong thread, can't restart query.\n");
365 context_free_occlusion_query(query
);
366 context
= context_acquire(This
->device
, NULL
, CTXUSAGE_RESOURCELOAD
);
367 context_alloc_occlusion_query(context
, query
);
371 context
= context_acquire(This
->device
, query
->context
->current_rt
, CTXUSAGE_RESOURCELOAD
);
374 GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB
));
375 checkGLcall("glEndQuery()");
381 if (query
->context
) context_free_occlusion_query(query
);
382 context
= context_acquire(This
->device
, NULL
, CTXUSAGE_RESOURCELOAD
);
383 context_alloc_occlusion_query(context
, query
);
387 GL_EXTCALL(glBeginQueryARB(GL_SAMPLES_PASSED_ARB
, query
->id
));
388 checkGLcall("glBeginQuery()");
391 context_release(context
);
393 if (dwIssueFlags
& WINED3DISSUE_END
) {
394 /* Msdn says _END on a non-building occlusion query returns an error, but
395 * our tests show that it returns OK. But OpenGL doesn't like it, so avoid
396 * generating an error
398 if (This
->state
== QUERY_BUILDING
)
400 if (query
->context
->tid
!= GetCurrentThreadId())
402 FIXME("Wrong thread, can't end query.\n");
406 context
= context_acquire(This
->device
, query
->context
->current_rt
, CTXUSAGE_RESOURCELOAD
);
409 GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB
));
410 checkGLcall("glEndQuery()");
413 context_release(context
);
418 FIXME("(%p) : Occlusion queries not supported\n", This
);
421 if(dwIssueFlags
& WINED3DISSUE_BEGIN
) {
422 This
->state
= QUERY_BUILDING
;
424 This
->state
= QUERY_SIGNALLED
;
426 return WINED3D_OK
; /* can be WINED3DERR_INVALIDCALL. */
429 static const IWineD3DQueryVtbl IWineD3DEventQuery_Vtbl
=
431 /*** IUnknown methods ***/
432 IWineD3DQueryImpl_QueryInterface
,
433 IWineD3DQueryImpl_AddRef
,
434 IWineD3DQueryImpl_Release
,
435 /*** IWineD3Dquery methods ***/
436 IWineD3DQueryImpl_GetParent
,
437 IWineD3DEventQueryImpl_GetData
,
438 IWineD3DEventQueryImpl_GetDataSize
,
439 IWineD3DQueryImpl_GetType
,
440 IWineD3DEventQueryImpl_Issue
443 static const IWineD3DQueryVtbl IWineD3DOcclusionQuery_Vtbl
=
445 /*** IUnknown methods ***/
446 IWineD3DQueryImpl_QueryInterface
,
447 IWineD3DQueryImpl_AddRef
,
448 IWineD3DQueryImpl_Release
,
449 /*** IWineD3Dquery methods ***/
450 IWineD3DQueryImpl_GetParent
,
451 IWineD3DOcclusionQueryImpl_GetData
,
452 IWineD3DOcclusionQueryImpl_GetDataSize
,
453 IWineD3DQueryImpl_GetType
,
454 IWineD3DOcclusionQueryImpl_Issue
457 HRESULT
query_init(IWineD3DQueryImpl
*query
, IWineD3DDeviceImpl
*device
,
458 WINED3DQUERYTYPE type
, IUnknown
*parent
)
460 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
464 case WINED3DQUERYTYPE_OCCLUSION
:
465 TRACE("Occlusion query.\n");
466 if (!gl_info
->supported
[ARB_OCCLUSION_QUERY
])
468 WARN("Unsupported in local OpenGL implementation: ARB_OCCLUSION_QUERY.\n");
469 return WINED3DERR_NOTAVAILABLE
;
471 query
->lpVtbl
= &IWineD3DOcclusionQuery_Vtbl
;
472 query
->extendedData
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct wined3d_occlusion_query
));
473 if (!query
->extendedData
)
475 ERR("Failed to allocate occlusion query extended data.\n");
476 return E_OUTOFMEMORY
;
478 ((struct wined3d_occlusion_query
*)query
->extendedData
)->context
= NULL
;
481 case WINED3DQUERYTYPE_EVENT
:
482 TRACE("Event query.\n");
483 if (!gl_info
->supported
[ARB_SYNC
] && !gl_info
->supported
[NV_FENCE
]
484 && !gl_info
->supported
[APPLE_FENCE
])
486 /* Half-Life 2 needs this query. It does not render the main
487 * menu correctly otherwise. Pretend to support it, faking
488 * this query does not do much harm except potentially
489 * lowering performance. */
490 FIXME("Event query: Unimplemented, but pretending to be supported.\n");
492 query
->lpVtbl
= &IWineD3DEventQuery_Vtbl
;
493 query
->extendedData
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct wined3d_event_query
));
494 if (!query
->extendedData
)
496 ERR("Failed to allocate event query extended data.\n");
497 return E_OUTOFMEMORY
;
499 ((struct wined3d_event_query
*)query
->extendedData
)->context
= NULL
;
502 case WINED3DQUERYTYPE_VCACHE
:
503 case WINED3DQUERYTYPE_RESOURCEMANAGER
:
504 case WINED3DQUERYTYPE_VERTEXSTATS
:
505 case WINED3DQUERYTYPE_TIMESTAMP
:
506 case WINED3DQUERYTYPE_TIMESTAMPDISJOINT
:
507 case WINED3DQUERYTYPE_TIMESTAMPFREQ
:
508 case WINED3DQUERYTYPE_PIPELINETIMINGS
:
509 case WINED3DQUERYTYPE_INTERFACETIMINGS
:
510 case WINED3DQUERYTYPE_VERTEXTIMINGS
:
511 case WINED3DQUERYTYPE_PIXELTIMINGS
:
512 case WINED3DQUERYTYPE_BANDWIDTHTIMINGS
:
513 case WINED3DQUERYTYPE_CACHEUTILIZATION
:
515 FIXME("Unhandled query type %#x.\n", type
);
516 return WINED3DERR_NOTAVAILABLE
;
520 query
->state
= QUERY_CREATED
;
521 query
->device
= device
;
522 query
->parent
= parent
;