diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs b/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs index 8f17e00f..e8f55f5c 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs @@ -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); diff --git a/src/ComputeSharp/Graphics/Interop/ReferenceTracker.cs b/src/ComputeSharp/Graphics/Interop/ReferenceTracker.cs index 3f26d23b..6d325165 100644 --- a/src/ComputeSharp/Graphics/Interop/ReferenceTracker.cs +++ b/src/ComputeSharp/Graphics/Interop/ReferenceTracker.cs @@ -40,6 +40,23 @@ internal struct ReferenceTracker : IDisposable this.isDisposeRequested = false; } + /// + /// Adds an untracked reference to the underlying object. This needs to be released with . + /// + /// Thrown if the tracked object has been disposed. + public void DangerousAddRef() + { + _ = GetLease(); + } + + /// + /// Decrements the reference count for the underlying object. A call to this API has to match a previous call. + /// + public void DangerousRelease() + { + ReturnLease(); + } + /// /// Gets a value to extend the lifetime of the tracked object. /// diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index 46c03c31..321a43af 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -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; /// /// A context to batch compute operations in a single invocation, minimizing GPU overhead. /// +/// +/// +/// This type must always be used in a statement and disposed properly. +/// Not doing so is undefined behavior and may result in the target device not being disposed correctly. +/// +/// +/// For more documentation on this, see the remarks in . +/// +/// public struct ComputeContext : IDisposable, IAsyncDisposable { /// @@ -30,6 +40,11 @@ public struct ComputeContext : IDisposable, IAsyncDisposable /// private CommandList commandList; + /// + /// The lease to ensure is not disposed until the context is no longer in use. + /// + private ReferenceTracker.Lease lease; + /// /// Creates a new instance with the specified parameters. /// @@ -38,6 +53,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable { this.device = device; this.commandList = default; + this.lease = device.GetReferenceTracker().GetLease(); } /// @@ -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(); + } } /// @@ -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(); + } } ///