forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gl-bind-attrib-location-test.html
203 lines (172 loc) · 6.08 KB
/
gl-bind-attrib-location-test.html
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<!--
Copyright (c) 2009 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>WebGL BindAttribLocation Conformance Tests</title>
<link rel="stylesheet" href="../resources/js-test-style.css"/>
<script src="../resources/js-test-pre.js"></script>
<script src="resources/webgl-test.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<canvas style="border: 1px solid black;" id="canvas" width="50" height="50"></canvas>
<script id="vshader" type="text/something-not-javascript">
attribute vec4 vPosition;
attribute vec4 vColor;
varying vec4 color;
void main()
{
gl_Position = vPosition;
color = vColor;
}
</script>
<script id="fshader" type="text/something-not-javascript">
#ifdef GL_ES
precision highp float;
#endif
varying vec4 color;
void main()
{
gl_FragColor = color;
}
</script>
<script>
description("This test ensures WebGL implementations don't allow names that start with 'gl_' when calling bindAttribLocation.");
debug("");
debug("Canvas.getContext");
var gl = create3DContext(document.getElementById("canvas"));
shouldBeNonNull(gl);
function fail(x,y, buf, shouldBe)
{
var i = (y*50+x) * 4;
var reason = "pixel at ("+x+","+y+") is ("+buf[i]+","+buf[i+1]+","+buf[i+2]+","+buf[i+3]+"), should be "+shouldBe;
testFailed(reason);
}
function pass()
{
testPassed("drawing is correct");
}
function loadShader(shaderType, shaderId) {
// Get the shader source.
var shaderSource = document.getElementById(shaderId).text;
// Create the shader object
var shader = gl.createShader(shaderType);
if (shader == null) {
debug("*** Error: unable to create shader '"+shaderId+"'");
return null;
}
// Load the shader source
gl.shaderSource(shader, shaderSource);
// Compile the shader
gl.compileShader(shader);
// Check the compile status
var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (!compiled) {
// Something went wrong during compilation; get the error
var error = gl.getShaderInfoLog(shader);
debug("*** Error compiling shader '"+shader+"':"+error);
gl.deleteShader(shader);
return null;
}
return shader;
}
debug("");
debug("Checking gl.bindAttribLocation.");
var program = gl.createProgram();
gl.bindAttribLocation(program, 0, "gl_foo");
glErrorShouldBe(gl, gl.INVALID_OPERATION,
"bindAttribLocation should return INVALID_OPERATION if name starts with 'gl_'");
gl.bindAttribLocation(program, 0, "gl_TexCoord0");
glErrorShouldBe(gl, gl.INVALID_OPERATION,
"bindAttribLocation should return INVALID_OPERATION if name starts with 'gl_'");
var vs = loadShader(gl.VERTEX_SHADER, "vshader");
var fs = loadShader(gl.FRAGMENT_SHADER, "fshader");
gl.attachShader(program, vs);
gl.attachShader(program, fs);
var positions = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positions);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 0,0.5,0, -0.5,-0.5,0, 0.5,-0.5,0 ]), gl.STATIC_DRAW);
var colors = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colors);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
0,1,0,1,
0,1,0,1,
0,1,0,1]), gl.STATIC_DRAW);
function setBindLocations(colorLocation, positionLocation) {
gl.bindAttribLocation(program, positionLocation, "vPosition");
gl.bindAttribLocation(program, colorLocation, "vColor");
gl.linkProgram(program);
gl.useProgram(program);
var linked = (gl.getProgramParameter(program, gl.LINK_STATUS) != 0);
assertMsg(linked, "program linked successfully");
debug("vPosition:" + gl.getAttribLocation(program, "vPosition"))
debug("vColor :" + gl.getAttribLocation(program, "vColor"))
assertMsg(gl.getAttribLocation(program, "vPosition") == positionLocation,
"location of vPosition should be " + positionLocation);
assertMsg(gl.getAttribLocation(program, "vColor") == colorLocation,
"location of vColor should be " + colorLocation);
var ploc = gl.getAttribLocation(program, "vPosition");
var cloc = gl.getAttribLocation(program, "vColor");
gl.bindBuffer(gl.ARRAY_BUFFER, positions);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, colors);
gl.enableVertexAttribArray(colorLocation);
gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 0, 0);
}
function checkDraw(colorLocation, positionLocation, r, g, b, a) {
gl.clearColor(0, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 3);
var width = 50;
var height = 50;
var buf = new Uint8Array(width * height * 4);
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
function checkPixel(x, y, r, g, b, a) {
var offset = (y * width + x) * 4;
if (buf[offset + 0] != r ||
buf[offset + 1] != g ||
buf[offset + 2] != b ||
buf[offset + 3] != a) {
fail(x, y, buf, "(" + r + "," + g + "," + b + "," + a + ")");
return false;
}
return true;
}
// Test several locations
// First line should be all black
var success = true;
for (var i = 0; i < 50; ++i)
success = success && checkPixel(i, 0, 0, 0, 0, 255);
// Line 15 should be red for at least 10 rgba pixels starting 20 pixels in
var offset = (15 * 50 + 20) * 4;
for (var i = 0; i < 10; ++i)
success = success && checkPixel(20 + i, 15, r, g, b, a);
// Last line should be all black
for (var i = 0; i < 50; ++i)
success = success && checkPixel(i, 49, 0, 0, 0, 255);
if (success)
pass();
gl.disableVertexAttribArray(positionLocation);
gl.disableVertexAttribArray(colorLocation);
}
setBindLocations(2, 3);
checkDraw(2, 3, 0, 255, 0, 255);
setBindLocations(0, 3);
gl.disableVertexAttribArray(0);
gl.vertexAttrib4f(0, 1, 0, 0, 1);
checkDraw(0, 3, 255, 0, 0, 255);
glErrorShouldBe(gl, gl.NO_ERROR);
debug("");
successfullyParsed = true;
</script>
<script src="../resources/js-test-post.js"></script>
</body>
</html>