forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
read-pixels-test.html
224 lines (207 loc) · 6.7 KB
/
read-pixels-test.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!--
Copyright (c) 2009 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>WebGL ReadPixels conformance test.</title>
<link rel="stylesheet" href="../resources/js-test-style.css"/>
<script src="../resources/js-test-pre.js"></script>
<script src="resources/webgl-test.js"> </script>
<script src="resources/webgl-test-utils.js"> </script>
</head>
<body>
<canvas id="example" width="200" height="200" style="width: 20px; height: 20px"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
description("Checks that ReadPixels works as expected.");
var wtu = WebGLTestUtils;
var canvas = document.getElementById("example");
var gl = create3DContext(canvas);
if (window.initNonKhronosFramework) {
window.initNonKhronosFramework(true);
}
var actual;
var expected;
var width = 2;
var height = 2;
var continueTestFunc = continueTestPart1;
gl.clearColor(1, 1, 1, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
// Resize the canvas to 2x2. This is an attempt to get stuff in the backbuffer.
// that shouldn't be there.
canvas.addEventListener("webglcontextrestored", continueTestAfterContextRestored, false);
canvas.width = width;
canvas.height = height;
if (gl.getError() != gl.CONTEXT_LOST_WEBGL) {
continueTestPart1();
}
function continueTestAfterContextRestored() {
window.gl = create3DContext(canvas);
var func = continueTestFunc;
window.continueTestFunc = function() { testFailed("should not be here"); };
func();
}
function continueTestPart1() {
gl.clearColor(0.5, 0.7, 1.0, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
var innerColor = [0.5, 0.7, 1.0, 1];
var outerColor = [0, 0, 0, 0];
var tests = [
{ msg: 'in range', checkColor: innerColor, x: 0, y: 0,
oneColor: innerColor, oneX: 0, oneY: 0},
{ msg: 'off top left', checkColor: outerColor, x: -1, y: -1,
oneColor: innerColor, oneX: 1, oneY: 1},
{ msg: 'off bottom right', checkColor: outerColor, x: 1, y: 1,
oneColor: innerColor, oneX: 0, oneY: 0},
{ msg: 'completely off top ', checkColor: outerColor, x: 0, y: -2,
oneColor: outerColor, oneX: 0, oneY: 0},
{ msg: 'completely off bottom', checkColor: outerColor, x: 0, y: 2,
oneColor: outerColor, oneX: 0, oneY: 0},
{ msg: 'completely off left', checkColor: outerColor, x: -2, y: 0,
oneColor: outerColor, oneX: 0, oneY: 0},
{ msg: 'completeley off right', checkColor: outerColor, x: 2, y: 0,
oneColor: outerColor, oneX: 0, oneY: 0}
];
for (var tt = 0; tt < tests.length; ++tt) {
var test = tests[tt];
debug("");
debug("checking: " + test.msg);
checkBuffer(test.checkColor, test.x, test.y,
test.oneColor, test.oneX, test.oneY);
}
glErrorShouldBe(gl, gl.NO_ERROR, "there should be no GL errors");
function checkBuffer(checkColor, x, y, oneColor, oneX, oneY) {
var buf = new Uint8Array(width * height * 4);
gl.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
for (var yy = 0; yy < height; ++yy) {
for (var xx = 0; xx < width; ++xx) {
var offset = (yy * width + xx) * 4;
var expectedColors = (oneX == xx && oneY == yy) ? oneColor : checkColor;
for (var cc = 0; cc < 4; ++cc) {
var expectedColor = expectedColors[cc] * 255;
var color = buf[offset + cc];
var diff = Math.abs(expectedColor - color);
assertMsg(diff < 3,
"color pixel at " + xx + ", " + yy + " should be about " + expectedColor);
}
}
}
}
var badFormats = [
{
format: gl.RGB,
type: gl.UNSIGNED_BYTE,
dest: new Uint8Array(3),
error: gl.INVALID_OPERATION
},
{
format: gl.RGB,
type: gl.UNSIGNED_SHORT_5_6_5,
dest: new Uint8Array(3),
error: gl.INVALID_OPERATION
},
{
format: gl.RGBA,
type: gl.UNSIGNED_SHORT_5_5_5_1,
dest: new Uint16Array(1),
error: gl.INVALID_OPERATION
},
{
format: gl.RGBA,
type: gl.UNSIGNED_SHORT_4_4_4_4,
dest: new Uint16Array(1),
error: gl.INVALID_OPERATION
},
{
format: gl.ALPHA,
type: gl.UNSIGNED_BYTE,
dest: new Uint8Array(1),
error: gl.INVALID_OPERATION
},
{
format: gl.LUMINANCE,
type: gl.UNSIGNED_BYTE,
dest: new Uint8Array(1),
error: gl.INVALID_ENUM
},
{
format: gl.LUMINANCE_ALPHA,
type: gl.UNSIGNED_BYTE,
dest: new Uint8Array(2),
error: gl.INVALID_ENUM
}
];
debug("");
debug("check disallowed formats");
for (var tt = 0; tt < badFormats.length; ++ tt) {
var info = badFormats[tt]
var format = info.format;
var type = info.type;
var dest = info.dest;
var error = info.error;
gl.readPixels(0, 0, 1, 1, format, type, dest);
// note that the GL error is INVALID_OPERATION if both format and type are invalid, but
// INVALID_ENUM if only one is.
glErrorShouldBe(
gl, error,
"Should not be able to read as " + wtu.glEnumToString(gl, format) +
" / " + wtu.glEnumToString(gl, type));
}
debug("");
debug("check reading with lots of drawing");
continueTestFunc = continueTestPart2;
width = 1024;
height = 1024;
canvas.width = width;
canvas.height = height;
if (gl.getError() != gl.CONTEXT_LOST_WEBGL) {
continueTestPart2();
}
}
function continueTestPart2() {
gl.viewport(0, 0, 1024, 1024);
var program = wtu.setupTexturedQuad(gl);
var loc = gl.getUniformLocation(program, "tex");
gl.disable(gl.BLEND);
gl.disable(gl.DEPTH_TEST);
var colors = [[255, 0, 0, 255], [0, 255, 0, 255], [0, 0, 255, 255]];
var textures = [];
var results = [];
for (var ii = 0; ii < colors.length; ++ii) {
gl.activeTexture(gl.TEXTURE0 + ii);
var tex = gl.createTexture();
wtu.fillTexture(gl, tex, 1, 1, colors[ii]);
textures.push(tex);
}
for (var ii = 0; ii < colors.length; ++ii) {
for (var jj = 0; jj < 300 + ii + 1; ++jj) {
gl.uniform1i(loc, jj % 3);
gl.drawArrays(gl.TRIANGLES, 0, 6);
}
var buf = new Uint8Array(4);
gl.readPixels(512, 512, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf);
results.push(buf);
for (var kk = 0; kk < 99; ++kk) {
gl.uniform1i(loc, (jj + kk) % 3);
gl.drawArrays(gl.TRIANGLES, 0, 6);
}
}
for (var ii = 0; ii < colors.length; ++ii) {
var buf = results[ii];
var color = colors[ii];
actual = [buf[0], buf[1], buf[2], buf[3]];
expected = [color[0], color[1], color[2], color[3]];
shouldBe("actual", "expected");
}
glErrorShouldBe(gl, gl.NO_ERROR, "there should be no GL errors");
debug("");
finishTest();
}
</script>
</body>
</html>