diff --git a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs index f7c134fb..01ea621d 100644 --- a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs +++ b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs @@ -103,6 +103,8 @@ partial class ComputeShaderPanel { var @this = (ComputeShaderPanel)d; var isDynamicResolutionEnabled = (bool)e.NewValue; + + @this.swapChainManager.QueueDynamicResolutionModeChange(isDynamicResolutionEnabled); } /// diff --git a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs index 6ba8eff5..9b2ebb8f 100644 --- a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs +++ b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs @@ -54,6 +54,7 @@ public sealed partial class ComputeShaderPanel : SwapChainPanel this.swapChainManager.QueueResize(ActualWidth, ActualHeight); this.swapChainManager.QueueCompositionScaleChange(CompositionScaleX, CompositionScaleY); this.swapChainManager.QueueResolutionScaleChange(ResolutionScale); + this.swapChainManager.QueueDynamicResolutionModeChange(IsDynamicResolutionEnabled); if (ShaderRunner is IShaderRunner shaderRunner) { diff --git a/src/ComputeSharp.UI/Helpers/DynamicResolutionManager.cs b/src/ComputeSharp.UI/Helpers/DynamicResolutionManager.cs index 26b9004e..cfbbc869 100644 --- a/src/ComputeSharp.UI/Helpers/DynamicResolutionManager.cs +++ b/src/ComputeSharp.UI/Helpers/DynamicResolutionManager.cs @@ -118,10 +118,9 @@ internal unsafe ref struct DynamicResolutionManager if (averageFrameTimeInTicks is < LowerFrameTimeThresholdInTicksFor60fps or > UpperFrameTimeThresholdInTicksFor60fps) { - float - frameTimeDelta = (TargetFrameTimeInTicksFor60fps - averageFrameTimeInTicks) / (float)averageFrameTimeInTicks, - scaleFactorDelta = scaleFactor * frameTimeDelta * ScaleFactorDeltaK, - updatedScaleFactor = Math.Clamp(scaleFactor + scaleFactorDelta, 0.10f, 1.0f); + float frameTimeDelta = (TargetFrameTimeInTicksFor60fps - averageFrameTimeInTicks) / (float)averageFrameTimeInTicks; + float scaleFactorDelta = scaleFactor * frameTimeDelta * ScaleFactorDeltaK; + float updatedScaleFactor = Math.Clamp(scaleFactor + scaleFactorDelta, 0.10f, 1.0f); // Apply the scale factor update if the target scale has changed enough. This helps to avoid // too frequence resolution changes if the scale factor only changes very little every time. diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs index 64ef76d0..ae019d43 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs @@ -429,19 +429,33 @@ 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(); - Stopwatch frameStopwatch = Stopwatch.StartNew(); CancellationToken cancellationToken = this.renderCancellationTokenSource!.Token; - DynamicResolutionManager.Create(out DynamicResolutionManager frameTimeWatcher); - - bool isDynamicResolutionEnabled = this.isDynamicResolutionEnabled; - // 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(); @@ -453,24 +467,38 @@ internal sealed unsafe class SwapChainManager : NativeObject // Main render loop, until cancellation is requested while (!cancellationToken.IsCancellationRequested) { - // Update the resolution mode, if needed - if (isDynamicResolutionEnabled != this.isDynamicResolutionEnabled) - { - isDynamicResolutionEnabled = !isDynamicResolutionEnabled; + this.OnResize(); + this.OnUpdate(renderStopwatch.Elapsed); + this.OnPresent(); + } - if (isDynamicResolutionEnabled) - { - DynamicResolutionManager.Create(out frameTimeWatcher); - } - else - { - this.targetResolutionScale = this.resolutionScale; - } - } + 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 (isDynamicResolutionEnabled && - frameTimeWatcher.Advance(frameStopwatch.ElapsedTicks, ref this.targetResolutionScale)) + if (frameTimeWatcher.Advance(frameStopwatch.ElapsedTicks, ref this.targetResolutionScale)) { this.isResizePending = true; } @@ -488,10 +516,9 @@ internal sealed unsafe class SwapChainManager : NativeObject /// /// Starts the current render loop. /// + /// The instance to use to render frames. public void StartRenderLoop(IShaderRunner shaderRunner) { - this.isDynamicResolutionEnabled = false; - ThrowIfDisposed(); Guard.IsNotNull(shaderRunner, nameof(shaderRunner)); @@ -501,7 +528,7 @@ internal sealed unsafe class SwapChainManager : NativeObject this.renderCancellationTokenSource?.Cancel(); this.renderThread?.Join(); - Thread newRenderThread = new(static args => ((SwapChainManager)args!).RenderLoop()); + Thread newRenderThread = new(static args => ((SwapChainManager)args!).SwitchAndStartRenderLoop()); this.shaderRunner = shaderRunner; this.renderCancellationTokenSource = new CancellationTokenSource(); @@ -512,15 +539,33 @@ internal sealed unsafe class SwapChainManager : NativeObject } /// - /// Stops the current render loop, if one is running. + /// Queues a change in the dynamic resolution mode. /// - public void StopRenderLoop() + /// Whether or not to use dynamic resolution. + public void QueueDynamicResolutionModeChange(bool isDynamicResolutionEnabled) { ThrowIfDisposed(); lock (this.renderLock) { - this.renderCancellationTokenSource?.Cancel(); + // 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; + } } } @@ -531,6 +576,8 @@ internal sealed unsafe class SwapChainManager : NativeObject /// The height of the render resolution. public void QueueResize(double width, double height) { + ThrowIfDisposed(); + this.width = (float)width; this.height = (float)height; @@ -544,6 +591,8 @@ internal sealed unsafe class SwapChainManager : NativeObject /// The composition scale on the Y axis public void QueueCompositionScaleChange(double compositionScaleX, double compositionScaleY) { + ThrowIfDisposed(); + this.compositionScaleX = (float)compositionScaleX; this.compositionScaleY = (float)compositionScaleY; @@ -556,12 +605,26 @@ internal sealed unsafe class SwapChainManager : NativeObject /// The resolution scale factor to use. public void QueueResolutionScaleChange(double resolutionScale) { + ThrowIfDisposed(); + this.resolutionScale = (float)resolutionScale; - this.targetResolutionScale = (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() {