diff --git a/src/ComputeSharp/Graphics/Commands/CommandList.cs b/src/ComputeSharp/Graphics/Commands/CommandList.cs
index 993ece90..42f0ffc8 100644
--- a/src/ComputeSharp/Graphics/Commands/CommandList.cs
+++ b/src/ComputeSharp/Graphics/Commands/CommandList.cs
@@ -1,10 +1,7 @@
using System;
using System.Diagnostics.Contracts;
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
{
@@ -23,16 +20,16 @@ namespace ComputeSharp.Graphics.Commands
///
private readonly D3D12_COMMAND_LIST_TYPE d3D12CommandListType;
- ///
- /// The object in use by the current instance.
- ///
- private ComPtr d3D12CommandAllocator;
-
///
/// The object in use by the current instance.
///
private ComPtr d3D12GraphicsCommandList;
+ ///
+ /// The object in use by the current instance.
+ ///
+ private ComPtr d3D12CommandAllocator;
+
///
/// Creates a new instance with the specified parameters.
///
@@ -42,14 +39,14 @@ namespace ComputeSharp.Graphics.Commands
{
this.device = device;
this.d3D12CommandListType = d3D12CommandListType;
- this.d3D12CommandAllocator = device.GetCommandAllocator(d3D12CommandListType);
- this.d3D12GraphicsCommandList = device.D3D12Device->CreateCommandList(d3D12CommandListType, this.d3D12CommandAllocator);
- // Set the heap descriptor if the command list is not for copy operations
- if (d3D12CommandListType is not D3D12_COMMAND_LIST_TYPE_COPY)
- {
- device.SetDescriptorHeapForCommandList(this.d3D12GraphicsCommandList);
- }
+ Unsafe.SkipInit(out this.d3D12GraphicsCommandList);
+ Unsafe.SkipInit(out this.d3D12CommandAllocator);
+
+ device.GetCommandListAndAllocator(
+ d3D12CommandListType,
+ out *(ID3D12GraphicsCommandList**)Unsafe.AsPointer(ref this.d3D12GraphicsCommandList),
+ out *(ID3D12CommandAllocator**)Unsafe.AsPointer(ref this.d3D12CommandAllocator));
}
///
@@ -67,9 +64,19 @@ namespace ComputeSharp.Graphics.Commands
///
/// The object in use, with ownership.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public ComPtr DetachD3D12CommandAllocator()
+ public ID3D12CommandAllocator* DetachD3D12CommandAllocator()
{
- return this.d3D12CommandAllocator.Move();
+ return this.d3D12CommandAllocator.Detach();
+ }
+
+ ///
+ /// Detaches the object in use by the current instance.
+ ///
+ /// The object in use, with ownership.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public ID3D12GraphicsCommandList* DetachD3D12CommandList()
+ {
+ return this.d3D12GraphicsCommandList.Detach();
}
///
diff --git a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandAllocatorPool.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandAllocatorPool.cs
deleted file mode 100644
index 85461480..00000000
--- a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandAllocatorPool.cs
+++ /dev/null
@@ -1,81 +0,0 @@
-using System;
-using System.Collections.Generic;
-using ComputeSharp.Core.Extensions;
-using ComputeSharp.Graphics.Extensions;
-using TerraFX.Interop;
-
-namespace ComputeSharp.Graphics.Commands.Interop
-{
- ///
- /// 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))
- {
- d3D12CommandAllocator.Get()->Reset().Assert();
-
- 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/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs
index ebdff56e..bc7d8cb2 100644
--- a/src/ComputeSharp/Graphics/GraphicsDevice.cs
+++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs
@@ -47,21 +47,21 @@ namespace ComputeSharp
///
private ComPtr d3D12CopyFence;
- ///
- /// The instance for compute operations.
- ///
- private readonly ID3D12CommandAllocatorPool computeCommandAllocatorPool;
-
- ///
- /// Gets the instance for copy operations.
- ///
- private readonly ID3D12CommandAllocatorPool copyCommandAllocatorPool;
-
///
/// The instance to use when allocating new buffers.
///
private ID3D12DescriptorHandleAllocator shaderResourceViewDescriptorAllocator;
+ ///
+ /// The instance for compute operations.
+ ///
+ private readonly ID3D12CommandListPool computeCommandListPool;
+
+ ///
+ /// Gets the instance for copy operations.
+ ///
+ private readonly ID3D12CommandListPool copyCommandListPool;
+
///
/// The next fence value for compute operations using .
///
@@ -91,14 +91,15 @@ namespace ComputeSharp
internal GraphicsDevice(ID3D12Device* d3D12Device, IDXGIAdapter* dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1)
{
this.d3D12Device = new ComPtr(d3D12Device);
+
this.d3D12ComputeCommandQueue = d3D12Device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COMPUTE);
this.d3D12CopyCommandQueue = d3D12Device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COPY);
this.d3D12ComputeFence = d3D12Device->CreateFence();
this.d3D12CopyFence = d3D12Device->CreateFence();
- this.computeCommandAllocatorPool = new ID3D12CommandAllocatorPool(D3D12_COMMAND_LIST_TYPE_COMPUTE);
- this.copyCommandAllocatorPool = new ID3D12CommandAllocatorPool(D3D12_COMMAND_LIST_TYPE_COPY);
this.shaderResourceViewDescriptorAllocator = new ID3D12DescriptorHandleAllocator(d3D12Device);
+ this.computeCommandListPool = new ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE_COMPUTE, this.shaderResourceViewDescriptorAllocator.D3D12DescriptorHeap);
+ this.copyCommandListPool = new ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE_COPY, null);
Luid = Luid.FromLUID(dxgiDescription1->AdapterLuid);
Name = new string((char*)dxgiDescription1->Description);
@@ -266,27 +267,29 @@ namespace ComputeSharp
this.shaderResourceViewDescriptorAllocator.Return(d3D12CpuDescriptorHandle, d3D12GpuDescriptorHandle);
}
- ///
+ ///
/// The type of command allocator to rent.
- internal ComPtr GetCommandAllocator(D3D12_COMMAND_LIST_TYPE d3D12CommandListType)
+ /// The resulting value.
+ /// The resulting value.
+ internal void GetCommandListAndAllocator(
+ D3D12_COMMAND_LIST_TYPE d3D12CommandListType,
+ out ID3D12GraphicsCommandList* d3D12CommandList,
+ out ID3D12CommandAllocator* d3D12CommandAllocator)
{
- return d3D12CommandListType switch
+ switch (d3D12CommandListType)
{
- D3D12_COMMAND_LIST_TYPE_COMPUTE => this.computeCommandAllocatorPool.GetCommandAllocator(this.d3D12Device, this.d3D12ComputeFence),
- D3D12_COMMAND_LIST_TYPE_COPY => this.copyCommandAllocatorPool.GetCommandAllocator(this.d3D12Device, this.d3D12CopyFence),
- _ => ThrowHelper.ThrowArgumentException>()
- };
- }
-
- ///
- /// Sets the descriptor heap for a given instance.
- ///
- /// The input instance to use.
- internal void SetDescriptorHeapForCommandList(ID3D12GraphicsCommandList* d3D12GraphicsCommandList)
- {
- ID3D12DescriptorHeap* d3D12DescriptorHeap = this.shaderResourceViewDescriptorAllocator.D3D12DescriptorHeap;
-
- d3D12GraphicsCommandList->SetDescriptorHeaps(1, &d3D12DescriptorHeap);
+ case D3D12_COMMAND_LIST_TYPE_COMPUTE:
+ this.computeCommandListPool.Rent(this.d3D12Device.Get(), out d3D12CommandList, out d3D12CommandAllocator);
+ break;
+ case D3D12_COMMAND_LIST_TYPE_COPY:
+ this.copyCommandListPool.Rent(this.d3D12Device.Get(), out d3D12CommandList, out d3D12CommandAllocator);
+ break;
+ default:
+ ThrowHelper.ThrowArgumentException>();
+ d3D12CommandList = null;
+ d3D12CommandAllocator = null;
+ break;
+ }
}
///
@@ -295,7 +298,7 @@ namespace ComputeSharp
/// The input to execute.
internal void ExecuteCommandList(ref CommandList commandList)
{
- ref readonly ID3D12CommandAllocatorPool commandAllocatorPool = ref Unsafe.NullRef();
+ ref readonly ID3D12CommandListPool commandListPool = ref Unsafe.NullRef();
ID3D12CommandQueue* d3D12CommandQueue;
ID3D12Fence* d3D12Fence;
ulong d3D12FenceValue;
@@ -304,13 +307,13 @@ namespace ComputeSharp
switch (commandList.D3D12CommandListType)
{
case D3D12_COMMAND_LIST_TYPE_COMPUTE:
- commandAllocatorPool = ref this.computeCommandAllocatorPool;
+ commandListPool = ref this.computeCommandListPool;
d3D12CommandQueue = this.d3D12ComputeCommandQueue;
d3D12Fence = this.d3D12ComputeFence;
d3D12FenceValue = this.nextD3D12ComputeFenceValue++;
break;
case D3D12_COMMAND_LIST_TYPE_COPY:
- commandAllocatorPool = ref this.copyCommandAllocatorPool;
+ commandListPool = ref this.copyCommandListPool;
d3D12CommandQueue = this.d3D12CopyCommandQueue;
d3D12Fence = this.d3D12CopyFence;
d3D12FenceValue = this.nextD3D12CopyFenceValue++;
@@ -323,14 +326,17 @@ namespace ComputeSharp
d3D12CommandQueue->Signal(d3D12Fence, d3D12FenceValue).Assert();
+ // Enqueue the command allocator pool so that it can be reused later. We can do this
+ // before checking and awaiting the fence as the command list and allocaton can already
+ // be reused right after the call to ExecuteCommandLists, and this way we can potentially
+ // do this work in parallel while the GPU dispatching is still being executed.
+ commandListPool.Return(commandList.DetachD3D12CommandList(), commandList.DetachD3D12CommandAllocator());
+
// If the fence value hasn't been reached, wait until the operation completes
if (d3D12FenceValue > d3D12Fence->GetCompletedValue())
{
d3D12Fence->SetEventOnCompletion(d3D12FenceValue, default).Assert();
}
-
- // Enqueue the command allocator pool so that it can be reused later
- commandAllocatorPool.Enqueue(commandList.DetachD3D12CommandAllocator());
}
///
@@ -348,8 +354,8 @@ namespace ComputeSharp
this.d3D12CopyCommandQueue.Dispose();
this.d3D12ComputeFence.Dispose();
this.d3D12CopyFence.Dispose();
- this.computeCommandAllocatorPool.Dispose();
- this.copyCommandAllocatorPool.Dispose();
+ this.computeCommandListPool.Dispose();
+ this.copyCommandListPool.Dispose();
this.shaderResourceViewDescriptorAllocator.Dispose();
this.pool.Dispose();
this.allocator.Dispose();