summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/query/occlusion-query-scissor.html
blob: dec88e56d31a230399ee51d598df0fe4f22d8b99 (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
<!--
Copyright (c) 2022 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 w/Scissor 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 with the scissor test.");
debug('Regression test for <a href="http://anglebug.com/7157">http://anglebug.com/7157</a>');

debug("");

const wtu = WebGLTestUtils;

const wait = () => new Promise(resolve => setTimeout(resolve));

async function runOcclusionQueryTest(gl) {
    const kSize = 4;
    const colors = [
      [0, 0, 0, 255],
      [255, 0, 0, 255],
      [0, 255, 0, 255],
      [255, 255, 0, 255],
    ];
    const framebuffers = colors.map((color, index) => {
      const tex = gl.createTexture();
      gl.bindTexture(gl.TEXTURE_2D, tex);
      gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, kSize, kSize, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);

      const fb = gl.createFramebuffer();
      gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
      gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);

      gl.clearColor(index & 1, (index >> 1) & 1, (index >> 2) & 1, 1);
      gl.clear(gl.COLOR_BUFFER_BIT);

      wtu.checkCanvasRect(gl, 0, 0, kSize, kSize, color);

      return fb;
    });

    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "setup should succeed");

    gl.viewport(0, 0, kSize, kSize);

    const program = wtu.setupSimpleColorProgram(gl, 0);
    gl.uniform4f(gl.getUniformLocation(program, "u_color"), 0, 1, 0, 1);
    wtu.setupUnitQuad(gl, 0);

    const query = gl.createQuery();;

    for (let i = 0; i < 256; ++i)
    {
        gl.beginQuery(gl.ANY_SAMPLES_PASSED, query);
        let drawn = false;

        framebuffers.forEach((fb, index) => {
            gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
            const scissor = (i >> 4 >> index) & 1;
            if (i & (1 << index))
            {
                if (scissor)
                {
                    gl.enable(gl.SCISSOR_TEST);
                    gl.scissor(0, 0, 0, 0);
                }
                wtu.drawUnitQuad(gl);
                drawn ||= !scissor;
                if (scissor)
                {
                    gl.disable(gl.SCISSOR_TEST);
                    gl.scissor(0, 0, kSize, kSize);
                }
            }
        });

        gl.endQuery(gl.ANY_SAMPLES_PASSED);
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should draw");

        do {
          await wait();
        } while (!gl.getQueryParameter(query, gl.QUERY_RESULT_AVAILABLE));

        const result = gl.getQueryParameter(query, gl.QUERY_RESULT);

        const expected = drawn ? 1 : 0;
        assertMsg(result === expected, `pass ${i}, result: ${result} === expected: ${expected}`);
    }
    finishTest();
}

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

if (!gl) {
    testFailed("WebGL context does not exist");
} else {
    testPassed("WebGL context exists");
    runOcclusionQueryTest(gl);
}
</script>
</body>
</html>