diff --git a/src/common/timer.cpp b/src/common/timer.cpp index 6db98ae8..5c583bff 100644 --- a/src/common/timer.cpp +++ b/src/common/timer.cpp @@ -12,10 +12,11 @@ namespace Common { #ifdef _WIN32 static double s_counter_frequency; +static bool s_counter_initialized = false; -Timer::Value Timer::GetValue(void) +// counter frequency does not change after system startup, so we can cache it +static double GetCounterFrequency() { - static bool s_counter_initialized = false; // even if this races, it should still result in the same value.. if (!s_counter_initialized) { @@ -24,32 +25,25 @@ Timer::Value Timer::GetValue(void) s_counter_frequency = static_cast(Freq.QuadPart) / 1000000000.0; s_counter_initialized = true; } + return s_counter_frequency; +} + +Timer::Value Timer::GetValue(void) +{ + GetCounterFrequency(); Timer::Value ReturnValue; QueryPerformanceCounter(reinterpret_cast(&ReturnValue)); return ReturnValue; } -double Timer::ConvertValueToNanoseconds(Timer::Value value) -{ - return (static_cast(value) / s_counter_frequency); -} - -double Timer::ConvertValueToMilliseconds(Timer::Value value) -{ - return ((static_cast(value) / s_counter_frequency) / 1000000.0); -} - -double Timer::ConvertValueToSeconds(Timer::Value value) -{ - return ((static_cast(value) / s_counter_frequency) / 1000000000.0); -} - Timer::Value Timer::ConvertSecondsToValue(double s) { - return static_cast((s * 1000000000.0) * s_counter_frequency); + return static_cast((s * 1000000000.0) * GetCounterFrequency()); } + #else + Timer::Value Timer::GetValue(void) { struct timespec tv; @@ -57,45 +51,11 @@ Timer::Value Timer::GetValue(void) return ((Value)tv.tv_nsec + (Value)tv.tv_sec * 1000000000); } -double Timer::ConvertValueToNanoseconds(Timer::Value value) -{ - return static_cast(value); -} - -double Timer::ConvertValueToMilliseconds(Timer::Value value) -{ - return (static_cast(value) / 1000000.0); -} - -double Timer::ConvertValueToSeconds(Timer::Value value) -{ - return (static_cast(value) / 1000000000.0); -} - Timer::Value Timer::ConvertSecondsToValue(double s) { return static_cast(s * 1000000000.0); } + #endif -Timer::Timer() -{ - Reset(); -} - -void Timer::Reset() -{ - m_tvStartValue = GetValue(); -} - -double Timer::GetTimeSeconds() const -{ - return ConvertValueToSeconds(GetValue() - m_tvStartValue); -} - -double Timer::GetTimeMilliseconds() const -{ - return ConvertValueToMilliseconds(GetValue() - m_tvStartValue); -} - } // namespace Common diff --git a/src/common/timer.h b/src/common/timer.h index f1da4744..4d692bfe 100644 --- a/src/common/timer.h +++ b/src/common/timer.h @@ -3,26 +3,17 @@ namespace Common { +// Minimal monotonic timer used only for host-side throttling/sleep decisions +// (never for emulation state - that would break determinism). Time is kept in +// integer Value units; thresholds are produced with ConvertSecondsToValue and +// compared directly, so no time is ever stored in floating point. class Timer { public: using Value = std::uint64_t; - Timer(); - static Value GetValue(void); - static double ConvertValueToSeconds(Value value); - static double ConvertValueToMilliseconds(Value value); - static double ConvertValueToNanoseconds(Value value); static Value ConvertSecondsToValue(double s); - - void Reset(void); - - double GetTimeSeconds(void) const; - double GetTimeMilliseconds(void) const; - -private: - Value m_tvStartValue; }; } // namespace Common diff --git a/src/core/texture_replacements.cpp b/src/core/texture_replacements.cpp index 4cee4358..2afd38ab 100644 --- a/src/core/texture_replacements.cpp +++ b/src/core/texture_replacements.cpp @@ -223,18 +223,17 @@ const TextureReplacementTexture* TextureReplacements::LoadTexture(const std::str void TextureReplacements::PreloadTextures() { - static constexpr float UPDATE_INTERVAL = 1.0f; - - Common::Timer last_update_time; + const Common::Timer::Value update_interval = Common::Timer::ConvertSecondsToValue(1.0); + Common::Timer::Value last_update_time = Common::Timer::GetValue(); uint32_t num_textures_loaded = 0; const uint32_t total_textures = static_cast(m_vram_write_replacements.size()); #define UPDATE_PROGRESS() \ - if (last_update_time.GetTimeSeconds() >= UPDATE_INTERVAL) \ + if ((Common::Timer::GetValue() - last_update_time) >= update_interval) \ { \ g_host_interface->DisplayLoadingScreen("Preloading replacement textures...", 0, static_cast(total_textures), \ static_cast(num_textures_loaded)); \ - last_update_time.Reset(); \ + last_update_time = Common::Timer::GetValue(); \ } for (const auto& it : m_vram_write_replacements)