Add eager disposal of cached pipeline states when disposing devices

このコミットが含まれているのは:
Sergio Pedri committed 2022-07-31 23:26:13 +02:00
1 親 9dd707a835
コミット edddb72ddd
3ファイル変更
+117 -1

検索条件に一致するファイルはありません

+39
ファイルの表示
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -10,6 +11,7 @@ using ComputeSharp.Core.Extensions;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Interop;
using ComputeSharp.Shaders.Models;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using static TerraFX.Interop.DirectX.D3D12_COMMAND_LIST_TYPE;
@@ -107,6 +109,17 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
/// </summary>
private HRESULT deviceRemovedReason;
/// <summary>
/// The list of cached <see cref="PipelineData"/> objects for the current device.
/// </summary>
/// <remarks>
/// A new entry is added to this list every time a shader is dispatched using this device. These cached items
/// are stored via a <see cref="ConditionalWeakTable{TKey, TValue}"/>, meaning they will be collected automatically
/// when the device is collected. But, due to the fact that could happen at any time, a list is required to guarantee
/// the additional references added by the native objects in the pipeline model can be released immediately.
/// </remarks>
private List<PipelineData> cachedPipelineData;
/// <summary>
/// Raised whenever the device is lost.
/// </summary>
@@ -170,6 +183,7 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
#endif
this.deviceRemovedReason = S.S_OK;
this.cachedPipelineData = new List<PipelineData>();
RegisterDeviceLostCallback(this, out this.deviceHandle, out this.deviceRemovedEvent, out this.deviceRemovedWaitHandle);
}
@@ -337,6 +351,21 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
#endif
}
/// <summary>
/// Registers a <see cref="PipelineData"/> object for the current device to enable early disposal.
/// </summary>
/// <param name="pipelineData">The <see cref="PipelineData"/> instance just loaded to run a shader.</param>
internal void RegisterPipelineData(PipelineData pipelineData)
{
// This is only used when first initializing a pipeline data for a given shader.
// Adding an item is very quick, so we can just use a lock and not a concurrent
// list. That would've added more overhead given the low contention in this case.
lock (this.cachedPipelineData)
{
this.cachedPipelineData.Add(pipelineData);
}
}
/// <inheritdoc cref="ID3D12DescriptorHandleAllocator.Rent"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void RentShaderResourceViewDescriptorHandles(out ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles)
@@ -421,6 +450,16 @@ public sealed unsafe partial class GraphicsDevice : NativeObject
#endif
UnregisterDeviceLostCallback(this);
// Explicitly release the cached pipeline objects as well.
// Locking is not needed here, as this method is only invoked
// when the device has been disposed and no other usage is active
// for the device (ie. no lease exists), meaning that no other
// thread could potentially add a new pipeline data concurrently.
foreach (PipelineData pipelineData in this.cachedPipelineData)
{
pipelineData.Dispose();
}
}
/// <inheritdoc/>
+3
ファイルの表示
@@ -98,5 +98,8 @@ internal static class PipelineDataLoader<T>
pipelineData = new PipelineData(d3D12RootSignature.Get(), d3D12PipelineState.Get());
shaderData.CachedPipelines.Add(device, pipelineData);
// Register the newly created pipeline data to enable early disposal
device.RegisterPipelineData(pipelineData);
}
}
+75 -1
ファイルの表示
@@ -12,7 +12,7 @@ namespace ComputeSharp.Tests.DeviceLost;
[TestClass]
[TestCategory("DeviceDisposal")]
public class DeviceDisposalTests
public partial class DeviceDisposalTests
{
[TestMethod]
public unsafe void DeviceDisposal_GetDefault_ReferenceCounting()
@@ -137,4 +137,78 @@ public class DeviceDisposalTests
Assert.AreEqual(refCount, 1u);
}
[CombinatorialTestMethod]
[AllDevices]
public unsafe void DeviceDisposal_WithComputeShader_ReferenceCounting(Device device)
{
using ComPtr<ID3D12Device> d3D12Device = default;
using (GraphicsDevice graphicsDevice = device.Get())
{
using var buffer = graphicsDevice.AllocateReadWriteBuffer<float>(128);
using (ComputeContext context = graphicsDevice.CreateComputeContext())
{
context.For(buffer.Length, new InitializeShader(buffer));
}
InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof<ID3D12Device>(), (void**)d3D12Device.GetAddressOf());
}
_ = d3D12Device.Get()->AddRef();
uint refCount = d3D12Device.Get()->Release();
Assert.AreEqual(refCount, 1u);
}
[EmbeddedBytecode(DispatchAxis.X)]
[AutoConstructor]
private partial struct InitializeShader : IComputeShader
{
private readonly ReadWriteBuffer<float> buffer;
public void Execute()
{
buffer[ThreadIds.X] = ThreadIds.X;
}
}
[CombinatorialTestMethod]
[AllDevices]
public unsafe void DeviceDisposal_WithPixelShader_ReferenceCounting(Device device)
{
using ComPtr<ID3D12Device> d3D12Device = default;
using (GraphicsDevice graphicsDevice = device.Get())
{
using var texture = graphicsDevice.AllocateReadWriteTexture2D<Rgba32, float4>(128, 128);
using (ComputeContext context = graphicsDevice.CreateComputeContext())
{
context.ForEach(texture, default(HelloWorldShader));
}
InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof<ID3D12Device>(), (void**)d3D12Device.GetAddressOf());
}
_ = d3D12Device.Get()->AddRef();
uint refCount = d3D12Device.Get()->Release();
Assert.AreEqual(refCount, 1u);
}
[EmbeddedBytecode(DispatchAxis.XY)]
private partial struct HelloWorldShader : IPixelShader<float4>
{
public float4 Execute()
{
float2 uv = ThreadIds.Normalized.XY;
float3 col = 0.5f + 0.5f * Hlsl.Cos(new float3(uv, uv.X) + new float3(0, 2, 4));
return new(col, 1f);
}
}
}