Add AllocationServices API and implement it

このコミットが含まれているのは:
Sergio Pedri committed 2023-07-01 20:57:24 +02:00
1 親 5f02ddaffe
コミット dbb684a084
2ファイル変更
+112 -31

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

+85 -31
ファイルの表示
@@ -1,13 +1,16 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Interop.Allocation;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
namespace ComputeSharp.Graphics.Helpers;
/// <inheritdoc/>
partial class DeviceHelper
unsafe partial class DeviceHelper
{
/// <summary>
/// The local cache of <see cref="GraphicsDevice"/> instances that are currently usable.
@@ -24,11 +27,16 @@ partial class DeviceHelper
/// </summary>
private static GraphicsDevice? defaultDevice;
/// <summary>
/// The global <see cref="ID3D12MemoryAllocatorFactory"/> instance to use, if any.
/// </summary>
private static ID3D12MemoryAllocatorFactory* globalAllocatorFactory;
/// <summary>
/// Gets the cached <see cref="GraphicsDevice"/> instance or creates and caches a new one.
/// </summary>
/// <returns>The default <see cref="GraphicsDevice"/> instance.</returns>
public static unsafe GraphicsDevice GetDefaultDeviceFromCacheOrCreateInstance()
public static GraphicsDevice GetDefaultDeviceFromCacheOrCreateInstance()
{
lock (DevicesCache)
{
@@ -57,35 +65,6 @@ partial class DeviceHelper
}
}
/// <summary>
/// Retrieves a <see cref="GraphicsDevice"/> instance for an <see cref="ID3D12Device"/> object.
/// </summary>
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> to use to get a <see cref="GraphicsDevice"/> instance.</param>
/// <param name="dxgiAdapter">The <see cref="IDXGIAdapter"/> that <paramref name="d3D12Device"/> was created from.</param>
/// <param name="dxgiDescription1">The available info for the <see cref="GraphicsDevice"/> instance.</param>
/// <returns>A <see cref="GraphicsDevice"/> instance for the input device.</returns>
private static unsafe GraphicsDevice GetOrCreateDevice(ID3D12Device* d3D12Device, IDXGIAdapter* dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1)
{
lock (DevicesCache)
{
Luid luid = Luid.FromLUID(dxgiDescription1->AdapterLuid);
if (!DevicesCache.TryGetValue(luid, out GraphicsDevice? device))
{
device = new GraphicsDevice(d3D12Device, dxgiAdapter, dxgiDescription1);
DevicesCache.Add(luid, device);
if (Configuration.IsDebugOutputEnabled)
{
D3D12InfoQueueMap.Add(luid, d3D12Device->CreateInfoQueue());
}
}
return device;
}
}
/// <summary>
/// Removes a <see cref="GraphicsDevice"/> from the internal cache.
/// </summary>
@@ -110,4 +89,79 @@ partial class DeviceHelper
}
}
}
/// <summary>
/// Configures the global <c>ID3D12MemoryAllocatorFactory</c> instance to use for the current process.
/// </summary>
/// <param name="allocatorFactory"></param>
public static void ConfigureAllocatorFactory(ID3D12MemoryAllocatorFactory* allocatorFactory)
{
lock (DevicesCache)
{
default(InvalidOperationException).ThrowIf(DevicesCache.Count != 0, "The allocator factory can only be configured before creating devices.");
default(InvalidOperationException).ThrowIf(globalAllocatorFactory is not null, "The allocator factory can only be configured once.");
_ = ((IUnknown*)allocatorFactory)->AddRef();
globalAllocatorFactory = allocatorFactory;
}
}
/// <summary>
/// Retrieves a <see cref="GraphicsDevice"/> instance for an <see cref="ID3D12Device"/> object.
/// </summary>
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> to use to get a <see cref="GraphicsDevice"/> instance.</param>
/// <param name="dxgiAdapter">The <see cref="IDXGIAdapter"/> that <paramref name="d3D12Device"/> was created from.</param>
/// <param name="dxgiDescription1">The available info for the <see cref="GraphicsDevice"/> instance.</param>
/// <returns>A <see cref="GraphicsDevice"/> instance for the input device.</returns>
private static GraphicsDevice GetOrCreateDevice(ID3D12Device* d3D12Device, IDXGIAdapter* dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1)
{
lock (DevicesCache)
{
Luid luid = Luid.FromLUID(dxgiDescription1->AdapterLuid);
if (!DevicesCache.TryGetValue(luid, out GraphicsDevice? device))
{
using ComPtr<ID3D12MemoryAllocator> memoryAllocator = default;
// Try to create a new ID3D12MemoryAllocator instance for the new device
_ = TryGetMemoryAllocator(d3D12Device, dxgiAdapter, memoryAllocator.GetAddressOf());
// Create the device passing the underlying device, adapter, and optional allocator
device = new GraphicsDevice(d3D12Device, dxgiAdapter, dxgiDescription1);
DevicesCache.Add(luid, device);
if (Configuration.IsDebugOutputEnabled)
{
D3D12InfoQueueMap.Add(luid, d3D12Device->CreateInfoQueue());
}
}
return device;
}
}
/// <summary>
/// Tries to create a new <see cref="ID3D12MemoryAllocator"/> instance for a given device.
/// </summary>
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> to try to create an allocator for.</param>
/// <param name="dxgiAdapter">The <see cref="IDXGIAdapter"/> that <paramref name="d3D12Device"/> was created from.</param>
/// <param name="memoryAllocator">The resulting <see cref="ID3D12MemoryAllocator"/> instance.</param>
/// <returns>Whether an <see cref="ID3D12MemoryAllocator"/> instance could be created successfully.</returns>
private static bool TryGetMemoryAllocator(ID3D12Device* d3D12Device, IDXGIAdapter* dxgiAdapter, ID3D12MemoryAllocator** memoryAllocator)
{
ID3D12MemoryAllocatorFactory* allocatorFactory = globalAllocatorFactory;
if (allocatorFactory is null)
{
*memoryAllocator = null;
return false;
}
allocatorFactory->CreateAllocator(d3D12Device, dxgiAdapter, memoryAllocator).Assert();
return true;
}
}
+27
ファイルの表示
@@ -0,0 +1,27 @@
using System;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Interop.Allocation;
namespace ComputeSharp.Interop;
/// <summary>
/// Provides methods to configure the global allocator factory instance that will be used by all <see cref="GraphicsDevice"/> objects.
/// </summary>
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>
/// <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)
{
default(ArgumentNullException).ThrowIfNull(allocatorFactory);
DeviceHelper.ConfigureAllocatorFactory((ID3D12MemoryAllocatorFactory*)allocatorFactory);
}
}