babylon.internalTexture.ts 17 KB

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