Fix reference tracking with ComputeContext and async execution
このコミットが含まれているのは:
1 親
84ebe36975
コミット
1ab3931391
3ファイル変更
+70
-3
検索条件に一致するファイルはありません
@@ -9,6 +9,7 @@ using CommunityToolkit.Diagnostics;
|
||||
using ComputeSharp.Core.Extensions;
|
||||
using ComputeSharp.Graphics.Commands;
|
||||
using ComputeSharp.Graphics.Commands.Interop;
|
||||
using ComputeSharp.Interop;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
using static TerraFX.Interop.DirectX.D3D12_COMMAND_LIST_TYPE;
|
||||
@@ -191,7 +192,9 @@ unsafe partial class GraphicsDevice
|
||||
|
||||
HANDLE waitHandle;
|
||||
|
||||
_ = Windows.RegisterWaitForSingleObject(
|
||||
device.GetReferenceTracker().DangerousAddRef();
|
||||
|
||||
int result = Windows.RegisterWaitForSingleObject(
|
||||
phNewWaitObject: &waitHandle,
|
||||
hObject: eventHandle,
|
||||
#if NET6_0_OR_GREATER
|
||||
@@ -203,6 +206,16 @@ unsafe partial class GraphicsDevice
|
||||
dwMilliseconds: Windows.INFINITE,
|
||||
dwFlags: 0);
|
||||
|
||||
// The register is successful if the return value is nonzero
|
||||
if (result == 0)
|
||||
{
|
||||
device.GetReferenceTracker().DangerousRelease();
|
||||
|
||||
NativeMemory.Free(callbackContext);
|
||||
|
||||
ThrowHelper.ThrowWin32Exception("Failed to register the compute context completion callback.");
|
||||
}
|
||||
|
||||
return waitForFenceValueTaskSource;
|
||||
}
|
||||
|
||||
@@ -227,6 +240,9 @@ unsafe partial class GraphicsDevice
|
||||
|
||||
device.computeCommandListPool.Return(d3D12GraphicsCommandList, d3D12CommandAllocator);
|
||||
|
||||
// Decrement the reference count that was incremented when scheduling the completion callback
|
||||
device.GetReferenceTracker().DangerousRelease();
|
||||
|
||||
_ = Windows.CloseHandle(eventHandle);
|
||||
|
||||
NativeMemory.Free(callbackContext);
|
||||
|
||||
@@ -40,6 +40,23 @@ internal struct ReferenceTracker : IDisposable
|
||||
this.isDisposeRequested = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an untracked reference to the underlying object. This needs to be released with <see cref="DangerousRelease"/>.
|
||||
/// </summary>
|
||||
/// <exception cref="ObjectDisposedException">Thrown if the tracked object has been disposed.</exception>
|
||||
public void DangerousAddRef()
|
||||
{
|
||||
_ = GetLease();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrements the reference count for the underlying object. A call to this API has to match a previous <see cref="DangerousAddRef"/> call.
|
||||
/// </summary>
|
||||
public void DangerousRelease()
|
||||
{
|
||||
ReturnLease();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="Lease"/> value to extend the lifetime of the tracked object.
|
||||
/// </summary>
|
||||
|
||||
@@ -6,6 +6,7 @@ using CommunityToolkit.Diagnostics;
|
||||
using ComputeSharp.__Internals;
|
||||
using ComputeSharp.Graphics.Commands;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using ComputeSharp.Interop;
|
||||
using ComputeSharp.Shaders.Dispatching;
|
||||
using ComputeSharp.Shaders.Loading;
|
||||
using ComputeSharp.Shaders.Models;
|
||||
@@ -18,6 +19,15 @@ namespace ComputeSharp;
|
||||
/// <summary>
|
||||
/// A context to batch compute operations in a single invocation, minimizing GPU overhead.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This type must always be used in a <see langword="using"/> statement and disposed properly.
|
||||
/// Not doing so is undefined behavior and may result in the target device not being disposed correctly.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// For more documentation on this, see the remarks in <see cref="GraphicsDeviceExtensions.CreateComputeContext(ComputeSharp.GraphicsDevice)"/>.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public struct ComputeContext : IDisposable, IAsyncDisposable
|
||||
{
|
||||
/// <summary>
|
||||
@@ -30,6 +40,11 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
|
||||
/// </summary>
|
||||
private CommandList commandList;
|
||||
|
||||
/// <summary>
|
||||
/// The lease to ensure <see cref="device"/> is not disposed until the context is no longer in use.
|
||||
/// </summary>
|
||||
private ReferenceTracker.Lease lease;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ComputeContext"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
@@ -38,6 +53,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
|
||||
{
|
||||
this.device = device;
|
||||
this.commandList = default;
|
||||
this.lease = device.GetReferenceTracker().GetLease();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -290,10 +306,19 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
|
||||
|
||||
if (!this.commandList.IsAllocated)
|
||||
{
|
||||
this.lease.Dispose();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.commandList.ExecuteAndWaitForCompletion();
|
||||
try
|
||||
{
|
||||
this.commandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
finally
|
||||
{
|
||||
this.lease.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -306,6 +331,8 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
|
||||
|
||||
if (!this.commandList.IsAllocated)
|
||||
{
|
||||
this.lease.Dispose();
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
return ValueTask.CompletedTask;
|
||||
#else
|
||||
@@ -313,7 +340,14 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
|
||||
#endif
|
||||
}
|
||||
|
||||
return this.commandList.ExecuteAndWaitForCompletionAsync();
|
||||
try
|
||||
{
|
||||
return this.commandList.ExecuteAndWaitForCompletionAsync();
|
||||
}
|
||||
finally
|
||||
{
|
||||
this.lease.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする