WebGPUTextureUtils.js 27 KB

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