Refactor shader system: arrays, keywords, property syntax

Major refactor of shader compiler and related systems:
- Switch ShaderDescriptor/PassDescriptor to arrays; remove IPassDescriptor
- Rewrite keywords block parser/semantic analysis for flexible syntax
- Change property initializers to brace syntax `{ ... }`
- Simplify TokenStream API (remove ref index params)
- Make GetBindlessIndex return uint (~0u for not found)
- Update shader compilation and variant logic for new descriptors
- Update test shader syntax to match new property/keyword formats
- Add AGENTS.md agent development guide
- Add Antlr4 dependency to Ghost.DSL
- Miscellaneous code style and error handling improvements
This commit is contained in:
2026-01-10 18:36:18 +09:00
parent 6a041f75ba
commit d71bdb3fc9
18 changed files with 548 additions and 246 deletions

View File

@@ -2,26 +2,42 @@ using Ghost.Core.Graphics;
namespace Ghost.DSL.ShaderCompiler.Parser;
internal class KeywordsBlock : IBlockParser<List<FunctionCallDeclaration>, List<KeywordsGroup>>
internal class KeywordsBlock : IBlockParser<List<List<Token>>, List<KeywordsGroup>>
{
public static bool ShouldEnter(Token token)
{
return token.Match(TokenType.Keyword, TokenLexicon.KnownKeywords.KEYWORDS);
}
public static List<FunctionCallDeclaration> Parse(TokenStreamSlice stream)
public static List<List<Token>> Parse(TokenStreamSlice stream)
{
stream.Expect(TokenType.Keyword);
stream.Expect(TokenType.LBrace);
var keywords = new List<FunctionCallDeclaration>();
var keywords = new List<List<Token>>();
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 });
var keys = new List<Token>();
while (!bodyStream.Match(TokenType.Semicolon))
{
var expectType = TokenType.Identifier;
if (keys.Count == 0)
{
expectType |= TokenType.Keyword;
}
var argument = bodyStream.Expect(expectType);
keys.Add(argument);
if (bodyStream.Match(TokenType.Comma))
{
bodyStream.Consume();
}
}
keywords.Add(keys);
bodyStream.Expect(TokenType.Semicolon);
}
@@ -30,7 +46,7 @@ internal class KeywordsBlock : IBlockParser<List<FunctionCallDeclaration>, List<
return keywords;
}
public static List<KeywordsGroup>? SemanticAnalysis(List<FunctionCallDeclaration>? syntax, List<DSLShaderError> errors)
public static List<KeywordsGroup>? SemanticAnalysis(List<List<Token>>? syntax, List<DSLShaderError> errors)
{
if (syntax == null)
{
@@ -38,42 +54,42 @@ internal class KeywordsBlock : IBlockParser<List<FunctionCallDeclaration>, List<
}
var keywords = new List<KeywordsGroup>(syntax.Count);
foreach (var keyword in syntax)
foreach (var keys in syntax)
{
if (keyword.arguments == null || keyword.arguments.Count == 0)
if (keys.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)
group.space = keys[0].lexeme switch
{
case TokenLexicon.KnownFunctions.LOCAL:
group.space = KeywordSpace.Local;
break;
case TokenLexicon.KnownFunctions.GLOBAL:
group.space = KeywordSpace.Global;
break;
default:
TokenLexicon.KnownFunctions.LOCAL => KeywordSpace.Local,
TokenLexicon.KnownFunctions.GLOBAL => KeywordSpace.Global,
_ => KeywordSpace.Local
};
for (var i = 0; i < keys.Count; i++)
{
var token = keys[i];
if (i == 0 && token.type == TokenType.Keyword)
{
continue;
}
if (token.type != TokenType.Identifier)
{
errors.Add(new DSLShaderError
{
message = $"Unknown function name '{keyword.name.lexeme}'.",
line = keyword.name.line,
column = keyword.name.column
message = $"Invalid keyword '{token.lexeme}' in keywords block.",
line = token.line,
column = token.column
});
continue;
}
}
foreach (var arg in keyword.arguments)
{
group.keywords ??= new List<string>(keyword.arguments.Count);
group.keywords.Add(arg.lexeme);
group.keywords ??= new List<string>(keys.Count);
group.keywords.Add(token.lexeme);
}
keywords.Add(group);

View File

@@ -68,6 +68,7 @@ internal class PassBlock : IBlockParser<PassSyntax, PassSemantic>
{
name = syntax.name.lexeme,
defines = DefinesBlock.SemanticAnalysis(syntax.defines, errors),
includes = IncludesBlock.SemanticAnalysis(syntax.includes, errors),
keywords = KeywordsBlock.SemanticAnalysis(syntax.keywords, errors),
localPipeline = PipelineBlock.SemanticAnalysis(syntax.localPipeline, errors),
};

View File

@@ -218,14 +218,18 @@ internal class PropertiesBlock : IBlockParser<PropertiesSyntax, List<PropertySem
{
case TokenType.Equals:
{
var constructorTypeToken = bodyStream.Expect(TokenType.Identifier);
var args = ParseUtility.ParseFunctionArguments(ref bodyStream, TokenType.Identifier | TokenType.Number);
shaderProperty.propertyConstructor = new FunctionCallDeclaration
bodyStream.Expect(TokenType.LBrace);
while (!bodyStream.Match(TokenType.RBrace))
{
name = constructorTypeToken,
arguments = args
};
var token = bodyStream.Consume();
if (!token.Match(TokenType.Comma))
{
shaderProperty.propertyInitializer ??= new List<Token>();
shaderProperty.propertyInitializer.Add(token);
}
}
bodyStream.Expect(TokenType.RBrace);
bodyStream.Expect(TokenType.Semicolon);
break;
@@ -282,9 +286,9 @@ internal class PropertiesBlock : IBlockParser<PropertiesSyntax, List<PropertySem
continue;
}
if (property.propertyConstructor != null)
if (property.propertyInitializer != null)
{
flowControl = ValidatePropertyConstructor(errors, property, model);
flowControl = ValidatePropertyInitializer(errors, property, model);
if (!flowControl)
{
continue;
@@ -346,14 +350,14 @@ internal class PropertiesBlock : IBlockParser<PropertiesSyntax, List<PropertySem
return true;
}
private static bool ValidatePropertyConstructor(List<DSLShaderError> errors, PropertyDeclaration property, PropertySemantic model)
private static bool ValidatePropertyInitializer(List<DSLShaderError> errors, PropertyDeclaration property, PropertySemantic model)
{
var constructor = property.propertyConstructor;
if (!constructor.HasValue)
var initializer = property.propertyInitializer;
if (initializer == null)
{
errors.Add(new DSLShaderError
{
message = "Shader property constructor is null.",
message = "Shader property initializer is null.",
line = property.name.line,
column = property.name.column
});
@@ -361,63 +365,25 @@ internal class PropertiesBlock : IBlockParser<PropertiesSyntax, List<PropertySem
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
message = $"No initializer metadata registered for property type '{model.type}'.",
line = property.name.line,
column = property.name.column
});
return false;
}
// Count check
if (constructorValue.arguments == null)
if (initializer.Count != info.ArgCount)
{
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
message = $"Shader property constructor for type '{property.type.lexeme}' expects {info.ArgCount} argument(s), but got {initializer.Count}.",
line = property.name.line,
column = property.name.column
});
return false;
@@ -425,9 +391,9 @@ internal class PropertiesBlock : IBlockParser<PropertiesSyntax, List<PropertySem
// Type check (uniform requirement for all args)
var hasError = false;
for (var i = 0; i < constructorValue.arguments.Count; i++)
for (var i = 0; i < initializer.Count; i++)
{
var arg = constructorValue.arguments[i];
var arg = initializer[i];
if (!arg.Match(info.ArgTokenType))
{
errors.Add(new DSLShaderError
@@ -451,15 +417,15 @@ internal class PropertiesBlock : IBlockParser<PropertiesSyntax, List<PropertySem
{
try
{
model.defaultValue = info.Builder(constructorValue.arguments, errors);
model.defaultValue = info.Builder(initializer, 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
line = property.name.line,
column = property.name.column
});
return false;