From 49c5690ff8ce1e4df664bd9decf95bed7f9c2df7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 3 Jan 2022 10:23:14 +0100 Subject: [PATCH] Avoid blocking on UI thread in shader panels --- src/ComputeSharp.UI/ComputeSharp.UI.projitems | 1 + .../Extensions/SemaphoreSlimExtensions.cs | 56 +++++++++++++++++++ .../Helpers/SwapChainManager.Rendering.cs | 55 +++++++++++------- .../Helpers/SwapChainManager.cs | 34 +++++++---- 4 files changed, 114 insertions(+), 32 deletions(-) create mode 100644 src/ComputeSharp.UI/Extensions/SemaphoreSlimExtensions.cs diff --git a/src/ComputeSharp.UI/ComputeSharp.UI.projitems b/src/ComputeSharp.UI/ComputeSharp.UI.projitems index 694e6245..2f58b838 100644 --- a/src/ComputeSharp.UI/ComputeSharp.UI.projitems +++ b/src/ComputeSharp.UI/ComputeSharp.UI.projitems @@ -13,6 +13,7 @@ + diff --git a/src/ComputeSharp.UI/Extensions/SemaphoreSlimExtensions.cs b/src/ComputeSharp.UI/Extensions/SemaphoreSlimExtensions.cs new file mode 100644 index 00000000..fd2ab3d0 --- /dev/null +++ b/src/ComputeSharp.UI/Extensions/SemaphoreSlimExtensions.cs @@ -0,0 +1,56 @@ +using System; +using System.ComponentModel; +using System.Threading; +using System.Threading.Tasks; + +#pragma warning disable CS0618 + +#if WINDOWS_UWP +namespace ComputeSharp.Uwp.Extensions; +#else +namespace ComputeSharp.WinUI.Extensions; +#endif + +/// +/// A helper type for . +/// +internal static class SemaphoreSlimExtensions +{ + /// + /// Acquires a lock for the current instance, that is automatically released outside the block. + /// + public static async ValueTask LockAsync(this SemaphoreSlim semaphore) + { + await semaphore.WaitAsync().ConfigureAwait(false); + + return new(semaphore); + } + + /// + /// Private class that implements the automatic release of the semaphore + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public readonly struct Lock : IDisposable + { + /// + /// The underlying semaphore used by this instance. + /// + private readonly SemaphoreSlim semaphore; + + /// + /// Creates a new instance. + /// + /// The instance to wrap. + [Obsolete("Use AsyncMutex.LockAsync instead.")] + public Lock(SemaphoreSlim semaphore) + { + this.semaphore = semaphore; + } + + /// + public void Dispose() + { + this.semaphore.Release(); + } + } +} diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs index fd265119..a9344b05 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs @@ -2,6 +2,11 @@ using System.Diagnostics; using System.Runtime.CompilerServices; using System.Threading; +#if WINDOWS_UWP +using ComputeSharp.Uwp.Extensions; +#else +using ComputeSharp.WinUI.Extensions; +#endif using Microsoft.Toolkit.Diagnostics; #pragma warning disable CS0420 @@ -20,16 +25,17 @@ partial class SwapChainManager /// /// The instance to use, if available. /// The instance to use to render frames. - public void StartRenderLoop(IFrameRequestQueue? frameRequestQueue, IShaderRunner shaderRunner) + public async void StartRenderLoop(IFrameRequestQueue? frameRequestQueue, IShaderRunner shaderRunner) { ThrowIfDisposed(); Guard.IsNotNull(shaderRunner, nameof(shaderRunner)); - lock (this.renderLock) + using (await this.setupSemaphore.LockAsync()) { this.renderCancellationTokenSource?.Cancel(); - this.renderThread?.Join(); + + await this.renderSemaphore.WaitAsync(); Thread newRenderThread = new(static args => ((SwapChainManager)args!).SwitchAndStartRenderLoop()); @@ -37,6 +43,7 @@ partial class SwapChainManager this.shaderRunner = shaderRunner; this.renderCancellationTokenSource = new CancellationTokenSource(); this.renderThread = newRenderThread; + this.renderSemaphore = new SemaphoreSlim(0, 1); newRenderThread.Start(this); } @@ -45,14 +52,13 @@ partial class SwapChainManager /// /// Stops the current render loop, if one is running. /// - public void StopRenderLoop() + public async void StopRenderLoop() { ThrowIfDisposed(); - lock (this.renderLock) + using (await this.setupSemaphore.LockAsync()) { this.renderCancellationTokenSource?.Cancel(); - this.renderThread?.Join(); } } @@ -60,22 +66,24 @@ partial class SwapChainManager /// Queues a change in the dynamic resolution mode. /// /// Whether or not to use dynamic resolution. - public void QueueDynamicResolutionModeChange(bool isDynamicResolutionEnabled) + public async void QueueDynamicResolutionModeChange(bool isDynamicResolutionEnabled) { ThrowIfDisposed(); - lock (this.renderLock) + using (await this.setupSemaphore.LockAsync()) { // If there is a render thread currently running, stop it and restart it if (this.renderCancellationTokenSource?.IsCancellationRequested == false) { this.renderCancellationTokenSource?.Cancel(); - this.renderThread?.Join(); + + await this.renderSemaphore.WaitAsync(); Thread newRenderThread = new(static args => ((SwapChainManager)args!).SwitchAndStartRenderLoop()); this.renderCancellationTokenSource = new CancellationTokenSource(); this.renderThread = newRenderThread; + this.renderSemaphore = new SemaphoreSlim(0, 1); this.isDynamicResolutionEnabled = isDynamicResolutionEnabled; newRenderThread.Start(this); @@ -135,17 +143,24 @@ partial class SwapChainManager /// private void SwitchAndStartRenderLoop() { - if (this.isDynamicResolutionEnabled) + try { - this.targetResolutionScale = 1.0f; + if (this.isDynamicResolutionEnabled) + { + this.targetResolutionScale = 1.0f; - RenderLoopWithDynamicResolution(); + RenderLoopWithDynamicResolution(); + } + else + { + this.targetResolutionScale = this.resolutionScale; + + RenderLoop(); + } } - else + finally { - this.targetResolutionScale = this.resolutionScale; - - RenderLoop(); + this.renderSemaphore.Release(); } } @@ -304,9 +319,9 @@ partial class SwapChainManager { ThrowIfDisposed(); - lock (this.renderLock) - { - this.renderCancellationTokenSource?.Cancel(); - } + this.setupSemaphore.Wait(); + + this.renderCancellationTokenSource?.Cancel(); + this.renderSemaphore.Wait(); } } diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs index 80533ead..b4ec17e2 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs @@ -21,9 +21,9 @@ namespace ComputeSharp.WinUI.Helpers; internal sealed unsafe partial class SwapChainManager : NativeObject { /// - /// A lock object for the render loops. + /// An object for the render loop management. /// - private readonly object renderLock = new(); + private readonly SemaphoreSlim setupSemaphore = new(1, 1); /// /// The pointer for the device currently in use. @@ -85,6 +85,11 @@ internal sealed unsafe partial class SwapChainManager : NativeObject /// private volatile Thread? renderThread; + /// + /// The used to wait for render threads to complete. + /// + private volatile SemaphoreSlim renderSemaphore = new(1, 1); + /// /// The to use for . /// @@ -403,16 +408,21 @@ internal sealed unsafe partial class SwapChainManager : NativeObject /// protected override void OnDispose() { - UnsafeStopRenderLoopAndWait(); + ThreadPool.UnsafeQueueUserWorkItem(static state => + { + SwapChainManager @this = (SwapChainManager)state!; - this.d3D12Device.Dispose(); - this.d3D12CommandQueue.Dispose(); - this.d3D12Fence.Dispose(); - this.d3D12CommandAllocator.Dispose(); - this.d3D12GraphicsCommandList.Dispose(); - this.dxgiSwapChain3.Dispose(); - this.d3D12Resource0.Dispose(); - this.d3D12Resource1.Dispose(); - this.texture?.Dispose(); + @this.UnsafeStopRenderLoopAndWait(); + + @this.d3D12Device.Dispose(); + @this.d3D12CommandQueue.Dispose(); + @this.d3D12Fence.Dispose(); + @this.d3D12CommandAllocator.Dispose(); + @this.d3D12GraphicsCommandList.Dispose(); + @this.dxgiSwapChain3.Dispose(); + @this.d3D12Resource0.Dispose(); + @this.d3D12Resource1.Dispose(); + @this.texture?.Dispose(); + }, this); } }