diff --git a/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs b/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs new file mode 100644 index 00000000..2798dd83 --- /dev/null +++ b/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs @@ -0,0 +1,120 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.Contracts; +using System.Runtime.InteropServices; +using TerraFX.Interop; +using FX = TerraFX.Interop.Windows; + +namespace ComputeSharp.Graphics.Commands +{ + /// + /// A type that acts as a pool to get new instances wheen needed. + /// + internal 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(ID3D12CommandAllocator* commandAllocator, ulong fenceValue) + { + lock (this.d3d12CommandAllocatorQueue) + { + this.d3d12CommandAllocatorQueue.Enqueue(new(commandAllocator, fenceValue)); + } + } + + /// + /// Gets a new or reused item to use to issue GPU commands. + /// + [Pure] + public ID3D12CommandAllocator* GetCommandAllocator(ID3D12Device* d3D12Device, ID3D12Fence* d3D12Fence) + { + lock (this.d3d12CommandAllocatorQueue) + { + // See if there is a reusable command allocator + if (this.d3d12CommandAllocatorQueue.Count > 0) + { + ID3D12CommandAllocatorEntry d3D12CommandAllocatorEntry = this.d3d12CommandAllocatorQueue.Peek(); + + // We found one that is no longer in use, reset it and return it + if (d3D12CommandAllocatorEntry.FenceValue <= d3D12Fence->GetCompletedValue()) + { + this.d3d12CommandAllocatorQueue.Dequeue(); + + d3D12CommandAllocatorEntry.D3D12CommandAllocator->Reset(); + + return d3D12CommandAllocatorEntry.D3D12CommandAllocator; + } + } + + // Create a new command allocator + ID3D12CommandAllocator* d3D12CommandAllocator; + Guid d3D12CommandAllocatorGuid = FX.IID_ID3D12CommandAllocator; + + int result = d3D12Device->CreateCommandAllocator( + this.d3d12CommandListType, + &d3D12CommandAllocatorGuid, + (void**)&d3D12CommandAllocator); + + if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + + return d3D12CommandAllocator; + } + } + + /// + public void Dispose() + { + throw new NotImplementedException(); + } + + /// + /// A type that stores info on pooled references. + /// + private readonly unsafe struct ID3D12CommandAllocatorEntry + { + /// + /// The current reference. + /// + public readonly ID3D12CommandAllocator* 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(ID3D12CommandAllocator* d3D12CommandAllocator, ulong fenceValue) + { + D3D12CommandAllocator = d3D12CommandAllocator; + FenceValue = fenceValue; + } + } + } +}