WebGPUTextureUtils.js 23 KB

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