<!--
/*
** 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>Point-specific shader variables test</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="c" width="64" height="64"></canvas>
<div id="description"></div>
<div id="console"></div>

<script id="vs-assign" type="x-shader/x-vertex">
attribute vec2 aPosition;

varying vec2 vPos;

void main()
{
    gl_Position = vec4(aPosition, 0, 1);
    vPos = aPosition;

    gl_PointSize = 1.0;
}
</script>

<script id="vs-conditional" type="x-shader/x-vertex">
uniform float renderingPoints; // not assigned, equal to 0.0
attribute vec2 aPosition;

varying vec2 vPos;

void main()
{
    gl_Position = vec4(aPosition, 0, 1);
    vPos = aPosition;

    if (renderingPoints > 0.0) {
        gl_PointSize = 1.0;
    }
}
</script>

<script id="fs-overwrite" type="x-shader/x-fragment">
varying mediump vec2 vPos;

void main()
{
    gl_FragColor = vec4(gl_PointCoord.xy, 0, 1);
    gl_FragColor = vec4(vPos * -2.0, 0, 1);
}
</script>

<script id="fs-unused-branch" type="x-shader/x-fragment">
varying mediump vec2 vPos;
uniform mediump float uDefaultsToZero;

void main()
{
    gl_FragColor = vec4(vPos * -2.0, 0, 1);
    if (uDefaultsToZero == 1.0) {
        gl_FragColor = vec4(gl_PointCoord.xy, 0, 1);
    }
}
</script>

<script>
"use strict";
description(document.title);

debug('This test verifies rendering with programs referencing shader variables specific to rendering of POINTS primitives.');

var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("c", {depth: false});

var prog_overwrite = wtu.setupProgram(gl, ["vs-assign", "fs-overwrite"], ["aPosition"]);
var prog_branch = wtu.setupProgram(gl, ["vs-assign", "fs-unused-branch"], ["aPosition"]);
var prog_cond_overwrite = wtu.setupProgram(gl, ["vs-conditional", "fs-overwrite"], ["aPosition"]);
var prog_cond_branch = wtu.setupProgram(gl, ["vs-conditional", "fs-unused-branch"], ["aPosition"]);

var vertData = new Float32Array([
    -1, -1,
    +1, -1,
    -1, +1,
]);

var vertexObject = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
gl.bufferData(gl.ARRAY_BUFFER, vertData, gl.STATIC_DRAW);

gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);

//////////

debug("");
debug("prog-overwrite");

gl.clear(gl.COLOR_BUFFER_BIT);
wtu.checkCanvasRect(gl, 0, 0, 1, 1, [0, 0, 0, 0]); // Bottom-left

gl.useProgram(prog_overwrite);
gl.drawArrays(gl.TRIANGLES, 0, 3);

wtu.checkCanvasRect(gl, 0, 0, 1, 1, [255, 255, 0, 255]); // Bottom-left
wtu.checkCanvasRect(gl, 63, 63, 1, 1, [0, 0, 0, 0]); // Top-right


//////////

debug("");
debug("prog-branch");

gl.clear(gl.COLOR_BUFFER_BIT);
wtu.checkCanvasRect(gl, 0, 0, 1, 1, [0, 0, 0, 0]); // Bottom-left

gl.useProgram(prog_branch);
gl.drawArrays(gl.TRIANGLES, 0, 3);

wtu.checkCanvasRect(gl, 0, 0, 1, 1, [255, 255, 0, 255]); // Bottom-left
wtu.checkCanvasRect(gl, 63, 63, 1, 1, [0, 0, 0, 0]); // Top-right

//////////

debug("");
debug("prog-cond-overwrite");

gl.clear(gl.COLOR_BUFFER_BIT);
wtu.checkCanvasRect(gl, 0, 0, 1, 1, [0, 0, 0, 0]); // Bottom-left

gl.useProgram(prog_cond_overwrite);
gl.drawArrays(gl.TRIANGLES, 0, 3);

wtu.checkCanvasRect(gl, 0, 0, 1, 1, [255, 255, 0, 255]); // Bottom-left
wtu.checkCanvasRect(gl, 63, 63, 1, 1, [0, 0, 0, 0]); // Top-right


//////////

debug("");
debug("prog-cond-branch");

gl.clear(gl.COLOR_BUFFER_BIT);
wtu.checkCanvasRect(gl, 0, 0, 1, 1, [0, 0, 0, 0]); // Bottom-left

gl.useProgram(prog_cond_branch);
gl.drawArrays(gl.TRIANGLES, 0, 3);

wtu.checkCanvasRect(gl, 0, 0, 1, 1, [255, 255, 0, 255]); // Bottom-left
wtu.checkCanvasRect(gl, 63, 63, 1, 1, [0, 0, 0, 0]); // Top-right

var successfullyParsed = true;
</script>

<script src="../../js/js-test-post.js"></script>

</body>
</html>
