From 702b880b89e365f0596fc668087932d38236dbe6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 3 Nov 2021 00:08:04 +0100 Subject: [PATCH] Removed ReferenceCountPtr and UniquePtr types --- .../Extensions/Interop/AllocatorExtensions.cs | 23 ++- .../Extensions/Interop/ComPtrExtensions.cs | 44 ++++++ .../Interop/ID3D12DeviceExtensions.cs | 4 +- src/ComputeSharp/Graphics/GraphicsDevice.cs | 8 +- .../Graphics/Interop/ReferenceCountPtr{T}.cs | 132 ------------------ .../Graphics/Interop/UniquePtr{T}.cs | 112 --------------- .../Graphics/Resources/Abstract/Buffer{T}.cs | 2 +- .../Resources/Abstract/StructuredBuffer{T}.cs | 5 +- .../Resources/Abstract/Texture2D{T}.cs | 6 +- .../Resources/Abstract/Texture3D{T}.cs | 6 +- .../Resources/Abstract/TransferBuffer{T}.cs | 2 +- .../Abstract/TransferTexture2D{T}.cs | 2 +- .../Abstract/TransferTexture3D{T}.cs | 2 +- 13 files changed, 73 insertions(+), 275 deletions(-) delete mode 100644 src/ComputeSharp/Graphics/Interop/ReferenceCountPtr{T}.cs delete mode 100644 src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs index 95dffd84..aa1fbfa0 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs @@ -1,5 +1,4 @@ using ComputeSharp.Core.Extensions; -using ComputeSharp.Interop; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_CPU_PAGE_PROPERTY; @@ -22,9 +21,9 @@ internal static unsafe class AllocatorExtensions /// /// The instance in use. /// A instance suited to be used for cache coherent UMA devices. - public static ReferenceCountPtr CreatePoolForCacheCoherentUMA(this ref D3D12MA_Allocator allocator) + public static ComPtr CreatePoolForCacheCoherentUMA(this ref D3D12MA_Allocator allocator) { - using ReferenceCountPtr pool = default; + using ComPtr pool = default; D3D12MA_POOL_DESC poolDesc = default; poolDesc.HeapProperties.CreationNodeMask = 1; @@ -46,8 +45,8 @@ internal static unsafe class AllocatorExtensions /// The resource type currently in use. /// The allocation mode to use for the new resource. /// The size in bytes of the current buffer. - /// An reference for the current object. - public static UniquePtr CreateResource( + /// An reference for the current object. + public static ComPtr CreateResource( this ref D3D12MA_Allocator allocator, D3D12MA_Pool* pool, ResourceType resourceType, @@ -67,7 +66,7 @@ internal static unsafe class AllocatorExtensions _ => ThrowHelper.ThrowArgumentException<(D3D12_HEAP_TYPE, D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() }; - using UniquePtr allocation = default; + using ComPtr allocation = default; D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(sizeInBytes, d3D12ResourceFlags); @@ -99,8 +98,8 @@ internal static unsafe class AllocatorExtensions /// The width of the texture resource. /// The height of the texture resource. /// The default value for the resource. - /// An reference for the current object. - public static UniquePtr CreateResource( + /// An reference for the current object. + public static ComPtr CreateResource( this ref D3D12MA_Allocator allocator, D3D12MA_Pool* pool, ResourceType resourceType, @@ -120,7 +119,7 @@ internal static unsafe class AllocatorExtensions _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() }; - using UniquePtr allocation = default; + using ComPtr allocation = default; D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, mipLevels: 1, flags: d3D12ResourceFlags); @@ -153,8 +152,8 @@ internal static unsafe class AllocatorExtensions /// The height of the texture resource. /// The depth of the texture resource. /// The default value for the resource. - /// An reference for the current object. - public static UniquePtr CreateResource( + /// An reference for the current object. + public static ComPtr CreateResource( this ref D3D12MA_Allocator allocator, D3D12MA_Pool* pool, ResourceType resourceType, @@ -175,7 +174,7 @@ internal static unsafe class AllocatorExtensions _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() }; - using UniquePtr allocation = default; + using ComPtr allocation = default; D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(dxgiFormat, width, height, depth, mipLevels: 1, flags: d3D12ResourceFlags); diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs index 5906b523..1a87e316 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs @@ -9,6 +9,50 @@ namespace ComputeSharp.Core.Extensions; /// internal static class ComPtrExtensions { + /// + /// Invokes on the wrapped object for an input value. + /// + /// The type of the current instance. + /// The input instance to increment the reference count for. + /// A instance of type . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe uint AddRef(this ComPtr ptr) + where T : unmanaged + { + if (ptr.Get() is not null) + { + return ((IUnknown*)ptr.Get())->AddRef(); + } + + return 0; + } + + /// + /// Invokes on the wrapped object for an input value. + /// If the object has been deleted, it also resets . + /// + /// The type of the current instance. + /// The input instance to release. + /// A instance of type . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe uint Release(this ref ComPtr ptr) + where T : unmanaged + { + if (ptr.Get() is not null) + { + uint count = ((IUnknown*)ptr.Get())->Release(); + + if (count == 0) + { + ptr = default; + } + + return count; + } + + return 0; + } + /// /// Reinterprets the current instance as one of type . /// diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs index c08dfc4c..bbc772f9 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs @@ -27,9 +27,9 @@ internal static unsafe class ID3D12DeviceExtensions /// 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) + public static ComPtr CreateAllocator(this ref ID3D12Device d3D12Device, IDXGIAdapter* dxgiAdapter) { - using ReferenceCountPtr allocator = default; + using ComPtr allocator = default; D3D12MA_ALLOCATOR_DESC allocatorDesc = default; allocatorDesc.pDevice = (ID3D12Device*)Unsafe.AsPointer(ref d3D12Device); diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 12ac2757..2970de18 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -76,12 +76,12 @@ public sealed unsafe class GraphicsDevice : NativeObject /// /// The in use associated to the current device. /// - private ReferenceCountPtr allocator; + private ComPtr allocator; /// /// The instance in use, if is . /// - private ReferenceCountPtr pool; + private ComPtr pool; /// /// Creates a new instance for the input . @@ -259,8 +259,8 @@ public sealed unsafe class GraphicsDevice : NativeObject /// internal void UnregisterAllocatedResource() { - this.pool.Dispose(); - this.allocator.Dispose(); + this.pool.Release(); + this.allocator.Release(); } /// diff --git a/src/ComputeSharp/Graphics/Interop/ReferenceCountPtr{T}.cs b/src/ComputeSharp/Graphics/Interop/ReferenceCountPtr{T}.cs deleted file mode 100644 index 0cf8d2f0..00000000 --- a/src/ComputeSharp/Graphics/Interop/ReferenceCountPtr{T}.cs +++ /dev/null @@ -1,132 +0,0 @@ -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 deleted file mode 100644 index f76b1fa1..00000000 --- a/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs +++ /dev/null @@ -1,112 +0,0 @@ -using System; -using System.ComponentModel; -using System.Diagnostics.Contracts; -using System.Runtime.CompilerServices; -using TerraFX.Interop; - -namespace ComputeSharp.Interop; - -/// -/// A -equivalent type to safely work with pointers to public APIs. -/// -/// 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 UniquePtr : IDisposable - where T : unmanaged -{ - /// - /// The raw pointer to an object, if existing. - /// - private T* pointer; - - /// - /// Creates a new instance from a raw pointer. - /// - /// The raw pointer to wrap. - public UniquePtr(T* other) - { - this.pointer = other; - } - - /// - /// Unwraps a instance and returns the internal raw pointer. - /// - /// The instance to unwrap. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator T*(UniquePtr other) - { - return other.Get(); - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Dispose() - { - 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 UniquePtr Move() - { - UniquePtr copy = this; - - this = default; - - return copy; - } -} diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 034f0c6e..9a3c57e5 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -23,7 +23,7 @@ public unsafe abstract class Buffer : NativeObject /// /// The instance used to retrieve . /// - private UniquePtr allocation; + private ComPtr allocation; /// /// The instance currently mapped. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 4992bab5..83fd562e 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -2,7 +2,6 @@ using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Graphics.Resources.Interop; -using ComputeSharp.Interop; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; @@ -61,7 +60,7 @@ public abstract class StructuredBuffer : Buffer nint byteOffset = (nint)sourceOffset * sizeof(T); nint byteLength = count * sizeof(T); - using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength); + using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.ReadBack, AllocationMode.Default, (ulong)byteLength); using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) { @@ -204,7 +203,7 @@ public abstract class StructuredBuffer : Buffer nint byteOffset = (nint)offset * sizeof(T); nint byteLength = length * sizeof(T); - using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.Upload, AllocationMode.Default, (ulong)byteLength); + using ComPtr allocation = GraphicsDevice.Allocator->CreateResource(null, ResourceType.Upload, AllocationMode.Default, (ulong)byteLength); using (ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map()) fixed (void* sourcePointer = &source) diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index efd1761e..57b8220d 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -28,7 +28,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// /// The instance used to retrieve . /// - private UniquePtr allocation; + private ComPtr allocation; /// /// The instance currently mapped. @@ -173,7 +173,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource( + using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( GraphicsDevice.Pool, ResourceType.ReadBack, AllocationMode.Default, @@ -386,7 +386,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource( + using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( GraphicsDevice.Pool, ResourceType.Upload, AllocationMode.Default, diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index b2a26d23..883c180e 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -28,7 +28,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper /// /// The instance used to retrieve . /// - private UniquePtr allocation; + private ComPtr allocation; /// /// The instance currently mapped. @@ -188,7 +188,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource( + using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( GraphicsDevice.Pool, ResourceType.ReadBack, AllocationMode.Default, @@ -427,7 +427,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource( + using ComPtr allocation = GraphicsDevice.Allocator->CreateResource( GraphicsDevice.Pool, ResourceType.Upload, AllocationMode.Default, diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs index 8c8ff41d..fd9ab1d9 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -20,7 +20,7 @@ public abstract unsafe class TransferBuffer : NativeObject, IMemoryOwner /// /// The instance used to retrieve . /// - private UniquePtr allocation; + private ComPtr allocation; /// /// The instance currently mapped. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs index dfa83ac1..80526093 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -20,7 +20,7 @@ public unsafe abstract class TransferTexture2D : NativeObject /// /// The instance used to retrieve . /// - private UniquePtr allocation; + private ComPtr allocation; /// /// The instance currently mapped. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs index d9bf3be6..e5a72cad 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -20,7 +20,7 @@ public unsafe abstract class TransferTexture3D : NativeObject /// /// The instance used to retrieve . /// - private UniquePtr allocation; + private ComPtr allocation; /// /// The instance currently mapped.