diff --git a/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs b/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs index 1770ad27..26bfe745 100644 --- a/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs +++ b/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs @@ -1,8 +1,11 @@ -using System.Runtime.CompilerServices; +using System; +using System.Reflection; +using System.Reflection.Emit; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Microsoft.Toolkit.Diagnostics; -namespace System.Reflection.Emit +namespace ComputeSharp.Shaders.Extensions { /// /// A that provides extension methods for the type diff --git a/src/ComputeSharp.Shaders/Extensions/StringExtensions.cs b/src/ComputeSharp.Shaders/Extensions/StringExtensions.cs deleted file mode 100644 index 16f21407..00000000 --- a/src/ComputeSharp.Shaders/Extensions/StringExtensions.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System.Diagnostics.Contracts; -using System.Linq; -using System.Text.RegularExpressions; - -namespace System -{ - /// - /// A that provides extension methods for the type - /// - internal static class StringExtensions - { - /// - /// Removes the left padding for an input multiline - /// - /// The input to process - /// A left-aligned with the same text as the input - [Pure] - public static string RemoveLeftPadding(this string text) - { - string[] lines = text.Split(Environment.NewLine); - int spaces = text.TakeWhile(c => c == ' ').Count(); - Regex regex = new Regex($"^{new string(Enumerable.Repeat(' ', spaces).ToArray())}"); - - return lines.Aggregate(string.Empty, (result, line) => $"{result}{Environment.NewLine}{regex.Replace(line, string.Empty)}"); - } - } -} diff --git a/src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs b/src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs index 081a27c0..72771697 100644 --- a/src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs +++ b/src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs @@ -1,53 +1,21 @@ -using System.Diagnostics.CodeAnalysis; +using System; using System.Diagnostics.Contracts; -using System.Reflection; -using System.Runtime.CompilerServices; -namespace System +namespace ComputeSharp.Shaders.Extensions { /// - /// A that provides extension methods for the type + /// A that provides extension methods for the type. /// internal static class TypeExtensions { /// - /// Gets whether or not the input represents a + /// Gets whether or not the input represents a . /// - /// The input type to analyze + /// The input type to analyze. [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - 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; - - /// - /// Tries to find an ancestor member starting from the input - /// - /// The input type to start the search from - /// The name of the member to retrieve - /// A bitmask comprised of one or more that specify how the search is conducted - /// when a member with the given name is found, otherwise - public static bool TryFindAncestorMember(this Type type, string name, BindingFlags bindingAttr, [NotNullWhen(true)] out (Type DeclaringType, MemberInfo MemberInfo)? result) + public static bool IsDelegate(this Type type) { - while ((type = type.DeclaringType!) != null) - { - MemberInfo[] memberInfos = type.GetMember(name, bindingAttr); - if (memberInfos.Length > 0) - { - result = (type, memberInfos[0]); - return true; - } - } - - result = null; - return false; + return type.IsSubclassOf(typeof(Delegate)); } } } diff --git a/src/ComputeSharp.Shaders/Translation/ShaderHashCodeProvider.cs b/src/ComputeSharp.Shaders/Translation/ShaderHashCodeProvider.cs index 955ca45e..d4e617f3 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderHashCodeProvider.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderHashCodeProvider.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Reflection; using System.Reflection.Emit; using System.Runtime.CompilerServices; +using ComputeSharp.Shaders.Extensions; namespace ComputeSharp.Shaders.Translation { diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs index 781fa8d7..7da9b114 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs @@ -8,6 +8,7 @@ using System.Runtime.InteropServices; using ComputeSharp.Core.Interop; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Shaders.Extensions; using ComputeSharp.Shaders.Mappings; using ComputeSharp.Shaders.Translation.Models; using Microsoft.Toolkit.Diagnostics; diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs index 347bbcfc..091a1215 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs @@ -178,12 +178,7 @@ namespace ComputeSharp.Shaders.Translation this.capturedFields.Add(fieldInfo); this.fieldsInfo.Add(new CapturedFieldInfo(hlslType, hlslName)); } - else if (fieldType.IsDelegate() && - fieldInfo.GetValue(shader) 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))) + else if (fieldInfo.GetValue(shader) is Delegate { Method: { IsStatic: true } } func) { // Captured static delegates with a return type throw new NotImplementedException();