WebGPUTextures.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. import { GPUTextureFormat, GPUAddressMode, GPUFilterMode, GPUTextureDimension } from './constants.js';
  2. import { CubeTexture, Texture, NearestFilter, NearestMipmapNearestFilter, NearestMipmapLinearFilter, LinearFilter, RepeatWrapping, MirroredRepeatWrapping,
  3. RGBAFormat, RedFormat, RGFormat, RGBA_S3TC_DXT1_Format, RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT5_Format, UnsignedByteType, FloatType, HalfFloatType, sRGBEncoding
  4. } from 'three';
  5. import WebGPUTextureUtils from './WebGPUTextureUtils.js';
  6. class WebGPUTextures {
  7. constructor( device, properties, info ) {
  8. this.device = device;
  9. this.properties = properties;
  10. this.info = info;
  11. this.defaultTexture = null;
  12. this.defaultCubeTexture = null;
  13. this.defaultSampler = null;
  14. this.samplerCache = new Map();
  15. this.utils = null;
  16. }
  17. getDefaultSampler() {
  18. if ( this.defaultSampler === null ) {
  19. this.defaultSampler = this.device.createSampler( {} );
  20. }
  21. return this.defaultSampler;
  22. }
  23. getDefaultTexture() {
  24. if ( this.defaultTexture === null ) {
  25. const texture = new Texture();
  26. texture.minFilter = NearestFilter;
  27. texture.magFilter = NearestFilter;
  28. this._uploadTexture( texture );
  29. this.defaultTexture = this.getTextureGPU( texture );
  30. }
  31. return this.defaultTexture;
  32. }
  33. getDefaultCubeTexture() {
  34. if ( this.defaultCubeTexture === null ) {
  35. const texture = new CubeTexture();
  36. texture.minFilter = NearestFilter;
  37. texture.magFilter = NearestFilter;
  38. this._uploadTexture( texture );
  39. this.defaultCubeTexture = this.getTextureGPU( texture );
  40. }
  41. return this.defaultCubeTexture;
  42. }
  43. getTextureGPU( texture ) {
  44. const textureProperties = this.properties.get( texture );
  45. return textureProperties.textureGPU;
  46. }
  47. getSampler( texture ) {
  48. const textureProperties = this.properties.get( texture );
  49. return textureProperties.samplerGPU;
  50. }
  51. updateTexture( texture ) {
  52. let needsUpdate = false;
  53. const textureProperties = this.properties.get( texture );
  54. if ( texture.version > 0 && textureProperties.version !== texture.version ) {
  55. const image = texture.image;
  56. if ( image === undefined ) {
  57. console.warn( 'THREE.WebGPURenderer: Texture marked for update but image is undefined.' );
  58. } else if ( image.complete === false ) {
  59. console.warn( 'THREE.WebGPURenderer: Texture marked for update but image is incomplete.' );
  60. } else {
  61. // texture init
  62. if ( textureProperties.initialized === undefined ) {
  63. textureProperties.initialized = true;
  64. const disposeCallback = onTextureDispose.bind( this );
  65. textureProperties.disposeCallback = disposeCallback;
  66. texture.addEventListener( 'dispose', disposeCallback );
  67. this.info.memory.textures ++;
  68. }
  69. //
  70. needsUpdate = this._uploadTexture( texture );
  71. }
  72. }
  73. // if the texture is used for RTT, it's necessary to init it once so the binding
  74. // group's resource definition points to the respective GPUTexture
  75. if ( textureProperties.initializedRTT === false ) {
  76. textureProperties.initializedRTT = true;
  77. needsUpdate = true;
  78. }
  79. return needsUpdate;
  80. }
  81. updateSampler( texture ) {
  82. const array = [];
  83. array.push( texture.wrapS );
  84. array.push( texture.wrapT );
  85. array.push( texture.wrapR );
  86. array.push( texture.magFilter );
  87. array.push( texture.minFilter );
  88. array.push( texture.anisotropy );
  89. const key = array.join();
  90. let samplerGPU = this.samplerCache.get( key );
  91. if ( samplerGPU === undefined ) {
  92. samplerGPU = this.device.createSampler( {
  93. addressModeU: this._convertAddressMode( texture.wrapS ),
  94. addressModeV: this._convertAddressMode( texture.wrapT ),
  95. addressModeW: this._convertAddressMode( texture.wrapR ),
  96. magFilter: this._convertFilterMode( texture.magFilter ),
  97. minFilter: this._convertFilterMode( texture.minFilter ),
  98. mipmapFilter: this._convertFilterMode( texture.minFilter ),
  99. maxAnisotropy: texture.anisotropy
  100. } );
  101. this.samplerCache.set( key, samplerGPU );
  102. }
  103. const textureProperties = this.properties.get( texture );
  104. textureProperties.samplerGPU = samplerGPU;
  105. }
  106. initRenderTarget( renderTarget ) {
  107. const properties = this.properties;
  108. const renderTargetProperties = properties.get( renderTarget );
  109. if ( renderTargetProperties.initialized === undefined ) {
  110. const device = this.device;
  111. const width = renderTarget.width;
  112. const height = renderTarget.height;
  113. const colorTextureFormat = this._getFormat( renderTarget.texture );
  114. const colorTextureGPU = device.createTexture( {
  115. size: {
  116. width: width,
  117. height: height,
  118. depthOrArrayLayers: 1
  119. },
  120. format: colorTextureFormat,
  121. usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
  122. } );
  123. this.info.memory.textures ++;
  124. renderTargetProperties.colorTextureGPU = colorTextureGPU;
  125. renderTargetProperties.colorTextureFormat = colorTextureFormat;
  126. // When the ".texture" or ".depthTexture" property of a render target is used as a map,
  127. // the renderer has to find the respective GPUTexture objects to setup the bind groups.
  128. // Since it's not possible to see just from a texture object whether it belongs to a render
  129. // target or not, we need the initializedRTT flag.
  130. const textureProperties = properties.get( renderTarget.texture );
  131. textureProperties.textureGPU = colorTextureGPU;
  132. textureProperties.initializedRTT = false;
  133. if ( renderTarget.depthBuffer === true ) {
  134. const depthTextureFormat = GPUTextureFormat.Depth24PlusStencil8; // @TODO: Make configurable
  135. const depthTextureGPU = device.createTexture( {
  136. size: {
  137. width: width,
  138. height: height,
  139. depthOrArrayLayers: 1
  140. },
  141. format: depthTextureFormat,
  142. usage: GPUTextureUsage.RENDER_ATTACHMENT
  143. } );
  144. this.info.memory.textures ++;
  145. renderTargetProperties.depthTextureGPU = depthTextureGPU;
  146. renderTargetProperties.depthTextureFormat = depthTextureFormat;
  147. if ( renderTarget.depthTexture !== null ) {
  148. const depthTextureProperties = properties.get( renderTarget.depthTexture );
  149. depthTextureProperties.textureGPU = depthTextureGPU;
  150. depthTextureProperties.initializedRTT = false;
  151. }
  152. }
  153. //
  154. const disposeCallback = onRenderTargetDispose.bind( this );
  155. renderTargetProperties.disposeCallback = disposeCallback;
  156. renderTarget.addEventListener( 'dispose', disposeCallback );
  157. //
  158. renderTargetProperties.initialized = true;
  159. }
  160. }
  161. dispose() {
  162. this.samplerCache.clear();
  163. }
  164. _convertAddressMode( value ) {
  165. let addressMode = GPUAddressMode.ClampToEdge;
  166. if ( value === RepeatWrapping ) {
  167. addressMode = GPUAddressMode.Repeat;
  168. } else if ( value === MirroredRepeatWrapping ) {
  169. addressMode = GPUAddressMode.MirrorRepeat;
  170. }
  171. return addressMode;
  172. }
  173. _convertFilterMode( value ) {
  174. let filterMode = GPUFilterMode.Linear;
  175. if ( value === NearestFilter || value === NearestMipmapNearestFilter || value === NearestMipmapLinearFilter ) {
  176. filterMode = GPUFilterMode.Nearest;
  177. }
  178. return filterMode;
  179. }
  180. _uploadTexture( texture ) {
  181. let needsUpdate = false;
  182. const device = this.device;
  183. const image = texture.image;
  184. const textureProperties = this.properties.get( texture );
  185. const { width, height, depth } = this._getSize( texture );
  186. const needsMipmaps = this._needsMipmaps( texture );
  187. const dimension = this._getDimension( texture );
  188. const mipLevelCount = this._getMipLevelCount( texture, width, height, needsMipmaps );
  189. const format = this._getFormat( texture );
  190. let usage = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST;
  191. if ( needsMipmaps === true ) {
  192. // current mipmap generation requires RENDER_ATTACHMENT
  193. usage |= GPUTextureUsage.RENDER_ATTACHMENT;
  194. }
  195. const textureGPUDescriptor = {
  196. size: {
  197. width: width,
  198. height: height,
  199. depthOrArrayLayers: depth,
  200. },
  201. mipLevelCount: mipLevelCount,
  202. sampleCount: 1,
  203. dimension: dimension,
  204. format: format,
  205. usage: usage
  206. };
  207. // texture creation
  208. let textureGPU = textureProperties.textureGPU;
  209. if ( textureGPU === undefined ) {
  210. textureGPU = device.createTexture( textureGPUDescriptor );
  211. textureProperties.textureGPU = textureGPU;
  212. needsUpdate = true;
  213. }
  214. // transfer texture data
  215. if ( texture.isDataTexture || texture.isDataArrayTexture || texture.isData3DTexture ) {
  216. this._copyBufferToTexture( image, format, textureGPU );
  217. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor );
  218. } else if ( texture.isCompressedTexture ) {
  219. this._copyCompressedBufferToTexture( texture.mipmaps, format, textureGPU );
  220. } else if ( texture.isCubeTexture ) {
  221. if ( image.length === 6 ) {
  222. this._copyCubeMapToTexture( image, format, texture, textureGPU, textureGPUDescriptor, needsMipmaps );
  223. }
  224. } else {
  225. if ( image !== null ) {
  226. // assume HTMLImageElement, HTMLCanvasElement or ImageBitmap
  227. this._getImageBitmap( image, texture ).then( imageBitmap => {
  228. this._copyExternalImageToTexture( imageBitmap, textureGPU );
  229. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor );
  230. } );
  231. }
  232. }
  233. textureProperties.version = texture.version;
  234. return needsUpdate;
  235. }
  236. _copyBufferToTexture( image, format, textureGPU, origin = { x: 0, y: 0, z: 0 } ) {
  237. // @TODO: Consider to use GPUCommandEncoder.copyBufferToTexture()
  238. // @TODO: Consider to support valid buffer layouts with other formats like RGB
  239. const data = image.data;
  240. const bytesPerTexel = this._getBytesPerTexel( format );
  241. const bytesPerRow = Math.ceil( image.width * bytesPerTexel / 256 ) * 256;
  242. this.device.queue.writeTexture(
  243. {
  244. texture: textureGPU,
  245. mipLevel: 0,
  246. origin
  247. },
  248. data,
  249. {
  250. offset: 0,
  251. bytesPerRow
  252. },
  253. {
  254. width: image.width,
  255. height: image.height,
  256. depthOrArrayLayers: ( image.depth !== undefined ) ? image.depth : 1
  257. } );
  258. }
  259. _copyCubeMapToTexture( images, format, texture, textureGPU, textureGPUDescriptor, needsMipmaps ) {
  260. for ( let i = 0; i < 6; i ++ ) {
  261. const image = images[ i ];
  262. if ( image.isDataTexture ) {
  263. this._copyBufferToTexture( image.image, format, textureGPU, { z: i } );
  264. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor, i );
  265. } else {
  266. this._getImageBitmap( image, texture ).then( imageBitmap => {
  267. this._copyExternalImageToTexture( imageBitmap, textureGPU, { z: i } );
  268. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor, i );
  269. } );
  270. }
  271. }
  272. }
  273. _copyExternalImageToTexture( image, textureGPU, origin = { x: 0, y: 0, z: 0 } ) {
  274. this.device.queue.copyExternalImageToTexture(
  275. {
  276. source: image
  277. }, {
  278. texture: textureGPU,
  279. mipLevel: 0,
  280. origin
  281. }, {
  282. width: image.width,
  283. height: image.height,
  284. depthOrArrayLayers: 1
  285. }
  286. );
  287. }
  288. _copyCompressedBufferToTexture( mipmaps, format, textureGPU ) {
  289. // @TODO: Consider to use GPUCommandEncoder.copyBufferToTexture()
  290. const blockData = this._getBlockData( format );
  291. for ( let i = 0; i < mipmaps.length; i ++ ) {
  292. const mipmap = mipmaps[ i ];
  293. const width = mipmap.width;
  294. const height = mipmap.height;
  295. const bytesPerRow = Math.ceil( width / blockData.width ) * blockData.byteLength;
  296. this.device.queue.writeTexture(
  297. {
  298. texture: textureGPU,
  299. mipLevel: i
  300. },
  301. mipmap.data,
  302. {
  303. offset: 0,
  304. bytesPerRow
  305. },
  306. {
  307. width: Math.ceil( width / blockData.width ) * blockData.width,
  308. height: Math.ceil( height / blockData.width ) * blockData.width,
  309. depthOrArrayLayers: 1
  310. } );
  311. }
  312. }
  313. _generateMipmaps( textureGPU, textureGPUDescriptor, baseArrayLayer ) {
  314. if ( this.utils === null ) {
  315. this.utils = new WebGPUTextureUtils( this.device ); // only create this helper if necessary
  316. }
  317. this.utils.generateMipmaps( textureGPU, textureGPUDescriptor, baseArrayLayer );
  318. }
  319. _getBlockData( format ) {
  320. // this method is only relevant for compressed texture formats
  321. if ( format === GPUTextureFormat.BC1RGBAUnorm || format === GPUTextureFormat.BC1RGBAUnormSRGB ) return { byteLength: 8, width: 4, height: 4 }; // DXT1
  322. if ( format === GPUTextureFormat.BC2RGBAUnorm || format === GPUTextureFormat.BC2RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // DXT3
  323. if ( format === GPUTextureFormat.BC3RGBAUnorm || format === GPUTextureFormat.BC3RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // DXT5
  324. if ( format === GPUTextureFormat.BC4RUnorm || format === GPUTextureFormat.BC4RSNorm ) return { byteLength: 8, width: 4, height: 4 }; // RGTC1
  325. if ( format === GPUTextureFormat.BC5RGUnorm || format === GPUTextureFormat.BC5RGSnorm ) return { byteLength: 16, width: 4, height: 4 }; // RGTC2
  326. if ( format === GPUTextureFormat.BC6HRGBUFloat || format === GPUTextureFormat.BC6HRGBFloat ) return { byteLength: 16, width: 4, height: 4 }; // BPTC (float)
  327. if ( format === GPUTextureFormat.BC7RGBAUnorm || format === GPUTextureFormat.BC7RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // BPTC (unorm)
  328. }
  329. _getBytesPerTexel( format ) {
  330. if ( format === GPUTextureFormat.R8Unorm ) return 1;
  331. if ( format === GPUTextureFormat.R16Float ) return 2;
  332. if ( format === GPUTextureFormat.RG8Unorm ) return 2;
  333. if ( format === GPUTextureFormat.RG16Float ) return 4;
  334. if ( format === GPUTextureFormat.R32Float ) return 4;
  335. if ( format === GPUTextureFormat.RGBA8Unorm || format === GPUTextureFormat.RGBA8UnormSRGB ) return 4;
  336. if ( format === GPUTextureFormat.RG32Float ) return 8;
  337. if ( format === GPUTextureFormat.RGBA16Float ) return 8;
  338. if ( format === GPUTextureFormat.RGBA32Float ) return 16;
  339. }
  340. _getDimension( texture ) {
  341. let dimension;
  342. if ( texture.isData3DTexture ) {
  343. dimension = GPUTextureDimension.ThreeD;
  344. } else {
  345. dimension = GPUTextureDimension.TwoD;
  346. }
  347. return dimension;
  348. }
  349. _getFormat( texture ) {
  350. const format = texture.format;
  351. const type = texture.type;
  352. const encoding = texture.encoding;
  353. let formatGPU;
  354. switch ( format ) {
  355. case RGBA_S3TC_DXT1_Format:
  356. formatGPU = ( encoding === sRGBEncoding ) ? GPUTextureFormat.BC1RGBAUnormSRGB : GPUTextureFormat.BC1RGBAUnorm;
  357. break;
  358. case RGBA_S3TC_DXT3_Format:
  359. formatGPU = ( encoding === sRGBEncoding ) ? GPUTextureFormat.BC2RGBAUnormSRGB : GPUTextureFormat.BC2RGBAUnorm;
  360. break;
  361. case RGBA_S3TC_DXT5_Format:
  362. formatGPU = ( encoding === sRGBEncoding ) ? GPUTextureFormat.BC3RGBAUnormSRGB : GPUTextureFormat.BC3RGBAUnorm;
  363. break;
  364. case RGBAFormat:
  365. switch ( type ) {
  366. case UnsignedByteType:
  367. formatGPU = ( encoding === sRGBEncoding ) ? GPUTextureFormat.RGBA8UnormSRGB : GPUTextureFormat.RGBA8Unorm;
  368. break;
  369. case HalfFloatType:
  370. formatGPU = GPUTextureFormat.RGBA16Float;
  371. break;
  372. case FloatType:
  373. formatGPU = GPUTextureFormat.RGBA32Float;
  374. break;
  375. default:
  376. console.error( 'WebGPURenderer: Unsupported texture type with RGBAFormat.', type );
  377. }
  378. break;
  379. case RedFormat:
  380. switch ( type ) {
  381. case UnsignedByteType:
  382. formatGPU = GPUTextureFormat.R8Unorm;
  383. break;
  384. case HalfFloatType:
  385. formatGPU = GPUTextureFormat.R16Float;
  386. break;
  387. case FloatType:
  388. formatGPU = GPUTextureFormat.R32Float;
  389. break;
  390. default:
  391. console.error( 'WebGPURenderer: Unsupported texture type with RedFormat.', type );
  392. }
  393. break;
  394. case RGFormat:
  395. switch ( type ) {
  396. case UnsignedByteType:
  397. formatGPU = GPUTextureFormat.RG8Unorm;
  398. break;
  399. case HalfFloatType:
  400. formatGPU = GPUTextureFormat.RG16Float;
  401. break;
  402. case FloatType:
  403. formatGPU = GPUTextureFormat.RG32Float;
  404. break;
  405. default:
  406. console.error( 'WebGPURenderer: Unsupported texture type with RGFormat.', type );
  407. }
  408. break;
  409. default:
  410. console.error( 'WebGPURenderer: Unsupported texture format.', format );
  411. }
  412. return formatGPU;
  413. }
  414. _getImageBitmap( image, texture ) {
  415. const width = image.width;
  416. const height = image.height;
  417. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  418. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ) {
  419. const options = {};
  420. options.imageOrientation = ( texture.flipY === true ) ? 'flipY' : 'none';
  421. options.premultiplyAlpha = ( texture.premultiplyAlpha === true ) ? 'premultiply' : 'default';
  422. return createImageBitmap( image, 0, 0, width, height, options );
  423. } else {
  424. // assume ImageBitmap
  425. return Promise.resolve( image );
  426. }
  427. }
  428. _getMipLevelCount( texture, width, height, needsMipmaps ) {
  429. let mipLevelCount;
  430. if ( texture.isCompressedTexture ) {
  431. mipLevelCount = texture.mipmaps.length;
  432. } else if ( needsMipmaps === true ) {
  433. mipLevelCount = Math.floor( Math.log2( Math.max( width, height ) ) ) + 1;
  434. } else {
  435. mipLevelCount = 1; // a texture without mipmaps has a base mip (mipLevel 0)
  436. }
  437. return mipLevelCount;
  438. }
  439. _getSize( texture ) {
  440. const image = texture.image;
  441. let width, height, depth;
  442. if ( texture.isCubeTexture ) {
  443. const faceImage = image.length > 0 ? image[ 0 ].image || image[ 0 ] : null;
  444. width = faceImage?.width || 1;
  445. height = faceImage?.height || 1;
  446. depth = 6; // one image for each side of the cube map
  447. } else if ( image !== null ) {
  448. width = image.width;
  449. height = image.height;
  450. depth = ( image.depth !== undefined ) ? image.depth : 1;
  451. } else {
  452. width = height = depth = 1;
  453. }
  454. return { width, height, depth };
  455. }
  456. _needsMipmaps( texture ) {
  457. return ( texture.isCompressedTexture !== true ) && ( texture.generateMipmaps === true ) && ( texture.minFilter !== NearestFilter ) && ( texture.minFilter !== LinearFilter );
  458. }
  459. }
  460. function onRenderTargetDispose( event ) {
  461. const renderTarget = event.target;
  462. const properties = this.properties;
  463. const renderTargetProperties = properties.get( renderTarget );
  464. renderTarget.removeEventListener( 'dispose', renderTargetProperties.disposeCallback );
  465. renderTargetProperties.colorTextureGPU.destroy();
  466. properties.remove( renderTarget.texture );
  467. this.info.memory.textures --;
  468. if ( renderTarget.depthBuffer === true ) {
  469. renderTargetProperties.depthTextureGPU.destroy();
  470. this.info.memory.textures --;
  471. if ( renderTarget.depthTexture !== null ) {
  472. properties.remove( renderTarget.depthTexture );
  473. }
  474. }
  475. properties.remove( renderTarget );
  476. }
  477. function onTextureDispose( event ) {
  478. const texture = event.target;
  479. const textureProperties = this.properties.get( texture );
  480. textureProperties.textureGPU.destroy();
  481. texture.removeEventListener( 'dispose', textureProperties.disposeCallback );
  482. this.properties.remove( texture );
  483. this.info.memory.textures --;
  484. }
  485. export default WebGPUTextures;