upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/main.cpp
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 16:38:21 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 16:41:36 -0500
commit712714bef25bbe057117e09fa159ae26ac75f0f3 (patch)
tree40cd101f7bcc0f8e0f785eb57dcfea9060ee10fa /runtime/src/main.cpp
parent0bf7ef80667e0d7e62e4b22c78c7a713600f5edc (diff)
parent24d8af66af151f024b0b520fb397d899924f5486 (diff)
Merge main into perf/big-compute-budget (#1889)
Brings #1889 up to date with main after #1742 (the pipeline compile unlock), resolving the five conflicts as described in the guide on the PR: https://github.com/ubiquitarianism/leonida/pull/1889#issuecomment-7175252290 - vulkan_replay.h: PipelineCompiles::build takes both the large-compile budget and main's `prior` compiles; build_pipelines passes the budget, and main's check_compiled_pipelines / finish_compiled_pipelines stay. - vulkan_backend.h: execute_now takes the large-compile demand and passes the budget to the pass-0 compile, and pass 1 passes the budget too. set_large_compiles locks pipeline_mutex_, since #1742 moved the pipeline state under that lock. - replay_pipeline_compiles_test.cpp: both test blocks kept, and the prior_compiles_reuse call passes an empty budget. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/main.cpp')
-rw-r--r--runtime/src/main.cpp52
1 files changed, 51 insertions, 1 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp
index f01997775..094aaa321 100644
--- a/runtime/src/main.cpp
+++ b/runtime/src/main.cpp
@@ -24,6 +24,7 @@
24#include <exception> 24#include <exception>
25#include <filesystem> 25#include <filesystem>
26#include <map> 26#include <map>
27#include <mutex>
27#include <string> 28#include <string>
28#include <system_error> 29#include <system_error>
29#include <vector> 30#include <vector>
@@ -196,7 +197,9 @@ bool configure_first_run_ui(const std::filesystem::path& package, std::filesyste
196 {L"LEONIDA_D3D12", L"vulkan"}, {L"LEONIDA_ARENA_MB", L"2048"}, 197 {L"LEONIDA_D3D12", L"vulkan"}, {L"LEONIDA_ARENA_MB", L"2048"},
197 {L"LEONIDA_GRAPHICS_STAGE_LAYOUT", L"1"}, {L"LEONIDA_PIPELINE_COMPILE", L"on-demand"}, 198 {L"LEONIDA_GRAPHICS_STAGE_LAYOUT", L"1"}, {L"LEONIDA_PIPELINE_COMPILE", L"on-demand"},
198 {L"LEONIDA_FRAME_PACING", L"1"}, 199 {L"LEONIDA_FRAME_PACING", L"1"},
199 {L"LEONIDA_KEYBOARD_PAD", L"always"}, 200 // Focused, not always: keys typed into another window must not press anything on a consent page.
201 // Scripted presses (LEONIDA_INPUT_SCRIPT) still run while the window is unfocused.
202 {L"LEONIDA_KEYBOARD_PAD", L"1"},
200 }; 203 };
201 for (const auto& setting : settings) { 204 for (const auto& setting : settings) {
202 if (!set_default(setting.name, setting.value)) return false; 205 if (!set_default(setting.name, setting.value)) return false;
@@ -231,15 +234,53 @@ int usage() {
231// The run ends with the title's Vulkan device alive, so no ~Device stops the background pipeline compiler: stop it 234// The run ends with the title's Vulkan device alive, so no ~Device stops the background pipeline compiler: stop it
232// here, before ExitProcess ends its thread, maybe inside the driver's compiler (rt::d3d12::stop_pipeline_compiles). 235// here, before ExitProcess ends its thread, maybe inside the driver's compiler (rt::d3d12::stop_pipeline_compiles).
233constexpr unsigned kExitCompileWaitMs = 2000; // a console close gives the whole process 5 s 236constexpr unsigned kExitCompileWaitMs = 2000; // a console close gives the whole process 5 s
237// For a build still running on a title thread, then the session cache write (rt::d3d12::save_pipeline_caches_at_exit).
238constexpr unsigned kExitCacheWaitMs = 1000;
234 239
235static BOOL WINAPI stop_compiles_on_console_exit(DWORD event) { 240static BOOL WINAPI stop_compiles_on_console_exit(DWORD event) {
236 if (event == CTRL_C_EVENT || event == CTRL_BREAK_EVENT || event == CTRL_CLOSE_EVENT) { 241 if (event == CTRL_C_EVENT || event == CTRL_BREAK_EVENT || event == CTRL_CLOSE_EVENT) {
237 rt::d3d12_xs::stop_list_values(500); // as at the run's end: no list-value thread cut off inside the layer 242 rt::d3d12_xs::stop_list_values(500); // as at the run's end: no list-value thread cut off inside the layer
238 rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs); 243 rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs);
244 rt::d3d12::save_pipeline_caches_at_exit(kExitCacheWaitMs);
239 } 245 }
240 return FALSE; // the next handler (the default one: ExitProcess) ends the process, as before 246 return FALSE; // the next handler (the default one: ExitProcess) ends the process, as before
241} 247}
242 248
249// A run that ends in an exception nothing handles (a fault in the title or one of its DLLs, the int3 after a device
250// loss) would otherwise keep none of the pipelines it compiled, and the next boot builds them all again. The device is
251// left alone apart from that write; the exception then goes on as before (the previous filter, or Windows' own
252// handling), so the exit code is the exception's. Not for a stack overflow: no stack is left to write with.
253static LPTOP_LEVEL_EXCEPTION_FILTER previous_fatal_filter = nullptr;
254// The retail title installs a filter of its own (SetUnhandledExceptionFilter from api-ms-win-core-errorhandling),
255// which replaced this one and ended the process without it (measured: a title fault at 354 s wrote nothing). Its
256// call is served here instead: this filter stays installed and hands the exception to the title's, as Windows would.
257static void* volatile title_fatal_filter = nullptr;
258
259static LPTOP_LEVEL_EXCEPTION_FILTER WINAPI title_set_unhandled_exception_filter(LPTOP_LEVEL_EXCEPTION_FILTER filter) {
260 return reinterpret_cast<LPTOP_LEVEL_EXCEPTION_FILTER>(
261 InterlockedExchangePointer(&title_fatal_filter, reinterpret_cast<void*>(filter)));
262}
263
264static LONG WINAPI save_caches_on_fatal_exception(EXCEPTION_POINTERS* info) {
265 const DWORD code = info && info->ExceptionRecord ? info->ExceptionRecord->ExceptionCode : 0;
266 if (code != EXCEPTION_STACK_OVERFLOW) {
267 // Several title threads can fault at once (measured: eight within a second). The first writes; the others
268 // wait here for it, so none of them reaches the title's filter, which may end the process, mid-write.
269 static std::once_flag once;
270 std::call_once(once, [code] {
271 std::fprintf(stderr, "[rt] unhandled exception 0x%08lx: writing the session pipeline cache before the "
272 "process ends\n", static_cast<unsigned long>(code));
273 rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs);
274 rt::d3d12::save_pipeline_caches_at_exit(kExitCacheWaitMs);
275 std::fflush(stderr);
276 });
277 }
278 if (auto* title = reinterpret_cast<LPTOP_LEVEL_EXCEPTION_FILTER>(
279 InterlockedCompareExchangePointer(&title_fatal_filter, nullptr, nullptr)))
280 return title(info);
281 return previous_fatal_filter ? previous_fatal_filter(info) : EXCEPTION_CONTINUE_SEARCH;
282}
283
243static BOOL WINAPI show_package_window(HWND window, int command) { 284static BOOL WINAPI show_package_window(HWND window, int command) {
244 const BOOL previous = ShowWindow(window, command); 285 const BOOL previous = ShowWindow(window, command);
245 const DWORD error = GetLastError(); 286 const DWORD error = GetLastError();
@@ -347,6 +388,13 @@ int wmain(int argc, wchar_t** argv) {
347 rt::fs::configure(game_dir, overlay_dir); 388 rt::fs::configure(game_dir, overlay_dir);
348#ifdef LEONIDA_VULKAN_DEVICE 389#ifdef LEONIDA_VULKAN_DEVICE
349 if (run) { 390 if (run) {
391 // Before the image is loaded, so its import binds here (save_caches_on_fatal_exception).
392 const rt::shims::Entry filter_imports[] = {
393 {"api-ms-win-core-errorhandling-l1-1-0.dll", "SetUnhandledExceptionFilter",
394 reinterpret_cast<void*>(&title_set_unhandled_exception_filter)},
395 {"kernel32.dll", "SetUnhandledExceptionFilter", reinterpret_cast<void*>(&title_set_unhandled_exception_filter)},
396 };
397 rt::shims::Table{filter_imports, std::size(filter_imports)};
350 rt::package_splash::configure(game_dir); 398 rt::package_splash::configure(game_dir);
351 if (rt::package_splash::configured()) { 399 if (rt::package_splash::configured()) {
352 const rt::shims::Entry window_imports[] = { 400 const rt::shims::Entry window_imports[] = {
@@ -516,6 +564,7 @@ int wmain(int argc, wchar_t** argv) {
516 leonida::thread_sampler::start(main_module.base, main_module.size); // LEONIDA_SAMPLE_MS (diagnostic) 564 leonida::thread_sampler::start(main_module.base, main_module.size); // LEONIDA_SAMPLE_MS (diagnostic)
517#ifdef LEONIDA_VULKAN_DEVICE 565#ifdef LEONIDA_VULKAN_DEVICE
518 SetConsoleCtrlHandler(&stop_compiles_on_console_exit, TRUE); 566 SetConsoleCtrlHandler(&stop_compiles_on_console_exit, TRUE);
567 previous_fatal_filter = SetUnhandledExceptionFilter(&save_caches_on_fatal_exception);
519#endif 568#endif
520 int code; 569 int code;
521 { 570 {
@@ -536,6 +585,7 @@ int wmain(int argc, wchar_t** argv) {
536 rt::d3d12_xs::stop_list_values(500); 585 rt::d3d12_xs::stop_list_values(500);
537#ifdef LEONIDA_VULKAN_DEVICE 586#ifdef LEONIDA_VULKAN_DEVICE
538 rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs); 587 rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs);
588 rt::d3d12::save_pipeline_caches_at_exit(kExitCacheWaitMs);
539#endif 589#endif
540 rt::cleanup_tls_clones(); 590 rt::cleanup_tls_clones();
541 ExitProcess(static_cast<UINT>(code)); 591 ExitProcess(static_cast<UINT>(code));