5 <script src='/tests/SimpleTest/SimpleTest.js'
></script>
6 <link rel='stylesheet' href='/tests/SimpleTest/test.css'
>
11 function shouldBe(testStr
, refStr
) {
12 ok(testStr
== refStr
, 'Expected "' + refStr
+ '", was "' + testStr
+ '".');
15 function getErrorStr(gl
, err
) {
16 if (!err
) return "NO_ERROR";
26 function glErrorShouldBe(gl
, expected
, opt_info
) {
28 opt_info
= opt_info
+ ': '
33 if (!expected
.length
) {
34 expected
= [expected
];
36 expected
= expected
.map(x
=> getErrorStr(gl
, x
));
37 let was
= gl
.getError();
38 was
= getErrorStr(gl
, was
);
39 console
.log(expected
);
40 ok(expected
.includes(was
),
41 `${opt_info}Expected gl.getError() in [${expected}], was ${was}.`);
45 const gl
= document
.createElement('canvas').getContext('webgl2');
47 todo(false, 'No webgl2, skipping...');
50 const pbo
= gl
.createBuffer();
51 gl
.bindBuffer(gl
.PIXEL_UNPACK_BUFFER
, pbo
);
53 const PBO_DATA
= new Uint8Array([
57 gl
.bufferData(gl
.PIXEL_UNPACK_BUFFER
, PBO_DATA
, gl
.STATIC_DRAW
);
59 const tex
= gl
.createTexture();
60 gl
.bindTexture(gl
.TEXTURE_2D
, tex
);
62 const fb
= gl
.createFramebuffer();
63 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, fb
);
64 gl
.framebufferTexture2D(gl
.FRAMEBUFFER
, gl
.COLOR_ATTACHMENT0
, gl
.TEXTURE_2D
, tex
, 0);
66 const readback
= new Uint8Array(4);
68 const PBO_LIST
= [null, pbo
];
69 for (const cur
of PBO_LIST
) {
70 gl
.bindBuffer(gl
.PIXEL_UNPACK_BUFFER
, cur
);
72 function tryUpload(arg
, expectedReadback
) {
73 let argType
= typeof(arg
);
77 const argForPbo
= (argType
== 'number');
78 const pboBound
= !!cur
;
79 const expectedErr
= (argForPbo
== pboBound
) ? 0 : gl
.INVALID_OPERATION
;
80 gl
.texImage2D(gl
.TEXTURE_2D
, 0, gl
.RGBA8
, 1, 1, 0, gl
.RGBA
, gl
.UNSIGNED_BYTE
,
82 const with_without
= pboBound
? 'with' : 'without';
83 glErrorShouldBe(gl
, expectedErr
, `${with_without} pbo, texImage(..., ${argType}("${arg}"))`);
85 if (expectedErr
) return;
86 gl
.readPixels(0, 0, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, readback
);
87 shouldBe(expectedReadback
.toString(), readback
.toString());
90 const CPU_DATA
= new Uint8Array([255, 255, 0, 255]);
92 tryUpload(null, [0,0,0,0]);
93 tryUpload(CPU_DATA
, CPU_DATA
);
94 tryUpload(0, PBO_DATA
.slice(0,4));
95 tryUpload(4, PBO_DATA
.slice(4));