diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IConstantBufferLoader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IConstantBufferLoader.cs new file mode 100644 index 00000000..76c584af --- /dev/null +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IConstantBufferLoader.cs @@ -0,0 +1,16 @@ +using System; + +namespace ComputeSharp.__Internals; + +/// +/// A type representing a data loader for the constant buffer of a compute shader. +/// +public interface IConstantBufferLoader +{ + /// + /// Loads the constant buffer of a compute shader. + /// + /// The constant buffer for the compute shader (the size must be a multiple of the size of a DWORD value). + /// Thrown if the size of is not a multiple of the size of a DWORD value). + void LoadConstantBuffer(ReadOnlySpan data); +} \ No newline at end of file diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IDispatchDataLoader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IDispatchDataLoader.cs deleted file mode 100644 index 9fddceaf..00000000 --- a/src/ComputeSharp/Core/Interfaces/__Internals/IDispatchDataLoader.cs +++ /dev/null @@ -1,24 +0,0 @@ -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. - void LoadCapturedValues(ReadOnlySpan data); - - /// - /// Loads the captured resources used by the shader to be dispatched. - /// - /// The sequence of serialized resources. - void LoadCapturedResources(ReadOnlySpan data); -} \ No newline at end of file diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs index cf2a9eb7..859829ed 100644 --- a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs @@ -60,18 +60,15 @@ public interface IShader ReadOnlyMemory HlslBytecode { get; } /// - /// Loads the dispatch data for the shader. + /// Loads the constant buffer of a given input 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(ref TLoader loader, GraphicsDevice device, int x, int y, int z) - where TLoader : struct, IDispatchDataLoader; + void LoadConstantBuffer(ref TLoader loader, int x, int y, int z) + where TLoader : struct, IConstantBufferLoader; /// /// Loads the graphics resources for the shader. diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs index b8f66857..b403881d 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs @@ -288,11 +288,11 @@ internal static unsafe class ID3D12GraphicsCommandListExtensions /// /// The instance in use. /// The input buffer with the constant data to bind. - public static void SetComputeRoot32BitConstants(this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ReadOnlySpan data) + public static void SetComputeRoot32BitConstants(this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ReadOnlySpan data) { - fixed (uint* p = data) + fixed (byte* p = data) { - d3D12GraphicsCommandList.SetComputeRoot32BitConstants(0, (uint)data.Length, p, 0); + d3D12GraphicsCommandList.SetComputeRoot32BitConstants(0, (uint)data.Length / sizeof(uint), p, 0); } } } \ No newline at end of file diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index 21152d8f..0f92e29f 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -172,9 +172,13 @@ public struct ComputeContext : IDisposable, IAsyncDisposable commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature); - ComputeShaderDispatchDataLoader dataLoader = new(commandList.D3D12GraphicsCommandList); + D3D12GraphicsCommandListConstantBufferLoader dataLoader = new(commandList.D3D12GraphicsCommandList); - shader.LoadDispatchData(ref dataLoader, this.device, x, y, z); + shader.LoadConstantBuffer(ref dataLoader, x, y, z); + + D3D12GraphicsCommandListGraphicsResourceLoader graphicsResourceLoader = new(commandList.D3D12GraphicsCommandList, this.device, rootParameterOffset: 1); + + shader.LoadGraphicsResources(ref graphicsResourceLoader); commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, (uint)groupsZ); } @@ -206,9 +210,13 @@ public struct ComputeContext : IDisposable, IAsyncDisposable commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature); - PixelShaderDispatchDataLoader dataLoader = new(commandList.D3D12GraphicsCommandList); + D3D12GraphicsCommandListConstantBufferLoader constantBufferLoader = new(commandList.D3D12GraphicsCommandList); - shader.LoadDispatchData(ref dataLoader, this.device, x, y, 1); + shader.LoadConstantBuffer(ref constantBufferLoader, x, y, 1); + + D3D12GraphicsCommandListGraphicsResourceLoader graphicsResourceLoader = new(commandList.D3D12GraphicsCommandList, this.device, rootParameterOffset: 2); + + shader.LoadGraphicsResources(ref graphicsResourceLoader); // Load the implicit output texture commandList.D3D12GraphicsCommandList->SetComputeRootDescriptorTable( diff --git a/src/ComputeSharp/Shaders/Dispatching/ComputeShaderDispatchDataLoader.cs b/src/ComputeSharp/Shaders/Dispatching/ComputeShaderDispatchDataLoader.cs deleted file mode 100644 index 8f131c65..00000000 --- a/src/ComputeSharp/Shaders/Dispatching/ComputeShaderDispatchDataLoader.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using ComputeSharp.Graphics.Extensions; -using ComputeSharp.__Internals; -using TerraFX.Interop.DirectX; -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++; - } - } -} \ No newline at end of file diff --git a/src/ComputeSharp/Shaders/Dispatching/D3D12GraphicsCommandListConstantBufferLoader.cs b/src/ComputeSharp/Shaders/Dispatching/D3D12GraphicsCommandListConstantBufferLoader.cs new file mode 100644 index 00000000..5d8056a5 --- /dev/null +++ b/src/ComputeSharp/Shaders/Dispatching/D3D12GraphicsCommandListConstantBufferLoader.cs @@ -0,0 +1,37 @@ +using System; +using System.Runtime.CompilerServices; +using ComputeSharp.Graphics.Extensions; +using ComputeSharp.__Internals; +using TerraFX.Interop.DirectX; + +namespace ComputeSharp.Shaders.Dispatching; + +/// +/// An implementation for compute shaders dispatched via . +/// +internal readonly unsafe struct D3D12GraphicsCommandListConstantBufferLoader : IConstantBufferLoader +{ + /// + /// The object in use. + /// + private readonly ID3D12GraphicsCommandList* d3D12GraphicsCommandList; + + /// + /// Creates a new instance. + /// + /// The object to use. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal D3D12GraphicsCommandListConstantBufferLoader(ID3D12GraphicsCommandList* d3D12GraphicsCommandList) + { + this.d3D12GraphicsCommandList = d3D12GraphicsCommandList; + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void LoadConstantBuffer(ReadOnlySpan data) + { + default(ArgumentException).ThrowIf((data.Length & 3) != 0, nameof(data)); + + this.d3D12GraphicsCommandList->SetComputeRoot32BitConstants(data); + } +} \ No newline at end of file diff --git a/src/ComputeSharp/Shaders/Dispatching/PixelShaderDispatchDataLoader.cs b/src/ComputeSharp/Shaders/Dispatching/PixelShaderDispatchDataLoader.cs deleted file mode 100644 index c43bf1f1..00000000 --- a/src/ComputeSharp/Shaders/Dispatching/PixelShaderDispatchDataLoader.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using ComputeSharp.Graphics.Extensions; -using ComputeSharp.__Internals; -using TerraFX.Interop.DirectX; - -#pragma warning disable CS0618 - -namespace ComputeSharp.Shaders.Dispatching; - -/// -/// A data loader for pixel shaders. -/// -internal readonly unsafe struct PixelShaderDispatchDataLoader : IDispatchDataLoader -{ - /// - /// The object in use. - /// - private readonly ID3D12GraphicsCommandList* d3D12GraphicsCommandList; - - /// - /// Creates a new instance. - /// - /// The object to use. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal PixelShaderDispatchDataLoader(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 = 2; - - // Skip the first target resource, as that is loaded explicitly by the shader runner using the - // target texture passed in by the user, which is not actually captured by the shader itself. - // The target root descriptor table index is therefore 2 in this case, as it needs to skip not - // only the this implicit texture to draw on, but also the constant buffer with capture values. - while (Unsafe.IsAddressLessThan(ref startRef, ref endRef)) - { - d3D12GraphicsCommandList->SetComputeRootDescriptorTable(offset, startRef); - - startRef = ref Unsafe.Add(ref startRef, 1); - offset++; - } - } -} \ No newline at end of file diff --git a/tests/ComputeSharp.Tests.Internals/Helpers/DebugDispatchDataLoader.cs b/tests/ComputeSharp.Tests.Internals/Helpers/DebugDispatchDataLoader.cs index 9dc41823..b2eb5c49 100644 --- a/tests/ComputeSharp.Tests.Internals/Helpers/DebugDispatchDataLoader.cs +++ b/tests/ComputeSharp.Tests.Internals/Helpers/DebugDispatchDataLoader.cs @@ -9,7 +9,7 @@ namespace ComputeSharp.Tests.Internals.Helpers; /// /// A debug data loader for generic shaders. /// -internal readonly unsafe struct DebugDispatchDataLoader : IDispatchDataLoader +internal readonly unsafe struct DebugDispatchDataLoader : IConstantBufferLoader { /// /// The backing, mutable data for and .