summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/attribs/gl-bindAttribLocation-matrix.html
blob: 19f255102d61d96d57a78590ad434ffc1fb9af3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!--

/*
** Copyright (c) 2014 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">
<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>