WebGPUTextures.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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 colorTextureGPU = device.createTexture( {
  144. size: {
  145. width: width,
  146. height: height,
  147. depthOrArrayLayers: 1
  148. },
  149. format: colorTextureFormat,
  150. usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
  151. } );
  152. this.info.memory.textures ++;
  153. renderTargetProperties.colorTextureGPU = colorTextureGPU;
  154. renderTargetProperties.colorTextureFormat = colorTextureFormat;
  155. // When the ".texture" or ".depthTexture" property of a render target is used as a map,
  156. // the renderer has to find the respective GPUTexture objects to setup the bind groups.
  157. // Since it's not possible to see just from a texture object whether it belongs to a render
  158. // target or not, we need the initializedRTT flag.
  159. const textureProperties = properties.get( renderTarget.texture );
  160. textureProperties.textureGPU = colorTextureGPU;
  161. textureProperties.initializedRTT = false;
  162. if ( renderTarget.depthBuffer === true ) {
  163. const depthTextureFormat = renderTarget.depthTexture !== null ? this._getFormat( renderTarget.depthTexture ) : GPUTextureFormat.Depth24PlusStencil8;
  164. const depthTextureGPU = device.createTexture( {
  165. size: {
  166. width: width,
  167. height: height,
  168. depthOrArrayLayers: 1
  169. },
  170. format: depthTextureFormat,
  171. usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
  172. } );
  173. this.info.memory.textures ++;
  174. renderTargetProperties.depthTextureGPU = depthTextureGPU;
  175. renderTargetProperties.depthTextureFormat = depthTextureFormat;
  176. if ( renderTarget.depthTexture !== null ) {
  177. const depthTextureProperties = properties.get( renderTarget.depthTexture );
  178. depthTextureProperties.textureGPU = depthTextureGPU;
  179. depthTextureProperties.initializedRTT = false;
  180. }
  181. }
  182. //
  183. const disposeCallback = onRenderTargetDispose.bind( this );
  184. renderTargetProperties.disposeCallback = disposeCallback;
  185. renderTarget.addEventListener( 'dispose', disposeCallback );
  186. //
  187. renderTargetProperties.initialized = true;
  188. }
  189. }
  190. dispose() {
  191. this.samplerCache.clear();
  192. }
  193. _convertAddressMode( value ) {
  194. let addressMode = GPUAddressMode.ClampToEdge;
  195. if ( value === RepeatWrapping ) {
  196. addressMode = GPUAddressMode.Repeat;
  197. } else if ( value === MirroredRepeatWrapping ) {
  198. addressMode = GPUAddressMode.MirrorRepeat;
  199. }
  200. return addressMode;
  201. }
  202. _convertFilterMode( value ) {
  203. let filterMode = GPUFilterMode.Linear;
  204. if ( value === NearestFilter || value === NearestMipmapNearestFilter || value === NearestMipmapLinearFilter ) {
  205. filterMode = GPUFilterMode.Nearest;
  206. }
  207. return filterMode;
  208. }
  209. _uploadVideoTexture( texture ) {
  210. const device = this.device;
  211. const textureProperties = this.properties.get( texture );
  212. const textureGPU = device.importExternalTexture( {
  213. source: texture.source.data
  214. } );
  215. textureProperties.textureGPU = textureGPU;
  216. //textureProperties.version = texture.version; // @TODO: Force update for now, study a better solution soon using native VideoTexture.update() to fix warns
  217. return true;
  218. }
  219. _uploadTexture( texture ) {
  220. let needsUpdate = false;
  221. const device = this.device;
  222. const image = texture.image;
  223. const textureProperties = this.properties.get( texture );
  224. const { width, height, depth } = this._getSize( texture );
  225. const needsMipmaps = this._needsMipmaps( texture );
  226. const dimension = this._getDimension( texture );
  227. const mipLevelCount = this._getMipLevelCount( texture, width, height, needsMipmaps );
  228. const format = this._getFormat( texture );
  229. let usage = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST;
  230. if ( needsMipmaps === true ) {
  231. // current mipmap generation requires RENDER_ATTACHMENT
  232. usage |= GPUTextureUsage.RENDER_ATTACHMENT;
  233. }
  234. const textureGPUDescriptor = {
  235. size: {
  236. width: width,
  237. height: height,
  238. depthOrArrayLayers: depth,
  239. },
  240. mipLevelCount: mipLevelCount,
  241. sampleCount: 1,
  242. dimension: dimension,
  243. format: format,
  244. usage: usage
  245. };
  246. // texture creation
  247. let textureGPU = textureProperties.textureGPU;
  248. if ( textureGPU === undefined ) {
  249. textureGPU = device.createTexture( textureGPUDescriptor );
  250. needsUpdate = true;
  251. }
  252. // transfer texture data
  253. if ( texture.isDataTexture || texture.isDataArrayTexture || texture.isData3DTexture ) {
  254. this._copyBufferToTexture( image, format, textureGPU );
  255. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor );
  256. } else if ( texture.isCompressedTexture ) {
  257. this._copyCompressedBufferToTexture( texture.mipmaps, format, textureGPU );
  258. } else if ( texture.isCubeTexture ) {
  259. if ( image.length === 6 ) {
  260. this._copyCubeMapToTexture( image, format, texture, textureGPU, textureGPUDescriptor, needsMipmaps );
  261. }
  262. } else if ( texture.isDepthTexture !== true && image !== null ) {
  263. // assume HTMLImageElement, HTMLCanvasElement or ImageBitmap
  264. this._getImageBitmap( image, texture ).then( imageBitmap => {
  265. this._copyExternalImageToTexture( imageBitmap, textureGPU );
  266. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor );
  267. } );
  268. }
  269. //
  270. textureProperties.textureGPU = textureGPU;
  271. textureProperties.version = texture.version;
  272. return needsUpdate;
  273. }
  274. _copyBufferToTexture( image, format, textureGPU, origin = { x: 0, y: 0, z: 0 } ) {
  275. // @TODO: Consider to use GPUCommandEncoder.copyBufferToTexture()
  276. // @TODO: Consider to support valid buffer layouts with other formats like RGB
  277. const data = image.data;
  278. const bytesPerTexel = this._getBytesPerTexel( format );
  279. const bytesPerRow = image.width * bytesPerTexel;
  280. this.device.queue.writeTexture(
  281. {
  282. texture: textureGPU,
  283. mipLevel: 0,
  284. origin
  285. },
  286. data,
  287. {
  288. offset: 0,
  289. bytesPerRow
  290. },
  291. {
  292. width: image.width,
  293. height: image.height,
  294. depthOrArrayLayers: ( image.depth !== undefined ) ? image.depth : 1
  295. } );
  296. }
  297. _copyCubeMapToTexture( images, format, texture, textureGPU, textureGPUDescriptor, needsMipmaps ) {
  298. for ( let i = 0; i < 6; i ++ ) {
  299. const image = images[ i ];
  300. if ( image.isDataTexture ) {
  301. this._copyBufferToTexture( image.image, format, textureGPU, { z: i } );
  302. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor, i );
  303. } else {
  304. this._getImageBitmap( image, texture ).then( imageBitmap => {
  305. this._copyExternalImageToTexture( imageBitmap, textureGPU, { z: i } );
  306. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor, i );
  307. } );
  308. }
  309. }
  310. }
  311. _copyExternalImageToTexture( image, textureGPU, origin = { x: 0, y: 0, z: 0 } ) {
  312. this.device.queue.copyExternalImageToTexture(
  313. {
  314. source: image
  315. }, {
  316. texture: textureGPU,
  317. mipLevel: 0,
  318. origin
  319. }, {
  320. width: image.width,
  321. height: image.height,
  322. depthOrArrayLayers: 1
  323. }
  324. );
  325. }
  326. _copyCompressedBufferToTexture( mipmaps, format, textureGPU ) {
  327. // @TODO: Consider to use GPUCommandEncoder.copyBufferToTexture()
  328. const blockData = this._getBlockData( format );
  329. for ( let i = 0; i < mipmaps.length; i ++ ) {
  330. const mipmap = mipmaps[ i ];
  331. const width = mipmap.width;
  332. const height = mipmap.height;
  333. const bytesPerRow = Math.ceil( width / blockData.width ) * blockData.byteLength;
  334. this.device.queue.writeTexture(
  335. {
  336. texture: textureGPU,
  337. mipLevel: i
  338. },
  339. mipmap.data,
  340. {
  341. offset: 0,
  342. bytesPerRow
  343. },
  344. {
  345. width: Math.ceil( width / blockData.width ) * blockData.width,
  346. height: Math.ceil( height / blockData.width ) * blockData.width,
  347. depthOrArrayLayers: 1
  348. } );
  349. }
  350. }
  351. _generateMipmaps( textureGPU, textureGPUDescriptor, baseArrayLayer ) {
  352. if ( this.utils === null ) {
  353. this.utils = new WebGPUTextureUtils( this.device ); // only create this helper if necessary
  354. }
  355. this.utils.generateMipmaps( textureGPU, textureGPUDescriptor, baseArrayLayer );
  356. }
  357. _getBlockData( format ) {
  358. // this method is only relevant for compressed texture formats
  359. if ( format === GPUTextureFormat.BC1RGBAUnorm || format === GPUTextureFormat.BC1RGBAUnormSRGB ) return { byteLength: 8, width: 4, height: 4 }; // DXT1
  360. if ( format === GPUTextureFormat.BC2RGBAUnorm || format === GPUTextureFormat.BC2RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // DXT3
  361. if ( format === GPUTextureFormat.BC3RGBAUnorm || format === GPUTextureFormat.BC3RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // DXT5
  362. if ( format === GPUTextureFormat.BC4RUnorm || format === GPUTextureFormat.BC4RSNorm ) return { byteLength: 8, width: 4, height: 4 }; // RGTC1
  363. if ( format === GPUTextureFormat.BC5RGUnorm || format === GPUTextureFormat.BC5RGSnorm ) return { byteLength: 16, width: 4, height: 4 }; // RGTC2
  364. if ( format === GPUTextureFormat.BC6HRGBUFloat || format === GPUTextureFormat.BC6HRGBFloat ) return { byteLength: 16, width: 4, height: 4 }; // BPTC (float)
  365. if ( format === GPUTextureFormat.BC7RGBAUnorm || format === GPUTextureFormat.BC7RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // BPTC (unorm)
  366. }
  367. _getBytesPerTexel( format ) {
  368. if ( format === GPUTextureFormat.R8Unorm ) return 1;
  369. if ( format === GPUTextureFormat.R16Float ) return 2;
  370. if ( format === GPUTextureFormat.RG8Unorm ) return 2;
  371. if ( format === GPUTextureFormat.RG16Float ) return 4;
  372. if ( format === GPUTextureFormat.R32Float ) return 4;
  373. if ( format === GPUTextureFormat.RGBA8Unorm || format === GPUTextureFormat.RGBA8UnormSRGB ) return 4;
  374. if ( format === GPUTextureFormat.RG32Float ) return 8;
  375. if ( format === GPUTextureFormat.RGBA16Float ) return 8;
  376. if ( format === GPUTextureFormat.RGBA32Float ) return 16;
  377. }
  378. _getDimension( texture ) {
  379. let dimension;
  380. if ( texture.isData3DTexture ) {
  381. dimension = GPUTextureDimension.ThreeD;
  382. } else {
  383. dimension = GPUTextureDimension.TwoD;
  384. }
  385. return dimension;
  386. }
  387. _getFormat( texture ) {
  388. const format = texture.format;
  389. const type = texture.type;
  390. const colorSpace = texture.colorSpace;
  391. let formatGPU;
  392. switch ( format ) {
  393. case RGBA_S3TC_DXT1_Format:
  394. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC1RGBAUnormSRGB : GPUTextureFormat.BC1RGBAUnorm;
  395. break;
  396. case RGBA_S3TC_DXT3_Format:
  397. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC2RGBAUnormSRGB : GPUTextureFormat.BC2RGBAUnorm;
  398. break;
  399. case RGBA_S3TC_DXT5_Format:
  400. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC3RGBAUnormSRGB : GPUTextureFormat.BC3RGBAUnorm;
  401. break;
  402. case DepthFormat:
  403. formatGPU = GPUTextureFormat.Depth32Float;
  404. break;
  405. case RGBAFormat:
  406. switch ( type ) {
  407. case UnsignedByteType:
  408. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.RGBA8UnormSRGB : GPUTextureFormat.RGBA8Unorm;
  409. break;
  410. case HalfFloatType:
  411. formatGPU = GPUTextureFormat.RGBA16Float;
  412. break;
  413. case FloatType:
  414. formatGPU = GPUTextureFormat.RGBA32Float;
  415. break;
  416. default:
  417. console.error( 'WebGPURenderer: Unsupported texture type with RGBAFormat.', type );
  418. }
  419. break;
  420. case RedFormat:
  421. switch ( type ) {
  422. case UnsignedByteType:
  423. formatGPU = GPUTextureFormat.R8Unorm;
  424. break;
  425. case HalfFloatType:
  426. formatGPU = GPUTextureFormat.R16Float;
  427. break;
  428. case FloatType:
  429. formatGPU = GPUTextureFormat.R32Float;
  430. break;
  431. default:
  432. console.error( 'WebGPURenderer: Unsupported texture type with RedFormat.', type );
  433. }
  434. break;
  435. case RGFormat:
  436. switch ( type ) {
  437. case UnsignedByteType:
  438. formatGPU = GPUTextureFormat.RG8Unorm;
  439. break;
  440. case HalfFloatType:
  441. formatGPU = GPUTextureFormat.RG16Float;
  442. break;
  443. case FloatType:
  444. formatGPU = GPUTextureFormat.RG32Float;
  445. break;
  446. default:
  447. console.error( 'WebGPURenderer: Unsupported texture type with RGFormat.', type );
  448. }
  449. break;
  450. default:
  451. console.error( 'WebGPURenderer: Unsupported texture format.', format );
  452. }
  453. return formatGPU;
  454. }
  455. _getImageBitmap( image, texture ) {
  456. const width = image.width;
  457. const height = image.height;
  458. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  459. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ) {
  460. const options = {};
  461. options.imageOrientation = ( texture.flipY === true ) ? 'flipY' : 'none';
  462. options.premultiplyAlpha = ( texture.premultiplyAlpha === true ) ? 'premultiply' : 'default';
  463. return createImageBitmap( image, 0, 0, width, height, options );
  464. } else {
  465. // assume ImageBitmap
  466. return Promise.resolve( image );
  467. }
  468. }
  469. _getMipLevelCount( texture, width, height, needsMipmaps ) {
  470. let mipLevelCount;
  471. if ( texture.isCompressedTexture ) {
  472. mipLevelCount = texture.mipmaps.length;
  473. } else if ( needsMipmaps === true ) {
  474. mipLevelCount = Math.floor( Math.log2( Math.max( width, height ) ) ) + 1;
  475. } else {
  476. mipLevelCount = 1; // a texture without mipmaps has a base mip (mipLevel 0)
  477. }
  478. return mipLevelCount;
  479. }
  480. _getSize( texture ) {
  481. const image = texture.image;
  482. let width, height, depth;
  483. if ( texture.isCubeTexture ) {
  484. const faceImage = image.length > 0 ? image[ 0 ].image || image[ 0 ] : null;
  485. width = faceImage ? faceImage.width : 1;
  486. height = faceImage ? faceImage.height : 1;
  487. depth = 6; // one image for each side of the cube map
  488. } else if ( image !== null ) {
  489. width = image.width;
  490. height = image.height;
  491. depth = ( image.depth !== undefined ) ? image.depth : 1;
  492. } else {
  493. width = height = depth = 1;
  494. }
  495. return { width, height, depth };
  496. }
  497. _needsMipmaps( texture ) {
  498. return ( texture.isCompressedTexture !== true ) && ( texture.generateMipmaps === true ) && ( texture.minFilter !== NearestFilter ) && ( texture.minFilter !== LinearFilter );
  499. }
  500. }
  501. function onRenderTargetDispose( event ) {
  502. const renderTarget = event.target;
  503. const properties = this.properties;
  504. const renderTargetProperties = properties.get( renderTarget );
  505. renderTarget.removeEventListener( 'dispose', renderTargetProperties.disposeCallback );
  506. renderTargetProperties.colorTextureGPU.destroy();
  507. properties.remove( renderTarget.texture );
  508. this.info.memory.textures --;
  509. if ( renderTarget.depthBuffer === true ) {
  510. renderTargetProperties.depthTextureGPU.destroy();
  511. this.info.memory.textures --;
  512. if ( renderTarget.depthTexture !== null ) {
  513. properties.remove( renderTarget.depthTexture );
  514. }
  515. }
  516. properties.remove( renderTarget );
  517. }
  518. function onTextureDispose( event ) {
  519. const texture = event.target;
  520. const textureProperties = this.properties.get( texture );
  521. textureProperties.textureGPU.destroy();
  522. texture.removeEventListener( 'dispose', textureProperties.disposeCallback );
  523. this.properties.remove( texture );
  524. this.info.memory.textures --;
  525. }
  526. export default WebGPUTextures;