WebGPUTextures.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. import { GPUTextureFormat, GPUAddressMode, GPUFilterMode, GPUTextureDimension } from './constants.js';
  2. import { VideoTexture, CubeTexture, Texture, NearestFilter, NearestMipmapNearestFilter, NearestMipmapLinearFilter, LinearFilter, RepeatWrapping, MirroredRepeatWrapping,
  3. RGBAFormat, RedFormat, RGFormat, RGBA_S3TC_DXT1_Format, RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT5_Format, UnsignedByteType, FloatType, HalfFloatType, SRGBColorSpace, DepthFormat, DepthTexture
  4. } from 'three';
  5. import WebGPUTextureUtils from './WebGPUTextureUtils.js';
  6. class WebGPUTextures {
  7. constructor( device, properties, info ) {
  8. this.device = device;
  9. this.properties = properties;
  10. this.info = info;
  11. this.defaultTexture = null;
  12. this.depthDefaultTexture = null;
  13. this.defaultVideoTexture = null;
  14. this.defaultCubeTexture = null;
  15. this.defaultSampler = null;
  16. this.samplerCache = new Map();
  17. this.utils = null;
  18. }
  19. getDefaultSampler() {
  20. if ( this.defaultSampler === null ) {
  21. this.defaultSampler = this.device.createSampler( {} );
  22. }
  23. return this.defaultSampler;
  24. }
  25. getDepthDefaultTexture() {
  26. if ( this.depthDefaultTexture === null ) {
  27. const depthTexture = new DepthTexture();
  28. depthTexture.minFilter = NearestFilter;
  29. depthTexture.magFilter = NearestFilter;
  30. depthTexture.image.width = 1;
  31. depthTexture.image.height = 1;
  32. this._uploadTexture( depthTexture );
  33. this.depthDefaultTexture = this.getTextureGPU( depthTexture );
  34. }
  35. return this.depthDefaultTexture;
  36. }
  37. getDefaultTexture() {
  38. if ( this.defaultTexture === null ) {
  39. const texture = new Texture();
  40. texture.minFilter = NearestFilter;
  41. texture.magFilter = NearestFilter;
  42. this._uploadTexture( texture );
  43. this.defaultTexture = this.getTextureGPU( texture );
  44. }
  45. return this.defaultTexture;
  46. }
  47. getVideoDefaultTexture() {
  48. if ( this.defaultVideoTexture === null ) {
  49. const video = document.getElementById( 'video' );
  50. const texture = new VideoTexture( video );
  51. texture.minFilter = NearestFilter;
  52. texture.magFilter = NearestFilter;
  53. this._uploadVideoTexture( texture );
  54. this.defaultVideoTexture = this.getTextureGPU( texture );
  55. }
  56. return this.defaultVideoTexture;
  57. }
  58. getDefaultCubeTexture() {
  59. if ( this.defaultCubeTexture === null ) {
  60. const texture = new CubeTexture();
  61. texture.minFilter = NearestFilter;
  62. texture.magFilter = NearestFilter;
  63. this._uploadTexture( texture );
  64. this.defaultCubeTexture = this.getTextureGPU( texture );
  65. }
  66. return this.defaultCubeTexture;
  67. }
  68. getTextureGPU( texture ) {
  69. const textureProperties = this.properties.get( texture );
  70. return textureProperties.textureGPU;
  71. }
  72. getSampler( texture ) {
  73. const textureProperties = this.properties.get( texture );
  74. return textureProperties.samplerGPU;
  75. }
  76. updateTexture( texture ) {
  77. let needsUpdate = false;
  78. const textureProperties = this.properties.get( texture );
  79. if ( texture.version > 0 && textureProperties.version !== texture.version ) {
  80. const image = texture.image;
  81. if ( image === undefined ) {
  82. console.warn( 'THREE.WebGPURenderer: Texture marked for update but image is undefined.' );
  83. } else if ( image.complete === false ) {
  84. console.warn( 'THREE.WebGPURenderer: Texture marked for update but image is incomplete.' );
  85. } else {
  86. // texture init
  87. if ( textureProperties.initialized === undefined ) {
  88. textureProperties.initialized = true;
  89. const disposeCallback = onTextureDispose.bind( this );
  90. textureProperties.disposeCallback = disposeCallback;
  91. texture.addEventListener( 'dispose', disposeCallback );
  92. this.info.memory.textures ++;
  93. }
  94. //
  95. if ( texture.isVideoTexture ) {
  96. needsUpdate = this._uploadVideoTexture( texture );
  97. } else {
  98. needsUpdate = this._uploadTexture( texture );
  99. }
  100. }
  101. }
  102. // if the texture is used for RTT, it's necessary to init it once so the binding
  103. // group's resource definition points to the respective GPUTexture
  104. if ( textureProperties.initializedRTT === false ) {
  105. textureProperties.initializedRTT = true;
  106. needsUpdate = true;
  107. }
  108. return needsUpdate;
  109. }
  110. updateSampler( texture ) {
  111. const array = [];
  112. array.push( texture.wrapS );
  113. array.push( texture.wrapT );
  114. array.push( texture.wrapR );
  115. array.push( texture.magFilter );
  116. array.push( texture.minFilter );
  117. array.push( texture.anisotropy );
  118. const key = array.join();
  119. let samplerGPU = this.samplerCache.get( key );
  120. if ( samplerGPU === undefined ) {
  121. samplerGPU = this.device.createSampler( {
  122. addressModeU: this._convertAddressMode( texture.wrapS ),
  123. addressModeV: this._convertAddressMode( texture.wrapT ),
  124. addressModeW: this._convertAddressMode( texture.wrapR ),
  125. magFilter: this._convertFilterMode( texture.magFilter ),
  126. minFilter: this._convertFilterMode( texture.minFilter ),
  127. mipmapFilter: this._convertFilterMode( texture.minFilter ),
  128. maxAnisotropy: texture.anisotropy
  129. } );
  130. this.samplerCache.set( key, samplerGPU );
  131. }
  132. const textureProperties = this.properties.get( texture );
  133. textureProperties.samplerGPU = samplerGPU;
  134. }
  135. initRenderTarget( renderTarget ) {
  136. const properties = this.properties;
  137. const renderTargetProperties = properties.get( renderTarget );
  138. if ( renderTargetProperties.initialized === undefined ) {
  139. const device = this.device;
  140. const width = renderTarget.width;
  141. const height = renderTarget.height;
  142. const colorTextureFormat = this._getFormat( renderTarget.texture );
  143. const label = renderTarget.texture.name ? '_' + renderTarget.texture.name : '';
  144. const colorTextureGPU = device.createTexture( {
  145. label: 'renderTarget' + label,
  146. size: {
  147. width: width,
  148. height: height,
  149. depthOrArrayLayers: 1
  150. },
  151. format: colorTextureFormat,
  152. usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
  153. } );
  154. this.info.memory.textures ++;
  155. renderTargetProperties.colorTextureGPU = colorTextureGPU;
  156. renderTargetProperties.colorTextureFormat = colorTextureFormat;
  157. // When the ".texture" or ".depthTexture" property of a render target is used as a map,
  158. // the renderer has to find the respective GPUTexture objects to setup the bind groups.
  159. // Since it's not possible to see just from a texture object whether it belongs to a render
  160. // target or not, we need the initializedRTT flag.
  161. const textureProperties = properties.get( renderTarget.texture );
  162. textureProperties.textureGPU = colorTextureGPU;
  163. textureProperties.initializedRTT = false;
  164. if ( renderTarget.depthBuffer === true ) {
  165. const depthTextureFormat = renderTarget.depthTexture !== null ? this._getFormat( renderTarget.depthTexture ) : GPUTextureFormat.Depth24PlusStencil8;
  166. const depthTextureGPU = device.createTexture( {
  167. label: 'renderTarget' + label + '_depthBuffer',
  168. size: {
  169. width: width,
  170. height: height,
  171. depthOrArrayLayers: 1
  172. },
  173. format: depthTextureFormat,
  174. usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
  175. } );
  176. this.info.memory.textures ++;
  177. renderTargetProperties.depthTextureGPU = depthTextureGPU;
  178. renderTargetProperties.depthTextureFormat = depthTextureFormat;
  179. if ( renderTarget.depthTexture !== null ) {
  180. const depthTextureProperties = properties.get( renderTarget.depthTexture );
  181. depthTextureProperties.textureGPU = depthTextureGPU;
  182. depthTextureProperties.initializedRTT = false;
  183. }
  184. }
  185. //
  186. const disposeCallback = onRenderTargetDispose.bind( this );
  187. renderTargetProperties.disposeCallback = disposeCallback;
  188. renderTarget.addEventListener( 'dispose', disposeCallback );
  189. //
  190. renderTargetProperties.initialized = true;
  191. }
  192. }
  193. dispose() {
  194. this.samplerCache.clear();
  195. }
  196. _convertAddressMode( value ) {
  197. let addressMode = GPUAddressMode.ClampToEdge;
  198. if ( value === RepeatWrapping ) {
  199. addressMode = GPUAddressMode.Repeat;
  200. } else if ( value === MirroredRepeatWrapping ) {
  201. addressMode = GPUAddressMode.MirrorRepeat;
  202. }
  203. return addressMode;
  204. }
  205. _convertFilterMode( value ) {
  206. let filterMode = GPUFilterMode.Linear;
  207. if ( value === NearestFilter || value === NearestMipmapNearestFilter || value === NearestMipmapLinearFilter ) {
  208. filterMode = GPUFilterMode.Nearest;
  209. }
  210. return filterMode;
  211. }
  212. _uploadVideoTexture( texture ) {
  213. const device = this.device;
  214. const textureProperties = this.properties.get( texture );
  215. const textureGPU = device.importExternalTexture( {
  216. source: texture.source.data
  217. } );
  218. textureProperties.textureGPU = textureGPU;
  219. //textureProperties.version = texture.version; // @TODO: Force update for now, study a better solution soon using native VideoTexture.update() to fix warns
  220. return true;
  221. }
  222. _uploadTexture( texture ) {
  223. let needsUpdate = false;
  224. const device = this.device;
  225. const image = texture.image;
  226. const textureProperties = this.properties.get( texture );
  227. const { width, height, depth } = this._getSize( texture );
  228. const needsMipmaps = this._needsMipmaps( texture );
  229. const dimension = this._getDimension( texture );
  230. const mipLevelCount = this._getMipLevelCount( texture, width, height, needsMipmaps );
  231. const format = this._getFormat( texture );
  232. let usage = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST;
  233. if ( needsMipmaps === true ) {
  234. // current mipmap generation requires RENDER_ATTACHMENT
  235. usage |= GPUTextureUsage.RENDER_ATTACHMENT;
  236. }
  237. const textureGPUDescriptor = {
  238. label: texture.name,
  239. size: {
  240. width: width,
  241. height: height,
  242. depthOrArrayLayers: depth,
  243. },
  244. mipLevelCount: mipLevelCount,
  245. sampleCount: 1,
  246. dimension: dimension,
  247. format: format,
  248. usage: usage
  249. };
  250. // texture creation
  251. let textureGPU = textureProperties.textureGPU;
  252. if ( textureGPU === undefined ) {
  253. textureGPU = device.createTexture( textureGPUDescriptor );
  254. needsUpdate = true;
  255. }
  256. // transfer texture data
  257. if ( texture.isDataTexture || texture.isDataArrayTexture || texture.isData3DTexture ) {
  258. this._copyBufferToTexture( image, format, textureGPU );
  259. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor );
  260. } else if ( texture.isCompressedTexture ) {
  261. this._copyCompressedBufferToTexture( texture.mipmaps, format, textureGPU );
  262. } else if ( texture.isCubeTexture ) {
  263. if ( image.length === 6 ) {
  264. this._copyCubeMapToTexture( image, format, texture, textureGPU, textureGPUDescriptor, needsMipmaps );
  265. }
  266. } else if ( texture.isDepthTexture !== true && image !== null ) {
  267. // assume HTMLImageElement, HTMLCanvasElement or ImageBitmap
  268. this._getImageBitmap( image, texture ).then( imageBitmap => {
  269. this._copyExternalImageToTexture( imageBitmap, textureGPU );
  270. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor );
  271. } );
  272. }
  273. //
  274. textureProperties.textureGPU = textureGPU;
  275. textureProperties.version = texture.version;
  276. return needsUpdate;
  277. }
  278. _copyBufferToTexture( image, format, textureGPU, origin = { x: 0, y: 0, z: 0 } ) {
  279. // @TODO: Consider to use GPUCommandEncoder.copyBufferToTexture()
  280. // @TODO: Consider to support valid buffer layouts with other formats like RGB
  281. const data = image.data;
  282. const bytesPerTexel = this._getBytesPerTexel( format );
  283. const bytesPerRow = image.width * bytesPerTexel;
  284. this.device.queue.writeTexture(
  285. {
  286. texture: textureGPU,
  287. mipLevel: 0,
  288. origin
  289. },
  290. data,
  291. {
  292. offset: 0,
  293. bytesPerRow
  294. },
  295. {
  296. width: image.width,
  297. height: image.height,
  298. depthOrArrayLayers: ( image.depth !== undefined ) ? image.depth : 1
  299. } );
  300. }
  301. _copyCubeMapToTexture( images, format, texture, textureGPU, textureGPUDescriptor, needsMipmaps ) {
  302. for ( let i = 0; i < 6; i ++ ) {
  303. const image = images[ i ];
  304. if ( image.isDataTexture ) {
  305. this._copyBufferToTexture( image.image, format, textureGPU, { z: i } );
  306. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor, i );
  307. } else {
  308. this._getImageBitmap( image, texture ).then( imageBitmap => {
  309. this._copyExternalImageToTexture( imageBitmap, textureGPU, { z: i } );
  310. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor, i );
  311. } );
  312. }
  313. }
  314. }
  315. _copyExternalImageToTexture( image, textureGPU, origin = { x: 0, y: 0, z: 0 } ) {
  316. this.device.queue.copyExternalImageToTexture(
  317. {
  318. source: image
  319. }, {
  320. texture: textureGPU,
  321. mipLevel: 0,
  322. origin
  323. }, {
  324. width: image.width,
  325. height: image.height,
  326. depthOrArrayLayers: 1
  327. }
  328. );
  329. }
  330. _copyCompressedBufferToTexture( mipmaps, format, textureGPU ) {
  331. // @TODO: Consider to use GPUCommandEncoder.copyBufferToTexture()
  332. const blockData = this._getBlockData( format );
  333. for ( let i = 0; i < mipmaps.length; i ++ ) {
  334. const mipmap = mipmaps[ i ];
  335. const width = mipmap.width;
  336. const height = mipmap.height;
  337. const bytesPerRow = Math.ceil( width / blockData.width ) * blockData.byteLength;
  338. this.device.queue.writeTexture(
  339. {
  340. texture: textureGPU,
  341. mipLevel: i
  342. },
  343. mipmap.data,
  344. {
  345. offset: 0,
  346. bytesPerRow
  347. },
  348. {
  349. width: Math.ceil( width / blockData.width ) * blockData.width,
  350. height: Math.ceil( height / blockData.width ) * blockData.width,
  351. depthOrArrayLayers: 1
  352. } );
  353. }
  354. }
  355. _generateMipmaps( textureGPU, textureGPUDescriptor, baseArrayLayer ) {
  356. if ( this.utils === null ) {
  357. this.utils = new WebGPUTextureUtils( this.device ); // only create this helper if necessary
  358. }
  359. this.utils.generateMipmaps( textureGPU, textureGPUDescriptor, baseArrayLayer );
  360. }
  361. _getBlockData( format ) {
  362. // this method is only relevant for compressed texture formats
  363. if ( format === GPUTextureFormat.BC1RGBAUnorm || format === GPUTextureFormat.BC1RGBAUnormSRGB ) return { byteLength: 8, width: 4, height: 4 }; // DXT1
  364. if ( format === GPUTextureFormat.BC2RGBAUnorm || format === GPUTextureFormat.BC2RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // DXT3
  365. if ( format === GPUTextureFormat.BC3RGBAUnorm || format === GPUTextureFormat.BC3RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // DXT5
  366. if ( format === GPUTextureFormat.BC4RUnorm || format === GPUTextureFormat.BC4RSNorm ) return { byteLength: 8, width: 4, height: 4 }; // RGTC1
  367. if ( format === GPUTextureFormat.BC5RGUnorm || format === GPUTextureFormat.BC5RGSnorm ) return { byteLength: 16, width: 4, height: 4 }; // RGTC2
  368. if ( format === GPUTextureFormat.BC6HRGBUFloat || format === GPUTextureFormat.BC6HRGBFloat ) return { byteLength: 16, width: 4, height: 4 }; // BPTC (float)
  369. if ( format === GPUTextureFormat.BC7RGBAUnorm || format === GPUTextureFormat.BC7RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // BPTC (unorm)
  370. }
  371. _getBytesPerTexel( format ) {
  372. if ( format === GPUTextureFormat.R8Unorm ) return 1;
  373. if ( format === GPUTextureFormat.R16Float ) return 2;
  374. if ( format === GPUTextureFormat.RG8Unorm ) return 2;
  375. if ( format === GPUTextureFormat.RG16Float ) return 4;
  376. if ( format === GPUTextureFormat.R32Float ) return 4;
  377. if ( format === GPUTextureFormat.RGBA8Unorm || format === GPUTextureFormat.RGBA8UnormSRGB ) return 4;
  378. if ( format === GPUTextureFormat.RG32Float ) return 8;
  379. if ( format === GPUTextureFormat.RGBA16Float ) return 8;
  380. if ( format === GPUTextureFormat.RGBA32Float ) return 16;
  381. }
  382. _getDimension( texture ) {
  383. let dimension;
  384. if ( texture.isData3DTexture ) {
  385. dimension = GPUTextureDimension.ThreeD;
  386. } else {
  387. dimension = GPUTextureDimension.TwoD;
  388. }
  389. return dimension;
  390. }
  391. _getFormat( texture ) {
  392. const format = texture.format;
  393. const type = texture.type;
  394. const colorSpace = texture.colorSpace;
  395. let formatGPU;
  396. switch ( format ) {
  397. case RGBA_S3TC_DXT1_Format:
  398. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC1RGBAUnormSRGB : GPUTextureFormat.BC1RGBAUnorm;
  399. break;
  400. case RGBA_S3TC_DXT3_Format:
  401. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC2RGBAUnormSRGB : GPUTextureFormat.BC2RGBAUnorm;
  402. break;
  403. case RGBA_S3TC_DXT5_Format:
  404. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC3RGBAUnormSRGB : GPUTextureFormat.BC3RGBAUnorm;
  405. break;
  406. case DepthFormat:
  407. formatGPU = GPUTextureFormat.Depth32Float;
  408. break;
  409. case RGBAFormat:
  410. switch ( type ) {
  411. case UnsignedByteType:
  412. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.RGBA8UnormSRGB : GPUTextureFormat.RGBA8Unorm;
  413. break;
  414. case HalfFloatType:
  415. formatGPU = GPUTextureFormat.RGBA16Float;
  416. break;
  417. case FloatType:
  418. formatGPU = GPUTextureFormat.RGBA32Float;
  419. break;
  420. default:
  421. console.error( 'WebGPURenderer: Unsupported texture type with RGBAFormat.', type );
  422. }
  423. break;
  424. case RedFormat:
  425. switch ( type ) {
  426. case UnsignedByteType:
  427. formatGPU = GPUTextureFormat.R8Unorm;
  428. break;
  429. case HalfFloatType:
  430. formatGPU = GPUTextureFormat.R16Float;
  431. break;
  432. case FloatType:
  433. formatGPU = GPUTextureFormat.R32Float;
  434. break;
  435. default:
  436. console.error( 'WebGPURenderer: Unsupported texture type with RedFormat.', type );
  437. }
  438. break;
  439. case RGFormat:
  440. switch ( type ) {
  441. case UnsignedByteType:
  442. formatGPU = GPUTextureFormat.RG8Unorm;
  443. break;
  444. case HalfFloatType:
  445. formatGPU = GPUTextureFormat.RG16Float;
  446. break;
  447. case FloatType:
  448. formatGPU = GPUTextureFormat.RG32Float;
  449. break;
  450. default:
  451. console.error( 'WebGPURenderer: Unsupported texture type with RGFormat.', type );
  452. }
  453. break;
  454. default:
  455. console.error( 'WebGPURenderer: Unsupported texture format.', format );
  456. }
  457. return formatGPU;
  458. }
  459. _getImageBitmap( image, texture ) {
  460. const width = image.width;
  461. const height = image.height;
  462. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  463. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ) {
  464. const options = {};
  465. options.imageOrientation = ( texture.flipY === true ) ? 'flipY' : 'none';
  466. options.premultiplyAlpha = ( texture.premultiplyAlpha === true ) ? 'premultiply' : 'default';
  467. return createImageBitmap( image, 0, 0, width, height, options );
  468. } else {
  469. // assume ImageBitmap
  470. return Promise.resolve( image );
  471. }
  472. }
  473. _getMipLevelCount( texture, width, height, needsMipmaps ) {
  474. let mipLevelCount;
  475. if ( texture.isCompressedTexture ) {
  476. mipLevelCount = texture.mipmaps.length;
  477. } else if ( needsMipmaps === true ) {
  478. mipLevelCount = Math.floor( Math.log2( Math.max( width, height ) ) ) + 1;
  479. } else {
  480. mipLevelCount = 1; // a texture without mipmaps has a base mip (mipLevel 0)
  481. }
  482. return mipLevelCount;
  483. }
  484. _getSize( texture ) {
  485. const image = texture.image;
  486. let width, height, depth;
  487. if ( texture.isCubeTexture ) {
  488. const faceImage = image.length > 0 ? image[ 0 ].image || image[ 0 ] : null;
  489. width = faceImage ? faceImage.width : 1;
  490. height = faceImage ? faceImage.height : 1;
  491. depth = 6; // one image for each side of the cube map
  492. } else if ( image !== null ) {
  493. width = image.width;
  494. height = image.height;
  495. depth = ( image.depth !== undefined ) ? image.depth : 1;
  496. } else {
  497. width = height = depth = 1;
  498. }
  499. return { width, height, depth };
  500. }
  501. _needsMipmaps( texture ) {
  502. return ( texture.isCompressedTexture !== true ) && ( texture.generateMipmaps === true ) && ( texture.minFilter !== NearestFilter ) && ( texture.minFilter !== LinearFilter );
  503. }
  504. }
  505. function onRenderTargetDispose( event ) {
  506. const renderTarget = event.target;
  507. const properties = this.properties;
  508. const renderTargetProperties = properties.get( renderTarget );
  509. renderTarget.removeEventListener( 'dispose', renderTargetProperties.disposeCallback );
  510. renderTargetProperties.colorTextureGPU.destroy();
  511. properties.remove( renderTarget.texture );
  512. this.info.memory.textures --;
  513. if ( renderTarget.depthBuffer === true ) {
  514. renderTargetProperties.depthTextureGPU.destroy();
  515. this.info.memory.textures --;
  516. if ( renderTarget.depthTexture !== null ) {
  517. properties.remove( renderTarget.depthTexture );
  518. }
  519. }
  520. properties.remove( renderTarget );
  521. }
  522. function onTextureDispose( event ) {
  523. const texture = event.target;
  524. const textureProperties = this.properties.get( texture );
  525. textureProperties.textureGPU.destroy();
  526. texture.removeEventListener( 'dispose', textureProperties.disposeCallback );
  527. this.properties.remove( texture );
  528. this.info.memory.textures --;
  529. }
  530. export default WebGPUTextures;