All files / engine/Source/Renderer RenderState.js

97.36% Statements 259/266
97.42% Branches 340/349
100% Functions 31/31
97.35% Lines 258/265

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968                      8911x                   17810x                                       4465x               4459x                         8899x                         26673x                               4468x 4468x 4468x 4468x 4468x 4468x 4468x 4468x 4468x 4468x 4468x   4468x   4468x 4468x 4468x   4468x 4468x       4468x 4468x         4468x       4468x       4468x       4468x           4468x 4468x 4468x                                 4468x                                 4468x         4468x                   4468x       2x       4466x 1x   4465x 1x   4464x       2x       4462x   1x       4461x   1x       4460x   1x       4459x 1x   4458x                     2x       4456x 1x   4455x 1x   4454x 1x   4453x 1x       4452x 1x       4451x 1x       4450x 1x   4449x 1x   4448x 1x       4447x 1x       4446x 1x       4445x 1x       4444x 1x       4443x 1x         4442x 15x         15x           15x         15x               4442x 4442x     1x 1x                                                                                                                                                                                                                                                   1x 15689x 15689x 15689x 15327x 15327x       362x 336x 336x 336x 228x   228x   228x           228x     336x     336x         336x           1x 3x 3x 3x     3x 3x 3x 3x   3x   2x     2x 2x           3x 1x               1x 1x             1x 1x       161275x 55421x   105854x         749x       10900x 10900x   10900x   10900x 5129x         745x       1944x 1944x   1944x   1944x 418x         66501x 66501x       66501x   66501x 2402x     2402x         747x 747x       38827x 38827x   38827x   38827x 20160x         18063x 18063x       9595x       10546x       8970x       18569x 18569x       18569x   18569x 8970x 8970x 8970x                   23789x 23789x   23789x   23789x 18342x 18342x 18342x 18342x         18342x 18342x 18342x   18342x 18342x 18342x 18342x   18342x             18342x 18342x 18342x 18342x   18342x                   745x 745x   745x   745x         1x     44399x 44399x 950x 950x 950x     44399x 44399x     1x 745x 745x 745x 745x 745x 745x 745x 745x 745x 745x 745x 745x 745x 745x       261x   261x 4x     261x       86x     261x       261x         24x     261x       2x     261x       155x     261x           43x     261x 77x     261x 41x     261x                                     94x     261x               261x     1x               80919x             42434x 42434x 261x 261x     42434x 42434x 108455x       80919x     80919x           80919x 65756x     80919x     80919x     80919x       17824x     80919x         43654x       1x   2428x 1x       2427x                                                                                                                                            
import BoundingRectangle from "../Core/BoundingRectangle.js";
import Color from "../Core/Color.js";
import Frozen from "../Core/Frozen.js";
import defined from "../Core/defined.js";
import DeveloperError from "../Core/DeveloperError.js";
import WebGLConstants from "../Core/WebGLConstants.js";
import WindingOrder from "../Core/WindingOrder.js";
import ContextLimits from "./ContextLimits.js";
import freezeRenderState from "./freezeRenderState.js";
 
function validateBlendEquation(blendEquation) {
  return (
    blendEquation === WebGLConstants.FUNC_ADD ||
    blendEquation === WebGLConstants.FUNC_SUBTRACT ||
    blendEquation === WebGLConstants.FUNC_REVERSE_SUBTRACT ||
    blendEquation === WebGLConstants.MIN ||
    blendEquation === WebGLConstants.MAX
  );
}
 
function validateBlendFunction(blendFunction) {
  return (
    blendFunction === WebGLConstants.ZERO ||
    blendFunction === WebGLConstants.ONE ||
    blendFunction === WebGLConstants.SRC_COLOR ||
    blendFunction === WebGLConstants.ONE_MINUS_SRC_COLOR ||
    blendFunction === WebGLConstants.DST_COLOR ||
    blendFunction === WebGLConstants.ONE_MINUS_DST_COLOR ||
    blendFunction === WebGLConstants.SRC_ALPHA ||
    blendFunction === WebGLConstants.ONE_MINUS_SRC_ALPHA ||
    blendFunction === WebGLConstants.DST_ALPHA ||
    blendFunction === WebGLConstants.ONE_MINUS_DST_ALPHA ||
    blendFunction === WebGLConstants.CONSTANT_COLOR ||
    blendFunction === WebGLConstants.ONE_MINUS_CONSTANT_COLOR ||
    blendFunction === WebGLConstants.CONSTANT_ALPHA ||
    blendFunction === WebGLConstants.ONE_MINUS_CONSTANT_ALPHA ||
    blendFunction === WebGLConstants.SRC_ALPHA_SATURATE
  );
}
 
function validateCullFace(cullFace) {
  return (
    cullFace === WebGLConstants.FRONT ||
    cullFace === WebGLConstants.BACK ||
    cullFace === WebGLConstants.FRONT_AND_BACK
  );
}
 
