From 5a26f3d8ccd113853e2595cd4d5176eed9d2b971 Mon Sep 17 00:00:00 2001 From: SwayYan Date: Wed, 20 Sep 2023 12:06:40 +0800 Subject: [PATCH 001/131] fix(shader-lab): compatible with empty macro --- packages/shader-lab/src/RuntimeContext.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shader-lab/src/RuntimeContext.ts b/packages/shader-lab/src/RuntimeContext.ts index 8624faf161..5b70213a89 100644 --- a/packages/shader-lab/src/RuntimeContext.ts +++ b/packages/shader-lab/src/RuntimeContext.ts @@ -270,7 +270,7 @@ export default class RuntimeContext { getMacroText(macros: (FnMacroAstNode | FnMacroConditionAstNode)[], needSort = false): string { const list = needSort ? macros.sort(AstNodeUtils.astSortAsc) : macros; - return list.map((item) => item.serialize(this)).join("\n"); + return list?.map((item) => item.serialize(this)).join("\n"); } getGlobalText(): string { From 0d45d9c77f9803f5d69d7f5e058289ff2821ae5b Mon Sep 17 00:00:00 2001 From: SwayYan Date: Thu, 21 Sep 2023 16:02:36 +0800 Subject: [PATCH 002/131] fix(shader-lab): add break and continue syntax --- packages/shader-lab/src/ShaderVisitor.ts | 20 ++++++++++++++++ packages/shader-lab/src/ast-node/AstNode.ts | 12 ++++++++++ .../shader-lab/src/parser/ShaderParser.ts | 12 ++++++++++ .../src/parser/tokens/GLSLKeywords.ts | 4 +++- packages/shader-lab/src/types.ts | 24 +++++++++++++++++++ 5 files changed, 71 insertions(+), 1 deletion(-) diff --git a/packages/shader-lab/src/ShaderVisitor.ts b/packages/shader-lab/src/ShaderVisitor.ts index c7ffa899e1..812e400f2e 100644 --- a/packages/shader-lab/src/ShaderVisitor.ts +++ b/packages/shader-lab/src/ShaderVisitor.ts @@ -9,8 +9,10 @@ import { BlendFactorAstNode, BlendOperationAstNode, BooleanAstNode, + BreakStatementAstNode, CompareFunctionAstNode, ConditionExprAstNode, + ContinueStatementAstNode, CullModeAstNode, DeclarationWithoutAssignAstNode, DiscardStatementAstNode, @@ -80,8 +82,10 @@ import { _ruleBlendStatePropertyDeclarationCstChildren, _ruleBlendStateValueCstChildren, _ruleBooleanCstChildren, + _ruleBreakStatementCstChildren, _ruleCompareFunctionCstChildren, _ruleConditionExprCstChildren, + _ruleContinueStatementCstChildren, _ruleCullModeCstChildren, _ruleDeclarationWithoutAssignCstChildren, _ruleDepthSatePropertyDeclarationCstChildren, @@ -417,6 +421,22 @@ export class ShaderVisitor extends ShaderVisitorConstructor implements Partial { diff --git a/packages/shader-lab/src/ast-node/AstNode.ts b/packages/shader-lab/src/ast-node/AstNode.ts index 0fe5a1aa92..75578f0044 100644 --- a/packages/shader-lab/src/ast-node/AstNode.ts +++ b/packages/shader-lab/src/ast-node/AstNode.ts @@ -272,6 +272,18 @@ export class DiscardStatementAstNode extends AstNode { } } +export class BreakStatementAstNode extends AstNode { + override _doSerialization(context?: RuntimeContext, args?: any): string { + return "break;"; + } +} + +export class ContinueStatementAstNode extends AstNode { + override _doSerialization(context?: RuntimeContext, args?: any): string { + return "continue;"; + } +} + export class FnParenthesisAtomicAstNode extends AstNode { override _doSerialization(context?: RuntimeContext, args?: any): string { let ret = `(${this.content.parenthesisNode.serialize(context)})`; diff --git a/packages/shader-lab/src/parser/ShaderParser.ts b/packages/shader-lab/src/parser/ShaderParser.ts index e1068c01c9..7540e501f9 100644 --- a/packages/shader-lab/src/parser/ShaderParser.ts +++ b/packages/shader-lab/src/parser/ShaderParser.ts @@ -334,6 +334,16 @@ export class ShaderParser extends CstParser { this.CONSUME(Symbols.Semicolon); }); + private _ruleBreakStatement = this.RULE("_ruleBreakStatement", () => { + this.CONSUME(GLKeywords.Break); + this.CONSUME(Symbols.Semicolon); + }); + + private _ruleContinueStatement = this.RULE("_ruleContinueStatement", () => { + this.CONSUME(GLKeywords.Continue); + this.CONSUME(Symbols.Semicolon); + }); + private _ruleFnStatement = this.RULE("_ruleFnStatement", () => { this.OR([ { ALT: () => this.SUBRULE(this._ruleFnCall) }, @@ -342,6 +352,8 @@ export class ShaderParser extends CstParser { { ALT: () => this.SUBRULE(this._ruleFnVariableDeclaration) }, { ALT: () => this.SUBRULE(this._ruleFnConditionStatement) }, { ALT: () => this.SUBRULE(this._ruleDiscardStatement) }, + { ALT: () => this.SUBRULE(this._ruleBreakStatement) }, + { ALT: () => this.SUBRULE(this._ruleContinueStatement) }, { ALT: () => this.SUBRULE(this._ruleForLoopStatement) }, { ALT: () => this.SUBRULE(this._ruleFn) } ]); diff --git a/packages/shader-lab/src/parser/tokens/GLSLKeywords.ts b/packages/shader-lab/src/parser/tokens/GLSLKeywords.ts index fdaffe3f6a..b05eab0913 100644 --- a/packages/shader-lab/src/parser/tokens/GLSLKeywords.ts +++ b/packages/shader-lab/src/parser/tokens/GLSLKeywords.ts @@ -23,6 +23,8 @@ export const Struct = TokenUtils.createKeywordToken("struct"); export const If = TokenUtils.createKeywordToken("if"); export const Else = TokenUtils.createKeywordToken("else"); export const Discard = TokenUtils.createKeywordToken("discard"); +export const Break = TokenUtils.createKeywordToken("break"); +export const Continue = TokenUtils.createKeywordToken("continue"); export const Void = TokenUtils.createKeywordToken("void"); export const Return = TokenUtils.createKeywordToken("return"); export const For = TokenUtils.createKeywordToken("for"); @@ -30,4 +32,4 @@ export const For = TokenUtils.createKeywordToken("for"); // export const variableTokenList = [GLPosition, GLFragColor]; export const funcTokenList = [Texture2D]; export const macroTokenList = [M_DEFINE, M_IFDEF, M_IFNDEF, M_IF, M_ELSE, M_ELIF, M_ENDIF, M_UNDEFINE, M_INCLUDE]; -export const otherTokenList = [Struct, If, Else, Discard, Void, Return, For]; +export const otherTokenList = [Struct, If, Else, Discard, Void, Return, For, Break, Continue]; diff --git a/packages/shader-lab/src/types.ts b/packages/shader-lab/src/types.ts index dd59e24365..afdfae48b3 100644 --- a/packages/shader-lab/src/types.ts +++ b/packages/shader-lab/src/types.ts @@ -481,6 +481,26 @@ export type _ruleDiscardStatementCstChildren = { Semicolon: IToken[]; }; +export interface _ruleBreakStatementCstNode extends CstNode { + name: "_ruleBreakStatement"; + children: _ruleBreakStatementCstChildren; +} + +export type _ruleBreakStatementCstChildren = { + break: IToken[]; + Semicolon: IToken[]; +}; + +export interface _ruleContinueStatementCstNode extends CstNode { + name: "_ruleContinueStatement"; + children: _ruleContinueStatementCstChildren; +} + +export type _ruleContinueStatementCstChildren = { + continue: IToken[]; + Semicolon: IToken[]; +}; + export interface _ruleFnStatementCstNode extends CstNode { name: "_ruleFnStatement"; children: _ruleFnStatementCstChildren; @@ -493,6 +513,8 @@ export type _ruleFnStatementCstChildren = { _ruleFnVariableDeclaration?: _ruleFnVariableDeclarationCstNode[]; _ruleFnConditionStatement?: _ruleFnConditionStatementCstNode[]; _ruleDiscardStatement?: _ruleDiscardStatementCstNode[]; + _ruleBreakStatement?: _ruleBreakStatementCstNode[]; + _ruleContinueStatement?: _ruleContinueStatementCstNode[]; _ruleForLoopStatement?: _ruleForLoopStatementCstNode[]; _ruleFn?: _ruleFnCstNode[]; }; @@ -1241,6 +1263,8 @@ export interface ICstNodeVisitor extends ICstVisitor { _ruleArrayIndex(children: _ruleArrayIndexCstChildren, param?: IN): OUT; _ruleMultiplicationOperator(children: _ruleMultiplicationOperatorCstChildren, param?: IN): OUT; _ruleDiscardStatement(children: _ruleDiscardStatementCstChildren, param?: IN): OUT; + _ruleBreakStatement(children: _ruleBreakStatementCstChildren, param?: IN): OUT; + _ruleContinueStatement(children: _ruleContinueStatementCstChildren, param?: IN): OUT; _ruleFnStatement(children: _ruleFnStatementCstChildren, param?: IN): OUT; _ruleFnAssignStatement(children: _ruleFnAssignStatementCstChildren, param?: IN): OUT; _ruleForLoopStatement(children: _ruleForLoopStatementCstChildren, param?: IN): OUT; From 8871d9b06712665c786b25c0423e8b8fac15fdc9 Mon Sep 17 00:00:00 2001 From: SwayYan Date: Wed, 11 Oct 2023 16:15:11 +0800 Subject: [PATCH 003/131] fix: typo --- packages/core/src/shader/Shader.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/shader/Shader.ts b/packages/core/src/shader/Shader.ts index a922c528c4..d0eb131856 100644 --- a/packages/core/src/shader/Shader.ts +++ b/packages/core/src/shader/Shader.ts @@ -38,7 +38,7 @@ export class Shader { * // Import shaderLab * import { ShaderLab } from "@galacean/engine-shader-lab"; * // Create engine with shaderLab - * const engine = await WebGLEngine.create({ canvas: "canvas", new ShaderLab() }); + * const engine = await WebGLEngine.create({ canvas: "canvas", shader: new ShaderLab() }); * ... * ``` * From f649a58c681862c5707e4602bb3d49e176bd0b19 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Wed, 1 Nov 2023 12:04:29 +0800 Subject: [PATCH 004/131] fix(shader-lab): Make usepass compatible with buitin shader --- packages/shader-lab/package.json | 1 + packages/shader-lab/src/parser/tokens/Value.ts | 2 +- tests/src/shader-lab/shaders/demo.shader | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/shader-lab/package.json b/packages/shader-lab/package.json index 089fbb7e83..1bc62d74c6 100644 --- a/packages/shader-lab/package.json +++ b/packages/shader-lab/package.json @@ -8,6 +8,7 @@ "license": "MIT", "main": "dist/main.js", "module": "dist/module.js", + "browser": "dist/browser.js", "debug": "src/index.ts", "types": "types/index.d.ts", "scripts": { diff --git a/packages/shader-lab/src/parser/tokens/Value.ts b/packages/shader-lab/src/parser/tokens/Value.ts index 88f689a030..9505758125 100644 --- a/packages/shader-lab/src/parser/tokens/Value.ts +++ b/packages/shader-lab/src/parser/tokens/Value.ts @@ -4,7 +4,7 @@ export const ValueInt = createToken({ name: "ValueInt", pattern: /-?\d+/ }); export const ValueFloat = createToken({ name: "ValueFloat", pattern: /-?\d+\.\d+/ }); export const ValueString = createToken({ name: "ValueString", - pattern: /"[\w\s/\.]*"/ + pattern: /"[\w-\s/\.]*"/ }); export const ValueTrue = createToken({ name: "ValueTrue", pattern: /true/ }); export const ValueFalse = createToken({ name: "ValueFalse", pattern: /false/ }); diff --git a/tests/src/shader-lab/shaders/demo.shader b/tests/src/shader-lab/shaders/demo.shader index ccf3984092..ca29fe4be0 100644 --- a/tests/src/shader-lab/shaders/demo.shader +++ b/tests/src/shader-lab/shaders/demo.shader @@ -114,5 +114,6 @@ Shader "Water" { #endif } } + UsePass "blinn-phong/Default/Forward" } } \ No newline at end of file From e33a66f7ba008562fa9498a0db02b8f19facc5ef Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 2 Nov 2023 15:51:25 +0800 Subject: [PATCH 005/131] fix(shader-lab): compatible with no varying variable --- packages/shader-lab/src/Ast2GLSLUtils.ts | 22 +++++--------- tests/src/shader-lab/ShaderLab.test.ts | 5 ++++ .../src/shader-lab/shaders/noFragArgs.shader | 30 +++++++++++++++++++ 3 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 tests/src/shader-lab/shaders/noFragArgs.shader diff --git a/packages/shader-lab/src/Ast2GLSLUtils.ts b/packages/shader-lab/src/Ast2GLSLUtils.ts index b3bf252e47..5d3c074676 100644 --- a/packages/shader-lab/src/Ast2GLSLUtils.ts +++ b/packages/shader-lab/src/Ast2GLSLUtils.ts @@ -24,20 +24,14 @@ export class Ast2GLSLUtils { // parse varying variables const varyingStructAstNode = context.findGlobal(vertFnAst.content.returnType.content.text)?.ast as StructAstNode; - if (!varyingStructAstNode) { - context.diagnostics.push({ - severity: DiagnosticSeverity.Error, - message: "no varying struct definition", - token: vertFnAst.content.returnType.position - }); - return ""; + if (varyingStructAstNode) { + context.varyingStructInfo.structAstNode = varyingStructAstNode; + context.varyingStructInfo.reference = varyingStructAstNode.content.variables.map((v) => ({ + referenced: false, + property: v, + text: `varying ${v.content.type.serialize(context)} ${v.content.variableNode.serialize(context)}` + })); } - context.varyingStructInfo.structAstNode = varyingStructAstNode; - context.varyingStructInfo.reference = varyingStructAstNode.content.variables.map((v) => ({ - referenced: false, - property: v, - text: `varying ${v.content.type.serialize(context)} ${v.content.variableNode.serialize(context)}` - })); // parsing attribute variables vertFnAst.content.args.forEach((arg) => { @@ -101,7 +95,7 @@ export class Ast2GLSLUtils { } context.setMainFnAst(fragFnAst); - context.varyingStructInfo.objectName = fragFnAst.content.args[0].content.name; + context.varyingStructInfo.objectName = fragFnAst.content.args?.[0].content.name; const fragmentFnStr = fragFnAst.serialize(context); // There may be global variable references in conditional macro statement, so it needs to be serialized first. diff --git a/tests/src/shader-lab/ShaderLab.test.ts b/tests/src/shader-lab/ShaderLab.test.ts index eaa6f66838..bc3baa55f3 100644 --- a/tests/src/shader-lab/ShaderLab.test.ts +++ b/tests/src/shader-lab/ShaderLab.test.ts @@ -200,4 +200,9 @@ describe("ShaderLab", () => { const demoShader = fs.readFileSync(path.join(__dirname, "shaders/triangle.shader")).toString(); glslValidate(demoShader, shaderLab); }); + + it("No frag shader args", () => { + const demoShader = fs.readFileSync(path.join(__dirname, "shaders/noFragArgs.shader")).toString(); + glslValidate(demoShader, shaderLab); + }); }); diff --git a/tests/src/shader-lab/shaders/noFragArgs.shader b/tests/src/shader-lab/shaders/noFragArgs.shader new file mode 100644 index 0000000000..f99b87871f --- /dev/null +++ b/tests/src/shader-lab/shaders/noFragArgs.shader @@ -0,0 +1,30 @@ +Shader "Test-Default" { + Tags { ReplaceTag = "transparent" } + SubShader "Default" { + Pass "test" { + Tags { ReplaceTag = "opaque" } + mat4 renderer_MVPMat; + + struct a2v { + vec4 POSITION; + } + + // struct v2f { + // vec2 uv; + // } + + VertexShader = vert; + FragmentShader = frag; + + v2f vert(a2v v) { + gl_Position = renderer_MVPMat * v.POSITION; + } + + void frag() { + vec3 grayColor = vec3(0.299, 0.587, 0.114); + float gray = dot(grayColor, gl_FragColor.rgb); + gl_FragColor = vec4(gray, gray, gray, gl_FragColor.a); + } + } + } +} \ No newline at end of file From 41ef06fc71c57a40a3976d4b479583eec8d285dc Mon Sep 17 00:00:00 2001 From: Sway007 Date: Mon, 6 Nov 2023 11:01:22 +0800 Subject: [PATCH 006/131] feat(shader-lab): detect mismatch return type --- packages/shader-lab/src/ast-node/AstNode.ts | 13 +++++++++++++ packages/shader-lab/src/ast-node/AstNodeContent.ts | 4 +++- tests/src/shader-lab/shaders/noFragArgs.shader | 2 +- tests/src/shader-lab/shaders/planarShadow.shader | 1 + 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/shader-lab/src/ast-node/AstNode.ts b/packages/shader-lab/src/ast-node/AstNode.ts index 2e7da08a96..fd26a17b25 100644 --- a/packages/shader-lab/src/ast-node/AstNode.ts +++ b/packages/shader-lab/src/ast-node/AstNode.ts @@ -202,6 +202,18 @@ export class FnAstNode extends AstNode { } const body = this.content.body.serialize(context); + if ( + (this.content.returnType.content.text === "void" && this.content.returnStatement) || + (this.content.returnType.content.text !== "void" && !this.content.returnStatement) + ) { + context.diagnostics.push({ + severity: DiagnosticSeverity.Error, + message: "Mismatched return type", + token: this.position + }); + throw "Mismatched return type"; + } + context.functionAstStack.pop(); return `${returnType} ${fnName} (${args}) {\n${body}\n}`; } @@ -493,6 +505,7 @@ export class FnArrayVariableAstNode extends AstNode export class FnReturnStatementAstNode extends AstNode { override _doSerialization(context: RuntimeContext): string { + context.currentFunctionInfo.fnAst.content.returnStatement = this; if (context.currentFunctionInfo.fnAst === context.currentMainFnAst) { return ""; } diff --git a/packages/shader-lab/src/ast-node/AstNodeContent.ts b/packages/shader-lab/src/ast-node/AstNodeContent.ts index 39606d5ce8..3f837ff48c 100644 --- a/packages/shader-lab/src/ast-node/AstNodeContent.ts +++ b/packages/shader-lab/src/ast-node/AstNodeContent.ts @@ -42,7 +42,8 @@ import { FnVariableDeclareUnitAstNode, FnMacroUndefineAstNode, FnMacroConditionAstNode, - RenderQueueValueAstNode + RenderQueueValueAstNode, + FnReturnStatementAstNode } from "./AstNode"; export interface IShaderAstContent { @@ -111,6 +112,7 @@ export interface IFnAstContent { name: string; args: FnArgAstNode[]; body: AstNode; + returnStatement?: FnReturnStatementAstNode; } export interface IFnBodyAstContent { diff --git a/tests/src/shader-lab/shaders/noFragArgs.shader b/tests/src/shader-lab/shaders/noFragArgs.shader index f99b87871f..438888ced9 100644 --- a/tests/src/shader-lab/shaders/noFragArgs.shader +++ b/tests/src/shader-lab/shaders/noFragArgs.shader @@ -16,7 +16,7 @@ Shader "Test-Default" { VertexShader = vert; FragmentShader = frag; - v2f vert(a2v v) { + void vert(a2v v) { gl_Position = renderer_MVPMat * v.POSITION; } diff --git a/tests/src/shader-lab/shaders/planarShadow.shader b/tests/src/shader-lab/shaders/planarShadow.shader index 954033c5fe..eb0d494de3 100644 --- a/tests/src/shader-lab/shaders/planarShadow.shader +++ b/tests/src/shader-lab/shaders/planarShadow.shader @@ -123,6 +123,7 @@ Shader "PlanarShadow" { // shadow color o.color = u_planarShadowColor; o.color.a *= falloff; + return o; } VertexShader = vert; From b5214fc9c4167448195a966668aa7f9c9af013d8 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 7 Nov 2023 16:13:35 +0800 Subject: [PATCH 007/131] fix(shader-lab): renderState assignment --- packages/shader-lab/src/RuntimeContext.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/shader-lab/src/RuntimeContext.ts b/packages/shader-lab/src/RuntimeContext.ts index 5b70213a89..1dd6083b45 100644 --- a/packages/shader-lab/src/RuntimeContext.ts +++ b/packages/shader-lab/src/RuntimeContext.ts @@ -205,17 +205,16 @@ export default class RuntimeContext { const [constantProps, variableProps] = ret.renderStates; this.payload = { parsingRenderState: true }; - const tmpRenderStates = ast.content.renderStates; + const tmpRenderStates = ast.content.renderStates ?? []; ast.content.properties.forEach((prop) => this._parsePassProperty(>ast, prop, ret, tmpRenderStates) ); - if (tmpRenderStates) { - for (const rs of tmpRenderStates) { - const [constP, variableP] = rs.getContentValue(this).properties; - Object.assign(constantProps, constP); - Object.assign(variableProps, variableP); - } + for (const rs of tmpRenderStates) { + const [constP, variableP] = rs.getContentValue(this).properties; + Object.assign(constantProps, constP); + Object.assign(variableProps, variableP); } + this.payload = undefined; const renderQueueNode = ast.content.renderQueue; From f36ce020b558f0b0f792565396c1c91cbff47b32 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Mon, 13 Nov 2023 10:01:57 +0800 Subject: [PATCH 008/131] feat: extend material loader data type --- packages/loader/src/MaterialLoader.ts | 4 ++++ .../resource-deserialize/resources/schema/MaterialSchema.ts | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/loader/src/MaterialLoader.ts b/packages/loader/src/MaterialLoader.ts index 145f5c8780..a8bfd96884 100644 --- a/packages/loader/src/MaterialLoader.ts +++ b/packages/loader/src/MaterialLoader.ts @@ -74,6 +74,10 @@ class MaterialLoader extends Loader { }) ); break; + case "Boolean": + materialShaderData.setInt(key, value ? 1 : 0); + case "Integer": + materialShaderData.setInt(key, Number(value)); } } diff --git a/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts b/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts index 07410cec8e..377ea86126 100644 --- a/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts +++ b/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts @@ -90,7 +90,7 @@ export interface IMaterialSchema { shader: string; shaderData: { [key: string]: { - type: "Vector2" | "Vector3" | "Vector4" | "Color" | "Float" | "Texture"; + type: "Vector2" | "Vector3" | "Vector4" | "Color" | "Float" | "Texture" | "Boolean" | "Integer"; value: IVector3 | IVector2 | IColor | number | IAssetRef; }; }; From 634236f90337ca8e0a1643c933e7cb2caf54506d Mon Sep 17 00:00:00 2001 From: Sway007 Date: Mon, 13 Nov 2023 14:19:40 +0800 Subject: [PATCH 009/131] fix(shader-lab): glsl type pattern --- .../shader-lab/src/parser/tokens/GLSLTypes.ts | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/shader-lab/src/parser/tokens/GLSLTypes.ts b/packages/shader-lab/src/parser/tokens/GLSLTypes.ts index 8f767dff0c..fda23bde90 100644 --- a/packages/shader-lab/src/parser/tokens/GLSLTypes.ts +++ b/packages/shader-lab/src/parser/tokens/GLSLTypes.ts @@ -1,26 +1,26 @@ import { createToken } from "chevrotain"; -export const glsl_mat2 = createToken({ name: "glsl_mat2", pattern: /mat2/ }); -export const glsl_mat3 = createToken({ name: "glsl_mat3", pattern: /mat3/ }); -export const glsl_mat4 = createToken({ name: "glsl_mat4", pattern: /mat4/ }); +export const glsl_mat2 = createToken({ name: "glsl_mat2", pattern: /mat2\s/ }); +export const glsl_mat3 = createToken({ name: "glsl_mat3", pattern: /mat3\s/ }); +export const glsl_mat4 = createToken({ name: "glsl_mat4", pattern: /mat4\s/ }); -export const glsl_vec2 = createToken({ name: "glsl_vec2", pattern: /vec2/ }); -export const glsl_vec3 = createToken({ name: "glsl_vec3", pattern: /vec3/ }); -export const glsl_vec4 = createToken({ name: "glsl_vec4", pattern: /vec4/ }); +export const glsl_vec2 = createToken({ name: "glsl_vec2", pattern: /vec2\s/ }); +export const glsl_vec3 = createToken({ name: "glsl_vec3", pattern: /vec3\s/ }); +export const glsl_vec4 = createToken({ name: "glsl_vec4", pattern: /vec4\s/ }); -export const glsl_ivec2 = createToken({ name: "glsl_ivec2", pattern: /ivec2/ }); -export const glsl_ivec3 = createToken({ name: "glsl_ivec3", pattern: /ivec3/ }); -export const glsl_ivec4 = createToken({ name: "glsl_ivec4", pattern: /ivec4/ }); +export const glsl_ivec2 = createToken({ name: "glsl_ivec2", pattern: /ivec2\s/ }); +export const glsl_ivec3 = createToken({ name: "glsl_ivec3", pattern: /ivec3\s/ }); +export const glsl_ivec4 = createToken({ name: "glsl_ivec4", pattern: /ivec4\s/ }); -export const glsl_float = createToken({ name: "glsl_float", pattern: /float/ }); -export const glsl_int = createToken({ name: "glsl_int", pattern: /int/ }); +export const glsl_float = createToken({ name: "glsl_float", pattern: /float\s/ }); +export const glsl_int = createToken({ name: "glsl_int", pattern: /int\s/ }); export const glsl_sampler2D = createToken({ name: "glsl_sampler2D", - pattern: /sampler2D/ + pattern: /sampler2D\s/ }); -export const glsl_sampler2DArray = createToken({ name: "sampler2DArray", pattern: /sampler2DArray/ }); +export const glsl_sampler2DArray = createToken({ name: "sampler2DArray", pattern: /sampler2DArray / }); export const tokenList = [ glsl_ivec2, From 91e6fa4aab6df2da732a7b53227cd61714505fab Mon Sep 17 00:00:00 2001 From: Sway007 Date: Mon, 13 Nov 2023 14:39:27 +0800 Subject: [PATCH 010/131] fix(shader-lab): glsl type pattern --- packages/shader-lab/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/shader-lab/package.json b/packages/shader-lab/package.json index d79611f7e6..5bf22de905 100644 --- a/packages/shader-lab/package.json +++ b/packages/shader-lab/package.json @@ -8,7 +8,6 @@ "license": "MIT", "main": "dist/main.js", "module": "dist/module.js", - "browser": "dist/browser.js", "debug": "src/index.ts", "types": "types/index.d.ts", "scripts": { From 3932448c79d48428521e6c172c6352702a7cb838 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Wed, 15 Nov 2023 14:27:54 +0800 Subject: [PATCH 011/131] fix: switch case break --- packages/loader/src/MaterialLoader.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/loader/src/MaterialLoader.ts b/packages/loader/src/MaterialLoader.ts index a8bfd96884..6048f24301 100644 --- a/packages/loader/src/MaterialLoader.ts +++ b/packages/loader/src/MaterialLoader.ts @@ -76,8 +76,10 @@ class MaterialLoader extends Loader { break; case "Boolean": materialShaderData.setInt(key, value ? 1 : 0); + break; case "Integer": materialShaderData.setInt(key, Number(value)); + break; } } From b5fa1e68465fd2f394cfca9e5f5e30ba18672263 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Mon, 11 Mar 2024 20:55:35 +0800 Subject: [PATCH 012/131] feat: add loader for shader asset --- packages/core/src/asset/AssetType.ts | 4 + packages/loader/src/MaterialLoader.ts | 118 +++++++++--------- packages/loader/src/ShaderChunkLoader.ts | 20 +++ packages/loader/src/ShaderLoader.ts | 41 ++++++ packages/loader/src/index.ts | 2 + .../resources/schema/BasicSchema.ts | 2 + .../resources/schema/MaterialSchema.ts | 3 +- 7 files changed, 132 insertions(+), 58 deletions(-) create mode 100644 packages/loader/src/ShaderChunkLoader.ts create mode 100644 packages/loader/src/ShaderLoader.ts diff --git a/packages/core/src/asset/AssetType.ts b/packages/core/src/asset/AssetType.ts index 0da3e4a24f..008af6ea37 100644 --- a/packages/core/src/asset/AssetType.ts +++ b/packages/core/src/asset/AssetType.ts @@ -23,6 +23,10 @@ export enum AssetType { TextureCube = "TextureCube", /** Material. */ Material = "Material", + /** Shader */ + Shader = "Shader", + /** Shader Chunk */ + ShaderChunk = "ShaderChunk", /** Mesh. */ Mesh = "Mesh", /** AnimationClip. */ diff --git a/packages/loader/src/MaterialLoader.ts b/packages/loader/src/MaterialLoader.ts index 6048f24301..e7e0ec09b8 100644 --- a/packages/loader/src/MaterialLoader.ts +++ b/packages/loader/src/MaterialLoader.ts @@ -32,70 +32,74 @@ class MaterialLoader extends Loader { }) .then((materialSchema: IMaterialSchema) => { const engine = resourceManager.engine; - const { name, shader, shaderData, macros, renderState } = materialSchema; - const material = new Material(engine, Shader.find(shader)); - material.name = name; + const { name, shaderData, macros, renderState, shaderRef } = materialSchema; - const texturePromises = new Array>(); - const materialShaderData = material.shaderData; - for (let key in shaderData) { - const { type, value } = shaderData[key]; + // @ts-ignore + resourceManager.getResourceByRef(shaderRef).then((shader) => { + const material = new Material(engine, shader); + material.name = name; - switch (type) { - case "Vector2": - materialShaderData.setVector2(key, new Vector2((value).x, (value).y)); - break; - case "Vector3": - materialShaderData.setVector3( - key, - new Vector3((value).x, (value).y, (value).z) - ); - break; - case "Vector4": - materialShaderData.setVector4( - key, - new Vector4((value).x, (value).y, (value).z, (value).w) - ); - break; - case "Color": - materialShaderData.setColor( - key, - new Color((value).r, (value).g, (value).b, (value).a) - ); - break; - case "Float": - materialShaderData.setFloat(key, value); - break; - case "Texture": - texturePromises.push( - // @ts-ignore - resourceManager.getResourceByRef(value).then((texture) => { - materialShaderData.setTexture(key, texture); - }) - ); - break; - case "Boolean": - materialShaderData.setInt(key, value ? 1 : 0); - break; - case "Integer": - materialShaderData.setInt(key, Number(value)); - break; + const texturePromises = new Array>(); + const materialShaderData = material.shaderData; + for (let key in shaderData) { + const { type, value } = shaderData[key]; + + switch (type) { + case "Vector2": + materialShaderData.setVector2(key, new Vector2((value).x, (value).y)); + break; + case "Vector3": + materialShaderData.setVector3( + key, + new Vector3((value).x, (value).y, (value).z) + ); + break; + case "Vector4": + materialShaderData.setVector4( + key, + new Vector4((value).x, (value).y, (value).z, (value).w) + ); + break; + case "Color": + materialShaderData.setColor( + key, + new Color((value).r, (value).g, (value).b, (value).a) + ); + break; + case "Float": + materialShaderData.setFloat(key, value); + break; + case "Texture": + texturePromises.push( + // @ts-ignore + resourceManager.getResourceByRef(value).then((texture) => { + materialShaderData.setTexture(key, texture); + }) + ); + break; + case "Boolean": + materialShaderData.setInt(key, value ? 1 : 0); + break; + case "Integer": + materialShaderData.setInt(key, Number(value)); + break; + } } - } - for (let i = 0, length = macros.length; i < length; i++) { - const { name, value } = macros[i]; - if (value == undefined) { - materialShaderData.enableMacro(name); - } else { - materialShaderData.enableMacro(name, value); + for (let i = 0, length = macros.length; i < length; i++) { + const { name, value } = macros[i]; + if (value == undefined) { + materialShaderData.enableMacro(name); + } else { + materialShaderData.enableMacro(name, value); + } } - } - parseProperty(material, "renderState", renderState); + parseProperty(material, "renderState", renderState); - return Promise.all(texturePromises).then(() => { - resolve(material); + return Promise.all(texturePromises).then(() => { + resolve(material); + }); }); }) .catch(reject); diff --git a/packages/loader/src/ShaderChunkLoader.ts b/packages/loader/src/ShaderChunkLoader.ts new file mode 100644 index 0000000000..f85229d904 --- /dev/null +++ b/packages/loader/src/ShaderChunkLoader.ts @@ -0,0 +1,20 @@ +import { + AssetPromise, + AssetType, + LoadItem, + Loader, + ResourceManager, + Shader, + ShaderFactory, + resourceLoader +} from "@galacean/engine-core"; + +@resourceLoader(AssetType.ShaderChunk, ["glsl"], false) +class ShaderLoader extends Loader { + load(item: LoadItem, resourceManager: ResourceManager): AssetPromise { + return this.request(item.url, { ...item, type: "text" }).then((code) => { + const { includeKey } = item.params; + ShaderFactory.registerInclude(includeKey, code); + }); + } +} diff --git a/packages/loader/src/ShaderLoader.ts b/packages/loader/src/ShaderLoader.ts new file mode 100644 index 0000000000..5e0cdb705e --- /dev/null +++ b/packages/loader/src/ShaderLoader.ts @@ -0,0 +1,41 @@ +import { + AssetPromise, + AssetType, + LoadItem, + Loader, + ResourceManager, + Shader, + resourceLoader +} from "@galacean/engine-core"; + +@resourceLoader(AssetType.Shader, ["gs", "gsl"], false) +class ShaderLoader extends Loader { + load(item: LoadItem, resourceManager: ResourceManager): AssetPromise { + return this.request(item.url, { ...item, type: "text" }).then((code: string) => { + const builtinShader = this.getBuiltinShader(code); + if (builtinShader) { + return Shader.find(builtinShader); + } + + const matches = code.matchAll(/^[ \t]*#include +"([^$\\"]+)"/gm); + return Promise.all( + Array.from(matches).map((m) => { + const path = m[1]; + if (path) { + // @ts-ignore + const resource = resourceManager._virtualPathMap[path]; + if (!resource) return; + return resourceManager.load({ type: AssetType.ShaderChunk, url: resource, params: { includeKey: path } }); + } + }) + ).then(() => { + return Shader.create(code); + }); + }); + } + + private getBuiltinShader(code: string) { + const match = code.match(/^\s*\/\/\s*@builtin\s+(\w+)/); + if (match && match[1]) return match[1]; + } +} diff --git a/packages/loader/src/index.ts b/packages/loader/src/index.ts index 1983a18981..cd515e8d08 100644 --- a/packages/loader/src/index.ts +++ b/packages/loader/src/index.ts @@ -18,6 +18,8 @@ import "./SpriteLoader"; import "./Texture2DLoader"; import "./TextureCubeLoader"; import "./ktx2/KTX2Loader"; +import "./ShaderLoader"; +import "./ShaderChunkLoader"; export { GLTFLoader } from "./GLTFLoader"; export type { GLTFParams } from "./GLTFLoader"; diff --git a/packages/loader/src/resource-deserialize/resources/schema/BasicSchema.ts b/packages/loader/src/resource-deserialize/resources/schema/BasicSchema.ts index 2a36864ee8..4b58ce321b 100644 --- a/packages/loader/src/resource-deserialize/resources/schema/BasicSchema.ts +++ b/packages/loader/src/resource-deserialize/resources/schema/BasicSchema.ts @@ -92,3 +92,5 @@ export type IBasicType = export type IAssetRef = { key?: string; refId: string }; export type IEntityRef = { entityId: string }; + +export type IShaderRef = { refId: string }; diff --git a/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts b/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts index 377ea86126..8364ac14b7 100644 --- a/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts +++ b/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts @@ -7,7 +7,7 @@ import { RenderQueueType, StencilOperation } from "@galacean/engine-core"; -import type { IAssetRef, IColor, IVector2, IVector3 } from "./BasicSchema"; +import type { IAssetRef, IColor, IShaderRef, IVector2, IVector3 } from "./BasicSchema"; export interface IRenderState { /** Blend state. */ @@ -96,4 +96,5 @@ export interface IMaterialSchema { }; macros: Array<{ name: string; value?: string }>; renderState: IRenderState; + shaderRef: IShaderRef; } From d8d9a873f38d25fba2ffaae2c46b5d4dd37d5453 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 12 Mar 2024 13:44:15 +0800 Subject: [PATCH 013/131] feat: compatible with 1.2-pre version --- packages/loader/src/MaterialLoader.ts | 145 ++++++++++++++------------ 1 file changed, 80 insertions(+), 65 deletions(-) diff --git a/packages/loader/src/MaterialLoader.ts b/packages/loader/src/MaterialLoader.ts index e7e0ec09b8..99a01ddbdd 100644 --- a/packages/loader/src/MaterialLoader.ts +++ b/packages/loader/src/MaterialLoader.ts @@ -1,6 +1,7 @@ import { AssetPromise, AssetType, + Engine, LoadItem, Loader, Material, @@ -32,77 +33,91 @@ class MaterialLoader extends Loader { }) .then((materialSchema: IMaterialSchema) => { const engine = resourceManager.engine; - const { name, shaderData, macros, renderState, shaderRef } = materialSchema; + const { shaderRef, shader } = materialSchema; - // @ts-ignore - resourceManager.getResourceByRef(shaderRef).then((shader) => { - const material = new Material(engine, shader); - material.name = name; + if (shaderRef) { + resolve( + resourceManager + // @ts-ignore + .getResourceByRef(shaderRef) + .then((shaderObject) => this.getMaterialByShader(materialSchema, shaderObject, engine)) + ); + } else { + // compatible with 1.2-pre version material schema + const shaderObject = Shader.find(shader); + resolve(this.getMaterialByShader(materialSchema, shaderObject, engine)); + } + }) + .catch(reject); + }); + } + + private getMaterialByShader(materialSchema: IMaterialSchema, shader: Shader, engine: Engine): Promise { + const { name, shaderData, macros, renderState } = materialSchema; - const texturePromises = new Array>(); - const materialShaderData = material.shaderData; - for (let key in shaderData) { - const { type, value } = shaderData[key]; + const material = new Material(engine, shader); + material.name = name; - switch (type) { - case "Vector2": - materialShaderData.setVector2(key, new Vector2((value).x, (value).y)); - break; - case "Vector3": - materialShaderData.setVector3( - key, - new Vector3((value).x, (value).y, (value).z) - ); - break; - case "Vector4": - materialShaderData.setVector4( - key, - new Vector4((value).x, (value).y, (value).z, (value).w) - ); - break; - case "Color": - materialShaderData.setColor( - key, - new Color((value).r, (value).g, (value).b, (value).a) - ); - break; - case "Float": - materialShaderData.setFloat(key, value); - break; - case "Texture": - texturePromises.push( - // @ts-ignore - resourceManager.getResourceByRef(value).then((texture) => { - materialShaderData.setTexture(key, texture); - }) - ); - break; - case "Boolean": - materialShaderData.setInt(key, value ? 1 : 0); - break; - case "Integer": - materialShaderData.setInt(key, Number(value)); - break; - } - } + const texturePromises = new Array>(); + const materialShaderData = material.shaderData; + for (let key in shaderData) { + const { type, value } = shaderData[key]; - for (let i = 0, length = macros.length; i < length; i++) { - const { name, value } = macros[i]; - if (value == undefined) { - materialShaderData.enableMacro(name); - } else { - materialShaderData.enableMacro(name, value); - } - } + switch (type) { + case "Vector2": + materialShaderData.setVector2(key, new Vector2((value).x, (value).y)); + break; + case "Vector3": + materialShaderData.setVector3( + key, + new Vector3((value).x, (value).y, (value).z) + ); + break; + case "Vector4": + materialShaderData.setVector4( + key, + new Vector4((value).x, (value).y, (value).z, (value).w) + ); + break; + case "Color": + materialShaderData.setColor( + key, + new Color((value).r, (value).g, (value).b, (value).a) + ); + break; + case "Float": + materialShaderData.setFloat(key, value); + break; + case "Texture": + texturePromises.push( + // @ts-ignore + resourceManager.getResourceByRef(value).then((texture) => { + materialShaderData.setTexture(key, texture); + }) + ); + break; + case "Boolean": + materialShaderData.setInt(key, value ? 1 : 0); + break; + case "Integer": + materialShaderData.setInt(key, Number(value)); + break; + } + } - parseProperty(material, "renderState", renderState); + for (let i = 0, length = macros.length; i < length; i++) { + const { name, value } = macros[i]; + if (value == undefined) { + materialShaderData.enableMacro(name); + } else { + materialShaderData.enableMacro(name, value); + } + } - return Promise.all(texturePromises).then(() => { - resolve(material); - }); - }); - }) - .catch(reject); + parseProperty(material, "renderState", renderState); + + return Promise.all(texturePromises).then(() => { + return material; }); } } From a5f76dcac738b0ddc765c0e4a1a97b8b1d92082d Mon Sep 17 00:00:00 2001 From: Sway007 Date: Mon, 18 Mar 2024 10:10:01 +0800 Subject: [PATCH 014/131] feat: opt code --- packages/shader-lab/src/preprocessor/Preprocessor.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/shader-lab/src/preprocessor/Preprocessor.ts b/packages/shader-lab/src/preprocessor/Preprocessor.ts index 4de2b3ff3a..ecff714b3c 100644 --- a/packages/shader-lab/src/preprocessor/Preprocessor.ts +++ b/packages/shader-lab/src/preprocessor/Preprocessor.ts @@ -129,7 +129,6 @@ export class Preprocessor { const tokenizer = this._tokenizer; const variable = this.consumeToken(this._tokenizer); - if (variable.res?.text === "xxx") debugger; if (!variable.res || variable.end) throw "No defined variable"; if (this._definePairs.get(variable.res.text)) throw `redefined macro: ${variable.res.text}`; From 326afeae90c143ec7de8c6ef8d36646691f11a96 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Fri, 26 Apr 2024 10:47:31 +0800 Subject: [PATCH 015/131] fix: keep macro declaration --- packages/shader-lab/src/RuntimeContext.ts | 5 +---- packages/shader-lab/src/preprocessor/Preprocessor.ts | 8 ++++---- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/packages/shader-lab/src/RuntimeContext.ts b/packages/shader-lab/src/RuntimeContext.ts index bad888bc0f..1a2cf6ea01 100644 --- a/packages/shader-lab/src/RuntimeContext.ts +++ b/packages/shader-lab/src/RuntimeContext.ts @@ -314,10 +314,7 @@ export default class RuntimeContext { getExtendedDefineMacros() { return Array.from(this._preprocessor._definePairs.entries()) .map(([k, v]) => { - let ret = `#define ${k}`; - if (!v.isFunction) { - ret += ` ${v.replacer}`; - } + let ret = `#define ${k} ${v.originText}`; return ret; }) .join("\n"); diff --git a/packages/shader-lab/src/preprocessor/Preprocessor.ts b/packages/shader-lab/src/preprocessor/Preprocessor.ts index ecff714b3c..fd1d543671 100644 --- a/packages/shader-lab/src/preprocessor/Preprocessor.ts +++ b/packages/shader-lab/src/preprocessor/Preprocessor.ts @@ -16,8 +16,8 @@ export class Preprocessor { private _curMacroLvl = 0; /** @internal */ - _definePairs: Map = new Map(); - private set definePairs(pairs: Map) { + _definePairs: Map = new Map(); + private set definePairs(pairs: Map) { this._definePairs = pairs; } private _replacers: IReplaceSegment[] = []; @@ -156,9 +156,9 @@ export class Preprocessor { const idx = macroArgs.findIndex((item) => item === m); return args[idx]; }); - this._definePairs.set(variable.res.text, { isFunction: true, replacer }); + this._definePairs.set(variable.res.text, { isFunction: true, replacer, originText: chunk }); } else { - this._definePairs.set(variable.res.text, { isFunction: false, replacer: chunk }); + this._definePairs.set(variable.res.text, { isFunction: false, replacer: chunk, originText: chunk }); } this._replacers.push({ startIdx: macroToken.start.index, endIdx: tokenizer.curIndex, replace: "" }); From a46c5dcb93d1e66cb26f351d67fb03514700b8dd Mon Sep 17 00:00:00 2001 From: Sway007 Date: Fri, 26 Apr 2024 11:04:12 +0800 Subject: [PATCH 016/131] test: add unitest --- tests/src/shader-lab/ShaderLab.test.ts | 4 ++++ tests/src/shader-lab/shaders/demo.shader | 2 ++ 2 files changed, 6 insertions(+) diff --git a/tests/src/shader-lab/ShaderLab.test.ts b/tests/src/shader-lab/ShaderLab.test.ts index 42d59e4422..ea2f80b555 100644 --- a/tests/src/shader-lab/ShaderLab.test.ts +++ b/tests/src/shader-lab/ShaderLab.test.ts @@ -133,6 +133,10 @@ describe("ShaderLab", () => { expect(usePass).to.equal("pbr/Default/Forward"); }); + it("marco completeness", () => { + expect(pass.vertexSource.includes("#define saturate clamp( a, 0.0, 1.0 )")).to.true; + }); + it("render state", () => { expect(pass.renderStates).not.be.null; diff --git a/tests/src/shader-lab/shaders/demo.shader b/tests/src/shader-lab/shaders/demo.shader index 7f7b7a21b3..a529185d3e 100644 --- a/tests/src/shader-lab/shaders/demo.shader +++ b/tests/src/shader-lab/shaders/demo.shader @@ -36,6 +36,8 @@ Shader "Water" { UsePass "pbr/Default/Forward" Pass "default" { + #define saturate( a ) clamp( a, 0.0, 1.0 ) + Tags { ReplacementTag = "Opaque", Tag2 = true, Tag3 = 1.9 } struct a2v { From bfd4b1dfd1367800f70cacd2cd850bee1818491f Mon Sep 17 00:00:00 2001 From: Sway007 Date: Mon, 6 May 2024 14:55:43 +0800 Subject: [PATCH 017/131] fix: macro function & line change --- packages/shader-lab/src/RuntimeContext.ts | 12 ++++++------ packages/shader-lab/src/preprocessor/Preprocessor.ts | 12 ++++++++++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/shader-lab/src/RuntimeContext.ts b/packages/shader-lab/src/RuntimeContext.ts index 1a2cf6ea01..52028b43f9 100644 --- a/packages/shader-lab/src/RuntimeContext.ts +++ b/packages/shader-lab/src/RuntimeContext.ts @@ -138,11 +138,12 @@ export default class RuntimeContext { addDiagnostic(diagnostic: IDiagnostic) { let offset = this._parsingContext.getTextLineOffsetAt(diagnostic.token.start.index); + let token: IPositionRange = { start: { ...diagnostic.token.start }, end: { ...diagnostic.token.end } }; if (offset) { - diagnostic.token.start.line += offset; - diagnostic.token.end.line += offset; + token.start.line += offset; + token.end.line += offset; } - this._diagnostics.push(diagnostic); + this._diagnostics.push({ ...diagnostic, token }); } setSerializingNode(node: AstNode) { @@ -313,9 +314,8 @@ export default class RuntimeContext { // If be aware of the complete input macro list when parsing, the snippets below is not required. getExtendedDefineMacros() { return Array.from(this._preprocessor._definePairs.entries()) - .map(([k, v]) => { - let ret = `#define ${k} ${v.originText}`; - return ret; + .map(([_, v]) => { + return v.originText; }) .join("\n"); } diff --git a/packages/shader-lab/src/preprocessor/Preprocessor.ts b/packages/shader-lab/src/preprocessor/Preprocessor.ts index fd1d543671..b0279d8383 100644 --- a/packages/shader-lab/src/preprocessor/Preprocessor.ts +++ b/packages/shader-lab/src/preprocessor/Preprocessor.ts @@ -156,9 +156,17 @@ export class Preprocessor { const idx = macroArgs.findIndex((item) => item === m); return args[idx]; }); - this._definePairs.set(variable.res.text, { isFunction: true, replacer, originText: chunk }); + this._definePairs.set(variable.res.text, { + isFunction: true, + replacer, + originText: `#define ${variable.res.text}(${macroArgs.join(",")}) ${chunk}` + }); } else { - this._definePairs.set(variable.res.text, { isFunction: false, replacer: chunk, originText: chunk }); + this._definePairs.set(variable.res.text, { + isFunction: false, + replacer: chunk, + originText: `#define ${variable.res.text} ${chunk}` + }); } this._replacers.push({ startIdx: macroToken.start.index, endIdx: tokenizer.curIndex, replace: "" }); From 5fe627d8d6aa16247cbfb2e01a939376da0ec56d Mon Sep 17 00:00:00 2001 From: Sway007 Date: Mon, 6 May 2024 15:07:08 +0800 Subject: [PATCH 018/131] fix: test case --- tests/src/shader-lab/ShaderLab.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/shader-lab/ShaderLab.test.ts b/tests/src/shader-lab/ShaderLab.test.ts index ea2f80b555..afbdbd6720 100644 --- a/tests/src/shader-lab/ShaderLab.test.ts +++ b/tests/src/shader-lab/ShaderLab.test.ts @@ -134,7 +134,7 @@ describe("ShaderLab", () => { }); it("marco completeness", () => { - expect(pass.vertexSource.includes("#define saturate clamp( a, 0.0, 1.0 )")).to.true; + expect(pass.vertexSource.includes("#define saturate(a) clamp( a, 0.0, 1.0 )")).to.true; }); it("render state", () => { From d65e88acd898fe8d89d8eb5ed90cdd4e6fa2ec89 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 7 May 2024 14:00:00 +0800 Subject: [PATCH 019/131] "v0.0.0-experimental-shaderlab.0" --- packages/core/package.json | 2 +- packages/design/package.json | 2 +- packages/galacean/package.json | 2 +- packages/loader/package.json | 2 +- packages/math/package.json | 2 +- packages/physics-lite/package.json | 2 +- packages/physics-physx/package.json | 2 +- packages/rhi-webgl/package.json | 2 +- packages/shader-lab/package.json | 2 +- packages/xr-webxr/package.json | 2 +- packages/xr/package.json | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index 77b01e6c5c..4d9252c3fb 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-core", - "version": "1.2.0-beta.2", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/design/package.json b/packages/design/package.json index 88cae1feab..b74888de6d 100644 --- a/packages/design/package.json +++ b/packages/design/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-design", - "version": "1.2.0-beta.2", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/galacean/package.json b/packages/galacean/package.json index f76c1e3ff5..c72a01b5d3 100644 --- a/packages/galacean/package.json +++ b/packages/galacean/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine", - "version": "1.2.0-beta.2", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/loader/package.json b/packages/loader/package.json index 908cae677f..6aa4229881 100644 --- a/packages/loader/package.json +++ b/packages/loader/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-loader", - "version": "1.2.0-beta.2", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/math/package.json b/packages/math/package.json index c456af7596..4278d42c80 100644 --- a/packages/math/package.json +++ b/packages/math/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-math", - "version": "1.2.0-beta.2", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/physics-lite/package.json b/packages/physics-lite/package.json index 3ae0ad8083..e02c791b88 100644 --- a/packages/physics-lite/package.json +++ b/packages/physics-lite/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-physics-lite", - "version": "1.2.0-beta.2", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/physics-physx/package.json b/packages/physics-physx/package.json index cfcaa27190..b230e45879 100644 --- a/packages/physics-physx/package.json +++ b/packages/physics-physx/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-physics-physx", - "version": "1.2.0-beta.2", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/rhi-webgl/package.json b/packages/rhi-webgl/package.json index cd35a713b8..deca2a084f 100644 --- a/packages/rhi-webgl/package.json +++ b/packages/rhi-webgl/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-rhi-webgl", - "version": "1.2.0-beta.2", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/shader-lab/package.json b/packages/shader-lab/package.json index a5b7379c85..ca30fc7e8d 100644 --- a/packages/shader-lab/package.json +++ b/packages/shader-lab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-shader-lab", - "version": "1.2.0-beta.2", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/xr-webxr/package.json b/packages/xr-webxr/package.json index 7e23be7a19..31d5ef2721 100644 --- a/packages/xr-webxr/package.json +++ b/packages/xr-webxr/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-xr-webxr", - "version": "1.2.0-beta.2", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/xr/package.json b/packages/xr/package.json index 0e170982ba..dea641cc33 100644 --- a/packages/xr/package.json +++ b/packages/xr/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-xr", - "version": "1.2.0-beta.2", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From 81ba3059a8501af42980753c9b526a39a9bcf8ee Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 7 May 2024 14:02:12 +0800 Subject: [PATCH 020/131] "v0.0.0-experimental-shaderlab.1" --- packages/core/package.json | 2 +- packages/design/package.json | 2 +- packages/galacean/package.json | 2 +- packages/loader/package.json | 2 +- packages/math/package.json | 2 +- packages/physics-lite/package.json | 2 +- packages/physics-physx/package.json | 2 +- packages/rhi-webgl/package.json | 2 +- packages/shader-lab/package.json | 2 +- packages/xr-webxr/package.json | 2 +- packages/xr/package.json | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index 4d9252c3fb..7e198f3d0c 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-core", - "version": "0.0.0-experimental-shaderlab.0", + "version": "0.0.0-experimental-shaderlab.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/design/package.json b/packages/design/package.json index b74888de6d..9cedb7ae0a 100644 --- a/packages/design/package.json +++ b/packages/design/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-design", - "version": "0.0.0-experimental-shaderlab.0", + "version": "0.0.0-experimental-shaderlab.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/galacean/package.json b/packages/galacean/package.json index c72a01b5d3..ef3dd2eec1 100644 --- a/packages/galacean/package.json +++ b/packages/galacean/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine", - "version": "0.0.0-experimental-shaderlab.0", + "version": "0.0.0-experimental-shaderlab.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/loader/package.json b/packages/loader/package.json index 6aa4229881..f342362068 100644 --- a/packages/loader/package.json +++ b/packages/loader/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-loader", - "version": "0.0.0-experimental-shaderlab.0", + "version": "0.0.0-experimental-shaderlab.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/math/package.json b/packages/math/package.json index 4278d42c80..9cded8595a 100644 --- a/packages/math/package.json +++ b/packages/math/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-math", - "version": "0.0.0-experimental-shaderlab.0", + "version": "0.0.0-experimental-shaderlab.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/physics-lite/package.json b/packages/physics-lite/package.json index e02c791b88..70bcfd86d0 100644 --- a/packages/physics-lite/package.json +++ b/packages/physics-lite/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-physics-lite", - "version": "0.0.0-experimental-shaderlab.0", + "version": "0.0.0-experimental-shaderlab.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/physics-physx/package.json b/packages/physics-physx/package.json index b230e45879..70b8f02447 100644 --- a/packages/physics-physx/package.json +++ b/packages/physics-physx/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-physics-physx", - "version": "0.0.0-experimental-shaderlab.0", + "version": "0.0.0-experimental-shaderlab.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/rhi-webgl/package.json b/packages/rhi-webgl/package.json index deca2a084f..dd76fb6cfb 100644 --- a/packages/rhi-webgl/package.json +++ b/packages/rhi-webgl/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-rhi-webgl", - "version": "0.0.0-experimental-shaderlab.0", + "version": "0.0.0-experimental-shaderlab.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/shader-lab/package.json b/packages/shader-lab/package.json index ca30fc7e8d..fe9872427f 100644 --- a/packages/shader-lab/package.json +++ b/packages/shader-lab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-shader-lab", - "version": "0.0.0-experimental-shaderlab.0", + "version": "0.0.0-experimental-shaderlab.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/xr-webxr/package.json b/packages/xr-webxr/package.json index 31d5ef2721..f3152d021c 100644 --- a/packages/xr-webxr/package.json +++ b/packages/xr-webxr/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-xr-webxr", - "version": "0.0.0-experimental-shaderlab.0", + "version": "0.0.0-experimental-shaderlab.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/xr/package.json b/packages/xr/package.json index dea641cc33..6addc1d7a3 100644 --- a/packages/xr/package.json +++ b/packages/xr/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-xr", - "version": "0.0.0-experimental-shaderlab.0", + "version": "0.0.0-experimental-shaderlab.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From 470dc628995011bc2cea7761f31b44e9fa8d8ca2 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 7 May 2024 15:50:11 +0800 Subject: [PATCH 021/131] fix: recursive chunck load --- packages/loader/src/MaterialLoader.ts | 2 +- packages/loader/src/ShaderChunkLoader.ts | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/loader/src/MaterialLoader.ts b/packages/loader/src/MaterialLoader.ts index 99a01ddbdd..5fc9cb295d 100644 --- a/packages/loader/src/MaterialLoader.ts +++ b/packages/loader/src/MaterialLoader.ts @@ -91,7 +91,7 @@ class MaterialLoader extends Loader { case "Texture": texturePromises.push( // @ts-ignore - resourceManager.getResourceByRef(value).then((texture) => { + engine.resourceManager.getResourceByRef(value).then((texture) => { materialShaderData.setTexture(key, texture); }) ); diff --git a/packages/loader/src/ShaderChunkLoader.ts b/packages/loader/src/ShaderChunkLoader.ts index f85229d904..79cd824ffc 100644 --- a/packages/loader/src/ShaderChunkLoader.ts +++ b/packages/loader/src/ShaderChunkLoader.ts @@ -10,11 +10,24 @@ import { } from "@galacean/engine-core"; @resourceLoader(AssetType.ShaderChunk, ["glsl"], false) -class ShaderLoader extends Loader { +class ShaderChunkLoader extends Loader { load(item: LoadItem, resourceManager: ResourceManager): AssetPromise { - return this.request(item.url, { ...item, type: "text" }).then((code) => { + return this.request(item.url, { ...item, type: "text" }).then(async (code) => { const { includeKey } = item.params; ShaderFactory.registerInclude(includeKey, code); + + const matches = code.matchAll(/^[ \t]*#include +"([^$\\"]+)"/gm); + await Promise.all( + Array.from(matches).map((m) => { + const path = m[1]; + if (path) { + // @ts-ignore + const resource = resourceManager._virtualPathMap[path]; + if (!resource) return; + return resourceManager.load({ type: AssetType.ShaderChunk, url: resource, params: { includeKey: path } }); + } + }) + ); }); } } From 6c7436a7ed1a8676bb3a6be8d4de795ce9bdae02 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 7 May 2024 18:41:32 +0800 Subject: [PATCH 022/131] "v0.0.0-experimental-shaderlab.2" --- packages/core/package.json | 2 +- packages/design/package.json | 2 +- packages/galacean/package.json | 2 +- packages/loader/package.json | 2 +- packages/math/package.json | 2 +- packages/physics-lite/package.json | 2 +- packages/physics-physx/package.json | 2 +- packages/rhi-webgl/package.json | 2 +- packages/shader-lab/package.json | 2 +- packages/xr-webxr/package.json | 2 +- packages/xr/package.json | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index 7e198f3d0c..f1176fd642 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-core", - "version": "0.0.0-experimental-shaderlab.1", + "version": "0.0.0-experimental-shaderlab.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/design/package.json b/packages/design/package.json index 9cedb7ae0a..0b65722b9b 100644 --- a/packages/design/package.json +++ b/packages/design/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-design", - "version": "0.0.0-experimental-shaderlab.1", + "version": "0.0.0-experimental-shaderlab.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/galacean/package.json b/packages/galacean/package.json index ef3dd2eec1..86efd1d2b7 100644 --- a/packages/galacean/package.json +++ b/packages/galacean/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine", - "version": "0.0.0-experimental-shaderlab.1", + "version": "0.0.0-experimental-shaderlab.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/loader/package.json b/packages/loader/package.json index f342362068..bca9b0f70d 100644 --- a/packages/loader/package.json +++ b/packages/loader/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-loader", - "version": "0.0.0-experimental-shaderlab.1", + "version": "0.0.0-experimental-shaderlab.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/math/package.json b/packages/math/package.json index 9cded8595a..779403c9af 100644 --- a/packages/math/package.json +++ b/packages/math/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-math", - "version": "0.0.0-experimental-shaderlab.1", + "version": "0.0.0-experimental-shaderlab.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/physics-lite/package.json b/packages/physics-lite/package.json index 70bcfd86d0..893a824ca2 100644 --- a/packages/physics-lite/package.json +++ b/packages/physics-lite/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-physics-lite", - "version": "0.0.0-experimental-shaderlab.1", + "version": "0.0.0-experimental-shaderlab.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/physics-physx/package.json b/packages/physics-physx/package.json index 70b8f02447..427dec9332 100644 --- a/packages/physics-physx/package.json +++ b/packages/physics-physx/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-physics-physx", - "version": "0.0.0-experimental-shaderlab.1", + "version": "0.0.0-experimental-shaderlab.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/rhi-webgl/package.json b/packages/rhi-webgl/package.json index dd76fb6cfb..13705e80b0 100644 --- a/packages/rhi-webgl/package.json +++ b/packages/rhi-webgl/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-rhi-webgl", - "version": "0.0.0-experimental-shaderlab.1", + "version": "0.0.0-experimental-shaderlab.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/shader-lab/package.json b/packages/shader-lab/package.json index fe9872427f..a47cfae7d9 100644 --- a/packages/shader-lab/package.json +++ b/packages/shader-lab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-shader-lab", - "version": "0.0.0-experimental-shaderlab.1", + "version": "0.0.0-experimental-shaderlab.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/xr-webxr/package.json b/packages/xr-webxr/package.json index f3152d021c..ba814be61c 100644 --- a/packages/xr-webxr/package.json +++ b/packages/xr-webxr/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-xr-webxr", - "version": "0.0.0-experimental-shaderlab.1", + "version": "0.0.0-experimental-shaderlab.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/xr/package.json b/packages/xr/package.json index 6addc1d7a3..388dd791fb 100644 --- a/packages/xr/package.json +++ b/packages/xr/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-xr", - "version": "0.0.0-experimental-shaderlab.1", + "version": "0.0.0-experimental-shaderlab.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From 671cace03db15504f45adad8614105a45a12fa1d Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 11 Jun 2024 11:20:20 +0800 Subject: [PATCH 023/131] fix: array index loss --- packages/shader-lab/src/ast-node/AstNode.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/shader-lab/src/ast-node/AstNode.ts b/packages/shader-lab/src/ast-node/AstNode.ts index 2dd987174f..f889655f4b 100644 --- a/packages/shader-lab/src/ast-node/AstNode.ts +++ b/packages/shader-lab/src/ast-node/AstNode.ts @@ -627,13 +627,19 @@ export class FnVariableAstNode extends AstNode { override _doSerialization(context: RuntimeContext): string { const objName = this.content.variable; const propName = this.content.properties?.[0].content; + + const propList = [...(this.content.properties ?? []), ...(this.content.indexes ?? [])] + .sort((a, b) => AstNodeUtils.astSortAsc(a.position, b.position)) + .map((item) => item.serialize(context)) + .join(""); + if (propName) { if (objName === context.varyingStructInfo.objectName) { const ref = context.varyingStructInfo.reference?.find( (ref) => ref.property.content.variableNode.content.variable === propName ); ref && (ref.referenced = true); - return this.content.properties.map((item) => item.content).join("."); + return propList; } else { const attribStruct = context.attributeStructListInfo.find((struct) => struct.objectName === objName); if (attribStruct) { @@ -641,7 +647,7 @@ export class FnVariableAstNode extends AstNode { (ref) => ref.property.content.variableNode.content.variable === propName ); ref && (ref.referenced = true); - return this.content.properties.map((item) => item.content).join("."); + return propList; } } } @@ -654,10 +660,7 @@ export class FnVariableAstNode extends AstNode { }); } } - const propList = [...(this.content.properties ?? []), ...(this.content.indexes ?? [])] - .sort((a, b) => AstNodeUtils.astSortAsc(a.position, b.position)) - .map((item) => item.serialize(context)) - .join(""); + return objName + propList; } } From 9226d388d701046813775ea0ba38efd691b9630e Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 11 Jun 2024 22:05:25 +0800 Subject: [PATCH 024/131] fix: test-case --- packages/shader-lab/src/ast-node/AstNode.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/shader-lab/src/ast-node/AstNode.ts b/packages/shader-lab/src/ast-node/AstNode.ts index f889655f4b..86285640cc 100644 --- a/packages/shader-lab/src/ast-node/AstNode.ts +++ b/packages/shader-lab/src/ast-node/AstNode.ts @@ -628,10 +628,9 @@ export class FnVariableAstNode extends AstNode { const objName = this.content.variable; const propName = this.content.properties?.[0].content; - const propList = [...(this.content.properties ?? []), ...(this.content.indexes ?? [])] - .sort((a, b) => AstNodeUtils.astSortAsc(a.position, b.position)) - .map((item) => item.serialize(context)) - .join(""); + const propList = [...(this.content.properties ?? []), ...(this.content.indexes ?? [])].sort((a, b) => + AstNodeUtils.astSortAsc(a.position, b.position) + ); if (propName) { if (objName === context.varyingStructInfo.objectName) { @@ -639,7 +638,7 @@ export class FnVariableAstNode extends AstNode { (ref) => ref.property.content.variableNode.content.variable === propName ); ref && (ref.referenced = true); - return propList; + return propList.map((item) => item.content).join("."); } else { const attribStruct = context.attributeStructListInfo.find((struct) => struct.objectName === objName); if (attribStruct) { @@ -647,7 +646,7 @@ export class FnVariableAstNode extends AstNode { (ref) => ref.property.content.variableNode.content.variable === propName ); ref && (ref.referenced = true); - return propList; + return propList.map((item) => item.content).join("."); } } } @@ -661,7 +660,7 @@ export class FnVariableAstNode extends AstNode { } } - return objName + propList; + return objName + propList.map((item) => item.serialize(context)).join(""); } } From ff6a69a3f69ddacc82f409bf55e8fb72cf322ceb Mon Sep 17 00:00:00 2001 From: Sway007 Date: Wed, 12 Jun 2024 10:09:07 +0800 Subject: [PATCH 025/131] fix: test-case --- packages/shader-lab/src/ast-node/AstNode.ts | 15 +++++++++------ tests/src/shader-lab/shaders/demo.shader | 2 ++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/shader-lab/src/ast-node/AstNode.ts b/packages/shader-lab/src/ast-node/AstNode.ts index 86285640cc..e18ee3662b 100644 --- a/packages/shader-lab/src/ast-node/AstNode.ts +++ b/packages/shader-lab/src/ast-node/AstNode.ts @@ -628,9 +628,10 @@ export class FnVariableAstNode extends AstNode { const objName = this.content.variable; const propName = this.content.properties?.[0].content; - const propList = [...(this.content.properties ?? []), ...(this.content.indexes ?? [])].sort((a, b) => - AstNodeUtils.astSortAsc(a.position, b.position) - ); + const propList = [...(this.content.properties ?? []), ...(this.content.indexes ?? [])] + .sort((a, b) => AstNodeUtils.astSortAsc(a.position, b.position)) + .map((item) => item.serialize(context)) + .join(""); if (propName) { if (objName === context.varyingStructInfo.objectName) { @@ -638,7 +639,8 @@ export class FnVariableAstNode extends AstNode { (ref) => ref.property.content.variableNode.content.variable === propName ); ref && (ref.referenced = true); - return propList.map((item) => item.content).join("."); + if (propList.startsWith(".")) return propList.slice(1); + return propList; } else { const attribStruct = context.attributeStructListInfo.find((struct) => struct.objectName === objName); if (attribStruct) { @@ -646,7 +648,8 @@ export class FnVariableAstNode extends AstNode { (ref) => ref.property.content.variableNode.content.variable === propName ); ref && (ref.referenced = true); - return propList.map((item) => item.content).join("."); + if (propList.startsWith(".")) return propList.slice(1); + return propList; } } } @@ -660,7 +663,7 @@ export class FnVariableAstNode extends AstNode { } } - return objName + propList.map((item) => item.serialize(context)).join(""); + return objName + propList; } } diff --git a/tests/src/shader-lab/shaders/demo.shader b/tests/src/shader-lab/shaders/demo.shader index a529185d3e..1148d96683 100644 --- a/tests/src/shader-lab/shaders/demo.shader +++ b/tests/src/shader-lab/shaders/demo.shader @@ -43,6 +43,7 @@ Shader "Water" { struct a2v { vec4 POSITION; vec2 TEXCOORD_0; + mat3 TBN; } struct v2f { @@ -88,6 +89,7 @@ Shader "Water" { vec4 tmp = renderer_MVMat * POSITION; o.v_position = tmp.xyz; gl_Position = renderer_MVPMat * v.POSITION; + vec3 tangentW = v.TBN[0]; return o; } From 94e30f52b65593a853c2afc03cc62e7456e96825 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 16 Jul 2024 14:03:29 +0800 Subject: [PATCH 026/131] fix: conditional compilation --- packages/shader-lab/package.json | 7 +------ packages/shader-lab/src/Utils.ts | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/shader-lab/package.json b/packages/shader-lab/package.json index ceb5d162f1..5490505c02 100644 --- a/packages/shader-lab/package.json +++ b/packages/shader-lab/package.json @@ -12,9 +12,7 @@ "debug": "src/index.ts", "types": "types/index.d.ts", "scripts": { - "b:types": "tsc", - "gen_diagram": "ts-node ./scripts/genDiagram.ts", - "gen_dts": "ts-node ./scripts/genDts.ts" + "b:types": "tsc" }, "umd": { "name": "Galacean.ShaderLab", @@ -26,9 +24,6 @@ "dist/**/*", "types/**/*" ], - "dependencies": { - "chevrotain": "^10.5.0" - }, "devDependencies": { "@galacean/engine-design": "workspace:*", "@galacean/engine": "workspace:*" diff --git a/packages/shader-lab/src/Utils.ts b/packages/shader-lab/src/Utils.ts index a7d65617e8..38614c1e0e 100644 --- a/packages/shader-lab/src/Utils.ts +++ b/packages/shader-lab/src/Utils.ts @@ -4,8 +4,8 @@ import { EKeyword, ETokenType, GalaceanDataType, ShaderRange, ShaderPosition } f import { TreeNode } from "./parser/AST"; // #if _EDITOR import State from "./lalr/State"; -import { Logger } from "@galacean/engine"; // #endif +import { Logger } from "@galacean/engine"; export class ParserUtils { static unwrapNodeByType(node: TreeNode, type: ENonTerminal): T | undefined { From e3c7aa5b90c15e139174a7f320bf6d3be3447eb5 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 16 Jul 2024 15:35:24 +0800 Subject: [PATCH 027/131] fix: expand token --- packages/shader-lab/src/index.ts | 3 +-- packages/shader-lab/src/preprocessor/PpParser.ts | 2 -- packages/shader-lab/src/preprocessor/PpScanner.ts | 2 +- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/shader-lab/src/index.ts b/packages/shader-lab/src/index.ts index bb84aed6cd..7e733a06cc 100644 --- a/packages/shader-lab/src/index.ts +++ b/packages/shader-lab/src/index.ts @@ -1,8 +1,7 @@ export { ShaderLab } from "./ShaderLab"; // #if _EDITOR -import { Preprocessor } from "./preprocessor"; -export { Preprocessor }; +export { Preprocessor } from "./preprocessor"; // #endif //@ts-ignore diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 8d59d2e403..f269c1fa63 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -605,9 +605,7 @@ export default class PpParser { } private static _onToken(token: BaseToken, scanner: PpScanner) { - // #if !_EDITOR this._skipEditorBlock(token, scanner); - // #endif this._expandToken(token, scanner); } diff --git a/packages/shader-lab/src/preprocessor/PpScanner.ts b/packages/shader-lab/src/preprocessor/PpScanner.ts index 33422d7d5f..2eb39a9de1 100644 --- a/packages/shader-lab/src/preprocessor/PpScanner.ts +++ b/packages/shader-lab/src/preprocessor/PpScanner.ts @@ -84,7 +84,7 @@ export default class PpScanner extends BaseScanner { if (this.isEnd()) return EOF; const start = this._currentIndex; - while (LexerUtils.isLetter(this.getCurChar())) { + while (LexerUtils.isLetter(this.getCurChar()) && !this.isEnd()) { this.advance(); } const end = this._currentIndex; From 36b77e081bdddbd6754dc454df4ec3b28cf41ad1 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 16 Jul 2024 15:40:17 +0800 Subject: [PATCH 028/131] fix: expand token --- packages/shader-lab/src/preprocessor/PpScanner.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/shader-lab/src/preprocessor/PpScanner.ts b/packages/shader-lab/src/preprocessor/PpScanner.ts index 2eb39a9de1..7544558c74 100644 --- a/packages/shader-lab/src/preprocessor/PpScanner.ts +++ b/packages/shader-lab/src/preprocessor/PpScanner.ts @@ -189,7 +189,6 @@ export default class PpScanner extends BaseScanner { return { token, nextDirective: directive }; } - // #if !_EDITOR scanPairedBlock(lc = "{", rc = "}") { this.scanToChar(lc); let lvl = 0; @@ -199,7 +198,6 @@ export default class PpScanner extends BaseScanner { this._advance(); } while (lvl > 0); } - // #endif /** * @returns end ShaderPosition From b2e07fc8a6a25ddef75f193f00cf54d71723a72c Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 16 Jul 2024 17:40:18 +0800 Subject: [PATCH 029/131] feat: code opt --- .../shader-lab/src/preprocessor/PpParser.ts | 2 +- .../shader-lab/src/preprocessor/PpScanner.ts | 20 +++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index f269c1fa63..98a1d5c639 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -612,7 +612,7 @@ export default class PpParser { private static _skipEditorBlock(token: BaseToken, scanner: PpScanner) { if (token.lexeme === "EditorProperties" || token.lexeme === "EditorMacros") { const start = scanner.current - token.lexeme.length; - scanner.scanPairedBlock(); + scanner.scanPairedBlock("{", "}"); const end = scanner.current; const startPosition = ShaderLab.createPosition(start); const endPosition = ShaderLab.createPosition(end); diff --git a/packages/shader-lab/src/preprocessor/PpScanner.ts b/packages/shader-lab/src/preprocessor/PpScanner.ts index 7544558c74..892f8b87d3 100644 --- a/packages/shader-lab/src/preprocessor/PpScanner.ts +++ b/packages/shader-lab/src/preprocessor/PpScanner.ts @@ -161,7 +161,8 @@ export default class PpScanner extends BaseScanner { } scanToChar(char: string) { - while (this.getCurChar() !== char && !this.isEnd()) { + const source = this._source; + while (source[this._currentIndex] !== char && !this.isEnd()) { this.advance(); } } @@ -189,14 +190,21 @@ export default class PpScanner extends BaseScanner { return { token, nextDirective: directive }; } - scanPairedBlock(lc = "{", rc = "}") { + scanPairedBlock(lc: string, rc: string): void { this.scanToChar(lc); - let lvl = 0; + let level = 0; + const source = this._source; + do { - if (this.getCurChar() === lc) lvl += 1; - else if (this.getCurChar() === rc) lvl -= 1; + const curChar = source[this._currentIndex]; + + if (curChar === lc) { + level++; + } else if (curChar === rc) { + level--; + } this._advance(); - } while (lvl > 0); + } while (level > 0); } /** From 93df92e2e442a6b79a5f380323571b926b11b4a3 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 16 Jul 2024 17:43:24 +0800 Subject: [PATCH 030/131] fix: ci --- pnpm-lock.yaml | 43 +------------------------------------------ 1 file changed, 1 insertion(+), 42 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bc827d5d7d..d76a98b9c1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -245,10 +245,6 @@ importers: version: link:../design packages/shader-lab: - dependencies: - chevrotain: - specifier: ^10.5.0 - version: 10.5.0 devDependencies: '@galacean/engine': specifier: workspace:* @@ -521,29 +517,6 @@ packages: to-fast-properties: 2.0.0 dev: true - /@chevrotain/cst-dts-gen@10.5.0: - resolution: {integrity: sha512-lhmC/FyqQ2o7pGK4Om+hzuDrm9rhFYIJ/AXoQBeongmn870Xeb0L6oGEiuR8nohFNL5sMaQEJWCxr1oIVIVXrw==} - dependencies: - '@chevrotain/gast': 10.5.0 - '@chevrotain/types': 10.5.0 - lodash: 4.17.21 - dev: false - - /@chevrotain/gast@10.5.0: - resolution: {integrity: sha512-pXdMJ9XeDAbgOWKuD1Fldz4ieCs6+nLNmyVhe2gZVqoO7v8HXuHYs5OV2EzUtbuai37TlOAQHrTDvxMnvMJz3A==} - dependencies: - '@chevrotain/types': 10.5.0 - lodash: 4.17.21 - dev: false - - /@chevrotain/types@10.5.0: - resolution: {integrity: sha512-f1MAia0x/pAVPWH/T73BJVyO2XU5tI4/iE7cnxb7tqdNTNhQI3Uq3XkqcoteTmD4t1aM0LbHCJOhgIDn07kl2A==} - dev: false - - /@chevrotain/utils@10.5.0: - resolution: {integrity: sha512-hBzuU5+JjB2cqNZyszkDHZgOSrUUT8V3dhgRl8Q9Gp6dAj/H5+KILGjbhDpc3Iy9qmqlm/akuOI2ut9VUtzJxQ==} - dev: false - /@choojs/findup@0.2.1: resolution: {integrity: sha512-YstAqNb0MCN8PjdLCDfRsBcGVRN41f3vgLvaI0IrIcBp4AqILRSS0DeWNGkicC+f/zRIPJLc+9RURVSepwvfBw==} hasBin: true @@ -2040,17 +2013,6 @@ packages: engines: {node: '>= 0.8.0'} dev: true - /chevrotain@10.5.0: - resolution: {integrity: sha512-Pkv5rBY3+CsHOYfV5g/Vs5JY9WTHHDEKOlohI2XeygaZhUeqhAlldZ8Hz9cRmxu709bvS08YzxHdTPHhffc13A==} - dependencies: - '@chevrotain/cst-dts-gen': 10.5.0 - '@chevrotain/gast': 10.5.0 - '@chevrotain/types': 10.5.0 - '@chevrotain/utils': 10.5.0 - lodash: 4.17.21 - regexp-to-ast: 0.5.0 - dev: false - /chokidar@3.5.1: resolution: {integrity: sha512-9+s+Od+W0VJJzawDma/gvBNQqkTiqYTWLuZoyAsivsI4AaWTCzHG06/TMjsf1cYe9Cb97UCEhjz7HvnPk2p/tw==} engines: {node: '>= 8.10.0'} @@ -4267,6 +4229,7 @@ packages: /lodash@4.17.21: resolution: {integrity: sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==} + dev: true /log-symbols@4.0.0: resolution: {integrity: sha512-FN8JBzLx6CzeMrB0tg6pqlGU1wCrXW+ZXGH481kfsBqer0hToTIiHdjH4Mq8xJUbvATujKCvaREGWpGUionraA==} @@ -5081,10 +5044,6 @@ packages: resolution: {integrity: sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==} dev: true - /regexp-to-ast@0.5.0: - resolution: {integrity: sha512-tlbJqcMHnPKI9zSrystikWKwHkBqu2a/Sgw01h3zFjvYrMxEDYHzzoMZnUrbIfpTFEsoRnnviOXNCzFiSc54Qw==} - dev: false - /release-zalgo@1.0.0: resolution: {integrity: sha512-gUAyHVHPPC5wdqX/LG4LWtRYtgjxyX78oanFNTMMyFEfOqdC54s3eE82imuWKbOeqYht2CrNf64Qb8vgmmtZGA==} engines: {node: '>=4'} From 8e0cc2856dd5a13acfdbc93e7d509f399b7b2978 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Wed, 17 Jul 2024 11:25:25 +0800 Subject: [PATCH 031/131] feat: cache error shader --- packages/shader-lab/src/ShaderLab.ts | 3 +++ packages/shader-lab/src/parser/ShaderTargetParser.ts | 6 ++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index c687e7fad7..c14ac06e31 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -80,6 +80,9 @@ export class ShaderLab implements IShaderLab { const lexer = new Lexer(ppdContent); const tokens = lexer.tokenize(); const program = ShaderLab._parser.parse(tokens); + if (!program) { + return { vertex: "", fragment: "" }; + } const codeGen = backend === ShaderPlatformTarget.GLES100 ? GLES100Visitor.getVisitor() : GLES300Visitor.getVisitor(); diff --git a/packages/shader-lab/src/parser/ShaderTargetParser.ts b/packages/shader-lab/src/parser/ShaderTargetParser.ts index 3248f92da4..56250740b7 100644 --- a/packages/shader-lab/src/parser/ShaderTargetParser.ts +++ b/packages/shader-lab/src/parser/ShaderTargetParser.ts @@ -52,7 +52,7 @@ export class ShaderTargetParser { this.sematicAnalyzer = new SematicAnalyzer(); } - parse(tokens: Generator) { + parse(tokens: Generator): ASTNode.GLShaderProgram | null { this.sematicAnalyzer.reset(); const start = performance.now(); const { _traceBackStack: traceBackStack, sematicAnalyzer } = this; @@ -104,9 +104,7 @@ export class ShaderTargetParser { continue; } else { Logger.error(token.location, `parse error token ${token.lexeme}`); - // #if _EDITOR - throw `invalid action table by token ${token.lexeme}, ${token.location.start.line}, ${token.location.start.column}`; - // #endif + return null; } } } From c92e674e13314d307ffc4c147545a0968b510c18 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Wed, 17 Jul 2024 16:09:18 +0800 Subject: [PATCH 032/131] fix: preprocessor parse expression --- packages/shader-lab/src/preprocessor/PpParser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 98a1d5c639..6eeba54e60 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -382,7 +382,7 @@ export default class PpParser { if (scanner.getCurChar() === "(") { scanner.advance(); scanner.skipSpace(false); - const ret = this._parseConstant(scanner); + const ret = this._parseConstantExpression(scanner); scanner.scanToChar(")"); scanner.advance(); return ret; From 9eb7398d5d2a924a37529bfa54568549b83a6f3f Mon Sep 17 00:00:00 2001 From: Sway007 Date: Wed, 17 Jul 2024 18:15:14 +0800 Subject: [PATCH 033/131] fix: gles 100 code gen --- packages/shader-lab/src/codeGen/GLES100.ts | 10 +++++++++- packages/shader-lab/src/codeGen/GLES300.ts | 2 +- packages/shader-lab/src/codeGen/GLESVisitor.ts | 7 ++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/shader-lab/src/codeGen/GLES100.ts b/packages/shader-lab/src/codeGen/GLES100.ts index 91a87020fb..e590e63c9f 100644 --- a/packages/shader-lab/src/codeGen/GLES100.ts +++ b/packages/shader-lab/src/codeGen/GLES100.ts @@ -4,7 +4,15 @@ import { VisitorContext } from "./VisitorContext"; import { ICodeSegment } from "./types"; export class GLES100Visitor extends GLESVisitor { - versionText: string = "#version 100 es"; + override _versionText: string = `#version 100`; + override _extensions: string = [ + "GL_EXT_shader_texture_lod", + "GL_OES_standard_derivatives", + "GL_EXT_draw_buffers", + "GL_EXT_frag_depth" + ] + .map((e) => `#extension ${e} : enable\n`) + .join(""); private static _singleton: GLES100Visitor; static getVisitor(): GLES100Visitor { diff --git a/packages/shader-lab/src/codeGen/GLES300.ts b/packages/shader-lab/src/codeGen/GLES300.ts index 650a693eab..31576ab866 100644 --- a/packages/shader-lab/src/codeGen/GLES300.ts +++ b/packages/shader-lab/src/codeGen/GLES300.ts @@ -11,7 +11,7 @@ import { ShaderLab } from "../ShaderLab"; const V3_GL_FragColor = "GS_glFragColor"; export class GLES300Visitor extends GLESVisitor { - versionText: string = "#version 300 es"; + override _versionText: string = "#version 300 es"; private static _singleton: GLES300Visitor; static getVisitor(): GLES300Visitor { diff --git a/packages/shader-lab/src/codeGen/GLESVisitor.ts b/packages/shader-lab/src/codeGen/GLESVisitor.ts index 7db94d83be..9f16c22d20 100644 --- a/packages/shader-lab/src/codeGen/GLESVisitor.ts +++ b/packages/shader-lab/src/codeGen/GLESVisitor.ts @@ -11,7 +11,8 @@ import { VisitorContext } from "./VisitorContext"; const defaultPrecision = `precision mediump float;`; export abstract class GLESVisitor extends CodeGenVisitor { - abstract versionText: string; + protected _versionText: string = ""; + protected _extensions: string = ""; abstract getAttributeDeclare(): ICodeSegment[]; abstract getVaryingDeclare(): ICodeSegment[]; @@ -81,7 +82,7 @@ export abstract class GLESVisitor extends CodeGenVisitor { VisitorContext.context.reset(); - return `${this.versionText}\n${defaultPrecision}\n${globalCode}\n\nvoid main() ${statements}`; + return `${this._versionText}\n${defaultPrecision}\n${globalCode}\n\nvoid main() ${statements}`; } private _fragmentMain(entry: string, data: ShaderData): string { @@ -101,7 +102,7 @@ export abstract class GLESVisitor extends CodeGenVisitor { .join("\n"); VisitorContext.context.reset(); - return `${this.versionText}\n${defaultPrecision}\n${globalCode}\n\nvoid main() ${statements}`; + return `${this._versionText}\n${this._extensions}\n${defaultPrecision}\n${globalCode}\n\nvoid main() ${statements}`; } private _getGlobalText( From 9f5b5bc515dd8546005906d5fc7bf2e036b8d73d Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 18 Jul 2024 10:34:35 +0800 Subject: [PATCH 034/131] feat: support relative path include --- packages/core/src/shader/Shader.ts | 24 ++++++++++++------- packages/core/src/shader/ShaderPass.ts | 21 +++++++++++----- packages/design/src/shader-lab/IShaderLab.ts | 6 ++++- packages/shader-lab/src/ShaderLab.ts | 11 +++++---- .../shader-lab/src/preprocessor/PpParser.ts | 12 +++++++--- .../src/preprocessor/Preprocessor.ts | 9 +++++-- tests/src/shader-lab/ShaderValidate.ts | 5 ++-- 7 files changed, 61 insertions(+), 27 deletions(-) diff --git a/packages/core/src/shader/Shader.ts b/packages/core/src/shader/Shader.ts index 5b5cda10e6..270333c4e8 100644 --- a/packages/core/src/shader/Shader.ts +++ b/packages/core/src/shader/Shader.ts @@ -44,12 +44,13 @@ export class Shader implements IReferable { * ``` * * @param shaderSource - shader code + * @param path - used by the preprocessor in `ShaderLab` to resolve the relative path of `#include` directive * @returns Shader * * @throws * Throw string exception if shaderLab has not been enabled properly. */ - static create(shaderSource: string): Shader; + static create(shaderSource: string, path?: string): Shader; /** * Create a shader. @@ -78,13 +79,17 @@ export class Shader implements IReferable { static create( nameOrShaderSource: string, - vertexSourceOrShaderPassesOrSubShaders?: SubShader[] | ShaderPass[] | string, + vertexSourceOrShaderPassesOrSubShadersOrPath?: SubShader[] | ShaderPass[] | string, fragmentSource?: string ): Shader { let shader: Shader; const shaderMap = Shader._shaderMap; - if (!vertexSourceOrShaderPassesOrSubShaders) { + if ( + !fragmentSource && + (vertexSourceOrShaderPassesOrSubShadersOrPath === undefined || + typeof vertexSourceOrShaderPassesOrSubShadersOrPath === "string") + ) { if (!Shader._shaderLab) { throw "ShaderLab has not been set up yet."; } @@ -109,6 +114,7 @@ export class Shader implements IReferable { passInfo.contents, passInfo.vertexEntry, passInfo.fragmentEntry, + vertexSourceOrShaderPassesOrSubShadersOrPath ?? "", passInfo.tags ); @@ -143,17 +149,17 @@ export class Shader implements IReferable { console.error(`Shader named "${nameOrShaderSource}" already exists.`); return; } - if (typeof vertexSourceOrShaderPassesOrSubShaders === "string") { - const shaderPass = new ShaderPass(vertexSourceOrShaderPassesOrSubShaders, fragmentSource); + if (typeof vertexSourceOrShaderPassesOrSubShadersOrPath === "string") { + const shaderPass = new ShaderPass(vertexSourceOrShaderPassesOrSubShadersOrPath, fragmentSource); shader = new Shader(nameOrShaderSource, [new SubShader("Default", [shaderPass])]); } else { - if (vertexSourceOrShaderPassesOrSubShaders.length > 0) { - if (vertexSourceOrShaderPassesOrSubShaders[0].constructor === ShaderPass) { + if (vertexSourceOrShaderPassesOrSubShadersOrPath.length > 0) { + if (vertexSourceOrShaderPassesOrSubShadersOrPath[0].constructor === ShaderPass) { shader = new Shader(nameOrShaderSource, [ - new SubShader("Default", vertexSourceOrShaderPassesOrSubShaders) + new SubShader("Default", vertexSourceOrShaderPassesOrSubShadersOrPath) ]); } else { - shader = new Shader(nameOrShaderSource, vertexSourceOrShaderPassesOrSubShaders.slice()); + shader = new Shader(nameOrShaderSource, vertexSourceOrShaderPassesOrSubShadersOrPath.slice()); } } else { throw "SubShader or ShaderPass count must large than 0."; diff --git a/packages/core/src/shader/ShaderPass.ts b/packages/core/src/shader/ShaderPass.ts index a33e8e304e..5ce8be5a10 100644 --- a/packages/core/src/shader/ShaderPass.ts +++ b/packages/core/src/shader/ShaderPass.ts @@ -18,6 +18,7 @@ import { RenderState } from "./state/RenderState"; */ export class ShaderPass extends ShaderPart { private static _shaderPassCounter: number = 0; + private static _shaderRootPath = "shaders://root/"; /** @internal */ _shaderPassId: number = 0; @@ -39,6 +40,7 @@ export class ShaderPass extends ShaderPart { private readonly _shaderLabSource: string; private readonly _vertexEntry: string; private readonly _fragmentEntry: string; + private readonly _path: string; private _platformMacros: string[] = []; @@ -50,6 +52,7 @@ export class ShaderPass extends ShaderPart { shaderLabSource: string, vertexEntry: string, fragmentEntry: string, + path: string, tags?: Record ); @@ -80,6 +83,7 @@ export class ShaderPass extends ShaderPart { vertexSourceOrFragmentSourceOrCode?: string | Record, fragmentSourceOrTagsOrVertexEntry?: string | Record, fragmentEntryOrTags?: string | Record, + tagsOrPath?: Record | string, tags?: Record ) { super(); @@ -88,6 +92,7 @@ export class ShaderPass extends ShaderPart { if (typeof fragmentEntryOrTags === "string") { this._name = nameOrVertexSource; + this._path = tagsOrPath as string; this._shaderLabSource = vertexSourceOrFragmentSourceOrCode as string; this._vertexEntry = fragmentSourceOrTagsOrVertexEntry as string; this._fragmentEntry = fragmentEntryOrTags; @@ -156,21 +161,23 @@ export class ShaderPass extends ShaderPart { vertexEntry: string, fragmentEntry: string ) { + const { _path, _platformMacros } = this; + const isWebGL2 = engine._hardwareRenderer.isWebGL2; const macros = new Array(); ShaderMacro._getMacrosElements(macroCollection, macros); - this._platformMacros.length = 0; + _platformMacros.length = 0; if (engine._hardwareRenderer.canIUse(GLCapabilityType.shaderTextureLod)) { - this._platformMacros.push("HAS_TEX_LOD"); + _platformMacros.push("HAS_TEX_LOD"); } if (engine._hardwareRenderer.canIUse(GLCapabilityType.standardDerivatives)) { - this._platformMacros.push("HAS_DERIVATIVES"); + _platformMacros.push("HAS_DERIVATIVES"); } if (isWebGL2) { - this._platformMacros.push("GRAPHICS_API_WEBGL2"); + _platformMacros.push("GRAPHICS_API_WEBGL2"); } else { - this._platformMacros.push("GRAPHICS_API_WEBGL1"); + _platformMacros.push("GRAPHICS_API_WEBGL1"); } const start = performance.now(); @@ -180,7 +187,9 @@ export class ShaderPass extends ShaderPart { fragmentEntry, macros, isWebGL2 ? ShaderPlatformTarget.GLES300 : ShaderPlatformTarget.GLES100, - this._platformMacros + _platformMacros, + ShaderPass._shaderRootPath, + new URL(_path, ShaderPass._shaderRootPath).href ); Logger.info(`[ShaderLab compilation] cost time: ${performance.now() - start}ms`); diff --git a/packages/design/src/shader-lab/IShaderLab.ts b/packages/design/src/shader-lab/IShaderLab.ts index 0ed9c83dd4..22f9012a9d 100644 --- a/packages/design/src/shader-lab/IShaderLab.ts +++ b/packages/design/src/shader-lab/IShaderLab.ts @@ -14,6 +14,8 @@ export interface IShaderLab { /** * @internal * Parse shader pass source code. + * @param pathOrigin follow the specifications of [URL.origin](https://developer.mozilla.org/en-US/docs/Web/API/URL/origin), like: `shaders://root/` + * @param basePathForIncludeKey the base path to resolve the relative path of `#include` directive. Must be prefixed by `pathOrigin` */ _parseShaderPass( shaderPassSource: string, @@ -21,6 +23,8 @@ export interface IShaderLab { fragmentEntry: string, macros: any[], backend: number, - platformMacros: string[] + platformMacros: string[], + pathOrigin: string, + basePathForIncludeKey: string ): IShaderProgramSource; } diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index c14ac06e31..4be32fce0c 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -53,11 +53,12 @@ export class ShaderLab implements IShaderLab { fragmentEntry: string, macros: ShaderMacro[], backend: ShaderPlatformTarget, - platformMacros: string[] + platformMacros: string[], + pathOrigin: string, + basePathForIncludeKey: string ) { ShaderLabObjectPool.clearAllShaderLabObjectPool(); - - Preprocessor.reset(ShaderLib); + Preprocessor.reset(ShaderLib, pathOrigin, basePathForIncludeKey); for (const macro of macros) { Preprocessor.addPredefinedMacro(macro.name, macro.value); } @@ -119,7 +120,9 @@ export class ShaderLab implements IShaderLab { pass.fragmentEntry, macros, backend, - [] + [], + "shaders://root/", + new URL(pass.name, "shaders://root/").href ) as any; passInfo.name = pass.name; passResult.push(passInfo); diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 6eeba54e60..0e0145ce04 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -28,14 +28,18 @@ export default class PpParser { private static _branchMacros: Set = new Set(); private static _includeMap: Record; + private static _basePathForIncludeKey: string; + private static _pathOrigin: string; - static reset(includeMap: Record) { + static reset(includeMap: Record, pathOrigin: string, basePathForIncludeKey: string) { this._definedMacros.clear(); this._expandSegmentsStack.length = 0; this._expandSegmentsStack.push([]); this._branchMacros.clear(); this.addPredefinedMacro("GL_ES"); this._includeMap = includeMap; + this._basePathForIncludeKey = basePathForIncludeKey; + this._pathOrigin = pathOrigin; } static addPredefinedMacro(macro: string, value?: string) { @@ -94,12 +98,14 @@ export default class PpParser { scanner.skipSpace(true); const id = scanner.scanQuotedString(); + const includedPath = new URL(id.lexeme, this._basePathForIncludeKey).href.substring(this._pathOrigin.length); + scanner.scanToChar("\n"); const end = scanner.getShaderPosition(); - const chunk = this._includeMap[id.lexeme]; + const chunk = this._includeMap[includedPath]; if (!chunk) { - ParserUtils.throw(id.location, `Shader slice "${id.lexeme}" not founded.`); + ParserUtils.throw(id.location, `Shader slice "${includedPath}" not founded.`); } const range = ShaderLab.createRange(start, end); diff --git a/packages/shader-lab/src/preprocessor/Preprocessor.ts b/packages/shader-lab/src/preprocessor/Preprocessor.ts index a95cd6efdb..c14a3e4e09 100644 --- a/packages/shader-lab/src/preprocessor/Preprocessor.ts +++ b/packages/shader-lab/src/preprocessor/Preprocessor.ts @@ -5,8 +5,13 @@ import PpScanner from "./PpScanner"; export class Preprocessor { static baseScanner: PpScanner; - static reset(includeMap: Record): void { - PpParser.reset(includeMap); + /** + * Reset the parser of `Preprocessor` + * @param pathOrigin follow the specifications of [URL.origin](https://developer.mozilla.org/en-US/docs/Web/API/URL/origin), like: `shaders://root/` + * @param basePathForIncludeKey the base path to resolve the relative path of `#include` directive. Must be prefixed by `pathOrigin` + */ + static reset(includeMap: Record, pathOrigin: string, basePathForIncludeKey: string): void { + PpParser.reset(includeMap, pathOrigin, basePathForIncludeKey); } /** diff --git a/tests/src/shader-lab/ShaderValidate.ts b/tests/src/shader-lab/ShaderValidate.ts index e6be8e3553..016ca86112 100644 --- a/tests/src/shader-lab/ShaderValidate.ts +++ b/tests/src/shader-lab/ShaderValidate.ts @@ -80,14 +80,15 @@ export function glslValidate(shaderSource, _shaderLab?: ShaderLab, includeMap = shader.subShaders.forEach((subShader) => { subShader.passes.forEach((pass) => { if (pass.isUsePass) return; - // @ts-ignore const compiledPass = shaderLab._parseShaderPass( pass.contents, pass.vertexEntry, pass.fragmentEntry, [], ShaderPlatformTarget.GLES300, - [] + [], + "shaders://root/", + "shaders://root/" ); validateShaderPass(pass, compiledPass.vertex, compiledPass.fragment); }); From 964b84611b9037a034278953353bf4036526526b Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 18 Jul 2024 10:42:22 +0800 Subject: [PATCH 035/131] feat: test case --- tests/src/shader-lab/shaders/unlit.shader | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/shader-lab/shaders/unlit.shader b/tests/src/shader-lab/shaders/unlit.shader index 7065358f19..061edabd66 100644 --- a/tests/src/shader-lab/shaders/unlit.shader +++ b/tests/src/shader-lab/shaders/unlit.shader @@ -54,7 +54,7 @@ Shader "Water" { ivec3 renderer_BlendShapeTextureInfo; vec2 renderer_BlendShapeWeights[RENDERER_BLENDSHAPE_COUNT]; - #include "test_common" + #include "./test_common" #include "brdf" BlendState = blendState; From f6fed8b5b4014c5e523c0a10b2230387af40fded Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 18 Jul 2024 11:22:28 +0800 Subject: [PATCH 036/131] feat: compatible with builtin shader chunk import --- packages/shader-lab/src/preprocessor/PpParser.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 0e0145ce04..02ee847c41 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -98,11 +98,17 @@ export default class PpParser { scanner.skipSpace(true); const id = scanner.scanQuotedString(); - const includedPath = new URL(id.lexeme, this._basePathForIncludeKey).href.substring(this._pathOrigin.length); + let includedPath: string; + // builtin path + if (id.lexeme[0] !== "." && id.lexeme[0] !== "/") { + includedPath = id.lexeme; + } else { + // relative path + includedPath = new URL(id.lexeme, this._basePathForIncludeKey).href.substring(this._pathOrigin.length); + } scanner.scanToChar("\n"); const end = scanner.getShaderPosition(); - const chunk = this._includeMap[includedPath]; if (!chunk) { ParserUtils.throw(id.location, `Shader slice "${includedPath}" not founded.`); From 519ab4051e2c1c2581b27e481c5b881363123ab1 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 18 Jul 2024 14:14:35 +0800 Subject: [PATCH 037/131] feat: code opt --- packages/core/src/shader/ShaderPass.ts | 20 +++++++++---------- packages/design/src/shader-lab/IShaderLab.ts | 4 +--- packages/shader-lab/src/ShaderLab.ts | 9 ++++----- .../shader-lab/src/preprocessor/PpParser.ts | 8 ++++---- .../src/preprocessor/Preprocessor.ts | 4 ++-- tests/src/shader-lab/ShaderValidate.ts | 6 +++--- 6 files changed, 24 insertions(+), 27 deletions(-) diff --git a/packages/core/src/shader/ShaderPass.ts b/packages/core/src/shader/ShaderPass.ts index 5ce8be5a10..9c0e13f66e 100644 --- a/packages/core/src/shader/ShaderPass.ts +++ b/packages/core/src/shader/ShaderPass.ts @@ -18,7 +18,8 @@ import { RenderState } from "./state/RenderState"; */ export class ShaderPass extends ShaderPart { private static _shaderPassCounter: number = 0; - private static _shaderRootPath = "shaders://root/"; + /** @internal */ + static _shaderRootPath = "shaders://root/"; /** @internal */ _shaderPassId: number = 0; @@ -161,23 +162,23 @@ export class ShaderPass extends ShaderPart { vertexEntry: string, fragmentEntry: string ) { - const { _path, _platformMacros } = this; + const { _path: path, _platformMacros: platformMacros } = this; const isWebGL2 = engine._hardwareRenderer.isWebGL2; const macros = new Array(); ShaderMacro._getMacrosElements(macroCollection, macros); - _platformMacros.length = 0; + platformMacros.length = 0; if (engine._hardwareRenderer.canIUse(GLCapabilityType.shaderTextureLod)) { - _platformMacros.push("HAS_TEX_LOD"); + platformMacros.push("HAS_TEX_LOD"); } if (engine._hardwareRenderer.canIUse(GLCapabilityType.standardDerivatives)) { - _platformMacros.push("HAS_DERIVATIVES"); + platformMacros.push("HAS_DERIVATIVES"); } if (isWebGL2) { - _platformMacros.push("GRAPHICS_API_WEBGL2"); + platformMacros.push("GRAPHICS_API_WEBGL2"); } else { - _platformMacros.push("GRAPHICS_API_WEBGL1"); + platformMacros.push("GRAPHICS_API_WEBGL1"); } const start = performance.now(); @@ -187,9 +188,8 @@ export class ShaderPass extends ShaderPart { fragmentEntry, macros, isWebGL2 ? ShaderPlatformTarget.GLES300 : ShaderPlatformTarget.GLES100, - _platformMacros, - ShaderPass._shaderRootPath, - new URL(_path, ShaderPass._shaderRootPath).href + platformMacros, + new URL(path, ShaderPass._shaderRootPath).href ); Logger.info(`[ShaderLab compilation] cost time: ${performance.now() - start}ms`); diff --git a/packages/design/src/shader-lab/IShaderLab.ts b/packages/design/src/shader-lab/IShaderLab.ts index 22f9012a9d..fec2d72571 100644 --- a/packages/design/src/shader-lab/IShaderLab.ts +++ b/packages/design/src/shader-lab/IShaderLab.ts @@ -14,8 +14,7 @@ export interface IShaderLab { /** * @internal * Parse shader pass source code. - * @param pathOrigin follow the specifications of [URL.origin](https://developer.mozilla.org/en-US/docs/Web/API/URL/origin), like: `shaders://root/` - * @param basePathForIncludeKey the base path to resolve the relative path of `#include` directive. Must be prefixed by `pathOrigin` + * @param basePathForIncludeKey the base path to resolve the relative path of `#include` directive. Must follow the specifications of [URL.origin](https://developer.mozilla.org/en-US/docs/Web/API/URL/origin), like: `shaders://root/` */ _parseShaderPass( shaderPassSource: string, @@ -24,7 +23,6 @@ export interface IShaderLab { macros: any[], backend: number, platformMacros: string[], - pathOrigin: string, basePathForIncludeKey: string ): IShaderProgramSource; } diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index 4be32fce0c..335d64c47e 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -5,7 +5,7 @@ import { GLES100Visitor, GLES300Visitor } from "./codeGen"; import { IShaderContent, IShaderLab } from "@galacean/engine-design/src/shader-lab"; import { ShaderContentParser } from "./contentParser"; // @ts-ignore -import { Logger, ShaderLib, ShaderMacro, ShaderPlatformTarget } from "@galacean/engine"; +import { Logger, ShaderLib, ShaderMacro, ShaderPass, ShaderPlatformTarget } from "@galacean/engine"; import { ShaderPosition, ShaderRange } from "./common"; import { ShaderLabObjectPool } from "./ShaderLabObjectPool"; @@ -54,11 +54,10 @@ export class ShaderLab implements IShaderLab { macros: ShaderMacro[], backend: ShaderPlatformTarget, platformMacros: string[], - pathOrigin: string, basePathForIncludeKey: string ) { ShaderLabObjectPool.clearAllShaderLabObjectPool(); - Preprocessor.reset(ShaderLib, pathOrigin, basePathForIncludeKey); + Preprocessor.reset(ShaderLib, basePathForIncludeKey); for (const macro of macros) { Preprocessor.addPredefinedMacro(macro.name, macro.value); } @@ -121,8 +120,8 @@ export class ShaderLab implements IShaderLab { macros, backend, [], - "shaders://root/", - new URL(pass.name, "shaders://root/").href + // @ts-ignore + new URL(pass.name, ShaderPass._shaderRootPath).href ) as any; passInfo.name = pass.name; passResult.push(passInfo); diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 02ee847c41..8b8b627b16 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -10,6 +10,7 @@ import { EPpKeyword, EPpToken, PpConstant } from "./constants"; import PpScanner from "./PpScanner"; import { PpUtils } from "./Utils"; import { ShaderLab } from "../ShaderLab"; +import { ShaderPass } from "@galacean/engine"; export interface ExpandSegment { // #if _EDITOR @@ -29,9 +30,8 @@ export default class PpParser { private static _includeMap: Record; private static _basePathForIncludeKey: string; - private static _pathOrigin: string; - static reset(includeMap: Record, pathOrigin: string, basePathForIncludeKey: string) { + static reset(includeMap: Record, basePathForIncludeKey: string) { this._definedMacros.clear(); this._expandSegmentsStack.length = 0; this._expandSegmentsStack.push([]); @@ -39,7 +39,6 @@ export default class PpParser { this.addPredefinedMacro("GL_ES"); this._includeMap = includeMap; this._basePathForIncludeKey = basePathForIncludeKey; - this._pathOrigin = pathOrigin; } static addPredefinedMacro(macro: string, value?: string) { @@ -104,7 +103,8 @@ export default class PpParser { includedPath = id.lexeme; } else { // relative path - includedPath = new URL(id.lexeme, this._basePathForIncludeKey).href.substring(this._pathOrigin.length); + // @ts-ignore + includedPath = new URL(id.lexeme, this._basePathForIncludeKey).href.substring(ShaderPass._shaderRootPath.length); } scanner.scanToChar("\n"); diff --git a/packages/shader-lab/src/preprocessor/Preprocessor.ts b/packages/shader-lab/src/preprocessor/Preprocessor.ts index c14a3e4e09..1f37c0caec 100644 --- a/packages/shader-lab/src/preprocessor/Preprocessor.ts +++ b/packages/shader-lab/src/preprocessor/Preprocessor.ts @@ -10,8 +10,8 @@ export class Preprocessor { * @param pathOrigin follow the specifications of [URL.origin](https://developer.mozilla.org/en-US/docs/Web/API/URL/origin), like: `shaders://root/` * @param basePathForIncludeKey the base path to resolve the relative path of `#include` directive. Must be prefixed by `pathOrigin` */ - static reset(includeMap: Record, pathOrigin: string, basePathForIncludeKey: string): void { - PpParser.reset(includeMap, pathOrigin, basePathForIncludeKey); + static reset(includeMap: Record, basePathForIncludeKey: string): void { + PpParser.reset(includeMap, basePathForIncludeKey); } /** diff --git a/tests/src/shader-lab/ShaderValidate.ts b/tests/src/shader-lab/ShaderValidate.ts index 016ca86112..adf297ed42 100644 --- a/tests/src/shader-lab/ShaderValidate.ts +++ b/tests/src/shader-lab/ShaderValidate.ts @@ -1,6 +1,6 @@ import { expect } from "chai"; import { ShaderLab } from "@galacean/engine-shader-lab"; -import { Shader, ShaderFactory, ShaderPlatformTarget } from "@galacean/engine-core"; +import { Shader, ShaderFactory, ShaderPass, ShaderPlatformTarget } from "@galacean/engine-core"; import { IShaderContent } from "@galacean/engine-design/src/shader-lab"; function addLineNum(str: string) { @@ -87,8 +87,8 @@ export function glslValidate(shaderSource, _shaderLab?: ShaderLab, includeMap = [], ShaderPlatformTarget.GLES300, [], - "shaders://root/", - "shaders://root/" + // @ts-ignore + ShaderPass._shaderRootPath ); validateShaderPass(pass, compiledPass.vertex, compiledPass.fragment); }); From ee6a8139fca739937df18570573f8e5873a47949 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 18 Jul 2024 14:45:45 +0800 Subject: [PATCH 038/131] feat: code opt --- packages/core/src/shader/Shader.ts | 36 +++++++++++++++----------- packages/core/src/shader/ShaderPass.ts | 10 ++++--- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/packages/core/src/shader/Shader.ts b/packages/core/src/shader/Shader.ts index 270333c4e8..799dc6cd8c 100644 --- a/packages/core/src/shader/Shader.ts +++ b/packages/core/src/shader/Shader.ts @@ -44,13 +44,12 @@ export class Shader implements IReferable { * ``` * * @param shaderSource - shader code - * @param path - used by the preprocessor in `ShaderLab` to resolve the relative path of `#include` directive * @returns Shader * * @throws * Throw string exception if shaderLab has not been enabled properly. */ - static create(shaderSource: string, path?: string): Shader; + static create(shaderSource: string): Shader; /** * Create a shader. @@ -79,17 +78,13 @@ export class Shader implements IReferable { static create( nameOrShaderSource: string, - vertexSourceOrShaderPassesOrSubShadersOrPath?: SubShader[] | ShaderPass[] | string, + vertexSourceOrShaderPassesOrSubShaders?: SubShader[] | ShaderPass[] | string, fragmentSource?: string ): Shader { let shader: Shader; const shaderMap = Shader._shaderMap; - if ( - !fragmentSource && - (vertexSourceOrShaderPassesOrSubShadersOrPath === undefined || - typeof vertexSourceOrShaderPassesOrSubShadersOrPath === "string") - ) { + if (!vertexSourceOrShaderPassesOrSubShaders) { if (!Shader._shaderLab) { throw "ShaderLab has not been set up yet."; } @@ -114,7 +109,6 @@ export class Shader implements IReferable { passInfo.contents, passInfo.vertexEntry, passInfo.fragmentEntry, - vertexSourceOrShaderPassesOrSubShadersOrPath ?? "", passInfo.tags ); @@ -149,17 +143,17 @@ export class Shader implements IReferable { console.error(`Shader named "${nameOrShaderSource}" already exists.`); return; } - if (typeof vertexSourceOrShaderPassesOrSubShadersOrPath === "string") { - const shaderPass = new ShaderPass(vertexSourceOrShaderPassesOrSubShadersOrPath, fragmentSource); + if (typeof vertexSourceOrShaderPassesOrSubShaders === "string") { + const shaderPass = new ShaderPass(vertexSourceOrShaderPassesOrSubShaders, fragmentSource); shader = new Shader(nameOrShaderSource, [new SubShader("Default", [shaderPass])]); } else { - if (vertexSourceOrShaderPassesOrSubShadersOrPath.length > 0) { - if (vertexSourceOrShaderPassesOrSubShadersOrPath[0].constructor === ShaderPass) { + if (vertexSourceOrShaderPassesOrSubShaders.length > 0) { + if (vertexSourceOrShaderPassesOrSubShaders[0].constructor === ShaderPass) { shader = new Shader(nameOrShaderSource, [ - new SubShader("Default", vertexSourceOrShaderPassesOrSubShadersOrPath) + new SubShader("Default", vertexSourceOrShaderPassesOrSubShaders) ]); } else { - shader = new Shader(nameOrShaderSource, vertexSourceOrShaderPassesOrSubShadersOrPath.slice()); + shader = new Shader(nameOrShaderSource, vertexSourceOrShaderPassesOrSubShaders.slice()); } } else { throw "SubShader or ShaderPass count must large than 0."; @@ -287,6 +281,18 @@ export class Shader implements IReferable { return this._destroyed; } + /** + * @internal + * path should follow the specifications of [URL.origin](https://developer.mozilla.org/en-US/docs/Web/API/URL/origin), like: `shaders://root/` + */ + _registerPath(path: string) { + for (const subShader of this._subShaders) { + for (const shaderPass of subShader.passes) { + shaderPass.path = path; + } + } + } + private constructor( public readonly name: string, subShaders: SubShader[] diff --git a/packages/core/src/shader/ShaderPass.ts b/packages/core/src/shader/ShaderPass.ts index 9c0e13f66e..c8ecf4040b 100644 --- a/packages/core/src/shader/ShaderPass.ts +++ b/packages/core/src/shader/ShaderPass.ts @@ -41,10 +41,15 @@ export class ShaderPass extends ShaderPart { private readonly _shaderLabSource: string; private readonly _vertexEntry: string; private readonly _fragmentEntry: string; - private readonly _path: string; + private _path = ""; private _platformMacros: string[] = []; + /** @internal */ + set path(val: string) { + this._path = val; + } + /** * @internal */ @@ -53,7 +58,6 @@ export class ShaderPass extends ShaderPart { shaderLabSource: string, vertexEntry: string, fragmentEntry: string, - path: string, tags?: Record ); @@ -84,7 +88,6 @@ export class ShaderPass extends ShaderPart { vertexSourceOrFragmentSourceOrCode?: string | Record, fragmentSourceOrTagsOrVertexEntry?: string | Record, fragmentEntryOrTags?: string | Record, - tagsOrPath?: Record | string, tags?: Record ) { super(); @@ -93,7 +96,6 @@ export class ShaderPass extends ShaderPart { if (typeof fragmentEntryOrTags === "string") { this._name = nameOrVertexSource; - this._path = tagsOrPath as string; this._shaderLabSource = vertexSourceOrFragmentSourceOrCode as string; this._vertexEntry = fragmentSourceOrTagsOrVertexEntry as string; this._fragmentEntry = fragmentEntryOrTags; From d7e0ac87e177b0409e2a5a1169cea1e89d928bc8 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 18 Jul 2024 15:11:03 +0800 Subject: [PATCH 039/131] feat: code opt --- packages/shader-lab/src/preprocessor/PpParser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 8b8b627b16..025d311cc0 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -99,7 +99,7 @@ export default class PpParser { const id = scanner.scanQuotedString(); let includedPath: string; // builtin path - if (id.lexeme[0] !== "." && id.lexeme[0] !== "/") { + if (id.lexeme[0] !== ".") { includedPath = id.lexeme; } else { // relative path From 8f75c28312314eff486a71449a3df13f5b59f078 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 18 Jul 2024 15:28:37 +0800 Subject: [PATCH 040/131] feat: code opt --- packages/core/src/shader/Shader.ts | 2 +- packages/core/src/shader/ShaderPass.ts | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/core/src/shader/Shader.ts b/packages/core/src/shader/Shader.ts index 799dc6cd8c..8b216200ef 100644 --- a/packages/core/src/shader/Shader.ts +++ b/packages/core/src/shader/Shader.ts @@ -288,7 +288,7 @@ export class Shader implements IReferable { _registerPath(path: string) { for (const subShader of this._subShaders) { for (const shaderPass of subShader.passes) { - shaderPass.path = path; + shaderPass._path = path; } } } diff --git a/packages/core/src/shader/ShaderPass.ts b/packages/core/src/shader/ShaderPass.ts index c8ecf4040b..934b8030a0 100644 --- a/packages/core/src/shader/ShaderPass.ts +++ b/packages/core/src/shader/ShaderPass.ts @@ -41,15 +41,11 @@ export class ShaderPass extends ShaderPart { private readonly _shaderLabSource: string; private readonly _vertexEntry: string; private readonly _fragmentEntry: string; - private _path = ""; + /** @internal */ + _path = ""; private _platformMacros: string[] = []; - /** @internal */ - set path(val: string) { - this._path = val; - } - /** * @internal */ From 00447fb46a3b228bf29a9ccfd0d1acf0839a40aa Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 18 Jul 2024 15:30:28 +0800 Subject: [PATCH 041/131] feat: code opt --- packages/shader-lab/src/preprocessor/Preprocessor.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/shader-lab/src/preprocessor/Preprocessor.ts b/packages/shader-lab/src/preprocessor/Preprocessor.ts index 1f37c0caec..35021f393e 100644 --- a/packages/shader-lab/src/preprocessor/Preprocessor.ts +++ b/packages/shader-lab/src/preprocessor/Preprocessor.ts @@ -7,8 +7,7 @@ export class Preprocessor { /** * Reset the parser of `Preprocessor` - * @param pathOrigin follow the specifications of [URL.origin](https://developer.mozilla.org/en-US/docs/Web/API/URL/origin), like: `shaders://root/` - * @param basePathForIncludeKey the base path to resolve the relative path of `#include` directive. Must be prefixed by `pathOrigin` + * @param basePathForIncludeKey - the base path to resolve the relative path of `#include` directive */ static reset(includeMap: Record, basePathForIncludeKey: string): void { PpParser.reset(includeMap, basePathForIncludeKey); From d5a3c0cb9169e6b122dd8370c0d799306a077d32 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Fri, 19 Jul 2024 18:05:33 +0800 Subject: [PATCH 042/131] feat: add log --- packages/core/src/index.ts | 2 + packages/shader-lab/src/Error.ts | 17 +++++-- packages/shader-lab/src/ShaderLab.ts | 19 +++++++- .../shader-lab/src/common/ShaderPosition.ts | 4 ++ packages/shader-lab/src/common/ShaderRange.ts | 4 ++ .../shader-lab/src/preprocessor/PpParser.ts | 45 ++++++++++++------- .../src/preprocessor/Preprocessor.ts | 8 +++- 7 files changed, 79 insertions(+), 20 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 01952de4e2..df404a7cf9 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -66,3 +66,5 @@ export * from "./Utils"; export { ShaderMacroCollection } from "./shader/ShaderMacroCollection"; export * from "./postProcess"; +/** @internal */ +export { ShaderProgram } from "./shader/ShaderProgram"; diff --git a/packages/shader-lab/src/Error.ts b/packages/shader-lab/src/Error.ts index 6649f64157..06163f21a7 100644 --- a/packages/shader-lab/src/Error.ts +++ b/packages/shader-lab/src/Error.ts @@ -1,13 +1,24 @@ // #if _EDITOR -import { ShaderRange } from "./common"; +import { ShaderPosition, ShaderRange } from "./common"; export abstract class GSError extends Error { - readonly loc: ShaderRange; + readonly loc: ShaderRange | ShaderPosition; - constructor(message: string, loc: ShaderRange, cause?: Error) { + constructor(message: string, loc: ShaderRange | ShaderPosition, cause?: Error) { super(message, { cause }); this.loc = loc; } + + override toString(): string { + return `${this.loc.toString()}: ${this.message}`; + } +} + +export class PreprocessorError extends GSError { + constructor(message: string, loc: ShaderRange | ShaderPosition, cause?: Error) { + super(message, loc, cause); + this.name = "PreprocessorError"; + } } export class SemanticError extends GSError { diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index 335d64c47e..a19981711a 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -5,9 +5,10 @@ import { GLES100Visitor, GLES300Visitor } from "./codeGen"; import { IShaderContent, IShaderLab } from "@galacean/engine-design/src/shader-lab"; import { ShaderContentParser } from "./contentParser"; // @ts-ignore -import { Logger, ShaderLib, ShaderMacro, ShaderPass, ShaderPlatformTarget } from "@galacean/engine"; +import { Logger, ShaderLib, ShaderMacro, ShaderPass, ShaderPlatformTarget, ShaderProgram } from "@galacean/engine"; import { ShaderPosition, ShaderRange } from "./common"; import { ShaderLabObjectPool } from "./ShaderLabObjectPool"; +import { GSError, PreprocessorError } from "./Error"; export class ShaderLab implements IShaderLab { /** @@ -74,6 +75,10 @@ export class ShaderLab implements IShaderLab { const preprocessorStart = performance.now(); const ppdContent = Preprocessor.process(source); + if (Preprocessor.errors.length > 0) { + this._reportPreprocessError(Preprocessor.errors, source); + return { vertex: "", fragment: "" }; + } Logger.info(`[pass compilation - preprocessor] cost time ${performance.now() - preprocessorStart}ms`); @@ -130,4 +135,16 @@ export class ShaderLab implements IShaderLab { return passResult; } // #endif + + private _reportPreprocessError(errors: PreprocessorError[], source: string) { + const errorsMsg = errors.map((item) => item.toString()).join("\n"); + console.error( + "Error occur when expansion ShaderLab code:\n\n" + + errorsMsg + + "ShaderLab source:\n\n" + + ShaderProgram._addLineNum(source) + ); + } + + // private _reportParserError() } diff --git a/packages/shader-lab/src/common/ShaderPosition.ts b/packages/shader-lab/src/common/ShaderPosition.ts index 3b5ee0fc24..5b173ba834 100644 --- a/packages/shader-lab/src/common/ShaderPosition.ts +++ b/packages/shader-lab/src/common/ShaderPosition.ts @@ -26,4 +26,8 @@ export class ShaderPosition implements IPoolElement { this.line = 0; this.column = 0; } + + toString() { + return ``; + } } diff --git a/packages/shader-lab/src/common/ShaderRange.ts b/packages/shader-lab/src/common/ShaderRange.ts index e79d4143e0..41a6e436bc 100644 --- a/packages/shader-lab/src/common/ShaderRange.ts +++ b/packages/shader-lab/src/common/ShaderRange.ts @@ -14,4 +14,8 @@ export class ShaderRange implements IPoolElement { this.start.dispose(); this.end.dispose(); } + + toString() { + return `[Start: ${this.start.toString()}; End: ${this.end.toString()}]`; + } } diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 025d311cc0..8af73fb6ca 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -1,4 +1,4 @@ -import { ShaderRange } from "../common"; +import { ShaderPosition, ShaderRange } from "../common"; import LexerUtils from "../lexer/Utils"; import { MacroDefine } from "./MacroDefine"; // #if _EDITOR @@ -10,7 +10,8 @@ import { EPpKeyword, EPpToken, PpConstant } from "./constants"; import PpScanner from "./PpScanner"; import { PpUtils } from "./Utils"; import { ShaderLab } from "../ShaderLab"; -import { ShaderPass } from "@galacean/engine"; +import { Logger, ShaderPass } from "@galacean/engine"; +import { PreprocessorError } from "../Error"; export interface ExpandSegment { // #if _EDITOR @@ -31,6 +32,9 @@ export default class PpParser { private static _includeMap: Record; private static _basePathForIncludeKey: string; + /** @internal */ + static _errors: PreprocessorError[] = []; + static reset(includeMap: Record, basePathForIncludeKey: string) { this._definedMacros.clear(); this._expandSegmentsStack.length = 0; @@ -39,6 +43,7 @@ export default class PpParser { this.addPredefinedMacro("GL_ES"); this._includeMap = includeMap; this._basePathForIncludeKey = basePathForIncludeKey; + this._errors.length = 0; } static addPredefinedMacro(macro: string, value?: string) { @@ -54,8 +59,8 @@ export default class PpParser { this._definedMacros.set(macro, new MacroDefine(tk, macroBody)); } - static parse(scanner: PpScanner): string { - while (!scanner.isEnd()) { + static parse(scanner: PpScanner): string | null { + while (!scanner.isEnd() && this._errors.length === 0) { const directive = scanner.scanDirective(this._onToken.bind(this))!; if (scanner.isEnd()) break; switch (directive.type) { @@ -84,6 +89,7 @@ export default class PpParser { break; } } + if (this._errors.length > 0) return null; return PpUtils.expand(this.expandSegments, scanner.source, scanner.sourceMap); } @@ -92,6 +98,10 @@ export default class PpParser { return this._expandSegmentsStack[this._expandSegmentsStack.length - 1]; } + private static reportError(loc: ShaderRange | ShaderPosition, message: string) { + this._errors.push(new PreprocessorError(message, loc)); + } + private static _parseInclude(scanner: PpScanner) { const start = scanner.getShaderPosition(8); @@ -111,7 +121,9 @@ export default class PpParser { const end = scanner.getShaderPosition(); const chunk = this._includeMap[includedPath]; if (!chunk) { - ParserUtils.throw(id.location, `Shader slice "${includedPath}" not founded.`); + this.reportError(id.location, `Shader slice "${includedPath}" not founded.`); + + return; } const range = ShaderLab.createRange(start, end); @@ -285,7 +297,8 @@ export default class PpParser { scanner.skipSpace(false); const operand2 = this._parseRelationalExpression(scanner) as number; if (typeof operand1 !== typeof operand2 && typeof operand1 !== "number") { - ParserUtils.throw(opPos, "invalid operator in relation expression."); + this.reportError(opPos, "invalid operator in relation expression."); + return; } switch (operator) { case ">": @@ -310,7 +323,8 @@ export default class PpParser { scanner.skipSpace(false); const operand2 = this._parseShiftExpression(scanner) as number; if (typeof operand1 !== typeof operand2 && typeof operand1 !== "number") { - ParserUtils.throw(opPos, "invalid operator in shift expression."); + this.reportError(opPos, "invalid operator in shift expression."); + return; } switch (operator) { case ">>": @@ -333,7 +347,8 @@ export default class PpParser { scanner.skipSpace(false); const operand2 = this._parseAdditiveExpression(scanner) as number; if (typeof operand1 !== typeof operand2 && typeof operand1 !== "number") { - ParserUtils.throw(opPos, "invalid operator."); + this.reportError(opPos, "invalid operator."); + return false; } switch (operator) { case "+": @@ -354,7 +369,8 @@ export default class PpParser { scanner.skipSpace(false); const operand2 = this._parseMulticativeExpression(scanner) as number; if (typeof operand1 !== typeof operand2 && typeof operand1 !== "number") { - ParserUtils.throw(opPos, "invalid operator."); + this.reportError(opPos, "invalid operator."); + return; } switch (operator) { case "*": @@ -375,7 +391,7 @@ export default class PpParser { const opPos = scanner.getShaderPosition(); const parenExpr = this._parseParenthesisExpression(scanner); if ((operator === "!" && typeof parenExpr !== "boolean") || (operator !== "!" && typeof parenExpr !== "number")) { - ParserUtils.throw(opPos, "invalid operator."); + this.reportError(opPos, "invalid operator."); } switch (operator) { @@ -417,15 +433,14 @@ export default class PpParser { } else { const macro = this._definedMacros.get(id.lexeme); if (!macro) { - // ParserUtils.throw(id.location, 'undefined macro:', id.lexeme); return false; } if (macro.isFunction) { - ParserUtils.throw(id.location, "invalid function macro usage"); + this.reportError(id.location, "invalid function macro usage"); } const value = Number(macro.body.lexeme); if (!Number.isInteger(value)) { - ParserUtils.throw(id.location, "invalid const macro:", id.lexeme); + this.reportError(id.location, `invalid const macro: ${id.lexeme}`); } this._branchMacros.add(id.lexeme); return value; @@ -434,7 +449,7 @@ export default class PpParser { const integer = scanner.scanInteger(); return Number(integer.lexeme); } else { - ParserUtils.throw(scanner.getShaderPosition(), "invalid token", scanner.getCurChar()); + this.reportError(scanner.getShaderPosition(), `invalid token: ${scanner.getCurChar()}`); } } @@ -571,7 +586,7 @@ export default class PpParser { let end = macro.location.end; if (this._definedMacros.get(macro.lexeme) && macro.lexeme.startsWith("GL_")) { - ParserUtils.throw(macro.location, "redefined macro:", macro.lexeme); + this.reportError(macro.location, `redefined macro: ${macro.lexeme}`); } let macroArgs: BaseToken[] | undefined; diff --git a/packages/shader-lab/src/preprocessor/Preprocessor.ts b/packages/shader-lab/src/preprocessor/Preprocessor.ts index 35021f393e..1e06f346db 100644 --- a/packages/shader-lab/src/preprocessor/Preprocessor.ts +++ b/packages/shader-lab/src/preprocessor/Preprocessor.ts @@ -1,8 +1,14 @@ +import { PreprocessorError } from "../Error"; import PpParser from "./PpParser"; import PpScanner from "./PpScanner"; /** @internal */ export class Preprocessor { + /** @internal */ + static get errors(): PreprocessorError[] { + return PpParser._errors; + } + static baseScanner: PpScanner; /** @@ -16,7 +22,7 @@ export class Preprocessor { /** * Should call it after reset. */ - static process(source: string): string { + static process(source: string): string | null { this.baseScanner = new PpScanner(source); return PpParser.parse(this.baseScanner); } From c1762262d15cbf7f88c4b85a67acbc168cc4ab2e Mon Sep 17 00:00:00 2001 From: Sway007 Date: Fri, 19 Jul 2024 20:03:34 +0800 Subject: [PATCH 043/131] feat: add log --- packages/shader-lab/src/Error.ts | 4 +--- packages/shader-lab/src/ShaderLab.ts | 23 +++++++++++-------- packages/shader-lab/src/common/BaseScanner.ts | 13 +++++++++-- .../shader-lab/src/common/ShaderPosition.ts | 2 +- packages/shader-lab/src/common/ShaderRange.ts | 2 +- .../shader-lab/src/preprocessor/PpParser.ts | 8 ++++++- .../shader-lab/src/preprocessor/PpScanner.ts | 3 --- .../src/preprocessor/Preprocessor.ts | 8 +------ 8 files changed, 35 insertions(+), 28 deletions(-) diff --git a/packages/shader-lab/src/Error.ts b/packages/shader-lab/src/Error.ts index 06163f21a7..669ab2655a 100644 --- a/packages/shader-lab/src/Error.ts +++ b/packages/shader-lab/src/Error.ts @@ -1,4 +1,3 @@ -// #if _EDITOR import { ShaderPosition, ShaderRange } from "./common"; export abstract class GSError extends Error { @@ -10,7 +9,7 @@ export abstract class GSError extends Error { } override toString(): string { - return `${this.loc.toString()}: ${this.message}`; + return `${this.loc.toString()}\nReason: ${this.message}`; } } @@ -27,4 +26,3 @@ export class SemanticError extends GSError { this.name = "SemanticError"; } } -// #endif diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index a19981711a..34db4640c5 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -8,7 +8,8 @@ import { ShaderContentParser } from "./contentParser"; import { Logger, ShaderLib, ShaderMacro, ShaderPass, ShaderPlatformTarget, ShaderProgram } from "@galacean/engine"; import { ShaderPosition, ShaderRange } from "./common"; import { ShaderLabObjectPool } from "./ShaderLabObjectPool"; -import { GSError, PreprocessorError } from "./Error"; +import { PreprocessorError } from "./Error"; +import { PpParser } from "./preprocessor/PpParser"; export class ShaderLab implements IShaderLab { /** @@ -75,8 +76,8 @@ export class ShaderLab implements IShaderLab { const preprocessorStart = performance.now(); const ppdContent = Preprocessor.process(source); - if (Preprocessor.errors.length > 0) { - this._reportPreprocessError(Preprocessor.errors, source); + if (PpParser._errors.length > 0) { + this._reportPreprocessError(PpParser._errors); return { vertex: "", fragment: "" }; } @@ -136,14 +137,16 @@ export class ShaderLab implements IShaderLab { } // #endif - private _reportPreprocessError(errors: PreprocessorError[], source: string) { + private _reportPreprocessError(errors: PreprocessorError[]) { const errorsMsg = errors.map((item) => item.toString()).join("\n"); - console.error( - "Error occur when expansion ShaderLab code:\n\n" + - errorsMsg + - "ShaderLab source:\n\n" + - ShaderProgram._addLineNum(source) - ); + if (Logger.isEnabled) { + Logger.error( + `\nPreprocessor error occur in file ${PpParser._scanningFile}:\n\n` + + errorsMsg + + "ShaderLab source:\n\n" + + ShaderProgram._addLineNum(PpParser._scanningText) + ); + } } // private _reportParserError() diff --git a/packages/shader-lab/src/common/BaseScanner.ts b/packages/shader-lab/src/common/BaseScanner.ts index 87ca8f80a5..2b28672093 100644 --- a/packages/shader-lab/src/common/BaseScanner.ts +++ b/packages/shader-lab/src/common/BaseScanner.ts @@ -44,6 +44,16 @@ export default class BaseScanner { ); } + // #if _EDITOR + get line() { + return this._line; + } + + get column() { + return this._column; + } + // #endif + protected readonly _keywordsMap: Map; constructor(source: string, kws: Map = new Map()) { @@ -73,8 +83,6 @@ export default class BaseScanner { return; } - this._currentIndex++; - // #if _EDITOR if (this.getCurChar() === "\n") { this._line += 1; @@ -83,6 +91,7 @@ export default class BaseScanner { this._column += 1; } // #endif + this._currentIndex++; } skipSpace(includeLineBreak: boolean): void { diff --git a/packages/shader-lab/src/common/ShaderPosition.ts b/packages/shader-lab/src/common/ShaderPosition.ts index 5b173ba834..308e02b58d 100644 --- a/packages/shader-lab/src/common/ShaderPosition.ts +++ b/packages/shader-lab/src/common/ShaderPosition.ts @@ -28,6 +28,6 @@ export class ShaderPosition implements IPoolElement { } toString() { - return ``; + return ``; } } diff --git a/packages/shader-lab/src/common/ShaderRange.ts b/packages/shader-lab/src/common/ShaderRange.ts index 41a6e436bc..8b450e51e8 100644 --- a/packages/shader-lab/src/common/ShaderRange.ts +++ b/packages/shader-lab/src/common/ShaderRange.ts @@ -16,6 +16,6 @@ export class ShaderRange implements IPoolElement { } toString() { - return `[Start: ${this.start.toString()}; End: ${this.end.toString()}]`; + return `Start: ${this.start.toString()};\nEnd: ${this.end.toString()}\n`; } } diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 8af73fb6ca..40b2441713 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -22,7 +22,7 @@ export interface ExpandSegment { } /** @internal */ -export default class PpParser { +export class PpParser { private static _definedMacros: Map = new Map(); private static _expandSegmentsStack: ExpandSegment[][] = [[]]; @@ -34,6 +34,10 @@ export default class PpParser { /** @internal */ static _errors: PreprocessorError[] = []; + /** @internal */ + static _scanningText: string; + /** @internal */ + static _scanningFile = "__main__"; static reset(includeMap: Record, basePathForIncludeKey: string) { this._definedMacros.clear(); @@ -60,6 +64,8 @@ export default class PpParser { } static parse(scanner: PpScanner): string | null { + this._scanningText = scanner.source; + this._scanningFile = scanner.file; while (!scanner.isEnd() && this._errors.length === 0) { const directive = scanner.scanDirective(this._onToken.bind(this))!; if (scanner.isEnd()) break; diff --git a/packages/shader-lab/src/preprocessor/PpScanner.ts b/packages/shader-lab/src/preprocessor/PpScanner.ts index 892f8b87d3..b2f56c8b37 100644 --- a/packages/shader-lab/src/preprocessor/PpScanner.ts +++ b/packages/shader-lab/src/preprocessor/PpScanner.ts @@ -15,9 +15,6 @@ export type OnToken = (token: BaseToken, scanner: PpScanner) => void; export default class PpScanner extends BaseScanner { private static _splitCharacters = /[\w#.]/; - private line: number = 0; - private column: number = 0; - private macroLvl = 0; // #if _EDITOR diff --git a/packages/shader-lab/src/preprocessor/Preprocessor.ts b/packages/shader-lab/src/preprocessor/Preprocessor.ts index 1e06f346db..7eb6278792 100644 --- a/packages/shader-lab/src/preprocessor/Preprocessor.ts +++ b/packages/shader-lab/src/preprocessor/Preprocessor.ts @@ -1,14 +1,8 @@ -import { PreprocessorError } from "../Error"; -import PpParser from "./PpParser"; +import { PpParser } from "./PpParser"; import PpScanner from "./PpScanner"; /** @internal */ export class Preprocessor { - /** @internal */ - static get errors(): PreprocessorError[] { - return PpParser._errors; - } - static baseScanner: PpScanner; /** From b8ca43ff5e38a67668ef08238f10def470a427d3 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Mon, 22 Jul 2024 14:49:49 +0800 Subject: [PATCH 044/131] feat: add log --- packages/shader-lab/src/Error.ts | 4 +- packages/shader-lab/src/ShaderLab.ts | 38 ++++++++++--------- .../shader-lab/src/parser/SemanticAnalyzer.ts | 14 ++----- .../src/parser/ShaderTargetParser.ts | 14 ++++++- .../src/parser/builtin/functions.ts | 4 +- 5 files changed, 41 insertions(+), 33 deletions(-) diff --git a/packages/shader-lab/src/Error.ts b/packages/shader-lab/src/Error.ts index 669ab2655a..4264c2411b 100644 --- a/packages/shader-lab/src/Error.ts +++ b/packages/shader-lab/src/Error.ts @@ -9,7 +9,7 @@ export abstract class GSError extends Error { } override toString(): string { - return `${this.loc.toString()}\nReason: ${this.message}`; + return `>>>>>\n${this.loc.toString()}\nReason: ${this.message}\n<<<<<`; } } @@ -20,7 +20,7 @@ export class PreprocessorError extends GSError { } } -export class SemanticError extends GSError { +export class CompilationError extends GSError { constructor(message: string, loc: ShaderRange, cause?: Error) { super(message, loc, cause); this.name = "SemanticError"; diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index 34db4640c5..246eb336ee 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -8,7 +8,7 @@ import { ShaderContentParser } from "./contentParser"; import { Logger, ShaderLib, ShaderMacro, ShaderPass, ShaderPlatformTarget, ShaderProgram } from "@galacean/engine"; import { ShaderPosition, ShaderRange } from "./common"; import { ShaderLabObjectPool } from "./ShaderLabObjectPool"; -import { PreprocessorError } from "./Error"; +import { PreprocessorError, CompilationError } from "./Error"; import { PpParser } from "./preprocessor/PpParser"; export class ShaderLab implements IShaderLab { @@ -68,13 +68,7 @@ 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 (PpParser._errors.length > 0) { this._reportPreprocessError(PpParser._errors); @@ -85,8 +79,12 @@ export class ShaderLab implements IShaderLab { const lexer = new Lexer(ppdContent); const tokens = lexer.tokenize(); - const program = ShaderLab._parser.parse(tokens); + + const { _parser: parser } = ShaderLab; + + const program = parser.parse(tokens); if (!program) { + this._reportParserError(parser.errors, ppdContent); return { vertex: "", fragment: "" }; } const codeGen = @@ -138,16 +136,22 @@ export class ShaderLab implements IShaderLab { // #endif private _reportPreprocessError(errors: PreprocessorError[]) { + if (!Logger.isEnabled) return; const errorsMsg = errors.map((item) => item.toString()).join("\n"); - if (Logger.isEnabled) { - Logger.error( - `\nPreprocessor error occur in file ${PpParser._scanningFile}:\n\n` + - errorsMsg + - "ShaderLab source:\n\n" + - ShaderProgram._addLineNum(PpParser._scanningText) - ); - } + Logger.error( + `\nPreprocessor error occur in file ${PpParser._scanningFile}:\n\n` + + errorsMsg + + "\nShaderLab source:\n\n" + + ShaderProgram._addLineNum(PpParser._scanningText) + ); } - // private _reportParserError() + private _reportParserError(errors: CompilationError[], source: string) { + if (!Logger.isEnabled) return; + const errorsMsg = errors.map((item) => item.toString()).join("\n"); + Logger.error( + `\nShaderLab syntax error occur.\n${errorsMsg}\n\nExpanded ShaderLab source:\n\n` + + ShaderProgram._addLineNum(source) + ); + } } diff --git a/packages/shader-lab/src/parser/SemanticAnalyzer.ts b/packages/shader-lab/src/parser/SemanticAnalyzer.ts index ea7b0487bf..fd61bd53c9 100644 --- a/packages/shader-lab/src/parser/SemanticAnalyzer.ts +++ b/packages/shader-lab/src/parser/SemanticAnalyzer.ts @@ -1,7 +1,7 @@ import { ShaderRange } from "../common"; import { TreeNode } from "./AST"; // #if _EDITOR -import { SemanticError } from "../Error"; +import { CompilationError } from "../Error"; // #endif import { ShaderData } from "./ShaderInfo"; import { SymbolInfo, SymbolTable } from "../parser/symbolTable"; @@ -22,9 +22,7 @@ export default class SematicAnalyzer { symbolTable: SymbolTableStack = new SymbolTableStack(); private _shaderData = new ShaderData(); - // #if _EDITOR - readonly errors: SemanticError[] = []; - // #endif + readonly errors: CompilationError[] = []; get shaderData() { return this._shaderData; @@ -41,9 +39,7 @@ export default class SematicAnalyzer { this._shaderData = new ShaderData(); this.symbolTable.clear(); this.newScope(); - // #if _EDITOR this.errors.length = 0; - // #endif } newScope() { @@ -63,13 +59,11 @@ export default class SematicAnalyzer { return this._translationRuleTable.get(pid); } - // #if _EDITOR error(loc: ShaderRange, ...param: any[]) { - Logger.error(loc, ...param); + // Logger.warn(loc, ...param); - const err = new SemanticError(param.join(""), loc); + const err = new CompilationError(param.join(""), loc); this.errors.push(err); return err; } - // #endif } diff --git a/packages/shader-lab/src/parser/ShaderTargetParser.ts b/packages/shader-lab/src/parser/ShaderTargetParser.ts index 56250740b7..89d268af36 100644 --- a/packages/shader-lab/src/parser/ShaderTargetParser.ts +++ b/packages/shader-lab/src/parser/ShaderTargetParser.ts @@ -10,6 +10,7 @@ import { addTranslationRule, createGrammar } from "../lalr/CFG"; import { LALR1 } from "../lalr"; import { ParserUtils } from "../Utils"; import { Logger } from "@galacean/engine"; +import { CompilationError } from "../Error"; /** * The syntax parser and sematic analyzer of `ShaderLab` compiler @@ -31,6 +32,11 @@ export class ShaderTargetParser { return this.gotoTable.get(this.curState); } + /** @internal */ + get errors() { + return this.sematicAnalyzer.errors; + } + static _singleton: ShaderTargetParser; static create() { @@ -53,7 +59,7 @@ export class ShaderTargetParser { } parse(tokens: Generator): ASTNode.GLShaderProgram | null { - this.sematicAnalyzer.reset(); + this._reset(); const start = performance.now(); const { _traceBackStack: traceBackStack, sematicAnalyzer } = this; traceBackStack.push(0); @@ -103,12 +109,16 @@ export class ShaderTargetParser { traceBackStack.push(nextState); continue; } else { - Logger.error(token.location, `parse error token ${token.lexeme}`); + this.sematicAnalyzer.errors.push(new CompilationError(`Unexpected token ${token.lexeme}`, token.location)); return null; } } } + private _reset() { + this.sematicAnalyzer.reset(); + } + // #if _EDITOR private _printStack(nextToken: BaseToken) { let str = ""; diff --git a/packages/shader-lab/src/parser/builtin/functions.ts b/packages/shader-lab/src/parser/builtin/functions.ts index d6b5d4dcf3..d43bb85f5c 100644 --- a/packages/shader-lab/src/parser/builtin/functions.ts +++ b/packages/shader-lab/src/parser/builtin/functions.ts @@ -136,8 +136,8 @@ BuiltinFunction._create("max", EGenType.GenType, EGenType.GenType); BuiltinFunction._create("max", EGenType.GenType, EKeyword.FLOAT); BuiltinFunction._create("max", EGenType.GenIntType, EGenType.GenIntType); BuiltinFunction._create("max", EGenType.GenIntType, EKeyword.INT); -BuiltinFunction._create("max", EGenType.GenUintType, EGenType.GenUintType, EGenType.GenUintType); -BuiltinFunction._create("max", EGenType.GenUintType, EGenType.GenUintType, EKeyword.UINT); +// BuiltinFunction._create("max", EGenType.GenUintType, EGenType.GenUintType, EGenType.GenUintType); +// BuiltinFunction._create("max", EGenType.GenUintType, EGenType.GenUintType, EKeyword.UINT); BuiltinFunction._create("clamp", EGenType.GenType, EGenType.GenType, EGenType.GenType, EGenType.GenType); BuiltinFunction._create("clamp", EGenType.GenType, EGenType.GenType, EKeyword.FLOAT, EKeyword.FLOAT); BuiltinFunction._create("clamp", EGenType.GenIntType, EGenType.GenIntType, EGenType.GenIntType, EGenType.GenIntType); From ce23026e1ff15d143bba61490204e1894ef12ca8 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 1 Aug 2024 16:05:47 +0800 Subject: [PATCH 045/131] fix: multi dist --- packages/shader-lab/package.json | 1 + rollup.config.js | 61 +++++++++++++++----------- tests/src/shader-lab/ShaderLab.test.ts | 12 +---- tsconfig.json | 4 +- 4 files changed, 42 insertions(+), 36 deletions(-) diff --git a/packages/shader-lab/package.json b/packages/shader-lab/package.json index 9c84279881..35a57b309c 100644 --- a/packages/shader-lab/package.json +++ b/packages/shader-lab/package.json @@ -9,6 +9,7 @@ "main": "dist/main.js", "module": "dist/module.js", "browser": "dist/browser.min.js", + "editor": "dist/editor.js", "debug": "src/index.ts", "types": "types/index.d.ts", "scripts": { diff --git a/rollup.config.js b/rollup.config.js index f616b01aec..eb6a20e866 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -8,7 +8,6 @@ import serve from "rollup-plugin-serve"; import miniProgramPlugin from "./rollup.miniprogram.plugin"; import replace from "@rollup/plugin-replace"; import { swc, defineRollupSwcOption, minify } from "rollup-plugin-swc3"; -import modify from "rollup-plugin-modify"; import jscc from "rollup-plugin-jscc"; const { BUILD_TYPE, NODE_ENV } = process.env; @@ -26,6 +25,8 @@ const pkgs = fs }; }); +const shaderLabPkg = pkgs.find((item) => item.pkgJson.name === "@galacean/engine-shader-lab"); + // toGlobalName const extensions = [".js", ".jsx", ".ts", ".tsx"]; @@ -49,9 +50,6 @@ const commonPlugins = [ }) ), commonjs(), - jscc({ - values: { _EDITOR: NODE_ENV !== "release" } - }), NODE_ENV === "development" ? serve({ contentBase: "packages", @@ -60,9 +58,15 @@ const commonPlugins = [ : null ]; -function config({ location, pkgJson }) { +function config({ location, pkgJson, editorMode }) { const input = path.join(location, "src", "index.ts"); const dependencies = Object.assign({}, pkgJson.dependencies ?? {}, pkgJson.peerDependencies ?? {}); + commonPlugins.push( + jscc({ + values: { _EDITOR: NODE_ENV !== "release" || editorMode } + }) + ); + const external = Object.keys(dependencies); commonPlugins.push( replace({ @@ -76,17 +80,14 @@ function config({ location, pkgJson }) { const umdConfig = pkgJson.umd; let file = path.join(location, "dist", "browser.js"); - const plugins = [ - modify({ - find: "chevrotain", - replace: path.join(process.cwd(), "packages", "shader-lab", `./node_modules/chevrotain/lib/chevrotain.js`) - }), - ...commonPlugins - ]; + const plugins = commonPlugins; if (compress) { plugins.push(minify({ sourceMap: true })); file = path.join(location, "dist", "browser.min.js"); } + if (editorMode) { + file = path.join(location, "dist", "browser.editor.js"); + } const umdExternal = Object.keys(umdConfig.globals ?? {}); @@ -106,13 +107,17 @@ function config({ location, pkgJson }) { }; }, mini: () => { + let file = path.join(location, "dist", "miniprogram.js"); const plugins = [...commonPlugins, ...miniProgramPlugin]; + if (editorMode) { + file = path.join(location, "dist", "miniprogram.editor.js"); + } return { input, output: [ { format: "cjs", - file: path.join(location, "dist/miniprogram.js"), + file, sourcemap: false } ], @@ -121,24 +126,24 @@ function config({ location, pkgJson }) { }; }, module: () => { - const plugins = [ - modify({ - find: "chevrotain", - replace: path.join(process.cwd(), "packages", "shader-lab", `./node_modules/chevrotain/lib/chevrotain.js`) - }), - ...commonPlugins - ]; + const plugins = commonPlugins; + let esFile = pkgJson.module; + let mainFile = pkgJson.main; + if (editorMode) { + esFile = path.join(location, "dist", "module.editor.js"); + mainFile = path.join(location, "dist", "main.editor.js"); + } return { input, external, output: [ { - file: path.join(location, pkgJson.module), + file: esFile, format: "es", sourcemap: true }, { - file: path.join(location, pkgJson.main), + file: mainFile, sourcemap: true, format: "commonjs" } @@ -174,7 +179,7 @@ switch (BUILD_TYPE) { function getUMD() { const configs = pkgs.filter((pkg) => pkg.pkgJson.umd); - return configs + const umds = configs .map((config) => makeRollupConfig({ ...config, type: "umd" })) .concat( configs.map((config) => @@ -186,16 +191,22 @@ function getUMD() { }) ) ); + umds.push(makeRollupConfig({ ...shaderLabPkg, editorMode: true, type: "umd" })); + return umds; } function getModule() { const configs = [...pkgs]; - return configs.map((config) => makeRollupConfig({ ...config, type: "module" })); + const modules = configs.map((config) => makeRollupConfig({ ...config, type: "module" })); + modules.push(makeRollupConfig({ ...shaderLabPkg, editorMode: true, type: "module" })); + return modules; } function getMini() { const configs = [...pkgs]; - return configs.map((config) => makeRollupConfig({ ...config, type: "mini" })); + const minis = configs.map((config) => makeRollupConfig({ ...config, type: "mini" })); + minis.push(makeRollupConfig({ ...shaderLabPkg, editorMode: true, type: "mini" })); + return minis; } function getAll() { diff --git a/tests/src/shader-lab/ShaderLab.test.ts b/tests/src/shader-lab/ShaderLab.test.ts index 1fc6cbfed4..e6385ef312 100644 --- a/tests/src/shader-lab/ShaderLab.test.ts +++ b/tests/src/shader-lab/ShaderLab.test.ts @@ -1,14 +1,6 @@ -import { - BlendFactor, - BlendOperation, - CompareFunction, - CullMode, - RenderQueueType, - RenderStateDataKey, - StencilOperation -} from "@galacean/engine-core"; +import { BlendOperation, CompareFunction, CullMode, RenderStateDataKey } from "@galacean/engine-core"; import { Color } from "@galacean/engine-math"; -import { ShaderLab } from "@galacean/engine-shader-lab"; +import { ShaderLab } from "@galacean/engine-shader-lab/dist/main.editor"; import { glslValidate } from "./ShaderValidate"; import chai, { expect } from "chai"; diff --git a/tsconfig.json b/tsconfig.json index f62e2e508d..6bef4d478a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,7 +14,9 @@ "skipLibCheck": true, "paths": { "@/*": ["src/*"] // 配置模块别名,对于 chair 项目需做更改,见下文 - } + }, + "allowJs": true, + "checkJs": false }, "exclude": ["node_modules", "types", "packages/*/tests"] } From 8c907960b645f66f50ca8edeaf96825572daa7f0 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Fri, 23 Aug 2024 14:15:08 +0800 Subject: [PATCH 046/131] feat(shaderlab): [wip] optmize error log --- packages/core/src/index.ts | 2 - packages/shader-lab/src/Error.ts | 70 +++++++++++++++-- packages/shader-lab/src/ShaderLab.ts | 78 +++++++++---------- packages/shader-lab/src/Utils.ts | 8 +- .../shader-lab/src/codeGen/CodeGenVisitor.ts | 2 - .../shader-lab/src/codeGen/GLESVisitor.ts | 26 ++++++- packages/shader-lab/src/common/BaseScanner.ts | 22 +++--- packages/shader-lab/src/common/BaseToken.ts | 8 +- .../shader-lab/src/common/ShaderPosition.ts | 16 +--- .../src/contentParser/ShaderContentParser.ts | 51 +++++++++--- packages/shader-lab/src/lexer/Lexer.ts | 6 +- packages/shader-lab/src/parser/AST.ts | 10 +-- .../shader-lab/src/parser/SemanticAnalyzer.ts | 8 +- .../src/parser/ShaderTargetParser.ts | 5 +- .../src/preprocessor/MacroDefine.ts | 4 +- .../shader-lab/src/preprocessor/PpParser.ts | 29 ++++--- .../shader-lab/src/preprocessor/PpScanner.ts | 8 +- 17 files changed, 210 insertions(+), 143 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index df404a7cf9..01952de4e2 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -66,5 +66,3 @@ export * from "./Utils"; export { ShaderMacroCollection } from "./shader/ShaderMacroCollection"; export * from "./postProcess"; -/** @internal */ -export { ShaderProgram } from "./shader/ShaderProgram"; diff --git a/packages/shader-lab/src/Error.ts b/packages/shader-lab/src/Error.ts index 4264c2411b..0d71c26375 100644 --- a/packages/shader-lab/src/Error.ts +++ b/packages/shader-lab/src/Error.ts @@ -1,28 +1,82 @@ +import { Logger } from "@galacean/engine"; import { ShaderPosition, ShaderRange } from "./common"; +export enum ErrorLevel { + ERROR = 0, + WARN +} + export abstract class GSError extends Error { + static wrappingLineCount = 2; + readonly loc: ShaderRange | ShaderPosition; + readonly source: string; + readonly file?: string; + level = ErrorLevel.ERROR; - constructor(message: string, loc: ShaderRange | ShaderPosition, cause?: Error) { + constructor(message: string, loc: ShaderRange | ShaderPosition, source: string, file?: string, cause?: Error) { super(message, { cause }); this.loc = loc; + this.source = source; + this.file = file; } - override toString(): string { - return `>>>>>\n${this.loc.toString()}\nReason: ${this.message}\n<<<<<`; + log(_source?: string): void { + if (!Logger.enable) return; + const logger = this.level === ErrorLevel.ERROR ? Logger.error : Logger.warn; + + let start: ShaderPosition, end: ShaderPosition; + const { message, loc, source: originSource } = this; + let source = originSource; + if (_source) source = _source; + if (!source) { + logger(message); + } + + 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); + diagnosticMessage += "^".repeat(Math.max(end.column - start.column, 1)); + diagnosticMessage += "\n"; + } + } + + logger(diagnosticMessage); } } export class PreprocessorError extends GSError { - constructor(message: string, loc: ShaderRange | ShaderPosition, cause?: Error) { - super(message, loc, cause); + 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, cause?: Error) { - super(message, loc, cause); - this.name = "SemanticError"; + constructor(message: string, loc: ShaderRange | ShaderPosition, source: string, file?: string, cause?: Error) { + super(message, loc, source, file, cause); + this.name = "CompilationError"; + } +} + +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"; } } diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index 246eb336ee..19b5bf44cc 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -2,44 +2,28 @@ 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, ShaderProgram } from "@galacean/engine"; import { ShaderPosition, ShaderRange } from "./common"; import { ShaderLabObjectPool } from "./ShaderLabObjectPool"; -import { PreprocessorError, CompilationError } from "./Error"; +import { GSError } from "./Error"; 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); return position; } @@ -49,6 +33,15 @@ export class ShaderLab implements IShaderLab { return range; } + private _errors: GSError[] = []; + + /** + * Retrieve the compilation errors + */ + get errors(): GSError[] | undefined { + return this._errors; + } + _parseShaderPass( source: string, vertexEntry: string, @@ -71,7 +64,10 @@ export class ShaderLab implements IShaderLab { const preprocessorStart = performance.now(); const ppdContent = Preprocessor.process(source); if (PpParser._errors.length > 0) { - this._reportPreprocessError(PpParser._errors); + for (const err of PpParser._errors) { + this._errors.push(err); + } + this._logErrors(this._errors); return { vertex: "", fragment: "" }; } @@ -82,9 +78,13 @@ export class ShaderLab implements IShaderLab { const { _parser: parser } = ShaderLab; + ShaderLab._processingPassText = ppdContent; const program = parser.parse(tokens); + for (const err of parser.errors) { + this._errors.push(err); + } if (!program) { - this._reportParserError(parser.errors, ppdContent); + this._logErrors(this._errors); return { vertex: "", fragment: "" }; } const codeGen = @@ -93,11 +93,18 @@ export class ShaderLab implements IShaderLab { const start = performance.now(); const ret = codeGen.visitShaderProgram(program, vertexEntry, fragmentEntry); Logger.info(`[CodeGen] cost time: ${performance.now() - start}ms`); + ShaderLab._processingPassText = undefined; + + for (const err of codeGen.errors) { + this._errors.push(err); + } + this._logErrors(this._errors); return ret; } _parseShaderContent(shaderSource: string): IShaderContent { + this._errors.length = 0; ShaderContentParser.reset(); return ShaderContentParser.parse(shaderSource); } @@ -135,23 +142,10 @@ export class ShaderLab implements IShaderLab { } // #endif - private _reportPreprocessError(errors: PreprocessorError[]) { + private _logErrors(errors: GSError[], source?: string) { if (!Logger.isEnabled) return; - const errorsMsg = errors.map((item) => item.toString()).join("\n"); - Logger.error( - `\nPreprocessor error occur in file ${PpParser._scanningFile}:\n\n` + - errorsMsg + - "\nShaderLab source:\n\n" + - ShaderProgram._addLineNum(PpParser._scanningText) - ); - } - - private _reportParserError(errors: CompilationError[], source: string) { - if (!Logger.isEnabled) return; - const errorsMsg = errors.map((item) => item.toString()).join("\n"); - Logger.error( - `\nShaderLab syntax error occur.\n${errorsMsg}\n\nExpanded ShaderLab source:\n\n` + - ShaderProgram._addLineNum(source) - ); + for (const err of errors) { + err.log(source); + } } } diff --git a/packages/shader-lab/src/Utils.ts b/packages/shader-lab/src/Utils.ts index 38614c1e0e..a84aadaea2 100644 --- a/packages/shader-lab/src/Utils.ts +++ b/packages/shader-lab/src/Utils.ts @@ -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 import State from "./lalr/State"; // #endif -import { Logger } from "@galacean/engine"; export class ParserUtils { static unwrapNodeByType(node: TreeNode, type: ENonTerminal): T | undefined { @@ -39,11 +38,6 @@ export class ParserUtils { return sm < ENonTerminal.START; } - static throw(pos: ShaderPosition | ShaderRange | number, ...msgs: any[]) { - Logger.error(pos.toString(), ...msgs); - throw msgs.join(" "); - } - /** * @internal */ diff --git a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts index ba7c2f5780..a79b12a755 100644 --- a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts +++ b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts @@ -11,8 +11,6 @@ import { VisitorContext } from "./VisitorContext"; * The code generator */ export class CodeGenVisitor { - protected constructor() {} - defaultCodeGen(children: NodeChild[]) { let ret: string[] = []; for (const child of children) { diff --git a/packages/shader-lab/src/codeGen/GLESVisitor.ts b/packages/shader-lab/src/codeGen/GLESVisitor.ts index 2f8e35cbab..6ed40663dc 100644 --- a/packages/shader-lab/src/codeGen/GLESVisitor.ts +++ b/packages/shader-lab/src/codeGen/GLESVisitor.ts @@ -5,8 +5,10 @@ import { ESymbolType, FnSymbol, StructSymbol, SymbolInfo } from "../parser/symbo import { EShaderStage } from "../common/Enums"; import { IShaderInfo } from "@galacean/engine-design"; import { ICodeSegment } from "./types"; -import { Logger } from "@galacean/engine"; import { VisitorContext } from "./VisitorContext"; +import { CompilationError, GSError } from "../Error"; +import { ShaderPosition, ShaderRange } from "../common"; +import { ShaderLab } from "../ShaderLab"; const defaultPrecision = ` #ifdef GL_FRAGMENT_PRECISION_HIGH @@ -22,10 +24,20 @@ export abstract class GLESVisitor extends CodeGenVisitor { protected _versionText: string = ""; protected _extensions: string = ""; + private _errors: GSError[] = []; + abstract getAttributeDeclare(): ICodeSegment[]; abstract getVaryingDeclare(): ICodeSegment[]; + /** + * @internal + */ + get errors() { + return this._errors; + } + visitShaderProgram(node: ASTNode.GLShaderProgram, vertexEntry: string, fragmentEntry: string): IShaderInfo { + this._errors.length = 0; VisitorContext.reset(); VisitorContext.context._passSymbolTable = node.shaderData.symbolTable; @@ -45,11 +57,11 @@ export abstract class GLESVisitor extends CodeGenVisitor { const returnType = fnNode.protoType.returnType; if (typeof returnType.type !== "string") { - Logger.warn("main entry can only return struct."); + this.reportError(returnType.location, "main entry can only return struct."); } else { const varyStruct = symbolTable.lookup({ ident: returnType.type, symbolType: ESymbolType.STRUCT }); if (!varyStruct) { - Logger.warn("invalid varying struct:", returnType.type); + this.reportError(returnType.location, `invalid varying struct: ${returnType.type}`); } else { VisitorContext.context.varyingStruct = varyStruct.astNode; } @@ -64,7 +76,7 @@ export abstract class GLESVisitor extends CodeGenVisitor { symbolType: ESymbolType.STRUCT }); if (!structSymbol) { - Logger.warn("no attribute struct found."); + this.reportError(paramInfo.astNode.location, `Not found attribute struct "${paramInfo.typeInfo.type}".`); continue; } VisitorContext.context.attributeStructs.push(structSymbol.astNode); @@ -147,4 +159,10 @@ export abstract class GLESVisitor extends CodeGenVisitor { } return this._getGlobalText(data, textList, lastLength, _serialized); } + + private reportError(loc: ShaderRange | ShaderPosition, message: string): CompilationError { + const error = new CompilationError(message, loc, ShaderLab._processingPassText); + this._errors.push(error); + return error; + } } diff --git a/packages/shader-lab/src/common/BaseScanner.ts b/packages/shader-lab/src/common/BaseScanner.ts index 2b28672093..c979c412a4 100644 --- a/packages/shader-lab/src/common/BaseScanner.ts +++ b/packages/shader-lab/src/common/BaseScanner.ts @@ -1,6 +1,6 @@ import { ETokenType, ShaderRange, ShaderPosition } from "."; +import { ScannerError } from "../Error"; import { ShaderLab } from "../ShaderLab"; -import { ParserUtils } from "../Utils"; import { BaseToken } from "./BaseToken"; export type OnToken = (token: BaseToken, scanner: BaseScanner) => void; @@ -35,16 +35,9 @@ export default class BaseScanner { } get curPosition(): ShaderPosition { - return ShaderLab.createPosition( - this._currentIndex, - // #if _EDITOR - this._column, - this._line - // #endif - ); + return ShaderLab.createPosition(this._currentIndex, this._line, this._column); } - // #if _EDITOR get line() { return this._line; } @@ -52,7 +45,6 @@ export default class BaseScanner { get column() { return this._column; } - // #endif protected readonly _keywordsMap: Map; @@ -83,14 +75,12 @@ export default class BaseScanner { return; } - // #if _EDITOR if (this.getCurChar() === "\n") { this._line += 1; this._column = 0; } else { this._column += 1; } - // #endif this._currentIndex++; } @@ -133,11 +123,17 @@ export default class BaseScanner { this.skipCommentsAndSpace(); const peek = this.peek(text.length); if (peek !== text) { - ParserUtils.throw(this._currentIndex, `Expect ${text}, got ${peek}`); + this.throwError(this.curPosition, `Expect text "${text}", but got "${peek}"`); } this.advance(text.length); } + throwError(pos: ShaderPosition | ShaderRange, ...msgs: any[]) { + const error = new ScannerError(msgs.join(" "), pos, this._source); + error.log(); + throw error; + } + scanPairedText(left: string, right: string, balanced = false, skipLeading = false) { if (!skipLeading) { this.scanText(left); diff --git a/packages/shader-lab/src/common/BaseToken.ts b/packages/shader-lab/src/common/BaseToken.ts index 6a7b49541d..087ffd99a6 100644 --- a/packages/shader-lab/src/common/BaseToken.ts +++ b/packages/shader-lab/src/common/BaseToken.ts @@ -20,13 +20,7 @@ export class BaseToken implements IPoolElement { if (arg instanceof ShaderRange) { this.location = arg as ShaderRange; } else { - const end = ShaderLab.createPosition( - arg.index + lexeme.length, - // #if _EDITOR - arg.line, - arg.column - // #endif - ); + const end = ShaderLab.createPosition(arg.index + lexeme.length, arg.line, arg.column + lexeme.length); this.location = ShaderLab.createRange(arg, end); } } diff --git a/packages/shader-lab/src/common/ShaderPosition.ts b/packages/shader-lab/src/common/ShaderPosition.ts index 308e02b58d..e1ab88b619 100644 --- a/packages/shader-lab/src/common/ShaderPosition.ts +++ b/packages/shader-lab/src/common/ShaderPosition.ts @@ -2,23 +2,13 @@ import { IPoolElement } from "@galacean/engine"; export class ShaderPosition implements IPoolElement { index: number; - // #if _EDITOR - line?: number; - column?: number; - // #endif + line: number; + column: number; - setX( - index: number, - /** #if _EDITOR */ - line?: number, - column?: number - /** #endif */ - ) { + setX(index: number, line: number, column: number) { this.index = index; - /** #if _EDITOR */ this.line = line; this.column = column; - /** #endif */ } dispose(): void { diff --git a/packages/shader-lab/src/contentParser/ShaderContentParser.ts b/packages/shader-lab/src/contentParser/ShaderContentParser.ts index 3d4b4a03a7..fbc5e181df 100644 --- a/packages/shader-lab/src/contentParser/ShaderContentParser.ts +++ b/packages/shader-lab/src/contentParser/ShaderContentParser.ts @@ -23,7 +23,7 @@ import { IShaderPassContent, IRenderStates } from "@galacean/engine-design"; -import { ParserUtils } from "../Utils"; +import { CompilationError } from "../Error"; const EngineType = [ EKeyword.GS_RenderQueueType, @@ -88,7 +88,6 @@ export class ShaderContentParser { for (let i = 0; i < subShader.passes.length; i++) { const pass = subShader.passes[i]; - // for (const pass of subShader.passes) { Object.assign(pass.renderStates.constantMap, constMap); Object.assign(pass.renderStates.variableMap, variableMap); if (pass.isUsePass) continue; @@ -174,7 +173,13 @@ export class ShaderContentParser { scanner.scanText(";"); const sm = this._symbolTable.lookup({ type: stateToken.type, ident: variable.lexeme }); if (!sm?.value) { - ParserUtils.throw(scanner.current, `Invalid ${stateToken.lexeme} variable:`, variable.lexeme); + const error = new CompilationError( + `Invalid ${stateToken.lexeme} variable: ${variable.lexeme}`, + scanner.curPosition, + scanner.source + ); + error.log(); + throw error; } const renderState = sm.value as IRenderStates; Object.assign(ret.renderStates.constantMap, renderState.constantMap); @@ -222,15 +227,28 @@ export class ShaderContentParser { scanner.scanText("]"); scanner.scanText("="); } else if (op.lexeme !== "=") { - ParserUtils.throw(scanner.current, "Invalid syntax, expect character '=', but got", op.lexeme); + const error = new CompilationError( + `Invalid syntax, expect character '=', but got ${op.lexeme}`, + scanner.curPosition, + scanner.source + ); + error.log(); + throw error; } renderStateProp += idx; } renderStateProp = state + renderStateProp; const renderStateElementKey = RenderStateDataKey[renderStateProp]; - if (renderStateElementKey == undefined) - ParserUtils.throw(scanner.current, "Invalid render state element", renderStateProp); + if (renderStateElementKey == undefined) { + const error = new CompilationError( + `Invalid render state element ${renderStateProp}`, + scanner.curPosition, + scanner.source + ); + error.log(); + throw error; + } scanner.skipCommentsAndSpace(); let value: any; @@ -258,8 +276,15 @@ export class ShaderContentParser { scanner._advance(); const engineTypeProp = scanner.scanToken(); value = ShaderContentParser._engineType[token.lexeme]?.[engineTypeProp.lexeme]; - if (value == undefined) - ParserUtils.throw(scanner.current, "Invalid engine constant:", `${token.lexeme}.${engineTypeProp.lexeme}`); + if (value == undefined) { + const error = new CompilationError( + `Invalid engine constant: ${token.lexeme}.${engineTypeProp.lexeme}`, + scanner.curPosition, + scanner.source + ); + error.log(); + throw error; + } } else { value = token.lexeme; } @@ -278,7 +303,9 @@ export class ShaderContentParser { scanner.scanText(";"); const value = ShaderContentParser._engineType.RenderQueueType[word.lexeme]; if (value == undefined) { - ParserUtils.throw(scanner.current, "Invalid render queue", word.lexeme); + const error = new CompilationError(`Invalid render queue ${word.lexeme}`, word.location, scanner.source); + error.log(); + throw error; } const key = RenderStateDataKey.RenderQueueType; ret.renderStates.constantMap[key] = value; @@ -423,11 +450,11 @@ export class ShaderContentParser { this._addGlobalStatement(ret, scanner, start, word.lexeme.length); scanner.scanText("="); const entry = scanner.scanToken(); - // #if _EDITOR if (ret[word.lexeme]) { - ParserUtils.throw(scanner.current, "reassign main entry"); + const error = new CompilationError("reassign main entry", scanner.curPosition, scanner.source); + error.log(); + throw error; } - // #endif const key = word.type === EKeyword.GS_VertexShader ? "vertexEntry" : "fragmentEntry"; ret[key] = entry.lexeme; scanner.scanText(";"); diff --git a/packages/shader-lab/src/lexer/Lexer.ts b/packages/shader-lab/src/lexer/Lexer.ts index 346e7788f6..84dc9dd08c 100644 --- a/packages/shader-lab/src/lexer/Lexer.ts +++ b/packages/shader-lab/src/lexer/Lexer.ts @@ -278,8 +278,7 @@ export class Lexer extends BaseScanner { return this._scanStringConst(); default: - console.log("at position", start); - throw `Unexpected character ${this.getCurChar()}`; + this.throwError(this.curPosition, `Unexpected character ${this.getCurChar()}`); } return token; } @@ -385,7 +384,8 @@ export class Lexer extends BaseScanner { buffer.push(this.getCurChar()); this.advance(); } - if (!LexerUtils.isNum(this.getCurChar())) throw "lexing error, invalid exponent suffix."; + if (!LexerUtils.isNum(this.getCurChar())) + this.throwError(this.curPosition, "lexing error, invalid exponent suffix."); while (LexerUtils.isNum(this.getCurChar())) { buffer.push(this.getCurChar()); this.advance(); diff --git a/packages/shader-lab/src/parser/AST.ts b/packages/shader-lab/src/parser/AST.ts index 1ebad0decf..71745de4d4 100644 --- a/packages/shader-lab/src/parser/AST.ts +++ b/packages/shader-lab/src/parser/AST.ts @@ -435,7 +435,7 @@ export namespace ASTNode { this.compute = (a, b) => a % b; break; default: - throw `not implemented operator ${operator.lexeme}`; + sa.error(operator.location, `not implemented operator ${operator.lexeme}`); } } } @@ -459,10 +459,10 @@ export namespace ASTNode { else { const id = child as VariableIdentifier; if (!id.symbolInfo) { - sa.error(id.location, "undeclared symbol:", id.lexeme); + sa.error(id.location, "Undeclared symbol:", id.lexeme); } if (!ParserUtils.typeCompatible(EKeyword.INT, id.typeInfo)) { - sa.error(id.location, "invalid integer."); + sa.error(id.location, "Invalid integer."); return; } } @@ -535,7 +535,7 @@ export namespace ASTNode { const arraySpecifier = this.children[3] as ArraySpecifier; // #if _EDITOR if (typeInfo.arraySpecifier && arraySpecifier) { - sa.error(arraySpecifier.location, "array of array is not supported."); + sa.error(arraySpecifier.location, "Array of array is not supported."); } // #endif typeInfo.arraySpecifier = arraySpecifier; @@ -872,7 +872,7 @@ export namespace ASTNode { const fnSymbol = sa.symbolTable.lookup({ ident: fnIdent, symbolType: ESymbolType.FN, signature: paramSig }); if (!fnSymbol) { // #if _EDITOR - sa.error(this.location, "no overload function type found:", functionIdentifier.ident); + sa.error(this.location, "No overload function type found: ", functionIdentifier.ident); // #endif return; } diff --git a/packages/shader-lab/src/parser/SemanticAnalyzer.ts b/packages/shader-lab/src/parser/SemanticAnalyzer.ts index fd61bd53c9..691341026d 100644 --- a/packages/shader-lab/src/parser/SemanticAnalyzer.ts +++ b/packages/shader-lab/src/parser/SemanticAnalyzer.ts @@ -1,13 +1,11 @@ import { ShaderRange } from "../common"; import { TreeNode } from "./AST"; -// #if _EDITOR import { CompilationError } from "../Error"; -// #endif import { ShaderData } from "./ShaderInfo"; import { SymbolInfo, SymbolTable } from "../parser/symbolTable"; import { NodeChild } from "./types"; import { SymbolTableStack } from "../common/BaseSymbolTable"; -import { Logger } from "@galacean/engine"; +import { ShaderLab } from "../ShaderLab"; export type TranslationRule = (sa: SematicAnalyzer, ...tokens: NodeChild[]) => T; @@ -60,9 +58,7 @@ export default class SematicAnalyzer { } error(loc: ShaderRange, ...param: any[]) { - // Logger.warn(loc, ...param); - - const err = new CompilationError(param.join(""), loc); + const err = new CompilationError(param.join(""), loc, ShaderLab._processingPassText); this.errors.push(err); return err; } diff --git a/packages/shader-lab/src/parser/ShaderTargetParser.ts b/packages/shader-lab/src/parser/ShaderTargetParser.ts index 89d268af36..f12c8b27d4 100644 --- a/packages/shader-lab/src/parser/ShaderTargetParser.ts +++ b/packages/shader-lab/src/parser/ShaderTargetParser.ts @@ -11,6 +11,7 @@ import { LALR1 } from "../lalr"; import { ParserUtils } from "../Utils"; import { Logger } from "@galacean/engine"; import { CompilationError } from "../Error"; +import { ShaderLab } from "../ShaderLab"; /** * The syntax parser and sematic analyzer of `ShaderLab` compiler @@ -109,7 +110,9 @@ export class ShaderTargetParser { traceBackStack.push(nextState); continue; } else { - this.sematicAnalyzer.errors.push(new CompilationError(`Unexpected token ${token.lexeme}`, token.location)); + this.sematicAnalyzer.errors.push( + new CompilationError(`Unexpected token ${token.lexeme}`, token.location, ShaderLab._processingPassText) + ); return null; } } diff --git a/packages/shader-lab/src/preprocessor/MacroDefine.ts b/packages/shader-lab/src/preprocessor/MacroDefine.ts index e2cca2fa8b..8149799287 100644 --- a/packages/shader-lab/src/preprocessor/MacroDefine.ts +++ b/packages/shader-lab/src/preprocessor/MacroDefine.ts @@ -1,6 +1,6 @@ import { BaseToken } from "../common/BaseToken"; import { ShaderRange } from "../common"; -import { ParserUtils } from "../Utils"; +import { PreprocessorError } from "../Error"; export class MacroDefine { readonly location?: ShaderRange; @@ -29,7 +29,7 @@ export class MacroDefine { // #if _EDITOR if (args.length !== this.args?.length) { - ParserUtils.throw(this.location, "mismatched function macro"); + throw new PreprocessorError("mismatched function macro", this.location, ""); } // #endif const replaceRegex = new RegExp(`\\b(${argsTextList.join("|")})\\b`, "g"); diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 77df9593ac..79e7664641 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -103,8 +103,8 @@ export class PpParser { return this._expandSegmentsStack[this._expandSegmentsStack.length - 1]; } - private static reportError(loc: ShaderRange | ShaderPosition, message: string) { - this._errors.push(new PreprocessorError(message, loc)); + private static reportError(loc: ShaderRange | ShaderPosition, message: string, source: string, file: string) { + this._errors.push(new PreprocessorError(message, loc, source, file)); } private static _parseInclude(scanner: PpScanner) { @@ -126,7 +126,7 @@ export class PpParser { const end = scanner.getShaderPosition(); const chunk = this._includeMap[includedPath]; if (!chunk) { - this.reportError(id.location, `Shader slice "${includedPath}" not founded.`); + this.reportError(id.location, `Shader slice "${includedPath}" not founded.`, scanner.source, scanner.file); return; } @@ -301,7 +301,7 @@ export class PpParser { scanner.skipSpace(false); const operand2 = this._parseRelationalExpression(scanner) as number; if (typeof operand1 !== typeof operand2 && typeof operand1 !== "number") { - this.reportError(opPos, "invalid operator in relation expression."); + this.reportError(opPos, "invalid operator in relation expression.", scanner.source, scanner.file); return; } switch (operator) { @@ -327,7 +327,7 @@ export class PpParser { scanner.skipSpace(false); const operand2 = this._parseShiftExpression(scanner) as number; if (typeof operand1 !== typeof operand2 && typeof operand1 !== "number") { - this.reportError(opPos, "invalid operator in shift expression."); + this.reportError(opPos, "invalid operator in shift expression.", scanner.source, scanner.file); return; } switch (operator) { @@ -351,7 +351,7 @@ export class PpParser { scanner.skipSpace(false); const operand2 = this._parseAdditiveExpression(scanner) as number; if (typeof operand1 !== typeof operand2 && typeof operand1 !== "number") { - this.reportError(opPos, "invalid operator."); + this.reportError(opPos, "invalid operator.", scanner.source, scanner.file); return false; } switch (operator) { @@ -373,7 +373,7 @@ export class PpParser { scanner.skipSpace(false); const operand2 = this._parseMulticativeExpression(scanner) as number; if (typeof operand1 !== typeof operand2 && typeof operand1 !== "number") { - this.reportError(opPos, "invalid operator."); + this.reportError(opPos, "invalid operator.", scanner.source, scanner.file); return; } switch (operator) { @@ -395,7 +395,7 @@ export class PpParser { const opPos = scanner.getShaderPosition(); const parenExpr = this._parseParenthesisExpression(scanner); if ((operator === "!" && typeof parenExpr !== "boolean") || (operator !== "!" && typeof parenExpr !== "number")) { - this.reportError(opPos, "invalid operator."); + this.reportError(opPos, "invalid operator.", scanner.source, scanner.file); } switch (operator) { @@ -440,11 +440,11 @@ export class PpParser { return false; } if (macro.isFunction) { - this.reportError(id.location, "invalid function macro usage"); + this.reportError(id.location, "invalid function macro usage", scanner.source, scanner.file); } const value = Number(macro.body.lexeme); if (!Number.isInteger(value)) { - this.reportError(id.location, `invalid const macro: ${id.lexeme}`); + this.reportError(id.location, `invalid const macro: ${id.lexeme}`, scanner.source, scanner.file); } this._branchMacros.add(id.lexeme); return value; @@ -453,7 +453,12 @@ export class PpParser { const integer = scanner.scanInteger(); return Number(integer.lexeme); } else { - this.reportError(scanner.getShaderPosition(), `invalid token: ${scanner.getCurChar()}`); + this.reportError( + scanner.getShaderPosition(), + `invalid token: ${scanner.getCurChar()}`, + scanner.source, + scanner.file + ); } } @@ -590,7 +595,7 @@ export class PpParser { let end = macro.location.end; if (this._definedMacros.get(macro.lexeme) && macro.lexeme.startsWith("GL_")) { - this.reportError(macro.location, `redefined macro: ${macro.lexeme}`); + this.reportError(macro.location, `redefined macro: ${macro.lexeme}`, scanner.source, scanner.file); } let macroArgs: BaseToken[] | undefined; diff --git a/packages/shader-lab/src/preprocessor/PpScanner.ts b/packages/shader-lab/src/preprocessor/PpScanner.ts index b2f56c8b37..33a6f7ea30 100644 --- a/packages/shader-lab/src/preprocessor/PpScanner.ts +++ b/packages/shader-lab/src/preprocessor/PpScanner.ts @@ -87,7 +87,7 @@ export default class PpScanner extends BaseScanner { const end = this._currentIndex; const word = this._source.slice(start, end); if (end === start) { - ParserUtils.throw(this.getShaderPosition(), "no word found."); + this.throwError(this.getShaderPosition(), "no word found."); } const kw = PpKeyword.get(word); if (kw) { @@ -141,14 +141,14 @@ export default class PpScanner extends BaseScanner { scanQuotedString(): BaseToken { this.skipSpace(true); if (this.getCurChar() !== '"') { - ParserUtils.throw(this.getShaderPosition(), "unexpected char, expected '\"'"); + this.throwError(this.getShaderPosition(), "unexpected char, expected '\"'"); } const ShaderPosition = this.getShaderPosition(); this._advance(); const start = this._currentIndex; while (this.getCurChar() !== '"' && !this.isEnd()) this._advance(); if (this.isEnd()) { - ParserUtils.throw(this.getShaderPosition(), "unexpected char, expected '\"'"); + this.throwError(this.getShaderPosition(), "unexpected char, expected '\"'"); } const word = this._source.slice(start, this._currentIndex); @@ -230,7 +230,7 @@ export default class PpScanner extends BaseScanner { this.advance(); } if (this._currentIndex === start) { - ParserUtils.throw(this.getShaderPosition(), "no integer found"); + this.throwError(this.getShaderPosition(), "no integer found"); } const integer = this._source.slice(start, this._currentIndex); From f12b8f7cce23a5e48ceb0935049f5e9c369753d1 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 09:43:17 +0800 Subject: [PATCH 047/131] feat(shaderlab): multi package --- packages/shader-lab/src/Error.ts | 4 ++++ packages/shader-lab/src/lexer/Lexer.ts | 2 -- .../shader-lab/src/preprocessor/PpScanner.ts | 1 - rollup.config.js | 22 +++++++++---------- tests/src/shader-lab/Preprocessor.test.ts | 2 +- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/packages/shader-lab/src/Error.ts b/packages/shader-lab/src/Error.ts index 0d71c26375..bd4c4aa127 100644 --- a/packages/shader-lab/src/Error.ts +++ b/packages/shader-lab/src/Error.ts @@ -23,6 +23,7 @@ export abstract class GSError extends Error { log(_source?: string): void { if (!Logger.enable) return; + // #if _EDITOR const logger = this.level === ErrorLevel.ERROR ? Logger.error : Logger.warn; let start: ShaderPosition, end: ShaderPosition; @@ -57,6 +58,9 @@ export abstract class GSError extends Error { } logger(diagnosticMessage); + // #else + Logger.error("compile error."); + // #endif } } diff --git a/packages/shader-lab/src/lexer/Lexer.ts b/packages/shader-lab/src/lexer/Lexer.ts index 84dc9dd08c..003fa459c7 100644 --- a/packages/shader-lab/src/lexer/Lexer.ts +++ b/packages/shader-lab/src/lexer/Lexer.ts @@ -12,9 +12,7 @@ export class Lexer extends BaseScanner { reset(source: string) { this._source = source; this._currentIndex = 0; - // #if _EDITOR this._line = this._column = 0; - // #endif } *tokenize() { diff --git a/packages/shader-lab/src/preprocessor/PpScanner.ts b/packages/shader-lab/src/preprocessor/PpScanner.ts index 33a6f7ea30..d2a62994b6 100644 --- a/packages/shader-lab/src/preprocessor/PpScanner.ts +++ b/packages/shader-lab/src/preprocessor/PpScanner.ts @@ -5,7 +5,6 @@ import PpSourceMap from "./sourceMap"; // #endif import BaseScanner from "../common/BaseScanner"; import { BaseToken, EOF } from "../common/BaseToken"; -import { ParserUtils } from "../Utils"; import { EPpKeyword, EPpToken, PpKeyword } from "./constants"; import { PpUtils } from "./Utils"; import { ShaderLab } from "../ShaderLab"; diff --git a/rollup.config.js b/rollup.config.js index eb6a20e866..447dfe84ee 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -61,14 +61,16 @@ const commonPlugins = [ function config({ location, pkgJson, editorMode }) { const input = path.join(location, "src", "index.ts"); const dependencies = Object.assign({}, pkgJson.dependencies ?? {}, pkgJson.peerDependencies ?? {}); - commonPlugins.push( + const curPlugins = Array.from(commonPlugins); + + curPlugins.push( jscc({ - values: { _EDITOR: NODE_ENV !== "release" || editorMode } + values: { _EDITOR: editorMode } }) ); const external = Object.keys(dependencies); - commonPlugins.push( + curPlugins.push( replace({ preventAssignment: true, __buildVersion: pkgJson.version @@ -80,9 +82,8 @@ function config({ location, pkgJson, editorMode }) { const umdConfig = pkgJson.umd; let file = path.join(location, "dist", "browser.js"); - const plugins = commonPlugins; if (compress) { - plugins.push(minify({ sourceMap: true })); + curPlugins.push(minify({ sourceMap: true })); file = path.join(location, "dist", "browser.min.js"); } if (editorMode) { @@ -103,12 +104,12 @@ function config({ location, pkgJson, editorMode }) { globals: umdConfig.globals } ], - plugins + plugins: curPlugins }; }, mini: () => { let file = path.join(location, "dist", "miniprogram.js"); - const plugins = [...commonPlugins, ...miniProgramPlugin]; + const plugins = [...curPlugins, ...miniProgramPlugin]; if (editorMode) { file = path.join(location, "dist", "miniprogram.editor.js"); } @@ -126,9 +127,8 @@ function config({ location, pkgJson, editorMode }) { }; }, module: () => { - const plugins = commonPlugins; - let esFile = pkgJson.module; - let mainFile = pkgJson.main; + let esFile = path.join(location, pkgJson.module); + let mainFile = path.join(location, pkgJson.main); if (editorMode) { esFile = path.join(location, "dist", "module.editor.js"); mainFile = path.join(location, "dist", "main.editor.js"); @@ -148,7 +148,7 @@ function config({ location, pkgJson, editorMode }) { format: "commonjs" } ], - plugins + plugins: curPlugins }; } }; diff --git a/tests/src/shader-lab/Preprocessor.test.ts b/tests/src/shader-lab/Preprocessor.test.ts index 315f857c63..87e87471b2 100644 --- a/tests/src/shader-lab/Preprocessor.test.ts +++ b/tests/src/shader-lab/Preprocessor.test.ts @@ -3,7 +3,7 @@ import { testCaseList } from "./test-case"; import { ShaderLib } from "@galacean/engine-core"; import { expect } from "chai"; import { readFileSync } from "fs"; -import { Preprocessor } from "@galacean/engine-shader-lab"; +import { Preprocessor } from "@galacean/engine-shader-lab/dist/main.editor"; import { join } from "path"; const includedSource = readFileSync(join(__dirname, "test-case/included.txt")).toString(); From c32969b7812012e1b98a761217f0978a99364a5c Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 11:27:58 +0800 Subject: [PATCH 048/131] feat(shaderlab): opt code --- packages/shader-lab/README.md | 13 +++++ packages/shader-lab/src/Error.ts | 2 + packages/shader-lab/src/ShaderLab.ts | 9 +++- .../shader-lab/src/codeGen/CodeGenVisitor.ts | 34 +++++++++++-- .../shader-lab/src/codeGen/GLESVisitor.ts | 21 ++------ .../shader-lab/src/codeGen/VisitorContext.ts | 49 +++++++++++++------ packages/shader-lab/src/common/BaseScanner.ts | 12 +++-- .../src/contentParser/ShaderContentParser.ts | 11 +++-- .../shader-lab/src/parser/SemanticAnalyzer.ts | 10 +++- .../src/parser/ShaderTargetParser.ts | 7 +++ packages/shader-lab/src/parser/TargetParser.y | 2 +- .../shader-lab/src/preprocessor/PpParser.ts | 8 ++- 12 files changed, 129 insertions(+), 49 deletions(-) diff --git a/packages/shader-lab/README.md b/packages/shader-lab/README.md index 6a6b323460..97c7bf8da4 100644 --- a/packages/shader-lab/README.md +++ b/packages/shader-lab/README.md @@ -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"; +``` + ## 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. diff --git a/packages/shader-lab/src/Error.ts b/packages/shader-lab/src/Error.ts index bd4c4aa127..4936486b17 100644 --- a/packages/shader-lab/src/Error.ts +++ b/packages/shader-lab/src/Error.ts @@ -64,6 +64,7 @@ export abstract class GSError extends Error { } } +// #if _EDITOR export class PreprocessorError extends GSError { constructor(message: string, loc: ShaderRange | ShaderPosition, source: string, file?: string, cause?: Error) { super(message, loc, source, file, cause); @@ -84,3 +85,4 @@ export class ScannerError extends GSError { this.name = "ScannerError"; } } +// #endif diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index 19b5bf44cc..f1905ea92b 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -5,7 +5,7 @@ import { GLES100Visitor, GLES300Visitor } from "./codeGen"; import { IShaderContent, IShaderLab } from "@galacean/engine-design"; import { ShaderContentParser } from "./contentParser"; // @ts-ignore -import { Logger, ShaderLib, ShaderMacro, ShaderPass, ShaderPlatformTarget, ShaderProgram } from "@galacean/engine"; +import { Logger, ShaderLib, ShaderMacro, ShaderPass, ShaderPlatformTarget } from "@galacean/engine"; import { ShaderPosition, ShaderRange } from "./common"; import { ShaderLabObjectPool } from "./ShaderLabObjectPool"; import { GSError } from "./Error"; @@ -65,7 +65,7 @@ export class ShaderLab implements IShaderLab { const ppdContent = Preprocessor.process(source); if (PpParser._errors.length > 0) { for (const err of PpParser._errors) { - this._errors.push(err); + this._errors.push(err); } this._logErrors(this._errors); return { vertex: "", fragment: "" }; @@ -143,9 +143,14 @@ export class ShaderLab implements IShaderLab { // #endif private _logErrors(errors: GSError[], source?: string) { + // #if !_EDITOR + Logger.error(`${errors.length} errors occur!`); + // #else if (!Logger.isEnabled) return; for (const err of errors) { + if (!err.log) debugger; err.log(source); } + // #endif } } diff --git a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts index a79b12a755..534a1473bc 100644 --- a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts +++ b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts @@ -1,16 +1,27 @@ import { ENonTerminal } from "../parser/GrammarSymbol"; import { BaseToken as Token } from "../common/BaseToken"; -import { EKeyword } from "../common"; +import { EKeyword, ShaderPosition, ShaderRange } from "../common"; import { ASTNode, TreeNode } from "../parser/AST"; import { ESymbolType, FnSymbol, VarSymbol } from "../parser/symbolTable"; import { ParserUtils } from "../Utils"; import { NodeChild } from "../parser/types"; import { VisitorContext } from "./VisitorContext"; +// #if _EDITOR +import { CompilationError, GSError } from "../Error"; +// #endif +import { ShaderLab } from "../ShaderLab"; /** + * @internal * The code generator */ export class CodeGenVisitor { + protected _errors: GSError[] = []; + + get errors() { + return this._errors; + } + defaultCodeGen(children: NodeChild[]) { let ret: string[] = []; for (const child of children) { @@ -33,10 +44,16 @@ export class CodeGenVisitor { if (prop instanceof Token) { if (context.isAttributeStruct(postExpr.type)) { - context.referenceAttribute(prop.lexeme); + const error = context.referenceAttribute(prop); + if (error) { + this._errors.push(error); + } return prop.lexeme; } else if (context.isVaryingStruct(postExpr.type)) { - context.referenceVarying(prop.lexeme); + const error = context.referenceVarying(prop); + if (error) { + this._errors.push(error); + } return prop.lexeme; } @@ -170,4 +187,15 @@ export class CodeGenVisitor { visitFunctionIdentifier(node: ASTNode.FunctionIdentifier): string { return this.defaultCodeGen(node.children); } + + protected reportError(loc: ShaderRange | ShaderPosition, message: string): CompilationError { + let error: Error; + // #if _EDITOR + error = new CompilationError(message, loc, ShaderLab._processingPassText); + // #else + error = new Error(message); + // #endif + this._errors.push(error); + return error; + } } diff --git a/packages/shader-lab/src/codeGen/GLESVisitor.ts b/packages/shader-lab/src/codeGen/GLESVisitor.ts index 6ed40663dc..55e39efacf 100644 --- a/packages/shader-lab/src/codeGen/GLESVisitor.ts +++ b/packages/shader-lab/src/codeGen/GLESVisitor.ts @@ -6,9 +6,6 @@ import { EShaderStage } from "../common/Enums"; import { IShaderInfo } from "@galacean/engine-design"; import { ICodeSegment } from "./types"; import { VisitorContext } from "./VisitorContext"; -import { CompilationError, GSError } from "../Error"; -import { ShaderPosition, ShaderRange } from "../common"; -import { ShaderLab } from "../ShaderLab"; const defaultPrecision = ` #ifdef GL_FRAGMENT_PRECISION_HIGH @@ -20,22 +17,16 @@ const defaultPrecision = ` #endif `; +/** + * @internal + */ export abstract class GLESVisitor extends CodeGenVisitor { protected _versionText: string = ""; protected _extensions: string = ""; - private _errors: GSError[] = []; - abstract getAttributeDeclare(): ICodeSegment[]; abstract getVaryingDeclare(): ICodeSegment[]; - /** - * @internal - */ - get errors() { - return this._errors; - } - visitShaderProgram(node: ASTNode.GLShaderProgram, vertexEntry: string, fragmentEntry: string): IShaderInfo { this._errors.length = 0; VisitorContext.reset(); @@ -159,10 +150,4 @@ export abstract class GLESVisitor extends CodeGenVisitor { } return this._getGlobalText(data, textList, lastLength, _serialized); } - - private reportError(loc: ShaderRange | ShaderPosition, message: string): CompilationError { - const error = new CompilationError(message, loc, ShaderLab._processingPassText); - this._errors.push(error); - return error; - } } diff --git a/packages/shader-lab/src/codeGen/VisitorContext.ts b/packages/shader-lab/src/codeGen/VisitorContext.ts index f537450227..d96f42759c 100644 --- a/packages/shader-lab/src/codeGen/VisitorContext.ts +++ b/packages/shader-lab/src/codeGen/VisitorContext.ts @@ -3,7 +3,11 @@ import { EShaderStage } from "../common/Enums"; import { ASTNode } from "../parser/AST"; import { ESymbolType, SymbolTable, SymbolInfo } from "../parser/symbolTable"; import { IParamInfo } from "../parser/types"; +import { CompilationError, GSError } from "../Error"; +import { BaseToken } from "../common/BaseToken"; +import { ShaderLab } from "../ShaderLab"; +/** @internal */ export class VisitorContext { private static _singleton: VisitorContext; static get context() { @@ -30,12 +34,13 @@ export class VisitorContext { _curFn?: ASTNode.FunctionProtoType; _passSymbolTable: SymbolTable; + + private constructor() {} + get passSymbolTable() { return this._passSymbolTable; } - private constructor() {} - reset() { this.attributeList.length = 0; this.attributeStructs.length = 0; @@ -52,26 +57,42 @@ export class VisitorContext { return this.varyingStruct?.ident?.lexeme === type; } - referenceAttribute(ident: string) { - if (this._referencedAttributeList[ident]) return; + referenceAttribute(ident: BaseToken): GSError { + if (this._referencedAttributeList[ident.lexeme]) return; - const prop = this.attributeList.find((item) => item.ident.lexeme === ident); + const prop = this.attributeList.find((item) => item.ident.lexeme === ident.lexeme); if (!prop) { - Logger.error("referenced attribute not found:", ident); - return; + // #if _EDITOR + return new CompilationError( + `referenced attribute not found: ${ident.lexeme}`, + ident.location, + ShaderLab._processingPassText + ); + // #else + // @ts-ignore + return new Error(`referenced attribute not found: ${ident.lexeme}`); + // #endif } - this._referencedAttributeList[ident] = prop; + this._referencedAttributeList[ident.lexeme] = prop; } - referenceVarying(ident: string) { - if (this._referencedVaryingList[ident]) return; + referenceVarying(ident: BaseToken): CompilationError | undefined { + if (this._referencedVaryingList[ident.lexeme]) return; - const prop = this.varyingStruct?.propList.find((item) => item.ident.lexeme === ident); + const prop = this.varyingStruct?.propList.find((item) => item.ident.lexeme === ident.lexeme); if (!prop) { - Logger.error("referenced varying not found:", ident); - return; + // #if _EDITOR + return new CompilationError( + `referenced varying not found: ${ident.lexeme}`, + ident.location, + ShaderLab._processingPassText + ); + // #else + // @ts-ignore + return new Error(`referenced varying not found: ${ident.lexeme}`); + // #endif } - this._referencedVaryingList[ident] = prop; + this._referencedVaryingList[ident.lexeme] = prop; } referenceGlobal(ident: string, type: ESymbolType) { diff --git a/packages/shader-lab/src/common/BaseScanner.ts b/packages/shader-lab/src/common/BaseScanner.ts index c979c412a4..c1d897970f 100644 --- a/packages/shader-lab/src/common/BaseScanner.ts +++ b/packages/shader-lab/src/common/BaseScanner.ts @@ -1,5 +1,7 @@ import { ETokenType, ShaderRange, ShaderPosition } from "."; +// #if _EDITOR import { ScannerError } from "../Error"; +// #endif import { ShaderLab } from "../ShaderLab"; import { BaseToken } from "./BaseToken"; @@ -21,10 +23,8 @@ export default class BaseScanner { protected _currentIndex = 0; protected _source: string; - // #if _EDITOR protected _column = 0; protected _line = 0; - // #endif get current(): number { return this._currentIndex; @@ -129,9 +129,11 @@ export default class BaseScanner { } throwError(pos: ShaderPosition | ShaderRange, ...msgs: any[]) { - const error = new ScannerError(msgs.join(" "), pos, this._source); - error.log(); - throw error; + // #if _EDITOR + throw new ScannerError(msgs.join(" "), pos, this._source); + // #else + throw new Error(msgs.join("")); + // #endif } scanPairedText(left: string, right: string, balanced = false, skipLeading = false) { diff --git a/packages/shader-lab/src/contentParser/ShaderContentParser.ts b/packages/shader-lab/src/contentParser/ShaderContentParser.ts index fbc5e181df..7c22162971 100644 --- a/packages/shader-lab/src/contentParser/ShaderContentParser.ts +++ b/packages/shader-lab/src/contentParser/ShaderContentParser.ts @@ -23,7 +23,9 @@ import { IShaderPassContent, IRenderStates } from "@galacean/engine-design"; +// #if _EDITOR import { CompilationError } from "../Error"; +// #endif const EngineType = [ EKeyword.GS_RenderQueueType, @@ -173,13 +175,16 @@ export class ShaderContentParser { scanner.scanText(";"); const sm = this._symbolTable.lookup({ type: stateToken.type, ident: variable.lexeme }); if (!sm?.value) { - const error = new CompilationError( + let error: CompilationError; + // #if _EDITOR + throw new CompilationError( `Invalid ${stateToken.lexeme} variable: ${variable.lexeme}`, scanner.curPosition, scanner.source ); - error.log(); - throw error; + // #else + throw new Error(`Invalid ${stateToken.lexeme} variable: ${variable.lexeme}`); + // #endif } const renderState = sm.value as IRenderStates; Object.assign(ret.renderStates.constantMap, renderState.constantMap); diff --git a/packages/shader-lab/src/parser/SemanticAnalyzer.ts b/packages/shader-lab/src/parser/SemanticAnalyzer.ts index 691341026d..8972a21411 100644 --- a/packages/shader-lab/src/parser/SemanticAnalyzer.ts +++ b/packages/shader-lab/src/parser/SemanticAnalyzer.ts @@ -1,6 +1,8 @@ import { ShaderRange } from "../common"; import { TreeNode } from "./AST"; +// #if _EDITOR import { CompilationError } from "../Error"; +// #endif import { ShaderData } from "./ShaderInfo"; import { SymbolInfo, SymbolTable } from "../parser/symbolTable"; import { NodeChild } from "./types"; @@ -58,7 +60,13 @@ export default class SematicAnalyzer { } error(loc: ShaderRange, ...param: any[]) { - const err = new CompilationError(param.join(""), loc, ShaderLab._processingPassText); + let err: CompilationError; + // #if !_EDITOR + // @ts-ignore + err = new Error(param.join("")); + // #else + err = new CompilationError(param.join(""), loc, ShaderLab._processingPassText); + // #endif this.errors.push(err); return err; } diff --git a/packages/shader-lab/src/parser/ShaderTargetParser.ts b/packages/shader-lab/src/parser/ShaderTargetParser.ts index f12c8b27d4..8a737d4025 100644 --- a/packages/shader-lab/src/parser/ShaderTargetParser.ts +++ b/packages/shader-lab/src/parser/ShaderTargetParser.ts @@ -10,7 +10,9 @@ import { addTranslationRule, createGrammar } from "../lalr/CFG"; import { LALR1 } from "../lalr"; import { ParserUtils } from "../Utils"; import { Logger } from "@galacean/engine"; +// #if _EDITOR import { CompilationError } from "../Error"; +// #endif import { ShaderLab } from "../ShaderLab"; /** @@ -110,9 +112,14 @@ export class ShaderTargetParser { traceBackStack.push(nextState); continue; } else { + // #if _EDITOR this.sematicAnalyzer.errors.push( new CompilationError(`Unexpected token ${token.lexeme}`, token.location, ShaderLab._processingPassText) ); + // #else + // @ts-ignore + this.sematicAnalyzer.errors.push(new Error(`Unexpected token ${token.lexeme}`)); + // #endif return null; } } diff --git a/packages/shader-lab/src/parser/TargetParser.y b/packages/shader-lab/src/parser/TargetParser.y index 17c9b00013..0d943c24be 100644 --- a/packages/shader-lab/src/parser/TargetParser.y +++ b/packages/shader-lab/src/parser/TargetParser.y @@ -432,7 +432,7 @@ expression_statement: // Dangling else ambiguity selection_statement: IF '(' expression ')' statement - | IF '(' expression ')' statement ELSE statement + | IF '(' expression ')' statement ELSE statement ELSE ; iteration_statement: diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 79e7664641..5225623edc 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -3,6 +3,7 @@ import LexerUtils from "../lexer/Utils"; import { MacroDefine } from "./MacroDefine"; // #if _EDITOR import PpSourceMap, { BlockInfo } from "./sourceMap"; +import { PreprocessorError } from "../Error"; // #endif import { BaseToken } from "../common/BaseToken"; import { EPpKeyword, EPpToken, PpConstant } from "./constants"; @@ -10,7 +11,6 @@ import PpScanner from "./PpScanner"; import { PpUtils } from "./Utils"; import { ShaderLab } from "../ShaderLab"; import { ShaderPass } from "@galacean/engine"; -import { PreprocessorError } from "../Error"; export interface ExpandSegment { // #if _EDITOR @@ -32,7 +32,7 @@ export class PpParser { private static _basePathForIncludeKey: string; /** @internal */ - static _errors: PreprocessorError[] = []; + static _errors: Error[] = []; /** @internal */ static _scanningText: string; /** @internal */ @@ -104,7 +104,11 @@ export class PpParser { } private static reportError(loc: ShaderRange | ShaderPosition, message: string, source: string, file: string) { + // #if _EDITOR this._errors.push(new PreprocessorError(message, loc, source, file)); + // #else + this._errors.push(new Error(message)); + // #endif } private static _parseInclude(scanner: PpScanner) { From 9d4d808455e79a0d54a9539ab0d477337e2615b8 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 11:34:00 +0800 Subject: [PATCH 049/131] feat(shaderlab): opt code --- package.json | 4 ---- 1 file changed, 4 deletions(-) diff --git a/package.json b/package.json index 2f5c761e16..4f775522ab 100644 --- a/package.json +++ b/package.json @@ -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", From f5edeb3c626905bb02aa0f98f80ccc99f35a2815 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 11:38:05 +0800 Subject: [PATCH 050/131] feat(shaderlab): opt code --- .github/workflows/ci.yml | 6 +++--- packages/shader-lab/package.json | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2aa897a514..a86c651de5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,7 +50,7 @@ jobs: - name: Install run: pnpm install - - run: npm run build:editor + - run: npm run build codecov: runs-on: macos-latest @@ -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 @@ -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 diff --git a/packages/shader-lab/package.json b/packages/shader-lab/package.json index 9803e55f33..f521adade1 100644 --- a/packages/shader-lab/package.json +++ b/packages/shader-lab/package.json @@ -9,7 +9,6 @@ "main": "dist/main.js", "module": "dist/module.js", "browser": "dist/browser.min.js", - "editor": "dist/editor.js", "debug": "src/index.ts", "types": "types/index.d.ts", "scripts": { From 68e1556fe764425c72418b195f13211c5277a246 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 11:42:07 +0800 Subject: [PATCH 051/131] feat(shaderlab): opt code --- packages/shader-lab/src/Error.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/packages/shader-lab/src/Error.ts b/packages/shader-lab/src/Error.ts index 4936486b17..a1d5ccfff7 100644 --- a/packages/shader-lab/src/Error.ts +++ b/packages/shader-lab/src/Error.ts @@ -1,18 +1,12 @@ import { Logger } from "@galacean/engine"; import { ShaderPosition, ShaderRange } from "./common"; -export enum ErrorLevel { - ERROR = 0, - WARN -} - export abstract class GSError extends Error { static wrappingLineCount = 2; readonly loc: ShaderRange | ShaderPosition; readonly source: string; readonly file?: string; - level = ErrorLevel.ERROR; constructor(message: string, loc: ShaderRange | ShaderPosition, source: string, file?: string, cause?: Error) { super(message, { cause }); @@ -24,14 +18,12 @@ export abstract class GSError extends Error { log(_source?: string): void { if (!Logger.enable) return; // #if _EDITOR - const logger = this.level === ErrorLevel.ERROR ? Logger.error : Logger.warn; - let start: ShaderPosition, end: ShaderPosition; const { message, loc, source: originSource } = this; let source = originSource; if (_source) source = _source; if (!source) { - logger(message); + Logger.error(message); } if (loc instanceof ShaderPosition) { @@ -57,7 +49,7 @@ export abstract class GSError extends Error { } } - logger(diagnosticMessage); + Logger.error(diagnosticMessage); // #else Logger.error("compile error."); // #endif From dd4777c8f1cc4a1c25d6eefb6b4d54a6e89f9a6c Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 12:20:23 +0800 Subject: [PATCH 052/131] feat(shaderlab): test case --- packages/shader-lab/src/Error.ts | 6 +----- packages/shader-lab/src/ShaderLab.ts | 12 ++++++++---- packages/shader-lab/src/codeGen/GLES100.ts | 1 - packages/shader-lab/src/codeGen/VisitorContext.ts | 3 ++- packages/shader-lab/src/index.ts | 1 + 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/shader-lab/src/Error.ts b/packages/shader-lab/src/Error.ts index a1d5ccfff7..77b9bc4e08 100644 --- a/packages/shader-lab/src/Error.ts +++ b/packages/shader-lab/src/Error.ts @@ -1,3 +1,4 @@ +// #if _EDITOR import { Logger } from "@galacean/engine"; import { ShaderPosition, ShaderRange } from "./common"; @@ -17,7 +18,6 @@ export abstract class GSError extends Error { log(_source?: string): void { if (!Logger.enable) return; - // #if _EDITOR let start: ShaderPosition, end: ShaderPosition; const { message, loc, source: originSource } = this; let source = originSource; @@ -50,13 +50,9 @@ export abstract class GSError extends Error { } Logger.error(diagnosticMessage); - // #else - Logger.error("compile error."); - // #endif } } -// #if _EDITOR export class PreprocessorError extends GSError { constructor(message: string, loc: ShaderRange | ShaderPosition, source: string, file?: string, cause?: Error) { super(message, loc, source, file, cause); diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index f1905ea92b..8a7aba71e4 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -8,7 +8,9 @@ import { ShaderContentParser } from "./contentParser"; 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 { @@ -116,8 +118,8 @@ export class ShaderLab implements IShaderLab { */ _parse( shaderSource: string, - macros: ShaderMacro[], - backend: ShaderPlatformTarget + macros: ShaderMacro[] = [], + backend: ShaderPlatformTarget = ShaderPlatformTarget.GLES100 ): (ReturnType & { name: string })[] { const structInfo = this._parseShaderContent(shaderSource); const passResult = [] as any; @@ -142,13 +144,15 @@ export class ShaderLab implements IShaderLab { } // #endif - private _logErrors(errors: GSError[], source?: string) { + /** + * @internal + */ + _logErrors(errors: GSError[], source?: string) { // #if !_EDITOR Logger.error(`${errors.length} errors occur!`); // #else if (!Logger.isEnabled) return; for (const err of errors) { - if (!err.log) debugger; err.log(source); } // #endif diff --git a/packages/shader-lab/src/codeGen/GLES100.ts b/packages/shader-lab/src/codeGen/GLES100.ts index e590e63c9f..fdfd225f71 100644 --- a/packages/shader-lab/src/codeGen/GLES100.ts +++ b/packages/shader-lab/src/codeGen/GLES100.ts @@ -1,4 +1,3 @@ -import { CodeGenVisitor } from "./CodeGenVisitor"; import { GLESVisitor } from "./GLESVisitor"; import { VisitorContext } from "./VisitorContext"; import { ICodeSegment } from "./types"; diff --git a/packages/shader-lab/src/codeGen/VisitorContext.ts b/packages/shader-lab/src/codeGen/VisitorContext.ts index d96f42759c..8eeb95a5fb 100644 --- a/packages/shader-lab/src/codeGen/VisitorContext.ts +++ b/packages/shader-lab/src/codeGen/VisitorContext.ts @@ -1,9 +1,10 @@ -import { Logger } from "@galacean/engine"; import { EShaderStage } from "../common/Enums"; import { ASTNode } from "../parser/AST"; import { ESymbolType, SymbolTable, SymbolInfo } from "../parser/symbolTable"; import { IParamInfo } from "../parser/types"; +// #if _EDITOR import { CompilationError, GSError } from "../Error"; +// #endif import { BaseToken } from "../common/BaseToken"; import { ShaderLab } from "../ShaderLab"; diff --git a/packages/shader-lab/src/index.ts b/packages/shader-lab/src/index.ts index 7e733a06cc..5877d39f91 100644 --- a/packages/shader-lab/src/index.ts +++ b/packages/shader-lab/src/index.ts @@ -2,6 +2,7 @@ export { ShaderLab } from "./ShaderLab"; // #if _EDITOR export { Preprocessor } from "./preprocessor"; +export * from "./Error"; // #endif //@ts-ignore From 1a918dab02092e3477c5fe8254ac77de2f39489c Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 14:03:51 +0800 Subject: [PATCH 053/131] feat: opt code --- packages/shader-lab/src/common/ShaderPosition.ts | 4 ---- packages/shader-lab/src/common/ShaderRange.ts | 4 ---- 2 files changed, 8 deletions(-) diff --git a/packages/shader-lab/src/common/ShaderPosition.ts b/packages/shader-lab/src/common/ShaderPosition.ts index e1ab88b619..f6cb2b19d0 100644 --- a/packages/shader-lab/src/common/ShaderPosition.ts +++ b/packages/shader-lab/src/common/ShaderPosition.ts @@ -16,8 +16,4 @@ export class ShaderPosition implements IPoolElement { this.line = 0; this.column = 0; } - - toString() { - return ``; - } } diff --git a/packages/shader-lab/src/common/ShaderRange.ts b/packages/shader-lab/src/common/ShaderRange.ts index 8b450e51e8..e79d4143e0 100644 --- a/packages/shader-lab/src/common/ShaderRange.ts +++ b/packages/shader-lab/src/common/ShaderRange.ts @@ -14,8 +14,4 @@ export class ShaderRange implements IPoolElement { this.start.dispose(); this.end.dispose(); } - - toString() { - return `Start: ${this.start.toString()};\nEnd: ${this.end.toString()}\n`; - } } From e4d267c81fc03fea8ebd66bb47dda4c2924cf74f Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 14:34:49 +0800 Subject: [PATCH 054/131] feat: opt code --- tests/src/shader-lab/ShaderLab.test.ts | 74 +++++++------- tests/src/shader-lab/ShaderValidate.ts | 29 +++++- .../shaders/compilation-error.shader | 98 +++++++++++++++++++ 3 files changed, 167 insertions(+), 34 deletions(-) create mode 100644 tests/src/shader-lab/shaders/compilation-error.shader diff --git a/tests/src/shader-lab/ShaderLab.test.ts b/tests/src/shader-lab/ShaderLab.test.ts index e6385ef312..9f351b5695 100644 --- a/tests/src/shader-lab/ShaderLab.test.ts +++ b/tests/src/shader-lab/ShaderLab.test.ts @@ -1,9 +1,10 @@ import { BlendOperation, CompareFunction, CullMode, RenderStateDataKey } from "@galacean/engine-core"; import { Color } from "@galacean/engine-math"; -import { ShaderLab } from "@galacean/engine-shader-lab/dist/main.editor"; -import { glslValidate } from "./ShaderValidate"; +import { ShaderLab as ShaderLabEditor, CompilationError } from "@galacean/engine-shader-lab/dist/main.editor"; +import { ShaderLab as ShaderLabRelease } from "@galacean/engine-shader-lab"; +import { glslValidate, shaderParse } from "./ShaderValidate"; -import chai, { expect } from "chai"; +import chai, { expect, assert } from "chai"; import spies from "chai-spies"; import fs from "fs"; import path from "path"; @@ -103,7 +104,8 @@ vec4 linearToGamma(vec4 linearIn){ #endif `; -const shaderLab = new ShaderLab(); +const shaderLabEditor = new ShaderLabEditor(); +const shaderLabRelease = new ShaderLabRelease(); describe("ShaderLab", () => { let shader: IShaderContent; @@ -112,7 +114,7 @@ describe("ShaderLab", () => { let pass1: IShaderContent["subShaders"][number]["passes"][number]; before(() => { - shader = shaderLab._parseShaderContent(demoShader); + shader = shaderLabEditor._parseShaderContent(demoShader); subShader = shader.subShaders[0]; passList = subShader.passes; expect(passList[0].isUsePass).to.be.true; @@ -121,7 +123,7 @@ describe("ShaderLab", () => { }); it("create shaderLab", async () => { - expect(shaderLab).not.be.null; + expect(shaderLabEditor).not.be.null; }); it("shader name", () => { @@ -180,68 +182,74 @@ describe("ShaderLab", () => { }); it("engine shader", async () => { - glslValidate(demoShader, shaderLab); + glslValidate(demoShader, shaderLabEditor); + glslValidate(demoShader, shaderLabRelease); }); it("include", () => { const demoShader = fs.readFileSync(path.join(__dirname, "shaders/unlit.shader")).toString(); - glslValidate(demoShader, shaderLab, { test_common: commonSource }); + glslValidate(demoShader, shaderLabEditor, { test_common: commonSource }); }); it("planarShadow shader", () => { const demoShader = fs.readFileSync(path.join(__dirname, "shaders/planarShadow.shader")).toString(); - glslValidate(demoShader, shaderLab); + glslValidate(demoShader, shaderLabEditor); + glslValidate(demoShader, shaderLabRelease); }); it("Empty macro shader", () => { const demoShader = fs.readFileSync(path.join(__dirname, "shaders/triangle.shader")).toString(); - glslValidate(demoShader, shaderLab); + glslValidate(demoShader, shaderLabEditor); + glslValidate(demoShader, shaderLabRelease); }); it("No frag shader args", () => { const demoShader = fs.readFileSync(path.join(__dirname, "shaders/noFragArgs.shader")).toString(); - glslValidate(demoShader, shaderLab); + glslValidate(demoShader, shaderLabEditor); + glslValidate(demoShader, shaderLabRelease); }); it("water full shader(complex)", () => { const demoShader = fs.readFileSync(path.join(__dirname, "shaders/waterfull.shader")).toString(); - glslValidate(demoShader, shaderLab); + glslValidate(demoShader, shaderLabEditor); + glslValidate(demoShader, shaderLabRelease); }); it("glass shader", () => { const demoShader = fs.readFileSync(path.join(__dirname, "shaders/glass.shader")).toString(); - glslValidate(demoShader, shaderLab); + glslValidate(demoShader, shaderLabEditor); + glslValidate(demoShader, shaderLabRelease); }); - // it("shader with duplicate name", () => { - // const demoShader = fs.readFileSync(path.join(__dirname, "shaders/glass.shader")).toString(); - // (Shader as any)._shaderLab = shaderLab; - - // const shaderInstance = Shader.create(demoShader); - // expect(shaderInstance).instanceOf(Shader); - - // const errorSpy = chai.spy.on(console, "error"); - // Shader.create(demoShader); - // expect(errorSpy).to.have.been.called.with('Shader named "Gem" already exists.'); - // shaderInstance.destroy(); - // chai.spy.restore(console, "error"); - - // const sameNameShader = Shader.create(demoShader); - // expect(sameNameShader).instanceOf(Shader); - // }); - it("template shader", () => { const demoShader = fs.readFileSync(path.join(__dirname, "shaders/template.shader")).toString(); - glslValidate(demoShader, shaderLab); + glslValidate(demoShader, shaderLabEditor); + glslValidate(demoShader, shaderLabRelease); }); it("multi-pass", () => { const shaderSource = fs.readFileSync(path.join(__dirname, "shaders/multi-pass.shader")).toString(); - glslValidate(shaderSource, shaderLab); + glslValidate(shaderSource, shaderLabEditor); + glslValidate(shaderSource, shaderLabRelease); }); it("macro-with-preprocessor", () => { const shaderSource = fs.readFileSync(path.join(__dirname, "shaders/macro-pre.shader")).toString(); - glslValidate(shaderSource, shaderLab); + glslValidate(shaderSource, shaderLabEditor); + glslValidate(shaderSource, shaderLabRelease); + }); + + it("compilation-error", () => { + const errorShader = fs.readFileSync(path.join(__dirname, "shaders/compilation-error.shader")).toString(); + // @ts-ignore + shaderLabEditor._parse(errorShader); + expect(shaderLabEditor._errors.length).to.eq(3); + assert.instanceOf(shaderLabEditor._errors[0], CompilationError); + assert.instanceOf(shaderLabEditor._errors[1], CompilationError); + assert.instanceOf(shaderLabEditor._errors[2], CompilationError); + + shaderParse.bind(shaderLabRelease)(errorShader); + // @ts-ignore + expect(shaderLabRelease._errors.length).to.eq(1); }); }); diff --git a/tests/src/shader-lab/ShaderValidate.ts b/tests/src/shader-lab/ShaderValidate.ts index adf297ed42..82d96fcf70 100644 --- a/tests/src/shader-lab/ShaderValidate.ts +++ b/tests/src/shader-lab/ShaderValidate.ts @@ -1,6 +1,6 @@ import { expect } from "chai"; import { ShaderLab } from "@galacean/engine-shader-lab"; -import { Shader, ShaderFactory, ShaderPass, ShaderPlatformTarget } from "@galacean/engine-core"; +import { Shader, ShaderFactory, ShaderPass, ShaderPlatformTarget, ShaderMacro } from "@galacean/engine-core"; import { IShaderContent } from "@galacean/engine-design/src/shader-lab"; function addLineNum(str: string) { @@ -94,3 +94,30 @@ export function glslValidate(shaderSource, _shaderLab?: ShaderLab, includeMap = }); }); } + +export function shaderParse( + shaderSource: string, + macros: ShaderMacro[] = [], + backend: ShaderPlatformTarget = ShaderPlatformTarget.GLES100 +): (ReturnType & { 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); + } + } + return passResult; +} diff --git a/tests/src/shader-lab/shaders/compilation-error.shader b/tests/src/shader-lab/shaders/compilation-error.shader new file mode 100644 index 0000000000..2f5f66fed5 --- /dev/null +++ b/tests/src/shader-lab/shaders/compilation-error.shader @@ -0,0 +1,98 @@ +Shader "custom/pbr" { + EditorProperties { + material_BaseColor("Main Color", Color) = (0, 0, 0, 1); + material_AlphaCutoff("Alpha Cutoff", Range(0, 1, 0.01)) = 0; + material_BaseTexture("Texture", Texture2D); + } + + EditorMacros { + Header("Conditional Macors") { + MATERIAL_HAS_BASETEXTURE("Base Texture"); + MATERIAL_IS_ALPHA_CUTOFF("Alpha Cutoff"); + MATERIAL_IS_TRANSPARENT("Transparent"); + } + } + + SubShader "Default" { + Pass "Pass0" { + + #ifdef MATERIAL_IS_TRANSPARENT + BlendState { + Enabled = true; + SourceColorBlendFactor = BlendFactor.SourceAlpha; + DestinationColorBlendFactor = BlendFactor.OneMinusSourceAlpha; + SourceAlphaBlendFactor = BlendFactor.One; + DestinationAlphaBlendFactor = BlendFactor.OneMinusSourceAlpha; + } + DepthState { + WriteEnabled = false; + } + RenderQueueType = Transparent; + #else + BlendState { + Enabled = false; + SourceColorBlendFactor = BlendFactor.SourceAlpha; + DestinationColorBlendFactor = BlendFactor.OneMinusSourceAlpha; + SourceAlphaBlendFactor = BlendFactor.One; + DestinationAlphaBlendFactor = BlendFactor.OneMinusSourceAlpha; + } + DepthState { + WriteEnabled = true; + } + RenderQueueType = Opaque; + #endif + + mat4 renderer_MVPMat; + vec4 material_BaseColor; + float material_AlphaCutoff; + sampler2D material_BaseTexture; + + struct Attributes { + vec4 POSITION; + vec2 TEXCOORD_0; + }; + + struct Varyings { + vec3 v_pos; + vec2 v_uv; + }; + + VertexShader = vert; + FragmentShader = frag; + + Varyings vert(Attributes attr) { + Varyings v; + + gl_Position = renderer_MVPMat * attr2.POSITION; + none(); + v.v_pos = gl_Position.xyz; + v.v_uv = attr.TEXCOORD_023; + return v; + } + + void frag(Varyings v) { + vec4 baseColor = material_BaseColor; + + #ifdef MATERIAL_HAS_BASETEXTURE + vec4 textureColor = texture2D(material_BaseTexture, v.v_uv); + #ifndef ENGINE_IS_COLORSPACE_GAMMA + textureColor = gammaToLinear(textureColor); + #endif + baseColor *= textureColor; + #endif + + #ifdef MATERIAL_IS_ALPHA_CUTOFF + if( baseColor.a < material_AlphaCutoff ) { + discard; + } + #endif + + gl_FragColor = baseColor; + + #ifndef MATERIAL_IS_TRANSPARENT + gl_FragColor.a = 1.0; + #endif + } + } + } + } \ No newline at end of file From 077e00977c5642aed4399471bd6f13f352778768 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 14:44:12 +0800 Subject: [PATCH 055/131] feat: opt code --- .../src/contentParser/ShaderContentParser.ts | 41 +++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/packages/shader-lab/src/contentParser/ShaderContentParser.ts b/packages/shader-lab/src/contentParser/ShaderContentParser.ts index 7c22162971..4a0c51fd8e 100644 --- a/packages/shader-lab/src/contentParser/ShaderContentParser.ts +++ b/packages/shader-lab/src/contentParser/ShaderContentParser.ts @@ -175,7 +175,6 @@ export class ShaderContentParser { scanner.scanText(";"); const sm = this._symbolTable.lookup({ type: stateToken.type, ident: variable.lexeme }); if (!sm?.value) { - let error: CompilationError; // #if _EDITOR throw new CompilationError( `Invalid ${stateToken.lexeme} variable: ${variable.lexeme}`, @@ -232,13 +231,15 @@ export class ShaderContentParser { scanner.scanText("]"); scanner.scanText("="); } else if (op.lexeme !== "=") { - const error = new CompilationError( + // #if _EDITOR + throw new CompilationError( `Invalid syntax, expect character '=', but got ${op.lexeme}`, scanner.curPosition, scanner.source ); - error.log(); - throw error; + // #else + throw new Error(`Invalid syntax, expect character '=', but got ${op.lexeme}`); + // #endif } renderStateProp += idx; } @@ -246,13 +247,15 @@ export class ShaderContentParser { renderStateProp = state + renderStateProp; const renderStateElementKey = RenderStateDataKey[renderStateProp]; if (renderStateElementKey == undefined) { - const error = new CompilationError( + // #if _EDITOR + throw new CompilationError( `Invalid render state element ${renderStateProp}`, scanner.curPosition, scanner.source ); - error.log(); - throw error; + // #else + throw new Error(`Invalid render state element ${renderStateProp}`); + // #endif } scanner.skipCommentsAndSpace(); @@ -282,13 +285,15 @@ export class ShaderContentParser { const engineTypeProp = scanner.scanToken(); value = ShaderContentParser._engineType[token.lexeme]?.[engineTypeProp.lexeme]; if (value == undefined) { - const error = new CompilationError( + // #if _EDITOR + throw new CompilationError( `Invalid engine constant: ${token.lexeme}.${engineTypeProp.lexeme}`, scanner.curPosition, scanner.source ); - error.log(); - throw error; + // #else + throw new Error(`Invalid engine constant: ${token.lexeme}.${engineTypeProp.lexeme}`); + // #endif } } else { value = token.lexeme; @@ -308,9 +313,11 @@ export class ShaderContentParser { scanner.scanText(";"); const value = ShaderContentParser._engineType.RenderQueueType[word.lexeme]; if (value == undefined) { - const error = new CompilationError(`Invalid render queue ${word.lexeme}`, word.location, scanner.source); - error.log(); - throw error; + // #if _EDITOR + throw new CompilationError(`Invalid render queue ${word.lexeme}`, word.location, scanner.source); + // #else + throw new Error(`Invalid render queue ${word.lexeme}`); + // #endif } const key = RenderStateDataKey.RenderQueueType; ret.renderStates.constantMap[key] = value; @@ -456,9 +463,11 @@ export class ShaderContentParser { scanner.scanText("="); const entry = scanner.scanToken(); if (ret[word.lexeme]) { - const error = new CompilationError("reassign main entry", scanner.curPosition, scanner.source); - error.log(); - throw error; + // #if _EDITOR + throw new CompilationError("reassign main entry", scanner.curPosition, scanner.source); + // #else + throw new Error("reassign main entry"); + // #endif } const key = word.type === EKeyword.GS_VertexShader ? "vertexEntry" : "fragmentEntry"; ret[key] = entry.lexeme; From 462af900015dae97beea0ded385e436c067b009e Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 14:47:40 +0800 Subject: [PATCH 056/131] feat: opt code --- tsconfig.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index 6bef4d478a..f62e2e508d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,9 +14,7 @@ "skipLibCheck": true, "paths": { "@/*": ["src/*"] // 配置模块别名,对于 chair 项目需做更改,见下文 - }, - "allowJs": true, - "checkJs": false + } }, "exclude": ["node_modules", "types", "packages/*/tests"] } From 54f9c79b9c670d195f33eeaedb1722080017e855 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 15:28:36 +0800 Subject: [PATCH 057/131] feat: opt code --- packages/shader-lab/src/ShaderLab.ts | 13 ++-- packages/shader-lab/src/common/BaseScanner.ts | 10 ++- .../shader-lab/src/contentParser/Scanner.ts | 7 ++ .../src/contentParser/ShaderContentParser.ts | 74 +++++++++++++------ 4 files changed, 74 insertions(+), 30 deletions(-) diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index 8a7aba71e4..b779be907f 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -53,7 +53,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); @@ -106,9 +105,14 @@ export class ShaderLab implements IShaderLab { } _parseShaderContent(shaderSource: string): IShaderContent { + ShaderLabObjectPool.clearAllShaderLabObjectPool(); this._errors.length = 0; + const ret = ShaderContentParser.parse(shaderSource); + for (const error of ShaderContentParser._errors) { + this._errors.push(error); + } ShaderContentParser.reset(); - return ShaderContentParser.parse(shaderSource); + return ret; } // #if _EDITOR @@ -148,10 +152,9 @@ export class ShaderLab implements IShaderLab { * @internal */ _logErrors(errors: GSError[], source?: string) { - // #if !_EDITOR - Logger.error(`${errors.length} errors occur!`); - // #else if (!Logger.isEnabled) return; + Logger.error(`${errors.length} errors occur!`); + // #if _EDITOR for (const err of errors) { err.log(source); } diff --git a/packages/shader-lab/src/common/BaseScanner.ts b/packages/shader-lab/src/common/BaseScanner.ts index c1d897970f..5b02703d5f 100644 --- a/packages/shader-lab/src/common/BaseScanner.ts +++ b/packages/shader-lab/src/common/BaseScanner.ts @@ -7,6 +7,9 @@ import { BaseToken } from "./BaseToken"; export type OnToken = (token: BaseToken, scanner: BaseScanner) => void; +/** + * @internal + */ export default class BaseScanner { private static _spaceCharsWithBreak = [" ", "\t", "\n"]; private static _spaceChars = [" ", "\t"]; @@ -67,9 +70,6 @@ export default class BaseScanner { } } - /** - * @internal - */ _advance(): void { if (this.isEnd()) { return; @@ -130,7 +130,9 @@ export default class BaseScanner { throwError(pos: ShaderPosition | ShaderRange, ...msgs: any[]) { // #if _EDITOR - throw new ScannerError(msgs.join(" "), pos, this._source); + const error = new ScannerError(msgs.join(" "), pos, this._source); + error.log(); + throw error; // #else throw new Error(msgs.join("")); // #endif diff --git a/packages/shader-lab/src/contentParser/Scanner.ts b/packages/shader-lab/src/contentParser/Scanner.ts index 63cdd33404..8052556cc8 100644 --- a/packages/shader-lab/src/contentParser/Scanner.ts +++ b/packages/shader-lab/src/contentParser/Scanner.ts @@ -25,4 +25,11 @@ export default class Scanner extends BaseScanner { } return Number(this._source.substring(start, this._currentIndex)); } + + scanToCharacter(char: string): void { + while (this.getCurChar() !== char) { + this._advance(); + } + this._advance(); + } } diff --git a/packages/shader-lab/src/contentParser/ShaderContentParser.ts b/packages/shader-lab/src/contentParser/ShaderContentParser.ts index 4a0c51fd8e..29cc58596a 100644 --- a/packages/shader-lab/src/contentParser/ShaderContentParser.ts +++ b/packages/shader-lab/src/contentParser/ShaderContentParser.ts @@ -46,8 +46,14 @@ const RenderStateType = [ EKeyword.GS_StencilState ]; +/** + * @internal + */ export class ShaderContentParser { static _engineType = { RenderQueueType, CompareFunction, StencilOperation, BlendOperation, BlendFactor, CullMode }; + + static _errors: CompilationError[] = []; + private static _isRenderStateDeclarator(token: BaseToken) { return RenderStateType.includes(token.type); } @@ -59,6 +65,7 @@ export class ShaderContentParser { private static _symbolTable: SymbolTableStack = new SymbolTableStack(); static reset() { + this._errors.length = 0; this._symbolTable.clear(); this._newScope(); } @@ -175,15 +182,19 @@ export class ShaderContentParser { scanner.scanText(";"); const sm = this._symbolTable.lookup({ type: stateToken.type, ident: variable.lexeme }); if (!sm?.value) { - // #if _EDITOR - throw new CompilationError( + let error: CompilationError; + // #if !_EDITOR + // @ts-ignore + error = new Error(`Invalid ${stateToken.lexeme} variable: ${variable.lexeme}`); + // #else + error = new CompilationError( `Invalid ${stateToken.lexeme} variable: ${variable.lexeme}`, scanner.curPosition, scanner.source ); - // #else - throw new Error(`Invalid ${stateToken.lexeme} variable: ${variable.lexeme}`); // #endif + this._errors.push(error); + return; } const renderState = sm.value as IRenderStates; Object.assign(ret.renderStates.constantMap, renderState.constantMap); @@ -231,15 +242,20 @@ export class ShaderContentParser { scanner.scanText("]"); scanner.scanText("="); } else if (op.lexeme !== "=") { - // #if _EDITOR - throw new CompilationError( + let error: CompilationError; + // #if !_EDITOR + // @ts-ignore + error = new Error(`Invalid syntax, expect character '=', but got ${op.lexeme}`); + // #else + error = new CompilationError( `Invalid syntax, expect character '=', but got ${op.lexeme}`, scanner.curPosition, scanner.source ); - // #else - throw new Error(`Invalid syntax, expect character '=', but got ${op.lexeme}`); // #endif + this._errors.push(error); + scanner.scanToCharacter(";"); + return; } renderStateProp += idx; } @@ -247,15 +263,20 @@ export class ShaderContentParser { renderStateProp = state + renderStateProp; const renderStateElementKey = RenderStateDataKey[renderStateProp]; if (renderStateElementKey == undefined) { - // #if _EDITOR - throw new CompilationError( + let error: CompilationError; + // #if !_EDITOR + // @ts-ignore + error = new Error(`Invalid render state element ${renderStateProp}`); + // #else + error = new CompilationError( `Invalid render state element ${renderStateProp}`, scanner.curPosition, scanner.source ); - // #else - throw new Error(`Invalid render state element ${renderStateProp}`); // #endif + this._errors.push(error); + scanner.scanToCharacter(";"); + return; } scanner.skipCommentsAndSpace(); @@ -285,15 +306,20 @@ export class ShaderContentParser { const engineTypeProp = scanner.scanToken(); value = ShaderContentParser._engineType[token.lexeme]?.[engineTypeProp.lexeme]; if (value == undefined) { - // #if _EDITOR - throw new CompilationError( + let error: CompilationError; + // #if !_EDITOR + // @ts-ignore + error = new Error(`Invalid engine constant: ${token.lexeme}.${engineTypeProp.lexeme}`); + // #else + error = new CompilationError( `Invalid engine constant: ${token.lexeme}.${engineTypeProp.lexeme}`, - scanner.curPosition, + engineTypeProp.location, scanner.source ); - // #else - throw new Error(`Invalid engine constant: ${token.lexeme}.${engineTypeProp.lexeme}`); // #endif + this._errors.push(error); + scanner.scanToCharacter(";"); + return; } } else { value = token.lexeme; @@ -313,11 +339,15 @@ export class ShaderContentParser { scanner.scanText(";"); const value = ShaderContentParser._engineType.RenderQueueType[word.lexeme]; if (value == undefined) { - // #if _EDITOR - throw new CompilationError(`Invalid render queue ${word.lexeme}`, word.location, scanner.source); + let error: CompilationError; + // #if !_EDITOR + // @ts-ignore + error = new Error(`Invalid render queue ${word.lexeme}`); // #else - throw new Error(`Invalid render queue ${word.lexeme}`); + error = new CompilationError(`Invalid render queue ${word.lexeme}`, word.location, scanner.source); // #endif + this._errors.push(error); + return; } const key = RenderStateDataKey.RenderQueueType; ret.renderStates.constantMap[key] = value; @@ -464,7 +494,9 @@ export class ShaderContentParser { const entry = scanner.scanToken(); if (ret[word.lexeme]) { // #if _EDITOR - throw new CompilationError("reassign main entry", scanner.curPosition, scanner.source); + const error = new CompilationError("reassign main entry", scanner.curPosition, scanner.source); + error.log(); + throw error; // #else throw new Error("reassign main entry"); // #endif From d01b0600e0a2fe6bccd50cc85baef26e0ac31175 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 15:38:13 +0800 Subject: [PATCH 058/131] feat: opt code --- packages/shader-lab/src/ShaderLab.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index b779be907f..69b5088e69 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -106,12 +106,12 @@ export class ShaderLab implements IShaderLab { _parseShaderContent(shaderSource: string): IShaderContent { ShaderLabObjectPool.clearAllShaderLabObjectPool(); + ShaderContentParser.reset(); this._errors.length = 0; const ret = ShaderContentParser.parse(shaderSource); for (const error of ShaderContentParser._errors) { this._errors.push(error); } - ShaderContentParser.reset(); return ret; } From df84ce06a597c118daf735f1a0f9ef679a91e04c Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 15:59:00 +0800 Subject: [PATCH 059/131] feat: opt code --- packages/shader-lab/src/codeGen/CodeGenVisitor.ts | 6 +++--- packages/shader-lab/src/codeGen/GLESVisitor.ts | 7 ++++--- packages/shader-lab/src/parser/SemanticAnalyzer.ts | 12 +++++------- packages/shader-lab/src/parser/ShaderTargetParser.ts | 5 ++--- tests/src/shader-lab/ShaderLab.test.ts | 4 +--- 5 files changed, 15 insertions(+), 19 deletions(-) diff --git a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts index 534a1473bc..69e3fdee87 100644 --- a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts +++ b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts @@ -192,10 +192,10 @@ export class CodeGenVisitor { let error: Error; // #if _EDITOR error = new CompilationError(message, loc, ShaderLab._processingPassText); - // #else - error = new Error(message); - // #endif this._errors.push(error); return error; + // #else + throw new Error(message); + // #endif } } diff --git a/packages/shader-lab/src/codeGen/GLESVisitor.ts b/packages/shader-lab/src/codeGen/GLESVisitor.ts index 55e39efacf..401bea870e 100644 --- a/packages/shader-lab/src/codeGen/GLESVisitor.ts +++ b/packages/shader-lab/src/codeGen/GLESVisitor.ts @@ -6,6 +6,7 @@ import { EShaderStage } from "../common/Enums"; import { IShaderInfo } from "@galacean/engine-design"; import { ICodeSegment } from "./types"; import { VisitorContext } from "./VisitorContext"; +import { EKeyword } from "../common"; const defaultPrecision = ` #ifdef GL_FRAGMENT_PRECISION_HIGH @@ -47,15 +48,15 @@ export abstract class GLESVisitor extends CodeGenVisitor { VisitorContext.context.stage = EShaderStage.VERTEX; const returnType = fnNode.protoType.returnType; - if (typeof returnType.type !== "string") { - this.reportError(returnType.location, "main entry can only return struct."); - } else { + if (typeof returnType.type === "string") { const varyStruct = symbolTable.lookup({ ident: returnType.type, symbolType: ESymbolType.STRUCT }); if (!varyStruct) { this.reportError(returnType.location, `invalid varying struct: ${returnType.type}`); } else { VisitorContext.context.varyingStruct = varyStruct.astNode; } + } else if (returnType.type !== EKeyword.VOID) { + this.reportError(returnType.location, "main entry can only return struct."); } const paramList = fnNode.protoType.parameterList; diff --git a/packages/shader-lab/src/parser/SemanticAnalyzer.ts b/packages/shader-lab/src/parser/SemanticAnalyzer.ts index 8972a21411..c12e1335f2 100644 --- a/packages/shader-lab/src/parser/SemanticAnalyzer.ts +++ b/packages/shader-lab/src/parser/SemanticAnalyzer.ts @@ -60,14 +60,12 @@ export default class SematicAnalyzer { } error(loc: ShaderRange, ...param: any[]) { - let err: CompilationError; - // #if !_EDITOR - // @ts-ignore - err = new Error(param.join("")); - // #else - err = new CompilationError(param.join(""), loc, ShaderLab._processingPassText); - // #endif + // #if _EDITOR + const err = new CompilationError(param.join(""), loc, ShaderLab._processingPassText); this.errors.push(err); return err; + // #else + throw new Error(param.join("")); + // #endif } } diff --git a/packages/shader-lab/src/parser/ShaderTargetParser.ts b/packages/shader-lab/src/parser/ShaderTargetParser.ts index 8a737d4025..809a2e22fb 100644 --- a/packages/shader-lab/src/parser/ShaderTargetParser.ts +++ b/packages/shader-lab/src/parser/ShaderTargetParser.ts @@ -116,11 +116,10 @@ export class ShaderTargetParser { this.sematicAnalyzer.errors.push( new CompilationError(`Unexpected token ${token.lexeme}`, token.location, ShaderLab._processingPassText) ); + return null; // #else - // @ts-ignore - this.sematicAnalyzer.errors.push(new Error(`Unexpected token ${token.lexeme}`)); + throw new Error(`Unexpected token ${token.lexeme}`); // #endif - return null; } } } diff --git a/tests/src/shader-lab/ShaderLab.test.ts b/tests/src/shader-lab/ShaderLab.test.ts index 9f351b5695..b6ba9edfe7 100644 --- a/tests/src/shader-lab/ShaderLab.test.ts +++ b/tests/src/shader-lab/ShaderLab.test.ts @@ -248,8 +248,6 @@ describe("ShaderLab", () => { assert.instanceOf(shaderLabEditor._errors[1], CompilationError); assert.instanceOf(shaderLabEditor._errors[2], CompilationError); - shaderParse.bind(shaderLabRelease)(errorShader); - // @ts-ignore - expect(shaderLabRelease._errors.length).to.eq(1); + expect(shaderParse.bind(shaderLabRelease)).to.throw(Error); }); }); From 1426499e3e5a8f2fc6d96f6f5e7c95cccff7bd78 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 16:13:03 +0800 Subject: [PATCH 060/131] feat: opt code --- .../shader-lab/src/codeGen/VisitorContext.ts | 4 +- .../src/contentParser/ShaderContentParser.ts | 91 ++++++++----------- .../shader-lab/src/preprocessor/PpParser.ts | 2 +- 3 files changed, 43 insertions(+), 54 deletions(-) diff --git a/packages/shader-lab/src/codeGen/VisitorContext.ts b/packages/shader-lab/src/codeGen/VisitorContext.ts index 8eeb95a5fb..fdbcc47625 100644 --- a/packages/shader-lab/src/codeGen/VisitorContext.ts +++ b/packages/shader-lab/src/codeGen/VisitorContext.ts @@ -71,7 +71,7 @@ export class VisitorContext { ); // #else // @ts-ignore - return new Error(`referenced attribute not found: ${ident.lexeme}`); + throw new Error(`referenced attribute not found: ${ident.lexeme}`); // #endif } this._referencedAttributeList[ident.lexeme] = prop; @@ -90,7 +90,7 @@ export class VisitorContext { ); // #else // @ts-ignore - return new Error(`referenced varying not found: ${ident.lexeme}`); + throw new Error(`referenced varying not found: ${ident.lexeme}`); // #endif } this._referencedVaryingList[ident.lexeme] = prop; diff --git a/packages/shader-lab/src/contentParser/ShaderContentParser.ts b/packages/shader-lab/src/contentParser/ShaderContentParser.ts index 29cc58596a..27677cd605 100644 --- a/packages/shader-lab/src/contentParser/ShaderContentParser.ts +++ b/packages/shader-lab/src/contentParser/ShaderContentParser.ts @@ -182,19 +182,18 @@ export class ShaderContentParser { scanner.scanText(";"); const sm = this._symbolTable.lookup({ type: stateToken.type, ident: variable.lexeme }); if (!sm?.value) { - let error: CompilationError; - // #if !_EDITOR - // @ts-ignore - error = new Error(`Invalid ${stateToken.lexeme} variable: ${variable.lexeme}`); - // #else - error = new CompilationError( - `Invalid ${stateToken.lexeme} variable: ${variable.lexeme}`, - scanner.curPosition, - scanner.source + // #if _EDITOR + this._errors.push( + new CompilationError( + `Invalid "${stateToken.lexeme}" variable: ${variable.lexeme}`, + variable.location, + scanner.source + ) ); - // #endif - this._errors.push(error); return; + // #else + throw new Error(`Invalid "${stateToken.lexeme}" variable: ${variable.lexeme}`); + // #endif } const renderState = sm.value as IRenderStates; Object.assign(ret.renderStates.constantMap, renderState.constantMap); @@ -242,20 +241,19 @@ export class ShaderContentParser { scanner.scanText("]"); scanner.scanText("="); } else if (op.lexeme !== "=") { - let error: CompilationError; - // #if !_EDITOR - // @ts-ignore - error = new Error(`Invalid syntax, expect character '=', but got ${op.lexeme}`); - // #else - error = new CompilationError( - `Invalid syntax, expect character '=', but got ${op.lexeme}`, - scanner.curPosition, - scanner.source + // #if _EDITOR + this._errors.push( + new CompilationError( + `Invalid syntax, expect character '=', but got ${op.lexeme}`, + scanner.curPosition, + scanner.source + ) ); - // #endif - this._errors.push(error); scanner.scanToCharacter(";"); return; + // #else + throw new Error(`Invalid syntax, expect character '=', but got ${op.lexeme}`); + // #endif } renderStateProp += idx; } @@ -263,20 +261,15 @@ export class ShaderContentParser { renderStateProp = state + renderStateProp; const renderStateElementKey = RenderStateDataKey[renderStateProp]; if (renderStateElementKey == undefined) { - let error: CompilationError; - // #if !_EDITOR - // @ts-ignore - error = new Error(`Invalid render state element ${renderStateProp}`); - // #else - error = new CompilationError( - `Invalid render state element ${renderStateProp}`, - scanner.curPosition, - scanner.source + // #if _EDITOR + this._errors.push( + new CompilationError(`Invalid render state element ${renderStateProp}`, scanner.curPosition, scanner.source) ); - // #endif - this._errors.push(error); scanner.scanToCharacter(";"); return; + // #else + throw new Error(`Invalid render state element ${renderStateProp}`); + // #endif } scanner.skipCommentsAndSpace(); @@ -306,20 +299,19 @@ export class ShaderContentParser { const engineTypeProp = scanner.scanToken(); value = ShaderContentParser._engineType[token.lexeme]?.[engineTypeProp.lexeme]; if (value == undefined) { - let error: CompilationError; - // #if !_EDITOR - // @ts-ignore - error = new Error(`Invalid engine constant: ${token.lexeme}.${engineTypeProp.lexeme}`); - // #else - error = new CompilationError( - `Invalid engine constant: ${token.lexeme}.${engineTypeProp.lexeme}`, - engineTypeProp.location, - scanner.source + // #if _EDITOR + this._errors.push( + new CompilationError( + `Invalid engine constant: ${token.lexeme}.${engineTypeProp.lexeme}`, + engineTypeProp.location, + scanner.source + ) ); - // #endif - this._errors.push(error); scanner.scanToCharacter(";"); return; + // #else + throw new Error(`Invalid engine constant: ${token.lexeme}.${engineTypeProp.lexeme}`); + // #endif } } else { value = token.lexeme; @@ -339,15 +331,12 @@ export class ShaderContentParser { scanner.scanText(";"); const value = ShaderContentParser._engineType.RenderQueueType[word.lexeme]; if (value == undefined) { - let error: CompilationError; - // #if !_EDITOR - // @ts-ignore - error = new Error(`Invalid render queue ${word.lexeme}`); + // #if _EDITOR + this._errors.push(new CompilationError(`Invalid render queue ${word.lexeme}`, word.location, scanner.source)); + return; // #else - error = new CompilationError(`Invalid render queue ${word.lexeme}`, word.location, scanner.source); + throw new Error(`Invalid render queue ${word.lexeme}`); // #endif - this._errors.push(error); - return; } const key = RenderStateDataKey.RenderQueueType; ret.renderStates.constantMap[key] = value; diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 5225623edc..2257e94c51 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -107,7 +107,7 @@ export class PpParser { // #if _EDITOR this._errors.push(new PreprocessorError(message, loc, source, file)); // #else - this._errors.push(new Error(message)); + throw new Error(message); // #endif } From 48eb236b473cf78b266f12ed12dce4d29f7261f1 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 16:28:52 +0800 Subject: [PATCH 061/131] feat: opt code --- packages/shader-lab/src/ShaderLab.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index 69b5088e69..8d57dbdb8a 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -35,6 +35,7 @@ export class ShaderLab implements IShaderLab { return range; } + // #if _EDITOR private _errors: GSError[] = []; /** @@ -43,6 +44,7 @@ export class ShaderLab implements IShaderLab { get errors(): GSError[] | undefined { return this._errors; } + // #endif _parseShaderPass( source: string, @@ -64,6 +66,7 @@ export class ShaderLab implements IShaderLab { 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(err); @@ -71,6 +74,7 @@ export class ShaderLab implements IShaderLab { this._logErrors(this._errors); return { vertex: "", fragment: "" }; } + // #endif Logger.info(`[pass compilation - preprocessor] cost time ${performance.now() - preprocessorStart}ms`); @@ -81,6 +85,8 @@ export class ShaderLab implements IShaderLab { ShaderLab._processingPassText = ppdContent; const program = parser.parse(tokens); + + // #if _EDITOR for (const err of parser.errors) { this._errors.push(err); } @@ -88,6 +94,8 @@ export class ShaderLab implements IShaderLab { this._logErrors(this._errors); return { vertex: "", fragment: "" }; } + // #endif + const codeGen = backend === ShaderPlatformTarget.GLES100 ? GLES100Visitor.getVisitor() : GLES300Visitor.getVisitor(); @@ -96,10 +104,12 @@ export class ShaderLab implements IShaderLab { 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; } @@ -146,7 +156,6 @@ export class ShaderLab implements IShaderLab { } return passResult; } - // #endif /** * @internal @@ -154,10 +163,9 @@ export class ShaderLab implements IShaderLab { _logErrors(errors: GSError[], source?: string) { if (!Logger.isEnabled) return; Logger.error(`${errors.length} errors occur!`); - // #if _EDITOR for (const err of errors) { err.log(source); } - // #endif } + // #endif } From 3b06a3f676c9f296a5e315c5a564ce7389d553ef Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 16:31:39 +0800 Subject: [PATCH 062/131] feat: opt code --- packages/shader-lab/src/ShaderLab.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index 8d57dbdb8a..52fe636451 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -117,7 +117,9 @@ export class ShaderLab implements IShaderLab { _parseShaderContent(shaderSource: string): IShaderContent { ShaderLabObjectPool.clearAllShaderLabObjectPool(); ShaderContentParser.reset(); + // #if _EDITOR this._errors.length = 0; + // #endif const ret = ShaderContentParser.parse(shaderSource); for (const error of ShaderContentParser._errors) { this._errors.push(error); From ab356b65016b6ea2967043f1f8b249a43d2ebcd4 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 16:34:46 +0800 Subject: [PATCH 063/131] feat: opt code --- packages/shader-lab/src/parser/SemanticAnalyzer.ts | 4 ++++ packages/shader-lab/src/parser/ShaderTargetParser.ts | 2 ++ 2 files changed, 6 insertions(+) diff --git a/packages/shader-lab/src/parser/SemanticAnalyzer.ts b/packages/shader-lab/src/parser/SemanticAnalyzer.ts index c12e1335f2..f0f2d69886 100644 --- a/packages/shader-lab/src/parser/SemanticAnalyzer.ts +++ b/packages/shader-lab/src/parser/SemanticAnalyzer.ts @@ -22,7 +22,9 @@ export default class SematicAnalyzer { symbolTable: SymbolTableStack = new SymbolTableStack(); private _shaderData = new ShaderData(); + // #if _EDITOR readonly errors: CompilationError[] = []; + // #endif get shaderData() { return this._shaderData; @@ -39,7 +41,9 @@ export default class SematicAnalyzer { this._shaderData = new ShaderData(); this.symbolTable.clear(); this.newScope(); + // #if _EDITOR this.errors.length = 0; + // #endif } newScope() { diff --git a/packages/shader-lab/src/parser/ShaderTargetParser.ts b/packages/shader-lab/src/parser/ShaderTargetParser.ts index 809a2e22fb..ad8d322580 100644 --- a/packages/shader-lab/src/parser/ShaderTargetParser.ts +++ b/packages/shader-lab/src/parser/ShaderTargetParser.ts @@ -35,10 +35,12 @@ export class ShaderTargetParser { return this.gotoTable.get(this.curState); } + // #if _EDITOR /** @internal */ get errors() { return this.sematicAnalyzer.errors; } + // #endif static _singleton: ShaderTargetParser; From ab6de2c9cf30552c7e42ac62a6176b2af723069c Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 16:36:53 +0800 Subject: [PATCH 064/131] feat: opt code --- packages/shader-lab/src/parser/ShaderTargetParser.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/shader-lab/src/parser/ShaderTargetParser.ts b/packages/shader-lab/src/parser/ShaderTargetParser.ts index ad8d322580..9edf858b07 100644 --- a/packages/shader-lab/src/parser/ShaderTargetParser.ts +++ b/packages/shader-lab/src/parser/ShaderTargetParser.ts @@ -64,7 +64,7 @@ export class ShaderTargetParser { } parse(tokens: Generator): ASTNode.GLShaderProgram | null { - this._reset(); + this.sematicAnalyzer.reset(); const start = performance.now(); const { _traceBackStack: traceBackStack, sematicAnalyzer } = this; traceBackStack.push(0); @@ -126,10 +126,6 @@ export class ShaderTargetParser { } } - private _reset() { - this.sematicAnalyzer.reset(); - } - // #if _EDITOR private _printStack(nextToken: BaseToken) { let str = ""; From 5092aabc70e8999f2c59901736957abb226f20d3 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 16:39:20 +0800 Subject: [PATCH 065/131] feat: opt code --- packages/shader-lab/src/parser/TargetParser.y | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shader-lab/src/parser/TargetParser.y b/packages/shader-lab/src/parser/TargetParser.y index 0d943c24be..17c9b00013 100644 --- a/packages/shader-lab/src/parser/TargetParser.y +++ b/packages/shader-lab/src/parser/TargetParser.y @@ -432,7 +432,7 @@ expression_statement: // Dangling else ambiguity selection_statement: IF '(' expression ')' statement - | IF '(' expression ')' statement ELSE statement ELSE + | IF '(' expression ')' statement ELSE statement ; iteration_statement: From 8a5f8382a5179cf4afc975c92689b0c82e38881f Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 16:41:11 +0800 Subject: [PATCH 066/131] feat: opt code --- packages/shader-lab/src/parser/builtin/functions.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/shader-lab/src/parser/builtin/functions.ts b/packages/shader-lab/src/parser/builtin/functions.ts index d43bb85f5c..1ff01771f6 100644 --- a/packages/shader-lab/src/parser/builtin/functions.ts +++ b/packages/shader-lab/src/parser/builtin/functions.ts @@ -136,8 +136,6 @@ BuiltinFunction._create("max", EGenType.GenType, EGenType.GenType); BuiltinFunction._create("max", EGenType.GenType, EKeyword.FLOAT); BuiltinFunction._create("max", EGenType.GenIntType, EGenType.GenIntType); BuiltinFunction._create("max", EGenType.GenIntType, EKeyword.INT); -// BuiltinFunction._create("max", EGenType.GenUintType, EGenType.GenUintType, EGenType.GenUintType); -// BuiltinFunction._create("max", EGenType.GenUintType, EGenType.GenUintType, EKeyword.UINT); BuiltinFunction._create("clamp", EGenType.GenType, EGenType.GenType, EGenType.GenType, EGenType.GenType); BuiltinFunction._create("clamp", EGenType.GenType, EGenType.GenType, EKeyword.FLOAT, EKeyword.FLOAT); BuiltinFunction._create("clamp", EGenType.GenIntType, EGenType.GenIntType, EGenType.GenIntType, EGenType.GenIntType); From ff3f5aea5e9f5e2c7abb1bfd1546f9be0f732f1b Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 16:50:35 +0800 Subject: [PATCH 067/131] feat: opt code --- .../shader-lab/src/parser/builtin/functions.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/shader-lab/src/parser/builtin/functions.ts b/packages/shader-lab/src/parser/builtin/functions.ts index 1ff01771f6..c65b1883eb 100644 --- a/packages/shader-lab/src/parser/builtin/functions.ts +++ b/packages/shader-lab/src/parser/builtin/functions.ts @@ -126,16 +126,16 @@ BuiltinFunction._create("ceil", EGenType.GenType, EGenType.GenType); BuiltinFunction._create("fract", EGenType.GenType, EGenType.GenType); BuiltinFunction._create("mod", EGenType.GenType, EGenType.GenType, EKeyword.FLOAT); BuiltinFunction._create("mod", EGenType.GenType, EGenType.GenType, EGenType.GenType); -BuiltinFunction._create("min", EGenType.GenType, EGenType.GenType); -BuiltinFunction._create("min", EGenType.GenType, EKeyword.FLOAT); -BuiltinFunction._create("min", EGenType.GenIntType, EGenType.GenIntType); -BuiltinFunction._create("min", EGenType.GenIntType, EKeyword.INT); +BuiltinFunction._create("min", EGenType.GenType, EGenType.GenType, EGenType.GenType); +BuiltinFunction._create("min", EGenType.GenType, EGenType.GenType, EKeyword.FLOAT); +BuiltinFunction._create("min", EGenType.GenIntType, EGenType.GenIntType, EGenType.GenIntType); +BuiltinFunction._create("min", EGenType.GenIntType, EGenType.GenIntType, EKeyword.INT); BuiltinFunction._create("min", EGenType.GenUintType, EGenType.GenUintType, EGenType.GenUintType); BuiltinFunction._create("min", EGenType.GenUintType, EGenType.GenUintType, EKeyword.UINT); -BuiltinFunction._create("max", EGenType.GenType, EGenType.GenType); -BuiltinFunction._create("max", EGenType.GenType, EKeyword.FLOAT); -BuiltinFunction._create("max", EGenType.GenIntType, EGenType.GenIntType); -BuiltinFunction._create("max", EGenType.GenIntType, EKeyword.INT); +BuiltinFunction._create("max", EGenType.GenType, EGenType.GenType, EGenType.GenType); +BuiltinFunction._create("max", EGenType.GenType, EGenType.GenType, EKeyword.FLOAT); +BuiltinFunction._create("max", EGenType.GenIntType, EGenType.GenIntType, EGenType.GenIntType); +BuiltinFunction._create("max", EGenType.GenIntType, EGenType.GenIntType, EKeyword.INT); BuiltinFunction._create("clamp", EGenType.GenType, EGenType.GenType, EGenType.GenType, EGenType.GenType); BuiltinFunction._create("clamp", EGenType.GenType, EGenType.GenType, EKeyword.FLOAT, EKeyword.FLOAT); BuiltinFunction._create("clamp", EGenType.GenIntType, EGenType.GenIntType, EGenType.GenIntType, EGenType.GenIntType); From c679a9e125f433cfd35f50706073df1e45009555 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 27 Aug 2024 16:56:51 +0800 Subject: [PATCH 068/131] feat: opt code --- packages/shader-lab/src/preprocessor/PpParser.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 2257e94c51..55581c7274 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -31,11 +31,10 @@ export class PpParser { private static _includeMap: Record; private static _basePathForIncludeKey: string; - /** @internal */ + // #if _EDITOR static _errors: Error[] = []; - /** @internal */ + // #endif static _scanningText: string; - /** @internal */ static _scanningFile = "__main__"; static reset(includeMap: Record, basePathForIncludeKey: string) { @@ -46,7 +45,9 @@ export class PpParser { this.addPredefinedMacro("GL_ES"); this._includeMap = includeMap; this._basePathForIncludeKey = basePathForIncludeKey; + // #if _EDITOR this._errors.length = 0; + // #endif } static addPredefinedMacro(macro: string, value?: string) { @@ -65,7 +66,7 @@ export class PpParser { static parse(scanner: PpScanner): string | null { this._scanningText = scanner.source; this._scanningFile = scanner.file; - while (!scanner.isEnd() && this._errors.length === 0) { + while (!scanner.isEnd()) { const directive = scanner.scanDirective(this._onToken.bind(this))!; if (scanner.isEnd()) break; switch (directive.type) { @@ -94,7 +95,9 @@ export class PpParser { break; } } + // #if _EDITOR if (this._errors.length > 0) return null; + // #endif return PpUtils.expand(this.expandSegments, scanner.source, scanner.sourceMap); } From 44b7e84ec951e12da520498e041abc09e933a230 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Mon, 2 Sep 2024 17:23:56 +0800 Subject: [PATCH 069/131] feat: opt code --- packages/shader-lab/src/Error.ts | 1 + packages/shader-lab/src/ShaderLab.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/shader-lab/src/Error.ts b/packages/shader-lab/src/Error.ts index 77b9bc4e08..1082af414c 100644 --- a/packages/shader-lab/src/Error.ts +++ b/packages/shader-lab/src/Error.ts @@ -24,6 +24,7 @@ export abstract class GSError extends Error { if (_source) source = _source; if (!source) { Logger.error(message); + return; } if (loc instanceof ShaderPosition) { diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index 52fe636451..b8474f1fa9 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -163,7 +163,7 @@ export class ShaderLab implements IShaderLab { * @internal */ _logErrors(errors: GSError[], source?: string) { - if (!Logger.isEnabled) return; + if (errors.length === 0 || !Logger.isEnabled) return; Logger.error(`${errors.length} errors occur!`); for (const err of errors) { err.log(source); From eb0bcdea62a8d3986f2e5a45d3bbac7db7865e0b Mon Sep 17 00:00:00 2001 From: Sway007 Date: Wed, 11 Sep 2024 10:00:48 +0800 Subject: [PATCH 070/131] feat: code opt --- packages/shader-lab/src/ShaderLab.ts | 2 +- packages/shader-lab/src/contentParser/Scanner.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index b8474f1fa9..19bf8c363a 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -41,7 +41,7 @@ export class ShaderLab implements IShaderLab { /** * Retrieve the compilation errors */ - get errors(): GSError[] | undefined { + get errors(): GSError[] { return this._errors; } // #endif diff --git a/packages/shader-lab/src/contentParser/Scanner.ts b/packages/shader-lab/src/contentParser/Scanner.ts index 8052556cc8..3d3aa9b527 100644 --- a/packages/shader-lab/src/contentParser/Scanner.ts +++ b/packages/shader-lab/src/contentParser/Scanner.ts @@ -27,7 +27,7 @@ export default class Scanner extends BaseScanner { } scanToCharacter(char: string): void { - while (this.getCurChar() !== char) { + while (this.getCurChar() !== char && !this.isEnd()) { this._advance(); } this._advance(); From 2f567059a6cfb5d06c62d84da3c390274bd10932 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 26 Sep 2024 14:51:51 +0800 Subject: [PATCH 071/131] feat: code opt --- package.json | 1 - packages/shader-lab/src/Error.ts | 6 ++++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 4f775522ab..1a4f19fb4d 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,6 @@ "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:umd": "cross-env NODE_ENV=release BUILD_TYPE=UMD rollup -cw -m inline", diff --git a/packages/shader-lab/src/Error.ts b/packages/shader-lab/src/Error.ts index 1082af414c..edc9dd9a7a 100644 --- a/packages/shader-lab/src/Error.ts +++ b/packages/shader-lab/src/Error.ts @@ -43,9 +43,11 @@ export abstract class GSError extends Error { diagnosticMessage += lineSplit; diagnosticMessage += `${lines[i]}\n`; + const paddingLength = lineSplit.length + start.column; + const remarkLength = Math.max(end.column - start.column, 1); if (i >= start.line && i <= end.line) { - diagnosticMessage += " ".repeat(lineSplit.length + start.column); - diagnosticMessage += "^".repeat(Math.max(end.column - start.column, 1)); + diagnosticMessage += " ".repeat(paddingLength); + diagnosticMessage += "^".repeat(remarkLength); diagnosticMessage += "\n"; } } From e1f749a0b058ef98f822e2882c87ceefc95a0edb Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 26 Sep 2024 16:45:06 +0800 Subject: [PATCH 072/131] feat: add shaderlab verbose package --- packages/shader-lab/README.md | 12 ++++--- packages/shader-lab/package.json | 3 +- packages/shader-lab/verbose/package.json | 15 +++++++++ rollup.config.js | 40 ++++++++++++++--------- tests/src/shader-lab/Preprocessor.test.ts | 2 +- tests/src/shader-lab/ShaderLab.test.ts | 6 +++- 6 files changed, 54 insertions(+), 24 deletions(-) create mode 100644 packages/shader-lab/verbose/package.json diff --git a/packages/shader-lab/README.md b/packages/shader-lab/README.md index 97c7bf8da4..bcc27e9c7d 100644 --- a/packages/shader-lab/README.md +++ b/packages/shader-lab/README.md @@ -26,17 +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. +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 debug version by import: +you can use `Verbose` version by import: ```ts // umd -import { ShaderLab } from "@galacean/engine-shader-lab/dist/browser.editor"; +import { ShaderLab } from "@galacean/engine-shader-lab/dist/browser.verbose"; +// minified umd +import { ShaderLab } from "@galacean/engine-shader-lab/dist/browser.verbose.min"; // esmoudle -import { ShaderLab } from "@galacean/engine-shader-lab/dist/module.editor"; +import { ShaderLab } from "@galacean/engine-shader-lab/dist/module.verbose"; // commonjs -import { ShaderLab } from "@galacean/engine-shader-lab/dist/main.editor"; +import { ShaderLab } from "@galacean/engine-shader-lab/dist/main.verbose"; ``` ## CFG Grammar conflict detection diff --git a/packages/shader-lab/package.json b/packages/shader-lab/package.json index 9ff3d0a370..9479db3458 100644 --- a/packages/shader-lab/package.json +++ b/packages/shader-lab/package.json @@ -25,7 +25,8 @@ }, "files": [ "dist/**/*", - "types/**/*" + "types/**/*", + "verbose" ], "devDependencies": { "@galacean/engine-design": "workspace:*", diff --git a/packages/shader-lab/verbose/package.json b/packages/shader-lab/verbose/package.json new file mode 100644 index 0000000000..d2993f42f3 --- /dev/null +++ b/packages/shader-lab/verbose/package.json @@ -0,0 +1,15 @@ +{ + "name": "@galacean/engine-shader-lab-verbose", + "license": "MIT", + "main": "../dist/main.verbose.js", + "module": "../dist/module.verbose.js", + "browser": "../dist/browser.verbose.min.js", + "debug": "../src/index.ts", + "types": "../types/index.d.ts", + "umd": { + "name": "Galacean.ShaderLab", + "globals": { + "@galacean/engine": "Galacean" + } + } +} diff --git a/rollup.config.js b/rollup.config.js index 447dfe84ee..bb1941cf0c 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -58,14 +58,14 @@ const commonPlugins = [ : null ]; -function config({ location, pkgJson, editorMode }) { +function config({ location, pkgJson, verboseMode }) { const input = path.join(location, "src", "index.ts"); const dependencies = Object.assign({}, pkgJson.dependencies ?? {}, pkgJson.peerDependencies ?? {}); const curPlugins = Array.from(commonPlugins); curPlugins.push( jscc({ - values: { _EDITOR: editorMode } + values: { _EDITOR: verboseMode } }) ); @@ -82,12 +82,20 @@ function config({ location, pkgJson, editorMode }) { const umdConfig = pkgJson.umd; let file = path.join(location, "dist", "browser.js"); - if (compress) { - curPlugins.push(minify({ sourceMap: true })); - file = path.join(location, "dist", "browser.min.js"); - } - if (editorMode) { - file = path.join(location, "dist", "browser.editor.js"); + if (verboseMode) { + if (compress) { + curPlugins.push(minify({ sourceMap: true })); + file = path.join(location, "dist", "browser.verbose.min.js"); + } else { + file = path.join(location, "dist", "browser.verbose.js"); + } + } else { + if (compress) { + curPlugins.push(minify({ sourceMap: true })); + file = path.join(location, "dist", "browser.min.js"); + } else { + file = path.join(location, "dist", "browser.js"); + } } const umdExternal = Object.keys(umdConfig.globals ?? {}); @@ -110,8 +118,8 @@ function config({ location, pkgJson, editorMode }) { mini: () => { let file = path.join(location, "dist", "miniprogram.js"); const plugins = [...curPlugins, ...miniProgramPlugin]; - if (editorMode) { - file = path.join(location, "dist", "miniprogram.editor.js"); + if (verboseMode) { + file = path.join(location, "dist", "miniprogram.verbose.js"); } return { input, @@ -129,9 +137,9 @@ function config({ location, pkgJson, editorMode }) { module: () => { let esFile = path.join(location, pkgJson.module); let mainFile = path.join(location, pkgJson.main); - if (editorMode) { - esFile = path.join(location, "dist", "module.editor.js"); - mainFile = path.join(location, "dist", "main.editor.js"); + if (verboseMode) { + esFile = path.join(location, "dist", "module.verbose.js"); + mainFile = path.join(location, "dist", "main.verbose.js"); } return { input, @@ -191,21 +199,21 @@ function getUMD() { }) ) ); - umds.push(makeRollupConfig({ ...shaderLabPkg, editorMode: true, type: "umd" })); + umds.push(makeRollupConfig({ ...shaderLabPkg, verboseMode: true, type: "umd" })); return umds; } function getModule() { const configs = [...pkgs]; const modules = configs.map((config) => makeRollupConfig({ ...config, type: "module" })); - modules.push(makeRollupConfig({ ...shaderLabPkg, editorMode: true, type: "module" })); + modules.push(makeRollupConfig({ ...shaderLabPkg, verboseMode: true, type: "module" })); return modules; } function getMini() { const configs = [...pkgs]; const minis = configs.map((config) => makeRollupConfig({ ...config, type: "mini" })); - minis.push(makeRollupConfig({ ...shaderLabPkg, editorMode: true, type: "mini" })); + minis.push(makeRollupConfig({ ...shaderLabPkg, verboseMode: true, type: "mini" })); return minis; } diff --git a/tests/src/shader-lab/Preprocessor.test.ts b/tests/src/shader-lab/Preprocessor.test.ts index 87e87471b2..e9a9a7628a 100644 --- a/tests/src/shader-lab/Preprocessor.test.ts +++ b/tests/src/shader-lab/Preprocessor.test.ts @@ -3,7 +3,7 @@ import { testCaseList } from "./test-case"; import { ShaderLib } from "@galacean/engine-core"; import { expect } from "chai"; import { readFileSync } from "fs"; -import { Preprocessor } from "@galacean/engine-shader-lab/dist/main.editor"; +import { Preprocessor } from "@galacean/engine-shader-lab/verbose"; import { join } from "path"; const includedSource = readFileSync(join(__dirname, "test-case/included.txt")).toString(); diff --git a/tests/src/shader-lab/ShaderLab.test.ts b/tests/src/shader-lab/ShaderLab.test.ts index b6ba9edfe7..6b7d887adb 100644 --- a/tests/src/shader-lab/ShaderLab.test.ts +++ b/tests/src/shader-lab/ShaderLab.test.ts @@ -1,6 +1,6 @@ import { BlendOperation, CompareFunction, CullMode, RenderStateDataKey } from "@galacean/engine-core"; import { Color } from "@galacean/engine-math"; -import { ShaderLab as ShaderLabEditor, CompilationError } from "@galacean/engine-shader-lab/dist/main.editor"; +import { ShaderLab as ShaderLabEditor, CompilationError } from "@galacean/engine-shader-lab/verbose"; import { ShaderLab as ShaderLabRelease } from "@galacean/engine-shader-lab"; import { glslValidate, shaderParse } from "./ShaderValidate"; @@ -243,9 +243,13 @@ describe("ShaderLab", () => { const errorShader = fs.readFileSync(path.join(__dirname, "shaders/compilation-error.shader")).toString(); // @ts-ignore shaderLabEditor._parse(errorShader); + // @ts-ignore expect(shaderLabEditor._errors.length).to.eq(3); + // @ts-ignore assert.instanceOf(shaderLabEditor._errors[0], CompilationError); + // @ts-ignore assert.instanceOf(shaderLabEditor._errors[1], CompilationError); + // @ts-ignore assert.instanceOf(shaderLabEditor._errors[2], CompilationError); expect(shaderParse.bind(shaderLabRelease)).to.throw(Error); From 6e36dd0923b1642ce44fe60a3b415fc73233aaa8 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 26 Sep 2024 16:48:30 +0800 Subject: [PATCH 073/131] feat: add shaderlab verbose package --- packages/shader-lab/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shader-lab/src/index.ts b/packages/shader-lab/src/index.ts index 5877d39f91..b163472751 100644 --- a/packages/shader-lab/src/index.ts +++ b/packages/shader-lab/src/index.ts @@ -10,7 +10,7 @@ export const version = `__buildVersion`; let mode = "Release"; // #if _EDITOR -mode = "Editor"; +mode = "Verbose"; // #endif console.log(`Galacean ShaderLab version: ${version}. mode: ${mode}`); From 032a631393ccc02b8327186308d93295643e34e5 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 26 Sep 2024 17:26:17 +0800 Subject: [PATCH 074/131] feat: update readme --- packages/shader-lab/README.md | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/shader-lab/README.md b/packages/shader-lab/README.md index bcc27e9c7d..92a1c6bd53 100644 --- a/packages/shader-lab/README.md +++ b/packages/shader-lab/README.md @@ -31,14 +31,7 @@ There are two versions of ShaderLab: `Release` and `Verbose`. The `Verbose` vers you can use `Verbose` version by import: ```ts -// umd -import { ShaderLab } from "@galacean/engine-shader-lab/dist/browser.verbose"; -// minified umd -import { ShaderLab } from "@galacean/engine-shader-lab/dist/browser.verbose.min"; -// esmoudle -import { ShaderLab } from "@galacean/engine-shader-lab/dist/module.verbose"; -// commonjs -import { ShaderLab } from "@galacean/engine-shader-lab/dist/main.verbose"; +import { ShaderLab } from "@galacean/engine-shader-lab/verbose"; ``` ## CFG Grammar conflict detection From adb871f0ad4cd577ead6c69ffee1e3b7b01096c7 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 26 Sep 2024 19:07:20 +0800 Subject: [PATCH 075/131] feat: rollup.config.js opt --- packages/shader-lab/verbose/package.json | 1 - rollup.config.js | 13 ++++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/packages/shader-lab/verbose/package.json b/packages/shader-lab/verbose/package.json index d2993f42f3..6a46602bdd 100644 --- a/packages/shader-lab/verbose/package.json +++ b/packages/shader-lab/verbose/package.json @@ -1,5 +1,4 @@ { - "name": "@galacean/engine-shader-lab-verbose", "license": "MIT", "main": "../dist/main.verbose.js", "module": "../dist/module.verbose.js", diff --git a/rollup.config.js b/rollup.config.js index bb1941cf0c..f339465482 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -26,6 +26,7 @@ const pkgs = fs }); const shaderLabPkg = pkgs.find((item) => item.pkgJson.name === "@galacean/engine-shader-lab"); +pkgs.push({ ...shaderLabPkg, verboseMode: true }); // toGlobalName @@ -187,7 +188,7 @@ switch (BUILD_TYPE) { function getUMD() { const configs = pkgs.filter((pkg) => pkg.pkgJson.umd); - const umds = configs + return configs .map((config) => makeRollupConfig({ ...config, type: "umd" })) .concat( configs.map((config) => @@ -199,22 +200,16 @@ function getUMD() { }) ) ); - umds.push(makeRollupConfig({ ...shaderLabPkg, verboseMode: true, type: "umd" })); - return umds; } function getModule() { const configs = [...pkgs]; - const modules = configs.map((config) => makeRollupConfig({ ...config, type: "module" })); - modules.push(makeRollupConfig({ ...shaderLabPkg, verboseMode: true, type: "module" })); - return modules; + return configs.map((config) => makeRollupConfig({ ...config, type: "module" })); } function getMini() { const configs = [...pkgs]; - const minis = configs.map((config) => makeRollupConfig({ ...config, type: "mini" })); - minis.push(makeRollupConfig({ ...shaderLabPkg, verboseMode: true, type: "mini" })); - return minis; + return configs.map((config) => makeRollupConfig({ ...config, type: "mini" })); } function getAll() { From cc8e0a163d09c78c5474320a0d592dc2b29eca18 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 8 Oct 2024 19:08:04 +0800 Subject: [PATCH 076/131] feat: code opt & compatible with multiple log --- packages/shader-lab/package.json | 2 +- packages/shader-lab/src/Error.ts | 87 +++++++++---------- packages/shader-lab/src/ShaderLab.ts | 2 +- .../shader-lab/src/ShaderLabObjectPool.ts | 1 + .../shader-lab/src/codeGen/CodeGenVisitor.ts | 10 +-- .../shader-lab/src/codeGen/VisitorContext.ts | 10 ++- packages/shader-lab/src/common/BaseScanner.ts | 4 +- packages/shader-lab/src/common/ShaderRange.ts | 2 +- .../src/contentParser/ShaderContentParser.ts | 31 +++++-- .../shader-lab/src/parser/SemanticAnalyzer.ts | 6 +- .../src/parser/ShaderTargetParser.ts | 9 +- .../src/preprocessor/MacroDefine.ts | 4 +- .../shader-lab/src/preprocessor/PpParser.ts | 4 +- 13 files changed, 94 insertions(+), 78 deletions(-) diff --git a/packages/shader-lab/package.json b/packages/shader-lab/package.json index 9479db3458..a4297bb52d 100644 --- a/packages/shader-lab/package.json +++ b/packages/shader-lab/package.json @@ -26,7 +26,7 @@ "files": [ "dist/**/*", "types/**/*", - "verbose" + "verbose/package.json" ], "devDependencies": { "@galacean/engine-design": "workspace:*", diff --git a/packages/shader-lab/src/Error.ts b/packages/shader-lab/src/Error.ts index edc9dd9a7a..b18414f13b 100644 --- a/packages/shader-lab/src/Error.ts +++ b/packages/shader-lab/src/Error.ts @@ -2,29 +2,32 @@ import { Logger } from "@galacean/engine"; import { ShaderPosition, ShaderRange } from "./common"; -export abstract class GSError extends Error { +export class GSError extends Error { static wrappingLineCount = 2; - readonly loc: ShaderRange | ShaderPosition; - readonly source: string; - readonly file?: string; + constructor( + name: GSErrorName, + message: string, + public readonly location: ShaderRange | ShaderPosition, + public readonly source: string, + public readonly file?: string + ) { + super(message); + this.name = name; + } - 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(content?: string): void { + Logger.error(this.toString(content)); } - log(_source?: string): void { + override toString(content?: string): string { 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); - return; + const { message, location: loc, source: originSource } = this; + let logSource = originSource; + if (content) logSource = content; + if (!logSource) { + return message; } if (loc instanceof ShaderPosition) { @@ -33,47 +36,37 @@ export abstract class GSError extends Error { start = loc.start; end = loc.end; } - const lines = source.split("\n"); + const lines = logSource.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; + for (let i = start.line - GSError.wrappingLineCount, n = end.line + GSError.wrappingLineCount; i <= n; i++) { + diagnosticMessage += lineSplit + `${lines[i]}\n`; - diagnosticMessage += lineSplit; - diagnosticMessage += `${lines[i]}\n`; - const paddingLength = lineSplit.length + start.column; - const remarkLength = Math.max(end.column - start.column, 1); - if (i >= start.line && i <= end.line) { - diagnosticMessage += " ".repeat(paddingLength); - diagnosticMessage += "^".repeat(remarkLength); - diagnosticMessage += "\n"; - } - } + if (i < start.line || i > end.line) continue; - Logger.error(diagnosticMessage); - } -} + 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); -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"; - } -} + diagnosticMessage += " ".repeat(paddingLength) + "^".repeat(remarkLength) + "\n"; + } -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"; + return diagnosticMessage; } } -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"; - } +export enum GSErrorName { + PreprocessorError = "PreprocessorError", + CompilationError = "CompilationError", + ScannerError = "ScannerError" } // #endif diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index 19bf8c363a..b4a47d7fd9 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -31,7 +31,7 @@ export class ShaderLab implements IShaderLab { static createRange(start: ShaderPosition, end: ShaderPosition): ShaderRange { const range = this._shaderRangePool.get(); - range.setX(start, end); + range.set(start, end); return range; } diff --git a/packages/shader-lab/src/ShaderLabObjectPool.ts b/packages/shader-lab/src/ShaderLabObjectPool.ts index 6284dc1593..c4698d1bab 100644 --- a/packages/shader-lab/src/ShaderLabObjectPool.ts +++ b/packages/shader-lab/src/ShaderLabObjectPool.ts @@ -1,5 +1,6 @@ import { ClearableObjectPool, IPoolElement } from "@galacean/engine"; +// 放到静态类里 /** * @internal */ diff --git a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts index 69e3fdee87..117a856f0e 100644 --- a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts +++ b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts @@ -7,7 +7,7 @@ import { ParserUtils } from "../Utils"; import { NodeChild } from "../parser/types"; import { VisitorContext } from "./VisitorContext"; // #if _EDITOR -import { CompilationError, GSError } from "../Error"; +import { GSErrorName, GSError } from "../Error"; // #endif import { ShaderLab } from "../ShaderLab"; @@ -188,12 +188,12 @@ export class CodeGenVisitor { return this.defaultCodeGen(node.children); } - protected reportError(loc: ShaderRange | ShaderPosition, message: string): CompilationError { + protected reportError(loc: ShaderRange | ShaderPosition, message: string): GSError { let error: Error; // #if _EDITOR - error = new CompilationError(message, loc, ShaderLab._processingPassText); - this._errors.push(error); - return error; + error = new GSError(GSErrorName.CompilationError, message, loc, ShaderLab._processingPassText); + this._errors.push(error); + return error; // #else throw new Error(message); // #endif diff --git a/packages/shader-lab/src/codeGen/VisitorContext.ts b/packages/shader-lab/src/codeGen/VisitorContext.ts index fdbcc47625..61c57eedb5 100644 --- a/packages/shader-lab/src/codeGen/VisitorContext.ts +++ b/packages/shader-lab/src/codeGen/VisitorContext.ts @@ -3,7 +3,7 @@ import { ASTNode } from "../parser/AST"; import { ESymbolType, SymbolTable, SymbolInfo } from "../parser/symbolTable"; import { IParamInfo } from "../parser/types"; // #if _EDITOR -import { CompilationError, GSError } from "../Error"; +import { GSErrorName, GSError } from "../Error"; // #endif import { BaseToken } from "../common/BaseToken"; import { ShaderLab } from "../ShaderLab"; @@ -64,7 +64,8 @@ export class VisitorContext { const prop = this.attributeList.find((item) => item.ident.lexeme === ident.lexeme); if (!prop) { // #if _EDITOR - return new CompilationError( + return new GSError( + GSErrorName.CompilationError, `referenced attribute not found: ${ident.lexeme}`, ident.location, ShaderLab._processingPassText @@ -77,13 +78,14 @@ export class VisitorContext { this._referencedAttributeList[ident.lexeme] = prop; } - referenceVarying(ident: BaseToken): CompilationError | undefined { + referenceVarying(ident: BaseToken): GSError | undefined { if (this._referencedVaryingList[ident.lexeme]) return; const prop = this.varyingStruct?.propList.find((item) => item.ident.lexeme === ident.lexeme); if (!prop) { // #if _EDITOR - return new CompilationError( + return new GSError( + GSErrorName.CompilationError, `referenced varying not found: ${ident.lexeme}`, ident.location, ShaderLab._processingPassText diff --git a/packages/shader-lab/src/common/BaseScanner.ts b/packages/shader-lab/src/common/BaseScanner.ts index 5b02703d5f..f63624f3d2 100644 --- a/packages/shader-lab/src/common/BaseScanner.ts +++ b/packages/shader-lab/src/common/BaseScanner.ts @@ -1,6 +1,6 @@ import { ETokenType, ShaderRange, ShaderPosition } from "."; // #if _EDITOR -import { ScannerError } from "../Error"; +import { GSError, GSErrorName } from "../Error"; // #endif import { ShaderLab } from "../ShaderLab"; import { BaseToken } from "./BaseToken"; @@ -130,7 +130,7 @@ export default class BaseScanner { throwError(pos: ShaderPosition | ShaderRange, ...msgs: any[]) { // #if _EDITOR - const error = new ScannerError(msgs.join(" "), pos, this._source); + const error = new GSError(GSErrorName.ScannerError, msgs.join(" "), pos, this._source); error.log(); throw error; // #else diff --git a/packages/shader-lab/src/common/ShaderRange.ts b/packages/shader-lab/src/common/ShaderRange.ts index e79d4143e0..6ead5e85d0 100644 --- a/packages/shader-lab/src/common/ShaderRange.ts +++ b/packages/shader-lab/src/common/ShaderRange.ts @@ -5,7 +5,7 @@ export class ShaderRange implements IPoolElement { public start: ShaderPosition; public end: ShaderPosition; - setX(start: ShaderPosition, end: ShaderPosition) { + set(start: ShaderPosition, end: ShaderPosition) { this.start = start; this.end = end; } diff --git a/packages/shader-lab/src/contentParser/ShaderContentParser.ts b/packages/shader-lab/src/contentParser/ShaderContentParser.ts index 27677cd605..12aef26943 100644 --- a/packages/shader-lab/src/contentParser/ShaderContentParser.ts +++ b/packages/shader-lab/src/contentParser/ShaderContentParser.ts @@ -24,7 +24,7 @@ import { IRenderStates } from "@galacean/engine-design"; // #if _EDITOR -import { CompilationError } from "../Error"; +import { GSError, GSErrorName } from "../Error"; // #endif const EngineType = [ @@ -52,7 +52,7 @@ const RenderStateType = [ export class ShaderContentParser { static _engineType = { RenderQueueType, CompareFunction, StencilOperation, BlendOperation, BlendFactor, CullMode }; - static _errors: CompilationError[] = []; + static _errors: GSError[] = []; private static _isRenderStateDeclarator(token: BaseToken) { return RenderStateType.includes(token.type); @@ -184,7 +184,8 @@ export class ShaderContentParser { if (!sm?.value) { // #if _EDITOR this._errors.push( - new CompilationError( + new GSError( + GSErrorName.CompilationError, `Invalid "${stateToken.lexeme}" variable: ${variable.lexeme}`, variable.location, scanner.source @@ -243,7 +244,8 @@ export class ShaderContentParser { } else if (op.lexeme !== "=") { // #if _EDITOR this._errors.push( - new CompilationError( + new GSError( + GSErrorName.CompilationError, `Invalid syntax, expect character '=', but got ${op.lexeme}`, scanner.curPosition, scanner.source @@ -263,7 +265,12 @@ export class ShaderContentParser { if (renderStateElementKey == undefined) { // #if _EDITOR this._errors.push( - new CompilationError(`Invalid render state element ${renderStateProp}`, scanner.curPosition, scanner.source) + new GSError( + GSErrorName.CompilationError, + `Invalid render state element ${renderStateProp}`, + scanner.curPosition, + scanner.source + ) ); scanner.scanToCharacter(";"); return; @@ -301,7 +308,8 @@ export class ShaderContentParser { if (value == undefined) { // #if _EDITOR this._errors.push( - new CompilationError( + new GSError( + GSErrorName.CompilationError, `Invalid engine constant: ${token.lexeme}.${engineTypeProp.lexeme}`, engineTypeProp.location, scanner.source @@ -332,7 +340,9 @@ export class ShaderContentParser { const value = ShaderContentParser._engineType.RenderQueueType[word.lexeme]; if (value == undefined) { // #if _EDITOR - this._errors.push(new CompilationError(`Invalid render queue ${word.lexeme}`, word.location, scanner.source)); + this._errors.push( + new GSError(GSErrorName.CompilationError, `Invalid render queue ${word.lexeme}`, word.location, scanner.source) + ); return; // #else throw new Error(`Invalid render queue ${word.lexeme}`); @@ -483,7 +493,12 @@ export class ShaderContentParser { const entry = scanner.scanToken(); if (ret[word.lexeme]) { // #if _EDITOR - const error = new CompilationError("reassign main entry", scanner.curPosition, scanner.source); + const error = new GSError( + GSErrorName.CompilationError, + "reassign main entry", + scanner.curPosition, + scanner.source + ); error.log(); throw error; // #else diff --git a/packages/shader-lab/src/parser/SemanticAnalyzer.ts b/packages/shader-lab/src/parser/SemanticAnalyzer.ts index f0f2d69886..8220537133 100644 --- a/packages/shader-lab/src/parser/SemanticAnalyzer.ts +++ b/packages/shader-lab/src/parser/SemanticAnalyzer.ts @@ -1,7 +1,7 @@ import { ShaderRange } from "../common"; import { TreeNode } from "./AST"; // #if _EDITOR -import { CompilationError } from "../Error"; +import { GSError, GSErrorName } from "../Error"; // #endif import { ShaderData } from "./ShaderInfo"; import { SymbolInfo, SymbolTable } from "../parser/symbolTable"; @@ -23,7 +23,7 @@ export default class SematicAnalyzer { private _shaderData = new ShaderData(); // #if _EDITOR - readonly errors: CompilationError[] = []; + readonly errors: GSError[] = []; // #endif get shaderData() { @@ -65,7 +65,7 @@ export default class SematicAnalyzer { error(loc: ShaderRange, ...param: any[]) { // #if _EDITOR - const err = new CompilationError(param.join(""), loc, ShaderLab._processingPassText); + const err = new GSError(GSErrorName.CompilationError, param.join(""), loc, ShaderLab._processingPassText); this.errors.push(err); return err; // #else diff --git a/packages/shader-lab/src/parser/ShaderTargetParser.ts b/packages/shader-lab/src/parser/ShaderTargetParser.ts index 9edf858b07..fe681d0b6a 100644 --- a/packages/shader-lab/src/parser/ShaderTargetParser.ts +++ b/packages/shader-lab/src/parser/ShaderTargetParser.ts @@ -11,7 +11,7 @@ import { LALR1 } from "../lalr"; import { ParserUtils } from "../Utils"; import { Logger } from "@galacean/engine"; // #if _EDITOR -import { CompilationError } from "../Error"; +import { GSError, GSErrorName } from "../Error"; // #endif import { ShaderLab } from "../ShaderLab"; @@ -116,7 +116,12 @@ export class ShaderTargetParser { } else { // #if _EDITOR this.sematicAnalyzer.errors.push( - new CompilationError(`Unexpected token ${token.lexeme}`, token.location, ShaderLab._processingPassText) + new GSError( + GSErrorName.CompilationError, + `Unexpected token ${token.lexeme}`, + token.location, + ShaderLab._processingPassText + ) ); return null; // #else diff --git a/packages/shader-lab/src/preprocessor/MacroDefine.ts b/packages/shader-lab/src/preprocessor/MacroDefine.ts index 8149799287..638b0b828d 100644 --- a/packages/shader-lab/src/preprocessor/MacroDefine.ts +++ b/packages/shader-lab/src/preprocessor/MacroDefine.ts @@ -1,6 +1,6 @@ import { BaseToken } from "../common/BaseToken"; import { ShaderRange } from "../common"; -import { PreprocessorError } from "../Error"; +import { GSError, GSErrorName } from "../Error"; export class MacroDefine { readonly location?: ShaderRange; @@ -29,7 +29,7 @@ export class MacroDefine { // #if _EDITOR if (args.length !== this.args?.length) { - throw new PreprocessorError("mismatched function macro", this.location, ""); + throw new GSError(GSErrorName.PreprocessorError, "mismatched function macro", this.location, ""); } // #endif const replaceRegex = new RegExp(`\\b(${argsTextList.join("|")})\\b`, "g"); diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 55581c7274..cb89f55169 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -3,7 +3,7 @@ import LexerUtils from "../lexer/Utils"; import { MacroDefine } from "./MacroDefine"; // #if _EDITOR import PpSourceMap, { BlockInfo } from "./sourceMap"; -import { PreprocessorError } from "../Error"; +import { GSError, GSErrorName } from "../Error"; // #endif import { BaseToken } from "../common/BaseToken"; import { EPpKeyword, EPpToken, PpConstant } from "./constants"; @@ -108,7 +108,7 @@ export class PpParser { private static reportError(loc: ShaderRange | ShaderPosition, message: string, source: string, file: string) { // #if _EDITOR - this._errors.push(new PreprocessorError(message, loc, source, file)); + this._errors.push(new GSError(GSErrorName.PreprocessorError, message, loc, source, file)); // #else throw new Error(message); // #endif From b7da54ebf9c344d06c4b17118bbcf534bf6677df Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 8 Oct 2024 19:10:31 +0800 Subject: [PATCH 077/131] feat: code opt --- packages/shader-lab/src/ShaderLab.ts | 2 +- .../shader-lab/src/common/ShaderPosition.ts | 2 +- tests/src/shader-lab/ShaderLab.test.ts | 44 +++++++++++-------- .../shaders/compilation-error.shader | 4 +- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index b4a47d7fd9..13aac817f5 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -25,7 +25,7 @@ export class ShaderLab implements IShaderLab { static createPosition(index: number, line?: number, column?: number): ShaderPosition { const position = this._shaderPositionPool.get(); - position.setX(index, line, column); + position.set(index, line, column); return position; } diff --git a/packages/shader-lab/src/common/ShaderPosition.ts b/packages/shader-lab/src/common/ShaderPosition.ts index f6cb2b19d0..01d6babc41 100644 --- a/packages/shader-lab/src/common/ShaderPosition.ts +++ b/packages/shader-lab/src/common/ShaderPosition.ts @@ -5,7 +5,7 @@ export class ShaderPosition implements IPoolElement { line: number; column: number; - setX(index: number, line: number, column: number) { + set(index: number, line: number, column: number) { this.index = index; this.line = line; this.column = column; diff --git a/tests/src/shader-lab/ShaderLab.test.ts b/tests/src/shader-lab/ShaderLab.test.ts index 6b7d887adb..56c635adac 100644 --- a/tests/src/shader-lab/ShaderLab.test.ts +++ b/tests/src/shader-lab/ShaderLab.test.ts @@ -1,6 +1,6 @@ import { BlendOperation, CompareFunction, CullMode, RenderStateDataKey } from "@galacean/engine-core"; import { Color } from "@galacean/engine-math"; -import { ShaderLab as ShaderLabEditor, CompilationError } from "@galacean/engine-shader-lab/verbose"; +import { ShaderLab as ShaderLabVerbose, GSError } from "@galacean/engine-shader-lab/verbose"; import { ShaderLab as ShaderLabRelease } from "@galacean/engine-shader-lab"; import { glslValidate, shaderParse } from "./ShaderValidate"; @@ -104,7 +104,7 @@ vec4 linearToGamma(vec4 linearIn){ #endif `; -const shaderLabEditor = new ShaderLabEditor(); +const shaderLabVerbose = new ShaderLabVerbose(); const shaderLabRelease = new ShaderLabRelease(); describe("ShaderLab", () => { @@ -114,7 +114,7 @@ describe("ShaderLab", () => { let pass1: IShaderContent["subShaders"][number]["passes"][number]; before(() => { - shader = shaderLabEditor._parseShaderContent(demoShader); + shader = shaderLabVerbose._parseShaderContent(demoShader); subShader = shader.subShaders[0]; passList = subShader.passes; expect(passList[0].isUsePass).to.be.true; @@ -123,7 +123,7 @@ describe("ShaderLab", () => { }); it("create shaderLab", async () => { - expect(shaderLabEditor).not.be.null; + expect(shaderLabVerbose).not.be.null; }); it("shader name", () => { @@ -182,75 +182,81 @@ describe("ShaderLab", () => { }); it("engine shader", async () => { - glslValidate(demoShader, shaderLabEditor); + glslValidate(demoShader, shaderLabVerbose); glslValidate(demoShader, shaderLabRelease); }); it("include", () => { const demoShader = fs.readFileSync(path.join(__dirname, "shaders/unlit.shader")).toString(); - glslValidate(demoShader, shaderLabEditor, { test_common: commonSource }); + glslValidate(demoShader, shaderLabVerbose, { test_common: commonSource }); }); it("planarShadow shader", () => { const demoShader = fs.readFileSync(path.join(__dirname, "shaders/planarShadow.shader")).toString(); - glslValidate(demoShader, shaderLabEditor); + glslValidate(demoShader, shaderLabVerbose); glslValidate(demoShader, shaderLabRelease); }); it("Empty macro shader", () => { const demoShader = fs.readFileSync(path.join(__dirname, "shaders/triangle.shader")).toString(); - glslValidate(demoShader, shaderLabEditor); + glslValidate(demoShader, shaderLabVerbose); glslValidate(demoShader, shaderLabRelease); }); it("No frag shader args", () => { const demoShader = fs.readFileSync(path.join(__dirname, "shaders/noFragArgs.shader")).toString(); - glslValidate(demoShader, shaderLabEditor); + glslValidate(demoShader, shaderLabVerbose); glslValidate(demoShader, shaderLabRelease); }); it("water full shader(complex)", () => { const demoShader = fs.readFileSync(path.join(__dirname, "shaders/waterfull.shader")).toString(); - glslValidate(demoShader, shaderLabEditor); + glslValidate(demoShader, shaderLabVerbose); glslValidate(demoShader, shaderLabRelease); }); it("glass shader", () => { const demoShader = fs.readFileSync(path.join(__dirname, "shaders/glass.shader")).toString(); - glslValidate(demoShader, shaderLabEditor); + glslValidate(demoShader, shaderLabVerbose); glslValidate(demoShader, shaderLabRelease); }); it("template shader", () => { const demoShader = fs.readFileSync(path.join(__dirname, "shaders/template.shader")).toString(); - glslValidate(demoShader, shaderLabEditor); + glslValidate(demoShader, shaderLabVerbose); glslValidate(demoShader, shaderLabRelease); }); it("multi-pass", () => { const shaderSource = fs.readFileSync(path.join(__dirname, "shaders/multi-pass.shader")).toString(); - glslValidate(shaderSource, shaderLabEditor); + glslValidate(shaderSource, shaderLabVerbose); glslValidate(shaderSource, shaderLabRelease); }); it("macro-with-preprocessor", () => { const shaderSource = fs.readFileSync(path.join(__dirname, "shaders/macro-pre.shader")).toString(); - glslValidate(shaderSource, shaderLabEditor); + glslValidate(shaderSource, shaderLabVerbose); glslValidate(shaderSource, shaderLabRelease); }); it("compilation-error", () => { const errorShader = fs.readFileSync(path.join(__dirname, "shaders/compilation-error.shader")).toString(); // @ts-ignore - shaderLabEditor._parse(errorShader); + shaderLabVerbose._parse(errorShader); // @ts-ignore - expect(shaderLabEditor._errors.length).to.eq(3); + expect(shaderLabVerbose._errors.length).to.eq(3); // @ts-ignore - assert.instanceOf(shaderLabEditor._errors[0], CompilationError); + assert.instanceOf(shaderLabVerbose._errors[0], GSError); // @ts-ignore - assert.instanceOf(shaderLabEditor._errors[1], CompilationError); + assert.instanceOf(shaderLabVerbose._errors[1], GSError); // @ts-ignore - assert.instanceOf(shaderLabEditor._errors[2], CompilationError); + assert.instanceOf(shaderLabVerbose._errors[2], GSError); + + debugger; + // @ts-ignore + for (const err of shaderLabVerbose._errors) { + console.log(err.toString()); + } expect(shaderParse.bind(shaderLabRelease)).to.throw(Error); }); diff --git a/tests/src/shader-lab/shaders/compilation-error.shader b/tests/src/shader-lab/shaders/compilation-error.shader index 2f5f66fed5..09216b6cf4 100644 --- a/tests/src/shader-lab/shaders/compilation-error.shader +++ b/tests/src/shader-lab/shaders/compilation-error.shader @@ -64,7 +64,9 @@ Shader "custom/pbr" { Varyings v; gl_Position = renderer_MVPMat * attr2.POSITION; - none(); + none( + 12 + ); v.v_pos = gl_Position.xyz; v.v_uv = attr.TEXCOORD_023; return v; From 59776c45db983f54889bfd5f81b77c865cebb286 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 8 Oct 2024 19:30:39 +0800 Subject: [PATCH 078/131] feat: _EDITOR to _VERBOSE --- packages/shader-lab/src/Error.ts | 2 +- packages/shader-lab/src/ShaderLab.ts | 14 ++-- packages/shader-lab/src/Utils.ts | 6 +- .../shader-lab/src/codeGen/CodeGenVisitor.ts | 4 +- .../shader-lab/src/codeGen/VisitorContext.ts | 6 +- packages/shader-lab/src/common/BaseScanner.ts | 4 +- .../src/contentParser/ShaderContentParser.ts | 14 ++-- packages/shader-lab/src/index.ts | 4 +- packages/shader-lab/src/lalr/CFG.ts | 62 ++++++++--------- packages/shader-lab/src/lalr/LALR1.ts | 2 +- packages/shader-lab/src/lalr/StateItem.ts | 4 +- packages/shader-lab/src/lexer/Lexer.ts | 2 +- packages/shader-lab/src/parser/AST.ts | 62 ++++++++--------- .../shader-lab/src/parser/SemanticAnalyzer.ts | 8 +-- .../src/parser/ShaderTargetParser.ts | 8 +-- .../shader-lab/src/parser/builtin/index.ts | 2 +- .../src/preprocessor/MacroDefine.ts | 2 +- .../shader-lab/src/preprocessor/PpParser.ts | 68 +++++++++---------- .../shader-lab/src/preprocessor/PpScanner.ts | 8 +-- .../src/preprocessor/Preprocessor.ts | 2 +- packages/shader-lab/src/preprocessor/Utils.ts | 6 +- .../src/preprocessor/sourceMap/index.ts | 2 +- rollup.config.js | 2 +- 23 files changed, 147 insertions(+), 147 deletions(-) diff --git a/packages/shader-lab/src/Error.ts b/packages/shader-lab/src/Error.ts index b18414f13b..b936cb66b7 100644 --- a/packages/shader-lab/src/Error.ts +++ b/packages/shader-lab/src/Error.ts @@ -1,4 +1,4 @@ -// #if _EDITOR +// #if _VERBOSE import { Logger } from "@galacean/engine"; import { ShaderPosition, ShaderRange } from "./common"; diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index 13aac817f5..55e87e7d66 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -8,7 +8,7 @@ import { ShaderContentParser } from "./contentParser"; import { Logger, ShaderLib, ShaderMacro, ShaderPass, ShaderPlatformTarget } from "@galacean/engine"; import { ShaderPosition, ShaderRange } from "./common"; import { ShaderLabObjectPool } from "./ShaderLabObjectPool"; -// #if _EDITOR +// #if _VERBOSE import { GSError } from "./Error"; // #endif import { PpParser } from "./preprocessor/PpParser"; @@ -35,7 +35,7 @@ export class ShaderLab implements IShaderLab { return range; } - // #if _EDITOR + // #if _VERBOSE private _errors: GSError[] = []; /** @@ -66,7 +66,7 @@ export class ShaderLab implements IShaderLab { const preprocessorStart = performance.now(); const ppdContent = Preprocessor.process(source); - // #if _EDITOR + // #if _VERBOSE if (PpParser._errors.length > 0) { for (const err of PpParser._errors) { this._errors.push(err); @@ -86,7 +86,7 @@ export class ShaderLab implements IShaderLab { ShaderLab._processingPassText = ppdContent; const program = parser.parse(tokens); - // #if _EDITOR + // #if _VERBOSE for (const err of parser.errors) { this._errors.push(err); } @@ -104,7 +104,7 @@ export class ShaderLab implements IShaderLab { Logger.info(`[CodeGen] cost time: ${performance.now() - start}ms`); ShaderLab._processingPassText = undefined; - // #if _EDITOR + // #if _VERBOSE for (const err of codeGen.errors) { this._errors.push(err); } @@ -117,7 +117,7 @@ export class ShaderLab implements IShaderLab { _parseShaderContent(shaderSource: string): IShaderContent { ShaderLabObjectPool.clearAllShaderLabObjectPool(); ShaderContentParser.reset(); - // #if _EDITOR + // #if _VERBOSE this._errors.length = 0; // #endif const ret = ShaderContentParser.parse(shaderSource); @@ -127,7 +127,7 @@ export class ShaderLab implements IShaderLab { return ret; } - // #if _EDITOR + // #if _VERBOSE /** * @internal * For debug diff --git a/packages/shader-lab/src/Utils.ts b/packages/shader-lab/src/Utils.ts index a84aadaea2..a269ef3199 100644 --- a/packages/shader-lab/src/Utils.ts +++ b/packages/shader-lab/src/Utils.ts @@ -2,7 +2,7 @@ import { ENonTerminal, GrammarSymbol } from "./parser/GrammarSymbol"; import { BaseToken as Token } from "./common/BaseToken"; import { EKeyword, ETokenType, GalaceanDataType } from "./common"; import { TreeNode } from "./parser/AST"; -// #if _EDITOR +// #if _VERBOSE import State from "./lalr/State"; // #endif @@ -14,7 +14,7 @@ export class ParserUtils { return ParserUtils.unwrapNodeByType(child, type); } - // #if _EDITOR + // #if _VERBOSE /** * Check if type `tb` is compatible with type `ta`. */ @@ -41,7 +41,7 @@ export class ParserUtils { /** * @internal */ - // #if _EDITOR + // #if _VERBOSE static printStatePool(logPath: string) { let output = ""; diff --git a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts index 117a856f0e..6c6fee34f6 100644 --- a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts +++ b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts @@ -6,7 +6,7 @@ import { ESymbolType, FnSymbol, VarSymbol } from "../parser/symbolTable"; import { ParserUtils } from "../Utils"; import { NodeChild } from "../parser/types"; import { VisitorContext } from "./VisitorContext"; -// #if _EDITOR +// #if _VERBOSE import { GSErrorName, GSError } from "../Error"; // #endif import { ShaderLab } from "../ShaderLab"; @@ -190,7 +190,7 @@ export class CodeGenVisitor { protected reportError(loc: ShaderRange | ShaderPosition, message: string): GSError { let error: Error; - // #if _EDITOR + // #if _VERBOSE error = new GSError(GSErrorName.CompilationError, message, loc, ShaderLab._processingPassText); this._errors.push(error); return error; diff --git a/packages/shader-lab/src/codeGen/VisitorContext.ts b/packages/shader-lab/src/codeGen/VisitorContext.ts index 61c57eedb5..536e7f9e6a 100644 --- a/packages/shader-lab/src/codeGen/VisitorContext.ts +++ b/packages/shader-lab/src/codeGen/VisitorContext.ts @@ -2,7 +2,7 @@ import { EShaderStage } from "../common/Enums"; import { ASTNode } from "../parser/AST"; import { ESymbolType, SymbolTable, SymbolInfo } from "../parser/symbolTable"; import { IParamInfo } from "../parser/types"; -// #if _EDITOR +// #if _VERBOSE import { GSErrorName, GSError } from "../Error"; // #endif import { BaseToken } from "../common/BaseToken"; @@ -63,7 +63,7 @@ export class VisitorContext { const prop = this.attributeList.find((item) => item.ident.lexeme === ident.lexeme); if (!prop) { - // #if _EDITOR + // #if _VERBOSE return new GSError( GSErrorName.CompilationError, `referenced attribute not found: ${ident.lexeme}`, @@ -83,7 +83,7 @@ export class VisitorContext { const prop = this.varyingStruct?.propList.find((item) => item.ident.lexeme === ident.lexeme); if (!prop) { - // #if _EDITOR + // #if _VERBOSE return new GSError( GSErrorName.CompilationError, `referenced varying not found: ${ident.lexeme}`, diff --git a/packages/shader-lab/src/common/BaseScanner.ts b/packages/shader-lab/src/common/BaseScanner.ts index f63624f3d2..8eab44fddb 100644 --- a/packages/shader-lab/src/common/BaseScanner.ts +++ b/packages/shader-lab/src/common/BaseScanner.ts @@ -1,5 +1,5 @@ import { ETokenType, ShaderRange, ShaderPosition } from "."; -// #if _EDITOR +// #if _VERBOSE import { GSError, GSErrorName } from "../Error"; // #endif import { ShaderLab } from "../ShaderLab"; @@ -129,7 +129,7 @@ export default class BaseScanner { } throwError(pos: ShaderPosition | ShaderRange, ...msgs: any[]) { - // #if _EDITOR + // #if _VERBOSE const error = new GSError(GSErrorName.ScannerError, msgs.join(" "), pos, this._source); error.log(); throw error; diff --git a/packages/shader-lab/src/contentParser/ShaderContentParser.ts b/packages/shader-lab/src/contentParser/ShaderContentParser.ts index 12aef26943..c4adc5cb66 100644 --- a/packages/shader-lab/src/contentParser/ShaderContentParser.ts +++ b/packages/shader-lab/src/contentParser/ShaderContentParser.ts @@ -23,7 +23,7 @@ import { IShaderPassContent, IRenderStates } from "@galacean/engine-design"; -// #if _EDITOR +// #if _VERBOSE import { GSError, GSErrorName } from "../Error"; // #endif @@ -182,7 +182,7 @@ export class ShaderContentParser { scanner.scanText(";"); const sm = this._symbolTable.lookup({ type: stateToken.type, ident: variable.lexeme }); if (!sm?.value) { - // #if _EDITOR + // #if _VERBOSE this._errors.push( new GSError( GSErrorName.CompilationError, @@ -242,7 +242,7 @@ export class ShaderContentParser { scanner.scanText("]"); scanner.scanText("="); } else if (op.lexeme !== "=") { - // #if _EDITOR + // #if _VERBOSE this._errors.push( new GSError( GSErrorName.CompilationError, @@ -263,7 +263,7 @@ export class ShaderContentParser { renderStateProp = state + renderStateProp; const renderStateElementKey = RenderStateDataKey[renderStateProp]; if (renderStateElementKey == undefined) { - // #if _EDITOR + // #if _VERBOSE this._errors.push( new GSError( GSErrorName.CompilationError, @@ -306,7 +306,7 @@ export class ShaderContentParser { const engineTypeProp = scanner.scanToken(); value = ShaderContentParser._engineType[token.lexeme]?.[engineTypeProp.lexeme]; if (value == undefined) { - // #if _EDITOR + // #if _VERBOSE this._errors.push( new GSError( GSErrorName.CompilationError, @@ -339,7 +339,7 @@ export class ShaderContentParser { scanner.scanText(";"); const value = ShaderContentParser._engineType.RenderQueueType[word.lexeme]; if (value == undefined) { - // #if _EDITOR + // #if _VERBOSE this._errors.push( new GSError(GSErrorName.CompilationError, `Invalid render queue ${word.lexeme}`, word.location, scanner.source) ); @@ -492,7 +492,7 @@ export class ShaderContentParser { scanner.scanText("="); const entry = scanner.scanToken(); if (ret[word.lexeme]) { - // #if _EDITOR + // #if _VERBOSE const error = new GSError( GSErrorName.CompilationError, "reassign main entry", diff --git a/packages/shader-lab/src/index.ts b/packages/shader-lab/src/index.ts index b163472751..bf8254f312 100644 --- a/packages/shader-lab/src/index.ts +++ b/packages/shader-lab/src/index.ts @@ -1,6 +1,6 @@ export { ShaderLab } from "./ShaderLab"; -// #if _EDITOR +// #if _VERBOSE export { Preprocessor } from "./preprocessor"; export * from "./Error"; // #endif @@ -9,7 +9,7 @@ export * from "./Error"; export const version = `__buildVersion`; let mode = "Release"; -// #if _EDITOR +// #if _VERBOSE mode = "Verbose"; // #endif diff --git a/packages/shader-lab/src/lalr/CFG.ts b/packages/shader-lab/src/lalr/CFG.ts index f199f95b00..01ff210182 100644 --- a/packages/shader-lab/src/lalr/CFG.ts +++ b/packages/shader-lab/src/lalr/CFG.ts @@ -121,7 +121,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ ...GrammarUtils.createProductionWithOptions( ENonTerminal.storage_qualifier, [[EKeyword.CONST], [EKeyword.IN], [EKeyword.INOUT], [EKeyword.OUT], [EKeyword.CENTROID]], - // #if _EDITOR + // #if _VERBOSE ASTNode.StorageQualifier.pool // #endif ), @@ -129,7 +129,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ ...GrammarUtils.createProductionWithOptions( ENonTerminal.interpolation_qualifier, [[EKeyword.SMOOTH], [EKeyword.FLAT]], - // #if _EDITOR + // #if _VERBOSE ASTNode.InterpolationQualifier.pool // #endif ), @@ -137,7 +137,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ ...GrammarUtils.createProductionWithOptions( ENonTerminal.invariant_qualifier, [[EKeyword.INVARIANT]], - // #if _EDITOR + // #if _VERBOSE ASTNode.InvariantQualifier.pool // #endif ), @@ -145,7 +145,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ ...GrammarUtils.createProductionWithOptions( ENonTerminal.precision_qualifier, [[EKeyword.HIGHP], [EKeyword.MEDIUMP], [EKeyword.LOWP]], - // #if _EDITOR + // #if _VERBOSE ASTNode.PrecisionQualifier.pool // #endif ), @@ -253,7 +253,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ ENonTerminal.assignment_expression ] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.ConditionalExpression.pool // #endif ), @@ -264,7 +264,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ [ENonTerminal.logical_xor_expression], [ENonTerminal.logical_or_expression, ETokenType.OR_OP, ENonTerminal.logical_xor_expression] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.LogicalOrExpression.pool // #endif ), @@ -275,7 +275,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ [ENonTerminal.logical_and_expression], [ENonTerminal.logical_xor_expression, ETokenType.XOR_OP, ENonTerminal.logical_and_expression] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.LogicalXorExpression.pool // #endif ), @@ -286,7 +286,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ [ENonTerminal.inclusive_or_expression], [ENonTerminal.logical_and_expression, ETokenType.AND_OP, ENonTerminal.inclusive_or_expression] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.LogicalAndExpression.pool // #endif ), @@ -297,7 +297,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ [ENonTerminal.exclusive_or_expression], [ENonTerminal.inclusive_or_expression, ETokenType.VERTICAL_BAR, ENonTerminal.exclusive_or_expression] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.InclusiveOrExpression.pool // #endif ), @@ -308,7 +308,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ [ENonTerminal.and_expression], [ENonTerminal.exclusive_or_expression, ETokenType.CARET, ENonTerminal.and_expression] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.ExclusiveOrExpression.pool // #endif ), @@ -319,7 +319,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ [ENonTerminal.equality_expression], [ENonTerminal.and_expression, ETokenType.AMPERSAND, ENonTerminal.equality_expression] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.AndExpression.pool // #endif ), @@ -331,7 +331,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ [ENonTerminal.equality_expression, ETokenType.EQ_OP, ENonTerminal.relational_expression], [ENonTerminal.equality_expression, ETokenType.NE_OP, ENonTerminal.relational_expression] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.EqualityExpression.pool // #endif ), @@ -345,7 +345,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ [ENonTerminal.relational_expression, ETokenType.LE_OP, ENonTerminal.shift_expression], [ENonTerminal.relational_expression, ETokenType.GE_OP, ENonTerminal.shift_expression] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.RelationalExpression.pool // #endif ), @@ -357,7 +357,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ [ENonTerminal.shift_expression, ETokenType.LEFT_OP, ENonTerminal.additive_expression], [ENonTerminal.shift_expression, ETokenType.RIGHT_OP, ENonTerminal.additive_expression] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.ShiftExpression.pool // #endif ), @@ -369,7 +369,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ [ENonTerminal.additive_expression, ETokenType.PLUS, ENonTerminal.multiplicative_expression], [ENonTerminal.additive_expression, ETokenType.DASH, ENonTerminal.multiplicative_expression] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.AdditiveExpression.pool // #endif ), @@ -382,7 +382,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ [ENonTerminal.multiplicative_expression, ETokenType.SLASH, ENonTerminal.unary_expression], [ENonTerminal.multiplicative_expression, ETokenType.PERCENT, ENonTerminal.unary_expression] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.MultiplicativeExpression.pool // #endif ), @@ -395,7 +395,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ [ETokenType.DEC_OP, ENonTerminal.unary_expression], [ENonTerminal.unary_operator, ENonTerminal.unary_expression] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.UnaryExpression.pool // #endif ), @@ -403,7 +403,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ ...GrammarUtils.createProductionWithOptions( ENonTerminal.unary_operator, [[ETokenType.PLUS], [ETokenType.DASH], [ETokenType.BANG], [ETokenType.TILDE]], - // #if _EDITOR + // #if _VERBOSE ASTNode.UnaryOperator.pool // #endif ), @@ -468,7 +468,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ [ETokenType.XOR_ASSIGN], [ETokenType.OR_ASSIGN] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.AssignmentOperator.pool // #endif ), @@ -566,7 +566,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ ...GrammarUtils.createProductionWithOptions( ENonTerminal.statement, [[ENonTerminal.compound_statement], [ENonTerminal.simple_statement]], - // #if _EDITOR + // #if _VERBOSE ASTNode.Statement.pool // #endif ), @@ -586,7 +586,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ [ETokenType.LEFT_BRACE, ETokenType.RIGHT_BRACE], [ENonTerminal.scope_brace, ENonTerminal.statement_list, ENonTerminal.scope_end_brace] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.CompoundStatement.pool // #endif ), @@ -600,7 +600,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ [ENonTerminal.iteration_statement], [ENonTerminal.jump_statement] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.SimpleStatement.pool // #endif ), @@ -673,7 +673,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ [ENonTerminal.assignment_expression], [ETokenType.LEFT_BRACE, ENonTerminal.initializer_list, ETokenType.RIGHT_BRACE] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.Initializer.pool // #endif ), @@ -681,7 +681,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ ...GrammarUtils.createProductionWithOptions( ENonTerminal.initializer_list, [[ENonTerminal.initializer], [ENonTerminal.initializer_list, ETokenType.COMMA, ENonTerminal.initializer]], - // #if _EDITOR + // #if _VERBOSE ASTNode.InitializerList.pool // #endif ), @@ -689,7 +689,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ ...GrammarUtils.createProductionWithOptions( ENonTerminal.expression_statement, [[ETokenType.SEMICOLON], [ENonTerminal.expression, ETokenType.SEMICOLON]], - // #if _EDITOR + // #if _VERBOSE ASTNode.ExpressionStatement.pool // #endif ), @@ -709,7 +709,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ ENonTerminal.statement ] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.SelectionStatement.pool // #endif ), @@ -727,7 +727,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ ENonTerminal.statement ] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.IterationStatement.pool // #endif ), @@ -748,7 +748,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ ...GrammarUtils.createProductionWithOptions( ENonTerminal.for_init_statement, [[ENonTerminal.expression_statement], [ENonTerminal.declaration]], - // #if _EDITOR + // #if _VERBOSE ASTNode.ForInitStatement.pool // #endif ), @@ -759,7 +759,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ [ENonTerminal.expression], [ENonTerminal.fully_specified_type, ETokenType.ID, ETokenType.EQUAL, ENonTerminal.initializer] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.Condition.pool // #endif ), @@ -770,7 +770,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ [ENonTerminal.conditionopt, ETokenType.SEMICOLON], [ENonTerminal.conditionopt, ETokenType.SEMICOLON, ENonTerminal.expression] ], - // #if _EDITOR + // #if _VERBOSE ASTNode.ForRestStatement.pool // #endif ), @@ -778,7 +778,7 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [ ...GrammarUtils.createProductionWithOptions( ENonTerminal.conditionopt, [[ETokenType.EPSILON], [ENonTerminal.condition]], - // #if _EDITOR + // #if _VERBOSE ASTNode.ConditionOpt.pool // #endif ), diff --git a/packages/shader-lab/src/lalr/LALR1.ts b/packages/shader-lab/src/lalr/LALR1.ts index ac489891e4..b83eb78f60 100644 --- a/packages/shader-lab/src/lalr/LALR1.ts +++ b/packages/shader-lab/src/lalr/LALR1.ts @@ -161,7 +161,7 @@ export class LALR1 { if (terminal === EKeyword.ELSE && exist.action === EAction.Shift && action.action === EAction.Reduce) { return; } else { - // #if _EDITOR + // #if _VERBOSE console.warn( `conflict detect: `, Utils.printAction(exist), diff --git a/packages/shader-lab/src/lalr/StateItem.ts b/packages/shader-lab/src/lalr/StateItem.ts index 16e7e66fd1..ccf6bbb845 100644 --- a/packages/shader-lab/src/lalr/StateItem.ts +++ b/packages/shader-lab/src/lalr/StateItem.ts @@ -59,13 +59,13 @@ export default class StateItem { } advance() { - // #if _EDITOR + // #if _VERBOSE if (this.canReduce()) throw `Error: advance reduce-able parsing state item`; // #endif return new StateItem(this.production, this.position + 1, this.lookaheadSet); } - // #if _EDITOR + // #if _VERBOSE toString() { const coreItem = this.production.derivation.map((item) => GrammarUtils.toString(item)); coreItem[this.position] = "." + (coreItem[this.position] ?? ""); diff --git a/packages/shader-lab/src/lexer/Lexer.ts b/packages/shader-lab/src/lexer/Lexer.ts index 003fa459c7..abe0c10a8c 100644 --- a/packages/shader-lab/src/lexer/Lexer.ts +++ b/packages/shader-lab/src/lexer/Lexer.ts @@ -311,7 +311,7 @@ export class Lexer extends BaseScanner { private _getPosition(offset /** offset from starting point */ = 0) { return ShaderLab.createPosition( this.current - offset, - // #if _EDITOR + // #if _VERBOSE this._line, this._column - offset // #endif diff --git a/packages/shader-lab/src/parser/AST.ts b/packages/shader-lab/src/parser/AST.ts index 71745de4d4..7e8ed343aa 100644 --- a/packages/shader-lab/src/parser/AST.ts +++ b/packages/shader-lab/src/parser/AST.ts @@ -1,4 +1,4 @@ -// #if _EDITOR +// #if _VERBOSE import { BuiltinFunction, BuiltinVariable, NonGenericGalaceanType } from "./builtin"; // #endif import { CodeGenVisitor } from "../codeGen"; @@ -101,7 +101,7 @@ export namespace ASTNode { super.set(loc, children, ENonTerminal.jump_statement); } - // #if _EDITOR + // #if _VERBOSE override semanticAnalyze(sa: SematicAnalyzer): void { if (ASTNode._unwrapToken(this.children![0]).type === EKeyword.RETURN) { // TODO: check the equality of function return type declared and this type. @@ -114,7 +114,7 @@ export namespace ASTNode { } } - // #if _EDITOR + // #if _VERBOSE export class ConditionOpt extends TreeNode { static pool = new ShaderLabObjectPool(ConditionOpt); @@ -187,7 +187,7 @@ export namespace ASTNode { } } - // #if _EDITOR + // #if _VERBOSE export class InitializerList extends ExpressionAstNode { static pool = new ShaderLabObjectPool(InitializerList); @@ -335,7 +335,7 @@ export namespace ASTNode { } } - // #if _EDITOR + // #if _VERBOSE export class StorageQualifier extends BasicTypeQualifier { static pool = new ShaderLabObjectPool(StorageQualifier); @@ -455,7 +455,7 @@ export namespace ASTNode { if (child instanceof Token) { this.value = Number(child.lexeme); } - // #if _EDITOR + // #if _VERBOSE else { const id = child as VariableIdentifier; if (!id.symbolInfo) { @@ -533,7 +533,7 @@ export namespace ASTNode { } else if (this.children.length === 4 || this.children.length === 6) { const typeInfo = this.typeInfo; const arraySpecifier = this.children[3] as ArraySpecifier; - // #if _EDITOR + // #if _VERBOSE if (typeInfo.arraySpecifier && arraySpecifier) { sa.error(arraySpecifier.location, "Array of array is not supported."); } @@ -747,7 +747,7 @@ export namespace ASTNode { } } - // #if _EDITOR + // #if _VERBOSE export class SimpleStatement extends TreeNode { static pool = new ShaderLabObjectPool(SimpleStatement); @@ -773,7 +773,7 @@ export namespace ASTNode { } } - // #if _EDITOR + // #if _VERBOSE export class Statement extends TreeNode { static pool = new ShaderLabObjectPool(Statement); @@ -861,7 +861,7 @@ export namespace ASTNode { paramSig = paramList.paramSig as any; } } - // #if _EDITOR + // #if _VERBOSE const builtinFn = BuiltinFunction.getFn(fnIdent, ...(paramSig ?? [])); if (builtinFn) { this.type = BuiltinFunction.getReturnType(builtinFn.fun, builtinFn.genType); @@ -871,7 +871,7 @@ export namespace ASTNode { const fnSymbol = sa.symbolTable.lookup({ ident: fnIdent, symbolType: ESymbolType.FN, signature: paramSig }); if (!fnSymbol) { - // #if _EDITOR + // #if _VERBOSE sa.error(this.location, "No overload function type found: ", functionIdentifier.ident); // #endif return; @@ -964,7 +964,7 @@ export namespace ASTNode { super.set(loc, children, ENonTerminal.assignment_expression); } - // #if _EDITOR + // #if _VERBOSE override semanticAnalyze(sa: SematicAnalyzer): void { if (this.children.length === 1) { const expr = this.children[0] as ConditionalExpression; @@ -977,7 +977,7 @@ export namespace ASTNode { // #endif } - // #if _EDITOR + // #if _VERBOSE export class AssignmentOperator extends TreeNode { static pool = new ShaderLabObjectPool(AssignmentOperator); @@ -994,7 +994,7 @@ export namespace ASTNode { super.set(loc, children, ENonTerminal.expression); } - // #if _EDITOR + // #if _VERBOSE override semanticAnalyze(sa: SematicAnalyzer): void { if (this.children.length === 1) { const expr = this.children[0] as AssignmentExpression; @@ -1056,7 +1056,7 @@ export namespace ASTNode { } } - // #if _EDITOR + // #if _VERBOSE export class UnaryOperator extends TreeNode { static pool = new ShaderLabObjectPool(UnaryOperator); @@ -1066,7 +1066,7 @@ export namespace ASTNode { } // #endif - // #if _EDITOR + // #if _VERBOSE export class UnaryExpression extends ExpressionAstNode { static pool = new ShaderLabObjectPool(UnaryExpression); @@ -1077,7 +1077,7 @@ export namespace ASTNode { } // #endif - // #if _EDITOR + // #if _VERBOSE export class MultiplicativeExpression extends ExpressionAstNode { static pool = new ShaderLabObjectPool(MultiplicativeExpression); @@ -1096,7 +1096,7 @@ export namespace ASTNode { } // #endif - // #if _EDITOR + // #if _VERBOSE export class AdditiveExpression extends ExpressionAstNode { static pool = new ShaderLabObjectPool(AdditiveExpression); @@ -1115,7 +1115,7 @@ export namespace ASTNode { } // #endif - // #if _EDITOR + // #if _VERBOSE export class ShiftExpression extends ExpressionAstNode { static pool = new ShaderLabObjectPool(ShiftExpression); @@ -1130,7 +1130,7 @@ export namespace ASTNode { } // #endif - // #if _EDITOR + // #if _VERBOSE export class RelationalExpression extends ExpressionAstNode { static pool = new ShaderLabObjectPool(RelationalExpression); @@ -1148,7 +1148,7 @@ export namespace ASTNode { } // #endif - // #if _EDITOR + // #if _VERBOSE export class EqualityExpression extends ExpressionAstNode { static pool = new ShaderLabObjectPool(EqualityExpression); @@ -1166,7 +1166,7 @@ export namespace ASTNode { } // #endif - // #if _EDITOR + // #if _VERBOSE export class AndExpression extends ExpressionAstNode { static pool = new ShaderLabObjectPool(AndExpression); @@ -1184,7 +1184,7 @@ export namespace ASTNode { } // #endif - // #if _EDITOR + // #if _VERBOSE export class ExclusiveOrExpression extends ExpressionAstNode { static pool = new ShaderLabObjectPool(ExclusiveOrExpression); @@ -1202,7 +1202,7 @@ export namespace ASTNode { } // #endif - // #if _EDITOR + // #if _VERBOSE export class InclusiveOrExpression extends ExpressionAstNode { static pool = new ShaderLabObjectPool(InclusiveOrExpression); @@ -1220,7 +1220,7 @@ export namespace ASTNode { } // #endif - // #if _EDITOR + // #if _VERBOSE export class LogicalAndExpression extends ExpressionAstNode { static pool = new ShaderLabObjectPool(LogicalAndExpression); @@ -1238,7 +1238,7 @@ export namespace ASTNode { } // #endif - // #if _EDITOR + // #if _VERBOSE export class LogicalXorExpression extends ExpressionAstNode { static pool = new ShaderLabObjectPool(LogicalXorExpression); @@ -1256,7 +1256,7 @@ export namespace ASTNode { } // #endif - // #if _EDITOR + // #if _VERBOSE export class LogicalOrExpression extends ExpressionAstNode { static pool = new ShaderLabObjectPool(LogicalOrExpression); @@ -1274,7 +1274,7 @@ export namespace ASTNode { } // #endif - // #if _EDITOR + // #if _VERBOSE export class ConditionalExpression extends ExpressionAstNode { static pool = new ShaderLabObjectPool(ConditionalExpression); @@ -1421,7 +1421,7 @@ export namespace ASTNode { symbolInfo: | VarSymbol - // #if _EDITOR + // #if _VERBOSE | BuiltinVariable // #endif | null; @@ -1442,7 +1442,7 @@ export namespace ASTNode { override semanticAnalyze(sa: SematicAnalyzer): void { const token = this.children[0] as Token; - // #if _EDITOR + // #if _VERBOSE const builtinVar = BuiltinVariable.getVar(token.lexeme); if (builtinVar) { this.symbolInfo = builtinVar; @@ -1451,7 +1451,7 @@ export namespace ASTNode { // #endif this.symbolInfo = sa.symbolTable.lookup({ ident: token.lexeme, symbolType: ESymbolType.VAR }) as VarSymbol; - // #if _EDITOR + // #if _VERBOSE if (!this.symbolInfo) { sa.error(this.location, "undeclared identifier:", token.lexeme); } diff --git a/packages/shader-lab/src/parser/SemanticAnalyzer.ts b/packages/shader-lab/src/parser/SemanticAnalyzer.ts index 8220537133..891bf27152 100644 --- a/packages/shader-lab/src/parser/SemanticAnalyzer.ts +++ b/packages/shader-lab/src/parser/SemanticAnalyzer.ts @@ -1,6 +1,6 @@ import { ShaderRange } from "../common"; import { TreeNode } from "./AST"; -// #if _EDITOR +// #if _VERBOSE import { GSError, GSErrorName } from "../Error"; // #endif import { ShaderData } from "./ShaderInfo"; @@ -22,7 +22,7 @@ export default class SematicAnalyzer { symbolTable: SymbolTableStack = new SymbolTableStack(); private _shaderData = new ShaderData(); - // #if _EDITOR + // #if _VERBOSE readonly errors: GSError[] = []; // #endif @@ -41,7 +41,7 @@ export default class SematicAnalyzer { this._shaderData = new ShaderData(); this.symbolTable.clear(); this.newScope(); - // #if _EDITOR + // #if _VERBOSE this.errors.length = 0; // #endif } @@ -64,7 +64,7 @@ export default class SematicAnalyzer { } error(loc: ShaderRange, ...param: any[]) { - // #if _EDITOR + // #if _VERBOSE const err = new GSError(GSErrorName.CompilationError, param.join(""), loc, ShaderLab._processingPassText); this.errors.push(err); return err; diff --git a/packages/shader-lab/src/parser/ShaderTargetParser.ts b/packages/shader-lab/src/parser/ShaderTargetParser.ts index fe681d0b6a..b30b18c778 100644 --- a/packages/shader-lab/src/parser/ShaderTargetParser.ts +++ b/packages/shader-lab/src/parser/ShaderTargetParser.ts @@ -10,7 +10,7 @@ import { addTranslationRule, createGrammar } from "../lalr/CFG"; import { LALR1 } from "../lalr"; import { ParserUtils } from "../Utils"; import { Logger } from "@galacean/engine"; -// #if _EDITOR +// #if _VERBOSE import { GSError, GSErrorName } from "../Error"; // #endif import { ShaderLab } from "../ShaderLab"; @@ -35,7 +35,7 @@ export class ShaderTargetParser { return this.gotoTable.get(this.curState); } - // #if _EDITOR + // #if _VERBOSE /** @internal */ get errors() { return this.sematicAnalyzer.errors; @@ -114,7 +114,7 @@ export class ShaderTargetParser { traceBackStack.push(nextState); continue; } else { - // #if _EDITOR + // #if _VERBOSE this.sematicAnalyzer.errors.push( new GSError( GSErrorName.CompilationError, @@ -131,7 +131,7 @@ export class ShaderTargetParser { } } - // #if _EDITOR + // #if _VERBOSE private _printStack(nextToken: BaseToken) { let str = ""; for (let i = 0; i < this._traceBackStack.length - 1; i++) { diff --git a/packages/shader-lab/src/parser/builtin/index.ts b/packages/shader-lab/src/parser/builtin/index.ts index f613228147..618db58261 100644 --- a/packages/shader-lab/src/parser/builtin/index.ts +++ b/packages/shader-lab/src/parser/builtin/index.ts @@ -1,4 +1,4 @@ -// #if _EDITOR +// #if _VERBOSE export * from "./functions"; export * from "./variables"; // #endif diff --git a/packages/shader-lab/src/preprocessor/MacroDefine.ts b/packages/shader-lab/src/preprocessor/MacroDefine.ts index 638b0b828d..dacbb48b2e 100644 --- a/packages/shader-lab/src/preprocessor/MacroDefine.ts +++ b/packages/shader-lab/src/preprocessor/MacroDefine.ts @@ -27,7 +27,7 @@ export class MacroDefine { if (this.isFunction) { const argsTextList = this.args!.map((item) => item.lexeme); - // #if _EDITOR + // #if _VERBOSE if (args.length !== this.args?.length) { throw new GSError(GSErrorName.PreprocessorError, "mismatched function macro", this.location, ""); } diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index cb89f55169..91a1b8b1ae 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -1,7 +1,7 @@ import { ShaderPosition, ShaderRange } from "../common"; import LexerUtils from "../lexer/Utils"; import { MacroDefine } from "./MacroDefine"; -// #if _EDITOR +// #if _VERBOSE import PpSourceMap, { BlockInfo } from "./sourceMap"; import { GSError, GSErrorName } from "../Error"; // #endif @@ -13,7 +13,7 @@ import { ShaderLab } from "../ShaderLab"; import { ShaderPass } from "@galacean/engine"; export interface ExpandSegment { - // #if _EDITOR + // #if _VERBOSE block?: BlockInfo; // #endif rangeInBlock: ShaderRange; @@ -31,7 +31,7 @@ export class PpParser { private static _includeMap: Record; private static _basePathForIncludeKey: string; - // #if _EDITOR + // #if _VERBOSE static _errors: Error[] = []; // #endif static _scanningText: string; @@ -45,7 +45,7 @@ export class PpParser { this.addPredefinedMacro("GL_ES"); this._includeMap = includeMap; this._basePathForIncludeKey = basePathForIncludeKey; - // #if _EDITOR + // #if _VERBOSE this._errors.length = 0; // #endif } @@ -95,7 +95,7 @@ export class PpParser { break; } } - // #if _EDITOR + // #if _VERBOSE if (this._errors.length > 0) return null; // #endif @@ -107,7 +107,7 @@ export class PpParser { } private static reportError(loc: ShaderRange | ShaderPosition, message: string, source: string, file: string) { - // #if _EDITOR + // #if _VERBOSE this._errors.push(new GSError(GSErrorName.PreprocessorError, message, loc, source, file)); // #else throw new Error(message); @@ -139,11 +139,11 @@ export class PpParser { const range = ShaderLab.createRange(start, end); const expanded = this._expandMacroChunk(chunk, range, id.lexeme); - // #if _EDITOR + // #if _VERBOSE const block = new BlockInfo(id.lexeme, undefined, expanded.sourceMap); // #endif this.expandSegments.push({ - // #if _EDITOR + // #if _VERBOSE block, // #endif rangeInBlock: range, @@ -167,13 +167,13 @@ export class PpParser { const expanded = this._expandMacroChunk(bodyChunk.lexeme, bodyChunk.location, scanner); - // #if _EDITOR + // #if _VERBOSE const block = new BlockInfo(scanner.file, scanner.blockRange, expanded.sourceMap); // #endif const range = ShaderLab.createRange(bodyChunk.location.start, end); this.expandSegments.push({ - // #if _EDITOR + // #if _VERBOSE block, // #endif rangeInBlock: range, @@ -202,13 +202,13 @@ export class PpParser { if (directive === EPpKeyword.else) { const { token: elseChunk } = scanner.scanMacroBranchChunk(); const expanded = this._expandMacroChunk(elseChunk.lexeme, elseChunk.location, scanner); - // #if _EDITOR + // #if _VERBOSE const block = new BlockInfo(scanner.file, scanner.blockRange, expanded.sourceMap); // #endif const startPosition = ShaderLab.createPosition(start); const range = ShaderLab.createRange(startPosition, scanner.getShaderPosition()); this.expandSegments.push({ - // #if _EDITOR + // #if _VERBOSE block, // #endif rangeInBlock: range, @@ -220,28 +220,28 @@ export class PpParser { if (!!constantExpr) { const end = nextDirective.type === EPpKeyword.endif ? scanner.current : scanner.scanRemainMacro().index; const expanded = this._expandMacroChunk(bodyChunk.lexeme, bodyChunk.location, scanner); - // #if _EDITOR + // #if _VERBOSE const block = new BlockInfo(scanner.file, scanner.blockRange, expanded.sourceMap); // #endif const startPosition = ShaderLab.createPosition(start); const endPosition = ShaderLab.createPosition(end); const range = ShaderLab.createRange(startPosition, endPosition); this.expandSegments.push({ - // #if _EDITOR + // #if _VERBOSE block, // #endif rangeInBlock: range, replace: expanded.content }); } else { - // #if _EDITOR + // #if _VERBOSE const block = new BlockInfo(scanner.file, scanner.blockRange); // #endif const startPosition = ShaderLab.createPosition(start); const endPosition = ShaderLab.createPosition(scanner.current); const range = ShaderLab.createRange(startPosition, endPosition); this.expandSegments.push({ - // #if _EDITOR + // #if _VERBOSE block, // #endif rangeInBlock: range, @@ -478,7 +478,7 @@ export class PpParser { parentScanner: PpScanner ): { content: string; - // #if _EDITOR + // #if _VERBOSE sourceMap: PpSourceMap; // #endif }; @@ -488,7 +488,7 @@ export class PpParser { file: string ): { content: string; - // #if _EDITOR + // #if _VERBOSE sourceMap: PpSourceMap; // #endif }; @@ -498,7 +498,7 @@ export class PpParser { scannerOrFile: PpScanner | string ): { content: string; - // #if _EDITOR + // #if _VERBOSE sourceMap: PpSourceMap; // #endif } { @@ -513,7 +513,7 @@ export class PpParser { this._expandSegmentsStack.pop(); return { content: ret, - // #if _EDITOR + // #if _VERBOSE sourceMap: scanner.sourceMap // #endif }; @@ -532,12 +532,12 @@ export class PpParser { const end = nextDirective.type === EPpKeyword.endif ? scanner.getShaderPosition() : scanner.scanRemainMacro(); const expanded = this._expandMacroChunk(bodyChunk.lexeme, bodyChunk.location, scanner); - // #if _EDITOR + // #if _VERBOSE const blockInfo = new BlockInfo(scanner.file, scanner.blockRange, expanded.sourceMap); // #endif const range = ShaderLab.createRange(bodyChunk.location.start, end); this.expandSegments.push({ - // #if _EDITOR + // #if _VERBOSE block: blockInfo, // #endif rangeInBlock: range, @@ -552,14 +552,14 @@ export class PpParser { } private static _addEmptyReplace(scanner: PpScanner, start: number) { - // #if _EDITOR + // #if _VERBOSE const block = new BlockInfo(scanner.file, scanner.blockRange); // #endif const startPosition = ShaderLab.createPosition(start); const endPosition = scanner.curPosition; const range = ShaderLab.createRange(startPosition, endPosition); this.expandSegments.push({ - // #if _EDITOR + // #if _VERBOSE block, // #endif rangeInBlock: range, @@ -577,12 +577,12 @@ export class PpParser { if (!!constantExpr) { const end = nextDirective.type === EPpKeyword.endif ? scanner.getShaderPosition() : scanner.scanRemainMacro(); const expanded = this._expandMacroChunk(bodyChunk.lexeme, bodyChunk.location, scanner); - // #if _EDITOR + // #if _VERBOSE const block = new BlockInfo(scanner.file, scanner.blockRange, expanded.sourceMap); // #endif const range = ShaderLab.createRange(bodyChunk.location.start, end); this.expandSegments.push({ - // #if _EDITOR + // #if _VERBOSE block, // #endif rangeInBlock: range, @@ -615,12 +615,12 @@ export class PpParser { const macroDefine = new MacroDefine(macro, macroBody, range, macroArgs); this._definedMacros.set(macro.lexeme, macroDefine); - // #if _EDITOR + // #if _VERBOSE const block = new BlockInfo(scanner.file, scanner.blockRange); // #endif this.expandSegments.push({ - // #if _EDITOR + // #if _VERBOSE block, // #endif rangeInBlock: ShaderLab.createRange(start, scanner.curPosition), @@ -632,13 +632,13 @@ export class PpParser { const start = scanner.current - 6; const macro = scanner.scanWord(); - // #if _EDITOR + // #if _VERBOSE const block = new BlockInfo(scanner.file, scanner.blockRange); // #endif const startPosition = ShaderLab.createPosition(start); const range = ShaderLab.createRange(startPosition, scanner.curPosition); this.expandSegments.push({ - // #if _EDITOR + // #if _VERBOSE block, // #endif rangeInBlock: range, @@ -693,12 +693,12 @@ export class PpParser { const range = ShaderLab.createRange(token.location!.start, scanner.curPosition); replace = macro.expand(...args); const expanded = this._expandMacroChunk(replace, range, scanner); - // #if _EDITOR + // #if _VERBOSE const block = new BlockInfo(scanner.file, scanner.blockRange, expanded.sourceMap); // #endif const blockRange = ShaderLab.createRange(token.location!.start, scanner.curPosition); this.expandSegments.push({ - // #if _EDITOR + // #if _VERBOSE block, // #endif rangeInBlock: blockRange, @@ -706,12 +706,12 @@ export class PpParser { }); } else { const expanded = this._expandMacroChunk(replace, token.location, scanner); - // #if _EDITOR + // #if _VERBOSE const block = new BlockInfo(scanner.file, scanner.blockRange, expanded.sourceMap); // #endif const range = ShaderLab.createRange(token.location.start, token.location.end); this.expandSegments.push({ - // #if _EDITOR + // #if _VERBOSE block, // #endif rangeInBlock: range, diff --git a/packages/shader-lab/src/preprocessor/PpScanner.ts b/packages/shader-lab/src/preprocessor/PpScanner.ts index d2a62994b6..e57e56716a 100644 --- a/packages/shader-lab/src/preprocessor/PpScanner.ts +++ b/packages/shader-lab/src/preprocessor/PpScanner.ts @@ -1,6 +1,6 @@ import { ShaderRange, ShaderPosition } from "../common"; import LexerUtils from "../lexer/Utils"; -// #if _EDITOR +// #if _VERBOSE import PpSourceMap from "./sourceMap"; // #endif import BaseScanner from "../common/BaseScanner"; @@ -16,7 +16,7 @@ export default class PpScanner extends BaseScanner { private macroLvl = 0; - // #if _EDITOR + // #if _VERBOSE readonly sourceMap = new PpSourceMap(); readonly file: string; readonly blockRange?: ShaderRange; @@ -24,13 +24,13 @@ export default class PpScanner extends BaseScanner { constructor( source: string, - // #if _EDITOR + // #if _VERBOSE file = "__main__", blockRange?: ShaderRange // #endif ) { super(source); - // #if _EDITOR + // #if _VERBOSE this.file = file; this.blockRange = blockRange; // #endif diff --git a/packages/shader-lab/src/preprocessor/Preprocessor.ts b/packages/shader-lab/src/preprocessor/Preprocessor.ts index 7eb6278792..846e0166dd 100644 --- a/packages/shader-lab/src/preprocessor/Preprocessor.ts +++ b/packages/shader-lab/src/preprocessor/Preprocessor.ts @@ -25,7 +25,7 @@ export class Preprocessor { PpParser.addPredefinedMacro(macro, value); } - // #if _EDITOR + // #if _VERBOSE static convertSourceIndex(index: number) { return this.baseScanner.sourceMap.map(index); } diff --git a/packages/shader-lab/src/preprocessor/Utils.ts b/packages/shader-lab/src/preprocessor/Utils.ts index aec8474356..cb66422a19 100644 --- a/packages/shader-lab/src/preprocessor/Utils.ts +++ b/packages/shader-lab/src/preprocessor/Utils.ts @@ -1,6 +1,6 @@ import { ShaderRange } from "../common"; import { ExpandSegment } from "./PpParser"; -// #if _EDITOR +// #if _VERBOSE import PpSourceMap, { MapRange } from "./sourceMap"; // #endif @@ -8,7 +8,7 @@ export class PpUtils { static expand( segments: ExpandSegment[], source: string, - // #if _EDITOR + // #if _VERBOSE sourceMap?: PpSourceMap //#endif ) { @@ -22,7 +22,7 @@ export class PpUtils { const generatedIdxEnd = generatedIdx + originSlice.length + seg.replace.length; - // #if _EDITOR + // #if _VERBOSE const mapRange = new MapRange(seg.block, seg.rangeInBlock, { start: generatedIdx + originSlice.length, end: generatedIdxEnd diff --git a/packages/shader-lab/src/preprocessor/sourceMap/index.ts b/packages/shader-lab/src/preprocessor/sourceMap/index.ts index d932da13d6..44ea26abc9 100644 --- a/packages/shader-lab/src/preprocessor/sourceMap/index.ts +++ b/packages/shader-lab/src/preprocessor/sourceMap/index.ts @@ -1,6 +1,6 @@ import { ShaderRange } from "../../common/ShaderRange"; -// #if _EDITOR +// #if _VERBOSE export class BlockInfo { readonly sourceFile: string; readonly rangeInFile?: ShaderRange; diff --git a/rollup.config.js b/rollup.config.js index f339465482..ad02bf0645 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -66,7 +66,7 @@ function config({ location, pkgJson, verboseMode }) { curPlugins.push( jscc({ - values: { _EDITOR: verboseMode } + values: { _VERBOSE: verboseMode } }) ); From 11ebcbc56d9ff5b1b4c5e10dcc8fbf73c472687f Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 8 Oct 2024 20:12:28 +0800 Subject: [PATCH 079/131] feat: opt shaderlabobjectpool --- .../src/{Utils.ts => ParserUtils.ts} | 0 packages/shader-lab/src/ShaderLab.ts | 12 +- .../shader-lab/src/ShaderLabObjectPool.ts | 19 --- packages/shader-lab/src/ShaderLabUtils.ts | 17 ++ .../shader-lab/src/codeGen/CodeGenVisitor.ts | 2 +- packages/shader-lab/src/common/BaseToken.ts | 4 +- packages/shader-lab/src/lalr/Utils.ts | 8 +- packages/shader-lab/src/parser/AST.ts | 156 +++++++++--------- .../src/parser/ShaderTargetParser.ts | 2 +- tests/src/shader-lab/ShaderLab.test.ts | 1 - 10 files changed, 108 insertions(+), 113 deletions(-) rename packages/shader-lab/src/{Utils.ts => ParserUtils.ts} (100%) delete mode 100644 packages/shader-lab/src/ShaderLabObjectPool.ts create mode 100644 packages/shader-lab/src/ShaderLabUtils.ts diff --git a/packages/shader-lab/src/Utils.ts b/packages/shader-lab/src/ParserUtils.ts similarity index 100% rename from packages/shader-lab/src/Utils.ts rename to packages/shader-lab/src/ParserUtils.ts diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index 55e87e7d66..077e5adedc 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -7,20 +7,17 @@ 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 _VERBOSE import { GSError } from "./Error"; // #endif import { PpParser } from "./preprocessor/PpParser"; +import { ShaderLabUtils } from "./ShaderLabUtils"; export class ShaderLab implements IShaderLab { private static _parser = ShaderTargetParser.create(); - private static _shaderPositionPool = new ShaderLabObjectPool(ShaderPosition); - private static _shaderRangePool = new ShaderLabObjectPool(ShaderRange); + private static _shaderPositionPool = ShaderLabUtils.createObjectPool(ShaderPosition); + private static _shaderRangePool = ShaderLabUtils.createObjectPool(ShaderRange); - /** - * @internal - */ static _processingPassText?: string; static createPosition(index: number, line?: number, column?: number): ShaderPosition { @@ -115,7 +112,7 @@ export class ShaderLab implements IShaderLab { } _parseShaderContent(shaderSource: string): IShaderContent { - ShaderLabObjectPool.clearAllShaderLabObjectPool(); + ShaderLabUtils.clearAllShaderLabObjectPool(); ShaderContentParser.reset(); // #if _VERBOSE this._errors.length = 0; @@ -129,6 +126,7 @@ export class ShaderLab implements IShaderLab { // #if _VERBOSE /** + * TODO: * @internal * For debug */ diff --git a/packages/shader-lab/src/ShaderLabObjectPool.ts b/packages/shader-lab/src/ShaderLabObjectPool.ts deleted file mode 100644 index c4698d1bab..0000000000 --- a/packages/shader-lab/src/ShaderLabObjectPool.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { ClearableObjectPool, IPoolElement } from "@galacean/engine"; - -// 放到静态类里 -/** - * @internal - */ -export class ShaderLabObjectPool extends ClearableObjectPool { - static ShaderLabObjectPoolSet: ShaderLabObjectPool[] = []; - static clearAllShaderLabObjectPool() { - for (let i = 0; i < this.ShaderLabObjectPoolSet.length; i++) { - this.ShaderLabObjectPoolSet[i].clear(); - } - } - - constructor(type: new () => T) { - super(type); - ShaderLabObjectPool.ShaderLabObjectPoolSet.push(this); - } -} diff --git a/packages/shader-lab/src/ShaderLabUtils.ts b/packages/shader-lab/src/ShaderLabUtils.ts new file mode 100644 index 0000000000..c10e6ff4d8 --- /dev/null +++ b/packages/shader-lab/src/ShaderLabUtils.ts @@ -0,0 +1,17 @@ +import { ClearableObjectPool, IPoolElement } from "@galacean/engine"; + +export class ShaderLabUtils { + private static _shaderLabObjectPoolSet: ClearableObjectPool[] = []; + + static createObjectPool(type: new () => T) { + const pool = new ClearableObjectPool(type); + ShaderLabUtils._shaderLabObjectPoolSet.push(pool); + return pool; + } + + static clearAllShaderLabObjectPool() { + for (let i = 0; i < ShaderLabUtils._shaderLabObjectPoolSet.length; i++) { + ShaderLabUtils._shaderLabObjectPoolSet[i].clear(); + } + } +} diff --git a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts index 6c6fee34f6..4fbe72d83a 100644 --- a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts +++ b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts @@ -3,7 +3,7 @@ import { BaseToken as Token } from "../common/BaseToken"; import { EKeyword, ShaderPosition, ShaderRange } from "../common"; import { ASTNode, TreeNode } from "../parser/AST"; import { ESymbolType, FnSymbol, VarSymbol } from "../parser/symbolTable"; -import { ParserUtils } from "../Utils"; +import { ParserUtils } from "../ParserUtils"; import { NodeChild } from "../parser/types"; import { VisitorContext } from "./VisitorContext"; // #if _VERBOSE diff --git a/packages/shader-lab/src/common/BaseToken.ts b/packages/shader-lab/src/common/BaseToken.ts index 087ffd99a6..380cbc0891 100644 --- a/packages/shader-lab/src/common/BaseToken.ts +++ b/packages/shader-lab/src/common/BaseToken.ts @@ -1,11 +1,11 @@ import { ETokenType } from "./types"; import { ShaderRange, ShaderPosition } from "."; import { ShaderLab } from "../ShaderLab"; -import { ShaderLabObjectPool } from "../ShaderLabObjectPool"; import { IPoolElement } from "@galacean/engine"; +import { ShaderLabUtils } from "../ShaderLabUtils"; export class BaseToken implements IPoolElement { - static pool = new ShaderLabObjectPool(BaseToken); + static pool = ShaderLabUtils.createObjectPool(BaseToken); type: T; lexeme: string; diff --git a/packages/shader-lab/src/lalr/Utils.ts b/packages/shader-lab/src/lalr/Utils.ts index 3aed2648cc..8eb9492fef 100644 --- a/packages/shader-lab/src/lalr/Utils.ts +++ b/packages/shader-lab/src/lalr/Utils.ts @@ -5,8 +5,7 @@ import { ENonTerminal, GrammarSymbol } from "../parser/GrammarSymbol"; import Production from "./Production"; import { ActionInfo, EAction } from "./types"; import { ShaderLab } from "../ShaderLab"; -import { ShaderLabObjectPool } from "../ShaderLabObjectPool"; -import { IPoolElement } from "@galacean/engine"; +import { ClearableObjectPool, IPoolElement } from "@galacean/engine"; import { NodeChild } from "../parser/types"; export default class GrammarUtils { @@ -25,8 +24,7 @@ export default class GrammarUtils { goal: ENonTerminal, options: GrammarSymbol[][], /** the ast node */ - // astType?: ASTNodeConstructor - astTypePool?: ShaderLabObjectPool< + astTypePool?: ClearableObjectPool< { set: (loc: ShaderRange, children: NodeChild[]) => void } & IPoolElement & TreeNode > ) { @@ -74,6 +72,7 @@ export default class GrammarUtils { return a.action === b.action && a.target === b.target; } + // #if _VERBOSE static printAction(actionInfo: ActionInfo) { return ` ${ actionInfo.action === EAction.Reduce ? Production.pool.get(actionInfo.target!) : `State ${actionInfo.target!}` @@ -84,4 +83,5 @@ export default class GrammarUtils { const deriv = production.derivation.map((gs) => GrammarUtils.toString(gs)).join("|"); return `${ENonTerminal[production.goal]} :=> ${deriv}`; } + // #endif } diff --git a/packages/shader-lab/src/parser/AST.ts b/packages/shader-lab/src/parser/AST.ts index 7e8ed343aa..f01ffbdce6 100644 --- a/packages/shader-lab/src/parser/AST.ts +++ b/packages/shader-lab/src/parser/AST.ts @@ -8,10 +8,10 @@ import { EKeyword, ETokenType, TokenType, ShaderRange, GalaceanDataType, TypeAny import SematicAnalyzer from "./SemanticAnalyzer"; import { ShaderData } from "./ShaderInfo"; import { ESymbolType, FnSymbol, StructSymbol, VarSymbol } from "./symbolTable"; -import { ParserUtils } from "../Utils"; +import { ParserUtils } from "../ParserUtils"; import { IParamInfo, NodeChild, StructProp, SymbolType } from "./types"; -import { ShaderLabObjectPool } from "../ShaderLabObjectPool"; -import { IPoolElement } from "@galacean/engine"; +import { ClearableObjectPool, IPoolElement } from "@galacean/engine"; +import { ShaderLabUtils } from "../ShaderLabUtils"; export abstract class TreeNode implements IPoolElement { /** The non-terminal in grammar. */ @@ -44,7 +44,7 @@ export abstract class TreeNode implements IPoolElement { } export namespace ASTNode { - export type ASTNodePool = ShaderLabObjectPool< + export type ASTNodePool = ClearableObjectPool< { set: (loc: ShaderRange, children: NodeChild[]) => void } & IPoolElement & TreeNode >; @@ -63,7 +63,7 @@ export namespace ASTNode { } export class TrivialNode extends TreeNode { - static pool = new ShaderLabObjectPool(TrivialNode); + static pool = ShaderLabUtils.createObjectPool(TrivialNode); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal._ignore); @@ -71,7 +71,7 @@ export namespace ASTNode { } export class ScopeBrace extends TreeNode { - static pool = new ShaderLabObjectPool(ScopeBrace); + static pool = ShaderLabUtils.createObjectPool(ScopeBrace); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.scope_brace); @@ -83,7 +83,7 @@ export namespace ASTNode { } export class ScopeEndBrace extends TreeNode { - static pool = new ShaderLabObjectPool(ScopeEndBrace); + static pool = ShaderLabUtils.createObjectPool(ScopeEndBrace); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.scope_end_brace); @@ -95,7 +95,7 @@ export namespace ASTNode { } export class JumpStatement extends TreeNode { - static pool = new ShaderLabObjectPool(JumpStatement); + static pool = ShaderLabUtils.createObjectPool(JumpStatement); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.jump_statement); @@ -116,7 +116,7 @@ export namespace ASTNode { // #if _VERBOSE export class ConditionOpt extends TreeNode { - static pool = new ShaderLabObjectPool(ConditionOpt); + static pool = ShaderLabUtils.createObjectPool(ConditionOpt); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.conditionopt); @@ -124,7 +124,7 @@ export namespace ASTNode { } export class ForRestStatement extends TreeNode { - static pool = new ShaderLabObjectPool(ForRestStatement); + static pool = ShaderLabUtils.createObjectPool(ForRestStatement); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.for_rest_statement); @@ -132,7 +132,7 @@ export namespace ASTNode { } export class Condition extends TreeNode { - static pool = new ShaderLabObjectPool(Condition); + static pool = ShaderLabUtils.createObjectPool(Condition); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.condition); @@ -140,7 +140,7 @@ export namespace ASTNode { } export class ForInitStatement extends TreeNode { - static pool = new ShaderLabObjectPool(ForInitStatement); + static pool = ShaderLabUtils.createObjectPool(ForInitStatement); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.for_init_statement); @@ -148,7 +148,7 @@ export namespace ASTNode { } export class IterationStatement extends TreeNode { - static pool = new ShaderLabObjectPool(IterationStatement); + static pool = ShaderLabUtils.createObjectPool(IterationStatement); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.iteration_statement); @@ -156,7 +156,7 @@ export namespace ASTNode { } export class SelectionStatement extends TreeNode { - static pool = new ShaderLabObjectPool(SelectionStatement); + static pool = ShaderLabUtils.createObjectPool(SelectionStatement); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.selection_statement); @@ -164,7 +164,7 @@ export namespace ASTNode { } export class ExpressionStatement extends TreeNode { - static pool = new ShaderLabObjectPool(ExpressionStatement); + static pool = ShaderLabUtils.createObjectPool(ExpressionStatement); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.expression_statement); @@ -189,7 +189,7 @@ export namespace ASTNode { // #if _VERBOSE export class InitializerList extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(InitializerList); + static pool = ShaderLabUtils.createObjectPool(InitializerList); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.initializer_list); @@ -202,7 +202,7 @@ export namespace ASTNode { } export class Initializer extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(Initializer); + static pool = ShaderLabUtils.createObjectPool(Initializer); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.initializer); @@ -219,7 +219,7 @@ export namespace ASTNode { // #endif export class SingleDeclaration extends TreeNode { - static pool = new ShaderLabObjectPool(SingleDeclaration); + static pool = ShaderLabUtils.createObjectPool(SingleDeclaration); typeSpecifier: TypeSpecifier; arraySpecifier?: ArraySpecifier; @@ -258,7 +258,7 @@ export namespace ASTNode { } export class FullySpecifiedType extends TreeNode { - static pool = new ShaderLabObjectPool(FullySpecifiedType); + static pool = ShaderLabUtils.createObjectPool(FullySpecifiedType); get qualifierList() { if (this.children.length > 1) { @@ -280,7 +280,7 @@ export namespace ASTNode { } export class TypeQualifier extends TreeNode { - static pool = new ShaderLabObjectPool(TypeQualifier); + static pool = ShaderLabUtils.createObjectPool(TypeQualifier); qualifierList: EKeyword[]; @@ -301,7 +301,7 @@ export namespace ASTNode { } export class SingleTypeQualifier extends TreeNode { - static pool = new ShaderLabObjectPool(SingleTypeQualifier); + static pool = ShaderLabUtils.createObjectPool(SingleTypeQualifier); qualifier: EKeyword; lexeme: string; @@ -337,7 +337,7 @@ export namespace ASTNode { // #if _VERBOSE export class StorageQualifier extends BasicTypeQualifier { - static pool = new ShaderLabObjectPool(StorageQualifier); + static pool = ShaderLabUtils.createObjectPool(StorageQualifier); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.storage_qualifier); @@ -345,7 +345,7 @@ export namespace ASTNode { } export class PrecisionQualifier extends BasicTypeQualifier { - static pool = new ShaderLabObjectPool(PrecisionQualifier); + static pool = ShaderLabUtils.createObjectPool(PrecisionQualifier); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.precision_qualifier); @@ -353,7 +353,7 @@ export namespace ASTNode { } export class InterpolationQualifier extends BasicTypeQualifier { - static pool = new ShaderLabObjectPool(InterpolationQualifier); + static pool = ShaderLabUtils.createObjectPool(InterpolationQualifier); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.interpolation_qualifier); @@ -361,7 +361,7 @@ export namespace ASTNode { } export class InvariantQualifier extends BasicTypeQualifier { - static pool = new ShaderLabObjectPool(InvariantQualifier); + static pool = ShaderLabUtils.createObjectPool(InvariantQualifier); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.invariant_qualifier); @@ -370,7 +370,7 @@ export namespace ASTNode { // #endif export class TypeSpecifier extends TreeNode { - static pool = new ShaderLabObjectPool(TypeSpecifier); + static pool = ShaderLabUtils.createObjectPool(TypeSpecifier); get type(): GalaceanDataType { return (this.children![0] as TypeSpecifierNonArray).type; @@ -392,7 +392,7 @@ export namespace ASTNode { } export class ArraySpecifier extends TreeNode { - static pool = new ShaderLabObjectPool(ArraySpecifier); + static pool = ShaderLabUtils.createObjectPool(ArraySpecifier); get size(): number | undefined { const integerConstantExpr = this.children[1] as IntegerConstantExpression; @@ -405,7 +405,7 @@ export namespace ASTNode { } export class IntegerConstantExpressionOperator extends TreeNode { - static pool = new ShaderLabObjectPool(IntegerConstantExpressionOperator); + static pool = ShaderLabUtils.createObjectPool(IntegerConstantExpressionOperator); compute: (a: number, b: number) => number; get lexeme(): string { @@ -441,7 +441,7 @@ export namespace ASTNode { } export class IntegerConstantExpression extends TreeNode { - static pool = new ShaderLabObjectPool(IntegerConstantExpression); + static pool = ShaderLabUtils.createObjectPool(IntegerConstantExpression); value?: number; override set(loc: ShaderRange, children: NodeChild[]) { @@ -472,7 +472,7 @@ export namespace ASTNode { } export class TypeSpecifierNonArray extends TreeNode { - static pool = new ShaderLabObjectPool(TypeSpecifierNonArray); + static pool = ShaderLabUtils.createObjectPool(TypeSpecifierNonArray); type: GalaceanDataType; lexeme: string; @@ -490,7 +490,7 @@ export namespace ASTNode { } export class ExtBuiltinTypeSpecifierNonArray extends TreeNode { - static pool = new ShaderLabObjectPool(ExtBuiltinTypeSpecifierNonArray); + static pool = ShaderLabUtils.createObjectPool(ExtBuiltinTypeSpecifierNonArray); type: TokenType; lexeme: string; @@ -504,7 +504,7 @@ export namespace ASTNode { } export class InitDeclaratorList extends TreeNode { - static pool = new ShaderLabObjectPool(InitDeclaratorList); + static pool = ShaderLabUtils.createObjectPool(InitDeclaratorList); get typeInfo(): SymbolType { if (this.children.length === 1) { @@ -547,7 +547,7 @@ export namespace ASTNode { } export class IdentifierList extends TreeNode { - static pool = new ShaderLabObjectPool(IdentifierList); + static pool = ShaderLabUtils.createObjectPool(IdentifierList); get idList(): Token[] { if (this.children.length === 2) { @@ -562,7 +562,7 @@ export namespace ASTNode { } export class Declaration extends TreeNode { - static pool = new ShaderLabObjectPool(Declaration); + static pool = ShaderLabUtils.createObjectPool(Declaration); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.declaration); @@ -574,7 +574,7 @@ export namespace ASTNode { } export class FunctionProtoType extends TreeNode { - static pool = new ShaderLabObjectPool(FunctionProtoType); + static pool = ShaderLabUtils.createObjectPool(FunctionProtoType); private get declarator() { return this.children[0] as FunctionDeclarator; @@ -606,7 +606,7 @@ export namespace ASTNode { } export class FunctionDeclarator extends TreeNode { - static pool = new ShaderLabObjectPool(FunctionDeclarator); + static pool = ShaderLabUtils.createObjectPool(FunctionDeclarator); private get header() { return this.children[0] as FunctionHeader; @@ -638,7 +638,7 @@ export namespace ASTNode { } export class FunctionHeader extends TreeNode { - static pool = new ShaderLabObjectPool(FunctionHeader); + static pool = ShaderLabUtils.createObjectPool(FunctionHeader); get ident() { return this.children[1] as Token; @@ -661,7 +661,7 @@ export namespace ASTNode { } export class FunctionParameterList extends TreeNode { - static pool = new ShaderLabObjectPool(FunctionParameterList); + static pool = ShaderLabUtils.createObjectPool(FunctionParameterList); get parameterInfoList(): IParamInfo[] { if (this.children.length === 1) { @@ -694,7 +694,7 @@ export namespace ASTNode { } export class ParameterDeclaration extends TreeNode { - static pool = new ShaderLabObjectPool(ParameterDeclaration); + static pool = ShaderLabUtils.createObjectPool(ParameterDeclaration); get typeQualifier() { if (this.children.length === 2) return this.children[0] as TypeQualifier; @@ -730,7 +730,7 @@ export namespace ASTNode { } export class ParameterDeclarator extends TreeNode { - static pool = new ShaderLabObjectPool(ParameterDeclarator); + static pool = ShaderLabUtils.createObjectPool(ParameterDeclarator); get ident() { return this.children[1] as Token; @@ -749,7 +749,7 @@ export namespace ASTNode { // #if _VERBOSE export class SimpleStatement extends TreeNode { - static pool = new ShaderLabObjectPool(SimpleStatement); + static pool = ShaderLabUtils.createObjectPool(SimpleStatement); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.simple_statement); @@ -757,7 +757,7 @@ export namespace ASTNode { } export class CompoundStatement extends TreeNode { - static pool = new ShaderLabObjectPool(CompoundStatement); + static pool = ShaderLabUtils.createObjectPool(CompoundStatement); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.compound_statement); @@ -766,7 +766,7 @@ export namespace ASTNode { // #endif export class CompoundStatementNoScope extends TreeNode { - static pool = new ShaderLabObjectPool(CompoundStatementNoScope); + static pool = ShaderLabUtils.createObjectPool(CompoundStatementNoScope); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.compound_statement_no_scope); @@ -775,7 +775,7 @@ export namespace ASTNode { // #if _VERBOSE export class Statement extends TreeNode { - static pool = new ShaderLabObjectPool(Statement); + static pool = ShaderLabUtils.createObjectPool(Statement); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.statement); @@ -784,7 +784,7 @@ export namespace ASTNode { // #endif export class StatementList extends TreeNode { - static pool = new ShaderLabObjectPool(StatementList); + static pool = ShaderLabUtils.createObjectPool(StatementList); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.statement_list); @@ -796,7 +796,7 @@ export namespace ASTNode { } export class FunctionDefinition extends TreeNode { - static pool = new ShaderLabObjectPool(FunctionDefinition); + static pool = ShaderLabUtils.createObjectPool(FunctionDefinition); get protoType() { return this.children[0] as FunctionProtoType; @@ -822,7 +822,7 @@ export namespace ASTNode { } export class FunctionCall extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(FunctionCall); + static pool = ShaderLabUtils.createObjectPool(FunctionCall); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.function_call); @@ -838,7 +838,7 @@ export namespace ASTNode { } export class FunctionCallGeneric extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(FunctionCallGeneric); + static pool = ShaderLabUtils.createObjectPool(FunctionCallGeneric); fnSymbol: FnSymbol | StructSymbol | undefined; @@ -883,7 +883,7 @@ export namespace ASTNode { } export class FunctionCallParameterList extends TreeNode { - static pool = new ShaderLabObjectPool(FunctionCallParameterList); + static pool = ShaderLabUtils.createObjectPool(FunctionCallParameterList); get paramSig(): GalaceanDataType[] | undefined { if (this.children.length === 1) { @@ -918,7 +918,7 @@ export namespace ASTNode { } export class PrecisionSpecifier extends TreeNode { - static pool = new ShaderLabObjectPool(PrecisionSpecifier); + static pool = ShaderLabUtils.createObjectPool(PrecisionSpecifier); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.precision_specifier); @@ -930,7 +930,7 @@ export namespace ASTNode { } export class FunctionIdentifier extends TreeNode { - static pool = new ShaderLabObjectPool(FunctionIdentifier); + static pool = ShaderLabUtils.createObjectPool(FunctionIdentifier); get ident() { const ty = this.children[0] as TypeSpecifier; @@ -958,7 +958,7 @@ export namespace ASTNode { } export class AssignmentExpression extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(AssignmentExpression); + static pool = ShaderLabUtils.createObjectPool(AssignmentExpression); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.assignment_expression); @@ -979,7 +979,7 @@ export namespace ASTNode { // #if _VERBOSE export class AssignmentOperator extends TreeNode { - static pool = new ShaderLabObjectPool(AssignmentOperator); + static pool = ShaderLabUtils.createObjectPool(AssignmentOperator); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.assignment_operator); @@ -988,7 +988,7 @@ export namespace ASTNode { // #endif export class Expression extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(Expression); + static pool = ShaderLabUtils.createObjectPool(Expression); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.expression); @@ -1008,7 +1008,7 @@ export namespace ASTNode { } export class PrimaryExpression extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(PrimaryExpression); + static pool = ShaderLabUtils.createObjectPool(PrimaryExpression); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.primary_expression); @@ -1041,7 +1041,7 @@ export namespace ASTNode { } export class PostfixExpression extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(PostfixExpression); + static pool = ShaderLabUtils.createObjectPool(PostfixExpression); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.postfix_expression); @@ -1058,7 +1058,7 @@ export namespace ASTNode { // #if _VERBOSE export class UnaryOperator extends TreeNode { - static pool = new ShaderLabObjectPool(UnaryOperator); + static pool = ShaderLabUtils.createObjectPool(UnaryOperator); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.unary_operator); @@ -1068,7 +1068,7 @@ export namespace ASTNode { // #if _VERBOSE export class UnaryExpression extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(UnaryExpression); + static pool = ShaderLabUtils.createObjectPool(UnaryExpression); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.unary_expression); @@ -1079,7 +1079,7 @@ export namespace ASTNode { // #if _VERBOSE export class MultiplicativeExpression extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(MultiplicativeExpression); + static pool = ShaderLabUtils.createObjectPool(MultiplicativeExpression); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.multiplicative_expression); @@ -1098,7 +1098,7 @@ export namespace ASTNode { // #if _VERBOSE export class AdditiveExpression extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(AdditiveExpression); + static pool = ShaderLabUtils.createObjectPool(AdditiveExpression); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.additive_expression); @@ -1117,7 +1117,7 @@ export namespace ASTNode { // #if _VERBOSE export class ShiftExpression extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(ShiftExpression); + static pool = ShaderLabUtils.createObjectPool(ShiftExpression); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.shift_expression); @@ -1132,7 +1132,7 @@ export namespace ASTNode { // #if _VERBOSE export class RelationalExpression extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(RelationalExpression); + static pool = ShaderLabUtils.createObjectPool(RelationalExpression); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.relational_expression); @@ -1150,7 +1150,7 @@ export namespace ASTNode { // #if _VERBOSE export class EqualityExpression extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(EqualityExpression); + static pool = ShaderLabUtils.createObjectPool(EqualityExpression); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.equality_expression); @@ -1168,7 +1168,7 @@ export namespace ASTNode { // #if _VERBOSE export class AndExpression extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(AndExpression); + static pool = ShaderLabUtils.createObjectPool(AndExpression); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.and_expression); @@ -1186,7 +1186,7 @@ export namespace ASTNode { // #if _VERBOSE export class ExclusiveOrExpression extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(ExclusiveOrExpression); + static pool = ShaderLabUtils.createObjectPool(ExclusiveOrExpression); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.exclusive_or_expression); @@ -1204,7 +1204,7 @@ export namespace ASTNode { // #if _VERBOSE export class InclusiveOrExpression extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(InclusiveOrExpression); + static pool = ShaderLabUtils.createObjectPool(InclusiveOrExpression); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.inclusive_or_expression); @@ -1222,7 +1222,7 @@ export namespace ASTNode { // #if _VERBOSE export class LogicalAndExpression extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(LogicalAndExpression); + static pool = ShaderLabUtils.createObjectPool(LogicalAndExpression); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.logical_and_expression); @@ -1240,7 +1240,7 @@ export namespace ASTNode { // #if _VERBOSE export class LogicalXorExpression extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(LogicalXorExpression); + static pool = ShaderLabUtils.createObjectPool(LogicalXorExpression); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.logical_xor_expression); @@ -1258,7 +1258,7 @@ export namespace ASTNode { // #if _VERBOSE export class LogicalOrExpression extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(LogicalOrExpression); + static pool = ShaderLabUtils.createObjectPool(LogicalOrExpression); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.logical_or_expression); @@ -1276,7 +1276,7 @@ export namespace ASTNode { // #if _VERBOSE export class ConditionalExpression extends ExpressionAstNode { - static pool = new ShaderLabObjectPool(ConditionalExpression); + static pool = ShaderLabUtils.createObjectPool(ConditionalExpression); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.conditional_expression); @@ -1291,7 +1291,7 @@ export namespace ASTNode { // #endif export class StructSpecifier extends TreeNode { - static pool = new ShaderLabObjectPool(StructSpecifier); + static pool = ShaderLabUtils.createObjectPool(StructSpecifier); ident?: Token; @@ -1313,7 +1313,7 @@ export namespace ASTNode { } export class StructDeclarationList extends TreeNode { - static pool = new ShaderLabObjectPool(StructDeclarationList); + static pool = ShaderLabUtils.createObjectPool(StructDeclarationList); get propList(): StructProp[] { if (this.children.length === 1) { @@ -1330,7 +1330,7 @@ export namespace ASTNode { } export class StructDeclaration extends TreeNode { - static pool = new ShaderLabObjectPool(StructDeclaration); + static pool = ShaderLabUtils.createObjectPool(StructDeclaration); get typeSpecifier() { if (this.children.length === 3) { @@ -1363,7 +1363,7 @@ export namespace ASTNode { } export class StructDeclaratorList extends TreeNode { - static pool = new ShaderLabObjectPool(StructDeclaratorList); + static pool = ShaderLabUtils.createObjectPool(StructDeclaratorList); get declaratorList(): StructDeclarator[] { if (this.children.length === 1) { @@ -1380,7 +1380,7 @@ export namespace ASTNode { } export class StructDeclarator extends TreeNode { - static pool = new ShaderLabObjectPool(StructDeclarator); + static pool = ShaderLabUtils.createObjectPool(StructDeclarator); get ident() { return this.children[0] as Token; @@ -1396,7 +1396,7 @@ export namespace ASTNode { } export class VariableDeclaration extends TreeNode { - static pool = new ShaderLabObjectPool(VariableDeclaration); + static pool = ShaderLabUtils.createObjectPool(VariableDeclaration); override set(loc: ShaderRange, children: NodeChild[]) { super.set(loc, children, ENonTerminal.variable_declaration); @@ -1417,7 +1417,7 @@ export namespace ASTNode { } export class VariableIdentifier extends TreeNode { - static pool = new ShaderLabObjectPool(VariableIdentifier); + static pool = ShaderLabUtils.createObjectPool(VariableIdentifier); symbolInfo: | VarSymbol @@ -1464,7 +1464,7 @@ export namespace ASTNode { } export class GLShaderProgram extends TreeNode { - static pool = new ShaderLabObjectPool(GLShaderProgram); + static pool = ShaderLabUtils.createObjectPool(GLShaderProgram); shaderData: ShaderData; diff --git a/packages/shader-lab/src/parser/ShaderTargetParser.ts b/packages/shader-lab/src/parser/ShaderTargetParser.ts index b30b18c778..73626f0db9 100644 --- a/packages/shader-lab/src/parser/ShaderTargetParser.ts +++ b/packages/shader-lab/src/parser/ShaderTargetParser.ts @@ -8,7 +8,7 @@ import SematicAnalyzer from "./SemanticAnalyzer"; import { TraceStackItem } from "./types"; import { addTranslationRule, createGrammar } from "../lalr/CFG"; import { LALR1 } from "../lalr"; -import { ParserUtils } from "../Utils"; +import { ParserUtils } from "../ParserUtils"; import { Logger } from "@galacean/engine"; // #if _VERBOSE import { GSError, GSErrorName } from "../Error"; diff --git a/tests/src/shader-lab/ShaderLab.test.ts b/tests/src/shader-lab/ShaderLab.test.ts index 56c635adac..fcbbc85a41 100644 --- a/tests/src/shader-lab/ShaderLab.test.ts +++ b/tests/src/shader-lab/ShaderLab.test.ts @@ -252,7 +252,6 @@ describe("ShaderLab", () => { // @ts-ignore assert.instanceOf(shaderLabVerbose._errors[2], GSError); - debugger; // @ts-ignore for (const err of shaderLabVerbose._errors) { console.log(err.toString()); From 67f9c8d31662563e1ffc628bfa2ebdc235b99987 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 8 Oct 2024 20:22:52 +0800 Subject: [PATCH 080/131] feat: rm redundant code --- packages/shader-lab/src/ShaderLab.ts | 32 -------------------------- tests/src/shader-lab/ShaderLab.test.ts | 5 ++-- 2 files changed, 2 insertions(+), 35 deletions(-) diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index 077e5adedc..da99119e52 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -125,38 +125,6 @@ export class ShaderLab implements IShaderLab { } // #if _VERBOSE - /** - * TODO: - * @internal - * For debug - */ - _parse( - shaderSource: string, - macros: ShaderMacro[] = [], - backend: ShaderPlatformTarget = ShaderPlatformTarget.GLES100 - ): (ReturnType & { 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); - } - } - return passResult; - } - /** * @internal */ diff --git a/tests/src/shader-lab/ShaderLab.test.ts b/tests/src/shader-lab/ShaderLab.test.ts index fcbbc85a41..ce901ecd48 100644 --- a/tests/src/shader-lab/ShaderLab.test.ts +++ b/tests/src/shader-lab/ShaderLab.test.ts @@ -241,8 +241,7 @@ describe("ShaderLab", () => { it("compilation-error", () => { const errorShader = fs.readFileSync(path.join(__dirname, "shaders/compilation-error.shader")).toString(); - // @ts-ignore - shaderLabVerbose._parse(errorShader); + shaderParse.bind(shaderLabVerbose)(errorShader); // @ts-ignore expect(shaderLabVerbose._errors.length).to.eq(3); // @ts-ignore @@ -257,6 +256,6 @@ describe("ShaderLab", () => { console.log(err.toString()); } - expect(shaderParse.bind(shaderLabRelease)).to.throw(Error); + expect(shaderParse.bind(shaderLabRelease, errorShader)).to.throw(Error); }); }); From c76c81f2c4a90c24a22432ebf8f79cdbedff0e46 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Wed, 9 Oct 2024 10:36:03 +0800 Subject: [PATCH 081/131] feat: code opt --- packages/shader-lab/src/{Error.ts => GSError.ts} | 0 packages/shader-lab/src/ShaderLab.ts | 3 ++- packages/shader-lab/src/codeGen/CodeGenVisitor.ts | 2 +- packages/shader-lab/src/codeGen/VisitorContext.ts | 2 +- packages/shader-lab/src/common/BaseScanner.ts | 2 +- packages/shader-lab/src/contentParser/ShaderContentParser.ts | 2 +- packages/shader-lab/src/index.ts | 2 +- packages/shader-lab/src/parser/SemanticAnalyzer.ts | 2 +- packages/shader-lab/src/parser/ShaderTargetParser.ts | 2 +- packages/shader-lab/src/preprocessor/MacroDefine.ts | 2 +- packages/shader-lab/src/preprocessor/PpParser.ts | 2 +- 11 files changed, 11 insertions(+), 10 deletions(-) rename packages/shader-lab/src/{Error.ts => GSError.ts} (100%) diff --git a/packages/shader-lab/src/Error.ts b/packages/shader-lab/src/GSError.ts similarity index 100% rename from packages/shader-lab/src/Error.ts rename to packages/shader-lab/src/GSError.ts diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index da99119e52..14e83c1b2b 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -8,11 +8,12 @@ import { ShaderContentParser } from "./contentParser"; import { Logger, ShaderLib, ShaderMacro, ShaderPass, ShaderPlatformTarget } from "@galacean/engine"; import { ShaderPosition, ShaderRange } from "./common"; // #if _VERBOSE -import { GSError } from "./Error"; +import { GSError } from "./GSError"; // #endif import { PpParser } from "./preprocessor/PpParser"; import { ShaderLabUtils } from "./ShaderLabUtils"; +/** @internal */ export class ShaderLab implements IShaderLab { private static _parser = ShaderTargetParser.create(); private static _shaderPositionPool = ShaderLabUtils.createObjectPool(ShaderPosition); diff --git a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts index 4fbe72d83a..d7670ec154 100644 --- a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts +++ b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts @@ -7,7 +7,7 @@ import { ParserUtils } from "../ParserUtils"; import { NodeChild } from "../parser/types"; import { VisitorContext } from "./VisitorContext"; // #if _VERBOSE -import { GSErrorName, GSError } from "../Error"; +import { GSErrorName, GSError } from "../GSError"; // #endif import { ShaderLab } from "../ShaderLab"; diff --git a/packages/shader-lab/src/codeGen/VisitorContext.ts b/packages/shader-lab/src/codeGen/VisitorContext.ts index 536e7f9e6a..7109a0332c 100644 --- a/packages/shader-lab/src/codeGen/VisitorContext.ts +++ b/packages/shader-lab/src/codeGen/VisitorContext.ts @@ -3,7 +3,7 @@ import { ASTNode } from "../parser/AST"; import { ESymbolType, SymbolTable, SymbolInfo } from "../parser/symbolTable"; import { IParamInfo } from "../parser/types"; // #if _VERBOSE -import { GSErrorName, GSError } from "../Error"; +import { GSErrorName, GSError } from "../GSError"; // #endif import { BaseToken } from "../common/BaseToken"; import { ShaderLab } from "../ShaderLab"; diff --git a/packages/shader-lab/src/common/BaseScanner.ts b/packages/shader-lab/src/common/BaseScanner.ts index 8eab44fddb..4fc62b9bd3 100644 --- a/packages/shader-lab/src/common/BaseScanner.ts +++ b/packages/shader-lab/src/common/BaseScanner.ts @@ -1,6 +1,6 @@ import { ETokenType, ShaderRange, ShaderPosition } from "."; // #if _VERBOSE -import { GSError, GSErrorName } from "../Error"; +import { GSError, GSErrorName } from "../GSError"; // #endif import { ShaderLab } from "../ShaderLab"; import { BaseToken } from "./BaseToken"; diff --git a/packages/shader-lab/src/contentParser/ShaderContentParser.ts b/packages/shader-lab/src/contentParser/ShaderContentParser.ts index c4adc5cb66..68cccab29e 100644 --- a/packages/shader-lab/src/contentParser/ShaderContentParser.ts +++ b/packages/shader-lab/src/contentParser/ShaderContentParser.ts @@ -24,7 +24,7 @@ import { IRenderStates } from "@galacean/engine-design"; // #if _VERBOSE -import { GSError, GSErrorName } from "../Error"; +import { GSError, GSErrorName } from "../GSError"; // #endif const EngineType = [ diff --git a/packages/shader-lab/src/index.ts b/packages/shader-lab/src/index.ts index bf8254f312..637143b64d 100644 --- a/packages/shader-lab/src/index.ts +++ b/packages/shader-lab/src/index.ts @@ -2,7 +2,7 @@ export { ShaderLab } from "./ShaderLab"; // #if _VERBOSE export { Preprocessor } from "./preprocessor"; -export * from "./Error"; +export * from "./GSError"; // #endif //@ts-ignore diff --git a/packages/shader-lab/src/parser/SemanticAnalyzer.ts b/packages/shader-lab/src/parser/SemanticAnalyzer.ts index 891bf27152..04c4e0c068 100644 --- a/packages/shader-lab/src/parser/SemanticAnalyzer.ts +++ b/packages/shader-lab/src/parser/SemanticAnalyzer.ts @@ -1,7 +1,7 @@ import { ShaderRange } from "../common"; import { TreeNode } from "./AST"; // #if _VERBOSE -import { GSError, GSErrorName } from "../Error"; +import { GSError, GSErrorName } from "../GSError"; // #endif import { ShaderData } from "./ShaderInfo"; import { SymbolInfo, SymbolTable } from "../parser/symbolTable"; diff --git a/packages/shader-lab/src/parser/ShaderTargetParser.ts b/packages/shader-lab/src/parser/ShaderTargetParser.ts index 73626f0db9..f30cb49eeb 100644 --- a/packages/shader-lab/src/parser/ShaderTargetParser.ts +++ b/packages/shader-lab/src/parser/ShaderTargetParser.ts @@ -11,7 +11,7 @@ import { LALR1 } from "../lalr"; import { ParserUtils } from "../ParserUtils"; import { Logger } from "@galacean/engine"; // #if _VERBOSE -import { GSError, GSErrorName } from "../Error"; +import { GSError, GSErrorName } from "../GSError"; // #endif import { ShaderLab } from "../ShaderLab"; diff --git a/packages/shader-lab/src/preprocessor/MacroDefine.ts b/packages/shader-lab/src/preprocessor/MacroDefine.ts index dacbb48b2e..7d7f2db229 100644 --- a/packages/shader-lab/src/preprocessor/MacroDefine.ts +++ b/packages/shader-lab/src/preprocessor/MacroDefine.ts @@ -1,6 +1,6 @@ import { BaseToken } from "../common/BaseToken"; import { ShaderRange } from "../common"; -import { GSError, GSErrorName } from "../Error"; +import { GSError, GSErrorName } from "../GSError"; export class MacroDefine { readonly location?: ShaderRange; diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 91a1b8b1ae..3cfc192283 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -3,7 +3,7 @@ import LexerUtils from "../lexer/Utils"; import { MacroDefine } from "./MacroDefine"; // #if _VERBOSE import PpSourceMap, { BlockInfo } from "./sourceMap"; -import { GSError, GSErrorName } from "../Error"; +import { GSError, GSErrorName } from "../GSError"; // #endif import { BaseToken } from "../common/BaseToken"; import { EPpKeyword, EPpToken, PpConstant } from "./constants"; From 2236ac37e25d3f76fbb55983d8b1e34b7847bb0d Mon Sep 17 00:00:00 2001 From: Sway007 Date: Wed, 9 Oct 2024 11:09:06 +0800 Subject: [PATCH 082/131] feat: code opt --- packages/shader-lab/src/GSError.ts | 17 ++++++----------- packages/shader-lab/src/ShaderLab.ts | 11 ++++++----- packages/shader-lab/src/common/BaseScanner.ts | 3 ++- .../src/contentParser/ShaderContentParser.ts | 2 +- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/packages/shader-lab/src/GSError.ts b/packages/shader-lab/src/GSError.ts index b936cb66b7..722cbfa485 100644 --- a/packages/shader-lab/src/GSError.ts +++ b/packages/shader-lab/src/GSError.ts @@ -1,6 +1,7 @@ // #if _VERBOSE import { Logger } from "@galacean/engine"; -import { ShaderPosition, ShaderRange } from "./common"; +import { ShaderPosition } from "./common/ShaderPosition"; +import { ShaderRange } from "./common/ShaderRange"; export class GSError extends Error { static wrappingLineCount = 2; @@ -16,17 +17,11 @@ export class GSError extends Error { this.name = name; } - log(content?: string): void { - Logger.error(this.toString(content)); - } - - override toString(content?: string): string { + override toString(): string { if (!Logger.enable) return; let start: ShaderPosition, end: ShaderPosition; - const { message, location: loc, source: originSource } = this; - let logSource = originSource; - if (content) logSource = content; - if (!logSource) { + const { message, location: loc, source } = this; + if (!source) { return message; } @@ -36,7 +31,7 @@ export class GSError extends Error { start = loc.start; end = loc.end; } - const lines = logSource.split("\n"); + const lines = source.split("\n"); let diagnosticMessage = `${this.name}: ${message}\n\n`; const lineSplit = "|···"; diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index 14e83c1b2b..437257468f 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -69,7 +69,7 @@ export class ShaderLab implements IShaderLab { for (const err of PpParser._errors) { this._errors.push(err); } - this._logErrors(this._errors); + this._logErrors(); return { vertex: "", fragment: "" }; } // #endif @@ -89,7 +89,7 @@ export class ShaderLab implements IShaderLab { this._errors.push(err); } if (!program) { - this._logErrors(this._errors); + this._logErrors(); return { vertex: "", fragment: "" }; } // #endif @@ -106,7 +106,7 @@ export class ShaderLab implements IShaderLab { for (const err of codeGen.errors) { this._errors.push(err); } - this._logErrors(this._errors); + this._logErrors(); // #endif return ret; @@ -129,11 +129,12 @@ export class ShaderLab implements IShaderLab { /** * @internal */ - _logErrors(errors: GSError[], source?: string) { + _logErrors() { + const errors = this._errors; if (errors.length === 0 || !Logger.isEnabled) return; Logger.error(`${errors.length} errors occur!`); for (const err of errors) { - err.log(source); + Logger.error(err.toString()); } } // #endif diff --git a/packages/shader-lab/src/common/BaseScanner.ts b/packages/shader-lab/src/common/BaseScanner.ts index 4fc62b9bd3..ab932754f3 100644 --- a/packages/shader-lab/src/common/BaseScanner.ts +++ b/packages/shader-lab/src/common/BaseScanner.ts @@ -1,3 +1,4 @@ +import { Logger } from "@galacean/engine"; import { ETokenType, ShaderRange, ShaderPosition } from "."; // #if _VERBOSE import { GSError, GSErrorName } from "../GSError"; @@ -131,7 +132,7 @@ export default class BaseScanner { throwError(pos: ShaderPosition | ShaderRange, ...msgs: any[]) { // #if _VERBOSE const error = new GSError(GSErrorName.ScannerError, msgs.join(" "), pos, this._source); - error.log(); + Logger.error(error.toString()); throw error; // #else throw new Error(msgs.join("")); diff --git a/packages/shader-lab/src/contentParser/ShaderContentParser.ts b/packages/shader-lab/src/contentParser/ShaderContentParser.ts index 68cccab29e..5f8ae7e903 100644 --- a/packages/shader-lab/src/contentParser/ShaderContentParser.ts +++ b/packages/shader-lab/src/contentParser/ShaderContentParser.ts @@ -499,7 +499,7 @@ export class ShaderContentParser { scanner.curPosition, scanner.source ); - error.log(); + Logger.error(error.toString()); throw error; // #else throw new Error("reassign main entry"); From 9f8cbe71e7588ca68325f7fa38f4a736d8c1b219 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Wed, 9 Oct 2024 11:46:13 +0800 Subject: [PATCH 083/131] feat: code opt --- packages/shader-lab/src/GSError.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/shader-lab/src/GSError.ts b/packages/shader-lab/src/GSError.ts index 722cbfa485..2551598564 100644 --- a/packages/shader-lab/src/GSError.ts +++ b/packages/shader-lab/src/GSError.ts @@ -1,5 +1,4 @@ // #if _VERBOSE -import { Logger } from "@galacean/engine"; import { ShaderPosition } from "./common/ShaderPosition"; import { ShaderRange } from "./common/ShaderRange"; @@ -18,7 +17,6 @@ export class GSError extends Error { } override toString(): string { - if (!Logger.enable) return; let start: ShaderPosition, end: ShaderPosition; const { message, location: loc, source } = this; if (!source) { From 0df3db6f70cbdace2a3df7f1933a4eb8afd236cd Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 10 Oct 2024 16:04:52 +0800 Subject: [PATCH 084/131] fix: new scope when parse pass --- .../src/contentParser/ShaderContentParser.ts | 1 + tests/src/shader-lab/shaders/multi-pass.shader | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/packages/shader-lab/src/contentParser/ShaderContentParser.ts b/packages/shader-lab/src/contentParser/ShaderContentParser.ts index 5f8ae7e903..f2dc1e950e 100644 --- a/packages/shader-lab/src/contentParser/ShaderContentParser.ts +++ b/packages/shader-lab/src/contentParser/ShaderContentParser.ts @@ -457,6 +457,7 @@ export class ShaderContentParser { } private static _parsePass(scanner: Scanner): IShaderPassContent { + this._newScope(); const ret = { globalContents: [], renderStates: { constantMap: {}, variableMap: {} }, diff --git a/tests/src/shader-lab/shaders/multi-pass.shader b/tests/src/shader-lab/shaders/multi-pass.shader index 2e7314ee81..bfb68f50fd 100644 --- a/tests/src/shader-lab/shaders/multi-pass.shader +++ b/tests/src/shader-lab/shaders/multi-pass.shader @@ -2,6 +2,14 @@ Shader "Triangle" { SubShader "Default" { mat4 renderer_MVPMat; + BlendState transparentBlendState { + Enabled = true; + SourceColorBlendFactor = BlendFactor.SourceAlpha; + DestinationColorBlendFactor = BlendFactor.OneMinusSourceAlpha; + SourceAlphaBlendFactor = BlendFactor.One; + DestinationAlphaBlendFactor = BlendFactor.OneMinusSourceAlpha; + } + struct a2v { vec4 POSITION; vec2 TEXCOORD_0; @@ -52,6 +60,8 @@ Shader "Triangle" { Pass "0" { vec3 u_color; + BlendState = transparentBlendState; + struct a2v { vec4 POSITION; }; @@ -79,6 +89,8 @@ Shader "Triangle" { Pass "1" { vec3 u_color; + BlendState = transparentBlendState; + struct a2v { vec4 POSITION; }; From a72aadefe2b9093b586617d5ce9de66dfc3058a8 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 10 Oct 2024 17:34:18 +0800 Subject: [PATCH 085/131] feat: code opt --- packages/shader-lab/src/codeGen/CodeGenVisitor.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts index d7670ec154..f25940fa84 100644 --- a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts +++ b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts @@ -188,12 +188,9 @@ export class CodeGenVisitor { return this.defaultCodeGen(node.children); } - protected reportError(loc: ShaderRange | ShaderPosition, message: string): GSError { - let error: Error; + protected reportError(loc: ShaderRange | ShaderPosition, message: string): void { // #if _VERBOSE - error = new GSError(GSErrorName.CompilationError, message, loc, ShaderLab._processingPassText); - this._errors.push(error); - return error; + this._errors.push(new GSError(GSErrorName.CompilationError, message, loc, ShaderLab._processingPassText)); // #else throw new Error(message); // #endif From a19e9baa3dce5627a9b35d683d9d834c10f5eb64 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Fri, 11 Oct 2024 10:52:11 +0800 Subject: [PATCH 086/131] feat: code opt --- packages/shader-lab/src/GSError.ts | 10 +- packages/shader-lab/src/ShaderLab.ts | 40 +++--- packages/shader-lab/src/ShaderLabUtils.ts | 17 +++ .../shader-lab/src/codeGen/CodeGenVisitor.ts | 18 +-- .../shader-lab/src/codeGen/GLESVisitor.ts | 4 +- .../shader-lab/src/codeGen/VisitorContext.ts | 27 ++-- packages/shader-lab/src/common/BaseScanner.ts | 34 ++--- packages/shader-lab/src/common/BaseToken.ts | 8 +- .../shader-lab/src/common/ShaderPosition.ts | 14 +- .../shader-lab/src/contentParser/Scanner.ts | 2 + .../src/contentParser/ShaderContentParser.ts | 121 ++++++++---------- packages/shader-lab/src/lexer/Lexer.ts | 6 +- .../src/preprocessor/MacroDefine.ts | 54 ++++---- .../shader-lab/src/preprocessor/PpParser.ts | 21 ++- .../shader-lab/src/preprocessor/PpScanner.ts | 10 +- tests/src/shader-lab/ShaderLab.test.ts | 10 +- .../src/shader-lab/test-case/source/frag.txt | 3 +- 17 files changed, 214 insertions(+), 185 deletions(-) diff --git a/packages/shader-lab/src/GSError.ts b/packages/shader-lab/src/GSError.ts index 2551598564..27032ba4cd 100644 --- a/packages/shader-lab/src/GSError.ts +++ b/packages/shader-lab/src/GSError.ts @@ -18,16 +18,16 @@ export class GSError extends Error { override toString(): string { let start: ShaderPosition, end: ShaderPosition; - const { message, location: loc, source } = this; + const { message, location, source } = this; if (!source) { return message; } - if (loc instanceof ShaderPosition) { - start = end = loc; + if (location instanceof ShaderPosition) { + start = end = location; } else { - start = loc.start; - end = loc.end; + start = location.start; + end = location.end; } const lines = source.split("\n"); diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index 437257468f..d979392be8 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -5,7 +5,7 @@ import { GLES100Visitor, GLES300Visitor } from "./codeGen"; import { IShaderContent, IShaderLab } from "@galacean/engine-design"; 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"; // #if _VERBOSE import { GSError } from "./GSError"; @@ -19,11 +19,19 @@ export class ShaderLab implements IShaderLab { private static _shaderPositionPool = ShaderLabUtils.createObjectPool(ShaderPosition); private static _shaderRangePool = ShaderLabUtils.createObjectPool(ShaderRange); + // #if _VERBOSE static _processingPassText?: string; + // #endif static createPosition(index: number, line?: number, column?: number): ShaderPosition { const position = this._shaderPositionPool.get(); - position.set(index, line, column); + position.set( + index, + // #if _VERBOSE + line, + column + // #endif + ); return position; } @@ -34,14 +42,8 @@ export class ShaderLab implements IShaderLab { } // #if _VERBOSE - private _errors: GSError[] = []; - - /** - * Retrieve the compilation errors - */ - get errors(): GSError[] { - return this._errors; - } + /** Retrieve the compilation errors */ + readonly errors: GSError[] = []; // #endif _parseShaderPass( @@ -67,7 +69,7 @@ export class ShaderLab implements IShaderLab { // #if _VERBOSE if (PpParser._errors.length > 0) { for (const err of PpParser._errors) { - this._errors.push(err); + this.errors.push(err); } this._logErrors(); return { vertex: "", fragment: "" }; @@ -86,7 +88,7 @@ export class ShaderLab implements IShaderLab { // #if _VERBOSE for (const err of parser.errors) { - this._errors.push(err); + this.errors.push(err); } if (!program) { this._logErrors(); @@ -104,7 +106,7 @@ export class ShaderLab implements IShaderLab { // #if _VERBOSE for (const err of codeGen.errors) { - this._errors.push(err); + this.errors.push(err); } this._logErrors(); // #endif @@ -115,13 +117,15 @@ export class ShaderLab implements IShaderLab { _parseShaderContent(shaderSource: string): IShaderContent { ShaderLabUtils.clearAllShaderLabObjectPool(); ShaderContentParser.reset(); - // #if _VERBOSE - this._errors.length = 0; - // #endif const ret = ShaderContentParser.parse(shaderSource); + + // #if _VERBOSE + this.errors.length = 0; for (const error of ShaderContentParser._errors) { - this._errors.push(error); + this.errors.push(error); } + // #endif + return ret; } @@ -130,7 +134,7 @@ export class ShaderLab implements IShaderLab { * @internal */ _logErrors() { - const errors = this._errors; + const errors = this.errors; if (errors.length === 0 || !Logger.isEnabled) return; Logger.error(`${errors.length} errors occur!`); for (const err of errors) { diff --git a/packages/shader-lab/src/ShaderLabUtils.ts b/packages/shader-lab/src/ShaderLabUtils.ts index c10e6ff4d8..ed70cf4df2 100644 --- a/packages/shader-lab/src/ShaderLabUtils.ts +++ b/packages/shader-lab/src/ShaderLabUtils.ts @@ -1,4 +1,7 @@ import { ClearableObjectPool, IPoolElement } from "@galacean/engine"; +import { GSError, GSErrorName } from "./GSError"; +import { ShaderRange } from "./common/ShaderRange"; +import { ShaderPosition } from "./common/ShaderPosition"; export class ShaderLabUtils { private static _shaderLabObjectPoolSet: ClearableObjectPool[] = []; @@ -14,4 +17,18 @@ export class ShaderLabUtils { ShaderLabUtils._shaderLabObjectPoolSet[i].clear(); } } + + static createGSError( + message: string, + errorName: GSErrorName, + source: string, + location: ShaderRange | ShaderPosition, + file?: string + ) { + // #if _VERBOSE + return new GSError(errorName, message, location, source, file); + // #else + throw new Error(message); + // #endif + } } diff --git a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts index f25940fa84..19dacca853 100644 --- a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts +++ b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts @@ -16,11 +16,9 @@ import { ShaderLab } from "../ShaderLab"; * The code generator */ export class CodeGenVisitor { - protected _errors: GSError[] = []; - - get errors() { - return this._errors; - } + // #if _VERBOSE + readonly errors: GSError[] = []; + // #endif defaultCodeGen(children: NodeChild[]) { let ret: string[] = []; @@ -45,15 +43,19 @@ export class CodeGenVisitor { if (prop instanceof Token) { if (context.isAttributeStruct(postExpr.type)) { const error = context.referenceAttribute(prop); + // #if _VERBOSE if (error) { - this._errors.push(error); + this.errors.push(error); } + // #endif return prop.lexeme; } else if (context.isVaryingStruct(postExpr.type)) { const error = context.referenceVarying(prop); + // #if _VERBOSE if (error) { - this._errors.push(error); + this.errors.push(error); } + // #endif return prop.lexeme; } @@ -190,7 +192,7 @@ export class CodeGenVisitor { protected reportError(loc: ShaderRange | ShaderPosition, message: string): void { // #if _VERBOSE - this._errors.push(new GSError(GSErrorName.CompilationError, message, loc, ShaderLab._processingPassText)); + this.errors.push(new GSError(GSErrorName.CompilationError, message, loc, ShaderLab._processingPassText)); // #else throw new Error(message); // #endif diff --git a/packages/shader-lab/src/codeGen/GLESVisitor.ts b/packages/shader-lab/src/codeGen/GLESVisitor.ts index 401bea870e..bbee12473b 100644 --- a/packages/shader-lab/src/codeGen/GLESVisitor.ts +++ b/packages/shader-lab/src/codeGen/GLESVisitor.ts @@ -29,7 +29,9 @@ export abstract class GLESVisitor extends CodeGenVisitor { abstract getVaryingDeclare(): ICodeSegment[]; visitShaderProgram(node: ASTNode.GLShaderProgram, vertexEntry: string, fragmentEntry: string): IShaderInfo { - this._errors.length = 0; + // #if _VERBOSE + this.errors.length = 0; + // #endif VisitorContext.reset(); VisitorContext.context._passSymbolTable = node.shaderData.symbolTable; diff --git a/packages/shader-lab/src/codeGen/VisitorContext.ts b/packages/shader-lab/src/codeGen/VisitorContext.ts index 7109a0332c..dcb37dc753 100644 --- a/packages/shader-lab/src/codeGen/VisitorContext.ts +++ b/packages/shader-lab/src/codeGen/VisitorContext.ts @@ -7,6 +7,7 @@ import { GSErrorName, GSError } from "../GSError"; // #endif import { BaseToken } from "../common/BaseToken"; import { ShaderLab } from "../ShaderLab"; +import { ShaderLabUtils } from "../ShaderLabUtils"; /** @internal */ export class VisitorContext { @@ -63,17 +64,12 @@ export class VisitorContext { const prop = this.attributeList.find((item) => item.ident.lexeme === ident.lexeme); if (!prop) { - // #if _VERBOSE - return new GSError( - GSErrorName.CompilationError, + return ShaderLabUtils.createGSError( `referenced attribute not found: ${ident.lexeme}`, - ident.location, - ShaderLab._processingPassText + GSErrorName.CompilationError, + ShaderLab._processingPassText, + ident.location ); - // #else - // @ts-ignore - throw new Error(`referenced attribute not found: ${ident.lexeme}`); - // #endif } this._referencedAttributeList[ident.lexeme] = prop; } @@ -83,17 +79,12 @@ export class VisitorContext { const prop = this.varyingStruct?.propList.find((item) => item.ident.lexeme === ident.lexeme); if (!prop) { - // #if _VERBOSE - return new GSError( - GSErrorName.CompilationError, + return ShaderLabUtils.createGSError( `referenced varying not found: ${ident.lexeme}`, - ident.location, - ShaderLab._processingPassText + GSErrorName.CompilationError, + ShaderLab._processingPassText, + ident.location ); - // #else - // @ts-ignore - throw new Error(`referenced varying not found: ${ident.lexeme}`); - // #endif } this._referencedVaryingList[ident.lexeme] = prop; } diff --git a/packages/shader-lab/src/common/BaseScanner.ts b/packages/shader-lab/src/common/BaseScanner.ts index ab932754f3..4e8281bd51 100644 --- a/packages/shader-lab/src/common/BaseScanner.ts +++ b/packages/shader-lab/src/common/BaseScanner.ts @@ -5,6 +5,7 @@ import { GSError, GSErrorName } from "../GSError"; // #endif import { ShaderLab } from "../ShaderLab"; import { BaseToken } from "./BaseToken"; +import { ShaderLabUtils } from "../ShaderLabUtils"; export type OnToken = (token: BaseToken, scanner: BaseScanner) => void; @@ -27,8 +28,10 @@ export default class BaseScanner { protected _currentIndex = 0; protected _source: string; + // #if _VERBOSE protected _column = 0; protected _line = 0; + // #endif get current(): number { return this._currentIndex; @@ -38,8 +41,14 @@ export default class BaseScanner { return this._source; } - get curPosition(): ShaderPosition { - return ShaderLab.createPosition(this._currentIndex, this._line, this._column); + getCurPosition(): ShaderPosition { + return ShaderLab.createPosition( + this._currentIndex, + // #if _VERBOSE + this._line, + this._column + // #endif + ); } get line() { @@ -98,20 +107,20 @@ export default class BaseScanner { skipCommentsAndSpace(): ShaderRange | undefined { this.skipSpace(true); if (this.peek(2) === "//") { - const start = this.curPosition; + const start = this.getCurPosition(); this.advance(2); // single line comments while (this.getCurChar() !== "\n") this._advance(); this.skipCommentsAndSpace(); - return ShaderLab.createRange(start, this.curPosition); + return ShaderLab.createRange(start, this.getCurPosition()); } else if (this.peek(2) === "/*") { - const start = this.curPosition; + const start = this.getCurPosition(); this.advance(2); // multi-line comments while (this.peek(2) !== "*/" && !this.isEnd()) this._advance(); this.advance(2); this.skipCommentsAndSpace(); - return ShaderLab.createRange(start, this.curPosition); + return ShaderLab.createRange(start, this.getCurPosition()); } } @@ -124,19 +133,14 @@ export default class BaseScanner { this.skipCommentsAndSpace(); const peek = this.peek(text.length); if (peek !== text) { - this.throwError(this.curPosition, `Expect text "${text}", but got "${peek}"`); + this.throwError(this.getCurPosition(), `Expect text "${text}", but got "${peek}"`); } this.advance(text.length); } throwError(pos: ShaderPosition | ShaderRange, ...msgs: any[]) { - // #if _VERBOSE - const error = new GSError(GSErrorName.ScannerError, msgs.join(" "), pos, this._source); - Logger.error(error.toString()); + const error = ShaderLabUtils.createGSError(msgs.join(" "), GSErrorName.ScannerError, this._source, pos); throw error; - // #else - throw new Error(msgs.join("")); - // #endif } scanPairedText(left: string, right: string, balanced = false, skipLeading = false) { @@ -167,10 +171,10 @@ export default class BaseScanner { scanToken(onToken?: OnToken, splitCharRegex = /\w/) { this.skipCommentsAndSpace(); - const start = this.curPosition; + const start = this.getCurPosition(); if (this.isEnd()) return; while (splitCharRegex.test(this.getCurChar()) && !this.isEnd()) this._advance(); - const end = this.curPosition; + const end = this.getCurPosition(); if (start.index === end.index) { this._advance(); diff --git a/packages/shader-lab/src/common/BaseToken.ts b/packages/shader-lab/src/common/BaseToken.ts index 380cbc0891..21d9a2aba8 100644 --- a/packages/shader-lab/src/common/BaseToken.ts +++ b/packages/shader-lab/src/common/BaseToken.ts @@ -20,7 +20,13 @@ export class BaseToken implements IPoolElement { if (arg instanceof ShaderRange) { this.location = arg as ShaderRange; } else { - const end = ShaderLab.createPosition(arg.index + lexeme.length, arg.line, arg.column + lexeme.length); + const end = ShaderLab.createPosition( + arg.index + lexeme.length, + // #if _VERBOSE + arg.line, + arg.column + lexeme.length + // #endif + ); this.location = ShaderLab.createRange(arg, end); } } diff --git a/packages/shader-lab/src/common/ShaderPosition.ts b/packages/shader-lab/src/common/ShaderPosition.ts index 01d6babc41..9dc3820b33 100644 --- a/packages/shader-lab/src/common/ShaderPosition.ts +++ b/packages/shader-lab/src/common/ShaderPosition.ts @@ -2,18 +2,30 @@ import { IPoolElement } from "@galacean/engine"; export class ShaderPosition implements IPoolElement { index: number; + // #if _VERBOSE line: number; column: number; + // #endif - set(index: number, line: number, column: number) { + set( + index: number, + // #if _VERBOSE + line: number, + column: number + // #endif + ) { this.index = index; + // #if _VERBOSE this.line = line; this.column = column; + // #endif } dispose(): void { this.index = 0; + // #if _VERBOSE this.line = 0; this.column = 0; + // #endif } } diff --git a/packages/shader-lab/src/contentParser/Scanner.ts b/packages/shader-lab/src/contentParser/Scanner.ts index 3d3aa9b527..9661240cd8 100644 --- a/packages/shader-lab/src/contentParser/Scanner.ts +++ b/packages/shader-lab/src/contentParser/Scanner.ts @@ -26,10 +26,12 @@ export default class Scanner extends BaseScanner { return Number(this._source.substring(start, this._currentIndex)); } + // #if _VERBOSE scanToCharacter(char: string): void { while (this.getCurChar() !== char && !this.isEnd()) { this._advance(); } this._advance(); } + // #endif } diff --git a/packages/shader-lab/src/contentParser/ShaderContentParser.ts b/packages/shader-lab/src/contentParser/ShaderContentParser.ts index f2dc1e950e..bd0df38daf 100644 --- a/packages/shader-lab/src/contentParser/ShaderContentParser.ts +++ b/packages/shader-lab/src/contentParser/ShaderContentParser.ts @@ -25,6 +25,7 @@ import { } from "@galacean/engine-design"; // #if _VERBOSE import { GSError, GSErrorName } from "../GSError"; +import { ShaderLabUtils } from "../ShaderLabUtils"; // #endif const EngineType = [ @@ -113,7 +114,7 @@ export class ShaderContentParser { private static _parseShaderStatements(ret: IShaderContent, scanner: Scanner) { let braceLevel = 1; - let start = scanner.curPosition; + let start = scanner.getCurPosition(); while (true) { const word = scanner.scanToken(); @@ -122,20 +123,20 @@ export class ShaderContentParser { this._addGlobalStatement(ret, scanner, start, word.lexeme.length); const subShader = this._parseSubShader(scanner); ret.subShaders.push(subShader); - start = scanner.curPosition; + start = scanner.getCurPosition(); break; case EKeyword.GS_EditorProperties: case EKeyword.GS_EditorMacros: this._addGlobalStatement(ret, scanner, start, word.lexeme.length); scanner.scanPairedText("{", "}", true); - start = scanner.curPosition; + start = scanner.getCurPosition(); break; case EKeyword.GS_RenderQueueType: this._addGlobalStatement(ret, scanner, start, word.lexeme.length); this._parseRenderQueueAssignment(ret, scanner); - start = scanner.curPosition; + start = scanner.getCurPosition(); break; case ETokenType.NOT_WORD: @@ -153,12 +154,12 @@ export class ShaderContentParser { if (ShaderContentParser._isRenderStateDeclarator(word)) { this._addGlobalStatement(ret, scanner, start, word.lexeme.length); this._parseRenderStateDeclarationOrAssignment(ret, word, scanner); - start = scanner.curPosition; + start = scanner.getCurPosition(); break; } else if (ShaderContentParser._isEngineType(word)) { this._addGlobalStatement(ret, scanner, start, word.lexeme.length); this._parseVariableDeclaration(word.type, scanner); - start = scanner.curPosition; + start = scanner.getCurPosition(); break; } } @@ -182,18 +183,15 @@ export class ShaderContentParser { scanner.scanText(";"); const sm = this._symbolTable.lookup({ type: stateToken.type, ident: variable.lexeme }); if (!sm?.value) { - // #if _VERBOSE - this._errors.push( - new GSError( - GSErrorName.CompilationError, - `Invalid "${stateToken.lexeme}" variable: ${variable.lexeme}`, - variable.location, - scanner.source - ) + const error = ShaderLabUtils.createGSError( + `Invalid "${stateToken.lexeme}" variable: ${variable.lexeme}`, + GSErrorName.CompilationError, + scanner.source, + variable.location ); + // #if _VERBOSE + this._errors.push(error); return; - // #else - throw new Error(`Invalid "${stateToken.lexeme}" variable: ${variable.lexeme}`); // #endif } const renderState = sm.value as IRenderStates; @@ -242,19 +240,16 @@ export class ShaderContentParser { scanner.scanText("]"); scanner.scanText("="); } else if (op.lexeme !== "=") { - // #if _VERBOSE - this._errors.push( - new GSError( - GSErrorName.CompilationError, - `Invalid syntax, expect character '=', but got ${op.lexeme}`, - scanner.curPosition, - scanner.source - ) + const error = ShaderLabUtils.createGSError( + `Invalid syntax, expect character '=', but got ${op.lexeme}`, + GSErrorName.CompilationError, + scanner.source, + scanner.getCurPosition() ); + // #if _VERBOSE + this._errors.push(error); scanner.scanToCharacter(";"); return; - // #else - throw new Error(`Invalid syntax, expect character '=', but got ${op.lexeme}`); // #endif } renderStateProp += idx; @@ -263,19 +258,16 @@ export class ShaderContentParser { renderStateProp = state + renderStateProp; const renderStateElementKey = RenderStateDataKey[renderStateProp]; if (renderStateElementKey == undefined) { - // #if _VERBOSE - this._errors.push( - new GSError( - GSErrorName.CompilationError, - `Invalid render state element ${renderStateProp}`, - scanner.curPosition, - scanner.source - ) + const error = ShaderLabUtils.createGSError( + `Invalid render state element ${renderStateProp}`, + GSErrorName.CompilationError, + scanner.source, + scanner.getCurPosition() ); + // #if _VERBOSE + this._errors.push(error); scanner.scanToCharacter(";"); return; - // #else - throw new Error(`Invalid render state element ${renderStateProp}`); // #endif } @@ -306,19 +298,16 @@ export class ShaderContentParser { const engineTypeProp = scanner.scanToken(); value = ShaderContentParser._engineType[token.lexeme]?.[engineTypeProp.lexeme]; if (value == undefined) { - // #if _VERBOSE - this._errors.push( - new GSError( - GSErrorName.CompilationError, - `Invalid engine constant: ${token.lexeme}.${engineTypeProp.lexeme}`, - engineTypeProp.location, - scanner.source - ) + const error = ShaderLabUtils.createGSError( + `Invalid engine constant: ${token.lexeme}.${engineTypeProp.lexeme}`, + GSErrorName.CompilationError, + scanner.source, + engineTypeProp.location ); + // #if _VERBOSE + this._errors.push(error); scanner.scanToCharacter(";"); return; - // #else - throw new Error(`Invalid engine constant: ${token.lexeme}.${engineTypeProp.lexeme}`); // #endif } } else { @@ -360,7 +349,7 @@ export class ShaderContentParser { ) { if (scanner.current > start.index + offset) { ret.globalContents.push({ - range: { start, end: { ...scanner.curPosition, index: scanner.current - offset - 1 } }, + range: { start, end: { ...scanner.getCurPosition(), index: scanner.current - offset - 1 } }, content: scanner.source.substring(start.index, scanner.current - offset - 1) }); } @@ -379,7 +368,7 @@ export class ShaderContentParser { scanner.scanText("{"); scanner.skipCommentsAndSpace(); - let start = scanner.curPosition; + let start = scanner.getCurPosition(); while (true) { const word = scanner.scanToken(); @@ -388,13 +377,13 @@ export class ShaderContentParser { this._addGlobalStatement(ret, scanner, start, word.lexeme.length); const pass = this._parsePass(scanner); ret.passes.push(pass); - start = scanner.curPosition; + start = scanner.getCurPosition(); break; case EKeyword.GS_RenderQueueType: this._addGlobalStatement(ret, scanner, start, word.lexeme.length); this._parseRenderQueueAssignment(ret, scanner); - start = scanner.curPosition; + start = scanner.getCurPosition(); break; case EKeyword.GS_UsePass: @@ -402,13 +391,13 @@ export class ShaderContentParser { const name = scanner.scanPairedText('"', '"'); // @ts-ignore ret.passes.push({ name, isUsePass: true, renderStates: { constantMap: {}, variableMap: {} }, tags: {} }); - start = scanner.curPosition; + start = scanner.getCurPosition(); break; case EKeyword.GS_Tags: this._addGlobalStatement(ret, scanner, start, word.lexeme.length); this._parseTags(ret, scanner); - start = scanner.curPosition; + start = scanner.getCurPosition(); break; case ETokenType.NOT_WORD: @@ -426,12 +415,12 @@ export class ShaderContentParser { if (ShaderContentParser._isRenderStateDeclarator(word)) { this._addGlobalStatement(ret, scanner, start, word.lexeme.length); this._parseRenderStateDeclarationOrAssignment(ret, word, scanner); - start = scanner.curPosition; + start = scanner.getCurPosition(); break; } else if (ShaderContentParser._isEngineType(word)) { this._addGlobalStatement(ret, scanner, start, word.lexeme.length); this._parseVariableDeclaration(word.type, scanner); - start = scanner.curPosition; + start = scanner.getCurPosition(); break; } } @@ -470,7 +459,7 @@ export class ShaderContentParser { let braceLevel = 1; scanner.skipCommentsAndSpace(); - let start = scanner.curPosition; + let start = scanner.getCurPosition(); while (true) { const word = scanner.scanToken(); @@ -478,13 +467,13 @@ export class ShaderContentParser { case EKeyword.GS_RenderQueueType: this._addGlobalStatement(ret, scanner, start, word.lexeme.length); this._parseRenderQueueAssignment(ret, scanner); - start = scanner.curPosition; + start = scanner.getCurPosition(); break; case EKeyword.GS_Tags: this._addGlobalStatement(ret, scanner, start, word.lexeme.length); this._parseTags(ret, scanner); - start = scanner.curPosition; + start = scanner.getCurPosition(); break; case EKeyword.GS_VertexShader: @@ -493,23 +482,21 @@ export class ShaderContentParser { scanner.scanText("="); const entry = scanner.scanToken(); if (ret[word.lexeme]) { - // #if _VERBOSE - const error = new GSError( - GSErrorName.CompilationError, + const error = ShaderLabUtils.createGSError( "reassign main entry", - scanner.curPosition, - scanner.source + GSErrorName.CompilationError, + scanner.source, + scanner.getCurPosition() ); + // #if _VERBOSE Logger.error(error.toString()); throw error; - // #else - throw new Error("reassign main entry"); // #endif } const key = word.type === EKeyword.GS_VertexShader ? "vertexEntry" : "fragmentEntry"; ret[key] = entry.lexeme; scanner.scanText(";"); - start = scanner.curPosition; + start = scanner.getCurPosition(); break; case ETokenType.NOT_WORD: @@ -527,12 +514,12 @@ export class ShaderContentParser { if (ShaderContentParser._isRenderStateDeclarator(word)) { this._addGlobalStatement(ret, scanner, start, word.lexeme.length); this._parseRenderStateDeclarationOrAssignment(ret, word, scanner); - start = scanner.curPosition; + start = scanner.getCurPosition(); break; } else if (ShaderContentParser._isEngineType(word)) { this._addGlobalStatement(ret, scanner, start, word.lexeme.length); this._parseVariableDeclaration(word.type, scanner); - start = scanner.curPosition; + start = scanner.getCurPosition(); break; } } diff --git a/packages/shader-lab/src/lexer/Lexer.ts b/packages/shader-lab/src/lexer/Lexer.ts index abe0c10a8c..326aa79da1 100644 --- a/packages/shader-lab/src/lexer/Lexer.ts +++ b/packages/shader-lab/src/lexer/Lexer.ts @@ -12,7 +12,9 @@ export class Lexer extends BaseScanner { reset(source: string) { this._source = source; this._currentIndex = 0; + // #if _VERBOSE this._line = this._column = 0; + // #endif } *tokenize() { @@ -276,7 +278,7 @@ export class Lexer extends BaseScanner { return this._scanStringConst(); default: - this.throwError(this.curPosition, `Unexpected character ${this.getCurChar()}`); + this.throwError(this.getCurPosition(), `Unexpected character ${this.getCurChar()}`); } return token; } @@ -383,7 +385,7 @@ export class Lexer extends BaseScanner { this.advance(); } if (!LexerUtils.isNum(this.getCurChar())) - this.throwError(this.curPosition, "lexing error, invalid exponent suffix."); + this.throwError(this.getCurPosition(), "lexing error, invalid exponent suffix."); while (LexerUtils.isNum(this.getCurChar())) { buffer.push(this.getCurChar()); this.advance(); diff --git a/packages/shader-lab/src/preprocessor/MacroDefine.ts b/packages/shader-lab/src/preprocessor/MacroDefine.ts index 7d7f2db229..53e3253adb 100644 --- a/packages/shader-lab/src/preprocessor/MacroDefine.ts +++ b/packages/shader-lab/src/preprocessor/MacroDefine.ts @@ -3,11 +3,6 @@ import { ShaderRange } from "../common"; import { GSError, GSErrorName } from "../GSError"; export class MacroDefine { - readonly location?: ShaderRange; - readonly macro: BaseToken; - readonly args?: BaseToken[]; - readonly body?: BaseToken; - get isFunction() { return !!this.args?.length; } @@ -16,34 +11,33 @@ export class MacroDefine { return this.macro.lexeme; } - constructor(macro: BaseToken, body?: BaseToken, loc?: ShaderRange, args?: BaseToken[]) { - this.location = loc; - this.macro = macro; - this.body = body; - this.args = args; - } + private _replaceRegex?: RegExp; - private _expand(...args: string[]): string { - if (this.isFunction) { - const argsTextList = this.args!.map((item) => item.lexeme); - - // #if _VERBOSE - if (args.length !== this.args?.length) { - throw new GSError(GSErrorName.PreprocessorError, "mismatched function macro", this.location, ""); - } - // #endif - const replaceRegex = new RegExp(`\\b(${argsTextList.join("|")})\\b`, "g"); - return this.body.lexeme.replaceAll(replaceRegex, (_, m) => { - const idx = argsTextList.findIndex((item) => item === m); - return args[idx]; - }); + constructor( + public readonly macro: BaseToken, + public readonly body?: BaseToken, + public readonly location?: ShaderRange, + public readonly args?: BaseToken[] + ) { + if (args) { + const argsTextList = this.args.map((item) => item.lexeme); + this._replaceRegex = new RegExp(`\\b(${argsTextList.join("|")})\\b`, "g"); } - return this.body.lexeme; } - expand(...args: string[]): string { - const ret = this._expand(...args); - // TODO: erase the comments, any more performant and lightweight solution? - return ret.replaceAll(/(\/\/[^\n]*|\/\*.*\*\/)/gs, ""); + expandFunctionBody(args: string[]): string { + const argsTextList = this.args!.map((item) => item.lexeme); + + // #if _VERBOSE + if (args.length !== this.args?.length) { + throw new GSError(GSErrorName.PreprocessorError, "mismatched function macro", this.location, ""); + } + // #endif + + const expanded = this.body.lexeme.replace(this._replaceRegex, (_, m) => { + return args[argsTextList.indexOf(m)]; + }); + + return expanded; } } diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 3cfc192283..8a9a534e0e 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -11,6 +11,7 @@ import PpScanner from "./PpScanner"; import { PpUtils } from "./Utils"; import { ShaderLab } from "../ShaderLab"; import { ShaderPass } from "@galacean/engine"; +import { ShaderLabUtils } from "../ShaderLabUtils"; export interface ExpandSegment { // #if _VERBOSE @@ -107,11 +108,8 @@ export class PpParser { } private static reportError(loc: ShaderRange | ShaderPosition, message: string, source: string, file: string) { - // #if _VERBOSE - this._errors.push(new GSError(GSErrorName.PreprocessorError, message, loc, source, file)); - // #else - throw new Error(message); - // #endif + const error = ShaderLabUtils.createGSError(message, GSErrorName.PreprocessorError, source, loc, file); + this._errors.push(error); } private static _parseInclude(scanner: PpScanner) { @@ -556,7 +554,7 @@ export class PpParser { const block = new BlockInfo(scanner.file, scanner.blockRange); // #endif const startPosition = ShaderLab.createPosition(start); - const endPosition = scanner.curPosition; + const endPosition = scanner.getCurPosition(); const range = ShaderLab.createRange(startPosition, endPosition); this.expandSegments.push({ // #if _VERBOSE @@ -623,7 +621,7 @@ export class PpParser { // #if _VERBOSE block, // #endif - rangeInBlock: ShaderLab.createRange(start, scanner.curPosition), + rangeInBlock: ShaderLab.createRange(start, scanner.getCurPosition()), replace: "" }); } @@ -636,7 +634,7 @@ export class PpParser { const block = new BlockInfo(scanner.file, scanner.blockRange); // #endif const startPosition = ShaderLab.createPosition(start); - const range = ShaderLab.createRange(startPosition, scanner.curPosition); + const range = ShaderLab.createRange(startPosition, scanner.getCurPosition()); this.expandSegments.push({ // #if _VERBOSE block, @@ -668,6 +666,7 @@ export class PpParser { const macro = this._definedMacros.get(token.lexeme); if (macro) { let replace = macro.body.lexeme; + debugger; if (macro.isFunction) { scanner.scanToChar("("); scanner.advance(); @@ -690,13 +689,13 @@ export class PpParser { args.push(scanner.source.slice(curIdx, scanner.current)); scanner.advance(); - const range = ShaderLab.createRange(token.location!.start, scanner.curPosition); - replace = macro.expand(...args); + const range = ShaderLab.createRange(token.location!.start, scanner.getCurPosition()); + replace = macro.expandFunctionBody(args); const expanded = this._expandMacroChunk(replace, range, scanner); // #if _VERBOSE const block = new BlockInfo(scanner.file, scanner.blockRange, expanded.sourceMap); // #endif - const blockRange = ShaderLab.createRange(token.location!.start, scanner.curPosition); + const blockRange = ShaderLab.createRange(token.location!.start, scanner.getCurPosition()); this.expandSegments.push({ // #if _VERBOSE block, diff --git a/packages/shader-lab/src/preprocessor/PpScanner.ts b/packages/shader-lab/src/preprocessor/PpScanner.ts index e57e56716a..5df0945b5c 100644 --- a/packages/shader-lab/src/preprocessor/PpScanner.ts +++ b/packages/shader-lab/src/preprocessor/PpScanner.ts @@ -101,7 +101,13 @@ export default class PpScanner extends BaseScanner { } getShaderPosition(offset /** offset from starting point */ = 0) { - return ShaderLab.createPosition(this._currentIndex - offset, this.line, this.column - offset); + return ShaderLab.createPosition( + this._currentIndex - offset, + // #if _VERBOSE + this.line, + this.column - offset + // #endif + ); } /** @@ -292,7 +298,7 @@ export default class PpScanner extends BaseScanner { while (this.getCurChar() !== "\n" && !this.isEnd()) { this._advance(); } - return ShaderLab.createRange(start, this.curPosition); + return ShaderLab.createRange(start, this.getCurPosition()); } else if (this.peek(2) === "/*") { const start = this.getShaderPosition(); // multi-line comments diff --git a/tests/src/shader-lab/ShaderLab.test.ts b/tests/src/shader-lab/ShaderLab.test.ts index ce901ecd48..dbb3717f78 100644 --- a/tests/src/shader-lab/ShaderLab.test.ts +++ b/tests/src/shader-lab/ShaderLab.test.ts @@ -243,16 +243,16 @@ describe("ShaderLab", () => { const errorShader = fs.readFileSync(path.join(__dirname, "shaders/compilation-error.shader")).toString(); shaderParse.bind(shaderLabVerbose)(errorShader); // @ts-ignore - expect(shaderLabVerbose._errors.length).to.eq(3); + expect(shaderLabVerbose.errors.length).to.eq(3); // @ts-ignore - assert.instanceOf(shaderLabVerbose._errors[0], GSError); + assert.instanceOf(shaderLabVerbose.errors[0], GSError); // @ts-ignore - assert.instanceOf(shaderLabVerbose._errors[1], GSError); + assert.instanceOf(shaderLabVerbose.errors[1], GSError); // @ts-ignore - assert.instanceOf(shaderLabVerbose._errors[2], GSError); + assert.instanceOf(shaderLabVerbose.errors[2], GSError); // @ts-ignore - for (const err of shaderLabVerbose._errors) { + for (const err of shaderLabVerbose.errors) { console.log(err.toString()); } diff --git a/tests/src/shader-lab/test-case/source/frag.txt b/tests/src/shader-lab/test-case/source/frag.txt index c0ee4f0cea..e697d6028d 100644 --- a/tests/src/shader-lab/test-case/source/frag.txt +++ b/tests/src/shader-lab/test-case/source/frag.txt @@ -10,7 +10,8 @@ void Gerstner() { // #include "ShadowCoord" -#define TT 1.0 // comments +#define TT 1./** comments +*/0 // test #define QQ vec4 a = vec4(TT,1.0,3.0,4.0); From 4c47c15e77f43d9e6022fa699001724d32c9620e Mon Sep 17 00:00:00 2001 From: Sway007 Date: Fri, 11 Oct 2024 10:57:40 +0800 Subject: [PATCH 087/131] feat: code opt --- packages/shader-lab/src/common/BaseScanner.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/shader-lab/src/common/BaseScanner.ts b/packages/shader-lab/src/common/BaseScanner.ts index 4e8281bd51..f70ae8b4a8 100644 --- a/packages/shader-lab/src/common/BaseScanner.ts +++ b/packages/shader-lab/src/common/BaseScanner.ts @@ -85,12 +85,15 @@ export default class BaseScanner { return; } + // #if _VERBOSE if (this.getCurChar() === "\n") { this._line += 1; this._column = 0; } else { this._column += 1; } + // #endif + this._currentIndex++; } From d83cb2935eb268f6e9dbf856b49d39f78627b1c1 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Fri, 11 Oct 2024 11:20:12 +0800 Subject: [PATCH 088/131] feat: code opt --- packages/shader-lab/src/ShaderLabUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shader-lab/src/ShaderLabUtils.ts b/packages/shader-lab/src/ShaderLabUtils.ts index ed70cf4df2..aaf0feed82 100644 --- a/packages/shader-lab/src/ShaderLabUtils.ts +++ b/packages/shader-lab/src/ShaderLabUtils.ts @@ -13,7 +13,7 @@ export class ShaderLabUtils { } static clearAllShaderLabObjectPool() { - for (let i = 0; i < ShaderLabUtils._shaderLabObjectPoolSet.length; i++) { + for (let i = 0, n = ShaderLabUtils._shaderLabObjectPoolSet.length; i < n; i++) { ShaderLabUtils._shaderLabObjectPoolSet[i].clear(); } } From a180d29d956e638e5a0bfc2c8ba917a5530ecf48 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Sat, 12 Oct 2024 11:49:50 +0800 Subject: [PATCH 089/131] feat: code opt --- packages/shader-lab/src/preprocessor/PpParser.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 8a9a534e0e..32560cb95c 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -666,7 +666,6 @@ export class PpParser { const macro = this._definedMacros.get(token.lexeme); if (macro) { let replace = macro.body.lexeme; - debugger; if (macro.isFunction) { scanner.scanToChar("("); scanner.advance(); From 7c57e150917e295779662506059819b8e3147806 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Sat, 12 Oct 2024 13:48:42 +0800 Subject: [PATCH 090/131] feat: add testcase for BaseScanner --- packages/shader-lab/src/index.ts | 4 +++ tests/src/shader-lab/Scanner.test.ts | 42 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 tests/src/shader-lab/Scanner.test.ts diff --git a/packages/shader-lab/src/index.ts b/packages/shader-lab/src/index.ts index 637143b64d..3c8e899c1d 100644 --- a/packages/shader-lab/src/index.ts +++ b/packages/shader-lab/src/index.ts @@ -1,5 +1,9 @@ export { ShaderLab } from "./ShaderLab"; +import BaseScanner from "./common/BaseScanner"; +/** @internal */ +export { BaseScanner }; + // #if _VERBOSE export { Preprocessor } from "./preprocessor"; export * from "./GSError"; diff --git a/tests/src/shader-lab/Scanner.test.ts b/tests/src/shader-lab/Scanner.test.ts new file mode 100644 index 0000000000..148add8d73 --- /dev/null +++ b/tests/src/shader-lab/Scanner.test.ts @@ -0,0 +1,42 @@ +// @ts-ignore +import { BaseScanner } from "@galacean/engine-shader-lab"; +import { expect } from "chai"; + +describe("BaseScanner", () => { + describe("skipCommentsAndSpace()", () => { + it("should skip over multi-line comments and proceed to the next token", () => { + const source = `/* This is a + multi-line comment */ + nextToken`; + const scanner = new BaseScanner(source); + + scanner.skipCommentsAndSpace(); + + const token = scanner.scanToken(); + expect(token?.lexeme).to.equal("nextToken"); + }); + + it("should skip over multiple multi-line comments and spaces", () => { + const source = `/* First comment */ + /* Second comment */ + + thirdToken`; + const scanner = new BaseScanner(source); + + scanner.skipCommentsAndSpace(); + + const token = scanner.scanToken(); + expect(token?.lexeme).to.equal("thirdToken"); + }); + + it("should handle multi-line comments without leading spaces", () => { + const source = `/*Comment without leading space*/nextToken`; + const scanner = new BaseScanner(source); + + scanner.skipCommentsAndSpace(); + + const token = scanner.scanToken(); + expect(token?.lexeme).to.equal("nextToken"); + }); + }); +}); From 4c63401b6500dc17750badfdcc700f37fcee296b Mon Sep 17 00:00:00 2001 From: Sway007 Date: Sat, 12 Oct 2024 15:26:59 +0800 Subject: [PATCH 091/131] feat: code opt --- .../shader-lab/src/codeGen/CodeGenVisitor.ts | 2 +- packages/shader-lab/src/codeGen/GLESVisitor.ts | 6 +++--- .../src/contentParser/ShaderContentParser.ts | 12 +++++++----- .../src/parser/ShaderTargetParser.ts | 18 ++++++++---------- .../shader-lab/src/preprocessor/MacroDefine.ts | 5 ++--- 5 files changed, 21 insertions(+), 22 deletions(-) diff --git a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts index 19dacca853..288393045d 100644 --- a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts +++ b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts @@ -190,7 +190,7 @@ export class CodeGenVisitor { return this.defaultCodeGen(node.children); } - protected reportError(loc: ShaderRange | ShaderPosition, message: string): void { + protected _reportError(loc: ShaderRange | ShaderPosition, message: string): void { // #if _VERBOSE this.errors.push(new GSError(GSErrorName.CompilationError, message, loc, ShaderLab._processingPassText)); // #else diff --git a/packages/shader-lab/src/codeGen/GLESVisitor.ts b/packages/shader-lab/src/codeGen/GLESVisitor.ts index bbee12473b..e3923dcf3e 100644 --- a/packages/shader-lab/src/codeGen/GLESVisitor.ts +++ b/packages/shader-lab/src/codeGen/GLESVisitor.ts @@ -53,12 +53,12 @@ export abstract class GLESVisitor extends CodeGenVisitor { if (typeof returnType.type === "string") { const varyStruct = symbolTable.lookup({ ident: returnType.type, symbolType: ESymbolType.STRUCT }); if (!varyStruct) { - this.reportError(returnType.location, `invalid varying struct: ${returnType.type}`); + this._reportError(returnType.location, `invalid varying struct: ${returnType.type}`); } else { VisitorContext.context.varyingStruct = varyStruct.astNode; } } else if (returnType.type !== EKeyword.VOID) { - this.reportError(returnType.location, "main entry can only return struct."); + this._reportError(returnType.location, "main entry can only return struct."); } const paramList = fnNode.protoType.parameterList; @@ -70,7 +70,7 @@ export abstract class GLESVisitor extends CodeGenVisitor { symbolType: ESymbolType.STRUCT }); if (!structSymbol) { - this.reportError(paramInfo.astNode.location, `Not found attribute struct "${paramInfo.typeInfo.type}".`); + this._reportError(paramInfo.astNode.location, `Not found attribute struct "${paramInfo.typeInfo.type}".`); continue; } VisitorContext.context.attributeStructs.push(structSymbol.astNode); diff --git a/packages/shader-lab/src/contentParser/ShaderContentParser.ts b/packages/shader-lab/src/contentParser/ShaderContentParser.ts index bd0df38daf..d2cc87be39 100644 --- a/packages/shader-lab/src/contentParser/ShaderContentParser.ts +++ b/packages/shader-lab/src/contentParser/ShaderContentParser.ts @@ -328,13 +328,15 @@ export class ShaderContentParser { scanner.scanText(";"); const value = ShaderContentParser._engineType.RenderQueueType[word.lexeme]; if (value == undefined) { - // #if _VERBOSE - this._errors.push( - new GSError(GSErrorName.CompilationError, `Invalid render queue ${word.lexeme}`, word.location, scanner.source) + const error = ShaderLabUtils.createGSError( + `Invalid render queue ${word.lexeme}`, + GSErrorName.CompilationError, + scanner.source, + word.location ); + // #if _VERBOSE + this._errors.push(error); return; - // #else - throw new Error(`Invalid render queue ${word.lexeme}`); // #endif } const key = RenderStateDataKey.RenderQueueType; diff --git a/packages/shader-lab/src/parser/ShaderTargetParser.ts b/packages/shader-lab/src/parser/ShaderTargetParser.ts index f30cb49eeb..574e9ca511 100644 --- a/packages/shader-lab/src/parser/ShaderTargetParser.ts +++ b/packages/shader-lab/src/parser/ShaderTargetParser.ts @@ -14,6 +14,7 @@ import { Logger } from "@galacean/engine"; import { GSError, GSErrorName } from "../GSError"; // #endif import { ShaderLab } from "../ShaderLab"; +import { ShaderLabUtils } from "../ShaderLabUtils"; /** * The syntax parser and sematic analyzer of `ShaderLab` compiler @@ -114,18 +115,15 @@ export class ShaderTargetParser { traceBackStack.push(nextState); continue; } else { - // #if _VERBOSE - this.sematicAnalyzer.errors.push( - new GSError( - GSErrorName.CompilationError, - `Unexpected token ${token.lexeme}`, - token.location, - ShaderLab._processingPassText - ) + const error = ShaderLabUtils.createGSError( + `Unexpected token ${token.lexeme}`, + GSErrorName.CompilationError, + ShaderLab._processingPassText, + token.location ); + // #if _VERBOSE + this.sematicAnalyzer.errors.push(error); return null; - // #else - throw new Error(`Unexpected token ${token.lexeme}`); // #endif } } diff --git a/packages/shader-lab/src/preprocessor/MacroDefine.ts b/packages/shader-lab/src/preprocessor/MacroDefine.ts index 53e3253adb..df341db8ef 100644 --- a/packages/shader-lab/src/preprocessor/MacroDefine.ts +++ b/packages/shader-lab/src/preprocessor/MacroDefine.ts @@ -1,6 +1,7 @@ import { BaseToken } from "../common/BaseToken"; import { ShaderRange } from "../common"; import { GSError, GSErrorName } from "../GSError"; +import { ShaderLabUtils } from "../ShaderLabUtils"; export class MacroDefine { get isFunction() { @@ -28,11 +29,9 @@ export class MacroDefine { expandFunctionBody(args: string[]): string { const argsTextList = this.args!.map((item) => item.lexeme); - // #if _VERBOSE if (args.length !== this.args?.length) { - throw new GSError(GSErrorName.PreprocessorError, "mismatched function macro", this.location, ""); + throw ShaderLabUtils.createGSError("mismatched function macro", GSErrorName.PreprocessorError, "", this.location); } - // #endif const expanded = this.body.lexeme.replace(this._replaceRegex, (_, m) => { return args[argsTextList.indexOf(m)]; From 87c719d2fe31a7cb0526a87f27a9c5f3ca5b452a Mon Sep 17 00:00:00 2001 From: Sway007 Date: Sat, 12 Oct 2024 15:39:06 +0800 Subject: [PATCH 092/131] feat: code opt --- packages/shader-lab/src/preprocessor/MacroDefine.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/shader-lab/src/preprocessor/MacroDefine.ts b/packages/shader-lab/src/preprocessor/MacroDefine.ts index df341db8ef..408d0b38a8 100644 --- a/packages/shader-lab/src/preprocessor/MacroDefine.ts +++ b/packages/shader-lab/src/preprocessor/MacroDefine.ts @@ -1,6 +1,8 @@ import { BaseToken } from "../common/BaseToken"; import { ShaderRange } from "../common"; -import { GSError, GSErrorName } from "../GSError"; +// #if _VERBOSE +import { GSErrorName } from "../GSError"; +// #endif import { ShaderLabUtils } from "../ShaderLabUtils"; export class MacroDefine { @@ -13,6 +15,7 @@ export class MacroDefine { } private _replaceRegex?: RegExp; + private readonly _argsTextList: string[]; constructor( public readonly macro: BaseToken, @@ -21,20 +24,18 @@ export class MacroDefine { public readonly args?: BaseToken[] ) { if (args) { - const argsTextList = this.args.map((item) => item.lexeme); - this._replaceRegex = new RegExp(`\\b(${argsTextList.join("|")})\\b`, "g"); + this._argsTextList = this.args.map((item) => item.lexeme); + this._replaceRegex = new RegExp(`\\b(${this._argsTextList.join("|")})\\b`, "g"); } } expandFunctionBody(args: string[]): string { - const argsTextList = this.args!.map((item) => item.lexeme); - if (args.length !== this.args?.length) { throw ShaderLabUtils.createGSError("mismatched function macro", GSErrorName.PreprocessorError, "", this.location); } const expanded = this.body.lexeme.replace(this._replaceRegex, (_, m) => { - return args[argsTextList.indexOf(m)]; + return args[this._argsTextList.indexOf(m)]; }); return expanded; From 2661cae7b1661638e39d29e557f0fd903bcdf98d Mon Sep 17 00:00:00 2001 From: Sway007 Date: Sat, 12 Oct 2024 15:43:36 +0800 Subject: [PATCH 093/131] feat: code opt --- packages/shader-lab/src/preprocessor/MacroDefine.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/shader-lab/src/preprocessor/MacroDefine.ts b/packages/shader-lab/src/preprocessor/MacroDefine.ts index 408d0b38a8..4e4a3fb338 100644 --- a/packages/shader-lab/src/preprocessor/MacroDefine.ts +++ b/packages/shader-lab/src/preprocessor/MacroDefine.ts @@ -34,7 +34,8 @@ export class MacroDefine { throw ShaderLabUtils.createGSError("mismatched function macro", GSErrorName.PreprocessorError, "", this.location); } - const expanded = this.body.lexeme.replace(this._replaceRegex, (_, m) => { + const expanded = this.body.lexeme.replace(this._replaceRegex, (m) => { + debugger; return args[this._argsTextList.indexOf(m)]; }); From a85ee453ca77780aa010c4e24dd63bb2ebbd48d4 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Sat, 12 Oct 2024 15:46:43 +0800 Subject: [PATCH 094/131] feat: code opt --- packages/shader-lab/src/preprocessor/MacroDefine.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/shader-lab/src/preprocessor/MacroDefine.ts b/packages/shader-lab/src/preprocessor/MacroDefine.ts index 4e4a3fb338..3b9cee7be2 100644 --- a/packages/shader-lab/src/preprocessor/MacroDefine.ts +++ b/packages/shader-lab/src/preprocessor/MacroDefine.ts @@ -35,7 +35,6 @@ export class MacroDefine { } const expanded = this.body.lexeme.replace(this._replaceRegex, (m) => { - debugger; return args[this._argsTextList.indexOf(m)]; }); From 4c5020668257da0d1a19ffaaac301a0216494bd8 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Sat, 12 Oct 2024 15:48:52 +0800 Subject: [PATCH 095/131] feat: code opt --- packages/shader-lab/src/preprocessor/MacroDefine.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/shader-lab/src/preprocessor/MacroDefine.ts b/packages/shader-lab/src/preprocessor/MacroDefine.ts index 3b9cee7be2..51968a0a95 100644 --- a/packages/shader-lab/src/preprocessor/MacroDefine.ts +++ b/packages/shader-lab/src/preprocessor/MacroDefine.ts @@ -10,10 +10,6 @@ export class MacroDefine { return !!this.args?.length; } - get macroLexeme() { - return this.macro.lexeme; - } - private _replaceRegex?: RegExp; private readonly _argsTextList: string[]; From cae81c200e64e8842072beba52559fde5f0e0941 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Sat, 12 Oct 2024 15:52:25 +0800 Subject: [PATCH 096/131] feat: code opt --- packages/shader-lab/src/preprocessor/MacroDefine.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/shader-lab/src/preprocessor/MacroDefine.ts b/packages/shader-lab/src/preprocessor/MacroDefine.ts index 51968a0a95..07a545a236 100644 --- a/packages/shader-lab/src/preprocessor/MacroDefine.ts +++ b/packages/shader-lab/src/preprocessor/MacroDefine.ts @@ -6,10 +6,6 @@ import { GSErrorName } from "../GSError"; import { ShaderLabUtils } from "../ShaderLabUtils"; export class MacroDefine { - get isFunction() { - return !!this.args?.length; - } - private _replaceRegex?: RegExp; private readonly _argsTextList: string[]; @@ -25,6 +21,10 @@ export class MacroDefine { } } + get isFunction(): boolean { + return !!this.args?.length; + } + expandFunctionBody(args: string[]): string { if (args.length !== this.args?.length) { throw ShaderLabUtils.createGSError("mismatched function macro", GSErrorName.PreprocessorError, "", this.location); From eb515be456f7c90f38e277aa64c1386e7b343298 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Sat, 12 Oct 2024 16:02:33 +0800 Subject: [PATCH 097/131] feat: code opt --- .../src/preprocessor/MacroDefine.ts | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/packages/shader-lab/src/preprocessor/MacroDefine.ts b/packages/shader-lab/src/preprocessor/MacroDefine.ts index 07a545a236..56db10d50d 100644 --- a/packages/shader-lab/src/preprocessor/MacroDefine.ts +++ b/packages/shader-lab/src/preprocessor/MacroDefine.ts @@ -1,13 +1,17 @@ import { BaseToken } from "../common/BaseToken"; import { ShaderRange } from "../common"; +import { ShaderLabUtils } from "../ShaderLabUtils"; // #if _VERBOSE import { GSErrorName } from "../GSError"; // #endif -import { ShaderLabUtils } from "../ShaderLabUtils"; export class MacroDefine { private _replaceRegex?: RegExp; - private readonly _argsTextList: string[]; + private readonly _argsLexemes: string[]; + + get isFunction(): boolean { + return !!this.args?.length; + } constructor( public readonly macro: BaseToken, @@ -16,24 +20,18 @@ export class MacroDefine { public readonly args?: BaseToken[] ) { if (args) { - this._argsTextList = this.args.map((item) => item.lexeme); - this._replaceRegex = new RegExp(`\\b(${this._argsTextList.join("|")})\\b`, "g"); + this._argsLexemes = this.args.map((item) => item.lexeme); + this._replaceRegex = new RegExp(`\\b(${this._argsLexemes.join("|")})\\b`, "g"); } } - get isFunction(): boolean { - return !!this.args?.length; - } - expandFunctionBody(args: string[]): string { if (args.length !== this.args?.length) { throw ShaderLabUtils.createGSError("mismatched function macro", GSErrorName.PreprocessorError, "", this.location); } - const expanded = this.body.lexeme.replace(this._replaceRegex, (m) => { - return args[this._argsTextList.indexOf(m)]; + return this.body.lexeme.replace(this._replaceRegex, (m) => { + return args[this._argsLexemes.indexOf(m)]; }); - - return expanded; } } From fb3974eb296e9f66a57ee0cecade980dd5b2c323 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Sat, 12 Oct 2024 17:18:56 +0800 Subject: [PATCH 098/131] feat: code opt --- packages/shader-lab/src/GSError.ts | 2 +- packages/shader-lab/src/ShaderLabUtils.ts | 7 +++++-- packages/shader-lab/src/codeGen/CodeGenVisitor.ts | 5 +++-- packages/shader-lab/src/codeGen/VisitorContext.ts | 7 ++++--- packages/shader-lab/src/common/BaseScanner.ts | 5 +---- .../shader-lab/src/contentParser/ShaderContentParser.ts | 3 ++- packages/shader-lab/src/parser/SemanticAnalyzer.ts | 7 ++++--- packages/shader-lab/src/parser/ShaderTargetParser.ts | 4 +--- packages/shader-lab/src/preprocessor/PpParser.ts | 8 ++++---- 9 files changed, 25 insertions(+), 23 deletions(-) diff --git a/packages/shader-lab/src/GSError.ts b/packages/shader-lab/src/GSError.ts index 27032ba4cd..0724ae105a 100644 --- a/packages/shader-lab/src/GSError.ts +++ b/packages/shader-lab/src/GSError.ts @@ -57,9 +57,9 @@ export class GSError extends Error { } } +// #endif export enum GSErrorName { PreprocessorError = "PreprocessorError", CompilationError = "CompilationError", ScannerError = "ScannerError" } -// #endif diff --git a/packages/shader-lab/src/ShaderLabUtils.ts b/packages/shader-lab/src/ShaderLabUtils.ts index aaf0feed82..b2847b67e8 100644 --- a/packages/shader-lab/src/ShaderLabUtils.ts +++ b/packages/shader-lab/src/ShaderLabUtils.ts @@ -1,7 +1,10 @@ import { ClearableObjectPool, IPoolElement } from "@galacean/engine"; -import { GSError, GSErrorName } from "./GSError"; +import { GSErrorName } from "./GSError"; import { ShaderRange } from "./common/ShaderRange"; import { ShaderPosition } from "./common/ShaderPosition"; +// #if _VERBOSE +import { GSError } from "./GSError"; +// #endif export class ShaderLabUtils { private static _shaderLabObjectPoolSet: ClearableObjectPool[] = []; @@ -28,7 +31,7 @@ export class ShaderLabUtils { // #if _VERBOSE return new GSError(errorName, message, location, source, file); // #else - throw new Error(message); + throw new Error(`[${errorName}]: ${message}`); // #endif } } diff --git a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts index 288393045d..a42b3abe33 100644 --- a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts +++ b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts @@ -6,10 +6,11 @@ import { ESymbolType, FnSymbol, VarSymbol } from "../parser/symbolTable"; import { ParserUtils } from "../ParserUtils"; import { NodeChild } from "../parser/types"; import { VisitorContext } from "./VisitorContext"; +import { ShaderLab } from "../ShaderLab"; +import { GSErrorName } from "../GSError"; // #if _VERBOSE -import { GSErrorName, GSError } from "../GSError"; +import { GSError } from "../GSError"; // #endif -import { ShaderLab } from "../ShaderLab"; /** * @internal diff --git a/packages/shader-lab/src/codeGen/VisitorContext.ts b/packages/shader-lab/src/codeGen/VisitorContext.ts index dcb37dc753..3d6308f78e 100644 --- a/packages/shader-lab/src/codeGen/VisitorContext.ts +++ b/packages/shader-lab/src/codeGen/VisitorContext.ts @@ -2,12 +2,13 @@ import { EShaderStage } from "../common/Enums"; import { ASTNode } from "../parser/AST"; import { ESymbolType, SymbolTable, SymbolInfo } from "../parser/symbolTable"; import { IParamInfo } from "../parser/types"; -// #if _VERBOSE -import { GSErrorName, GSError } from "../GSError"; -// #endif +import { GSErrorName } from "../GSError"; import { BaseToken } from "../common/BaseToken"; import { ShaderLab } from "../ShaderLab"; import { ShaderLabUtils } from "../ShaderLabUtils"; +// #if _VERBOSE +import { GSError } from "../GSError"; +// #endif /** @internal */ export class VisitorContext { diff --git a/packages/shader-lab/src/common/BaseScanner.ts b/packages/shader-lab/src/common/BaseScanner.ts index f70ae8b4a8..55d42157b3 100644 --- a/packages/shader-lab/src/common/BaseScanner.ts +++ b/packages/shader-lab/src/common/BaseScanner.ts @@ -1,8 +1,5 @@ -import { Logger } from "@galacean/engine"; import { ETokenType, ShaderRange, ShaderPosition } from "."; -// #if _VERBOSE -import { GSError, GSErrorName } from "../GSError"; -// #endif +import { GSErrorName } from "../GSError"; import { ShaderLab } from "../ShaderLab"; import { BaseToken } from "./BaseToken"; import { ShaderLabUtils } from "../ShaderLabUtils"; diff --git a/packages/shader-lab/src/contentParser/ShaderContentParser.ts b/packages/shader-lab/src/contentParser/ShaderContentParser.ts index d2cc87be39..72bd7bcd2b 100644 --- a/packages/shader-lab/src/contentParser/ShaderContentParser.ts +++ b/packages/shader-lab/src/contentParser/ShaderContentParser.ts @@ -23,8 +23,9 @@ import { IShaderPassContent, IRenderStates } from "@galacean/engine-design"; +import { GSErrorName } from "../GSError"; // #if _VERBOSE -import { GSError, GSErrorName } from "../GSError"; +import { GSError } from "../GSError"; import { ShaderLabUtils } from "../ShaderLabUtils"; // #endif diff --git a/packages/shader-lab/src/parser/SemanticAnalyzer.ts b/packages/shader-lab/src/parser/SemanticAnalyzer.ts index 04c4e0c068..b078287d97 100644 --- a/packages/shader-lab/src/parser/SemanticAnalyzer.ts +++ b/packages/shader-lab/src/parser/SemanticAnalyzer.ts @@ -1,13 +1,14 @@ import { ShaderRange } from "../common"; import { TreeNode } from "./AST"; -// #if _VERBOSE -import { GSError, GSErrorName } from "../GSError"; -// #endif +import { GSErrorName } from "../GSError"; import { ShaderData } from "./ShaderInfo"; import { SymbolInfo, SymbolTable } from "../parser/symbolTable"; import { NodeChild } from "./types"; import { SymbolTableStack } from "../common/BaseSymbolTable"; import { ShaderLab } from "../ShaderLab"; +// #if _VERBOSE +import { GSError } from "../GSError"; +// #endif export type TranslationRule = (sa: SematicAnalyzer, ...tokens: NodeChild[]) => T; diff --git a/packages/shader-lab/src/parser/ShaderTargetParser.ts b/packages/shader-lab/src/parser/ShaderTargetParser.ts index 574e9ca511..e0b70f8b66 100644 --- a/packages/shader-lab/src/parser/ShaderTargetParser.ts +++ b/packages/shader-lab/src/parser/ShaderTargetParser.ts @@ -10,9 +10,7 @@ import { addTranslationRule, createGrammar } from "../lalr/CFG"; import { LALR1 } from "../lalr"; import { ParserUtils } from "../ParserUtils"; import { Logger } from "@galacean/engine"; -// #if _VERBOSE -import { GSError, GSErrorName } from "../GSError"; -// #endif +import { GSErrorName } from "../GSError"; import { ShaderLab } from "../ShaderLab"; import { ShaderLabUtils } from "../ShaderLabUtils"; diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 32560cb95c..0667b12501 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -1,10 +1,6 @@ import { ShaderPosition, ShaderRange } from "../common"; import LexerUtils from "../lexer/Utils"; import { MacroDefine } from "./MacroDefine"; -// #if _VERBOSE -import PpSourceMap, { BlockInfo } from "./sourceMap"; -import { GSError, GSErrorName } from "../GSError"; -// #endif import { BaseToken } from "../common/BaseToken"; import { EPpKeyword, EPpToken, PpConstant } from "./constants"; import PpScanner from "./PpScanner"; @@ -12,6 +8,10 @@ import { PpUtils } from "./Utils"; import { ShaderLab } from "../ShaderLab"; import { ShaderPass } from "@galacean/engine"; import { ShaderLabUtils } from "../ShaderLabUtils"; +import { GSErrorName } from "../GSError"; +// #if _VERBOSE +import PpSourceMap, { BlockInfo } from "./sourceMap"; +// #endif export interface ExpandSegment { // #if _VERBOSE From f0742cfabcad4c884804a7f580de398e9d1eac66 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Sat, 12 Oct 2024 18:08:46 +0800 Subject: [PATCH 099/131] feat: code opt --- packages/shader-lab/src/common/BaseScanner.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/shader-lab/src/common/BaseScanner.ts b/packages/shader-lab/src/common/BaseScanner.ts index 55d42157b3..44c86d9f4b 100644 --- a/packages/shader-lab/src/common/BaseScanner.ts +++ b/packages/shader-lab/src/common/BaseScanner.ts @@ -48,6 +48,7 @@ export default class BaseScanner { ); } + // #if _VERBOSE get line() { return this._line; } @@ -55,6 +56,7 @@ export default class BaseScanner { get column() { return this._column; } + // #endif protected readonly _keywordsMap: Map; From ef6496bcec07f74552d4de1805927c640c62539d Mon Sep 17 00:00:00 2001 From: Sway007 Date: Mon, 14 Oct 2024 08:47:56 +0800 Subject: [PATCH 100/131] feat: code opt --- packages/shader-lab/src/index.ts | 4 --- tests/src/shader-lab/Scanner.test.ts | 42 ------------------------ tests/src/shader-lab/shaders/demo.shader | 8 +++++ 3 files changed, 8 insertions(+), 46 deletions(-) delete mode 100644 tests/src/shader-lab/Scanner.test.ts diff --git a/packages/shader-lab/src/index.ts b/packages/shader-lab/src/index.ts index 3c8e899c1d..637143b64d 100644 --- a/packages/shader-lab/src/index.ts +++ b/packages/shader-lab/src/index.ts @@ -1,9 +1,5 @@ export { ShaderLab } from "./ShaderLab"; -import BaseScanner from "./common/BaseScanner"; -/** @internal */ -export { BaseScanner }; - // #if _VERBOSE export { Preprocessor } from "./preprocessor"; export * from "./GSError"; diff --git a/tests/src/shader-lab/Scanner.test.ts b/tests/src/shader-lab/Scanner.test.ts deleted file mode 100644 index 148add8d73..0000000000 --- a/tests/src/shader-lab/Scanner.test.ts +++ /dev/null @@ -1,42 +0,0 @@ -// @ts-ignore -import { BaseScanner } from "@galacean/engine-shader-lab"; -import { expect } from "chai"; - -describe("BaseScanner", () => { - describe("skipCommentsAndSpace()", () => { - it("should skip over multi-line comments and proceed to the next token", () => { - const source = `/* This is a - multi-line comment */ - nextToken`; - const scanner = new BaseScanner(source); - - scanner.skipCommentsAndSpace(); - - const token = scanner.scanToken(); - expect(token?.lexeme).to.equal("nextToken"); - }); - - it("should skip over multiple multi-line comments and spaces", () => { - const source = `/* First comment */ - /* Second comment */ - - thirdToken`; - const scanner = new BaseScanner(source); - - scanner.skipCommentsAndSpace(); - - const token = scanner.scanToken(); - expect(token?.lexeme).to.equal("thirdToken"); - }); - - it("should handle multi-line comments without leading spaces", () => { - const source = `/*Comment without leading space*/nextToken`; - const scanner = new BaseScanner(source); - - scanner.skipCommentsAndSpace(); - - const token = scanner.scanToken(); - expect(token?.lexeme).to.equal("nextToken"); - }); - }); -}); diff --git a/tests/src/shader-lab/shaders/demo.shader b/tests/src/shader-lab/shaders/demo.shader index 3f1d37a88d..c6ca75e4f2 100644 --- a/tests/src/shader-lab/shaders/demo.shader +++ b/tests/src/shader-lab/shaders/demo.shader @@ -75,8 +75,13 @@ Shader "Water" { RenderQueueType = Opaque; + /* First comment */ + /* Second comment */ + #define SCENE_SHADOW_TYPE 3 + /*Comment without leading space*/ + v2f vert(a2v v) { v2f o; @@ -88,6 +93,9 @@ Shader "Water" { return o; } + /* This is a + multi-line comment */ + void frag(v2f i) { vec4 color = texture2D(material_BaseTexture, i.v_uv) * u_color; float fogDistance = length(i.v_position); From 8086b867a18d98c991bd735ff9decf5f39a40cd6 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 17 Oct 2024 14:53:09 +0800 Subject: [PATCH 101/131] feat(shaderlab): ignore editor module --- packages/shader-lab/src/common/Keywords.ts | 1 + packages/shader-lab/src/contentParser/KeywordMap.ts | 1 + packages/shader-lab/src/contentParser/ShaderContentParser.ts | 1 + packages/shader-lab/src/preprocessor/PpParser.ts | 2 +- 4 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/shader-lab/src/common/Keywords.ts b/packages/shader-lab/src/common/Keywords.ts index 9085d0d6f5..0ab7b47fa5 100644 --- a/packages/shader-lab/src/common/Keywords.ts +++ b/packages/shader-lab/src/common/Keywords.ts @@ -77,6 +77,7 @@ export enum EKeyword { GS_RasterState, GS_EditorProperties, GS_EditorMacros, + GS_Editor, GS_Tags, GS_ReplacementTag, GS_LightMode, diff --git a/packages/shader-lab/src/contentParser/KeywordMap.ts b/packages/shader-lab/src/contentParser/KeywordMap.ts index f6b20188da..bb43d824fa 100644 --- a/packages/shader-lab/src/contentParser/KeywordMap.ts +++ b/packages/shader-lab/src/contentParser/KeywordMap.ts @@ -8,6 +8,7 @@ export const KeywordMap = new Map([ ["RasterState", EKeyword.GS_RasterState], ["EditorProperties", EKeyword.GS_EditorProperties], ["EditorMacros", EKeyword.GS_EditorMacros], + ["Editor", EKeyword.GS_Editor], ["Tags", EKeyword.GS_Tags], ["VertexShader", EKeyword.GS_VertexShader], ["FragmentShader", EKeyword.GS_FragmentShader], diff --git a/packages/shader-lab/src/contentParser/ShaderContentParser.ts b/packages/shader-lab/src/contentParser/ShaderContentParser.ts index 72bd7bcd2b..a3345f1b88 100644 --- a/packages/shader-lab/src/contentParser/ShaderContentParser.ts +++ b/packages/shader-lab/src/contentParser/ShaderContentParser.ts @@ -129,6 +129,7 @@ export class ShaderContentParser { case EKeyword.GS_EditorProperties: case EKeyword.GS_EditorMacros: + case EKeyword.GS_Editor: this._addGlobalStatement(ret, scanner, start, word.lexeme.length); scanner.scanPairedText("{", "}", true); start = scanner.getCurPosition(); diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 0667b12501..242094186b 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -651,7 +651,7 @@ export class PpParser { } private static _skipEditorBlock(token: BaseToken, scanner: PpScanner) { - if (token.lexeme === "EditorProperties" || token.lexeme === "EditorMacros") { + if (token.lexeme === "EditorProperties" || token.lexeme === "EditorMacros" || token.lexeme === "Editor") { const start = scanner.current - token.lexeme.length; scanner.scanPairedBlock("{", "}"); const end = scanner.current; From c80f1414f6c3f87bbb176860b3a387b0039334f3 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 31 Oct 2024 14:55:20 +0800 Subject: [PATCH 102/131] feat: code opt --- packages/shader-lab/src/preprocessor/PpParser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 104babf3a0..b0e348e06c 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -647,7 +647,7 @@ export class PpParser { } private static _skipEditorBlock(token: BaseToken, scanner: PpScanner) { - if (token.lexeme === "EditorProperties" || token.lexeme === "EditorMacros" || token.lexeme === "Editor") { + if (["EditorProperties", "EditorMacros", "Editor"].indexOf(token.lexeme)) { const start = scanner.current - token.lexeme.length; scanner.scanPairedBlock("{", "}"); const end = scanner.current; From 0d5769379b873ca00d8f2754f51e3b24dadc70a1 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 31 Oct 2024 15:00:57 +0800 Subject: [PATCH 103/131] feat: code opt --- packages/shader-lab/src/preprocessor/PpParser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index b0e348e06c..f63f3be1c3 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -647,7 +647,7 @@ export class PpParser { } private static _skipEditorBlock(token: BaseToken, scanner: PpScanner) { - if (["EditorProperties", "EditorMacros", "Editor"].indexOf(token.lexeme)) { + if (["EditorProperties", "EditorMacros", "Editor"].indexOf(token.lexeme) !== -1) { const start = scanner.current - token.lexeme.length; scanner.scanPairedBlock("{", "}"); const end = scanner.current; From b173007b2783a4cde945eab22652792fcb096d76 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 31 Oct 2024 16:12:47 +0800 Subject: [PATCH 104/131] feat: hide ShaderChunk type --- packages/core/src/asset/AssetType.ts | 2 -- packages/loader/src/ShaderChunkLoader.ts | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/core/src/asset/AssetType.ts b/packages/core/src/asset/AssetType.ts index 008af6ea37..c5bc1f6547 100644 --- a/packages/core/src/asset/AssetType.ts +++ b/packages/core/src/asset/AssetType.ts @@ -25,8 +25,6 @@ export enum AssetType { Material = "Material", /** Shader */ Shader = "Shader", - /** Shader Chunk */ - ShaderChunk = "ShaderChunk", /** Mesh. */ Mesh = "Mesh", /** AnimationClip. */ diff --git a/packages/loader/src/ShaderChunkLoader.ts b/packages/loader/src/ShaderChunkLoader.ts index 79cd824ffc..dc93c94b81 100644 --- a/packages/loader/src/ShaderChunkLoader.ts +++ b/packages/loader/src/ShaderChunkLoader.ts @@ -9,7 +9,7 @@ import { resourceLoader } from "@galacean/engine-core"; -@resourceLoader(AssetType.ShaderChunk, ["glsl"], false) +@resourceLoader("ShaderChunk", ["glsl"], false) class ShaderChunkLoader extends Loader { load(item: LoadItem, resourceManager: ResourceManager): AssetPromise { return this.request(item.url, { ...item, type: "text" }).then(async (code) => { @@ -24,7 +24,7 @@ class ShaderChunkLoader extends Loader { // @ts-ignore const resource = resourceManager._virtualPathMap[path]; if (!resource) return; - return resourceManager.load({ type: AssetType.ShaderChunk, url: resource, params: { includeKey: path } }); + return resourceManager.load({ type: "ShaderChunk", url: resource, params: { includeKey: path } }); } }) ); From 0addd19e933e93eff35a7295ed939eba73601b0d Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 31 Oct 2024 16:53:33 +0800 Subject: [PATCH 105/131] feat: add types --- packages/loader/src/ShaderChunkLoader.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/loader/src/ShaderChunkLoader.ts b/packages/loader/src/ShaderChunkLoader.ts index dc93c94b81..c150267572 100644 --- a/packages/loader/src/ShaderChunkLoader.ts +++ b/packages/loader/src/ShaderChunkLoader.ts @@ -12,7 +12,7 @@ import { @resourceLoader("ShaderChunk", ["glsl"], false) class ShaderChunkLoader extends Loader { load(item: LoadItem, resourceManager: ResourceManager): AssetPromise { - return this.request(item.url, { ...item, type: "text" }).then(async (code) => { + return this.request(item.url, { ...item, type: "text" }).then(async (code: string) => { const { includeKey } = item.params; ShaderFactory.registerInclude(includeKey, code); From f9434bbd6133d38be28f47d55e1619995da38eac Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 31 Oct 2024 17:12:54 +0800 Subject: [PATCH 106/131] feat: code opt --- packages/core/src/asset/AssetType.ts | 2 +- packages/loader/src/ShaderChunkLoader.ts | 19 ++++++------------- packages/loader/src/ShaderLoader.ts | 8 +++++--- .../resources/schema/BasicSchema.ts | 2 +- 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/packages/core/src/asset/AssetType.ts b/packages/core/src/asset/AssetType.ts index 96756d6483..4f8ddab140 100644 --- a/packages/core/src/asset/AssetType.ts +++ b/packages/core/src/asset/AssetType.ts @@ -23,7 +23,7 @@ export enum AssetType { TextureCube = "TextureCube", /** Material. */ Material = "Material", - /** Shader */ + /** Shader. */ Shader = "Shader", /** Mesh. */ Mesh = "Mesh", diff --git a/packages/loader/src/ShaderChunkLoader.ts b/packages/loader/src/ShaderChunkLoader.ts index c150267572..14320e871d 100644 --- a/packages/loader/src/ShaderChunkLoader.ts +++ b/packages/loader/src/ShaderChunkLoader.ts @@ -1,22 +1,15 @@ -import { - AssetPromise, - AssetType, - LoadItem, - Loader, - ResourceManager, - Shader, - ShaderFactory, - resourceLoader -} from "@galacean/engine-core"; +import { AssetPromise, LoadItem, Loader, ResourceManager, ShaderFactory, resourceLoader } from "@galacean/engine-core"; -@resourceLoader("ShaderChunk", ["glsl"], false) +@resourceLoader("ShaderChunk", ["glsl"]) class ShaderChunkLoader extends Loader { + private static _includeRegex = /^[ \t]*#include +"([^$\\"]+)"/gm; + load(item: LoadItem, resourceManager: ResourceManager): AssetPromise { - return this.request(item.url, { ...item, type: "text" }).then(async (code: string) => { + return this.request(item.url, { ...item, type: "text" }).then(async (code: string) => { const { includeKey } = item.params; ShaderFactory.registerInclude(includeKey, code); - const matches = code.matchAll(/^[ \t]*#include +"([^$\\"]+)"/gm); + const matches = code.matchAll(ShaderChunkLoader._includeRegex); await Promise.all( Array.from(matches).map((m) => { const path = m[1]; diff --git a/packages/loader/src/ShaderLoader.ts b/packages/loader/src/ShaderLoader.ts index 148788431e..18cd7fb7c2 100644 --- a/packages/loader/src/ShaderLoader.ts +++ b/packages/loader/src/ShaderLoader.ts @@ -8,10 +8,12 @@ import { resourceLoader } from "@galacean/engine-core"; -@resourceLoader(AssetType.Shader, ["gs", "gsl"], false) +@resourceLoader(AssetType.Shader, ["gs", "gsl"]) class ShaderLoader extends Loader { + private static _builtinRegex = /^\s*\/\/\s*@builtin\s+(\w+)/; + load(item: LoadItem, resourceManager: ResourceManager): AssetPromise { - return this.request(item.url, { ...item, type: "text" }).then((code: string) => { + return this.request(item.url, { ...item, type: "text" }).then((code: string) => { const builtinShader = this.getBuiltinShader(code); if (builtinShader) { return Shader.find(builtinShader); @@ -35,7 +37,7 @@ class ShaderLoader extends Loader { } private getBuiltinShader(code: string) { - const match = code.match(/^\s*\/\/\s*@builtin\s+(\w+)/); + const match = code.match(ShaderLoader._builtinRegex); if (match && match[1]) return match[1]; } } diff --git a/packages/loader/src/resource-deserialize/resources/schema/BasicSchema.ts b/packages/loader/src/resource-deserialize/resources/schema/BasicSchema.ts index 033c0e21ae..ef49397f30 100644 --- a/packages/loader/src/resource-deserialize/resources/schema/BasicSchema.ts +++ b/packages/loader/src/resource-deserialize/resources/schema/BasicSchema.ts @@ -93,4 +93,4 @@ export type IAssetRef = { key?: string; refId: string }; export type IEntityRef = { entityId: string }; -export type IShaderRef = { refId: string }; +export type IShaderRef = Omit; From d12640e6f1df482bd085a9c540b8353243f8b0c5 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 31 Oct 2024 17:19:52 +0800 Subject: [PATCH 107/131] feat: code opt --- packages/loader/src/ShaderChunkLoader.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/loader/src/ShaderChunkLoader.ts b/packages/loader/src/ShaderChunkLoader.ts index 14320e871d..f4b5289f3a 100644 --- a/packages/loader/src/ShaderChunkLoader.ts +++ b/packages/loader/src/ShaderChunkLoader.ts @@ -1,23 +1,23 @@ import { AssetPromise, LoadItem, Loader, ResourceManager, ShaderFactory, resourceLoader } from "@galacean/engine-core"; @resourceLoader("ShaderChunk", ["glsl"]) -class ShaderChunkLoader extends Loader { +class ShaderChunkLoader extends Loader { private static _includeRegex = /^[ \t]*#include +"([^$\\"]+)"/gm; - load(item: LoadItem, resourceManager: ResourceManager): AssetPromise { - return this.request(item.url, { ...item, type: "text" }).then(async (code: string) => { + load(item: LoadItem, resourceManager: ResourceManager): AssetPromise { + return this.request(item.url, { ...item, type: "text" }).then((code: string) => { const { includeKey } = item.params; ShaderFactory.registerInclude(includeKey, code); const matches = code.matchAll(ShaderChunkLoader._includeRegex); - await Promise.all( - Array.from(matches).map((m) => { + return Promise.all( + matches.map((m) => { const path = m[1]; if (path) { // @ts-ignore const resource = resourceManager._virtualPathMap[path]; if (!resource) return; - return resourceManager.load({ type: "ShaderChunk", url: resource, params: { includeKey: path } }); + return resourceManager.load({ type: "ShaderChunk", url: resource, params: { includeKey: path } }); } }) ); From 4b64817c6280e3f3fd5f52d70b39393d5e3d1539 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 31 Oct 2024 17:27:38 +0800 Subject: [PATCH 108/131] feat: opt regex --- packages/loader/src/ShaderChunkLoader.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/loader/src/ShaderChunkLoader.ts b/packages/loader/src/ShaderChunkLoader.ts index f4b5289f3a..b9da9c5216 100644 --- a/packages/loader/src/ShaderChunkLoader.ts +++ b/packages/loader/src/ShaderChunkLoader.ts @@ -2,7 +2,7 @@ import { AssetPromise, LoadItem, Loader, ResourceManager, ShaderFactory, resourc @resourceLoader("ShaderChunk", ["glsl"]) class ShaderChunkLoader extends Loader { - private static _includeRegex = /^[ \t]*#include +"([^$\\"]+)"/gm; + private static _includeRegex = /^[ \t]*#include\s+"([^\\"]+)"/gm; load(item: LoadItem, resourceManager: ResourceManager): AssetPromise { return this.request(item.url, { ...item, type: "text" }).then((code: string) => { From d351baee4522d746cdd680b89ead8131f5caada0 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 31 Oct 2024 19:52:52 +0800 Subject: [PATCH 109/131] feat: support relative include in shader loader --- packages/core/src/asset/LoadItem.ts | 4 ++ packages/core/src/asset/ResourceManager.ts | 3 +- packages/loader/src/PathUtils.ts | 13 ++++++ packages/loader/src/ShaderChunkLoader.ts | 46 +++++++++++++++------- packages/loader/src/ShaderLoader.ts | 39 +++++++++++++----- 5 files changed, 80 insertions(+), 25 deletions(-) create mode 100644 packages/loader/src/PathUtils.ts diff --git a/packages/core/src/asset/LoadItem.ts b/packages/core/src/asset/LoadItem.ts index 7897f2bd61..af8385fcb7 100644 --- a/packages/core/src/asset/LoadItem.ts +++ b/packages/core/src/asset/LoadItem.ts @@ -26,6 +26,10 @@ export type LoadItem = { * Additional parameters for specified loader. */ params?: Record; + /** + * Asset uuid + */ + uuid?: string; } & PickOnlyOne<{ /** * Loading url. diff --git a/packages/core/src/asset/ResourceManager.ts b/packages/core/src/asset/ResourceManager.ts index c3bedf1177..8b0cb5bf49 100644 --- a/packages/core/src/asset/ResourceManager.ts +++ b/packages/core/src/asset/ResourceManager.ts @@ -545,7 +545,8 @@ export class ResourceManager { url = key ? `${url}${url.indexOf("?") > -1 ? "&" : "?"}q=${key}` : url; promise = this.load({ url, - type: this._editorResourceConfig[refId].type + type: this._editorResourceConfig[refId].type, + uuid: refId }); } return promise.then((item) => (isClone ? item.clone() : item)); diff --git a/packages/loader/src/PathUtils.ts b/packages/loader/src/PathUtils.ts new file mode 100644 index 0000000000..2cc9fa8d0d --- /dev/null +++ b/packages/loader/src/PathUtils.ts @@ -0,0 +1,13 @@ +/** @internal */ +export class PathUtils { + private static _urlSchema = "files://"; + static shaderIncludeRegex = /\s#include\s+"([^\\"]+)"/gm; + + static pathResolve(path: string, base: string): string { + return new URL(path, PathUtils._urlSchema + base).href.substring(PathUtils._urlSchema.length); + } + + static isRelativePath(path: string): boolean { + return path[0] === "."; + } +} diff --git a/packages/loader/src/ShaderChunkLoader.ts b/packages/loader/src/ShaderChunkLoader.ts index b9da9c5216..bf5092c4ae 100644 --- a/packages/loader/src/ShaderChunkLoader.ts +++ b/packages/loader/src/ShaderChunkLoader.ts @@ -1,24 +1,42 @@ -import { AssetPromise, LoadItem, Loader, ResourceManager, ShaderFactory, resourceLoader } from "@galacean/engine-core"; +import { + AssetPromise, + LoadItem, + Loader, + ResourceManager, + ShaderFactory, + resourceLoader, + // @ts-ignore + ShaderLib +} from "@galacean/engine-core"; +import { PathUtils } from "./PathUtils"; @resourceLoader("ShaderChunk", ["glsl"]) class ShaderChunkLoader extends Loader { - private static _includeRegex = /^[ \t]*#include\s+"([^\\"]+)"/gm; - load(item: LoadItem, resourceManager: ResourceManager): AssetPromise { return this.request(item.url, { ...item, type: "text" }).then((code: string) => { - const { includeKey } = item.params; - ShaderFactory.registerInclude(includeKey, code); + const { includeKey, shaderPath } = item.params; + ShaderFactory.registerInclude((includeKey).substring(1), code); + + const matches = code.matchAll(PathUtils.shaderIncludeRegex); + const shaderChunkPaths: string[] = []; + for (const match of matches) { + const matchedPath = match[1]; + const path = PathUtils.isRelativePath(matchedPath) ? PathUtils.pathResolve(match[1], shaderPath) : matchedPath; + if (!ShaderLib[path]) { + shaderChunkPaths.push(path); + } + } - const matches = code.matchAll(ShaderChunkLoader._includeRegex); return Promise.all( - matches.map((m) => { - const path = m[1]; - if (path) { - // @ts-ignore - const resource = resourceManager._virtualPathMap[path]; - if (!resource) return; - return resourceManager.load({ type: "ShaderChunk", url: resource, params: { includeKey: path } }); - } + shaderChunkPaths.map((path) => { + // @ts-ignore + const resource = resourceManager._virtualPathMap[path]; + if (!resource) return; + return resourceManager.load({ + type: "ShaderChunk", + url: resource, + params: { includeKey: path, shaderPath } + }); }) ); }); diff --git a/packages/loader/src/ShaderLoader.ts b/packages/loader/src/ShaderLoader.ts index 18cd7fb7c2..11fb057dc8 100644 --- a/packages/loader/src/ShaderLoader.ts +++ b/packages/loader/src/ShaderLoader.ts @@ -5,8 +5,12 @@ import { Loader, ResourceManager, Shader, - resourceLoader + resourceLoader, + // @ts-ignore + ShaderLib, + Logger } from "@galacean/engine-core"; +import { PathUtils } from "./PathUtils"; @resourceLoader(AssetType.Shader, ["gs", "gsl"]) class ShaderLoader extends Loader { @@ -19,16 +23,31 @@ class ShaderLoader extends Loader { return Shader.find(builtinShader); } - const matches = code.matchAll(/^[ \t]*#include +"([^$\\"]+)"/gm); + const { uuid } = item; + // @ts-ignore + const shaderConfig = resourceManager._editorResourceConfig[uuid]; + if (!shaderConfig) { + Logger.error("not found shader", uuid); + return; + } + const shaderPath: string = shaderConfig.virtualPath; + + const shaderChunkPaths: string[] = []; + const matches = code.matchAll(PathUtils.shaderIncludeRegex); + for (const match of matches) { + const matchedPath = match[1]; + const path = PathUtils.isRelativePath(matchedPath) ? PathUtils.pathResolve(match[1], shaderPath) : matchedPath; + if (!ShaderLib[path]) { + shaderChunkPaths.push(path); + } + } + return Promise.all( - Array.from(matches).map((m) => { - const path = m[1]; - if (path) { - // @ts-ignore - const resource = resourceManager._virtualPathMap[path]; - if (!resource) return; - return resourceManager.load({ type: "ShaderChunk", url: resource, params: { includeKey: path } }); - } + shaderChunkPaths.map((path) => { + // @ts-ignore + const resource = resourceManager._virtualPathMap[path]; + if (!resource) return; + return resourceManager.load({ type: "ShaderChunk", url: resource, params: { includeKey: path, shaderPath } }); }) ).then(() => { return Shader.create(code); From 2df9acbdb09a74ee34d2a2928255321a21292276 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Mon, 4 Nov 2024 15:58:12 +0800 Subject: [PATCH 110/131] refactor: loader api --- packages/core/src/asset/LoadItem.ts | 4 +-- packages/core/src/asset/ResourceManager.ts | 9 +++--- packages/loader/src/ShaderChunkLoader.ts | 26 ++++++++-------- packages/loader/src/ShaderLoader.ts | 36 +++++++++------------- 4 files changed, 35 insertions(+), 40 deletions(-) diff --git a/packages/core/src/asset/LoadItem.ts b/packages/core/src/asset/LoadItem.ts index af8385fcb7..34407cf296 100644 --- a/packages/core/src/asset/LoadItem.ts +++ b/packages/core/src/asset/LoadItem.ts @@ -27,9 +27,9 @@ export type LoadItem = { */ params?: Record; /** - * Asset uuid + * Asset path in editor */ - uuid?: string; + virtualPath?: string; } & PickOnlyOne<{ /** * Loading url. diff --git a/packages/core/src/asset/ResourceManager.ts b/packages/core/src/asset/ResourceManager.ts index 8b0cb5bf49..d0591e8b69 100644 --- a/packages/core/src/asset/ResourceManager.ts +++ b/packages/core/src/asset/ResourceManager.ts @@ -329,7 +329,7 @@ export class ResourceManager { // Check url mapping const itemURL = item.url; - let url = this._virtualPathMap[itemURL] ? this._virtualPathMap[itemURL] : itemURL; + let url = this._virtualPathMap[itemURL] ?? itemURL; // Not absolute and base url is set if (!Utils.isAbsoluteUrl(url) && this.baseUrl) url = Utils.resolveAbsoluteUrl(this.baseUrl, url); @@ -537,7 +537,8 @@ export class ResourceManager { if (obj) { promise = Promise.resolve(obj); } else { - let url = this._editorResourceConfig[refId]?.path; + const resourceConfig = this._editorResourceConfig[refId]; + let url = resourceConfig?.path; if (!url) { Logger.warn(`refId:${refId} is not find in this._editorResourceConfig.`); return Promise.resolve(null); @@ -545,8 +546,8 @@ export class ResourceManager { url = key ? `${url}${url.indexOf("?") > -1 ? "&" : "?"}q=${key}` : url; promise = this.load({ url, - type: this._editorResourceConfig[refId].type, - uuid: refId + virtualPath: resourceConfig.virtualPath, + type: resourceConfig.type }); } return promise.then((item) => (isClone ? item.clone() : item)); diff --git a/packages/loader/src/ShaderChunkLoader.ts b/packages/loader/src/ShaderChunkLoader.ts index bf5092c4ae..274bf1781a 100644 --- a/packages/loader/src/ShaderChunkLoader.ts +++ b/packages/loader/src/ShaderChunkLoader.ts @@ -13,29 +13,29 @@ import { PathUtils } from "./PathUtils"; @resourceLoader("ShaderChunk", ["glsl"]) class ShaderChunkLoader extends Loader { load(item: LoadItem, resourceManager: ResourceManager): AssetPromise { - return this.request(item.url, { ...item, type: "text" }).then((code: string) => { - const { includeKey, shaderPath } = item.params; - ShaderFactory.registerInclude((includeKey).substring(1), code); + const { virtualPath, url } = item; + const shaderVirtualPath = item.params?.shaderVirtualPath ?? "/"; + const chunkPath = virtualPath ?? new URL(url).pathname; + + return this.request(url, { ...item, type: "text" }).then((code: string) => { + ShaderFactory.registerInclude(chunkPath.substring(1), code); const matches = code.matchAll(PathUtils.shaderIncludeRegex); const shaderChunkPaths: string[] = []; for (const match of matches) { - const matchedPath = match[1]; - const path = PathUtils.isRelativePath(matchedPath) ? PathUtils.pathResolve(match[1], shaderPath) : matchedPath; - if (!ShaderLib[path]) { - shaderChunkPaths.push(path); + const chunkPath = PathUtils.pathResolve(match[1], shaderVirtualPath); + if (!ShaderLib[chunkPath.substring(1)]) { + shaderChunkPaths.push(chunkPath); } } return Promise.all( - shaderChunkPaths.map((path) => { - // @ts-ignore - const resource = resourceManager._virtualPathMap[path]; - if (!resource) return; + shaderChunkPaths.map((chunkPath) => { return resourceManager.load({ type: "ShaderChunk", - url: resource, - params: { includeKey: path, shaderPath } + url: chunkPath, + virtualPath: chunkPath, + params: { shaderVirtualPath } }); }) ); diff --git a/packages/loader/src/ShaderLoader.ts b/packages/loader/src/ShaderLoader.ts index 11fb057dc8..d86a4dc0f5 100644 --- a/packages/loader/src/ShaderLoader.ts +++ b/packages/loader/src/ShaderLoader.ts @@ -7,8 +7,7 @@ import { Shader, resourceLoader, // @ts-ignore - ShaderLib, - Logger + ShaderLib } from "@galacean/engine-core"; import { PathUtils } from "./PathUtils"; @@ -17,37 +16,32 @@ class ShaderLoader extends Loader { private static _builtinRegex = /^\s*\/\/\s*@builtin\s+(\w+)/; load(item: LoadItem, resourceManager: ResourceManager): AssetPromise { - return this.request(item.url, { ...item, type: "text" }).then((code: string) => { + const { virtualPath, url } = item; + const shaderVirtualPath = virtualPath ?? "/"; + + return this.request(url, { ...item, type: "text" }).then((code: string) => { const builtinShader = this.getBuiltinShader(code); if (builtinShader) { return Shader.find(builtinShader); } - const { uuid } = item; - // @ts-ignore - const shaderConfig = resourceManager._editorResourceConfig[uuid]; - if (!shaderConfig) { - Logger.error("not found shader", uuid); - return; - } - const shaderPath: string = shaderConfig.virtualPath; - const shaderChunkPaths: string[] = []; const matches = code.matchAll(PathUtils.shaderIncludeRegex); for (const match of matches) { - const matchedPath = match[1]; - const path = PathUtils.isRelativePath(matchedPath) ? PathUtils.pathResolve(match[1], shaderPath) : matchedPath; - if (!ShaderLib[path]) { - shaderChunkPaths.push(path); + const chunkPath = PathUtils.pathResolve(match[1], shaderVirtualPath); + if (!ShaderLib[chunkPath.substring(1)]) { + shaderChunkPaths.push(chunkPath); } } return Promise.all( - shaderChunkPaths.map((path) => { - // @ts-ignore - const resource = resourceManager._virtualPathMap[path]; - if (!resource) return; - return resourceManager.load({ type: "ShaderChunk", url: resource, params: { includeKey: path, shaderPath } }); + shaderChunkPaths.map((chunkPath) => { + return resourceManager.load({ + type: "ShaderChunk", + url: chunkPath, + virtualPath: chunkPath, + params: { shaderVirtualPath } + }); }) ).then(() => { return Shader.create(code); From e037baa852198cf10c2b31ea3b8d5c9e610669bb Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 5 Nov 2024 14:23:40 +0800 Subject: [PATCH 111/131] fix: return null shader source when compile error --- packages/shader-lab/src/ShaderLab.ts | 4 +++- packages/shader-lab/src/ShaderLabUtils.ts | 5 +++-- packages/shader-lab/src/parser/ShaderTargetParser.ts | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/shader-lab/src/ShaderLab.ts b/packages/shader-lab/src/ShaderLab.ts index 37df11060b..95a804d95f 100644 --- a/packages/shader-lab/src/ShaderLab.ts +++ b/packages/shader-lab/src/ShaderLab.ts @@ -91,11 +91,13 @@ export class ShaderLab implements IShaderLab { for (const err of parser.errors) { this.errors.push(err); } + // #endif if (!program) { + // #if _VERBOSE this._logErrors(); + // #endif return undefined; } - // #endif const codeGen = backend === ShaderPlatformTarget.GLES100 ? GLES100Visitor.getVisitor() : GLES300Visitor.getVisitor(); diff --git a/packages/shader-lab/src/ShaderLabUtils.ts b/packages/shader-lab/src/ShaderLabUtils.ts index b2847b67e8..f48be18913 100644 --- a/packages/shader-lab/src/ShaderLabUtils.ts +++ b/packages/shader-lab/src/ShaderLabUtils.ts @@ -27,11 +27,12 @@ export class ShaderLabUtils { source: string, location: ShaderRange | ShaderPosition, file?: string - ) { + ): GSError { // #if _VERBOSE return new GSError(errorName, message, location, source, file); // #else - throw new Error(`[${errorName}]: ${message}`); + // @ts-ignore + return new Error(`[${errorName}]: ${message}`); // #endif } } diff --git a/packages/shader-lab/src/parser/ShaderTargetParser.ts b/packages/shader-lab/src/parser/ShaderTargetParser.ts index e0b70f8b66..125fe89fb2 100644 --- a/packages/shader-lab/src/parser/ShaderTargetParser.ts +++ b/packages/shader-lab/src/parser/ShaderTargetParser.ts @@ -121,8 +121,8 @@ export class ShaderTargetParser { ); // #if _VERBOSE this.sematicAnalyzer.errors.push(error); - return null; // #endif + return null; } } } From 2e3c530a5366b0a7cb30c1ba554e7c90a2ee4135 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 5 Nov 2024 15:44:38 +0800 Subject: [PATCH 112/131] feat: code opt --- packages/loader/src/MaterialLoader.ts | 26 ++++++---- packages/loader/src/ShaderChunkLoader.ts | 47 +++++++++++-------- packages/loader/src/ShaderLoader.ts | 26 ++-------- .../resources/schema/MaterialSchema.ts | 14 +++++- 4 files changed, 61 insertions(+), 52 deletions(-) diff --git a/packages/loader/src/MaterialLoader.ts b/packages/loader/src/MaterialLoader.ts index 5fc9cb295d..8e901b7348 100644 --- a/packages/loader/src/MaterialLoader.ts +++ b/packages/loader/src/MaterialLoader.ts @@ -11,7 +11,15 @@ import { resourceLoader } from "@galacean/engine-core"; import { Color, Vector2, Vector3, Vector4 } from "@galacean/engine-math"; -import type { IAssetRef, IColor, IMaterialSchema, IVector2, IVector3, IVector4 } from "./resource-deserialize"; +import { + EngineMaterialPropertyType, + type IAssetRef, + type IColor, + type IMaterialSchema, + type IVector2, + type IVector3, + type IVector4 +} from "./resource-deserialize"; function parseProperty(object: Object, key: string, value: any) { if (typeof value === "object") { @@ -64,31 +72,31 @@ class MaterialLoader extends Loader { const { type, value } = shaderData[key]; switch (type) { - case "Vector2": + case EngineMaterialPropertyType.Vector2: materialShaderData.setVector2(key, new Vector2((value).x, (value).y)); break; - case "Vector3": + case EngineMaterialPropertyType.Vector3: materialShaderData.setVector3( key, new Vector3((value).x, (value).y, (value).z) ); break; - case "Vector4": + case EngineMaterialPropertyType.Vector4: materialShaderData.setVector4( key, new Vector4((value).x, (value).y, (value).z, (value).w) ); break; - case "Color": + case EngineMaterialPropertyType.Color: materialShaderData.setColor( key, new Color((value).r, (value).g, (value).b, (value).a) ); break; - case "Float": + case EngineMaterialPropertyType.Float: materialShaderData.setFloat(key, value); break; - case "Texture": + case EngineMaterialPropertyType.Texture: texturePromises.push( // @ts-ignore engine.resourceManager.getResourceByRef(value).then((texture) => { @@ -96,10 +104,10 @@ class MaterialLoader extends Loader { }) ); break; - case "Boolean": + case EngineMaterialPropertyType.Boolean: materialShaderData.setInt(key, value ? 1 : 0); break; - case "Integer": + case EngineMaterialPropertyType.Integer: materialShaderData.setInt(key, Number(value)); break; } diff --git a/packages/loader/src/ShaderChunkLoader.ts b/packages/loader/src/ShaderChunkLoader.ts index 274bf1781a..fd72e38e98 100644 --- a/packages/loader/src/ShaderChunkLoader.ts +++ b/packages/loader/src/ShaderChunkLoader.ts @@ -20,25 +20,34 @@ class ShaderChunkLoader extends Loader { return this.request(url, { ...item, type: "text" }).then((code: string) => { ShaderFactory.registerInclude(chunkPath.substring(1), code); - const matches = code.matchAll(PathUtils.shaderIncludeRegex); - const shaderChunkPaths: string[] = []; - for (const match of matches) { - const chunkPath = PathUtils.pathResolve(match[1], shaderVirtualPath); - if (!ShaderLib[chunkPath.substring(1)]) { - shaderChunkPaths.push(chunkPath); - } - } - - return Promise.all( - shaderChunkPaths.map((chunkPath) => { - return resourceManager.load({ - type: "ShaderChunk", - url: chunkPath, - virtualPath: chunkPath, - params: { shaderVirtualPath } - }); - }) - ); + return _loadChunksInCode(code, shaderVirtualPath, resourceManager); }); } } + +/** @internal */ +export function _loadChunksInCode( + code: string, + shaderVirtualPath: string, + resourceManager: ResourceManager +): Promise { + const shaderChunkPaths: string[] = []; + const matches = code.matchAll(PathUtils.shaderIncludeRegex); + for (const match of matches) { + const chunkPath = PathUtils.pathResolve(match[1], shaderVirtualPath); + if (!ShaderLib[chunkPath.substring(1)]) { + shaderChunkPaths.push(chunkPath); + } + } + + return Promise.all( + shaderChunkPaths.map((chunkPath) => { + return resourceManager.load({ + type: "ShaderChunk", + url: chunkPath, + virtualPath: chunkPath, + params: { shaderVirtualPath } + }); + }) + ); +} diff --git a/packages/loader/src/ShaderLoader.ts b/packages/loader/src/ShaderLoader.ts index d86a4dc0f5..31c5574cb7 100644 --- a/packages/loader/src/ShaderLoader.ts +++ b/packages/loader/src/ShaderLoader.ts @@ -5,11 +5,9 @@ import { Loader, ResourceManager, Shader, - resourceLoader, - // @ts-ignore - ShaderLib + resourceLoader } from "@galacean/engine-core"; -import { PathUtils } from "./PathUtils"; +import { _loadChunksInCode } from "./ShaderChunkLoader"; @resourceLoader(AssetType.Shader, ["gs", "gsl"]) class ShaderLoader extends Loader { @@ -25,25 +23,7 @@ class ShaderLoader extends Loader { return Shader.find(builtinShader); } - const shaderChunkPaths: string[] = []; - const matches = code.matchAll(PathUtils.shaderIncludeRegex); - for (const match of matches) { - const chunkPath = PathUtils.pathResolve(match[1], shaderVirtualPath); - if (!ShaderLib[chunkPath.substring(1)]) { - shaderChunkPaths.push(chunkPath); - } - } - - return Promise.all( - shaderChunkPaths.map((chunkPath) => { - return resourceManager.load({ - type: "ShaderChunk", - url: chunkPath, - virtualPath: chunkPath, - params: { shaderVirtualPath } - }); - }) - ).then(() => { + return _loadChunksInCode(code, shaderVirtualPath, resourceManager).then(() => { return Shader.create(code); }); }); diff --git a/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts b/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts index 8364ac14b7..a4d2e15591 100644 --- a/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts +++ b/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts @@ -90,7 +90,7 @@ export interface IMaterialSchema { shader: string; shaderData: { [key: string]: { - type: "Vector2" | "Vector3" | "Vector4" | "Color" | "Float" | "Texture" | "Boolean" | "Integer"; + type: EngineMaterialPropertyType; value: IVector3 | IVector2 | IColor | number | IAssetRef; }; }; @@ -98,3 +98,15 @@ export interface IMaterialSchema { renderState: IRenderState; shaderRef: IShaderRef; } + +/** @internal */ +export enum EngineMaterialPropertyType { + Vector2 = "Vector2", + Vector3 = "Vector3", + Vector4 = "Vector4", + Color = "Color", + Float = "Float", + Texture = "Texture", + Boolean = "Boolean", + Integer = "Integer" +} From 882229f66357480280b5101f69bf83299e9532ff Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 5 Nov 2024 15:50:20 +0800 Subject: [PATCH 113/131] feat: code opt --- packages/loader/src/MaterialLoader.ts | 18 +++++++++--------- .../resources/schema/MaterialSchema.ts | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/loader/src/MaterialLoader.ts b/packages/loader/src/MaterialLoader.ts index 8e901b7348..66292ab4ef 100644 --- a/packages/loader/src/MaterialLoader.ts +++ b/packages/loader/src/MaterialLoader.ts @@ -12,7 +12,7 @@ import { } from "@galacean/engine-core"; import { Color, Vector2, Vector3, Vector4 } from "@galacean/engine-math"; import { - EngineMaterialPropertyType, + MaterialLoadType, type IAssetRef, type IColor, type IMaterialSchema, @@ -72,31 +72,31 @@ class MaterialLoader extends Loader { const { type, value } = shaderData[key]; switch (type) { - case EngineMaterialPropertyType.Vector2: + case MaterialLoadType.Vector2: materialShaderData.setVector2(key, new Vector2((value).x, (value).y)); break; - case EngineMaterialPropertyType.Vector3: + case MaterialLoadType.Vector3: materialShaderData.setVector3( key, new Vector3((value).x, (value).y, (value).z) ); break; - case EngineMaterialPropertyType.Vector4: + case MaterialLoadType.Vector4: materialShaderData.setVector4( key, new Vector4((value).x, (value).y, (value).z, (value).w) ); break; - case EngineMaterialPropertyType.Color: + case MaterialLoadType.Color: materialShaderData.setColor( key, new Color((value).r, (value).g, (value).b, (value).a) ); break; - case EngineMaterialPropertyType.Float: + case MaterialLoadType.Float: materialShaderData.setFloat(key, value); break; - case EngineMaterialPropertyType.Texture: + case MaterialLoadType.Texture: texturePromises.push( // @ts-ignore engine.resourceManager.getResourceByRef(value).then((texture) => { @@ -104,10 +104,10 @@ class MaterialLoader extends Loader { }) ); break; - case EngineMaterialPropertyType.Boolean: + case MaterialLoadType.Boolean: materialShaderData.setInt(key, value ? 1 : 0); break; - case EngineMaterialPropertyType.Integer: + case MaterialLoadType.Integer: materialShaderData.setInt(key, Number(value)); break; } diff --git a/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts b/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts index a4d2e15591..c149d61e05 100644 --- a/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts +++ b/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts @@ -90,7 +90,7 @@ export interface IMaterialSchema { shader: string; shaderData: { [key: string]: { - type: EngineMaterialPropertyType; + type: MaterialLoadType; value: IVector3 | IVector2 | IColor | number | IAssetRef; }; }; @@ -100,7 +100,7 @@ export interface IMaterialSchema { } /** @internal */ -export enum EngineMaterialPropertyType { +export enum MaterialLoadType { Vector2 = "Vector2", Vector3 = "Vector3", Vector4 = "Vector4", From e747bae84d94b809f4bb8e8ab28b85694f425a52 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 5 Nov 2024 15:50:35 +0800 Subject: [PATCH 114/131] feat: code opt --- .../src/resource-deserialize/resources/schema/MaterialSchema.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts b/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts index c149d61e05..0024cdd77f 100644 --- a/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts +++ b/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts @@ -99,7 +99,6 @@ export interface IMaterialSchema { shaderRef: IShaderRef; } -/** @internal */ export enum MaterialLoadType { Vector2 = "Vector2", Vector3 = "Vector3", From 0927b2d5f6e9d13ec48b9129f46a384f01cda798 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 5 Nov 2024 16:00:43 +0800 Subject: [PATCH 115/131] feat: code opt --- packages/loader/src/MaterialLoader.ts | 18 +++++++++--------- .../resources/schema/MaterialSchema.ts | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/loader/src/MaterialLoader.ts b/packages/loader/src/MaterialLoader.ts index 66292ab4ef..51d76f9ec8 100644 --- a/packages/loader/src/MaterialLoader.ts +++ b/packages/loader/src/MaterialLoader.ts @@ -12,7 +12,7 @@ import { } from "@galacean/engine-core"; import { Color, Vector2, Vector3, Vector4 } from "@galacean/engine-math"; import { - MaterialLoadType, + MaterialLoaderType, type IAssetRef, type IColor, type IMaterialSchema, @@ -72,31 +72,31 @@ class MaterialLoader extends Loader { const { type, value } = shaderData[key]; switch (type) { - case MaterialLoadType.Vector2: + case MaterialLoaderType.Vector2: materialShaderData.setVector2(key, new Vector2((value).x, (value).y)); break; - case MaterialLoadType.Vector3: + case MaterialLoaderType.Vector3: materialShaderData.setVector3( key, new Vector3((value).x, (value).y, (value).z) ); break; - case MaterialLoadType.Vector4: + case MaterialLoaderType.Vector4: materialShaderData.setVector4( key, new Vector4((value).x, (value).y, (value).z, (value).w) ); break; - case MaterialLoadType.Color: + case MaterialLoaderType.Color: materialShaderData.setColor( key, new Color((value).r, (value).g, (value).b, (value).a) ); break; - case MaterialLoadType.Float: + case MaterialLoaderType.Float: materialShaderData.setFloat(key, value); break; - case MaterialLoadType.Texture: + case MaterialLoaderType.Texture: texturePromises.push( // @ts-ignore engine.resourceManager.getResourceByRef(value).then((texture) => { @@ -104,10 +104,10 @@ class MaterialLoader extends Loader { }) ); break; - case MaterialLoadType.Boolean: + case MaterialLoaderType.Boolean: materialShaderData.setInt(key, value ? 1 : 0); break; - case MaterialLoadType.Integer: + case MaterialLoaderType.Integer: materialShaderData.setInt(key, Number(value)); break; } diff --git a/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts b/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts index 0024cdd77f..6f4a73eef5 100644 --- a/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts +++ b/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts @@ -90,7 +90,7 @@ export interface IMaterialSchema { shader: string; shaderData: { [key: string]: { - type: MaterialLoadType; + type: MaterialLoaderType; value: IVector3 | IVector2 | IColor | number | IAssetRef; }; }; @@ -99,7 +99,7 @@ export interface IMaterialSchema { shaderRef: IShaderRef; } -export enum MaterialLoadType { +export enum MaterialLoaderType { Vector2 = "Vector2", Vector3 = "Vector3", Vector4 = "Vector4", From c0e2a60192ba0e67a5f20148d1f77be27c8598d0 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 5 Nov 2024 16:02:22 +0800 Subject: [PATCH 116/131] feat: code opt --- packages/loader/src/ShaderLoader.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/loader/src/ShaderLoader.ts b/packages/loader/src/ShaderLoader.ts index 31c5574cb7..e21150ab94 100644 --- a/packages/loader/src/ShaderLoader.ts +++ b/packages/loader/src/ShaderLoader.ts @@ -19,6 +19,7 @@ class ShaderLoader extends Loader { return this.request(url, { ...item, type: "text" }).then((code: string) => { const builtinShader = this.getBuiltinShader(code); + // TODO: delete the snippets below when breaking change version released if (builtinShader) { return Shader.find(builtinShader); } From 2b911ee0f379c0ac627a038d11fff997351aa9c8 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 5 Nov 2024 17:23:40 +0800 Subject: [PATCH 117/131] fix: renderqueue assignment support variable --- .../src/contentParser/ShaderContentParser.ts | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/packages/shader-lab/src/contentParser/ShaderContentParser.ts b/packages/shader-lab/src/contentParser/ShaderContentParser.ts index a3345f1b88..cf234065df 100644 --- a/packages/shader-lab/src/contentParser/ShaderContentParser.ts +++ b/packages/shader-lab/src/contentParser/ShaderContentParser.ts @@ -26,8 +26,8 @@ import { import { GSErrorName } from "../GSError"; // #if _VERBOSE import { GSError } from "../GSError"; -import { ShaderLabUtils } from "../ShaderLabUtils"; // #endif +import { ShaderLabUtils } from "../ShaderLabUtils"; const EngineType = [ EKeyword.GS_RenderQueueType, @@ -329,20 +329,12 @@ export class ShaderContentParser { const word = scanner.scanToken(); scanner.scanText(";"); const value = ShaderContentParser._engineType.RenderQueueType[word.lexeme]; + const key = RenderStateDataKey.RenderQueueType; if (value == undefined) { - const error = ShaderLabUtils.createGSError( - `Invalid render queue ${word.lexeme}`, - GSErrorName.CompilationError, - scanner.source, - word.location - ); - // #if _VERBOSE - this._errors.push(error); - return; - // #endif + ret.renderStates.variableMap[key] = word.lexeme; + } else { + ret.renderStates.constantMap[key] = value; } - const key = RenderStateDataKey.RenderQueueType; - ret.renderStates.constantMap[key] = value; } private static _addGlobalStatement( From cc64947c91175615a469a5f16fa00b993b9d6cb2 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Tue, 12 Nov 2024 17:37:30 +0800 Subject: [PATCH 118/131] feat: code clean --- packages/core/src/asset/LoadItem.ts | 4 ---- packages/loader/src/PathUtils.ts | 13 ------------- .../resources/schema/BasicSchema.ts | 2 -- .../resources/schema/MaterialSchema.ts | 2 +- 4 files changed, 1 insertion(+), 20 deletions(-) delete mode 100644 packages/loader/src/PathUtils.ts diff --git a/packages/core/src/asset/LoadItem.ts b/packages/core/src/asset/LoadItem.ts index 34407cf296..7897f2bd61 100644 --- a/packages/core/src/asset/LoadItem.ts +++ b/packages/core/src/asset/LoadItem.ts @@ -26,10 +26,6 @@ export type LoadItem = { * Additional parameters for specified loader. */ params?: Record; - /** - * Asset path in editor - */ - virtualPath?: string; } & PickOnlyOne<{ /** * Loading url. diff --git a/packages/loader/src/PathUtils.ts b/packages/loader/src/PathUtils.ts deleted file mode 100644 index 2cc9fa8d0d..0000000000 --- a/packages/loader/src/PathUtils.ts +++ /dev/null @@ -1,13 +0,0 @@ -/** @internal */ -export class PathUtils { - private static _urlSchema = "files://"; - static shaderIncludeRegex = /\s#include\s+"([^\\"]+)"/gm; - - static pathResolve(path: string, base: string): string { - return new URL(path, PathUtils._urlSchema + base).href.substring(PathUtils._urlSchema.length); - } - - static isRelativePath(path: string): boolean { - return path[0] === "."; - } -} diff --git a/packages/loader/src/resource-deserialize/resources/schema/BasicSchema.ts b/packages/loader/src/resource-deserialize/resources/schema/BasicSchema.ts index ef49397f30..1cce358d62 100644 --- a/packages/loader/src/resource-deserialize/resources/schema/BasicSchema.ts +++ b/packages/loader/src/resource-deserialize/resources/schema/BasicSchema.ts @@ -92,5 +92,3 @@ export type IBasicType = export type IAssetRef = { key?: string; refId: string }; export type IEntityRef = { entityId: string }; - -export type IShaderRef = Omit; diff --git a/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts b/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts index 9df501328f..f2f6012918 100644 --- a/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts +++ b/packages/loader/src/resource-deserialize/resources/schema/MaterialSchema.ts @@ -7,7 +7,7 @@ import { RenderQueueType, StencilOperation } from "@galacean/engine-core"; -import type { IAssetRef, IColor, IShaderRef, IVector2, IVector3 } from "./BasicSchema"; +import type { IAssetRef, IColor, IVector2, IVector3 } from "./BasicSchema"; export interface IRenderState { /** Blend state. */ From fa6b2de005136c5d677974a85f8a90b280392b96 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Wed, 13 Nov 2024 17:33:48 +0800 Subject: [PATCH 119/131] fix: verbose version --- packages/shader-lab/src/preprocessor/PpParser.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index f63f3be1c3..283015ed80 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -105,7 +105,9 @@ export class PpParser { private static reportError(loc: ShaderRange | ShaderPosition, message: string, source: string, file: string) { const error = ShaderLabUtils.createGSError(message, GSErrorName.PreprocessorError, source, loc, file); + // #if _VERBOSE this._errors.push(error); + // #endif } private static _parseInclude(scanner: PpScanner) { From 671df031803ad99608d1b091267a830bbe3d863a Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 14 Nov 2024 16:54:39 +0800 Subject: [PATCH 120/131] fix: resouce manager load local resource --- packages/core/src/asset/ResourceManager.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/core/src/asset/ResourceManager.ts b/packages/core/src/asset/ResourceManager.ts index b76de6f79b..6dc2dd9921 100644 --- a/packages/core/src/asset/ResourceManager.ts +++ b/packages/core/src/asset/ResourceManager.ts @@ -573,19 +573,20 @@ export class ResourceManager { Logger.warn(`refId:${refId} is not find in this._editorResourceConfig.`); return Promise.resolve(null); } - const remoteUrl = resourceConfig.path; - const queryPath = new URL(remoteUrl).search; - let url = resourceConfig.virtualPath + queryPath; - if (key) { - url += (url.indexOf("?") > -1 ? "&" : "?") + "q=" + key; - } - + const url = resourceConfig.path; promise = this.load({ url, type: resourceConfig.type }); } - return promise.then((item) => (isClone ? item.clone() : item)); + return promise.then((item) => { + let resource = item; + if (key) { + const paths = this._parseQueryPath(key); + resource = this._getResolveResource(item, paths); + } + return isClone ? resource.clone() : resource; + }); } /** From 257dffe83a0ae3d91f5ce51ae22edf9db2733be1 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 14 Nov 2024 20:33:11 +0800 Subject: [PATCH 121/131] fix: unitest --- tests/vitest.config.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/vitest.config.ts b/tests/vitest.config.ts index 7014792762..bcfcd88158 100644 --- a/tests/vitest.config.ts +++ b/tests/vitest.config.ts @@ -15,6 +15,7 @@ export default defineProject({ provider: "playwright", enabled: true, name: "chromium", + headless: true, providerOptions: { launch: { args: ["--use-gl=egl", "--ignore-gpu-blocklist", "--use-gl=angle"] From caa9c01ecc62819db27907a93041456722e4077b Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 14 Nov 2024 20:35:39 +0800 Subject: [PATCH 122/131] fix: unitest --- tests/src/shader-lab/ShaderLab.test.ts | 2 -- tests/vitest.config.ts | 1 - 2 files changed, 3 deletions(-) diff --git a/tests/src/shader-lab/ShaderLab.test.ts b/tests/src/shader-lab/ShaderLab.test.ts index 71aae9b691..f996389a30 100644 --- a/tests/src/shader-lab/ShaderLab.test.ts +++ b/tests/src/shader-lab/ShaderLab.test.ts @@ -253,7 +253,5 @@ describe("ShaderLab", () => { for (const err of shaderLabVerbose.errors) { console.log(err.toString()); } - - expect(shaderParse.bind(shaderLabRelease, errorShader)).to.throw(Error); }); }); diff --git a/tests/vitest.config.ts b/tests/vitest.config.ts index bcfcd88158..7014792762 100644 --- a/tests/vitest.config.ts +++ b/tests/vitest.config.ts @@ -15,7 +15,6 @@ export default defineProject({ provider: "playwright", enabled: true, name: "chromium", - headless: true, providerOptions: { launch: { args: ["--use-gl=egl", "--ignore-gpu-blocklist", "--use-gl=angle"] From fb008e393f5defa205615dea3caef99c9deadbbd Mon Sep 17 00:00:00 2001 From: Sway007 Date: Thu, 14 Nov 2024 20:50:22 +0800 Subject: [PATCH 123/131] fix: e2e --- packages/core/src/asset/ResourceManager.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/core/src/asset/ResourceManager.ts b/packages/core/src/asset/ResourceManager.ts index 6dc2dd9921..b76de6f79b 100644 --- a/packages/core/src/asset/ResourceManager.ts +++ b/packages/core/src/asset/ResourceManager.ts @@ -573,20 +573,19 @@ export class ResourceManager { Logger.warn(`refId:${refId} is not find in this._editorResourceConfig.`); return Promise.resolve(null); } - const url = resourceConfig.path; + const remoteUrl = resourceConfig.path; + const queryPath = new URL(remoteUrl).search; + let url = resourceConfig.virtualPath + queryPath; + if (key) { + url += (url.indexOf("?") > -1 ? "&" : "?") + "q=" + key; + } + promise = this.load({ url, type: resourceConfig.type }); } - return promise.then((item) => { - let resource = item; - if (key) { - const paths = this._parseQueryPath(key); - resource = this._getResolveResource(item, paths); - } - return isClone ? resource.clone() : resource; - }); + return promise.then((item) => (isClone ? item.clone() : item)); } /** From 102770588849f117701b2bba8441cedb22b9c7d0 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Fri, 15 Nov 2024 16:04:01 +0800 Subject: [PATCH 124/131] feat: code opt --- packages/shader-lab/src/ShaderLabUtils.ts | 3 +-- packages/shader-lab/src/codeGen/VisitorContext.ts | 4 ++-- .../shader-lab/src/contentParser/ShaderContentParser.ts | 8 ++++---- packages/shader-lab/src/parser/ShaderTargetParser.ts | 4 ++-- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/packages/shader-lab/src/ShaderLabUtils.ts b/packages/shader-lab/src/ShaderLabUtils.ts index f48be18913..f4d411ef13 100644 --- a/packages/shader-lab/src/ShaderLabUtils.ts +++ b/packages/shader-lab/src/ShaderLabUtils.ts @@ -27,11 +27,10 @@ export class ShaderLabUtils { source: string, location: ShaderRange | ShaderPosition, file?: string - ): GSError { + ): Error { // #if _VERBOSE return new GSError(errorName, message, location, source, file); // #else - // @ts-ignore return new Error(`[${errorName}]: ${message}`); // #endif } diff --git a/packages/shader-lab/src/codeGen/VisitorContext.ts b/packages/shader-lab/src/codeGen/VisitorContext.ts index 3d6308f78e..3b6ee8fcb4 100644 --- a/packages/shader-lab/src/codeGen/VisitorContext.ts +++ b/packages/shader-lab/src/codeGen/VisitorContext.ts @@ -70,7 +70,7 @@ export class VisitorContext { GSErrorName.CompilationError, ShaderLab._processingPassText, ident.location - ); + ) as GSError; } this._referencedAttributeList[ident.lexeme] = prop; } @@ -85,7 +85,7 @@ export class VisitorContext { GSErrorName.CompilationError, ShaderLab._processingPassText, ident.location - ); + ) as GSError; } this._referencedVaryingList[ident.lexeme] = prop; } diff --git a/packages/shader-lab/src/contentParser/ShaderContentParser.ts b/packages/shader-lab/src/contentParser/ShaderContentParser.ts index cf234065df..eba5e1c058 100644 --- a/packages/shader-lab/src/contentParser/ShaderContentParser.ts +++ b/packages/shader-lab/src/contentParser/ShaderContentParser.ts @@ -192,7 +192,7 @@ export class ShaderContentParser { variable.location ); // #if _VERBOSE - this._errors.push(error); + this._errors.push(error); return; // #endif } @@ -249,7 +249,7 @@ export class ShaderContentParser { scanner.getCurPosition() ); // #if _VERBOSE - this._errors.push(error); + this._errors.push(error); scanner.scanToCharacter(";"); return; // #endif @@ -267,7 +267,7 @@ export class ShaderContentParser { scanner.getCurPosition() ); // #if _VERBOSE - this._errors.push(error); + this._errors.push(error); scanner.scanToCharacter(";"); return; // #endif @@ -307,7 +307,7 @@ export class ShaderContentParser { engineTypeProp.location ); // #if _VERBOSE - this._errors.push(error); + this._errors.push(error); scanner.scanToCharacter(";"); return; // #endif diff --git a/packages/shader-lab/src/parser/ShaderTargetParser.ts b/packages/shader-lab/src/parser/ShaderTargetParser.ts index 125fe89fb2..2bbb3da72c 100644 --- a/packages/shader-lab/src/parser/ShaderTargetParser.ts +++ b/packages/shader-lab/src/parser/ShaderTargetParser.ts @@ -10,7 +10,7 @@ import { addTranslationRule, createGrammar } from "../lalr/CFG"; import { LALR1 } from "../lalr"; import { ParserUtils } from "../ParserUtils"; import { Logger } from "@galacean/engine"; -import { GSErrorName } from "../GSError"; +import { GSError, GSErrorName } from "../GSError"; import { ShaderLab } from "../ShaderLab"; import { ShaderLabUtils } from "../ShaderLabUtils"; @@ -120,7 +120,7 @@ export class ShaderTargetParser { token.location ); // #if _VERBOSE - this.sematicAnalyzer.errors.push(error); + this.sematicAnalyzer.errors.push(error); // #endif return null; } From e57f2aab0c64b0d816467868dc6240f4ebe2bf9c Mon Sep 17 00:00:00 2001 From: Sway007 Date: Fri, 15 Nov 2024 16:09:29 +0800 Subject: [PATCH 125/131] feat: code opt --- packages/shader-lab/src/codeGen/CodeGenVisitor.ts | 4 ++-- packages/shader-lab/src/codeGen/VisitorContext.ts | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts index a42b3abe33..ec817461f4 100644 --- a/packages/shader-lab/src/codeGen/CodeGenVisitor.ts +++ b/packages/shader-lab/src/codeGen/CodeGenVisitor.ts @@ -46,7 +46,7 @@ export class CodeGenVisitor { const error = context.referenceAttribute(prop); // #if _VERBOSE if (error) { - this.errors.push(error); + this.errors.push(error); } // #endif return prop.lexeme; @@ -54,7 +54,7 @@ export class CodeGenVisitor { const error = context.referenceVarying(prop); // #if _VERBOSE if (error) { - this.errors.push(error); + this.errors.push(error); } // #endif return prop.lexeme; diff --git a/packages/shader-lab/src/codeGen/VisitorContext.ts b/packages/shader-lab/src/codeGen/VisitorContext.ts index 3b6ee8fcb4..6a177d12b7 100644 --- a/packages/shader-lab/src/codeGen/VisitorContext.ts +++ b/packages/shader-lab/src/codeGen/VisitorContext.ts @@ -60,7 +60,7 @@ export class VisitorContext { return this.varyingStruct?.ident?.lexeme === type; } - referenceAttribute(ident: BaseToken): GSError { + referenceAttribute(ident: BaseToken): Error { if (this._referencedAttributeList[ident.lexeme]) return; const prop = this.attributeList.find((item) => item.ident.lexeme === ident.lexeme); @@ -70,12 +70,12 @@ export class VisitorContext { GSErrorName.CompilationError, ShaderLab._processingPassText, ident.location - ) as GSError; + ); } this._referencedAttributeList[ident.lexeme] = prop; } - referenceVarying(ident: BaseToken): GSError | undefined { + referenceVarying(ident: BaseToken): Error | undefined { if (this._referencedVaryingList[ident.lexeme]) return; const prop = this.varyingStruct?.propList.find((item) => item.ident.lexeme === ident.lexeme); @@ -85,7 +85,7 @@ export class VisitorContext { GSErrorName.CompilationError, ShaderLab._processingPassText, ident.location - ) as GSError; + ); } this._referencedVaryingList[ident.lexeme] = prop; } From 0c8d1d6f73ff5ea48532d93666f4917661cdd1c7 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Fri, 15 Nov 2024 16:39:48 +0800 Subject: [PATCH 126/131] feat: code opt --- .../shader-lab/src/preprocessor/PpParser.ts | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 283015ed80..b3d607e081 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -644,20 +644,21 @@ export class PpParser { } private static _onToken(token: BaseToken, scanner: PpScanner) { - this._skipEditorBlock(token, scanner); - this._expandToken(token, scanner); + if (["EditorProperties", "EditorMacros", "Editor"].indexOf(token.lexeme) !== -1) { + this._skipEditorBlock(token, scanner); + } else { + this._expandToken(token, scanner); + } } private static _skipEditorBlock(token: BaseToken, scanner: PpScanner) { - if (["EditorProperties", "EditorMacros", "Editor"].indexOf(token.lexeme) !== -1) { - const start = scanner.current - token.lexeme.length; - scanner.scanPairedBlock("{", "}"); - const end = scanner.current; - const startPosition = ShaderLab.createPosition(start); - const endPosition = ShaderLab.createPosition(end); - const range = ShaderLab.createRange(startPosition, endPosition); - this.expandSegments.push({ rangeInBlock: range, replace: "" }); - } + const start = scanner.current - token.lexeme.length; + scanner.scanPairedBlock("{", "}"); + const end = scanner.current; + const startPosition = ShaderLab.createPosition(start); + const endPosition = ShaderLab.createPosition(end); + const range = ShaderLab.createRange(startPosition, endPosition); + this.expandSegments.push({ rangeInBlock: range, replace: "" }); } private static _expandToken(token: BaseToken, scanner: PpScanner) { From c05c7ef9ac12d043857e374bad5ac3239bac7718 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Fri, 15 Nov 2024 16:42:19 +0800 Subject: [PATCH 127/131] feat: code opt --- packages/shader-lab/src/preprocessor/PpParser.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index b3d607e081..84951a10c1 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -645,13 +645,13 @@ export class PpParser { private static _onToken(token: BaseToken, scanner: PpScanner) { if (["EditorProperties", "EditorMacros", "Editor"].indexOf(token.lexeme) !== -1) { - this._skipEditorBlock(token, scanner); + this._skipBlock(token, scanner); } else { this._expandToken(token, scanner); } } - private static _skipEditorBlock(token: BaseToken, scanner: PpScanner) { + private static _skipBlock(token: BaseToken, scanner: PpScanner) { const start = scanner.current - token.lexeme.length; scanner.scanPairedBlock("{", "}"); const end = scanner.current; From 9de010425113fb476ad30d23b1a36dde587c5c71 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Fri, 15 Nov 2024 16:45:12 +0800 Subject: [PATCH 128/131] feat: code opt --- packages/shader-lab/src/preprocessor/PpParser.ts | 4 ++-- packages/shader-lab/src/preprocessor/constants.ts | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 84951a10c1..acec28b517 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -2,7 +2,7 @@ import { ShaderPosition, ShaderRange } from "../common"; import LexerUtils from "../lexer/Utils"; import { MacroDefine } from "./MacroDefine"; import { BaseToken } from "../common/BaseToken"; -import { EPpKeyword, EPpToken, PpConstant } from "./constants"; +import { EPpKeyword, EPpToken, PpConstant, SkipTokens } from "./constants"; import PpScanner from "./PpScanner"; import { PpUtils } from "./Utils"; import { ShaderLab } from "../ShaderLab"; @@ -644,7 +644,7 @@ export class PpParser { } private static _onToken(token: BaseToken, scanner: PpScanner) { - if (["EditorProperties", "EditorMacros", "Editor"].indexOf(token.lexeme) !== -1) { + if (SkipTokens.indexOf(token.lexeme) !== -1) { this._skipBlock(token, scanner); } else { this._expandToken(token, scanner); diff --git a/packages/shader-lab/src/preprocessor/constants.ts b/packages/shader-lab/src/preprocessor/constants.ts index 98a8912213..857dba8fc5 100644 --- a/packages/shader-lab/src/preprocessor/constants.ts +++ b/packages/shader-lab/src/preprocessor/constants.ts @@ -70,3 +70,5 @@ export const PpKeyword = new Map([ ]); export type PpConstant = boolean | number; + +export const SkipTokens = ["EditorProperties", "EditorMacros", "Editor"]; From eeb11886ba0e2226326a9920ec509103f932239b Mon Sep 17 00:00:00 2001 From: Sway007 Date: Fri, 15 Nov 2024 17:20:45 +0800 Subject: [PATCH 129/131] feat: remove redundant --- .../shader-lab/src/preprocessor/PpParser.ts | 18 ++---------------- .../shader-lab/src/preprocessor/constants.ts | 2 -- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index acec28b517..2a4ab3bdca 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -2,7 +2,7 @@ import { ShaderPosition, ShaderRange } from "../common"; import LexerUtils from "../lexer/Utils"; import { MacroDefine } from "./MacroDefine"; import { BaseToken } from "../common/BaseToken"; -import { EPpKeyword, EPpToken, PpConstant, SkipTokens } from "./constants"; +import { EPpKeyword, EPpToken, PpConstant } from "./constants"; import PpScanner from "./PpScanner"; import { PpUtils } from "./Utils"; import { ShaderLab } from "../ShaderLab"; @@ -644,21 +644,7 @@ export class PpParser { } private static _onToken(token: BaseToken, scanner: PpScanner) { - if (SkipTokens.indexOf(token.lexeme) !== -1) { - this._skipBlock(token, scanner); - } else { - this._expandToken(token, scanner); - } - } - - private static _skipBlock(token: BaseToken, scanner: PpScanner) { - const start = scanner.current - token.lexeme.length; - scanner.scanPairedBlock("{", "}"); - const end = scanner.current; - const startPosition = ShaderLab.createPosition(start); - const endPosition = ShaderLab.createPosition(end); - const range = ShaderLab.createRange(startPosition, endPosition); - this.expandSegments.push({ rangeInBlock: range, replace: "" }); + this._expandToken(token, scanner); } private static _expandToken(token: BaseToken, scanner: PpScanner) { diff --git a/packages/shader-lab/src/preprocessor/constants.ts b/packages/shader-lab/src/preprocessor/constants.ts index 857dba8fc5..98a8912213 100644 --- a/packages/shader-lab/src/preprocessor/constants.ts +++ b/packages/shader-lab/src/preprocessor/constants.ts @@ -70,5 +70,3 @@ export const PpKeyword = new Map([ ]); export type PpConstant = boolean | number; - -export const SkipTokens = ["EditorProperties", "EditorMacros", "Editor"]; From 28b9a958e69de7bc52a2d254909d2f664c9ab0f7 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Fri, 15 Nov 2024 17:29:25 +0800 Subject: [PATCH 130/131] feat: remove redundant code --- packages/shader-lab/src/preprocessor/PpParser.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/shader-lab/src/preprocessor/PpParser.ts b/packages/shader-lab/src/preprocessor/PpParser.ts index 2a4ab3bdca..df8e76982a 100644 --- a/packages/shader-lab/src/preprocessor/PpParser.ts +++ b/packages/shader-lab/src/preprocessor/PpParser.ts @@ -644,10 +644,6 @@ export class PpParser { } private static _onToken(token: BaseToken, scanner: PpScanner) { - this._expandToken(token, scanner); - } - - private static _expandToken(token: BaseToken, scanner: PpScanner) { const macro = this._definedMacros.get(token.lexeme); if (macro) { let replace = macro.body.lexeme; From 2749c7612edc397338e13d37e72abcfcdce02501 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Fri, 15 Nov 2024 17:46:19 +0800 Subject: [PATCH 131/131] feat: add unitest --- tests/src/shader-lab/ShaderLab.test.ts | 2 ++ tests/src/shader-lab/shaders/demo.shader | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tests/src/shader-lab/ShaderLab.test.ts b/tests/src/shader-lab/ShaderLab.test.ts index f996389a30..08b367ba52 100644 --- a/tests/src/shader-lab/ShaderLab.test.ts +++ b/tests/src/shader-lab/ShaderLab.test.ts @@ -136,6 +136,8 @@ describe("ShaderLab", () => { expect(pass1.renderStates).not.be.null; const { constantMap, variableMap } = pass1.renderStates; + expect(Object.values(variableMap).includes("customRenderQueue")); + expect(constantMap).not.be.null; expect(toString(constantMap[RenderStateDataKey.BlendStateBlendColor] as Color)).eq("Color(1, 1, 1, 1)"); diff --git a/tests/src/shader-lab/shaders/demo.shader b/tests/src/shader-lab/shaders/demo.shader index c6ca75e4f2..c0ff0dc8f7 100644 --- a/tests/src/shader-lab/shaders/demo.shader +++ b/tests/src/shader-lab/shaders/demo.shader @@ -38,6 +38,8 @@ Shader "Water" { Pass "default" { Tags { ReplacementTag = "Opaque", pipelineStage = "DepthOnly"} + RenderQueueType = customRenderQueue; + struct a2v { vec4 POSITION; vec2 TEXCOORD_0;