OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 |
| 3 /* |
| 4 ** Copyright (c) 2012 The Khronos Group Inc. |
| 5 ** |
| 6 ** Permission is hereby granted, free of charge, to any person obtaining a |
| 7 ** copy of this software and/or associated documentation files (the |
| 8 ** "Materials"), to deal in the Materials without restriction, including |
| 9 ** without limitation the rights to use, copy, modify, merge, publish, |
| 10 ** distribute, sublicense, and/or sell copies of the Materials, and to |
| 11 ** permit persons to whom the Materials are furnished to do so, subject to |
| 12 ** the following conditions: |
| 13 ** |
| 14 ** The above copyright notice and this permission notice shall be included |
| 15 ** in all copies or substantial portions of the Materials. |
| 16 ** |
| 17 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 21 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 22 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 23 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 24 */ |
| 25 |
| 26 --> |
| 27 <!DOCTYPE html> |
| 28 <html> |
| 29 <head> |
| 30 <meta charset="utf-8"> |
| 31 <title>WebGL Uninitialized GL Resources Tests</title> |
| 32 <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
| 33 <script src="../../resources/js-test-pre.js"></script> |
| 34 <script src="../resources/webgl-test.js"></script> |
| 35 <script src="../resources/webgl-test-utils.js"></script> |
| 36 </head> |
| 37 <body> |
| 38 <div id="description"></div> |
| 39 <div id="console"></div> |
| 40 <canvas id="canvas" width="2" height="2"> </canvas> |
| 41 <script> |
| 42 description("Tests to check user code cannot access uninitialized data from GL r
esources."); |
| 43 |
| 44 var wtu = WebGLTestUtils; |
| 45 var gl = wtu.create3DContext("canvas"); |
| 46 if (!gl) |
| 47 testFailed("Context created."); |
| 48 else |
| 49 testPassed("Context created."); |
| 50 |
| 51 function setupTexture(texWidth, texHeight) { |
| 52 var texture = gl.createTexture(); |
| 53 gl.bindTexture(gl.TEXTURE_2D, texture); |
| 54 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, texWidth, texHeight, 0, gl.RGBA, gl
.UNSIGNED_BYTE, null); |
| 55 |
| 56 // this can be quite undeterministic so to improve odds of seeing uninitiali
zed data write bits |
| 57 // into tex then delete texture then re-create one with same characteristics
(driver will likely reuse mem) |
| 58 // with this trick on r59046 WebKit/OSX I get FAIL 100% of the time instead
of ~15% of the time. |
| 59 |
| 60 var badData = new Uint8Array(texWidth * texHeight * 4); |
| 61 for (var i = 0; i < badData.length; ++i) |
| 62 badData[i] = i % 255; |
| 63 |
| 64 gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, texWidth, texHeight, gl.RGBA, gl.UN
SIGNED_BYTE, badData); |
| 65 gl.finish(); // make sure it has been uploaded |
| 66 |
| 67 gl.deleteTexture(texture); |
| 68 gl.finish(); // make sure it has been deleted |
| 69 |
| 70 var texture = gl.createTexture(); |
| 71 gl.bindTexture(gl.TEXTURE_2D, texture); |
| 72 return texture; |
| 73 } |
| 74 |
| 75 function checkNonZeroPixels(texture, texWidth, texHeight, skipX, skipY, skipWidt
h, skipHeight, skipR, skipG, skipB, skipA) { |
| 76 gl.bindTexture(gl.TEXTURE_2D, null); |
| 77 var fb = gl.createFramebuffer(); |
| 78 gl.bindFramebuffer(gl.FRAMEBUFFER, fb); |
| 79 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D,
texture, 0); |
| 80 shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLE
TE"); |
| 81 |
| 82 var data = new Uint8Array(texWidth * texHeight * 4); |
| 83 gl.readPixels(0, 0, texWidth, texHeight, gl.RGBA, gl.UNSIGNED_BYTE, data); |
| 84 |
| 85 var k = 0; |
| 86 for (var y = 0; y < texHeight; ++y) { |
| 87 for (var x = 0; x < texWidth; ++x) { |
| 88 var index = (y * texWidth + x) * 4; |
| 89 if (x >= skipX && x < skipX + skipWidth && y >= skipY && y < skipY +
skipHeight) { |
| 90 if (data[index] != skipR || data[index + 1] != skipG || data[ind
ex + 2] != skipB || data[index + 3] != skipA) { |
| 91 testFailed("non-zero pixel values are wrong"); |
| 92 return; |
| 93 } |
| 94 } else { |
| 95 for (var i = 0; i < 4; ++i) { |
| 96 if (data[index + i] != 0) |
| 97 k++; |
| 98 } |
| 99 } |
| 100 } |
| 101 } |
| 102 if (k) { |
| 103 testFailed("Found " + k + " non-zero bytes"); |
| 104 } else { |
| 105 testPassed("All data initialized"); |
| 106 } |
| 107 } |
| 108 |
| 109 var width = 512; |
| 110 var height = 512; |
| 111 |
| 112 debug(""); |
| 113 debug("Reading an uninitialized texture (texImage2D) should succeed with all byt
es set to 0."); |
| 114 |
| 115 var tex = setupTexture(width, height); |
| 116 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_
BYTE, null); |
| 117 checkNonZeroPixels(tex, width, height, 0, 0, 0, 0, 0, 0, 0, 0); |
| 118 gl.deleteTexture(tex); |
| 119 gl.finish(); |
| 120 glErrorShouldBe(gl, gl.NO_ERROR); |
| 121 |
| 122 debug(""); |
| 123 debug("Reading an uninitialized portion of a texture (copyTexImage2D) should suc
ceed with all bytes set to 0."); |
| 124 |
| 125 var tex = setupTexture(width, height); |
| 126 var fbo = gl.createFramebuffer(); |
| 127 gl.bindFramebuffer(gl.FRAMEBUFFER, fbo); |
| 128 var rbo = gl.createRenderbuffer(); |
| 129 gl.bindRenderbuffer(gl.RENDERBUFFER, rbo); |
| 130 var fboWidth = 16; |
| 131 var fboHeight = 16; |
| 132 gl.renderbufferStorage(gl.RENDERBUFFER, gl.RGBA4, fboWidth, fboHeight); |
| 133 gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER
, rbo); |
| 134 shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE")
; |
| 135 gl.clearColor(1.0, 0.0, 0.0, 1.0); |
| 136 gl.clear(gl.COLOR_BUFFER_BIT); |
| 137 glErrorShouldBe(gl, gl.NO_ERROR); |
| 138 gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 0, 0, width, height, 0); |
| 139 checkNonZeroPixels(tex, width, height, 0, 0, fboWidth, fboHeight, 255, 0, 0, 255
); |
| 140 gl.deleteTexture(tex); |
| 141 gl.finish(); |
| 142 glErrorShouldBe(gl, gl.NO_ERROR); |
| 143 |
| 144 debug(""); |
| 145 debug("Reading an uninitialized portion of a texture (copyTexImage2D with negati
ve x and y) should succeed with all bytes set to 0."); |
| 146 |
| 147 var tex = setupTexture(width, height); |
| 148 var fbo = gl.createFramebuffer(); |
| 149 gl.bindFramebuffer(gl.FRAMEBUFFER, fbo); |
| 150 var rbo = gl.createRenderbuffer(); |
| 151 gl.bindRenderbuffer(gl.RENDERBUFFER, rbo); |
| 152 var fboWidth = 16; |
| 153 var fboHeight = 16; |
| 154 gl.renderbufferStorage(gl.RENDERBUFFER, gl.RGBA4, fboWidth, fboHeight); |
| 155 gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER
, rbo); |
| 156 shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE")
; |
| 157 gl.clearColor(1.0, 0.0, 0.0, 1.0); |
| 158 gl.clear(gl.COLOR_BUFFER_BIT); |
| 159 glErrorShouldBe(gl, gl.NO_ERROR); |
| 160 var x = -8; |
| 161 var y = -8; |
| 162 gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, x, y, width, height, 0); |
| 163 checkNonZeroPixels(tex, width, height, -x, -y, fboWidth, fboHeight, 255, 0, 0, 2
55); |
| 164 gl.deleteTexture(tex); |
| 165 gl.finish(); |
| 166 glErrorShouldBe(gl, gl.NO_ERROR); |
| 167 |
| 168 debug(""); |
| 169 debug("Reading an uninitialized portion of a texture (copyTexImage2D from WebGL
internal fbo) should succeed with all bytes set to 0."); |
| 170 |
| 171 var tex = setupTexture(width, height); |
| 172 gl.bindFramebuffer(gl.FRAMEBUFFER, null); |
| 173 gl.clearColor(0.0, 1.0, 0.0, 0.0); |
| 174 gl.clear(gl.COLOR_BUFFER_BIT); |
| 175 glErrorShouldBe(gl, gl.NO_ERROR); |
| 176 gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 0, 0, width, height, 0); |
| 177 checkNonZeroPixels(tex, width, height, 0, 0, gl.canvas.width, gl.canvas.height,
0, 255, 0, 0); |
| 178 gl.deleteTexture(tex); |
| 179 gl.finish(); |
| 180 glErrorShouldBe(gl, gl.NO_ERROR); |
| 181 |
| 182 //TODO: uninitialized vertex array buffer |
| 183 //TODO: uninitialized vertex elements buffer |
| 184 //TODO: uninitialized framebuffer? (implementations would need to do a GL clear
at first binding?) |
| 185 //TODO: uninitialized renderbuffer? (implementations would need to do a GL clear
at first binding?) |
| 186 //TODO: uninitialized uniform arrays? |
| 187 |
| 188 debug(""); |
| 189 successfullyParsed = true; |
| 190 </script> |
| 191 <script src="../../resources/js-test-post.js"></script> |
| 192 </body> |
| 193 </html> |
| 194 |
OLD | NEW |