function validateDepthFunction(depthFunction) {
  return (
    depthFunction === WebGLConstants.NEVER ||
    depthFunction === WebGLConstants.LESS ||
    depthFunction === WebGLConstants.EQUAL ||
    depthFunction === WebGLConstants.LEQUAL ||
    depthFunction === WebGLConstants.GREATER ||
    depthFunction === WebGLConstants.NOTEQUAL ||
    depthFunction === WebGLConstants.GEQUAL ||
    depthFunction === WebGLConstants.ALWAYS
  );
}
 
function validateStencilFunction(stencilFunction) {
  return (
    stencilFunction === WebGLConstants.NEVER ||
    stencilFunction === WebGLConstants.LESS ||
    stencilFunction === WebGLConstants.EQUAL ||
    stencilFunction === WebGLConstants.LEQUAL ||
    stencilFunction === WebGLConstants.GREATER ||
    stencilFunction === WebGLConstants.NOTEQUAL ||
    stencilFunction === WebGLConstants.GEQUAL ||
    stencilFunction === WebGLConstants.ALWAYS
  );
}
 
function validateStencilOperation(stencilOperation) {
  return (
    stencilOperation === WebGLConstants.ZERO ||
    stencilOperation === WebGLConstants.KEEP ||
    stencilOperation === WebGLConstants.REPLACE ||
    stencilOperation === WebGLConstants.INCR ||
    stencilOperation === WebGLConstants.DECR ||
    stencilOperation === WebGLConstants.INVERT ||
    stencilOperation === WebGLConstants.INCR_WRAP ||
    stencilOperation === WebGLConstants.DECR_WRAP
  );
}
 
/**
 * @private
 */
