WebGPUTextures.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. import { GPUTextureFormat, GPUAddressMode, GPUFilterMode, GPUTextureDimension, GPUFeatureName } from './constants.js';
  2. import { VideoTexture, CubeTexture, Texture, NearestFilter, NearestMipmapNearestFilter, NearestMipmapLinearFilter, LinearFilter, RepeatWrapping, MirroredRepeatWrapping, RGB_ETC2_Format, RGBA_ETC2_EAC_Format,
  3. RGBAFormat, RedFormat, RGFormat, RGBA_S3TC_DXT1_Format, RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT5_Format, UnsignedByteType, FloatType, HalfFloatType, SRGBColorSpace, DepthFormat, DepthStencilFormat, DepthTexture,
  4. RGBA_ASTC_4x4_Format, RGBA_ASTC_5x4_Format, RGBA_ASTC_5x5_Format, RGBA_ASTC_6x5_Format, RGBA_ASTC_6x6_Format, RGBA_ASTC_8x5_Format, RGBA_ASTC_8x6_Format, RGBA_ASTC_8x8_Format, RGBA_ASTC_10x5_Format,
  5. RGBA_ASTC_10x6_Format, RGBA_ASTC_10x8_Format, RGBA_ASTC_10x10_Format, RGBA_ASTC_12x10_Format, RGBA_ASTC_12x12_Format, UnsignedIntType, UnsignedShortType, UnsignedInt248Type
  6. } from 'three';
  7. import WebGPUTextureUtils from './WebGPUTextureUtils.js';
  8. class WebGPUTextures {
  9. constructor( device, properties, info ) {
  10. this.device = device;
  11. this.properties = properties;
  12. this.info = info;
  13. this.defaultTexture = null;
  14. this.depthDefaultTexture = null;
  15. this.defaultVideoTexture = null;
  16. this.defaultCubeTexture = null;
  17. this.defaultSampler = null;
  18. this.samplerCache = new Map();
  19. this.utils = null;
  20. }
  21. getDefaultSampler() {
  22. if ( this.defaultSampler === null ) {
  23. this.defaultSampler = this.device.createSampler( {} );
  24. }
  25. return this.defaultSampler;
  26. }
  27. getDefaultDepthTexture() {
  28. if ( this.depthDefaultTexture === null ) {
  29. const depthTexture = new DepthTexture();
  30. depthTexture.image.width = 1;
  31. depthTexture.image.height = 1;
  32. this._uploadTexture( depthTexture );
  33. this.depthDefaultTexture = this.getTextureGPU( depthTexture );
  34. }
  35. return this.depthDefaultTexture;
  36. }
  37. getDefaultTexture() {
  38. if ( this.defaultTexture === null ) {
  39. const texture = new Texture();
  40. texture.minFilter = NearestFilter;
  41. texture.magFilter = NearestFilter;
  42. this._uploadTexture( texture );
  43. this.defaultTexture = this.getTextureGPU( texture );
  44. }
  45. return this.defaultTexture;
  46. }
  47. getDefaultVideoTexture() {
  48. if ( this.defaultVideoTexture === null ) {
  49. const video = document.getElementById( 'video' );
  50. const texture = new VideoTexture( video );
  51. texture.minFilter = NearestFilter;
  52. texture.magFilter = NearestFilter;
  53. this._uploadVideoTexture( texture );
  54. this.defaultVideoTexture = this.getTextureGPU( texture );
  55. }
  56. return this.defaultVideoTexture;
  57. }
  58. getDefaultCubeTexture() {
  59. if ( this.defaultCubeTexture === null ) {
  60. const texture = new CubeTexture();
  61. texture.minFilter = NearestFilter;
  62. texture.magFilter = NearestFilter;
  63. this._uploadTexture( texture );
  64. this.defaultCubeTexture = this.getTextureGPU( texture );
  65. }
  66. return this.defaultCubeTexture;
  67. }
  68. getTextureGPU( texture ) {
  69. const textureProperties = this.properties.get( texture );
  70. return textureProperties.textureGPU;
  71. }
  72. getSampler( texture ) {
  73. const textureProperties = this.properties.get( texture );
  74. return textureProperties.samplerGPU;
  75. }
  76. updateTexture( texture ) {
  77. let needsUpdate = false;
  78. const textureProperties = this.properties.get( texture );
  79. if ( texture.version > 0 && textureProperties.version !== texture.version ) {
  80. const image = texture.image;
  81. if ( image === undefined ) {
  82. console.warn( 'THREE.WebGPURenderer: Texture marked for update but image is undefined.' );
  83. } else if ( image.complete === false ) {
  84. console.warn( 'THREE.WebGPURenderer: Texture marked for update but image is incomplete.' );
  85. } else {
  86. // texture init
  87. if ( textureProperties.initialized === undefined ) {
  88. textureProperties.initialized = true;
  89. const disposeCallback = onTextureDispose.bind( this );
  90. textureProperties.disposeCallback = disposeCallback;
  91. texture.addEventListener( 'dispose', disposeCallback );
  92. this.info.memory.textures ++;
  93. }
  94. //
  95. if ( texture.isVideoTexture ) {
  96. needsUpdate = this._uploadVideoTexture( texture );
  97. } else {
  98. needsUpdate = this._uploadTexture( texture );
  99. }
  100. }
  101. }
  102. // if the texture is used for RTT, it's necessary to init it once so the binding
  103. // group's resource definition points to the respective GPUTexture
  104. if ( textureProperties.initializedRTT === false ) {
  105. textureProperties.initializedRTT = true;
  106. needsUpdate = true;
  107. }
  108. return needsUpdate;
  109. }
  110. updateSampler( texture ) {
  111. const array = [];
  112. array.push( texture.wrapS );
  113. array.push( texture.wrapT );
  114. array.push( texture.wrapR );
  115. array.push( texture.magFilter );
  116. array.push( texture.minFilter );
  117. array.push( texture.anisotropy );
  118. const key = array.join();
  119. let samplerGPU = this.samplerCache.get( key );
  120. if ( samplerGPU === undefined ) {
  121. samplerGPU = this.device.createSampler( {
  122. addressModeU: this._convertAddressMode( texture.wrapS ),
  123. addressModeV: this._convertAddressMode( texture.wrapT ),
  124. addressModeW: this._convertAddressMode( texture.wrapR ),
  125. magFilter: this._convertFilterMode( texture.magFilter ),
  126. minFilter: this._convertFilterMode( texture.minFilter ),
  127. mipmapFilter: this._convertFilterMode( texture.minFilter ),
  128. maxAnisotropy: texture.anisotropy
  129. } );
  130. this.samplerCache.set( key, samplerGPU );
  131. }
  132. const textureProperties = this.properties.get( texture );
  133. textureProperties.samplerGPU = samplerGPU;
  134. }
  135. initRenderTarget( renderTarget ) {
  136. const properties = this.properties;
  137. const renderTargetProperties = properties.get( renderTarget );
  138. if ( renderTargetProperties.initialized === undefined ) {
  139. const device = this.device;
  140. const width = renderTarget.width;
  141. const height = renderTarget.height;
  142. const texture = renderTarget.texture;
  143. const colorTextureFormat = texture.internalFormat || this._getFormat( texture );
  144. const label = texture.name ? '_' + texture.name : '';
  145. const needsMipmaps = this._needsMipmaps( texture );
  146. const mipLevelCount = this._getMipLevelCount( texture, width, height, needsMipmaps );
  147. const colorTextureGPU = device.createTexture( {
  148. label: 'renderTarget' + label,
  149. size: {
  150. width: width,
  151. height: height,
  152. depthOrArrayLayers: 1
  153. },
  154. mipLevelCount: mipLevelCount,
  155. format: colorTextureFormat,
  156. usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
  157. } );
  158. this.info.memory.textures ++;
  159. renderTargetProperties.colorTextureGPU = colorTextureGPU;
  160. renderTargetProperties.colorTextureFormat = colorTextureFormat;
  161. // When the ".texture" or ".depthTexture" property of a render target is used as a map,
  162. // the renderer has to find the respective GPUTexture objects to setup the bind groups.
  163. // Since it's not possible to see just from a texture object whether it belongs to a render
  164. // target or not, we need the initializedRTT flag.
  165. const textureProperties = properties.get( texture );
  166. textureProperties.textureGPU = colorTextureGPU;
  167. textureProperties.initializedRTT = false;
  168. if ( renderTarget.depthBuffer === true ) {
  169. const depthTextureFormat = renderTarget.depthTexture !== null ? this._getFormat( renderTarget.depthTexture ) : GPUTextureFormat.Depth24PlusStencil8;
  170. const depthTextureGPU = device.createTexture( {
  171. label: 'renderTarget' + label + '_depthBuffer',
  172. size: {
  173. width: width,
  174. height: height,
  175. depthOrArrayLayers: 1
  176. },
  177. format: depthTextureFormat,
  178. usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
  179. } );
  180. this.info.memory.textures ++;
  181. renderTargetProperties.depthTextureGPU = depthTextureGPU;
  182. renderTargetProperties.depthTextureFormat = depthTextureFormat;
  183. if ( renderTarget.depthTexture !== null ) {
  184. const depthTextureProperties = properties.get( renderTarget.depthTexture );
  185. depthTextureProperties.textureGPU = depthTextureGPU;
  186. depthTextureProperties.initializedRTT = false;
  187. }
  188. }
  189. //
  190. const disposeCallback = onRenderTargetDispose.bind( this );
  191. renderTargetProperties.disposeCallback = disposeCallback;
  192. renderTarget.addEventListener( 'dispose', disposeCallback );
  193. //
  194. renderTargetProperties.initialized = true;
  195. }
  196. }
  197. dispose() {
  198. this.samplerCache.clear();
  199. }
  200. _convertAddressMode( value ) {
  201. let addressMode = GPUAddressMode.ClampToEdge;
  202. if ( value === RepeatWrapping ) {
  203. addressMode = GPUAddressMode.Repeat;
  204. } else if ( value === MirroredRepeatWrapping ) {
  205. addressMode = GPUAddressMode.MirrorRepeat;
  206. }
  207. return addressMode;
  208. }
  209. _convertFilterMode( value ) {
  210. let filterMode = GPUFilterMode.Linear;
  211. if ( value === NearestFilter || value === NearestMipmapNearestFilter || value === NearestMipmapLinearFilter ) {
  212. filterMode = GPUFilterMode.Nearest;
  213. }
  214. return filterMode;
  215. }
  216. _uploadVideoTexture( texture ) {
  217. const device = this.device;
  218. const textureProperties = this.properties.get( texture );
  219. const textureGPU = device.importExternalTexture( {
  220. source: texture.source.data
  221. } );
  222. textureProperties.textureGPU = textureGPU;
  223. //textureProperties.version = texture.version; // @TODO: Force update for now, study a better solution soon using native VideoTexture.update() to fix warns
  224. return true;
  225. }
  226. _uploadTexture( texture ) {
  227. let needsUpdate = false;
  228. const device = this.device;
  229. const image = texture.image;
  230. const textureProperties = this.properties.get( texture );
  231. const { width, height, depth } = this._getSize( texture );
  232. const needsMipmaps = this._needsMipmaps( texture );
  233. const dimension = this._getDimension( texture );
  234. const mipLevelCount = this._getMipLevelCount( texture, width, height, needsMipmaps );
  235. const format = texture.internalFormat || this._getFormat( texture );
  236. let usage = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST;
  237. if ( needsMipmaps ) {
  238. // current mipmap generation requires RENDER_ATTACHMENT
  239. usage |= GPUTextureUsage.RENDER_ATTACHMENT;
  240. }
  241. const textureGPUDescriptor = {
  242. label: texture.name,
  243. size: {
  244. width: width,
  245. height: height,
  246. depthOrArrayLayers: depth,
  247. },
  248. mipLevelCount: mipLevelCount,
  249. sampleCount: 1,
  250. dimension: dimension,
  251. format: format,
  252. usage: usage
  253. };
  254. // texture creation
  255. let textureGPU = textureProperties.textureGPU;
  256. if ( textureGPU === undefined ) {
  257. textureGPU = device.createTexture( textureGPUDescriptor );
  258. needsUpdate = true;
  259. }
  260. // transfer texture data
  261. if ( texture.isDataTexture || texture.isDataArrayTexture || texture.isData3DTexture ) {
  262. this._copyBufferToTexture( image, textureGPU, textureGPUDescriptor, needsMipmaps );
  263. } else if ( texture.isCompressedTexture ) {
  264. this._copyCompressedBufferToTexture( texture.mipmaps, textureGPU, textureGPUDescriptor );
  265. } else if ( texture.isCubeTexture ) {
  266. if ( image.length === 6 ) {
  267. this._copyCubeMapToTexture( image, texture, textureGPU, textureGPUDescriptor, needsMipmaps );
  268. }
  269. } else if ( texture.isRenderTargetTexture ) {
  270. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor );
  271. } else if ( texture.isDepthTexture !== true && image !== null ) {
  272. this._copyImageToTexture( image, texture, textureGPU, textureGPUDescriptor, needsMipmaps );
  273. }
  274. //
  275. textureProperties.textureGPU = textureGPU;
  276. textureProperties.version = texture.version;
  277. return needsUpdate;
  278. }
  279. _copyBufferToTexture( image, textureGPU, textureGPUDescriptor, needsMipmaps, originDepth = 0 ) {
  280. // @TODO: Consider to use GPUCommandEncoder.copyBufferToTexture()
  281. // @TODO: Consider to support valid buffer layouts with other formats like RGB
  282. const data = image.data;
  283. const bytesPerTexel = this._getBytesPerTexel( textureGPUDescriptor.format );
  284. const bytesPerRow = image.width * bytesPerTexel;
  285. this.device.queue.writeTexture(
  286. {
  287. texture: textureGPU,
  288. mipLevel: 0,
  289. origin: { x: 0, y: 0, z: originDepth }
  290. },
  291. data,
  292. {
  293. offset: 0,
  294. bytesPerRow
  295. },
  296. {
  297. width: image.width,
  298. height: image.height,
  299. depthOrArrayLayers: ( image.depth !== undefined ) ? image.depth : 1
  300. } );
  301. if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor, originDepth );
  302. }
  303. _copyCubeMapToTexture( images, texture, textureGPU, textureGPUDescriptor, needsMipmaps ) {
  304. for ( let i = 0; i < 6; i ++ ) {
  305. const image = images[ i ];
  306. if ( image.isDataTexture ) {
  307. this._copyBufferToTexture( image.image, textureGPU, textureGPUDescriptor, needsMipmaps, i );
  308. } else {
  309. this._copyImageToTexture( image, texture, textureGPU, textureGPUDescriptor, needsMipmaps, i );
  310. }
  311. }
  312. }
  313. _copyExternalImageToTexture( image, textureGPU, textureGPUDescriptor, needsMipmaps, originDepth = 0 ) {
  314. this.device.queue.copyExternalImageToTexture(
  315. {
  316. source: image
  317. }, {
  318. texture: textureGPU,
  319. mipLevel: 0,
  320. origin: { x: 0, y: 0, z: originDepth }
  321. }, {
  322. width: image.width,
  323. height: image.height,
  324. depthOrArrayLayers: 1
  325. }
  326. );
  327. if ( needsMipmaps ) this._generateMipmaps( textureGPU, textureGPUDescriptor, originDepth );
  328. }
  329. _copyCompressedBufferToTexture( mipmaps, textureGPU, textureGPUDescriptor ) {
  330. // @TODO: Consider to use GPUCommandEncoder.copyBufferToTexture()
  331. const blockData = this._getBlockData( textureGPUDescriptor.format );
  332. for ( let i = 0; i < mipmaps.length; i ++ ) {
  333. const mipmap = mipmaps[ i ];
  334. const width = mipmap.width;
  335. const height = mipmap.height;
  336. const bytesPerRow = Math.ceil( width / blockData.width ) * blockData.byteLength;
  337. this.device.queue.writeTexture(
  338. {
  339. texture: textureGPU,
  340. mipLevel: i
  341. },
  342. mipmap.data,
  343. {
  344. offset: 0,
  345. bytesPerRow
  346. },
  347. {
  348. width: Math.ceil( width / blockData.width ) * blockData.width,
  349. height: Math.ceil( height / blockData.width ) * blockData.width,
  350. depthOrArrayLayers: 1
  351. }
  352. );
  353. }
  354. }
  355. _generateMipmaps( textureGPU, textureGPUDescriptor, baseArrayLayer ) {
  356. if ( this.utils === null ) {
  357. this.utils = new WebGPUTextureUtils( this.device ); // only create this helper if necessary
  358. }
  359. this.utils.generateMipmaps( textureGPU, textureGPUDescriptor, baseArrayLayer );
  360. }
  361. _getBlockData( format ) {
  362. // this method is only relevant for compressed texture formats
  363. if ( format === GPUTextureFormat.BC1RGBAUnorm || format === GPUTextureFormat.BC1RGBAUnormSRGB ) return { byteLength: 8, width: 4, height: 4 }; // DXT1
  364. if ( format === GPUTextureFormat.BC2RGBAUnorm || format === GPUTextureFormat.BC2RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // DXT3
  365. if ( format === GPUTextureFormat.BC3RGBAUnorm || format === GPUTextureFormat.BC3RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // DXT5
  366. if ( format === GPUTextureFormat.BC4RUnorm || format === GPUTextureFormat.BC4RSNorm ) return { byteLength: 8, width: 4, height: 4 }; // RGTC1
  367. if ( format === GPUTextureFormat.BC5RGUnorm || format === GPUTextureFormat.BC5RGSnorm ) return { byteLength: 16, width: 4, height: 4 }; // RGTC2
  368. if ( format === GPUTextureFormat.BC6HRGBUFloat || format === GPUTextureFormat.BC6HRGBFloat ) return { byteLength: 16, width: 4, height: 4 }; // BPTC (float)
  369. if ( format === GPUTextureFormat.BC7RGBAUnorm || format === GPUTextureFormat.BC7RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // BPTC (unorm)
  370. if ( format === GPUTextureFormat.ETC2RGB8Unorm || format === GPUTextureFormat.ETC2RGB8UnormSRGB ) return { byteLength: 8, width: 4, height: 4 };
  371. if ( format === GPUTextureFormat.ETC2RGB8A1Unorm || format === GPUTextureFormat.ETC2RGB8A1UnormSRGB ) return { byteLength: 8, width: 4, height: 4 };
  372. if ( format === GPUTextureFormat.ETC2RGBA8Unorm || format === GPUTextureFormat.ETC2RGBA8UnormSRGB ) return { byteLength: 16, width: 4, height: 4 };
  373. if ( format === GPUTextureFormat.EACR11Unorm ) return { byteLength: 8, width: 4, height: 4 };
  374. if ( format === GPUTextureFormat.EACR11Snorm ) return { byteLength: 8, width: 4, height: 4 };
  375. if ( format === GPUTextureFormat.EACRG11Unorm ) return { byteLength: 16, width: 4, height: 4 };
  376. if ( format === GPUTextureFormat.EACRG11Snorm ) return { byteLength: 16, width: 4, height: 4 };
  377. if ( format === GPUTextureFormat.ASTC4x4Unorm || format === GPUTextureFormat.ASTC4x4UnormSRGB ) return { byteLength: 16, width: 4, height: 4 };
  378. if ( format === GPUTextureFormat.ASTC5x4Unorm || format === GPUTextureFormat.ASTC5x4UnormSRGB ) return { byteLength: 16, width: 5, height: 4 };
  379. if ( format === GPUTextureFormat.ASTC5x5Unorm || format === GPUTextureFormat.ASTC5x5UnormSRGB ) return { byteLength: 16, width: 5, height: 5 };
  380. if ( format === GPUTextureFormat.ASTC6x5Unorm || format === GPUTextureFormat.ASTC6x5UnormSRGB ) return { byteLength: 16, width: 6, height: 5 };
  381. if ( format === GPUTextureFormat.ASTC6x6Unorm || format === GPUTextureFormat.ASTC6x6UnormSRGB ) return { byteLength: 16, width: 6, height: 6 };
  382. if ( format === GPUTextureFormat.ASTC8x5Unorm || format === GPUTextureFormat.ASTC8x5UnormSRGB ) return { byteLength: 16, width: 8, height: 5 };
  383. if ( format === GPUTextureFormat.ASTC8x6Unorm || format === GPUTextureFormat.ASTC8x6UnormSRGB ) return { byteLength: 16, width: 8, height: 6 };
  384. if ( format === GPUTextureFormat.ASTC8x8Unorm || format === GPUTextureFormat.ASTC8x8UnormSRGB ) return { byteLength: 16, width: 8, height: 8 };
  385. if ( format === GPUTextureFormat.ASTC10x5Unorm || format === GPUTextureFormat.ASTC10x5UnormSRGB ) return { byteLength: 16, width: 10, height: 5 };
  386. if ( format === GPUTextureFormat.ASTC10x6Unorm || format === GPUTextureFormat.ASTC10x6UnormSRGB ) return { byteLength: 16, width: 10, height: 6 };
  387. if ( format === GPUTextureFormat.ASTC10x8Unorm || format === GPUTextureFormat.ASTC10x8UnormSRGB ) return { byteLength: 16, width: 10, height: 8 };
  388. if ( format === GPUTextureFormat.ASTC10x10Unorm || format === GPUTextureFormat.ASTC10x10UnormSRGB ) return { byteLength: 16, width: 10, height: 10 };
  389. if ( format === GPUTextureFormat.ASTC12x10Unorm || format === GPUTextureFormat.ASTC12x10UnormSRGB ) return { byteLength: 16, width: 12, height: 10 };
  390. if ( format === GPUTextureFormat.ASTC12x12Unorm || format === GPUTextureFormat.ASTC12x12UnormSRGB ) return { byteLength: 16, width: 12, height: 12 };
  391. }
  392. _getBytesPerTexel( format ) {
  393. if ( format === GPUTextureFormat.R8Unorm ) return 1;
  394. if ( format === GPUTextureFormat.R16Float ) return 2;
  395. if ( format === GPUTextureFormat.RG8Unorm ) return 2;
  396. if ( format === GPUTextureFormat.RG16Float ) return 4;
  397. if ( format === GPUTextureFormat.R32Float ) return 4;
  398. if ( format === GPUTextureFormat.RGBA8Unorm || format === GPUTextureFormat.RGBA8UnormSRGB ) return 4;
  399. if ( format === GPUTextureFormat.RG32Float ) return 8;
  400. if ( format === GPUTextureFormat.RGBA16Float ) return 8;
  401. if ( format === GPUTextureFormat.RGBA32Float ) return 16;
  402. }
  403. _getDimension( texture ) {
  404. let dimension;
  405. if ( texture.isData3DTexture ) {
  406. dimension = GPUTextureDimension.ThreeD;
  407. } else {
  408. dimension = GPUTextureDimension.TwoD;
  409. }
  410. return dimension;
  411. }
  412. _getFormat( texture ) {
  413. const format = texture.format;
  414. const type = texture.type;
  415. const colorSpace = texture.colorSpace;
  416. let formatGPU;
  417. if ( texture.isCompressedTexture === true ) {
  418. switch ( format ) {
  419. case RGBA_S3TC_DXT1_Format:
  420. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC1RGBAUnormSRGB : GPUTextureFormat.BC1RGBAUnorm;
  421. break;
  422. case RGBA_S3TC_DXT3_Format:
  423. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC2RGBAUnormSRGB : GPUTextureFormat.BC2RGBAUnorm;
  424. break;
  425. case RGBA_S3TC_DXT5_Format:
  426. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC3RGBAUnormSRGB : GPUTextureFormat.BC3RGBAUnorm;
  427. break;
  428. case RGB_ETC2_Format:
  429. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ETC2RGB8UnormSRGB : GPUTextureFormat.ETC2RGB8Unorm;
  430. break;
  431. case RGBA_ETC2_EAC_Format:
  432. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ETC2RGBA8UnormSRGB : GPUTextureFormat.ETC2RGBA8Unorm;
  433. break;
  434. case RGBA_ASTC_4x4_Format:
  435. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC4x4UnormSRGB : GPUTextureFormat.ASTC4x4Unorm;
  436. break;
  437. case RGBA_ASTC_5x4_Format:
  438. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC5x4UnormSRGB : GPUTextureFormat.ASTC5x4Unorm;
  439. break;
  440. case RGBA_ASTC_5x5_Format:
  441. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC5x5UnormSRGB : GPUTextureFormat.ASTC5x5Unorm;
  442. break;
  443. case RGBA_ASTC_6x5_Format:
  444. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC6x5UnormSRGB : GPUTextureFormat.ASTC6x5Unorm;
  445. break;
  446. case RGBA_ASTC_6x6_Format:
  447. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC6x6UnormSRGB : GPUTextureFormat.ASTC6x6Unorm;
  448. break;
  449. case RGBA_ASTC_8x5_Format:
  450. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC8x5UnormSRGB : GPUTextureFormat.ASTC8x5Unorm;
  451. break;
  452. case RGBA_ASTC_8x6_Format:
  453. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC8x6UnormSRGB : GPUTextureFormat.ASTC8x6Unorm;
  454. break;
  455. case RGBA_ASTC_8x8_Format:
  456. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC8x8UnormSRGB : GPUTextureFormat.ASTC8x8Unorm;
  457. break;
  458. case RGBA_ASTC_10x5_Format:
  459. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC10x5UnormSRGB : GPUTextureFormat.ASTC10x5Unorm;
  460. break;
  461. case RGBA_ASTC_10x6_Format:
  462. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC10x6UnormSRGB : GPUTextureFormat.ASTC10x6Unorm;
  463. break;
  464. case RGBA_ASTC_10x8_Format:
  465. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC10x8UnormSRGB : GPUTextureFormat.ASTC10x8Unorm;
  466. break;
  467. case RGBA_ASTC_10x10_Format:
  468. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC10x10UnormSRGB : GPUTextureFormat.ASTC10x10Unorm;
  469. break;
  470. case RGBA_ASTC_12x10_Format:
  471. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC12x10UnormSRGB : GPUTextureFormat.ASTC12x10Unorm;
  472. break;
  473. case RGBA_ASTC_12x12_Format:
  474. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC12x12UnormSRGB : GPUTextureFormat.ASTC12x12Unorm;
  475. break;
  476. default:
  477. console.error( 'WebGPURenderer: Unsupported texture format.', format );
  478. }
  479. } else {
  480. switch ( format ) {
  481. case RGBAFormat:
  482. switch ( type ) {
  483. case UnsignedByteType:
  484. formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.RGBA8UnormSRGB : GPUTextureFormat.RGBA8Unorm;
  485. break;
  486. case HalfFloatType:
  487. formatGPU = GPUTextureFormat.RGBA16Float;
  488. break;
  489. case FloatType:
  490. formatGPU = GPUTextureFormat.RGBA32Float;
  491. break;
  492. default:
  493. console.error( 'WebGPURenderer: Unsupported texture type with RGBAFormat.', type );
  494. }
  495. break;
  496. case RedFormat:
  497. switch ( type ) {
  498. case UnsignedByteType:
  499. formatGPU = GPUTextureFormat.R8Unorm;
  500. break;
  501. case HalfFloatType:
  502. formatGPU = GPUTextureFormat.R16Float;
  503. break;
  504. case FloatType:
  505. formatGPU = GPUTextureFormat.R32Float;
  506. break;
  507. default:
  508. console.error( 'WebGPURenderer: Unsupported texture type with RedFormat.', type );
  509. }
  510. break;
  511. case RGFormat:
  512. switch ( type ) {
  513. case UnsignedByteType:
  514. formatGPU = GPUTextureFormat.RG8Unorm;
  515. break;
  516. case HalfFloatType:
  517. formatGPU = GPUTextureFormat.RG16Float;
  518. break;
  519. case FloatType:
  520. formatGPU = GPUTextureFormat.RG32Float;
  521. break;
  522. default:
  523. console.error( 'WebGPURenderer: Unsupported texture type with RGFormat.', type );
  524. }
  525. break;
  526. case DepthFormat:
  527. switch ( type ) {
  528. case UnsignedShortType:
  529. formatGPU = GPUTextureFormat.Depth16Unorm;
  530. break;
  531. case UnsignedIntType:
  532. formatGPU = GPUTextureFormat.Depth24Plus;
  533. break;
  534. case FloatType:
  535. formatGPU = GPUTextureFormat.Depth32Float;
  536. break;
  537. default:
  538. console.error( 'WebGPURenderer: Unsupported texture type with DepthFormat.', type );
  539. }
  540. break;
  541. case DepthStencilFormat:
  542. switch ( type ) {
  543. case UnsignedInt248Type:
  544. formatGPU = GPUTextureFormat.Depth24PlusStencil8;
  545. break;
  546. case FloatType:
  547. if ( this.device.features.has( GPUFeatureName.Depth32FloatStencil8 ) === false ) {
  548. console.error( 'WebGPURenderer: Depth textures with DepthStencilFormat + FloatType can only be used with the "depth32float-stencil8" GPU feature.' );
  549. }
  550. formatGPU = GPUTextureFormat.Depth32FloatStencil8;
  551. break;
  552. default:
  553. console.error( 'WebGPURenderer: Unsupported texture type with DepthStencilFormat.', type );
  554. }
  555. break;
  556. default:
  557. console.error( 'WebGPURenderer: Unsupported texture format.', format );
  558. }
  559. }
  560. return formatGPU;
  561. }
  562. _isHTMLImage( image ) {
  563. return ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) || ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement );
  564. }
  565. _copyImageToTexture( image, texture, textureGPU, textureGPUDescriptor, needsMipmaps, originDepth ) {
  566. if ( this._isHTMLImage( image ) ) {
  567. this._getImageBitmapFromHTML( image, texture ).then( imageBitmap => {
  568. this._copyExternalImageToTexture( imageBitmap, textureGPU, textureGPUDescriptor, needsMipmaps, originDepth );
  569. } );
  570. } else {
  571. // assume ImageBitmap
  572. this._copyExternalImageToTexture( image, textureGPU, textureGPUDescriptor, needsMipmaps, originDepth );
  573. }
  574. }
  575. _getImageBitmapFromHTML( image, texture ) {
  576. const width = image.width;
  577. const height = image.height;
  578. const options = {};
  579. options.imageOrientation = ( texture.flipY === true ) ? 'flipY' : 'none';
  580. options.premultiplyAlpha = ( texture.premultiplyAlpha === true ) ? 'premultiply' : 'default';
  581. return createImageBitmap( image, 0, 0, width, height, options );
  582. }
  583. _getImageBitmap( image, texture ) {
  584. const width = image.width;
  585. const height = image.height;
  586. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  587. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ) {
  588. const options = {};
  589. options.imageOrientation = ( texture.flipY === true ) ? 'flipY' : 'none';
  590. options.premultiplyAlpha = ( texture.premultiplyAlpha === true ) ? 'premultiply' : 'default';
  591. return createImageBitmap( image, 0, 0, width, height, options );
  592. } else {
  593. // assume ImageBitmap
  594. return Promise.resolve( image );
  595. }
  596. }
  597. _getMipLevelCount( texture, width, height, needsMipmaps ) {
  598. let mipLevelCount;
  599. if ( texture.isCompressedTexture ) {
  600. mipLevelCount = texture.mipmaps.length;
  601. } else if ( needsMipmaps ) {
  602. mipLevelCount = Math.floor( Math.log2( Math.max( width, height ) ) ) + 1;
  603. } else {
  604. mipLevelCount = 1; // a texture without mipmaps has a base mip (mipLevel 0)
  605. }
  606. return mipLevelCount;
  607. }
  608. _getSize( texture ) {
  609. const image = texture.image;
  610. let width, height, depth;
  611. if ( texture.isCubeTexture ) {
  612. const faceImage = image.length > 0 ? image[ 0 ].image || image[ 0 ] : null;
  613. width = faceImage ? faceImage.width : 1;
  614. height = faceImage ? faceImage.height : 1;
  615. depth = 6; // one image for each side of the cube map
  616. } else if ( image !== null ) {
  617. width = image.width;
  618. height = image.height;
  619. depth = ( image.depth !== undefined ) ? image.depth : 1;
  620. } else {
  621. width = height = depth = 1;
  622. }
  623. return { width, height, depth };
  624. }
  625. _needsMipmaps( texture ) {
  626. return ( texture.isCompressedTexture !== true ) && ( texture.generateMipmaps === true ) && ( texture.minFilter !== NearestFilter ) && ( texture.minFilter !== LinearFilter );
  627. }
  628. }
  629. function onRenderTargetDispose( event ) {
  630. const renderTarget = event.target;
  631. const properties = this.properties;
  632. const renderTargetProperties = properties.get( renderTarget );
  633. renderTarget.removeEventListener( 'dispose', renderTargetProperties.disposeCallback );
  634. renderTargetProperties.colorTextureGPU.destroy();
  635. properties.remove( renderTarget.texture );
  636. this.info.memory.textures --;
  637. if ( renderTarget.depthBuffer === true ) {
  638. renderTargetProperties.depthTextureGPU.destroy();
  639. this.info.memory.textures --;
  640. if ( renderTarget.depthTexture !== null ) {
  641. properties.remove( renderTarget.depthTexture );
  642. }
  643. }
  644. properties.remove( renderTarget );
  645. }
  646. function onTextureDispose( event ) {
  647. const texture = event.target;
  648. const textureProperties = this.properties.get( texture );
  649. textureProperties.textureGPU.destroy();
  650. texture.removeEventListener( 'dispose', textureProperties.disposeCallback );
  651. this.properties.remove( texture );
  652. this.info.memory.textures --;
  653. }
  654. export default WebGPUTextures;