<!--
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>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>
