diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IDispatchDataLoader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IDispatchDataLoader.cs
new file mode 100644
index 00000000..c74fc4a7
--- /dev/null
+++ b/src/ComputeSharp/Core/Interfaces/__Internals/IDispatchDataLoader.cs
@@ -0,0 +1,29 @@
+using System;
+using System.ComponentModel;
+
+namespace ComputeSharp.__Internals
+{
+ ///
+ /// A base representing a data loader for a shader being dispatched.
+ ///
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ [Obsolete("This interface is not intended to be used directly by user code")]
+ public interface IDispatchDataLoader
+ {
+ ///
+ /// Loads the captured values used by the shader to be dispatched.
+ ///
+ /// The sequence of serialized captured values.
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ [Obsolete("This method is not intended to be called directly by user code")]
+ void LoadCapturedValues(ReadOnlySpan data);
+
+ ///
+ /// Loads the captured resources used by the shader to be dispatched.
+ ///
+ /// The sequence of serialized resources.
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ [Obsolete("This method is not intended to be called directly by user code")]
+ void LoadCapturedResources(ReadOnlySpan data);
+ }
+}
diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IShader{TSelf}.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IShader{TSelf}.cs
index 51af2bbf..d3389bf0 100644
--- a/src/ComputeSharp/Core/Interfaces/__Internals/IShader{TSelf}.cs
+++ b/src/ComputeSharp/Core/Interfaces/__Internals/IShader{TSelf}.cs
@@ -19,5 +19,19 @@ namespace ComputeSharp.__Internals
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This interface is not intended to be used directly by user code")]
int GetDispatchId();
+
+ ///
+ /// Loads the dispatch data for the shader.
+ ///
+ /// The type of data loader being used.
+ /// The instance to use to load the data.
+ /// The device the shader is being dispatched on.
+ /// 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.
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ [Obsolete("This method is not intended to be called directly by user code")]
+ void LoadDispatchData(in TDataLoader dataLoader, GraphicsDevice device, int x, int y, int z)
+ where TDataLoader : struct, IDispatchDataLoader;
}
}
diff --git a/src/ComputeSharp/Shaders/Dispatching/ComputeShaderDispatchDataLoader.cs b/src/ComputeSharp/Shaders/Dispatching/ComputeShaderDispatchDataLoader.cs
new file mode 100644
index 00000000..86bc33f6
--- /dev/null
+++ b/src/ComputeSharp/Shaders/Dispatching/ComputeShaderDispatchDataLoader.cs
@@ -0,0 +1,57 @@
+using System;
+using System.Runtime.CompilerServices;
+using ComputeSharp.Graphics.Extensions;
+using ComputeSharp.__Internals;
+using TerraFX.Interop;
+using System.Runtime.InteropServices;
+
+#pragma warning disable CS0618
+
+namespace ComputeSharp.Shaders.Dispatching
+{
+ ///
+ /// A data loader for compute shaders.
+ ///
+ internal readonly unsafe struct ComputeShaderDispatchDataLoader : IDispatchDataLoader
+ {
+ ///
+ /// The object in use.
+ ///
+ private readonly ID3D12GraphicsCommandList* d3D12GraphicsCommandList;
+
+ ///
+ /// Creates a new instance.
+ ///
+ /// The object to use.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal ComputeShaderDispatchDataLoader(ID3D12GraphicsCommandList* d3D12GraphicsCommandList)
+ {
+ this.d3D12GraphicsCommandList = d3D12GraphicsCommandList;
+ }
+
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public void LoadCapturedValues(ReadOnlySpan data)
+ {
+ this.d3D12GraphicsCommandList->SetComputeRoot32BitConstants(data);
+ }
+
+ ///
+ public void LoadCapturedResources(ReadOnlySpan data)
+ {
+ ID3D12GraphicsCommandList* d3D12GraphicsCommandList = this.d3D12GraphicsCommandList;
+ ref ulong resourcesRef = ref MemoryMarshal.GetReference(data);
+ ref D3D12_GPU_DESCRIPTOR_HANDLE startRef = ref Unsafe.As(ref resourcesRef);
+ ref D3D12_GPU_DESCRIPTOR_HANDLE endRef = ref Unsafe.Add(ref startRef, data.Length);
+ uint offset = 1;
+
+ while (Unsafe.IsAddressLessThan(ref startRef, ref endRef))
+ {
+ d3D12GraphicsCommandList->SetComputeRootDescriptorTable(offset, startRef);
+
+ startRef = ref Unsafe.Add(ref startRef, 1);
+ offset++;
+ }
+ }
+ }
+}
diff --git a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs
index 40538161..a9cee52b 100644
--- a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs
+++ b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs
@@ -13,6 +13,7 @@ using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop;
using FX = TerraFX.Interop.Windows;
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
+using ComputeSharp.Shaders.Dispatching;
#pragma warning disable CS0618
@@ -120,7 +121,6 @@ namespace ComputeSharp.Shaders
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),
@@ -130,15 +130,13 @@ namespace ComputeSharp.Shaders
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(shader.GetDispatchId(), threadsX, threadsY, threadsZ);
- CachedShader shaderData;
PipelineData? pipelineData;
lock (ShadersCache)
{
// Get or preload the shader
- if (!ShadersCache.TryGetValue(key, out shaderData))
+ if (!ShadersCache.TryGetValue(key, out CachedShader shaderData))
{
LoadShader(threadsX, threadsY, threadsZ, in shader, out shaderData);
@@ -153,27 +151,15 @@ namespace ComputeSharp.Shaders
}
}
- // Create the commands list and set the pipeline state
using CommandList commandList = new(device, D3D12_COMMAND_LIST_TYPE_COMPUTE);
commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature);
commandList.D3D12GraphicsCommandList->SetPipelineState(pipelineData.D3D12PipelineState);
- // Extract the dispatch data for the shader invocation
- using DispatchData dispatchData = shaderData.Loader.GetDispatchData(device, in shader, x, y, z);
+ ComputeShaderDispatchDataLoader dataLoader = new(commandList.D3D12GraphicsCommandList);
- // Initialize the loop targets and the captured values
- commandList.D3D12GraphicsCommandList->SetComputeRoot32BitConstants(dispatchData.Variables);
+ shader.LoadDispatchData(in dataLoader, device, x, y, z);
- ReadOnlySpan resources = dispatchData.Resources;
-
- for (int i = 0; i < resources.Length; i++)
- {
- // Load the captured buffers
- commandList.D3D12GraphicsCommandList->SetComputeRootDescriptorTable((uint)i + 1, resources[i]);
- }
-
- // Dispatch and wait for completion
commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, (uint)groupsZ);
commandList.ExecuteAndWaitForCompletion();
}