function RenderState(renderState) {
  const rs = renderState ?? Frozen.EMPTY_OBJECT;
  const cull = rs.cull ?? Frozen.EMPTY_OBJECT;
  const polygonOffset = rs.polygonOffset ?? Frozen.EMPTY_OBJECT;
  const scissorTest = rs.scissorTest ?? Frozen.EMPTY_OBJECT;
  const scissorTestRectangle = scissorTest.rectangle ?? Frozen.EMPTY_OBJECT;
  const depthRange = rs.depthRange ?? Frozen.EMPTY_OBJECT;
  const depthTest = rs.depthTest ?? Frozen.EMPTY_OBJECT;
  const colorMask = rs.colorMask ?? Frozen.EMPTY_OBJECT;
  const blending = rs.blending ?? Frozen.EMPTY_OBJECT;
  const blendingColor = blending.color ?? Frozen.EMPTY_OBJECT;
  const stencilTest = rs.stencilTest ?? Frozen.EMPTY_OBJECT;
  const stencilTestFrontOperation =
    stencilTest.frontOperation ?? Frozen.EMPTY_OBJECT;
  const stencilTestBackOperation =
    stencilTest.backOperation ?? Frozen.EMPTY_OBJECT;
  const sampleCoverage = rs.sampleCoverage ?? Frozen.EMPTY_OBJECT;
  const viewport = rs.viewport;
 
  this.frontFace = rs.frontFace ?? WindingOrder.COUNTER_CLOCKWISE;
  this.cull = {
    enabled: cull.enabled ?? false,
    face: cull.face ?? WebGLConstants.BACK,
  };
  this.lineWidth = rs.lineWidth ?? 1.0;
  this.polygonOffset = {
    enabled: polygonOffset.enabled ?? false,
    factor: polygonOffset.factor ?? 0,
    units: polygonOffset.units ?? 0,
  };
  this.scissorTest = {
    enabled: scissorTest.enabled ?? false,
    rectangle: BoundingRectangle.clone(scissorTestRectangle),
  };
  this.depthRange = {
    near: depthRange.near ?? 0,
    far: depthRange.far ?? 1,
  };
  this.depthTest = {
    enabled: depthTest.enabled ?? false,
    func: depthTest.func ?? WebGLConstants.LESS, // func, because function is a JavaScript keyword
  };
  this.colorMask = {
    red: colorMask.red ?? true,
    green: colorMask.green ?? true,
    blue: colorMask.blue ?? true,
    alpha: colorMask.alpha ?? true,
  };
  this.depthMask = rs.depthMask ?? true;
  this.stencilMask = rs.stencilMask ?? ~0;
  this.blending = {
    enabled: blending.enabled ?? false,
    color: new Color(
      blendingColor.red ?? 0.0,
      blendingColor.green ?? 0.0,
      blendingColor.blue ?? 0.0,
      blendingColor.alpha ?? 0.0,
    ),
    equationRgb: blending.equationRgb ?? WebGLConstants.FUNC_ADD,
    equationAlpha: blending.equationAlpha ?? WebGLConstants.FUNC_ADD,
    functionSourceRgb: blending.functionSourceRgb ?? WebGLConstants.ONE,
    functionSourceAlpha: blending.functionSourceAlpha ?? WebGLConstants.ONE,
    functionDestinationRgb:
      blending.functionDestinationRgb ?? WebGLConstants.ZERO,
    functionDestinationAlpha:
      blending.functionDestinationAlpha ?? WebGLConstants.ZERO,
  };
  this.stencilTest = {
    enabled: stencilTest.enabled ?? false,
    frontFunction: stencilTest.frontFunction ?? WebGLConstants.ALWAYS,
    backFunction: stencilTest.backFunction ?? WebGLConstants.ALWAYS,
    reference: stencilTest.reference ?? 0,
    mask: stencilTest.mask ?? ~0,
    frontOperation: {
      fail: stencilTestFrontOperation.fail ?? WebGLConstants.KEEP,
      zFail: stencilTestFrontOperation.zFail ?? WebGLConstants.KEEP,
      zPass: stencilTestFrontOperation.zPass ?? WebGLConstants.KEEP,
    },
    backOperation: {
      fail: stencilTestBackOperation.fail ?? WebGLConstants.KEEP,
      zFail: stencilTestBackOperation.zFail ?? WebGLConstants.KEEP,
      zPass: stencilTestBackOperation.zPass ?? WebGLConstants.KEEP,
    },
  };
  this.sampleCoverage = {
    enabled: sampleCoverage.enabled ?? false,
    value: sampleCoverage.value ?? 1.0,
    invert: sampleCoverage.invert ?? false,
  };
  this.viewport = defined(viewport)
    ? new BoundingRectangle(
        viewport.x,
        viewport.y,
        viewport.width,
        viewport.height,
      )
    : undefined;
 
  //>>includeStart('debug', pragmas.debug);
  if (
    this.lineWidth < ContextLimits.minimumAliasedLineWidth ||
    this.lineWidth > ContextLimits.maximumAliasedLineWidth
  ) {
    throw new DeveloperError(
      "renderState.lineWidth is out of range.  Check minimumAliasedLineWidth and maximumAliasedLineWidth.",
    );
  }
  if (!WindingOrder.validate(this.frontFace)) {
    throw new DeveloperError("Invalid renderState.frontFace.");
  }
  if (!validateCullFace(this.cull.face)) {
    throw new DeveloperError("Invalid renderState.cull.face.");
  }
  if (
    this.scissorTest.rectangle.width < 0 ||
    this.scissorTest.rectangle.height < 0
  ) {
    throw new DeveloperError(
      "renderState.scissorTest.rectangle.width and renderState.scissorTest.rectangle.height must be greater than or equal to zero.",
    );
  }
  if (this.depthRange.near > this.depthRange.far) {
    // WebGL specific - not an error in GL ES
    throw new DeveloperError(
      "renderState.depthRange.near can not be greater than renderState.depthRange.far.",
    );
  }
  if (this.depthRange.near < 0) {
    // Would be clamped by GL
    throw new DeveloperError(
      "renderState.depthRange.near must be greater than or equal to zero.",
    );
  }
  if (this.depthRange.far > 1) {
    // Would be clamped by GL
    throw new DeveloperError(
      "renderState.depthRange.far must be less than or equal to one.",
    );
  }
  if (!validateDepthFunction(this.depthTest.func)) {
    throw new DeveloperError("Invalid renderState.depthTest.func.");
  }
  if (
    this.blending.color.red < 0.0 ||
    this.blending.color.red > 1.0 ||
    this.blending.color.green < 0.0 ||
    this.blending.color.green > 1.0 ||
    this.blending.color.blue < 0.0 ||
    this.blending.color.blue > 1.0 ||
    this.blending.color.alpha < 0.0 ||
    this.blending.color.alpha > 1.0
  ) {
    // Would be clamped by GL
    throw new DeveloperError(
      "renderState.blending.color components must be greater than or equal to zero and less than or equal to one.",
    );
  }
  if (!validateBlendEquation(this.blending.equationRgb)) {
    throw new DeveloperError("Invalid renderState.blending.equationRgb.");
  }
  if (!validateBlendEquation(this.blending.equationAlpha)) {
    throw new DeveloperError("Invalid renderState.blending.equationAlpha.");
  }
  if (!validateBlendFunction(this.blending.functionSourceRgb)) {
    throw new DeveloperError("Invalid renderState.blending.functionSourceRgb.");
  }
  if (!validateBlendFunction(this.blending.functionSourceAlpha)) {
    throw new DeveloperError(
      "Invalid renderState.blending.functionSourceAlpha.",
    );
  }
  if (!validateBlendFunction(this.blending.functionDestinationRgb)) {
    throw new DeveloperError(
      "Invalid renderState.blending.functionDestinationRgb.",
    );
  }
  if (!validateBlendFunction(this.blending.functionDestinationAlpha)) {
    throw new DeveloperError(
      "Invalid renderState.blending.functionDestinationAlpha.",
    );
  }
  if (!validateStencilFunction(this.stencilTest.frontFunction)) {
    throw new DeveloperError("Invalid renderState.stencilTest.frontFunction.");
  }
  if (!validateStencilFunction(this.stencilTest.backFunction)) {
    throw new DeveloperError("Invalid renderState.stencilTest.backFunction.");
  }
  if (!validateStencilOperation(this.stencilTest.frontOperation.fail)) {
    throw new DeveloperError(
      "Invalid renderState.stencilTest.frontOperation.fail.",
    );
  }
  if (!validateStencilOperation(this.stencilTest.frontOperation.zFail)) {
    throw new DeveloperError(
      "Invalid renderState.stencilTest.frontOperation.zFail.",
    );
  }
  if (!validateStencilOperation(this.stencilTest.frontOperation.zPass)) {
    throw new DeveloperError(
      "Invalid renderState.stencilTest.frontOperation.zPass.",
    );
  }
  if (!validateStencilOperation(this.stencilTest.backOperation.fail)) {
    throw new DeveloperError(
      "Invalid renderState.stencilTest.backOperation.fail.",
    );
  }
  if (!validateStencilOperation(this.stencilTest.backOperation.zFail)) {
    throw new DeveloperError(
      "Invalid renderState.stencilTest.backOperation.zFail.",
    );
  }
  if (!validateStencilOperation(this.stencilTest.backOperation.zPass)) {
    throw new DeveloperError(
      "Invalid renderState.stencilTest.backOperation.zPass.",
    );
  }
 
  if (defined(this.viewport)) {
    Iif (this.viewport.width < 0) {
      throw new DeveloperError(
        "renderState.viewport.width must be greater than or equal to zero.",
      );
    }
    Iif (this.viewport.height < 0) {
      throw new DeveloperError(
        "renderState.viewport.height must be greater than or equal to zero.",
      );
    }
 
    Iif (this.viewport.width > ContextLimits.maximumViewportWidth) {
      throw new DeveloperError(
        `renderState.viewport.width must be less than or equal to the maximum viewport width (${ContextLimits.maximumViewportWidth.toString()}).  Check maximumViewportWidth.`,
      );
    }
    Iif (this.viewport.height > ContextLimits.maximumViewportHeight) {
      throw new DeveloperError(
        `renderState.viewport.height must be less than or equal to the maximum viewport height (${ContextLimits.maximumViewportHeight.toString()}).  Check maximumViewportHeight.`,
      );
    }
  }
  //>>includeEnd('debug');
 
  this.id = 0;
  this._applyFunctions = [];
}
 
