Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / webgl / src / sdk / tests / conformance / glsl / misc / shader-with-short-circuiting-operators.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 short-circuit evaluation</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     </head>
37   <body>
38     <div id="description"></div>
39     <div id="console"></div>
40     <canvas id="example" width="2" height="2"> </canvas>
41
42     <!-------------------------------------
43   WebGL Shaders
44 ---------------------------------------->
45     <!-- Pass through Shaders -->
46     <script id="vshader0" type="x-shader/x-vertex">
47       /* PASS-THROUGH VERTEX SHADER */
48       attribute vec4 vPosition;
49
50       void main()
51       {
52         gl_Position = vPosition;
53       }
54     </script>
55
56     <script id="fshader0" type="x-shader/x-fragment">
57       /* PASS-THROUGH FRAGMENT SHADER */
58       precision mediump float;
59       varying vec4 vPassThrough;
60
61       void main()
62       {
63         gl_FragColor = vPassThrough;
64       }
65     </script>
66
67     <!-- basic conditonal short circuit Shaders -->
68     <script id="vshader1" type="x-shader/x-vertex">
69       attribute vec4 vPosition;
70       varying vec4 vPassThrough;
71
72       void main()
73       {
74         int x = 1;
75         $(variables)
76       
77         if ($(condition))
78         { /*do nothing*/ }
79
80         /* if x was unmodified return green, else return red */
81         vPassThrough = (x == 1) ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1);
82         gl_Position = vPosition;
83       }
84     </script>
85
86     <script id="fshader1" type="x-shader/x-fragment">
87       precision mediump float;
88
89       void main()
90       {
91         int x = 1;
92         $(variables)
93         
94         if ($(condition))
95         { /*do nothing*/ }
96
97         gl_FragColor = (x == 1) ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1);
98       }
99     </script>
100
101     <!-- Main body of the Webgl program -->
102     <script>
103     "use strict";
104       description();
105
106       var wtu = WebGLTestUtils;
107       var gl = wtu.create3DContext();
108
109       wtu.setupUnitQuad(gl, [0, 1]);
110
111       var shaderTemplates = [
112       { vs: "vshader1", fs: "fshader0" }, // basic vertex short-circuit test
113       { vs: "vshader0", fs: "fshader1" }, // basic fragment short-circuit test
114       ];
115
116       /* replace the names of the shaders in the tempate variables with
117       * the shaders themselves */
118       for (var ii = 0; ii < shaderTemplates.length; ++ii) {
119         var template = shaderTemplates[ii];
120         template.vs = wtu.getScript(template.vs);
121         template.fs = wtu.getScript(template.fs);
122       }
123
124       /* define the conditon that will be used in the shaders. If additional
125        * variables are needed that are not present i the shader they my be
126        * defined in the variables variable */
127       var tests = [
128         { condition: "true || (x = 0) == 1", variables: "" }, /* test basic 'or' short circuit */
129         { condition: "false && (x = 0) == 1", variables: "" }, /* test basic 'and' short circuit */
130         { condition: "(j == 3 && j == k) || (j > (x = 0))", variables: "int j = 3;\nint k = 3;" }, /* test basic 'or' short circuit with actual condition */
131         { condition: "(j == 3 && j == k) && (j > (x = 0))", variables: "int j = 3;\nint k = 4;" }, /* test basic 'and' short circuit with actual condition */
132         { condition: "(j + 3 > k && ((j < 10) || (x + 5 > j + (x = 0))) || ( x = 0 ) == 7)", variables: "int j = 5;\nint k = 3;" }, /* complex test */
133         { condition: "j + 1 == 6 ? x == 1 || j > (x = 0) : (x = 0) == 1 && (x = 0) <= 1", variables: "int j = 5;" }, /* nested with ternary operator */
134         { condition: "true && (true || (x = 0) == 1)", variables: "" }, /* test unfold short circuit update order correctness */
135       ];
136
137       function testShortCircuit(test) {
138         debug("");
139         debug("testing short circuit condition: " + test.condition);
140
141         /* Setting clear color to blue */
142         gl.clearColor(0.0, 0.0, 1.0, 1.0);
143
144         for (var ii = 0; ii < shaderTemplates.length; ++ii) {
145
146           /* clear the screen so that subsequent tests don't conflict */
147           gl.clear(gl.COLOR_BUFFER_BIT);
148           var template = shaderTemplates[ii];
149
150           var vs = wtu.replaceParams(template.vs, test);
151           var fs = wtu.replaceParams(template.fs, test);
152
153           var program = wtu.setupProgram(gl, [vs, fs], ['vPosition'], undefined, true);
154
155           wtu.clearAndDrawUnitQuad(gl);
156           wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green", 0);
157
158           gl.deleteProgram(program);
159
160           wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no GL errors");
161         }
162       }
163
164       var testNdx = 0;
165       function runNextTest() {
166           testShortCircuit(tests[testNdx++]);
167           if (testNdx >= tests.length) {
168               finishTest();
169           } else {
170               setTimeout(runNextTest, 0);
171           }
172       }
173
174       runNextTest();
175
176       var successfullyParsed = true;
177
178     </script>
179   </body>
180 </html>