Refactor: variant-aware shader/material pipeline overhaul
Major architectural update to graphics/material/shader system: - Introduced strongly-typed key structs (Key64/Key128) for passes, variants, and pipelines; removed legacy key types. - Implemented robust hashing and key generation utilities for efficient variant and pipeline lookup/caching. - Shader compiler now compiles/caches all keyword variants using new key system; includes handled as lists. - Switched to push constant root signature for per-draw data; updated HLSL and C# codegen accordingly. - Refactored Material, Shader, and Pass data structures for cache efficiency and variant support. - Pipeline library and PSO management now use 128-bit keys and variant-specific caching. - Replaced WorldNode with SceneNode in editor/scene graph; introduced ComponentManager for archetype/query management. - Migrated math utilities to Misaki.HighPerformance.Mathematics; updated editor controls. - Updated all HLSL and codegen for new buffer/push constant layouts and macros. - Misc: project reference cleanup, D3D12 Work Graph support, doc updates, and code modernization.
This commit is contained in:
45
Ghost.DSL/ShaderCompiler/Parser/DefinesBlock.cs
Normal file
45
Ghost.DSL/ShaderCompiler/Parser/DefinesBlock.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
namespace Ghost.DSL.ShaderCompiler.Parser;
|
||||
|
||||
internal class DefinesBlock : IBlockParser<List<Token>, List<string>>
|
||||
{
|
||||
public static bool ShouldEnter(Token token)
|
||||
{
|
||||
return token.Match(TokenType.Keyword, TokenLexicon.KnownKeywords.DEFINES);
|
||||
}
|
||||
|
||||
public static List<Token> Parse(TokenStreamSlice stream)
|
||||
{
|
||||
stream.Expect(TokenType.Keyword);
|
||||
stream.Expect(TokenType.LBrace);
|
||||
|
||||
var defines = new List<Token>();
|
||||
|
||||
var bodyStream = stream.Slice(stream.Remaining - 1);
|
||||
while (bodyStream.HasMore)
|
||||
{
|
||||
var defineToken = bodyStream.Expect(TokenType.Identifier);
|
||||
defines.Add(defineToken);
|
||||
bodyStream.Expect(TokenType.Semicolon);
|
||||
}
|
||||
|
||||
stream.Expect(TokenType.RBrace);
|
||||
|
||||
return defines;
|
||||
}
|
||||
|
||||
public static List<string>? SemanticAnalysis(List<Token>? syntax, List<DSLShaderError> errors)
|
||||
{
|
||||
if (syntax == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var defines = new List<string>(syntax.Count);
|
||||
foreach (var defineToken in syntax)
|
||||
{
|
||||
defines.Add(defineToken.lexeme);
|
||||
}
|
||||
|
||||
return defines;
|
||||
}
|
||||
}
|
||||
8
Ghost.DSL/ShaderCompiler/Parser/IBlockParser.cs
Normal file
8
Ghost.DSL/ShaderCompiler/Parser/IBlockParser.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Ghost.DSL.ShaderCompiler.Parser;
|
||||
|
||||
internal interface IBlockParser<T, U>
|
||||
{
|
||||
public static abstract bool ShouldEnter(Token token);
|
||||
public static abstract T? Parse(TokenStreamSlice ts);
|
||||
public static abstract U? SemanticAnalysis(T? syntax, List<DSLShaderError> errors);
|
||||
}
|
||||
59
Ghost.DSL/ShaderCompiler/Parser/IncludesBlock.cs
Normal file
59
Ghost.DSL/ShaderCompiler/Parser/IncludesBlock.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
namespace Ghost.DSL.ShaderCompiler.Parser;
|
||||
|
||||
internal class IncludesBlock : IBlockParser<List<Token>, List<string>>
|
||||
{
|
||||
public static bool ShouldEnter(Token token)
|
||||
{
|
||||
return token.Match(TokenType.Keyword, TokenLexicon.KnownKeywords.INCLUDES);
|
||||
}
|
||||
|
||||
public static List<Token> Parse(TokenStreamSlice stream)
|
||||
{
|
||||
stream.Expect(TokenType.Keyword);
|
||||
stream.Expect(TokenType.LBrace);
|
||||
|
||||
var includes = new List<Token>();
|
||||
|
||||
var bodyStream = stream.Slice(stream.Remaining - 1);
|
||||
while (bodyStream.HasMore)
|
||||
{
|
||||
var includeToken = bodyStream.Expect(TokenType.StringLiteral);
|
||||
includes.Add(includeToken);
|
||||
bodyStream.Expect(TokenType.Semicolon);
|
||||
}
|
||||
|
||||
stream.Expect(TokenType.RBrace);
|
||||
|
||||
return includes;
|
||||
}
|
||||
|
||||
public static List<string>? SemanticAnalysis(List<Token>? syntax, List<DSLShaderError> errors)
|
||||
{
|
||||
if (syntax == null || syntax.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var includes = new List<string>(syntax.Count);
|
||||
foreach (var includeToken in syntax)
|
||||
{
|
||||
var path = includeToken.lexeme;
|
||||
if (File.Exists(path))
|
||||
{
|
||||
includes.Add(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Included file '{path}' not found.",
|
||||
line = includeToken.line,
|
||||
column = includeToken.column
|
||||
});
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return includes;
|
||||
}
|
||||
}
|
||||
84
Ghost.DSL/ShaderCompiler/Parser/KeywordsBlock.cs
Normal file
84
Ghost.DSL/ShaderCompiler/Parser/KeywordsBlock.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using Ghost.Core.Graphics;
|
||||
|
||||
namespace Ghost.DSL.ShaderCompiler.Parser;
|
||||
|
||||
internal class KeywordsBlock : IBlockParser<List<FunctionCallDeclaration>, List<KeywordsGroup>>
|
||||
{
|
||||
public static bool ShouldEnter(Token token)
|
||||
{
|
||||
return token.Match(TokenType.Keyword, TokenLexicon.KnownKeywords.KEYWORDS);
|
||||
}
|
||||
|
||||
public static List<FunctionCallDeclaration> Parse(TokenStreamSlice stream)
|
||||
{
|
||||
stream.Expect(TokenType.Keyword);
|
||||
stream.Expect(TokenType.LBrace);
|
||||
|
||||
var keywords = new List<FunctionCallDeclaration>();
|
||||
|
||||
var bodyStream = stream.Slice(stream.Remaining - 1);
|
||||
while (bodyStream.HasMore)
|
||||
{
|
||||
var keywordToken = bodyStream.Expect(TokenType.Identifier);
|
||||
var args = ParseUtility.ParseFunctionArguments(ref bodyStream, TokenType.Identifier);
|
||||
keywords.Add(new FunctionCallDeclaration { name = keywordToken, arguments = args });
|
||||
bodyStream.Expect(TokenType.Semicolon);
|
||||
}
|
||||
|
||||
stream.Expect(TokenType.RBrace);
|
||||
|
||||
return keywords;
|
||||
}
|
||||
|
||||
public static List<KeywordsGroup>? SemanticAnalysis(List<FunctionCallDeclaration>? syntax, List<DSLShaderError> errors)
|
||||
{
|
||||
if (syntax == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var keywords = new List<KeywordsGroup>(syntax.Count);
|
||||
foreach (var keyword in syntax)
|
||||
{
|
||||
if (keyword.arguments == null || keyword.arguments.Count == 0)
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Function '{keyword.name.lexeme}' must have at least one argument.",
|
||||
line = keyword.name.line,
|
||||
column = keyword.name.column
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
var group = new KeywordsGroup();
|
||||
switch (keyword.name.lexeme)
|
||||
{
|
||||
case TokenLexicon.KnownFunctions.LOCAL:
|
||||
group.space = KeywordSpace.Local;
|
||||
break;
|
||||
case TokenLexicon.KnownFunctions.GLOBAL:
|
||||
group.space = KeywordSpace.Global;
|
||||
break;
|
||||
default:
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Unknown function name '{keyword.name.lexeme}'.",
|
||||
line = keyword.name.line,
|
||||
column = keyword.name.column
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var arg in keyword.arguments)
|
||||
{
|
||||
group.keywords ??= new List<string>(keyword.arguments.Count);
|
||||
group.keywords.Add(arg.lexeme);
|
||||
}
|
||||
|
||||
keywords.Add(group);
|
||||
}
|
||||
|
||||
return keywords;
|
||||
}
|
||||
}
|
||||
71
Ghost.DSL/ShaderCompiler/Parser/ParseUtility.cs
Normal file
71
Ghost.DSL/ShaderCompiler/Parser/ParseUtility.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
namespace Ghost.DSL.ShaderCompiler.Parser;
|
||||
|
||||
internal static class ParseUtility
|
||||
{
|
||||
public static List<Token> ParseFunctionArguments(ref TokenStreamSlice stream, TokenType tokenType)
|
||||
{
|
||||
var args = new List<Token>();
|
||||
stream.Expect(TokenType.LParen);
|
||||
|
||||
while (!stream.Peek().type.Equals(TokenType.RParen))
|
||||
{
|
||||
var argToken = stream.Expect(tokenType);
|
||||
args.Add(argToken);
|
||||
|
||||
if (stream.Peek().type == TokenType.Comma)
|
||||
{
|
||||
stream.Consume();
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
stream.Expect(TokenType.RParen);
|
||||
return args;
|
||||
}
|
||||
|
||||
public static FunctionCallDeclaration ParseFunction(ref TokenStreamSlice stream, TokenType tokenType)
|
||||
{
|
||||
var functionToken = stream.Expect(TokenType.Identifier);
|
||||
var args = ParseFunctionArguments(ref stream, tokenType);
|
||||
stream.Expect(TokenType.Semicolon);
|
||||
|
||||
return new FunctionCallDeclaration
|
||||
{
|
||||
name = functionToken,
|
||||
arguments = args
|
||||
};
|
||||
}
|
||||
|
||||
public static bool TrySliceLine(ref TokenStreamSlice stream, out TokenStreamSlice lineStream)
|
||||
{
|
||||
var length = 0;
|
||||
if (!stream.TryPeek(out var nextToken))
|
||||
{
|
||||
lineStream = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
while (!nextToken.Match(TokenType.Semicolon) && !nextToken.Match(TokenType.RBrace))
|
||||
{
|
||||
length++;
|
||||
|
||||
if (!stream.TryPeek(length, out nextToken))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (length > 0)
|
||||
{
|
||||
lineStream = stream.Slice(length);
|
||||
stream.Consume(); // Consume the semicolon
|
||||
return true;
|
||||
}
|
||||
|
||||
lineStream = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
140
Ghost.DSL/ShaderCompiler/Parser/PassBlock.cs
Normal file
140
Ghost.DSL/ShaderCompiler/Parser/PassBlock.cs
Normal file
@@ -0,0 +1,140 @@
|
||||
using Ghost.Core.Graphics;
|
||||
|
||||
namespace Ghost.DSL.ShaderCompiler.Parser;
|
||||
|
||||
// TODO: Add pass template support.
|
||||
// Pass templates let user to inject their own custom code into the generated HLSL code.
|
||||
// This is useful for adding custom lighting models, custom shadowing techniques, or other advanced effects without touching the core shader code.
|
||||
internal class PassBlock : IBlockParser<PassSyntax, PassSemantic>
|
||||
{
|
||||
public static bool ShouldEnter(Token token)
|
||||
{
|
||||
return token.Match(TokenType.Keyword, TokenLexicon.KnownKeywords.PASS);
|
||||
}
|
||||
|
||||
public static PassSyntax Parse(TokenStreamSlice stream)
|
||||
{
|
||||
var pass = new PassSyntax();
|
||||
|
||||
stream.Expect(TokenType.Keyword);
|
||||
pass.name = stream.Expect(TokenType.StringLiteral);
|
||||
stream.Expect(TokenType.LBrace);
|
||||
|
||||
var bodyStream = stream.Slice(stream.Remaining - 1);
|
||||
while (bodyStream.TryPeek(out var nextToken))
|
||||
{
|
||||
if (DefinesBlock.ShouldEnter(nextToken))
|
||||
{
|
||||
pass.defines = DefinesBlock.Parse(bodyStream.SliceNextBlock());
|
||||
}
|
||||
else if (KeywordsBlock.ShouldEnter(nextToken))
|
||||
{
|
||||
pass.keywords = KeywordsBlock.Parse(bodyStream.SliceNextBlock());
|
||||
}
|
||||
else if (PipelineBlock.ShouldEnter(nextToken))
|
||||
{
|
||||
pass.localPipeline = PipelineBlock.Parse(bodyStream.SliceNextBlock());
|
||||
}
|
||||
else if (IncludesBlock.ShouldEnter(nextToken))
|
||||
{
|
||||
pass.includes = IncludesBlock.Parse(bodyStream.SliceNextBlock());
|
||||
}
|
||||
else if (nextToken.Match(TokenType.Identifier))
|
||||
{
|
||||
var func = ParseUtility.ParseFunction(ref bodyStream, TokenType.StringLiteral);
|
||||
|
||||
pass.functionCalls ??= new();
|
||||
pass.functionCalls.Add(func);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Unexpected token '{nextToken}' in pass body.");
|
||||
}
|
||||
}
|
||||
|
||||
stream.Expect(TokenType.RBrace);
|
||||
|
||||
return pass;
|
||||
}
|
||||
|
||||
public static PassSemantic? SemanticAnalysis(PassSyntax? syntax, List<DSLShaderError> errors)
|
||||
{
|
||||
if (syntax == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var semantic = new PassSemantic
|
||||
{
|
||||
name = syntax.name.lexeme,
|
||||
defines = DefinesBlock.SemanticAnalysis(syntax.defines, errors),
|
||||
keywords = KeywordsBlock.SemanticAnalysis(syntax.keywords, errors),
|
||||
localPipeline = PipelineBlock.SemanticAnalysis(syntax.localPipeline, errors),
|
||||
};
|
||||
|
||||
if (syntax.functionCalls != null)
|
||||
{
|
||||
foreach (var func in syntax.functionCalls)
|
||||
{
|
||||
switch (func.name.lexeme)
|
||||
{
|
||||
case TokenLexicon.KnownFunctions.TASK_SHADER:
|
||||
AnalysisShaderEntry(errors, func, ref semantic.taskShader);
|
||||
break;
|
||||
|
||||
case TokenLexicon.KnownFunctions.MESH_SHADER:
|
||||
AnalysisShaderEntry(errors, func, ref semantic.meshShader);
|
||||
break;
|
||||
|
||||
case TokenLexicon.KnownFunctions.PIXEL_SHADER:
|
||||
AnalysisShaderEntry(errors, func, ref semantic.pixelShader);
|
||||
break;
|
||||
|
||||
default:
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Unknown function '{func.name.lexeme}' in pass {syntax.name.lexeme}.",
|
||||
line = func.name.line,
|
||||
column = func.name.column
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (semantic.meshShader.shader == null || semantic.pixelShader.shader == null)
|
||||
{
|
||||
// TODO: Inheritance from base pass.
|
||||
// TODO: Add mesh shader support.
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Pass {syntax.name.lexeme} must contain a mesh shader (ms) and a pixel shader (ps) declaration.",
|
||||
line = syntax.name.line,
|
||||
column = syntax.name.column
|
||||
});
|
||||
}
|
||||
|
||||
return semantic;
|
||||
}
|
||||
|
||||
private static void AnalysisShaderEntry(List<DSLShaderError> errors, FunctionCallDeclaration func, ref ShaderEntryPoint shaderEntryPoint)
|
||||
{
|
||||
if (func.arguments?.Count != 2)
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = "Shader declaration requires exactly two arguments: (shaderPath, entryPoint).",
|
||||
line = func.name.line,
|
||||
column = func.name.column
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
shaderEntryPoint = new ShaderEntryPoint
|
||||
{
|
||||
shader = func.arguments[0].lexeme,
|
||||
entry = func.arguments[1].lexeme
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
143
Ghost.DSL/ShaderCompiler/Parser/PipelineBlock.cs
Normal file
143
Ghost.DSL/ShaderCompiler/Parser/PipelineBlock.cs
Normal file
@@ -0,0 +1,143 @@
|
||||
using Ghost.Core.Graphics;
|
||||
|
||||
namespace Ghost.DSL.ShaderCompiler.Parser;
|
||||
|
||||
internal class PipelineBlock : IBlockParser<PipelineSyntax, PipelineSemantic>
|
||||
{
|
||||
public static bool ShouldEnter(Token token)
|
||||
{
|
||||
return token.Match(TokenType.Keyword, TokenLexicon.KnownKeywords.PIPELINE);
|
||||
}
|
||||
|
||||
public static PipelineSyntax Parse(TokenStreamSlice stream)
|
||||
{
|
||||
stream.Expect(TokenType.Keyword);
|
||||
stream.Expect(TokenType.LBrace);
|
||||
|
||||
var pipeline = new PipelineSyntax();
|
||||
|
||||
var bodyStream = stream.Slice(stream.Remaining - 1);
|
||||
while (bodyStream.HasMore)
|
||||
{
|
||||
var stateToken = bodyStream.Expect(TokenType.Identifier);
|
||||
bodyStream.Expect(TokenType.Equals);
|
||||
var valueToken = bodyStream.Expect(TokenType.Identifier | TokenType.Number);
|
||||
|
||||
pipeline.values ??= new();
|
||||
pipeline.values.Add(new ValueDeclaration
|
||||
{
|
||||
name = stateToken,
|
||||
value = valueToken
|
||||
});
|
||||
|
||||
bodyStream.Expect(TokenType.Semicolon);
|
||||
}
|
||||
|
||||
stream.Expect(TokenType.RBrace);
|
||||
|
||||
return pipeline;
|
||||
}
|
||||
|
||||
public static PipelineSemantic? SemanticAnalysis(PipelineSyntax? syntax, List<DSLShaderError> errors)
|
||||
{
|
||||
if (syntax == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var semantic = new PipelineSemantic();
|
||||
if (syntax.values != null)
|
||||
{
|
||||
foreach (var valueDecl in syntax.values)
|
||||
{
|
||||
switch (valueDecl.name.lexeme)
|
||||
{
|
||||
case TokenLexicon.KnownPipelineProperties.ZTEST:
|
||||
if (Enum.TryParse<ZTest>(valueDecl.value.lexeme, true, out var zTest))
|
||||
{
|
||||
semantic.zTest = zTest;
|
||||
}
|
||||
else
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Invalid ZTest option: {valueDecl.value.lexeme}",
|
||||
line = valueDecl.value.line,
|
||||
column = valueDecl.value.column
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case TokenLexicon.KnownPipelineProperties.ZWRITE:
|
||||
if (Enum.TryParse<ZWrite>(valueDecl.value.lexeme, true, out var zWrite))
|
||||
{
|
||||
semantic.zWrite = zWrite;
|
||||
}
|
||||
else
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Invalid ZWrite option: {valueDecl.value.lexeme}",
|
||||
line = valueDecl.value.line,
|
||||
column = valueDecl.value.column
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case TokenLexicon.KnownPipelineProperties.CULL:
|
||||
if (Enum.TryParse<Cull>(valueDecl.value.lexeme, true, out var cull))
|
||||
{
|
||||
semantic.cull = cull;
|
||||
}
|
||||
else
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Invalid Cull option: {valueDecl.value.lexeme}",
|
||||
line = valueDecl.value.line,
|
||||
column = valueDecl.value.column
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case TokenLexicon.KnownPipelineProperties.BLEND:
|
||||
if (Enum.TryParse<Blend>(valueDecl.value.lexeme, true, out var blend))
|
||||
{
|
||||
semantic.blend = blend;
|
||||
}
|
||||
else
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Invalid Blend option: {valueDecl.value.lexeme}",
|
||||
line = valueDecl.value.line,
|
||||
column = valueDecl.value.column
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case TokenLexicon.KnownPipelineProperties.COLORMASK:
|
||||
if (Enum.TryParse<ColorWriteMask>(valueDecl.value.lexeme, true, out var colorMask))
|
||||
{
|
||||
semantic.colorMask = colorMask;
|
||||
}
|
||||
else
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Invalid Color Mask value: {valueDecl.value.lexeme}",
|
||||
line = valueDecl.value.line,
|
||||
column = valueDecl.value.column
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return semantic;
|
||||
}
|
||||
}
|
||||
471
Ghost.DSL/ShaderCompiler/Parser/PropertiesBlock.cs
Normal file
471
Ghost.DSL/ShaderCompiler/Parser/PropertiesBlock.cs
Normal file
@@ -0,0 +1,471 @@
|
||||
using Ghost.Core.Graphics;
|
||||
using Misaki.HighPerformance.Mathematics;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Ghost.DSL.ShaderCompiler.Parser;
|
||||
|
||||
internal class PropertiesBlock : IBlockParser<PropertiesSyntax, List<PropertySemantic>>
|
||||
{
|
||||
private delegate object? PropertyValueBuilder(List<Token> tokens, List<DSLShaderError> errors);
|
||||
|
||||
private sealed record PropTypeInfo(int ArgCount, TokenType ArgTokenType, PropertyValueBuilder? Builder);
|
||||
|
||||
private static readonly Dictionary<ShaderPropertyType, PropTypeInfo> s_propTypeInfo = new()
|
||||
{
|
||||
// Floats
|
||||
[ShaderPropertyType.Float] = new(1, TokenType.Number, (syntax, errors) => ParseFloatValue(syntax[0], errors)),
|
||||
[ShaderPropertyType.Float2] = new(2, TokenType.Number, (syntax, errors) => new float2(
|
||||
ParseFloatValue(syntax[0], errors),
|
||||
ParseFloatValue(syntax[1], errors))),
|
||||
[ShaderPropertyType.Float3] = new(3, TokenType.Number, (syntax, errors) => new float3(
|
||||
ParseFloatValue(syntax[0], errors),
|
||||
ParseFloatValue(syntax[1], errors),
|
||||
ParseFloatValue(syntax[2], errors))),
|
||||
[ShaderPropertyType.Float4] = new(4, TokenType.Number, (syntax, errors) => new float4(
|
||||
ParseFloatValue(syntax[0], errors),
|
||||
ParseFloatValue(syntax[1], errors),
|
||||
ParseFloatValue(syntax[2], errors),
|
||||
ParseFloatValue(syntax[3], errors))),
|
||||
|
||||
// Ints
|
||||
[ShaderPropertyType.Int] = new(1, TokenType.Number, (syntax, errors) => ParseIntValue(syntax[0], errors)),
|
||||
[ShaderPropertyType.Int2] = new(2, TokenType.Number, (syntax, errors) => new int2(
|
||||
ParseIntValue(syntax[0], errors),
|
||||
ParseIntValue(syntax[1], errors))),
|
||||
[ShaderPropertyType.Int3] = new(3, TokenType.Number, (syntax, errors) => new int3(
|
||||
ParseIntValue(syntax[0], errors),
|
||||
ParseIntValue(syntax[1], errors),
|
||||
ParseIntValue(syntax[2], errors))),
|
||||
[ShaderPropertyType.Int4] = new(4, TokenType.Number, (syntax, errors) => new int4(
|
||||
ParseIntValue(syntax[0], errors),
|
||||
ParseIntValue(syntax[1], errors),
|
||||
ParseIntValue(syntax[2], errors),
|
||||
ParseIntValue(syntax[3], errors))),
|
||||
|
||||
// UInts
|
||||
[ShaderPropertyType.UInt] = new(1, TokenType.Number, (syntax, errors) => ParseUIntValue(syntax[0], errors)),
|
||||
[ShaderPropertyType.UInt2] = new(2, TokenType.Number, (syntax, errors) => new uint2(
|
||||
ParseUIntValue(syntax[0], errors),
|
||||
ParseUIntValue(syntax[1], errors))),
|
||||
[ShaderPropertyType.UInt3] = new(3, TokenType.Number, (syntax, errors) => new uint3(
|
||||
ParseUIntValue(syntax[0], errors),
|
||||
ParseUIntValue(syntax[1], errors),
|
||||
ParseUIntValue(syntax[2], errors))),
|
||||
[ShaderPropertyType.UInt4] = new(4, TokenType.Number, (syntax, errors) => new uint4(
|
||||
ParseUIntValue(syntax[0], errors),
|
||||
ParseUIntValue(syntax[1], errors),
|
||||
ParseUIntValue(syntax[2], errors),
|
||||
ParseUIntValue(syntax[3], errors))),
|
||||
|
||||
// Bools (numbers 0/1)
|
||||
[ShaderPropertyType.Bool] = new(1, TokenType.Number, (syntax, errors) => ParseBoolValue(syntax[0], errors)),
|
||||
[ShaderPropertyType.Bool2] = new(2, TokenType.Number, (syntax, errors) => new bool2(
|
||||
ParseBoolValue(syntax[0], errors),
|
||||
ParseBoolValue(syntax[1], errors))),
|
||||
[ShaderPropertyType.Bool3] = new(3, TokenType.Number, (syntax, errors) => new bool3(
|
||||
ParseBoolValue(syntax[0], errors),
|
||||
ParseBoolValue(syntax[1], errors),
|
||||
ParseBoolValue(syntax[2], errors))),
|
||||
[ShaderPropertyType.Bool4] = new(4, TokenType.Number, (syntax, errors) => new bool4(
|
||||
ParseBoolValue(syntax[0], errors),
|
||||
ParseBoolValue(syntax[1], errors),
|
||||
ParseBoolValue(syntax[2], errors),
|
||||
ParseBoolValue(syntax[3], errors))),
|
||||
|
||||
// Textures (single identifier argument)
|
||||
[ShaderPropertyType.Texture2D] = new(1, TokenType.Identifier, (syntax, errors) => ParseTextureDefault(syntax[0], errors)),
|
||||
[ShaderPropertyType.Texture3D] = new(1, TokenType.Identifier, (syntax, errors) => ParseTextureDefault(syntax[0], errors)),
|
||||
[ShaderPropertyType.TextureCube] = new(1, TokenType.Identifier, (syntax, errors) => ParseTextureDefault(syntax[0], errors)),
|
||||
};
|
||||
|
||||
private static float ParseFloatValue(Token token, List<DSLShaderError> errors)
|
||||
{
|
||||
if (!float.TryParse(token.lexeme, CultureInfo.InvariantCulture, out var result))
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Failed to parse float value '{token.lexeme}'.",
|
||||
line = token.line,
|
||||
column = token.column
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int ParseIntValue(Token token, List<DSLShaderError> errors)
|
||||
{
|
||||
if (!int.TryParse(token.lexeme, CultureInfo.InvariantCulture, out var result))
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Failed to parse int value '{token.lexeme}'.",
|
||||
line = token.line,
|
||||
column = token.column
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static uint ParseUIntValue(Token token, List<DSLShaderError> errors)
|
||||
{
|
||||
if (!uint.TryParse(token.lexeme, CultureInfo.InvariantCulture, out var result))
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Failed to parse uint value '{token.lexeme}'.",
|
||||
line = token.line,
|
||||
column = token.column
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static bool ParseBoolValue(Token token, List<DSLShaderError> errors)
|
||||
{
|
||||
if (!bool.TryParse(token.lexeme, out var result))
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Failed to parse bool value '{token.lexeme}'.",
|
||||
line = token.line,
|
||||
column = token.column
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static string ParseTextureDefault(Token token, List<DSLShaderError> errors)
|
||||
{
|
||||
if (!TokenLexicon.IsTextureDefaultValue(token.lexeme))
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Texture default value '{token.lexeme}' is not valid.",
|
||||
line = token.line,
|
||||
column = token.column
|
||||
});
|
||||
}
|
||||
|
||||
return token.lexeme;
|
||||
}
|
||||
|
||||
public static bool ShouldEnter(Token token)
|
||||
{
|
||||
return token.Match(TokenType.Keyword, TokenLexicon.KnownKeywords.PROPERTIES);
|
||||
}
|
||||
|
||||
private static ShaderPropertyType FromString(string type)
|
||||
{
|
||||
return type.ToLower() switch
|
||||
{
|
||||
TokenLexicon.KnownTypes.FLOAT => ShaderPropertyType.Float,
|
||||
TokenLexicon.KnownTypes.FLOAT2 => ShaderPropertyType.Float2,
|
||||
TokenLexicon.KnownTypes.FLOAT3 => ShaderPropertyType.Float3,
|
||||
TokenLexicon.KnownTypes.FLOAT4 => ShaderPropertyType.Float4,
|
||||
TokenLexicon.KnownTypes.FLOAT4X4 => ShaderPropertyType.Float4x4,
|
||||
TokenLexicon.KnownTypes.INT => ShaderPropertyType.Int,
|
||||
TokenLexicon.KnownTypes.INT2 => ShaderPropertyType.Int2,
|
||||
TokenLexicon.KnownTypes.INT3 => ShaderPropertyType.Int3,
|
||||
TokenLexicon.KnownTypes.INT4 => ShaderPropertyType.Int4,
|
||||
TokenLexicon.KnownTypes.UINT => ShaderPropertyType.UInt,
|
||||
TokenLexicon.KnownTypes.UINT2 => ShaderPropertyType.UInt2,
|
||||
TokenLexicon.KnownTypes.UINT3 => ShaderPropertyType.UInt3,
|
||||
TokenLexicon.KnownTypes.UINT4 => ShaderPropertyType.UInt4,
|
||||
TokenLexicon.KnownTypes.BOOL => ShaderPropertyType.Bool,
|
||||
TokenLexicon.KnownTypes.BOOL2 => ShaderPropertyType.Bool2,
|
||||
TokenLexicon.KnownTypes.BOOL3 => ShaderPropertyType.Bool3,
|
||||
TokenLexicon.KnownTypes.BOOL4 => ShaderPropertyType.Bool4,
|
||||
TokenLexicon.KnownTypes.TEXTURE2D => ShaderPropertyType.Texture2D,
|
||||
TokenLexicon.KnownTypes.TEXTURE3D => ShaderPropertyType.Texture3D,
|
||||
TokenLexicon.KnownTypes.TEXTURECUBE => ShaderPropertyType.TextureCube,
|
||||
TokenLexicon.KnownTypes.TEXTURECUBE_ARRAY => ShaderPropertyType.TextureCubeArray,
|
||||
TokenLexicon.KnownTypes.TEXTURE2D_ARRAY => ShaderPropertyType.Texture2DArray,
|
||||
TokenLexicon.KnownTypes.SAMPLER => ShaderPropertyType.Sampler,
|
||||
_ => ShaderPropertyType.None,
|
||||
};
|
||||
}
|
||||
|
||||
public static PropertiesSyntax Parse(TokenStreamSlice stream)
|
||||
{
|
||||
stream.Expect(TokenType.Keyword);
|
||||
stream.Expect(TokenType.LBrace);
|
||||
|
||||
var syntax = new PropertiesSyntax();
|
||||
|
||||
var bodyStream = stream.Slice(stream.Remaining - 1);
|
||||
while (bodyStream.HasMore)
|
||||
{
|
||||
var shaderProperty = new PropertyDeclaration();
|
||||
if (bodyStream.Match(TokenType.Keyword, TokenLexicon.KnownKeywords.GLOBAL)
|
||||
|| bodyStream.Match(TokenType.Keyword, TokenLexicon.KnownKeywords.LOCAL))
|
||||
{
|
||||
var scopeToken = bodyStream.Consume();
|
||||
shaderProperty.scope = scopeToken;
|
||||
}
|
||||
|
||||
var typeToken = bodyStream.Expect(TokenType.Identifier);
|
||||
var nameToken = bodyStream.Expect(TokenType.Identifier);
|
||||
|
||||
shaderProperty.type = typeToken;
|
||||
shaderProperty.name = nameToken;
|
||||
|
||||
var nextToken = bodyStream.Consume();
|
||||
switch (nextToken.type)
|
||||
{
|
||||
case TokenType.Equals:
|
||||
{
|
||||
var constructorTypeToken = bodyStream.Expect(TokenType.Identifier);
|
||||
var args = ParseUtility.ParseFunctionArguments(ref bodyStream, TokenType.Identifier | TokenType.Number);
|
||||
shaderProperty.propertyConstructor = new FunctionCallDeclaration
|
||||
{
|
||||
name = constructorTypeToken,
|
||||
arguments = args
|
||||
};
|
||||
|
||||
bodyStream.Expect(TokenType.Semicolon);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case TokenType.Semicolon:
|
||||
break;
|
||||
default:
|
||||
throw new Exception($"Unexpected token '{nextToken.lexeme}' in property declaration.");
|
||||
}
|
||||
|
||||
syntax.properties ??= new();
|
||||
syntax.properties.Add(shaderProperty);
|
||||
}
|
||||
|
||||
stream.Expect(TokenType.RBrace);
|
||||
|
||||
return syntax;
|
||||
}
|
||||
|
||||
public static List<PropertySemantic>? SemanticAnalysis(PropertiesSyntax? syntax, List<DSLShaderError> errors)
|
||||
{
|
||||
if (syntax == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var models = new List<PropertySemantic>();
|
||||
var usedPropertyNames = new HashSet<string>();
|
||||
|
||||
if (syntax.properties != null)
|
||||
{
|
||||
foreach (var property in syntax.properties)
|
||||
{
|
||||
var model = new PropertySemantic
|
||||
{
|
||||
scope = property.scope.lexeme switch
|
||||
{
|
||||
TokenLexicon.KnownKeywords.GLOBAL => PropertyScope.Global,
|
||||
TokenLexicon.KnownKeywords.LOCAL => PropertyScope.Local,
|
||||
_ => PropertyScope.Local,
|
||||
}
|
||||
};
|
||||
|
||||
var flowControl = ValidatePropertyType(errors, property, model);
|
||||
if (!flowControl)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
flowControl = ValidatePropertyName(errors, usedPropertyNames, property, model);
|
||||
if (!flowControl)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (property.propertyConstructor != null)
|
||||
{
|
||||
flowControl = ValidatePropertyConstructor(errors, property, model);
|
||||
if (!flowControl)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
usedPropertyNames.Add(property.name.lexeme);
|
||||
models.Add(model);
|
||||
}
|
||||
}
|
||||
|
||||
return models;
|
||||
}
|
||||
|
||||
private static bool ValidatePropertyType(List<DSLShaderError> errors, PropertyDeclaration property, PropertySemantic model)
|
||||
{
|
||||
if (!TokenLexicon.IsType(property.type.lexeme))
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Shader property type '{property.type.lexeme}' is not a valid type.",
|
||||
line = property.type.line,
|
||||
column = property.type.column
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
model.type = FromString(property.type.lexeme);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool ValidatePropertyName(List<DSLShaderError> errors, HashSet<string> usedPropertyNames, PropertyDeclaration property, PropertySemantic model)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(property.name.lexeme))
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = "Shader property has an empty name.",
|
||||
line = property.name.line,
|
||||
column = property.name.column
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
else if (usedPropertyNames.Contains(property.name.lexeme))
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Shader property name '{property.name.lexeme}' is duplicated.",
|
||||
line = property.name.line,
|
||||
column = property.name.column
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
model.name = property.name.lexeme;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool ValidatePropertyConstructor(List<DSLShaderError> errors, PropertyDeclaration property, PropertySemantic model)
|
||||
{
|
||||
var constructor = property.propertyConstructor;
|
||||
if (!constructor.HasValue)
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = "Shader property constructor is null.",
|
||||
line = property.name.line,
|
||||
column = property.name.column
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
var constructorValue = constructor.Value;
|
||||
if (string.IsNullOrWhiteSpace(constructorValue.name.lexeme))
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = "Shader property constructor has an empty name.",
|
||||
line = constructorValue.name.line,
|
||||
column = constructorValue.name.column
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (constructorValue.name.lexeme != property.type.lexeme)
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Shader property constructor name '{constructorValue.name.lexeme}' does not match property type '{property.type.lexeme}'.",
|
||||
line = constructorValue.name.line,
|
||||
column = constructorValue.name.column
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!s_propTypeInfo.TryGetValue(model.type, out var info))
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"No constructor metadata registered for property type '{model.type}'.",
|
||||
line = constructorValue.name.line,
|
||||
column = constructorValue.name.column
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Count check
|
||||
if (constructorValue.arguments == null)
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = "Shader property constructor arguments are null.",
|
||||
line = constructorValue.name.line,
|
||||
column = constructorValue.name.column
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (constructorValue.arguments.Count != info.ArgCount)
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Shader property constructor for type '{property.type.lexeme}' expects {info.ArgCount} argument(s), but got {constructorValue.arguments.Count}.",
|
||||
line = constructorValue.name.line,
|
||||
column = constructorValue.name.column
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Type check (uniform requirement for all args)
|
||||
var hasError = false;
|
||||
for (var i = 0; i < constructorValue.arguments.Count; i++)
|
||||
{
|
||||
var arg = constructorValue.arguments[i];
|
||||
if (!arg.Match(info.ArgTokenType))
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Shader property constructor argument {i} expects token kind '{info.ArgTokenType}', but got '{arg.type}'.",
|
||||
line = arg.line,
|
||||
column = arg.column
|
||||
});
|
||||
|
||||
hasError = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasError)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Build default Value if we have a builder (textures currently null / TODO)
|
||||
if (info.Builder != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
model.defaultValue = info.Builder(constructorValue.arguments, errors);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Failed to construct default value for property '{property.name.lexeme}': {ex.Message}",
|
||||
line = constructorValue.name.line,
|
||||
column = constructorValue.name.column
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
114
Ghost.DSL/ShaderCompiler/Parser/ShaderBlock.cs
Normal file
114
Ghost.DSL/ShaderCompiler/Parser/ShaderBlock.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
namespace Ghost.DSL.ShaderCompiler.Parser;
|
||||
|
||||
internal class ShaderBlock : IBlockParser<DSLShaderSyntax, DSLShaderSemantics>
|
||||
{
|
||||
public static bool ShouldEnter(Token token)
|
||||
{
|
||||
return token.Match(TokenType.Keyword, TokenLexicon.KnownKeywords.SHADER);
|
||||
}
|
||||
|
||||
public static DSLShaderSyntax Parse(TokenStreamSlice stream)
|
||||
{
|
||||
var shader = new DSLShaderSyntax();
|
||||
|
||||
stream.Expect(TokenType.Keyword);
|
||||
shader.name = stream.Expect(TokenType.StringLiteral);
|
||||
stream.Expect(TokenType.LBrace);
|
||||
|
||||
var bodyStream = stream.Slice(stream.Remaining - 1);
|
||||
while (bodyStream.TryPeek(out var nextToken))
|
||||
{
|
||||
if (PropertiesBlock.ShouldEnter(nextToken))
|
||||
{
|
||||
shader.properties = PropertiesBlock.Parse(bodyStream.SliceNextBlock());
|
||||
}
|
||||
else if (PipelineBlock.ShouldEnter(nextToken))
|
||||
{
|
||||
shader.pipeline = PipelineBlock.Parse(bodyStream.SliceNextBlock());
|
||||
}
|
||||
else if (PassBlock.ShouldEnter(nextToken))
|
||||
{
|
||||
shader.passes ??= new();
|
||||
shader.passes.Add(PassBlock.Parse(bodyStream.SliceNextBlock()));
|
||||
}
|
||||
else if (nextToken.Match(TokenType.Identifier))
|
||||
{
|
||||
var func = ParseUtility.ParseFunction(ref bodyStream, TokenType.StringLiteral | TokenType.Number | TokenType.Identifier);
|
||||
|
||||
shader.functionCalls ??= new();
|
||||
shader.functionCalls.Add(func);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Unexpected token '{nextToken}' in shader body.");
|
||||
}
|
||||
}
|
||||
|
||||
stream.Expect(TokenType.RBrace);
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
public static DSLShaderSemantics? SemanticAnalysis(DSLShaderSyntax? syntax, List<DSLShaderError> errors)
|
||||
{
|
||||
if (syntax == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var shaderModel = new DSLShaderSemantics
|
||||
{
|
||||
name = syntax.name.lexeme,
|
||||
properties = PropertiesBlock.SemanticAnalysis(syntax.properties, errors),
|
||||
pipeline = PipelineBlock.SemanticAnalysis(syntax.pipeline, errors)
|
||||
};
|
||||
|
||||
if (syntax.passes != null)
|
||||
{
|
||||
foreach (var passSyntax in syntax.passes)
|
||||
{
|
||||
var passModel = PassBlock.SemanticAnalysis(passSyntax, errors);
|
||||
if (passModel != null)
|
||||
{
|
||||
shaderModel.passes ??= new();
|
||||
shaderModel.passes.Add(passModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (syntax.functionCalls != null)
|
||||
{
|
||||
foreach (var func in syntax.functionCalls)
|
||||
{
|
||||
switch (func.name.lexeme)
|
||||
{
|
||||
case TokenLexicon.KnownFunctions.FALLBACK:
|
||||
if (func.arguments == null || func.arguments.Count != 1)
|
||||
{
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = "Fallback declaration requires exactly one arguments: (fallback shader name).",
|
||||
line = func.name.line,
|
||||
column = func.name.column
|
||||
});
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
shaderModel.fallback = func.arguments[0].lexeme;
|
||||
break;
|
||||
default:
|
||||
errors.Add(new DSLShaderError
|
||||
{
|
||||
message = $"Unknown function '{func.name.lexeme}' in shader.",
|
||||
line = func.name.line,
|
||||
column = func.name.column
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return shaderModel;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user