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 "use strict"; |
| 43 description("Tests to check user code cannot access uninitialized data from GL r
esources."); |
| 44 |
| 45 var wtu = WebGLTestUtils; |
| 46 var gl = wtu.create3DContext("canvas"); |
| 47 if (!gl) |
| 48 testFailed("Context created."); |
| 49 else |
| 50 testPassed("Context created."); |
| 51 |
| 52 function setupTexture(texWidth, texHeight) { |
| 53 var texture = gl.createTexture(); |
| 54 gl.bindTexture(gl.TEXTURE_2D, texture); |
| 55 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, texWidth, texHeight, 0, gl.RGBA, gl
.UNSIGNED_BYTE, null); |
| 56 |
| 57 // this can be quite undeterministic so to improve odds of seeing uninitiali
zed data write bits |
| 58 // into tex then delete texture then re-create one with same characteristics
(driver will likely reuse mem) |
| 59 // with this trick on r59046 WebKit/OSX I get FAIL 100% of the time instead
of ~15% of the time. |
| 60 |
| 61 var badData = new Uint8Array(texWidth * texHeight * 4); |
| 62 for (var i = 0; i < badData.length; ++i) |
| 63 badData[i] = i % 255; |
| 64 |
| 65 gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, texWidth, texHeight, gl.RGBA, gl.UN
SIGNED_BYTE, badData); |
| 66 gl.finish(); // make sure it has been uploaded |
| 67 |
| 68 gl.deleteTexture(texture); |
| 69 gl.finish(); // make sure it has been deleted |
| 70 |
| 71 var texture = gl.createTexture(); |
| 72 gl.bindTexture(gl.TEXTURE_2D, texture); |
| 73 return texture; |
| 74 } |
| 75 |
| 76 function checkNonZeroPixels(texture, texWidth, texHeight, skipX, skipY, skipWidt
h, skipHeight, skipR, skipG, skipB, skipA) { |
| 77 gl.bindTexture(gl.TEXTURE_2D, null); |
| 78 var fb = gl.createFramebuffer(); |
| 79 gl.bindFramebuffer(gl.FRAMEBUFFER, fb); |
| 80 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D,
texture, 0); |
| 81 shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLE
TE"); |
| 82 |
| 83 var data = new Uint8Array(texWidth * texHeight * 4); |
| 84 gl.readPixels(0, 0, texWidth, texHeight, gl.RGBA, gl.UNSIGNED_BYTE, data); |
| 85 |
| 86 var k = 0; |
| 87 for (var y = 0; y < texHeight; ++y) { |
| 88 for (var x = 0; x < texWidth; ++x) { |
| 89 var index = (y * texWidth + x) * 4; |
| 90 if (x >= skipX && x < skipX + skipWidth && y >= skipY && y < skipY +
skipHeight) { |
| 91 if (data[index] != skipR || data[index + 1] != skipG || data[ind
ex + 2] != skipB || data[index + 3] != skipA) { |
| 92 testFailed("non-zero pixel values are wrong"); |
| 93 return; |
| 94 } |
| 95 } else { |
| 96 for (var i = 0; i < 4; ++i) { |
| 97 if (data[index + i] != 0) |
| 98 k++; |
| 99 } |
| 100 } |
| 101 } |
| 102 } |
| 103 if (k) { |
| 104 testFailed("Found " + k + " non-zero bytes"); |
| 105 } else { |
| 106 testPassed("All data initialized"); |
| 107 } |
| 108 } |
| 109 |
| 110 var width = 512; |
| 111 var height = 512; |
| 112 |
| 113 debug(""); |
| 114 debug("Reading an uninitialized texture (texImage2D) should succeed with all byt
es set to 0."); |
| 115 |
| 116 var tex = setupTexture(width, height); |
| 117 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_
BYTE, null); |
| 118 checkNonZeroPixels(tex, width, height, 0, 0, 0, 0, 0, 0, 0, 0); |
| 119 gl.deleteTexture(tex); |
| 120 gl.finish(); |
| 121 glErrorShouldBe(gl, gl.NO_ERROR); |
| 122 |
| 123 debug(""); |
| 124 debug("Reading an uninitialized portion of a texture (copyTexImage2D) should suc
ceed with all bytes set to 0."); |
| 125 |
| 126 var tex = setupTexture(width, height); |
| 127 var fbo = gl.createFramebuffer(); |
| 128 gl.bindFramebuffer(gl.FRAMEBUFFER, fbo); |
| 129 var rbo = gl.createRenderbuffer(); |
| 130 gl.bindRenderbuffer(gl.RENDERBUFFER, rbo); |
| 131 var fboWidth = 16; |
| 132 var fboHeight = 16; |
| 133 gl.renderbufferStorage(gl.RENDERBUFFER, gl.RGBA4, fboWidth, fboHeight); |
| 134 gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER
, rbo); |
| 135 shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE")
; |
| 136 gl.clearColor(1.0, 0.0, 0.0, 1.0); |
| 137 gl.clear(gl.COLOR_BUFFER_BIT); |
| 138 glErrorShouldBe(gl, gl.NO_ERROR); |
| 139 gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 0, 0, width, height, 0); |
| 140 checkNonZeroPixels(tex, width, height, 0, 0, fboWidth, fboHeight, 255, 0, 0, 255
); |
| 141 gl.deleteTexture(tex); |
| 142 gl.finish(); |
| 143 glErrorShouldBe(gl, gl.NO_ERROR); |
| 144 |
| 145 debug(""); |
| 146 debug("Reading an uninitialized portion of a texture (copyTexImage2D with negati
ve x and y) should succeed with all bytes set to 0."); |
| 147 |
| 148 var tex = setupTexture(width, height); |
| 149 var fbo = gl.createFramebuffer(); |
| 150 gl.bindFramebuffer(gl.FRAMEBUFFER, fbo); |
| 151 var rbo = gl.createRenderbuffer(); |
| 152 gl.bindRenderbuffer(gl.RENDERBUFFER, rbo); |
| 153 var fboWidth = 16; |
| 154 var fboHeight = 16; |
| 155 gl.renderbufferStorage(gl.RENDERBUFFER, gl.RGBA4, fboWidth, fboHeight); |
| 156 gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER
, rbo); |
| 157 shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE")
; |
| 158 gl.clearColor(1.0, 0.0, 0.0, 1.0); |
| 159 gl.clear(gl.COLOR_BUFFER_BIT); |
| 160 glErrorShouldBe(gl, gl.NO_ERROR); |
| 161 var x = -8; |
| 162 var y = -8; |
| 163 gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, x, y, width, height, 0); |
| 164 checkNonZeroPixels(tex, width, height, -x, -y, fboWidth, fboHeight, 255, 0, 0, 2
55); |
| 165 gl.deleteTexture(tex); |
| 166 gl.finish(); |
| 167 glErrorShouldBe(gl, gl.NO_ERROR); |
| 168 |
| 169 debug(""); |
| 170 debug("Reading an uninitialized portion of a texture (copyTexImage2D from WebGL
internal fbo) should succeed with all bytes set to 0."); |
| 171 |
| 172 var tex = setupTexture(width, height); |
| 173 gl.bindFramebuffer(gl.FRAMEBUFFER, null); |
| 174 gl.clearColor(0.0, 1.0, 0.0, 0.0); |
| 175 gl.clear(gl.COLOR_BUFFER_BIT); |
| 176 glErrorShouldBe(gl, gl.NO_ERROR); |
| 177 gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 0, 0, width, height, 0); |
| 178 checkNonZeroPixels(tex, width, height, 0, 0, gl.canvas.width, gl.canvas.height,
0, 255, 0, 0); |
| 179 gl.deleteTexture(tex); |
| 180 gl.finish(); |
| 181 glErrorShouldBe(gl, gl.NO_ERROR); |
| 182 |
| 183 //TODO: uninitialized vertex array buffer |
| 184 //TODO: uninitialized vertex elements buffer |
| 185 //TODO: uninitialized framebuffer? (implementations would need to do a GL clear
at first binding?) |
| 186 //TODO: uninitialized renderbuffer? (implementations would need to do a GL clear
at first binding?) |
| 187 //TODO: uninitialized uniform arrays? |
| 188 |
| 189 debug(""); |
| 190 var successfullyParsed = true; |
| 191 </script> |
| 192 <script src="../../resources/js-test-post.js"></script> |
| 193 </body> |
| 194 </html> |
| 195 |
OLD | NEW |