1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
//
// Copyright (c) 2013 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Query11.cpp: Defines the rx::Query11 class which implements rx::QueryImpl.
#include "libANGLE/renderer/d3d/d3d11/Query11.h"
#include <GLES2/gl2ext.h>
#include "common/utilities.h"
#include "libANGLE/renderer/d3d/d3d11/Renderer11.h"
#include "libANGLE/renderer/d3d/d3d11/renderer11_utils.h"
namespace
{
GLuint64 MergeQueryResults(GLenum type, GLuint64 currentResult, GLuint64 newResult)
{
switch (type)
{
case GL_ANY_SAMPLES_PASSED:
case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
return (currentResult == GL_TRUE || newResult == GL_TRUE) ? GL_TRUE : GL_FALSE;
case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
return currentResult + newResult;
case GL_TIME_ELAPSED_EXT:
return currentResult + newResult;
case GL_TIMESTAMP_EXT:
return newResult;
case GL_COMMANDS_COMPLETED_CHROMIUM:
return newResult;
default:
UNREACHABLE();
return 0;
}
}
} // anonymous namespace
namespace rx
{
Query11::QueryState::QueryState()
: query(nullptr), beginTimestamp(nullptr), endTimestamp(nullptr), finished(false)
{
}
Query11::QueryState::~QueryState()
{
SafeRelease(beginTimestamp);
SafeRelease(endTimestamp);
SafeRelease(query);
}
Query11::Query11(Renderer11 *renderer, GLenum type)
: QueryImpl(type), mResult(0), mResultSum(0), mRenderer(renderer)
{
mActiveQuery = std::unique_ptr<QueryState>(new QueryState());
}
Query11::~Query11()
{
mRenderer->getStateManager()->onDeleteQueryObject(this);
}
gl::Error Query11::begin()
{
mResultSum = 0;
mRenderer->getStateManager()->onBeginQuery(this);
return resume();
}
gl::Error Query11::end()
{
return pause();
}
gl::Error Query11::queryCounter()
{
// This doesn't do anything for D3D11 as we don't support timestamps
ASSERT(getType() == GL_TIMESTAMP_EXT);
mResultSum = 0;
mPendingQueries.push_back(std::unique_ptr<QueryState>(new QueryState()));
return gl::Error(GL_NO_ERROR);
}
template <typename T>
gl::Error Query11::getResultBase(T *params)
{
ASSERT(mActiveQuery->query == nullptr);
ANGLE_TRY(flush(true));
ASSERT(mPendingQueries.empty());
*params = static_cast<T>(mResultSum);
return gl::NoError();
}
gl::Error Query11::getResult(GLint *params)
{
return getResultBase(params);
}
gl::Error Query11::getResult(GLuint *params)
{
return getResultBase(params);
}
gl::Error Query11::getResult(GLint64 *params)
{
return getResultBase(params);
}
gl::Error Query11::getResult(GLuint64 *params)
{
return getResultBase(params);
}
gl::Error Query11::isResultAvailable(bool *available)
{
ANGLE_TRY(flush(false));
*available = mPendingQueries.empty();
return gl::NoError();
}
gl::Error Query11::pause()
{
if (mActiveQuery->query != nullptr)
{
ID3D11DeviceContext *context = mRenderer->getDeviceContext();
GLenum queryType = getType();
// If we are doing time elapsed query the end timestamp
if (queryType == GL_TIME_ELAPSED_EXT)
{
context->End(mActiveQuery->endTimestamp);
}
context->End(mActiveQuery->query);
mPendingQueries.push_back(std::move(mActiveQuery));
mActiveQuery = std::unique_ptr<QueryState>(new QueryState());
}
return flush(false);
}
gl::Error Query11::resume()
{
if (mActiveQuery->query == nullptr)
{
ANGLE_TRY(flush(false));
GLenum queryType = getType();
D3D11_QUERY d3dQueryType = gl_d3d11::ConvertQueryType(queryType);
D3D11_QUERY_DESC queryDesc;
queryDesc.Query = d3dQueryType;
queryDesc.MiscFlags = 0;
ID3D11Device *device = mRenderer->getDevice();
HRESULT result = device->CreateQuery(&queryDesc, &mActiveQuery->query);
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "Internal query creation failed, result: 0x%X.",
result);
}
// If we are doing time elapsed we also need a query to actually query the timestamp
if (queryType == GL_TIME_ELAPSED_EXT)
{
D3D11_QUERY_DESC desc;
desc.Query = D3D11_QUERY_TIMESTAMP;
desc.MiscFlags = 0;
result = device->CreateQuery(&desc, &mActiveQuery->beginTimestamp);
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "Internal query creation failed, result: 0x%X.",
result);
}
result = device->CreateQuery(&desc, &mActiveQuery->endTimestamp);
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "Internal query creation failed, result: 0x%X.",
result);
}
}
ID3D11DeviceContext *context = mRenderer->getDeviceContext();
if (d3dQueryType != D3D11_QUERY_EVENT)
{
context->Begin(mActiveQuery->query);
}
// If we are doing time elapsed, query the begin timestamp
if (queryType == GL_TIME_ELAPSED_EXT)
{
context->End(mActiveQuery->beginTimestamp);
}
}
return gl::Error(GL_NO_ERROR);
}
gl::Error Query11::flush(bool force)
{
while (!mPendingQueries.empty())
{
QueryState *query = mPendingQueries.front().get();
do
{
ANGLE_TRY(testQuery(query));
if (!query->finished && !force)
{
return gl::Error(GL_NO_ERROR);
}
} while (!query->finished);
mResultSum = MergeQueryResults(getType(), mResultSum, mResult);
mPendingQueries.pop_front();
}
return gl::NoError();
}
gl::Error Query11::testQuery(QueryState *queryState)
{
if (!queryState->finished)
{
ID3D11DeviceContext *context = mRenderer->getDeviceContext();
switch (getType())
{
case GL_ANY_SAMPLES_PASSED_EXT:
case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
{
ASSERT(queryState->query);
UINT64 numPixels = 0;
HRESULT result =
context->GetData(queryState->query, &numPixels, sizeof(numPixels), 0);
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to get the data of an internal query, result: 0x%X.",
result);
}
if (result == S_OK)
{
queryState->finished = true;
mResult = (numPixels > 0) ? GL_TRUE : GL_FALSE;
}
}
break;
case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
{
ASSERT(queryState->query);
D3D11_QUERY_DATA_SO_STATISTICS soStats = {0};
HRESULT result = context->GetData(queryState->query, &soStats, sizeof(soStats), 0);
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to get the data of an internal query, result: 0x%X.",
result);
}
if (result == S_OK)
{
queryState->finished = true;
mResult = static_cast<GLuint64>(soStats.NumPrimitivesWritten);
}
}
break;
case GL_TIME_ELAPSED_EXT:
{
ASSERT(queryState->query);
ASSERT(queryState->beginTimestamp);
ASSERT(queryState->endTimestamp);
D3D11_QUERY_DATA_TIMESTAMP_DISJOINT timeStats = {0};
HRESULT result =
context->GetData(queryState->query, &timeStats, sizeof(timeStats), 0);
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to get the data of an internal query, result: 0x%X.",
result);
}
if (result == S_OK)
{
UINT64 beginTime = 0;
HRESULT beginRes =
context->GetData(queryState->beginTimestamp, &beginTime, sizeof(UINT64), 0);
if (FAILED(beginRes))
{
return gl::Error(
GL_OUT_OF_MEMORY,
"Failed to get the data of an internal query, result: 0x%X.", beginRes);
}
UINT64 endTime = 0;
HRESULT endRes =
context->GetData(queryState->endTimestamp, &endTime, sizeof(UINT64), 0);
if (FAILED(endRes))
{
return gl::Error(
GL_OUT_OF_MEMORY,
"Failed to get the data of an internal query, result: 0x%X.", endRes);
}
if (beginRes == S_OK && endRes == S_OK)
{
queryState->finished = true;
if (timeStats.Disjoint)
{
mRenderer->setGPUDisjoint();
}
static_assert(sizeof(UINT64) == sizeof(unsigned long long),
"D3D UINT64 isn't 64 bits");
angle::CheckedNumeric<UINT64> checkedTime(endTime);
checkedTime -= beginTime;
checkedTime *= 1000000000ull;
checkedTime /= timeStats.Frequency;
if (checkedTime.IsValid())
{
mResult = checkedTime.ValueOrDie();
}
else
{
mResult = std::numeric_limits<GLuint64>::max() / timeStats.Frequency;
// If an overflow does somehow occur, there is no way the elapsed time
// is accurate, so we generate a disjoint event
mRenderer->setGPUDisjoint();
}
}
}
}
break;
case GL_TIMESTAMP_EXT:
{
// D3D11 doesn't support GL timestamp queries as D3D timestamps are not guaranteed
// to have any sort of continuity outside of a disjoint timestamp query block, which
// GL depends on
ASSERT(queryState->query == nullptr);
mResult = 0;
queryState->finished = true;
}
break;
case GL_COMMANDS_COMPLETED_CHROMIUM:
{
ASSERT(queryState->query);
BOOL completed = 0;
HRESULT result =
context->GetData(queryState->query, &completed, sizeof(completed), 0);
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to get the data of an internal query, result: 0x%X.",
result);
}
if (result == S_OK)
{
queryState->finished = true;
ASSERT(completed == TRUE);
mResult = (completed == TRUE) ? GL_TRUE : GL_FALSE;
}
}
break;
default:
UNREACHABLE();
break;
}
if (!queryState->finished && mRenderer->testDeviceLost())
{
mRenderer->notifyDeviceLost();
return gl::Error(GL_OUT_OF_MEMORY, "Failed to test get query result, device is lost.");
}
}
return gl::NoError();
}
} // namespace rx
|