diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs index 1b232919..5e401f67 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs @@ -22,9 +22,9 @@ namespace ComputeSharp.Graphics.Extensions /// /// The instance in use. /// A instance suited to be used for cache coherent UMA devices. - public static UniquePtr CreatePoolForCacheCoherentUMA(this ref D3D12MA_Allocator allocator) + public static ReferenceCountPtr CreatePoolForCacheCoherentUMA(this ref D3D12MA_Allocator allocator) { - using UniquePtr pool = default; + using ReferenceCountPtr pool = default; D3D12MA_POOL_DESC poolDesc = default; poolDesc.HeapProperties.CreationNodeMask = 1; diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs index 9287ed3f..87462db5 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs @@ -1,6 +1,8 @@ using System; using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; using ComputeSharp.Core.Extensions; +using ComputeSharp.Interop; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_QUEUE_FLAGS; using static TerraFX.Interop.D3D12_COMMAND_QUEUE_PRIORITY; @@ -20,6 +22,25 @@ namespace ComputeSharp.Graphics.Extensions /// internal static unsafe class ID3D12DeviceExtensions { + /// + /// Creates a instance for a given device. + /// + /// The target to use to create the allocator. + /// The that was created from. + /// A instance to create resources on the device in use. + public static ReferenceCountPtr CreateAllocator(this ref ID3D12Device d3D12Device, IDXGIAdapter* dxgiAdapter) + { + using ReferenceCountPtr 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(); + } + /// /// Creates a new for a given device. /// diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 3e47fad7..546f6d5a 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -76,12 +76,12 @@ namespace ComputeSharp /// /// The in use associated to the current device. /// - private UniquePtr allocator; + private ReferenceCountPtr allocator; /// /// The instance in use, if is . /// - private UniquePtr pool; + private ReferenceCountPtr pool; /// /// Creates a new instance for the input . @@ -117,14 +117,7 @@ namespace ComputeSharp IsCacheCoherentUMA = d3D12Architecture1Data.CacheCoherentUMA != 0; - D3D12MA_ALLOCATOR_DESC allocatorDesc = default; - allocatorDesc.pDevice = d3D12Device; - allocatorDesc.pAdapter = dxgiAdapter; - - fixed (D3D12MA_Allocator** allocator = this.allocator) - { - D3D12MemAlloc.D3D12MA_CreateAllocator(&allocatorDesc, allocator).Assert(); - } + this.allocator = d3D12Device->CreateAllocator(dxgiAdapter); if (IsCacheCoherentUMA) { @@ -252,6 +245,24 @@ namespace ComputeSharp D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW); } + /// + /// Registers that a new resource has been allocated on the current device. + /// + internal void RegisterAllocatedResource() + { + this.pool.AddRef(); + this.allocator.AddRef(); + } + + /// + /// Unregisters a generic resource that was allocated on the current device. + /// + internal void UnregisterAllocatedResource() + { + this.pool.Dispose(); + this.allocator.Dispose(); + } + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void RentShaderResourceViewDescriptorHandles( diff --git a/src/ComputeSharp/Graphics/Interop/ReferenceCountPtr{T}.cs b/src/ComputeSharp/Graphics/Interop/ReferenceCountPtr{T}.cs new file mode 100644 index 00000000..cc7d8e51 --- /dev/null +++ b/src/ComputeSharp/Graphics/Interop/ReferenceCountPtr{T}.cs @@ -0,0 +1,133 @@ +using System; +using System.ComponentModel; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using System.Threading; +using TerraFX.Interop; + +namespace ComputeSharp.Interop +{ + /// + /// A -equivalent type to safely work with pointers to public APIs. + /// This special type internally keeps track of a reference count for the wrapped native object. + /// This works around the lack of internal reference counting for objects from . + /// + /// Must be either , , or . + /// + /// This type is not marked as so that it can also be used in fields, so make sure to + /// avoid copying it and disposing it multiple times, as that would lead to a access violation exceptions. + /// + internal unsafe struct ReferenceCountPtr : IDisposable + where T : unmanaged + { + /// + /// The raw pointer to an object, if existing. + /// + private T* pointer; + + /// + /// The current reference count - 1 for . + /// + private volatile int count; + + /// + /// Creates a new instance from a raw pointer. + /// + /// The raw pointer to wrap. + public ReferenceCountPtr(T* other) + { + this.pointer = other; + this.count = 0; + } + + /// + /// Unwraps a instance and returns the internal raw pointer. + /// + /// The instance to unwrap. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator T*(ReferenceCountPtr other) + { + return other.Get(); + } + + /// + /// Increments the internal reference count. + /// + public void AddRef() + { + Interlocked.Increment(ref this.count); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Dispose() + { + if (Interlocked.Decrement(ref this.count) < 0) + { + T* pointer = this.pointer; + + if (pointer != null) + { + this.pointer = null; + + if (typeof(T) == typeof(D3D12MA_Allocator)) ((D3D12MA_Allocator*)pointer)->Release(); + else if (typeof(T) == typeof(D3D12MA_Allocation)) ((D3D12MA_Allocation*)pointer)->Release(); + else if (typeof(T) == typeof(D3D12MA_Pool)) ((D3D12MA_Pool*)pointer)->Release(); + else if (typeof(T) == typeof(D3D12MA_VirtualBlock)) ((D3D12MA_VirtualBlock*)pointer)->Release(); + else throw new ArgumentException("Invalid pointer type"); + } + } + } + + /// + /// Gets the currently wrapped raw pointer to an object. + /// + /// The raw pointer wrapped by the current instance. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly T* Get() + { + return this.pointer; + } + + /// + /// Gets the address of the current instance as a raw double pointer. + /// + /// The raw pointer to the current instance. + /// This method is only valid when the current instance is on the stack or pinned. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly T** GetAddressOf() + { + return (T**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); + } + + /// + /// Gets the address of the current instance as a managed reference to pointer. + /// + /// The reference to the current instance. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [EditorBrowsable(EditorBrowsableState.Never)] + public readonly ref T* GetPinnableReference() + { + fixed (T** ptr = &this.pointer) + { + return ref *ptr; + } + } + + /// + /// Moves the current instance and resets it without releasing the reference. + /// + /// The moved instance. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ReferenceCountPtr Move() + { + ReferenceCountPtr copy = this; + + this = default; + + return copy; + } + } +} diff --git a/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs b/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs index 2cec96e1..0a074455 100644 --- a/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs +++ b/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs @@ -28,7 +28,7 @@ namespace ComputeSharp.Interop /// The raw pointer to wrap. public UniquePtr(T* other) { - pointer = other; + this.pointer = other; } ///