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 1c3c24cc..ee36936e 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -13,7 +13,6 @@ using ComputeSharp.Shaders.Renderer.Models.Functions; using ComputeSharp.Shaders.Translation.Enums; using ComputeSharp.Shaders.Translation.Models; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Vortice.Direct3D12; using ParameterInfo = ComputeSharp.Shaders.Renderer.Models.Functions.ParameterInfo; @@ -149,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,6 +270,15 @@ namespace ComputeSharp.Shaders.Translation LoadFieldInfo(fieldInfo, null, updatedParents); } } + else if (fieldType.IsClass && fieldType.IsGenericType && + 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) || + HlslKnownTypes.IsKnownVectorType(type))) + { + // Captured static delegates with a return type + LoadStaticMethodSource(fieldName, func.Method); + } } /// diff --git a/tests/ComputeSharp.Tests/IntrinsicTests.cs b/tests/ComputeSharp.Tests/IntrinsicTests.cs index 7f0e4916..d6cd3365 100644 --- a/tests/ComputeSharp.Tests/IntrinsicTests.cs +++ b/tests/ComputeSharp.Tests/IntrinsicTests.cs @@ -4,7 +4,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests { [TestClass] - [TestCategory("IntrinsicTests")] + [TestCategory("Intrinsics")] public class IntrinsicTests { [TestMethod] diff --git a/tests/ComputeSharp.Tests/StaticFunctionsTests.cs b/tests/ComputeSharp.Tests/StaticFunctionsTests.cs new file mode 100644 index 00000000..5a5ad8b7 --- /dev/null +++ b/tests/ComputeSharp.Tests/StaticFunctionsTests.cs @@ -0,0 +1,79 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests +{ + [TestClass] + [TestCategory("StaticFunctions")] + public class StaticFunctionsTests + { + [TestMethod] + public void FloatToFloatFunc() + { + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(1); + + Func square = StaticMethodsContainer.Square; + + Gpu.Default.For(1, id => buffer[0] = square(3)); + + float[] result = buffer.GetData(); + + Assert.IsTrue(MathF.Abs(result[0] - 9) < 0.0001f); + } + + [TestMethod] + public void InternalFloatToFloatFunc() + { + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(1); + + Func square = StaticMethodsContainer.InternalSquare; + + Gpu.Default.For(1, id => buffer[0] = square(3)); + + float[] result = buffer.GetData(); + + Assert.IsTrue(MathF.Abs(result[0] - 9) < 0.0001f); + } + + [TestMethod] + public void ChangingFuncsFromSameMethod() + { + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(1); + + Func func = StaticMethodsContainer.Square; + + Gpu.Default.For(1, id => buffer[0] = func(3)); + + float[] result = buffer.GetData(); + + Assert.IsTrue(MathF.Abs(result[0] - 9) < 0.0001f); + + func = StaticMethodsContainer.Negate; + + Gpu.Default.For(1, id => buffer[0] = func(3)); + + result = buffer.GetData(); + + Assert.IsTrue(MathF.Abs(result[0] + 3) < 0.0001f); + } + + [TestMethod] + public void ChangingFuncsFromExternalMethod() + { + MethodRunner(StaticMethodsContainer.Square, 3, 9); + + MethodRunner(StaticMethodsContainer.Negate, 3, -3); + } + + private void MethodRunner(Func func, float input, float expected) + { + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(1); + + Gpu.Default.For(1, id => buffer[0] = func(input)); + + float[] result = buffer.GetData(); + + Assert.IsTrue(MathF.Abs(result[0] - expected) < 0.0001f); + } + } +} diff --git a/tests/ComputeSharp.Tests/StaticMethodsTests.cs b/tests/ComputeSharp.Tests/StaticMethodsTests.cs index a2123fd8..3efd02a7 100644 --- a/tests/ComputeSharp.Tests/StaticMethodsTests.cs +++ b/tests/ComputeSharp.Tests/StaticMethodsTests.cs @@ -12,6 +12,9 @@ namespace ComputeSharp.Tests [Pure] public static float Square(float x) => x * x; + [Pure] + public static float Negate(float x) => -x; + [Pure] internal static float InternalSquare(float x) => x * x; @@ -27,7 +30,7 @@ namespace ComputeSharp.Tests } [TestClass] - [TestCategory("StaticMethodsTests")] + [TestCategory("StaticMethods")] public class StaticMethodsTests { [TestMethod]