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:
authorNotRexed <therealnothingfn@gmail.com>2026-11-27 17:50:37 -0500
committerNotRexed <therealnothingfn@gmail.com>2026-11-27 17:50:37 -0500
commitb87abdef5735adc68a614b2025ffcf9869a47cc5 (patch)
tree0d8b473e8e753883a01e00153175994d6b7949c2 /runtime/src/main.cpp
parent84e728faffb36cd96a08934e5632ba9b194801f3 (diff)
parent34e33d3ddc4dd95ad14a074db886df3d25588201 (diff)
Merge mandy's main into port/d3d12-nvidia
Brings the D3D12 runtime branch (d3d12/, its launcher scripts and notes, and the pull requests reviewed and applied on it) together with mandy's main. Conflicts (67 files, none under d3d12/): docs/, make_game.py, runtime/, scripts/build_runtime.ps1, tests/ and tools/. Every one takes main's side: main is where that code is maintained, and this branch's copies were older states of the same files or the same pull requests in another form. tests/test_fspx_unpack.py is main's too, as it tests main's tools/fspx_unpack.py. Nothing under d3d12/ is changed by the merge. Checked before pushing: python -m compileall over make_game.py, tools and tests; make_game.py --help; scripts/check_no_game_data.py over the files this adds to main; tests/test_text_write_line_endings.py. The translator suite, the C++ runtime build and CI have not run on the result. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/main.cpp')
-rw-r--r--runtime/src/main.cpp539
1 files changed, 428 insertions, 111 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp
index 8039b6a87..c8545f338 100644
--- a/runtime/src/main.cpp
+++ b/runtime/src/main.cpp
@@ -9,6 +9,13 @@
9// gta6rt --init <image> [--game-dir DIR] [--overlay DIR] 9// gta6rt --init <image> [--game-dir DIR] [--overlay DIR]
10// The same mapping, then each loaded DLL's TLS callbacks and DllMain(DLL_PROCESS_ATTACH) are run. 10// The same mapping, then each loaded DLL's TLS callbacks and DllMain(DLL_PROCESS_ATTACH) are run.
11// 11//
12// gta6rt --precompile-pipelines [--threads N] [--big-threads N] [--big-bytes N] [--all] [--smallest-first]
13// [--limit N] [--min-bytes N] [--max-bytes N] [--filter PART]... [--skip-listed] [--list]
14// No game: creates the Vulkan device as --run does and builds every compute pipeline of the shader library
15// (LEONIDA_SHADERS) into the pipeline journal and the driver's cache, so a title's first dispatch of a large one
16// does not stall it. Resumable: entries already journaled are skipped (--all: built anyway). Programs named by
17// LEONIDA_SKIP_COMPUTE are built too unless --skip-listed is given. See shims/d3d12_precompile.h.
18//
12// gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--trace-d3d12] [--trace FILE] [-- game arguments...] 19// gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--trace-d3d12] [--trace FILE] [-- game arguments...]
13// The same, then the EXE's own TLS callbacks and its entry point, on a game thread. The game's argv and 20// The same, then the EXE's own TLS callbacks and its entry point, on a game thread. The game's argv and
14// command line are <exe name> plus the arguments after "--"; exit/ExitProcess from the game end the run 21// command line are <exe name> plus the arguments after "--"; exit/ExitProcess from the game end the run
@@ -18,32 +25,180 @@
18// (--overlay, default <game-dir>/overlay) so the package is never modified (shims/file_system.cpp). 25// (--overlay, default <game-dir>/overlay) so the package is never modified (shims/file_system.cpp).
19#include <windows.h> 26#include <windows.h>
20 27
28#include <cstdint>
21#include <cstdio> 29#include <cstdio>
22#include <cstdlib> 30#include <cstdlib>
23#include <cstring>
24#include <exception> 31#include <exception>
25#include <filesystem> 32#include <filesystem>
26#include <map> 33#include <map>
34#include <mutex>
27#include <string> 35#include <string>
28#include <system_error> 36#include <system_error>
29#include <vector> 37#include <vector>
30 38
39#include <cstring>
40
41namespace {
42
43// The retail title runs a hang watchdog thread: when the game's main loop has not set its "kick" byte for 30
44// one-second ticks, it calls a crash callback and then writes to address 0 (`movl $0, 0`). Under the runtime the
45// game thread can legitimately stall for longer than that, e.g. while the driver compiles pipelines against a cold
46// cache (first boot after a driver update), and the title then kills itself. By default the watchdog stays armed, as
47// on the console. LEONIDA_TITLE_WATCHDOG=0 asks to keep it from firing, as before. The two places that takes are
48// measured locally and not kept in the repository: LEONIDA_TITLE_WATCHDOG_KICK="<trap RVA>,<kick RVA>" (hex) names
49// them, and only with both variables, and only when that exact trap instruction sits at the first RVA, does a
50// runtime thread set the byte at the second twice a second, so the title's watchdog never fires. Either variable
51// alone leaves the watchdog armed and says so once.
52constexpr unsigned char kWatchdogTrap[] = {0xC7, 0x04, 0x25, 0, 0, 0, 0, 0, 0, 0, 0}; // movl $0, 0x0
53
54bool watchdog_rvas(std::uint32_t& trap_rva, std::uint32_t& kick_rva) {
55 char value[64]{};
56 const DWORD n = GetEnvironmentVariableA("LEONIDA_TITLE_WATCHDOG_KICK", value, sizeof(value));
57 if (!n || n >= sizeof(value)) return false;
58 char* rest = nullptr;
59 const unsigned long long trap = std::strtoull(value, &rest, 16);
60 if (!rest || *rest != ',') return false;
61 const unsigned long long kick = std::strtoull(rest + 1, &rest, 16);
62 if (!rest || *rest || !trap || !kick || trap > 0xFFFFFFFFull || kick > 0xFFFFFFFFull) return false;
63 trap_rva = static_cast<std::uint32_t>(trap);
64 kick_rva = static_cast<std::uint32_t>(kick);
65 return true;
66}
67
68void keep_title_watchdog_kicked(std::uint8_t* base) {
69 char keep[4]{};
70 const bool disarm = GetEnvironmentVariableA("LEONIDA_TITLE_WATCHDOG", keep, sizeof(keep)) == 1 && keep[0] == '0';
71 std::uint32_t kWatchdogTrapRva = 0, kWatchdogKickRva = 0;
72 const bool named = watchdog_rvas(kWatchdogTrapRva, kWatchdogKickRva);
73 if (!disarm) {
74 if (named)
75 std::fprintf(stderr, "[rt] loader: LEONIDA_TITLE_WATCHDOG_KICK is set but LEONIDA_TITLE_WATCHDOG is not 0;"
76 " the title's hang watchdog stays armed\n");
77 return;
78 }
79 if (!named) {
80 std::fprintf(stderr, "[rt] loader: LEONIDA_TITLE_WATCHDOG=0 needs LEONIDA_TITLE_WATCHDOG_KICK=<trap RVA>,"
81 "<kick RVA> (hex); the title's hang watchdog stays armed\n");
82 return;
83 }
84 MEMORY_BASIC_INFORMATION trap{}, kick{};
85 if (!VirtualQuery(base + kWatchdogTrapRva, &trap, sizeof(trap)) || trap.State != MEM_COMMIT ||
86 std::memcmp(base + kWatchdogTrapRva, kWatchdogTrap, sizeof(kWatchdogTrap)) != 0)
87 return; // not the trap the RVAs name: leave the title's watchdog alone
88 const DWORD writable = PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY;
89 if (!VirtualQuery(base + kWatchdogKickRva, &kick, sizeof(kick)) || kick.State != MEM_COMMIT ||
90 !(kick.Protect & writable))
91 return;
92 volatile std::uint8_t* const byte = base + kWatchdogKickRva;
93 const HANDLE thread = CreateThread(
94 nullptr, 0,
95 [](LPVOID p) -> DWORD {
96 auto* b = static_cast<volatile std::uint8_t*>(p);
97 for (;;) {
98 *b = 1;
99 Sleep(500);
100 }
101 },
102 const_cast<std::uint8_t*>(byte), 0, nullptr);
103 if (thread) {
104 CloseHandle(thread);
105 std::fprintf(stderr, "[rt] loader: keeping the title's hang watchdog kicked (LEONIDA_TITLE_WATCHDOG=0; unset or 1 keeps it armed)\n");
106 }
107}
108
109// LEONIDA_PEEK="<rva>:3,<rva>,<rva>" (diagnostic): a thread prints the 8-byte values at these RVAs of the
110// title's image (":n" = n consecutive values, at most 32; "RVA>OFF" reads through the pointer stored at the RVA, at
111// pointer + OFF, both hex) every LEONIDA_PEEK_SECONDS (default 10). For answering "what does this
112// global hold at run time" without a debugger: the reverse-engineering notes (docs/re/requests.md) ask for exactly
113// that. Reads are checked; nothing is written.
114void start_title_peek(std::uint8_t* base) {
115 static char spec[1024];
116 const DWORD n = GetEnvironmentVariableA("LEONIDA_PEEK", spec, sizeof(spec));
117 if (!n || n >= sizeof(spec)) return;
118 static std::uint8_t* image = base;
119 const HANDLE thread = CreateThread(
120 nullptr, 0,
121 [](LPVOID) -> DWORD {
122 char seconds[16]{};
123 DWORD period = 10;
124 if (GetEnvironmentVariableA("LEONIDA_PEEK_SECONDS", seconds, sizeof(seconds)) > 0)
125 period = std::max<DWORD>(1, static_cast<DWORD>(std::strtoul(seconds, nullptr, 10)));
126 for (unsigned tick = 0;; ++tick) {
127 std::string line = "[rt] peek t=" + std::to_string(tick * period) + "s:";
128 for (const char* at = spec; *at;) {
129 char* end = nullptr;
130 const std::uint64_t rva = std::strtoull(at, &end, 16);
131 if (end == at) break;
132 // "RVA>OFF": the 8-byte pointer at the RVA, then the values at pointer + OFF (both hex).
133 bool deref = false;
134 std::uint64_t offset = 0;
135 if (*end == '>') {
136 deref = true;
137 offset = std::strtoull(end + 1, &end, 16);
138 }
139 unsigned count = 1;
140 if (*end == ':') count = static_cast<unsigned>(std::strtoul(end + 1, &end, 10));
141 char text[48];
142 if (deref)
143 std::snprintf(text, sizeof(text), " 0x%llx>%llx=", static_cast<unsigned long long>(rva),
144 static_cast<unsigned long long>(offset));
145 else
146 std::snprintf(text, sizeof(text), " 0x%llx=", static_cast<unsigned long long>(rva));
147 line += text;
148 const std::uint8_t* from = image + rva;
149 if (deref) {
150 std::uint64_t pointer = 0;
151 SIZE_T got = 0;
152 if (!ReadProcessMemory(GetCurrentProcess(), from, &pointer, sizeof(pointer), &got) ||
153 got != sizeof(pointer) || !pointer)
154 count = 0, line += "?";
155 from = reinterpret_cast<const std::uint8_t*>(pointer + offset);
156 }
157 for (unsigned i = 0; i < count && i < 32; ++i) {
158 std::uint64_t value = 0;
159 SIZE_T got = 0;
160 const bool ok = ReadProcessMemory(GetCurrentProcess(), from + 8ull * i, &value,
161 sizeof(value), &got) && got == sizeof(value);
162 std::snprintf(text, sizeof(text), ok ? "%s%llx" : "%s?", i ? "," : "",
163 static_cast<unsigned long long>(value));
164 line += text;
165 }
166 at = *end == ',' ? end + 1 : end;
167 if (at == end && *end) break;
168 }
169 std::fprintf(stderr, "%s\n", line.c_str());
170 Sleep(period * 1000);
171 }
172 },
173 nullptr, 0, nullptr);
174 if (thread) CloseHandle(thread);
175}
176
177} // namespace
178
31#include "base_reservation.h" 179#include "base_reservation.h"
180#include "compile_gate_policy.h"
32#include "code_hooks.h" 181#include "code_hooks.h"
33#include "display_profile.h" 182#include "cpu_emulation.h"
34#include "frame_step.h"
35#include "host_cpu.h" 183#include "host_cpu.h"
36#include "mods.h" 184#include "mods.h"
37#include "pe_loader.h" 185#include "pe_loader.h"
186#include "thread_stacks.h"
187#include "dev_overlay.h"
188#include "thread_sampler.h"
189#include "title_page_watch.h"
190#include "title_geometry_watch.h"
38#include "../shims/d3d12_trace.h" 191#include "../shims/d3d12_trace.h"
39#include "../shims/d3d12_xs_device.h" 192#include "../shims/d3d12_xs_device.h"
193#include "../shims/file_system.h"
40#ifdef LEONIDA_VULKAN_DEVICE 194#ifdef LEONIDA_VULKAN_DEVICE
41#include "../shims/d3d12_com.h" // title memory (rt::d3d12::title_memory_visible, set_title_allocator) 195#include "vulkan_device_fault.h"
196#include "../shims/d3d12_compile_exit.h"
197#include "../shims/d3d12_precompile.h"
198#include "../shims/package_splash.h"
42#endif 199#endif
43#include "../shims/file_system.h"
44#include "../shims/process_shim.h" 200#include "../shims/process_shim.h"
45#include "../shims/shims.h" 201#include "../shims/shims.h"
46#include "console_insn.h"
47#include "../shims/startup_trace.h" 202#include "../shims/startup_trace.h"
48#include "../shims/xmem.h" 203#include "../shims/xmem.h"
49 204
@@ -58,78 +213,192 @@ const char* binding_name(rt::Binding b) {
58 } 213 }
59} 214}
60 215
61// The title's output size (src/display_profile.h, VIctory's native 1440p profile): LEONIDA_DISPLAY_MODE other than 216// Module TLS index 0 for the game's executable (rt::reserve_tls_index0), from the CRT's initializer table: .CRT$XCT
62// 3840x2160, or LEONIDA_RENDER_SCALE, patches the mapped executable before any of its code runs. 217// runs after the C runtime is set up and before every C++ dynamic initializer of gta6rt (.CRT$XCU), so before any of
63void apply_display_profile(const rt::Module& module) { 218// gta6rt's code can have started a thread. The frames above it (wmainCRTStartup, __scrt_common_main_seh, _initterm)
64 char mode_text[64] = {}, scale_text[32] = {}; 219// hold no TLS slot address. The result is read again where an image is run.
65 const DWORD mode_length = GetEnvironmentVariableA("LEONIDA_DISPLAY_MODE", mode_text, sizeof(mode_text)); 220void __cdecl reserve_tls_index0_at_start() { rt::reserve_tls_index0(); }
66 const DWORD scale_length = GetEnvironmentVariableA("LEONIDA_RENDER_SCALE", scale_text, sizeof(scale_text)); 221#pragma section(".CRT$XCT", read)
67 leonida::DisplayMode mode; 222extern "C" __declspec(allocate(".CRT$XCT")) void(__cdecl* const gta6rt_reserve_tls_index0)() =
68 if (mode_length && (mode_length >= sizeof(mode_text) || !leonida::parse_display_mode(mode_text, &mode))) { 223 reserve_tls_index0_at_start;
69 std::fprintf(stderr, "[rt] display: LEONIDA_DISPLAY_MODE is not WxH with W in 640..7680 and H in 360..4320; " 224#pragma comment(linker, "/INCLUDE:gta6rt_reserve_tls_index0")
70 "the title keeps its 3840x2160\n");
71 return;
72 }
73 float scale = 0.0f;
74 if (scale_length) {
75 char* end = nullptr;
76 scale = scale_length < sizeof(scale_text) ? std::strtof(scale_text, &end) : 0.0f;
77 if (!end || end == scale_text || *end || !(scale >= 0.25f && scale <= 1.0f)) {
78 std::fprintf(stderr, "[rt] display: LEONIDA_RENDER_SCALE is not a number in 0.25..1; ignored\n");
79 scale = 0.0f;
80 }
81 }
82 const leonida::display_profile::Plan plan = leonida::display_profile::plan(mode, scale);
83 if (plan.refused)
84 std::fprintf(stderr, "[rt] display: %s keeps its 3840x2160 output: %s\n", module.name.c_str(), plan.refused);
85 if (plan.patches.empty()) return;
86 std::string error;
87 if (leonida::display_profile::apply_to_image(module.base, module.size, plan, &error))
88 std::fprintf(stderr, "[rt] display: %s outputs %ux%u and renders its scene at %ux%u (scale %.3f, scanout "
89 "reservation 0x%X): SDR mode, scanout reservation, scene scale and Settings viewport "
90 "patched\n", module.name.c_str(), plan.output.width, plan.output.height,
91 plan.render_width, plan.render_height, plan.scale, plan.pool);
92 else
93 std::fprintf(stderr, "[rt] display: %s keeps its 3840x2160 output: %s (nothing patched)\n",
94 module.name.c_str(), error.c_str());
95}
96 225
97// The title's time step (src/frame_step.h): the measured frame time at any frame rate, unless LEONIDA_TIME_STEP=stock. 226// Select the settings used to display the offline first-run UI. Keep explicit caller settings.
98void apply_frame_step(const rt::Module& module) { 227bool configure_first_run_ui(const std::filesystem::path& package, std::filesystem::path& overlay) {
99 char value[32] = {}; 228 // A variable that is set but empty is not a setting: Windows cannot hold one, but a caller on Wine can pass one
100 const DWORD length = GetEnvironmentVariableA("LEONIDA_TIME_STEP", value, sizeof(value)); 229 // (NAME= in the environment), and asking for the size alone reports it as present there.
101 bool known = true; 230 const auto set_default = [](const wchar_t* name, const wchar_t* value) {
102 const bool wanted = leonida::frame_step::wanted(length < sizeof(value) ? value : "?", &known); 231 wchar_t first[2]{};
103 if (!known) std::fprintf(stderr, "[rt] time step: LEONIDA_TIME_STEP is not measured or stock; measured\n"); 232 return GetEnvironmentVariableW(name, first, 2) != 0 || _wputenv_s(name, value) == 0;
104 if (!wanted) { 233 };
105 std::fprintf(stderr, "[rt] time step: %s keeps its own step (1/30 to 1/15 s a frame)\n", module.name.c_str()); 234 const struct { const wchar_t* name; const wchar_t* value; } settings[] = {
106 return; 235 {L"LEONIDA_D3D12", L"vulkan"}, {L"LEONIDA_ARENA_MB", L"2048"},
236 {L"LEONIDA_GRAPHICS_STAGE_LAYOUT", L"1"}, {L"LEONIDA_PIPELINE_COMPILE", L"on-demand"},
237 {L"LEONIDA_FRAME_PACING", L"1"},
238 // Focused, not always: keys typed into another window must not press anything on a consent page.
239 // Scripted presses (LEONIDA_INPUT_SCRIPT) still run while the window is unfocused.
240 {L"LEONIDA_KEYBOARD_PAD", L"1"},
241 };
242 for (const auto& setting : settings) {
243 if (!set_default(setting.name, setting.value)) return false;
244 }
245 std::wstring executable(32768, L'\0');
246 const DWORD length = GetModuleFileNameW(nullptr, executable.data(), static_cast<DWORD>(executable.size()));
247 if (!length || length >= executable.size()) return false;
248 executable.resize(length);
249 const auto state = std::filesystem::path(executable).parent_path() / L"first-run-ui";
250 if (overlay.empty()) overlay = state / L"overlay";
251 if (!GetEnvironmentVariableW(L"LEONIDA_XMEM_BACKING_DIR", nullptr, 0)) {
252 std::error_code error;
253 const auto backing = state / L"xmem";
254 std::filesystem::create_directories(backing, error);
255 if (error || !set_default(L"LEONIDA_XMEM_BACKING_DIR", backing.c_str())) return false;
107 } 256 }
108 std::string error; 257 return SetCurrentDirectoryW(package.c_str()) != FALSE;
109 if (leonida::frame_step::apply_to_image(module.base, module.size, leonida::frame_step::plan(), &error))
110 std::fprintf(stderr, "[rt] time step: %s steps by the measured frame time (1/3000 to 1 s a frame; "
111 "LEONIDA_TIME_STEP=stock for its own)\n", module.name.c_str());
112 else
113 std::fprintf(stderr, "[rt] time step: %s keeps its own step: %s (nothing patched)\n", module.name.c_str(),
114 error.c_str());
115} 258}
116 259
117int usage() { 260int usage() {
118 std::fprintf(stderr, 261 std::fprintf(stderr,
119 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace FILE]\n" 262 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace FILE]\n"
120 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE]\n" 263 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE]\n"
121 " gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n"); 264 " gta6rt --first-run-ui <exe> [--game-dir DIR] [--overlay DIR] [--trace FILE]\n"
265 " gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n"
266 " gta6rt --precompile-pipelines [--threads N] [--big-threads N] [--big-bytes N] [--all] [--smallest-first] [--limit N] [--min-bytes N] [--max-bytes N] [--filter PART]... [--skip-listed] [--list] [--via-helper]\n"
267 " gta6rt --compile-server [--parent PID] [--threads N] [--idle-threads N] [--idle-exit SECONDS]\n");
122 return 2; 268 return 2;
123} 269}
124 270
271#ifdef LEONIDA_VULKAN_DEVICE
272// gta6rt --precompile-pipelines ...: argv[1] is the mode.
273int precompile_main(int argc, wchar_t** argv) {
274 rt::d3d12::PrecompileOptions options;
275 const auto number = [&](int& i, auto& out) {
276 if (i + 1 >= argc) return false;
277 wchar_t* end = nullptr;
278 const unsigned long long v = std::wcstoull(argv[++i], &end, 10);
279 if (!end || *end) return false;
280 out = static_cast<std::remove_reference_t<decltype(out)>>(v);
281 return true;
282 };
283 for (int i = 2; i < argc; ++i) {
284 const std::wstring a = argv[i];
285 bool ok = true;
286 if (a == L"--threads") ok = number(i, options.threads);
287 else if (a == L"--big-threads") ok = number(i, options.big_threads);
288 else if (a == L"--big-bytes") ok = number(i, options.big_bytes);
289 else if (a == L"--limit") ok = number(i, options.limit);
290 else if (a == L"--min-bytes") ok = number(i, options.min_bytes);
291 else if (a == L"--max-bytes") ok = number(i, options.max_bytes);
292 else if (a == L"--all") options.rebuild = true;
293 else if (a == L"--smallest-first") options.smallest_first = true;
294 else if (a == L"--skip-listed") options.skip_listed = true;
295 else if (a == L"--list") options.dry_run = true;
296 else if (a == L"--via-helper") options.via_helper = true;
297 else if (a == L"--filter" && i + 1 < argc) {
298 const std::wstring part = argv[++i];
299 options.filters.emplace_back(part.begin(), part.end()); // library names are ASCII
300 } else ok = false;
301 if (!ok) return usage();
302 }
303 return rt::d3d12::precompile_compute_pipelines(options);
304}
305
306// gta6rt --compile-server [--parent PID] [--threads N] [--idle-threads N] [--idle-exit SECONDS]
307// (shims/d3d12_compile_helper.h).
308int compile_server_main(int argc, wchar_t** argv) {
309 rt::d3d12::CompileServerOptions options;
310 for (int i = 2; i < argc; ++i) {
311 const std::wstring a = argv[i];
312 if (i + 1 >= argc) return usage();
313 wchar_t* end = nullptr;
314 const unsigned long long v = std::wcstoull(argv[++i], &end, 10);
315 if (!end || *end) return usage();
316 if (a == L"--parent") options.parent_pid = static_cast<unsigned long>(v);
317 else if (a == L"--threads") options.threads = static_cast<unsigned>(v);
318 else if (a == L"--idle-threads") options.idle_threads = static_cast<unsigned>(v);
319 else if (a == L"--idle-exit") options.idle_exit_s = static_cast<unsigned>(v);
320 else return usage();
321 }
322 return rt::d3d12::compile_server(options);
323}
324#endif
325
125} // namespace 326} // namespace
126 327
328#ifdef LEONIDA_VULKAN_DEVICE
329// The run ends with the title's Vulkan device alive, so no ~Device stops the background pipeline compiler: stop it
330// here, before ExitProcess ends its thread, maybe inside the driver's compiler (rt::d3d12::stop_pipeline_compiles).
331constexpr unsigned kExitCompileWaitMs = 2000; // a console close gives the whole process 5 s
332// For a build still running on a title thread, then the session cache write (rt::d3d12::save_pipeline_caches_at_exit).
333constexpr unsigned kExitCacheWaitMs = 1000;
334
335static BOOL WINAPI stop_compiles_on_console_exit(DWORD event) {
336 if (event == CTRL_C_EVENT || event == CTRL_BREAK_EVENT || event == CTRL_CLOSE_EVENT) {
337 rt::d3d12_xs::stop_list_values(500); // as at the run's end: no list-value thread cut off inside the layer
338 rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs);
339 rt::d3d12::save_pipeline_caches_at_exit(kExitCacheWaitMs);
340 }
341 return FALSE; // the next handler (the default one: ExitProcess) ends the process, as before
342}
343
344// A run that ends in an exception nothing handles (a fault in the title or one of its DLLs, the int3 after a device
345// loss) would otherwise keep none of the pipelines it compiled, and the next boot builds them all again. The device is
346// left alone apart from that write; the exception then goes on as before (the previous filter, or Windows' own
347// handling), so the exit code is the exception's. Not for a stack overflow: no stack is left to write with.
348static LPTOP_LEVEL_EXCEPTION_FILTER previous_fatal_filter = nullptr;
349// The retail title installs a filter of its own (SetUnhandledExceptionFilter from api-ms-win-core-errorhandling),
350// which replaced this one and ended the process without it (measured: a title fault at 354 s wrote nothing). Its
351// call is served here instead: this filter stays installed and hands the exception to the title's, as Windows would.
352static void* volatile title_fatal_filter = nullptr;
353
354static LPTOP_LEVEL_EXCEPTION_FILTER WINAPI title_set_unhandled_exception_filter(LPTOP_LEVEL_EXCEPTION_FILTER filter) {
355 return reinterpret_cast<LPTOP_LEVEL_EXCEPTION_FILTER>(
356 InterlockedExchangePointer(&title_fatal_filter, reinterpret_cast<void*>(filter)));
357}
358
359static LONG WINAPI save_caches_on_fatal_exception(EXCEPTION_POINTERS* info) {
360 const DWORD code = info && info->ExceptionRecord ? info->ExceptionRecord->ExceptionCode : 0;
361 if (code != EXCEPTION_STACK_OVERFLOW) {
362 // Several title threads can fault at once (measured: eight within a second). The first writes; the others
363 // wait here for it, so none of them reaches the title's filter, which may end the process, mid-write.
364 static std::once_flag once;
365 std::call_once(once, [code] {
366 std::fprintf(stderr, "[rt] unhandled exception 0x%08lx: writing the session pipeline cache before the "
367 "process ends\n", static_cast<unsigned long>(code));
368 rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs);
369 rt::d3d12::save_pipeline_caches_at_exit(kExitCacheWaitMs);
370 std::fflush(stderr);
371 });
372 }
373 if (auto* title = reinterpret_cast<LPTOP_LEVEL_EXCEPTION_FILTER>(
374 InterlockedCompareExchangePointer(&title_fatal_filter, nullptr, nullptr)))
375 return title(info);
376 return previous_fatal_filter ? previous_fatal_filter(info) : EXCEPTION_CONTINUE_SEARCH;
377}
378
379static BOOL WINAPI show_package_window(HWND window, int command) {
380 const BOOL previous = ShowWindow(window, command);
381 const DWORD error = GetLastError();
382 if (command != SW_HIDE && rt::package_splash::paint_window(window))
383 rt::startup_trace::event("package_splash_window");
384 SetLastError(error);
385 return previous;
386}
387#endif
388
127int wmain(int argc, wchar_t** argv) { 389int wmain(int argc, wchar_t** argv) {
390#ifdef LEONIDA_VULKAN_DEVICE
391 if (argc > 1 && std::wstring(argv[1]) == L"--precompile-pipelines") return precompile_main(argc, argv);
392 if (argc > 1 && std::wstring(argv[1]) == L"--compile-server") return compile_server_main(argc, argv);
393#endif
394 // The title's process (not a build host above): LEONIDA_COMPILE_GATE=auto may calibrate the driver when the device
395 // is made (src/compile_gate_policy.h).
396 leonida::compile_gate_policy::calibration_allowed = true;
128 std::filesystem::path image, game_dir, overlay_dir, mods_dir; 397 std::filesystem::path image, game_dir, overlay_dir, mods_dir;
129 uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash 398 uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash
130 int archetype_lookup_arg = 0; 399 int archetype_lookup_arg = 0;
131 uint32_t view_mode_getter_rva = 0; // --view-mode-getter RVA: the game's view mode of a camera context 400 uint32_t view_mode_getter_rva = 0; // --view-mode-getter RVA: the game's view mode of a camera context
132 bool dry_run = false, init = false, run = false, trace_d3d12 = false; 401 bool dry_run = false, init = false, run = false, trace_d3d12 = false, first_run_ui = false;
133 std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84) 402 std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84)
134 std::wstring trace_file; 403 std::wstring trace_file;
135 for (int i = 1; i < argc; ++i) { 404 for (int i = 1; i < argc; ++i) {
@@ -140,6 +409,7 @@ int wmain(int argc, wchar_t** argv) {
140 } 409 }
141 if (a == L"--dry-run") dry_run = true; 410 if (a == L"--dry-run") dry_run = true;
142 else if (a == L"--init") init = true; 411 else if (a == L"--init") init = true;
412 else if (a == L"--first-run-ui") first_run_ui = true;
143 else if (a == L"--run") run = true; 413 else if (a == L"--run") run = true;
144 else if (a == L"--trace-d3d12") trace_d3d12 = true; 414 else if (a == L"--trace-d3d12") trace_d3d12 = true;
145 else if (a == L"--trace" && i + 1 < argc) trace_file = argv[++i]; 415 else if (a == L"--trace" && i + 1 < argc) trace_file = argv[++i];
@@ -166,7 +436,8 @@ int wmain(int argc, wchar_t** argv) {
166 } 436 }
167 if (image.empty()) return usage(); 437 if (image.empty()) return usage();
168 // Exactly one mode: --dry-run --run would otherwise initialise the modules and call the entry point. 438 // Exactly one mode: --dry-run --run would otherwise initialise the modules and call the entry point.
169 if (dry_run + init + run != 1) return usage(); 439 if (dry_run + init + run + first_run_ui != 1) return usage();
440 run = run || first_run_ui;
170 // The game runs AVX code from its TLS callbacks on; without AVX that is a bare 0xC000001D (src/host_cpu.h). 441 // The game runs AVX code from its TLS callbacks on; without AVX that is a bare 0xC000001D (src/host_cpu.h).
171 if ((init || run) && !rt::host_cpu::check_skipped()) 442 if ((init || run) && !rt::host_cpu::check_skipped())
172 if (const auto missing = rt::host_cpu::missing_host_features(); !missing.empty()) { 443 if (const auto missing = rt::host_cpu::missing_host_features(); !missing.empty()) {
@@ -181,7 +452,21 @@ int wmain(int argc, wchar_t** argv) {
181 std::filesystem::path a = std::filesystem::absolute(p, ec); 452 std::filesystem::path a = std::filesystem::absolute(p, ec);
182 return ec ? p : a; 453 return ec ? p : a;
183 }; 454 };
455 if (first_run_ui) {
456 image = absolute_or_same(image);
457 if (!mods_dir.empty()) mods_dir = absolute_or_same(mods_dir);
458 if (!trace_file.empty()) trace_file = absolute_or_same(trace_file).wstring();
459 }
184 game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir); 460 game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir);
461 // An unattended run can select a fresh write root even when its launcher supplies --overlay.
462 if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_WRITE_DIR", nullptr, 0)) {
463 std::wstring dir(size, 0);
464 const DWORD n = GetEnvironmentVariableW(L"LEONIDA_WRITE_DIR", dir.data(), size);
465 if (n && n < size) {
466 dir.resize(n);
467 overlay_dir = dir;
468 }
469 }
185 if (!overlay_dir.empty()) overlay_dir = absolute_or_same(overlay_dir); 470 if (!overlay_dir.empty()) overlay_dir = absolute_or_same(overlay_dir);
186 // LEONIDA_SHADERS too: the D3D12 device reads it at the title's first pipeline state, after it may have moved. 471 // LEONIDA_SHADERS too: the D3D12 device reads it at the title's first pipeline state, after it may have moved.
187 if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_SHADERS", nullptr, 0)) { 472 if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_SHADERS", nullptr, 0)) {
@@ -197,8 +482,31 @@ int wmain(int argc, wchar_t** argv) {
197 if (!rt::base_reservation::inherited()) 482 if (!rt::base_reservation::inherited())
198 if (const auto range = rt::base_reservation::needed(image)) return rt::base_reservation::relaunch(*range); 483 if (const auto range = rt::base_reservation::needed(image)) return rt::base_reservation::relaunch(*range);
199 // GAME: paths read under --game-dir; whatever a title writes lands in the overlay, never in the package. 484 // GAME: paths read under --game-dir; whatever a title writes lands in the overlay, never in the package.
485 if (first_run_ui && !configure_first_run_ui(game_dir, overlay_dir)) {
486 std::fprintf(stderr, "cannot initialize the first-run UI paths or graphics settings\n");
487 return 1;
488 }
200 if (overlay_dir.empty()) overlay_dir = game_dir / "overlay"; 489 if (overlay_dir.empty()) overlay_dir = game_dir / "overlay";
201 rt::fs::configure(game_dir, overlay_dir); 490 rt::fs::configure(game_dir, overlay_dir);
491#ifdef LEONIDA_VULKAN_DEVICE
492 if (run) {
493 // Before the image is loaded, so its import binds here (save_caches_on_fatal_exception).
494 const rt::shims::Entry filter_imports[] = {
495 {"api-ms-win-core-errorhandling-l1-1-0.dll", "SetUnhandledExceptionFilter",
496 reinterpret_cast<void*>(&title_set_unhandled_exception_filter)},
497 {"kernel32.dll", "SetUnhandledExceptionFilter", reinterpret_cast<void*>(&title_set_unhandled_exception_filter)},
498 };
499 rt::shims::Table{filter_imports, std::size(filter_imports)};
500 rt::package_splash::configure(game_dir);
501 if (rt::package_splash::configured()) {
502 const rt::shims::Entry window_imports[] = {
503 {"ext-ms-win-rtcore-ntuser-window-l1-1-0.dll", "ShowWindow", reinterpret_cast<void*>(&show_package_window)},
504 {"user32.dll", "ShowWindow", reinterpret_cast<void*>(&show_package_window)},
505 };
506 rt::shims::Table{window_imports, std::size(window_imports)};
507 }
508 }
509#endif
202 // Mods (docs/modding.md): <folder of gta6rt.exe>\mods unless --mods names another. Loaded and reported here; 510 // Mods (docs/modding.md): <folder of gta6rt.exe>\mods unless --mods names another. Loaded and reported here;
203 // the archetype swaps take effect once the game's archetype lookup is hooked for this executable version. 511 // the archetype swaps take effect once the game's archetype lookup is hooked for this executable version.
204 if (mods_dir.empty()) { 512 if (mods_dir.empty()) {
@@ -230,61 +538,44 @@ int wmain(int argc, wchar_t** argv) {
230 rt::d3d12_trace::set_enabled(trace_d3d12); 538 rt::d3d12_trace::set_enabled(trace_d3d12);
231 // Slot 133 imports the title's pages; xmem commits them on first touch, so they are committed first. 539 // Slot 133 imports the title's pages; xmem commits them on first touch, so they are committed first.
232 rt::d3d12_xs::set_placed_at_va_pages_hook(&rt::xmem::commit_physical_range); 540 rt::d3d12_xs::set_placed_at_va_pages_hook(&rt::xmem::commit_physical_range);
233 // Queue slot 31, CopyPageMappingsBatchX, maps xmem's physical pages (VIctory d3d12x.rs: use_page_mapper). 541 if (rt::xmem::slot31_runs() && rt::xmem::queue_page_mappings_enabled())
234 rt::d3d12_xs::set_page_mapper(&rt::xmem::map_alias); 542 rt::d3d12_xs::set_copy_page_mappings_hook(&rt::xmem::copy_page_mappings);
235#ifdef LEONIDA_VULKAN_DEVICE 543 // So do the file shims' reads, whose kernel writes do not fault them in: on ERROR_NOACCESS (title_page_watch.h).
236 // The title's GPU memory, as xmem sees it, goes to the Vulkan layer's title memory (src/title_memory.h). With 544 leonida::geometry_writes::aliases.store(&rt::xmem::geometry_aliases);
237 // LEONIDA_TITLE_MEMORY=sparse xmem tells physical pages apart (their pools, and which pool bytes a mapped page 545 leonida::kernel_writes::set_committer(&rt::xmem::commit_physical_range);
238 // shows), which the layer binds into sparse buffers; otherwise it reports them as visible ranges, as before. 546 rt::d3d12_xs::set_texture_page_provider(&rt::xmem::pin_mapped_data);
239 char title_memory_mode[16] = {}; 547 leonida::kernel_writes::set_unguarder(&rt::xmem::reopen_unowned);
240 const DWORD title_memory_length =
241 GetEnvironmentVariableA("LEONIDA_TITLE_MEMORY", title_memory_mode, sizeof(title_memory_mode));
242 // Sparse is the default (the imports path loses the device on the title, docs/port_status.md); "imports" or "0"
243 // keep xmem reporting visible ranges. A device whose sparse self-test fails takes mapped pages as visible ranges.
244 const bool title_memory_named = title_memory_length && title_memory_length < sizeof(title_memory_mode);
245 const bool sparse_title_memory = !title_memory_named || (std::strcmp(title_memory_mode, "imports") != 0 &&
246 std::strcmp(title_memory_mode, "0") != 0);
247 using Mapped = void (*)(std::uintptr_t, std::size_t, const void*);
248 using Pool = void (*)(const void*, std::size_t, bool);
249 static const Mapped mapped = [](std::uintptr_t start, std::size_t size, const void* backing) {
250 rt::d3d12::title_memory_mapped(start, size, backing);
251 };
252 static const Pool pool = [](const void* backing, std::size_t size, bool alive) {
253 rt::d3d12::title_memory_pool(backing, size, alive);
254 };
255 static const rt::xmem::GraphicsListener title_memory{
256 [](std::uintptr_t start, std::size_t size) { rt::d3d12::title_memory_visible(start, size); },
257 [](std::uintptr_t start, std::size_t size) { rt::d3d12::title_memory_gone(start, size); },
258 sparse_title_memory ? mapped : nullptr, sparse_title_memory ? pool : nullptr};
259 rt::xmem::set_graphics_listener(&title_memory);
260#endif
261 if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) { 548 if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) {
262 std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n" 549 std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n"
263 : "error: cannot create the trace file\n"); 550 : "error: cannot create the trace file\n");
264 return 2; 551 return 2;
265 } 552 }
553 // The console's Zen 2 instructions this CPU may lack (docs/cpu_features.md): emulated on #UD. Installed after the
554 // trace's handler so it runs first, and an emulated instruction is not recorded as an exception.
555 if (const auto host = rt::cpu_emulation::detect_host(); !host.sse4a || !host.clzero || !host.monitorx || !host.clwb)
556 std::fprintf(stderr, "[rt] cpu: this CPU lacks%s%s%s%s; the game's uses are emulated (docs/cpu_features.md)\n",
557 host.sse4a ? "" : " SSE4a", host.clzero ? "" : " CLZERO", host.monitorx ? "" : " MONITORX",
558 host.clwb ? "" : " CLWB");
559 if (!rt::cpu_emulation::install())
560 std::fprintf(stderr, "[rt] cpu: AddVectoredExceptionHandler failed (error %lu); instructions this CPU lacks "
561 "will stop the game\n", GetLastError());
266 562
267 try { 563 try {
268 rt::Loader loader(game_dir); 564 rt::Loader loader(game_dir);
269 rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code 565 rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code
270 // GetModuleHandle(NULL) and GetModuleFileName(NULL) in mapped code name the title, not gta6rt.exe. 566 // An executable with TLS that did not get index 0: its /GA code would read and write the TLS in slot 0, which
271 rt::shims::set_main_image(main_module.base); 567 // is gta6rt's own (or another executable's), so no code of it may run.
272 // CLZERO, CLWB, MONITORX, SSE4a and the rest of the console CPU's instructions a PC's may lack are emulated 568 const auto* main_nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>(
273 // in the mapped images (src/console_insn.h). Added after the startup trace's handler, so it runs first and an 569 main_module.base + reinterpret_cast<const IMAGE_DOS_HEADER*>(main_module.base)->e_lfanew);
274 // emulated instruction is no exception event. 570 const bool main_is_exe = (main_nt->FileHeader.Characteristics & IMAGE_FILE_DLL) == 0;
275 rt::console_insn::install(); 571 if ((init || run) && main_is_exe && main_module.tls_slot != rt::kNoTlsIndex && main_module.tls_index != 0) {
276 if (init || run) { 572 std::string why;
277 apply_display_profile(main_module); 573 if (rt::reserve_tls_index0(&why)) why = "another mapped executable holds it";
278 apply_frame_step(main_module); 574 std::fprintf(stderr,
279 } 575 "error: %s cannot have module TLS index 0 (%s); code built with /GA would read and write "
280 // __XMemAllocationHooks__ (VIctory xmem.rs:243-300, d3d12x.rs:20): the title's allocation routines. XMemAlloc 576 "another module's TLS through slot 0, so it is not run\n",
281 // and XMemFree call them from now on, and committed resources take their memory from them, as the console's 577 main_module.name.c_str(), why.c_str());
282 // D3D runtime gives them (the title counts its back buffers against a pool of its own that way). 578 return 1;
283 if (const auto hooks = main_module.exports.find("__XMemAllocationHooks__"); hooks != main_module.exports.end()) {
284 rt::xmem::set_allocation_hooks(hooks->second);
285#ifdef LEONIDA_VULKAN_DEVICE
286 rt::d3d12::set_title_allocator(&rt::xmem::title_alloc, &rt::xmem::title_free);
287#endif
288 } 579 }
289 // The mods' archetype swaps (docs/modding.md): hooked here, before any image code runs, when the lookup's RVA 580 // The mods' archetype swaps (docs/modding.md): hooked here, before any image code runs, when the lookup's RVA
290 // for this executable is given. The count of redirected calls shows whether the RVA is the real lookup. 581 // for this executable is given. The count of redirected calls shows whether the RVA is the real lookup.
@@ -354,7 +645,9 @@ int wmain(int argc, wchar_t** argv) {
354 std::fprintf(stderr, "error: %s has no entry point\n", main_module.name.c_str()); 645 std::fprintf(stderr, "error: %s has no entry point\n", main_module.name.c_str());
355 return 1; 646 return 1;
356 } 647 }
357 const std::wstring paths = L"console paths: GAME: -> " + game_dir.native() + L", writes -> " + overlay_dir.native(); 648 const std::filesystem::path scratch_dir = rt::fs::scratch_dir();
649 const std::wstring paths = L"console paths: GAME: -> " + game_dir.native() + L", writes -> " +
650 overlay_dir.native() + L", T: -> " + scratch_dir.native();
358 const HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); 651 const HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
359 DWORD mode; 652 DWORD mode;
360 bool printed = false; 653 bool printed = false;
@@ -367,12 +660,29 @@ int wmain(int argc, wchar_t** argv) {
367 if (!printed) { 660 if (!printed) {
368 const auto game = game_dir.u8string(); 661 const auto game = game_dir.u8string();
369 const auto overlay = overlay_dir.u8string(); 662 const auto overlay = overlay_dir.u8string();
370 std::printf("console paths: GAME: -> %s, writes -> %s\n", 663 const auto scratch = scratch_dir.u8string();
371 reinterpret_cast<const char*>(game.c_str()), reinterpret_cast<const char*>(overlay.c_str())); 664 std::printf("console paths: GAME: -> %s, writes -> %s, T: -> %s\n",
665 reinterpret_cast<const char*>(game.c_str()), reinterpret_cast<const char*>(overlay.c_str()),
666 reinterpret_cast<const char*>(scratch.c_str()));
372 } 667 }
373 std::printf("running %s\n", main_module.name.c_str()); 668 std::printf("running %s\n", main_module.name.c_str());
374 std::fflush(stdout); 669 std::fflush(stdout);
375 const int code = rt::process::run_entry(main_module.base + main_module.entry_rva); 670 keep_title_watchdog_kicked(main_module.base);
671 start_title_peek(main_module.base);
672 leonida::dev_overlay::start(); // LEONIDA_DEV_OVERLAY=1
673 leonida::thread_sampler::start(main_module.base, main_module.size); // LEONIDA_SAMPLE_MS (diagnostic)
674#ifdef LEONIDA_VULKAN_DEVICE
675 SetConsoleCtrlHandler(&stop_compiles_on_console_exit, TRUE);
676 previous_fatal_filter = SetUnhandledExceptionFilter(&save_caches_on_fatal_exception);
677#endif
678 int code;
679#ifdef LEONIDA_VULKAN_DEVICE
680 leonida::thread_stack_hook.store(&rt::print_thread_stacks_now, std::memory_order_release);
681#endif
682 {
683 auto stacks = rt::start_thread_stacks();
684 code = rt::process::run_entry(main_module.base + main_module.entry_rva);
685 }
376 std::fflush(stdout); 686 std::fflush(stdout);
377 std::printf("game exited with code %d\n", code); 687 std::printf("game exited with code %d\n", code);
378 rt::shims::report_called_unimplemented(); 688 rt::shims::report_called_unimplemented();
@@ -382,6 +692,13 @@ int wmain(int argc, wchar_t** argv) {
382 // Not `return`: a normal host exit runs the CRT's atexit table, which the game shares with gta6rt (its 692 // Not `return`: a normal host exit runs the CRT's atexit table, which the game shares with gta6rt (its
383 // handlers would run against a game that has stopped) and whose last word decides the exit status. 693 // handlers would run against a game that has stopped) and whose last word decides the exit status.
384 // ExitProcess also skips atexit, so the TLS clone cleanup registered there is run here instead. 694 // ExitProcess also skips atexit, so the TLS clone cleanup registered there is run here instead.
695 // The list-value threads end first: ExitProcess must not cut one off inside the layer (in a held submission,
696 // holding its queues' lock).
697 rt::d3d12_xs::stop_list_values(500);
698#ifdef LEONIDA_VULKAN_DEVICE
699 rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs);
700 rt::d3d12::save_pipeline_caches_at_exit(kExitCacheWaitMs);
701#endif
385 rt::cleanup_tls_clones(); 702 rt::cleanup_tls_clones();
386 ExitProcess(static_cast<UINT>(code)); 703 ExitProcess(static_cast<UINT>(code));
387 } catch (const std::exception& e) { 704 } catch (const std::exception& e) {