WebGPUTextures.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. import { GPUTextureFormat, GPUAddressMode, GPUFilterMode } from './constants.js';
  2. import { Texture, NearestFilter, NearestMipmapNearestFilter, NearestMipmapLinearFilter, LinearFilter, RepeatWrapping, MirroredRepeatWrapping, FloatType, HalfFloatType } from '../../../../build/three.module.js';
  3. import WebGPUTextureUtils from './WebGPUTextureUtils.js';
  4. class WebGPUTextures {
  5. constructor( device, properties, info, glslang ) {
  6. this.device = device;
  7. this.properties = properties;
  8. this.info = info;
  9. this.glslang = glslang;
  10. this.defaultTexture = null;
  11. this.defaultSampler = null;
  12. this.samplerCache = new Map();
  13. this.utils = null;
  14. }
  15. getDefaultSampler() {
  16. if ( this.defaultSampler === null ) {
  17. this.defaultSampler = this.device.createSampler( {} );
  18. }
  19. return this.defaultSampler;
  20. }
  21. getDefaultTexture() {
  22. if ( this.defaultTexture === null ) {
  23. this.defaultTexture = this._createTexture( new Texture() );
  24. }
  25. return this.defaultTexture;
  26. }
  27. getTextureGPU( texture ) {
  28. const textureProperties = this.properties.get( texture );
  29. return textureProperties.textureGPU;
  30. }
  31. getSampler( texture ) {
  32. const textureProperties = this.properties.get( texture );
  33. return textureProperties.samplerGPU;
  34. }
  35. updateTexture( texture ) {
  36. let forceUpdate = false;
  37. const textureProperties = this.properties.get( texture );
  38. if ( texture.version > 0 && textureProperties.version !== texture.version ) {
  39. const image = texture.image;
  40. if ( image === undefined ) {
  41. console.warn( 'THREE.WebGPURenderer: Texture marked for update but image is undefined.' );
  42. } else if ( image.complete === false ) {
  43. console.warn( 'THREE.WebGPURenderer: Texture marked for update but image is incomplete.' );
  44. } else {
  45. // texture init
  46. if ( textureProperties.initialized === undefined ) {
  47. textureProperties.initialized = true;
  48. const disposeCallback = onTextureDispose.bind( this );
  49. textureProperties.disposeCallback = disposeCallback;
  50. texture.addEventListener( 'dispose', disposeCallback );
  51. this.info.memory.textures ++;
  52. }
  53. // texture creation
  54. if ( textureProperties.textureGPU !== undefined ) {
  55. // @TODO: Avoid calling of destroy() in certain scenarios. When only the contents of a texture
  56. // are updated, a buffer upload should be sufficient. However, if the user changes
  57. // the dimensions of the texture, format or usage, a new instance of GPUTexture is required.
  58. textureProperties.textureGPU.destroy();
  59. }
  60. textureProperties.textureGPU = this._createTexture( texture );
  61. textureProperties.version = texture.version;
  62. forceUpdate = true;
  63. }
  64. }
  65. // if the texture is used for RTT, it's necessary to init it once so the binding
  66. // group's resource definition points to the respective GPUTexture
  67. if ( textureProperties.initializedRTT === false ) {
  68. textureProperties.initializedRTT = true;
  69. forceUpdate = true;
  70. }
  71. return forceUpdate;
  72. }
  73. updateSampler( texture ) {
  74. const array = [];
  75. array.push( texture.wrapS );
  76. array.push( texture.wrapT );
  77. array.push( texture.wrapR );
  78. array.push( texture.magFilter );
  79. array.push( texture.minFilter );
  80. array.push( texture.anisotropy );
  81. const key = array.join();
  82. let samplerGPU = this.samplerCache.get( key );
  83. if ( samplerGPU === undefined ) {
  84. samplerGPU = this.device.createSampler( {
  85. addressModeU: this._convertAddressMode( texture.wrapS ),
  86. addressModeV: this._convertAddressMode( texture.wrapT ),
  87. addressModeW: this._convertAddressMode( texture.wrapR ),
  88. magFilter: this._convertFilterMode( texture.magFilter ),
  89. minFilter: this._convertFilterMode( texture.minFilter ),
  90. mipmapFilter: this._convertFilterMode( texture.minFilter ),
  91. maxAnisotropy: texture.anisotropy
  92. } );
  93. this.samplerCache.set( key, samplerGPU );
  94. }
  95. const textureProperties = this.properties.get( texture );
  96. textureProperties.samplerGPU = samplerGPU;
  97. }
  98. initRenderTarget( renderTarget ) {
  99. const properties = this.properties;
  100. const renderTargetProperties = properties.get( renderTarget );
  101. if ( renderTargetProperties.initialized === undefined ) {
  102. const device = this.device;
  103. const width = renderTarget.width;
  104. const height = renderTarget.height;
  105. const colorTextureGPU = device.createTexture( {
  106. size: {
  107. width: width,
  108. height: height,
  109. depth: 1
  110. },
  111. format: GPUTextureFormat.BRGA8Unorm, // @TODO: Make configurable
  112. usage: GPUTextureUsage.OUTPUT_ATTACHMENT | GPUTextureUsage.SAMPLED
  113. } );
  114. renderTargetProperties.colorTextureGPU = colorTextureGPU;
  115. // When the ".texture" or ".depthTexture" property of a render target is used as a map,
  116. // the renderer has to find the respective GPUTexture objects to setup the bind groups.
  117. // Since it's not possible to see just from a texture object whether it belongs to a render
  118. // target or not, we need the initializedRTT flag.
  119. const textureProperties = properties.get( renderTarget.texture );
  120. textureProperties.textureGPU = colorTextureGPU;
  121. textureProperties.initializedRTT = false;
  122. if ( renderTarget.depthBuffer === true ) {
  123. const depthTextureGPU = device.createTexture( {
  124. size: {
  125. width: width,
  126. height: height,
  127. depth: 1
  128. },
  129. format: GPUTextureFormat.Depth24PlusStencil8, // @TODO: Make configurable
  130. usage: GPUTextureUsage.OUTPUT_ATTACHMENT
  131. } );
  132. renderTargetProperties.depthTextureGPU = depthTextureGPU;
  133. if ( renderTarget.depthTexture !== null ) {
  134. const depthTextureProperties = properties.get( renderTarget.depthTexture );
  135. depthTextureProperties.textureGPU = depthTextureGPU;
  136. depthTextureProperties.initializedRTT = false;
  137. }
  138. }
  139. //
  140. const disposeCallback = onRenderTargetDispose.bind( this );
  141. renderTargetProperties.disposeCallback = disposeCallback;
  142. renderTarget.addEventListener( 'dispose', disposeCallback );
  143. //
  144. renderTargetProperties.initialized = true;
  145. }
  146. }
  147. dispose() {
  148. this.samplerCache.clear();
  149. }
  150. _computeMipLevelCount( width, height ) {
  151. return Math.floor( Math.log2( Math.max( width, height ) ) ) + 1;
  152. }
  153. _convertAddressMode( value ) {
  154. let addressMode = GPUAddressMode.ClampToEdge;
  155. if ( value === RepeatWrapping ) {
  156. addressMode = GPUAddressMode.Repeat;
  157. } else if ( value === MirroredRepeatWrapping ) {
  158. addressMode = GPUAddressMode.MirrorRepeat;
  159. }
  160. return addressMode;
  161. }
  162. _convertFilterMode( value ) {
  163. let filterMode = GPUFilterMode.Linear;
  164. if ( value === NearestFilter || value === NearestMipmapNearestFilter || value === NearestMipmapLinearFilter ) {
  165. filterMode = GPUFilterMode.Nearest;
  166. }
  167. return filterMode;
  168. }
  169. _convertFormat( type ) {
  170. let formatGPU = GPUTextureFormat.RGBA8Unorm;
  171. if ( type === FloatType ) {
  172. formatGPU = GPUTextureFormat.RGBA32Float;
  173. } else if ( type === HalfFloatType ) {
  174. formatGPU = GPUTextureFormat.RGBA16Float;
  175. }
  176. return formatGPU;
  177. }
  178. _createTexture( texture ) {
  179. const device = this.device;
  180. const image = texture.image;
  181. const width = ( image !== undefined ) ? image.width : 1;
  182. const height = ( image !== undefined ) ? image.height : 1;
  183. const format = this._convertFormat( texture.type );
  184. const needsMipmaps = this._needsMipmaps( texture );
  185. const mipLevelCount = ( needsMipmaps === true ) ? this._computeMipLevelCount( width, height ) : undefined;
  186. let usage = GPUTextureUsage.SAMPLED | GPUTextureUsage.COPY_DST;
  187. if ( needsMipmaps === true ) {
  188. usage |= GPUTextureUsage.OUTPUT_ATTACHMENT;
  189. }
  190. // texture creation
  191. const textureGPUDescriptor = {
  192. size: {
  193. width: width,
  194. height: height,
  195. depth: 1,
  196. },
  197. format: format,
  198. usage: usage,
  199. mipLevelCount: mipLevelCount
  200. };
  201. const textureGPU = device.createTexture( textureGPUDescriptor );
  202. // transfer texture data
  203. if ( texture.isDataTexture ) {
  204. this._copyBufferToTexture( image, format, textureGPU );
  205. } else {
  206. // convert HTML iamges and canvas elements to ImageBitmap before copy
  207. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  208. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ) {
  209. const options = {};
  210. options.imageOrientation = ( texture.flipY === true ) ? 'flipY' : 'none';
  211. options.premultiplyAlpha = ( texture.premultiplyAlpha === true ) ? 'premultiply' : 'default';
  212. createImageBitmap( image, 0, 0, width, height, options ).then( imageBitmap => {
  213. this._copyImageBitmapToTexture( imageBitmap, textureGPU, needsMipmaps, textureGPUDescriptor );
  214. } );
  215. } else {
  216. if ( image !== undefined ) {
  217. // assuming ImageBitmap. Directly start copy operation of the contents of ImageBitmap into the destination texture
  218. this._copyImageBitmapToTexture( image, textureGPU, needsMipmaps, textureGPUDescriptor );
  219. }
  220. }
  221. }
  222. return textureGPU;
  223. }
  224. _copyBufferToTexture( image, format, textureGPU ) {
  225. // this code assumes data textures in RGBA format
  226. // @TODO: Consider to support valid buffer layouts with other formats like RGB
  227. // @TODO: Support mipmaps
  228. const device = this.device;
  229. const data = image.data;
  230. const bytesPerTexel = this._getBytesPerTexel( format );
  231. const bytesPerRow = Math.ceil( image.width * bytesPerTexel / 256 ) * 256;
  232. const textureDataBuffer = device.createBuffer( {
  233. size: data.byteLength,
  234. usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC,
  235. mappedAtCreation: true,
  236. } );
  237. new data.constructor( textureDataBuffer.getMappedRange() ).set( data );
  238. textureDataBuffer.unmap();
  239. const commandEncoder = device.createCommandEncoder( {} );
  240. commandEncoder.copyBufferToTexture(
  241. {
  242. buffer: textureDataBuffer,
  243. bytesPerRow: bytesPerRow
  244. }, {
  245. texture: textureGPU
  246. }, {
  247. width: image.width,
  248. height: image.height,
  249. depth: 1
  250. } );
  251. device.defaultQueue.submit( [ commandEncoder.finish() ] );
  252. textureDataBuffer.destroy();
  253. }
  254. _copyImageBitmapToTexture( imageBitmap, textureGPU, needsMipmaps, textureGPUDescriptor ) {
  255. const device = this.device;
  256. device.defaultQueue.copyImageBitmapToTexture(
  257. {
  258. imageBitmap: imageBitmap
  259. }, {
  260. texture: textureGPU
  261. }, {
  262. width: imageBitmap.width,
  263. height: imageBitmap.height,
  264. depth: 1
  265. }
  266. );
  267. if ( needsMipmaps === true ) {
  268. if ( this.utils === null ) {
  269. this.utils = new WebGPUTextureUtils( this.device, this.glslang ); // only create this helper if necessary
  270. }
  271. this.utils.generateMipmappedTexture( imageBitmap, textureGPU, textureGPUDescriptor );
  272. }
  273. }
  274. _getBytesPerTexel( format ) {
  275. if ( format === GPUTextureFormat.RGBA8Unorm ) return 4;
  276. if ( format === GPUTextureFormat.RGBA16Float ) return 8;
  277. if ( format === GPUTextureFormat.RGBA32Float ) return 16;
  278. }
  279. _needsMipmaps( texture ) {
  280. return ( texture.generateMipmaps === true ) && ( texture.minFilter !== NearestFilter ) && ( texture.minFilter !== LinearFilter );
  281. }
  282. }
  283. function onRenderTargetDispose( event ) {
  284. const renderTarget = event.target;
  285. const properties = this.properties;
  286. const renderTargetProperties = properties.get( renderTarget );
  287. renderTarget.removeEventListener( 'dispose', renderTargetProperties.disposeCallback );
  288. renderTargetProperties.colorTextureGPU.destroy();
  289. properties.remove( renderTarget.texture );
  290. if ( renderTarget.depthBuffer === true ) {
  291. renderTargetProperties.depthTextureGPU.destroy();
  292. if ( renderTarget.depthTexture !== null ) {
  293. properties.remove( renderTarget.depthTexture );
  294. }
  295. }
  296. properties.remove( renderTarget );
  297. }
  298. function onTextureDispose( event ) {
  299. const texture = event.target;
  300. const textureProperties = this.properties.get( texture );
  301. textureProperties.textureGPU.destroy();
  302. texture.removeEventListener( 'dispose', textureProperties.disposeCallback );
  303. this.properties.remove( texture );
  304. this.info.memory.textures --;
  305. }
  306. export default WebGPUTextures;