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 sealed unsafe class D3D12MemoryAllocatorFactory : ICustomQueryInterface { /// /// The object wrapped by the current instance. /// private readonly ID3D12MemoryAllocatorFactoryImpl* allocatorFactory; /// /// Creates a new instance (implementing ID3D12MemoryAllocatorFactory). /// /// /// The resulting object should only be used to provide an argument for . /// public D3D12MemoryAllocatorFactory() { fixed (ID3D12MemoryAllocatorFactoryImpl** allocatorFactory = &this.allocatorFactory) { ID3D12MemoryAllocatorFactoryImpl.Factory(allocatorFactory).Assert(); } } /// /// Releases the underlying ID3D12MemoryAllocatorFactory object. /// ~D3D12MemoryAllocatorFactory() { if (this.allocatorFactory is not null) { _ = this.allocatorFactory->Release(); } } /// 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 }; } } }