Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix ShaderLab variable list declaration & CRLF break & macro #if bug #2465

Merged
merged 26 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6613ba2
feat: supprot global declaration list
Sway007 Dec 16, 2024
4130a87
feat: update unitest
Sway007 Dec 16, 2024
b27c4bd
feat: update unitest
Sway007 Dec 16, 2024
637d884
fix: last comment
Sway007 Dec 16, 2024
6f1fa10
feat: code opt
Sway007 Dec 16, 2024
e348595
feat: disable glsl compress for non mini package
Sway007 Dec 18, 2024
6c99773
Merge branch 'dev/1.4' of github.com:galacean/engine into dev/1.4
Sway007 Dec 25, 2024
c9fd98c
Merge branch 'dev/1.4' of github.com:galacean/engine into fix/shaderl…
Sway007 Dec 25, 2024
47da18a
feat: code opt
Sway007 Dec 26, 2024
8b0de12
feat: code opt
Sway007 Dec 26, 2024
cd47edc
fix: compatible with empty macro in if branch
Sway007 Dec 27, 2024
bc17b5e
Merge branch 'dev/1.4' of github.com:galacean/engine into dev/1.4
Sway007 Jan 2, 2025
b80c07d
Merge branch 'dev/1.4' of github.com:galacean/engine into dev/1.4
Sway007 Jan 2, 2025
6dac155
fix: shaderlab compatible with CRLF format
Sway007 Jan 2, 2025
3131488
Merge branch 'fix/shaderlab/crlf' into fix/shaderlab/global_decl_list
Sway007 Jan 2, 2025
fe9a4bd
Merge branch 'dev/1.4' of github.com:galacean/engine into dev/1.4
Sway007 Jan 2, 2025
f1efb56
Merge branch 'dev/1.4' of github.com:galacean/engine into dev/1.4
Sway007 Jan 6, 2025
d9a142a
Merge branch 'dev/1.4' of github.com:galacean/engine into dev/1.4
Sway007 Jan 7, 2025
3ec3b99
fix: ci
Sway007 Jan 3, 2025
35b5926
Merge branch 'dev/1.4' of github.com:galacean/engine into dev/1.4
Sway007 Jan 8, 2025
3748049
Merge branch 'dev/1.4' into fix/shaderlab/global_decl_list
Sway007 Jan 8, 2025
6cd03dd
feat: skip space compatible with multiple line
Sway007 Jan 8, 2025
f775ee5
feat: code opt
Sway007 Jan 8, 2025
b814758
feat: code opt
Sway007 Jan 8, 2025
ec9f4da
feat: code opt
Sway007 Jan 8, 2025
434742e
feat: code opt
Sway007 Jan 8, 2025
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
2 changes: 1 addition & 1 deletion packages/shader-lab/src/common/BaseScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default class BaseScanner {
const start = this.getCurPosition();
this.advance(2);
// single line comments
while (this.getCurChar() !== "\n") this._advance();
while (this.getCurChar() !== "\n" && !this.isEnd()) this._advance();
zhuxudong marked this conversation as resolved.
Show resolved Hide resolved
this.skipCommentsAndSpace();
return ShaderLab.createRange(start, this.getCurPosition());
} else if (this.peek(2) === "/*") {
Expand Down
1 change: 1 addition & 0 deletions packages/shader-lab/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export enum ETokenType {
COLON,
/** = */
EQUAL,
/** ; */
SEMICOLON,
/** ! */
BANG,
Expand Down
21 changes: 17 additions & 4 deletions packages/shader-lab/src/lalr/CFG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,34 @@ const productionAndRules: [GrammarSymbol[], TranslationRule | undefined][] = [

...GrammarUtils.createProductionWithOptions(NoneTerminal.global_declaration, [
[NoneTerminal.precision_specifier],
[NoneTerminal.variable_declaration],
[NoneTerminal.variable_declaration_statement],
[NoneTerminal.struct_specifier],
[NoneTerminal.function_definition]
]),

...GrammarUtils.createProductionWithOptions(
NoneTerminal.variable_declaration,
[
[EKeyword.GS_RenderQueueType, ETokenType.ID, ETokenType.SEMICOLON],
[NoneTerminal.fully_specified_type, ETokenType.ID, ETokenType.SEMICOLON],
[NoneTerminal.fully_specified_type, ETokenType.ID, NoneTerminal.array_specifier, ETokenType.SEMICOLON]
[NoneTerminal.fully_specified_type, ETokenType.ID],
[NoneTerminal.fully_specified_type, ETokenType.ID, NoneTerminal.array_specifier]
],
ASTNode.VariableDeclaration.pool
),

...GrammarUtils.createProductionWithOptions(
NoneTerminal.variable_declaration_list,
[
[NoneTerminal.variable_declaration],
[NoneTerminal.variable_declaration_list, ETokenType.COMMA, ETokenType.ID],
[NoneTerminal.variable_declaration_list, ETokenType.COMMA, ETokenType.ID, NoneTerminal.array_specifier]
],
ASTNode.VariableDeclarationList.pool
),

...GrammarUtils.createProductionWithOptions(NoneTerminal.variable_declaration_statement, [
[NoneTerminal.variable_declaration_list, ETokenType.SEMICOLON]
]),

...GrammarUtils.createProductionWithOptions(
NoneTerminal.ext_builtin_type_specifier_nonarray,
[
Expand Down
73 changes: 59 additions & 14 deletions packages/shader-lab/src/parser/AST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,31 @@
}

override semanticAnalyze(sa: SematicAnalyzer): void {
const fullyType = this.children[0] as FullySpecifiedType;
const id = this.children[1] as Token;
this.typeSpecifier = fullyType.typeSpecifier;
const children = this.children;
const childrenLen = children.length;
const fullyType = children[0] as FullySpecifiedType;
const typeSpecifier = fullyType.typeSpecifier;
this.typeSpecifier = typeSpecifier;
this.arraySpecifier = typeSpecifier.arraySpecifier;

const id = children[1] as Token;

let sm: VarSymbol;
if (this.children.length === 2 || this.children.length === 4) {
const symbolType = new SymbolType(fullyType.type, fullyType.typeSpecifier.lexeme);
const initializer = this.children[3] as Initializer;
if (childrenLen === 2 || childrenLen === 4) {
zhuxudong marked this conversation as resolved.
Show resolved Hide resolved
const symbolType = new SymbolType(fullyType.type, typeSpecifier.lexeme, this.arraySpecifier);
const initializer = children[3] as Initializer;

sm = new VarSymbol(id.lexeme, symbolType, false, initializer);
} else {
const arraySpecifier = this.children[2] as ArraySpecifier;
const arraySpecifier = children[2] as ArraySpecifier;
// #if _VERBOSE
if (arraySpecifier && this.arraySpecifier) {
sa.reportError(arraySpecifier.location, "Array of array is not supported.");
}

Check warning on line 201 in packages/shader-lab/src/parser/AST.ts

View check run for this annotation

Codecov / codecov/patch

packages/shader-lab/src/parser/AST.ts#L200-L201

Added lines #L200 - L201 were not covered by tests
// #endif
this.arraySpecifier = arraySpecifier;
const symbolType = new SymbolType(fullyType.type, fullyType.typeSpecifier.lexeme, arraySpecifier);
const initializer = this.children[4] as Initializer;
const symbolType = new SymbolType(fullyType.type, typeSpecifier.lexeme, this.arraySpecifier);
const initializer = children[4] as Initializer;

sm = new VarSymbol(id.lexeme, symbolType, false, initializer);
}
Expand Down Expand Up @@ -288,6 +298,9 @@
get arraySize(): number {
return (this.children?.[1] as ArraySpecifier)?.size;
}
get arraySpecifier(): ArraySpecifier {
return this.children[1] as ArraySpecifier;
}

get isCustom() {
return typeof this.type === "string";
Expand Down Expand Up @@ -1110,17 +1123,49 @@

@ASTNodeDecorator(NoneTerminal.variable_declaration)
export class VariableDeclaration extends TreeNode {
type: FullySpecifiedType;

override semanticAnalyze(sa: SematicAnalyzer): void {
const type = this.children[0] as FullySpecifiedType;
const ident = this.children[1] as Token;
let sm: VarSymbol;
sm = new VarSymbol(ident.lexeme, new SymbolType(type.type, type.typeSpecifier.lexeme), true, this);
const children = this.children;
const type = children[0] as FullySpecifiedType;
const ident = children[1] as Token;
this.type = type;
const sm = new VarSymbol(ident.lexeme, new SymbolType(type.type, type.typeSpecifier.lexeme), true, this);

sa.symbolTableStack.insert(sm);
}

override codeGen(visitor: CodeGenVisitor): string {
return visitor.visitGlobalVariableDeclaration(this);
return visitor.visitGlobalVariableDeclaration(this) + ";";
}
}

@ASTNodeDecorator(NoneTerminal.variable_declaration_list)
export class VariableDeclarationList extends TreeNode {
type: FullySpecifiedType;

override semanticAnalyze(sa: SematicAnalyzer): void {
const { children } = this;
const length = children.length;
const variableDeclaration = children[0] as VariableDeclaration;
const type = variableDeclaration.type;
this.type = type;

if (length === 1) {
return;
}

const ident = children[2] as Token;

const newVariable = VariableDeclaration.pool.get();
if (length === 3) {
// variable_declaration_list ',' id
newVariable.set(ident.location, [type, ident]);

Check warning on line 1163 in packages/shader-lab/src/parser/AST.ts

View check run for this annotation

Codecov / codecov/patch

packages/shader-lab/src/parser/AST.ts#L1163

Added line #L1163 was not covered by tests
} else {
// variable_declaration_list ',' id array_specifier
newVariable.set(ident.location, [type, ident, children[3] as ArraySpecifier]);
}
newVariable.semanticAnalyze(sa);
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/shader-lab/src/parser/GrammarSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export enum NoneTerminal {
// glsl
global_declaration,
variable_declaration,
variable_declaration_list,
variable_declaration_statement,
array_specifier_list,
array_specifier,
ext_builtin_type_specifier_nonarray,
Expand Down
15 changes: 12 additions & 3 deletions packages/shader-lab/src/parser/TargetParser.y
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,25 @@ gs_shader_program:

global_declaration:
precision_specifier
| variable_declaration
| variable_declaration_statement
| struct_specifier
| function_definition
;

variable_declaration:
fully_specified_type id ';'
| fully_specified_type id array_specifier ';'
fully_specified_type id
| fully_specified_type id array_specifier
;

variable_declaration_list:
variable_declaration
| variable_declaration_list ',' id
| variable_declaration_list ',' id array_specifier
;

variable_declaration_statement:
variable_declaration_list ';'

variable_identifier:
id
;
Expand Down
3 changes: 3 additions & 0 deletions packages/shader-lab/src/parser/builtin/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,13 @@ BuiltinFunction._create("textureLod", EGenType.GVec4, EGenType.GSampler3D, EKeyw
BuiltinFunction._create("textureLod", EGenType.GVec4, EGenType.GSamplerCube, EKeyword.VEC3, EKeyword.FLOAT);
BuiltinFunction._create("textureLod", EKeyword.FLOAT, EKeyword.SAMPLER2D_SHADOW, EKeyword.VEC3, EKeyword.FLOAT);
BuiltinFunction._create("textureLod", EGenType.GVec4, EGenType.GSampler2DArray, EKeyword.VEC3, EKeyword.FLOAT);
BuiltinFunction._create("texture2DLodEXT", EGenType.GVec4, EGenType.GSampler2D, EKeyword.VEC2, EKeyword.FLOAT);
BuiltinFunction._create("texture2DLodEXT", EGenType.GVec4, EGenType.GSampler3D, EKeyword.VEC3, EKeyword.FLOAT);

BuiltinFunction._create("textureCube", EKeyword.SAMPLER_CUBE, EKeyword.VEC3);
BuiltinFunction._create("textureCube", EKeyword.SAMPLER_CUBE, EKeyword.VEC3, EKeyword.FLOAT);
BuiltinFunction._create("textureCubeLod", EKeyword.SAMPLER_CUBE, EKeyword.VEC3, EKeyword.FLOAT);
BuiltinFunction._create("textureCubeLodEXT", EGenType.GVec4, EGenType.GSamplerCube, EKeyword.VEC3, EKeyword.FLOAT);

BuiltinFunction._create(
"textureOffset",
Expand Down
19 changes: 19 additions & 0 deletions tests/src/shader-lab/shaders/demo.shader
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,20 @@ Shader "Water" {

/*Comment without leading space*/

// test global declaration list.
vec2 v1, v2[2], v3[3];

v2f vert(a2v v) {
v2f o;

vec2 weights[2], offsets[2];
weights[0] = vec2(.1);
offsets[1] = vec2(.1);

float[2] c;
c[0] = 1.0;
c[1] = .4;

o.v_uv = v.TEXCOORD_0;
vec4 tmp = renderer_MVMat * v.POSITION;
o.v_position = tmp.xyz;
Expand All @@ -109,6 +120,14 @@ Shader "Water" {
gl_FragColor = linearToGamma(gl_FragColor);
#endif


#define MATERIAL_ENABLE_SS_REFRACTION

#ifdef MATERIAL_ENABLE_SS_REFRACTION

// last lint comment
#endif

// For testing only (macro)
#if SCENE_SHADOW_TYPE == 2 || defined(XX_Macro)
gl_FragColor = linearToGamma(gl_FragColor);
Expand Down
Loading