GlobeTranslucencyFramebuffer.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import BoundingRectangle from "../Core/BoundingRectangle.js";
  2. import Color from "../Core/Color.js";
  3. import defined from "../Core/defined.js";
  4. import destroyObject from "../Core/destroyObject.js";
  5. import ClearCommand from "../Renderer/ClearCommand.js";
  6. import FramebufferManager from "../Renderer/FramebufferManager.js";
  7. import PixelDatatype from "../Renderer/PixelDatatype.js";
  8. import RenderState from "../Renderer/RenderState.js";
  9. import PassThroughDepth from "../Shaders/PostProcessStages/PassThroughDepth.js";
  10. /**
  11. * @private
  12. */
  13. function GlobeTranslucencyFramebuffer() {
  14. this._framebuffer = new FramebufferManager({
  15. depthStencil: true,
  16. supportsDepthTexture: true,
  17. });
  18. this._packedDepthFramebuffer = new FramebufferManager();
  19. this._renderState = undefined;
  20. this._packedDepthCommand = undefined;
  21. this._clearCommand = undefined;
  22. this._viewport = new BoundingRectangle();
  23. this._useScissorTest = false;
  24. this._scissorRectangle = undefined;
  25. this._useHdr = undefined;
  26. }
  27. Object.defineProperties(GlobeTranslucencyFramebuffer.prototype, {
  28. // Exposed for testing
  29. classificationTexture: {
  30. get: function () {
  31. return this._framebuffer.getColorTexture();
  32. },
  33. },
  34. classificationFramebuffer: {
  35. get: function () {
  36. return this._framebuffer.framebuffer;
  37. },
  38. },
  39. // Exposed for testing
  40. packedDepthFramebuffer: {
  41. get: function () {
  42. return this._packedDepthFramebuffer.framebuffer;
  43. },
  44. },
  45. depthStencilTexture: {
  46. get: function () {
  47. return this._framebuffer.getDepthStencilTexture();
  48. },
  49. },
  50. // Exposed for testing
  51. depthStencilRenderbuffer: {
  52. get: function () {
  53. return this._framebuffer.getDepthStencilRenderbuffer();
  54. },
  55. },
  56. packedDepthTexture: {
  57. get: function () {
  58. return this._packedDepthFramebuffer.getColorTexture();
  59. },
  60. },
  61. });
  62. function destroyResources(globeTranslucency) {
  63. globeTranslucency._framebuffer.destroy();
  64. globeTranslucency._packedDepthFramebuffer.destroy();
  65. }
  66. function updateResources(globeTranslucency, context, width, height, hdr) {
  67. const pixelDatatype = hdr
  68. ? context.halfFloatingPointTexture
  69. ? PixelDatatype.HALF_FLOAT
  70. : PixelDatatype.FLOAT
  71. : PixelDatatype.UNSIGNED_BYTE;
  72. globeTranslucency._framebuffer.update(
  73. context,
  74. width,
  75. height,
  76. 1,
  77. pixelDatatype
  78. );
  79. globeTranslucency._packedDepthFramebuffer.update(context, width, height);
  80. }
  81. function updateCommands(globeTranslucency, context, width, height, passState) {
  82. globeTranslucency._viewport.width = width;
  83. globeTranslucency._viewport.height = height;
  84. const useScissorTest = !BoundingRectangle.equals(
  85. globeTranslucency._viewport,
  86. passState.viewport
  87. );
  88. let updateScissor = useScissorTest !== globeTranslucency._useScissorTest;
  89. globeTranslucency._useScissorTest = useScissorTest;
  90. if (
  91. !BoundingRectangle.equals(
  92. globeTranslucency._scissorRectangle,
  93. passState.viewport
  94. )
  95. ) {
  96. globeTranslucency._scissorRectangle = BoundingRectangle.clone(
  97. passState.viewport,
  98. globeTranslucency._scissorRectangle
  99. );
  100. updateScissor = true;
  101. }
  102. if (
  103. !defined(globeTranslucency._renderState) ||
  104. !BoundingRectangle.equals(
  105. globeTranslucency._viewport,
  106. globeTranslucency._renderState.viewport
  107. ) ||
  108. updateScissor
  109. ) {
  110. globeTranslucency._renderState = RenderState.fromCache({
  111. viewport: globeTranslucency._viewport,
  112. scissorTest: {
  113. enabled: globeTranslucency._useScissorTest,
  114. rectangle: globeTranslucency._scissorRectangle,
  115. },
  116. });
  117. }
  118. if (!defined(globeTranslucency._packedDepthCommand)) {
  119. globeTranslucency._packedDepthCommand = context.createViewportQuadCommand(
  120. PassThroughDepth,
  121. {
  122. uniformMap: {
  123. u_depthTexture: function () {
  124. return globeTranslucency.depthStencilTexture;
  125. },
  126. },
  127. owner: globeTranslucency,
  128. }
  129. );
  130. }
  131. if (!defined(globeTranslucency._clearCommand)) {
  132. globeTranslucency._clearCommand = new ClearCommand({
  133. color: new Color(0.0, 0.0, 0.0, 0.0),
  134. depth: 1.0,
  135. stencil: 0.0,
  136. owner: globeTranslucency,
  137. });
  138. }
  139. globeTranslucency._packedDepthCommand.framebuffer =
  140. globeTranslucency._packedDepthFramebuffer.framebuffer;
  141. globeTranslucency._packedDepthCommand.renderState =
  142. globeTranslucency._renderState;
  143. globeTranslucency._clearCommand.framebuffer =
  144. globeTranslucency.classificationFramebuffer;
  145. globeTranslucency._clearCommand.renderState = globeTranslucency._renderState;
  146. }
  147. GlobeTranslucencyFramebuffer.prototype.updateAndClear = function (
  148. hdr,
  149. viewport,
  150. context,
  151. passState
  152. ) {
  153. const width = viewport.width;
  154. const height = viewport.height;
  155. updateResources(this, context, width, height, hdr);
  156. updateCommands(this, context, width, height, passState);
  157. this._useHdr = hdr;
  158. };
  159. GlobeTranslucencyFramebuffer.prototype.clearClassification = function (
  160. context,
  161. passState
  162. ) {
  163. this._clearCommand.execute(context, passState);
  164. };
  165. GlobeTranslucencyFramebuffer.prototype.packDepth = function (
  166. context,
  167. passState
  168. ) {
  169. this._packedDepthCommand.execute(context, passState);
  170. return this.packedDepthTexture;
  171. };
  172. GlobeTranslucencyFramebuffer.prototype.isDestroyed = function () {
  173. return false;
  174. };
  175. GlobeTranslucencyFramebuffer.prototype.destroy = function () {
  176. destroyResources(this);
  177. return destroyObject(this);
  178. };
  179. export default GlobeTranslucencyFramebuffer;