WebGPUTextures.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import { GPUTextureFormat, GPUAddressMode, GPUFilterMode } from './constants.js';
  2. import { Texture, NearestFilter, NearestMipmapNearestFilter, NearestMipmapLinearFilter, RepeatWrapping, MirroredRepeatWrapping } 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 call of _uploadTexture() 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. _createTexture( texture ) {
  97. const device = this.device;
  98. const image = texture.image;
  99. const width = ( image !== undefined ) ? image.width : 1;
  100. const height = ( image !== undefined ) ? image.height : 1;
  101. const textureGPU = device.createTexture( {
  102. size: {
  103. width: width,
  104. height: height,
  105. depth: 1,
  106. },
  107. format: GPUTextureFormat.RGBA8Unorm,
  108. usage: GPUTextureUsage.SAMPLED | GPUTextureUsage.COPY_DST,
  109. } );
  110. // convert HTML iamges and canvas elements to ImageBitmap before copy
  111. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  112. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ) {
  113. const options = {};
  114. options.imageOrientation = ( texture.flipY === true ) ? 'flipY' : 'none';
  115. createImageBitmap( image, 0, 0, width, height, options ).then( imageBitmap => {
  116. this._uploadTexture( imageBitmap, textureGPU );
  117. } );
  118. } else {
  119. if ( image !== undefined ) {
  120. // assuming ImageBitmap. Directly start copy operation of the contents of ImageBitmap into the destination texture
  121. this._uploadTexture( image, textureGPU );
  122. }
  123. }
  124. return textureGPU;
  125. }
  126. _uploadTexture( imageBitmap, textureGPU ) {
  127. const device = this.device;
  128. device.defaultQueue.copyImageBitmapToTexture(
  129. {
  130. imageBitmap: imageBitmap
  131. }, {
  132. texture: textureGPU
  133. }, {
  134. width: imageBitmap.width,
  135. height: imageBitmap.height,
  136. depth: 1
  137. }
  138. );
  139. }
  140. }
  141. export default WebGPUTextures;
粤ICP备19079148号