1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
2 "http://www.w3.org/TR/html4/loose.dtd">
5 <title>WebGL ReadPixels conformance test.
</title>
6 <script src=
"../../../resources/js-test.js"></script>
7 <script src=
"resources/webgl-test.js"> </script>
8 <script src=
"resources/webgl-test-utils.js"> </script>
11 <canvas id=
"example" width=
"200" height=
"200" style=
"width: 20px; height: 20px"></canvas>
12 <div id=
"description"></div>
13 <div id=
"console"></div>
15 description("Checks that ReadPixels works as expected.");
18 window
.internals
.settings
.setWebGLErrorsToConsoleEnabled(false);
20 var wtu
= WebGLTestUtils
;
21 var canvas
= document
.getElementById("example");
22 var gl
= create3DContext(canvas
);
24 if (window
.initNonKhronosFramework
) {
25 window
.initNonKhronosFramework(true);
32 var continueTestFunc
= continueTestPart1
;
34 gl
.clearColor(1, 1, 1, 1);
35 gl
.clear(gl
.COLOR_BUFFER_BIT
);
37 // Resize the canvas to 2x2. This is an attempt to get stuff in the backbuffer.
38 // that shouldn't be there.
39 canvas
.addEventListener("webglcontextrestored", continueTestAfterContextRestored
, false);
41 canvas
.height
= height
;
42 if (gl
.getError() != gl
.CONTEXT_LOST_WEBGL
) {
46 function continueTestAfterContextRestored() {
47 window
.gl
= create3DContext(canvas
);
48 var func
= continueTestFunc
;
49 window
.continueTestFunc = function() { testFailed("should not be here"); };
53 function continueTestPart1() {
54 gl
.clearColor(0.5, 0.7, 1.0, 1);
55 gl
.clear(gl
.COLOR_BUFFER_BIT
);
57 var innerColor
= [0.5, 0.7, 1.0, 1];
58 var outerColor
= [0, 0, 0, 0];
61 { msg
: 'in range', checkColor
: innerColor
, x
: 0, y
: 0,
62 oneColor
: innerColor
, oneX
: 0, oneY
: 0},
63 { msg
: 'off top left', checkColor
: outerColor
, x
: -1, y
: -1,
64 oneColor
: innerColor
, oneX
: 1, oneY
: 1},
65 { msg
: 'off bottom right', checkColor
: outerColor
, x
: 1, y
: 1,
66 oneColor
: innerColor
, oneX
: 0, oneY
: 0},
67 { msg
: 'completely off top ', checkColor
: outerColor
, x
: 0, y
: -2,
68 oneColor
: outerColor
, oneX
: 0, oneY
: 0},
69 { msg
: 'completely off bottom', checkColor
: outerColor
, x
: 0, y
: 2,
70 oneColor
: outerColor
, oneX
: 0, oneY
: 0},
71 { msg
: 'completely off left', checkColor
: outerColor
, x
: -2, y
: 0,
72 oneColor
: outerColor
, oneX
: 0, oneY
: 0},
73 { msg
: 'completeley off right', checkColor
: outerColor
, x
: 2, y
: 0,
74 oneColor
: outerColor
, oneX
: 0, oneY
: 0}
77 for (var tt
= 0; tt
< tests
.length
; ++tt
) {
80 debug("checking: " + test
.msg
);
81 checkBuffer(test
.checkColor
, test
.x
, test
.y
,
82 test
.oneColor
, test
.oneX
, test
.oneY
);
85 glErrorShouldBe(gl
, gl
.NO_ERROR
, "there should be no GL errors");
87 function checkBuffer(checkColor
, x
, y
, oneColor
, oneX
, oneY
) {
88 var buf
= new Uint8Array(width
* height
* 4);
89 gl
.readPixels(x
, y
, width
, height
, gl
.RGBA
, gl
.UNSIGNED_BYTE
, buf
);
90 for (var yy
= 0; yy
< height
; ++yy
) {
91 for (var xx
= 0; xx
< width
; ++xx
) {
92 var offset
= (yy
* width
+ xx
) * 4;
93 var expectedColors
= (oneX
== xx
&& oneY
== yy
) ? oneColor
: checkColor
;
94 for (var cc
= 0; cc
< 4; ++cc
) {
95 var expectedColor
= expectedColors
[cc
] * 255;
96 var color
= buf
[offset
+ cc
];
97 var diff
= Math
.abs(expectedColor
- color
);
99 "color pixel at " + xx
+ ", " + yy
+ " should be about " + expectedColor
);
108 type
: gl
.UNSIGNED_BYTE
,
109 dest
: new Uint8Array(3),
110 error
: gl
.INVALID_OPERATION
114 type
: gl
.UNSIGNED_SHORT_5_6_5
,
115 dest
: new Uint8Array(3),
116 error
: gl
.INVALID_OPERATION
120 type
: gl
.UNSIGNED_SHORT_5_5_5_1
,
121 dest
: new Uint16Array(1),
122 error
: gl
.INVALID_OPERATION
126 type
: gl
.UNSIGNED_SHORT_4_4_4_4
,
127 dest
: new Uint16Array(1),
128 error
: gl
.INVALID_OPERATION
132 type
: gl
.UNSIGNED_BYTE
,
133 dest
: new Uint8Array(1),
134 error
: gl
.INVALID_OPERATION
137 format
: gl
.LUMINANCE
,
138 type
: gl
.UNSIGNED_BYTE
,
139 dest
: new Uint8Array(1),
140 error
: gl
.INVALID_ENUM
143 format
: gl
.LUMINANCE_ALPHA
,
144 type
: gl
.UNSIGNED_BYTE
,
145 dest
: new Uint8Array(2),
146 error
: gl
.INVALID_ENUM
150 debug("check disallowed formats");
151 for (var tt
= 0; tt
< badFormats
.length
; ++ tt
) {
152 var info
= badFormats
[tt
]
153 var format
= info
.format
;
154 var type
= info
.type
;
155 var dest
= info
.dest
;
156 var error
= info
.error
;
157 gl
.readPixels(0, 0, 1, 1, format
, type
, dest
);
158 // note that the GL error is INVALID_OPERATION if both format and type are invalid, but
159 // INVALID_ENUM if only one is.
162 "Should not be able to read as " + wtu
.glEnumToString(gl
, format
) +
163 " / " + wtu
.glEnumToString(gl
, type
));
167 debug("check reading with lots of drawing");
168 continueTestFunc
= continueTestPart2
;
171 canvas
.width
= width
;
172 canvas
.height
= height
;
173 if (gl
.getError() != gl
.CONTEXT_LOST_WEBGL
) {
178 function continueTestPart2() {
179 gl
.viewport(0, 0, 1024, 1024);
180 var program
= wtu
.setupTexturedQuad(gl
);
181 var loc
= gl
.getUniformLocation(program
, "tex");
182 gl
.disable(gl
.BLEND
);
183 gl
.disable(gl
.DEPTH_TEST
);
184 var colors
= [[255, 0, 0, 255], [0, 255, 0, 255], [0, 0, 255, 255]];
187 for (var ii
= 0; ii
< colors
.length
; ++ii
) {
188 gl
.activeTexture(gl
.TEXTURE0
+ ii
);
189 var tex
= gl
.createTexture();
190 wtu
.fillTexture(gl
, tex
, 1, 1, colors
[ii
]);
193 for (var ii
= 0; ii
< colors
.length
; ++ii
) {
194 for (var jj
= 0; jj
< 0 + ii
+ 1; ++jj
) {
195 gl
.uniform1i(loc
, jj
% 3);
196 gl
.drawArrays(gl
.TRIANGLES
, 0, 6);
198 var buf
= new Uint8Array(4);
199 gl
.readPixels(512, 512, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, buf
);
201 for (var kk
= 0; kk
< 1; ++kk
) {
202 gl
.uniform1i(loc
, (jj
+ kk
) % 3);
203 gl
.drawArrays(gl
.TRIANGLES
, 0, 6);
206 for (var ii
= 0; ii
< colors
.length
; ++ii
) {
207 var buf
= results
[ii
];
208 var color
= colors
[ii
];
209 actual
= [buf
[0], buf
[1], buf
[2], buf
[3]];
210 expected
= [color
[0], color
[1], color
[2], color
[3]];
211 shouldBe("actual", "expected");
213 glErrorShouldBe(gl
, gl
.NO_ERROR
, "there should be no GL errors");