internalTexture.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. import { Observable } from "../../Misc/observable";
  2. import { Nullable, int } from "../../types";
  3. import { RenderTargetCreationOptions } from "../../Materials/Textures/renderTargetCreationOptions";
  4. import { _TimeToken } from "../../Instrumentation/timeToken";
  5. import { Constants } from "../../Engines/constants";
  6. import { _DevTools } from '../../Misc/devTools';
  7. import { Engine } from '../../Engines/engine';
  8. declare type ThinEngine = import("../../Engines/thinEngine").ThinEngine;
  9. declare type BaseTexture = import("../../Materials/Textures/baseTexture").BaseTexture;
  10. declare type SphericalPolynomial = import("../../Maths/sphericalPolynomial").SphericalPolynomial;
  11. /**
  12. * Defines the source of the internal texture
  13. */
  14. export enum InternalTextureSource {
  15. /**
  16. * The source of the texture data is unknown
  17. */
  18. Unknown,
  19. /**
  20. * Texture data comes from an URL
  21. */
  22. Url,
  23. /**
  24. * Texture data is only used for temporary storage
  25. */
  26. Temp,
  27. /**
  28. * Texture data comes from raw data (ArrayBuffer)
  29. */
  30. Raw,
  31. /**
  32. * Texture content is dynamic (video or dynamic texture)
  33. */
  34. Dynamic,
  35. /**
  36. * Texture content is generated by rendering to it
  37. */
  38. RenderTarget,
  39. /**
  40. * Texture content is part of a multi render target process
  41. */
  42. MultiRenderTarget,
  43. /**
  44. * Texture data comes from a cube data file
  45. */
  46. Cube,
  47. /**
  48. * Texture data comes from a raw cube data
  49. */
  50. CubeRaw,
  51. /**
  52. * Texture data come from a prefiltered cube data file
  53. */
  54. CubePrefiltered,
  55. /**
  56. * Texture content is raw 3D data
  57. */
  58. Raw3D,
  59. /**
  60. * Texture content is a depth texture
  61. */
  62. Depth,
  63. /**
  64. * Texture data comes from a raw cube data encoded with RGBD
  65. */
  66. CubeRawRGBD
  67. }
  68. /**
  69. * Class used to store data associated with WebGL texture data for the engine
  70. * This class should not be used directly
  71. */
  72. export class InternalTexture {
  73. /** @hidden */
  74. public static _UpdateRGBDAsync = (internalTexture: InternalTexture, data: ArrayBufferView[][], sphericalPolynomial: Nullable<SphericalPolynomial>, lodScale: number, lodOffset: number): Promise<void> => {
  75. throw _DevTools.WarnImport("environmentTextureTools");
  76. }
  77. /**
  78. * Defines if the texture is ready
  79. */
  80. public isReady: boolean = false;
  81. /**
  82. * Defines if the texture is a cube texture
  83. */
  84. public isCube: boolean = false;
  85. /**
  86. * Defines if the texture contains 3D data
  87. */
  88. public is3D: boolean = false;
  89. /**
  90. * Defines if the texture contains multiview data
  91. */
  92. public isMultiview: boolean = false;
  93. /**
  94. * Gets the URL used to load this texture
  95. */
  96. public url: string = "";
  97. /**
  98. * Gets the sampling mode of the texture
  99. */
  100. public samplingMode: number = -1;
  101. /**
  102. * Gets a boolean indicating if the texture needs mipmaps generation
  103. */
  104. public generateMipMaps: boolean = false;
  105. /**
  106. * Gets the number of samples used by the texture (WebGL2+ only)
  107. */
  108. public samples: number = 0;
  109. /**
  110. * Gets the type of the texture (int, float...)
  111. */
  112. public type: number = -1;
  113. /**
  114. * Gets the format of the texture (RGB, RGBA...)
  115. */
  116. public format: number = -1;
  117. /**
  118. * Observable called when the texture is loaded
  119. */
  120. public onLoadedObservable = new Observable<InternalTexture>();
  121. /**
  122. * Gets the width of the texture
  123. */
  124. public width: number = 0;
  125. /**
  126. * Gets the height of the texture
  127. */
  128. public height: number = 0;
  129. /**
  130. * Gets the depth of the texture
  131. */
  132. public depth: number = 0;
  133. /**
  134. * Gets the initial width of the texture (It could be rescaled if the current system does not support non power of two textures)
  135. */
  136. public baseWidth: number = 0;
  137. /**
  138. * Gets the initial height of the texture (It could be rescaled if the current system does not support non power of two textures)
  139. */
  140. public baseHeight: number = 0;
  141. /**
  142. * Gets the initial depth of the texture (It could be rescaled if the current system does not support non power of two textures)
  143. */
  144. public baseDepth: number = 0;
  145. /**
  146. * Gets a boolean indicating if the texture is inverted on Y axis
  147. */
  148. public invertY: boolean = false;
  149. // Private
  150. /** @hidden */
  151. public _invertVScale = false;
  152. /** @hidden */
  153. public _associatedChannel = -1;
  154. /** @hidden */
  155. public _source = InternalTextureSource.Unknown;
  156. /** @hidden */
  157. public _buffer: Nullable<string | ArrayBuffer | ArrayBufferView | HTMLImageElement | Blob> = null;
  158. /** @hidden */
  159. public _bufferView: Nullable<ArrayBufferView> = null;
  160. /** @hidden */
  161. public _bufferViewArray: Nullable<ArrayBufferView[]> = null;
  162. /** @hidden */
  163. public _bufferViewArrayArray: Nullable<ArrayBufferView[][]> = null;
  164. /** @hidden */
  165. public _size: number = 0;
  166. /** @hidden */
  167. public _extension: string = "";
  168. /** @hidden */
  169. public _files: Nullable<string[]> = null;
  170. /** @hidden */
  171. public _workingCanvas: Nullable<HTMLCanvasElement> = null;
  172. /** @hidden */
  173. public _workingContext: Nullable<CanvasRenderingContext2D> = null;
  174. /** @hidden */
  175. public _framebuffer: Nullable<WebGLFramebuffer> = null;
  176. /** @hidden */
  177. public _depthStencilBuffer: Nullable<WebGLRenderbuffer> = null;
  178. /** @hidden */
  179. public _MSAAFramebuffer: Nullable<WebGLFramebuffer> = null;
  180. /** @hidden */
  181. public _MSAARenderBuffer: Nullable<WebGLRenderbuffer> = null;
  182. /** @hidden */
  183. public _attachments: Nullable<number[]> = null;
  184. /** @hidden */
  185. public _cachedCoordinatesMode: Nullable<number> = null;
  186. /** @hidden */
  187. public _cachedWrapU: Nullable<number> = null;
  188. /** @hidden */
  189. public _cachedWrapV: Nullable<number> = null;
  190. /** @hidden */
  191. public _cachedWrapR: Nullable<number> = null;
  192. /** @hidden */
  193. public _cachedAnisotropicFilteringLevel: Nullable<number> = null;
  194. /** @hidden */
  195. public _isDisabled: boolean = false;
  196. /** @hidden */
  197. public _compression: Nullable<string> = null;
  198. /** @hidden */
  199. public _generateStencilBuffer: boolean = false;
  200. /** @hidden */
  201. public _generateDepthBuffer: boolean = false;
  202. /** @hidden */
  203. public _comparisonFunction: number = 0;
  204. /** @hidden */
  205. public _sphericalPolynomial: Nullable<SphericalPolynomial> = null;
  206. /** @hidden */
  207. public _lodGenerationScale: number = 0;
  208. /** @hidden */
  209. public _lodGenerationOffset: number = 0;
  210. // Multiview
  211. /** @hidden */
  212. public _colorTextureArray: Nullable<WebGLTexture> = null;
  213. /** @hidden */
  214. public _depthStencilTextureArray: Nullable<WebGLTexture> = null;
  215. // The following three fields helps sharing generated fixed LODs for texture filtering
  216. // In environment not supporting the textureLOD extension like EDGE. They are for internal use only.
  217. // They are at the level of the gl texture to benefit from the cache.
  218. /** @hidden */
  219. public _lodTextureHigh: Nullable<BaseTexture> = null;
  220. /** @hidden */
  221. public _lodTextureMid: Nullable<BaseTexture> = null;
  222. /** @hidden */
  223. public _lodTextureLow: Nullable<BaseTexture> = null;
  224. /** @hidden */
  225. public _isRGBD: boolean = false;
  226. /** @hidden */
  227. public _linearSpecularLOD: boolean = false;
  228. /** @hidden */
  229. public _irradianceTexture: Nullable<BaseTexture> = null;
  230. /** @hidden */
  231. public _webGLTexture: Nullable<WebGLTexture> = null;
  232. /** @hidden */
  233. public _references: number = 1;
  234. private _engine: ThinEngine;
  235. /**
  236. * Gets the Engine the texture belongs to.
  237. * @returns The babylon engine
  238. */
  239. public getEngine(): ThinEngine {
  240. return this._engine;
  241. }
  242. /**
  243. * Gets the data source type of the texture
  244. */
  245. public get source(): InternalTextureSource {
  246. return this._source;
  247. }
  248. /**
  249. * Creates a new InternalTexture
  250. * @param engine defines the engine to use
  251. * @param source defines the type of data that will be used
  252. * @param delayAllocation if the texture allocation should be delayed (default: false)
  253. */
  254. constructor(engine: ThinEngine, source: InternalTextureSource, delayAllocation = false) {
  255. this._engine = engine;
  256. this._source = source;
  257. if (!delayAllocation) {
  258. this._webGLTexture = engine._createTexture();
  259. }
  260. }
  261. /**
  262. * Increments the number of references (ie. the number of Texture that point to it)
  263. */
  264. public incrementReferences(): void {
  265. this._references++;
  266. }
  267. /**
  268. * Change the size of the texture (not the size of the content)
  269. * @param width defines the new width
  270. * @param height defines the new height
  271. * @param depth defines the new depth (1 by default)
  272. */
  273. public updateSize(width: int, height: int, depth: int = 1): void {
  274. this.width = width;
  275. this.height = height;
  276. this.depth = depth;
  277. this.baseWidth = width;
  278. this.baseHeight = height;
  279. this.baseDepth = depth;
  280. this._size = width * height * depth;
  281. }
  282. /** @hidden */
  283. public _rebuild(): void {
  284. var proxy: InternalTexture;
  285. this.isReady = false;
  286. this._cachedCoordinatesMode = null;
  287. this._cachedWrapU = null;
  288. this._cachedWrapV = null;
  289. this._cachedAnisotropicFilteringLevel = null;
  290. switch (this.source) {
  291. case InternalTextureSource.Temp:
  292. return;
  293. case InternalTextureSource.Url:
  294. proxy = this._engine.createTexture(this.url, !this.generateMipMaps, this.invertY, null, this.samplingMode, () => {
  295. proxy._swapAndDie(this);
  296. this.isReady = true;
  297. }, null, this._buffer, undefined, this.format);
  298. return;
  299. case InternalTextureSource.Raw:
  300. proxy = this._engine.createRawTexture(this._bufferView, this.baseWidth, this.baseHeight, this.format, this.generateMipMaps,
  301. this.invertY, this.samplingMode, this._compression);
  302. proxy._swapAndDie(this);
  303. this.isReady = true;
  304. return;
  305. case InternalTextureSource.Raw3D:
  306. proxy = this._engine.createRawTexture3D(this._bufferView, this.baseWidth, this.baseHeight, this.baseDepth, this.format, this.generateMipMaps,
  307. this.invertY, this.samplingMode, this._compression);
  308. proxy._swapAndDie(this);
  309. this.isReady = true;
  310. return;
  311. case InternalTextureSource.Dynamic:
  312. proxy = this._engine.createDynamicTexture(this.baseWidth, this.baseHeight, this.generateMipMaps, this.samplingMode);
  313. proxy._swapAndDie(this);
  314. this._engine.updateDynamicTexture(this, this._engine.getRenderingCanvas()!, this.invertY, undefined, undefined, true);
  315. // The engine will make sure to update content so no need to flag it as isReady = true
  316. return;
  317. case InternalTextureSource.RenderTarget:
  318. let options = new RenderTargetCreationOptions();
  319. options.generateDepthBuffer = this._generateDepthBuffer;
  320. options.generateMipMaps = this.generateMipMaps;
  321. options.generateStencilBuffer = this._generateStencilBuffer;
  322. options.samplingMode = this.samplingMode;
  323. options.type = this.type;
  324. if (this.isCube) {
  325. proxy = this._engine.createRenderTargetCubeTexture(this.width, options);
  326. } else {
  327. let size = {
  328. width: this.width,
  329. height: this.height
  330. };
  331. proxy = (this._engine as Engine).createRenderTargetTexture(size, options);
  332. }
  333. proxy._swapAndDie(this);
  334. this.isReady = true;
  335. return;
  336. case InternalTextureSource.Depth:
  337. let depthTextureOptions = {
  338. bilinearFiltering: this.samplingMode !== Constants.TEXTURE_BILINEAR_SAMPLINGMODE,
  339. comparisonFunction: this._comparisonFunction,
  340. generateStencil: this._generateStencilBuffer,
  341. isCube: this.isCube
  342. };
  343. proxy = this._engine.createDepthStencilTexture({ width: this.width, height: this.height }, depthTextureOptions);
  344. proxy._swapAndDie(this);
  345. this.isReady = true;
  346. return;
  347. case InternalTextureSource.Cube:
  348. proxy = this._engine.createCubeTexture(this.url, null, this._files, !this.generateMipMaps, () => {
  349. proxy._swapAndDie(this);
  350. this.isReady = true;
  351. }, null, this.format, this._extension);
  352. return;
  353. case InternalTextureSource.CubeRaw:
  354. proxy = this._engine.createRawCubeTexture(this._bufferViewArray!, this.width, this.format, this.type, this.generateMipMaps, this.invertY, this.samplingMode, this._compression);
  355. proxy._swapAndDie(this);
  356. this.isReady = true;
  357. return;
  358. case InternalTextureSource.CubeRawRGBD:
  359. proxy = this._engine.createRawCubeTexture(null, this.width, this.format, this.type, this.generateMipMaps, this.invertY, this.samplingMode, this._compression);
  360. InternalTexture._UpdateRGBDAsync(proxy, this._bufferViewArrayArray!, this._sphericalPolynomial, this._lodGenerationScale, this._lodGenerationOffset).then(() => {
  361. proxy._swapAndDie(this);
  362. this.isReady = true;
  363. });
  364. return;
  365. case InternalTextureSource.CubePrefiltered:
  366. proxy = this._engine.createPrefilteredCubeTexture(this.url, null, this._lodGenerationScale, this._lodGenerationOffset, (proxy) => {
  367. if (proxy) {
  368. proxy._swapAndDie(this);
  369. }
  370. this.isReady = true;
  371. }, null, this.format, this._extension);
  372. proxy._sphericalPolynomial = this._sphericalPolynomial;
  373. return;
  374. }
  375. }
  376. /** @hidden */
  377. public _swapAndDie(target: InternalTexture): void {
  378. target._webGLTexture = this._webGLTexture;
  379. target._isRGBD = this._isRGBD;
  380. if (this._framebuffer) {
  381. target._framebuffer = this._framebuffer;
  382. }
  383. if (this._depthStencilBuffer) {
  384. target._depthStencilBuffer = this._depthStencilBuffer;
  385. }
  386. if (this._lodTextureHigh) {
  387. if (target._lodTextureHigh) {
  388. target._lodTextureHigh.dispose();
  389. }
  390. target._lodTextureHigh = this._lodTextureHigh;
  391. }
  392. if (this._lodTextureMid) {
  393. if (target._lodTextureMid) {
  394. target._lodTextureMid.dispose();
  395. }
  396. target._lodTextureMid = this._lodTextureMid;
  397. }
  398. if (this._lodTextureLow) {
  399. if (target._lodTextureLow) {
  400. target._lodTextureLow.dispose();
  401. }
  402. target._lodTextureLow = this._lodTextureLow;
  403. }
  404. if (this._irradianceTexture) {
  405. if (target._irradianceTexture) {
  406. target._irradianceTexture.dispose();
  407. }
  408. target._irradianceTexture = this._irradianceTexture;
  409. }
  410. let cache = this._engine.getLoadedTexturesCache();
  411. var index = cache.indexOf(this);
  412. if (index !== -1) {
  413. cache.splice(index, 1);
  414. }
  415. var index = cache.indexOf(target);
  416. if (index === -1) {
  417. cache.push(target);
  418. }
  419. }
  420. /**
  421. * Dispose the current allocated resources
  422. */
  423. public dispose(): void {
  424. if (!this._webGLTexture) {
  425. return;
  426. }
  427. this._references--;
  428. if (this._references === 0) {
  429. this._engine._releaseTexture(this);
  430. this._webGLTexture = null;
  431. }
  432. }
  433. }