WebGPUTextures.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. renderTargetProperties.initialized = true;
  140. }
  141. }
  142. dispose() {
  143. this.samplerCache.clear();
  144. }
  145. _computeMipLevelCount( width, height ) {
  146. return Math.floor( Math.log2( Math.max( width, height ) ) ) + 1;
  147. }
  148. _convertAddressMode( value ) {
  149. let addressMode = GPUAddressMode.ClampToEdge;
  150. if ( value === RepeatWrapping ) {
  151. addressMode = GPUAddressMode.Repeat;
  152. } else if ( value === MirroredRepeatWrapping ) {
  153. addressMode = GPUAddressMode.MirrorRepeat;
  154. }
  155. return addressMode;
  156. }
  157. _convertFilterMode( value ) {
  158. let filterMode = GPUFilterMode.Linear;
  159. if ( value === NearestFilter || value === NearestMipmapNearestFilter || value === NearestMipmapLinearFilter ) {
  160. filterMode = GPUFilterMode.Nearest;
  161. }
  162. return filterMode;
  163. }
  164. _convertFormat( type ) {
  165. let formatGPU = GPUTextureFormat.RGBA8Unorm;
  166. if ( type === FloatType ) {
  167. formatGPU = GPUTextureFormat.RGBA32Float;
  168. } else if ( type === HalfFloatType ) {
  169. formatGPU = GPUTextureFormat.RGBA16Float;
  170. }
  171. return formatGPU;
  172. }
  173. _createTexture( texture ) {
  174. const device = this.device;
  175. const image = texture.image;
  176. const width = ( image !== undefined ) ? image.width : 1;
  177. const height = ( image !== undefined ) ? image.height : 1;
  178. const format = this._convertFormat( texture.type );
  179. const needsMipmaps = this._needsMipmaps( texture );
  180. const mipLevelCount = ( needsMipmaps === true ) ? this._computeMipLevelCount( width, height ) : undefined;
  181. let usage = GPUTextureUsage.SAMPLED | GPUTextureUsage.COPY_DST;
  182. if ( needsMipmaps === true ) {
  183. usage |= GPUTextureUsage.OUTPUT_ATTACHMENT;
  184. }
  185. // texture creation
  186. const textureGPUDescriptor = {
  187. size: {
  188. width: width,
  189. height: height,
  190. depth: 1,
  191. },
  192. format: format,
  193. usage: usage,
  194. mipLevelCount: mipLevelCount
  195. };
  196. const textureGPU = device.createTexture( textureGPUDescriptor );
  197. // transfer texture data
  198. if ( texture.isDataTexture ) {
  199. this._copyBufferToTexture( image, format, textureGPU );
  200. } else {
  201. // convert HTML iamges and canvas elements to ImageBitmap before copy
  202. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  203. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ) {
  204. const options = {};
  205. options.imageOrientation = ( texture.flipY === true ) ? 'flipY' : 'none';
  206. options.premultiplyAlpha = ( texture.premultiplyAlpha === true ) ? 'premultiply' : 'default';
  207. createImageBitmap( image, 0, 0, width, height, options ).then( imageBitmap => {
  208. this._copyImageBitmapToTexture( imageBitmap, textureGPU, needsMipmaps, textureGPUDescriptor );
  209. } );
  210. } else {
  211. if ( image !== undefined ) {
  212. // assuming ImageBitmap. Directly start copy operation of the contents of ImageBitmap into the destination texture
  213. this._copyImageBitmapToTexture( image, textureGPU, needsMipmaps, textureGPUDescriptor );
  214. }
  215. }
  216. }
  217. return textureGPU;
  218. }
  219. _copyBufferToTexture( image, format, textureGPU ) {
  220. // this code assumes data textures in RGBA format
  221. // @TODO: Consider to support valid buffer layouts with other formats like RGB
  222. // @TODO: Support mipmaps
  223. const device = this.device;
  224. const data = image.data;
  225. const bytesPerTexel = this._getBytesPerTexel( format );
  226. const bytesPerRow = Math.ceil( image.width * bytesPerTexel / 256 ) * 256;
  227. const textureDataBuffer = device.createBuffer( {
  228. size: data.byteLength,
  229. usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC,
  230. mappedAtCreation: true,
  231. } );
  232. new data.constructor( textureDataBuffer.getMappedRange() ).set( data );
  233. textureDataBuffer.unmap();
  234. const commandEncoder = device.createCommandEncoder( {} );
  235. commandEncoder.copyBufferToTexture(
  236. {
  237. buffer: textureDataBuffer,
  238. bytesPerRow: bytesPerRow
  239. }, {
  240. texture: textureGPU
  241. }, {
  242. width: image.width,
  243. height: image.height,
  244. depth: 1
  245. } );
  246. device.defaultQueue.submit( [ commandEncoder.finish() ] );
  247. textureDataBuffer.destroy();
  248. }
  249. _copyImageBitmapToTexture( imageBitmap, textureGPU, needsMipmaps, textureGPUDescriptor ) {
  250. const device = this.device;
  251. device.defaultQueue.copyImageBitmapToTexture(
  252. {
  253. imageBitmap: imageBitmap
  254. }, {
  255. texture: textureGPU
  256. }, {
  257. width: imageBitmap.width,
  258. height: imageBitmap.height,
  259. depth: 1
  260. }
  261. );
  262. if ( needsMipmaps === true ) {
  263. if ( this.utils === null ) {
  264. this.utils = new WebGPUTextureUtils( this.device, this.glslang ); // only create this helper if necessary
  265. }
  266. this.utils.generateMipmappedTexture( imageBitmap, textureGPU, textureGPUDescriptor );
  267. }
  268. }
  269. _getBytesPerTexel( format ) {
  270. if ( format === GPUTextureFormat.RGBA8Unorm ) return 4;
  271. if ( format === GPUTextureFormat.RGBA16Float ) return 8;
  272. if ( format === GPUTextureFormat.RGBA32Float ) return 16;
  273. }
  274. _needsMipmaps( texture ) {
  275. return ( texture.generateMipmaps === true ) && ( texture.minFilter !== NearestFilter ) && ( texture.minFilter !== LinearFilter );
  276. }
  277. }
  278. function onTextureDispose( event ) {
  279. const texture = event.target;
  280. const textureProperties = this.properties.get( texture );
  281. textureProperties.textureGPU.destroy();
  282. texture.removeEventListener( 'dispose', textureProperties.disposeCallback );
  283. this.properties.remove( texture );
  284. this.info.memory.textures --;
  285. }
  286. export default WebGPUTextures;