diff --git a/src/ComputeSharp.WinUI/ComputeShaderPanel.Rendering.cs b/src/ComputeSharp.WinUI/ComputeShaderPanel.Rendering.cs
index 5e588738..18cefc21 100644
--- a/src/ComputeSharp.WinUI/ComputeShaderPanel.Rendering.cs
+++ b/src/ComputeSharp.WinUI/ComputeShaderPanel.Rendering.cs
@@ -1,8 +1,11 @@
using System;
+using System.Diagnostics;
+using System.Threading;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Interop;
using Microsoft.UI.Xaml.Controls;
using TerraFX.Interop;
+using WinRT;
using FX = TerraFX.Interop.Windows;
namespace ComputeSharp.WinUI
@@ -10,16 +13,16 @@ namespace ComputeSharp.WinUI
///
public sealed partial class ComputeShaderPanel : SwapChainPanel
{
+ ///
+ /// The render thread in use, if any.
+ ///
+ private Thread? renderThread;
+
///
/// The instance used to create shaders to run.
///
private IShaderRunner? shaderRunner;
- ///
- /// The resolution scale used to render frames.
- ///
- private double resolutionScale;
-
///
/// The pointer for the device currently in use.
///
@@ -100,6 +103,16 @@ namespace ComputeSharp.WinUI
///
private float compositionScaleY;
+ ///
+ /// The resolution scale used to render frames.
+ ///
+ private double resolutionScale;
+
+ ///
+ /// Indicates whether or not the rendering has been canceled.
+ ///
+ private bool isCancellationRequested;
+
///
/// Initializes the current application.
///
@@ -120,7 +133,7 @@ namespace ComputeSharp.WinUI
d3D12CommandQueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAGS.D3D12_COMMAND_QUEUE_FLAG_NONE;
d3D12CommandQueueDesc.NodeMask = 0;
- d3D12Device.Get()->CreateCommandQueue(
+ this.d3D12Device.Get()->CreateCommandQueue(
&d3D12CommandQueueDesc,
FX.__uuidof(),
(void**)d3D12CommandQueue).Assert();
@@ -178,7 +191,7 @@ namespace ComputeSharp.WinUI
this.d3D12Device.Get()->CreateCommandList(
0,
D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_DIRECT,
- d3D12CommandAllocator,
+ this.d3D12CommandAllocator,
null,
FX.__uuidof(),
(void**)d3D12GraphicsCommandList).Assert();
@@ -186,6 +199,24 @@ namespace ComputeSharp.WinUI
// Close the command list to prepare it for future use
this.d3D12GraphicsCommandList.Get()->Close().Assert();
+
+ // Extract the ISwapChainPanelNative reference from the current panel, then query the
+ // IDXGISwapChain reference just created and set that as the swap chain panel to use.
+ using (ComPtr swapChainPanelNative = default)
+ {
+ IUnknown* swapChainPanel = (IUnknown*)((IWinRTObject)this).NativeObject.ThisPtr;
+ Guid iSwapChainPanelNativeUuid = Guid.Parse("63AAD0B8-7C24-40FF-85A8-640D944CC325");
+
+ swapChainPanel->QueryInterface(
+ &iSwapChainPanelNativeUuid,
+ (void**)&swapChainPanelNative).Assert();
+
+ using ComPtr idxgiSwapChain = default;
+
+ this.dxgiSwapChain1.CopyTo(&idxgiSwapChain).Assert();
+
+ swapChainPanelNative.Get()->SetSwapChain(idxgiSwapChain.Get()).Assert();
+ }
}
///
@@ -334,5 +365,45 @@ namespace ComputeSharp.WinUI
this.nextD3D12FenceValue++;
}
+
+ ///
+ /// Executes the render loop for the current panel.
+ ///
+ /// This method assumes to be invoked from a background thread.
+ private void OnStartRenderLoop()
+ {
+ static void ExecuteRenderLoop(ComputeShaderPanel @this)
+ {
+ Stopwatch
+ startStopwatch = Stopwatch.StartNew(),
+ frameStopwatch = Stopwatch.StartNew();
+
+ const long targetFrameTimeInTicksFor60fps = 166666;
+
+ @this.OnUpdate(TimeSpan.Zero);
+
+ while (!@this.isCancellationRequested)
+ {
+ if (frameStopwatch.ElapsedTicks >= targetFrameTimeInTicksFor60fps)
+ {
+ frameStopwatch.Restart();
+
+ @this.OnUpdate(startStopwatch.Elapsed);
+ }
+ }
+ }
+
+ this.isCancellationRequested = false;
+ this.renderThread = new Thread(static args => ExecuteRenderLoop((ComputeShaderPanel)args!));
+ this.renderThread.Start(this);
+ }
+
+ ///
+ /// Stops the current render loop, if one is running.
+ ///
+ private void OnStopRenderLoop()
+ {
+ this.isCancellationRequested = true;
+ }
}
}
diff --git a/src/ComputeSharp.WinUI/ComputeShaderPanel.cs b/src/ComputeSharp.WinUI/ComputeShaderPanel.cs
index 676e75e2..dbbf41da 100644
--- a/src/ComputeSharp.WinUI/ComputeShaderPanel.cs
+++ b/src/ComputeSharp.WinUI/ComputeShaderPanel.cs
@@ -1,11 +1,5 @@
-using System;
-using System.Diagnostics;
-using System.Threading;
-using ComputeSharp.Core.Extensions;
-using Microsoft.UI.Xaml;
+using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
-using TerraFX.Interop;
-using WinRT;
namespace ComputeSharp.WinUI
{
@@ -15,14 +9,13 @@ namespace ComputeSharp.WinUI
///
public sealed unsafe partial class ComputeShaderPanel : SwapChainPanel
{
- private Thread? renderThread;
-
///
/// Creates a new instance.
///
public ComputeShaderPanel()
{
this.Loaded += ComputeShaderPanel_Loaded;
+ this.Unloaded += ComputeShaderPanel_Unloaded;
this.SizeChanged += ComputeShaderPanel_SizeChanged;
this.CompositionScaleChanged += ComputeShaderPanel_CompositionScaleChanged;
}
@@ -37,49 +30,13 @@ namespace ComputeSharp.WinUI
this.resolutionScale = ResolutionScale;
OnInitialize();
+ OnStartRenderLoop();
+ }
- // Extract the ISwapChainPanelNative reference from the current panel, then query the
- // IDXGISwapChain reference just created and set that as the swap chain panel to use.
- using (ComPtr swapChainPanelNative = default)
- {
- IUnknown* swapChainPanel = (IUnknown*)((IWinRTObject)this).NativeObject.ThisPtr;
- Guid iSwapChainPanelNativeUuid = Guid.Parse("63AAD0B8-7C24-40FF-85A8-640D944CC325");
-
- swapChainPanel->QueryInterface(
- &iSwapChainPanelNativeUuid,
- (void**)&swapChainPanelNative).Assert();
-
- using ComPtr idxgiSwapChain = default;
-
- this.dxgiSwapChain1.CopyTo(&idxgiSwapChain).Assert();
-
- swapChainPanelNative.Get()->SetSwapChain(idxgiSwapChain.Get()).Assert();
- }
-
- this.renderThread = new Thread(static args =>
- {
- var panel = (ComputeShaderPanel)args!;
-
- Stopwatch
- startStopwatch = Stopwatch.StartNew(),
- frameStopwatch = Stopwatch.StartNew();
-
- const long targetFrameTimeInTicksFor60fps = 166666;
-
- panel.OnUpdate(TimeSpan.Zero);
-
- while (true)
- {
- if (frameStopwatch.ElapsedTicks >= targetFrameTimeInTicksFor60fps)
- {
- frameStopwatch.Restart();
-
- panel.OnUpdate(startStopwatch.Elapsed);
- }
- }
- });
-
- this.renderThread.Start(this);
+ // Requests a cancellation for the render thread
+ private void ComputeShaderPanel_Unloaded(object sender, RoutedEventArgs e)
+ {
+ OnStopRenderLoop();
}
// Updates the background store for the frame size factors used by the render thread