summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/extensions/ext-disjoint-timer-query.html
blob: 7965987c642d178687c83856615686e8e44a37f2 (plain)
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
<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL EXT_disjoint_timer_query Conformance Tests</title>
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"></script>
</head>
<body>
<div id="description"></div>
<canvas id="canvas" style="width: 50px; height: 50px;"> </canvas>
<div id="console"></div>

<script>
"use strict";
description("This test verifies the functionality of the EXT_disjoint_timer_query extension, if it is available.");

var wtu = WebGLTestUtils;
var canvas = document.getElementById("canvas");
var gl = wtu.create3DContext(canvas);
var gl2 = null;
var ext = null;
var ext2 = null;
var query = null;
var query2 = null;
var elapsed_query = null;
var timestamp_query1 = null;
var timestamp_query2 = null;
var availability_retry = 500;
var timestamp_counter_bits = 0;

if (!gl) {
    testFailed("WebGL context does not exist");
    finishTest();
} else {
    testPassed("WebGL context exists");

    // Query the extension and store globally so shouldBe can access it
    ext = wtu.getExtensionWithKnownPrefixes(gl, "EXT_disjoint_timer_query");
    if (!ext) {
        testPassed("No EXT_disjoint_timer_query support -- this is legal");
        finishTest();
    } else {
        if (wtu.getDefault3DContextVersion() > 1) {
            testFailed("EXT_disjoint_timer_query must not be advertised on WebGL 2.0 contexts");
            finishTest();
        } else {
            runSanityTests();

            // Clear disjoint value.
            gl.getParameter(ext.GPU_DISJOINT_EXT);

            runElapsedTimeTest();
            timestamp_counter_bits = ext.getQueryEXT(ext.TIMESTAMP_EXT, ext.QUERY_COUNTER_BITS_EXT);
            if (timestamp_counter_bits > 0) {
                runTimeStampTest();
            }
            verifyQueryResultsNotAvailable();
            verifyDeleteQueryBehavior();
            verifyDeleteQueryErrorBehavior();

            window.requestAnimationFrame(checkQueryResults);
        }
    }
}

