5 <script src='/tests/SimpleTest/SimpleTest.js'
></script>
6 <link rel='stylesheet' href='/tests/SimpleTest/test.css'
>
11 var RED
= [1, 0, 0, 1];
12 var GREEN
= [0, 1, 0, 1];
13 var BLUE
= [0, 0, 1, 1];
14 var WHITE
= [1, 1, 1, 1];
15 var ZERO
= [0, 0, 0, 0];
17 function DrawColors(gl
) {
18 var fnClearToColor = function(color
) {
19 gl
.clearColor(color
[0], color
[1], color
[2], color
[3]);
20 gl
.clear(gl
.COLOR_BUFFER_BIT
);
23 gl
.enable(gl
.SCISSOR_TEST
);
30 gl
.scissor(0, 0, 1, 1);
33 gl
.scissor(1, 0, 1, 1);
36 gl
.scissor(0, 1, 1, 1);
37 fnClearToColor(GREEN
);
39 gl
.scissor(1, 1, 1, 1);
40 fnClearToColor(WHITE
);
43 function ClearBufferPair(gl
, byteCount
) {
44 // Using `null` here clears to zero according to WebGL.
45 gl
.bufferData(gl
.PIXEL_PACK_BUFFER
, byteCount
, gl
.STREAM_READ
);
47 var arr
= new Uint8Array(byteCount
);
51 function ColorToString(color
, offset
=0) {
52 var arr
= [ color
[offset
+0],
56 return '[' + arr
.join(', ') + ']';
59 function TestIsUNormColor(refColor
, testData
, offset
) {
60 if (testData
.length
< offset
+ 4) {
61 ok(false, 'testData not long enough.');
65 (refColor
[0] * 255) | 0,
66 (refColor
[1] * 255) | 0,
67 (refColor
[2] * 255) | 0,
68 (refColor
[3] * 255) | 0,
71 var refStr
= ColorToString(refUNormColor
);
72 var testStr
= ColorToString(testData
, offset
);
73 ok(testStr
== refStr
, 'Expected ' + refStr
+ ', was ' + testStr
+ '.');
76 function section(text
) {
78 ok(true, 'Section: ' + text
);
81 function EnsureNoError(gl
) {
82 var glErr
= gl
.getError();
83 while (gl
.getError()) {}
90 var err
= new Error();
91 var stackStr
= err
.stack
;
92 if (stackStr
!== undefined) {
93 var stackArr
= stackStr
.split('\n');
94 stackStr
= stackArr
[1]; // First one after present scope.
95 extraInfo
= ': ' + stackStr
;
98 ok(false, 'Unexpected GL error: 0x' + glErr
.toString(16) + extraInfo
);
101 function TestError(gl
, refErrVal
, str
='') {
103 str
= 'gl.getError()';
105 str
= str
+ ': gl.getError()';
108 var err
= gl
.getError();
109 while (gl
.getError()) {}
111 ShouldBe(err
.toString(16), refErrVal
.toString(16), str
);
114 function ShouldBe(val
, ref
, str
='') {
119 ok(val
== ref
, str
+ 'Should be `' + ref
+ '`, was `' + val
+ '`.');
125 var canvas
= document
.createElement('canvas');
128 canvas
.style
= 'width: 256px; height: 256px; border: 1px solid black;';
129 document
.body
.appendChild(canvas
);
135 gl
= canvas
.getContext('webgl2', attribs
);
137 todo(false, 'WebGL 2 not present, skipping.');
143 TestIsUNormColor(RED
, new Uint8Array([255, 0, 0, 255]), 0);
147 gl
.clearColor(RED
[0], RED
[1], RED
[2], RED
[3]);
148 gl
.clear(gl
.COLOR_BUFFER_BIT
);
150 var data
= new Uint8Array(16);
151 gl
.readPixels(0, 0, 2, 2, gl
.RGBA
, gl
.UNSIGNED_BYTE
, data
);
152 console
.log(JSON
.stringify(data
));
153 TestIsUNormColor(RED
, data
, 0);
162 gl
.readPixels(0, 0, 2, 2, gl
.RGBA
, gl
.UNSIGNED_BYTE
, 0);
163 TestError(gl
, gl
.INVALID_OPERATION
);
165 var data
= new Uint8Array(16);
166 gl
.readPixels(0, 0, 2, 2, gl
.RGBA
, gl
.UNSIGNED_BYTE
, data
);
168 TestIsUNormColor(RED
, data
, 0);
169 TestIsUNormColor(BLUE
, data
, 4);
170 TestIsUNormColor(GREEN
, data
, 8);
171 TestIsUNormColor(WHITE
, data
, 12);
175 var a
= gl
.createBuffer();
176 gl
.bindBuffer(gl
.PIXEL_PACK_BUFFER
, a
);
179 gl
.readPixels(0, 0, 2, 2, gl
.RGBA
, gl
.UNSIGNED_BYTE
, data
);
180 TestError(gl
, gl
.INVALID_OPERATION
);
185 section('Basic readback');
186 data
= ClearBufferPair(gl
, 16);
188 gl
.readPixels(0, 0, 2, 2, gl
.RGBA
, gl
.UNSIGNED_BYTE
, 0);
190 gl
.getBufferSubData(gl
.PIXEL_PACK_BUFFER
, 0, data
);
192 TestIsUNormColor(RED
, data
, 0);
193 TestIsUNormColor(BLUE
, data
, 4);
194 TestIsUNormColor(GREEN
, data
, 8);
195 TestIsUNormColor(WHITE
, data
, 12);
197 section('Subrect readback');
198 data
= ClearBufferPair(gl
, 8);
199 gl
.readPixels(1, 1, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, 0);
200 gl
.getBufferSubData(gl
.PIXEL_PACK_BUFFER
, 0, data
);
202 TestIsUNormColor(WHITE
, data
, 0);
203 TestIsUNormColor(ZERO
, data
, 4);
205 section('ReadPixels offset:4');
206 data
= ClearBufferPair(gl
, 16);
207 gl
.readPixels(1, 1, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, 4);
208 gl
.getBufferSubData(gl
.PIXEL_PACK_BUFFER
, 0, data
);
210 TestIsUNormColor(ZERO
, data
, 0);
211 TestIsUNormColor(WHITE
, data
, 4);
212 TestIsUNormColor(ZERO
, data
, 8);
213 TestIsUNormColor(ZERO
, data
, 12);
215 section('ReadPixels offset:5');
216 gl
.readPixels(1, 1, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, 5);
217 gl
.getBufferSubData(gl
.PIXEL_PACK_BUFFER
, 0, data
);
219 TestIsUNormColor(ZERO
, data
, 0);
220 TestIsUNormColor(WHITE
, data
, 4); // Should remain from previous read.
221 TestIsUNormColor(WHITE
, data
, 5);
222 TestIsUNormColor(ZERO
, data
, 9);
223 TestIsUNormColor(ZERO
, data
, 12);
225 section('GetBufferSubData src too small');
226 data
= ClearBufferPair(gl
, 16);
228 gl
.getBufferSubData(gl
.PIXEL_PACK_BUFFER
, 1, data
);
229 TestError(gl
, gl
.INVALID_VALUE
);
230 TestIsUNormColor(ZERO
, data
, 0);
231 TestIsUNormColor(ZERO
, data
, 4);
232 TestIsUNormColor(ZERO
, data
, 8);
233 TestIsUNormColor(ZERO
, data
, 12);
235 section('GetBufferSubData offset:1');
236 data
= new Uint8Array(15);
237 gl
.readPixels(1, 1, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, 8);
238 gl
.getBufferSubData(gl
.PIXEL_PACK_BUFFER
, 1, data
);
240 TestIsUNormColor(ZERO
, data
, 0);
241 TestIsUNormColor(ZERO
, data
, 3);
242 TestIsUNormColor(WHITE
, data
, 7);
243 TestIsUNormColor(ZERO
, data
, 11);
245 //////////////////////////////////////
247 section('Test packing state');
250 function TestPackState(enumStr
, initialVal
, changedVal
) {
251 var enumVal
= gl
[enumStr
];
253 ShouldBe(gl
.getParameter(enumVal
), initialVal
, 'Initial ' + enumStr
);
254 gl
.pixelStorei(enumVal
, changedVal
);
255 ShouldBe(gl
.getParameter(enumVal
), changedVal
, 'Changed ' + enumStr
);
256 gl
.pixelStorei(enumVal
, initialVal
);
257 ShouldBe(gl
.getParameter(enumVal
), initialVal
, 'Reverted ' + enumStr
);
261 TestPackState('PACK_ALIGNMENT', 4, 1);
262 TestPackState('PACK_ROW_LENGTH', 0, 16);
263 TestPackState('PACK_SKIP_PIXELS', 0, 3);
264 TestPackState('PACK_SKIP_ROWS', 0, 3);
272 SimpleTest
.waitForExplicitFinish();
276 ['webgl.force-enabled', true],
277 ['webgl.disable-angle', true],
279 var prefEnv
= {'set': prefArrArr
};
280 SpecialPowers
.pushPrefEnv(prefEnv
, RunTest
);
282 todo(false, 'No SpecialPowers, but trying anyway...');