2018-03-10 02:12:57 +00:00
|
|
|
|
using System.Diagnostics;
|
2018-03-06 20:18:49 +00:00
|
|
|
|
using System.Timers;
|
|
|
|
|
|
2018-06-11 00:46:42 +00:00
|
|
|
|
namespace Ryujinx.HLE
|
2018-03-06 20:18:49 +00:00
|
|
|
|
{
|
|
|
|
|
public class PerformanceStatistics
|
|
|
|
|
{
|
2018-06-24 00:39:25 +00:00
|
|
|
|
private const double FrameRateWeight = 0.5;
|
|
|
|
|
|
|
|
|
|
private const int FrameTypeSystem = 0;
|
|
|
|
|
private const int FrameTypeGame = 1;
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
private double[] AverageFrameRate;
|
|
|
|
|
private double[] AccumulatedFrameTime;
|
|
|
|
|
private double[] PreviousFrameTime;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
private long[] FramesRendered;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
private object[] FrameLock;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
private double TicksToSeconds;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
private Stopwatch ExecutionTime;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
private Timer ResetTimer;
|
2018-03-06 20:18:49 +00:00
|
|
|
|
|
|
|
|
|
public PerformanceStatistics()
|
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
AverageFrameRate = new double[2];
|
|
|
|
|
AccumulatedFrameTime = new double[2];
|
|
|
|
|
PreviousFrameTime = new double[2];
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
FramesRendered = new long[2];
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
FrameLock = new object[] { new object(), new object() };
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
ExecutionTime = new Stopwatch();
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
ExecutionTime.Start();
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
ResetTimer = new Timer(1000);
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
ResetTimer.Elapsed += ResetTimerElapsed;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
ResetTimer.AutoReset = true;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
ResetTimer.Start();
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
TicksToSeconds = 1.0 / Stopwatch.Frequency;
|
2018-03-06 20:18:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ResetTimerElapsed(object sender, ElapsedEventArgs e)
|
|
|
|
|
{
|
2018-06-24 00:39:25 +00:00
|
|
|
|
CalculateAverageFrameRate(FrameTypeSystem);
|
|
|
|
|
CalculateAverageFrameRate(FrameTypeGame);
|
2018-03-06 20:18:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
private void CalculateAverageFrameRate(int FrameType)
|
2018-03-06 20:18:49 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
double FrameRate = 0;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
if (AccumulatedFrameTime[FrameType] > 0)
|
2018-06-24 00:39:25 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
FrameRate = FramesRendered[FrameType] / AccumulatedFrameTime[FrameType];
|
2018-06-24 00:39:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
lock (FrameLock[FrameType])
|
2018-06-24 00:39:25 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
AverageFrameRate[FrameType] = LinearInterpolate(AverageFrameRate[FrameType], FrameRate);
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
FramesRendered[FrameType] = 0;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
AccumulatedFrameTime[FrameType] = 0;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
}
|
2018-03-06 20:18:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
private double LinearInterpolate(double Old, double New)
|
2018-03-06 20:18:49 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
return Old * (1.0 - FrameRateWeight) + New * FrameRateWeight;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RecordSystemFrameTime()
|
|
|
|
|
{
|
|
|
|
|
RecordFrameTime(FrameTypeSystem);
|
2018-03-06 20:18:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RecordGameFrameTime()
|
|
|
|
|
{
|
2018-06-24 00:39:25 +00:00
|
|
|
|
RecordFrameTime(FrameTypeGame);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
private void RecordFrameTime(int FrameType)
|
2018-06-24 00:39:25 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
double CurrentFrameTime = ExecutionTime.ElapsedTicks * TicksToSeconds;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
double ElapsedFrameTime = CurrentFrameTime - PreviousFrameTime[FrameType];
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
PreviousFrameTime[FrameType] = CurrentFrameTime;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
lock (FrameLock[FrameType])
|
2018-06-24 00:39:25 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
AccumulatedFrameTime[FrameType] += ElapsedFrameTime;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
FramesRendered[FrameType]++;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double GetSystemFrameRate()
|
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
return AverageFrameRate[FrameTypeSystem];
|
2018-03-06 20:18:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-24 00:39:25 +00:00
|
|
|
|
public double GetGameFrameRate()
|
2018-03-06 20:18:49 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
return AverageFrameRate[FrameTypeGame];
|
2018-03-06 20:18:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|