diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index 2815ce8b..c149e409 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Reflection; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Buffers.Extensions; @@ -41,7 +40,7 @@ namespace ComputeSharp.Shaders /// /// The mapping used to cache and reuse compiled shaders /// - private static readonly Dictionary<(MethodInfo Method, int ThreadsX, int ThreadsY, int ThreadsZ), (ShaderLoader Loader, ShaderBytecode Bytecode)> ShadersCache = new Dictionary<(MethodInfo, int, int, int), (ShaderLoader, ShaderBytecode)>(); + private static readonly Dictionary<(int Id, int ThreadsX, int ThreadsY, int ThreadsZ), (ShaderLoader Loader, ShaderBytecode Bytecode)> ShadersCache = new Dictionary<(int, int, int, int), (ShaderLoader, ShaderBytecode)>(); /// /// Compiles and runs the input shader on a target instance, with the specified parameters @@ -61,7 +60,7 @@ namespace ComputeSharp.Shaders Action action) { // Try to get the cache shader - var key = (action.Method, threadsX, threadsY, threadsZ); + var key = (ShaderLoader.GetHashCode(action), threadsX, threadsY, threadsZ); if (!ShadersCache.TryGetValue(key, out var shaderData)) { // Load the input shader diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 16803a60..ee36936e 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -148,6 +148,30 @@ namespace ComputeSharp.Shaders.Translation /// public IReadOnlyList FunctionsList => _FunctionsList; + /// + /// Gets a unique hash code for a given + /// + /// The input instance to inspect + [Pure] + public static int GetHashCode(Action action) + { + int hashcode = action.Method.GetHashCode(); + + foreach (FieldInfo fieldInfo in action.Method.DeclaringType.GetFields()) + { + if (fieldInfo.FieldType.IsClass && fieldInfo.FieldType.IsGenericType && + fieldInfo.GetValue(action.Target) is Delegate func && func.Method.IsStatic && + (HlslKnownTypes.IsKnownScalarType(func.Method.ReturnType) || HlslKnownTypes.IsKnownVectorType(func.Method.ReturnType)) && + fieldInfo.FieldType.GenericTypeArguments.All(type => HlslKnownTypes.IsKnownScalarType(type) || + HlslKnownTypes.IsKnownVectorType(type))) + { + hashcode = unchecked(hashcode * 17 + func.Method.GetHashCode()); + } + } + + return hashcode; + } + /// /// Loads and processes an input /// @@ -247,7 +271,6 @@ namespace ComputeSharp.Shaders.Translation } } else if (fieldType.IsClass && fieldType.IsGenericType && - fieldType.IsSubclassOf(typeof(Delegate)) && memberInfo.GetValue(Action.Target) is Delegate func && func.Method.IsStatic && (HlslKnownTypes.IsKnownScalarType(func.Method.ReturnType) || HlslKnownTypes.IsKnownVectorType(func.Method.ReturnType)) && fieldType.GenericTypeArguments.All(type => HlslKnownTypes.IsKnownScalarType(type) ||