diff --git a/src/ComputeSharp.D3D12MemoryAllocator/D3D12MemoryAllocatorFactory.cs b/src/ComputeSharp.D3D12MemoryAllocator/D3D12MemoryAllocatorFactory.cs
index 7c95646d..24343f83 100644
--- a/src/ComputeSharp.D3D12MemoryAllocator/D3D12MemoryAllocatorFactory.cs
+++ b/src/ComputeSharp.D3D12MemoryAllocator/D3D12MemoryAllocatorFactory.cs
@@ -1,26 +1,70 @@
+using System;
+using System.Runtime.InteropServices;
using ComputeSharp.Core.Extensions;
using ComputeSharp.D3D12MemoryAllocator.Interop;
+using TerraFX.Interop.Windows;
namespace ComputeSharp.D3D12MemoryAllocator;
///
/// The entry point to obtain ID3D12MemoryAllocatorFactory instances using D3D12MA.
///
-public static unsafe class D3D12MemoryAllocatorFactory
+public sealed unsafe class D3D12MemoryAllocatorFactory : ICustomQueryInterface
{
///
- /// Creates a new ID3D12MemoryAllocatorFactory instance for D3D12MA.
+ /// The object wrapped by the current instance.
+ ///
+ private readonly ID3D12MemoryAllocatorFactoryImpl* allocatorFactory;
+
+ ///
+ /// Creates a new instance (implementing ID3D12MemoryAllocatorFactory).
///
- /// The resulting ID3D12MemoryAllocatorFactory instance for D3D12MA.
///
- /// This method should only be used to provide an argument for .
+ /// The resulting object should only be used to provide an argument for .
///
- public static void* CreateInstance()
+ public D3D12MemoryAllocatorFactory()
{
- ID3D12MemoryAllocatorFactoryImpl* allocatorFactory = null;
+ fixed (ID3D12MemoryAllocatorFactoryImpl** allocatorFactory = &this.allocatorFactory)
+ {
+ ID3D12MemoryAllocatorFactoryImpl.Factory(allocatorFactory).Assert();
+ }
+ }
- ID3D12MemoryAllocatorFactoryImpl.Factory(&allocatorFactory).Assert();
+ ///
+ /// Releases the underlying ID3D12MemoryAllocatorFactory object.
+ ///
+ ~D3D12MemoryAllocatorFactory()
+ {
+ if (this.allocatorFactory is not null)
+ {
+ _ = this.allocatorFactory->Release();
+ }
+ }
- return allocatorFactory;
+ ///
+ CustomQueryInterfaceResult ICustomQueryInterface.GetInterface(ref Guid iid, out IntPtr ppv)
+ {
+ // Handle invalid cases where the object was not constructed correctly.
+ // Practically speaking, this should never happen in a real world scenario.
+ if (this.allocatorFactory is null)
+ {
+ ppv = default;
+
+ return CustomQueryInterfaceResult.Failed;
+ }
+
+ fixed (Guid* riid = &iid)
+ fixed (IntPtr* ppResult = &ppv)
+ {
+ int hresult = this.allocatorFactory->QueryInterface(riid, (void**)ppResult);
+
+ GC.KeepAlive(this);
+
+ return hresult switch
+ {
+ S.S_OK => CustomQueryInterfaceResult.Handled,
+ _ => CustomQueryInterfaceResult.Failed
+ };
+ }
}
}
diff --git a/src/ComputeSharp.D3D12MemoryAllocator/Interop/ID3D12MemoryAllocatorFactoryImpl.cs b/src/ComputeSharp.D3D12MemoryAllocator/Interop/ID3D12MemoryAllocatorFactoryImpl.cs
index 4c37a045..2795efa4 100644
--- a/src/ComputeSharp.D3D12MemoryAllocator/Interop/ID3D12MemoryAllocatorFactoryImpl.cs
+++ b/src/ComputeSharp.D3D12MemoryAllocator/Interop/ID3D12MemoryAllocatorFactoryImpl.cs
@@ -76,8 +76,8 @@ internal unsafe struct ID3D12MemoryAllocatorFactoryImpl
}
///
- [UnmanagedCallersOnly]
- private static int QueryInterface(ID3D12MemoryAllocatorFactoryImpl* @this, Guid* riid, void** ppvObject)
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public int QueryInterface(Guid* riid, void** ppvObject)
{
if (ppvObject is null)
{
@@ -87,9 +87,9 @@ internal unsafe struct ID3D12MemoryAllocatorFactoryImpl
if (riid->Equals(Windows.__uuidof()) ||
riid->Equals(ID3D12MemoryAllocatorFactory.Guid))
{
- _ = Interlocked.Increment(ref @this->referenceCount);
+ _ = Interlocked.Increment(ref this.referenceCount);
- *ppvObject = @this;
+ *ppvObject = Unsafe.AsPointer(ref this);
return S.S_OK;
}
@@ -99,6 +99,27 @@ internal unsafe struct ID3D12MemoryAllocatorFactoryImpl
return E.E_NOINTERFACE;
}
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public uint Release()
+ {
+ uint referenceCount = (uint)Interlocked.Decrement(ref this.referenceCount);
+
+ if (referenceCount == 0)
+ {
+ NativeMemory.Free(Unsafe.AsPointer(ref this));
+ }
+
+ return referenceCount;
+ }
+
+ ///
+ [UnmanagedCallersOnly]
+ private static int QueryInterface(ID3D12MemoryAllocatorFactoryImpl* @this, Guid* riid, void** ppvObject)
+ {
+ return @this->QueryInterface(riid, ppvObject);
+ }
+
///
[UnmanagedCallersOnly]
private static uint AddRef(ID3D12MemoryAllocatorFactoryImpl* @this)
@@ -110,14 +131,7 @@ internal unsafe struct ID3D12MemoryAllocatorFactoryImpl
[UnmanagedCallersOnly]
private static uint Release(ID3D12MemoryAllocatorFactoryImpl* @this)
{
- uint referenceCount = (uint)Interlocked.Decrement(ref @this->referenceCount);
-
- if (referenceCount == 0)
- {
- NativeMemory.Free(@this);
- }
-
- return referenceCount;
+ return @this->Release();
}
///
diff --git a/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocatorFactory.cs b/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocatorFactory.cs
index bc06b6b2..7e903686 100644
--- a/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocatorFactory.cs
+++ b/src/ComputeSharp/Graphics/Interop/Allocation/ID3D12MemoryAllocatorFactory.cs
@@ -1,6 +1,9 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
+#if NET6_0_OR_GREATER
+using TerraFX.Interop;
+#endif
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
@@ -12,7 +15,7 @@ namespace ComputeSharp.Interop.Allocation;
/// A factory type for objects.
///
[Guid("CC1E74A7-786D-40F4-8AE2-F8B7A255587E")]
-internal unsafe struct ID3D12MemoryAllocatorFactory
+internal unsafe struct ID3D12MemoryAllocatorFactory : IUnknown.Interface
{
///
/// Gets the for (CC1E74A7-786D-40F4-8AE2-F8B7A255587E).
@@ -38,11 +41,37 @@ internal unsafe struct ID3D12MemoryAllocatorFactory
}
}
+#if NET6_0_OR_GREATER
+ ///
+ static Guid* INativeGuid.NativeGuid => (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in Guid));
+#endif
+
///
/// The vtable for the current instance.
///
private readonly void** lpVtbl;
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public HRESULT QueryInterface(Guid* riid, void** ppvObject)
+ {
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[0])((ID3D12MemoryAllocatorFactory*)Unsafe.AsPointer(ref this), riid, ppvObject);
+ }
+
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public uint AddRef()
+ {
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[1])((ID3D12MemoryAllocatorFactory*)Unsafe.AsPointer(ref this));
+ }
+
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public uint Release()
+ {
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[2])((ID3D12MemoryAllocatorFactory*)Unsafe.AsPointer(ref this));
+ }
+
///
/// Creates a new for a given device.
///
diff --git a/src/ComputeSharp/Graphics/Interop/AllocationServices.cs b/src/ComputeSharp/Graphics/Interop/AllocationServices.cs
index cb43fb1a..4e1d3b6f 100644
--- a/src/ComputeSharp/Graphics/Interop/AllocationServices.cs
+++ b/src/ComputeSharp/Graphics/Interop/AllocationServices.cs
@@ -1,6 +1,8 @@
using System;
+using System.Runtime.InteropServices;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Interop.Allocation;
+using TerraFX.Interop.Windows;
namespace ComputeSharp.Interop;
@@ -12,16 +14,25 @@ public static unsafe class AllocationServices
///
/// Configures the global ID3D12MemoryAllocatorFactory instance to use for the current process.
///
- /// The input ID3D12MemoryAllocatorFactory instance to use.
+ /// The input instance to use (implementing ID3D12MemoryAllocatorFactory).
/// Thrown if is .
/// Thrown if the allocator factory cannot be configured at this time.
///
/// The allocatory factory can only be configured once, and before any instance has been created.
///
- public static void ConfigureAllocatorFactory(void* allocatorFactory)
+ public static void ConfigureAllocatorFactory(ICustomQueryInterface allocatorFactory)
{
default(ArgumentNullException).ThrowIfNull(allocatorFactory);
- DeviceHelper.ConfigureAllocatorFactory((ID3D12MemoryAllocatorFactory*)allocatorFactory);
+ using ComPtr allocatorFactoryAbi = default;
+
+ // Unwrap the underlying ID3D12MemoryAllocatorFactory object
+ CustomQueryInterfaceResult result = allocatorFactory.GetInterface(
+ iid: ref *(Guid*)Windows.__uuidof(),
+ ppv: out *(IntPtr*)allocatorFactoryAbi.GetAddressOf());
+
+ default(InvalidOperationException).ThrowIf(result != CustomQueryInterfaceResult.Handled, "Failed to retrieve the underlying ID3D12MemoryAllocatorFactory object.");
+
+ DeviceHelper.ConfigureAllocatorFactory(allocatorFactoryAbi.Get());
}
}
\ No newline at end of file