summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/glsl/misc/large-loop-compile.html
blob: 1f096ec30f32b8f1ae85fb3bd83104dd8ff87694 (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
<!--
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 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");
      const timeString = `${endTime - startTime} ms`;
      if (endTime - startTime < 7500) {
        testPassed("Shader compilation completed in a reasonable amount of time: " + timeString);
      } else {
        testFailed("Shader compilation took an unreasonably long time: " + timeString);
      }
    }
    finishTest();
  }, 500);
}
var successfullyParsed = true;
</script>
</body>
</html>