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) { fixed (ID3D12Device** ppvObject = d3D12Device) { InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof(), (void**)ppvObject); } } /// /// 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. /// /// Removing a device will cause the device removed callback to be invoked on a separate thread, to get a /// reference tracking lease for the device and then raise the event. If this is not awaited, it means /// that that callback might race against a thread just calling Dispose(), so that that thread would not /// actually cause the object to release resources until the device removed callback has also released its /// own lease. To avoid issues related to this, the event should always be awaited after removing a device. /// public static async Task RemoveDeviceAsync(GraphicsDevice graphicsDevice) { TaskCompletionSource tcs = new(); graphicsDevice.DeviceLost += (s, e) => tcs.SetResult(null); RemoveDevice(graphicsDevice); await tcs.Task; } }