KTX2Loader.js 15 KB

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