forked from Misaki/GhostEngine
Refactoring Rendering backend
This commit is contained in:
29
Ghost.Shader/ParserBlock/DefinesBlock.cs
Normal file
29
Ghost.Shader/ParserBlock/DefinesBlock.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
namespace Ghost.Shader.ParserBlock;
|
||||
|
||||
internal class DefinesBlock : IBlockParser<List<string>>
|
||||
{
|
||||
public static bool ShouldEnter(Token token)
|
||||
{
|
||||
return token.Match(TokenType.Keyword, TokenLexicon.KnownKeywords.DEFINES);
|
||||
}
|
||||
|
||||
public static List<string> Parse(TokenStreamSlice stream)
|
||||
{
|
||||
stream.Expect(TokenType.Keyword);
|
||||
stream.Expect(TokenType.LBrace);
|
||||
|
||||
var defines = new List<string>();
|
||||
|
||||
var bodyStream = stream.Slice(stream.Remaining - 1);
|
||||
while (bodyStream.HasMore)
|
||||
{
|
||||
var defineToken = bodyStream.Expect(TokenType.Identifier);
|
||||
defines.Add(defineToken.lexeme);
|
||||
bodyStream.Expect(TokenType.Semicolon);
|
||||
}
|
||||
|
||||
stream.Expect(TokenType.RBrace);
|
||||
|
||||
return defines;
|
||||
}
|
||||
}
|
||||
13
Ghost.Shader/ParserBlock/IBlockParser.cs
Normal file
13
Ghost.Shader/ParserBlock/IBlockParser.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace Ghost.Shader.ParserBlock;
|
||||
|
||||
internal interface IBlockParser<T>
|
||||
{
|
||||
public static abstract bool ShouldEnter(Token token);
|
||||
|
||||
public static abstract T Parse(TokenStreamSlice ts);
|
||||
}
|
||||
|
||||
internal interface IBlockParser<T, U> : IBlockParser<T>
|
||||
{
|
||||
public U SemanticAnalysis(T syntax, List<ShaderError> errors);
|
||||
}
|
||||
29
Ghost.Shader/ParserBlock/IncludesBlock.cs
Normal file
29
Ghost.Shader/ParserBlock/IncludesBlock.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
namespace Ghost.Shader.ParserBlock;
|
||||
|
||||
internal class IncludesBlock : IBlockParser<List<string>>
|
||||
{
|
||||
public static bool ShouldEnter(Token token)
|
||||
{
|
||||
return token.Match(TokenType.Keyword, TokenLexicon.KnownKeywords.INCLUDES);
|
||||
}
|
||||
|
||||
public static List<string> Parse(TokenStreamSlice stream)
|
||||
{
|
||||
stream.Expect(TokenType.Keyword);
|
||||
stream.Expect(TokenType.LBrace);
|
||||
|
||||
var includes = new List<string>();
|
||||
|
||||
var bodyStream = stream.Slice(stream.Remaining - 1);
|
||||
while (bodyStream.HasMore)
|
||||
{
|
||||
var includeToken = bodyStream.Expect(TokenType.StringLiteral);
|
||||
includes.Add(includeToken.lexeme);
|
||||
bodyStream.Expect(TokenType.Semicolon);
|
||||
}
|
||||
|
||||
stream.Expect(TokenType.RBrace);
|
||||
|
||||
return includes;
|
||||
}
|
||||
}
|
||||
30
Ghost.Shader/ParserBlock/KeywordsBlock.cs
Normal file
30
Ghost.Shader/ParserBlock/KeywordsBlock.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
namespace Ghost.Shader.ParserBlock;
|
||||
|
||||
internal class KeywordsBlock : IBlockParser<List<FunctionCall>>
|
||||
{
|
||||
public static bool ShouldEnter(Token token)
|
||||
{
|
||||
return token.Match(TokenType.Keyword, TokenLexicon.KnownKeywords.KEYWORDS);
|
||||
}
|
||||
|
||||
public static List<FunctionCall> Parse(TokenStreamSlice stream)
|
||||
{
|
||||
stream.Expect(TokenType.Keyword);
|
||||
stream.Expect(TokenType.LBrace);
|
||||
|
||||
var keywords = new List<FunctionCall>();
|
||||
|
||||
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 FunctionCall { name = keywordToken, arguments = args });
|
||||
bodyStream.Expect(TokenType.Semicolon);
|
||||
}
|
||||
|
||||
stream.Expect(TokenType.RBrace);
|
||||
|
||||
return keywords;
|
||||
}
|
||||
}
|
||||
63
Ghost.Shader/ParserBlock/PassBlock.cs
Normal file
63
Ghost.Shader/ParserBlock/PassBlock.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
namespace Ghost.Shader.ParserBlock;
|
||||
|
||||
internal class PassBlock : IBlockParser<ShaderPassSyntax>
|
||||
{
|
||||
public static bool ShouldEnter(Token token)
|
||||
{
|
||||
return token.Match(TokenType.Keyword, TokenLexicon.KnownKeywords.PASS);
|
||||
}
|
||||
|
||||
public static ShaderPassSyntax Parse(TokenStreamSlice stream)
|
||||
{
|
||||
var pass = new ShaderPassSyntax();
|
||||
|
||||
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 (IncludesBlock.ShouldEnter(nextToken))
|
||||
{
|
||||
pass.includes = IncludesBlock.Parse(bodyStream.SliceNextBlock());
|
||||
}
|
||||
else if (KeywordsBlock.ShouldEnter(nextToken))
|
||||
{
|
||||
pass.keywords = KeywordsBlock.Parse(bodyStream.SliceNextBlock());
|
||||
}
|
||||
else if (PipelineBlock.ShouldEnter(nextToken))
|
||||
{
|
||||
pass.overridePipeline = PipelineBlock.Parse(bodyStream.SliceNextBlock());
|
||||
}
|
||||
else if (nextToken.Match(TokenType.Identifier, "vs"))
|
||||
{
|
||||
bodyStream.Expect(TokenType.Identifier, "vs");
|
||||
var vsArgs = ParseUtility.ParseFunctionArguments(ref bodyStream, TokenType.StringLiteral);
|
||||
pass.vertexShader = vsArgs[0];
|
||||
pass.vertexEntry = vsArgs[1];
|
||||
bodyStream.Expect(TokenType.Semicolon);
|
||||
}
|
||||
else if (nextToken.Match(TokenType.Identifier, "ps"))
|
||||
{
|
||||
bodyStream.Expect(TokenType.Identifier, "ps");
|
||||
var psArgs = ParseUtility.ParseFunctionArguments(ref bodyStream, TokenType.StringLiteral);
|
||||
pass.pixelShader = psArgs[0];
|
||||
pass.pixelEntry = psArgs[1];
|
||||
bodyStream.Expect(TokenType.Semicolon);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Unexpected token '{nextToken.lexeme}' in pass body.");
|
||||
}
|
||||
}
|
||||
|
||||
stream.Expect(TokenType.RBrace);
|
||||
|
||||
return pass;
|
||||
}
|
||||
}
|
||||
217
Ghost.Shader/ParserBlock/PipelineBlock.cs
Normal file
217
Ghost.Shader/ParserBlock/PipelineBlock.cs
Normal file
@@ -0,0 +1,217 @@
|
||||
using static Ghost.Shader.TokenLexicon;
|
||||
|
||||
namespace Ghost.Shader.ParserBlock;
|
||||
|
||||
internal class PipelineBlock : IBlockParser<PipelineStateSyntax, PipelineStateModel>
|
||||
{
|
||||
public static bool ShouldEnter(Token token)
|
||||
{
|
||||
return token.Match(TokenType.Keyword, TokenLexicon.KnownKeywords.PIPELINE);
|
||||
}
|
||||
|
||||
public static PipelineStateSyntax Parse(TokenStreamSlice stream)
|
||||
{
|
||||
stream.Expect(TokenType.Keyword);
|
||||
stream.Expect(TokenType.LBrace);
|
||||
|
||||
var pipeline = new PipelineStateSyntax();
|
||||
|
||||
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);
|
||||
|
||||
switch (stateToken.lexeme)
|
||||
{
|
||||
case KnownPipelineProperties.ZTEST:
|
||||
pipeline.zTest = valueToken;
|
||||
break;
|
||||
case KnownPipelineProperties.ZWRITE:
|
||||
pipeline.zWrite = valueToken;
|
||||
break;
|
||||
case KnownPipelineProperties.CULL:
|
||||
pipeline.cull = valueToken;
|
||||
break;
|
||||
case KnownPipelineProperties.BLEND:
|
||||
pipeline.blend = valueToken;
|
||||
break;
|
||||
case KnownPipelineProperties.COLORMASK:
|
||||
pipeline.colorMask = valueToken;
|
||||
break;
|
||||
default:
|
||||
throw new InvalidDataException($"Unknown pipeline state: {stateToken.lexeme}");
|
||||
}
|
||||
|
||||
bodyStream.Expect(TokenType.Semicolon);
|
||||
}
|
||||
|
||||
stream.Expect(TokenType.RBrace);
|
||||
|
||||
return pipeline;
|
||||
}
|
||||
|
||||
public PipelineStateModel SemanticAnalysis(PipelineStateSyntax syntax, List<ShaderError> errors)
|
||||
{
|
||||
var model = new PipelineStateModel();
|
||||
|
||||
// ZTest
|
||||
if (!syntax.zTest.Match(TokenType.None))
|
||||
{
|
||||
switch (syntax.zTest.lexeme)
|
||||
{
|
||||
case "disable":
|
||||
model.zTest = ZTestOptions.Disabled;
|
||||
break;
|
||||
case "less":
|
||||
model.zTest = ZTestOptions.Less;
|
||||
break;
|
||||
case "less_equal":
|
||||
model.zTest = ZTestOptions.LessEqual;
|
||||
break;
|
||||
case "equal":
|
||||
model.zTest = ZTestOptions.Equal;
|
||||
break;
|
||||
case "greater_equal":
|
||||
model.zTest = ZTestOptions.GreaterEqual;
|
||||
break;
|
||||
case "greater":
|
||||
model.zTest = ZTestOptions.Greater;
|
||||
break;
|
||||
case "not_equal":
|
||||
model.zTest = ZTestOptions.NotEqual;
|
||||
break;
|
||||
case "always":
|
||||
model.zTest = ZTestOptions.Always;
|
||||
break;
|
||||
default:
|
||||
errors.Add(new ShaderError
|
||||
{
|
||||
message = $"Invalid ZTest option: {syntax.zTest.lexeme}",
|
||||
line = syntax.zTest.line,
|
||||
column = syntax.zTest.column
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
model.zTest = ZTestOptions.LessEqual;
|
||||
}
|
||||
|
||||
// ZWrite
|
||||
if (!syntax.zWrite.Match(TokenType.None))
|
||||
{
|
||||
switch (syntax.zWrite.lexeme)
|
||||
{
|
||||
case "on":
|
||||
model.zWrite = ZWriteOptions.On;
|
||||
break;
|
||||
case "off":
|
||||
model.zWrite = ZWriteOptions.Off;
|
||||
break;
|
||||
default:
|
||||
errors.Add(new ShaderError
|
||||
{
|
||||
message = $"Invalid ZWrite option: {syntax.zWrite.lexeme}",
|
||||
line = syntax.zWrite.line,
|
||||
column = syntax.zWrite.column
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
model.zWrite = ZWriteOptions.On;
|
||||
}
|
||||
|
||||
// Cull
|
||||
if (!syntax.cull.Match(TokenType.None))
|
||||
{
|
||||
switch (syntax.cull.lexeme)
|
||||
{
|
||||
case "off":
|
||||
model.cull = CullOptions.Off;
|
||||
break;
|
||||
case "front":
|
||||
model.cull = CullOptions.Front;
|
||||
break;
|
||||
case "back":
|
||||
model.cull = CullOptions.Back;
|
||||
break;
|
||||
default:
|
||||
errors.Add(new ShaderError
|
||||
{
|
||||
message = $"Invalid Cull option: {syntax.cull.lexeme}",
|
||||
line = syntax.cull.line,
|
||||
column = syntax.cull.column
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
model.cull = CullOptions.Back;
|
||||
}
|
||||
|
||||
// Blend
|
||||
if (!syntax.blend.Match(TokenType.None))
|
||||
{
|
||||
switch (syntax.blend.lexeme)
|
||||
{
|
||||
case "opaque":
|
||||
model.blend = BlendOptions.Opaque;
|
||||
break;
|
||||
case "alpha":
|
||||
model.blend = BlendOptions.Alpha;
|
||||
break;
|
||||
case "additive":
|
||||
model.blend = BlendOptions.Additive;
|
||||
break;
|
||||
case "multiply":
|
||||
model.blend = BlendOptions.Multiply;
|
||||
break;
|
||||
case "premultiplied":
|
||||
model.blend = BlendOptions.PremultipliedAlpha;
|
||||
break;
|
||||
default:
|
||||
errors.Add(new ShaderError
|
||||
{
|
||||
message = $"Invalid Blend option: {syntax.blend.lexeme}",
|
||||
line = syntax.blend.line,
|
||||
column = syntax.blend.column
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
model.blend = BlendOptions.Opaque;
|
||||
}
|
||||
|
||||
// Color Mask
|
||||
if (!syntax.colorMask.Match(TokenType.None))
|
||||
{
|
||||
if (uint.TryParse(syntax.colorMask.lexeme, out var colorMask))
|
||||
{
|
||||
model.colorMask = colorMask;
|
||||
}
|
||||
else
|
||||
{
|
||||
errors.Add(new ShaderError
|
||||
{
|
||||
message = $"Invalid Color Mask value: {syntax.colorMask.lexeme}",
|
||||
line = syntax.colorMask.line,
|
||||
column = syntax.colorMask.column
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
model.colorMask = 0xF; // Default to RGBA
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
}
|
||||
337
Ghost.Shader/ParserBlock/PropertiesBlock.cs
Normal file
337
Ghost.Shader/ParserBlock/PropertiesBlock.cs
Normal file
@@ -0,0 +1,337 @@
|
||||
using Misaki.HighPerformance.Mathematics;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Ghost.Shader.ParserBlock;
|
||||
|
||||
internal class PropertiesBlock : IBlockParser<List<PropertySyntax>, List<PropertyModel>>
|
||||
{
|
||||
private sealed record PropTypeInfo(int ArgCount, TokenType ArgTokenType, Func<List<Token>, object?>? Builder);
|
||||
|
||||
private static readonly Dictionary<ShaderPropertyType, PropTypeInfo> s_propTypeInfo = new()
|
||||
{
|
||||
// Floats
|
||||
[ShaderPropertyType.Float] = new(1, TokenType.Number, a => float.Parse(a[0].lexeme, CultureInfo.InvariantCulture)),
|
||||
[ShaderPropertyType.Float2] = new(2, TokenType.Number, a => new float2(
|
||||
float.Parse(a[0].lexeme, CultureInfo.InvariantCulture),
|
||||
float.Parse(a[1].lexeme, CultureInfo.InvariantCulture))),
|
||||
[ShaderPropertyType.Float3] = new(3, TokenType.Number, a => new float3(
|
||||
float.Parse(a[0].lexeme, CultureInfo.InvariantCulture),
|
||||
float.Parse(a[1].lexeme, CultureInfo.InvariantCulture),
|
||||
float.Parse(a[2].lexeme, CultureInfo.InvariantCulture))),
|
||||
[ShaderPropertyType.Float4] = new(4, TokenType.Number, a => new float4(
|
||||
float.Parse(a[0].lexeme, CultureInfo.InvariantCulture),
|
||||
float.Parse(a[1].lexeme, CultureInfo.InvariantCulture),
|
||||
float.Parse(a[2].lexeme, CultureInfo.InvariantCulture),
|
||||
float.Parse(a[3].lexeme, CultureInfo.InvariantCulture))),
|
||||
|
||||
// Ints
|
||||
[ShaderPropertyType.Int] = new(1, TokenType.Number, a => int.Parse(a[0].lexeme, CultureInfo.InvariantCulture)),
|
||||
[ShaderPropertyType.Int2] = new(2, TokenType.Number, a => new int2(
|
||||
int.Parse(a[0].lexeme, CultureInfo.InvariantCulture),
|
||||
int.Parse(a[1].lexeme, CultureInfo.InvariantCulture))),
|
||||
[ShaderPropertyType.Int3] = new(3, TokenType.Number, a => new int3(
|
||||
int.Parse(a[0].lexeme, CultureInfo.InvariantCulture),
|
||||
int.Parse(a[1].lexeme, CultureInfo.InvariantCulture),
|
||||
int.Parse(a[2].lexeme, CultureInfo.InvariantCulture))),
|
||||
[ShaderPropertyType.Int4] = new(4, TokenType.Number, a => new int4(
|
||||
int.Parse(a[0].lexeme, CultureInfo.InvariantCulture),
|
||||
int.Parse(a[1].lexeme, CultureInfo.InvariantCulture),
|
||||
int.Parse(a[2].lexeme, CultureInfo.InvariantCulture),
|
||||
int.Parse(a[3].lexeme, CultureInfo.InvariantCulture))),
|
||||
|
||||
// UInts
|
||||
[ShaderPropertyType.UInt] = new(1, TokenType.Number, a => uint.Parse(a[0].lexeme, CultureInfo.InvariantCulture)),
|
||||
[ShaderPropertyType.UInt2] = new(2, TokenType.Number, a => new uint2(
|
||||
uint.Parse(a[0].lexeme, CultureInfo.InvariantCulture),
|
||||
uint.Parse(a[1].lexeme, CultureInfo.InvariantCulture))),
|
||||
[ShaderPropertyType.UInt3] = new(3, TokenType.Number, a => new uint3(
|
||||
uint.Parse(a[0].lexeme, CultureInfo.InvariantCulture),
|
||||
uint.Parse(a[1].lexeme, CultureInfo.InvariantCulture),
|
||||
uint.Parse(a[2].lexeme, CultureInfo.InvariantCulture))),
|
||||
[ShaderPropertyType.UInt4] = new(4, TokenType.Number, a => new uint4(
|
||||
uint.Parse(a[0].lexeme, CultureInfo.InvariantCulture),
|
||||
uint.Parse(a[1].lexeme, CultureInfo.InvariantCulture),
|
||||
uint.Parse(a[2].lexeme, CultureInfo.InvariantCulture),
|
||||
uint.Parse(a[3].lexeme, CultureInfo.InvariantCulture))),
|
||||
|
||||
// Bools (numbers 0/1)
|
||||
[ShaderPropertyType.Bool] = new(1, TokenType.Number, a => a[0].lexeme != "0"),
|
||||
[ShaderPropertyType.Bool2] = new(2, TokenType.Number, a => new bool2(a[0].lexeme != "0", a[1].lexeme != "0")),
|
||||
[ShaderPropertyType.Bool3] = new(3, TokenType.Number, a => new bool3(a[0].lexeme != "0", a[1].lexeme != "0", a[2].lexeme != "0")),
|
||||
[ShaderPropertyType.Bool4] = new(4, TokenType.Number, a => new bool4(a[0].lexeme != "0", a[1].lexeme != "0", a[2].lexeme != "0", a[3].lexeme != "0")),
|
||||
|
||||
// Textures (single identifier argument – currently no default object built)
|
||||
[ShaderPropertyType.Texture2D] = new(1, TokenType.Identifier, null),
|
||||
[ShaderPropertyType.Texture3D] = new(1, TokenType.Identifier, null),
|
||||
[ShaderPropertyType.TextureCube] = new(1, TokenType.Identifier, null),
|
||||
};
|
||||
|
||||
public static bool ShouldEnter(Token token)
|
||||
{
|
||||
return token.Match(TokenType.Keyword, TokenLexicon.KnownKeywords.PROPERTIES);
|
||||
}
|
||||
|
||||
private static ShaderPropertyType FromString(string type)
|
||||
{
|
||||
return type.ToLower() switch
|
||||
{
|
||||
"float" => ShaderPropertyType.Float,
|
||||
"float2" => ShaderPropertyType.Float2,
|
||||
"float3" => ShaderPropertyType.Float3,
|
||||
"float4" => ShaderPropertyType.Float4,
|
||||
"int" => ShaderPropertyType.Int,
|
||||
"int2" => ShaderPropertyType.Int2,
|
||||
"int3" => ShaderPropertyType.Int3,
|
||||
"int4" => ShaderPropertyType.Int4,
|
||||
"uint" => ShaderPropertyType.UInt,
|
||||
"uint2" => ShaderPropertyType.UInt2,
|
||||
"uint3" => ShaderPropertyType.UInt3,
|
||||
"uint4" => ShaderPropertyType.UInt4,
|
||||
"bool" => ShaderPropertyType.Bool,
|
||||
"bool2" => ShaderPropertyType.Bool2,
|
||||
"bool3" => ShaderPropertyType.Bool3,
|
||||
"bool4" => ShaderPropertyType.Bool4,
|
||||
"texture2d" => ShaderPropertyType.Texture2D,
|
||||
"texture3d" => ShaderPropertyType.Texture3D,
|
||||
"texturecube" => ShaderPropertyType.TextureCube,
|
||||
_ => ShaderPropertyType.None,
|
||||
};
|
||||
}
|
||||
|
||||
public static List<PropertySyntax> Parse(TokenStreamSlice stream)
|
||||
{
|
||||
stream.Expect(TokenType.Keyword);
|
||||
stream.Expect(TokenType.LBrace);
|
||||
|
||||
var properties = new List<PropertySyntax>();
|
||||
|
||||
var bodyStream = stream.Slice(stream.Remaining - 1);
|
||||
while (bodyStream.HasMore)
|
||||
{
|
||||
var shaderProperty = new PropertySyntax();
|
||||
|
||||
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 FunctionCall
|
||||
{
|
||||
name = constructorTypeToken,
|
||||
arguments = args
|
||||
};
|
||||
|
||||
bodyStream.Expect(TokenType.Semicolon);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case TokenType.Semicolon:
|
||||
break;
|
||||
default:
|
||||
throw new Exception($"Unexpected token '{nextToken.lexeme}' in property declaration.");
|
||||
}
|
||||
|
||||
properties.Add(shaderProperty);
|
||||
}
|
||||
|
||||
stream.Expect(TokenType.RBrace);
|
||||
|
||||
return properties;
|
||||
}
|
||||
|
||||
public List<PropertyModel> SemanticAnalysis(List<PropertySyntax> syntax, List<ShaderError> errors)
|
||||
{
|
||||
var models = new List<PropertyModel>();
|
||||
var usedPropertyNames = new HashSet<string>();
|
||||
|
||||
foreach (var property in syntax)
|
||||
{
|
||||
var model = new PropertyModel();
|
||||
|
||||
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<ShaderError> errors, PropertySyntax property, PropertyModel model)
|
||||
{
|
||||
if (!TokenLexicon.IsType(property.type.lexeme))
|
||||
{
|
||||
errors.Add(new ShaderError
|
||||
{
|
||||
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<ShaderError> errors, HashSet<string> usedPropertyNames, PropertySyntax property, PropertyModel model)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(property.name.lexeme))
|
||||
{
|
||||
errors.Add(new ShaderError
|
||||
{
|
||||
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 ShaderError
|
||||
{
|
||||
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<ShaderError> errors, PropertySyntax property, PropertyModel model)
|
||||
{
|
||||
var constructor = property.propertyConstructor;
|
||||
if (constructor == null)
|
||||
{
|
||||
errors.Add(new ShaderError
|
||||
{
|
||||
message = "Shader property constructor is null.",
|
||||
line = property.name.line,
|
||||
column = property.name.column
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(constructor.name.lexeme))
|
||||
{
|
||||
errors.Add(new ShaderError
|
||||
{
|
||||
message = "Shader property constructor has an empty name.",
|
||||
line = constructor.name.line,
|
||||
column = constructor.name.column
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
if (constructor.name.lexeme != property.type.lexeme)
|
||||
{
|
||||
errors.Add(new ShaderError
|
||||
{
|
||||
message = $"Shader property constructor name '{constructor.name.lexeme}' does not match property type '{property.type.lexeme}'.",
|
||||
line = constructor.name.line,
|
||||
column = constructor.name.column
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!s_propTypeInfo.TryGetValue(model.type, out var info))
|
||||
{
|
||||
errors.Add(new ShaderError
|
||||
{
|
||||
message = $"No constructor metadata registered for property type '{model.type}'.",
|
||||
line = constructor.name.line,
|
||||
column = constructor.name.column
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
// Count check
|
||||
if (constructor.arguments.Count != info.ArgCount)
|
||||
{
|
||||
errors.Add(new ShaderError
|
||||
{
|
||||
message = $"Shader property constructor for type '{property.type.lexeme}' expects {info.ArgCount} argument(s), but got {constructor.arguments.Count}.",
|
||||
line = constructor.name.line,
|
||||
column = constructor.name.column
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
// Type check (uniform requirement for all args)
|
||||
var hasError = false;
|
||||
for (var i = 0; i < constructor.arguments.Count; i++)
|
||||
{
|
||||
var arg = constructor.arguments[i];
|
||||
if (!arg.Match(info.ArgTokenType))
|
||||
{
|
||||
errors.Add(new ShaderError
|
||||
{
|
||||
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(constructor.arguments);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
errors.Add(new ShaderError
|
||||
{
|
||||
message = $"Failed to construct default value for property '{property.name.lexeme}': {ex.Message}",
|
||||
line = constructor.name.line,
|
||||
column = constructor.name.column
|
||||
});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
43
Ghost.Shader/ParserBlock/ShaderBlock.cs
Normal file
43
Ghost.Shader/ParserBlock/ShaderBlock.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
namespace Ghost.Shader.ParserBlock;
|
||||
|
||||
internal class ShaderBlock : IBlockParser<ShaderSyntax>
|
||||
{
|
||||
public static bool ShouldEnter(Token token)
|
||||
{
|
||||
return token.Match(TokenType.Keyword, TokenLexicon.KnownKeywords.SHADER);
|
||||
}
|
||||
|
||||
public static ShaderSyntax Parse(TokenStreamSlice stream)
|
||||
{
|
||||
var shader = new ShaderSyntax();
|
||||
|
||||
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.Add(PassBlock.Parse(bodyStream.SliceNextBlock()));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Unexpected token '{nextToken.lexeme}' in shader body.");
|
||||
}
|
||||
}
|
||||
|
||||
stream.Expect(TokenType.RBrace);
|
||||
|
||||
return shader;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user