From 64c39499621a1368c7e21445dedf47764e1f5478 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 16 Sep 2019 03:25:51 +0200 Subject: [PATCH] Moved code to new hashcode implementation --- src/ComputeSharp.Shaders/ShaderRunner.cs | 2 +- .../Translation/ShaderLoader.cs | 25 ------------------- .../ShaderHashCodeTests.cs | 12 ++++----- 3 files changed, 7 insertions(+), 32 deletions(-) diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index 40da9b5e..944aee43 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -64,7 +64,7 @@ namespace ComputeSharp.Shaders CachedShader shaderData; lock (ShadersCache) { - var key = (ShaderLoader.GetHashCode(action), threadsX, threadsY, threadsZ); + var key = (ShaderHashCodeProvider.GetHashCode(action), threadsX, threadsY, threadsZ); if (!ShadersCache.TryGetValue(key, out shaderData)) { // Load the input shader diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 7c6faaa8..ed75fcee 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -147,31 +147,6 @@ namespace ComputeSharp.Shaders.Translation /// public IReadOnlyList LocalFunctionsList => _LocalFunctionsList; - /// - /// 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.IsDelegate() && - 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))) - { - hashcode = unchecked(hashcode * 17 + func.Method.GetHashCode()); - } - } - - return hashcode; - } - /// /// Loads and processes an input /// diff --git a/tests/ComputeSharp.Tests.Internals/ShaderHashCodeTests.cs b/tests/ComputeSharp.Tests.Internals/ShaderHashCodeTests.cs index b8d06d9a..bd0dfa99 100644 --- a/tests/ComputeSharp.Tests.Internals/ShaderHashCodeTests.cs +++ b/tests/ComputeSharp.Tests.Internals/ShaderHashCodeTests.cs @@ -17,14 +17,14 @@ namespace ComputeSharp.Tests.Internals Action action1 = id => buffer[0] = value; int - hash1 = ShaderLoader.GetHashCode(action1), - hash2 = ShaderLoader.GetHashCode(action1); + hash1 = ShaderHashCodeProvider.GetHashCode(action1), + hash2 = ShaderHashCodeProvider.GetHashCode(action1); Assert.IsTrue(hash1 == hash2); Action action2 = id => buffer[0] = value; - int hash3 = ShaderLoader.GetHashCode(action2); + int hash3 = ShaderHashCodeProvider.GetHashCode(action2); Assert.IsFalse(hash1 == hash3); } @@ -39,14 +39,14 @@ namespace ComputeSharp.Tests.Internals Action action = id => buffer[0] = f(2); int - hash1 = ShaderLoader.GetHashCode(action), - hash2 = ShaderLoader.GetHashCode(action); + hash1 = ShaderHashCodeProvider.GetHashCode(action), + hash2 = ShaderHashCodeProvider.GetHashCode(action); Assert.IsTrue(hash1 == hash2); f = x => x + 1; - int hash3 = ShaderLoader.GetHashCode(action); + int hash3 = ShaderHashCodeProvider.GetHashCode(action); Assert.IsFalse(hash1 == hash3); }