summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/glsl/misc/shader-with-short-circuiting-operators.html
blob: c05649aa839994d05f932fb792d9bad8e290d975 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL short-circuit evaluation</title>
<link rel="stylesheet" href="../../../resources/js-test-style.css"/>
<script src="../../../js/js-test-pre.js"></script>
<script src="../../../js/webgl-test-utils.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<canvas id="example" width="2" height="2"> </canvas>

<!-------------------------------------
  WebGL Shaders
---------------------------------------->
<!-- Pass through Shaders -->
<script id="vshader0" type="x-shader/x-vertex">
/* PASS-THROUGH VERTEX SHADER */
attribute vec4 vPosition;

void main()
{
    gl_Position = vPosition;
}
</script>

<script id="fshader0" type="x-shader/x-fragment">
/* PASS-THROUGH FRAGMENT SHADER */
precision mediump float;
varying vec4 vPassThrough;

void main()
{
    gl_FragColor = vPassThrough;
}
</script>

<!-- basic conditonal short circuit Shaders -->
<script id="vshader1" type="x-shader/x-vertex">
attribute vec4 vPosition;
varying vec4 vPassThrough;

void main()
{
    int x = 1;
    $(variables)

    if ($(condition))
    { /*do nothing*/ }

    /* if x was unmodified return green, else return red */
    vPassThrough = (x == 1) ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1);
    gl_Position = vPosition;
}
</script>

<script id="fshader1" type="x-shader/x-fragment">
precision mediump float;

void main()
{
    int x = 1;
    $(variables)

    if ($(condition))
    { /*do nothing*/ }

    gl_FragColor = (x == 1) ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1);
}
</script>

<!-- Main body of the Webgl program -->
<script>
"use strict";
description();

var wtu = WebGLTestUtils;
var gl = wtu.create3DContext();

wtu.setupUnitQuad(gl, [0, 1]);

var shaderTemplates = [
    { vs: "vshader1", fs: "fshader0" }, // basic vertex short-circuit test
    { vs: "vshader0", fs: "fshader1" }, // basic fragment short-circuit test
];

/* replace the names of the shaders in the tempate variables with
 * the shaders themselves */
for (var ii = 0; ii < shaderTemplates.length; ++ii) {
    var template = shaderTemplates[ii];
    template.vs = wtu.getScript(template.vs);
    template.fs = wtu.getScript(template.fs);
}

/* define the conditon that will be used in the shaders. If additional
 * variables are needed that are not present i the shader they my be
 * defined in the variables variable */
var tests = [
    { condition: "true || (x = 0) == 1", variables: "" }, /* test basic 'or' short circuit */
    { condition: "false && (x = 0) == 1", variables: "" }, /* test basic 'and' short circuit */
    { condition: "(j == 3 && j == k) || (j > (x = 0))", variables: "int j = 3;\nint k = 3;" }, /* test basic 'or' short circuit with actual condition */
    { condition: "(j == 3 && j == k) && (j > (x = 0))", variables: "int j = 3;\nint k = 4;" }, /* test basic 'and' short circuit with actual condition */
    { condition: "(j + 3 > k && ((j < 10) || (x + 5 > j + (x = 0))) || ( x = 0 ) == 7)", variables: "int j = 5;\nint k = 3;" }, /* complex test */
    { condition: "j + 1 == 6 ? x == 1 || j > (x = 0) : (x = 0) == 1 && (x = 0) <= 1", variables: "int j = 5;" }, /* nested with ternary operator */
    { condition: "true && (true || (x = 0) == 1)", variables: "" }, /* test unfold short circuit update order correctness */
];

function testShortCircuit(test) {
    debug("");
    debug("testing short circuit condition: " + test.condition);

    /* Setting clear color to blue */
    gl.clearColor(0.0, 0.0, 1.0, 1.0);

    for (var ii = 0; ii < shaderTemplates.length; ++ii) {

        /* clear the screen so that subsequent tests don't conflict */
        gl.clear(gl.COLOR_BUFFER_BIT);
        var template = shaderTemplates[ii];

        var vs = wtu.replaceParams(template.vs, test);
        var fs = wtu.replaceParams(template.fs, test);

        var program = wtu.setupProgram(gl, [vs, fs], ['vPosition'], undefined, true);

        wtu.clearAndDrawUnitQuad(gl);
        wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green", 0);

        gl.deleteProgram(program);

        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no GL errors");
    }
}

var testNdx = 0;
function runNextTest() {
    testShortCircuit(tests[testNdx++]);
    if (testNdx >= tests.length) {
        finishTest();
    } else {
        setTimeout(runNextTest, 0);
    }
}

runNextTest();
</script>
</body>
</html>