Ported command list execution code

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-17 00:51:42 +01:00
1 親 fa63f3d5f0
コミット 0defb1cf25
3ファイル変更
+91 -34

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

+2 -6
ファイルの表示
@@ -69,15 +69,11 @@ namespace ComputeSharp
/// </remarks>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe ComPtr<U> Upcast<T, U>(this in ComPtr<T> ptr)
public static unsafe ref readonly ComPtr<U> Upcast<T, U>(this in ComPtr<T> ptr)
where T : unmanaged
where U : unmanaged
{
ComPtr<U> copy = default;
*(T**)Unsafe.AsPointer(ref copy) = ptr.Get();
return copy;
return ref Unsafe.As<ComPtr<T>, ComPtr<U>>(ref Unsafe.AsRef(in ptr));
}
}
}
+39 -12
ファイルの表示
@@ -1,4 +1,6 @@
using System;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using ComputeSharp.Graphics.Buffers.Abstract;
using ComputeSharp.Graphics.Extensions;
using TerraFX.Interop;
@@ -9,7 +11,7 @@ namespace ComputeSharp.Graphics.Commands
/// <summary>
/// A command list to set and execute operaations on the GPU.
/// </summary>
internal readonly unsafe ref struct CommandList2
internal unsafe ref struct CommandList2
{
/// <summary>
/// The <see cref="GraphicsDevice2"/> instance associated with the current command list.
@@ -24,12 +26,12 @@ namespace ComputeSharp.Graphics.Commands
/// <summary>
/// The <see cref="ID3D12CommandAllocator"/> object in use by the current instance.
/// </summary>
private readonly ComPtr<ID3D12CommandAllocator> d3D12CommandAllocator;
private ComPtr<ID3D12CommandAllocator> d3D12CommandAllocator;
/// <summary>
/// The <see cref="ID3D12GraphicsCommandList"/> object in use by the current instance.
/// </summary>
private readonly ComPtr<ID3D12GraphicsCommandList> d3D12GraphicsCommandList;
private ComPtr<ID3D12GraphicsCommandList> d3D12GraphicsCommandList;
/// <summary>
/// Creates a new <see cref="CommandList2"/> instance with the specified parameters.
@@ -50,6 +52,32 @@ namespace ComputeSharp.Graphics.Commands
}
}
/// <summary>
/// Gets the command list type being used by the current instance.
/// </summary>
public readonly D3D12_COMMAND_LIST_TYPE D3d12CommandListType => this.d3d12CommandListType;
/// <summary>
/// Detaches the <see cref="ID3D12CommandAllocator"/> object in use by the current instance.
/// </summary>
/// <returns>The <see cref="ID3D12CommandAllocator"/> object in use, with ownership.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ComPtr<ID3D12CommandAllocator> DetachD3D12CommandAllocator()
{
return this.d3D12CommandAllocator.Move();
}
/// <summary>
/// Gets a pointer to the <see cref="ID3D12CommandList"/> object in use by the current instance.
/// </summary>
/// <returns>A double pointer to the current <see cref="ID3D12CommandList"/> object to execute.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ID3D12CommandList** GetD3D12CommandListAddressOf()
{
return this.d3D12GraphicsCommandList.Upcast<ID3D12GraphicsCommandList, ID3D12CommandList>().GetAddressOf();
}
/// <summary>
/// Copies a memory region from one resource to another.
/// </summary>
@@ -58,7 +86,7 @@ namespace ComputeSharp.Graphics.Commands
/// <param name="d3d12ResourceDestination">The destination <see cref="ID3D12Resource"/> to write to.</param>
/// <param name="destinationOffset">The starting offset to write the destination resource from.</param>
/// <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)
public readonly void CopyBufferRegion(ID3D12Resource* d3D12ResourceSource, int sourceOffset, ID3D12Resource* d3d12ResourceDestination, int destinationOffset, int numBytes)
{
this.d3D12GraphicsCommandList.Get()->CopyBufferRegion(d3d12ResourceDestination, (uint)destinationOffset, d3D12ResourceSource, (uint)sourceOffset, (uint)numBytes);
}
@@ -68,16 +96,16 @@ namespace ComputeSharp.Graphics.Commands
/// </summary>
/// <param name="rootParameterIndex">The root parameter index to bind to the input resource.</param>
/// <param name="resource">The input <see cref="GraphicsResource"/> instance to bind.</param>
public void SetComputeRootDescriptorTable(int rootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle)
public readonly void SetComputeRootDescriptorTable(int rootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle)
{
this.d3D12GraphicsCommandList.Get()->SetComputeRootDescriptorTable((uint)rootParameterIndex, d3D12GpuDescriptorHandle);
}
/// <summary>
/// Sets a given <see cref="PipelineState"/> object ready to be executed.
/// Sets a given <see cref="PipelineData"/> object ready to be executed.
/// </summary>
/// <param name="pipelineState">The input <see cref="PipelineState"/> to setup.</param>
public void SetPipelineData(PipelineData pipelineData)
/// <param name="pipelineState">The input <see cref="PipelineData"/> to setup.</param>
public readonly void SetPipelineData(PipelineData pipelineData)
{
this.d3D12GraphicsCommandList.Get()->SetComputeRootSignature(pipelineData.D3D12RootSignature);
this.d3D12GraphicsCommandList.Get()->SetPipelineState(pipelineData.D3D12PipelineState);
@@ -89,7 +117,7 @@ namespace ComputeSharp.Graphics.Commands
/// <param name="threadGroupCountX">The number of thread groups to schedule for the X axis.</param>
/// <param name="threadGroupCountY">The number of thread groups to schedule for the Y axis.</param>
/// <param name="threadGroupCountZ">The number of thread groups to schedule for the Z axis.</param>
public void Dispatch(int threadGroupCountX, int threadGroupCountY, int threadGroupCountZ)
public readonly void Dispatch(int threadGroupCountX, int threadGroupCountY, int threadGroupCountZ)
{
this.d3D12GraphicsCommandList.Get()->Dispatch((uint)threadGroupCountX, (uint)threadGroupCountY, (uint)threadGroupCountZ);
}
@@ -101,14 +129,13 @@ namespace ComputeSharp.Graphics.Commands
{
this.d3D12GraphicsCommandList.Get()->Close();
// TODO: GraphicsDevice.ExecuteCommandList(this);
this.device.ExecuteCommandList(ref this);
}
/// <inheritdoc cref="IDisposable.Dispose"/>
public void Dispose()
{
this.device.EnqueueCommandAllocator(this.d3D12CommandAllocator.Move(), this.d3d12CommandListType);
this.d3D12CommandAllocator.Dispose();
this.d3D12GraphicsCommandList.Dispose();
}
}
+50 -16
ファイルの表示
@@ -1,6 +1,8 @@
using ComputeSharp.Core.Interop;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using System.Runtime.CompilerServices;
using TerraFX.Interop;
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
using static TerraFX.Interop.D3D12_FEATURE;
@@ -121,22 +123,6 @@ namespace ComputeSharp.Graphics
this.shaderResourceViewDescriptorAllocator.Allocate(out d3d12CpuDescriptorHandle, out d3d12GpuDescriptorHandle);
}
/// <inheritdoc cref="CommandAllocatorPool2.Enqueue"/>
/// <param name="d3D12CommandAllocator">The <see cref="ID3D12CommandAllocator"/> instance to return to the pool.</param>
/// <param name="d3d12CommandListType">The type of command allocator to return to the pool.</param>
internal void EnqueueCommandAllocator(ComPtr<ID3D12CommandAllocator> d3D12CommandAllocator, D3D12_COMMAND_LIST_TYPE d3d12CommandListType)
{
switch (d3d12CommandListType)
{
case D3D12_COMMAND_LIST_TYPE_COMPUTE:
this.computeCommandAllocatorPool.Enqueue(d3D12CommandAllocator, this.nextD3D12ComputeFenceValue);
break;
case D3D12_COMMAND_LIST_TYPE_COPY:
this.copyCommandAllocatorPool.Enqueue(d3D12CommandAllocator, this.nextD3D12CopyFenceValue);
break;
};
}
/// <inheritdoc cref="CommandAllocatorPool2.GetCommandAllocator"/>
/// <param name="d3d12CommandListType">The type of command allocator to rent.</param>
internal ComPtr<ID3D12CommandAllocator> GetCommandAllocator(D3D12_COMMAND_LIST_TYPE d3d12CommandListType)
@@ -160,6 +146,54 @@ namespace ComputeSharp.Graphics
d3D12GraphicsCommandList->SetDescriptorHeaps(1, &d3D12DescriptorHeap);
}
/// <summary>
/// Executes a given command list and waits for the operation to be completed.
/// </summary>
/// <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>();
ID3D12CommandQueue* d3D12CommandQueue;
ID3D12Fence* d3D12Fence;
ulong d3D12FenceValue;
// Get the target command queue, fence and pool for the list type
switch (commandList.D3d12CommandListType)
{
case D3D12_COMMAND_LIST_TYPE_COMPUTE:
commandAllocatorPool = ref this.computeCommandAllocatorPool;
d3D12CommandQueue = this.d3D12ComputeCommandQueue;
d3D12Fence = this.d3D12ComputeFence;
d3D12FenceValue = this.nextD3D12ComputeFenceValue++;
break;
case D3D12_COMMAND_LIST_TYPE_COPY:
commandAllocatorPool = ref this.copyCommandAllocatorPool;
d3D12CommandQueue = this.d3D12CopyCommandQueue;
d3D12Fence = this.d3D12CopyFence;
d3D12FenceValue = this.nextD3D12CopyFenceValue++;
break;
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());
int result = d3D12CommandQueue->Signal(d3D12Fence, d3D12FenceValue);
ThrowHelper.ThrowIfFailed(result);
// If the fence value hasn't been reached, wait until the operation completes
if (d3D12FenceValue > d3D12Fence->GetCompletedValue())
{
result = d3D12Fence->SetEventOnCompletion(d3D12FenceValue, default);
ThrowHelper.ThrowIfFailed(result);
}
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{