From 571feb1400bd5f4fe6813890289d838906bf5546 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 1 Aug 2021 13:36:56 +0200 Subject: [PATCH] Enabled dynamic resolution scaling in ComputeShaderPanel --- .../ComputeShaderPanel.Properties.cs | 0 .../ComputeShaderPanel.Rendering.cs | 12 +- .../{ => Controls}/ComputeShaderPanel.cs | 0 .../Helpers/DynamicResolutionManager.cs | 109 ++++++++++++++++++ 4 files changed, 119 insertions(+), 2 deletions(-) rename src/ComputeSharp.WinUI/{ => Controls}/ComputeShaderPanel.Properties.cs (100%) rename src/ComputeSharp.WinUI/{ => Controls}/ComputeShaderPanel.Rendering.cs (97%) rename src/ComputeSharp.WinUI/{ => Controls}/ComputeShaderPanel.cs (100%) create mode 100644 src/ComputeSharp.WinUI/Helpers/DynamicResolutionManager.cs diff --git a/src/ComputeSharp.WinUI/ComputeShaderPanel.Properties.cs b/src/ComputeSharp.WinUI/Controls/ComputeShaderPanel.Properties.cs similarity index 100% rename from src/ComputeSharp.WinUI/ComputeShaderPanel.Properties.cs rename to src/ComputeSharp.WinUI/Controls/ComputeShaderPanel.Properties.cs diff --git a/src/ComputeSharp.WinUI/ComputeShaderPanel.Rendering.cs b/src/ComputeSharp.WinUI/Controls/ComputeShaderPanel.Rendering.cs similarity index 97% rename from src/ComputeSharp.WinUI/ComputeShaderPanel.Rendering.cs rename to src/ComputeSharp.WinUI/Controls/ComputeShaderPanel.Rendering.cs index dd03754d..b5631010 100644 --- a/src/ComputeSharp.WinUI/ComputeShaderPanel.Rendering.cs +++ b/src/ComputeSharp.WinUI/Controls/ComputeShaderPanel.Rendering.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Threading; using ComputeSharp.Core.Extensions; using ComputeSharp.Interop; +using ComputeSharp.WinUI.Helpers; using TerraFX.Interop; using WinRT; using FX = TerraFX.Interop.Windows; @@ -132,12 +133,12 @@ namespace ComputeSharp.WinUI /// private unsafe void OnInitialize() { - if (isInitialized) + if (this.isInitialized) { return; } - isInitialized = true; + this.isInitialized = true; // Get the underlying ID3D12Device in use fixed (ID3D12Device** d3D12Device = this.d3D12Device) @@ -417,6 +418,8 @@ namespace ComputeSharp.WinUI renderStopwatch = @this.renderStopwatch ??= new(), frameStopwatch = Stopwatch.StartNew(); + 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.OnUpdate(renderStopwatch.Elapsed); @@ -430,6 +433,11 @@ namespace ComputeSharp.WinUI { _ = FX.WaitForSingleObjectEx(frameLatencyWaitableObject, 1000, 1); + if (frameTimeWatcher.Advance(frameStopwatch.ElapsedTicks, ref @this.resolutionScale)) + { + @this.isResizePending = true; + } + while (frameStopwatch.ElapsedTicks < targetFrameTimeInTicksFor60fps) { } diff --git a/src/ComputeSharp.WinUI/ComputeShaderPanel.cs b/src/ComputeSharp.WinUI/Controls/ComputeShaderPanel.cs similarity index 100% rename from src/ComputeSharp.WinUI/ComputeShaderPanel.cs rename to src/ComputeSharp.WinUI/Controls/ComputeShaderPanel.cs diff --git a/src/ComputeSharp.WinUI/Helpers/DynamicResolutionManager.cs b/src/ComputeSharp.WinUI/Helpers/DynamicResolutionManager.cs new file mode 100644 index 00000000..d1798920 --- /dev/null +++ b/src/ComputeSharp.WinUI/Helpers/DynamicResolutionManager.cs @@ -0,0 +1,109 @@ +using System; +using System.Runtime.InteropServices; + +namespace ComputeSharp.WinUI.Helpers +{ + /// + /// A helper type that is responsible for applying dynamic resolution changes. + /// + internal unsafe ref struct DynamicResolutionManager + { + /// + /// The target frame time in ticks to reach 60fps. + /// + private const long TargetFrameTimeInTicksFor60fps = 166666; + + /// + /// The size of the sliding frame time window to monitor frame times. + /// + private const int SlidingFrameTimeWindowLength = 32; + + /// + /// The threshold for scale factor updates. + /// + private const double ScaleFactorDeltaThreshold = 0.05; + + /// + /// The window of registered frame times for previous frames. + /// + private fixed long frameTimesInTicks[SlidingFrameTimeWindowLength]; + + /// + /// The current index into . + /// + /// + /// This will have a value of -1 when a scale factor update has just been applied, indicating + /// that the following frame update should not be tracked to contribute towards the next dynamic resolution + /// check. This is done to avoid having the swap chain resize cost influence the moving average of frame times. + /// + private int frameTimeOffset; + + /// + /// The current sum of values within . + /// + private long slidingFrameTimeWindowSum; + + /// + /// Initializes a new instance. + /// + /// The resulting instance. + public static void Create(out DynamicResolutionManager manager) + { + manager.frameTimeOffset = 0; + manager.slidingFrameTimeWindowSum = TargetFrameTimeInTicksFor60fps * SlidingFrameTimeWindowLength; + + MemoryMarshal.CreateSpan(ref manager.frameTimesInTicks[0], SlidingFrameTimeWindowLength).Fill(TargetFrameTimeInTicksFor60fps); + } + + /// + /// Advances one frame and updates the scale factor, if needed. + /// + /// The frame time in ticks for the last rendered frame. + /// The current scale factor value being used to render frames. + /// Whether or not has been updated and a resize is needed. + public bool Advance(long frameTimeInTicks, ref double scaleFactor) + { + // Ignore the current frame if a resize has just happened + if (this.frameTimeOffset == -1) + { + this.frameTimeOffset = 0; + + return false; + } + + ref long previousFrameTimeInTicks = ref this.frameTimesInTicks[this.frameTimeOffset]; + + this.slidingFrameTimeWindowSum -= previousFrameTimeInTicks; + this.slidingFrameTimeWindowSum += frameTimeInTicks; + + previousFrameTimeInTicks = frameTimeInTicks; + + this.frameTimeOffset = (int)(((uint)this.frameTimeOffset + 1) % SlidingFrameTimeWindowLength); + + // Check for a dynamic resolution update if the end of the sliding window has been reached. + // This is not done every frame to reduce the overhead and amortize for render time fluctuations. + if (this.frameTimeOffset == 0) + { + // This formula is adapted from https://software.intel.com/content/www/us/en/develop/articles/dynamic-resolution-rendering-article.html. + long averageFrameTimeInTicks = this.slidingFrameTimeWindowSum / SlidingFrameTimeWindowLength; + double + frameTimeDelta = (TargetFrameTimeInTicksFor60fps - averageFrameTimeInTicks) / (double)averageFrameTimeInTicks, + scaleFactorDelta = scaleFactor * frameTimeDelta, + updatedScaleFactor = Math.Clamp(scaleFactor + scaleFactorDelta, 0.10, 1.0); + + // 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. + if (Math.Abs(scaleFactor - updatedScaleFactor) >= ScaleFactorDeltaThreshold) + { + scaleFactor = updatedScaleFactor; + + this.frameTimeOffset = -1; + + return true; + } + } + + return false; + } + } +}