KTX2Loader.js 14 KB

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