<!--
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>WebGL Transform Feedback Conformance Tests - one buffer bound to two binding points</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>
<div id="description"></div>
<div id="console"></div>
<script id="vshader" type="x-shader/x-vertex">#version 300 es

in vec4 in_data;
out vec4 out_add;
out vec4 out_mul;
void main(void) {
    out_add = in_data + vec4(2.0, 3.0, 4.0, 5.0);
    out_mul = in_data * vec4(2.0, 3.0, 4.0, 5.0);
}
</script>
<script>
"use strict";
description();

debug("");

var wtu = WebGLTestUtils;
var gl = wtu.create3DContext(null, null, 2);
var program = null;

if (!gl) {
    testFailed("WebGL context does not exist");
} else {
    testPassed("WebGL context exists");
    runTwoOutFeedbackTest();
}

function doDrawWithTransformFeedback(expectedError, msg) {
    gl.beginTransformFeedback(gl.POINTS);
    wtu.glErrorShouldBe(gl, expectedError, msg);
    gl.drawArrays(gl.POINTS, 0, 3);
    gl.endTransformFeedback();
    gl.getError();
}

function runTwoOutFeedbackTest() {
    debug("");
    debug("Test binding the same buffer to two transform feedback binding points. Buffer should be untouched and an error should be generated.")

    // Build the input and output buffers
    var in_data = [
        1.0, 2.0, 3.0, 4.0,
        2.0, 4.0, 8.0, 16.0,
        0.75, 0.5, 0.25, 0.0
    ];

    var in_buffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, in_buffer);
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(in_data), gl.STATIC_DRAW);

    // Create the transform feedback shader
    program = wtu.setupTransformFeedbackProgram(gl, ["vshader", wtu.simpleColorFragmentShaderESSL300],
        ["out_add", "out_mul"], gl.SEPARATE_ATTRIBS,
        ["in_data"]);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "linking transform feedback shader should not set an error");
    shouldBeNonNull("program");

    // Draw the the transform feedback buffers
    var tf = gl.createTransformFeedback();

    gl.enableVertexAttribArray(0);
    gl.bindBuffer(gl.ARRAY_BUFFER, in_buffer);
    gl.vertexAttribPointer(0, 4, gl.FLOAT, false, 16, 0);

    var out_buffer = gl.createBuffer();
    var out_buffer2 = gl.createBuffer();
    gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, out_buffer);

    // Test binding the same buffer to two transform feedback binding points with bindBufferBase.
    debug("");
    gl.enable(gl.RASTERIZER_DISCARD);
    gl.bufferData(gl.TRANSFORM_FEEDBACK_BUFFER, Float32Array.BYTES_PER_ELEMENT * in_data.length * 2, gl.STATIC_DRAW);
    gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, out_buffer);
    gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 1, out_buffer);

    doDrawWithTransformFeedback(gl.INVALID_OPERATION, "same buffer bound to two transform feedback binding points with bindBufferBase");

    gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, null);
    gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 1, null);
    gl.disable(gl.RASTERIZER_DISCARD);

    // Check buffer contents.
    var expectedZeroes = [];
    for (var i = 0; i < in_data.length * 2; ++i)
    {
        expectedZeroes.push(0.0);
    }
    gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, out_buffer);
    debug("buffer should be untouched - filled with zeroes");
    wtu.checkFloatBuffer(gl, gl.TRANSFORM_FEEDBACK_BUFFER, expectedZeroes);

    // Test binding the same buffer to two transform feedback binding points with bindBufferRange. The ranges overlap just slightly.
    debug("");
    gl.enable(gl.RASTERIZER_DISCARD);
    gl.bufferData(gl.TRANSFORM_FEEDBACK_BUFFER, Float32Array.BYTES_PER_ELEMENT * in_data.length * 2, gl.STATIC_DRAW);
    gl.bindBufferRange(gl.TRANSFORM_FEEDBACK_BUFFER, 0, out_buffer, 0, in_data.length * 4);
    gl.bindBufferRange(gl.TRANSFORM_FEEDBACK_BUFFER, 1, out_buffer, (in_data.length - 1) * 4, in_data.length * 4);

    doDrawWithTransformFeedback(gl.INVALID_OPERATION, "same buffer bound to two transform feedback binding points with bindBufferRange, overlapping ranges");

    gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, null);
    gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 1, null);
    gl.disable(gl.RASTERIZER_DISCARD);

    // Check buffer contents.
    gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, out_buffer);
    debug("buffer should be untouched - filled with zeroes");
    wtu.checkFloatBuffer(gl, gl.TRANSFORM_FEEDBACK_BUFFER, expectedZeroes);

    // Test non-overlapping ranges.
    debug("");
    gl.enable(gl.RASTERIZER_DISCARD);
    gl.bufferData(gl.TRANSFORM_FEEDBACK_BUFFER, Float32Array.BYTES_PER_ELEMENT * in_data.length * 2, gl.STATIC_DRAW);
    gl.bindBufferRange(gl.TRANSFORM_FEEDBACK_BUFFER, 0, out_buffer, 0, in_data.length * 4);
    gl.bindBufferRange(gl.TRANSFORM_FEEDBACK_BUFFER, 1, out_buffer, in_data.length * 4, in_data.length * 4);

    doDrawWithTransformFeedback(gl.INVALID_OPERATION, "same buffer bound to two transform feedback binding points with bindBufferRange, non-overlapping ranges");

    gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, null);
    gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 1, null);
    gl.disable(gl.RASTERIZER_DISCARD);

    // Check buffer contents.
    gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, out_buffer);
    debug("buffer should be untouched - filled with zeroes");
    wtu.checkFloatBuffer(gl, gl.TRANSFORM_FEEDBACK_BUFFER, expectedZeroes);

    // Test binding the same buffer to a binding point that doesn't have a corresponding output in the vertex shader.
    debug("");
    gl.enable(gl.RASTERIZER_DISCARD);
    gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, out_buffer2);
    gl.bufferData(gl.TRANSFORM_FEEDBACK_BUFFER, Float32Array.BYTES_PER_ELEMENT * in_data.length * 2, gl.STATIC_DRAW);
    gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, out_buffer);
    gl.bufferData(gl.TRANSFORM_FEEDBACK_BUFFER, Float32Array.BYTES_PER_ELEMENT * in_data.length * 2, gl.STATIC_DRAW);
    gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, out_buffer);
    gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 1, out_buffer2);
    gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 2, out_buffer);  // No corresponding output.

    doDrawWithTransformFeedback(gl.INVALID_OPERATION, "same buffer bound to two transform feedback binding points with bindBufferBase, but one of the binding points doesn't have a corresponding shader output");

    gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, null);
    gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 1, null);
    gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 2, null);
    gl.disable(gl.RASTERIZER_DISCARD);

    // Check buffer contents.
    debug("buffers should be untouched - filled with zeroes");
    gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, out_buffer);
    wtu.checkFloatBuffer(gl, gl.TRANSFORM_FEEDBACK_BUFFER, expectedZeroes);
    gl.bindBuffer(gl.TRANSFORM_FEEDBACK_BUFFER, out_buffer2);
    wtu.checkFloatBuffer(gl, gl.TRANSFORM_FEEDBACK_BUFFER, expectedZeroes);

    finishTest();
}
</script>

</body>
</html>
