6 <script src=
"../../../resources/js-test.js"></script>
7 <script src=
"resources/webgl-test.js"></script>
8 <script id=
"vshader" type=
"x-shader/x-vertex">
10 attribute vec4 colorIn;
16 gl_Position = vec4(pos.xyz,
1.0);
20 <script id=
"fshader" type=
"x-shader/x-fragment">
21 precision mediump float;
32 var successfullyParsed
= false;
34 // These four declarations need to be global for "shouldBe" to see them
36 var contextAttribs
= null;
37 var pixel
= [0, 0, 0, 1];
38 var correctColor
= null;
46 if (window
.initNonKhronosFramework
) {
47 window
.initNonKhronosFramework(true);
51 window
.internals
.settings
.setWebGLErrorsToConsoleEnabled(false);
53 description('Verify WebGLContextAttributes are working as specified, including alpha, depth, stencil, antialias, but not premultipliedAlpha');
58 function getWebGL(canvasWidth
, canvasHeight
, contextAttribs
, clearColor
, clearDepth
, clearStencil
)
60 var canvas
= document
.createElement("canvas");
63 canvas
.width
= canvasWidth
;
64 canvas
.height
= canvasHeight
;
66 gl
= create3DContext(canvas
, contextAttribs
);
70 var program
= createProgram(gl
, "vshader", "fshader", ["pos", "colorIn"]);
74 gl
.useProgram(program
);
76 gl
.enable(gl
.DEPTH_TEST
);
77 gl
.enable(gl
.STENCIL_TEST
);
80 gl
.clearColor(clearColor
[0], clearColor
[1], clearColor
[2], clearColor
[3]);
81 gl
.clearDepth(clearDepth
);
82 gl
.clearStencil(clearStencil
);
83 gl
.clear(gl
.COLOR_BUFFER_BIT
| gl
.DEPTH_BUFFER_BIT
| gl
.STENCIL_BUFFER_BIT
);
85 framebuffer
= gl
.createFramebuffer();
86 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, framebuffer
);
87 var texture
= gl
.createTexture();
88 gl
.bindTexture(gl
.TEXTURE_2D
, texture
);
89 gl
.texImage2D(gl
.TEXTURE_2D
, 0, gl
.RGBA
, gl
.canvas
.width
, gl
.canvas
.height
, 0, gl
.RGBA
, gl
.UNSIGNED_BYTE
, null);
90 gl
.framebufferTexture2D(gl
.FRAMEBUFFER
, gl
.COLOR_ATTACHMENT0
, gl
.TEXTURE_2D
, texture
, 0);
93 fbHasColor
= gl
.checkFramebufferStatus(gl
.FRAMEBUFFER
) == gl
.FRAMEBUFFER_COMPLETE
;
95 var depthStencil
= gl
.createRenderbuffer();
96 gl
.bindRenderbuffer(gl
.RENDERBUFFER
, depthStencil
);
97 gl
.renderbufferStorage(gl
.RENDERBUFFER
, gl
.DEPTH_STENCIL
, gl
.canvas
.width
, gl
.canvas
.height
);
98 gl
.framebufferRenderbuffer(gl
.FRAMEBUFFER
, gl
.DEPTH_STENCIL_ATTACHMENT
, gl
.RENDERBUFFER
, depthStencil
);
99 fbHasDepth
= gl
.checkFramebufferStatus(gl
.FRAMEBUFFER
) == gl
.FRAMEBUFFER_COMPLETE
;
101 gl
.framebufferRenderbuffer(gl
.FRAMEBUFFER
, gl
.DEPTH_STENCIL_ATTACHMENT
, gl
.RENDERBUFFER
, null);
102 shouldBe('gl.checkFramebufferStatus(gl.FRAMEBUFFER)', 'gl.FRAMEBUFFER_COMPLETE');
107 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, null);
108 glErrorShouldBe(gl
, gl
.NO_ERROR
, "should be no errors");
113 function drawAndReadPixel(gl
, vertices
, colors
, x
, y
)
115 var colorOffset
= vertices
.byteLength
;
117 var vbo
= gl
.createBuffer();
118 gl
.bindBuffer(gl
.ARRAY_BUFFER
, vbo
);
119 gl
.bufferData(gl
.ARRAY_BUFFER
, colorOffset
+ colors
.byteLength
, gl
.STATIC_DRAW
);
120 gl
.bufferSubData(gl
.ARRAY_BUFFER
, 0, vertices
);
121 gl
.bufferSubData(gl
.ARRAY_BUFFER
, colorOffset
, colors
);
123 gl
.vertexAttribPointer(0, 3, gl
.FLOAT
, false, 0, 0);
124 gl
.enableVertexAttribArray(0);
125 gl
.vertexAttribPointer(1, 4, gl
.UNSIGNED_BYTE
, true, 0, colorOffset
);
126 gl
.enableVertexAttribArray(1);
128 gl
.drawArrays(gl
.TRIANGLES
, 0, vertices
.length
/ 3);
130 var buf
= new Uint8Array(1 * 1 * 4);
131 gl
.readPixels(x
, y
, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, buf
);
135 function testDefault()
137 debug("Testing default attributes: { stencil:false }");
138 shouldBeNonNull("gl = getWebGL(1, 1, null, [ 0, 0, 0, 0 ], 1, 0)");
139 shouldBeFalse("gl.getContextAttributes().stencil");
140 shouldBeTrue("gl.getParameter(gl.STENCIL_BITS) == 0");
143 function testAlpha(alpha
)
145 debug("Testing alpha = " + alpha
);
147 shouldBeNonNull("gl = getWebGL(1, 1, { alpha: true, depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 0 ], 1, 0)");
148 shouldBeTrue("gl.getParameter(gl.ALPHA_BITS) >= 8");
150 shouldBeNonNull("gl = getWebGL(1, 1, { alpha: false, depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 0 ], 1, 0)");
151 shouldBeTrue("gl.getParameter(gl.ALPHA_BITS) == 0");
153 shouldBeTrue("gl.getParameter(gl.RED_BITS) >= 8");
154 shouldBeTrue("gl.getParameter(gl.GREEN_BITS) >= 8");
155 shouldBeTrue("gl.getParameter(gl.BLUE_BITS) >= 8");
156 shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) == 0");
157 shouldBeTrue("gl.getParameter(gl.STENCIL_BITS) == 0");
159 shouldBeNonNull("contextAttribs = gl.getContextAttributes()");
160 shouldBeTrue("contextAttribs.alpha == " + alpha
);
162 var buf
= new Uint8Array(1 * 1 * 4);
163 gl
.readPixels(0, 0, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, buf
);
168 correctColor
= (contextAttribs
.alpha
? [0, 0, 0, 0] : [0, 0, 0, 255]);
169 shouldBe("pixel", "correctColor");
172 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, framebuffer
);
173 gl
.clearColor(0.5, 0.5, 0.5, 0.5);
174 gl
.clear(gl
.COLOR_BUFFER_BIT
);
175 gl
.readPixels(0, 0, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, buf
);
180 shouldBeTrue("Math.abs(pixel[0] - 127) <= 1 && Math.abs(pixel[1] - 127) <= 1 && Math.abs(pixel[2] - 127) <= 1 && Math.abs(pixel[3] - 127) <= 1");
181 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, null);
185 function testDepth(depth
)
187 debug("Testing depth = " + depth
);
189 shouldBeNonNull("gl = getWebGL(1, 1, { stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
190 shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) >= 16");
192 shouldBeNonNull("gl = getWebGL(1, 1, { depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
193 shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) == 0");
195 shouldBeTrue("gl.getParameter(gl.RED_BITS) >= 8");
196 shouldBeTrue("gl.getParameter(gl.GREEN_BITS) >= 8");
197 shouldBeTrue("gl.getParameter(gl.BLUE_BITS) >= 8");
198 shouldBeTrue("gl.getParameter(gl.ALPHA_BITS) >= 8");
200 shouldBeNonNull("contextAttribs = gl.getContextAttributes()");
202 gl
.depthFunc(gl
.NEVER
);
204 var vertices
= new Float32Array([
211 var colors
= new Uint8Array([
219 var buf
= drawAndReadPixel(gl
, vertices
, colors
, 0, 0);
224 correctColor
= (contextAttribs
.depth
? [0, 0, 0, 255] : [255, 0, 0, 255]);
225 shouldBe("pixel", "correctColor");
228 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, framebuffer
);
229 gl
.clear(gl
.COLOR_BUFFER_BIT
| gl
.DEPTH_BUFFER_BIT
);
230 var buf
= drawAndReadPixel(gl
, vertices
, colors
, 0, 0);
235 shouldBe("pixel", "[0, 0, 0, 255]");
236 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, null);
240 function testStencilAndDepth(stencil
, depth
)
242 debug("Testing stencil = " + stencil
+ ", depth = " + depth
);
244 "gl = getWebGL(1, 1, { depth: " + depth
+ ", stencil: " + stencil
+ ", antialias: false }, [ 0, 0, 0, 1 ], 1, 0)";
245 shouldBeNonNull(creationString
);
247 shouldBeTrue("gl.getParameter(gl.RED_BITS) >= 8");
248 shouldBeTrue("gl.getParameter(gl.GREEN_BITS) >= 8");
249 shouldBeTrue("gl.getParameter(gl.BLUE_BITS) >= 8");
250 shouldBeTrue("gl.getParameter(gl.ALPHA_BITS) >= 8");
252 shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) >= 16");
254 shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) == 0");
257 shouldBeTrue("gl.getParameter(gl.STENCIL_BITS) >= 8");
259 shouldBeTrue("gl.getParameter(gl.STENCIL_BITS) == 0");
261 shouldBeNonNull("contextAttribs = gl.getContextAttributes()");
262 if (!depth
&& contextAttribs
.depth
) {
263 testFailed("WebGL implementation provided a depth buffer when it should not have");
265 if (!contextAttribs
.depth
)
267 if (!stencil
&& contextAttribs
.stencil
) {
268 testFailed("WebGL implementation provided a stencil buffer when it should not have");
270 if (!contextAttribs
.stencil
)
273 gl
.depthFunc(gl
.ALWAYS
);
275 gl
.stencilFunc(gl
.NEVER
, 1, 1);
276 gl
.stencilOp(gl
.KEEP
, gl
.KEEP
, gl
.KEEP
);
278 var vertices
= new Float32Array([
285 var colors
= new Uint8Array([
293 var buf
= drawAndReadPixel(gl
, vertices
, colors
, 0, 0);
298 correctColor
= (stencil
? [0, 0, 0, 255] : [255, 0, 0, 255]);
299 shouldBe("pixel", "correctColor");
302 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, framebuffer
);
303 gl
.clear(gl
.COLOR_BUFFER_BIT
| gl
.DEPTH_BUFFER_BIT
);
304 var buf
= drawAndReadPixel(gl
, vertices
, colors
, 0, 0);
309 shouldBe("pixel", "[0, 0, 0, 255]");
310 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, null);
314 function testAntialias(antialias
)
316 debug("Testing antialias = " + antialias
);
318 shouldBeNonNull("gl = getWebGL(2, 2, { depth: false, stencil: false, alpha: false, antialias: true }, [ 0, 0, 0, 1 ], 1, 0)");
320 shouldBeNonNull("gl = getWebGL(2, 2, { depth: false, stencil: false, alpha: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
321 shouldBeNonNull("contextAttribs = gl.getContextAttributes()");
323 var vertices
= new Float32Array([
327 var colors
= new Uint8Array([
331 var buf
= drawAndReadPixel(gl
, vertices
, colors
, 0, 0);
333 shouldBe("pixel[0] != 255 && pixel[0] != 0", "contextAttribs.antialias");
343 testStencilAndDepth(true, false);
344 testStencilAndDepth(false, false);
345 testStencilAndDepth(true, true);
346 testStencilAndDepth(false, true);
348 testAntialias(false);
355 <body onload=
"init()">
356 <div id=
"description"></div>
357 <div id=
"console"></div>