Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | <!DOCTYPE html> |
michael@0 | 2 | <html> |
michael@0 | 3 | <head> |
michael@0 | 4 | <meta charset="utf-8"> |
michael@0 | 5 | <title>WebGL Uninitialized GL Resources Tests</title> |
michael@0 | 6 | <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
michael@0 | 7 | <script src="../../resources/js-test-pre.js"></script> |
michael@0 | 8 | <script src="../resources/webgl-test.js"></script> |
michael@0 | 9 | <script src="../resources/webgl-test-utils.js"></script> |
michael@0 | 10 | </head> |
michael@0 | 11 | <body> |
michael@0 | 12 | <div id="description"></div> |
michael@0 | 13 | <div id="console"></div> |
michael@0 | 14 | <canvas id="canvas" width="2" height="2"> </canvas> |
michael@0 | 15 | <script> |
michael@0 | 16 | description("Tests to check user code cannot access uninitialized data from GL resources."); |
michael@0 | 17 | |
michael@0 | 18 | var wtu = WebGLTestUtils; |
michael@0 | 19 | var gl = wtu.create3DContext("canvas"); |
michael@0 | 20 | if (!gl) |
michael@0 | 21 | testFailed("Context created."); |
michael@0 | 22 | else |
michael@0 | 23 | testPassed("Context created."); |
michael@0 | 24 | |
michael@0 | 25 | function setupTexture(texWidth, texHeight) { |
michael@0 | 26 | var texture = gl.createTexture(); |
michael@0 | 27 | gl.bindTexture(gl.TEXTURE_2D, texture); |
michael@0 | 28 | gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, texWidth, texHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); |
michael@0 | 29 | |
michael@0 | 30 | // this can be quite undeterministic so to improve odds of seeing uninitialized data write bits |
michael@0 | 31 | // into tex then delete texture then re-create one with same characteristics (driver will likely reuse mem) |
michael@0 | 32 | // with this trick on r59046 WebKit/OSX I get FAIL 100% of the time instead of ~15% of the time. |
michael@0 | 33 | |
michael@0 | 34 | var badData = new Uint8Array(texWidth * texHeight * 4); |
michael@0 | 35 | for (var i = 0; i < badData.length; ++i) |
michael@0 | 36 | badData[i] = i % 255; |
michael@0 | 37 | |
michael@0 | 38 | gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, texWidth, texHeight, gl.RGBA, gl.UNSIGNED_BYTE, badData); |
michael@0 | 39 | gl.finish(); // make sure it has been uploaded |
michael@0 | 40 | |
michael@0 | 41 | gl.deleteTexture(texture); |
michael@0 | 42 | gl.finish(); // make sure it has been deleted |
michael@0 | 43 | |
michael@0 | 44 | var texture = gl.createTexture(); |
michael@0 | 45 | gl.bindTexture(gl.TEXTURE_2D, texture); |
michael@0 | 46 | return texture; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | function checkNonZeroPixels(texture, texWidth, texHeight, skipX, skipY, skipWidth, skipHeight, skipR, skipG, skipB, skipA) { |
michael@0 | 50 | gl.bindTexture(gl.TEXTURE_2D, null); |
michael@0 | 51 | var fb = gl.createFramebuffer(); |
michael@0 | 52 | gl.bindFramebuffer(gl.FRAMEBUFFER, fb); |
michael@0 | 53 | gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0); |
michael@0 | 54 | shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE"); |
michael@0 | 55 | |
michael@0 | 56 | var data = new Uint8Array(texWidth * texHeight * 4); |
michael@0 | 57 | gl.readPixels(0, 0, texWidth, texHeight, gl.RGBA, gl.UNSIGNED_BYTE, data); |
michael@0 | 58 | |
michael@0 | 59 | var k = 0; |
michael@0 | 60 | for (var y = 0; y < texHeight; ++y) { |
michael@0 | 61 | for (var x = 0; x < texWidth; ++x) { |
michael@0 | 62 | var index = (y * texWidth + x) * 4; |
michael@0 | 63 | if (x >= skipX && x < skipX + skipWidth && y >= skipY && y < skipY + skipHeight) { |
michael@0 | 64 | if (data[index] != skipR || data[index + 1] != skipG || data[index + 2] != skipB || data[index + 3] != skipA) { |
michael@0 | 65 | testFailed("non-zero pixel values are wrong at (" + x + ", " + y + "), data was (" + |
michael@0 | 66 | data[index] + "," + data[index + 1] + "," + data[index + 2] + "," + data[index + 3] + |
michael@0 | 67 | ") should have been (" + skipR + "," + skipG + "," + skipB + "," + skipA + ")"); |
michael@0 | 68 | return; |
michael@0 | 69 | } |
michael@0 | 70 | } else { |
michael@0 | 71 | for (var i = 0; i < 4; ++i) { |
michael@0 | 72 | if (data[index + i] != 0) |
michael@0 | 73 | k++; |
michael@0 | 74 | } |
michael@0 | 75 | } |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | if (k) { |
michael@0 | 79 | testFailed("Found " + k + " non-zero bytes"); |
michael@0 | 80 | } else { |
michael@0 | 81 | testPassed("All data initialized"); |
michael@0 | 82 | } |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | var width = 512; |
michael@0 | 86 | var height = 512; |
michael@0 | 87 | |
michael@0 | 88 | debug(""); |
michael@0 | 89 | debug("Reading an uninitialized texture (texImage2D) should succeed with all bytes set to 0."); |
michael@0 | 90 | |
michael@0 | 91 | var tex = setupTexture(width, height); |
michael@0 | 92 | gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); |
michael@0 | 93 | checkNonZeroPixels(tex, width, height, 0, 0, 0, 0, 0, 0, 0, 0); |
michael@0 | 94 | gl.deleteTexture(tex); |
michael@0 | 95 | gl.finish(); |
michael@0 | 96 | glErrorShouldBe(gl, gl.NO_ERROR); |
michael@0 | 97 | |
michael@0 | 98 | debug(""); |
michael@0 | 99 | debug("Reading an uninitialized portion of a texture (copyTexImage2D) should succeed with all bytes set to 0."); |
michael@0 | 100 | |
michael@0 | 101 | var tex = setupTexture(width, height); |
michael@0 | 102 | var fbo = gl.createFramebuffer(); |
michael@0 | 103 | gl.bindFramebuffer(gl.FRAMEBUFFER, fbo); |
michael@0 | 104 | var rbo = gl.createRenderbuffer(); |
michael@0 | 105 | gl.bindRenderbuffer(gl.RENDERBUFFER, rbo); |
michael@0 | 106 | var fboWidth = 16; |
michael@0 | 107 | var fboHeight = 16; |
michael@0 | 108 | gl.renderbufferStorage(gl.RENDERBUFFER, gl.RGBA4, fboWidth, fboHeight); |
michael@0 | 109 | gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, rbo); |
michael@0 | 110 | shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE"); |
michael@0 | 111 | gl.clearColor(1.0, 0.0, 0.0, 1.0); |
michael@0 | 112 | gl.clear(gl.COLOR_BUFFER_BIT); |
michael@0 | 113 | glErrorShouldBe(gl, gl.NO_ERROR); |
michael@0 | 114 | gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 0, 0, width, height, 0); |
michael@0 | 115 | checkNonZeroPixels(tex, width, height, 0, 0, fboWidth, fboHeight, 255, 0, 0, 255); |
michael@0 | 116 | gl.deleteTexture(tex); |
michael@0 | 117 | gl.finish(); |
michael@0 | 118 | glErrorShouldBe(gl, gl.NO_ERROR); |
michael@0 | 119 | |
michael@0 | 120 | debug(""); |
michael@0 | 121 | debug("Reading an uninitialized portion of a texture (copyTexImage2D with negative x and y) should succeed with all bytes set to 0."); |
michael@0 | 122 | |
michael@0 | 123 | var tex = setupTexture(width, height); |
michael@0 | 124 | var fbo = gl.createFramebuffer(); |
michael@0 | 125 | gl.bindFramebuffer(gl.FRAMEBUFFER, fbo); |
michael@0 | 126 | var rbo = gl.createRenderbuffer(); |
michael@0 | 127 | gl.bindRenderbuffer(gl.RENDERBUFFER, rbo); |
michael@0 | 128 | var fboWidth = 16; |
michael@0 | 129 | var fboHeight = 16; |
michael@0 | 130 | gl.renderbufferStorage(gl.RENDERBUFFER, gl.RGBA4, fboWidth, fboHeight); |
michael@0 | 131 | gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, rbo); |
michael@0 | 132 | shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE"); |
michael@0 | 133 | gl.clearColor(1.0, 0.0, 0.0, 1.0); |
michael@0 | 134 | gl.clear(gl.COLOR_BUFFER_BIT); |
michael@0 | 135 | glErrorShouldBe(gl, gl.NO_ERROR); |
michael@0 | 136 | var x = -8; |
michael@0 | 137 | var y = -8; |
michael@0 | 138 | gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, x, y, width, height, 0); |
michael@0 | 139 | checkNonZeroPixels(tex, width, height, -x, -y, fboWidth, fboHeight, 255, 0, 0, 255); |
michael@0 | 140 | gl.deleteTexture(tex); |
michael@0 | 141 | gl.finish(); |
michael@0 | 142 | glErrorShouldBe(gl, gl.NO_ERROR); |
michael@0 | 143 | |
michael@0 | 144 | debug(""); |
michael@0 | 145 | debug("Reading an uninitialized portion of a texture (copyTexImage2D from WebGL internal fbo) should succeed with all bytes set to 0."); |
michael@0 | 146 | |
michael@0 | 147 | var tex = setupTexture(width, height); |
michael@0 | 148 | gl.bindFramebuffer(gl.FRAMEBUFFER, null); |
michael@0 | 149 | gl.clearColor(0.0, 1.0, 0.0, 0.0); |
michael@0 | 150 | gl.clear(gl.COLOR_BUFFER_BIT); |
michael@0 | 151 | glErrorShouldBe(gl, gl.NO_ERROR); |
michael@0 | 152 | gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 0, 0, width, height, 0); |
michael@0 | 153 | checkNonZeroPixels(tex, width, height, 0, 0, gl.canvas.width, gl.canvas.height, 0, 255, 0, 0); |
michael@0 | 154 | gl.deleteTexture(tex); |
michael@0 | 155 | gl.finish(); |
michael@0 | 156 | glErrorShouldBe(gl, gl.NO_ERROR); |
michael@0 | 157 | |
michael@0 | 158 | //TODO: uninitialized vertex array buffer |
michael@0 | 159 | //TODO: uninitialized vertex elements buffer |
michael@0 | 160 | //TODO: uninitialized framebuffer? (implementations would need to do a GL clear at first binding?) |
michael@0 | 161 | //TODO: uninitialized renderbuffer? (implementations would need to do a GL clear at first binding?) |
michael@0 | 162 | //TODO: uninitialized uniform arrays? |
michael@0 | 163 | |
michael@0 | 164 | debug(""); |
michael@0 | 165 | successfullyParsed = true; |
michael@0 | 166 | </script> |
michael@0 | 167 | <script>finishTest();</script> |
michael@0 | 168 | </body> |
michael@0 | 169 | </html> |
michael@0 | 170 |