diff --git a/Ryujinx.Core/PerformanceStatistics.cs b/Ryujinx.Core/PerformanceStatistics.cs index 57a43961ed..3740daa584 100644 --- a/Ryujinx.Core/PerformanceStatistics.cs +++ b/Ryujinx.Core/PerformanceStatistics.cs @@ -2,12 +2,14 @@ using System.Collections.Generic; using System.Diagnostics; using System.Text; +using System.Timers; namespace Ryujinx.Core { public class PerformanceStatistics { Stopwatch ExecutionTime = new Stopwatch(); + Timer ResetTimer = new Timer(1000); long CurrentGameFrameEnded; long CurrentSystemFrameEnded; @@ -15,25 +17,35 @@ namespace Ryujinx.Core long LastGameFrameEnded; long LastSystemFrameEnded; - public double CurrentGameFrameTime; - public double CurrentSystemFrameTime; - public double PreviousGameFrameTime; - public double PreviousSystemFrameTime; - public double GameFrameRate => 1000f / (CurrentSystemFrameTime / 1000); - public double SystemFrameRate => 1000f/(CurrentSystemFrameTime/1000); + double AccumulatedGameFrameTime; + double AccumulatedSystemFrameTime; + double CurrentGameFrameTime; + double CurrentSystemFrameTime; + double PreviousGameFrameTime; + double PreviousSystemFrameTime; + public double GameFrameRate { get; private set; } + public double SystemFrameRate { get; private set; } public long SystemFramesRendered; public long GameFramesRendered; - public long ElapsedMilliseconds { get => ExecutionTime.ElapsedMilliseconds; } - public long ElapsedMicroseconds { get => (long) - (((double)ExecutionTime.ElapsedTicks / Stopwatch.Frequency) * 1000000); } - public long ElapsedNanoseconds { get => (long) - (((double)ExecutionTime.ElapsedTicks / Stopwatch.Frequency) * 1000000000); } + public long ElapsedMilliseconds => ExecutionTime.ElapsedMilliseconds; + public long ElapsedMicroseconds => (long) + (((double)ExecutionTime.ElapsedTicks / Stopwatch.Frequency) * 1000000); + public long ElapsedNanoseconds => (long) + (((double)ExecutionTime.ElapsedTicks / Stopwatch.Frequency) * 1000000000); public PerformanceStatistics() { ExecutionTime.Start(); + ResetTimer.Elapsed += ResetTimerElapsed; + ResetTimer.AutoReset = true; + ResetTimer.Start(); } - + + private void ResetTimerElapsed(object sender, ElapsedEventArgs e) + { + ResetStatistics(); + } + public void StartSystemFrame() { PreviousSystemFrameTime = CurrentSystemFrameTime; @@ -45,6 +57,7 @@ namespace Ryujinx.Core { CurrentSystemFrameEnded = ElapsedMicroseconds; CurrentSystemFrameTime = CurrentSystemFrameEnded - CurrentSystemFrameStart; + AccumulatedSystemFrameTime += CurrentSystemFrameTime; SystemFramesRendered++; } @@ -54,7 +67,21 @@ namespace Ryujinx.Core CurrentGameFrameTime = CurrentGameFrameEnded - LastGameFrameEnded; PreviousGameFrameTime = CurrentGameFrameTime; LastGameFrameEnded = CurrentGameFrameEnded; + AccumulatedGameFrameTime += CurrentGameFrameTime; GameFramesRendered++; } + + public void ResetStatistics() + { + GameFrameRate = 1000 / ((AccumulatedGameFrameTime / GameFramesRendered) / 1000); + GameFrameRate = double.IsNaN(GameFrameRate) ? 0 : GameFrameRate; + SystemFrameRate = 1000 / ((AccumulatedSystemFrameTime / SystemFramesRendered) / 1000); + SystemFrameRate = double.IsNaN(SystemFrameRate) ? 0 : SystemFrameRate; + + GameFramesRendered = 0; + SystemFramesRendered = 0; + AccumulatedGameFrameTime = 0; + AccumulatedSystemFrameTime = 0; + } } }