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

Improve ShaderLab compilation error log and package build #2364

Open
wants to merge 82 commits into
base: dev/1.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
5a26f3d
fix(shader-lab): compatible with empty macro
Sway007 Sep 20, 2023
5ecc318
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Sep 20, 2023
cafc24f
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Sep 20, 2023
dc69489
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Sep 21, 2023
221b7b6
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Sep 21, 2023
0d45d9c
fix(shader-lab): add break and continue syntax
Sway007 Sep 21, 2023
0f14c3f
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Sep 27, 2023
8871d9b
fix: typo
Sway007 Oct 11, 2023
3b4ffd7
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Nov 1, 2023
f649a58
fix(shader-lab): Make usepass compatible with buitin shader
Sway007 Nov 1, 2023
598fc56
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Nov 2, 2023
e33a66f
fix(shader-lab): compatible with no varying variable
Sway007 Nov 2, 2023
41ef06f
feat(shader-lab): detect mismatch return type
Sway007 Nov 6, 2023
b5214fc
fix(shader-lab): renderState assignment
Sway007 Nov 7, 2023
f36ce02
feat: extend material loader data type
Sway007 Nov 13, 2023
ff8b7c2
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Nov 13, 2023
634236f
fix(shader-lab): glsl type pattern
Sway007 Nov 13, 2023
91e6fa4
fix(shader-lab): glsl type pattern
Sway007 Nov 13, 2023
3932448
fix: switch case break
Sway007 Nov 15, 2023
671cace
fix: array index loss
Sway007 Jun 11, 2024
92b972e
feat: merge
Sway007 Jun 11, 2024
9226d38
fix: test-case
Sway007 Jun 11, 2024
ff6a69a
fix: test-case
Sway007 Jun 12, 2024
83b9ca2
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Jun 13, 2024
f510c2a
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Jul 15, 2024
94e30f5
fix: conditional compilation
Sway007 Jul 16, 2024
e3c7aa5
fix: expand token
Sway007 Jul 16, 2024
36b77e0
fix: expand token
Sway007 Jul 16, 2024
b2e07fc
feat: code opt
Sway007 Jul 16, 2024
93df92e
fix: ci
Sway007 Jul 16, 2024
8e0cc28
feat: cache error shader
Sway007 Jul 17, 2024
7652250
Merge branch 'dev/1.3' of https://github.com/galacean/engine into fea…
Sway007 Jul 17, 2024
c92e674
fix: preprocessor parse expression
Sway007 Jul 17, 2024
3fefbe9
Merge branch 'dev/1.3' of https://github.com/galacean/engine into fea…
Sway007 Jul 17, 2024
9eb7398
fix: gles 100 code gen
Sway007 Jul 17, 2024
9f5b5bc
feat: support relative path include
Sway007 Jul 18, 2024
82f73b2
Merge branch 'dev/1.3' of https://github.com/galacean/engine into fea…
Sway007 Jul 18, 2024
964b846
feat: test case
Sway007 Jul 18, 2024
f6fed8b
feat: compatible with builtin shader chunk import
Sway007 Jul 18, 2024
519ab40
feat: code opt
Sway007 Jul 18, 2024
ee6a813
feat: code opt
Sway007 Jul 18, 2024
d7e0ac8
feat: code opt
Sway007 Jul 18, 2024
8f75c28
feat: code opt
Sway007 Jul 18, 2024
00447fb
feat: code opt
Sway007 Jul 18, 2024
d5a3c0c
feat: add log
Sway007 Jul 19, 2024
c176226
feat: add log
Sway007 Jul 19, 2024
b8ca43f
feat: add log
Sway007 Jul 22, 2024
a6d21d6
Merge branch 'dev/1.3' of https://github.com/galacean/engine into fea…
Sway007 Jul 30, 2024
ce23026
fix: multi dist
Sway007 Aug 1, 2024
3bcaeb0
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Aug 5, 2024
79a7a0c
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Aug 5, 2024
fa7131c
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Aug 6, 2024
3093bd2
Merge branch 'main' into feat/shaderlab-error-report
Sway007 Aug 20, 2024
6eba20a
Merge branch 'main' of https://github.com/galacean/engine into feat/s…
Sway007 Aug 20, 2024
8c90796
feat(shaderlab): [wip] optmize error log
Sway007 Aug 23, 2024
dab8003
Merge branch 'main' of https://github.com/galacean/engine into feat/s…
Sway007 Aug 26, 2024
f12b8f7
feat(shaderlab): multi package
Sway007 Aug 27, 2024
c32969b
feat(shaderlab): opt code
Sway007 Aug 27, 2024
9d4d808
feat(shaderlab): opt code
Sway007 Aug 27, 2024
f5edeb3
feat(shaderlab): opt code
Sway007 Aug 27, 2024
68e1556
feat(shaderlab): opt code
Sway007 Aug 27, 2024
337ba2e
Fix error in node environment (#2362)
zhuxudong Aug 27, 2024
dd4777c
feat(shaderlab): test case
Sway007 Aug 27, 2024
1a918da
feat: opt code
Sway007 Aug 27, 2024
4c9e16c
Merge branch 'main' of https://github.com/galacean/engine into feat/s…
Sway007 Aug 27, 2024
e4d267c
feat: opt code
Sway007 Aug 27, 2024
077e009
feat: opt code
Sway007 Aug 27, 2024
462af90
feat: opt code
Sway007 Aug 27, 2024
54f9c79
feat: opt code
Sway007 Aug 27, 2024
d01b060
feat: opt code
Sway007 Aug 27, 2024
df84ce0
feat: opt code
Sway007 Aug 27, 2024
1426499
feat: opt code
Sway007 Aug 27, 2024
48eb236
feat: opt code
Sway007 Aug 27, 2024
3b06a3f
feat: opt code
Sway007 Aug 27, 2024
ab356b6
feat: opt code
Sway007 Aug 27, 2024
ab6de2c
feat: opt code
Sway007 Aug 27, 2024
5092aab
feat: opt code
Sway007 Aug 27, 2024
8a5f838
feat: opt code
Sway007 Aug 27, 2024
ff3f5ae
feat: opt code
Sway007 Aug 27, 2024
c679a9e
feat: opt code
Sway007 Aug 27, 2024
44b7e84
feat: opt code
Sway007 Sep 2, 2024
eb0bcde
feat: code opt
Sway007 Sep 11, 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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:

- name: Install
run: pnpm install
- run: npm run build:editor
- run: npm run build

codecov:
runs-on: macos-latest
Expand All @@ -66,7 +66,7 @@ jobs:
- name: Install
run: pnpm install
- name: Build
run: npm run build:editor
run: npm run build
- name: Test
run: npm run test-cov
- run: pnpm install codecov -w
Expand Down Expand Up @@ -94,7 +94,7 @@ jobs:
- name: Run Cypress Tests
uses: cypress-io/github-action@v5
with:
build: npm run build:editor
build: npm run build
start: npm run e2e:case
wait-on: "http://localhost:5175"
wait-on-timeout: 120
Expand Down
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@
"build:editor": "npm run b:module:editor && npm run b:types",
"lint": "eslint packages/*/src --ext .ts",
"watch": "cross-env NODE_ENV=release BUILD_TYPE=MODULE rollup -cw -m inline",
"watch:editor": "cross-env NODE_ENV=editor BUILD_TYPE=MODULE rollup -cw -m inline",
"watch:umd": "cross-env NODE_ENV=release BUILD_TYPE=UMD rollup -cw -m inline",
"watch:umd:editor": "cross-env NODE_ENV=editor BUILD_TYPE=UMD rollup -cw -m inline",
"b:types": "pnpm -r --filter=./packages/* run b:types",
"b:module": "cross-env BUILD_TYPE=MODULE NODE_ENV=release rollup -c",
"b:module:editor": "cross-env BUILD_TYPE=MODULE NODE_ENV=editor rollup -c",
"b:umd": "cross-env BUILD_TYPE=UMD NODE_ENV=release rollup -c",
"b:miniprogram": "cross-env BUILD_TYPE=MINI rollup -c",
"b:all": "cross-env NODE_ENV=release npm run b:types && cross-env BUILD_TYPE=ALL NODE_ENV=release rollup -c",
"b:all:editor": "cross-env NODE_ENV=editor npm run b:types && cross-env BUILD_TYPE=ALL rollup -c",
"clean": "pnpm -r exec rm -rf dist && pnpm -r exec rm -rf types",
"e2e:case": "pnpm -C ./e2e run case",
"e2e": "cypress run --browser chrome --headless",
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/SystemInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export class SystemInfo {
static platform: Platform = Platform.Unknown;
/** The operating system is running on. */
static operatingSystem: string = "";

/** @internal */
static _isBrowser = true;

/** Whether the system support SIMD. */
private static _simdSupported: boolean | null = null;

Expand All @@ -24,6 +28,7 @@ export class SystemInfo {
static _initialize(): void {
{
if (typeof navigator == "undefined") {
SystemInfo._isBrowser = false;
return;
}

Expand Down
13 changes: 9 additions & 4 deletions packages/loader/src/gltf/extensions/EXT_texture_webp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Texture2D } from "@galacean/engine-core";
import { SystemInfo, Texture2D } from "@galacean/engine-core";
import type { ITexture } from "../GLTFSchema";
import { registerGLTFExtension } from "../parser/GLTFParser";
import { GLTFParserContext } from "../parser/GLTFParserContext";
Expand All @@ -15,10 +15,15 @@ class EXT_texture_webp extends GLTFExtensionParser {

constructor() {
super();
const testCanvas = document.createElement("canvas");

testCanvas.width = testCanvas.height = 1;
this._supportWebP = testCanvas.toDataURL("image/webp").indexOf("data:image/webp") == 0;
// @ts-ignore
if (SystemInfo._isBrowser) {
const testCanvas = document.createElement("canvas");
testCanvas.width = testCanvas.height = 1;
this._supportWebP = testCanvas.toDataURL("image/webp").indexOf("data:image/webp") == 0;
} else {
this._supportWebP = false;
}
}

override async createAndParse(
Expand Down
13 changes: 13 additions & 0 deletions packages/shader-lab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ const shader = Shader.create(galaceanShaderCode);
engine.run()
```

There are two versions of ShaderLab: `Release` and `Debug`. The Debug version offers more user-friendly diagnostic information for debug ShaderLab compilation errors, while the Release version provides superior performance.

you can use debug version by import:

```ts
// umd
import { ShaderLab } from "@galacean/engine-shader-lab/dist/browser.editor";
// esmoudle
import { ShaderLab } from "@galacean/engine-shader-lab/dist/module.editor";
// commonjs
import { ShaderLab } from "@galacean/engine-shader-lab/dist/main.editor";
```

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个也是用平台编译带来的缺点,带来这么多潜规则

## CFG Grammar conflict detection

The Galacean ShaderLab syntax is defined using Context-Free Grammar (CFG) and is documented within the `\*.y` file. When modifications to the ShaderLab syntax are required, it is recommended to make changes to the existing CFG syntax file, and employ [Bison](https://www.gnu.org/software/bison/manual/bison.html) to detect any potential grammar conflicts.
Expand Down
72 changes: 65 additions & 7 deletions packages/shader-lab/src/Error.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,77 @@
// #if _EDITOR
import { ShaderRange } from "./common";
import { Logger } from "@galacean/engine";
import { ShaderPosition, ShaderRange } from "./common";

export abstract class GSError extends Error {
readonly loc: ShaderRange;
static wrappingLineCount = 2;

constructor(message: string, loc: ShaderRange, cause?: Error) {
readonly loc: ShaderRange | ShaderPosition;
readonly source: string;
readonly file?: string;

constructor(message: string, loc: ShaderRange | ShaderPosition, source: string, file?: string, cause?: Error) {
super(message, { cause });
this.loc = loc;
this.source = source;
this.file = file;
}

log(_source?: string): void {
if (!Logger.enable) return;
let start: ShaderPosition, end: ShaderPosition;
const { message, loc, source: originSource } = this;
let source = originSource;
if (_source) source = _source;
if (!source) {
Logger.error(message);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里没有 source 可以直接 return 了

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return;
}

if (loc instanceof ShaderPosition) {
start = end = loc;
} else {
start = loc.start;
end = loc.end;
}
const lines = source.split("\n");

let diagnosticMessage = `${this.name}: ${message}\n\n`;
const lineSplit = "|···";

for (let i = 0; i <= end.line + GSError.wrappingLineCount; i++) {
if (i < start.line - GSError.wrappingLineCount) continue;

diagnosticMessage += lineSplit;
diagnosticMessage += `${lines[i]}\n`;
if (i >= start.line && i <= end.line) {
diagnosticMessage += " ".repeat(lineSplit.length + start.column);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for 循环要特别注意这种高频操作,别浪费性能,移到外面去

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个类都是在Editor版本里的,release版本没有这些性能损耗

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哥,Editor 就不用管性能了吗

diagnosticMessage += "^".repeat(Math.max(end.column - start.column, 1));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上,max 更耗性能了

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

diagnosticMessage += "\n";
}
}

Logger.error(diagnosticMessage);
}
}

export class PreprocessorError extends GSError {
constructor(message: string, loc: ShaderRange | ShaderPosition, source: string, file?: string, cause?: Error) {
super(message, loc, source, file, cause);
this.name = "PreprocessorError";
}
}

export class CompilationError extends GSError {
constructor(message: string, loc: ShaderRange | ShaderPosition, source: string, file?: string, cause?: Error) {
super(message, loc, source, file, cause);
this.name = "CompilationError";
}
}

export class SemanticError extends GSError {
constructor(message: string, loc: ShaderRange, cause?: Error) {
super(message, loc, cause);
this.name = "SemanticError";
export class ScannerError extends GSError {
constructor(message: string, loc: ShaderRange | ShaderPosition, source: string, file?: string, cause?: Error) {
super(message, loc, source, file, cause);
this.name = "ScannerError";
}
}
// #endif
106 changes: 73 additions & 33 deletions packages/shader-lab/src/ShaderLab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,30 @@ import { Lexer } from "./lexer";
import { ShaderTargetParser } from "./parser";
import { Preprocessor } from "./preprocessor";
import { GLES100Visitor, GLES300Visitor } from "./codeGen";
import { IShaderContent, IShaderLab } from "@galacean/engine-design/src/shader-lab";
import { IShaderContent, IShaderLab } from "@galacean/engine-design";
import { ShaderContentParser } from "./contentParser";
// @ts-ignore
import { Logger, ShaderLib, ShaderMacro, ShaderPass, ShaderPlatformTarget } from "@galacean/engine";
import { ShaderPosition, ShaderRange } from "./common";
import { ShaderLabObjectPool } from "./ShaderLabObjectPool";
// #if _EDITOR
import { GSError } from "./Error";
// #endif
import { PpParser } from "./preprocessor/PpParser";

export class ShaderLab implements IShaderLab {
/**
* @internal
*/
private static _parser = ShaderTargetParser.create();
/**
* @internal
*/
private static _shaderPositionPool = new ShaderLabObjectPool(ShaderPosition);
private static _shaderRangePool = new ShaderLabObjectPool(ShaderRange);

/**
* @internal
*/
private static _shaderRangePool = new ShaderLabObjectPool(ShaderRange);
static _processingPassText?: string;

static createPosition(
index: number,
// #if _EDITOR
line?: number,
column?: number
// #endif
): ShaderPosition {
static createPosition(index: number, line?: number, column?: number): ShaderPosition {
const position = this._shaderPositionPool.get();
position.setX(
index,
// #if _EDITOR
line,
column
// #endif
);
position.setX(index, line, column);
Comment on lines +26 to +28
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor suggestion: Use class name instead of this in static context.

Using this in a static context can be confusing. Use the class name instead.

Apply this diff to refactor the code:

- const position = this._shaderPositionPool.get();
+ const position = ShaderLab._shaderPositionPool.get();
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
static createPosition(index: number, line?: number, column?: number): ShaderPosition {
const position = this._shaderPositionPool.get();
position.setX(
index,
// #if _EDITOR
line,
column
// #endif
);
position.setX(index, line, column);
static createPosition(index: number, line?: number, column?: number): ShaderPosition {
const position = ShaderLab._shaderPositionPool.get();
position.setX(index, line, column);
Tools
Biome

[error] 27-27: Using this in a static context can be confusing.

this refers to the class.
Unsafe fix: Use the class name instead.

(lint/complexity/noThisInStatic)

return position;
}

Expand All @@ -47,6 +35,17 @@ export class ShaderLab implements IShaderLab {
return range;
}

// #if _EDITOR
private _errors: GSError[] = [];

/**
* Retrieve the compilation errors
*/
get errors(): GSError[] {
return this._errors;
}
// #endif

_parseShaderPass(
source: string,
vertexEntry: string,
Expand All @@ -56,7 +55,6 @@ export class ShaderLab implements IShaderLab {
platformMacros: string[],
basePathForIncludeKey: string
) {
ShaderLabObjectPool.clearAllShaderLabObjectPool();
Preprocessor.reset(ShaderLib, basePathForIncludeKey);
for (const macro of macros) {
Preprocessor.addPredefinedMacro(macro.name, macro.value);
Expand All @@ -66,36 +64,67 @@ export class ShaderLab implements IShaderLab {
Preprocessor.addPredefinedMacro(platformMacros[i]);
}

// #if _EDITOR
// TODO: index to position
// Logger.convertSourceIndex = Preprocessor.convertSourceIndex.bind(Preprocessor);
// #endif

const preprocessorStart = performance.now();

const ppdContent = Preprocessor.process(source);
// #if _EDITOR
if (PpParser._errors.length > 0) {
for (const err of PpParser._errors) {
this._errors.push(<GSError>err);
}
this._logErrors(this._errors);
return { vertex: "", fragment: "" };
}
// #endif

Logger.info(`[pass compilation - preprocessor] cost time ${performance.now() - preprocessorStart}ms`);

const lexer = new Lexer(ppdContent);
const tokens = lexer.tokenize();
const program = ShaderLab._parser.parse(tokens);

const { _parser: parser } = ShaderLab;

ShaderLab._processingPassText = ppdContent;
const program = parser.parse(tokens);

// #if _EDITOR
for (const err of parser.errors) {
this._errors.push(err);
}
if (!program) {
this._logErrors(this._errors);
return { vertex: "", fragment: "" };
}
// #endif

const codeGen =
backend === ShaderPlatformTarget.GLES100 ? GLES100Visitor.getVisitor() : GLES300Visitor.getVisitor();

const start = performance.now();
const ret = codeGen.visitShaderProgram(program, vertexEntry, fragmentEntry);
Logger.info(`[CodeGen] cost time: ${performance.now() - start}ms`);
ShaderLab._processingPassText = undefined;

// #if _EDITOR
for (const err of codeGen.errors) {
this._errors.push(err);
}
this._logErrors(this._errors);
// #endif

return ret;
}

_parseShaderContent(shaderSource: string): IShaderContent {
ShaderLabObjectPool.clearAllShaderLabObjectPool();
ShaderContentParser.reset();
return ShaderContentParser.parse(shaderSource);
// #if _EDITOR
this._errors.length = 0;
// #endif
const ret = ShaderContentParser.parse(shaderSource);
for (const error of ShaderContentParser._errors) {
this._errors.push(error);
}
return ret;
}

// #if _EDITOR
Expand All @@ -105,8 +134,8 @@ export class ShaderLab implements IShaderLab {
*/
_parse(
shaderSource: string,
macros: ShaderMacro[],
backend: ShaderPlatformTarget
macros: ShaderMacro[] = [],
backend: ShaderPlatformTarget = ShaderPlatformTarget.GLES100
): (ReturnType<ShaderLab["_parseShaderPass"]> & { name: string })[] {
const structInfo = this._parseShaderContent(shaderSource);
const passResult = [] as any;
Expand All @@ -129,5 +158,16 @@ export class ShaderLab implements IShaderLab {
}
return passResult;
}

/**
* @internal
*/
_logErrors(errors: GSError[], source?: string) {
if (errors.length === 0 || !Logger.isEnabled) return;
Logger.error(`${errors.length} errors occur!`);
for (const err of errors) {
err.log(source);
}
}
// #endif
}
Loading
Loading