WebGPUTextures.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 WeakMap();
  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.sampler;
  31. }
  32. updateTexture( texture ) {
  33. let updated = 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. updated = true;
  60. }
  61. }
  62. return updated;
  63. }
  64. updateSampler( texture ) {
  65. let updated = false;
  66. const array = [];
  67. array.push( texture.wrapS );
  68. array.push( texture.wrapT );
  69. array.push( texture.wrapR );
  70. array.push( texture.magFilter );
  71. array.push( texture.minFilter );
  72. array.push( texture.anisotropy );
  73. const newKey = array.join();
  74. const key = this.samplerCache.get( texture );
  75. if ( key !== newKey ) {
  76. this.samplerCache.set( texture, newKey );
  77. const sampler = this.device.createSampler( {
  78. addressModeU: this._convertAddressMode( texture.wrapS ),
  79. addressModeV: this._convertAddressMode( texture.wrapT ),
  80. addressModeW: this._convertAddressMode( texture.wrapR ),
  81. magFilter: this._convertFilterMode( texture.magFilter ),
  82. minFilter: this._convertFilterMode( texture.minFilter ),
  83. mipmapFilter: this._convertFilterMode( texture.minFilter ),
  84. maxAnisotropy: texture.anisotropy
  85. } );
  86. const textureProperties = this.properties.get( texture );
  87. textureProperties.sampler = sampler;
  88. updated = true;
  89. }
  90. return updated;
  91. }
  92. _convertAddressMode( value ) {
  93. let addressMode = GPUAddressMode.ClampToEdge;
  94. if ( value === RepeatWrapping ) {
  95. addressMode = GPUAddressMode.Repeat;
  96. } else if ( value === MirroredRepeatWrapping ) {
  97. addressMode = GPUAddressMode.MirrorRepeat;
  98. }
  99. return addressMode;
  100. }
  101. _convertFilterMode( value ) {
  102. let filterMode = GPUFilterMode.Linear;
  103. if ( value === NearestFilter || value === NearestMipmapNearestFilter || value === NearestMipmapLinearFilter ) {
  104. filterMode = GPUFilterMode.Nearest;
  105. }
  106. return filterMode;
  107. }
  108. _convertFormat( type ) {
  109. let formatGPU = GPUTextureFormat.RGBA8Unorm;
  110. if ( type === FloatType ) {
  111. formatGPU = GPUTextureFormat.RGBA32Float;
  112. } else if ( type === HalfFloatType ) {
  113. formatGPU = GPUTextureFormat.RGBA16Float;
  114. }
  115. return formatGPU;
  116. }
  117. _createTexture( texture ) {
  118. const device = this.device;
  119. const image = texture.image;
  120. const width = ( image !== undefined ) ? image.width : 1;
  121. const height = ( image !== undefined ) ? image.height : 1;
  122. const format = this._convertFormat( texture.type );
  123. const textureGPU = device.createTexture( {
  124. size: {
  125. width: width,
  126. height: height,
  127. depth: 1,
  128. },
  129. format: format,
  130. usage: GPUTextureUsage.SAMPLED | GPUTextureUsage.COPY_DST,
  131. } );
  132. if ( texture.isDataTexture ) {
  133. this.__copyBufferToTexture( image, format, textureGPU );
  134. } else {
  135. // convert HTML iamges and canvas elements to ImageBitmap before copy
  136. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  137. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ) {
  138. const options = {};
  139. options.imageOrientation = ( texture.flipY === true ) ? 'flipY' : 'none';
  140. options.premultiplyAlpha = ( texture.premultiplyAlpha === true ) ? 'premultiply' : 'default';
  141. createImageBitmap( image, 0, 0, width, height, options ).then( imageBitmap => {
  142. this._copyImageBitmapToTexture( imageBitmap, textureGPU );
  143. } );
  144. } else {
  145. if ( image !== undefined ) {
  146. // assuming ImageBitmap. Directly start copy operation of the contents of ImageBitmap into the destination texture
  147. this._copyImageBitmapToTexture( image, textureGPU );
  148. }
  149. }
  150. }
  151. return textureGPU;
  152. }
  153. __copyBufferToTexture( image, format, textureGPU ) {
  154. // this code assumes data textures in RGBA format
  155. // TODO: Consider to support valid buffer layouts with other formats like RGB
  156. const device = this.device;
  157. const data = image.data;
  158. const bytesPerTexel = this._getBytesPerTexel( format );
  159. const bytesPerRow = Math.ceil( image.width * bytesPerTexel / 256 ) * 256;
  160. const textureDataBuffer = device.createBuffer( {
  161. size: data.byteLength,
  162. usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC,
  163. mappedAtCreation: true,
  164. } );
  165. new data.constructor( textureDataBuffer.getMappedRange() ).set( data );
  166. textureDataBuffer.unmap();
  167. const commandEncoder = device.createCommandEncoder( {} );
  168. commandEncoder.copyBufferToTexture(
  169. {
  170. buffer: textureDataBuffer,
  171. bytesPerRow: bytesPerRow
  172. }, {
  173. texture: textureGPU
  174. }, {
  175. width: image.width,
  176. height: image.height,
  177. depth: 1
  178. } );
  179. device.defaultQueue.submit( [ commandEncoder.finish() ] );
  180. textureDataBuffer.destroy();
  181. }
  182. _getBytesPerTexel( format ) {
  183. if ( format === GPUTextureFormat.RGBA8Unorm ) return 4;
  184. if ( format === GPUTextureFormat.RGBA16Float ) return 8;
  185. if ( format === GPUTextureFormat.RGBA32Float ) return 16;
  186. }
  187. _copyImageBitmapToTexture( imageBitmap, textureGPU ) {
  188. const device = this.device;
  189. device.defaultQueue.copyImageBitmapToTexture(
  190. {
  191. imageBitmap: imageBitmap
  192. }, {
  193. texture: textureGPU
  194. }, {
  195. width: imageBitmap.width,
  196. height: imageBitmap.height,
  197. depth: 1
  198. }
  199. );
  200. }
  201. }
  202. function onTextureDispose( event ) {
  203. const texture = event.target;
  204. const textureProperties = this.properties.get( texture );
  205. textureProperties.textureGPU.destroy();
  206. texture.removeEventListener( 'dispose', textureProperties.disposeCallback );
  207. this.properties.remove( texture );
  208. this.info.memory.textures --;
  209. }
  210. export default WebGPUTextures;