WebGPUTextures.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. import { GPUTextureFormat, GPUAddressMode, GPUFilterMode, GPUTextureDimension } from './constants.js';
  2. import { VideoTexture, CubeTexture, Texture, NearestFilter, NearestMipmapNearestFilter, NearestMipmapLinearFilter, LinearFilter, RepeatWrapping, MirroredRepeatWrapping, RGB_ETC2_Format, RGBA_ETC2_EAC_Format,
  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 ) {
  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, textureGPU, textureGPUDescriptor, needsMipmaps );
  259. } else if ( texture.isCompressedTexture ) {
  260. this._copyCompressedBufferToTexture( texture.mipmaps, textureGPU, textureGPUDescriptor );
  261. } else if ( texture.isCubeTexture ) {
  262. if ( image.length === 6 ) {
  263. this._copyCubeMapToTexture( image, texture, textureGPU, textureGPUDescriptor, needsMipmaps );
  264. }
  265. } else if ( texture.isDepthTexture !== true && image !== null ) {
  266. this._copyImageToTexture( image, texture, textureGPU, textureGPUDescriptor, needsMipmaps );
  267. }
  268. //
  269. textureProperties.textureGPU = textureGPU;
  270. textureProperties.version = texture.version;
  271. return needsUpdate;
  272. }
  273. _copyBufferToTexture( image, textureGPU, textureGPUDescriptor, needsMipmaps, originDepth = 0 ) {
  274. // @TODO: Consider to use GPUCommandEncoder.copyBufferToTexture()
  275. // @TODO: Consider to support valid buffer layouts with other formats like RGB
  276. const data = image.data;
  277. const bytesPerTexel = this._getBytesPerTexel( textureGPUDescriptor.format );
  278. const bytesPerRow = image.width * bytesPerTexel;
  279. this.device.queue.writeTexture(
  280. {
  281. texture: textureGPU,
  282. mipLevel: 0,
  283. origin: { x: 0, y: 0, z: originDepth }
  284. },
  285. data,
  286. {
  287. offset: 0,
  288. bytesPerRow
  289. },
  290. {
  291. width: image.width,
  292. height: image.height,
  293. depthOrArrayLayers: ( image.depth !== undefined ) ? image.depth : 1
  294. } );
  295. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor, originDepth );
  296. }
  297. _copyCubeMapToTexture( images, 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, textureGPU, textureGPUDescriptor, needsMipmaps, i );
  302. } else {
  303. this._copyImageToTexture( image, texture, textureGPU, textureGPUDescriptor, needsMipmaps, i );
  304. }
  305. }
  306. }
  307. _copyExternalImageToTexture( image, textureGPU, textureGPUDescriptor, needsMipmaps, originDepth = 0 ) {
  308. this.device.queue.copyExternalImageToTexture(
  309. {
  310. source: image
  311. }, {
  312. texture: textureGPU,
  313. mipLevel: 0,
  314. origin: { x: 0, y: 0, z: originDepth }
  315. }, {
  316. width: image.width,
  317. height: image.height,
  318. depthOrArrayLayers: 1
  319. }
  320. );
  321. if ( needsMipmaps ) this._generateMipmaps( textureGPU, textureGPUDescriptor, originDepth );
  322. }
  323. _copyCompressedBufferToTexture( mipmaps, textureGPU, textureGPUDescriptor ) {
  324. // @TODO: Consider to use GPUCommandEncoder.copyBufferToTexture()
  325. const blockData = this._getBlockData( textureGPUDescriptor.format );
  326. for ( let i = 0; i < mipmaps.length; i ++ ) {
  327. const mipmap = mipmaps[ i ];
  328. const width = mipmap.width;
  329. const height = mipmap.height;
  330. const bytesPerRow = Math.ceil( width / blockData.width ) * blockData.byteLength;
  331. this.device.queue.writeTexture(
  332. {
  333. texture: textureGPU,
  334. mipLevel: i
  335. },
  336. mipmap.data,
  337. {
  338. offset: 0,
  339. bytesPerRow
  340. },
  341. {
  342. width: Math.ceil( width / blockData.width ) * blockData.width,
  343. height: Math.ceil( height / blockData.width ) * blockData.width,
  344. depthOrArrayLayers: 1
  345. } );
  346. }
  347. }
  348. _generateMipmaps( textureGPU, textureGPUDescriptor, baseArrayLayer ) {
  349. if ( this.utils === null ) {
  350. this.utils = new WebGPUTextureUtils( this.device ); // only create this helper if necessary
  351. }
  352. this.utils.generateMipmaps( textureGPU, textureGPUDescriptor, baseArrayLayer );
  353. }
  354. _getBlockData( format ) {
  355. // this method is only relevant for compressed texture formats
  356. if ( format === GPUTextureFormat.BC1RGBAUnorm || format === GPUTextureFormat.BC1RGBAUnormSRGB ) return { byteLength: 8, width: 4, height: 4 }; // DXT1
  357. if ( format === GPUTextureFormat.BC2RGBAUnorm || format === GPUTextureFormat.BC2RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // DXT3
  358. if ( format === GPUTextureFormat.BC3RGBAUnorm || format === GPUTextureFormat.BC3RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // DXT5
  359. if ( format === GPUTextureFormat.BC4RUnorm || format === GPUTextureFormat.BC4RSNorm ) return { byteLength: 8, width: 4, height: 4 }; // RGTC1
  360. if ( format === GPUTextureFormat.BC5RGUnorm || format === GPUTextureFormat.BC5RGSnorm ) return { byteLength: 16, width: 4, height: 4 }; // RGTC2
  361. if ( format === GPUTextureFormat.BC6HRGBUFloat || format === GPUTextureFormat.BC6HRGBFloat ) return { byteLength: 16, width: 4, height: 4 }; // BPTC (float)
  362. if ( format === GPUTextureFormat.BC7RGBAUnorm || format === GPUTextureFormat.BC7RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // BPTC (unorm)
  363. }
  364. _getBytesPerTexel( format ) {
  365. if ( format === GPUTextureFormat.R8Unorm ) return 1;
  366. if ( format === GPUTextureFormat.R16Float ) return 2;
  367. if ( format === GPUTextureFormat.RG8Unorm ) return 2;
  368. if ( format === GPUTextureFormat.RG16Float ) return 4;
  369. if ( format === GPUTextureFormat.R32Float ) return 4;
  370. if ( format === GPUTextureFormat.RGBA8Unorm || format === GPUTextureFormat.RGBA8UnormSRGB ) return 4;
  371. if ( format === GPUTextureFormat.RG32Float ) return 8;
  372. if ( format === GPUTextureFormat.RGBA16Float ) return 8;
  373. if ( format === GPUTextureFormat.RGBA32Float ) return 16;
  374. }
  375. _getDimension( texture ) {
  376. let dimension;
  377. if ( texture.isData3DTexture ) {
  378. dimension = GPUTextureDimension.ThreeD;
  379. } else {
  380. dimension = GPUTextureDimension.TwoD;
  381. }
  382. return dimension;
  383. }
  384. _getFormat( texture ) {
  385. const format = texture.format;
  386. const type = texture.type;
  387. const colorSpace = texture.colorSpace;
  388. let formatGPU;
  389. switch ( format ) {
  390. case RGBA_S3TC_DXT1_Format:
  391. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC1RGBAUnormSRGB : GPUTextureFormat.BC1RGBAUnorm;
  392. break;
  393. case RGBA_S3TC_DXT3_Format:
  394. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC2RGBAUnormSRGB : GPUTextureFormat.BC2RGBAUnorm;
  395. break;
  396. case RGBA_S3TC_DXT5_Format:
  397. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC3RGBAUnormSRGB : GPUTextureFormat.BC3RGBAUnorm;
  398. break;
  399. case RGB_ETC2_Format:
  400. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ETC2RGB8UnormSRGB : GPUTextureFormat.ETC2RGB8Unorm;
  401. break;
  402. case RGBA_ETC2_EAC_Format:
  403. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ETC2RGBA8UnormSRGB : GPUTextureFormat.ETC2RGBA8Unorm;
  404. break;
  405. case DepthFormat:
  406. formatGPU = GPUTextureFormat.Depth32Float;
  407. break;
  408. case RGBAFormat:
  409. switch ( type ) {
  410. case UnsignedByteType:
  411. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.RGBA8UnormSRGB : GPUTextureFormat.RGBA8Unorm;
  412. break;
  413. case HalfFloatType:
  414. formatGPU = GPUTextureFormat.RGBA16Float;
  415. break;
  416. case FloatType:
  417. formatGPU = GPUTextureFormat.RGBA32Float;
  418. break;
  419. default:
  420. console.error( 'WebGPURenderer: Unsupported texture type with RGBAFormat.', type );
  421. }
  422. break;
  423. case RedFormat:
  424. switch ( type ) {
  425. case UnsignedByteType:
  426. formatGPU = GPUTextureFormat.R8Unorm;
  427. break;
  428. case HalfFloatType:
  429. formatGPU = GPUTextureFormat.R16Float;
  430. break;
  431. case FloatType:
  432. formatGPU = GPUTextureFormat.R32Float;
  433. break;
  434. default:
  435. console.error( 'WebGPURenderer: Unsupported texture type with RedFormat.', type );
  436. }
  437. break;
  438. case RGFormat:
  439. switch ( type ) {
  440. case UnsignedByteType:
  441. formatGPU = GPUTextureFormat.RG8Unorm;
  442. break;
  443. case HalfFloatType:
  444. formatGPU = GPUTextureFormat.RG16Float;
  445. break;
  446. case FloatType:
  447. formatGPU = GPUTextureFormat.RG32Float;
  448. break;
  449. default:
  450. console.error( 'WebGPURenderer: Unsupported texture type with RGFormat.', type );
  451. }
  452. break;
  453. default:
  454. console.error( 'WebGPURenderer: Unsupported texture format.', format );
  455. }
  456. return formatGPU;
  457. }
  458. _isHTMLImage( image ) {
  459. return ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) || ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement );
  460. }
  461. _copyImageToTexture( image, texture, textureGPU, textureGPUDescriptor, needsMipmaps, originDepth ) {
  462. if ( this._isHTMLImage( image ) ) {
  463. this._getImageBitmapFromHTML( image, texture ).then( imageBitmap => {
  464. this._copyExternalImageToTexture( imageBitmap, textureGPU, textureGPUDescriptor, needsMipmaps, originDepth );
  465. } );
  466. } else {
  467. // assume ImageBitmap
  468. this._copyExternalImageToTexture( image, textureGPU, textureGPUDescriptor, needsMipmaps, originDepth );
  469. }
  470. }
  471. _getImageBitmapFromHTML( image, texture ) {
  472. const width = image.width;
  473. const height = image.height;
  474. const options = {};
  475. options.imageOrientation = ( texture.flipY === true ) ? 'flipY' : 'none';
  476. options.premultiplyAlpha = ( texture.premultiplyAlpha === true ) ? 'premultiply' : 'default';
  477. return createImageBitmap( image, 0, 0, width, height, options );
  478. }
  479. _getImageBitmap( image, texture ) {
  480. const width = image.width;
  481. const height = image.height;
  482. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  483. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ) {
  484. const options = {};
  485. options.imageOrientation = ( texture.flipY === true ) ? 'flipY' : 'none';
  486. options.premultiplyAlpha = ( texture.premultiplyAlpha === true ) ? 'premultiply' : 'default';
  487. return createImageBitmap( image, 0, 0, width, height, options );
  488. } else {
  489. // assume ImageBitmap
  490. return Promise.resolve( image );
  491. }
  492. }
  493. _getMipLevelCount( texture, width, height, needsMipmaps ) {
  494. let mipLevelCount;
  495. if ( texture.isCompressedTexture ) {
  496. mipLevelCount = texture.mipmaps.length;
  497. } else if ( needsMipmaps ) {
  498. mipLevelCount = Math.floor( Math.log2( Math.max( width, height ) ) ) + 1;
  499. } else {
  500. mipLevelCount = 1; // a texture without mipmaps has a base mip (mipLevel 0)
  501. }
  502. return mipLevelCount;
  503. }
  504. _getSize( texture ) {
  505. const image = texture.image;
  506. let width, height, depth;
  507. if ( texture.isCubeTexture ) {
  508. const faceImage = image.length > 0 ? image[ 0 ].image || image[ 0 ] : null;
  509. width = faceImage ? faceImage.width : 1;
  510. height = faceImage ? faceImage.height : 1;
  511. depth = 6; // one image for each side of the cube map
  512. } else if ( image !== null ) {
  513. width = image.width;
  514. height = image.height;
  515. depth = ( image.depth !== undefined ) ? image.depth : 1;
  516. } else {
  517. width = height = depth = 1;
  518. }
  519. return { width, height, depth };
  520. }
  521. _needsMipmaps( texture ) {
  522. return ( texture.isCompressedTexture !== true ) && ( texture.generateMipmaps === true ) && ( texture.minFilter !== NearestFilter ) && ( texture.minFilter !== LinearFilter );
  523. }
  524. }
  525. function onRenderTargetDispose( event ) {
  526. const renderTarget = event.target;
  527. const properties = this.properties;
  528. const renderTargetProperties = properties.get( renderTarget );
  529. renderTarget.removeEventListener( 'dispose', renderTargetProperties.disposeCallback );
  530. renderTargetProperties.colorTextureGPU.destroy();
  531. properties.remove( renderTarget.texture );
  532. this.info.memory.textures --;
  533. if ( renderTarget.depthBuffer === true ) {
  534. renderTargetProperties.depthTextureGPU.destroy();
  535. this.info.memory.textures --;
  536. if ( renderTarget.depthTexture !== null ) {
  537. properties.remove( renderTarget.depthTexture );
  538. }
  539. }
  540. properties.remove( renderTarget );
  541. }
  542. function onTextureDispose( event ) {
  543. const texture = event.target;
  544. const textureProperties = this.properties.get( texture );
  545. textureProperties.textureGPU.destroy();
  546. texture.removeEventListener( 'dispose', textureProperties.disposeCallback );
  547. this.properties.remove( texture );
  548. this.info.memory.textures --;
  549. }
  550. export default WebGPUTextures;