From df970a5e40105209ea786296d13b4d097116e7c7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 17 Mar 2021 17:27:48 +0100 Subject: [PATCH] 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)