vector-dynamic-indexing.html (9628B)
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>GLSL dynamic vector and matrix indexing test</title> 12 <link rel="stylesheet" href="../../resources/js-test-style.css"/> 13 <script src="../../js/js-test-pre.js"></script> 14 <script src="../../js/webgl-test-utils.js"></script> 15 <script src="../../js/glsl-conformance-test.js"></script> 16 </head> 17 <body> 18 <div id="description"></div> 19 <div id="console"></div> 20 <script id="fshaderIndexMatrixTwice" type="x-shader/x-fragment">#version 300 es 21 precision mediump float; 22 23 out vec4 my_FragColor; 24 25 uniform int u_zero; 26 27 void main() { 28 mat2 m = mat2(0.0, 0.0, 0.0, 1.0); 29 float f = m[u_zero + 1][u_zero + 1]; 30 my_FragColor = vec4(1.0 - f, f, 0.0, 1.0); 31 } 32 </script> 33 <script id="fshaderIndexWithValueFromIndexingExpression" type="x-shader/x-fragment">#version 300 es 34 precision mediump float; 35 36 out vec4 my_FragColor; 37 38 uniform int u_zero; 39 40 void main() { 41 ivec2 i = ivec2(0, 2); 42 vec4 v = vec4(0.0, 0.2, 1.0, 0.4); 43 float f = v[i[u_zero + 1]]; 44 my_FragColor = vec4(1.0 - f, f, 0.0, 1.0); 45 } 46 </script> 47 <script id="fshaderIndexLValue" type="x-shader/x-fragment">#version 300 es 48 precision mediump float; 49 50 out vec4 my_FragColor; 51 52 uniform int u_zero; 53 54 void main() { 55 vec4 v = vec4(1.0, 2.0, 3.0, 4.0); 56 v[u_zero + 1] = 5.0; 57 vec4 expected = vec4(1.0, 5.0, 3.0, 4.0); 58 float f = 1.0 - distance(v, expected); 59 my_FragColor = vec4(1.0 - f, f, 0.0, 1.0); 60 } 61 </script> 62 <script id="fshaderIndexLValueWithValueFromIndexingExpression" type="x-shader/x-fragment">#version 300 es 63 precision mediump float; 64 65 out vec4 my_FragColor; 66 67 uniform int u_zero; 68 69 void main() { 70 ivec2 i = ivec2(0, 2); 71 vec4 v = vec4(1.0, 2.0, 3.0, 4.0); 72 v[i[u_zero + 1]] = 5.0; 73 vec4 expected = vec4(1.0, 2.0, 5.0, 4.0); 74 float f = 1.0 - distance(v, expected); 75 my_FragColor = vec4(1.0 - f, f, 0.0, 1.0); 76 } 77 </script> 78 <script id="fshaderIndexBuiltInFunctionCallOutParameter" type="x-shader/x-fragment">#version 300 es 79 precision mediump float; 80 81 out vec4 my_FragColor; 82 83 uniform int u_zero; 84 85 void main() { 86 vec4 v = vec4(1.0, 2.0, 3.0, 4.0); 87 modf(5.5, v[u_zero + 3]); 88 vec4 expected = vec4(1.0, 2.0, 3.0, 5.0); 89 float f = 1.0 - distance(v, expected); 90 my_FragColor = vec4(1.0 - f, f, 0.0, 1.0); 91 } 92 </script> 93 <script id="fshaderIndexUserDefinedFunctionCallOutParameter" type="x-shader/x-fragment">#version 300 es 94 precision mediump float; 95 96 out vec4 my_FragColor; 97 98 uniform int u_zero; 99 100 void foo(out float f) { 101 modf(5.5, f); 102 } 103 104 void main() { 105 vec4 v = vec4(1.0, 2.0, 3.0, 4.0); 106 foo(v[u_zero + 3]); 107 vec4 expected = vec4(1.0, 2.0, 3.0, 5.0); 108 float f = 1.0 - distance(v, expected); 109 my_FragColor = vec4(1.0 - f, f, 0.0, 1.0); 110 } 111 </script> 112 <script id="fshaderIndexUserDefinedFunctionCallInOutParameter" type="x-shader/x-fragment">#version 300 es 113 precision mediump float; 114 115 out vec4 my_FragColor; 116 117 uniform int u_zero; 118 119 void foo(inout float f) { 120 float g = f + 2.5; 121 modf(g, f); 122 } 123 124 void main() { 125 vec4 v = vec4(1.0, 2.0, 3.0, 4.0); 126 foo(v[u_zero + 2]); 127 vec4 expected = vec4(1.0, 2.0, 5.0, 4.0); 128 float f = 1.0 - distance(v, expected); 129 my_FragColor = vec4(1.0 - f, f, 0.0, 1.0); 130 } 131 </script> 132 <script id="fshaderIndexWithSideEffects" type="x-shader/x-fragment">#version 300 es 133 precision mediump float; 134 135 out vec4 my_FragColor; 136 137 uniform int u_zero; 138 139 int sideEffectCounter = 0; 140 141 int funcWithSideEffects() { 142 sideEffectCounter++; 143 return 2; 144 } 145 146 void main() { 147 vec4 v = vec4(1.0, 2.0, 3.0, 4.0); 148 v[funcWithSideEffects()] = 5.0; 149 vec4 expected = vec4(1.0, 2.0, 5.0, 4.0); 150 float f = 1.0 - distance(v, expected); 151 if (sideEffectCounter != 1) { 152 f = 0.0; 153 } 154 my_FragColor = vec4(1.0 - f, f, 0.0, 1.0); 155 } 156 </script> 157 <script id="fshaderIndexInOutWithSideEffects" type="x-shader/x-fragment">#version 300 es 158 precision mediump float; 159 160 out vec4 my_FragColor; 161 162 uniform int u_zero; 163 164 int sideEffectCounter = 0; 165 166 int funcWithSideEffects() { 167 sideEffectCounter++; 168 return 2; 169 } 170 171 void main() { 172 vec4 v = vec4(1.0, 2.0, 3.0, 4.0); 173 v[funcWithSideEffects()]++; 174 vec4 expected = vec4(1.0, 2.0, 4.0, 4.0); 175 float f = 1.0 - distance(v, expected); 176 if (sideEffectCounter != 1) { 177 f = 0.0; 178 } 179 my_FragColor = vec4(1.0 - f, f, 0.0, 1.0); 180 } 181 </script> 182 <script id="fshaderIndexUserDefinedFunctionCallInOutParameterWithIndexWithSideEffects" type="x-shader/x-fragment">#version 300 es 183 precision mediump float; 184 185 out vec4 my_FragColor; 186 187 uniform int u_zero; 188 189 int sideEffectCounter = 0; 190 191 void foo(inout float f) { 192 float g = f + 2.5; 193 modf(g, f); 194 } 195 196 int funcWithSideEffects() { 197 sideEffectCounter++; 198 return 2; 199 } 200 201 void main() { 202 vec4 v = vec4(1.0, 2.0, 3.0, 4.0); 203 foo(v[funcWithSideEffects()]); 204 vec4 expected = vec4(1.0, 2.0, 5.0, 4.0); 205 float f = 1.0 - distance(v, expected); 206 if (sideEffectCounter != 1) { 207 f = 0.0; 208 } 209 my_FragColor = vec4(1.0 - f, f, 0.0, 1.0); 210 } 211 </script> 212 <script id="fshaderIndexLValueWithUint" type="x-shader/x-fragment">#version 300 es 213 precision mediump float; 214 215 out vec4 my_FragColor; 216 217 uniform uint u_zero; 218 219 void main() { 220 vec4 v = vec4(1.0, 2.0, 3.0, 4.0); 221 v[u_zero] = 5.0; 222 vec4 expected = vec4(5.0, 2.0, 3.0, 4.0); 223 float f = 1.0 - distance(v, expected); 224 my_FragColor = vec4(1.0 - f, f, 0.0, 1.0); 225 } 226 </script> 227 <script id="fshaderIndexUniform" type="x-shader/x-fragment">#version 300 es 228 precision mediump float; 229 230 out vec4 my_FragColor; 231 232 uniform vec4 u_zeroVec; 233 uniform uint u_zero; 234 235 void main() { 236 // This test is just to catch a crash bug that occurred in ANGLE's workaround. 237 // Rendering result is not meaningful. 238 float f = u_zeroVec[u_zero]; 239 my_FragColor = vec4(f, 1.0, 0.0, 1.0); 240 } 241 </script> 242 <script id="fshaderSequenceDynamicIndexingVectorLvalue" type="x-shader/x-fragment">#version 300 es 243 precision mediump float; 244 245 out vec4 my_FragColor; 246 uniform int u_zero; 247 248 int sideEffectCounter = 0; 249 float func() { 250 ++sideEffectCounter; 251 return -1.0; 252 } 253 254 void main() { 255 vec4 v = vec4(0.0, 2.0, 4.0, 6.0); 256 float f = (func(), (++v[u_zero + sideEffectCounter])); 257 my_FragColor = (abs(f - 3.0) < 0.01 && abs(v[1] - 3.0) < 0.01 && sideEffectCounter == 1) ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1); 258 } 259 </script> 260 <script id="fshaderIndexMatrixTwiceInLValue" type="x-shader/x-fragment">#version 300 es 261 precision mediump float; 262 263 out vec4 my_FragColor; 264 265 uniform int u_zero; 266 267 void main() { 268 mat2 m = mat2(0.0, 0.0, 0.0, 0.0); 269 m[u_zero + 1][u_zero + 1] = float(u_zero + 1); 270 float f = m[1][1]; 271 my_FragColor = vec4(1.0 - f, f, 0.0, 1.0); 272 } 273 </script> 274 <script type="application/javascript"> 275 "use strict"; 276 description("Dynamic indexing of vectors and matrices should work."); 277 278 debug("Dynamic indexing of vectors and matrices requires complex workarounds on HLSL backends. Try to test possible bugs those workarounds might have."); 279 280 GLSLConformanceTester.runRenderTests([ 281 { 282 fShaderId: 'fshaderIndexMatrixTwice', 283 fShaderSuccess: true, 284 linkSuccess: true, 285 passMsg: 'Index matrix and then index the resulting vector in the same expression' 286 }, 287 { 288 fShaderId: 'fshaderIndexWithValueFromIndexingExpression', 289 fShaderSuccess: true, 290 linkSuccess: true, 291 passMsg: 'Index a vector with an index that is the result of indexing' 292 }, 293 { 294 fShaderId: 'fshaderIndexLValue', 295 fShaderSuccess: true, 296 linkSuccess: true, 297 passMsg: 'Index on the left-hand side of assignment' 298 }, 299 { 300 fShaderId: 'fshaderIndexLValueWithValueFromIndexingExpression', 301 fShaderSuccess: true, 302 linkSuccess: true, 303 passMsg: 'Index on the left-hand side of assignment with an index that is the result of indexing' 304 }, 305 { 306 fShaderId: 'fshaderIndexBuiltInFunctionCallOutParameter', 307 fShaderSuccess: true, 308 linkSuccess: true, 309 passMsg: 'Index the out parameter passed to built-in modf' 310 }, 311 { 312 fShaderId: 'fshaderIndexUserDefinedFunctionCallOutParameter', 313 fShaderSuccess: true, 314 linkSuccess: true, 315 passMsg: 'Index an out parameter passed to an user-defined function' 316 }, 317 { 318 fShaderId: 'fshaderIndexUserDefinedFunctionCallInOutParameter', 319 fShaderSuccess: true, 320 linkSuccess: true, 321 passMsg: 'Index an inout parameter passed to an user-defined function' 322 }, 323 { 324 fShaderId: 'fshaderIndexWithSideEffects', 325 fShaderSuccess: true, 326 linkSuccess: true, 327 passMsg: 'Use expression with side effects as an index of an l-value' 328 }, 329 { 330 fShaderId: 'fshaderIndexInOutWithSideEffects', 331 fShaderSuccess: true, 332 linkSuccess: true, 333 passMsg: 'Use expression with side effects as an index of an l-value that is both read and written' 334 }, 335 { 336 fShaderId: 'fshaderIndexUserDefinedFunctionCallInOutParameterWithIndexWithSideEffects', 337 fShaderSuccess: true, 338 linkSuccess: true, 339 passMsg: 'Index an inout parameter passed to an user-defined function with an index with side effects' 340 }, 341 { 342 fShaderId: 'fshaderIndexLValueWithUint', 343 fShaderSuccess: true, 344 linkSuccess: true, 345 passMsg: 'Index on the left-hand side of assignment with an uint' 346 }, 347 { 348 fShaderId: 'fshaderIndexUniform', 349 fShaderSuccess: true, 350 linkSuccess: true, 351 passMsg: 'Index a uniform with a uniform' 352 }, 353 { 354 fShaderId: 'fshaderSequenceDynamicIndexingVectorLvalue', 355 fShaderSuccess: true, 356 linkSuccess: true, 357 passMsg: 'Sequence operator with dynamic indexing of a vector as an l-value inside' 358 }, 359 { 360 fShaderId: 'fshaderIndexMatrixTwiceInLValue', 361 fShaderSuccess: true, 362 linkSuccess: true, 363 passMsg: 'Index matrix and then index the resulting vector in the same expression inside an l-value' 364 } 365 ], 2); 366 </script> 367 </body> 368 </html>