GlobeTranslucencyFramebuffer.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 PixelFormat from "../Core/PixelFormat.js";
  6. import ClearCommand from "../Renderer/ClearCommand.js";
  7. import Framebuffer from "../Renderer/Framebuffer.js";
  8. import PixelDatatype from "../Renderer/PixelDatatype.js";
  9. import Renderbuffer from "../Renderer/Renderbuffer.js";
  10. import RenderbufferFormat from "../Renderer/RenderbufferFormat.js";
  11. import RenderState from "../Renderer/RenderState.js";
  12. import Sampler from "../Renderer/Sampler.js";
  13. import Texture from "../Renderer/Texture.js";
  14. import PassThroughDepth from "../Shaders/PostProcessStages/PassThroughDepth.js";
  15. /**
  16. * @private
  17. */
  18. function GlobeTranslucencyFramebuffer() {
  19. this._colorTexture = undefined;
  20. this._depthStencilTexture = undefined;
  21. this._depthStencilRenderbuffer = undefined;
  22. this._framebuffer = undefined;
  23. this._packedDepthTexture = undefined;
  24. this._packedDepthFramebuffer = undefined;
  25. this._renderState = undefined;
  26. this._packedDepthCommand = undefined;
  27. this._clearCommand = undefined;
  28. this._viewport = new BoundingRectangle();
  29. this._useScissorTest = false;
  30. this._scissorRectangle = undefined;
  31. this._useHdr = undefined;
  32. }
  33. Object.defineProperties(GlobeTranslucencyFramebuffer.prototype, {
  34. classificationTexture: {
  35. get: function () {
  36. return this._colorTexture;
  37. },
  38. },
  39. classificationFramebuffer: {
  40. get: function () {
  41. return this._framebuffer;
  42. },
  43. },
  44. });
  45. function destroyResources(globeTranslucency) {
  46. globeTranslucency._colorTexture =
  47. globeTranslucency._colorTexture &&
  48. !globeTranslucency._colorTexture.isDestroyed() &&
  49. globeTranslucency._colorTexture.destroy();
  50. globeTranslucency._depthStencilTexture =
  51. globeTranslucency._depthStencilTexture &&
  52. !globeTranslucency._depthStencilTexture.isDestroyed() &&
  53. globeTranslucency._depthStencilTexture.destroy();
  54. globeTranslucency._depthStencilRenderbuffer =
  55. globeTranslucency._depthStencilRenderbuffer &&
  56. !globeTranslucency._depthStencilRenderbuffer.isDestroyed() &&
  57. globeTranslucency._depthStencilRenderbuffer.destroy();
  58. globeTranslucency._framebuffer =
  59. globeTranslucency._framebuffer &&
  60. !globeTranslucency._framebuffer.isDestroyed() &&
  61. globeTranslucency._framebuffer.destroy();
  62. globeTranslucency._packedDepthTexture =
  63. globeTranslucency._packedDepthTexture &&
  64. !globeTranslucency._packedDepthTexture.isDestroyed() &&
  65. globeTranslucency._packedDepthTexture.destroy();
  66. globeTranslucency._packedDepthFramebuffer =
  67. globeTranslucency._packedDepthFramebuffer &&
  68. !globeTranslucency._packedDepthFramebuffer.isDestroyed() &&
  69. globeTranslucency._packedDepthFramebuffer.destroy();
  70. }
  71. function createResources(globeTranslucency, context, width, height, hdr) {
  72. var pixelDatatype = hdr
  73. ? context.halfFloatingPointTexture
  74. ? PixelDatatype.HALF_FLOAT
  75. : PixelDatatype.FLOAT
  76. : PixelDatatype.UNSIGNED_BYTE;
  77. globeTranslucency._colorTexture = new Texture({
  78. context: context,
  79. width: width,
  80. height: height,
  81. pixelFormat: PixelFormat.RGBA,
  82. pixelDatatype: pixelDatatype,
  83. sampler: Sampler.NEAREST,
  84. });
  85. if (context.depthTexture) {
  86. globeTranslucency._depthStencilTexture = new Texture({
  87. context: context,
  88. width: width,
  89. height: height,
  90. pixelFormat: PixelFormat.DEPTH_STENCIL,
  91. pixelDatatype: PixelDatatype.UNSIGNED_INT_24_8,
  92. });
  93. } else {
  94. globeTranslucency._depthStencilRenderbuffer = new Renderbuffer({
  95. context: context,
  96. width: width,
  97. height: height,
  98. format: RenderbufferFormat.DEPTH_STENCIL,
  99. });
  100. }
  101. globeTranslucency._framebuffer = new Framebuffer({
  102. context: context,
  103. colorTextures: [globeTranslucency._colorTexture],
  104. depthStencilTexture: globeTranslucency._depthStencilTexture,
  105. depthStencilRenderbuffer: globeTranslucency._depthStencilRenderbuffer,
  106. destroyAttachments: false,
  107. });
  108. globeTranslucency._packedDepthTexture = new Texture({
  109. context: context,
  110. width: width,
  111. height: height,
  112. pixelFormat: PixelFormat.RGBA,
  113. pixelDatatype: PixelDatatype.UNSIGNED_BYTE,
  114. sampler: Sampler.NEAREST,
  115. });
  116. globeTranslucency._packedDepthFramebuffer = new Framebuffer({
  117. context: context,
  118. colorTextures: [globeTranslucency._packedDepthTexture],
  119. destroyAttachments: false,
  120. });
  121. }
  122. function updateResources(globeTranslucency, context, width, height, hdr) {
  123. var colorTexture = globeTranslucency._colorTexture;
  124. var textureChanged =
  125. !defined(colorTexture) ||
  126. colorTexture.width !== width ||
  127. colorTexture.height !== height ||
  128. hdr !== globeTranslucency._useHdr;
  129. if (textureChanged) {
  130. destroyResources(globeTranslucency);
  131. createResources(globeTranslucency, context, width, height, hdr);
  132. }
  133. }
  134. function updateCommands(globeTranslucency, context, width, height, passState) {
  135. globeTranslucency._viewport.width = width;
  136. globeTranslucency._viewport.height = height;
  137. var useScissorTest = !BoundingRectangle.equals(
  138. globeTranslucency._viewport,
  139. passState.viewport
  140. );
  141. var updateScissor = useScissorTest !== globeTranslucency._useScissorTest;
  142. globeTranslucency._useScissorTest = useScissorTest;
  143. if (
  144. !BoundingRectangle.equals(
  145. globeTranslucency._scissorRectangle,
  146. passState.viewport
  147. )
  148. ) {
  149. globeTranslucency._scissorRectangle = BoundingRectangle.clone(
  150. passState.viewport,
  151. globeTranslucency._scissorRectangle
  152. );
  153. updateScissor = true;
  154. }
  155. if (
  156. !defined(globeTranslucency._renderState) ||
  157. !BoundingRectangle.equals(
  158. globeTranslucency._viewport,
  159. globeTranslucency._renderState.viewport
  160. ) ||
  161. updateScissor
  162. ) {
  163. globeTranslucency._renderState = RenderState.fromCache({
  164. viewport: globeTranslucency._viewport,
  165. scissorTest: {
  166. enabled: globeTranslucency._useScissorTest,
  167. rectangle: globeTranslucency._scissorRectangle,
  168. },
  169. });
  170. }
  171. if (!defined(globeTranslucency._packedDepthCommand)) {
  172. globeTranslucency._packedDepthCommand = context.createViewportQuadCommand(
  173. PassThroughDepth,
  174. {
  175. uniformMap: {
  176. u_depthTexture: function () {
  177. return globeTranslucency._depthStencilTexture;
  178. },
  179. },
  180. owner: globeTranslucency,
  181. }
  182. );
  183. }
  184. if (!defined(globeTranslucency._clearCommand)) {
  185. globeTranslucency._clearCommand = new ClearCommand({
  186. color: new Color(0.0, 0.0, 0.0, 0.0),
  187. depth: 1.0,
  188. stencil: 0.0,
  189. owner: globeTranslucency,
  190. });
  191. }
  192. globeTranslucency._packedDepthCommand.framebuffer =
  193. globeTranslucency._packedDepthFramebuffer;
  194. globeTranslucency._packedDepthCommand.renderState =
  195. globeTranslucency._renderState;
  196. globeTranslucency._clearCommand.framebuffer = globeTranslucency._framebuffer;
  197. globeTranslucency._clearCommand.renderState = globeTranslucency._renderState;
  198. }
  199. GlobeTranslucencyFramebuffer.prototype.updateAndClear = function (
  200. hdr,
  201. viewport,
  202. context,
  203. passState
  204. ) {
  205. var width = viewport.width;
  206. var height = viewport.height;
  207. updateResources(this, context, width, height, hdr);
  208. updateCommands(this, context, width, height, passState);
  209. this._useHdr = hdr;
  210. };
  211. GlobeTranslucencyFramebuffer.prototype.clearClassification = function (
  212. context,
  213. passState
  214. ) {
  215. this._clearCommand.execute(context, passState);
  216. };
  217. GlobeTranslucencyFramebuffer.prototype.packDepth = function (
  218. context,
  219. passState
  220. ) {
  221. this._packedDepthCommand.execute(context, passState);
  222. return this._packedDepthTexture;
  223. };
  224. GlobeTranslucencyFramebuffer.prototype.isDestroyed = function () {
  225. return false;
  226. };
  227. GlobeTranslucencyFramebuffer.prototype.destroy = function () {
  228. destroyResources(this);
  229. return destroyObject(this);
  230. };
  231. export default GlobeTranslucencyFramebuffer;