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