let nextRenderStateId = 0;
let renderStateCache = {};
 
/**
 * Validates and then finds or creates an immutable render state, which defines the pipeline
 * state for a {@link DrawCommand} or {@link ClearCommand}.  All inputs states are optional.  Omitted states
 * use the defaults shown in the example below.
 *
 * @param {object} [renderState] The states defining the render state as shown in the example below.
 *
 * @exception {RuntimeError} renderState.lineWidth is out of range.
 * @exception {DeveloperError} Invalid renderState.frontFace.
 * @exception {DeveloperError} Invalid renderState.cull.face.
 * @exception {DeveloperError} scissorTest.rectangle.width and scissorTest.rectangle.height must be greater than or equal to zero.
 * @exception {DeveloperError} renderState.depthRange.near can't be greater than renderState.depthRange.far.
 * @exception {DeveloperError} renderState.depthRange.near must be greater than or equal to zero.
 * @exception {DeveloperError} renderState.depthRange.far must be less than or equal to zero.
 * @exception {DeveloperError} Invalid renderState.depthTest.func.
 * @exception {DeveloperError} renderState.blending.color components must be greater than or equal to zero and less than or equal to one
 * @exception {DeveloperError} Invalid renderState.blending.equationRgb.
 * @exception {DeveloperError} Invalid renderState.blending.equationAlpha.
 * @exception {DeveloperError} Invalid renderState.blending.functionSourceRgb.
 * @exception {DeveloperError} Invalid renderState.blending.functionSourceAlpha.
 * @exception {DeveloperError} Invalid renderState.blending.functionDestinationRgb.
 * @exception {DeveloperError} Invalid renderState.blending.functionDestinationAlpha.
 * @exception {DeveloperError} Invalid renderState.stencilTest.frontFunction.
 * @exception {DeveloperError} Invalid renderState.stencilTest.backFunction.
 * @exception {DeveloperError} Invalid renderState.stencilTest.frontOperation.fail.
 * @exception {DeveloperError} Invalid renderState.stencilTest.frontOperation.zFail.
 * @exception {DeveloperError} Invalid renderState.stencilTest.frontOperation.zPass.
 * @exception {DeveloperError} Invalid renderState.stencilTest.backOperation.fail.
 * @exception {DeveloperError} Invalid renderState.stencilTest.backOperation.zFail.
 * @exception {DeveloperError} Invalid renderState.stencilTest.backOperation.zPass.
 * @exception {DeveloperError} renderState.viewport.width must be greater than or equal to zero.
 * @exception {DeveloperError} renderState.viewport.width must be less than or equal to the maximum viewport width.
 * @exception {DeveloperError} renderState.viewport.height must be greater than or equal to zero.
 * @exception {DeveloperError} renderState.viewport.height must be less than or equal to the maximum viewport height.
 *
 *
 * @example
 * const defaults = {
 *     frontFace : WindingOrder.COUNTER_CLOCKWISE,
 *     cull : {
 *         enabled : false,
 *         face : CullFace.BACK
 *     },
 *     lineWidth : 1,
 *     polygonOffset : {
 *         enabled : false,
 *         factor : 0,
 *         units : 0
 *     },
 *     scissorTest : {
 *         enabled : false,
 *         rectangle : {
 *             x : 0,
 *             y : 0,
 *             width : 0,
 *             height : 0
 *         }
 *     },
 *     depthRange : {
 *         near : 0,
 *         far : 1
 *     },
 *     depthTest : {
 *         enabled : false,
 *         func : DepthFunction.LESS
 *      },
 *     colorMask : {
 *         red : true,
 *         green : true,
 *         blue : true,
 *         alpha : true
 *     },
 *     depthMask : true,
 *     stencilMask : ~0,
 *     blending : {
 *         enabled : false,
 *         color : {
 *             red : 0.0,
 *             green : 0.0,
 *             blue : 0.0,
 *             alpha : 0.0
 *         },
 *         equationRgb : BlendEquation.ADD,
 *         equationAlpha : BlendEquation.ADD,
 *         functionSourceRgb : BlendFunction.ONE,
 *         functionSourceAlpha : BlendFunction.ONE,
 *         functionDestinationRgb : BlendFunction.ZERO,
 *         functionDestinationAlpha : BlendFunction.ZERO
 *     },
 *     stencilTest : {
 *         enabled : false,
 *         frontFunction : StencilFunction.ALWAYS,
 *         backFunction : StencilFunction.ALWAYS,
 *         reference : 0,
 *         mask : ~0,
 *         frontOperation : {
 *             fail : StencilOperation.KEEP,
 *             zFail : StencilOperation.KEEP,
 *             zPass : StencilOperation.KEEP
 *         },
 *         backOperation : {
 *             fail : StencilOperation.KEEP,
 *             zFail : StencilOperation.KEEP,
 *             zPass : StencilOperation.KEEP
 *         }
 *     },
 *     sampleCoverage : {
 *         enabled : false,
 *         value : 1.0,
 *         invert : false
 *      }
 * };
 *
 * const rs = RenderState.fromCache(defaults);
 *
 * @see DrawCommand
 * @see ClearCommand
 *
 * @private
 */
