Add IAsyncDisposable to ComputeContext

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-30 17:03:28 +01:00
1 親 fdec5df11d
コミット c03006aa67
11ファイル変更
+328 -60

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

+1
ファイルの表示
@@ -27,5 +27,6 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\NativeMemory.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\OSPlatformAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\SupportedOSVersionAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading.Tasks\TaskCompletionSource.cs" />
</ItemGroup>
</Project>
+15
ファイルの表示
@@ -0,0 +1,15 @@
namespace System.Threading.Tasks;
/// <summary>
/// A polyfill type that mirrors the non-generic <see cref="TaskCompletionSource{T}"/> type on .NET 6.
/// </summary>
internal sealed class TaskCompletionSource : TaskCompletionSource<object?>
{
/// <summary>
/// Transitions the underlying <see cref="Task"/> into the <see cref="TaskStatus.RanToCompletion"/> state.
/// </summary>
public void SetResult()
{
SetResult(null);
}
}
+1
ファイルの表示
@@ -22,6 +22,7 @@
<!-- .NET Standard 2.0 uses a local fork -->
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="6.0.0" />
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
</ItemGroup>
+14 -2
ファイルの表示
@@ -1,5 +1,6 @@
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using static TerraFX.Interop.DirectX.D3D12_COMMAND_LIST_TYPE;
@@ -9,7 +10,7 @@ namespace ComputeSharp.Graphics.Commands;
/// <summary>
/// A command list to set and execute operations on the GPU.
/// </summary>
internal unsafe ref struct CommandList
internal unsafe struct CommandList : IDisposable
{
/// <summary>
/// The <see cref="GraphicsDevice"/> instance associated with the current command list.
@@ -138,7 +139,18 @@ internal unsafe ref struct CommandList
this.device.ExecuteCommandList(ref this);
}
/// <inheritdoc cref="IDisposable.Dispose"/>
/// <summary>
/// Executes the commands in the current commands list, returns a <see cref="ValueTask"/>.
/// </summary>
/// <returns>The <see cref="ValueTask"/> to await for the operations to complete.</returns>
public ValueTask ExecuteAndWaitForCompletionAsync()
{
this.d3D12GraphicsCommandList.Get()->Close();
return this.device.ExecuteCommandListAsync(ref this);
}
/// <inheritdoc/>
public void Dispose()
{
this.d3D12CommandAllocator.Dispose();
+235
ファイルの表示
@@ -0,0 +1,235 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Graphics.Commands.Interop;
using ComputeSharp.Interop;
using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using static TerraFX.Interop.DirectX.D3D12_COMMAND_LIST_TYPE;
#if !NET6_0_OR_GREATER
using UnmanagedCallersOnlyAttribute = ComputeSharp.NetStandard.System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute;
#endif
namespace ComputeSharp;
/// <inheritdoc/>
unsafe partial class GraphicsDevice : NativeObject
{
#if !NET6_0_OR_GREATER
/// <summary>
/// A delegate for a callback to pass to <see cref="Windows.RegisterWaitForSingleObject(HANDLE*, HANDLE, void*, void*, uint, uint)"/>.
/// </summary>
/// <param name="pContext">The input context.</param>
/// <param name="timedOut">Whether the wait has timed out.</param>
private delegate void WaitForSingleObjectCallbackDelegate(void* pContext, byte timedOut);
/// <summary>
/// A cached <see cref="WaitForSingleObjectCallbackDelegate"/> instance wrapping <see cref="WaitForSingleObjectCallback(void*, byte)"/>.
/// </summary>
private static readonly WaitForSingleObjectCallbackDelegate WaitForSingleObjectCallbackWrapper = WaitForSingleObjectCallback;
#endif
/// <summary>
/// Executes a given command list and waits for the operation to be completed.
/// </summary>
/// <param name="commandList">The input <see cref="CommandList"/> to execute.</param>
internal void ExecuteCommandList(ref CommandList commandList)
{
ref readonly ID3D12CommandListPool commandListPool = ref Unsafe.NullRef<ID3D12CommandListPool>();
ID3D12CommandQueue* d3D12CommandQueue;
ID3D12Fence* d3D12Fence;
ref ulong d3D12FenceValue = ref Unsafe.NullRef<ulong>();
// Get the target command queue, fence and pool for the list type
switch (commandList.D3D12CommandListType)
{
case D3D12_COMMAND_LIST_TYPE_COMPUTE:
commandListPool = ref this.computeCommandListPool;
d3D12CommandQueue = this.d3D12ComputeCommandQueue;
d3D12Fence = this.d3D12ComputeFence;
d3D12FenceValue = ref this.nextD3D12ComputeFenceValue;
break;
case D3D12_COMMAND_LIST_TYPE_COPY:
commandListPool = ref this.copyCommandListPool;
d3D12CommandQueue = this.d3D12CopyCommandQueue;
d3D12Fence = this.d3D12CopyFence;
d3D12FenceValue = ref this.nextD3D12CopyFenceValue;
break;
default: ThrowHelper.ThrowArgumentException(); return;
}
// Execute the command list
d3D12CommandQueue->ExecuteCommandLists(1, commandList.GetD3D12CommandListAddressOf());
#if NET6_0_OR_GREATER
ulong updatedFenceValue = Interlocked.Increment(ref d3D12FenceValue);
#else
ulong updatedFenceValue = (ulong)Interlocked.Increment(ref Unsafe.As<ulong, long>(ref d3D12FenceValue));
#endif
// Signal to the target fence with the updated value. Note that incrementing the
// target fence value to signal must be done after executing the command list.
d3D12CommandQueue->Signal(d3D12Fence, updatedFenceValue).Assert();
// If the fence value hasn't been reached, wait until the operation completes
if (updatedFenceValue > d3D12Fence->GetCompletedValue())
{
d3D12Fence->SetEventOnCompletion(updatedFenceValue, default).Assert();
}
// Return the rented command list and command allocator so that they can be reused
commandListPool.Return(commandList.DetachD3D12CommandList(), commandList.DetachD3D12CommandAllocator());
}
/// <summary>
/// Executes a given command list and returns an awaitable for the operation to be completed.
/// </summary>
/// <param name="commandList">The input <see cref="ValueTask"/> to execute.</param>
/// <returns>The <see cref="ValueTask"/> object representing the operation to wait for.</returns>
/// <remarks>This method is only supported for compute operations.</remarks>
internal ValueTask ExecuteCommandListAsync(ref CommandList commandList)
{
// Execute the command list
this.d3D12ComputeCommandQueue.Get()->ExecuteCommandLists(1, commandList.GetD3D12CommandListAddressOf());
#if NET6_0_OR_GREATER
ulong updatedFenceValue = Interlocked.Increment(ref this.nextD3D12ComputeFenceValue);
#else
ulong updatedFenceValue = (ulong)Interlocked.Increment(ref Unsafe.As<ulong, long>(ref this.nextD3D12ComputeFenceValue));
#endif
// Signal to the target fence with the updated value. Note that incrementing the
// target fence value to signal must be done after executing the command list.
this.d3D12ComputeCommandQueue.Get()->Signal(this.d3D12ComputeFence.Get(), updatedFenceValue).Assert();
// If the fence value has been reached, complete synchronously
if (updatedFenceValue <= this.d3D12ComputeFence.Get()->GetCompletedValue())
{
// Return the rented command list and command allocator so that they can be reused
this.computeCommandListPool.Return(commandList.DetachD3D12CommandList(), commandList.DetachD3D12CommandAllocator());
#if NET6_0_OR_GREATER
return ValueTask.CompletedTask;
#else
return new(Task.CompletedTask);
#endif
}
// Setup the awaitable task
Task computeTask = WaitForFenceAsync(
updatedFenceValue,
this,
commandList.DetachD3D12CommandList(),
commandList.DetachD3D12CommandAllocator());
return new(computeTask);
}
/// <summary>
/// A context to support <see cref="WaitForSingleObjectCallback"/>.
/// </summary>
private struct CallbackContext
{
/// <summary>
/// The <see cref="GCHandle"/> wrapping the <see cref="TaskCompletionSource"/> object to signal completion.
/// </summary>
public GCHandle TaskCompletionSourceHandle;
/// <summary>
/// The <see cref="GCHandle"/> wrapping the <see cref="GraphicsDevice"/> instance in use.
/// </summary>
public GCHandle GraphicsDeviceHandle;
/// <summary>
/// The <see cref="ID3D12GraphicsCommandList"/> used to queue the operations.
/// </summary>
public ID3D12GraphicsCommandList* D3D12GraphicsCommandList;
/// <summary>
/// The <see cref="ID3D12GraphicsCommandList"/> object backing <see name="D3D12GraphicsCommandList"/>.
/// </summary>
public ID3D12CommandAllocator* D3D12CommandAllocator;
/// <summary>
/// The event handle set when the target fence value is reached.
/// </summary>
public HANDLE EventHandle;
}
/// <summary>
/// Asynchronously waits for a target fence value to be completed.
/// </summary>
/// <param name="d3D12FenceValue">The target fence value to wait for.</param>
/// <param name="device">The <see cref="GraphicsDevice"/> instance executing the operations.</param>
/// <param name="d3D12GraphicsCommandList">The <see cref="ID3D12GraphicsCommandList"/> used to queue the operations.</param>
/// <param name="d3D12CommandAllocator">The <see cref="ID3D12GraphicsCommandList"/> object backing <paramref name="d3D12GraphicsCommandList"/>.</param>
/// <returns>The <see cref="ValueTask"/> object representing the operation to wait for.</returns>
/// <remarks>This method is only supported for compute operations.</remarks>
private static Task WaitForFenceAsync(
ulong d3D12FenceValue,
GraphicsDevice device,
ID3D12GraphicsCommandList* d3D12GraphicsCommandList,
ID3D12CommandAllocator* d3D12CommandAllocator)
{
HANDLE eventHandle = Windows.CreateEventW(null, Windows.FALSE, Windows.FALSE, null);
device.d3D12ComputeFence.Get()->SetEventOnCompletion(d3D12FenceValue, eventHandle).Assert();
TaskCompletionSource taskCompletionSource = new();
CallbackContext* callbackContext = (CallbackContext*)NativeMemory.Alloc((nuint)sizeof(CallbackContext));
callbackContext->TaskCompletionSourceHandle = GCHandle.Alloc(taskCompletionSource);
callbackContext->GraphicsDeviceHandle = GCHandle.Alloc(device);
callbackContext->D3D12GraphicsCommandList = d3D12GraphicsCommandList;
callbackContext->D3D12CommandAllocator = d3D12CommandAllocator;
callbackContext->EventHandle = eventHandle;
HANDLE waitHandle;
_ = Windows.RegisterWaitForSingleObject(
phNewWaitObject: &waitHandle,
hObject: eventHandle,
#if NET6_0_OR_GREATER
Callback: &WaitForSingleObjectCallback,
#else
Callback: (void*)Marshal.GetFunctionPointerForDelegate(WaitForSingleObjectCallbackWrapper),
#endif
Context: callbackContext,
dwMilliseconds: Windows.INFINITE,
dwFlags: 0);
return taskCompletionSource.Task;
}
/// <summary>
/// The callback to signal the completion of a compute pipeline.
/// </summary>
/// <param name="pContext">The input context.</param>
/// <param name="timedOut">Whether the wait has timed out.</param>
[UnmanagedCallersOnly]
private static void WaitForSingleObjectCallback(void* pContext, byte timedOut)
{
CallbackContext* callbackContext = (CallbackContext*)pContext;
TaskCompletionSource taskCompletionSource = Unsafe.As<TaskCompletionSource>(callbackContext->TaskCompletionSourceHandle.Target)!;
GraphicsDevice device = Unsafe.As<GraphicsDevice>(callbackContext->GraphicsDeviceHandle.Target)!;
ID3D12GraphicsCommandList* d3D12GraphicsCommandList = callbackContext->D3D12GraphicsCommandList;
ID3D12CommandAllocator* d3D12CommandAllocator = callbackContext->D3D12CommandAllocator;
HANDLE eventHandle = callbackContext->EventHandle;
callbackContext->TaskCompletionSourceHandle.Free();
callbackContext->GraphicsDeviceHandle.Free();
device.computeCommandListPool.Return(d3D12GraphicsCommandList, d3D12CommandAllocator);
_ = Windows.CloseHandle(eventHandle);
NativeMemory.Free(callbackContext);
taskCompletionSource.SetResult();
}
}
+3 -56
ファイルの表示
@@ -1,9 +1,9 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Graphics.Commands.Interop;
#if NET6_0_OR_GREATER
using ComputeSharp.Core.Extensions;
#endif
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Interop;
@@ -14,7 +14,6 @@ using static TerraFX.Interop.DirectX.D3D12_COMMAND_LIST_TYPE;
using static TerraFX.Interop.DirectX.D3D12_FEATURE;
using static TerraFX.Interop.DirectX.D3D12_FORMAT_SUPPORT1;
using static TerraFX.Interop.DirectX.DXGI_ADAPTER_FLAG;
namespace ComputeSharp;
/// <summary>
@@ -347,58 +346,6 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
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="CommandList"/> to execute.</param>
internal void ExecuteCommandList(ref CommandList commandList)
{
ref readonly ID3D12CommandListPool commandListPool = ref Unsafe.NullRef<ID3D12CommandListPool>();
ID3D12CommandQueue* d3D12CommandQueue;
ID3D12Fence* d3D12Fence;
ref ulong d3D12FenceValue = ref Unsafe.NullRef<ulong>();
// Get the target command queue, fence and pool for the list type
switch (commandList.D3D12CommandListType)
{
case D3D12_COMMAND_LIST_TYPE_COMPUTE:
commandListPool = ref this.computeCommandListPool;
d3D12CommandQueue = this.d3D12ComputeCommandQueue;
d3D12Fence = this.d3D12ComputeFence;
d3D12FenceValue = ref this.nextD3D12ComputeFenceValue;
break;
case D3D12_COMMAND_LIST_TYPE_COPY:
commandListPool = ref this.copyCommandListPool;
d3D12CommandQueue = this.d3D12CopyCommandQueue;
d3D12Fence = this.d3D12CopyFence;
d3D12FenceValue = ref this.nextD3D12CopyFenceValue;
break;
default: ThrowHelper.ThrowArgumentException(); return;
}
// Execute the command list
d3D12CommandQueue->ExecuteCommandLists(1, commandList.GetD3D12CommandListAddressOf());
#if NET6_0_OR_GREATER
ulong updatedFenceValue = Interlocked.Increment(ref d3D12FenceValue);
#else
ulong updatedFenceValue = (ulong)Interlocked.Increment(ref Unsafe.As<ulong, long>(ref d3D12FenceValue));
#endif
// Signal to the target fence with the updated value. Note that incrementing the
// target fence value to signal must be done after executing the command list.
d3D12CommandQueue->Signal(d3D12Fence, updatedFenceValue).Assert();
// If the fence value hasn't been reached, wait until the operation completes
if (updatedFenceValue > d3D12Fence->GetCompletedValue())
{
d3D12Fence->SetEventOnCompletion(updatedFenceValue, default).Assert();
}
// Return the rented command list and command allocator so that they can be reused
commandListPool.Return(commandList.DetachD3D12CommandList(), commandList.DetachD3D12CommandAllocator());
}
/// <inheritdoc/>
protected override void OnDispose()
{
+21 -2
ファイルの表示
@@ -1,5 +1,6 @@
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using ComputeSharp.__Internals;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Graphics.Extensions;
@@ -15,7 +16,7 @@ namespace ComputeSharp;
/// <summary>
/// A context to batch compute operations in a single invocation, minimizing GPU overhead.
/// </summary>
public ref struct ComputeContext
public struct ComputeContext : IDisposable, IAsyncDisposable
{
/// <summary>
/// The <see cref="GraphicsDevice"/> instance owning the current context.
@@ -231,7 +232,7 @@ public ref struct ComputeContext
commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, 1);
}
/// <inheritdoc cref="IDisposable.Dispose"/>
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
@@ -245,6 +246,24 @@ public ref struct ComputeContext
this.commandList.ExecuteAndWaitForCompletion();
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ValueTask DisposeAsync()
{
ThrowInvalidOperationExceptionIfDeviceIsNull();
if (!this.commandList.IsAllocated)
{
#if NET6_0_OR_GREATER
return ValueTask.CompletedTask;
#else
return new(Task.CompletedTask);
#endif
}
return this.commandList.ExecuteAndWaitForCompletionAsync();
}
/// <summary>
/// Gets the current <see cref="CommandList"/> instance.
/// </summary>
+2
ファイルの表示
@@ -233,9 +233,11 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\objidlbase\IStream.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\objidlbase\STATSTG.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\objidlbase\STGTY.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\handleapi\Windows.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\synchapi\Windows.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\Unknwnbase\IID.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\Unknwnbase\IUnknown.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\WinBase\Windows.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\wincodec\CLSID.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\wincodec\GUID.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\um\wincodec\IWICBitmap.cs" />
+18
ファイルの表示
@@ -0,0 +1,18 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
// Ported from um/synchapi.h in the Windows SDK for Windows 10.0.20348.0
// Original source is Copyright © Microsoft. All rights reserved.
using System.Runtime.InteropServices;
namespace TerraFX.Interop.Windows
{
internal static unsafe partial class Windows
{
[DllImport("kernel32", ExactSpelling = true)]
public static extern BOOL RegisterWaitForSingleObject([NativeTypeName("PHANDLE")] HANDLE* phNewWaitObject, HANDLE hObject, [NativeTypeName("WAITORTIMERCALLBACK")] void* Callback, [NativeTypeName("PVOID")] void* Context, [NativeTypeName("ULONG")] uint dwMilliseconds, [NativeTypeName("ULONG")] uint dwFlags);
[NativeTypeName("#define INFINITE 0xFFFFFFFF")]
public const uint INFINITE = 0xFFFFFFFF;
}
}
+15
ファイルの表示
@@ -0,0 +1,15 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
// Ported from um/synchapi.h in the Windows SDK for Windows 10.0.20348.0
// Original source is Copyright © Microsoft. All rights reserved.
using System.Runtime.InteropServices;
namespace TerraFX.Interop.Windows
{
internal static unsafe partial class Windows
{
[DllImport("kernel32", ExactSpelling = true)]
public static extern BOOL CloseHandle(HANDLE hObject);
}
}
+3
ファイルの表示
@@ -9,6 +9,9 @@ namespace TerraFX.Interop.Windows
{
internal static unsafe partial class Windows
{
[DllImport("kernel32", ExactSpelling = true)]
public static extern HANDLE CreateEventW([NativeTypeName("LPSECURITY_ATTRIBUTES")] SECURITY_ATTRIBUTES* lpEventAttributes, BOOL bManualReset, BOOL bInitialState, [NativeTypeName("LPCWSTR")] ushort* lpName);
[DllImport("kernel32", ExactSpelling = true)]
[return: NativeTypeName("DWORD")]
public static extern uint WaitForSingleObjectEx(HANDLE hHandle, [NativeTypeName("DWORD")] uint dwMilliseconds, BOOL bAlertable);