Switch allocator factories to ICustomQueryInterface

このコミットが含まれているのは:
Sergio Pedri committed 2023-07-01 21:18:48 +02:00
1 親 8806945042
コミット 00b841b33e
4ファイル変更
+122 -24

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

+52 -8
ファイルの表示
@@ -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;
/// <summary>
/// The entry point to obtain <c>ID3D12MemoryAllocatorFactory</c> instances using <see href="https://gpuopen.com/d3d12-memory-allocator/">D3D12MA</see>.
/// </summary>
public static unsafe class D3D12MemoryAllocatorFactory
public sealed unsafe class D3D12MemoryAllocatorFactory : ICustomQueryInterface
{
/// <summary>
/// Creates a new <c>ID3D12MemoryAllocatorFactory</c> instance for D3D12MA.
/// The <see cref="ID3D12MemoryAllocatorFactoryImpl"/> object wrapped by the current instance.
/// </summary>
private readonly ID3D12MemoryAllocatorFactoryImpl* allocatorFactory;
/// <summary>
/// Creates a new <see cref="D3D12MemoryAllocatorFactory"/> instance (implementing <c>ID3D12MemoryAllocatorFactory</c>).
/// </summary>
/// <returns>The resulting <c>ID3D12MemoryAllocatorFactory</c> instance for D3D12MA.</returns>
/// <remarks>
/// This method should only be used to provide an argument for <see cref="ComputeSharp.Interop.AllocationServices.ConfigureAllocatorFactory"/>.
/// The resulting object should only be used to provide an argument for <see cref="ComputeSharp.Interop.AllocationServices.ConfigureAllocatorFactory"/>.
/// </remarks>
public static void* CreateInstance()
public D3D12MemoryAllocatorFactory()
{
ID3D12MemoryAllocatorFactoryImpl* allocatorFactory = null;
fixed (ID3D12MemoryAllocatorFactoryImpl** allocatorFactory = &this.allocatorFactory)
{
ID3D12MemoryAllocatorFactoryImpl.Factory(allocatorFactory).Assert();
}
}
ID3D12MemoryAllocatorFactoryImpl.Factory(&allocatorFactory).Assert();
/// <summary>
/// Releases the underlying <c>ID3D12MemoryAllocatorFactory</c> object.
/// </summary>
~D3D12MemoryAllocatorFactory()
{
if (this.allocatorFactory is not null)
{
_ = this.allocatorFactory->Release();
}
}
return allocatorFactory;
/// <inheritdoc/>
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
};
}
}
}
+26 -12
ファイルの表示
@@ -76,8 +76,8 @@ internal unsafe struct ID3D12MemoryAllocatorFactoryImpl
}
/// <inheritdoc cref="IUnknown.QueryInterface"/>
[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<IUnknown>()) ||
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;
}
/// <inheritdoc cref="IUnknown.Release"/>
[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;
}
/// <inheritdoc cref="IUnknown.QueryInterface"/>
[UnmanagedCallersOnly]
private static int QueryInterface(ID3D12MemoryAllocatorFactoryImpl* @this, Guid* riid, void** ppvObject)
{
return @this->QueryInterface(riid, ppvObject);
}
/// <inheritdoc cref="IUnknown.AddRef"/>
[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();
}
/// <inheritdoc cref="ID3D12MemoryAllocator.AllocateResource"/>
+30 -1
ファイルの表示
@@ -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 <see cref="ID3D12MemoryAllocator"/> objects.
/// </summary>
[Guid("CC1E74A7-786D-40F4-8AE2-F8B7A255587E")]
internal unsafe struct ID3D12MemoryAllocatorFactory
internal unsafe struct ID3D12MemoryAllocatorFactory : IUnknown.Interface
{
/// <summary>
/// Gets the <see cref="System.Guid"/> for <see cref="ID3D12MemoryAllocatorFactory"/> (<c>CC1E74A7-786D-40F4-8AE2-F8B7A255587E</c>).
@@ -38,11 +41,37 @@ internal unsafe struct ID3D12MemoryAllocatorFactory
}
}
#if NET6_0_OR_GREATER
/// <inheritdoc/>
static Guid* INativeGuid.NativeGuid => (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in Guid));
#endif
/// <summary>
/// The vtable for the current instance.
/// </summary>
private readonly void** lpVtbl;
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public HRESULT QueryInterface(Guid* riid, void** ppvObject)
{
return ((delegate* unmanaged[Stdcall]<ID3D12MemoryAllocatorFactory*, Guid*, void**, int>)this.lpVtbl[0])((ID3D12MemoryAllocatorFactory*)Unsafe.AsPointer(ref this), riid, ppvObject);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint AddRef()
{
return ((delegate* unmanaged[Stdcall]<ID3D12MemoryAllocatorFactory*, uint>)this.lpVtbl[1])((ID3D12MemoryAllocatorFactory*)Unsafe.AsPointer(ref this));
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint Release()
{
return ((delegate* unmanaged[Stdcall]<ID3D12MemoryAllocatorFactory*, uint>)this.lpVtbl[2])((ID3D12MemoryAllocatorFactory*)Unsafe.AsPointer(ref this));
}
/// <summary>
/// Creates a new <see cref="ID3D12MemoryAllocator"/> for a given device.
/// </summary>
+14 -3
ファイルの表示
@@ -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
/// <summary>
/// Configures the global <c>ID3D12MemoryAllocatorFactory</c> instance to use for the current process.
/// </summary>
/// <param name="allocatorFactory">The input <c>ID3D12MemoryAllocatorFactory</c> instance to use.</param>
/// <param name="allocatorFactory">The input <see cref="ICustomQueryInterface"/> instance to use (implementing <c>ID3D12MemoryAllocatorFactory</c>).</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="allocatorFactory"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">Thrown if the allocator factory cannot be configured at this time.</exception>
/// <remarks>
/// The allocatory factory can only be configured once, and before any <see cref="GraphicsDevice"/> instance has been created.
/// </remarks>
public static void ConfigureAllocatorFactory(void* allocatorFactory)
public static void ConfigureAllocatorFactory(ICustomQueryInterface allocatorFactory)
{
default(ArgumentNullException).ThrowIfNull(allocatorFactory);
DeviceHelper.ConfigureAllocatorFactory((ID3D12MemoryAllocatorFactory*)allocatorFactory);
using ComPtr<ID3D12MemoryAllocatorFactory> allocatorFactoryAbi = default;
// Unwrap the underlying ID3D12MemoryAllocatorFactory object
CustomQueryInterfaceResult result = allocatorFactory.GetInterface(
iid: ref *(Guid*)Windows.__uuidof<ID3D12MemoryAllocatorFactory>(),
ppv: out *(IntPtr*)allocatorFactoryAbi.GetAddressOf());
default(InvalidOperationException).ThrowIf(result != CustomQueryInterfaceResult.Handled, "Failed to retrieve the underlying ID3D12MemoryAllocatorFactory object.");
DeviceHelper.ConfigureAllocatorFactory(allocatorFactoryAbi.Get());
}
}