WebGPUTextures.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. import { GPUTextureFormat, GPUAddressMode, GPUFilterMode } from './constants.js';
  2. import { Texture, NearestFilter, NearestMipmapNearestFilter, NearestMipmapLinearFilter, LinearFilter, RepeatWrapping, MirroredRepeatWrapping,
  3. RGBFormat, RGBAFormat, RGBA_S3TC_DXT1_Format, RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT5_Format, UnsignedByteType, FloatType, HalfFloatType
  4. } from '../../../../build/three.module.js';
  5. import WebGPUTextureUtils from './WebGPUTextureUtils.js';
  6. class WebGPUTextures {
  7. constructor( device, properties, info, glslang ) {
  8. this.device = device;
  9. this.properties = properties;
  10. this.info = info;
  11. this.glslang = glslang;
  12. this.defaultTexture = 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.defaultTexture = this._createTexture( texture );
  29. }
  30. return this.defaultTexture;
  31. }
  32. getTextureGPU( texture ) {
  33. const textureProperties = this.properties.get( texture );
  34. return textureProperties.textureGPU;
  35. }
  36. getSampler( texture ) {
  37. const textureProperties = this.properties.get( texture );
  38. return textureProperties.samplerGPU;
  39. }
  40. updateTexture( texture ) {
  41. let forceUpdate = false;
  42. const textureProperties = this.properties.get( texture );
  43. if ( texture.version > 0 && textureProperties.version !== texture.version ) {
  44. const image = texture.image;
  45. if ( image === undefined ) {
  46. console.warn( 'THREE.WebGPURenderer: Texture marked for update but image is undefined.' );
  47. } else if ( image.complete === false ) {
  48. console.warn( 'THREE.WebGPURenderer: Texture marked for update but image is incomplete.' );
  49. } else {
  50. // texture init
  51. if ( textureProperties.initialized === undefined ) {
  52. textureProperties.initialized = true;
  53. const disposeCallback = onTextureDispose.bind( this );
  54. textureProperties.disposeCallback = disposeCallback;
  55. texture.addEventListener( 'dispose', disposeCallback );
  56. this.info.memory.textures ++;
  57. }
  58. // texture creation
  59. if ( textureProperties.textureGPU !== undefined ) {
  60. // @TODO: Avoid calling of destroy() in certain scenarios. When only the contents of a texture
  61. // are updated, a buffer upload should be sufficient. However, if the user changes
  62. // the dimensions of the texture, format or usage, a new instance of GPUTexture is required.
  63. textureProperties.textureGPU.destroy();
  64. }
  65. textureProperties.textureGPU = this._createTexture( texture );
  66. textureProperties.version = texture.version;
  67. forceUpdate = true;
  68. }
  69. }
  70. // if the texture is used for RTT, it's necessary to init it once so the binding
  71. // group's resource definition points to the respective GPUTexture
  72. if ( textureProperties.initializedRTT === false ) {
  73. textureProperties.initializedRTT = true;
  74. forceUpdate = true;
  75. }
  76. return forceUpdate;
  77. }
  78. updateSampler( texture ) {
  79. const array = [];
  80. array.push( texture.wrapS );
  81. array.push( texture.wrapT );
  82. array.push( texture.wrapR );
  83. array.push( texture.magFilter );
  84. array.push( texture.minFilter );
  85. array.push( texture.anisotropy );
  86. const key = array.join();
  87. let samplerGPU = this.samplerCache.get( key );
  88. if ( samplerGPU === undefined ) {
  89. samplerGPU = this.device.createSampler( {
  90. addressModeU: this._convertAddressMode( texture.wrapS ),
  91. addressModeV: this._convertAddressMode( texture.wrapT ),
  92. addressModeW: this._convertAddressMode( texture.wrapR ),
  93. magFilter: this._convertFilterMode( texture.magFilter ),
  94. minFilter: this._convertFilterMode( texture.minFilter ),
  95. mipmapFilter: this._convertFilterMode( texture.minFilter ),
  96. maxAnisotropy: texture.anisotropy
  97. } );
  98. this.samplerCache.set( key, samplerGPU );
  99. }
  100. const textureProperties = this.properties.get( texture );
  101. textureProperties.samplerGPU = samplerGPU;
  102. }
  103. initRenderTarget( renderTarget ) {
  104. const properties = this.properties;
  105. const renderTargetProperties = properties.get( renderTarget );
  106. if ( renderTargetProperties.initialized === undefined ) {
  107. const device = this.device;
  108. const width = renderTarget.width;
  109. const height = renderTarget.height;
  110. const colorTextureGPU = device.createTexture( {
  111. size: {
  112. width: width,
  113. height: height,
  114. depth: 1
  115. },
  116. format: GPUTextureFormat.BRGA8Unorm, // @TODO: Make configurable
  117. usage: GPUTextureUsage.OUTPUT_ATTACHMENT | GPUTextureUsage.SAMPLED
  118. } );
  119. renderTargetProperties.colorTextureGPU = colorTextureGPU;
  120. // When the ".texture" or ".depthTexture" property of a render target is used as a map,
  121. // the renderer has to find the respective GPUTexture objects to setup the bind groups.
  122. // Since it's not possible to see just from a texture object whether it belongs to a render
  123. // target or not, we need the initializedRTT flag.
  124. const textureProperties = properties.get( renderTarget.texture );
  125. textureProperties.textureGPU = colorTextureGPU;
  126. textureProperties.initializedRTT = false;
  127. if ( renderTarget.depthBuffer === true ) {
  128. const depthTextureGPU = device.createTexture( {
  129. size: {
  130. width: width,
  131. height: height,
  132. depth: 1
  133. },
  134. format: GPUTextureFormat.Depth24PlusStencil8, // @TODO: Make configurable
  135. usage: GPUTextureUsage.OUTPUT_ATTACHMENT
  136. } );
  137. renderTargetProperties.depthTextureGPU = depthTextureGPU;
  138. if ( renderTarget.depthTexture !== null ) {
  139. const depthTextureProperties = properties.get( renderTarget.depthTexture );
  140. depthTextureProperties.textureGPU = depthTextureGPU;
  141. depthTextureProperties.initializedRTT = false;
  142. }
  143. }
  144. //
  145. const disposeCallback = onRenderTargetDispose.bind( this );
  146. renderTargetProperties.disposeCallback = disposeCallback;
  147. renderTarget.addEventListener( 'dispose', disposeCallback );
  148. //
  149. renderTargetProperties.initialized = true;
  150. }
  151. }
  152. dispose() {
  153. this.samplerCache.clear();
  154. }
  155. _convertAddressMode( value ) {
  156. let addressMode = GPUAddressMode.ClampToEdge;
  157. if ( value === RepeatWrapping ) {
  158. addressMode = GPUAddressMode.Repeat;
  159. } else if ( value === MirroredRepeatWrapping ) {
  160. addressMode = GPUAddressMode.MirrorRepeat;
  161. }
  162. return addressMode;
  163. }
  164. _convertFilterMode( value ) {
  165. let filterMode = GPUFilterMode.Linear;
  166. if ( value === NearestFilter || value === NearestMipmapNearestFilter || value === NearestMipmapLinearFilter ) {
  167. filterMode = GPUFilterMode.Nearest;
  168. }
  169. return filterMode;
  170. }
  171. _createTexture( texture ) {
  172. const device = this.device;
  173. const image = texture.image;
  174. const width = ( image !== undefined ) ? image.width : 1;
  175. const height = ( image !== undefined ) ? image.height : 1;
  176. const format = this._getFormat( texture );
  177. const needsMipmaps = this._needsMipmaps( texture );
  178. let mipLevelCount;
  179. let usage = GPUTextureUsage.SAMPLED | GPUTextureUsage.COPY_DST;
  180. if ( texture.isCompressedTexture ) {
  181. mipLevelCount = texture.mipmaps.length;
  182. } else {
  183. if ( needsMipmaps === true ) {
  184. mipLevelCount = this._getMipLevelCount( width, height );
  185. // current mipmap generation requires OUTPUT_ATTACHMENT
  186. usage |= GPUTextureUsage.OUTPUT_ATTACHMENT;
  187. }
  188. }
  189. // texture creation
  190. const textureGPUDescriptor = {
  191. size: {
  192. width: width,
  193. height: height,
  194. depth: 1,
  195. },
  196. format: format,
  197. usage: usage,
  198. mipLevelCount: mipLevelCount
  199. };
  200. const textureGPU = device.createTexture( textureGPUDescriptor );
  201. // transfer texture data
  202. if ( texture.isDataTexture ) {
  203. this._copyBufferToTexture( image, format, textureGPU );
  204. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor );
  205. } else if ( texture.isCompressedTexture ) {
  206. this._copyCompressedBufferToTexture( texture.mipmaps, format, textureGPU );
  207. } else {
  208. // convert HTML images and canvas elements to ImageBitmap
  209. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  210. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ) {
  211. const options = {};
  212. options.imageOrientation = ( texture.flipY === true ) ? 'flipY' : 'none';
  213. options.premultiplyAlpha = ( texture.premultiplyAlpha === true ) ? 'premultiply' : 'default';
  214. createImageBitmap( image, 0, 0, width, height, options ).then( imageBitmap => {
  215. this._copyImageBitmapToTexture( imageBitmap, textureGPU );
  216. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor );
  217. } );
  218. } else {
  219. if ( image !== undefined ) {
  220. // assume ImageBitmap
  221. this._copyImageBitmapToTexture( image, textureGPU );
  222. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor );
  223. }
  224. }
  225. }
  226. return textureGPU;
  227. }
  228. _copyBufferToTexture( image, format, textureGPU ) {
  229. // @TODO: Consider to use GPUCommandEncoder.copyBufferToTexture()
  230. // @TODO: Consider to support valid buffer layouts with other formats like RGB
  231. const data = image.data;
  232. const bytesPerTexel = this._getBytesPerTexel( format );
  233. const bytesPerRow = Math.ceil( image.width * bytesPerTexel / 256 ) * 256;
  234. this.device.defaultQueue.writeTexture(
  235. {
  236. texture: textureGPU,
  237. mipLevel: 0 // @TODO: Support mipmaps
  238. },
  239. data,
  240. {
  241. offset: 0,
  242. bytesPerRow
  243. },
  244. {
  245. width: image.width,
  246. height: image.height,
  247. depth: 1
  248. } );
  249. }
  250. _copyImageBitmapToTexture( image, textureGPU ) {
  251. this.device.defaultQueue.copyImageBitmapToTexture(
  252. {
  253. imageBitmap: image
  254. }, {
  255. texture: textureGPU
  256. }, {
  257. width: image.width,
  258. height: image.height,
  259. depth: 1
  260. }
  261. );
  262. }
  263. _copyCompressedBufferToTexture( mipmaps, format, textureGPU ) {
  264. // @TODO: Consider to use GPUCommandEncoder.copyBufferToTexture()
  265. const blockData = this._getBlockData( format );
  266. for ( let i = 0; i < mipmaps.length; i ++ ) {
  267. const mipmap = mipmaps[ i ];
  268. const width = mipmap.width;
  269. const height = mipmap.height;
  270. const bytesPerRow = Math.ceil( width / blockData.width ) * blockData.byteLength;
  271. this.device.defaultQueue.writeTexture(
  272. {
  273. texture: textureGPU,
  274. mipLevel: i
  275. },
  276. mipmap.data,
  277. {
  278. offset: 0,
  279. bytesPerRow
  280. },
  281. {
  282. width: Math.ceil( width / blockData.width ) * blockData.width,
  283. height: Math.ceil( height / blockData.width ) * blockData.width,
  284. depth: 1,
  285. } );
  286. }
  287. }
  288. _generateMipmaps( textureGPU, textureGPUDescriptor ) {
  289. if ( this.utils === null ) {
  290. this.utils = new WebGPUTextureUtils( this.device, this.glslang ); // only create this helper if necessary
  291. }
  292. this.utils.generateMipmaps( textureGPU, textureGPUDescriptor );
  293. }
  294. _getBlockData( format ) {
  295. if ( format === GPUTextureFormat.BC1RGBAUnorm ) return { byteLength: 8, width: 4, height: 4 };
  296. if ( format === GPUTextureFormat.BC2RGBAUnorm ) return { byteLength: 16, width: 4, height: 4 };
  297. if ( format === GPUTextureFormat.BC3RGBAUnorm ) return { byteLength: 16, width: 4, height: 4 };
  298. }
  299. _getBytesPerTexel( format ) {
  300. if ( format === GPUTextureFormat.RGBA8Unorm ) return 4;
  301. if ( format === GPUTextureFormat.RGBA16Float ) return 8;
  302. if ( format === GPUTextureFormat.RGBA32Float ) return 16;
  303. }
  304. _getFormat( texture ) {
  305. const format = texture.format;
  306. const type = texture.type;
  307. let formatGPU;
  308. switch ( format ) {
  309. case RGBA_S3TC_DXT1_Format:
  310. formatGPU = GPUTextureFormat.BC1RGBAUnorm;
  311. break;
  312. case RGBA_S3TC_DXT3_Format:
  313. formatGPU = GPUTextureFormat.BC2RGBAUnorm;
  314. break;
  315. case RGBA_S3TC_DXT5_Format:
  316. formatGPU = GPUTextureFormat.BC3RGBAUnorm;
  317. break;
  318. case RGBFormat:
  319. case RGBAFormat:
  320. switch ( type ) {
  321. case UnsignedByteType:
  322. formatGPU = GPUTextureFormat.RGBA8Unorm;
  323. break;
  324. case FloatType:
  325. formatGPU = GPUTextureFormat.RGBA32Float;
  326. break;
  327. case HalfFloatType:
  328. formatGPU = GPUTextureFormat.RGBA16Float;
  329. break;
  330. default:
  331. console.error( 'WebGPURenderer: Unsupported texture type with RGBAFormat.', type );
  332. }
  333. break;
  334. default:
  335. console.error( 'WebGPURenderer: Unsupported texture format.', format );
  336. }
  337. return formatGPU;
  338. }
  339. _getMipLevelCount( width, height ) {
  340. return Math.floor( Math.log2( Math.max( width, height ) ) ) + 1;
  341. }
  342. _needsMipmaps( texture ) {
  343. return ( texture.isCompressedTexture !== true ) && ( texture.generateMipmaps === true ) && ( texture.minFilter !== NearestFilter ) && ( texture.minFilter !== LinearFilter );
  344. }
  345. }
  346. function onRenderTargetDispose( event ) {
  347. const renderTarget = event.target;
  348. const properties = this.properties;
  349. const renderTargetProperties = properties.get( renderTarget );
  350. renderTarget.removeEventListener( 'dispose', renderTargetProperties.disposeCallback );
  351. renderTargetProperties.colorTextureGPU.destroy();
  352. properties.remove( renderTarget.texture );
  353. if ( renderTarget.depthBuffer === true ) {
  354. renderTargetProperties.depthTextureGPU.destroy();
  355. if ( renderTarget.depthTexture !== null ) {
  356. properties.remove( renderTarget.depthTexture );
  357. }
  358. }
  359. properties.remove( renderTarget );
  360. }
  361. function onTextureDispose( event ) {
  362. const texture = event.target;
  363. const textureProperties = this.properties.get( texture );
  364. textureProperties.textureGPU.destroy();
  365. texture.removeEventListener( 'dispose', textureProperties.disposeCallback );
  366. this.properties.remove( texture );
  367. this.info.memory.textures --;
  368. }
  369. export default WebGPUTextures;