<!--
Copyright (c) 2021 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">
<title>BlitFramebuffer Should Be Unaffected by ColorMask</title>
<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>
</head>
<body>
<canvas id="canvas" width="8" height="8"></canvas>
<div id="description"></div>
<div id="console"></div>

<script>
"use strict";

const wtu = WebGLTestUtils;
description("This test verifies that the blitFramebuffer is unaffected by the colorMask state.");

debug('Regression test for <a href="https://crbug.com/1257769">https://crbug.com/1257769</a> and <a href="https://bugs.webkit.org/show_bug.cgi?id=220129">https://bugs.webkit.org/show_bug.cgi?id=220129</a>');

function allocateTexture(gl, size) {
    const tex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, size, size, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
    return tex;
}

function allocateFBO(gl, tex) {
    const fbo = gl.createFramebuffer();
    gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fbo);
    gl.framebufferTexture2D(gl.DRAW_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
    return fbo;
}

function run() {
    const gl = wtu.create3DContext("canvas", { antialias: false }, 2);

    if (!gl) {
        testFailed("WebGL context does not exist");
        finishTest();
        return;
    }

    const size = 8;

    testPassed("WebGL context exists");

    // Allocate source and destination textures and framebuffer objects.
    const sourceTex = allocateTexture(gl, size);
    const sourceFBO = allocateFBO(gl, sourceTex);

    const destTex = allocateTexture(gl, size);
    const destFBO = allocateFBO(gl, destTex);

    const program = wtu.setupColorQuad(gl);

    gl.bindFramebuffer(gl.FRAMEBUFFER, sourceFBO);

    // Clear the source framebuffer to red.
    gl.clearColor(1, 0, 0, 1);
    gl.colorMask(true, true, true, true);
    gl.clear(gl.COLOR_BUFFER_BIT);

    // Draw a transparent green quad.
    gl.useProgram(program);
    wtu.drawFloatColorQuad(gl, [ 0, 255, 0, 0 ]);

    // Clear the alpha channel.
    gl.colorMask(false, false, false, true);
    gl.clearColor(0, 0, 0, 1);
    gl.clear(gl.COLOR_BUFFER_BIT);

    // At this point, even setting the colorMask to all-true won't
    // work around the bug, since that state is latched inside ANGLE
    // only during draws / clears.

    // Blit source to dest.
    gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, destFBO);
    gl.blitFramebuffer(0, 0, size, size, 0, 0, size, size, gl.COLOR_BUFFER_BIT, gl.NEAREST);
    gl.bindFramebuffer(gl.READ_FRAMEBUFFER, destFBO);

    // Note that the on-screen canvas is always black - we don't blit the result to it.
    wtu.checkCanvas(gl, [ 0, 255, 0, 255 ], "should be green", 1);
    finishTest();
}

var successfullyParsed = true;

requestAnimationFrame(run);

</script>

</body>
</html>
