From 5e2a822a24ac06c3a2f0578617e0fbeedb71152f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 17 Nov 2020 01:50:21 +0100 Subject: [PATCH] Optimized command allocator pool type --- .../Commands/CommandAllocatorPool2.cs | 123 ------------------ .../Interop/ID3D12CommandAllocatorPool.cs | 83 ++++++++++++ src/ComputeSharp.Graphics/GraphicsDevice2.cs | 22 ++-- 3 files changed, 94 insertions(+), 134 deletions(-) delete mode 100644 src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs create mode 100644 src/ComputeSharp.Graphics/Commands/Interop/ID3D12CommandAllocatorPool.cs diff --git a/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs b/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs deleted file mode 100644 index 31a412bf..00000000 --- a/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs +++ /dev/null @@ -1,123 +0,0 @@ -using ComputeSharp.Graphics.Extensions; -using ComputeSharp.Graphics.Helpers; -using System; -using System.Collections.Generic; -using System.Diagnostics.Contracts; -using TerraFX.Interop; - -namespace ComputeSharp.Graphics.Commands -{ - /// - /// A type that acts as a pool to get new instances wheen needed. - /// - internal readonly unsafe struct CommandAllocatorPool2 - { - /// - /// The command list type being used by the current instance. - /// - private readonly D3D12_COMMAND_LIST_TYPE d3d12CommandListType; - - /// - /// The queue of currently pooled instances. - /// - private readonly Queue d3d12CommandAllocatorQueue; - - /// - /// Creates a new instance with the specified values. - /// - /// The command list type to use. - public CommandAllocatorPool2(D3D12_COMMAND_LIST_TYPE d3d12CommandListType) - { - this.d3d12CommandListType = d3d12CommandListType; - this.d3d12CommandAllocatorQueue = new(); - } - - /// - /// Adds a new item to the internal pool queue. - /// - /// The input to enqueue. - /// The fence value for the input . - public void Enqueue(ComPtr commandAllocator, ulong fenceValue) - { - lock (this.d3d12CommandAllocatorQueue) - { - this.d3d12CommandAllocatorQueue.Enqueue(new(commandAllocator, fenceValue)); - } - } - - /// - /// Gets a new or reused item to use to issue GPU commands. - /// - /// The rented object to use. - public ComPtr GetCommandAllocator(ID3D12Device* d3D12Device, ID3D12Fence* d3D12Fence) - { - using ComPtr d3D12CommandAllocator = default; - - lock (this.d3d12CommandAllocatorQueue) - { - // See if there is a reusable command allocator - if (this.d3d12CommandAllocatorQueue.Count > 0) - { - ID3D12CommandAllocatorEntry d3D12CommandAllocatorEntry = this.d3d12CommandAllocatorQueue.Peek(); - - d3D12CommandAllocatorEntry.D3D12CommandAllocator.Swap(ref *&d3D12CommandAllocator); - - // We found one that is no longer in use, reset it and return it - if (d3D12CommandAllocatorEntry.FenceValue <= d3D12Fence->GetCompletedValue()) - { - _ = this.d3d12CommandAllocatorQueue.Dequeue(); - - int result = d3D12CommandAllocator.Get()->Reset(); - - ThrowHelper.ThrowIfFailed(result); - - return d3D12CommandAllocator.Move(); - } - } - - return d3D12Device->CreateCommandAllocator(this.d3d12CommandListType); - } - } - - /// - public void Dispose() - { - lock (this.d3d12CommandAllocatorQueue) - { - foreach (var entry in this.d3d12CommandAllocatorQueue) - { - entry.D3D12CommandAllocator.Dispose(); - } - - this.d3d12CommandAllocatorQueue.Clear(); - } - } - - /// - /// A type that stores info on pooled references. - /// - private unsafe struct ID3D12CommandAllocatorEntry - { - /// - /// The current reference. - /// - public ComPtr D3D12CommandAllocator; - - /// - /// The fence value for the current reference. - /// - public readonly ulong FenceValue; - - /// - /// Creates a new instance with the specified parameters. - /// - /// The input reference. - /// The fence value for the input reference. - public ID3D12CommandAllocatorEntry(ComPtr d3D12CommandAllocator, ulong fenceValue) - { - D3D12CommandAllocator = d3D12CommandAllocator; - FenceValue = fenceValue; - } - } - } -} diff --git a/src/ComputeSharp.Graphics/Commands/Interop/ID3D12CommandAllocatorPool.cs b/src/ComputeSharp.Graphics/Commands/Interop/ID3D12CommandAllocatorPool.cs new file mode 100644 index 00000000..18281468 --- /dev/null +++ b/src/ComputeSharp.Graphics/Commands/Interop/ID3D12CommandAllocatorPool.cs @@ -0,0 +1,83 @@ +using ComputeSharp.Graphics.Extensions; +using ComputeSharp.Graphics.Helpers; +using System; +using System.Collections.Generic; +using TerraFX.Interop; + +namespace ComputeSharp.Graphics.Commands +{ + /// + /// A type that acts as a pool to get new instances wheen needed. + /// + internal readonly unsafe struct ID3D12CommandAllocatorPool + { + /// + /// The command list type being used by the current instance. + /// + private readonly D3D12_COMMAND_LIST_TYPE d3d12CommandListType; + + /// + /// The queue of currently pooled instances. + /// + private readonly Queue> d3d12CommandAllocatorQueue; + + /// + /// Creates a new instance with the specified values. + /// + /// The command list type to use. + public ID3D12CommandAllocatorPool(D3D12_COMMAND_LIST_TYPE d3d12CommandListType) + { + this.d3d12CommandListType = d3d12CommandListType; + this.d3d12CommandAllocatorQueue = new(); + } + + /// + /// Adds a new item to the internal pool queue. + /// + /// The input to enqueue. + public void Enqueue(ComPtr d3D12CommandAllocator) + { + lock (this.d3d12CommandAllocatorQueue) + { + this.d3d12CommandAllocatorQueue.Enqueue(d3D12CommandAllocator); + } + } + + /// + /// Gets a new or reused item to use to issue GPU commands. + /// + /// The rented object to use. + public ComPtr GetCommandAllocator(ID3D12Device* d3D12Device, ID3D12Fence* d3D12Fence) + { + using ComPtr d3D12CommandAllocator = default; + + lock (this.d3d12CommandAllocatorQueue) + { + if (this.d3d12CommandAllocatorQueue.TryDequeue(out *&d3D12CommandAllocator)) + { + int result = d3D12CommandAllocator.Get()->Reset(); + + ThrowHelper.ThrowIfFailed(result); + + return d3D12CommandAllocator.Move(); + } + + return d3D12Device->CreateCommandAllocator(this.d3d12CommandListType); + } + } + + /// + public void Dispose() + { + lock (this.d3d12CommandAllocatorQueue) + { + foreach (ComPtr d3D12CommandAllocator in this.d3d12CommandAllocatorQueue) + { + d3D12CommandAllocator.Dispose(); + } + + this.d3d12CommandAllocatorQueue.Clear(); + } + } + } +} diff --git a/src/ComputeSharp.Graphics/GraphicsDevice2.cs b/src/ComputeSharp.Graphics/GraphicsDevice2.cs index 6e2e6a18..47e169de 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice2.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice2.cs @@ -40,14 +40,14 @@ namespace ComputeSharp.Graphics private ComPtr d3D12CopyFence; /// - /// The instance for compute operations. + /// The instance for compute operations. /// - private readonly CommandAllocatorPool2 computeCommandAllocatorPool; + private readonly ID3D12CommandAllocatorPool computeCommandAllocatorPool; /// - /// Gets the instance for copy operations. + /// Gets the instance for copy operations. /// - private readonly CommandAllocatorPool2 copyCommandAllocatorPool; + private readonly ID3D12CommandAllocatorPool copyCommandAllocatorPool; /// /// The instance to use when allocating new buffers. @@ -77,8 +77,8 @@ namespace ComputeSharp.Graphics this.d3D12ComputeFence = d3d12device.Get()->CreateFence(); this.d3D12CopyFence = d3d12device.Get()->CreateFence(); - this.computeCommandAllocatorPool = new CommandAllocatorPool2(D3D12_COMMAND_LIST_TYPE_COMPUTE); - this.copyCommandAllocatorPool = new CommandAllocatorPool2(D3D12_COMMAND_LIST_TYPE_COPY); + this.computeCommandAllocatorPool = new ID3D12CommandAllocatorPool(D3D12_COMMAND_LIST_TYPE_COMPUTE); + this.copyCommandAllocatorPool = new ID3D12CommandAllocatorPool(D3D12_COMMAND_LIST_TYPE_COPY); this.shaderResourceViewDescriptorAllocator = new DescriptorAllocator2(d3d12device); Name = new string((char*)dxgiDescription1->Description); @@ -123,7 +123,7 @@ namespace ComputeSharp.Graphics this.shaderResourceViewDescriptorAllocator.Allocate(out d3d12CpuDescriptorHandle, out d3d12GpuDescriptorHandle); } - /// + /// /// The type of command allocator to rent. internal ComPtr GetCommandAllocator(D3D12_COMMAND_LIST_TYPE d3d12CommandListType) { @@ -152,7 +152,7 @@ namespace ComputeSharp.Graphics /// The input to execute. internal void ExecuteCommandList(ref CommandList2 commandList) { - ref readonly CommandAllocatorPool2 commandAllocatorPool = ref Unsafe.NullRef(); + ref readonly ID3D12CommandAllocatorPool commandAllocatorPool = ref Unsafe.NullRef(); ID3D12CommandQueue* d3D12CommandQueue; ID3D12Fence* d3D12Fence; ulong d3D12FenceValue; @@ -175,9 +175,6 @@ namespace ComputeSharp.Graphics default: throw null!; } - // Enqueue the command allocator pool so that it can be reused later - commandAllocatorPool.Enqueue(commandList.DetachD3D12CommandAllocator(), d3D12FenceValue); - // Execute the command list and signal to the target fence d3D12CommandQueue->ExecuteCommandLists(1, commandList.GetD3D12CommandListAddressOf()); @@ -192,6 +189,9 @@ namespace ComputeSharp.Graphics ThrowHelper.ThrowIfFailed(result); } + + // Enqueue the command allocator pool so that it can be reused later + commandAllocatorPool.Enqueue(commandList.DetachD3D12CommandAllocator()); } ///