diff --git a/src/ComputeSharp.UI/Helpers/DynamicResolutionManager.cs b/src/ComputeSharp.UI/Helpers/DynamicResolutionManager.cs index ac3b07ec..796acbb1 100644 --- a/src/ComputeSharp.UI/Helpers/DynamicResolutionManager.cs +++ b/src/ComputeSharp.UI/Helpers/DynamicResolutionManager.cs @@ -1,6 +1,9 @@ using System; using System.Runtime.CompilerServices; using System.Threading; +using ComputeSharp.Core.Extensions; +using TerraFX.Interop.Windows; +using Win32 = TerraFX.Interop.Windows.Windows; #if WINDOWS_UWP namespace ComputeSharp.Uwp.Helpers; @@ -66,9 +69,22 @@ internal unsafe ref struct DynamicResolutionManager /// /// Initializes a new instance. /// - /// The target framerate to use. - public DynamicResolutionManager(int targetFramerate) + public DynamicResolutionManager() { + DWM_TIMING_INFO dwmTimingInfo = default; + + dwmTimingInfo.cbSize = (uint)sizeof(DWM_TIMING_INFO); + + // Get the composition timing info (the refresh rate depends on the compositor) + Win32.DwmGetCompositionTimingInfo(HWND.NULL, &dwmTimingInfo).Assert(); + + // Calculates the best target framerate based on monitor refresh rate. Even with v-sync disabled, + // the target framerate should always match the refresh rate in use, instead of being capped to + // 60fps (as users could have a 120Hz screen and disabled v-sync). Here the refresh rate is retrieved, + // then the target framerate is calculated as being the rounding of this value to the next integer, + // then rounded up to the next multiple of 30, and then clamped between 30 and 240fps. + double refreshRateInHz = dwmTimingInfo.rateRefresh.uiNumerator / (double)dwmTimingInfo.rateRefresh.uiDenominator; + int targetFramerate = Math.Clamp((((int)Math.Ceiling(refreshRateInHz) + 29) / 30) * 30, 30, 240); long targetFrameTimeInTicks = TimeSpan.FromSeconds(1).Ticks / targetFramerate; long upperFrameTimeThresholdInTicks = TimeSpan.FromSeconds(1).Ticks / (targetFramerate - 2); long lowerFrameTimeThresholdInTicks = TimeSpan.FromSeconds(1).Ticks / (targetFramerate + 2); diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs index 9a51483a..12858505 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs @@ -263,7 +263,7 @@ partial class SwapChainManager Stopwatch renderStopwatch = this.renderStopwatch ??= new(); Stopwatch frameStopwatch = new(); CancellationToken cancellationToken = this.renderCancellationTokenSource!.Token; - DynamicResolutionManager frameTimeWatcher = new(60); + DynamicResolutionManager frameTimeWatcher = new(); if (!OnFrameRequest(out object? parameter, cancellationToken)) { diff --git a/src/TerraFX.Interop.Windows/TerraFX.Interop.Windows.projitems b/src/TerraFX.Interop.Windows/TerraFX.Interop.Windows.projitems index fd914b80..5a927617 100644 --- a/src/TerraFX.Interop.Windows/TerraFX.Interop.Windows.projitems +++ b/src/TerraFX.Interop.Windows/TerraFX.Interop.Windows.projitems @@ -228,6 +228,9 @@ + + + diff --git a/src/TerraFX.Interop.Windows/Windows/um/dwmapi/DWM_TIMING_INFO.cs b/src/TerraFX.Interop.Windows/Windows/um/dwmapi/DWM_TIMING_INFO.cs new file mode 100644 index 00000000..99ddf4b4 --- /dev/null +++ b/src/TerraFX.Interop.Windows/Windows/um/dwmapi/DWM_TIMING_INFO.cs @@ -0,0 +1,124 @@ +// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +// Ported from um/dwmapi.h in the Windows SDK for Windows 10.0.22000.0 +// Original source is Copyright © Microsoft. All rights reserved. + +using System.Runtime.InteropServices; + +namespace TerraFX.Interop.Windows; + +[StructLayout(LayoutKind.Sequential, Pack = 1)] +internal partial struct DWM_TIMING_INFO +{ + [NativeTypeName("UINT32")] + public uint cbSize; + + public UNSIGNED_RATIO rateRefresh; + + [NativeTypeName("QPC_TIME")] + public ulong qpcRefreshPeriod; + + public UNSIGNED_RATIO rateCompose; + + [NativeTypeName("QPC_TIME")] + public ulong qpcVBlank; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cRefresh; + + public uint cDXRefresh; + + [NativeTypeName("QPC_TIME")] + public ulong qpcCompose; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cFrame; + + public uint cDXPresent; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cRefreshFrame; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cFrameSubmitted; + + public uint cDXPresentSubmitted; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cFrameConfirmed; + + public uint cDXPresentConfirmed; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cRefreshConfirmed; + + public uint cDXRefreshConfirmed; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cFramesLate; + + public uint cFramesOutstanding; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cFrameDisplayed; + + [NativeTypeName("QPC_TIME")] + public ulong qpcFrameDisplayed; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cRefreshFrameDisplayed; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cFrameComplete; + + [NativeTypeName("QPC_TIME")] + public ulong qpcFrameComplete; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cFramePending; + + [NativeTypeName("QPC_TIME")] + public ulong qpcFramePending; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cFramesDisplayed; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cFramesComplete; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cFramesPending; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cFramesAvailable; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cFramesDropped; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cFramesMissed; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cRefreshNextDisplayed; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cRefreshNextPresented; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cRefreshesDisplayed; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cRefreshesPresented; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cRefreshStarted; + + [NativeTypeName("ULONGLONG")] + public ulong cPixelsReceived; + + [NativeTypeName("ULONGLONG")] + public ulong cPixelsDrawn; + + [NativeTypeName("DWM_FRAME_COUNT")] + public ulong cBuffersEmpty; +} \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows/Windows/um/dwmapi/UNSIGNED_RATIO.cs b/src/TerraFX.Interop.Windows/Windows/um/dwmapi/UNSIGNED_RATIO.cs new file mode 100644 index 00000000..9d950570 --- /dev/null +++ b/src/TerraFX.Interop.Windows/Windows/um/dwmapi/UNSIGNED_RATIO.cs @@ -0,0 +1,18 @@ +// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +// Ported from um/dwmapi.h in the Windows SDK for Windows 10.0.22000.0 +// Original source is Copyright © Microsoft. All rights reserved. + +using System.Runtime.InteropServices; + +namespace TerraFX.Interop.Windows; + +[StructLayout(LayoutKind.Sequential, Pack = 1)] +internal partial struct UNSIGNED_RATIO +{ + [NativeTypeName("UINT32")] + public uint uiNumerator; + + [NativeTypeName("UINT32")] + public uint uiDenominator; +} \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows/Windows/um/dwmapi/Windows.cs b/src/TerraFX.Interop.Windows/Windows/um/dwmapi/Windows.cs new file mode 100644 index 00000000..ed27198f --- /dev/null +++ b/src/TerraFX.Interop.Windows/Windows/um/dwmapi/Windows.cs @@ -0,0 +1,14 @@ +// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +// Ported from um/dwmapi.h in the Windows SDK for Windows 10.0.22000.0 +// Original source is Copyright © Microsoft. All rights reserved. + +using System.Runtime.InteropServices; + +namespace TerraFX.Interop.Windows; + +internal static unsafe partial class Windows +{ + [DllImport("dwmapi", ExactSpelling = true)] + public static extern HRESULT DwmGetCompositionTimingInfo(HWND hwnd, DWM_TIMING_INFO* pTimingInfo); +} \ No newline at end of file