From 7e9b6116da19e2444ff42063c45ca8a1d6b93cfd Mon Sep 17 00:00:00 2001 From: kberg-amzn Date: Tue, 7 Sep 2021 21:04:24 -0700 Subject: [PATCH] Update component application tick to use the ITime interface and respect simulation t_scale settings Signed-off-by: kberg-amzn --- .../AzCore/Component/ComponentApplication.cpp | 20 ++++++--- .../AzCore/Component/ComponentApplication.h | 3 +- Code/Framework/AzCore/AzCore/Time/ITime.h | 41 ++++++++++++++++++- .../AzCore/Time/TimeSystemComponent.cpp | 19 +++++---- .../AzCore/AzCore/Time/TimeSystemComponent.h | 5 ++- 5 files changed, 71 insertions(+), 17 deletions(-) diff --git a/Code/Framework/AzCore/AzCore/Component/ComponentApplication.cpp b/Code/Framework/AzCore/AzCore/Component/ComponentApplication.cpp index eda08401a2..e02485f8d9 100644 --- a/Code/Framework/AzCore/AzCore/Component/ComponentApplication.cpp +++ b/Code/Framework/AzCore/AzCore/Component/ComponentApplication.cpp @@ -612,6 +612,7 @@ namespace AZ AZ_Assert(m_systemEntity, "SystemEntity failed to initialize!"); AddRequiredSystemComponents(m_systemEntity.get()); + //m_currentTime = GetElapsedTimeUs(); m_isStarted = true; return m_systemEntity.get(); } @@ -652,7 +653,6 @@ namespace AZ ComponentApplicationBus::Handler::BusConnect(); - m_currentTime = AZStd::chrono::system_clock::now(); TickRequestBus::Handler::BusConnect(); #if defined(AZ_ENABLE_DEBUG_TOOLS) @@ -1368,14 +1368,18 @@ namespace AZ { AZ_PROFILE_SCOPE(System, "Component application simulation tick"); - AZStd::chrono::system_clock::time_point now = AZStd::chrono::system_clock::now(); + TimeUs now = GetElapsedTimeUs(); + if (m_currentTime == TimeUs{ 0 }) + { + m_currentTime = now; + } m_deltaTime = 0.0f; if (now >= m_currentTime) { - AZStd::chrono::duration delta = now - m_currentTime; - m_deltaTime = deltaOverride >= 0.f ? deltaOverride : delta.count(); + float delta = TimeUsToSeconds(now - m_currentTime); + m_deltaTime = deltaOverride >= 0.f ? deltaOverride : delta; } { @@ -1385,7 +1389,9 @@ namespace AZ m_currentTime = now; { AZ_PROFILE_SCOPE(AzCore, "ComponentApplication::Tick:OnTick"); - EBUS_EVENT(TickBus, OnTick, m_deltaTime, ScriptTimePoint(now)); + auto epoch = AZStd::chrono::time_point(); + auto chronoNow = AZStd::chrono::microseconds(aznumeric_cast(now)); + EBUS_EVENT(TickBus, OnTick, m_deltaTime, ScriptTimePoint(epoch + chronoNow)); } } } @@ -1508,7 +1514,9 @@ namespace AZ //========================================================================= ScriptTimePoint ComponentApplication::GetTimeAtCurrentTick() { - return ScriptTimePoint(m_currentTime); + auto epoch = AZStd::chrono::time_point(); + auto chronoCurrent = AZStd::chrono::microseconds(aznumeric_cast(m_currentTime)); + return ScriptTimePoint(epoch + chronoCurrent); } //========================================================================= diff --git a/Code/Framework/AzCore/AzCore/Component/ComponentApplication.h b/Code/Framework/AzCore/AzCore/Component/ComponentApplication.h index 3768a75d83..1f76f0f79e 100644 --- a/Code/Framework/AzCore/AzCore/Component/ComponentApplication.h +++ b/Code/Framework/AzCore/AzCore/Component/ComponentApplication.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -368,7 +369,7 @@ namespace AZ } } - AZStd::chrono::system_clock::time_point m_currentTime{ AZStd::chrono::system_clock::time_point::max() }; + AZ::TimeUs m_currentTime{ 0 }; float m_deltaTime{ 0.0f }; AZStd::unique_ptr m_moduleManager; AZStd::unique_ptr m_settingsRegistry; diff --git a/Code/Framework/AzCore/AzCore/Time/ITime.h b/Code/Framework/AzCore/AzCore/Time/ITime.h index 845017bd14..96332cd3da 100644 --- a/Code/Framework/AzCore/AzCore/Time/ITime.h +++ b/Code/Framework/AzCore/AzCore/Time/ITime.h @@ -19,6 +19,10 @@ namespace AZ //! This is a strong typedef for representing a millisecond value since application start. AZ_TYPE_SAFE_INTEGRAL(TimeMs, int64_t); + //! This is a strong typedef for representing a microsecond value since application start. + //! Using int64_t as the underlying type, this is good to represent approximately 292,471 years + AZ_TYPE_SAFE_INTEGRAL(TimeUs, int64_t); + //! @class ITime //! @brief This is an AZ::Interface<> for managing time related operations. class ITime @@ -33,6 +37,10 @@ namespace AZ //! @return the number of milliseconds that have elapsed since application start virtual TimeMs GetElapsedTimeMs() const = 0; + //! Returns the number of microseconds since application start. + //! @return the number of microseconds that have elapsed since application start + virtual TimeUs GetElapsedTimeUs() const = 0; + AZ_DISABLE_COPY_MOVE(ITime); }; @@ -51,6 +59,37 @@ namespace AZ { return AZ::Interface::Get()->GetElapsedTimeMs(); } -} + + //! This is a simple convenience wrapper + inline TimeUs GetElapsedTimeUs() + { + return AZ::Interface::Get()->GetElapsedTimeUs(); + } + + //! Converts from milliseconds to microseconds + inline TimeUs TimeMsToUs(TimeMs value) + { + return static_cast(value * static_cast(1000)); + } + + //! Converts from microseconds to milliseconds + inline TimeMs TimeUsToMs(TimeUs value) + { + return static_cast(value * static_cast(1000)); + } + + //! Converts from milliseconds to seconds + inline float TimeMsToSeconds(TimeMs value) + { + return static_cast(value) / 1000.0f; + } + + //! Converts from microseconds to seconds + inline float TimeUsToSeconds(TimeUs value) + { + return static_cast(value) / 1000000.0f; + } +} // namespace AZ AZ_TYPE_SAFE_INTEGRAL_SERIALIZEBINDING(AZ::TimeMs); +AZ_TYPE_SAFE_INTEGRAL_SERIALIZEBINDING(AZ::TimeUs); diff --git a/Code/Framework/AzCore/AzCore/Time/TimeSystemComponent.cpp b/Code/Framework/AzCore/AzCore/Time/TimeSystemComponent.cpp index a2e7733222..99145d377a 100644 --- a/Code/Framework/AzCore/AzCore/Time/TimeSystemComponent.cpp +++ b/Code/Framework/AzCore/AzCore/Time/TimeSystemComponent.cpp @@ -35,7 +35,7 @@ namespace AZ TimeSystemComponent::TimeSystemComponent() { - m_lastInvokedTimeMs = static_cast(AZStd::GetTimeNowMicroSecond() / 1000); + m_lastInvokedTimeUs = static_cast(AZStd::GetTimeNowMicroSecond()); AZ::Interface::Register(this); ITimeRequestBus::Handler::BusConnect(); } @@ -58,18 +58,23 @@ namespace AZ TimeMs TimeSystemComponent::GetElapsedTimeMs() const { - TimeMs currentTime = static_cast(AZStd::GetTimeNowMicroSecond() / 1000); - TimeMs deltaTime = currentTime - m_lastInvokedTimeMs; + return TimeUsToMs(GetElapsedTimeUs()); + } + + TimeUs TimeSystemComponent::GetElapsedTimeUs() const + { + TimeUs currentTime = static_cast(AZStd::GetTimeNowMicroSecond()); + TimeUs deltaTime = currentTime - m_lastInvokedTimeUs; if (t_scale != 1.0f) { float floatDelta = static_cast(deltaTime) * t_scale; - deltaTime = static_cast(static_cast(floatDelta)); + deltaTime = static_cast(static_cast(floatDelta)); } - m_accumulatedTimeMs += deltaTime; - m_lastInvokedTimeMs = currentTime; + m_accumulatedTimeUs += deltaTime; + m_lastInvokedTimeUs = currentTime; - return m_accumulatedTimeMs; + return m_accumulatedTimeUs; } } diff --git a/Code/Framework/AzCore/AzCore/Time/TimeSystemComponent.h b/Code/Framework/AzCore/AzCore/Time/TimeSystemComponent.h index adf576becb..3ab3dbc234 100644 --- a/Code/Framework/AzCore/AzCore/Time/TimeSystemComponent.h +++ b/Code/Framework/AzCore/AzCore/Time/TimeSystemComponent.h @@ -39,11 +39,12 @@ namespace AZ //! ITime overrides. //! @{ TimeMs GetElapsedTimeMs() const override; + TimeUs GetElapsedTimeUs() const override; //! @} private: - mutable TimeMs m_lastInvokedTimeMs = TimeMs{0}; - mutable TimeMs m_accumulatedTimeMs = TimeMs{0}; + mutable TimeUs m_lastInvokedTimeUs = TimeUs{0}; + mutable TimeUs m_accumulatedTimeUs = TimeUs{0}; }; }