RenderState.fromCache = function (renderState) {
  const partialKey = JSON.stringify(renderState);
  let cachedState = renderStateCache[partialKey];
  if (defined(cachedState)) {
    ++cachedState.referenceCount;
    return cachedState.state;
  }
 
  // Cache miss.  Fully define render state and try again.
  let states = new RenderState(renderState);
  const fullKey = JSON.stringify(states);
  cachedState = renderStateCache[fullKey];
  if (!defined(cachedState)) {
    states.id = nextRenderStateId++;
    //>>includeStart('debug', pragmas.debug);
    states = freezeRenderState(states);
    //>>includeEnd('debug');
    cachedState = {
      referenceCount: 0,
      state: states,
    };
 
    // Cache full render state.  Multiple partially defined render states may map to this.
    renderStateCache[fullKey] = cachedState;
  }
 
  ++cachedState.referenceCount;
 
  // Cache partial render state so we can skip validation on a cache hit for a partially defined render state
  renderStateCache[partialKey] = {
    referenceCount: 1,
    state: cachedState.state,
  };
 
  return cachedState.state;
};
 
/**
 * @private
 */
RenderState.removeFromCache = function (renderState) {
  const states = new RenderState(renderState);
  const fullKey = JSON.stringify(states);
  const fullCachedState = renderStateCache[fullKey];
 
  // decrement partial key reference count
  const partialKey = JSON.stringify(renderState);
  const cachedState = renderStateCache[partialKey];
  Eif (defined(cachedState)) {
    --cachedState.referenceCount;
 
    if (cachedState.referenceCount === 0) {
      // remove partial key
      delete renderStateCache[partialKey];
 
      // decrement full key reference count
      Eif (defined(fullCachedState)) {
        --fullCachedState.referenceCount;
      }
    }
  }
 
  // remove full key if reference count is zero
  if (defined(fullCachedState) && fullCachedState.referenceCount === 0) {
    delete renderStateCache[fullKey];
  }
};
 
/**
 * This function is for testing purposes only.
 * @private
 */
RenderState.getCache = function () {
  return renderStateCache;
};
 
