From c263489b240d42ae9f0a1998e7072f3bf1f206df Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 30 Jan 2021 16:04:07 +0100 Subject: [PATCH] Minor code refactoring --- src/ComputeSharp/Graphics/GraphicsDevice.cs | 17 +++++------ .../Graphics/Interop/AllocationPtr.cs | 28 +++++++++++++------ .../Graphics/Interop/AllocatorPtr.cs | 28 +++++++++++++------ 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 1611fd49..8462f7ce 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -2,6 +2,7 @@ using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using ComputeSharp.Core.Extensions; +using ComputeSharp.Graphics; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; @@ -75,7 +76,7 @@ namespace ComputeSharp /// /// The in use associated to the current device. /// - internal Allocator* allocator; + internal AllocatorPtr allocator; /// /// Creates a new instance for the input . @@ -114,7 +115,7 @@ namespace ComputeSharp allocatorDesc.pDevice = d3D12Device; allocatorDesc.pAdapter = dxgiAdapter; - fixed (Allocator** allocator = &this.allocator) + fixed (Allocator** allocator = this.allocator) { D3D12MemoryAllocator.CreateAllocator(&allocatorDesc, allocator).Assert(); } @@ -161,6 +162,11 @@ namespace ComputeSharp /// internal ID3D12Device* D3D12Device => this.d3D12Device; + /// + /// Gets the underlying wrapped by the current instance. + /// + internal Allocator* Allocator => this.allocator; + /// /// Gets whether or not the current device has a cache coherent UMA architecture. /// @@ -331,12 +337,7 @@ namespace ComputeSharp this.computeCommandAllocatorPool.Dispose(); this.copyCommandAllocatorPool.Dispose(); this.shaderResourceViewDescriptorAllocator.Dispose(); - - if (this.allocator != null) - { - this.allocator->Release(); - this.allocator = null; - } + this.allocator.Dispose(); return true; } diff --git a/src/ComputeSharp/Graphics/Interop/AllocationPtr.cs b/src/ComputeSharp/Graphics/Interop/AllocationPtr.cs index 48f31b83..e9a04e54 100644 --- a/src/ComputeSharp/Graphics/Interop/AllocationPtr.cs +++ b/src/ComputeSharp/Graphics/Interop/AllocationPtr.cs @@ -1,9 +1,10 @@ using System; +using System.ComponentModel; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using TerraFX.Interop; -namespace ComputeSharp.Graphics.Interop +namespace ComputeSharp.Graphics { /// /// A -equivalent type to safely work with pointers. @@ -28,6 +29,16 @@ namespace ComputeSharp.Graphics.Interop pointer = other; } + /// + /// Unwraps a instance and returns the internal raw pointer. + /// + /// The instance to unwrap. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Allocation*(AllocationPtr other) + { + return other.Get(); + } + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Dispose() @@ -66,16 +77,17 @@ namespace ComputeSharp.Graphics.Interop } /// - /// Gets the address of the current instance as a raw double pointer. + /// Gets the address of the current instance as a managed reference to pointer. /// - /// The raw pointer to the input instance. - /// This method is only valid when the current instance is on the stack or pinned. - [Pure] + /// The reference to the current instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly void** GetVoidAddressOf() - where T : unmanaged + [EditorBrowsable(EditorBrowsableState.Never)] + public readonly ref Allocation* GetPinnableReference() { - return (void**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); + fixed (Allocation** ptr = &this.pointer) + { + return ref *ptr; + } } /// diff --git a/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs b/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs index 97b75867..1d7033a4 100644 --- a/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs +++ b/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs @@ -1,9 +1,10 @@ using System; +using System.ComponentModel; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using TerraFX.Interop; -namespace ComputeSharp.Graphics.Interop +namespace ComputeSharp.Graphics { /// /// A -equivalent type to safely work with pointers. @@ -28,6 +29,16 @@ namespace ComputeSharp.Graphics.Interop pointer = other; } + /// + /// Unwraps a instance and returns the internal raw pointer. + /// + /// The instance to unwrap. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Allocator*(AllocatorPtr other) + { + return other.Get(); + } + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Dispose() @@ -66,16 +77,17 @@ namespace ComputeSharp.Graphics.Interop } /// - /// Gets the address of the current instance as a raw double pointer. + /// Gets the address of the current instance as a managed reference to pointer. /// - /// The raw pointer to the input instance. - /// This method is only valid when the current instance is on the stack or pinned. - [Pure] + /// The reference to the current instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly void** GetVoidAddressOf() - where T : unmanaged + [EditorBrowsable(EditorBrowsableState.Never)] + public readonly ref Allocator* GetPinnableReference() { - return (void**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); + fixed (Allocator** ptr = &this.pointer) + { + return ref *ptr; + } } ///