content/canvas/test/webgl-conformance/conformance/misc/uninitialized-test.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/canvas/test/webgl-conformance/conformance/misc/uninitialized-test.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,170 @@
     1.4 +<!DOCTYPE html>
     1.5 +<html>
     1.6 +<head>
     1.7 +<meta charset="utf-8">
     1.8 +<title>WebGL Uninitialized GL Resources Tests</title>
     1.9 +<link rel="stylesheet" href="../../resources/js-test-style.css"/>
    1.10 +<script src="../../resources/js-test-pre.js"></script>
    1.11 +<script src="../resources/webgl-test.js"></script>
    1.12 +<script src="../resources/webgl-test-utils.js"></script>
    1.13 +</head>
    1.14 +<body>
    1.15 +<div id="description"></div>
    1.16 +<div id="console"></div>
    1.17 +<canvas id="canvas" width="2" height="2"> </canvas>
    1.18 +<script>
    1.19 +description("Tests to check user code cannot access uninitialized data from GL resources.");
    1.20 +
    1.21 +var wtu = WebGLTestUtils;
    1.22 +var gl = wtu.create3DContext("canvas");
    1.23 +if (!gl)
    1.24 +  testFailed("Context created.");
    1.25 +else
    1.26 +  testPassed("Context created.");
    1.27 +
    1.28 +function setupTexture(texWidth, texHeight) {
    1.29 +    var texture = gl.createTexture();
    1.30 +    gl.bindTexture(gl.TEXTURE_2D, texture);
    1.31 +    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, texWidth, texHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
    1.32 +
    1.33 +    // this can be quite undeterministic so to improve odds of seeing uninitialized data write bits
    1.34 +    // into tex then delete texture then re-create one with same characteristics (driver will likely reuse mem)
    1.35 +    // with this trick on r59046 WebKit/OSX I get FAIL 100% of the time instead of ~15% of the time.
    1.36 +
    1.37 +    var badData = new Uint8Array(texWidth * texHeight * 4);
    1.38 +    for (var i = 0; i < badData.length; ++i)
    1.39 +        badData[i] = i % 255;
    1.40 +
    1.41 +    gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, texWidth, texHeight, gl.RGBA, gl.UNSIGNED_BYTE, badData);
    1.42 +    gl.finish(); // make sure it has been uploaded
    1.43 +
    1.44 +    gl.deleteTexture(texture);
    1.45 +    gl.finish(); // make sure it has been deleted
    1.46 +
    1.47 +    var texture = gl.createTexture();
    1.48 +    gl.bindTexture(gl.TEXTURE_2D, texture);
    1.49 +    return texture;
    1.50 +}
    1.51 +
    1.52 +function checkNonZeroPixels(texture, texWidth, texHeight, skipX, skipY, skipWidth, skipHeight, skipR, skipG, skipB, skipA) {
    1.53 +    gl.bindTexture(gl.TEXTURE_2D, null);
    1.54 +    var fb = gl.createFramebuffer();
    1.55 +    gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
    1.56 +    gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
    1.57 +    shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE");
    1.58 +
    1.59 +    var data = new Uint8Array(texWidth * texHeight * 4);
    1.60 +    gl.readPixels(0, 0, texWidth, texHeight, gl.RGBA, gl.UNSIGNED_BYTE, data);
    1.61 +
    1.62 +    var k = 0;
    1.63 +    for (var y = 0; y < texHeight; ++y) {
    1.64 +        for (var x = 0; x < texWidth; ++x) {
    1.65 +            var index = (y * texWidth + x) * 4;
    1.66 +            if (x >= skipX && x < skipX + skipWidth && y >= skipY && y < skipY + skipHeight) {
    1.67 +                if (data[index] != skipR || data[index + 1] != skipG || data[index + 2] != skipB || data[index + 3] != skipA) {
    1.68 +                    testFailed("non-zero pixel values are wrong at (" + x + ", " + y + "), data was (" +
    1.69 +                               data[index] + "," + data[index + 1] + "," + data[index + 2] + "," + data[index + 3] +
    1.70 +                               ") should have been (" + skipR + "," + skipG + "," + skipB + "," + skipA + ")");
    1.71 +                    return;
    1.72 +                }
    1.73 +            } else {
    1.74 +                for (var i = 0; i < 4; ++i) {
    1.75 +                    if (data[index + i] != 0)
    1.76 +                        k++;
    1.77 +                }
    1.78 +            }
    1.79 +        }
    1.80 +    }
    1.81 +    if (k) {
    1.82 +        testFailed("Found " + k + " non-zero bytes");
    1.83 +    } else {
    1.84 +        testPassed("All data initialized");
    1.85 +    }
    1.86 +}
    1.87 +
    1.88 +var width = 512;
    1.89 +var height = 512;
    1.90 +
    1.91 +debug("");
    1.92 +debug("Reading an uninitialized texture (texImage2D) should succeed with all bytes set to 0.");
    1.93 +
    1.94 +var tex = setupTexture(width, height);
    1.95 +gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
    1.96 +checkNonZeroPixels(tex, width, height, 0, 0, 0, 0, 0, 0, 0, 0);
    1.97 +gl.deleteTexture(tex);
    1.98 +gl.finish();
    1.99 +glErrorShouldBe(gl, gl.NO_ERROR);
   1.100 +
   1.101 +debug("");
   1.102 +debug("Reading an uninitialized portion of a texture (copyTexImage2D) should succeed with all bytes set to 0.");
   1.103 +
   1.104 +var tex = setupTexture(width, height);
   1.105 +var fbo = gl.createFramebuffer();
   1.106 +gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
   1.107 +var rbo = gl.createRenderbuffer();
   1.108 +gl.bindRenderbuffer(gl.RENDERBUFFER, rbo);
   1.109 +var fboWidth = 16;
   1.110 +var fboHeight = 16;
   1.111 +gl.renderbufferStorage(gl.RENDERBUFFER, gl.RGBA4, fboWidth, fboHeight);
   1.112 +gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, rbo);
   1.113 +shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE");
   1.114 +gl.clearColor(1.0, 0.0, 0.0, 1.0);
   1.115 +gl.clear(gl.COLOR_BUFFER_BIT);
   1.116 +glErrorShouldBe(gl, gl.NO_ERROR);
   1.117 +gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 0, 0, width, height, 0);
   1.118 +checkNonZeroPixels(tex, width, height, 0, 0, fboWidth, fboHeight, 255, 0, 0, 255);
   1.119 +gl.deleteTexture(tex);
   1.120 +gl.finish();
   1.121 +glErrorShouldBe(gl, gl.NO_ERROR);
   1.122 +
   1.123 +debug("");
   1.124 +debug("Reading an uninitialized portion of a texture (copyTexImage2D with negative x and y) should succeed with all bytes set to 0.");
   1.125 +
   1.126 +var tex = setupTexture(width, height);
   1.127 +var fbo = gl.createFramebuffer();
   1.128 +gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
   1.129 +var rbo = gl.createRenderbuffer();
   1.130 +gl.bindRenderbuffer(gl.RENDERBUFFER, rbo);
   1.131 +var fboWidth = 16;
   1.132 +var fboHeight = 16;
   1.133 +gl.renderbufferStorage(gl.RENDERBUFFER, gl.RGBA4, fboWidth, fboHeight);
   1.134 +gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, rbo);
   1.135 +shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE");
   1.136 +gl.clearColor(1.0, 0.0, 0.0, 1.0);
   1.137 +gl.clear(gl.COLOR_BUFFER_BIT);
   1.138 +glErrorShouldBe(gl, gl.NO_ERROR);
   1.139 +var x = -8;
   1.140 +var y = -8;
   1.141 +gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, x, y, width, height, 0);
   1.142 +checkNonZeroPixels(tex, width, height, -x, -y, fboWidth, fboHeight, 255, 0, 0, 255);
   1.143 +gl.deleteTexture(tex);
   1.144 +gl.finish();
   1.145 +glErrorShouldBe(gl, gl.NO_ERROR);
   1.146 +
   1.147 +debug("");
   1.148 +debug("Reading an uninitialized portion of a texture (copyTexImage2D from WebGL internal fbo) should succeed with all bytes set to 0.");
   1.149 +
   1.150 +var tex = setupTexture(width, height);
   1.151 +gl.bindFramebuffer(gl.FRAMEBUFFER, null);
   1.152 +gl.clearColor(0.0, 1.0, 0.0, 0.0);
   1.153 +gl.clear(gl.COLOR_BUFFER_BIT);
   1.154 +glErrorShouldBe(gl, gl.NO_ERROR);
   1.155 +gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 0, 0, width, height, 0);
   1.156 +checkNonZeroPixels(tex, width, height, 0, 0, gl.canvas.width, gl.canvas.height, 0, 255, 0, 0);
   1.157 +gl.deleteTexture(tex);
   1.158 +gl.finish();
   1.159 +glErrorShouldBe(gl, gl.NO_ERROR);
   1.160 +
   1.161 +//TODO: uninitialized vertex array buffer
   1.162 +//TODO: uninitialized vertex elements buffer
   1.163 +//TODO: uninitialized framebuffer? (implementations would need to do a GL clear at first binding?)
   1.164 +//TODO: uninitialized renderbuffer? (implementations would need to do a GL clear at first binding?)
   1.165 +//TODO: uninitialized uniform arrays?
   1.166 +
   1.167 +debug("");
   1.168 +successfullyParsed = true;
   1.169 +</script>
   1.170 +<script>finishTest();</script>
   1.171 +</body>
   1.172 +</html>
   1.173 +

mercurial