<!--

/*
** 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>Verify uint(bool), ivec(bvec), and uvec(bvec) work correctly (Mac AMD driver bug)</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="2" height="2"> </canvas>
<div id="description"></div>
<div id="console"></div>
<script id="vshader-uint-1" type="x-shader/x-vertex">#version 300 es
in vec3 aPosition;
flat out uint uvalue;
uniform bool bvalue;

void main() {
    gl_Position = vec4(aPosition, 1);
    uvalue = uint(bvalue);
}
</script>
<script id="fshader-uint-1" type="x-shader/x-fragment">#version 300 es
flat in uint uvalue;
out highp vec4 myFragColor;

void main() {
    if (uvalue == 1u)
        myFragColor = vec4(1.0, 0.0, 0.0, 1.0);
    else if (uvalue == 0u)
        myFragColor = vec4(0.0, 1.0, 0.0, 1.0);
    else
        myFragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
</script>

<script id="vshader-simple" type="x-shader/x-vertex">#version 300 es
in vec3 aPosition;

void main() {
    gl_Position = vec4(aPosition, 1);
}
</script>
<script id="fshader-uint-2" type="x-shader/x-fragment">#version 300 es
uniform bool bvalue;
out highp vec4 myFragColor;

void main() {
    uint uvalue = uint(bvalue);

    if (uvalue == 1u)
        myFragColor = vec4(1.0, 0.0, 0.0, 1.0);
    else if (uvalue == 0u)
        myFragColor = vec4(0.0, 1.0, 0.0, 1.0);
    else
        myFragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
</script>

<script id="vshader-ivec2-1" type="x-shader/x-vertex">#version 300 es
in vec3 aPosition;
flat out ivec2 ivalue;
uniform bool bvalue;

void main() {
    gl_Position = vec4(aPosition, 1);
    ivalue = ivec2(bvec2(bvalue, bvalue));
}
</script>
<script id="fshader-ivec2-1" type="x-shader/x-fragment">#version 300 es
flat in ivec2 ivalue;
out highp vec4 myFragColor;

void main() {
    if (ivalue == ivec2(1, 1))
        myFragColor = vec4(1.0, 0.0, 0.0, 1.0);
    else if (ivalue == ivec2(0, 0))
        myFragColor = vec4(0.0, 1.0, 0.0, 1.0);
    else
        myFragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
</script>

<script id="fshader-ivec2-2" type="x-shader/x-fragment">#version 300 es
uniform bool bvalue;
out highp vec4 myFragColor;

void main() {
    ivec2 ivalue = ivec2(bvec2(bvalue, bvalue));

    if (ivalue == ivec2(1, 1))
        myFragColor = vec4(1.0, 0.0, 0.0, 1.0);
    else if (ivalue == ivec2(0, 0))
        myFragColor = vec4(0.0, 1.0, 0.0, 1.0);
    else
        myFragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
</script>

<script id="vshader-uvec2-1" type="x-shader/x-vertex">#version 300 es
in vec3 aPosition;
flat out uvec2 uvalue;
uniform bool bvalue;

void main() {
    gl_Position = vec4(aPosition, 1);
    uvalue = uvec2(bvec2(bvalue, bvalue));
}
</script>
<script id="fshader-uvec2-1" type="x-shader/x-fragment">#version 300 es
flat in uvec2 uvalue;
out highp vec4 myFragColor;

void main() {
    if (uvalue == uvec2(1u, 1u))
        myFragColor = vec4(1.0, 0.0, 0.0, 1.0);
    else if (uvalue == uvec2(0u, 0u))
        myFragColor = vec4(0.0, 1.0, 0.0, 1.0);
    else
        myFragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
</script>

<script id="fshader-uvec2-2" type="x-shader/x-fragment">#version 300 es
uniform bool bvalue;
out highp vec4 myFragColor;

void main() {
    uvec2 uvalue = uvec2(bvec2(bvalue, bvalue));

    if (uvalue == uvec2(1u, 1u))
        myFragColor = vec4(1.0, 0.0, 0.0, 1.0);
    else if (uvalue == uvec2(0u, 0u))
        myFragColor = vec4(0.0, 1.0, 0.0, 1.0);
    else
        myFragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
</script>

<script id="vshader-ivec3-1" type="x-shader/x-vertex">#version 300 es
in vec3 aPosition;
flat out ivec3 ivalue;
uniform bool bvalue;

void main() {
    gl_Position = vec4(aPosition, 1);
    ivalue = ivec3(bvec3(bvalue, bvalue, bvalue));
}
</script>
<script id="fshader-ivec3-1" type="x-shader/x-fragment">#version 300 es
flat in ivec3 ivalue;
out highp vec4 myFragColor;

void main() {
    if (ivalue == ivec3(1, 1, 1))
        myFragColor = vec4(1.0, 0.0, 0.0, 1.0);
    else if (ivalue == ivec3(0, 0, 0))
        myFragColor = vec4(0.0, 1.0, 0.0, 1.0);
    else
        myFragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
</script>

<script id="fshader-ivec3-2" type="x-shader/x-fragment">#version 300 es
uniform bool bvalue;
out highp vec4 myFragColor;

void main() {
    ivec3 ivalue = ivec3(bvec3(bvalue, bvalue, bvalue));

    if (ivalue == ivec3(1, 1, 1))
        myFragColor = vec4(1.0, 0.0, 0.0, 1.0);
    else if (ivalue == ivec3(0, 0, 0))
        myFragColor = vec4(0.0, 1.0, 0.0, 1.0);
    else
        myFragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
</script>

<script id="vshader-uvec3-1" type="x-shader/x-vertex">#version 300 es
in vec3 aPosition;
flat out uvec3 uvalue;
uniform bool bvalue;

void main() {
    gl_Position = vec4(aPosition, 1);
    uvalue = uvec3(bvec3(bvalue, bvalue, bvalue));
}
</script>
<script id="fshader-uvec3-1" type="x-shader/x-fragment">#version 300 es
flat in uvec3 uvalue;
out highp vec4 myFragColor;

void main() {
    if (uvalue == uvec3(1u, 1u, 1u))
        myFragColor = vec4(1.0, 0.0, 0.0, 1.0);
    else if (uvalue == uvec3(0u, 0u, 0u))
        myFragColor = vec4(0.0, 1.0, 0.0, 1.0);
    else
        myFragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
</script>

<script id="fshader-uvec3-2" type="x-shader/x-fragment">#version 300 es
uniform bool bvalue;
out highp vec4 myFragColor;

void main() {
    uvec3 uvalue = uvec3(bvec3(bvalue, bvalue, bvalue));

    if (uvalue == uvec3(1u, 1u, 1u))
        myFragColor = vec4(1.0, 0.0, 0.0, 1.0);
    else if (uvalue == uvec3(0u, 0u, 0u))
        myFragColor = vec4(0.0, 1.0, 0.0, 1.0);
    else
        myFragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
</script>

<script id="vshader-ivec4-1" type="x-shader/x-vertex">#version 300 es
in vec3 aPosition;
flat out ivec4 ivalue;
uniform bool bvalue;

void main() {
    gl_Position = vec4(aPosition, 1);
    ivalue = ivec4(bvec4(bvalue, bvalue, bvalue, bvalue));
}
</script>
<script id="fshader-ivec4-1" type="x-shader/x-fragment">#version 300 es
flat in ivec4 ivalue;
out highp vec4 myFragColor;

void main() {
    if (ivalue == ivec4(1, 1, 1, 1))
        myFragColor = vec4(1.0, 0.0, 0.0, 1.0);
    else if (ivalue == ivec4(0, 0, 0, 0))
        myFragColor = vec4(0.0, 1.0, 0.0, 1.0);
    else
        myFragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
</script>

<script id="fshader-ivec4-2" type="x-shader/x-fragment">#version 300 es
uniform bool bvalue;
out highp vec4 myFragColor;

void main() {
    ivec4 ivalue = ivec4(bvec4(bvalue, bvalue, bvalue, bvalue));

    if (ivalue == ivec4(1, 1, 1, 1))
        myFragColor = vec4(1.0, 0.0, 0.0, 1.0);
    else if (ivalue == ivec4(0, 0, 0, 0))
        myFragColor = vec4(0.0, 1.0, 0.0, 1.0);
    else
        myFragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
</script>

<script id="vshader-uvec4-1" type="x-shader/x-vertex">#version 300 es
in vec3 aPosition;
flat out uvec4 uvalue;
uniform bool bvalue;

void main() {
    gl_Position = vec4(aPosition, 1);
    uvalue = uvec4(bvec4(bvalue, bvalue, bvalue, bvalue));
}
</script>
<script id="fshader-uvec4-1" type="x-shader/x-fragment">#version 300 es
flat in uvec4 uvalue;
out highp vec4 myFragColor;

void main() {
    if (uvalue == uvec4(1u, 1u, 1u, 1u))
        myFragColor = vec4(1.0, 0.0, 0.0, 1.0);
    else if (uvalue == uvec4(0u, 0u, 0u, 0u))
        myFragColor = vec4(0.0, 1.0, 0.0, 1.0);
    else
        myFragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
</script>

<script id="fshader-uvec4-2" type="x-shader/x-fragment">#version 300 es
uniform bool bvalue;
out highp vec4 myFragColor;

void main() {
    uvec4 uvalue = uvec4(bvec4(bvalue, bvalue, bvalue, bvalue));

    if (uvalue == uvec4(1u, 1u, 1u, 1u))
        myFragColor = vec4(1.0, 0.0, 0.0, 1.0);
    else if (uvalue == uvec4(0u, 0u, 0u, 0u))
        myFragColor = vec4(0.0, 1.0, 0.0, 1.0);
    else
        myFragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
</script>

<script type="application/javascript">
"use strict";
description("Verify uint(bool), ivec(bvec), and uvec(bvec) work correctly");
debug("");
var wtu = WebGLTestUtils;
function test() {
    var gl = wtu.create3DContext("canvas", undefined, 2);
    if (!gl) {
        testFailed("context does not exist");
        return;
    }
    wtu.setupUnitQuad(gl);

    var testCases = [
        { vshader: "vshader-uint-1", fshader: "fshader-uint-1", desc: "vertex shader uint" },
        { vshader: "vshader-simple", fshader: "fshader-uint-2", desc: "fragment shader uint" },
        { vshader: "vshader-ivec2-1", fshader: "fshader-ivec2-1", desc: "vertex shader ivec2" },
        { vshader: "vshader-simple", fshader: "fshader-ivec2-2", desc: "fragment shader ivec2" },
        { vshader: "vshader-uvec2-1", fshader: "fshader-uvec2-1", desc: "vertex shader uvec2" },
        { vshader: "vshader-simple", fshader: "fshader-uvec2-2", desc: "fragment shader uvec2" },
        { vshader: "vshader-ivec3-1", fshader: "fshader-ivec3-1", desc: "vertex shader ivec3" },
        { vshader: "vshader-simple", fshader: "fshader-ivec3-2", desc: "fragment shader ivec3" },
        { vshader: "vshader-uvec3-1", fshader: "fshader-uvec3-1", desc: "vertex shader uvec3" },
        { vshader: "vshader-simple", fshader: "fshader-uvec3-2", desc: "fragment shader uvec3" },
        { vshader: "vshader-ivec4-1", fshader: "fshader-ivec4-1", desc: "vertex shader ivec4" },
        { vshader: "vshader-simple", fshader: "fshader-ivec4-2", desc: "fragment shader ivec4" },
        { vshader: "vshader-uvec4-1", fshader: "fshader-uvec4-1", desc: "vertex shader uvec4" },
        { vshader: "vshader-simple", fshader: "fshader-uvec4-2", desc: "fragment shader uvec4" },
    ];

    for (var idx = 0; idx < testCases.length; ++idx) {
        var test = testCases[idx];

        debug("");
        var program = wtu.setupProgram(gl, [test.vshader, test.fshader], ["aPosition"]);
        if (!program) {
            testFailed("Fail to set up program");
        } else {
            var uniformLoc = gl.getUniformLocation(program, 'bvalue');
            debug("Testing " + test.desc + " with false");
            gl.uniform1i(uniformLoc, 0);
            wtu.drawUnitQuad(gl);
            wtu.checkCanvas(gl, [0, 255, 0, 255]);
            debug("Testing " + test.desc + " with true");
            gl.uniform1i(uniformLoc, 1);
            wtu.drawUnitQuad(gl);
            wtu.checkCanvas(gl, [255, 0, 0, 255]);
            gl.deleteProgram(program);
            wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from testing");
        }
    }
};

test();

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

