From 51dcb2491f776f11a72f4bc22cdd054abd5ec93c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 16 Jul 2021 17:29:01 +0200 Subject: [PATCH] Added ID3D12CommandListPool type --- .../Commands/Interop/ID3D12CommandListPool.cs | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs diff --git a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs new file mode 100644 index 00000000..b3b16ebd --- /dev/null +++ b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs @@ -0,0 +1,151 @@ +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using ComputeSharp.Core.Extensions; +using ComputeSharp.Graphics.Extensions; +using TerraFX.Interop; +using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; + +namespace ComputeSharp.Graphics.Commands.Interop +{ + /// + /// A type that acts as a pool to get new instances wheen needed. + /// + internal readonly unsafe struct ID3D12CommandListPool : IDisposable + { + /// + /// The command list type being used by the current instance. + /// + private readonly D3D12_COMMAND_LIST_TYPE d3D12CommandListType; + + /// + /// The object in use for the current instance. + /// + private readonly ID3D12DescriptorHeap* d3D12DescriptorHeap; + + /// + /// The queue of items with the available command lists. + /// + private readonly Queue d3D12CommandListBundleQueue; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The command list type to use. + /// The object in use for the current instance. + public ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE d3D12CommandListType, ID3D12DescriptorHeap* d3D12DescriptorHeap) + { + this.d3D12CommandListType = d3D12CommandListType; + this.d3D12DescriptorHeap = d3D12DescriptorHeap; + this.d3D12CommandListBundleQueue = new Queue(); + } + + /// + /// Rents a and pair. + /// + /// The renting the command list. + /// The resulting value. + /// The resulting value. + public void Rent(ID3D12Device* d3D12Device, out ID3D12GraphicsCommandList* d3D12CommandList, out ID3D12CommandAllocator* d3D12CommandAllocator) + { + lock (this.d3D12CommandListBundleQueue) + { + if (this.d3D12CommandListBundleQueue.TryDequeue(out D3D12CommandListBundle d3D12CommandListBundle)) + { + d3D12CommandListBundle.D3D12CommandAllocator->Reset().Assert(); + d3D12CommandListBundle.D3D12CommandList->Reset(d3D12CommandListBundle.D3D12CommandAllocator, null).Assert(); + + d3D12CommandList = d3D12CommandListBundle.D3D12CommandList; + d3D12CommandAllocator = d3D12CommandListBundle.D3D12CommandAllocator; + + return; + } + + CreateCommandListAndAllocator(d3D12Device, out d3D12CommandList, out d3D12CommandAllocator); + } + } + + /// + /// Returns a and pair. + /// + /// The returned value. + /// The returned value. + public void Return(ID3D12GraphicsCommandList* d3D12CommandList, ID3D12CommandAllocator* d3D12CommandAllocator) + { + lock (this.d3D12CommandListBundleQueue) + { + this.d3D12CommandListBundleQueue.Enqueue(new D3D12CommandListBundle(d3D12CommandList, d3D12CommandAllocator)); + } + } + + /// + public void Dispose() + { + lock (this.d3D12CommandListBundleQueue) + { + foreach (D3D12CommandListBundle d3D12CommandListBundle in this.d3D12CommandListBundleQueue) + { + d3D12CommandListBundle.D3D12CommandList->Release(); + d3D12CommandListBundle.D3D12CommandAllocator->Release(); + } + + this.d3D12CommandListBundleQueue.Clear(); + } + } + + /// + /// Creates a new and pair. + /// + /// The renting the command list. + /// The resulting value. + /// The resulting value. + [MethodImpl(MethodImplOptions.NoInlining)] + private void CreateCommandListAndAllocator(ID3D12Device* d3D12Device, out ID3D12GraphicsCommandList* d3D12CommandList, out ID3D12CommandAllocator* d3D12CommandAllocator) + { + using ComPtr d3D12CommandAllocatorComPtr = d3D12Device->CreateCommandAllocator(this.d3D12CommandListType); + using ComPtr d3D12CommandListComPtr = d3D12Device->CreateCommandList(this.d3D12CommandListType, d3D12CommandAllocatorComPtr.Get()); + + // Set the heap descriptor if the command list is not for copy operations + if (this.d3D12CommandListType is not D3D12_COMMAND_LIST_TYPE_COPY) + { + ID3D12DescriptorHeap* d3D12DescriptorHeap = this.d3D12DescriptorHeap; + + d3D12CommandListComPtr.Get()->SetDescriptorHeaps(1, &d3D12DescriptorHeap); + } + + fixed (ID3D12GraphicsCommandList** d3D12CommandListPtr = &d3D12CommandList) + fixed (ID3D12CommandAllocator** d3D12CommandAllocatorPtr = &d3D12CommandAllocator) + { + d3D12CommandListComPtr.CopyTo(d3D12CommandListPtr); + d3D12CommandAllocatorComPtr.CopyTo(d3D12CommandAllocatorPtr); + } + } + + /// + /// A type representing a bundle of a cached command list and related allocator. + /// + private readonly struct D3D12CommandListBundle + { + /// + /// The value for the current entry. + /// + public readonly ID3D12GraphicsCommandList* D3D12CommandList; + + /// + /// The value for the current entry. + /// + public readonly ID3D12CommandAllocator* D3D12CommandAllocator; + + /// + /// Creates a new instance with the given parameters. + /// + /// The value to wrap. + /// The value to wrap. + public D3D12CommandListBundle(ID3D12GraphicsCommandList* d3D12CommandList, ID3D12CommandAllocator* d3D12CommandAllocator) + { + D3D12CommandList = d3D12CommandList; + D3D12CommandAllocator = d3D12CommandAllocator; + } + } + } +}