diff --git a/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs b/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs
index 32eec734..db6ce1d1 100644
--- a/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs
+++ b/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs
@@ -4,6 +4,7 @@ using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;
+using ComputeSharp.Core.Extensions;
using Microsoft.Toolkit.Diagnostics;
namespace ComputeSharp
@@ -23,11 +24,13 @@ namespace ComputeSharp
///
/// The fully qualified name of the current method.
/// The collection of custom types.
+ /// The source code for the target entry point method.
/// The collection of processed methods.
- public ShaderMethodSourceAttribute(string methodName, string[] types, string[] methods)
+ public ShaderMethodSourceAttribute(string methodName, string[] types, string invokeMethod, string[] methods)
{
MethodName = methodName;
Types = types;
+ InvokeMethod = invokeMethod;
Methods = methods;
}
@@ -41,6 +44,11 @@ namespace ComputeSharp
///
internal IReadOnlyCollection Types { get; }
+ ///
+ /// Gets the source code for the target entry point method.
+ ///
+ internal string InvokeMethod { get; }
+
///
/// Gets the collection of processed methods.
///
@@ -56,8 +64,7 @@ namespace ComputeSharp
{
Guard.IsTrue(function.Method.IsStatic, "Captured delegates need to wrap static methods");
- Type declaringType = function.Method.DeclaringType;
- string methodName = $"{declaringType.FullName}.{function.Method.Name}";
+ string methodName = function.Method.GetFullName();
return (
from attribute in function.Method.DeclaringType.Assembly.GetCustomAttributes()
diff --git a/src/ComputeSharp.Core/Extensions/MethodInfoExtensions.cs b/src/ComputeSharp.Core/Extensions/MethodInfoExtensions.cs
new file mode 100644
index 00000000..1a09359b
--- /dev/null
+++ b/src/ComputeSharp.Core/Extensions/MethodInfoExtensions.cs
@@ -0,0 +1,25 @@
+using System.Diagnostics.Contracts;
+using System.Linq;
+using System.Reflection;
+
+namespace ComputeSharp.Core.Extensions
+{
+ ///
+ /// A that provides extension methods for the type.
+ ///
+ internal static class MethodInfoExtensions
+ {
+ ///
+ /// Gets a fully qualified name for the input method, including parameters.
+ ///
+ /// The input instance.
+ /// A fully qualified name for , including parameters.
+ [Pure]
+ public static string GetFullName(this MethodInfo method)
+ {
+ var parameters = string.Join(", ", method.GetParameters().Select(static p => p.ParameterType.FullName));
+
+ return $"{method.DeclaringType!.FullName}.{method.Name}({parameters})";
+ }
+ }
+}
diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs
index 091a1215..97987103 100644
--- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs
+++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs
@@ -56,6 +56,16 @@ namespace ComputeSharp.Shaders.Translation
///
private readonly List fieldsInfo = new();
+ ///
+ /// The with the collected methods for the shader.
+ ///
+ private readonly List methodsInfo = new();
+
+ ///
+ /// The with the collected declared types for the shader.
+ ///
+ private readonly List declaredTypes = new();
+
///
/// Creates a new instance.
///
@@ -81,10 +91,10 @@ namespace ComputeSharp.Shaders.Translation
public IReadOnlyList FieldsInfo => this.fieldsInfo;
///
- public IReadOnlyCollection MethodsInfo { get; private set; }
+ public IReadOnlyCollection MethodsInfo => this.methodsInfo;
///
- public IReadOnlyCollection DeclaredTypes { get; private set; }
+ public IReadOnlyCollection DeclaredTypes => this.declaredTypes;
///
/// Loads and processes an input shadeer
@@ -181,7 +191,11 @@ namespace ComputeSharp.Shaders.Translation
else if (fieldInfo.GetValue(shader) is Delegate { Method: { IsStatic: true } } func)
{
// Captured static delegates with a return type
- throw new NotImplementedException();
+ var methodSource = ShaderMethodSourceAttribute.GetForDelegate(func);
+
+ this.declaredTypes.AddRange(methodSource.Types);
+ this.methodsInfo.Add(methodSource.InvokeMethod.Replace("____", hlslName));
+ this.methodsInfo.AddRange(methodSource.Methods);
}
else ThrowHelper.ThrowArgumentException("Invalid captured variable");
}
@@ -191,9 +205,10 @@ namespace ComputeSharp.Shaders.Translation
///
private void LoadMethodMetadata()
{
- DeclaredTypes = Attribute.Types;
EntryPoint = Attribute.ExecuteMethod;
- MethodsInfo = Attribute.Methods;
+
+ this.methodsInfo.AddRange(Attribute.Methods);
+ this.declaredTypes.AddRange(Attribute.Types);
}
}
}
diff --git a/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs
index 4cdc55aa..392a250c 100644
--- a/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs
+++ b/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs
@@ -56,7 +56,7 @@ namespace ComputeSharp.SourceGenerators.Extensions
{
if (includeParameters)
{
- var parameters = $"{string.Join(", ", symbol.Parameters.Select(static p => ((INamedTypeSymbol)p.Type).GetFullMetadataName()))}";
+ var parameters = string.Join(", ", symbol.Parameters.Select(static p => ((INamedTypeSymbol)p.Type).GetFullMetadataName()));
return $"{symbol.ToDisplayString(FullyQualifiedWithoutGlobalAndParametersFormat)}({parameters})";
}
diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
index cb8cefa9..59368f90 100644
--- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
@@ -112,9 +112,6 @@ namespace ComputeSharp.SourceGenerators
{
INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type;
- // Delegate types are not included and are processed separately
- if (typeSymbol.TypeKind == TypeKind.Delegate) continue;
-
string typeName = HlslKnownTypes.GetMappedName(typeSymbol);
_ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping);
diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs
index 6969ffc1..39cfc05a 100644
--- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs
+++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs
@@ -73,8 +73,13 @@ namespace ComputeSharp.SourceGenerators.Mappings
///
/// The input type to map.
/// The HLSL-compatible type name that can be used in an HLSL shader.
+ [Pure]
public static string GetMappedName(INamedTypeSymbol typeSymbol)
{
+ // Delegate types just return an empty string, as they're not actually
+ // used in the generated shaders, but just mapped to a function at runtime.
+ if (typeSymbol.TypeKind == TypeKind.Delegate) return "";
+
string typeName = typeSymbol.GetFullMetadataName();
// Special case for the structured buffer types
diff --git a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs
index 0d158099..7e6dae24 100644
--- a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs
@@ -45,7 +45,7 @@ namespace ComputeSharp.SourceGenerators
Dictionary staticMethods = new(SymbolEqualityComparer.Default);
// Explore the syntax tree and extract the processed info
- var processedMethods = GetProcessedMethods(methodDeclaration, semanticModel, discoveredTypes, staticMethods).ToArray();
+ var (invokeMethod, processedMethods) = GetProcessedMethods(methodDeclaration, semanticModel, discoveredTypes, staticMethods);
var processedTypes = IComputeShaderSourceGenerator.GetProcessedTypes(discoveredTypes).ToArray();
// Helper that converts a sequence of strings into an array expression.
@@ -68,6 +68,7 @@ namespace ComputeSharp.SourceGenerators
Attribute(IdentifierName(typeof(ShaderMethodSourceAttribute).FullName)).AddArgumentListArguments(
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(methodDeclarationSymbol.GetFullMetadataName(true)))),
AttributeArgument(ArrayExpression(processedTypes)),
+ AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(invokeMethod))),
AttributeArgument(ArrayExpression(processedMethods)))))
.WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList()))
.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword))))
@@ -88,7 +89,7 @@ namespace ComputeSharp.SourceGenerators
/// The set of discovered and processed static methods.
/// A sequence of processed methods in .
[Pure]
- private static IEnumerable GetProcessedMethods(
+ private static (string InvokeMethod, IEnumerable Methods) GetProcessedMethods(
MethodDeclarationSyntax methodDeclaration,
SemanticModel semanticModel,
ICollection discoveredTypes,
@@ -97,15 +98,22 @@ namespace ComputeSharp.SourceGenerators
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods);
// Rewrite the method syntax tree
- var processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia();
+ var processedMethod = shaderSourceRewriter
+ .Visit(methodDeclaration)!
+ .WithIdentifier(Identifier("____"))
+ .WithoutTrivia()
+ .NormalizeWhitespace()
+ .ToFullString();
- yield return processedMethod.NormalizeWhitespace().ToFullString();
+ List methods = new(shaderSourceRewriter.LocalFunctions.Count);
// Emit the extracted local functions
foreach (var localFunction in shaderSourceRewriter.LocalFunctions)
{
- yield return localFunction.Value.NormalizeWhitespace().ToFullString();
+ methods.Add(localFunction.Value.NormalizeWhitespace().ToFullString());
}
+
+ return (processedMethod, methods);
}
}
}