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

Merged
merged 114 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 112 commits
Commits
Show all changes
114 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
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
2f56705
feat: code opt
Sway007 Sep 26, 2024
b102ac8
Merge branch 'dev/1.4' of https://github.com/galacean/engine into fea…
Sway007 Sep 26, 2024
e1f749a
feat: add shaderlab verbose package
Sway007 Sep 26, 2024
6e36dd0
feat: add shaderlab verbose package
Sway007 Sep 26, 2024
032a631
feat: update readme
Sway007 Sep 26, 2024
adb871f
feat: rollup.config.js opt
Sway007 Sep 26, 2024
cc8e0a1
feat: code opt & compatible with multiple log
Sway007 Oct 8, 2024
b7da54e
feat: code opt
Sway007 Oct 8, 2024
59776c4
feat: _EDITOR to _VERBOSE
Sway007 Oct 8, 2024
11ebcbc
feat: opt shaderlabobjectpool
Sway007 Oct 8, 2024
67f9c8d
feat: rm redundant code
Sway007 Oct 8, 2024
c76c81f
feat: code opt
Sway007 Oct 9, 2024
2236ac3
feat: code opt
Sway007 Oct 9, 2024
9f8cbe7
feat: code opt
Sway007 Oct 9, 2024
0df3db6
fix: new scope when parse pass
Sway007 Oct 10, 2024
a72aade
feat: code opt
Sway007 Oct 10, 2024
a19e9ba
feat: code opt
Sway007 Oct 11, 2024
4c47c15
feat: code opt
Sway007 Oct 11, 2024
d83cb29
feat: code opt
Sway007 Oct 11, 2024
a180d29
feat: code opt
Sway007 Oct 12, 2024
7c57e15
feat: add testcase for BaseScanner
Sway007 Oct 12, 2024
4c63401
feat: code opt
Sway007 Oct 12, 2024
87c719d
feat: code opt
Sway007 Oct 12, 2024
2661cae
feat: code opt
Sway007 Oct 12, 2024
a85ee45
feat: code opt
Sway007 Oct 12, 2024
4c50206
feat: code opt
Sway007 Oct 12, 2024
cae81c2
feat: code opt
Sway007 Oct 12, 2024
eb515be
feat: code opt
Sway007 Oct 12, 2024
fb3974e
feat: code opt
Sway007 Oct 12, 2024
f0742cf
feat: code opt
Sway007 Oct 12, 2024
ef6496b
feat: code opt
Sway007 Oct 14, 2024
4197f1d
feat: code opt
Sway007 Oct 16, 2024
2e343bb
feat: code opt
Sway007 Oct 16, 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
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@
"test-debug": "cross-env TS_NODE_PROJECT=tsconfig.tests.json floss --path tests -r ts-node/register --debug",
"test-cov": "cross-env TS_NODE_PROJECT=tsconfig.tests.json IS_COV=1 nyc --reporter=lcov floss --path tests -r ts-node/register",
"build": "npm run b:module && npm run b:types",
"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
8 changes: 8 additions & 0 deletions packages/shader-lab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ const shader = Shader.create(galaceanShaderCode);
engine.run()
```

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

you can use `Verbose` version by import:

```ts
import { ShaderLab } from "@galacean/engine-shader-lab/verbose";
```

GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
## 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
3 changes: 2 additions & 1 deletion packages/shader-lab/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
},
"files": [
"dist/**/*",
"types/**/*"
"types/**/*",
"verbose/package.json"
],
"devDependencies": {
"@galacean/engine-design": "workspace:*",
Expand Down
19 changes: 0 additions & 19 deletions packages/shader-lab/src/Error.ts

This file was deleted.

65 changes: 65 additions & 0 deletions packages/shader-lab/src/GSError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// #if _VERBOSE
import { ShaderPosition } from "./common/ShaderPosition";
import { ShaderRange } from "./common/ShaderRange";

export class GSError extends Error {
static wrappingLineCount = 2;

constructor(
name: GSErrorName,
message: string,
public readonly location: ShaderRange | ShaderPosition,
public readonly source: string,
public readonly file?: string
) {
super(message);
this.name = name;
}

override toString(): string {
let start: ShaderPosition, end: ShaderPosition;
const { message, location, source } = this;
if (!source) {
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
return message;

Check warning on line 23 in packages/shader-lab/src/GSError.ts

View check run for this annotation

Codecov / codecov/patch

packages/shader-lab/src/GSError.ts#L23

Added line #L23 was not covered by tests
}

if (location instanceof ShaderPosition) {
start = end = location;

Check warning on line 27 in packages/shader-lab/src/GSError.ts

View check run for this annotation

Codecov / codecov/patch

packages/shader-lab/src/GSError.ts#L27

Added line #L27 was not covered by tests
} else {
start = location.start;
end = location.end;
}
Comment on lines +19 to +31
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

⚠️ Potential issue

Test Coverage Missing for toString Method Scenarios

The toString method in GSError.ts lacks test coverage for the following scenarios:

  1. When source is falsy.
  2. When location is an instance of ShaderPosition.

Please add test cases to cover these scenarios to ensure consistent return types and comprehensive validation.

🔗 Analysis chain

Ensure consistent return type and improve test coverage.

The toString method handles different location types well. However, there are two points to consider:

  1. The early return when source is falsy might lead to inconsistent return types. Consider modifying it to always return a string:
if (!source) {
  return `${this.name}: ${message}`;
}
  1. Static analysis indicates that lines 23 and 27 are not covered by tests. Consider adding test cases to cover these scenarios:
    • When source is falsy
    • When location is an instance of ShaderPosition

To verify the test coverage, you can run the following command:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check test coverage for GSError.ts

# Test: Look for test files related to GSError
fd -e test.ts -e spec.ts GSError

# Test: Check for test cases covering falsy source and ShaderPosition
rg -i 'test|describe|it.*gsError.*source.*falsy' $(fd -e test.ts -e spec.ts)
rg -i 'test|describe|it.*gsError.*ShaderPosition' $(fd -e test.ts -e spec.ts)

Length of output: 185777

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 23-23: packages/shader-lab/src/GSError.ts#L23
Added line #L23 was not covered by tests


[warning] 27-27: packages/shader-lab/src/GSError.ts#L27
Added line #L27 was not covered by tests

const lines = source.split("\n");

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

for (let i = start.line - GSError.wrappingLineCount, n = end.line + GSError.wrappingLineCount; i <= n; i++) {
diagnosticMessage += lineSplit + `${lines[i]}\n`;
Copy link
Member

Choose a reason for hiding this comment

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

cache lines[i]


if (i < start.line || i > end.line) continue;

let remarkStart = 0;
let remarkEnd = lines[i].length;
let paddingLength = lineSplit.length;
if (i === start.line) {
remarkStart = start.column;
paddingLength += start.column;
} else if (i === end.line) {
remarkEnd = end.column;
}
const remarkLength = Math.max(remarkEnd - remarkStart, 1);

diagnosticMessage += " ".repeat(paddingLength) + "^".repeat(remarkLength) + "\n";
}
Sway007 marked this conversation as resolved.
Show resolved Hide resolved
Sway007 marked this conversation as resolved.
Show resolved Hide resolved

return diagnosticMessage;
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add bounds checking to prevent potential runtime errors.

The error message formatting logic is comprehensive and provides clear visual indication of the error location. However, there's a potential issue with accessing lines[i] without bounds checking, which could lead to runtime errors if i is out of bounds.

To prevent potential runtime errors, add bounds checking before accessing lines[i]:

 for (let i = start.line - GSError.wrappingLineCount, n = end.line + GSError.wrappingLineCount; i <= n; i++) {
+  if (i >= 0 && i < lines.length) {
     diagnosticMessage += lineSplit + `${lines[i]}\n`;
     
     // ... rest of the loop body ...
     
+  }
 }

This change ensures that we only access valid indices of the lines array, preventing potential runtime errors.

📝 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
const lines = source.split("\n");
let diagnosticMessage = `${this.name}: ${message}\n\n`;
const lineSplit = "|···";
for (let i = start.line - GSError.wrappingLineCount, n = end.line + GSError.wrappingLineCount; i <= n; i++) {
diagnosticMessage += lineSplit + `${lines[i]}\n`;
if (i < start.line || i > end.line) continue;
let remarkStart = 0;
let remarkEnd = lines[i].length;
let paddingLength = lineSplit.length;
if (i === start.line) {
remarkStart = start.column;
paddingLength += start.column;
} else if (i === end.line) {
remarkEnd = end.column;
}
const remarkLength = Math.max(remarkEnd - remarkStart, 1);
diagnosticMessage += " ".repeat(paddingLength) + "^".repeat(remarkLength) + "\n";
}
return diagnosticMessage;
}
const lines = source.split("\n");
let diagnosticMessage = `${this.name}: ${message}\n\n`;
const lineSplit = "|···";
for (let i = start.line - GSError.wrappingLineCount, n = end.line + GSError.wrappingLineCount; i <= n; i++) {
if (i >= 0 && i < lines.length) {
diagnosticMessage += lineSplit + `${lines[i]}\n`;
if (i < start.line || i > end.line) continue;
let remarkStart = 0;
let remarkEnd = lines[i].length;
let paddingLength = lineSplit.length;
if (i === start.line) {
remarkStart = start.column;
paddingLength += start.column;
} else if (i === end.line) {
remarkEnd = end.column;
}
const remarkLength = Math.max(remarkEnd - remarkStart, 1);
diagnosticMessage += " ".repeat(paddingLength) + "^".repeat(remarkLength) + "\n";
}
}
return diagnosticMessage;
}

}

// #endif
export enum GSErrorName {
PreprocessorError = "PreprocessorError",
CompilationError = "CompilationError",
ScannerError = "ScannerError"
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { ENonTerminal, GrammarSymbol } from "./parser/GrammarSymbol";
import { BaseToken as Token } from "./common/BaseToken";
import { EKeyword, ETokenType, GalaceanDataType, ShaderRange, ShaderPosition } from "./common";
import { EKeyword, ETokenType, GalaceanDataType } from "./common";
import { TreeNode } from "./parser/AST";
// #if _EDITOR
// #if _VERBOSE
import State from "./lalr/State";
// #endif
import { Logger } from "@galacean/engine";

export class ParserUtils {
static unwrapNodeByType<T = TreeNode>(node: TreeNode, type: ENonTerminal): T | undefined {
Expand All @@ -15,7 +14,7 @@ export class ParserUtils {
return ParserUtils.unwrapNodeByType(child, type);
}

// #if _EDITOR
// #if _VERBOSE
/**
* Check if type `tb` is compatible with type `ta`.
*/
Expand All @@ -39,15 +38,10 @@ export class ParserUtils {
return sm < ENonTerminal.START;
}

static throw(pos: ShaderPosition | ShaderRange | number, ...msgs: any[]) {
Logger.error(pos.toString(), ...msgs);
throw msgs.join(" ");
}

/**
* @internal
*/
// #if _EDITOR
// #if _VERBOSE
static printStatePool(logPath: string) {
let output = "";

Expand Down
130 changes: 71 additions & 59 deletions packages/shader-lab/src/ShaderLab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,32 @@
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";
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
import { ShaderContentParser } from "./contentParser";
// @ts-ignore
import { Logger, ShaderLib, ShaderMacro, ShaderPass, ShaderPlatformTarget } from "@galacean/engine";
import { Logger, ShaderLib, ShaderMacro, ShaderPlatformTarget } from "@galacean/engine";
import { ShaderPosition, ShaderRange } from "./common";
import { ShaderLabObjectPool } from "./ShaderLabObjectPool";
// #if _VERBOSE
import { GSError } from "./GSError";
// #endif
Sway007 marked this conversation as resolved.
Show resolved Hide resolved
import { PpParser } from "./preprocessor/PpParser";
import { ShaderLabUtils } from "./ShaderLabUtils";

/** @internal */
export class ShaderLab implements IShaderLab {
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
/**
* @internal
*/
private static _parser = ShaderTargetParser.create();
/**
* @internal
*/
private static _shaderPositionPool = new ShaderLabObjectPool(ShaderPosition);
/**
* @internal
*/
private static _shaderRangePool = new ShaderLabObjectPool(ShaderRange);
private static _shaderPositionPool = ShaderLabUtils.createObjectPool(ShaderPosition);
private static _shaderRangePool = ShaderLabUtils.createObjectPool(ShaderRange);

static createPosition(
index: number,
// #if _EDITOR
line?: number,
column?: number
// #endif
): ShaderPosition {
// #if _VERBOSE
static _processingPassText?: string;
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
// #endif

static createPosition(index: number, line?: number, column?: number): ShaderPosition {
const position = this._shaderPositionPool.get();
position.setX(
position.set(
index,
// #if _EDITOR
// #if _VERBOSE
line,
column
// #endif
Expand All @@ -43,10 +37,15 @@

static createRange(start: ShaderPosition, end: ShaderPosition): ShaderRange {
const range = this._shaderRangePool.get();
range.setX(start, end);
range.set(start, end);
return range;
}

// #if _VERBOSE
/** Retrieve the compilation errors */
readonly errors: GSError[] = [];
// #endif

_parseShaderPass(
source: string,
vertexEntry: string,
Expand All @@ -56,7 +55,6 @@
platformMacros: string[],
basePathForIncludeKey: string
) {
ShaderLabObjectPool.clearAllShaderLabObjectPool();
Preprocessor.reset(ShaderLib, basePathForIncludeKey);
for (const macro of macros) {
Preprocessor.addPredefinedMacro(macro.name, macro.value);
Expand All @@ -66,68 +64,82 @@
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 _VERBOSE
if (PpParser._errors.length > 0) {
for (const err of PpParser._errors) {
this.errors.push(<GSError>err);

Check warning on line 72 in packages/shader-lab/src/ShaderLab.ts

View check run for this annotation

Codecov / codecov/patch

packages/shader-lab/src/ShaderLab.ts#L71-L72

Added lines #L71 - L72 were not covered by tests
}
this._logErrors();
return { vertex: "", fragment: "" };

Check warning on line 75 in packages/shader-lab/src/ShaderLab.ts

View check run for this annotation

Codecov / codecov/patch

packages/shader-lab/src/ShaderLab.ts#L74-L75

Added lines #L74 - L75 were not covered by tests
}
// #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;
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
const program = parser.parse(tokens);

// #if _VERBOSE
for (const err of parser.errors) {
this.errors.push(err);
}
if (!program) {
this._logErrors();

Check warning on line 94 in packages/shader-lab/src/ShaderLab.ts

View check run for this annotation

Codecov / codecov/patch

packages/shader-lab/src/ShaderLab.ts#L94

Added line #L94 was not covered by tests
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 _VERBOSE
for (const err of codeGen.errors) {
this.errors.push(err);
}
this._logErrors();
// #endif

return ret;
}

_parseShaderContent(shaderSource: string): IShaderContent {
ShaderLabUtils.clearAllShaderLabObjectPool();
ShaderContentParser.reset();
return ShaderContentParser.parse(shaderSource);
const ret = ShaderContentParser.parse(shaderSource);

// #if _VERBOSE
this.errors.length = 0;
for (const error of ShaderContentParser._errors) {
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
this.errors.push(error);

Check warning on line 125 in packages/shader-lab/src/ShaderLab.ts

View check run for this annotation

Codecov / codecov/patch

packages/shader-lab/src/ShaderLab.ts#L125

Added line #L125 was not covered by tests
}
// #endif

return ret;
}

// #if _EDITOR
// #if _VERBOSE
/**
* @internal
* For debug
*/
_parse(
shaderSource: string,
macros: ShaderMacro[],
backend: ShaderPlatformTarget
): (ReturnType<ShaderLab["_parseShaderPass"]> & { name: string })[] {
const structInfo = this._parseShaderContent(shaderSource);
const passResult = [] as any;
for (const subShader of structInfo.subShaders) {
for (const pass of subShader.passes) {
if (pass.isUsePass) continue;
const passInfo = this._parseShaderPass(
pass.contents,
pass.vertexEntry,
pass.fragmentEntry,
macros,
backend,
[],
// @ts-ignore
new URL(pass.name, ShaderPass._shaderRootPath).href
) as any;
passInfo.name = pass.name;
passResult.push(passInfo);
}
_logErrors() {
const errors = this.errors;
if (errors.length === 0 || !Logger.isEnabled) return;
Logger.error(`${errors.length} errors occur!`);
for (const err of errors) {
Logger.error(err.toString());

Check warning on line 141 in packages/shader-lab/src/ShaderLab.ts

View check run for this annotation

Codecov / codecov/patch

packages/shader-lab/src/ShaderLab.ts#L139-L141

Added lines #L139 - L141 were not covered by tests
}
return passResult;
}
// #endif
}
Loading
Loading