|
1 <!-- |
|
2 Copyright (c) 2011 The Chromium Authors. All rights reserved. |
|
3 Use of this source code is governed by a BSD-style license that can be |
|
4 found in the LICENSE file. |
|
5 --> |
|
6 <!DOCTYPE html> |
|
7 <html> |
|
8 <head> |
|
9 <meta charset="utf-8"> |
|
10 <title>WebGL BindAttribLocation Conformance Tests</title> |
|
11 <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
|
12 <script src="../../resources/js-test-pre.js"></script> |
|
13 <script src="../resources/webgl-test.js"></script> |
|
14 </head> |
|
15 <body> |
|
16 <div id="description"></div> |
|
17 <div id="console"></div> |
|
18 <canvas style="border: 1px solid black;" id="canvas" width="50" height="50"></canvas> |
|
19 <script id="vshader" type="text/something-not-javascript"> |
|
20 attribute vec4 vPosition; |
|
21 attribute vec4 vColor; |
|
22 varying vec4 color; |
|
23 void main() |
|
24 { |
|
25 gl_Position = vPosition; |
|
26 color = vColor; |
|
27 } |
|
28 </script> |
|
29 <script id="fshader" type="text/something-not-javascript"> |
|
30 precision mediump float; |
|
31 |
|
32 varying vec4 color; |
|
33 void main() |
|
34 { |
|
35 gl_FragColor = color; |
|
36 } |
|
37 </script> |
|
38 <script> |
|
39 description("This test ensures WebGL implementations don't allow names that start with 'gl_' when calling bindAttribLocation."); |
|
40 |
|
41 debug(""); |
|
42 debug("Canvas.getContext"); |
|
43 |
|
44 var gl = create3DContext(document.getElementById("canvas")); |
|
45 shouldBeNonNull("gl"); |
|
46 |
|
47 function fail(x,y, buf, shouldBe) |
|
48 { |
|
49 var i = (y*50+x) * 4; |
|
50 var reason = "pixel at ("+x+","+y+") is ("+buf[i]+","+buf[i+1]+","+buf[i+2]+","+buf[i+3]+"), should be "+shouldBe; |
|
51 testFailed(reason); |
|
52 } |
|
53 |
|
54 function pass() |
|
55 { |
|
56 testPassed("drawing is correct"); |
|
57 } |
|
58 |
|
59 function loadShader(shaderType, shaderId) { |
|
60 // Get the shader source. |
|
61 var shaderSource = document.getElementById(shaderId).text; |
|
62 |
|
63 // Create the shader object |
|
64 var shader = gl.createShader(shaderType); |
|
65 if (shader == null) { |
|
66 debug("*** Error: unable to create shader '"+shaderId+"'"); |
|
67 return null; |
|
68 } |
|
69 |
|
70 // Load the shader source |
|
71 gl.shaderSource(shader, shaderSource); |
|
72 |
|
73 // Compile the shader |
|
74 gl.compileShader(shader); |
|
75 |
|
76 // Check the compile status |
|
77 var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS); |
|
78 if (!compiled) { |
|
79 // Something went wrong during compilation; get the error |
|
80 var error = gl.getShaderInfoLog(shader); |
|
81 debug("*** Error compiling shader '"+shader+"':"+error); |
|
82 gl.deleteShader(shader); |
|
83 return null; |
|
84 } |
|
85 return shader; |
|
86 } |
|
87 |
|
88 debug(""); |
|
89 debug("Checking gl.bindAttribLocation."); |
|
90 |
|
91 var program = gl.createProgram(); |
|
92 gl.bindAttribLocation(program, 0, "gl_foo"); |
|
93 glErrorShouldBe(gl, gl.INVALID_OPERATION, |
|
94 "bindAttribLocation should return INVALID_OPERATION if name starts with 'gl_'"); |
|
95 gl.bindAttribLocation(program, 0, "gl_TexCoord0"); |
|
96 glErrorShouldBe(gl, gl.INVALID_OPERATION, |
|
97 "bindAttribLocation should return INVALID_OPERATION if name starts with 'gl_'"); |
|
98 |
|
99 var vs = loadShader(gl.VERTEX_SHADER, "vshader"); |
|
100 var fs = loadShader(gl.FRAGMENT_SHADER, "fshader"); |
|
101 gl.attachShader(program, vs); |
|
102 gl.attachShader(program, fs); |
|
103 |
|
104 var positions = gl.createBuffer(); |
|
105 gl.bindBuffer(gl.ARRAY_BUFFER, positions); |
|
106 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 0,0.5,0, -0.5,-0.5,0, 0.5,-0.5,0 ]), gl.STATIC_DRAW); |
|
107 |
|
108 var colors = gl.createBuffer(); |
|
109 gl.bindBuffer(gl.ARRAY_BUFFER, colors); |
|
110 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ |
|
111 0,1,0,1, |
|
112 0,1,0,1, |
|
113 0,1,0,1]), gl.STATIC_DRAW); |
|
114 |
|
115 function setBindLocations(colorLocation, positionLocation) { |
|
116 gl.bindAttribLocation(program, positionLocation, "vPosition"); |
|
117 gl.bindAttribLocation(program, colorLocation, "vColor"); |
|
118 gl.linkProgram(program); |
|
119 gl.useProgram(program); |
|
120 var linked = (gl.getProgramParameter(program, gl.LINK_STATUS) != 0); |
|
121 assertMsg(linked, "program linked successfully"); |
|
122 |
|
123 debug("vPosition:" + gl.getAttribLocation(program, "vPosition")) |
|
124 debug("vColor :" + gl.getAttribLocation(program, "vColor")) |
|
125 assertMsg(gl.getAttribLocation(program, "vPosition") == positionLocation, |
|
126 "location of vPosition should be " + positionLocation); |
|
127 assertMsg(gl.getAttribLocation(program, "vColor") == colorLocation, |
|
128 "location of vColor should be " + colorLocation); |
|
129 |
|
130 var ploc = gl.getAttribLocation(program, "vPosition"); |
|
131 var cloc = gl.getAttribLocation(program, "vColor"); |
|
132 gl.bindBuffer(gl.ARRAY_BUFFER, positions); |
|
133 gl.enableVertexAttribArray(positionLocation); |
|
134 gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0); |
|
135 gl.bindBuffer(gl.ARRAY_BUFFER, colors); |
|
136 gl.enableVertexAttribArray(colorLocation); |
|
137 gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 0, 0); |
|
138 } |
|
139 |
|
140 function checkDraw(colorLocation, positionLocation, r, g, b, a) { |
|
141 gl.clearColor(0, 0, 0, 1); |
|
142 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
|
143 gl.drawArrays(gl.TRIANGLES, 0, 3); |
|
144 |
|
145 var width = 50; |
|
146 var height = 50; |
|
147 var buf = new Uint8Array(width * height * 4); |
|
148 gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf); |
|
149 |
|
150 function checkPixel(x, y, r, g, b, a) { |
|
151 var offset = (y * width + x) * 4; |
|
152 if (buf[offset + 0] != r || |
|
153 buf[offset + 1] != g || |
|
154 buf[offset + 2] != b || |
|
155 buf[offset + 3] != a) { |
|
156 fail(x, y, buf, "(" + r + "," + g + "," + b + "," + a + ")"); |
|
157 return false; |
|
158 } |
|
159 return true; |
|
160 } |
|
161 |
|
162 // Test several locations |
|
163 // First line should be all black |
|
164 var success = true; |
|
165 for (var i = 0; i < 50; ++i) |
|
166 success = success && checkPixel(i, 0, 0, 0, 0, 255); |
|
167 |
|
168 // Line 15 should be red for at least 10 rgba pixels starting 20 pixels in |
|
169 var offset = (15 * 50 + 20) * 4; |
|
170 for (var i = 0; i < 10; ++i) |
|
171 success = success && checkPixel(20 + i, 15, r, g, b, a); |
|
172 |
|
173 // Last line should be all black |
|
174 for (var i = 0; i < 50; ++i) |
|
175 success = success && checkPixel(i, 49, 0, 0, 0, 255); |
|
176 |
|
177 if (success) |
|
178 pass(); |
|
179 |
|
180 gl.disableVertexAttribArray(positionLocation); |
|
181 gl.disableVertexAttribArray(colorLocation); |
|
182 } |
|
183 |
|
184 setBindLocations(2, 3); |
|
185 checkDraw(2, 3, 0, 255, 0, 255); |
|
186 |
|
187 setBindLocations(0, 3); |
|
188 gl.disableVertexAttribArray(0); |
|
189 gl.vertexAttrib4f(0, 1, 0, 0, 1); |
|
190 checkDraw(0, 3, 255, 0, 0, 255); |
|
191 |
|
192 glErrorShouldBe(gl, gl.NO_ERROR); |
|
193 |
|
194 debug(""); |
|
195 successfullyParsed = true; |
|
196 |
|
197 </script> |
|
198 <script>finishTest();</script> |
|
199 |
|
200 </body> |
|
201 </html> |