-
Notifications
You must be signed in to change notification settings - Fork 12
/
query.js
74 lines (62 loc) · 1.65 KB
/
query.js
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
import { checkProps } from "./utils.js";
/**
* @typedef {import("./types.js").PexResource} QueryOptions
* @property {ctx.QueryTarget} [target=ctx.QueryTarget.TimeElapsed] query type
*/
/**
* @typedef {QueryOptions} PexQuery
* @property {ctx.QueryState} [state=ctx.QueryState.Ready]
* @property {number} [result] result of the measurement
*/
const allowedProps = ["target"];
function createQuery(ctx, opts = {}) {
checkProps(allowedProps, opts);
const gl = ctx.gl;
const query = Object.assign(
{
class: "query",
handle: gl.createQuery(),
target: null,
state: ctx.QueryState.Ready,
result: null,
_begin: begin,
_end: end,
_available: available,
_dispose() {
gl.deleteQuery(this.handle);
this.handle = null;
},
},
opts,
);
if (!query.target) {
query.target = ctx.capabilities.disjointTimerQuery
? ctx.QueryTarget.TimeElapsed
: ctx.QueryTarget.AnySamplesPassed;
}
return query;
}
function begin({ QueryState, gl }, q) {
if (q.state !== QueryState.Ready) return false;
gl.beginQuery(q.target, q.handle);
q.state = QueryState.Active;
q.result = null;
return true;
}
function end({ QueryState, gl }, q) {
if (q.state !== QueryState.Active) return false;
gl.endQuery(q.target);
q.state = QueryState.Pending;
return true;
}
function available({ gl, QueryState }, q) {
const available = gl.getQueryParameter(q.handle, gl.QUERY_RESULT_AVAILABLE);
if (available) {
q.result = gl.getQueryParameter(q.handle, gl.QUERY_RESULT);
q.state = QueryState.Ready;
return true;
} else {
return false;
}
}
export default createQuery;