智慧水务管理系统 - 精河县供水工程综合管理平台

MultisampleFramebuffer.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import Check from "../Core/Check.js";
  2. import Frozen from "../Core/Frozen.js";
  3. import defined from "../Core/defined.js";
  4. import destroyObject from "../Core/destroyObject.js";
  5. import DeveloperError from "../Core/DeveloperError.js";
  6. import Framebuffer from "./Framebuffer.js";
  7. /**
  8. * Creates a multisampling wrapper around two framebuffers with optional initial
  9. * color and depth-stencil attachments. The first framebuffer has multisampled
  10. * renderbuffer attachments and is bound to READ_FRAMEBUFFER during the blit. The
  11. * second is bound to DRAW_FRAMEBUFFER during the blit, and has texture attachments
  12. * to store the copied pixels.
  13. *
  14. * @param {object} options Object with the following properties:
  15. * @param {Context} options.context
  16. * @param {number} options.width
  17. * @param {number} options.height
  18. * @param {Texture[]} [options.colorTextures]
  19. * @param {Renderbuffer[]} [options.colorRenderbuffers]
  20. * @param {Texture} [options.depthStencilTexture]
  21. * @param {Renderbuffer} [options.depthStencilRenderbuffer]
  22. * @param {boolean} [options.destroyAttachments]
  23. *
  24. * @exception {DeveloperError} Both color renderbuffer and texture attachments must be provided.
  25. * @exception {DeveloperError} Both depth-stencil renderbuffer and texture attachments must be provided.
  26. *
  27. * @private
  28. * @constructor
  29. */
  30. function MultisampleFramebuffer(options) {
  31. options = options ?? Frozen.EMPTY_OBJECT;
  32. const {
  33. context,
  34. width,
  35. height,
  36. colorRenderbuffers,
  37. colorTextures,
  38. depthStencilRenderbuffer,
  39. depthStencilTexture,
  40. destroyAttachments,
  41. } = options;
  42. //>>includeStart('debug', pragmas.debug);
  43. Check.defined("options.context", context);
  44. Check.defined("options.width", width);
  45. Check.defined("options.height", height);
  46. //>>includeEnd('debug');
  47. this._width = width;
  48. this._height = height;
  49. if (defined(colorRenderbuffers) !== defined(colorTextures)) {
  50. throw new DeveloperError(
  51. "Both color renderbuffer and texture attachments must be provided.",
  52. );
  53. }
  54. if (defined(depthStencilRenderbuffer) !== defined(depthStencilTexture)) {
  55. throw new DeveloperError(
  56. "Both depth-stencil renderbuffer and texture attachments must be provided.",
  57. );
  58. }
  59. this._renderFramebuffer = new Framebuffer({
  60. context: context,
  61. colorRenderbuffers: colorRenderbuffers,
  62. depthStencilRenderbuffer: depthStencilRenderbuffer,
  63. destroyAttachments: destroyAttachments,
  64. });
  65. this._colorFramebuffer = new Framebuffer({
  66. context: context,
  67. colorTextures: colorTextures,
  68. depthStencilTexture: depthStencilTexture,
  69. destroyAttachments: destroyAttachments,
  70. });
  71. }
  72. MultisampleFramebuffer.prototype.getRenderFramebuffer = function () {
  73. return this._renderFramebuffer;
  74. };
  75. MultisampleFramebuffer.prototype.getColorFramebuffer = function () {
  76. return this._colorFramebuffer;
  77. };
  78. /**
  79. * Copy from the render framebuffer to the color framebuffer, resolving the stencil.
  80. *
  81. * @param {Context} context
  82. * @param {boolean} blitStencil <code>true</code> if the stencil mask should be applied.
  83. *
  84. * @private
  85. */
  86. MultisampleFramebuffer.prototype.blitFramebuffers = function (
  87. context,
  88. blitStencil,
  89. ) {
  90. this._renderFramebuffer.bindRead();
  91. this._colorFramebuffer.bindDraw();
  92. const gl = context._gl;
  93. let mask = 0;
  94. if (this._colorFramebuffer._colorTextures.length > 0) {
  95. mask |= gl.COLOR_BUFFER_BIT;
  96. }
  97. if (defined(this._colorFramebuffer.depthStencilTexture)) {
  98. mask |= gl.DEPTH_BUFFER_BIT | (blitStencil ? gl.STENCIL_BUFFER_BIT : 0);
  99. }
  100. gl.blitFramebuffer(
  101. 0,
  102. 0,
  103. this._width,
  104. this._height,
  105. 0,
  106. 0,
  107. this._width,
  108. this._height,
  109. mask,
  110. gl.NEAREST,
  111. );
  112. gl.bindFramebuffer(gl.READ_FRAMEBUFFER, null);
  113. gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, null);
  114. };
  115. MultisampleFramebuffer.prototype.isDestroyed = function () {
  116. return false;
  117. };
  118. MultisampleFramebuffer.prototype.destroy = function () {
  119. this._renderFramebuffer.destroy();
  120. this._colorFramebuffer.destroy();
  121. return destroyObject(this);
  122. };
  123. export default MultisampleFramebuffer;