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 uniform default values</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>
36 <script src="../../resources/test-eval.js"></script>
39 <div id="description"></div>
40 <div id="console"></div>
41 <canvas id="example" width="2" height="2"> </canvas>
42 <script id="vshader0" type="x-shader/x-vertex">
43 attribute vec4 vPosition;
46 gl_Position = vPosition;
49 <script id="fshader0" type="x-shader/x-fragment">
50 precision mediump float;
51 uniform $(type) u_uniform;
53 bool isZero($(type) value) {
59 gl_FragColor = isZero(u_uniform) ? vec4(0,1,0,1) : vec4(1,0,0,1);
62 <script id="vshader1" type="x-shader/x-vertex">
63 attribute vec4 vPosition;
65 uniform $(type) u_uniform;
67 bool isZero($(type) value) {
73 gl_Position = vPosition;
74 v_color = isZero(u_uniform) ? vec4(0,1,0,1) : vec4(1,0,0,1);
77 <script id="fshader1" type="x-shader/x-fragment">
78 precision mediump float;
82 gl_FragColor = v_color;
85 <script id="vshader2" type="x-shader/x-vertex">
86 attribute vec4 vPosition;
89 gl_Position = vPosition;
92 <script id="fshader2" type="x-shader/x-fragment">
93 precision mediump float;
94 uniform $(type) u_uniform[2];
96 bool isZero($(type) value) {
102 gl_FragColor = isZero(u_uniform[1]) ? vec4(0,1,0,1) : vec4(1,0,0,1);
105 <script id="vshader3" type="x-shader/x-vertex">
106 attribute vec4 vPosition;
107 varying vec4 v_color;
108 uniform $(type) u_uniform[2];
110 bool isZero($(type) value) {
116 gl_Position = vPosition;
117 v_color = isZero(u_uniform[1]) ? vec4(0,1,0,1) : vec4(1,0,0,1);
120 <script id="fshader3" type="x-shader/x-fragment">
121 precision mediump float;
122 varying vec4 v_color;
125 gl_FragColor = v_color;
134 check: "return value == 0.0",
135 setFn: function(gl, loc) { gl.uniform1f(loc, 3.0); }
138 check: "return value == 0",
139 setFn: function(gl, loc) { gl.uniform1i(loc, 3.0); }
142 check: "return value == false",
143 setFn: function(gl, loc) { gl.uniform1i(loc, 1); }
146 check: "return value[0] == 0.0 && value[1] == 0.0",
147 setFn: function(gl, loc) { gl.uniform2f(loc, 3.0, 3.0); }
150 check: "return value[0] == 0.0 && value[1] == 0.0 && value[2] == 0.0",
151 setFn: function(gl, loc) { gl.uniform3f(loc, 3.0, 3.0, 3.0); }
154 check: "return value[0] == 0.0 && value[1] == 0.0 && value[2] == 0.0 && value[3] == 0.0",
155 setFn: function(gl, loc) { gl.uniform4f(loc, 3.0, 3.0, 3.0, 3.0); }
158 check: "return value[0] == 0 && value[1] == 0",
159 setFn: function(gl, loc) { gl.uniform2i(loc, 3, 3); }
162 check: "return value[0] == 0 && value[1] == 0 && value[2] == 0",
163 setFn: function(gl, loc) { gl.uniform3i(loc, 3, 3, 3); }
166 check: "return value[0] == 0 && value[1] == 0 && value[2] == 0 && value[3] == 0",
167 setFn: function(gl, loc) { gl.uniform4i(loc, 3, 3, 3, 3); }
170 check: "return value[0] == false && value[1] == false",
171 setFn: function(gl, loc) { gl.uniform2i(loc, 1, 1); }
174 check: "return value[0] == false && value[1] == false && value[2] == false",
175 setFn: function(gl, loc) { gl.uniform3i(loc, 1, 1, 1); }
178 check: "return value[0] == false && value[1] == false && value[2] == false && value[3] == false",
179 setFn: function(gl, loc) { gl.uniform4i(loc, 1, 1, 1, 1); }
184 "value[0][0] == 0.0 && value[0][1] == 0.0 && " +
185 "value[1][0] == 0.0 && value[1][0] == 0.0",
188 "value[0] == 0.0 && value[1] == 0.0 && " +
189 "value[2] == 0.0 && value[3] == 0.0",
190 setFn: function(gl, loc) { gl.uniformMatrix2fv(loc, false, [1, 1, 1, 1]); }
195 "value[0][0] == 0.0 && value[1][0] == 0.0 && value[2][0] == 0.0 && " +
196 "value[0][1] == 0.0 && value[1][1] == 0.0 && value[2][1] == 0.0 && " +
197 "value[0][2] == 0.0 && value[1][2] == 0.0 && value[2][2] == 0.0",
200 "value[0] == 0.0 && value[1] == 0.0 && value[2] == 0.0 && " +
201 "value[3] == 0.0 && value[4] == 0.0 && value[5] == 0.0 && " +
202 "value[6] == 0.0 && value[7] == 0.0 && value[8] == 0.0",
203 setFn: function(gl, loc) { gl.uniformMatrix3fv(loc, false, [1, 1, 1, 1, 1, 1, 1, 1, 1]); }
208 "value[0][0] == 0.0 && value[1][0] == 0.0 && value[2][0] == 0.0 && value[3][0] == 0.0 && " +
209 "value[0][1] == 0.0 && value[1][1] == 0.0 && value[2][1] == 0.0 && value[3][1] == 0.0 && " +
210 "value[0][2] == 0.0 && value[1][2] == 0.0 && value[2][2] == 0.0 && value[3][2] == 0.0 && " +
211 "value[0][3] == 0.0 && value[1][3] == 0.0 && value[2][3] == 0.0 && value[3][3] == 0.0",
214 "value[ 0] == 0.0 && value[ 1] == 0.0 && value[ 2] == 0.0 && value[ 3] == 0.0 && " +
215 "value[ 4] == 0.0 && value[ 5] == 0.0 && value[ 6] == 0.0 && value[ 7] == 0.0 && " +
216 "value[ 8] == 0.0 && value[ 9] == 0.0 && value[10] == 0.0 && value[11] == 0.0 && " +
217 "value[12] == 0.0 && value[13] == 0.0 && value[14] == 0.0 && value[15] == 0.0",
218 setFn: function(gl, loc) { gl.uniformMatrix4fv(loc, false, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]); }
222 "vec4 v = texture2D(value, vec2(0, 0));" +
223 "return v.x == 1.0 && v.y == 1.0 && v.z == 1.0 && v.w == 1.0",
226 setFn: function(gl, loc) { gl.uniform1i(loc, 1); }
228 { type: 'samplerCube',
230 "vec4 v = textureCube(value, vec3(0, 0, 0));" +
231 "return v.x == 1.0 && v.y == 1.0 && v.z == 1.0 && v.w == 1.0",
234 setFn: function(gl, loc) { gl.uniform1i(loc, 1); }
238 var wtu = WebGLTestUtils;
239 var gl = wtu.create3DContext();
240 var c = document.getElementById("console");
243 wtu.setupUnitQuad(gl, [0, 1]);
245 // Set unit 0 to a non-0 texture.
246 var haveVertexTextureImageUnits =
247 gl.getParameter(gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS) >= 2;
248 var tex2D = gl.createTexture();
249 var texCube = gl.createTexture();
250 gl.bindTexture(gl.TEXTURE_2D, tex2D);
251 gl.bindTexture(gl.TEXTURE_CUBE_MAP, texCube);
253 var pixel = new Uint8Array([255, 255, 255, 255]);
256 gl.TEXTURE_CUBE_MAP_POSITIVE_X,
257 gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
258 gl.TEXTURE_CUBE_MAP_POSITIVE_Y,
259 gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
260 gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
261 gl.TEXTURE_CUBE_MAP_NEGATIVE_Z
263 for (var ii = 0; ii < targets.length; ++ii) {
265 targets[ii], 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixel);
268 var shaderTemplates = [
269 { vs: "vshader0", fs: "fshader0", type: 'f' },
270 { vs: "vshader1", fs: "fshader1", type: 'v' },
271 { vs: "vshader2", fs: "fshader2", type: 'f' },
272 { vs: "vshader3", fs: "fshader3", type: 'v' },
275 // Get shader templates
276 for (var ii = 0; ii < shaderTemplates.length; ++ii) {
277 var template = shaderTemplates[ii];
278 template.vs = wtu.getScript(template.vs);
279 template.fs = wtu.getScript(template.fs);
282 function testType(test) {
284 debug("testing: " + test.type);
286 for (var ii = 0; ii < shaderTemplates.length; ++ii) {
287 var template = shaderTemplates[ii];
289 if (test.type.substring(0, 7) == "sampler" &&
290 template.type == 'v' &&
291 !haveVertexTextureImageUnits) {
295 var vs = wtu.replaceParams(template.vs, test);
296 var fs = wtu.replaceParams(template.fs, test);
298 wtu.addShaderSource(c, "vertex shader", vs);
299 wtu.addShaderSource(c, "fragment shader", fs);
301 var vs = wtu.loadShader(gl, vs, gl.VERTEX_SHADER);
302 var fs = wtu.loadShader(gl, fs, gl.FRAGMENT_SHADER);
303 var program = wtu.createProgram(gl, vs, fs);
305 gl.useProgram(program);
307 var loc = gl.getUniformLocation(program, "u_uniform[1]");
309 var loc = gl.getUniformLocation(program, "u_uniform");
312 var value = gl.getUniform(program, loc);
313 TestEval("checkFn = function(value) {" + (test.valueCheck ? test.valueCheck : test.check) + ";}");
314 if (checkFn(value)) {
315 testPassed("uniform is zero");
317 testFailed("uniform is not zero");
320 debug("default value should be zero");
321 wtu.clearAndDrawUnitQuad(gl);
322 wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green", 0);
324 debug("test test by setting value");
327 wtu.clearAndDrawUnitQuad(gl);
328 wtu.checkCanvas(gl, [255, 0, 0, 255], "should be red", 0);
330 debug("re-linking should reset to defaults");
331 gl.linkProgram(program);
333 wtu.clearAndDrawUnitQuad(gl);
334 wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green", 0);
336 gl.deleteProgram(program);
340 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no GL errors");
345 function runNextTest() {
346 testType(tests[testNdx++]);
347 if (testNdx >= tests.length) {
350 setTimeout(runNextTest, 0);
356 var successfullyParsed = true;