8000 Fix `ShaderLab` diagnostics position error when source include `EditorProperties` & `EditorMacros` module by Sway007 · Pull Request #1966 · galacean/engine · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix ShaderLab diagnostics position error when source include EditorProperties & EditorMacros module #1966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 43 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
5a26f3d
fix(shader-lab): compatible with empty macro
Sway007 Sep 20, 2023
5ecc318
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Sep 20, 2023
cafc24f
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Sep 20, 2023
dc69489
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Sep 21, 2023
221b7b6
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Sep 21, 2023
0d45d9c
fix(shader-lab): add break and continue syntax
Sway007 Sep 21, 2023
0f14c3f
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Sep 27, 2023
8871d9b
fix: typo
Sway007 Oct 11, 2023
3b4ffd7
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Nov 1, 2023
f649a58
fix(shader-lab): Make usepass compatible with buitin shader
Sway007 Nov 1, 2023
598fc56
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Nov 2, 2023
e33a66f
fix(shader-lab): compatible with no varying variable
Sway007 Nov 2, 2023
41ef06f
feat(shader-lab): detect mismatch return type
Sway007 Nov 6, 2023
b5214fc
fix(shader-lab): renderState assignment
Sway007 Nov 7, 2023
f36ce02
feat: extend material loader data type
Sway007 Nov 13, 2023
ff8b7c2
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Nov 13, 2023
634236f
fix(shader-lab): glsl type pattern
Sway007 Nov 13, 2023
91e6fa4
fix(shader-lab): glsl type pattern
Sway007 Nov 13, 2023
3932448
fix: switch case break
Sway007 Nov 15, 2023
bef3a13
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Nov 20, 2023
60638fa
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Nov 29, 2023
6552ddb
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Nov 29, 2023
e9b38d3
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Dec 7, 2023
cb42f6d
fix: detect shaders with duplicate name
Sway007 Dec 7, 2023
845ed83
feat: merge main
Sway007 Dec 8, 2023
08e70e5
fix: correct ast-node's position when having module
Sway007 Dec 8, 2023
05db00f
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Dec 13, 2023
62cafc5
feat: add shader destroy function
Sway007 Dec 13, 2023
4fdc5e5
feat: opt code
Sway007 Dec 13, 2023
1e3b615
Merge branch 'pr/shader/destroy' into fix/shaderlab/position-offset
Sway007 Dec 13, 2023
6307246
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Dec 18, 2023
94499a4
Merge branch 'main' of https://github.com/galacean/engine
Sway007 Dec 20, 2023
0f50ec9
Merge branch 'main' of https://github.com/galacean/engine into fix/sh…
Sway007 Dec 25, 2023
89a470a
Merge branch 'main' into fix/shaderlab/position-offset
Sway007 Dec 27, 2023
36600ac
feat: merge dev/1.2
Sway007 Dec 27, 2023
0ee673c
refactor: syntax for include code fragment
Sway007 Jan 8, 2024
eef181c
fix: recorrect diagnostic postion when shaderlab include editor prope…
Sway007 Jan 11, 2024
459e037
Merge branch 'dev/1.2' of https://github.com/galacean/engine into fix…
Sway007 Jan 11, 2024
cb6ac43
fix: shaderProgramPool recursiveDestroy
Sway007 Jan 11, 2024
395d250
feat: opt code
Sway007 Jan 11, 2024
c1199a0
Merge branch 'dev/1.2' of https://github.com/galacean/engine into fix…
Sway007 Jan 12, 2024
1229bac
feat: opt code
Sway007 Jan 16, 2024
177d510
feat: opt code
Sway007 Jan 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/shader-lab/src/Ast2GLSLUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class Ast2GLSLUtils {
(fn) => fn.content.name === vertexFnProperty.content.value.content.variable
);
if (!vertFnAst) {
context.diagnostics.push({
context.addDiagnostic({
severity: DiagnosticSeverity.Error,
message: `Not found vertex shader definition: ${vertexFnProperty.content.value}`,
token: vertexFnProperty.position
Expand All @@ -39,7 +39,7 @@ export class Ast2GLSLUtils {
if (type.isCustom) {
const structAstNode = context.findGlobal(type.text).ast as StructAstNode;
if (!structAstNode) {
context.diagnostics.push({
context.addDiagnostic({
severity: DiagnosticSeverity.Error,
message: "no attribute struct definition",
token: arg.position
Expand Down Expand Up @@ -86,7 +86,7 @@ export class Ast2GLSLUtils {
(fn) => fn.content.name === fragmentFnProperty.content.value.content.variable
);
if (!fragFnAst) {
context.diagnostics.push({
context.addDiagnostic({
severity: DiagnosticSeverity.Error,
message: `Not found fragment shader definition: ${fragmentFnProperty.content.value}`,
token: fragmentFnProperty.position
Expand Down
10 changes: 6 additions & 4 deletions packages/shader-lab/src/AstNodeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export class AstNodeUtils {

static defaultVisit(this: ICstVisitor<any, AstNode>, ctx: CstChildrenDictionary): ObjectAstNode {
const content = {} as Record<string, AstNode>;
let start: IPosition = { line: Number.MAX_SAFE_INTEGER, character: -1 },
end: IPosition = { line: 0, character: -1 };
let start: IPosition = { line: Number.MAX_SAFE_INTEGER, character: -1, index: -1 },
end: IPosition = { line: 0, character: -1, index: -1 };

for (const k in ctx) {
if (AstNodeUtils.isCstNode(ctx[k][0])) {
Expand Down Expand Up @@ -63,11 +63,13 @@ export class AstNodeUtils {
return {
start: {
line: token.startLine,
character: token.startColumn
character: token.startColumn,
index: token.startOffset
},
end: {
line: token.endLine,
character: token.endColumn
character: token.endColumn,
index: token.endOffset
}
};
}
Expand Down
66 changes: 66 additions & 0 deletions packages/shader-lab/src/ParsingContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { IPosition } from "./ast-node";

export interface IPositionOffset {
/** Offset of the first character of the Token. 0-indexed. */
index: number;
line: number;
}

export default class ParsingContext {
private _parseString: string;
get parseString(): string {
return this._parseString;
}

private _positionOffsetList: IPositionOffset[] = [];
get positionOffsetList() {
return this._positionOffsetList;
}

constructor(input: string) {
this._parseString = input;
}

balanceGroups(title: string) {
let balance = 0;
let start = -1;
let end = -1;
let line = 0;
for (let i = 0; i < this._parseString.length; i++) {
if (this._parseString.substring(i, i + title.length) === title) {
start = i;
}
if (start < 0) continue;

if (this._parseString[i] === "{") {
balance++;
} else if (this._parseString[i] === "}") {
if (--balance === 0) {
end = i + 1;
break;
}
} else if (this._parseString[i] === "\n") {
line++;
}
}
if (start >= 0) return { start, end, line };
}

filterString(title: string) {
let range = this.balanceGroups(title);
if (range) {
this._parseString = this._parseString.slice(0, range.start) + this._parseString.slice(range.end);
this._positionOffsetList.push({ index: range.start, line: range.line });
}
}

getTextLineOffsetAt(index: number) {
let offset = 0;
for (const item of this._positionOffsetList) {
if (index >= item.index) {
offset += item.line;
}
}
return offset;
}
}
23 changes: 19 additions & 4 deletions packages/shader-lab/src/RuntimeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from "./ast-node";
import { AstNodeUtils } from "./AstNodeUtils";
import { RenderStateDataKey } from "@galacean/engine";
import ParsingContext from "./ParsingContext";

export interface IDiagnostic {
severity: DiagnosticSeverity;
Expand Down Expand Up @@ -66,7 +67,7 @@ export default class RuntimeContext {

functionAstStack: { fnAst: FnAstNode; localDeclaration: VariableDeclarationAstNode[] }[] = [];
/** Diagnostic for linting service. */
diagnostics: IDiagnostic[] = [];
private _diagnostics: IDiagnostic[] = [];
/** Varying info. */
varyingTypeAstNode?: ReturnTypeAstNode;
/** Varying. */
Expand Down Expand Up @@ -94,6 +95,11 @@ export default class RuntimeContext {
private _passGlobalMap: GlobalMap = new Map();
/** The main function */
private _currentMainFnAst?: FnAstNode;
private _parsingContext: ParsingContext;

set parsingContext(context: ParsingContext) {
this._parsingContext = context;
}

/** Current position */
get serializingAstNode() {
Expand All @@ -108,6 +114,15 @@ export default class RuntimeContext {
return this._currentMainFnAst;
}

addDiagnostic(diagnostic: IDiagnostic) {
let offset = this._parsingContext.getTextLineOffsetAt(diagnostic.token.start.index);
if (offset) {
diagnostic.token.start.line += offset;
diagnostic.token.end.line += offset;
}
this._diagnostics.push(diagnostic);
}

setSerializingNode(node: AstNode) {
this._serializingNodeStack.push(node);
}
Expand Down Expand Up @@ -166,7 +181,7 @@ export default class RuntimeContext {
switch (prop.content.type) {
case VERT_FN_NAME:
if (ret.vertexSource) {
this.diagnostics.push({
this.addDiagnostic({
severity: DiagnosticSeverity.Error,
message: "multiple vertex main function found",
token: prop.position
Expand All @@ -178,7 +193,7 @@ export default class RuntimeContext {
break;
case FRAG_FN_NAME:
if (ret.fragmentSource) {
this.diagnostics.push({
this.addDiagnostic({
severity: DiagnosticSeverity.Error,
message: "multiple fragment main function found",
token: prop.position
Expand All @@ -193,7 +208,7 @@ export default class RuntimeContext {
const variable = prop.content.value;
const astNode = this.findGlobal(variable.content.variable)?.ast;
if (!astNode) {
this.diagnostics.push({
this.addDiagnostic({
severity: DiagnosticSeverity.Error,
message: "variable definition not found",
token: prop.position
Expand Down
20 changes: 13 additions & 7 deletions packages/shader-lab/src/ShaderLab.ts F438
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IShaderLab } from "@galacean/engine-design";
import { ShaderParser } from "./parser/ShaderParser";
import { ShaderVisitor } from "./ShaderVisitor";
import RuntimeContext from "./RuntimeContext";
import ParsingContext from "./ParsingContext";

export class ShaderLab implements IShaderLab {
/** @internal */
Expand All @@ -23,21 +24,26 @@ export class ShaderLab implements IShaderLab {
}

parseShader(shaderSource: string) {
const editorPropertiesRegex = /EditorProperties\s+\{[^}]*?\}/;
const parsingContext = new ParsingContext(shaderSource);
this._context.parsingContext = parsingContext;
parsingContext.filterString("EditorProperties");
parsingContext.filterString("EditorMacros");

const input = shaderSource.replace(editorPropertiesRegex, "");

this._parser.parse(input);
this._parser.parse(parsingContext.parseString);
const cst = this._parser.ruleShader();
if (this._parser.errors.length > 0) {
console.log(this._parser.errors);
for (const err of this._parser.errors) {
const offset = parsingContext.getTextLineOffsetAt(err.token.startOffset);
if (offset) {
err.token.startLine += offset;
err.token.endLine += offset;
}
}
throw this._parser.errors;
}

const ast = this._visitor.visit(cst);

const shaderInfo = this._context.parse(ast);

return shaderInfo;
}
}
52 changes: 42 additions & 10 deletions packages/shader-lab/src/ShaderVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ export class ShaderVisitor extends ShaderVisitorConstructor implements Partial<I
}

_ruleFnBody(ctx: _ruleFnBodyCstChildren) {
let start: IPosition = { line: Number.MAX_SAFE_INTEGER, character: -1 },
end: IPosition = { line: 0, character: -1 };
let start: IPosition = { line: Number.MAX_SAFE_INTEGER, character: -1, index: -1 },
end: IPosition = { line: 0, character: -1, index: -1 };

const iterate = (item: CstNode) => {
const astInfo = this.visit(item);
Expand Down Expand Up @@ -749,8 +749,16 @@ export class ShaderVisitor extends ShaderVisitorConstructor implements Partial<I

_ruleBlendStatePropertyDeclaration(children: _ruleBlendStatePropertyDeclarationCstChildren, param?: any) {
const position: IPositionRange = {
start: { line: children.BlendState[0].startLine, character: children.BlendState[0].startOffset },
end: { line: children.RCurly[0].endLine, character: children.RCurly[0].endOffset }
start: {
line: children.BlendState[0].startLine,
character: children.BlendState[0].startColumn,
index: children.BlendState[0].startOffset
},
end: {
line: children.RCurly[0].endLine,
character: children.RCurly[0].endColumn,
index: children.RCurly[0].endOffset
}
};

const variable = children.Identifier?.[0].image;
Expand Down Expand Up @@ -817,8 +825,16 @@ export class ShaderVisitor extends ShaderVisitorConstructor implements Partial<I

_ruleRasterStatePropertyDeclaration(children: _ruleRasterStatePropertyDeclarationCstChildren, param?: any) {
const position: IPositionRange = {
start: { line: children.RasterState[0].startLine, character: children.RasterState[0].startOffset },
end: { line: children.RCurly[0].startLine, character: children.RCurly[0].startOffset }
start: {
line: children.RasterState[0].startLine,
character: children.RasterState[0].startColumn,
index: children.RasterState[0].startOffset
},
end: {
line: children.RCurly[0].startLine,
character: children.RCurly[0].endColumn,
index: children.RasterState[0].endOffset
}
};

const variable = children.Identifier?.[0].image;
Expand All @@ -831,8 +847,16 @@ export class ShaderVisitor extends ShaderVisitorConstructor implements Partial<I

_ruleDepthSatePropertyDeclaration(children: _ruleDepthSatePropertyDeclarationCstChildren, param?: any) {
const position: IPositionRange = {
start: { line: children.DepthState[0].startLine, character: children.DepthState[0].startOffset },
end: { line: children.RCurly[0].startLine, character: children.RCurly[0].startOffset }
start: {
line: children.DepthState[0].startLine,
character: children.DepthState[0].startOffset,
index: children.DepthState[0].startOffset
},
end: {
line: children.RCurly[0].endLine,
character: children.RCurly[0].endColumn,
index: children.RCurly[0].endOffset
}
};

const variable = children.Identifier?.[0].image;
Expand Down Expand Up @@ -887,8 +911,16 @@ export class ShaderVisitor extends ShaderVisitorConstructor implements Partial<I

_ruleStencilStatePropertyDeclaration(children: _ruleStencilStatePropertyDeclarationCstChildren, param?: any) {
const position: IPositionRange = {
start: { line: children.StencilState[0].startLine, character: children.StencilState[0].startOffset },
end: { line: children.RCurly[0].startLine, character: children.RCurly[0].startOffset }
start: {
line: children.StencilState[0].startLine,
character: children.StencilState[0].startColumn,
index: children.StencilState[0].startOffset
},
end: {
line: children.RCurly[0].endLine,
character: children.RCurly[0].endColumn,
index: children.RCurly[0].endOffset
}
};

const variable = children.Identifier?.[0].image;
Expand Down
14 changes: 8 additions & 6 deletions packages/shader-lab/src/ast-node/AstNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ import {
export interface IPosition {
line: number;
character: number;
// offset from first character
index: number;
}

export interface IPositionRange {
Expand Down Expand Up @@ -208,7 +210,7 @@ export class FnAstNode extends AstNode<IFnAstContent> {
(this.content.returnType.content.text === "void" && this.content.returnStatement) ||
(this.content.returnType.content.text !== "void" && !this.content.returnStatement)
) {
context.diagnostics.push({
context.addDiagnostic({
severity: DiagnosticSeverity.Error,
message: "Mismatched return type",
token: this.position
Expand Down Expand Up @@ -317,7 +319,7 @@ export class FnCallAstNode extends AstNode<IFnCallAstContent> {
override _doSerialization(context: RuntimeContext): string {
if (this.content.isCustom) {
if (!context.referenceGlobal(this.content.function)) {
context.diagnostics.push({
context.addDiagnostic({
severity: DiagnosticSeverity.Warning,
message: `Not found function definition: ${this.content.function}`,
token: this.position
Expand Down Expand Up @@ -497,7 +499,7 @@ export class FnVariableAstNode extends AstNode<IFnVariableAstContent> {
}
if (!context.findLocal(objName)) {
if (!context.referenceGlobal(objName)) {
context.diagnostics.push({
context.addDiagnostic({
severity: DiagnosticSeverity.Error,
message: `Not found variable definition: ${objName}`,
token: this.position
Expand Down Expand Up @@ -571,7 +573,7 @@ export class RenderStateDeclarationAstNode extends AstNode<IRenderStateDeclarati
}
const renderStateKey = RenderStateDataKey[_propertyKey];
if (renderStateKey === undefined) {
context?.diagnostics.push({
context?.addDiagnostic({
severity: DiagnosticSeverity.Error,
message: "invalid render state key",
token: prop.position
Expand Down Expand Up @@ -603,7 +605,7 @@ export class RenderStatePropertyItemAstNode extends AstNode<IRenderStateProperty
if (isVariable && context) {
const global = context.findGlobal(this.content.value.content);
if (!global) {
context.diagnostics.push({
context.addDiagnostic({
severity: DiagnosticSeverity.Warning,
message: "not found variable definition",
token: this.content.value.position
Expand Down Expand Up @@ -645,7 +647,7 @@ export class VariableDeclarationAstNode extends AstNode<IFnVariableDeclarationAs
}
if (typeNode.content.isCustom) {
if (!context.referenceGlobal(typeNode.content.text)) {
context.diagnostics.push({
context.addDiagnostic({
severity: DiagnosticSeverity.Error,
message: `Undefined type ${typeNode.content.text}`,
token: this.position
Expand Down
Loading
0