3 <script src="../../js/resources/js-test-pre.js"></script>
4 <script src="resources/webgl-test.js"></script>
5 <script id="vshader" type="x-shader/x-vertex">
7 attribute vec4 colorIn;
13 gl_Position = vec4(pos.xyz, 1.0);
17 <script id="fshader" type="x-shader/x-fragment">
19 precision highp float;
31 // These four declarations need to be global for "shouldBe" to see them
33 var contextAttribs = null;
34 var pixel = [0, 0, 0, 1];
35 var correctColor = null;
39 if (window.initNonKhronosFramework) {
40 window.initNonKhronosFramework(true);
43 description('Verify WebGLContextAttributes are working as specified, including alpha, depth, stencil, antialias, but not premultipliedAlpha');
48 function getWebGL(canvasName, contextAttribs, clearColor, clearDepth, clearStencil)
50 var canvas = document.getElementById(canvasName);
51 var gl = canvas.getContext("experimental-webgl", contextAttribs);
53 alert("No WebGL context found");
58 gl.console = ("console" in window) ? window.console : { log: function() { } };
61 var vertexShader = loadShader(gl, "vshader");
62 var fragmentShader = loadShader(gl, "fshader");
64 if (!vertexShader || !fragmentShader)
67 // Create the program object
68 gl.program = gl.createProgram();
73 // Attach our two shaders to the program
74 gl.attachShader(gl.program, vertexShader);
75 gl.attachShader(gl.program, fragmentShader);
78 var attribs = [ "pos", "colorIn" ];
79 for (var i in attribs)
80 gl.bindAttribLocation(gl.program, parseInt(i), attribs[i]);
83 gl.linkProgram(gl.program);
85 // Check the link status
86 var linked = gl.getProgramParameter(gl.program, gl.LINK_STATUS);
88 // something went wrong with the link
89 var error = gl.getProgramInfoLog (gl.program);
90 gl.console.log("Error in program linking:"+error);
92 gl.deleteProgram(gl.program);
93 gl.deleteProgram(fragmentShader);
94 gl.deleteProgram(vertexShader);
99 gl.useProgram(gl.program);
101 gl.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
102 gl.clearDepth(clearDepth);
103 gl.clearStencil(clearStencil);
104 gl.enable(gl.DEPTH_TEST);
105 gl.enable(gl.STENCIL_TEST);
106 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
111 function drawAndReadPixel(gl, vertices, colors, x, y)
113 var colorOffset = vertices.byteLength;
115 var vbo = gl.createBuffer();
116 gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
117 gl.bufferData(gl.ARRAY_BUFFER, colorOffset + colors.byteLength, gl.STATIC_DRAW);
118 gl.bufferSubData(gl.ARRAY_BUFFER, 0, vertices);
119 gl.bufferSubData(gl.ARRAY_BUFFER, colorOffset, colors);
121 gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
122 gl.enableVertexAttribArray(0);
123 gl.vertexAttribPointer(1, 4, gl.UNSIGNED_BYTE, true, 0, colorOffset);
124 gl.enableVertexAttribArray(1);
126 gl.drawArrays(gl.TRIANGLES, 0, vertices.length / 3);
128 var buf = new Uint8Array(1 * 1 * 4);
129 gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf);
133 function testAlpha(alpha)
135 debug("Testing alpha = " + alpha);
137 shouldBeNonNull("webGL = getWebGL('alphaOn', { alpha: true, depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 0 ], 1, 0)");
139 shouldBeNonNull("webGL = getWebGL('alphaOff', { alpha: false, depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 0 ], 1, 0)");
140 shouldBeNonNull("contextAttribs = webGL.getContextAttributes()");
141 shouldBeDefined("contextAttribs.alpha");
142 shouldBeDefined("contextAttribs.depth");
143 shouldBeDefined("contextAttribs.stencil");
144 shouldBeDefined("contextAttribs.antialias");
145 shouldBeDefined("contextAttribs.premultipliedAlpha");
147 var buf = new Uint8Array(1 * 1 * 4);
148 webGL.readPixels(0, 0, 1, 1, webGL.RGBA, webGL.UNSIGNED_BYTE, buf);
153 correctColor = (contextAttribs.alpha ? [0, 0, 0, 0] : [0, 0, 0, 255]);
154 shouldBe("pixel", "correctColor");
157 function testDepth(depth)
159 debug("Testing depth = " + depth);
161 shouldBeNonNull("webGL = getWebGL('depthOn', { stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
163 shouldBeNonNull("webGL = getWebGL('depthOff', { depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
164 shouldBeNonNull("contextAttribs = webGL.getContextAttributes()");
165 shouldBeDefined("contextAttribs.depth");
166 shouldBeDefined("contextAttribs.alpha");
167 shouldBeDefined("contextAttribs.stencil");
168 shouldBeDefined("contextAttribs.antialias");
169 shouldBeDefined("contextAttribs.premultipliedAlpha");
171 webGL.depthFunc(webGL.NEVER);
173 var vertices = new Float32Array([
180 var colors = new Uint8Array([
188 var buf = drawAndReadPixel(webGL, vertices, colors, 0, 0);
193 correctColor = (contextAttribs.depth ? [0, 0, 0, 255] : [255, 0, 0, 255]);
194 shouldBe("pixel", "correctColor");
197 function testStencil(stencil)
199 debug("Testing stencil = " + stencil);
201 shouldBeNonNull("webGL = getWebGL('stencilOn', { depth: false, stencil: true, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
203 shouldBeNonNull("webGL = getWebGL('stencilOff', { depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
204 shouldBeNonNull("contextAttribs = webGL.getContextAttributes()");
205 shouldBeDefined("contextAttribs.depth");
206 shouldBeDefined("contextAttribs.alpha");
207 shouldBeDefined("contextAttribs.stencil");
208 shouldBeDefined("contextAttribs.antialias");
209 shouldBeDefined("contextAttribs.premultipliedAlpha");
211 webGL.depthFunc(webGL.ALWAYS);
213 webGL.stencilFunc(webGL.NEVER, 1, 1);
214 webGL.stencilOp(webGL.KEEP, webGL.KEEP, webGL.KEEP);
216 var vertices = new Float32Array([
223 var colors = new Uint8Array([
231 var buf = drawAndReadPixel(webGL, vertices, colors, 0, 0);
236 correctColor = (contextAttribs.stencil ? [0, 0, 0, 255] : [255, 0, 0, 255]);
237 shouldBe("pixel", "correctColor");
240 function testAntialias(antialias)
242 debug("Testing antialias = " + antialias);
244 shouldBeNonNull("webGL = getWebGL('antialiasOn', { depth: false, stencil: false, alpha: false, antialias: true }, [ 0, 0, 0, 1 ], 1, 0)");
246 shouldBeNonNull("webGL = getWebGL('antialiasOff', { depth: false, stencil: false, alpha: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
247 shouldBeNonNull("contextAttribs = webGL.getContextAttributes()");
248 shouldBeDefined("contextAttribs.depth");
249 shouldBeDefined("contextAttribs.alpha");
250 shouldBeDefined("contextAttribs.stencil");
251 shouldBeDefined("contextAttribs.antialias");
252 shouldBeDefined("contextAttribs.premultipliedAlpha");
254 var vertices = new Float32Array([
258 var colors = new Uint8Array([
262 var buf = drawAndReadPixel(webGL, vertices, colors, 0, 0);
264 shouldBe("pixel[0] != 255 && pixel[0] != 0", "contextAttribs.antialias");
277 testAntialias(false);
278 var epilogue = document.createElement("script");
279 epilogue.onload = finish;
280 epilogue.src = "../../js/resources/js-test-post.js";
281 document.body.appendChild(epilogue);
285 if (window.nonKhronosFrameworkNotifyDone) {
286 window.nonKhronosFrameworkNotifyDone();
291 <body onload="init()">
292 <canvas id="alphaOn" width="1px" height="1px"></canvas>
293 <canvas id="alphaOff" width="1px" height="1px"></canvas>
294 <canvas id="depthOn" width="1px" height="1px"></canvas>
295 <canvas id="depthOff" width="1px" height="1px"></canvas>
296 <canvas id="stencilOn" width="1px" height="1px"></canvas>
297 <canvas id="stencilOff" width="1px" height="1px"></canvas>
298 <canvas id="antialiasOn" width="2px" height="2px"></canvas>
299 <canvas id="antialiasOff" width="2px" height="2px"></canvas>
300 <div id="description"></div>
301 <div id="console"></div>