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
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
;
47 TRACE("(%p) : device %p\n", query
, device
);
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
;
66 if (gl_info
->supported
[ARB_SYNC
])
68 GLenum gl_ret
= GL_EXTCALL(glClientWaitSync(query
->object
.sync
, 0, 0));
69 checkGLcall("glClientWaitSync");
73 case GL_ALREADY_SIGNALED
:
74 case GL_CONDITION_SATISFIED
:
75 ret
= WINED3D_EVENT_QUERY_OK
;
78 case GL_TIMEOUT_EXPIRED
:
79 ret
= WINED3D_EVENT_QUERY_WAITING
;
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
;
104 ERR("Event query created despite lack of GL support\n");
105 ret
= WINED3D_EVENT_QUERY_ERROR
;
110 context_release(context
);
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
);
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
);
141 if (gl_info
->supported
[ARB_SYNC
])
143 GLenum gl_ret
= GL_EXTCALL(glClientWaitSync(query
->object
.sync
, 0, ~(GLuint64
)0));
144 checkGLcall("glClientWaitSync");
148 case GL_ALREADY_SIGNALED
:
149 case GL_CONDITION_SATISFIED
:
150 ret
= WINED3D_EVENT_QUERY_OK
;
153 /* We don't expect a timeout for a ~584 year wait */
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
;
173 ERR("Event query created without GL support\n");
174 ret
= WINED3D_EVENT_QUERY_ERROR
;
178 context_release(context
);
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
;
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
);
197 context
= context_acquire(device
, query
->context
->current_rt
);
202 context
= context_acquire(device
, NULL
);
203 context_alloc_event_query(context
, query
);
206 gl_info
= context
->gl_info
;
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");
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
);
245 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
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
;
260 TRACE("(%p) : Releasing from %d\n", This
, This
->ref
);
261 ref
= InterlockedDecrement(&This
->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
);
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
;
328 TRACE("(%p) : type D3DQUERY_OCCLUSION, pData %p, dwSize %#x, flags %#x.\n", query
, pData
, dwSize
, flags
);
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");
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");
348 if (!gl_info
->supported
[ARB_OCCLUSION_QUERY
])
350 WARN("%p Occlusion queries not supported. Returning 1.\n", query
);
355 if (oq
->context
->tid
!= GetCurrentThreadId())
357 FIXME("%p Wrong thread, returning 1.\n", query
);
362 context
= context_acquire(query
->device
, oq
->context
->current_rt
);
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
);
374 GL_EXTCALL(glGetQueryObjectuivARB(oq
->id
, GL_QUERY_RESULT_ARB
, &samples
));
375 checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT)");
376 TRACE("Returning %d samples.\n", samples
);
388 context_release(context
);
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
;
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
;
405 WARN("Event query not supported by GL, reporting GPU idle.\n");
410 ret
= wined3d_event_query_test(event_query
, query
->device
);
413 case WINED3D_EVENT_QUERY_OK
:
414 case WINED3D_EVENT_QUERY_NOT_STARTED
:
418 case WINED3D_EVENT_QUERY_WAITING
:
422 case WINED3D_EVENT_QUERY_WRONG_THREAD
:
423 FIXME("(%p) Wrong thread, reporting GPU idle.\n", query
);
427 case WINED3D_EVENT_QUERY_ERROR
:
428 ERR("The GL event query failed, returning D3DERR_INVALIDCALL\n");
429 return WINED3DERR_INVALIDCALL
;
435 static WINED3DQUERYTYPE WINAPI
IWineD3DQueryImpl_GetType(IWineD3DQuery
* iface
){
436 IWineD3DQueryImpl
*This
= (IWineD3DQueryImpl
*)iface
;
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
;
463 query
->state
= QUERY_SIGNALLED
;
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
);
495 context
= context_acquire(query
->device
, oq
->context
->current_rt
);
498 GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB
));
499 checkGLcall("glEndQuery()");
505 if (oq
->context
) context_free_occlusion_query(oq
);
506 context
= context_acquire(query
->device
, NULL
);
507 context_alloc_occlusion_query(context
, oq
);
511 GL_EXTCALL(glBeginQueryARB(GL_SAMPLES_PASSED_ARB
, oq
->id
));
512 checkGLcall("glBeginQuery()");
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");
531 context
= context_acquire(query
->device
, oq
->context
->current_rt
);
534 GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB
));
535 checkGLcall("glEndQuery()");
538 context_release(context
);
545 FIXME("%p Occlusion queries not supported.\n", query
);
548 if (flags
& WINED3DISSUE_BEGIN
)
549 query
->state
= QUERY_BUILDING
;
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
;
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
;
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
;
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
:
638 FIXME("Unhandled query type %#x.\n", type
);
639 return WINED3DERR_NOTAVAILABLE
;
642 query
->lpVtbl
= &IWineD3DQuery_Vtbl
;
644 query
->state
= QUERY_CREATED
;
645 query
->device
= device
;