WebGPUTextureUtils.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. import {
  2. GPUTextureFormat, GPUAddressMode, GPUFilterMode, GPUTextureDimension, GPUFeatureName
  3. } from './WebGPUConstants.js';
  4. import {
  5. CubeTexture, Texture,
  6. NearestFilter, NearestMipmapNearestFilter, NearestMipmapLinearFilter, LinearFilter,
  7. RepeatWrapping, MirroredRepeatWrapping,
  8. RGB_ETC2_Format, RGBA_ETC2_EAC_Format,
  9. RGBAFormat, RedFormat, RGFormat, RGBA_S3TC_DXT1_Format, RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT5_Format, UnsignedByteType, FloatType, HalfFloatType, SRGBColorSpace, DepthFormat, DepthStencilFormat,
  10. 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,
  11. RGBA_ASTC_10x6_Format, RGBA_ASTC_10x8_Format, RGBA_ASTC_10x10_Format, RGBA_ASTC_12x10_Format, RGBA_ASTC_12x12_Format, UnsignedIntType, UnsignedShortType, UnsignedInt248Type,
  12. NeverCompare, AlwaysCompare, LessCompare, LessEqualCompare, EqualCompare, GreaterEqualCompare, GreaterCompare, NotEqualCompare
  13. } from 'three';
  14. import { CubeReflectionMapping, CubeRefractionMapping, EquirectangularReflectionMapping, EquirectangularRefractionMapping } from 'three';
  15. import WebGPUTextureMipmapUtils from './WebGPUTextureMipmapUtils.js';
  16. const _compareToWebGPU = {
  17. [ NeverCompare ]: 'never',
  18. [ AlwaysCompare ]: 'less',
  19. [ LessCompare ]: 'equal',
  20. [ LessEqualCompare ]: 'less-equal',
  21. [ EqualCompare ]: 'greater',
  22. [ GreaterEqualCompare ]: 'not-equal',
  23. [ GreaterCompare ]: 'greater-equal',
  24. [ NotEqualCompare ]: 'always'
  25. };
  26. class WebGPUTextureUtils {
  27. constructor( backend ) {
  28. this.backend = backend;
  29. this.mipmapUtils = null;
  30. this.defaultTexture = null;
  31. this.defaultCubeTexture = null;
  32. }
  33. createSampler( texture ) {
  34. const backend = this.backend;
  35. const device = backend.device;
  36. const textureGPU = backend.get( texture );
  37. const samplerDescriptorGPU = {
  38. addressModeU: this._convertAddressMode( texture.wrapS ),
  39. addressModeV: this._convertAddressMode( texture.wrapT ),
  40. addressModeW: this._convertAddressMode( texture.wrapR ),
  41. magFilter: this._convertFilterMode( texture.magFilter ),
  42. minFilter: this._convertFilterMode( texture.minFilter ),
  43. mipmapFilter: this._convertFilterMode( texture.minFilter ),
  44. maxAnisotropy: texture.anisotropy
  45. };
  46. if ( texture.isDepthTexture && texture.compareFunction !== null ) {
  47. samplerDescriptorGPU.compare = _compareToWebGPU[ texture.compareFunction ];
  48. }
  49. textureGPU.sampler = device.createSampler( samplerDescriptorGPU );
  50. }
  51. createDefaultTexture( texture ) {
  52. let textureGPU;
  53. if ( texture.isCubeTexture ) {
  54. textureGPU = this._getDefaultCubeTextureGPU();
  55. } else {
  56. textureGPU = this._getDefaultTextureGPU();
  57. }
  58. this.backend.get( texture ).texture = textureGPU;
  59. }
  60. createTexture( texture ) {
  61. const backend = this.backend;
  62. const textureData = backend.get( texture );
  63. if ( textureData.initialized ) {
  64. throw new Error( 'WebGPUTextureUtils: Texture already initialized.' );
  65. }
  66. const { width, height, depth } = this._getSize( texture );
  67. const needsMipmaps = this._needsMipmaps( texture );
  68. const dimension = this._getDimension( texture );
  69. const mipLevelCount = this._getMipLevelCount( texture, width, height, needsMipmaps );
  70. const format = texture.internalFormat || this._getFormat( texture );
  71. //const sampleCount = texture.isRenderTargetTexture || texture.isDepthTexture ? backend.utils.getSampleCount( renderContext ) : 1;
  72. const sampleCount = 1;
  73. let usage = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST;
  74. if ( texture.isCompressedTexture !== true ) {
  75. usage |= GPUTextureUsage.RENDER_ATTACHMENT;
  76. }
  77. const textureDescriptorGPU = {
  78. label: texture.name,
  79. size: {
  80. width: width,
  81. height: height,
  82. depthOrArrayLayers: depth,
  83. },
  84. mipLevelCount: mipLevelCount,
  85. sampleCount: sampleCount,
  86. dimension: dimension,
  87. format: format,
  88. usage: usage
  89. };
  90. // texture creation
  91. if ( texture.isVideoTexture ) {
  92. const video = texture.source.data;
  93. const videoFrame = new VideoFrame( video );
  94. textureDescriptorGPU.size.width = videoFrame.displayWidth;
  95. textureDescriptorGPU.size.height = videoFrame.displayHeight;
  96. videoFrame.close();
  97. textureData.externalTexture = video;
  98. } else {
  99. textureData.texture = backend.device.createTexture( textureDescriptorGPU );
  100. }
  101. textureData.initialized = true;
  102. textureData.needsMipmaps = needsMipmaps;
  103. textureData.textureDescriptorGPU = textureDescriptorGPU;
  104. }
  105. destroyTexture( texture ) {
  106. const backend = this.backend;
  107. const textureData = backend.get( texture );
  108. textureData.texture.destroy();
  109. backend.delete( texture );
  110. }
  111. destroySampler( texture ) {
  112. const backend = this.backend;
  113. const textureData = backend.get( texture );
  114. delete textureData.sampler;
  115. }
  116. generateMipmaps( texture ) {
  117. const textureData = this.backend.get( texture );
  118. if ( texture.isCubeTexture ) {
  119. for ( let i = 0; i < 6; i ++ ) {
  120. this._generateMipmaps( textureData.texture, textureData.textureDescriptorGPU, i );
  121. }
  122. } else {
  123. this._generateMipmaps( textureData.texture, textureData.textureDescriptorGPU );
  124. }
  125. }
  126. updateTexture( texture ) {
  127. const textureData = this.backend.get( texture );
  128. const { needsMipmaps, textureDescriptorGPU } = textureData;
  129. // transfer texture data
  130. if ( texture.isDataTexture || texture.isDataArrayTexture || texture.isData3DTexture ) {
  131. this._copyBufferToTexture( texture.image, textureData.texture, textureDescriptorGPU, needsMipmaps );
  132. } else if ( texture.isCompressedTexture ) {
  133. this._copyCompressedBufferToTexture( texture.mipmaps, textureData.texture, textureDescriptorGPU );
  134. } else if ( texture.isCubeTexture ) {
  135. if ( texture.image.length === 6 ) {
  136. this._copyCubeMapToTexture( texture.image, texture, textureData.texture, textureDescriptorGPU, needsMipmaps );
  137. }
  138. } else if ( texture.isRenderTargetTexture ) {
  139. if ( needsMipmaps === true ) this._generateMipmaps( textureData.texture, textureDescriptorGPU );
  140. } else if ( texture.isVideoTexture ) {
  141. const video = texture.source.data;
  142. textureData.externalTexture = video;
  143. } else if ( texture.image !== null ) {
  144. this._copyImageToTexture( texture.image, texture, textureData.texture, textureDescriptorGPU, needsMipmaps );
  145. } else {
  146. console.warn( 'WebGPUTextureUtils: Unable to update texture.' );
  147. }
  148. //
  149. textureData.version = texture.version;
  150. }
  151. _isEnvironmentTexture( texture ) {
  152. const mapping = texture.mapping;
  153. return ( mapping === EquirectangularReflectionMapping || mapping === EquirectangularRefractionMapping ) || ( mapping === CubeReflectionMapping || mapping === CubeRefractionMapping );
  154. }
  155. _getDefaultTextureGPU() {
  156. let defaultTexture = this.defaultTexture;
  157. if ( defaultTexture === null ) {
  158. const texture = new Texture();
  159. texture.minFilter = NearestFilter;
  160. texture.magFilter = NearestFilter;
  161. this.createTexture( texture );
  162. this.defaultTexture = defaultTexture = texture;
  163. }
  164. return this.backend.get( defaultTexture ).texture;
  165. }
  166. _getDefaultCubeTextureGPU() {
  167. let defaultCubeTexture = this.defaultTexture;
  168. if ( defaultCubeTexture === null ) {
  169. const texture = new CubeTexture();
  170. texture.minFilter = NearestFilter;
  171. texture.magFilter = NearestFilter;
  172. this.createTexture( texture );
  173. this.defaultCubeTexture = defaultCubeTexture = texture;
  174. }
  175. return this.backend.get( defaultCubeTexture ).texture;
  176. }
  177. _copyImageToTexture( image, texture, textureGPU, textureDescriptorGPU, needsMipmaps, originDepth ) {
  178. if ( this._isHTMLImage( image ) ) {
  179. this._getImageBitmapFromHTML( image, texture ).then( imageBitmap => {
  180. this._copyExternalImageToTexture( imageBitmap, textureGPU, textureDescriptorGPU, needsMipmaps, originDepth );
  181. } );
  182. } else {
  183. // assume ImageBitmap
  184. this._copyExternalImageToTexture( image, textureGPU, textureDescriptorGPU, needsMipmaps, originDepth );
  185. }
  186. }
  187. _isHTMLImage( image ) {
  188. return ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) || ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement );
  189. }
  190. _copyCubeMapToTexture( images, texture, textureGPU, textureDescriptorGPU, needsMipmaps ) {
  191. for ( let i = 0; i < 6; i ++ ) {
  192. const image = images[ i ];
  193. if ( image.isDataTexture ) {
  194. this._copyBufferToTexture( image.image, textureGPU, textureDescriptorGPU, needsMipmaps, i );
  195. } else {
  196. this._copyImageToTexture( image, texture, textureGPU, textureDescriptorGPU, needsMipmaps, i );
  197. }
  198. }
  199. }
  200. _copyExternalImageToTexture( image, textureGPU, textureDescriptorGPU, needsMipmaps, originDepth = 0 ) {
  201. const device = this.backend.device;
  202. device.queue.copyExternalImageToTexture(
  203. {
  204. source: image
  205. }, {
  206. texture: textureGPU,
  207. mipLevel: 0,
  208. origin: { x: 0, y: 0, z: originDepth }
  209. }, {
  210. width: image.width,
  211. height: image.height,
  212. depthOrArrayLayers: 1
  213. }
  214. );
  215. if ( needsMipmaps ) this._generateMipmaps( textureGPU, textureDescriptorGPU, originDepth );
  216. }
  217. _generateMipmaps( textureGPU, textureDescriptorGPU, baseArrayLayer = 0 ) {
  218. if ( this.mipmapUtils === null ) {
  219. this.mipmapUtils = new WebGPUTextureMipmapUtils( this.backend.device );
  220. }
  221. this.mipmapUtils.generateMipmaps( textureGPU, textureDescriptorGPU, baseArrayLayer );
  222. }
  223. _getImageBitmapFromHTML( image, texture ) {
  224. const width = image.width;
  225. const height = image.height;
  226. const options = {};
  227. options.imageOrientation = ( texture.flipY === true ) ? 'flipY' : 'none';
  228. options.premultiplyAlpha = ( texture.premultiplyAlpha === true ) ? 'premultiply' : 'default';
  229. return createImageBitmap( image, 0, 0, width, height, options );
  230. }
  231. _copyBufferToTexture( image, textureGPU, textureDescriptorGPU, needsMipmaps, originDepth = 0 ) {
  232. // @TODO: Consider to use GPUCommandEncoder.copyBufferToTexture()
  233. // @TODO: Consider to support valid buffer layouts with other formats like RGB
  234. const device = this.backend.device;
  235. const data = image.data;
  236. const bytesPerTexel = this._getBytesPerTexel( textureDescriptorGPU.format );
  237. const bytesPerRow = image.width * bytesPerTexel;
  238. device.queue.writeTexture(
  239. {
  240. texture: textureGPU,
  241. mipLevel: 0,
  242. origin: { x: 0, y: 0, z: originDepth }
  243. },
  244. data,
  245. {
  246. offset: 0,
  247. bytesPerRow
  248. },
  249. {
  250. width: image.width,
  251. height: image.height,
  252. depthOrArrayLayers: ( image.depth !== undefined ) ? image.depth : 1
  253. } );
  254. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureDescriptorGPU, originDepth );
  255. }
  256. _copyCompressedBufferToTexture( mipmaps, textureGPU, textureDescriptorGPU ) {
  257. // @TODO: Consider to use GPUCommandEncoder.copyBufferToTexture()
  258. const device = this.backend.device;
  259. const blockData = this._getBlockData( textureDescriptorGPU.format );
  260. for ( let i = 0; i < mipmaps.length; i ++ ) {
  261. const mipmap = mipmaps[ i ];
  262. const width = mipmap.width;
  263. const height = mipmap.height;
  264. const bytesPerRow = Math.ceil( width / blockData.width ) * blockData.byteLength;
  265. device.queue.writeTexture(
  266. {
  267. texture: textureGPU,
  268. mipLevel: i
  269. },
  270. mipmap.data,
  271. {
  272. offset: 0,
  273. bytesPerRow
  274. },
  275. {
  276. width: Math.ceil( width / blockData.width ) * blockData.width,
  277. height: Math.ceil( height / blockData.width ) * blockData.width,
  278. depthOrArrayLayers: 1
  279. }
  280. );
  281. }
  282. }
  283. _getBlockData( format ) {
  284. // this method is only relevant for compressed texture formats
  285. if ( format === GPUTextureFormat.BC1RGBAUnorm || format === GPUTextureFormat.BC1RGBAUnormSRGB ) return { byteLength: 8, width: 4, height: 4 }; // DXT1
  286. if ( format === GPUTextureFormat.BC2RGBAUnorm || format === GPUTextureFormat.BC2RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // DXT3
  287. if ( format === GPUTextureFormat.BC3RGBAUnorm || format === GPUTextureFormat.BC3RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // DXT5
  288. if ( format === GPUTextureFormat.BC4RUnorm || format === GPUTextureFormat.BC4RSNorm ) return { byteLength: 8, width: 4, height: 4 }; // RGTC1
  289. if ( format === GPUTextureFormat.BC5RGUnorm || format === GPUTextureFormat.BC5RGSnorm ) return { byteLength: 16, width: 4, height: 4 }; // RGTC2
  290. if ( format === GPUTextureFormat.BC6HRGBUFloat || format === GPUTextureFormat.BC6HRGBFloat ) return { byteLength: 16, width: 4, height: 4 }; // BPTC (float)
  291. if ( format === GPUTextureFormat.BC7RGBAUnorm || format === GPUTextureFormat.BC7RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // BPTC (unorm)
  292. if ( format === GPUTextureFormat.ETC2RGB8Unorm || format === GPUTextureFormat.ETC2RGB8UnormSRGB ) return { byteLength: 8, width: 4, height: 4 };
  293. if ( format === GPUTextureFormat.ETC2RGB8A1Unorm || format === GPUTextureFormat.ETC2RGB8A1UnormSRGB ) return { byteLength: 8, width: 4, height: 4 };
  294. if ( format === GPUTextureFormat.ETC2RGBA8Unorm || format === GPUTextureFormat.ETC2RGBA8UnormSRGB ) return { byteLength: 16, width: 4, height: 4 };
  295. if ( format === GPUTextureFormat.EACR11Unorm ) return { byteLength: 8, width: 4, height: 4 };
  296. if ( format === GPUTextureFormat.EACR11Snorm ) return { byteLength: 8, width: 4, height: 4 };
  297. if ( format === GPUTextureFormat.EACRG11Unorm ) return { byteLength: 16, width: 4, height: 4 };
  298. if ( format === GPUTextureFormat.EACRG11Snorm ) return { byteLength: 16, width: 4, height: 4 };
  299. if ( format === GPUTextureFormat.ASTC4x4Unorm || format === GPUTextureFormat.ASTC4x4UnormSRGB ) return { byteLength: 16, width: 4, height: 4 };
  300. if ( format === GPUTextureFormat.ASTC5x4Unorm || format === GPUTextureFormat.ASTC5x4UnormSRGB ) return { byteLength: 16, width: 5, height: 4 };
  301. if ( format === GPUTextureFormat.ASTC5x5Unorm || format === GPUTextureFormat.ASTC5x5UnormSRGB ) return { byteLength: 16, width: 5, height: 5 };
  302. if ( format === GPUTextureFormat.ASTC6x5Unorm || format === GPUTextureFormat.ASTC6x5UnormSRGB ) return { byteLength: 16, width: 6, height: 5 };
  303. if ( format === GPUTextureFormat.ASTC6x6Unorm || format === GPUTextureFormat.ASTC6x6UnormSRGB ) return { byteLength: 16, width: 6, height: 6 };
  304. if ( format === GPUTextureFormat.ASTC8x5Unorm || format === GPUTextureFormat.ASTC8x5UnormSRGB ) return { byteLength: 16, width: 8, height: 5 };
  305. if ( format === GPUTextureFormat.ASTC8x6Unorm || format === GPUTextureFormat.ASTC8x6UnormSRGB ) return { byteLength: 16, width: 8, height: 6 };
  306. if ( format === GPUTextureFormat.ASTC8x8Unorm || format === GPUTextureFormat.ASTC8x8UnormSRGB ) return { byteLength: 16, width: 8, height: 8 };
  307. if ( format === GPUTextureFormat.ASTC10x5Unorm || format === GPUTextureFormat.ASTC10x5UnormSRGB ) return { byteLength: 16, width: 10, height: 5 };
  308. if ( format === GPUTextureFormat.ASTC10x6Unorm || format === GPUTextureFormat.ASTC10x6UnormSRGB ) return { byteLength: 16, width: 10, height: 6 };
  309. if ( format === GPUTextureFormat.ASTC10x8Unorm || format === GPUTextureFormat.ASTC10x8UnormSRGB ) return { byteLength: 16, width: 10, height: 8 };
  310. if ( format === GPUTextureFormat.ASTC10x10Unorm || format === GPUTextureFormat.ASTC10x10UnormSRGB ) return { byteLength: 16, width: 10, height: 10 };
  311. if ( format === GPUTextureFormat.ASTC12x10Unorm || format === GPUTextureFormat.ASTC12x10UnormSRGB ) return { byteLength: 16, width: 12, height: 10 };
  312. if ( format === GPUTextureFormat.ASTC12x12Unorm || format === GPUTextureFormat.ASTC12x12UnormSRGB ) return { byteLength: 16, width: 12, height: 12 };
  313. }
  314. _convertAddressMode( value ) {
  315. let addressMode = GPUAddressMode.ClampToEdge;
  316. if ( value === RepeatWrapping ) {
  317. addressMode = GPUAddressMode.Repeat;
  318. } else if ( value === MirroredRepeatWrapping ) {
  319. addressMode = GPUAddressMode.MirrorRepeat;
  320. }
  321. return addressMode;
  322. }
  323. _convertFilterMode( value ) {
  324. let filterMode = GPUFilterMode.Linear;
  325. if ( value === NearestFilter || value === NearestMipmapNearestFilter || value === NearestMipmapLinearFilter ) {
  326. filterMode = GPUFilterMode.Nearest;
  327. }
  328. return filterMode;
  329. }
  330. _getSize( texture ) {
  331. const image = texture.image;
  332. let width, height, depth;
  333. if ( texture.isCubeTexture ) {
  334. const faceImage = image.length > 0 ? image[ 0 ].image || image[ 0 ] : null;
  335. width = faceImage ? faceImage.width : 1;
  336. height = faceImage ? faceImage.height : 1;
  337. depth = 6; // one image for each side of the cube map
  338. } else if ( image !== null ) {
  339. width = image.width;
  340. height = image.height;
  341. depth = ( image.depth !== undefined ) ? image.depth : 1;
  342. } else {
  343. width = height = depth = 1;
  344. }
  345. return { width, height, depth };
  346. }
  347. _needsMipmaps( texture ) {
  348. if ( this._isEnvironmentTexture( texture ) ) return true;
  349. return ( texture.isCompressedTexture !== true ) /*&& ( texture.generateMipmaps === true )*/ && ( texture.minFilter !== NearestFilter ) && ( texture.minFilter !== LinearFilter );
  350. }
  351. _getBytesPerTexel( format ) {
  352. if ( format === GPUTextureFormat.R8Unorm ) return 1;
  353. if ( format === GPUTextureFormat.R16Float ) return 2;
  354. if ( format === GPUTextureFormat.RG8Unorm ) return 2;
  355. if ( format === GPUTextureFormat.RG16Float ) return 4;
  356. if ( format === GPUTextureFormat.R32Float ) return 4;
  357. if ( format === GPUTextureFormat.RGBA8Unorm || format === GPUTextureFormat.RGBA8UnormSRGB ) return 4;
  358. if ( format === GPUTextureFormat.RG32Float ) return 8;
  359. if ( format === GPUTextureFormat.RGBA16Float ) return 8;
  360. if ( format === GPUTextureFormat.RGBA32Float ) return 16;
  361. }
  362. _getDimension( texture ) {
  363. let dimension;
  364. if ( texture.isData3DTexture ) {
  365. dimension = GPUTextureDimension.ThreeD;
  366. } else {
  367. dimension = GPUTextureDimension.TwoD;
  368. }
  369. return dimension;
  370. }
  371. _getMipLevelCount( texture, width, height, needsMipmaps ) {
  372. let mipLevelCount;
  373. if ( texture.isCompressedTexture ) {
  374. mipLevelCount = texture.mipmaps.length;
  375. } else if ( needsMipmaps ) {
  376. mipLevelCount = Math.floor( Math.log2( Math.max( width, height ) ) ) + 1;
  377. } else {
  378. mipLevelCount = 1; // a texture without mipmaps has a base mip (mipLevel 0)
  379. }
  380. return mipLevelCount;
  381. }
  382. _getFormat( texture ) {
  383. const format = texture.format;
  384. const type = texture.type;
  385. const colorSpace = texture.colorSpace;
  386. let formatGPU;
  387. if ( /*texture.isRenderTargetTexture === true ||*/ texture.isFramebufferTexture === true ) {
  388. formatGPU = GPUTextureFormat.BGRA8Unorm;
  389. } else if ( texture.isCompressedTexture === true ) {
  390. switch ( format ) {
  391. case RGBA_S3TC_DXT1_Format:
  392. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC1RGBAUnormSRGB : GPUTextureFormat.BC1RGBAUnorm;
  393. break;
  394. case RGBA_S3TC_DXT3_Format:
  395. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC2RGBAUnormSRGB : GPUTextureFormat.BC2RGBAUnorm;
  396. break;
  397. case RGBA_S3TC_DXT5_Format:
  398. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC3RGBAUnormSRGB : GPUTextureFormat.BC3RGBAUnorm;
  399. break;
  400. case RGB_ETC2_Format:
  401. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ETC2RGB8UnormSRGB : GPUTextureFormat.ETC2RGB8Unorm;
  402. break;
  403. case RGBA_ETC2_EAC_Format:
  404. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ETC2RGBA8UnormSRGB : GPUTextureFormat.ETC2RGBA8Unorm;
  405. break;
  406. case RGBA_ASTC_4x4_Format:
  407. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC4x4UnormSRGB : GPUTextureFormat.ASTC4x4Unorm;
  408. break;
  409. case RGBA_ASTC_5x4_Format:
  410. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC5x4UnormSRGB : GPUTextureFormat.ASTC5x4Unorm;
  411. break;
  412. case RGBA_ASTC_5x5_Format:
  413. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC5x5UnormSRGB : GPUTextureFormat.ASTC5x5Unorm;
  414. break;
  415. case RGBA_ASTC_6x5_Format:
  416. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC6x5UnormSRGB : GPUTextureFormat.ASTC6x5Unorm;
  417. break;
  418. case RGBA_ASTC_6x6_Format:
  419. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC6x6UnormSRGB : GPUTextureFormat.ASTC6x6Unorm;
  420. break;
  421. case RGBA_ASTC_8x5_Format:
  422. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC8x5UnormSRGB : GPUTextureFormat.ASTC8x5Unorm;
  423. break;
  424. case RGBA_ASTC_8x6_Format:
  425. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC8x6UnormSRGB : GPUTextureFormat.ASTC8x6Unorm;
  426. break;
  427. case RGBA_ASTC_8x8_Format:
  428. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC8x8UnormSRGB : GPUTextureFormat.ASTC8x8Unorm;
  429. break;
  430. case RGBA_ASTC_10x5_Format:
  431. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC10x5UnormSRGB : GPUTextureFormat.ASTC10x5Unorm;
  432. break;
  433. case RGBA_ASTC_10x6_Format:
  434. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC10x6UnormSRGB : GPUTextureFormat.ASTC10x6Unorm;
  435. break;
  436. case RGBA_ASTC_10x8_Format:
  437. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC10x8UnormSRGB : GPUTextureFormat.ASTC10x8Unorm;
  438. break;
  439. case RGBA_ASTC_10x10_Format:
  440. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC10x10UnormSRGB : GPUTextureFormat.ASTC10x10Unorm;
  441. break;
  442. case RGBA_ASTC_12x10_Format:
  443. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC12x10UnormSRGB : GPUTextureFormat.ASTC12x10Unorm;
  444. break;
  445. case RGBA_ASTC_12x12_Format:
  446. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC12x12UnormSRGB : GPUTextureFormat.ASTC12x12Unorm;
  447. break;
  448. default:
  449. console.error( 'WebGPURenderer: Unsupported texture format.', format );
  450. }
  451. } else {
  452. switch ( format ) {
  453. case RGBAFormat:
  454. switch ( type ) {
  455. case UnsignedByteType:
  456. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.RGBA8UnormSRGB : GPUTextureFormat.RGBA8Unorm;
  457. break;
  458. case HalfFloatType:
  459. formatGPU = GPUTextureFormat.RGBA16Float;
  460. break;
  461. case FloatType:
  462. formatGPU = GPUTextureFormat.RGBA32Float;
  463. break;
  464. default:
  465. console.error( 'WebGPURenderer: Unsupported texture type with RGBAFormat.', type );
  466. }
  467. break;
  468. case RedFormat:
  469. switch ( type ) {
  470. case UnsignedByteType:
  471. formatGPU = GPUTextureFormat.R8Unorm;
  472. break;
  473. case HalfFloatType:
  474. formatGPU = GPUTextureFormat.R16Float;
  475. break;
  476. case FloatType:
  477. formatGPU = GPUTextureFormat.R32Float;
  478. break;
  479. default:
  480. console.error( 'WebGPURenderer: Unsupported texture type with RedFormat.', type );
  481. }
  482. break;
  483. case RGFormat:
  484. switch ( type ) {
  485. case UnsignedByteType:
  486. formatGPU = GPUTextureFormat.RG8Unorm;
  487. break;
  488. case HalfFloatType:
  489. formatGPU = GPUTextureFormat.RG16Float;
  490. break;
  491. case FloatType:
  492. formatGPU = GPUTextureFormat.RG32Float;
  493. break;
  494. default:
  495. console.error( 'WebGPURenderer: Unsupported texture type with RGFormat.', type );
  496. }
  497. break;
  498. case DepthFormat:
  499. switch ( type ) {
  500. case UnsignedShortType:
  501. formatGPU = GPUTextureFormat.Depth16Unorm;
  502. break;
  503. case UnsignedIntType:
  504. formatGPU = GPUTextureFormat.Depth24Plus;
  505. break;
  506. case FloatType:
  507. formatGPU = GPUTextureFormat.Depth32Float;
  508. break;
  509. default:
  510. console.error( 'WebGPURenderer: Unsupported texture type with DepthFormat.', type );
  511. }
  512. break;
  513. case DepthStencilFormat:
  514. switch ( type ) {
  515. case UnsignedInt248Type:
  516. formatGPU = GPUTextureFormat.Depth24PlusStencil8;
  517. break;
  518. case FloatType:
  519. if ( this.device.features.has( GPUFeatureName.Depth32FloatStencil8 ) === false ) {
  520. console.error( 'WebGPURenderer: Depth textures with DepthStencilFormat + FloatType can only be used with the "depth32float-stencil8" GPU feature.' );
  521. }
  522. formatGPU = GPUTextureFormat.Depth32FloatStencil8;
  523. break;
  524. default:
  525. console.error( 'WebGPURenderer: Unsupported texture type with DepthStencilFormat.', type );
  526. }
  527. break;
  528. default:
  529. console.error( 'WebGPURenderer: Unsupported texture format.', format );
  530. }
  531. }
  532. return formatGPU;
  533. }
  534. }
  535. export default WebGPUTextureUtils;