From 4bc957bb3ba22c9d3f8b3e5ac76d0fe9b2f18516 Mon Sep 17 00:00:00 2001 From: zilmar Date: Sat, 17 Sep 2016 07:37:27 +1000 Subject: [PATCH] Use the new high performance counter instead of date time class --- Source/Common/Common.vcproj | 8 ++++ Source/Common/Common.vcxproj | 6 ++- Source/Common/Common.vcxproj.filters | 14 ++++-- Source/Common/DateTimeClass.cpp | 40 +--------------- Source/Common/DateTimeClass.h | 5 +- Source/Glide64/Gfx_1.3.h | 6 +-- Source/Glide64/Main.cpp | 11 +++-- .../N64System/FramePerSecondClass.cpp | 48 ++++++++++--------- .../N64System/FramePerSecondClass.h | 19 ++++---- Source/Project64-core/N64System/N64Class.cpp | 2 +- .../N64System/SpeedLimiterClass.cpp | 37 +++++++------- .../N64System/SpeedLimiterClass.h | 6 +-- 12 files changed, 91 insertions(+), 111 deletions(-) diff --git a/Source/Common/Common.vcproj b/Source/Common/Common.vcproj index da6014f0e..516cfa595 100644 --- a/Source/Common/Common.vcproj +++ b/Source/Common/Common.vcproj @@ -137,6 +137,10 @@ RelativePath=".\FileClass.cpp" > + + @@ -222,6 +226,10 @@ RelativePath=".\FileClass.h" > + + diff --git a/Source/Common/Common.vcxproj b/Source/Common/Common.vcxproj index 5d5b1888a..4ce014430 100644 --- a/Source/Common/Common.vcxproj +++ b/Source/Common/Common.vcxproj @@ -33,7 +33,9 @@ + + @@ -46,14 +48,15 @@ - + + @@ -66,7 +69,6 @@ - diff --git a/Source/Common/Common.vcxproj.filters b/Source/Common/Common.vcxproj.filters index 6e53d7b77..437d0328f 100644 --- a/Source/Common/Common.vcxproj.filters +++ b/Source/Common/Common.vcxproj.filters @@ -53,10 +53,13 @@ Source Files - + Source Files - + + Source Files + + Source Files @@ -112,10 +115,13 @@ Header Files - + Header Files - + + Header Files + + Header Files diff --git a/Source/Common/DateTimeClass.cpp b/Source/Common/DateTimeClass.cpp index a08d3cf9d..141e5e89d 100644 --- a/Source/Common/DateTimeClass.cpp +++ b/Source/Common/DateTimeClass.cpp @@ -1,51 +1,15 @@ #include "stdafx.h" #include "DateTimeClass.h" -#ifdef ANDROID -#include -#else -#include -#endif #include CDateTime::CDateTime() { - m_time = 0; -} - -CDateTime & CDateTime::SetToNow(void) -{ -#ifdef ANDROID - struct timespec now; - clock_gettime(CLOCK_REALTIME, &now); - m_time = (now.tv_sec * 1000l) + round(now.tv_nsec / 1.0e6); -#else - struct timeb now; - (void)::ftime(&now); - m_time = (now.time * 1000l) + now.millitm; -#endif - return *this; + m_time = time(NULL); } std::string CDateTime::Format(const char * format) { char buffer[100]; - time_t TimeValue = m_time / 1000l; - strftime(buffer, sizeof(buffer), format, localtime(&TimeValue)); + strftime(buffer, sizeof(buffer), format, localtime(&m_time)); return std::string(buffer); } - -double CDateTime::DiffernceMilliseconds(const CDateTime & compare) -{ - double diff = (double)(m_time - compare.m_time); - return diff / 1000.0; -} - -uint64_t CDateTime::Value(void) -{ - return m_time; -} - -void CDateTime::SetValue(uint64_t value) -{ - m_time = value; -} diff --git a/Source/Common/DateTimeClass.h b/Source/Common/DateTimeClass.h index 5bbfbd17b..b218558df 100644 --- a/Source/Common/DateTimeClass.h +++ b/Source/Common/DateTimeClass.h @@ -7,10 +7,7 @@ public: CDateTime(); CDateTime & SetToNow (void); std::string Format (const char * format); - double DiffernceMilliseconds (const CDateTime & compare); - uint64_t Value(void); - void SetValue(uint64_t value); private: - uint64_t m_time; + time_t m_time; }; \ No newline at end of file diff --git a/Source/Glide64/Gfx_1.3.h b/Source/Glide64/Gfx_1.3.h index c9d22bcde..2e5dd608d 100644 --- a/Source/Glide64/Gfx_1.3.h +++ b/Source/Glide64/Gfx_1.3.h @@ -70,7 +70,7 @@ the plugin #include // offsetof #include #include -#include +#include #include #include "GlideExtensions.h" #include "rdp.h" @@ -150,8 +150,8 @@ extern "C" { #define COLORED_DEBUGGER // ;) pretty colors #ifdef FPS - extern CDateTime fps_last; - extern CDateTime fps_next; + extern HighResTimeStamp fps_last; + extern HighResTimeStamp fps_next; extern float fps; extern uint32_t fps_count; #endif diff --git a/Source/Glide64/Main.cpp b/Source/Glide64/Main.cpp index e21bac4e5..3678a5b2f 100644 --- a/Source/Glide64/Main.cpp +++ b/Source/Glide64/Main.cpp @@ -43,6 +43,7 @@ #include "Version.h" #include #include +#include #include #include #include @@ -91,8 +92,8 @@ int64 perf_next; #endif #ifdef FPS -CDateTime fps_last; -CDateTime fps_next; +HighResTimeStamp fps_last; +HighResTimeStamp fps_next; float fps = 0.0f; uint32_t fps_count = 0; @@ -1796,7 +1797,7 @@ void CALL UpdateScreen(void) // Check frames per second fps_next.SetToNow(); - double diff_secs = fps_next.DiffernceMilliseconds(fps_last); + double diff_secs = (double)(fps_next.GetMicroSeconds() - fps_last.GetMicroSeconds()) / 1000000; if (diff_secs > 0.5f) { fps = (float)(fps_count / diff_secs); @@ -2018,11 +2019,11 @@ void newSwapBuffers() { if (g_settings->clock_24_hr) { - output(956.0f, 0, 1, CDateTime().SetToNow().Format("%H:%M:%S").c_str(), 0); + output(956.0f, 0, 1, CDateTime().Format("%H:%M:%S").c_str(), 0); } else { - output(930.0f, 0, 1, CDateTime().SetToNow().Format("%I:%M:%S %p").c_str(), 0); + output(930.0f, 0, 1, CDateTime().Format("%I:%M:%S %p").c_str(), 0); } } //hotkeys diff --git a/Source/Project64-core/N64System/FramePerSecondClass.cpp b/Source/Project64-core/N64System/FramePerSecondClass.cpp index 04e854844..ab2f1af3b 100644 --- a/Source/Project64-core/N64System/FramePerSecondClass.cpp +++ b/Source/Project64-core/N64System/FramePerSecondClass.cpp @@ -11,16 +11,14 @@ #include "stdafx.h" #include "FramePerSecondClass.h" #include -#ifdef _WIN32 -#include -#endif CFramePerSecond::CFramePerSecond() : m_CurrentViFrame(0), m_CurrentDlistFrame(0), m_iFrameRateType(g_Settings->LoadDword(UserInterface_FrameDisplayType)), m_ScreenHertz(g_Settings->LoadDword(GameRunning_ScreenHertz)), -m_ViFrameRate(0) +m_ViFrameRateWhole(0), +m_ViFrameRateFraction(0) { g_Settings->RegisterChangeCB(UserInterface_FrameDisplayType, this, (CSettings::SettingChangedFunc)FrameRateTypeChanged); g_Settings->RegisterChangeCB(GameRunning_ScreenHertz, this, (CSettings::SettingChangedFunc)ScreenHertzChanged); @@ -42,7 +40,7 @@ void CFramePerSecond::Reset(bool ClearDisplay) { m_CurrentDlistFrame = 0; m_CurrentViFrame = 0; - m_LastViFrame.SetValue(0); + m_LastViFrame.SetMicroSeconds(0); for (int count = 0; count < NoOfFrames; count++) { @@ -57,7 +55,7 @@ void CFramePerSecond::Reset(bool ClearDisplay) if (m_iFrameRateType == FR_VIs) { - DisplayViCounter(0); + DisplayViCounter(-1, 0); } } @@ -71,11 +69,13 @@ void CFramePerSecond::UpdateViCounter(void) } if ((m_CurrentViFrame & 7) == 0) { - CDateTime Time; + HighResTimeStamp Time; Time.SetToNow(); - m_ViFrames[(m_CurrentViFrame >> 3) % NoOfFrames] = Time.Value() - m_LastViFrame.Value(); + + uint64_t time_diff = Time.GetMicroSeconds() - m_LastViFrame.GetMicroSeconds(); + m_ViFrames[(m_CurrentViFrame >> 3) % NoOfFrames] = time_diff; m_LastViFrame = Time; - DisplayViCounter(0); + DisplayViCounter(-1, 0); } m_CurrentViFrame += 1; } @@ -85,11 +85,11 @@ void CFramePerSecond::UpdateDisplay(void) std::string DisplayString; if (m_iFrameRateType == FR_VIs || m_iFrameRateType == FR_VIs_DLs) { - DisplayString = stdstr_f(m_ViFrameRate >= 0 ? "VI/s: %.2f" : "VI/s: -.--", m_ViFrameRate); + DisplayString = stdstr_f(m_ViFrameRateWhole >= 0 ? "VI/s: %d.%d" : "VI/s: -.--", m_ViFrameRateWhole, m_ViFrameRateFraction); } - if (m_iFrameRateType == FR_PERCENT && m_ViFrameRate > 0) + if (m_iFrameRateType == FR_PERCENT && m_ViFrameRateWhole > 0) { - float Percent = ((float)m_ViFrameRate) / m_ScreenHertz; + float Percent = ((float)m_ViFrameRateWhole + ((float)m_ViFrameRateFraction / 100)) / m_ScreenHertz; DisplayString = stdstr_f("%.1f %%", Percent * 100).c_str(); } if (m_iFrameRateType == FR_DLs || m_iFrameRateType == FR_VIs_DLs) @@ -100,15 +100,16 @@ void CFramePerSecond::UpdateDisplay(void) g_Notify->DisplayMessage2(DisplayString.c_str()); } -void CFramePerSecond::DisplayViCounter(uint32_t FrameRate) +void CFramePerSecond::DisplayViCounter(int32_t FrameRateWhole, uint32_t FrameRateFraction) { if (m_iFrameRateType != FR_VIs && m_iFrameRateType != FR_VIs_DLs && m_iFrameRateType != FR_PERCENT) { return; } - if (FrameRate != 0) + if (FrameRateWhole >= 0) { - m_ViFrameRate = (float)FrameRate; + m_ViFrameRateWhole = FrameRateWhole; + m_ViFrameRateFraction = FrameRateFraction; } else { @@ -121,11 +122,14 @@ void CFramePerSecond::DisplayViCounter(uint32_t FrameRate) { Total += m_ViFrames[count]; } - m_ViFrameRate = ((NoOfFrames << 3) / ((float)Total / 1000)); + int baseFPS = (uint32_t)(((uint64_t)NoOfFrames << 3) * 100000000 / Total); + m_ViFrameRateWhole = baseFPS / 100; + m_ViFrameRateFraction = baseFPS % 100; } else { - m_ViFrameRate = -1.0; + m_ViFrameRateWhole = -1; + m_ViFrameRateFraction = 0; } } UpdateDisplay(); @@ -151,10 +155,10 @@ void CFramePerSecond::UpdateDlCounter(void) } if ((m_CurrentDlistFrame & 3) == 0) { - CDateTime Time; - Time.SetToNow(); - m_FramesDlist[(m_CurrentDlistFrame >> 2) % NoOfFrames] = Time.Value() - m_LastDlistFrame.Value(); - m_LastDlistFrame = Time; + HighResTimeStamp Now; + Now.SetToNow(); + m_FramesDlist[(m_CurrentDlistFrame >> 2) % NoOfFrames] = Now.GetMicroSeconds() - m_LastDlistFrame.GetMicroSeconds(); + m_LastDlistFrame = Now; if (m_CurrentDlistFrame > (NoOfFrames << 2)) { int64_t Total; @@ -164,7 +168,7 @@ void CFramePerSecond::UpdateDlCounter(void) { Total += m_FramesDlist[count]; } - m_DlistFrameRate = ((NoOfFrames << 2) / ((float)Total / 1000)); + m_DlistFrameRate = ((NoOfFrames << 2) / ((float)Total / 1000000)); } else { diff --git a/Source/Project64-core/N64System/FramePerSecondClass.h b/Source/Project64-core/N64System/FramePerSecondClass.h index e3e06bc0f..19afef8ed 100644 --- a/Source/Project64-core/N64System/FramePerSecondClass.h +++ b/Source/Project64-core/N64System/FramePerSecondClass.h @@ -9,7 +9,7 @@ * * ****************************************************************************/ #pragma once -#include +#include class CFramePerSecond { @@ -21,7 +21,7 @@ public: void UpdateDlCounter(void); void UpdateViCounter(void); - void DisplayViCounter(uint32_t FrameRate); + void DisplayViCounter(int32_t FrameRateWhole, uint32_t FrameRateFraction); private: CFramePerSecond(const CFramePerSecond&); // Disable copy constructor @@ -35,14 +35,15 @@ private: enum { NoOfFrames = 7 }; - CDateTime m_LastViFrame; - int64_t m_ViFrames[NoOfFrames]; - int32_t m_CurrentViFrame; - float m_ViFrameRate; + HighResTimeStamp m_LastViFrame; + uint64_t m_ViFrames[NoOfFrames]; + uint32_t m_CurrentViFrame; + int32_t m_ViFrameRateWhole; + uint32_t m_ViFrameRateFraction; //Dlist - CDateTime m_LastDlistFrame; - int64_t m_FramesDlist[NoOfFrames]; - int32_t m_CurrentDlistFrame; + HighResTimeStamp m_LastDlistFrame; + uint64_t m_FramesDlist[NoOfFrames]; + uint32_t m_CurrentDlistFrame; float m_DlistFrameRate; }; diff --git a/Source/Project64-core/N64System/N64Class.cpp b/Source/Project64-core/N64System/N64Class.cpp index 8ba9ab41d..16aea41fb 100644 --- a/Source/Project64-core/N64System/N64Class.cpp +++ b/Source/Project64-core/N64System/N64Class.cpp @@ -2089,7 +2089,7 @@ void CN64System::RefreshScreen() uint32_t FrameRate; if (m_Limiter.Timer_Process(&FrameRate) && bDisplayFrameRate()) { - m_FPS.DisplayViCounter(FrameRate); + m_FPS.DisplayViCounter(FrameRate, 0); m_bCleanFrameBox = true; } } diff --git a/Source/Project64-core/N64System/SpeedLimiterClass.cpp b/Source/Project64-core/N64System/SpeedLimiterClass.cpp index b28c56326..1b78cee4e 100644 --- a/Source/Project64-core/N64System/SpeedLimiterClass.cpp +++ b/Source/Project64-core/N64System/SpeedLimiterClass.cpp @@ -11,17 +11,11 @@ #include "stdafx.h" #include "SpeedLimiterClass.h" #include -#ifdef _WIN32 -#include -#include -#pragma comment(lib, "winmm.lib") -#endif CSpeedLimiter::CSpeedLimiter() : m_Frames(0), m_Speed(60), -m_BaseSpeed(60), -m_Ratio(1000.0F / (float)m_Speed) +m_BaseSpeed(60) { } @@ -38,31 +32,37 @@ void CSpeedLimiter::SetHertz(uint32_t Hertz) void CSpeedLimiter::FixSpeedRatio() { - m_Ratio = 1000.0f / static_cast(m_Speed); + m_MicroSecondsPerFrame = 1000000 / m_Speed; m_Frames = 0; } bool CSpeedLimiter::Timer_Process(uint32_t * FrameRate) { m_Frames += 1; - CDateTime CurrentTime; + HighResTimeStamp CurrentTime; CurrentTime.SetToNow(); /* Calculate time that should of elapsed for this frame */ - uint64_t CalculatedTime = (m_LastTime.Value()) + (m_Ratio * (double)m_Frames); - if (CurrentTime.Value() < CalculatedTime) + uint64_t LastTime = m_LastTime.GetMicroSeconds(), CurrentTimeValue = CurrentTime.GetMicroSeconds(); + if (LastTime == 0) { - int32_t time = (int)(CalculatedTime - CurrentTime.Value()); + m_Frames = 0; + m_LastTime = CurrentTime; + return true; + } + uint64_t CalculatedTime = LastTime + (m_MicroSecondsPerFrame * m_Frames); + if (CurrentTimeValue < CalculatedTime) + { + int32_t time = (int)(CalculatedTime - CurrentTimeValue); if (time > 0) { -#ifndef ANDROID - pjutil::Sleep(time); -#endif + pjutil::Sleep((time / 1000) + 1); } /* Refresh current time */ CurrentTime.SetToNow(); + CurrentTimeValue = CurrentTime.GetMicroSeconds(); } - if (CurrentTime.Value() - m_LastTime.Value() >= 1000) + if (CurrentTimeValue - LastTime >= 1000000) { /* Output FPS */ if (FrameRate != NULL) { *FrameRate = m_Frames; } @@ -70,10 +70,7 @@ bool CSpeedLimiter::Timer_Process(uint32_t * FrameRate) m_LastTime = CurrentTime; return true; } - else - { - return false; - } + return false; } void CSpeedLimiter::IncreaseSpeed() diff --git a/Source/Project64-core/N64System/SpeedLimiterClass.h b/Source/Project64-core/N64System/SpeedLimiterClass.h index 10160c42b..e213373ae 100644 --- a/Source/Project64-core/N64System/SpeedLimiterClass.h +++ b/Source/Project64-core/N64System/SpeedLimiterClass.h @@ -11,7 +11,7 @@ #pragma once #include -#include +#include class CSpeedLimiter : private CGameSettings @@ -32,6 +32,6 @@ private: void FixSpeedRatio(); uint32_t m_Speed, m_BaseSpeed, m_Frames; - CDateTime m_LastTime; - double m_Ratio; + HighResTimeStamp m_LastTime; + uint32_t m_MicroSecondsPerFrame; };