From c757a427987afc937c6529c41fbfdebe20327d08 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Mon, 26 Oct 2026 10:50:45 +0300 Subject: [PATCH] Add the frame rate overlay: TOTK_PERF_OVERLAY fps shows the frame rate in the top right corner, full adds the mean and longest frame time of the last half second, F10 cycles the modes and saves the choice Co-Authored-By: Claude Opus 5.5 --- docs/backlog.md | 4 +- runtime/ui/menu.cpp | 86 +++++++++++++++++++++++++++++++++++------ runtime/ui/menu.h | 1 + runtime/ui/menu_win.cpp | 5 +++ runtime/ui_text.c | 9 ++++- runtime/ui_text.h | 1 + totk.ini.example | 3 ++ 7 files changed, 94 insertions(+), 15 deletions(-) diff --git a/docs/backlog.md b/docs/backlog.md index daff15a0..2eab7fa9 100644 --- a/docs/backlog.md +++ b/docs/backlog.md @@ -157,8 +157,8 @@ Each platform item gets a written design first, agreed with the maintainer. says "after restart". Dear ImGui is drawn after the blit into the swapchain image (`runtime/ui/menu.cpp`). Left to do, in this order (unfinished work on branch `task/menu-advanced`): - 1. Performance overlay (`TOTK_PERF_OVERLAY`, off, fps or full, F10 cycles; SavageWindRecomp `runtime/perfmon.c`): - frame rate, frame time, CPU and GPU load through PDH and NVML. Its menu row is greyed out until then. + 1. Performance overlay: `TOTK_PERF_OVERLAY` (F10 cycles) shows the frame rate, and in `full` the mean and longest + frame time. Left: CPU and GPU load through PDH and NVML (SavageWindRecomp `runtime/perfmon.c`). 2. Advanced tab: the remaining `cfg` keys (scheduling, replay thread, pipeline and shader switches), reset of all settings, and a system information block (`sysinfo.c`) with copy to clipboard and open the log folder. 3. Game language list without Portuguese (TotK ships ja, en, fr, de, it, es, nl, ru, zh-CN, zh-TW, ko). diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 098d934d..4ca251d5 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -444,9 +444,7 @@ static void tab_screen(float scale) { opt_choice("TOTK_VK_PRESENT", UI_MENU_PRESENT, present, 3, UI_MENU_PRESENT_DESC); row_gpu(); static const UiTextId perf[] = {UI_MENU_PERF_OFF, UI_MENU_PERF_FPS, UI_MENU_PERF_FULL}; - ImGui::BeginDisabled(); // no overlay draws yet (docs/backlog.md, in-game menu) opt_choice("TOTK_PERF_OVERLAY", UI_MENU_PERF_OVERLAY, perf, 3, UI_MENU_PERF_OVERLAY_DESC); - ImGui::EndDisabled(); opt_end(); } @@ -556,17 +554,81 @@ static void layout_menu(uint32_t w, uint32_t h, float scale) { static int g_save_pending; // a changed setting still waits for its write to totk.ini (opt_save_tick) +// ---- the performance overlay (TOTK_PERF_OVERLAY): the frame rate over the presents of the last half second, in "full" +// also their mean and longest interval, in the top right corner. Off, it costs a cfg_version load per present. +static unsigned g_perf_ver = ~0u; +static int g_perf_mode; // 0 off, 1 fps, 2 full +static std::atomic g_perf_save; // F10 changed the mode: totk.ini is written on the present thread +static uint64_t g_perf_last_ns, g_perf_win_ns, g_perf_max_ns; +static int g_perf_frames; +static char g_perf_text[96]; + +static int perf_mode_of(const char *s) { return !strcmp(s, "full") ? 2 : !strcmp(s, "fps") ? 1 : 0; } + +extern "C" void menu_perf_cycle(void) { + static const char *const modes[] = {"off", "fps", "full"}; + cfg_set("TOTK_PERF_OVERLAY", modes[(perf_mode_of(cfg_value("TOTK_PERF_OVERLAY")) + 1) % 3]); + g_perf_save.store(1); +} + +static void perf_tick(uint64_t now) { + if (cfg_version() != g_perf_ver) { + g_perf_ver = cfg_version(); + int m = perf_mode_of(cfg_value("TOTK_PERF_OVERLAY")); + if (m != g_perf_mode) g_perf_last_ns = 0, g_perf_text[0] = 0; + g_perf_mode = m; + } + if (!g_perf_mode) return; + uint64_t d = now - g_perf_last_ns; + if (!g_perf_last_ns || d > 2000000000ull) { // first frame, or a stall the window should not average in + g_perf_win_ns = g_perf_max_ns = 0; + g_perf_frames = 0; + } else { + g_perf_win_ns += d; + g_perf_frames++; + if (d > g_perf_max_ns) g_perf_max_ns = d; + if (g_perf_win_ns >= 500000000ull) { + double fps = g_perf_frames * 1e9 / g_perf_win_ns; + if (g_perf_mode == 2) + snprintf(g_perf_text, sizeof g_perf_text, menu_text(UI_PERF_LINE_FULL), fps, + g_perf_win_ns / 1e6 / g_perf_frames, g_perf_max_ns / 1e6); + else + snprintf(g_perf_text, sizeof g_perf_text, menu_text(UI_PERF_LINE_FPS), fps); + g_perf_win_ns = g_perf_max_ns = 0; + g_perf_frames = 0; + } + } + g_perf_last_ns = now; +} + +static void perf_draw(float w, float scale) { + if (!g_perf_mode || !g_perf_text[0]) return; + ImDrawList *dl = ImGui::GetForegroundDrawList(); + ImVec2 sz = ImGui::CalcTextSize(g_perf_text); + float pad = 6 * scale; + ImVec2 p(w - 12 * scale - sz.x - pad * 2, 12 * scale); // the top right corner: the game's HUD leaves it empty + dl->AddRectFilled(p, ImVec2(p.x + sz.x + pad * 2, p.y + sz.y + pad * 2), IM_COL32(0, 0, 0, 150), 4 * scale); + dl->AddText(ImVec2(p.x + pad, p.y + pad), IM_COL32(255, 255, 255, 235), g_perf_text); +} + extern "C" int menu_overlay_frame(uint64_t frame, const MenuTarget *t, const char **shot) { *shot = NULL; if (!g_test.parsed) test_parse(); if (g_test.next_set < g_test.n_sets && frame >= g_test.sets[g_test.next_set].frame) test_apply_sets(frame); - if (!g_open.load(std::memory_order_relaxed)) { + if (g_perf_save.exchange(0)) cfg_save(); + perf_tick(plat_now_ns()); + bool open = g_open.load(std::memory_order_relaxed); + if (!open) { if (g_save_pending) g_save_pending = opt_save_tick(1); // closed: the last change goes to totk.ini now - if (!g_test.at || g_test.opened || frame < g_test.at) return 0; - g_test.opened = frame; - g_test.page = 0; - g_tab_request = 0; - set_open(1); + if (g_test.at && !g_test.opened && frame >= g_test.at) { + g_test.opened = frame; + g_test.page = 0; + g_tab_request = 0; + set_open(1); + open = true; + } else if (!g_perf_mode) { + return 0; + } } if (g_failed_flag()) { set_open(0); @@ -599,7 +661,8 @@ extern "C" int menu_overlay_frame(uint64_t frame, const MenuTarget *t, const cha double dt = g_last_ns ? (t0 - g_last_ns) / 1e9 : 1 / 60.0; io.DeltaTime = (float)(dt < 1e-4 ? 1e-4 : dt > 0.1 ? 0.1 : dt); g_last_ns = t0; - if (g_test.page < 0) { + if (!open) { // the overlay alone: no input reaches ImGui + } else if (g_test.page < 0) { drain_events(io); pad_events(io, reset); } else { // the test hook's pages: the user's input in a background test window must not change settings @@ -609,9 +672,10 @@ extern "C" int menu_overlay_frame(uint64_t frame, const MenuTarget *t, const cha } ImGui_ImplVulkan_NewFrame(); ImGui::NewFrame(); - layout_menu(t->width, t->height, scale); + if (open) layout_menu(t->width, t->height, scale); + perf_draw((float)t->width, scale); ImGui::Render(); - g_save_pending = opt_save_tick(0); + if (open) g_save_pending = opt_save_tick(0); g_cost_ns += plat_now_ns() - t0; if (g_test.page >= 0 && frame >= g_test.opened + g_test.per_page * (uint64_t)(g_test.page + 1)) { static char path[512]; // this frame's image goes to a file; then the next tab, or the menu closes diff --git a/runtime/ui/menu.h b/runtime/ui/menu.h index 45eaea8a..05ce9bf5 100644 --- a/runtime/ui/menu.h +++ b/runtime/ui/menu.h @@ -24,6 +24,7 @@ extern "C" { // ---- state, any thread int menu_is_open(void); void menu_toggle(void); // F1 / Esc on the keyboard, Minus+Plus on the pad +void menu_perf_cycle(void); // F10: the performance overlay off, fps, full // ---- events from the window adapter, used only while the menu is open. key: an ImGuiKey; coordinates in pixels of the // window's client area, which is the swapchain image. diff --git a/runtime/ui/menu_win.cpp b/runtime/ui/menu_win.cpp index 3abbdeb6..632c17e3 100644 --- a/runtime/ui/menu_win.cpp +++ b/runtime/ui/menu_win.cpp @@ -50,6 +50,11 @@ extern "C" int menu_window_msg(HWND h, UINT m, WPARAM w, LPARAM l, LRESULT *res) *res = 0; return 1; } + if (w == VK_F10) { // also keeps F10 from opening the window menu + if (!(l & (1 << 30))) menu_perf_cycle(); + *res = 0; + return 1; + } [[fallthrough]]; case WM_KEYUP: case WM_SYSKEYUP: diff --git a/runtime/ui_text.c b/runtime/ui_text.c index 90730863..051ad2a8 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -42,8 +42,11 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_GPU] = "Graphics card", [UI_MENU_GPU_DESC] = "The Vulkan device that draws the game.", [UI_MENU_GPU_AUTO] = "Automatic", [UI_MENU_PERF_OVERLAY] = "Performance overlay", - [UI_MENU_PERF_OVERLAY_DESC] = "Frame rate and system numbers over the picture; F10 cycles the modes.", + [UI_MENU_PERF_OVERLAY_DESC] = "Frame rate in the top right corner; Full adds the mean and the longest frame time of the " + "last half second. F10 cycles the modes.", [UI_MENU_PERF_OFF] = "Off", [UI_MENU_PERF_FPS] = "FPS", [UI_MENU_PERF_FULL] = "Full", + // printf formats of the overlay line: frame rate; frame rate, mean and longest frame time in ms + [UI_PERF_LINE_FPS] = "%.0f fps", [UI_PERF_LINE_FULL] = "%.0f fps %.1f ms max %.1f ms", [UI_MENU_FPS60] = "Frame rate", [UI_MENU_FPS60_DESC] = "Runs the game at 60 frames per second instead of 30.", [UI_MENU_FPS_60] = "60 FPS", [UI_MENU_FPS_30] = "30 FPS", @@ -176,8 +179,10 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_GPU] = "Видеокарта", [UI_MENU_GPU_DESC] = "Устройство Vulkan, которое рисует игру.", [UI_MENU_GPU_AUTO] = "Автоматически", [UI_MENU_PERF_OVERLAY] = "Индикатор производительности", - [UI_MENU_PERF_OVERLAY_DESC] = "Частота кадров и данные о системе поверх картинки; F10 переключает режимы.", + [UI_MENU_PERF_OVERLAY_DESC] = "Частота кадров в правом верхнем углу; «Полный» добавляет среднее и самое долгое время " + "кадра за последние полсекунды. F10 переключает режимы.", [UI_MENU_PERF_OFF] = "Выкл.", [UI_MENU_PERF_FPS] = "FPS", [UI_MENU_PERF_FULL] = "Полный", + [UI_PERF_LINE_FPS] = "%.0f к/с", [UI_PERF_LINE_FULL] = "%.0f к/с %.1f мс макс. %.1f мс", [UI_MENU_FPS60] = "Частота кадров", [UI_MENU_FPS60_DESC] = "Игра идёт в 60 кадров в секунду вместо 30.", [UI_MENU_FPS_60] = "60 FPS", [UI_MENU_FPS_30] = "30 FPS", diff --git a/runtime/ui_text.h b/runtime/ui_text.h index eb9e2f9a..e8426cd0 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -28,6 +28,7 @@ typedef enum { UI_MENU_PRESENT, UI_MENU_PRESENT_DESC, UI_MENU_PRESENT_MAILBOX, UI_MENU_PRESENT_VSYNC, UI_MENU_PRESENT_IMMEDIATE, UI_MENU_GPU, UI_MENU_GPU_DESC, UI_MENU_GPU_AUTO, UI_MENU_PERF_OVERLAY, UI_MENU_PERF_OVERLAY_DESC, UI_MENU_PERF_OFF, UI_MENU_PERF_FPS, UI_MENU_PERF_FULL, + UI_PERF_LINE_FPS, UI_PERF_LINE_FULL, // Game UI_MENU_FPS60, UI_MENU_FPS60_DESC, UI_MENU_FPS_60, UI_MENU_FPS_30, diff --git a/totk.ini.example b/totk.ini.example index d37a5ec3..5112c539 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -25,6 +25,9 @@ # here. Changes while the game runs take effect at once. # window_size = 1920x1080 # fullscreen = 0 +# Performance overlay in the top right corner: off, fps, or full (frame rate, mean and longest frame time). F10 +# cycles the modes in game. +# perf_overlay = off # Which GPU to use: its index in the log, or part of its name. Default: the first discrete GPU with Vulkan 1.3. # gpu_index = 0 # gpu_name = NVIDIA