From 2d5f44a7ff3729ba30a4de211a5da519bbb793fc Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Thu, 12 Dec 2019 21:24:39 +0100 Subject: [PATCH] [hud] Implement FPS display as a HUD item --- src/dxvk/hud/dxvk_hud.cpp | 1 + src/dxvk/hud/dxvk_hud_item.cpp | 34 ++++++++++++++++++++++++++++++++++ src/dxvk/hud/dxvk_hud_item.h | 27 +++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/src/dxvk/hud/dxvk_hud.cpp b/src/dxvk/hud/dxvk_hud.cpp index 50c6564d2..4fff49b3a 100644 --- a/src/dxvk/hud/dxvk_hud.cpp +++ b/src/dxvk/hud/dxvk_hud.cpp @@ -37,6 +37,7 @@ namespace dxvk::hud { addItem("version"); addItem("api", m_device); addItem("devinfo", m_device); + addItem("fps"); } diff --git a/src/dxvk/hud/dxvk_hud_item.cpp b/src/dxvk/hud/dxvk_hud_item.cpp index 34c90bb4d..c23195c1b 100644 --- a/src/dxvk/hud/dxvk_hud_item.cpp +++ b/src/dxvk/hud/dxvk_hud_item.cpp @@ -147,4 +147,38 @@ namespace dxvk::hud { return position; } + + HudFpsItem::HudFpsItem() { } + HudFpsItem::~HudFpsItem() { } + + + void HudFpsItem::update(dxvk::high_resolution_clock::time_point time) { + m_frameCount += 1; + + auto elapsed = std::chrono::duration_cast(time - m_lastUpdate); + + if (elapsed.count() >= UpdateInterval) { + int64_t fps = (10'000'000ll * m_frameCount) / elapsed.count(); + + m_frameRate = str::format("FPS: ", fps / 10, ".", fps % 10); + m_frameCount = 0; + m_lastUpdate = time; + } + } + + + HudPos HudFpsItem::render( + HudRenderer& renderer, + HudPos position) { + position.y += 16.0f; + + renderer.drawText(16.0f, + { position.x, position.y }, + { 1.0f, 1.0f, 1.0f, 1.0f }, + m_frameRate); + + position.y += 8.0f; + return position; + } + } diff --git a/src/dxvk/hud/dxvk_hud_item.h b/src/dxvk/hud/dxvk_hud_item.h index 476b8e18c..0fa0b9649 100644 --- a/src/dxvk/hud/dxvk_hud_item.h +++ b/src/dxvk/hud/dxvk_hud_item.h @@ -160,4 +160,31 @@ namespace dxvk::hud { }; + /** + * \brief HUD item to display the frame rate + */ + class HudFpsItem : public HudItem { + constexpr static int64_t UpdateInterval = 500'000; + public: + + HudFpsItem(); + + ~HudFpsItem(); + + void update(dxvk::high_resolution_clock::time_point time); + + HudPos render( + HudRenderer& renderer, + HudPos position); + + private: + + uint32_t m_frameCount = 0; + dxvk::high_resolution_clock::time_point m_lastUpdate + = dxvk::high_resolution_clock::now(); + + std::string m_frameRate; + + }; + } \ No newline at end of file