forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebGL2ContextQueries.cpp
409 lines (341 loc) · 12.5 KB
/
WebGL2ContextQueries.cpp
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
401
402
403
404
405
406
407
408
409
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "WebGL2Context.h"
#include "GLContext.h"
#include "WebGLQuery.h"
#include "gfxPrefs.h"
#include "nsThreadUtils.h"
namespace mozilla {
/*
* We fake ANY_SAMPLES_PASSED and ANY_SAMPLES_PASSED_CONSERVATIVE with
* SAMPLES_PASSED on desktop.
*
* OpenGL ES 3.0 spec 4.1.6:
* If the target of the query is ANY_SAMPLES_PASSED_CONSERVATIVE, an
* implementation may choose to use a less precise version of the test which
* can additionally set the samples-boolean state to TRUE in some other
* implementation-dependent cases.
*/
static const char*
GetQueryTargetEnumString(GLenum target)
{
switch (target)
{
case LOCAL_GL_ANY_SAMPLES_PASSED:
return "ANY_SAMPLES_PASSED";
case LOCAL_GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
return "ANY_SAMPLES_PASSED_CONSERVATIVE";
case LOCAL_GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
return "TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN";
default:
break;
}
MOZ_ASSERT(false, "Unknown query `target`.");
return "UNKNOWN_QUERY_TARGET";
}
static inline GLenum
SimulateOcclusionQueryTarget(const gl::GLContext* gl, GLenum target)
{
MOZ_ASSERT(target == LOCAL_GL_ANY_SAMPLES_PASSED ||
target == LOCAL_GL_ANY_SAMPLES_PASSED_CONSERVATIVE,
"unknown occlusion query target");
if (gl->IsSupported(gl::GLFeature::occlusion_query_boolean)) {
return target;
} else if (gl->IsSupported(gl::GLFeature::occlusion_query2)) {
return LOCAL_GL_ANY_SAMPLES_PASSED;
}
return LOCAL_GL_SAMPLES_PASSED;
}
WebGLRefPtr<WebGLQuery>&
WebGLContext::GetQuerySlotByTarget(GLenum target)
{
/* This function assumes that target has been validated for either
* WebGL1 or WebGL2.
*/
switch (target) {
case LOCAL_GL_ANY_SAMPLES_PASSED:
case LOCAL_GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
return mActiveOcclusionQuery;
case LOCAL_GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
return mActiveTransformFeedbackQuery;
default:
MOZ_CRASH("Should not get here.");
}
}
// -------------------------------------------------------------------------
// Query Objects
already_AddRefed<WebGLQuery>
WebGL2Context::CreateQuery()
{
if (IsContextLost())
return nullptr;
if (mActiveOcclusionQuery && !gl->IsGLES()) {
/* http://www.opengl.org/registry/specs/ARB/occlusion_query.txt
*
* Calling either GenQueriesARB or DeleteQueriesARB while any query of
* any target is active causes an INVALID_OPERATION error to be
* generated.
*/
GenerateWarning("createQuery: The WebGL 2 prototype might generate"
" INVALID_OPERATION when creating a query object while"
" one other is active.");
/*
* We *need* to lock webgl2 to GL>=3.0 on desktop, but we don't have a
* good mechanism to do this yet. See bug 898404.
*/
}
RefPtr<WebGLQuery> globj = new WebGLQuery(this);
return globj.forget();
}
void
WebGL2Context::DeleteQuery(WebGLQuery* query)
{
if (IsContextLost())
return;
if (!query)
return;
if (query->IsDeleted())
return;
if (query->IsActive())
EndQuery(query->mType);
if (mActiveOcclusionQuery && !gl->IsGLES()) {
/* http://www.opengl.org/registry/specs/ARB/occlusion_query.txt
*
* Calling either GenQueriesARB or DeleteQueriesARB while any query of
* any target is active causes an INVALID_OPERATION error to be
* generated.
*/
GenerateWarning("deleteQuery: The WebGL 2 prototype might generate"
" INVALID_OPERATION when deleting a query object while"
" one other is active.");
}
query->RequestDelete();
}
bool
WebGL2Context::IsQuery(WebGLQuery* query)
{
if (IsContextLost())
return false;
if (!query)
return false;
return (ValidateObjectAllowDeleted("isQuery", query) &&
!query->IsDeleted() &&
query->HasEverBeenActive());
}
void
WebGL2Context::BeginQuery(GLenum target, WebGLQuery* query)
{
if (IsContextLost())
return;
if (!ValidateQueryTarget(target, "beginQuery"))
return;
if (!query) {
/* From GLES's EXT_occlusion_query_boolean:
* BeginQueryEXT sets the active query object name for the query
* type given by <target> to <id>. If BeginQueryEXT is called with
* an <id> of zero, if the active query object name for <target> is
* non-zero (for the targets ANY_SAMPLES_PASSED_EXT and
* ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if the active query for
* either target is non-zero), if <id> is the name of an existing
* query object whose type does not match <target>, or if <id> is
* the active query object name for any query type, the error
* INVALID_OPERATION is generated.
*/
ErrorInvalidOperation("beginQuery: Query should not be null.");
return;
}
if (query->IsDeleted()) {
/* From GLES's EXT_occlusion_query_boolean:
* BeginQueryEXT fails and an INVALID_OPERATION error is generated
* if <id> is not a name returned from a previous call to
* GenQueriesEXT, or if such a name has since been deleted with
* DeleteQueriesEXT.
*/
ErrorInvalidOperation("beginQuery: Query has been deleted.");
return;
}
if (query->HasEverBeenActive() &&
query->mType != target)
{
ErrorInvalidOperation("beginQuery: Target doesn't match with the query"
" type.");
return;
}
WebGLRefPtr<WebGLQuery>& querySlot = GetQuerySlotByTarget(target);
WebGLQuery* activeQuery = querySlot.get();
if (activeQuery)
return ErrorInvalidOperation("beginQuery: An other query already active.");
if (!query->HasEverBeenActive())
query->mType = target;
MakeContextCurrent();
if (target == LOCAL_GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN) {
gl->fBeginQuery(LOCAL_GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN,
query->mGLName);
} else {
gl->fBeginQuery(SimulateOcclusionQueryTarget(gl, target),
query->mGLName);
}
UpdateBoundQuery(target, query);
}
void
WebGL2Context::EndQuery(GLenum target)
{
if (IsContextLost())
return;
if (!ValidateQueryTarget(target, "endQuery"))
return;
WebGLRefPtr<WebGLQuery>& querySlot = GetQuerySlotByTarget(target);
WebGLQuery* activeQuery = querySlot.get();
if (!activeQuery || target != activeQuery->mType)
{
/* From GLES's EXT_occlusion_query_boolean:
* marks the end of the sequence of commands to be tracked for the
* query type given by <target>. The active query object for
* <target> is updated to indicate that query results are not
* available, and the active query object name for <target> is reset
* to zero. When the commands issued prior to EndQueryEXT have
* completed and a final query result is available, the query object
* active when EndQueryEXT is called is updated by the GL. The query
* object is updated to indicate that the query results are
* available and to contain the query result. If the active query
* object name for <target> is zero when EndQueryEXT is called, the
* error INVALID_OPERATION is generated.
*/
ErrorInvalidOperation("endQuery: There is no active query of type %s.",
GetQueryTargetEnumString(target));
return;
}
MakeContextCurrent();
if (target == LOCAL_GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN) {
gl->fEndQuery(LOCAL_GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN);
} else {
gl->fEndQuery(SimulateOcclusionQueryTarget(gl, target));
}
UpdateBoundQuery(target, nullptr);
NS_DispatchToCurrentThread(new WebGLQuery::AvailableRunnable(activeQuery));
}
already_AddRefed<WebGLQuery>
WebGL2Context::GetQuery(GLenum target, GLenum pname)
{
if (IsContextLost())
return nullptr;
if (!ValidateQueryTarget(target, "getQuery"))
return nullptr;
if (pname != LOCAL_GL_CURRENT_QUERY) {
/* OpenGL ES 3.0 spec 6.1.7:
* pname must be CURRENT_QUERY.
*/
ErrorInvalidEnum("getQuery: `pname` must be CURRENT_QUERY.");
return nullptr;
}
WebGLRefPtr<WebGLQuery>& targetSlot = GetQuerySlotByTarget(target);
RefPtr<WebGLQuery> tmp = targetSlot.get();
if (tmp && tmp->mType != target) {
// Query in slot doesn't match target
return nullptr;
}
return tmp.forget();
}
static bool
ValidateQueryEnum(WebGLContext* webgl, GLenum pname, const char* info)
{
switch (pname) {
case LOCAL_GL_QUERY_RESULT_AVAILABLE:
case LOCAL_GL_QUERY_RESULT:
return true;
default:
webgl->ErrorInvalidEnum("%s: invalid pname: %s", info, webgl->EnumName(pname));
return false;
}
}
void
WebGL2Context::GetQueryParameter(JSContext*, WebGLQuery* query, GLenum pname,
JS::MutableHandleValue retval)
{
retval.set(JS::NullValue());
if (IsContextLost())
return;
if (!ValidateQueryEnum(this, pname, "getQueryParameter"))
return;
if (!query) {
/* OpenGL ES 3.0 spec 6.1.7 (spec getQueryObject 1):
* If id is not the name of a query object, or if the query object
* named by id is currently active, then an INVALID_OPERATION error
* is generated. pname must be QUERY_RESULT or
* QUERY_RESULT_AVAILABLE.
*/
ErrorInvalidOperation("getQueryObject: `query` should not be null.");
return;
}
if (query->IsDeleted()) {
// See (spec getQueryObject 1)
ErrorInvalidOperation("getQueryObject: `query` has been deleted.");
return;
}
if (query->IsActive()) {
// See (spec getQueryObject 1)
ErrorInvalidOperation("getQueryObject: `query` is active.");
return;
}
if (!query->HasEverBeenActive()) {
/* See (spec getQueryObject 1)
* If this instance of WebGLQuery has never been active before, that
* mean that query->mGLName is not a query object yet.
*/
ErrorInvalidOperation("getQueryObject: `query` has never been active.");
return;
}
// We must wait for an event loop before the query can be available
if (!query->mCanBeAvailable && !gfxPrefs::WebGLImmediateQueries()) {
if (pname == LOCAL_GL_QUERY_RESULT_AVAILABLE) {
retval.set(JS::BooleanValue(false));
}
return;
}
MakeContextCurrent();
GLuint returned = 0;
switch (pname) {
case LOCAL_GL_QUERY_RESULT_AVAILABLE:
gl->fGetQueryObjectuiv(query->mGLName, LOCAL_GL_QUERY_RESULT_AVAILABLE, &returned);
retval.set(JS::BooleanValue(returned != 0));
return;
case LOCAL_GL_QUERY_RESULT:
gl->fGetQueryObjectuiv(query->mGLName, LOCAL_GL_QUERY_RESULT, &returned);
if (query->mType == LOCAL_GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN) {
retval.set(JS::NumberValue(returned));
return;
}
/*
* test (returned != 0) is important because ARB_occlusion_query on desktop drivers
* return the number of samples drawed when the OpenGL ES extension
* ARB_occlusion_query_boolean return only a boolean if a sample has been drawed.
*/
retval.set(JS::BooleanValue(returned != 0));
return;
default:
break;
}
ErrorInvalidEnum("getQueryObject: `pname` must be QUERY_RESULT{_AVAILABLE}.");
}
void
WebGL2Context::UpdateBoundQuery(GLenum target, WebGLQuery* query)
{
WebGLRefPtr<WebGLQuery>& querySlot = GetQuerySlotByTarget(target);
querySlot = query;
}
bool
WebGL2Context::ValidateQueryTarget(GLenum target, const char* info)
{
switch (target) {
case LOCAL_GL_ANY_SAMPLES_PASSED:
case LOCAL_GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
case LOCAL_GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
return true;
default:
ErrorInvalidEnumInfo(info, target);
return false;
}
}
} // namespace mozilla