OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 Use of this source code is governed by a BSD-style license that can be |
| 4 found in the LICENSE file. |
| 5 --> |
| 6 <!DOCTYPE html> |
| 7 <html> |
| 8 <head> |
| 9 <meta charset="utf-8"> |
| 10 <title>WebGL ReadPixels conformance test.</title> |
| 11 <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
| 12 <script src="../../resources/js-test-pre.js"></script> |
| 13 <script src="../resources/webgl-test.js"> </script> |
| 14 <script src="../resources/webgl-test-utils.js"> </script> |
| 15 </head> |
| 16 <body> |
| 17 <canvas id="example" width="200" height="200" style="width: 20px; height: 20px">
</canvas> |
| 18 <div id="description"></div> |
| 19 <div id="console"></div> |
| 20 <script> |
| 21 description("Checks that ReadPixels works as expected."); |
| 22 |
| 23 var wtu = WebGLTestUtils; |
| 24 var canvas = document.getElementById("example"); |
| 25 var gl = create3DContext(canvas); |
| 26 |
| 27 if (window.initNonKhronosFramework) { |
| 28 window.initNonKhronosFramework(true); |
| 29 } |
| 30 |
| 31 var actual; |
| 32 var expected; |
| 33 var width = 2; |
| 34 var height = 2; |
| 35 var continueTestFunc = continueTestPart1; |
| 36 |
| 37 gl.clearColor(1, 1, 1, 1); |
| 38 gl.clear(gl.COLOR_BUFFER_BIT); |
| 39 |
| 40 // Resize the canvas to 2x2. This is an attempt to get stuff in the backbuffer. |
| 41 // that shouldn't be there. |
| 42 canvas.addEventListener("webglcontextlost", function(e) { e.preventDefault(); },
false); |
| 43 canvas.addEventListener("webglcontextrestored", continueTestAfterContextRestored
, false); |
| 44 canvas.width = width; |
| 45 canvas.height = height; |
| 46 if (gl.getError() != gl.CONTEXT_LOST_WEBGL) { |
| 47 continueTestPart1(); |
| 48 } |
| 49 |
| 50 function continueTestAfterContextRestored() { |
| 51 window.gl = create3DContext(canvas); |
| 52 var func = continueTestFunc; |
| 53 window.continueTestFunc = function() { testFailed("should not be here"); }; |
| 54 func(); |
| 55 } |
| 56 |
| 57 function continueTestPart1() { |
| 58 gl.clearColor(0.5, 0.7, 1.0, 1); |
| 59 gl.clear(gl.COLOR_BUFFER_BIT); |
| 60 |
| 61 var innerColor = [0.5, 0.7, 1.0, 1]; |
| 62 var outerColor = [0, 0, 0, 0]; |
| 63 |
| 64 var tests = [ |
| 65 { msg: 'in range', checkColor: innerColor, x: 0, y: 0, |
| 66 oneColor: innerColor, oneX: 0, oneY: 0}, |
| 67 { msg: 'off top left', checkColor: outerColor, x: -1, y: -1, |
| 68 oneColor: innerColor, oneX: 1, oneY: 1}, |
| 69 { msg: 'off bottom right', checkColor: outerColor, x: 1, y: 1, |
| 70 oneColor: innerColor, oneX: 0, oneY: 0}, |
| 71 { msg: 'completely off top ', checkColor: outerColor, x: 0, y: -2, |
| 72 oneColor: outerColor, oneX: 0, oneY: 0}, |
| 73 { msg: 'completely off bottom', checkColor: outerColor, x: 0, y: 2, |
| 74 oneColor: outerColor, oneX: 0, oneY: 0}, |
| 75 { msg: 'completely off left', checkColor: outerColor, x: -2, y: 0, |
| 76 oneColor: outerColor, oneX: 0, oneY: 0}, |
| 77 { msg: 'completeley off right', checkColor: outerColor, x: 2, y: 0, |
| 78 oneColor: outerColor, oneX: 0, oneY: 0} |
| 79 ]; |
| 80 |
| 81 for (var tt = 0; tt < tests.length; ++tt) { |
| 82 var test = tests[tt]; |
| 83 debug(""); |
| 84 debug("checking: " + test.msg); |
| 85 checkBuffer(test.checkColor, test.x, test.y, |
| 86 test.oneColor, test.oneX, test.oneY); |
| 87 } |
| 88 |
| 89 glErrorShouldBe(gl, gl.NO_ERROR, "there should be no GL errors"); |
| 90 |
| 91 function checkBuffer(checkColor, x, y, oneColor, oneX, oneY) { |
| 92 var buf = new Uint8Array(width * height * 4); |
| 93 gl.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf); |
| 94 for (var yy = 0; yy < height; ++yy) { |
| 95 for (var xx = 0; xx < width; ++xx) { |
| 96 var offset = (yy * width + xx) * 4; |
| 97 var expectedColors = (oneX == xx && oneY == yy) ? oneColor : checkColor; |
| 98 for (var cc = 0; cc < 4; ++cc) { |
| 99 var expectedColor = expectedColors[cc] * 255; |
| 100 var color = buf[offset + cc]; |
| 101 var diff = Math.abs(expectedColor - color); |
| 102 assertMsg(diff < 3, |
| 103 "color pixel at " + xx + ", " + yy + " should be about " + e
xpectedColor); |
| 104 } |
| 105 } |
| 106 } |
| 107 } |
| 108 |
| 109 var badFormats = [ |
| 110 { |
| 111 format: gl.RGB, |
| 112 type: gl.UNSIGNED_BYTE, |
| 113 dest: new Uint8Array(3), |
| 114 error: gl.INVALID_OPERATION |
| 115 }, |
| 116 { |
| 117 format: gl.RGB, |
| 118 type: gl.UNSIGNED_SHORT_5_6_5, |
| 119 dest: new Uint8Array(3), |
| 120 error: gl.INVALID_OPERATION |
| 121 }, |
| 122 { |
| 123 format: gl.RGBA, |
| 124 type: gl.UNSIGNED_SHORT_5_5_5_1, |
| 125 dest: new Uint16Array(1), |
| 126 error: gl.INVALID_OPERATION |
| 127 }, |
| 128 { |
| 129 format: gl.RGBA, |
| 130 type: gl.UNSIGNED_SHORT_4_4_4_4, |
| 131 dest: new Uint16Array(1), |
| 132 error: gl.INVALID_OPERATION |
| 133 }, |
| 134 { |
| 135 format: gl.ALPHA, |
| 136 type: gl.UNSIGNED_BYTE, |
| 137 dest: new Uint8Array(1), |
| 138 error: gl.INVALID_OPERATION |
| 139 }, |
| 140 { |
| 141 format: gl.LUMINANCE, |
| 142 type: gl.UNSIGNED_BYTE, |
| 143 dest: new Uint8Array(1), |
| 144 error: gl.INVALID_ENUM |
| 145 }, |
| 146 { |
| 147 format: gl.LUMINANCE_ALPHA, |
| 148 type: gl.UNSIGNED_BYTE, |
| 149 dest: new Uint8Array(2), |
| 150 error: gl.INVALID_ENUM |
| 151 } |
| 152 ]; |
| 153 debug(""); |
| 154 debug("check disallowed formats"); |
| 155 for (var tt = 0; tt < badFormats.length; ++ tt) { |
| 156 var info = badFormats[tt] |
| 157 var format = info.format; |
| 158 var type = info.type; |
| 159 var dest = info.dest; |
| 160 var error = info.error; |
| 161 gl.readPixels(0, 0, 1, 1, format, type, dest); |
| 162 // note that the GL error is INVALID_OPERATION if both format and type are i
nvalid, but |
| 163 // INVALID_ENUM if only one is. |
| 164 glErrorShouldBe( |
| 165 gl, error, |
| 166 "Should not be able to read as " + wtu.glEnumToString(gl, format) + |
| 167 " / " + wtu.glEnumToString(gl, type)); |
| 168 } |
| 169 |
| 170 debug(""); |
| 171 debug("check reading with lots of drawing"); |
| 172 continueTestFunc = continueTestPart2; |
| 173 width = 1024; |
| 174 height = 1024; |
| 175 canvas.width = width; |
| 176 canvas.height = height; |
| 177 if (gl.getError() != gl.CONTEXT_LOST_WEBGL) { |
| 178 continueTestPart2(); |
| 179 } |
| 180 } |
| 181 |
| 182 function continueTestPart2() { |
| 183 gl.viewport(0, 0, 1024, 1024); |
| 184 var program = wtu.setupTexturedQuad(gl); |
| 185 var loc = gl.getUniformLocation(program, "tex"); |
| 186 gl.disable(gl.BLEND); |
| 187 gl.disable(gl.DEPTH_TEST); |
| 188 var colors = [[255, 0, 0, 255], [0, 255, 0, 255], [0, 0, 255, 255]]; |
| 189 var textures = []; |
| 190 var results = []; |
| 191 for (var ii = 0; ii < colors.length; ++ii) { |
| 192 gl.activeTexture(gl.TEXTURE0 + ii); |
| 193 var tex = gl.createTexture(); |
| 194 wtu.fillTexture(gl, tex, 1, 1, colors[ii]); |
| 195 textures.push(tex); |
| 196 } |
| 197 for (var ii = 0; ii < colors.length; ++ii) { |
| 198 for (var jj = 0; jj < 300 + ii + 1; ++jj) { |
| 199 gl.uniform1i(loc, jj % 3); |
| 200 gl.drawArrays(gl.TRIANGLES, 0, 6); |
| 201 } |
| 202 var buf = new Uint8Array(4); |
| 203 gl.readPixels(512, 512, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf); |
| 204 results.push(buf); |
| 205 for (var kk = 0; kk < 99; ++kk) { |
| 206 gl.uniform1i(loc, (jj + kk) % 3); |
| 207 gl.drawArrays(gl.TRIANGLES, 0, 6); |
| 208 } |
| 209 } |
| 210 for (var ii = 0; ii < colors.length; ++ii) { |
| 211 var buf = results[ii]; |
| 212 var color = colors[ii]; |
| 213 actual = [buf[0], buf[1], buf[2], buf[3]]; |
| 214 expected = [color[0], color[1], color[2], color[3]]; |
| 215 shouldBe("actual", "expected"); |
| 216 } |
| 217 glErrorShouldBe(gl, gl.NO_ERROR, "there should be no GL errors"); |
| 218 |
| 219 debug(""); |
| 220 finishTest(); |
| 221 } |
| 222 </script> |
| 223 </body> |
| 224 </html> |
| 225 |
OLD | NEW |