4 ** Copyright (c) 2012 The Khronos Group Inc.
6 ** Permission is hereby granted, free of charge, to any person obtaining a
7 ** copy of this software and/or associated documentation files (the
8 ** "Materials"), to deal in the Materials without restriction, including
9 ** without limitation the rights to use, copy, modify, merge, publish,
10 ** distribute, sublicense, and/or sell copies of the Materials, and to
11 ** permit persons to whom the Materials are furnished to do so, subject to
12 ** the following conditions:
14 ** The above copyright notice and this permission notice shall be included
15 ** in all copies or substantial portions of the Materials.
17 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
31 <meta charset="utf-8">
32 <title>WebGL BindAttribLocation Conformance Tests</title>
33 <link rel="stylesheet" href="../../resources/js-test-style.css"/>
34 <script src="../../resources/js-test-pre.js"></script>
35 <script src="../resources/webgl-test-utils.js"></script>
38 <div id="description"></div>
39 <div id="console"></div>
40 <canvas style="border: 1px solid black;" id="canvas" width="50" height="50"></canvas>
41 <script id="vshader" type="text/something-not-javascript">
42 attribute vec4 vPosition;
43 attribute vec4 vColor;
47 gl_Position = vPosition;
51 <script id="fshader" type="text/something-not-javascript">
52 precision mediump float;
62 description("This test ensures WebGL implementations don't allow names that start with 'gl_' when calling bindAttribLocation.");
65 debug("Canvas.getContext");
67 var wtu = WebGLTestUtils;
68 var gl = wtu.create3DContext("canvas");
69 shouldBeNonNull("gl");
72 debug("Checking gl.bindAttribLocation.");
74 var program = gl.createProgram();
75 gl.bindAttribLocation(program, 0, "gl_foo");
76 wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION,
77 "bindAttribLocation should return INVALID_OPERATION if name starts with 'gl_'");
78 gl.bindAttribLocation(program, 0, "gl_TexCoord0");
79 wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION,
80 "bindAttribLocation should return INVALID_OPERATION if name starts with 'gl_'");
82 var vs = wtu.loadShaderFromScript(gl, 'vshader', gl.VERTEX_SHADER);
83 var fs = wtu.loadShaderFromScript(gl, 'fshader', gl.FRAGMENT_SHADER);
84 gl.attachShader(program, vs);
85 gl.attachShader(program, fs);
87 var positions = gl.createBuffer();
88 gl.bindBuffer(gl.ARRAY_BUFFER, positions);
89 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 0,0.5,0, -0.5,-0.5,0, 0.5,-0.5,0 ]), gl.STATIC_DRAW);
91 var colors = gl.createBuffer();
92 gl.bindBuffer(gl.ARRAY_BUFFER, colors);
93 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
96 0,1,0,1]), gl.STATIC_DRAW);
98 function setBindLocations(colorLocation, positionLocation) {
99 gl.bindAttribLocation(program, positionLocation, "vPosition");
100 gl.bindAttribLocation(program, colorLocation, "vColor");
101 gl.linkProgram(program);
102 gl.useProgram(program);
103 var linked = (gl.getProgramParameter(program, gl.LINK_STATUS) != 0);
104 assertMsg(linked, "program linked successfully");
106 debug("vPosition:" + gl.getAttribLocation(program, "vPosition"))
107 debug("vColor :" + gl.getAttribLocation(program, "vColor"))
108 assertMsg(gl.getAttribLocation(program, "vPosition") == positionLocation,
109 "location of vPosition should be " + positionLocation);
110 assertMsg(gl.getAttribLocation(program, "vColor") == colorLocation,
111 "location of vColor should be " + colorLocation);
113 var ploc = gl.getAttribLocation(program, "vPosition");
114 var cloc = gl.getAttribLocation(program, "vColor");
115 gl.bindBuffer(gl.ARRAY_BUFFER, positions);
116 gl.enableVertexAttribArray(positionLocation);
117 gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0);
118 gl.bindBuffer(gl.ARRAY_BUFFER, colors);
119 gl.enableVertexAttribArray(colorLocation);
120 gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 0, 0);
123 function checkDraw(colorLocation, positionLocation, r, g, b, a) {
124 gl.clearColor(0, 0, 0, 1);
125 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
126 gl.drawArrays(gl.TRIANGLES, 0, 3);
131 // Test several locations
132 wtu.checkCanvasRect(gl, 0, 0, width, 1, [0, 0, 0, 255],
133 "First line should be all black");
134 wtu.checkCanvasRect(gl, 20, 15, 10, 1, [r, g, b, a],
135 "Line 15 should be red for at least 10 rgba pixels starting 20 pixels in");
136 wtu.checkCanvasRect(gl, 0, height - 1, width, 0, [0, 0, 0, 255],
137 "Last line should be all black");
139 gl.disableVertexAttribArray(positionLocation);
140 gl.disableVertexAttribArray(colorLocation);
143 setBindLocations(2, 3);
144 checkDraw(2, 3, 0, 255, 0, 255);
146 setBindLocations(0, 3);
147 gl.disableVertexAttribArray(0);
148 gl.vertexAttrib4f(0, 1, 0, 0, 1);
149 checkDraw(0, 3, 255, 0, 0, 255);
151 wtu.glErrorShouldBe(gl, gl.NO_ERROR);
154 var successfullyParsed = true;
157 <script src="../../resources/js-test-post.js"></script>