WebGPUTextureUtils.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  1. import {
  2. GPUTextureFormat, GPUAddressMode, GPUFilterMode, GPUTextureDimension, GPUFeatureName, GPUTextureViewDimension
  3. } from './WebGPUConstants.js';
  4. import { ColorManagement } from '../../../math/ColorManagement.js';
  5. import WebGPUTexturePassUtils from './WebGPUTexturePassUtils.js';
  6. import {
  7. ByteType, ShortType,
  8. NearestFilter, NearestMipmapNearestFilter, NearestMipmapLinearFilter,
  9. RepeatWrapping, MirroredRepeatWrapping,
  10. RGB_ETC2_Format, RGBA_ETC2_EAC_Format,
  11. RGBAFormat, RGBFormat, RedFormat, RGFormat, RGBA_S3TC_DXT1_Format, RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT5_Format, UnsignedByteType, FloatType, HalfFloatType, SRGBTransfer, DepthFormat, DepthStencilFormat,
  12. 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,
  13. RGBA_ASTC_10x6_Format, RGBA_ASTC_10x8_Format, RGBA_ASTC_10x10_Format, RGBA_ASTC_12x10_Format, RGBA_ASTC_12x12_Format, UnsignedIntType, UnsignedShortType, UnsignedInt248Type, UnsignedInt5999Type,
  14. NeverCompare, AlwaysCompare, LessCompare, LessEqualCompare, EqualCompare, GreaterEqualCompare, GreaterCompare, NotEqualCompare, IntType, RedIntegerFormat, RGIntegerFormat, RGBAIntegerFormat,
  15. UnsignedInt101111Type
  16. } from '../../../constants.js';
  17. import { CubeTexture } from '../../../textures/CubeTexture.js';
  18. import { DepthTexture } from '../../../textures/DepthTexture.js';
  19. import { Texture } from '../../../textures/Texture.js';
  20. const _compareToWebGPU = {
  21. [ NeverCompare ]: 'never',
  22. [ LessCompare ]: 'less',
  23. [ EqualCompare ]: 'equal',
  24. [ LessEqualCompare ]: 'less-equal',
  25. [ GreaterCompare ]: 'greater',
  26. [ GreaterEqualCompare ]: 'greater-equal',
  27. [ AlwaysCompare ]: 'always',
  28. [ NotEqualCompare ]: 'not-equal'
  29. };
  30. const _flipMap = [ 0, 1, 3, 2, 4, 5 ];
  31. /**
  32. * A WebGPU backend utility module for managing textures.
  33. *
  34. * @private
  35. */
  36. class WebGPUTextureUtils {
  37. /**
  38. * Constructs a new utility object.
  39. *
  40. * @param {WebGPUBackend} backend - The WebGPU backend.
  41. */
  42. constructor( backend ) {
  43. /**
  44. * A reference to the WebGPU backend.
  45. *
  46. * @type {WebGPUBackend}
  47. */
  48. this.backend = backend;
  49. /**
  50. * A reference to the pass utils.
  51. *
  52. * @type {?WebGPUTexturePassUtils}
  53. * @default null
  54. */
  55. this._passUtils = null;
  56. /**
  57. * A dictionary for managing default textures. The key
  58. * is the texture format, the value the texture object.
  59. *
  60. * @type {Object<string,Texture>}
  61. */
  62. this.defaultTexture = {};
  63. /**
  64. * A dictionary for managing default cube textures. The key
  65. * is the texture format, the value the texture object.
  66. *
  67. * @type {Object<string,CubeTexture>}
  68. */
  69. this.defaultCubeTexture = {};
  70. /**
  71. * A default video frame.
  72. *
  73. * @type {?VideoFrame}
  74. * @default null
  75. */
  76. this.defaultVideoFrame = null;
  77. /**
  78. * Represents the color attachment of the default framebuffer.
  79. *
  80. * @type {?GPUTexture}
  81. * @default null
  82. */
  83. this.colorBuffer = null;
  84. /**
  85. * Represents the depth attachment of the default framebuffer.
  86. *
  87. * @type {DepthTexture}
  88. */
  89. this.depthTexture = new DepthTexture();
  90. this.depthTexture.name = 'depthBuffer';
  91. }
  92. /**
  93. * Creates a GPU sampler for the given texture.
  94. *
  95. * @param {Texture} texture - The texture to create the sampler for.
  96. */
  97. createSampler( texture ) {
  98. const backend = this.backend;
  99. const device = backend.device;
  100. const textureGPU = backend.get( texture );
  101. const samplerDescriptorGPU = {
  102. addressModeU: this._convertAddressMode( texture.wrapS ),
  103. addressModeV: this._convertAddressMode( texture.wrapT ),
  104. addressModeW: this._convertAddressMode( texture.wrapR ),
  105. magFilter: this._convertFilterMode( texture.magFilter ),
  106. minFilter: this._convertFilterMode( texture.minFilter ),
  107. mipmapFilter: this._convertFilterMode( texture.minFilter ),
  108. maxAnisotropy: 1
  109. };
  110. // anisotropy can only be used when all filter modes are set to linear.
  111. if ( samplerDescriptorGPU.magFilter === GPUFilterMode.Linear && samplerDescriptorGPU.minFilter === GPUFilterMode.Linear && samplerDescriptorGPU.mipmapFilter === GPUFilterMode.Linear ) {
  112. samplerDescriptorGPU.maxAnisotropy = texture.anisotropy;
  113. }
  114. if ( texture.isDepthTexture && texture.compareFunction !== null ) {
  115. samplerDescriptorGPU.compare = _compareToWebGPU[ texture.compareFunction ];
  116. }
  117. textureGPU.sampler = device.createSampler( samplerDescriptorGPU );
  118. }
  119. /**
  120. * Creates a default texture for the given texture that can be used
  121. * as a placeholder until the actual texture is ready for usage.
  122. *
  123. * @param {Texture} texture - The texture to create a default texture for.
  124. */
  125. createDefaultTexture( texture ) {
  126. let textureGPU;
  127. const format = getFormat( texture );
  128. if ( texture.isCubeTexture ) {
  129. textureGPU = this._getDefaultCubeTextureGPU( format );
  130. } else {
  131. textureGPU = this._getDefaultTextureGPU( format );
  132. }
  133. this.backend.get( texture ).texture = textureGPU;
  134. }
  135. /**
  136. * Defines a texture on the GPU for the given texture object.
  137. *
  138. * @param {Texture} texture - The texture.
  139. * @param {Object} [options={}] - Optional configuration parameter.
  140. */
  141. createTexture( texture, options = {} ) {
  142. const backend = this.backend;
  143. const textureData = backend.get( texture );
  144. if ( textureData.initialized ) {
  145. throw new Error( 'WebGPUTextureUtils: Texture already initialized.' );
  146. }
  147. if ( texture.isExternalTexture ) {
  148. textureData.texture = texture.sourceTexture;
  149. textureData.initialized = true;
  150. return;
  151. }
  152. if ( options.needsMipmaps === undefined ) options.needsMipmaps = false;
  153. if ( options.levels === undefined ) options.levels = 1;
  154. if ( options.depth === undefined ) options.depth = 1;
  155. const { width, height, depth, levels } = options;
  156. if ( texture.isFramebufferTexture ) {
  157. if ( options.renderTarget ) {
  158. options.format = this.backend.utils.getCurrentColorFormat( options.renderTarget );
  159. } else {
  160. options.format = this.backend.utils.getPreferredCanvasFormat();
  161. }
  162. }
  163. const dimension = this._getDimension( texture );
  164. const format = texture.internalFormat || options.format || getFormat( texture, backend.device );
  165. textureData.format = format;
  166. const { samples, primarySamples, isMSAA } = backend.utils.getTextureSampleData( texture );
  167. let usage = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.COPY_SRC;
  168. if ( texture.isStorageTexture === true ) {
  169. usage |= GPUTextureUsage.STORAGE_BINDING;
  170. }
  171. if ( texture.isCompressedTexture !== true && texture.isCompressedArrayTexture !== true ) {
  172. usage |= GPUTextureUsage.RENDER_ATTACHMENT;
  173. }
  174. const textureDescriptorGPU = {
  175. label: texture.name,
  176. size: {
  177. width: width,
  178. height: height,
  179. depthOrArrayLayers: depth,
  180. },
  181. mipLevelCount: levels,
  182. sampleCount: primarySamples,
  183. dimension: dimension,
  184. format: format,
  185. usage: usage
  186. };
  187. // texture creation
  188. if ( format === undefined ) {
  189. console.warn( 'WebGPURenderer: Texture format not supported.' );
  190. this.createDefaultTexture( texture );
  191. return;
  192. }
  193. if ( texture.isCubeTexture ) {
  194. textureDescriptorGPU.textureBindingViewDimension = GPUTextureViewDimension.Cube;
  195. }
  196. textureData.texture = backend.device.createTexture( textureDescriptorGPU );
  197. if ( isMSAA ) {
  198. const msaaTextureDescriptorGPU = Object.assign( {}, textureDescriptorGPU );
  199. msaaTextureDescriptorGPU.label = msaaTextureDescriptorGPU.label + '-msaa';
  200. msaaTextureDescriptorGPU.sampleCount = samples;
  201. msaaTextureDescriptorGPU.mipLevelCount = 1; // See https://www.w3.org/TR/webgpu/#texture-creation
  202. textureData.msaaTexture = backend.device.createTexture( msaaTextureDescriptorGPU );
  203. }
  204. textureData.initialized = true;
  205. textureData.textureDescriptorGPU = textureDescriptorGPU;
  206. }
  207. /**
  208. * Destroys the GPU data for the given texture object.
  209. *
  210. * @param {Texture} texture - The texture.
  211. */
  212. destroyTexture( texture ) {
  213. const backend = this.backend;
  214. const textureData = backend.get( texture );
  215. if ( textureData.texture !== undefined ) textureData.texture.destroy();
  216. if ( textureData.msaaTexture !== undefined ) textureData.msaaTexture.destroy();
  217. backend.delete( texture );
  218. }
  219. /**
  220. * Destroys the GPU sampler for the given texture.
  221. *
  222. * @param {Texture} texture - The texture to destroy the sampler for.
  223. */
  224. destroySampler( texture ) {
  225. const backend = this.backend;
  226. const textureData = backend.get( texture );
  227. delete textureData.sampler;
  228. }
  229. /**
  230. * Generates mipmaps for the given texture.
  231. *
  232. * @param {Texture} texture - The texture.
  233. */
  234. generateMipmaps( texture ) {
  235. const textureData = this.backend.get( texture );
  236. if ( texture.isCubeTexture ) {
  237. for ( let i = 0; i < 6; i ++ ) {
  238. this._generateMipmaps( textureData.texture, textureData.textureDescriptorGPU, i );
  239. }
  240. } else {
  241. const depth = texture.image.depth || 1;
  242. for ( let i = 0; i < depth; i ++ ) {
  243. this._generateMipmaps( textureData.texture, textureData.textureDescriptorGPU, i );
  244. }
  245. }
  246. }
  247. /**
  248. * Returns the color buffer representing the color
  249. * attachment of the default framebuffer.
  250. *
  251. * @return {GPUTexture} The color buffer.
  252. */
  253. getColorBuffer() {
  254. if ( this.colorBuffer ) this.colorBuffer.destroy();
  255. const backend = this.backend;
  256. const { width, height } = backend.getDrawingBufferSize();
  257. this.colorBuffer = backend.device.createTexture( {
  258. label: 'colorBuffer',
  259. size: {
  260. width: width,
  261. height: height,
  262. depthOrArrayLayers: 1
  263. },
  264. sampleCount: backend.utils.getSampleCount( backend.renderer.samples ),
  265. format: backend.utils.getPreferredCanvasFormat(),
  266. usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC
  267. } );
  268. return this.colorBuffer;
  269. }
  270. /**
  271. * Returns the depth buffer representing the depth
  272. * attachment of the default framebuffer.
  273. *
  274. * @param {boolean} [depth=true] - Whether depth is enabled or not.
  275. * @param {boolean} [stencil=false] - Whether stencil is enabled or not.
  276. * @return {GPUTexture} The depth buffer.
  277. */
  278. getDepthBuffer( depth = true, stencil = false ) {
  279. const backend = this.backend;
  280. const { width, height } = backend.getDrawingBufferSize();
  281. const depthTexture = this.depthTexture;
  282. const depthTextureGPU = backend.get( depthTexture ).texture;
  283. let format, type;
  284. if ( stencil ) {
  285. format = DepthStencilFormat;
  286. type = UnsignedInt248Type;
  287. } else if ( depth ) {
  288. format = DepthFormat;
  289. type = UnsignedIntType;
  290. }
  291. if ( depthTextureGPU !== undefined ) {
  292. if ( depthTexture.image.width === width && depthTexture.image.height === height && depthTexture.format === format && depthTexture.type === type ) {
  293. return depthTextureGPU;
  294. }
  295. this.destroyTexture( depthTexture );
  296. }
  297. depthTexture.name = 'depthBuffer';
  298. depthTexture.format = format;
  299. depthTexture.type = type;
  300. depthTexture.image.width = width;
  301. depthTexture.image.height = height;
  302. this.createTexture( depthTexture, { width, height } );
  303. return backend.get( depthTexture ).texture;
  304. }
  305. /**
  306. * Uploads the updated texture data to the GPU.
  307. *
  308. * @param {Texture} texture - The texture.
  309. * @param {Object} [options={}] - Optional configuration parameter.
  310. */
  311. updateTexture( texture, options ) {
  312. const textureData = this.backend.get( texture );
  313. const mipmaps = texture.mipmaps;
  314. const { textureDescriptorGPU } = textureData;
  315. if ( texture.isRenderTargetTexture || ( textureDescriptorGPU === undefined /* unsupported texture format */ ) )
  316. return;
  317. // transfer texture data
  318. if ( texture.isDataTexture ) {
  319. if ( mipmaps.length > 0 ) {
  320. for ( let i = 0, il = mipmaps.length; i < il; i ++ ) {
  321. const mipmap = mipmaps[ i ];
  322. this._copyBufferToTexture( mipmap, textureData.texture, textureDescriptorGPU, 0, texture.flipY, 0, i );
  323. }
  324. } else {
  325. this._copyBufferToTexture( options.image, textureData.texture, textureDescriptorGPU, 0, texture.flipY );
  326. }
  327. } else if ( texture.isArrayTexture || texture.isDataArrayTexture || texture.isData3DTexture ) {
  328. for ( let i = 0; i < options.image.depth; i ++ ) {
  329. this._copyBufferToTexture( options.image, textureData.texture, textureDescriptorGPU, i, texture.flipY, i );
  330. }
  331. } else if ( texture.isCompressedTexture || texture.isCompressedArrayTexture ) {
  332. this._copyCompressedBufferToTexture( texture.mipmaps, textureData.texture, textureDescriptorGPU );
  333. } else if ( texture.isCubeTexture ) {
  334. this._copyCubeMapToTexture( texture, textureData.texture, textureDescriptorGPU );
  335. } else {
  336. if ( mipmaps.length > 0 ) {
  337. for ( let i = 0, il = mipmaps.length; i < il; i ++ ) {
  338. const mipmap = mipmaps[ i ];
  339. this._copyImageToTexture( mipmap, textureData.texture, textureDescriptorGPU, 0, texture.flipY, texture.premultiplyAlpha, i );
  340. }
  341. } else {
  342. this._copyImageToTexture( options.image, textureData.texture, textureDescriptorGPU, 0, texture.flipY, texture.premultiplyAlpha );
  343. }
  344. }
  345. //
  346. textureData.version = texture.version;
  347. if ( texture.onUpdate ) texture.onUpdate( texture );
  348. }
  349. /**
  350. * Returns texture data as a typed array.
  351. *
  352. * @async
  353. * @param {Texture} texture - The texture to copy.
  354. * @param {number} x - The x coordinate of the copy origin.
  355. * @param {number} y - The y coordinate of the copy origin.
  356. * @param {number} width - The width of the copy.
  357. * @param {number} height - The height of the copy.
  358. * @param {number} faceIndex - The face index.
  359. * @return {Promise<TypedArray>} A Promise that resolves with a typed array when the copy operation has finished.
  360. */
  361. async copyTextureToBuffer( texture, x, y, width, height, faceIndex ) {
  362. const device = this.backend.device;
  363. const textureData = this.backend.get( texture );
  364. const textureGPU = textureData.texture;
  365. const format = textureData.textureDescriptorGPU.format;
  366. const bytesPerTexel = this._getBytesPerTexel( format );
  367. let bytesPerRow = width * bytesPerTexel;
  368. bytesPerRow = Math.ceil( bytesPerRow / 256 ) * 256; // Align to 256 bytes
  369. const readBuffer = device.createBuffer(
  370. {
  371. size: width * height * bytesPerTexel,
  372. usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
  373. }
  374. );
  375. const encoder = device.createCommandEncoder();
  376. encoder.copyTextureToBuffer(
  377. {
  378. texture: textureGPU,
  379. origin: { x, y, z: faceIndex },
  380. },
  381. {
  382. buffer: readBuffer,
  383. bytesPerRow: bytesPerRow
  384. },
  385. {
  386. width: width,
  387. height: height
  388. }
  389. );
  390. const typedArrayType = this._getTypedArrayType( format );
  391. device.queue.submit( [ encoder.finish() ] );
  392. await readBuffer.mapAsync( GPUMapMode.READ );
  393. const buffer = readBuffer.getMappedRange();
  394. return new typedArrayType( buffer );
  395. }
  396. /**
  397. * Returns the default GPU texture for the given format.
  398. *
  399. * @private
  400. * @param {string} format - The GPU format.
  401. * @return {GPUTexture} The GPU texture.
  402. */
  403. _getDefaultTextureGPU( format ) {
  404. let defaultTexture = this.defaultTexture[ format ];
  405. if ( defaultTexture === undefined ) {
  406. const texture = new Texture();
  407. texture.minFilter = NearestFilter;
  408. texture.magFilter = NearestFilter;
  409. this.createTexture( texture, { width: 1, height: 1, format } );
  410. this.defaultTexture[ format ] = defaultTexture = texture;
  411. }
  412. return this.backend.get( defaultTexture ).texture;
  413. }
  414. /**
  415. * Returns the default GPU cube texture for the given format.
  416. *
  417. * @private
  418. * @param {string} format - The GPU format.
  419. * @return {GPUTexture} The GPU texture.
  420. */
  421. _getDefaultCubeTextureGPU( format ) {
  422. let defaultCubeTexture = this.defaultTexture[ format ];
  423. if ( defaultCubeTexture === undefined ) {
  424. const texture = new CubeTexture();
  425. texture.minFilter = NearestFilter;
  426. texture.magFilter = NearestFilter;
  427. this.createTexture( texture, { width: 1, height: 1, depth: 6 } );
  428. this.defaultCubeTexture[ format ] = defaultCubeTexture = texture;
  429. }
  430. return this.backend.get( defaultCubeTexture ).texture;
  431. }
  432. /**
  433. * Uploads cube texture image data to the GPU memory.
  434. *
  435. * @private
  436. * @param {CubeTexture} texture - The cube texture.
  437. * @param {GPUTexture} textureGPU - The GPU texture.
  438. * @param {Object} textureDescriptorGPU - The GPU texture descriptor.
  439. */
  440. _copyCubeMapToTexture( texture, textureGPU, textureDescriptorGPU ) {
  441. const images = texture.images;
  442. const mipmaps = texture.mipmaps;
  443. for ( let i = 0; i < 6; i ++ ) {
  444. const image = images[ i ];
  445. const flipIndex = texture.flipY === true ? _flipMap[ i ] : i;
  446. if ( image.isDataTexture ) {
  447. this._copyBufferToTexture( image.image, textureGPU, textureDescriptorGPU, flipIndex, texture.flipY );
  448. } else {
  449. this._copyImageToTexture( image, textureGPU, textureDescriptorGPU, flipIndex, texture.flipY, texture.premultiplyAlpha );
  450. }
  451. for ( let j = 0; j < mipmaps.length; j ++ ) {
  452. const mipmap = mipmaps[ j ];
  453. const image = mipmap.images[ i ];
  454. if ( image.isDataTexture ) {
  455. this._copyBufferToTexture( image.image, textureGPU, textureDescriptorGPU, flipIndex, texture.flipY, 0, j + 1 );
  456. } else {
  457. this._copyImageToTexture( image, textureGPU, textureDescriptorGPU, flipIndex, texture.flipY, texture.premultiplyAlpha, j + 1 );
  458. }
  459. }
  460. }
  461. }
  462. /**
  463. * Uploads texture image data to the GPU memory.
  464. *
  465. * @private
  466. * @param {HTMLImageElement|ImageBitmap|HTMLCanvasElement} image - The image data.
  467. * @param {GPUTexture} textureGPU - The GPU texture.
  468. * @param {Object} textureDescriptorGPU - The GPU texture descriptor.
  469. * @param {number} originDepth - The origin depth.
  470. * @param {boolean} flipY - Whether to flip texture data along their vertical axis or not.
  471. * @param {boolean} premultiplyAlpha - Whether the texture should have its RGB channels premultiplied by the alpha channel or not.
  472. * @param {number} [mipLevel=0] - The mip level where the data should be copied to.
  473. */
  474. _copyImageToTexture( image, textureGPU, textureDescriptorGPU, originDepth, flipY, premultiplyAlpha, mipLevel = 0 ) {
  475. const device = this.backend.device;
  476. const width = ( mipLevel > 0 ) ? image.width : textureDescriptorGPU.size.width;
  477. const height = ( mipLevel > 0 ) ? image.height : textureDescriptorGPU.size.height;
  478. device.queue.copyExternalImageToTexture(
  479. {
  480. source: image,
  481. flipY: flipY
  482. }, {
  483. texture: textureGPU,
  484. mipLevel: mipLevel,
  485. origin: { x: 0, y: 0, z: originDepth },
  486. premultipliedAlpha: premultiplyAlpha
  487. }, {
  488. width: width,
  489. height: height,
  490. depthOrArrayLayers: 1
  491. }
  492. );
  493. }
  494. /**
  495. * Returns the pass utils singleton.
  496. *
  497. * @private
  498. * @return {WebGPUTexturePassUtils} The utils instance.
  499. */
  500. _getPassUtils() {
  501. let passUtils = this._passUtils;
  502. if ( passUtils === null ) {
  503. this._passUtils = passUtils = new WebGPUTexturePassUtils( this.backend.device );
  504. }
  505. return passUtils;
  506. }
  507. /**
  508. * Generates mipmaps for the given GPU texture.
  509. *
  510. * @private
  511. * @param {GPUTexture} textureGPU - The GPU texture object.
  512. * @param {Object} textureDescriptorGPU - The texture descriptor.
  513. * @param {number} [baseArrayLayer=0] - The index of the first array layer accessible to the texture view.
  514. */
  515. _generateMipmaps( textureGPU, textureDescriptorGPU, baseArrayLayer = 0 ) {
  516. this._getPassUtils().generateMipmaps( textureGPU, textureDescriptorGPU, baseArrayLayer );
  517. }
  518. /**
  519. * Flip the contents of the given GPU texture along its vertical axis.
  520. *
  521. * @private
  522. * @param {GPUTexture} textureGPU - The GPU texture object.
  523. * @param {Object} textureDescriptorGPU - The texture descriptor.
  524. * @param {number} [originDepth=0] - The origin depth.
  525. */
  526. _flipY( textureGPU, textureDescriptorGPU, originDepth = 0 ) {
  527. this._getPassUtils().flipY( textureGPU, textureDescriptorGPU, originDepth );
  528. }
  529. /**
  530. * Uploads texture buffer data to the GPU memory.
  531. *
  532. * @private
  533. * @param {Object} image - An object defining the image buffer data.
  534. * @param {GPUTexture} textureGPU - The GPU texture.
  535. * @param {Object} textureDescriptorGPU - The GPU texture descriptor.
  536. * @param {number} originDepth - The origin depth.
  537. * @param {boolean} flipY - Whether to flip texture data along their vertical axis or not.
  538. * @param {number} [depth=0] - The depth offset when copying array or 3D texture data.
  539. * @param {number} [mipLevel=0] - The mip level where the data should be copied to.
  540. */
  541. _copyBufferToTexture( image, textureGPU, textureDescriptorGPU, originDepth, flipY, depth = 0, mipLevel = 0 ) {
  542. // @TODO: Consider to use GPUCommandEncoder.copyBufferToTexture()
  543. // @TODO: Consider to support valid buffer layouts with other formats like RGB
  544. const device = this.backend.device;
  545. const data = image.data;
  546. const bytesPerTexel = this._getBytesPerTexel( textureDescriptorGPU.format );
  547. const bytesPerRow = image.width * bytesPerTexel;
  548. device.queue.writeTexture(
  549. {
  550. texture: textureGPU,
  551. mipLevel: mipLevel,
  552. origin: { x: 0, y: 0, z: originDepth }
  553. },
  554. data,
  555. {
  556. offset: image.width * image.height * bytesPerTexel * depth,
  557. bytesPerRow
  558. },
  559. {
  560. width: image.width,
  561. height: image.height,
  562. depthOrArrayLayers: 1
  563. } );
  564. if ( flipY === true ) {
  565. this._flipY( textureGPU, textureDescriptorGPU, originDepth );
  566. }
  567. }
  568. /**
  569. * Uploads compressed texture data to the GPU memory.
  570. *
  571. * @private
  572. * @param {Array<Object>} mipmaps - An array with mipmap data.
  573. * @param {GPUTexture} textureGPU - The GPU texture.
  574. * @param {Object} textureDescriptorGPU - The GPU texture descriptor.
  575. */
  576. _copyCompressedBufferToTexture( mipmaps, textureGPU, textureDescriptorGPU ) {
  577. // @TODO: Consider to use GPUCommandEncoder.copyBufferToTexture()
  578. const device = this.backend.device;
  579. const blockData = this._getBlockData( textureDescriptorGPU.format );
  580. const isArrayTexture = textureDescriptorGPU.size.depthOrArrayLayers > 1;
  581. for ( let i = 0; i < mipmaps.length; i ++ ) {
  582. const mipmap = mipmaps[ i ];
  583. const width = mipmap.width;
  584. const height = mipmap.height;
  585. const depth = isArrayTexture ? textureDescriptorGPU.size.depthOrArrayLayers : 1;
  586. const bytesPerRow = Math.ceil( width / blockData.width ) * blockData.byteLength;
  587. const bytesPerImage = bytesPerRow * Math.ceil( height / blockData.height );
  588. for ( let j = 0; j < depth; j ++ ) {
  589. device.queue.writeTexture(
  590. {
  591. texture: textureGPU,
  592. mipLevel: i,
  593. origin: { x: 0, y: 0, z: j }
  594. },
  595. mipmap.data,
  596. {
  597. offset: j * bytesPerImage,
  598. bytesPerRow,
  599. rowsPerImage: Math.ceil( height / blockData.height )
  600. },
  601. {
  602. width: Math.ceil( width / blockData.width ) * blockData.width,
  603. height: Math.ceil( height / blockData.height ) * blockData.height,
  604. depthOrArrayLayers: 1
  605. }
  606. );
  607. }
  608. }
  609. }
  610. /**
  611. * This method is only relevant for compressed texture formats. It returns a block
  612. * data descriptor for the given GPU compressed texture format.
  613. *
  614. * @private
  615. * @param {string} format - The GPU compressed texture format.
  616. * @return {Object} The block data descriptor.
  617. */
  618. _getBlockData( format ) {
  619. if ( format === GPUTextureFormat.BC1RGBAUnorm || format === GPUTextureFormat.BC1RGBAUnormSRGB ) return { byteLength: 8, width: 4, height: 4 }; // DXT1
  620. if ( format === GPUTextureFormat.BC2RGBAUnorm || format === GPUTextureFormat.BC2RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // DXT3
  621. if ( format === GPUTextureFormat.BC3RGBAUnorm || format === GPUTextureFormat.BC3RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // DXT5
  622. if ( format === GPUTextureFormat.BC4RUnorm || format === GPUTextureFormat.BC4RSnorm ) return { byteLength: 8, width: 4, height: 4 }; // RGTC1
  623. if ( format === GPUTextureFormat.BC5RGUnorm || format === GPUTextureFormat.BC5RGSnorm ) return { byteLength: 16, width: 4, height: 4 }; // RGTC2
  624. if ( format === GPUTextureFormat.BC6HRGBUFloat || format === GPUTextureFormat.BC6HRGBFloat ) return { byteLength: 16, width: 4, height: 4 }; // BPTC (float)
  625. if ( format === GPUTextureFormat.BC7RGBAUnorm || format === GPUTextureFormat.BC7RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // BPTC (unorm)
  626. if ( format === GPUTextureFormat.ETC2RGB8Unorm || format === GPUTextureFormat.ETC2RGB8UnormSRGB ) return { byteLength: 8, width: 4, height: 4 };
  627. if ( format === GPUTextureFormat.ETC2RGB8A1Unorm || format === GPUTextureFormat.ETC2RGB8A1UnormSRGB ) return { byteLength: 8, width: 4, height: 4 };
  628. if ( format === GPUTextureFormat.ETC2RGBA8Unorm || format === GPUTextureFormat.ETC2RGBA8UnormSRGB ) return { byteLength: 16, width: 4, height: 4 };
  629. if ( format === GPUTextureFormat.EACR11Unorm ) return { byteLength: 8, width: 4, height: 4 };
  630. if ( format === GPUTextureFormat.EACR11Snorm ) return { byteLength: 8, width: 4, height: 4 };
  631. if ( format === GPUTextureFormat.EACRG11Unorm ) return { byteLength: 16, width: 4, height: 4 };
  632. if ( format === GPUTextureFormat.EACRG11Snorm ) return { byteLength: 16, width: 4, height: 4 };
  633. if ( format === GPUTextureFormat.ASTC4x4Unorm || format === GPUTextureFormat.ASTC4x4UnormSRGB ) return { byteLength: 16, width: 4, height: 4 };
  634. if ( format === GPUTextureFormat.ASTC5x4Unorm || format === GPUTextureFormat.ASTC5x4UnormSRGB ) return { byteLength: 16, width: 5, height: 4 };
  635. if ( format === GPUTextureFormat.ASTC5x5Unorm || format === GPUTextureFormat.ASTC5x5UnormSRGB ) return { byteLength: 16, width: 5, height: 5 };
  636. if ( format === GPUTextureFormat.ASTC6x5Unorm || format === GPUTextureFormat.ASTC6x5UnormSRGB ) return { byteLength: 16, width: 6, height: 5 };
  637. if ( format === GPUTextureFormat.ASTC6x6Unorm || format === GPUTextureFormat.ASTC6x6UnormSRGB ) return { byteLength: 16, width: 6, height: 6 };
  638. if ( format === GPUTextureFormat.ASTC8x5Unorm || format === GPUTextureFormat.ASTC8x5UnormSRGB ) return { byteLength: 16, width: 8, height: 5 };
  639. if ( format === GPUTextureFormat.ASTC8x6Unorm || format === GPUTextureFormat.ASTC8x6UnormSRGB ) return { byteLength: 16, width: 8, height: 6 };
  640. if ( format === GPUTextureFormat.ASTC8x8Unorm || format === GPUTextureFormat.ASTC8x8UnormSRGB ) return { byteLength: 16, width: 8, height: 8 };
  641. if ( format === GPUTextureFormat.ASTC10x5Unorm || format === GPUTextureFormat.ASTC10x5UnormSRGB ) return { byteLength: 16, width: 10, height: 5 };
  642. if ( format === GPUTextureFormat.ASTC10x6Unorm || format === GPUTextureFormat.ASTC10x6UnormSRGB ) return { byteLength: 16, width: 10, height: 6 };
  643. if ( format === GPUTextureFormat.ASTC10x8Unorm || format === GPUTextureFormat.ASTC10x8UnormSRGB ) return { byteLength: 16, width: 10, height: 8 };
  644. if ( format === GPUTextureFormat.ASTC10x10Unorm || format === GPUTextureFormat.ASTC10x10UnormSRGB ) return { byteLength: 16, width: 10, height: 10 };
  645. if ( format === GPUTextureFormat.ASTC12x10Unorm || format === GPUTextureFormat.ASTC12x10UnormSRGB ) return { byteLength: 16, width: 12, height: 10 };
  646. if ( format === GPUTextureFormat.ASTC12x12Unorm || format === GPUTextureFormat.ASTC12x12UnormSRGB ) return { byteLength: 16, width: 12, height: 12 };
  647. }
  648. /**
  649. * Converts the three.js uv wrapping constants to GPU address mode constants.
  650. *
  651. * @private
  652. * @param {number} value - The three.js constant defining a uv wrapping mode.
  653. * @return {string} The GPU address mode.
  654. */
  655. _convertAddressMode( value ) {
  656. let addressMode = GPUAddressMode.ClampToEdge;
  657. if ( value === RepeatWrapping ) {
  658. addressMode = GPUAddressMode.Repeat;
  659. } else if ( value === MirroredRepeatWrapping ) {
  660. addressMode = GPUAddressMode.MirrorRepeat;
  661. }
  662. return addressMode;
  663. }
  664. /**
  665. * Converts the three.js filter constants to GPU filter constants.
  666. *
  667. * @private
  668. * @param {number} value - The three.js constant defining a filter mode.
  669. * @return {string} The GPU filter mode.
  670. */
  671. _convertFilterMode( value ) {
  672. let filterMode = GPUFilterMode.Linear;
  673. if ( value === NearestFilter || value === NearestMipmapNearestFilter || value === NearestMipmapLinearFilter ) {
  674. filterMode = GPUFilterMode.Nearest;
  675. }
  676. return filterMode;
  677. }
  678. /**
  679. * Returns the bytes-per-texel value for the given GPU texture format.
  680. *
  681. * @private
  682. * @param {string} format - The GPU texture format.
  683. * @return {number} The bytes-per-texel.
  684. */
  685. _getBytesPerTexel( format ) {
  686. // 8-bit formats
  687. if ( format === GPUTextureFormat.R8Unorm ||
  688. format === GPUTextureFormat.R8Snorm ||
  689. format === GPUTextureFormat.R8Uint ||
  690. format === GPUTextureFormat.R8Sint ) return 1;
  691. // 16-bit formats
  692. if ( format === GPUTextureFormat.R16Uint ||
  693. format === GPUTextureFormat.R16Sint ||
  694. format === GPUTextureFormat.R16Float ||
  695. format === GPUTextureFormat.RG8Unorm ||
  696. format === GPUTextureFormat.RG8Snorm ||
  697. format === GPUTextureFormat.RG8Uint ||
  698. format === GPUTextureFormat.RG8Sint ) return 2;
  699. // 32-bit formats
  700. if ( format === GPUTextureFormat.R32Uint ||
  701. format === GPUTextureFormat.R32Sint ||
  702. format === GPUTextureFormat.R32Float ||
  703. format === GPUTextureFormat.RG16Uint ||
  704. format === GPUTextureFormat.RG16Sint ||
  705. format === GPUTextureFormat.RG16Float ||
  706. format === GPUTextureFormat.RGBA8Unorm ||
  707. format === GPUTextureFormat.RGBA8UnormSRGB ||
  708. format === GPUTextureFormat.RGBA8Snorm ||
  709. format === GPUTextureFormat.RGBA8Uint ||
  710. format === GPUTextureFormat.RGBA8Sint ||
  711. format === GPUTextureFormat.BGRA8Unorm ||
  712. format === GPUTextureFormat.BGRA8UnormSRGB ||
  713. // Packed 32-bit formats
  714. format === GPUTextureFormat.RGB9E5UFloat ||
  715. format === GPUTextureFormat.RGB10A2Unorm ||
  716. format === GPUTextureFormat.RG11B10UFloat ||
  717. format === GPUTextureFormat.Depth32Float ||
  718. format === GPUTextureFormat.Depth24Plus ||
  719. format === GPUTextureFormat.Depth24PlusStencil8 ||
  720. format === GPUTextureFormat.Depth32FloatStencil8 ) return 4;
  721. // 64-bit formats
  722. if ( format === GPUTextureFormat.RG32Uint ||
  723. format === GPUTextureFormat.RG32Sint ||
  724. format === GPUTextureFormat.RG32Float ||
  725. format === GPUTextureFormat.RGBA16Uint ||
  726. format === GPUTextureFormat.RGBA16Sint ||
  727. format === GPUTextureFormat.RGBA16Float ) return 8;
  728. // 128-bit formats
  729. if ( format === GPUTextureFormat.RGBA32Uint ||
  730. format === GPUTextureFormat.RGBA32Sint ||
  731. format === GPUTextureFormat.RGBA32Float ) return 16;
  732. }
  733. /**
  734. * Returns the corresponding typed array type for the given GPU texture format.
  735. *
  736. * @private
  737. * @param {string} format - The GPU texture format.
  738. * @return {TypedArray.constructor} The typed array type.
  739. */
  740. _getTypedArrayType( format ) {
  741. if ( format === GPUTextureFormat.R8Uint ) return Uint8Array;
  742. if ( format === GPUTextureFormat.R8Sint ) return Int8Array;
  743. if ( format === GPUTextureFormat.R8Unorm ) return Uint8Array;
  744. if ( format === GPUTextureFormat.R8Snorm ) return Int8Array;
  745. if ( format === GPUTextureFormat.RG8Uint ) return Uint8Array;
  746. if ( format === GPUTextureFormat.RG8Sint ) return Int8Array;
  747. if ( format === GPUTextureFormat.RG8Unorm ) return Uint8Array;
  748. if ( format === GPUTextureFormat.RG8Snorm ) return Int8Array;
  749. if ( format === GPUTextureFormat.RGBA8Uint ) return Uint8Array;
  750. if ( format === GPUTextureFormat.RGBA8Sint ) return Int8Array;
  751. if ( format === GPUTextureFormat.RGBA8Unorm || format === GPUTextureFormat.RGBA8UnormSRGB ) return Uint8Array;
  752. if ( format === GPUTextureFormat.RGBA8Snorm ) return Int8Array;
  753. if ( format === GPUTextureFormat.R16Uint ) return Uint16Array;
  754. if ( format === GPUTextureFormat.R16Sint ) return Int16Array;
  755. if ( format === GPUTextureFormat.RG16Uint ) return Uint16Array;
  756. if ( format === GPUTextureFormat.RG16Sint ) return Int16Array;
  757. if ( format === GPUTextureFormat.RGBA16Uint ) return Uint16Array;
  758. if ( format === GPUTextureFormat.RGBA16Sint ) return Int16Array;
  759. if ( format === GPUTextureFormat.R16Float ) return Uint16Array;
  760. if ( format === GPUTextureFormat.RG16Float ) return Uint16Array;
  761. if ( format === GPUTextureFormat.RGBA16Float ) return Uint16Array;
  762. if ( format === GPUTextureFormat.R32Uint ) return Uint32Array;
  763. if ( format === GPUTextureFormat.R32Sint ) return Int32Array;
  764. if ( format === GPUTextureFormat.R32Float ) return Float32Array;
  765. if ( format === GPUTextureFormat.RG32Uint ) return Uint32Array;
  766. if ( format === GPUTextureFormat.RG32Sint ) return Int32Array;
  767. if ( format === GPUTextureFormat.RG32Float ) return Float32Array;
  768. if ( format === GPUTextureFormat.RGBA32Uint ) return Uint32Array;
  769. if ( format === GPUTextureFormat.RGBA32Sint ) return Int32Array;
  770. if ( format === GPUTextureFormat.RGBA32Float ) return Float32Array;
  771. if ( format === GPUTextureFormat.BGRA8Unorm || format === GPUTextureFormat.BGRA8UnormSRGB ) return Uint8Array;
  772. if ( format === GPUTextureFormat.RGB10A2Unorm ) return Uint32Array;
  773. if ( format === GPUTextureFormat.RGB9E5UFloat ) return Uint32Array;
  774. if ( format === GPUTextureFormat.RG11B10UFloat ) return Uint32Array;
  775. if ( format === GPUTextureFormat.Depth32Float ) return Float32Array;
  776. if ( format === GPUTextureFormat.Depth24Plus ) return Uint32Array;
  777. if ( format === GPUTextureFormat.Depth24PlusStencil8 ) return Uint32Array;
  778. if ( format === GPUTextureFormat.Depth32FloatStencil8 ) return Float32Array;
  779. }
  780. /**
  781. * Returns the GPU dimensions for the given texture.
  782. *
  783. * @private
  784. * @param {Texture} texture - The texture.
  785. * @return {string} The GPU dimension.
  786. */
  787. _getDimension( texture ) {
  788. let dimension;
  789. if ( texture.is3DTexture || texture.isData3DTexture ) {
  790. dimension = GPUTextureDimension.ThreeD;
  791. } else {
  792. dimension = GPUTextureDimension.TwoD;
  793. }
  794. return dimension;
  795. }
  796. }
  797. /**
  798. * Returns the GPU format for the given texture.
  799. *
  800. * @param {Texture} texture - The texture.
  801. * @param {?GPUDevice} [device=null] - The GPU device which is used for feature detection.
  802. * It is not necessary to apply the device for most formats.
  803. * @return {string} The GPU format.
  804. */
  805. export function getFormat( texture, device = null ) {
  806. const format = texture.format;
  807. const type = texture.type;
  808. const colorSpace = texture.colorSpace;
  809. const transfer = ColorManagement.getTransfer( colorSpace );
  810. let formatGPU;
  811. if ( texture.isCompressedTexture === true || texture.isCompressedArrayTexture === true ) {
  812. switch ( format ) {
  813. case RGBA_S3TC_DXT1_Format:
  814. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.BC1RGBAUnormSRGB : GPUTextureFormat.BC1RGBAUnorm;
  815. break;
  816. case RGBA_S3TC_DXT3_Format:
  817. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.BC2RGBAUnormSRGB : GPUTextureFormat.BC2RGBAUnorm;
  818. break;
  819. case RGBA_S3TC_DXT5_Format:
  820. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.BC3RGBAUnormSRGB : GPUTextureFormat.BC3RGBAUnorm;
  821. break;
  822. case RGB_ETC2_Format:
  823. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.ETC2RGB8UnormSRGB : GPUTextureFormat.ETC2RGB8Unorm;
  824. break;
  825. case RGBA_ETC2_EAC_Format:
  826. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.ETC2RGBA8UnormSRGB : GPUTextureFormat.ETC2RGBA8Unorm;
  827. break;
  828. case RGBA_ASTC_4x4_Format:
  829. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.ASTC4x4UnormSRGB : GPUTextureFormat.ASTC4x4Unorm;
  830. break;
  831. case RGBA_ASTC_5x4_Format:
  832. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.ASTC5x4UnormSRGB : GPUTextureFormat.ASTC5x4Unorm;
  833. break;
  834. case RGBA_ASTC_5x5_Format:
  835. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.ASTC5x5UnormSRGB : GPUTextureFormat.ASTC5x5Unorm;
  836. break;
  837. case RGBA_ASTC_6x5_Format:
  838. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.ASTC6x5UnormSRGB : GPUTextureFormat.ASTC6x5Unorm;
  839. break;
  840. case RGBA_ASTC_6x6_Format:
  841. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.ASTC6x6UnormSRGB : GPUTextureFormat.ASTC6x6Unorm;
  842. break;
  843. case RGBA_ASTC_8x5_Format:
  844. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.ASTC8x5UnormSRGB : GPUTextureFormat.ASTC8x5Unorm;
  845. break;
  846. case RGBA_ASTC_8x6_Format:
  847. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.ASTC8x6UnormSRGB : GPUTextureFormat.ASTC8x6Unorm;
  848. break;
  849. case RGBA_ASTC_8x8_Format:
  850. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.ASTC8x8UnormSRGB : GPUTextureFormat.ASTC8x8Unorm;
  851. break;
  852. case RGBA_ASTC_10x5_Format:
  853. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.ASTC10x5UnormSRGB : GPUTextureFormat.ASTC10x5Unorm;
  854. break;
  855. case RGBA_ASTC_10x6_Format:
  856. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.ASTC10x6UnormSRGB : GPUTextureFormat.ASTC10x6Unorm;
  857. break;
  858. case RGBA_ASTC_10x8_Format:
  859. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.ASTC10x8UnormSRGB : GPUTextureFormat.ASTC10x8Unorm;
  860. break;
  861. case RGBA_ASTC_10x10_Format:
  862. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.ASTC10x10UnormSRGB : GPUTextureFormat.ASTC10x10Unorm;
  863. break;
  864. case RGBA_ASTC_12x10_Format:
  865. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.ASTC12x10UnormSRGB : GPUTextureFormat.ASTC12x10Unorm;
  866. break;
  867. case RGBA_ASTC_12x12_Format:
  868. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.ASTC12x12UnormSRGB : GPUTextureFormat.ASTC12x12Unorm;
  869. break;
  870. case RGBAFormat:
  871. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.RGBA8UnormSRGB : GPUTextureFormat.RGBA8Unorm;
  872. break;
  873. default:
  874. console.error( 'WebGPURenderer: Unsupported texture format.', format );
  875. }
  876. } else {
  877. switch ( format ) {
  878. case RGBAFormat:
  879. switch ( type ) {
  880. case ByteType:
  881. formatGPU = GPUTextureFormat.RGBA8Snorm;
  882. break;
  883. case ShortType:
  884. formatGPU = GPUTextureFormat.RGBA16Sint;
  885. break;
  886. case UnsignedShortType:
  887. formatGPU = GPUTextureFormat.RGBA16Uint;
  888. break;
  889. case UnsignedIntType:
  890. formatGPU = GPUTextureFormat.RGBA32Uint;
  891. break;
  892. case IntType:
  893. formatGPU = GPUTextureFormat.RGBA32Sint;
  894. break;
  895. case UnsignedByteType:
  896. formatGPU = ( transfer === SRGBTransfer ) ? GPUTextureFormat.RGBA8UnormSRGB : GPUTextureFormat.RGBA8Unorm;
  897. break;
  898. case HalfFloatType:
  899. formatGPU = GPUTextureFormat.RGBA16Float;
  900. break;
  901. case FloatType:
  902. formatGPU = GPUTextureFormat.RGBA32Float;
  903. break;
  904. default:
  905. console.error( 'WebGPURenderer: Unsupported texture type with RGBAFormat.', type );
  906. }
  907. break;
  908. case RGBFormat:
  909. switch ( type ) {
  910. case UnsignedInt5999Type:
  911. formatGPU = GPUTextureFormat.RGB9E5UFloat;
  912. break;
  913. case UnsignedInt101111Type:
  914. formatGPU = GPUTextureFormat.RG11B10UFloat;
  915. break;
  916. default:
  917. console.error( 'WebGPURenderer: Unsupported texture type with RGBFormat.', type );
  918. }
  919. break;
  920. case RedFormat:
  921. switch ( type ) {
  922. case ByteType:
  923. formatGPU = GPUTextureFormat.R8Snorm;
  924. break;
  925. case ShortType:
  926. formatGPU = GPUTextureFormat.R16Sint;
  927. break;
  928. case UnsignedShortType:
  929. formatGPU = GPUTextureFormat.R16Uint;
  930. break;
  931. case UnsignedIntType:
  932. formatGPU = GPUTextureFormat.R32Uint;
  933. break;
  934. case IntType:
  935. formatGPU = GPUTextureFormat.R32Sint;
  936. break;
  937. case UnsignedByteType:
  938. formatGPU = GPUTextureFormat.R8Unorm;
  939. break;
  940. case HalfFloatType:
  941. formatGPU = GPUTextureFormat.R16Float;
  942. break;
  943. case FloatType:
  944. formatGPU = GPUTextureFormat.R32Float;
  945. break;
  946. default:
  947. console.error( 'WebGPURenderer: Unsupported texture type with RedFormat.', type );
  948. }
  949. break;
  950. case RGFormat:
  951. switch ( type ) {
  952. case ByteType:
  953. formatGPU = GPUTextureFormat.RG8Snorm;
  954. break;
  955. case ShortType:
  956. formatGPU = GPUTextureFormat.RG16Sint;
  957. break;
  958. case UnsignedShortType:
  959. formatGPU = GPUTextureFormat.RG16Uint;
  960. break;
  961. case UnsignedIntType:
  962. formatGPU = GPUTextureFormat.RG32Uint;
  963. break;
  964. case IntType:
  965. formatGPU = GPUTextureFormat.RG32Sint;
  966. break;
  967. case UnsignedByteType:
  968. formatGPU = GPUTextureFormat.RG8Unorm;
  969. break;
  970. case HalfFloatType:
  971. formatGPU = GPUTextureFormat.RG16Float;
  972. break;
  973. case FloatType:
  974. formatGPU = GPUTextureFormat.RG32Float;
  975. break;
  976. default:
  977. console.error( 'WebGPURenderer: Unsupported texture type with RGFormat.', type );
  978. }
  979. break;
  980. case DepthFormat:
  981. switch ( type ) {
  982. case UnsignedShortType:
  983. formatGPU = GPUTextureFormat.Depth16Unorm;
  984. break;
  985. case UnsignedIntType:
  986. formatGPU = GPUTextureFormat.Depth24Plus;
  987. break;
  988. case FloatType:
  989. formatGPU = GPUTextureFormat.Depth32Float;
  990. break;
  991. default:
  992. console.error( 'WebGPURenderer: Unsupported texture type with DepthFormat.', type );
  993. }
  994. break;
  995. case DepthStencilFormat:
  996. switch ( type ) {
  997. case UnsignedInt248Type:
  998. formatGPU = GPUTextureFormat.Depth24PlusStencil8;
  999. break;
  1000. case FloatType:
  1001. if ( device && device.features.has( GPUFeatureName.Depth32FloatStencil8 ) === false ) {
  1002. console.error( 'WebGPURenderer: Depth textures with DepthStencilFormat + FloatType can only be used with the "depth32float-stencil8" GPU feature.' );
  1003. }
  1004. formatGPU = GPUTextureFormat.Depth32FloatStencil8;
  1005. break;
  1006. default:
  1007. console.error( 'WebGPURenderer: Unsupported texture type with DepthStencilFormat.', type );
  1008. }
  1009. break;
  1010. case RedIntegerFormat:
  1011. switch ( type ) {
  1012. case IntType:
  1013. formatGPU = GPUTextureFormat.R32Sint;
  1014. break;
  1015. case UnsignedIntType:
  1016. formatGPU = GPUTextureFormat.R32Uint;
  1017. break;
  1018. default:
  1019. console.error( 'WebGPURenderer: Unsupported texture type with RedIntegerFormat.', type );
  1020. }
  1021. break;
  1022. case RGIntegerFormat:
  1023. switch ( type ) {
  1024. case IntType:
  1025. formatGPU = GPUTextureFormat.RG32Sint;
  1026. break;
  1027. case UnsignedIntType:
  1028. formatGPU = GPUTextureFormat.RG32Uint;
  1029. break;
  1030. default:
  1031. console.error( 'WebGPURenderer: Unsupported texture type with RGIntegerFormat.', type );
  1032. }
  1033. break;
  1034. case RGBAIntegerFormat:
  1035. switch ( type ) {
  1036. case IntType:
  1037. formatGPU = GPUTextureFormat.RGBA32Sint;
  1038. break;
  1039. case UnsignedIntType:
  1040. formatGPU = GPUTextureFormat.RGBA32Uint;
  1041. break;
  1042. default:
  1043. console.error( 'WebGPURenderer: Unsupported texture type with RGBAIntegerFormat.', type );
  1044. }
  1045. break;
  1046. default:
  1047. console.error( 'WebGPURenderer: Unsupported texture format.', format );
  1048. }
  1049. }
  1050. return formatGPU;
  1051. }
  1052. export default WebGPUTextureUtils;
粤ICP备19079148号