khronosTextureContainer2Worker.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. export function applyConfig(urls, binariesAndModulesContainer) {
  2. const KTX2DecoderModule = binariesAndModulesContainer?.jsDecoderModule || KTX2DECODER;
  3. if (urls) {
  4. if (urls.wasmUASTCToASTC) {
  5. KTX2DecoderModule.LiteTranscoder_UASTC_ASTC.WasmModuleURL = urls.wasmUASTCToASTC;
  6. }
  7. if (urls.wasmUASTCToBC7) {
  8. KTX2DecoderModule.LiteTranscoder_UASTC_BC7.WasmModuleURL = urls.wasmUASTCToBC7;
  9. }
  10. if (urls.wasmUASTCToRGBA_UNORM) {
  11. KTX2DecoderModule.LiteTranscoder_UASTC_RGBA_UNORM.WasmModuleURL = urls.wasmUASTCToRGBA_UNORM;
  12. }
  13. if (urls.wasmUASTCToRGBA_SRGB) {
  14. KTX2DecoderModule.LiteTranscoder_UASTC_RGBA_SRGB.WasmModuleURL = urls.wasmUASTCToRGBA_SRGB;
  15. }
  16. if (urls.wasmUASTCToR8_UNORM) {
  17. KTX2DecoderModule.LiteTranscoder_UASTC_R8_UNORM.WasmModuleURL = urls.wasmUASTCToR8_UNORM;
  18. }
  19. if (urls.wasmUASTCToRG8_UNORM) {
  20. KTX2DecoderModule.LiteTranscoder_UASTC_RG8_UNORM.WasmModuleURL = urls.wasmUASTCToRG8_UNORM;
  21. }
  22. if (urls.jsMSCTranscoder) {
  23. KTX2DecoderModule.MSCTranscoder.JSModuleURL = urls.jsMSCTranscoder;
  24. }
  25. if (urls.wasmMSCTranscoder) {
  26. KTX2DecoderModule.MSCTranscoder.WasmModuleURL = urls.wasmMSCTranscoder;
  27. }
  28. if (urls.wasmZSTDDecoder) {
  29. KTX2DecoderModule.ZSTDDecoder.WasmModuleURL = urls.wasmZSTDDecoder;
  30. }
  31. }
  32. if (binariesAndModulesContainer) {
  33. if (binariesAndModulesContainer.wasmUASTCToASTC) {
  34. KTX2DecoderModule.LiteTranscoder_UASTC_ASTC.WasmBinary = binariesAndModulesContainer.wasmUASTCToASTC;
  35. }
  36. if (binariesAndModulesContainer.wasmUASTCToBC7) {
  37. KTX2DecoderModule.LiteTranscoder_UASTC_BC7.WasmBinary = binariesAndModulesContainer.wasmUASTCToBC7;
  38. }
  39. if (binariesAndModulesContainer.wasmUASTCToRGBA_UNORM) {
  40. KTX2DecoderModule.LiteTranscoder_UASTC_RGBA_UNORM.WasmBinary = binariesAndModulesContainer.wasmUASTCToRGBA_UNORM;
  41. }
  42. if (binariesAndModulesContainer.wasmUASTCToRGBA_SRGB) {
  43. KTX2DecoderModule.LiteTranscoder_UASTC_RGBA_SRGB.WasmBinary = binariesAndModulesContainer.wasmUASTCToRGBA_SRGB;
  44. }
  45. if (binariesAndModulesContainer.wasmUASTCToR8_UNORM) {
  46. KTX2DecoderModule.LiteTranscoder_UASTC_R8_UNORM.WasmBinary = binariesAndModulesContainer.wasmUASTCToR8_UNORM;
  47. }
  48. if (binariesAndModulesContainer.wasmUASTCToRG8_UNORM) {
  49. KTX2DecoderModule.LiteTranscoder_UASTC_RG8_UNORM.WasmBinary = binariesAndModulesContainer.wasmUASTCToRG8_UNORM;
  50. }
  51. if (binariesAndModulesContainer.jsMSCTranscoder) {
  52. KTX2DecoderModule.MSCTranscoder.JSModule = binariesAndModulesContainer.jsMSCTranscoder;
  53. }
  54. if (binariesAndModulesContainer.wasmMSCTranscoder) {
  55. KTX2DecoderModule.MSCTranscoder.WasmBinary = binariesAndModulesContainer.wasmMSCTranscoder;
  56. }
  57. if (binariesAndModulesContainer.wasmZSTDDecoder) {
  58. KTX2DecoderModule.ZSTDDecoder.WasmBinary = binariesAndModulesContainer.wasmZSTDDecoder;
  59. }
  60. }
  61. }
  62. export function workerFunction(KTX2DecoderModule) {
  63. if (typeof KTX2DecoderModule === "undefined" && typeof KTX2DECODER !== "undefined") {
  64. KTX2DecoderModule = KTX2DECODER;
  65. }
  66. let ktx2Decoder;
  67. onmessage = (event) => {
  68. if (!event.data) {
  69. return;
  70. }
  71. switch (event.data.action) {
  72. case "init": {
  73. const urls = event.data.urls;
  74. if (urls) {
  75. if (urls.jsDecoderModule && typeof KTX2DecoderModule === "undefined") {
  76. importScripts(urls.jsDecoderModule);
  77. // assuming global namespace populated by the script (UMD pattern)
  78. KTX2DecoderModule = KTX2DECODER;
  79. }
  80. applyConfig(urls);
  81. }
  82. if (event.data.wasmBinaries) {
  83. applyConfig(undefined, { ...event.data.wasmBinaries, jsDecoderModule: KTX2DecoderModule });
  84. }
  85. ktx2Decoder = new KTX2DecoderModule.KTX2Decoder();
  86. postMessage({ action: "init" });
  87. break;
  88. }
  89. case "setDefaultDecoderOptions": {
  90. KTX2DecoderModule.KTX2Decoder.DefaultDecoderOptions = event.data.options;
  91. break;
  92. }
  93. case "decode":
  94. ktx2Decoder
  95. .decode(event.data.data, event.data.caps, event.data.options)
  96. .then((data) => {
  97. const buffers = [];
  98. for (let mip = 0; mip < data.mipmaps.length; ++mip) {
  99. const mipmap = data.mipmaps[mip];
  100. if (mipmap && mipmap.data) {
  101. buffers.push(mipmap.data.buffer);
  102. }
  103. }
  104. postMessage({ action: "decoded", success: true, decodedData: data }, buffers);
  105. })
  106. .catch((reason) => {
  107. postMessage({ action: "decoded", success: false, msg: reason });
  108. });
  109. break;
  110. }
  111. };
  112. }
  113. export function initializeWebWorker(worker, wasmBinaries, urls) {
  114. return new Promise((resolve, reject) => {
  115. const onError = (error) => {
  116. worker.removeEventListener("error", onError);
  117. worker.removeEventListener("message", onMessage);
  118. reject(error);
  119. };
  120. const onMessage = (message) => {
  121. if (message.data.action === "init") {
  122. worker.removeEventListener("error", onError);
  123. worker.removeEventListener("message", onMessage);
  124. resolve(worker);
  125. }
  126. };
  127. worker.addEventListener("error", onError);
  128. worker.addEventListener("message", onMessage);
  129. worker.postMessage({
  130. action: "init",
  131. urls,
  132. wasmBinaries,
  133. });
  134. });
  135. }
  136. //# sourceMappingURL=khronosTextureContainer2Worker.js.map