diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.GetDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.GetDevice.cs index 5d9a5c6b..5df13dbd 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.GetDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.GetDevice.cs @@ -21,13 +21,13 @@ partial class GraphicsDevice /// support device lost scenarios (see ) and due to how the DirectX 12 /// runtime handles caching of device instances, this method has the following behavior: /// - /// If no device has been created, create the default one, cache it, and return it. - /// If a cached device is available, return that. - /// If the returned device is disposed, the cache is reset. - /// - /// If is called again after that, the returned device might map to a different adapter. - /// This would be the case if the first device was lost due to the adapter being removed from the system. - /// + /// If no device has been created, create the default one, cache it, and return it. + /// If a cached device is available, return that. + /// If the returned device is disposed, the cache is reset. + /// + /// If is called again after that, the returned device might map to a different adapter. + /// This would be the case if the first device was lost due to the adapter being removed from the system. + /// /// /// There is one additional caveat to the list above: if is called, then that device is lost, then /// the returned instance is disposed incorrectly (eg. with some existing resources not being diff --git a/tests/ComputeSharp.Tests/AllocationServicesTests.cs b/tests/ComputeSharp.Tests/AllocationServicesTests.cs index dc873efb..e5f0f074 100644 --- a/tests/ComputeSharp.Tests/AllocationServicesTests.cs +++ b/tests/ComputeSharp.Tests/AllocationServicesTests.cs @@ -1,27 +1,26 @@ #if USE_D3D12MA using System; +using System.Reflection; using System.Runtime.InteropServices; +using CommunityToolkit.HighPerformance.Helpers; using ComputeSharp.D3D12MemoryAllocator; using ComputeSharp.Interop; +using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; using Win32; +using HRESULT = Win32.HResult; namespace ComputeSharp.Tests; [TestClass] [TestCategory("AllocationServices")] -public class AllocationServicesTests +public unsafe class AllocationServicesTests { [TestMethod] - public unsafe void ConfigureAllocationFactory_CorrectLogic() + public void ConfigureAllocationFactory_CorrectLogic() { -#if !USE_D3D12MA - // Ensure a device has been created already (if D3D12MA is set, this isn't needed, because - // the global assembly initializer will have always configured D3D12MA for the process). - _ = GraphicsDevice.GetDefault(); -#endif - D3D12MemoryAllocatorFactory allocatorFactory = new(); Guid uuidOfID3D12MemoryAllocatorFactory = new("CC1E74A7-786D-40F4-8AE2-F8B7A255587E"); @@ -34,12 +33,8 @@ public class AllocationServicesTests Assert.AreEqual(CustomQueryInterfaceResult.Handled, result); Assert.IsTrue(d3D12MemoryAllocatorFactory.Get() is not null); - _ = d3D12MemoryAllocatorFactory.Get()->AddRef(); - - uint referenceCount = d3D12MemoryAllocatorFactory.Get()->Release(); - // There's two references: one from the managed object, one from the local ComPtr - Assert.AreEqual(2u, referenceCount); + Assert.AreEqual(2u, d3D12MemoryAllocatorFactory.GetReferenceCount()); try { @@ -55,12 +50,8 @@ public class AllocationServicesTests Assert.Fail(); } - _ = d3D12MemoryAllocatorFactory.Get()->AddRef(); - - referenceCount = d3D12MemoryAllocatorFactory.Get()->Release(); - // No additional references should've been added - Assert.AreEqual(2u, referenceCount); + Assert.AreEqual(2u, d3D12MemoryAllocatorFactory.GetReferenceCount()); } [TestMethod] @@ -69,6 +60,119 @@ public class AllocationServicesTests { AllocationServices.ConfigureAllocatorFactory(null!); } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ConstantBuffer<>))] + [Resource(typeof(ReadOnlyBuffer<>))] + [Resource(typeof(ReadWriteBuffer<>))] + public void ValidateAllocatorFunctionalityWithBuffer(Device device, Type bufferType) + { + ValidateAllocationState(device.Get().AllocateBuffer(bufferType, 128)); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<>))] + [Resource(typeof(ReadWriteTexture2D<>))] + public void ValidateAllocatorFunctionalityWithTexture2D(Device device, Type bufferType) + { + ValidateAllocationState(device.Get().AllocateTexture2D(bufferType, 16, 16)); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + public void ValidateAllocatorFunctionalityWithTexture3D(Device device, Type bufferType) + { + ValidateAllocationState(device.Get().AllocateTexture3D(bufferType, 16, 16, 4)); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadBuffer<>))] + [Resource(typeof(ReadBackBuffer<>))] + public void ValidateAllocatorFunctionalityWithTransferBuffer(Device device, Type bufferType) + { + ValidateAllocationState(device.Get().AllocateTransferBuffer(bufferType, 128)); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadTexture2D<>))] + [Resource(typeof(ReadBackTexture2D<>))] + public void ValidateAllocatorFunctionalityWithTransferTexture2D(Device device, Type bufferType) + { + ValidateAllocationState(device.Get().AllocateTransferTexture2D(bufferType, 16, 16)); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadTexture3D<>))] + [Resource(typeof(ReadBackTexture3D<>))] + public void ValidateAllocatorFunctionalityWithTransferTexture3D(Device device, Type bufferType) + { + ValidateAllocationState(device.Get().AllocateTransferTexture3D(bufferType, 16, 16, 4)); + } + + private static void ValidateAllocationState(IGraphicsResource resource) + { + using ComPtr allocationPtr = default; + + Type? instanceType = resource.GetType(); + FieldInfo? allocationFieldInfo = null; + FieldInfo? d3D12ResourceFieldInfo = null; + + // Get the allocation field (might be in a base type) + do + { + allocationFieldInfo = instanceType?.GetField("allocation", BindingFlags.Instance | BindingFlags.NonPublic); + d3D12ResourceFieldInfo = instanceType?.GetField("d3D12Resource", BindingFlags.Instance | BindingFlags.NonPublic); + + instanceType = instanceType?.BaseType; + } + while (allocationFieldInfo is null && instanceType is not null); + + Assert.IsNotNull(allocationFieldInfo); + Assert.IsNotNull(d3D12ResourceFieldInfo); + + // Get the allocation and D3D12 resource pointers (we use reflection as they're private fields) + object allocation = allocationFieldInfo.GetValue(resource)!; + object d3D12Resource = d3D12ResourceFieldInfo.GetValue(resource)!; + + *&allocationPtr = new ComPtr((IUnknown*)ObjectMarshal.DangerousGetObjectDataReferenceAt(allocation, default)); + + using ComPtr d3D12ResourcePtr = new((IUnknown*)ObjectMarshal.DangerousGetObjectDataReferenceAt(d3D12Resource, default)); + + Assert.AreNotEqual(0, (nint)allocationPtr.Get()); + Assert.AreNotEqual(0, (nint)d3D12ResourcePtr.Get()); + + using ComPtr wrappedD3D12ResourcePtr = default; + + // Invoke ID3D12Allocation::GetD3D12Resource (manually, as the type definition is internal) + HRESULT hresult = ((delegate* unmanaged[Stdcall])(*(void***)allocationPtr.Get())[3])( + allocationPtr.Get(), + wrappedD3D12ResourcePtr.GetAddressOf()); + + Assert.IsTrue(hresult.Success); + + using ComPtr d3D12ResourceUnknown = default; + using ComPtr wrappedD3D12ResourceUnknown = default; + + // Compare the identity of the D3D12 resource from the managed resource, and the one from the allocation + Assert.IsTrue(d3D12ResourcePtr.CopyTo(d3D12ResourceUnknown.GetAddressOf()).Success); + Assert.IsTrue(wrappedD3D12ResourcePtr.CopyTo(wrappedD3D12ResourceUnknown.GetAddressOf()).Success); + Assert.AreEqual((nint)d3D12ResourceUnknown.Get(), (nint)wrappedD3D12ResourceUnknown.Get()); + + // The allocation is kept alive by the resource at this point + Assert.IsTrue(allocationPtr.GetReferenceCount() > 1); + + ((IDisposable)resource).Dispose(); + + // The local reference is the only thing keeping the allocation alive at this point + Assert.AreEqual(1u, allocationPtr.GetReferenceCount()); + } } #endif \ No newline at end of file