- Upgrade timer code to use nanosecond accuracy internally

This commit is contained in:
Magnus Norddahl 2017-11-12 12:57:19 +01:00
parent 0db0f2f7b9
commit e3141a4af3
2 changed files with 57 additions and 23 deletions

View file

@ -45,14 +45,34 @@
// //
//========================================================================== //==========================================================================
static unsigned int FirstFrameStartTime; static uint64_t FirstFrameStartTime;
static unsigned int CurrentFrameStartTime; static uint64_t CurrentFrameStartTime;
static unsigned int FreezeTime; static uint64_t FreezeTime;
static uint32_t performanceGetTime() static uint64_t GetClockTimeNS()
{ {
using namespace std::chrono; using namespace std::chrono;
return (uint32_t)duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count(); return (uint64_t)duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count();
}
static uint64_t MSToNS(unsigned int ms)
{
return static_cast<uint64_t>(ms) * 1'000'000;
}
static uint32_t NSToMS(uint64_t ns)
{
return static_cast<uint32_t>(ns / 1'000'000);
}
static int NSToTic(uint64_t ns)
{
return static_cast<int>(ns * TICRATE / 1'000'000'000);
}
static uint64_t TicToNS(int tic)
{
return static_cast<uint64_t>(tic) * 1'000'000'000 / TICRATE;
} }
void I_SetFrameTime() void I_SetFrameTime()
@ -65,7 +85,7 @@ void I_SetFrameTime()
if (FreezeTime == 0) if (FreezeTime == 0)
{ {
CurrentFrameStartTime = performanceGetTime(); CurrentFrameStartTime = GetClockTimeNS();
if (FirstFrameStartTime == 0) if (FirstFrameStartTime == 0)
FirstFrameStartTime = CurrentFrameStartTime; FirstFrameStartTime = CurrentFrameStartTime;
} }
@ -100,15 +120,7 @@ int I_WaitForTic(int prevtic)
return time; return time;
} }
unsigned int I_FPSTime() uint64_t I_NSTime()
{
if (FreezeTime == 0)
return CurrentFrameStartTime;
else
return performanceGetTime();
}
unsigned int I_MSTime()
{ {
if (FreezeTime == 0) if (FreezeTime == 0)
{ {
@ -118,26 +130,44 @@ unsigned int I_MSTime()
{ {
if (FirstFrameStartTime == 0) if (FirstFrameStartTime == 0)
{ {
FirstFrameStartTime = performanceGetTime(); FirstFrameStartTime = GetClockTimeNS();
return 0; return 0;
} }
else else
{ {
return performanceGetTime() - FirstFrameStartTime; return GetClockTimeNS() - FirstFrameStartTime;
} }
} }
} }
uint64_t I_FPSTimeNS()
{
if (FreezeTime == 0)
return NSToMS(CurrentFrameStartTime);
else
return NSToMS(GetClockTimeNS());
}
unsigned int I_MSTime()
{
return NSToMS(I_NSTime());
}
unsigned int I_FPSTime()
{
return NSToMS(I_FPSTimeNS());
}
int I_GetTime() int I_GetTime()
{ {
return (CurrentFrameStartTime - FirstFrameStartTime) * TICRATE / 1000 + 1; return NSToTic(CurrentFrameStartTime - FirstFrameStartTime) + 1;
} }
double I_GetTimeFrac(uint32_t *ms) double I_GetTimeFrac(uint32_t *ms)
{ {
unsigned int currentTic = (CurrentFrameStartTime - FirstFrameStartTime) * TICRATE / 1000; int currentTic = NSToTic(CurrentFrameStartTime - FirstFrameStartTime);
unsigned int ticStartTime = FirstFrameStartTime + currentTic * 1000 / TICRATE; uint64_t ticStartTime = FirstFrameStartTime + TicToNS(currentTic);
unsigned int ticNextTime = FirstFrameStartTime + (currentTic + 1) * 1000 / TICRATE; uint64_t ticNextTime = FirstFrameStartTime + TicToNS(currentTic + 1);
if (ms) if (ms)
*ms = currentTic + 1; *ms = currentTic + 1;
@ -149,11 +179,11 @@ void I_FreezeTime(bool frozen)
{ {
if (frozen) if (frozen)
{ {
FreezeTime = performanceGetTime(); FreezeTime = GetClockTimeNS();
} }
else else
{ {
FirstFrameStartTime += performanceGetTime() - FreezeTime; FirstFrameStartTime += GetClockTimeNS() - FreezeTime;
FreezeTime = 0; FreezeTime = 0;
I_SetFrameTime(); I_SetFrameTime();
} }

View file

@ -22,3 +22,7 @@ void I_FreezeTime(bool frozen);
// [RH] Returns millisecond-accurate time // [RH] Returns millisecond-accurate time
unsigned int I_MSTime(); unsigned int I_MSTime();
unsigned int I_FPSTime(); unsigned int I_FPSTime();
// Nanosecond-accurate time
uint64_t I_NSTime();
uint64_t I_FPSTimeNS();