Added ReferenceCountPtr<T> type, code refactoring
このコミットが含まれているのは:
1 親
f7f99ad2fd
コミット
c7f7eac265
5ファイル変更
+178
-13
検索条件に一致するファイルはありません
@@ -22,9 +22,9 @@ namespace ComputeSharp.Graphics.Extensions
|
||||
/// </summary>
|
||||
/// <param name="allocator">The <see cref="D3D12MA_Allocator"/> instance in use.</param>
|
||||
/// <returns>A <see cref="D3D12MA_Pool"/> instance suited to be used for cache coherent UMA devices.</returns>
|
||||
public static UniquePtr<D3D12MA_Pool> CreatePoolForCacheCoherentUMA(this ref D3D12MA_Allocator allocator)
|
||||
public static ReferenceCountPtr<D3D12MA_Pool> CreatePoolForCacheCoherentUMA(this ref D3D12MA_Allocator allocator)
|
||||
{
|
||||
using UniquePtr<D3D12MA_Pool> pool = default;
|
||||
using ReferenceCountPtr<D3D12MA_Pool> pool = default;
|
||||
|
||||
D3D12MA_POOL_DESC poolDesc = default;
|
||||
poolDesc.HeapProperties.CreationNodeMask = 1;
|
||||
|
||||
@@ -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
|
||||
/// </summary>
|
||||
internal static unsafe class ID3D12DeviceExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a <see cref="D3D12MA_Allocator"/> instance for a given device.
|
||||
/// </summary>
|
||||
/// <param name="d3D12Device">The target <see cref="ID3D12Device"/> to use to create the allocator.</param>
|
||||
/// <param name="dxgiAdapter">The <see cref="IDXGIAdapter"/> that <paramref name="d3D12Device"/> was created from.</param>
|
||||
/// <returns>A <see cref="D3D12MA_Allocator"/> instance to create resources on the device in use.</returns>
|
||||
public static ReferenceCountPtr<D3D12MA_Allocator> CreateAllocator(this ref ID3D12Device d3D12Device, IDXGIAdapter* dxgiAdapter)
|
||||
{
|
||||
using ReferenceCountPtr<D3D12MA_Allocator> 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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ID3D12InfoQueue"/> for a given device.
|
||||
/// </summary>
|
||||
|
||||
@@ -76,12 +76,12 @@ namespace ComputeSharp
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Allocator"/> in use associated to the current device.
|
||||
/// </summary>
|
||||
private UniquePtr<D3D12MA_Allocator> allocator;
|
||||
private ReferenceCountPtr<D3D12MA_Allocator> allocator;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="D3D12MA_Pool"/> instance in use, if <see cref="IsCacheCoherentUMA"/> is <see langword="true"/>.
|
||||
/// </summary>
|
||||
private UniquePtr<D3D12MA_Pool> pool;
|
||||
private ReferenceCountPtr<D3D12MA_Pool> pool;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="GraphicsDevice"/> instance for the input <see cref="ID3D12Device"/>.
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers that a new resource has been allocated on the current device.
|
||||
/// </summary>
|
||||
internal void RegisterAllocatedResource()
|
||||
{
|
||||
this.pool.AddRef();
|
||||
this.allocator.AddRef();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters a generic resource that was allocated on the current device.
|
||||
/// </summary>
|
||||
internal void UnregisterAllocatedResource()
|
||||
{
|
||||
this.pool.Dispose();
|
||||
this.allocator.Dispose();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ID3D12DescriptorHandleAllocator.Rent"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal void RentShaderResourceViewDescriptorHandles(
|
||||
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see cref="ComPtr{T}"/>-equivalent type to safely work with pointers to public <see cref="D3D12MemAlloc"/> 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 <see cref="D3D12MemAlloc"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Must be either <see cref="D3D12MA_Allocator"/>, <see cref="D3D12MA_Allocation"/>, <see cref="D3D12MA_Pool"/> or <see cref="D3D12MA_VirtualBlock"/>.</typeparam>
|
||||
/// <remarks>
|
||||
/// This type is not marked as <see langword="ref"/> 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.
|
||||
/// </remarks>
|
||||
internal unsafe struct ReferenceCountPtr<T> : IDisposable
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// The raw pointer to an <typeparamref name="T"/> object, if existing.
|
||||
/// </summary>
|
||||
private T* pointer;
|
||||
|
||||
/// <summary>
|
||||
/// The current reference count - 1 for <see cref="pointer"/>.
|
||||
/// </summary>
|
||||
private volatile int count;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ReferenceCountPtr{T}"/> instance from a raw pointer.
|
||||
/// </summary>
|
||||
/// <param name="other">The raw pointer to wrap.</param>
|
||||
public ReferenceCountPtr(T* other)
|
||||
{
|
||||
this.pointer = other;
|
||||
this.count = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unwraps a <see cref="ReferenceCountPtr{T}"/> instance and returns the internal raw pointer.
|
||||
/// </summary>
|
||||
/// <param name="other">The <see cref="ReferenceCountPtr{T}"/> instance to unwrap.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static implicit operator T*(ReferenceCountPtr<T> other)
|
||||
{
|
||||
return other.Get();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Increments the internal reference count.
|
||||
/// </summary>
|
||||
public void AddRef()
|
||||
{
|
||||
Interlocked.Increment(ref this.count);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
[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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the currently wrapped raw pointer to an <typeparamref name="T"/> object.
|
||||
/// </summary>
|
||||
/// <returns>The raw pointer wrapped by the current <typeparamref name="T"/> instance.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly T* Get()
|
||||
{
|
||||
return this.pointer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the address of the current <see cref="ReferenceCountPtr{T}"/> instance as a raw <typeparamref name="T"/> double pointer.
|
||||
/// </summary>
|
||||
/// <returns>The raw pointer to the current <see cref="ReferenceCountPtr{T}"/> instance.</returns>
|
||||
/// <remarks>This method is only valid when the current <see cref="ReferenceCountPtr{T}"/> instance is on the stack or pinned.</remarks>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly T** GetAddressOf()
|
||||
{
|
||||
return (T**)Unsafe.AsPointer(ref Unsafe.AsRef(in this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the address of the current <see cref="ReferenceCountPtr{T}"/> instance as a managed <typeparamref name="T"/> reference to pointer.
|
||||
/// </summary>
|
||||
/// <returns>The reference to the current <see cref="ReferenceCountPtr{T}"/> instance.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public readonly ref T* GetPinnableReference()
|
||||
{
|
||||
fixed (T** ptr = &this.pointer)
|
||||
{
|
||||
return ref *ptr;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Moves the current <see cref="ReferenceCountPtr{T}"/> instance and resets it without releasing the reference.
|
||||
/// </summary>
|
||||
/// <returns>The moved <see cref="ReferenceCountPtr{T}"/> instance.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ReferenceCountPtr<T> Move()
|
||||
{
|
||||
ReferenceCountPtr<T> copy = this;
|
||||
|
||||
this = default;
|
||||
|
||||
return copy;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ namespace ComputeSharp.Interop
|
||||
/// <param name="other">The raw pointer to wrap.</param>
|
||||
public UniquePtr(T* other)
|
||||
{
|
||||
pointer = other;
|
||||
this.pointer = other;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする