1[ 2 { 3 "op":"mod", 4 "return_type":"float", 5 "args":[ 6 "float x", 7 "float y" 8 ], 9 "body":[ 10 "return x - y * floor(x / y);" 11 ] 12 }, 13 { 14 "op":"mod", 15 "return_type":"float2", 16 "args":[ 17 "float2 x", 18 "float2 y" 19 ], 20 "body":[ 21 "return x - y * floor(x / y);" 22 ] 23 }, 24 { 25 "op":"mod", 26 "return_type":"float2", 27 "args":[ 28 "float2 x", 29 "float y" 30 ], 31 "body":[ 32 "return x - y * floor(x / y);" 33 ] 34 }, 35 { 36 "op":"mod", 37 "return_type":"float3", 38 "args":[ 39 "float3 x", 40 "float3 y" 41 ], 42 "body":[ 43 "return x - y * floor(x / y);" 44 ] 45 }, 46 { 47 "op":"mod", 48 "return_type":"float3", 49 "args":[ 50 "float3 x", 51 "float y" 52 ], 53 "body":[ 54 "return x - y * floor(x / y);" 55 ] 56 }, 57 { 58 "op":"mod", 59 "return_type":"float4", 60 "args":[ 61 "float4 x", 62 "float4 y" 63 ], 64 "body":[ 65 "return x - y * floor(x / y);" 66 ] 67 }, 68 { 69 "op":"mod", 70 "return_type":"float4", 71 "args":[ 72 "float4 x", 73 "float y" 74 ], 75 "body":[ 76 "return x - y * floor(x / y);" 77 ] 78 }, 79 { 80 "op":"frexp", 81 "return_type":"float", 82 "args":[ 83 "float x", 84 "out int exp" 85 ], 86 "body":[ 87 "float fexp;", 88 "float mantissa = frexp(abs(x), fexp) * sign(x);", 89 "exp = int(fexp);", 90 "return mantissa;" 91 ] 92 }, 93 { 94 "op":"frexp", 95 "return_type":"float2", 96 "args":[ 97 "float2 x", 98 "out int2 exp" 99 ], 100 "body":[ 101 "float2 fexp;", 102 "float2 mantissa = frexp(abs(x), fexp) * sign(x);", 103 "exp = int2(fexp);", 104 "return mantissa;" 105 ] 106 }, 107 { 108 "op":"frexp", 109 "return_type":"float3", 110 "args":[ 111 "float3 x", 112 "out int3 exp" 113 ], 114 "body":[ 115 "float3 fexp;", 116 "float3 mantissa = frexp(abs(x), fexp) * sign(x);", 117 "exp = int3(fexp);", 118 "return mantissa;" 119 ] 120 }, 121 { 122 "op":"frexp", 123 "return_type":"float4", 124 "args":[ 125 "float4 x", 126 "out int4 exp" 127 ], 128 "body":[ 129 "float4 fexp;", 130 "float4 mantissa = frexp(abs(x), fexp) * sign(x);", 131 "exp = int4(fexp);", 132 "return mantissa;" 133 ] 134 }, 135 { 136 "op":"ldexp", 137 "return_type":"float", 138 "args":[ 139 "float x", 140 "int exp" 141 ], 142 "body":[ 143 "return ldexp(x, float(exp));" 144 ] 145 }, 146 { 147 "op":"ldexp", 148 "return_type":"float2", 149 "args":[ 150 "float2 x", 151 "int2 exp" 152 ], 153 "body":[ 154 "return ldexp(x, float2(exp));" 155 ] 156 }, 157 { 158 "op":"ldexp", 159 "return_type":"float3", 160 "args":[ 161 "float3 x", 162 "int3 exp" 163 ], 164 "body":[ 165 "return ldexp(x, float3(exp));" 166 ] 167 }, 168 { 169 "op":"ldexp", 170 "return_type":"float4", 171 "args":[ 172 "float4 x", 173 "int4 exp" 174 ], 175 "body":[ 176 "return ldexp(x, float4(exp));" 177 ] 178 }, 179 { 180 "op":"faceforward", 181 "return_type":"float", 182 "args":[ 183 "float N", 184 "float I", 185 "float Nref" 186 ], 187 "body":[ 188 "if(dot(Nref, I) >= 0)", 189 "{", 190 " return -N;", 191 "}", 192 "else", 193 "{", 194 " return N;", 195 "}" 196 ] 197 }, 198 { 199 "op":"faceforward", 200 "return_type":"float2", 201 "args":[ 202 "float2 N", 203 "float2 I", 204 "float2 Nref" 205 ], 206 "body":[ 207 "if(dot(Nref, I) >= 0)", 208 "{", 209 " return -N;", 210 "}", 211 "else", 212 "{", 213 " return N;", 214 "}" 215 ] 216 }, 217 { 218 "op":"faceforward", 219 "return_type":"float3", 220 "args":[ 221 "float3 N", 222 "float3 I", 223 "float3 Nref" 224 ], 225 "body":[ 226 "if(dot(Nref, I) >= 0)", 227 "{", 228 " return -N;", 229 "}", 230 "else", 231 "{", 232 " return N;", 233 "}" 234 ] 235 }, 236 { 237 "op":"faceforward", 238 "return_type":"float4", 239 "args":[ 240 "float4 N", 241 "float4 I", 242 "float4 Nref" 243 ], 244 "body":[ 245 "if(dot(Nref, I) >= 0)", 246 "{", 247 " return -N;", 248 "}", 249 "else", 250 "{", 251 " return N;", 252 "}" 253 ] 254 }, 255 { 256 "op":"atan", 257 "return_type":"float", 258 "args":[ 259 "float y", 260 "float x" 261 ], 262 "body":[ 263 "if(x == 0 && y == 0) x = 1;", 264 "return atan2(y, x);" 265 ] 266 }, 267 { 268 "op":"atan", 269 "return_type":"float2", 270 "args":[ 271 "float2 y", 272 "float2 x" 273 ], 274 "body":[ 275 "if(x[0] == 0 && y[0] == 0) x[0] = 1;", 276 "if(x[1] == 0 && y[1] == 0) x[1] = 1;", 277 "return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));" 278 ] 279 }, 280 { 281 "op":"atan", 282 "return_type":"float3", 283 "args":[ 284 "float3 y", 285 "float3 x" 286 ], 287 "body":[ 288 "if(x[0] == 0 && y[0] == 0) x[0] = 1;", 289 "if(x[1] == 0 && y[1] == 0) x[1] = 1;", 290 "if(x[2] == 0 && y[2] == 0) x[2] = 1;", 291 "return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));" 292 ] 293 }, 294 { 295 "op":"atan", 296 "return_type":"float4", 297 "args":[ 298 "float4 y", 299 "float4 x" 300 ], 301 "body":[ 302 "if(x[0] == 0 && y[0] == 0) x[0] = 1;", 303 "if(x[1] == 0 && y[1] == 0) x[1] = 1;", 304 "if(x[2] == 0 && y[2] == 0) x[2] = 1;", 305 "if(x[3] == 0 && y[3] == 0) x[3] = 1;", 306 "return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], ", 307 "x[2]), atan2(y[3], x[3]));" 308 ] 309 }, 310 { 311 "op":"asinh", 312 "return_type":"float", 313 "args":[ 314 "in float x" 315 ], 316 "body":[ 317 "return log(x + sqrt(pow(x, 2.0) + 1.0));" 318 ] 319 }, 320 { 321 "op":"asinh", 322 "return_type":"float2", 323 "args":[ 324 "in float2 x" 325 ], 326 "body":[ 327 "return log(x + sqrt(pow(x, 2.0) + 1.0));" 328 ] 329 }, 330 { 331 "op":"asinh", 332 "return_type":"float3", 333 "args":[ 334 "in float3 x" 335 ], 336 "body":[ 337 "return log(x + sqrt(pow(x, 2.0) + 1.0));" 338 ] 339 }, 340 { 341 "op":"asinh", 342 "return_type":"float4", 343 "args":[ 344 "in float4 x" 345 ], 346 "body":[ 347 "return log(x + sqrt(pow(x, 2.0) + 1.0));" 348 ] 349 }, 350 { 351 "op":"acosh", 352 "return_type":"float", 353 "args":[ 354 "in float x" 355 ], 356 "body":[ 357 "return log(x + sqrt(x + 1.0) * sqrt(x - 1.0));" 358 ] 359 }, 360 { 361 "op":"acosh", 362 "return_type":"float2", 363 "args":[ 364 "in float2 x" 365 ], 366 "body":[ 367 "return log(x + sqrt(x + 1.0) * sqrt(x - 1.0));" 368 ] 369 }, 370 { 371 "op":"acosh", 372 "return_type":"float3", 373 "args":[ 374 "in float3 x" 375 ], 376 "body":[ 377 "return log(x + sqrt(x + 1.0) * sqrt(x - 1.0));" 378 ] 379 }, 380 { 381 "op":"acosh", 382 "return_type":"float4", 383 "args":[ 384 "in float4 x" 385 ], 386 "body":[ 387 "return log(x + sqrt(x + 1.0) * sqrt(x - 1.0));" 388 ] 389 }, 390 { 391 "op":"atanh", 392 "return_type":"float", 393 "args":[ 394 "in float x" 395 ], 396 "body":[ 397 "return 0.5 * log((1.0 + x) / (1.0 - x));" 398 ] 399 }, 400 { 401 "op":"atanh", 402 "return_type":"float2", 403 "args":[ 404 "in float2 x" 405 ], 406 "body":[ 407 "return 0.5 * log((1.0 + x) / (1.0 - x));" 408 ] 409 }, 410 { 411 "op":"atanh", 412 "return_type":"float3", 413 "args":[ 414 "in float3 x" 415 ], 416 "body":[ 417 "return 0.5 * log((1.0 + x) / (1.0 - x));" 418 ] 419 }, 420 { 421 "op":"atanh", 422 "return_type":"float4", 423 "args":[ 424 "in float4 x" 425 ], 426 "body":[ 427 "return 0.5 * log((1.0 + x) / (1.0 - x));" 428 ] 429 }, 430 { 431 "op":"roundEven", 432 "return_type":"float", 433 "args":[ 434 "in float x" 435 ], 436 "body":[ 437 "return (frac(x) == 0.5 && trunc(x) % 2.0 == 0.0) ? trunc(x) : round(x);" 438 ] 439 }, 440 { 441 "op":"roundEven", 442 "return_type":"float2", 443 "args":[ 444 "in float2 x" 445 ], 446 "body":[ 447 "float2 v;", 448 "v[0] = (frac(x[0]) == 0.5 && trunc(x[0]) % 2.0 == 0.0) ? trunc(x[0]) : round(x[0]);", 449 "v[1] = (frac(x[1]) == 0.5 && trunc(x[1]) % 2.0 == 0.0) ? trunc(x[1]) : round(x[1]);", 450 "return v;" 451 ] 452 }, 453 { 454 "op":"roundEven", 455 "return_type":"float3", 456 "args":[ 457 "in float3 x" 458 ], 459 "body":[ 460 "float3 v;", 461 "v[0] = (frac(x[0]) == 0.5 && trunc(x[0]) % 2.0 == 0.0) ? trunc(x[0]) : round(x[0]);", 462 "v[1] = (frac(x[1]) == 0.5 && trunc(x[1]) % 2.0 == 0.0) ? trunc(x[1]) : round(x[1]);", 463 "v[2] = (frac(x[2]) == 0.5 && trunc(x[2]) % 2.0 == 0.0) ? trunc(x[2]) : round(x[2]);", 464 "return v;" 465 ] 466 }, 467 { 468 "op":"roundEven", 469 "return_type":"float4", 470 "args":[ 471 "in float4 x" 472 ], 473 "body":[ 474 "float4 v;", 475 "v[0] = (frac(x[0]) == 0.5 && trunc(x[0]) % 2.0 == 0.0) ? trunc(x[0]) : round(x[0]);", 476 "v[1] = (frac(x[1]) == 0.5 && trunc(x[1]) % 2.0 == 0.0) ? trunc(x[1]) : round(x[1]);", 477 "v[2] = (frac(x[2]) == 0.5 && trunc(x[2]) % 2.0 == 0.0) ? trunc(x[2]) : round(x[2]);", 478 "v[3] = (frac(x[3]) == 0.5 && trunc(x[3]) % 2.0 == 0.0) ? trunc(x[3]) : round(x[3]);", 479 "return v;" 480 ] 481 }, 482 { 483 "op":"packSnorm2x16", 484 "return_type":"uint", 485 "args":[ 486 "in float2 v" 487 ], 488 "helper":[ 489 "int webgl_toSnorm16(in float x) {", 490 " return int(round(clamp(x, -1.0, 1.0) * 32767.0));", 491 "}" 492 ], 493 "body":[ 494 "int x = webgl_toSnorm16(v.x);", 495 "int y = webgl_toSnorm16(v.y);", 496 "return (asuint(y) << 16) | (asuint(x) & 0xffffu);" 497 ] 498 }, 499 { 500 "op":"packUnorm2x16", 501 "return_type":"uint", 502 "args":[ 503 "in float2 v" 504 ], 505 "helper":[ 506 "uint webgl_toUnorm16(in float x) {", 507 " return uint(round(clamp(x, 0.0, 1.0) * 65535.0));", 508 "}" 509 ], 510 "body":[ 511 "uint x = webgl_toUnorm16(v.x);", 512 "uint y = webgl_toUnorm16(v.y);", 513 "return (y << 16) | x;" 514 ] 515 }, 516 { 517 "op":"packHalf2x16", 518 "return_type":"uint", 519 "args":[ 520 "in float2 v" 521 ], 522 "body":[ 523 "uint x = f32tof16(v.x);", 524 "uint y = f32tof16(v.y);", 525 "return (y << 16) | x;" 526 ] 527 }, 528 { 529 "op":"unpackSnorm2x16", 530 "return_type":"float2", 531 "args":[ 532 "in uint u" 533 ], 534 "helper":[ 535 "float webgl_fromSnorm16(in uint x) {", 536 " int xi = asint(x & 0x7fffu) - asint(x & 0x8000u);", 537 " return clamp(float(xi) / 32767.0, -1.0, 1.0);", 538 "}" 539 ], 540 "body":[ 541 "uint y = (u >> 16);", 542 "uint x = u;", 543 "return float2(webgl_fromSnorm16(x), webgl_fromSnorm16(y));" 544 ] 545 }, 546 { 547 "op":"unpackUnorm2x16", 548 "return_type":"float2", 549 "args":[ 550 "in uint u" 551 ], 552 "helper":[ 553 "float webgl_fromUnorm16(in uint x) {", 554 " return float(x) / 65535.0;", 555 "}" 556 ], 557 "body":[ 558 "uint y = (u >> 16);", 559 "uint x = u & 0xffffu;", 560 "return float2(webgl_fromUnorm16(x), webgl_fromUnorm16(y));" 561 ] 562 }, 563 { 564 "op":"unpackHalf2x16", 565 "return_type":"float2", 566 "args":[ 567 "in uint u" 568 ], 569 "body":[ 570 "uint y = (u >> 16);", 571 "uint x = u & 0xffffu;", 572 "return float2(f16tof32(x), f16tof32(y));" 573 ] 574 }, 575 { 576 "op":"packSnorm4x8", 577 "return_type":"uint", 578 "args":[ 579 "in float4 v" 580 ], 581 "helper":[ 582 "int webgl_toSnorm8(in float x) {", 583 " return int(round(clamp(x, -1.0, 1.0) * 127.0));", 584 "}" 585 ], 586 "body":[ 587 "int x = webgl_toSnorm8(v.x);", 588 "int y = webgl_toSnorm8(v.y);", 589 "int z = webgl_toSnorm8(v.z);", 590 "int w = webgl_toSnorm8(v.w);", 591 "return ((asuint(w) & 0xffu) << 24) | ((asuint(z) & 0xffu) << 16) ", 592 "| ((asuint(y) & 0xffu) << 8) | (asuint(x) & 0xffu);" 593 ] 594 }, 595 { 596 "op":"packUnorm4x8", 597 "return_type":"uint", 598 "args":[ 599 "in float4 v" 600 ], 601 "helper":[ 602 "uint webgl_toUnorm8(in float x) {", 603 " return uint(round(clamp(x, 0.0, 1.0) * 255.0));", 604 "}" 605 ], 606 "body":[ 607 "uint x = webgl_toUnorm8(v.x);", 608 "uint y = webgl_toUnorm8(v.y);", 609 "uint z = webgl_toUnorm8(v.z);", 610 "uint w = webgl_toUnorm8(v.w);", 611 "return (w << 24) | (z << 16) | (y << 8) | x;" 612 ] 613 }, 614 { 615 "op":"unpackSnorm4x8", 616 "return_type":"float4", 617 "args":[ 618 "in uint u" 619 ], 620 "helper":[ 621 "float webgl_fromSnorm8(in uint x) {", 622 " int xi = asint(x & 0x7fu) - asint(x & 0x80u);", 623 " return clamp(float(xi) / 127.0, -1.0, 1.0);", 624 "}" 625 ], 626 "body":[ 627 "uint w = (u >> 24);", 628 "uint z = (u >> 16);", 629 "uint y = (u >> 8);", 630 "uint x = u;", 631 "return float4(webgl_fromSnorm8(x), webgl_fromSnorm8(y), ", 632 "webgl_fromSnorm8(z), webgl_fromSnorm8(w));" 633 ] 634 }, 635 { 636 "op":"unpackUnorm4x8", 637 "return_type":"float4", 638 "args":[ 639 "in uint u" 640 ], 641 "helper":[ 642 "float webgl_fromUnorm8(in uint x) {", 643 " return float(x) / 255.0;", 644 "}" 645 ], 646 "body":[ 647 "uint w = (u >> 24) & 0xffu;", 648 "uint z = (u >> 16) & 0xffu;", 649 "uint y = (u >> 8) & 0xffu;", 650 "uint x = u & 0xffu;", 651 "return float4(webgl_fromUnorm8(x), webgl_fromUnorm8(y), ", 652 "webgl_fromUnorm8(z), webgl_fromUnorm8(w));" 653 ] 654 }, 655 { 656 "comment":[ 657 "The matrix resulting from outer product needs to be transposed", 658 "(matrices are stored as transposed to simplify element access in HLSL).", 659 "So the function should return transpose(c * r) where c is a column vector", 660 "and r is a row vector. This can be simplified by using the following", 661 "formula:", 662 "transpose(c * r) = transpose(r) * transpose(c)", 663 "transpose(r) and transpose(c) are in a sense free, since to get the", 664 "transpose of r, we simply can build a column matrix out of the original", 665 "vector instead of a row matrix." 666 ], 667 "op":"outerProduct", 668 "return_type":"float2x2", 669 "args":[ 670 "in float2 c", 671 "in float2 r" 672 ], 673 "body":[ 674 "return mul(float2x1(r), float1x2(c));" 675 ] 676 }, 677 { 678 "op":"outerProduct", 679 "return_type":"float3x3", 680 "args":[ 681 "in float3 c", 682 "in float3 r" 683 ], 684 "body":[ 685 "return mul(float3x1(r), float1x3(c));" 686 ] 687 }, 688 { 689 "op":"outerProduct", 690 "return_type":"float4x4", 691 "args":[ 692 "in float4 c", 693 "in float4 r" 694 ], 695 "body":[ 696 "return mul(float4x1(r), float1x4(c));" 697 ] 698 }, 699 { 700 "op":"outerProduct", 701 "return_type":"float2x3", 702 "args":[ 703 "in float3 c", 704 "in float2 r" 705 ], 706 "body":[ 707 "return mul(float2x1(r), float1x3(c));" 708 ] 709 }, 710 { 711 "op":"outerProduct", 712 "return_type":"float3x2", 713 "args":[ 714 "in float2 c", 715 "in float3 r" 716 ], 717 "body":[ 718 "return mul(float3x1(r), float1x2(c));" 719 ] 720 }, 721 { 722 "op":"outerProduct", 723 "return_type":"float2x4", 724 "args":[ 725 "in float4 c", 726 "in float2 r" 727 ], 728 "body":[ 729 "return mul(float2x1(r), float1x4(c));" 730 ] 731 }, 732 { 733 "op":"outerProduct", 734 "return_type":"float4x2", 735 "args":[ 736 "in float2 c", 737 "in float4 r" 738 ], 739 "body":[ 740 "return mul(float4x1(r), float1x2(c));" 741 ] 742 }, 743 { 744 "op":"outerProduct", 745 "return_type":"float3x4", 746 "args":[ 747 "in float4 c", 748 "in float3 r" 749 ], 750 "body":[ 751 "return mul(float3x1(r), float1x4(c));" 752 ] 753 }, 754 { 755 "op":"outerProduct", 756 "return_type":"float4x3", 757 "args":[ 758 "in float3 c", 759 "in float4 r" 760 ], 761 "body":[ 762 "return mul(float4x1(r), float1x3(c));" 763 ] 764 }, 765 { 766 "comment":[ 767 "Remember here that the parameter matrix is actually the transpose", 768 "of the matrix that we're trying to invert, and the resulting matrix", 769 "should also be the transpose of the inverse.", 770 "When accessing the parameter matrix with m[a][b] it can be thought of so", 771 "that a is the column and b is the row of the matrix that we're inverting.", 772 "We calculate the inverse as the adjugate matrix divided by the", 773 "determinant of the matrix being inverted. However, as the result needs", 774 "to be transposed, we actually use of the transpose of the adjugate matrix", 775 "which happens to be the cofactor matrix. That's stored in 'cof'.", 776 "We don't need to care about divide-by-zero since results are undefined", 777 "for singular or poorly-conditioned matrices." 778 ], 779 "op":"inverse", 780 "return_type":"float2x2", 781 "args":[ 782 "in float2x2 m" 783 ], 784 "body":[ 785 "float2x2 cof = { m[1][1], -m[0][1], -m[1][0], m[0][0] };", 786 "return cof / determinant(transpose(m));" 787 ] 788 }, 789 { 790 "comment":[ 791 "cofAB is the cofactor for column A and row B." 792 ], 793 "op":"inverse", 794 "return_type":"float3x3", 795 "args":[ 796 "in float3x3 m" 797 ], 798 "body":[ 799 "float cof00 = m[1][1] * m[2][2] - m[2][1] * m[1][2];", 800 "float cof01 = -(m[1][0] * m[2][2] - m[2][0] * m[1][2]);", 801 "float cof02 = m[1][0] * m[2][1] - m[2][0] * m[1][1];", 802 "float cof10 = -(m[0][1] * m[2][2] - m[2][1] * m[0][2]);", 803 "float cof11 = m[0][0] * m[2][2] - m[2][0] * m[0][2];", 804 "float cof12 = -(m[0][0] * m[2][1] - m[2][0] * m[0][1]);", 805 "float cof20 = m[0][1] * m[1][2] - m[1][1] * m[0][2];", 806 "float cof21 = -(m[0][0] * m[1][2] - m[1][0] * m[0][2]);", 807 "float cof22 = m[0][0] * m[1][1] - m[1][0] * m[0][1];", 808 "float3x3 cof = { cof00, cof10, cof20, cof01, cof11, cof21, cof02, cof12, cof22 };", 809 "return cof / determinant(transpose(m));" 810 ] 811 }, 812 { 813 "op":"inverse", 814 "return_type":"float4x4", 815 "args":[ 816 "in float4x4 m" 817 ], 818 "body":[ 819 "float cof00 = m[1][1] * m[2][2] * m[3][3] + m[2][1] * m[3][2] * m[1][3] + m[3][1] * ", 820 "m[1][2] * m[2][3]", 821 " - m[1][1] * m[3][2] * m[2][3] - m[2][1] * m[1][2] * m[3][3] - m[3][1] * m[2][2] * ", 822 "m[1][3];", 823 "float cof01 = -(m[1][0] * m[2][2] * m[3][3] + m[2][0] * m[3][2] * m[1][3] + m[3][0] * ", 824 "m[1][2] * m[2][3]", 825 " - m[1][0] * m[3][2] * m[2][3] - m[2][0] * m[1][2] * m[3][3] - m[3][0] * m[2][2] * ", 826 "m[1][3]);", 827 "float cof02 = m[1][0] * m[2][1] * m[3][3] + m[2][0] * m[3][1] * m[1][3] + m[3][0] * ", 828 "m[1][1] * m[2][3]", 829 " - m[1][0] * m[3][1] * m[2][3] - m[2][0] * m[1][1] * m[3][3] - m[3][0] * m[2][1] * ", 830 "m[1][3];", 831 "float cof03 = -(m[1][0] * m[2][1] * m[3][2] + m[2][0] * m[3][1] * m[1][2] + m[3][0] * ", 832 "m[1][1] * m[2][2]", 833 " - m[1][0] * m[3][1] * m[2][2] - m[2][0] * m[1][1] * m[3][2] - m[3][0] * m[2][1] * ", 834 "m[1][2]);", 835 "float cof10 = -(m[0][1] * m[2][2] * m[3][3] + m[2][1] * m[3][2] * m[0][3] + m[3][1] * ", 836 "m[0][2] * m[2][3]", 837 " - m[0][1] * m[3][2] * m[2][3] - m[2][1] * m[0][2] * m[3][3] - m[3][1] * m[2][2] * ", 838 "m[0][3]);", 839 "float cof11 = m[0][0] * m[2][2] * m[3][3] + m[2][0] * m[3][2] * m[0][3] + m[3][0] * ", 840 "m[0][2] * m[2][3]", 841 " - m[0][0] * m[3][2] * m[2][3] - m[2][0] * m[0][2] * m[3][3] - m[3][0] * m[2][2] * ", 842 "m[0][3];", 843 "float cof12 = -(m[0][0] * m[2][1] * m[3][3] + m[2][0] * m[3][1] * m[0][3] + m[3][0] * ", 844 "m[0][1] * m[2][3]", 845 " - m[0][0] * m[3][1] * m[2][3] - m[2][0] * m[0][1] * m[3][3] - m[3][0] * m[2][1] * ", 846 "m[0][3]);", 847 "float cof13 = m[0][0] * m[2][1] * m[3][2] + m[2][0] * m[3][1] * m[0][2] + m[3][0] * ", 848 "m[0][1] * m[2][2]", 849 " - m[0][0] * m[3][1] * m[2][2] - m[2][0] * m[0][1] * m[3][2] - m[3][0] * m[2][1] * ", 850 "m[0][2];", 851 "float cof20 = m[0][1] * m[1][2] * m[3][3] + m[1][1] * m[3][2] * m[0][3] + m[3][1] * ", 852 "m[0][2] * m[1][3]", 853 " - m[0][1] * m[3][2] * m[1][3] - m[1][1] * m[0][2] * m[3][3] - m[3][1] * m[1][2] * ", 854 "m[0][3];", 855 "float cof21 = -(m[0][0] * m[1][2] * m[3][3] + m[1][0] * m[3][2] * m[0][3] + m[3][0] * ", 856 "m[0][2] * m[1][3]", 857 " - m[0][0] * m[3][2] * m[1][3] - m[1][0] * m[0][2] * m[3][3] - m[3][0] * m[1][2] * ", 858 "m[0][3]);", 859 "float cof22 = m[0][0] * m[1][1] * m[3][3] + m[1][0] * m[3][1] * m[0][3] + m[3][0] * ", 860 "m[0][1] * m[1][3]", 861 " - m[0][0] * m[3][1] * m[1][3] - m[1][0] * m[0][1] * m[3][3] - m[3][0] * m[1][1] * ", 862 "m[0][3];", 863 "float cof23 = -(m[0][0] * m[1][1] * m[3][2] + m[1][0] * m[3][1] * m[0][2] + m[3][0] * ", 864 "m[0][1] * m[1][2]", 865 " - m[0][0] * m[3][1] * m[1][2] - m[1][0] * m[0][1] * m[3][2] - m[3][0] * m[1][1] * ", 866 "m[0][2]);", 867 "float cof30 = -(m[0][1] * m[1][2] * m[2][3] + m[1][1] * m[2][2] * m[0][3] + m[2][1] * ", 868 "m[0][2] * m[1][3]", 869 " - m[0][1] * m[2][2] * m[1][3] - m[1][1] * m[0][2] * m[2][3] - m[2][1] * m[1][2] * ", 870 "m[0][3]);", 871 "float cof31 = m[0][0] * m[1][2] * m[2][3] + m[1][0] * m[2][2] * m[0][3] + m[2][0] * ", 872 "m[0][2] * m[1][3]", 873 " - m[0][0] * m[2][2] * m[1][3] - m[1][0] * m[0][2] * m[2][3] - m[2][0] * m[1][2] * ", 874 "m[0][3];", 875 "float cof32 = -(m[0][0] * m[1][1] * m[2][3] + m[1][0] * m[2][1] * m[0][3] + m[2][0] * ", 876 "m[0][1] * m[1][3]", 877 " - m[0][0] * m[2][1] * m[1][3] - m[1][0] * m[0][1] * m[2][3] - m[2][0] * m[1][1] * ", 878 "m[0][3]);", 879 "float cof33 = m[0][0] * m[1][1] * m[2][2] + m[1][0] * m[2][1] * m[0][2] + m[2][0] * ", 880 "m[0][1] * m[1][2]", 881 " - m[0][0] * m[2][1] * m[1][2] - m[1][0] * m[0][1] * m[2][2] - m[2][0] * m[1][1] * ", 882 "m[0][2];", 883 "float4x4 cof = { cof00, cof10, cof20, cof30, cof01, cof11, cof21, cof31,", 884 " cof02, cof12, cof22, cof32, cof03, cof13, cof23, cof33 };", 885 "return cof / determinant(transpose(m));" 886 ] 887 }, 888 { 889 "comment":[ 890 "Emulate ESSL3 variant of mix that takes last argument as boolean vector.", 891 "genType mix(genType x, genType y, genBType a): Selects which vector each returned component", 892 "comes from. For a component of 'a' that is false, the corresponding component of 'x' is", 893 "returned. For a component of 'a' that is true, the corresponding component of 'y' is returned." 894 ], 895 "op":"mix", 896 "return_type":"float", 897 "args":[ 898 "float x", 899 "float y", 900 "bool a" 901 ], 902 "body":[ 903 "return a ? y : x;" 904 ] 905 }, 906 { 907 "op":"mix", 908 "return_type":"float2", 909 "args":[ 910 "float2 x", 911 "float2 y", 912 "bool2 a" 913 ], 914 "body":[ 915 "return a ? y : x;" 916 ] 917 }, 918 { 919 "op":"mix", 920 "return_type":"float3", 921 "args":[ 922 "float3 x", 923 "float3 y", 924 "bool3 a" 925 ], 926 "body":[ 927 "return a ? y : x;" 928 ] 929 }, 930 { 931 "op":"mix", 932 "return_type":"float4", 933 "args":[ 934 "float4 x", 935 "float4 y", 936 "bool4 a" 937 ], 938 "body":[ 939 "return a ? y : x;" 940 ] 941 }, 942 { 943 "op":"bitfieldExtract", 944 "return_type":"uint", 945 "args":[ 946 "uint value", 947 "int offset", 948 "int bits" 949 ], 950 "body":[ 951 "if (offset < 0 || bits <= 0 || offset >= 32 || bits > 32 || offset + bits > 32)", 952 "{", 953 " return 0u;", 954 "}", 955 "uint maskMsb = (1u << (bits - 1));", 956 "uint mask = ((maskMsb - 1u) | maskMsb) << offset;", 957 "return (value & mask) >> offset;" 958 ] 959 }, 960 { 961 "op":"bitfieldExtract", 962 "return_type":"uint2", 963 "args":[ 964 "uint2 value", 965 "int offset", 966 "int bits" 967 ], 968 "body":[ 969 "if (offset < 0 || bits <= 0 || offset >= 32 || bits > 32 || offset + bits > 32)", 970 "{", 971 " return uint2(0u, 0u);", 972 "}", 973 "uint maskMsb = (1u << (bits - 1));", 974 "uint mask = ((maskMsb - 1u) | maskMsb) << offset;", 975 "return (value & mask) >> offset;" 976 ] 977 }, 978 { 979 "op":"bitfieldExtract", 980 "return_type":"uint3", 981 "args":[ 982 "uint3 value", 983 "int offset", 984 "int bits" 985 ], 986 "body":[ 987 "if (offset < 0 || bits <= 0 || offset >= 32 || bits > 32 || offset + bits > 32)", 988 "{", 989 " return uint3(0u, 0u, 0u);", 990 "}", 991 "uint maskMsb = (1u << (bits - 1));", 992 "uint mask = ((maskMsb - 1u) | maskMsb) << offset;", 993 "return (value & mask) >> offset;" 994 ] 995 }, 996 { 997 "op":"bitfieldExtract", 998 "return_type":"uint4", 999 "args":[ 1000 "uint4 value", 1001 "int offset", 1002 "int bits" 1003 ], 1004 "body":[ 1005 "if (offset < 0 || bits <= 0 || offset >= 32 || bits > 32 || offset + bits > 32)", 1006 "{", 1007 " return uint4(0u, 0u, 0u, 0u);", 1008 "}", 1009 "uint maskMsb = (1u << (bits - 1));", 1010 "uint mask = ((maskMsb - 1u) | maskMsb) << offset;", 1011 "return (value & mask) >> offset;" 1012 ] 1013 }, 1014 { 1015 "op":"bitfieldExtract", 1016 "return_type":"int", 1017 "args":[ 1018 "int value", 1019 "int offset", 1020 "int bits" 1021 ], 1022 "body":[ 1023 "if (offset < 0 || bits <= 0 || offset >= 32 || bits > 32 || offset + bits > 32)", 1024 "{", 1025 " return 0;", 1026 "}", 1027 "uint maskMsb = (1u << (bits - 1));", 1028 "uint mask = ((maskMsb - 1u) | maskMsb) << offset;", 1029 "uint resultUnsigned = (asuint(value) & mask) >> offset;", 1030 "if (bits != 32 && (resultUnsigned & maskMsb) != 0)", 1031 "{", 1032 " uint higherBitsMask = ((1u << (32 - bits)) - 1u) << bits;", 1033 " resultUnsigned |= higherBitsMask;", 1034 "}", 1035 "return asint(resultUnsigned);" 1036 ] 1037 }, 1038 { 1039 "op":"bitfieldExtract", 1040 "return_type":"int2", 1041 "args":[ 1042 "int2 value", 1043 "int offset", 1044 "int bits" 1045 ], 1046 "body":[ 1047 "if (offset < 0 || bits <= 0 || offset >= 32 || bits > 32 || offset + bits > 32)", 1048 "{", 1049 " return int2(0, 0);", 1050 "}", 1051 "uint maskMsb = (1u << (bits - 1));", 1052 "uint mask = ((maskMsb - 1u) | maskMsb) << offset;", 1053 "uint2 resultUnsigned = (asuint(value) & mask) >> offset;", 1054 "if (bits != 32)", 1055 "{", 1056 " uint higherBitsMask = ((1u << (32 - bits)) - 1u) << bits;", 1057 " resultUnsigned |= ((resultUnsigned & maskMsb) >> (bits - 1)) * higherBitsMask;", 1058 "}", 1059 "return asint(resultUnsigned);" 1060 ] 1061 }, 1062 { 1063 "op":"bitfieldExtract", 1064 "return_type":"int3", 1065 "args":[ 1066 "int3 value", 1067 "int offset", 1068 "int bits" 1069 ], 1070 "body":[ 1071 "if (offset < 0 || bits <= 0 || offset >= 32 || bits > 32 || offset + bits > 32)", 1072 "{", 1073 " return int3(0, 0, 0);", 1074 "}", 1075 "uint maskMsb = (1u << (bits - 1));", 1076 "uint mask = ((maskMsb - 1u) | maskMsb) << offset;", 1077 "uint3 resultUnsigned = (asuint(value) & mask) >> offset;", 1078 "if (bits != 32)", 1079 "{", 1080 " uint higherBitsMask = ((1u << (32 - bits)) - 1u) << bits;", 1081 " resultUnsigned |= ((resultUnsigned & maskMsb) >> (bits - 1)) * higherBitsMask;", 1082 "}", 1083 "return asint(resultUnsigned);" 1084 ] 1085 }, 1086 { 1087 "op":"bitfieldExtract", 1088 "return_type":"int4", 1089 "args":[ 1090 "int4 value", 1091 "int offset", 1092 "int bits" 1093 ], 1094 "body":[ 1095 "if (offset < 0 || bits <= 0 || offset >= 32 || bits > 32 || offset + bits > 32)", 1096 "{", 1097 " return int4(0, 0, 0, 0);", 1098 "}", 1099 "uint maskMsb = (1u << (bits - 1));", 1100 "uint mask = ((maskMsb - 1u) | maskMsb) << offset;", 1101 "uint4 resultUnsigned = (asuint(value) & mask) >> offset;", 1102 "if (bits != 32)", 1103 "{", 1104 " uint higherBitsMask = ((1u << (32 - bits)) - 1u) << bits;", 1105 " resultUnsigned |= ((resultUnsigned & maskMsb) >> (bits - 1)) * higherBitsMask;", 1106 "}", 1107 "return asint(resultUnsigned);" 1108 ] 1109 }, 1110 { 1111 "op":"bitfieldInsert", 1112 "return_type":"uint", 1113 "args":[ 1114 "uint base", 1115 "uint insert", 1116 "int offset", 1117 "int bits" 1118 ], 1119 "body":[ 1120 "if (offset < 0 || bits <= 0 || offset >= 32 || bits > 32 || offset + bits > 32)", 1121 "{", 1122 " return base;", 1123 "}", 1124 "uint maskMsb = (1u << (bits - 1));", 1125 "uint insertMask = ((maskMsb - 1u) | maskMsb) << offset;", 1126 "uint baseMask = ~insertMask;", 1127 "return (base & baseMask) | ((insert << offset) & insertMask);" 1128 ] 1129 }, 1130 { 1131 "op":"bitfieldInsert", 1132 "return_type":"uint2", 1133 "args":[ 1134 "uint2 base", 1135 "uint2 insert", 1136 "int offset", 1137 "int bits" 1138 ], 1139 "body":[ 1140 "if (offset < 0 || bits <= 0 || offset >= 32 || bits > 32 || offset + bits > 32)", 1141 "{", 1142 " return base;", 1143 "}", 1144 "uint maskMsb = (1u << (bits - 1));", 1145 "uint insertMask = ((maskMsb - 1u) | maskMsb) << offset;", 1146 "uint baseMask = ~insertMask;", 1147 "return (base & baseMask) | ((insert << offset) & insertMask);" 1148 ] 1149 }, 1150 { 1151 "op":"bitfieldInsert", 1152 "return_type":"uint3", 1153 "args":[ 1154 "uint3 base", 1155 "uint3 insert", 1156 "int offset", 1157 "int bits" 1158 ], 1159 "body":[ 1160 "if (offset < 0 || bits <= 0 || offset >= 32 || bits > 32 || offset + bits > 32)", 1161 "{", 1162 " return base;", 1163 "}", 1164 "uint maskMsb = (1u << (bits - 1));", 1165 "uint insertMask = ((maskMsb - 1u) | maskMsb) << offset;", 1166 "uint baseMask = ~insertMask;", 1167 "return (base & baseMask) | ((insert << offset) & insertMask);" 1168 ] 1169 }, 1170 { 1171 "op":"bitfieldInsert", 1172 "return_type":"uint4", 1173 "args":[ 1174 "uint4 base", 1175 "uint4 insert", 1176 "int offset", 1177 "int bits" 1178 ], 1179 "body":[ 1180 "if (offset < 0 || bits <= 0 || offset >= 32 || bits > 32 || offset + bits > 32)", 1181 "{", 1182 " return base;", 1183 "}", 1184 "uint maskMsb = (1u << (bits - 1));", 1185 "uint insertMask = ((maskMsb - 1u) | maskMsb) << offset;", 1186 "uint baseMask = ~insertMask;", 1187 "return (base & baseMask) | ((insert << offset) & insertMask);" 1188 ] 1189 }, 1190 { 1191 "op":"bitfieldInsert", 1192 "return_type":"int", 1193 "args":[ 1194 "int base", 1195 "int insert", 1196 "int offset", 1197 "int bits" 1198 ], 1199 "body":[ 1200 "if (offset < 0 || bits <= 0 || offset >= 32 || bits > 32 || offset + bits > 32)", 1201 "{", 1202 " return base;", 1203 "}", 1204 "uint maskMsb = (1u << (bits - 1));", 1205 "uint insertMask = ((maskMsb - 1u) | maskMsb) << offset;", 1206 "uint baseMask = ~insertMask;", 1207 "uint resultUnsigned = (asuint(base) & baseMask) | ((asuint(insert) << offset) & ", 1208 " insertMask);", 1209 "return asint(resultUnsigned);" 1210 ] 1211 }, 1212 { 1213 "op":"bitfieldInsert", 1214 "return_type":"int2", 1215 "args":[ 1216 "int2 base", 1217 "int2 insert", 1218 "int offset", 1219 "int bits" 1220 ], 1221 "body":[ 1222 "if (offset < 0 || bits <= 0 || offset >= 32 || bits > 32 || offset + bits > 32)", 1223 "{", 1224 " return base;", 1225 "}", 1226 "uint maskMsb = (1u << (bits - 1));", 1227 "uint insertMask = ((maskMsb - 1u) | maskMsb) << offset;", 1228 "uint baseMask = ~insertMask;", 1229 "uint2 resultUnsigned = (asuint(base) & baseMask) | ((asuint(insert) << offset) & ", 1230 " insertMask);", 1231 "return asint(resultUnsigned);" 1232 ] 1233 }, 1234 { 1235 "op":"bitfieldInsert", 1236 "return_type":"int3", 1237 "args":[ 1238 "int3 base", 1239 "int3 insert", 1240 "int offset", 1241 "int bits" 1242 ], 1243 "body":[ 1244 "if (offset < 0 || bits <= 0 || offset >= 32 || bits > 32 || offset + bits > 32)", 1245 "{", 1246 " return base;", 1247 "}", 1248 "uint maskMsb = (1u << (bits - 1));", 1249 "uint insertMask = ((maskMsb - 1u) | maskMsb) << offset;", 1250 "uint baseMask = ~insertMask;", 1251 "uint3 resultUnsigned = (asuint(base) & baseMask) | ((asuint(insert) << offset) & ", 1252 " insertMask);", 1253 "return asint(resultUnsigned);" 1254 ] 1255 }, 1256 { 1257 "op":"bitfieldInsert", 1258 "return_type":"int4", 1259 "args":[ 1260 "int4 base", 1261 "int4 insert", 1262 "int offset", 1263 "int bits" 1264 ], 1265 "body":[ 1266 "if (offset < 0 || bits <= 0 || offset >= 32 || bits > 32 || offset + bits > 32)", 1267 "{", 1268 " return base;", 1269 "}", 1270 "uint maskMsb = (1u << (bits - 1));", 1271 "uint insertMask = ((maskMsb - 1u) | maskMsb) << offset;", 1272 "uint baseMask = ~insertMask;", 1273 "uint4 resultUnsigned = (asuint(base) & baseMask) | ((asuint(insert) << offset) & ", 1274 "insertMask);", 1275 "return asint(resultUnsigned);" 1276 ] 1277 }, 1278 { 1279 "op":"uaddCarry", 1280 "return_type":"uint", 1281 "args":[ 1282 "uint x", 1283 "uint y", 1284 "out uint carry" 1285 ], 1286 "body":[ 1287 "carry = uint(x > (0xffffffffu - y));", 1288 "return x + y;" 1289 ] 1290 }, 1291 { 1292 "op":"uaddCarry", 1293 "return_type":"uint2", 1294 "args":[ 1295 "uint2 x", 1296 "uint2 y", 1297 "out uint2 carry" 1298 ], 1299 "body":[ 1300 "carry = uint2(x > (0xffffffffu - y));", 1301 "return x + y;" 1302 ] 1303 }, 1304 { 1305 "op":"uaddCarry", 1306 "return_type":"uint3", 1307 "args":[ 1308 "uint3 x", 1309 "uint3 y", 1310 "out uint3 carry" 1311 ], 1312 "body":[ 1313 "carry = uint3(x > (0xffffffffu - y));", 1314 "return x + y;" 1315 ] 1316 }, 1317 { 1318 "op":"uaddCarry", 1319 "return_type":"uint4", 1320 "args":[ 1321 "uint4 x", 1322 "uint4 y", 1323 "out uint4 carry" 1324 ], 1325 "body":[ 1326 "carry = uint4(x > (0xffffffffu - y));", 1327 "return x + y;" 1328 ] 1329 }, 1330 { 1331 "op":"usubBorrow", 1332 "return_type":"uint", 1333 "args":[ 1334 "uint x", 1335 "uint y", 1336 "out uint borrow" 1337 ], 1338 "body":[ 1339 "borrow = uint(x < y);", 1340 "return x - y;" 1341 ] 1342 }, 1343 { 1344 "op":"usubBorrow", 1345 "return_type":"uint2", 1346 "args":[ 1347 "uint2 x", 1348 "uint2 y", 1349 "out uint2 borrow" 1350 ], 1351 "body":[ 1352 "borrow = uint2(x < y);", 1353 "return x - y;" 1354 ] 1355 }, 1356 { 1357 "op":"usubBorrow", 1358 "return_type":"uint3", 1359 "args":[ 1360 "uint3 x", 1361 "uint3 y", 1362 "out uint3 borrow" 1363 ], 1364 "body":[ 1365 "borrow = uint3(x < y);", 1366 "return x - y;" 1367 ] 1368 }, 1369 { 1370 "op":"usubBorrow", 1371 "return_type":"uint4", 1372 "args":[ 1373 "uint4 x", 1374 "uint4 y", 1375 "out uint4 borrow" 1376 ], 1377 "body":[ 1378 "borrow = uint4(x < y);", 1379 "return x - y;" 1380 ] 1381 }, 1382 { 1383 "comment":[ 1384 "We emulate tanh just to avoid overflow on large arguments." 1385 ], 1386 "op":"tanh", 1387 "return_type":"float", 1388 "args":[ 1389 "float x" 1390 ], 1391 "body":[ 1392 "return (abs(x) > 15.0) ? sign(x) : tanh(x);" 1393 ] 1394 }, 1395 { 1396 "op":"tanh", 1397 "return_type":"float2", 1398 "args":[ 1399 "float2 x" 1400 ], 1401 "body":[ 1402 "return (abs(x) > 15.0) ? sign(x) : tanh(x);" 1403 ] 1404 }, 1405 { 1406 "op":"tanh", 1407 "return_type":"float3", 1408 "args":[ 1409 "float3 x" 1410 ], 1411 "body":[ 1412 "return (abs(x) > 15.0) ? sign(x) : tanh(x);" 1413 ] 1414 }, 1415 { 1416 "op":"tanh", 1417 "return_type":"float4", 1418 "args":[ 1419 "float4 x" 1420 ], 1421 "body":[ 1422 "return (abs(x) > 15.0) ? sign(x) : tanh(x);" 1423 ] 1424 } 1425] 1426