Avoid blocking on UI thread in shader panels
このコミットが含まれているのは:
1 親
d11f524ade
コミット
49c5690ff8
4ファイル変更
+114
-32
検索条件に一致するファイルはありません
@@ -13,6 +13,7 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\AnimatedComputeShaderPanel.Properties.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\ComputeShaderPanel.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\ComputeShaderPanel.Properties.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SemaphoreSlimExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\SwapChainManager.Rendering.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\SwapChainManager.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\DynamicResolutionManager.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
|
||||
|
||||
/// <summary>
|
||||
/// A helper type for <see cref="SemaphoreSlim"/>.
|
||||
/// </summary>
|
||||
internal static class SemaphoreSlimExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Acquires a lock for the current instance, that is automatically released outside the <see langword="using"/> block.
|
||||
/// </summary>
|
||||
public static async ValueTask<Lock> LockAsync(this SemaphoreSlim semaphore)
|
||||
{
|
||||
await semaphore.WaitAsync().ConfigureAwait(false);
|
||||
|
||||
return new(semaphore);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Private class that implements the automatic release of the semaphore
|
||||
/// </summary>
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public readonly struct Lock : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// The underlying semaphore used by this instance.
|
||||
/// </summary>
|
||||
private readonly SemaphoreSlim semaphore;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Lock"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="semaphore">The <see cref="SemaphoreSlim"/> instance to wrap.</param>
|
||||
[Obsolete("Use AsyncMutex.LockAsync instead.")]
|
||||
public Lock(SemaphoreSlim semaphore)
|
||||
{
|
||||
this.semaphore = semaphore;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
this.semaphore.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
/// </summary>
|
||||
/// <param name="frameRequestQueue">The <see cref="IFrameRequestQueue"/> instance to use, if available.</param>
|
||||
/// <param name="shaderRunner">The <see cref="IShaderRunner"/> instance to use to render frames.</param>
|
||||
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
|
||||
/// <summary>
|
||||
/// Stops the current render loop, if one is running.
|
||||
/// </summary>
|
||||
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.
|
||||
/// </summary>
|
||||
/// <param name="isDynamicResolutionEnabled">Whether or not to use dynamic resolution.</param>
|
||||
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
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -21,9 +21,9 @@ namespace ComputeSharp.WinUI.Helpers;
|
||||
internal sealed unsafe partial class SwapChainManager : NativeObject
|
||||
{
|
||||
/// <summary>
|
||||
/// A lock object for the render loops.
|
||||
/// An <see cref="SemaphoreSlim"/> object for the render loop management.
|
||||
/// </summary>
|
||||
private readonly object renderLock = new();
|
||||
private readonly SemaphoreSlim setupSemaphore = new(1, 1);
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ID3D12Device"/> pointer for the device currently in use.
|
||||
@@ -85,6 +85,11 @@ internal sealed unsafe partial class SwapChainManager : NativeObject
|
||||
/// </summary>
|
||||
private volatile Thread? renderThread;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="SemaphoreSlim"/> used to wait for render threads to complete.
|
||||
/// </summary>
|
||||
private volatile SemaphoreSlim renderSemaphore = new(1, 1);
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="CancellationTokenSource"/> to use for <see cref="renderThread"/>.
|
||||
/// </summary>
|
||||
@@ -403,16 +408,21 @@ internal sealed unsafe partial class SwapChainManager : NativeObject
|
||||
/// <inheritdoc/>
|
||||
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);
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする