Code refactoring in IShaderGenerator
このコミットが含まれているのは:
1 親
1262bf6e0e
コミット
0004353052
13ファイル変更
+1897
-1835
検索条件に一致するファイルはありません
+207
-203
@@ -14,241 +14,245 @@ namespace ComputeSharp.SourceGenerators;
|
||||
/// <inheritdoc/>
|
||||
public sealed partial class IShaderGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a <see cref="MethodDeclarationSyntax"/> instance for the <c>BuildHlslString</c> method.
|
||||
/// </summary>
|
||||
/// <param name="hlslSourceInfo">The input <see cref="HlslSourceInfo"/> instance to use.</param>
|
||||
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the <c>BuildHlslString</c> method.</returns>
|
||||
private static MethodDeclarationSyntax CreateBuildHlslStringMethod(HlslSourceInfo hlslSourceInfo)
|
||||
/// <inheritdoc/>
|
||||
internal static partial class BuildHlslString
|
||||
{
|
||||
ImmutableArray<StatementSyntax> bodyStatements = GenerateRenderMethodBody(hlslSourceInfo);
|
||||
|
||||
// This code produces a method declaration as follows:
|
||||
//
|
||||
// readonly void global::ComputeSharp.__Internals.IShader.BuildHlslString(ref global::ComputeSharp.__Internals.ArrayPoolStringBuilder builder, int threadsX, int threadsY, int threadsZ)
|
||||
// {
|
||||
// <BODY>
|
||||
// }
|
||||
return
|
||||
MethodDeclaration(
|
||||
PredefinedType(Token(SyntaxKind.VoidKeyword)),
|
||||
Identifier("BuildHlslString"))
|
||||
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.__Internals.{nameof(IShader)}")))
|
||||
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
|
||||
.AddParameterListParameters(
|
||||
Parameter(Identifier("builder")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(IdentifierName("global::ComputeSharp.__Internals.ArrayPoolStringBuilder")),
|
||||
Parameter(Identifier("threadsX")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))),
|
||||
Parameter(Identifier("threadsY")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))),
|
||||
Parameter(Identifier("threadsZ")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))))
|
||||
.WithBody(Block(bodyStatements));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Produces the series of statements to build the current HLSL source.
|
||||
/// </summary>
|
||||
/// <param name="hlslSourceInfo">The input <see cref="HlslSourceInfo"/> instance to use.</param>
|
||||
/// <returns>The series of statements to build the HLSL source to compile to execute the current shader.</returns>
|
||||
private static ImmutableArray<StatementSyntax> GenerateRenderMethodBody(HlslSourceInfo hlslSourceInfo)
|
||||
{
|
||||
ImmutableArray<StatementSyntax>.Builder statements = ImmutableArray.CreateBuilder<StatementSyntax>();
|
||||
StringBuilder textBuilder = new();
|
||||
int capturedDelegates = 0;
|
||||
int prologueStatements = 0;
|
||||
int sizeHint = 64;
|
||||
|
||||
void AppendLF()
|
||||
/// <summary>
|
||||
/// Creates a <see cref="MethodDeclarationSyntax"/> instance for the <c>BuildHlslString</c> method.
|
||||
/// </summary>
|
||||
/// <param name="hlslSourceInfo">The input <see cref="HlslSourceInfo"/> instance to use.</param>
|
||||
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the <c>BuildHlslString</c> method.</returns>
|
||||
public static MethodDeclarationSyntax GetSyntax(HlslSourceInfo hlslSourceInfo)
|
||||
{
|
||||
textBuilder.Append('\n');
|
||||
ImmutableArray<StatementSyntax> bodyStatements = GenerateRenderMethodBody(hlslSourceInfo);
|
||||
|
||||
// This code produces a method declaration as follows:
|
||||
//
|
||||
// readonly void global::ComputeSharp.__Internals.IShader.BuildHlslString(ref global::ComputeSharp.__Internals.ArrayPoolStringBuilder builder, int threadsX, int threadsY, int threadsZ)
|
||||
// {
|
||||
// <BODY>
|
||||
// }
|
||||
return
|
||||
MethodDeclaration(
|
||||
PredefinedType(Token(SyntaxKind.VoidKeyword)),
|
||||
Identifier("BuildHlslString"))
|
||||
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.__Internals.{nameof(IShader)}")))
|
||||
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
|
||||
.AddParameterListParameters(
|
||||
Parameter(Identifier("builder")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(IdentifierName("global::ComputeSharp.__Internals.ArrayPoolStringBuilder")),
|
||||
Parameter(Identifier("threadsX")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))),
|
||||
Parameter(Identifier("threadsY")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))),
|
||||
Parameter(Identifier("threadsZ")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))))
|
||||
.WithBody(Block(bodyStatements));
|
||||
}
|
||||
|
||||
void AppendLine(string text)
|
||||
/// <summary>
|
||||
/// Produces the series of statements to build the current HLSL source.
|
||||
/// </summary>
|
||||
/// <param name="hlslSourceInfo">The input <see cref="HlslSourceInfo"/> instance to use.</param>
|
||||
/// <returns>The series of statements to build the HLSL source to compile to execute the current shader.</returns>
|
||||
private static ImmutableArray<StatementSyntax> GenerateRenderMethodBody(HlslSourceInfo hlslSourceInfo)
|
||||
{
|
||||
textBuilder.Append(text);
|
||||
}
|
||||
ImmutableArray<StatementSyntax>.Builder statements = ImmutableArray.CreateBuilder<StatementSyntax>();
|
||||
StringBuilder textBuilder = new();
|
||||
int capturedDelegates = 0;
|
||||
int prologueStatements = 0;
|
||||
int sizeHint = 64;
|
||||
|
||||
void AppendParsedStatement(string text)
|
||||
{
|
||||
FlushText();
|
||||
|
||||
statements.Add(ParseStatement(text));
|
||||
}
|
||||
|
||||
void FlushText()
|
||||
{
|
||||
if (textBuilder.Length > 0)
|
||||
void AppendLF()
|
||||
{
|
||||
string text = textBuilder.ToString();
|
||||
|
||||
textBuilder.Append(text);
|
||||
|
||||
sizeHint += textBuilder.Length;
|
||||
|
||||
statements.Add(
|
||||
ExpressionStatement(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName("builder"), IdentifierName("Append")))
|
||||
.AddArgumentListArguments(Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(text))))));
|
||||
|
||||
textBuilder.Clear();
|
||||
textBuilder.Append('\n');
|
||||
}
|
||||
}
|
||||
|
||||
// Declare the hashsets to track imported members and types from delegates, if needed
|
||||
if (!hlslSourceInfo.Delegates.IsEmpty)
|
||||
{
|
||||
void DeclareMapping(int index, string name, IEnumerable<string> items)
|
||||
void AppendLine(string text)
|
||||
{
|
||||
// global::System.Collections.Generic.HashSet<string> <NAME> = new();
|
||||
statements.Insert(index,
|
||||
LocalDeclarationStatement(VariableDeclaration(
|
||||
GenericName(Identifier("global::System.Collections.Generic.HashSet"))
|
||||
.AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.StringKeyword))))
|
||||
.AddVariables(
|
||||
VariableDeclarator(Identifier(name))
|
||||
.WithInitializer(EqualsValueClause(ImplicitObjectCreationExpression())))));
|
||||
textBuilder.Append(text);
|
||||
}
|
||||
|
||||
prologueStatements++;
|
||||
void AppendParsedStatement(string text)
|
||||
{
|
||||
FlushText();
|
||||
|
||||
// <NAME>.Add("<ITEM>");
|
||||
foreach (var item in items)
|
||||
statements.Add(ParseStatement(text));
|
||||
}
|
||||
|
||||
void FlushText()
|
||||
{
|
||||
if (textBuilder.Length > 0)
|
||||
{
|
||||
string text = textBuilder.ToString();
|
||||
|
||||
textBuilder.Append(text);
|
||||
|
||||
sizeHint += textBuilder.Length;
|
||||
|
||||
statements.Add(
|
||||
ExpressionStatement(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName("builder"), IdentifierName("Append")))
|
||||
.AddArgumentListArguments(Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(text))))));
|
||||
|
||||
textBuilder.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
// Declare the hashsets to track imported members and types from delegates, if needed
|
||||
if (!hlslSourceInfo.Delegates.IsEmpty)
|
||||
{
|
||||
void DeclareMapping(int index, string name, IEnumerable<string> items)
|
||||
{
|
||||
// global::System.Collections.Generic.HashSet<string> <NAME> = new();
|
||||
statements.Insert(index,
|
||||
LocalDeclarationStatement(VariableDeclaration(
|
||||
GenericName(Identifier("global::System.Collections.Generic.HashSet"))
|
||||
.AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.StringKeyword))))
|
||||
.AddVariables(
|
||||
VariableDeclarator(Identifier(name))
|
||||
.WithInitializer(EqualsValueClause(ImplicitObjectCreationExpression())))));
|
||||
|
||||
prologueStatements++;
|
||||
|
||||
// <NAME>.Add("<ITEM>");
|
||||
foreach (var item in items)
|
||||
{
|
||||
statements.Add(
|
||||
ExpressionStatement(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName(name),
|
||||
IdentifierName("Add")))
|
||||
.AddArgumentListArguments(Argument(
|
||||
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(item))))));
|
||||
|
||||
prologueStatements++;
|
||||
}
|
||||
}
|
||||
|
||||
DeclareMapping(0, "__typeNames", hlslSourceInfo.DefinedTypes);
|
||||
DeclareMapping(1, "__constantNames", hlslSourceInfo.DefinedConstants);
|
||||
DeclareMapping(2, "__methodNames", hlslSourceInfo.MethodSignatures);
|
||||
|
||||
// Go through all existing delegate fields, if any
|
||||
foreach (string fieldName in hlslSourceInfo.Delegates)
|
||||
{
|
||||
// global::ComputeSharp.__Internals.ShaderMethodSourceAttribute __<DELEGATE_NAME>Attribute = global::ComputeSharp.__Internals.ShaderMethodSourceAttribute.GetForDelegate(<DELEGATE_NAME>, "<DELEGATE_NAME>");
|
||||
statements.Add(
|
||||
LocalDeclarationStatement(VariableDeclaration(IdentifierName($"global::ComputeSharp.__Internals.{nameof(ShaderMethodSourceAttribute)}"))
|
||||
.AddVariables(
|
||||
VariableDeclarator(Identifier($"__{fieldName}Attribute"))
|
||||
.WithInitializer(
|
||||
EqualsValueClause(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName($"global::ComputeSharp.__Internals.{nameof(ShaderMethodSourceAttribute)}"),
|
||||
IdentifierName(nameof(ShaderMethodSourceAttribute.GetForDelegate))))
|
||||
.AddArgumentListArguments(
|
||||
Argument(IdentifierName(fieldName)),
|
||||
Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(fieldName)))))))));
|
||||
|
||||
capturedDelegates++;
|
||||
prologueStatements++;
|
||||
}
|
||||
}
|
||||
|
||||
// Header and thread ids
|
||||
AppendLine(hlslSourceInfo.HeaderAndThreadsX);
|
||||
AppendParsedStatement("builder.Append(threadsX);");
|
||||
AppendLine(hlslSourceInfo.ThreadsY);
|
||||
AppendParsedStatement("builder.Append(threadsY);");
|
||||
AppendLine(hlslSourceInfo.ThreadsZ);
|
||||
AppendParsedStatement("builder.Append(threadsZ);");
|
||||
|
||||
// Define declarations
|
||||
AppendLine(hlslSourceInfo.Defines);
|
||||
|
||||
// Defines from captured delegates
|
||||
foreach (string fieldName in hlslSourceInfo.Delegates)
|
||||
{
|
||||
AppendParsedStatement($"__{fieldName}Attribute.AppendConstants(ref builder, __constantNames);");
|
||||
}
|
||||
|
||||
// Static fields and declared types
|
||||
AppendLine(hlslSourceInfo.StaticFieldsAndDeclaredTypes);
|
||||
|
||||
// Declared types from captured delegates
|
||||
foreach (string fieldName in hlslSourceInfo.Delegates)
|
||||
{
|
||||
AppendParsedStatement($"__{fieldName}Attribute.AppendTypes(ref builder, __typeNames);");
|
||||
}
|
||||
|
||||
// Captured variables
|
||||
AppendLine(hlslSourceInfo.CapturedFieldsAndResourcesAndForwardDeclarations);
|
||||
|
||||
// Forward declarations from captured delegates
|
||||
foreach (string fieldName in hlslSourceInfo.Delegates)
|
||||
{
|
||||
AppendParsedStatement($"__{fieldName}Attribute.AppendForwardDeclarations(ref builder, __methodNames);");
|
||||
}
|
||||
|
||||
// Remove all forward declarations from methods that are embedded into the shader.
|
||||
// This is necessary to avoid duplicate definitions from methods from delegates.
|
||||
if (capturedDelegates > 0)
|
||||
{
|
||||
// <NAME>.Add("<ITEM>");
|
||||
foreach (string forwardDeclaration in hlslSourceInfo.MethodSignatures)
|
||||
{
|
||||
FlushText();
|
||||
|
||||
statements.Add(
|
||||
ExpressionStatement(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName(name),
|
||||
IdentifierName("Add")))
|
||||
IdentifierName("__methodNames"),
|
||||
IdentifierName("Remove")))
|
||||
.AddArgumentListArguments(Argument(
|
||||
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(item))))));
|
||||
|
||||
prologueStatements++;
|
||||
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(forwardDeclaration))))));
|
||||
}
|
||||
}
|
||||
|
||||
DeclareMapping(0, "__typeNames", hlslSourceInfo.DefinedTypes);
|
||||
DeclareMapping(1, "__constantNames", hlslSourceInfo.DefinedConstants);
|
||||
DeclareMapping(2, "__methodNames", hlslSourceInfo.MethodSignatures);
|
||||
// Captured methods
|
||||
AppendLine(hlslSourceInfo.CapturedMethods);
|
||||
|
||||
// Go through all existing delegate fields, if any
|
||||
// Captured methods from captured delegates
|
||||
foreach (string fieldName in hlslSourceInfo.Delegates)
|
||||
{
|
||||
// global::ComputeSharp.__Internals.ShaderMethodSourceAttribute __<DELEGATE_NAME>Attribute = global::ComputeSharp.__Internals.ShaderMethodSourceAttribute.GetForDelegate(<DELEGATE_NAME>, "<DELEGATE_NAME>");
|
||||
statements.Add(
|
||||
LocalDeclarationStatement(VariableDeclaration(IdentifierName($"global::ComputeSharp.__Internals.{nameof(ShaderMethodSourceAttribute)}"))
|
||||
.AddVariables(
|
||||
VariableDeclarator(Identifier($"__{fieldName}Attribute"))
|
||||
.WithInitializer(
|
||||
EqualsValueClause(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName($"global::ComputeSharp.__Internals.{nameof(ShaderMethodSourceAttribute)}"),
|
||||
IdentifierName(nameof(ShaderMethodSourceAttribute.GetForDelegate))))
|
||||
.AddArgumentListArguments(
|
||||
Argument(IdentifierName(fieldName)),
|
||||
Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(fieldName)))))))));
|
||||
|
||||
capturedDelegates++;
|
||||
prologueStatements++;
|
||||
AppendParsedStatement($"__{fieldName}Attribute.AppendMethods(ref builder, __methodNames);");
|
||||
}
|
||||
}
|
||||
|
||||
// Header and thread ids
|
||||
AppendLine(hlslSourceInfo.HeaderAndThreadsX);
|
||||
AppendParsedStatement("builder.Append(threadsX);");
|
||||
AppendLine(hlslSourceInfo.ThreadsY);
|
||||
AppendParsedStatement("builder.Append(threadsY);");
|
||||
AppendLine(hlslSourceInfo.ThreadsZ);
|
||||
AppendParsedStatement("builder.Append(threadsZ);");
|
||||
|
||||
// Define declarations
|
||||
AppendLine(hlslSourceInfo.Defines);
|
||||
|
||||
// Defines from captured delegates
|
||||
foreach (string fieldName in hlslSourceInfo.Delegates)
|
||||
{
|
||||
AppendParsedStatement($"__{fieldName}Attribute.AppendConstants(ref builder, __constantNames);");
|
||||
}
|
||||
|
||||
// Static fields and declared types
|
||||
AppendLine(hlslSourceInfo.StaticFieldsAndDeclaredTypes);
|
||||
|
||||
// Declared types from captured delegates
|
||||
foreach (string fieldName in hlslSourceInfo.Delegates)
|
||||
{
|
||||
AppendParsedStatement($"__{fieldName}Attribute.AppendTypes(ref builder, __typeNames);");
|
||||
}
|
||||
|
||||
// Captured variables
|
||||
AppendLine(hlslSourceInfo.CapturedFieldsAndResourcesAndForwardDeclarations);
|
||||
|
||||
// Forward declarations from captured delegates
|
||||
foreach (string fieldName in hlslSourceInfo.Delegates)
|
||||
{
|
||||
AppendParsedStatement($"__{fieldName}Attribute.AppendForwardDeclarations(ref builder, __methodNames);");
|
||||
}
|
||||
|
||||
// Remove all forward declarations from methods that are embedded into the shader.
|
||||
// This is necessary to avoid duplicate definitions from methods from delegates.
|
||||
if (capturedDelegates > 0)
|
||||
{
|
||||
// <NAME>.Add("<ITEM>");
|
||||
foreach (string forwardDeclaration in hlslSourceInfo.MethodSignatures)
|
||||
// Captured delegate methods
|
||||
foreach (string fieldName in hlslSourceInfo.Delegates)
|
||||
{
|
||||
FlushText();
|
||||
AppendLF();
|
||||
AppendParsedStatement($"__{fieldName}Attribute.AppendMappedInvokeMethod(ref builder, \"{fieldName}\");");
|
||||
AppendLF();
|
||||
}
|
||||
|
||||
statements.Add(
|
||||
ExpressionStatement(
|
||||
// Entry point
|
||||
AppendLine(hlslSourceInfo.EntryPoint);
|
||||
|
||||
FlushText();
|
||||
|
||||
// builder = global::ComputeSharp.__Internals.ArrayPoolStringBuilder.Create(<SIZE_HINT>);
|
||||
statements.Insert(
|
||||
prologueStatements,
|
||||
ExpressionStatement(
|
||||
AssignmentExpression(
|
||||
SyntaxKind.SimpleAssignmentExpression,
|
||||
IdentifierName("builder"),
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("__methodNames"),
|
||||
IdentifierName("Remove")))
|
||||
.AddArgumentListArguments(Argument(
|
||||
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(forwardDeclaration))))));
|
||||
}
|
||||
IdentifierName("global::ComputeSharp.__Internals.ArrayPoolStringBuilder"),
|
||||
IdentifierName("Create")))
|
||||
.AddArgumentListArguments(
|
||||
Argument(LiteralExpression(
|
||||
SyntaxKind.NumericLiteralExpression,
|
||||
Literal(sizeHint)))))));
|
||||
|
||||
return statements.ToImmutable();
|
||||
}
|
||||
|
||||
// Captured methods
|
||||
AppendLine(hlslSourceInfo.CapturedMethods);
|
||||
|
||||
// Captured methods from captured delegates
|
||||
foreach (string fieldName in hlslSourceInfo.Delegates)
|
||||
{
|
||||
AppendParsedStatement($"__{fieldName}Attribute.AppendMethods(ref builder, __methodNames);");
|
||||
}
|
||||
|
||||
// Captured delegate methods
|
||||
foreach (string fieldName in hlslSourceInfo.Delegates)
|
||||
{
|
||||
AppendLF();
|
||||
AppendParsedStatement($"__{fieldName}Attribute.AppendMappedInvokeMethod(ref builder, \"{fieldName}\");");
|
||||
AppendLF();
|
||||
}
|
||||
|
||||
// Entry point
|
||||
AppendLine(hlslSourceInfo.EntryPoint);
|
||||
|
||||
FlushText();
|
||||
|
||||
// builder = global::ComputeSharp.__Internals.ArrayPoolStringBuilder.Create(<SIZE_HINT>);
|
||||
statements.Insert(
|
||||
prologueStatements,
|
||||
ExpressionStatement(
|
||||
AssignmentExpression(
|
||||
SyntaxKind.SimpleAssignmentExpression,
|
||||
IdentifierName("builder"),
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("global::ComputeSharp.__Internals.ArrayPoolStringBuilder"),
|
||||
IdentifierName("Create")))
|
||||
.AddArgumentListArguments(
|
||||
Argument(LiteralExpression(
|
||||
SyntaxKind.NumericLiteralExpression,
|
||||
Literal(sizeHint)))))));
|
||||
|
||||
return statements.ToImmutable();
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする