From 42c862c148442d4942ea82ad988ebeed1828611f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 31 Jul 2022 16:13:45 +0200 Subject: [PATCH] Fix disposing resources after device on .NET 6 --- .../Extensions/Interop/ComPtrExtensions.cs | 19 ++----------------- src/ComputeSharp/Graphics/GraphicsDevice.cs | 11 +++++++++-- .../DeviceDisposalTests.cs | 5 ----- 3 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs index 86e597ea..40e28633 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs @@ -8,6 +8,7 @@ namespace ComputeSharp.Core.Extensions; /// internal static class ComPtrExtensions { +#if NET6_0_OR_GREATER /// /// Invokes on the wrapped object for an input value. /// @@ -16,20 +17,11 @@ internal static class ComPtrExtensions /// A instance of type . [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe uint AddRef(this ComPtr ptr) -#if NET6_0_OR_GREATER where T : unmanaged, IUnknown.Interface -#else - where T : unmanaged -#endif { if (ptr.Get() is not null) { -#if NET6_0_OR_GREATER return ptr.Get()->AddRef(); -#else - return ((IUnknown*)ptr.Get())->AddRef(); -#endif - } return 0; @@ -44,19 +36,11 @@ internal static class ComPtrExtensions /// A instance of type . [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe uint Release(this ref ComPtr ptr) -#if NET6_0_OR_GREATER where T : unmanaged, IUnknown.Interface -#else - where T : unmanaged -#endif { if (ptr.Get() is not null) { -#if NET6_0_OR_GREATER uint count = ptr.Get()->Release(); -#else - uint count = ((IUnknown*)ptr.Get())->Release(); -#endif if (count == 0) { @@ -68,6 +52,7 @@ internal static class ComPtrExtensions return 0; } +#endif /// /// Reinterprets the current instance as one of type . diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 9f243bff..2847205e 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -444,9 +444,16 @@ public sealed unsafe partial class GraphicsDevice : NativeObject this.computeCommandListPool.Dispose(); this.copyCommandListPool.Dispose(); this.shaderResourceViewDescriptorAllocator.Dispose(); + + // On .NET 6, D3D12MA is used. In this case, the pool and allocator must be kept alive + // until all associated resources are returned and destroyed. Because of this, when the + // device is disposed (since there might be outstanding resources that are still alive), + // the pool and allocator are only released, but not disposed. This allows reosurces to + // also release them when disposed (since each resource keeps a reference back to the + // parent device). When the last one is disposed, the pool and allocator will be deleted. #if NET6_0_OR_GREATER - this.pool.Dispose(); - this.allocator.Dispose(); + this.pool.Release(); + this.allocator.Release(); #endif UnregisterDeviceLostCallback(this); diff --git a/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs b/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs index 83ce5707..7d2c3be4 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs +++ b/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs @@ -82,11 +82,6 @@ public partial class DeviceDisposalTests [AllDevices] public void DeviceDisposal_DisposedResourcesAfterDevice_ReferenceCounting(Device device) { -#if NET6_0_OR_GREATER - // TODO: investigate ref count with resources disposed after device when using D3D12MA - Assert.Inconclusive(); -#endif - using ComPtr d3D12Device = default; ConstantBuffer r1;