<!--
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>Clear sRGB Color 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>
<canvas id="example" width="8" height="8"></canvas>
<div id="description"></div>
<div id="console"></div>

<script>
"use strict";

var wtu = WebGLTestUtils;
description("This test verifies the functionality of clearing srgb color buffer.");

var gl = wtu.create3DContext("example", undefined, 2);

var tex = gl.createTexture();
var fbo = gl.createFramebuffer();
var size = 8;

if (!gl) {
    testFailed("WebGL context does not exist");
} else {
    testPassed("WebGL context exists");

    // Create a srgb color buffer
    init();

    clear_srgb_color_buffer(0);
    clear_srgb_color_buffer(1);
}

function init() {
    gl.bindTexture(gl.TEXTURE_2D, tex);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.SRGB8_ALPHA8, size, size, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
    gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
    gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
    if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
        testFailed("Framebuffer incomplete.");
        return;
    } else {
        testPassed("framebuffer complete!");
    }
}

function clear_srgb_color_buffer(iter) {
    debug("");
    debug("Clear sRGB color buffer through glClear or glClearBufferfv");

    var color = [0x33, 0x88, 0xbb, 0xff];
    gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);

    if (iter == 0) {
        gl.clearColor(color[0] / 255, color[1] / 255, color[2] / 255, color[3] / 255);
        gl.clear(gl.COLOR_BUFFER_BIT);
    } else {
        var data = new Float32Array([color[0] / 255, color[1] / 255, color[2] / 255, color[3] / 255]);
        gl.clearBufferfv(gl.COLOR, 0, data);
    }

    var color_ref = wtu.linearToSRGB(color);
    var tolerance = 3;
    var msg = "";
    wtu.checkCanvasRect(gl, 0, 0, size, size, color_ref, msg, tolerance);
}

gl.bindTexture(gl.TEXTURE_2D, null);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.deleteTexture(tex);
gl.deleteFramebuffer(fbo);

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

</body>
</html>
