Migrated code to use new pooled command lists

このコミットが含まれているのは:
Sergio Pedri committed 2021-07-17 14:02:41 +02:00
1 親 51dcb2491f
コミット ede3f9bbce
3ファイル変更
+68 -136

検索条件に一致するファイルはありません

+24 -17
ファイルの表示
@@ -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
/// </summary>
private readonly D3D12_COMMAND_LIST_TYPE d3D12CommandListType;
/// <summary>
/// The <see cref="ID3D12CommandAllocator"/> object in use by the current instance.
/// </summary>
private ComPtr<ID3D12CommandAllocator> d3D12CommandAllocator;
/// <summary>
/// The <see cref="ID3D12GraphicsCommandList"/> object in use by the current instance.
/// </summary>
private ComPtr<ID3D12GraphicsCommandList> d3D12GraphicsCommandList;
/// <summary>
/// The <see cref="ID3D12CommandAllocator"/> object in use by the current instance.
/// </summary>
private ComPtr<ID3D12CommandAllocator> d3D12CommandAllocator;
/// <summary>
/// Creates a new <see cref="CommandList"/> instance with the specified parameters.
/// </summary>
@@ -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));
}
/// <summary>
@@ -67,9 +64,19 @@ namespace ComputeSharp.Graphics.Commands
/// </summary>
/// <returns>The <see cref="ID3D12CommandAllocator"/> object in use, with ownership.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ComPtr<ID3D12CommandAllocator> DetachD3D12CommandAllocator()
public ID3D12CommandAllocator* DetachD3D12CommandAllocator()
{
return this.d3D12CommandAllocator.Move();
return this.d3D12CommandAllocator.Detach();
}
/// <summary>
/// Detaches the <see cref="ID3D12GraphicsCommandList"/> object in use by the current instance.
/// </summary>
/// <returns>The <see cref="ID3D12GraphicsCommandList"/> object in use, with ownership.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ID3D12GraphicsCommandList* DetachD3D12CommandList()
{
return this.d3D12GraphicsCommandList.Detach();
}
/// <summary>
-81
ファイルの表示
@@ -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
{
/// <summary>
/// A type that acts as a pool to get new <see cref="ID3D12CommandAllocator"/> instances wheen needed.
/// </summary>
internal readonly unsafe struct ID3D12CommandAllocatorPool
{
/// <summary>
/// The command list type being used by the current instance.
/// </summary>
private readonly D3D12_COMMAND_LIST_TYPE d3D12CommandListType;
/// <summary>
/// The queue of currently pooled <see cref="ID3D12CommandAllocator"/> instances.
/// </summary>
private readonly Queue<ComPtr<ID3D12CommandAllocator>> d3D12CommandAllocatorQueue;
/// <summary>
/// Creates a new <see cref="ID3D12CommandAllocatorPool"/> instance with the specified values.
/// </summary>
/// <param name="d3D12CommandListType">The command list type to use.</param>
public ID3D12CommandAllocatorPool(D3D12_COMMAND_LIST_TYPE d3D12CommandListType)
{
this.d3D12CommandListType = d3D12CommandListType;
this.d3D12CommandAllocatorQueue = new();
}
/// <summary>
/// Adds a new <see cref="ID3D12CommandAllocator"/> item to the internal pool queue.
/// </summary>
/// <param name="d3D12CommandAllocator">The input <see cref="ID3D12CommandAllocator"/> to enqueue.</param>
public void Enqueue(ComPtr<ID3D12CommandAllocator> d3D12CommandAllocator)
{
lock (this.d3D12CommandAllocatorQueue)
{
this.d3D12CommandAllocatorQueue.Enqueue(d3D12CommandAllocator);
}
}
/// <summary>
/// Gets a new or reused <see cref="ID3D12CommandAllocator"/> item to use to issue GPU commands.
/// </summary>
/// <returns>The rented <see cref="ID3D12CommandAllocator"/> object to use.</returns>
public ComPtr<ID3D12CommandAllocator> GetCommandAllocator(ID3D12Device* d3D12Device, ID3D12Fence* d3D12Fence)
{
using ComPtr<ID3D12CommandAllocator> d3D12CommandAllocator = default;
lock (this.d3D12CommandAllocatorQueue)
{
if (this.d3D12CommandAllocatorQueue.TryDequeue(out *&d3D12CommandAllocator))
{
d3D12CommandAllocator.Get()->Reset().Assert();
return d3D12CommandAllocator.Move();
}
return d3D12Device->CreateCommandAllocator(this.d3D12CommandListType);
}
}
/// <inheritdoc cref="IDisposable.Dispose"/>
public void Dispose()
{
lock (this.d3D12CommandAllocatorQueue)
{
foreach (ComPtr<ID3D12CommandAllocator> d3D12CommandAllocator in this.d3D12CommandAllocatorQueue)
{
d3D12CommandAllocator.Dispose();
}
this.d3D12CommandAllocatorQueue.Clear();
}
}
}
}
+44 -38
ファイルの表示
@@ -47,21 +47,21 @@ namespace ComputeSharp
/// </summary>
private ComPtr<ID3D12Fence> d3D12CopyFence;
/// <summary>
/// The <see cref="ID3D12CommandAllocatorPool"/> instance for compute operations.
/// </summary>
private readonly ID3D12CommandAllocatorPool computeCommandAllocatorPool;
/// <summary>
/// Gets the <see cref="ID3D12CommandAllocatorPool"/> instance for copy operations.
/// </summary>
private readonly ID3D12CommandAllocatorPool copyCommandAllocatorPool;
/// <summary>
/// The <see cref="ID3D12DescriptorHandleAllocator"/> instance to use when allocating new buffers.
/// </summary>
private ID3D12DescriptorHandleAllocator shaderResourceViewDescriptorAllocator;
/// <summary>
/// The <see cref="ID3D12CommandListPool"/> instance for compute operations.
/// </summary>
private readonly ID3D12CommandListPool computeCommandListPool;
/// <summary>
/// Gets the <see cref="ID3D12CommandListPool"/> instance for copy operations.
/// </summary>
private readonly ID3D12CommandListPool copyCommandListPool;
/// <summary>
/// The next fence value for compute operations using <see cref="d3D12ComputeCommandQueue"/>.
/// </summary>
@@ -91,14 +91,15 @@ namespace ComputeSharp
internal GraphicsDevice(ID3D12Device* d3D12Device, IDXGIAdapter* dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1)
{
this.d3D12Device = new ComPtr<ID3D12Device>(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);
}
/// <inheritdoc cref="ID3D12CommandAllocatorPool.GetCommandAllocator"/>
/// <inheritdoc cref="ID3D12CommandListPool.Rent"/>
/// <param name="d3D12CommandListType">The type of command allocator to rent.</param>
internal ComPtr<ID3D12CommandAllocator> GetCommandAllocator(D3D12_COMMAND_LIST_TYPE d3D12CommandListType)
/// <param name="d3D12CommandList">The resulting <see cref="ID3D12GraphicsCommandList"/> value.</param>
/// <param name="d3D12CommandAllocator">The resulting <see cref="ID3D12CommandAllocator"/> value.</param>
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<ComPtr<ID3D12CommandAllocator>>()
};
}
/// <summary>
/// Sets the descriptor heap for a given <see cref="ID3D12GraphicsCommandList"/> instance.
/// </summary>
/// <param name="d3D12GraphicsCommandList">The input <see cref="ID3D12GraphicsCommandList"/> instance to use.</param>
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<ComPtr<ID3D12CommandAllocator>>();
d3D12CommandList = null;
d3D12CommandAllocator = null;
break;
}
}
/// <summary>
@@ -295,7 +298,7 @@ namespace ComputeSharp
/// <param name="commandList">The input <see cref="CommandList"/> to execute.</param>
internal void ExecuteCommandList(ref CommandList commandList)
{
ref readonly ID3D12CommandAllocatorPool commandAllocatorPool = ref Unsafe.NullRef<ID3D12CommandAllocatorPool>();
ref readonly ID3D12CommandListPool commandListPool = ref Unsafe.NullRef<ID3D12CommandListPool>();
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());
}
/// <inheritdoc/>
@@ -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();