Merge pull request #130 from Sergio0694/bugfix/resource-disposing
Improved resource disposing
このコミットが含まれているのは:
21ファイル変更
+426
-86
検索条件に一致するファイルはありません
@@ -40,12 +40,10 @@ namespace ComputeSharp.Graphics.Commands
|
||||
public ID3D12PipelineState* D3D12PipelineState => this.d3D12PipelineState;
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool OnDispose()
|
||||
protected override void OnDispose()
|
||||
{
|
||||
this.d3D12RootSignature.Dispose();
|
||||
this.d3D12PipelineState.Dispose();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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(
|
||||
@@ -370,13 +381,8 @@ namespace ComputeSharp
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool OnDispose()
|
||||
protected override void OnDispose()
|
||||
{
|
||||
if (DeviceHelper.GetDefaultDeviceLuid() == Luid)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
DeviceHelper.NotifyDisposedDevice(this);
|
||||
|
||||
this.d3D12Device.Dispose();
|
||||
@@ -389,8 +395,6 @@ namespace ComputeSharp
|
||||
this.shaderResourceViewDescriptorAllocator.Dispose();
|
||||
this.pool.Dispose();
|
||||
this.allocator.Dispose();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -203,11 +203,9 @@ namespace ComputeSharp.Graphics.Helpers
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool OnDispose()
|
||||
protected override void OnDispose()
|
||||
{
|
||||
this.dxgiFactory4.Dispose();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
|
||||
namespace ComputeSharp.Interop
|
||||
{
|
||||
@@ -8,51 +10,64 @@ namespace ComputeSharp.Interop
|
||||
/// </summary>
|
||||
public abstract class NativeObject : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates whether or not the current instance has been disposed.
|
||||
/// </summary>
|
||||
private volatile int isDisposed;
|
||||
|
||||
/// <summary>
|
||||
/// Releases unmanaged resources and performs other cleanup operations.
|
||||
/// </summary>
|
||||
~NativeObject()
|
||||
{
|
||||
_ = CheckAndDispose();
|
||||
if (Interlocked.CompareExchange(ref this.isDisposed, 1, 0) == 0)
|
||||
{
|
||||
OnDispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether or not the current instance has already been disposed.
|
||||
/// </summary>
|
||||
internal bool IsDisposed { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Releases all the native resources for the current instance.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
internal bool IsDisposed
|
||||
{
|
||||
if (CheckAndDispose())
|
||||
{
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => this.isDisposed != 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
||||
/// </summary>
|
||||
private bool CheckAndDispose()
|
||||
/// <inheritdoc/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Dispose()
|
||||
{
|
||||
if (!IsDisposed)
|
||||
// If the current instance is a graphics device, check that the LUID doesn't
|
||||
// match the one for the default device, which is deliberately never disposed.
|
||||
// This type check is inlined and then resolved at JIT time if the target instance
|
||||
// is visible to the compiler. That is, if Dispose() is explicitly called on a
|
||||
// sealed type that is not GraphicsDevice, this entire path will just be removed.
|
||||
if (GetType() == typeof(GraphicsDevice) &&
|
||||
DeviceHelper.GetDefaultDeviceLuid() == Unsafe.As<GraphicsDevice>(this).Luid)
|
||||
{
|
||||
return IsDisposed = OnDispose();
|
||||
return;
|
||||
}
|
||||
|
||||
return true;
|
||||
if (Interlocked.CompareExchange(ref this.isDisposed, 1, 0) == 0)
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
void DisposeAndSuppressFinalize()
|
||||
{
|
||||
OnDispose();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
DisposeAndSuppressFinalize();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases unmanaged and (optionally) managed resources.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// Whether or not the dispose has actually been executed. This is done in order to allow derived types to
|
||||
/// optionally cancel a dispose operation, by not releasing resources and returning <see langword="false"/>.
|
||||
/// </returns>
|
||||
protected abstract bool OnDispose();
|
||||
protected abstract void OnDispose();
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ObjectDisposedException"/> if the current instance has been disposed.
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -80,6 +80,7 @@ namespace ComputeSharp.Resources
|
||||
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, (ulong)SizeInBytes);
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
|
||||
device.RegisterAllocatedResource();
|
||||
device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
|
||||
|
||||
switch (resourceType)
|
||||
@@ -167,17 +168,16 @@ namespace ComputeSharp.Resources
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool OnDispose()
|
||||
protected override void OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
this.allocation.Dispose();
|
||||
|
||||
if (GraphicsDevice?.IsDisposed == false)
|
||||
if (GraphicsDevice is GraphicsDevice device)
|
||||
{
|
||||
GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle);
|
||||
device.UnregisterAllocatedResource();
|
||||
device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -107,6 +107,7 @@ namespace ComputeSharp.Resources
|
||||
out _,
|
||||
out _);
|
||||
|
||||
device.RegisterAllocatedResource();
|
||||
device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
|
||||
|
||||
switch (resourceType)
|
||||
@@ -431,17 +432,16 @@ namespace ComputeSharp.Resources
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool OnDispose()
|
||||
protected override void OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
this.allocation.Dispose();
|
||||
|
||||
if (GraphicsDevice?.IsDisposed == false)
|
||||
if (GraphicsDevice is GraphicsDevice device)
|
||||
{
|
||||
GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle);
|
||||
device.UnregisterAllocatedResource();
|
||||
device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -111,6 +111,7 @@ namespace ComputeSharp.Resources
|
||||
out _,
|
||||
out _);
|
||||
|
||||
device.RegisterAllocatedResource();
|
||||
device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
|
||||
|
||||
switch (resourceType)
|
||||
@@ -474,17 +475,16 @@ namespace ComputeSharp.Resources
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool OnDispose()
|
||||
protected override void OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
this.allocation.Dispose();
|
||||
|
||||
if (GraphicsDevice?.IsDisposed == false)
|
||||
if (GraphicsDevice is GraphicsDevice device)
|
||||
{
|
||||
GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle);
|
||||
device.UnregisterAllocatedResource();
|
||||
device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -54,6 +54,8 @@ namespace ComputeSharp.Resources
|
||||
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, sizeInBytes);
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
|
||||
device.RegisterAllocatedResource();
|
||||
|
||||
this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
|
||||
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
@@ -104,12 +106,15 @@ namespace ComputeSharp.Resources
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool OnDispose()
|
||||
protected override void OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
this.allocation.Dispose();
|
||||
|
||||
return true;
|
||||
if (GraphicsDevice is GraphicsDevice device)
|
||||
{
|
||||
device.UnregisterAllocatedResource();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -71,6 +71,8 @@ namespace ComputeSharp.Resources
|
||||
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes);
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
|
||||
device.RegisterAllocatedResource();
|
||||
|
||||
this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
|
||||
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
@@ -114,12 +116,15 @@ namespace ComputeSharp.Resources
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool OnDispose()
|
||||
protected override void OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
this.allocation.Dispose();
|
||||
|
||||
return true;
|
||||
if (GraphicsDevice is GraphicsDevice device)
|
||||
{
|
||||
device.UnregisterAllocatedResource();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -74,6 +74,8 @@ namespace ComputeSharp.Resources
|
||||
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes);
|
||||
this.d3D12Resource = new ComPtr<ID3D12Resource>(this.allocation.Get()->GetResource());
|
||||
|
||||
device.RegisterAllocatedResource();
|
||||
|
||||
this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer;
|
||||
|
||||
this.d3D12Resource.Get()->SetName(this);
|
||||
@@ -93,6 +95,7 @@ namespace ComputeSharp.Resources
|
||||
/// Gets the height of the current texture.
|
||||
/// </summary>
|
||||
public int Height => (int)this.d3D12PlacedSubresourceFootprint.Footprint.Height;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the depth of the current texture.
|
||||
/// </summary>
|
||||
@@ -121,12 +124,15 @@ namespace ComputeSharp.Resources
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool OnDispose()
|
||||
protected override void OnDispose()
|
||||
{
|
||||
this.d3D12Resource.Dispose();
|
||||
this.allocation.Dispose();
|
||||
|
||||
return true;
|
||||
if (GraphicsDevice is GraphicsDevice device)
|
||||
{
|
||||
device.UnregisterAllocatedResource();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -35,11 +35,9 @@ namespace ComputeSharp.Shaders.Translation.Models
|
||||
public D3D12_SHADER_BYTECODE D3D12ShaderBytecode => new((ID3DBlob*)this.dxcBlobBytecode.Get());
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool OnDispose()
|
||||
protected override void OnDispose()
|
||||
{
|
||||
this.dxcBlobBytecode.Dispose();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -111,6 +111,33 @@ namespace ComputeSharp.Tests
|
||||
_ = buffer.ToArray();
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ConstantBuffer<>))]
|
||||
[Resource(typeof(ReadOnlyBuffer<>))]
|
||||
[Resource(typeof(ReadWriteBuffer<>))]
|
||||
public void DisposeAfterDevice(Device device, Type bufferType)
|
||||
{
|
||||
GraphicsDevice? gpu = device switch
|
||||
{
|
||||
Device.Discrete => Gpu.QueryDevices(info => info.IsHardwareAccelerated).FirstOrDefault(),
|
||||
Device.Warp => Gpu.QueryDevices(info => !info.IsHardwareAccelerated).First(),
|
||||
_ => throw new ArgumentException(nameof(device))
|
||||
};
|
||||
|
||||
if (gpu is null)
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Buffer<float> buffer = gpu.AllocateBuffer<float>(bufferType, 128);
|
||||
|
||||
gpu.Dispose();
|
||||
buffer.Dispose();
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ConstantBuffer<>))]
|
||||
|
||||
@@ -86,6 +86,32 @@ namespace ComputeSharp.Tests
|
||||
_ = texture.ToArray();
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<>))]
|
||||
[Resource(typeof(ReadWriteTexture2D<>))]
|
||||
public void DisposeAfterDevice(Device device, Type textureType)
|
||||
{
|
||||
GraphicsDevice? gpu = device switch
|
||||
{
|
||||
Device.Discrete => Gpu.QueryDevices(info => info.IsHardwareAccelerated).FirstOrDefault(),
|
||||
Device.Warp => Gpu.QueryDevices(info => !info.IsHardwareAccelerated).First(),
|
||||
_ => throw new ArgumentException(nameof(device))
|
||||
};
|
||||
|
||||
if (gpu is null)
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Texture2D<float> texture = gpu.AllocateTexture2D<float>(textureType, 10, 10);
|
||||
|
||||
gpu.Dispose();
|
||||
texture.Dispose();
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture2D<>))]
|
||||
|
||||
@@ -105,6 +105,32 @@ namespace ComputeSharp.Tests
|
||||
_ = texture.ToArray();
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture3D<>))]
|
||||
[Resource(typeof(ReadWriteTexture3D<>))]
|
||||
public void DisposeAfterDevice(Device device, Type textureType)
|
||||
{
|
||||
GraphicsDevice? gpu = device switch
|
||||
{
|
||||
Device.Discrete => Gpu.QueryDevices(info => info.IsHardwareAccelerated).FirstOrDefault(),
|
||||
Device.Warp => Gpu.QueryDevices(info => !info.IsHardwareAccelerated).First(),
|
||||
_ => throw new ArgumentException(nameof(device))
|
||||
};
|
||||
|
||||
if (gpu is null)
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Texture3D<float> texture = gpu.AllocateTexture3D<float>(textureType, 10, 10, 4);
|
||||
|
||||
gpu.Dispose();
|
||||
texture.Dispose();
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ReadOnlyTexture3D<>))]
|
||||
|
||||
@@ -66,6 +66,32 @@ namespace ComputeSharp.Tests
|
||||
_ = buffer.Span;
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(UploadBuffer<>))]
|
||||
[Resource(typeof(ReadBackBuffer<>))]
|
||||
public void DisposeAfterDevice(Device device, Type bufferType)
|
||||
{
|
||||
GraphicsDevice? gpu = device switch
|
||||
{
|
||||
Device.Discrete => Gpu.QueryDevices(info => info.IsHardwareAccelerated).FirstOrDefault(),
|
||||
Device.Warp => Gpu.QueryDevices(info => !info.IsHardwareAccelerated).First(),
|
||||
_ => throw new ArgumentException(nameof(device))
|
||||
};
|
||||
|
||||
if (gpu is null)
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
TransferBuffer<float> buffer = gpu.AllocateTransferBuffer<float>(bufferType, 128);
|
||||
|
||||
gpu.Dispose();
|
||||
buffer.Dispose();
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void Allocate_UploadBuffer_Copy_Full(Device device)
|
||||
|
||||
@@ -18,9 +18,9 @@ namespace ComputeSharp.Tests
|
||||
[Resource(typeof(ReadBackTexture2D<>))]
|
||||
[Data(AllocationMode.Default)]
|
||||
[Data(AllocationMode.Clear)]
|
||||
public unsafe void Allocate_Uninitialized_Ok(Device device, Type bufferType, AllocationMode allocationMode)
|
||||
public unsafe void Allocate_Uninitialized_Ok(Device device, Type textureType, AllocationMode allocationMode)
|
||||
{
|
||||
using TransferTexture2D<float> texture = device.Get().AllocateTransferTexture2D<float>(bufferType, 128, 256, allocationMode);
|
||||
using TransferTexture2D<float> texture = device.Get().AllocateTransferTexture2D<float>(textureType, 128, 256, allocationMode);
|
||||
|
||||
Assert.IsNotNull(texture);
|
||||
Assert.AreEqual(texture.Width, 128);
|
||||
@@ -50,9 +50,9 @@ namespace ComputeSharp.Tests
|
||||
[Data(0, -4314)]
|
||||
[Data(-14, -53)]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Allocate_Uninitialized_Fail(Device device, Type bufferType, int width, int height)
|
||||
public void Allocate_Uninitialized_Fail(Device device, Type textureType, int width, int height)
|
||||
{
|
||||
using TransferTexture2D<float> texture = device.Get().AllocateTransferTexture2D<float>(bufferType, width, height);
|
||||
using TransferTexture2D<float> texture = device.Get().AllocateTransferTexture2D<float>(textureType, width, height);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
@@ -60,15 +60,41 @@ namespace ComputeSharp.Tests
|
||||
[Resource(typeof(UploadTexture2D<>))]
|
||||
[Resource(typeof(ReadBackTexture2D<>))]
|
||||
[ExpectedException(typeof(ObjectDisposedException))]
|
||||
public void UsageAfterDispose(Device device, Type bufferType)
|
||||
public void UsageAfterDispose(Device device, Type textureType)
|
||||
{
|
||||
using TransferTexture2D<float> texture = device.Get().AllocateTransferTexture2D<float>(bufferType, 32, 32);
|
||||
using TransferTexture2D<float> texture = device.Get().AllocateTransferTexture2D<float>(textureType, 32, 32);
|
||||
|
||||
texture.Dispose();
|
||||
|
||||
_ = texture.View;
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(UploadTexture2D<>))]
|
||||
[Resource(typeof(ReadBackTexture2D<>))]
|
||||
public void DisposeAfterDevice(Device device, Type textureType)
|
||||
{
|
||||
GraphicsDevice? gpu = device switch
|
||||
{
|
||||
Device.Discrete => Gpu.QueryDevices(info => info.IsHardwareAccelerated).FirstOrDefault(),
|
||||
Device.Warp => Gpu.QueryDevices(info => !info.IsHardwareAccelerated).First(),
|
||||
_ => throw new ArgumentException(nameof(device))
|
||||
};
|
||||
|
||||
if (gpu is null)
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
TransferTexture2D<float> texture = gpu.AllocateTransferTexture2D<float>(textureType, 32, 32);
|
||||
|
||||
gpu.Dispose();
|
||||
texture.Dispose();
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void Allocate_UploadTexture2D_Copy_Full(Device device)
|
||||
|
||||
@@ -18,9 +18,9 @@ namespace ComputeSharp.Tests
|
||||
[Resource(typeof(ReadBackTexture3D<>))]
|
||||
[Data(AllocationMode.Default)]
|
||||
[Data(AllocationMode.Clear)]
|
||||
public unsafe void Allocate_Uninitialized_Ok(Device device, Type bufferType, AllocationMode allocationMode)
|
||||
public unsafe void Allocate_Uninitialized_Ok(Device device, Type textureType, AllocationMode allocationMode)
|
||||
{
|
||||
using TransferTexture3D<float> texture = device.Get().AllocateTransferTexture3D<float>(bufferType, 128, 256, 32, allocationMode);
|
||||
using TransferTexture3D<float> texture = device.Get().AllocateTransferTexture3D<float>(textureType, 128, 256, 32, allocationMode);
|
||||
|
||||
Assert.IsNotNull(texture);
|
||||
Assert.AreEqual(texture.Width, 128);
|
||||
@@ -57,9 +57,9 @@ namespace ComputeSharp.Tests
|
||||
[Data(12, 12, -1)]
|
||||
[Data(2, 4, ushort.MaxValue + 1)]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void Allocate_Uninitialized_Fail(Device device, Type bufferType, int width, int height, int depth)
|
||||
public void Allocate_Uninitialized_Fail(Device device, Type textureType, int width, int height, int depth)
|
||||
{
|
||||
using TransferTexture3D<float> texture = device.Get().AllocateTransferTexture3D<float>(bufferType, width, height, depth);
|
||||
using TransferTexture3D<float> texture = device.Get().AllocateTransferTexture3D<float>(textureType, width, height, depth);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
@@ -67,15 +67,41 @@ namespace ComputeSharp.Tests
|
||||
[Resource(typeof(UploadTexture3D<>))]
|
||||
[Resource(typeof(ReadBackTexture3D<>))]
|
||||
[ExpectedException(typeof(ObjectDisposedException))]
|
||||
public void UsageAfterDispose(Device device, Type bufferType)
|
||||
public void UsageAfterDispose(Device device, Type textureType)
|
||||
{
|
||||
using TransferTexture3D<float> texture = device.Get().AllocateTransferTexture3D<float>(bufferType, 32, 32, 16);
|
||||
using TransferTexture3D<float> texture = device.Get().AllocateTransferTexture3D<float>(textureType, 32, 32, 16);
|
||||
|
||||
texture.Dispose();
|
||||
|
||||
_ = texture.View;
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(UploadTexture3D<>))]
|
||||
[Resource(typeof(ReadBackTexture3D<>))]
|
||||
public void DisposeAfterDevice(Device device, Type textureType)
|
||||
{
|
||||
GraphicsDevice? gpu = device switch
|
||||
{
|
||||
Device.Discrete => Gpu.QueryDevices(info => info.IsHardwareAccelerated).FirstOrDefault(),
|
||||
Device.Warp => Gpu.QueryDevices(info => !info.IsHardwareAccelerated).First(),
|
||||
_ => throw new ArgumentException(nameof(device))
|
||||
};
|
||||
|
||||
if (gpu is null)
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
TransferTexture3D<float> texture = gpu.AllocateTransferTexture3D<float>(textureType, 32, 32, 16);
|
||||
|
||||
gpu.Dispose();
|
||||
texture.Dispose();
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void Allocate_UploadTexture3D_Copy_Full(Device device)
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする