KTX2Loader.js 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. import {
  2. CompressedArrayTexture,
  3. CompressedCubeTexture,
  4. CompressedTexture,
  5. Data3DTexture,
  6. DataTexture,
  7. FileLoader,
  8. FloatType,
  9. HalfFloatType,
  10. LinearFilter,
  11. LinearMipmapLinearFilter,
  12. NearestFilter,
  13. NearestMipmapNearestFilter,
  14. LinearSRGBColorSpace,
  15. Loader,
  16. NoColorSpace,
  17. RGBAFormat,
  18. RGBA_ASTC_4x4_Format,
  19. RGBA_ASTC_6x6_Format,
  20. RGBA_BPTC_Format,
  21. RGBA_S3TC_DXT3_Format,
  22. RGBA_ETC2_EAC_Format,
  23. RGBA_PVRTC_4BPPV1_Format,
  24. RGBA_S3TC_DXT1_Format,
  25. RGBA_S3TC_DXT5_Format,
  26. RGB_BPTC_UNSIGNED_Format,
  27. RGB_ETC1_Format,
  28. RGB_ETC2_Format,
  29. RGB_PVRTC_4BPPV1_Format,
  30. RGB_S3TC_DXT1_Format,
  31. RGBFormat,
  32. RGFormat,
  33. RedFormat,
  34. SRGBColorSpace,
  35. UnsignedByteType,
  36. UnsignedInt5999Type,
  37. UnsignedInt101111Type
  38. } from 'three';
  39. import { WorkerPool } from '../utils/WorkerPool.js';
  40. import {
  41. read,
  42. KHR_DF_FLAG_ALPHA_PREMULTIPLIED,
  43. KHR_DF_PRIMARIES_BT709,
  44. KHR_DF_PRIMARIES_DISPLAYP3,
  45. KHR_DF_PRIMARIES_UNSPECIFIED,
  46. KHR_DF_TRANSFER_SRGB,
  47. KHR_SUPERCOMPRESSION_NONE,
  48. KHR_SUPERCOMPRESSION_ZSTD,
  49. VK_FORMAT_ASTC_4x4_SFLOAT_BLOCK_EXT,
  50. VK_FORMAT_ASTC_4x4_SRGB_BLOCK,
  51. VK_FORMAT_ASTC_4x4_UNORM_BLOCK,
  52. VK_FORMAT_ASTC_6x6_SRGB_BLOCK,
  53. VK_FORMAT_ASTC_6x6_UNORM_BLOCK,
  54. VK_FORMAT_BC1_RGBA_SRGB_BLOCK,
  55. VK_FORMAT_BC1_RGBA_UNORM_BLOCK,
  56. VK_FORMAT_BC1_RGB_SRGB_BLOCK,
  57. VK_FORMAT_BC1_RGB_UNORM_BLOCK,
  58. VK_FORMAT_BC3_SRGB_BLOCK,
  59. VK_FORMAT_BC3_UNORM_BLOCK,
  60. VK_FORMAT_BC5_SNORM_BLOCK,
  61. VK_FORMAT_BC5_UNORM_BLOCK,
  62. VK_FORMAT_BC7_SRGB_BLOCK,
  63. VK_FORMAT_BC7_UNORM_BLOCK,
  64. VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK,
  65. VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK,
  66. VK_FORMAT_R16G16B16A16_SFLOAT,
  67. VK_FORMAT_R16G16_SFLOAT,
  68. VK_FORMAT_R16_SFLOAT,
  69. VK_FORMAT_R32G32B32A32_SFLOAT,
  70. VK_FORMAT_R32G32_SFLOAT,
  71. VK_FORMAT_R32_SFLOAT,
  72. VK_FORMAT_R8G8B8A8_SRGB,
  73. VK_FORMAT_R8G8B8A8_UNORM,
  74. VK_FORMAT_R8G8_SRGB,
  75. VK_FORMAT_R8G8_UNORM,
  76. VK_FORMAT_R8_SRGB,
  77. VK_FORMAT_R8_UNORM,
  78. VK_FORMAT_E5B9G9R9_UFLOAT_PACK32,
  79. VK_FORMAT_B10G11R11_UFLOAT_PACK32,
  80. VK_FORMAT_UNDEFINED
  81. } from '../libs/ktx-parse.module.js';
  82. import { ZSTDDecoder } from '../libs/zstddec.module.js';
  83. import { DisplayP3ColorSpace, LinearDisplayP3ColorSpace } from '../math/ColorSpaces.js';
  84. const _taskCache = new WeakMap();
  85. let _activeLoaders = 0;
  86. let _zstd;
  87. /**
  88. * A loader for KTX 2.0 GPU Texture containers.
  89. *
  90. * KTX 2.0 is a container format for various GPU texture formats. The loader supports Basis Universal GPU textures,
  91. * which can be quickly transcoded to a wide variety of GPU texture compression formats. While KTX 2.0 also allows
  92. * other hardware-specific formats, this loader does not yet parse them.
  93. *
  94. * This loader parses the KTX 2.0 container and transcodes to a supported GPU compressed texture format.
  95. * The required WASM transcoder and JS wrapper are available from the `examples/jsm/libs/basis` directory.
  96. *
  97. * This loader relies on Web Assembly which is not supported in older browsers.
  98. *
  99. * References:
  100. * - [KTX specification]{@link http://github.khronos.org/KTX-Specification/}
  101. * - [DFD]{@link https://www.khronos.org/registry/DataFormat/specs/1.3/dataformat.1.3.html#basicdescriptor}
  102. * - [BasisU HDR]{@link https://github.com/BinomialLLC/basis_universal/wiki/UASTC-HDR-Texture-Specification-v1.0}
  103. *
  104. * ```js
  105. * const loader = new KTX2Loader();
  106. * loader.setTranscoderPath( 'examples/jsm/libs/basis/' );
  107. * loader.detectSupport( renderer );
  108. * const texture = loader.loadAsync( 'diffuse.ktx2' );
  109. * ```
  110. *
  111. * @augments Loader
  112. * @three_import import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
  113. */
  114. class KTX2Loader extends Loader {
  115. /**
  116. * Constructs a new KTX2 loader.
  117. *
  118. * @param {LoadingManager} [manager] - The loading manager.
  119. */
  120. constructor( manager ) {
  121. super( manager );
  122. this.transcoderPath = '';
  123. this.transcoderBinary = null;
  124. this.transcoderPending = null;
  125. this.workerPool = new WorkerPool();
  126. this.workerSourceURL = '';
  127. this.workerConfig = null;
  128. if ( typeof MSC_TRANSCODER !== 'undefined' ) {
  129. console.warn(
  130. 'THREE.KTX2Loader: Please update to latest "basis_transcoder".'
  131. + ' "msc_basis_transcoder" is no longer supported in three.js r125+.'
  132. );
  133. }
  134. }
  135. /**
  136. * Sets the transcoder path.
  137. *
  138. * The WASM transcoder and JS wrapper are available from the `examples/jsm/libs/basis` directory.
  139. *
  140. * @param {string} path - The transcoder path to set.
  141. * @return {KTX2Loader} A reference to this loader.
  142. */
  143. setTranscoderPath( path ) {
  144. this.transcoderPath = path;
  145. return this;
  146. }
  147. /**
  148. * Sets the maximum number of Web Workers to be allocated by this instance.
  149. *
  150. * @param {number} workerLimit - The worker limit.
  151. * @return {KTX2Loader} A reference to this loader.
  152. */
  153. setWorkerLimit( workerLimit ) {
  154. this.workerPool.setWorkerLimit( workerLimit );
  155. return this;
  156. }
  157. /**
  158. * Async version of {@link KTX2Loader#detectSupport}.
  159. *
  160. * @async
  161. * @param {WebGPURenderer|WebGLRenderer} renderer - The renderer.
  162. * @return {Promise} A Promise that resolves when the support has been detected.
  163. */
  164. async detectSupportAsync( renderer ) {
  165. this.workerConfig = {
  166. astcSupported: await renderer.hasFeatureAsync( 'texture-compression-astc' ),
  167. astcHDRSupported: false, // https://github.com/gpuweb/gpuweb/issues/3856
  168. etc1Supported: await renderer.hasFeatureAsync( 'texture-compression-etc1' ),
  169. etc2Supported: await renderer.hasFeatureAsync( 'texture-compression-etc2' ),
  170. dxtSupported: await renderer.hasFeatureAsync( 'texture-compression-bc' ),
  171. bptcSupported: await renderer.hasFeatureAsync( 'texture-compression-bptc' ),
  172. pvrtcSupported: await renderer.hasFeatureAsync( 'texture-compression-pvrtc' )
  173. };
  174. return this;
  175. }
  176. /**
  177. * Detects hardware support for available compressed texture formats, to determine
  178. * the output format for the transcoder. Must be called before loading a texture.
  179. *
  180. * @param {WebGPURenderer|WebGLRenderer} renderer - The renderer.
  181. * @return {KTX2Loader} A reference to this loader.
  182. */
  183. detectSupport( renderer ) {
  184. if ( renderer.isWebGPURenderer === true ) {
  185. this.workerConfig = {
  186. astcSupported: renderer.hasFeature( 'texture-compression-astc' ),
  187. astcHDRSupported: false, // https://github.com/gpuweb/gpuweb/issues/3856
  188. etc1Supported: renderer.hasFeature( 'texture-compression-etc1' ),
  189. etc2Supported: renderer.hasFeature( 'texture-compression-etc2' ),
  190. dxtSupported: renderer.hasFeature( 'texture-compression-bc' ),
  191. bptcSupported: renderer.hasFeature( 'texture-compression-bptc' ),
  192. pvrtcSupported: renderer.hasFeature( 'texture-compression-pvrtc' )
  193. };
  194. } else {
  195. this.workerConfig = {
  196. astcSupported: renderer.extensions.has( 'WEBGL_compressed_texture_astc' ),
  197. astcHDRSupported: renderer.extensions.has( 'WEBGL_compressed_texture_astc' )
  198. && renderer.extensions.get( 'WEBGL_compressed_texture_astc' ).getSupportedProfiles().includes( 'hdr' ),
  199. etc1Supported: renderer.extensions.has( 'WEBGL_compressed_texture_etc1' ),
  200. etc2Supported: renderer.extensions.has( 'WEBGL_compressed_texture_etc' ),
  201. dxtSupported: renderer.extensions.has( 'WEBGL_compressed_texture_s3tc' ),
  202. bptcSupported: renderer.extensions.has( 'EXT_texture_compression_bptc' ),
  203. pvrtcSupported: renderer.extensions.has( 'WEBGL_compressed_texture_pvrtc' )
  204. || renderer.extensions.has( 'WEBKIT_WEBGL_compressed_texture_pvrtc' )
  205. };
  206. }
  207. return this;
  208. }
  209. // TODO: Make this method private
  210. init() {
  211. if ( ! this.transcoderPending ) {
  212. // Load transcoder wrapper.
  213. const jsLoader = new FileLoader( this.manager );
  214. jsLoader.setPath( this.transcoderPath );
  215. jsLoader.setWithCredentials( this.withCredentials );
  216. const jsContent = jsLoader.loadAsync( 'basis_transcoder.js' );
  217. // Load transcoder WASM binary.
  218. const binaryLoader = new FileLoader( this.manager );
  219. binaryLoader.setPath( this.transcoderPath );
  220. binaryLoader.setResponseType( 'arraybuffer' );
  221. binaryLoader.setWithCredentials( this.withCredentials );
  222. const binaryContent = binaryLoader.loadAsync( 'basis_transcoder.wasm' );
  223. this.transcoderPending = Promise.all( [ jsContent, binaryContent ] )
  224. .then( ( [ jsContent, binaryContent ] ) => {
  225. const fn = KTX2Loader.BasisWorker.toString();
  226. const body = [
  227. '/* constants */',
  228. 'let _EngineFormat = ' + JSON.stringify( KTX2Loader.EngineFormat ),
  229. 'let _EngineType = ' + JSON.stringify( KTX2Loader.EngineType ),
  230. 'let _TranscoderFormat = ' + JSON.stringify( KTX2Loader.TranscoderFormat ),
  231. 'let _BasisFormat = ' + JSON.stringify( KTX2Loader.BasisFormat ),
  232. '/* basis_transcoder.js */',
  233. jsContent,
  234. '/* worker */',
  235. fn.substring( fn.indexOf( '{' ) + 1, fn.lastIndexOf( '}' ) )
  236. ].join( '\n' );
  237. this.workerSourceURL = URL.createObjectURL( new Blob( [ body ] ) );
  238. this.transcoderBinary = binaryContent;
  239. this.workerPool.setWorkerCreator( () => {
  240. const worker = new Worker( this.workerSourceURL );
  241. const transcoderBinary = this.transcoderBinary.slice( 0 );
  242. worker.postMessage( { type: 'init', config: this.workerConfig, transcoderBinary }, [ transcoderBinary ] );
  243. return worker;
  244. } );
  245. } );
  246. if ( _activeLoaders > 0 ) {
  247. // Each instance loads a transcoder and allocates workers, increasing network and memory cost.
  248. console.warn(
  249. 'THREE.KTX2Loader: Multiple active KTX2 loaders may cause performance issues.'
  250. + ' Use a single KTX2Loader instance, or call .dispose() on old instances.'
  251. );
  252. }
  253. _activeLoaders ++;
  254. }
  255. return this.transcoderPending;
  256. }
  257. /**
  258. * Starts loading from the given URL and passes the loaded KTX2 texture
  259. * to the `onLoad()` callback.
  260. *
  261. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  262. * @param {function(CompressedTexture)} onLoad - Executed when the loading process has been finished.
  263. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  264. * @param {onErrorCallback} onError - Executed when errors occur.
  265. */
  266. load( url, onLoad, onProgress, onError ) {
  267. if ( this.workerConfig === null ) {
  268. throw new Error( 'THREE.KTX2Loader: Missing initialization with `.detectSupport( renderer )`.' );
  269. }
  270. const loader = new FileLoader( this.manager );
  271. loader.setPath( this.path );
  272. loader.setCrossOrigin( this.crossOrigin );
  273. loader.setWithCredentials( this.withCredentials );
  274. loader.setResponseType( 'arraybuffer' );
  275. loader.load( url, ( buffer ) => {
  276. this.parse( buffer, onLoad, onError );
  277. }, onProgress, onError );
  278. }
  279. /**
  280. * Parses the given KTX2 data.
  281. *
  282. * @param {ArrayBuffer} buffer - The raw KTX2 data as an array buffer.
  283. * @param {function(CompressedTexture)} onLoad - Executed when the loading/parsing process has been finished.
  284. * @param {onErrorCallback} onError - Executed when errors occur.
  285. * @returns {Promise} A Promise that resolves when the parsing has been finished.
  286. */
  287. parse( buffer, onLoad, onError ) {
  288. if ( this.workerConfig === null ) {
  289. throw new Error( 'THREE.KTX2Loader: Missing initialization with `.detectSupport( renderer )`.' );
  290. }
  291. // Check for an existing task using this buffer. A transferred buffer cannot be transferred
  292. // again from this thread.
  293. if ( _taskCache.has( buffer ) ) {
  294. const cachedTask = _taskCache.get( buffer );
  295. return cachedTask.promise.then( onLoad ).catch( onError );
  296. }
  297. this._createTexture( buffer )
  298. .then( ( texture ) => onLoad ? onLoad( texture ) : null )
  299. .catch( onError );
  300. }
  301. _createTextureFrom( transcodeResult, container ) {
  302. const { type: messageType, error, data: { faces, width, height, format, type, dfdFlags } } = transcodeResult;
  303. if ( messageType === 'error' ) return Promise.reject( error );
  304. let texture;
  305. if ( container.faceCount === 6 ) {
  306. texture = new CompressedCubeTexture( faces, format, type );
  307. } else {
  308. const mipmaps = faces[ 0 ].mipmaps;
  309. texture = container.layerCount > 1
  310. ? new CompressedArrayTexture( mipmaps, width, height, container.layerCount, format, type )
  311. : new CompressedTexture( mipmaps, width, height, format, type );
  312. }
  313. texture.minFilter = faces[ 0 ].mipmaps.length === 1 ? LinearFilter : LinearMipmapLinearFilter;
  314. texture.magFilter = LinearFilter;
  315. texture.generateMipmaps = false;
  316. texture.needsUpdate = true;
  317. texture.colorSpace = parseColorSpace( container );
  318. texture.premultiplyAlpha = !! ( dfdFlags & KHR_DF_FLAG_ALPHA_PREMULTIPLIED );
  319. return texture;
  320. }
  321. /**
  322. * @private
  323. * @param {ArrayBuffer} buffer
  324. * @param {?Object} config
  325. * @return {Promise<CompressedTexture|CompressedArrayTexture|DataTexture|Data3DTexture>}
  326. */
  327. async _createTexture( buffer, config = {} ) {
  328. const container = read( new Uint8Array( buffer ) );
  329. // Basis UASTC HDR is a subset of ASTC, which can be transcoded efficiently
  330. // to BC6H. To detect whether a KTX2 file uses Basis UASTC HDR, or default
  331. // ASTC, inspect the DFD color model.
  332. //
  333. // Source: https://github.com/BinomialLLC/basis_universal/issues/381
  334. const isBasisHDR = container.vkFormat === VK_FORMAT_ASTC_4x4_SFLOAT_BLOCK_EXT
  335. && container.dataFormatDescriptor[ 0 ].colorModel === 0xA7;
  336. // If the device supports ASTC, Basis UASTC HDR requires no transcoder.
  337. const needsTranscoder = container.vkFormat === VK_FORMAT_UNDEFINED
  338. || isBasisHDR && ! this.workerConfig.astcHDRSupported;
  339. if ( ! needsTranscoder ) {
  340. return createRawTexture( container );
  341. }
  342. //
  343. const taskConfig = config;
  344. const texturePending = this.init().then( () => {
  345. return this.workerPool.postMessage( { type: 'transcode', buffer, taskConfig: taskConfig }, [ buffer ] );
  346. } ).then( ( e ) => this._createTextureFrom( e.data, container ) );
  347. // Cache the task result.
  348. _taskCache.set( buffer, { promise: texturePending } );
  349. return texturePending;
  350. }
  351. /**
  352. * Frees internal resources. This method should be called
  353. * when the loader is no longer required.
  354. */
  355. dispose() {
  356. this.workerPool.dispose();
  357. if ( this.workerSourceURL ) URL.revokeObjectURL( this.workerSourceURL );
  358. _activeLoaders --;
  359. }
  360. }
  361. /* CONSTANTS */
  362. KTX2Loader.BasisFormat = {
  363. ETC1S: 0,
  364. UASTC: 1,
  365. UASTC_HDR: 2,
  366. };
  367. // Source: https://github.com/BinomialLLC/basis_universal/blob/master/webgl/texture_test/index.html
  368. KTX2Loader.TranscoderFormat = {
  369. ETC1: 0,
  370. ETC2: 1,
  371. BC1: 2,
  372. BC3: 3,
  373. BC4: 4,
  374. BC5: 5,
  375. BC7_M6_OPAQUE_ONLY: 6,
  376. BC7_M5: 7,
  377. PVRTC1_4_RGB: 8,
  378. PVRTC1_4_RGBA: 9,
  379. ASTC_4x4: 10,
  380. ATC_RGB: 11,
  381. ATC_RGBA_INTERPOLATED_ALPHA: 12,
  382. RGBA32: 13,
  383. RGB565: 14,
  384. BGR565: 15,
  385. RGBA4444: 16,
  386. BC6H: 22,
  387. RGB_HALF: 24,
  388. RGBA_HALF: 25,
  389. };
  390. KTX2Loader.EngineFormat = {
  391. RGBAFormat: RGBAFormat,
  392. RGBA_ASTC_4x4_Format: RGBA_ASTC_4x4_Format,
  393. RGB_BPTC_UNSIGNED_Format: RGB_BPTC_UNSIGNED_Format,
  394. RGBA_BPTC_Format: RGBA_BPTC_Format,
  395. RGBA_ETC2_EAC_Format: RGBA_ETC2_EAC_Format,
  396. RGBA_PVRTC_4BPPV1_Format: RGBA_PVRTC_4BPPV1_Format,
  397. RGBA_S3TC_DXT5_Format: RGBA_S3TC_DXT5_Format,
  398. RGB_ETC1_Format: RGB_ETC1_Format,
  399. RGB_ETC2_Format: RGB_ETC2_Format,
  400. RGB_PVRTC_4BPPV1_Format: RGB_PVRTC_4BPPV1_Format,
  401. RGBA_S3TC_DXT1_Format: RGBA_S3TC_DXT1_Format,
  402. };
  403. KTX2Loader.EngineType = {
  404. UnsignedByteType: UnsignedByteType,
  405. HalfFloatType: HalfFloatType,
  406. FloatType: FloatType,
  407. };
  408. /* WEB WORKER */
  409. KTX2Loader.BasisWorker = function () {
  410. let config;
  411. let transcoderPending;
  412. let BasisModule;
  413. const EngineFormat = _EngineFormat; // eslint-disable-line no-undef
  414. const EngineType = _EngineType; // eslint-disable-line no-undef
  415. const TranscoderFormat = _TranscoderFormat; // eslint-disable-line no-undef
  416. const BasisFormat = _BasisFormat; // eslint-disable-line no-undef
  417. self.addEventListener( 'message', function ( e ) {
  418. const message = e.data;
  419. switch ( message.type ) {
  420. case 'init':
  421. config = message.config;
  422. init( message.transcoderBinary );
  423. break;
  424. case 'transcode':
  425. transcoderPending.then( () => {
  426. try {
  427. const { faces, buffers, width, height, hasAlpha, format, type, dfdFlags } = transcode( message.buffer );
  428. self.postMessage( { type: 'transcode', id: message.id, data: { faces, width, height, hasAlpha, format, type, dfdFlags } }, buffers );
  429. } catch ( error ) {
  430. console.error( error );
  431. self.postMessage( { type: 'error', id: message.id, error: error.message } );
  432. }
  433. } );
  434. break;
  435. }
  436. } );
  437. function init( wasmBinary ) {
  438. transcoderPending = new Promise( ( resolve ) => {
  439. BasisModule = { wasmBinary, onRuntimeInitialized: resolve };
  440. BASIS( BasisModule ); // eslint-disable-line no-undef
  441. } ).then( () => {
  442. BasisModule.initializeBasis();
  443. if ( BasisModule.KTX2File === undefined ) {
  444. console.warn( 'THREE.KTX2Loader: Please update Basis Universal transcoder.' );
  445. }
  446. } );
  447. }
  448. function transcode( buffer ) {
  449. const ktx2File = new BasisModule.KTX2File( new Uint8Array( buffer ) );
  450. function cleanup() {
  451. ktx2File.close();
  452. ktx2File.delete();
  453. }
  454. if ( ! ktx2File.isValid() ) {
  455. cleanup();
  456. throw new Error( 'THREE.KTX2Loader: Invalid or unsupported .ktx2 file' );
  457. }
  458. let basisFormat;
  459. if ( ktx2File.isUASTC() ) {
  460. basisFormat = BasisFormat.UASTC;
  461. } else if ( ktx2File.isETC1S() ) {
  462. basisFormat = BasisFormat.ETC1S;
  463. } else if ( ktx2File.isHDR() ) {
  464. basisFormat = BasisFormat.UASTC_HDR;
  465. } else {
  466. throw new Error( 'THREE.KTX2Loader: Unknown Basis encoding' );
  467. }
  468. const width = ktx2File.getWidth();
  469. const height = ktx2File.getHeight();
  470. const layerCount = ktx2File.getLayers() || 1;
  471. const levelCount = ktx2File.getLevels();
  472. const faceCount = ktx2File.getFaces();
  473. const hasAlpha = ktx2File.getHasAlpha();
  474. const dfdFlags = ktx2File.getDFDFlags();
  475. const { transcoderFormat, engineFormat, engineType } = getTranscoderFormat( basisFormat, width, height, hasAlpha );
  476. if ( ! width || ! height || ! levelCount ) {
  477. cleanup();
  478. throw new Error( 'THREE.KTX2Loader: Invalid texture' );
  479. }
  480. if ( ! ktx2File.startTranscoding() ) {
  481. cleanup();
  482. throw new Error( 'THREE.KTX2Loader: .startTranscoding failed' );
  483. }
  484. const faces = [];
  485. const buffers = [];
  486. for ( let face = 0; face < faceCount; face ++ ) {
  487. const mipmaps = [];
  488. for ( let mip = 0; mip < levelCount; mip ++ ) {
  489. const layerMips = [];
  490. let mipWidth, mipHeight;
  491. for ( let layer = 0; layer < layerCount; layer ++ ) {
  492. const levelInfo = ktx2File.getImageLevelInfo( mip, layer, face );
  493. if ( face === 0 && mip === 0 && layer === 0 && ( levelInfo.origWidth % 4 !== 0 || levelInfo.origHeight % 4 !== 0 ) ) {
  494. console.warn( 'THREE.KTX2Loader: ETC1S and UASTC textures should use multiple-of-four dimensions.' );
  495. }
  496. if ( levelCount > 1 ) {
  497. mipWidth = levelInfo.origWidth;
  498. mipHeight = levelInfo.origHeight;
  499. } else {
  500. // Handles non-multiple-of-four dimensions in textures without mipmaps. Textures with
  501. // mipmaps must use multiple-of-four dimensions, for some texture formats and APIs.
  502. // See mrdoob/three.js#25908.
  503. mipWidth = levelInfo.width;
  504. mipHeight = levelInfo.height;
  505. }
  506. let dst = new Uint8Array( ktx2File.getImageTranscodedSizeInBytes( mip, layer, 0, transcoderFormat ) );
  507. const status = ktx2File.transcodeImage( dst, mip, layer, face, transcoderFormat, 0, - 1, - 1 );
  508. if ( engineType === EngineType.HalfFloatType ) {
  509. dst = new Uint16Array( dst.buffer, dst.byteOffset, dst.byteLength / Uint16Array.BYTES_PER_ELEMENT );
  510. }
  511. if ( ! status ) {
  512. cleanup();
  513. throw new Error( 'THREE.KTX2Loader: .transcodeImage failed.' );
  514. }
  515. layerMips.push( dst );
  516. }
  517. const mipData = concat( layerMips );
  518. mipmaps.push( { data: mipData, width: mipWidth, height: mipHeight } );
  519. buffers.push( mipData.buffer );
  520. }
  521. faces.push( { mipmaps, width, height, format: engineFormat, type: engineType } );
  522. }
  523. cleanup();
  524. return { faces, buffers, width, height, hasAlpha, dfdFlags, format: engineFormat, type: engineType };
  525. }
  526. //
  527. // Optimal choice of a transcoder target format depends on the Basis format (ETC1S, UASTC, or
  528. // UASTC HDR), device capabilities, and texture dimensions. The list below ranks the formats
  529. // separately for each format. Currently, priority is assigned based on:
  530. //
  531. // high quality > low quality > uncompressed
  532. //
  533. // Prioritization may be revisited, or exposed for configuration, in the future.
  534. //
  535. // Reference: https://github.com/KhronosGroup/3D-Formats-Guidelines/blob/main/KTXDeveloperGuide.md
  536. const FORMAT_OPTIONS = [
  537. {
  538. if: 'astcSupported',
  539. basisFormat: [ BasisFormat.UASTC ],
  540. transcoderFormat: [ TranscoderFormat.ASTC_4x4, TranscoderFormat.ASTC_4x4 ],
  541. engineFormat: [ EngineFormat.RGBA_ASTC_4x4_Format, EngineFormat.RGBA_ASTC_4x4_Format ],
  542. engineType: [ EngineType.UnsignedByteType ],
  543. priorityETC1S: Infinity,
  544. priorityUASTC: 1,
  545. needsPowerOfTwo: false,
  546. },
  547. {
  548. if: 'bptcSupported',
  549. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC ],
  550. transcoderFormat: [ TranscoderFormat.BC7_M5, TranscoderFormat.BC7_M5 ],
  551. engineFormat: [ EngineFormat.RGBA_BPTC_Format, EngineFormat.RGBA_BPTC_Format ],
  552. engineType: [ EngineType.UnsignedByteType ],
  553. priorityETC1S: 3,
  554. priorityUASTC: 2,
  555. needsPowerOfTwo: false,
  556. },
  557. {
  558. if: 'dxtSupported',
  559. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC ],
  560. transcoderFormat: [ TranscoderFormat.BC1, TranscoderFormat.BC3 ],
  561. engineFormat: [ EngineFormat.RGBA_S3TC_DXT1_Format, EngineFormat.RGBA_S3TC_DXT5_Format ],
  562. engineType: [ EngineType.UnsignedByteType ],
  563. priorityETC1S: 4,
  564. priorityUASTC: 5,
  565. needsPowerOfTwo: false,
  566. },
  567. {
  568. if: 'etc2Supported',
  569. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC ],
  570. transcoderFormat: [ TranscoderFormat.ETC1, TranscoderFormat.ETC2 ],
  571. engineFormat: [ EngineFormat.RGB_ETC2_Format, EngineFormat.RGBA_ETC2_EAC_Format ],
  572. engineType: [ EngineType.UnsignedByteType ],
  573. priorityETC1S: 1,
  574. priorityUASTC: 3,
  575. needsPowerOfTwo: false,
  576. },
  577. {
  578. if: 'etc1Supported',
  579. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC ],
  580. transcoderFormat: [ TranscoderFormat.ETC1 ],
  581. engineFormat: [ EngineFormat.RGB_ETC1_Format ],
  582. engineType: [ EngineType.UnsignedByteType ],
  583. priorityETC1S: 2,
  584. priorityUASTC: 4,
  585. needsPowerOfTwo: false,
  586. },
  587. {
  588. if: 'pvrtcSupported',
  589. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC ],
  590. transcoderFormat: [ TranscoderFormat.PVRTC1_4_RGB, TranscoderFormat.PVRTC1_4_RGBA ],
  591. engineFormat: [ EngineFormat.RGB_PVRTC_4BPPV1_Format, EngineFormat.RGBA_PVRTC_4BPPV1_Format ],
  592. engineType: [ EngineType.UnsignedByteType ],
  593. priorityETC1S: 5,
  594. priorityUASTC: 6,
  595. needsPowerOfTwo: true,
  596. },
  597. {
  598. if: 'bptcSupported',
  599. basisFormat: [ BasisFormat.UASTC_HDR ],
  600. transcoderFormat: [ TranscoderFormat.BC6H ],
  601. engineFormat: [ EngineFormat.RGB_BPTC_UNSIGNED_Format ],
  602. engineType: [ EngineType.HalfFloatType ],
  603. priorityHDR: 1,
  604. needsPowerOfTwo: false,
  605. },
  606. // Uncompressed fallbacks.
  607. {
  608. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC ],
  609. transcoderFormat: [ TranscoderFormat.RGBA32, TranscoderFormat.RGBA32 ],
  610. engineFormat: [ EngineFormat.RGBAFormat, EngineFormat.RGBAFormat ],
  611. engineType: [ EngineType.UnsignedByteType, EngineType.UnsignedByteType ],
  612. priorityETC1S: 100,
  613. priorityUASTC: 100,
  614. needsPowerOfTwo: false,
  615. },
  616. {
  617. basisFormat: [ BasisFormat.UASTC_HDR ],
  618. transcoderFormat: [ TranscoderFormat.RGBA_HALF ],
  619. engineFormat: [ EngineFormat.RGBAFormat ],
  620. engineType: [ EngineType.HalfFloatType ],
  621. priorityHDR: 100,
  622. needsPowerOfTwo: false,
  623. }
  624. ];
  625. const OPTIONS = {
  626. // TODO: For ETC1S we intentionally sort by _UASTC_ priority, preserving
  627. // a historical accident shown to avoid performance pitfalls for Linux with
  628. // Firefox & AMD GPU (RadeonSI). Further work needed.
  629. // See https://github.com/mrdoob/three.js/pull/29730.
  630. [ BasisFormat.ETC1S ]: FORMAT_OPTIONS
  631. .filter( ( opt ) => opt.basisFormat.includes( BasisFormat.ETC1S ) )
  632. .sort( ( a, b ) => a.priorityUASTC - b.priorityUASTC ),
  633. [ BasisFormat.UASTC ]: FORMAT_OPTIONS
  634. .filter( ( opt ) => opt.basisFormat.includes( BasisFormat.UASTC ) )
  635. .sort( ( a, b ) => a.priorityUASTC - b.priorityUASTC ),
  636. [ BasisFormat.UASTC_HDR ]: FORMAT_OPTIONS
  637. .filter( ( opt ) => opt.basisFormat.includes( BasisFormat.UASTC_HDR ) )
  638. .sort( ( a, b ) => a.priorityHDR - b.priorityHDR ),
  639. };
  640. function getTranscoderFormat( basisFormat, width, height, hasAlpha ) {
  641. const options = OPTIONS[ basisFormat ];
  642. for ( let i = 0; i < options.length; i ++ ) {
  643. const opt = options[ i ];
  644. if ( opt.if && ! config[ opt.if ] ) continue;
  645. if ( ! opt.basisFormat.includes( basisFormat ) ) continue;
  646. if ( hasAlpha && opt.transcoderFormat.length < 2 ) continue;
  647. if ( opt.needsPowerOfTwo && ! ( isPowerOfTwo( width ) && isPowerOfTwo( height ) ) ) continue;
  648. const transcoderFormat = opt.transcoderFormat[ hasAlpha ? 1 : 0 ];
  649. const engineFormat = opt.engineFormat[ hasAlpha ? 1 : 0 ];
  650. const engineType = opt.engineType[ 0 ];
  651. return { transcoderFormat, engineFormat, engineType };
  652. }
  653. throw new Error( 'THREE.KTX2Loader: Failed to identify transcoding target.' );
  654. }
  655. function isPowerOfTwo( value ) {
  656. if ( value <= 2 ) return true;
  657. return ( value & ( value - 1 ) ) === 0 && value !== 0;
  658. }
  659. /**
  660. * Concatenates N byte arrays.
  661. *
  662. * @param {Uint8Array[]} arrays
  663. * @return {Uint8Array}
  664. */
  665. function concat( arrays ) {
  666. if ( arrays.length === 1 ) return arrays[ 0 ];
  667. let totalByteLength = 0;
  668. for ( let i = 0; i < arrays.length; i ++ ) {
  669. const array = arrays[ i ];
  670. totalByteLength += array.byteLength;
  671. }
  672. const result = new Uint8Array( totalByteLength );
  673. let byteOffset = 0;
  674. for ( let i = 0; i < arrays.length; i ++ ) {
  675. const array = arrays[ i ];
  676. result.set( array, byteOffset );
  677. byteOffset += array.byteLength;
  678. }
  679. return result;
  680. }
  681. };
  682. // Parsing for non-Basis textures. These textures may have supercompression
  683. // like Zstd, but they do not require transcoding.
  684. const UNCOMPRESSED_FORMATS = new Set( [ RGBAFormat, RGBFormat, RGFormat, RedFormat ] );
  685. const FORMAT_MAP = {
  686. [ VK_FORMAT_R32G32B32A32_SFLOAT ]: RGBAFormat,
  687. [ VK_FORMAT_R16G16B16A16_SFLOAT ]: RGBAFormat,
  688. [ VK_FORMAT_R8G8B8A8_UNORM ]: RGBAFormat,
  689. [ VK_FORMAT_R8G8B8A8_SRGB ]: RGBAFormat,
  690. [ VK_FORMAT_R32G32_SFLOAT ]: RGFormat,
  691. [ VK_FORMAT_R16G16_SFLOAT ]: RGFormat,
  692. [ VK_FORMAT_R8G8_UNORM ]: RGFormat,
  693. [ VK_FORMAT_R8G8_SRGB ]: RGFormat,
  694. [ VK_FORMAT_R32_SFLOAT ]: RedFormat,
  695. [ VK_FORMAT_R16_SFLOAT ]: RedFormat,
  696. [ VK_FORMAT_R8_SRGB ]: RedFormat,
  697. [ VK_FORMAT_R8_UNORM ]: RedFormat,
  698. [ VK_FORMAT_E5B9G9R9_UFLOAT_PACK32 ]: RGBFormat,
  699. [ VK_FORMAT_B10G11R11_UFLOAT_PACK32 ]: RGBFormat,
  700. [ VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK ]: RGB_ETC2_Format,
  701. [ VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK ]: RGBA_ETC2_EAC_Format,
  702. [ VK_FORMAT_ASTC_4x4_SFLOAT_BLOCK_EXT ]: RGBA_ASTC_4x4_Format,
  703. [ VK_FORMAT_ASTC_4x4_SRGB_BLOCK ]: RGBA_ASTC_4x4_Format,
  704. [ VK_FORMAT_ASTC_4x4_UNORM_BLOCK ]: RGBA_ASTC_4x4_Format,
  705. [ VK_FORMAT_ASTC_6x6_SRGB_BLOCK ]: RGBA_ASTC_6x6_Format,
  706. [ VK_FORMAT_ASTC_6x6_UNORM_BLOCK ]: RGBA_ASTC_6x6_Format,
  707. [ VK_FORMAT_BC1_RGBA_UNORM_BLOCK ]: RGBA_S3TC_DXT1_Format,
  708. [ VK_FORMAT_BC1_RGBA_SRGB_BLOCK ]: RGBA_S3TC_DXT1_Format,
  709. [ VK_FORMAT_BC1_RGB_UNORM_BLOCK ]: RGB_S3TC_DXT1_Format,
  710. [ VK_FORMAT_BC1_RGB_SRGB_BLOCK ]: RGB_S3TC_DXT1_Format,
  711. [ VK_FORMAT_BC3_SRGB_BLOCK ]: RGBA_S3TC_DXT3_Format,
  712. [ VK_FORMAT_BC3_UNORM_BLOCK ]: RGBA_S3TC_DXT3_Format,
  713. [ VK_FORMAT_BC5_SNORM_BLOCK ]: RGBA_S3TC_DXT5_Format,
  714. [ VK_FORMAT_BC5_UNORM_BLOCK ]: RGBA_S3TC_DXT5_Format,
  715. [ VK_FORMAT_BC7_SRGB_BLOCK ]: RGBA_BPTC_Format,
  716. [ VK_FORMAT_BC7_UNORM_BLOCK ]: RGBA_BPTC_Format,
  717. };
  718. const TYPE_MAP = {
  719. [ VK_FORMAT_R32G32B32A32_SFLOAT ]: FloatType,
  720. [ VK_FORMAT_R16G16B16A16_SFLOAT ]: HalfFloatType,
  721. [ VK_FORMAT_R8G8B8A8_UNORM ]: UnsignedByteType,
  722. [ VK_FORMAT_R8G8B8A8_SRGB ]: UnsignedByteType,
  723. [ VK_FORMAT_R32G32_SFLOAT ]: FloatType,
  724. [ VK_FORMAT_R16G16_SFLOAT ]: HalfFloatType,
  725. [ VK_FORMAT_R8G8_UNORM ]: UnsignedByteType,
  726. [ VK_FORMAT_R8G8_SRGB ]: UnsignedByteType,
  727. [ VK_FORMAT_R32_SFLOAT ]: FloatType,
  728. [ VK_FORMAT_R16_SFLOAT ]: HalfFloatType,
  729. [ VK_FORMAT_R8_SRGB ]: UnsignedByteType,
  730. [ VK_FORMAT_R8_UNORM ]: UnsignedByteType,
  731. [ VK_FORMAT_E5B9G9R9_UFLOAT_PACK32 ]: UnsignedInt5999Type,
  732. [ VK_FORMAT_B10G11R11_UFLOAT_PACK32 ]: UnsignedInt101111Type,
  733. [ VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK ]: UnsignedByteType,
  734. [ VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK ]: UnsignedByteType,
  735. [ VK_FORMAT_ASTC_4x4_SFLOAT_BLOCK_EXT ]: HalfFloatType,
  736. [ VK_FORMAT_ASTC_6x6_SRGB_BLOCK ]: UnsignedByteType,
  737. [ VK_FORMAT_ASTC_6x6_UNORM_BLOCK ]: UnsignedByteType,
  738. };
  739. async function createRawTexture( container ) {
  740. const { vkFormat } = container;
  741. if ( FORMAT_MAP[ vkFormat ] === undefined ) {
  742. throw new Error( 'THREE.KTX2Loader: Unsupported vkFormat.' );
  743. }
  744. //
  745. let zstd;
  746. if ( container.supercompressionScheme === KHR_SUPERCOMPRESSION_ZSTD ) {
  747. if ( ! _zstd ) {
  748. _zstd = new Promise( async ( resolve ) => {
  749. const zstd = new ZSTDDecoder();
  750. await zstd.init();
  751. resolve( zstd );
  752. } );
  753. }
  754. zstd = await _zstd;
  755. }
  756. //
  757. const mipmaps = [];
  758. for ( let levelIndex = 0; levelIndex < container.levels.length; levelIndex ++ ) {
  759. const levelWidth = Math.max( 1, container.pixelWidth >> levelIndex );
  760. const levelHeight = Math.max( 1, container.pixelHeight >> levelIndex );
  761. const levelDepth = container.pixelDepth ? Math.max( 1, container.pixelDepth >> levelIndex ) : 0;
  762. const level = container.levels[ levelIndex ];
  763. let levelData;
  764. if ( container.supercompressionScheme === KHR_SUPERCOMPRESSION_NONE ) {
  765. levelData = level.levelData;
  766. } else if ( container.supercompressionScheme === KHR_SUPERCOMPRESSION_ZSTD ) {
  767. levelData = zstd.decode( level.levelData, level.uncompressedByteLength );
  768. } else {
  769. throw new Error( 'THREE.KTX2Loader: Unsupported supercompressionScheme.' );
  770. }
  771. let data;
  772. if ( TYPE_MAP[ vkFormat ] === FloatType ) {
  773. data = new Float32Array(
  774. levelData.buffer,
  775. levelData.byteOffset,
  776. levelData.byteLength / Float32Array.BYTES_PER_ELEMENT
  777. );
  778. } else if ( TYPE_MAP[ vkFormat ] === HalfFloatType ) {
  779. data = new Uint16Array(
  780. levelData.buffer,
  781. levelData.byteOffset,
  782. levelData.byteLength / Uint16Array.BYTES_PER_ELEMENT
  783. );
  784. } else if ( TYPE_MAP[ vkFormat ] === UnsignedInt5999Type || TYPE_MAP[ vkFormat ] === UnsignedInt101111Type ) {
  785. data = new Uint32Array(
  786. levelData.buffer,
  787. levelData.byteOffset,
  788. levelData.byteLength / Uint32Array.BYTES_PER_ELEMENT
  789. );
  790. } else {
  791. data = levelData;
  792. }
  793. mipmaps.push( {
  794. data: data,
  795. width: levelWidth,
  796. height: levelHeight,
  797. depth: levelDepth,
  798. } );
  799. }
  800. // levelCount = 0 implies runtime-generated mipmaps.
  801. const useMipmaps = container.levelCount === 0 || mipmaps.length > 1
  802. let texture;
  803. if ( UNCOMPRESSED_FORMATS.has( FORMAT_MAP[ vkFormat ] ) ) {
  804. texture = container.pixelDepth === 0
  805. ? new DataTexture( mipmaps[ 0 ].data, container.pixelWidth, container.pixelHeight )
  806. : new Data3DTexture( mipmaps[ 0 ].data, container.pixelWidth, container.pixelHeight, container.pixelDepth );
  807. texture.minFilter = useMipmaps ? NearestMipmapNearestFilter : NearestFilter;
  808. texture.magFilter = NearestFilter;
  809. texture.generateMipmaps = container.levelCount === 0;
  810. } else {
  811. if ( container.pixelDepth > 0 ) throw new Error( 'THREE.KTX2Loader: Unsupported pixelDepth.' );
  812. texture = new CompressedTexture( mipmaps, container.pixelWidth, container.pixelHeight );
  813. texture.minFilter = useMipmaps ? LinearMipmapLinearFilter : LinearFilter;
  814. texture.magFilter = LinearFilter;
  815. }
  816. texture.mipmaps = mipmaps;
  817. texture.type = TYPE_MAP[ vkFormat ];
  818. texture.format = FORMAT_MAP[ vkFormat ];
  819. texture.colorSpace = parseColorSpace( container );
  820. texture.needsUpdate = true;
  821. //
  822. return Promise.resolve( texture );
  823. }
  824. function parseColorSpace( container ) {
  825. const dfd = container.dataFormatDescriptor[ 0 ];
  826. if ( dfd.colorPrimaries === KHR_DF_PRIMARIES_BT709 ) {
  827. return dfd.transferFunction === KHR_DF_TRANSFER_SRGB ? SRGBColorSpace : LinearSRGBColorSpace;
  828. } else if ( dfd.colorPrimaries === KHR_DF_PRIMARIES_DISPLAYP3 ) {
  829. return dfd.transferFunction === KHR_DF_TRANSFER_SRGB ? DisplayP3ColorSpace : LinearDisplayP3ColorSpace;
  830. } else if ( dfd.colorPrimaries === KHR_DF_PRIMARIES_UNSPECIFIED ) {
  831. return NoColorSpace;
  832. } else {
  833. console.warn( `THREE.KTX2Loader: Unsupported color primaries, "${ dfd.colorPrimaries }"` );
  834. return NoColorSpace;
  835. }
  836. }
  837. export { KTX2Loader };
粤ICP备19079148号