diff --git a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs
index 3afef990..dd1b884f 100644
--- a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs
+++ b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs
@@ -108,6 +108,13 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable
/// The returned value.
public void Return(in ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles)
{
+ if (d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle.ptr == 0 &&
+ d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle.ptr == 0 &&
+ d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible.ptr == 0)
+ {
+ return;
+ }
+
lock (this.d3D12DescriptorHandlePairs)
{
default(InvalidOperationException).ThrowIf(this.size >= DescriptorsPerHeap);
@@ -129,4 +136,4 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable
this.d3D12DescriptorHeap.Dispose();
this.d3D12DescriptorHeapNonShaderVisible.Dispose();
}
-}
\ No newline at end of file
+}
diff --git a/tests/ComputeSharp.Tests/InteropServicesTests.Sharing.cs b/tests/ComputeSharp.Tests/InteropServicesTests.Sharing.cs
index 2e9c323e..175a5790 100644
--- a/tests/ComputeSharp.Tests/InteropServicesTests.Sharing.cs
+++ b/tests/ComputeSharp.Tests/InteropServicesTests.Sharing.cs
@@ -2,6 +2,7 @@ using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
+using ComputeSharp.Graphics.Commands.Interop;
using ComputeSharp.Interop;
using ComputeSharp.Tests.Attributes;
using ComputeSharp.Tests.Extensions;
@@ -16,6 +17,26 @@ public unsafe partial class InteropServicesTests
[DllImport("kernel32", ExactSpelling = true)]
private static extern int CloseHandle(nint hObject);
+ [CombinatorialTestMethod]
+ [AllDevices]
+ public void DescriptorHandleAllocator_DefaultReturnIsIgnoredAndDuplicateReturnThrows(Device device)
+ {
+ ID3D12DescriptorHandleAllocator allocator = new(device.Get().D3D12Device);
+
+ try
+ {
+ allocator.Return(default);
+ allocator.Rent(out ID3D12ResourceDescriptorHandles handles);
+ allocator.Return(in handles);
+
+ Assert.ThrowsExactly(() => allocator.Return(in handles));
+ }
+ finally
+ {
+ allocator.Dispose();
+ }
+ }
+
[CombinatorialTestMethod]
[AllDevices]
public void SharedReadWriteTexture2D_ComputeShaderRoundTrip(Device device)