1 |
- !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.KTX2DECODER=t():e.KTX2DECODER=t()}("undefined"!=typeof self?self:"undefined"!=typeof global?global:this,(function(){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var a=t[n]={i:n,l:!1,exports:{}};return e[n].call(a.exports,a,a.exports,r),a.l=!0,a.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var a in e)r.d(n,a,function(t){return e[t]}.bind(null,a));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="/dist/",r(r.s=1)}([function(e,t,r){"use strict";r.d(t,"b",(function(){return U})),r.d(t,"h",(function(){return i})),r.d(t,"c",(function(){return d})),r.d(t,"m",(function(){return n})),r.d(t,"n",(function(){return a})),r.d(t,"i",(function(){return s})),r.d(t,"j",(function(){return c})),r.d(t,"k",(function(){return u})),r.d(t,"l",(function(){return w})),r.d(t,"a",(function(){return o})),r.d(t,"d",(function(){return m})),r.d(t,"e",(function(){return _})),r.d(t,"f",(function(){return g})),r.d(t,"g",(function(){return b}));var n,a,o=function(){function e(e,t,r){e.buffer?this._dataView=new DataView(e.buffer,e.byteOffset+(null!=t?t:0),null!=r?r:e.byteLength):this._dataView=new DataView(e,null!=t?t:0,null!=r?r:e.byteLength),this._dataByteOffset=0}return Object.defineProperty(e.prototype,"byteOffset",{get:function(){return this._dataByteOffset},enumerable:!1,configurable:!0}),e.prototype.readUint8=function(){var e=this._dataView.getUint8(this._dataByteOffset);return this._dataByteOffset+=1,e},e.prototype.readInt8=function(){var e=this._dataView.getInt8(this._dataByteOffset);return this._dataByteOffset+=1,e},e.prototype.readUint16=function(){var e=this._dataView.getUint16(this._dataByteOffset,!0);return this._dataByteOffset+=2,e},e.prototype.readInt16=function(){var e=this._dataView.getInt16(this._dataByteOffset,!0);return this._dataByteOffset+=2,e},e.prototype.readUint32=function(){var e=this._dataView.getUint32(this._dataByteOffset,!0);return this._dataByteOffset+=4,e},e.prototype.readInt32=function(){var e=this._dataView.getInt32(this._dataByteOffset,!0);return this._dataByteOffset+=4,e},e.prototype.readUint64=function(){var e=this._dataView.getUint32(this._dataByteOffset,!0),t=this._dataView.getUint32(this._dataByteOffset+4,!0),r=e+Math.pow(2,32)*t;return this._dataByteOffset+=8,r},e.prototype.readUint8Array=function(e){var t=new Uint8Array(this._dataView.buffer,this._dataView.byteOffset+this._dataByteOffset,e);return this._dataByteOffset+=e,t},e.prototype.skipBytes=function(e){return this._dataByteOffset+=e,this},e}();!function(e){e[e.ETC1S=0]="ETC1S",e[e.UASTC4x4=1]="UASTC4x4"}(n||(n={})),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"}(a||(a={}));var i,s=function(){function e(){}return e.CanTranscode=function(e,t){return!1},e.prototype.initialize=function(){},e.prototype.needMemoryManager=function(){return!1},e.prototype.setMemoryManager=function(e){},e.prototype.transcode=function(e,t,r,n,a,o,i,s,d){return Promise.resolve(null)},e}();!function(e){e[e.None=0]="None",e[e.BasisLZ=1]="BasisLZ",e[e.ZStandard=2]="ZStandard",e[e.ZLib=3]="ZLib"}(i||(i={}));var d=function(){function e(e){this._data=e}return Object.defineProperty(e.prototype,"data",{get:function(){return this._data},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"header",{get:function(){return this._header},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"levels",{get:function(){return this._levels},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"dfdBlock",{get:function(){return this._dfdBlock},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"supercompressionGlobalData",{get:function(){return this._supercompressionGlobalData},enumerable:!1,configurable:!0}),e.prototype.isValid=function(){return e.IsValid(this._data)},e.prototype.parse=function(){var e=12,t=new o(this._data,e,68),r=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(r.pixelDepth>0)throw new Error("Failed to parse KTX2 file - Only 2D textures are currently supported.");if(r.layerCount>1)throw new Error("Failed to parse KTX2 file - Array textures are not currently supported.");if(r.faceCount>1)throw new Error("Failed to parse KTX2 file - Cube textures are not currently supported.");e+=t.byteOffset;for(var n=Math.max(1,r.levelCount),a=new o(this._data,e,3*n*8),i=this._levels=[];n--;)i.push({byteOffset:a.readUint64(),byteLength:a.readUint64(),uncompressedByteLength:a.readUint64()});e+=a.byteOffset;var s=new o(this._data,r.dfdByteOffset,r.dfdByteLength),d=this._dfdBlock={vendorId:s.skipBytes(4).readUint16(),descriptorType:s.readUint16(),versionNumber:s.readUint16(),descriptorBlockSize:s.readUint16(),colorModel:s.readUint8(),colorPrimaries:s.readUint8(),transferFunction:s.readUint8(),flags:s.readUint8(),texelBlockDimension:{x:s.readUint8()+1,y:s.readUint8()+1,z:s.readUint8()+1,w:s.readUint8()+1},bytesPlane:[s.readUint8(),s.readUint8(),s.readUint8(),s.readUint8(),s.readUint8(),s.readUint8(),s.readUint8(),s.readUint8()],numSamples:0,samples:new Array};d.numSamples=(d.descriptorBlockSize-24)/16;for(var u=0;u<d.numSamples;u++){var c={bitOffset:s.readUint16(),bitLength:s.readUint8()+1,channelType:s.readUint8(),channelFlags:0,samplePosition:[s.readUint8(),s.readUint8(),s.readUint8(),s.readUint8()],sampleLower:s.readUint32(),sampleUpper:s.readUint32()};c.channelFlags=(240&c.channelType)>>4,c.channelType=15&c.channelType,d.samples.push(c)}var f=this._supercompressionGlobalData={};if(r.sgdByteLength>0){var h=new o(this._data,r.sgdByteOffset,r.sgdByteLength);f.endpointCount=h.readUint16(),f.selectorCount=h.readUint16(),f.endpointsByteLength=h.readUint32(),f.selectorsByteLength=h.readUint32(),f.tablesByteLength=h.readUint32(),f.extendedByteLength=h.readUint32(),f.imageDescs=[];var l=this._getImageCount();for(u=0;u<l;u++)f.imageDescs.push({imageFlags:h.readUint32(),rgbSliceByteOffset:h.readUint32(),rgbSliceByteLength:h.readUint32(),alphaSliceByteOffset:h.readUint32(),alphaSliceByteLength:h.readUint32()});var p=r.sgdByteOffset+h.byteOffset,y=p+f.endpointsByteLength,m=y+f.selectorsByteLength,_=m+f.tablesByteLength;f.endpointsData=new Uint8Array(this._data.buffer,this._data.byteOffset+p,f.endpointsByteLength),f.selectorsData=new Uint8Array(this._data.buffer,this._data.byteOffset+y,f.selectorsByteLength),f.tablesData=new Uint8Array(this._data.buffer,this._data.byteOffset+m,f.tablesByteLength),f.extendedData=new Uint8Array(this._data.buffer,this._data.byteOffset+_,f.extendedByteLength)}},e.prototype._getImageCount=function(){for(var e=Math.max(this._header.pixelDepth,1),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},Object.defineProperty(e.prototype,"textureFormat",{get:function(){return 166===this._dfdBlock.colorModel?n.UASTC4x4:n.ETC1S},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"hasAlpha",{get:function(){switch(this.textureFormat){case n.ETC1S:return 2===this._dfdBlock.numSamples&&(15===this._dfdBlock.samples[0].channelType||15===this._dfdBlock.samples[1].channelType);case n.UASTC4x4:return 3===this._dfdBlock.samples[0].channelType}return!1},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"needZSTDDecoder",{get:function(){return this._header.supercompressionScheme===i.ZStandard},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"isInGammaSpace",{get:function(){return 2===this._dfdBlock.transferFunction},enumerable:!1,configurable:!0}),e.IsValid=function(e){if(e.byteLength>=12){var 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},e}(),u=function(){function e(t){void 0===t&&(t=e.InitialMemoryPages),this._numPages=t,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)}return e.LoadWASM=function(e){if(this.LoadBinariesFromCurrentThread)return new Promise((function(t,r){fetch(e).then((function(t){if(t.ok)return t.arrayBuffer();throw new Error('Could not fetch the wasm component from "'+e+'": '+t.status+" - "+t.statusText)})).then((function(e){return t(e)})).catch((function(e){r(e)}))}));var t=this._RequestId++;return new Promise((function(r){var n=function(e){"wasmLoaded"===e.data.action&&e.data.id===t&&(self.removeEventListener("message",n),r(e.data.wasmBinary))};self.addEventListener("message",n),postMessage({action:"loadWASM",path:e,id:t})}))},Object.defineProperty(e.prototype,"wasmMemory",{get:function(){return this._memory},enumerable:!1,configurable:!0}),e.prototype.getMemoryView=function(e,t,r){return void 0===t&&(t=0),r=null!=r?r:e<<16,this._numPages<e?(this._memory.grow(e-this._numPages),this._numPages=e,this._memoryView=new Uint8Array(this._memory.buffer,t,r),this._memoryViewByteLength=r,this._memoryViewOffset=t):(this._memoryView=new Uint8Array(this._memory.buffer,t,r),this._memoryViewByteLength=r,this._memoryViewOffset=t),this._memoryView},e.LoadBinariesFromCurrentThread=!0,e.InitialMemoryPages=16,e._RequestId=0,e}(),c=function(){function e(){}return e.RegisterTranscoder=function(t){e._Transcoders.push(t)},e.prototype.findTranscoder=function(t,r){for(var o=null,i=0;i<e._Transcoders.length;++i)if(e._Transcoders[i].CanTranscode(t,r)){var s=n[t]+"_"+a[r];(o=e._transcoderInstances[s])||((o=new e._Transcoders[i]).initialize(),o.needMemoryManager()&&(this._wasmMemoryManager||(this._wasmMemoryManager=new u),o.setMemoryManager(this._wasmMemoryManager)),e._transcoderInstances[s]=o);break}return o},e._Transcoders=[],e._transcoderInstances={},e}(),f=function(e,t){return(f=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])})(e,t)};function h(e,t){function r(){this.constructor=e}f(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}Object.create;Object.create;var l,p,y,m=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return h(t,e),t.prototype._loadModule=function(){var e=this;return this._modulePromise||(this._modulePromise=u.LoadWASM(this._modulePath).then((function(t){return new Promise((function(r){WebAssembly.instantiate(t,{env:{memory:e._memoryManager.wasmMemory}}).then((function(e){r({module:e.instance.exports})}))}))}))),this._modulePromise},Object.defineProperty(t.prototype,"memoryManager",{get:function(){return this._memoryManager},enumerable:!1,configurable:!0}),t.prototype.setModulePath=function(e){this._modulePath=e},t.prototype.needMemoryManager=function(){return!0},t.prototype.setMemoryManager=function(e){this._memoryManager=e},t.prototype.transcode=function(e,t,r,n,a,o,i,s,d){var u=this;return this._loadModule().then((function(e){var t=e.module,r=(n+3>>2)*(a+3>>2),o=1+(16*r+65535>>16),i=u.memoryManager.getMemoryView(o,65536,16*r);return i.set(d),0===t.transcode(r)?i.slice():null}))},t}(s),_=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return h(t,e),t.CanTranscode=function(e,t){return e===n.UASTC4x4&&t===a.ASTC_4x4_RGBA},t.prototype.initialize=function(){this.setModulePath(t.WasmModuleURL)},t.WasmModuleURL="https://preview.babylonjs.com/ktx2Transcoders/uastc_astc.wasm",t}(m),g=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return h(t,e),t.CanTranscode=function(e,t){return e===n.UASTC4x4&&t===a.BC7_RGBA},t.prototype.initialize=function(){this.setModulePath(t.WasmModuleURL)},t.WasmModuleURL="https://preview.babylonjs.com/ktx2Transcoders/uastc_bc7.wasm",t}(m),b=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return h(t,e),t.prototype._getMSCBasisTranscoder=function(){var e=this;return this._mscBasisTranscoderPromise||(this._mscBasisTranscoderPromise=u.LoadWASM(t.WasmModuleURL).then((function(r){return t.UseFromWorkerThread&&importScripts(t.JSModuleURL),new Promise((function(t){MSC_TRANSCODER({wasmBinary:r}).then((function(r){r.initTranscoders(),e._mscBasisModule=r,t()}))}))}))),this._mscBasisTranscoderPromise},t.CanTranscode=function(e,t){return!0},t.prototype.transcode=function(e,t,r,o,i,s,d,u,c){var f=this;return this._getMSCBasisTranscoder().then((function(){var h,l=f._mscBasisModule,p=e===n.UASTC4x4?new l.UastcImageTranscoder:new l.BasisLzEtc1sImageTranscoder,y=e===n.UASTC4x4?l.TextureFormat.UASTC4x4:l.TextureFormat.ETC1S,m=new l.ImageInfo(y,o,i,r),_=l.TranscodeTarget[a[t]];if(!l.isFormatSupported(_,y))throw new Error('MSCTranscoder: Transcoding from "'+n[e]+'" to "'+a[t]+'" not supported by current transcoder build.');if(e===n.ETC1S){var g=d.supercompressionGlobalData;p.decodePalettes(g.endpointCount,g.endpointsData,g.selectorCount,g.selectorsData),p.decodeTables(g.tablesData),m.flags=u.imageFlags,m.rgbByteOffset=0,m.rgbByteLength=u.rgbSliceByteLength,m.alphaByteOffset=u.alphaSliceByteOffset>0?u.rgbSliceByteLength:0,m.alphaByteLength=u.alphaSliceByteLength,h=p.transcodeImage(_,c,m,0,!1)}else m.flags=0,m.rgbByteOffset=0,m.rgbByteLength=s,m.alphaByteOffset=0,m.alphaByteLength=0,h=p.transcodeImage(_,c,m,0,d.hasAlpha,!1);if(h&&void 0!==h.transcodedImage){var b=h.transcodedImage.get_typed_memory_view().slice();return h.transcodedImage.delete(),b}return null}))},t.JSModuleURL="https://preview.babylonjs.com/ktx2Transcoders/msc_basis_transcoder.js",t.WasmModuleURL="https://preview.babylonjs.com/ktx2Transcoders/msc_basis_transcoder.wasm",t.UseFromWorkerThread=!0,t}(s),B={env:{emscripten_notify_memory_growth:function(e){y=new Uint8Array(p.exports.memory.buffer)}}},w=function(){function e(){}return e.prototype.init=function(){return l||(l="undefined"!=typeof fetch?fetch(e.WasmModuleURL).then((function(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((function(e){return WebAssembly.instantiate(e,B)})).then(this._init):WebAssembly.instantiateStreaming(fetch(e.WasmModuleURL),B).then(this._init))},e.prototype._init=function(e){p=e.instance,B.env.emscripten_notify_memory_growth(0)},e.prototype.decode=function(e,t){if(void 0===t&&(t=0),!p)throw new Error("ZSTDDecoder: Await .init() before decoding.");var r=e.byteLength,n=p.exports.malloc(r);y.set(e,n),t=t||Number(p.exports.ZSTD_findDecompressedSize(n,r));var a=p.exports.malloc(t),o=p.exports.ZSTD_decompress(a,t,n,r),i=y.slice(a,a+o);return p.exports.free(n),p.exports.free(a),i},e.WasmModuleURL="https://preview.babylonjs.com/zstddec.wasm",e}(),T=function(e){return 0==(e&e-1)&&0!==e},U=function(){function e(){this._transcoderMgr=new c}return e.prototype.decode=function(e,t){var r=this;return Promise.resolve().then((function(){var n=new d(e);if(!n.isValid())throw new Error("Invalid KT2 file: wrong signature");return n.parse(),n.needZSTDDecoder?(r._zstdDecoder||(r._zstdDecoder=new w),r._zstdDecoder.init().then((function(){return r._decodeData(n,t)}))):r._decodeData(n,t)}))},e.prototype._decodeData=function(e,t){var r=e.header.pixelWidth,o=e.header.pixelHeight,s=e.textureFormat,d=T(r)&&T(o),u=-1,c=-1;t.astc?(u=a.ASTC_4x4_RGBA,c=37808):t.bptc?(u=a.BC7_RGBA,c=36492):t.s3tc?(u=e.hasAlpha?a.BC3_RGBA:a.BC1_RGB,c=e.hasAlpha?33779:33776):t.pvrtc&&d?(u=e.hasAlpha?a.PVRTC1_4_RGBA:a.PVRTC1_4_RGB,c=e.hasAlpha?35842:35840):t.etc2?(u=e.hasAlpha?a.ETC2_RGBA:a.ETC1_RGB,c=e.hasAlpha?37496:37492):t.etc1?(u=a.ETC1_RGB,c=36196):(u=a.RGBA32,c=32856);var f=this._transcoderMgr.findTranscoder(s,u);if(null===f)throw new Error('no transcoder found to transcode source texture format "'+n[s]+'" to format "'+a[u]+'"');for(var h=[],l=[],p=[],y={width:0,height:0,transcodedFormat:c,mipmaps:h,isInGammaSpace:e.isInGammaSpace},m=0,_=0;_<e.header.levelCount;_++){_>0&&(m+=Math.max(e.header.layerCount,1)*e.header.faceCount*Math.max(e.header.pixelDepth>>_-1,1));var g=Math.ceil(r/(1<<_)),b=Math.ceil(o/(1<<_)),B=e.header.faceCount,w=(g+3>>2)*(b+3>>2)*e.dfdBlock.bytesPlane[0],U=e.levels[_].uncompressedByteLength,v=e.data.buffer,C=e.levels[_].byteOffset+e.data.byteOffset,O=0;e.header.supercompressionScheme===i.ZStandard&&(v=this._zstdDecoder.decode(new Uint8Array(v,C,e.levels[_].byteLength),U),C=0),0===_&&(y.width=g,y.height=b);for(var S=function(t){var r=void 0,n=null;e.header.supercompressionScheme===i.BasisLZ?(n=e.supercompressionGlobalData.imageDescs[m+t],r=new Uint8Array(v,C+n.rgbSliceByteOffset,n.rgbSliceByteLength+n.alphaSliceByteLength)):(r=new Uint8Array(v,C+O,w),O+=w);var a={data:null,width:g,height:b},o=f.transcode(s,u,_,g,b,U,e,n,r).then((function(e){return a.data=e,e&&p.push(e.buffer),e})).catch((function(e){var t;return y.errors=null!==(t=y.errors)&&void 0!==t?t:"",y.errors+=e+"\n",null}));l.push(o),h.push(a)},M=0;M<B;M++)S(M)}return Promise.all(l).then((function(){return y}))},e}();c.RegisterTranscoder(_),c.RegisterTranscoder(g),c.RegisterTranscoder(b)},function(e,t,r){"use strict";r.r(t),function(e){var n=r(0);r.d(t,"KTX2Decoder",(function(){return n.b})),r.d(t,"SupercompressionScheme",(function(){return n.h})),r.d(t,"KTX2FileReader",(function(){return n.c})),r.d(t,"sourceTextureFormat",(function(){return n.m})),r.d(t,"transcodeTarget",(function(){return n.n})),r.d(t,"Transcoder",(function(){return n.i})),r.d(t,"TranscoderManager",(function(){return n.j})),r.d(t,"WASMMemoryManager",(function(){return n.k})),r.d(t,"ZSTDDecoder",(function(){return n.l})),r.d(t,"DataReader",(function(){return n.a})),r.d(t,"LiteTranscoder",(function(){return n.d})),r.d(t,"LiteTranscoder_UASTC_ASTC",(function(){return n.e})),r.d(t,"LiteTranscoder_UASTC_BC7",(function(){return n.f})),r.d(t,"MSCTranscoder",(function(){return n.g}));var a=void 0!==e?e:"undefined"!=typeof window?window:void 0;void 0!==a&&(a.KTX2DECODER=n.b)}.call(this,r(2))},function(e,t){var r;r=function(){return this}();try{r=r||new Function("return this")()}catch(e){"object"==typeof window&&(r=window)}e.exports=r}])}));
|