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