gecko/content/canvas/test/webgl/conformance/context-attributes-alpha-depth-stencil-antialias.html

317 lines
10 KiB
HTML

<!--
Copyright (c) 2010 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.
-->
<html>
<head>
<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 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">
varying vec4 color;
void main()
{
gl_FragColor = color;
}
</script>
<script>
var successfullyParsed = false;
// These four declarations need to be global for "shouldBe" to see them
var webGL = null;
var contextAttribs = null;
var pixel = [0, 0, 0, 1];
var correctColor = null;
function init()
{
if (window.initNonKhronosFramework) {
window.initNonKhronosFramework(true);
}
description('Verify WebGLContextAttributes are working as specified, including alpha, depth, stencil, antialias, but not premultipliedAlpha');
runTest();
}
function getWebGL(canvasName, contextAttribs, clearColor, clearDepth, clearStencil)
{
var canvas = document.getElementById(canvasName);
var gl = canvas.getContext("experimental-webgl", contextAttribs);
if (!gl) {
alert("No WebGL context found");
return null;
}
var actualContextAttribs = gl.getContextAttributes();
// Add a console
gl.console = ("console" in window) ? window.console : { log: function() { } };
// create our shaders
var vertexShader = loadShader(gl, "vshader");
var fragmentShader = loadShader(gl, "fshader");
if (!vertexShader || !fragmentShader)
return null;
// Create the program object
gl.program = gl.createProgram();
if (!gl.program)
return null;
// Attach our two shaders to the program
gl.attachShader(gl.program, vertexShader);
gl.attachShader(gl.program, fragmentShader);
// Bind attributes
var attribs = [ "pos", "colorIn" ];
for (var i in attribs)
gl.bindAttribLocation(gl.program, i, attribs[i]);
// Link the program
gl.linkProgram(gl.program);
// Check the link status
var linked = gl.getProgramParameter(gl.program, gl.LINK_STATUS);
if (!linked) {
// something went wrong with the link
var error = gl.getProgramInfoLog (gl.program);
gl.console.log("Error in program linking:"+error);
gl.deleteProgram(gl.program);
gl.deleteProgram(fragmentShader);
gl.deleteProgram(vertexShader);
return null;
}
gl.useProgram(gl.program);
gl.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
gl.clearDepth(clearDepth);
gl.clearStencil(clearStencil);
gl.enable(gl.DEPTH_TEST);
gl.enable(gl.STENCIL_TEST);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
return gl;
}
function drawAndReadPixel(gl, vertices, colors, x, y)
{
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);
var buf = new Uint8Array(1 * 1 * 4);
gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf);
return buf;
}
function testAlpha(alpha)
{
debug("Testing alpha = " + alpha);
if (alpha)
shouldBeNonNull("webGL = getWebGL('alphaOn', { alpha: true, depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 0 ], 1, 0)");
else
shouldBeNonNull("webGL = getWebGL('alphaOff', { alpha: false, depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 0 ], 1, 0)");
shouldBeNonNull("contextAttribs = webGL.getContextAttributes()");
shouldBe("contextAttribs.alpha", (alpha ? "true" : "false"));
shouldBe("contextAttribs.depth", "false");
shouldBe("contextAttribs.stencil", "false");
shouldBe("contextAttribs.antialias", "false");
shouldBe("contextAttribs.premultipliedAlpha", "true");
var buf = new Uint8Array(1 * 1 * 4);
webGL.readPixels(0, 0, 1, 1, webGL.RGBA, webGL.UNSIGNED_BYTE, buf);
pixel[0] = buf[0];
pixel[1] = buf[1];
pixel[2] = buf[2];
pixel[3] = buf[3];
correctColor = (alpha ? [0, 0, 0, 0] : [0, 0, 0, 255]);
shouldBe("pixel", "correctColor");
}
function testDepth(depth)
{
debug("Testing depth = " + depth);
if (depth)
shouldBeNonNull("webGL = getWebGL('depthOn', { stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
else
shouldBeNonNull("webGL = getWebGL('depthOff', { depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
shouldBeNonNull("contextAttribs = webGL.getContextAttributes()");
shouldBe("contextAttribs.depth", (depth ? "true" : "false"));
shouldBe("contextAttribs.alpha", "true");
shouldBe("contextAttribs.stencil", "false");
shouldBe("contextAttribs.antialias", "false");
shouldBe("contextAttribs.premultipliedAlpha", "true");
webGL.depthFunc(webGL.NEVER);
var vertices = new WebGLFloatArray([
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 WebGLUnsignedByteArray([
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255]);
var buf = drawAndReadPixel(webGL, vertices, colors, 0, 0);
pixel[0] = buf[0];
pixel[1] = buf[1];
pixel[2] = buf[2];
pixel[3] = buf[3];
correctColor = (depth ? [0, 0, 0, 255] : [255, 0, 0, 255]);
shouldBe("pixel", "correctColor");
}
function testStencil(stencil)
{
debug("Testing stencil = " + stencil);
if (stencil)
shouldBeNonNull("webGL = getWebGL('stencilOn', { depth: false, stencil: true, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
else
shouldBeNonNull("webGL = getWebGL('stencilOff', { depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
shouldBeNonNull("contextAttribs = webGL.getContextAttributes()");
// If EXT_packed_depth_stencil is supported, both depth & stencil will be true; otherwise, both will be false.
shouldBe("contextAttribs.depth == contextAttribs.stencil", "true");
shouldBe("contextAttribs.alpha", "true");
shouldBe("contextAttribs.antialias", "false");
shouldBe("contextAttribs.premultipliedAlpha", "true");
webGL.depthFunc(webGL.ALWAYS);
webGL.stencilFunc(webGL.NEVER, 1, 1);
webGL.stencilOp(webGL.KEEP, webGL.KEEP, webGL.KEEP);
var vertices = new WebGLFloatArray([
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 WebGLUnsignedByteArray([
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255]);
var buf = drawAndReadPixel(webGL, vertices, colors, 0, 0);
pixel[0] = buf[0];
pixel[1] = buf[1];
pixel[2] = buf[2];
pixel[3] = buf[3];
correctColor = (stencil ? [0, 0, 0, 255] : [255, 0, 0, 255]);
// If stencil is requested but not supported, we fake the effect.
if (stencil && !contextAttribs.stencil)
pixel[0] = 0;
shouldBe("pixel", "correctColor");
}
function testAntialias(antialias)
{
debug("Testing antialias = " + antialias);
if (antialias)
shouldBeNonNull("webGL = getWebGL('antialiasOn', { depth: false, stencil: false, alpha: false, antialias: true }, [ 0, 0, 0, 1 ], 1, 0)");
else
shouldBeNonNull("webGL = getWebGL('antialiasOff', { depth: false, stencil: false, alpha: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
shouldBeNonNull("contextAttribs = webGL.getContextAttributes()");
shouldBe("contextAttribs.depth", "false");
shouldBe("contextAttribs.stencil", "false");
shouldBe("contextAttribs.alpha", "false");
shouldBe("contextAttribs.antialias == true || contextAttribs.antialias == false", "true");
shouldBe("contextAttribs.premultipliedAlpha", "true");
var vertices = new WebGLFloatArray([
1.0, 1.0, 0.0,
-1.0, 1.0, 0.0,
-1.0, -1.0, 0.0]);
var colors = new WebGLUnsignedByteArray([
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255]);
var buf = drawAndReadPixel(webGL, vertices, colors, 0, 0);
pixel[0] = buf[0];
// If antialias is requested but not supported, we fake the effect.
if (antialias && !contextAttribs.antialias)
pixel[0] = 127;
shouldBe("pixel[0] == 255 || pixel[0] == 0", (antialias ? "false" : "true"));
}
function runTest()
{
testAlpha(true);
testAlpha(false);
testDepth(true);
testDepth(false);
testStencil(true);
testStencil(false);
testAntialias(true);
testAntialias(false);
successfullyParsed = true;
var epilogue = document.createElement("script");
epilogue.onload = finish;
epilogue.src = "../resources/js-test-post.js";
document.body.appendChild(epilogue);
}
function finish() {
if (window.nonKhronosFrameworkNotifyDone) {
window.nonKhronosFrameworkNotifyDone();
}
}
</script>
</head>
<body onload="init()">
<canvas id="alphaOn" width="1px" height="1px"></canvas>
<canvas id="alphaOff" width="1px" height="1px"></canvas>
<canvas id="depthOn" width="1px" height="1px"></canvas>
<canvas id="depthOff" width="1px" height="1px"></canvas>
<canvas id="stencilOn" width="1px" height="1px"></canvas>
<canvas id="stencilOff" width="1px" height="1px"></canvas>
<canvas id="antialiasOn" width="2px" height="2px"></canvas>
<canvas id="antialiasOff" width="2px" height="2px"></canvas>
<div id="description"></div>
<div id="console"></div>
</body>
</html>