From db01f40030ba4a06706f2abb5c83586fe09cba62 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 28 Dec 2021 13:55:59 +0100 Subject: [PATCH] Add [Start|End|Full]Barrier APIs to ComputeContext type --- .../__Internals/GraphicsResourceHelper.cs | 9 +- .../Resources/Abstract/StructuredBuffer{T}.cs | 9 + .../Resources/Abstract/Texture2D{T}.cs | 15 + .../Resources/Abstract/Texture3D{T}.cs | 15 + src/ComputeSharp/Shaders/ComputeContext.cs | 5 + .../Extensions/ComputeContextExtensions.cs | 267 ++++++++++++++++++ 6 files changed, 319 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs index fb40d83b..48df0aa0 100644 --- a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs @@ -17,7 +17,7 @@ public static class GraphicsResourceHelper /// /// An interface for non-generic graphics resource types. /// - internal interface IGraphicsResource + internal unsafe interface IGraphicsResource { /// /// Validates the given resource for usage with a specified device, and retrieves its GPU descriptor handle. @@ -25,6 +25,13 @@ public static class GraphicsResourceHelper /// The target instance in use. /// The GPU descriptor handle for the resource. D3D12_GPU_DESCRIPTOR_HANDLE ValidateAndGetGpuDescriptorHandle(GraphicsDevice device); + + /// + /// Validates the given resource for usage with a specified device, and retrieves the underlying object. + /// + /// The target instance in use. + /// The the underlying object. + ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index f4ebdea0..b92907c1 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -306,4 +306,13 @@ public abstract class StructuredBuffer : Buffer copyCommandList.ExecuteAndWaitForCompletion(); } } + + /// + internal unsafe ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + return D3D12Resource; + } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index af1cf799..a0d0e834 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -555,6 +555,15 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper } } + /// + internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + return D3D12Resource; + } + /// D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { @@ -563,4 +572,10 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper return D3D12GpuDescriptorHandle; } + + /// + ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device) + { + return ValidateAndGetID3D12Resource(device); + } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 1120489a..8121175a 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -608,6 +608,15 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper } } + /// + internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + return D3D12Resource; + } + /// D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) { @@ -616,4 +625,10 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper return D3D12GpuDescriptorHandle; } + + /// + ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device) + { + return ValidateAndGetID3D12Resource(device); + } } diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index d4e5f85e..9df85400 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -37,6 +37,11 @@ public ref struct ComputeContext this.commandList = default; } + /// + /// Gets the associated with the current instance. + /// + public GraphicsDevice GraphicsDevice => this.device; + /// /// Starts a resource barrier for a specific resource. /// diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs index 0852b281..df2d30ef 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -1,4 +1,7 @@ using System.Runtime.CompilerServices; +using ComputeSharp.__Internals; + +#pragma warning disable CS0618 namespace ComputeSharp; @@ -7,6 +10,270 @@ namespace ComputeSharp; /// public static partial class ComputeContextExtensions { + /// + /// Starts a resource barrier for a specific resource. + /// + /// The type of items stored on the buffer. + /// The to use to insert the resource barrier. + /// The input instance to start the barrier for. + public static unsafe void StartBarrier(this in ComputeContext context, ReadWriteBuffer buffer) + where T : unmanaged + { + context.StartBarrier(buffer.ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Starts a resource barrier for a specific resource. + /// + /// The type of items stored on the texture. + /// The to use to insert the resource barrier. + /// The input instance to start the barrier for. + public static unsafe void StartBarrier(this in ComputeContext context, ReadWriteTexture2D texture) + where T : unmanaged + { + context.StartBarrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Starts a resource barrier for a specific resource. + /// + /// The type of items stored on the texture. + /// The to use to insert the resource barrier. + /// The input instance to start the barrier for. + public static unsafe void StartBarrier(this in ComputeContext context, ReadWriteTexture3D texture) + where T : unmanaged + { + context.StartBarrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Starts a resource barrier for a specific resource. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + /// The to use to insert the resource barrier. + /// The input instance to start the barrier for. + public static unsafe void StartBarrier(this in ComputeContext context, ReadWriteTexture2D texture) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + context.StartBarrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Starts a resource barrier for a specific resource. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + /// The to use to insert the resource barrier. + /// The input instance to start the barrier for. + public static unsafe void StartBarrier(this in ComputeContext context, ReadWriteTexture3D texture) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + context.StartBarrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Starts a resource barrier for a specific resource. + /// + /// The type of pixels stored on the texture. + /// The to use to insert the resource barrier. + /// The input instance to start the barrier for. + public static unsafe void StartBarrier(this in ComputeContext context, IReadWriteTexture2D texture) + where TPixel : unmanaged + { + context.StartBarrier(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Starts a resource barrier for a specific resource. + /// + /// The type of pixels stored on the texture. + /// The to use to insert the resource barrier. + /// The input instance to start the barrier for. + public static unsafe void StartBarrier(this in ComputeContext context, IReadWriteTexture3D texture) + where TPixel : unmanaged + { + context.StartBarrier(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Ends a resource barrier for a specific resource. + /// + /// The type of items stored on the buffer. + /// The to use to insert the resource barrier. + /// The input instance to end the barrier for. + public static unsafe void EndBarrier(this in ComputeContext context, ReadWriteBuffer buffer) + where T : unmanaged + { + context.EndBarrier(buffer.ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Ends a resource barrier for a specific resource. + /// + /// The type of items stored on the texture. + /// The to use to insert the resource barrier. + /// The input instance to end the barrier for. + public static unsafe void EndBarrier(this in ComputeContext context, ReadWriteTexture2D texture) + where T : unmanaged + { + context.EndBarrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Ends a resource barrier for a specific resource. + /// + /// The type of items stored on the texture. + /// The to use to insert the resource barrier. + /// The input instance to end the barrier for. + public static unsafe void EndBarrier(this in ComputeContext context, ReadWriteTexture3D texture) + where T : unmanaged + { + context.EndBarrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Ends a resource barrier for a specific resource. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + /// The to use to insert the resource barrier. + /// The input instance to end the barrier for. + public static unsafe void EndBarrier(this in ComputeContext context, ReadWriteTexture2D texture) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + context.EndBarrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Ends a resource barrier for a specific resource. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + /// The to use to insert the resource barrier. + /// The input instance to end the barrier for. + public static unsafe void EndBarrier(this in ComputeContext context, ReadWriteTexture3D texture) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + context.EndBarrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Ends a resource barrier for a specific resource. + /// + /// The type of pixels stored on the texture. + /// The to use to insert the resource barrier. + /// The input instance to end the barrier for. + public static unsafe void EndBarrier(this in ComputeContext context, IReadWriteTexture2D texture) + where TPixel : unmanaged + { + context.EndBarrier(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Ends a resource barrier for a specific resource. + /// + /// The type of pixels stored on the texture. + /// The to use to insert the resource barrier. + /// The input instance to end the barrier for. + public static unsafe void EndBarrier(this in ComputeContext context, IReadWriteTexture3D texture) + where TPixel : unmanaged + { + context.EndBarrier(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Inserts a full resource barrier for a specific resource. + /// + /// The type of items stored on the buffer. + /// The to use to insert the resource barrier. + /// The input instance to insert the barrier for. + public static unsafe void FullBarrier(this in ComputeContext context, ReadWriteBuffer buffer) + where T : unmanaged + { + context.FullBarrier(buffer.ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Inserts a full resource barrier for a specific resource. + /// + /// The type of items stored on the texture. + /// The to use to insert the resource barrier. + /// The input instance to insert the barrier for. + public static unsafe void FullBarrier(this in ComputeContext context, ReadWriteTexture2D texture) + where T : unmanaged + { + context.FullBarrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Inserts a full resource barrier for a specific resource. + /// + /// The type of items stored on the texture. + /// The to use to insert the resource barrier. + /// The input instance to insert the barrier for. + public static unsafe void FullBarrier(this in ComputeContext context, ReadWriteTexture3D texture) + where T : unmanaged + { + context.FullBarrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Inserts a full resource barrier for a specific resource. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + /// The to use to insert the resource barrier. + /// The input instance to insert the barrier for. + public static unsafe void FullBarrier(this in ComputeContext context, ReadWriteTexture2D texture) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + context.FullBarrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Inserts a full resource barrier for a specific resource. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + /// The to use to insert the resource barrier. + /// The input instance to insert the barrier for. + public static unsafe void FullBarrier(this in ComputeContext context, ReadWriteTexture3D texture) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + context.FullBarrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Inserts a full resource barrier for a specific resource. + /// + /// The type of pixels stored on the texture. + /// The to use to insert the resource barrier. + /// The input instance to insert the barrier for. + public static unsafe void FullBarrier(this in ComputeContext context, IReadWriteTexture2D texture) + where TPixel : unmanaged + { + context.FullBarrier(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + + /// + /// Inserts a full resource barrier for a specific resource. + /// + /// The type of pixels stored on the texture. + /// The to use to insert the resource barrier. + /// The input instance to insert the barrier for. + public static unsafe void FullBarrier(this in ComputeContext context, IReadWriteTexture3D texture) + where TPixel : unmanaged + { + context.FullBarrier(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice)); + } + /// /// Runs the input shader on a target instance, with the specified parameters. ///