diff --git a/src/ComputeSharp/Graphics/Commands/CommandList.cs b/src/ComputeSharp/Graphics/Commands/CommandList.cs
index b6f330c9..e31c9241 100644
--- a/src/ComputeSharp/Graphics/Commands/CommandList.cs
+++ b/src/ComputeSharp/Graphics/Commands/CommandList.cs
@@ -78,6 +78,16 @@ internal unsafe ref struct CommandList
device.SetDescriptorHeapForCommandList(this.d3D12GraphicsCommandList);
}
+ ///
+ /// Gets whether or not the curreent instance is allocated.
+ ///
+ public readonly bool IsAllocated => this.device is not null;
+
+ ///
+ /// Gets the instance associated with the current command list.
+ ///
+ public GraphicsDevice GraphicsDevice => this.device;
+
///
/// Gets the command list type being used by the current instance.
///
diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs
index 28475326..8679210c 100644
--- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs
+++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs
@@ -9,7 +9,7 @@ namespace ComputeSharp;
public static partial class GraphicsDeviceExtensions
{
///
- /// Compiles and runs the input shader on a target instance, with the specified parameters.
+ /// Runs the input shader on a target instance, with the specified parameters.
///
/// The type of compute shader to run.
/// The to use to run the shader.
@@ -22,7 +22,7 @@ public static partial class GraphicsDeviceExtensions
}
///
- /// Compiles and runs the input shader on a target instance, with the specified parameters.
+ /// Runs the input shader on a target instance, with the specified parameters.
///
/// The type of compute shader to run.
/// The to use to run the shader.
@@ -36,7 +36,7 @@ public static partial class GraphicsDeviceExtensions
}
///
- /// Compiles and runs the input shader on a target instance, with the specified parameters.
+ /// Runs the input shader on a target instance, with the specified parameters.
///
/// The type of compute shader to run.
/// The to use to run the shader.
@@ -51,7 +51,7 @@ public static partial class GraphicsDeviceExtensions
}
///
- /// Compiles and runs the input shader on a target instance, with the specified parameters.
+ /// Runs the input shader on a target instance, with the specified parameters.
///
/// The type of compute shader to run.
/// The to use to run the shader.
@@ -69,7 +69,7 @@ public static partial class GraphicsDeviceExtensions
}
///
- /// Compiles and runs the input shader on a target instance, with the specified parameters.
+ /// Runs the input shader on a target instance, with the specified parameters.
///
/// The type of pixel shader to run.
/// The type of pixels being processed by the shader.
@@ -83,7 +83,7 @@ public static partial class GraphicsDeviceExtensions
}
///
- /// Compiles and runs the input shader on a target instance, with the specified parameters.
+ /// Runs the input shader on a target instance, with the specified parameters.
///
/// The type of pixel shader to run.
/// The type of pixels being processed by the shader.
@@ -96,4 +96,26 @@ public static partial class GraphicsDeviceExtensions
{
ShaderRunner.Run(device, texture, ref Unsafe.AsRef(in shader));
}
+
+ ///
+ /// Creates a new instance to batch compute operations together.
+ ///
+ /// The to use to run shaders.
+ /// A new instance to batch compute operations together.
+ ///
+ /// The returned instance should be used in a block or declaration:
+ ///
+ /// using (var context = device.CreateComputeContext())
+ /// {
+ /// // Dispatch shaders here...
+ /// }
+ ///
+ /// All dispatched shaders will be executed as soon as the context goes out of scope.
+ ///
+ public static ComputeContext CreateComputeContext(this GraphicsDevice device)
+ {
+ device.ThrowIfDisposed();
+
+ return new(device);
+ }
}
diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs
new file mode 100644
index 00000000..b1c69ab2
--- /dev/null
+++ b/src/ComputeSharp/Shaders/ComputeContext.cs
@@ -0,0 +1,213 @@
+using System;
+using System.Runtime.CompilerServices;
+using ComputeSharp.__Internals;
+using ComputeSharp.Graphics.Commands;
+using ComputeSharp.Shaders;
+using ComputeSharp.Shaders.Dispatching;
+using Microsoft.Toolkit.Diagnostics;
+using TerraFX.Interop.DirectX;
+
+#pragma warning disable CS0618
+
+namespace ComputeSharp;
+
+///
+/// A context to batch compute operations in a single invocation, minimizing GPU overhead.
+///
+public ref struct ComputeContext
+{
+ ///
+ /// The instance owning the current context.
+ ///
+ private readonly GraphicsDevice device;
+
+ ///
+ /// The current instance used to dispatch shaders.
+ ///
+ private CommandList commandList;
+
+ ///
+ /// Creates a new instance with the specified parameters.
+ ///
+ /// The instance owning the current context.
+ internal ComputeContext(GraphicsDevice device)
+ {
+ this.device = device;
+ this.commandList = default;
+ }
+
+ ///
+ /// 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 number of iterations to run on the Z axis.
+ /// The input instance representing the compute shader to run.
+ internal unsafe void Run(int x, int y, int z, ref T shader)
+ where T : struct, IComputeShader
+ {
+ 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 = (int)this.device.WavefrontSize;
+ threadsY = threadsZ = 1;
+ break;
+ case 3: // (1, _, _)
+ threadsX = 1;
+ threadsY = threadsZ = 8;
+ break;
+ case 4: // (1, _, 1)
+ threadsX = threadsZ = 1;
+ threadsY = (int)this.device.WavefrontSize;
+ break;
+ case 5: // (1, 1, _)
+ threadsX = threadsY = 1;
+ threadsZ = (int)this.device.WavefrontSize;
+ break;
+ default: // (_, _, _)
+ threadsX = threadsY = threadsZ = 4;
+ break;
+ }
+
+ Run(x, y, z, threadsX, threadsY, threadsZ, ref shader);
+ }
+
+ ///
+ /// Runs the input shader with the specified parameters.
+ ///
+ /// The type of compute shader to run.
+ /// 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.
+ internal unsafe void Run(
+ int x,
+ int y,
+ int z,
+ int threadsX,
+ int threadsY,
+ int threadsZ,
+ ref T shader)
+ where T : struct, IComputeShader
+ {
+ 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 = ShaderRunner.GetPipelineData(this.device, threadsX, threadsY, threadsZ, ref shader);
+
+ ref CommandList commandList = ref GetCommandList(ref this, this.device, pipelineData.D3D12PipelineState);
+
+ commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature);
+
+ ComputeShaderDispatchDataLoader dataLoader = new(commandList.D3D12GraphicsCommandList);
+
+ shader.LoadDispatchData(ref dataLoader, this.device, x, y, z);
+
+ commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, (uint)groupsZ);
+ }
+
+ ///
+ /// Runs the input shader with the specified parameters.
+ ///
+ /// The type of pixel shader to run.
+ /// The type of pixel to work on.
+ /// The target texture to invoke the pixel shader upon.
+ /// The input instance representing the pixel shader to run.
+ internal unsafe void Run(IReadWriteTexture2D texture, ref T shader)
+ where T : struct, IPixelShader
+ where TPixel : unmanaged
+ {
+ 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 = ShaderRunner.GetPipelineData(this.device, threadsX, threadsY, threadsZ, ref shader);
+
+ ref CommandList commandList = ref GetCommandList(ref this, this.device, pipelineData.D3D12PipelineState);
+
+ commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature);
+
+ PixelShaderDispatchDataLoader dataLoader = new(commandList.D3D12GraphicsCommandList);
+
+ shader.LoadDispatchData(ref dataLoader, this.device, x, y, 1);
+
+ // Load the implicit output texture
+ commandList.D3D12GraphicsCommandList->SetComputeRootDescriptorTable(
+ 1,
+ ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuDescriptorHandle(this.device));
+
+ commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, 1);
+ }
+
+ ///
+ public void Dispose()
+ {
+ if (!this.commandList.IsAllocated)
+ {
+ return;
+ }
+
+ this.commandList.ExecuteAndWaitForCompletion();
+ }
+
+ ///
+ /// Gets the current instance, and initializes it as needed.
+ ///
+ /// The current instance.
+ /// The to use to run the shader.
+ /// The input to load.
+ /// A reference to the instance to use.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private static unsafe ref CommandList GetCommandList(ref ComputeContext @this, GraphicsDevice device, ID3D12PipelineState* pipelineState)
+ {
+ if (@this.commandList.IsAllocated)
+ {
+ @this.commandList.D3D12GraphicsCommandList->SetPipelineState(pipelineState);
+ }
+ else
+ {
+ @this.commandList = new CommandList(device, pipelineState);
+ }
+
+ return ref @this.commandList;
+ }
+}
diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs
new file mode 100644
index 00000000..29347582
--- /dev/null
+++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs
@@ -0,0 +1,98 @@
+using System.Runtime.CompilerServices;
+
+namespace ComputeSharp;
+
+///
+/// A that contains extension methods for the type, used to run compute shaders.
+///
+public static partial class ComputeContextExtensions
+{
+ ///
+ /// Runs the input shader on a target instance, with the specified parameters.
+ ///
+ /// The type of compute shader to run.
+ /// The to use to run the shader.
+ /// The number of iterations to run on the X axis.
+ /// The input instance representing the compute shader to run.
+ 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));
+ }
+
+ ///
+ /// Runs the input shader on a target instance, with the specified parameters.
+ ///
+ /// The type of compute shader to run.
+ /// 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 input instance representing the compute shader to run.
+ public static void For(this ComputeContext context, int x, int y, in T shader)
+ where T : struct, IComputeShader
+ {
+ context.Run(x, y, 1, ref Unsafe.AsRef(in shader));
+ }
+
+ ///
+ /// Runs the input shader on a target instance, with the specified parameters.
+ ///
+ /// The type of compute shader to run.
+ /// 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 void For(this ComputeContext context, int x, int y, int z, in T shader)
+ where T : struct, IComputeShader
+ {
+ context.Run(x, y, z, ref Unsafe.AsRef(in shader));
+ }
+
+ ///
+ /// Runs the input shader on a target instance, with the specified parameters.
+ ///
+ /// The type of compute shader to run.
+ /// 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 void For(this ComputeContext context, int x, int y, int z, int threadsX, int threadsY, int threadsZ, in T shader)
+ where T : struct, IComputeShader
+ {
+ context.Run(x, y, z, threadsX, threadsY, threadsZ, ref Unsafe.AsRef(in shader));
+ }
+
+ ///
+ /// Runs the input shader on a target instance, with the specified parameters.
+ ///
+ /// The type of pixel shader to run.
+ /// The type of pixels being processed by the shader.
+ /// The to use to run the shader.
+ /// The target texture to apply the pixel shader to.
+ public static void ForEach(this ComputeContext context, IReadWriteTexture2D texture)
+ where T : struct, IPixelShader
+ where TPixel : unmanaged
+ {
+ context.Run(texture, ref Unsafe.AsRef(default(T)));
+ }
+
+ ///
+ /// Runs the input shader on a target instance, with the specified parameters.
+ ///
+ /// The type of pixel shader to run.
+ /// The type of pixels being processed by the shader.
+ /// The to use to run the shader.
+ /// The target texture to apply the pixel shader to.
+ /// The input instance representing the pixel shader to run.
+ public static void ForEach(this ComputeContext context, IReadWriteTexture2D texture, in T shader)
+ where T : struct, IPixelShader
+ where TPixel : unmanaged
+ {
+ context.Run(texture, ref Unsafe.AsRef(in shader));
+ }
+}
diff --git a/src/ComputeSharp/Shaders/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp/Shaders/Extensions/Interop/ID3D12DeviceExtensions.cs
similarity index 100%
rename from src/ComputeSharp/Shaders/Extensions/ID3D12DeviceExtensions.cs
rename to src/ComputeSharp/Shaders/Extensions/Interop/ID3D12DeviceExtensions.cs
diff --git a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs
index a15f04c8..edd10335 100644
--- a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs
+++ b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs
@@ -27,7 +27,7 @@ internal static class ShaderRunner
private static readonly Dictionary ShadersCache = new();
///
- /// Compiles and runs the input shader on a target instance, with the specified parameters.
+ /// 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.
@@ -84,7 +84,7 @@ internal static class ShaderRunner
}
///
- /// Compiles and runs the input shader on a target instance, with the specified parameters.
+ /// 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.
@@ -137,7 +137,7 @@ internal static class ShaderRunner
}
///
- /// Compiles and runs the input shader on a target instance, with the specified parameters.
+ /// 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.