Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions GameWorld/GameWorldCore/GameWorld.Core/WpfWindow/D3D11Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,17 @@ public static bool UseASingleSharedGraphicsDevice
public bool IsFixedTimeStep => false;

/// <summary>
/// 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.
/// </summary>
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;

/// <summary>
/// Gets a value indicating whether the controls runs in the context of a designer (e.g.
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
Loading