WebGPUTextures.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.samplerCache = new WeakMap();
  10. }
  11. getDefaultSampler() {
  12. if ( this.defaultSampler === null ) {
  13. this.defaultSampler = this.device.createSampler( {} );
  14. }
  15. return this.defaultSampler;
  16. }
  17. getDefaultTexture() {
  18. if ( this.defaultTexture === null ) {
  19. this.defaultTexture = this._createTexture( new Texture() );
  20. }
  21. return this.defaultTexture;
  22. }
  23. getTextureGPU( texture ) {
  24. const textureProperties = this.properties.get( texture );
  25. return textureProperties.textureGPU;
  26. }
  27. getSampler( texture ) {
  28. const textureProperties = this.properties.get( texture );
  29. return textureProperties.sampler;
  30. }
  31. updateTexture( texture ) {
  32. let updated = false;
  33. const textureProperties = this.properties.get( texture );
  34. if ( texture.version > 0 && textureProperties.version !== texture.version ) {
  35. const image = texture.image;
  36. if ( image === undefined ) {
  37. console.warn( 'THREE.WebGPURenderer: Texture marked for update but image is undefined' );
  38. } else if ( image.complete === false ) {
  39. console.warn( 'THREE.WebGPURenderer: Texture marked for update but image is incomplete' );
  40. } else {
  41. textureProperties.textureGPU = this._createTexture( texture );
  42. textureProperties.version = texture.version;
  43. updated = true;
  44. }
  45. }
  46. return updated;
  47. }
  48. updateSampler( texture ) {
  49. let updated = false;
  50. const array = [];
  51. array.push( texture.magFilter );
  52. array.push( texture.minFilter );
  53. array.push( texture.anisotropy );
  54. const newKey = array.join();
  55. const key = this.samplerCache.get( texture );
  56. if ( key !== newKey ) {
  57. this.samplerCache.set( texture, newKey );
  58. const sampler = this.device.createSampler( {
  59. addressModeU: this._convertAddressMode( texture.wrapS ),
  60. addressModeV: this._convertAddressMode( texture.wrapT ),
  61. addressModeW: this._convertAddressMode( texture.wrapR ),
  62. magFilter: this._convertFilterMode( texture.magFilter ),
  63. minFilter: this._convertFilterMode( texture.minFilter ),
  64. mipmapFilter: this._convertFilterMode( texture.minFilter ),
  65. maxAnisotropy: texture.anisotropy
  66. } );
  67. const textureProperties = this.properties.get( texture );
  68. textureProperties.sampler = sampler;
  69. updated = true;
  70. }
  71. return updated;
  72. }
  73. _convertAddressMode( value ) {
  74. let addressMode = 'clamp-to-edge';
  75. if ( value === RepeatWrapping ) {
  76. addressMode = 'repeat';
  77. } else if ( value === MirroredRepeatWrapping ) {
  78. addressMode = 'mirror-repeat';
  79. }
  80. return addressMode;
  81. }
  82. _convertFilterMode( value ) {
  83. let filterMode = 'linear';
  84. if ( value === NearestFilter || value === NearestMipmapNearestFilter || value === NearestMipmapLinearFilter ) {
  85. filterMode = 'nearest';
  86. }
  87. return filterMode;
  88. }
  89. _createTexture( texture ) {
  90. const device = this.device;
  91. const image = texture.image;
  92. const width = ( image !== undefined ) ? image.width : 1;
  93. const height = ( image !== undefined ) ? image.height : 1;
  94. const textureGPU = device.createTexture( {
  95. size: {
  96. width: width,
  97. height: height,
  98. depth: 1,
  99. },
  100. format: "rgba8unorm",
  101. usage: GPUTextureUsage.SAMPLED | GPUTextureUsage.COPY_DST,
  102. } );
  103. // convert HTML iamges and canvas elements to ImageBitmap before copy
  104. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  105. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ) {
  106. createImageBitmap( image, 0, 0, width, height ).then( imageBitmap => {
  107. this._uploadTexture( imageBitmap, textureGPU );
  108. } );
  109. } else {
  110. if ( image !== undefined ) {
  111. // assuming ImageBitmap. Directly start operation of the contents of ImageBitmap into the destination texture
  112. this._uploadTexture( image, textureGPU );
  113. }
  114. }
  115. return textureGPU;
  116. }
  117. _uploadTexture( imageBitmap, textureGPU ) {
  118. const device = this.device;
  119. device.defaultQueue.copyImageBitmapToTexture(
  120. {
  121. imageBitmap: imageBitmap
  122. }, {
  123. texture: textureGPU
  124. }, {
  125. width: imageBitmap.width,
  126. height: imageBitmap.height,
  127. depth: 1
  128. }
  129. );
  130. }
  131. }
  132. export default WebGPUTextures;