WebGPUTextureUtils.js 31 KB

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