summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/fast/canvas/webgl/renderbuffer-initialization.html
blob: c0de8e183c4b3bf1e6502eaa6a24b2edb145a589 (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
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="resources/webgl-test.js"></script>
<script>
function runTest(gl, width, height)
{

    debug('Test whether the WebGL internal buffers have been initialized to 0.');
    var totalBytes = width * height * 4;
    var buf = new Uint8Array(totalBytes);
    gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    if (gl.getError() != gl.NO_ERROR) {
        testFailed('GL error detected after readPixels().');
        return false;
    }
    for (var i = 0; i < totalBytes; ++i) {
        if (buf[i] != 0) {
            testFailed('WebGL internal buffers are dirty.');
            return false;
        }
    }
    testPassed('Buffers have been initialized to 0.');

    debug('Test whether user created buffers have been initialized to 0.');
    var fbo = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
    var colorbuffer = gl.createRenderbuffer();
    gl.bindRenderbuffer(gl.RENDERBUFFER, colorbuffer);
    gl.renderbufferStorage(gl.RENDERBUFFER, gl.RGBA4, width, height);
    if (gl.getError() != gl.NO_ERROR) {
        testFailed('GL error detected after renderbufferStorage(internalformat = RGBA4).');
        return false;
    }
    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, colorbuffer);
    if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
        testFailed('Framebuffer incomplete.');
        return false;
    }
    gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    if (gl.getError() != gl.NO_ERROR) {
        testFailed('GL error detected after readPixels().');
        return false;
    }
    for (var i = 0; i < totalBytes; ++i) {
        if (buf[i] != 0) {
            testFailed('User created buffers are dirty.');
            return false;
        }
    }

    testPassed('Buffers have been initialized to 0.');
    return true;
}
</script>
</head>
<body>
<canvas id="testbed" width="400px" height="400px"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>

description('Verify renderbuffers are initialized to 0 before being read in WebGL');

var canvas = document.getElementById("testbed");
var gl = canvas.getContext("webgl");
if (gl) {
    runTest(gl, canvas.width, canvas.height);

    // Testing that canvas resizing will clear the buffers with 0 instead of the current clear values.
    gl.clearColor(1, 0, 0, 1);
    canvas.width += 1;
    canvas.height += 1;
    runTest(gl, canvas.width, canvas.height);

    // Testing buffer clearing won't change the clear values.
    var clearColor = gl.getParameter(gl.COLOR_CLEAR_VALUE);
    shouldBe("clearColor", "[1, 0, 0, 1]");
} else
    testFailed('canvas.getContext() failed');
</script>
</body>
</html>