2 * Copyright 2005 Oliver Stieber
3 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
4 * Copyright 2009-2010 Henri Verbeet for CodeWeavers.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wined3d_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
27 BOOL
wined3d_event_query_supported(const struct wined3d_gl_info
*gl_info
)
29 return gl_info
->supported
[ARB_SYNC
] || gl_info
->supported
[NV_FENCE
] || gl_info
->supported
[APPLE_FENCE
];
32 void wined3d_event_query_destroy(struct wined3d_event_query
*query
)
34 if (query
->context
) context_free_event_query(query
);
35 HeapFree(GetProcessHeap(), 0, query
);
38 static enum wined3d_event_query_result
wined3d_event_query_test(const struct wined3d_event_query
*query
,
39 struct wined3d_device
*device
)
41 struct wined3d_context
*context
;
42 const struct wined3d_gl_info
*gl_info
;
43 enum wined3d_event_query_result ret
;
46 TRACE("(%p) : device %p\n", query
, device
);
50 TRACE("Query not started\n");
51 return WINED3D_EVENT_QUERY_NOT_STARTED
;
54 if (!query
->context
->gl_info
->supported
[ARB_SYNC
] && query
->context
->tid
!= GetCurrentThreadId())
56 WARN("Event query tested from wrong thread\n");
57 return WINED3D_EVENT_QUERY_WRONG_THREAD
;
60 context
= context_acquire(device
, query
->context
->current_rt
);
61 gl_info
= context
->gl_info
;
65 if (gl_info
->supported
[ARB_SYNC
])
67 GLenum gl_ret
= GL_EXTCALL(glClientWaitSync(query
->object
.sync
, 0, 0));
68 checkGLcall("glClientWaitSync");
72 case GL_ALREADY_SIGNALED
:
73 case GL_CONDITION_SATISFIED
:
74 ret
= WINED3D_EVENT_QUERY_OK
;
77 case GL_TIMEOUT_EXPIRED
:
78 ret
= WINED3D_EVENT_QUERY_WAITING
;
83 ERR("glClientWaitSync returned %#x.\n", gl_ret
);
84 ret
= WINED3D_EVENT_QUERY_ERROR
;
87 else if (gl_info
->supported
[APPLE_FENCE
])
89 fence_result
= GL_EXTCALL(glTestFenceAPPLE(query
->object
.id
));
90 checkGLcall("glTestFenceAPPLE");
91 if (fence_result
) ret
= WINED3D_EVENT_QUERY_OK
;
92 else ret
= WINED3D_EVENT_QUERY_WAITING
;
94 else if (gl_info
->supported
[NV_FENCE
])
96 fence_result
= GL_EXTCALL(glTestFenceNV(query
->object
.id
));
97 checkGLcall("glTestFenceNV");
98 if (fence_result
) ret
= WINED3D_EVENT_QUERY_OK
;
99 else ret
= WINED3D_EVENT_QUERY_WAITING
;
103 ERR("Event query created despite lack of GL support\n");
104 ret
= WINED3D_EVENT_QUERY_ERROR
;
109 context_release(context
);
113 enum wined3d_event_query_result
wined3d_event_query_finish(const struct wined3d_event_query
*query
,
114 struct wined3d_device
*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
, struct wined3d_device
*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 ULONG CDECL
wined3d_query_incref(struct wined3d_query
*query
)
235 ULONG refcount
= InterlockedIncrement(&query
->ref
);
237 TRACE("%p increasing refcount to %u.\n", query
, refcount
);
242 ULONG CDECL
wined3d_query_decref(struct wined3d_query
*query
)
244 ULONG refcount
= InterlockedDecrement(&query
->ref
);
246 TRACE("%p decreasing refcount to %u.\n", query
, refcount
);
250 /* Queries are specific to the GL context that created them. Not
251 * deleting the query will obviously leak it, but that's still better
252 * than potentially deleting a different query with the same id in this
253 * context, and (still) leaking the actual query. */
254 if (query
->type
== WINED3DQUERYTYPE_EVENT
)
256 struct wined3d_event_query
*event_query
= query
->extendedData
;
257 if (event_query
) wined3d_event_query_destroy(event_query
);
259 else if (query
->type
== WINED3DQUERYTYPE_OCCLUSION
)
261 struct wined3d_occlusion_query
*oq
= query
->extendedData
;
263 if (oq
->context
) context_free_occlusion_query(oq
);
264 HeapFree(GetProcessHeap(), 0, query
->extendedData
);
267 HeapFree(GetProcessHeap(), 0, query
);
273 HRESULT CDECL
wined3d_query_get_data(struct wined3d_query
*query
,
274 void *data
, UINT data_size
, DWORD flags
)
276 TRACE("query %p, data %p, data_size %u, flags %#x.\n",
277 query
, data
, data_size
, flags
);
279 return query
->query_ops
->query_get_data(query
, data
, data_size
, flags
);
282 UINT CDECL
wined3d_query_get_data_size(const struct wined3d_query
*query
)
284 TRACE("query %p.\n", query
);
286 return query
->data_size
;
289 HRESULT CDECL
wined3d_query_issue(struct wined3d_query
*query
, DWORD flags
)
291 TRACE("query %p, flags %#x.\n", query
, flags
);
293 return query
->query_ops
->query_issue(query
, flags
);
296 static HRESULT
wined3d_occlusion_query_ops_get_data(struct wined3d_query
*query
,
297 void *pData
, DWORD dwSize
, DWORD flags
)
299 struct wined3d_occlusion_query
*oq
= query
->extendedData
;
300 struct wined3d_device
*device
= query
->device
;
301 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
302 struct wined3d_context
*context
;
308 TRACE("(%p) : type D3DQUERY_OCCLUSION, pData %p, dwSize %#x, flags %#x.\n", query
, pData
, dwSize
, flags
);
311 query
->state
= QUERY_CREATED
;
313 if (query
->state
== QUERY_CREATED
)
315 /* D3D allows GetData on a new query, OpenGL doesn't. So just invent the data ourselves */
316 TRACE("Query wasn't yet started, returning S_OK\n");
321 if (query
->state
== QUERY_BUILDING
)
323 /* Msdn says this returns an error, but our tests show that S_FALSE is returned */
324 TRACE("Query is building, returning S_FALSE\n");
328 if (!gl_info
->supported
[ARB_OCCLUSION_QUERY
])
330 WARN("%p Occlusion queries not supported. Returning 1.\n", query
);
335 if (oq
->context
->tid
!= GetCurrentThreadId())
337 FIXME("%p Wrong thread, returning 1.\n", query
);
342 context
= context_acquire(query
->device
, oq
->context
->current_rt
);
346 GL_EXTCALL(glGetQueryObjectuivARB(oq
->id
, GL_QUERY_RESULT_AVAILABLE_ARB
, &available
));
347 checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT_AVAILABLE)");
348 TRACE("available %#x.\n", available
);
354 GL_EXTCALL(glGetQueryObjectuivARB(oq
->id
, GL_QUERY_RESULT_ARB
, &samples
));
355 checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT)");
356 TRACE("Returning %d samples.\n", samples
);
368 context_release(context
);
373 static HRESULT
wined3d_event_query_ops_get_data(struct wined3d_query
*query
,
374 void *pData
, DWORD dwSize
, DWORD flags
)
376 struct wined3d_event_query
*event_query
= query
->extendedData
;
378 enum wined3d_event_query_result ret
;
380 TRACE("query %p, pData %p, dwSize %#x, flags %#x.\n", query
, pData
, dwSize
, flags
);
382 if (!pData
|| !dwSize
) return S_OK
;
385 WARN("Event query not supported by GL, reporting GPU idle.\n");
390 ret
= wined3d_event_query_test(event_query
, query
->device
);
393 case WINED3D_EVENT_QUERY_OK
:
394 case WINED3D_EVENT_QUERY_NOT_STARTED
:
398 case WINED3D_EVENT_QUERY_WAITING
:
402 case WINED3D_EVENT_QUERY_WRONG_THREAD
:
403 FIXME("(%p) Wrong thread, reporting GPU idle.\n", query
);
407 case WINED3D_EVENT_QUERY_ERROR
:
408 ERR("The GL event query failed, returning D3DERR_INVALIDCALL\n");
409 return WINED3DERR_INVALIDCALL
;
415 WINED3DQUERYTYPE CDECL
wined3d_query_get_type(const struct wined3d_query
*query
)
417 TRACE("query %p.\n", query
);
422 static HRESULT
wined3d_event_query_ops_issue(struct wined3d_query
*query
, DWORD flags
)
424 TRACE("query %p, flags %#x.\n", query
, flags
);
426 TRACE("(%p) : flags %#x, type D3DQUERY_EVENT\n", query
, flags
);
427 if (flags
& WINED3DISSUE_END
)
429 struct wined3d_event_query
*event_query
= query
->extendedData
;
431 /* Faked event query support */
432 if (!event_query
) return WINED3D_OK
;
434 wined3d_event_query_issue(event_query
, query
->device
);
436 else if (flags
& WINED3DISSUE_BEGIN
)
438 /* Started implicitly at device creation */
439 ERR("Event query issued with START flag - what to do?\n");
442 if (flags
& WINED3DISSUE_BEGIN
)
443 query
->state
= QUERY_BUILDING
;
445 query
->state
= QUERY_SIGNALLED
;
450 static HRESULT
wined3d_occlusion_query_ops_issue(struct wined3d_query
*query
, DWORD flags
)
452 struct wined3d_device
*device
= query
->device
;
453 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
455 TRACE("query %p, flags %#x.\n", query
, flags
);
457 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
459 struct wined3d_occlusion_query
*oq
= query
->extendedData
;
460 struct wined3d_context
*context
;
462 /* This is allowed according to msdn and our tests. Reset the query and restart */
463 if (flags
& WINED3DISSUE_BEGIN
)
465 if (query
->state
== QUERY_BUILDING
)
467 if (oq
->context
->tid
!= GetCurrentThreadId())
469 FIXME("Wrong thread, can't restart query.\n");
471 context_free_occlusion_query(oq
);
472 context
= context_acquire(query
->device
, NULL
);
473 context_alloc_occlusion_query(context
, oq
);
477 context
= context_acquire(query
->device
, oq
->context
->current_rt
);
480 GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB
));
481 checkGLcall("glEndQuery()");
487 if (oq
->context
) context_free_occlusion_query(oq
);
488 context
= context_acquire(query
->device
, NULL
);
489 context_alloc_occlusion_query(context
, oq
);
493 GL_EXTCALL(glBeginQueryARB(GL_SAMPLES_PASSED_ARB
, oq
->id
));
494 checkGLcall("glBeginQuery()");
497 context_release(context
);
499 if (flags
& WINED3DISSUE_END
)
501 /* Msdn says _END on a non-building occlusion query returns an error, but
502 * our tests show that it returns OK. But OpenGL doesn't like it, so avoid
503 * generating an error
505 if (query
->state
== QUERY_BUILDING
)
507 if (oq
->context
->tid
!= GetCurrentThreadId())
509 FIXME("Wrong thread, can't end query.\n");
513 context
= context_acquire(query
->device
, oq
->context
->current_rt
);
516 GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB
));
517 checkGLcall("glEndQuery()");
520 context_release(context
);
527 FIXME("%p Occlusion queries not supported.\n", query
);
530 if (flags
& WINED3DISSUE_BEGIN
)
531 query
->state
= QUERY_BUILDING
;
533 query
->state
= QUERY_SIGNALLED
;
535 return WINED3D_OK
; /* can be WINED3DERR_INVALIDCALL. */
538 static const struct wined3d_query_ops event_query_ops
=
540 wined3d_event_query_ops_get_data
,
541 wined3d_event_query_ops_issue
,
544 static const struct wined3d_query_ops occlusion_query_ops
=
546 wined3d_occlusion_query_ops_get_data
,
547 wined3d_occlusion_query_ops_issue
,
550 static HRESULT
query_init(struct wined3d_query
*query
, struct wined3d_device
*device
, WINED3DQUERYTYPE type
)
552 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
556 case WINED3DQUERYTYPE_OCCLUSION
:
557 TRACE("Occlusion query.\n");
558 if (!gl_info
->supported
[ARB_OCCLUSION_QUERY
])
560 WARN("Unsupported in local OpenGL implementation: ARB_OCCLUSION_QUERY.\n");
561 return WINED3DERR_NOTAVAILABLE
;
563 query
->query_ops
= &occlusion_query_ops
;
564 query
->data_size
= sizeof(DWORD
);
565 query
->extendedData
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct wined3d_occlusion_query
));
566 if (!query
->extendedData
)
568 ERR("Failed to allocate occlusion query extended data.\n");
569 return E_OUTOFMEMORY
;
571 ((struct wined3d_occlusion_query
*)query
->extendedData
)->context
= NULL
;
574 case WINED3DQUERYTYPE_EVENT
:
575 TRACE("Event query.\n");
576 if (!wined3d_event_query_supported(gl_info
))
578 /* Half-Life 2 needs this query. It does not render the main
579 * menu correctly otherwise. Pretend to support it, faking
580 * this query does not do much harm except potentially
581 * lowering performance. */
582 FIXME("Event query: Unimplemented, but pretending to be supported.\n");
584 query
->query_ops
= &event_query_ops
;
585 query
->data_size
= sizeof(BOOL
);
586 query
->extendedData
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct wined3d_event_query
));
587 if (!query
->extendedData
)
589 ERR("Failed to allocate event query memory.\n");
590 return E_OUTOFMEMORY
;
594 case WINED3DQUERYTYPE_VCACHE
:
595 case WINED3DQUERYTYPE_RESOURCEMANAGER
:
596 case WINED3DQUERYTYPE_VERTEXSTATS
:
597 case WINED3DQUERYTYPE_TIMESTAMP
:
598 case WINED3DQUERYTYPE_TIMESTAMPDISJOINT
:
599 case WINED3DQUERYTYPE_TIMESTAMPFREQ
:
600 case WINED3DQUERYTYPE_PIPELINETIMINGS
:
601 case WINED3DQUERYTYPE_INTERFACETIMINGS
:
602 case WINED3DQUERYTYPE_VERTEXTIMINGS
:
603 case WINED3DQUERYTYPE_PIXELTIMINGS
:
604 case WINED3DQUERYTYPE_BANDWIDTHTIMINGS
:
605 case WINED3DQUERYTYPE_CACHEUTILIZATION
:
607 FIXME("Unhandled query type %#x.\n", type
);
608 return WINED3DERR_NOTAVAILABLE
;
612 query
->state
= QUERY_CREATED
;
613 query
->device
= device
;
619 HRESULT CDECL
wined3d_query_create(struct wined3d_device
*device
,
620 WINED3DQUERYTYPE type
, struct wined3d_query
**query
)
622 struct wined3d_query
*object
;
625 TRACE("device %p, type %#x, query %p.\n", device
, type
, query
);
627 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
630 ERR("Failed to allocate query memory.\n");
631 return E_OUTOFMEMORY
;
634 hr
= query_init(object
, device
, type
);
637 WARN("Failed to initialize query, hr %#x.\n", hr
);
638 HeapFree(GetProcessHeap(), 0, object
);
642 TRACE("Created query %p.\n", object
);