function runSanityTests() {
    debug("");
    debug("Testing timer query expectations");

    shouldBe("ext.QUERY_COUNTER_BITS_EXT", "0x8864");
    shouldBe("ext.CURRENT_QUERY_EXT", "0x8865");
    shouldBe("ext.QUERY_RESULT_EXT", "0x8866");
    shouldBe("ext.QUERY_RESULT_AVAILABLE_EXT", "0x8867");
    shouldBe("ext.TIME_ELAPSED_EXT", "0x88BF");
    shouldBe("ext.TIMESTAMP_EXT", "0x8E28");
    shouldBe("ext.GPU_DISJOINT_EXT", "0x8FBB");

    shouldBe("ext.isQueryEXT(null)", "false");

    shouldBeTrue("ext.getQueryEXT(ext.TIME_ELAPSED_EXT, ext.CURRENT_QUERY_EXT) === null");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);
    shouldBeTrue("ext.getQueryEXT(ext.TIME_ELAPSED_EXT, ext.QUERY_COUNTER_BITS_EXT) >= 30");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);

    shouldBeTrue("ext.getQueryEXT(ext.TIMESTAMP_EXT, ext.CURRENT_QUERY_EXT) === null");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);

    // Certain drivers set timestamp counter bits to 0 as they don't support timestamps
    shouldBeTrue("ext.getQueryEXT(ext.TIMESTAMP_EXT, ext.QUERY_COUNTER_BITS_EXT) >= 30 || " +
             "ext.getQueryEXT(ext.TIMESTAMP_EXT, ext.QUERY_COUNTER_BITS_EXT) === 0");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);

    debug("");
    debug("Testing time elapsed query lifecycle");
    query = ext.createQueryEXT();
    shouldBe("ext.isQueryEXT(query)", "false");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Query creation must succeed.");
    shouldThrow("ext.beginQueryEXT(ext.TIMESTAMP_EXT, null)");
    ext.beginQueryEXT(ext.TIMESTAMP_EXT, query);
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "Beginning a timestamp query should fail.");
    ext.beginQueryEXT(ext.TIME_ELAPSED_EXT, query);
    shouldBe("ext.isQueryEXT(query)", "true");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Beginning an inactive time elapsed query should succeed.");
    ext.beginQueryEXT(ext.TIME_ELAPSED_EXT, query);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "Attempting to begin an active query should fail.");
    ext.getQueryObjectEXT(query, ext.QUERY_RESULT_AVAILABLE_EXT);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "Fetching query result availability of an active query should fail.");
    ext.getQueryObjectEXT(query, ext.QUERY_RESULT_EXT);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "Fetching query result of an active query should fail.");
    shouldBe("ext.getQueryEXT(ext.TIME_ELAPSED_EXT, ext.CURRENT_QUERY_EXT)", "query");
    ext.endQueryEXT(ext.TIME_ELAPSED_EXT);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Ending an active time elapsed query should succeed.");
    shouldThrow("ext.getQueryObjectEXT(null, ext.QUERY_RESULT_AVAILABLE_EXT)");
    ext.getQueryObjectEXT(query, ext.QUERY_RESULT_AVAILABLE_EXT);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Fetching query result availability after query end should succeed.");
    ext.endQueryEXT(ext.TIME_ELAPSED_EXT);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "Attempting to end an inactive query should fail.");
    ext.queryCounterEXT(query, ext.TIMESTAMP_EXT);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "Should not be able to use time elapsed query to store a timestamp.");
    ext.deleteQueryEXT(query);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Query deletion must succeed.");
    ext.beginQueryEXT(ext.TIME_ELAPSED_EXT, query);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "Beginning a deleted query must fail.");
    ext.getQueryObjectEXT(query, ext.QUERY_RESULT_AVAILABLE_EXT);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "Fetching query result availability after query deletion should fail.");
    shouldBe("ext.isQueryEXT(query)", "false");

    debug("");
    debug("Testing timestamp counter");
    query = ext.createQueryEXT();
    shouldThrow("ext.queryCounterEXT(null, ext.TIMESTAMP_EXT)");
    ext.queryCounterEXT(query, ext.TIMESTAMP_EXT);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Timestamp counter queries should work.");
    ext.deleteQueryEXT(query);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);

    debug("");
    debug("Performing parameter sanity checks");
    gl.getParameter(ext.TIMESTAMP_EXT);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "getParameter timestamp calls should work.");
    gl.getParameter(ext.GPU_DISJOINT_EXT);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "getParameter disjoint calls should work.");

    debug("");
    debug("Testing current query conditions");
    query = ext.createQueryEXT();
    query2 = ext.createQueryEXT();
    shouldBe("ext.getQueryEXT(ext.TIME_ELAPSED_EXT, ext.CURRENT_QUERY_EXT)", "null");
    ext.beginQueryEXT(ext.TIME_ELAPSED_EXT, query);
    shouldBe("ext.getQueryEXT(ext.TIME_ELAPSED_EXT, ext.CURRENT_QUERY_EXT)", "query");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);

    debug("");
    debug("Testing failed begin query should not change the current query.");
    ext.beginQueryEXT(ext.TIME_ELAPSED_EXT, query2);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "Beginning an elapsed query without ending should fail.");
    shouldBe("ext.getQueryEXT(ext.TIME_ELAPSED_EXT, ext.CURRENT_QUERY_EXT)", "query");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);

    debug("");
    debug("Testing beginning a timestamp query is invalid and should not change the elapsed query.");
    ext.beginQueryEXT(ext.TIMESTAMP_EXT, query2)
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM);
    shouldBe("ext.getQueryEXT(ext.TIME_ELAPSED_EXT, ext.CURRENT_QUERY_EXT)", "query");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);

    debug("");
    debug("Testing timestamp queries end immediately so are never current.");
    ext.queryCounterEXT(query2, ext.TIMESTAMP_EXT);
    shouldBe("ext.getQueryEXT(ext.TIMESTAMP_EXT, ext.CURRENT_QUERY_EXT)", "null");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);

    debug("");
    debug("Testing ending the query should clear the current query.");
    ext.endQueryEXT(ext.TIME_ELAPSED_EXT);
    shouldBe("ext.getQueryEXT(ext.TIME_ELAPSED_EXT, ext.CURRENT_QUERY_EXT)", "null");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);

    debug("");
    debug("Testing beginning a elapsed query using a timestamp query should fail and not affect current query.")
    ext.beginQueryEXT(ext.TIME_ELAPSED_EXT, query2);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "Switching query targets should fail.");
    shouldBe("ext.getQueryEXT(ext.TIME_ELAPSED_EXT, ext.CURRENT_QUERY_EXT)", "null");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);

    ext.deleteQueryEXT(query);
    ext.deleteQueryEXT(query2);

    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors at end of sanity tests");
}

