2 Copyright (c) 2019 The Khronos Group Inc.
3 Use of this source code is governed by an MIT-style license that can be
4 found in the LICENSE.txt file.
10 <meta charset=
"utf-8">
11 <link rel=
"stylesheet" href=
"../../resources/js-test-style.css"/>
12 <script src=
"../../js/js-test-pre.js"></script>
13 <script src=
"../../js/webgl-test-utils.js"></script>
14 <script id=
"vshader" type=
"x-shader/x-vertex">
16 attribute vec4 colorIn;
22 gl_Position = vec4(pos.xyz,
1.0);
26 <script id=
"fshader" type=
"x-shader/x-fragment">
27 precision mediump float;
37 <canvas id=
"example" width=
"32" height=
"32"></canvas>
38 <canvas id=
"example2" width=
"32" height=
"32"></canvas>
39 <div id=
"description"></div>
40 <div id=
"console"></div>
44 // The below declarations need to be global for "shouldBe" to see them
45 var wtu
= WebGLTestUtils
;
48 var pixel
= [ 0, 0, 0, 0 ];
49 var expectedColor
= [ 0, 0, 0, 0 ];
51 function calculatePixelBytes(format
, type
)
68 case gl
.UNSIGNED_BYTE
:
70 case gl
.UNSIGNED_SHORT_5_6_5
:
75 case gl
.UNSIGNED_SHORT_4_4_4_4
:
76 case gl
.UNSIGNED_SHORT_5_5_5_1
:
77 if (format
!= gl
.RGBA
)
87 function calculatePaddingBytes(bytesPerPixel
, packAlignment
, width
)
90 switch (packAlignment
) {
95 padding
= (bytesPerPixel
* width
) % packAlignment
;
97 padding
= packAlignment
- padding
;
105 function packColor(format
, type
, r
, g
, b
, a
)
107 // FIXME: not sure if the color packing is correct for UNSIGNED_SHORT_*.
108 var color
= [ 0, 0, 0, 0 ];
110 case gl
.UNSIGNED_BYTE
:
130 case gl
.UNSIGNED_SHORT_5_6_5
:
131 if (format
!= gl
.RGB
)
136 color
[0] = (r
<< 11) + (g
<< 5) + b
;
138 case gl
.UNSIGNED_SHORT_4_4_4_4
:
139 if (format
!= gl
.RGBA
)
145 color
[0] = (r
<< 12) + (g
<< 8) + (b
<< 4) + a
;
147 case gl
.UNSIGNED_SHORT_5_5_5_1
:
148 if (format
!= gl
.RGBA
)
154 color
[0] = (r
<< 11) + (g
<< 6) + (b
<< 1) + a
;
162 function runTestIteration(format
, type
, packAlignment
, width
, height
)
164 debug("Testing PACK_ALIGNMENT = " + packAlignment
+ ", width = " + width
+ ", height = " + height
);
165 gl
.clearColor(1, 0.4, 0, 1);
166 gl
.clear(gl
.COLOR_BUFFER_BIT
);
167 gl
.pixelStorei(gl
.PACK_ALIGNMENT
, packAlignment
);
168 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
);
169 var bytesPerPixel
= calculatePixelBytes(format
, type
);
170 var padding
= calculatePaddingBytes(bytesPerPixel
, packAlignment
, width
);
171 var size
= bytesPerPixel
* width
* height
+ padding
* (height
- 1);
172 if (type
!= gl
.UNSIGNED_BYTE
) {
173 throw "test error: only UNSIGNED_BYTE is valid to ReadPixels";
177 array
= new Uint8Array(size
);
178 gl
.readPixels(0, 0, width
, height
, format
, type
, array
);
179 if (width
< 0 || height
< 0) {
180 wtu
.glErrorShouldBe(gl
, gl
.INVALID_VALUE
);
184 wtu
.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
;
192 for (var i
= 0; i
< numComponents
; ++i
)
193 pixel
[i
] = array
[pos
+ i
];
194 for (var i
= numComponents
; i
< 4; ++i
)
196 expectedColor
= packColor(format
, type
, 255, 102, 0, 255);
197 shouldBeNonNull("expectedColor");
198 shouldBe("pixel", "expectedColor");
201 description('Verify readPixels() works fine with various PACK_ALIGNMENT values.');
204 debug("<h1>antialias = false</h1>");
205 shouldBeNonNull("gl = wtu.create3DContext('example', {antialias: false})")
206 var formats
= [ gl
.RGBA
];
207 var formatNames
= [ "RGBA" ];
209 debug("<h1>antialias = true</h1>");
210 shouldBeNonNull("gl = wtu.create3DContext('example2', {antialias: true})")
213 function runAllIterations() {
214 shouldBeNonNull("program = wtu.setupProgram(gl, ['vshader', 'fshader'], ['pos', 'colorIn'])");
216 for (var i
= 0; i
< formats
.length
; ++i
) {
217 var format
= formats
[i
];
219 debug("Testing format = " + formatNames
[i
] + " and type = UNSIGNED_BYTE");
220 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 1, 1, 2);
221 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 2, 1, 2);
222 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 4, 1, 2);
223 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 8, 1, 2);
224 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 4, 2, 2);
225 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 8, 2, 2);
226 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 4, 3, 2);
227 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 8, 3, 2);
228 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 4, 4, 2);
229 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 8, 4, 2);
230 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 8, 5, 1);
231 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 4, 5, 2);
232 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 8, 5, 2);
233 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 8, 6, 2);
234 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 8, 7, 2);
235 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 8, 8, 2);
236 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 1, 0, 0);
237 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 2, 0, 0);
238 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 4, 0, 0);
239 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 8, 0, 0);
240 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 1, -1, 1);
241 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 2, 1, -1);
242 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 4, 0, -1);
243 runTestIteration(format
, gl
.UNSIGNED_BYTE
, 8, -1, -1);
246 var successfullyParsed
= true;
249 <script src=
"../../js/js-test-post.js"></script>