Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / canvas / webgl / gl-bind-attrib-location-test.html
blob8911f9b6772726c2b71c1d0e0c0a45252f207599
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
2 "http://www.w3.org/TR/html4/loose.dtd">
3 <html>
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6 <title>WebGL BindAttribLocation Conformance Tests</title>
7 <script src="../../../resources/js-test.js"></script>
8 <script src="resources/webgl-test.js"></script>
9 </head>
10 <body>
11 <div id="description"></div>
12 <div id="console"></div>
13 <canvas style="border: 1px solid black;" id="canvas" width="50" height="50"></canvas>
14 <script id="vshader" type="text/something-not-javascript">
15 attribute vec4 vPosition;
16 attribute vec4 vColor;
17 varying vec4 color;
18 void main()
20 gl_Position = vPosition;
21 color = vColor;
23 </script>
24 <script id="fshader" type="text/something-not-javascript">
25 #ifdef GL_ES
26 precision highp float;
27 #endif
28 varying vec4 color;
29 void main()
31 gl_FragColor = color;
33 </script>
34 <script>
35 description("This test ensures WebGL implementations don't allow names that start with 'gl_' when calling bindAttribLocation.");
37 if (window.internals)
38 window.internals.settings.setWebGLErrorsToConsoleEnabled(false);
40 debug("");
41 debug("Canvas.getContext");
43 var gl = create3DContext(document.getElementById("canvas"));
44 shouldBeNonNull(gl);
46 function fail(x,y, buf, shouldBe)
48 var i = (y*50+x) * 4;
49 var reason = "pixel at ("+x+","+y+") is ("+buf[i]+","+buf[i+1]+","+buf[i+2]+","+buf[i+3]+"), should be "+shouldBe;
50 testFailed(reason);
53 function pass()
55 testPassed("drawing is correct");
58 function loadShader(shaderType, shaderId) {
59 // Get the shader source.
60 var shaderSource = document.getElementById(shaderId).text;
62 // Create the shader object
63 var shader = gl.createShader(shaderType);
64 if (shader == null) {
65 debug("*** Error: unable to create shader '"+shaderId+"'");
66 return null;
69 // Load the shader source
70 gl.shaderSource(shader, shaderSource);
72 // Compile the shader
73 gl.compileShader(shader);
75 // Check the compile status
76 var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
77 if (!compiled) {
78 // Something went wrong during compilation; get the error
79 var error = gl.getShaderInfoLog(shader);
80 debug("*** Error compiling shader '"+shader+"':"+error);
81 gl.deleteShader(shader);
82 return null;
84 return shader;
87 debug("");
88 debug("Checking gl.bindAttribLocation.");
90 var program = gl.createProgram();
91 gl.bindAttribLocation(program, 0, "gl_foo");
92 glErrorShouldBe(gl, gl.INVALID_OPERATION,
93 "bindAttribLocation should return INVALID_OPERATION if name starts with 'gl_'");
94 gl.bindAttribLocation(program, 0, "gl_TexCoord0");
95 glErrorShouldBe(gl, gl.INVALID_OPERATION,
96 "bindAttribLocation should return INVALID_OPERATION if name starts with 'gl_'");
98 var vs = loadShader(gl.VERTEX_SHADER, "vshader");
99 var fs = loadShader(gl.FRAGMENT_SHADER, "fshader");
100 gl.attachShader(program, vs);
101 gl.attachShader(program, fs);
103 var positions = gl.createBuffer();
104 gl.bindBuffer(gl.ARRAY_BUFFER, positions);
105 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 var colors = gl.createBuffer();
108 gl.bindBuffer(gl.ARRAY_BUFFER, colors);
109 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
110 0,1,0,1,
111 0,1,0,1,
112 0,1,0,1]), gl.STATIC_DRAW);
114 function setBindLocations(colorLocation, positionLocation) {
115 gl.bindAttribLocation(program, positionLocation, "vPosition");
116 gl.bindAttribLocation(program, colorLocation, "vColor");
117 gl.linkProgram(program);
118 gl.useProgram(program);
119 var linked = (gl.getProgramParameter(program, gl.LINK_STATUS) != 0);
120 assertMsg(linked, "program linked successfully");
122 debug("vPosition:" + gl.getAttribLocation(program, "vPosition"))
123 debug("vColor :" + gl.getAttribLocation(program, "vColor"))
124 assertMsg(gl.getAttribLocation(program, "vPosition") == positionLocation,
125 "location of vPositon should be " + positionLocation);
126 assertMsg(gl.getAttribLocation(program, "vColor") == colorLocation,
127 "location of vColor should be " + colorLocation);
129 var ploc = gl.getAttribLocation(program, "vPosition");
130 var cloc = gl.getAttribLocation(program, "vColor");
131 gl.bindBuffer(gl.ARRAY_BUFFER, positions);
132 gl.enableVertexAttribArray(positionLocation);
133 gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0);
134 gl.bindBuffer(gl.ARRAY_BUFFER, colors);
135 gl.enableVertexAttribArray(colorLocation);
136 gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 0, 0);
139 function checkDraw(colorLocation, positionLocation, r, g, b, a) {
140 gl.clearColor(0, 0, 0, 1);
141 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
142 gl.drawArrays(gl.TRIANGLES, 0, 3);
144 var width = 50;
145 var height = 50;
146 var buf = new Uint8Array(width * height * 4);
147 gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
149 function checkPixel(x, y, r, g, b, a) {
150 var offset = (y * width + x) * 4;
151 if (buf[offset + 0] != r ||
152 buf[offset + 1] != g ||
153 buf[offset + 2] != b ||
154 buf[offset + 3] != a) {
155 fail(x, y, buf, "(" + r + "," + g + "," + b + "," + a + ")");
156 return false;
158 return true;
161 // Test several locations
162 // First line should be all black
163 var success = true;
164 for (var i = 0; i < 50; ++i)
165 success = success && checkPixel(i, 0, 0, 0, 0, 255);
167 // Line 15 should be red for at least 10 rgba pixels starting 20 pixels in
168 var offset = (15 * 50 + 20) * 4;
169 for (var i = 0; i < 10; ++i)
170 success = success && checkPixel(20 + i, 15, r, g, b, a);
172 // Last line should be all black
173 for (var i = 0; i < 50; ++i)
174 success = success && checkPixel(i, 49, 0, 0, 0, 255);
176 if (success)
177 pass();
179 gl.disableVertexAttribArray(positionLocation);
180 gl.disableVertexAttribArray(colorLocation);
183 setBindLocations(2, 3);
184 checkDraw(2, 3, 0, 255, 0, 255);
186 setBindLocations(0, 3);
187 gl.disableVertexAttribArray(0);
188 gl.vertexAttrib4f(0, 1, 0, 0, 1);
189 checkDraw(0, 3, 255, 0, 0, 255);
191 glErrorShouldBe(gl, gl.NO_ERROR);
193 debug("");
195 </script>
197 </body>
198 </html>