WebGPUTextures.js 9.7 KB

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