Skip to content

Commit

Permalink
Merge pull request #14447 from RaananW/overrideCDNUrls
Browse files Browse the repository at this point in the history
Change script loading architecture
  • Loading branch information
sebavan committed Oct 23, 2023
2 parents 965d7f4 + a06f335 commit 9341036
Show file tree
Hide file tree
Showing 25 changed files with 213 additions and 149 deletions.
4 changes: 2 additions & 2 deletions packages/dev/core/src/Debug/debugLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export class DebugLayer {
* By default it uses the babylonjs CDN.
* @ignoreNaming
*/
public static InspectorURL = `https://unpkg.com/babylonjs-inspector@${Engine.Version}/babylon.inspector.bundle.js`;
public static InspectorURL = `inspector/v${Engine.Version}/babylon.inspector.bundle.js`;

private _scene: Scene;

Expand Down Expand Up @@ -391,7 +391,7 @@ export class DebugLayer {
const inspectorUrl = config && config.inspectorURL ? config.inspectorURL : DebugLayer.InspectorURL;

// Load inspector and add it to the DOM
Tools.LoadScript(inspectorUrl, () => {
Tools.LoadBabylonScript(inspectorUrl, () => {
this._createInspector(config);
resolve(this);
});
Expand Down
15 changes: 4 additions & 11 deletions packages/dev/core/src/Engines/WebGPU/webgpuTintWASM.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { IsWindowObjectExist } from "../../Misc/domManagement";
import { Tools } from "../../Misc/tools";

declare function importScripts(...urls: string[]): void;

/**
* Options to load the associated Twgsl library
*/
Expand All @@ -25,8 +22,8 @@ export interface TwgslOptions {
export class WebGPUTintWASM {
// Default twgsl options.
private static readonly _TWgslDefaultOptions: TwgslOptions = {
jsPath: "https://preview.babylonjs.com/twgsl/twgsl.js",
wasmPath: "https://preview.babylonjs.com/twgsl/twgsl.wasm",
jsPath: "twgsl/twgsl.js",
wasmPath: "twgsl/twgsl.wasm",
};

public static ShowWGSLShaderCode = false;
Expand All @@ -52,15 +49,11 @@ export class WebGPUTintWASM {
}

if (twgslOptions.jsPath && twgslOptions.wasmPath) {
if (IsWindowObjectExist()) {
await Tools.LoadScriptAsync(twgslOptions.jsPath);
} else {
importScripts(twgslOptions.jsPath);
}
await Tools.LoadBabylonScriptAsync(twgslOptions.jsPath);
}

if ((self as any).twgsl) {
WebGPUTintWASM._twgsl = await (self as any).twgsl(twgslOptions!.wasmPath);
WebGPUTintWASM._twgsl = await (self as any).twgsl(Tools.GetBabylonScriptURL(twgslOptions!.wasmPath!));
return Promise.resolve();
}

Expand Down
16 changes: 5 additions & 11 deletions packages/dev/core/src/Engines/webgpuEngine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Logger } from "../Misc/logger";
import { IsWindowObjectExist } from "../Misc/domManagement";
import type { Nullable, DataArray, IndicesArray, Immutable } from "../types";
import { Color4 } from "../Maths/math";
import { Engine } from "../Engines/engine";
Expand Down Expand Up @@ -166,8 +165,8 @@ export interface WebGPUEngineOptions extends ThinEngineOptions, GPURequestAdapte
export class WebGPUEngine extends Engine {
// Default glslang options.
private static readonly _GLSLslangDefaultOptions: GlslangOptions = {
jsPath: "https://preview.babylonjs.com/glslang/glslang.js",
wasmPath: "https://preview.babylonjs.com/glslang/glslang.wasm",
jsPath: "glslang/glslang.js",
wasmPath: "glslang/glslang.wasm",
};

/** true to enable using TintWASM to convert Spir-V to WGSL */
Expand Down Expand Up @@ -742,14 +741,9 @@ export class WebGPUEngine extends Engine {
}

if (glslangOptions.jsPath && glslangOptions.wasmPath) {
if (IsWindowObjectExist()) {
return Tools.LoadScriptAsync(glslangOptions.jsPath).then(() => {
return (self as any).glslang(glslangOptions!.wasmPath);
});
} else {
importScripts(glslangOptions.jsPath);
return (self as any).glslang(glslangOptions!.wasmPath);
}
return Tools.LoadBabylonScriptAsync(glslangOptions.jsPath).then(() => {
return (self as any).glslang(Tools.GetBabylonScriptURL(glslangOptions!.wasmPath!));
});
}

return Promise.reject("gslang is not available.");
Expand Down
4 changes: 2 additions & 2 deletions packages/dev/core/src/Materials/Node/nodeMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class NodeMaterial extends PushMaterial {
private _animationFrame = -1;

/** Define the Url to load node editor script */
public static EditorURL = `https://unpkg.com/babylonjs-node-editor@${Engine.Version}/babylon.nodeEditor.js`;
public static EditorURL = `nodeEditor/v${Engine.Version}/babylon.nodeEditor.js`;

/** Define the Url to load snippets */
public static SnippetUrl = Constants.SnippetUrl;
Expand Down Expand Up @@ -1743,7 +1743,7 @@ export class NodeMaterial extends PushMaterial {
const editorUrl = config && config.editorURL ? config.editorURL : NodeMaterial.EditorURL;

// Load editor and add it to the DOM
Tools.LoadScript(editorUrl, () => {
Tools.LoadBabylonScript(editorUrl, () => {
this.BJSNODEMATERIALEDITOR = this.BJSNODEMATERIALEDITOR || this._getGlobalNodeMaterialEditor();
this._createNodeEditor(config?.nodeEditorConfig);
resolve();
Expand Down
14 changes: 7 additions & 7 deletions packages/dev/core/src/Meshes/Compression/dracoCompression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ export class DracoCompression implements IDisposable {
*/
public static Configuration: IDracoCompressionConfiguration = {
decoder: {
wasmUrl: "https://preview.babylonjs.com/draco_wasm_wrapper_gltf.js",
wasmBinaryUrl: "https://preview.babylonjs.com/draco_decoder_gltf.wasm",
fallbackUrl: "https://preview.babylonjs.com/draco_decoder_gltf.js",
wasmUrl: "draco_wasm_wrapper_gltf.js",
wasmBinaryUrl: "draco_decoder_gltf.wasm",
fallbackUrl: "draco_decoder_gltf.js",
},
};

Expand Down Expand Up @@ -359,11 +359,11 @@ export class DracoCompression implements IDisposable {
const decoderInfo: { url: string | undefined; wasmBinaryPromise: Promise<ArrayBuffer | string | undefined> } =
decoder.wasmUrl && decoder.wasmBinaryUrl && typeof WebAssembly === "object"
? {
url: Tools.GetAbsoluteUrl(decoder.wasmUrl),
wasmBinaryPromise: Tools.LoadFileAsync(Tools.GetAbsoluteUrl(decoder.wasmBinaryUrl)),
url: Tools.GetBabylonScriptURL(decoder.wasmUrl, true),
wasmBinaryPromise: Tools.LoadFileAsync(Tools.GetBabylonScriptURL(decoder.wasmBinaryUrl, true)),
}
: {
url: Tools.GetAbsoluteUrl(decoder.fallbackUrl!),
url: Tools.GetBabylonScriptURL(decoder.fallbackUrl!),
wasmBinaryPromise: Promise.resolve(undefined),
};

Expand Down Expand Up @@ -408,7 +408,7 @@ export class DracoCompression implements IDisposable {
throw new Error("Draco decoder module is not available");
}

return Tools.LoadScriptAsync(decoderInfo.url).then(() => {
return Tools.LoadBabylonScriptAsync(decoderInfo.url).then(() => {
return createDecoderAsync(decoderWasmBinary as ArrayBuffer);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class MeshoptCompression implements IDisposable {
*/
public static Configuration: IMeshoptCompressionConfiguration = {
decoder: {
url: "https://preview.babylonjs.com/meshopt_decoder.js",
url: "meshopt_decoder.js",
},
};

Expand All @@ -78,7 +78,7 @@ export class MeshoptCompression implements IDisposable {
constructor() {
const decoder = MeshoptCompression.Configuration.decoder;

this._decoderModulePromise = Tools.LoadScriptAsync(Tools.GetAbsoluteUrl(decoder.url)).then(() => {
this._decoderModulePromise = Tools.LoadBabylonScriptAsync(decoder.url).then(() => {
// Wait for WebAssembly compilation before resolving promise
return MeshoptDecoder.ready;
});
Expand Down
4 changes: 2 additions & 2 deletions packages/dev/core/src/Meshes/Node/nodeGeometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class NodeGeometry {
private _buildExecutionTime: number = 0;

/** Define the Url to load node editor script */
public static EditorURL = `https://unpkg.com/babylonjs-node-geometry-editor@${Engine.Version}/babylon.nodeGeometryEditor.js`;
public static EditorURL = `nodeGeometryEditor/v${Engine.Version}/babylon.nodeGeometryEditor.js`;

/** Define the Url to load snippets */
public static SnippetUrl = Constants.SnippetUrl;
Expand Down Expand Up @@ -194,7 +194,7 @@ export class NodeGeometry {
const editorUrl = config && config.editorURL ? config.editorURL : NodeGeometry.EditorURL;

// Load editor and add it to the DOM
Tools.LoadScript(editorUrl, () => {
Tools.LoadBabylonScript(editorUrl, () => {
this.BJSNODEGEOMETRYEDITOR = this.BJSNODEGEOMETRYEDITOR || this._getGlobalNodeGeometryEditor();
this._createNodeEditor(config?.nodeGeometryEditorConfig);
resolve();
Expand Down
8 changes: 4 additions & 4 deletions packages/dev/core/src/Misc/basis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ export const BasisToolsOptions = {
/**
* URL to use when loading the basis transcoder
*/
JSModuleURL: "https://cdn.babylonjs.com/basisTranscoder/1/basis_transcoder.js",
JSModuleURL: "basisTranscoder/1/basis_transcoder.js",
/**
* URL to use when loading the wasm module for the transcoder
*/
WasmModuleURL: "https://cdn.babylonjs.com/basisTranscoder/1/basis_transcoder.wasm",
WasmModuleURL: "basisTranscoder/1/basis_transcoder.wasm",
};

/**
Expand Down Expand Up @@ -168,7 +168,7 @@ const _CreateWorkerAsync = () => {
if (_Worker) {
res(_Worker);
} else {
Tools.LoadFileAsync(BasisToolsOptions.WasmModuleURL)
Tools.LoadFileAsync(Tools.GetBabylonScriptURL(BasisToolsOptions.WasmModuleURL))
.then((wasmBinary) => {
if (typeof URL !== "function") {
return reject("Basis transcoder requires an environment with a URL constructor");
Expand All @@ -185,7 +185,7 @@ const _CreateWorkerAsync = () => {
}
};
_Worker.addEventListener("message", initHandler);
_Worker.postMessage({ action: "init", url: BasisToolsOptions.JSModuleURL, wasmBinary: wasmBinary });
_Worker.postMessage({ action: "init", url: Tools.GetBabylonScriptURL(BasisToolsOptions.JSModuleURL), wasmBinary: wasmBinary });
})
.catch(reject);
}
Expand Down
18 changes: 15 additions & 3 deletions packages/dev/core/src/Misc/fileTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export const FileToolsOptions: {
BaseUrl: string;
CorsBehavior: string | ((url: string | string[]) => string);
PreprocessUrl: (url: string) => string;
ScriptBaseUrl: string;
ScriptPreprocessUrl: (url: string) => string;
} = {
/**
* Gets or sets the retry strategy to apply when an error happens while loading an asset.
Expand All @@ -100,9 +102,19 @@ export const FileToolsOptions: {
* Gets or sets a function used to pre-process url before using them to load assets
* @param url
*/
PreprocessUrl: (url: string) => {
return url;
},
PreprocessUrl: (url: string) => url,

/**
* Gets or sets the base URL to use to load scripts
* Used for both JS and WASM
*/
ScriptBaseUrl: "https://cdn.babylonjs.com/",
/**
* Gets or sets a function used to pre-process script url before using them to load.
* Used for both JS and WASM
* @param url defines the url to process
*/
ScriptPreprocessUrl: (url: string) => url,
};

/**
Expand Down
46 changes: 21 additions & 25 deletions packages/dev/core/src/Misc/khronosTextureContainer2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,40 @@ import { EngineFormat, TranscodeTarget } from "core/Materials/Textures/ktx2decod

declare let KTX2DECODER: any;

function getAbsoluteUrlOrNull(url: Nullable<string>): Nullable<string> {
return url ? Tools.GetAbsoluteUrl(url) : null;
}

function applyConfig(urls: typeof KhronosTextureContainer2.URLConfig): void {
if (urls.wasmUASTCToASTC !== null) {
if (urls.wasmUASTCToASTC) {
KTX2DECODER.LiteTranscoder_UASTC_ASTC.WasmModuleURL = urls.wasmUASTCToASTC;
}

if (urls.wasmUASTCToBC7 !== null) {
if (urls.wasmUASTCToBC7) {
KTX2DECODER.LiteTranscoder_UASTC_BC7.WasmModuleURL = urls.wasmUASTCToBC7;
}

if (urls.wasmUASTCToRGBA_UNORM !== null) {
if (urls.wasmUASTCToRGBA_UNORM) {
KTX2DECODER.LiteTranscoder_UASTC_RGBA_UNORM.WasmModuleURL = urls.wasmUASTCToRGBA_UNORM;
}

if (urls.wasmUASTCToRGBA_SRGB !== null) {
if (urls.wasmUASTCToRGBA_SRGB) {
KTX2DECODER.LiteTranscoder_UASTC_RGBA_SRGB.WasmModuleURL = urls.wasmUASTCToRGBA_SRGB;
}

if (urls.wasmUASTCToR8_UNORM !== null) {
if (urls.wasmUASTCToR8_UNORM) {
KTX2DECODER.LiteTranscoder_UASTC_R8_UNORM.WasmModuleURL = urls.wasmUASTCToR8_UNORM;
}

if (urls.wasmUASTCToRG8_UNORM !== null) {
if (urls.wasmUASTCToRG8_UNORM) {
KTX2DECODER.LiteTranscoder_UASTC_RG8_UNORM.WasmModuleURL = urls.wasmUASTCToRG8_UNORM;
}

if (urls.jsMSCTranscoder !== null) {
if (urls.jsMSCTranscoder) {
KTX2DECODER.MSCTranscoder.JSModuleURL = urls.jsMSCTranscoder;
}

if (urls.wasmMSCTranscoder !== null) {
if (urls.wasmMSCTranscoder) {
KTX2DECODER.MSCTranscoder.WasmModuleURL = urls.wasmMSCTranscoder;
}

if (urls.wasmZSTDDecoder !== null) {
if (urls.wasmZSTDDecoder) {
KTX2DECODER.ZSTDDecoder.WasmModuleURL = urls.wasmZSTDDecoder;
}
}
Expand Down Expand Up @@ -245,7 +241,7 @@ export class KhronosTextureContainer2 {
wasmMSCTranscoder: Nullable<string>;
wasmZSTDDecoder: Nullable<string>;
} = {
jsDecoderModule: "https://preview.babylonjs.com/babylon.ktx2Decoder.js",
jsDecoderModule: "babylon.ktx2Decoder.js",
wasmUASTCToASTC: null,
wasmUASTCToBC7: null,
wasmUASTCToRGBA_UNORM: null,
Expand Down Expand Up @@ -285,16 +281,16 @@ export class KhronosTextureContainer2 {
}

const urls = {
jsDecoderModule: Tools.GetAbsoluteUrl(this.URLConfig.jsDecoderModule),
wasmUASTCToASTC: getAbsoluteUrlOrNull(this.URLConfig.wasmUASTCToASTC),
wasmUASTCToBC7: getAbsoluteUrlOrNull(this.URLConfig.wasmUASTCToBC7),
wasmUASTCToRGBA_UNORM: getAbsoluteUrlOrNull(this.URLConfig.wasmUASTCToRGBA_UNORM),
wasmUASTCToRGBA_SRGB: getAbsoluteUrlOrNull(this.URLConfig.wasmUASTCToRGBA_SRGB),
wasmUASTCToR8_UNORM: getAbsoluteUrlOrNull(this.URLConfig.wasmUASTCToR8_UNORM),
wasmUASTCToRG8_UNORM: getAbsoluteUrlOrNull(this.URLConfig.wasmUASTCToRG8_UNORM),
jsMSCTranscoder: getAbsoluteUrlOrNull(this.URLConfig.jsMSCTranscoder),
wasmMSCTranscoder: getAbsoluteUrlOrNull(this.URLConfig.wasmMSCTranscoder),
wasmZSTDDecoder: getAbsoluteUrlOrNull(this.URLConfig.wasmZSTDDecoder),
jsDecoderModule: Tools.GetBabylonScriptURL(this.URLConfig.jsDecoderModule, true),
wasmUASTCToASTC: Tools.GetBabylonScriptURL(this.URLConfig.wasmUASTCToASTC, true),
wasmUASTCToBC7: Tools.GetBabylonScriptURL(this.URLConfig.wasmUASTCToBC7, true),
wasmUASTCToRGBA_UNORM: Tools.GetBabylonScriptURL(this.URLConfig.wasmUASTCToRGBA_UNORM, true),
wasmUASTCToRGBA_SRGB: Tools.GetBabylonScriptURL(this.URLConfig.wasmUASTCToRGBA_SRGB, true),
wasmUASTCToR8_UNORM: Tools.GetBabylonScriptURL(this.URLConfig.wasmUASTCToR8_UNORM, true),
wasmUASTCToRG8_UNORM: Tools.GetBabylonScriptURL(this.URLConfig.wasmUASTCToRG8_UNORM, true),
jsMSCTranscoder: Tools.GetBabylonScriptURL(this.URLConfig.jsMSCTranscoder, true),
wasmMSCTranscoder: Tools.GetBabylonScriptURL(this.URLConfig.wasmMSCTranscoder, true),
wasmZSTDDecoder: Tools.GetBabylonScriptURL(this.URLConfig.wasmZSTDDecoder, true),
};

if (numWorkers && typeof Worker === "function" && typeof URL !== "undefined") {
Expand Down Expand Up @@ -334,7 +330,7 @@ export class KhronosTextureContainer2 {
);
});
} else if (typeof KTX2DECODER === "undefined") {
KhronosTextureContainer2._DecoderModulePromise = Tools.LoadScriptAsync(urls.jsDecoderModule).then(() => {
KhronosTextureContainer2._DecoderModulePromise = Tools.LoadBabylonScriptAsync(urls.jsDecoderModule).then(() => {
KTX2DECODER.MSCTranscoder.UseFromWorkerThread = false;
KTX2DECODER.WASMMemoryManager.LoadBinariesFromCurrentThread = true;
applyConfig(urls);
Expand Down

0 comments on commit 9341036

Please sign in to comment.