summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/glsl/misc/large-loop-compile.html
blob: f264b08a4ce6ea3060a1692997fe3188882c3edc (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<!--

/*
** Copyright (c) 2012 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/

-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL GLSL Conformance Tests</title>
<link rel="stylesheet" href="../../../resources/js-test-style.css"/>
<link rel="stylesheet" href="../../../resources/glsl-feature-tests.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>
<script id="vertexShader" type="text/something-not-javascript">
attribute vec2 position;

void main(){
    gl_Position = vec4(position, 0.0, 1.0);
}
</script>
<script id="fragmentShader" type="text/something-not-javascript">
precision mediump float;
uniform sampler2D source;

mat3 front = mat3(
     1.0,  0.0,  0.0,
     0.0,  1.0,  0.0,
     0.0,  0.0,  1.0
);

mat3 back = mat3(
    -1.0,  0.0,  0.0,
     0.0,  1.0,  0.0,
     0.0,  0.0, -1.0
);

mat3 left = mat3(
     0.0,  0.0, -1.0,
     0.0,  1.0,  0.0,
     1.0,  0.0,  0.0
);

mat3 right = mat3(
     0.0,  0.0,  1.0,
     0.0,  1.0,  0.0,
    -1.0,  0.0,  0.0
);

mat3 up = mat3(
     1.0,  0.0,  0.0,
     0.0,  0.0,  1.0,
     0.0, -1.0,  0.0
);

mat3 down = mat3(
     1.0,  0.0,  0.0,
     0.0,  0.0, -1.0,
     0.0,  1.0,  0.0
);

float coefficient(vec3 normal){
    int index = int(gl_FragCoord.x);
    float x = normal.x;
    float y = normal.y;
    float z = normal.z;

    if(index==0){
        return 1.0;
    }
    else if(index==1){
        return y;
    }
    else if(index==2){
        return z;
    }
    else if(index==3){
        return x;
    }
    else if(index==4){
        return x*y;
    }
    else if(index==5){
        return y*z;
    }
    else if(index==6){
        return 3.0*z*z - 1.0;
    }
    else if(index==7){
        return x*z;
    }
    else{
        return x*x - y*y;
    }
}

vec3 sample(float cidx, mat3 side){
    vec3 result = vec3(0.0);
    float divider = 0.0;

    for(int i=0; i<256; i++){
        float x = mod(float(i), 16.0);
        float y = float(i/16);
        vec2 texcoord = (vec2(x+cidx*16.0, y+floor(gl_FragCoord.y)*16.0)+0.5)/6.0;
        vec2 sidecoord = ((vec2(x,y)+vec2(0.5, 0.5))/vec2(16.0))*2.0-1.0;
        vec3 normal = normalize(vec3(sidecoord, -1.0));
        vec3 texel = texture2D(source, texcoord).rgb;
        result += coefficient(side*normal) * texel * -normal.z;
        divider += -normal.z;
    }
    return result/divider;
}

void main(){
    vec3 result = (
        //sample(0.0, front) +
        //sample(1.0, back) +
        sample(2.0, left) +
        sample(3.0, right) +
        sample(4.0, up) +
        sample(5.0, down)
    )/6.0;
    gl_FragColor = vec4(result, 1.0);
}
</script>
<script>
"use strict";
var receivedContextLost = false;
description("Ensures that compilation of a large loop completes in a reasonable period of time and does not cause the WebGL context to be lost");
var wtu = WebGLTestUtils;
var canvas = document.createElement('canvas');
canvas.width = 32;
canvas.height = 32;
canvas.addEventListener("webglcontextlost", function(e) {
  testFailed("context was lost during shader compilation or linking");
  receivedContextLost = true;
});
var gl = wtu.create3DContext(canvas);
if (!gl) {
  testFailed("context does not exist");
  finishTest();
} else {
  var startTime = Date.now();
  wtu.setupProgram(gl, ["vertexShader", "fragmentShader"], undefined, undefined, true);
  gl.clearColor(0.0, 1.0, 0.0, 1.0);
  gl.clear(gl.COLOR_BUFFER_BIT);
  wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green", 0);
  var endTime = Date.now();

  // Delay for some period to increase chances that context lost event will be delivered.
  setTimeout(function() {
    if (!receivedContextLost) {
      testPassed("Large loop compiled and linked without terminating the WebGL context");
      if (endTime - startTime < 5000) {
        testPassed("Shader compilation completed in a reasonable amount of time");
      } else {
        testFailed("Shader compilation took an unreasonably long time");
      }
    }
    finishTest();
  }, 500);
}
var successfullyParsed = true;
</script>
</body>
</html>