Textures.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. import DataMap from './DataMap.js';
  2. import { Vector3 } from '../../math/Vector3.js';
  3. import { DepthTexture } from '../../textures/DepthTexture.js';
  4. import { DepthStencilFormat, DepthFormat, UnsignedIntType, UnsignedInt248Type, UnsignedByteType } from '../../constants.js';
  5. const _size = /*@__PURE__*/ new Vector3();
  6. /**
  7. * This module manages the textures of the renderer.
  8. *
  9. * @private
  10. * @augments DataMap
  11. */
  12. class Textures extends DataMap {
  13. /**
  14. * Constructs a new texture management component.
  15. *
  16. * @param {Renderer} renderer - The renderer.
  17. * @param {Backend} backend - The renderer's backend.
  18. * @param {Info} info - Renderer component for managing metrics and monitoring data.
  19. */
  20. constructor( renderer, backend, info ) {
  21. super();
  22. /**
  23. * The renderer.
  24. *
  25. * @type {Renderer}
  26. */
  27. this.renderer = renderer;
  28. /**
  29. * The backend.
  30. *
  31. * @type {Backend}
  32. */
  33. this.backend = backend;
  34. /**
  35. * Renderer component for managing metrics and monitoring data.
  36. *
  37. * @type {Info}
  38. */
  39. this.info = info;
  40. }
  41. /**
  42. * Updates the given render target. Based on the given render target configuration,
  43. * it updates the texture states representing the attachments of the framebuffer.
  44. *
  45. * @param {RenderTarget} renderTarget - The render target to update.
  46. * @param {number} [activeMipmapLevel=0] - The active mipmap level.
  47. */
  48. updateRenderTarget( renderTarget, activeMipmapLevel = 0 ) {
  49. const renderTargetData = this.get( renderTarget );
  50. const sampleCount = renderTarget.samples === 0 ? 1 : renderTarget.samples;
  51. const depthTextureMips = renderTargetData.depthTextureMips || ( renderTargetData.depthTextureMips = {} );
  52. const textures = renderTarget.textures;
  53. const size = this.getSize( textures[ 0 ] );
  54. const mipWidth = size.width >> activeMipmapLevel;
  55. const mipHeight = size.height >> activeMipmapLevel;
  56. let depthTexture = renderTarget.depthTexture || depthTextureMips[ activeMipmapLevel ];
  57. const useDepthTexture = renderTarget.depthBuffer === true || renderTarget.stencilBuffer === true;
  58. let textureNeedsUpdate = false;
  59. if ( depthTexture === undefined && useDepthTexture ) {
  60. depthTexture = new DepthTexture();
  61. depthTexture.format = renderTarget.stencilBuffer ? DepthStencilFormat : DepthFormat;
  62. depthTexture.type = renderTarget.stencilBuffer ? UnsignedInt248Type : UnsignedIntType; // FloatType
  63. depthTexture.image.width = mipWidth;
  64. depthTexture.image.height = mipHeight;
  65. depthTexture.image.depth = size.depth;
  66. depthTextureMips[ activeMipmapLevel ] = depthTexture;
  67. }
  68. if ( renderTargetData.width !== size.width || size.height !== renderTargetData.height ) {
  69. textureNeedsUpdate = true;
  70. if ( depthTexture ) {
  71. depthTexture.needsUpdate = true;
  72. depthTexture.image.width = mipWidth;
  73. depthTexture.image.height = mipHeight;
  74. depthTexture.image.depth = depthTexture.isArrayTexture ? depthTexture.image.depth : 1;
  75. }
  76. }
  77. renderTargetData.width = size.width;
  78. renderTargetData.height = size.height;
  79. renderTargetData.textures = textures;
  80. renderTargetData.depthTexture = depthTexture || null;
  81. renderTargetData.depth = renderTarget.depthBuffer;
  82. renderTargetData.stencil = renderTarget.stencilBuffer;
  83. renderTargetData.renderTarget = renderTarget;
  84. if ( renderTargetData.sampleCount !== sampleCount ) {
  85. textureNeedsUpdate = true;
  86. if ( depthTexture ) {
  87. depthTexture.needsUpdate = true;
  88. }
  89. renderTargetData.sampleCount = sampleCount;
  90. }
  91. //
  92. const options = { sampleCount };
  93. // XR render targets require no texture updates
  94. if ( renderTarget.isXRRenderTarget !== true ) {
  95. for ( let i = 0; i < textures.length; i ++ ) {
  96. const texture = textures[ i ];
  97. if ( textureNeedsUpdate ) texture.needsUpdate = true;
  98. this.updateTexture( texture, options );
  99. }
  100. if ( depthTexture ) {
  101. this.updateTexture( depthTexture, options );
  102. }
  103. }
  104. // dispose handler
  105. if ( renderTargetData.initialized !== true ) {
  106. renderTargetData.initialized = true;
  107. // dispose
  108. const onDispose = () => {
  109. renderTarget.removeEventListener( 'dispose', onDispose );
  110. for ( let i = 0; i < textures.length; i ++ ) {
  111. this._destroyTexture( textures[ i ] );
  112. }
  113. if ( depthTexture ) {
  114. this._destroyTexture( depthTexture );
  115. }
  116. this.delete( renderTarget );
  117. };
  118. renderTarget.addEventListener( 'dispose', onDispose );
  119. }
  120. }
  121. /**
  122. * Updates the given texture. Depending on the texture state, this method
  123. * triggers the upload of texture data to the GPU memory. If the texture data are
  124. * not yet ready for the upload, it uses default texture data for as a placeholder.
  125. *
  126. * @param {Texture} texture - The texture to update.
  127. * @param {Object} [options={}] - The options.
  128. */
  129. updateTexture( texture, options = {} ) {
  130. const textureData = this.get( texture );
  131. if ( textureData.initialized === true && textureData.version === texture.version ) return;
  132. const isRenderTarget = texture.isRenderTargetTexture || texture.isDepthTexture || texture.isFramebufferTexture;
  133. const backend = this.backend;
  134. if ( isRenderTarget && textureData.initialized === true ) {
  135. // it's an update
  136. backend.destroySampler( texture );
  137. backend.destroyTexture( texture );
  138. }
  139. //
  140. if ( texture.isFramebufferTexture ) {
  141. const renderTarget = this.renderer.getRenderTarget();
  142. if ( renderTarget ) {
  143. texture.type = renderTarget.texture.type;
  144. } else {
  145. texture.type = UnsignedByteType;
  146. }
  147. }
  148. //
  149. const { width, height, depth } = this.getSize( texture );
  150. options.width = width;
  151. options.height = height;
  152. options.depth = depth;
  153. options.needsMipmaps = this.needsMipmaps( texture );
  154. options.levels = options.needsMipmaps ? this.getMipLevels( texture, width, height ) : 1;
  155. //
  156. if ( isRenderTarget || texture.isStorageTexture === true ) {
  157. backend.createSampler( texture );
  158. backend.createTexture( texture, options );
  159. textureData.generation = texture.version;
  160. } else {
  161. const needsCreate = textureData.initialized !== true;
  162. if ( needsCreate ) backend.createSampler( texture );
  163. if ( texture.version > 0 ) {
  164. const image = texture.image;
  165. if ( image === undefined ) {
  166. console.warn( 'THREE.Renderer: Texture marked for update but image is undefined.' );
  167. } else if ( image.complete === false ) {
  168. console.warn( 'THREE.Renderer: Texture marked for update but image is incomplete.' );
  169. } else {
  170. if ( texture.images ) {
  171. const images = [];
  172. for ( const image of texture.images ) {
  173. images.push( image );
  174. }
  175. options.images = images;
  176. } else {
  177. options.image = image;
  178. }
  179. if ( textureData.isDefaultTexture === undefined || textureData.isDefaultTexture === true ) {
  180. backend.createTexture( texture, options );
  181. textureData.isDefaultTexture = false;
  182. textureData.generation = texture.version;
  183. }
  184. if ( texture.source.dataReady === true ) backend.updateTexture( texture, options );
  185. if ( options.needsMipmaps && texture.mipmaps.length === 0 ) backend.generateMipmaps( texture );
  186. }
  187. } else {
  188. // async update
  189. backend.createDefaultTexture( texture );
  190. textureData.isDefaultTexture = true;
  191. textureData.generation = texture.version;
  192. }
  193. }
  194. // dispose handler
  195. if ( textureData.initialized !== true ) {
  196. textureData.initialized = true;
  197. textureData.generation = texture.version;
  198. //
  199. this.info.memory.textures ++;
  200. // dispose
  201. const onDispose = () => {
  202. texture.removeEventListener( 'dispose', onDispose );
  203. this._destroyTexture( texture );
  204. };
  205. texture.addEventListener( 'dispose', onDispose );
  206. }
  207. //
  208. textureData.version = texture.version;
  209. }
  210. /**
  211. * Computes the size of the given texture and writes the result
  212. * into the target vector. This vector is also returned by the
  213. * method.
  214. *
  215. * If no texture data are available for the compute yet, the method
  216. * returns default size values.
  217. *
  218. * @param {Texture} texture - The texture to compute the size for.
  219. * @param {Vector3} target - The target vector.
  220. * @return {Vector3} The target vector.
  221. */
  222. getSize( texture, target = _size ) {
  223. let image = texture.images ? texture.images[ 0 ] : texture.image;
  224. if ( image ) {
  225. if ( image.image !== undefined ) image = image.image;
  226. target.width = image.width || 1;
  227. target.height = image.height || 1;
  228. target.depth = texture.isCubeTexture ? 6 : ( image.depth || 1 );
  229. } else {
  230. target.width = target.height = target.depth = 1;
  231. }
  232. return target;
  233. }
  234. /**
  235. * Computes the number of mipmap levels for the given texture.
  236. *
  237. * @param {Texture} texture - The texture.
  238. * @param {number} width - The texture's width.
  239. * @param {number} height - The texture's height.
  240. * @return {number} The number of mipmap levels.
  241. */
  242. getMipLevels( texture, width, height ) {
  243. let mipLevelCount;
  244. if ( texture.isCompressedTexture ) {
  245. if ( texture.mipmaps ) {
  246. mipLevelCount = texture.mipmaps.length;
  247. } else {
  248. mipLevelCount = 1;
  249. }
  250. } else {
  251. mipLevelCount = Math.floor( Math.log2( Math.max( width, height ) ) ) + 1;
  252. }
  253. return mipLevelCount;
  254. }
  255. /**
  256. * Returns `true` if the given texture requires mipmaps.
  257. *
  258. * @param {Texture} texture - The texture.
  259. * @return {boolean} Whether mipmaps are required or not.
  260. */
  261. needsMipmaps( texture ) {
  262. return texture.isCompressedTexture === true || texture.generateMipmaps;
  263. }
  264. /**
  265. * Frees internal resource when the given texture isn't
  266. * required anymore.
  267. *
  268. * @param {Texture} texture - The texture to destroy.
  269. */
  270. _destroyTexture( texture ) {
  271. if ( this.has( texture ) === true ) {
  272. this.backend.destroySampler( texture );
  273. this.backend.destroyTexture( texture );
  274. this.delete( texture );
  275. this.info.memory.textures --;
  276. }
  277. }
  278. }
  279. export default Textures;
粤ICP备19079148号