Replace float/UInt64 durations by a more precise Time class (#388)

Improve Clock class with atomic RestartIfOver method and allows to choose required precision
This commit is contained in:
Jérôme Leclercq
2022-12-29 21:31:46 +01:00
committed by GitHub
parent 1de5f65536
commit dd421a6385
84 changed files with 1278 additions and 663 deletions

View File

@@ -1,162 +0,0 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Clock.hpp>
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <Nazara/Core/Win32/ClockImpl.hpp>
#elif defined(NAZARA_PLATFORM_POSIX)
#include <Nazara/Core/Posix/ClockImpl.hpp>
#else
#error OS not handled
#endif
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
namespace Detail
{
UInt64 GetMicrosecondsLowPrecision()
{
return ClockImplGetElapsedMilliseconds()*1000ULL;
}
UInt64 GetElapsedMicrosecondsFirstRun()
{
if (ClockImplInitializeHighPrecision())
GetElapsedMicroseconds = ClockImplGetElapsedMicroseconds;
else
GetElapsedMicroseconds = GetMicrosecondsLowPrecision;
return GetElapsedMicroseconds();
}
}
/*!
* \ingroup core
* \class Nz::Clock
* \brief Utility class that measure the elapsed time
*/
/*!
* \brief Constructs a Clock object
*
* \param startingValue The starting time value, in microseconds
* \param paused The clock pause state
*/
Clock::Clock(UInt64 startingValue, bool paused) :
m_elapsedTime(startingValue),
m_refTime(GetElapsedMicroseconds()),
m_paused(paused)
{
}
/*!
* Returns the elapsed time in seconds
* \return Seconds elapsed
*
* \see GetMicroseconds, GetMilliseconds
*/
float Clock::GetSeconds() const
{
return GetMicroseconds()/1'000'000.f;
}
/*!
* Returns the elapsed time in microseconds
* \return Microseconds elapsed
*
* \see GetMilliseconds, GetSeconds
*/
UInt64 Clock::GetMicroseconds() const
{
UInt64 elapsedMicroseconds = m_elapsedTime;
if (!m_paused)
elapsedMicroseconds += (GetElapsedMicroseconds() - m_refTime);
return elapsedMicroseconds;
}
/*!
* Returns the elapsed time in milliseconds
* \return Milliseconds elapsed
*
* \see GetMicroseconds, GetSeconds
*/
UInt64 Clock::GetMilliseconds() const
{
return GetMicroseconds()/1000;
}
/*!
* Returns the current pause state of the clock
* \return Boolean indicating if the clock is currently paused
*
* \see Pause, Unpause
*/
bool Clock::IsPaused() const
{
return m_paused;
}
/*!
* \brief Pause the clock
*
* Pauses the clock, making the time retrieving functions to always return the value at the time the clock was paused
* This has no effect if the clock is already paused
*
* \see IsPaused, Unpause
*/
void Clock::Pause()
{
if (!m_paused)
{
m_elapsedTime += GetElapsedMicroseconds() - m_refTime;
m_paused = true;
}
}
/*!
* \brief Restart the clock
* \return Microseconds elapsed
*
* Restarts the clock, putting it's time counter back to zero (as if the clock got constructed).
* It also compute the elapsed microseconds since the last Restart() call without any time loss (a problem that the combination of GetElapsedMicroseconds and Restart have).
*/
UInt64 Clock::Restart(UInt64 startingValue, bool paused)
{
Nz::UInt64 now = GetElapsedMicroseconds();
Nz::UInt64 elapsedTime = m_elapsedTime;
if (!m_paused)
elapsedTime += (now - m_refTime);
m_elapsedTime = startingValue;
m_refTime = now;
m_paused = paused;
return elapsedTime;
}
/*!
* \brief Unpause the clock
*
* Unpauses the clock, making the clock continue to measure the time
* This has no effect if the clock is already unpaused
*
* \see IsPaused, Unpause
*/
void Clock::Unpause()
{
if (m_paused)
{
m_refTime = GetElapsedMicroseconds();
m_paused = false;
}
}
ClockFunction GetElapsedMicroseconds = Detail::GetElapsedMicrosecondsFirstRun;
ClockFunction GetElapsedMilliseconds = ClockImplGetElapsedMilliseconds;
}

View File

@@ -0,0 +1,29 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Darwin/TimeImpl.hpp>
#include <time.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
bool InitializeHighPrecisionTimer()
{
return true; //< No initialization required
}
Time GetElapsedNanosecondsImpl()
{
UInt64 nanoseconds = clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW);
return Time::Nanoseconds(static_cast<Int64>(nanoseconds));
}
Time GetElapsedMillisecondsImpl()
{
timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
return Time::Seconds(time.tv_sec) + Time::Nanoseconds(time.tv_nsec);
}
}

View File

