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

Add loader for Shader and ShaderChunk asset #2027

Merged
merged 46 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
b5fa1e6
feat: add loader for shader asset
Sway007 Mar 11, 2024
d8d9a87
feat: compatible with 1.2-pre version
Sway007 Mar 12, 2024
4f575fc
Merge branch 'main' of https://github.com/galacean/engine into feat/s…
Sway007 Mar 18, 2024
a5f76dc
feat: opt code
Sway007 Mar 18, 2024
d2d14d1
Merge branch 'main' of https://github.com/galacean/engine into feat/s…
Sway007 Apr 16, 2024
326afea
fix: keep macro declaration
Sway007 Apr 26, 2024
a46c5dc
test: add unitest
Sway007 Apr 26, 2024
bfd4b1d
fix: macro function & line change
Sway007 May 6, 2024
5fe627d
fix: test case
Sway007 May 6, 2024
cfdc44d
Merge branch 'fix/shaderlab/macro' into feat/shaderloader
Sway007 May 7, 2024
d65e88a
"v0.0.0-experimental-shaderlab.0"
Sway007 May 7, 2024
81ba305
"v0.0.0-experimental-shaderlab.1"
Sway007 May 7, 2024
470dc62
fix: recursive chunck load
Sway007 May 7, 2024
6c7436a
"v0.0.0-experimental-shaderlab.2"
zhuxudong May 7, 2024
b173007
feat: hide ShaderChunk type
Sway007 Oct 31, 2024
006a08c
Merge branch 'main' of https://github.com/galacean/engine into feat/s…
Sway007 Oct 31, 2024
0addd19
feat: add types
Sway007 Oct 31, 2024
f9434bb
feat: code opt
Sway007 Oct 31, 2024
d12640e
feat: code opt
Sway007 Oct 31, 2024
4b64817
feat: opt regex
Sway007 Oct 31, 2024
d351bae
feat: support relative include in shader loader
Sway007 Oct 31, 2024
2df9acb
refactor: loader api
Sway007 Nov 4, 2024
2e3c530
feat: code opt
Sway007 Nov 5, 2024
882229f
feat: code opt
Sway007 Nov 5, 2024
e747bae
feat: code opt
Sway007 Nov 5, 2024
0927b2d
feat: code opt
Sway007 Nov 5, 2024
c0e2a60
feat: code opt
Sway007 Nov 5, 2024
c9c9f11
feat: refactor loader request
Sway007 Nov 6, 2024
dd13a56
feat: code opt
Sway007 Nov 6, 2024
0e8ddb1
feat: code opt
Sway007 Nov 6, 2024
f0dcf1e
feat: code opt
Sway007 Nov 6, 2024
44598d8
feat: code opt
Sway007 Nov 6, 2024
435088c
fix: shader loader bug with query
GuoLei1990 Nov 6, 2024
d91596a
fix: opt code
GuoLei1990 Nov 6, 2024
ecda84f
fix: shader loader url
Sway007 Nov 6, 2024
ffe8611
feat: code opt
Sway007 Nov 6, 2024
11eaef8
feat: code opt
Sway007 Nov 6, 2024
f96d0ab
feat: code opt
Sway007 Nov 6, 2024
d660f94
feat: code opt
Sway007 Nov 6, 2024
6271ba4
feat: code opt
Sway007 Nov 6, 2024
2aacca9
feat: code opt
Sway007 Nov 6, 2024
1a10beb
feat: code opt
Sway007 Nov 6, 2024
744adc0
feat: code opt
Sway007 Nov 6, 2024
0f11490
feat: code opt
Sway007 Nov 6, 2024
aaeca8a
feat: code opt
Sway007 Nov 6, 2024
9c0acf9
feat: code opt
Sway007 Nov 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/src/asset/AssetType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export enum AssetType {
TextureCube = "TextureCube",
/** Material. */
Material = "Material",
/** Shader. */
Shader = "Shader",
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
/** Mesh. */
Mesh = "Mesh",
/** AnimationClip. */
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/asset/Loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@ export abstract class Loader<T> {
constructor(public readonly useCache: boolean) {}
initialize?(engine: Engine, configuration: EngineConfiguration): Promise<void>;
abstract load(item: LoadItem, resourceManager: ResourceManager): AssetPromise<T>;
request: <U>(url: string, config: RequestConfig) => AssetPromise<U> = request;
request<U>(url: string, resourceManager: ResourceManager, config: RequestConfig): AssetPromise<U> {
const remoteUrl = resourceManager._virtualPathMap[url] ?? url;
return request(remoteUrl, config);
}
}
80 changes: 47 additions & 33 deletions packages/core/src/asset/ResourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,14 @@
* @internal
*/
_onSubAssetSuccess<T>(assetBaseURL: string, assetSubPath: string, value: T): void {
const subPromiseCallback = this._subAssetPromiseCallbacks[assetBaseURL]?.[assetSubPath];
const remoteAssetBaseURL = this._virtualPathMap[assetBaseURL] ?? assetBaseURL;

const subPromiseCallback = this._subAssetPromiseCallbacks[remoteAssetBaseURL]?.[assetSubPath];
if (subPromiseCallback) {
subPromiseCallback.resolve(value);
} else {
// Pending
(this._subAssetPromiseCallbacks[assetBaseURL] ||= {})[assetSubPath] = {
(this._subAssetPromiseCallbacks[remoteAssetBaseURL] ||= {})[assetSubPath] = {
resolvedValue: value
};
}
Expand Down Expand Up @@ -326,10 +328,7 @@

private _loadSingleItem<T>(itemOrURL: LoadItem | string): AssetPromise<T> {
const item = this._assignDefaultOptions(typeof itemOrURL === "string" ? { url: itemOrURL } : itemOrURL);

// Check url mapping
const itemURL = item.url;
let url = this._virtualPathMap[itemURL] ? this._virtualPathMap[itemURL] : itemURL;
let { url } = item;

// Not absolute and base url is set
if (!Utils.isAbsoluteUrl(url) && this.baseUrl) url = Utils.resolveAbsoluteUrl(this.baseUrl, url);
Expand All @@ -338,27 +337,30 @@
const { assetBaseURL, queryPath } = this._parseURL(url);
const paths = queryPath ? this._parseQueryPath(queryPath) : [];

// Get remote asset base url
const remoteAssetBaseURL = this._virtualPathMap[assetBaseURL] ?? assetBaseURL;

// Check cache
const cacheObject = this._assetUrlPool[assetBaseURL];
const cacheObject = this._assetUrlPool[remoteAssetBaseURL];
if (cacheObject) {
return new AssetPromise((resolve) => {
resolve(this._getResolveResource(cacheObject, paths) as T);
});
}

// Get asset url
let assetURL = assetBaseURL;
let remoteAssetURL = remoteAssetBaseURL;
if (queryPath) {
assetURL += "?q=" + paths.shift();
remoteAssetURL += "?q=" + paths.shift();
let index: string;
while ((index = paths.shift())) {
assetURL += `[${index}]`;
remoteAssetURL += `[${index}]`;
}
}

// Check is loading
const loadingPromises = this._loadingPromises;
const loadingPromise = loadingPromises[assetURL];
const loadingPromise = loadingPromises[remoteAssetURL];
if (loadingPromise) {
return new AssetPromise((resolve, reject, setTaskCompleteProgress, setTaskDetailProgress) => {
loadingPromise
Expand All @@ -381,47 +383,53 @@
// Check sub asset
if (queryPath) {
// Check whether load main asset
const mainPromise = loadingPromises[assetBaseURL] || this._loadMainAsset(loader, item, assetBaseURL);
const mainPromise =
loadingPromises[remoteAssetBaseURL] || this._loadMainAsset(loader, item, remoteAssetBaseURL, assetBaseURL);
mainPromise.catch((e) => {
this._onSubAssetFail(assetBaseURL, queryPath, e);
this._onSubAssetFail(remoteAssetBaseURL, queryPath, e);

Check warning on line 389 in packages/core/src/asset/ResourceManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/asset/ResourceManager.ts#L389

Added line #L389 was not covered by tests
});

return this._createSubAssetPromiseCallback<T>(assetBaseURL, assetURL, queryPath);
return this._createSubAssetPromiseCallback<T>(remoteAssetBaseURL, remoteAssetURL, queryPath);
}

return this._loadMainAsset(loader, item, assetBaseURL);
return this._loadMainAsset(loader, item, remoteAssetBaseURL, assetBaseURL);
}

private _loadMainAsset<T>(loader: Loader<T>, item: LoadItem, assetBaseURL: string): AssetPromise<T> {
private _loadMainAsset<T>(
loader: Loader<T>,
item: LoadItem,
remoteAssetBaseURL: string,
assetBaseURL: string
): AssetPromise<T> {
item.url = assetBaseURL;
const loadingPromises = this._loadingPromises;
const promise = loader.load(item, this);
loadingPromises[assetBaseURL] = promise;
loadingPromises[remoteAssetBaseURL] = promise;

promise.then(
(resource: T) => {
if (loader.useCache) {
this._addAsset(assetBaseURL, resource as EngineObject);
this._addAsset(remoteAssetBaseURL, resource as EngineObject);
}
delete loadingPromises[assetBaseURL];
this._releaseSubAssetPromiseCallback(assetBaseURL);
delete loadingPromises[remoteAssetBaseURL];
this._releaseSubAssetPromiseCallback(remoteAssetBaseURL);
},
() => {
delete loadingPromises[assetBaseURL];
this._releaseSubAssetPromiseCallback(assetBaseURL);
delete loadingPromises[remoteAssetBaseURL];
this._releaseSubAssetPromiseCallback(remoteAssetBaseURL);

Check warning on line 419 in packages/core/src/asset/ResourceManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/asset/ResourceManager.ts#L418-L419

Added lines #L418 - L419 were not covered by tests
}
);

return promise;
}

private _createSubAssetPromiseCallback<T>(
assetBaseURL: string,
assetURL: string,
remoteAssetBaseURL: string,
remoteAssetURL: string,
assetSubPath: string
): AssetPromise<T> {
const loadingPromises = this._loadingPromises;
const subPromiseCallback = this._subAssetPromiseCallbacks[assetBaseURL]?.[assetSubPath];
const subPromiseCallback = this._subAssetPromiseCallbacks[remoteAssetBaseURL]?.[assetSubPath];
const resolvedValue = subPromiseCallback?.resolvedValue;
const rejectedValue = subPromiseCallback?.rejectedValue;

Expand All @@ -438,19 +446,19 @@

// Pending
const promise = new AssetPromise<T>((resolve, reject) => {
(this._subAssetPromiseCallbacks[assetBaseURL] ||= {})[assetSubPath] = {
(this._subAssetPromiseCallbacks[remoteAssetBaseURL] ||= {})[assetSubPath] = {
resolve,
reject
};
});

loadingPromises[assetURL] = promise;
loadingPromises[remoteAssetURL] = promise;

promise.then(
() => {
delete loadingPromises[assetURL];
delete loadingPromises[remoteAssetURL];

Check warning on line 459 in packages/core/src/asset/ResourceManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/asset/ResourceManager.ts#L459

Added line #L459 was not covered by tests
},
() => delete loadingPromises[assetURL]
() => delete loadingPromises[remoteAssetURL]

Check warning on line 461 in packages/core/src/asset/ResourceManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/asset/ResourceManager.ts#L461

Added line #L461 was not covered by tests
);

return promise;
Expand Down Expand Up @@ -537,15 +545,21 @@
if (obj) {
promise = Promise.resolve(obj);
} else {
let url = this._editorResourceConfig[refId]?.path;
if (!url) {
const resourceConfig = this._editorResourceConfig[refId];

Check warning on line 548 in packages/core/src/asset/ResourceManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/asset/ResourceManager.ts#L548

Added line #L548 was not covered by tests
if (!resourceConfig) {
Logger.warn(`refId:${refId} is not find in this._editorResourceConfig.`);
return Promise.resolve(null);
}
url = key ? `${url}${url.indexOf("?") > -1 ? "&" : "?"}q=${key}` : url;
const remoteUrl = resourceConfig.path;
const queryPath = new URL(remoteUrl).search;
let url = resourceConfig.virtualPath + queryPath;

Check warning on line 555 in packages/core/src/asset/ResourceManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/asset/ResourceManager.ts#L553-L555

Added lines #L553 - L555 were not covered by tests
if (key) {
url += (url.indexOf("?") > -1 ? "&" : "?") + "q=" + key;
}
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved

GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
promise = this.load<any>({
url,
type: this._editorResourceConfig[refId].type
type: resourceConfig.type
});
}
return promise.then((item) => (isClone ? item.clone() : item));
Expand Down
2 changes: 1 addition & 1 deletion packages/loader/src/AnimationClipLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class AnimationClipLoader extends Loader<AnimationClip> {
load(item: LoadItem, resourceManager: ResourceManager): AssetPromise<AnimationClip> {
return new AssetPromise((resolve, reject) => {
this.request<any>(item.url, {
this.request<any>(item.url, resourceManager, {

Check warning on line 17 in packages/loader/src/AnimationClipLoader.ts

View check run for this annotation

Codecov / codecov/patch

packages/loader/src/AnimationClipLoader.ts#L17

Added line #L17 was not covered by tests
...item,
type: "arraybuffer"
})
Expand Down
2 changes: 1 addition & 1 deletion packages/loader/src/AnimatorControllerLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class AnimatorControllerLoader extends Loader<AnimatorController> {
load(item: LoadItem, resourceManager: ResourceManager): AssetPromise<AnimatorController> {
return new AssetPromise((resolve, reject) => {
this.request<any>(item.url, {
this.request<any>(item.url, resourceManager, {

Check warning on line 22 in packages/loader/src/AnimatorControllerLoader.ts

View check run for this annotation

Codecov / codecov/patch

packages/loader/src/AnimatorControllerLoader.ts#L22

Added line #L22 was not covered by tests
...item,
type: "json"
})
Expand Down
6 changes: 3 additions & 3 deletions packages/loader/src/BufferLoader.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { resourceLoader, Loader, AssetPromise, AssetType, LoadItem } from "@galacean/engine-core";
import { resourceLoader, Loader, AssetPromise, AssetType, LoadItem, ResourceManager } from "@galacean/engine-core";

function isBase64(url) {
return /^data:(.+?);base64,/.test(url);
}
@resourceLoader(AssetType.Buffer, ["bin", "r3bin"], false)
class BufferLoader extends Loader<ArrayBuffer> {
load(item: LoadItem): AssetPromise<ArrayBuffer> {
load(item: LoadItem, resourceManager: ResourceManager): AssetPromise<ArrayBuffer> {
const url = item.url;
if (isBase64(url)) {
return new AssetPromise((resolve) => {
Expand All @@ -14,7 +14,7 @@ class BufferLoader extends Loader<ArrayBuffer> {
resolve(result.buffer);
});
}
return this.request(url, {
return this.request(url, resourceManager, {
...item,
type: "arraybuffer"
});
Expand Down
2 changes: 1 addition & 1 deletion packages/loader/src/EnvLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { SphericalHarmonics3 } from "@galacean/engine-math";
class EnvLoader extends Loader<AmbientLight> {
load(item: LoadItem, resourceManager: ResourceManager): AssetPromise<AmbientLight> {
return new AssetPromise((resolve, reject) => {
this.request<ArrayBuffer>(item.url, { ...item, type: "arraybuffer" })
this.request<ArrayBuffer>(item.url, resourceManager, { ...item, type: "arraybuffer" })
.then((arraybuffer) => {
const shArray = new Float32Array(arraybuffer, 0, 27);
const shByteLength = 27 * 4;
Expand Down
2 changes: 1 addition & 1 deletion packages/loader/src/FontLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class FontLoader extends Loader<Font> {
load(item: LoadItem, resourceManager: ResourceManager): AssetPromise<Font> {
return new AssetPromise((resolve, reject) => {
this.request<any>(item.url, { ...item, type: "json" })
this.request<any>(item.url, resourceManager, { ...item, type: "json" })

Check warning on line 15 in packages/loader/src/FontLoader.ts

View check run for this annotation

Codecov / codecov/patch

packages/loader/src/FontLoader.ts#L15

Added line #L15 was not covered by tests
.then((data) => {
const { fontName, fontUrl } = data;

Expand Down
3 changes: 1 addition & 2 deletions packages/loader/src/GLTFLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ export class GLTFLoader extends Loader<GLTFResource> {
}

override load(item: LoadItem, resourceManager: ResourceManager): AssetPromise<GLTFResource> {
const url = item.url;
const params = <GLTFParams>item.params;
const glTFResource = new GLTFResource(resourceManager.engine, url);
const glTFResource = new GLTFResource(resourceManager.engine, item.url);
const context = new GLTFParserContext(glTFResource, resourceManager, {
keepMeshData: false,
...params
Expand Down
2 changes: 1 addition & 1 deletion packages/loader/src/HDRLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@
return new AssetPromise((resolve, reject) => {
const engine = resourceManager.engine;

this.request<ArrayBuffer>(item.url, { ...item, type: "arraybuffer" })
this.request<ArrayBuffer>(item.url, resourceManager, { ...item, type: "arraybuffer" })

Check warning on line 384 in packages/loader/src/HDRLoader.ts

View check run for this annotation

Codecov / codecov/patch

packages/loader/src/HDRLoader.ts#L384

Added line #L384 was not covered by tests
.then((buffer) => {
const uint8Array = new Uint8Array(buffer);
const { width, height, dataPosition } = HDRLoader._parseHeader(uint8Array);
Expand Down
6 changes: 3 additions & 3 deletions packages/loader/src/JSONLoader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { resourceLoader, Loader, AssetPromise, AssetType, LoadItem } from "@galacean/engine-core";
import { resourceLoader, Loader, AssetPromise, AssetType, LoadItem, ResourceManager } from "@galacean/engine-core";

@resourceLoader(AssetType.JSON, ["json"], false)
class JSONLoader extends Loader<string> {
load(item: LoadItem): AssetPromise<string> {
return this.request(item.url, {
load(item: LoadItem, resourceManager: ResourceManager): AssetPromise<string> {
return this.request(item.url, resourceManager, {
...item,
type: "json"
});
Expand Down
2 changes: 1 addition & 1 deletion packages/loader/src/KTXCubeLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class KTXCubeLoader extends Loader<TextureCube> {
return new AssetPromise((resolve, reject) => {
Promise.all(
item.urls.map((url) =>
this.request<ArrayBuffer>(url, {
this.request<ArrayBuffer>(url, resourceManager, {
...item,
type: "arraybuffer"
})
Expand Down
2 changes: 1 addition & 1 deletion packages/loader/src/KTXLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
export class KTXLoader extends Loader<Texture2D> {
load(item: LoadItem, resourceManager: ResourceManager): AssetPromise<Texture2D> {
return new AssetPromise((resolve, reject) => {
this.request<ArrayBuffer>(item.url, {
this.request<ArrayBuffer>(item.url, resourceManager, {

Check warning on line 16 in packages/loader/src/KTXLoader.ts

View check run for this annotation

Codecov / codecov/patch

packages/loader/src/KTXLoader.ts#L16

Added line #L16 was not covered by tests
...item,
type: "arraybuffer"
})
Expand Down
Loading
Loading