function runElapsedTimeTest() {
    debug("");
    debug("Testing elapsed time query");

    elapsed_query = ext.createQueryEXT();
    ext.beginQueryEXT(ext.TIME_ELAPSED_EXT, elapsed_query);
    gl.clearColor(0, 0, 1, 1);
    gl.clear(gl.COLOR_BUFFER_BIT);
    ext.endQueryEXT(ext.TIME_ELAPSED_EXT);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Time elapsed query should have no errors");
}

function runTimeStampTest() {
    debug("");
    debug("Testing timestamp query");

    timestamp_query1 = ext.createQueryEXT();
    timestamp_query2 = ext.createQueryEXT();
    ext.queryCounterEXT(timestamp_query1, ext.TIMESTAMP_EXT);
    gl.clearColor(1, 0, 0, 1);
    gl.clear(gl.COLOR_BUFFER_BIT);
    ext.queryCounterEXT(timestamp_query2, ext.TIMESTAMP_EXT);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Timestamp queries should have no errors");
}

function verifyQueryResultsNotAvailable() {
    debug("");
    debug("Verifying queries' results don't become available too early");

    // Verify as best as possible that the implementation doesn't
    // allow a query's result to become available the same frame, by
    // spin-looping for some time and ensuring that none of the
    // queries' results become available.
    var startTime = Date.now();
    while (Date.now() - startTime < 2000) {
        gl.finish();
        if (ext.getQueryObjectEXT(elapsed_query, ext.QUERY_RESULT_AVAILABLE_EXT)) {
            testFailed("One of the queries' results became available too early");
            return;
        }
        if (timestamp_counter_bits > 0) {
            if (ext.getQueryObjectEXT(timestamp_query1, ext.QUERY_RESULT_AVAILABLE_EXT) ||
                ext.getQueryObjectEXT(timestamp_query2, ext.QUERY_RESULT_AVAILABLE_EXT)) {
                testFailed("One of the queries' results became available too early");
                return;
            }
        }
    }

    testPassed("Queries' results didn't become available in a spin loop");
}

function verifyDeleteQueryBehavior() {
    debug("");
    debug("Testing deleting an active query should end it.");

    // Use a new context for this test
    gl2 = wtu.create3DContext(null, null, 1);
    if (!gl2) return;
    ext2 = gl2.getExtension("EXT_disjoint_timer_query");
    if (!ext2) return;

    query = ext2.createQueryEXT();
    ext2.beginQueryEXT(ext2.TIME_ELAPSED_EXT, query);
    wtu.glErrorShouldBe(gl2, gl2.NONE, "The query began successfully");
    ext2.deleteQueryEXT(query);
    wtu.glErrorShouldBe(gl2, gl2.NONE, "Deletion of the active query succeeds");
    shouldBeNull("ext2.getQueryEXT(ext2.TIME_ELAPSED_EXT, ext2.CURRENT_QUERY_EXT)");
    shouldBeFalse("ext2.isQueryEXT(query)");
    query = ext2.createQueryEXT();
    ext2.beginQueryEXT(ext2.TIME_ELAPSED_EXT, query);
    wtu.glErrorShouldBe(gl2, gl2.NONE, "Beginning a new query succeeds");
    ext2.endQueryEXT(ext2.TIME_ELAPSED_EXT);
    ext2.deleteQueryEXT(query);
    wtu.glErrorShouldBe(gl2, gl2.NONE);
    query = null;
    ext2 = null;
    gl2 = null;
}

