From edddb72ddddabe4d62d4bf288444f17bf7dc258d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 26 Jul 2022 02:13:22 +0200 Subject: [PATCH] Add eager disposal of cached pipeline states when disposing devices --- src/ComputeSharp/Graphics/GraphicsDevice.cs | 39 ++++++++++ .../Shaders/Loading/PipelineDataLoader{T}.cs | 3 + .../DeviceDisposalTests.cs | 76 ++++++++++++++++++- 3 files changed, 117 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index a516d4a0..c988f565 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -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 /// private HRESULT deviceRemovedReason; + /// + /// The list of cached objects for the current device. + /// + /// + /// A new entry is added to this list every time a shader is dispatched using this device. These cached items + /// are stored via a , 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. + /// + private List cachedPipelineData; + /// /// Raised whenever the device is lost. /// @@ -170,6 +183,7 @@ public sealed unsafe partial class GraphicsDevice : NativeObject #endif this.deviceRemovedReason = S.S_OK; + this.cachedPipelineData = new List(); RegisterDeviceLostCallback(this, out this.deviceHandle, out this.deviceRemovedEvent, out this.deviceRemovedWaitHandle); } @@ -337,6 +351,21 @@ public sealed unsafe partial class GraphicsDevice : NativeObject #endif } + /// + /// Registers a object for the current device to enable early disposal. + /// + /// The instance just loaded to run a shader. + 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); + } + } + /// [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(); + } } /// diff --git a/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs b/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs index 4af52e05..415e9b89 100644 --- a/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs +++ b/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs @@ -98,5 +98,8 @@ internal static class PipelineDataLoader 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); } } diff --git a/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs b/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs index 113429b1..174b7b0d 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs +++ b/tests/ComputeSharp.Tests.DeviceLost/DeviceDisposalTests.cs @@ -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 d3D12Device = default; + + using (GraphicsDevice graphicsDevice = device.Get()) + { + using var buffer = graphicsDevice.AllocateReadWriteBuffer(128); + + using (ComputeContext context = graphicsDevice.CreateComputeContext()) + { + context.For(buffer.Length, new InitializeShader(buffer)); + } + + InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof(), (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 buffer; + + public void Execute() + { + buffer[ThreadIds.X] = ThreadIds.X; + } + } + + [CombinatorialTestMethod] + [AllDevices] + public unsafe void DeviceDisposal_WithPixelShader_ReferenceCounting(Device device) + { + using ComPtr d3D12Device = default; + + using (GraphicsDevice graphicsDevice = device.Get()) + { + using var texture = graphicsDevice.AllocateReadWriteTexture2D(128, 128); + + using (ComputeContext context = graphicsDevice.CreateComputeContext()) + { + context.ForEach(texture, default(HelloWorldShader)); + } + + InteropServices.GetID3D12Device(graphicsDevice, Windows.__uuidof(), (void**)d3D12Device.GetAddressOf()); + } + + _ = d3D12Device.Get()->AddRef(); + + uint refCount = d3D12Device.Get()->Release(); + + Assert.AreEqual(refCount, 1u); + } + + [EmbeddedBytecode(DispatchAxis.XY)] + private partial struct HelloWorldShader : IPixelShader + { + 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); + } + } }