From 78265819807836eee4b3f85672de1d307cb1df90 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 16 Sep 2022 14:38:49 +0200 Subject: [PATCH] Fix dispatch [numthreads] values for ND dispatches --- .../GraphicsDeviceExtensions.Dispatching.cs | 4 +- src/ComputeSharp/Shaders/ComputeContext.cs | 76 +++++++------------ .../Extensions/ComputeContextExtensions.cs | 4 +- 3 files changed, 31 insertions(+), 53 deletions(-) diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs index bf7c5ee9..4b85af2b 100644 --- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs +++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs @@ -22,7 +22,7 @@ public static partial class GraphicsDeviceExtensions using ComputeContext context = device.CreateComputeContext(); - context.Run(x, 1, 1, ref Unsafe.AsRef(in shader)); + context.Run(x, ref Unsafe.AsRef(in shader)); } /// @@ -40,7 +40,7 @@ public static partial class GraphicsDeviceExtensions using ComputeContext context = device.CreateComputeContext(); - context.Run(x, y, 1, ref Unsafe.AsRef(in shader)); + context.Run(x, y, ref Unsafe.AsRef(in shader)); } /// diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index d7021e83..ef62fe51 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; +using System.Threading; using System.Threading.Tasks; using CommunityToolkit.Diagnostics; using ComputeSharp.__Internals; @@ -119,6 +120,31 @@ public struct ComputeContext : IDisposable, IAsyncDisposable commandList.D3D12GraphicsCommandList->FillUnorderedAccessView(d3D12Resource, d3D12GpuDescriptorHandle, d3D12CpuDescriptorHandle, value); } + /// + /// Runs the input shader with the specified parameters. + /// + /// The number of iterations to run on the X axis. + /// The input instance representing the compute shader to run. + internal readonly unsafe void Run(int x, ref T shader) + where T : struct, IComputeShader + { + + Run(x, 1, 1, 64, 1, 1, ref shader); + } + + /// + /// Runs the input shader with the specified parameters. + /// + /// The number of iterations to run on the X axis. + /// The number of iterations to run on the Y axis. + /// The input instance representing the compute shader to run. + internal readonly unsafe void Run(int x, int y, ref T shader) + where T : struct, IComputeShader + { + + Run(x, y, 1, 8, 8, 1, ref shader); + } + /// /// Runs the input shader with the specified parameters. /// @@ -129,56 +155,8 @@ public struct ComputeContext : IDisposable, IAsyncDisposable internal readonly unsafe void Run(int x, int y, int z, ref T shader) where T : struct, IComputeShader { - ThrowInvalidOperationExceptionIfDeviceIsNull(); - // Here we calculate the optimized [numthreads] values. Using small thread group sizes leads to the - // best average performance due to better occupancy of the GPU with different shaders, using any - // number of registers and any amount of thread local storage. We use 64 for 1D dispatches, otherwise - // a multiple of 32 that is greater than or equal to 64, which still results in an evenly divisible - // number of waves per thread group on all existing GPU devices. All GPUs will generally have a wavefront - // size of either 16 (eg. Intel mobile GPUs), 32 (nvidia GPUs) or 64 (AMD GPUs), so in all cases 64 will - // be a multiple of that, guaranteeing that all thread waves will be saturated when dispatching shaders. - - bool xIs1 = x == 1; - bool yIs1 = y == 1; - bool zIs1 = z == 1; - int mask = *(byte*)&xIs1 << 2 | *(byte*)&yIs1 << 1 | *(byte*)&zIs1; - int threadsX; - int threadsY; - int threadsZ; - - switch (mask - 1) - { - case 0: // (_, _, 1) - threadsX = threadsY = 8; - threadsZ = 1; - break; - case 1: // (_, 1, _) - threadsX = threadsZ = 8; - threadsY = 1; - break; - case 2: // (_, 1, 1) - threadsX = 64; - threadsY = threadsZ = 1; - break; - case 3: // (1, _, _) - threadsX = 1; - threadsY = threadsZ = 8; - break; - case 4: // (1, _, 1) - threadsX = threadsZ = 1; - threadsY = 64; - break; - case 5: // (1, 1, _) - threadsX = threadsY = 1; - threadsZ = 64; - break; - default: // (_, _, _) - threadsX = threadsY = threadsZ = 4; - break; - } - - Run(x, y, z, threadsX, threadsY, threadsZ, ref shader); + Run(x, y, z, 4, 4, 4, ref shader); } /// diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs index 1cc635f9..8b9c6ce5 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -488,7 +488,7 @@ public static class ComputeContextExtensions public static void For(this in ComputeContext context, int x, in T shader) where T : struct, IComputeShader { - context.Run(x, 1, 1, ref Unsafe.AsRef(in shader)); + context.Run(x, ref Unsafe.AsRef(in shader)); } /// @@ -502,7 +502,7 @@ public static class ComputeContextExtensions public static void For(this in ComputeContext context, int x, int y, in T shader) where T : struct, IComputeShader { - context.Run(x, y, 1, ref Unsafe.AsRef(in shader)); + context.Run(x, y, ref Unsafe.AsRef(in shader)); } ///