<!--
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">
<title>WebGL BlitFramebuffer Resolve to Back Buffer</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>
<div id="description"></div>
<div id="canvasHeader"></div>
<div id="console"></div>

<script>
"use strict";

var wtu = WebGLTestUtils;
description("This test verifies the behavior of blitFramebuffer when resolving directly to the back buffer.");

debug("Regression test for <a href='http://crbug.com/699566'>http://crbug.com/699566</a>");

function runTest(testParams) {
    const sz = 64;

    if (testParams.multisampled === undefined) {
        testParams.multisampled = true;
    }

    debug('');
    debug('Testing with alpha = ' + testParams.attribs.alpha +
          ', antialias = ' + testParams.attribs.antialias +
          ', internalformat = ' + testParams.internalformat +
          ', multisampled = ' + testParams.multisampled);

    var canvas = document.createElement('canvas');
    canvas.width = sz;
    canvas.height = sz;
    document.getElementById('canvasHeader').appendChild(canvas);
    var gl = wtu.create3DContext(canvas, testParams.attribs, 2);

    // Find the supported samples for a multisampled renderbuffer of the appropriate internal format.
    let samples = [0];
    if (testParams.multisampled) {
        samples = gl.getInternalformatParameter(gl.RENDERBUFFER, gl[testParams.internalformat], gl.SAMPLES);
        if (!samples || !samples.length) {
            testFailed("At least one multisampled format is required to be supported");
            return;
        }
    }

    // Create a framebuffer with a multisampled renderbuffer.
    let rb = gl.createRenderbuffer();
    gl.bindRenderbuffer(gl.RENDERBUFFER, rb);
    gl.renderbufferStorageMultisample(gl.RENDERBUFFER, samples[0], gl[testParams.internalformat], sz, sz);

    // Create a framebuffer.
    let fb = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, rb);

    // Check for completeness.
    if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
        testFailed("Rendering to a multisampled renderbuffer of format " + testParams.internalformat + " is required by the spec");
        return;
    }

    // Clear to specified color.
    gl.clearColor.apply(gl, testParams.clearColor);
    gl.clear(gl.COLOR_BUFFER_BIT);

    // Unbind draw framebuffer. Read framebuffer is now user framebuffer;
    // draw framebuffer is default framebuffer.
    gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, null);

    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors before blit");

    // Blit from user framebuffer to default framebuffer.
    gl.blitFramebuffer(0, 0, sz, sz, 0, 0, sz, sz, gl.COLOR_BUFFER_BIT, gl.NEAREST);

    if (testParams.shouldSucceed) {
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be legal to blit/resolve to default back buffer");
    } else {
        wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "incompatible src/dest blitFramebuffer combination must fail");
    }

    // Unbind user framebuffer completely.
    gl.bindFramebuffer(gl.READ_FRAMEBUFFER, null);

    if (testParams.shouldSucceed) {
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no error before readback");
        wtu.checkCanvasRect(gl, 0, 0, 8, 8, testParams.resultColor);
        wtu.glErrorShouldBe(gl, gl.NO_ERROR);
    }
}

var tests = [
    // No-alpha, no-antialias, RGB8 source
    {
        attribs: {
            alpha: false,
            antialias: false,
        },
        internalformat: 'RGB8',
        clearColor: [ 0.0, 1.0, 0.0, 0.5 ],
        resultColor: [ 0, 255, 0, 255 ],
        shouldSucceed: true,
    },
    // No-alpha, no-antialias, RGBA8 source
    {
        attribs: {
            alpha: false,
            antialias: false,
        },
        internalformat: 'RGBA8',
        clearColor: [ 0.0, 1.0, 0.0, 0.5 ],
        resultColor: [ 0, 255, 0, 255 ],
        shouldSucceed: false,
    },
    // No-alpha, no-antialias, RGBA8 source, single-sampled blit
    {
        attribs: {
            alpha: false,
            antialias: false,
        },
        internalformat: 'RGBA8',
        clearColor: [ 0.0, 1.0, 0.0, 0.5 ],
        resultColor: [ 0, 255, 0, 255 ],
        shouldSucceed: true,
        multisampled: false,
    },
    // Alpha, no-antialias, RGB8 source
    {
        attribs: {
            alpha: true,
            antialias: false,
        },
        internalformat: 'RGB8',
        clearColor: [ 0.0, 1.0, 0.0, 1.0 ],
        resultColor: [ 0, 255, 0, 255 ],
        shouldSucceed: false,
    },
    // No-alpha, no-antialias, RGBA8 source
    // premultiplyAlpha:false just to avoid semantically incorrect
    // colors (should only affect rendering, not contents of WebGL
    // back buffer)
    {
        attribs: {
            alpha: false,
            antialias: false,
            premultiplyAlpha: false,
        },
        internalformat: 'RGBA8',
        clearColor: [ 0.0, 1.0, 0.0, 1.0 ],
        resultColor: [ 0, 255, 0, 255 ],
        shouldSucceed: false,
    },
    // Alpha, no-antialias, RGBA8 source
    // premultiplyAlpha:false just to avoid semantically incorrect
    // colors (should only affect rendering, not contents of WebGL
    // back buffer)
    {
        attribs: {
            alpha: true,
            antialias: false,
            premultiplyAlpha: false,
        },
        internalformat: 'RGBA8',
        clearColor: [ 0.0, 1.0, 0.0, 0.0 ],
        resultColor: [ 0, 255, 0, 0 ],
        shouldSucceed: true,
    },

    // All attempts to blit to an antialiased back buffer should fail.

    // No-alpha, antialias, RGB8 source
    {
        attribs: {
            alpha: false,
            antialias: true,
        },
        internalformat: 'RGB8',
        clearColor: [ 0.0, 1.0, 0.0, 1.0 ],
        resultColor: [ 0, 255, 0, 255 ],
        shouldSucceed: false,
    },
    // Alpha, antialias, RGB8 source
    {
        attribs: {
            alpha: true,
            antialias: true,
        },
        internalformat: 'RGB8',
        clearColor: [ 0.0, 1.0, 0.0, 1.0 ],
        resultColor: [ 0, 255, 0, 255 ],
        shouldSucceed: false,
    },
    // No-alpha, antialias, RGBA8 source
    // premultiplyAlpha:false just to avoid semantically incorrect
    // colors (should only affect rendering, not contents of WebGL
    // back buffer)
    {
        attribs: {
            alpha: false,
            antialias: true,
            premultiplyAlpha: false,
        },
        internalformat: 'RGBA8',
        clearColor: [ 0.0, 1.0, 0.0, 1.0 ],
        resultColor: [ 0, 255, 0, 255 ],
        shouldSucceed: false,
    },
    // Alpha, antialias, RGBA8 source
    // premultiplyAlpha:false just to avoid semantically incorrect
    // colors (should only affect rendering, not contents of WebGL
    // back buffer)
    {
        attribs: {
            alpha: true,
            antialias: true,
            premultiplyAlpha: false,
        },
        internalformat: 'RGBA8',
        clearColor: [ 0.0, 1.0, 0.0, 0.0 ],
        resultColor: [ 0, 255, 0, 0 ],
        shouldSucceed: false,
    },
];

for (var ii in tests) {
    runTest(tests[ii]);
}

var successfullyParsed = true;
</script>
<script src="../../js/js-test-post.js"></script>

</body>
</html>
