query.c revision 4237d5a79f48789aacc67dc43378d2d7813a39f4
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * Copyright 2005 Oliver Stieber
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * Copyright 2009-2010 Henri Verbeet for CodeWeavers.
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * This library is free software; you can redistribute it and/or
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * modify it under the terms of the GNU Lesser General Public
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * License as published by the Free Software Foundation; either
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * version 2.1 of the License, or (at your option) any later version.
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * This library is distributed in the hope that it will be useful,
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * but WITHOUT ANY WARRANTY; without even the implied warranty of
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * Lesser General Public License for more details.
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * You should have received a copy of the GNU Lesser General Public
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * License along with this library; if not, write to the Free Software
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
4237d5a79f48789aacc67dc43378d2d7813a39f4vboxsync * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
4237d5a79f48789aacc67dc43378d2d7813a39f4vboxsync * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
4237d5a79f48789aacc67dc43378d2d7813a39f4vboxsync * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
4237d5a79f48789aacc67dc43378d2d7813a39f4vboxsync * a choice of LGPL license versions is made available with the language indicating
4237d5a79f48789aacc67dc43378d2d7813a39f4vboxsync * that LGPLv2 or any later version may be used, or where a choice of which version
4237d5a79f48789aacc67dc43378d2d7813a39f4vboxsync * of the LGPL is applied is otherwise unspecified.
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncBOOL wined3d_event_query_supported(const struct wined3d_gl_info *gl_info)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync return gl_info->supported[ARB_SYNC] || gl_info->supported[NV_FENCE] || gl_info->supported[APPLE_FENCE];
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncvoid wined3d_event_query_destroy(struct wined3d_event_query *query)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (query->context) context_free_event_query(query);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic enum wined3d_event_query_result wined3d_event_query_test(const struct wined3d_event_query *query,
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (!query->context->gl_info->supported[ARB_SYNC] && query->context->tid != GetCurrentThreadId())
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync context = context_acquire(device, query->context->current_rt);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync GLenum gl_ret = GL_EXTCALL(glClientWaitSync(query->object.sync, 0, 0));
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync fence_result = GL_EXTCALL(glTestFenceAPPLE(query->object.id));
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync fence_result = GL_EXTCALL(glTestFenceNV(query->object.id));
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync /* doing Flush (rather than Finish) should be enough since we're serialized on the host in any way */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync ERR("Event query created despite lack of GL support\n");
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncenum wined3d_event_query_result wined3d_event_query_finish(const struct wined3d_event_query *query,
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (query->context->tid != GetCurrentThreadId() && !gl_info->supported[ARB_SYNC])
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync /* A glFinish does not reliably wait for draws in other contexts. The caller has
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * to find its own way to cope with the thread switch
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync context = context_acquire(device, query->context->current_rt);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync /* Apple seems to be into arbitrary limits, and timeouts larger than
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * 0xfffffffffffffbff immediately return GL_TIMEOUT_EXPIRED. We don't
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * really care and can live with waiting a few μs less. (OS X 10.7.4). */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync GLenum gl_ret = GL_EXTCALL(glClientWaitSync(query->object.sync, GL_SYNC_FLUSH_COMMANDS_BIT, ~(GLuint64)0xffff));
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync /* We don't expect a timeout for a ~584 year wait */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncvoid wined3d_event_query_issue(struct wined3d_event_query *query, const struct wined3d_device *device)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (!query->context->gl_info->supported[ARB_SYNC] && query->context->tid != GetCurrentThreadId())
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync context = context_acquire(device, query->context->current_rt);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (query->object.sync) GL_EXTCALL(glDeleteSync(query->object.sync));
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync query->object.sync = GL_EXTCALL(glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync GL_EXTCALL(glSetFenceNV(query->object.id, GL_ALL_COMPLETED_NV));
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncULONG CDECL wined3d_query_incref(struct wined3d_query *query)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync ULONG refcount = InterlockedIncrement(&query->ref);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync TRACE("%p increasing refcount to %u.\n", query, refcount);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncULONG CDECL wined3d_query_decref(struct wined3d_query *query)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync ULONG refcount = InterlockedDecrement(&query->ref);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync TRACE("%p decreasing refcount to %u.\n", query, refcount);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync /* Queries are specific to the GL context that created them. Not
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * deleting the query will obviously leak it, but that's still better
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * than potentially deleting a different query with the same id in this
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * context, and (still) leaking the actual query. */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync struct wined3d_event_query *event_query = query->extendedData;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (event_query) wined3d_event_query_destroy(event_query);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync else if (query->type == WINED3D_QUERY_TYPE_OCCLUSION)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync struct wined3d_occlusion_query *oq = query->extendedData;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync HeapFree(GetProcessHeap(), 0, query->extendedData);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncHRESULT CDECL wined3d_query_get_data(struct wined3d_query *query,
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync TRACE("query %p, data %p, data_size %u, flags %#x.\n",
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync return query->query_ops->query_get_data(query, data, data_size, flags);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncUINT CDECL wined3d_query_get_data_size(const struct wined3d_query *query)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncHRESULT CDECL wined3d_query_issue(struct wined3d_query *query, DWORD flags)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync return query->query_ops->query_issue(query, flags);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic HRESULT wined3d_occlusion_query_ops_get_data(struct wined3d_query *query,
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync struct wined3d_occlusion_query *oq = query->extendedData;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync TRACE("(%p) : type D3DQUERY_OCCLUSION, pData %p, dwSize %#x, flags %#x.\n", query, pData, dwSize, flags);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync /* D3D allows GetData on a new query, OpenGL doesn't. So just invent the data ourselves */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync TRACE("Query wasn't yet started, returning S_OK\n");
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync /* Msdn says this returns an error, but our tests show that S_FALSE is returned */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync WARN("%p Occlusion queries not supported. Returning 1.\n", query);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync context = context_acquire(query->device, oq->context->current_rt);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync GL_EXTCALL(glGetQueryObjectuivARB(oq->id, GL_QUERY_RESULT_AVAILABLE_ARB, &available));
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT_AVAILABLE)");
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync GL_EXTCALL(glGetQueryObjectuivARB(oq->id, GL_QUERY_RESULT_ARB, &samples));
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT)");
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic HRESULT wined3d_event_query_ops_get_data(struct wined3d_query *query,
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync struct wined3d_event_query *event_query = query->extendedData;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync TRACE("query %p, pData %p, dwSize %#x, flags %#x.\n", query, pData, dwSize, flags);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync WARN("Event query not supported by GL, reporting GPU idle.\n");
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync ret = wined3d_event_query_test(event_query, query->device);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync FIXME("(%p) Wrong thread, reporting GPU idle.\n", query);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync ERR("The GL event query failed, returning D3DERR_INVALIDCALL\n");
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncenum wined3d_query_type CDECL wined3d_query_get_type(const struct wined3d_query *query)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic HRESULT wined3d_event_query_ops_issue(struct wined3d_query *query, DWORD flags)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync TRACE("(%p) : flags %#x, type D3DQUERY_EVENT\n", query, flags);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync struct wined3d_event_query *event_query = query->extendedData;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync /* Faked event query support */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync wined3d_event_query_issue(event_query, query->device);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync /* Started implicitly at device creation */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync ERR("Event query issued with START flag - what to do?\n");
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic HRESULT wined3d_occlusion_query_ops_issue(struct wined3d_query *query, DWORD flags)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync struct wined3d_occlusion_query *oq = query->extendedData;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync /* This is allowed according to msdn and our tests. Reset the query and restart */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync context = context_acquire(query->device, oq->context->current_rt);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync GL_EXTCALL(glBeginQueryARB(GL_SAMPLES_PASSED_ARB, oq->id));
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync /* Msdn says _END on a non-building occlusion query returns an error, but
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * our tests show that it returns OK. But OpenGL doesn't like it, so avoid
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * generating an error
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync context = context_acquire(query->device, oq->context->current_rt);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync FIXME("%p Occlusion queries not supported.\n", query);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync return WINED3D_OK; /* can be WINED3DERR_INVALIDCALL. */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic const struct wined3d_query_ops event_query_ops =
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic const struct wined3d_query_ops occlusion_query_ops =
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic HRESULT query_init(struct wined3d_query *query, struct wined3d_device *device, enum wined3d_query_type type)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync WARN("Unsupported in local OpenGL implementation: ARB_OCCLUSION_QUERY.\n");
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync query->extendedData = HeapAlloc(GetProcessHeap(), 0, sizeof(struct wined3d_occlusion_query));
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync ERR("Failed to allocate occlusion query extended data.\n");
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync ((struct wined3d_occlusion_query *)query->extendedData)->context = NULL;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync /* Half-Life 2 needs this query. It does not render the main
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * menu correctly otherwise. Pretend to support it, faking
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * this query does not do much harm except potentially
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * lowering performance. */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync FIXME("Event query: Unimplemented, but pretending to be supported.\n");
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync query->extendedData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct wined3d_event_query));
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncHRESULT CDECL wined3d_query_create(struct wined3d_device *device,
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync enum wined3d_query_type type, struct wined3d_query **query)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync TRACE("device %p, type %#x, query %p.\n", device, type, query);