@@ -0,0 +1,20 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_CORE_DARWIN_TIMEIMPL_HPP
#define NAZARA_CORE_DARWIN_TIMEIMPL_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Time.hpp>
namespace Nz
{
bool InitializeHighPrecisionTimer();
Time GetElapsedNanosecondsImpl();
Time GetElapsedMillisecondsImpl();
}
#endif // NAZARA_CORE_DARWIN_TIMEIMPL_HPP

View File

@@ -1,29 +0,0 @@
// Copyright (C) 2022 Alexandre Janniaux
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Posix/ClockImpl.hpp>
#include <sys/time.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
bool ClockImplInitializeHighPrecision()
{
return true; // No initialization needed
}
UInt64 ClockImplGetElapsedMicroseconds()
{
timeval clock;
gettimeofday(&clock, nullptr);
return static_cast<UInt64>(clock.tv_sec*1000000 + clock.tv_usec);
}
UInt64 ClockImplGetElapsedMilliseconds()
{
timeval clock;
gettimeofday(&clock, nullptr);
return static_cast<UInt64>(clock.tv_sec*1000 + (clock.tv_usec/1000));
}
}

View File

@@ -1,19 +0,0 @@
// Copyright (C) 2022 Alexandre Janniaux
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_CORE_POSIX_CLOCKIMPL_HPP
#define NAZARA_CORE_POSIX_CLOCKIMPL_HPP
#include <Nazara/Prerequisites.hpp>
namespace Nz
{
bool ClockImplInitializeHighPrecision();
UInt64 ClockImplGetElapsedMicroseconds();
UInt64 ClockImplGetElapsedMilliseconds();
}
#endif // NAZARA_CORE_POSIX_CLOCKIMPL_HPP

View File

@@ -0,0 +1,31 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Posix/TimeImpl.hpp>
#include <time.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
bool InitializeHighPrecisionTimer()
{
return true; //< No initialization required
}
Time GetElapsedNanosecondsImpl()
{
timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
return Time::Seconds(time.tv_sec) + Time::Nanoseconds(time.tv_nsec);
}
Time GetElapsedMillisecondsImpl()
{
timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
return Time::Seconds(time.tv_sec) + Time::Nanoseconds(time.tv_nsec);
}
}

View File

@@ -4,16 +4,17 @@
#pragma once
#ifndef NAZARA_CORE_WIN32_CLOCKIMPL_HPP
#define NAZARA_CORE_WIN32_CLOCKIMPL_HPP
#ifndef NAZARA_CORE_POSIX_TIMEIMPL_HPP
#define NAZARA_CORE_POSIX_TIMEIMPL_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Time.hpp>
namespace Nz
{
bool ClockImplInitializeHighPrecision();
UInt64 ClockImplGetElapsedMicroseconds();
UInt64 ClockImplGetElapsedMilliseconds();
bool InitializeHighPrecisionTimer();
Time GetElapsedNanosecondsImpl();
Time GetElapsedMillisecondsImpl();
}
#endif // NAZARA_CORE_WIN32_CLOCKIMPL_HPP
#endif // NAZARA_CORE_POSIX_TIMEIMPL_HPP

View File

