WebGPUTextureUtils.js 25 KB

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