diff --git a/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj b/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj index 89272596..cc49bdf9 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj +++ b/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj @@ -4,6 +4,11 @@ AnyCPU;x64;ARM64 + + + $(DefineConstants);USE_D3D12MA + + @@ -16,6 +21,7 @@ + diff --git a/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs b/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs index dcfb7041..9efe9cc2 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs +++ b/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs @@ -1,4 +1,7 @@ using System; +#if USE_D3D12MA +using ComputeSharp.D3D12MemoryAllocator; +#endif using ComputeSharp.Interop; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.DeviceLost.Helpers; @@ -15,6 +18,15 @@ using Win32 = Win32.Apis; [TestCategory("DeviceDisposal")] public partial class DeviceDisposalTests { + [AssemblyInitialize] + public static void ConfigureImageSharp(TestContext _) + { +#if USE_D3D12MA + // If requested by the test runner, configure D3D12MA + AllocationServices.ConfigureAllocatorFactory(new D3D12MemoryAllocatorFactory()); +#endif + } + [TestMethod] public void DeviceDisposal_GetDefault_ReferenceCounting() { diff --git a/tests/ComputeSharp.Tests/AllocationServicesTests.cs b/tests/ComputeSharp.Tests/AllocationServicesTests.cs new file mode 100644 index 00000000..274311fc --- /dev/null +++ b/tests/ComputeSharp.Tests/AllocationServicesTests.cs @@ -0,0 +1,74 @@ +#if NET6_0_OR_GREATER + +using System; +using System.Runtime.InteropServices; +using ComputeSharp.D3D12MemoryAllocator; +using ComputeSharp.Interop; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Win32; + +namespace ComputeSharp.Tests; + +[TestClass] +[TestCategory("AllocationServices")] +public class AllocationServicesTests +{ + [TestMethod] + public unsafe 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"); + + using ComPtr d3D12MemoryAllocatorFactory = default; + + CustomQueryInterfaceResult result = ((ICustomQueryInterface)allocatorFactory).GetInterface( + iid: ref uuidOfID3D12MemoryAllocatorFactory, + ppv: out *(IntPtr*)d3D12MemoryAllocatorFactory.GetAddressOf()); + + 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); + + try + { + // Try to configure the allocator (this should fail) + AllocationServices.ConfigureAllocatorFactory(allocatorFactory); + } + catch (InvalidOperationException) + { + // This is expected + } + catch + { + Assert.Fail(); + } + + _ = d3D12MemoryAllocatorFactory.Get()->AddRef(); + + referenceCount = d3D12MemoryAllocatorFactory.Get()->Release(); + + // No additional references should've been added + Assert.AreEqual(2u, referenceCount); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException))] + public void ConfigureAllocationFactory_ValidatesArgument() + { + AllocationServices.ConfigureAllocatorFactory(null!); + } +} + +#endif \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj index d4b67d61..6a706a74 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -4,6 +4,11 @@ AnyCPU;x64;ARM64 + + + $(DefineConstants);USE_D3D12MA + + @@ -17,6 +22,7 @@ + diff --git a/tests/ComputeSharp.Tests/ImagingTests.cs b/tests/ComputeSharp.Tests/ImagingTests.cs index 7008bbcb..72a62e51 100644 --- a/tests/ComputeSharp.Tests/ImagingTests.cs +++ b/tests/ComputeSharp.Tests/ImagingTests.cs @@ -3,6 +3,10 @@ using System.Buffers; using System.IO; using System.Reflection; using CommunityToolkit.HighPerformance.Buffers; +#if USE_D3D12MA +using ComputeSharp.D3D12MemoryAllocator; +using ComputeSharp.Interop; +#endif using ComputeSharp.Resources; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; @@ -23,6 +27,11 @@ public class ImagingTests public static void ConfigureImageSharp(TestContext _) { Configuration.Default.PreferContiguousImageBuffers = true; + +#if USE_D3D12MA + // If requested by the test runner, configure D3D12MA + AllocationServices.ConfigureAllocatorFactory(new D3D12MemoryAllocatorFactory()); +#endif } [CombinatorialTestMethod]