WebGPUTextureUtils.js 27 KB

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