diff --git a/src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs b/src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs index 3b82188e..77f693d5 100644 --- a/src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs +++ b/src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; +using System.Reflection; using System.Runtime.CompilerServices; namespace ComputeSharp.Shaders.Extensions @@ -20,6 +21,14 @@ namespace ComputeSharp.Shaders.Extensions public static bool IsDelegate(this Type type) => type.IsClass && type.IsSubclassOf(typeof(Delegate)); + /// + /// Gets whether or not the input represents a with no captured variables + /// + /// The input type to analyze + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsStatelessDelegateContainer(this Type type) => type.GetFields(BindingFlags.Static | BindingFlags.Public).Length > 0; + /// /// The mapping of supported known types to display string /// diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 56c4f75b..30c5ddc4 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -160,7 +160,8 @@ namespace ComputeSharp.Shaders.Translation foreach (FieldInfo fieldInfo in action.Method.DeclaringType.GetFields()) { if (fieldInfo.FieldType.IsDelegate() && - fieldInfo.GetValue(action.Target) is Delegate func && func.Method.IsStatic && + fieldInfo.GetValue(action.Target) is Delegate func && + (func.Method.IsStatic || func.Method.DeclaringType.IsStatelessDelegateContainer()) && (HlslKnownTypes.IsKnownScalarType(func.Method.ReturnType) || HlslKnownTypes.IsKnownVectorType(func.Method.ReturnType)) && fieldInfo.FieldType.GenericTypeArguments.All(type => HlslKnownTypes.IsKnownScalarType(type) || HlslKnownTypes.IsKnownVectorType(type))) @@ -271,7 +272,8 @@ namespace ComputeSharp.Shaders.Translation } } else if (fieldType.IsDelegate() && - memberInfo.GetValue(Action.Target) is Delegate func && func.Method.IsStatic && + memberInfo.GetValue(Action.Target) is Delegate func && + (func.Method.IsStatic || func.Method.DeclaringType.IsStatelessDelegateContainer()) && (HlslKnownTypes.IsKnownScalarType(func.Method.ReturnType) || HlslKnownTypes.IsKnownVectorType(func.Method.ReturnType)) && fieldType.GenericTypeArguments.All(type => HlslKnownTypes.IsKnownScalarType(type) || HlslKnownTypes.IsKnownVectorType(type)))