From ae0738441765d86e008ad82b909a4a2d62f2e8ec Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 7 Jul 2021 14:20:52 +0200 Subject: [PATCH] Removed ShaderHashCodeProvider, switched to interface method --- .../GraphicsDeviceExtensions.Dispatching.cs | 20 +++-- src/ComputeSharp/Shaders/ShaderRunner{T}.cs | 6 +- .../Translation/ShaderHashCodeProvider.cs | 75 ------------------- .../ComputeSharp.Tests/ShaderMembersTests.cs | 1 - 4 files changed, 12 insertions(+), 90 deletions(-) delete mode 100644 src/ComputeSharp/Shaders/Translation/ShaderHashCodeProvider.cs diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs index 543ec018..25b55a2b 100644 --- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs +++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs @@ -1,9 +1,7 @@ -using System; -using System.Diagnostics.Contracts; +using ComputeSharp.Shaders; using ComputeSharp.__Internals; -using ComputeSharp.Graphics.Helpers; -using ComputeSharp.Resources; -using ComputeSharp.Shaders; + +#pragma warning disable CS0618 namespace ComputeSharp { @@ -20,7 +18,7 @@ namespace ComputeSharp /// The number of iterations to run on the X axis. /// The input instance representing the compute shader to run. public static void For(this GraphicsDevice device, int x, in T shader) - where T : struct, IComputeShader + where T : struct, IComputeShader, IShader { ShaderRunner.Run(device, x, 1, 1, in shader); } @@ -34,7 +32,7 @@ namespace ComputeSharp /// The number of iterations to run on the Y axis. /// The input instance representing the compute shader to run. public static void For(this GraphicsDevice device, int x, int y, in T shader) - where T : struct, IComputeShader + where T : struct, IComputeShader, IShader { ShaderRunner.Run(device, x, y, 1, in shader); } @@ -49,7 +47,7 @@ namespace ComputeSharp /// The number of iterations to run on the Z axis. /// The input instance representing the compute shader to run. public static void For(this GraphicsDevice device, int x, int y, int z, in T shader) - where T : struct, IComputeShader + where T : struct, IComputeShader, IShader { ShaderRunner.Run(device, x, y, z, in shader); } @@ -67,7 +65,7 @@ namespace ComputeSharp /// The number of threads in each thread group for the Z axis. /// The input instance representing the compute shader to run. public static void For(this GraphicsDevice device, int x, int y, int z, int threadsX, int threadsY, int threadsZ, in T shader) - where T : struct, IComputeShader + where T : struct, IComputeShader, IShader { ShaderRunner.Run(device, x, y, z, threadsX, threadsY, threadsZ, in shader); } @@ -80,7 +78,7 @@ namespace ComputeSharp /// The to use to run the shader. /// The target texture to apply the pixel shader to. public static void ForEach(this GraphicsDevice device, IReadWriteTexture2D texture) - where T : struct, IPixelShader + where T : struct, IPixelShader, IShader where TPixel : unmanaged { ShaderRunner.Run(device, texture, default); @@ -95,7 +93,7 @@ namespace ComputeSharp /// The target texture to apply the pixel shader to. /// The input instance representing the pixel shader to run. public static void ForEach(this GraphicsDevice device, IReadWriteTexture2D texture, in T shader) - where T : struct, IPixelShader + where T : struct, IPixelShader, IShader where TPixel : unmanaged { ShaderRunner.Run(device, texture, in shader); diff --git a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs index 220b2c2c..40538161 100644 --- a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs @@ -23,7 +23,7 @@ namespace ComputeSharp.Shaders /// /// The type of compute shader to run. internal static class ShaderRunner - where T : struct + where T : struct, IShader { /// /// The mapping used to cache and reuse compiled shaders. @@ -131,7 +131,7 @@ namespace ComputeSharp.Shaders Guard.IsBetweenOrEqualTo(groupsZ, 1, FX.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); // Create the shader key - ShaderKey key = new(ShaderHashCodeProvider.GetHashCode(in shader), threadsX, threadsY, threadsZ); + ShaderKey key = new(shader.GetDispatchId(), threadsX, threadsY, threadsZ); CachedShader shaderData; PipelineData? pipelineData; @@ -202,7 +202,7 @@ namespace ComputeSharp.Shaders Guard.IsBetweenOrEqualTo(groupsY, 1, FX.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); // Create the shader key - ShaderKey key = new(ShaderHashCodeProvider.GetHashCode(in shader), threadsX, threadsY, 1); + ShaderKey key = new(shader.GetDispatchId(), threadsX, threadsY, 1); CachedShader shaderData; PipelineData? pipelineData; diff --git a/src/ComputeSharp/Shaders/Translation/ShaderHashCodeProvider.cs b/src/ComputeSharp/Shaders/Translation/ShaderHashCodeProvider.cs deleted file mode 100644 index 2304eba6..00000000 --- a/src/ComputeSharp/Shaders/Translation/ShaderHashCodeProvider.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System; -using System.Diagnostics.Contracts; -using System.Reflection; -using System.Runtime.CompilerServices; - -namespace ComputeSharp.Shaders.Translation -{ - /// - /// A that handles the cache system for the generated shaders. - /// - internal static class ShaderHashCodeProvider - { - /// - /// A type containing static info for a given shader type . - /// - /// The type of compute shader currently in use. - private static class TypeInfo - where T : struct - { - /// - /// The id of the compute shader type in use. - /// - public static readonly int Id; - - /// - /// The optional instance, if needed. - /// - public static readonly Hasher? Hasher; - - /// - /// Initializes the static members of the type. - /// - static TypeInfo() - { - Id = HashCode.Combine(typeof(T)); - - // Get the generated hasher, if one if present - if (typeof(T).Assembly.GetType("ComputeSharp.__Internals.HashCodeProvider") is Type type && - type.GetMethod("CombineHash", new[] { typeof(int), typeof(T).MakeByRefType() }) is MethodInfo method) - { - Hasher = method.CreateDelegate>(); - } - } - } - - /// - /// Gets a unique hashcode for the input compute shader instance. - /// - /// The type of compute shader currently in use. - /// The input instance representing the shader to run. - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int GetHashCode(in T shader) - where T : struct - { - int hash = TypeInfo.Id; - - Hasher? hasher = TypeInfo.Hasher; - - if (hasher != null) hash = hasher(hash, in shader); - - return hash; - } - - /// - /// A that represents an aggregate hash function for a given instance. - /// - /// The type of compute shader currently in use. - /// The initial hash value. - /// The compute shader instance to use to compute the final hash value. - /// The final hash value for the input closure. - private delegate int Hasher(int hash, in T shader) - where T : struct; - } -} diff --git a/tests/ComputeSharp.Tests/ShaderMembersTests.cs b/tests/ComputeSharp.Tests/ShaderMembersTests.cs index cff85204..00d40cf7 100644 --- a/tests/ComputeSharp.Tests/ShaderMembersTests.cs +++ b/tests/ComputeSharp.Tests/ShaderMembersTests.cs @@ -1,4 +1,3 @@ -using System; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting;