diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs
index da82e0fd..bf9be010 100644
--- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs
+++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs
@@ -149,7 +149,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
}
///
- protected override void OnDispose()
+ protected override bool OnDispose()
{
this.d3D12Resource.Dispose();
@@ -157,6 +157,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
{
GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle);
}
+
+ return true;
}
///
diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs
index 5e120c3b..dd554edc 100644
--- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs
+++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs
@@ -253,7 +253,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
}
///
- protected override void OnDispose()
+ protected override bool OnDispose()
{
this.d3D12Resource.Dispose();
@@ -261,6 +261,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
{
GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle);
}
+
+ return true;
}
///
diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs
index 714c8341..30b99bd8 100644
--- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs
+++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs
@@ -276,7 +276,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
}
///
- protected override void OnDispose()
+ protected override bool OnDispose()
{
this.d3D12Resource.Dispose();
@@ -284,6 +284,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
{
GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle);
}
+
+ return true;
}
///
diff --git a/src/ComputeSharp.Graphics/Commands/PipelineData.cs b/src/ComputeSharp.Graphics/Commands/PipelineData.cs
index cfa68aeb..346daef9 100644
--- a/src/ComputeSharp.Graphics/Commands/PipelineData.cs
+++ b/src/ComputeSharp.Graphics/Commands/PipelineData.cs
@@ -41,10 +41,12 @@ namespace ComputeSharp.Graphics.Commands
public ID3D12PipelineState* D3D12PipelineState => this.d3D12PipelineState;
///
- protected override void OnDispose()
+ protected override bool OnDispose()
{
this.d3D12RootSignature.Dispose();
this.d3D12PipelineState.Dispose();
+
+ return true;
}
}
}
diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs
index e0cbc585..a5e18802 100644
--- a/src/ComputeSharp.Graphics/GraphicsDevice.cs
+++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs
@@ -210,9 +210,12 @@ namespace ComputeSharp.Graphics
}
///
- protected override void OnDispose()
+ protected override bool OnDispose()
{
- if (DeviceHelper.GetDefaultDeviceLuid() == Luid) return;
+ if (DeviceHelper.GetDefaultDeviceLuid() == Luid)
+ {
+ return false;
+ }
DeviceHelper.NotifyDisposedDevice(this);
@@ -224,6 +227,8 @@ namespace ComputeSharp.Graphics
this.computeCommandAllocatorPool.Dispose();
this.copyCommandAllocatorPool.Dispose();
this.shaderResourceViewDescriptorAllocator.Dispose();
+
+ return true;
}
///
diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs
index ffcb7958..0c6b73cb 100644
--- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs
+++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs
@@ -155,9 +155,11 @@ namespace ComputeSharp.Graphics.Helpers
}
///
- protected override void OnDispose()
+ protected override bool OnDispose()
{
this.dxgiFactory4.Dispose();
+
+ return true;
}
}
}
diff --git a/src/ComputeSharp.Graphics/Interop/NativeObject.cs b/src/ComputeSharp.Graphics/Interop/NativeObject.cs
index c82228ab..c0b4c75e 100644
--- a/src/ComputeSharp.Graphics/Interop/NativeObject.cs
+++ b/src/ComputeSharp.Graphics/Interop/NativeObject.cs
@@ -13,7 +13,7 @@ namespace ComputeSharp.Graphics.Interop
///
~NativeObject()
{
- CheckAndDispose();
+ _ = CheckAndDispose();
}
///
@@ -26,28 +26,33 @@ namespace ComputeSharp.Graphics.Interop
///
public void Dispose()
{
- GC.SuppressFinalize(this);
-
- CheckAndDispose();
+ if (CheckAndDispose())
+ {
+ GC.SuppressFinalize(this);
+ }
}
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
- private void CheckAndDispose()
+ private bool CheckAndDispose()
{
if (!IsDisposed)
{
- IsDisposed = true;
-
- OnDispose();
+ return IsDisposed = OnDispose();
}
+
+ return true;
}
///
/// Releases unmanaged and (optionally) managed resources.
///
- protected abstract void OnDispose();
+ ///
+ /// 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();
///
/// Throws an if the current instance has been disposed.
diff --git a/src/ComputeSharp.Shaders/Translation/Interop/IDxcBlobObject.cs b/src/ComputeSharp.Shaders/Translation/Interop/IDxcBlobObject.cs
index a328ddf9..bb5efd3e 100644
--- a/src/ComputeSharp.Shaders/Translation/Interop/IDxcBlobObject.cs
+++ b/src/ComputeSharp.Shaders/Translation/Interop/IDxcBlobObject.cs
@@ -28,9 +28,11 @@ namespace ComputeSharp.Shaders.Translation.Interop
public D3D12_SHADER_BYTECODE D3D12ShaderBytecode => new((ID3DBlob*)this.dxcBlob.Get());
///
- protected override void OnDispose()
+ protected override bool OnDispose()
{
this.dxcBlob.Dispose();
+
+ return true;
}
}
}
diff --git a/src/ComputeSharp/Gpu.cs b/src/ComputeSharp/Gpu.cs
index 1e3610b2..c138b30b 100644
--- a/src/ComputeSharp/Gpu.cs
+++ b/src/ComputeSharp/Gpu.cs
@@ -24,6 +24,8 @@ namespace ComputeSharp
///
/// Gets the default instance for the current machine.
+ /// This instance cannot be manually disposed - attempting to do so is safe and it will
+ /// not cause an exception, but it will simply do nothing and not dispose the device.
///
/// Thrown when a default device is not available.
/// Make sure to check before accessing this property.
diff --git a/tests/ComputeSharp.Tests/InitializationTests.cs b/tests/ComputeSharp.Tests/InitializationTests.cs
index 9fc2f1d0..1b3a19e2 100644
--- a/tests/ComputeSharp.Tests/InitializationTests.cs
+++ b/tests/ComputeSharp.Tests/InitializationTests.cs
@@ -25,6 +25,16 @@ namespace ComputeSharp.Tests
Assert.IsTrue(Gpu.Default.WavefrontSize != 0);
}
+ [TestMethod]
+ public void DisposeDefault()
+ {
+ using var before = Gpu.Default.AllocateReadOnlyBuffer(128);
+
+ Gpu.Default.Dispose();
+
+ using var after = Gpu.Default.AllocateReadOnlyBuffer(128);
+ }
+
[TestMethod]
public void EnumerateDevices()
{