diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs
index f59ea44d..dac9d470 100644
--- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs
+++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs
@@ -29,7 +29,8 @@ namespace ComputeSharp.SourceGenerators
StructDeclarationSyntax structDeclaration,
INamedTypeSymbol structDeclarationSymbol,
out string? implicitTextureType,
- out bool isSamplerUsed)
+ out bool isSamplerUsed,
+ out bool isHashSetDirectiveNeeded)
{
// Properties are not supported
DetectAndReportPropertyDeclarations(context, structDeclarationSymbol);
@@ -71,7 +72,8 @@ namespace ComputeSharp.SourceGenerators
sharedBuffers,
forwardDeclarations,
processedMethods,
- entryPoint);
+ entryPoint,
+ out isHashSetDirectiveNeeded);
implicitTextureType = pixelShaderTextureType;
isSamplerUsed = accessesStaticSampler;
@@ -469,6 +471,7 @@ namespace ComputeSharp.SourceGenerators
/// The sequence of forward method declarations.
/// The sequence of processed methods used by the shader.
/// The body of the entry point of the shader.
+ /// Indicates whether or not the directive for is needed.
/// The series of statements to build the HLSL source to compile to execute the current shader.
private static IEnumerable GenerateRenderMethodBody(
IEnumerable<(string Name, string Value)> definedConstants,
@@ -479,9 +482,10 @@ namespace ComputeSharp.SourceGenerators
string? implicitTextureType,
bool isSamplerUsed,
IEnumerable<(string Name, string Type, int? Count)> sharedBuffers,
- IEnumerable? forwardDeclarations,
+ IEnumerable forwardDeclarations,
IEnumerable processedMethods,
- string executeMethod)
+ string executeMethod,
+ out bool isHashSetDirectiveNeeded)
{
List statements = new();
StringBuilder textBuilder = new();
@@ -558,6 +562,42 @@ namespace ComputeSharp.SourceGenerators
}
}
+ // Declare the hashsets to track imported members and types from delegates, if needed
+ if (capturedDelegates > 0)
+ {
+ void DeclareMapping(int index, string name, IEnumerable items)
+ {
+ // HashSet = new();
+ statements.Insert(index,
+ LocalDeclarationStatement(VariableDeclaration(
+ GenericName(Identifier("HashSet"))
+ .AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.StringKeyword))))
+ .AddVariables(
+ VariableDeclarator(Identifier(name))
+ .WithInitializer(EqualsValueClause(ImplicitObjectCreationExpression())))));
+
+ // .Add("- ");
+ foreach (var item in items)
+ {
+ statements.Add(
+ ExpressionStatement(
+ InvocationExpression(
+ MemberAccessExpression(
+ SyntaxKind.SimpleMemberAccessExpression,
+ IdentifierName(name),
+ IdentifierName("Add")))
+ .AddArgumentListArguments(Argument(
+ LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(item))))));
+ }
+ }
+
+ DeclareMapping(0, "__typeNames", declaredTypes.Select(static t => t.Name));
+ DeclareMapping(1, "__constantNames", definedConstants.Select(static t => t.Name));
+ DeclareMapping(2, "__methodNames", forwardDeclarations);
+ }
+
+ isHashSetDirectiveNeeded = capturedDelegates > 0;
+
// Header
AppendLineAndLF("// ================================================");
AppendLineAndLF("// AUTO GENERATED");
@@ -599,7 +639,7 @@ namespace ComputeSharp.SourceGenerators
}
// Declared types
- foreach (var (typeName, typeDefinition) in declaredTypes)
+ foreach (var (_, typeDefinition) in declaredTypes)
{
AppendLF();
AppendLineAndLF(typeDefinition);
@@ -702,7 +742,7 @@ namespace ComputeSharp.SourceGenerators
}
// Captured delegate methods
- foreach (var (fieldSymbol, fieldName, fieldType) in instanceFields)
+ foreach (var (fieldSymbol, fieldName, _) in instanceFields)
{
if (fieldSymbol.Type.TypeKind == TypeKind.Delegate)
{
@@ -718,8 +758,13 @@ namespace ComputeSharp.SourceGenerators
FlushText();
+ // The builder declaration needs to go after the captured delegate attribute
+ // declaration, and also after the 3 name mapping hashsets inserted at the top.
+ int builderDeclarationIndex = capturedDelegates > 0 ? capturedDelegates + 3 : 0;
+
// builder = ArrayPoolStringBuilder.Create();
- statements.Insert(capturedDelegates,
+ statements.Insert(
+ builderDeclarationIndex,
ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
index 23d30bbd..e341f03d 100644
--- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
@@ -59,7 +59,7 @@ namespace ComputeSharp.SourceGenerators
MethodDeclarationSyntax
getDispatchIdMethod = CreateGetDispatchIdMethod(structDeclarationSymbol),
loadDispatchDataMethod = CreateLoadDispatchDataMethod(context, structDeclarationSymbol, out var discoveredResources, out int root32BitConstants),
- buildHlslStringMethod = CreateBuildHlslStringMethod(context, structDeclaration, structDeclarationSymbol, out string? implicitTextureType, out bool isSamplerUsed),
+ buildHlslStringMethod = CreateBuildHlslStringMethod(context, structDeclaration, structDeclarationSymbol, out string? implicitTextureType, out bool isSamplerUsed, out bool isHashSetDirectiveNeeded),
loadDispatchMetadataMethod = CreateLoadDispatchMetadataMethod(implicitTextureType, discoveredResources, root32BitConstants, isSamplerUsed);
// Reorder the method declarations to respect the order in the interface definition
@@ -122,6 +122,23 @@ namespace ComputeSharp.SourceGenerators
.WithoutTrivia();
}
+ // Prepare the necessary using directives
+ List usingDirectives = new()
+ {
+ UsingDirective(IdentifierName("System")),
+ UsingDirective(IdentifierName("System.CodeDom.Compiler")),
+ UsingDirective(IdentifierName("System.ComponentModel")),
+ UsingDirective(IdentifierName("System.Diagnostics")),
+ UsingDirective(IdentifierName("System.Diagnostics.CodeAnalysis")),
+ UsingDirective(IdentifierName("System.Runtime.CompilerServices")),
+ UsingDirective(IdentifierName("ComputeSharp.__Internals"))
+ };
+
+ if (isHashSetDirectiveNeeded)
+ {
+ usingDirectives.Insert(2, UsingDirective(IdentifierName("System.Collections.Generic")));
+ }
+
// Create a static method to create the combined hashcode for a given shader type.
// This code takes a block syntax and produces a compilation unit as follows:
//
@@ -140,14 +157,7 @@ namespace ComputeSharp.SourceGenerators
//
// }
var source =
- CompilationUnit().AddUsings(
- UsingDirective(IdentifierName("System")),
- UsingDirective(IdentifierName("System.CodeDom.Compiler")),
- UsingDirective(IdentifierName("System.ComponentModel")),
- UsingDirective(IdentifierName("System.Diagnostics")),
- UsingDirective(IdentifierName("System.Diagnostics.CodeAnalysis")),
- UsingDirective(IdentifierName("System.Runtime.CompilerServices")),
- UsingDirective(IdentifierName("ComputeSharp.__Internals"))).AddMembers(
+ CompilationUnit().AddUsings(usingDirectives.ToArray()).AddMembers(
NamespaceDeclaration(IdentifierName(namespaceName)).AddMembers(typeDeclarationSyntax)
.WithNamespaceKeyword(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.NamespaceKeyword, TriviaList())))
.NormalizeWhitespace(eol: "\n")
@@ -186,13 +196,15 @@ namespace ComputeSharp.SourceGenerators
/// The for .
/// The implicit texture type, if available (if the shader is a pixel shader).
/// Whether or not the current shader type requires a static sampler to be available.
+ /// Indicates whether or not the directive for is needed.
/// The resulting instance for the BuildHlslString method.
private static partial MethodDeclarationSyntax CreateBuildHlslStringMethod(
GeneratorExecutionContext context,
StructDeclarationSyntax structDeclaration,
INamedTypeSymbol structDeclarationSymbol,
out string? implicitTextureType,
- out bool isSamplerUsed);
+ out bool isSamplerUsed,
+ out bool isHashSetDirectiveNeeded);
///
/// Creates a instance for the LoadDispatchMetadata method.