WebGPUTextures.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 ) {
  5. this.device = device;
  6. this.properties = properties;
  7. this.textures = new WeakMap();
  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. if ( textureProperties.textureGPU !== undefined ) {
  43. // TODO: Avoid calling of destroy() in certain scenarios. When only the contents of a texture
  44. // are updated, a buffer upload should be sufficient. However, if the user changes
  45. // the dimensions of the texture, format or usage, a new instance of GPUTexture is required.
  46. textureProperties.textureGPU.destroy();
  47. }
  48. textureProperties.textureGPU = this._createTexture( texture );
  49. textureProperties.version = texture.version;
  50. updated = true;
  51. }
  52. }
  53. return updated;
  54. }
  55. updateSampler( texture ) {
  56. let updated = false;
  57. const array = [];
  58. array.push( texture.magFilter );
  59. array.push( texture.minFilter );
  60. array.push( texture.anisotropy );
  61. const newKey = array.join();
  62. const key = this.samplerCache.get( texture );
  63. if ( key !== newKey ) {
  64. this.samplerCache.set( texture, newKey );
  65. const sampler = this.device.createSampler( {
  66. addressModeU: this._convertAddressMode( texture.wrapS ),
  67. addressModeV: this._convertAddressMode( texture.wrapT ),
  68. addressModeW: this._convertAddressMode( texture.wrapR ),
  69. magFilter: this._convertFilterMode( texture.magFilter ),
  70. minFilter: this._convertFilterMode( texture.minFilter ),
  71. mipmapFilter: this._convertFilterMode( texture.minFilter ),
  72. maxAnisotropy: texture.anisotropy
  73. } );
  74. const textureProperties = this.properties.get( texture );
  75. textureProperties.sampler = sampler;
  76. updated = true;
  77. }
  78. return updated;
  79. }
  80. _convertAddressMode( value ) {
  81. let addressMode = GPUAddressMode.ClampToEdge;
  82. if ( value === RepeatWrapping ) {
  83. addressMode = GPUAddressMode.Repeat;
  84. } else if ( value === MirroredRepeatWrapping ) {
  85. addressMode = GPUAddressMode.MirrorRepeat;
  86. }
  87. return addressMode;
  88. }
  89. _convertFilterMode( value ) {
  90. let filterMode = GPUFilterMode.Linear;
  91. if ( value === NearestFilter || value === NearestMipmapNearestFilter || value === NearestMipmapLinearFilter ) {
  92. filterMode = GPUFilterMode.Nearest;
  93. }
  94. return filterMode;
  95. }
  96. _convertFormat( type ) {
  97. let formatGPU = GPUTextureFormat.RGBA8Unorm;
  98. if ( type === FloatType ) {
  99. formatGPU = GPUTextureFormat.RGBA32Float;
  100. } else if ( type === HalfFloatType ) {
  101. formatGPU = GPUTextureFormat.RGBA16Float;
  102. }
  103. return formatGPU;
  104. }
  105. _createTexture( texture ) {
  106. const device = this.device;
  107. const image = texture.image;
  108. const width = ( image !== undefined ) ? image.width : 1;
  109. const height = ( image !== undefined ) ? image.height : 1;
  110. const format = this._convertFormat( texture.type );
  111. const textureGPU = device.createTexture( {
  112. size: {
  113. width: width,
  114. height: height,
  115. depth: 1,
  116. },
  117. format: format,
  118. usage: GPUTextureUsage.SAMPLED | GPUTextureUsage.COPY_DST,
  119. } );
  120. if ( texture.isDataTexture ) {
  121. this.__copyBufferToTexture( image, format, textureGPU );
  122. } else {
  123. // convert HTML iamges and canvas elements to ImageBitmap before copy
  124. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  125. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ) {
  126. const options = {};
  127. options.imageOrientation = ( texture.flipY === true ) ? 'flipY' : 'none';
  128. createImageBitmap( image, 0, 0, width, height, options ).then( imageBitmap => {
  129. this._copyImageBitmapToTexture( imageBitmap, textureGPU );
  130. } );
  131. } else {
  132. if ( image !== undefined ) {
  133. // assuming ImageBitmap. Directly start copy operation of the contents of ImageBitmap into the destination texture
  134. this._copyImageBitmapToTexture( image, textureGPU );
  135. }
  136. }
  137. }
  138. return textureGPU;
  139. }
  140. __copyBufferToTexture( image, format, textureGPU ) {
  141. // this code assumes data textures in RGBA format
  142. // TODO: Consider to support valid buffer layouts with other formats like RGB
  143. const device = this.device;
  144. const data = image.data;
  145. const bytesPerTexel = this._getBytesPerTexel( format );
  146. const bytesPerRow = Math.ceil( image.width * bytesPerTexel / 256 ) * 256;
  147. const textureDataBuffer = device.createBuffer( {
  148. size: data.byteLength,
  149. usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC,
  150. mappedAtCreation: true,
  151. } );
  152. new data.constructor( textureDataBuffer.getMappedRange() ).set( data );
  153. textureDataBuffer.unmap();
  154. const commandEncoder = device.createCommandEncoder( {} );
  155. commandEncoder.copyBufferToTexture(
  156. {
  157. buffer: textureDataBuffer,
  158. bytesPerRow: bytesPerRow
  159. }, {
  160. texture: textureGPU
  161. }, {
  162. width: image.width,
  163. height: image.height,
  164. depth: 1
  165. } );
  166. device.defaultQueue.submit( [ commandEncoder.finish() ] );
  167. textureDataBuffer.destroy();
  168. }
  169. _getBytesPerTexel( format ) {
  170. if ( format === GPUTextureFormat.RGBA8Unorm ) return 4;
  171. if ( format === GPUTextureFormat.RGBA32Float ) return 16;
  172. if ( format === GPUTextureFormat.RGBA16Float ) return 8;
  173. }
  174. _copyImageBitmapToTexture( imageBitmap, textureGPU ) {
  175. const device = this.device;
  176. device.defaultQueue.copyImageBitmapToTexture(
  177. {
  178. imageBitmap: imageBitmap
  179. }, {
  180. texture: textureGPU
  181. }, {
  182. width: imageBitmap.width,
  183. height: imageBitmap.height,
  184. depth: 1
  185. }
  186. );
  187. }
  188. }
  189. export default WebGPUTextures;