Implement wrapping types for D3D12MA

このコミットが含まれているのは:
Sergio Pedri committed 2023-07-01 21:18:48 +02:00
1 親 56dcbe201f
コミット 2070246f90
6ファイル変更
+613 -249

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

-249
ファイルの表示
@@ -1,249 +0,0 @@
using System;
using ComputeSharp.Core.Extensions;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using static TerraFX.Interop.DirectX.D3D12_CPU_PAGE_PROPERTY;
using static TerraFX.Interop.DirectX.D3D12_HEAP_TYPE;
using static TerraFX.Interop.DirectX.D3D12_MEMORY_POOL;
using static TerraFX.Interop.DirectX.D3D12_RESOURCE_FLAGS;
using static TerraFX.Interop.DirectX.D3D12_RESOURCE_STATES;
using static TerraFX.Interop.DirectX.D3D12MA_ALLOCATION_FLAGS;
using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType;
namespace ComputeSharp.Graphics.Extensions;
/// <summary>
/// A <see langword="class"/> with extensions for the <see cref="D3D12MA_Allocator"/> type.
/// </summary>
internal static unsafe class AllocatorExtensions
{
/// <summary>
/// Creates a <see cref="D3D12MA_Pool"/> instance suited to be used for cache coherent UMA devices.
/// </summary>
/// <param name="allocator">The <see cref="D3D12MA_Allocator"/> instance in use.</param>
/// <returns>A <see cref="D3D12MA_Pool"/> instance suited to be used for cache coherent UMA devices.</returns>
public static ComPtr<D3D12MA_Pool> CreatePoolForCacheCoherentUMA(this ref D3D12MA_Allocator allocator)
{
using ComPtr<D3D12MA_Pool> pool = default;
D3D12MA_POOL_DESC poolDesc = default;
poolDesc.HeapProperties.CreationNodeMask = 1;
poolDesc.HeapProperties.VisibleNodeMask = 1;
poolDesc.HeapProperties.Type = D3D12_HEAP_TYPE_CUSTOM;
poolDesc.HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK;
poolDesc.HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
allocator.CreatePool(&poolDesc, pool.GetAddressOf()).Assert();
return pool.Move();
}
/// <summary>
/// Creates a resource for a given buffer type.
/// </summary>
/// <param name="allocator">The <see cref="D3D12MA_Allocator"/> instance in use.</param>
/// <param name="pool">The <see cref="D3D12MA_Pool"/> instance to use, if any.</param>
/// <param name="resourceType">The resource type currently in use.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <param name="sizeInBytes">The size in bytes of the current buffer.</param>
/// <returns>An <see cref="ComPtr{T}"/> reference for the current <see cref="D3D12MA_Allocation"/> object.</returns>
public static ComPtr<D3D12MA_Allocation> CreateResource(
this ref D3D12MA_Allocator allocator,
D3D12MA_Pool* pool,
ResourceType resourceType,
AllocationMode allocationMode,
ulong sizeInBytes)
{
D3D12MA_ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? D3D12MA_ALLOCATION_FLAG_NONE : D3D12MA_ALLOCATION_FLAG_COMMITTED;
(D3D12_HEAP_TYPE d3D12HeapType,
D3D12_RESOURCE_FLAGS d3D12ResourceFlags,
D3D12_RESOURCE_STATES d3D12ResourceStates) = resourceType switch
{
ResourceType.Constant => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ),
ResourceType.ReadOnly => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON),
ResourceType.ReadWrite => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COMMON),
ResourceType.ReadBack => (D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST),
ResourceType.Upload => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ),
_ => default(ArgumentException).Throw<(D3D12_HEAP_TYPE, D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>(nameof(resourceType))
};
using ComPtr<D3D12MA_Allocation> allocation = default;
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(sizeInBytes, d3D12ResourceFlags);
D3D12MA_ALLOCATION_DESC allocationDesc = default;
allocationDesc.HeapType = d3D12HeapType;
allocationDesc.Flags = allocationFlags;
allocationDesc.CustomPool = pool;
allocator.CreateResource(
&allocationDesc,
&d3D12ResourceDescription,
d3D12ResourceStates,
null,
allocation.GetAddressOf(),
null,
null).Assert();
return allocation.Move();
}
/// <summary>
/// Creates a resource for a given 1D texture type.
/// </summary>
/// <param name="allocator">The <see cref="D3D12MA_Allocator"/> instance in use.</param>
/// <param name="pool">The <see cref="D3D12MA_Pool"/> instance to use, if any.</param>
/// <param name="resourceType">The resource type currently in use.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> value to use.</param>
/// <param name="width">The width of the texture resource.</param>
/// <param name="d3D12ResourceStates">The default <see cref="D3D12_RESOURCE_STATES"/> value for the resource.</param>
/// <returns>An <see cref="ComPtr{T}"/> reference for the current <see cref="D3D12MA_Allocation"/> object.</returns>
public static ComPtr<D3D12MA_Allocation> CreateResource(
this ref D3D12MA_Allocator allocator,
D3D12MA_Pool* pool,
ResourceType resourceType,
AllocationMode allocationMode,
DXGI_FORMAT dxgiFormat,
uint width,
out D3D12_RESOURCE_STATES d3D12ResourceStates)
{
D3D12MA_ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? D3D12MA_ALLOCATION_FLAG_NONE : D3D12MA_ALLOCATION_FLAG_COMMITTED;
D3D12_RESOURCE_FLAGS d3D12ResourceFlags;
(d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch
{
ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON),
ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS),
_ => default(ArgumentException).Throw<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>(nameof(resourceType))
};
using ComPtr<D3D12MA_Allocation> allocation = default;
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex1D(dxgiFormat, width, mipLevels: 1, flags: d3D12ResourceFlags);
D3D12MA_ALLOCATION_DESC allocationDesc = default;
allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT;
allocationDesc.Flags = allocationFlags;
allocationDesc.CustomPool = pool;
allocator.CreateResource(
&allocationDesc,
&d3D12ResourceDescription,
d3D12ResourceStates,
null,
allocation.GetAddressOf(),
null,
null).Assert();
return allocation.Move();
}
/// <summary>
/// Creates a resource for a given 2D texture type.
/// </summary>
/// <param name="allocator">The <see cref="D3D12MA_Allocator"/> instance in use.</param>
/// <param name="pool">The <see cref="D3D12MA_Pool"/> instance to use, if any.</param>
/// <param name="resourceType">The resource type currently in use.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> value to use.</param>
/// <param name="width">The width of the texture resource.</param>
/// <param name="height">The height of the texture resource.</param>
/// <param name="d3D12ResourceStates">The default <see cref="D3D12_RESOURCE_STATES"/> value for the resource.</param>
/// <returns>An <see cref="ComPtr{T}"/> reference for the current <see cref="D3D12MA_Allocation"/> object.</returns>
public static ComPtr<D3D12MA_Allocation> CreateResource(
this ref D3D12MA_Allocator allocator,
D3D12MA_Pool* pool,
ResourceType resourceType,
AllocationMode allocationMode,
DXGI_FORMAT dxgiFormat,
uint width,
uint height,
out D3D12_RESOURCE_STATES d3D12ResourceStates)
{
D3D12MA_ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? D3D12MA_ALLOCATION_FLAG_NONE : D3D12MA_ALLOCATION_FLAG_COMMITTED;
D3D12_RESOURCE_FLAGS d3D12ResourceFlags;
(d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch
{
ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON),
ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS),
_ => default(ArgumentException).Throw<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>(nameof(resourceType))
};
using ComPtr<D3D12MA_Allocation> allocation = default;
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, mipLevels: 1, flags: d3D12ResourceFlags);
D3D12MA_ALLOCATION_DESC allocationDesc = default;
allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT;
allocationDesc.Flags = allocationFlags;
allocationDesc.CustomPool = pool;
allocator.CreateResource(
&allocationDesc,
&d3D12ResourceDescription,
d3D12ResourceStates,
null,
allocation.GetAddressOf(),
null,
null).Assert();
return allocation.Move();
}
/// <summary>
/// Creates a resource for a given 3D texture type.
/// </summary>
/// <param name="allocator">The <see cref="D3D12MA_Allocator"/> instance in use.</param>
/// <param name="pool">The <see cref="D3D12MA_Pool"/> instance to use, if any.</param>
/// <param name="resourceType">The resource type currently in use.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> value to use.</param>
/// <param name="width">The width of the texture resource.</param>
/// <param name="height">The height of the texture resource.</param>
/// <param name="depth">The depth of the texture resource.</param>
/// <param name="d3D12ResourceStates">The default <see cref="D3D12_RESOURCE_STATES"/> value for the resource.</param>
/// <returns>An <see cref="ComPtr{T}"/> reference for the current <see cref="D3D12MA_Allocation"/> object.</returns>
public static ComPtr<D3D12MA_Allocation> CreateResource(
this ref D3D12MA_Allocator allocator,
D3D12MA_Pool* pool,
ResourceType resourceType,
AllocationMode allocationMode,
DXGI_FORMAT dxgiFormat,
uint width,
uint height,
ushort depth,
out D3D12_RESOURCE_STATES d3D12ResourceStates)
{
D3D12MA_ALLOCATION_FLAGS allocationFlags = allocationMode == AllocationMode.Default ? D3D12MA_ALLOCATION_FLAG_NONE : D3D12MA_ALLOCATION_FLAG_COMMITTED;
D3D12_RESOURCE_FLAGS d3D12ResourceFlags;
(d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch
{
ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON),
ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS),
_ => default(ArgumentException).Throw<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>(nameof(resourceType))
};
using ComPtr<D3D12MA_Allocation> allocation = default;
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(dxgiFormat, width, height, depth, mipLevels: 1, flags: d3D12ResourceFlags);
D3D12MA_ALLOCATION_DESC allocationDesc = default;
allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT;
allocationDesc.Flags = allocationFlags;
allocationDesc.CustomPool = pool;
allocator.CreateResource(
&allocationDesc,
&d3D12ResourceDescription,
d3D12ResourceStates,
null,
allocation.GetAddressOf(),
null,
null).Assert();
return allocation.Move();
}
}
+35
ファイルの表示
@@ -0,0 +1,35 @@
using ComputeSharp.Core.Extensions;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using static TerraFX.Interop.DirectX.D3D12_CPU_PAGE_PROPERTY;
using static TerraFX.Interop.DirectX.D3D12_HEAP_TYPE;
using static TerraFX.Interop.DirectX.D3D12_MEMORY_POOL;
namespace ComputeSharp.D3D12MemoryAllocator.Extensions;
/// <summary>
/// A <see langword="class"/> with extensions for the <see cref="D3D12MA_Allocator"/> type.
/// </summary>
internal static unsafe class AllocatorExtensions
{
/// <summary>
/// Creates a <see cref="D3D12MA_Pool"/> instance suited to be used for cache coherent UMA devices.
/// </summary>
/// <param name="allocator">The <see cref="D3D12MA_Allocator"/> instance in use.</param>
/// <returns>A <see cref="D3D12MA_Pool"/> instance suited to be used for cache coherent UMA devices.</returns>
public static ComPtr<D3D12MA_Pool> CreatePoolForCacheCoherentUMA(this ref D3D12MA_Allocator allocator)
{
using ComPtr<D3D12MA_Pool> pool = default;
D3D12MA_POOL_DESC poolDesc = default;
poolDesc.HeapProperties.CreationNodeMask = 1;
poolDesc.HeapProperties.VisibleNodeMask = 1;
poolDesc.HeapProperties.Type = D3D12_HEAP_TYPE_CUSTOM;
poolDesc.HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK;
poolDesc.HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
allocator.CreatePool(&poolDesc, pool.GetAddressOf()).Assert();
return pool.Move();
}
}
+31
ファイルの表示
@@ -0,0 +1,31 @@
using ComputeSharp.Core.Extensions;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using System.Runtime.CompilerServices;
namespace ComputeSharp.Graphics.Extensions;
/// <summary>
/// A <see langword="class"/> with extensions for the <see cref="ID3D12Device"/> type.
/// </summary>
internal static unsafe class ID3D12DeviceExtensions
{
/// <summary>
/// Creates a <see cref="D3D12MA_Allocator"/> instance for a given device.
/// </summary>
/// <param name="d3D12Device">The target <see cref="ID3D12Device"/> to use to create the allocator.</param>
/// <param name="dxgiAdapter">The <see cref="IDXGIAdapter"/> that <paramref name="d3D12Device"/> was created from.</param>
/// <returns>A <see cref="D3D12MA_Allocator"/> instance to create resources on the device in use.</returns>
public static ComPtr<D3D12MA_Allocator> CreateAllocator(this ref ID3D12Device d3D12Device, IDXGIAdapter* dxgiAdapter)
{
using ComPtr<D3D12MA_Allocator> allocator = default;
D3D12MA_ALLOCATOR_DESC allocatorDesc = default;
allocatorDesc.pDevice = (ID3D12Device*)Unsafe.AsPointer(ref d3D12Device);
allocatorDesc.pAdapter = dxgiAdapter;
D3D12MemAlloc.D3D12MA_CreateAllocator(&allocatorDesc, allocator.GetAddressOf()).Assert();
return allocator.Move();
}
}
+174
ファイルの表示
@@ -0,0 +1,174 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using ComputeSharp.Interop.Allocation;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
namespace ComputeSharp.D3D12MemoryAllocator.Interop;
/// <summary>
/// An implementation of the <see cref="ID3D12Allocation"/> interface.
/// </summary>
internal unsafe struct ID3D12AllocationImpl
{
/// <summary>
/// The shared method table pointer for all <see cref="ID3D12Allocation"/> instances.
/// </summary>
private static readonly void** Vtbl = InitVtbl();
/// <summary>
/// Builds the custom method table pointer for <see cref="ID3D12Allocation"/>.
/// </summary>
/// <returns>The method table pointer for <see cref="ID3D12Allocation"/>.</returns>
private static void** InitVtbl()
{
void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(ID3D12AllocationImpl), sizeof(void*) * 4);
lpVtbl[0] = (delegate* unmanaged<ID3D12AllocationImpl*, Guid*, void**, int>)&QueryInterface;
lpVtbl[1] = (delegate* unmanaged<ID3D12AllocationImpl*, uint>)&AddRef;
lpVtbl[2] = (delegate* unmanaged<ID3D12AllocationImpl*, uint>)&Release;
lpVtbl[3] = (delegate* unmanaged<ID3D12AllocationImpl*, ID3D12Resource**, int>)&GetD3D12Resource;
return lpVtbl;
}
/// <summary>
/// The vtable pointer for the current instance.
/// </summary>
private void** lpVtbl;
/// <summary>
/// The current reference count for the object (from <see cref="IUnknown"/>).
/// </summary>
private volatile int referenceCount;
/// <summary>
/// The parent <see cref="ID3D12MemoryAllocatorImpl"/> instance.
/// </summary>
private ID3D12MemoryAllocatorImpl* allocatorImpl;
/// <summary>
/// The wrapped <see cref="D3D12MA_Allocation"/> instance.
/// </summary>
private D3D12MA_Allocation* allocation;
/// <summary>
/// The underlying <see cref="ID3D12Resource"/> instance.
/// </summary>
private ID3D12Resource* d3D12Resource;
/// <summary>
/// The factory method for <see cref="ID3D12AllocationImpl"/> instances.
/// </summary>
/// <param name="allocationImpl">The resulting <see cref="ID3D12AllocationImpl"/> instance.</param>
/// <param name="allocatorImpl">The parent <see cref="ID3D12MemoryAllocatorImpl"/> instance.</param>
/// <param name="allocation">The wrapped <see cref="D3D12MA_Allocation"/> instance.</param>
/// <param name="d3D12Resource">The underlying <see cref="ID3D12Resource"/> instance.</param>
/// <returns>An <see cref="HRESULT"/> for the operation.</returns>
public static HRESULT Factory(
ID3D12AllocationImpl** allocationImpl,
ID3D12MemoryAllocatorImpl* allocatorImpl,
D3D12MA_Allocation* allocation,
ID3D12Resource* d3D12Resource)
{
ID3D12AllocationImpl* @this;
try
{
@this = (ID3D12AllocationImpl*)NativeMemory.Alloc((nuint)sizeof(ID3D12AllocationImpl));
}
catch (OutOfMemoryException)
{
*allocationImpl = null;
return E.E_OUTOFMEMORY;
}
_ = allocatorImpl->AddRef();
_ = allocation->AddRef();
_ = d3D12Resource->AddRef();
@this->lpVtbl = Vtbl;
@this->referenceCount = 1;
@this->allocatorImpl = allocatorImpl;
@this->allocation = allocation;
@this->d3D12Resource = d3D12Resource;
*allocationImpl = @this;
return S.S_OK;
}
/// <inheritdoc cref="IUnknown.QueryInterface"/>
[UnmanagedCallersOnly]
private static int QueryInterface(ID3D12AllocationImpl* @this, Guid* riid, void** ppvObject)
{
if (ppvObject is null)
{
return E.E_POINTER;
}
if (riid->Equals(Windows.__uuidof<IUnknown>()) ||
riid->Equals(ID3D12Allocation.Guid))
{
_ = Interlocked.Increment(ref @this->referenceCount);
*ppvObject = @this;
return S.S_OK;
}
*ppvObject = null;
return E.E_NOINTERFACE;
}
/// <inheritdoc cref="IUnknown.AddRef"/>
[UnmanagedCallersOnly]
private static uint AddRef(ID3D12AllocationImpl* @this)
{
return (uint)Interlocked.Increment(ref @this->referenceCount);
}
/// <inheritdoc cref="IUnknown.Release"/>
[UnmanagedCallersOnly]
private static uint Release(ID3D12AllocationImpl* @this)
{
uint referenceCount = (uint)Interlocked.Decrement(ref @this->referenceCount);
if (referenceCount == 0)
{
_ = @this->allocation->Release();
_ = @this->d3D12Resource->Release();
// Release the parent allocator as well as last thing. This is needed because the D3D12MA allocator
// will fail if an allocator is released while there are still outstanding allocations. So here we
// increment the reference count of the allocator when creating a new allocation object, and then we
// decrement it after the underlying allocation has been released. This means that if this allocation
// is the last thing keeping the allocator alive, it'll be disposed correctly after this last object.
_ = @this->allocatorImpl->Release();
NativeMemory.Free(@this);
}
return referenceCount;
}
/// <inheritdoc cref="ID3D12Allocation.GetD3D12Resource"/>
[UnmanagedCallersOnly]
private static int GetD3D12Resource(ID3D12AllocationImpl* @this, ID3D12Resource** resource)
{
if (resource is null)
{
return E.E_POINTER;
}
_ = @this->d3D12Resource->AddRef();
*resource = @this->d3D12Resource;
return S.S_OK;
}
}
+157
ファイルの表示
@@ -0,0 +1,157 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using ComputeSharp.D3D12MemoryAllocator.Extensions;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Interop.Allocation;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using static TerraFX.Interop.DirectX.D3D12_FEATURE;
namespace ComputeSharp.D3D12MemoryAllocator.Interop;
/// <summary>
/// An implementation of the <see cref="ID3D12MemoryAllocatorFactory"/> interface.
/// </summary>
internal unsafe struct ID3D12MemoryAllocatorFactoryImpl
{
/// <summary>
/// The shared method table pointer for all <see cref="ID3D12MemoryAllocatorFactory"/> instances.
/// </summary>
private static readonly void** Vtbl = InitVtbl();
/// <summary>
/// Builds the custom method table pointer for <see cref="ID3D12MemoryAllocatorFactory"/>.
/// </summary>
/// <returns>The method table pointer for <see cref="ID3D12MemoryAllocatorFactory"/>.</returns>
private static void** InitVtbl()
{
void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(ID3D12MemoryAllocatorFactoryImpl), sizeof(void*) * 4);
lpVtbl[0] = (delegate* unmanaged<ID3D12MemoryAllocatorFactoryImpl*, Guid*, void**, int>)&QueryInterface;
lpVtbl[1] = (delegate* unmanaged<ID3D12MemoryAllocatorFactoryImpl*, uint>)&AddRef;
lpVtbl[2] = (delegate* unmanaged<ID3D12MemoryAllocatorFactoryImpl*, uint>)&Release;
lpVtbl[3] = (delegate* unmanaged<ID3D12MemoryAllocatorFactoryImpl*, ID3D12Device*, IDXGIAdapter*, ID3D12MemoryAllocator**, int>)&CreateAllocator;
return lpVtbl;
}
/// <summary>
/// The vtable pointer for the current instance.
/// </summary>
private void** lpVtbl;
/// <summary>
/// The current reference count for the object (from <see cref="IUnknown"/>).
/// </summary>
private volatile int referenceCount;
/// <summary>
/// The factory method for <see cref="ID3D12MemoryAllocatorFactoryImpl"/> instances.
/// </summary>
/// <param name="allocatorFactoryImpl">The resulting <see cref="ID3D12MemoryAllocatorFactoryImpl"/> instance.</param>
/// <returns>An <see cref="HRESULT"/> for the operation.</returns>
public static HRESULT Factory(ID3D12MemoryAllocatorFactoryImpl** allocatorFactoryImpl)
{
ID3D12MemoryAllocatorFactoryImpl* @this;
try
{
@this = (ID3D12MemoryAllocatorFactoryImpl*)NativeMemory.Alloc((nuint)sizeof(ID3D12MemoryAllocatorFactoryImpl));
}
catch (OutOfMemoryException)
{
*allocatorFactoryImpl = null;
return E.E_OUTOFMEMORY;
}
@this->lpVtbl = Vtbl;
@this->referenceCount = 1;
*allocatorFactoryImpl = @this;
return S.S_OK;
}
/// <inheritdoc cref="IUnknown.QueryInterface"/>
[UnmanagedCallersOnly]
private static int QueryInterface(ID3D12MemoryAllocatorFactoryImpl* @this, Guid* riid, void** ppvObject)
{
if (ppvObject is null)
{
return E.E_POINTER;
}
if (riid->Equals(Windows.__uuidof<IUnknown>()) ||
riid->Equals(ID3D12MemoryAllocatorFactory.Guid))
{
_ = Interlocked.Increment(ref @this->referenceCount);
*ppvObject = @this;
return S.S_OK;
}
*ppvObject = null;
return E.E_NOINTERFACE;
}
/// <inheritdoc cref="IUnknown.AddRef"/>
[UnmanagedCallersOnly]
private static uint AddRef(ID3D12MemoryAllocatorFactoryImpl* @this)
{
return (uint)Interlocked.Increment(ref @this->referenceCount);
}
/// <inheritdoc cref="IUnknown.Release"/>
[UnmanagedCallersOnly]
private static uint Release(ID3D12MemoryAllocatorFactoryImpl* @this)
{
uint referenceCount = (uint)Interlocked.Decrement(ref @this->referenceCount);
if (referenceCount == 0)
{
NativeMemory.Free(@this);
}
return referenceCount;
}
/// <inheritdoc cref="ID3D12MemoryAllocator.AllocateResource"/>
[UnmanagedCallersOnly]
private static int CreateAllocator(
ID3D12MemoryAllocatorFactoryImpl* @this,
ID3D12Device* device,
IDXGIAdapter* adapter,
ID3D12MemoryAllocator** allocator)
{
try
{
using ComPtr<D3D12MA_Allocator> d3D12MA_allocator = device->CreateAllocator(adapter);
// If the target device is a UMA device, also create a pool and use that in the allocator
if (device->CheckFeatureSupport<D3D12_FEATURE_DATA_ARCHITECTURE1>(D3D12_FEATURE_ARCHITECTURE1).CacheCoherentUMA)
{
using ComPtr<D3D12MA_Pool> pool = d3D12MA_allocator.Get()->CreatePoolForCacheCoherentUMA();
return ID3D12MemoryAllocatorImpl.Factory(
allocatorImpl: (ID3D12MemoryAllocatorImpl**)allocator,
allocator: d3D12MA_allocator.Get(),
pool: pool.Get());
}
// Otherwise create a standard allocator with no explicit pool
return ID3D12MemoryAllocatorImpl.Factory(
allocatorImpl: (ID3D12MemoryAllocatorImpl**)allocator,
allocator: d3D12MA_allocator.Get(),
pool: null);
}
catch (Exception e)
{
return e.HResult;
}
}
}
+216
ファイルの表示
@@ -0,0 +1,216 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Interop.Allocation;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using static TerraFX.Interop.DirectX.D3D12MA_ALLOCATION_FLAGS;
namespace ComputeSharp.D3D12MemoryAllocator.Interop;
/// <summary>
/// An implementation of the <see cref="ID3D12MemoryAllocator"/> interface.
/// </summary>
internal unsafe struct ID3D12MemoryAllocatorImpl
{
/// <summary>
/// The shared method table pointer for all <see cref="ID3D12MemoryAllocator"/> instances.
/// </summary>
private static readonly void** Vtbl = InitVtbl();
/// <summary>
/// Builds the custom method table pointer for <see cref="ID3D12MemoryAllocator"/>.
/// </summary>
/// <returns>The method table pointer for <see cref="ID3D12MemoryAllocator"/>.</returns>
private static void** InitVtbl()
{
void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(ID3D12MemoryAllocator), sizeof(void*) * 4);
lpVtbl[0] = (delegate* unmanaged<ID3D12MemoryAllocatorImpl*, Guid*, void**, int>)&QueryInterface;
lpVtbl[1] = (delegate* unmanaged<ID3D12MemoryAllocatorImpl*, uint>)&AddRef;
lpVtbl[2] = (delegate* unmanaged<ID3D12MemoryAllocatorImpl*, uint>)&Release;
lpVtbl[3] = (delegate* unmanaged<ID3D12MemoryAllocatorImpl*, D3D12_RESOURCE_DESC*, D3D12_HEAP_TYPE, D3D12_RESOURCE_STATES, BOOL, ID3D12Allocation**, int>)&AllocateResource;
return lpVtbl;
}
/// <summary>
/// The vtable pointer for the current instance.
/// </summary>
private void** lpVtbl;
/// <summary>
/// The current reference count for the object (from <see cref="IUnknown"/>).
/// </summary>
private volatile int referenceCount;
/// <summary>
/// The <see cref="D3D12MA_Allocator"/> in use associated to the current device.
/// </summary>
private D3D12MA_Allocator* allocator;
/// <summary>
/// The <see cref="D3D12MA_Pool"/> instance in use, if the device is using cache coherent UMA.
/// </summary>
private D3D12MA_Pool* pool;
/// <summary>
/// The factory method for <see cref="ID3D12MemoryAllocatorImpl"/> instances.
/// </summary>
/// <param name="allocatorImpl">The resulting <see cref="ID3D12MemoryAllocatorImpl"/> instance.</param>
/// <param name="allocator">The input <see cref="D3D12MA_Allocator"/> instance.</param>
/// <param name="pool">The input <see cref="D3D12MA_Pool"/> instance, if available.</param>
/// <returns>An <see cref="HRESULT"/> for the operation.</returns>
public static HRESULT Factory(
ID3D12MemoryAllocatorImpl** allocatorImpl,
D3D12MA_Allocator* allocator,
D3D12MA_Pool* pool)
{
ID3D12MemoryAllocatorImpl* @this;
try
{
@this = (ID3D12MemoryAllocatorImpl*)NativeMemory.Alloc((nuint)sizeof(ID3D12MemoryAllocatorImpl));
}
catch (OutOfMemoryException)
{
*allocatorImpl = null;
return E.E_OUTOFMEMORY;
}
_ = allocator->AddRef();
// The pool is not necessarily available (it's only used for UMA devices), as it allows creating
// resources in a CPU readable memory region. With non UMA devices, we use the default behavior.
if (pool is not null)
{
_ = pool->AddRef();
}
@this->lpVtbl = Vtbl;
@this->referenceCount = 1;
@this->allocator = allocator;
@this->pool = pool;
*allocatorImpl = @this;
return S.S_OK;
}
/// <inheritdoc cref="IUnknown.AddRef"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint AddRef()
{
return (uint)Interlocked.Increment(ref this.referenceCount);
}
/// <inheritdoc cref="IUnknown.Release"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint Release()
{
uint referenceCount = (uint)Interlocked.Decrement(ref this.referenceCount);
if (referenceCount == 0)
{
// The pool must be released before its associated allocator is disposed. Note that in
// order for the disposal to work correctly, all resources created from the allocator
// must have been disposed already before the allocator is released. This is done by
// having the allocator also increment the reference count of their associated allocator.
// So here we just have to release first the pool, if present, and then the allocator.
if (this.pool is not null)
{
_ = this.pool->Release();
}
_ = this.allocator->Release();
NativeMemory.Free(Unsafe.AsPointer(ref this));
}
return referenceCount;
}
/// <inheritdoc cref="IUnknown.QueryInterface"/>
[UnmanagedCallersOnly]
private static int QueryInterface(ID3D12MemoryAllocatorImpl* @this, Guid* riid, void** ppvObject)
{
if (ppvObject is null)
{
return E.E_POINTER;
}
if (riid->Equals(Windows.__uuidof<IUnknown>()) ||
riid->Equals(ID3D12MemoryAllocator.Guid))
{
_ = Interlocked.Increment(ref @this->referenceCount);
*ppvObject = @this;
return S.S_OK;
}
*ppvObject = null;
return E.E_NOINTERFACE;
}
/// <inheritdoc cref="IUnknown.AddRef"/>
[UnmanagedCallersOnly]
private static uint AddRef(ID3D12MemoryAllocatorImpl* @this)
{
return @this->AddRef();
}
/// <inheritdoc cref="IUnknown.Release"/>
[UnmanagedCallersOnly]
private static uint Release(ID3D12MemoryAllocatorImpl* @this)
{
return @this->Release();
}
/// <inheritdoc cref="ID3D12MemoryAllocator.AllocateResource"/>
[UnmanagedCallersOnly]
private static int AllocateResource(
ID3D12MemoryAllocatorImpl* @this,
D3D12_RESOURCE_DESC* resourceDescription,
D3D12_HEAP_TYPE heapType,
D3D12_RESOURCE_STATES resourceStates,
BOOL clearAllocation,
ID3D12Allocation** allocation)
{
D3D12MA_ALLOCATION_FLAGS allocationFlags = clearAllocation ? D3D12MA_ALLOCATION_FLAG_COMMITTED : D3D12MA_ALLOCATION_FLAG_NONE;
D3D12MA_ALLOCATION_DESC allocationDesc = default;
allocationDesc.HeapType = heapType;
allocationDesc.Flags = allocationFlags;
allocationDesc.CustomPool = @this->pool;
using ComPtr<D3D12MA_Allocation> d3D12MA_allocation = default;
using ComPtr<ID3D12Resource> d3D12Resource = default;
// Invoke D3D12MA and create an allocation and resource
HRESULT hresult = @this->allocator->CreateResource(
pAllocDesc: &allocationDesc,
pResourceDesc: resourceDescription,
InitialResourceState: resourceStates,
pOptimizedClearValue: null,
ppAllocation: d3D12MA_allocation.GetAddressOf(),
riidResource: Windows.__uuidof<ID3D12Resource>(),
ppvResource: d3D12Resource.GetVoidAddressOf());
if (!Windows.SUCCEEDED(hresult))
{
return hresult;
}
// Create the allocation wrapper
return ID3D12AllocationImpl.Factory(
allocationImpl: (ID3D12AllocationImpl**)allocation,
allocatorImpl: @this,
allocation: d3D12MA_allocation.Get(),
d3D12Resource: d3D12Resource.Get());
}
}