From 51dcb2491f776f11a72f4bc22cdd054abd5ec93c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 16 Jul 2021 17:29:01 +0200 Subject: [PATCH 1/4] 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; + } + } + } +} From ede3f9bbcecd1a0a8a587770ae70f8ace3c531f3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 16 Jul 2021 18:09:47 +0200 Subject: [PATCH 2/4] Migrated code to use new pooled command lists --- .../Graphics/Commands/CommandList.cs | 41 ++++++---- .../Interop/ID3D12CommandAllocatorPool.cs | 81 ------------------ src/ComputeSharp/Graphics/GraphicsDevice.cs | 82 ++++++++++--------- 3 files changed, 68 insertions(+), 136 deletions(-) delete mode 100644 src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandAllocatorPool.cs 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(); From 3e6c6e6fe592addded40c06db0cc78430422a8c2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Jul 2021 13:22:42 +0200 Subject: [PATCH 3/4] Fixed incorrect command allocator reset during execution --- src/ComputeSharp/Graphics/GraphicsDevice.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index bc7d8cb2..30932b32 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -326,17 +326,14 @@ 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(); } + + // Return the rented command list and command allocator so that they can be reused + commandListPool.Return(commandList.DetachD3D12CommandList(), commandList.DetachD3D12CommandAllocator()); } /// From 013a518f3da47b6ea322c7cfd6745dbd8085a38f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Jul 2021 15:44:33 +0200 Subject: [PATCH 4/4] Fixed missing call to SetDescriptorHeaps for reused lists --- .../Graphics/Commands/CommandList.cs | 7 +++++++ .../Commands/Interop/ID3D12CommandListPool.cs | 18 +----------------- src/ComputeSharp/Graphics/GraphicsDevice.cs | 15 +++++++++++++-- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/ComputeSharp/Graphics/Commands/CommandList.cs b/src/ComputeSharp/Graphics/Commands/CommandList.cs index 42f0ffc8..993aad79 100644 --- a/src/ComputeSharp/Graphics/Commands/CommandList.cs +++ b/src/ComputeSharp/Graphics/Commands/CommandList.cs @@ -2,6 +2,7 @@ using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using TerraFX.Interop; +using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; namespace ComputeSharp.Graphics.Commands { @@ -47,6 +48,12 @@ namespace ComputeSharp.Graphics.Commands 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) + { + device.SetDescriptorHeapForCommandList(this.d3D12GraphicsCommandList); + } } /// diff --git a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs index b3b16ebd..5c74e0df 100644 --- a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs +++ b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs @@ -4,7 +4,6 @@ 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 { @@ -18,11 +17,6 @@ namespace ComputeSharp.Graphics.Commands.Interop /// 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. /// @@ -32,11 +26,9 @@ namespace ComputeSharp.Graphics.Commands.Interop /// 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) + public ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE d3D12CommandListType) { this.d3D12CommandListType = d3D12CommandListType; - this.d3D12DescriptorHeap = d3D12DescriptorHeap; this.d3D12CommandListBundleQueue = new Queue(); } @@ -105,14 +97,6 @@ namespace ComputeSharp.Graphics.Commands.Interop 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) { diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 30932b32..4d6900ed 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -98,8 +98,8 @@ namespace ComputeSharp this.d3D12CopyFence = d3D12Device->CreateFence(); 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); + 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); @@ -292,6 +292,17 @@ namespace ComputeSharp } } + /// + /// 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); + } + /// /// Executes a given command list and waits for the operation to be completed. ///