| OLD | NEW |
| (Empty) |
| 1 <html> | |
| 2 <head> | |
| 3 <script src="../../../resources/js-test.js"></script> | |
| 4 <script src="resources/webgl-test.js"></script> | |
| 5 <script id="vshader" type="x-shader/x-vertex"> | |
| 6 attribute vec3 pos; | |
| 7 attribute vec4 colorIn; | |
| 8 varying vec4 color; | |
| 9 | |
| 10 void main() | |
| 11 { | |
| 12 color = colorIn; | |
| 13 gl_Position = vec4(pos.xyz, 1.0); | |
| 14 } | |
| 15 </script> | |
| 16 | |
| 17 <script id="fshader" type="x-shader/x-fragment"> | |
| 18 #ifdef GL_ES | |
| 19 precision mediump float; | |
| 20 #endif | |
| 21 varying vec4 color; | |
| 22 | |
| 23 void main() | |
| 24 { | |
| 25 gl_FragColor = color; | |
| 26 } | |
| 27 </script> | |
| 28 </head> | |
| 29 <body> | |
| 30 <canvas id="example" width="32px" height="32px"></canvas> | |
| 31 <div id="description"></div> | |
| 32 <div id="console"></div> | |
| 33 <script> | |
| 34 if (window.internals) | |
| 35 window.internals.settings.setWebGLErrorsToConsoleEnabled(false); | |
| 36 | |
| 37 // The below declarations need to be global for "shouldBe" to see them | |
| 38 var gl = null; | |
| 39 var array = null; | |
| 40 var pixel = [ 0, 0, 0, 0 ]; | |
| 41 var expectedColor = [ 0, 0, 0, 0 ]; | |
| 42 | |
| 43 function calculatePixelBytes(format, type) | |
| 44 { | |
| 45 var size = 0; | |
| 46 switch (format) { | |
| 47 case gl.ALPHA: | |
| 48 size = 1; | |
| 49 break; | |
| 50 case gl.RGB: | |
| 51 size = 3; | |
| 52 break; | |
| 53 case gl.RGBA: | |
| 54 size = 4; | |
| 55 break; | |
| 56 default: | |
| 57 return -1; | |
| 58 } | |
| 59 switch (type) { | |
| 60 case gl.UNSIGNED_BYTE: | |
| 61 break; | |
| 62 case gl.UNSIGNED_SHORT_5_6_5: | |
| 63 if (format != gl.RGB) | |
| 64 return -1; | |
| 65 size = 2; | |
| 66 break; | |
| 67 case gl.UNSIGNED_SHORT_4_4_4_4: | |
| 68 case gl.UNSIGNED_SHORT_5_5_5_1: | |
| 69 if (format != gl.RGBA) | |
| 70 return -1; | |
| 71 size = 2; | |
| 72 break; | |
| 73 default: | |
| 74 return -1; | |
| 75 } | |
| 76 return size; | |
| 77 } | |
| 78 | |
| 79 function calculatePaddingBytes(bytesPerPixel, packAlignment, width) | |
| 80 { | |
| 81 var padding = 0; | |
| 82 switch (packAlignment) { | |
| 83 case 1: | |
| 84 case 2: | |
| 85 case 4: | |
| 86 case 8: | |
| 87 padding = (bytesPerPixel * width) % packAlignment; | |
| 88 if (padding > 0) | |
| 89 padding = packAlignment - padding; | |
| 90 break; | |
| 91 default: | |
| 92 return -1; | |
| 93 } | |
| 94 return padding; | |
| 95 } | |
| 96 | |
| 97 function packColor(format, type, r, g, b, a) | |
| 98 { | |
| 99 // FIXME: not sure if the color packing is correct for UNSIGNED_SHORT_*. | |
| 100 var color = [ 0, 0, 0, 0 ]; | |
| 101 switch (type) { | |
| 102 case gl.UNSIGNED_BYTE: | |
| 103 switch (format) { | |
| 104 case gl.ALPHA: | |
| 105 color[0] = a; | |
| 106 break; | |
| 107 case gl.RGB: | |
| 108 color[0] = r; | |
| 109 color[1] = g; | |
| 110 color[2] = b; | |
| 111 break; | |
| 112 case gl.RGBA: | |
| 113 color[0] = r; | |
| 114 color[1] = g; | |
| 115 color[2] = b; | |
| 116 color[3] = a; | |
| 117 break; | |
| 118 default: | |
| 119 return null; | |
| 120 } | |
| 121 break; | |
| 122 case gl.UNSIGNED_SHORT_5_6_5: | |
| 123 if (format != gl.RGB) | |
| 124 return null; | |
| 125 r >>= 3; | |
| 126 g >>= 2; | |
| 127 b >>= 3; | |
| 128 color[0] = (r << 11) + (g << 5) + b; | |
| 129 break; | |
| 130 case gl.UNSIGNED_SHORT_4_4_4_4: | |
| 131 if (format != gl.RGBA) | |
| 132 return null; | |
| 133 r >>= 4; | |
| 134 g >>= 4; | |
| 135 b >>= 4; | |
| 136 a >>= 4; | |
| 137 color[0] = (r << 12) + (g << 8) + (b << 4) + a; | |
| 138 break; | |
| 139 case gl.UNSIGNED_SHORT_5_5_5_1: | |
| 140 if (format != gl.RGBA) | |
| 141 return null; | |
| 142 r >>= 3; | |
| 143 g >>= 3; | |
| 144 b >>= 3; | |
| 145 a >>= 7; | |
| 146 color[0] = (r << 11) + (g << 6) + (b << 1) + a; | |
| 147 break; | |
| 148 Default: | |
| 149 return null; | |
| 150 } | |
| 151 return color; | |
| 152 } | |
| 153 | |
| 154 function runTestIteration(format, type, packAlignment, width, height) | |
| 155 { | |
| 156 debug("Testing PACK_ALIGNMENT = " + packAlignment + ", width = " + width + "
, height = " + height); | |
| 157 gl.clearColor(1, 0.4, 0, 1); | |
| 158 gl.clear(gl.COLOR_BUFFER_BIT); | |
| 159 gl.pixelStorei(gl.PACK_ALIGNMENT, packAlignment); | |
| 160 glErrorShouldBe(gl, gl.NO_ERROR); | |
| 161 var bytesPerPixel = calculatePixelBytes(format, type); | |
| 162 var padding = calculatePaddingBytes(bytesPerPixel, packAlignment, width); | |
| 163 var size = bytesPerPixel * width * height + padding * (height - 1); | |
| 164 var isShort = false; | |
| 165 switch (type) { | |
| 166 case gl.UNSIGNED_SHORT_5_6_5: | |
| 167 case gl.UNSIGNED_SHORT_4_4_4_4: | |
| 168 case gl.UNSIGNED_SHORT_5_5_5_1: | |
| 169 isShort = true; | |
| 170 } | |
| 171 if (isShort) | |
| 172 size /= 2; | |
| 173 if (size < 0) | |
| 174 size = 0; | |
| 175 if (type == gl.UNSIGNED_BYTE) | |
| 176 array = new Uint8Array(size); | |
| 177 else | |
| 178 array = new Uint16Array(size); | |
| 179 gl.readPixels(0, 0, width, height, format, type, array); | |
| 180 if (width < 0 || height < 0) { | |
| 181 glErrorShouldBe(gl, gl.INVALID_VALUE); | |
| 182 return; | |
| 183 } else { | |
| 184 glErrorShouldBe(gl, gl.NO_ERROR); | |
| 185 if (!array.length) | |
| 186 return; | |
| 187 } | |
| 188 // Check the last pixel of the last row. | |
| 189 var bytesPerRow = width * bytesPerPixel + padding; | |
| 190 var pos = bytesPerRow * (height - 1) + (width - 1) * bytesPerPixel; | |
| 191 var numComponents = bytesPerPixel; | |
| 192 if (isShort) { | |
| 193 pos /= 2; | |
| 194 numComponents /= 2; | |
| 195 } | |
| 196 for (var i = 0; i < numComponents; ++i) | |
| 197 pixel[i] = array[pos + i]; | |
| 198 for (var i = numComponents; i < 4; ++i) | |
| 199 pixel[i] = 0; | |
| 200 expectedColor = packColor(format, type, 255, 102, 0, 255); | |
| 201 shouldBeNonNull(expectedColor); | |
| 202 shouldBe("pixel", "expectedColor"); | |
| 203 } | |
| 204 | |
| 205 description('Verify readPixels() works fine with various PACK_ALIGNMENT values.'
); | |
| 206 | |
| 207 shouldBeNonNull("gl = initWebGL('example', 'vshader', 'fshader', [ 'pos', 'color
In' ], [ 0, 0, 0, 1 ], 1)"); | |
| 208 gl.disable(gl.BLEND); | |
| 209 | |
| 210 debug("Testing format = RGBA and type = UNSIGNED_BYTE"); | |
| 211 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 1, 1, 2); | |
| 212 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 2, 1, 2); | |
| 213 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 4, 1, 2); | |
| 214 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 8, 1, 2); | |
| 215 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 4, 2, 2); | |
| 216 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 8, 2, 2); | |
| 217 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 4, 3, 2); | |
| 218 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 8, 3, 2); | |
| 219 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 4, 4, 2); | |
| 220 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 8, 4, 2); | |
| 221 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 8, 5, 1); | |
| 222 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 4, 5, 2); | |
| 223 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 8, 5, 2); | |
| 224 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 8, 6, 2); | |
| 225 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 8, 7, 2); | |
| 226 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 8, 8, 2); | |
| 227 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 1, 0, 0); | |
| 228 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 2, 0, 0); | |
| 229 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 4, 0, 0); | |
| 230 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 8, 0, 0); | |
| 231 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 1, -1, 1); | |
| 232 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 2, 1, -1); | |
| 233 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 4, 0, -1); | |
| 234 runTestIteration(gl.RGBA, gl.UNSIGNED_BYTE, 8, -1, -1); | |
| 235 </script> | |
| 236 </body> | |
| 237 </html> | |
| OLD | NEW |