WebGPUTextures.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. 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 = GPUAddressMode.ClampToEdge;
  76. if ( value === RepeatWrapping ) {
  77. addressMode = GPUAddressMode.Repeat;
  78. } else if ( value === MirroredRepeatWrapping ) {
  79. addressMode = GPUAddressMode.MirrorRepeat;
  80. }
  81. return addressMode;
  82. }
  83. _convertFilterMode( value ) {
  84. let filterMode = GPUFilterMode.Linear;
  85. if ( value === NearestFilter || value === NearestMipmapNearestFilter || value === NearestMipmapLinearFilter ) {
  86. filterMode = GPUFilterMode.Nearest;
  87. }
  88. return filterMode;
  89. }
  90. _createTexture( texture ) {
  91. const device = this.device;
  92. const image = texture.image;
  93. const width = ( image !== undefined ) ? image.width : 1;
  94. const height = ( image !== undefined ) ? image.height : 1;
  95. const textureGPU = device.createTexture( {
  96. size: {
  97. width: width,
  98. height: height,
  99. depth: 1,
  100. },
  101. format: GPUTextureFormat.RGBA8Unorm,
  102. usage: GPUTextureUsage.SAMPLED | GPUTextureUsage.COPY_DST,
  103. } );
  104. // convert HTML iamges and canvas elements to ImageBitmap before copy
  105. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  106. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ) {
  107. createImageBitmap( image, 0, 0, width, height ).then( imageBitmap => {
  108. this._uploadTexture( imageBitmap, textureGPU );
  109. } );
  110. } else {
  111. if ( image !== undefined ) {
  112. // assuming ImageBitmap. Directly start copy operation of the contents of ImageBitmap into the destination texture
  113. this._uploadTexture( image, textureGPU );
  114. }
  115. }
  116. return textureGPU;
  117. }
  118. _uploadTexture( imageBitmap, textureGPU ) {
  119. const device = this.device;
  120. device.defaultQueue.copyImageBitmapToTexture(
  121. {
  122. imageBitmap: imageBitmap
  123. }, {
  124. texture: textureGPU
  125. }, {
  126. width: imageBitmap.width,
  127. height: imageBitmap.height,
  128. depth: 1
  129. }
  130. );
  131. }
  132. }
  133. export default WebGPUTextures;