KTX2Loader.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. ( function () {
  2. /**
  3. * THREE.Loader for KTX 2.0 GPU Texture containers.
  4. *
  5. * KTX 2.0 is a container format for various GPU texture formats. The loader
  6. * supports Basis Universal GPU textures, which can be quickly transcoded to
  7. * a wide variety of GPU texture compression formats. While KTX 2.0 also allows
  8. * other hardware-specific formats, this loader does not yet parse them.
  9. *
  10. * References:
  11. * - KTX: http://github.khronos.org/KTX-Specification/
  12. * - DFD: https://www.khronos.org/registry/DataFormat/specs/1.3/dataformat.1.3.html#basicdescriptor
  13. */
  14. const KTX2TransferSRGB = 2;
  15. const KTX2_ALPHA_PREMULTIPLIED = 1;
  16. const _taskCache = new WeakMap();
  17. let _activeLoaders = 0;
  18. class KTX2Loader extends THREE.Loader {
  19. constructor( manager ) {
  20. super( manager );
  21. this.transcoderPath = '';
  22. this.transcoderBinary = null;
  23. this.transcoderPending = null;
  24. this.workerPool = new THREE.WorkerPool();
  25. this.workerSourceURL = '';
  26. this.workerConfig = null;
  27. if ( typeof MSC_TRANSCODER !== 'undefined' ) {
  28. console.warn( 'THREE.KTX2Loader: Please update to latest "basis_transcoder".' + ' "msc_basis_transcoder" is no longer supported in three.js r125+.' );
  29. }
  30. }
  31. setTranscoderPath( path ) {
  32. this.transcoderPath = path;
  33. return this;
  34. }
  35. setWorkerLimit( num ) {
  36. this.workerPool.setWorkerLimit( num );
  37. return this;
  38. }
  39. detectSupport( renderer ) {
  40. this.workerConfig = {
  41. astcSupported: renderer.extensions.has( 'WEBGL_compressed_texture_astc' ),
  42. etc1Supported: renderer.extensions.has( 'WEBGL_compressed_texture_etc1' ),
  43. etc2Supported: renderer.extensions.has( 'WEBGL_compressed_texture_etc' ),
  44. dxtSupported: renderer.extensions.has( 'WEBGL_compressed_texture_s3tc' ),
  45. bptcSupported: renderer.extensions.has( 'EXT_texture_compression_bptc' ),
  46. pvrtcSupported: renderer.extensions.has( 'WEBGL_compressed_texture_pvrtc' ) || renderer.extensions.has( 'WEBKIT_WEBGL_compressed_texture_pvrtc' )
  47. };
  48. if ( renderer.capabilities.isWebGL2 ) {
  49. // https://github.com/mrdoob/three.js/pull/22928
  50. this.workerConfig.etc1Supported = false;
  51. }
  52. return this;
  53. }
  54. dispose() {
  55. this.workerPool.dispose();
  56. if ( this.workerSourceURL ) URL.revokeObjectURL( this.workerSourceURL );
  57. return this;
  58. }
  59. init() {
  60. if ( ! this.transcoderPending ) {
  61. // Load transcoder wrapper.
  62. const jsLoader = new THREE.FileLoader( this.manager );
  63. jsLoader.setPath( this.transcoderPath );
  64. jsLoader.setWithCredentials( this.withCredentials );
  65. const jsContent = jsLoader.loadAsync( 'basis_transcoder.js' ); // Load transcoder WASM binary.
  66. const binaryLoader = new THREE.FileLoader( this.manager );
  67. binaryLoader.setPath( this.transcoderPath );
  68. binaryLoader.setResponseType( 'arraybuffer' );
  69. binaryLoader.setWithCredentials( this.withCredentials );
  70. const binaryContent = binaryLoader.loadAsync( 'basis_transcoder.wasm' );
  71. this.transcoderPending = Promise.all( [ jsContent, binaryContent ] ).then( ( [ jsContent, binaryContent ] ) => {
  72. const fn = KTX2Loader.BasisWorker.toString();
  73. const body = [ '/* constants */', 'let _EngineFormat = ' + JSON.stringify( KTX2Loader.EngineFormat ), 'let _TranscoderFormat = ' + JSON.stringify( KTX2Loader.TranscoderFormat ), 'let _BasisFormat = ' + JSON.stringify( KTX2Loader.BasisFormat ), '/* basis_transcoder.js */', jsContent, '/* worker */', fn.substring( fn.indexOf( '{' ) + 1, fn.lastIndexOf( '}' ) ) ].join( '\n' );
  74. this.workerSourceURL = URL.createObjectURL( new Blob( [ body ] ) );
  75. this.transcoderBinary = binaryContent;
  76. this.workerPool.setWorkerCreator( () => {
  77. const worker = new Worker( this.workerSourceURL );
  78. const transcoderBinary = this.transcoderBinary.slice( 0 );
  79. worker.postMessage( {
  80. type: 'init',
  81. config: this.workerConfig,
  82. transcoderBinary
  83. }, [ transcoderBinary ] );
  84. return worker;
  85. } );
  86. } );
  87. if ( _activeLoaders > 0 ) {
  88. // Each instance loads a transcoder and allocates workers, increasing network and memory cost.
  89. console.warn( 'THREE.KTX2Loader: Multiple active KTX2 loaders may cause performance issues.' + ' Use a single KTX2Loader instance, or call .dispose() on old instances.' );
  90. }
  91. _activeLoaders ++;
  92. }
  93. return this.transcoderPending;
  94. }
  95. load( url, onLoad, onProgress, onError ) {
  96. if ( this.workerConfig === null ) {
  97. throw new Error( 'THREE.KTX2Loader: Missing initialization with `.detectSupport( renderer )`.' );
  98. }
  99. const loader = new THREE.FileLoader( this.manager );
  100. loader.setResponseType( 'arraybuffer' );
  101. loader.setWithCredentials( this.withCredentials );
  102. const texture = new THREE.CompressedTexture();
  103. loader.load( url, buffer => {
  104. // Check for an existing task using this buffer. A transferred buffer cannot be transferred
  105. // again from this thread.
  106. if ( _taskCache.has( buffer ) ) {
  107. const cachedTask = _taskCache.get( buffer );
  108. return cachedTask.promise.then( onLoad ).catch( onError );
  109. }
  110. this._createTexture( [ buffer ] ).then( function ( _texture ) {
  111. texture.copy( _texture );
  112. texture.needsUpdate = true;
  113. if ( onLoad ) onLoad( texture );
  114. } ).catch( onError );
  115. }, onProgress, onError );
  116. return texture;
  117. }
  118. _createTextureFrom( transcodeResult ) {
  119. const {
  120. mipmaps,
  121. width,
  122. height,
  123. format,
  124. type,
  125. error,
  126. dfdTransferFn,
  127. dfdFlags
  128. } = transcodeResult;
  129. if ( type === 'error' ) return Promise.reject( error );
  130. const texture = new THREE.CompressedTexture( mipmaps, width, height, format, THREE.UnsignedByteType );
  131. texture.minFilter = mipmaps.length === 1 ? THREE.LinearFilter : THREE.LinearMipmapLinearFilter;
  132. texture.magFilter = THREE.LinearFilter;
  133. texture.generateMipmaps = false;
  134. texture.needsUpdate = true;
  135. texture.encoding = dfdTransferFn === KTX2TransferSRGB ? THREE.sRGBEncoding : THREE.LinearEncoding;
  136. texture.premultiplyAlpha = !! ( dfdFlags & KTX2_ALPHA_PREMULTIPLIED );
  137. return texture;
  138. }
  139. /**
  140. * @param {ArrayBuffer[]} buffers
  141. * @param {object?} config
  142. * @return {Promise<CompressedTexture>}
  143. */
  144. _createTexture( buffers, config = {} ) {
  145. const taskConfig = config;
  146. const texturePending = this.init().then( () => {
  147. return this.workerPool.postMessage( {
  148. type: 'transcode',
  149. buffers,
  150. taskConfig: taskConfig
  151. }, buffers );
  152. } ).then( e => this._createTextureFrom( e.data ) ); // Cache the task result.
  153. _taskCache.set( buffers[ 0 ], {
  154. promise: texturePending
  155. } );
  156. return texturePending;
  157. }
  158. dispose() {
  159. URL.revokeObjectURL( this.workerSourceURL );
  160. this.workerPool.dispose();
  161. _activeLoaders --;
  162. return this;
  163. }
  164. }
  165. /* CONSTANTS */
  166. KTX2Loader.BasisFormat = {
  167. ETC1S: 0,
  168. UASTC_4x4: 1
  169. };
  170. KTX2Loader.TranscoderFormat = {
  171. ETC1: 0,
  172. ETC2: 1,
  173. BC1: 2,
  174. BC3: 3,
  175. BC4: 4,
  176. BC5: 5,
  177. BC7_M6_OPAQUE_ONLY: 6,
  178. BC7_M5: 7,
  179. PVRTC1_4_RGB: 8,
  180. PVRTC1_4_RGBA: 9,
  181. ASTC_4x4: 10,
  182. ATC_RGB: 11,
  183. ATC_RGBA_INTERPOLATED_ALPHA: 12,
  184. RGBA32: 13,
  185. RGB565: 14,
  186. BGR565: 15,
  187. RGBA4444: 16
  188. };
  189. KTX2Loader.EngineFormat = {
  190. RGBAFormat: THREE.RGBAFormat,
  191. RGBA_ASTC_4x4_Format: THREE.RGBA_ASTC_4x4_Format,
  192. RGBA_BPTC_Format: THREE.RGBA_BPTC_Format,
  193. RGBA_ETC2_EAC_Format: THREE.RGBA_ETC2_EAC_Format,
  194. RGBA_PVRTC_4BPPV1_Format: THREE.RGBA_PVRTC_4BPPV1_Format,
  195. RGBA_S3TC_DXT5_Format: THREE.RGBA_S3TC_DXT5_Format,
  196. RGB_ETC1_Format: THREE.RGB_ETC1_Format,
  197. RGB_ETC2_Format: THREE.RGB_ETC2_Format,
  198. RGB_PVRTC_4BPPV1_Format: THREE.RGB_PVRTC_4BPPV1_Format,
  199. RGB_S3TC_DXT1_Format: THREE.RGB_S3TC_DXT1_Format
  200. };
  201. /* WEB WORKER */
  202. KTX2Loader.BasisWorker = function () {
  203. let config;
  204. let transcoderPending;
  205. let BasisModule;
  206. const EngineFormat = _EngineFormat; // eslint-disable-line no-undef
  207. const TranscoderFormat = _TranscoderFormat; // eslint-disable-line no-undef
  208. const BasisFormat = _BasisFormat; // eslint-disable-line no-undef
  209. self.addEventListener( 'message', function ( e ) {
  210. const message = e.data;
  211. switch ( message.type ) {
  212. case 'init':
  213. config = message.config;
  214. init( message.transcoderBinary );
  215. break;
  216. case 'transcode':
  217. transcoderPending.then( () => {
  218. try {
  219. const {
  220. width,
  221. height,
  222. hasAlpha,
  223. mipmaps,
  224. format,
  225. dfdTransferFn,
  226. dfdFlags
  227. } = transcode( message.buffers[ 0 ] );
  228. const buffers = [];
  229. for ( let i = 0; i < mipmaps.length; ++ i ) {
  230. buffers.push( mipmaps[ i ].data.buffer );
  231. }
  232. self.postMessage( {
  233. type: 'transcode',
  234. id: message.id,
  235. width,
  236. height,
  237. hasAlpha,
  238. mipmaps,
  239. format,
  240. dfdTransferFn,
  241. dfdFlags
  242. }, buffers );
  243. } catch ( error ) {
  244. console.error( error );
  245. self.postMessage( {
  246. type: 'error',
  247. id: message.id,
  248. error: error.message
  249. } );
  250. }
  251. } );
  252. break;
  253. }
  254. } );
  255. function init( wasmBinary ) {
  256. transcoderPending = new Promise( resolve => {
  257. BasisModule = {
  258. wasmBinary,
  259. onRuntimeInitialized: resolve
  260. };
  261. BASIS( BasisModule ); // eslint-disable-line no-undef
  262. } ).then( () => {
  263. BasisModule.initializeBasis();
  264. if ( BasisModule.KTX2File === undefined ) {
  265. console.warn( 'THREE.KTX2Loader: Please update Basis Universal transcoder.' );
  266. }
  267. } );
  268. }
  269. function transcode( buffer ) {
  270. const ktx2File = new BasisModule.KTX2File( new Uint8Array( buffer ) );
  271. function cleanup() {
  272. ktx2File.close();
  273. ktx2File.delete();
  274. }
  275. if ( ! ktx2File.isValid() ) {
  276. cleanup();
  277. throw new Error( 'THREE.KTX2Loader: Invalid or unsupported .ktx2 file' );
  278. }
  279. const basisFormat = ktx2File.isUASTC() ? BasisFormat.UASTC_4x4 : BasisFormat.ETC1S;
  280. const width = ktx2File.getWidth();
  281. const height = ktx2File.getHeight();
  282. const levels = ktx2File.getLevels();
  283. const hasAlpha = ktx2File.getHasAlpha();
  284. const dfdTransferFn = ktx2File.getDFDTransferFunc();
  285. const dfdFlags = ktx2File.getDFDFlags();
  286. const {
  287. transcoderFormat,
  288. engineFormat
  289. } = getTranscoderFormat( basisFormat, width, height, hasAlpha );
  290. if ( ! width || ! height || ! levels ) {
  291. cleanup();
  292. throw new Error( 'THREE.KTX2Loader: Invalid texture' );
  293. }
  294. if ( ! ktx2File.startTranscoding() ) {
  295. cleanup();
  296. throw new Error( 'THREE.KTX2Loader: .startTranscoding failed' );
  297. }
  298. const mipmaps = [];
  299. for ( let mip = 0; mip < levels; mip ++ ) {
  300. const levelInfo = ktx2File.getImageLevelInfo( mip, 0, 0 );
  301. const mipWidth = levelInfo.origWidth;
  302. const mipHeight = levelInfo.origHeight;
  303. const dst = new Uint8Array( ktx2File.getImageTranscodedSizeInBytes( mip, 0, 0, transcoderFormat ) );
  304. const status = ktx2File.transcodeImage( dst, mip, 0, 0, transcoderFormat, 0, - 1, - 1 );
  305. if ( ! status ) {
  306. cleanup();
  307. throw new Error( 'THREE.KTX2Loader: .transcodeImage failed.' );
  308. }
  309. mipmaps.push( {
  310. data: dst,
  311. width: mipWidth,
  312. height: mipHeight
  313. } );
  314. }
  315. cleanup();
  316. return {
  317. width,
  318. height,
  319. hasAlpha,
  320. mipmaps,
  321. format: engineFormat,
  322. dfdTransferFn,
  323. dfdFlags
  324. };
  325. } //
  326. // Optimal choice of a transcoder target format depends on the Basis format (ETC1S or UASTC),
  327. // device capabilities, and texture dimensions. The list below ranks the formats separately
  328. // for ETC1S and UASTC.
  329. //
  330. // In some cases, transcoding UASTC to RGBA32 might be preferred for higher quality (at
  331. // significant memory cost) compared to ETC1/2, BC1/3, and PVRTC. The transcoder currently
  332. // chooses RGBA32 only as a last resort and does not expose that option to the caller.
  333. const FORMAT_OPTIONS = [ {
  334. if: 'astcSupported',
  335. basisFormat: [ BasisFormat.UASTC_4x4 ],
  336. transcoderFormat: [ TranscoderFormat.ASTC_4x4, TranscoderFormat.ASTC_4x4 ],
  337. engineFormat: [ EngineFormat.RGBA_ASTC_4x4_Format, EngineFormat.RGBA_ASTC_4x4_Format ],
  338. priorityETC1S: Infinity,
  339. priorityUASTC: 1,
  340. needsPowerOfTwo: false
  341. }, {
  342. if: 'bptcSupported',
  343. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  344. transcoderFormat: [ TranscoderFormat.BC7_M5, TranscoderFormat.BC7_M5 ],
  345. engineFormat: [ EngineFormat.RGBA_BPTC_Format, EngineFormat.RGBA_BPTC_Format ],
  346. priorityETC1S: 3,
  347. priorityUASTC: 2,
  348. needsPowerOfTwo: false
  349. }, {
  350. if: 'dxtSupported',
  351. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  352. transcoderFormat: [ TranscoderFormat.BC1, TranscoderFormat.BC3 ],
  353. engineFormat: [ EngineFormat.RGB_S3TC_DXT1_Format, EngineFormat.RGBA_S3TC_DXT5_Format ],
  354. priorityETC1S: 4,
  355. priorityUASTC: 5,
  356. needsPowerOfTwo: false
  357. }, {
  358. if: 'etc2Supported',
  359. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  360. transcoderFormat: [ TranscoderFormat.ETC1, TranscoderFormat.ETC2 ],
  361. engineFormat: [ EngineFormat.RGB_ETC2_Format, EngineFormat.RGBA_ETC2_EAC_Format ],
  362. priorityETC1S: 1,
  363. priorityUASTC: 3,
  364. needsPowerOfTwo: false
  365. }, {
  366. if: 'etc1Supported',
  367. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  368. transcoderFormat: [ TranscoderFormat.ETC1 ],
  369. engineFormat: [ EngineFormat.RGB_ETC1_Format ],
  370. priorityETC1S: 2,
  371. priorityUASTC: 4,
  372. needsPowerOfTwo: false
  373. }, {
  374. if: 'pvrtcSupported',
  375. basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
  376. transcoderFormat: [ TranscoderFormat.PVRTC1_4_RGB, TranscoderFormat.PVRTC1_4_RGBA ],
  377. engineFormat: [ EngineFormat.RGB_PVRTC_4BPPV1_Format, EngineFormat.RGBA_PVRTC_4BPPV1_Format ],
  378. priorityETC1S: 5,
  379. priorityUASTC: 6,
  380. needsPowerOfTwo: true
  381. } ];
  382. const ETC1S_OPTIONS = FORMAT_OPTIONS.sort( function ( a, b ) {
  383. return a.priorityETC1S - b.priorityETC1S;
  384. } );
  385. const UASTC_OPTIONS = FORMAT_OPTIONS.sort( function ( a, b ) {
  386. return a.priorityUASTC - b.priorityUASTC;
  387. } );
  388. function getTranscoderFormat( basisFormat, width, height, hasAlpha ) {
  389. let transcoderFormat;
  390. let engineFormat;
  391. const options = basisFormat === BasisFormat.ETC1S ? ETC1S_OPTIONS : UASTC_OPTIONS;
  392. for ( let i = 0; i < options.length; i ++ ) {
  393. const opt = options[ i ];
  394. if ( ! config[ opt.if ] ) continue;
  395. if ( ! opt.basisFormat.includes( basisFormat ) ) continue;
  396. if ( hasAlpha && opt.transcoderFormat.length < 2 ) continue;
  397. if ( opt.needsPowerOfTwo && ! ( isPowerOfTwo( width ) && isPowerOfTwo( height ) ) ) continue;
  398. transcoderFormat = opt.transcoderFormat[ hasAlpha ? 1 : 0 ];
  399. engineFormat = opt.engineFormat[ hasAlpha ? 1 : 0 ];
  400. return {
  401. transcoderFormat,
  402. engineFormat
  403. };
  404. }
  405. console.warn( 'THREE.KTX2Loader: No suitable compressed texture format found. Decoding to RGBA32.' );
  406. transcoderFormat = TranscoderFormat.RGBA32;
  407. engineFormat = EngineFormat.RGBAFormat;
  408. return {
  409. transcoderFormat,
  410. engineFormat
  411. };
  412. }
  413. function isPowerOfTwo( value ) {
  414. if ( value <= 2 ) return true;
  415. return ( value & value - 1 ) === 0 && value !== 0;
  416. }
  417. };
  418. THREE.KTX2Loader = KTX2Loader;
  419. } )();