Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / angle / src / libGLESv2 / renderer / d3d / d3d11 / Query11.cpp
1 #include "precompiled.h"
2 //
3 // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
6 //
7
8 // Query11.cpp: Defines the rx::Query11 class which implements rx::QueryImpl.
9
10 #include "libGLESv2/renderer/d3d/d3d11/Query11.h"
11 #include "libGLESv2/renderer/d3d/d3d11/Renderer11.h"
12 #include "libGLESv2/renderer/d3d/d3d11/renderer11_utils.h"
13 #include "libGLESv2/main.h"
14
15 namespace rx
16 {
17
18 static bool checkOcclusionQuery(ID3D11DeviceContext *context, ID3D11Query *query, UINT64 *numPixels)
19 {
20     HRESULT result = context->GetData(query, numPixels, sizeof(UINT64), 0);
21     return (result == S_OK);
22 }
23
24 static bool checkStreamOutPrimitivesWritten(ID3D11DeviceContext *context, ID3D11Query *query, UINT64 *numPrimitives)
25 {
26     D3D11_QUERY_DATA_SO_STATISTICS soStats = { 0 };
27     HRESULT result = context->GetData(query, &soStats, sizeof(D3D11_QUERY_DATA_SO_STATISTICS), 0);
28     *numPrimitives = soStats.NumPrimitivesWritten;
29     return (result == S_OK);
30 }
31
32 Query11::Query11(rx::Renderer11 *renderer, GLenum type) : QueryImpl(type)
33 {
34     mRenderer = renderer;
35     mQuery = NULL;
36 }
37
38 Query11::~Query11()
39 {
40     SafeRelease(mQuery);
41 }
42
43 void Query11::begin()
44 {
45     if (mQuery == NULL)
46     {
47         D3D11_QUERY_DESC queryDesc;
48         queryDesc.Query = gl_d3d11::ConvertQueryType(getType());
49         queryDesc.MiscFlags = 0;
50
51         if (FAILED(mRenderer->getDevice()->CreateQuery(&queryDesc, &mQuery)))
52         {
53             return gl::error(GL_OUT_OF_MEMORY);
54         }
55     }
56
57     mRenderer->getDeviceContext()->Begin(mQuery);
58 }
59
60 void Query11::end()
61 {
62     ASSERT(mQuery);
63     mRenderer->getDeviceContext()->End(mQuery);
64
65     mStatus = GL_FALSE;
66     mResult = GL_FALSE;
67 }
68
69 GLuint Query11::getResult()
70 {
71     if (mQuery != NULL)
72     {
73         while (!testQuery())
74         {
75             Sleep(0);
76             // explicitly check for device loss, some drivers seem to return S_FALSE
77             // if the device is lost
78             if (mRenderer->testDeviceLost(true))
79             {
80                 return gl::error(GL_OUT_OF_MEMORY, 0);
81             }
82         }
83     }
84
85     return mResult;
86 }
87
88 GLboolean Query11::isResultAvailable()
89 {
90     if (mQuery != NULL)
91     {
92         testQuery();
93     }
94
95     return mStatus;
96 }
97
98 GLboolean Query11::testQuery()
99 {
100     if (mQuery != NULL && mStatus != GL_TRUE)
101     {
102         ID3D11DeviceContext *context = mRenderer->getDeviceContext();
103
104         bool queryFinished = false;
105         switch (getType())
106         {
107           case GL_ANY_SAMPLES_PASSED_EXT:
108           case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
109             {
110                 UINT64 numPixels = 0;
111                 queryFinished = checkOcclusionQuery(context, mQuery, &numPixels);
112                 if (queryFinished)
113                 {
114                     mResult = (numPixels > 0) ? GL_TRUE : GL_FALSE;
115                 }
116             }
117             break;
118
119           case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
120             {
121                 UINT64 numPrimitives = 0;
122                 queryFinished = checkStreamOutPrimitivesWritten(context, mQuery, &numPrimitives);
123                 if (queryFinished)
124                 {
125                     mResult = static_cast<GLuint>(numPrimitives);
126                 }
127             }
128             break;
129
130         default:
131             UNREACHABLE();
132             break;
133         }
134
135         if (queryFinished)
136         {
137             mStatus = GL_TRUE;
138         }
139         else if (mRenderer->testDeviceLost(true))
140         {
141             return gl::error(GL_OUT_OF_MEMORY, GL_TRUE);
142         }
143
144         return mStatus;
145     }
146
147     return GL_TRUE; // prevent blocking when query is null
148 }
149
150 bool Query11::isStarted() const
151 {
152     return (mQuery != NULL);
153 }
154
155 }