Avoid blocking on UI thread in shader panels
このコミットが含まれているのは:
1 親
d11f524ade
コミット
49c5690ff8
4ファイル変更
+114
-32
検索条件に一致するファイルはありません
@@ -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);
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする