diff --git a/GameWorld/GameWorldCore/GameWorld.Core/WpfWindow/D3D11Host.cs b/GameWorld/GameWorldCore/GameWorld.Core/WpfWindow/D3D11Host.cs index e0b903619..7eca2250a 100644 --- a/GameWorld/GameWorldCore/GameWorld.Core/WpfWindow/D3D11Host.cs +++ b/GameWorld/GameWorldCore/GameWorld.Core/WpfWindow/D3D11Host.cs @@ -100,10 +100,17 @@ public static bool UseASingleSharedGraphicsDevice public bool IsFixedTimeStep => false; /// - /// Gets or sets the target time between two updates. Defaults to 60fps. - /// WPF is limiting its rendering to 60 FPS max, therefore setting a target value higher than 60 fps (lower than TimeSpan.FromSeconds(1 / 60.0)) will have no effect. + /// Gets or sets the minimum time between two updates. Defaults to zero: draw on every WPF frame. + /// WPF raises CompositionTarget.Rendering once per display refresh, not at a fixed 60 FPS, and a + /// skipped frame's time is not carried over, so a cap that does not divide the refresh period + /// drops whole frames - 16 ms on a 75 Hz display drew every other frame, 37 FPS. /// - public TimeSpan TargetElapsedTime { get; set; } = TimeSpan.FromTicks(160000); // 60 fps + public TimeSpan TargetElapsedTime { get; set; } = DefaultTargetElapsedTime; + + internal static readonly TimeSpan DefaultTargetElapsedTime = TimeSpan.Zero; + + internal static bool IsFrameDue(TimeSpan lastRenderingTime, TimeSpan renderingTime, TimeSpan targetElapsedTime) + => renderingTime - lastRenderingTime >= targetElapsedTime; /// /// Gets a value indicating whether the controls runs in the context of a designer (e.g. @@ -554,7 +561,7 @@ private void OnRendering(object sender, EventArgs eventArgs) var deltaTicks = renderingEventArgs.RenderingTime.Ticks - _lastRenderingTime.Ticks; // accumulate until time is greater than target time between frames - if (deltaTicks >= TargetElapsedTime.Ticks) + if (IsFrameDue(_lastRenderingTime, renderingEventArgs.RenderingTime, TargetElapsedTime)) { // enough time has passed to draw a single frame // draw into cache diff --git a/GameWorld/GameWorldCore/GameWorld.Core/WpfWindow/WpfGame.cs b/GameWorld/GameWorldCore/GameWorld.Core/WpfWindow/WpfGame.cs index 3903e6d35..575379eed 100644 --- a/GameWorld/GameWorldCore/GameWorld.Core/WpfWindow/WpfGame.cs +++ b/GameWorld/GameWorldCore/GameWorld.Core/WpfWindow/WpfGame.cs @@ -144,7 +144,7 @@ protected sealed override void Render(GameTime time) { try { - // just run as fast as possible, WPF itself is limited to 60 FPS so that's the max we will get + // just run as fast as possible, WPF raises one frame per display refresh so that's the max we will get Update(time); Draw(time); } diff --git a/GameWorld/GameWorldCore/GameWorld.CoreTest/Rendering/FrameRateTests.cs b/GameWorld/GameWorldCore/GameWorld.CoreTest/Rendering/FrameRateTests.cs new file mode 100644 index 000000000..e0f40fd69 --- /dev/null +++ b/GameWorld/GameWorldCore/GameWorld.CoreTest/Rendering/FrameRateTests.cs @@ -0,0 +1,33 @@ +using GameWorld.Core.WpfWindow; + +namespace GameWorld.Core.Test.Rendering +{ + // WPF raises CompositionTarget.Rendering once per display refresh, not at a fixed 60 Hz, and + // D3D11Host only draws on the ticks IsFrameDue lets through. A cap that does not divide the + // refresh period drops whole ticks: a 16 ms cap on a 75 Hz display drew every other one. + internal class FrameRateTests + { + [TestCase(60)] + [TestCase(75)] + [TestCase(100)] + [TestCase(144)] + public void DefaultTarget_DrawsEveryDisplayRefresh(int refreshHz) + { + var period = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / refreshHz); + var lastRendered = TimeSpan.Zero; + var framesDrawn = 0; + + for (var tick = 1; tick <= refreshHz; tick++) + { + var renderingTime = period * tick; + if (D3D11Host.IsFrameDue(lastRendered, renderingTime, D3D11Host.DefaultTargetElapsedTime)) + { + framesDrawn++; + lastRendered = renderingTime; + } + } + + Assert.That(framesDrawn, Is.EqualTo(refreshHz), $"frames drawn in one second at {refreshHz} Hz"); + } + } +}