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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
<!--
/*
** Copyright (c) 2012 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"></script>
<script id="vshader" type="x-shader/x-vertex">
attribute vec3 pos;
attribute vec4 colorIn;
varying vec4 color;
void main()
{
color = colorIn;
gl_Position = vec4(pos.xyz, 1.0);
}
</script>
<script id="fshader" type="x-shader/x-fragment">
precision mediump float;
varying vec4 color;
void main()
{
gl_FragColor = color;
}
</script>
<script>
"use strict";
// These four declarations need to be global for "shouldBe" to see them
var wtu = WebGLTestUtils;
var gl;
var contextAttribs = null;
var redChannels = [0, 0, 0];
var correctColor = null;
var framebuffer;
var fbHasColor;
var fbHasDepth;
var fbHasStencil;
function init()
{
description('Verify WebGLContextAttributes are working as specified, including alpha, depth, stencil, antialias, but not premultipliedAlpha');
runTest();
}
function getWebGL(canvasWidth, canvasHeight, contextAttribs, clearColor, clearDepth, clearStencil)
{
var canvas = document.createElement("canvas");
if (!canvas)
return null;
canvas.width = canvasWidth;
canvas.height = canvasHeight;
gl = wtu.create3DContext(canvas, contextAttribs);
if (!gl)
return null;
var program = wtu.setupProgram(gl, ["vshader", "fshader"], ["pos", "colorIn"]);
if (!program)
return null;
gl.enable(gl.DEPTH_TEST);
gl.enable(gl.STENCIL_TEST);
gl.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
gl.clearDepth(clearDepth);
gl.clearStencil(clearStencil);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
framebuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.canvas.width, gl.canvas.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
fbHasStencil = false;
fbHasDepth = false;
fbHasColor = gl.checkFramebufferStatus(gl.FRAMEBUFFER) == gl.FRAMEBUFFER_COMPLETE;
if (fbHasColor) {
var depthStencil = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, depthStencil);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_STENCIL, gl.canvas.width, gl.canvas.height);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, depthStencil);
fbHasDepth = gl.checkFramebufferStatus(gl.FRAMEBUFFER) == gl.FRAMEBUFFER_COMPLETE;
if (!fbHasDepth) {
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, null);
shouldBe('gl.checkFramebufferStatus(gl.FRAMEBUFFER)', 'gl.FRAMEBUFFER_COMPLETE');
} else {
fbHasStencil = true;
}
}
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
return gl;
}
function drawAndReadPixel(gl, vertices, colors)
{
var colorOffset = vertices.byteLength;
var vbo = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
gl.bufferData(gl.ARRAY_BUFFER, colorOffset + colors.byteLength, gl.STATIC_DRAW);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, vertices);
gl.bufferSubData(gl.ARRAY_BUFFER, colorOffset, colors);
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(1, 4, gl.UNSIGNED_BYTE, true, 0, colorOffset);
gl.enableVertexAttribArray(1);
gl.drawArrays(gl.TRIANGLES, 0, vertices.length / 3);
}
function testDefault()
{
debug("Testing default attributes: { stencil:false }");
shouldBeNonNull("gl = getWebGL(1, 1, null, [ 0, 0, 0, 0 ], 1, 0)");
shouldBeFalse("gl.getContextAttributes().stencil");
shouldBeTrue("gl.getParameter(gl.STENCIL_BITS) == 0");
}
function testAlpha(alpha)
{
debug("Testing alpha = " + alpha);
if (alpha) {
shouldBeNonNull("gl = getWebGL(1, 1, { alpha: true, depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 0 ], 1, 0)");
shouldBeTrue("gl.getParameter(gl.ALPHA_BITS) >= 8");
} else {
shouldBeNonNull("gl = getWebGL(1, 1, { alpha: false, depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 0 ], 1, 0)");
shouldBeTrue("gl.getParameter(gl.ALPHA_BITS) == 0");
}
shouldBeTrue("gl.getParameter(gl.RED_BITS) >= 8");
shouldBeTrue("gl.getParameter(gl.GREEN_BITS) >= 8");
shouldBeTrue("gl.getParameter(gl.BLUE_BITS) >= 8");
shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) == 0");
shouldBeTrue("gl.getParameter(gl.STENCIL_BITS) == 0");
shouldBeNonNull("contextAttribs = gl.getContextAttributes()");
shouldBeTrue("contextAttribs.alpha == " + alpha);
var correctColor = (contextAttribs.alpha ? [0, 0, 0, 0] : [0, 0, 0, 255]);
wtu.checkCanvasRect(gl, 0, 0, 1, 1, correctColor);
if (fbHasColor) {
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
gl.clearColor(0.5, 0.5, 0.5, 0.5);
gl.clear(gl.COLOR_BUFFER_BIT);
wtu.checkCanvasRect(gl, 0, 0, 1, 1, [127, 127, 127, 127], undefined, 1);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
}
function testDepth(depth)
{
debug("Testing depth = " + depth);
if (depth) {
shouldBeNonNull("gl = getWebGL(1, 1, { stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) >= 16");
} else {
shouldBeNonNull("gl = getWebGL(1, 1, { depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) == 0");
}
shouldBeTrue("gl.getParameter(gl.RED_BITS) >= 8");
shouldBeTrue("gl.getParameter(gl.GREEN_BITS) >= 8");
shouldBeTrue("gl.getParameter(gl.BLUE_BITS) >= 8");
shouldBeTrue("gl.getParameter(gl.ALPHA_BITS) >= 8");
shouldBeNonNull("contextAttribs = gl.getContextAttributes()");
gl.depthFunc(gl.NEVER);
var vertices = new Float32Array([
1.0, 1.0, 0.0,
-1.0, 1.0, 0.0,
-1.0, -1.0, 0.0,
1.0, 1.0, 0.0,
-1.0, -1.0, 0.0,
1.0, -1.0, 0.0]);
var colors = new Uint8Array([
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255]);
drawAndReadPixel(gl, vertices, colors, 0, 0);
correctColor = (contextAttribs.depth ? [0, 0, 0, 255] : [255, 0, 0, 255]);
wtu.checkCanvasRect(gl, 0, 0, 1, 1, correctColor);
if (fbHasDepth) {
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
drawAndReadPixel(gl, vertices, colors, 0, 0);
wtu.checkCanvasRect(gl, 0, 0, 1, 1, [0, 0, 0, 255]);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
}
function testStencilAndDepth(stencil, depth)
{
debug("Testing stencil = " + stencil + ", depth = " + depth);
var creationString =
"gl = getWebGL(1, 1, { depth: " + depth + ", stencil: " + stencil + ", antialias: false }, [ 0, 0, 0, 1 ], 1, 0)";
shouldBeNonNull(creationString);
shouldBeTrue("gl.getParameter(gl.RED_BITS) >= 8");
shouldBeTrue("gl.getParameter(gl.GREEN_BITS) >= 8");
shouldBeTrue("gl.getParameter(gl.BLUE_BITS) >= 8");
shouldBeTrue("gl.getParameter(gl.ALPHA_BITS) >= 8");
if (depth)
shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) >= 16");
else
shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) == 0");
if (stencil)
shouldBeTrue("gl.getParameter(gl.STENCIL_BITS) >= 8");
else
shouldBeTrue("gl.getParameter(gl.STENCIL_BITS) == 0");
shouldBeNonNull("contextAttribs = gl.getContextAttributes()");
if (!depth && contextAttribs.depth) {
testFailed("WebGL implementation provided a depth buffer when it should not have");
}
if (!contextAttribs.depth)
depth = false;
if (!stencil && contextAttribs.stencil) {
testFailed("WebGL implementation provided a stencil buffer when it should not have");
}
if (!contextAttribs.stencil)
stencil = false;
gl.depthFunc(gl.ALWAYS);
gl.stencilFunc(gl.NEVER, 1, 1);
gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
var vertices = new Float32Array([
1.0, 1.0, 0.0,
-1.0, 1.0, 0.0,
-1.0, -1.0, 0.0,
1.0, 1.0, 0.0,
-1.0, -1.0, 0.0,
1.0, -1.0, 0.0]);
var colors = new Uint8Array([
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255]);
drawAndReadPixel(gl, vertices, colors, 0, 0);
correctColor = (stencil ? [0, 0, 0, 255] : [255, 0, 0, 255]);
wtu.checkCanvasRect(gl, 0, 0, 1, 1, correctColor)
if (fbHasStencil) {
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
drawAndReadPixel(gl, vertices, colors, 0, 0);
wtu.checkCanvasRect(gl, 0, 0, 1, 1, [0, 0, 0, 255]);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
}
function testAntialias(antialias)
{
debug("Testing antialias = " + antialias);
// Both the width and height of canvas are N.
// Note that "N = 2" doesn't work for some post processing AA per the discussion at https://github.com/KhronosGroup/WebGL/pull/1977.
var N = 3;
if (antialias)
shouldBeNonNull("gl = getWebGL(" + N + ", " + N + ", { depth: false, stencil: false, alpha: false, antialias: true }, [ 0, 0, 0, 1 ], 1, 0)");
else
shouldBeNonNull("gl = getWebGL(" + N + ", " + N + ", { depth: false, stencil: false, alpha: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
shouldBeNonNull("contextAttribs = gl.getContextAttributes()");
var vertices = new Float32Array([
1.0, 1.0, 0.0,
-1.0, 1.0, 0.0,
-1.0, -1.0, 0.0]);
var colors = new Uint8Array([
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255]);
drawAndReadPixel(gl, vertices, colors, 0, 0);
var buf = new Uint8Array(N * N * 4);
gl.readPixels(0, 0, N, N, gl.RGBA, gl.UNSIGNED_BYTE, buf);
redChannels[0] = buf[4 * (N + 1)]; // (1, 1)
redChannels[1] = buf[4 * N * (N - 1)]; // left top
redChannels[2] = buf[4 * (N - 1)]; // right bottom
shouldBeTrue("redChannels[1] == 255 && redChannels[2] == 0");
shouldBe("redChannels[0] != 255 && redChannels[0] != 0", "contextAttribs.antialias");
}
function runTest()
{
testDefault();
testAlpha(true);
testAlpha(false);
testDepth(true);
testDepth(false);
testStencilAndDepth(true, false);
testStencilAndDepth(false, false);
testStencilAndDepth(true, true);
testStencilAndDepth(false, true);
testAntialias(true);
testAntialias(false);
finishTest();
}
</script>
</head>
<body onload="init()">
<div id="description"></div>
<div id="console"></div>
</body>
</html>
|