From c7f7eac265ee5ecfeca86331a40cc86c6cf40dc4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 17 Mar 2021 17:27:22 +0100 Subject: [PATCH 1/5] Added ReferenceCountPtr type, code refactoring --- .../Extensions/Interop/AllocatorExtensions.cs | 4 +- .../Interop/ID3D12DeviceExtensions.cs | 21 +++ src/ComputeSharp/Graphics/GraphicsDevice.cs | 31 ++-- .../Graphics/Interop/ReferenceCountPtr{T}.cs | 133 ++++++++++++++++++ .../Graphics/Interop/UniquePtr{T}.cs | 2 +- 5 files changed, 178 insertions(+), 13 deletions(-) create mode 100644 src/ComputeSharp/Graphics/Interop/ReferenceCountPtr{T}.cs diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs index 1b232919..5e401f67 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs @@ -22,9 +22,9 @@ namespace ComputeSharp.Graphics.Extensions /// /// The instance in use. /// A instance suited to be used for cache coherent UMA devices. - public static UniquePtr CreatePoolForCacheCoherentUMA(this ref D3D12MA_Allocator allocator) + public static ReferenceCountPtr CreatePoolForCacheCoherentUMA(this ref D3D12MA_Allocator allocator) { - using UniquePtr pool = default; + using ReferenceCountPtr pool = default; D3D12MA_POOL_DESC poolDesc = default; poolDesc.HeapProperties.CreationNodeMask = 1; diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs index 9287ed3f..87462db5 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs @@ -1,6 +1,8 @@ using System; using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; using ComputeSharp.Core.Extensions; +using ComputeSharp.Interop; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_QUEUE_FLAGS; using static TerraFX.Interop.D3D12_COMMAND_QUEUE_PRIORITY; @@ -20,6 +22,25 @@ namespace ComputeSharp.Graphics.Extensions /// internal static unsafe class ID3D12DeviceExtensions { + /// + /// Creates a instance for a given device. + /// + /// The target to use to create the allocator. + /// The that was created from. + /// A instance to create resources on the device in use. + public static ReferenceCountPtr CreateAllocator(this ref ID3D12Device d3D12Device, IDXGIAdapter* dxgiAdapter) + { + using ReferenceCountPtr allocator = default; + + D3D12MA_ALLOCATOR_DESC allocatorDesc = default; + allocatorDesc.pDevice = (ID3D12Device*)Unsafe.AsPointer(ref d3D12Device); + allocatorDesc.pAdapter = dxgiAdapter; + + D3D12MemAlloc.D3D12MA_CreateAllocator(&allocatorDesc, allocator.GetAddressOf()).Assert(); + + return allocator.Move(); + } + /// /// Creates a new for a given device. /// diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 3e47fad7..546f6d5a 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -76,12 +76,12 @@ namespace ComputeSharp /// /// The in use associated to the current device. /// - private UniquePtr allocator; + private ReferenceCountPtr allocator; /// /// The instance in use, if is . /// - private UniquePtr pool; + private ReferenceCountPtr pool; /// /// Creates a new instance for the input . @@ -117,14 +117,7 @@ namespace ComputeSharp IsCacheCoherentUMA = d3D12Architecture1Data.CacheCoherentUMA != 0; - D3D12MA_ALLOCATOR_DESC allocatorDesc = default; - allocatorDesc.pDevice = d3D12Device; - allocatorDesc.pAdapter = dxgiAdapter; - - fixed (D3D12MA_Allocator** allocator = this.allocator) - { - D3D12MemAlloc.D3D12MA_CreateAllocator(&allocatorDesc, allocator).Assert(); - } + this.allocator = d3D12Device->CreateAllocator(dxgiAdapter); if (IsCacheCoherentUMA) { @@ -252,6 +245,24 @@ namespace ComputeSharp D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW); } + /// + /// Registers that a new resource has been allocated on the current device. + /// + internal void RegisterAllocatedResource() + { + this.pool.AddRef(); + this.allocator.AddRef(); + } + + /// + /// Unregisters a generic resource that was allocated on the current device. + /// + internal void UnregisterAllocatedResource() + { + this.pool.Dispose(); + this.allocator.Dispose(); + } + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void RentShaderResourceViewDescriptorHandles( diff --git a/src/ComputeSharp/Graphics/Interop/ReferenceCountPtr{T}.cs b/src/ComputeSharp/Graphics/Interop/ReferenceCountPtr{T}.cs new file mode 100644 index 00000000..cc7d8e51 --- /dev/null +++ b/src/ComputeSharp/Graphics/Interop/ReferenceCountPtr{T}.cs @@ -0,0 +1,133 @@ +using System; +using System.ComponentModel; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using System.Threading; +using TerraFX.Interop; + +namespace ComputeSharp.Interop +{ + /// + /// A -equivalent type to safely work with pointers to public APIs. + /// This special type internally keeps track of a reference count for the wrapped native object. + /// This works around the lack of internal reference counting for objects from . + /// + /// Must be either , , or . + /// + /// This type is not marked as so that it can also be used in fields, so make sure to + /// avoid copying it and disposing it multiple times, as that would lead to a access violation exceptions. + /// + internal unsafe struct ReferenceCountPtr : IDisposable + where T : unmanaged + { + /// + /// The raw pointer to an object, if existing. + /// + private T* pointer; + + /// + /// The current reference count - 1 for . + /// + private volatile int count; + + /// + /// Creates a new instance from a raw pointer. + /// + /// The raw pointer to wrap. + public ReferenceCountPtr(T* other) + { + this.pointer = other; + this.count = 0; + } + + /// + /// Unwraps a instance and returns the internal raw pointer. + /// + /// The instance to unwrap. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator T*(ReferenceCountPtr other) + { + return other.Get(); + } + + /// + /// Increments the internal reference count. + /// + public void AddRef() + { + Interlocked.Increment(ref this.count); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Dispose() + { + if (Interlocked.Decrement(ref this.count) < 0) + { + T* pointer = this.pointer; + + if (pointer != null) + { + this.pointer = null; + + if (typeof(T) == typeof(D3D12MA_Allocator)) ((D3D12MA_Allocator*)pointer)->Release(); + else if (typeof(T) == typeof(D3D12MA_Allocation)) ((D3D12MA_Allocation*)pointer)->Release(); + else if (typeof(T) == typeof(D3D12MA_Pool)) ((D3D12MA_Pool*)pointer)->Release(); + else if (typeof(T) == typeof(D3D12MA_VirtualBlock)) ((D3D12MA_VirtualBlock*)pointer)->Release(); + else throw new ArgumentException("Invalid pointer type"); + } + } + } + + /// + /// Gets the currently wrapped raw pointer to an object. + /// + /// The raw pointer wrapped by the current instance. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly T* Get() + { + return this.pointer; + } + + /// + /// Gets the address of the current instance as a raw double pointer. + /// + /// The raw pointer to the current instance. + /// This method is only valid when the current instance is on the stack or pinned. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly T** GetAddressOf() + { + return (T**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); + } + + /// + /// Gets the address of the current instance as a managed reference to pointer. + /// + /// The reference to the current instance. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [EditorBrowsable(EditorBrowsableState.Never)] + public readonly ref T* GetPinnableReference() + { + fixed (T** ptr = &this.pointer) + { + return ref *ptr; + } + } + + /// + /// Moves the current instance and resets it without releasing the reference. + /// + /// The moved instance. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ReferenceCountPtr Move() + { + ReferenceCountPtr copy = this; + + this = default; + + return copy; + } + } +} diff --git a/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs b/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs index 2cec96e1..0a074455 100644 --- a/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs +++ b/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs @@ -28,7 +28,7 @@ namespace ComputeSharp.Interop /// The raw pointer to wrap. public UniquePtr(T* other) { - pointer = other; + this.pointer = other; } /// From df970a5e40105209ea786296d13b4d097116e7c7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 17 Mar 2021 17:27:48 +0100 Subject: [PATCH 2/5] Fixed resource disposing after owning device --- .../Graphics/Resources/Abstract/Buffer{T}.cs | 3 ++ .../Resources/Abstract/Texture2D{T}.cs | 3 ++ .../Resources/Abstract/Texture3D{T}.cs | 3 ++ .../Resources/Abstract/TransferBuffer{T}.cs | 4 ++ .../Abstract/TransferTexture2D{T}.cs | 4 ++ .../Abstract/TransferTexture3D{T}.cs | 4 ++ tests/ComputeSharp.Tests/BufferTests.cs | 27 +++++++++++++ tests/ComputeSharp.Tests/Texture2DTests.cs | 26 +++++++++++++ tests/ComputeSharp.Tests/Texture3DTests.cs | 26 +++++++++++++ .../ComputeSharp.Tests/TransferBufferTests.cs | 26 +++++++++++++ .../TransferTexture2DTests.cs | 38 ++++++++++++++++--- .../TransferTexture3DTests.cs | 38 ++++++++++++++++--- 12 files changed, 190 insertions(+), 12 deletions(-) diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index b3bc0bb1..1aafa7de 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -80,6 +80,7 @@ namespace ComputeSharp.Resources this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, (ulong)SizeInBytes); this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); + device.RegisterAllocatedResource(); device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); switch (resourceType) @@ -172,6 +173,8 @@ namespace ComputeSharp.Resources this.d3D12Resource.Dispose(); this.allocation.Dispose(); + GraphicsDevice.UnregisterAllocatedResource(); + if (GraphicsDevice?.IsDisposed == false) { GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 172ee5f9..4748c9c1 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -107,6 +107,7 @@ namespace ComputeSharp.Resources out _, out _); + device.RegisterAllocatedResource(); device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); switch (resourceType) @@ -436,6 +437,8 @@ namespace ComputeSharp.Resources this.d3D12Resource.Dispose(); this.allocation.Dispose(); + GraphicsDevice.UnregisterAllocatedResource(); + if (GraphicsDevice?.IsDisposed == false) { GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 917675c7..23bd7e13 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -111,6 +111,7 @@ namespace ComputeSharp.Resources out _, out _); + device.RegisterAllocatedResource(); device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); switch (resourceType) @@ -479,6 +480,8 @@ namespace ComputeSharp.Resources this.d3D12Resource.Dispose(); this.allocation.Dispose(); + GraphicsDevice.UnregisterAllocatedResource(); + if (GraphicsDevice?.IsDisposed == false) { GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs index d3625203..6dcf981b 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -54,6 +54,8 @@ namespace ComputeSharp.Resources this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, sizeInBytes); this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); + device.RegisterAllocatedResource(); + this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; this.d3D12Resource.Get()->SetName(this); @@ -109,6 +111,8 @@ namespace ComputeSharp.Resources this.d3D12Resource.Dispose(); this.allocation.Dispose(); + GraphicsDevice.UnregisterAllocatedResource(); + return true; } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs index 593c0079..ff77a788 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -71,6 +71,8 @@ namespace ComputeSharp.Resources this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes); this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); + device.RegisterAllocatedResource(); + this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; this.d3D12Resource.Get()->SetName(this); @@ -119,6 +121,8 @@ namespace ComputeSharp.Resources this.d3D12Resource.Dispose(); this.allocation.Dispose(); + GraphicsDevice.UnregisterAllocatedResource(); + return true; } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs index 0f82c7b9..a37bdf6e 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -74,6 +74,8 @@ namespace ComputeSharp.Resources this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes); this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); + device.RegisterAllocatedResource(); + this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; this.d3D12Resource.Get()->SetName(this); @@ -126,6 +128,8 @@ namespace ComputeSharp.Resources this.d3D12Resource.Dispose(); this.allocation.Dispose(); + GraphicsDevice.UnregisterAllocatedResource(); + return true; } diff --git a/tests/ComputeSharp.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs index 705b166f..bebf29b8 100644 --- a/tests/ComputeSharp.Tests/BufferTests.cs +++ b/tests/ComputeSharp.Tests/BufferTests.cs @@ -111,6 +111,33 @@ namespace ComputeSharp.Tests _ = buffer.ToArray(); } + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ConstantBuffer<>))] + [Resource(typeof(ReadOnlyBuffer<>))] + [Resource(typeof(ReadWriteBuffer<>))] + public void DisposeAfterDevice(Device device, Type bufferType) + { + GraphicsDevice? gpu = device switch + { + Device.Discrete => Gpu.QueryDevices(info => info.IsHardwareAccelerated).FirstOrDefault(), + Device.Warp => Gpu.QueryDevices(info => !info.IsHardwareAccelerated).First(), + _ => throw new ArgumentException(nameof(device)) + }; + + if (gpu is null) + { + Assert.Inconclusive(); + + return; + } + + Buffer buffer = gpu.AllocateBuffer(bufferType, 128); + + gpu.Dispose(); + buffer.Dispose(); + } + [CombinatorialTestMethod] [AllDevices] [Resource(typeof(ConstantBuffer<>))] diff --git a/tests/ComputeSharp.Tests/Texture2DTests.cs b/tests/ComputeSharp.Tests/Texture2DTests.cs index ee23d188..3f54caef 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.cs @@ -86,6 +86,32 @@ namespace ComputeSharp.Tests _ = texture.ToArray(); } + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<>))] + [Resource(typeof(ReadWriteTexture2D<>))] + public void DisposeAfterDevice(Device device, Type textureType) + { + GraphicsDevice? gpu = device switch + { + Device.Discrete => Gpu.QueryDevices(info => info.IsHardwareAccelerated).FirstOrDefault(), + Device.Warp => Gpu.QueryDevices(info => !info.IsHardwareAccelerated).First(), + _ => throw new ArgumentException(nameof(device)) + }; + + if (gpu is null) + { + Assert.Inconclusive(); + + return; + } + + Texture2D texture = gpu.AllocateTexture2D(textureType, 10, 10); + + gpu.Dispose(); + texture.Dispose(); + } + [CombinatorialTestMethod] [AllDevices] [Resource(typeof(ReadOnlyTexture2D<>))] diff --git a/tests/ComputeSharp.Tests/Texture3DTests.cs b/tests/ComputeSharp.Tests/Texture3DTests.cs index 34a384b7..c860d70f 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.cs +++ b/tests/ComputeSharp.Tests/Texture3DTests.cs @@ -105,6 +105,32 @@ namespace ComputeSharp.Tests _ = texture.ToArray(); } + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + public void DisposeAfterDevice(Device device, Type textureType) + { + GraphicsDevice? gpu = device switch + { + Device.Discrete => Gpu.QueryDevices(info => info.IsHardwareAccelerated).FirstOrDefault(), + Device.Warp => Gpu.QueryDevices(info => !info.IsHardwareAccelerated).First(), + _ => throw new ArgumentException(nameof(device)) + }; + + if (gpu is null) + { + Assert.Inconclusive(); + + return; + } + + Texture3D texture = gpu.AllocateTexture3D(textureType, 10, 10, 4); + + gpu.Dispose(); + texture.Dispose(); + } + [CombinatorialTestMethod] [AllDevices] [Resource(typeof(ReadOnlyTexture3D<>))] diff --git a/tests/ComputeSharp.Tests/TransferBufferTests.cs b/tests/ComputeSharp.Tests/TransferBufferTests.cs index a16c9c3d..c35afee1 100644 --- a/tests/ComputeSharp.Tests/TransferBufferTests.cs +++ b/tests/ComputeSharp.Tests/TransferBufferTests.cs @@ -66,6 +66,32 @@ namespace ComputeSharp.Tests _ = buffer.Span; } + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadBuffer<>))] + [Resource(typeof(ReadBackBuffer<>))] + public void DisposeAfterDevice(Device device, Type bufferType) + { + GraphicsDevice? gpu = device switch + { + Device.Discrete => Gpu.QueryDevices(info => info.IsHardwareAccelerated).FirstOrDefault(), + Device.Warp => Gpu.QueryDevices(info => !info.IsHardwareAccelerated).First(), + _ => throw new ArgumentException(nameof(device)) + }; + + if (gpu is null) + { + Assert.Inconclusive(); + + return; + } + + TransferBuffer buffer = gpu.AllocateTransferBuffer(bufferType, 128); + + gpu.Dispose(); + buffer.Dispose(); + } + [CombinatorialTestMethod] [AllDevices] public void Allocate_UploadBuffer_Copy_Full(Device device) diff --git a/tests/ComputeSharp.Tests/TransferTexture2DTests.cs b/tests/ComputeSharp.Tests/TransferTexture2DTests.cs index 2e4a6e58..2a413ecf 100644 --- a/tests/ComputeSharp.Tests/TransferTexture2DTests.cs +++ b/tests/ComputeSharp.Tests/TransferTexture2DTests.cs @@ -18,9 +18,9 @@ namespace ComputeSharp.Tests [Resource(typeof(ReadBackTexture2D<>))] [Data(AllocationMode.Default)] [Data(AllocationMode.Clear)] - public unsafe void Allocate_Uninitialized_Ok(Device device, Type bufferType, AllocationMode allocationMode) + public unsafe void Allocate_Uninitialized_Ok(Device device, Type textureType, AllocationMode allocationMode) { - using TransferTexture2D texture = device.Get().AllocateTransferTexture2D(bufferType, 128, 256, allocationMode); + using TransferTexture2D texture = device.Get().AllocateTransferTexture2D(textureType, 128, 256, allocationMode); Assert.IsNotNull(texture); Assert.AreEqual(texture.Width, 128); @@ -50,9 +50,9 @@ namespace ComputeSharp.Tests [Data(0, -4314)] [Data(-14, -53)] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void Allocate_Uninitialized_Fail(Device device, Type bufferType, int width, int height) + public void Allocate_Uninitialized_Fail(Device device, Type textureType, int width, int height) { - using TransferTexture2D texture = device.Get().AllocateTransferTexture2D(bufferType, width, height); + using TransferTexture2D texture = device.Get().AllocateTransferTexture2D(textureType, width, height); } [CombinatorialTestMethod] @@ -60,15 +60,41 @@ namespace ComputeSharp.Tests [Resource(typeof(UploadTexture2D<>))] [Resource(typeof(ReadBackTexture2D<>))] [ExpectedException(typeof(ObjectDisposedException))] - public void UsageAfterDispose(Device device, Type bufferType) + public void UsageAfterDispose(Device device, Type textureType) { - using TransferTexture2D texture = device.Get().AllocateTransferTexture2D(bufferType, 32, 32); + using TransferTexture2D texture = device.Get().AllocateTransferTexture2D(textureType, 32, 32); texture.Dispose(); _ = texture.View; } + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadTexture2D<>))] + [Resource(typeof(ReadBackTexture2D<>))] + public void DisposeAfterDevice(Device device, Type textureType) + { + GraphicsDevice? gpu = device switch + { + Device.Discrete => Gpu.QueryDevices(info => info.IsHardwareAccelerated).FirstOrDefault(), + Device.Warp => Gpu.QueryDevices(info => !info.IsHardwareAccelerated).First(), + _ => throw new ArgumentException(nameof(device)) + }; + + if (gpu is null) + { + Assert.Inconclusive(); + + return; + } + + TransferTexture2D texture = gpu.AllocateTransferTexture2D(textureType, 32, 32); + + gpu.Dispose(); + texture.Dispose(); + } + [CombinatorialTestMethod] [AllDevices] public void Allocate_UploadTexture2D_Copy_Full(Device device) diff --git a/tests/ComputeSharp.Tests/TransferTexture3DTests.cs b/tests/ComputeSharp.Tests/TransferTexture3DTests.cs index ad06735c..302198d0 100644 --- a/tests/ComputeSharp.Tests/TransferTexture3DTests.cs +++ b/tests/ComputeSharp.Tests/TransferTexture3DTests.cs @@ -18,9 +18,9 @@ namespace ComputeSharp.Tests [Resource(typeof(ReadBackTexture3D<>))] [Data(AllocationMode.Default)] [Data(AllocationMode.Clear)] - public unsafe void Allocate_Uninitialized_Ok(Device device, Type bufferType, AllocationMode allocationMode) + public unsafe void Allocate_Uninitialized_Ok(Device device, Type textureType, AllocationMode allocationMode) { - using TransferTexture3D texture = device.Get().AllocateTransferTexture3D(bufferType, 128, 256, 32, allocationMode); + using TransferTexture3D texture = device.Get().AllocateTransferTexture3D(textureType, 128, 256, 32, allocationMode); Assert.IsNotNull(texture); Assert.AreEqual(texture.Width, 128); @@ -57,9 +57,9 @@ namespace ComputeSharp.Tests [Data(12, 12, -1)] [Data(2, 4, ushort.MaxValue + 1)] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void Allocate_Uninitialized_Fail(Device device, Type bufferType, int width, int height, int depth) + public void Allocate_Uninitialized_Fail(Device device, Type textureType, int width, int height, int depth) { - using TransferTexture3D texture = device.Get().AllocateTransferTexture3D(bufferType, width, height, depth); + using TransferTexture3D texture = device.Get().AllocateTransferTexture3D(textureType, width, height, depth); } [CombinatorialTestMethod] @@ -67,15 +67,41 @@ namespace ComputeSharp.Tests [Resource(typeof(UploadTexture3D<>))] [Resource(typeof(ReadBackTexture3D<>))] [ExpectedException(typeof(ObjectDisposedException))] - public void UsageAfterDispose(Device device, Type bufferType) + public void UsageAfterDispose(Device device, Type textureType) { - using TransferTexture3D texture = device.Get().AllocateTransferTexture3D(bufferType, 32, 32, 16); + using TransferTexture3D texture = device.Get().AllocateTransferTexture3D(textureType, 32, 32, 16); texture.Dispose(); _ = texture.View; } + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadTexture3D<>))] + [Resource(typeof(ReadBackTexture3D<>))] + public void DisposeAfterDevice(Device device, Type textureType) + { + GraphicsDevice? gpu = device switch + { + Device.Discrete => Gpu.QueryDevices(info => info.IsHardwareAccelerated).FirstOrDefault(), + Device.Warp => Gpu.QueryDevices(info => !info.IsHardwareAccelerated).First(), + _ => throw new ArgumentException(nameof(device)) + }; + + if (gpu is null) + { + Assert.Inconclusive(); + + return; + } + + TransferTexture3D texture = gpu.AllocateTransferTexture3D(textureType, 32, 32, 16); + + gpu.Dispose(); + texture.Dispose(); + } + [CombinatorialTestMethod] [AllDevices] public void Allocate_UploadTexture3D_Copy_Full(Device device) From bee668f2f8e95de0ee4b1bf8213c2189ed3091f4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 6 Jul 2021 13:47:03 +0200 Subject: [PATCH 3/5] Improved concurrency support for native resource disposal --- .../Graphics/Commands/PipelineData.cs | 4 +- src/ComputeSharp/Graphics/GraphicsDevice.cs | 6 +-- .../Helpers/DeviceHelper.Enumerator.cs | 4 +- .../Graphics/Interop/NativeObject.cs | 40 ++++++++++--------- .../Graphics/Resources/Abstract/Buffer{T}.cs | 4 +- .../Resources/Abstract/Texture2D{T}.cs | 9 +---- .../Resources/Abstract/Texture3D{T}.cs | 9 +---- .../Resources/Abstract/TransferBuffer{T}.cs | 4 +- .../Abstract/TransferTexture2D{T}.cs | 4 +- .../Abstract/TransferTexture3D{T}.cs | 4 +- .../Translation/Models/CachedShader.cs | 4 +- 11 files changed, 34 insertions(+), 58 deletions(-) diff --git a/src/ComputeSharp/Graphics/Commands/PipelineData.cs b/src/ComputeSharp/Graphics/Commands/PipelineData.cs index 7f0e16d1..602c4ded 100644 --- a/src/ComputeSharp/Graphics/Commands/PipelineData.cs +++ b/src/ComputeSharp/Graphics/Commands/PipelineData.cs @@ -40,12 +40,10 @@ namespace ComputeSharp.Graphics.Commands public ID3D12PipelineState* D3D12PipelineState => this.d3D12PipelineState; /// - protected override bool OnDispose() + protected override void OnDispose() { this.d3D12RootSignature.Dispose(); this.d3D12PipelineState.Dispose(); - - return true; } } } diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 546f6d5a..1a1bbd0f 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -381,11 +381,11 @@ namespace ComputeSharp } /// - protected override bool OnDispose() + protected override void OnDispose() { if (DeviceHelper.GetDefaultDeviceLuid() == Luid) { - return false; + return; } DeviceHelper.NotifyDisposedDevice(this); @@ -400,8 +400,6 @@ namespace ComputeSharp this.shaderResourceViewDescriptorAllocator.Dispose(); this.pool.Dispose(); this.allocator.Dispose(); - - return true; } /// diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs index 69bee9a5..dd31c1f0 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs @@ -203,11 +203,9 @@ namespace ComputeSharp.Graphics.Helpers } /// - protected override bool OnDispose() + protected override void OnDispose() { this.dxgiFactory4.Dispose(); - - return true; } } } diff --git a/src/ComputeSharp/Graphics/Interop/NativeObject.cs b/src/ComputeSharp/Graphics/Interop/NativeObject.cs index 10bdb1cd..fc20600f 100644 --- a/src/ComputeSharp/Graphics/Interop/NativeObject.cs +++ b/src/ComputeSharp/Graphics/Interop/NativeObject.cs @@ -1,5 +1,6 @@ using System; using System.Runtime.CompilerServices; +using System.Threading; namespace ComputeSharp.Interop { @@ -8,51 +9,52 @@ namespace ComputeSharp.Interop /// public abstract class NativeObject : IDisposable { + /// + /// Indicates whether or not the current instance has been disposed. + /// + private volatile int isDisposed; + /// /// Releases unmanaged resources and performs other cleanup operations. /// ~NativeObject() { - _ = CheckAndDispose(); + CheckAndDispose(); } /// /// Gets whether or not the current instance has already been disposed. /// - internal bool IsDisposed { get; private set; } + internal bool IsDisposed + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.isDisposed != 0; + } - /// - /// Releases all the native resources for the current instance. - /// + /// public void Dispose() { - if (CheckAndDispose()) - { - GC.SuppressFinalize(this); - } + CheckAndDispose(); + + GC.SuppressFinalize(this); } /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// - private bool CheckAndDispose() + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void CheckAndDispose() { - if (!IsDisposed) + if (Interlocked.CompareExchange(ref this.isDisposed, 1, 0) == 0) { - return IsDisposed = OnDispose(); + OnDispose(); } - - return true; } /// /// Releases unmanaged and (optionally) managed resources. /// - /// - /// Whether or not the dispose has actually been executed. This is done in order to allow derived types to - /// optionally cancel a dispose operation, by not releasing resources and returning . - /// - protected abstract bool OnDispose(); + protected abstract void OnDispose(); /// /// Throws an if the current instance has been disposed. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 1aafa7de..ea97671a 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -168,7 +168,7 @@ namespace ComputeSharp.Resources } /// - protected override bool OnDispose() + protected override void OnDispose() { this.d3D12Resource.Dispose(); this.allocation.Dispose(); @@ -179,8 +179,6 @@ namespace ComputeSharp.Resources { GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); } - - return true; } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 4748c9c1..74f17096 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -432,19 +432,14 @@ namespace ComputeSharp.Resources } /// - protected override bool OnDispose() + protected override void OnDispose() { this.d3D12Resource.Dispose(); this.allocation.Dispose(); GraphicsDevice.UnregisterAllocatedResource(); - if (GraphicsDevice?.IsDisposed == false) - { - GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); - } - - return true; + GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 23bd7e13..1248db42 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -475,19 +475,14 @@ namespace ComputeSharp.Resources } /// - protected override bool OnDispose() + protected override void OnDispose() { this.d3D12Resource.Dispose(); this.allocation.Dispose(); GraphicsDevice.UnregisterAllocatedResource(); - if (GraphicsDevice?.IsDisposed == false) - { - GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); - } - - return true; + GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs index 6dcf981b..7476b25e 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -106,14 +106,12 @@ namespace ComputeSharp.Resources } /// - protected override bool OnDispose() + protected override void OnDispose() { this.d3D12Resource.Dispose(); this.allocation.Dispose(); GraphicsDevice.UnregisterAllocatedResource(); - - return true; } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs index ff77a788..dbabfe4b 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -116,14 +116,12 @@ namespace ComputeSharp.Resources } /// - protected override bool OnDispose() + protected override void OnDispose() { this.d3D12Resource.Dispose(); this.allocation.Dispose(); GraphicsDevice.UnregisterAllocatedResource(); - - return true; } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs index a37bdf6e..1aa16cc1 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -123,14 +123,12 @@ namespace ComputeSharp.Resources } /// - protected override bool OnDispose() + protected override void OnDispose() { this.d3D12Resource.Dispose(); this.allocation.Dispose(); GraphicsDevice.UnregisterAllocatedResource(); - - return true; } /// diff --git a/src/ComputeSharp/Shaders/Translation/Models/CachedShader.cs b/src/ComputeSharp/Shaders/Translation/Models/CachedShader.cs index 8bf11804..cf2664f3 100644 --- a/src/ComputeSharp/Shaders/Translation/Models/CachedShader.cs +++ b/src/ComputeSharp/Shaders/Translation/Models/CachedShader.cs @@ -35,11 +35,9 @@ namespace ComputeSharp.Shaders.Translation.Models public D3D12_SHADER_BYTECODE D3D12ShaderBytecode => new((ID3DBlob*)this.dxcBlobBytecode.Get()); /// - protected override bool OnDispose() + protected override void OnDispose() { this.dxcBlobBytecode.Dispose(); - - return true; } } } From 740420ddcdec21fb1cdd9d67aecd25340f3f3b16 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Jul 2021 14:31:52 +0200 Subject: [PATCH 4/5] Fixed NativeObject.Dispose() logic for GraphicsDevice instances --- src/ComputeSharp/Graphics/GraphicsDevice.cs | 5 --- .../Graphics/Interop/NativeObject.cs | 37 +++++++++++++------ 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 1a1bbd0f..f11840c8 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -383,11 +383,6 @@ namespace ComputeSharp /// protected override void OnDispose() { - if (DeviceHelper.GetDefaultDeviceLuid() == Luid) - { - return; - } - DeviceHelper.NotifyDisposedDevice(this); this.d3D12Device.Dispose(); diff --git a/src/ComputeSharp/Graphics/Interop/NativeObject.cs b/src/ComputeSharp/Graphics/Interop/NativeObject.cs index fc20600f..6bb2f3ce 100644 --- a/src/ComputeSharp/Graphics/Interop/NativeObject.cs +++ b/src/ComputeSharp/Graphics/Interop/NativeObject.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using System.Threading; +using ComputeSharp.Graphics.Helpers; namespace ComputeSharp.Interop { @@ -19,7 +20,10 @@ namespace ComputeSharp.Interop /// ~NativeObject() { - CheckAndDispose(); + if (Interlocked.CompareExchange(ref this.isDisposed, 1, 0) == 0) + { + OnDispose(); + } } /// @@ -32,22 +36,31 @@ namespace ComputeSharp.Interop } /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Dispose() { - CheckAndDispose(); + // If the current instance is a graphics device, check that the LUID doesn't + // match the one for the default device, which is deliberately never disposed. + // This type check is inlined and then resolved at JIT time if the target instance + // is visible to the compiler. That is, if Dispose() is explicitly called on a + // sealed type that is not GraphicsDevice, this entire path will just be removed. + if (GetType() == typeof(GraphicsDevice) && + DeviceHelper.GetDefaultDeviceLuid() == Unsafe.As(this).Luid) + { + return; + } - GC.SuppressFinalize(this); - } - - /// - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void CheckAndDispose() - { if (Interlocked.CompareExchange(ref this.isDisposed, 1, 0) == 0) { - OnDispose(); + [MethodImpl(MethodImplOptions.NoInlining)] + void DisposeAndSuppressFinalize() + { + OnDispose(); + + GC.SuppressFinalize(this); + } + + DisposeAndSuppressFinalize(); } } From 65812a422b96ad07a9390feed9be53caad380288 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 18 Jul 2021 14:45:55 +0200 Subject: [PATCH 5/5] Fixed a crash when disposing faulted resources --- src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs | 7 +++---- .../Graphics/Resources/Abstract/Texture2D{T}.cs | 8 +++++--- .../Graphics/Resources/Abstract/Texture3D{T}.cs | 8 +++++--- .../Graphics/Resources/Abstract/TransferBuffer{T}.cs | 5 ++++- .../Graphics/Resources/Abstract/TransferTexture2D{T}.cs | 5 ++++- .../Graphics/Resources/Abstract/TransferTexture3D{T}.cs | 6 +++++- 6 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index ea97671a..b0aeafa0 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -173,11 +173,10 @@ namespace ComputeSharp.Resources this.d3D12Resource.Dispose(); this.allocation.Dispose(); - GraphicsDevice.UnregisterAllocatedResource(); - - if (GraphicsDevice?.IsDisposed == false) + if (GraphicsDevice is GraphicsDevice device) { - GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); + device.UnregisterAllocatedResource(); + device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 74f17096..4ef3b3e7 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -437,9 +437,11 @@ namespace ComputeSharp.Resources this.d3D12Resource.Dispose(); this.allocation.Dispose(); - GraphicsDevice.UnregisterAllocatedResource(); - - GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); + if (GraphicsDevice is GraphicsDevice device) + { + device.UnregisterAllocatedResource(); + device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); + } } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 1248db42..680f7f35 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -480,9 +480,11 @@ namespace ComputeSharp.Resources this.d3D12Resource.Dispose(); this.allocation.Dispose(); - GraphicsDevice.UnregisterAllocatedResource(); - - GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); + if (GraphicsDevice is GraphicsDevice device) + { + device.UnregisterAllocatedResource(); + device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); + } } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs index 7476b25e..75b0199a 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -111,7 +111,10 @@ namespace ComputeSharp.Resources this.d3D12Resource.Dispose(); this.allocation.Dispose(); - GraphicsDevice.UnregisterAllocatedResource(); + if (GraphicsDevice is GraphicsDevice device) + { + device.UnregisterAllocatedResource(); + } } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs index dbabfe4b..462cfcd1 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -121,7 +121,10 @@ namespace ComputeSharp.Resources this.d3D12Resource.Dispose(); this.allocation.Dispose(); - GraphicsDevice.UnregisterAllocatedResource(); + if (GraphicsDevice is GraphicsDevice device) + { + device.UnregisterAllocatedResource(); + } } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs index 1aa16cc1..53fe5ae8 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -95,6 +95,7 @@ namespace ComputeSharp.Resources /// Gets the height of the current texture. /// public int Height => (int)this.d3D12PlacedSubresourceFootprint.Footprint.Height; + /// /// Gets the depth of the current texture. /// @@ -128,7 +129,10 @@ namespace ComputeSharp.Resources this.d3D12Resource.Dispose(); this.allocation.Dispose(); - GraphicsDevice.UnregisterAllocatedResource(); + if (GraphicsDevice is GraphicsDevice device) + { + device.UnregisterAllocatedResource(); + } } ///