Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / webgl / src / sdk / tests / conformance / uniforms / uniform-default-values.html
1 <!--
2
3 /*
4 ** Copyright (c) 2012 The Khronos Group Inc.
5 **
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:
13 **
14 ** The above copyright notice and this permission notice shall be included
15 ** in all copies or substantial portions of the Materials.
16 **
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.
24 */
25
26 -->
27
28 <!DOCTYPE html>
29 <html>
30 <head>
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>
37 </head>
38 <body>
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;
44 void main()
45 {
46     gl_Position = vPosition;
47 }
48 </script>
49 <script id="fshader0" type="x-shader/x-fragment">
50 precision mediump float;
51 uniform $(type) u_uniform;
52
53 bool isZero($(type) value) {
54  $(check);
55 }
56
57 void main()
58 {
59   gl_FragColor = isZero(u_uniform) ? vec4(0,1,0,1) : vec4(1,0,0,1);
60 }
61 </script>
62 <script id="vshader1" type="x-shader/x-vertex">
63 attribute vec4 vPosition;
64 varying vec4 v_color;
65 uniform $(type) u_uniform;
66
67 bool isZero($(type) value) {
68    $(check);
69 }
70
71 void main()
72 {
73     gl_Position = vPosition;
74     v_color = isZero(u_uniform) ? vec4(0,1,0,1) : vec4(1,0,0,1);
75 }
76 </script>
77 <script id="fshader1" type="x-shader/x-fragment">
78 precision mediump float;
79 varying vec4 v_color;
80 void main()
81 {
82     gl_FragColor = v_color;
83 }
84 </script>
85 <script id="vshader2" type="x-shader/x-vertex">
86 attribute vec4 vPosition;
87 void main()
88 {
89     gl_Position = vPosition;
90 }
91 </script>
92 <script id="fshader2" type="x-shader/x-fragment">
93 precision mediump float;
94 uniform $(type) u_uniform[2];
95
96 bool isZero($(type) value) {
97   $(check);
98 }
99
100 void main()
101 {
102   gl_FragColor = isZero(u_uniform[1]) ? vec4(0,1,0,1) : vec4(1,0,0,1);
103 }
104 </script>
105 <script id="vshader3" type="x-shader/x-vertex">
106 attribute vec4 vPosition;
107 varying vec4 v_color;
108 uniform $(type) u_uniform[2];
109
110 bool isZero($(type) value) {
111    $(check);
112 }
113
114 void main()
115 {
116     gl_Position = vPosition;
117     v_color = isZero(u_uniform[1]) ? vec4(0,1,0,1) : vec4(1,0,0,1);
118 }
119 </script>
120 <script id="fshader3" type="x-shader/x-fragment">
121 precision mediump float;
122 varying vec4 v_color;
123 void main()
124 {
125     gl_FragColor = v_color;
126 }
127 </script>
128 <script>
129 "use strict";
130 description();
131
132 var tests = [
133 { type: 'float',
134   check: "return value == 0.0",
135   setFn: function(gl, loc) { gl.uniform1f(loc, 3.0); }
136 },
137 { type: 'int',
138   check: "return value == 0",
139   setFn: function(gl, loc) { gl.uniform1i(loc, 3.0); }
140 },
141 { type: 'bool',
142   check: "return value == false",
143   setFn: function(gl, loc) { gl.uniform1i(loc, 1); }
144 },
145 { type: 'vec2',
146   check: "return value[0] == 0.0 && value[1] == 0.0",
147   setFn: function(gl, loc) { gl.uniform2f(loc, 3.0, 3.0); }
148 },
149 { type: 'vec3',
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); }
152 },
153 { type: 'vec4',
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); }
156 },
157 { type: 'ivec2',
158   check: "return value[0] == 0 && value[1] == 0",
159   setFn: function(gl, loc) { gl.uniform2i(loc, 3, 3); }
160 },
161 { type: 'ivec3',
162   check: "return value[0] == 0 && value[1] == 0 && value[2] == 0",
163   setFn: function(gl, loc) { gl.uniform3i(loc, 3, 3, 3); }
164 },
165 { type: 'ivec4',
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); }
168 },
169 { type: 'bvec2',
170   check: "return value[0] == false && value[1] == false",
171   setFn: function(gl, loc) { gl.uniform2i(loc, 1, 1); }
172 },
173 { type: 'bvec3',
174   check: "return value[0] == false && value[1] == false && value[2] == false",
175   setFn: function(gl, loc) { gl.uniform3i(loc, 1, 1, 1); }
176 },
177 { type: 'bvec4',
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); }
180 },
181 { type: 'mat2',
182   check:
183     "return " +
184     "value[0][0] == 0.0 && value[0][1] == 0.0 && " +
185     "value[1][0] == 0.0 && value[1][0] == 0.0",
186   valueCheck:
187     "return " +
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]); }
191 },
192 { type: 'mat3',
193   check:
194     "return " +
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",
198   valueCheck:
199     "return " +
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]); }
204 },
205 { type: 'mat4',
206   check:
207     "return " +
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",
212   valueCheck:
213     "return " +
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]); }
219 },
220 { type: 'sampler2D',
221   check:
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",
224   valueCheck:
225     "return value == 0",
226   setFn: function(gl, loc) { gl.uniform1i(loc, 1); }
227 },
228 { type: 'samplerCube',
229   check:
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",
232   valueCheck:
233     "return value == 0",
234   setFn: function(gl, loc) { gl.uniform1i(loc, 1); }
235 },
236 ];
237
238 var wtu = WebGLTestUtils;
239 var gl = wtu.create3DContext();
240 var c = document.getElementById("console");
241 var checkFn;
242
243 wtu.setupUnitQuad(gl, [0, 1]);
244
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);
252
253 var pixel = new Uint8Array([255, 255, 255, 255]);
254 var targets = [
255   gl.TEXTURE_2D,
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
262 ];
263 for (var ii = 0; ii < targets.length; ++ii) {
264     gl.texImage2D(
265         targets[ii], 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixel);
266 }
267
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' },
273 ];
274
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);
280 }
281
282 function testType(test) {
283   debug("");
284   debug("testing: " + test.type);
285
286   for (var ii = 0; ii < shaderTemplates.length; ++ii) {
287     var template = shaderTemplates[ii];
288
289     if (test.type.substring(0, 7) == "sampler" &&
290         template.type == 'v' &&
291         !haveVertexTextureImageUnits) {
292       continue;
293     }
294
295     var vs = wtu.replaceParams(template.vs, test);
296     var fs = wtu.replaceParams(template.fs, test);
297
298     wtu.addShaderSource(c, "vertex shader", vs);
299     wtu.addShaderSource(c, "fragment shader", fs);
300
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);
304
305     gl.useProgram(program);
306
307     var loc = gl.getUniformLocation(program, "u_uniform[1]");
308     if (!loc) {
309         var loc = gl.getUniformLocation(program, "u_uniform");
310     }
311
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");
316     } else {
317         testFailed("uniform is not zero");
318     }
319
320     debug("default value should be zero");
321     wtu.clearAndDrawUnitQuad(gl);
322     wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green", 0);
323
324     debug("test test by setting value");
325     test.setFn(gl, loc);
326
327     wtu.clearAndDrawUnitQuad(gl);
328     wtu.checkCanvas(gl, [255, 0, 0, 255], "should be red", 0);
329
330     debug("re-linking should reset to defaults");
331     gl.linkProgram(program);
332
333     wtu.clearAndDrawUnitQuad(gl);
334     wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green", 0);
335
336     gl.deleteProgram(program);
337     gl.deleteShader(vs);
338     gl.deleteShader(fs);
339
340     wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no GL errors");
341   }
342 }
343
344 var testNdx = 0;
345 function runNextTest() {
346     testType(tests[testNdx++]);
347     if (testNdx >= tests.length) {
348         finishTest();
349     } else {
350         setTimeout(runNextTest, 0);
351     }
352 }
353
354 runNextTest();
355
356 var successfullyParsed = true;
357
358 </script>
359 </body>
360 </html>