<!--

/*
** 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.
    var N = 2;
    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>
