diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs index d964ffab..2c985a08 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs @@ -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; /// -partial class DeviceHelper +unsafe partial class DeviceHelper { /// /// The local cache of instances that are currently usable. @@ -24,11 +27,16 @@ partial class DeviceHelper /// private static GraphicsDevice? defaultDevice; + /// + /// The global instance to use, if any. + /// + private static ID3D12MemoryAllocatorFactory* globalAllocatorFactory; + /// /// Gets the cached instance or creates and caches a new one. /// /// The default instance. - public static unsafe GraphicsDevice GetDefaultDeviceFromCacheOrCreateInstance() + public static GraphicsDevice GetDefaultDeviceFromCacheOrCreateInstance() { lock (DevicesCache) { @@ -57,35 +65,6 @@ partial class DeviceHelper } } - /// - /// Retrieves a instance for an object. - /// - /// The to use to get a instance. - /// The that was created from. - /// The available info for the instance. - /// A instance for the input device. - 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; - } - } - /// /// Removes a from the internal cache. /// @@ -110,4 +89,79 @@ partial class DeviceHelper } } } + + /// + /// Configures the global ID3D12MemoryAllocatorFactory instance to use for the current process. + /// + /// + 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; + } + } + + /// + /// Retrieves a instance for an object. + /// + /// The to use to get a instance. + /// The that was created from. + /// The available info for the instance. + /// A instance for the input device. + 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 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; + } + } + + /// + /// Tries to create a new instance for a given device. + /// + /// The to try to create an allocator for. + /// The that was created from. + /// The resulting instance. + /// Whether an instance could be created successfully. + 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; + } } \ No newline at end of file diff --git a/src/ComputeSharp/Graphics/Interop/AllocationServices.cs b/src/ComputeSharp/Graphics/Interop/AllocationServices.cs new file mode 100644 index 00000000..cb43fb1a --- /dev/null +++ b/src/ComputeSharp/Graphics/Interop/AllocationServices.cs @@ -0,0 +1,27 @@ +using System; +using ComputeSharp.Graphics.Helpers; +using ComputeSharp.Interop.Allocation; + +namespace ComputeSharp.Interop; + +/// +/// Provides methods to configure the global allocator factory instance that will be used by all objects. +/// +public static unsafe class AllocationServices +{ + /// + /// Configures the global ID3D12MemoryAllocatorFactory instance to use for the current process. + /// + /// The input ID3D12MemoryAllocatorFactory instance to use. + /// 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) + { + default(ArgumentNullException).ThrowIfNull(allocatorFactory); + + DeviceHelper.ConfigureAllocatorFactory((ID3D12MemoryAllocatorFactory*)allocatorFactory); + } +} \ No newline at end of file