WebGPUTextureUtils.js 27 KB

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