1
0
mirror of https://github.com/doitsujin/dxvk.git synced 2025-01-19 05:52:11 +01:00

[hud] Implement FPS display as a HUD item

This commit is contained in:
Philip Rebohle 2019-12-12 21:24:39 +01:00
parent 0da5aac357
commit 2d5f44a7ff
No known key found for this signature in database
GPG Key ID: C8CC613427A31C99
3 changed files with 62 additions and 0 deletions

View File

@ -37,6 +37,7 @@ namespace dxvk::hud {
addItem<HudVersionItem>("version");
addItem<HudClientApiItem>("api", m_device);
addItem<HudDeviceInfoItem>("devinfo", m_device);
addItem<HudFpsItem>("fps");
}

View File

@ -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<std::chrono::microseconds>(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;
}
}

View File

@ -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;
};
}