ATOM-15842 Update the fps calculation for editor (#1500)

main
Qing Tao 5 years ago committed by GitHub
parent a160ffb9a3
commit 81c2bf54d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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<double> actualInterval = AZStd::chrono::seconds(0);
AZStd::optional<AZ::ScriptTimePoint> lastTime;
AZStd::optional<double> minFPS;
AZStd::optional<double> maxFPS;
for (const AZ::ScriptTimePoint& time : m_fpsHistory)
AZStd::optional<AZStd::chrono::system_clock::time_point> lastTime;
double minFPS = DBL_MAX;
double maxFPS = 0;
AZStd::chrono::duration<double> deltaTime;
for (const auto& time : m_fpsHistory)
{
if (lastTime.has_value())
{
AZStd::chrono::duration<double> 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<double>(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);
}

@ -65,9 +65,8 @@ namespace AZ
AzFramework::FontDrawInterface* m_fontDrawInterface = nullptr;
float m_lineSpacing;
AZStd::chrono::duration<double> m_fpsInterval = AZStd::chrono::seconds(1);
AZStd::deque<AZ::ScriptTimePoint> m_fpsHistory;
AZStd::deque<AZStd::chrono::system_clock::time_point> m_fpsHistory;
AZStd::optional<AZStd::chrono::system_clock::time_point> m_lastMemoryUpdate;
AZ::TickRequests* m_tickRequests = nullptr;
bool m_updateRootPassQuery = true;
};
} // namespace Render

Loading…
Cancel
Save