1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
2 "http://www.w3.org/TR/html4/loose.dtd">
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6 <title>WebGL uniform array Conformance Tests</title>
7 <link rel="stylesheet" href="../../js/resources/js-test-style.css"/>
8 <script src="../../js/resources/js-test-pre.js"></script>
9 <script src="resources/webgl-test.js"></script>
12 <div id="description"></div>
13 <div id="console"></div>
14 <canvas id="example" width="2" height="2"> </canvas>
15 <script id="vshader" type="x-shader/x-vertex">
16 attribute vec4 vPosition;
19 gl_Position = vPosition;
23 <script id="fshader" type="x-shader/x-fragment">
24 uniform $type color[3];
27 gl_FragColor = vec4(color[0]$elem, color[1]$elem, color[2]$elem, 1);
31 function loadShader(ctx, shaderType, shaderSource) {
32 // Create the shader object
33 var shader = ctx.createShader(shaderType);
35 debug("*** Error: unable to create shader '"+shader+"'");
39 // Load the shader source
40 ctx.shaderSource(shader, shaderSource);
43 ctx.compileShader(shader);
45 // Check the compile status
46 var compiled = ctx.getShaderParameter(shader, ctx.COMPILE_STATUS);
48 // Something went wrong during compilation; get the error
49 var error = ctx.getShaderInfoLog(shader);
50 debug("*** Error compiling shader '"+shader+"':"+error);
51 ctx.deleteShader(shader);
58 function loadProgram(ctx, vertexShaderSrc, fragmentShaderSrc) {
59 var program = ctx.createProgram();
60 var vShader = loadShader(ctx, ctx.VERTEX_SHADER, vertexShaderSrc)
61 var fShader = loadShader(ctx, ctx.FRAGMENT_SHADER, fragmentShaderSrc);
62 ctx.attachShader(program, vShader);
63 ctx.attachShader(program, fShader);
64 ctx.linkProgram(program);
65 var linked = ctx.getProgramParameter(program, ctx.LINK_STATUS);
67 // something went wrong with the link
68 var error = ctx.getProgramInfoLog (ctx.program);
69 debug("Error in program linking:" + error);
70 ctx.deleteProgram(ctx.program);
73 // ctx.deleteShader(fShader);
74 // ctx.deleteShader(vShader);
78 description("This test ensures WebGL implementations handle uniform arrays correctly.");
82 var gl = create3DContext(document.getElementById("example"));
84 var vSrc = document.getElementById("vshader").text;
85 var fTemplate = document.getElementById("fshader").text;
93 badSet: function(loc) {
94 gl.uniform2fv(loc, [1, 2]);
96 srcValueAsString: function(index, srcValues) {
97 return srcValues[index].toString();
99 returnValueAsString: function(value) {
100 return value === null ? 'null' : value.toString();
102 checkType: function(value) {
103 return typeof values === 'number';
105 checkValue: function(typeInfo, index, value) {
106 return typeInfo.srcValues[index] == value;
108 srcValues: [16, 15, 14]
111 jsTypeOf: 'Float32Array',
112 setter: 'uniform2fv',
115 badSet: function(loc) {
116 gl.uniform1fv(loc, [2]);
118 srcValueAsString: function(index, srcValues) {
119 return "[" + srcValues[index * 2 + 0].toString() + ", " +
120 srcValues[index * 2 + 1].toString() + "]";
122 returnValueAsString: function(value) {
123 return value === null ? 'null' : ("[" + value[0] + ", " + value[1] + "]");
125 checkType: function(value) {
126 return typeof values.length === 'number' &&
129 checkValue: function(typeInfo, index, value) {
130 return value !== null &&
131 typeInfo.srcValues[index * 2 + 0] == value[0] &&
132 typeInfo.srcValues[index * 2 + 1] == value[1];
134 srcValues: [16, 15, 14, 13, 12, 11],
137 jsTypeOf: 'Float32Array',
138 setter: 'uniform3fv',
141 badSet: function(loc) {
142 gl.uniform1fv(loc, [2]);
144 srcValueAsString: function(index, srcValues) {
145 return "[" + srcValues[index * 3 + 0].toString() + ", " +
146 srcValues[index * 3 + 1].toString() + ", " +
147 srcValues[index * 3 + 2].toString() + "]";
149 returnValueAsString: function(value) {
150 return value === null ? 'null' :
151 ("[" + value[0] + ", " + value[1] + ", " + value[2] + "]");
153 checkType: function(value) {
154 return typeof values.length === 'number' &&
157 checkValue: function(typeInfo, index, value) {
158 return value !== null &&
159 typeInfo.srcValues[index * 3 + 0] == value[0] &&
160 typeInfo.srcValues[index * 3 + 1] == value[1] &&
161 typeInfo.srcValues[index * 3 + 2] == value[2];
163 srcValues: [16, 15, 14, 13, 12, 11, 10, 11, 9],
166 jsTypeOf: 'Float32Array',
167 setter: 'uniform4fv',
170 badSet: function(loc) {
171 gl.uniform1fv(loc, [2]);
173 srcValueAsString: function(index, srcValues) {
174 return "[" + srcValues[index * 4 + 0].toString() + ", " +
175 srcValues[index * 4 + 1].toString() + ", " +
176 srcValues[index * 4 + 2].toString() + ", " +
177 srcValues[index * 4 + 3].toString() + "]";
179 returnValueAsString: function(value) {
180 return value === null ? 'null' :
181 ("[" + value[0] + ", " + value[1] +
182 ", " + value[2] + ", " + value[3] + "]");
184 checkType: function(value) {
185 return typeof values.length === 'number' &&
188 checkValue: function(typeInfo, index, value) {
189 return value !== null &&
190 typeInfo.srcValues[index * 4 + 0] == value[0] &&
191 typeInfo.srcValues[index * 4 + 1] == value[1] &&
192 typeInfo.srcValues[index * 4 + 2] == value[2] &&
193 typeInfo.srcValues[index * 4 + 3] == value[3];
195 srcValues: [16, 15, 14, 13, 12, 11, 10, 11, 9, 8, 7, 6, 5],
199 for (var tt = 0; tt < typeInfos.length; ++tt) {
200 var typeInfo = typeInfos[tt];
202 debug("check " + typeInfo.type);
203 var fSrc = fTemplate.replace(/\$type/g, typeInfo.type).
204 replace(/\$elem/g, typeInfo.elem);
205 //debug("fSrc: " + fSrc);
206 var program = loadProgram(gl, vSrc, fSrc);
208 var numUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
209 assertMsg(numUniforms == 1, "1 uniform found");
210 var info = gl.getActiveUniform(program, 0);
211 assertMsg(info.name == "color[0]",
212 "uniform name is 'color[0]' not 'color' as per OpenGL ES 2.0.24 section 2.10");
213 var loc = gl.getUniformLocation(program, "color");
214 var srcValues = typeInfo.srcValues;
216 // Try setting the value before using the program
217 gl[typeInfo.setter](loc, srcValues);
218 assertMsg(gl.getError() == gl.INVALID_OPERATION,
219 "should fail if there is no current program");
221 gl.useProgram(program);
222 gl[typeInfo.setter](loc, srcValues);
223 assertMsg(gl.getError() == gl.NO_ERROR,
224 "can set an array of uniforms with gl." + typeInfo.setter);
225 var values = gl.getUniform(program, loc);
226 assertMsg(gl.getError() == gl.NO_ERROR,
227 "can call gl.getUniform");
228 assertMsg(typeInfo.checkType(values),
229 "gl.getUniform returns the correct type.");
230 for (var ii = 0; ii < typeInfo.numSrcValues; ++ii) {
231 var elemLoc = gl.getUniformLocation(program, "color[" + ii + "]");
232 assertMsg(gl.getError() == gl.NO_ERROR,
233 "can get location of element " + ii +
234 " of array from gl.getUniformLocation");
235 var value = gl.getUniform(program, elemLoc);
236 assertMsg(gl.getError() == gl.NO_ERROR,
237 "can get value of element " + ii + " of array from gl.getUniform");
238 assertMsg(typeInfo.checkValue(typeInfo, ii, value),
239 "value put in (" + typeInfo.srcValueAsString(ii, srcValues) +
240 ") matches value pulled out (" +
241 typeInfo.returnValueAsString(value) + ")");
243 typeInfo.badSet(loc);
244 assertMsg(gl.getError() == gl.INVALID_OPERATION,
245 "using the wrong size of gl.Uniform fails");
248 assertMsg(gl.getError() == gl.NO_ERROR,
249 "can call gl.useProgram(null)");
252 successfullyParsed = true;
255 <script src="../../js/resources/js-test-post.js"></script>