From f9c9b5a24b3450db7b819d4da51231a2092ec5f2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 30 Jul 2022 15:57:54 +0200 Subject: [PATCH] Code refactoring to unit tests --- .../DeviceDisposalTests.cs | 73 ++++---------- .../DeviceLostTests.cs | 72 +++----------- .../GetDefaultDeviceTests.cs | 84 ++++++----------- .../Helpers/GraphicsDeviceHelper.cs | 94 +++++++++++++++++++ 4 files changed, 152 insertions(+), 171 deletions(-) create mode 100644 tests/ComputeSharp.Tests.DeviceLost/Helpers/GraphicsDeviceHelper.cs diff --git a/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs b/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs index 6c6b9070..83ce5707 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs +++ b/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs @@ -1,13 +1,12 @@ using System; using ComputeSharp.Interop; using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.DeviceLost.Helpers; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; -#pragma warning disable CA1416 - namespace ComputeSharp.Tests.DeviceLost; [TestClass] @@ -15,48 +14,30 @@ namespace ComputeSharp.Tests.DeviceLost; public partial class DeviceDisposalTests { [TestMethod] - public unsafe void DeviceDisposal_GetDefault_ReferenceCounting() + public void DeviceDisposal_GetDefault_ReferenceCounting() { using ComPtr d3D12Device = default; using (GraphicsDevice graphicsDevice = GraphicsDevice.GetDefault()) { - InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof(), (void**)d3D12Device.GetAddressOf()); + GraphicsDeviceHelper.GetD3D12Device(graphicsDevice, in d3D12Device); } - // This test has to ensure a GraphicsDevice instance correctly destroys the underlying device - // when disposed. To verify this, we first get the underlying ID3D12Device* object, which causes - // its reference count to be incremented by 1. Assuming GraphicsDevice is working correctly, once - // it is disposed, the underlying ID3D12Device* object should have its ref count set to just 1, - // which is the one we added when doing QueryInterface on it. To verify this, we first increment - // that, and then release it to get the updated count. If everything is correct, the ref count - // we got back should be 1, with the last one just being from our own ID3D12Device* local. We - // can't just do Release() and check that, as that'd cause the ref count to just go to 0 but - // without clearing the pointer stored in the ComPtr local, which would then crash when - // going out of scope and trying to dereference that object to call Release() on it. - _ = d3D12Device.Get()->AddRef(); - - uint refCount = d3D12Device.Get()->Release(); - - Assert.AreEqual(refCount, 1u); + Assert.AreEqual(1u, GraphicsDeviceHelper.GetD3D12DeviceRefCount(d3D12Device)); } [CombinatorialTestMethod] [AllDevices] - public unsafe void DeviceDisposal_ReferenceCounting(Device device) + public void DeviceDisposal_ReferenceCounting(Device device) { using ComPtr d3D12Device = default; using (GraphicsDevice graphicsDevice = device.Get()) { - InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof(), (void**)d3D12Device.GetAddressOf()); + GraphicsDeviceHelper.GetD3D12Device(graphicsDevice, in d3D12Device); } - _ = d3D12Device.Get()->AddRef(); - - uint refCount = d3D12Device.Get()->Release(); - - Assert.AreEqual(refCount, 1u); + Assert.AreEqual(1u, GraphicsDeviceHelper.GetD3D12DeviceRefCount(d3D12Device)); } [CombinatorialTestMethod] @@ -75,7 +56,7 @@ public partial class DeviceDisposalTests [CombinatorialTestMethod] [AllDevices] - public unsafe void DeviceDisposal_DisposedResources_ReferenceCounting(Device device) + public void DeviceDisposal_DisposedResources_ReferenceCounting(Device device) { using ComPtr d3D12Device = default; @@ -91,19 +72,15 @@ public partial class DeviceDisposalTests using var r8 = graphicsDevice.AllocateUploadTexture2D(128, 128); using var r9 = graphicsDevice.AllocateReadBackTexture2D(128, 128); - InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof(), (void**)d3D12Device.GetAddressOf()); + GraphicsDeviceHelper.GetD3D12Device(graphicsDevice, in d3D12Device); } - _ = d3D12Device.Get()->AddRef(); - - uint refCount = d3D12Device.Get()->Release(); - - Assert.AreEqual(refCount, 1u); + Assert.AreEqual(1u, GraphicsDeviceHelper.GetD3D12DeviceRefCount(d3D12Device)); } [CombinatorialTestMethod] [AllDevices] - public unsafe void DeviceDisposal_DisposedResourcesAfterDevice_ReferenceCounting(Device device) + public void DeviceDisposal_DisposedResourcesAfterDevice_ReferenceCounting(Device device) { #if NET6_0_OR_GREATER // TODO: investigate ref count with resources disposed after device when using D3D12MA @@ -126,7 +103,7 @@ public partial class DeviceDisposalTests r4 = graphicsDevice.AllocateReadOnlyTexture2D(128, 128); r5 = graphicsDevice.AllocateReadWriteTexture2D(128, 128); - InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof(), (void**)d3D12Device.GetAddressOf()); + GraphicsDeviceHelper.GetD3D12Device(graphicsDevice, in d3D12Device); } r1.Dispose(); @@ -135,16 +112,12 @@ public partial class DeviceDisposalTests r4.Dispose(); r5.Dispose(); - _ = d3D12Device.Get()->AddRef(); - - uint refCount = d3D12Device.Get()->Release(); - - Assert.AreEqual(refCount, 1u); + Assert.AreEqual(1u, GraphicsDeviceHelper.GetD3D12DeviceRefCount(d3D12Device)); } [CombinatorialTestMethod] [AllDevices] - public unsafe void DeviceDisposal_WithComputeShader_ReferenceCounting(Device device) + public void DeviceDisposal_WithComputeShader_ReferenceCounting(Device device) { using ComPtr d3D12Device = default; @@ -157,14 +130,10 @@ public partial class DeviceDisposalTests context.For(buffer.Length, new InitializeShader(buffer)); } - InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof(), (void**)d3D12Device.GetAddressOf()); + GraphicsDeviceHelper.GetD3D12Device(graphicsDevice, in d3D12Device); } - _ = d3D12Device.Get()->AddRef(); - - uint refCount = d3D12Device.Get()->Release(); - - Assert.AreEqual(refCount, 1u); + Assert.AreEqual(1u, GraphicsDeviceHelper.GetD3D12DeviceRefCount(d3D12Device)); } [EmbeddedBytecode(DispatchAxis.X)] @@ -181,7 +150,7 @@ public partial class DeviceDisposalTests [CombinatorialTestMethod] [AllDevices] - public unsafe void DeviceDisposal_WithPixelShader_ReferenceCounting(Device device) + public void DeviceDisposal_WithPixelShader_ReferenceCounting(Device device) { using ComPtr d3D12Device = default; @@ -194,14 +163,10 @@ public partial class DeviceDisposalTests context.ForEach(texture, default(HelloWorldShader)); } - InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof(), (void**)d3D12Device.GetAddressOf()); + GraphicsDeviceHelper.GetD3D12Device(graphicsDevice, in d3D12Device); } - _ = d3D12Device.Get()->AddRef(); - - uint refCount = d3D12Device.Get()->Release(); - - Assert.AreEqual(refCount, 1u); + Assert.AreEqual(1u, GraphicsDeviceHelper.GetD3D12DeviceRefCount(d3D12Device)); } [EmbeddedBytecode(DispatchAxis.XY)] diff --git a/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs b/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs index 0bb4e179..e3077d35 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs +++ b/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs @@ -1,12 +1,9 @@ using System; using System.Threading.Tasks; -using ComputeSharp.Interop; using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.DeviceLost.Helpers; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; -using TerraFX.Interop.DirectX; - -#pragma warning disable CA1416 namespace ComputeSharp.Tests.DeviceLost; @@ -25,7 +22,7 @@ public class DeviceLostTests // Register the device lost callback graphicsDevice.DeviceLost += (s, e) => tcs.SetResult((s, e)); - RemoveDevice(graphicsDevice); + GraphicsDeviceHelper.RemoveDevice(graphicsDevice); // Wait up to a second for the event to be raised (it's raised asynchronously on a thread pool thread) await Task.WhenAny(tcs.Task, Task.Delay(1000)); @@ -50,7 +47,7 @@ public class DeviceLostTests { using GraphicsDevice graphicsDevice = device.Get(); - await RemoveDeviceAsync(graphicsDevice); + await GraphicsDeviceHelper.RemoveDeviceAsync(graphicsDevice); using var _ = graphicsDevice.AllocateBuffer(bufferType, 128); } @@ -64,7 +61,7 @@ public class DeviceLostTests { using GraphicsDevice graphicsDevice = device.Get(); - await RemoveDeviceAsync(graphicsDevice); + await GraphicsDeviceHelper.RemoveDeviceAsync(graphicsDevice); using var _ = graphicsDevice.AllocateTexture2D(textureType, 128, 128); } @@ -78,7 +75,7 @@ public class DeviceLostTests { using GraphicsDevice graphicsDevice = device.Get(); - await RemoveDeviceAsync(graphicsDevice); + await GraphicsDeviceHelper.RemoveDeviceAsync(graphicsDevice); using var _ = graphicsDevice.AllocateTexture3D(textureType, 128, 128, 3); } @@ -90,7 +87,7 @@ public class DeviceLostTests { using GraphicsDevice graphicsDevice = device.Get(); - await RemoveDeviceAsync(graphicsDevice); + await GraphicsDeviceHelper.RemoveDeviceAsync(graphicsDevice); _ = graphicsDevice.IsDoublePrecisionSupportAvailable(); } @@ -102,7 +99,7 @@ public class DeviceLostTests { using GraphicsDevice graphicsDevice = device.Get(); - await RemoveDeviceAsync(graphicsDevice); + await GraphicsDeviceHelper.RemoveDeviceAsync(graphicsDevice); _ = graphicsDevice.IsReadOnlyTexture2DSupportedForType(); } @@ -114,7 +111,7 @@ public class DeviceLostTests { using GraphicsDevice graphicsDevice = device.Get(); - await RemoveDeviceAsync(graphicsDevice); + await GraphicsDeviceHelper.RemoveDeviceAsync(graphicsDevice); _ = graphicsDevice.IsReadWriteTexture2DSupportedForType(); } @@ -126,7 +123,7 @@ public class DeviceLostTests { using GraphicsDevice graphicsDevice = device.Get(); - await RemoveDeviceAsync(graphicsDevice); + await GraphicsDeviceHelper.RemoveDeviceAsync(graphicsDevice); _ = graphicsDevice.IsReadOnlyTexture3DSupportedForType(); } @@ -138,7 +135,7 @@ public class DeviceLostTests { using GraphicsDevice graphicsDevice = device.Get(); - await RemoveDeviceAsync(graphicsDevice); + await GraphicsDeviceHelper.RemoveDeviceAsync(graphicsDevice); _ = graphicsDevice.IsReadWriteTexture3DSupportedForType(); } @@ -150,55 +147,8 @@ public class DeviceLostTests { using GraphicsDevice graphicsDevice = device.Get(); - await RemoveDeviceAsync(graphicsDevice); + await GraphicsDeviceHelper.RemoveDeviceAsync(graphicsDevice); using var _ = graphicsDevice.CreateComputeContext(); } - - /// - /// Removes the underlying device for a given instance. - /// - /// The target instance. - private static unsafe void RemoveDevice(GraphicsDevice graphicsDevice) - { - ID3D12Device5* d3D12Device = default; - Guid d3D12Device5Guid = typeof(ID3D12Device5).GUID; - - if (InteropServices.TryGetID3D12Device(graphicsDevice, &d3D12Device5Guid, (void**)&d3D12Device) != 0) - { - Assert.Inconclusive(); - } - - try - { - d3D12Device->RemoveDevice(); - } - finally - { - if (d3D12Device is not null) - { - d3D12Device->Release(); - } - } - } - - /// - /// Removes the underlying device for a given instance and waits for it to be reported. - /// - /// The target instance. - internal static async Task RemoveDeviceAsync(GraphicsDevice graphicsDevice) - { - TaskCompletionSource tcs = new(); - - graphicsDevice.DeviceLost += (s, e) => tcs.SetResult(null); - - RemoveDevice(graphicsDevice); - - await Task.WhenAny(tcs.Task, Task.Delay(1000)); - - if (!tcs.Task.IsCompleted) - { - Assert.Fail(); - } - } } diff --git a/tests/ComputeSharp.Tests.DeviceLost/GetDefaultDeviceTests.cs b/tests/ComputeSharp.Tests.DeviceLost/GetDefaultDeviceTests.cs index bc053f14..c1071b4c 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/GetDefaultDeviceTests.cs +++ b/tests/ComputeSharp.Tests.DeviceLost/GetDefaultDeviceTests.cs @@ -1,7 +1,5 @@ using System; -using System.Runtime.CompilerServices; -using System.Threading.Tasks; -using ComputeSharp.Interop; +using ComputeSharp.Tests.DeviceLost.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -13,7 +11,7 @@ namespace ComputeSharp.Tests.DeviceLost; public class GetDefaultDeviceTests { [TestMethod] - public async Task GetDefault_VerifyContract() + public unsafe void GetDefault_VerifyContract() { GraphicsDevice device1 = GraphicsDevice.GetDefault(); @@ -22,12 +20,7 @@ public class GetDefaultDeviceTests using (ComPtr d3D12Device = default) { - unsafe void GetNativeDevice(in ComPtr d3D12Device) - { - InteropServices.GetID3D12Device(device1, Windows.__uuidof(), (void**)d3D12Device.GetAddressOf()); - } - - GetNativeDevice(in d3D12Device); + GraphicsDeviceHelper.GetD3D12Device(device1, in d3D12Device); GraphicsDevice device2 = GraphicsDevice.GetDefault(); @@ -35,7 +28,7 @@ public class GetDefaultDeviceTests // GraphicsDevice.GetDefault() again will always return the same cached instance. Assert.AreSame(device1, device2); - await DeviceLostTests.RemoveDeviceAsync(device1); + GraphicsDeviceHelper.RemoveDevice(device1); // The device is lost but not disposed, so the same instance should be returned yet again GraphicsDevice device3 = GraphicsDevice.GetDefault(); @@ -48,17 +41,8 @@ public class GetDefaultDeviceTests // The device is correctly marked as disposed Assert.ThrowsException(() => device1.IsDoublePrecisionSupportAvailable()); - unsafe void VerifyDisposeForDevice1(ComPtr d3D12Device) - { - _ = d3D12Device.Get()->AddRef(); - - uint refCount = d3D12Device.Get()->Release(); - - // Now there should just be this one last reference left to the native device - Assert.AreEqual(refCount, 1u); - } - - VerifyDisposeForDevice1(d3D12Device); + // Now there should just be this one last reference left to the native device + Assert.AreEqual(1u, GraphicsDeviceHelper.GetD3D12DeviceRefCount(in d3D12Device)); } // Calling GraphicsDevice.GetDefault() after disposing it should always succeed... @@ -71,76 +55,64 @@ public class GetDefaultDeviceTests // The LUID should match though, as the same adapter should be returned Assert.AreEqual(device1.Luid, device4.Luid); - unsafe void GetDefault_VerifyContract_Part2() - { - using ComPtr d3D12Device = default; + GraphicsDeviceHelper.RemoveDevice(device4); - // Increment the reference count on the internal device, causing it to remain alive - InteropServices.GetID3D12Device(device4, Windows.__uuidof(), (void**)d3D12Device.GetAddressOf()); + using (ComPtr d3D12Device = default) + { + GraphicsDeviceHelper.GetD3D12Device(device4, in d3D12Device); device4.Dispose(); - _ = d3D12Device.Get()->AddRef(); - - uint refCount = d3D12Device.Get()->Release(); + // The device is correctly marked as disposed + Assert.ThrowsException(() => device4.IsDoublePrecisionSupportAvailable()); // Sanity check that this is in fact the only reference keeping the device alive - Assert.AreEqual(refCount, 1u); + Assert.AreEqual(1u, GraphicsDeviceHelper.GetD3D12DeviceRefCount(in d3D12Device)); HRESULT removalReason = d3D12Device.Get()->GetDeviceRemovedReason(); // Sanity check that the device is in fact removed - Assert.AreEqual((int)removalReason, DXGI.DXGI_ERROR_DEVICE_REMOVED); + Assert.AreEqual(DXGI.DXGI_ERROR_DEVICE_REMOVED, (int)removalReason); // Calling GraphicsDevice.GetDefault() will now throw, because the native device has not been disposed properly Assert.ThrowsException(() => GraphicsDevice.GetDefault()); } - - await DeviceLostTests.RemoveDeviceAsync(device4); - - GetDefault_VerifyContract_Part2(); } [TestMethod] - public unsafe void GetDefault_DisposeAndUseNewInstance() + public void GetDefault_DisposeAndUseNewInstance() { + GraphicsDevice device1 = GraphicsDevice.GetDefault(); + using (ComPtr d3D12Device = default) { - GraphicsDevice device = GraphicsDevice.GetDefault(); - - using (device.AllocateReadOnlyBuffer(128)) + using (device1.AllocateReadOnlyBuffer(128)) { } - InteropServices.GetID3D12Device(device, Windows.__uuidof(), (void**)d3D12Device.GetAddressOf()); + GraphicsDeviceHelper.GetD3D12Device(device1, in d3D12Device); - device.Dispose(); + device1.Dispose(); - _ = d3D12Device.Get()->AddRef(); - - uint refCount = d3D12Device.Get()->Release(); - - Assert.AreEqual(refCount, 1u); + Assert.AreEqual(1u, GraphicsDeviceHelper.GetD3D12DeviceRefCount(in d3D12Device)); } // Just same test but twice, just for good measure using (ComPtr d3D12Device = default) { - GraphicsDevice device = GraphicsDevice.GetDefault(); + GraphicsDevice device2 = GraphicsDevice.GetDefault(); - using (device.AllocateReadOnlyBuffer(128)) + Assert.AreNotSame(device1, device2); + + using (device2.AllocateReadOnlyBuffer(128)) { } - InteropServices.GetID3D12Device(device, Windows.__uuidof(), (void**)d3D12Device.GetAddressOf()); + GraphicsDeviceHelper.GetD3D12Device(device2, in d3D12Device); - device.Dispose(); + device2.Dispose(); - _ = d3D12Device.Get()->AddRef(); - - uint refCount = d3D12Device.Get()->Release(); - - Assert.AreEqual(refCount, 1u); + Assert.AreEqual(1u, GraphicsDeviceHelper.GetD3D12DeviceRefCount(in d3D12Device)); } } } diff --git a/tests/ComputeSharp.Tests.DeviceLost/Helpers/GraphicsDeviceHelper.cs b/tests/ComputeSharp.Tests.DeviceLost/Helpers/GraphicsDeviceHelper.cs new file mode 100644 index 00000000..017d1118 --- /dev/null +++ b/tests/ComputeSharp.Tests.DeviceLost/Helpers/GraphicsDeviceHelper.cs @@ -0,0 +1,94 @@ +using System; +using System.Threading.Tasks; +using ComputeSharp.Interop; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; + +#pragma warning disable CA1416 + +namespace ComputeSharp.Tests.DeviceLost.Helpers; + +/// +/// A helper to do common checks on native device objects. +/// +internal static class GraphicsDeviceHelper +{ + /// + /// Gets the underlying object from a instance. + /// + /// The input instance. + /// The underlying object for . + public static unsafe void GetD3D12Device(GraphicsDevice graphicsDevice, in ComPtr d3D12Device) + { + InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof(), (void**)d3D12Device.GetAddressOf()); + } + + /// + /// Gets the reference count for a given object. + /// + /// The target object to get the reference count for. + public static unsafe uint GetD3D12DeviceRefCount(in ComPtr d3D12Device) + { + // Tests using this helper have to ensure a GraphicsDevice instance correctly destroys the underlying + // device when disposed. To verify this, we first get the underlying ID3D12Device* object, which causes + // its reference count to be incremented by 1. Assuming GraphicsDevice is working correctly, once + // it is disposed, the underlying ID3D12Device* object should have its ref count set to just 1, + // which is the one we added when doing QueryInterface on it. To verify this, we first increment + // that, and then release it to get the updated count. If everything is correct, the ref count + // we got back should be 1, with the last one just being from our own ID3D12Device* local. We + // can't just do Release() and check that, as that'd cause the ref count to just go to 0 but + // without clearing the pointer stored in the ComPtr local, which would then crash when + // going out of scope and trying to dereference that object to call Release() on it. + _ = d3D12Device.Get()->AddRef(); + + return d3D12Device.Get()->Release(); + } + + /// + /// Removes the underlying device for a given instance. + /// + /// The target instance. + public static unsafe void RemoveDevice(GraphicsDevice graphicsDevice) + { + ID3D12Device5* d3D12Device = default; + Guid d3D12Device5Guid = typeof(ID3D12Device5).GUID; + + if (InteropServices.TryGetID3D12Device(graphicsDevice, &d3D12Device5Guid, (void**)&d3D12Device) != 0) + { + Assert.Inconclusive(); + } + + try + { + d3D12Device->RemoveDevice(); + } + finally + { + if (d3D12Device is not null) + { + d3D12Device->Release(); + } + } + } + + /// + /// Removes the underlying device for a given instance and waits for it to be reported. + /// + /// The target instance. + public static async Task RemoveDeviceAsync(GraphicsDevice graphicsDevice) + { + TaskCompletionSource tcs = new(); + + graphicsDevice.DeviceLost += (s, e) => tcs.SetResult(null); + + RemoveDevice(graphicsDevice); + + await Task.WhenAny(tcs.Task, Task.Delay(1000)); + + if (!tcs.Task.IsCompleted) + { + Assert.Fail(); + } + } +}