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">
7 attribute vec4 colorIn;
13 gl_Position = vec4(pos.xyz,
1.0);
17 <script id=
"fshader" type=
"x-shader/x-fragment">
19 precision mediump float;
30 <canvas id=
"example" width=
"32px" height=
"32px"></canvas>
31 <div id=
"description"></div>
32 <div id=
"console"></div>
35 window
.internals
.settings
.setWebGLErrorsToConsoleEnabled(false);
37 // The below declarations need to be global for "shouldBe" to see them
40 var pixel
= [ 0, 0, 0, 0 ];
41 var expectedColor
= [ 0, 0, 0, 0 ];
43 function calculatePixelBytes(format
, type
)
60 case gl
.UNSIGNED_BYTE
:
62 case gl
.UNSIGNED_SHORT_5_6_5
:
67 case gl
.UNSIGNED_SHORT_4_4_4_4
:
68 case gl
.UNSIGNED_SHORT_5_5_5_1
:
69 if (format
!= gl
.RGBA
)
79 function calculatePaddingBytes(bytesPerPixel
, packAlignment
, width
)
82 switch (packAlignment
) {
87 padding
= (bytesPerPixel
* width
) % packAlignment
;
89 padding
= packAlignment
- padding
;
97 function packColor(format
, type
, r
, g
, b
, a
)
99 // FIXME: not sure if the color packing is correct for UNSIGNED_SHORT_*.
100 var color
= [ 0, 0, 0, 0 ];
102 case gl
.UNSIGNED_BYTE
:
122 case gl
.UNSIGNED_SHORT_5_6_5
:
123 if (format
!= gl
.RGB
)
128 color
[0] = (r
<< 11) + (g
<< 5) + b
;
130 case gl
.UNSIGNED_SHORT_4_4_4_4
:
131 if (format
!= gl
.RGBA
)
137 color
[0] = (r
<< 12) + (g
<< 8) + (b
<< 4) + a
;
139 case gl
.UNSIGNED_SHORT_5_5_5_1
:
140 if (format
!= gl
.RGBA
)
146 color
[0] = (r
<< 11) + (g
<< 6) + (b
<< 1) + a
;
154 function runTestIteration(format
, type
, packAlignment
, width
, height
)
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);
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
:
175 if (type
== gl
.UNSIGNED_BYTE
)
176 array
= new Uint8Array(size
);
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
);
184 glErrorShouldBe(gl
, gl
.NO_ERROR
);
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
;
196 for (var i
= 0; i
< numComponents
; ++i
)
197 pixel
[i
] = array
[pos
+ i
];
198 for (var i
= numComponents
; i
< 4; ++i
)
200 expectedColor
= packColor(format
, type
, 255, 102, 0, 255);
201 shouldBeNonNull(expectedColor
);
202 shouldBe("pixel", "expectedColor");
205 description('Verify readPixels() works fine with various PACK_ALIGNMENT values.');
207 shouldBeNonNull("gl = initWebGL('example', 'vshader', 'fshader', [ 'pos', 'colorIn' ], [ 0, 0, 0, 1 ], 1)");
208 gl
.disable(gl
.BLEND
);
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);