@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 3.63 kB
JavaScript
import{WasmModule as e}from"../../core/wasm-module.js";import{PIXELFORMAT_RGB565 as t,PIXELFORMAT_RGBA4 as r}from"../../platform/graphics/constants.js";import{BasisWorker as s}from"./basis-worker.js";import{http as l}from"../../platform/net/http.js";const i=e=>({astc:!!e.extCompressedTextureASTC,atc:!!e.extCompressedTextureATC,dxt:!!e.extCompressedTextureS3TC,etc1:!!e.extCompressedTextureETC1,etc2:!!e.extCompressedTextureETC,pvr:!!e.extCompressedTexturePVRTC});class a{constructor(e,t,r){this.queue=e,this.worker=new Worker(t.workerUrl),this.worker.addEventListener("message",(e=>{const t=e.data;this.queue.handleResponse(t.url,t.err,t.data),this.eager||this.queue.enqueueClient(this)})),this.worker.postMessage({type:"init",config:t}),this.eager=r}run(e){const t=[];e.data instanceof ArrayBuffer&&t.push(e.data),this.worker.postMessage({type:"transcode",url:e.url,format:e.format,data:e.data,options:e.options},t),this.eager&&this.queue.enqueueClient(this)}}const n=["etc1","etc2","astc","dxt","pvr","atc"],o=["astc","dxt","etc2","pvr","atc"],c=new class{constructor(){this.callbacks={},this.queue=[],this.clients=[]}enqueueJob(e,t,r,s){if(this.callbacks.hasOwnProperty(e))this.callbacks[e].push(r);else{this.callbacks[e]=[r];const l={url:e,data:t,options:s};this.clients.length>0?this.clients.shift().run(l):this.queue.push(l)}}enqueueClient(e){this.queue.length>0?e.run(this.queue.shift()):this.clients.push(e)}handleResponse(e,s,l){const i=this.callbacks[e];if(s)for(let e=0;e<i.length;++e)i[e](s);else{l.format===t||l.format===r?l.levels=l.levels.map((e=>new Uint16Array(e))):l.levels=l.levels.map((e=>new Uint8Array(e)));for(let e=0;e<i.length;++e)i[e](null,l)}delete this.callbacks[e]}};let u=null,m=!1;function h(t){if(!m){if(t){if(t.lazyInit)return void(u=t)}else t=u||{};if(!t.glueUrl||!t.wasmUrl||!t.fallbackUrl){const r=e.getConfig("BASIS");r&&(t={glueUrl:r.glueUrl,wasmUrl:r.wasmUrl,fallbackUrl:r.fallbackUrl,numWorkers:r.numWorkers})}if(t.glueUrl||t.wasmUrl||t.fallbackUrl){m=!0;const e=Math.max(1,Math.min(16,t.numWorkers||1)),r=1===t.numWorkers||!t.hasOwnProperty("eagerWorkers")||t.eagerWorkers;t.rgbPriority=t.rgbPriority||n,t.rgbaPriority=t.rgbaPriority||o,t.maxRetries=t.hasOwnProperty("maxRetries")?t.maxRetries:5,((e,t)=>{const r=e=>{const t=["/* basis */",e,"",`(${s.toString()})()\n\n`].join("\n");return new Blob([t],{type:"application/javascript"})},i=(s,l)=>{t(null,{workerUrl:URL.createObjectURL(r(s)),module:l,rgbPriority:e.rgbPriority,rgbaPriority:e.rgbaPriority})},a={cache:!0,responseType:"text",retry:e.maxRetries>0,maxRetries:e.maxRetries};if(e.glueUrl&&e.wasmUrl&&(()=>{try{if("object"==typeof WebAssembly&&"function"==typeof WebAssembly.instantiate){const e=new WebAssembly.Module(Uint8Array.of(0,97,115,109,1,0,0,0));if(e instanceof WebAssembly.Module)return new WebAssembly.Instance(e)instanceof WebAssembly.Instance}}catch(e){}return!1})()){let r=null,s=null;l.get(e.glueUrl,a,((e,l)=>{e?t(e):s?i(l,s):r=l}));const n=fetch(e.wasmUrl),o=()=>{n.then((e=>e.arrayBuffer())).then((e=>WebAssembly.compile(e))).then((e=>{r?i(r,e):s=e})).catch((e=>{t(e,null)}))};WebAssembly.compileStreaming?WebAssembly.compileStreaming(n).then((e=>{r?i(r,e):s=e})).catch((e=>{o()})):o()}else l.get(e.fallbackUrl,a,((e,r)=>{e?t(e,null):i(r,null)}))})(t,((t,s)=>{if(t)console.error(`failed to initialize basis worker: ${t}`);else for(let t=0;t<e;++t)c.enqueueClient(new a(c,s,r))}))}}}let f=null;function b(e,t,r,s,l){return h(),f||(f={webgl2:e.isWebGL2,formats:i(e)}),c.enqueueJob(t,r,s,{deviceDetails:f,isGGGR:!(null==l||!l.isGGGR),isKTX2:!(null==l||!l.isKTX2)}),m}export{h as basisInitialize,b as basisTranscode};