upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/title_hud_test.cpp
diff options
context:
space:
mode:
authorTauwni <102470988+Tauwni@users.noreply.github.com>2026-11-26 12:19:12 +0100
committerTauwni <102470988+Tauwni@users.noreply.github.com>2026-11-26 12:19:12 +0100
commitbeffaa9a476ca1b3dfd9cfa55e39eafbb1f5eec6 (patch)
tree32877805bf4eb46d9a89d5753db6b5a2b06dc845 /runtime/test/title_hud_test.cpp
parentf29ca7bacae9507f83e8c56abdb604bd4efb4cf1 (diff)
parenta73a57e1339be072baf5522351c8ec7f1482b56b (diff)
Merge main into fix/null-descriptor-generated-dir
Diffstat (limited to 'runtime/test/title_hud_test.cpp')
-rw-r--r--runtime/test/title_hud_test.cpp190
1 files changed, 190 insertions, 0 deletions
diff --git a/runtime/test/title_hud_test.cpp b/runtime/test/title_hud_test.cpp
new file mode 100644
index 000000000..661f4d30c
--- /dev/null
+++ b/runtime/test/title_hud_test.cpp
@@ -0,0 +1,190 @@
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. The memory figures: VK_EXT_memory_budget's heaps
3// summed, DXGI's as the fallback, "n/a" where neither has one, and the private bytes Wine reports. A real Win32
4// window and made-up budgets; no GPU needed (d3d12_vram_budget_test reads a real device's).
5#include "title_hud.h"
6#include "../src/vulkan_memory_budget.h"
7
8#include <windows.h>
9
10#include <atomic>
11#include <cstdint>
12#include <cstdio>
13#include <string>
14
15#define CHECK(condition) do { \
16 if (!(condition)) { \
17 std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \
18 return 1; \
19 } \
20} while (0)
21
22static std::wstring title_of(HWND window) {
23 wchar_t text[512] = {};
24 InternalGetWindowText(window, text, 512); // no message: the window's own thread may be busy
25 return text;
26}
27
28// Pumps this thread's messages for `ms`, presenting every 16 ms when `present` is set.
29static void pump(DWORD ms, rt::d3d12_xs::TitleHud* present) {
30 const ULONGLONG end = GetTickCount64() + ms;
31 while (GetTickCount64() < end) {
32 MSG msg;
33 while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) {
34 TranslateMessage(&msg);
35 DispatchMessageW(&msg);
36 }
37 if (present) present->note_present();
38 Sleep(16);
39 }
40}
41
42constexpr std::uint64_t kGiB = 1ull << 30;
43
44// The memory figures without a window: VK_EXT_memory_budget's heaps summed, DXGI's answer as the fallback, and this
45// process's own private bytes. Made-up values except the last. The text and the private-bytes choice are
46// src/memory_figures.h's, shared with the dev overlay; dev_overlay_test checks them.
47static int check_figures() {
48 using rt::d3d12_xs::TitleHud;
49 namespace mf = leonida::memory_figures;
50
51 // Two device-local heaps (VRAM and a BAR window) and system memory.
52 VkPhysicalDeviceMemoryProperties heaps{};
53 heaps.memoryHeapCount = 3;
54 heaps.memoryHeaps[0] = {16 * kGiB, VK_MEMORY_HEAP_DEVICE_LOCAL_BIT};
55 heaps.memoryHeaps[1] = {32 * kGiB, 0};
56 heaps.memoryHeaps[2] = {kGiB / 4, VK_MEMORY_HEAP_DEVICE_LOCAL_BIT};
57 VkPhysicalDeviceMemoryBudgetPropertiesEXT budget{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT};
58 budget.heapUsage[0] = 3 * kGiB + kGiB / 4;
59 budget.heapBudget[0] = 15 * kGiB + kGiB * 3 / 4;
60 budget.heapUsage[1] = kGiB / 2;
61 budget.heapBudget[1] = 24 * kGiB;
62 budget.heapUsage[2] = kGiB / 4;
63 budget.heapBudget[2] = kGiB / 4;
64 budget.heapUsage[3] = 7 * kGiB; // past memoryHeapCount: not counted
65 budget.heapBudget[3] = 7 * kGiB;
66 const leonida::VramBudget summed = leonida::sum_vram_budget(heaps, budget);
67 CHECK(summed.available);
68 CHECK(summed.local_usage == 3 * kGiB + kGiB / 2 && summed.local_budget == 16 * kGiB);
69 CHECK(summed.shared_usage == kGiB / 2);
70 CHECK(mf::vram_text(summed) == "VRAM 3.5/16.0 GB +0.5 shared");
71
72 // A device that reports no budget for its device-local heaps has no figure, not a 0.
73 const VkPhysicalDeviceMemoryBudgetPropertiesEXT empty{
74 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT};
75 CHECK(!leonida::sum_vram_budget(heaps, empty).available);
76
77 // DXGI: Wine's QueryVideoMemoryInfo answers S_OK with no usage, which is not a figure; Windows' is.
78 DXGI_QUERY_VIDEO_MEMORY_INFO local{}, non_local{};
79 local.Budget = 24 * kGiB;
80 CHECK(!TitleHud::dxgi_budget(local, non_local).available);
81 local.CurrentUsage = 2 * kGiB;
82 non_local.CurrentUsage = kGiB;
83 const leonida::VramBudget dxgi = TitleHud::dxgi_budget(local, non_local);
84 CHECK(dxgi.available && dxgi.local_usage == 2 * kGiB && dxgi.local_budget == 24 * kGiB &&
85 dxgi.shared_usage == kGiB);
86
87 // This process's own counters: its private bytes are never 0, on Windows or under Wine (where only PagefileUsage
88 // is filled).
89 const mf::ProcessMemory own = mf::process_memory();
90 std::fprintf(stderr, "own counters: PrivateUsage %llu, PagefileUsage %llu, WorkingSetSize %llu\n",
91 (unsigned long long)own.private_usage, (unsigned long long)own.pagefile_usage,
92 (unsigned long long)own.working_set);
93 CHECK(own.read && mf::private_bytes(own) > 0);
94 return 0;
95}
96
97int main() {
98 if (const int failed = check_figures()) return failed;
99 WNDCLASSW cls{};
100 cls.lpfnWndProc = DefWindowProcW;
101 cls.hInstance = GetModuleHandleW(nullptr);
102 cls.lpszClassName = L"LeonidaTitleHudTest";
103 CHECK(RegisterClassW(&cls));
104 HWND window = CreateWindowExW(WS_EX_NOACTIVATE, cls.lpszClassName, L"before", WS_OVERLAPPEDWINDOW, 0, 0, 64, 64,
105 nullptr, nullptr, cls.hInstance, nullptr);
106 CHECK(window);
107 {
108 rt::d3d12_xs::TitleHud hud(window);
109 // Presents at ~60 Hz for 1.5 s: the title shows a frame rate and the counters.
110 pump(1500, &hud);
111 const std::wstring text = title_of(window);
112 std::fwprintf(stderr, L"title: %ls\n", text.c_str());
113 CHECK(text.rfind(L"Leonida | ", 0) == 0);
114 CHECK(text.find(L" fps ") != std::wstring::npos && text.find(L" ms (worst ") != std::wstring::npos);
115 CHECK(text.find(L"RAM ") != std::wstring::npos && text.find(L"VRAM ") != std::wstring::npos);
116 // The private bytes are read under Wine as well ("VRAM n/a" may show: no device behind this HUD).
117 CHECK(text.find(L"| RAM n/a") == std::wstring::npos);
118 CHECK(text.find(L"PSO -") != std::wstring::npos); // no LEONIDA_MEM_PROBE: no pipeline counters
119 const int fps = _wtoi(text.c_str() + 10);
120 CHECK(fps >= 10 && fps <= 120); // a loose band: Sleep(16) on a loaded machine is not 60 Hz
121
122 // Presents stop while the window still pumps: the title says how long since the last frame.
123 pump(1300, nullptr);
124 const std::wstring stalled = title_of(window);
125 std::fwprintf(stderr, L"title: %ls\n", stalled.c_str());
126 CHECK(stalled.find(L"0 fps, no frame for ") != std::wstring::npos);
127
128 // The window's thread stops pumping: the HUD's SendMessageTimeout gives up and the HUD thread is not stuck,
129 // so the destructor below returns.
130 Sleep(1200);
131 }
132 {
133 // A VRAM source (the device's VK_EXT_memory_budget in the runtime) is what the title shows, and it is read
134 // at most once per refresh, on the HUD's thread.
135 std::atomic<unsigned> reads{0};
136 rt::d3d12_xs::TitleHud hud(window, [&reads] {
137 ++reads;
138 leonida::VramBudget fake;
139 fake.available = true;
140 fake.local_usage = 3 * kGiB + kGiB / 2;
141 fake.local_budget = 20 * kGiB;
142 fake.shared_usage = kGiB / 2;
143 return fake;
144 });
145 pump(1300, &hud);
146 const std::wstring text = title_of(window);
147 std::fwprintf(stderr, L"title: %ls\n", text.c_str());
148 CHECK(text.find(L"| VRAM 3.5/20.0 GB +0.5 shared |") != std::wstring::npos);
149 CHECK(reads.load() >= 1 && reads.load() <= 3); // 1.3 s at one refresh per 500 ms
150 }
151 {
152 // No source of its own: the HUD shows what a device published (src/memory_figures.h), the figure the dev
153 // overlay shows as well.
154 static const int device = 0;
155 leonida::memory_figures::publish_vram_reader(
156 [](const void*) {
157 leonida::VramBudget fake;
158 fake.available = true;
159 fake.local_usage = 5 * kGiB;
160 fake.local_budget = 12 * kGiB;
161 return fake;
162 },
163 &device);
164 {
165 rt::d3d12_xs::TitleHud hud(window);
166 pump(700, &hud);
167 const std::wstring text = title_of(window);
168 std::fwprintf(stderr, L"title: %ls\n", text.c_str());
169 CHECK(text.find(L"| VRAM 5.0/12.0 GB +0.0 shared |") != std::wstring::npos);
170 }
171 leonida::memory_figures::withdraw_vram_reader(&device);
172 }
173 {
174 // A source with no figure (a device without VK_EXT_memory_budget): DXGI's figure where it has one (Windows),
175 // else "n/a" (Wine, whose DXGI answers 0; check_figures covers that choice).
176 rt::d3d12_xs::TitleHud hud(window, [] { return leonida::VramBudget{}; });
177 pump(700, &hud);
178 const std::wstring text = title_of(window);
179 std::fwprintf(stderr, L"title: %ls\n", text.c_str());
180 CHECK(text.find(L"| VRAM ") != std::wstring::npos && text.find(L"VRAM 3.5/20.0") == std::wstring::npos);
181 }
182 const ULONGLONG before = GetTickCount64();
183 {
184 rt::d3d12_xs::TitleHud quick(window);
185 } // stops at once, without waiting a whole interval
186 CHECK(GetTickCount64() - before < 1000);
187 DestroyWindow(window);
188 std::printf("title_hud: counters in the title bar of another thread's window; a busy window does not hold it\n");
189 return 0;
190}