Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change script loading architecture #14447

Merged
merged 9 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion 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
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.LoadScriptAsync(twgslOptions.jsPath);
}
Popov72 marked this conversation as resolved.
Show resolved Hide resolved

if ((self as any).twgsl) {
WebGPUTintWASM._twgsl = await (self as any).twgsl(twgslOptions!.wasmPath);
WebGPUTintWASM._twgsl = await (self as any).twgsl(Tools.GetScriptUrl(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.LoadScriptAsync(glslangOptions.jsPath).then(() => {
Popov72 marked this conversation as resolved.
Show resolved Hide resolved
return (self as any).glslang(Tools.GetScriptUrl(glslangOptions!.wasmPath!));
});
}

return Promise.reject("gslang is not available.");
Expand Down
2 changes: 1 addition & 1 deletion 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
12 changes: 6 additions & 6 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.GetScriptUrl(decoder.wasmUrl, true),
wasmBinaryPromise: Tools.LoadFileAsync(Tools.GetScriptUrl(decoder.wasmBinaryUrl, true)),
}
: {
url: Tools.GetAbsoluteUrl(decoder.fallbackUrl!),
url: Tools.GetScriptUrl(decoder.fallbackUrl!),
wasmBinaryPromise: Promise.resolve(undefined),
};

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.LoadScriptAsync(Tools.GetScriptUrl(decoder.url, true)).then(() => {
// Wait for WebAssembly compilation before resolving promise
return MeshoptDecoder.ready;
});
Expand Down
2 changes: 1 addition & 1 deletion 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
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.GetScriptUrl(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.GetScriptUrl(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
44 changes: 20 additions & 24 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.GetScriptUrl(this.URLConfig.jsDecoderModule, true),
wasmUASTCToASTC: Tools.GetScriptUrl(this.URLConfig.wasmUASTCToASTC, true),
wasmUASTCToBC7: Tools.GetScriptUrl(this.URLConfig.wasmUASTCToBC7, true),
wasmUASTCToRGBA_UNORM: Tools.GetScriptUrl(this.URLConfig.wasmUASTCToRGBA_UNORM, true),
wasmUASTCToRGBA_SRGB: Tools.GetScriptUrl(this.URLConfig.wasmUASTCToRGBA_SRGB, true),
wasmUASTCToR8_UNORM: Tools.GetScriptUrl(this.URLConfig.wasmUASTCToR8_UNORM, true),
wasmUASTCToRG8_UNORM: Tools.GetScriptUrl(this.URLConfig.wasmUASTCToRG8_UNORM, true),
jsMSCTranscoder: Tools.GetScriptUrl(this.URLConfig.jsMSCTranscoder, true),
wasmMSCTranscoder: Tools.GetScriptUrl(this.URLConfig.wasmMSCTranscoder, true),
wasmZSTDDecoder: Tools.GetScriptUrl(this.URLConfig.wasmZSTDDecoder, true),
};

if (numWorkers && typeof Worker === "function" && typeof URL !== "undefined") {
Expand Down
63 changes: 61 additions & 2 deletions packages/dev/core/src/Misc/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,39 @@ export class Tools {
FileToolsOptions.BaseUrl = value;
}

/**
* This function checks whether a URL is absolute or not.
* It will also detect data and blob URLs
* @param url the url to check
* @returns is the url absolute or relative
*/
public static IsAbsoluteUrl(url: string): boolean {
bghgary marked this conversation as resolved.
Show resolved Hide resolved
return url.indexOf("://") !== -1 || url.indexOf("//") === 0 || url.indexOf("data:") === 0 || url.indexOf("blob:") === 0;
}
RaananW marked this conversation as resolved.
Show resolved Hide resolved

/**
* Sets the base URL to use to load scripts
*/
public static set ScriptBaseUrl(value: string) {
FileToolsOptions.ScriptBaseUrl = value;
}

public static get ScriptBaseUrl(): string {
return FileToolsOptions.ScriptBaseUrl;
}

/**
* Sets a preprocessing function to run on a source URL before importing it
* Note that this function will execute AFTER the base URL is appended to the URL
*/
public static set ScriptPreprocessUrl(func: (source: string) => string) {
FileToolsOptions.ScriptPreprocessUrl = func;
}

public static get ScriptPreprocessUrl(): (source: string) => string {
return FileToolsOptions.ScriptPreprocessUrl;
}

/**
* Enable/Disable Custom HTTP Request Headers globally.
* default = false
Expand All @@ -70,7 +103,7 @@ export class Tools {
}

/**
* Default behaviour for cors in the application.
* Default behavior for cors in the application.
* It can be a string if the expected behavior is identical in the entire app.
* Or a callback to be able to set it per url or on a group of them (in case of Video source for instance)
*/
Expand Down Expand Up @@ -416,15 +449,41 @@ export class Tools {
});
}

/**
* Get a script URL including preprocessing
* @param scriptUrl the script Url to process
* @returns a modified URL to use
*/
public static GetScriptUrl(scriptUrl: Nullable<string>, forceAbsoluteUrl?: boolean): string {
sebavan marked this conversation as resolved.
Show resolved Hide resolved
if (!scriptUrl) {
return "";
}
// check if the URL is absolute or relative. Otherwise, append the base URL
if (!Tools.IsAbsoluteUrl(scriptUrl)) {
scriptUrl = Tools.ScriptBaseUrl + (scriptUrl[0] === "/" ? scriptUrl.substring(1) : scriptUrl);
RaananW marked this conversation as resolved.
Show resolved Hide resolved
}

// run the preprocessor
scriptUrl = Tools.ScriptPreprocessUrl(scriptUrl);

if (forceAbsoluteUrl) {
scriptUrl = Tools.GetAbsoluteUrl(scriptUrl);
}

return scriptUrl;
}

/**
* Load a script (identified by an url). When the url returns, the
* content of this file is added into a new script element, attached to the DOM (body element)
* @param scriptUrl defines the url of the script to laod
* @param scriptUrl defines the url of the script to load
* @param onSuccess defines the callback called when the script is loaded
* @param onError defines the callback to call if an error occurs
* @param scriptId defines the id of the script element
*/
public static LoadScript(scriptUrl: string, onSuccess: () => void, onError?: (message?: string, exception?: any) => void, scriptId?: string) {
scriptUrl = Tools.GetScriptUrl(scriptUrl);
RaananW marked this conversation as resolved.
Show resolved Hide resolved

if (typeof importScripts === "function") {
try {
importScripts(scriptUrl);
Expand Down