diff --git a/src/ComputeSharp/ComputeSharp.csproj b/src/ComputeSharp/ComputeSharp.csproj
index bfbb1376..fb2ca081 100644
--- a/src/ComputeSharp/ComputeSharp.csproj
+++ b/src/ComputeSharp/ComputeSharp.csproj
@@ -236,8 +236,4 @@
UIntMxN.g.cs
-
-
-
-
diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs
index 02ebe90d..fff4ea9d 100644
--- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs
+++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs
@@ -1,6 +1,5 @@
-using ComputeSharp.Shaders;
+using System.Runtime.CompilerServices;
using Microsoft.Toolkit.Diagnostics;
-using System.Runtime.CompilerServices;
namespace ComputeSharp;
@@ -21,7 +20,9 @@ public static partial class GraphicsDeviceExtensions
{
Guard.IsNotNull(device, nameof(device));
- ShaderRunner.Run(device, x, 1, 1, ref Unsafe.AsRef(in shader));
+ using ComputeContext context = device.CreateComputeContext();
+
+ context.Run(x, 1, 1, ref Unsafe.AsRef(in shader));
}
///
@@ -37,7 +38,9 @@ public static partial class GraphicsDeviceExtensions
{
Guard.IsNotNull(device, nameof(device));
- ShaderRunner.Run(device, x, y, 1, ref Unsafe.AsRef(in shader));
+ using ComputeContext context = device.CreateComputeContext();
+
+ context.Run(x, y, 1, ref Unsafe.AsRef(in shader));
}
///
@@ -54,7 +57,9 @@ public static partial class GraphicsDeviceExtensions
{
Guard.IsNotNull(device, nameof(device));
- ShaderRunner.Run(device, x, y, z, ref Unsafe.AsRef(in shader));
+ using ComputeContext context = device.CreateComputeContext();
+
+ context.Run(x, y, z, ref Unsafe.AsRef(in shader));
}
///
@@ -74,7 +79,9 @@ public static partial class GraphicsDeviceExtensions
{
Guard.IsNotNull(device, nameof(device));
- ShaderRunner.Run(device, x, y, z, threadsX, threadsY, threadsZ, ref Unsafe.AsRef(in shader));
+ using ComputeContext context = device.CreateComputeContext();
+
+ context.Run(x, y, z, threadsX, threadsY, threadsZ, ref Unsafe.AsRef(in shader));
}
///
@@ -91,7 +98,9 @@ public static partial class GraphicsDeviceExtensions
Guard.IsNotNull(device, nameof(device));
Guard.IsNotNull(texture, nameof(texture));
- ShaderRunner.Run(device, texture, ref Unsafe.AsRef(default(T)));
+ using ComputeContext context = device.CreateComputeContext();
+
+ context.Run(texture, ref Unsafe.AsRef(default(T)));
}
///
@@ -109,7 +118,9 @@ public static partial class GraphicsDeviceExtensions
Guard.IsNotNull(device, nameof(device));
Guard.IsNotNull(texture, nameof(texture));
- ShaderRunner.Run(device, texture, ref Unsafe.AsRef(in shader));
+ using ComputeContext context = device.CreateComputeContext();
+
+ context.Run(texture, ref Unsafe.AsRef(in shader));
}
///
diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs
index 63a16507..b5ca7f5a 100644
--- a/src/ComputeSharp/Shaders/ComputeContext.cs
+++ b/src/ComputeSharp/Shaders/ComputeContext.cs
@@ -117,6 +117,14 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
{
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;
@@ -136,7 +144,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
threadsY = 1;
break;
case 2: // (_, 1, 1)
- threadsX = (int)this.device.WavefrontSize;
+ threadsX = 64;
threadsY = threadsZ = 1;
break;
case 3: // (1, _, _)
@@ -145,11 +153,11 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
break;
case 4: // (1, _, 1)
threadsX = threadsZ = 1;
- threadsY = (int)this.device.WavefrontSize;
+ threadsY = 64;
break;
case 5: // (1, 1, _)
threadsX = threadsY = 1;
- threadsZ = (int)this.device.WavefrontSize;
+ threadsZ = 64;
break;
default: // (_, _, _)
threadsX = threadsY = threadsZ = 4;
@@ -198,7 +206,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
Guard.IsBetweenOrEqualTo(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
Guard.IsBetweenOrEqualTo(groupsZ, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
- PipelineData pipelineData = ShaderRunner.GetPipelineData(this.device, threadsX, threadsY, threadsZ, ref shader);
+ PipelineData pipelineData = PipelineDataLoader.GetPipelineData(this.device, threadsX, threadsY, threadsZ, ref shader);
ref CommandList commandList = ref GetCommandList(in this, pipelineData.D3D12PipelineState);
@@ -235,7 +243,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
Guard.IsBetweenOrEqualTo(groupsX, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
Guard.IsBetweenOrEqualTo(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
- PipelineData pipelineData = ShaderRunner.GetPipelineData(this.device, threadsX, threadsY, threadsZ, ref shader);
+ PipelineData pipelineData = PipelineDataLoader.GetPipelineData(this.device, threadsX, threadsY, threadsZ, ref shader);
ref CommandList commandList = ref GetCommandList(in this, pipelineData.D3D12PipelineState);
diff --git a/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs b/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs
new file mode 100644
index 00000000..61210d7d
--- /dev/null
+++ b/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs
@@ -0,0 +1,101 @@
+using System;
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+using ComputeSharp.Graphics.Commands;
+using ComputeSharp.Shaders.Dispatching;
+using ComputeSharp.Shaders.Extensions;
+using ComputeSharp.Shaders.Translation.Models;
+using ComputeSharp.__Internals;
+using TerraFX.Interop.DirectX;
+using TerraFX.Interop.Windows;
+
+#pragma warning disable CS0618
+
+namespace ComputeSharp.Shaders;
+
+///
+/// A responsible for performing all the necessary operations to compile and load shader data.
+///
+/// The type of compute shader to load.
+internal static class PipelineDataLoader
+ where T : struct, IShader
+{
+ ///
+ /// The mapping used to cache and reuse compiled shaders.
+ ///
+ private static readonly Dictionary ShadersCache = new();
+
+ ///
+ /// Gets the instance for a given shader.
+ ///
+ /// The to use to run the shader.
+ /// The number of threads in each thread group for the X axis.
+ /// The number of threads in each thread group for the Y axis.
+ /// The number of threads in each thread group for the Z axis.
+ /// The input instance representing the compute shader to run.
+ /// The instance for a given shader.
+ public static unsafe PipelineData GetPipelineData(GraphicsDevice device, int threadsX, int threadsY, int threadsZ, ref T shader)
+ {
+ ShaderKey key = new(shader.GetDispatchId(), threadsX, threadsY, threadsZ);
+
+ lock (ShadersCache)
+ {
+ // Get or preload the shader
+ if (!ShadersCache.TryGetValue(key, out ICachedShader? shaderData))
+ {
+ LoadShader(threadsX, threadsY, threadsZ, ref shader, out shaderData);
+
+ // Cache for later use
+ ShadersCache.Add(key, shaderData);
+ }
+
+ // Create the reusable pipeline state
+ if (!shaderData.CachedPipelines.TryGetValue(device, out PipelineData? pipelineData))
+ {
+ CreatePipelineData(device, shaderData, out pipelineData);
+ }
+
+ return pipelineData;
+ }
+ }
+
+ ///
+ /// Loads a shader with the specified parameters.
+ ///
+ /// The number of threads in each thread group for the X axis.
+ /// The number of threads in each thread group for the Y axis.
+ /// The number of threads in each thread group for the Z axis.
+ /// The input instance representing the compute shader to run.
+ /// The instance to return with the cached shader data.
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ private static unsafe void LoadShader(int threadsX, int threadsY, int threadsZ, ref T shader, out ICachedShader shaderData)
+ {
+ ShaderBytecodeLoader bytecodeLoader = default;
+
+ shader.LoadBytecode(ref bytecodeLoader, threadsX, threadsY, threadsZ);
+
+ shaderData = bytecodeLoader.GetCachedShader();
+ }
+
+ ///
+ /// Creates and caches a instance for a given shader.
+ ///
+ /// The to use to run the shader.
+ /// The instance with the data on the loaded shader.
+ /// The resulting instance to use to run the shader.
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ private static unsafe void CreatePipelineData(GraphicsDevice device, ICachedShader shaderData, out PipelineData pipelineData)
+ {
+ using ComPtr d3D12RootSignature = default;
+
+ ShaderDispatchMetadataLoader metadataLoader = new(device.D3D12Device);
+
+ Unsafe.NullRef().LoadDispatchMetadata(ref metadataLoader, out *(IntPtr*)&d3D12RootSignature);
+
+ using ComPtr d3D12PipelineState = device.D3D12Device->CreateComputePipelineState(d3D12RootSignature.Get(), shaderData.D3D12ShaderBytecode);
+
+ pipelineData = new PipelineData(d3D12RootSignature.Get(), d3D12PipelineState.Get());
+
+ shaderData.CachedPipelines.Add(device, pipelineData);
+ }
+}
diff --git a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs
deleted file mode 100644
index 0327f1d4..00000000
--- a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs
+++ /dev/null
@@ -1,256 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Runtime.CompilerServices;
-using ComputeSharp.Graphics.Commands;
-using ComputeSharp.Shaders.Dispatching;
-using ComputeSharp.Shaders.Extensions;
-using ComputeSharp.Shaders.Translation.Models;
-using ComputeSharp.__Internals;
-using Microsoft.Toolkit.Diagnostics;
-using TerraFX.Interop.DirectX;
-using TerraFX.Interop.Windows;
-
-#pragma warning disable CS0618
-
-namespace ComputeSharp.Shaders;
-
-///
-/// A responsible for performing all the necessary operations to compile and run a compute shader.
-///
-/// The type of compute shader to run.
-internal static class ShaderRunner
- where T : struct, IShader
-{
- ///
- /// The mapping used to cache and reuse compiled shaders.
- ///
- private static readonly Dictionary ShadersCache = new();
-
- ///
- /// Runs the input shader on a target instance, with the specified parameters.
- ///
- /// The to use to run the shader.
- /// The number of iterations to run on the X axis.
- /// 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 unsafe void Run(GraphicsDevice device, int x, int y, int z, ref T shader)
- {
- // 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(device, x, y, z, threadsX, threadsY, threadsZ, ref shader);
- }
-
- ///
- /// Runs the input shader on a target instance, with the specified parameters.
- ///
- /// The to use to run the shader.
- /// The number of iterations to run on the X axis.
- /// The number of iterations to run on the Y axis.
- /// The number of iterations to run on the Z axis.
- /// The number of threads in each thread group for the X axis.
- /// The number of threads in each thread group for the Y axis.
- /// The number of threads in each thread group for the Z axis.
- /// The input instance representing the compute shader to run.
- public static unsafe void Run(
- GraphicsDevice device,
- int x,
- int y,
- int z,
- int threadsX,
- int threadsY,
- int threadsZ,
- ref T shader)
- {
- 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");
-
- int groupsX = Math.DivRem(x, threadsX, out int modX) + (modX == 0 ? 0 : 1);
- int groupsY = Math.DivRem(y, threadsY, out int modY) + (modY == 0 ? 0 : 1);
- int groupsZ = Math.DivRem(z, threadsZ, out int modZ) + (modZ == 0 ? 0 : 1);
-
- Guard.IsBetweenOrEqualTo(groupsX, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
- Guard.IsBetweenOrEqualTo(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
- Guard.IsBetweenOrEqualTo(groupsZ, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
-
- PipelineData pipelineData = GetPipelineData(device, threadsX, threadsY, threadsZ, ref shader);
-
- using CommandList commandList = new(device, pipelineData.D3D12PipelineState);
-
- commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature);
-
- ComputeShaderDispatchDataLoader dataLoader = new(commandList.D3D12GraphicsCommandList);
-
- shader.LoadDispatchData(ref dataLoader, device, x, y, z);
-
- commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, (uint)groupsZ);
- commandList.ExecuteAndWaitForCompletion();
- }
-
- ///
- /// Runs the input shader on a target instance, with the specified parameters.
- ///
- /// The to use to run the shader.
- /// The target texture to invoke the pixel shader upon.
- /// The input instance representing the pixel shader to run.
- public static unsafe void Run(GraphicsDevice device, IReadWriteNormalizedTexture2D texture, ref T shader)
- where TPixel : unmanaged
- {
- device.ThrowIfDisposed();
-
- int x = texture.Width;
- int y = texture.Height;
- const int threadsX = 8;
- const int threadsY = 8;
- const int threadsZ = 1;
- int groupsX = Math.DivRem(x, threadsX, out int modX) + (modX == 0 ? 0 : 1);
- int groupsY = Math.DivRem(y, threadsY, out int modY) + (modY == 0 ? 0 : 1);
-
- Guard.IsBetweenOrEqualTo(groupsX, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
- Guard.IsBetweenOrEqualTo(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
-
- PipelineData pipelineData = GetPipelineData(device, threadsX, threadsY, threadsZ, ref shader);
-
- using CommandList commandList = new(device, pipelineData.D3D12PipelineState);
-
- commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature);
-
- PixelShaderDispatchDataLoader dataLoader = new(commandList.D3D12GraphicsCommandList);
-
- shader.LoadDispatchData(ref dataLoader, device, x, y, 1);
-
- // Load the implicit output texture
- commandList.D3D12GraphicsCommandList->SetComputeRootDescriptorTable(
- 1,
- ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuDescriptorHandle(device));
-
- commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, 1);
- commandList.ExecuteAndWaitForCompletion();
- }
-
- ///
- /// Gets the instance for a given shader.
- ///
- /// The to use to run the shader.
- /// The number of threads in each thread group for the X axis.
- /// The number of threads in each thread group for the Y axis.
- /// The number of threads in each thread group for the Z axis.
- /// The input instance representing the compute shader to run.
- /// The instance for a given shader.
- public static unsafe PipelineData GetPipelineData(GraphicsDevice device, int threadsX, int threadsY, int threadsZ, ref T shader)
- {
- ShaderKey key = new(shader.GetDispatchId(), threadsX, threadsY, threadsZ);
-
- lock (ShadersCache)
- {
- // Get or preload the shader
- if (!ShadersCache.TryGetValue(key, out ICachedShader? shaderData))
- {
- LoadShader(threadsX, threadsY, threadsZ, ref shader, out shaderData);
-
- // Cache for later use
- ShadersCache.Add(key, shaderData);
- }
-
- // Create the reusable pipeline state
- if (!shaderData.CachedPipelines.TryGetValue(device, out PipelineData? pipelineData))
- {
- CreatePipelineData(device, shaderData, out pipelineData);
- }
-
- return pipelineData;
- }
- }
-
- ///
- /// Loads a shader with the specified parameters.
- ///
- /// The number of threads in each thread group for the X axis.
- /// The number of threads in each thread group for the Y axis.
- /// The number of threads in each thread group for the Z axis.
- /// The input instance representing the compute shader to run.
- /// The instance to return with the cached shader data.
- [MethodImpl(MethodImplOptions.NoInlining)]
- private static unsafe void LoadShader(int threadsX, int threadsY, int threadsZ, ref T shader, out ICachedShader shaderData)
- {
- ShaderBytecodeLoader bytecodeLoader = default;
-
- shader.LoadBytecode(ref bytecodeLoader, threadsX, threadsY, threadsZ);
-
- shaderData = bytecodeLoader.GetCachedShader();
- }
-
- ///
- /// Creates and caches a instance for a given shader.
- ///
- /// The to use to run the shader.
- /// The instance with the data on the loaded shader.
- /// The resulting instance to use to run the shader.
- [MethodImpl(MethodImplOptions.NoInlining)]
- private static unsafe void CreatePipelineData(GraphicsDevice device, ICachedShader shaderData, out PipelineData pipelineData)
- {
- using ComPtr d3D12RootSignature = default;
-
- ShaderDispatchMetadataLoader metadataLoader = new(device.D3D12Device);
-
- Unsafe.NullRef().LoadDispatchMetadata(ref metadataLoader, out *(IntPtr*)&d3D12RootSignature);
-
- using ComPtr d3D12PipelineState = device.D3D12Device->CreateComputePipelineState(d3D12RootSignature.Get(), shaderData.D3D12ShaderBytecode);
-
- pipelineData = new PipelineData(d3D12RootSignature.Get(), d3D12PipelineState.Get());
-
- shaderData.CachedPipelines.Add(device, pipelineData);
- }
-}