From 342cb64ad7b52bc9ae60e68892dbc2e1e9cabeb2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 15 Oct 2023 17:02:06 +0200 Subject: [PATCH] Add IGraphicsResourceLoader and associated loader --- .../__Internals/IGraphicsResourceLoader.cs | 19 +++++ .../Core/Interfaces/__Internals/IShader.cs | 8 ++ ...aphicsCommandListGraphicsResourceLoader.cs | 75 +++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 src/ComputeSharp/Core/Interfaces/__Internals/IGraphicsResourceLoader.cs create mode 100644 src/ComputeSharp/Shaders/Dispatching/D3D12GraphicsCommandListGraphicsResourceLoader.cs diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IGraphicsResourceLoader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IGraphicsResourceLoader.cs new file mode 100644 index 00000000..d5edd8c8 --- /dev/null +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IGraphicsResourceLoader.cs @@ -0,0 +1,19 @@ +using System; + +namespace ComputeSharp.__Internals; + +/// +/// A type representing a loader object for values used in compute shaders. +/// +public interface IGraphicsResourceLoader +{ + /// + /// Loads a resource used by the shader to be dispatched. + /// + /// The input resource to be loaded. + /// The index to use to bind the resource to the shader. + /// Thrown if is . + /// Thrown if is not a valid resource object. + /// Thrown if the resource isn't associated with the same device used to dispatch the shader. + void LoadGraphicsResource(IGraphicsResource resource, uint index); +} \ 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 df79288c..cf2a9eb7 100644 --- a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs @@ -72,4 +72,12 @@ public interface IShader [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; + + /// + /// Loads the graphics resources for the shader. + /// + /// The type of graphics resource loader being used. + /// The instance to use to load the data. + void LoadGraphicsResources(ref TLoader loader) + where TLoader : struct, IGraphicsResourceLoader; } \ No newline at end of file diff --git a/src/ComputeSharp/Shaders/Dispatching/D3D12GraphicsCommandListGraphicsResourceLoader.cs b/src/ComputeSharp/Shaders/Dispatching/D3D12GraphicsCommandListGraphicsResourceLoader.cs new file mode 100644 index 00000000..4858ee7e --- /dev/null +++ b/src/ComputeSharp/Shaders/Dispatching/D3D12GraphicsCommandListGraphicsResourceLoader.cs @@ -0,0 +1,75 @@ +using System; +using System.Runtime.CompilerServices; +using ComputeSharp.__Internals; +using TerraFX.Interop.DirectX; +using ComputeSharp.Resources.Interop; + +namespace ComputeSharp.Shaders.Dispatching; + +/// +/// An loader for compute shaders dispatched via . +/// +internal readonly unsafe struct D3D12GraphicsCommandListGraphicsResourceLoader : IGraphicsResourceLoader +{ + /// + /// The object in use. + /// + private readonly ID3D12GraphicsCommandList* d3D12GraphicsCommandList; + + /// + /// The object in use. + /// + private readonly GraphicsDevice graphicsDevice; + + /// + /// The offset into the compute root descriptor table for loaded resources. + /// + /// + /// + /// This is used to correctly align the explicit resources captured by shaders, with the implicit ones + /// that are injected by ComputeSharp. For instance, the constant buffer is always added automatically. + /// + /// + /// For compute shaders capturing an implicit texture, this is used to 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. + /// + /// + private readonly uint rootParameterOffset; + + /// + /// Creates a new instance. + /// + /// The object to use. + /// The instance that is tied to. + /// The offset into the compute root descriptor table for loaded resources. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal D3D12GraphicsCommandListGraphicsResourceLoader( + ID3D12GraphicsCommandList* d3D12GraphicsCommandList, + GraphicsDevice graphicsDevice, + uint rootParameterOffset) + { + this.d3D12GraphicsCommandList = d3D12GraphicsCommandList; + this.graphicsDevice = graphicsDevice; + this.rootParameterOffset = rootParameterOffset; + } + + /// + void IGraphicsResourceLoader.LoadGraphicsResource(IGraphicsResource resource, uint index) + { + default(ArgumentNullException).ThrowIfNull(resource); + + if (resource is not ID3D12ReadOnlyResource readOnlyResource) + { + default(ArgumentException).Throw(nameof(resource)); + + return; + } + + this.d3D12GraphicsCommandList->SetComputeRootDescriptorTable( + RootParameterIndex: this.rootParameterOffset + index, + BaseDescriptor: readOnlyResource.ValidateAndGetGpuDescriptorHandle(this.graphicsDevice)); + } +} \ No newline at end of file