/**
 * This function is for testing purposes only.
 * @private
 */
RenderState.clearCache = function () {
  renderStateCache = {};
};
 
function enableOrDisable(gl, glEnum, enable) {
  if (enable) {
    gl.enable(glEnum);
  } else {
    gl.disable(glEnum);
  }
}
 
function applyFrontFace(gl, renderState) {
  gl.frontFace(renderState.frontFace);
}
 
function applyCull(gl, renderState) {
  const cull = renderState.cull;
  const enabled = cull.enabled;
 
  enableOrDisable(gl, gl.CULL_FACE, enabled);
 
  if (enabled) {
    gl.cullFace(cull.face);
  }
}
 
function applyLineWidth(gl, renderState) {
  gl.lineWidth(renderState.lineWidth);
}
 
function applyPolygonOffset(gl, renderState) {
  const polygonOffset = renderState.polygonOffset;
  const enabled = polygonOffset.enabled;
 
  enableOrDisable(gl, gl.POLYGON_OFFSET_FILL, enabled);
 
  if (enabled) {
    gl.polygonOffset(polygonOffset.factor, polygonOffset.units);
  }
}
 
function applyScissorTest(gl, renderState, passState) {
  const scissorTest = renderState.scissorTest;
  const enabled = defined(passState.scissorTest)
    ? passState.scissorTest.enabled
    : scissorTest.enabled;
 
  enableOrDisable(gl, gl.SCISSOR_TEST, enabled);
 
  if (enabled) {
    const rectangle = defined(passState.scissorTest)
      ? passState.scissorTest.rectangle
      : scissorTest.rectangle;
    gl.scissor(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
  }
}
 
function applyDepthRange(gl, renderState) {
  const depthRange = renderState.depthRange;
  gl.depthRange(depthRange.near, depthRange.far);
}
 
function applyDepthTest(gl, renderState) {
  const depthTest = renderState.depthTest;
  const enabled = depthTest.enabled;
 
  enableOrDisable(gl, gl.DEPTH_TEST, enabled);
 
  if (enabled) {
    gl.depthFunc(depthTest.func);
  }
}
 
function applyColorMask(gl, renderState) {
  const colorMask = renderState.colorMask;
  gl.colorMask(colorMask.red, colorMask.green, colorMask.blue, colorMask.alpha);
}
 
function applyDepthMask(gl, renderState) {
  gl.depthMask(renderState.depthMask);
}
 
function applyStencilMask(gl, renderState) {
  gl.stencilMask(renderState.stencilMask);
}
 
function applyBlendingColor(gl, color) {
  gl.blendColor(color.red, color.green, color.blue, color.alpha);
}
 
function applyBlending(gl, renderState, passState) {
  const blending = renderState.blending;
  const enabled = defined(passState.blendingEnabled)
    ? passState.blendingEnabled
    : blending.enabled;
 
  enableOrDisable(gl, gl.BLEND, enabled);
 
  if (enabled) {
    applyBlendingColor(gl, blending.color);
    gl.blendEquationSeparate(blending.equationRgb, blending.equationAlpha);
    gl.blendFuncSeparate(
      blending.functionSourceRgb,
      blending.functionDestinationRgb,
      blending.functionSourceAlpha,
      blending.functionDestinationAlpha,
    );
  }
}
 
function applyStencilTest(gl, renderState) {
  const stencilTest = renderState.stencilTest;
  const enabled = stencilTest.enabled;
 
  enableOrDisable(gl, gl.STENCIL_TEST, enabled);
 
  if (enabled) {
    const frontFunction = stencilTest.frontFunction;
    const backFunction = stencilTest.backFunction;
    const reference = stencilTest.reference;
    const mask = stencilTest.mask;
 
    // Section 6.8 of the WebGL spec requires the reference and masks to be the same for
    // front- and back-face tests.  This call prevents invalid operation errors when calling
    // stencilFuncSeparate on Firefox.  Perhaps they should delay validation to avoid requiring this.
    gl.stencilFunc(frontFunction, reference, mask);
    gl.stencilFuncSeparate(gl.BACK, backFunction, reference, mask);
    gl.stencilFuncSeparate(gl.FRONT, frontFunction, reference, mask);
 
    const frontOperation = stencilTest.frontOperation;
    const frontOperationFail = frontOperation.fail;
    const frontOperationZFail = frontOperation.zFail;
    const frontOperationZPass = frontOperation.zPass;
 
    gl.stencilOpSeparate(
      gl.FRONT,
      frontOperationFail,
      frontOperationZFail,
      frontOperationZPass,
    );
 
    const backOperation = stencilTest.backOperation;
    const backOperationFail = backOperation.fail;
    const backOperationZFail = backOperation.zFail;
    const backOperationZPass = backOperation.zPass;
 
    gl.stencilOpSeparate(
      gl.BACK,
      backOperationFail,
      backOperationZFail,
      backOperationZPass,
    );
  }
}
 
function applySampleCoverage(gl, renderState) {
  const sampleCoverage = renderState.sampleCoverage;
  const enabled = sampleCoverage.enabled;
 
  enableOrDisable(gl, gl.SAMPLE_COVERAGE, enabled);
 
  Iif (enabled) {
    gl.sampleCoverage(sampleCoverage.value, sampleCoverage.invert);
  }
}
 
const scratchViewport = new BoundingRectangle();
 
function applyViewport(gl, renderState, passState) {
  let viewport = renderState.viewport ?? passState.viewport;
  if (!defined(viewport)) {
    viewport = scratchViewport;
    viewport.width = passState.context.drawingBufferWidth;
    viewport.height = passState.context.drawingBufferHeight;
  }
 
  passState.context.uniformState.viewport = viewport;
  gl.viewport(viewport.x, viewport.y, viewport.width, viewport.height);
}
 
RenderState.apply = function (gl, renderState, passState) {
  applyFrontFace(gl, renderState);
  applyCull(gl, renderState);
  applyLineWidth(gl, renderState);
  applyPolygonOffset(gl, renderState);
  applyDepthRange(gl, renderState);
  applyDepthTest(gl, renderState);
  applyColorMask(gl, renderState);
  applyDepthMask(gl, renderState);
  applyStencilMask(gl, renderState);
  applyStencilTest(gl, renderState);
  applySampleCoverage(gl, renderState);
  applyScissorTest(gl, renderState, passState);
  applyBlending(gl, renderState, passState);
  applyViewport(gl, renderState, passState);
};
 
function createFuncs(previousState, nextState) {
  const funcs = [];
 
  if (previousState.frontFace !== nextState.frontFace) {
    funcs.push(applyFrontFace);
  }
 
  if (
    previousState.cull.enabled !== nextState.cull.enabled ||
    previousState.cull.face !== nextState.cull.face
  ) {
    funcs.push(applyCull);
  }
 
  Iif (previousState.lineWidth !== nextState.lineWidth) {
    funcs.push(applyLineWidth);
  }
 
  if (
    previousState.polygonOffset.enabled !== nextState.polygonOffset.enabled ||
    previousState.polygonOffset.factor !== nextState.polygonOffset.factor ||
    previousState.polygonOffset.units !== nextState.polygonOffset.units
  ) {
    funcs.push(applyPolygonOffset);
  }
 
  if (
    previousState.depthRange.near !== nextState.depthRange.near ||
    previousState.depthRange.far !== nextState.depthRange.far
  ) {
    funcs.push(applyDepthRange);
  }
 
  if (
    previousState.depthTest.enabled !== nextState.depthTest.enabled ||
    previousState.depthTest.func !== nextState.depthTest.func
  ) {
    funcs.push(applyDepthTest);
  }
 
  if (
    previousState.colorMask.red !== nextState.colorMask.red ||
    previousState.colorMask.green !== nextState.colorMask.green ||
    previousState.colorMask.blue !== nextState.colorMask.blue ||
    previousState.colorMask.alpha !== nextState.colorMask.alpha
  ) {
    funcs.push(applyColorMask);
  }
 
  if (previousState.depthMask !== nextState.depthMask) {
    funcs.push(applyDepthMask);
  }
 
  if (previousState.stencilMask !== nextState.stencilMask) {
    funcs.push(applyStencilMask);
  }
 
  if (
    previousState.stencilTest.enabled !== nextState.stencilTest.enabled ||
    previousState.stencilTest.frontFunction !==
      nextState.stencilTest.frontFunction ||
    previousState.stencilTest.backFunction !==
      nextState.stencilTest.backFunction ||
    previousState.stencilTest.reference !== nextState.stencilTest.reference ||
    previousState.stencilTest.mask !== nextState.stencilTest.mask ||
    previousState.stencilTest.frontOperation.fail !==
      nextState.stencilTest.frontOperation.fail ||
    previousState.stencilTest.frontOperation.zFail !==
      nextState.stencilTest.frontOperation.zFail ||
    previousState.stencilTest.backOperation.fail !==
      nextState.stencilTest.backOperation.fail ||
    previousState.stencilTest.backOperation.zFail !==
      nextState.stencilTest.backOperation.zFail ||
    previousState.stencilTest.backOperation.zPass !==
      nextState.stencilTest.backOperation.zPass
  ) {
    funcs.push(applyStencilTest);
  }
 
  Iif (
    previousState.sampleCoverage.enabled !== nextState.sampleCoverage.enabled ||
    previousState.sampleCoverage.value !== nextState.sampleCoverage.value ||
    previousState.sampleCoverage.invert !== nextState.sampleCoverage.invert
  ) {
    funcs.push(applySampleCoverage);
  }
 
  return funcs;
}
 
RenderState.partialApply = function (
  gl,
  previousRenderState,
  renderState,
  previousPassState,
  passState,
  clear,
) {
  if (previousRenderState !== renderState) {
    // When a new render state is applied, instead of making WebGL calls for all the states or first
    // comparing the states one-by-one with the previous state (basically a linear search), we take
    // advantage of RenderState's immutability, and store a dynamically populated sparse data structure
    // containing functions that make the minimum number of WebGL calls when transitioning from one state
    // to the other.  In practice, this works well since state-to-state transitions generally only require a
    // few WebGL calls, especially if commands are stored by state.
    let funcs = renderState._applyFunctions[previousRenderState.id];
    if (!defined(funcs)) {
      funcs = createFuncs(previousRenderState, renderState);
      renderState._applyFunctions[previousRenderState.id] = funcs;
    }
 
    const len = funcs.length;
    for (let i = 0; i < len; ++i) {
      funcs[i](gl, renderState);
    }
  }
 
  const previousScissorTest = defined(previousPassState.scissorTest)
    ? previousPassState.scissorTest
    : previousRenderState.scissorTest;
  const scissorTest = defined(passState.scissorTest)
    ? passState.scissorTest
    : renderState.scissorTest;
 
  // Our scissor rectangle can get out of sync with the GL scissor rectangle on clears.
  // Seems to be a problem only on ANGLE. See https://github.com/CesiumGS/cesium/issues/2994
  if (previousScissorTest !== scissorTest || clear) {
    applyScissorTest(gl, renderState, passState);
  }
 
  const previousBlendingEnabled = defined(previousPassState.blendingEnabled)
    ? previousPassState.blendingEnabled
    : previousRenderState.blending.enabled;
  const blendingEnabled = defined(passState.blendingEnabled)
    ? passState.blendingEnabled
    : renderState.blending.enabled;
  if (
    previousBlendingEnabled !== blendingEnabled ||
    (blendingEnabled && previousRenderState.blending !== renderState.blending)
  ) {
    applyBlending(gl, renderState, passState);
  }
 
  if (
    previousRenderState !== renderState ||
    previousPassState !== passState ||
    previousPassState.context !== passState.context
  ) {
    applyViewport(gl, renderState, passState);
  }
};
 
RenderState.getState = function (renderState) {
  //>>includeStart('debug', pragmas.debug);
  if (!defined(renderState)) {
    throw new DeveloperError("renderState is required.");
  }
  //>>includeEnd('debug');
 
  return {
    frontFace: renderState.frontFace,
    cull: {
      enabled: renderState.cull.enabled,
      face: renderState.cull.face,
    },
    lineWidth: renderState.lineWidth,
    polygonOffset: {
      enabled: renderState.polygonOffset.enabled,
      factor: renderState.polygonOffset.factor,
      units: renderState.polygonOffset.units,
    },
    scissorTest: {
      enabled: renderState.scissorTest.enabled,
      rectangle: BoundingRectangle.clone(renderState.scissorTest.rectangle),
    },
    depthRange: {
      near: renderState.depthRange.near,
      far: renderState.depthRange.far,
    },
    depthTest: {
      enabled: renderState.depthTest.enabled,
      func: renderState.depthTest.func,
    },
    colorMask: {
      red: renderState.colorMask.red,
      green: renderState.colorMask.green,
      blue: renderState.colorMask.blue,
      alpha: renderState.colorMask.alpha,
    },
    depthMask: renderState.depthMask,
    stencilMask: renderState.stencilMask,
    blending: {
      enabled: renderState.blending.enabled,
      color: Color.clone(renderState.blending.color),
      equationRgb: renderState.blending.equationRgb,
      equationAlpha: renderState.blending.equationAlpha,
      functionSourceRgb: renderState.blending.functionSourceRgb,
      functionSourceAlpha: renderState.blending.functionSourceAlpha,
      functionDestinationRgb: renderState.blending.functionDestinationRgb,
      functionDestinationAlpha: renderState.blending.functionDestinationAlpha,
    },
    stencilTest: {
      enabled: renderState.stencilTest.enabled,
      frontFunction: renderState.stencilTest.frontFunction,
      backFunction: renderState.stencilTest.backFunction,
      reference: renderState.stencilTest.reference,
      mask: renderState.stencilTest.mask,
      frontOperation: {
        fail: renderState.stencilTest.frontOperation.fail,
        zFail: renderState.stencilTest.frontOperation.zFail,
        zPass: renderState.stencilTest.frontOperation.zPass,
      },
      backOperation: {
        fail: renderState.stencilTest.backOperation.fail,
        zFail: renderState.stencilTest.backOperation.zFail,
        zPass: renderState.stencilTest.backOperation.zPass,
      },
    },
    sampleCoverage: {
      enabled: renderState.sampleCoverage.enabled,
      value: renderState.sampleCoverage.value,
      invert: renderState.sampleCoverage.invert,
    },
    viewport: defined(renderState.viewport)
      ? BoundingRectangle.clone(renderState.viewport)
      : undefined,
  };
};
export default RenderState;