Optimized command allocator pool type

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-17 01:50:21 +01:00
1 親 0defb1cf25
コミット 5e2a822a24
3ファイル変更
+94 -134

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

-123
ファイルの表示
@@ -1,123 +0,0 @@
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using TerraFX.Interop;
namespace ComputeSharp.Graphics.Commands
{
/// <summary>
/// A type that acts as a pool to get new <see cref="ID3D12CommandAllocator"/> instances wheen needed.
/// </summary>
internal readonly unsafe struct CommandAllocatorPool2
{
/// <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<ID3D12CommandAllocatorEntry> d3d12CommandAllocatorQueue;
/// <summary>
/// Creates a new <see cref="CommandAllocatorPool2"/> instance with the specified values.
/// </summary>
/// <param name="d3d12CommandListType">The command list type to use.</param>
public CommandAllocatorPool2(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="commandAllocator">The input <see cref="ID3D12CommandAllocator"/> to enqueue.</param>
/// <param name="fenceValue">The fence value for the input <see cref="ID3D12CommandAllocator"/>.</param>
public void Enqueue(ComPtr<ID3D12CommandAllocator> commandAllocator, ulong fenceValue)
{
lock (this.d3d12CommandAllocatorQueue)
{
this.d3d12CommandAllocatorQueue.Enqueue(new(commandAllocator, fenceValue));
}
}
/// <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)
{
// See if there is a reusable command allocator
if (this.d3d12CommandAllocatorQueue.Count > 0)
{
ID3D12CommandAllocatorEntry d3D12CommandAllocatorEntry = this.d3d12CommandAllocatorQueue.Peek();
d3D12CommandAllocatorEntry.D3D12CommandAllocator.Swap(ref *&d3D12CommandAllocator);
// We found one that is no longer in use, reset it and return it
if (d3D12CommandAllocatorEntry.FenceValue <= d3D12Fence->GetCompletedValue())
{
_ = this.d3d12CommandAllocatorQueue.Dequeue();
int result = d3D12CommandAllocator.Get()->Reset();
ThrowHelper.ThrowIfFailed(result);
return d3D12CommandAllocator.Move();
}
}
return d3D12Device->CreateCommandAllocator(this.d3d12CommandListType);
}
}
/// <inheritdoc cref="IDisposable.Dispose"/>
public void Dispose()
{
lock (this.d3d12CommandAllocatorQueue)
{
foreach (var entry in this.d3d12CommandAllocatorQueue)
{
entry.D3D12CommandAllocator.Dispose();
}
this.d3d12CommandAllocatorQueue.Clear();
}
}
/// <summary>
/// A type that stores info on pooled <see cref="ID3D12CommandAllocator"/> references.
/// </summary>
private unsafe struct ID3D12CommandAllocatorEntry
{
/// <summary>
/// The current <see cref="ID3D12CommandAllocator"/> reference.
/// </summary>
public ComPtr<ID3D12CommandAllocator> D3D12CommandAllocator;
/// <summary>
/// The fence value for the current <see cref="ID3D12CommandAllocator"/> reference.
/// </summary>
public readonly ulong FenceValue;
/// <summary>
/// Creates a new <see cref="ID3D12CommandAllocatorEntry"/> instance with the specified parameters.
/// </summary>
/// <param name="d3D12CommandAllocator">The input <see cref="ID3D12CommandAllocator"/> reference.</param>
/// <param name="fenceValue">The fence value for the input <see cref="ID3D12CommandAllocator"/> reference.</param>
public ID3D12CommandAllocatorEntry(ComPtr<ID3D12CommandAllocator> d3D12CommandAllocator, ulong fenceValue)
{
D3D12CommandAllocator = d3D12CommandAllocator;
FenceValue = fenceValue;
}
}
}
}
+83
ファイルの表示
@@ -0,0 +1,83 @@
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using System;
using System.Collections.Generic;
using TerraFX.Interop;
namespace ComputeSharp.Graphics.Commands
{
/// <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))
{
int result = d3D12CommandAllocator.Get()->Reset();
ThrowHelper.ThrowIfFailed(result);
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();
}
}
}
}
+11 -11
ファイルの表示
@@ -40,14 +40,14 @@ namespace ComputeSharp.Graphics
private ComPtr<ID3D12Fence> d3D12CopyFence;
/// <summary>
/// The <see cref="CommandAllocatorPool2"/> instance for compute operations.
/// The <see cref="ID3D12CommandAllocatorPool"/> instance for compute operations.
/// </summary>
private readonly CommandAllocatorPool2 computeCommandAllocatorPool;
private readonly ID3D12CommandAllocatorPool computeCommandAllocatorPool;
/// <summary>
/// Gets the <see cref="CommandAllocatorPool2"/> instance for copy operations.
/// Gets the <see cref="ID3D12CommandAllocatorPool"/> instance for copy operations.
/// </summary>
private readonly CommandAllocatorPool2 copyCommandAllocatorPool;
private readonly ID3D12CommandAllocatorPool copyCommandAllocatorPool;
/// <summary>
/// The <see cref="DescriptorAllocator2"/> instance to use when allocating new buffers.
@@ -77,8 +77,8 @@ namespace ComputeSharp.Graphics
this.d3D12ComputeFence = d3d12device.Get()->CreateFence();
this.d3D12CopyFence = d3d12device.Get()->CreateFence();
this.computeCommandAllocatorPool = new CommandAllocatorPool2(D3D12_COMMAND_LIST_TYPE_COMPUTE);
this.copyCommandAllocatorPool = new CommandAllocatorPool2(D3D12_COMMAND_LIST_TYPE_COPY);
this.computeCommandAllocatorPool = new ID3D12CommandAllocatorPool(D3D12_COMMAND_LIST_TYPE_COMPUTE);
this.copyCommandAllocatorPool = new ID3D12CommandAllocatorPool(D3D12_COMMAND_LIST_TYPE_COPY);
this.shaderResourceViewDescriptorAllocator = new DescriptorAllocator2(d3d12device);
Name = new string((char*)dxgiDescription1->Description);
@@ -123,7 +123,7 @@ namespace ComputeSharp.Graphics
this.shaderResourceViewDescriptorAllocator.Allocate(out d3d12CpuDescriptorHandle, out d3d12GpuDescriptorHandle);
}
/// <inheritdoc cref="CommandAllocatorPool2.GetCommandAllocator"/>
/// <inheritdoc cref="ID3D12CommandAllocatorPool.GetCommandAllocator"/>
/// <param name="d3d12CommandListType">The type of command allocator to rent.</param>
internal ComPtr<ID3D12CommandAllocator> GetCommandAllocator(D3D12_COMMAND_LIST_TYPE d3d12CommandListType)
{
@@ -152,7 +152,7 @@ namespace ComputeSharp.Graphics
/// <param name="commandList">The input <see cref="CommandList2"/> to execute.</param>
internal void ExecuteCommandList(ref CommandList2 commandList)
{
ref readonly CommandAllocatorPool2 commandAllocatorPool = ref Unsafe.NullRef<CommandAllocatorPool2>();
ref readonly ID3D12CommandAllocatorPool commandAllocatorPool = ref Unsafe.NullRef<ID3D12CommandAllocatorPool>();
ID3D12CommandQueue* d3D12CommandQueue;
ID3D12Fence* d3D12Fence;
ulong d3D12FenceValue;
@@ -175,9 +175,6 @@ namespace ComputeSharp.Graphics
default: throw null!;
}
// Enqueue the command allocator pool so that it can be reused later
commandAllocatorPool.Enqueue(commandList.DetachD3D12CommandAllocator(), d3D12FenceValue);
// Execute the command list and signal to the target fence
d3D12CommandQueue->ExecuteCommandLists(1, commandList.GetD3D12CommandListAddressOf());
@@ -192,6 +189,9 @@ namespace ComputeSharp.Graphics
ThrowHelper.ThrowIfFailed(result);
}
// Enqueue the command allocator pool so that it can be reused later
commandAllocatorPool.Enqueue(commandList.DetachD3D12CommandAllocator());
}
/// <inheritdoc/>