Skip to content

Commit 9a475d9

Browse files
Merge pull request #406 from DerpyNewb/pr/viewport-fps-cap
Draw the viewport on every WPF frame (37-50 FPS on 75-144 Hz screens)
2 parents ceab35a + 3086669 commit 9a475d9

3 files changed

Lines changed: 45 additions & 5 deletions

File tree

‎GameWorld/GameWorldCore/GameWorld.Core/WpfWindow/D3D11Host.cs‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,17 @@ public static bool UseASingleSharedGraphicsDevice
100100
public bool IsFixedTimeStep => false;
101101

102102
/// <summary>
103-
/// Gets or sets the target time between two updates. Defaults to 60fps.
104-
/// 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.
103+
/// Gets or sets the minimum time between two updates. Defaults to zero: draw on every WPF frame.
104+
/// WPF raises CompositionTarget.Rendering once per display refresh, not at a fixed 60 FPS, and a
105+
/// skipped frame's time is not carried over, so a cap that does not divide the refresh period
106+
/// drops whole frames - 16 ms on a 75 Hz display drew every other frame, 37 FPS.
105107
/// </summary>
106-
public TimeSpan TargetElapsedTime { get; set; } = TimeSpan.FromTicks(160000); // 60 fps
108+
public TimeSpan TargetElapsedTime { get; set; } = DefaultTargetElapsedTime;
109+
110+
internal static readonly TimeSpan DefaultTargetElapsedTime = TimeSpan.Zero;
111+
112+
internal static bool IsFrameDue(TimeSpan lastRenderingTime, TimeSpan renderingTime, TimeSpan targetElapsedTime)
113+
=> renderingTime - lastRenderingTime >= targetElapsedTime;
107114

108115
/// <summary>
109116
/// 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)
554561

555562
var deltaTicks = renderingEventArgs.RenderingTime.Ticks - _lastRenderingTime.Ticks;
556563
// accumulate until time is greater than target time between frames
557-
if (deltaTicks >= TargetElapsedTime.Ticks)
564+
if (IsFrameDue(_lastRenderingTime, renderingEventArgs.RenderingTime, TargetElapsedTime))
558565
{
559566
// enough time has passed to draw a single frame
560567
// draw into cache

‎GameWorld/GameWorldCore/GameWorld.Core/WpfWindow/WpfGame.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ protected sealed override void Render(GameTime time)
144144
{
145145
try
146146
{
147-
// just run as fast as possible, WPF itself is limited to 60 FPS so that's the max we will get
147+
// just run as fast as possible, WPF raises one frame per display refresh so that's the max we will get
148148
Update(time);
149149
Draw(time);
150150
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using GameWorld.Core.WpfWindow;
2+
3+
namespace GameWorld.Core.Test.Rendering
4+
{
5+
// WPF raises CompositionTarget.Rendering once per display refresh, not at a fixed 60 Hz, and
6+
// D3D11Host only draws on the ticks IsFrameDue lets through. A cap that does not divide the
7+
// refresh period drops whole ticks: a 16 ms cap on a 75 Hz display drew every other one.
8+
internal class FrameRateTests
9+
{
10+
[TestCase(60)]
11+
[TestCase(75)]
12+
[TestCase(100)]
13+
[TestCase(144)]
14+
public void DefaultTarget_DrawsEveryDisplayRefresh(int refreshHz)
15+
{
16+
var period = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / refreshHz);
17+
var lastRendered = TimeSpan.Zero;
18+
var framesDrawn = 0;
19+
20+
for (var tick = 1; tick <= refreshHz; tick++)
21+
{
22+
var renderingTime = period * tick;
23+
if (D3D11Host.IsFrameDue(lastRendered, renderingTime, D3D11Host.DefaultTargetElapsedTime))
24+
{
25+
framesDrawn++;
26+
lastRendered = renderingTime;
27+
}
28+
}
29+
30+
Assert.That(framesDrawn, Is.EqualTo(refreshHz), $"frames drawn in one second at {refreshHz} Hz");
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)