shader-with-non-reserved-words.js (11520B)
1 /* 2 Copyright (c) 2022 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 async function testNonReservedWords(part, numParts) { 8 9 description(`test shaders using reserved words as identifiers compile ${part} of ${numParts}`); 10 11 const DXWords = [ 12 "Buffer", 13 "double", 14 "uint", 15 "half", 16 "dword", 17 "string", 18 "texture", 19 "pixelshader", 20 "vertexshader", 21 "switch", 22 "min16float", 23 "min10float", 24 "min16int", 25 "min12int", 26 "min16uint", 27 "vector", 28 "matrix", 29 "float2", 30 "float3", 31 "float4", 32 "float1x1", 33 "float1x2", 34 "float1x3", 35 "float1x4", 36 "float2x1", 37 "float2x2", 38 "float2x3", 39 "float2x4", 40 "float3x1", 41 "float3x2", 42 "float3x3", 43 "float3x4", 44 "float4x1", 45 "float4x2", 46 "float4x3", 47 "float4x4", 48 "int1x1", 49 "int1x2", 50 "int1x3", 51 "int1x4", 52 "int2x1", 53 "int2x2", 54 "int2x3", 55 "int2x4", 56 "int3x1", 57 "int3x2", 58 "int3x3", 59 "int3x4", 60 "int4x1", 61 "int4x2", 62 "int4x3", 63 "int4x4", 64 "double1x1", 65 "double1x2", 66 "double1x3", 67 "double1x4", 68 "double2x1", 69 "double2x2", 70 "double2x3", 71 "double2x4", 72 "double3x1", 73 "double3x2", 74 "double3x3", 75 "double3x4", 76 "double4x1", 77 "double4x2", 78 "double4x3", 79 "double4x4", 80 "abort", 81 "abs", 82 "acos", 83 "all", 84 "AllMemoryBarrier", 85 "AllMemoryBarrierWithGroupSync", 86 "any", 87 "asdouble", 88 "asfloat", 89 "asin", 90 "asint", 91 "asint", 92 "asuint", 93 "asuint", 94 "atan", 95 "atan2", 96 "ceil", 97 "clamp", 98 "clip", 99 "cos", 100 "cosh", 101 "countbits", 102 "cross", 103 "D3DCOLORtoUBYTE4", 104 "ddx", 105 "ddx_coarse", 106 "ddx_fine", 107 "ddy", 108 "ddy_coarse", 109 "ddy_fine", 110 "degrees", 111 "determinant", 112 "DeviceMemoryBarrier", 113 "DeviceMemoryBarrierWithGroupSync", 114 "distance", 115 "dot", 116 "dst", 117 "errorf", 118 "EvaluateAttributeAtCentroid", 119 "EvaluateAttributeAtSample", 120 "EvaluateAttributeSnapped", 121 "exp", 122 "exp2", 123 "f16tof32", 124 "f32tof16", 125 "faceforward", 126 "firstbithigh", 127 "firstbitlow", 128 "floor", 129 "fma", 130 "fmod", 131 "frac", 132 "frexp", 133 "fwidth", 134 "GetRenderTargetSampleCount", 135 "GetRenderTargetSamplePosition", 136 "GroupMemoryBarrier", 137 "GroupMemoryBarrierWithGroupSync", 138 "InterlockedAdd", 139 "InterlockedAnd", 140 "InterlockedCompareExchange", 141 "InterlockedCompareStore", 142 "InterlockedExchange", 143 "InterlockedMax", 144 "InterlockedMin", 145 "InterlockedOr", 146 "InterlockedXor", 147 "isfinite", 148 "isinf", 149 "isnan", 150 "ldexp", 151 "length", 152 "lerp", 153 "lit", 154 "log", 155 "log10", 156 "log2", 157 "mad", 158 "max", 159 "min", 160 "modf", 161 "msad4", 162 "mul", 163 "noise", 164 "normalize", 165 "pow", 166 "printf", 167 "Process2DQuadTessFactorsAvg", 168 "Process2DQuadTessFactorsMax", 169 "Process2DQuadTessFactorsMin", 170 "ProcessIsolineTessFactors", 171 "ProcessQuadTessFactorsAvg", 172 "ProcessQuadTessFactorsMax", 173 "ProcessQuadTessFactorsMin", 174 "ProcessTriTessFactorsAvg", 175 "ProcessTriTessFactorsMax", 176 "ProcessTriTessFactorsMin", 177 "radians", 178 "rcp", 179 "reflect", 180 "refract", 181 "reversebits", 182 "round", 183 "rsqrt", 184 "saturate", 185 "sign", 186 "sin", 187 "sincos", 188 "sinh", 189 "smoothstep", 190 "sqrt", 191 "step", 192 "tan", 193 "tanh", 194 "tex1D", 195 "tex1D", 196 "tex1Dbias", 197 "tex1Dgrad", 198 "tex1Dlod", 199 "tex1Dproj", 200 "tex2D", 201 "tex2D", 202 "tex2Dbias", 203 "tex2Dgrad", 204 "tex2Dlod", 205 "tex2Dproj", 206 "tex3D", 207 "tex3D", 208 "tex3Dbias", 209 "tex3Dgrad", 210 "tex3Dlod", 211 "tex3Dproj", 212 "texCUBE", 213 "texCUBE", 214 "texCUBEbias", 215 "texCUBEgrad", 216 "texCUBElod", 217 "texCUBEproj", 218 "transpose", 219 "trunc" 220 ]; 221 222 const GLSL_4_20_11_words = [ 223 "attribute", 224 "const", 225 "uniform", 226 "varying", 227 "coherent", 228 "volatile", 229 "restrict", 230 "readonly", 231 "writeonly", 232 "atomic_uint", 233 "layout", 234 "centroid", 235 "flat", 236 "smooth", 237 "noperspective", 238 "patch", 239 "sample", 240 "break", 241 "continue", 242 "do", 243 "for", 244 "while", 245 "switch", 246 "case", 247 "default", 248 "if", 249 "else", 250 "subroutine", 251 "in", 252 "out", 253 "inout", 254 "float", 255 "double", 256 "int", 257 "void", 258 "bool", 259 "true", 260 "false", 261 "invariant", 262 "discard", 263 "return", 264 "mat2", 265 "mat3", 266 "mat4", 267 "dmat2", 268 "dmat3", 269 "dmat4", 270 "mat2x2", 271 "mat2x3", 272 "mat2x4", 273 "dmat2x2", 274 "dmat2x3", 275 "dmat2x4", 276 "mat3x2", 277 "mat3x3", 278 "mat3x4", 279 "dmat3x2", 280 "dmat3x3", 281 "dmat3x4", 282 "mat4x2", 283 "mat4x3", 284 "mat4x4", 285 "dmat4x2", 286 "dmat4x3", 287 "dmat4x4", 288 "vec2", 289 "vec3", 290 "vec4", 291 "ivec2", 292 "ivec3", 293 "ivec4", 294 "bvec2", 295 "bvec3", 296 "bvec4", 297 "dvec2", 298 "dvec3", 299 "dvec4", 300 "uint", 301 "uvec2", 302 "uvec3", 303 "uvec4", 304 "lowp", 305 "mediump", 306 "highp", 307 "precision", 308 "sampler1D", 309 "sampler2D", 310 "sampler3D", 311 "samplerCube", 312 "sampler1DShadow", 313 "sampler2DShadow", 314 "samplerCubeShadow", 315 "sampler1DArray", 316 "sampler2DArray", 317 "sampler1DArrayShadow", 318 "sampler2DArrayShadow", 319 "isampler1D", 320 "isampler2D", 321 "isampler3D", 322 "isamplerCube", 323 "isampler1DArray", 324 "isampler2DArray", 325 "usampler1D", 326 "usampler2D", 327 "usampler3D", 328 "usamplerCube", 329 "usampler1DArray", 330 "usampler2DArray", 331 "sampler2DRect", 332 "sampler2DRectShadow", 333 "isampler2DRect", 334 "usampler2DRect", 335 "samplerBuffer", 336 "isamplerBuffer", 337 "usamplerBuffer", 338 "sampler2DMS", 339 "isampler2DMS", 340 "usampler2DMS", 341 "sampler2DMSArray", 342 "isampler2DMSArray", 343 "usampler2DMSArray", 344 "samplerCubeArray", 345 "samplerCubeArrayShadow", 346 "isamplerCubeArray", 347 "usamplerCubeArray", 348 "image1D", 349 "iimage1D", 350 "uimage1D", 351 "image2D", 352 "iimage2D", 353 "uimage2D", 354 "image3D", 355 "iimage3D", 356 "uimage3D", 357 "image2DRect", 358 "iimage2DRect", 359 "uimage2DRect", 360 "imageCube", 361 "iimageCube", 362 "uimageCube", 363 "imageBuffer", 364 "iimageBuffer", 365 "uimageBuffer", 366 "image1DArray", 367 "iimage1DArray", 368 "uimage1DArray", 369 "image2DArray", 370 "iimage2DArray", 371 "uimage2DArray", 372 "imageCubeArray", 373 "iimageCubeArray", 374 "uimageCubeArray", 375 "image2DMS", 376 "iimage2DMS", 377 "uimage2DMS", 378 "image2DMSArray", 379 "iimage2DMSArray", 380 "uimage2DMSArray", 381 "struct" 382 ]; 383 384 const GLSL_4_20_11_future_words = [ 385 "common", 386 "partition", 387 "active", 388 "asm", 389 "class", 390 "union", 391 "enum", 392 "typedef", 393 "template", 394 "this", 395 "packed", 396 "resource", 397 "goto", 398 "inline", 399 "noinline", 400 "public", 401 "static", 402 "extern", 403 "external", 404 "interface", 405 "long", 406 "short", 407 "half", 408 "fixed", 409 "unsigned", 410 "superp", 411 "input", 412 "output", 413 "hvec2", 414 "hvec3", 415 "hvec4", 416 "fvec2", 417 "fvec3", 418 "fvec4", 419 "sampler3DRect", 420 "filter", 421 "sizeof", 422 "cast", 423 "namespace", 424 "using", 425 "row_major" 426 ]; 427 428 const GLSL_1_0_17_words = [ 429 "attribute", 430 "const", 431 "uniform", 432 "varying", 433 "break", 434 "continue", 435 "do", 436 "for", 437 "while", 438 "if", 439 "else", 440 "in", 441 "out", 442 "inout", 443 "float", 444 "int", 445 "void", 446 "bool", 447 "true", 448 "false", 449 "lowp", 450 "mediump", 451 "highp", 452 "precision", 453 "invariant", 454 "discard", 455 "return", 456 "mat2", 457 "mat3", 458 "mat4", 459 "vec2", 460 "vec3", 461 "vec4", 462 "ivec2", 463 "ivec3", 464 "ivec4", 465 "bvec2", 466 "bvec3", 467 "bvec4", 468 "sampler2D", 469 "samplerCube", 470 "struct" 471 ] 472 473 const GLSL_1_0_17_FutureWords = [ 474 "asm", 475 "class", 476 "union", 477 "enum", 478 "typedef", 479 "template", 480 "this", 481 "packed", 482 "goto", 483 "switch", 484 "default", 485 "inline", 486 "noinline", 487 "volatile", 488 "public", 489 "static", 490 "extern", 491 "external", 492 "interface", 493 "flat", 494 "long", 495 "short", 496 "double", 497 "half", 498 "fixed", 499 "unsigned", 500 "superp", 501 "input", 502 "output", 503 "hvec2", 504 "hvec3", 505 "hvec4", 506 "dvec2", 507 "dvec3", 508 "dvec4", 509 "fvec2", 510 "fvec3", 511 "fvec4", 512 "sampler1D", 513 "sampler3D", 514 "sampler1DShadow", 515 "sampler2DShadow", 516 "sampler2DRect", 517 "sampler3DRect", 518 "sampler2DRectShadow", 519 "sizeof", 520 "cast", 521 "namespace", 522 "using" 523 ]; 524 525 const allBadWords = [ 526 ...DXWords, 527 ...GLSL_4_20_11_words, 528 ...GLSL_4_20_11_future_words, 529 ]; 530 const numWordsPerPart = Math.ceil(allBadWords.length / numParts); 531 const firstWordNdx = numWordsPerPart * (part - 1); 532 const badWords = allBadWords.slice(firstWordNdx, firstWordNdx + numWordsPerPart); 533 debug(`running tests for words ${firstWordNdx} to ${firstWordNdx + badWords.length - 1} of ${allBadWords.length}`); 534 535 const shaders = { 536 vertexShader0: ` 537 struct $replaceMe { 538 vec4 $replaceMe; 539 }; 540 struct Foo { 541 $replaceMe $replaceMe; 542 }; 543 attribute vec4 position; 544 void main() 545 { 546 Foo f; 547 f.$replaceMe.$replaceMe = position; 548 gl_Position = f.$replaceMe.$replaceMe; 549 } 550 `, 551 fragmentShader0: ` 552 precision mediump float; 553 vec4 $replaceMe() { 554 return vec4(0,1,0,1); 555 } 556 void main() 557 { 558 gl_FragColor = $replaceMe(); 559 } 560 `, 561 vertexShader1: ` 562 attribute vec4 $replaceMe; 563 void main() 564 { 565 gl_Position = $replaceMe; 566 } 567 `, 568 fragmentShader1: ` 569 precision mediump float; 570 vec4 foo(vec4 $replaceMe) { 571 return $replaceMe; 572 } 573 void main() 574 { 575 gl_FragColor = foo(vec4(1,0,1,1)); 576 } 577 `, 578 vertexShader2: ` 579 varying vec4 $replaceMe; 580 attribute vec4 position; 581 void main() 582 { 583 gl_Position = position; 584 $replaceMe = position; 585 } 586 `, 587 fragmentShader2: ` 588 precision mediump float; 589 varying vec4 $replaceMe; 590 void main() 591 { 592 gl_FragColor = $replaceMe; 593 } 594 `, 595 vertexShader3: ` 596 attribute vec4 position; 597 void main() 598 { 599 gl_Position = position; 600 } 601 `, 602 fragmentShader3: ` 603 precision mediump float; 604 uniform vec4 $replaceMe; 605 void main() 606 { 607 gl_FragColor = $replaceMe; 608 } 609 `, 610 }; 611 612 const wtu = WebGLTestUtils; 613 const gl = wtu.create3DContext(); 614 const wait = ms => new Promise(resolve => setTimeout(resolve, ms)); 615 616 const reservedWords = new Set([ 617 ...GLSL_1_0_17_words, 618 ...GLSL_1_0_17_FutureWords, 619 ]); 620 621 const checkedWords = new Set(); 622 623 const src = []; 624 for (let ii = 0; ii < 4; ++ii) { 625 const vSrc = shaders[`vertexShader${ii}`]; 626 const fSrc = shaders[`fragmentShader${ii}`]; 627 src.push({vSrc: vSrc, fSrc: fSrc}); 628 } 629 630 for (const badWord of badWords) { 631 testWord(badWord); 632 await wait(); 633 } 634 finishTest(); 635 636 function testWord(word) { 637 if (reservedWords.has(word) || checkedWords.has(word)) { 638 return; 639 } 640 checkedWords.add(word); 641 debug(""); 642 debug(`testing: ${word}`); 643 644 for (let ii = 0; ii < src.length; ++ii) { 645 const vs = src[ii].vSrc.replace(/\$replaceMe/g, word); 646 const fs = src[ii].fSrc.replace(/\$replaceMe/g, word); 647 648 let success = true; 649 const program = wtu.loadProgram(gl, vs, fs, function(msg) { 650 debug(msg); 651 success = false; 652 }, true); 653 if (success) { 654 testPassed(`shader with: '${word}' compiled`); 655 } else { 656 testFailed(`shader with: '${word}' failed to compile`); 657 } 658 if (program) { 659 gl.deleteProgram(program); 660 } 661 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no GL errors"); 662 } 663 } 664 }