From 2ceb3b83f6f825941ed819a853806f7fde201a70 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 28 Dec 2021 21:14:49 +0100 Subject: [PATCH] Add initial prototype of Clear APIs --- .../ID3D12GraphicsCommandListExtensions.cs | 50 ++++++++++- src/ComputeSharp/Shaders/ComputeContext.cs | 28 +++++- .../Extensions/ComputeContextExtensions.cs | 90 +++++++++++++++++++ .../um/d3d12/ID3D12GraphicsCommandList.cs | 14 +++ 4 files changed, 177 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs index 987fbf90..ad2664be 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs @@ -1,7 +1,6 @@ using System; +using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop.DirectX; -using static TerraFX.Interop.DirectX.D3D12_RESOURCE_BARRIER_FLAGS; -using static TerraFX.Interop.DirectX.D3D12_RESOURCE_BARRIER_TYPE; namespace ComputeSharp.Graphics.Extensions; @@ -213,13 +212,58 @@ internal static unsafe class ID3D12GraphicsCommandListExtensions /// /// The instance in use. /// The to insert the barrier for. - public static void UAVBarrier(this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ID3D12Resource* d3D12Resource) + public static void UnorderedAccessViewBarrier(this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ID3D12Resource* d3D12Resource) { D3D12_RESOURCE_BARRIER d3D12ResourceBarrier = D3D12_RESOURCE_BARRIER.InitUAV(d3D12Resource); d3D12GraphicsCommandList.ResourceBarrier(1, &d3D12ResourceBarrier); } + /// + /// Clears a target UAV resource. + /// + /// + /// The instance in use. + /// The to clear. + /// The value for the target resource. + /// The value for the target resource. + public static unsafe void ClearUnorderedAccessView( + this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, + ID3D12Resource* d3D12Resource, + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle) + where T : unmanaged + { + if (typeof(T) == typeof(uint)) + { + UInt4 values = default; + + d3D12GraphicsCommandList.ClearUnorderedAccessViewUint( + ViewGPUHandleInCurrentHeap: d3D12GpuDescriptorHandle, + ViewCPUHandle: d3D12CpuDescriptorHandle, + pResource: d3D12Resource, + Values: (uint*)&values, + NumRects: 0, + pRects: null); + } + else if (typeof(T) == typeof(float)) + { + Float4 values = default; + + d3D12GraphicsCommandList.ClearUnorderedAccessViewFloat( + ViewGPUHandleInCurrentHeap: d3D12GpuDescriptorHandle, + ViewCPUHandle: d3D12CpuDescriptorHandle, + pResource: d3D12Resource, + Values: (float*)&values, + NumRects: 0, + pRects: null); + } + else + { + ThrowHelper.ThrowArgumentException("Invalid clear type"); + } + } + /// /// Binds an input constant buffer to the first root parameter. /// diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index 17b0dcd4..cca1e2cb 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -42,6 +42,25 @@ public ref struct ComputeContext /// public GraphicsDevice GraphicsDevice => this.device; + /// + /// Clears a specific resource. + /// + /// The to clear. + /// The value for the target resource. + /// The value for the target resource. + internal readonly unsafe void Clear( + ID3D12Resource* d3D12Resource, + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle) + where T : unmanaged + { + ThrowInvalidOperationExceptionIfDeviceIsNull(); + + ref CommandList commandList = ref GetCommandList(in this, null); + + commandList.D3D12GraphicsCommandList->ClearUnorderedAccessView(d3D12Resource, d3D12GpuDescriptorHandle, d3D12CpuDescriptorHandle); + } + /// /// Inserts a resource barrier for a specific resource. /// @@ -52,7 +71,7 @@ public ref struct ComputeContext ref CommandList commandList = ref GetCommandList(in this); - commandList.D3D12GraphicsCommandList->UAVBarrier(d3D12Resource); + commandList.D3D12GraphicsCommandList->UnorderedAccessViewBarrier(d3D12Resource); } /// @@ -253,7 +272,12 @@ public ref struct ComputeContext if (context.commandList.IsAllocated) { - context.commandList.D3D12GraphicsCommandList->SetPipelineState(pipelineState); + // Skip setting the pipeline state if the new state is null. This is the case when the upcoming + // operation is not a shader dispatch, but just a resource clear. In this case there is no state. + if (pipelineState is not null) + { + context.commandList.D3D12GraphicsCommandList->SetPipelineState(pipelineState); + } } else { diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs index 3fd3e302..6e880332 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -98,6 +98,96 @@ public static partial class ComputeContextExtensions context.Barrier(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice)); } + /// + /// Clears a specific resource. + /// + /// The type of items stored on the buffer. + /// The to use to clear the resource. + /// The input instance to clear. + public static unsafe void Clear(this in ComputeContext context, ReadWriteBuffer buffer) + where T : unmanaged + { + var handles = buffer.ValidateAndGetGpuAndCpuDescriptorHandles(context.GraphicsDevice); + + context.Clear(buffer.D3D12Resource, handles.Gpu, handles.Cpu); + } + + /// + /// Clears a specific resource, with a specified value. + /// + /// The type of items stored on the texture. + /// The to use to clear the resource. + /// The input instance to clear. + public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture2D texture) + where T : unmanaged + { + throw new System.NotImplementedException(); + } + + /// + /// Clears a specific resource, with a specified value. + /// + /// The type of items stored on the texture. + /// The to use to clear the resource. + /// The input instance to clear. + public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture3D texture) + where T : unmanaged + { + throw new System.NotImplementedException(); + } + + /// + /// Clears a specific resource, with a specified value. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + /// The to use to clear the resource. + /// The input instance to clear. + public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture2D texture) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + throw new System.NotImplementedException(); + } + + /// + /// Clears a specific resource, with a specified value. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + /// The to use to clear the resource. + /// The input instance to clear. + public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture3D texture) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + throw new System.NotImplementedException(); + } + + /// + /// Clears a specific resource, with a specified value. + /// + /// The type of pixels stored on the texture. + /// The to use to clear the resource. + /// The input instance to clear. + public static unsafe void Clear(this in ComputeContext context, IReadWriteTexture2D texture) + where TPixel : unmanaged + { + throw new System.NotImplementedException(); + } + + /// + /// Clears a specific resource, with a specified value. + /// + /// The type of pixels stored on the texture. + /// The to use to clear the resource. + /// The input instance to clear. + public static unsafe void Clear(this in ComputeContext context, IReadWriteTexture3D texture) + where TPixel : unmanaged + { + throw new System.NotImplementedException(); + } + /// /// Runs the input shader on a target instance, with the specified parameters. /// diff --git a/src/TerraFX.Interop.Windows/DirectX/um/d3d12/ID3D12GraphicsCommandList.cs b/src/TerraFX.Interop.Windows/DirectX/um/d3d12/ID3D12GraphicsCommandList.cs index cace23fe..ffc02277 100644 --- a/src/TerraFX.Interop.Windows/DirectX/um/d3d12/ID3D12GraphicsCommandList.cs +++ b/src/TerraFX.Interop.Windows/DirectX/um/d3d12/ID3D12GraphicsCommandList.cs @@ -298,5 +298,19 @@ namespace TerraFX.Interop.DirectX { ((delegate* unmanaged[Stdcall])(lpVtbl[42]))((ID3D12GraphicsCommandList*)Unsafe.AsPointer(ref this), RootParameterIndex, BufferLocation); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(49)] + public void ClearUnorderedAccessViewUint(D3D12_GPU_DESCRIPTOR_HANDLE ViewGPUHandleInCurrentHeap, D3D12_CPU_DESCRIPTOR_HANDLE ViewCPUHandle, ID3D12Resource* pResource, [NativeTypeName("const UINT [4]")] uint* Values, uint NumRects, [NativeTypeName("const D3D12_RECT *")] void* pRects) + { + ((delegate* unmanaged[Stdcall])(lpVtbl[49]))((ID3D12GraphicsCommandList*)Unsafe.AsPointer(ref this), ViewGPUHandleInCurrentHeap, ViewCPUHandle, pResource, Values, NumRects, pRects); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(50)] + public void ClearUnorderedAccessViewFloat(D3D12_GPU_DESCRIPTOR_HANDLE ViewGPUHandleInCurrentHeap, D3D12_CPU_DESCRIPTOR_HANDLE ViewCPUHandle, ID3D12Resource* pResource, [NativeTypeName("const FLOAT [4]")] float* Values, uint NumRects, [NativeTypeName("const D3D12_RECT *")] void* pRects) + { + ((delegate* unmanaged[Stdcall])(lpVtbl[50]))((ID3D12GraphicsCommandList*)Unsafe.AsPointer(ref this), ViewGPUHandleInCurrentHeap, ViewCPUHandle, pResource, Values, NumRects, pRects); + } } }