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 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>
38 <div id="description"></div>
39 <div id="console"></div>
40 <canvas id="example" width="2" height="2"> </canvas>
42 <!-------------------------------------
44 ---------------------------------------->
45 <!-- Pass through Shaders -->
46 <script id="vshader0" type="x-shader/x-vertex">
47 /* PASS-THROUGH VERTEX SHADER */
48 attribute vec4 vPosition;
52 gl_Position = vPosition;
56 <script id="fshader0" type="x-shader/x-fragment">
57 /* PASS-THROUGH FRAGMENT SHADER */
58 precision mediump float;
59 varying vec4 vPassThrough;
63 gl_FragColor = vPassThrough;
67 <!-- basic conditonal short circuit Shaders -->
68 <script id="vshader1" type="x-shader/x-vertex">
69 attribute vec4 vPosition;
70 varying vec4 vPassThrough;
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;
86 <script id="fshader1" type="x-shader/x-fragment">
87 precision mediump float;
97 gl_FragColor = (x == 1) ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1);
101 <!-- Main body of the Webgl program -->
106 var wtu = WebGLTestUtils;
107 var gl = wtu.create3DContext();
109 wtu.setupUnitQuad(gl, [0, 1]);
111 var shaderTemplates = [
112 { vs: "vshader1", fs: "fshader0" }, // basic vertex short-circuit test
113 { vs: "vshader0", fs: "fshader1" }, // basic fragment short-circuit test
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);
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 */
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 */
137 function testShortCircuit(test) {
139 debug("testing short circuit condition: " + test.condition);
141 /* Setting clear color to blue */
142 gl.clearColor(0.0, 0.0, 1.0, 1.0);
144 for (var ii = 0; ii < shaderTemplates.length; ++ii) {
146 /* clear the screen so that subsequent tests don't conflict */
147 gl.clear(gl.COLOR_BUFFER_BIT);
148 var template = shaderTemplates[ii];
150 var vs = wtu.replaceParams(template.vs, test);
151 var fs = wtu.replaceParams(template.fs, test);
153 var program = wtu.setupProgram(gl, [vs, fs], ['vPosition'], undefined, true);
155 wtu.clearAndDrawUnitQuad(gl);
156 wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green", 0);
158 gl.deleteProgram(program);
160 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no GL errors");
165 function runNextTest() {
166 testShortCircuit(tests[testNdx++]);
167 if (testNdx >= tests.length) {
170 setTimeout(runNextTest, 0);
176 var successfullyParsed = true;