Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
33 changes: 22 additions & 11 deletions packages/shader-lab/src/sourceParser/ShaderSourceParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,8 @@ export class ShaderSourceParser {
scanner: SourceLexer
) {
const ident = scanner.scanToken();
let isDeclaration: boolean;
if (ident.type === ETokenType.ID) {
isDeclaration = true;
scanner.scanText("{");
} else if (ident.lexeme === "{") {
isDeclaration = false;
} else if (ident.lexeme === "=") {
const variable = scanner.scanToken();
scanner.scanText(";");
Expand All @@ -157,15 +153,17 @@ export class ShaderSourceParser {
Object.assign(renderStates.constantMap, renderState.constantMap);
Object.assign(renderStates.variableMap, renderState.variableMap);
return;
} else {
throw ShaderLabUtils.createGSError(
`Invalid render state syntax`,
GSErrorName.CompilationError,
scanner.source,
ident.location
);
Copy link
Member

Choose a reason for hiding this comment

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

Not cache?

}

const renderState = this._parseRenderStatePropList(stateToken.lexeme, scanner);
if (isDeclaration) {
this._symbolTableStack.insert({ ident: ident.lexeme, type: stateToken.type, value: renderState });
} else {
Object.assign(renderStates.constantMap, renderState.constantMap);
Object.assign(renderStates.variableMap, renderState.variableMap);
}
this._symbolTableStack.insert({ ident: ident.lexeme, type: stateToken.type, value: renderState });
}

private static _parseVariableDeclaration(type: number, scanner: SourceLexer) {
Expand Down Expand Up @@ -275,6 +273,19 @@ export class ShaderSourceParser {
}
} else {
value = token.lexeme;
const sm = ShaderSourceParser._lookupSymbolByType(value, ETokenType.ID);

if (!sm) {
const error = ShaderLabUtils.createGSError(
`Invalid variable: ${value}`,
GSErrorName.CompilationError,
scanner.source,
token.location
);
// #if _VERBOSE
this._errors.push(<GSError>error);
Copy link
Member

Choose a reason for hiding this comment

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

scanner.scanText(";"); is missed?

// #endif
}
}
}
scanner.scanText(";");
Expand Down Expand Up @@ -312,6 +323,7 @@ export class ShaderSourceParser {
const key = RenderStateDataKey.RenderQueueType;
if (value == undefined) {
const sm = ShaderSourceParser._lookupSymbolByType(word.lexeme, Keyword.GSRenderQueueType);
renderStates.variableMap[key] = word.lexeme;
if (!sm) {
const error = ShaderLabUtils.createGSError(
`Invalid RenderQueueType variable: ${word.lexeme}`,
Expand All @@ -324,7 +336,6 @@ export class ShaderSourceParser {
return;
// #endif
}
renderStates.variableMap[key] = word.lexeme;
} else {
renderStates.constantMap[key] = value;
}
Expand Down
1 change: 0 additions & 1 deletion packages/shader-lab/src/sourceParser/ShaderSourceParser.y
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ variable_type:

render_state_assignment:
render_state_declarator '=' id ';'
| render_state_declarator '{' render_state_prop_list '}'
;

render_state_declaration:
Expand Down
20 changes: 10 additions & 10 deletions tests/src/shader-lab/ShaderLab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,6 @@ describe("ShaderLab", () => {
registerIncludes();
});

it("builtin-function", async () => {
let shaderSource = await readFile("./shaders/builtin-function.shader");
shaderSource = shaderSource.replace("__$$insert_maros$$__", commonMacros);
glslValidate(shaderSource, shaderLabVerbose, {});
});

it("create shaderLab", async () => {
expect(shaderLabVerbose).not.be.null;
});
Expand All @@ -172,7 +166,7 @@ describe("ShaderLab", () => {
expect(pass1.renderStates).not.be.null;

const { constantMap, variableMap } = pass1.renderStates;
expect(Object.values(variableMap).includes("customRenderQueue"));
expect(Object.values(variableMap).includes("customRenderQueue")).to.be.true;

expect(constantMap).not.be.null;

Expand All @@ -181,7 +175,6 @@ describe("ShaderLab", () => {
expect(constantMap).include({
// Stencil State
[RenderStateDataKey.StencilStateEnabled]: true,
[RenderStateDataKey.StencilStateReferenceValue]: 2,
[RenderStateDataKey.StencilStateMask]: 1.3,
[RenderStateDataKey.StencilStateWriteMask]: 0.32,
[RenderStateDataKey.StencilStateCompareFunctionFront]: CompareFunction.Less,
Expand All @@ -205,8 +198,15 @@ describe("ShaderLab", () => {
[RenderStateDataKey.BlendStateSourceAlphaBlendFactor0]: "material_SrcBlend"
});

expect(shaderLabVerbose.errors.length).to.eq(1);
expect(shaderLabVerbose.errors[0].message).contains("Invalid RenderQueueType variable: Unknown");
expect(shaderLabVerbose.errors.length).to.eq(2);
expect(shaderLabVerbose.errors[0].message).contains("Invalid RenderQueueType variable: customRenderQueue");
expect(shaderLabVerbose.errors[1].message).contains("Invalid variable: referenceValue");
});

it("builtin-function", async () => {
let shaderSource = await readFile("./shaders/builtin-function.shader");
shaderSource = shaderSource.replace("__$$insert_maros$$__", commonMacros);
glslValidate(shaderSource, shaderLabVerbose, {});
});

it("shader tags", () => {
Expand Down
10 changes: 7 additions & 3 deletions tests/src/shader-lab/shaders/builtin-function.shader
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,26 @@ Shader "/Folder1/test.gs" {
Pass "Forward Pass" {
Tags { pipelineStage = "Forward"}

DepthState {
DepthState depthState {
WriteEnabled = depthWriteEnabled;
}

BlendState {
BlendState blendState {
Enabled = blendEnabled;
SourceColorBlendFactor = sourceColorBlendFactor;
DestinationColorBlendFactor = destinationColorBlendFactor;
SourceAlphaBlendFactor = sourceAlphaBlendFactor;
DestinationAlphaBlendFactor = destinationAlphaBlendFactor;
}

RasterState{
RasterState rasterState {
CullMode = rasterStateCullMode;
}

DepthState = depthState;
BlendState = blendState;
RasterState = rasterState;

#define IS_METALLIC_WORKFLOW
#define MATERIAL_ENABLE_IRIDESCENCE

Expand Down
14 changes: 10 additions & 4 deletions tests/src/shader-lab/shaders/compilation-error.shader
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,34 @@ Shader "custom/pbr" {
Pass "Pass0" {

#ifdef MATERIAL_IS_TRANSPARENT
BlendState {
BlendState blendState {
Enabled = true;
SourceColorBlendFactor = BlendFactor.SourceAlpha;
DestinationColorBlendFactor = BlendFactor.OneMinusSourceAlpha;
SourceAlphaBlendFactor = BlendFactor.One;
DestinationAlphaBlendFactor = BlendFactor.OneMinusSourceAlpha;
}
DepthState {
DepthState depthState {
WriteEnabled = false;
}

BlendState = blendState;
DepthState = depthState;
RenderQueueType = Transparent;
#else
BlendState {
BlendState blendState {
Enabled = false;
SourceColorBlendFactor = BlendFactor.SourceAlpha;
DestinationColorBlendFactor = BlendFactor.OneMinusSourceAlpha;
SourceAlphaBlendFactor = BlendFactor.One;
DestinationAlphaBlendFactor = BlendFactor.OneMinusSourceAlpha;
}
DepthState {
DepthState depthState {
WriteEnabled = true;
}

BlendState = blendState;
DepthState = depthState;
RenderQueueType = Opaque;
#endif

Expand Down
8 changes: 5 additions & 3 deletions tests/src/shader-lab/shaders/demo.shader
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ Shader "Water" {

BlendFactor material_SrcBlend;

BlendState {
BlendState blendState {
SourceAlphaBlendFactor = material_SrcBlend;
Enabled[0] = true;
ColorWriteMask[0] = 0.8;
BlendColor = Color(1.0, 1.0, 1.0, 1.0);
AlphaBlendOperation = BlendOperation.Max;
}
BlendState = blendState;

UsePass "pbr/Default/Forward"

Expand Down Expand Up @@ -63,15 +64,16 @@ Shader "Water" {
return vec4(pow(linearIn.rgb, vec3(1.0 / 2.2)), linearIn.a);
}

StencilState {
StencilState stencilState {
Enabled = true;
ReferenceValue = 2;
ReferenceValue = referenceValue;
Mask = 1.3; // 0xffffffff
WriteMask = 0.32; // 0xffffffff
CompareFunctionFront = CompareFunction.Less;
PassOperationBack = StencilOperation.Zero;
}

StencilState = stencilState;
DepthState = depthState;
RasterState = rasterState;

Expand Down
3 changes: 2 additions & 1 deletion tests/src/shader-lab/shaders/macro-pre.shader
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Shader "Water" {

BlendState = blendState;

StencilState {
StencilState stencilState {
Enabled = true;
ReferenceValue = 2;
Mask = 1.3; // 0xffffffff
Expand All @@ -63,6 +63,7 @@ Shader "Water" {
PassOperationBack = StencilOperation.Zero;
}

StencilState = stencilState;
DepthState = depthState;
RasterState = rasterState;

Expand Down
7 changes: 5 additions & 2 deletions tests/src/shader-lab/shaders/planarShadow.shader
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ Shader "PlanarShadow" {

Pass "planarShadow" {
// render states
DepthState {
DepthState depthState {
WriteEnabled = true;
}

DepthState = depthState;

BlendState blendState {
Enabled[0] = true;
SourceColorBlendFactor = BlendFactor.SourceAlpha;
Expand All @@ -18,7 +20,7 @@ Shader "PlanarShadow" {
DestinationAlphaBlendFactor = BlendFactor.OneMinusSourceAlpha;
}

StencilState {
StencilState stencilState {
Enabled = true;
ReferenceValue = 0;
CompareFunctionFront = CompareFunction.Equal;
Expand All @@ -32,6 +34,7 @@ Shader "PlanarShadow" {
}

BlendState = blendState;
StencilState = stencilState;

vec3 u_lightDir;
float u_planarHeight;
Expand Down
3 changes: 2 additions & 1 deletion tests/src/shader-lab/shaders/unlit.shader
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Shader "Water" {

BlendState = blendState;

StencilState {
StencilState stencilState {
Enabled = true;
ReferenceValue = 2;
Mask = 1.3; // 0xffffffff
Expand All @@ -68,6 +68,7 @@ Shader "Water" {
PassOperationBack = StencilOperation.Zero;
}

StencilState = stencilState;
DepthState = depthState;
RasterState = rasterState;

Expand Down