diff --git a/src/ComputeSharp/Graphics/Commands/CommandList.cs b/src/ComputeSharp/Graphics/Commands/CommandList.cs
index 706f8258..881c3c14 100644
--- a/src/ComputeSharp/Graphics/Commands/CommandList.cs
+++ b/src/ComputeSharp/Graphics/Commands/CommandList.cs
@@ -156,11 +156,11 @@ internal unsafe struct CommandList : IDisposable
return this.device.ExecuteCommandListAsync(ref this);
}
- public void ExecuteWithoutWaiting()
+ public void ExecuteWithoutWaiting(ref GraphicsResourceLeaseSet? resourceLeases)
{
this.d3D12GraphicsCommandList.Get()->Close().Assert();
- this.device.ExecuteCommandListWithoutWaiting(ref this);
+ this.device.ExecuteCommandListWithoutWaiting(ref this, ref resourceLeases);
}
///
diff --git a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs
index 3bccd574..f1e9aad7 100644
--- a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs
+++ b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs
@@ -75,7 +75,7 @@ internal readonly unsafe struct ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE d3
}
}
- public void ReturnWhenCompleted(ID3D12GraphicsCommandList* d3D12CommandList, ID3D12CommandAllocator* d3D12CommandAllocator, ulong fenceValue)
+ public void ReturnWhenCompleted(ID3D12GraphicsCommandList* d3D12CommandList, ID3D12CommandAllocator* d3D12CommandAllocator, ulong fenceValue, GraphicsResourceLeaseSet? resourceLeases)
{
lock (this.d3D12CommandListBundleQueue)
{
@@ -90,10 +90,12 @@ internal readonly unsafe struct ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE d3
d3D12Fence->SetEventOnCompletion(pendingD3D12CommandListBundle.FenceValue, default).Assert();
}
+ pendingD3D12CommandListBundle.ResourceLeases?.Release();
+
this.d3D12CommandListBundleQueue.Enqueue(pendingD3D12CommandListBundle.D3D12CommandListBundle);
}
- this.pendingD3D12CommandListBundleQueue.Enqueue(new PendingD3D12CommandListBundle(d3D12CommandList, d3D12CommandAllocator, fenceValue));
+ this.pendingD3D12CommandListBundleQueue.Enqueue(new PendingD3D12CommandListBundle(d3D12CommandList, d3D12CommandAllocator, fenceValue, resourceLeases));
}
}
@@ -122,6 +124,8 @@ internal readonly unsafe struct ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE d3
foreach (PendingD3D12CommandListBundle current in this.pendingD3D12CommandListBundleQueue)
{
+ current.ResourceLeases?.Release();
+
_ = current.D3D12CommandListBundle.D3D12CommandList->Release();
_ = current.D3D12CommandListBundle.D3D12CommandAllocator->Release();
}
@@ -139,6 +143,9 @@ internal readonly unsafe struct ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE d3
pendingD3D12CommandListBundle.FenceValue <= completedValue)
{
_ = this.pendingD3D12CommandListBundleQueue.Dequeue();
+
+ pendingD3D12CommandListBundle.ResourceLeases?.Release();
+
this.d3D12CommandListBundleQueue.Enqueue(pendingD3D12CommandListBundle.D3D12CommandListBundle);
}
}
@@ -182,10 +189,12 @@ internal readonly unsafe struct ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE d3
public readonly ID3D12CommandAllocator* D3D12CommandAllocator = d3D12CommandAllocator;
}
- private readonly struct PendingD3D12CommandListBundle(ID3D12GraphicsCommandList* d3D12CommandList, ID3D12CommandAllocator* d3D12CommandAllocator, ulong fenceValue)
+ private readonly struct PendingD3D12CommandListBundle(ID3D12GraphicsCommandList* d3D12CommandList, ID3D12CommandAllocator* d3D12CommandAllocator, ulong fenceValue, GraphicsResourceLeaseSet? resourceLeases)
{
public readonly D3D12CommandListBundle D3D12CommandListBundle = new(d3D12CommandList, d3D12CommandAllocator);
public readonly ulong FenceValue = fenceValue;
+
+ public readonly GraphicsResourceLeaseSet? ResourceLeases = resourceLeases;
}
}
diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs b/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs
index a81956ed..9528c557 100644
--- a/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs
+++ b/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs
@@ -98,7 +98,7 @@ unsafe partial class GraphicsDevice
commandList.DetachD3D12CommandAllocator()).AsValueTask();
}
- internal void ExecuteCommandListWithoutWaiting(ref CommandList commandList)
+ internal void ExecuteCommandListWithoutWaiting(ref CommandList commandList, ref GraphicsResourceLeaseSet? resourceLeases)
{
ulong updatedFenceValue = ExecuteCommandListCore(
ref commandList,
@@ -107,10 +107,15 @@ unsafe partial class GraphicsDevice
ref this.nextD3D12ComputeFenceValue,
this.d3D12ComputeCommandQueueLock);
+ GraphicsResourceLeaseSet? pendingResourceLeases = resourceLeases;
+
+ resourceLeases = null;
+
this.computeCommandListPool.ReturnWhenCompleted(
commandList.DetachD3D12CommandList(),
commandList.DetachD3D12CommandAllocator(),
- updatedFenceValue);
+ updatedFenceValue,
+ pendingResourceLeases);
}
private static ulong ExecuteCommandListCore(
diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs
index b2f4284b..1f552c13 100644
--- a/src/ComputeSharp/Shaders/ComputeContext.cs
+++ b/src/ComputeSharp/Shaders/ComputeContext.cs
@@ -5,6 +5,7 @@ using System.Threading.Tasks;
using ComputeSharp.Descriptors;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Graphics.Extensions;
+using ComputeSharp.Interop;
using ComputeSharp.Resources.Interop;
using ComputeSharp.Shaders.Dispatching;
using ComputeSharp.Shaders.Loading;
@@ -36,6 +37,8 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
///
private CommandList commandList;
+ private GraphicsResourceLeaseSet? resourceLeases;
+
private bool isSubmitted;
///
@@ -46,6 +49,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
{
this.device = device;
this.commandList = default;
+ this.resourceLeases = null;
this.isSubmitted = false;
// Increment the reference count for the device. This has to be released when disposing the context.
@@ -62,7 +66,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
{
default(InvalidOperationException).ThrowIf(this.device is null);
- return this.device;
+ return this.device!;
}
}
@@ -166,7 +170,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsZ, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
- PipelineData pipelineData = PipelineDataLoader.GetPipelineData(this.device);
+ PipelineData pipelineData = PipelineDataLoader.GetPipelineData(this.device!);
ref CommandList commandList = ref GetCommandList(pipelineData.D3D12PipelineState);
@@ -176,7 +180,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
T.LoadConstantBuffer(in shader, ref dataLoader, x, y, z);
- D3D12GraphicsCommandListGraphicsResourceLoader graphicsResourceLoader = new(commandList.D3D12GraphicsCommandList, this.device, rootParameterOffset: 1);
+ D3D12GraphicsCommandListGraphicsResourceLoader graphicsResourceLoader = new(commandList.D3D12GraphicsCommandList, this.device!, rootParameterOffset: 1, this.resourceLeases!);
T.LoadGraphicsResources(in shader, ref graphicsResourceLoader);
@@ -204,7 +208,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsX, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION);
default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
- PipelineData pipelineData = PipelineDataLoader.GetPipelineData(this.device);
+ PipelineData pipelineData = PipelineDataLoader.GetPipelineData(this.device!);
ref CommandList commandList = ref GetCommandList(pipelineData.D3D12PipelineState);
@@ -214,14 +218,18 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
T.LoadConstantBuffer(in shader, ref constantBufferLoader, x, y, 1);
- D3D12GraphicsCommandListGraphicsResourceLoader graphicsResourceLoader = new(commandList.D3D12GraphicsCommandList, this.device, rootParameterOffset: 2);
+ D3D12GraphicsCommandListGraphicsResourceLoader graphicsResourceLoader = new(commandList.D3D12GraphicsCommandList, this.device!, rootParameterOffset: 2, this.resourceLeases!);
T.LoadGraphicsResources(in shader, ref graphicsResourceLoader);
+ _ = ((ID3D12ReadOnlyResource)texture).ValidateAndGetID3D12Resource(this.device!, out ReferenceTracker.Lease textureLease);
+
+ this.resourceLeases!.Add(textureLease);
+
// Load the implicit output texture
commandList.D3D12GraphicsCommandList->SetComputeRootDescriptorTable(
1,
- ((ID3D12ReadOnlyResource)texture).ValidateAndGetGpuDescriptorHandle(this.device));
+ ((ID3D12ReadOnlyResource)texture).ValidateAndGetGpuDescriptorHandle(this.device!));
commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, 1);
}
@@ -259,8 +267,14 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
this.device = null;
+ GraphicsResourceLeaseSet? resourceLeases = this.resourceLeases;
+
+ this.resourceLeases = null;
+
if (!this.commandList.IsAllocated)
{
+ resourceLeases?.Release();
+
device.GetReferenceTracker().DangerousRelease();
return;
@@ -272,6 +286,8 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
}
finally
{
+ resourceLeases?.Release();
+
device.GetReferenceTracker().DangerousRelease();
}
}
@@ -291,8 +307,14 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
this.device = null;
+ GraphicsResourceLeaseSet? resourceLeases = this.resourceLeases;
+
+ this.resourceLeases = null;
+
if (!this.commandList.IsAllocated)
{
+ resourceLeases?.Release();
+
device.GetReferenceTracker().DangerousRelease();
return ValueTask.CompletedTask;
@@ -300,7 +322,31 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
try
{
- return this.commandList.ExecuteAndWaitForCompletionAsync();
+ ValueTask executeTask = this.commandList.ExecuteAndWaitForCompletionAsync();
+
+ if (resourceLeases is null)
+ {
+ return executeTask;
+ }
+
+ if (executeTask.IsCompletedSuccessfully)
+ {
+ resourceLeases.Release();
+
+ return executeTask;
+ }
+
+ ValueTask releaseTask = ReleaseWhenCompletedAsync(executeTask, resourceLeases);
+
+ resourceLeases = null;
+
+ return releaseTask;
+ }
+ catch
+ {
+ resourceLeases?.Release();
+
+ throw;
}
finally
{
@@ -308,6 +354,18 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
}
}
+ private static async ValueTask ReleaseWhenCompletedAsync(ValueTask executeTask, GraphicsResourceLeaseSet resourceLeases)
+ {
+ try
+ {
+ await executeTask.ConfigureAwait(false);
+ }
+ finally
+ {
+ resourceLeases.Release();
+ }
+ }
+
///
/// 現在のコンテキストに記録されたコマンドをGPUへ送信します。
///
@@ -327,8 +385,14 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
this.device = null;
this.isSubmitted = true;
+ GraphicsResourceLeaseSet? resourceLeases = this.resourceLeases;
+
+ this.resourceLeases = null;
+
if (!this.commandList.IsAllocated)
{
+ resourceLeases?.Release();
+
device.GetReferenceTracker().DangerousRelease();
return;
@@ -336,10 +400,12 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
try
{
- this.commandList.ExecuteWithoutWaiting();
+ this.commandList.ExecuteWithoutWaiting(ref resourceLeases);
}
finally
{
+ resourceLeases?.Release();
+
device.GetReferenceTracker().DangerousRelease();
}
}
@@ -386,8 +452,23 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
else
{
commandList = new CommandList(this.device!, pipelineState);
+
+ ref GraphicsResourceLeaseSet? resourceLeases = ref Unsafe.AsRef(in this.resourceLeases);
+
+ resourceLeases ??= GraphicsResourceLeaseSet.Rent();
}
return ref commandList;
}
+
+ internal readonly void TrackResourceLease(ref ReferenceTracker.Lease lease)
+ {
+ ref GraphicsResourceLeaseSet? resourceLeases = ref Unsafe.AsRef(in this.resourceLeases);
+
+ resourceLeases ??= GraphicsResourceLeaseSet.Rent();
+
+ resourceLeases.Add(lease);
+
+ lease = default;
+ }
}
diff --git a/src/ComputeSharp/Shaders/Dispatching/D3D12GraphicsCommandListGraphicsResourceLoader.cs b/src/ComputeSharp/Shaders/Dispatching/D3D12GraphicsCommandListGraphicsResourceLoader.cs
index df646f23..b587b1dc 100644
--- a/src/ComputeSharp/Shaders/Dispatching/D3D12GraphicsCommandListGraphicsResourceLoader.cs
+++ b/src/ComputeSharp/Shaders/Dispatching/D3D12GraphicsCommandListGraphicsResourceLoader.cs
@@ -1,5 +1,7 @@
using System;
using System.Runtime.CompilerServices;
+using ComputeSharp.Graphics.Commands;
+using ComputeSharp.Interop;
using ComputeSharp.Resources.Interop;
using ComputeSharp.Descriptors;
using ComputeSharp.Win32;
@@ -39,21 +41,26 @@ internal readonly unsafe struct D3D12GraphicsCommandListGraphicsResourceLoader :
///
private readonly uint rootParameterOffset;
+ private readonly GraphicsResourceLeaseSet resourceLeases;
+
///
/// Creates a new instance.
///
/// The object to use.
/// The instance that is tied to.
/// The offset into the compute root descriptor table for loaded resources.
+ /// 読み込んだリソースのリースを保持する インスタンス。
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal D3D12GraphicsCommandListGraphicsResourceLoader(
ID3D12GraphicsCommandList* d3D12GraphicsCommandList,
GraphicsDevice graphicsDevice,
- uint rootParameterOffset)
+ uint rootParameterOffset,
+ GraphicsResourceLeaseSet resourceLeases)
{
this.d3D12GraphicsCommandList = d3D12GraphicsCommandList;
this.graphicsDevice = graphicsDevice;
this.rootParameterOffset = rootParameterOffset;
+ this.resourceLeases = resourceLeases;
}
///
@@ -68,6 +75,10 @@ internal readonly unsafe struct D3D12GraphicsCommandListGraphicsResourceLoader :
return;
}
+ _ = readOnlyResource.ValidateAndGetID3D12Resource(this.graphicsDevice, out ReferenceTracker.Lease lease);
+
+ this.resourceLeases.Add(lease);
+
this.d3D12GraphicsCommandList->SetComputeRootDescriptorTable(
RootParameterIndex: this.rootParameterOffset + index,
BaseDescriptor: readOnlyResource.ValidateAndGetGpuDescriptorHandle(this.graphicsDevice));