﻿<!--
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 bindAttribLocation with Matrix Attributes Conformance Test</title>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<canvas id="canvas" width="8" height="8" style="width: 8px; height: 8px;"></canvas>
<script>
"use strict";
description("This test verifies that vectors placed via bindAttribLocation right after matricies will fail if there is insufficient room for the matrix.");

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

// Make sure we have room for at least a mat4.
var maxAttributes = gl.getParameter(gl.MAX_VERTEX_ATTRIBS);
debug('MAX_VERTEX_ATTRIBUTES is ' + maxAttributes);
shouldBeGreaterThanOrEqual('maxAttributes', '4');

var glFragmentShader = wtu.loadShader(gl, wtu.simpleColorFragmentShader, gl.FRAGMENT_SHADER);

// Given a matrix dimension, load a vertex shader with a matrix of that dimension
// and a vector. Ensure that both the vector and matrix are active attributes.
// Return the compiled vertex shader.
function loadVertexShader(numMatrixDimensions) {
    var strVertexShader =
        'attribute mat' + numMatrixDimensions + ' matrix;\n' +
        'attribute vec' + numMatrixDimensions + ' vector;\n' +
        'void main(void) { gl_Position = vec4(vector*matrix';
    // Ensure the vec4 has the correct number of dimensions in order to be assignable
    // to gl_Position.
    for (var ii = numMatrixDimensions; ii < 4; ++ii) {
        strVertexShader += ",0.0";
    }
    strVertexShader += ");}\n";
    return wtu.loadShader(gl, strVertexShader, gl.VERTEX_SHADER);
}

// Given a vertex shader, matrix location and vector location, create and link
// a program with glFragmentShader and a vertex shader returned by loadVertexShader
// attached. Bind the matrix to matrixLocation and the vector to vectorLocation.
// Return whether the link was successful.
function createAndLinkProgram(glVertexShader, matrixLocation, vectorLocation) {
    var glProgram = gl.createProgram();
    gl.bindAttribLocation(glProgram, matrixLocation, 'matrix');
    gl.bindAttribLocation(glProgram, vectorLocation, 'vector');
    gl.attachShader(glProgram, glVertexShader);
    gl.attachShader(glProgram, glFragmentShader);
    gl.linkProgram(glProgram);
    return gl.getProgramParameter(glProgram, gl.LINK_STATUS);
}

// For each matrix dimension (mat2, mat3 and mat4)
for (var mm = 2; mm <= 4; ++mm) {
    debug('Testing ' + mm + ' dimensional matrices');
    var glVertexShader = loadVertexShader(mm);
    // Per the WebGL spec: "LinkProgram will fail if the attribute bindings assigned
    // by bindAttribLocation do not leave enough space to assign a location for an
    // active matrix attribute which requires multiple contiguous generic attributes."
    // We will test this by placing the vector after the matrix attribute such that there
    // is not enough room for the matrix. Vertify the link operation fails.

    // Run the test for each available attribute slot.  Go to maxAttributes-mm to leave enough room
    // for the matrix itself. Leave another slot open for the vector following the matrix.
    for (var pp = 0; pp <= maxAttributes - mm - 1; ++pp) {
        // For each matrix dimension, bind the vector right after the matrix such that we leave
        // insufficient room for the matrix. Verify doing this will fail the link operation.
        for (var ll = 0; ll < mm; ++ll) {
            var vectorLocation = pp + ll;
            assertMsg(!createAndLinkProgram(glVertexShader, /*matrixLocation*/pp, vectorLocation),
                "Matrix with location " + pp + " and vector with location " + vectorLocation + " should not link.");
        }
        // Ensure that once we have left enough room for the matrix, the program links successfully.
        var vectorLocation = pp + ll;
        assertMsg(createAndLinkProgram(glVertexShader, /*matrixLocation*/pp, vectorLocation),
            "Matrix with location " + pp + " and vector with location " + vectorLocation + " should link.");
        debug('');
    }
    debug('');
}

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