Added UniquePtr<T> type for D3D12MA types
このコミットが含まれているのは:
1 親
c6991cdf0e
コミット
e08eb53cd5
8ファイル変更
+135
-23
検索条件に一致するファイルはありません
@@ -21,8 +21,8 @@ namespace ComputeSharp.Graphics.Extensions
|
||||
/// <param name="resourceType">The resource type currently in use.</param>
|
||||
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
|
||||
/// <param name="sizeInBytes">The size in bytes of the current buffer.</param>
|
||||
/// <returns>An <see cref="AllocationPtr"/> reference for the current buffer.</returns>
|
||||
public static AllocationPtr CreateResource(
|
||||
/// <returns>An <see cref="UniquePtr{T}"/> reference for the current <see cref="Allocation"/> object.</returns>
|
||||
public static UniquePtr<Allocation> 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> allocation = default;
|
||||
|
||||
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(sizeInBytes, d3D12ResourceFlags);
|
||||
|
||||
@@ -71,8 +71,8 @@ namespace ComputeSharp.Graphics.Extensions
|
||||
/// <param name="width">The width of the texture resource.</param>
|
||||
/// <param name="height">The height of the texture resource.</param>
|
||||
/// <param name="d3D12ResourceStates">The default <see cref="D3D12_RESOURCE_STATES"/> value for the resource.</param>
|
||||
/// <returns>An <see cref="AllocationPtr"/> reference for the current texture.</returns>
|
||||
public static AllocationPtr CreateResource(
|
||||
/// <returns>An <see cref="UniquePtr{T}"/> reference for the current <see cref="Allocation"/> object.</returns>
|
||||
public static UniquePtr<Allocation> 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> allocation = default;
|
||||
|
||||
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, flags: d3D12ResourceFlags);
|
||||
|
||||
@@ -122,8 +122,8 @@ namespace ComputeSharp.Graphics.Extensions
|
||||
/// <param name="height">The height of the texture resource.</param>
|
||||
/// <param name="depth">The depth of the texture resource.</param>
|
||||
/// <param name="d3D12ResourceStates">The default <see cref="D3D12_RESOURCE_STATES"/> value for the resource.</param>
|
||||
/// <returns>An <see cref="AllocationPtr"/> reference for the current texture.</returns>
|
||||
public static AllocationPtr CreateResource(
|
||||
/// <returns>An <see cref="UniquePtr{T}"/> reference for the current <see cref="Allocation"/> object.</returns>
|
||||
public static UniquePtr<Allocation> 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> allocation = default;
|
||||
|
||||
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(dxgiFormat, width, height, depth, flags: d3D12ResourceFlags);
|
||||
|
||||
|
||||
@@ -43,12 +43,11 @@ namespace ComputeSharp.Core.Extensions
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the address of the current <see cref="ComPtr{T}"/> instance as a raw <see langword="void"/> double pointer.
|
||||
/// Moves the current <see cref="ComPtr{T}"/> instance and resets it without releasing the reference.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to wrap in the current <see cref="ComPtr{T}"/> instance.</typeparam>
|
||||
/// <param name="ptr">The input <see cref="ComPtr{T}"/> instance to get the address for.</param>
|
||||
/// <returns>The raw pointer to the input <see cref="ComPtr{T}"/> instance.</returns>
|
||||
/// <remarks>This method is only valid when the current <see cref="ComPtr{T}"/> instance is on the stack or pinned.</remarks>
|
||||
/// <param name="ptr">The input <see cref="ComPtr{T}"/> instance to move.</param>
|
||||
/// <returns>The moved <see cref="ComPtr{T}"/> instance.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe ComPtr<T> Move<T>(this in ComPtr<T> ptr)
|
||||
where T : unmanaged
|
||||
|
||||
@@ -76,7 +76,7 @@ namespace ComputeSharp
|
||||
/// <summary>
|
||||
/// The <see cref="Allocator"/> in use associated to the current device.
|
||||
/// </summary>
|
||||
private AllocatorPtr allocator;
|
||||
private UniquePtr<Allocator> allocator;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="GraphicsDevice"/> instance for the input <see cref="ID3D12Device"/>.
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
using TerraFX.Interop;
|
||||
|
||||
namespace ComputeSharp.Graphics
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see cref="ComPtr{T}"/>-equivalent type to safely work with pointers to public <see cref="D3D12MemoryAllocator"/> APIs.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Must be either <see cref="Allocator"/>, <see cref="Allocation"/>, <see cref="Pool"/> or <see cref="VirtualBlock"/>.</typeparam>
|
||||
/// <remarks>
|
||||
/// While this type is not marked as <see langword="ref"/> so that it can also be used in fields, make sure to
|
||||
/// keep the reference counts properly tracked if you do store <see cref="ComPtr{T}"/> instances on the heap.
|
||||
/// </remarks>
|
||||
internal unsafe struct UniquePtr<T> : IDisposable
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// The raw pointer to an <typeparamref name="T"/> object, if existing.
|
||||
/// </summary>
|
||||
private T* pointer;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="UniquePtr{T}"/> instance from a raw pointer.
|
||||
/// </summary>
|
||||
/// <param name="other">The raw pointer to wrap.</param>
|
||||
public UniquePtr(T* other)
|
||||
{
|
||||
pointer = other;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unwraps a <see cref="UniquePtr{T}"/> instance and returns the internal raw pointer.
|
||||
/// </summary>
|
||||
/// <param name="other">The <see cref="UniquePtr{T}"/> instance to unwrap.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static implicit operator T*(UniquePtr<T> other)
|
||||
{
|
||||
return other.Get();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
[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");
|
||||
}
|
||||
}
|
||||
|
||||
/// <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="UniquePtr{T}"/> instance as a raw <typeparamref name="T"/> double pointer.
|
||||
/// </summary>
|
||||
/// <returns>The raw pointer to the current <see cref="UniquePtr{T}"/> instance.</returns>
|
||||
/// <remarks>This method is only valid when the current <see cref="UniquePtr{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="UniquePtr{T}"/> instance as a managed <typeparamref name="T"/> reference to pointer.
|
||||
/// </summary>
|
||||
/// <returns>The reference to the current <see cref="UniquePtr{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="UniquePtr{T}"/> instance and resets it without releasing the reference.
|
||||
/// </summary>
|
||||
/// <returns>The moved <see cref="UniquePtr{T}"/> instance.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public UniquePtr<T> Move()
|
||||
{
|
||||
UniquePtr<T> copy = this;
|
||||
|
||||
this = default;
|
||||
|
||||
return copy;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,7 @@ namespace ComputeSharp.Resources
|
||||
/// The <see cref="Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>, if any.
|
||||
/// </summary>
|
||||
/// <remarks>This will be <see langword="null"/> if the owning device has <see cref="GraphicsDevice.IsCacheCoherentUMA"/> set.</remarks>
|
||||
private AllocationPtr allocation;
|
||||
private UniquePtr<Allocation> allocation;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Buffer{T}"/> instance with the specified parameters.
|
||||
|
||||
@@ -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> 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> allocation = GraphicsDevice.Allocator->CreateResource(ResourceType.Upload, AllocationMode.Default, (ulong)byteSize);
|
||||
|
||||
using (ID3D12ResourceMap resource = allocation.Get()->GetResource()->Map())
|
||||
fixed (void* sourcePointer = &source)
|
||||
|
||||
@@ -60,7 +60,7 @@ namespace ComputeSharp.Resources
|
||||
/// The <see cref="Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>, if any.
|
||||
/// </summary>
|
||||
/// <remarks>This will be <see langword="null"/> if the owning device has <see cref="GraphicsDevice.IsCacheCoherentUMA"/> set.</remarks>
|
||||
private AllocationPtr allocation;
|
||||
private UniquePtr<Allocation> allocation;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Texture2D{T}"/> 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> allocation = default;
|
||||
using ComPtr<ID3D12Resource> 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> allocation = default;
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = default;
|
||||
|
||||
if (GraphicsDevice.IsCacheCoherentUMA)
|
||||
|
||||
@@ -60,7 +60,7 @@ namespace ComputeSharp.Resources
|
||||
/// The <see cref="Allocation"/> instance used to retrieve <see cref="d3D12Resource"/>, if any.
|
||||
/// </summary>
|
||||
/// <remarks>This will be <see langword="null"/> if the owning device has <see cref="GraphicsDevice.IsCacheCoherentUMA"/> set.</remarks>
|
||||
private AllocationPtr allocation;
|
||||
private UniquePtr<Allocation> allocation;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Texture3D{T}"/> 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> allocation = default;
|
||||
using ComPtr<ID3D12Resource> 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> allocation = default;
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = default;
|
||||
|
||||
if (GraphicsDevice.IsCacheCoherentUMA)
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする