upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorkawaii-shakal <deusex1998@gmail.com>2026-11-25 20:51:50 +0500
committerkawaii-shakal <deusex1998@gmail.com>2026-11-25 20:51:50 +0500
commite86b3ceaa8d9359df5baaee7e5246d219fb88dbb (patch)
tree766912e2d62b5fa5a24b677421ec60484d8f0f52 /runtime
parent71a769d7d85208f86c6e2e50263aca7df1becce9 (diff)
d3d12_xs: LEONIDA_TITLE_HUD shows frame rate, memory and pipeline compiles in the window's title bar (RT-TITLE-HUD)
A tester has no way to see, during a boot, whether the game is slow because of frame time, memory pressure or pipelines compiled in the frame. With LEONIDA_TITLE_HUD=1 the window the title presents in gets, twice a second: - fps and the average and worst frame time from PresentX; - private memory and working set; - local GPU memory against the DXGI budget, and shared GPU memory; - pipelines created and the seconds spent in vkCreate*Pipelines in the last interval (with LEONIDA_MEM_PROBE); - time since the first present. A thread of its own sets the text with SendMessageTimeout, so a busy or hung window thread never holds a present. title_hud_test drives a real window from another thread and checks the text and the shutdown. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime')
-rw-r--r--runtime/CMakeLists.txt10
-rw-r--r--runtime/shims/d3d12_xs_device.cpp12
-rw-r--r--runtime/shims/title_hud.h164
-rw-r--r--runtime/test/title_hud_test.cpp71
4 files changed, 253 insertions, 4 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt
index 86378dfc8..d3f42089b 100644
--- a/runtime/CMakeLists.txt
+++ b/runtime/CMakeLists.txt
@@ -765,6 +765,16 @@ if(BUILD_TESTING)
765 endif() 765 endif()
766 target_link_libraries(title_window_test PRIVATE user32) 766 target_link_libraries(title_window_test PRIVATE user32)
767 add_test(NAME title_window COMMAND title_window_test) 767 add_test(NAME title_window COMMAND title_window_test)
768
769 # LEONIDA_TITLE_HUD's counters in the title bar of another thread's window (shims/title_hud.h): user32 and DXGI.
770 add_executable(title_hud_test test/title_hud_test.cpp)
771 target_include_directories(title_hud_test PRIVATE shims)
772 target_compile_definitions(title_hud_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX UNICODE _UNICODE)
773 if(MSVC)
774 target_compile_options(title_hud_test PRIVATE /W4 /permissive- /utf-8)
775 endif()
776 target_link_libraries(title_hud_test PRIVATE Vulkan::Headers user32 psapi)
777 add_test(NAME title_hud COMMAND title_hud_test)
768 endif() 778 endif()
769 779
770 add_executable(pipeline_cache_file_test test/pipeline_cache_file_test.cpp) 780 add_executable(pipeline_cache_file_test test/pipeline_cache_file_test.cpp)
diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp
index 90dcc6113..61e795d44 100644
--- a/runtime/shims/d3d12_xs_device.cpp
+++ b/runtime/shims/d3d12_xs_device.cpp
@@ -49,6 +49,7 @@
49// WindowPresenter. Other builds of this file (mock and COM tests) keep the refused-slot behaviour. 49// WindowPresenter. Other builds of this file (mock and COM tests) keep the refused-slot behaviour.
50#include "xbox_present.h" 50#include "xbox_present.h"
51#include "title_window.h" 51#include "title_window.h"
52#include "title_hud.h"
52#include "package_splash.h" 53#include "package_splash.h"
53#include "startup_trace.h" 54#include "startup_trace.h"
54#endif 55#endif
@@ -1569,6 +1570,7 @@ struct PresentState {
1569 } 1570 }
1570 HWND window = nullptr; // the runtime's own fallback window, when the title has no window to present in 1571 HWND window = nullptr; // the runtime's own fallback window, when the title has no window to present in
1571 std::unique_ptr<rt::d3d12::XboxPresentation> xbox; 1572 std::unique_ptr<rt::d3d12::XboxPresentation> xbox;
1573 std::unique_ptr<TitleHud> hud; // LEONIDA_TITLE_HUD: counters in the window's title bar (title_hud.h)
1572}; 1574};
1573 1575
1574void destroy_present_state(void* present_state) { 1576void destroy_present_state(void* present_state) {
@@ -1739,6 +1741,7 @@ PresentState* present_state_for(DeviceProxy* device, bool splash_only = false) {
1739 std::fprintf(stderr, "[rt] d3d12_xs: frame interval %u us replayed (0x%08lx), origin event (0x%08lx)\n", 1741 std::fprintf(stderr, "[rt] d3d12_xs: frame interval %u us replayed (0x%08lx), origin event (0x%08lx)\n",
1740 interval, static_cast<unsigned long>(set), static_cast<unsigned long>(armed)); 1742 interval, static_cast<unsigned long>(set), static_cast<unsigned long>(armed));
1741 } 1743 }
1744 if (TitleHud::enabled()) state->hud = std::make_unique<TitleHud>(target);
1742 if (splash_only) device->present_state = pending.release(); 1745 if (splash_only) device->present_state = pending.release();
1743 std::fprintf(stderr, "[rt] d3d12_xs: PresentX ready (window %p: %s, client area %ldx%ld)\n", 1746 std::fprintf(stderr, "[rt] d3d12_xs: PresentX ready (window %p: %s, client area %ldx%ld)\n",
1744 static_cast<void*>(target), 1747 static_cast<void*>(target),
@@ -1758,10 +1761,11 @@ std::uint64_t queue_present_x(ChildProxy* self, std::uint64_t plane_count, std::
1758 std::uint64_t params) { 1761 std::uint64_t params) {
1759 PresentState* state = self->device ? present_state_for(self->device) : nullptr; 1762 PresentState* state = self->device ? present_state_for(self->device) : nullptr;
1760 if (!state || !state->xbox) return hr_to_u64(S_OK); // a title still runs with nothing presented 1763 if (!state || !state->xbox) return hr_to_u64(S_OK); // a title still runs with nothing presented
1761 return hr_to_u64(state->xbox->PresentX( 1764 const HRESULT result = state->xbox->PresentX(
1762 static_cast<UINT>(plane_count), 1765 static_cast<UINT>(plane_count), reinterpret_cast<const rt::d3d12::XboxPresentPlaneParameters*>(planes),
1763 reinterpret_cast<const rt::d3d12::XboxPresentPlaneParameters*>(planes), 1766 reinterpret_cast<const rt::d3d12::XboxPresentParameters*>(params));
1764 reinterpret_cast<const rt::d3d12::XboxPresentParameters*>(params))); 1767 if (SUCCEEDED(result) && state->hud) state->hud->note_present();
1768 return hr_to_u64(result);
1765} 1769}
1766 1770
1767#else // !LEONIDA_VULKAN_DEVICE: this translation unit stands alone (mock and COM tests) 1771#else // !LEONIDA_VULKAN_DEVICE: this translation unit stands alone (mock and COM tests)
diff --git a/runtime/shims/title_hud.h b/runtime/shims/title_hud.h
new file mode 100644
index 000000000..bdaf44db3
--- /dev/null
+++ b/runtime/shims/title_hud.h
@@ -0,0 +1,164 @@
1#pragma once
2// LEONIDA_TITLE_HUD=1 shows live counters in the title bar of the window the title presents in:
3// frame rate and frame time from PresentX (average and worst frame of the last half second);
4// the process's private memory and its GPU memory (DXGI QueryVideoMemoryInfo, local and non-local segments);
5// graphics and compute pipelines created, and the seconds spent in vkCreate*Pipelines in the last interval,
6// which is where an inline compile shows up as a hitch (needs LEONIDA_MEM_PROBE, src/mem_probe.h; else "-");
7// time since the first present.
8// The text is set from a thread of its own with SendMessageTimeout, so a busy or hung window thread never holds
9// the present path.
10
11#include <windows.h>
12#include <dxgi1_4.h>
13#include <psapi.h>
14#include <wrl/client.h>
15
16#include <algorithm>
17#include <atomic>
18#include <cstdint>
19#include <cstdio>
20#include <cstdlib>
21#include <cwchar>
22#include <thread>
23
24#include "../src/mem_probe.h"
25
26namespace rt::d3d12_xs {
27
28class TitleHud {
29public:
30 static bool enabled() {
31 static const bool on = [] {
32 char v[2] = {};
33 return GetEnvironmentVariableA("LEONIDA_TITLE_HUD", v, sizeof v) == 1 && v[0] != '0';
34 }();
35 return on;
36 }
37
38 explicit TitleHud(HWND window) : window_(window) {
39 QueryPerformanceFrequency(&frequency_);
40 stop_ = CreateEventW(nullptr, TRUE, FALSE, nullptr);
41 if (stop_) thread_ = std::thread([this] { run(); });
42 }
43 TitleHud(const TitleHud&) = delete;
44 TitleHud& operator=(const TitleHud&) = delete;
45 ~TitleHud() {
46 if (stop_) SetEvent(stop_);
47 if (thread_.joinable()) thread_.join();
48 if (stop_) CloseHandle(stop_);
49 }
50
51 // On every successful PresentX.
52 void note_present() {
53 LARGE_INTEGER now;
54 QueryPerformanceCounter(&now);
55 const std::int64_t previous = last_present_.exchange(now.QuadPart, std::memory_order_relaxed);
56 if (previous) {
57 const std::int64_t gap = now.QuadPart - previous;
58 std::int64_t worst = worst_gap_.load(std::memory_order_relaxed);
59 while (gap > worst && !worst_gap_.compare_exchange_weak(worst, gap, std::memory_order_relaxed)) {}
60 } else {
61 first_present_.store(now.QuadPart, std::memory_order_relaxed);
62 }
63 presents_.fetch_add(1, std::memory_order_relaxed);
64 }
65
66private:
67 static constexpr DWORD interval_ms = 500;
68
69 void run() {
70 Microsoft::WRL::ComPtr<IDXGIAdapter3> adapter = gpu_adapter();
71 std::uint64_t last_presents = 0, last_compile_ns = 0;
72 LARGE_INTEGER last;
73 QueryPerformanceCounter(&last);
74 while (WaitForSingleObject(stop_, interval_ms) == WAIT_TIMEOUT) {
75 if (!IsWindow(window_)) return;
76 LARGE_INTEGER now;
77 QueryPerformanceCounter(&now);
78 const double seconds = double(now.QuadPart - last.QuadPart) / double(frequency_.QuadPart);
79 last = now;
80 const std::uint64_t presents = presents_.load(std::memory_order_relaxed);
81 const std::uint64_t frames = presents - last_presents;
82 last_presents = presents;
83 const double worst_ms =
84 double(worst_gap_.exchange(0, std::memory_order_relaxed)) * 1000.0 / double(frequency_.QuadPart);
85 const double fps = seconds > 0 ? double(frames) / seconds : 0.0;
86 const double frame_ms = frames ? seconds * 1000.0 / double(frames) : 0.0;
87
88 wchar_t memory[96] = L"RAM -";
89 PROCESS_MEMORY_COUNTERS_EX counters{};
90 counters.cb = sizeof counters;
91 if (GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&counters),
92 sizeof counters))
93 std::swprintf(memory, 96, L"RAM %.1f GB (ws %.1f)", gb(counters.PrivateUsage),
94 gb(counters.WorkingSetSize));
95
96 wchar_t video[96] = L"VRAM -";
97 DXGI_QUERY_VIDEO_MEMORY_INFO local{}, shared{};
98 if (adapter && SUCCEEDED(adapter->QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_LOCAL, &local)) &&
99 SUCCEEDED(adapter->QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL, &shared)))
100 std::swprintf(video, 96, L"VRAM %.1f/%.1f GB +%.1f shared", gb(local.CurrentUsage),
101 gb(local.Budget), gb(shared.CurrentUsage));
102
103 wchar_t pipelines[128] = L"PSO -";
104 auto& probe = leonida::mem_probe::state();
105 if (probe.installed) {
106 std::uint64_t graphics, compute, ns;
107 {
108 std::lock_guard lock(probe.mutex);
109 graphics = probe.graphics_made;
110 compute = probe.compute_made;
111 ns = probe.graphics_ns + probe.compute_ns;
112 }
113 const double compiling = double(ns - last_compile_ns) / 1e9;
114 last_compile_ns = ns;
115 std::swprintf(pipelines, 128, L"PSO %llug %lluc, compiling %.1f s", (unsigned long long)graphics,
116 (unsigned long long)compute, compiling);
117 }
118
119 const std::int64_t first = first_present_.load(std::memory_order_relaxed);
120 const unsigned up = first ? unsigned((now.QuadPart - first) / frequency_.QuadPart) : 0;
121
122 wchar_t text[512];
123 std::swprintf(text, 512, L"Leonida | %.0f fps %.1f ms (worst %.0f) | %ls | %ls | %ls | %u:%02u", fps,
124 frame_ms, worst_ms, memory, video, pipelines, up / 60, up % 60);
125 DWORD_PTR ignored = 0;
126 SendMessageTimeoutW(window_, WM_SETTEXT, 0, reinterpret_cast<LPARAM>(text), SMTO_ABORTIFHUNG,
127 interval_ms, &ignored);
128 }
129 }
130
131 static double gb(std::uint64_t bytes) { return double(bytes) / double(1ull << 30); }
132
133 // The hardware adapter with the most dedicated memory: the one LEONIDA_VK_DEVICE picks on a machine with a
134 // discrete GPU. The host's own dxgi.dll, not the title's shim.
135 static Microsoft::WRL::ComPtr<IDXGIAdapter3> gpu_adapter() {
136 using Create = HRESULT(WINAPI*)(REFIID, void**);
137 HMODULE dxgi = LoadLibraryW(L"dxgi.dll");
138 auto create = dxgi ? reinterpret_cast<Create>(GetProcAddress(dxgi, "CreateDXGIFactory1")) : nullptr;
139 Microsoft::WRL::ComPtr<IDXGIFactory1> factory;
140 Microsoft::WRL::ComPtr<IDXGIAdapter3> best;
141 if (!create || FAILED(create(IID_PPV_ARGS(&factory)))) return best;
142 SIZE_T most = 0;
143 Microsoft::WRL::ComPtr<IDXGIAdapter1> adapter;
144 for (UINT i = 0; factory->EnumAdapters1(i, &adapter) != DXGI_ERROR_NOT_FOUND; ++i) {
145 DXGI_ADAPTER_DESC1 desc{};
146 if (FAILED(adapter->GetDesc1(&desc)) || (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)) continue;
147 Microsoft::WRL::ComPtr<IDXGIAdapter3> three;
148 if (desc.DedicatedVideoMemory > most && SUCCEEDED(adapter.As(&three))) {
149 most = desc.DedicatedVideoMemory;
150 best = three;
151 }
152 }
153 return best;
154 }
155
156 HWND window_;
157 HANDLE stop_ = nullptr;
158 std::thread thread_;
159 LARGE_INTEGER frequency_{};
160 std::atomic<std::int64_t> last_present_{0}, first_present_{0}, worst_gap_{0};
161 std::atomic<std::uint64_t> presents_{0};
162};
163
164} // namespace rt::d3d12_xs
diff --git a/runtime/test/title_hud_test.cpp b/runtime/test/title_hud_test.cpp
new file mode 100644
index 000000000..8d4d17675
--- /dev/null
+++ b/runtime/test/title_hud_test.cpp
@@ -0,0 +1,71 @@
1// shims/title_hud.h: LEONIDA_TITLE_HUD's counters reach the title bar of a window owned by another thread, and a
2// window that stops pumping messages does not hold the HUD thread. A real Win32 window; no GPU needed.
3#include "title_hud.h"
4
5#include <windows.h>
6
7#include <cstdio>
8#include <string>
9
10#define CHECK(condition) do { \
11 if (!(condition)) { \
12 std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \
13 return 1; \
14 } \
15} while (0)
16
17static std::wstring title_of(HWND window) {
18 wchar_t text[512] = {};
19 InternalGetWindowText(window, text, 512); // no message: the window's own thread may be busy
20 return text;
21}
22
23// Pumps this thread's messages for `ms`, presenting every 16 ms when `present` is set.
24static void pump(DWORD ms, rt::d3d12_xs::TitleHud* present) {
25 const ULONGLONG end = GetTickCount64() + ms;
26 while (GetTickCount64() < end) {
27 MSG msg;
28 while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) {
29 TranslateMessage(&msg);
30 DispatchMessageW(&msg);
31 }
32 if (present) present->note_present();
33 Sleep(16);
34 }
35}
36
37int main() {
38 WNDCLASSW cls{};
39 cls.lpfnWndProc = DefWindowProcW;
40 cls.hInstance = GetModuleHandleW(nullptr);
41 cls.lpszClassName = L"LeonidaTitleHudTest";
42 CHECK(RegisterClassW(&cls));
43 HWND window = CreateWindowExW(WS_EX_NOACTIVATE, cls.lpszClassName, L"before", WS_OVERLAPPEDWINDOW, 0, 0, 64, 64,
44 nullptr, nullptr, cls.hInstance, nullptr);
45 CHECK(window);
46 {
47 rt::d3d12_xs::TitleHud hud(window);
48 // Presents at ~60 Hz for 1.5 s: the title shows a frame rate and the counters.
49 pump(1500, &hud);
50 const std::wstring text = title_of(window);
51 std::fwprintf(stderr, L"title: %ls\n", text.c_str());
52 CHECK(text.rfind(L"Leonida | ", 0) == 0);
53 CHECK(text.find(L" fps ") != std::wstring::npos && text.find(L" ms (worst ") != std::wstring::npos);
54 CHECK(text.find(L"RAM ") != std::wstring::npos && text.find(L"VRAM ") != std::wstring::npos);
55 CHECK(text.find(L"PSO -") != std::wstring::npos); // no LEONIDA_MEM_PROBE: no pipeline counters
56 const int fps = _wtoi(text.c_str() + 10);
57 CHECK(fps >= 10 && fps <= 120); // a loose band: Sleep(16) on a loaded machine is not 60 Hz
58
59 // The window's thread stops pumping: the HUD's SendMessageTimeout gives up and the HUD thread is not stuck,
60 // so the destructor below returns.
61 Sleep(1200);
62 }
63 const ULONGLONG before = GetTickCount64();
64 {
65 rt::d3d12_xs::TitleHud quick(window);
66 } // stops at once, without waiting a whole interval
67 CHECK(GetTickCount64() - before < 1000);
68 DestroyWindow(window);
69 std::printf("title_hud: counters in the title bar of another thread's window; a busy window does not hold it\n");
70 return 0;
71}