diff --git a/src/ComputeSharp.Core/Attributes/AutoConstructorAttribute.cs b/src/ComputeSharp.Core/Attributes/AutoConstructorAttribute.cs index c5db9978..bd4dc3e7 100644 --- a/src/ComputeSharp.Core/Attributes/AutoConstructorAttribute.cs +++ b/src/ComputeSharp.Core/Attributes/AutoConstructorAttribute.cs @@ -5,7 +5,7 @@ namespace ComputeSharp /// /// An attribute that indicates that a target shader type should get an automatic constructor for all fields. /// - [AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] + [AttributeUsage(AttributeTargets.Struct)] public sealed class AutoConstructorAttribute : Attribute { } diff --git a/src/ComputeSharp.Core/Attributes/IComputeShaderSourceAttribute.cs b/src/ComputeSharp.Core/Attributes/IComputeShaderSourceAttribute.cs index 78a86340..b1db45a8 100644 --- a/src/ComputeSharp.Core/Attributes/IComputeShaderSourceAttribute.cs +++ b/src/ComputeSharp.Core/Attributes/IComputeShaderSourceAttribute.cs @@ -8,9 +8,9 @@ using System.Reflection; namespace ComputeSharp { /// - /// An attribute that indicates that a target shader type should get an automatic constructor for all fields. - /// This attribute is not meant to be directly used by applications using ComputeSharp. + /// An attribute that contains info on a processed compute shader that can be executed. /// + /// This attribute is not meant to be directly used by applications using ComputeSharp. [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This attribute is meant to be used from the source generator only")] diff --git a/src/ComputeSharp.Core/Attributes/ShaderMethodAttribute.cs b/src/ComputeSharp.Core/Attributes/ShaderMethodAttribute.cs new file mode 100644 index 00000000..9eae4f37 --- /dev/null +++ b/src/ComputeSharp.Core/Attributes/ShaderMethodAttribute.cs @@ -0,0 +1,16 @@ +using System; + +namespace ComputeSharp +{ + /// + /// An attribute that indicates that a method will be indirectly used in a compute shader. + /// This method is necessary for methods that are captured and used within a given shader + /// through a field (of some custom type). In order to be processed + /// correctly, wrapped methods need to have this attribute manually applied to them. + /// Methods also need to be static, though this can only be tested at runtime. + /// + [AttributeUsage(AttributeTargets.Method)] + public sealed class ShaderMethodAttribute : Attribute + { + } +} \ No newline at end of file diff --git a/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs b/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs new file mode 100644 index 00000000..32eec734 --- /dev/null +++ b/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics.Contracts; +using System.Linq; +using System.Reflection; +using Microsoft.Toolkit.Diagnostics; + +namespace ComputeSharp +{ + /// + /// An attribute that contains info on a processed shader method that can be executed within a shader. + /// Instances of this attribute are generated from method annotated with . + /// + /// This attribute is not meant to be directly used by applications using ComputeSharp. + [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This attribute is meant to be used from the source generator only")] + public sealed class ShaderMethodSourceAttribute : Attribute + { + /// + /// Creates a new instance with the specified parameters. + /// + /// The fully qualified name of the current method. + /// The collection of custom types. + /// The collection of processed methods. + public ShaderMethodSourceAttribute(string methodName, string[] types, string[] methods) + { + MethodName = methodName; + Types = types; + Methods = methods; + } + + /// + /// Gets the fully qualified name of the shader type. + /// + internal string MethodName { get; } + + /// + /// Gets the collection of processed custom types. + /// + internal IReadOnlyCollection Types { get; } + + /// + /// Gets the collection of processed methods. + /// + internal IReadOnlyCollection Methods { get; } + + /// + /// Gets the associated instance for a specified type. + /// + /// The shader type to get the attribute for. + /// The associated instance for type . + [Pure] + internal static ShaderMethodSourceAttribute GetForDelegate(Delegate function) + { + Guard.IsTrue(function.Method.IsStatic, "Captured delegates need to wrap static methods"); + + Type declaringType = function.Method.DeclaringType; + string methodName = $"{declaringType.FullName}.{function.Method.Name}"; + + return ( + from attribute in function.Method.DeclaringType.Assembly.GetCustomAttributes() + where attribute.MethodName == methodName + select attribute).Single(); + } + } +} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs index ea60def7..fdf653bb 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs @@ -111,6 +111,10 @@ namespace ComputeSharp.SourceGenerators foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) { 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);