WebGPUTextures.js 6.9 KB

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