WebGPUTextureUtils.js 25 KB

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