global-variable-init.html (7957B)
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>Global variable initializer restrictions</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 <script src="../../../js/glsl-conformance-test.js"></script> 17 </head> 18 <body> 19 <div id="description"></div> 20 <div id="console"></div> 21 <script id="constGlobalShader" type="x-shader/x-vertex"> 22 precision mediump float; 23 attribute vec4 aPosition; 24 varying float v; 25 26 const float c = 1.0; 27 float f = c; 28 29 void main() { 30 v = f; 31 gl_Position = aPosition; 32 } 33 </script> 34 <script id="globalShader" type="x-shader/x-vertex"> 35 precision mediump float; 36 attribute vec4 aPosition; 37 varying float v; 38 39 float c = 1.0; 40 float f = c; 41 42 void main() { 43 v = f; 44 gl_Position = aPosition; 45 } 46 </script> 47 <script id="uniformShader" type="x-shader/x-vertex"> 48 precision mediump float; 49 attribute vec4 aPosition; 50 varying float v; 51 52 uniform float u; 53 float f = u; 54 55 void main() { 56 v = f; 57 gl_Position = aPosition; 58 } 59 </script> 60 <script id="builtinFunctionShader" type="x-shader/x-vertex"> 61 precision mediump float; 62 attribute vec4 aPosition; 63 varying float v; 64 65 float c = 1.0; 66 float f = sin(c); 67 68 void main() { 69 v = f; 70 gl_Position = aPosition; 71 } 72 </script> 73 <script id="builtinTextureFunctionShader" type="x-shader/x-vertex"> 74 precision mediump float; 75 attribute vec4 aPosition; 76 varying float v; 77 78 uniform sampler2D s; 79 float f = texture2DLod(s, vec2(0.5, 0.5), 0.0).x; 80 81 void main() { 82 v = f; 83 gl_Position = aPosition; 84 } 85 </script> 86 <script id="attributeShader" type="x-shader/x-vertex"> 87 precision mediump float; 88 attribute vec4 aPosition; 89 varying float v; 90 91 float f = aPosition.x; 92 93 void main() { 94 v = f; 95 gl_Position = aPosition; 96 } 97 </script> 98 <script id="userDefinedFunctionShader" type="x-shader/x-vertex"> 99 precision mediump float; 100 attribute vec4 aPosition; 101 varying float v; 102 103 float foo() { 104 return 1.0; 105 } 106 float f = foo(); 107 108 void main() { 109 v = f; 110 gl_Position = aPosition; 111 } 112 </script> 113 <script id="varyingShader" type="x-shader/x-fragment"> 114 precision mediump float; 115 varying float v; 116 float f = v; 117 118 void main() { 119 gl_FragColor = vec4(f); 120 } 121 </script> 122 <script id="globalLValueShader" type="x-shader/x-vertex"> 123 precision mediump float; 124 attribute vec4 aPosition; 125 varying float v; 126 127 float c = 1.0; 128 float f = (c = 0.0); 129 130 void main() { 131 v = f; 132 gl_Position = aPosition; 133 } 134 </script> 135 <script id="globalLValueShader2" type="x-shader/x-vertex"> 136 precision mediump float; 137 attribute vec4 aPosition; 138 varying float v; 139 140 float c = 1.0; 141 float f = (c++); 142 143 void main() { 144 v = f; 145 gl_Position = aPosition; 146 } 147 </script> 148 <script id="globalNonConstTernary" type="x-shader/x-fragment"> 149 precision mediump float; 150 float green = 1.0; 151 float black = 0.0; 152 float f = true ? green : black; 153 154 void main() { 155 gl_FragColor = vec4(0.0, f, 0.0, 1.0); 156 } 157 </script> 158 <script id="globalUniformTernary" type="x-shader/x-fragment"> 159 precision mediump float; 160 uniform float u_zero; 161 float green = 1.0 + u_zero; 162 float f = true ? green : u_zero; 163 164 void main() { 165 gl_FragColor = vec4(0.0, f, 0.0, 1.0); 166 } 167 </script> 168 <script id="globalUniformTernary2" type="x-shader/x-fragment"> 169 precision mediump float; 170 uniform float u_zero; 171 float green = 1.0; 172 float f = (u_zero < 0.1) ? green : 0.0; 173 174 void main() { 175 gl_FragColor = vec4(0.0, f, 0.0, 1.0); 176 } 177 </script> 178 <script id="globalUniformStruct" type="x-shader/x-fragment"> 179 precision mediump float; 180 struct S { 181 float zero; 182 int one; 183 }; 184 uniform S us; 185 S s = us; 186 187 void main() { 188 float green = (s.one == 1) ? 1.0 : 0.0; 189 gl_FragColor = vec4(0.0, green, 0.0, 1.0); 190 } 191 </script> 192 <script id="builtInConstant" type="x-shader/x-fragment"> 193 precision mediump float; 194 int i = gl_MaxFragmentUniformVectors; 195 196 void main() { 197 float green = (i > 0) ? 1.0 : 0.0; 198 gl_FragColor = vec4(0.0, green, 0.0, 1.0); 199 } 200 </script> 201 <script id="builtInNonConstant" type="x-shader/x-fragment"> 202 precision mediump float; 203 vec4 v = gl_FragCoord; 204 205 void main() { 206 gl_FragColor = v; 207 } 208 </script> 209 <script type="application/javascript"> 210 "use strict"; 211 description(); 212 GLSLConformanceTester.runTests([ 213 { 214 vShaderId: "constGlobalShader", 215 vShaderSuccess: true, 216 linkSuccess: true, 217 passMsg: "A const global in a global variable initializer should be accepted by WebGL." 218 }, 219 { 220 vShaderId: "globalShader", 221 vShaderSuccess: true, 222 linkSuccess: true, 223 passMsg: "Another global in a global variable initializer should be accepted by WebGL." 224 }, 225 { 226 vShaderId: "uniformShader", 227 vShaderSuccess: true, 228 linkSuccess: true, 229 passMsg: "A uniform in a global variable initializer should be accepted by WebGL." 230 }, 231 { 232 vShaderId: "builtinFunctionShader", 233 vShaderSuccess: true, 234 linkSuccess: true, 235 passMsg: "A built-in math function in a global variable initializer should be accepted by WebGL." 236 }, 237 { 238 vShaderId: "builtinTextureFunctionShader", 239 vShaderSuccess: false, 240 linkSuccess: false, 241 passMsg: "A texture lookup function in a global variable initializer should not be accepted by WebGL." 242 }, 243 { 244 vShaderId: "attributeShader", 245 vShaderSuccess: false, 246 linkSuccess: false, 247 passMsg: "An attribute in a global variable initializer should not be accepted by WebGL." 248 }, 249 { 250 vShaderId: "userDefinedFunctionShader", 251 vShaderSuccess: false, 252 linkSuccess: false, 253 passMsg: "A user-defined function call in a global variable initializer should not be accepted by WebGL." 254 }, 255 { 256 vShaderId: "constGlobalShader", 257 vShaderSuccess: true, 258 fShaderId: "varyingShader", 259 fShaderSuccess: false, 260 linkSuccess: false, 261 passMsg: "A varying in a global variable initializer should not be accepted by WebGL." 262 }, 263 { 264 vShaderId: "globalLValueShader", 265 vShaderSuccess: false, 266 linkSuccess: false, 267 passMsg: "Another global as an l-value in a global variable initializer should not be accepted by WebGL." 268 }, 269 { 270 vShaderId: "globalLValueShader2", 271 vShaderSuccess: false, 272 linkSuccess: false, 273 passMsg: "Another global as an l-value (parameter of ++) in a global variable initializer should not be accepted by WebGL." 274 }, 275 { 276 fShaderId: "globalNonConstTernary", 277 fShaderSuccess: true, 278 linkSuccess: true, 279 render: true, 280 passMsg: "Non-const global variables as operands for a ternary operator in a global variable initializer should be accepted by WebGL." 281 }, 282 { 283 fShaderId: "globalUniformTernary", 284 fShaderSuccess: true, 285 linkSuccess: true, 286 render: true, 287 passMsg: "A uniform as the second operand for a ternary operator in a global variable initializer should be accepted by WebGL." 288 }, 289 { 290 fShaderId: "globalUniformTernary2", 291 fShaderSuccess: true, 292 linkSuccess: true, 293 render: true, 294 passMsg: "Referencing a uniform inside the first operand for a ternary operator in a global variable initializer should be accepted by WebGL." 295 }, 296 { 297 fShaderId: "globalUniformStruct", 298 fShaderSuccess: true, 299 linkSuccess: true, 300 render: true, 301 uniforms: [ 302 { name: 'us.one', functionName: 'uniform1i', value: 1 } 303 ], 304 passMsg: "A global struct initialized with a uniform struct should be accepted by WebGL." 305 }, 306 { 307 fShaderId: "builtInConstant", 308 fShaderSuccess: true, 309 linkSuccess: true, 310 render: true, 311 passMsg: "Referencing a built-in constant in a global variable initializer should be accepted by WebGL." 312 }, 313 { 314 fShaderId: "builtInNonConstant", 315 fShaderSuccess: false, 316 linkSuccess: false, 317 passMsg: "Referencing a built-in non-constant in a global variable initializer should not be accepted by WebGL." 318 } 319 ]); 320 var successfullyParsed = true; 321 </script> 322 </body> 323 </html>