upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

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