<!--

/*
** Copyright (c) 2016 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/

-->

<!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>
<script src="../../js/glsl-conformance-test.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>
