From e08eb53cd5ffbb26a7af34f6127313bbb8f81038 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 31 Jan 2021 14:50:20 +0100 Subject: [PATCH] Added UniquePtr type for D3D12MA types --- .../Extensions/Interop/AllocatorExtensions.cs | 18 +-- .../Extensions/Interop/ComPtrExtensions.cs | 7 +- src/ComputeSharp/Graphics/GraphicsDevice.cs | 2 +- .../Graphics/Interop/UniquePtr{T}.cs | 113 ++++++++++++++++++ .../Graphics/Resources/Abstract/Buffer{T}.cs | 2 +- .../Resources/Abstract/StructuredBuffer{T}.cs | 4 +- .../Resources/Abstract/Texture2D{T}.cs | 6 +- .../Resources/Abstract/Texture3D{T}.cs | 6 +- 8 files changed, 135 insertions(+), 23 deletions(-) create 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 d3ee4e58..12a4d875 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs @@ -21,8 +21,8 @@ namespace ComputeSharp.Graphics.Extensions /// 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 buffer. - public static AllocationPtr CreateResource( + /// An reference for the current object. + public static UniquePtr CreateResource( this ref Allocator allocator, ResourceType resourceType, AllocationMode allocationMode, @@ -41,7 +41,7 @@ namespace ComputeSharp.Graphics.Extensions _ => ThrowHelper.ThrowArgumentException<(D3D12_HEAP_TYPE, D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() }; - using AllocationPtr allocation = default; + using UniquePtr allocation = default; D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(sizeInBytes, d3D12ResourceFlags); @@ -71,8 +71,8 @@ namespace ComputeSharp.Graphics.Extensions /// The width of the texture resource. /// The height of the texture resource. /// The default value for the resource. - /// An reference for the current texture. - public static AllocationPtr CreateResource( + /// An reference for the current object. + public static UniquePtr CreateResource( this ref Allocator allocator, ResourceType resourceType, AllocationMode allocationMode, @@ -91,7 +91,7 @@ namespace ComputeSharp.Graphics.Extensions _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() }; - using AllocationPtr allocation = default; + using UniquePtr allocation = default; D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, flags: d3D12ResourceFlags); @@ -122,8 +122,8 @@ namespace ComputeSharp.Graphics.Extensions /// The height of the texture resource. /// The depth of the texture resource. /// The default value for the resource. - /// An reference for the current texture. - public static AllocationPtr CreateResource( + /// An reference for the current object. + public static UniquePtr CreateResource( this ref Allocator allocator, ResourceType resourceType, AllocationMode allocationMode, @@ -143,7 +143,7 @@ namespace ComputeSharp.Graphics.Extensions _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() }; - using AllocationPtr allocation = default; + using UniquePtr allocation = default; D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(dxgiFormat, width, height, depth, flags: d3D12ResourceFlags); diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs index 7388fdb8..7458b7d4 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ComPtrExtensions.cs @@ -43,12 +43,11 @@ namespace ComputeSharp.Core.Extensions } /// - /// Gets the address of the current instance as a raw double pointer. + /// Moves the current instance and resets it without releasing the reference. /// /// The type to wrap in the current instance. - /// The input instance to get the address for. - /// The raw pointer to the input instance. - /// This method is only valid when the current instance is on the stack or pinned. + /// The input instance to move. + /// The moved instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe ComPtr Move(this in ComPtr ptr) where T : unmanaged diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index afdf3cf6..8acf2ef6 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -76,7 +76,7 @@ namespace ComputeSharp /// /// The in use associated to the current device. /// - private AllocatorPtr allocator; + private UniquePtr allocator; /// /// Creates a new instance for the input . diff --git a/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs b/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs new file mode 100644 index 00000000..69e0d3b4 --- /dev/null +++ b/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs @@ -0,0 +1,113 @@ +using System; +using System.ComponentModel; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using TerraFX.Interop; + +namespace ComputeSharp.Graphics +{ + /// + /// A -equivalent type to safely work with pointers to public APIs. + /// + /// Must be either , , or . + /// + /// While this type is not marked as so that it can also be used in fields, make sure to + /// keep the reference counts properly tracked if you do store instances on the heap. + /// + 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) + { + 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(Allocator)) ((Allocator*)pointer)->Release(); + else if (typeof(T) == typeof(Allocation)) ((Allocation*)pointer)->Release(); + else if (typeof(T) == typeof(Pool)) ((Pool*)pointer)->Release(); + else if (typeof(T) == typeof(VirtualBlock)) ((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 37164e8f..5e4a5b42 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -43,7 +43,7 @@ namespace ComputeSharp.Resources /// The instance used to retrieve , if any. /// /// This will be if the owning device has set. - private AllocationPtr allocation; + private UniquePtr allocation; /// /// Creates a new instance with the specified parameters. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index b37a9ad4..81018c39 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -59,7 +59,7 @@ namespace ComputeSharp.Resources byteOffset = (nint)offset * sizeof(T), byteLength = length * sizeof(T); - using AllocationPtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.ReadBack, AllocationMode.Default, (ulong)byteSize); + using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.ReadBack, AllocationMode.Default, (ulong)byteSize); using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) { @@ -157,7 +157,7 @@ namespace ComputeSharp.Resources byteOffset = (nint)offset * sizeof(T), byteLength = length * sizeof(T); - using AllocationPtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.Upload, AllocationMode.Default, (ulong)byteSize); + using UniquePtr allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.Upload, AllocationMode.Default, (ulong)byteSize); 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 ca07242b..817fc478 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -60,7 +60,7 @@ namespace ComputeSharp.Resources /// The instance used to retrieve , if any. /// /// This will be if the owning device has set. - private AllocationPtr allocation; + private UniquePtr allocation; /// /// Creates a new instance with the specified parameters. @@ -186,7 +186,7 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using AllocationPtr allocation = default; + using UniquePtr allocation = default; using ComPtr d3D12Resource = default; if (GraphicsDevice.IsCacheCoherentUMA) @@ -336,7 +336,7 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using AllocationPtr allocation = default; + using UniquePtr allocation = default; using ComPtr d3D12Resource = default; if (GraphicsDevice.IsCacheCoherentUMA) diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 8de2ee64..1380156c 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -60,7 +60,7 @@ namespace ComputeSharp.Resources /// The instance used to retrieve , if any. /// /// This will be if the owning device has set. - private AllocationPtr allocation; + private UniquePtr allocation; /// /// Creates a new instance with the specified parameters. @@ -202,7 +202,7 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using AllocationPtr allocation = default; + using UniquePtr allocation = default; using ComPtr d3D12Resource = default; if (GraphicsDevice.IsCacheCoherentUMA) @@ -369,7 +369,7 @@ namespace ComputeSharp.Resources out ulong rowSizeInBytes, out ulong totalSizeInBytes); - using AllocationPtr allocation = default; + using UniquePtr allocation = default; using ComPtr d3D12Resource = default; if (GraphicsDevice.IsCacheCoherentUMA)