diff --git a/src/ComputeSharp/Graphics/Commands/CommandList.cs b/src/ComputeSharp/Graphics/Commands/CommandList.cs
index 993ece90..993aad79 100644
--- a/src/ComputeSharp/Graphics/Commands/CommandList.cs
+++ b/src/ComputeSharp/Graphics/Commands/CommandList.cs
@@ -1,8 +1,6 @@
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;
@@ -23,16 +21,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,8 +40,14 @@ namespace ComputeSharp.Graphics.Commands
{
this.device = device;
this.d3D12CommandListType = d3D12CommandListType;
- this.d3D12CommandAllocator = device.GetCommandAllocator(d3D12CommandListType);
- this.d3D12GraphicsCommandList = device.D3D12Device->CreateCommandList(d3D12CommandListType, this.d3D12CommandAllocator);
+
+ 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));
// Set the heap descriptor if the command list is not for copy operations
if (d3D12CommandListType is not D3D12_COMMAND_LIST_TYPE_COPY)
@@ -67,9 +71,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/Commands/Interop/ID3D12CommandListPool.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs
new file mode 100644
index 00000000..5c74e0df
--- /dev/null
+++ b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs
@@ -0,0 +1,135 @@
+using System;
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+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 ID3D12CommandListPool : IDisposable
+ {
+ ///
+ /// The command list type being used by the current instance.
+ ///
+ private readonly D3D12_COMMAND_LIST_TYPE d3D12CommandListType;
+
+ ///
+ /// 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.
+ public ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE d3D12CommandListType)
+ {
+ this.d3D12CommandListType = d3D12CommandListType;
+ 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());
+
+ 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;
+ }
+ }
+ }
+}
diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs
index ebdff56e..4d6900ed 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.copyCommandListPool = new ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE_COPY);
Luid = Luid.FromLUID(dxgiDescription1->AdapterLuid);
Name = new string((char*)dxgiDescription1->Description);
@@ -266,16 +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>()
- };
+ 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 +309,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 +318,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++;
@@ -329,8 +343,8 @@ namespace ComputeSharp
d3D12Fence->SetEventOnCompletion(d3D12FenceValue, default).Assert();
}
- // Enqueue the command allocator pool so that it can be reused later
- commandAllocatorPool.Enqueue(commandList.DetachD3D12CommandAllocator());
+ // Return the rented command list and command allocator so that they can be reused
+ commandListPool.Return(commandList.DetachD3D12CommandList(), commandList.DetachD3D12CommandAllocator());
}
///
@@ -348,8 +362,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();