From 4e3bfb5362b543658bcd420f99ec966df0317b27 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 13 Aug 2019 22:33:01 +0200 Subject: [PATCH] Code refactoring --- .../CommandAllocatorPool.cs | 72 +++++++++++++------ src/ComputeSharp.Graphics/CommandList.cs | 3 + 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/src/ComputeSharp.Graphics/CommandAllocatorPool.cs b/src/ComputeSharp.Graphics/CommandAllocatorPool.cs index a14dccd0..6f802d8a 100644 --- a/src/ComputeSharp.Graphics/CommandAllocatorPool.cs +++ b/src/ComputeSharp.Graphics/CommandAllocatorPool.cs @@ -1,51 +1,65 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Contracts; using SharpDX.Direct3D12; namespace ComputeSharp.Graphics { + /// + /// A that acts as a pool to get new instances wheen needed + /// internal sealed class CommandAllocatorPool : IDisposable { - private readonly Queue<(CommandAllocator, long)> commandAllocatorQueue = new Queue<(CommandAllocator, long)>(); + /// + /// The queue of currently pooled instances + /// + private readonly Queue<(CommandAllocator CommandAllocator, long FenceValue)> CommandAllocatorQueue = new Queue<(CommandAllocator, long)>(); + /// + /// Creates a new instance with the specified parameters + /// + /// The input to use to perform requested operations + /// The type of command list to use for the current instance public CommandAllocatorPool(GraphicsDevice device, CommandListType commandListType) { GraphicsDevice = device; CommandListType = commandListType; } - public CommandListType CommandListType { get; } - + /// + /// Gets the object targeted by the current instance + /// public GraphicsDevice GraphicsDevice { get; } - public void Dispose() - { - lock (commandAllocatorQueue) - { - foreach ((CommandAllocator commandAllocator, long _) in commandAllocatorQueue) - { - commandAllocator.Dispose(); - } - - commandAllocatorQueue.Clear(); - } - } + /// + /// Gets the command list type being used by the current instance + /// + public CommandListType CommandListType { get; } + /// + /// Adds a new item to the internal pool queue + /// + /// The input to enqueue + /// The fence value for the input public void Enqueue(CommandAllocator commandAllocator, long fenceValue) { - lock (commandAllocatorQueue) + lock (CommandAllocatorQueue) { - commandAllocatorQueue.Enqueue((commandAllocator, fenceValue)); + CommandAllocatorQueue.Enqueue((commandAllocator, fenceValue)); } } + /// + /// Gets a new or reused item to use to issue GPU commands + /// + [Pure] public CommandAllocator GetCommandAllocator() { - lock (commandAllocatorQueue) + lock (CommandAllocatorQueue) { - if (commandAllocatorQueue.Count > 0) + if (CommandAllocatorQueue.Count > 0) { - (CommandAllocator commandAllocator, long fenceValue) = commandAllocatorQueue.Peek(); + (CommandAllocator commandAllocator, long fenceValue) = CommandAllocatorQueue.Peek(); long completedValue = CommandListType switch { @@ -58,14 +72,28 @@ namespace ComputeSharp.Graphics if (fenceValue <= completedValue) { - commandAllocatorQueue.Dequeue(); + CommandAllocatorQueue.Dequeue(); commandAllocator.Reset(); return commandAllocator; } } - return GraphicsDevice.NativeDevice.CreateCommandAllocator((SharpDX.Direct3D12.CommandListType)CommandListType); + return GraphicsDevice.NativeDevice.CreateCommandAllocator(CommandListType); + } + } + + /// + public void Dispose() + { + lock (CommandAllocatorQueue) + { + foreach ((CommandAllocator commandAllocator, long _) in CommandAllocatorQueue) + { + commandAllocator.Dispose(); + } + + CommandAllocatorQueue.Clear(); } } } diff --git a/src/ComputeSharp.Graphics/CommandList.cs b/src/ComputeSharp.Graphics/CommandList.cs index 355f1981..2502c5ee 100644 --- a/src/ComputeSharp.Graphics/CommandList.cs +++ b/src/ComputeSharp.Graphics/CommandList.cs @@ -4,6 +4,9 @@ using SharpDX.Direct3D12; namespace ComputeSharp.Graphics { + /// + /// A that represents a list of commands to issue to a GPU + /// public sealed class CommandList : IDisposable { private readonly CompiledCommandList currentCommandList;