From 3514580452e236de474501bfa548a028ae9658e1 Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Fri, 25 Oct 2024 22:58:16 +0200 Subject: [PATCH] [hud] Add item to display frame latency sleep --- src/d3d11/d3d11_swapchain.cpp | 6 +++++- src/dxvk/hud/dxvk_hud_item.cpp | 33 +++++++++++++++++++++++++++++++++ src/dxvk/hud/dxvk_hud_item.h | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/d3d11/d3d11_swapchain.cpp b/src/d3d11/d3d11_swapchain.cpp index 74cec1449..fbece1ccd 100644 --- a/src/d3d11/d3d11_swapchain.cpp +++ b/src/d3d11/d3d11_swapchain.cpp @@ -682,8 +682,12 @@ namespace dxvk { void D3D11SwapChain::CreateHud() { m_hud = hud::Hud::createHud(m_device); - if (m_hud != nullptr) + if (m_hud) { m_hud->addItem("api", 1, GetApiName()); + + if (m_latencyControl) + m_hud->addItem("latency", -1, m_latencyControl); + } } diff --git a/src/dxvk/hud/dxvk_hud_item.cpp b/src/dxvk/hud/dxvk_hud_item.cpp index fb572d7c3..20def905c 100644 --- a/src/dxvk/hud/dxvk_hud_item.cpp +++ b/src/dxvk/hud/dxvk_hud_item.cpp @@ -1578,4 +1578,37 @@ namespace dxvk::hud { / (uint32_t(m_tasksTotal - m_offset)); } + + + + void HudLatencyControlItem::update(dxvk::high_resolution_clock::time_point time) { + uint64_t ticks = std::chrono::duration_cast(time - m_lastUpdate).count(); + m_sleepDuration = std::max(m_sleepDuration, m_latencyControl->getLastSleepDuration()); + + if (ticks >= UpdateInterval) { + uint64_t duration = m_sleepDuration.count() / 100000; + + m_sleepDurationText = str::format(duration / 10u, ".", duration % 10u, " ms"); + m_sleepDuration = std::chrono::nanoseconds(0); + + m_lastUpdate = time; + } + } + + + HudPos HudLatencyControlItem::render( + const DxvkContextObjects& ctx, + const HudPipelineKey& key, + const HudOptions& options, + HudRenderer& renderer, + HudPos position) { + position.y += 16; + renderer.drawText(16, position, 0xffc0ff00u, "Latency sleep:"); + renderer.drawText(16, { position.x + 180, position.y }, 0xffffffffu, m_sleepDurationText); + + position.y += 8; + + return position; + } + } diff --git a/src/dxvk/hud/dxvk_hud_item.h b/src/dxvk/hud/dxvk_hud_item.h index 238991fa8..238b47f14 100644 --- a/src/dxvk/hud/dxvk_hud_item.h +++ b/src/dxvk/hud/dxvk_hud_item.h @@ -1,10 +1,12 @@ #pragma once +#include #include #include #include #include +#include "../../util/util_latency.h" #include "../../util/util_time.h" #include "../dxvk_gpu_query.h" @@ -719,4 +721,34 @@ namespace dxvk::hud { }; + + /** + * \brief Latency control item + */ + class HudLatencyControlItem : public HudItem { + constexpr static int64_t UpdateInterval = 500'000; + public: + + HudLatencyControlItem(Rc latencyControl) + : m_latencyControl(std::move(latencyControl)) { } + + void update(dxvk::high_resolution_clock::time_point time); + + HudPos render( + const DxvkContextObjects& ctx, + const HudPipelineKey& key, + const HudOptions& options, + HudRenderer& renderer, + HudPos position); + + private: + + Rc m_latencyControl; + std::chrono::nanoseconds m_sleepDuration = { }; + std::string m_sleepDurationText; + + dxvk::high_resolution_clock::time_point m_lastUpdate = dxvk::high_resolution_clock::now(); + + }; + } \ No newline at end of file