diff --git a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs index 0bbac8f9..ad13f33f 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs @@ -9,7 +9,9 @@ using ComputeSharp.Shaders.Renderer; using ComputeSharp.Shaders.Translation; using ComputeSharp.Shaders.Translation.Interop; using ComputeSharp.Shaders.Translation.Models; +using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; +using FX = TerraFX.Interop.Windows; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; namespace ComputeSharp.Shaders @@ -34,13 +36,53 @@ namespace ComputeSharp.Shaders /// The number of iterations to run on the Y axis. /// The number of iterations to run on the Z axis. /// The input instance representing the compute shader to run. - public static void Run(GraphicsDevice device, int x, int y, int z, in T shader) + public static unsafe void Run(GraphicsDevice device, int x, int y, int z, in T shader) { - // Calculate the optimized thread num values - int threadsX, threadsY = 1, threadsZ = 1; - if (y == 1 && z == 1) threadsX = (int)device.WavefrontSize; - else if (z == 1) threadsX = threadsY = 8; - else threadsX = threadsY = threadsZ = 4; + // 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 the wave size of the device in + // use 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. + bool + xIs1 = x == 1, + yIs1 = y == 1, + zIs1 = z == 1; + int + mask = *(byte*)&xIs1 << 2 | *(byte*)&yIs1 << 1 | *(byte*)&zIs1, + threadsX, + threadsY, + 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 = (int)device.WavefrontSize; + threadsY = threadsZ = 1; + break; + case 3: // (1, _, _) + threadsX = 1; + threadsY = threadsZ = 8; + break; + case 4: // (1, _, 1) + threadsX = threadsZ = 1; + threadsY = (int)device.WavefrontSize; + break; + case 5: // (1, 1, _) + threadsX = threadsY = 1; + threadsZ = (int)device.WavefrontSize; + break; + default: // (_, _, _) + threadsX = threadsY = threadsZ = 4; + break; + } Run(device, x, y, z, threadsX, threadsY, threadsZ, in shader); } @@ -68,6 +110,24 @@ namespace ComputeSharp.Shaders { device.ThrowIfDisposed(); + Guard.IsGreaterThan(x, 0, nameof(x)); + Guard.IsGreaterThan(y, 0, nameof(y)); + Guard.IsGreaterThan(z, 0, nameof(z)); + Guard.IsBetweenOrEqualTo(threadsX, 1, 1024, nameof(threadsX)); + Guard.IsBetweenOrEqualTo(threadsY, 1, 1024, nameof(threadsY)); + Guard.IsBetweenOrEqualTo(threadsZ, 1, 64, nameof(threadsZ)); + Guard.IsLessThanOrEqualTo(threadsX * threadsY * threadsZ, 1024, "threadsXYZ"); + + // Calculate the dispatch values + int + groupsX = Math.DivRem(x, threadsX, out int modX) + (modX == 0 ? 0 : 1), + groupsY = Math.DivRem(y, threadsY, out int modY) + (modY == 0 ? 0 : 1), + groupsZ = Math.DivRem(z, threadsZ, out int modZ) + (modZ == 0 ? 0 : 1); + + Guard.IsBetweenOrEqualTo(groupsX, 1, FX.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); + Guard.IsBetweenOrEqualTo(groupsY, 1, FX.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); + 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); CachedShader shaderData; @@ -111,12 +171,6 @@ namespace ComputeSharp.Shaders commandList.D3D12GraphicsCommandList->SetComputeRootDescriptorTable((uint)i + 1, resources[i]); } - // Calculate the dispatch values - int - groupsX = Math.DivRem(x, threadsX, out int modX) + (modX == 0 ? 0 : 1), - groupsY = Math.DivRem(y, threadsY, out int modY) + (modY == 0 ? 0 : 1), - groupsZ = Math.DivRem(z, threadsZ, out int modZ) + (modZ == 0 ? 0 : 1); - // Dispatch and wait for completion commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, (uint)groupsZ); commandList.ExecuteAndWaitForCompletion(); diff --git a/tests/ComputeSharp.Tests/DispatchTests.cs b/tests/ComputeSharp.Tests/DispatchTests.cs index 2729160f..2252e6f9 100644 --- a/tests/ComputeSharp.Tests/DispatchTests.cs +++ b/tests/ComputeSharp.Tests/DispatchTests.cs @@ -1,3 +1,4 @@ +using System; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests @@ -31,6 +32,22 @@ namespace ComputeSharp.Tests } } + [TestMethod] + [DataRow(0, 8, 8)] + [DataRow(8, 0, 8)] + [DataRow(8, 8, 0)] + [DataRow(8, -3, 16)] + [DataRow(-1, -1, -1)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Verify_ThreadIds_OutOfRange(int x, int y, int z) + { + using ReadWriteTexture3D buffer = Gpu.Default.AllocateReadWriteTexture3D(50, 50, 50); + + Gpu.Default.For(x, y, z, new ThreadIdsShader(buffer)); + + Assert.Fail(); + } + [AutoConstructor] internal readonly partial struct ThreadIdsShader : IComputeShader { @@ -68,6 +85,25 @@ namespace ComputeSharp.Tests } } + [TestMethod] + [DataRow(0, 8, 8)] + [DataRow(8, 0, 8)] + [DataRow(8, 8, 0)] + [DataRow(1025, 8, 8)] + [DataRow(8, 2000, 8)] + [DataRow(8, 8, 70)] + [DataRow(8, -1, 8)] + [DataRow(8, 15, 16)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Verify_GroupIds_OutOfRange(int x, int y, int z) + { + using ReadWriteTexture3D buffer = Gpu.Default.AllocateReadWriteTexture3D(50, 50, 50); + + Gpu.Default.For(buffer.Width, buffer.Height, buffer.Depth, x, y, z, new GroupIdsShader(buffer)); + + Assert.Fail(); + } + [AutoConstructor] internal readonly partial struct GroupIdsShader : IComputeShader {