diff --git a/src/ComputeSharp/Graphics/Commands/PipelineData.cs b/src/ComputeSharp/Graphics/Commands/PipelineData.cs
index 7f0e16d1..602c4ded 100644
--- a/src/ComputeSharp/Graphics/Commands/PipelineData.cs
+++ b/src/ComputeSharp/Graphics/Commands/PipelineData.cs
@@ -40,12 +40,10 @@ namespace ComputeSharp.Graphics.Commands
public ID3D12PipelineState* D3D12PipelineState => this.d3D12PipelineState;
///
- protected override bool OnDispose()
+ protected override void OnDispose()
{
this.d3D12RootSignature.Dispose();
this.d3D12PipelineState.Dispose();
-
- return true;
}
}
}
diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs
index 1b232919..5e401f67 100644
--- a/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs
+++ b/src/ComputeSharp/Graphics/Extensions/Interop/AllocatorExtensions.cs
@@ -22,9 +22,9 @@ namespace ComputeSharp.Graphics.Extensions
///
/// The instance in use.
/// A instance suited to be used for cache coherent UMA devices.
- public static UniquePtr CreatePoolForCacheCoherentUMA(this ref D3D12MA_Allocator allocator)
+ public static ReferenceCountPtr CreatePoolForCacheCoherentUMA(this ref D3D12MA_Allocator allocator)
{
- using UniquePtr pool = default;
+ using ReferenceCountPtr pool = default;
D3D12MA_POOL_DESC poolDesc = default;
poolDesc.HeapProperties.CreationNodeMask = 1;
diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs
index 9287ed3f..87462db5 100644
--- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs
+++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs
@@ -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
///
internal static unsafe class ID3D12DeviceExtensions
{
+ ///
+ /// Creates a instance for a given device.
+ ///
+ /// The target to use to create the allocator.
+ /// The that was created from.
+ /// A instance to create resources on the device in use.
+ public static ReferenceCountPtr CreateAllocator(this ref ID3D12Device d3D12Device, IDXGIAdapter* dxgiAdapter)
+ {
+ using ReferenceCountPtr 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();
+ }
+
///
/// Creates a new for a given device.
///
diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs
index 3e47fad7..f11840c8 100644
--- a/src/ComputeSharp/Graphics/GraphicsDevice.cs
+++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs
@@ -76,12 +76,12 @@ namespace ComputeSharp
///
/// The in use associated to the current device.
///
- private UniquePtr allocator;
+ private ReferenceCountPtr allocator;
///
/// The instance in use, if is .
///
- private UniquePtr pool;
+ private ReferenceCountPtr pool;
///
/// Creates a new instance for the input .
@@ -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);
}
+ ///
+ /// Registers that a new resource has been allocated on the current device.
+ ///
+ internal void RegisterAllocatedResource()
+ {
+ this.pool.AddRef();
+ this.allocator.AddRef();
+ }
+
+ ///
+ /// Unregisters a generic resource that was allocated on the current device.
+ ///
+ internal void UnregisterAllocatedResource()
+ {
+ this.pool.Dispose();
+ this.allocator.Dispose();
+ }
+
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void RentShaderResourceViewDescriptorHandles(
@@ -370,13 +381,8 @@ namespace ComputeSharp
}
///
- 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;
}
///
diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs
index 69bee9a5..dd31c1f0 100644
--- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs
+++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs
@@ -203,11 +203,9 @@ namespace ComputeSharp.Graphics.Helpers
}
///
- protected override bool OnDispose()
+ protected override void OnDispose()
{
this.dxgiFactory4.Dispose();
-
- return true;
}
}
}
diff --git a/src/ComputeSharp/Graphics/Interop/NativeObject.cs b/src/ComputeSharp/Graphics/Interop/NativeObject.cs
index 10bdb1cd..6bb2f3ce 100644
--- a/src/ComputeSharp/Graphics/Interop/NativeObject.cs
+++ b/src/ComputeSharp/Graphics/Interop/NativeObject.cs
@@ -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
///
public abstract class NativeObject : IDisposable
{
+ ///
+ /// Indicates whether or not the current instance has been disposed.
+ ///
+ private volatile int isDisposed;
+
///
/// Releases unmanaged resources and performs other cleanup operations.
///
~NativeObject()
{
- _ = CheckAndDispose();
+ if (Interlocked.CompareExchange(ref this.isDisposed, 1, 0) == 0)
+ {
+ OnDispose();
+ }
}
///
/// Gets whether or not the current instance has already been disposed.
///
- internal bool IsDisposed { get; private set; }
-
- ///
- /// Releases all the native resources for the current instance.
- ///
- public void Dispose()
+ internal bool IsDisposed
{
- if (CheckAndDispose())
- {
- GC.SuppressFinalize(this);
- }
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ get => this.isDisposed != 0;
}
- ///
- /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
- ///
- private bool CheckAndDispose()
+ ///
+ [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(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();
+ }
}
///
/// Releases unmanaged and (optionally) managed resources.
///
- ///
- /// 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 .
- ///
- protected abstract bool OnDispose();
+ protected abstract void OnDispose();
///
/// Throws an if the current instance has been disposed.
diff --git a/src/ComputeSharp/Graphics/Interop/ReferenceCountPtr{T}.cs b/src/ComputeSharp/Graphics/Interop/ReferenceCountPtr{T}.cs
new file mode 100644
index 00000000..cc7d8e51
--- /dev/null
+++ b/src/ComputeSharp/Graphics/Interop/ReferenceCountPtr{T}.cs
@@ -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
+{
+ ///
+ /// A -equivalent type to safely work with pointers to public 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 .
+ ///
+ /// Must be either , , or .
+ ///
+ /// This type is not marked as 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.
+ ///
+ internal unsafe struct ReferenceCountPtr : IDisposable
+ where T : unmanaged
+ {
+ ///
+ /// The raw pointer to an object, if existing.
+ ///
+ private T* pointer;
+
+ ///
+ /// The current reference count - 1 for .
+ ///
+ private volatile int count;
+
+ ///
+ /// Creates a new instance from a raw pointer.
+ ///
+ /// The raw pointer to wrap.
+ public ReferenceCountPtr(T* other)
+ {
+ this.pointer = other;
+ this.count = 0;
+ }
+
+ ///
+ /// Unwraps a instance and returns the internal raw pointer.
+ ///
+ /// The instance to unwrap.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static implicit operator T*(ReferenceCountPtr other)
+ {
+ return other.Get();
+ }
+
+ ///
+ /// Increments the internal reference count.
+ ///
+ public void AddRef()
+ {
+ Interlocked.Increment(ref this.count);
+ }
+
+ ///
+ [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");
+ }
+ }
+ }
+
+ ///
+ /// 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 ReferenceCountPtr Move()
+ {
+ ReferenceCountPtr copy = this;
+
+ this = default;
+
+ return copy;
+ }
+ }
+}
diff --git a/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs b/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs
index 2cec96e1..0a074455 100644
--- a/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs
+++ b/src/ComputeSharp/Graphics/Interop/UniquePtr{T}.cs
@@ -28,7 +28,7 @@ namespace ComputeSharp.Interop
/// The raw pointer to wrap.
public UniquePtr(T* other)
{
- pointer = other;
+ this.pointer = other;
}
///
diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs
index b3bc0bb1..b0aeafa0 100644
--- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs
@@ -80,6 +80,7 @@ namespace ComputeSharp.Resources
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, (ulong)SizeInBytes);
this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource());
+ device.RegisterAllocatedResource();
device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
switch (resourceType)
@@ -167,17 +168,16 @@ namespace ComputeSharp.Resources
}
///
- 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;
}
///
diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs
index 172ee5f9..4ef3b3e7 100644
--- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs
@@ -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
}
///
- 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;
}
///
diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs
index 917675c7..680f7f35 100644
--- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs
@@ -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
}
///
- 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;
}
///
diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs
index d3625203..75b0199a 100644
--- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs
@@ -54,6 +54,8 @@ namespace ComputeSharp.Resources
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, sizeInBytes);
this.d3D12Resource = new ComPtr(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
}
///
- protected override bool OnDispose()
+ protected override void OnDispose()
{
this.d3D12Resource.Dispose();
this.allocation.Dispose();
- return true;
+ if (GraphicsDevice is GraphicsDevice device)
+ {
+ device.UnregisterAllocatedResource();
+ }
}
///
diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs
index 593c0079..462cfcd1 100644
--- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs
@@ -71,6 +71,8 @@ namespace ComputeSharp.Resources
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes);
this.d3D12Resource = new ComPtr(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
}
///
- protected override bool OnDispose()
+ protected override void OnDispose()
{
this.d3D12Resource.Dispose();
this.allocation.Dispose();
- return true;
+ if (GraphicsDevice is GraphicsDevice device)
+ {
+ device.UnregisterAllocatedResource();
+ }
}
///
diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs
index 0f82c7b9..53fe5ae8 100644
--- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs
@@ -74,6 +74,8 @@ namespace ComputeSharp.Resources
this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes);
this.d3D12Resource = new ComPtr(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.
///
public int Height => (int)this.d3D12PlacedSubresourceFootprint.Footprint.Height;
+
///
/// Gets the depth of the current texture.
///
@@ -121,12 +124,15 @@ namespace ComputeSharp.Resources
}
///
- protected override bool OnDispose()
+ protected override void OnDispose()
{
this.d3D12Resource.Dispose();
this.allocation.Dispose();
- return true;
+ if (GraphicsDevice is GraphicsDevice device)
+ {
+ device.UnregisterAllocatedResource();
+ }
}
///
diff --git a/src/ComputeSharp/Shaders/Translation/Models/CachedShader.cs b/src/ComputeSharp/Shaders/Translation/Models/CachedShader.cs
index 8bf11804..cf2664f3 100644
--- a/src/ComputeSharp/Shaders/Translation/Models/CachedShader.cs
+++ b/src/ComputeSharp/Shaders/Translation/Models/CachedShader.cs
@@ -35,11 +35,9 @@ namespace ComputeSharp.Shaders.Translation.Models
public D3D12_SHADER_BYTECODE D3D12ShaderBytecode => new((ID3DBlob*)this.dxcBlobBytecode.Get());
///
- protected override bool OnDispose()
+ protected override void OnDispose()
{
this.dxcBlobBytecode.Dispose();
-
- return true;
}
}
}
diff --git a/tests/ComputeSharp.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs
index 705b166f..bebf29b8 100644
--- a/tests/ComputeSharp.Tests/BufferTests.cs
+++ b/tests/ComputeSharp.Tests/BufferTests.cs
@@ -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 buffer = gpu.AllocateBuffer(bufferType, 128);
+
+ gpu.Dispose();
+ buffer.Dispose();
+ }
+
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ConstantBuffer<>))]
diff --git a/tests/ComputeSharp.Tests/Texture2DTests.cs b/tests/ComputeSharp.Tests/Texture2DTests.cs
index ee23d188..3f54caef 100644
--- a/tests/ComputeSharp.Tests/Texture2DTests.cs
+++ b/tests/ComputeSharp.Tests/Texture2DTests.cs
@@ -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 texture = gpu.AllocateTexture2D(textureType, 10, 10);
+
+ gpu.Dispose();
+ texture.Dispose();
+ }
+
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<>))]
diff --git a/tests/ComputeSharp.Tests/Texture3DTests.cs b/tests/ComputeSharp.Tests/Texture3DTests.cs
index 34a384b7..c860d70f 100644
--- a/tests/ComputeSharp.Tests/Texture3DTests.cs
+++ b/tests/ComputeSharp.Tests/Texture3DTests.cs
@@ -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 texture = gpu.AllocateTexture3D(textureType, 10, 10, 4);
+
+ gpu.Dispose();
+ texture.Dispose();
+ }
+
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture3D<>))]
diff --git a/tests/ComputeSharp.Tests/TransferBufferTests.cs b/tests/ComputeSharp.Tests/TransferBufferTests.cs
index a16c9c3d..c35afee1 100644
--- a/tests/ComputeSharp.Tests/TransferBufferTests.cs
+++ b/tests/ComputeSharp.Tests/TransferBufferTests.cs
@@ -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 buffer = gpu.AllocateTransferBuffer(bufferType, 128);
+
+ gpu.Dispose();
+ buffer.Dispose();
+ }
+
[CombinatorialTestMethod]
[AllDevices]
public void Allocate_UploadBuffer_Copy_Full(Device device)
diff --git a/tests/ComputeSharp.Tests/TransferTexture2DTests.cs b/tests/ComputeSharp.Tests/TransferTexture2DTests.cs
index 2e4a6e58..2a413ecf 100644
--- a/tests/ComputeSharp.Tests/TransferTexture2DTests.cs
+++ b/tests/ComputeSharp.Tests/TransferTexture2DTests.cs
@@ -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 texture = device.Get().AllocateTransferTexture2D(bufferType, 128, 256, allocationMode);
+ using TransferTexture2D texture = device.Get().AllocateTransferTexture2D(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 texture = device.Get().AllocateTransferTexture2D(bufferType, width, height);
+ using TransferTexture2D texture = device.Get().AllocateTransferTexture2D(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 texture = device.Get().AllocateTransferTexture2D(bufferType, 32, 32);
+ using TransferTexture2D texture = device.Get().AllocateTransferTexture2D(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 texture = gpu.AllocateTransferTexture2D(textureType, 32, 32);
+
+ gpu.Dispose();
+ texture.Dispose();
+ }
+
[CombinatorialTestMethod]
[AllDevices]
public void Allocate_UploadTexture2D_Copy_Full(Device device)
diff --git a/tests/ComputeSharp.Tests/TransferTexture3DTests.cs b/tests/ComputeSharp.Tests/TransferTexture3DTests.cs
index ad06735c..302198d0 100644
--- a/tests/ComputeSharp.Tests/TransferTexture3DTests.cs
+++ b/tests/ComputeSharp.Tests/TransferTexture3DTests.cs
@@ -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 texture = device.Get().AllocateTransferTexture3D(bufferType, 128, 256, 32, allocationMode);
+ using TransferTexture3D texture = device.Get().AllocateTransferTexture3D(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 texture = device.Get().AllocateTransferTexture3D(bufferType, width, height, depth);
+ using TransferTexture3D texture = device.Get().AllocateTransferTexture3D(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 texture = device.Get().AllocateTransferTexture3D(bufferType, 32, 32, 16);
+ using TransferTexture3D texture = device.Get().AllocateTransferTexture3D(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 texture = gpu.AllocateTransferTexture3D(textureType, 32, 32, 16);
+
+ gpu.Dispose();
+ texture.Dispose();
+ }
+
[CombinatorialTestMethod]
[AllDevices]
public void Allocate_UploadTexture3D_Copy_Full(Device device)