From 4da6f12e7efc40b51ecba1e3d77bdfa70f5a7cf9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 5 Sep 2019 13:00:48 +0200 Subject: [PATCH 1/7] Initial support for static delegates in shaders --- .../Translation/ShaderLoader.cs | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 1c3c24cc..dd8bffd0 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; @@ -247,6 +246,30 @@ namespace ComputeSharp.Shaders.Translation LoadFieldInfo(fieldInfo, null, updatedParents); } } + else if (fieldType.IsClass && fieldType.IsGenericType && + fieldType.IsSubclassOf(typeof(Delegate)) && + memberInfo.GetValue(Action.Target) is Delegate func && func.Method.IsStatic && + fieldType.GetGenericTypeDefinition() is Type fieldTypeDefinition && ( + fieldTypeDefinition == typeof(Func<>) || + fieldTypeDefinition == typeof(Func<,>) || + fieldTypeDefinition == typeof(Func<,,>) || + fieldTypeDefinition == typeof(Func<,,,>) || + fieldTypeDefinition == typeof(Func<,,,,>) || + fieldTypeDefinition == typeof(Func<,,,,,>) || + fieldTypeDefinition == typeof(Func<,,,,,,>) || + fieldTypeDefinition == typeof(Func<,,,,,,,>) || + fieldTypeDefinition == typeof(Func<,,,,,,,,>) || + fieldTypeDefinition == typeof(Func<,,,,,,,,,>) || + fieldTypeDefinition == typeof(Func<,,,,,,,,,,>) || + fieldTypeDefinition == typeof(Func<,,,,,,,,,,,>) || + fieldTypeDefinition == typeof(Func<,,,,,,,,,,,,>) || + fieldTypeDefinition == typeof(Func<,,,,,,,,,,,,,>) || + fieldTypeDefinition == typeof(Func<,,,,,,,,,,,,,,>) || + fieldTypeDefinition == typeof(Func<,,,,,,,,,,,,,,,>) || + fieldTypeDefinition == typeof(Func<,,,,,,,,,,,,,,,,>))) + { + LoadStaticMethodSource(fieldName, func.Method); + } } /// From f50e25c914dbf5da78d9f06c0c85a58acf1f9e5b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 5 Sep 2019 13:05:27 +0200 Subject: [PATCH 2/7] Added type parameters validation for captured functions --- src/ComputeSharp.Shaders/Translation/ShaderLoader.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index dd8bffd0..6c727f3f 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -266,10 +266,14 @@ namespace ComputeSharp.Shaders.Translation fieldTypeDefinition == typeof(Func<,,,,,,,,,,,,,>) || fieldTypeDefinition == typeof(Func<,,,,,,,,,,,,,,>) || fieldTypeDefinition == typeof(Func<,,,,,,,,,,,,,,,>) || - fieldTypeDefinition == typeof(Func<,,,,,,,,,,,,,,,,>))) + fieldTypeDefinition == typeof(Func<,,,,,,,,,,,,,,,,>)) && + fieldType.GenericTypeArguments.All(type => HlslKnownTypes.IsKnownScalarType(type) || + HlslKnownTypes.IsKnownVectorType(type))) { + // Captured static Func (and available generic type combinations) LoadStaticMethodSource(fieldName, func.Method); } + else throw new InvalidOperationException($"Invalid captured variable \"{fieldName}\" of type {fieldType}"); } /// From f7de2a2c65ef51499a14256bd5ab5738677bcf77 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 5 Sep 2019 13:27:02 +0200 Subject: [PATCH 3/7] Removed unnecessary exception in the shader loader --- src/ComputeSharp.Shaders/Translation/ShaderLoader.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 6c727f3f..4c20e6b7 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -273,7 +273,6 @@ namespace ComputeSharp.Shaders.Translation // Captured static Func (and available generic type combinations) LoadStaticMethodSource(fieldName, func.Method); } - else throw new InvalidOperationException($"Invalid captured variable \"{fieldName}\" of type {fieldType}"); } /// From 541f0b425cdd812e2766982ead1ff433c960076e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 5 Sep 2019 13:38:49 +0200 Subject: [PATCH 4/7] Added tests for captured functions --- tests/ComputeSharp.Tests/IntrinsicTests.cs | 2 +- .../StaticFunctionsTests.cs | 38 +++++++++++++++++++ .../ComputeSharp.Tests/StaticMethodsTests.cs | 2 +- 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 tests/ComputeSharp.Tests/StaticFunctionsTests.cs 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..d10a5d5e --- /dev/null +++ b/tests/ComputeSharp.Tests/StaticFunctionsTests.cs @@ -0,0 +1,38 @@ +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); + } + } +} diff --git a/tests/ComputeSharp.Tests/StaticMethodsTests.cs b/tests/ComputeSharp.Tests/StaticMethodsTests.cs index a2123fd8..bcb0eebb 100644 --- a/tests/ComputeSharp.Tests/StaticMethodsTests.cs +++ b/tests/ComputeSharp.Tests/StaticMethodsTests.cs @@ -27,7 +27,7 @@ namespace ComputeSharp.Tests } [TestClass] - [TestCategory("StaticMethodsTests")] + [TestCategory("StaticMethods")] public class StaticMethodsTests { [TestMethod] From 2c44c8fdbdc443c244a210f3a2ac7d204b6e6e20 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 5 Sep 2019 14:47:01 +0200 Subject: [PATCH 5/7] Added support for all types of delegates with a return type --- .../Translation/ShaderLoader.cs | 21 ++----------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 4c20e6b7..16803a60 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -249,28 +249,11 @@ namespace ComputeSharp.Shaders.Translation else if (fieldType.IsClass && fieldType.IsGenericType && fieldType.IsSubclassOf(typeof(Delegate)) && memberInfo.GetValue(Action.Target) is Delegate func && func.Method.IsStatic && - fieldType.GetGenericTypeDefinition() is Type fieldTypeDefinition && ( - fieldTypeDefinition == typeof(Func<>) || - fieldTypeDefinition == typeof(Func<,>) || - fieldTypeDefinition == typeof(Func<,,>) || - fieldTypeDefinition == typeof(Func<,,,>) || - fieldTypeDefinition == typeof(Func<,,,,>) || - fieldTypeDefinition == typeof(Func<,,,,,>) || - fieldTypeDefinition == typeof(Func<,,,,,,>) || - fieldTypeDefinition == typeof(Func<,,,,,,,>) || - fieldTypeDefinition == typeof(Func<,,,,,,,,>) || - fieldTypeDefinition == typeof(Func<,,,,,,,,,>) || - fieldTypeDefinition == typeof(Func<,,,,,,,,,,>) || - fieldTypeDefinition == typeof(Func<,,,,,,,,,,,>) || - fieldTypeDefinition == typeof(Func<,,,,,,,,,,,,>) || - fieldTypeDefinition == typeof(Func<,,,,,,,,,,,,,>) || - fieldTypeDefinition == typeof(Func<,,,,,,,,,,,,,,>) || - fieldTypeDefinition == typeof(Func<,,,,,,,,,,,,,,,>) || - fieldTypeDefinition == typeof(Func<,,,,,,,,,,,,,,,,>)) && + (HlslKnownTypes.IsKnownScalarType(func.Method.ReturnType) || HlslKnownTypes.IsKnownVectorType(func.Method.ReturnType)) && fieldType.GenericTypeArguments.All(type => HlslKnownTypes.IsKnownScalarType(type) || HlslKnownTypes.IsKnownVectorType(type))) { - // Captured static Func (and available generic type combinations) + // Captured static delegates with a return type LoadStaticMethodSource(fieldName, func.Method); } } From 146b98ea89d3c029cea4b1c6305a5feb607f38a7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 5 Sep 2019 15:11:03 +0200 Subject: [PATCH 6/7] Improved cache system for shaders with captured delegates --- src/ComputeSharp.Shaders/ShaderRunner.cs | 5 ++-- .../Translation/ShaderLoader.cs | 25 ++++++++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) 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) || From acb0d2b17baf54ffc4791e8f692444fa96e9d455 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 5 Sep 2019 15:11:42 +0200 Subject: [PATCH 7/7] Added tests for mutable captured delegates in a shader --- .../StaticFunctionsTests.cs | 41 +++++++++++++++++++ .../ComputeSharp.Tests/StaticMethodsTests.cs | 3 ++ 2 files changed, 44 insertions(+) diff --git a/tests/ComputeSharp.Tests/StaticFunctionsTests.cs b/tests/ComputeSharp.Tests/StaticFunctionsTests.cs index d10a5d5e..5a5ad8b7 100644 --- a/tests/ComputeSharp.Tests/StaticFunctionsTests.cs +++ b/tests/ComputeSharp.Tests/StaticFunctionsTests.cs @@ -34,5 +34,46 @@ namespace ComputeSharp.Tests 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 bcb0eebb..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;