FramebufferManager.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. import Framebuffer from "./Framebuffer.js";
  2. import MultisampleFramebuffer from "./MultisampleFramebuffer.js";
  3. import PixelDatatype from "./PixelDatatype.js";
  4. import Renderbuffer from "./Renderbuffer.js";
  5. import RenderbufferFormat from "./RenderbufferFormat.js";
  6. import Sampler from "./Sampler.js";
  7. import Texture from "./Texture.js";
  8. import defaultValue from "../Core/defaultValue.js";
  9. import defined from "../Core/defined.js";
  10. import DeveloperError from "../Core/DeveloperError.js";
  11. import PixelFormat from "../Core/PixelFormat.js";
  12. /**
  13. * Creates a wrapper object around a framebuffer and its resources.
  14. *
  15. * @param {object} options Object with the following properties:
  16. * @param {number} [options.numSamples=1] The multisampling rate of the render targets. Requires a WebGL2 context.
  17. * @param {number} [options.colorAttachmentsLength=1] The number of color attachments this FramebufferManager will create.
  18. * @param {boolean} [options.color=true] Whether the FramebufferManager will use color attachments.
  19. * @param {boolean} [options.depth=false] Whether the FramebufferManager will use depth attachments.
  20. * @param {boolean} [options.depthStencil=false] Whether the FramebufferManager will use depth-stencil attachments.
  21. * @param {boolean} [options.supportsDepthTexture=false] Whether the FramebufferManager will create a depth texture when the extension is supported.
  22. * @param {boolean} [options.createColorAttachments=true] Whether the FramebufferManager will construct its own color attachments.
  23. * @param {boolean} [options.createDepthAttachments=true] Whether the FramebufferManager will construct its own depth attachments.
  24. * @param {PixelDatatype} [options.pixelDatatype=undefined] The default pixel datatype to use when creating color attachments.
  25. * @param {PixelFormat} [options.pixelFormat=undefined] The default pixel format to use when creating color attachments.
  26. *
  27. * @exception {DeveloperError} Must enable at least one type of framebuffer attachment.
  28. * @exception {DeveloperError} Cannot have both a depth and depth-stencil attachment.
  29. *
  30. * @private
  31. * @constructor
  32. */
  33. function FramebufferManager(options) {
  34. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  35. this._numSamples = defaultValue(options.numSamples, 1);
  36. this._colorAttachmentsLength = defaultValue(
  37. options.colorAttachmentsLength,
  38. 1
  39. );
  40. this._color = defaultValue(options.color, true);
  41. this._depth = defaultValue(options.depth, false);
  42. this._depthStencil = defaultValue(options.depthStencil, false);
  43. this._supportsDepthTexture = defaultValue(
  44. options.supportsDepthTexture,
  45. false
  46. );
  47. //>>includeStart('debug', pragmas.debug);
  48. if (!this._color && !this._depth && !this._depthStencil) {
  49. throw new DeveloperError(
  50. "Must enable at least one type of framebuffer attachment."
  51. );
  52. }
  53. if (this._depth && this._depthStencil) {
  54. throw new DeveloperError(
  55. "Cannot have both a depth and depth-stencil attachment."
  56. );
  57. }
  58. //>>includeEnd('debug');
  59. this._createColorAttachments = defaultValue(
  60. options.createColorAttachments,
  61. true
  62. );
  63. this._createDepthAttachments = defaultValue(
  64. options.createDepthAttachments,
  65. true
  66. );
  67. this._pixelDatatype = options.pixelDatatype;
  68. this._pixelFormat = options.pixelFormat;
  69. this._width = undefined;
  70. this._height = undefined;
  71. this._framebuffer = undefined;
  72. this._multisampleFramebuffer = undefined;
  73. this._colorTextures = undefined;
  74. if (this._color) {
  75. this._colorTextures = new Array(this._colorAttachmentsLength);
  76. this._colorRenderbuffers = new Array(this._colorAttachmentsLength);
  77. }
  78. this._colorRenderbuffer = undefined;
  79. this._depthStencilRenderbuffer = undefined;
  80. this._depthStencilTexture = undefined;
  81. this._depthRenderbuffer = undefined;
  82. this._depthTexture = undefined;
  83. this._attachmentsDirty = false;
  84. }
  85. Object.defineProperties(FramebufferManager.prototype, {
  86. framebuffer: {
  87. get: function () {
  88. if (this._numSamples > 1) {
  89. return this._multisampleFramebuffer.getRenderFramebuffer();
  90. }
  91. return this._framebuffer;
  92. },
  93. },
  94. numSamples: {
  95. get: function () {
  96. return this._numSamples;
  97. },
  98. },
  99. status: {
  100. get: function () {
  101. return this.framebuffer.status;
  102. },
  103. },
  104. });
  105. FramebufferManager.prototype.isDirty = function (
  106. width,
  107. height,
  108. numSamples,
  109. pixelDatatype,
  110. pixelFormat
  111. ) {
  112. numSamples = defaultValue(numSamples, 1);
  113. const dimensionChanged = this._width !== width || this._height !== height;
  114. const samplesChanged = this._numSamples !== numSamples;
  115. const pixelChanged =
  116. (defined(pixelDatatype) && this._pixelDatatype !== pixelDatatype) ||
  117. (defined(pixelFormat) && this._pixelFormat !== pixelFormat);
  118. const framebufferDefined =
  119. numSamples === 1
  120. ? defined(this._framebuffer)
  121. : defined(this._multisampleFramebuffer);
  122. return (
  123. this._attachmentsDirty ||
  124. dimensionChanged ||
  125. samplesChanged ||
  126. pixelChanged ||
  127. !framebufferDefined ||
  128. (this._color && !defined(this._colorTextures[0]))
  129. );
  130. };
  131. FramebufferManager.prototype.update = function (
  132. context,
  133. width,
  134. height,
  135. numSamples,
  136. pixelDatatype,
  137. pixelFormat
  138. ) {
  139. //>>includeStart('debug', pragmas.debug);
  140. if (!defined(width) || !defined(height)) {
  141. throw new DeveloperError("width and height must be defined.");
  142. }
  143. //>>includeEnd('debug');
  144. numSamples = context.msaa ? defaultValue(numSamples, 1) : 1;
  145. pixelDatatype = defaultValue(
  146. pixelDatatype,
  147. this._color
  148. ? defaultValue(this._pixelDatatype, PixelDatatype.UNSIGNED_BYTE)
  149. : undefined
  150. );
  151. pixelFormat = defaultValue(
  152. pixelFormat,
  153. this._color ? defaultValue(this._pixelFormat, PixelFormat.RGBA) : undefined
  154. );
  155. if (this.isDirty(width, height, numSamples, pixelDatatype, pixelFormat)) {
  156. this.destroy();
  157. this._width = width;
  158. this._height = height;
  159. this._numSamples = numSamples;
  160. this._pixelDatatype = pixelDatatype;
  161. this._pixelFormat = pixelFormat;
  162. this._attachmentsDirty = false;
  163. // Create color texture
  164. if (this._color && this._createColorAttachments) {
  165. for (let i = 0; i < this._colorAttachmentsLength; ++i) {
  166. this._colorTextures[i] = new Texture({
  167. context: context,
  168. width: width,
  169. height: height,
  170. pixelFormat: pixelFormat,
  171. pixelDatatype: pixelDatatype,
  172. sampler: Sampler.NEAREST,
  173. });
  174. if (this._numSamples > 1) {
  175. const format = RenderbufferFormat.getColorFormat(pixelDatatype);
  176. this._colorRenderbuffers[i] = new Renderbuffer({
  177. context: context,
  178. width: width,
  179. height: height,
  180. format: format,
  181. numSamples: this._numSamples,
  182. });
  183. }
  184. }
  185. }
  186. // Create depth stencil texture or renderbuffer
  187. if (this._depthStencil && this._createDepthAttachments) {
  188. if (this._supportsDepthTexture && context.depthTexture) {
  189. this._depthStencilTexture = new Texture({
  190. context: context,
  191. width: width,
  192. height: height,
  193. pixelFormat: PixelFormat.DEPTH_STENCIL,
  194. pixelDatatype: PixelDatatype.UNSIGNED_INT_24_8,
  195. sampler: Sampler.NEAREST,
  196. });
  197. if (this._numSamples > 1) {
  198. this._depthStencilRenderbuffer = new Renderbuffer({
  199. context: context,
  200. width: width,
  201. height: height,
  202. format: RenderbufferFormat.DEPTH24_STENCIL8,
  203. numSamples: this._numSamples,
  204. });
  205. }
  206. } else {
  207. this._depthStencilRenderbuffer = new Renderbuffer({
  208. context: context,
  209. width: width,
  210. height: height,
  211. format: RenderbufferFormat.DEPTH_STENCIL,
  212. });
  213. }
  214. }
  215. // Create depth texture
  216. if (this._depth && this._createDepthAttachments) {
  217. if (this._supportsDepthTexture && context.depthTexture) {
  218. this._depthTexture = new Texture({
  219. context: context,
  220. width: width,
  221. height: height,
  222. pixelFormat: PixelFormat.DEPTH_COMPONENT,
  223. pixelDatatype: PixelDatatype.UNSIGNED_INT,
  224. sampler: Sampler.NEAREST,
  225. });
  226. } else {
  227. this._depthRenderbuffer = new Renderbuffer({
  228. context: context,
  229. width: width,
  230. height: height,
  231. format: RenderbufferFormat.DEPTH_COMPONENT16,
  232. });
  233. }
  234. }
  235. if (this._numSamples > 1) {
  236. this._multisampleFramebuffer = new MultisampleFramebuffer({
  237. context: context,
  238. width: this._width,
  239. height: this._height,
  240. colorTextures: this._colorTextures,
  241. colorRenderbuffers: this._colorRenderbuffers,
  242. depthStencilTexture: this._depthStencilTexture,
  243. depthStencilRenderbuffer: this._depthStencilRenderbuffer,
  244. destroyAttachments: false,
  245. });
  246. } else {
  247. this._framebuffer = new Framebuffer({
  248. context: context,
  249. colorTextures: this._colorTextures,
  250. depthTexture: this._depthTexture,
  251. depthRenderbuffer: this._depthRenderbuffer,
  252. depthStencilTexture: this._depthStencilTexture,
  253. depthStencilRenderbuffer: this._depthStencilRenderbuffer,
  254. destroyAttachments: false,
  255. });
  256. }
  257. }
  258. };
  259. FramebufferManager.prototype.getColorTexture = function (index) {
  260. index = defaultValue(index, 0);
  261. //>>includeStart('debug', pragmas.debug);
  262. if (index >= this._colorAttachmentsLength) {
  263. throw new DeveloperError(
  264. "index must be smaller than total number of color attachments."
  265. );
  266. }
  267. //>>includeEnd('debug');
  268. return this._colorTextures[index];
  269. };
  270. FramebufferManager.prototype.setColorTexture = function (texture, index) {
  271. index = defaultValue(index, 0);
  272. //>>includeStart('debug', pragmas.debug);
  273. if (this._createColorAttachments) {
  274. throw new DeveloperError(
  275. "createColorAttachments must be false if setColorTexture is called."
  276. );
  277. }
  278. if (index >= this._colorAttachmentsLength) {
  279. throw new DeveloperError(
  280. "index must be smaller than total number of color attachments."
  281. );
  282. }
  283. //>>includeEnd('debug');
  284. this._attachmentsDirty = texture !== this._colorTextures[index];
  285. this._colorTextures[index] = texture;
  286. };
  287. FramebufferManager.prototype.getColorRenderbuffer = function (index) {
  288. index = defaultValue(index, 0);
  289. //>>includeStart('debug', pragmas.debug);
  290. if (index >= this._colorAttachmentsLength) {
  291. throw new DeveloperError(
  292. "index must be smaller than total number of color attachments."
  293. );
  294. }
  295. //>>includeEnd('debug');
  296. return this._colorRenderbuffers[index];
  297. };
  298. FramebufferManager.prototype.setColorRenderbuffer = function (
  299. renderbuffer,
  300. index
  301. ) {
  302. index = defaultValue(index, 0);
  303. //>>includeStart('debug', pragmas.debug);
  304. if (this._createColorAttachments) {
  305. throw new DeveloperError(
  306. "createColorAttachments must be false if setColorRenderbuffer is called."
  307. );
  308. }
  309. if (index >= this._colorAttachmentsLength) {
  310. throw new DeveloperError(
  311. "index must be smaller than total number of color attachments."
  312. );
  313. }
  314. //>>includeEnd('debug');
  315. this._attachmentsDirty = renderbuffer !== this._colorRenderbuffers[index];
  316. this._colorRenderbuffers[index] = renderbuffer;
  317. };
  318. FramebufferManager.prototype.getDepthRenderbuffer = function () {
  319. return this._depthRenderbuffer;
  320. };
  321. FramebufferManager.prototype.setDepthRenderbuffer = function (renderbuffer) {
  322. //>>includeStart('debug', pragmas.debug);
  323. if (this._createDepthAttachments) {
  324. throw new DeveloperError(
  325. "createDepthAttachments must be false if setDepthRenderbuffer is called."
  326. );
  327. }
  328. //>>includeEnd('debug');
  329. this._attachmentsDirty = renderbuffer !== this._depthRenderbuffer;
  330. this._depthRenderbuffer = renderbuffer;
  331. };
  332. FramebufferManager.prototype.getDepthTexture = function () {
  333. return this._depthTexture;
  334. };
  335. FramebufferManager.prototype.setDepthTexture = function (texture) {
  336. //>>includeStart('debug', pragmas.debug);
  337. if (this._createDepthAttachments) {
  338. throw new DeveloperError(
  339. "createDepthAttachments must be false if setDepthTexture is called."
  340. );
  341. }
  342. //>>includeEnd('debug');
  343. this._attachmentsDirty = texture !== this._depthTexture;
  344. this._depthTexture = texture;
  345. };
  346. FramebufferManager.prototype.getDepthStencilRenderbuffer = function () {
  347. return this._depthStencilRenderbuffer;
  348. };
  349. FramebufferManager.prototype.setDepthStencilRenderbuffer = function (
  350. renderbuffer
  351. ) {
  352. //>>includeStart('debug', pragmas.debug);
  353. if (this._createDepthAttachments) {
  354. throw new DeveloperError(
  355. "createDepthAttachments must be false if setDepthStencilRenderbuffer is called."
  356. );
  357. }
  358. //>>includeEnd('debug');
  359. this._attachmentsDirty = renderbuffer !== this._depthStencilRenderbuffer;
  360. this._depthStencilRenderbuffer = renderbuffer;
  361. };
  362. FramebufferManager.prototype.getDepthStencilTexture = function () {
  363. return this._depthStencilTexture;
  364. };
  365. FramebufferManager.prototype.setDepthStencilTexture = function (texture) {
  366. //>>includeStart('debug', pragmas.debug);
  367. if (this._createDepthAttachments) {
  368. throw new DeveloperError(
  369. "createDepthAttachments must be false if setDepthStencilTexture is called."
  370. );
  371. }
  372. //>>includeEnd('debug');
  373. this._attachmentsDirty = texture !== this._depthStencilTexture;
  374. this._depthStencilTexture = texture;
  375. };
  376. FramebufferManager.prototype.prepareTextures = function (context, blitStencil) {
  377. if (this._numSamples > 1) {
  378. this._multisampleFramebuffer.blitFramebuffers(context, blitStencil);
  379. }
  380. };
  381. FramebufferManager.prototype.clear = function (
  382. context,
  383. clearCommand,
  384. passState
  385. ) {
  386. const framebuffer = clearCommand.framebuffer;
  387. clearCommand.framebuffer = this.framebuffer;
  388. clearCommand.execute(context, passState);
  389. clearCommand.framebuffer = framebuffer;
  390. };
  391. FramebufferManager.prototype.destroyFramebuffer = function () {
  392. this._framebuffer = this._framebuffer && this._framebuffer.destroy();
  393. this._multisampleFramebuffer =
  394. this._multisampleFramebuffer && this._multisampleFramebuffer.destroy();
  395. };
  396. FramebufferManager.prototype.destroy = function () {
  397. if (this._color) {
  398. let i;
  399. const length = this._colorTextures.length;
  400. for (i = 0; i < length; ++i) {
  401. const texture = this._colorTextures[i];
  402. if (this._createColorAttachments) {
  403. if (defined(texture) && !texture.isDestroyed()) {
  404. this._colorTextures[i].destroy();
  405. this._colorTextures[i] = undefined;
  406. }
  407. }
  408. if (defined(texture) && texture.isDestroyed()) {
  409. this._colorTextures[i] = undefined;
  410. }
  411. const renderbuffer = this._colorRenderbuffers[i];
  412. if (this._createColorAttachments) {
  413. if (defined(renderbuffer) && !renderbuffer.isDestroyed()) {
  414. this._colorRenderbuffers[i].destroy();
  415. this._colorRenderbuffers[i] = undefined;
  416. }
  417. }
  418. if (defined(renderbuffer) && renderbuffer.isDestroyed()) {
  419. this._colorRenderbuffers[i] = undefined;
  420. }
  421. }
  422. }
  423. if (this._depthStencil) {
  424. if (this._createDepthAttachments) {
  425. this._depthStencilTexture =
  426. this._depthStencilTexture && this._depthStencilTexture.destroy();
  427. this._depthStencilRenderbuffer =
  428. this._depthStencilRenderbuffer &&
  429. this._depthStencilRenderbuffer.destroy();
  430. }
  431. if (
  432. defined(this._depthStencilTexture) &&
  433. this._depthStencilTexture.isDestroyed()
  434. ) {
  435. this._depthStencilTexture = undefined;
  436. }
  437. if (
  438. defined(this._depthStencilRenderbuffer) &&
  439. this._depthStencilRenderbuffer.isDestroyed()
  440. ) {
  441. this._depthStencilRenderbuffer = undefined;
  442. }
  443. }
  444. if (this._depth) {
  445. if (this._createDepthAttachments) {
  446. this._depthTexture = this._depthTexture && this._depthTexture.destroy();
  447. this._depthRenderbuffer =
  448. this._depthRenderbuffer && this._depthRenderbuffer.destroy();
  449. }
  450. if (defined(this._depthTexture) && this._depthTexture.isDestroyed()) {
  451. this._depthTexture = undefined;
  452. }
  453. if (
  454. defined(this._depthRenderbuffer) &&
  455. this._depthRenderbuffer.isDestroyed()
  456. ) {
  457. this._depthRenderbuffer = undefined;
  458. }
  459. }
  460. this.destroyFramebuffer();
  461. };
  462. export default FramebufferManager;