diff --git a/Gems/AtomLyIntegration/AtomViewportDisplayInfo/Code/Source/AtomViewportDisplayInfoSystemComponent.cpp b/Gems/AtomLyIntegration/AtomViewportDisplayInfo/Code/Source/AtomViewportDisplayInfoSystemComponent.cpp index 07af2e445d..ecdebbd6c4 100644 --- a/Gems/AtomLyIntegration/AtomViewportDisplayInfo/Code/Source/AtomViewportDisplayInfoSystemComponent.cpp +++ b/Gems/AtomLyIntegration/AtomViewportDisplayInfo/Code/Source/AtomViewportDisplayInfoSystemComponent.cpp @@ -248,68 +248,50 @@ namespace AZ::Render void AtomViewportDisplayInfoSystemComponent::UpdateFramerate() { - if (!m_tickRequests) - { - m_tickRequests = AZ::TickRequestBus::FindFirstHandler(); - if (!m_tickRequests) - { - return; - } - } - - AZ::ScriptTimePoint currentTime = m_tickRequests->GetTimeAtCurrentTick(); - // Only keep as much sampling data as is required by our FPS history. - while (!m_fpsHistory.empty() && (currentTime.Get() - m_fpsHistory.front().Get()) > m_fpsInterval) + auto currentTime = AZStd::chrono::system_clock::now(); + while (!m_fpsHistory.empty() && (currentTime - m_fpsHistory.front()) > m_fpsInterval) { m_fpsHistory.pop_front(); } - - // Discard entries with a zero time-delta (can happen when we don't have window focus). - if (m_fpsHistory.empty() || (currentTime.Get() - m_fpsHistory.back().Get()) != AZStd::chrono::seconds(0)) - { - m_fpsHistory.push_back(currentTime); - } + m_fpsHistory.push_back(currentTime); } void AtomViewportDisplayInfoSystemComponent::DrawFramerate() { - AZStd::chrono::duration actualInterval = AZStd::chrono::seconds(0); - AZStd::optional lastTime; - AZStd::optional minFPS; - AZStd::optional maxFPS; - for (const AZ::ScriptTimePoint& time : m_fpsHistory) + AZStd::optional lastTime; + double minFPS = DBL_MAX; + double maxFPS = 0; + AZStd::chrono::duration deltaTime; + for (const auto& time : m_fpsHistory) { if (lastTime.has_value()) { - AZStd::chrono::duration deltaTime = time.Get() - lastTime.value().Get(); + deltaTime = time - lastTime.value(); double fps = AZStd::chrono::seconds(1) / deltaTime; - if (!minFPS.has_value()) - { - minFPS = fps; - maxFPS = fps; - } - else - { - minFPS = AZStd::min(minFPS.value(), fps); - maxFPS = AZStd::max(maxFPS.value(), fps); - } - actualInterval += deltaTime; + minFPS = AZStd::min(minFPS, fps); + maxFPS = AZStd::max(maxFPS, fps); } lastTime = time; } - const double averageFPS = (actualInterval.count() != 0.0) - ? aznumeric_cast(m_fpsHistory.size()) / actualInterval.count() - : 0.0; + double averageFPS = 0; + double averageFrameMs = 0; + if (m_fpsHistory.size() > 1) + { + deltaTime = m_fpsHistory.back() - m_fpsHistory.front(); + averageFPS = AZStd::chrono::seconds(m_fpsHistory.size()) / deltaTime; + averageFrameMs = 1000.0f/averageFPS; + } const double frameIntervalSeconds = m_fpsInterval.count(); DrawLine( AZStd::string::format( - "FPS %.1f [%.0f..%.0f], frame avg over %.1fs", + "FPS %.1f [%.0f..%.0f], %.1fms/frame, avg over %.1fs", averageFPS, - minFPS.value_or(0.0), - maxFPS.value_or(0.0), + minFPS, + maxFPS, + averageFrameMs, frameIntervalSeconds), AZ::Colors::Yellow); } diff --git a/Gems/AtomLyIntegration/AtomViewportDisplayInfo/Code/Source/AtomViewportDisplayInfoSystemComponent.h b/Gems/AtomLyIntegration/AtomViewportDisplayInfo/Code/Source/AtomViewportDisplayInfoSystemComponent.h index 3c49f09242..518911d286 100644 --- a/Gems/AtomLyIntegration/AtomViewportDisplayInfo/Code/Source/AtomViewportDisplayInfoSystemComponent.h +++ b/Gems/AtomLyIntegration/AtomViewportDisplayInfo/Code/Source/AtomViewportDisplayInfoSystemComponent.h @@ -65,9 +65,8 @@ namespace AZ AzFramework::FontDrawInterface* m_fontDrawInterface = nullptr; float m_lineSpacing; AZStd::chrono::duration m_fpsInterval = AZStd::chrono::seconds(1); - AZStd::deque m_fpsHistory; + AZStd::deque m_fpsHistory; AZStd::optional m_lastMemoryUpdate; - AZ::TickRequests* m_tickRequests = nullptr; bool m_updateRootPassQuery = true; }; } // namespace Render