summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/query/occlusion-query.html
blob: 3655ed87833200255309960b6f341f9c50325493 (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
<!--
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 Occlusion 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 occlusion query objects.");

debug("");

var tests = [];
var currentTest;
var currentTestIndex = 0;
var numberOfTestAttempts = 4; // Just to stress implementations a bit more.
var query;
var numberOfCompletionAttempts = 0;

function setupTests(gl) {
    tests = [
        {
            target: gl.ANY_SAMPLES_PASSED_CONSERVATIVE,
            name:   "ANY_SAMPLES_PASSED_CONSERVATIVE",
            result: 1,
        },
        {
            target: gl.ANY_SAMPLES_PASSED,
            name:   "ANY_SAMPLES_PASSED",
            result: 1,
        },
    ];
}

function runOcclusionQueryTest() {
    currentTest = tests[currentTestIndex];

    debug("");
    debug("Testing completion and behavior of " + currentTest.name + " occlusion query");
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    var program = wtu.setupSimpleColorProgram(gl, 0);
    gl.uniform4f(gl.getUniformLocation(program, "u_color"), 0, 1, 0, 1);
    wtu.setupUnitQuad(gl, 0);
    query = gl.createQuery();
    var target = currentTest.target;
    gl.beginQuery(target, query);
    wtu.drawUnitQuad(gl);
    gl.endQuery(target);

    // 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 numEarlyTests = 20000;
    while (--numEarlyTests > 0) {
        gl.finish();
        if (gl.getQueryParameter(query, gl.QUERY_RESULT_AVAILABLE)) {
            testFailed("Query's result became available too early");
            finishTest();
            return;
        }
    }

    testPassed("Query's result didn't become available too early");
    numberOfCompletionAttempts = 0;
    requestAnimationFrame(completeOcclusionQueryTest);
}

function completeOcclusionQueryTest() {
    ++numberOfCompletionAttempts;

    if (numberOfCompletionAttempts > 500) {
        testFailed("Query didn't become available in a reasonable time");
        finishTest();
        return;
    }

    if (!gl.getQueryParameter(query, gl.QUERY_RESULT_AVAILABLE)) {
        requestAnimationFrame(completeOcclusionQueryTest);
        return;
    }

    // No matter whether the test was run with ANY_SAMPLES_PASSED or
    // ANY_SAMPLES_PASSED_CONSERVATIVE, the query object should always
    // report a non-zero result.
    var result = gl.getQueryParameter(query, gl.QUERY_RESULT);
    if (result == currentTest.result) {
        testPassed("Occlusion query " + currentTest.name + " returned a correct result (" + result + ")");
    } else {
        testFailed("Occlusion query " + currentTest.name + " returned an incorrect result " + result + " (expected " + currentTest.result + ")");
    }

    gl.deleteQuery(query);
    query = null;

    ++currentTestIndex;
    if (currentTestIndex >= tests.length) {
        --numberOfTestAttempts;
        if (numberOfTestAttempts == 0) {
            finishTest();
        } else {
            currentTestIndex = 0;
            requestAnimationFrame(runOcclusionQueryTest);
        }
    } else {
        requestAnimationFrame(runOcclusionQueryTest);
    }
}

var wtu = WebGLTestUtils;
var canvas = document.getElementById("canvas");
var gl = wtu.create3DContext(canvas, null, 2);

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

    setupTests(gl);
    runOcclusionQueryTest();
}
</script>
</body>
</html>