MultisampleFramebuffer.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import Check from "../Core/Check.js";
  2. import defaultValue from "../Core/defaultValue.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 The initial framebuffer attachments. <code>context</code>, <code>width</code>, and <code>height</code> are required. The possible properties are <code>colorTextures</code>, <code>colorRenderbuffers</code>, <code>depthStencilTexture</code>, <code>depthStencilRenderbuffer</code>, and <code>destroyAttachments</code>.
  15. *
  16. * @exception {DeveloperError} Both color renderbuffer and texture attachments must be provided.
  17. * @exception {DeveloperError} Both depth-stencil renderbuffer and texture attachments must be provided.
  18. *
  19. * @private
  20. * @constructor
  21. */
  22. function MultisampleFramebuffer(options) {
  23. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  24. const context = options.context;
  25. const width = options.width;
  26. const height = options.height;
  27. //>>includeStart('debug', pragmas.debug);
  28. Check.defined("options.context", context);
  29. Check.defined("options.width", width);
  30. Check.defined("options.height", height);
  31. //>>includeEnd('debug');
  32. this._width = width;
  33. this._height = height;
  34. const colorRenderbuffers = options.colorRenderbuffers;
  35. const colorTextures = options.colorTextures;
  36. if (defined(colorRenderbuffers) !== defined(colorTextures)) {
  37. throw new DeveloperError(
  38. "Both color renderbuffer and texture attachments must be provided."
  39. );
  40. }
  41. const depthStencilRenderbuffer = options.depthStencilRenderbuffer;
  42. const depthStencilTexture = options.depthStencilTexture;
  43. if (defined(depthStencilRenderbuffer) !== defined(depthStencilTexture)) {
  44. throw new DeveloperError(
  45. "Both depth-stencil renderbuffer and texture attachments must be provided."
  46. );
  47. }
  48. this._renderFramebuffer = new Framebuffer({
  49. context: context,
  50. colorRenderbuffers: colorRenderbuffers,
  51. depthStencilRenderbuffer: depthStencilRenderbuffer,
  52. destroyAttachments: options.destroyAttachments,
  53. });
  54. this._colorFramebuffer = new Framebuffer({
  55. context: context,
  56. colorTextures: colorTextures,
  57. depthStencilTexture: depthStencilTexture,
  58. destroyAttachments: options.destroyAttachments,
  59. });
  60. }
  61. MultisampleFramebuffer.prototype.getRenderFramebuffer = function () {
  62. return this._renderFramebuffer;
  63. };
  64. MultisampleFramebuffer.prototype.getColorFramebuffer = function () {
  65. return this._colorFramebuffer;
  66. };
  67. MultisampleFramebuffer.prototype.blitFramebuffers = function (
  68. context,
  69. blitStencil
  70. ) {
  71. this._renderFramebuffer.bindRead();
  72. this._colorFramebuffer.bindDraw();
  73. const gl = context._gl;
  74. let mask = 0;
  75. if (this._colorFramebuffer._colorTextures.length > 0) {
  76. mask |= gl.COLOR_BUFFER_BIT;
  77. }
  78. if (defined(this._colorFramebuffer.depthStencilTexture)) {
  79. mask |= gl.DEPTH_BUFFER_BIT | (blitStencil ? gl.STENCIL_BUFFER_BIT : 0);
  80. }
  81. gl.blitFramebuffer(
  82. 0,
  83. 0,
  84. this._width,
  85. this._height,
  86. 0,
  87. 0,
  88. this._width,
  89. this._height,
  90. mask,
  91. gl.NEAREST
  92. );
  93. gl.bindFramebuffer(gl.READ_FRAMEBUFFER, null);
  94. gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, null);
  95. };
  96. MultisampleFramebuffer.prototype.isDestroyed = function () {
  97. return false;
  98. };
  99. MultisampleFramebuffer.prototype.destroy = function () {
  100. this._renderFramebuffer.destroy();
  101. this._colorFramebuffer.destroy();
  102. return destroyObject(this);
  103. };
  104. export default MultisampleFramebuffer;