michael@0: #include "precompiled.h" michael@0: // michael@0: // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // Query11.cpp: Defines the rx::Query11 class which implements rx::QueryImpl. michael@0: michael@0: #include "libGLESv2/renderer/Query11.h" michael@0: #include "libGLESv2/renderer/Renderer11.h" michael@0: #include "libGLESv2/main.h" michael@0: michael@0: namespace rx michael@0: { michael@0: michael@0: Query11::Query11(rx::Renderer11 *renderer, GLenum type) : QueryImpl(type) michael@0: { michael@0: mRenderer = renderer; michael@0: mQuery = NULL; michael@0: } michael@0: michael@0: Query11::~Query11() michael@0: { michael@0: if (mQuery) michael@0: { michael@0: mQuery->Release(); michael@0: mQuery = NULL; michael@0: } michael@0: } michael@0: michael@0: void Query11::begin() michael@0: { michael@0: if (mQuery == NULL) michael@0: { michael@0: D3D11_QUERY_DESC queryDesc; michael@0: queryDesc.Query = D3D11_QUERY_OCCLUSION; michael@0: queryDesc.MiscFlags = 0; michael@0: michael@0: if (FAILED(mRenderer->getDevice()->CreateQuery(&queryDesc, &mQuery))) michael@0: { michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: } michael@0: michael@0: mRenderer->getDeviceContext()->Begin(mQuery); michael@0: } michael@0: michael@0: void Query11::end() michael@0: { michael@0: if (mQuery == NULL) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: michael@0: mRenderer->getDeviceContext()->End(mQuery); michael@0: michael@0: mStatus = GL_FALSE; michael@0: mResult = GL_FALSE; michael@0: } michael@0: michael@0: GLuint Query11::getResult() michael@0: { michael@0: if (mQuery != NULL) michael@0: { michael@0: while (!testQuery()) michael@0: { michael@0: Sleep(0); michael@0: // explicitly check for device loss, some drivers seem to return S_FALSE michael@0: // if the device is lost michael@0: if (mRenderer->testDeviceLost(true)) michael@0: { michael@0: return gl::error(GL_OUT_OF_MEMORY, 0); michael@0: } michael@0: } michael@0: } michael@0: michael@0: return mResult; michael@0: } michael@0: michael@0: GLboolean Query11::isResultAvailable() michael@0: { michael@0: if (mQuery != NULL) michael@0: { michael@0: testQuery(); michael@0: } michael@0: michael@0: return mStatus; michael@0: } michael@0: michael@0: GLboolean Query11::testQuery() michael@0: { michael@0: if (mQuery != NULL && mStatus != GL_TRUE) michael@0: { michael@0: UINT64 numPixels = 0; michael@0: HRESULT result = mRenderer->getDeviceContext()->GetData(mQuery, &numPixels, sizeof(UINT64), 0); michael@0: if (result == S_OK) michael@0: { michael@0: mStatus = GL_TRUE; michael@0: michael@0: switch (getType()) michael@0: { michael@0: case GL_ANY_SAMPLES_PASSED_EXT: michael@0: case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT: michael@0: mResult = (numPixels > 0) ? GL_TRUE : GL_FALSE; michael@0: break; michael@0: default: michael@0: UNREACHABLE(); michael@0: } michael@0: } michael@0: else if (mRenderer->testDeviceLost(true)) michael@0: { michael@0: return gl::error(GL_OUT_OF_MEMORY, GL_TRUE); michael@0: } michael@0: michael@0: return mStatus; michael@0: } michael@0: michael@0: return GL_TRUE; // prevent blocking when query is null michael@0: } michael@0: michael@0: }