WebGPUTextures.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import { Texture, NearestFilter, NearestMipmapNearestFilter, NearestMipmapLinearFilter, RepeatWrapping, MirroredRepeatWrapping } from '../../../../build/three.module.js';
  2. class WebGPUTextures {
  3. constructor( device, properties ) {
  4. this.device = device;
  5. this.properties = properties;
  6. this.textures = new WeakMap();
  7. this.defaultTexture = null;
  8. this.defaultSampler = null;
  9. this.canvas = 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( new Image( 1, 1 ) ) );
  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. textureProperties.textureGPU = this._createTexture( texture );
  43. textureProperties.version = texture.version;
  44. updated = true;
  45. }
  46. }
  47. return updated;
  48. }
  49. updateSampler( texture ) {
  50. let updated = false;
  51. const array = [];
  52. array.push( texture.magFilter );
  53. array.push( texture.minFilter );
  54. array.push( texture.anisotropy );
  55. const newKey = array.join();
  56. const key = this.samplerCache.get( texture );
  57. if ( key !== newKey ) {
  58. this.samplerCache.set( texture, newKey );
  59. const sampler = this.device.createSampler( {
  60. addressModeU: this._convertAddressMode( texture.wrapS ),
  61. addressModeV: this._convertAddressMode( texture.wrapT ),
  62. addressModeW: this._convertAddressMode( texture.wrapR ),
  63. magFilter: this._convertFilterMode( texture.magFilter ),
  64. minFilter: this._convertFilterMode( texture.minFilter ),
  65. mipmapFilter: this._convertFilterMode( texture.minFilter ),
  66. maxAnisotropy: texture.anisotropy
  67. } );
  68. const textureProperties = this.properties.get( texture );
  69. textureProperties.sampler = sampler;
  70. updated = true;
  71. }
  72. return updated;
  73. }
  74. _convertAddressMode( value ) {
  75. let addressMode = 'clamp-to-edge';
  76. if ( value === RepeatWrapping ) {
  77. addressMode = 'repeat';
  78. } else if ( value === MirroredRepeatWrapping ) {
  79. addressMode = 'mirror-repeat';
  80. }
  81. return addressMode;
  82. }
  83. _convertFilterMode( value ) {
  84. let filterMode = 'linear';
  85. if ( value === NearestFilter || value === NearestMipmapNearestFilter || value === NearestMipmapLinearFilter ) {
  86. filterMode = 'nearest';
  87. }
  88. return filterMode;
  89. }
  90. _createTexture( texture ) {
  91. const device = this.device;
  92. const image = texture.image;
  93. if ( this.canvas === null ) this.canvas = new OffscreenCanvas( 1, 1 );
  94. const width = image.width;
  95. const height = image.height;
  96. this.canvas.width = width;
  97. this.canvas.height = height;
  98. const context = this.canvas.getContext( '2d' );
  99. context.translate( 0, height );
  100. context.scale( 1, - 1 );
  101. context.drawImage( image, 0, 0, width, height );
  102. const imageData = context.getImageData( 0, 0, width, height );
  103. let data = null;
  104. const bytesPerRow = Math.ceil( width * 4 / 256 ) * 256;
  105. if ( bytesPerRow === width * 4 ) {
  106. data = imageData.data;
  107. } else {
  108. // ensure 4 byte alignment
  109. data = new Uint8Array( bytesPerRow * height );
  110. let imagePixelIndex = 0;
  111. for ( let y = 0; y < height; ++ y ) {
  112. for ( let x = 0; x < width; ++ x ) {
  113. const i = x * 4 + y * bytesPerRow;
  114. data[ i ] = imageData.data[ imagePixelIndex ];
  115. data[ i + 1 ] = imageData.data[ imagePixelIndex + 1 ];
  116. data[ i + 2 ] = imageData.data[ imagePixelIndex + 2 ];
  117. data[ i + 3 ] = imageData.data[ imagePixelIndex + 3 ];
  118. imagePixelIndex += 4;
  119. }
  120. }
  121. }
  122. const textureGPU = device.createTexture( {
  123. size: {
  124. width: image.width,
  125. height: image.height,
  126. depth: 1,
  127. },
  128. format: "rgba8unorm",
  129. usage: GPUTextureUsage.SAMPLED | GPUTextureUsage.COPY_DST,
  130. } );
  131. const buffer = device.createBuffer( {
  132. size: data.byteLength,
  133. usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC,
  134. mappedAtCreation: true,
  135. } );
  136. new Uint8Array( buffer.getMappedRange() ).set( data );
  137. buffer.unmap();
  138. const commandEncoder = device.createCommandEncoder( {} );
  139. commandEncoder.copyBufferToTexture(
  140. {
  141. buffer: buffer, bytesPerRow: bytesPerRow,
  142. }, {
  143. texture: textureGPU,
  144. }, {
  145. width: image.width,
  146. height: image.height,
  147. depth: 1,
  148. } );
  149. device.defaultQueue.submit( [ commandEncoder.finish() ] );
  150. buffer.destroy();
  151. return textureGPU;
  152. }
  153. }
  154. export default WebGPUTextures;