diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 19:04:24 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 19:04:24 -0500 |
| commit | 8d832fd10913064e1c8aec74191e3ba8f70211c8 (patch) | |
| tree | e365199d7d86ebf592a5a0dc2f07cda4b74b58a7 /runtime/src/dev_overlay.h | |
| parent | 5da3b6be3cefc1499452ba8c8370a50092674d61 (diff) | |
| parent | c50ed94b35b37b697ab9c5fd176f3738480b4db5 (diff) | |
Merge main into rt-title-every-budget (#2122)
#2098 split the every-pass loop of refresh_title_textures by mode: with
LEONIDA_TEXTURE_REFRESH_ASYNC every persistent mapping is polled each
pass and only stale unmapped textures are visited, without it all mapped
or stale ones are. The async branch is kept as main has it.
LEONIDA_TITLE_EVERY_BUDGET_US applies to the synchronous pass, the one
before a submission on the submitting thread; under async the pass runs
on the refresh worker and the limit does not apply (README says so).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/dev_overlay.h')
| -rw-r--r-- | runtime/src/dev_overlay.h | 282 |
1 files changed, 282 insertions, 0 deletions
diff --git a/runtime/src/dev_overlay.h b/runtime/src/dev_overlay.h new file mode 100644 index 000000000..1eecfc366 --- /dev/null +++ b/runtime/src/dev_overlay.h | |||
| @@ -0,0 +1,282 @@ | |||
| 1 | #pragma once | ||
| 2 | // LEONIDA_DEV_OVERLAY=1 (off by default, Windows): a small click-through window over the top left corner of the title's | ||
| 3 | // window that says how the boot is doing: frames and submissions a second, where a submission's CPU time goes and which | ||
| 4 | // phase runs now, pipeline builds, indirect draws, CPU, memory. It is its own window with its own thread and GDI text, | ||
| 5 | // so it keeps counting while the title shows no frames for seconds (pipeline builds, a stalled submission), it is in no | ||
| 6 | // captured frame, and it changes nothing the GPU does. It only reads counters: submit_phases.h, indirect_draw_stats.h, | ||
| 7 | // dev_status.h, and memory_figures.h for the private memory and the device's GPU memory, which LEONIDA_TITLE_HUD | ||
| 8 | // (shims/title_hud.h) shows with the same text. The HUD is the other view: frame times, memory and compile time in the | ||
| 9 | // window's title bar. | ||
| 10 | #include "dev_status.h" | ||
| 11 | #include "indirect_draw_stats.h" | ||
| 12 | #include "memory_figures.h" | ||
| 13 | #include "submit_phases.h" | ||
| 14 | |||
| 15 | #ifdef _WIN32 | ||
| 16 | #ifndef NOMINMAX | ||
| 17 | #define NOMINMAX | ||
| 18 | #endif | ||
| 19 | #include <windows.h> | ||
| 20 | #include <psapi.h> | ||
| 21 | #ifdef _MSC_VER | ||
| 22 | #pragma comment(lib, "user32.lib") | ||
| 23 | #pragma comment(lib, "gdi32.lib") | ||
| 24 | #pragma comment(lib, "psapi.lib") | ||
| 25 | #endif | ||
| 26 | #endif | ||
| 27 | |||
| 28 | #include <algorithm> | ||
| 29 | #include <cstdint> | ||
| 30 | #include <cstdio> | ||
| 31 | #include <cstdlib> | ||
| 32 | #include <string> | ||
| 33 | #include <thread> | ||
| 34 | #include <vector> | ||
| 35 | |||
| 36 | namespace leonida::dev_overlay { | ||
| 37 | |||
| 38 | // The counters between two looks. A total that went down was reset by its own log line: what it holds is the new part. | ||
| 39 | inline std::uint64_t grown(std::uint64_t now, std::uint64_t& before) { | ||
| 40 | const std::uint64_t by = now >= before ? now - before : now; | ||
| 41 | before = now; | ||
| 42 | return by; | ||
| 43 | } | ||
| 44 | |||
| 45 | struct Sample { | ||
| 46 | double seconds = 0; | ||
| 47 | double frames = 0, submissions = 0; // a second | ||
| 48 | double phase_ms[submit_phases::kCount]{}; // a submission | ||
| 49 | int phase = -1; // running now | ||
| 50 | double phase_for = 0; // seconds it has been | ||
| 51 | unsigned building = 0; | ||
| 52 | std::uint64_t built = 0; | ||
| 53 | std::string build; // the last one started | ||
| 54 | unsigned long long drawn = 0, skipped = 0; // indirect draws so far | ||
| 55 | double cores = 0, free_gb = 0; | ||
| 56 | memory_figures::ProcessMemory ram; // private memory and working set | ||
| 57 | VramBudget vram; // the device's VK_EXT_memory_budget; "n/a" without it | ||
| 58 | }; | ||
| 59 | |||
| 60 | // The text of one sample: pure, for the test. | ||
| 61 | inline std::vector<std::string> lines(const Sample& s, double age) { | ||
| 62 | char text[160]; | ||
| 63 | std::vector<std::string> out; | ||
| 64 | std::snprintf(text, sizeof text, "Leonida dev %.0f s %.1f fps %.1f submissions/s", age, s.frames, s.submissions); | ||
| 65 | out.emplace_back(text); | ||
| 66 | double total = 0; | ||
| 67 | for (const double ms : s.phase_ms) total += ms; | ||
| 68 | std::snprintf(text, sizeof text, "CPU/submission %.1f ms: descriptors %.1f, textures %.1f, copies %.1f, replay %.1f", total, | ||
| 69 | s.phase_ms[submit_phases::heaps], s.phase_ms[submit_phases::textures], | ||
| 70 | s.phase_ms[submit_phases::shadows] + s.phase_ms[submit_phases::geometry], | ||
| 71 | s.phase_ms[submit_phases::replay]); | ||
| 72 | out.emplace_back(text); | ||
| 73 | static const char* const now[submit_phases::kCount] = {"waiting for the GPU", "fixing descriptors", "copying title memory", | ||
| 74 | "copying geometry", "refreshing textures", "recording and submitting"}; | ||
| 75 | if (s.phase >= 0 && s.phase < int(submit_phases::kCount)) | ||
| 76 | std::snprintf(text, sizeof text, "now: %s%s", now[s.phase], | ||
| 77 | s.phase_for >= 1 ? (" for " + std::to_string(int(s.phase_for)) + " s").c_str() : ""); | ||
| 78 | else | ||
| 79 | std::snprintf(text, sizeof text, "now: the title's own code"); | ||
| 80 | out.emplace_back(text); | ||
| 81 | if (s.building) | ||
| 82 | std::snprintf(text, sizeof text, "pipelines: %llu built, %u building (%.50s)", | ||
| 83 | static_cast<unsigned long long>(s.built), s.building, s.build.c_str()); | ||
| 84 | else | ||
| 85 | std::snprintf(text, sizeof text, "pipelines: %llu built", static_cast<unsigned long long>(s.built)); | ||
| 86 | out.emplace_back(text); | ||
| 87 | std::snprintf(text, sizeof text, "indirect draws: %llu drawn, %llu skipped", s.drawn, s.skipped); | ||
| 88 | out.emplace_back(text); | ||
| 89 | std::snprintf(text, sizeof text, "%.1f cores, %.1f GB RAM free", s.cores, s.free_gb); | ||
| 90 | out.emplace_back(text); | ||
| 91 | out.emplace_back(memory_figures::ram_text(s.ram) + " " + memory_figures::vram_text(s.vram)); | ||
| 92 | return out; | ||
| 93 | } | ||
| 94 | |||
| 95 | inline bool enabled() { | ||
| 96 | static const bool on = [] { | ||
| 97 | #ifdef _WIN32 | ||
| 98 | char value[3]{}; | ||
| 99 | std::size_t length = 0; | ||
| 100 | return getenv_s(&length, value, sizeof value, "LEONIDA_DEV_OVERLAY") == 0 && length == 2 && value[0] == '1'; | ||
| 101 | #else | ||
| 102 | const char* value = std::getenv("LEONIDA_DEV_OVERLAY"); | ||
| 103 | return value && value[0] == '1' && !value[1]; | ||
| 104 | #endif | ||
| 105 | }(); | ||
| 106 | return on; | ||
| 107 | } | ||
| 108 | |||
| 109 | #ifdef _WIN32 | ||
| 110 | class Overlay { | ||
| 111 | public: | ||
| 112 | void run() { | ||
| 113 | WNDCLASSW type{}; | ||
| 114 | type.lpfnWndProc = &Overlay::procedure; | ||
| 115 | type.hInstance = GetModuleHandleW(nullptr); | ||
| 116 | type.lpszClassName = L"LeonidaDevOverlay"; | ||
| 117 | if (!RegisterClassW(&type)) return; | ||
| 118 | window_ = CreateWindowExW(WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_NOACTIVATE | WS_EX_TOPMOST | WS_EX_TOOLWINDOW, | ||
| 119 | type.lpszClassName, L"", WS_POPUP, 0, 0, 10, 10, nullptr, nullptr, type.hInstance, this); | ||
| 120 | if (!window_) return; | ||
| 121 | SetWindowLongPtrW(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); | ||
| 122 | SetLayeredWindowAttributes(window_, 0, 205, LWA_ALPHA); | ||
| 123 | font_ = CreateFontW(-14, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, | ||
| 124 | CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, FIXED_PITCH | FF_MODERN, L"Consolas"); | ||
| 125 | look(); | ||
| 126 | SetTimer(window_, 1, 500, nullptr); | ||
| 127 | MSG message; | ||
| 128 | while (GetMessageW(&message, nullptr, 0, 0) > 0) DispatchMessageW(&message); | ||
| 129 | } | ||
| 130 | |||
| 131 | private: | ||
| 132 | static LRESULT CALLBACK procedure(HWND window, UINT message, WPARAM w, LPARAM l) { | ||
| 133 | auto* self = reinterpret_cast<Overlay*>(GetWindowLongPtrW(window, GWLP_USERDATA)); | ||
| 134 | if (self && message == WM_TIMER) { | ||
| 135 | self->look(); | ||
| 136 | return 0; | ||
| 137 | } | ||
| 138 | if (self && message == WM_PAINT) { | ||
| 139 | self->paint(); | ||
| 140 | return 0; | ||
| 141 | } | ||
| 142 | if (message == WM_NCHITTEST) return HTTRANSPARENT; | ||
| 143 | return DefWindowProcW(window, message, w, l); | ||
| 144 | } | ||
| 145 | |||
| 146 | // The process's largest visible top-level window is the title's. | ||
| 147 | struct Search { | ||
| 148 | HWND own, found; | ||
| 149 | std::int64_t area; | ||
| 150 | }; | ||
| 151 | static BOOL CALLBACK consider(HWND window, LPARAM parameter) { | ||
| 152 | auto& search = *reinterpret_cast<Search*>(parameter); | ||
| 153 | DWORD process = 0; | ||
| 154 | GetWindowThreadProcessId(window, &process); | ||
| 155 | RECT r{}; | ||
| 156 | if (process != GetCurrentProcessId() || window == search.own || !IsWindowVisible(window) || IsIconic(window) || | ||
| 157 | !GetClientRect(window, &r)) | ||
| 158 | return TRUE; | ||
| 159 | const std::int64_t area = std::int64_t(r.right) * r.bottom; | ||
| 160 | if (area > search.area) search = {search.own, window, area}; | ||
| 161 | return TRUE; | ||
| 162 | } | ||
| 163 | |||
| 164 | void look() { | ||
| 165 | namespace sp = submit_phases; | ||
| 166 | sp::Totals& totals = sp::totals(); | ||
| 167 | const std::int64_t now = sp::now_ns(); | ||
| 168 | Sample s; | ||
| 169 | s.seconds = before_ ? double(now - before_) * 1e-9 : 0; | ||
| 170 | const std::uint64_t frames = grown(totals.present_serial.load(std::memory_order_relaxed), frames_); | ||
| 171 | const std::uint64_t submissions = grown(totals.all_submissions.load(std::memory_order_relaxed), submissions_); | ||
| 172 | std::uint64_t ns[sp::kCount]; | ||
| 173 | for (unsigned p = 0; p < sp::kCount; ++p) ns[p] = grown(totals.ns[p].load(std::memory_order_relaxed), ns_[p]); | ||
| 174 | // A stall longer than a look leaves nothing to divide by: the figures then cover the last two seconds. | ||
| 175 | window_frames_ += frames; | ||
| 176 | window_submissions_ += submissions; | ||
| 177 | window_seconds_ += s.seconds; | ||
| 178 | for (unsigned p = 0; p < sp::kCount; ++p) window_ns_[p] += ns[p]; | ||
| 179 | if (window_seconds_ >= 2 || !before_) { | ||
| 180 | shown_ = {}; | ||
| 181 | if (window_seconds_ > 0) { | ||
| 182 | shown_.frames = double(window_frames_) / window_seconds_; | ||
| 183 | shown_.submissions = double(window_submissions_) / window_seconds_; | ||
| 184 | } | ||
| 185 | for (unsigned p = 0; p < sp::kCount; ++p) { | ||
| 186 | if (window_submissions_) shown_.phase_ms[p] = double(window_ns_[p]) * 1e-6 / double(window_submissions_); | ||
| 187 | window_ns_[p] = 0; | ||
| 188 | } | ||
| 189 | window_frames_ = window_submissions_ = 0; | ||
| 190 | window_seconds_ = 0; | ||
| 191 | } | ||
| 192 | s.frames = shown_.frames; | ||
| 193 | s.submissions = shown_.submissions; | ||
| 194 | std::copy(std::begin(shown_.phase_ms), std::end(shown_.phase_ms), std::begin(s.phase_ms)); | ||
| 195 | before_ = now; | ||
| 196 | |||
| 197 | const sp::Running running = sp::running(); | ||
| 198 | s.phase = running.phase; | ||
| 199 | if (s.phase >= 0) s.phase_for = std::max(0.0, double(now - running.since) * 1e-9); | ||
| 200 | dev_status::Builds& builds = dev_status::builds(); | ||
| 201 | s.building = builds.active.load(std::memory_order_relaxed); | ||
| 202 | s.built = builds.done.load(std::memory_order_relaxed); | ||
| 203 | s.build = builds.last(); | ||
| 204 | const auto& draws = indirect_stats::counts().values; | ||
| 205 | s.drawn = draws[std::size_t(indirect_stats::Event::emitted)].load(std::memory_order_relaxed); | ||
| 206 | s.skipped = draws[std::size_t(indirect_stats::Event::record_skip)].load(std::memory_order_relaxed) + | ||
| 207 | draws[std::size_t(indirect_stats::Event::replay_skip)].load(std::memory_order_relaxed); | ||
| 208 | FILETIME created{}, exited{}, kernel{}, user{}; | ||
| 209 | if (GetProcessTimes(GetCurrentProcess(), &created, &exited, &kernel, &user)) { | ||
| 210 | const auto ticks = [](const FILETIME& f) { return (std::uint64_t(f.dwHighDateTime) << 32) | f.dwLowDateTime; }; | ||
| 211 | const std::uint64_t used = grown(ticks(kernel) + ticks(user), cpu_); | ||
| 212 | if (s.seconds > 0) s.cores = double(used) * 1e-7 / s.seconds; | ||
| 213 | } | ||
| 214 | s.ram = memory_figures::process_memory(); | ||
| 215 | s.vram = memory_figures::latest_vram(); // reads the device at most once per 500 ms | ||
| 216 | MEMORYSTATUSEX status{sizeof status}; | ||
| 217 | if (GlobalMemoryStatusEx(&status)) s.free_gb = double(status.ullAvailPhys) / double(1u << 30); | ||
| 218 | lines_ = lines(s, sp::process_age()); | ||
| 219 | place(); | ||
| 220 | } | ||
| 221 | |||
| 222 | void place() { | ||
| 223 | Search search{window_, nullptr, 0}; | ||
| 224 | EnumWindows(&Overlay::consider, reinterpret_cast<LPARAM>(&search)); | ||
| 225 | // Shown only over the title while it is the window in front, so the topmost text is never over anything else. | ||
| 226 | if (!search.found || GetForegroundWindow() != search.found) { | ||
| 227 | ShowWindow(window_, SW_HIDE); | ||
| 228 | return; | ||
| 229 | } | ||
| 230 | POINT corner{8, 8}; | ||
| 231 | ClientToScreen(search.found, &corner); | ||
| 232 | std::size_t longest = 0; | ||
| 233 | for (const auto& line : lines_) longest = std::max(longest, line.size()); | ||
| 234 | SetWindowPos(window_, HWND_TOPMOST, corner.x, corner.y, int(longest) * 8 + 12, int(lines_.size()) * 17 + 8, | ||
| 235 | SWP_NOACTIVATE | SWP_SHOWWINDOW); | ||
| 236 | InvalidateRect(window_, nullptr, FALSE); | ||
| 237 | } | ||
| 238 | |||
| 239 | void paint() { | ||
| 240 | PAINTSTRUCT ps; | ||
| 241 | HDC dc = BeginPaint(window_, &ps); | ||
| 242 | RECT area; | ||
| 243 | GetClientRect(window_, &area); | ||
| 244 | HDC memory = CreateCompatibleDC(dc); | ||
| 245 | HBITMAP bitmap = CreateCompatibleBitmap(dc, area.right, area.bottom); | ||
| 246 | HGDIOBJ old_bitmap = SelectObject(memory, bitmap), old_font = SelectObject(memory, font_); | ||
| 247 | FillRect(memory, &area, static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH))); | ||
| 248 | SetBkMode(memory, TRANSPARENT); | ||
| 249 | int y = 4; | ||
| 250 | for (std::size_t i = 0; i < lines_.size(); ++i, y += 17) { | ||
| 251 | SetTextColor(memory, i == 0 ? RGB(120, 255, 140) : RGB(235, 235, 235)); | ||
| 252 | TextOutA(memory, 6, y, lines_[i].data(), int(lines_[i].size())); | ||
| 253 | } | ||
| 254 | BitBlt(dc, 0, 0, area.right, area.bottom, memory, 0, 0, SRCCOPY); | ||
| 255 | SelectObject(memory, old_bitmap); | ||
| 256 | SelectObject(memory, old_font); | ||
| 257 | DeleteObject(bitmap); | ||
| 258 | DeleteDC(memory); | ||
| 259 | EndPaint(window_, &ps); | ||
| 260 | } | ||
| 261 | |||
| 262 | HWND window_ = nullptr; | ||
| 263 | HFONT font_ = nullptr; | ||
| 264 | std::vector<std::string> lines_; | ||
| 265 | std::int64_t before_ = 0; | ||
| 266 | std::uint64_t frames_ = 0, submissions_ = 0, cpu_ = 0, ns_[submit_phases::kCount]{}; | ||
| 267 | std::uint64_t window_frames_ = 0, window_submissions_ = 0, window_ns_[submit_phases::kCount]{}; | ||
| 268 | double window_seconds_ = 0; | ||
| 269 | Sample shown_; | ||
| 270 | }; | ||
| 271 | #endif | ||
| 272 | |||
| 273 | // Once per process; nothing without LEONIDA_DEV_OVERLAY=1. | ||
| 274 | inline void start() { | ||
| 275 | #ifdef _WIN32 | ||
| 276 | if (!enabled()) return; | ||
| 277 | std::fprintf(stderr, "[rt] dev overlay on (LEONIDA_DEV_OVERLAY=1)\n"); | ||
| 278 | std::thread([] { static Overlay overlay; overlay.run(); }).detach(); | ||
| 279 | #endif | ||
| 280 | } | ||
| 281 | |||
| 282 | } // namespace leonida::dev_overlay | ||