8000 Opt ShaderLab module code by Sway007 · Pull Request #2419 · galacean/engine · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Opt ShaderLab module code 8000 #2419

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

Merged
merged 15 commits into from
Dec 11, 2024
2 changes: 1 addition & 1 deletion packages/shader-lab/src/ShaderLab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class ShaderLab implements IShaderLab {

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

_parseShaderPass(
Expand Down
9 changes: 9 additions & 0 deletions packages/shader-lab/src/TempArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IPoolElement } from "@galacean/engine";

export class TempArray<T> implements IPoolElement {
array: Array<T> = new Array();

dispose(): void {
this.array.length = 0;
}
}
21 changes: 14 additions & 7 deletions packages/shader-lab/src/codeGen/CodeGenVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,33 @@
// #if _VERBOSE
import { GSError } from "../GSError";
// #endif
import { ShaderLabUtils } from "../ShaderLabUtils";
import { Logger, ReturnableObjectPool } from "@galacean/engine";
import { TempArray } from "../TempArray";

/**
* @internal
* The code generator
*/
export class CodeGenVisitor {
export abstract class CodeGenVisitor {
// #if _VERBOSE
readonly errors: GSError[] = [];
readonly errors: Error[] = [];
// #endif
protected static _tmpArrayPool = new ReturnableObjectPool(TempArray<string>, 10);

defaultCodeGen(children: NodeChild[]) {
let ret: string[] = [];
const pool = CodeGenVisitor._tmpArrayPool;
let ret = pool.get();
ret.dispose();
for (const child of children) {
if (child instanceof Token) {
ret.push(child.lexeme);
ret.array.push(child.lexeme);
} else {
ret.push(child.codeGen(this));
ret.array.push(child.codeGen(this));
}
}
return ret.join(" ");
pool.return(ret);
return ret.array.join(" ");
}

visitPostfixExpression(node: ASTNode.PostfixExpression) {
Expand Down Expand Up @@ -195,7 +202,7 @@
// #if _VERBOSE
this.errors.push(new GSError(GSErrorName.CompilationError, message, loc, ShaderLab._processingPassText));
// #else
throw new Error(message);
Logger.error(message);

Check warning on line 205 in packages/shader-lab/src/codeGen/CodeGenVisitor.ts

View check run for this annotation

Codecov / codecov/patch

packages/shader-lab/src/codeGen/CodeGenVisitor.ts#L205

Added line #L205 was not covered by tests
// #endif
}
}
16 changes: 12 additions & 4 deletions packages/shader-lab/src/common/BaseScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { ShaderLab } from "../ShaderLab";
import { BaseToken } from "./BaseToken";
import { ShaderLabUtils } from "../ShaderLabUtils";
import { Logger } from "@galacean/engine";

export type BaseToken, scanner: BaseScanner) => void;

Expand Down Expand Up @@ -73,17 +74,21 @@
return this._source[this._currentIndex];
}

getCurCharCode(): number {
return this._source.charCodeAt(this._currentIndex);
}

advance(count = 1): void {
// #if _VERBOSE
for (let i = 0; i < count; i++) {
this._advance();
}
// #else
this._currentIndex += count;
// #endif
}

_advance(): void {
if (this.isEnd()) {
return;
}

// #if _VERBOSE
if (this.getCurChar() === "\n") {
this._line += 1;
Expand Down Expand Up @@ -142,6 +147,9 @@

throwError(pos: ShaderPosition | ShaderRange, ...msgs: any[]) {
const error = ShaderLabUtils.createGSError(msgs.join(" "), GSErrorName.ScannerError, this._source, pos);
// #if _VERBOSE
Logger.error(error.toString());
// #endif

Check warning on line 152 in packages/shader-lab/src/common/BaseScanner.ts

View check run for this annotation

Codecov / codecov/patch

packages/shader-lab/src/common/BaseScanner.ts#L150-L152

Added lines #L150 - L152 were not covered by tests
throw error;
}

Expand Down
8 changes: 6 additions & 2 deletions packages/shader-lab/src/contentParser/ShaderContentParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ export class ShaderContentParser {
this._symbolTable.newScope(symbolTable);
}

private static _dropScope() {
this._symbolTable.dropScope();
}

private static _parseRenderStatePropList(state: string, scanner: Scanner): IRenderStates {
const ret: IRenderStates = { constantMap: {}, variableMap: {} };
while (scanner.getCurChar() !== "}") {
Expand Down Expand Up @@ -402,7 +406,7 @@ export class ShaderContentParser {
braceLevel -= 1;
if (braceLevel === 0) {
this._addGlobalStatement(ret, scanner, start, word.lexeme.length);
this._symbolTable.dropScope();
this._dropScope();
return ret;
}
}
Expand Down Expand Up @@ -501,7 +505,7 @@ export class ShaderContentParser {
braceLevel -= 1;
if (braceLevel === 0) {
3372 this._addGlobalStatement(ret, scanner, start, word.lexeme.length);
this._symbolTable.dropScope();
this._dropScope();
return ret;
}
}
Expand Down
Loading
Loading
0