function verifyDeleteQueryErrorBehavior() {
    debug("");
    debug("Testing deleting a query created by another context.");

    // Use new contexts for this test
    gl2 = wtu.create3DContext(null, null, 1);
    var gl3 = wtu.create3DContext(null, null, 1);
    if (!gl2 || !gl3) return;
    ext2 = gl2.getExtension("EXT_disjoint_timer_query");
    var ext3 = gl3.getExtension("EXT_disjoint_timer_query");
    if (!ext2 || !ext3) return;

    query = ext2.createQueryEXT();
    ext2.beginQueryEXT(ext2.TIME_ELAPSED_EXT, query);
    ext3.deleteQueryEXT(query);
    wtu.glErrorShouldBe(gl3, gl3.INVALID_OPERATION);
    shouldBeTrue("ext2.isQueryEXT(query)");
    shouldBe("ext2.getQueryEXT(ext2.TIME_ELAPSED_EXT, ext2.CURRENT_QUERY_EXT)", "query");
    ext2.endQueryEXT(ext2.TIME_ELAPSED_EXT);
    ext2.deleteQueryEXT(query);
    wtu.glErrorShouldBe(gl2, gl2.NONE);
    query = null;
    ext2 = null;
    gl2 = null;
    gl3 = null;
}

function checkQueryResults() {
    if (availability_retry > 0) {
        // Make a reasonable attempt to wait for the queries' results to become available.
        if (!ext.getQueryObjectEXT(elapsed_query, ext.QUERY_RESULT_AVAILABLE_EXT) ||
            (timestamp_counter_bits > 0 && !ext.getQueryObjectEXT(timestamp_query2, ext.QUERY_RESULT_AVAILABLE_EXT))) {
            var error = gl.getError();
            if (error != gl.NO_ERROR) {
                testFailed("getQueryObjectEXT should have no errors: " + wtu.glEnumToString(gl, error));
                debug("");
                finishTest();
                return;
            }
            availability_retry--;
            window.requestAnimationFrame(checkQueryResults);
            return;
        }
    }

    debug("");
    debug("Testing query results");

    // Make sure queries are available.
    shouldBe("ext.getQueryObjectEXT(elapsed_query, ext.QUERY_RESULT_AVAILABLE_EXT)", "true");
    if (timestamp_counter_bits > 0) {
        shouldBe("ext.getQueryObjectEXT(timestamp_query1, ext.QUERY_RESULT_AVAILABLE_EXT)", "true");
        shouldBe("ext.getQueryObjectEXT(timestamp_query2, ext.QUERY_RESULT_AVAILABLE_EXT)", "true");
    }

    var disjoint_value = gl.getParameter(ext.GPU_DISJOINT_EXT);
    if (disjoint_value) {
        // Cannot validate results make sense, but this is okay.
        testPassed("Disjoint triggered.");
    } else {
        var elapsed_result = ext.getQueryObjectEXT(elapsed_query, ext.QUERY_RESULT_EXT);
        if (timestamp_counter_bits > 0) {
            var timestamp_result1 = ext.getQueryObjectEXT(timestamp_query1, ext.QUERY_RESULT_EXT);
            var timestamp_result2 = ext.getQueryObjectEXT(timestamp_query2, ext.QUERY_RESULT_EXT);
        }
        // Do some basic validity checking of the elapsed time query. There's no way it should
        // take more than about half a second for a no-op query.
        var halfSecondInNanos = 0.5 * 1000 * 1000 * 1000;
        if (elapsed_result < 0 || elapsed_result > halfSecondInNanos) {
            testFailed("Time elapsed query returned invalid data: " + elapsed_result);
        } else {
            testPassed("Time elapsed query results were valid.");
        }

        if (timestamp_counter_bits > 0) {
            if (timestamp_result1 <= 0 ||
                timestamp_result2 <= 0 ||
                timestamp_result2 <= timestamp_result1) {
                testFailed("Timestamp queries returned invalid data: timestamp_result1 = " +
                           timestamp_result1 + ", timestamp_result2 = " + timestamp_result2);
            } else {
                testPassed("Timestamp query results were valid.");
            }
        }
    }

    debug("");
    finishTest();
}
</script>
</body>
</html>