<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!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>
"use strict";

var wtu = WebGLTestUtils;

// This declaration needs to be global for "shouldBe" to see it
var gl;

function init()
{
    description('Verify that a WebGL context with alpha:false still works correctly after handling textures with an alpha channel.');

    runTest();
}

function getWebGL(contextAttribs)
{
    return wtu.create3DContext("c", contextAttribs);
}

function runTest()
{
    var buf = new Uint8Array(1 * 1 * 4);
    shouldBeNonNull("gl = getWebGL({ alpha: false, antialias: false })");

    // Clear to black. Alpha channel of clearColor() is ignored.
    gl.clearColor(0.0, 0.0, 0.0, 0.7);
    gl.clear(gl.COLOR_BUFFER_BIT);
    wtu.checkCanvasRect(gl, 0, 0, 1, 1, [0, 0, 0, 255],
                        "Alpha channel of clearColor should be ignored");

    wtu.waitForComposite(function() {
        // Make a new framebuffer and attach a texture with an alpha channel.
        var fbo = gl.createFramebuffer();
        gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
        var texture = gl.createTexture();
        gl.bindTexture(gl.TEXTURE_2D, texture);
        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
        gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);

        // Clear texture. Note that alpha channel is not 1.0.
        gl.clearColor(1.0, 0.0, 0.0, 0.5);
        gl.clear(gl.COLOR_BUFFER_BIT);
        wtu.checkCanvasRect(gl, 0, 0, 1, 1, [255, 0, 0, 128],
                            "Alpha channel of clearColor should be obeyed for FBO with alpha channel",
                            1);

        // Bind back buffer and check that its alpha channel is still 1.0.
        gl.bindFramebuffer(gl.FRAMEBUFFER, null);
        wtu.checkCanvasRect(gl, 0, 0, 1, 1, [0, 0, 0, 255],
                            "Alpha channel of back buffer should still be 255");
        finishTest();
    });
}

</script>
</head>
<body onload="init()">
<div id="description"></div>
<div id="console"></div>
<canvas width="20" height="20" style="border: 1px solid blue;" id="c"></canvas>
</body>
</html>
