large-loop-compile.html (4306B)
1 <!-- 2 Copyright (c) 2019 The Khronos Group Inc. 3 Use of this source code is governed by an MIT-style license that can be 4 found in the LICENSE.txt file. 5 --> 6 7 <!DOCTYPE html> 8 <html> 9 <head> 10 <meta charset="utf-8"> 11 <title>WebGL GLSL Conformance Tests</title> 12 <link rel="stylesheet" href="../../../resources/js-test-style.css"/> 13 <link rel="stylesheet" href="../../../resources/glsl-feature-tests.css"/> 14 <script src="../../../js/js-test-pre.js"></script> 15 <script src="../../../js/webgl-test-utils.js"></script> 16 </head> 17 <body> 18 <div id="description"></div> 19 <div id="console"></div> 20 <script id="vertexShader" type="text/something-not-javascript"> 21 attribute vec2 position; 22 23 void main(){ 24 gl_Position = vec4(position, 0.0, 1.0); 25 } 26 </script> 27 <script id="fragmentShader" type="text/something-not-javascript"> 28 precision mediump float; 29 uniform sampler2D source; 30 31 mat3 front = mat3( 32 1.0, 0.0, 0.0, 33 0.0, 1.0, 0.0, 34 0.0, 0.0, 1.0 35 ); 36 37 mat3 back = mat3( 38 -1.0, 0.0, 0.0, 39 0.0, 1.0, 0.0, 40 0.0, 0.0, -1.0 41 ); 42 43 mat3 left = mat3( 44 0.0, 0.0, -1.0, 45 0.0, 1.0, 0.0, 46 1.0, 0.0, 0.0 47 ); 48 49 mat3 right = mat3( 50 0.0, 0.0, 1.0, 51 0.0, 1.0, 0.0, 52 -1.0, 0.0, 0.0 53 ); 54 55 mat3 up = mat3( 56 1.0, 0.0, 0.0, 57 0.0, 0.0, 1.0, 58 0.0, -1.0, 0.0 59 ); 60 61 mat3 down = mat3( 62 1.0, 0.0, 0.0, 63 0.0, 0.0, -1.0, 64 0.0, 1.0, 0.0 65 ); 66 67 float coefficient(vec3 normal){ 68 int index = int(gl_FragCoord.x); 69 float x = normal.x; 70 float y = normal.y; 71 float z = normal.z; 72 73 if(index==0){ 74 return 1.0; 75 } 76 else if(index==1){ 77 return y; 78 } 79 else if(index==2){ 80 return z; 81 } 82 else if(index==3){ 83 return x; 84 } 85 else if(index==4){ 86 return x*y; 87 } 88 else if(index==5){ 89 return y*z; 90 } 91 else if(index==6){ 92 return 3.0*z*z - 1.0; 93 } 94 else if(index==7){ 95 return x*z; 96 } 97 else{ 98 return x*x - y*y; 99 } 100 } 101 102 vec3 sample(float cidx, mat3 side){ 103 vec3 result = vec3(0.0); 104 float divider = 0.0; 105 106 for(int i=0; i<256; i++){ 107 float x = mod(float(i), 16.0); 108 float y = float(i/16); 109 vec2 texcoord = (vec2(x+cidx*16.0, y+floor(gl_FragCoord.y)*16.0)+0.5)/6.0; 110 vec2 sidecoord = ((vec2(x,y)+vec2(0.5, 0.5))/vec2(16.0))*2.0-1.0; 111 vec3 normal = normalize(vec3(sidecoord, -1.0)); 112 vec3 texel = texture2D(source, texcoord).rgb; 113 result += coefficient(side*normal) * texel * -normal.z; 114 divider += -normal.z; 115 } 116 return result/divider; 117 } 118 119 void main(){ 120 vec3 result = ( 121 //sample(0.0, front) + 122 //sample(1.0, back) + 123 sample(2.0, left) + 124 sample(3.0, right) + 125 sample(4.0, up) + 126 sample(5.0, down) 127 )/6.0; 128 gl_FragColor = vec4(result, 1.0); 129 } 130 </script> 131 <script> 132 "use strict"; 133 var receivedContextLost = false; 134 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"); 135 var wtu = WebGLTestUtils; 136 var canvas = document.createElement('canvas'); 137 canvas.width = 32; 138 canvas.height = 32; 139 canvas.addEventListener("webglcontextlost", function(e) { 140 testFailed("context was lost during shader compilation or linking"); 141 receivedContextLost = true; 142 }); 143 var gl = wtu.create3DContext(canvas); 144 if (!gl) { 145 testFailed("context does not exist"); 146 finishTest(); 147 } else { 148 var startTime = Date.now(); 149 wtu.setupProgram(gl, ["vertexShader", "fragmentShader"], undefined, undefined, true); 150 gl.clearColor(0.0, 1.0, 0.0, 1.0); 151 gl.clear(gl.COLOR_BUFFER_BIT); 152 wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green", 0); 153 var endTime = Date.now(); 154 155 // Delay for some period to increase chances that context lost event will be delivered. 156 setTimeout(function() { 157 if (!receivedContextLost) { 158 testPassed("Large loop compiled and linked without terminating the WebGL context"); 159 const timeString = `${endTime - startTime} ms`; 160 if (endTime - startTime < 7500) { 161 testPassed("Shader compilation completed in a reasonable amount of time: " + timeString); 162 } else { 163 testFailed("Shader compilation took an unreasonably long time: " + timeString); 164 } 165 } 166 finishTest(); 167 }, 500); 168 } 169 var successfullyParsed = true; 170 </script> 171 </body> 172 </html>