babylon.internalTexture.ts 15 KB

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