From 62a3129046b4cbd9f6ef8dfb060b56608d7bbd67 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 21 Jan 2022 20:55:12 +0100 Subject: [PATCH] Fix dynamic resolution when v-sync is enabled --- .../Helpers/SwapChainManager.Rendering.cs | 20 +++++++++++++++++++ .../Helpers/SwapChainManager.cs | 9 ++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs index 196fdbb6..9a51483a 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs @@ -230,6 +230,7 @@ partial class SwapChainManager // resuming after a pause correctly renders the first frame at the right time. if (OnUpdate(renderStopwatch.Elapsed, parameter)) { + OnWaitForPresent(); OnPresent(); } @@ -247,6 +248,7 @@ partial class SwapChainManager if (OnUpdate(renderStopwatch.Elapsed, parameter)) { + OnWaitForPresent(); OnPresent(); } } @@ -272,6 +274,7 @@ partial class SwapChainManager if (OnUpdate(renderStopwatch.Elapsed, parameter)) { + OnWaitForPresent(); OnPresent(); } @@ -290,6 +293,18 @@ partial class SwapChainManager if (OnUpdate(renderStopwatch.Elapsed, parameter)) { + frameStopwatch.Stop(); + + // The time spent waiting for present isn't included in the frame time, as when v-sync is + // enabled it corresponds to the time spent waiting for the v-blank. If it was included, + // then the actual framerate would never exceed the target one, meaning that if dynamic + // resolution is enabled, the resolution scale could only keep decreasing without ever + // being able to be increased again, as the framerate would never exceed the target. + // Simply not counting the time spent waiting for that works around the issue. + OnWaitForPresent(); + + frameStopwatch.Start(); + OnPresent(); // Evaluate the dynamic resolution frame time step @@ -361,6 +376,11 @@ partial class SwapChainManager return this.shaderRunner!.TryExecute(this.texture!, time, parameter); } + /// + /// Waits for the panel to be ready to present a new frame for the current application. + /// + private unsafe partial void OnWaitForPresent(); + /// /// Presents the last rendered frame for the current application. /// diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs index c50db485..2d3fd60b 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs @@ -389,12 +389,15 @@ internal sealed unsafe partial class SwapChainManager : NativeObject } } + /// + private unsafe partial void OnWaitForPresent() + { + _ = Win32.WaitForSingleObjectEx(frameLatencyWaitableObject, Win32.INFINITE, true); + } + /// private unsafe partial void OnPresent() { - // Wait for the swap chain to be ready for presenting - _ = Win32.WaitForSingleObjectEx(frameLatencyWaitableObject, Win32.INFINITE, true); - using ComPtr d3D12Resource = default; // Get the underlying ID3D12Resource pointer for the texture