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 static HRESULT
wined3d_event_query_init(const struct wined3d_gl_info
*gl_info
, struct wined3d_event_query
**query
)
32 struct wined3d_event_query
*ret
;
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
));
40 ERR("Failed to allocate a wined3d event query structure.\n");
48 static 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 static 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
;
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
;
80 if (gl_info
->supported
[ARB_SYNC
])
82 GLenum gl_ret
= GL_EXTCALL(glClientWaitSync(query
->object
.sync
, 0, 0));
83 checkGLcall("glClientWaitSync");
87 case GL_ALREADY_SIGNALED
:
88 case GL_CONDITION_SATISFIED
:
89 ret
= WINED3D_EVENT_QUERY_OK
;
92 case GL_TIMEOUT_EXPIRED
:
93 ret
= WINED3D_EVENT_QUERY_WAITING
;
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
;
118 ERR("Event query created despite lack of GL support\n");
119 ret
= WINED3D_EVENT_QUERY_ERROR
;
124 context_release(context
);
128 static void wined3d_event_query_issue(struct wined3d_event_query
*query
, IWineD3DDeviceImpl
*device
)
130 const struct wined3d_gl_info
*gl_info
;
131 struct wined3d_context
*context
;
135 if (!query
->context
->gl_info
->supported
[ARB_SYNC
] && query
->context
->tid
!= GetCurrentThreadId())
137 context_free_event_query(query
);
138 context
= context_acquire(device
, NULL
, CTXUSAGE_RESOURCELOAD
);
139 context_alloc_event_query(context
, query
);
143 context
= context_acquire(device
, query
->context
->current_rt
, CTXUSAGE_RESOURCELOAD
);
148 context
= context_acquire(device
, NULL
, CTXUSAGE_RESOURCELOAD
);
149 context_alloc_event_query(context
, query
);
152 gl_info
= context
->gl_info
;
156 if (gl_info
->supported
[ARB_SYNC
])
158 if (query
->object
.sync
) GL_EXTCALL(glDeleteSync(query
->object
.sync
));
159 checkGLcall("glDeleteSync");
160 query
->object
.sync
= GL_EXTCALL(glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE
, 0));
161 checkGLcall("glFenceSync");
163 else if (gl_info
->supported
[APPLE_FENCE
])
165 GL_EXTCALL(glSetFenceAPPLE(query
->object
.id
));
166 checkGLcall("glSetFenceAPPLE");
168 else if (gl_info
->supported
[NV_FENCE
])
170 GL_EXTCALL(glSetFenceNV(query
->object
.id
, GL_ALL_COMPLETED_NV
));
171 checkGLcall("glSetFenceNV");
176 context_release(context
);
181 * http://www.gris.uni-tuebingen.de/~bartz/Publications/paper/hww98.pdf
182 * http://oss.sgi.com/projects/ogl-sample/registry/ARB/occlusion_query.txt
185 /* *******************************************
186 IWineD3DQuery IUnknown parts follow
187 ******************************************* */
188 static HRESULT WINAPI
IWineD3DQueryImpl_QueryInterface(IWineD3DQuery
*iface
, REFIID riid
, LPVOID
*ppobj
)
190 IWineD3DQueryImpl
*This
= (IWineD3DQueryImpl
*)iface
;
191 TRACE("(%p)->(%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
192 if (IsEqualGUID(riid
, &IID_IUnknown
)
193 || IsEqualGUID(riid
, &IID_IWineD3DBase
)
194 || IsEqualGUID(riid
, &IID_IWineD3DQuery
)) {
195 IUnknown_AddRef(iface
);
200 return E_NOINTERFACE
;
203 static ULONG WINAPI
IWineD3DQueryImpl_AddRef(IWineD3DQuery
*iface
) {
204 IWineD3DQueryImpl
*This
= (IWineD3DQueryImpl
*)iface
;
205 TRACE("(%p) : AddRef increasing from %d\n", This
, This
->ref
);
206 return InterlockedIncrement(&This
->ref
);
209 static ULONG WINAPI
IWineD3DQueryImpl_Release(IWineD3DQuery
*iface
) {
210 IWineD3DQueryImpl
*This
= (IWineD3DQueryImpl
*)iface
;
212 TRACE("(%p) : Releasing from %d\n", This
, This
->ref
);
213 ref
= InterlockedDecrement(&This
->ref
);
215 /* Queries are specific to the GL context that created them. Not
216 * deleting the query will obviously leak it, but that's still better
217 * than potentially deleting a different query with the same id in this
218 * context, and (still) leaking the actual query. */
219 if (This
->type
== WINED3DQUERYTYPE_EVENT
)
221 struct wined3d_event_query
*query
= This
->extendedData
;
222 if (query
) wined3d_event_query_destroy(query
);
224 else if (This
->type
== WINED3DQUERYTYPE_OCCLUSION
)
226 struct wined3d_occlusion_query
*query
= This
->extendedData
;
228 if (query
->context
) context_free_occlusion_query(query
);
229 HeapFree(GetProcessHeap(), 0, This
->extendedData
);
232 HeapFree(GetProcessHeap(), 0, This
);
237 /* *******************************************
238 IWineD3DQuery IWineD3DQuery parts follow
239 ******************************************* */
240 static HRESULT WINAPI
IWineD3DQueryImpl_GetParent(IWineD3DQuery
*iface
, IUnknown
**parent
)
242 TRACE("iface %p, parent %p.\n", iface
, parent
);
244 *parent
= (IUnknown
*)parent
;
245 IUnknown_AddRef(*parent
);
247 TRACE("Returning %p.\n", *parent
);
252 static HRESULT WINAPI
IWineD3DOcclusionQueryImpl_GetData(IWineD3DQuery
* iface
, void* pData
, DWORD dwSize
, DWORD dwGetDataFlags
) {
253 IWineD3DQueryImpl
*This
= (IWineD3DQueryImpl
*) iface
;
254 struct wined3d_occlusion_query
*query
= This
->extendedData
;
255 IWineD3DDeviceImpl
*device
= This
->device
;
256 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
257 struct wined3d_context
*context
;
263 TRACE("(%p) : type D3DQUERY_OCCLUSION, pData %p, dwSize %#x, dwGetDataFlags %#x\n", This
, pData
, dwSize
, dwGetDataFlags
);
265 if (!query
->context
) This
->state
= QUERY_CREATED
;
267 if (This
->state
== QUERY_CREATED
)
269 /* D3D allows GetData on a new query, OpenGL doesn't. So just invent the data ourselves */
270 TRACE("Query wasn't yet started, returning S_OK\n");
275 if (This
->state
== QUERY_BUILDING
)
277 /* Msdn says this returns an error, but our tests show that S_FALSE is returned */
278 TRACE("Query is building, returning S_FALSE\n");
282 if (!gl_info
->supported
[ARB_OCCLUSION_QUERY
])
284 WARN("(%p) : Occlusion queries not supported. Returning 1.\n", This
);
289 if (query
->context
->tid
!= GetCurrentThreadId())
291 FIXME("%p Wrong thread, returning 1.\n", This
);
296 context
= context_acquire(This
->device
, query
->context
->current_rt
, CTXUSAGE_RESOURCELOAD
);
300 GL_EXTCALL(glGetQueryObjectuivARB(query
->id
, GL_QUERY_RESULT_AVAILABLE_ARB
, &available
));
301 checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT_AVAILABLE)");
302 TRACE("(%p) : available %d.\n", This
, available
);
308 GL_EXTCALL(glGetQueryObjectuivARB(query
->id
, GL_QUERY_RESULT_ARB
, &samples
));
309 checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT)");
310 TRACE("(%p) : Returning %d samples.\n", This
, samples
);
322 context_release(context
);
327 static HRESULT WINAPI
IWineD3DEventQueryImpl_GetData(IWineD3DQuery
* iface
, void* pData
, DWORD dwSize
, DWORD dwGetDataFlags
) {
328 IWineD3DQueryImpl
*This
= (IWineD3DQueryImpl
*) iface
;
329 struct wined3d_event_query
*query
= This
->extendedData
;
331 enum wined3d_event_query_result ret
;
333 TRACE("(%p) : type D3DQUERY_EVENT, pData %p, dwSize %#x, dwGetDataFlags %#x\n", This
, pData
, dwSize
, dwGetDataFlags
);
335 if (!pData
|| !dwSize
) return S_OK
;
338 WARN("(%p): Event query not supported by GL, reporting GPU idle\n", This
);
343 ret
= wined3d_event_query_test(query
, This
->device
);
346 case WINED3D_EVENT_QUERY_OK
:
347 case WINED3D_EVENT_QUERY_NOT_STARTED
:
351 case WINED3D_EVENT_QUERY_WAITING
:
355 case WINED3D_EVENT_QUERY_WRONG_THREAD
:
356 FIXME("(%p) Wrong thread, reporting GPU idle.\n", This
);
360 case WINED3D_EVENT_QUERY_ERROR
:
361 ERR("The GL event query failed, returning D3DERR_INVALIDCALL\n");
362 return WINED3DERR_INVALIDCALL
;
368 static DWORD WINAPI
IWineD3DEventQueryImpl_GetDataSize(IWineD3DQuery
* iface
){
369 TRACE("(%p) : type D3DQUERY_EVENT\n", iface
);
374 static DWORD WINAPI
IWineD3DOcclusionQueryImpl_GetDataSize(IWineD3DQuery
* iface
){
375 TRACE("(%p) : type D3DQUERY_OCCLUSION\n", iface
);
377 return sizeof(DWORD
);
380 static WINED3DQUERYTYPE WINAPI
IWineD3DQueryImpl_GetType(IWineD3DQuery
* iface
){
381 IWineD3DQueryImpl
*This
= (IWineD3DQueryImpl
*)iface
;
385 static HRESULT WINAPI
IWineD3DEventQueryImpl_Issue(IWineD3DQuery
* iface
, DWORD dwIssueFlags
) {
386 IWineD3DQueryImpl
*This
= (IWineD3DQueryImpl
*)iface
;
388 TRACE("(%p) : dwIssueFlags %#x, type D3DQUERY_EVENT\n", This
, dwIssueFlags
);
389 if (dwIssueFlags
& WINED3DISSUE_END
)
391 struct wined3d_event_query
*query
= This
->extendedData
;
393 /* Faked event query support */
394 if (!query
) return WINED3D_OK
;
396 wined3d_event_query_issue(query
, This
->device
);
398 else if(dwIssueFlags
& WINED3DISSUE_BEGIN
)
400 /* Started implicitly at device creation */
401 ERR("Event query issued with START flag - what to do?\n");
404 if(dwIssueFlags
& WINED3DISSUE_BEGIN
) {
405 This
->state
= QUERY_BUILDING
;
407 This
->state
= QUERY_SIGNALLED
;
413 static HRESULT WINAPI
IWineD3DOcclusionQueryImpl_Issue(IWineD3DQuery
* iface
, DWORD dwIssueFlags
) {
414 IWineD3DQueryImpl
*This
= (IWineD3DQueryImpl
*)iface
;
415 IWineD3DDeviceImpl
*device
= This
->device
;
416 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
418 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
420 struct wined3d_occlusion_query
*query
= This
->extendedData
;
421 struct wined3d_context
*context
;
423 /* This is allowed according to msdn and our tests. Reset the query and restart */
424 if (dwIssueFlags
& WINED3DISSUE_BEGIN
)
426 if (This
->state
== QUERY_BUILDING
)
428 if (query
->context
->tid
!= GetCurrentThreadId())
430 FIXME("Wrong thread, can't restart query.\n");
432 context_free_occlusion_query(query
);
433 context
= context_acquire(This
->device
, NULL
, CTXUSAGE_RESOURCELOAD
);
434 context_alloc_occlusion_query(context
, query
);
438 context
= context_acquire(This
->device
, query
->context
->current_rt
, CTXUSAGE_RESOURCELOAD
);
441 GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB
));
442 checkGLcall("glEndQuery()");
448 if (query
->context
) context_free_occlusion_query(query
);
449 context
= context_acquire(This
->device
, NULL
, CTXUSAGE_RESOURCELOAD
);
450 context_alloc_occlusion_query(context
, query
);
454 GL_EXTCALL(glBeginQueryARB(GL_SAMPLES_PASSED_ARB
, query
->id
));
455 checkGLcall("glBeginQuery()");
458 context_release(context
);
460 if (dwIssueFlags
& WINED3DISSUE_END
) {
461 /* Msdn says _END on a non-building occlusion query returns an error, but
462 * our tests show that it returns OK. But OpenGL doesn't like it, so avoid
463 * generating an error
465 if (This
->state
== QUERY_BUILDING
)
467 if (query
->context
->tid
!= GetCurrentThreadId())
469 FIXME("Wrong thread, can't end query.\n");
473 context
= context_acquire(This
->device
, query
->context
->current_rt
, CTXUSAGE_RESOURCELOAD
);
476 GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB
));
477 checkGLcall("glEndQuery()");
480 context_release(context
);
485 FIXME("(%p) : Occlusion queries not supported\n", This
);
488 if(dwIssueFlags
& WINED3DISSUE_BEGIN
) {
489 This
->state
= QUERY_BUILDING
;
491 This
->state
= QUERY_SIGNALLED
;
493 return WINED3D_OK
; /* can be WINED3DERR_INVALIDCALL. */
496 static const IWineD3DQueryVtbl IWineD3DEventQuery_Vtbl
=
498 /*** IUnknown methods ***/
499 IWineD3DQueryImpl_QueryInterface
,
500 IWineD3DQueryImpl_AddRef
,
501 IWineD3DQueryImpl_Release
,
502 /*** IWineD3Dquery methods ***/
503 IWineD3DQueryImpl_GetParent
,
504 IWineD3DEventQueryImpl_GetData
,
505 IWineD3DEventQueryImpl_GetDataSize
,
506 IWineD3DQueryImpl_GetType
,
507 IWineD3DEventQueryImpl_Issue
510 static const IWineD3DQueryVtbl IWineD3DOcclusionQuery_Vtbl
=
512 /*** IUnknown methods ***/
513 IWineD3DQueryImpl_QueryInterface
,
514 IWineD3DQueryImpl_AddRef
,
515 IWineD3DQueryImpl_Release
,
516 /*** IWineD3Dquery methods ***/
517 IWineD3DQueryImpl_GetParent
,
518 IWineD3DOcclusionQueryImpl_GetData
,
519 IWineD3DOcclusionQueryImpl_GetDataSize
,
520 IWineD3DQueryImpl_GetType
,
521 IWineD3DOcclusionQueryImpl_Issue
524 HRESULT
query_init(IWineD3DQueryImpl
*query
, IWineD3DDeviceImpl
*device
,
525 WINED3DQUERYTYPE type
, IUnknown
*parent
)
527 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
532 case WINED3DQUERYTYPE_OCCLUSION
:
533 TRACE("Occlusion query.\n");
534 if (!gl_info
->supported
[ARB_OCCLUSION_QUERY
])
536 WARN("Unsupported in local OpenGL implementation: ARB_OCCLUSION_QUERY.\n");
537 return WINED3DERR_NOTAVAILABLE
;
539 query
->lpVtbl
= &IWineD3DOcclusionQuery_Vtbl
;
540 query
->extendedData
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct wined3d_occlusion_query
));
541 if (!query
->extendedData
)
543 ERR("Failed to allocate occlusion query extended data.\n");
544 return E_OUTOFMEMORY
;
546 ((struct wined3d_occlusion_query
*)query
->extendedData
)->context
= NULL
;
549 case WINED3DQUERYTYPE_EVENT
:
550 TRACE("Event query.\n");
551 query
->lpVtbl
= &IWineD3DEventQuery_Vtbl
;
552 hr
= wined3d_event_query_init(gl_info
, (struct wined3d_event_query
**) &query
->extendedData
);
555 /* Half-Life 2 needs this query. It does not render the main
556 * menu correctly otherwise. Pretend to support it, faking
557 * this query does not do much harm except potentially
558 * lowering performance. */
559 FIXME("Event query: Unimplemented, but pretending to be supported.\n");
567 case WINED3DQUERYTYPE_VCACHE
:
568 case WINED3DQUERYTYPE_RESOURCEMANAGER
:
569 case WINED3DQUERYTYPE_VERTEXSTATS
:
570 case WINED3DQUERYTYPE_TIMESTAMP
:
571 case WINED3DQUERYTYPE_TIMESTAMPDISJOINT
:
572 case WINED3DQUERYTYPE_TIMESTAMPFREQ
:
573 case WINED3DQUERYTYPE_PIPELINETIMINGS
:
574 case WINED3DQUERYTYPE_INTERFACETIMINGS
:
575 case WINED3DQUERYTYPE_VERTEXTIMINGS
:
576 case WINED3DQUERYTYPE_PIXELTIMINGS
:
577 case WINED3DQUERYTYPE_BANDWIDTHTIMINGS
:
578 case WINED3DQUERYTYPE_CACHEUTILIZATION
:
580 FIXME("Unhandled query type %#x.\n", type
);
581 return WINED3DERR_NOTAVAILABLE
;
585 query
->state
= QUERY_CREATED
;
586 query
->device
= device
;
587 query
->parent
= parent
;