| 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("webglcontextrestored", continueTestAfterContextRestored
, false); | |
| 43 canvas.width = width; | |
| 44 canvas.height = height; | |
| 45 if (gl.getError() != gl.CONTEXT_LOST_WEBGL) { | |
| 46 continueTestPart1(); | |
| 47 } | |
| 48 | |
| 49 function continueTestAfterContextRestored() { | |
| 50 window.gl = create3DContext(canvas); | |
| 51 var func = continueTestFunc; | |
| 52 window.continueTestFunc = function() { testFailed("should not be here"); }; | |
| 53 func(); | |
| 54 } | |
| 55 | |
| 56 function continueTestPart1() { | |
| 57 gl.clearColor(0.5, 0.7, 1.0, 1); | |
| 58 gl.clear(gl.COLOR_BUFFER_BIT); | |
| 59 | |
| 60 var innerColor = [0.5, 0.7, 1.0, 1]; | |
| 61 var outerColor = [0, 0, 0, 0]; | |
| 62 | |
| 63 var tests = [ | |
| 64 { msg: 'in range', checkColor: innerColor, x: 0, y: 0, | |
| 65 oneColor: innerColor, oneX: 0, oneY: 0}, | |
| 66 { msg: 'off top left', checkColor: outerColor, x: -1, y: -1, | |
| 67 oneColor: innerColor, oneX: 1, oneY: 1}, | |
| 68 { msg: 'off bottom right', checkColor: outerColor, x: 1, y: 1, | |
| 69 oneColor: innerColor, oneX: 0, oneY: 0}, | |
| 70 { msg: 'completely off top ', checkColor: outerColor, x: 0, y: -2, | |
| 71 oneColor: outerColor, oneX: 0, oneY: 0}, | |
| 72 { msg: 'completely off bottom', checkColor: outerColor, x: 0, y: 2, | |
| 73 oneColor: outerColor, oneX: 0, oneY: 0}, | |
| 74 { msg: 'completely off left', checkColor: outerColor, x: -2, y: 0, | |
| 75 oneColor: outerColor, oneX: 0, oneY: 0}, | |
| 76 { msg: 'completeley off right', checkColor: outerColor, x: 2, y: 0, | |
| 77 oneColor: outerColor, oneX: 0, oneY: 0} | |
| 78 ]; | |
| 79 | |
| 80 for (var tt = 0; tt < tests.length; ++tt) { | |
| 81 var test = tests[tt]; | |
| 82 debug(""); | |
| 83 debug("checking: " + test.msg); | |
| 84 checkBuffer(test.checkColor, test.x, test.y, | |
| 85 test.oneColor, test.oneX, test.oneY); | |
| 86 } | |
| 87 | |
| 88 glErrorShouldBe(gl, gl.NO_ERROR, "there should be no GL errors"); | |
| 89 | |
| 90 function checkBuffer(checkColor, x, y, oneColor, oneX, oneY) { | |
| 91 var buf = new Uint8Array(width * height * 4); | |
| 92 gl.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf); | |
| 93 for (var yy = 0; yy < height; ++yy) { | |
| 94 for (var xx = 0; xx < width; ++xx) { | |
| 95 var offset = (yy * width + xx) * 4; | |
| 96 var expectedColors = (oneX == xx && oneY == yy) ? oneColor : checkColor; | |
| 97 for (var cc = 0; cc < 4; ++cc) { | |
| 98 var expectedColor = expectedColors[cc] * 255; | |
| 99 var color = buf[offset + cc]; | |
| 100 var diff = Math.abs(expectedColor - color); | |
| 101 assertMsg(diff < 3, | |
| 102 "color pixel at " + xx + ", " + yy + " should be about " + e
xpectedColor); | |
| 103 } | |
| 104 } | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 var badFormats = [ | |
| 109 { | |
| 110 format: gl.RGB, | |
| 111 type: gl.UNSIGNED_BYTE, | |
| 112 dest: new Uint8Array(3), | |
| 113 error: gl.INVALID_OPERATION | |
| 114 }, | |
| 115 { | |
| 116 format: gl.RGB, | |
| 117 type: gl.UNSIGNED_SHORT_5_6_5, | |
| 118 dest: new Uint8Array(3), | |
| 119 error: gl.INVALID_OPERATION | |
| 120 }, | |
| 121 { | |
| 122 format: gl.RGBA, | |
| 123 type: gl.UNSIGNED_SHORT_5_5_5_1, | |
| 124 dest: new Uint16Array(1), | |
| 125 error: gl.INVALID_OPERATION | |
| 126 }, | |
| 127 { | |
| 128 format: gl.RGBA, | |
| 129 type: gl.UNSIGNED_SHORT_4_4_4_4, | |
| 130 dest: new Uint16Array(1), | |
| 131 error: gl.INVALID_OPERATION | |
| 132 }, | |
| 133 { | |
| 134 format: gl.ALPHA, | |
| 135 type: gl.UNSIGNED_BYTE, | |
| 136 dest: new Uint8Array(1), | |
| 137 error: gl.INVALID_OPERATION | |
| 138 }, | |
| 139 { | |
| 140 format: gl.LUMINANCE, | |
| 141 type: gl.UNSIGNED_BYTE, | |
| 142 dest: new Uint8Array(1), | |
| 143 error: gl.INVALID_ENUM | |
| 144 }, | |
| 145 { | |
| 146 format: gl.LUMINANCE_ALPHA, | |
| 147 type: gl.UNSIGNED_BYTE, | |
| 148 dest: new Uint8Array(2), | |
| 149 error: gl.INVALID_ENUM | |
| 150 } | |
| 151 ]; | |
| 152 debug(""); | |
| 153 debug("check disallowed formats"); | |
| 154 for (var tt = 0; tt < badFormats.length; ++ tt) { | |
| 155 var info = badFormats[tt] | |
| 156 var format = info.format; | |
| 157 var type = info.type; | |
| 158 var dest = info.dest; | |
| 159 var error = info.error; | |
| 160 gl.readPixels(0, 0, 1, 1, format, type, dest); | |
| 161 // note that the GL error is INVALID_OPERATION if both format and type are i
nvalid, but | |
| 162 // INVALID_ENUM if only one is. | |
| 163 glErrorShouldBe( | |
| 164 gl, error, | |
| 165 "Should not be able to read as " + wtu.glEnumToString(gl, format) + | |
| 166 " / " + wtu.glEnumToString(gl, type)); | |
| 167 } | |
| 168 | |
| 169 debug(""); | |
| 170 debug("check reading with lots of drawing"); | |
| 171 continueTestFunc = continueTestPart2; | |
| 172 width = 1024; | |
| 173 height = 1024; | |
| 174 canvas.width = width; | |
| 175 canvas.height = height; | |
| 176 if (gl.getError() != gl.CONTEXT_LOST_WEBGL) { | |
| 177 continueTestPart2(); | |
| 178 } | |
| 179 } | |
| 180 | |
| 181 function continueTestPart2() { | |
| 182 gl.viewport(0, 0, 1024, 1024); | |
| 183 var program = wtu.setupTexturedQuad(gl); | |
| 184 var loc = gl.getUniformLocation(program, "tex"); | |
| 185 gl.disable(gl.BLEND); | |
| 186 gl.disable(gl.DEPTH_TEST); | |
| 187 var colors = [[255, 0, 0, 255], [0, 255, 0, 255], [0, 0, 255, 255]]; | |
| 188 var textures = []; | |
| 189 var results = []; | |
| 190 for (var ii = 0; ii < colors.length; ++ii) { | |
| 191 gl.activeTexture(gl.TEXTURE0 + ii); | |
| 192 var tex = gl.createTexture(); | |
| 193 wtu.fillTexture(gl, tex, 1, 1, colors[ii]); | |
| 194 textures.push(tex); | |
| 195 } | |
| 196 for (var ii = 0; ii < colors.length; ++ii) { | |
| 197 for (var jj = 0; jj < 300 + ii + 1; ++jj) { | |
| 198 gl.uniform1i(loc, jj % 3); | |
| 199 gl.drawArrays(gl.TRIANGLES, 0, 6); | |
| 200 } | |
| 201 var buf = new Uint8Array(4); | |
| 202 gl.readPixels(512, 512, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf); | |
| 203 results.push(buf); | |
| 204 for (var kk = 0; kk < 99; ++kk) { | |
| 205 gl.uniform1i(loc, (jj + kk) % 3); | |
| 206 gl.drawArrays(gl.TRIANGLES, 0, 6); | |
| 207 } | |
| 208 } | |
| 209 for (var ii = 0; ii < colors.length; ++ii) { | |
| 210 var buf = results[ii]; | |
| 211 var color = colors[ii]; | |
| 212 actual = [buf[0], buf[1], buf[2], buf[3]]; | |
| 213 expected = [color[0], color[1], color[2], color[3]]; | |
| 214 shouldBe("actual", "expected"); | |
| 215 } | |
| 216 glErrorShouldBe(gl, gl.NO_ERROR, "there should be no GL errors"); | |
| 217 | |
| 218 debug(""); | |
| 219 finishTest(); | |
| 220 } | |
| 221 </script> | |
| 222 </body> | |
| 223 </html> | |
| 224 | |
| OLD | NEW |