Code refactoring to unit tests

このコミットが含まれているのは:
Sergio Pedri committed 2022-07-31 23:26:14 +02:00
1 親 358f89adb1
コミット f9c9b5a24b
4ファイル変更
+152 -171

検索条件に一致するファイルはありません

+19 -54
ファイルの表示
@@ -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<ID3D12Device> d3D12Device = default;
using (GraphicsDevice graphicsDevice = GraphicsDevice.GetDefault())
{
InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof<ID3D12Device>(), (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<T> 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<ID3D12Device> d3D12Device = default;
using (GraphicsDevice graphicsDevice = device.Get())
{
InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof<ID3D12Device>(), (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<ID3D12Device> d3D12Device = default;
@@ -91,19 +72,15 @@ public partial class DeviceDisposalTests
using var r8 = graphicsDevice.AllocateUploadTexture2D<float>(128, 128);
using var r9 = graphicsDevice.AllocateReadBackTexture2D<float>(128, 128);
InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof<ID3D12Device>(), (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<float>(128, 128);
r5 = graphicsDevice.AllocateReadWriteTexture2D<float>(128, 128);
InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof<ID3D12Device>(), (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<ID3D12Device> d3D12Device = default;
@@ -157,14 +130,10 @@ public partial class DeviceDisposalTests
context.For(buffer.Length, new InitializeShader(buffer));
}
InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof<ID3D12Device>(), (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<ID3D12Device> d3D12Device = default;
@@ -194,14 +163,10 @@ public partial class DeviceDisposalTests
context.ForEach(texture, default(HelloWorldShader));
}
InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof<ID3D12Device>(), (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)]
+11 -61
ファイルの表示
@@ -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<float>(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<float>(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<float>(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<float>();
}
@@ -114,7 +111,7 @@ public class DeviceLostTests
{
using GraphicsDevice graphicsDevice = device.Get();
await RemoveDeviceAsync(graphicsDevice);
await GraphicsDeviceHelper.RemoveDeviceAsync(graphicsDevice);
_ = graphicsDevice.IsReadWriteTexture2DSupportedForType<float>();
}
@@ -126,7 +123,7 @@ public class DeviceLostTests
{
using GraphicsDevice graphicsDevice = device.Get();
await RemoveDeviceAsync(graphicsDevice);
await GraphicsDeviceHelper.RemoveDeviceAsync(graphicsDevice);
_ = graphicsDevice.IsReadOnlyTexture3DSupportedForType<float>();
}
@@ -138,7 +135,7 @@ public class DeviceLostTests
{
using GraphicsDevice graphicsDevice = device.Get();
await RemoveDeviceAsync(graphicsDevice);
await GraphicsDeviceHelper.RemoveDeviceAsync(graphicsDevice);
_ = graphicsDevice.IsReadWriteTexture3DSupportedForType<float>();
}
@@ -150,55 +147,8 @@ public class DeviceLostTests
{
using GraphicsDevice graphicsDevice = device.Get();
await RemoveDeviceAsync(graphicsDevice);
await GraphicsDeviceHelper.RemoveDeviceAsync(graphicsDevice);
using var _ = graphicsDevice.CreateComputeContext();
}
/// <summary>
/// Removes the underlying device for a given <see cref="GraphicsDevice"/> instance.
/// </summary>
/// <param name="graphicsDevice">The target <see cref="GraphicsDevice"/> instance.</param>
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();
}
}
}
/// <summary>
/// Removes the underlying device for a given <see cref="GraphicsDevice"/> instance and waits for it to be reported.
/// </summary>
/// <param name="graphicsDevice">The target <see cref="GraphicsDevice"/> instance.</param>
internal static async Task RemoveDeviceAsync(GraphicsDevice graphicsDevice)
{
TaskCompletionSource<object?> 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();
}
}
}
+28 -56
ファイルの表示
@@ -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<ID3D12Device> d3D12Device = default)
{
unsafe void GetNativeDevice(in ComPtr<ID3D12Device> d3D12Device)
{
InteropServices.GetID3D12Device(device1, Windows.__uuidof<ID3D12Device>(), (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<ObjectDisposedException>(() => device1.IsDoublePrecisionSupportAvailable());
unsafe void VerifyDisposeForDevice1(ComPtr<ID3D12Device> 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<ID3D12Device> d3D12Device = default;
GraphicsDeviceHelper.RemoveDevice(device4);
// Increment the reference count on the internal device, causing it to remain alive
InteropServices.GetID3D12Device(device4, Windows.__uuidof<ID3D12Device>(), (void**)d3D12Device.GetAddressOf());
using (ComPtr<ID3D12Device> 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<ObjectDisposedException>(() => 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<InvalidOperationException>(() => 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<ID3D12Device> d3D12Device = default)
{
GraphicsDevice device = GraphicsDevice.GetDefault();
using (device.AllocateReadOnlyBuffer<float>(128))
using (device1.AllocateReadOnlyBuffer<float>(128))
{
}
InteropServices.GetID3D12Device(device, Windows.__uuidof<ID3D12Device>(), (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<ID3D12Device> d3D12Device = default)
{
GraphicsDevice device = GraphicsDevice.GetDefault();
GraphicsDevice device2 = GraphicsDevice.GetDefault();
using (device.AllocateReadOnlyBuffer<float>(128))
Assert.AreNotSame(device1, device2);
using (device2.AllocateReadOnlyBuffer<float>(128))
{
}
InteropServices.GetID3D12Device(device, Windows.__uuidof<ID3D12Device>(), (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));
}
}
}
+94
ファイルの表示
@@ -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;
/// <summary>
/// A helper to do common checks on native device objects.
/// </summary>
internal static class GraphicsDeviceHelper
{
/// <summary>
/// Gets the underlying <see cref="ID3D12Device"/> object from a <see cref="GraphicsDevice"/> instance.
/// </summary>
/// <param name="graphicsDevice">The input <see cref="GraphicsDevice"/> instance.</param>
/// <param name="d3D12Device">The underlying <see cref="ID3D12Device"/> object for <paramref name="graphicsDevice"/>.</param>
public static unsafe void GetD3D12Device(GraphicsDevice graphicsDevice, in ComPtr<ID3D12Device> d3D12Device)
{
InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof<ID3D12Device>(), (void**)d3D12Device.GetAddressOf());
}
/// <summary>
/// Gets the reference count for a given <see cref="ID3D12Device"/> object.
/// </summary>
/// <param name="d3D12Device">The target <see cref="ID3D12Device"/> object to get the reference count for.</param>
public static unsafe uint GetD3D12DeviceRefCount(in ComPtr<ID3D12Device> 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<T> 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();
}
/// <summary>
/// Removes the underlying device for a given <see cref="GraphicsDevice"/> instance.
/// </summary>
/// <param name="graphicsDevice">The target <see cref="GraphicsDevice"/> instance.</param>
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();
}
}
}
/// <summary>
/// Removes the underlying device for a given <see cref="GraphicsDevice"/> instance and waits for it to be reported.
/// </summary>
/// <param name="graphicsDevice">The target <see cref="GraphicsDevice"/> instance.</param>
public static async Task RemoveDeviceAsync(GraphicsDevice graphicsDevice)
{
TaskCompletionSource<object?> 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();
}
}
}