<!--
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>
</head>
<body>
<canvas id="canvas" width="32" height="32" style="width: 32px; height: 32px;"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
var wtu = WebGLTestUtils;
description(' Test multisample with sample number from 1 to max sample number which also includes the samples that may not be in the supported sample list');

var gl = wtu.create3DContext("canvas", null, 2);
var size = 32;
var program;

if (!gl) {
    testFailed('canvas.getContext() failed');
} else {
    program = wtu.setupColorQuad(gl);
    gl.viewport(0, 0, size, size);
    var supportedSampleCountArray = gl.getInternalformatParameter(gl.RENDERBUFFER, gl.RGBA8, gl.SAMPLES);
    var iterationCount = supportedSampleCountArray[0] + 1;
    for (var i = 1; i < iterationCount; i++)
    {
        runTest(gl, i, false);
        runTest(gl, i, true);
    }
}

function runTest(gl, sampleCount, isInverted) {
    // Setup multi-sample RBO
    var msColorRbo = gl.createRenderbuffer();
    gl.bindRenderbuffer(gl.RENDERBUFFER, msColorRbo);
    gl.renderbufferStorageMultisample(gl.RENDERBUFFER, sampleCount, gl.RGBA8, size, size);

    // Setup multi-sample FBO.
    var msFbo = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, msFbo);
    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, msColorRbo);

    // Setup resolve color RBO.
    var resolveColorRbo = gl.createRenderbuffer();
    gl.bindRenderbuffer(gl.RENDERBUFFER, resolveColorRbo);
    gl.renderbufferStorage(gl.RENDERBUFFER, gl.RGBA8, size, size);
    // Setup resolve FBO
    var resolveFbo = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, resolveFbo);
    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, resolveColorRbo);

    gl.bindFramebuffer(gl.FRAMEBUFFER, msFbo);
    if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
        testFailed("Framebuffer incomplete.");
        return;
    }
    gl.clearColor(0.0, 0.0, 0.0, 1.0);
    gl.clear(gl.COLOR_BUFFER_BIT);

    gl.enable(gl.SAMPLE_COVERAGE);
    var coverageValue = isInverted ? 0.0 : 1.0;
    gl.sampleCoverage(coverageValue, isInverted);

    var quadColor = [1.0, 0.0, 0.0, 1.0];
    gl.useProgram(program);
    wtu.drawFloatColorQuad(gl, quadColor);

    gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, resolveFbo);
    gl.blitFramebuffer(0, 0, size, size, 0, 0, size, size, gl.COLOR_BUFFER_BIT, gl.NEAREST);

    gl.bindFramebuffer(gl.FRAMEBUFFER, resolveFbo);
    wtu.checkCanvasRect(gl, 0, 0, size, size, [255, 0, 0, 255],
                        "User buffer has been rendered to red with sample = "
                        + sampleCount + ", coverageValue = " + coverageValue
                        + " and isInverted = " + isInverted, 3);

    gl.disable(gl.SAMPLE_COVERAGE);
    gl.deleteRenderbuffer(msColorRbo);
    gl.deleteRenderbuffer(resolveColorRbo);
    gl.deleteFramebuffer(msFbo);
    gl.deleteFramebuffer(resolveFbo);
}

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