<!--
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">
<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>
<title>WebGL Out-of-Bounds Array Buffer Conformance Test</title>
</head>
<body>
<canvas id="canvas" width="8" height="8" style="width: 100px; height: 100px;"></canvas>
<div id="description"></div>
<div id="console"></div>
<script id="vsCheckOutOfBounds" type="x-shader/x-vertex">
    precision mediump float;
    attribute vec2 position;
    attribute vec4 vecRandom;
    varying vec4 v_color;

    // Per the spec, each component can either contain existing contents
    // of the buffer or 0.
    bool testFloatComponent(float component) {
        return (component == 0.2 || component == 0.0);
    }
    // The last component is additionally allowed to be 1.0.
    bool testLastFloatComponent(float component) {
        return testFloatComponent(component) || component == 1.0;
    }

    void main() {
        if (testFloatComponent(vecRandom.x) &&
            testFloatComponent(vecRandom.y) &&
            testFloatComponent(vecRandom.z) &&
            testLastFloatComponent(vecRandom.w)) {
            v_color = vec4(0.0, 1.0, 0.0, 1.0); // green -- We're good
        } else {
            v_color = vec4(1.0, 0.0, 0.0, 1.0); // red -- Unexpected value
        }
        gl_Position = vec4(position, 0.0, 1.0);
    }
</script>
<script>
"use strict";
description("This test verifies that out-of-bounds array buffers behave according to spec.");

var wtu = WebGLTestUtils;
var canvas = document.getElementById("canvas");
var gl = wtu.create3DContext(canvas, {antialias: false});

var numberOfQuads = 200;

// Draw out-of-bounds beginning with the start offset passed in.
// Ensure that drawArrays flags either no error or INVALID_OPERATION. In the case of INVALID_OPERATION,
// no canvas pixels can be touched.  In the case of NO_ERROR, all written values must either be the
// zero vertex or a value in the vertex buffer.  See vsCheckOutOfBounds shader.
function drawAndVerifyOutOfBoundsArrays(gl, first, count) {
    gl.clearColor(0.0, 0.0, 1.0, 1.0);  // Start with blue to indicate no pixels touched.
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

    gl.drawArrays(gl.TRIANGLES, first, count);
    var error = gl.getError();
    if (error === gl.INVALID_OPERATION) {
        testPassed("drawArrays flagged INVALID_OPERATION, which is valid so long as all canvas pixels were not touched.");
        wtu.checkCanvas(gl, [0, 0, 255, 255]);
    } else if (error === gl.NO_ERROR) {
        testPassed("drawArrays flagged NO_ERROR, which is valid so long as all canvas pixels are green.");
        wtu.checkCanvas(gl, [0, 255, 0, 255]);
    } else {
        testFailed("Invalid error flagged by drawArrays. Should be INVALID_OPERATION or NO_ERROR");
    }
}

// Create a vertex buffer with 200 properly formed triangle quads. These quads will cover the
// canvas texture such that every single pixel is touched by the fragment shader.
var glQuadBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, glQuadBuffer);
var quadPositions = new Float32Array(numberOfQuads * /*ComponentsPerQuad*/2 * /*VerticesPerQuad*/6);
for (var i = 0; i < quadPositions.length; i += /*ComponentsPerQuad*/2 * /*VerticesPerQuad*/6) {
    quadPositions[i+0] = -1.0;  // upper left
    quadPositions[i+1] =  1.0;
    quadPositions[i+2] =  1.0;  // upper right
    quadPositions[i+3] =  1.0;
    quadPositions[i+4] = -1.0;  // lower left
    quadPositions[i+5] = -1.0;
    quadPositions[i+6] =  1.0;  // upper right
    quadPositions[i+7] =  1.0;
    quadPositions[i+8] =  1.0;  // lower right
    quadPositions[i+9] = -1.0;
    quadPositions[i+10] = -1.0;  // lower left
    quadPositions[i+11] = -1.0;
}
gl.bufferData(gl.ARRAY_BUFFER, quadPositions, gl.STATIC_DRAW);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);


// Create a small vertex buffer with determined-ahead-of-time "random" values (0.2). This buffer will be
// the one read past the end.
var glVertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, glVertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([0.2, 0.2, 0.2, 0.2, 0.2, 0.2]), gl.STATIC_DRAW);
gl.enableVertexAttribArray(1);
gl.vertexAttribPointer(1, 4, gl.FLOAT, false, 0, 0);

// Setup the verification program.
var glProgram = wtu.setupProgram(gl, ["vsCheckOutOfBounds", wtu.simpleVertexColorFragmentShader], ["position", "vecRandom"]);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Shader and buffer setup should not generate errors");

debug("Test -- Draw off the end of the vertex buffer near the beginning of the out of bounds area.");
drawAndVerifyOutOfBoundsArrays(gl, /*first*/6, /*count*/6);

debug("");

debug("Test -- Draw off the end of the vertex buffer near the end of the out of bounds area.")
drawAndVerifyOutOfBoundsArrays(gl, /*first*/(numberOfQuads - 1) * 6, /*count*/6);

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