WebGPUTextureUtils.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. import {
  2. GPUTextureFormat, GPUAddressMode, GPUFilterMode, GPUTextureDimension, GPUFeatureName
  3. } from './WebGPUConstants.js';
  4. import {
  5. ByteType, ShortType, 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 ) {
  203. this._copyBufferToTexture( options.image, textureData.texture, textureDescriptorGPU, 0, texture.flipY );
  204. } else if ( texture.isDataArrayTexture || texture.isData3DTexture ) {
  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 ByteType:
  600. formatGPU = GPUTextureFormat.RGBA8Snorm;
  601. break;
  602. case ShortType:
  603. formatGPU = GPUTextureFormat.RGBA16Sint;
  604. break;
  605. case UnsignedShortType:
  606. formatGPU = GPUTextureFormat.RGBA16Uint;
  607. break;
  608. case UnsignedIntType:
  609. formatGPU = GPUTextureFormat.RGBA32Uint;
  610. break;
  611. case IntType:
  612. formatGPU = GPUTextureFormat.RGBA32Sint;
  613. break;
  614. case UnsignedByteType:
  615. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.RGBA8UnormSRGB : GPUTextureFormat.RGBA8Unorm;
  616. break;
  617. case HalfFloatType:
  618. formatGPU = GPUTextureFormat.RGBA16Float;
  619. break;
  620. case FloatType:
  621. formatGPU = GPUTextureFormat.RGBA32Float;
  622. break;
  623. default:
  624. console.error( 'WebGPURenderer: Unsupported texture type with RGBAFormat.', type );
  625. }
  626. break;
  627. case RGBFormat:
  628. switch ( type ) {
  629. case UnsignedInt5999Type:
  630. formatGPU = GPUTextureFormat.RGB9E5UFloat;
  631. break;
  632. default:
  633. console.error( 'WebGPURenderer: Unsupported texture type with RGBFormat.', type );
  634. }
  635. break;
  636. case RedFormat:
  637. switch ( type ) {
  638. case ByteType:
  639. formatGPU = GPUTextureFormat.R8Snorm;
  640. break;
  641. case ShortType:
  642. formatGPU = GPUTextureFormat.R16Sint;
  643. break;
  644. case UnsignedShortType:
  645. formatGPU = GPUTextureFormat.R16Uint;
  646. break;
  647. case UnsignedIntType:
  648. formatGPU = GPUTextureFormat.R32Uint;
  649. break;
  650. case IntType:
  651. formatGPU = GPUTextureFormat.R32Sint;
  652. break;
  653. case UnsignedByteType:
  654. formatGPU = GPUTextureFormat.R8Unorm;
  655. break;
  656. case HalfFloatType:
  657. formatGPU = GPUTextureFormat.R16Float;
  658. break;
  659. case FloatType:
  660. formatGPU = GPUTextureFormat.R32Float;
  661. break;
  662. default:
  663. console.error( 'WebGPURenderer: Unsupported texture type with RedFormat.', type );
  664. }
  665. break;
  666. case RGFormat:
  667. switch ( type ) {
  668. case ByteType:
  669. formatGPU = GPUTextureFormat.RG8Snorm;
  670. break;
  671. case ShortType:
  672. formatGPU = GPUTextureFormat.RG16Sint;
  673. break;
  674. case UnsignedShortType:
  675. formatGPU = GPUTextureFormat.RG16Uint;
  676. break;
  677. case UnsignedIntType:
  678. formatGPU = GPUTextureFormat.RG32Uint;
  679. break;
  680. case IntType:
  681. formatGPU = GPUTextureFormat.RG32Sint;
  682. break;
  683. case UnsignedByteType:
  684. formatGPU = GPUTextureFormat.RG8Unorm;
  685. break;
  686. case HalfFloatType:
  687. formatGPU = GPUTextureFormat.RG16Float;
  688. break;
  689. case FloatType:
  690. formatGPU = GPUTextureFormat.RG32Float;
  691. break;
  692. default:
  693. console.error( 'WebGPURenderer: Unsupported texture type with RGFormat.', type );
  694. }
  695. break;
  696. case DepthFormat:
  697. switch ( type ) {
  698. case UnsignedShortType:
  699. formatGPU = GPUTextureFormat.Depth16Unorm;
  700. break;
  701. case UnsignedIntType:
  702. formatGPU = GPUTextureFormat.Depth24Plus;
  703. break;
  704. case FloatType:
  705. formatGPU = GPUTextureFormat.Depth32Float;
  706. break;
  707. default:
  708. console.error( 'WebGPURenderer: Unsupported texture type with DepthFormat.', type );
  709. }
  710. break;
  711. case DepthStencilFormat:
  712. switch ( type ) {
  713. case UnsignedInt248Type:
  714. formatGPU = GPUTextureFormat.Depth24PlusStencil8;
  715. break;
  716. case FloatType:
  717. if ( device && device.features.has( GPUFeatureName.Depth32FloatStencil8 ) === false ) {
  718. console.error( 'WebGPURenderer: Depth textures with DepthStencilFormat + FloatType can only be used with the "depth32float-stencil8" GPU feature.' );
  719. }
  720. formatGPU = GPUTextureFormat.Depth32FloatStencil8;
  721. break;
  722. default:
  723. console.error( 'WebGPURenderer: Unsupported texture type with DepthStencilFormat.', type );
  724. }
  725. break;
  726. case RedIntegerFormat:
  727. switch ( type ) {
  728. case IntType:
  729. formatGPU = GPUTextureFormat.R32Sint;
  730. break;
  731. case UnsignedIntType:
  732. formatGPU = GPUTextureFormat.R32Uint;
  733. break;
  734. default:
  735. console.error( 'WebGPURenderer: Unsupported texture type with RedIntegerFormat.', type );
  736. }
  737. break;
  738. case RGIntegerFormat:
  739. switch ( type ) {
  740. case IntType:
  741. formatGPU = GPUTextureFormat.RG32Sint;
  742. break;
  743. case UnsignedIntType:
  744. formatGPU = GPUTextureFormat.RG32Uint;
  745. break;
  746. default:
  747. console.error( 'WebGPURenderer: Unsupported texture type with RGIntegerFormat.', type );
  748. }
  749. break;
  750. case RGBAIntegerFormat:
  751. switch ( type ) {
  752. case IntType:
  753. formatGPU = GPUTextureFormat.RGBA32Sint;
  754. break;
  755. case UnsignedIntType:
  756. formatGPU = GPUTextureFormat.RGBA32Uint;
  757. break;
  758. default:
  759. console.error( 'WebGPURenderer: Unsupported texture type with RGBAIntegerFormat.', type );
  760. }
  761. break;
  762. default:
  763. console.error( 'WebGPURenderer: Unsupported texture format.', format );
  764. }
  765. }
  766. return formatGPU;
  767. }
  768. export default WebGPUTextureUtils;