diff --git a/src/ComputeSharp.UI/ComputeSharp.UI.projitems b/src/ComputeSharp.UI/ComputeSharp.UI.projitems index 571ddce3..c990a06c 100644 --- a/src/ComputeSharp.UI/ComputeSharp.UI.projitems +++ b/src/ComputeSharp.UI/ComputeSharp.UI.projitems @@ -11,6 +11,7 @@ + diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs new file mode 100644 index 00000000..a66a70e9 --- /dev/null +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs @@ -0,0 +1,254 @@ +using System; +using System.Diagnostics; +using System.Threading; +using Microsoft.Toolkit.Diagnostics; + +#pragma warning disable CS0420 + +#if WINDOWS_UWP +namespace ComputeSharp.Uwp.Helpers; +#else +namespace ComputeSharp.WinUI.Helpers; +#endif + +/// +partial class SwapChainManager +{ + /// + /// Starts the current render loop. + /// + /// The instance to use to render frames. + public void StartRenderLoop(IShaderRunner shaderRunner) + { + ThrowIfDisposed(); + + Guard.IsNotNull(shaderRunner, nameof(shaderRunner)); + + lock (this.renderLock) + { + this.renderCancellationTokenSource?.Cancel(); + this.renderThread?.Join(); + + Thread newRenderThread = new(static args => ((SwapChainManager)args!).SwitchAndStartRenderLoop()); + + this.shaderRunner = shaderRunner; + this.renderCancellationTokenSource = new CancellationTokenSource(); + this.renderThread = newRenderThread; + + newRenderThread.Start(this); + } + } + + /// + /// Stops the current render loop, if one is running. + /// + public void StopRenderLoop() + { + ThrowIfDisposed(); + + lock (this.renderLock) + { + this.renderCancellationTokenSource?.Cancel(); + this.renderThread?.Join(); + } + } + + /// + /// Queues a change in the dynamic resolution mode. + /// + /// Whether or not to use dynamic resolution. + public void QueueDynamicResolutionModeChange(bool isDynamicResolutionEnabled) + { + ThrowIfDisposed(); + + lock (this.renderLock) + { + // If there is a render thread currently running, stop it and restart it + if (this.renderCancellationTokenSource?.IsCancellationRequested == false) + { + this.renderCancellationTokenSource?.Cancel(); + this.renderThread?.Join(); + + Thread newRenderThread = new(static args => ((SwapChainManager)args!).SwitchAndStartRenderLoop()); + + this.renderCancellationTokenSource = new CancellationTokenSource(); + this.renderThread = newRenderThread; + this.isDynamicResolutionEnabled = isDynamicResolutionEnabled; + + newRenderThread.Start(this); + } + else + { + this.isDynamicResolutionEnabled = isDynamicResolutionEnabled; + } + } + } + + /// + /// Queues a resize operation. + /// + /// The width of the render resolution. + /// The height of the render resolution. + public void QueueResize(double width, double height) + { + ThrowIfDisposed(); + + this.width = (float)width; + this.height = (float)height; + + this.isResizePending = true; + } + + /// + /// Queues a change in the composition scale factors. + /// + /// The composition scale on the X axis. + /// The composition scale on the Y axis + public void QueueCompositionScaleChange(double compositionScaleX, double compositionScaleY) + { + ThrowIfDisposed(); + + this.compositionScaleX = (float)compositionScaleX; + this.compositionScaleY = (float)compositionScaleY; + + this.isResizePending = true; + } + + /// + /// Queues a change in the resolution scale factor. + /// + /// The resolution scale factor to use. + public void QueueResolutionScaleChange(double resolutionScale) + { + ThrowIfDisposed(); + + this.resolutionScale = (float)resolutionScale; + + this.isResizePending = true; + } + + /// + /// Selects the right render loop to start. + /// + private void SwitchAndStartRenderLoop() + { + if (this.isDynamicResolutionEnabled) + { + this.targetResolutionScale = 1.0f; + + RenderLoopWithDynamicResolution(); + } + else + { + this.targetResolutionScale = this.resolutionScale; + + RenderLoop(); + } + } + + /// + /// The core render loop. + /// + private void RenderLoop() + { + Stopwatch renderStopwatch = this.renderStopwatch ??= new(); + CancellationToken cancellationToken = this.renderCancellationTokenSource!.Token; + + // Start the initial frame separately, before the timer starts. This ensures that + // resuming after a pause correctly renders the first frame at the right time. + this.OnResize(); + this.OnUpdate(renderStopwatch.Elapsed); + this.OnPresent(); + + renderStopwatch.Start(); + + // Main render loop, until cancellation is requested + while (!cancellationToken.IsCancellationRequested) + { + this.OnResize(); + this.OnUpdate(renderStopwatch.Elapsed); + this.OnPresent(); + } + + renderStopwatch.Stop(); + } + + /// + /// The core render loop with dynamic resolution. + /// + private void RenderLoopWithDynamicResolution() + { + Stopwatch renderStopwatch = this.renderStopwatch ??= new(); + Stopwatch frameStopwatch = Stopwatch.StartNew(); + CancellationToken cancellationToken = this.renderCancellationTokenSource!.Token; + + DynamicResolutionManager.Create(out DynamicResolutionManager frameTimeWatcher); + + // Start the initial frame separately, before the timer starts. This ensures that + // resuming after a pause correctly renders the first frame at the right time. + this.OnResize(); + this.OnUpdate(renderStopwatch.Elapsed); + this.OnPresent(); + + renderStopwatch.Start(); + + // Main render loop, until cancellation is requested + while (!cancellationToken.IsCancellationRequested) + { + // Evaluate the dynamic resolution frame time step, if the mode is enabled + if (frameTimeWatcher.Advance(frameStopwatch.ElapsedTicks, ref this.targetResolutionScale)) + { + this.isResizePending = true; + } + + frameStopwatch.Restart(); + + this.OnResize(); + this.OnUpdate(renderStopwatch.Elapsed); + this.OnPresent(); + } + + renderStopwatch.Stop(); + } + + /// + /// Resizes the current application, if needed. + /// + private void OnResize() + { + if (this.isResizePending) + { + ApplyResize(); + + this.isResizePending = false; + } + } + + /// + /// Updates the render resolution, if needed, and renders a new frame. + /// + /// The current time since the start of the application. + private void OnUpdate(TimeSpan time) + { + this.shaderRunner!.Execute(this.texture!, time); + } + + /// + /// Presents the last rendered frame for the current application. + /// + private unsafe partial void OnPresent(); + + /// + /// Stops the current render loop, if one is running, and waits for it. + /// + /// This method doesn't check for disposal. + private void UnsafeStopRenderLoopAndWait() + { + ThrowIfDisposed(); + + lock (this.renderLock) + { + this.renderCancellationTokenSource?.Cancel(); + } + } +} diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs index ae019d43..8e2a3ea4 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs @@ -4,14 +4,11 @@ using System.Threading; using ComputeSharp.Core.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; -using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using TerraFX.Interop.WinRT; using Win32 = TerraFX.Interop.Windows.Windows; -#pragma warning disable CS0420 - #if WINDOWS_UWP namespace ComputeSharp.Uwp.Helpers; #else @@ -21,7 +18,7 @@ namespace ComputeSharp.WinUI.Helpers; /// /// A type managing rendering on a target swap chain object. /// -internal sealed unsafe class SwapChainManager : NativeObject +internal sealed unsafe partial class SwapChainManager : NativeObject { /// /// A lock object for the render loops. @@ -322,39 +319,8 @@ internal sealed unsafe class SwapChainManager : NativeObject (int)d3D12Resource0Description.Height); } - /// - /// Resizes the current application, if needed. - /// - private void OnResize() - { - if (this.isResizePending) - { - ApplyResize(); - - this.isResizePending = false; - } - } - - /// - /// Updates the render resolution, if needed, and renders a new frame. - /// - /// The current time since the start of the application. - private void OnUpdate(TimeSpan time) - { - // Skip if no factory is available - if (this.shaderRunner is null) - { - return; - } - - // Generate the new frame - this.shaderRunner.Execute(this.texture!, time); - } - - /// - /// Presents the last rendered frame for the current application. - /// - private unsafe void OnPresent() + /// + private unsafe partial void OnPresent() { // Wait for the swap chain to be ready for presenting _ = Win32.WaitForSingleObjectEx(frameLatencyWaitableObject, 1000, true); @@ -429,211 +395,10 @@ internal sealed unsafe class SwapChainManager : NativeObject } } - /// - /// Selects the right render loop to start. - /// - private void SwitchAndStartRenderLoop() - { - if (this.isDynamicResolutionEnabled) - { - this.targetResolutionScale = 1.0f; - - RenderLoopWithDynamicResolution(); - } - else - { - this.targetResolutionScale = this.resolutionScale; - - RenderLoop(); - } - } - - /// - /// The core render loop. - /// - private void RenderLoop() - { - Stopwatch renderStopwatch = this.renderStopwatch ??= new(); - CancellationToken cancellationToken = this.renderCancellationTokenSource!.Token; - - // Start the initial frame separately, before the timer starts. This ensures that - // resuming after a pause correctly renders the first frame at the right time. - this.OnResize(); - this.OnUpdate(renderStopwatch.Elapsed); - this.OnPresent(); - - renderStopwatch.Start(); - - // Main render loop, until cancellation is requested - while (!cancellationToken.IsCancellationRequested) - { - this.OnResize(); - this.OnUpdate(renderStopwatch.Elapsed); - this.OnPresent(); - } - - renderStopwatch.Stop(); - } - - /// - /// The core render loop with dynamic resolution. - /// - private void RenderLoopWithDynamicResolution() - { - Stopwatch renderStopwatch = this.renderStopwatch ??= new(); - Stopwatch frameStopwatch = Stopwatch.StartNew(); - CancellationToken cancellationToken = this.renderCancellationTokenSource!.Token; - - DynamicResolutionManager.Create(out DynamicResolutionManager frameTimeWatcher); - - // Start the initial frame separately, before the timer starts. This ensures that - // resuming after a pause correctly renders the first frame at the right time. - this.OnResize(); - this.OnUpdate(renderStopwatch.Elapsed); - this.OnPresent(); - - renderStopwatch.Start(); - - // Main render loop, until cancellation is requested - while (!cancellationToken.IsCancellationRequested) - { - // Evaluate the dynamic resolution frame time step, if the mode is enabled - if (frameTimeWatcher.Advance(frameStopwatch.ElapsedTicks, ref this.targetResolutionScale)) - { - this.isResizePending = true; - } - - frameStopwatch.Restart(); - - this.OnResize(); - this.OnUpdate(renderStopwatch.Elapsed); - this.OnPresent(); - } - - renderStopwatch.Stop(); - } - - /// - /// Starts the current render loop. - /// - /// The instance to use to render frames. - public void StartRenderLoop(IShaderRunner shaderRunner) - { - ThrowIfDisposed(); - - Guard.IsNotNull(shaderRunner, nameof(shaderRunner)); - - lock (this.renderLock) - { - this.renderCancellationTokenSource?.Cancel(); - this.renderThread?.Join(); - - Thread newRenderThread = new(static args => ((SwapChainManager)args!).SwitchAndStartRenderLoop()); - - this.shaderRunner = shaderRunner; - this.renderCancellationTokenSource = new CancellationTokenSource(); - this.renderThread = newRenderThread; - - newRenderThread.Start(this); - } - } - - /// - /// Queues a change in the dynamic resolution mode. - /// - /// Whether or not to use dynamic resolution. - public void QueueDynamicResolutionModeChange(bool isDynamicResolutionEnabled) - { - ThrowIfDisposed(); - - lock (this.renderLock) - { - // If there is a render thread currently running, stop it and restart it - if (this.renderCancellationTokenSource?.IsCancellationRequested == false) - { - this.renderCancellationTokenSource?.Cancel(); - this.renderThread?.Join(); - - Thread newRenderThread = new(static args => ((SwapChainManager)args!).SwitchAndStartRenderLoop()); - - this.renderCancellationTokenSource = new CancellationTokenSource(); - this.renderThread = newRenderThread; - this.isDynamicResolutionEnabled = isDynamicResolutionEnabled; - - newRenderThread.Start(this); - } - else - { - this.isDynamicResolutionEnabled = isDynamicResolutionEnabled; - } - } - } - - /// - /// Queues a resize operation. - /// - /// The width of the render resolution. - /// The height of the render resolution. - public void QueueResize(double width, double height) - { - ThrowIfDisposed(); - - this.width = (float)width; - this.height = (float)height; - - this.isResizePending = true; - } - - /// - /// Queues a change in the composition scale factors. - /// - /// The composition scale on the X axis. - /// The composition scale on the Y axis - public void QueueCompositionScaleChange(double compositionScaleX, double compositionScaleY) - { - ThrowIfDisposed(); - - this.compositionScaleX = (float)compositionScaleX; - this.compositionScaleY = (float)compositionScaleY; - - this.isResizePending = true; - } - - /// - /// Queues a change in the resolution scale factor. - /// - /// The resolution scale factor to use. - public void QueueResolutionScaleChange(double resolutionScale) - { - ThrowIfDisposed(); - - this.resolutionScale = (float)resolutionScale; - - this.isResizePending = true; - } - - /// - /// Stops the current render loop, if one is running. - /// - public void StopRenderLoop() - { - ThrowIfDisposed(); - - lock (this.renderLock) - { - this.renderCancellationTokenSource?.Cancel(); - } - } - /// protected override void OnDispose() { - // Ensure the rendering has stopped - lock (this.renderLock) - { - this.renderCancellationTokenSource?.Cancel(); - this.renderThread?.Join(); - } + UnsafeStopRenderLoopAndWait(); this.d3D12Device.Dispose(); this.d3D12CommandQueue.Dispose();