WebGPUTextures.js 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. import { GPUTextureFormat, GPUAddressMode, GPUFilterMode, GPUTextureDimension, GPUFeatureName } 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, DepthStencilFormat, DepthTexture,
  4. RGBA_ASTC_4x4_Format, RGBA_ASTC_5x4_Format, RGBA_ASTC_5x5_Format, RGBA_ASTC_6x5_Format, RGBA_ASTC_6x6_Format, RGBA_ASTC_8x5_Format, RGBA_ASTC_8x6_Format, RGBA_ASTC_8x8_Format, RGBA_ASTC_10x5_Format,
  5. RGBA_ASTC_10x6_Format, RGBA_ASTC_10x8_Format, RGBA_ASTC_10x10_Format, RGBA_ASTC_12x10_Format, RGBA_ASTC_12x12_Format, UnsignedIntType, UnsignedShortType, UnsignedInt248Type
  6. } from 'three';
  7. import WebGPUTextureUtils from './WebGPUTextureUtils.js';
  8. class WebGPUTextures {
  9. constructor( device, properties, info ) {
  10. this.device = device;
  11. this.properties = properties;
  12. this.info = info;
  13. this.defaultTexture = null;
  14. this.depthDefaultTexture = null;
  15. this.defaultVideoTexture = null;
  16. this.defaultCubeTexture = null;
  17. this.defaultSampler = null;
  18. this.samplerCache = new Map();
  19. this.utils = null;
  20. }
  21. getDefaultSampler() {
  22. if ( this.defaultSampler === null ) {
  23. this.defaultSampler = this.device.createSampler( {} );
  24. }
  25. return this.defaultSampler;
  26. }
  27. getDefaultDepthTexture() {
  28. if ( this.depthDefaultTexture === null ) {
  29. const depthTexture = new DepthTexture();
  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. getDefaultVideoTexture() {
  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. if ( format === GPUTextureFormat.ETC2RGB8Unorm || format === GPUTextureFormat.ETC2RGB8UnormSRGB ) return { byteLength: 8, width: 4, height: 4 };
  364. if ( format === GPUTextureFormat.ETC2RGB8A1Unorm || format === GPUTextureFormat.ETC2RGB8A1UnormSRGB ) return { byteLength: 8, width: 4, height: 4 };
  365. if ( format === GPUTextureFormat.ETC2RGBA8Unorm || format === GPUTextureFormat.ETC2RGBA8UnormSRGB ) return { byteLength: 16, width: 4, height: 4 };
  366. if ( format === GPUTextureFormat.EACR11Unorm ) return { byteLength: 8, width: 4, height: 4 };
  367. if ( format === GPUTextureFormat.EACR11Snorm ) return { byteLength: 8, width: 4, height: 4 };
  368. if ( format === GPUTextureFormat.EACRG11Unorm ) return { byteLength: 16, width: 4, height: 4 };
  369. if ( format === GPUTextureFormat.EACRG11Snorm ) return { byteLength: 16, width: 4, height: 4 };
  370. if ( format === GPUTextureFormat.ASTC4x4Unorm || format === GPUTextureFormat.ASTC4x4UnormSRGB ) return { byteLength: 16, width: 4, height: 4 };
  371. if ( format === GPUTextureFormat.ASTC5x4Unorm || format === GPUTextureFormat.ASTC5x4UnormSRGB ) return { byteLength: 16, width: 5, height: 4 };
  372. if ( format === GPUTextureFormat.ASTC5x5Unorm || format === GPUTextureFormat.ASTC5x5UnormSRGB ) return { byteLength: 16, width: 5, height: 5 };
  373. if ( format === GPUTextureFormat.ASTC6x5Unorm || format === GPUTextureFormat.ASTC6x5UnormSRGB ) return { byteLength: 16, width: 6, height: 5 };
  374. if ( format === GPUTextureFormat.ASTC6x6Unorm || format === GPUTextureFormat.ASTC6x6UnormSRGB ) return { byteLength: 16, width: 6, height: 6 };
  375. if ( format === GPUTextureFormat.ASTC8x5Unorm || format === GPUTextureFormat.ASTC8x5UnormSRGB ) return { byteLength: 16, width: 8, height: 5 };
  376. if ( format === GPUTextureFormat.ASTC8x6Unorm || format === GPUTextureFormat.ASTC8x6UnormSRGB ) return { byteLength: 16, width: 8, height: 6 };
  377. if ( format === GPUTextureFormat.ASTC8x8Unorm || format === GPUTextureFormat.ASTC8x8UnormSRGB ) return { byteLength: 16, width: 8, height: 8 };
  378. if ( format === GPUTextureFormat.ASTC10x5Unorm || format === GPUTextureFormat.ASTC10x5UnormSRGB ) return { byteLength: 16, width: 10, height: 5 };
  379. if ( format === GPUTextureFormat.ASTC10x6Unorm || format === GPUTextureFormat.ASTC10x6UnormSRGB ) return { byteLength: 16, width: 10, height: 6 };
  380. if ( format === GPUTextureFormat.ASTC10x8Unorm || format === GPUTextureFormat.ASTC10x8UnormSRGB ) return { byteLength: 16, width: 10, height: 8 };
  381. if ( format === GPUTextureFormat.ASTC10x10Unorm || format === GPUTextureFormat.ASTC10x10UnormSRGB ) return { byteLength: 16, width: 10, height: 10 };
  382. if ( format === GPUTextureFormat.ASTC12x10Unorm || format === GPUTextureFormat.ASTC12x10UnormSRGB ) return { byteLength: 16, width: 12, height: 10 };
  383. if ( format === GPUTextureFormat.ASTC12x12Unorm || format === GPUTextureFormat.ASTC12x12UnormSRGB ) return { byteLength: 16, width: 12, height: 12 };
  384. }
  385. _getBytesPerTexel( format ) {
  386. if ( format === GPUTextureFormat.R8Unorm ) return 1;
  387. if ( format === GPUTextureFormat.R16Float ) return 2;
  388. if ( format === GPUTextureFormat.RG8Unorm ) return 2;
  389. if ( format === GPUTextureFormat.RG16Float ) return 4;
  390. if ( format === GPUTextureFormat.R32Float ) return 4;
  391. if ( format === GPUTextureFormat.RGBA8Unorm || format === GPUTextureFormat.RGBA8UnormSRGB ) return 4;
  392. if ( format === GPUTextureFormat.RG32Float ) return 8;
  393. if ( format === GPUTextureFormat.RGBA16Float ) return 8;
  394. if ( format === GPUTextureFormat.RGBA32Float ) return 16;
  395. }
  396. _getDimension( texture ) {
  397. let dimension;
  398. if ( texture.isData3DTexture ) {
  399. dimension = GPUTextureDimension.ThreeD;
  400. } else {
  401. dimension = GPUTextureDimension.TwoD;
  402. }
  403. return dimension;
  404. }
  405. _getFormat( texture ) {
  406. const format = texture.format;
  407. const type = texture.type;
  408. const colorSpace = texture.colorSpace;
  409. let formatGPU;
  410. if ( texture.isCompressedTexture === true ) {
  411. switch ( format ) {
  412. case RGBA_S3TC_DXT1_Format:
  413. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC1RGBAUnormSRGB : GPUTextureFormat.BC1RGBAUnorm;
  414. break;
  415. case RGBA_S3TC_DXT3_Format:
  416. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC2RGBAUnormSRGB : GPUTextureFormat.BC2RGBAUnorm;
  417. break;
  418. case RGBA_S3TC_DXT5_Format:
  419. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC3RGBAUnormSRGB : GPUTextureFormat.BC3RGBAUnorm;
  420. break;
  421. case RGB_ETC2_Format:
  422. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ETC2RGB8UnormSRGB : GPUTextureFormat.ETC2RGB8Unorm;
  423. break;
  424. case RGBA_ETC2_EAC_Format:
  425. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ETC2RGBA8UnormSRGB : GPUTextureFormat.ETC2RGBA8Unorm;
  426. break;
  427. case RGBA_ASTC_4x4_Format:
  428. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC4x4UnormSRGB : GPUTextureFormat.ASTC4x4Unorm;
  429. break;
  430. case RGBA_ASTC_5x4_Format:
  431. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC5x4UnormSRGB : GPUTextureFormat.ASTC5x4Unorm;
  432. break;
  433. case RGBA_ASTC_5x5_Format:
  434. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC5x5UnormSRGB : GPUTextureFormat.ASTC5x5Unorm;
  435. break;
  436. case RGBA_ASTC_6x5_Format:
  437. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC6x5UnormSRGB : GPUTextureFormat.ASTC6x5Unorm;
  438. break;
  439. case RGBA_ASTC_6x6_Format:
  440. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC6x6UnormSRGB : GPUTextureFormat.ASTC6x6Unorm;
  441. break;
  442. case RGBA_ASTC_8x5_Format:
  443. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC8x5UnormSRGB : GPUTextureFormat.ASTC8x5Unorm;
  444. break;
  445. case RGBA_ASTC_8x6_Format:
  446. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC8x6UnormSRGB : GPUTextureFormat.ASTC8x6Unorm;
  447. break;
  448. case RGBA_ASTC_8x8_Format:
  449. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC8x8UnormSRGB : GPUTextureFormat.ASTC8x8Unorm;
  450. break;
  451. case RGBA_ASTC_10x5_Format:
  452. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC10x5UnormSRGB : GPUTextureFormat.ASTC10x5Unorm;
  453. break;
  454. case RGBA_ASTC_10x6_Format:
  455. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC10x6UnormSRGB : GPUTextureFormat.ASTC10x6Unorm;
  456. break;
  457. case RGBA_ASTC_10x8_Format:
  458. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC10x8UnormSRGB : GPUTextureFormat.ASTC10x8Unorm;
  459. break;
  460. case RGBA_ASTC_10x10_Format:
  461. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC10x10UnormSRGB : GPUTextureFormat.ASTC10x10Unorm;
  462. break;
  463. case RGBA_ASTC_12x10_Format:
  464. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC12x10UnormSRGB : GPUTextureFormat.ASTC12x10Unorm;
  465. break;
  466. case RGBA_ASTC_12x12_Format:
  467. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC12x12UnormSRGB : GPUTextureFormat.ASTC12x12Unorm;
  468. break;
  469. default:
  470. console.error( 'WebGPURenderer: Unsupported texture format.', format );
  471. }
  472. } else {
  473. switch ( format ) {
  474. case RGBAFormat:
  475. switch ( type ) {
  476. case UnsignedByteType:
  477. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.RGBA8UnormSRGB : GPUTextureFormat.RGBA8Unorm;
  478. break;
  479. case HalfFloatType:
  480. formatGPU = GPUTextureFormat.RGBA16Float;
  481. break;
  482. case FloatType:
  483. formatGPU = GPUTextureFormat.RGBA32Float;
  484. break;
  485. default:
  486. console.error( 'WebGPURenderer: Unsupported texture type with RGBAFormat.', type );
  487. }
  488. break;
  489. case RedFormat:
  490. switch ( type ) {
  491. case UnsignedByteType:
  492. formatGPU = GPUTextureFormat.R8Unorm;
  493. break;
  494. case HalfFloatType:
  495. formatGPU = GPUTextureFormat.R16Float;
  496. break;
  497. case FloatType:
  498. formatGPU = GPUTextureFormat.R32Float;
  499. break;
  500. default:
  501. console.error( 'WebGPURenderer: Unsupported texture type with RedFormat.', type );
  502. }
  503. break;
  504. case RGFormat:
  505. switch ( type ) {
  506. case UnsignedByteType:
  507. formatGPU = GPUTextureFormat.RG8Unorm;
  508. break;
  509. case HalfFloatType:
  510. formatGPU = GPUTextureFormat.RG16Float;
  511. break;
  512. case FloatType:
  513. formatGPU = GPUTextureFormat.RG32Float;
  514. break;
  515. default:
  516. console.error( 'WebGPURenderer: Unsupported texture type with RGFormat.', type );
  517. }
  518. break;
  519. case DepthFormat:
  520. switch ( type ) {
  521. case UnsignedShortType:
  522. formatGPU = GPUTextureFormat.Depth16Unorm;
  523. break;
  524. case UnsignedIntType:
  525. formatGPU = GPUTextureFormat.Depth24Plus;
  526. break;
  527. case FloatType:
  528. formatGPU = GPUTextureFormat.Depth32Float;
  529. break;
  530. default:
  531. console.error( 'WebGPURenderer: Unsupported texture type with DepthFormat.', type );
  532. }
  533. break;
  534. case DepthStencilFormat:
  535. switch ( type ) {
  536. case UnsignedInt248Type:
  537. formatGPU = GPUTextureFormat.Depth24PlusStencil8;
  538. break;
  539. case FloatType:
  540. if ( this.device.features.has( GPUFeatureName.Depth32FloatStencil8 ) === false ) {
  541. console.error( 'WebGPURenderer: Depth textures with DepthStencilFormat + FloatType can only be used with the "depth32float-stencil8" GPU feature.' );
  542. }
  543. formatGPU = GPUTextureFormat.Depth32FloatStencil8;
  544. break;
  545. default:
  546. console.error( 'WebGPURenderer: Unsupported texture type with DepthStencilFormat.', type );
  547. }
  548. break;
  549. default:
  550. console.error( 'WebGPURenderer: Unsupported texture format.', format );
  551. }
  552. }
  553. return formatGPU;
  554. }
  555. _isHTMLImage( image ) {
  556. return ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) || ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement );
  557. }
  558. _copyImageToTexture( image, texture, textureGPU, textureGPUDescriptor, needsMipmaps, originDepth ) {
  559. if ( this._isHTMLImage( image ) ) {
  560. this._getImageBitmapFromHTML( image, texture ).then( imageBitmap => {
  561. this._copyExternalImageToTexture( imageBitmap, textureGPU, textureGPUDescriptor, needsMipmaps, originDepth );
  562. } );
  563. } else {
  564. // assume ImageBitmap
  565. this._copyExternalImageToTexture( image, textureGPU, textureGPUDescriptor, needsMipmaps, originDepth );
  566. }
  567. }
  568. _getImageBitmapFromHTML( image, texture ) {
  569. const width = image.width;
  570. const height = image.height;
  571. const options = {};
  572. options.imageOrientation = ( texture.flipY === true ) ? 'flipY' : 'none';
  573. options.premultiplyAlpha = ( texture.premultiplyAlpha === true ) ? 'premultiply' : 'default';
  574. return createImageBitmap( image, 0, 0, width, height, options );
  575. }
  576. _getImageBitmap( image, texture ) {
  577. const width = image.width;
  578. const height = image.height;
  579. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  580. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ) {
  581. const options = {};
  582. options.imageOrientation = ( texture.flipY === true ) ? 'flipY' : 'none';
  583. options.premultiplyAlpha = ( texture.premultiplyAlpha === true ) ? 'premultiply' : 'default';
  584. return createImageBitmap( image, 0, 0, width, height, options );
  585. } else {
  586. // assume ImageBitmap
  587. return Promise.resolve( image );
  588. }
  589. }
  590. _getMipLevelCount( texture, width, height, needsMipmaps ) {
  591. let mipLevelCount;
  592. if ( texture.isCompressedTexture ) {
  593. mipLevelCount = texture.mipmaps.length;
  594. } else if ( needsMipmaps ) {
  595. mipLevelCount = Math.floor( Math.log2( Math.max( width, height ) ) ) + 1;
  596. } else {
  597. mipLevelCount = 1; // a texture without mipmaps has a base mip (mipLevel 0)
  598. }
  599. return mipLevelCount;
  600. }
  601. _getSize( texture ) {
  602. const image = texture.image;
  603. let width, height, depth;
  604. if ( texture.isCubeTexture ) {
  605. const faceImage = image.length > 0 ? image[ 0 ].image || image[ 0 ] : null;
  606. width = faceImage ? faceImage.width : 1;
  607. height = faceImage ? faceImage.height : 1;
  608. depth = 6; // one image for each side of the cube map
  609. } else if ( image !== null ) {
  610. width = image.width;
  611. height = image.height;
  612. depth = ( image.depth !== undefined ) ? image.depth : 1;
  613. } else {
  614. width = height = depth = 1;
  615. }
  616. return { width, height, depth };
  617. }
  618. _needsMipmaps( texture ) {
  619. return ( texture.isCompressedTexture !== true ) && ( texture.generateMipmaps === true ) && ( texture.minFilter !== NearestFilter ) && ( texture.minFilter !== LinearFilter );
  620. }
  621. }
  622. function onRenderTargetDispose( event ) {
  623. const renderTarget = event.target;
  624. const properties = this.properties;
  625. const renderTargetProperties = properties.get( renderTarget );
  626. renderTarget.removeEventListener( 'dispose', renderTargetProperties.disposeCallback );
  627. renderTargetProperties.colorTextureGPU.destroy();
  628. properties.remove( renderTarget.texture );
  629. this.info.memory.textures --;
  630. if ( renderTarget.depthBuffer === true ) {
  631. renderTargetProperties.depthTextureGPU.destroy();
  632. this.info.memory.textures --;
  633. if ( renderTarget.depthTexture !== null ) {
  634. properties.remove( renderTarget.depthTexture );
  635. }
  636. }
  637. properties.remove( renderTarget );
  638. }
  639. function onTextureDispose( event ) {
  640. const texture = event.target;
  641. const textureProperties = this.properties.get( texture );
  642. textureProperties.textureGPU.destroy();
  643. texture.removeEventListener( 'dispose', textureProperties.disposeCallback );
  644. this.properties.remove( texture );
  645. this.info.memory.textures --;
  646. }
  647. export default WebGPUTextures;