Code refactoring

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-16 22:55:53 +01:00
1 親 a292f8f017
コミット e7bc76fd30
6ファイル変更
+142 -90

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

+25 -10
ファイルの表示
@@ -1,4 +1,5 @@
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
@@ -36,7 +37,7 @@ namespace ComputeSharp.Graphics.Commands
/// </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(ID3D12CommandAllocator* commandAllocator, ulong fenceValue)
public void Enqueue(ComPtr<ID3D12CommandAllocator> commandAllocator, ulong fenceValue)
{
lock (this.d3d12CommandAllocatorQueue)
{
@@ -47,9 +48,11 @@ namespace ComputeSharp.Graphics.Commands
/// <summary>
/// Gets a new or reused <see cref="ID3D12CommandAllocator"/> item to use to issue GPU commands.
/// </summary>
[Pure]
public ID3D12CommandAllocator* GetCommandAllocator(ID3D12Device* d3D12Device, ID3D12Fence* d3D12Fence)
/// <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
@@ -57,14 +60,18 @@ namespace ComputeSharp.Graphics.Commands
{
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();
_ = this.d3d12CommandAllocatorQueue.Dequeue();
d3D12CommandAllocatorEntry.D3D12CommandAllocator->Reset();
int result = d3D12CommandAllocator.Get()->Reset();
return d3D12CommandAllocatorEntry.D3D12CommandAllocator;
ThrowHelper.ThrowIfFailed(result);
return d3D12CommandAllocator.Move();
}
}
@@ -75,18 +82,26 @@ namespace ComputeSharp.Graphics.Commands
/// <inheritdoc cref="IDisposable.Dispose"/>
public void Dispose()
{
throw new NotImplementedException();
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 readonly unsafe struct ID3D12CommandAllocatorEntry
private unsafe struct ID3D12CommandAllocatorEntry
{
/// <summary>
/// The current <see cref="ID3D12CommandAllocator"/> reference.
/// </summary>
public readonly ID3D12CommandAllocator* D3D12CommandAllocator;
public ComPtr<ID3D12CommandAllocator> D3D12CommandAllocator;
/// <summary>
/// The fence value for the current <see cref="ID3D12CommandAllocator"/> reference.
@@ -98,7 +113,7 @@ namespace ComputeSharp.Graphics.Commands
/// </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(ID3D12CommandAllocator* d3D12CommandAllocator, ulong fenceValue)
public ID3D12CommandAllocatorEntry(ComPtr<ID3D12CommandAllocator> d3D12CommandAllocator, ulong fenceValue)
{
D3D12CommandAllocator = d3D12CommandAllocator;
FenceValue = fenceValue;
+13 -29
ファイルの表示
@@ -24,12 +24,12 @@ namespace ComputeSharp.Graphics.Commands
/// <summary>
/// The <see cref="ID3D12CommandAllocator"/> object in use by the current instance.
/// </summary>
private readonly ID3D12CommandAllocator* d3D12CommandAllocator;
private readonly ComPtr<ID3D12CommandAllocator> d3D12CommandAllocator;
/// <summary>
/// The <see cref="ID3D12GraphicsCommandList"/> object in use by the current instance.
/// </summary>
public readonly ID3D12GraphicsCommandList* D3D12GraphicsCommandList;
private readonly ComPtr<ID3D12GraphicsCommandList> d3D12GraphicsCommandList;
/// <summary>
/// Creates a new <see cref="CommandList2"/> instance with the specified parameters.
@@ -40,21 +40,13 @@ namespace ComputeSharp.Graphics.Commands
{
this.device = device;
this.d3d12CommandListType = d3d12CommandListType;
this.d3D12CommandAllocator = d3d12CommandListType switch
{
D3D12_COMMAND_LIST_TYPE_COMPUTE => device.ComputeCommandAllocatorPool.GetCommandAllocator(device.D3D12Device, device.D3D12ComputeFence),
D3D12_COMMAND_LIST_TYPE_COPY => device.ComputeCommandAllocatorPool.GetCommandAllocator(device.D3D12Device, device.D3D12CopyFence),
_ => throw null!
};
D3D12GraphicsCommandList = device.D3D12Device->CreateCommandList(d3d12CommandListType, d3D12CommandAllocator);
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)
{
ID3D12DescriptorHeap* d3D12DescriptorHeap = device.ShaderResourceViewDescriptorAllocator.D3D12DescriptorHeap;
D3D12GraphicsCommandList->SetDescriptorHeaps(1, &d3D12DescriptorHeap);
device.SetDescriptorHeapForCommandList(this.d3D12GraphicsCommandList);
}
}
@@ -68,7 +60,7 @@ namespace ComputeSharp.Graphics.Commands
/// <param name="numBytes">The total number of bytes to copy from one resource to another.</param>
public void CopyBufferRegion(ID3D12Resource* d3D12ResourceSource, int sourceOffset, ID3D12Resource* d3d12ResourceDestination, int destinationOffset, int numBytes)
{
D3D12GraphicsCommandList->CopyBufferRegion(d3d12ResourceDestination, (uint)destinationOffset, d3D12ResourceSource, (uint)sourceOffset, (uint)numBytes);
this.d3D12GraphicsCommandList.Get()->CopyBufferRegion(d3d12ResourceDestination, (uint)destinationOffset, d3D12ResourceSource, (uint)sourceOffset, (uint)numBytes);
}
/// <summary>
@@ -78,7 +70,7 @@ namespace ComputeSharp.Graphics.Commands
/// <param name="resource">The input <see cref="GraphicsResource"/> instance to bind.</param>
public void SetComputeRootDescriptorTable(int rootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle)
{
D3D12GraphicsCommandList->SetComputeRootDescriptorTable((uint)rootParameterIndex, d3D12GpuDescriptorHandle);
this.d3D12GraphicsCommandList.Get()->SetComputeRootDescriptorTable((uint)rootParameterIndex, d3D12GpuDescriptorHandle);
}
/// <summary>
@@ -87,8 +79,8 @@ namespace ComputeSharp.Graphics.Commands
/// <param name="pipelineState">The input <see cref="PipelineState"/> to setup.</param>
public void SetPipelineData(PipelineData pipelineData)
{
D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature);
D3D12GraphicsCommandList->SetPipelineState(pipelineData.D3D12PipelineState);
this.d3D12GraphicsCommandList.Get()->SetComputeRootSignature(pipelineData.D3D12RootSignature);
this.d3D12GraphicsCommandList.Get()->SetPipelineState(pipelineData.D3D12PipelineState);
}
/// <summary>
@@ -99,7 +91,7 @@ namespace ComputeSharp.Graphics.Commands
/// <param name="threadGroupCountZ">The number of thread groups to schedule for the Z axis.</param>
public void Dispatch(int threadGroupCountX, int threadGroupCountY, int threadGroupCountZ)
{
D3D12GraphicsCommandList->Dispatch((uint)threadGroupCountX, (uint)threadGroupCountY, (uint)threadGroupCountZ);
this.d3D12GraphicsCommandList.Get()->Dispatch((uint)threadGroupCountX, (uint)threadGroupCountY, (uint)threadGroupCountZ);
}
/// <summary>
@@ -107,7 +99,7 @@ namespace ComputeSharp.Graphics.Commands
/// </summary>
public void ExecuteAndWaitForCompletion()
{
D3D12GraphicsCommandList->Close();
this.d3D12GraphicsCommandList.Get()->Close();
// TODO: GraphicsDevice.ExecuteCommandList(this);
}
@@ -115,17 +107,9 @@ namespace ComputeSharp.Graphics.Commands
/// <inheritdoc cref="IDisposable.Dispose"/>
public void Dispose()
{
switch (this.d3d12CommandListType)
{
case D3D12_COMMAND_LIST_TYPE_COMPUTE:
device.ComputeCommandAllocatorPool.Enqueue(this.d3D12CommandAllocator, device.NextD3D12ComputeFenceValue);
break;
case D3D12_COMMAND_LIST_TYPE_COPY:
device.CopyCommandAllocatorPool.Enqueue(this.d3D12CommandAllocator, device.NextD3D12CopyFenceValue);
break;
};
this.device.EnqueueCommandAllocator(this.d3D12CommandAllocator.Move(), this.d3d12CommandListType);
D3D12GraphicsCommandList->Release();
this.d3D12GraphicsCommandList.Dispose();
}
}
}
+2 -8
ファイルの表示
@@ -33,18 +33,12 @@ namespace ComputeSharp.Graphics.Commands
/// <summary>
/// Gets the <see cref="ID3D12RootSignature"/> instance for the current <see cref="PipelineData"/> object.
/// </summary>
public ID3D12RootSignature* D3D12RootSignature
{
get => this.d3D12RootSignature;
}
public ID3D12RootSignature* D3D12RootSignature => this.d3D12RootSignature;
/// <summary>
/// Gets the <see cref="ID3D12PipelineState"/> instance for the current <see cref="PipelineData"/> object.
/// </summary>
public ID3D12PipelineState* D3D12PipelineState
{
get => this.d3D12PipelineState;
}
public ID3D12PipelineState* D3D12PipelineState => this.d3D12PipelineState;
/// <inheritdoc/>
protected override void Dispose(bool disposing)