babylonjs-ktx2decoder 8.8.0 → 8.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("babylonjs-ktx2decoder",[],t):"object"==typeof exports?exports["babylonjs-ktx2decoder"]=t():e.KTX2DECODER=t()}("undefined"!=typeof self?self:"undefined"!=typeof global?global:this,(()=>(()=>{"use strict";var e={d:(t,a)=>{for(var r in a)e.o(a,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:a[r]})}};e.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),e.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),e.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var t={};e.d(t,{default:()=>L});var a,r,s,n,o,i,d,c,l={};e.r(l),e.d(l,{DataReader:()=>m,KTX2Decoder:()=>F,KTX2FileReader:()=>h,LiteTranscoder:()=>f,LiteTranscoder_UASTC_ASTC:()=>p,LiteTranscoder_UASTC_BC7:()=>T,LiteTranscoder_UASTC_R8_UNORM:()=>R,LiteTranscoder_UASTC_RG8_UNORM:()=>C,LiteTranscoder_UASTC_RGBA_SRGB:()=>B,LiteTranscoder_UASTC_RGBA_UNORM:()=>g,MSCTranscoder:()=>S,SupercompressionScheme:()=>n,Transcoder:()=>y,TranscoderManager:()=>u,WASMMemoryManager:()=>_,ZSTDDecoder:()=>w}),function(e){e[e.ETC1S=0]="ETC1S",e[e.UASTC4x4=1]="UASTC4x4"}(a||(a={})),function(e){e[e.ASTC_4X4_RGBA=0]="ASTC_4X4_RGBA",e[e.BC7_RGBA=1]="BC7_RGBA",e[e.BC3_RGBA=2]="BC3_RGBA",e[e.BC1_RGB=3]="BC1_RGB",e[e.PVRTC1_4_RGBA=4]="PVRTC1_4_RGBA",e[e.PVRTC1_4_RGB=5]="PVRTC1_4_RGB",e[e.ETC2_RGBA=6]="ETC2_RGBA",e[e.ETC1_RGB=7]="ETC1_RGB",e[e.RGBA32=8]="RGBA32",e[e.R8=9]="R8",e[e.RG8=10]="RG8"}(r||(r={})),function(e){e[e.COMPRESSED_RGBA_BPTC_UNORM_EXT=36492]="COMPRESSED_RGBA_BPTC_UNORM_EXT",e[e.COMPRESSED_RGBA_ASTC_4X4_KHR=37808]="COMPRESSED_RGBA_ASTC_4X4_KHR",e[e.COMPRESSED_RGB_S3TC_DXT1_EXT=33776]="COMPRESSED_RGB_S3TC_DXT1_EXT",e[e.COMPRESSED_RGBA_S3TC_DXT5_EXT=33779]="COMPRESSED_RGBA_S3TC_DXT5_EXT",e[e.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG=35842]="COMPRESSED_RGBA_PVRTC_4BPPV1_IMG",e[e.COMPRESSED_RGB_PVRTC_4BPPV1_IMG=35840]="COMPRESSED_RGB_PVRTC_4BPPV1_IMG",e[e.COMPRESSED_RGBA8_ETC2_EAC=37496]="COMPRESSED_RGBA8_ETC2_EAC",e[e.COMPRESSED_RGB8_ETC2=37492]="COMPRESSED_RGB8_ETC2",e[e.COMPRESSED_RGB_ETC1_WEBGL=36196]="COMPRESSED_RGB_ETC1_WEBGL",e[e.RGBA8Format=32856]="RGBA8Format",e[e.R8Format=33321]="R8Format",e[e.RG8Format=33323]="RG8Format"}(s||(s={}));class m{get byteOffset(){return this._dataByteOffset}constructor(e,t,a){e.buffer?this._dataView=new DataView(e.buffer,e.byteOffset+(t??0),a??e.byteLength):this._dataView=new DataView(e,t??0,a??e.byteLength),this._dataByteOffset=0}readUint8(){const e=this._dataView.getUint8(this._dataByteOffset);return this._dataByteOffset+=1,e}readInt8(){const e=this._dataView.getInt8(this._dataByteOffset);return this._dataByteOffset+=1,e}readUint16(){const e=this._dataView.getUint16(this._dataByteOffset,!0);return this._dataByteOffset+=2,e}readInt16(){const e=this._dataView.getInt16(this._dataByteOffset,!0);return this._dataByteOffset+=2,e}readUint32(){const e=this._dataView.getUint32(this._dataByteOffset,!0);return this._dataByteOffset+=4,e}readInt32(){const e=this._dataView.getInt32(this._dataByteOffset,!0);return this._dataByteOffset+=4,e}readUint64(){const e=this._dataView.getUint32(this._dataByteOffset,!0)+2**32*this._dataView.getUint32(this._dataByteOffset+4,!0);return this._dataByteOffset+=8,e}readUint8Array(e){const t=new Uint8Array(this._dataView.buffer,this._dataView.byteOffset+this._dataByteOffset,e);return this._dataByteOffset+=e,t}skipBytes(e){return this._dataByteOffset+=e,this}}!function(e){e[e.None=0]="None",e[e.BasisLZ=1]="BasisLZ",e[e.ZStandard=2]="ZStandard",e[e.ZLib=3]="ZLib"}(n||(n={})),function(e){e[e.ETC1S=163]="ETC1S",e[e.UASTC=166]="UASTC"}(o||(o={})),function(e){e[e.RGB=0]="RGB",e[e.RRR=3]="RRR",e[e.GGG=4]="GGG",e[e.AAA=15]="AAA"}(i||(i={})),function(e){e[e.RGB=0]="RGB",e[e.RGBA=3]="RGBA",e[e.RRR=4]="RRR",e[e.RRRG=5]="RRRG"}(d||(d={})),function(e){e[e.linear=1]="linear",e[e.sRGB=2]="sRGB"}(c||(c={}));class h{constructor(e){this._data=e}get data(){return this._data}get header(){return this._header}get levels(){return this._levels}get dfdBlock(){return this._dfdBlock}get supercompressionGlobalData(){return this._supercompressionGlobalData}isValid(){return h.IsValid(this._data)}parse(){let e=12;const t=new m(this._data,e,68),a=this._header={vkFormat:t.readUint32(),typeSize:t.readUint32(),pixelWidth:t.readUint32(),pixelHeight:t.readUint32(),pixelDepth:t.readUint32(),layerCount:t.readUint32(),faceCount:t.readUint32(),levelCount:t.readUint32(),supercompressionScheme:t.readUint32(),dfdByteOffset:t.readUint32(),dfdByteLength:t.readUint32(),kvdByteOffset:t.readUint32(),kvdByteLength:t.readUint32(),sgdByteOffset:t.readUint64(),sgdByteLength:t.readUint64()};if(a.pixelDepth>0)throw new Error("Failed to parse KTX2 file - Only 2D textures are currently supported.");if(a.layerCount>1)throw new Error("Failed to parse KTX2 file - Array textures are not currently supported.");if(a.faceCount>1)throw new Error("Failed to parse KTX2 file - Cube textures are not currently supported.");e+=t.byteOffset;let r=Math.max(1,a.levelCount);const s=new m(this._data,e,3*r*8),n=this._levels=[];for(;r--;)n.push({byteOffset:s.readUint64(),byteLength:s.readUint64(),uncompressedByteLength:s.readUint64()});e+=s.byteOffset;const o=new m(this._data,a.dfdByteOffset,a.dfdByteLength),i=this._dfdBlock={vendorId:o.skipBytes(4).readUint16(),descriptorType:o.readUint16(),versionNumber:o.readUint16(),descriptorBlockSize:o.readUint16(),colorModel:o.readUint8(),colorPrimaries:o.readUint8(),transferFunction:o.readUint8(),flags:o.readUint8(),texelBlockDimension:{x:o.readUint8()+1,y:o.readUint8()+1,z:o.readUint8()+1,w:o.readUint8()+1},bytesPlane:[o.readUint8(),o.readUint8(),o.readUint8(),o.readUint8(),o.readUint8(),o.readUint8(),o.readUint8(),o.readUint8()],numSamples:0,samples:new Array};i.numSamples=(i.descriptorBlockSize-24)/16;for(let e=0;e<i.numSamples;e++){const e={bitOffset:o.readUint16(),bitLength:o.readUint8()+1,channelType:o.readUint8(),channelFlags:0,samplePosition:[o.readUint8(),o.readUint8(),o.readUint8(),o.readUint8()],sampleLower:o.readUint32(),sampleUpper:o.readUint32()};e.channelFlags=(240&e.channelType)>>4,e.channelType=15&e.channelType,i.samples.push(e)}const d=this._supercompressionGlobalData={};if(a.sgdByteLength>0){const e=new m(this._data,a.sgdByteOffset,a.sgdByteLength);d.endpointCount=e.readUint16(),d.selectorCount=e.readUint16(),d.endpointsByteLength=e.readUint32(),d.selectorsByteLength=e.readUint32(),d.tablesByteLength=e.readUint32(),d.extendedByteLength=e.readUint32(),d.imageDescs=[];const t=this._getImageCount();for(let a=0;a<t;a++)d.imageDescs.push({imageFlags:e.readUint32(),rgbSliceByteOffset:e.readUint32(),rgbSliceByteLength:e.readUint32(),alphaSliceByteOffset:e.readUint32(),alphaSliceByteLength:e.readUint32()});const r=a.sgdByteOffset+e.byteOffset,s=r+d.endpointsByteLength,n=s+d.selectorsByteLength,o=n+d.tablesByteLength;d.endpointsData=new Uint8Array(this._data.buffer,this._data.byteOffset+r,d.endpointsByteLength),d.selectorsData=new Uint8Array(this._data.buffer,this._data.byteOffset+s,d.selectorsByteLength),d.tablesData=new Uint8Array(this._data.buffer,this._data.byteOffset+n,d.tablesByteLength),d.extendedData=new Uint8Array(this._data.buffer,this._data.byteOffset+o,d.extendedByteLength)}}_getImageCount(){let e=Math.max(this._header.pixelDepth,1);for(let t=1;t<this._header.levelCount;t++)e+=Math.max(this._header.pixelDepth>>t,1);return Math.max(this._header.layerCount,1)*this._header.faceCount*e}get textureFormat(){return 166===this._dfdBlock.colorModel?a.UASTC4x4:a.ETC1S}get hasAlpha(){switch(this.textureFormat){case a.ETC1S:return 2===this._dfdBlock.numSamples&&(15===this._dfdBlock.samples[0].channelType||15===this._dfdBlock.samples[1].channelType);case a.UASTC4x4:return 3===this._dfdBlock.samples[0].channelType}return!1}get needZSTDDecoder(){return this._header.supercompressionScheme===n.ZStandard}get isInGammaSpace(){return 2===this._dfdBlock.transferFunction}static IsValid(e){if(e.byteLength>=12){const t=new Uint8Array(e.buffer,e.byteOffset,12);if(171===t[0]&&75===t[1]&&84===t[2]&&88===t[3]&&32===t[4]&&50===t[5]&&48===t[6]&&187===t[7]&&13===t[8]&&10===t[9]&&26===t[10]&&10===t[11])return!0}return!1}}class _{static async LoadWASM(e){if(this.LoadBinariesFromCurrentThread)return new Promise(((t,a)=>{fetch(e).then((async t=>{if(t.ok)return t.arrayBuffer();throw new Error(`Could not fetch the wasm component from "${e}": ${t.status} - ${t.statusText}`)})).then((e=>t(e))).catch((e=>{a(e)}))}));const t=this._RequestId++;return new Promise((a=>{const r=e=>{"wasmLoaded"===e.data.action&&e.data.id===t&&(self.removeEventListener("message",r),a(e.data.wasmBinary))};self.addEventListener("message",r),postMessage({action:"loadWASM",path:e,id:t})}))}constructor(e=_.InitialMemoryPages){this._numPages=e,this._memory=new WebAssembly.Memory({initial:this._numPages}),this._memoryViewByteLength=this._numPages<<16,this._memoryViewOffset=0,this._memoryView=new Uint8Array(this._memory.buffer,this._memoryViewOffset,this._memoryViewByteLength)}get wasmMemory(){return this._memory}getMemoryView(e,t=0,a){return a=a??e<<16,this._numPages<e?(this._memory.grow(e-this._numPages),this._numPages=e,this._memoryView=new Uint8Array(this._memory.buffer,t,a),this._memoryViewByteLength=a,this._memoryViewOffset=t):(this._memoryView=new Uint8Array(this._memory.buffer,t,a),this._memoryViewByteLength=a,this._memoryViewOffset=t),this._memoryView}}_.LoadBinariesFromCurrentThread=!0,_.InitialMemoryPages=16,_._RequestId=0;class u{static RegisterTranscoder(e){u._Transcoders.push(e)}findTranscoder(e,t,s,n){let o=null;const i=a[e]+"_"+r[t];for(let a=0;a<u._Transcoders.length;++a)if(u._Transcoders[a].CanTranscode(e,t,s)&&(!n||n.indexOf(u._Transcoders[a].Name)<0)){o=this._getExistingTranscoder(i,u._Transcoders[a].Name),o||(o=new u._Transcoders[a],o.initialize(),o.needMemoryManager()&&(this._wasmMemoryManager||(this._wasmMemoryManager=new _),o.setMemoryManager(this._wasmMemoryManager)),u._TranscoderInstances[i]||(u._TranscoderInstances[i]=[]),u._TranscoderInstances[i].push(o));break}return o}_getExistingTranscoder(e,t){const a=u._TranscoderInstances[e];if(a)for(let e=0;e<a.length;++e){const r=a[e];if(t===r.getName())return r}return null}}u._Transcoders=[],u._TranscoderInstances={};class y{static CanTranscode(e,t,a){return!1}static GetWasmUrl(e){return y.WasmBaseUrl&&e.startsWith("https://cdn.babylonjs.com/")&&(e=e.replace("https://cdn.babylonjs.com/",y.WasmBaseUrl)),e}getName(){return y.Name}initialize(){}needMemoryManager(){return!1}setMemoryManager(e){}async transcode(e,t,a,r,s,n,o,i,d){return Promise.resolve(null)}}y.Name="Transcoder",y.WasmBaseUrl="";class f extends y{constructor(){super(...arguments),this._wasmBinary=null}async _instantiateWebAssemblyAsync(e){return WebAssembly.instantiate(e,{env:{memory:this._memoryManager.wasmMemory}}).then((e=>({module:e.instance.exports})))}async _loadModuleAsync(e=this._wasmBinary){return this._modulePromise=this._modulePromise||(e?Promise.resolve(e):_.LoadWASM(this._modulePath)).then((async e=>this._instantiateWebAssemblyAsync(e))),this._modulePromise}get memoryManager(){return this._memoryManager}setModulePath(e,t){this._modulePath=y.GetWasmUrl(e),this._wasmBinary=t}initialize(){this._transcodeInPlace=!0}needMemoryManager(){return!0}setMemoryManager(e){this._memoryManager=e}async transcode(e,t,a,r,s,n,o,i,d){return this._loadModuleAsync().then((e=>{const t=e.module,[a,o,i]=this._prepareTranscoding(r,s,n,d);return 0===t.transcode(i)?this._transcodeInPlace?a.slice():o.slice():null}))}_prepareTranscoding(e,t,a,r,s){const n=(e+3>>2)*(t+3>>2);void 0!==s&&(a=e*(t+3>>2)*4*s);const o=1+(16*n+65535+(this._transcodeInPlace?0:a)>>16),i=this.memoryManager.getMemoryView(o,65536,16*n),d=this._transcodeInPlace?null:new Uint8Array(this._memoryManager.wasmMemory.buffer,65536+16*n,void 0!==s?e*t*s:a);return i.set(r),[i,d,n]}}class p extends f{static CanTranscode(e,t,s){return e===a.UASTC4x4&&t===r.ASTC_4X4_RGBA}getName(){return p.Name}initialize(){super.initialize(),this.setModulePath(p.WasmModuleURL,p.WasmBinary)}}p.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_astc.wasm",p.WasmBinary=null,p.Name="UniversalTranscoder_UASTC_ASTC";class T extends f{static CanTranscode(e,t,s){return e===a.UASTC4x4&&t===r.BC7_RGBA}getName(){return T.Name}initialize(){super.initialize(),this.setModulePath(T.WasmModuleURL,T.WasmBinary)}}T.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_bc7.wasm",T.WasmBinary=null,T.Name="UniversalTranscoder_UASTC_BC7";class g extends f{static CanTranscode(e,t,s){return e===a.UASTC4x4&&t===r.RGBA32&&!s}getName(){return g.Name}initialize(){super.initialize(),this._transcodeInPlace=!1,this.setModulePath(g.WasmModuleURL,g.WasmBinary)}async transcode(e,t,a,r,s,n,o,i,d){return this._loadModuleAsync().then((e=>{const t=e.module,[,a]=this._prepareTranscoding(r,s,n,d,4);return 0===t.decode(r,s)?a.slice():null}))}}g.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_rgba8_unorm_v2.wasm",g.WasmBinary=null,g.Name="UniversalTranscoder_UASTC_RGBA_UNORM";class B extends f{static CanTranscode(e,t,s){return e===a.UASTC4x4&&t===r.RGBA32&&s}getName(){return B.Name}initialize(){super.initialize(),this._transcodeInPlace=!1,this.setModulePath(B.WasmModuleURL,B.WasmBinary)}async transcode(e,t,a,r,s,n,o,i,d){return this._loadModuleAsync().then((e=>{const t=e.module,[,a]=this._prepareTranscoding(r,s,n,d,4);return 0===t.decode(r,s)?a.slice():null}))}}B.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_rgba8_srgb_v2.wasm",B.WasmBinary=null,B.Name="UniversalTranscoder_UASTC_RGBA_SRGB";class R extends f{static CanTranscode(e,t,s){return e===a.UASTC4x4&&t===r.R8}getName(){return R.Name}initialize(){super.initialize(),this._transcodeInPlace=!1,this.setModulePath(R.WasmModuleURL,R.WasmBinary)}async transcode(e,t,a,r,s,n,o,i,d){return this._loadModuleAsync().then((e=>{const t=e.module,[,a]=this._prepareTranscoding(r,s,n,d,1);return 0===t.decode(r,s)?a.slice():null}))}}R.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_r8_unorm.wasm",R.WasmBinary=null,R.Name="UniversalTranscoder_UASTC_R8_UNORM";class C extends f{static CanTranscode(e,t,s){return e===a.UASTC4x4&&t===r.RG8}getName(){return C.Name}initialize(){super.initialize(),this._transcodeInPlace=!1,this.setModulePath(C.WasmModuleURL,C.WasmBinary)}async transcode(e,t,a,r,s,n,o,i,d){return this._loadModuleAsync().then((e=>{const t=e.module,[,a]=this._prepareTranscoding(r,s,n,d,2);return 0===t.decode(r,s)?a.slice():null}))}}C.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_rg8_unorm.wasm",C.WasmBinary=null,C.Name="UniversalTranscoder_UASTC_RG8_UNORM";class S extends y{getName(){return S.Name}async _getMSCBasisTranscoder(){return this._mscBasisTranscoderPromise||(this._mscBasisTranscoderPromise=(S.WasmBinary?Promise.resolve(S.WasmBinary):_.LoadWASM(y.GetWasmUrl(S.WasmModuleURL))).then((async e=>{if(S.JSModule&&"undefined"==typeof MSC_TRANSCODER)globalThis.MSC_TRANSCODER=S.JSModule;else if(S.UseFromWorkerThread)importScripts(y.GetWasmUrl(S.JSModuleURL));else if("undefined"==typeof MSC_TRANSCODER)return new Promise(((t,a)=>{const r=document.getElementsByTagName("head")[0],s=document.createElement("script");s.setAttribute("type","text/javascript"),s.setAttribute("src",y.GetWasmUrl(S.JSModuleURL)),s.onload=()=>{"undefined"!=typeof MSC_TRANSCODER?MSC_TRANSCODER({wasmBinary:e}).then((e=>{e.initTranscoders(),this._mscBasisModule=e,t()})):a("MSC_TRANSCODER script loaded but MSC_TRANSCODER is not defined.")},s.onerror=()=>{a("Can not load MSC_TRANSCODER script.")},r.appendChild(s)}));return new Promise((t=>{MSC_TRANSCODER({wasmBinary:e}).then((e=>{e.initTranscoders(),this._mscBasisModule=e,t()}))}))}))),this._mscBasisTranscoderPromise}static CanTranscode(e,t,a){return!0}async transcode(e,t,s,n,o,i,d,c,l){const m=!1;return this._getMSCBasisTranscoder().then((()=>{const h=this._mscBasisModule;let _,u,y,f=null;try{_=e===a.UASTC4x4?new h.UastcImageTranscoder:new h.BasisLzEtc1sImageTranscoder;const f=e===a.UASTC4x4?h.TextureFormat.UASTC4x4:h.TextureFormat.ETC1S;u=new h.ImageInfo(f,n,o,s);const p=h.TranscodeTarget[r[t]];if(!h.isFormatSupported(p,f))throw new Error(`MSCTranscoder: Transcoding from "${a[e]}" to "${r[t]}" not supported by current transcoder build.`);if(e===a.ETC1S){const e=d.supercompressionGlobalData;_.decodePalettes(e.endpointCount,e.endpointsData,e.selectorCount,e.selectorsData),_.decodeTables(e.tablesData),u.flags=c.imageFlags,u.rgbByteOffset=0,u.rgbByteLength=c.rgbSliceByteLength,u.alphaByteOffset=c.alphaSliceByteOffset>0?c.rgbSliceByteLength:0,u.alphaByteLength=c.alphaSliceByteLength,y=_.transcodeImage(p,l,u,0,m)}else u.flags=0,u.rgbByteOffset=0,u.rgbByteLength=i,u.alphaByteOffset=0,u.alphaByteLength=0,y=_.transcodeImage(p,l,u,0,d.hasAlpha,m)}finally{_&&_.delete(),u&&u.delete(),y&&y.transcodedImage&&(f=y.transcodedImage.get_typed_memory_view().slice(),y.transcodedImage.delete())}return f}))}}let A,U,M;S.JSModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/msc_basis_transcoder.js",S.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/msc_basis_transcoder.wasm",S.WasmBinary=null,S.JSModule=null,S.UseFromWorkerThread=!0,S.Name="MSCTranscoder";const b={env:{emscripten_notify_memory_growth:function(){M=new Uint8Array(U.exports.memory.buffer)}}};class w{async init(){return A||(A="undefined"!=typeof fetch?fetch(y.GetWasmUrl(w.WasmModuleURL)).then((async e=>{if(e.ok)return e.arrayBuffer();throw new Error(`Could not fetch the wasm component for the Zstandard decompression lib: ${e.status} - ${e.statusText}`)})).then((async e=>WebAssembly.instantiate(e,b))).then(this._init):WebAssembly.instantiateStreaming(fetch(w.WasmModuleURL),b).then(this._init),A)}_init(e){U=e.instance,b.env.emscripten_notify_memory_growth()}decode(e,t=0){if(!U)throw new Error("ZSTDDecoder: Await .init() before decoding.");const a=e.byteLength,r=U.exports.malloc(a);M.set(e,r),t=t||Number(U.exports.ZSTD_findDecompressedSize(r,a));const s=U.exports.malloc(t),n=U.exports.ZSTD_decompress(s,t,r,a),o=M.slice(s,s+n);return U.exports.free(r),U.exports.free(s),o}}w.WasmModuleURL="https://cdn.babylonjs.com/zstddec.wasm";const G={ETC1S:{option:"forceRGBA",yes:{transcodeFormat:r.RGBA32,engineFormat:32856,roundToMultiple4:!1},no:{cap:"etc2",yes:{alpha:!0,yes:{transcodeFormat:r.ETC2_RGBA,engineFormat:37496},no:{transcodeFormat:r.ETC1_RGB,engineFormat:37492}},no:{cap:"etc1",alpha:!1,yes:{transcodeFormat:r.ETC1_RGB,engineFormat:36196},no:{cap:"bptc",yes:{transcodeFormat:r.BC7_RGBA,engineFormat:36492},no:{cap:"s3tc",yes:{alpha:!0,yes:{transcodeFormat:r.BC3_RGBA,engineFormat:33779},no:{transcodeFormat:r.BC1_RGB,engineFormat:33776}},no:{cap:"pvrtc",needsPowerOfTwo:!0,yes:{alpha:!0,yes:{transcodeFormat:r.PVRTC1_4_RGBA,engineFormat:35842},no:{transcodeFormat:r.PVRTC1_4_RGB,engineFormat:35840}},no:{transcodeFormat:r.RGBA32,engineFormat:32856,roundToMultiple4:!1}}}}}}},UASTC:{option:"forceRGBA",yes:{transcodeFormat:r.RGBA32,engineFormat:32856,roundToMultiple4:!1},no:{option:"forceR8",yes:{transcodeFormat:r.R8,engineFormat:33321,roundToMultiple4:!1},no:{option:"forceRG8",yes:{transcodeFormat:r.RG8,engineFormat:33323,roundToMultiple4:!1},no:{cap:"astc",yes:{transcodeFormat:r.ASTC_4X4_RGBA,engineFormat:37808},no:{cap:"bptc",yes:{transcodeFormat:r.BC7_RGBA,engineFormat:36492},no:{option:"useRGBAIfASTCBC7NotAvailableWhenUASTC",yes:{transcodeFormat:r.RGBA32,engineFormat:32856,roundToMultiple4:!1},no:{cap:"etc2",yes:{alpha:!0,yes:{transcodeFormat:r.ETC2_RGBA,engineFormat:37496},no:{transcodeFormat:r.ETC1_RGB,engineFormat:37492}},no:{cap:"etc1",yes:{transcodeFormat:r.ETC1_RGB,engineFormat:36196},no:{cap:"s3tc",yes:{alpha:!0,yes:{transcodeFormat:r.BC3_RGBA,engineFormat:33779},no:{transcodeFormat:r.BC1_RGB,engineFormat:33776}},no:{cap:"pvrtc",needsPowerOfTwo:!0,yes:{alpha:!0,yes:{transcodeFormat:r.PVRTC1_4_RGBA,engineFormat:35842},no:{transcodeFormat:r.PVRTC1_4_RGB,engineFormat:35840}},no:{transcodeFormat:r.RGBA32,engineFormat:32856,roundToMultiple4:!1}}}}}}}}}}}};class O{static _IsLeafNode(e){return void 0!==e.engineFormat}get transcodeFormat(){return this._transcodeFormat}get engineFormat(){return this._engineFormat}get roundToMultiple4(){return this._roundToMultiple4}constructor(e,t,a,r,s){this._textureFormat=e,this._hasAlpha=t,this._isPowerOfTwo=a,this._caps=r,this._options=s??{},this.parseTree(G)}parseTree(e){const t=this._textureFormat===a.UASTC4x4?e.UASTC:e.ETC1S;return t&&this._parseNode(t),void 0!==t}_parseNode(e){if(e)if(O._IsLeafNode(e))this._transcodeFormat=e.transcodeFormat,this._engineFormat=e.engineFormat,this._roundToMultiple4=e.roundToMultiple4??!0;else{let t=!0;void 0!==e.cap&&(t=t&&!!this._caps[e.cap]),void 0!==e.option&&(t=t&&!!this._options[e.option]),void 0!==e.alpha&&(t=t&&this._hasAlpha===e.alpha),void 0!==e.needsPowerOfTwo&&(t=t&&this._isPowerOfTwo===e.needsPowerOfTwo),void 0!==e.transcodeFormat&&(t=Array.isArray(e.transcodeFormat)?t&&-1!==e.transcodeFormat.indexOf(this._transcodeFormat):t&&e.transcodeFormat===this._transcodeFormat),this._parseNode(t?e.yes:e.no)}}}const E=e=>!(e&e-1)&&0!==e;class F{constructor(){this._transcoderMgr=new u}async decode(e,t,a){const r={...a,...F.DefaultDecoderOptions};return Promise.resolve().then((async()=>{const a=new h(e);if(!a.isValid())throw new Error("Invalid KT2 file: wrong signature");return a.parse(),a.needZSTDDecoder?(this._zstdDecoder||(this._zstdDecoder=new w),this._zstdDecoder.init().then((async()=>this._decodeDataAsync(a,t,r)))):this._decodeDataAsync(a,t,r)}))}async _decodeDataAsync(e,t,s){const o=e.header.pixelWidth,i=e.header.pixelHeight,d=e.textureFormat,c=new O(d,e.hasAlpha,E(o)&&E(i),t,s);s?.transcodeFormatDecisionTree&&c.parseTree(s?.transcodeFormatDecisionTree);const l=c.transcodeFormat,m=c.engineFormat,h=c.roundToMultiple4,_=this._transcoderMgr.findTranscoder(d,l,e.isInGammaSpace,s?.bypassTranscoders);if(null===_)throw new Error(`no transcoder found to transcode source texture format "${a[d]}" to format "${r[l]}"`);const u=[],y=[],f={width:0,height:0,transcodedFormat:m,mipmaps:u,isInGammaSpace:e.isInGammaSpace,hasAlpha:e.hasAlpha,transcoderName:_.getName()};let p=0;for(let t=0;t<e.header.levelCount;t++){t>0&&(p+=Math.max(e.header.layerCount,1)*e.header.faceCount*Math.max(e.header.pixelDepth>>t-1,1));const a=Math.floor(o/(1<<t))||1,r=Math.floor(i/(1<<t))||1,s=e.header.faceCount,c=(a+3>>2)*(r+3>>2)*e.dfdBlock.bytesPlane[0],m=e.levels[t].uncompressedByteLength;let T=e.data.buffer,g=e.levels[t].byteOffset+e.data.byteOffset,B=0;e.header.supercompressionScheme===n.ZStandard&&(T=this._zstdDecoder.decode(new Uint8Array(T,g,e.levels[t].byteLength),m),g=0),0===t&&(f.width=h?a+3&-4:a,f.height=h?r+3&-4:r);for(let o=0;o<s;o++){let s,i=null;e.header.supercompressionScheme===n.BasisLZ?(i=e.supercompressionGlobalData.imageDescs[p+o],s=new Uint8Array(T,g+i.rgbSliceByteOffset,i.rgbSliceByteLength+i.alphaSliceByteLength)):(s=new Uint8Array(T,g+B,c),B+=c);const h={data:null,width:a,height:r},R=_.transcode(d,l,t,a,r,m,e,i,s).then((e=>(h.data=e,e))).catch((e=>(f.errors=f.errors??"",f.errors+=e+"\n"+e.stack+"\n",null)));y.push(R),u.push(h)}}return Promise.all(y).then((()=>f))}}F.DefaultDecoderOptions={},u.RegisterTranscoder(p),u.RegisterTranscoder(T),u.RegisterTranscoder(g),u.RegisterTranscoder(B),u.RegisterTranscoder(R),u.RegisterTranscoder(C),u.RegisterTranscoder(S);const P=void 0!==e.g?e.g:"undefined"!=typeof window?window:void 0;void 0!==P&&(P.KTX2DECODER=F);const L=l;return t.default})()));
1
+ !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("babylonjs-ktx2decoder",[],t):"object"==typeof exports?exports["babylonjs-ktx2decoder"]=t():e.KTX2DECODER=t()}("undefined"!=typeof self?self:"undefined"!=typeof global?global:this,(()=>(()=>{"use strict";var e={d:(t,a)=>{for(var r in a)e.o(a,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:a[r]})}};e.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),e.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),e.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var t={};e.d(t,{default:()=>L});var a,r,s,n,o,i,d,c,l={};e.r(l),e.d(l,{DataReader:()=>m,KTX2Decoder:()=>F,KTX2FileReader:()=>h,LiteTranscoder:()=>f,LiteTranscoder_UASTC_ASTC:()=>p,LiteTranscoder_UASTC_BC7:()=>T,LiteTranscoder_UASTC_R8_UNORM:()=>R,LiteTranscoder_UASTC_RG8_UNORM:()=>C,LiteTranscoder_UASTC_RGBA_SRGB:()=>B,LiteTranscoder_UASTC_RGBA_UNORM:()=>g,MSCTranscoder:()=>S,SupercompressionScheme:()=>n,Transcoder:()=>y,TranscoderManager:()=>u,WASMMemoryManager:()=>_,ZSTDDecoder:()=>b}),function(e){e[e.ETC1S=0]="ETC1S",e[e.UASTC4x4=1]="UASTC4x4"}(a||(a={})),function(e){e[e.ASTC_4X4_RGBA=0]="ASTC_4X4_RGBA",e[e.BC7_RGBA=1]="BC7_RGBA",e[e.BC3_RGBA=2]="BC3_RGBA",e[e.BC1_RGB=3]="BC1_RGB",e[e.PVRTC1_4_RGBA=4]="PVRTC1_4_RGBA",e[e.PVRTC1_4_RGB=5]="PVRTC1_4_RGB",e[e.ETC2_RGBA=6]="ETC2_RGBA",e[e.ETC1_RGB=7]="ETC1_RGB",e[e.RGBA32=8]="RGBA32",e[e.R8=9]="R8",e[e.RG8=10]="RG8"}(r||(r={})),function(e){e[e.COMPRESSED_RGBA_BPTC_UNORM_EXT=36492]="COMPRESSED_RGBA_BPTC_UNORM_EXT",e[e.COMPRESSED_RGBA_ASTC_4X4_KHR=37808]="COMPRESSED_RGBA_ASTC_4X4_KHR",e[e.COMPRESSED_RGB_S3TC_DXT1_EXT=33776]="COMPRESSED_RGB_S3TC_DXT1_EXT",e[e.COMPRESSED_RGBA_S3TC_DXT5_EXT=33779]="COMPRESSED_RGBA_S3TC_DXT5_EXT",e[e.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG=35842]="COMPRESSED_RGBA_PVRTC_4BPPV1_IMG",e[e.COMPRESSED_RGB_PVRTC_4BPPV1_IMG=35840]="COMPRESSED_RGB_PVRTC_4BPPV1_IMG",e[e.COMPRESSED_RGBA8_ETC2_EAC=37496]="COMPRESSED_RGBA8_ETC2_EAC",e[e.COMPRESSED_RGB8_ETC2=37492]="COMPRESSED_RGB8_ETC2",e[e.COMPRESSED_RGB_ETC1_WEBGL=36196]="COMPRESSED_RGB_ETC1_WEBGL",e[e.RGBA8Format=32856]="RGBA8Format",e[e.R8Format=33321]="R8Format",e[e.RG8Format=33323]="RG8Format"}(s||(s={}));class m{get byteOffset(){return this._dataByteOffset}constructor(e,t,a){e.buffer?this._dataView=new DataView(e.buffer,e.byteOffset+(t??0),a??e.byteLength):this._dataView=new DataView(e,t??0,a??e.byteLength),this._dataByteOffset=0}readUint8(){const e=this._dataView.getUint8(this._dataByteOffset);return this._dataByteOffset+=1,e}readInt8(){const e=this._dataView.getInt8(this._dataByteOffset);return this._dataByteOffset+=1,e}readUint16(){const e=this._dataView.getUint16(this._dataByteOffset,!0);return this._dataByteOffset+=2,e}readInt16(){const e=this._dataView.getInt16(this._dataByteOffset,!0);return this._dataByteOffset+=2,e}readUint32(){const e=this._dataView.getUint32(this._dataByteOffset,!0);return this._dataByteOffset+=4,e}readInt32(){const e=this._dataView.getInt32(this._dataByteOffset,!0);return this._dataByteOffset+=4,e}readUint64(){const e=this._dataView.getUint32(this._dataByteOffset,!0)+2**32*this._dataView.getUint32(this._dataByteOffset+4,!0);return this._dataByteOffset+=8,e}readUint8Array(e){const t=new Uint8Array(this._dataView.buffer,this._dataView.byteOffset+this._dataByteOffset,e);return this._dataByteOffset+=e,t}skipBytes(e){return this._dataByteOffset+=e,this}}!function(e){e[e.None=0]="None",e[e.BasisLZ=1]="BasisLZ",e[e.ZStandard=2]="ZStandard",e[e.ZLib=3]="ZLib"}(n||(n={})),function(e){e[e.ETC1S=163]="ETC1S",e[e.UASTC=166]="UASTC"}(o||(o={})),function(e){e[e.RGB=0]="RGB",e[e.RRR=3]="RRR",e[e.GGG=4]="GGG",e[e.AAA=15]="AAA"}(i||(i={})),function(e){e[e.RGB=0]="RGB",e[e.RGBA=3]="RGBA",e[e.RRR=4]="RRR",e[e.RRRG=5]="RRRG"}(d||(d={})),function(e){e[e.linear=1]="linear",e[e.sRGB=2]="sRGB"}(c||(c={}));class h{constructor(e){this._data=e}get data(){return this._data}get header(){return this._header}get levels(){return this._levels}get dfdBlock(){return this._dfdBlock}get supercompressionGlobalData(){return this._supercompressionGlobalData}isValid(){return h.IsValid(this._data)}parse(){let e=12;const t=new m(this._data,e,68),a=this._header={vkFormat:t.readUint32(),typeSize:t.readUint32(),pixelWidth:t.readUint32(),pixelHeight:t.readUint32(),pixelDepth:t.readUint32(),layerCount:t.readUint32(),faceCount:t.readUint32(),levelCount:t.readUint32(),supercompressionScheme:t.readUint32(),dfdByteOffset:t.readUint32(),dfdByteLength:t.readUint32(),kvdByteOffset:t.readUint32(),kvdByteLength:t.readUint32(),sgdByteOffset:t.readUint64(),sgdByteLength:t.readUint64()};if(a.pixelDepth>0)throw new Error("Failed to parse KTX2 file - Only 2D textures are currently supported.");if(a.layerCount>1)throw new Error("Failed to parse KTX2 file - Array textures are not currently supported.");if(a.faceCount>1)throw new Error("Failed to parse KTX2 file - Cube textures are not currently supported.");e+=t.byteOffset;let r=Math.max(1,a.levelCount);const s=new m(this._data,e,3*r*8),n=this._levels=[];for(;r--;)n.push({byteOffset:s.readUint64(),byteLength:s.readUint64(),uncompressedByteLength:s.readUint64()});e+=s.byteOffset;const o=new m(this._data,a.dfdByteOffset,a.dfdByteLength),i=this._dfdBlock={vendorId:o.skipBytes(4).readUint16(),descriptorType:o.readUint16(),versionNumber:o.readUint16(),descriptorBlockSize:o.readUint16(),colorModel:o.readUint8(),colorPrimaries:o.readUint8(),transferFunction:o.readUint8(),flags:o.readUint8(),texelBlockDimension:{x:o.readUint8()+1,y:o.readUint8()+1,z:o.readUint8()+1,w:o.readUint8()+1},bytesPlane:[o.readUint8(),o.readUint8(),o.readUint8(),o.readUint8(),o.readUint8(),o.readUint8(),o.readUint8(),o.readUint8()],numSamples:0,samples:new Array};i.numSamples=(i.descriptorBlockSize-24)/16;for(let e=0;e<i.numSamples;e++){const e={bitOffset:o.readUint16(),bitLength:o.readUint8()+1,channelType:o.readUint8(),channelFlags:0,samplePosition:[o.readUint8(),o.readUint8(),o.readUint8(),o.readUint8()],sampleLower:o.readUint32(),sampleUpper:o.readUint32()};e.channelFlags=(240&e.channelType)>>4,e.channelType=15&e.channelType,i.samples.push(e)}const d=this._supercompressionGlobalData={};if(a.sgdByteLength>0){const e=new m(this._data,a.sgdByteOffset,a.sgdByteLength);d.endpointCount=e.readUint16(),d.selectorCount=e.readUint16(),d.endpointsByteLength=e.readUint32(),d.selectorsByteLength=e.readUint32(),d.tablesByteLength=e.readUint32(),d.extendedByteLength=e.readUint32(),d.imageDescs=[];const t=this._getImageCount();for(let a=0;a<t;a++)d.imageDescs.push({imageFlags:e.readUint32(),rgbSliceByteOffset:e.readUint32(),rgbSliceByteLength:e.readUint32(),alphaSliceByteOffset:e.readUint32(),alphaSliceByteLength:e.readUint32()});const r=a.sgdByteOffset+e.byteOffset,s=r+d.endpointsByteLength,n=s+d.selectorsByteLength,o=n+d.tablesByteLength;d.endpointsData=new Uint8Array(this._data.buffer,this._data.byteOffset+r,d.endpointsByteLength),d.selectorsData=new Uint8Array(this._data.buffer,this._data.byteOffset+s,d.selectorsByteLength),d.tablesData=new Uint8Array(this._data.buffer,this._data.byteOffset+n,d.tablesByteLength),d.extendedData=new Uint8Array(this._data.buffer,this._data.byteOffset+o,d.extendedByteLength)}}_getImageCount(){let e=Math.max(this._header.pixelDepth,1);for(let t=1;t<this._header.levelCount;t++)e+=Math.max(this._header.pixelDepth>>t,1);return Math.max(this._header.layerCount,1)*this._header.faceCount*e}get textureFormat(){return 166===this._dfdBlock.colorModel?a.UASTC4x4:a.ETC1S}get hasAlpha(){switch(this.textureFormat){case a.ETC1S:return 2===this._dfdBlock.numSamples&&(15===this._dfdBlock.samples[0].channelType||15===this._dfdBlock.samples[1].channelType);case a.UASTC4x4:return 3===this._dfdBlock.samples[0].channelType}return!1}get needZSTDDecoder(){return this._header.supercompressionScheme===n.ZStandard}get isInGammaSpace(){return 2===this._dfdBlock.transferFunction}static IsValid(e){if(e.byteLength>=12){const t=new Uint8Array(e.buffer,e.byteOffset,12);if(171===t[0]&&75===t[1]&&84===t[2]&&88===t[3]&&32===t[4]&&50===t[5]&&48===t[6]&&187===t[7]&&13===t[8]&&10===t[9]&&26===t[10]&&10===t[11])return!0}return!1}}class _{static async LoadWASM(e){if(this.LoadBinariesFromCurrentThread)return await new Promise(((t,a)=>{fetch(e).then((async t=>{if(t.ok)return await t.arrayBuffer();throw new Error(`Could not fetch the wasm component from "${e}": ${t.status} - ${t.statusText}`)})).then((e=>t(e))).catch((e=>{a(e)}))}));const t=this._RequestId++;return await new Promise((a=>{const r=e=>{"wasmLoaded"===e.data.action&&e.data.id===t&&(self.removeEventListener("message",r),a(e.data.wasmBinary))};self.addEventListener("message",r),postMessage({action:"loadWASM",path:e,id:t})}))}constructor(e=_.InitialMemoryPages){this._numPages=e,this._memory=new WebAssembly.Memory({initial:this._numPages}),this._memoryViewByteLength=this._numPages<<16,this._memoryViewOffset=0,this._memoryView=new Uint8Array(this._memory.buffer,this._memoryViewOffset,this._memoryViewByteLength)}get wasmMemory(){return this._memory}getMemoryView(e,t=0,a){return a=a??e<<16,this._numPages<e?(this._memory.grow(e-this._numPages),this._numPages=e,this._memoryView=new Uint8Array(this._memory.buffer,t,a),this._memoryViewByteLength=a,this._memoryViewOffset=t):(this._memoryView=new Uint8Array(this._memory.buffer,t,a),this._memoryViewByteLength=a,this._memoryViewOffset=t),this._memoryView}}_.LoadBinariesFromCurrentThread=!0,_.InitialMemoryPages=16,_._RequestId=0;class u{static RegisterTranscoder(e){u._Transcoders.push(e)}findTranscoder(e,t,s,n){let o=null;const i=a[e]+"_"+r[t];for(let a=0;a<u._Transcoders.length;++a)if(u._Transcoders[a].CanTranscode(e,t,s)&&(!n||n.indexOf(u._Transcoders[a].Name)<0)){o=this._getExistingTranscoder(i,u._Transcoders[a].Name),o||(o=new u._Transcoders[a],o.initialize(),o.needMemoryManager()&&(this._wasmMemoryManager||(this._wasmMemoryManager=new _),o.setMemoryManager(this._wasmMemoryManager)),u._TranscoderInstances[i]||(u._TranscoderInstances[i]=[]),u._TranscoderInstances[i].push(o));break}return o}_getExistingTranscoder(e,t){const a=u._TranscoderInstances[e];if(a)for(let e=0;e<a.length;++e){const r=a[e];if(t===r.getName())return r}return null}}u._Transcoders=[],u._TranscoderInstances={};class y{static CanTranscode(e,t,a){return!1}static GetWasmUrl(e){return y.WasmBaseUrl&&e.startsWith("https://cdn.babylonjs.com/")&&(e=e.replace("https://cdn.babylonjs.com/",y.WasmBaseUrl)),e}getName(){return y.Name}initialize(){}needMemoryManager(){return!1}setMemoryManager(e){}async transcode(e,t,a,r,s,n,o,i,d){return null}}y.Name="Transcoder",y.WasmBaseUrl="";class f extends y{constructor(){super(...arguments),this._wasmBinary=null}async _instantiateWebAssemblyAsync(e){return await WebAssembly.instantiate(e,{env:{memory:this._memoryManager.wasmMemory}}).then((e=>({module:e.instance.exports})))}async _loadModuleAsync(e=this._wasmBinary){return this._modulePromise=this._modulePromise||(e?Promise.resolve(e):_.LoadWASM(this._modulePath)).then((async e=>await this._instantiateWebAssemblyAsync(e))),await this._modulePromise}get memoryManager(){return this._memoryManager}setModulePath(e,t){this._modulePath=y.GetWasmUrl(e),this._wasmBinary=t}initialize(){this._transcodeInPlace=!0}needMemoryManager(){return!0}setMemoryManager(e){this._memoryManager=e}async transcode(e,t,a,r,s,n,o,i,d){return await this._loadModuleAsync().then((e=>{const t=e.module,[a,o,i]=this._prepareTranscoding(r,s,n,d);return 0===t.transcode(i)?this._transcodeInPlace?a.slice():o.slice():null}))}_prepareTranscoding(e,t,a,r,s){const n=(e+3>>2)*(t+3>>2);void 0!==s&&(a=e*(t+3>>2)*4*s);const o=1+(16*n+65535+(this._transcodeInPlace?0:a)>>16),i=this.memoryManager.getMemoryView(o,65536,16*n),d=this._transcodeInPlace?null:new Uint8Array(this._memoryManager.wasmMemory.buffer,65536+16*n,void 0!==s?e*t*s:a);return i.set(r),[i,d,n]}}class p extends f{static CanTranscode(e,t,s){return e===a.UASTC4x4&&t===r.ASTC_4X4_RGBA}getName(){return p.Name}initialize(){super.initialize(),this.setModulePath(p.WasmModuleURL,p.WasmBinary)}}p.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_astc.wasm",p.WasmBinary=null,p.Name="UniversalTranscoder_UASTC_ASTC";class T extends f{static CanTranscode(e,t,s){return e===a.UASTC4x4&&t===r.BC7_RGBA}getName(){return T.Name}initialize(){super.initialize(),this.setModulePath(T.WasmModuleURL,T.WasmBinary)}}T.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_bc7.wasm",T.WasmBinary=null,T.Name="UniversalTranscoder_UASTC_BC7";class g extends f{static CanTranscode(e,t,s){return e===a.UASTC4x4&&t===r.RGBA32&&!s}getName(){return g.Name}initialize(){super.initialize(),this._transcodeInPlace=!1,this.setModulePath(g.WasmModuleURL,g.WasmBinary)}async transcode(e,t,a,r,s,n,o,i,d){const c=(await this._loadModuleAsync()).module,[,l]=this._prepareTranscoding(r,s,n,d,4);return 0===c.decode(r,s)?l.slice():null}}g.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_rgba8_unorm_v2.wasm",g.WasmBinary=null,g.Name="UniversalTranscoder_UASTC_RGBA_UNORM";class B extends f{static CanTranscode(e,t,s){return e===a.UASTC4x4&&t===r.RGBA32&&s}getName(){return B.Name}initialize(){super.initialize(),this._transcodeInPlace=!1,this.setModulePath(B.WasmModuleURL,B.WasmBinary)}async transcode(e,t,a,r,s,n,o,i,d){const c=(await this._loadModuleAsync()).module,[,l]=this._prepareTranscoding(r,s,n,d,4);return 0===c.decode(r,s)?l.slice():null}}B.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_rgba8_srgb_v2.wasm",B.WasmBinary=null,B.Name="UniversalTranscoder_UASTC_RGBA_SRGB";class R extends f{static CanTranscode(e,t,s){return e===a.UASTC4x4&&t===r.R8}getName(){return R.Name}initialize(){super.initialize(),this._transcodeInPlace=!1,this.setModulePath(R.WasmModuleURL,R.WasmBinary)}async transcode(e,t,a,r,s,n,o,i,d){const c=(await this._loadModuleAsync()).module,[,l]=this._prepareTranscoding(r,s,n,d,1);return 0===c.decode(r,s)?l.slice():null}}R.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_r8_unorm.wasm",R.WasmBinary=null,R.Name="UniversalTranscoder_UASTC_R8_UNORM";class C extends f{static CanTranscode(e,t,s){return e===a.UASTC4x4&&t===r.RG8}getName(){return C.Name}initialize(){super.initialize(),this._transcodeInPlace=!1,this.setModulePath(C.WasmModuleURL,C.WasmBinary)}async transcode(e,t,a,r,s,n,o,i,d){const c=(await this._loadModuleAsync()).module,[,l]=this._prepareTranscoding(r,s,n,d,2);return 0===c.decode(r,s)?l.slice():null}}C.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/uastc_rg8_unorm.wasm",C.WasmBinary=null,C.Name="UniversalTranscoder_UASTC_RG8_UNORM";class S extends y{getName(){return S.Name}async _getMSCBasisTranscoder(){return this._mscBasisTranscoderPromise||(this._mscBasisTranscoderPromise=(S.WasmBinary?Promise.resolve(S.WasmBinary):_.LoadWASM(y.GetWasmUrl(S.WasmModuleURL))).then((async e=>{if(S.JSModule&&"undefined"==typeof MSC_TRANSCODER)globalThis.MSC_TRANSCODER=S.JSModule;else if(S.UseFromWorkerThread)importScripts(y.GetWasmUrl(S.JSModuleURL));else if("undefined"==typeof MSC_TRANSCODER)return await new Promise(((t,a)=>{const r=document.getElementsByTagName("head")[0],s=document.createElement("script");s.setAttribute("type","text/javascript"),s.setAttribute("src",y.GetWasmUrl(S.JSModuleURL)),s.onload=()=>{"undefined"!=typeof MSC_TRANSCODER?MSC_TRANSCODER({wasmBinary:e}).then((e=>{e.initTranscoders(),this._mscBasisModule=e,t()})):a("MSC_TRANSCODER script loaded but MSC_TRANSCODER is not defined.")},s.onerror=()=>{a("Can not load MSC_TRANSCODER script.")},r.appendChild(s)}));return await new Promise((t=>{MSC_TRANSCODER({wasmBinary:e}).then((e=>{e.initTranscoders(),this._mscBasisModule=e,t()}))}))}))),await this._mscBasisTranscoderPromise}static CanTranscode(e,t,a){return!0}async transcode(e,t,s,n,o,i,d,c,l){const m=!1;return await this._getMSCBasisTranscoder().then((()=>{const h=this._mscBasisModule;let _,u,y,f=null;try{_=e===a.UASTC4x4?new h.UastcImageTranscoder:new h.BasisLzEtc1sImageTranscoder;const f=e===a.UASTC4x4?h.TextureFormat.UASTC4x4:h.TextureFormat.ETC1S;u=new h.ImageInfo(f,n,o,s);const p=h.TranscodeTarget[r[t]];if(!h.isFormatSupported(p,f))throw new Error(`MSCTranscoder: Transcoding from "${a[e]}" to "${r[t]}" not supported by current transcoder build.`);if(e===a.ETC1S){const e=d.supercompressionGlobalData;_.decodePalettes(e.endpointCount,e.endpointsData,e.selectorCount,e.selectorsData),_.decodeTables(e.tablesData),u.flags=c.imageFlags,u.rgbByteOffset=0,u.rgbByteLength=c.rgbSliceByteLength,u.alphaByteOffset=c.alphaSliceByteOffset>0?c.rgbSliceByteLength:0,u.alphaByteLength=c.alphaSliceByteLength,y=_.transcodeImage(p,l,u,0,m)}else u.flags=0,u.rgbByteOffset=0,u.rgbByteLength=i,u.alphaByteOffset=0,u.alphaByteLength=0,y=_.transcodeImage(p,l,u,0,d.hasAlpha,m)}finally{_&&_.delete(),u&&u.delete(),y&&y.transcodedImage&&(f=y.transcodedImage.get_typed_memory_view().slice(),y.transcodedImage.delete())}return f}))}}let w,A,U;S.JSModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/msc_basis_transcoder.js",S.WasmModuleURL="https://cdn.babylonjs.com/ktx2Transcoders/1/msc_basis_transcoder.wasm",S.WasmBinary=null,S.JSModule=null,S.UseFromWorkerThread=!0,S.Name="MSCTranscoder";const M={env:{emscripten_notify_memory_growth:function(){U=new Uint8Array(A.exports.memory.buffer)}}};class b{async init(){return w||(w="undefined"!=typeof fetch?fetch(y.GetWasmUrl(b.WasmModuleURL)).then((async e=>{if(e.ok)return await e.arrayBuffer();throw new Error(`Could not fetch the wasm component for the Zstandard decompression lib: ${e.status} - ${e.statusText}`)})).then((async e=>await WebAssembly.instantiate(e,M))).then(this._init):WebAssembly.instantiateStreaming(fetch(b.WasmModuleURL),M).then(this._init)),await w}_init(e){A=e.instance,M.env.emscripten_notify_memory_growth()}decode(e,t=0){if(!A)throw new Error("ZSTDDecoder: Await .init() before decoding.");const a=e.byteLength,r=A.exports.malloc(a);U.set(e,r),t=t||Number(A.exports.ZSTD_findDecompressedSize(r,a));const s=A.exports.malloc(t),n=A.exports.ZSTD_decompress(s,t,r,a),o=U.slice(s,s+n);return A.exports.free(r),A.exports.free(s),o}}b.WasmModuleURL="https://cdn.babylonjs.com/zstddec.wasm";const G={ETC1S:{option:"forceRGBA",yes:{transcodeFormat:r.RGBA32,engineFormat:32856,roundToMultiple4:!1},no:{cap:"etc2",yes:{alpha:!0,yes:{transcodeFormat:r.ETC2_RGBA,engineFormat:37496},no:{transcodeFormat:r.ETC1_RGB,engineFormat:37492}},no:{cap:"etc1",alpha:!1,yes:{transcodeFormat:r.ETC1_RGB,engineFormat:36196},no:{cap:"bptc",yes:{transcodeFormat:r.BC7_RGBA,engineFormat:36492},no:{cap:"s3tc",yes:{alpha:!0,yes:{transcodeFormat:r.BC3_RGBA,engineFormat:33779},no:{transcodeFormat:r.BC1_RGB,engineFormat:33776}},no:{cap:"pvrtc",needsPowerOfTwo:!0,yes:{alpha:!0,yes:{transcodeFormat:r.PVRTC1_4_RGBA,engineFormat:35842},no:{transcodeFormat:r.PVRTC1_4_RGB,engineFormat:35840}},no:{transcodeFormat:r.RGBA32,engineFormat:32856,roundToMultiple4:!1}}}}}}},UASTC:{option:"forceRGBA",yes:{transcodeFormat:r.RGBA32,engineFormat:32856,roundToMultiple4:!1},no:{option:"forceR8",yes:{transcodeFormat:r.R8,engineFormat:33321,roundToMultiple4:!1},no:{option:"forceRG8",yes:{transcodeFormat:r.RG8,engineFormat:33323,roundToMultiple4:!1},no:{cap:"astc",yes:{transcodeFormat:r.ASTC_4X4_RGBA,engineFormat:37808},no:{cap:"bptc",yes:{transcodeFormat:r.BC7_RGBA,engineFormat:36492},no:{option:"useRGBAIfASTCBC7NotAvailableWhenUASTC",yes:{transcodeFormat:r.RGBA32,engineFormat:32856,roundToMultiple4:!1},no:{cap:"etc2",yes:{alpha:!0,yes:{transcodeFormat:r.ETC2_RGBA,engineFormat:37496},no:{transcodeFormat:r.ETC1_RGB,engineFormat:37492}},no:{cap:"etc1",yes:{transcodeFormat:r.ETC1_RGB,engineFormat:36196},no:{cap:"s3tc",yes:{alpha:!0,yes:{transcodeFormat:r.BC3_RGBA,engineFormat:33779},no:{transcodeFormat:r.BC1_RGB,engineFormat:33776}},no:{cap:"pvrtc",needsPowerOfTwo:!0,yes:{alpha:!0,yes:{transcodeFormat:r.PVRTC1_4_RGBA,engineFormat:35842},no:{transcodeFormat:r.PVRTC1_4_RGB,engineFormat:35840}},no:{transcodeFormat:r.RGBA32,engineFormat:32856,roundToMultiple4:!1}}}}}}}}}}}};class O{static _IsLeafNode(e){return void 0!==e.engineFormat}get transcodeFormat(){return this._transcodeFormat}get engineFormat(){return this._engineFormat}get roundToMultiple4(){return this._roundToMultiple4}constructor(e,t,a,r,s){this._textureFormat=e,this._hasAlpha=t,this._isPowerOfTwo=a,this._caps=r,this._options=s??{},this.parseTree(G)}parseTree(e){const t=this._textureFormat===a.UASTC4x4?e.UASTC:e.ETC1S;return t&&this._parseNode(t),void 0!==t}_parseNode(e){if(e)if(O._IsLeafNode(e))this._transcodeFormat=e.transcodeFormat,this._engineFormat=e.engineFormat,this._roundToMultiple4=e.roundToMultiple4??!0;else{let t=!0;void 0!==e.cap&&(t=t&&!!this._caps[e.cap]),void 0!==e.option&&(t=t&&!!this._options[e.option]),void 0!==e.alpha&&(t=t&&this._hasAlpha===e.alpha),void 0!==e.needsPowerOfTwo&&(t=t&&this._isPowerOfTwo===e.needsPowerOfTwo),void 0!==e.transcodeFormat&&(t=Array.isArray(e.transcodeFormat)?t&&-1!==e.transcodeFormat.indexOf(this._transcodeFormat):t&&e.transcodeFormat===this._transcodeFormat),this._parseNode(t?e.yes:e.no)}}}const E=e=>!(e&e-1)&&0!==e;class F{constructor(){this._transcoderMgr=new u}async decode(e,t,a){const r={...a,...F.DefaultDecoderOptions},s=new h(e);if(!s.isValid())throw new Error("Invalid KT2 file: wrong signature");return s.parse(),s.needZSTDDecoder?(this._zstdDecoder||(this._zstdDecoder=new b),await this._zstdDecoder.init(),await this._decodeDataAsync(s,t,r)):await this._decodeDataAsync(s,t,r)}async _decodeDataAsync(e,t,s){const o=e.header.pixelWidth,i=e.header.pixelHeight,d=e.textureFormat,c=new O(d,e.hasAlpha,E(o)&&E(i),t,s);s?.transcodeFormatDecisionTree&&c.parseTree(s?.transcodeFormatDecisionTree);const l=c.transcodeFormat,m=c.engineFormat,h=c.roundToMultiple4,_=this._transcoderMgr.findTranscoder(d,l,e.isInGammaSpace,s?.bypassTranscoders);if(null===_)throw new Error(`no transcoder found to transcode source texture format "${a[d]}" to format "${r[l]}"`);const u=[],y=[],f={width:0,height:0,transcodedFormat:m,mipmaps:u,isInGammaSpace:e.isInGammaSpace,hasAlpha:e.hasAlpha,transcoderName:_.getName()};let p=0;for(let t=0;t<e.header.levelCount;t++){t>0&&(p+=Math.max(e.header.layerCount,1)*e.header.faceCount*Math.max(e.header.pixelDepth>>t-1,1));const a=Math.floor(o/(1<<t))||1,r=Math.floor(i/(1<<t))||1,s=e.header.faceCount,c=(a+3>>2)*(r+3>>2)*e.dfdBlock.bytesPlane[0],m=e.levels[t].uncompressedByteLength;let T=e.data.buffer,g=e.levels[t].byteOffset+e.data.byteOffset,B=0;e.header.supercompressionScheme===n.ZStandard&&(T=this._zstdDecoder.decode(new Uint8Array(T,g,e.levels[t].byteLength),m),g=0),0===t&&(f.width=h?a+3&-4:a,f.height=h?r+3&-4:r);for(let o=0;o<s;o++){let s,i=null;e.header.supercompressionScheme===n.BasisLZ?(i=e.supercompressionGlobalData.imageDescs[p+o],s=new Uint8Array(T,g+i.rgbSliceByteOffset,i.rgbSliceByteLength+i.alphaSliceByteLength)):(s=new Uint8Array(T,g+B,c),B+=c);const h={data:null,width:a,height:r},R=_.transcode(d,l,t,a,r,m,e,i,s).then((e=>(h.data=e,e))).catch((e=>(f.errors=f.errors??"",f.errors+=e+"\n"+e.stack+"\n",null)));y.push(R),u.push(h)}}return await Promise.all(y),f}}F.DefaultDecoderOptions={},u.RegisterTranscoder(p),u.RegisterTranscoder(T),u.RegisterTranscoder(g),u.RegisterTranscoder(B),u.RegisterTranscoder(R),u.RegisterTranscoder(C),u.RegisterTranscoder(S);const P=void 0!==e.g?e.g:"undefined"!=typeof window?window:void 0;void 0!==P&&(P.KTX2DECODER=F);const L=l;return t.default})()));
2
2
  //# sourceMappingURL=babylon.ktx2Decoder.js.map