@@ -8,7 +8,7 @@
namespace Nz
{
void LifetimeSystem::Update(float elapsedTime)
void LifetimeSystem::Update(Time elapsedTime)
{
auto view = m_registry.view<LifetimeComponent>();
for (auto [entity, lifetimeComponent] : view.each())

View File

@@ -11,10 +11,10 @@ namespace Nz
void SystemGraph::Update()
{
return Update(m_clock.Restart() / 1'000'000.f);
return Update(m_clock.Restart());
}
void SystemGraph::Update(float elapsedTime)
void SystemGraph::Update(Time elapsedTime)
{
if (!m_systemOrderUpdated)
{

36
src/Nazara/Core/Time.cpp Normal file
View File

@@ -0,0 +1,36 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Time.hpp>
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <Nazara/Core/Win32/TimeImpl.hpp>
#elif defined(NAZARA_PLATFORM_MACOS) || defined(NAZARA_PLATFORM_IOS)
#include <Nazara/Core/Darwin/TimeImpl.hpp>
#elif defined(NAZARA_PLATFORM_POSIX)
#include <Nazara/Core/Posix/TimeImpl.hpp>
#else
#error OS not handled
#endif
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
namespace NAZARA_ANONYMOUS_NAMESPACE
{
Time GetElapsedNanosecondsFirstRun()
{
if (InitializeHighPrecisionTimer())
GetElapsedNanoseconds = GetElapsedNanosecondsImpl;
else
GetElapsedNanoseconds = GetElapsedMillisecondsImpl;
return GetElapsedNanoseconds();
}
}
GetElapsedTimeFunction GetElapsedMilliseconds = GetElapsedMillisecondsImpl;
GetElapsedTimeFunction GetElapsedNanoseconds = NAZARA_ANONYMOUS_NAMESPACE_PREFIX(GetElapsedNanosecondsFirstRun);
}

View File

@@ -1,51 +0,0 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Win32/ClockImpl.hpp>
#include <Nazara/Core/Error.hpp>
#include <ctime>
#include <windows.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
namespace NAZARA_ANONYMOUS_NAMESPACE
{
LARGE_INTEGER s_frequency; // La fréquence ne varie pas pas au cours de l'exécution
}
bool ClockImplInitializeHighPrecision()
{
NAZARA_USE_ANONYMOUS_NAMESPACE
return QueryPerformanceFrequency(&s_frequency) != 0;
}
UInt64 ClockImplGetElapsedMicroseconds()
{
NAZARA_USE_ANONYMOUS_NAMESPACE
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx
//HANDLE thread = GetCurrentThread();
//DWORD oldMask = SetThreadAffinityMask(thread, 1);
LARGE_INTEGER time;
QueryPerformanceCounter(&time);
//SetThreadAffinityMask(thread, oldMask);
return time.QuadPart*1000000ULL / s_frequency.QuadPart;
}
UInt64 ClockImplGetElapsedMilliseconds()
{
#ifdef NAZARA_PLATFORM_WINDOWS_VISTA
return GetTickCount64();
#else
return GetTickCount();
#endif
}
}
#include <Nazara/Core/AntiWindows.hpp>

View File

@@ -5,7 +5,7 @@
#include <Nazara/Core/Win32/FileImpl.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/StringExt.hpp>
#include <Nazara/Core/Win32/Time.hpp>
#include <Nazara/Core/Win32/Utils.hpp>
#include <Nazara/Utils/CallOnExit.hpp>
#include <memory>
#include <Nazara/Core/Debug.hpp>

View File

@@ -0,0 +1,62 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Win32/TimeImpl.hpp>
#include <Nazara/Core/Error.hpp>
#include <ctime>
#include <windows.h>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
namespace NAZARA_ANONYMOUS_NAMESPACE
{
LARGE_INTEGER s_frequency; // The frequency of the performance counter is fixed at system boot and is consistent across all processors
}
bool InitializeHighPrecisionTimer()
{
NAZARA_USE_ANONYMOUS_NAMESPACE
return QueryPerformanceFrequency(&s_frequency) != 0 && s_frequency.QuadPart != 0;
}
Time GetElapsedNanosecondsImpl()
{
NAZARA_USE_ANONYMOUS_NAMESPACE
LARGE_INTEGER time;
QueryPerformanceCounter(&time);
if (s_frequency.QuadPart == 10'000'000) //< seems to be a common value
return Time::Nanoseconds(100ll * time.QuadPart);
else
{
// Compute using 128bits precisions
// https://stackoverflow.com/questions/23378063/how-can-i-use-mach-absolute-time-without-overflowing
UInt64 num = 1'000'000'000ll;
UInt64 denom = s_frequency.QuadPart;
UInt64 value = time.QuadPart;
UInt64 high = (value >> 32) * num;
UInt64 low = (value & 0xFFFFFFFFull) * num / denom;
UInt64 highRem = ((high % denom) << 32) / denom;
high /= denom;
return Time::Nanoseconds(SafeCast<Int64>((high << 32) + highRem + low));
}
}
Time GetElapsedMillisecondsImpl()
{
#ifdef NAZARA_UTILS_WINDOWS_NT6
return Time::Milliseconds(GetTickCount64());
#else
return Time::Milliseconds(GetTickCount());
#endif
}
}
#include <Nazara/Core/AntiWindows.hpp>

View File

@@ -0,0 +1,20 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_CORE_WIN32_TIMEIMPL_HPP
#define NAZARA_CORE_WIN32_TIMEIMPL_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Time.hpp>
namespace Nz
{
bool InitializeHighPrecisionTimer();
Time GetElapsedNanosecondsImpl();
Time GetElapsedMillisecondsImpl();
}
#endif // NAZARA_CORE_WIN32_TIMEIMPL_HPP

View File

@@ -2,7 +2,7 @@
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Win32/Time.hpp>
#include <Nazara/Core/Win32/Utils.hpp>
#include <Nazara/Core/Debug.hpp>
namespace Nz

View File

@@ -4,8 +4,8 @@
#pragma once
#ifndef NAZARA_CORE_WIN32_TIME_HPP
#define NAZARA_CORE_WIN32_TIME_HPP
#ifndef NAZARA_CORE_WIN32_UTILS_HPP
#define NAZARA_CORE_WIN32_UTILS_HPP
#include <Nazara/Prerequisites.hpp>
#include <ctime>
@@ -16,4 +16,4 @@ namespace Nz
time_t FileTimeToTime(FILETIME* time);
}
#endif // NAZARA_CORE_WIN32_TIME_HPP
#endif // NAZARA_CORE_WIN32_UTILS_HPP